blob: d0d46295b65254ac4b868db1f5a63077fc0718c2 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/init.h>
25#include <linux/delay.h>
26#include <linux/interrupt.h>
27#include <linux/slab.h>
28#include <linux/pci.h>
29#include <linux/firmware.h>
30#include <linux/moduleparam.h>
31
32#include <sound/core.h>
33#include <sound/control.h>
34#include <sound/pcm.h>
35#include <sound/info.h>
36#include <sound/asoundef.h>
37#include <sound/rawmidi.h>
38#include <sound/hwdep.h>
39#include <sound/initval.h>
40#include <sound/hdsp.h>
41
42#include <asm/byteorder.h>
43#include <asm/current.h>
44#include <asm/io.h>
45
46static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
47static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
48static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
49
50module_param_array(index, int, NULL, 0444);
51MODULE_PARM_DESC(index, "Index value for RME Hammerfall DSP interface.");
52module_param_array(id, charp, NULL, 0444);
53MODULE_PARM_DESC(id, "ID string for RME Hammerfall DSP interface.");
54module_param_array(enable, bool, NULL, 0444);
55MODULE_PARM_DESC(enable, "Enable/disable specific Hammerfall DSP soundcards.");
56MODULE_AUTHOR("Paul Davis <paul@linuxaudiosystems.com>, Marcus Andersson, Thomas Charbonnel <thomas@undata.org>");
57MODULE_DESCRIPTION("RME Hammerfall DSP");
58MODULE_LICENSE("GPL");
59MODULE_SUPPORTED_DEVICE("{{RME Hammerfall-DSP},"
60 "{RME HDSP-9652},"
61 "{RME HDSP-9632}}");
Clemens Ladisch7e0af292007-05-03 17:59:54 +020062#ifdef HDSP_FW_LOADER
63MODULE_FIRMWARE("multiface_firmware.bin");
64MODULE_FIRMWARE("multiface_firmware_rev11.bin");
65MODULE_FIRMWARE("digiface_firmware.bin");
66MODULE_FIRMWARE("digiface_firmware_rev11.bin");
67#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69#define HDSP_MAX_CHANNELS 26
70#define HDSP_MAX_DS_CHANNELS 14
71#define HDSP_MAX_QS_CHANNELS 8
72#define DIGIFACE_SS_CHANNELS 26
73#define DIGIFACE_DS_CHANNELS 14
74#define MULTIFACE_SS_CHANNELS 18
75#define MULTIFACE_DS_CHANNELS 14
76#define H9652_SS_CHANNELS 26
77#define H9652_DS_CHANNELS 14
78/* This does not include possible Analog Extension Boards
79 AEBs are detected at card initialization
80*/
81#define H9632_SS_CHANNELS 12
82#define H9632_DS_CHANNELS 8
83#define H9632_QS_CHANNELS 4
84
85/* Write registers. These are defined as byte-offsets from the iobase value.
86 */
87#define HDSP_resetPointer 0
Remy Brunod7923b22006-10-17 12:41:56 +020088#define HDSP_freqReg 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#define HDSP_outputBufferAddress 32
90#define HDSP_inputBufferAddress 36
91#define HDSP_controlRegister 64
92#define HDSP_interruptConfirmation 96
93#define HDSP_outputEnable 128
94#define HDSP_control2Reg 256
95#define HDSP_midiDataOut0 352
96#define HDSP_midiDataOut1 356
97#define HDSP_fifoData 368
98#define HDSP_inputEnable 384
99
100/* Read registers. These are defined as byte-offsets from the iobase value
101 */
102
103#define HDSP_statusRegister 0
104#define HDSP_timecode 128
105#define HDSP_status2Register 192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#define HDSP_midiDataIn0 360
107#define HDSP_midiDataIn1 364
108#define HDSP_midiStatusOut0 384
109#define HDSP_midiStatusOut1 388
110#define HDSP_midiStatusIn0 392
111#define HDSP_midiStatusIn1 396
112#define HDSP_fifoStatus 400
113
114/* the meters are regular i/o-mapped registers, but offset
115 considerably from the rest. the peak registers are reset
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100116 when read; the least-significant 4 bits are full-scale counters;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 the actual peak value is in the most-significant 24 bits.
118*/
119
120#define HDSP_playbackPeakLevel 4096 /* 26 * 32 bit values */
121#define HDSP_inputPeakLevel 4224 /* 26 * 32 bit values */
122#define HDSP_outputPeakLevel 4352 /* (26+2) * 32 bit values */
123#define HDSP_playbackRmsLevel 4612 /* 26 * 64 bit values */
124#define HDSP_inputRmsLevel 4868 /* 26 * 64 bit values */
125
126
127/* This is for H9652 cards
128 Peak values are read downward from the base
129 Rms values are read upward
130 There are rms values for the outputs too
131 26*3 values are read in ss mode
132 14*3 in ds mode, with no gap between values
133*/
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100134#define HDSP_9652_peakBase 7164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135#define HDSP_9652_rmsBase 4096
136
137/* c.f. the hdsp_9632_meters_t struct */
138#define HDSP_9632_metersBase 4096
139
140#define HDSP_IO_EXTENT 7168
141
142/* control2 register bits */
143
144#define HDSP_TMS 0x01
145#define HDSP_TCK 0x02
146#define HDSP_TDI 0x04
147#define HDSP_JTAG 0x08
148#define HDSP_PWDN 0x10
149#define HDSP_PROGRAM 0x020
150#define HDSP_CONFIG_MODE_0 0x040
151#define HDSP_CONFIG_MODE_1 0x080
152#define HDSP_VERSION_BIT 0x100
153#define HDSP_BIGENDIAN_MODE 0x200
154#define HDSP_RD_MULTIPLE 0x400
155#define HDSP_9652_ENABLE_MIXER 0x800
156#define HDSP_TDO 0x10000000
157
158#define HDSP_S_PROGRAM (HDSP_PROGRAM|HDSP_CONFIG_MODE_0)
159#define HDSP_S_LOAD (HDSP_PROGRAM|HDSP_CONFIG_MODE_1)
160
161/* Control Register bits */
162
163#define HDSP_Start (1<<0) /* start engine */
164#define HDSP_Latency0 (1<<1) /* buffer size = 2^n where n is defined by Latency{2,1,0} */
165#define HDSP_Latency1 (1<<2) /* [ see above ] */
166#define HDSP_Latency2 (1<<3) /* [ see above ] */
167#define HDSP_ClockModeMaster (1<<4) /* 1=Master, 0=Slave/Autosync */
168#define HDSP_AudioInterruptEnable (1<<5) /* what do you think ? */
169#define HDSP_Frequency0 (1<<6) /* 0=44.1kHz/88.2kHz/176.4kHz 1=48kHz/96kHz/192kHz */
170#define HDSP_Frequency1 (1<<7) /* 0=32kHz/64kHz/128kHz */
171#define HDSP_DoubleSpeed (1<<8) /* 0=normal speed, 1=double speed */
172#define HDSP_SPDIFProfessional (1<<9) /* 0=consumer, 1=professional */
173#define HDSP_SPDIFEmphasis (1<<10) /* 0=none, 1=on */
174#define HDSP_SPDIFNonAudio (1<<11) /* 0=off, 1=on */
175#define HDSP_SPDIFOpticalOut (1<<12) /* 1=use 1st ADAT connector for SPDIF, 0=do not */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100176#define HDSP_SyncRef2 (1<<13)
177#define HDSP_SPDIFInputSelect0 (1<<14)
178#define HDSP_SPDIFInputSelect1 (1<<15)
179#define HDSP_SyncRef0 (1<<16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180#define HDSP_SyncRef1 (1<<17)
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100181#define HDSP_AnalogExtensionBoard (1<<18) /* For H9632 cards */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182#define HDSP_XLRBreakoutCable (1<<20) /* For H9632 cards */
183#define HDSP_Midi0InterruptEnable (1<<22)
184#define HDSP_Midi1InterruptEnable (1<<23)
185#define HDSP_LineOut (1<<24)
186#define HDSP_ADGain0 (1<<25) /* From here : H9632 specific */
187#define HDSP_ADGain1 (1<<26)
188#define HDSP_DAGain0 (1<<27)
189#define HDSP_DAGain1 (1<<28)
190#define HDSP_PhoneGain0 (1<<29)
191#define HDSP_PhoneGain1 (1<<30)
192#define HDSP_QuadSpeed (1<<31)
193
194#define HDSP_ADGainMask (HDSP_ADGain0|HDSP_ADGain1)
195#define HDSP_ADGainMinus10dBV HDSP_ADGainMask
196#define HDSP_ADGainPlus4dBu (HDSP_ADGain0)
197#define HDSP_ADGainLowGain 0
198
199#define HDSP_DAGainMask (HDSP_DAGain0|HDSP_DAGain1)
200#define HDSP_DAGainHighGain HDSP_DAGainMask
201#define HDSP_DAGainPlus4dBu (HDSP_DAGain0)
202#define HDSP_DAGainMinus10dBV 0
203
204#define HDSP_PhoneGainMask (HDSP_PhoneGain0|HDSP_PhoneGain1)
205#define HDSP_PhoneGain0dB HDSP_PhoneGainMask
206#define HDSP_PhoneGainMinus6dB (HDSP_PhoneGain0)
207#define HDSP_PhoneGainMinus12dB 0
208
209#define HDSP_LatencyMask (HDSP_Latency0|HDSP_Latency1|HDSP_Latency2)
210#define HDSP_FrequencyMask (HDSP_Frequency0|HDSP_Frequency1|HDSP_DoubleSpeed|HDSP_QuadSpeed)
211
212#define HDSP_SPDIFInputMask (HDSP_SPDIFInputSelect0|HDSP_SPDIFInputSelect1)
213#define HDSP_SPDIFInputADAT1 0
214#define HDSP_SPDIFInputCoaxial (HDSP_SPDIFInputSelect0)
215#define HDSP_SPDIFInputCdrom (HDSP_SPDIFInputSelect1)
216#define HDSP_SPDIFInputAES (HDSP_SPDIFInputSelect0|HDSP_SPDIFInputSelect1)
217
218#define HDSP_SyncRefMask (HDSP_SyncRef0|HDSP_SyncRef1|HDSP_SyncRef2)
219#define HDSP_SyncRef_ADAT1 0
220#define HDSP_SyncRef_ADAT2 (HDSP_SyncRef0)
221#define HDSP_SyncRef_ADAT3 (HDSP_SyncRef1)
222#define HDSP_SyncRef_SPDIF (HDSP_SyncRef0|HDSP_SyncRef1)
223#define HDSP_SyncRef_WORD (HDSP_SyncRef2)
224#define HDSP_SyncRef_ADAT_SYNC (HDSP_SyncRef0|HDSP_SyncRef2)
225
226/* Sample Clock Sources */
227
228#define HDSP_CLOCK_SOURCE_AUTOSYNC 0
229#define HDSP_CLOCK_SOURCE_INTERNAL_32KHZ 1
230#define HDSP_CLOCK_SOURCE_INTERNAL_44_1KHZ 2
231#define HDSP_CLOCK_SOURCE_INTERNAL_48KHZ 3
232#define HDSP_CLOCK_SOURCE_INTERNAL_64KHZ 4
233#define HDSP_CLOCK_SOURCE_INTERNAL_88_2KHZ 5
234#define HDSP_CLOCK_SOURCE_INTERNAL_96KHZ 6
235#define HDSP_CLOCK_SOURCE_INTERNAL_128KHZ 7
236#define HDSP_CLOCK_SOURCE_INTERNAL_176_4KHZ 8
237#define HDSP_CLOCK_SOURCE_INTERNAL_192KHZ 9
238
239/* Preferred sync reference choices - used by "pref_sync_ref" control switch */
240
241#define HDSP_SYNC_FROM_WORD 0
242#define HDSP_SYNC_FROM_SPDIF 1
243#define HDSP_SYNC_FROM_ADAT1 2
244#define HDSP_SYNC_FROM_ADAT_SYNC 3
245#define HDSP_SYNC_FROM_ADAT2 4
246#define HDSP_SYNC_FROM_ADAT3 5
247
248/* SyncCheck status */
249
250#define HDSP_SYNC_CHECK_NO_LOCK 0
251#define HDSP_SYNC_CHECK_LOCK 1
252#define HDSP_SYNC_CHECK_SYNC 2
253
254/* AutoSync references - used by "autosync_ref" control switch */
255
256#define HDSP_AUTOSYNC_FROM_WORD 0
257#define HDSP_AUTOSYNC_FROM_ADAT_SYNC 1
258#define HDSP_AUTOSYNC_FROM_SPDIF 2
259#define HDSP_AUTOSYNC_FROM_NONE 3
260#define HDSP_AUTOSYNC_FROM_ADAT1 4
261#define HDSP_AUTOSYNC_FROM_ADAT2 5
262#define HDSP_AUTOSYNC_FROM_ADAT3 6
263
264/* Possible sources of S/PDIF input */
265
266#define HDSP_SPDIFIN_OPTICAL 0 /* optical (ADAT1) */
267#define HDSP_SPDIFIN_COAXIAL 1 /* coaxial (RCA) */
268#define HDSP_SPDIFIN_INTERNAL 2 /* internal (CDROM) */
269#define HDSP_SPDIFIN_AES 3 /* xlr for H9632 (AES)*/
270
271#define HDSP_Frequency32KHz HDSP_Frequency0
272#define HDSP_Frequency44_1KHz HDSP_Frequency1
273#define HDSP_Frequency48KHz (HDSP_Frequency1|HDSP_Frequency0)
274#define HDSP_Frequency64KHz (HDSP_DoubleSpeed|HDSP_Frequency0)
275#define HDSP_Frequency88_2KHz (HDSP_DoubleSpeed|HDSP_Frequency1)
276#define HDSP_Frequency96KHz (HDSP_DoubleSpeed|HDSP_Frequency1|HDSP_Frequency0)
277/* For H9632 cards */
278#define HDSP_Frequency128KHz (HDSP_QuadSpeed|HDSP_DoubleSpeed|HDSP_Frequency0)
279#define HDSP_Frequency176_4KHz (HDSP_QuadSpeed|HDSP_DoubleSpeed|HDSP_Frequency1)
280#define HDSP_Frequency192KHz (HDSP_QuadSpeed|HDSP_DoubleSpeed|HDSP_Frequency1|HDSP_Frequency0)
Julian Cablee4b60882007-03-19 11:44:40 +0100281/* RME says n = 104857600000000, but in the windows MADI driver, I see:
282 return 104857600000000 / rate; // 100 MHz
283 return 110100480000000 / rate; // 105 MHz
284*/
285#define DDS_NUMERATOR 104857600000000ULL; /* = 2^20 * 10^8 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287#define hdsp_encode_latency(x) (((x)<<1) & HDSP_LatencyMask)
288#define hdsp_decode_latency(x) (((x) & HDSP_LatencyMask)>>1)
289
290#define hdsp_encode_spdif_in(x) (((x)&0x3)<<14)
291#define hdsp_decode_spdif_in(x) (((x)>>14)&0x3)
292
293/* Status Register bits */
294
295#define HDSP_audioIRQPending (1<<0)
296#define HDSP_Lock2 (1<<1) /* this is for Digiface and H9652 */
297#define HDSP_spdifFrequency3 HDSP_Lock2 /* this is for H9632 only */
298#define HDSP_Lock1 (1<<2)
299#define HDSP_Lock0 (1<<3)
300#define HDSP_SPDIFSync (1<<4)
301#define HDSP_TimecodeLock (1<<5)
302#define HDSP_BufferPositionMask 0x000FFC0 /* Bit 6..15 : h/w buffer pointer */
303#define HDSP_Sync2 (1<<16)
304#define HDSP_Sync1 (1<<17)
305#define HDSP_Sync0 (1<<18)
306#define HDSP_DoubleSpeedStatus (1<<19)
307#define HDSP_ConfigError (1<<20)
308#define HDSP_DllError (1<<21)
309#define HDSP_spdifFrequency0 (1<<22)
310#define HDSP_spdifFrequency1 (1<<23)
311#define HDSP_spdifFrequency2 (1<<24)
312#define HDSP_SPDIFErrorFlag (1<<25)
313#define HDSP_BufferID (1<<26)
314#define HDSP_TimecodeSync (1<<27)
315#define HDSP_AEBO (1<<28) /* H9632 specific Analog Extension Boards */
316#define HDSP_AEBI (1<<29) /* 0 = present, 1 = absent */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100317#define HDSP_midi0IRQPending (1<<30)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318#define HDSP_midi1IRQPending (1<<31)
319
320#define HDSP_spdifFrequencyMask (HDSP_spdifFrequency0|HDSP_spdifFrequency1|HDSP_spdifFrequency2)
Remy Bruno47ba97f2008-02-22 17:57:02 +0100321#define HDSP_spdifFrequencyMask_9632 (HDSP_spdifFrequency0|\
322 HDSP_spdifFrequency1|\
323 HDSP_spdifFrequency2|\
324 HDSP_spdifFrequency3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326#define HDSP_spdifFrequency32KHz (HDSP_spdifFrequency0)
327#define HDSP_spdifFrequency44_1KHz (HDSP_spdifFrequency1)
328#define HDSP_spdifFrequency48KHz (HDSP_spdifFrequency0|HDSP_spdifFrequency1)
329
330#define HDSP_spdifFrequency64KHz (HDSP_spdifFrequency2)
331#define HDSP_spdifFrequency88_2KHz (HDSP_spdifFrequency0|HDSP_spdifFrequency2)
332#define HDSP_spdifFrequency96KHz (HDSP_spdifFrequency2|HDSP_spdifFrequency1)
333
334/* This is for H9632 cards */
Remy Bruno47ba97f2008-02-22 17:57:02 +0100335#define HDSP_spdifFrequency128KHz (HDSP_spdifFrequency0|\
336 HDSP_spdifFrequency1|\
337 HDSP_spdifFrequency2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338#define HDSP_spdifFrequency176_4KHz HDSP_spdifFrequency3
339#define HDSP_spdifFrequency192KHz (HDSP_spdifFrequency3|HDSP_spdifFrequency0)
340
341/* Status2 Register bits */
342
343#define HDSP_version0 (1<<0)
344#define HDSP_version1 (1<<1)
345#define HDSP_version2 (1<<2)
346#define HDSP_wc_lock (1<<3)
347#define HDSP_wc_sync (1<<4)
348#define HDSP_inp_freq0 (1<<5)
349#define HDSP_inp_freq1 (1<<6)
350#define HDSP_inp_freq2 (1<<7)
351#define HDSP_SelSyncRef0 (1<<8)
352#define HDSP_SelSyncRef1 (1<<9)
353#define HDSP_SelSyncRef2 (1<<10)
354
355#define HDSP_wc_valid (HDSP_wc_lock|HDSP_wc_sync)
356
357#define HDSP_systemFrequencyMask (HDSP_inp_freq0|HDSP_inp_freq1|HDSP_inp_freq2)
358#define HDSP_systemFrequency32 (HDSP_inp_freq0)
359#define HDSP_systemFrequency44_1 (HDSP_inp_freq1)
360#define HDSP_systemFrequency48 (HDSP_inp_freq0|HDSP_inp_freq1)
361#define HDSP_systemFrequency64 (HDSP_inp_freq2)
362#define HDSP_systemFrequency88_2 (HDSP_inp_freq0|HDSP_inp_freq2)
363#define HDSP_systemFrequency96 (HDSP_inp_freq1|HDSP_inp_freq2)
364/* FIXME : more values for 9632 cards ? */
365
366#define HDSP_SelSyncRefMask (HDSP_SelSyncRef0|HDSP_SelSyncRef1|HDSP_SelSyncRef2)
367#define HDSP_SelSyncRef_ADAT1 0
368#define HDSP_SelSyncRef_ADAT2 (HDSP_SelSyncRef0)
369#define HDSP_SelSyncRef_ADAT3 (HDSP_SelSyncRef1)
370#define HDSP_SelSyncRef_SPDIF (HDSP_SelSyncRef0|HDSP_SelSyncRef1)
371#define HDSP_SelSyncRef_WORD (HDSP_SelSyncRef2)
372#define HDSP_SelSyncRef_ADAT_SYNC (HDSP_SelSyncRef0|HDSP_SelSyncRef2)
373
374/* Card state flags */
375
376#define HDSP_InitializationComplete (1<<0)
377#define HDSP_FirmwareLoaded (1<<1)
378#define HDSP_FirmwareCached (1<<2)
379
380/* FIFO wait times, defined in terms of 1/10ths of msecs */
381
382#define HDSP_LONG_WAIT 5000
383#define HDSP_SHORT_WAIT 30
384
385#define UNITY_GAIN 32768
386#define MINUS_INFINITY_GAIN 0
387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388/* the size of a substream (1 mono data stream) */
389
390#define HDSP_CHANNEL_BUFFER_SAMPLES (16*1024)
391#define HDSP_CHANNEL_BUFFER_BYTES (4*HDSP_CHANNEL_BUFFER_SAMPLES)
392
393/* the size of the area we need to allocate for DMA transfers. the
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100394 size is the same regardless of the number of channels - the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 Multiface still uses the same memory area.
396
397 Note that we allocate 1 more channel than is apparently needed
398 because the h/w seems to write 1 byte beyond the end of the last
399 page. Sigh.
400*/
401
402#define HDSP_DMA_AREA_BYTES ((HDSP_MAX_CHANNELS+1) * HDSP_CHANNEL_BUFFER_BYTES)
403#define HDSP_DMA_AREA_KILOBYTES (HDSP_DMA_AREA_BYTES/1024)
404
Raphael Doursenaud8e365f92009-05-12 11:49:39 +0200405/* use hotplug firmware loader? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406#if defined(CONFIG_FW_LOADER) || defined(CONFIG_FW_LOADER_MODULE)
Raphael Doursenaud8e365f92009-05-12 11:49:39 +0200407#if !defined(HDSP_USE_HWDEP_LOADER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408#define HDSP_FW_LOADER
409#endif
410#endif
411
Takashi Iwai55e957d2005-11-17 14:52:13 +0100412struct hdsp_9632_meters {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 u32 input_peak[16];
414 u32 playback_peak[16];
415 u32 output_peak[16];
416 u32 xxx_peak[16];
417 u32 padding[64];
418 u32 input_rms_low[16];
419 u32 playback_rms_low[16];
420 u32 output_rms_low[16];
421 u32 xxx_rms_low[16];
422 u32 input_rms_high[16];
423 u32 playback_rms_high[16];
424 u32 output_rms_high[16];
425 u32 xxx_rms_high[16];
426};
427
Takashi Iwai55e957d2005-11-17 14:52:13 +0100428struct hdsp_midi {
429 struct hdsp *hdsp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 int id;
Takashi Iwai55e957d2005-11-17 14:52:13 +0100431 struct snd_rawmidi *rmidi;
432 struct snd_rawmidi_substream *input;
433 struct snd_rawmidi_substream *output;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 char istimer; /* timer in use */
435 struct timer_list timer;
436 spinlock_t lock;
437 int pending;
438};
439
Takashi Iwai55e957d2005-11-17 14:52:13 +0100440struct hdsp {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 spinlock_t lock;
Takashi Iwai55e957d2005-11-17 14:52:13 +0100442 struct snd_pcm_substream *capture_substream;
443 struct snd_pcm_substream *playback_substream;
444 struct hdsp_midi midi[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 struct tasklet_struct midi_tasklet;
446 int use_midi_tasklet;
447 int precise_ptr;
448 u32 control_register; /* cached value */
449 u32 control2_register; /* cached value */
450 u32 creg_spdif;
451 u32 creg_spdif_stream;
Takashi Iwaie3ea4d82005-07-04 18:12:39 +0200452 int clock_source_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 char *card_name; /* digiface/multiface */
Takashi Iwai55e957d2005-11-17 14:52:13 +0100454 enum HDSP_IO_Type io_type; /* ditto, but for code use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 unsigned short firmware_rev;
456 unsigned short state; /* stores state bits */
457 u32 firmware_cache[24413]; /* this helps recover from accidental iobox power failure */
458 size_t period_bytes; /* guess what this is */
459 unsigned char max_channels;
460 unsigned char qs_in_channels; /* quad speed mode for H9632 */
461 unsigned char ds_in_channels;
462 unsigned char ss_in_channels; /* different for multiface/digiface */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100463 unsigned char qs_out_channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 unsigned char ds_out_channels;
465 unsigned char ss_out_channels;
466
467 struct snd_dma_buffer capture_dma_buf;
468 struct snd_dma_buffer playback_dma_buf;
469 unsigned char *capture_buffer; /* suitably aligned address */
470 unsigned char *playback_buffer; /* suitably aligned address */
471
472 pid_t capture_pid;
473 pid_t playback_pid;
474 int running;
475 int system_sample_rate;
476 char *channel_map;
477 int dev;
478 int irq;
479 unsigned long port;
480 void __iomem *iobase;
Takashi Iwai55e957d2005-11-17 14:52:13 +0100481 struct snd_card *card;
482 struct snd_pcm *pcm;
483 struct snd_hwdep *hwdep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 struct pci_dev *pci;
Takashi Iwai55e957d2005-11-17 14:52:13 +0100485 struct snd_kcontrol *spdif_ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 unsigned short mixer_matrix[HDSP_MATRIX_MIXER_SIZE];
Remy Brunod7923b22006-10-17 12:41:56 +0200487 unsigned int dds_value; /* last value written to freq register */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488};
489
490/* These tables map the ALSA channels 1..N to the channels that we
491 need to use in order to find the relevant channel buffer. RME
492 refer to this kind of mapping as between "the ADAT channel and
493 the DMA channel." We index it using the logical audio channel,
494 and the value is the DMA channel (i.e. channel buffer number)
495 where the data for that channel can be read/written from/to.
496*/
497
498static char channel_map_df_ss[HDSP_MAX_CHANNELS] = {
499 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
500 18, 19, 20, 21, 22, 23, 24, 25
501};
502
503static char channel_map_mf_ss[HDSP_MAX_CHANNELS] = { /* Multiface */
504 /* Analog */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100505 0, 1, 2, 3, 4, 5, 6, 7,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 /* ADAT 2 */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100507 16, 17, 18, 19, 20, 21, 22, 23,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 /* SPDIF */
509 24, 25,
510 -1, -1, -1, -1, -1, -1, -1, -1
511};
512
513static char channel_map_ds[HDSP_MAX_CHANNELS] = {
514 /* ADAT channels are remapped */
515 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23,
516 /* channels 12 and 13 are S/PDIF */
517 24, 25,
518 /* others don't exist */
519 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
520};
521
522static char channel_map_H9632_ss[HDSP_MAX_CHANNELS] = {
523 /* ADAT channels */
524 0, 1, 2, 3, 4, 5, 6, 7,
525 /* SPDIF */
526 8, 9,
527 /* Analog */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100528 10, 11,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 /* AO4S-192 and AI4S-192 extension boards */
530 12, 13, 14, 15,
531 /* others don't exist */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100532 -1, -1, -1, -1, -1, -1, -1, -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 -1, -1
534};
535
536static char channel_map_H9632_ds[HDSP_MAX_CHANNELS] = {
537 /* ADAT */
538 1, 3, 5, 7,
539 /* SPDIF */
540 8, 9,
541 /* Analog */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100542 10, 11,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 /* AO4S-192 and AI4S-192 extension boards */
544 12, 13, 14, 15,
545 /* others don't exist */
546 -1, -1, -1, -1, -1, -1, -1, -1,
547 -1, -1, -1, -1, -1, -1
548};
549
550static char channel_map_H9632_qs[HDSP_MAX_CHANNELS] = {
551 /* ADAT is disabled in this mode */
552 /* SPDIF */
553 8, 9,
554 /* Analog */
555 10, 11,
556 /* AO4S-192 and AI4S-192 extension boards */
557 12, 13, 14, 15,
558 /* others don't exist */
559 -1, -1, -1, -1, -1, -1, -1, -1,
560 -1, -1, -1, -1, -1, -1, -1, -1,
561 -1, -1
562};
563
564static int snd_hammerfall_get_buffer(struct pci_dev *pci, struct snd_dma_buffer *dmab, size_t size)
565{
566 dmab->dev.type = SNDRV_DMA_TYPE_DEV;
567 dmab->dev.dev = snd_dma_pci_data(pci);
Takashi Iwaib6a96912005-05-30 18:27:03 +0200568 if (snd_dma_get_reserved_buf(dmab, snd_dma_pci_buf_id(pci))) {
569 if (dmab->bytes >= size)
570 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 }
Takashi Iwaib6a96912005-05-30 18:27:03 +0200572 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
573 size, dmab) < 0)
574 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 return 0;
576}
577
578static void snd_hammerfall_free_buffer(struct snd_dma_buffer *dmab, struct pci_dev *pci)
579{
Takashi Iwaib6a96912005-05-30 18:27:03 +0200580 if (dmab->area) {
581 dmab->dev.dev = NULL; /* make it anonymous */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 snd_dma_reserve_buf(dmab, snd_dma_pci_buf_id(pci));
Takashi Iwaib6a96912005-05-30 18:27:03 +0200583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584}
585
586
Takashi Iwaif40b6892006-07-05 16:51:05 +0200587static struct pci_device_id snd_hdsp_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 {
589 .vendor = PCI_VENDOR_ID_XILINX,
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100590 .device = PCI_DEVICE_ID_XILINX_HAMMERFALL_DSP,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 .subvendor = PCI_ANY_ID,
592 .subdevice = PCI_ANY_ID,
593 }, /* RME Hammerfall-DSP */
594 { 0, },
595};
596
597MODULE_DEVICE_TABLE(pci, snd_hdsp_ids);
598
599/* prototypes */
Takashi Iwai55e957d2005-11-17 14:52:13 +0100600static int snd_hdsp_create_alsa_devices(struct snd_card *card, struct hdsp *hdsp);
601static int snd_hdsp_create_pcm(struct snd_card *card, struct hdsp *hdsp);
602static int snd_hdsp_enable_io (struct hdsp *hdsp);
603static void snd_hdsp_initialize_midi_flush (struct hdsp *hdsp);
604static void snd_hdsp_initialize_channels (struct hdsp *hdsp);
605static int hdsp_fifo_wait(struct hdsp *hdsp, int count, int timeout);
606static int hdsp_autosync_ref(struct hdsp *hdsp);
607static int snd_hdsp_set_defaults(struct hdsp *hdsp);
608static void snd_hdsp_9652_enable_mixer (struct hdsp *hdsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Takashi Iwai55e957d2005-11-17 14:52:13 +0100610static int hdsp_playback_to_output_key (struct hdsp *hdsp, int in, int out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
Remy Brunoa3a68c82007-08-31 12:33:54 +0200612 switch (hdsp->io_type) {
613 case Multiface:
614 case Digiface:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 default:
Andreas Degert192b8e32008-01-16 15:59:48 +0100616 if (hdsp->firmware_rev == 0xa)
617 return (64 * out) + (32 + (in));
618 else
619 return (52 * out) + (26 + (in));
Remy Brunoa3a68c82007-08-31 12:33:54 +0200620 case H9632:
621 return (32 * out) + (16 + (in));
622 case H9652:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return (52 * out) + (26 + (in));
624 }
625}
626
Takashi Iwai55e957d2005-11-17 14:52:13 +0100627static int hdsp_input_to_output_key (struct hdsp *hdsp, int in, int out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
Remy Brunoa3a68c82007-08-31 12:33:54 +0200629 switch (hdsp->io_type) {
630 case Multiface:
631 case Digiface:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 default:
Andreas Degert192b8e32008-01-16 15:59:48 +0100633 if (hdsp->firmware_rev == 0xa)
634 return (64 * out) + in;
635 else
636 return (52 * out) + in;
Remy Brunoa3a68c82007-08-31 12:33:54 +0200637 case H9632:
638 return (32 * out) + in;
639 case H9652:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 return (52 * out) + in;
641 }
642}
643
Takashi Iwai55e957d2005-11-17 14:52:13 +0100644static void hdsp_write(struct hdsp *hdsp, int reg, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
646 writel(val, hdsp->iobase + reg);
647}
648
Takashi Iwai55e957d2005-11-17 14:52:13 +0100649static unsigned int hdsp_read(struct hdsp *hdsp, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
651 return readl (hdsp->iobase + reg);
652}
653
Takashi Iwai55e957d2005-11-17 14:52:13 +0100654static int hdsp_check_for_iobox (struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 if (hdsp->io_type == H9652 || hdsp->io_type == H9632) return 0;
657 if (hdsp_read (hdsp, HDSP_statusRegister) & HDSP_ConfigError) {
658 snd_printk ("Hammerfall-DSP: no Digiface or Multiface connected!\n");
659 hdsp->state &= ~HDSP_FirmwareLoaded;
660 return -EIO;
661 }
662 return 0;
Tim Blechmanne588ed82009-02-20 19:30:35 +0100663}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Tim Blechmanne588ed82009-02-20 19:30:35 +0100665static int hdsp_wait_for_iobox(struct hdsp *hdsp, unsigned int loops,
666 unsigned int delay)
667{
668 unsigned int i;
669
670 if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
671 return 0;
672
673 for (i = 0; i != loops; ++i) {
674 if (hdsp_read(hdsp, HDSP_statusRegister) & HDSP_ConfigError)
675 msleep(delay);
676 else {
677 snd_printd("Hammerfall-DSP: iobox found after %ums!\n",
678 i * delay);
679 return 0;
680 }
681 }
682
683 snd_printk("Hammerfall-DSP: no Digiface or Multiface connected!\n");
684 hdsp->state &= ~HDSP_FirmwareLoaded;
685 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686}
687
Takashi Iwai55e957d2005-11-17 14:52:13 +0100688static int snd_hdsp_load_firmware_from_cache(struct hdsp *hdsp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 int i;
691 unsigned long flags;
692
693 if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 snd_printk ("Hammerfall-DSP: loading firmware\n");
696
697 hdsp_write (hdsp, HDSP_control2Reg, HDSP_S_PROGRAM);
698 hdsp_write (hdsp, HDSP_fifoData, 0);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100699
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 if (hdsp_fifo_wait (hdsp, 0, HDSP_LONG_WAIT)) {
701 snd_printk ("Hammerfall-DSP: timeout waiting for download preparation\n");
702 return -EIO;
703 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 hdsp_write (hdsp, HDSP_control2Reg, HDSP_S_LOAD);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 for (i = 0; i < 24413; ++i) {
708 hdsp_write(hdsp, HDSP_fifoData, hdsp->firmware_cache[i]);
709 if (hdsp_fifo_wait (hdsp, 127, HDSP_LONG_WAIT)) {
710 snd_printk ("Hammerfall-DSP: timeout during firmware loading\n");
711 return -EIO;
712 }
713 }
714
Takashi Iwaib0b98112005-10-20 18:29:58 +0200715 ssleep(3);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 if (hdsp_fifo_wait (hdsp, 0, HDSP_LONG_WAIT)) {
718 snd_printk ("Hammerfall-DSP: timeout at end of firmware loading\n");
719 return -EIO;
720 }
721
722#ifdef SNDRV_BIG_ENDIAN
723 hdsp->control2_register = HDSP_BIGENDIAN_MODE;
724#else
725 hdsp->control2_register = 0;
726#endif
727 hdsp_write (hdsp, HDSP_control2Reg, hdsp->control2_register);
728 snd_printk ("Hammerfall-DSP: finished firmware loading\n");
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 }
731 if (hdsp->state & HDSP_InitializationComplete) {
Takashi Iwaib0b98112005-10-20 18:29:58 +0200732 snd_printk(KERN_INFO "Hammerfall-DSP: firmware loaded from cache, restoring defaults\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 spin_lock_irqsave(&hdsp->lock, flags);
734 snd_hdsp_set_defaults(hdsp);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100735 spin_unlock_irqrestore(&hdsp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 hdsp->state |= HDSP_FirmwareLoaded;
739
740 return 0;
741}
742
Takashi Iwai55e957d2005-11-17 14:52:13 +0100743static int hdsp_get_iobox_version (struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
745 if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 hdsp_write (hdsp, HDSP_control2Reg, HDSP_PROGRAM);
748 hdsp_write (hdsp, HDSP_fifoData, 0);
Takashi Iwaib0b98112005-10-20 18:29:58 +0200749 if (hdsp_fifo_wait (hdsp, 0, HDSP_SHORT_WAIT) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752 hdsp_write (hdsp, HDSP_control2Reg, HDSP_S_LOAD);
753 hdsp_write (hdsp, HDSP_fifoData, 0);
754
755 if (hdsp_fifo_wait (hdsp, 0, HDSP_SHORT_WAIT)) {
756 hdsp->io_type = Multiface;
757 hdsp_write (hdsp, HDSP_control2Reg, HDSP_VERSION_BIT);
758 hdsp_write (hdsp, HDSP_control2Reg, HDSP_S_LOAD);
759 hdsp_fifo_wait (hdsp, 0, HDSP_SHORT_WAIT);
760 } else {
761 hdsp->io_type = Digiface;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 } else {
764 /* firmware was already loaded, get iobox type */
Takashi Iwaib0b98112005-10-20 18:29:58 +0200765 if (hdsp_read(hdsp, HDSP_status2Register) & HDSP_version1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 hdsp->io_type = Multiface;
Takashi Iwaib0b98112005-10-20 18:29:58 +0200767 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 hdsp->io_type = Digiface;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 }
770 return 0;
771}
772
773
Takashi Iwai311e70a2006-09-06 12:13:37 +0200774#ifdef HDSP_FW_LOADER
Takashi Iwai92eed662008-02-22 18:35:56 +0100775static int hdsp_request_fw_loader(struct hdsp *hdsp);
Takashi Iwai311e70a2006-09-06 12:13:37 +0200776#endif
777
778static int hdsp_check_for_firmware (struct hdsp *hdsp, int load_on_demand)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
Takashi Iwai311e70a2006-09-06 12:13:37 +0200780 if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
781 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 hdsp->state &= ~HDSP_FirmwareLoaded;
Takashi Iwai311e70a2006-09-06 12:13:37 +0200784 if (! load_on_demand)
Takashi Iwaib0b98112005-10-20 18:29:58 +0200785 return -EIO;
Takashi Iwai311e70a2006-09-06 12:13:37 +0200786 snd_printk(KERN_ERR "Hammerfall-DSP: firmware not present.\n");
Takashi Iwaib0b98112005-10-20 18:29:58 +0200787 /* try to load firmware */
Takashi Iwai311e70a2006-09-06 12:13:37 +0200788 if (! (hdsp->state & HDSP_FirmwareCached)) {
789#ifdef HDSP_FW_LOADER
790 if (! hdsp_request_fw_loader(hdsp))
791 return 0;
792#endif
793 snd_printk(KERN_ERR
794 "Hammerfall-DSP: No firmware loaded nor "
795 "cached, please upload firmware.\n");
796 return -EIO;
Takashi Iwaib0b98112005-10-20 18:29:58 +0200797 }
Takashi Iwai311e70a2006-09-06 12:13:37 +0200798 if (snd_hdsp_load_firmware_from_cache(hdsp) != 0) {
799 snd_printk(KERN_ERR
800 "Hammerfall-DSP: Firmware loading from "
801 "cache failed, please upload manually.\n");
802 return -EIO;
803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 }
805 return 0;
806}
807
808
Takashi Iwai55e957d2005-11-17 14:52:13 +0100809static int hdsp_fifo_wait(struct hdsp *hdsp, int count, int timeout)
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100810{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 int i;
812
813 /* the fifoStatus registers reports on how many words
814 are available in the command FIFO.
815 */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 for (i = 0; i < timeout; i++) {
818
819 if ((int)(hdsp_read (hdsp, HDSP_fifoStatus) & 0xff) <= count)
820 return 0;
821
822 /* not very friendly, but we only do this during a firmware
823 load and changing the mixer, so we just put up with it.
824 */
825
826 udelay (100);
827 }
828
829 snd_printk ("Hammerfall-DSP: wait for FIFO status <= %d failed after %d iterations\n",
830 count, timeout);
831 return -1;
832}
833
Takashi Iwai55e957d2005-11-17 14:52:13 +0100834static int hdsp_read_gain (struct hdsp *hdsp, unsigned int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
Takashi Iwaib0b98112005-10-20 18:29:58 +0200836 if (addr >= HDSP_MATRIX_MIXER_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 return 0;
Takashi Iwaib0b98112005-10-20 18:29:58 +0200838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 return hdsp->mixer_matrix[addr];
840}
841
Takashi Iwai55e957d2005-11-17 14:52:13 +0100842static int hdsp_write_gain(struct hdsp *hdsp, unsigned int addr, unsigned short data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
844 unsigned int ad;
845
846 if (addr >= HDSP_MATRIX_MIXER_SIZE)
847 return -1;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 if (hdsp->io_type == H9652 || hdsp->io_type == H9632) {
850
851 /* from martin bjornsen:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 "You can only write dwords to the
854 mixer memory which contain two
855 mixer values in the low and high
856 word. So if you want to change
857 value 0 you have to read value 1
858 from the cache and write both to
859 the first dword in the mixer
860 memory."
861 */
862
Takashi Iwaib0b98112005-10-20 18:29:58 +0200863 if (hdsp->io_type == H9632 && addr >= 512)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Takashi Iwaib0b98112005-10-20 18:29:58 +0200866 if (hdsp->io_type == H9652 && addr >= 1352)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
869 hdsp->mixer_matrix[addr] = data;
870
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 /* `addr' addresses a 16-bit wide address, but
873 the address space accessed via hdsp_write
874 uses byte offsets. put another way, addr
875 varies from 0 to 1351, but to access the
876 corresponding memory location, we need
877 to access 0 to 2703 ...
878 */
879 ad = addr/2;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100880
881 hdsp_write (hdsp, 4096 + (ad*4),
882 (hdsp->mixer_matrix[(addr&0x7fe)+1] << 16) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 hdsp->mixer_matrix[addr&0x7fe]);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 return 0;
886
887 } else {
888
889 ad = (addr << 16) + data;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100890
Takashi Iwaib0b98112005-10-20 18:29:58 +0200891 if (hdsp_fifo_wait(hdsp, 127, HDSP_LONG_WAIT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894 hdsp_write (hdsp, HDSP_fifoData, ad);
895 hdsp->mixer_matrix[addr] = data;
896
897 }
898
899 return 0;
900}
901
Takashi Iwai55e957d2005-11-17 14:52:13 +0100902static int snd_hdsp_use_is_exclusive(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
904 unsigned long flags;
905 int ret = 1;
906
907 spin_lock_irqsave(&hdsp->lock, flags);
908 if ((hdsp->playback_pid != hdsp->capture_pid) &&
Takashi Iwaib0b98112005-10-20 18:29:58 +0200909 (hdsp->playback_pid >= 0) && (hdsp->capture_pid >= 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 spin_unlock_irqrestore(&hdsp->lock, flags);
912 return ret;
913}
914
Takashi Iwai55e957d2005-11-17 14:52:13 +0100915static int hdsp_spdif_sample_rate(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
917 unsigned int status = hdsp_read(hdsp, HDSP_statusRegister);
918 unsigned int rate_bits = (status & HDSP_spdifFrequencyMask);
919
Remy Bruno47ba97f2008-02-22 17:57:02 +0100920 /* For the 9632, the mask is different */
921 if (hdsp->io_type == H9632)
922 rate_bits = (status & HDSP_spdifFrequencyMask_9632);
923
Takashi Iwaib0b98112005-10-20 18:29:58 +0200924 if (status & HDSP_SPDIFErrorFlag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 return 0;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 switch (rate_bits) {
928 case HDSP_spdifFrequency32KHz: return 32000;
929 case HDSP_spdifFrequency44_1KHz: return 44100;
930 case HDSP_spdifFrequency48KHz: return 48000;
931 case HDSP_spdifFrequency64KHz: return 64000;
932 case HDSP_spdifFrequency88_2KHz: return 88200;
933 case HDSP_spdifFrequency96KHz: return 96000;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100934 case HDSP_spdifFrequency128KHz:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 if (hdsp->io_type == H9632) return 128000;
936 break;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100937 case HDSP_spdifFrequency176_4KHz:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 if (hdsp->io_type == H9632) return 176400;
939 break;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100940 case HDSP_spdifFrequency192KHz:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 if (hdsp->io_type == H9632) return 192000;
942 break;
943 default:
944 break;
945 }
946 snd_printk ("Hammerfall-DSP: unknown spdif frequency status; bits = 0x%x, status = 0x%x\n", rate_bits, status);
947 return 0;
948}
949
Remy Bruno47ba97f2008-02-22 17:57:02 +0100950static int hdsp_external_sample_rate(struct hdsp *hdsp)
951{
952 unsigned int status2 = hdsp_read(hdsp, HDSP_status2Register);
953 unsigned int rate_bits = status2 & HDSP_systemFrequencyMask;
954
955 /* For the 9632 card, there seems to be no bit for indicating external
956 * sample rate greater than 96kHz. The card reports the corresponding
957 * single speed. So the best means seems to get spdif rate when
958 * autosync reference is spdif */
959 if (hdsp->io_type == H9632 &&
960 hdsp_autosync_ref(hdsp) == HDSP_AUTOSYNC_FROM_SPDIF)
961 return hdsp_spdif_sample_rate(hdsp);
962
963 switch (rate_bits) {
964 case HDSP_systemFrequency32: return 32000;
965 case HDSP_systemFrequency44_1: return 44100;
966 case HDSP_systemFrequency48: return 48000;
967 case HDSP_systemFrequency64: return 64000;
968 case HDSP_systemFrequency88_2: return 88200;
969 case HDSP_systemFrequency96: return 96000;
970 default:
971 return 0;
972 }
973}
974
Takashi Iwai55e957d2005-11-17 14:52:13 +0100975static void hdsp_compute_period_size(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
977 hdsp->period_bytes = 1 << ((hdsp_decode_latency(hdsp->control_register) + 8));
978}
979
Takashi Iwai55e957d2005-11-17 14:52:13 +0100980static snd_pcm_uframes_t hdsp_hw_pointer(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981{
982 int position;
983
984 position = hdsp_read(hdsp, HDSP_statusRegister);
985
Takashi Iwaib0b98112005-10-20 18:29:58 +0200986 if (!hdsp->precise_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 return (position & HDSP_BufferID) ? (hdsp->period_bytes / 4) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
989 position &= HDSP_BufferPositionMask;
990 position /= 4;
991 position &= (hdsp->period_bytes/2) - 1;
992 return position;
993}
994
Takashi Iwai55e957d2005-11-17 14:52:13 +0100995static void hdsp_reset_hw_pointer(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996{
997 hdsp_write (hdsp, HDSP_resetPointer, 0);
Remy Brunod7923b22006-10-17 12:41:56 +0200998 if (hdsp->io_type == H9632 && hdsp->firmware_rev >= 152)
999 /* HDSP_resetPointer = HDSP_freqReg, which is strange and
1000 * requires (?) to write again DDS value after a reset pointer
1001 * (at least, it works like this) */
1002 hdsp_write (hdsp, HDSP_freqReg, hdsp->dds_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003}
1004
Takashi Iwai55e957d2005-11-17 14:52:13 +01001005static void hdsp_start_audio(struct hdsp *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006{
1007 s->control_register |= (HDSP_AudioInterruptEnable | HDSP_Start);
1008 hdsp_write(s, HDSP_controlRegister, s->control_register);
1009}
1010
Takashi Iwai55e957d2005-11-17 14:52:13 +01001011static void hdsp_stop_audio(struct hdsp *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012{
1013 s->control_register &= ~(HDSP_Start | HDSP_AudioInterruptEnable);
1014 hdsp_write(s, HDSP_controlRegister, s->control_register);
1015}
1016
Takashi Iwai55e957d2005-11-17 14:52:13 +01001017static void hdsp_silence_playback(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
1019 memset(hdsp->playback_buffer, 0, HDSP_DMA_AREA_BYTES);
1020}
1021
Takashi Iwai55e957d2005-11-17 14:52:13 +01001022static int hdsp_set_interrupt_interval(struct hdsp *s, unsigned int frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023{
1024 int n;
1025
1026 spin_lock_irq(&s->lock);
1027
1028 frames >>= 7;
1029 n = 0;
1030 while (frames) {
1031 n++;
1032 frames >>= 1;
1033 }
1034
1035 s->control_register &= ~HDSP_LatencyMask;
1036 s->control_register |= hdsp_encode_latency(n);
1037
1038 hdsp_write(s, HDSP_controlRegister, s->control_register);
1039
1040 hdsp_compute_period_size(s);
1041
1042 spin_unlock_irq(&s->lock);
1043
1044 return 0;
1045}
1046
Remy Brunod7923b22006-10-17 12:41:56 +02001047static void hdsp_set_dds_value(struct hdsp *hdsp, int rate)
1048{
1049 u64 n;
1050 u32 r;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001051
Remy Brunod7923b22006-10-17 12:41:56 +02001052 if (rate >= 112000)
1053 rate /= 4;
1054 else if (rate >= 56000)
1055 rate /= 2;
1056
Julian Cablee4b60882007-03-19 11:44:40 +01001057 n = DDS_NUMERATOR;
Remy Brunod7923b22006-10-17 12:41:56 +02001058 div64_32(&n, rate, &r);
1059 /* n should be less than 2^32 for being written to FREQ register */
Takashi Iwaida3cec32008-08-08 17:12:14 +02001060 snd_BUG_ON(n >> 32);
Remy Brunod7923b22006-10-17 12:41:56 +02001061 /* HDSP_freqReg and HDSP_resetPointer are the same, so keep the DDS
1062 value to write it after a reset */
1063 hdsp->dds_value = n;
1064 hdsp_write(hdsp, HDSP_freqReg, hdsp->dds_value);
1065}
1066
Takashi Iwai55e957d2005-11-17 14:52:13 +01001067static int hdsp_set_rate(struct hdsp *hdsp, int rate, int called_internally)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
1069 int reject_if_open = 0;
1070 int current_rate;
1071 int rate_bits;
1072
1073 /* ASSUMPTION: hdsp->lock is either held, or
1074 there is no need for it (e.g. during module
1075 initialization).
1076 */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001077
1078 if (!(hdsp->control_register & HDSP_ClockModeMaster)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 if (called_internally) {
1080 /* request from ctl or card initialization */
Takashi Iwaib0b98112005-10-20 18:29:58 +02001081 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 -07001082 return -1;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001083 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 /* hw_param request while in AutoSync mode */
1085 int external_freq = hdsp_external_sample_rate(hdsp);
1086 int spdif_freq = hdsp_spdif_sample_rate(hdsp);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001087
Takashi Iwaib0b98112005-10-20 18:29:58 +02001088 if ((spdif_freq == external_freq*2) && (hdsp_autosync_ref(hdsp) >= HDSP_AUTOSYNC_FROM_ADAT1))
1089 snd_printk(KERN_INFO "Hammerfall-DSP: Detected ADAT in double speed mode\n");
1090 else if (hdsp->io_type == H9632 && (spdif_freq == external_freq*4) && (hdsp_autosync_ref(hdsp) >= HDSP_AUTOSYNC_FROM_ADAT1))
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001091 snd_printk(KERN_INFO "Hammerfall-DSP: Detected ADAT in quad speed mode\n");
Takashi Iwaib0b98112005-10-20 18:29:58 +02001092 else if (rate != external_freq) {
1093 snd_printk(KERN_INFO "Hammerfall-DSP: No AutoSync source for requested rate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 return -1;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001095 }
1096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 }
1098
1099 current_rate = hdsp->system_sample_rate;
1100
1101 /* Changing from a "single speed" to a "double speed" rate is
1102 not allowed if any substreams are open. This is because
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001103 such a change causes a shift in the location of
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 the DMA buffers and a reduction in the number of available
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001105 buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
1107 Note that a similar but essentially insoluble problem
1108 exists for externally-driven rate changes. All we can do
1109 is to flag rate changes in the read/write routines. */
1110
Takashi Iwaib0b98112005-10-20 18:29:58 +02001111 if (rate > 96000 && hdsp->io_type != H9632)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 return -EINVAL;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 switch (rate) {
1115 case 32000:
Takashi Iwaib0b98112005-10-20 18:29:58 +02001116 if (current_rate > 48000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 rate_bits = HDSP_Frequency32KHz;
1119 break;
1120 case 44100:
Takashi Iwaib0b98112005-10-20 18:29:58 +02001121 if (current_rate > 48000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 rate_bits = HDSP_Frequency44_1KHz;
1124 break;
1125 case 48000:
Takashi Iwaib0b98112005-10-20 18:29:58 +02001126 if (current_rate > 48000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 rate_bits = HDSP_Frequency48KHz;
1129 break;
1130 case 64000:
Takashi Iwaib0b98112005-10-20 18:29:58 +02001131 if (current_rate <= 48000 || current_rate > 96000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 rate_bits = HDSP_Frequency64KHz;
1134 break;
1135 case 88200:
Takashi Iwaib0b98112005-10-20 18:29:58 +02001136 if (current_rate <= 48000 || current_rate > 96000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 rate_bits = HDSP_Frequency88_2KHz;
1139 break;
1140 case 96000:
Takashi Iwaib0b98112005-10-20 18:29:58 +02001141 if (current_rate <= 48000 || current_rate > 96000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 rate_bits = HDSP_Frequency96KHz;
1144 break;
1145 case 128000:
Takashi Iwaib0b98112005-10-20 18:29:58 +02001146 if (current_rate < 128000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 rate_bits = HDSP_Frequency128KHz;
1149 break;
1150 case 176400:
Takashi Iwaib0b98112005-10-20 18:29:58 +02001151 if (current_rate < 128000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 rate_bits = HDSP_Frequency176_4KHz;
1154 break;
1155 case 192000:
Takashi Iwaib0b98112005-10-20 18:29:58 +02001156 if (current_rate < 128000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 rate_bits = HDSP_Frequency192KHz;
1159 break;
1160 default:
1161 return -EINVAL;
1162 }
1163
1164 if (reject_if_open && (hdsp->capture_pid >= 0 || hdsp->playback_pid >= 0)) {
1165 snd_printk ("Hammerfall-DSP: cannot change speed mode (capture PID = %d, playback PID = %d)\n",
1166 hdsp->capture_pid,
1167 hdsp->playback_pid);
1168 return -EBUSY;
1169 }
1170
1171 hdsp->control_register &= ~HDSP_FrequencyMask;
1172 hdsp->control_register |= rate_bits;
1173 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1174
Remy Brunod7923b22006-10-17 12:41:56 +02001175 /* For HDSP9632 rev 152, need to set DDS value in FREQ register */
1176 if (hdsp->io_type == H9632 && hdsp->firmware_rev >= 152)
1177 hdsp_set_dds_value(hdsp, rate);
1178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 if (rate >= 128000) {
1180 hdsp->channel_map = channel_map_H9632_qs;
1181 } else if (rate > 48000) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02001182 if (hdsp->io_type == H9632)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 hdsp->channel_map = channel_map_H9632_ds;
Takashi Iwaib0b98112005-10-20 18:29:58 +02001184 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 hdsp->channel_map = channel_map_ds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 } else {
1187 switch (hdsp->io_type) {
1188 case Multiface:
1189 hdsp->channel_map = channel_map_mf_ss;
1190 break;
1191 case Digiface:
1192 case H9652:
1193 hdsp->channel_map = channel_map_df_ss;
1194 break;
1195 case H9632:
1196 hdsp->channel_map = channel_map_H9632_ss;
1197 break;
1198 default:
1199 /* should never happen */
1200 break;
1201 }
1202 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 hdsp->system_sample_rate = rate;
1205
1206 return 0;
1207}
1208
1209/*----------------------------------------------------------------------------
1210 MIDI
1211 ----------------------------------------------------------------------------*/
1212
Takashi Iwai55e957d2005-11-17 14:52:13 +01001213static unsigned char snd_hdsp_midi_read_byte (struct hdsp *hdsp, int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214{
1215 /* the hardware already does the relevant bit-mask with 0xff */
Takashi Iwaib0b98112005-10-20 18:29:58 +02001216 if (id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 return hdsp_read(hdsp, HDSP_midiDataIn1);
Takashi Iwaib0b98112005-10-20 18:29:58 +02001218 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 return hdsp_read(hdsp, HDSP_midiDataIn0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220}
1221
Takashi Iwai55e957d2005-11-17 14:52:13 +01001222static void snd_hdsp_midi_write_byte (struct hdsp *hdsp, int id, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
1224 /* the hardware already does the relevant bit-mask with 0xff */
Takashi Iwaib0b98112005-10-20 18:29:58 +02001225 if (id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 hdsp_write(hdsp, HDSP_midiDataOut1, val);
Takashi Iwaib0b98112005-10-20 18:29:58 +02001227 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 hdsp_write(hdsp, HDSP_midiDataOut0, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229}
1230
Takashi Iwai55e957d2005-11-17 14:52:13 +01001231static int snd_hdsp_midi_input_available (struct hdsp *hdsp, int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232{
Takashi Iwaib0b98112005-10-20 18:29:58 +02001233 if (id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 return (hdsp_read(hdsp, HDSP_midiStatusIn1) & 0xff);
Takashi Iwaib0b98112005-10-20 18:29:58 +02001235 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 return (hdsp_read(hdsp, HDSP_midiStatusIn0) & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237}
1238
Takashi Iwai55e957d2005-11-17 14:52:13 +01001239static int snd_hdsp_midi_output_possible (struct hdsp *hdsp, int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240{
1241 int fifo_bytes_used;
1242
Takashi Iwaib0b98112005-10-20 18:29:58 +02001243 if (id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 fifo_bytes_used = hdsp_read(hdsp, HDSP_midiStatusOut1) & 0xff;
Takashi Iwaib0b98112005-10-20 18:29:58 +02001245 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 fifo_bytes_used = hdsp_read(hdsp, HDSP_midiStatusOut0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Takashi Iwaib0b98112005-10-20 18:29:58 +02001248 if (fifo_bytes_used < 128)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 return 128 - fifo_bytes_used;
Takashi Iwaib0b98112005-10-20 18:29:58 +02001250 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252}
1253
Takashi Iwai55e957d2005-11-17 14:52:13 +01001254static void snd_hdsp_flush_midi_input (struct hdsp *hdsp, int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
Takashi Iwaib0b98112005-10-20 18:29:58 +02001256 while (snd_hdsp_midi_input_available (hdsp, id))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 snd_hdsp_midi_read_byte (hdsp, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258}
1259
Takashi Iwai55e957d2005-11-17 14:52:13 +01001260static int snd_hdsp_midi_output_write (struct hdsp_midi *hmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261{
1262 unsigned long flags;
1263 int n_pending;
1264 int to_write;
1265 int i;
1266 unsigned char buf[128];
1267
1268 /* Output is not interrupt driven */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 spin_lock_irqsave (&hmidi->lock, flags);
1271 if (hmidi->output) {
1272 if (!snd_rawmidi_transmit_empty (hmidi->output)) {
1273 if ((n_pending = snd_hdsp_midi_output_possible (hmidi->hdsp, hmidi->id)) > 0) {
1274 if (n_pending > (int)sizeof (buf))
1275 n_pending = sizeof (buf);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 if ((to_write = snd_rawmidi_transmit (hmidi->output, buf, n_pending)) > 0) {
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001278 for (i = 0; i < to_write; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 snd_hdsp_midi_write_byte (hmidi->hdsp, hmidi->id, buf[i]);
1280 }
1281 }
1282 }
1283 }
1284 spin_unlock_irqrestore (&hmidi->lock, flags);
1285 return 0;
1286}
1287
Takashi Iwai55e957d2005-11-17 14:52:13 +01001288static int snd_hdsp_midi_input_read (struct hdsp_midi *hmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289{
1290 unsigned char buf[128]; /* this buffer is designed to match the MIDI input FIFO size */
1291 unsigned long flags;
1292 int n_pending;
1293 int i;
1294
1295 spin_lock_irqsave (&hmidi->lock, flags);
1296 if ((n_pending = snd_hdsp_midi_input_available (hmidi->hdsp, hmidi->id)) > 0) {
1297 if (hmidi->input) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02001298 if (n_pending > (int)sizeof (buf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 n_pending = sizeof (buf);
Takashi Iwaib0b98112005-10-20 18:29:58 +02001300 for (i = 0; i < n_pending; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 buf[i] = snd_hdsp_midi_read_byte (hmidi->hdsp, hmidi->id);
Takashi Iwaib0b98112005-10-20 18:29:58 +02001302 if (n_pending)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 snd_rawmidi_receive (hmidi->input, buf, n_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 } else {
1305 /* flush the MIDI input FIFO */
Takashi Iwaib0b98112005-10-20 18:29:58 +02001306 while (--n_pending)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 snd_hdsp_midi_read_byte (hmidi->hdsp, hmidi->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 }
1309 }
1310 hmidi->pending = 0;
Takashi Iwaib0b98112005-10-20 18:29:58 +02001311 if (hmidi->id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 hmidi->hdsp->control_register |= HDSP_Midi1InterruptEnable;
Takashi Iwaib0b98112005-10-20 18:29:58 +02001313 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 hmidi->hdsp->control_register |= HDSP_Midi0InterruptEnable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 hdsp_write(hmidi->hdsp, HDSP_controlRegister, hmidi->hdsp->control_register);
1316 spin_unlock_irqrestore (&hmidi->lock, flags);
1317 return snd_hdsp_midi_output_write (hmidi);
1318}
1319
Takashi Iwai55e957d2005-11-17 14:52:13 +01001320static void snd_hdsp_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001322 struct hdsp *hdsp;
1323 struct hdsp_midi *hmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 unsigned long flags;
1325 u32 ie;
1326
Takashi Iwai55e957d2005-11-17 14:52:13 +01001327 hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 hdsp = hmidi->hdsp;
1329 ie = hmidi->id ? HDSP_Midi1InterruptEnable : HDSP_Midi0InterruptEnable;
1330 spin_lock_irqsave (&hdsp->lock, flags);
1331 if (up) {
1332 if (!(hdsp->control_register & ie)) {
1333 snd_hdsp_flush_midi_input (hdsp, hmidi->id);
1334 hdsp->control_register |= ie;
1335 }
1336 } else {
1337 hdsp->control_register &= ~ie;
1338 tasklet_kill(&hdsp->midi_tasklet);
1339 }
1340
1341 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1342 spin_unlock_irqrestore (&hdsp->lock, flags);
1343}
1344
1345static void snd_hdsp_midi_output_timer(unsigned long data)
1346{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001347 struct hdsp_midi *hmidi = (struct hdsp_midi *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 unsigned long flags;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001349
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 snd_hdsp_midi_output_write(hmidi);
1351 spin_lock_irqsave (&hmidi->lock, flags);
1352
1353 /* this does not bump hmidi->istimer, because the
1354 kernel automatically removed the timer when it
1355 expired, and we are now adding it back, thus
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001356 leaving istimer wherever it was set before.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 */
1358
1359 if (hmidi->istimer) {
1360 hmidi->timer.expires = 1 + jiffies;
1361 add_timer(&hmidi->timer);
1362 }
1363
1364 spin_unlock_irqrestore (&hmidi->lock, flags);
1365}
1366
Takashi Iwai55e957d2005-11-17 14:52:13 +01001367static void snd_hdsp_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001369 struct hdsp_midi *hmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 unsigned long flags;
1371
Takashi Iwai55e957d2005-11-17 14:52:13 +01001372 hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 spin_lock_irqsave (&hmidi->lock, flags);
1374 if (up) {
1375 if (!hmidi->istimer) {
1376 init_timer(&hmidi->timer);
1377 hmidi->timer.function = snd_hdsp_midi_output_timer;
1378 hmidi->timer.data = (unsigned long) hmidi;
1379 hmidi->timer.expires = 1 + jiffies;
1380 add_timer(&hmidi->timer);
1381 hmidi->istimer++;
1382 }
1383 } else {
Takashi Iwaib0b98112005-10-20 18:29:58 +02001384 if (hmidi->istimer && --hmidi->istimer <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 del_timer (&hmidi->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 }
1387 spin_unlock_irqrestore (&hmidi->lock, flags);
1388 if (up)
1389 snd_hdsp_midi_output_write(hmidi);
1390}
1391
Takashi Iwai55e957d2005-11-17 14:52:13 +01001392static int snd_hdsp_midi_input_open(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001394 struct hdsp_midi *hmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
Takashi Iwai55e957d2005-11-17 14:52:13 +01001396 hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 spin_lock_irq (&hmidi->lock);
1398 snd_hdsp_flush_midi_input (hmidi->hdsp, hmidi->id);
1399 hmidi->input = substream;
1400 spin_unlock_irq (&hmidi->lock);
1401
1402 return 0;
1403}
1404
Takashi Iwai55e957d2005-11-17 14:52:13 +01001405static int snd_hdsp_midi_output_open(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001407 struct hdsp_midi *hmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Takashi Iwai55e957d2005-11-17 14:52:13 +01001409 hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 spin_lock_irq (&hmidi->lock);
1411 hmidi->output = substream;
1412 spin_unlock_irq (&hmidi->lock);
1413
1414 return 0;
1415}
1416
Takashi Iwai55e957d2005-11-17 14:52:13 +01001417static int snd_hdsp_midi_input_close(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001419 struct hdsp_midi *hmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
1421 snd_hdsp_midi_input_trigger (substream, 0);
1422
Takashi Iwai55e957d2005-11-17 14:52:13 +01001423 hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 spin_lock_irq (&hmidi->lock);
1425 hmidi->input = NULL;
1426 spin_unlock_irq (&hmidi->lock);
1427
1428 return 0;
1429}
1430
Takashi Iwai55e957d2005-11-17 14:52:13 +01001431static int snd_hdsp_midi_output_close(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001433 struct hdsp_midi *hmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
1435 snd_hdsp_midi_output_trigger (substream, 0);
1436
Takashi Iwai55e957d2005-11-17 14:52:13 +01001437 hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 spin_lock_irq (&hmidi->lock);
1439 hmidi->output = NULL;
1440 spin_unlock_irq (&hmidi->lock);
1441
1442 return 0;
1443}
1444
Takashi Iwai55e957d2005-11-17 14:52:13 +01001445static struct snd_rawmidi_ops snd_hdsp_midi_output =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446{
1447 .open = snd_hdsp_midi_output_open,
1448 .close = snd_hdsp_midi_output_close,
1449 .trigger = snd_hdsp_midi_output_trigger,
1450};
1451
Takashi Iwai55e957d2005-11-17 14:52:13 +01001452static struct snd_rawmidi_ops snd_hdsp_midi_input =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453{
1454 .open = snd_hdsp_midi_input_open,
1455 .close = snd_hdsp_midi_input_close,
1456 .trigger = snd_hdsp_midi_input_trigger,
1457};
1458
Takashi Iwaif40b6892006-07-05 16:51:05 +02001459static int snd_hdsp_create_midi (struct snd_card *card, struct hdsp *hdsp, int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460{
1461 char buf[32];
1462
1463 hdsp->midi[id].id = id;
1464 hdsp->midi[id].rmidi = NULL;
1465 hdsp->midi[id].input = NULL;
1466 hdsp->midi[id].output = NULL;
1467 hdsp->midi[id].hdsp = hdsp;
1468 hdsp->midi[id].istimer = 0;
1469 hdsp->midi[id].pending = 0;
1470 spin_lock_init (&hdsp->midi[id].lock);
1471
1472 sprintf (buf, "%s MIDI %d", card->shortname, id+1);
Takashi Iwaib0b98112005-10-20 18:29:58 +02001473 if (snd_rawmidi_new (card, buf, id, 1, 1, &hdsp->midi[id].rmidi) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Jaroslav Kysela972d4c52008-11-12 16:37:48 +01001476 sprintf(hdsp->midi[id].rmidi->name, "HDSP MIDI %d", id+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 hdsp->midi[id].rmidi->private_data = &hdsp->midi[id];
1478
1479 snd_rawmidi_set_ops (hdsp->midi[id].rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_hdsp_midi_output);
1480 snd_rawmidi_set_ops (hdsp->midi[id].rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_hdsp_midi_input);
1481
1482 hdsp->midi[id].rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT |
1483 SNDRV_RAWMIDI_INFO_INPUT |
1484 SNDRV_RAWMIDI_INFO_DUPLEX;
1485
1486 return 0;
1487}
1488
1489/*-----------------------------------------------------------------------------
1490 Control Interface
1491 ----------------------------------------------------------------------------*/
1492
Takashi Iwai55e957d2005-11-17 14:52:13 +01001493static u32 snd_hdsp_convert_from_aes(struct snd_aes_iec958 *aes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494{
1495 u32 val = 0;
1496 val |= (aes->status[0] & IEC958_AES0_PROFESSIONAL) ? HDSP_SPDIFProfessional : 0;
1497 val |= (aes->status[0] & IEC958_AES0_NONAUDIO) ? HDSP_SPDIFNonAudio : 0;
1498 if (val & HDSP_SPDIFProfessional)
1499 val |= (aes->status[0] & IEC958_AES0_PRO_EMPHASIS_5015) ? HDSP_SPDIFEmphasis : 0;
1500 else
1501 val |= (aes->status[0] & IEC958_AES0_CON_EMPHASIS_5015) ? HDSP_SPDIFEmphasis : 0;
1502 return val;
1503}
1504
Takashi Iwai55e957d2005-11-17 14:52:13 +01001505static void snd_hdsp_convert_to_aes(struct snd_aes_iec958 *aes, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506{
1507 aes->status[0] = ((val & HDSP_SPDIFProfessional) ? IEC958_AES0_PROFESSIONAL : 0) |
1508 ((val & HDSP_SPDIFNonAudio) ? IEC958_AES0_NONAUDIO : 0);
1509 if (val & HDSP_SPDIFProfessional)
1510 aes->status[0] |= (val & HDSP_SPDIFEmphasis) ? IEC958_AES0_PRO_EMPHASIS_5015 : 0;
1511 else
1512 aes->status[0] |= (val & HDSP_SPDIFEmphasis) ? IEC958_AES0_CON_EMPHASIS_5015 : 0;
1513}
1514
Takashi Iwai55e957d2005-11-17 14:52:13 +01001515static int snd_hdsp_control_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
1517 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1518 uinfo->count = 1;
1519 return 0;
1520}
1521
Takashi Iwai55e957d2005-11-17 14:52:13 +01001522static int snd_hdsp_control_spdif_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001524 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001525
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 snd_hdsp_convert_to_aes(&ucontrol->value.iec958, hdsp->creg_spdif);
1527 return 0;
1528}
1529
Takashi Iwai55e957d2005-11-17 14:52:13 +01001530static int snd_hdsp_control_spdif_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001532 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 int change;
1534 u32 val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001535
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 val = snd_hdsp_convert_from_aes(&ucontrol->value.iec958);
1537 spin_lock_irq(&hdsp->lock);
1538 change = val != hdsp->creg_spdif;
1539 hdsp->creg_spdif = val;
1540 spin_unlock_irq(&hdsp->lock);
1541 return change;
1542}
1543
Takashi Iwai55e957d2005-11-17 14:52:13 +01001544static int snd_hdsp_control_spdif_stream_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545{
1546 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1547 uinfo->count = 1;
1548 return 0;
1549}
1550
Takashi Iwai55e957d2005-11-17 14:52:13 +01001551static int snd_hdsp_control_spdif_stream_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001553 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001554
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 snd_hdsp_convert_to_aes(&ucontrol->value.iec958, hdsp->creg_spdif_stream);
1556 return 0;
1557}
1558
Takashi Iwai55e957d2005-11-17 14:52:13 +01001559static int snd_hdsp_control_spdif_stream_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001561 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 int change;
1563 u32 val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001564
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 val = snd_hdsp_convert_from_aes(&ucontrol->value.iec958);
1566 spin_lock_irq(&hdsp->lock);
1567 change = val != hdsp->creg_spdif_stream;
1568 hdsp->creg_spdif_stream = val;
1569 hdsp->control_register &= ~(HDSP_SPDIFProfessional | HDSP_SPDIFNonAudio | HDSP_SPDIFEmphasis);
1570 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register |= val);
1571 spin_unlock_irq(&hdsp->lock);
1572 return change;
1573}
1574
Takashi Iwai55e957d2005-11-17 14:52:13 +01001575static int snd_hdsp_control_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576{
1577 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1578 uinfo->count = 1;
1579 return 0;
1580}
1581
Takashi Iwai55e957d2005-11-17 14:52:13 +01001582static int snd_hdsp_control_spdif_mask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583{
1584 ucontrol->value.iec958.status[0] = kcontrol->private_value;
1585 return 0;
1586}
1587
1588#define HDSP_SPDIF_IN(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001589{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 .name = xname, \
1591 .index = xindex, \
1592 .info = snd_hdsp_info_spdif_in, \
1593 .get = snd_hdsp_get_spdif_in, \
1594 .put = snd_hdsp_put_spdif_in }
1595
Takashi Iwai55e957d2005-11-17 14:52:13 +01001596static unsigned int hdsp_spdif_in(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597{
1598 return hdsp_decode_spdif_in(hdsp->control_register & HDSP_SPDIFInputMask);
1599}
1600
Takashi Iwai55e957d2005-11-17 14:52:13 +01001601static int hdsp_set_spdif_input(struct hdsp *hdsp, int in)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602{
1603 hdsp->control_register &= ~HDSP_SPDIFInputMask;
1604 hdsp->control_register |= hdsp_encode_spdif_in(in);
1605 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1606 return 0;
1607}
1608
Takashi Iwai55e957d2005-11-17 14:52:13 +01001609static int snd_hdsp_info_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610{
1611 static char *texts[4] = {"Optical", "Coaxial", "Internal", "AES"};
Takashi Iwai55e957d2005-11-17 14:52:13 +01001612 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
1614 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1615 uinfo->count = 1;
1616 uinfo->value.enumerated.items = ((hdsp->io_type == H9632) ? 4 : 3);
1617 if (uinfo->value.enumerated.item > ((hdsp->io_type == H9632) ? 3 : 2))
1618 uinfo->value.enumerated.item = ((hdsp->io_type == H9632) ? 3 : 2);
1619 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1620 return 0;
1621}
1622
Takashi Iwai55e957d2005-11-17 14:52:13 +01001623static int snd_hdsp_get_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001625 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001626
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 ucontrol->value.enumerated.item[0] = hdsp_spdif_in(hdsp);
1628 return 0;
1629}
1630
Takashi Iwai55e957d2005-11-17 14:52:13 +01001631static int snd_hdsp_put_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001633 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 int change;
1635 unsigned int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001636
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 if (!snd_hdsp_use_is_exclusive(hdsp))
1638 return -EBUSY;
1639 val = ucontrol->value.enumerated.item[0] % ((hdsp->io_type == H9632) ? 4 : 3);
1640 spin_lock_irq(&hdsp->lock);
1641 change = val != hdsp_spdif_in(hdsp);
1642 if (change)
1643 hdsp_set_spdif_input(hdsp, val);
1644 spin_unlock_irq(&hdsp->lock);
1645 return change;
1646}
1647
1648#define HDSP_SPDIF_OUT(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001649{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 .info = snd_hdsp_info_spdif_bits, \
1651 .get = snd_hdsp_get_spdif_out, .put = snd_hdsp_put_spdif_out }
1652
Takashi Iwai55e957d2005-11-17 14:52:13 +01001653static int hdsp_spdif_out(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654{
1655 return (hdsp->control_register & HDSP_SPDIFOpticalOut) ? 1 : 0;
1656}
1657
Takashi Iwai55e957d2005-11-17 14:52:13 +01001658static int hdsp_set_spdif_output(struct hdsp *hdsp, int out)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659{
Takashi Iwaib0b98112005-10-20 18:29:58 +02001660 if (out)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 hdsp->control_register |= HDSP_SPDIFOpticalOut;
Takashi Iwaib0b98112005-10-20 18:29:58 +02001662 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 hdsp->control_register &= ~HDSP_SPDIFOpticalOut;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1665 return 0;
1666}
1667
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001668#define snd_hdsp_info_spdif_bits snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
Takashi Iwai55e957d2005-11-17 14:52:13 +01001670static int snd_hdsp_get_spdif_out(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001672 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001673
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 ucontrol->value.integer.value[0] = hdsp_spdif_out(hdsp);
1675 return 0;
1676}
1677
Takashi Iwai55e957d2005-11-17 14:52:13 +01001678static int snd_hdsp_put_spdif_out(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001680 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 int change;
1682 unsigned int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001683
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 if (!snd_hdsp_use_is_exclusive(hdsp))
1685 return -EBUSY;
1686 val = ucontrol->value.integer.value[0] & 1;
1687 spin_lock_irq(&hdsp->lock);
1688 change = (int)val != hdsp_spdif_out(hdsp);
1689 hdsp_set_spdif_output(hdsp, val);
1690 spin_unlock_irq(&hdsp->lock);
1691 return change;
1692}
1693
1694#define HDSP_SPDIF_PROFESSIONAL(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001695{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 .info = snd_hdsp_info_spdif_bits, \
1697 .get = snd_hdsp_get_spdif_professional, .put = snd_hdsp_put_spdif_professional }
1698
Takashi Iwai55e957d2005-11-17 14:52:13 +01001699static int hdsp_spdif_professional(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700{
1701 return (hdsp->control_register & HDSP_SPDIFProfessional) ? 1 : 0;
1702}
1703
Takashi Iwai55e957d2005-11-17 14:52:13 +01001704static int hdsp_set_spdif_professional(struct hdsp *hdsp, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705{
Takashi Iwaib0b98112005-10-20 18:29:58 +02001706 if (val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 hdsp->control_register |= HDSP_SPDIFProfessional;
Takashi Iwaib0b98112005-10-20 18:29:58 +02001708 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 hdsp->control_register &= ~HDSP_SPDIFProfessional;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1711 return 0;
1712}
1713
Takashi Iwai55e957d2005-11-17 14:52:13 +01001714static int snd_hdsp_get_spdif_professional(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001716 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001717
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 ucontrol->value.integer.value[0] = hdsp_spdif_professional(hdsp);
1719 return 0;
1720}
1721
Takashi Iwai55e957d2005-11-17 14:52:13 +01001722static int snd_hdsp_put_spdif_professional(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001724 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 int change;
1726 unsigned int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001727
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 if (!snd_hdsp_use_is_exclusive(hdsp))
1729 return -EBUSY;
1730 val = ucontrol->value.integer.value[0] & 1;
1731 spin_lock_irq(&hdsp->lock);
1732 change = (int)val != hdsp_spdif_professional(hdsp);
1733 hdsp_set_spdif_professional(hdsp, val);
1734 spin_unlock_irq(&hdsp->lock);
1735 return change;
1736}
1737
1738#define HDSP_SPDIF_EMPHASIS(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001739{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 .info = snd_hdsp_info_spdif_bits, \
1741 .get = snd_hdsp_get_spdif_emphasis, .put = snd_hdsp_put_spdif_emphasis }
1742
Takashi Iwai55e957d2005-11-17 14:52:13 +01001743static int hdsp_spdif_emphasis(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744{
1745 return (hdsp->control_register & HDSP_SPDIFEmphasis) ? 1 : 0;
1746}
1747
Takashi Iwai55e957d2005-11-17 14:52:13 +01001748static int hdsp_set_spdif_emphasis(struct hdsp *hdsp, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749{
Takashi Iwaib0b98112005-10-20 18:29:58 +02001750 if (val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 hdsp->control_register |= HDSP_SPDIFEmphasis;
Takashi Iwaib0b98112005-10-20 18:29:58 +02001752 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 hdsp->control_register &= ~HDSP_SPDIFEmphasis;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1755 return 0;
1756}
1757
Takashi Iwai55e957d2005-11-17 14:52:13 +01001758static int snd_hdsp_get_spdif_emphasis(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001760 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001761
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 ucontrol->value.integer.value[0] = hdsp_spdif_emphasis(hdsp);
1763 return 0;
1764}
1765
Takashi Iwai55e957d2005-11-17 14:52:13 +01001766static int snd_hdsp_put_spdif_emphasis(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001768 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 int change;
1770 unsigned int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001771
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 if (!snd_hdsp_use_is_exclusive(hdsp))
1773 return -EBUSY;
1774 val = ucontrol->value.integer.value[0] & 1;
1775 spin_lock_irq(&hdsp->lock);
1776 change = (int)val != hdsp_spdif_emphasis(hdsp);
1777 hdsp_set_spdif_emphasis(hdsp, val);
1778 spin_unlock_irq(&hdsp->lock);
1779 return change;
1780}
1781
1782#define HDSP_SPDIF_NON_AUDIO(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001783{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 .info = snd_hdsp_info_spdif_bits, \
1785 .get = snd_hdsp_get_spdif_nonaudio, .put = snd_hdsp_put_spdif_nonaudio }
1786
Takashi Iwai55e957d2005-11-17 14:52:13 +01001787static int hdsp_spdif_nonaudio(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788{
1789 return (hdsp->control_register & HDSP_SPDIFNonAudio) ? 1 : 0;
1790}
1791
Takashi Iwai55e957d2005-11-17 14:52:13 +01001792static int hdsp_set_spdif_nonaudio(struct hdsp *hdsp, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793{
Takashi Iwaib0b98112005-10-20 18:29:58 +02001794 if (val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 hdsp->control_register |= HDSP_SPDIFNonAudio;
Takashi Iwaib0b98112005-10-20 18:29:58 +02001796 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 hdsp->control_register &= ~HDSP_SPDIFNonAudio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1799 return 0;
1800}
1801
Takashi Iwai55e957d2005-11-17 14:52:13 +01001802static int snd_hdsp_get_spdif_nonaudio(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001804 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001805
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 ucontrol->value.integer.value[0] = hdsp_spdif_nonaudio(hdsp);
1807 return 0;
1808}
1809
Takashi Iwai55e957d2005-11-17 14:52:13 +01001810static int snd_hdsp_put_spdif_nonaudio(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001812 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 int change;
1814 unsigned int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001815
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 if (!snd_hdsp_use_is_exclusive(hdsp))
1817 return -EBUSY;
1818 val = ucontrol->value.integer.value[0] & 1;
1819 spin_lock_irq(&hdsp->lock);
1820 change = (int)val != hdsp_spdif_nonaudio(hdsp);
1821 hdsp_set_spdif_nonaudio(hdsp, val);
1822 spin_unlock_irq(&hdsp->lock);
1823 return change;
1824}
1825
1826#define HDSP_SPDIF_SAMPLE_RATE(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001827{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 .name = xname, \
1829 .index = xindex, \
1830 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
1831 .info = snd_hdsp_info_spdif_sample_rate, \
1832 .get = snd_hdsp_get_spdif_sample_rate \
1833}
1834
Takashi Iwai55e957d2005-11-17 14:52:13 +01001835static int snd_hdsp_info_spdif_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836{
1837 static char *texts[] = {"32000", "44100", "48000", "64000", "88200", "96000", "None", "128000", "176400", "192000"};
Takashi Iwai55e957d2005-11-17 14:52:13 +01001838 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
1840 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1841 uinfo->count = 1;
1842 uinfo->value.enumerated.items = (hdsp->io_type == H9632) ? 10 : 7;
1843 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1844 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
1845 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1846 return 0;
1847}
1848
Takashi Iwai55e957d2005-11-17 14:52:13 +01001849static int snd_hdsp_get_spdif_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001851 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001852
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 switch (hdsp_spdif_sample_rate(hdsp)) {
1854 case 32000:
1855 ucontrol->value.enumerated.item[0] = 0;
1856 break;
1857 case 44100:
1858 ucontrol->value.enumerated.item[0] = 1;
1859 break;
1860 case 48000:
1861 ucontrol->value.enumerated.item[0] = 2;
1862 break;
1863 case 64000:
1864 ucontrol->value.enumerated.item[0] = 3;
1865 break;
1866 case 88200:
1867 ucontrol->value.enumerated.item[0] = 4;
1868 break;
1869 case 96000:
1870 ucontrol->value.enumerated.item[0] = 5;
1871 break;
1872 case 128000:
1873 ucontrol->value.enumerated.item[0] = 7;
1874 break;
1875 case 176400:
1876 ucontrol->value.enumerated.item[0] = 8;
1877 break;
1878 case 192000:
1879 ucontrol->value.enumerated.item[0] = 9;
1880 break;
1881 default:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001882 ucontrol->value.enumerated.item[0] = 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 }
1884 return 0;
1885}
1886
1887#define HDSP_SYSTEM_SAMPLE_RATE(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001888{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 .name = xname, \
1890 .index = xindex, \
1891 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
1892 .info = snd_hdsp_info_system_sample_rate, \
1893 .get = snd_hdsp_get_system_sample_rate \
1894}
1895
Takashi Iwai55e957d2005-11-17 14:52:13 +01001896static int snd_hdsp_info_system_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897{
1898 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1899 uinfo->count = 1;
1900 return 0;
1901}
1902
Takashi Iwai55e957d2005-11-17 14:52:13 +01001903static int snd_hdsp_get_system_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001905 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001906
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 ucontrol->value.enumerated.item[0] = hdsp->system_sample_rate;
1908 return 0;
1909}
1910
1911#define HDSP_AUTOSYNC_SAMPLE_RATE(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001912{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 .name = xname, \
1914 .index = xindex, \
1915 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
1916 .info = snd_hdsp_info_autosync_sample_rate, \
1917 .get = snd_hdsp_get_autosync_sample_rate \
1918}
1919
Takashi Iwai55e957d2005-11-17 14:52:13 +01001920static int snd_hdsp_info_autosync_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001922 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001923 static char *texts[] = {"32000", "44100", "48000", "64000", "88200", "96000", "None", "128000", "176400", "192000"};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1925 uinfo->count = 1;
1926 uinfo->value.enumerated.items = (hdsp->io_type == H9632) ? 10 : 7 ;
1927 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1928 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
1929 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1930 return 0;
1931}
1932
Takashi Iwai55e957d2005-11-17 14:52:13 +01001933static int snd_hdsp_get_autosync_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001935 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001936
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 switch (hdsp_external_sample_rate(hdsp)) {
1938 case 32000:
1939 ucontrol->value.enumerated.item[0] = 0;
1940 break;
1941 case 44100:
1942 ucontrol->value.enumerated.item[0] = 1;
1943 break;
1944 case 48000:
1945 ucontrol->value.enumerated.item[0] = 2;
1946 break;
1947 case 64000:
1948 ucontrol->value.enumerated.item[0] = 3;
1949 break;
1950 case 88200:
1951 ucontrol->value.enumerated.item[0] = 4;
1952 break;
1953 case 96000:
1954 ucontrol->value.enumerated.item[0] = 5;
1955 break;
1956 case 128000:
1957 ucontrol->value.enumerated.item[0] = 7;
1958 break;
1959 case 176400:
1960 ucontrol->value.enumerated.item[0] = 8;
1961 break;
1962 case 192000:
1963 ucontrol->value.enumerated.item[0] = 9;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001964 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 default:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001966 ucontrol->value.enumerated.item[0] = 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 }
1968 return 0;
1969}
1970
1971#define HDSP_SYSTEM_CLOCK_MODE(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001972{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 .name = xname, \
1974 .index = xindex, \
1975 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
1976 .info = snd_hdsp_info_system_clock_mode, \
1977 .get = snd_hdsp_get_system_clock_mode \
1978}
1979
Takashi Iwai55e957d2005-11-17 14:52:13 +01001980static int hdsp_system_clock_mode(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981{
Takashi Iwaib0b98112005-10-20 18:29:58 +02001982 if (hdsp->control_register & HDSP_ClockModeMaster)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 return 0;
Takashi Iwaib0b98112005-10-20 18:29:58 +02001984 else if (hdsp_external_sample_rate(hdsp) != hdsp->system_sample_rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 return 1;
1987}
1988
Takashi Iwai55e957d2005-11-17 14:52:13 +01001989static int snd_hdsp_info_system_clock_mode(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990{
1991 static char *texts[] = {"Master", "Slave" };
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001992
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1994 uinfo->count = 1;
1995 uinfo->value.enumerated.items = 2;
1996 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1997 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
1998 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1999 return 0;
2000}
2001
Takashi Iwai55e957d2005-11-17 14:52:13 +01002002static int snd_hdsp_get_system_clock_mode(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002004 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002005
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 ucontrol->value.enumerated.item[0] = hdsp_system_clock_mode(hdsp);
2007 return 0;
2008}
2009
2010#define HDSP_CLOCK_SOURCE(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002011{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 .name = xname, \
2013 .index = xindex, \
2014 .info = snd_hdsp_info_clock_source, \
2015 .get = snd_hdsp_get_clock_source, \
2016 .put = snd_hdsp_put_clock_source \
2017}
2018
Takashi Iwai55e957d2005-11-17 14:52:13 +01002019static int hdsp_clock_source(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020{
2021 if (hdsp->control_register & HDSP_ClockModeMaster) {
2022 switch (hdsp->system_sample_rate) {
2023 case 32000:
2024 return 1;
2025 case 44100:
2026 return 2;
2027 case 48000:
2028 return 3;
2029 case 64000:
2030 return 4;
2031 case 88200:
2032 return 5;
2033 case 96000:
2034 return 6;
2035 case 128000:
2036 return 7;
2037 case 176400:
2038 return 8;
2039 case 192000:
2040 return 9;
2041 default:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002042 return 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 }
2044 } else {
2045 return 0;
2046 }
2047}
2048
Takashi Iwai55e957d2005-11-17 14:52:13 +01002049static int hdsp_set_clock_source(struct hdsp *hdsp, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050{
2051 int rate;
2052 switch (mode) {
2053 case HDSP_CLOCK_SOURCE_AUTOSYNC:
2054 if (hdsp_external_sample_rate(hdsp) != 0) {
2055 if (!hdsp_set_rate(hdsp, hdsp_external_sample_rate(hdsp), 1)) {
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002056 hdsp->control_register &= ~HDSP_ClockModeMaster;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2058 return 0;
2059 }
2060 }
2061 return -1;
2062 case HDSP_CLOCK_SOURCE_INTERNAL_32KHZ:
2063 rate = 32000;
2064 break;
2065 case HDSP_CLOCK_SOURCE_INTERNAL_44_1KHZ:
2066 rate = 44100;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002067 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 case HDSP_CLOCK_SOURCE_INTERNAL_48KHZ:
2069 rate = 48000;
2070 break;
2071 case HDSP_CLOCK_SOURCE_INTERNAL_64KHZ:
2072 rate = 64000;
2073 break;
2074 case HDSP_CLOCK_SOURCE_INTERNAL_88_2KHZ:
2075 rate = 88200;
2076 break;
2077 case HDSP_CLOCK_SOURCE_INTERNAL_96KHZ:
2078 rate = 96000;
2079 break;
2080 case HDSP_CLOCK_SOURCE_INTERNAL_128KHZ:
2081 rate = 128000;
2082 break;
2083 case HDSP_CLOCK_SOURCE_INTERNAL_176_4KHZ:
2084 rate = 176400;
2085 break;
2086 case HDSP_CLOCK_SOURCE_INTERNAL_192KHZ:
2087 rate = 192000;
2088 break;
2089 default:
2090 rate = 48000;
2091 }
2092 hdsp->control_register |= HDSP_ClockModeMaster;
2093 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2094 hdsp_set_rate(hdsp, rate, 1);
2095 return 0;
2096}
2097
Takashi Iwai55e957d2005-11-17 14:52:13 +01002098static int snd_hdsp_info_clock_source(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099{
2100 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 +01002101 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002102
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2104 uinfo->count = 1;
2105 if (hdsp->io_type == H9632)
2106 uinfo->value.enumerated.items = 10;
2107 else
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002108 uinfo->value.enumerated.items = 7;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2110 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2111 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2112 return 0;
2113}
2114
Takashi Iwai55e957d2005-11-17 14:52:13 +01002115static int snd_hdsp_get_clock_source(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002117 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002118
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 ucontrol->value.enumerated.item[0] = hdsp_clock_source(hdsp);
2120 return 0;
2121}
2122
Takashi Iwai55e957d2005-11-17 14:52:13 +01002123static int snd_hdsp_put_clock_source(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002125 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 int change;
2127 int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002128
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 if (!snd_hdsp_use_is_exclusive(hdsp))
2130 return -EBUSY;
2131 val = ucontrol->value.enumerated.item[0];
2132 if (val < 0) val = 0;
2133 if (hdsp->io_type == H9632) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02002134 if (val > 9)
2135 val = 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 } else {
Takashi Iwaib0b98112005-10-20 18:29:58 +02002137 if (val > 6)
2138 val = 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 }
2140 spin_lock_irq(&hdsp->lock);
Takashi Iwaib0b98112005-10-20 18:29:58 +02002141 if (val != hdsp_clock_source(hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 change = (hdsp_set_clock_source(hdsp, val) == 0) ? 1 : 0;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002143 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 spin_unlock_irq(&hdsp->lock);
2146 return change;
2147}
2148
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002149#define snd_hdsp_info_clock_source_lock snd_ctl_boolean_mono_info
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02002150
Takashi Iwai55e957d2005-11-17 14:52:13 +01002151static int snd_hdsp_get_clock_source_lock(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02002152{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002153 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002154
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02002155 ucontrol->value.integer.value[0] = hdsp->clock_source_locked;
2156 return 0;
2157}
2158
Takashi Iwai55e957d2005-11-17 14:52:13 +01002159static int snd_hdsp_put_clock_source_lock(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02002160{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002161 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02002162 int change;
2163
2164 change = (int)ucontrol->value.integer.value[0] != hdsp->clock_source_locked;
2165 if (change)
Takashi Iwai4e98d6a2007-11-15 15:58:13 +01002166 hdsp->clock_source_locked = !!ucontrol->value.integer.value[0];
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02002167 return change;
2168}
2169
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170#define HDSP_DA_GAIN(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002171{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 .name = xname, \
2173 .index = xindex, \
2174 .info = snd_hdsp_info_da_gain, \
2175 .get = snd_hdsp_get_da_gain, \
2176 .put = snd_hdsp_put_da_gain \
2177}
2178
Takashi Iwai55e957d2005-11-17 14:52:13 +01002179static int hdsp_da_gain(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180{
2181 switch (hdsp->control_register & HDSP_DAGainMask) {
2182 case HDSP_DAGainHighGain:
2183 return 0;
2184 case HDSP_DAGainPlus4dBu:
2185 return 1;
2186 case HDSP_DAGainMinus10dBV:
2187 return 2;
2188 default:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002189 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 }
2191}
2192
Takashi Iwai55e957d2005-11-17 14:52:13 +01002193static int hdsp_set_da_gain(struct hdsp *hdsp, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194{
2195 hdsp->control_register &= ~HDSP_DAGainMask;
2196 switch (mode) {
2197 case 0:
2198 hdsp->control_register |= HDSP_DAGainHighGain;
2199 break;
2200 case 1:
2201 hdsp->control_register |= HDSP_DAGainPlus4dBu;
2202 break;
2203 case 2:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002204 hdsp->control_register |= HDSP_DAGainMinus10dBV;
2205 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 default:
2207 return -1;
2208
2209 }
2210 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2211 return 0;
2212}
2213
Takashi Iwai55e957d2005-11-17 14:52:13 +01002214static int snd_hdsp_info_da_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215{
2216 static char *texts[] = {"Hi Gain", "+4 dBu", "-10 dbV"};
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002217
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2219 uinfo->count = 1;
2220 uinfo->value.enumerated.items = 3;
2221 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2222 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2223 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2224 return 0;
2225}
2226
Takashi Iwai55e957d2005-11-17 14:52:13 +01002227static int snd_hdsp_get_da_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002229 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002230
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 ucontrol->value.enumerated.item[0] = hdsp_da_gain(hdsp);
2232 return 0;
2233}
2234
Takashi Iwai55e957d2005-11-17 14:52:13 +01002235static int snd_hdsp_put_da_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002237 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 int change;
2239 int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002240
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 if (!snd_hdsp_use_is_exclusive(hdsp))
2242 return -EBUSY;
2243 val = ucontrol->value.enumerated.item[0];
2244 if (val < 0) val = 0;
2245 if (val > 2) val = 2;
2246 spin_lock_irq(&hdsp->lock);
Takashi Iwaib0b98112005-10-20 18:29:58 +02002247 if (val != hdsp_da_gain(hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 change = (hdsp_set_da_gain(hdsp, val) == 0) ? 1 : 0;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002249 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 spin_unlock_irq(&hdsp->lock);
2252 return change;
2253}
2254
2255#define HDSP_AD_GAIN(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002256{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 .name = xname, \
2258 .index = xindex, \
2259 .info = snd_hdsp_info_ad_gain, \
2260 .get = snd_hdsp_get_ad_gain, \
2261 .put = snd_hdsp_put_ad_gain \
2262}
2263
Takashi Iwai55e957d2005-11-17 14:52:13 +01002264static int hdsp_ad_gain(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265{
2266 switch (hdsp->control_register & HDSP_ADGainMask) {
2267 case HDSP_ADGainMinus10dBV:
2268 return 0;
2269 case HDSP_ADGainPlus4dBu:
2270 return 1;
2271 case HDSP_ADGainLowGain:
2272 return 2;
2273 default:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002274 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 }
2276}
2277
Takashi Iwai55e957d2005-11-17 14:52:13 +01002278static int hdsp_set_ad_gain(struct hdsp *hdsp, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279{
2280 hdsp->control_register &= ~HDSP_ADGainMask;
2281 switch (mode) {
2282 case 0:
2283 hdsp->control_register |= HDSP_ADGainMinus10dBV;
2284 break;
2285 case 1:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002286 hdsp->control_register |= HDSP_ADGainPlus4dBu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 break;
2288 case 2:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002289 hdsp->control_register |= HDSP_ADGainLowGain;
2290 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 default:
2292 return -1;
2293
2294 }
2295 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2296 return 0;
2297}
2298
Takashi Iwai55e957d2005-11-17 14:52:13 +01002299static int snd_hdsp_info_ad_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300{
2301 static char *texts[] = {"-10 dBV", "+4 dBu", "Lo Gain"};
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002302
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2304 uinfo->count = 1;
2305 uinfo->value.enumerated.items = 3;
2306 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2307 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2308 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2309 return 0;
2310}
2311
Takashi Iwai55e957d2005-11-17 14:52:13 +01002312static int snd_hdsp_get_ad_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002314 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002315
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 ucontrol->value.enumerated.item[0] = hdsp_ad_gain(hdsp);
2317 return 0;
2318}
2319
Takashi Iwai55e957d2005-11-17 14:52:13 +01002320static int snd_hdsp_put_ad_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002322 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 int change;
2324 int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002325
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 if (!snd_hdsp_use_is_exclusive(hdsp))
2327 return -EBUSY;
2328 val = ucontrol->value.enumerated.item[0];
2329 if (val < 0) val = 0;
2330 if (val > 2) val = 2;
2331 spin_lock_irq(&hdsp->lock);
Takashi Iwaib0b98112005-10-20 18:29:58 +02002332 if (val != hdsp_ad_gain(hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 change = (hdsp_set_ad_gain(hdsp, val) == 0) ? 1 : 0;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002334 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 spin_unlock_irq(&hdsp->lock);
2337 return change;
2338}
2339
2340#define HDSP_PHONE_GAIN(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002341{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 .name = xname, \
2343 .index = xindex, \
2344 .info = snd_hdsp_info_phone_gain, \
2345 .get = snd_hdsp_get_phone_gain, \
2346 .put = snd_hdsp_put_phone_gain \
2347}
2348
Takashi Iwai55e957d2005-11-17 14:52:13 +01002349static int hdsp_phone_gain(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350{
2351 switch (hdsp->control_register & HDSP_PhoneGainMask) {
2352 case HDSP_PhoneGain0dB:
2353 return 0;
2354 case HDSP_PhoneGainMinus6dB:
2355 return 1;
2356 case HDSP_PhoneGainMinus12dB:
2357 return 2;
2358 default:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002359 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 }
2361}
2362
Takashi Iwai55e957d2005-11-17 14:52:13 +01002363static int hdsp_set_phone_gain(struct hdsp *hdsp, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364{
2365 hdsp->control_register &= ~HDSP_PhoneGainMask;
2366 switch (mode) {
2367 case 0:
2368 hdsp->control_register |= HDSP_PhoneGain0dB;
2369 break;
2370 case 1:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002371 hdsp->control_register |= HDSP_PhoneGainMinus6dB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 break;
2373 case 2:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002374 hdsp->control_register |= HDSP_PhoneGainMinus12dB;
2375 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 default:
2377 return -1;
2378
2379 }
2380 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2381 return 0;
2382}
2383
Takashi Iwai55e957d2005-11-17 14:52:13 +01002384static int snd_hdsp_info_phone_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385{
2386 static char *texts[] = {"0 dB", "-6 dB", "-12 dB"};
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002387
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2389 uinfo->count = 1;
2390 uinfo->value.enumerated.items = 3;
2391 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2392 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2393 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2394 return 0;
2395}
2396
Takashi Iwai55e957d2005-11-17 14:52:13 +01002397static int snd_hdsp_get_phone_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002399 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002400
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 ucontrol->value.enumerated.item[0] = hdsp_phone_gain(hdsp);
2402 return 0;
2403}
2404
Takashi Iwai55e957d2005-11-17 14:52:13 +01002405static int snd_hdsp_put_phone_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002407 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 int change;
2409 int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002410
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 if (!snd_hdsp_use_is_exclusive(hdsp))
2412 return -EBUSY;
2413 val = ucontrol->value.enumerated.item[0];
2414 if (val < 0) val = 0;
2415 if (val > 2) val = 2;
2416 spin_lock_irq(&hdsp->lock);
Takashi Iwaib0b98112005-10-20 18:29:58 +02002417 if (val != hdsp_phone_gain(hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 change = (hdsp_set_phone_gain(hdsp, val) == 0) ? 1 : 0;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002419 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 spin_unlock_irq(&hdsp->lock);
2422 return change;
2423}
2424
2425#define HDSP_XLR_BREAKOUT_CABLE(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002426{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 .name = xname, \
2428 .index = xindex, \
2429 .info = snd_hdsp_info_xlr_breakout_cable, \
2430 .get = snd_hdsp_get_xlr_breakout_cable, \
2431 .put = snd_hdsp_put_xlr_breakout_cable \
2432}
2433
Takashi Iwai55e957d2005-11-17 14:52:13 +01002434static int hdsp_xlr_breakout_cable(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435{
Takashi Iwaib0b98112005-10-20 18:29:58 +02002436 if (hdsp->control_register & HDSP_XLRBreakoutCable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 return 0;
2439}
2440
Takashi Iwai55e957d2005-11-17 14:52:13 +01002441static int hdsp_set_xlr_breakout_cable(struct hdsp *hdsp, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442{
Takashi Iwaib0b98112005-10-20 18:29:58 +02002443 if (mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 hdsp->control_register |= HDSP_XLRBreakoutCable;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002445 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 hdsp->control_register &= ~HDSP_XLRBreakoutCable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2448 return 0;
2449}
2450
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002451#define snd_hdsp_info_xlr_breakout_cable snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452
Takashi Iwai55e957d2005-11-17 14:52:13 +01002453static int snd_hdsp_get_xlr_breakout_cable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002455 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002456
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 ucontrol->value.enumerated.item[0] = hdsp_xlr_breakout_cable(hdsp);
2458 return 0;
2459}
2460
Takashi Iwai55e957d2005-11-17 14:52:13 +01002461static int snd_hdsp_put_xlr_breakout_cable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002463 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 int change;
2465 int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002466
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 if (!snd_hdsp_use_is_exclusive(hdsp))
2468 return -EBUSY;
2469 val = ucontrol->value.integer.value[0] & 1;
2470 spin_lock_irq(&hdsp->lock);
2471 change = (int)val != hdsp_xlr_breakout_cable(hdsp);
2472 hdsp_set_xlr_breakout_cable(hdsp, val);
2473 spin_unlock_irq(&hdsp->lock);
2474 return change;
2475}
2476
2477/* (De)activates old RME Analog Extension Board
2478 These are connected to the internal ADAT connector
2479 Switching this on desactivates external ADAT
2480*/
2481#define HDSP_AEB(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002482{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 .name = xname, \
2484 .index = xindex, \
2485 .info = snd_hdsp_info_aeb, \
2486 .get = snd_hdsp_get_aeb, \
2487 .put = snd_hdsp_put_aeb \
2488}
2489
Takashi Iwai55e957d2005-11-17 14:52:13 +01002490static int hdsp_aeb(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491{
Takashi Iwaib0b98112005-10-20 18:29:58 +02002492 if (hdsp->control_register & HDSP_AnalogExtensionBoard)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 return 0;
2495}
2496
Takashi Iwai55e957d2005-11-17 14:52:13 +01002497static int hdsp_set_aeb(struct hdsp *hdsp, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498{
Takashi Iwaib0b98112005-10-20 18:29:58 +02002499 if (mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 hdsp->control_register |= HDSP_AnalogExtensionBoard;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002501 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 hdsp->control_register &= ~HDSP_AnalogExtensionBoard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2504 return 0;
2505}
2506
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002507#define snd_hdsp_info_aeb snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
Takashi Iwai55e957d2005-11-17 14:52:13 +01002509static int snd_hdsp_get_aeb(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002511 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002512
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 ucontrol->value.enumerated.item[0] = hdsp_aeb(hdsp);
2514 return 0;
2515}
2516
Takashi Iwai55e957d2005-11-17 14:52:13 +01002517static int snd_hdsp_put_aeb(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002519 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 int change;
2521 int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002522
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 if (!snd_hdsp_use_is_exclusive(hdsp))
2524 return -EBUSY;
2525 val = ucontrol->value.integer.value[0] & 1;
2526 spin_lock_irq(&hdsp->lock);
2527 change = (int)val != hdsp_aeb(hdsp);
2528 hdsp_set_aeb(hdsp, val);
2529 spin_unlock_irq(&hdsp->lock);
2530 return change;
2531}
2532
2533#define HDSP_PREF_SYNC_REF(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002534{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 .name = xname, \
2536 .index = xindex, \
2537 .info = snd_hdsp_info_pref_sync_ref, \
2538 .get = snd_hdsp_get_pref_sync_ref, \
2539 .put = snd_hdsp_put_pref_sync_ref \
2540}
2541
Takashi Iwai55e957d2005-11-17 14:52:13 +01002542static int hdsp_pref_sync_ref(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543{
2544 /* Notice that this looks at the requested sync source,
2545 not the one actually in use.
2546 */
2547
2548 switch (hdsp->control_register & HDSP_SyncRefMask) {
2549 case HDSP_SyncRef_ADAT1:
2550 return HDSP_SYNC_FROM_ADAT1;
2551 case HDSP_SyncRef_ADAT2:
2552 return HDSP_SYNC_FROM_ADAT2;
2553 case HDSP_SyncRef_ADAT3:
2554 return HDSP_SYNC_FROM_ADAT3;
2555 case HDSP_SyncRef_SPDIF:
2556 return HDSP_SYNC_FROM_SPDIF;
2557 case HDSP_SyncRef_WORD:
2558 return HDSP_SYNC_FROM_WORD;
2559 case HDSP_SyncRef_ADAT_SYNC:
2560 return HDSP_SYNC_FROM_ADAT_SYNC;
2561 default:
2562 return HDSP_SYNC_FROM_WORD;
2563 }
2564 return 0;
2565}
2566
Takashi Iwai55e957d2005-11-17 14:52:13 +01002567static int hdsp_set_pref_sync_ref(struct hdsp *hdsp, int pref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568{
2569 hdsp->control_register &= ~HDSP_SyncRefMask;
2570 switch (pref) {
2571 case HDSP_SYNC_FROM_ADAT1:
2572 hdsp->control_register &= ~HDSP_SyncRefMask; /* clear SyncRef bits */
2573 break;
2574 case HDSP_SYNC_FROM_ADAT2:
2575 hdsp->control_register |= HDSP_SyncRef_ADAT2;
2576 break;
2577 case HDSP_SYNC_FROM_ADAT3:
2578 hdsp->control_register |= HDSP_SyncRef_ADAT3;
2579 break;
2580 case HDSP_SYNC_FROM_SPDIF:
2581 hdsp->control_register |= HDSP_SyncRef_SPDIF;
2582 break;
2583 case HDSP_SYNC_FROM_WORD:
2584 hdsp->control_register |= HDSP_SyncRef_WORD;
2585 break;
2586 case HDSP_SYNC_FROM_ADAT_SYNC:
2587 hdsp->control_register |= HDSP_SyncRef_ADAT_SYNC;
2588 break;
2589 default:
2590 return -1;
2591 }
2592 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2593 return 0;
2594}
2595
Takashi Iwai55e957d2005-11-17 14:52:13 +01002596static int snd_hdsp_info_pref_sync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597{
2598 static char *texts[] = {"Word", "IEC958", "ADAT1", "ADAT Sync", "ADAT2", "ADAT3" };
Takashi Iwai55e957d2005-11-17 14:52:13 +01002599 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002600
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2602 uinfo->count = 1;
2603
2604 switch (hdsp->io_type) {
2605 case Digiface:
2606 case H9652:
2607 uinfo->value.enumerated.items = 6;
2608 break;
2609 case Multiface:
2610 uinfo->value.enumerated.items = 4;
2611 break;
2612 case H9632:
2613 uinfo->value.enumerated.items = 3;
2614 break;
2615 default:
2616 uinfo->value.enumerated.items = 0;
2617 break;
2618 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002619
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2621 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2622 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2623 return 0;
2624}
2625
Takashi Iwai55e957d2005-11-17 14:52:13 +01002626static int snd_hdsp_get_pref_sync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002628 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002629
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 ucontrol->value.enumerated.item[0] = hdsp_pref_sync_ref(hdsp);
2631 return 0;
2632}
2633
Takashi Iwai55e957d2005-11-17 14:52:13 +01002634static int snd_hdsp_put_pref_sync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002636 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 int change, max;
2638 unsigned int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002639
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 if (!snd_hdsp_use_is_exclusive(hdsp))
2641 return -EBUSY;
2642
2643 switch (hdsp->io_type) {
2644 case Digiface:
2645 case H9652:
2646 max = 6;
2647 break;
2648 case Multiface:
2649 max = 4;
2650 break;
2651 case H9632:
2652 max = 3;
2653 break;
2654 default:
2655 return -EIO;
2656 }
2657
2658 val = ucontrol->value.enumerated.item[0] % max;
2659 spin_lock_irq(&hdsp->lock);
2660 change = (int)val != hdsp_pref_sync_ref(hdsp);
2661 hdsp_set_pref_sync_ref(hdsp, val);
2662 spin_unlock_irq(&hdsp->lock);
2663 return change;
2664}
2665
2666#define HDSP_AUTOSYNC_REF(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002667{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 .name = xname, \
2669 .index = xindex, \
2670 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
2671 .info = snd_hdsp_info_autosync_ref, \
2672 .get = snd_hdsp_get_autosync_ref, \
2673}
2674
Takashi Iwai55e957d2005-11-17 14:52:13 +01002675static int hdsp_autosync_ref(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676{
2677 /* This looks at the autosync selected sync reference */
2678 unsigned int status2 = hdsp_read(hdsp, HDSP_status2Register);
2679
2680 switch (status2 & HDSP_SelSyncRefMask) {
2681 case HDSP_SelSyncRef_WORD:
2682 return HDSP_AUTOSYNC_FROM_WORD;
2683 case HDSP_SelSyncRef_ADAT_SYNC:
2684 return HDSP_AUTOSYNC_FROM_ADAT_SYNC;
2685 case HDSP_SelSyncRef_SPDIF:
2686 return HDSP_AUTOSYNC_FROM_SPDIF;
2687 case HDSP_SelSyncRefMask:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002688 return HDSP_AUTOSYNC_FROM_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 case HDSP_SelSyncRef_ADAT1:
2690 return HDSP_AUTOSYNC_FROM_ADAT1;
2691 case HDSP_SelSyncRef_ADAT2:
2692 return HDSP_AUTOSYNC_FROM_ADAT2;
2693 case HDSP_SelSyncRef_ADAT3:
2694 return HDSP_AUTOSYNC_FROM_ADAT3;
2695 default:
2696 return HDSP_AUTOSYNC_FROM_WORD;
2697 }
2698 return 0;
2699}
2700
Takashi Iwai55e957d2005-11-17 14:52:13 +01002701static int snd_hdsp_info_autosync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702{
2703 static char *texts[] = {"Word", "ADAT Sync", "IEC958", "None", "ADAT1", "ADAT2", "ADAT3" };
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002704
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2706 uinfo->count = 1;
2707 uinfo->value.enumerated.items = 7;
2708 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2709 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2710 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2711 return 0;
2712}
2713
Takashi Iwai55e957d2005-11-17 14:52:13 +01002714static int snd_hdsp_get_autosync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002716 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002717
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 ucontrol->value.enumerated.item[0] = hdsp_autosync_ref(hdsp);
2719 return 0;
2720}
2721
2722#define HDSP_LINE_OUT(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002723{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 .name = xname, \
2725 .index = xindex, \
2726 .info = snd_hdsp_info_line_out, \
2727 .get = snd_hdsp_get_line_out, \
2728 .put = snd_hdsp_put_line_out \
2729}
2730
Takashi Iwai55e957d2005-11-17 14:52:13 +01002731static int hdsp_line_out(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732{
2733 return (hdsp->control_register & HDSP_LineOut) ? 1 : 0;
2734}
2735
Takashi Iwai55e957d2005-11-17 14:52:13 +01002736static int hdsp_set_line_output(struct hdsp *hdsp, int out)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737{
Takashi Iwaib0b98112005-10-20 18:29:58 +02002738 if (out)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 hdsp->control_register |= HDSP_LineOut;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002740 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 hdsp->control_register &= ~HDSP_LineOut;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2743 return 0;
2744}
2745
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002746#define snd_hdsp_info_line_out snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747
Takashi Iwai55e957d2005-11-17 14:52:13 +01002748static int snd_hdsp_get_line_out(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002750 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002751
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 spin_lock_irq(&hdsp->lock);
2753 ucontrol->value.integer.value[0] = hdsp_line_out(hdsp);
2754 spin_unlock_irq(&hdsp->lock);
2755 return 0;
2756}
2757
Takashi Iwai55e957d2005-11-17 14:52:13 +01002758static int snd_hdsp_put_line_out(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002760 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 int change;
2762 unsigned int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002763
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 if (!snd_hdsp_use_is_exclusive(hdsp))
2765 return -EBUSY;
2766 val = ucontrol->value.integer.value[0] & 1;
2767 spin_lock_irq(&hdsp->lock);
2768 change = (int)val != hdsp_line_out(hdsp);
2769 hdsp_set_line_output(hdsp, val);
2770 spin_unlock_irq(&hdsp->lock);
2771 return change;
2772}
2773
2774#define HDSP_PRECISE_POINTER(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002775{ .iface = SNDRV_CTL_ELEM_IFACE_CARD, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 .name = xname, \
2777 .index = xindex, \
2778 .info = snd_hdsp_info_precise_pointer, \
2779 .get = snd_hdsp_get_precise_pointer, \
2780 .put = snd_hdsp_put_precise_pointer \
2781}
2782
Takashi Iwai55e957d2005-11-17 14:52:13 +01002783static int hdsp_set_precise_pointer(struct hdsp *hdsp, int precise)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784{
Takashi Iwaib0b98112005-10-20 18:29:58 +02002785 if (precise)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 hdsp->precise_ptr = 1;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002787 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 hdsp->precise_ptr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 return 0;
2790}
2791
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002792#define snd_hdsp_info_precise_pointer snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793
Takashi Iwai55e957d2005-11-17 14:52:13 +01002794static int snd_hdsp_get_precise_pointer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002796 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002797
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 spin_lock_irq(&hdsp->lock);
2799 ucontrol->value.integer.value[0] = hdsp->precise_ptr;
2800 spin_unlock_irq(&hdsp->lock);
2801 return 0;
2802}
2803
Takashi Iwai55e957d2005-11-17 14:52:13 +01002804static int snd_hdsp_put_precise_pointer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002806 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 int change;
2808 unsigned int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002809
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 if (!snd_hdsp_use_is_exclusive(hdsp))
2811 return -EBUSY;
2812 val = ucontrol->value.integer.value[0] & 1;
2813 spin_lock_irq(&hdsp->lock);
2814 change = (int)val != hdsp->precise_ptr;
2815 hdsp_set_precise_pointer(hdsp, val);
2816 spin_unlock_irq(&hdsp->lock);
2817 return change;
2818}
2819
2820#define HDSP_USE_MIDI_TASKLET(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002821{ .iface = SNDRV_CTL_ELEM_IFACE_CARD, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 .name = xname, \
2823 .index = xindex, \
2824 .info = snd_hdsp_info_use_midi_tasklet, \
2825 .get = snd_hdsp_get_use_midi_tasklet, \
2826 .put = snd_hdsp_put_use_midi_tasklet \
2827}
2828
Takashi Iwai55e957d2005-11-17 14:52:13 +01002829static int hdsp_set_use_midi_tasklet(struct hdsp *hdsp, int use_tasklet)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830{
Takashi Iwaib0b98112005-10-20 18:29:58 +02002831 if (use_tasklet)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 hdsp->use_midi_tasklet = 1;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002833 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 hdsp->use_midi_tasklet = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 return 0;
2836}
2837
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002838#define snd_hdsp_info_use_midi_tasklet snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839
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);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002843
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 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;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002855
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 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];
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002897
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);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002902
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 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{
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002950 static char *texts[] = {"No Lock", "Lock", "Sync" };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 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;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002995 else {
Takashi Iwaib0b98112005-10-20 18:29:58 +02002996 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;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003031}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032
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)
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003049{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 int status = hdsp_read(hdsp, HDSP_statusRegister);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003051
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 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
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003056 return 1;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003057 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 return 0;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003059}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060
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;
Takashi Iwaida3cec32008-08-08 17:12:14 +02003067 snd_BUG_ON(offset < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068
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:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003077 if (offset >= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 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
Julian Cablee4b60882007-03-19 11:44:40 +01003088#define HDSP_DDS_OFFSET(xname, xindex) \
3089{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3090 .name = xname, \
3091 .index = xindex, \
3092 .info = snd_hdsp_info_dds_offset, \
3093 .get = snd_hdsp_get_dds_offset, \
3094 .put = snd_hdsp_put_dds_offset \
3095}
3096
3097static int hdsp_dds_offset(struct hdsp *hdsp)
3098{
3099 u64 n;
3100 u32 r;
3101 unsigned int dds_value = hdsp->dds_value;
3102 int system_sample_rate = hdsp->system_sample_rate;
3103
Takashi Iwai2a3988f2007-10-16 14:26:32 +02003104 if (!dds_value)
3105 return 0;
3106
Julian Cablee4b60882007-03-19 11:44:40 +01003107 n = DDS_NUMERATOR;
3108 /*
3109 * dds_value = n / rate
3110 * rate = n / dds_value
3111 */
3112 div64_32(&n, dds_value, &r);
3113 if (system_sample_rate >= 112000)
3114 n *= 4;
3115 else if (system_sample_rate >= 56000)
3116 n *= 2;
3117 return ((int)n) - system_sample_rate;
3118}
3119
3120static int hdsp_set_dds_offset(struct hdsp *hdsp, int offset_hz)
3121{
3122 int rate = hdsp->system_sample_rate + offset_hz;
3123 hdsp_set_dds_value(hdsp, rate);
3124 return 0;
3125}
3126
3127static int snd_hdsp_info_dds_offset(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
3128{
3129 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3130 uinfo->count = 1;
3131 uinfo->value.integer.min = -5000;
3132 uinfo->value.integer.max = 5000;
3133 return 0;
3134}
3135
3136static int snd_hdsp_get_dds_offset(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3137{
3138 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003139
Julian Cablee4b60882007-03-19 11:44:40 +01003140 ucontrol->value.enumerated.item[0] = hdsp_dds_offset(hdsp);
3141 return 0;
3142}
3143
3144static int snd_hdsp_put_dds_offset(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3145{
3146 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
3147 int change;
3148 int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003149
Julian Cablee4b60882007-03-19 11:44:40 +01003150 if (!snd_hdsp_use_is_exclusive(hdsp))
3151 return -EBUSY;
3152 val = ucontrol->value.enumerated.item[0];
3153 spin_lock_irq(&hdsp->lock);
3154 if (val != hdsp_dds_offset(hdsp))
3155 change = (hdsp_set_dds_offset(hdsp, val) == 0) ? 1 : 0;
3156 else
3157 change = 0;
3158 spin_unlock_irq(&hdsp->lock);
3159 return change;
3160}
3161
Takashi Iwai55e957d2005-11-17 14:52:13 +01003162static struct snd_kcontrol_new snd_hdsp_9632_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163HDSP_DA_GAIN("DA Gain", 0),
3164HDSP_AD_GAIN("AD Gain", 0),
3165HDSP_PHONE_GAIN("Phones Gain", 0),
Julian Cablee4b60882007-03-19 11:44:40 +01003166HDSP_XLR_BREAKOUT_CABLE("XLR Breakout Cable", 0),
3167HDSP_DDS_OFFSET("DDS Sample Rate Offset", 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168};
3169
Takashi Iwai55e957d2005-11-17 14:52:13 +01003170static struct snd_kcontrol_new snd_hdsp_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171{
Clemens Ladisch5549d542005-08-03 13:50:30 +02003172 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
3174 .info = snd_hdsp_control_spdif_info,
3175 .get = snd_hdsp_control_spdif_get,
3176 .put = snd_hdsp_control_spdif_put,
3177},
3178{
3179 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
Clemens Ladisch5549d542005-08-03 13:50:30 +02003180 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
3182 .info = snd_hdsp_control_spdif_stream_info,
3183 .get = snd_hdsp_control_spdif_stream_get,
3184 .put = snd_hdsp_control_spdif_stream_put,
3185},
3186{
3187 .access = SNDRV_CTL_ELEM_ACCESS_READ,
Clemens Ladisch5549d542005-08-03 13:50:30 +02003188 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
3190 .info = snd_hdsp_control_spdif_mask_info,
3191 .get = snd_hdsp_control_spdif_mask_get,
3192 .private_value = IEC958_AES0_NONAUDIO |
3193 IEC958_AES0_PROFESSIONAL |
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003194 IEC958_AES0_CON_EMPHASIS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195},
3196{
3197 .access = SNDRV_CTL_ELEM_ACCESS_READ,
Clemens Ladisch5549d542005-08-03 13:50:30 +02003198 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
3200 .info = snd_hdsp_control_spdif_mask_info,
3201 .get = snd_hdsp_control_spdif_mask_get,
3202 .private_value = IEC958_AES0_NONAUDIO |
3203 IEC958_AES0_PROFESSIONAL |
3204 IEC958_AES0_PRO_EMPHASIS,
3205},
3206HDSP_MIXER("Mixer", 0),
3207HDSP_SPDIF_IN("IEC958 Input Connector", 0),
3208HDSP_SPDIF_OUT("IEC958 Output also on ADAT1", 0),
3209HDSP_SPDIF_PROFESSIONAL("IEC958 Professional Bit", 0),
3210HDSP_SPDIF_EMPHASIS("IEC958 Emphasis Bit", 0),
3211HDSP_SPDIF_NON_AUDIO("IEC958 Non-audio Bit", 0),
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003212/* 'Sample Clock Source' complies with the alsa control naming scheme */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213HDSP_CLOCK_SOURCE("Sample Clock Source", 0),
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02003214{
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02003215 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3216 .name = "Sample Clock Source Locking",
3217 .info = snd_hdsp_info_clock_source_lock,
3218 .get = snd_hdsp_get_clock_source_lock,
3219 .put = snd_hdsp_put_clock_source_lock,
3220},
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221HDSP_SYSTEM_CLOCK_MODE("System Clock Mode", 0),
3222HDSP_PREF_SYNC_REF("Preferred Sync Reference", 0),
3223HDSP_AUTOSYNC_REF("AutoSync Reference", 0),
3224HDSP_SPDIF_SAMPLE_RATE("SPDIF Sample Rate", 0),
3225HDSP_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
3226/* 'External Rate' complies with the alsa control naming scheme */
3227HDSP_AUTOSYNC_SAMPLE_RATE("External Rate", 0),
3228HDSP_WC_SYNC_CHECK("Word Clock Lock Status", 0),
3229HDSP_SPDIF_SYNC_CHECK("SPDIF Lock Status", 0),
3230HDSP_ADATSYNC_SYNC_CHECK("ADAT Sync Lock Status", 0),
3231HDSP_LINE_OUT("Line Out", 0),
3232HDSP_PRECISE_POINTER("Precise Pointer", 0),
3233HDSP_USE_MIDI_TASKLET("Use Midi Tasklet", 0),
3234};
3235
Takashi Iwai55e957d2005-11-17 14:52:13 +01003236static struct snd_kcontrol_new snd_hdsp_96xx_aeb = HDSP_AEB("Analog Extension Board", 0);
3237static struct snd_kcontrol_new snd_hdsp_adat_sync_check = HDSP_ADAT_SYNC_CHECK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238
Takashi Iwai55e957d2005-11-17 14:52:13 +01003239static int snd_hdsp_create_controls(struct snd_card *card, struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240{
3241 unsigned int idx;
3242 int err;
Takashi Iwai55e957d2005-11-17 14:52:13 +01003243 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244
3245 for (idx = 0; idx < ARRAY_SIZE(snd_hdsp_controls); idx++) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02003246 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_controls[idx], hdsp))) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 if (idx == 1) /* IEC958 (S/PDIF) Stream */
3249 hdsp->spdif_ctl = kctl;
3250 }
3251
3252 /* ADAT SyncCheck status */
3253 snd_hdsp_adat_sync_check.name = "ADAT Lock Status";
3254 snd_hdsp_adat_sync_check.index = 1;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003255 if ((err = snd_ctl_add (card, kctl = snd_ctl_new1(&snd_hdsp_adat_sync_check, hdsp))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 if (hdsp->io_type == Digiface || hdsp->io_type == H9652) {
3258 for (idx = 1; idx < 3; ++idx) {
3259 snd_hdsp_adat_sync_check.index = idx+1;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003260 if ((err = snd_ctl_add (card, kctl = snd_ctl_new1(&snd_hdsp_adat_sync_check, hdsp))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 }
3263 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003264
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 /* DA, AD and Phone gain and XLR breakout cable controls for H9632 cards */
3266 if (hdsp->io_type == H9632) {
3267 for (idx = 0; idx < ARRAY_SIZE(snd_hdsp_9632_controls); idx++) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02003268 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_9632_controls[idx], hdsp))) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 }
3271 }
3272
3273 /* AEB control for H96xx card */
3274 if (hdsp->io_type == H9632 || hdsp->io_type == H9652) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02003275 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_96xx_aeb, hdsp))) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 }
3278
3279 return 0;
3280}
3281
3282/*------------------------------------------------------------
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003283 /proc interface
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 ------------------------------------------------------------*/
3285
3286static void
Takashi Iwai55e957d2005-11-17 14:52:13 +01003287snd_hdsp_proc_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003289 struct hdsp *hdsp = (struct hdsp *) entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 unsigned int status;
3291 unsigned int status2;
3292 char *pref_sync_ref;
3293 char *autosync_ref;
3294 char *system_clock_mode;
3295 char *clock_source;
3296 int x;
3297
Remy Brunoecb594e2006-06-12 09:25:22 +02003298 if (hdsp_check_for_iobox (hdsp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 snd_iprintf(buffer, "No I/O box connected.\nPlease connect one and upload firmware.\n");
3300 return;
Remy Brunoecb594e2006-06-12 09:25:22 +02003301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302
Takashi Iwaib0b98112005-10-20 18:29:58 +02003303 if (hdsp_check_for_firmware(hdsp, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 if (hdsp->state & HDSP_FirmwareCached) {
3305 if (snd_hdsp_load_firmware_from_cache(hdsp) != 0) {
3306 snd_iprintf(buffer, "Firmware loading from cache failed, please upload manually.\n");
3307 return;
3308 }
3309 } else {
Takashi Iwai311e70a2006-09-06 12:13:37 +02003310 int err = -EINVAL;
3311#ifdef HDSP_FW_LOADER
3312 err = hdsp_request_fw_loader(hdsp);
3313#endif
3314 if (err < 0) {
3315 snd_iprintf(buffer,
3316 "No firmware loaded nor cached, "
3317 "please upload firmware.\n");
3318 return;
3319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 }
3321 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003322
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 status = hdsp_read(hdsp, HDSP_statusRegister);
3324 status2 = hdsp_read(hdsp, HDSP_status2Register);
3325
3326 snd_iprintf(buffer, "%s (Card #%d)\n", hdsp->card_name, hdsp->card->number + 1);
3327 snd_iprintf(buffer, "Buffers: capture %p playback %p\n",
3328 hdsp->capture_buffer, hdsp->playback_buffer);
3329 snd_iprintf(buffer, "IRQ: %d Registers bus: 0x%lx VM: 0x%lx\n",
3330 hdsp->irq, hdsp->port, (unsigned long)hdsp->iobase);
3331 snd_iprintf(buffer, "Control register: 0x%x\n", hdsp->control_register);
3332 snd_iprintf(buffer, "Control2 register: 0x%x\n", hdsp->control2_register);
3333 snd_iprintf(buffer, "Status register: 0x%x\n", status);
3334 snd_iprintf(buffer, "Status2 register: 0x%x\n", status2);
3335 snd_iprintf(buffer, "FIFO status: %d\n", hdsp_read(hdsp, HDSP_fifoStatus) & 0xff);
3336 snd_iprintf(buffer, "MIDI1 Output status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusOut0));
3337 snd_iprintf(buffer, "MIDI1 Input status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusIn0));
3338 snd_iprintf(buffer, "MIDI2 Output status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusOut1));
3339 snd_iprintf(buffer, "MIDI2 Input status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusIn1));
3340 snd_iprintf(buffer, "Use Midi Tasklet: %s\n", hdsp->use_midi_tasklet ? "on" : "off");
3341
3342 snd_iprintf(buffer, "\n");
3343
3344 x = 1 << (6 + hdsp_decode_latency(hdsp->control_register & HDSP_LatencyMask));
3345
3346 snd_iprintf(buffer, "Buffer Size (Latency): %d samples (2 periods of %lu bytes)\n", x, (unsigned long) hdsp->period_bytes);
3347 snd_iprintf(buffer, "Hardware pointer (frames): %ld\n", hdsp_hw_pointer(hdsp));
3348 snd_iprintf(buffer, "Precise pointer: %s\n", hdsp->precise_ptr ? "on" : "off");
3349 snd_iprintf(buffer, "Line out: %s\n", (hdsp->control_register & HDSP_LineOut) ? "on" : "off");
3350
3351 snd_iprintf(buffer, "Firmware version: %d\n", (status2&HDSP_version0)|(status2&HDSP_version1)<<1|(status2&HDSP_version2)<<2);
3352
3353 snd_iprintf(buffer, "\n");
3354
3355
3356 switch (hdsp_clock_source(hdsp)) {
3357 case HDSP_CLOCK_SOURCE_AUTOSYNC:
3358 clock_source = "AutoSync";
3359 break;
3360 case HDSP_CLOCK_SOURCE_INTERNAL_32KHZ:
3361 clock_source = "Internal 32 kHz";
3362 break;
3363 case HDSP_CLOCK_SOURCE_INTERNAL_44_1KHZ:
3364 clock_source = "Internal 44.1 kHz";
3365 break;
3366 case HDSP_CLOCK_SOURCE_INTERNAL_48KHZ:
3367 clock_source = "Internal 48 kHz";
3368 break;
3369 case HDSP_CLOCK_SOURCE_INTERNAL_64KHZ:
3370 clock_source = "Internal 64 kHz";
3371 break;
3372 case HDSP_CLOCK_SOURCE_INTERNAL_88_2KHZ:
3373 clock_source = "Internal 88.2 kHz";
3374 break;
3375 case HDSP_CLOCK_SOURCE_INTERNAL_96KHZ:
3376 clock_source = "Internal 96 kHz";
3377 break;
3378 case HDSP_CLOCK_SOURCE_INTERNAL_128KHZ:
3379 clock_source = "Internal 128 kHz";
3380 break;
3381 case HDSP_CLOCK_SOURCE_INTERNAL_176_4KHZ:
3382 clock_source = "Internal 176.4 kHz";
3383 break;
3384 case HDSP_CLOCK_SOURCE_INTERNAL_192KHZ:
3385 clock_source = "Internal 192 kHz";
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003386 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 default:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003388 clock_source = "Error";
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 }
3390 snd_iprintf (buffer, "Sample Clock Source: %s\n", clock_source);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003391
Takashi Iwaib0b98112005-10-20 18:29:58 +02003392 if (hdsp_system_clock_mode(hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 system_clock_mode = "Slave";
Takashi Iwaib0b98112005-10-20 18:29:58 +02003394 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 system_clock_mode = "Master";
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003396
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 switch (hdsp_pref_sync_ref (hdsp)) {
3398 case HDSP_SYNC_FROM_WORD:
3399 pref_sync_ref = "Word Clock";
3400 break;
3401 case HDSP_SYNC_FROM_ADAT_SYNC:
3402 pref_sync_ref = "ADAT Sync";
3403 break;
3404 case HDSP_SYNC_FROM_SPDIF:
3405 pref_sync_ref = "SPDIF";
3406 break;
3407 case HDSP_SYNC_FROM_ADAT1:
3408 pref_sync_ref = "ADAT1";
3409 break;
3410 case HDSP_SYNC_FROM_ADAT2:
3411 pref_sync_ref = "ADAT2";
3412 break;
3413 case HDSP_SYNC_FROM_ADAT3:
3414 pref_sync_ref = "ADAT3";
3415 break;
3416 default:
3417 pref_sync_ref = "Word Clock";
3418 break;
3419 }
3420 snd_iprintf (buffer, "Preferred Sync Reference: %s\n", pref_sync_ref);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003421
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 switch (hdsp_autosync_ref (hdsp)) {
3423 case HDSP_AUTOSYNC_FROM_WORD:
3424 autosync_ref = "Word Clock";
3425 break;
3426 case HDSP_AUTOSYNC_FROM_ADAT_SYNC:
3427 autosync_ref = "ADAT Sync";
3428 break;
3429 case HDSP_AUTOSYNC_FROM_SPDIF:
3430 autosync_ref = "SPDIF";
3431 break;
3432 case HDSP_AUTOSYNC_FROM_NONE:
3433 autosync_ref = "None";
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003434 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 case HDSP_AUTOSYNC_FROM_ADAT1:
3436 autosync_ref = "ADAT1";
3437 break;
3438 case HDSP_AUTOSYNC_FROM_ADAT2:
3439 autosync_ref = "ADAT2";
3440 break;
3441 case HDSP_AUTOSYNC_FROM_ADAT3:
3442 autosync_ref = "ADAT3";
3443 break;
3444 default:
3445 autosync_ref = "---";
3446 break;
3447 }
3448 snd_iprintf (buffer, "AutoSync Reference: %s\n", autosync_ref);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003449
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 snd_iprintf (buffer, "AutoSync Frequency: %d\n", hdsp_external_sample_rate(hdsp));
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003451
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 snd_iprintf (buffer, "System Clock Mode: %s\n", system_clock_mode);
3453
3454 snd_iprintf (buffer, "System Clock Frequency: %d\n", hdsp->system_sample_rate);
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02003455 snd_iprintf (buffer, "System Clock Locked: %s\n", hdsp->clock_source_locked ? "Yes" : "No");
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003456
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 snd_iprintf(buffer, "\n");
3458
3459 switch (hdsp_spdif_in(hdsp)) {
3460 case HDSP_SPDIFIN_OPTICAL:
3461 snd_iprintf(buffer, "IEC958 input: Optical\n");
3462 break;
3463 case HDSP_SPDIFIN_COAXIAL:
3464 snd_iprintf(buffer, "IEC958 input: Coaxial\n");
3465 break;
3466 case HDSP_SPDIFIN_INTERNAL:
3467 snd_iprintf(buffer, "IEC958 input: Internal\n");
3468 break;
3469 case HDSP_SPDIFIN_AES:
3470 snd_iprintf(buffer, "IEC958 input: AES\n");
3471 break;
3472 default:
3473 snd_iprintf(buffer, "IEC958 input: ???\n");
3474 break;
3475 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003476
Takashi Iwaib0b98112005-10-20 18:29:58 +02003477 if (hdsp->control_register & HDSP_SPDIFOpticalOut)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 snd_iprintf(buffer, "IEC958 output: Coaxial & ADAT1\n");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003479 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 snd_iprintf(buffer, "IEC958 output: Coaxial only\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481
Takashi Iwaib0b98112005-10-20 18:29:58 +02003482 if (hdsp->control_register & HDSP_SPDIFProfessional)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 snd_iprintf(buffer, "IEC958 quality: Professional\n");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003484 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 snd_iprintf(buffer, "IEC958 quality: Consumer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486
Takashi Iwaib0b98112005-10-20 18:29:58 +02003487 if (hdsp->control_register & HDSP_SPDIFEmphasis)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 snd_iprintf(buffer, "IEC958 emphasis: on\n");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003489 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 snd_iprintf(buffer, "IEC958 emphasis: off\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491
Takashi Iwaib0b98112005-10-20 18:29:58 +02003492 if (hdsp->control_register & HDSP_SPDIFNonAudio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 snd_iprintf(buffer, "IEC958 NonAudio: on\n");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003494 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 snd_iprintf(buffer, "IEC958 NonAudio: off\n");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003496 if ((x = hdsp_spdif_sample_rate (hdsp)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 snd_iprintf (buffer, "IEC958 sample rate: %d\n", x);
Takashi Iwaib0b98112005-10-20 18:29:58 +02003498 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 snd_iprintf (buffer, "IEC958 sample rate: Error flag set\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500
3501 snd_iprintf(buffer, "\n");
3502
3503 /* Sync Check */
3504 x = status & HDSP_Sync0;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003505 if (status & HDSP_Lock0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 snd_iprintf(buffer, "ADAT1: %s\n", x ? "Sync" : "Lock");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003507 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 snd_iprintf(buffer, "ADAT1: No Lock\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509
3510 switch (hdsp->io_type) {
3511 case Digiface:
3512 case H9652:
3513 x = status & HDSP_Sync1;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003514 if (status & HDSP_Lock1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 snd_iprintf(buffer, "ADAT2: %s\n", x ? "Sync" : "Lock");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003516 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 snd_iprintf(buffer, "ADAT2: No Lock\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 x = status & HDSP_Sync2;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003519 if (status & HDSP_Lock2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 snd_iprintf(buffer, "ADAT3: %s\n", x ? "Sync" : "Lock");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003521 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 snd_iprintf(buffer, "ADAT3: No Lock\n");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003523 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 default:
3525 /* relax */
3526 break;
3527 }
3528
3529 x = status & HDSP_SPDIFSync;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003530 if (status & HDSP_SPDIFErrorFlag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 snd_iprintf (buffer, "SPDIF: No Lock\n");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003532 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 snd_iprintf (buffer, "SPDIF: %s\n", x ? "Sync" : "Lock");
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003534
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 x = status2 & HDSP_wc_sync;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003536 if (status2 & HDSP_wc_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 snd_iprintf (buffer, "Word Clock: %s\n", x ? "Sync" : "Lock");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003538 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 snd_iprintf (buffer, "Word Clock: No Lock\n");
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003540
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 x = status & HDSP_TimecodeSync;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003542 if (status & HDSP_TimecodeLock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 snd_iprintf(buffer, "ADAT Sync: %s\n", x ? "Sync" : "Lock");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003544 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 snd_iprintf(buffer, "ADAT Sync: No Lock\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546
3547 snd_iprintf(buffer, "\n");
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003548
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 /* Informations about H9632 specific controls */
3550 if (hdsp->io_type == H9632) {
3551 char *tmp;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003552
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 switch (hdsp_ad_gain(hdsp)) {
3554 case 0:
3555 tmp = "-10 dBV";
3556 break;
3557 case 1:
3558 tmp = "+4 dBu";
3559 break;
3560 default:
3561 tmp = "Lo Gain";
3562 break;
3563 }
3564 snd_iprintf(buffer, "AD Gain : %s\n", tmp);
3565
3566 switch (hdsp_da_gain(hdsp)) {
3567 case 0:
3568 tmp = "Hi Gain";
3569 break;
3570 case 1:
3571 tmp = "+4 dBu";
3572 break;
3573 default:
3574 tmp = "-10 dBV";
3575 break;
3576 }
3577 snd_iprintf(buffer, "DA Gain : %s\n", tmp);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003578
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 switch (hdsp_phone_gain(hdsp)) {
3580 case 0:
3581 tmp = "0 dB";
3582 break;
3583 case 1:
3584 tmp = "-6 dB";
3585 break;
3586 default:
3587 tmp = "-12 dB";
3588 break;
3589 }
3590 snd_iprintf(buffer, "Phones Gain : %s\n", tmp);
3591
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003592 snd_iprintf(buffer, "XLR Breakout Cable : %s\n", hdsp_xlr_breakout_cable(hdsp) ? "yes" : "no");
3593
Takashi Iwaib0b98112005-10-20 18:29:58 +02003594 if (hdsp->control_register & HDSP_AnalogExtensionBoard)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 snd_iprintf(buffer, "AEB : on (ADAT1 internal)\n");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003596 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 snd_iprintf(buffer, "AEB : off (ADAT1 external)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 snd_iprintf(buffer, "\n");
3599 }
3600
3601}
3602
Randy Dunlap1374f8c2008-01-16 14:55:42 +01003603static void snd_hdsp_proc_init(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003605 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606
3607 if (! snd_card_proc_new(hdsp->card, "hdsp", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02003608 snd_info_set_text_ops(entry, hdsp, snd_hdsp_proc_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609}
3610
Takashi Iwai55e957d2005-11-17 14:52:13 +01003611static void snd_hdsp_free_buffers(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612{
3613 snd_hammerfall_free_buffer(&hdsp->capture_dma_buf, hdsp->pci);
3614 snd_hammerfall_free_buffer(&hdsp->playback_dma_buf, hdsp->pci);
3615}
3616
Takashi Iwai55e957d2005-11-17 14:52:13 +01003617static int __devinit snd_hdsp_initialize_memory(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618{
3619 unsigned long pb_bus, cb_bus;
3620
3621 if (snd_hammerfall_get_buffer(hdsp->pci, &hdsp->capture_dma_buf, HDSP_DMA_AREA_BYTES) < 0 ||
3622 snd_hammerfall_get_buffer(hdsp->pci, &hdsp->playback_dma_buf, HDSP_DMA_AREA_BYTES) < 0) {
3623 if (hdsp->capture_dma_buf.area)
3624 snd_dma_free_pages(&hdsp->capture_dma_buf);
3625 printk(KERN_ERR "%s: no buffers available\n", hdsp->card_name);
3626 return -ENOMEM;
3627 }
3628
3629 /* Align to bus-space 64K boundary */
3630
Clemens Ladisch7ab39922006-10-09 08:13:32 +02003631 cb_bus = ALIGN(hdsp->capture_dma_buf.addr, 0x10000ul);
3632 pb_bus = ALIGN(hdsp->playback_dma_buf.addr, 0x10000ul);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633
3634 /* Tell the card where it is */
3635
3636 hdsp_write(hdsp, HDSP_inputBufferAddress, cb_bus);
3637 hdsp_write(hdsp, HDSP_outputBufferAddress, pb_bus);
3638
3639 hdsp->capture_buffer = hdsp->capture_dma_buf.area + (cb_bus - hdsp->capture_dma_buf.addr);
3640 hdsp->playback_buffer = hdsp->playback_dma_buf.area + (pb_bus - hdsp->playback_dma_buf.addr);
3641
3642 return 0;
3643}
3644
Takashi Iwai55e957d2005-11-17 14:52:13 +01003645static int snd_hdsp_set_defaults(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646{
3647 unsigned int i;
3648
3649 /* ASSUMPTION: hdsp->lock is either held, or
3650 there is no need to hold it (e.g. during module
Joe Perches561de312007-12-18 13:13:47 +01003651 initialization).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 */
3653
3654 /* set defaults:
3655
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003656 SPDIF Input via Coax
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 Master clock mode
3658 maximum latency (7 => 2^7 = 8192 samples, 64Kbyte buffer,
3659 which implies 2 4096 sample, 32Kbyte periods).
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003660 Enable line out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661 */
3662
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003663 hdsp->control_register = HDSP_ClockModeMaster |
3664 HDSP_SPDIFInputCoaxial |
3665 hdsp_encode_latency(7) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 HDSP_LineOut;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003667
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668
3669 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
3670
3671#ifdef SNDRV_BIG_ENDIAN
3672 hdsp->control2_register = HDSP_BIGENDIAN_MODE;
3673#else
3674 hdsp->control2_register = 0;
3675#endif
Takashi Iwaib0b98112005-10-20 18:29:58 +02003676 if (hdsp->io_type == H9652)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 snd_hdsp_9652_enable_mixer (hdsp);
Takashi Iwaib0b98112005-10-20 18:29:58 +02003678 else
3679 hdsp_write (hdsp, HDSP_control2Reg, hdsp->control2_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680
3681 hdsp_reset_hw_pointer(hdsp);
3682 hdsp_compute_period_size(hdsp);
3683
3684 /* silence everything */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003685
Takashi Iwaib0b98112005-10-20 18:29:58 +02003686 for (i = 0; i < HDSP_MATRIX_MIXER_SIZE; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 hdsp->mixer_matrix[i] = MINUS_INFINITY_GAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688
3689 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 +02003690 if (hdsp_write_gain (hdsp, i, MINUS_INFINITY_GAIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003693
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 /* H9632 specific defaults */
3695 if (hdsp->io_type == H9632) {
3696 hdsp->control_register |= (HDSP_DAGainPlus4dBu | HDSP_ADGainPlus4dBu | HDSP_PhoneGain0dB);
3697 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
3698 }
3699
3700 /* set a default rate so that the channel map is set up.
3701 */
3702
3703 hdsp_set_rate(hdsp, 48000, 1);
3704
3705 return 0;
3706}
3707
3708static void hdsp_midi_tasklet(unsigned long arg)
3709{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003710 struct hdsp *hdsp = (struct hdsp *)arg;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003711
Takashi Iwaib0b98112005-10-20 18:29:58 +02003712 if (hdsp->midi[0].pending)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 snd_hdsp_midi_input_read (&hdsp->midi[0]);
Takashi Iwaib0b98112005-10-20 18:29:58 +02003714 if (hdsp->midi[1].pending)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 snd_hdsp_midi_input_read (&hdsp->midi[1]);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003716}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717
David Howells7d12e782006-10-05 14:55:46 +01003718static irqreturn_t snd_hdsp_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003720 struct hdsp *hdsp = (struct hdsp *) dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 unsigned int status;
3722 int audio;
3723 int midi0;
3724 int midi1;
3725 unsigned int midi0status;
3726 unsigned int midi1status;
3727 int schedule = 0;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003728
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 status = hdsp_read(hdsp, HDSP_statusRegister);
3730
3731 audio = status & HDSP_audioIRQPending;
3732 midi0 = status & HDSP_midi0IRQPending;
3733 midi1 = status & HDSP_midi1IRQPending;
3734
Takashi Iwaib0b98112005-10-20 18:29:58 +02003735 if (!audio && !midi0 && !midi1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737
3738 hdsp_write(hdsp, HDSP_interruptConfirmation, 0);
3739
3740 midi0status = hdsp_read (hdsp, HDSP_midiStatusIn0) & 0xff;
3741 midi1status = hdsp_read (hdsp, HDSP_midiStatusIn1) & 0xff;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003742
Takashi Iwaic2503cd2009-03-05 09:37:40 +01003743 if (!(hdsp->state & HDSP_InitializationComplete))
3744 return IRQ_HANDLED;
3745
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746 if (audio) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02003747 if (hdsp->capture_substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 snd_pcm_period_elapsed(hdsp->pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003749
Takashi Iwaib0b98112005-10-20 18:29:58 +02003750 if (hdsp->playback_substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 snd_pcm_period_elapsed(hdsp->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003753
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754 if (midi0 && midi0status) {
3755 if (hdsp->use_midi_tasklet) {
3756 /* we disable interrupts for this input until processing is done */
3757 hdsp->control_register &= ~HDSP_Midi0InterruptEnable;
3758 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
3759 hdsp->midi[0].pending = 1;
3760 schedule = 1;
3761 } else {
3762 snd_hdsp_midi_input_read (&hdsp->midi[0]);
3763 }
3764 }
3765 if (hdsp->io_type != Multiface && hdsp->io_type != H9632 && midi1 && midi1status) {
3766 if (hdsp->use_midi_tasklet) {
3767 /* we disable interrupts for this input until processing is done */
3768 hdsp->control_register &= ~HDSP_Midi1InterruptEnable;
3769 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
3770 hdsp->midi[1].pending = 1;
3771 schedule = 1;
3772 } else {
3773 snd_hdsp_midi_input_read (&hdsp->midi[1]);
3774 }
3775 }
3776 if (hdsp->use_midi_tasklet && schedule)
Takashi Iwai1f041282008-12-18 12:17:55 +01003777 tasklet_schedule(&hdsp->midi_tasklet);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 return IRQ_HANDLED;
3779}
3780
Takashi Iwai55e957d2005-11-17 14:52:13 +01003781static snd_pcm_uframes_t snd_hdsp_hw_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003783 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 return hdsp_hw_pointer(hdsp);
3785}
3786
Takashi Iwai55e957d2005-11-17 14:52:13 +01003787static char *hdsp_channel_buffer_location(struct hdsp *hdsp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 int stream,
3789 int channel)
3790
3791{
3792 int mapped_channel;
3793
Takashi Iwaida3cec32008-08-08 17:12:14 +02003794 if (snd_BUG_ON(channel < 0 || channel >= hdsp->max_channels))
3795 return NULL;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003796
Takashi Iwaib0b98112005-10-20 18:29:58 +02003797 if ((mapped_channel = hdsp->channel_map[channel]) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 return NULL;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003799
Takashi Iwaib0b98112005-10-20 18:29:58 +02003800 if (stream == SNDRV_PCM_STREAM_CAPTURE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 return hdsp->capture_buffer + (mapped_channel * HDSP_CHANNEL_BUFFER_BYTES);
Takashi Iwaib0b98112005-10-20 18:29:58 +02003802 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 return hdsp->playback_buffer + (mapped_channel * HDSP_CHANNEL_BUFFER_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804}
3805
Takashi Iwai55e957d2005-11-17 14:52:13 +01003806static int snd_hdsp_playback_copy(struct snd_pcm_substream *substream, int channel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 snd_pcm_uframes_t pos, void __user *src, snd_pcm_uframes_t count)
3808{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003809 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 char *channel_buf;
3811
Takashi Iwaida3cec32008-08-08 17:12:14 +02003812 if (snd_BUG_ON(pos + count > HDSP_CHANNEL_BUFFER_BYTES / 4))
3813 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814
3815 channel_buf = hdsp_channel_buffer_location (hdsp, substream->pstr->stream, channel);
Takashi Iwaida3cec32008-08-08 17:12:14 +02003816 if (snd_BUG_ON(!channel_buf))
3817 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 if (copy_from_user(channel_buf + pos * 4, src, count * 4))
3819 return -EFAULT;
3820 return count;
3821}
3822
Takashi Iwai55e957d2005-11-17 14:52:13 +01003823static int snd_hdsp_capture_copy(struct snd_pcm_substream *substream, int channel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 snd_pcm_uframes_t pos, void __user *dst, snd_pcm_uframes_t count)
3825{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003826 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 char *channel_buf;
3828
Takashi Iwaida3cec32008-08-08 17:12:14 +02003829 if (snd_BUG_ON(pos + count > HDSP_CHANNEL_BUFFER_BYTES / 4))
3830 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831
3832 channel_buf = hdsp_channel_buffer_location (hdsp, substream->pstr->stream, channel);
Takashi Iwaida3cec32008-08-08 17:12:14 +02003833 if (snd_BUG_ON(!channel_buf))
3834 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 if (copy_to_user(dst, channel_buf + pos * 4, count * 4))
3836 return -EFAULT;
3837 return count;
3838}
3839
Takashi Iwai55e957d2005-11-17 14:52:13 +01003840static int snd_hdsp_hw_silence(struct snd_pcm_substream *substream, int channel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841 snd_pcm_uframes_t pos, snd_pcm_uframes_t count)
3842{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003843 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 char *channel_buf;
3845
3846 channel_buf = hdsp_channel_buffer_location (hdsp, substream->pstr->stream, channel);
Takashi Iwaida3cec32008-08-08 17:12:14 +02003847 if (snd_BUG_ON(!channel_buf))
3848 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 memset(channel_buf + pos * 4, 0, count * 4);
3850 return count;
3851}
3852
Takashi Iwai55e957d2005-11-17 14:52:13 +01003853static int snd_hdsp_reset(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003855 struct snd_pcm_runtime *runtime = substream->runtime;
3856 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
3857 struct snd_pcm_substream *other;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3859 other = hdsp->capture_substream;
3860 else
3861 other = hdsp->playback_substream;
3862 if (hdsp->running)
3863 runtime->status->hw_ptr = hdsp_hw_pointer(hdsp);
3864 else
3865 runtime->status->hw_ptr = 0;
3866 if (other) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01003867 struct snd_pcm_substream *s;
3868 struct snd_pcm_runtime *oruntime = other->runtime;
Takashi Iwaief991b92007-02-22 12:52:53 +01003869 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 if (s == other) {
3871 oruntime->status->hw_ptr = runtime->status->hw_ptr;
3872 break;
3873 }
3874 }
3875 }
3876 return 0;
3877}
3878
Takashi Iwai55e957d2005-11-17 14:52:13 +01003879static int snd_hdsp_hw_params(struct snd_pcm_substream *substream,
3880 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003882 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 int err;
3884 pid_t this_pid;
3885 pid_t other_pid;
3886
Takashi Iwaib0b98112005-10-20 18:29:58 +02003887 if (hdsp_check_for_iobox (hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889
Takashi Iwaib0b98112005-10-20 18:29:58 +02003890 if (hdsp_check_for_firmware(hdsp, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892
3893 spin_lock_irq(&hdsp->lock);
3894
3895 if (substream->pstr->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3896 hdsp->control_register &= ~(HDSP_SPDIFProfessional | HDSP_SPDIFNonAudio | HDSP_SPDIFEmphasis);
3897 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register |= hdsp->creg_spdif_stream);
3898 this_pid = hdsp->playback_pid;
3899 other_pid = hdsp->capture_pid;
3900 } else {
3901 this_pid = hdsp->capture_pid;
3902 other_pid = hdsp->playback_pid;
3903 }
3904
3905 if ((other_pid > 0) && (this_pid != other_pid)) {
3906
3907 /* The other stream is open, and not by the same
3908 task as this one. Make sure that the parameters
3909 that matter are the same.
3910 */
3911
3912 if (params_rate(params) != hdsp->system_sample_rate) {
3913 spin_unlock_irq(&hdsp->lock);
3914 _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_RATE);
3915 return -EBUSY;
3916 }
3917
3918 if (params_period_size(params) != hdsp->period_bytes / 4) {
3919 spin_unlock_irq(&hdsp->lock);
3920 _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
3921 return -EBUSY;
3922 }
3923
3924 /* We're fine. */
3925
3926 spin_unlock_irq(&hdsp->lock);
3927 return 0;
3928
3929 } else {
3930 spin_unlock_irq(&hdsp->lock);
3931 }
3932
3933 /* how to make sure that the rate matches an externally-set one ?
3934 */
3935
3936 spin_lock_irq(&hdsp->lock);
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02003937 if (! hdsp->clock_source_locked) {
3938 if ((err = hdsp_set_rate(hdsp, params_rate(params), 0)) < 0) {
3939 spin_unlock_irq(&hdsp->lock);
3940 _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_RATE);
3941 return err;
3942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 }
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02003944 spin_unlock_irq(&hdsp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945
3946 if ((err = hdsp_set_interrupt_interval(hdsp, params_period_size(params))) < 0) {
3947 _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
3948 return err;
3949 }
3950
3951 return 0;
3952}
3953
Takashi Iwai55e957d2005-11-17 14:52:13 +01003954static int snd_hdsp_channel_info(struct snd_pcm_substream *substream,
3955 struct snd_pcm_channel_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003957 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 int mapped_channel;
3959
Takashi Iwaida3cec32008-08-08 17:12:14 +02003960 if (snd_BUG_ON(info->channel >= hdsp->max_channels))
3961 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962
Takashi Iwaib0b98112005-10-20 18:29:58 +02003963 if ((mapped_channel = hdsp->channel_map[info->channel]) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965
3966 info->offset = mapped_channel * HDSP_CHANNEL_BUFFER_BYTES;
3967 info->first = 0;
3968 info->step = 32;
3969 return 0;
3970}
3971
Takashi Iwai55e957d2005-11-17 14:52:13 +01003972static int snd_hdsp_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 unsigned int cmd, void *arg)
3974{
3975 switch (cmd) {
3976 case SNDRV_PCM_IOCTL1_RESET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 return snd_hdsp_reset(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
Takashi Iwaib0b98112005-10-20 18:29:58 +02003979 return snd_hdsp_channel_info(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 default:
3981 break;
3982 }
3983
3984 return snd_pcm_lib_ioctl(substream, cmd, arg);
3985}
3986
Takashi Iwai55e957d2005-11-17 14:52:13 +01003987static int snd_hdsp_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003989 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
3990 struct snd_pcm_substream *other;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 int running;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003992
Takashi Iwaib0b98112005-10-20 18:29:58 +02003993 if (hdsp_check_for_iobox (hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995
Takashi Iwai311e70a2006-09-06 12:13:37 +02003996 if (hdsp_check_for_firmware(hdsp, 0)) /* no auto-loading in trigger */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998
3999 spin_lock(&hdsp->lock);
4000 running = hdsp->running;
4001 switch (cmd) {
4002 case SNDRV_PCM_TRIGGER_START:
4003 running |= 1 << substream->stream;
4004 break;
4005 case SNDRV_PCM_TRIGGER_STOP:
4006 running &= ~(1 << substream->stream);
4007 break;
4008 default:
4009 snd_BUG();
4010 spin_unlock(&hdsp->lock);
4011 return -EINVAL;
4012 }
4013 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
4014 other = hdsp->capture_substream;
4015 else
4016 other = hdsp->playback_substream;
4017
4018 if (other) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004019 struct snd_pcm_substream *s;
Takashi Iwaief991b92007-02-22 12:52:53 +01004020 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 if (s == other) {
4022 snd_pcm_trigger_done(s, substream);
4023 if (cmd == SNDRV_PCM_TRIGGER_START)
4024 running |= 1 << s->stream;
4025 else
4026 running &= ~(1 << s->stream);
4027 goto _ok;
4028 }
4029 }
4030 if (cmd == SNDRV_PCM_TRIGGER_START) {
4031 if (!(running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) &&
4032 substream->stream == SNDRV_PCM_STREAM_CAPTURE)
4033 hdsp_silence_playback(hdsp);
4034 } else {
4035 if (running &&
4036 substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
4037 hdsp_silence_playback(hdsp);
4038 }
4039 } else {
4040 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
4041 hdsp_silence_playback(hdsp);
4042 }
4043 _ok:
4044 snd_pcm_trigger_done(substream, substream);
4045 if (!hdsp->running && running)
4046 hdsp_start_audio(hdsp);
4047 else if (hdsp->running && !running)
4048 hdsp_stop_audio(hdsp);
4049 hdsp->running = running;
4050 spin_unlock(&hdsp->lock);
4051
4052 return 0;
4053}
4054
Takashi Iwai55e957d2005-11-17 14:52:13 +01004055static int snd_hdsp_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004057 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058 int result = 0;
4059
Takashi Iwaib0b98112005-10-20 18:29:58 +02004060 if (hdsp_check_for_iobox (hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062
Takashi Iwaib0b98112005-10-20 18:29:58 +02004063 if (hdsp_check_for_firmware(hdsp, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065
4066 spin_lock_irq(&hdsp->lock);
4067 if (!hdsp->running)
4068 hdsp_reset_hw_pointer(hdsp);
4069 spin_unlock_irq(&hdsp->lock);
4070 return result;
4071}
4072
Takashi Iwai55e957d2005-11-17 14:52:13 +01004073static struct snd_pcm_hardware snd_hdsp_playback_subinfo =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074{
4075 .info = (SNDRV_PCM_INFO_MMAP |
4076 SNDRV_PCM_INFO_MMAP_VALID |
4077 SNDRV_PCM_INFO_NONINTERLEAVED |
4078 SNDRV_PCM_INFO_SYNC_START |
4079 SNDRV_PCM_INFO_DOUBLE),
4080#ifdef SNDRV_BIG_ENDIAN
4081 .formats = SNDRV_PCM_FMTBIT_S32_BE,
4082#else
4083 .formats = SNDRV_PCM_FMTBIT_S32_LE,
4084#endif
4085 .rates = (SNDRV_PCM_RATE_32000 |
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004086 SNDRV_PCM_RATE_44100 |
4087 SNDRV_PCM_RATE_48000 |
4088 SNDRV_PCM_RATE_64000 |
4089 SNDRV_PCM_RATE_88200 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090 SNDRV_PCM_RATE_96000),
4091 .rate_min = 32000,
4092 .rate_max = 96000,
4093 .channels_min = 14,
4094 .channels_max = HDSP_MAX_CHANNELS,
4095 .buffer_bytes_max = HDSP_CHANNEL_BUFFER_BYTES * HDSP_MAX_CHANNELS,
4096 .period_bytes_min = (64 * 4) * 10,
4097 .period_bytes_max = (8192 * 4) * HDSP_MAX_CHANNELS,
4098 .periods_min = 2,
4099 .periods_max = 2,
4100 .fifo_size = 0
4101};
4102
Takashi Iwai55e957d2005-11-17 14:52:13 +01004103static struct snd_pcm_hardware snd_hdsp_capture_subinfo =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104{
4105 .info = (SNDRV_PCM_INFO_MMAP |
4106 SNDRV_PCM_INFO_MMAP_VALID |
4107 SNDRV_PCM_INFO_NONINTERLEAVED |
4108 SNDRV_PCM_INFO_SYNC_START),
4109#ifdef SNDRV_BIG_ENDIAN
4110 .formats = SNDRV_PCM_FMTBIT_S32_BE,
4111#else
4112 .formats = SNDRV_PCM_FMTBIT_S32_LE,
4113#endif
4114 .rates = (SNDRV_PCM_RATE_32000 |
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004115 SNDRV_PCM_RATE_44100 |
4116 SNDRV_PCM_RATE_48000 |
4117 SNDRV_PCM_RATE_64000 |
4118 SNDRV_PCM_RATE_88200 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 SNDRV_PCM_RATE_96000),
4120 .rate_min = 32000,
4121 .rate_max = 96000,
4122 .channels_min = 14,
4123 .channels_max = HDSP_MAX_CHANNELS,
4124 .buffer_bytes_max = HDSP_CHANNEL_BUFFER_BYTES * HDSP_MAX_CHANNELS,
4125 .period_bytes_min = (64 * 4) * 10,
4126 .period_bytes_max = (8192 * 4) * HDSP_MAX_CHANNELS,
4127 .periods_min = 2,
4128 .periods_max = 2,
4129 .fifo_size = 0
4130};
4131
4132static unsigned int hdsp_period_sizes[] = { 64, 128, 256, 512, 1024, 2048, 4096, 8192 };
4133
Takashi Iwai55e957d2005-11-17 14:52:13 +01004134static struct snd_pcm_hw_constraint_list hdsp_hw_constraints_period_sizes = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 .count = ARRAY_SIZE(hdsp_period_sizes),
4136 .list = hdsp_period_sizes,
4137 .mask = 0
4138};
4139
4140static unsigned int hdsp_9632_sample_rates[] = { 32000, 44100, 48000, 64000, 88200, 96000, 128000, 176400, 192000 };
4141
Takashi Iwai55e957d2005-11-17 14:52:13 +01004142static struct snd_pcm_hw_constraint_list hdsp_hw_constraints_9632_sample_rates = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 .count = ARRAY_SIZE(hdsp_9632_sample_rates),
4144 .list = hdsp_9632_sample_rates,
4145 .mask = 0
4146};
4147
Takashi Iwai55e957d2005-11-17 14:52:13 +01004148static int snd_hdsp_hw_rule_in_channels(struct snd_pcm_hw_params *params,
4149 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004151 struct hdsp *hdsp = rule->private;
4152 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 if (hdsp->io_type == H9632) {
4154 unsigned int list[3];
4155 list[0] = hdsp->qs_in_channels;
4156 list[1] = hdsp->ds_in_channels;
4157 list[2] = hdsp->ss_in_channels;
4158 return snd_interval_list(c, 3, list, 0);
4159 } else {
4160 unsigned int list[2];
4161 list[0] = hdsp->ds_in_channels;
4162 list[1] = hdsp->ss_in_channels;
4163 return snd_interval_list(c, 2, list, 0);
4164 }
4165}
4166
Takashi Iwai55e957d2005-11-17 14:52:13 +01004167static int snd_hdsp_hw_rule_out_channels(struct snd_pcm_hw_params *params,
4168 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169{
4170 unsigned int list[3];
Takashi Iwai55e957d2005-11-17 14:52:13 +01004171 struct hdsp *hdsp = rule->private;
4172 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 if (hdsp->io_type == H9632) {
4174 list[0] = hdsp->qs_out_channels;
4175 list[1] = hdsp->ds_out_channels;
4176 list[2] = hdsp->ss_out_channels;
4177 return snd_interval_list(c, 3, list, 0);
4178 } else {
4179 list[0] = hdsp->ds_out_channels;
4180 list[1] = hdsp->ss_out_channels;
4181 }
4182 return snd_interval_list(c, 2, list, 0);
4183}
4184
Takashi Iwai55e957d2005-11-17 14:52:13 +01004185static int snd_hdsp_hw_rule_in_channels_rate(struct snd_pcm_hw_params *params,
4186 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004188 struct hdsp *hdsp = rule->private;
4189 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
4190 struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 if (r->min > 96000 && hdsp->io_type == H9632) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004192 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 .min = hdsp->qs_in_channels,
4194 .max = hdsp->qs_in_channels,
4195 .integer = 1,
4196 };
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004197 return snd_interval_refine(c, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 } else if (r->min > 48000 && r->max <= 96000) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004199 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200 .min = hdsp->ds_in_channels,
4201 .max = hdsp->ds_in_channels,
4202 .integer = 1,
4203 };
4204 return snd_interval_refine(c, &t);
4205 } else if (r->max < 64000) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004206 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 .min = hdsp->ss_in_channels,
4208 .max = hdsp->ss_in_channels,
4209 .integer = 1,
4210 };
4211 return snd_interval_refine(c, &t);
4212 }
4213 return 0;
4214}
4215
Takashi Iwai55e957d2005-11-17 14:52:13 +01004216static int snd_hdsp_hw_rule_out_channels_rate(struct snd_pcm_hw_params *params,
4217 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004219 struct hdsp *hdsp = rule->private;
4220 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
4221 struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222 if (r->min > 96000 && hdsp->io_type == H9632) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004223 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 .min = hdsp->qs_out_channels,
4225 .max = hdsp->qs_out_channels,
4226 .integer = 1,
4227 };
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004228 return snd_interval_refine(c, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 } else if (r->min > 48000 && r->max <= 96000) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004230 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 .min = hdsp->ds_out_channels,
4232 .max = hdsp->ds_out_channels,
4233 .integer = 1,
4234 };
4235 return snd_interval_refine(c, &t);
4236 } else if (r->max < 64000) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004237 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 .min = hdsp->ss_out_channels,
4239 .max = hdsp->ss_out_channels,
4240 .integer = 1,
4241 };
4242 return snd_interval_refine(c, &t);
4243 }
4244 return 0;
4245}
4246
Takashi Iwai55e957d2005-11-17 14:52:13 +01004247static int snd_hdsp_hw_rule_rate_out_channels(struct snd_pcm_hw_params *params,
4248 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004250 struct hdsp *hdsp = rule->private;
4251 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
4252 struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 if (c->min >= hdsp->ss_out_channels) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004254 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 .min = 32000,
4256 .max = 48000,
4257 .integer = 1,
4258 };
4259 return snd_interval_refine(r, &t);
4260 } else if (c->max <= hdsp->qs_out_channels && hdsp->io_type == H9632) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004261 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 .min = 128000,
4263 .max = 192000,
4264 .integer = 1,
4265 };
4266 return snd_interval_refine(r, &t);
4267 } else if (c->max <= hdsp->ds_out_channels) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004268 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 .min = 64000,
4270 .max = 96000,
4271 .integer = 1,
4272 };
4273 return snd_interval_refine(r, &t);
4274 }
4275 return 0;
4276}
4277
Takashi Iwai55e957d2005-11-17 14:52:13 +01004278static int snd_hdsp_hw_rule_rate_in_channels(struct snd_pcm_hw_params *params,
4279 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004281 struct hdsp *hdsp = rule->private;
4282 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
4283 struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 if (c->min >= hdsp->ss_in_channels) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004285 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 .min = 32000,
4287 .max = 48000,
4288 .integer = 1,
4289 };
4290 return snd_interval_refine(r, &t);
4291 } else if (c->max <= hdsp->qs_in_channels && hdsp->io_type == H9632) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004292 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 .min = 128000,
4294 .max = 192000,
4295 .integer = 1,
4296 };
4297 return snd_interval_refine(r, &t);
4298 } else if (c->max <= hdsp->ds_in_channels) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004299 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 .min = 64000,
4301 .max = 96000,
4302 .integer = 1,
4303 };
4304 return snd_interval_refine(r, &t);
4305 }
4306 return 0;
4307}
4308
Takashi Iwai55e957d2005-11-17 14:52:13 +01004309static int snd_hdsp_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004311 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
4312 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313
Takashi Iwaib0b98112005-10-20 18:29:58 +02004314 if (hdsp_check_for_iobox (hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316
Takashi Iwaib0b98112005-10-20 18:29:58 +02004317 if (hdsp_check_for_firmware(hdsp, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319
4320 spin_lock_irq(&hdsp->lock);
4321
4322 snd_pcm_set_sync(substream);
4323
4324 runtime->hw = snd_hdsp_playback_subinfo;
4325 runtime->dma_area = hdsp->playback_buffer;
4326 runtime->dma_bytes = HDSP_DMA_AREA_BYTES;
4327
4328 hdsp->playback_pid = current->pid;
4329 hdsp->playback_substream = substream;
4330
4331 spin_unlock_irq(&hdsp->lock);
4332
4333 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
4334 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 +02004335 if (hdsp->clock_source_locked) {
4336 runtime->hw.rate_min = runtime->hw.rate_max = hdsp->system_sample_rate;
4337 } else if (hdsp->io_type == H9632) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 runtime->hw.rate_max = 192000;
4339 runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
4340 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hdsp_hw_constraints_9632_sample_rates);
4341 }
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02004342 if (hdsp->io_type == H9632) {
4343 runtime->hw.channels_min = hdsp->qs_out_channels;
4344 runtime->hw.channels_max = hdsp->ss_out_channels;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004345 }
4346
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
4348 snd_hdsp_hw_rule_out_channels, hdsp,
4349 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
4350 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
4351 snd_hdsp_hw_rule_out_channels_rate, hdsp,
4352 SNDRV_PCM_HW_PARAM_RATE, -1);
4353 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
4354 snd_hdsp_hw_rule_rate_out_channels, hdsp,
4355 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
4356
4357 hdsp->creg_spdif_stream = hdsp->creg_spdif;
4358 hdsp->spdif_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
4359 snd_ctl_notify(hdsp->card, SNDRV_CTL_EVENT_MASK_VALUE |
4360 SNDRV_CTL_EVENT_MASK_INFO, &hdsp->spdif_ctl->id);
4361 return 0;
4362}
4363
Takashi Iwai55e957d2005-11-17 14:52:13 +01004364static int snd_hdsp_playback_release(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004366 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367
4368 spin_lock_irq(&hdsp->lock);
4369
4370 hdsp->playback_pid = -1;
4371 hdsp->playback_substream = NULL;
4372
4373 spin_unlock_irq(&hdsp->lock);
4374
4375 hdsp->spdif_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
4376 snd_ctl_notify(hdsp->card, SNDRV_CTL_EVENT_MASK_VALUE |
4377 SNDRV_CTL_EVENT_MASK_INFO, &hdsp->spdif_ctl->id);
4378 return 0;
4379}
4380
4381
Takashi Iwai55e957d2005-11-17 14:52:13 +01004382static int snd_hdsp_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004384 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
4385 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386
Takashi Iwaib0b98112005-10-20 18:29:58 +02004387 if (hdsp_check_for_iobox (hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389
Takashi Iwaib0b98112005-10-20 18:29:58 +02004390 if (hdsp_check_for_firmware(hdsp, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392
4393 spin_lock_irq(&hdsp->lock);
4394
4395 snd_pcm_set_sync(substream);
4396
4397 runtime->hw = snd_hdsp_capture_subinfo;
4398 runtime->dma_area = hdsp->capture_buffer;
4399 runtime->dma_bytes = HDSP_DMA_AREA_BYTES;
4400
4401 hdsp->capture_pid = current->pid;
4402 hdsp->capture_substream = substream;
4403
4404 spin_unlock_irq(&hdsp->lock);
4405
4406 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
4407 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, &hdsp_hw_constraints_period_sizes);
4408 if (hdsp->io_type == H9632) {
4409 runtime->hw.channels_min = hdsp->qs_in_channels;
4410 runtime->hw.channels_max = hdsp->ss_in_channels;
4411 runtime->hw.rate_max = 192000;
4412 runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
4413 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hdsp_hw_constraints_9632_sample_rates);
4414 }
4415 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
4416 snd_hdsp_hw_rule_in_channels, hdsp,
4417 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
4418 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
4419 snd_hdsp_hw_rule_in_channels_rate, hdsp,
4420 SNDRV_PCM_HW_PARAM_RATE, -1);
4421 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
4422 snd_hdsp_hw_rule_rate_in_channels, hdsp,
4423 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
4424 return 0;
4425}
4426
Takashi Iwai55e957d2005-11-17 14:52:13 +01004427static int snd_hdsp_capture_release(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004429 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430
4431 spin_lock_irq(&hdsp->lock);
4432
4433 hdsp->capture_pid = -1;
4434 hdsp->capture_substream = NULL;
4435
4436 spin_unlock_irq(&hdsp->lock);
4437 return 0;
4438}
4439
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440/* helper functions for copying meter values */
4441static inline int copy_u32_le(void __user *dest, void __iomem *src)
4442{
4443 u32 val = readl(src);
4444 return copy_to_user(dest, &val, 4);
4445}
4446
4447static inline int copy_u64_le(void __user *dest, void __iomem *src_low, void __iomem *src_high)
4448{
4449 u32 rms_low, rms_high;
4450 u64 rms;
4451 rms_low = readl(src_low);
4452 rms_high = readl(src_high);
4453 rms = ((u64)rms_high << 32) | rms_low;
4454 return copy_to_user(dest, &rms, 8);
4455}
4456
4457static inline int copy_u48_le(void __user *dest, void __iomem *src_low, void __iomem *src_high)
4458{
4459 u32 rms_low, rms_high;
4460 u64 rms;
4461 rms_low = readl(src_low) & 0xffffff00;
4462 rms_high = readl(src_high) & 0xffffff00;
4463 rms = ((u64)rms_high << 32) | rms_low;
4464 return copy_to_user(dest, &rms, 8);
4465}
4466
Takashi Iwai55e957d2005-11-17 14:52:13 +01004467static int hdsp_9652_get_peak(struct hdsp *hdsp, struct hdsp_peak_rms __user *peak_rms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468{
4469 int doublespeed = 0;
4470 int i, j, channels, ofs;
4471
4472 if (hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DoubleSpeedStatus)
4473 doublespeed = 1;
4474 channels = doublespeed ? 14 : 26;
4475 for (i = 0, j = 0; i < 26; ++i) {
4476 if (doublespeed && (i & 4))
4477 continue;
4478 ofs = HDSP_9652_peakBase - j * 4;
4479 if (copy_u32_le(&peak_rms->input_peaks[i], hdsp->iobase + ofs))
4480 return -EFAULT;
4481 ofs -= channels * 4;
4482 if (copy_u32_le(&peak_rms->playback_peaks[i], hdsp->iobase + ofs))
4483 return -EFAULT;
4484 ofs -= channels * 4;
4485 if (copy_u32_le(&peak_rms->output_peaks[i], hdsp->iobase + ofs))
4486 return -EFAULT;
4487 ofs = HDSP_9652_rmsBase + j * 8;
4488 if (copy_u48_le(&peak_rms->input_rms[i], hdsp->iobase + ofs,
4489 hdsp->iobase + ofs + 4))
4490 return -EFAULT;
4491 ofs += channels * 8;
4492 if (copy_u48_le(&peak_rms->playback_rms[i], hdsp->iobase + ofs,
4493 hdsp->iobase + ofs + 4))
4494 return -EFAULT;
4495 ofs += channels * 8;
4496 if (copy_u48_le(&peak_rms->output_rms[i], hdsp->iobase + ofs,
4497 hdsp->iobase + ofs + 4))
4498 return -EFAULT;
4499 j++;
4500 }
4501 return 0;
4502}
4503
Takashi Iwai55e957d2005-11-17 14:52:13 +01004504static int hdsp_9632_get_peak(struct hdsp *hdsp, struct hdsp_peak_rms __user *peak_rms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505{
4506 int i, j;
Takashi Iwai55e957d2005-11-17 14:52:13 +01004507 struct hdsp_9632_meters __iomem *m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 int doublespeed = 0;
4509
4510 if (hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DoubleSpeedStatus)
4511 doublespeed = 1;
Takashi Iwai55e957d2005-11-17 14:52:13 +01004512 m = (struct hdsp_9632_meters __iomem *)(hdsp->iobase+HDSP_9632_metersBase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 for (i = 0, j = 0; i < 16; ++i, ++j) {
4514 if (copy_u32_le(&peak_rms->input_peaks[i], &m->input_peak[j]))
4515 return -EFAULT;
4516 if (copy_u32_le(&peak_rms->playback_peaks[i], &m->playback_peak[j]))
4517 return -EFAULT;
4518 if (copy_u32_le(&peak_rms->output_peaks[i], &m->output_peak[j]))
4519 return -EFAULT;
4520 if (copy_u64_le(&peak_rms->input_rms[i], &m->input_rms_low[j],
4521 &m->input_rms_high[j]))
4522 return -EFAULT;
4523 if (copy_u64_le(&peak_rms->playback_rms[i], &m->playback_rms_low[j],
4524 &m->playback_rms_high[j]))
4525 return -EFAULT;
4526 if (copy_u64_le(&peak_rms->output_rms[i], &m->output_rms_low[j],
4527 &m->output_rms_high[j]))
4528 return -EFAULT;
4529 if (doublespeed && i == 3) i += 4;
4530 }
4531 return 0;
4532}
4533
Takashi Iwai55e957d2005-11-17 14:52:13 +01004534static int hdsp_get_peak(struct hdsp *hdsp, struct hdsp_peak_rms __user *peak_rms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535{
4536 int i;
4537
4538 for (i = 0; i < 26; i++) {
4539 if (copy_u32_le(&peak_rms->playback_peaks[i],
4540 hdsp->iobase + HDSP_playbackPeakLevel + i * 4))
4541 return -EFAULT;
4542 if (copy_u32_le(&peak_rms->input_peaks[i],
4543 hdsp->iobase + HDSP_inputPeakLevel + i * 4))
4544 return -EFAULT;
4545 }
4546 for (i = 0; i < 28; i++) {
4547 if (copy_u32_le(&peak_rms->output_peaks[i],
4548 hdsp->iobase + HDSP_outputPeakLevel + i * 4))
4549 return -EFAULT;
4550 }
4551 for (i = 0; i < 26; ++i) {
4552 if (copy_u64_le(&peak_rms->playback_rms[i],
4553 hdsp->iobase + HDSP_playbackRmsLevel + i * 8 + 4,
4554 hdsp->iobase + HDSP_playbackRmsLevel + i * 8))
4555 return -EFAULT;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004556 if (copy_u64_le(&peak_rms->input_rms[i],
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 hdsp->iobase + HDSP_inputRmsLevel + i * 8 + 4,
4558 hdsp->iobase + HDSP_inputRmsLevel + i * 8))
4559 return -EFAULT;
4560 }
4561 return 0;
4562}
4563
Takashi Iwai55e957d2005-11-17 14:52:13 +01004564static 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 -07004565{
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004566 struct hdsp *hdsp = (struct hdsp *)hw->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 void __user *argp = (void __user *)arg;
Tim Blechmann3ae7e2e2008-11-08 14:42:18 +01004568 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569
4570 switch (cmd) {
4571 case SNDRV_HDSP_IOCTL_GET_PEAK_RMS: {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004572 struct hdsp_peak_rms __user *peak_rms = (struct hdsp_peak_rms __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573
Tim Blechmann3ae7e2e2008-11-08 14:42:18 +01004574 err = hdsp_check_for_iobox(hdsp);
4575 if (err < 0)
4576 return err;
4577
4578 err = hdsp_check_for_firmware(hdsp, 1);
4579 if (err < 0)
4580 return err;
4581
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 if (!(hdsp->state & HDSP_FirmwareLoaded)) {
4583 snd_printk(KERN_ERR "Hammerfall-DSP: firmware needs to be uploaded to the card.\n");
4584 return -EINVAL;
4585 }
4586
4587 switch (hdsp->io_type) {
4588 case H9652:
4589 return hdsp_9652_get_peak(hdsp, peak_rms);
4590 case H9632:
4591 return hdsp_9632_get_peak(hdsp, peak_rms);
4592 default:
4593 return hdsp_get_peak(hdsp, peak_rms);
4594 }
4595 }
4596 case SNDRV_HDSP_IOCTL_GET_CONFIG_INFO: {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004597 struct hdsp_config_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598 unsigned long flags;
4599 int i;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004600
Tim Blechmann3ae7e2e2008-11-08 14:42:18 +01004601 err = hdsp_check_for_iobox(hdsp);
4602 if (err < 0)
4603 return err;
4604
4605 err = hdsp_check_for_firmware(hdsp, 1);
4606 if (err < 0)
4607 return err;
4608
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 spin_lock_irqsave(&hdsp->lock, flags);
4610 info.pref_sync_ref = (unsigned char)hdsp_pref_sync_ref(hdsp);
4611 info.wordclock_sync_check = (unsigned char)hdsp_wc_sync_check(hdsp);
Takashi Iwaib0b98112005-10-20 18:29:58 +02004612 if (hdsp->io_type != H9632)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 info.adatsync_sync_check = (unsigned char)hdsp_adatsync_sync_check(hdsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614 info.spdif_sync_check = (unsigned char)hdsp_spdif_sync_check(hdsp);
Takashi Iwaib0b98112005-10-20 18:29:58 +02004615 for (i = 0; i < ((hdsp->io_type != Multiface && hdsp->io_type != H9632) ? 3 : 1); ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616 info.adat_sync_check[i] = (unsigned char)hdsp_adat_sync_check(hdsp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 info.spdif_in = (unsigned char)hdsp_spdif_in(hdsp);
4618 info.spdif_out = (unsigned char)hdsp_spdif_out(hdsp);
4619 info.spdif_professional = (unsigned char)hdsp_spdif_professional(hdsp);
4620 info.spdif_emphasis = (unsigned char)hdsp_spdif_emphasis(hdsp);
4621 info.spdif_nonaudio = (unsigned char)hdsp_spdif_nonaudio(hdsp);
4622 info.spdif_sample_rate = hdsp_spdif_sample_rate(hdsp);
4623 info.system_sample_rate = hdsp->system_sample_rate;
4624 info.autosync_sample_rate = hdsp_external_sample_rate(hdsp);
4625 info.system_clock_mode = (unsigned char)hdsp_system_clock_mode(hdsp);
4626 info.clock_source = (unsigned char)hdsp_clock_source(hdsp);
4627 info.autosync_ref = (unsigned char)hdsp_autosync_ref(hdsp);
4628 info.line_out = (unsigned char)hdsp_line_out(hdsp);
4629 if (hdsp->io_type == H9632) {
4630 info.da_gain = (unsigned char)hdsp_da_gain(hdsp);
4631 info.ad_gain = (unsigned char)hdsp_ad_gain(hdsp);
4632 info.phone_gain = (unsigned char)hdsp_phone_gain(hdsp);
4633 info.xlr_breakout_cable = (unsigned char)hdsp_xlr_breakout_cable(hdsp);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004634
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635 }
Takashi Iwaib0b98112005-10-20 18:29:58 +02004636 if (hdsp->io_type == H9632 || hdsp->io_type == H9652)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637 info.analog_extension_board = (unsigned char)hdsp_aeb(hdsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638 spin_unlock_irqrestore(&hdsp->lock, flags);
4639 if (copy_to_user(argp, &info, sizeof(info)))
4640 return -EFAULT;
4641 break;
4642 }
4643 case SNDRV_HDSP_IOCTL_GET_9632_AEB: {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004644 struct hdsp_9632_aeb h9632_aeb;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004645
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 if (hdsp->io_type != H9632) return -EINVAL;
4647 h9632_aeb.aebi = hdsp->ss_in_channels - H9632_SS_CHANNELS;
4648 h9632_aeb.aebo = hdsp->ss_out_channels - H9632_SS_CHANNELS;
4649 if (copy_to_user(argp, &h9632_aeb, sizeof(h9632_aeb)))
4650 return -EFAULT;
4651 break;
4652 }
4653 case SNDRV_HDSP_IOCTL_GET_VERSION: {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004654 struct hdsp_version hdsp_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 int err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004656
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657 if (hdsp->io_type == H9652 || hdsp->io_type == H9632) return -EINVAL;
4658 if (hdsp->io_type == Undefined) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02004659 if ((err = hdsp_get_iobox_version(hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 }
4662 hdsp_version.io_type = hdsp->io_type;
4663 hdsp_version.firmware_rev = hdsp->firmware_rev;
Takashi Iwaib0b98112005-10-20 18:29:58 +02004664 if ((err = copy_to_user(argp, &hdsp_version, sizeof(hdsp_version))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 break;
4667 }
4668 case SNDRV_HDSP_IOCTL_UPLOAD_FIRMWARE: {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004669 struct hdsp_firmware __user *firmware;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670 u32 __user *firmware_data;
4671 int err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004672
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 if (hdsp->io_type == H9652 || hdsp->io_type == H9632) return -EINVAL;
4674 /* SNDRV_HDSP_IOCTL_GET_VERSION must have been called */
4675 if (hdsp->io_type == Undefined) return -EINVAL;
4676
4677 if (hdsp->state & (HDSP_FirmwareCached | HDSP_FirmwareLoaded))
4678 return -EBUSY;
4679
Takashi Iwaib0b98112005-10-20 18:29:58 +02004680 snd_printk(KERN_INFO "Hammerfall-DSP: initializing firmware upload\n");
Takashi Iwai55e957d2005-11-17 14:52:13 +01004681 firmware = (struct hdsp_firmware __user *)argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682
Takashi Iwaib0b98112005-10-20 18:29:58 +02004683 if (get_user(firmware_data, &firmware->firmware_data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 return -EFAULT;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004685
Takashi Iwaib0b98112005-10-20 18:29:58 +02004686 if (hdsp_check_for_iobox (hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688
Takashi Iwaib0b98112005-10-20 18:29:58 +02004689 if (copy_from_user(hdsp->firmware_cache, firmware_data, sizeof(hdsp->firmware_cache)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690 return -EFAULT;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004691
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 hdsp->state |= HDSP_FirmwareCached;
4693
Takashi Iwaib0b98112005-10-20 18:29:58 +02004694 if ((err = snd_hdsp_load_firmware_from_cache(hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 return err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004696
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 if (!(hdsp->state & HDSP_InitializationComplete)) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02004698 if ((err = snd_hdsp_enable_io(hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 return err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004700
4701 snd_hdsp_initialize_channels(hdsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702 snd_hdsp_initialize_midi_flush(hdsp);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004703
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704 if ((err = snd_hdsp_create_alsa_devices(hdsp->card, hdsp)) < 0) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02004705 snd_printk(KERN_ERR "Hammerfall-DSP: error creating alsa devices\n");
4706 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 }
4708 }
4709 break;
4710 }
4711 case SNDRV_HDSP_IOCTL_GET_MIXER: {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004712 struct hdsp_mixer __user *mixer = (struct hdsp_mixer __user *)argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713 if (copy_to_user(mixer->matrix, hdsp->mixer_matrix, sizeof(unsigned short)*HDSP_MATRIX_MIXER_SIZE))
4714 return -EFAULT;
4715 break;
4716 }
4717 default:
4718 return -EINVAL;
4719 }
4720 return 0;
4721}
4722
Takashi Iwai55e957d2005-11-17 14:52:13 +01004723static struct snd_pcm_ops snd_hdsp_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 .open = snd_hdsp_playback_open,
4725 .close = snd_hdsp_playback_release,
4726 .ioctl = snd_hdsp_ioctl,
4727 .hw_params = snd_hdsp_hw_params,
4728 .prepare = snd_hdsp_prepare,
4729 .trigger = snd_hdsp_trigger,
4730 .pointer = snd_hdsp_hw_pointer,
4731 .copy = snd_hdsp_playback_copy,
4732 .silence = snd_hdsp_hw_silence,
4733};
4734
Takashi Iwai55e957d2005-11-17 14:52:13 +01004735static struct snd_pcm_ops snd_hdsp_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 .open = snd_hdsp_capture_open,
4737 .close = snd_hdsp_capture_release,
4738 .ioctl = snd_hdsp_ioctl,
4739 .hw_params = snd_hdsp_hw_params,
4740 .prepare = snd_hdsp_prepare,
4741 .trigger = snd_hdsp_trigger,
4742 .pointer = snd_hdsp_hw_pointer,
4743 .copy = snd_hdsp_capture_copy,
4744};
4745
Takashi Iwai92eed662008-02-22 18:35:56 +01004746static int snd_hdsp_create_hwdep(struct snd_card *card, struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004748 struct snd_hwdep *hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749 int err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004750
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751 if ((err = snd_hwdep_new(card, "HDSP hwdep", 0, &hw)) < 0)
4752 return err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004753
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754 hdsp->hwdep = hw;
4755 hw->private_data = hdsp;
4756 strcpy(hw->name, "HDSP hwdep interface");
4757
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758 hw->ops.ioctl = snd_hdsp_hwdep_ioctl;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004759
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 return 0;
4761}
4762
Takashi Iwai55e957d2005-11-17 14:52:13 +01004763static int snd_hdsp_create_pcm(struct snd_card *card, struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004765 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 int err;
4767
4768 if ((err = snd_pcm_new(card, hdsp->card_name, 0, 1, 1, &pcm)) < 0)
4769 return err;
4770
4771 hdsp->pcm = pcm;
4772 pcm->private_data = hdsp;
4773 strcpy(pcm->name, hdsp->card_name);
4774
4775 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_hdsp_playback_ops);
4776 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_hdsp_capture_ops);
4777
4778 pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
4779
4780 return 0;
4781}
4782
Takashi Iwai55e957d2005-11-17 14:52:13 +01004783static void snd_hdsp_9652_enable_mixer (struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784{
4785 hdsp->control2_register |= HDSP_9652_ENABLE_MIXER;
4786 hdsp_write (hdsp, HDSP_control2Reg, hdsp->control2_register);
4787}
4788
Takashi Iwai55e957d2005-11-17 14:52:13 +01004789static int snd_hdsp_enable_io (struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790{
4791 int i;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004792
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793 if (hdsp_fifo_wait (hdsp, 0, 100)) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02004794 snd_printk(KERN_ERR "Hammerfall-DSP: enable_io fifo_wait failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795 return -EIO;
4796 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004797
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 for (i = 0; i < hdsp->max_channels; ++i) {
4799 hdsp_write (hdsp, HDSP_inputEnable + (4 * i), 1);
4800 hdsp_write (hdsp, HDSP_outputEnable + (4 * i), 1);
4801 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004802
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 return 0;
4804}
4805
Takashi Iwai55e957d2005-11-17 14:52:13 +01004806static void snd_hdsp_initialize_channels(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807{
4808 int status, aebi_channels, aebo_channels;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004809
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810 switch (hdsp->io_type) {
4811 case Digiface:
4812 hdsp->card_name = "RME Hammerfall DSP + Digiface";
4813 hdsp->ss_in_channels = hdsp->ss_out_channels = DIGIFACE_SS_CHANNELS;
4814 hdsp->ds_in_channels = hdsp->ds_out_channels = DIGIFACE_DS_CHANNELS;
4815 break;
4816
4817 case H9652:
4818 hdsp->card_name = "RME Hammerfall HDSP 9652";
4819 hdsp->ss_in_channels = hdsp->ss_out_channels = H9652_SS_CHANNELS;
4820 hdsp->ds_in_channels = hdsp->ds_out_channels = H9652_DS_CHANNELS;
4821 break;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004822
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823 case H9632:
4824 status = hdsp_read(hdsp, HDSP_statusRegister);
4825 /* HDSP_AEBx bits are low when AEB are connected */
4826 aebi_channels = (status & HDSP_AEBI) ? 0 : 4;
4827 aebo_channels = (status & HDSP_AEBO) ? 0 : 4;
4828 hdsp->card_name = "RME Hammerfall HDSP 9632";
4829 hdsp->ss_in_channels = H9632_SS_CHANNELS+aebi_channels;
4830 hdsp->ds_in_channels = H9632_DS_CHANNELS+aebi_channels;
4831 hdsp->qs_in_channels = H9632_QS_CHANNELS+aebi_channels;
4832 hdsp->ss_out_channels = H9632_SS_CHANNELS+aebo_channels;
4833 hdsp->ds_out_channels = H9632_DS_CHANNELS+aebo_channels;
4834 hdsp->qs_out_channels = H9632_QS_CHANNELS+aebo_channels;
4835 break;
4836
4837 case Multiface:
4838 hdsp->card_name = "RME Hammerfall DSP + Multiface";
4839 hdsp->ss_in_channels = hdsp->ss_out_channels = MULTIFACE_SS_CHANNELS;
4840 hdsp->ds_in_channels = hdsp->ds_out_channels = MULTIFACE_DS_CHANNELS;
4841 break;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004842
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843 default:
4844 /* should never get here */
4845 break;
4846 }
4847}
4848
Takashi Iwai55e957d2005-11-17 14:52:13 +01004849static void snd_hdsp_initialize_midi_flush (struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850{
4851 snd_hdsp_flush_midi_input (hdsp, 0);
4852 snd_hdsp_flush_midi_input (hdsp, 1);
4853}
4854
Takashi Iwai55e957d2005-11-17 14:52:13 +01004855static int snd_hdsp_create_alsa_devices(struct snd_card *card, struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856{
4857 int err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004858
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859 if ((err = snd_hdsp_create_pcm(card, hdsp)) < 0) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02004860 snd_printk(KERN_ERR "Hammerfall-DSP: Error creating pcm interface\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861 return err;
4862 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004863
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864
4865 if ((err = snd_hdsp_create_midi(card, hdsp, 0)) < 0) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02004866 snd_printk(KERN_ERR "Hammerfall-DSP: Error creating first midi interface\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 return err;
4868 }
4869
4870 if (hdsp->io_type == Digiface || hdsp->io_type == H9652) {
4871 if ((err = snd_hdsp_create_midi(card, hdsp, 1)) < 0) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02004872 snd_printk(KERN_ERR "Hammerfall-DSP: Error creating second midi interface\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873 return err;
4874 }
4875 }
4876
4877 if ((err = snd_hdsp_create_controls(card, hdsp)) < 0) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02004878 snd_printk(KERN_ERR "Hammerfall-DSP: Error creating ctl interface\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879 return err;
4880 }
4881
4882 snd_hdsp_proc_init(hdsp);
4883
4884 hdsp->system_sample_rate = -1;
4885 hdsp->playback_pid = -1;
4886 hdsp->capture_pid = -1;
4887 hdsp->capture_substream = NULL;
4888 hdsp->playback_substream = NULL;
4889
4890 if ((err = snd_hdsp_set_defaults(hdsp)) < 0) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02004891 snd_printk(KERN_ERR "Hammerfall-DSP: Error setting default values\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892 return err;
4893 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004894
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895 if (!(hdsp->state & HDSP_InitializationComplete)) {
Clemens Ladischb73c1c12005-09-02 08:49:21 +02004896 strcpy(card->shortname, "Hammerfall DSP");
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004897 sprintf(card->longname, "%s at 0x%lx, irq %d", hdsp->card_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898 hdsp->port, hdsp->irq);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004899
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900 if ((err = snd_card_register(card)) < 0) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02004901 snd_printk(KERN_ERR "Hammerfall-DSP: error registering card\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902 return err;
4903 }
4904 hdsp->state |= HDSP_InitializationComplete;
4905 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004906
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 return 0;
4908}
4909
4910#ifdef HDSP_FW_LOADER
4911/* load firmware via hotplug fw loader */
Takashi Iwai92eed662008-02-22 18:35:56 +01004912static int hdsp_request_fw_loader(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913{
4914 const char *fwfile;
4915 const struct firmware *fw;
4916 int err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004917
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918 if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
4919 return 0;
4920 if (hdsp->io_type == Undefined) {
4921 if ((err = hdsp_get_iobox_version(hdsp)) < 0)
4922 return err;
4923 if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
4924 return 0;
4925 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004926
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927 /* caution: max length of firmware filename is 30! */
4928 switch (hdsp->io_type) {
4929 case Multiface:
4930 if (hdsp->firmware_rev == 0xa)
4931 fwfile = "multiface_firmware.bin";
4932 else
4933 fwfile = "multiface_firmware_rev11.bin";
4934 break;
4935 case Digiface:
4936 if (hdsp->firmware_rev == 0xa)
4937 fwfile = "digiface_firmware.bin";
4938 else
4939 fwfile = "digiface_firmware_rev11.bin";
4940 break;
4941 default:
4942 snd_printk(KERN_ERR "Hammerfall-DSP: invalid io_type %d\n", hdsp->io_type);
4943 return -EINVAL;
4944 }
4945
4946 if (request_firmware(&fw, fwfile, &hdsp->pci->dev)) {
4947 snd_printk(KERN_ERR "Hammerfall-DSP: cannot load firmware %s\n", fwfile);
4948 return -ENOENT;
4949 }
4950 if (fw->size < sizeof(hdsp->firmware_cache)) {
4951 snd_printk(KERN_ERR "Hammerfall-DSP: too short firmware size %d (expected %d)\n",
4952 (int)fw->size, (int)sizeof(hdsp->firmware_cache));
4953 release_firmware(fw);
4954 return -EINVAL;
4955 }
Thomas Charbonnel7679a032005-04-25 11:35:29 +02004956
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957 memcpy(hdsp->firmware_cache, fw->data, sizeof(hdsp->firmware_cache));
Thomas Charbonnel7679a032005-04-25 11:35:29 +02004958
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 release_firmware(fw);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004960
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 hdsp->state |= HDSP_FirmwareCached;
4962
4963 if ((err = snd_hdsp_load_firmware_from_cache(hdsp)) < 0)
4964 return err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004965
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966 if (!(hdsp->state & HDSP_InitializationComplete)) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02004967 if ((err = snd_hdsp_enable_io(hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969
4970 if ((err = snd_hdsp_create_hwdep(hdsp->card, hdsp)) < 0) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02004971 snd_printk(KERN_ERR "Hammerfall-DSP: error creating hwdep device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972 return err;
4973 }
4974 snd_hdsp_initialize_channels(hdsp);
4975 snd_hdsp_initialize_midi_flush(hdsp);
4976 if ((err = snd_hdsp_create_alsa_devices(hdsp->card, hdsp)) < 0) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02004977 snd_printk(KERN_ERR "Hammerfall-DSP: error creating alsa devices\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978 return err;
4979 }
4980 }
4981 return 0;
4982}
4983#endif
4984
Takashi Iwai55e957d2005-11-17 14:52:13 +01004985static int __devinit snd_hdsp_create(struct snd_card *card,
4986 struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987{
4988 struct pci_dev *pci = hdsp->pci;
4989 int err;
4990 int is_9652 = 0;
4991 int is_9632 = 0;
4992
4993 hdsp->irq = -1;
4994 hdsp->state = 0;
4995 hdsp->midi[0].rmidi = NULL;
4996 hdsp->midi[1].rmidi = NULL;
4997 hdsp->midi[0].input = NULL;
4998 hdsp->midi[1].input = NULL;
4999 hdsp->midi[0].output = NULL;
5000 hdsp->midi[1].output = NULL;
5001 hdsp->midi[0].pending = 0;
5002 hdsp->midi[1].pending = 0;
5003 spin_lock_init(&hdsp->midi[0].lock);
5004 spin_lock_init(&hdsp->midi[1].lock);
5005 hdsp->iobase = NULL;
5006 hdsp->control_register = 0;
5007 hdsp->control2_register = 0;
5008 hdsp->io_type = Undefined;
5009 hdsp->max_channels = 26;
5010
5011 hdsp->card = card;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005012
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013 spin_lock_init(&hdsp->lock);
5014
5015 tasklet_init(&hdsp->midi_tasklet, hdsp_midi_tasklet, (unsigned long)hdsp);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005016
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017 pci_read_config_word(hdsp->pci, PCI_CLASS_REVISION, &hdsp->firmware_rev);
5018 hdsp->firmware_rev &= 0xff;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005019
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020 /* From Martin Bjoernsen :
5021 "It is important that the card's latency timer register in
5022 the PCI configuration space is set to a value much larger
5023 than 0 by the computer's BIOS or the driver.
5024 The windows driver always sets this 8 bit register [...]
5025 to its maximum 255 to avoid problems with some computers."
5026 */
5027 pci_write_config_byte(hdsp->pci, PCI_LATENCY_TIMER, 0xFF);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005028
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029 strcpy(card->driver, "H-DSP");
5030 strcpy(card->mixername, "Xilinx FPGA");
5031
Takashi Iwaib0b98112005-10-20 18:29:58 +02005032 if (hdsp->firmware_rev < 0xa)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 return -ENODEV;
Takashi Iwaib0b98112005-10-20 18:29:58 +02005034 else if (hdsp->firmware_rev < 0x64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035 hdsp->card_name = "RME Hammerfall DSP";
Takashi Iwaib0b98112005-10-20 18:29:58 +02005036 else if (hdsp->firmware_rev < 0x96) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037 hdsp->card_name = "RME HDSP 9652";
5038 is_9652 = 1;
5039 } else {
5040 hdsp->card_name = "RME HDSP 9632";
5041 hdsp->max_channels = 16;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005042 is_9632 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 }
5044
Takashi Iwaib0b98112005-10-20 18:29:58 +02005045 if ((err = pci_enable_device(pci)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047
5048 pci_set_master(hdsp->pci);
5049
5050 if ((err = pci_request_regions(pci, "hdsp")) < 0)
5051 return err;
5052 hdsp->port = pci_resource_start(pci, 0);
5053 if ((hdsp->iobase = ioremap_nocache(hdsp->port, HDSP_IO_EXTENT)) == NULL) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02005054 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 -07005055 return -EBUSY;
5056 }
5057
Takashi Iwai437a5a42006-11-21 12:14:23 +01005058 if (request_irq(pci->irq, snd_hdsp_interrupt, IRQF_SHARED,
5059 "hdsp", hdsp)) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02005060 snd_printk(KERN_ERR "Hammerfall-DSP: unable to use IRQ %d\n", pci->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061 return -EBUSY;
5062 }
5063
5064 hdsp->irq = pci->irq;
Remy Bruno176546a2006-10-16 12:32:53 +02005065 hdsp->precise_ptr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066 hdsp->use_midi_tasklet = 1;
Remy Brunod7923b22006-10-17 12:41:56 +02005067 hdsp->dds_value = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068
Takashi Iwaib0b98112005-10-20 18:29:58 +02005069 if ((err = snd_hdsp_initialize_memory(hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070 return err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005071
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072 if (!is_9652 && !is_9632) {
Tim Blechmanne588ed82009-02-20 19:30:35 +01005073 /* we wait a maximum of 10 seconds to let freshly
5074 * inserted cardbus cards do their hardware init */
5075 err = hdsp_wait_for_iobox(hdsp, 1000, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076
Tim Blechmann00c9ddd2008-11-09 12:50:52 +01005077 if (err < 0)
5078 return err;
5079
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080 if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
5081#ifdef HDSP_FW_LOADER
Takashi Iwaib0b98112005-10-20 18:29:58 +02005082 if ((err = hdsp_request_fw_loader(hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083 /* we don't fail as this can happen
5084 if userspace is not ready for
5085 firmware upload
5086 */
Takashi Iwaib0b98112005-10-20 18:29:58 +02005087 snd_printk(KERN_ERR "Hammerfall-DSP: couldn't get firmware from userspace. try using hdsploader\n");
5088 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089 /* init is complete, we return */
5090 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091#endif
Tim Blechmann00c9ddd2008-11-09 12:50:52 +01005092 /* we defer initialization */
Takashi Iwaib0b98112005-10-20 18:29:58 +02005093 snd_printk(KERN_INFO "Hammerfall-DSP: card initialization pending : waiting for firmware\n");
5094 if ((err = snd_hdsp_create_hwdep(card, hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096 return 0;
5097 } else {
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005098 snd_printk(KERN_INFO "Hammerfall-DSP: Firmware already present, initializing card.\n");
Takashi Iwaib0b98112005-10-20 18:29:58 +02005099 if (hdsp_read(hdsp, HDSP_status2Register) & HDSP_version1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100 hdsp->io_type = Multiface;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005101 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102 hdsp->io_type = Digiface;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103 }
5104 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005105
Takashi Iwaib0b98112005-10-20 18:29:58 +02005106 if ((err = snd_hdsp_enable_io(hdsp)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107 return err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005108
Takashi Iwaib0b98112005-10-20 18:29:58 +02005109 if (is_9652)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110 hdsp->io_type = H9652;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005111
Takashi Iwaib0b98112005-10-20 18:29:58 +02005112 if (is_9632)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113 hdsp->io_type = H9632;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114
Takashi Iwaib0b98112005-10-20 18:29:58 +02005115 if ((err = snd_hdsp_create_hwdep(card, hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 return err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005117
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118 snd_hdsp_initialize_channels(hdsp);
5119 snd_hdsp_initialize_midi_flush(hdsp);
5120
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005121 hdsp->state |= HDSP_FirmwareLoaded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122
Takashi Iwaib0b98112005-10-20 18:29:58 +02005123 if ((err = snd_hdsp_create_alsa_devices(card, hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005126 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127}
5128
Takashi Iwai55e957d2005-11-17 14:52:13 +01005129static int snd_hdsp_free(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130{
5131 if (hdsp->port) {
5132 /* stop the audio, and cancel all interrupts */
5133 tasklet_kill(&hdsp->midi_tasklet);
5134 hdsp->control_register &= ~(HDSP_Start|HDSP_AudioInterruptEnable|HDSP_Midi0InterruptEnable|HDSP_Midi1InterruptEnable);
5135 hdsp_write (hdsp, HDSP_controlRegister, hdsp->control_register);
5136 }
5137
5138 if (hdsp->irq >= 0)
5139 free_irq(hdsp->irq, (void *)hdsp);
5140
5141 snd_hdsp_free_buffers(hdsp);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005142
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143 if (hdsp->iobase)
5144 iounmap(hdsp->iobase);
5145
5146 if (hdsp->port)
5147 pci_release_regions(hdsp->pci);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005148
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149 pci_disable_device(hdsp->pci);
5150 return 0;
5151}
5152
Takashi Iwai55e957d2005-11-17 14:52:13 +01005153static void snd_hdsp_card_free(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154{
Takashi Iwai55e957d2005-11-17 14:52:13 +01005155 struct hdsp *hdsp = (struct hdsp *) card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156
5157 if (hdsp)
5158 snd_hdsp_free(hdsp);
5159}
5160
5161static int __devinit snd_hdsp_probe(struct pci_dev *pci,
5162 const struct pci_device_id *pci_id)
5163{
5164 static int dev;
Takashi Iwai55e957d2005-11-17 14:52:13 +01005165 struct hdsp *hdsp;
5166 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167 int err;
5168
5169 if (dev >= SNDRV_CARDS)
5170 return -ENODEV;
5171 if (!enable[dev]) {
5172 dev++;
5173 return -ENOENT;
5174 }
5175
Takashi Iwaie58de7b2008-12-28 16:44:30 +01005176 err = snd_card_create(index[dev], id[dev], THIS_MODULE,
5177 sizeof(struct hdsp), &card);
5178 if (err < 0)
5179 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180
Takashi Iwai55e957d2005-11-17 14:52:13 +01005181 hdsp = (struct hdsp *) card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182 card->private_free = snd_hdsp_card_free;
5183 hdsp->dev = dev;
5184 hdsp->pci = pci;
5185 snd_card_set_dev(card, &pci->dev);
5186
5187 if ((err = snd_hdsp_create(card, hdsp)) < 0) {
5188 snd_card_free(card);
5189 return err;
5190 }
5191
5192 strcpy(card->shortname, "Hammerfall DSP");
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005193 sprintf(card->longname, "%s at 0x%lx, irq %d", hdsp->card_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194 hdsp->port, hdsp->irq);
5195
5196 if ((err = snd_card_register(card)) < 0) {
5197 snd_card_free(card);
5198 return err;
5199 }
5200 pci_set_drvdata(pci, card);
5201 dev++;
5202 return 0;
5203}
5204
5205static void __devexit snd_hdsp_remove(struct pci_dev *pci)
5206{
5207 snd_card_free(pci_get_drvdata(pci));
5208 pci_set_drvdata(pci, NULL);
5209}
5210
5211static struct pci_driver driver = {
5212 .name = "RME Hammerfall DSP",
5213 .id_table = snd_hdsp_ids,
5214 .probe = snd_hdsp_probe,
5215 .remove = __devexit_p(snd_hdsp_remove),
5216};
5217
5218static int __init alsa_card_hdsp_init(void)
5219{
Takashi Iwai01d25d42005-04-11 16:58:24 +02005220 return pci_register_driver(&driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221}
5222
5223static void __exit alsa_card_hdsp_exit(void)
5224{
5225 pci_unregister_driver(&driver);
5226}
5227
5228module_init(alsa_card_hdsp_init)
5229module_exit(alsa_card_hdsp_exit)