blob: 825fbbea7f6ffa9a687197acad91a30135b135c6 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/pci.h>
28#include <linux/firmware.h>
Paul Gortmaker65a77212011-07-15 13:13:37 -040029#include <linux/module.h>
Takashi Iwai3f7440a2009-06-05 17:40:04 +020030#include <linux/math64.h>
Takashi Iwai82329322012-11-22 21:19:18 +010031#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#include <sound/core.h>
34#include <sound/control.h>
35#include <sound/pcm.h>
36#include <sound/info.h>
37#include <sound/asoundef.h>
38#include <sound/rawmidi.h>
39#include <sound/hwdep.h>
40#include <sound/initval.h>
41#include <sound/hdsp.h>
42
43#include <asm/byteorder.h>
44#include <asm/current.h>
45#include <asm/io.h>
46
47static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
48static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
Rusty Russella67ff6a2011-12-15 13:49:36 +103049static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51module_param_array(index, int, NULL, 0444);
52MODULE_PARM_DESC(index, "Index value for RME Hammerfall DSP interface.");
53module_param_array(id, charp, NULL, 0444);
54MODULE_PARM_DESC(id, "ID string for RME Hammerfall DSP interface.");
55module_param_array(enable, bool, NULL, 0444);
56MODULE_PARM_DESC(enable, "Enable/disable specific Hammerfall DSP soundcards.");
57MODULE_AUTHOR("Paul Davis <paul@linuxaudiosystems.com>, Marcus Andersson, Thomas Charbonnel <thomas@undata.org>");
58MODULE_DESCRIPTION("RME Hammerfall DSP");
59MODULE_LICENSE("GPL");
60MODULE_SUPPORTED_DEVICE("{{RME Hammerfall-DSP},"
61 "{RME HDSP-9652},"
62 "{RME HDSP-9632}}");
Florian Faber28b26e12010-12-01 12:14:47 +010063MODULE_FIRMWARE("rpm_firmware.bin");
Clemens Ladisch7e0af292007-05-03 17:59:54 +020064MODULE_FIRMWARE("multiface_firmware.bin");
65MODULE_FIRMWARE("multiface_firmware_rev11.bin");
66MODULE_FIRMWARE("digiface_firmware.bin");
67MODULE_FIRMWARE("digiface_firmware_rev11.bin");
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
Florian Faber28b26e12010-12-01 12:14:47 +010084#define RPM_CHANNELS 6
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86/* Write registers. These are defined as byte-offsets from the iobase value.
87 */
88#define HDSP_resetPointer 0
Remy Brunod7923b22006-10-17 12:41:56 +020089#define HDSP_freqReg 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#define HDSP_outputBufferAddress 32
91#define HDSP_inputBufferAddress 36
92#define HDSP_controlRegister 64
93#define HDSP_interruptConfirmation 96
94#define HDSP_outputEnable 128
95#define HDSP_control2Reg 256
96#define HDSP_midiDataOut0 352
97#define HDSP_midiDataOut1 356
98#define HDSP_fifoData 368
99#define HDSP_inputEnable 384
100
101/* Read registers. These are defined as byte-offsets from the iobase value
102 */
103
104#define HDSP_statusRegister 0
105#define HDSP_timecode 128
106#define HDSP_status2Register 192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107#define HDSP_midiDataIn0 360
108#define HDSP_midiDataIn1 364
109#define HDSP_midiStatusOut0 384
110#define HDSP_midiStatusOut1 388
111#define HDSP_midiStatusIn0 392
112#define HDSP_midiStatusIn1 396
113#define HDSP_fifoStatus 400
114
115/* the meters are regular i/o-mapped registers, but offset
116 considerably from the rest. the peak registers are reset
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100117 when read; the least-significant 4 bits are full-scale counters;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 the actual peak value is in the most-significant 24 bits.
119*/
120
121#define HDSP_playbackPeakLevel 4096 /* 26 * 32 bit values */
122#define HDSP_inputPeakLevel 4224 /* 26 * 32 bit values */
123#define HDSP_outputPeakLevel 4352 /* (26+2) * 32 bit values */
124#define HDSP_playbackRmsLevel 4612 /* 26 * 64 bit values */
125#define HDSP_inputRmsLevel 4868 /* 26 * 64 bit values */
126
127
128/* This is for H9652 cards
129 Peak values are read downward from the base
130 Rms values are read upward
131 There are rms values for the outputs too
132 26*3 values are read in ss mode
133 14*3 in ds mode, with no gap between values
134*/
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100135#define HDSP_9652_peakBase 7164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136#define HDSP_9652_rmsBase 4096
137
138/* c.f. the hdsp_9632_meters_t struct */
139#define HDSP_9632_metersBase 4096
140
141#define HDSP_IO_EXTENT 7168
142
143/* control2 register bits */
144
145#define HDSP_TMS 0x01
146#define HDSP_TCK 0x02
147#define HDSP_TDI 0x04
148#define HDSP_JTAG 0x08
149#define HDSP_PWDN 0x10
150#define HDSP_PROGRAM 0x020
151#define HDSP_CONFIG_MODE_0 0x040
152#define HDSP_CONFIG_MODE_1 0x080
Adrian Knotha3466862011-10-27 21:57:53 +0200153#define HDSP_VERSION_BIT (0x100 | HDSP_S_LOAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154#define HDSP_BIGENDIAN_MODE 0x200
155#define HDSP_RD_MULTIPLE 0x400
156#define HDSP_9652_ENABLE_MIXER 0x800
Adrian Knoth0c2bc7c2013-01-15 18:52:20 +0100157#define HDSP_S200 0x800
158#define HDSP_S300 (0x100 | HDSP_S200) /* dummy, purpose of 0x100 unknown */
159#define HDSP_CYCLIC_MODE 0x1000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160#define HDSP_TDO 0x10000000
161
Adrian Knoth0c2bc7c2013-01-15 18:52:20 +0100162#define HDSP_S_PROGRAM (HDSP_CYCLIC_MODE|HDSP_PROGRAM|HDSP_CONFIG_MODE_0)
163#define HDSP_S_LOAD (HDSP_CYCLIC_MODE|HDSP_PROGRAM|HDSP_CONFIG_MODE_1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165/* Control Register bits */
166
167#define HDSP_Start (1<<0) /* start engine */
168#define HDSP_Latency0 (1<<1) /* buffer size = 2^n where n is defined by Latency{2,1,0} */
169#define HDSP_Latency1 (1<<2) /* [ see above ] */
170#define HDSP_Latency2 (1<<3) /* [ see above ] */
171#define HDSP_ClockModeMaster (1<<4) /* 1=Master, 0=Slave/Autosync */
172#define HDSP_AudioInterruptEnable (1<<5) /* what do you think ? */
173#define HDSP_Frequency0 (1<<6) /* 0=44.1kHz/88.2kHz/176.4kHz 1=48kHz/96kHz/192kHz */
174#define HDSP_Frequency1 (1<<7) /* 0=32kHz/64kHz/128kHz */
175#define HDSP_DoubleSpeed (1<<8) /* 0=normal speed, 1=double speed */
176#define HDSP_SPDIFProfessional (1<<9) /* 0=consumer, 1=professional */
177#define HDSP_SPDIFEmphasis (1<<10) /* 0=none, 1=on */
178#define HDSP_SPDIFNonAudio (1<<11) /* 0=off, 1=on */
179#define HDSP_SPDIFOpticalOut (1<<12) /* 1=use 1st ADAT connector for SPDIF, 0=do not */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100180#define HDSP_SyncRef2 (1<<13)
181#define HDSP_SPDIFInputSelect0 (1<<14)
182#define HDSP_SPDIFInputSelect1 (1<<15)
183#define HDSP_SyncRef0 (1<<16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184#define HDSP_SyncRef1 (1<<17)
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100185#define HDSP_AnalogExtensionBoard (1<<18) /* For H9632 cards */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186#define HDSP_XLRBreakoutCable (1<<20) /* For H9632 cards */
187#define HDSP_Midi0InterruptEnable (1<<22)
188#define HDSP_Midi1InterruptEnable (1<<23)
189#define HDSP_LineOut (1<<24)
190#define HDSP_ADGain0 (1<<25) /* From here : H9632 specific */
191#define HDSP_ADGain1 (1<<26)
192#define HDSP_DAGain0 (1<<27)
193#define HDSP_DAGain1 (1<<28)
194#define HDSP_PhoneGain0 (1<<29)
195#define HDSP_PhoneGain1 (1<<30)
196#define HDSP_QuadSpeed (1<<31)
197
Florian Faber28b26e12010-12-01 12:14:47 +0100198/* RPM uses some of the registers for special purposes */
199#define HDSP_RPM_Inp12 0x04A00
200#define HDSP_RPM_Inp12_Phon_6dB 0x00800 /* Dolby */
201#define HDSP_RPM_Inp12_Phon_0dB 0x00000 /* .. */
202#define HDSP_RPM_Inp12_Phon_n6dB 0x04000 /* inp_0 */
203#define HDSP_RPM_Inp12_Line_0dB 0x04200 /* Dolby+PRO */
204#define HDSP_RPM_Inp12_Line_n6dB 0x00200 /* PRO */
205
206#define HDSP_RPM_Inp34 0x32000
207#define HDSP_RPM_Inp34_Phon_6dB 0x20000 /* SyncRef1 */
208#define HDSP_RPM_Inp34_Phon_0dB 0x00000 /* .. */
209#define HDSP_RPM_Inp34_Phon_n6dB 0x02000 /* SyncRef2 */
210#define HDSP_RPM_Inp34_Line_0dB 0x30000 /* SyncRef1+SyncRef0 */
211#define HDSP_RPM_Inp34_Line_n6dB 0x10000 /* SyncRef0 */
212
213#define HDSP_RPM_Bypass 0x01000
214
215#define HDSP_RPM_Disconnect 0x00001
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217#define HDSP_ADGainMask (HDSP_ADGain0|HDSP_ADGain1)
218#define HDSP_ADGainMinus10dBV HDSP_ADGainMask
219#define HDSP_ADGainPlus4dBu (HDSP_ADGain0)
220#define HDSP_ADGainLowGain 0
221
222#define HDSP_DAGainMask (HDSP_DAGain0|HDSP_DAGain1)
223#define HDSP_DAGainHighGain HDSP_DAGainMask
224#define HDSP_DAGainPlus4dBu (HDSP_DAGain0)
225#define HDSP_DAGainMinus10dBV 0
226
227#define HDSP_PhoneGainMask (HDSP_PhoneGain0|HDSP_PhoneGain1)
228#define HDSP_PhoneGain0dB HDSP_PhoneGainMask
229#define HDSP_PhoneGainMinus6dB (HDSP_PhoneGain0)
230#define HDSP_PhoneGainMinus12dB 0
231
232#define HDSP_LatencyMask (HDSP_Latency0|HDSP_Latency1|HDSP_Latency2)
233#define HDSP_FrequencyMask (HDSP_Frequency0|HDSP_Frequency1|HDSP_DoubleSpeed|HDSP_QuadSpeed)
234
235#define HDSP_SPDIFInputMask (HDSP_SPDIFInputSelect0|HDSP_SPDIFInputSelect1)
236#define HDSP_SPDIFInputADAT1 0
237#define HDSP_SPDIFInputCoaxial (HDSP_SPDIFInputSelect0)
238#define HDSP_SPDIFInputCdrom (HDSP_SPDIFInputSelect1)
239#define HDSP_SPDIFInputAES (HDSP_SPDIFInputSelect0|HDSP_SPDIFInputSelect1)
240
241#define HDSP_SyncRefMask (HDSP_SyncRef0|HDSP_SyncRef1|HDSP_SyncRef2)
242#define HDSP_SyncRef_ADAT1 0
243#define HDSP_SyncRef_ADAT2 (HDSP_SyncRef0)
244#define HDSP_SyncRef_ADAT3 (HDSP_SyncRef1)
245#define HDSP_SyncRef_SPDIF (HDSP_SyncRef0|HDSP_SyncRef1)
246#define HDSP_SyncRef_WORD (HDSP_SyncRef2)
247#define HDSP_SyncRef_ADAT_SYNC (HDSP_SyncRef0|HDSP_SyncRef2)
248
249/* Sample Clock Sources */
250
251#define HDSP_CLOCK_SOURCE_AUTOSYNC 0
252#define HDSP_CLOCK_SOURCE_INTERNAL_32KHZ 1
253#define HDSP_CLOCK_SOURCE_INTERNAL_44_1KHZ 2
254#define HDSP_CLOCK_SOURCE_INTERNAL_48KHZ 3
255#define HDSP_CLOCK_SOURCE_INTERNAL_64KHZ 4
256#define HDSP_CLOCK_SOURCE_INTERNAL_88_2KHZ 5
257#define HDSP_CLOCK_SOURCE_INTERNAL_96KHZ 6
258#define HDSP_CLOCK_SOURCE_INTERNAL_128KHZ 7
259#define HDSP_CLOCK_SOURCE_INTERNAL_176_4KHZ 8
260#define HDSP_CLOCK_SOURCE_INTERNAL_192KHZ 9
261
262/* Preferred sync reference choices - used by "pref_sync_ref" control switch */
263
264#define HDSP_SYNC_FROM_WORD 0
265#define HDSP_SYNC_FROM_SPDIF 1
266#define HDSP_SYNC_FROM_ADAT1 2
267#define HDSP_SYNC_FROM_ADAT_SYNC 3
268#define HDSP_SYNC_FROM_ADAT2 4
269#define HDSP_SYNC_FROM_ADAT3 5
270
271/* SyncCheck status */
272
273#define HDSP_SYNC_CHECK_NO_LOCK 0
274#define HDSP_SYNC_CHECK_LOCK 1
275#define HDSP_SYNC_CHECK_SYNC 2
276
277/* AutoSync references - used by "autosync_ref" control switch */
278
279#define HDSP_AUTOSYNC_FROM_WORD 0
280#define HDSP_AUTOSYNC_FROM_ADAT_SYNC 1
281#define HDSP_AUTOSYNC_FROM_SPDIF 2
282#define HDSP_AUTOSYNC_FROM_NONE 3
283#define HDSP_AUTOSYNC_FROM_ADAT1 4
284#define HDSP_AUTOSYNC_FROM_ADAT2 5
285#define HDSP_AUTOSYNC_FROM_ADAT3 6
286
287/* Possible sources of S/PDIF input */
288
289#define HDSP_SPDIFIN_OPTICAL 0 /* optical (ADAT1) */
290#define HDSP_SPDIFIN_COAXIAL 1 /* coaxial (RCA) */
291#define HDSP_SPDIFIN_INTERNAL 2 /* internal (CDROM) */
292#define HDSP_SPDIFIN_AES 3 /* xlr for H9632 (AES)*/
293
294#define HDSP_Frequency32KHz HDSP_Frequency0
295#define HDSP_Frequency44_1KHz HDSP_Frequency1
296#define HDSP_Frequency48KHz (HDSP_Frequency1|HDSP_Frequency0)
297#define HDSP_Frequency64KHz (HDSP_DoubleSpeed|HDSP_Frequency0)
298#define HDSP_Frequency88_2KHz (HDSP_DoubleSpeed|HDSP_Frequency1)
299#define HDSP_Frequency96KHz (HDSP_DoubleSpeed|HDSP_Frequency1|HDSP_Frequency0)
300/* For H9632 cards */
301#define HDSP_Frequency128KHz (HDSP_QuadSpeed|HDSP_DoubleSpeed|HDSP_Frequency0)
302#define HDSP_Frequency176_4KHz (HDSP_QuadSpeed|HDSP_DoubleSpeed|HDSP_Frequency1)
303#define HDSP_Frequency192KHz (HDSP_QuadSpeed|HDSP_DoubleSpeed|HDSP_Frequency1|HDSP_Frequency0)
Julian Cablee4b60882007-03-19 11:44:40 +0100304/* RME says n = 104857600000000, but in the windows MADI driver, I see:
305 return 104857600000000 / rate; // 100 MHz
306 return 110100480000000 / rate; // 105 MHz
307*/
308#define DDS_NUMERATOR 104857600000000ULL; /* = 2^20 * 10^8 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310#define hdsp_encode_latency(x) (((x)<<1) & HDSP_LatencyMask)
311#define hdsp_decode_latency(x) (((x) & HDSP_LatencyMask)>>1)
312
313#define hdsp_encode_spdif_in(x) (((x)&0x3)<<14)
314#define hdsp_decode_spdif_in(x) (((x)>>14)&0x3)
315
316/* Status Register bits */
317
318#define HDSP_audioIRQPending (1<<0)
319#define HDSP_Lock2 (1<<1) /* this is for Digiface and H9652 */
320#define HDSP_spdifFrequency3 HDSP_Lock2 /* this is for H9632 only */
321#define HDSP_Lock1 (1<<2)
322#define HDSP_Lock0 (1<<3)
323#define HDSP_SPDIFSync (1<<4)
324#define HDSP_TimecodeLock (1<<5)
325#define HDSP_BufferPositionMask 0x000FFC0 /* Bit 6..15 : h/w buffer pointer */
326#define HDSP_Sync2 (1<<16)
327#define HDSP_Sync1 (1<<17)
328#define HDSP_Sync0 (1<<18)
329#define HDSP_DoubleSpeedStatus (1<<19)
330#define HDSP_ConfigError (1<<20)
331#define HDSP_DllError (1<<21)
332#define HDSP_spdifFrequency0 (1<<22)
333#define HDSP_spdifFrequency1 (1<<23)
334#define HDSP_spdifFrequency2 (1<<24)
335#define HDSP_SPDIFErrorFlag (1<<25)
336#define HDSP_BufferID (1<<26)
337#define HDSP_TimecodeSync (1<<27)
338#define HDSP_AEBO (1<<28) /* H9632 specific Analog Extension Boards */
339#define HDSP_AEBI (1<<29) /* 0 = present, 1 = absent */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100340#define HDSP_midi0IRQPending (1<<30)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341#define HDSP_midi1IRQPending (1<<31)
342
343#define HDSP_spdifFrequencyMask (HDSP_spdifFrequency0|HDSP_spdifFrequency1|HDSP_spdifFrequency2)
Remy Bruno47ba97f2008-02-22 17:57:02 +0100344#define HDSP_spdifFrequencyMask_9632 (HDSP_spdifFrequency0|\
345 HDSP_spdifFrequency1|\
346 HDSP_spdifFrequency2|\
347 HDSP_spdifFrequency3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349#define HDSP_spdifFrequency32KHz (HDSP_spdifFrequency0)
350#define HDSP_spdifFrequency44_1KHz (HDSP_spdifFrequency1)
351#define HDSP_spdifFrequency48KHz (HDSP_spdifFrequency0|HDSP_spdifFrequency1)
352
353#define HDSP_spdifFrequency64KHz (HDSP_spdifFrequency2)
354#define HDSP_spdifFrequency88_2KHz (HDSP_spdifFrequency0|HDSP_spdifFrequency2)
355#define HDSP_spdifFrequency96KHz (HDSP_spdifFrequency2|HDSP_spdifFrequency1)
356
357/* This is for H9632 cards */
Remy Bruno47ba97f2008-02-22 17:57:02 +0100358#define HDSP_spdifFrequency128KHz (HDSP_spdifFrequency0|\
359 HDSP_spdifFrequency1|\
360 HDSP_spdifFrequency2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361#define HDSP_spdifFrequency176_4KHz HDSP_spdifFrequency3
362#define HDSP_spdifFrequency192KHz (HDSP_spdifFrequency3|HDSP_spdifFrequency0)
363
364/* Status2 Register bits */
365
366#define HDSP_version0 (1<<0)
367#define HDSP_version1 (1<<1)
368#define HDSP_version2 (1<<2)
369#define HDSP_wc_lock (1<<3)
370#define HDSP_wc_sync (1<<4)
371#define HDSP_inp_freq0 (1<<5)
372#define HDSP_inp_freq1 (1<<6)
373#define HDSP_inp_freq2 (1<<7)
374#define HDSP_SelSyncRef0 (1<<8)
375#define HDSP_SelSyncRef1 (1<<9)
376#define HDSP_SelSyncRef2 (1<<10)
377
378#define HDSP_wc_valid (HDSP_wc_lock|HDSP_wc_sync)
379
380#define HDSP_systemFrequencyMask (HDSP_inp_freq0|HDSP_inp_freq1|HDSP_inp_freq2)
381#define HDSP_systemFrequency32 (HDSP_inp_freq0)
382#define HDSP_systemFrequency44_1 (HDSP_inp_freq1)
383#define HDSP_systemFrequency48 (HDSP_inp_freq0|HDSP_inp_freq1)
384#define HDSP_systemFrequency64 (HDSP_inp_freq2)
385#define HDSP_systemFrequency88_2 (HDSP_inp_freq0|HDSP_inp_freq2)
386#define HDSP_systemFrequency96 (HDSP_inp_freq1|HDSP_inp_freq2)
387/* FIXME : more values for 9632 cards ? */
388
389#define HDSP_SelSyncRefMask (HDSP_SelSyncRef0|HDSP_SelSyncRef1|HDSP_SelSyncRef2)
390#define HDSP_SelSyncRef_ADAT1 0
391#define HDSP_SelSyncRef_ADAT2 (HDSP_SelSyncRef0)
392#define HDSP_SelSyncRef_ADAT3 (HDSP_SelSyncRef1)
393#define HDSP_SelSyncRef_SPDIF (HDSP_SelSyncRef0|HDSP_SelSyncRef1)
394#define HDSP_SelSyncRef_WORD (HDSP_SelSyncRef2)
395#define HDSP_SelSyncRef_ADAT_SYNC (HDSP_SelSyncRef0|HDSP_SelSyncRef2)
396
397/* Card state flags */
398
399#define HDSP_InitializationComplete (1<<0)
400#define HDSP_FirmwareLoaded (1<<1)
401#define HDSP_FirmwareCached (1<<2)
402
403/* FIFO wait times, defined in terms of 1/10ths of msecs */
404
405#define HDSP_LONG_WAIT 5000
406#define HDSP_SHORT_WAIT 30
407
408#define UNITY_GAIN 32768
409#define MINUS_INFINITY_GAIN 0
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411/* the size of a substream (1 mono data stream) */
412
413#define HDSP_CHANNEL_BUFFER_SAMPLES (16*1024)
414#define HDSP_CHANNEL_BUFFER_BYTES (4*HDSP_CHANNEL_BUFFER_SAMPLES)
415
416/* the size of the area we need to allocate for DMA transfers. the
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100417 size is the same regardless of the number of channels - the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 Multiface still uses the same memory area.
419
420 Note that we allocate 1 more channel than is apparently needed
421 because the h/w seems to write 1 byte beyond the end of the last
422 page. Sigh.
423*/
424
425#define HDSP_DMA_AREA_BYTES ((HDSP_MAX_CHANNELS+1) * HDSP_CHANNEL_BUFFER_BYTES)
426#define HDSP_DMA_AREA_KILOBYTES (HDSP_DMA_AREA_BYTES/1024)
427
Takashi Iwai90caaef2012-11-22 16:55:11 +0100428#define HDSP_FIRMWARE_SIZE (24413 * 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Takashi Iwai55e957d2005-11-17 14:52:13 +0100430struct hdsp_9632_meters {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 u32 input_peak[16];
432 u32 playback_peak[16];
433 u32 output_peak[16];
434 u32 xxx_peak[16];
435 u32 padding[64];
436 u32 input_rms_low[16];
437 u32 playback_rms_low[16];
438 u32 output_rms_low[16];
439 u32 xxx_rms_low[16];
440 u32 input_rms_high[16];
441 u32 playback_rms_high[16];
442 u32 output_rms_high[16];
443 u32 xxx_rms_high[16];
444};
445
Takashi Iwai55e957d2005-11-17 14:52:13 +0100446struct hdsp_midi {
447 struct hdsp *hdsp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 int id;
Takashi Iwai55e957d2005-11-17 14:52:13 +0100449 struct snd_rawmidi *rmidi;
450 struct snd_rawmidi_substream *input;
451 struct snd_rawmidi_substream *output;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 char istimer; /* timer in use */
453 struct timer_list timer;
454 spinlock_t lock;
455 int pending;
456};
457
Takashi Iwai55e957d2005-11-17 14:52:13 +0100458struct hdsp {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 spinlock_t lock;
Takashi Iwai55e957d2005-11-17 14:52:13 +0100460 struct snd_pcm_substream *capture_substream;
461 struct snd_pcm_substream *playback_substream;
462 struct hdsp_midi midi[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 struct tasklet_struct midi_tasklet;
464 int use_midi_tasklet;
465 int precise_ptr;
466 u32 control_register; /* cached value */
467 u32 control2_register; /* cached value */
468 u32 creg_spdif;
469 u32 creg_spdif_stream;
Takashi Iwaie3ea4d82005-07-04 18:12:39 +0200470 int clock_source_locked;
Florian Faber28b26e12010-12-01 12:14:47 +0100471 char *card_name; /* digiface/multiface/rpm */
Takashi Iwai55e957d2005-11-17 14:52:13 +0100472 enum HDSP_IO_Type io_type; /* ditto, but for code use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 unsigned short firmware_rev;
474 unsigned short state; /* stores state bits */
Takashi Iwai90caaef2012-11-22 16:55:11 +0100475 const struct firmware *firmware;
476 u32 *fw_uploaded;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 size_t period_bytes; /* guess what this is */
478 unsigned char max_channels;
479 unsigned char qs_in_channels; /* quad speed mode for H9632 */
480 unsigned char ds_in_channels;
481 unsigned char ss_in_channels; /* different for multiface/digiface */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100482 unsigned char qs_out_channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 unsigned char ds_out_channels;
484 unsigned char ss_out_channels;
485
486 struct snd_dma_buffer capture_dma_buf;
487 struct snd_dma_buffer playback_dma_buf;
488 unsigned char *capture_buffer; /* suitably aligned address */
489 unsigned char *playback_buffer; /* suitably aligned address */
490
491 pid_t capture_pid;
492 pid_t playback_pid;
493 int running;
494 int system_sample_rate;
495 char *channel_map;
496 int dev;
497 int irq;
498 unsigned long port;
499 void __iomem *iobase;
Takashi Iwai55e957d2005-11-17 14:52:13 +0100500 struct snd_card *card;
501 struct snd_pcm *pcm;
502 struct snd_hwdep *hwdep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 struct pci_dev *pci;
Takashi Iwai55e957d2005-11-17 14:52:13 +0100504 struct snd_kcontrol *spdif_ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 unsigned short mixer_matrix[HDSP_MATRIX_MIXER_SIZE];
Remy Brunod7923b22006-10-17 12:41:56 +0200506 unsigned int dds_value; /* last value written to freq register */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507};
508
509/* These tables map the ALSA channels 1..N to the channels that we
510 need to use in order to find the relevant channel buffer. RME
511 refer to this kind of mapping as between "the ADAT channel and
512 the DMA channel." We index it using the logical audio channel,
513 and the value is the DMA channel (i.e. channel buffer number)
514 where the data for that channel can be read/written from/to.
515*/
516
517static char channel_map_df_ss[HDSP_MAX_CHANNELS] = {
518 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
519 18, 19, 20, 21, 22, 23, 24, 25
520};
521
522static char channel_map_mf_ss[HDSP_MAX_CHANNELS] = { /* Multiface */
523 /* Analog */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100524 0, 1, 2, 3, 4, 5, 6, 7,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 /* ADAT 2 */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100526 16, 17, 18, 19, 20, 21, 22, 23,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 /* SPDIF */
528 24, 25,
529 -1, -1, -1, -1, -1, -1, -1, -1
530};
531
532static char channel_map_ds[HDSP_MAX_CHANNELS] = {
533 /* ADAT channels are remapped */
534 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23,
535 /* channels 12 and 13 are S/PDIF */
536 24, 25,
537 /* others don't exist */
538 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
539};
540
541static char channel_map_H9632_ss[HDSP_MAX_CHANNELS] = {
542 /* ADAT channels */
543 0, 1, 2, 3, 4, 5, 6, 7,
544 /* SPDIF */
545 8, 9,
546 /* Analog */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100547 10, 11,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 /* AO4S-192 and AI4S-192 extension boards */
549 12, 13, 14, 15,
550 /* others don't exist */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100551 -1, -1, -1, -1, -1, -1, -1, -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 -1, -1
553};
554
555static char channel_map_H9632_ds[HDSP_MAX_CHANNELS] = {
556 /* ADAT */
557 1, 3, 5, 7,
558 /* SPDIF */
559 8, 9,
560 /* Analog */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100561 10, 11,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 /* AO4S-192 and AI4S-192 extension boards */
563 12, 13, 14, 15,
564 /* others don't exist */
565 -1, -1, -1, -1, -1, -1, -1, -1,
566 -1, -1, -1, -1, -1, -1
567};
568
569static char channel_map_H9632_qs[HDSP_MAX_CHANNELS] = {
570 /* ADAT is disabled in this mode */
571 /* SPDIF */
572 8, 9,
573 /* Analog */
574 10, 11,
575 /* AO4S-192 and AI4S-192 extension boards */
576 12, 13, 14, 15,
577 /* others don't exist */
578 -1, -1, -1, -1, -1, -1, -1, -1,
579 -1, -1, -1, -1, -1, -1, -1, -1,
580 -1, -1
581};
582
583static int snd_hammerfall_get_buffer(struct pci_dev *pci, struct snd_dma_buffer *dmab, size_t size)
584{
585 dmab->dev.type = SNDRV_DMA_TYPE_DEV;
586 dmab->dev.dev = snd_dma_pci_data(pci);
Takashi Iwaib6a96912005-05-30 18:27:03 +0200587 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
588 size, dmab) < 0)
589 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 return 0;
591}
592
593static void snd_hammerfall_free_buffer(struct snd_dma_buffer *dmab, struct pci_dev *pci)
594{
Takashi Iwai47d98c02014-01-08 16:12:25 +0100595 if (dmab->area)
596 snd_dma_free_pages(dmab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597}
598
599
Alexey Dobriyancebe41d2010-02-06 00:21:03 +0200600static DEFINE_PCI_DEVICE_TABLE(snd_hdsp_ids) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 {
602 .vendor = PCI_VENDOR_ID_XILINX,
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100603 .device = PCI_DEVICE_ID_XILINX_HAMMERFALL_DSP,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 .subvendor = PCI_ANY_ID,
605 .subdevice = PCI_ANY_ID,
606 }, /* RME Hammerfall-DSP */
607 { 0, },
608};
609
610MODULE_DEVICE_TABLE(pci, snd_hdsp_ids);
611
612/* prototypes */
Takashi Iwai55e957d2005-11-17 14:52:13 +0100613static int snd_hdsp_create_alsa_devices(struct snd_card *card, struct hdsp *hdsp);
614static int snd_hdsp_create_pcm(struct snd_card *card, struct hdsp *hdsp);
615static int snd_hdsp_enable_io (struct hdsp *hdsp);
616static void snd_hdsp_initialize_midi_flush (struct hdsp *hdsp);
617static void snd_hdsp_initialize_channels (struct hdsp *hdsp);
618static int hdsp_fifo_wait(struct hdsp *hdsp, int count, int timeout);
619static int hdsp_autosync_ref(struct hdsp *hdsp);
620static int snd_hdsp_set_defaults(struct hdsp *hdsp);
621static void snd_hdsp_9652_enable_mixer (struct hdsp *hdsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Takashi Iwai55e957d2005-11-17 14:52:13 +0100623static int hdsp_playback_to_output_key (struct hdsp *hdsp, int in, int out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
Remy Brunoa3a68c82007-08-31 12:33:54 +0200625 switch (hdsp->io_type) {
626 case Multiface:
627 case Digiface:
Florian Faber28b26e12010-12-01 12:14:47 +0100628 case RPM:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 default:
Andreas Degert192b8e32008-01-16 15:59:48 +0100630 if (hdsp->firmware_rev == 0xa)
631 return (64 * out) + (32 + (in));
632 else
633 return (52 * out) + (26 + (in));
Remy Brunoa3a68c82007-08-31 12:33:54 +0200634 case H9632:
635 return (32 * out) + (16 + (in));
636 case H9652:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 return (52 * out) + (26 + (in));
638 }
639}
640
Takashi Iwai55e957d2005-11-17 14:52:13 +0100641static int hdsp_input_to_output_key (struct hdsp *hdsp, int in, int out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
Remy Brunoa3a68c82007-08-31 12:33:54 +0200643 switch (hdsp->io_type) {
644 case Multiface:
645 case Digiface:
Florian Faber28b26e12010-12-01 12:14:47 +0100646 case RPM:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 default:
Andreas Degert192b8e32008-01-16 15:59:48 +0100648 if (hdsp->firmware_rev == 0xa)
649 return (64 * out) + in;
650 else
651 return (52 * out) + in;
Remy Brunoa3a68c82007-08-31 12:33:54 +0200652 case H9632:
653 return (32 * out) + in;
654 case H9652:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return (52 * out) + in;
656 }
657}
658
Takashi Iwai55e957d2005-11-17 14:52:13 +0100659static void hdsp_write(struct hdsp *hdsp, int reg, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
661 writel(val, hdsp->iobase + reg);
662}
663
Takashi Iwai55e957d2005-11-17 14:52:13 +0100664static unsigned int hdsp_read(struct hdsp *hdsp, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
666 return readl (hdsp->iobase + reg);
667}
668
Takashi Iwai55e957d2005-11-17 14:52:13 +0100669static int hdsp_check_for_iobox (struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
Adrian Knoth0c2bc7c2013-01-15 18:52:20 +0100671 int i;
672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 if (hdsp->io_type == H9652 || hdsp->io_type == H9632) return 0;
Adrian Knoth0c2bc7c2013-01-15 18:52:20 +0100674 for (i = 0; i < 500; i++) {
675 if (0 == (hdsp_read(hdsp, HDSP_statusRegister) &
676 HDSP_ConfigError)) {
677 if (i) {
678 snd_printd("Hammerfall-DSP: IO box found after %d ms\n",
679 (20 * i));
680 }
681 return 0;
682 }
683 msleep(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 }
Adrian Knoth0c2bc7c2013-01-15 18:52:20 +0100685 snd_printk(KERN_ERR "Hammerfall-DSP: no IO box connected!\n");
686 hdsp->state &= ~HDSP_FirmwareLoaded;
687 return -EIO;
Tim Blechmanne588ed82009-02-20 19:30:35 +0100688}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Tim Blechmanne588ed82009-02-20 19:30:35 +0100690static int hdsp_wait_for_iobox(struct hdsp *hdsp, unsigned int loops,
691 unsigned int delay)
692{
693 unsigned int i;
694
695 if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
696 return 0;
697
698 for (i = 0; i != loops; ++i) {
699 if (hdsp_read(hdsp, HDSP_statusRegister) & HDSP_ConfigError)
700 msleep(delay);
701 else {
702 snd_printd("Hammerfall-DSP: iobox found after %ums!\n",
703 i * delay);
704 return 0;
705 }
706 }
707
Florian Faber28b26e12010-12-01 12:14:47 +0100708 snd_printk("Hammerfall-DSP: no IO box connected!\n");
Tim Blechmanne588ed82009-02-20 19:30:35 +0100709 hdsp->state &= ~HDSP_FirmwareLoaded;
710 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711}
712
Takashi Iwai55e957d2005-11-17 14:52:13 +0100713static int snd_hdsp_load_firmware_from_cache(struct hdsp *hdsp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
715 int i;
716 unsigned long flags;
Takashi Iwai90caaef2012-11-22 16:55:11 +0100717 const u32 *cache;
718
719 if (hdsp->fw_uploaded)
720 cache = hdsp->fw_uploaded;
721 else {
722 if (!hdsp->firmware)
723 return -ENODEV;
724 cache = (u32 *)hdsp->firmware->data;
725 if (!cache)
726 return -ENODEV;
727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729 if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 snd_printk ("Hammerfall-DSP: loading firmware\n");
732
733 hdsp_write (hdsp, HDSP_control2Reg, HDSP_S_PROGRAM);
734 hdsp_write (hdsp, HDSP_fifoData, 0);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 if (hdsp_fifo_wait (hdsp, 0, HDSP_LONG_WAIT)) {
737 snd_printk ("Hammerfall-DSP: timeout waiting for download preparation\n");
Adrian Knoth0c2bc7c2013-01-15 18:52:20 +0100738 hdsp_write(hdsp, HDSP_control2Reg, HDSP_S200);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 return -EIO;
740 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 hdsp_write (hdsp, HDSP_control2Reg, HDSP_S_LOAD);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100743
Takashi Iwai90caaef2012-11-22 16:55:11 +0100744 for (i = 0; i < HDSP_FIRMWARE_SIZE / 4; ++i) {
745 hdsp_write(hdsp, HDSP_fifoData, cache[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 if (hdsp_fifo_wait (hdsp, 127, HDSP_LONG_WAIT)) {
747 snd_printk ("Hammerfall-DSP: timeout during firmware loading\n");
Adrian Knoth0c2bc7c2013-01-15 18:52:20 +0100748 hdsp_write(hdsp, HDSP_control2Reg, HDSP_S200);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 return -EIO;
750 }
751 }
752
Adrian Knoth0c2bc7c2013-01-15 18:52:20 +0100753 hdsp_fifo_wait(hdsp, 3, HDSP_LONG_WAIT);
754 hdsp_write(hdsp, HDSP_control2Reg, HDSP_S200);
755
Takashi Iwaib0b98112005-10-20 18:29:58 +0200756 ssleep(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757#ifdef SNDRV_BIG_ENDIAN
758 hdsp->control2_register = HDSP_BIGENDIAN_MODE;
759#else
760 hdsp->control2_register = 0;
761#endif
762 hdsp_write (hdsp, HDSP_control2Reg, hdsp->control2_register);
763 snd_printk ("Hammerfall-DSP: finished firmware loading\n");
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 }
766 if (hdsp->state & HDSP_InitializationComplete) {
Takashi Iwaib0b98112005-10-20 18:29:58 +0200767 snd_printk(KERN_INFO "Hammerfall-DSP: firmware loaded from cache, restoring defaults\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 spin_lock_irqsave(&hdsp->lock, flags);
769 snd_hdsp_set_defaults(hdsp);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100770 spin_unlock_irqrestore(&hdsp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 hdsp->state |= HDSP_FirmwareLoaded;
774
775 return 0;
776}
777
Takashi Iwai55e957d2005-11-17 14:52:13 +0100778static int hdsp_get_iobox_version (struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
780 if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100781
Adrian Knoth0c2bc7c2013-01-15 18:52:20 +0100782 hdsp_write(hdsp, HDSP_control2Reg, HDSP_S_LOAD);
783 hdsp_write(hdsp, HDSP_fifoData, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Adrian Knoth0c2bc7c2013-01-15 18:52:20 +0100785 if (hdsp_fifo_wait(hdsp, 0, HDSP_SHORT_WAIT) < 0) {
786 hdsp_write(hdsp, HDSP_control2Reg, HDSP_S300);
Florian Faber28b26e12010-12-01 12:14:47 +0100787 hdsp_write(hdsp, HDSP_control2Reg, HDSP_S_LOAD);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100788 }
Adrian Knoth0c2bc7c2013-01-15 18:52:20 +0100789
790 hdsp_write(hdsp, HDSP_control2Reg, HDSP_S200 | HDSP_PROGRAM);
791 hdsp_write (hdsp, HDSP_fifoData, 0);
792 if (hdsp_fifo_wait(hdsp, 0, HDSP_SHORT_WAIT) < 0) {
793 hdsp->io_type = Multiface;
794 snd_printk("Hammerfall-DSP: Multiface found\n");
795 return 0;
796 }
797
798 hdsp_write(hdsp, HDSP_control2Reg, HDSP_S_LOAD);
799 hdsp_write(hdsp, HDSP_fifoData, 0);
800 if (hdsp_fifo_wait(hdsp, 0, HDSP_SHORT_WAIT) == 0) {
801 hdsp->io_type = Digiface;
802 snd_printk("Hammerfall-DSP: Digiface found\n");
803 return 0;
804 }
805
806 hdsp_write(hdsp, HDSP_control2Reg, HDSP_S300);
807 hdsp_write(hdsp, HDSP_control2Reg, HDSP_S_LOAD);
808 hdsp_write(hdsp, HDSP_fifoData, 0);
809 if (hdsp_fifo_wait(hdsp, 0, HDSP_SHORT_WAIT) == 0) {
810 hdsp->io_type = Multiface;
811 snd_printk("Hammerfall-DSP: Multiface found\n");
812 return 0;
813 }
814
815 hdsp_write(hdsp, HDSP_control2Reg, HDSP_S300);
816 hdsp_write(hdsp, HDSP_control2Reg, HDSP_S_LOAD);
817 hdsp_write(hdsp, HDSP_fifoData, 0);
818 if (hdsp_fifo_wait(hdsp, 0, HDSP_SHORT_WAIT) < 0) {
819 hdsp->io_type = Multiface;
820 snd_printk("Hammerfall-DSP: Multiface found\n");
821 return 0;
822 }
823
824 hdsp->io_type = RPM;
825 snd_printk("Hammerfall-DSP: RPM found\n");
826 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 } else {
828 /* firmware was already loaded, get iobox type */
Florian Faber28b26e12010-12-01 12:14:47 +0100829 if (hdsp_read(hdsp, HDSP_status2Register) & HDSP_version2)
830 hdsp->io_type = RPM;
831 else if (hdsp_read(hdsp, HDSP_status2Register) & HDSP_version1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 hdsp->io_type = Multiface;
Takashi Iwaib0b98112005-10-20 18:29:58 +0200833 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 hdsp->io_type = Digiface;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 }
836 return 0;
837}
838
839
Takashi Iwai92eed662008-02-22 18:35:56 +0100840static int hdsp_request_fw_loader(struct hdsp *hdsp);
Takashi Iwai311e70a2006-09-06 12:13:37 +0200841
842static int hdsp_check_for_firmware (struct hdsp *hdsp, int load_on_demand)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
Takashi Iwai311e70a2006-09-06 12:13:37 +0200844 if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
845 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 hdsp->state &= ~HDSP_FirmwareLoaded;
Takashi Iwai311e70a2006-09-06 12:13:37 +0200848 if (! load_on_demand)
Takashi Iwaib0b98112005-10-20 18:29:58 +0200849 return -EIO;
Takashi Iwai311e70a2006-09-06 12:13:37 +0200850 snd_printk(KERN_ERR "Hammerfall-DSP: firmware not present.\n");
Takashi Iwaib0b98112005-10-20 18:29:58 +0200851 /* try to load firmware */
Takashi Iwai311e70a2006-09-06 12:13:37 +0200852 if (! (hdsp->state & HDSP_FirmwareCached)) {
Takashi Iwai311e70a2006-09-06 12:13:37 +0200853 if (! hdsp_request_fw_loader(hdsp))
854 return 0;
Takashi Iwai311e70a2006-09-06 12:13:37 +0200855 snd_printk(KERN_ERR
856 "Hammerfall-DSP: No firmware loaded nor "
857 "cached, please upload firmware.\n");
858 return -EIO;
Takashi Iwaib0b98112005-10-20 18:29:58 +0200859 }
Takashi Iwai311e70a2006-09-06 12:13:37 +0200860 if (snd_hdsp_load_firmware_from_cache(hdsp) != 0) {
861 snd_printk(KERN_ERR
862 "Hammerfall-DSP: Firmware loading from "
863 "cache failed, please upload manually.\n");
864 return -EIO;
865 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 }
867 return 0;
868}
869
870
Takashi Iwai55e957d2005-11-17 14:52:13 +0100871static int hdsp_fifo_wait(struct hdsp *hdsp, int count, int timeout)
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100872{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 int i;
874
875 /* the fifoStatus registers reports on how many words
876 are available in the command FIFO.
877 */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 for (i = 0; i < timeout; i++) {
880
881 if ((int)(hdsp_read (hdsp, HDSP_fifoStatus) & 0xff) <= count)
882 return 0;
883
884 /* not very friendly, but we only do this during a firmware
885 load and changing the mixer, so we just put up with it.
886 */
887
888 udelay (100);
889 }
890
891 snd_printk ("Hammerfall-DSP: wait for FIFO status <= %d failed after %d iterations\n",
892 count, timeout);
893 return -1;
894}
895
Takashi Iwai55e957d2005-11-17 14:52:13 +0100896static int hdsp_read_gain (struct hdsp *hdsp, unsigned int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
Takashi Iwaib0b98112005-10-20 18:29:58 +0200898 if (addr >= HDSP_MATRIX_MIXER_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 return 0;
Takashi Iwaib0b98112005-10-20 18:29:58 +0200900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 return hdsp->mixer_matrix[addr];
902}
903
Takashi Iwai55e957d2005-11-17 14:52:13 +0100904static int hdsp_write_gain(struct hdsp *hdsp, unsigned int addr, unsigned short data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
906 unsigned int ad;
907
908 if (addr >= HDSP_MATRIX_MIXER_SIZE)
909 return -1;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 if (hdsp->io_type == H9652 || hdsp->io_type == H9632) {
912
913 /* from martin bjornsen:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 "You can only write dwords to the
916 mixer memory which contain two
917 mixer values in the low and high
918 word. So if you want to change
919 value 0 you have to read value 1
920 from the cache and write both to
921 the first dword in the mixer
922 memory."
923 */
924
Takashi Iwaib0b98112005-10-20 18:29:58 +0200925 if (hdsp->io_type == H9632 && addr >= 512)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Takashi Iwaib0b98112005-10-20 18:29:58 +0200928 if (hdsp->io_type == H9652 && addr >= 1352)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
931 hdsp->mixer_matrix[addr] = data;
932
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 /* `addr' addresses a 16-bit wide address, but
935 the address space accessed via hdsp_write
936 uses byte offsets. put another way, addr
937 varies from 0 to 1351, but to access the
938 corresponding memory location, we need
939 to access 0 to 2703 ...
940 */
941 ad = addr/2;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100942
943 hdsp_write (hdsp, 4096 + (ad*4),
944 (hdsp->mixer_matrix[(addr&0x7fe)+1] << 16) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 hdsp->mixer_matrix[addr&0x7fe]);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 return 0;
948
949 } else {
950
951 ad = (addr << 16) + data;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100952
Takashi Iwaib0b98112005-10-20 18:29:58 +0200953 if (hdsp_fifo_wait(hdsp, 127, HDSP_LONG_WAIT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956 hdsp_write (hdsp, HDSP_fifoData, ad);
957 hdsp->mixer_matrix[addr] = data;
958
959 }
960
961 return 0;
962}
963
Takashi Iwai55e957d2005-11-17 14:52:13 +0100964static int snd_hdsp_use_is_exclusive(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
966 unsigned long flags;
967 int ret = 1;
968
969 spin_lock_irqsave(&hdsp->lock, flags);
970 if ((hdsp->playback_pid != hdsp->capture_pid) &&
Takashi Iwaib0b98112005-10-20 18:29:58 +0200971 (hdsp->playback_pid >= 0) && (hdsp->capture_pid >= 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 spin_unlock_irqrestore(&hdsp->lock, flags);
974 return ret;
975}
976
Takashi Iwai55e957d2005-11-17 14:52:13 +0100977static int hdsp_spdif_sample_rate(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
979 unsigned int status = hdsp_read(hdsp, HDSP_statusRegister);
980 unsigned int rate_bits = (status & HDSP_spdifFrequencyMask);
981
Remy Bruno47ba97f2008-02-22 17:57:02 +0100982 /* For the 9632, the mask is different */
983 if (hdsp->io_type == H9632)
984 rate_bits = (status & HDSP_spdifFrequencyMask_9632);
985
Takashi Iwaib0b98112005-10-20 18:29:58 +0200986 if (status & HDSP_SPDIFErrorFlag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 return 0;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 switch (rate_bits) {
990 case HDSP_spdifFrequency32KHz: return 32000;
991 case HDSP_spdifFrequency44_1KHz: return 44100;
992 case HDSP_spdifFrequency48KHz: return 48000;
993 case HDSP_spdifFrequency64KHz: return 64000;
994 case HDSP_spdifFrequency88_2KHz: return 88200;
995 case HDSP_spdifFrequency96KHz: return 96000;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100996 case HDSP_spdifFrequency128KHz:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 if (hdsp->io_type == H9632) return 128000;
998 break;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100999 case HDSP_spdifFrequency176_4KHz:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 if (hdsp->io_type == H9632) return 176400;
1001 break;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001002 case HDSP_spdifFrequency192KHz:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 if (hdsp->io_type == H9632) return 192000;
1004 break;
1005 default:
1006 break;
1007 }
1008 snd_printk ("Hammerfall-DSP: unknown spdif frequency status; bits = 0x%x, status = 0x%x\n", rate_bits, status);
1009 return 0;
1010}
1011
Remy Bruno47ba97f2008-02-22 17:57:02 +01001012static int hdsp_external_sample_rate(struct hdsp *hdsp)
1013{
1014 unsigned int status2 = hdsp_read(hdsp, HDSP_status2Register);
1015 unsigned int rate_bits = status2 & HDSP_systemFrequencyMask;
1016
1017 /* For the 9632 card, there seems to be no bit for indicating external
1018 * sample rate greater than 96kHz. The card reports the corresponding
1019 * single speed. So the best means seems to get spdif rate when
1020 * autosync reference is spdif */
1021 if (hdsp->io_type == H9632 &&
1022 hdsp_autosync_ref(hdsp) == HDSP_AUTOSYNC_FROM_SPDIF)
1023 return hdsp_spdif_sample_rate(hdsp);
1024
1025 switch (rate_bits) {
1026 case HDSP_systemFrequency32: return 32000;
1027 case HDSP_systemFrequency44_1: return 44100;
1028 case HDSP_systemFrequency48: return 48000;
1029 case HDSP_systemFrequency64: return 64000;
1030 case HDSP_systemFrequency88_2: return 88200;
1031 case HDSP_systemFrequency96: return 96000;
1032 default:
1033 return 0;
1034 }
1035}
1036
Takashi Iwai55e957d2005-11-17 14:52:13 +01001037static void hdsp_compute_period_size(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038{
1039 hdsp->period_bytes = 1 << ((hdsp_decode_latency(hdsp->control_register) + 8));
1040}
1041
Takashi Iwai55e957d2005-11-17 14:52:13 +01001042static snd_pcm_uframes_t hdsp_hw_pointer(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
1044 int position;
1045
1046 position = hdsp_read(hdsp, HDSP_statusRegister);
1047
Takashi Iwaib0b98112005-10-20 18:29:58 +02001048 if (!hdsp->precise_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 return (position & HDSP_BufferID) ? (hdsp->period_bytes / 4) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 position &= HDSP_BufferPositionMask;
1052 position /= 4;
1053 position &= (hdsp->period_bytes/2) - 1;
1054 return position;
1055}
1056
Takashi Iwai55e957d2005-11-17 14:52:13 +01001057static void hdsp_reset_hw_pointer(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058{
1059 hdsp_write (hdsp, HDSP_resetPointer, 0);
Remy Brunod7923b22006-10-17 12:41:56 +02001060 if (hdsp->io_type == H9632 && hdsp->firmware_rev >= 152)
1061 /* HDSP_resetPointer = HDSP_freqReg, which is strange and
1062 * requires (?) to write again DDS value after a reset pointer
1063 * (at least, it works like this) */
1064 hdsp_write (hdsp, HDSP_freqReg, hdsp->dds_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065}
1066
Takashi Iwai55e957d2005-11-17 14:52:13 +01001067static void hdsp_start_audio(struct hdsp *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
1069 s->control_register |= (HDSP_AudioInterruptEnable | HDSP_Start);
1070 hdsp_write(s, HDSP_controlRegister, s->control_register);
1071}
1072
Takashi Iwai55e957d2005-11-17 14:52:13 +01001073static void hdsp_stop_audio(struct hdsp *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
1075 s->control_register &= ~(HDSP_Start | HDSP_AudioInterruptEnable);
1076 hdsp_write(s, HDSP_controlRegister, s->control_register);
1077}
1078
Takashi Iwai55e957d2005-11-17 14:52:13 +01001079static void hdsp_silence_playback(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
1081 memset(hdsp->playback_buffer, 0, HDSP_DMA_AREA_BYTES);
1082}
1083
Takashi Iwai55e957d2005-11-17 14:52:13 +01001084static int hdsp_set_interrupt_interval(struct hdsp *s, unsigned int frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085{
1086 int n;
1087
1088 spin_lock_irq(&s->lock);
1089
1090 frames >>= 7;
1091 n = 0;
1092 while (frames) {
1093 n++;
1094 frames >>= 1;
1095 }
1096
1097 s->control_register &= ~HDSP_LatencyMask;
1098 s->control_register |= hdsp_encode_latency(n);
1099
1100 hdsp_write(s, HDSP_controlRegister, s->control_register);
1101
1102 hdsp_compute_period_size(s);
1103
1104 spin_unlock_irq(&s->lock);
1105
1106 return 0;
1107}
1108
Remy Brunod7923b22006-10-17 12:41:56 +02001109static void hdsp_set_dds_value(struct hdsp *hdsp, int rate)
1110{
1111 u64 n;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001112
Remy Brunod7923b22006-10-17 12:41:56 +02001113 if (rate >= 112000)
1114 rate /= 4;
1115 else if (rate >= 56000)
1116 rate /= 2;
1117
Julian Cablee4b60882007-03-19 11:44:40 +01001118 n = DDS_NUMERATOR;
Takashi Iwai3f7440a2009-06-05 17:40:04 +02001119 n = div_u64(n, rate);
Remy Brunod7923b22006-10-17 12:41:56 +02001120 /* n should be less than 2^32 for being written to FREQ register */
Takashi Iwaida3cec32008-08-08 17:12:14 +02001121 snd_BUG_ON(n >> 32);
Remy Brunod7923b22006-10-17 12:41:56 +02001122 /* HDSP_freqReg and HDSP_resetPointer are the same, so keep the DDS
1123 value to write it after a reset */
1124 hdsp->dds_value = n;
1125 hdsp_write(hdsp, HDSP_freqReg, hdsp->dds_value);
1126}
1127
Takashi Iwai55e957d2005-11-17 14:52:13 +01001128static int hdsp_set_rate(struct hdsp *hdsp, int rate, int called_internally)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129{
1130 int reject_if_open = 0;
1131 int current_rate;
1132 int rate_bits;
1133
1134 /* ASSUMPTION: hdsp->lock is either held, or
1135 there is no need for it (e.g. during module
1136 initialization).
1137 */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001138
1139 if (!(hdsp->control_register & HDSP_ClockModeMaster)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 if (called_internally) {
1141 /* request from ctl or card initialization */
Takashi Iwaib0b98112005-10-20 18:29:58 +02001142 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 -07001143 return -1;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001144 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 /* hw_param request while in AutoSync mode */
1146 int external_freq = hdsp_external_sample_rate(hdsp);
1147 int spdif_freq = hdsp_spdif_sample_rate(hdsp);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001148
Takashi Iwaib0b98112005-10-20 18:29:58 +02001149 if ((spdif_freq == external_freq*2) && (hdsp_autosync_ref(hdsp) >= HDSP_AUTOSYNC_FROM_ADAT1))
1150 snd_printk(KERN_INFO "Hammerfall-DSP: Detected ADAT in double speed mode\n");
1151 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 +01001152 snd_printk(KERN_INFO "Hammerfall-DSP: Detected ADAT in quad speed mode\n");
Takashi Iwaib0b98112005-10-20 18:29:58 +02001153 else if (rate != external_freq) {
1154 snd_printk(KERN_INFO "Hammerfall-DSP: No AutoSync source for requested rate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 return -1;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001156 }
1157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 }
1159
1160 current_rate = hdsp->system_sample_rate;
1161
1162 /* Changing from a "single speed" to a "double speed" rate is
1163 not allowed if any substreams are open. This is because
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001164 such a change causes a shift in the location of
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 the DMA buffers and a reduction in the number of available
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001166 buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
1168 Note that a similar but essentially insoluble problem
1169 exists for externally-driven rate changes. All we can do
1170 is to flag rate changes in the read/write routines. */
1171
Takashi Iwaib0b98112005-10-20 18:29:58 +02001172 if (rate > 96000 && hdsp->io_type != H9632)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 return -EINVAL;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001174
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 switch (rate) {
1176 case 32000:
Takashi Iwaib0b98112005-10-20 18:29:58 +02001177 if (current_rate > 48000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 rate_bits = HDSP_Frequency32KHz;
1180 break;
1181 case 44100:
Takashi Iwaib0b98112005-10-20 18:29:58 +02001182 if (current_rate > 48000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 rate_bits = HDSP_Frequency44_1KHz;
1185 break;
1186 case 48000:
Takashi Iwaib0b98112005-10-20 18:29:58 +02001187 if (current_rate > 48000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 rate_bits = HDSP_Frequency48KHz;
1190 break;
1191 case 64000:
Takashi Iwaib0b98112005-10-20 18:29:58 +02001192 if (current_rate <= 48000 || current_rate > 96000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 rate_bits = HDSP_Frequency64KHz;
1195 break;
1196 case 88200:
Takashi Iwaib0b98112005-10-20 18:29:58 +02001197 if (current_rate <= 48000 || current_rate > 96000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 rate_bits = HDSP_Frequency88_2KHz;
1200 break;
1201 case 96000:
Takashi Iwaib0b98112005-10-20 18:29:58 +02001202 if (current_rate <= 48000 || current_rate > 96000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 rate_bits = HDSP_Frequency96KHz;
1205 break;
1206 case 128000:
Takashi Iwaib0b98112005-10-20 18:29:58 +02001207 if (current_rate < 128000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 rate_bits = HDSP_Frequency128KHz;
1210 break;
1211 case 176400:
Takashi Iwaib0b98112005-10-20 18:29:58 +02001212 if (current_rate < 128000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 rate_bits = HDSP_Frequency176_4KHz;
1215 break;
1216 case 192000:
Takashi Iwaib0b98112005-10-20 18:29:58 +02001217 if (current_rate < 128000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 rate_bits = HDSP_Frequency192KHz;
1220 break;
1221 default:
1222 return -EINVAL;
1223 }
1224
1225 if (reject_if_open && (hdsp->capture_pid >= 0 || hdsp->playback_pid >= 0)) {
1226 snd_printk ("Hammerfall-DSP: cannot change speed mode (capture PID = %d, playback PID = %d)\n",
1227 hdsp->capture_pid,
1228 hdsp->playback_pid);
1229 return -EBUSY;
1230 }
1231
1232 hdsp->control_register &= ~HDSP_FrequencyMask;
1233 hdsp->control_register |= rate_bits;
1234 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1235
Remy Brunod7923b22006-10-17 12:41:56 +02001236 /* For HDSP9632 rev 152, need to set DDS value in FREQ register */
1237 if (hdsp->io_type == H9632 && hdsp->firmware_rev >= 152)
1238 hdsp_set_dds_value(hdsp, rate);
1239
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 if (rate >= 128000) {
1241 hdsp->channel_map = channel_map_H9632_qs;
1242 } else if (rate > 48000) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02001243 if (hdsp->io_type == H9632)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 hdsp->channel_map = channel_map_H9632_ds;
Takashi Iwaib0b98112005-10-20 18:29:58 +02001245 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 hdsp->channel_map = channel_map_ds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 } else {
1248 switch (hdsp->io_type) {
Florian Faber28b26e12010-12-01 12:14:47 +01001249 case RPM:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 case Multiface:
1251 hdsp->channel_map = channel_map_mf_ss;
1252 break;
1253 case Digiface:
1254 case H9652:
1255 hdsp->channel_map = channel_map_df_ss;
1256 break;
1257 case H9632:
1258 hdsp->channel_map = channel_map_H9632_ss;
1259 break;
1260 default:
1261 /* should never happen */
1262 break;
1263 }
1264 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 hdsp->system_sample_rate = rate;
1267
1268 return 0;
1269}
1270
1271/*----------------------------------------------------------------------------
1272 MIDI
1273 ----------------------------------------------------------------------------*/
1274
Takashi Iwai55e957d2005-11-17 14:52:13 +01001275static unsigned char snd_hdsp_midi_read_byte (struct hdsp *hdsp, int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276{
1277 /* the hardware already does the relevant bit-mask with 0xff */
Takashi Iwaib0b98112005-10-20 18:29:58 +02001278 if (id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 return hdsp_read(hdsp, HDSP_midiDataIn1);
Takashi Iwaib0b98112005-10-20 18:29:58 +02001280 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 return hdsp_read(hdsp, HDSP_midiDataIn0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282}
1283
Takashi Iwai55e957d2005-11-17 14:52:13 +01001284static void snd_hdsp_midi_write_byte (struct hdsp *hdsp, int id, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285{
1286 /* the hardware already does the relevant bit-mask with 0xff */
Takashi Iwaib0b98112005-10-20 18:29:58 +02001287 if (id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 hdsp_write(hdsp, HDSP_midiDataOut1, val);
Takashi Iwaib0b98112005-10-20 18:29:58 +02001289 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 hdsp_write(hdsp, HDSP_midiDataOut0, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291}
1292
Takashi Iwai55e957d2005-11-17 14:52:13 +01001293static int snd_hdsp_midi_input_available (struct hdsp *hdsp, int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294{
Takashi Iwaib0b98112005-10-20 18:29:58 +02001295 if (id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 return (hdsp_read(hdsp, HDSP_midiStatusIn1) & 0xff);
Takashi Iwaib0b98112005-10-20 18:29:58 +02001297 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 return (hdsp_read(hdsp, HDSP_midiStatusIn0) & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299}
1300
Takashi Iwai55e957d2005-11-17 14:52:13 +01001301static int snd_hdsp_midi_output_possible (struct hdsp *hdsp, int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302{
1303 int fifo_bytes_used;
1304
Takashi Iwaib0b98112005-10-20 18:29:58 +02001305 if (id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 fifo_bytes_used = hdsp_read(hdsp, HDSP_midiStatusOut1) & 0xff;
Takashi Iwaib0b98112005-10-20 18:29:58 +02001307 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 fifo_bytes_used = hdsp_read(hdsp, HDSP_midiStatusOut0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Takashi Iwaib0b98112005-10-20 18:29:58 +02001310 if (fifo_bytes_used < 128)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 return 128 - fifo_bytes_used;
Takashi Iwaib0b98112005-10-20 18:29:58 +02001312 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314}
1315
Takashi Iwai55e957d2005-11-17 14:52:13 +01001316static void snd_hdsp_flush_midi_input (struct hdsp *hdsp, int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317{
Takashi Iwaib0b98112005-10-20 18:29:58 +02001318 while (snd_hdsp_midi_input_available (hdsp, id))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 snd_hdsp_midi_read_byte (hdsp, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320}
1321
Takashi Iwai55e957d2005-11-17 14:52:13 +01001322static int snd_hdsp_midi_output_write (struct hdsp_midi *hmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323{
1324 unsigned long flags;
1325 int n_pending;
1326 int to_write;
1327 int i;
1328 unsigned char buf[128];
1329
1330 /* Output is not interrupt driven */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001331
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 spin_lock_irqsave (&hmidi->lock, flags);
1333 if (hmidi->output) {
1334 if (!snd_rawmidi_transmit_empty (hmidi->output)) {
1335 if ((n_pending = snd_hdsp_midi_output_possible (hmidi->hdsp, hmidi->id)) > 0) {
1336 if (n_pending > (int)sizeof (buf))
1337 n_pending = sizeof (buf);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 if ((to_write = snd_rawmidi_transmit (hmidi->output, buf, n_pending)) > 0) {
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001340 for (i = 0; i < to_write; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 snd_hdsp_midi_write_byte (hmidi->hdsp, hmidi->id, buf[i]);
1342 }
1343 }
1344 }
1345 }
1346 spin_unlock_irqrestore (&hmidi->lock, flags);
1347 return 0;
1348}
1349
Takashi Iwai55e957d2005-11-17 14:52:13 +01001350static int snd_hdsp_midi_input_read (struct hdsp_midi *hmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
1352 unsigned char buf[128]; /* this buffer is designed to match the MIDI input FIFO size */
1353 unsigned long flags;
1354 int n_pending;
1355 int i;
1356
1357 spin_lock_irqsave (&hmidi->lock, flags);
1358 if ((n_pending = snd_hdsp_midi_input_available (hmidi->hdsp, hmidi->id)) > 0) {
1359 if (hmidi->input) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02001360 if (n_pending > (int)sizeof (buf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 n_pending = sizeof (buf);
Takashi Iwaib0b98112005-10-20 18:29:58 +02001362 for (i = 0; i < n_pending; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 buf[i] = snd_hdsp_midi_read_byte (hmidi->hdsp, hmidi->id);
Takashi Iwaib0b98112005-10-20 18:29:58 +02001364 if (n_pending)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 snd_rawmidi_receive (hmidi->input, buf, n_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 } else {
1367 /* flush the MIDI input FIFO */
Takashi Iwaib0b98112005-10-20 18:29:58 +02001368 while (--n_pending)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 snd_hdsp_midi_read_byte (hmidi->hdsp, hmidi->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 }
1371 }
1372 hmidi->pending = 0;
Takashi Iwaib0b98112005-10-20 18:29:58 +02001373 if (hmidi->id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 hmidi->hdsp->control_register |= HDSP_Midi1InterruptEnable;
Takashi Iwaib0b98112005-10-20 18:29:58 +02001375 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 hmidi->hdsp->control_register |= HDSP_Midi0InterruptEnable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 hdsp_write(hmidi->hdsp, HDSP_controlRegister, hmidi->hdsp->control_register);
1378 spin_unlock_irqrestore (&hmidi->lock, flags);
1379 return snd_hdsp_midi_output_write (hmidi);
1380}
1381
Takashi Iwai55e957d2005-11-17 14:52:13 +01001382static void snd_hdsp_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001384 struct hdsp *hdsp;
1385 struct hdsp_midi *hmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 unsigned long flags;
1387 u32 ie;
1388
Takashi Iwai55e957d2005-11-17 14:52:13 +01001389 hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 hdsp = hmidi->hdsp;
1391 ie = hmidi->id ? HDSP_Midi1InterruptEnable : HDSP_Midi0InterruptEnable;
1392 spin_lock_irqsave (&hdsp->lock, flags);
1393 if (up) {
1394 if (!(hdsp->control_register & ie)) {
1395 snd_hdsp_flush_midi_input (hdsp, hmidi->id);
1396 hdsp->control_register |= ie;
1397 }
1398 } else {
1399 hdsp->control_register &= ~ie;
1400 tasklet_kill(&hdsp->midi_tasklet);
1401 }
1402
1403 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1404 spin_unlock_irqrestore (&hdsp->lock, flags);
1405}
1406
1407static void snd_hdsp_midi_output_timer(unsigned long data)
1408{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001409 struct hdsp_midi *hmidi = (struct hdsp_midi *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 unsigned long flags;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 snd_hdsp_midi_output_write(hmidi);
1413 spin_lock_irqsave (&hmidi->lock, flags);
1414
1415 /* this does not bump hmidi->istimer, because the
1416 kernel automatically removed the timer when it
1417 expired, and we are now adding it back, thus
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001418 leaving istimer wherever it was set before.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 */
1420
1421 if (hmidi->istimer) {
1422 hmidi->timer.expires = 1 + jiffies;
1423 add_timer(&hmidi->timer);
1424 }
1425
1426 spin_unlock_irqrestore (&hmidi->lock, flags);
1427}
1428
Takashi Iwai55e957d2005-11-17 14:52:13 +01001429static void snd_hdsp_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001431 struct hdsp_midi *hmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 unsigned long flags;
1433
Takashi Iwai55e957d2005-11-17 14:52:13 +01001434 hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 spin_lock_irqsave (&hmidi->lock, flags);
1436 if (up) {
1437 if (!hmidi->istimer) {
1438 init_timer(&hmidi->timer);
1439 hmidi->timer.function = snd_hdsp_midi_output_timer;
1440 hmidi->timer.data = (unsigned long) hmidi;
1441 hmidi->timer.expires = 1 + jiffies;
1442 add_timer(&hmidi->timer);
1443 hmidi->istimer++;
1444 }
1445 } else {
Takashi Iwaib0b98112005-10-20 18:29:58 +02001446 if (hmidi->istimer && --hmidi->istimer <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 del_timer (&hmidi->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 }
1449 spin_unlock_irqrestore (&hmidi->lock, flags);
1450 if (up)
1451 snd_hdsp_midi_output_write(hmidi);
1452}
1453
Takashi Iwai55e957d2005-11-17 14:52:13 +01001454static int snd_hdsp_midi_input_open(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001456 struct hdsp_midi *hmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
Takashi Iwai55e957d2005-11-17 14:52:13 +01001458 hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 spin_lock_irq (&hmidi->lock);
1460 snd_hdsp_flush_midi_input (hmidi->hdsp, hmidi->id);
1461 hmidi->input = substream;
1462 spin_unlock_irq (&hmidi->lock);
1463
1464 return 0;
1465}
1466
Takashi Iwai55e957d2005-11-17 14:52:13 +01001467static int snd_hdsp_midi_output_open(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001469 struct hdsp_midi *hmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Takashi Iwai55e957d2005-11-17 14:52:13 +01001471 hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 spin_lock_irq (&hmidi->lock);
1473 hmidi->output = substream;
1474 spin_unlock_irq (&hmidi->lock);
1475
1476 return 0;
1477}
1478
Takashi Iwai55e957d2005-11-17 14:52:13 +01001479static int snd_hdsp_midi_input_close(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001481 struct hdsp_midi *hmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
1483 snd_hdsp_midi_input_trigger (substream, 0);
1484
Takashi Iwai55e957d2005-11-17 14:52:13 +01001485 hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 spin_lock_irq (&hmidi->lock);
1487 hmidi->input = NULL;
1488 spin_unlock_irq (&hmidi->lock);
1489
1490 return 0;
1491}
1492
Takashi Iwai55e957d2005-11-17 14:52:13 +01001493static int snd_hdsp_midi_output_close(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001495 struct hdsp_midi *hmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
1497 snd_hdsp_midi_output_trigger (substream, 0);
1498
Takashi Iwai55e957d2005-11-17 14:52:13 +01001499 hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 spin_lock_irq (&hmidi->lock);
1501 hmidi->output = NULL;
1502 spin_unlock_irq (&hmidi->lock);
1503
1504 return 0;
1505}
1506
Takashi Iwai55e957d2005-11-17 14:52:13 +01001507static struct snd_rawmidi_ops snd_hdsp_midi_output =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508{
1509 .open = snd_hdsp_midi_output_open,
1510 .close = snd_hdsp_midi_output_close,
1511 .trigger = snd_hdsp_midi_output_trigger,
1512};
1513
Takashi Iwai55e957d2005-11-17 14:52:13 +01001514static struct snd_rawmidi_ops snd_hdsp_midi_input =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515{
1516 .open = snd_hdsp_midi_input_open,
1517 .close = snd_hdsp_midi_input_close,
1518 .trigger = snd_hdsp_midi_input_trigger,
1519};
1520
Takashi Iwaif40b6892006-07-05 16:51:05 +02001521static int snd_hdsp_create_midi (struct snd_card *card, struct hdsp *hdsp, int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522{
1523 char buf[32];
1524
1525 hdsp->midi[id].id = id;
1526 hdsp->midi[id].rmidi = NULL;
1527 hdsp->midi[id].input = NULL;
1528 hdsp->midi[id].output = NULL;
1529 hdsp->midi[id].hdsp = hdsp;
1530 hdsp->midi[id].istimer = 0;
1531 hdsp->midi[id].pending = 0;
1532 spin_lock_init (&hdsp->midi[id].lock);
1533
1534 sprintf (buf, "%s MIDI %d", card->shortname, id+1);
Takashi Iwaib0b98112005-10-20 18:29:58 +02001535 if (snd_rawmidi_new (card, buf, id, 1, 1, &hdsp->midi[id].rmidi) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
Jaroslav Kysela972d4c52008-11-12 16:37:48 +01001538 sprintf(hdsp->midi[id].rmidi->name, "HDSP MIDI %d", id+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 hdsp->midi[id].rmidi->private_data = &hdsp->midi[id];
1540
1541 snd_rawmidi_set_ops (hdsp->midi[id].rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_hdsp_midi_output);
1542 snd_rawmidi_set_ops (hdsp->midi[id].rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_hdsp_midi_input);
1543
1544 hdsp->midi[id].rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT |
1545 SNDRV_RAWMIDI_INFO_INPUT |
1546 SNDRV_RAWMIDI_INFO_DUPLEX;
1547
1548 return 0;
1549}
1550
1551/*-----------------------------------------------------------------------------
1552 Control Interface
1553 ----------------------------------------------------------------------------*/
1554
Takashi Iwai55e957d2005-11-17 14:52:13 +01001555static u32 snd_hdsp_convert_from_aes(struct snd_aes_iec958 *aes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556{
1557 u32 val = 0;
1558 val |= (aes->status[0] & IEC958_AES0_PROFESSIONAL) ? HDSP_SPDIFProfessional : 0;
1559 val |= (aes->status[0] & IEC958_AES0_NONAUDIO) ? HDSP_SPDIFNonAudio : 0;
1560 if (val & HDSP_SPDIFProfessional)
1561 val |= (aes->status[0] & IEC958_AES0_PRO_EMPHASIS_5015) ? HDSP_SPDIFEmphasis : 0;
1562 else
1563 val |= (aes->status[0] & IEC958_AES0_CON_EMPHASIS_5015) ? HDSP_SPDIFEmphasis : 0;
1564 return val;
1565}
1566
Takashi Iwai55e957d2005-11-17 14:52:13 +01001567static void snd_hdsp_convert_to_aes(struct snd_aes_iec958 *aes, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568{
1569 aes->status[0] = ((val & HDSP_SPDIFProfessional) ? IEC958_AES0_PROFESSIONAL : 0) |
1570 ((val & HDSP_SPDIFNonAudio) ? IEC958_AES0_NONAUDIO : 0);
1571 if (val & HDSP_SPDIFProfessional)
1572 aes->status[0] |= (val & HDSP_SPDIFEmphasis) ? IEC958_AES0_PRO_EMPHASIS_5015 : 0;
1573 else
1574 aes->status[0] |= (val & HDSP_SPDIFEmphasis) ? IEC958_AES0_CON_EMPHASIS_5015 : 0;
1575}
1576
Takashi Iwai55e957d2005-11-17 14:52:13 +01001577static int snd_hdsp_control_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578{
1579 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1580 uinfo->count = 1;
1581 return 0;
1582}
1583
Takashi Iwai55e957d2005-11-17 14:52:13 +01001584static int snd_hdsp_control_spdif_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001586 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001587
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 snd_hdsp_convert_to_aes(&ucontrol->value.iec958, hdsp->creg_spdif);
1589 return 0;
1590}
1591
Takashi Iwai55e957d2005-11-17 14:52:13 +01001592static int snd_hdsp_control_spdif_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001594 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 int change;
1596 u32 val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001597
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 val = snd_hdsp_convert_from_aes(&ucontrol->value.iec958);
1599 spin_lock_irq(&hdsp->lock);
1600 change = val != hdsp->creg_spdif;
1601 hdsp->creg_spdif = val;
1602 spin_unlock_irq(&hdsp->lock);
1603 return change;
1604}
1605
Takashi Iwai55e957d2005-11-17 14:52:13 +01001606static int snd_hdsp_control_spdif_stream_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607{
1608 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1609 uinfo->count = 1;
1610 return 0;
1611}
1612
Takashi Iwai55e957d2005-11-17 14:52:13 +01001613static int snd_hdsp_control_spdif_stream_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001615 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001616
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 snd_hdsp_convert_to_aes(&ucontrol->value.iec958, hdsp->creg_spdif_stream);
1618 return 0;
1619}
1620
Takashi Iwai55e957d2005-11-17 14:52:13 +01001621static int snd_hdsp_control_spdif_stream_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001623 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 int change;
1625 u32 val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001626
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 val = snd_hdsp_convert_from_aes(&ucontrol->value.iec958);
1628 spin_lock_irq(&hdsp->lock);
1629 change = val != hdsp->creg_spdif_stream;
1630 hdsp->creg_spdif_stream = val;
1631 hdsp->control_register &= ~(HDSP_SPDIFProfessional | HDSP_SPDIFNonAudio | HDSP_SPDIFEmphasis);
1632 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register |= val);
1633 spin_unlock_irq(&hdsp->lock);
1634 return change;
1635}
1636
Takashi Iwai55e957d2005-11-17 14:52:13 +01001637static int snd_hdsp_control_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638{
1639 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1640 uinfo->count = 1;
1641 return 0;
1642}
1643
Takashi Iwai55e957d2005-11-17 14:52:13 +01001644static int snd_hdsp_control_spdif_mask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645{
1646 ucontrol->value.iec958.status[0] = kcontrol->private_value;
1647 return 0;
1648}
1649
1650#define HDSP_SPDIF_IN(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001651{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 .name = xname, \
1653 .index = xindex, \
1654 .info = snd_hdsp_info_spdif_in, \
1655 .get = snd_hdsp_get_spdif_in, \
1656 .put = snd_hdsp_put_spdif_in }
1657
Takashi Iwai55e957d2005-11-17 14:52:13 +01001658static unsigned int hdsp_spdif_in(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659{
1660 return hdsp_decode_spdif_in(hdsp->control_register & HDSP_SPDIFInputMask);
1661}
1662
Takashi Iwai55e957d2005-11-17 14:52:13 +01001663static int hdsp_set_spdif_input(struct hdsp *hdsp, int in)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664{
1665 hdsp->control_register &= ~HDSP_SPDIFInputMask;
1666 hdsp->control_register |= hdsp_encode_spdif_in(in);
1667 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1668 return 0;
1669}
1670
Takashi Iwai55e957d2005-11-17 14:52:13 +01001671static int snd_hdsp_info_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672{
1673 static char *texts[4] = {"Optical", "Coaxial", "Internal", "AES"};
Takashi Iwai55e957d2005-11-17 14:52:13 +01001674 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
1676 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1677 uinfo->count = 1;
1678 uinfo->value.enumerated.items = ((hdsp->io_type == H9632) ? 4 : 3);
1679 if (uinfo->value.enumerated.item > ((hdsp->io_type == H9632) ? 3 : 2))
1680 uinfo->value.enumerated.item = ((hdsp->io_type == H9632) ? 3 : 2);
1681 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1682 return 0;
1683}
1684
Takashi Iwai55e957d2005-11-17 14:52:13 +01001685static int snd_hdsp_get_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001687 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001688
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 ucontrol->value.enumerated.item[0] = hdsp_spdif_in(hdsp);
1690 return 0;
1691}
1692
Takashi Iwai55e957d2005-11-17 14:52:13 +01001693static int snd_hdsp_put_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001695 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 int change;
1697 unsigned int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001698
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 if (!snd_hdsp_use_is_exclusive(hdsp))
1700 return -EBUSY;
1701 val = ucontrol->value.enumerated.item[0] % ((hdsp->io_type == H9632) ? 4 : 3);
1702 spin_lock_irq(&hdsp->lock);
1703 change = val != hdsp_spdif_in(hdsp);
1704 if (change)
1705 hdsp_set_spdif_input(hdsp, val);
1706 spin_unlock_irq(&hdsp->lock);
1707 return change;
1708}
1709
Adrian Knoth66d92442013-01-15 18:52:21 +01001710#define HDSP_TOGGLE_SETTING(xname, xindex) \
1711{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1712 .name = xname, \
1713 .private_value = xindex, \
1714 .info = snd_hdsp_info_toggle_setting, \
1715 .get = snd_hdsp_get_toggle_setting, \
1716 .put = snd_hdsp_put_toggle_setting \
1717}
1718
1719static int hdsp_toggle_setting(struct hdsp *hdsp, u32 regmask)
1720{
1721 return (hdsp->control_register & regmask) ? 1 : 0;
1722}
1723
1724static int hdsp_set_toggle_setting(struct hdsp *hdsp, u32 regmask, int out)
1725{
1726 if (out)
1727 hdsp->control_register |= regmask;
1728 else
1729 hdsp->control_register &= ~regmask;
1730 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1731
1732 return 0;
1733}
1734
1735#define snd_hdsp_info_toggle_setting snd_ctl_boolean_mono_info
1736
1737static int snd_hdsp_get_toggle_setting(struct snd_kcontrol *kcontrol,
1738 struct snd_ctl_elem_value *ucontrol)
1739{
1740 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
1741 u32 regmask = kcontrol->private_value;
1742
1743 spin_lock_irq(&hdsp->lock);
1744 ucontrol->value.integer.value[0] = hdsp_toggle_setting(hdsp, regmask);
1745 spin_unlock_irq(&hdsp->lock);
1746 return 0;
1747}
1748
1749static int snd_hdsp_put_toggle_setting(struct snd_kcontrol *kcontrol,
1750 struct snd_ctl_elem_value *ucontrol)
1751{
1752 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
1753 u32 regmask = kcontrol->private_value;
1754 int change;
1755 unsigned int val;
1756
1757 if (!snd_hdsp_use_is_exclusive(hdsp))
1758 return -EBUSY;
1759 val = ucontrol->value.integer.value[0] & 1;
1760 spin_lock_irq(&hdsp->lock);
1761 change = (int) val != hdsp_toggle_setting(hdsp, regmask);
1762 if (change)
1763 hdsp_set_toggle_setting(hdsp, regmask, val);
1764 spin_unlock_irq(&hdsp->lock);
1765 return change;
1766}
1767
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768#define HDSP_SPDIF_SAMPLE_RATE(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001769{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 .name = xname, \
1771 .index = xindex, \
1772 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
1773 .info = snd_hdsp_info_spdif_sample_rate, \
1774 .get = snd_hdsp_get_spdif_sample_rate \
1775}
1776
Takashi Iwai55e957d2005-11-17 14:52:13 +01001777static int snd_hdsp_info_spdif_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778{
1779 static char *texts[] = {"32000", "44100", "48000", "64000", "88200", "96000", "None", "128000", "176400", "192000"};
Takashi Iwai55e957d2005-11-17 14:52:13 +01001780 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
1782 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1783 uinfo->count = 1;
1784 uinfo->value.enumerated.items = (hdsp->io_type == H9632) ? 10 : 7;
1785 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1786 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
1787 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1788 return 0;
1789}
1790
Takashi Iwai55e957d2005-11-17 14:52:13 +01001791static int snd_hdsp_get_spdif_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001793 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001794
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 switch (hdsp_spdif_sample_rate(hdsp)) {
1796 case 32000:
1797 ucontrol->value.enumerated.item[0] = 0;
1798 break;
1799 case 44100:
1800 ucontrol->value.enumerated.item[0] = 1;
1801 break;
1802 case 48000:
1803 ucontrol->value.enumerated.item[0] = 2;
1804 break;
1805 case 64000:
1806 ucontrol->value.enumerated.item[0] = 3;
1807 break;
1808 case 88200:
1809 ucontrol->value.enumerated.item[0] = 4;
1810 break;
1811 case 96000:
1812 ucontrol->value.enumerated.item[0] = 5;
1813 break;
1814 case 128000:
1815 ucontrol->value.enumerated.item[0] = 7;
1816 break;
1817 case 176400:
1818 ucontrol->value.enumerated.item[0] = 8;
1819 break;
1820 case 192000:
1821 ucontrol->value.enumerated.item[0] = 9;
1822 break;
1823 default:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001824 ucontrol->value.enumerated.item[0] = 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 }
1826 return 0;
1827}
1828
1829#define HDSP_SYSTEM_SAMPLE_RATE(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001830{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 .name = xname, \
1832 .index = xindex, \
1833 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
1834 .info = snd_hdsp_info_system_sample_rate, \
1835 .get = snd_hdsp_get_system_sample_rate \
1836}
1837
Takashi Iwai55e957d2005-11-17 14:52:13 +01001838static int snd_hdsp_info_system_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839{
1840 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1841 uinfo->count = 1;
1842 return 0;
1843}
1844
Takashi Iwai55e957d2005-11-17 14:52:13 +01001845static int snd_hdsp_get_system_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001847 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001848
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 ucontrol->value.enumerated.item[0] = hdsp->system_sample_rate;
1850 return 0;
1851}
1852
1853#define HDSP_AUTOSYNC_SAMPLE_RATE(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001854{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 .name = xname, \
1856 .index = xindex, \
1857 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
1858 .info = snd_hdsp_info_autosync_sample_rate, \
1859 .get = snd_hdsp_get_autosync_sample_rate \
1860}
1861
Takashi Iwai55e957d2005-11-17 14:52:13 +01001862static int snd_hdsp_info_autosync_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001864 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001865 static char *texts[] = {"32000", "44100", "48000", "64000", "88200", "96000", "None", "128000", "176400", "192000"};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1867 uinfo->count = 1;
1868 uinfo->value.enumerated.items = (hdsp->io_type == H9632) ? 10 : 7 ;
1869 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1870 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
1871 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1872 return 0;
1873}
1874
Takashi Iwai55e957d2005-11-17 14:52:13 +01001875static int snd_hdsp_get_autosync_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001877 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001878
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 switch (hdsp_external_sample_rate(hdsp)) {
1880 case 32000:
1881 ucontrol->value.enumerated.item[0] = 0;
1882 break;
1883 case 44100:
1884 ucontrol->value.enumerated.item[0] = 1;
1885 break;
1886 case 48000:
1887 ucontrol->value.enumerated.item[0] = 2;
1888 break;
1889 case 64000:
1890 ucontrol->value.enumerated.item[0] = 3;
1891 break;
1892 case 88200:
1893 ucontrol->value.enumerated.item[0] = 4;
1894 break;
1895 case 96000:
1896 ucontrol->value.enumerated.item[0] = 5;
1897 break;
1898 case 128000:
1899 ucontrol->value.enumerated.item[0] = 7;
1900 break;
1901 case 176400:
1902 ucontrol->value.enumerated.item[0] = 8;
1903 break;
1904 case 192000:
1905 ucontrol->value.enumerated.item[0] = 9;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001906 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 default:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001908 ucontrol->value.enumerated.item[0] = 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 }
1910 return 0;
1911}
1912
1913#define HDSP_SYSTEM_CLOCK_MODE(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001914{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 .name = xname, \
1916 .index = xindex, \
1917 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
1918 .info = snd_hdsp_info_system_clock_mode, \
1919 .get = snd_hdsp_get_system_clock_mode \
1920}
1921
Takashi Iwai55e957d2005-11-17 14:52:13 +01001922static int hdsp_system_clock_mode(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923{
Takashi Iwaib0b98112005-10-20 18:29:58 +02001924 if (hdsp->control_register & HDSP_ClockModeMaster)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 return 0;
Takashi Iwaib0b98112005-10-20 18:29:58 +02001926 else if (hdsp_external_sample_rate(hdsp) != hdsp->system_sample_rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 return 1;
1929}
1930
Takashi Iwai55e957d2005-11-17 14:52:13 +01001931static int snd_hdsp_info_system_clock_mode(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932{
1933 static char *texts[] = {"Master", "Slave" };
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001934
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1936 uinfo->count = 1;
1937 uinfo->value.enumerated.items = 2;
1938 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1939 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
1940 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1941 return 0;
1942}
1943
Takashi Iwai55e957d2005-11-17 14:52:13 +01001944static int snd_hdsp_get_system_clock_mode(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001946 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001947
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 ucontrol->value.enumerated.item[0] = hdsp_system_clock_mode(hdsp);
1949 return 0;
1950}
1951
1952#define HDSP_CLOCK_SOURCE(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001953{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 .name = xname, \
1955 .index = xindex, \
1956 .info = snd_hdsp_info_clock_source, \
1957 .get = snd_hdsp_get_clock_source, \
1958 .put = snd_hdsp_put_clock_source \
1959}
1960
Takashi Iwai55e957d2005-11-17 14:52:13 +01001961static int hdsp_clock_source(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962{
1963 if (hdsp->control_register & HDSP_ClockModeMaster) {
1964 switch (hdsp->system_sample_rate) {
1965 case 32000:
1966 return 1;
1967 case 44100:
1968 return 2;
1969 case 48000:
1970 return 3;
1971 case 64000:
1972 return 4;
1973 case 88200:
1974 return 5;
1975 case 96000:
1976 return 6;
1977 case 128000:
1978 return 7;
1979 case 176400:
1980 return 8;
1981 case 192000:
1982 return 9;
1983 default:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001984 return 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 }
1986 } else {
1987 return 0;
1988 }
1989}
1990
Takashi Iwai55e957d2005-11-17 14:52:13 +01001991static int hdsp_set_clock_source(struct hdsp *hdsp, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992{
1993 int rate;
1994 switch (mode) {
1995 case HDSP_CLOCK_SOURCE_AUTOSYNC:
1996 if (hdsp_external_sample_rate(hdsp) != 0) {
1997 if (!hdsp_set_rate(hdsp, hdsp_external_sample_rate(hdsp), 1)) {
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001998 hdsp->control_register &= ~HDSP_ClockModeMaster;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2000 return 0;
2001 }
2002 }
2003 return -1;
2004 case HDSP_CLOCK_SOURCE_INTERNAL_32KHZ:
2005 rate = 32000;
2006 break;
2007 case HDSP_CLOCK_SOURCE_INTERNAL_44_1KHZ:
2008 rate = 44100;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002009 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 case HDSP_CLOCK_SOURCE_INTERNAL_48KHZ:
2011 rate = 48000;
2012 break;
2013 case HDSP_CLOCK_SOURCE_INTERNAL_64KHZ:
2014 rate = 64000;
2015 break;
2016 case HDSP_CLOCK_SOURCE_INTERNAL_88_2KHZ:
2017 rate = 88200;
2018 break;
2019 case HDSP_CLOCK_SOURCE_INTERNAL_96KHZ:
2020 rate = 96000;
2021 break;
2022 case HDSP_CLOCK_SOURCE_INTERNAL_128KHZ:
2023 rate = 128000;
2024 break;
2025 case HDSP_CLOCK_SOURCE_INTERNAL_176_4KHZ:
2026 rate = 176400;
2027 break;
2028 case HDSP_CLOCK_SOURCE_INTERNAL_192KHZ:
2029 rate = 192000;
2030 break;
2031 default:
2032 rate = 48000;
2033 }
2034 hdsp->control_register |= HDSP_ClockModeMaster;
2035 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2036 hdsp_set_rate(hdsp, rate, 1);
2037 return 0;
2038}
2039
Takashi Iwai55e957d2005-11-17 14:52:13 +01002040static int snd_hdsp_info_clock_source(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041{
2042 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 +01002043 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002044
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2046 uinfo->count = 1;
2047 if (hdsp->io_type == H9632)
2048 uinfo->value.enumerated.items = 10;
2049 else
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002050 uinfo->value.enumerated.items = 7;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2052 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2053 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2054 return 0;
2055}
2056
Takashi Iwai55e957d2005-11-17 14:52:13 +01002057static int snd_hdsp_get_clock_source(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002059 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002060
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 ucontrol->value.enumerated.item[0] = hdsp_clock_source(hdsp);
2062 return 0;
2063}
2064
Takashi Iwai55e957d2005-11-17 14:52:13 +01002065static int snd_hdsp_put_clock_source(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002067 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 int change;
2069 int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002070
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 if (!snd_hdsp_use_is_exclusive(hdsp))
2072 return -EBUSY;
2073 val = ucontrol->value.enumerated.item[0];
2074 if (val < 0) val = 0;
2075 if (hdsp->io_type == H9632) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02002076 if (val > 9)
2077 val = 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 } else {
Takashi Iwaib0b98112005-10-20 18:29:58 +02002079 if (val > 6)
2080 val = 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 }
2082 spin_lock_irq(&hdsp->lock);
Takashi Iwaib0b98112005-10-20 18:29:58 +02002083 if (val != hdsp_clock_source(hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 change = (hdsp_set_clock_source(hdsp, val) == 0) ? 1 : 0;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002085 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 spin_unlock_irq(&hdsp->lock);
2088 return change;
2089}
2090
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002091#define snd_hdsp_info_clock_source_lock snd_ctl_boolean_mono_info
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02002092
Takashi Iwai55e957d2005-11-17 14:52:13 +01002093static int snd_hdsp_get_clock_source_lock(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02002094{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002095 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002096
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02002097 ucontrol->value.integer.value[0] = hdsp->clock_source_locked;
2098 return 0;
2099}
2100
Takashi Iwai55e957d2005-11-17 14:52:13 +01002101static int snd_hdsp_put_clock_source_lock(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02002102{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002103 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02002104 int change;
2105
2106 change = (int)ucontrol->value.integer.value[0] != hdsp->clock_source_locked;
2107 if (change)
Takashi Iwai4e98d6a2007-11-15 15:58:13 +01002108 hdsp->clock_source_locked = !!ucontrol->value.integer.value[0];
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02002109 return change;
2110}
2111
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112#define HDSP_DA_GAIN(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002113{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 .name = xname, \
2115 .index = xindex, \
2116 .info = snd_hdsp_info_da_gain, \
2117 .get = snd_hdsp_get_da_gain, \
2118 .put = snd_hdsp_put_da_gain \
2119}
2120
Takashi Iwai55e957d2005-11-17 14:52:13 +01002121static int hdsp_da_gain(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122{
2123 switch (hdsp->control_register & HDSP_DAGainMask) {
2124 case HDSP_DAGainHighGain:
2125 return 0;
2126 case HDSP_DAGainPlus4dBu:
2127 return 1;
2128 case HDSP_DAGainMinus10dBV:
2129 return 2;
2130 default:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002131 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 }
2133}
2134
Takashi Iwai55e957d2005-11-17 14:52:13 +01002135static int hdsp_set_da_gain(struct hdsp *hdsp, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136{
2137 hdsp->control_register &= ~HDSP_DAGainMask;
2138 switch (mode) {
2139 case 0:
2140 hdsp->control_register |= HDSP_DAGainHighGain;
2141 break;
2142 case 1:
2143 hdsp->control_register |= HDSP_DAGainPlus4dBu;
2144 break;
2145 case 2:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002146 hdsp->control_register |= HDSP_DAGainMinus10dBV;
2147 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 default:
2149 return -1;
2150
2151 }
2152 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2153 return 0;
2154}
2155
Takashi Iwai55e957d2005-11-17 14:52:13 +01002156static int snd_hdsp_info_da_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157{
2158 static char *texts[] = {"Hi Gain", "+4 dBu", "-10 dbV"};
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002159
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2161 uinfo->count = 1;
2162 uinfo->value.enumerated.items = 3;
2163 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2164 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2165 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2166 return 0;
2167}
2168
Takashi Iwai55e957d2005-11-17 14:52:13 +01002169static int snd_hdsp_get_da_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002171 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002172
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 ucontrol->value.enumerated.item[0] = hdsp_da_gain(hdsp);
2174 return 0;
2175}
2176
Takashi Iwai55e957d2005-11-17 14:52:13 +01002177static int snd_hdsp_put_da_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002179 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 int change;
2181 int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002182
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 if (!snd_hdsp_use_is_exclusive(hdsp))
2184 return -EBUSY;
2185 val = ucontrol->value.enumerated.item[0];
2186 if (val < 0) val = 0;
2187 if (val > 2) val = 2;
2188 spin_lock_irq(&hdsp->lock);
Takashi Iwaib0b98112005-10-20 18:29:58 +02002189 if (val != hdsp_da_gain(hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 change = (hdsp_set_da_gain(hdsp, val) == 0) ? 1 : 0;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002191 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 spin_unlock_irq(&hdsp->lock);
2194 return change;
2195}
2196
2197#define HDSP_AD_GAIN(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002198{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 .name = xname, \
2200 .index = xindex, \
2201 .info = snd_hdsp_info_ad_gain, \
2202 .get = snd_hdsp_get_ad_gain, \
2203 .put = snd_hdsp_put_ad_gain \
2204}
2205
Takashi Iwai55e957d2005-11-17 14:52:13 +01002206static int hdsp_ad_gain(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207{
2208 switch (hdsp->control_register & HDSP_ADGainMask) {
2209 case HDSP_ADGainMinus10dBV:
2210 return 0;
2211 case HDSP_ADGainPlus4dBu:
2212 return 1;
2213 case HDSP_ADGainLowGain:
2214 return 2;
2215 default:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002216 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 }
2218}
2219
Takashi Iwai55e957d2005-11-17 14:52:13 +01002220static int hdsp_set_ad_gain(struct hdsp *hdsp, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221{
2222 hdsp->control_register &= ~HDSP_ADGainMask;
2223 switch (mode) {
2224 case 0:
2225 hdsp->control_register |= HDSP_ADGainMinus10dBV;
2226 break;
2227 case 1:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002228 hdsp->control_register |= HDSP_ADGainPlus4dBu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 break;
2230 case 2:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002231 hdsp->control_register |= HDSP_ADGainLowGain;
2232 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 default:
2234 return -1;
2235
2236 }
2237 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2238 return 0;
2239}
2240
Takashi Iwai55e957d2005-11-17 14:52:13 +01002241static int snd_hdsp_info_ad_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242{
2243 static char *texts[] = {"-10 dBV", "+4 dBu", "Lo Gain"};
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002244
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2246 uinfo->count = 1;
2247 uinfo->value.enumerated.items = 3;
2248 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2249 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2250 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2251 return 0;
2252}
2253
Takashi Iwai55e957d2005-11-17 14:52:13 +01002254static int snd_hdsp_get_ad_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002256 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002257
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 ucontrol->value.enumerated.item[0] = hdsp_ad_gain(hdsp);
2259 return 0;
2260}
2261
Takashi Iwai55e957d2005-11-17 14:52:13 +01002262static int snd_hdsp_put_ad_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002264 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 int change;
2266 int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002267
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 if (!snd_hdsp_use_is_exclusive(hdsp))
2269 return -EBUSY;
2270 val = ucontrol->value.enumerated.item[0];
2271 if (val < 0) val = 0;
2272 if (val > 2) val = 2;
2273 spin_lock_irq(&hdsp->lock);
Takashi Iwaib0b98112005-10-20 18:29:58 +02002274 if (val != hdsp_ad_gain(hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 change = (hdsp_set_ad_gain(hdsp, val) == 0) ? 1 : 0;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002276 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 spin_unlock_irq(&hdsp->lock);
2279 return change;
2280}
2281
2282#define HDSP_PHONE_GAIN(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002283{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 .name = xname, \
2285 .index = xindex, \
2286 .info = snd_hdsp_info_phone_gain, \
2287 .get = snd_hdsp_get_phone_gain, \
2288 .put = snd_hdsp_put_phone_gain \
2289}
2290
Takashi Iwai55e957d2005-11-17 14:52:13 +01002291static int hdsp_phone_gain(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292{
2293 switch (hdsp->control_register & HDSP_PhoneGainMask) {
2294 case HDSP_PhoneGain0dB:
2295 return 0;
2296 case HDSP_PhoneGainMinus6dB:
2297 return 1;
2298 case HDSP_PhoneGainMinus12dB:
2299 return 2;
2300 default:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002301 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 }
2303}
2304
Takashi Iwai55e957d2005-11-17 14:52:13 +01002305static int hdsp_set_phone_gain(struct hdsp *hdsp, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306{
2307 hdsp->control_register &= ~HDSP_PhoneGainMask;
2308 switch (mode) {
2309 case 0:
2310 hdsp->control_register |= HDSP_PhoneGain0dB;
2311 break;
2312 case 1:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002313 hdsp->control_register |= HDSP_PhoneGainMinus6dB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 break;
2315 case 2:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002316 hdsp->control_register |= HDSP_PhoneGainMinus12dB;
2317 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 default:
2319 return -1;
2320
2321 }
2322 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2323 return 0;
2324}
2325
Takashi Iwai55e957d2005-11-17 14:52:13 +01002326static int snd_hdsp_info_phone_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327{
2328 static char *texts[] = {"0 dB", "-6 dB", "-12 dB"};
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002329
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2331 uinfo->count = 1;
2332 uinfo->value.enumerated.items = 3;
2333 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2334 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2335 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2336 return 0;
2337}
2338
Takashi Iwai55e957d2005-11-17 14:52:13 +01002339static int snd_hdsp_get_phone_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002341 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002342
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 ucontrol->value.enumerated.item[0] = hdsp_phone_gain(hdsp);
2344 return 0;
2345}
2346
Takashi Iwai55e957d2005-11-17 14:52:13 +01002347static int snd_hdsp_put_phone_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002349 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 int change;
2351 int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002352
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 if (!snd_hdsp_use_is_exclusive(hdsp))
2354 return -EBUSY;
2355 val = ucontrol->value.enumerated.item[0];
2356 if (val < 0) val = 0;
2357 if (val > 2) val = 2;
2358 spin_lock_irq(&hdsp->lock);
Takashi Iwaib0b98112005-10-20 18:29:58 +02002359 if (val != hdsp_phone_gain(hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 change = (hdsp_set_phone_gain(hdsp, val) == 0) ? 1 : 0;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002361 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 spin_unlock_irq(&hdsp->lock);
2364 return change;
2365}
2366
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367#define HDSP_PREF_SYNC_REF(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002368{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 .name = xname, \
2370 .index = xindex, \
2371 .info = snd_hdsp_info_pref_sync_ref, \
2372 .get = snd_hdsp_get_pref_sync_ref, \
2373 .put = snd_hdsp_put_pref_sync_ref \
2374}
2375
Takashi Iwai55e957d2005-11-17 14:52:13 +01002376static int hdsp_pref_sync_ref(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377{
2378 /* Notice that this looks at the requested sync source,
2379 not the one actually in use.
2380 */
2381
2382 switch (hdsp->control_register & HDSP_SyncRefMask) {
2383 case HDSP_SyncRef_ADAT1:
2384 return HDSP_SYNC_FROM_ADAT1;
2385 case HDSP_SyncRef_ADAT2:
2386 return HDSP_SYNC_FROM_ADAT2;
2387 case HDSP_SyncRef_ADAT3:
2388 return HDSP_SYNC_FROM_ADAT3;
2389 case HDSP_SyncRef_SPDIF:
2390 return HDSP_SYNC_FROM_SPDIF;
2391 case HDSP_SyncRef_WORD:
2392 return HDSP_SYNC_FROM_WORD;
2393 case HDSP_SyncRef_ADAT_SYNC:
2394 return HDSP_SYNC_FROM_ADAT_SYNC;
2395 default:
2396 return HDSP_SYNC_FROM_WORD;
2397 }
2398 return 0;
2399}
2400
Takashi Iwai55e957d2005-11-17 14:52:13 +01002401static int hdsp_set_pref_sync_ref(struct hdsp *hdsp, int pref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402{
2403 hdsp->control_register &= ~HDSP_SyncRefMask;
2404 switch (pref) {
2405 case HDSP_SYNC_FROM_ADAT1:
2406 hdsp->control_register &= ~HDSP_SyncRefMask; /* clear SyncRef bits */
2407 break;
2408 case HDSP_SYNC_FROM_ADAT2:
2409 hdsp->control_register |= HDSP_SyncRef_ADAT2;
2410 break;
2411 case HDSP_SYNC_FROM_ADAT3:
2412 hdsp->control_register |= HDSP_SyncRef_ADAT3;
2413 break;
2414 case HDSP_SYNC_FROM_SPDIF:
2415 hdsp->control_register |= HDSP_SyncRef_SPDIF;
2416 break;
2417 case HDSP_SYNC_FROM_WORD:
2418 hdsp->control_register |= HDSP_SyncRef_WORD;
2419 break;
2420 case HDSP_SYNC_FROM_ADAT_SYNC:
2421 hdsp->control_register |= HDSP_SyncRef_ADAT_SYNC;
2422 break;
2423 default:
2424 return -1;
2425 }
2426 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2427 return 0;
2428}
2429
Takashi Iwai55e957d2005-11-17 14:52:13 +01002430static int snd_hdsp_info_pref_sync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431{
2432 static char *texts[] = {"Word", "IEC958", "ADAT1", "ADAT Sync", "ADAT2", "ADAT3" };
Takashi Iwai55e957d2005-11-17 14:52:13 +01002433 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002434
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2436 uinfo->count = 1;
2437
2438 switch (hdsp->io_type) {
2439 case Digiface:
2440 case H9652:
2441 uinfo->value.enumerated.items = 6;
2442 break;
2443 case Multiface:
2444 uinfo->value.enumerated.items = 4;
2445 break;
2446 case H9632:
2447 uinfo->value.enumerated.items = 3;
2448 break;
2449 default:
Takashi Iwai9badda02012-01-09 18:22:35 +01002450 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002452
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2454 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2455 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2456 return 0;
2457}
2458
Takashi Iwai55e957d2005-11-17 14:52:13 +01002459static int snd_hdsp_get_pref_sync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002461 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002462
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 ucontrol->value.enumerated.item[0] = hdsp_pref_sync_ref(hdsp);
2464 return 0;
2465}
2466
Takashi Iwai55e957d2005-11-17 14:52:13 +01002467static int snd_hdsp_put_pref_sync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002469 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 int change, max;
2471 unsigned int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002472
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 if (!snd_hdsp_use_is_exclusive(hdsp))
2474 return -EBUSY;
2475
2476 switch (hdsp->io_type) {
2477 case Digiface:
2478 case H9652:
2479 max = 6;
2480 break;
2481 case Multiface:
2482 max = 4;
2483 break;
2484 case H9632:
2485 max = 3;
2486 break;
2487 default:
2488 return -EIO;
2489 }
2490
2491 val = ucontrol->value.enumerated.item[0] % max;
2492 spin_lock_irq(&hdsp->lock);
2493 change = (int)val != hdsp_pref_sync_ref(hdsp);
2494 hdsp_set_pref_sync_ref(hdsp, val);
2495 spin_unlock_irq(&hdsp->lock);
2496 return change;
2497}
2498
2499#define HDSP_AUTOSYNC_REF(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002500{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 .name = xname, \
2502 .index = xindex, \
2503 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
2504 .info = snd_hdsp_info_autosync_ref, \
2505 .get = snd_hdsp_get_autosync_ref, \
2506}
2507
Takashi Iwai55e957d2005-11-17 14:52:13 +01002508static int hdsp_autosync_ref(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509{
2510 /* This looks at the autosync selected sync reference */
2511 unsigned int status2 = hdsp_read(hdsp, HDSP_status2Register);
2512
2513 switch (status2 & HDSP_SelSyncRefMask) {
2514 case HDSP_SelSyncRef_WORD:
2515 return HDSP_AUTOSYNC_FROM_WORD;
2516 case HDSP_SelSyncRef_ADAT_SYNC:
2517 return HDSP_AUTOSYNC_FROM_ADAT_SYNC;
2518 case HDSP_SelSyncRef_SPDIF:
2519 return HDSP_AUTOSYNC_FROM_SPDIF;
2520 case HDSP_SelSyncRefMask:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002521 return HDSP_AUTOSYNC_FROM_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 case HDSP_SelSyncRef_ADAT1:
2523 return HDSP_AUTOSYNC_FROM_ADAT1;
2524 case HDSP_SelSyncRef_ADAT2:
2525 return HDSP_AUTOSYNC_FROM_ADAT2;
2526 case HDSP_SelSyncRef_ADAT3:
2527 return HDSP_AUTOSYNC_FROM_ADAT3;
2528 default:
2529 return HDSP_AUTOSYNC_FROM_WORD;
2530 }
2531 return 0;
2532}
2533
Takashi Iwai55e957d2005-11-17 14:52:13 +01002534static int snd_hdsp_info_autosync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535{
2536 static char *texts[] = {"Word", "ADAT Sync", "IEC958", "None", "ADAT1", "ADAT2", "ADAT3" };
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002537
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2539 uinfo->count = 1;
2540 uinfo->value.enumerated.items = 7;
2541 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2542 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2543 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2544 return 0;
2545}
2546
Takashi Iwai55e957d2005-11-17 14:52:13 +01002547static int snd_hdsp_get_autosync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002549 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002550
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 ucontrol->value.enumerated.item[0] = hdsp_autosync_ref(hdsp);
2552 return 0;
2553}
2554
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555#define HDSP_PRECISE_POINTER(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002556{ .iface = SNDRV_CTL_ELEM_IFACE_CARD, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 .name = xname, \
2558 .index = xindex, \
2559 .info = snd_hdsp_info_precise_pointer, \
2560 .get = snd_hdsp_get_precise_pointer, \
2561 .put = snd_hdsp_put_precise_pointer \
2562}
2563
Takashi Iwai55e957d2005-11-17 14:52:13 +01002564static int hdsp_set_precise_pointer(struct hdsp *hdsp, int precise)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565{
Takashi Iwaib0b98112005-10-20 18:29:58 +02002566 if (precise)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 hdsp->precise_ptr = 1;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002568 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 hdsp->precise_ptr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 return 0;
2571}
2572
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002573#define snd_hdsp_info_precise_pointer snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574
Takashi Iwai55e957d2005-11-17 14:52:13 +01002575static int snd_hdsp_get_precise_pointer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002577 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002578
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 spin_lock_irq(&hdsp->lock);
2580 ucontrol->value.integer.value[0] = hdsp->precise_ptr;
2581 spin_unlock_irq(&hdsp->lock);
2582 return 0;
2583}
2584
Takashi Iwai55e957d2005-11-17 14:52:13 +01002585static int snd_hdsp_put_precise_pointer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002587 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 int change;
2589 unsigned int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002590
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 if (!snd_hdsp_use_is_exclusive(hdsp))
2592 return -EBUSY;
2593 val = ucontrol->value.integer.value[0] & 1;
2594 spin_lock_irq(&hdsp->lock);
2595 change = (int)val != hdsp->precise_ptr;
2596 hdsp_set_precise_pointer(hdsp, val);
2597 spin_unlock_irq(&hdsp->lock);
2598 return change;
2599}
2600
2601#define HDSP_USE_MIDI_TASKLET(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002602{ .iface = SNDRV_CTL_ELEM_IFACE_CARD, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 .name = xname, \
2604 .index = xindex, \
2605 .info = snd_hdsp_info_use_midi_tasklet, \
2606 .get = snd_hdsp_get_use_midi_tasklet, \
2607 .put = snd_hdsp_put_use_midi_tasklet \
2608}
2609
Takashi Iwai55e957d2005-11-17 14:52:13 +01002610static int hdsp_set_use_midi_tasklet(struct hdsp *hdsp, int use_tasklet)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611{
Takashi Iwaib0b98112005-10-20 18:29:58 +02002612 if (use_tasklet)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 hdsp->use_midi_tasklet = 1;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002614 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 hdsp->use_midi_tasklet = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 return 0;
2617}
2618
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002619#define snd_hdsp_info_use_midi_tasklet snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620
Takashi Iwai55e957d2005-11-17 14:52:13 +01002621static int snd_hdsp_get_use_midi_tasklet(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002623 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002624
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 spin_lock_irq(&hdsp->lock);
2626 ucontrol->value.integer.value[0] = hdsp->use_midi_tasklet;
2627 spin_unlock_irq(&hdsp->lock);
2628 return 0;
2629}
2630
Takashi Iwai55e957d2005-11-17 14:52:13 +01002631static int snd_hdsp_put_use_midi_tasklet(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002633 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 int change;
2635 unsigned int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002636
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 if (!snd_hdsp_use_is_exclusive(hdsp))
2638 return -EBUSY;
2639 val = ucontrol->value.integer.value[0] & 1;
2640 spin_lock_irq(&hdsp->lock);
2641 change = (int)val != hdsp->use_midi_tasklet;
2642 hdsp_set_use_midi_tasklet(hdsp, val);
2643 spin_unlock_irq(&hdsp->lock);
2644 return change;
2645}
2646
2647#define HDSP_MIXER(xname, xindex) \
2648{ .iface = SNDRV_CTL_ELEM_IFACE_HWDEP, \
2649 .name = xname, \
2650 .index = xindex, \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002651 .device = 0, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
2653 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
2654 .info = snd_hdsp_info_mixer, \
2655 .get = snd_hdsp_get_mixer, \
2656 .put = snd_hdsp_put_mixer \
2657}
2658
Takashi Iwai55e957d2005-11-17 14:52:13 +01002659static int snd_hdsp_info_mixer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660{
2661 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2662 uinfo->count = 3;
2663 uinfo->value.integer.min = 0;
2664 uinfo->value.integer.max = 65536;
2665 uinfo->value.integer.step = 1;
2666 return 0;
2667}
2668
Takashi Iwai55e957d2005-11-17 14:52:13 +01002669static int snd_hdsp_get_mixer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002671 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 int source;
2673 int destination;
2674 int addr;
2675
2676 source = ucontrol->value.integer.value[0];
2677 destination = ucontrol->value.integer.value[1];
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002678
Takashi Iwaib0b98112005-10-20 18:29:58 +02002679 if (source >= hdsp->max_channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 addr = hdsp_playback_to_output_key(hdsp,source-hdsp->max_channels,destination);
Takashi Iwaib0b98112005-10-20 18:29:58 +02002681 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 addr = hdsp_input_to_output_key(hdsp,source, destination);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002683
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 spin_lock_irq(&hdsp->lock);
2685 ucontrol->value.integer.value[2] = hdsp_read_gain (hdsp, addr);
2686 spin_unlock_irq(&hdsp->lock);
2687 return 0;
2688}
2689
Takashi Iwai55e957d2005-11-17 14:52:13 +01002690static int snd_hdsp_put_mixer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002692 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 int change;
2694 int source;
2695 int destination;
2696 int gain;
2697 int addr;
2698
2699 if (!snd_hdsp_use_is_exclusive(hdsp))
2700 return -EBUSY;
2701
2702 source = ucontrol->value.integer.value[0];
2703 destination = ucontrol->value.integer.value[1];
2704
Takashi Iwaib0b98112005-10-20 18:29:58 +02002705 if (source >= hdsp->max_channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 addr = hdsp_playback_to_output_key(hdsp,source-hdsp->max_channels, destination);
Takashi Iwaib0b98112005-10-20 18:29:58 +02002707 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 addr = hdsp_input_to_output_key(hdsp,source, destination);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709
2710 gain = ucontrol->value.integer.value[2];
2711
2712 spin_lock_irq(&hdsp->lock);
2713 change = gain != hdsp_read_gain(hdsp, addr);
2714 if (change)
2715 hdsp_write_gain(hdsp, addr, gain);
2716 spin_unlock_irq(&hdsp->lock);
2717 return change;
2718}
2719
2720#define HDSP_WC_SYNC_CHECK(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002721{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 .name = xname, \
2723 .index = xindex, \
2724 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
2725 .info = snd_hdsp_info_sync_check, \
2726 .get = snd_hdsp_get_wc_sync_check \
2727}
2728
Takashi Iwai55e957d2005-11-17 14:52:13 +01002729static int snd_hdsp_info_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730{
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002731 static char *texts[] = {"No Lock", "Lock", "Sync" };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2733 uinfo->count = 1;
2734 uinfo->value.enumerated.items = 3;
2735 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2736 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2737 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2738 return 0;
2739}
2740
Takashi Iwai55e957d2005-11-17 14:52:13 +01002741static int hdsp_wc_sync_check(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742{
2743 int status2 = hdsp_read(hdsp, HDSP_status2Register);
2744 if (status2 & HDSP_wc_lock) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02002745 if (status2 & HDSP_wc_sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 return 2;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002747 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 return 1;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002749 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 return 0;
2752}
2753
Takashi Iwai55e957d2005-11-17 14:52:13 +01002754static int snd_hdsp_get_wc_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002756 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757
2758 ucontrol->value.enumerated.item[0] = hdsp_wc_sync_check(hdsp);
2759 return 0;
2760}
2761
2762#define HDSP_SPDIF_SYNC_CHECK(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002763{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 .name = xname, \
2765 .index = xindex, \
2766 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
2767 .info = snd_hdsp_info_sync_check, \
2768 .get = snd_hdsp_get_spdif_sync_check \
2769}
2770
Takashi Iwai55e957d2005-11-17 14:52:13 +01002771static int hdsp_spdif_sync_check(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772{
2773 int status = hdsp_read(hdsp, HDSP_statusRegister);
Takashi Iwaib0b98112005-10-20 18:29:58 +02002774 if (status & HDSP_SPDIFErrorFlag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 return 0;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002776 else {
Takashi Iwaib0b98112005-10-20 18:29:58 +02002777 if (status & HDSP_SPDIFSync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 return 2;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002779 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 }
2782 return 0;
2783}
2784
Takashi Iwai55e957d2005-11-17 14:52:13 +01002785static int snd_hdsp_get_spdif_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002787 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788
2789 ucontrol->value.enumerated.item[0] = hdsp_spdif_sync_check(hdsp);
2790 return 0;
2791}
2792
2793#define HDSP_ADATSYNC_SYNC_CHECK(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002794{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 .name = xname, \
2796 .index = xindex, \
2797 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
2798 .info = snd_hdsp_info_sync_check, \
2799 .get = snd_hdsp_get_adatsync_sync_check \
2800}
2801
Takashi Iwai55e957d2005-11-17 14:52:13 +01002802static int hdsp_adatsync_sync_check(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803{
2804 int status = hdsp_read(hdsp, HDSP_statusRegister);
2805 if (status & HDSP_TimecodeLock) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02002806 if (status & HDSP_TimecodeSync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 return 2;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002808 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 return 1;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002810 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 return 0;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002812}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813
Takashi Iwai55e957d2005-11-17 14:52:13 +01002814static int snd_hdsp_get_adatsync_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002816 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817
2818 ucontrol->value.enumerated.item[0] = hdsp_adatsync_sync_check(hdsp);
2819 return 0;
2820}
2821
2822#define HDSP_ADAT_SYNC_CHECK \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002823{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
2825 .info = snd_hdsp_info_sync_check, \
2826 .get = snd_hdsp_get_adat_sync_check \
2827}
2828
Takashi Iwai55e957d2005-11-17 14:52:13 +01002829static int hdsp_adat_sync_check(struct hdsp *hdsp, int idx)
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002830{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 int status = hdsp_read(hdsp, HDSP_statusRegister);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002832
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 if (status & (HDSP_Lock0>>idx)) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02002834 if (status & (HDSP_Sync0>>idx))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 return 2;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002836 else
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002837 return 1;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002838 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 return 0;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002840}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841
Takashi Iwai55e957d2005-11-17 14:52:13 +01002842static int snd_hdsp_get_adat_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843{
2844 int offset;
Takashi Iwai55e957d2005-11-17 14:52:13 +01002845 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846
2847 offset = ucontrol->id.index - 1;
Takashi Iwaida3cec32008-08-08 17:12:14 +02002848 snd_BUG_ON(offset < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849
2850 switch (hdsp->io_type) {
2851 case Digiface:
2852 case H9652:
2853 if (offset >= 3)
2854 return -EINVAL;
2855 break;
2856 case Multiface:
2857 case H9632:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002858 if (offset >= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 return -EINVAL;
2860 break;
2861 default:
2862 return -EIO;
2863 }
2864
2865 ucontrol->value.enumerated.item[0] = hdsp_adat_sync_check(hdsp, offset);
2866 return 0;
2867}
2868
Julian Cablee4b60882007-03-19 11:44:40 +01002869#define HDSP_DDS_OFFSET(xname, xindex) \
2870{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2871 .name = xname, \
2872 .index = xindex, \
2873 .info = snd_hdsp_info_dds_offset, \
2874 .get = snd_hdsp_get_dds_offset, \
2875 .put = snd_hdsp_put_dds_offset \
2876}
2877
2878static int hdsp_dds_offset(struct hdsp *hdsp)
2879{
2880 u64 n;
Julian Cablee4b60882007-03-19 11:44:40 +01002881 unsigned int dds_value = hdsp->dds_value;
2882 int system_sample_rate = hdsp->system_sample_rate;
2883
Takashi Iwai2a3988f2007-10-16 14:26:32 +02002884 if (!dds_value)
2885 return 0;
2886
Julian Cablee4b60882007-03-19 11:44:40 +01002887 n = DDS_NUMERATOR;
2888 /*
2889 * dds_value = n / rate
2890 * rate = n / dds_value
2891 */
Takashi Iwai3f7440a2009-06-05 17:40:04 +02002892 n = div_u64(n, dds_value);
Julian Cablee4b60882007-03-19 11:44:40 +01002893 if (system_sample_rate >= 112000)
2894 n *= 4;
2895 else if (system_sample_rate >= 56000)
2896 n *= 2;
2897 return ((int)n) - system_sample_rate;
2898}
2899
2900static int hdsp_set_dds_offset(struct hdsp *hdsp, int offset_hz)
2901{
2902 int rate = hdsp->system_sample_rate + offset_hz;
2903 hdsp_set_dds_value(hdsp, rate);
2904 return 0;
2905}
2906
2907static int snd_hdsp_info_dds_offset(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
2908{
2909 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2910 uinfo->count = 1;
2911 uinfo->value.integer.min = -5000;
2912 uinfo->value.integer.max = 5000;
2913 return 0;
2914}
2915
2916static int snd_hdsp_get_dds_offset(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2917{
2918 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002919
Julian Cablee4b60882007-03-19 11:44:40 +01002920 ucontrol->value.enumerated.item[0] = hdsp_dds_offset(hdsp);
2921 return 0;
2922}
2923
2924static int snd_hdsp_put_dds_offset(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2925{
2926 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2927 int change;
2928 int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002929
Julian Cablee4b60882007-03-19 11:44:40 +01002930 if (!snd_hdsp_use_is_exclusive(hdsp))
2931 return -EBUSY;
2932 val = ucontrol->value.enumerated.item[0];
2933 spin_lock_irq(&hdsp->lock);
2934 if (val != hdsp_dds_offset(hdsp))
2935 change = (hdsp_set_dds_offset(hdsp, val) == 0) ? 1 : 0;
2936 else
2937 change = 0;
2938 spin_unlock_irq(&hdsp->lock);
2939 return change;
2940}
2941
Takashi Iwai55e957d2005-11-17 14:52:13 +01002942static struct snd_kcontrol_new snd_hdsp_9632_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943HDSP_DA_GAIN("DA Gain", 0),
2944HDSP_AD_GAIN("AD Gain", 0),
2945HDSP_PHONE_GAIN("Phones Gain", 0),
Adrian Knoth4833c672013-01-15 18:52:22 +01002946HDSP_TOGGLE_SETTING("XLR Breakout Cable", HDSP_XLRBreakoutCable),
Julian Cablee4b60882007-03-19 11:44:40 +01002947HDSP_DDS_OFFSET("DDS Sample Rate Offset", 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948};
2949
Takashi Iwai55e957d2005-11-17 14:52:13 +01002950static struct snd_kcontrol_new snd_hdsp_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951{
Clemens Ladisch5549d542005-08-03 13:50:30 +02002952 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
2954 .info = snd_hdsp_control_spdif_info,
2955 .get = snd_hdsp_control_spdif_get,
2956 .put = snd_hdsp_control_spdif_put,
2957},
2958{
2959 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
Clemens Ladisch5549d542005-08-03 13:50:30 +02002960 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
2962 .info = snd_hdsp_control_spdif_stream_info,
2963 .get = snd_hdsp_control_spdif_stream_get,
2964 .put = snd_hdsp_control_spdif_stream_put,
2965},
2966{
2967 .access = SNDRV_CTL_ELEM_ACCESS_READ,
Clemens Ladisch5549d542005-08-03 13:50:30 +02002968 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
2970 .info = snd_hdsp_control_spdif_mask_info,
2971 .get = snd_hdsp_control_spdif_mask_get,
2972 .private_value = IEC958_AES0_NONAUDIO |
2973 IEC958_AES0_PROFESSIONAL |
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002974 IEC958_AES0_CON_EMPHASIS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975},
2976{
2977 .access = SNDRV_CTL_ELEM_ACCESS_READ,
Clemens Ladisch5549d542005-08-03 13:50:30 +02002978 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
2980 .info = snd_hdsp_control_spdif_mask_info,
2981 .get = snd_hdsp_control_spdif_mask_get,
2982 .private_value = IEC958_AES0_NONAUDIO |
2983 IEC958_AES0_PROFESSIONAL |
2984 IEC958_AES0_PRO_EMPHASIS,
2985},
2986HDSP_MIXER("Mixer", 0),
2987HDSP_SPDIF_IN("IEC958 Input Connector", 0),
Adrian Knoth4833c672013-01-15 18:52:22 +01002988HDSP_TOGGLE_SETTING("IEC958 Output also on ADAT1", HDSP_SPDIFOpticalOut),
2989HDSP_TOGGLE_SETTING("IEC958 Professional Bit", HDSP_SPDIFProfessional),
2990HDSP_TOGGLE_SETTING("IEC958 Emphasis Bit", HDSP_SPDIFEmphasis),
2991HDSP_TOGGLE_SETTING("IEC958 Non-audio Bit", HDSP_SPDIFNonAudio),
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002992/* 'Sample Clock Source' complies with the alsa control naming scheme */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993HDSP_CLOCK_SOURCE("Sample Clock Source", 0),
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02002994{
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02002995 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2996 .name = "Sample Clock Source Locking",
2997 .info = snd_hdsp_info_clock_source_lock,
2998 .get = snd_hdsp_get_clock_source_lock,
2999 .put = snd_hdsp_put_clock_source_lock,
3000},
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001HDSP_SYSTEM_CLOCK_MODE("System Clock Mode", 0),
3002HDSP_PREF_SYNC_REF("Preferred Sync Reference", 0),
3003HDSP_AUTOSYNC_REF("AutoSync Reference", 0),
3004HDSP_SPDIF_SAMPLE_RATE("SPDIF Sample Rate", 0),
3005HDSP_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
3006/* 'External Rate' complies with the alsa control naming scheme */
3007HDSP_AUTOSYNC_SAMPLE_RATE("External Rate", 0),
3008HDSP_WC_SYNC_CHECK("Word Clock Lock Status", 0),
3009HDSP_SPDIF_SYNC_CHECK("SPDIF Lock Status", 0),
3010HDSP_ADATSYNC_SYNC_CHECK("ADAT Sync Lock Status", 0),
Adrian Knoth4833c672013-01-15 18:52:22 +01003011HDSP_TOGGLE_SETTING("Line Out", HDSP_LineOut),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012HDSP_PRECISE_POINTER("Precise Pointer", 0),
3013HDSP_USE_MIDI_TASKLET("Use Midi Tasklet", 0),
3014};
3015
Florian Faber28b26e12010-12-01 12:14:47 +01003016
3017static int hdsp_rpm_input12(struct hdsp *hdsp)
3018{
3019 switch (hdsp->control_register & HDSP_RPM_Inp12) {
3020 case HDSP_RPM_Inp12_Phon_6dB:
3021 return 0;
3022 case HDSP_RPM_Inp12_Phon_n6dB:
3023 return 2;
3024 case HDSP_RPM_Inp12_Line_0dB:
3025 return 3;
3026 case HDSP_RPM_Inp12_Line_n6dB:
3027 return 4;
3028 }
3029 return 1;
3030}
3031
3032
3033static int snd_hdsp_get_rpm_input12(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3034{
3035 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
3036
3037 ucontrol->value.enumerated.item[0] = hdsp_rpm_input12(hdsp);
3038 return 0;
3039}
3040
3041
3042static int hdsp_set_rpm_input12(struct hdsp *hdsp, int mode)
3043{
3044 hdsp->control_register &= ~HDSP_RPM_Inp12;
3045 switch (mode) {
3046 case 0:
3047 hdsp->control_register |= HDSP_RPM_Inp12_Phon_6dB;
3048 break;
3049 case 1:
3050 break;
3051 case 2:
3052 hdsp->control_register |= HDSP_RPM_Inp12_Phon_n6dB;
3053 break;
3054 case 3:
3055 hdsp->control_register |= HDSP_RPM_Inp12_Line_0dB;
3056 break;
3057 case 4:
3058 hdsp->control_register |= HDSP_RPM_Inp12_Line_n6dB;
3059 break;
3060 default:
3061 return -1;
3062 }
3063
3064 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
3065 return 0;
3066}
3067
3068
3069static int snd_hdsp_put_rpm_input12(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3070{
3071 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
3072 int change;
3073 int val;
3074
3075 if (!snd_hdsp_use_is_exclusive(hdsp))
3076 return -EBUSY;
3077 val = ucontrol->value.enumerated.item[0];
3078 if (val < 0)
3079 val = 0;
3080 if (val > 4)
3081 val = 4;
3082 spin_lock_irq(&hdsp->lock);
3083 if (val != hdsp_rpm_input12(hdsp))
3084 change = (hdsp_set_rpm_input12(hdsp, val) == 0) ? 1 : 0;
3085 else
3086 change = 0;
3087 spin_unlock_irq(&hdsp->lock);
3088 return change;
3089}
3090
3091
3092static int snd_hdsp_info_rpm_input(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
3093{
3094 static char *texts[] = {"Phono +6dB", "Phono 0dB", "Phono -6dB", "Line 0dB", "Line -6dB"};
3095
3096 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3097 uinfo->count = 1;
3098 uinfo->value.enumerated.items = 5;
3099 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
3100 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
3101 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
3102 return 0;
3103}
3104
3105
3106static int hdsp_rpm_input34(struct hdsp *hdsp)
3107{
3108 switch (hdsp->control_register & HDSP_RPM_Inp34) {
3109 case HDSP_RPM_Inp34_Phon_6dB:
3110 return 0;
3111 case HDSP_RPM_Inp34_Phon_n6dB:
3112 return 2;
3113 case HDSP_RPM_Inp34_Line_0dB:
3114 return 3;
3115 case HDSP_RPM_Inp34_Line_n6dB:
3116 return 4;
3117 }
3118 return 1;
3119}
3120
3121
3122static int snd_hdsp_get_rpm_input34(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3123{
3124 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
3125
3126 ucontrol->value.enumerated.item[0] = hdsp_rpm_input34(hdsp);
3127 return 0;
3128}
3129
3130
3131static int hdsp_set_rpm_input34(struct hdsp *hdsp, int mode)
3132{
3133 hdsp->control_register &= ~HDSP_RPM_Inp34;
3134 switch (mode) {
3135 case 0:
3136 hdsp->control_register |= HDSP_RPM_Inp34_Phon_6dB;
3137 break;
3138 case 1:
3139 break;
3140 case 2:
3141 hdsp->control_register |= HDSP_RPM_Inp34_Phon_n6dB;
3142 break;
3143 case 3:
3144 hdsp->control_register |= HDSP_RPM_Inp34_Line_0dB;
3145 break;
3146 case 4:
3147 hdsp->control_register |= HDSP_RPM_Inp34_Line_n6dB;
3148 break;
3149 default:
3150 return -1;
3151 }
3152
3153 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
3154 return 0;
3155}
3156
3157
3158static int snd_hdsp_put_rpm_input34(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3159{
3160 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
3161 int change;
3162 int val;
3163
3164 if (!snd_hdsp_use_is_exclusive(hdsp))
3165 return -EBUSY;
3166 val = ucontrol->value.enumerated.item[0];
3167 if (val < 0)
3168 val = 0;
3169 if (val > 4)
3170 val = 4;
3171 spin_lock_irq(&hdsp->lock);
3172 if (val != hdsp_rpm_input34(hdsp))
3173 change = (hdsp_set_rpm_input34(hdsp, val) == 0) ? 1 : 0;
3174 else
3175 change = 0;
3176 spin_unlock_irq(&hdsp->lock);
3177 return change;
3178}
3179
3180
3181/* RPM Bypass switch */
3182static int hdsp_rpm_bypass(struct hdsp *hdsp)
3183{
3184 return (hdsp->control_register & HDSP_RPM_Bypass) ? 1 : 0;
3185}
3186
3187
3188static int snd_hdsp_get_rpm_bypass(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3189{
3190 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
3191
3192 ucontrol->value.integer.value[0] = hdsp_rpm_bypass(hdsp);
3193 return 0;
3194}
3195
3196
3197static int hdsp_set_rpm_bypass(struct hdsp *hdsp, int on)
3198{
3199 if (on)
3200 hdsp->control_register |= HDSP_RPM_Bypass;
3201 else
3202 hdsp->control_register &= ~HDSP_RPM_Bypass;
3203 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
3204 return 0;
3205}
3206
3207
3208static int snd_hdsp_put_rpm_bypass(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3209{
3210 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
3211 int change;
3212 unsigned int val;
3213
3214 if (!snd_hdsp_use_is_exclusive(hdsp))
3215 return -EBUSY;
3216 val = ucontrol->value.integer.value[0] & 1;
3217 spin_lock_irq(&hdsp->lock);
3218 change = (int)val != hdsp_rpm_bypass(hdsp);
3219 hdsp_set_rpm_bypass(hdsp, val);
3220 spin_unlock_irq(&hdsp->lock);
3221 return change;
3222}
3223
3224
3225static int snd_hdsp_info_rpm_bypass(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
3226{
3227 static char *texts[] = {"On", "Off"};
3228
3229 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3230 uinfo->count = 1;
3231 uinfo->value.enumerated.items = 2;
3232 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
3233 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
3234 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
3235 return 0;
3236}
3237
3238
3239/* RPM Disconnect switch */
3240static int hdsp_rpm_disconnect(struct hdsp *hdsp)
3241{
3242 return (hdsp->control_register & HDSP_RPM_Disconnect) ? 1 : 0;
3243}
3244
3245
3246static int snd_hdsp_get_rpm_disconnect(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3247{
3248 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
3249
3250 ucontrol->value.integer.value[0] = hdsp_rpm_disconnect(hdsp);
3251 return 0;
3252}
3253
3254
3255static int hdsp_set_rpm_disconnect(struct hdsp *hdsp, int on)
3256{
3257 if (on)
3258 hdsp->control_register |= HDSP_RPM_Disconnect;
3259 else
3260 hdsp->control_register &= ~HDSP_RPM_Disconnect;
3261 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
3262 return 0;
3263}
3264
3265
3266static int snd_hdsp_put_rpm_disconnect(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3267{
3268 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
3269 int change;
3270 unsigned int val;
3271
3272 if (!snd_hdsp_use_is_exclusive(hdsp))
3273 return -EBUSY;
3274 val = ucontrol->value.integer.value[0] & 1;
3275 spin_lock_irq(&hdsp->lock);
3276 change = (int)val != hdsp_rpm_disconnect(hdsp);
3277 hdsp_set_rpm_disconnect(hdsp, val);
3278 spin_unlock_irq(&hdsp->lock);
3279 return change;
3280}
3281
3282static int snd_hdsp_info_rpm_disconnect(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
3283{
3284 static char *texts[] = {"On", "Off"};
3285
3286 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3287 uinfo->count = 1;
3288 uinfo->value.enumerated.items = 2;
3289 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
3290 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
3291 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
3292 return 0;
3293}
3294
3295static struct snd_kcontrol_new snd_hdsp_rpm_controls[] = {
3296 {
3297 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3298 .name = "RPM Bypass",
3299 .get = snd_hdsp_get_rpm_bypass,
3300 .put = snd_hdsp_put_rpm_bypass,
3301 .info = snd_hdsp_info_rpm_bypass
3302 },
3303 {
3304 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3305 .name = "RPM Disconnect",
3306 .get = snd_hdsp_get_rpm_disconnect,
3307 .put = snd_hdsp_put_rpm_disconnect,
3308 .info = snd_hdsp_info_rpm_disconnect
3309 },
3310 {
3311 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3312 .name = "Input 1/2",
3313 .get = snd_hdsp_get_rpm_input12,
3314 .put = snd_hdsp_put_rpm_input12,
3315 .info = snd_hdsp_info_rpm_input
3316 },
3317 {
3318 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3319 .name = "Input 3/4",
3320 .get = snd_hdsp_get_rpm_input34,
3321 .put = snd_hdsp_put_rpm_input34,
3322 .info = snd_hdsp_info_rpm_input
3323 },
3324 HDSP_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
3325 HDSP_MIXER("Mixer", 0)
3326};
3327
Adrian Knoth4833c672013-01-15 18:52:22 +01003328static struct snd_kcontrol_new snd_hdsp_96xx_aeb =
3329 HDSP_TOGGLE_SETTING("Analog Extension Board",
3330 HDSP_AnalogExtensionBoard);
Takashi Iwai55e957d2005-11-17 14:52:13 +01003331static struct snd_kcontrol_new snd_hdsp_adat_sync_check = HDSP_ADAT_SYNC_CHECK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332
Takashi Iwai55e957d2005-11-17 14:52:13 +01003333static int snd_hdsp_create_controls(struct snd_card *card, struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334{
3335 unsigned int idx;
3336 int err;
Takashi Iwai55e957d2005-11-17 14:52:13 +01003337 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338
Florian Faber28b26e12010-12-01 12:14:47 +01003339 if (hdsp->io_type == RPM) {
3340 /* RPM Bypass, Disconnect and Input switches */
3341 for (idx = 0; idx < ARRAY_SIZE(snd_hdsp_rpm_controls); idx++) {
3342 err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_rpm_controls[idx], hdsp));
3343 if (err < 0)
3344 return err;
3345 }
3346 return 0;
3347 }
3348
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 for (idx = 0; idx < ARRAY_SIZE(snd_hdsp_controls); idx++) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02003350 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_controls[idx], hdsp))) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 if (idx == 1) /* IEC958 (S/PDIF) Stream */
3353 hdsp->spdif_ctl = kctl;
3354 }
3355
3356 /* ADAT SyncCheck status */
3357 snd_hdsp_adat_sync_check.name = "ADAT Lock Status";
3358 snd_hdsp_adat_sync_check.index = 1;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003359 if ((err = snd_ctl_add (card, kctl = snd_ctl_new1(&snd_hdsp_adat_sync_check, hdsp))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 if (hdsp->io_type == Digiface || hdsp->io_type == H9652) {
3362 for (idx = 1; idx < 3; ++idx) {
3363 snd_hdsp_adat_sync_check.index = idx+1;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003364 if ((err = snd_ctl_add (card, kctl = snd_ctl_new1(&snd_hdsp_adat_sync_check, hdsp))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 }
3367 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003368
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 /* DA, AD and Phone gain and XLR breakout cable controls for H9632 cards */
3370 if (hdsp->io_type == H9632) {
3371 for (idx = 0; idx < ARRAY_SIZE(snd_hdsp_9632_controls); idx++) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02003372 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_9632_controls[idx], hdsp))) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 }
3375 }
3376
3377 /* AEB control for H96xx card */
3378 if (hdsp->io_type == H9632 || hdsp->io_type == H9652) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02003379 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_96xx_aeb, hdsp))) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 }
3382
3383 return 0;
3384}
3385
3386/*------------------------------------------------------------
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003387 /proc interface
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 ------------------------------------------------------------*/
3389
3390static void
Takashi Iwai55e957d2005-11-17 14:52:13 +01003391snd_hdsp_proc_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392{
Joe Perches9fe856e2010-09-04 18:52:54 -07003393 struct hdsp *hdsp = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 unsigned int status;
3395 unsigned int status2;
3396 char *pref_sync_ref;
3397 char *autosync_ref;
3398 char *system_clock_mode;
3399 char *clock_source;
3400 int x;
3401
Tim Blechmannc18bc9b2009-08-12 18:21:30 +02003402 status = hdsp_read(hdsp, HDSP_statusRegister);
3403 status2 = hdsp_read(hdsp, HDSP_status2Register);
3404
3405 snd_iprintf(buffer, "%s (Card #%d)\n", hdsp->card_name,
3406 hdsp->card->number + 1);
3407 snd_iprintf(buffer, "Buffers: capture %p playback %p\n",
3408 hdsp->capture_buffer, hdsp->playback_buffer);
3409 snd_iprintf(buffer, "IRQ: %d Registers bus: 0x%lx VM: 0x%lx\n",
3410 hdsp->irq, hdsp->port, (unsigned long)hdsp->iobase);
3411 snd_iprintf(buffer, "Control register: 0x%x\n", hdsp->control_register);
3412 snd_iprintf(buffer, "Control2 register: 0x%x\n",
3413 hdsp->control2_register);
3414 snd_iprintf(buffer, "Status register: 0x%x\n", status);
3415 snd_iprintf(buffer, "Status2 register: 0x%x\n", status2);
3416
3417 if (hdsp_check_for_iobox(hdsp)) {
3418 snd_iprintf(buffer, "No I/O box connected.\n"
3419 "Please connect one and upload firmware.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 return;
Tim Blechmannc18bc9b2009-08-12 18:21:30 +02003421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422
Takashi Iwaib0b98112005-10-20 18:29:58 +02003423 if (hdsp_check_for_firmware(hdsp, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 if (hdsp->state & HDSP_FirmwareCached) {
3425 if (snd_hdsp_load_firmware_from_cache(hdsp) != 0) {
Tim Blechmannc18bc9b2009-08-12 18:21:30 +02003426 snd_iprintf(buffer, "Firmware loading from "
3427 "cache failed, "
3428 "please upload manually.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 return;
3430 }
3431 } else {
Takashi Iwai311e70a2006-09-06 12:13:37 +02003432 int err = -EINVAL;
Takashi Iwai311e70a2006-09-06 12:13:37 +02003433 err = hdsp_request_fw_loader(hdsp);
Takashi Iwai311e70a2006-09-06 12:13:37 +02003434 if (err < 0) {
3435 snd_iprintf(buffer,
3436 "No firmware loaded nor cached, "
3437 "please upload firmware.\n");
3438 return;
3439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 }
3441 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003442
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 snd_iprintf(buffer, "FIFO status: %d\n", hdsp_read(hdsp, HDSP_fifoStatus) & 0xff);
3444 snd_iprintf(buffer, "MIDI1 Output status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusOut0));
3445 snd_iprintf(buffer, "MIDI1 Input status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusIn0));
3446 snd_iprintf(buffer, "MIDI2 Output status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusOut1));
3447 snd_iprintf(buffer, "MIDI2 Input status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusIn1));
3448 snd_iprintf(buffer, "Use Midi Tasklet: %s\n", hdsp->use_midi_tasklet ? "on" : "off");
3449
3450 snd_iprintf(buffer, "\n");
3451
3452 x = 1 << (6 + hdsp_decode_latency(hdsp->control_register & HDSP_LatencyMask));
3453
3454 snd_iprintf(buffer, "Buffer Size (Latency): %d samples (2 periods of %lu bytes)\n", x, (unsigned long) hdsp->period_bytes);
3455 snd_iprintf(buffer, "Hardware pointer (frames): %ld\n", hdsp_hw_pointer(hdsp));
3456 snd_iprintf(buffer, "Precise pointer: %s\n", hdsp->precise_ptr ? "on" : "off");
3457 snd_iprintf(buffer, "Line out: %s\n", (hdsp->control_register & HDSP_LineOut) ? "on" : "off");
3458
3459 snd_iprintf(buffer, "Firmware version: %d\n", (status2&HDSP_version0)|(status2&HDSP_version1)<<1|(status2&HDSP_version2)<<2);
3460
3461 snd_iprintf(buffer, "\n");
3462
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 switch (hdsp_clock_source(hdsp)) {
3464 case HDSP_CLOCK_SOURCE_AUTOSYNC:
3465 clock_source = "AutoSync";
3466 break;
3467 case HDSP_CLOCK_SOURCE_INTERNAL_32KHZ:
3468 clock_source = "Internal 32 kHz";
3469 break;
3470 case HDSP_CLOCK_SOURCE_INTERNAL_44_1KHZ:
3471 clock_source = "Internal 44.1 kHz";
3472 break;
3473 case HDSP_CLOCK_SOURCE_INTERNAL_48KHZ:
3474 clock_source = "Internal 48 kHz";
3475 break;
3476 case HDSP_CLOCK_SOURCE_INTERNAL_64KHZ:
3477 clock_source = "Internal 64 kHz";
3478 break;
3479 case HDSP_CLOCK_SOURCE_INTERNAL_88_2KHZ:
3480 clock_source = "Internal 88.2 kHz";
3481 break;
3482 case HDSP_CLOCK_SOURCE_INTERNAL_96KHZ:
3483 clock_source = "Internal 96 kHz";
3484 break;
3485 case HDSP_CLOCK_SOURCE_INTERNAL_128KHZ:
3486 clock_source = "Internal 128 kHz";
3487 break;
3488 case HDSP_CLOCK_SOURCE_INTERNAL_176_4KHZ:
3489 clock_source = "Internal 176.4 kHz";
3490 break;
3491 case HDSP_CLOCK_SOURCE_INTERNAL_192KHZ:
3492 clock_source = "Internal 192 kHz";
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003493 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 default:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003495 clock_source = "Error";
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 }
3497 snd_iprintf (buffer, "Sample Clock Source: %s\n", clock_source);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003498
Takashi Iwaib0b98112005-10-20 18:29:58 +02003499 if (hdsp_system_clock_mode(hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 system_clock_mode = "Slave";
Takashi Iwaib0b98112005-10-20 18:29:58 +02003501 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 system_clock_mode = "Master";
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003503
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 switch (hdsp_pref_sync_ref (hdsp)) {
3505 case HDSP_SYNC_FROM_WORD:
3506 pref_sync_ref = "Word Clock";
3507 break;
3508 case HDSP_SYNC_FROM_ADAT_SYNC:
3509 pref_sync_ref = "ADAT Sync";
3510 break;
3511 case HDSP_SYNC_FROM_SPDIF:
3512 pref_sync_ref = "SPDIF";
3513 break;
3514 case HDSP_SYNC_FROM_ADAT1:
3515 pref_sync_ref = "ADAT1";
3516 break;
3517 case HDSP_SYNC_FROM_ADAT2:
3518 pref_sync_ref = "ADAT2";
3519 break;
3520 case HDSP_SYNC_FROM_ADAT3:
3521 pref_sync_ref = "ADAT3";
3522 break;
3523 default:
3524 pref_sync_ref = "Word Clock";
3525 break;
3526 }
3527 snd_iprintf (buffer, "Preferred Sync Reference: %s\n", pref_sync_ref);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003528
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 switch (hdsp_autosync_ref (hdsp)) {
3530 case HDSP_AUTOSYNC_FROM_WORD:
3531 autosync_ref = "Word Clock";
3532 break;
3533 case HDSP_AUTOSYNC_FROM_ADAT_SYNC:
3534 autosync_ref = "ADAT Sync";
3535 break;
3536 case HDSP_AUTOSYNC_FROM_SPDIF:
3537 autosync_ref = "SPDIF";
3538 break;
3539 case HDSP_AUTOSYNC_FROM_NONE:
3540 autosync_ref = "None";
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003541 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 case HDSP_AUTOSYNC_FROM_ADAT1:
3543 autosync_ref = "ADAT1";
3544 break;
3545 case HDSP_AUTOSYNC_FROM_ADAT2:
3546 autosync_ref = "ADAT2";
3547 break;
3548 case HDSP_AUTOSYNC_FROM_ADAT3:
3549 autosync_ref = "ADAT3";
3550 break;
3551 default:
3552 autosync_ref = "---";
3553 break;
3554 }
3555 snd_iprintf (buffer, "AutoSync Reference: %s\n", autosync_ref);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003556
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 snd_iprintf (buffer, "AutoSync Frequency: %d\n", hdsp_external_sample_rate(hdsp));
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003558
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 snd_iprintf (buffer, "System Clock Mode: %s\n", system_clock_mode);
3560
3561 snd_iprintf (buffer, "System Clock Frequency: %d\n", hdsp->system_sample_rate);
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02003562 snd_iprintf (buffer, "System Clock Locked: %s\n", hdsp->clock_source_locked ? "Yes" : "No");
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003563
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 snd_iprintf(buffer, "\n");
3565
Florian Faber28b26e12010-12-01 12:14:47 +01003566 if (hdsp->io_type != RPM) {
3567 switch (hdsp_spdif_in(hdsp)) {
3568 case HDSP_SPDIFIN_OPTICAL:
3569 snd_iprintf(buffer, "IEC958 input: Optical\n");
3570 break;
3571 case HDSP_SPDIFIN_COAXIAL:
3572 snd_iprintf(buffer, "IEC958 input: Coaxial\n");
3573 break;
3574 case HDSP_SPDIFIN_INTERNAL:
3575 snd_iprintf(buffer, "IEC958 input: Internal\n");
3576 break;
3577 case HDSP_SPDIFIN_AES:
3578 snd_iprintf(buffer, "IEC958 input: AES\n");
3579 break;
3580 default:
3581 snd_iprintf(buffer, "IEC958 input: ???\n");
3582 break;
3583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003585
Florian Faber28b26e12010-12-01 12:14:47 +01003586 if (RPM == hdsp->io_type) {
3587 if (hdsp->control_register & HDSP_RPM_Bypass)
3588 snd_iprintf(buffer, "RPM Bypass: disabled\n");
3589 else
3590 snd_iprintf(buffer, "RPM Bypass: enabled\n");
3591 if (hdsp->control_register & HDSP_RPM_Disconnect)
3592 snd_iprintf(buffer, "RPM disconnected\n");
3593 else
3594 snd_iprintf(buffer, "RPM connected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595
Florian Faber28b26e12010-12-01 12:14:47 +01003596 switch (hdsp->control_register & HDSP_RPM_Inp12) {
3597 case HDSP_RPM_Inp12_Phon_6dB:
3598 snd_iprintf(buffer, "Input 1/2: Phono, 6dB\n");
3599 break;
3600 case HDSP_RPM_Inp12_Phon_0dB:
3601 snd_iprintf(buffer, "Input 1/2: Phono, 0dB\n");
3602 break;
3603 case HDSP_RPM_Inp12_Phon_n6dB:
3604 snd_iprintf(buffer, "Input 1/2: Phono, -6dB\n");
3605 break;
3606 case HDSP_RPM_Inp12_Line_0dB:
3607 snd_iprintf(buffer, "Input 1/2: Line, 0dB\n");
3608 break;
3609 case HDSP_RPM_Inp12_Line_n6dB:
3610 snd_iprintf(buffer, "Input 1/2: Line, -6dB\n");
3611 break;
3612 default:
3613 snd_iprintf(buffer, "Input 1/2: ???\n");
3614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615
Florian Faber28b26e12010-12-01 12:14:47 +01003616 switch (hdsp->control_register & HDSP_RPM_Inp34) {
3617 case HDSP_RPM_Inp34_Phon_6dB:
3618 snd_iprintf(buffer, "Input 3/4: Phono, 6dB\n");
3619 break;
3620 case HDSP_RPM_Inp34_Phon_0dB:
3621 snd_iprintf(buffer, "Input 3/4: Phono, 0dB\n");
3622 break;
3623 case HDSP_RPM_Inp34_Phon_n6dB:
3624 snd_iprintf(buffer, "Input 3/4: Phono, -6dB\n");
3625 break;
3626 case HDSP_RPM_Inp34_Line_0dB:
3627 snd_iprintf(buffer, "Input 3/4: Line, 0dB\n");
3628 break;
3629 case HDSP_RPM_Inp34_Line_n6dB:
3630 snd_iprintf(buffer, "Input 3/4: Line, -6dB\n");
3631 break;
3632 default:
3633 snd_iprintf(buffer, "Input 3/4: ???\n");
3634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635
Florian Faber28b26e12010-12-01 12:14:47 +01003636 } else {
3637 if (hdsp->control_register & HDSP_SPDIFOpticalOut)
3638 snd_iprintf(buffer, "IEC958 output: Coaxial & ADAT1\n");
3639 else
3640 snd_iprintf(buffer, "IEC958 output: Coaxial only\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641
Florian Faber28b26e12010-12-01 12:14:47 +01003642 if (hdsp->control_register & HDSP_SPDIFProfessional)
3643 snd_iprintf(buffer, "IEC958 quality: Professional\n");
3644 else
3645 snd_iprintf(buffer, "IEC958 quality: Consumer\n");
3646
3647 if (hdsp->control_register & HDSP_SPDIFEmphasis)
3648 snd_iprintf(buffer, "IEC958 emphasis: on\n");
3649 else
3650 snd_iprintf(buffer, "IEC958 emphasis: off\n");
3651
3652 if (hdsp->control_register & HDSP_SPDIFNonAudio)
3653 snd_iprintf(buffer, "IEC958 NonAudio: on\n");
3654 else
3655 snd_iprintf(buffer, "IEC958 NonAudio: off\n");
3656 x = hdsp_spdif_sample_rate(hdsp);
3657 if (x != 0)
3658 snd_iprintf(buffer, "IEC958 sample rate: %d\n", x);
3659 else
3660 snd_iprintf(buffer, "IEC958 sample rate: Error flag set\n");
3661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 snd_iprintf(buffer, "\n");
3663
3664 /* Sync Check */
3665 x = status & HDSP_Sync0;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003666 if (status & HDSP_Lock0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 snd_iprintf(buffer, "ADAT1: %s\n", x ? "Sync" : "Lock");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003668 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 snd_iprintf(buffer, "ADAT1: No Lock\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670
3671 switch (hdsp->io_type) {
3672 case Digiface:
3673 case H9652:
3674 x = status & HDSP_Sync1;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003675 if (status & HDSP_Lock1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 snd_iprintf(buffer, "ADAT2: %s\n", x ? "Sync" : "Lock");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003677 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 snd_iprintf(buffer, "ADAT2: No Lock\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 x = status & HDSP_Sync2;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003680 if (status & HDSP_Lock2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 snd_iprintf(buffer, "ADAT3: %s\n", x ? "Sync" : "Lock");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003682 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 snd_iprintf(buffer, "ADAT3: No Lock\n");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003684 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 default:
3686 /* relax */
3687 break;
3688 }
3689
3690 x = status & HDSP_SPDIFSync;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003691 if (status & HDSP_SPDIFErrorFlag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 snd_iprintf (buffer, "SPDIF: No Lock\n");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003693 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 snd_iprintf (buffer, "SPDIF: %s\n", x ? "Sync" : "Lock");
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003695
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 x = status2 & HDSP_wc_sync;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003697 if (status2 & HDSP_wc_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 snd_iprintf (buffer, "Word Clock: %s\n", x ? "Sync" : "Lock");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003699 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 snd_iprintf (buffer, "Word Clock: No Lock\n");
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003701
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 x = status & HDSP_TimecodeSync;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003703 if (status & HDSP_TimecodeLock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 snd_iprintf(buffer, "ADAT Sync: %s\n", x ? "Sync" : "Lock");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003705 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 snd_iprintf(buffer, "ADAT Sync: No Lock\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707
3708 snd_iprintf(buffer, "\n");
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003709
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 /* Informations about H9632 specific controls */
3711 if (hdsp->io_type == H9632) {
3712 char *tmp;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003713
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 switch (hdsp_ad_gain(hdsp)) {
3715 case 0:
3716 tmp = "-10 dBV";
3717 break;
3718 case 1:
3719 tmp = "+4 dBu";
3720 break;
3721 default:
3722 tmp = "Lo Gain";
3723 break;
3724 }
3725 snd_iprintf(buffer, "AD Gain : %s\n", tmp);
3726
3727 switch (hdsp_da_gain(hdsp)) {
3728 case 0:
3729 tmp = "Hi Gain";
3730 break;
3731 case 1:
3732 tmp = "+4 dBu";
3733 break;
3734 default:
3735 tmp = "-10 dBV";
3736 break;
3737 }
3738 snd_iprintf(buffer, "DA Gain : %s\n", tmp);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003739
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 switch (hdsp_phone_gain(hdsp)) {
3741 case 0:
3742 tmp = "0 dB";
3743 break;
3744 case 1:
3745 tmp = "-6 dB";
3746 break;
3747 default:
3748 tmp = "-12 dB";
3749 break;
3750 }
3751 snd_iprintf(buffer, "Phones Gain : %s\n", tmp);
3752
Adrian Knoth4833c672013-01-15 18:52:22 +01003753 snd_iprintf(buffer, "XLR Breakout Cable : %s\n",
3754 hdsp_toggle_setting(hdsp, HDSP_XLRBreakoutCable) ?
3755 "yes" : "no");
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003756
Takashi Iwaib0b98112005-10-20 18:29:58 +02003757 if (hdsp->control_register & HDSP_AnalogExtensionBoard)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758 snd_iprintf(buffer, "AEB : on (ADAT1 internal)\n");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003759 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 snd_iprintf(buffer, "AEB : off (ADAT1 external)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 snd_iprintf(buffer, "\n");
3762 }
3763
3764}
3765
Randy Dunlap1374f8c2008-01-16 14:55:42 +01003766static void snd_hdsp_proc_init(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003768 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769
3770 if (! snd_card_proc_new(hdsp->card, "hdsp", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02003771 snd_info_set_text_ops(entry, hdsp, snd_hdsp_proc_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772}
3773
Takashi Iwai55e957d2005-11-17 14:52:13 +01003774static void snd_hdsp_free_buffers(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775{
3776 snd_hammerfall_free_buffer(&hdsp->capture_dma_buf, hdsp->pci);
3777 snd_hammerfall_free_buffer(&hdsp->playback_dma_buf, hdsp->pci);
3778}
3779
Bill Pembertone23e7a12012-12-06 12:35:10 -05003780static int snd_hdsp_initialize_memory(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781{
3782 unsigned long pb_bus, cb_bus;
3783
3784 if (snd_hammerfall_get_buffer(hdsp->pci, &hdsp->capture_dma_buf, HDSP_DMA_AREA_BYTES) < 0 ||
3785 snd_hammerfall_get_buffer(hdsp->pci, &hdsp->playback_dma_buf, HDSP_DMA_AREA_BYTES) < 0) {
3786 if (hdsp->capture_dma_buf.area)
3787 snd_dma_free_pages(&hdsp->capture_dma_buf);
3788 printk(KERN_ERR "%s: no buffers available\n", hdsp->card_name);
3789 return -ENOMEM;
3790 }
3791
3792 /* Align to bus-space 64K boundary */
3793
Clemens Ladisch7ab39922006-10-09 08:13:32 +02003794 cb_bus = ALIGN(hdsp->capture_dma_buf.addr, 0x10000ul);
3795 pb_bus = ALIGN(hdsp->playback_dma_buf.addr, 0x10000ul);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796
3797 /* Tell the card where it is */
3798
3799 hdsp_write(hdsp, HDSP_inputBufferAddress, cb_bus);
3800 hdsp_write(hdsp, HDSP_outputBufferAddress, pb_bus);
3801
3802 hdsp->capture_buffer = hdsp->capture_dma_buf.area + (cb_bus - hdsp->capture_dma_buf.addr);
3803 hdsp->playback_buffer = hdsp->playback_dma_buf.area + (pb_bus - hdsp->playback_dma_buf.addr);
3804
3805 return 0;
3806}
3807
Takashi Iwai55e957d2005-11-17 14:52:13 +01003808static int snd_hdsp_set_defaults(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809{
3810 unsigned int i;
3811
3812 /* ASSUMPTION: hdsp->lock is either held, or
3813 there is no need to hold it (e.g. during module
Joe Perches561de312007-12-18 13:13:47 +01003814 initialization).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 */
3816
3817 /* set defaults:
3818
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003819 SPDIF Input via Coax
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 Master clock mode
3821 maximum latency (7 => 2^7 = 8192 samples, 64Kbyte buffer,
3822 which implies 2 4096 sample, 32Kbyte periods).
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003823 Enable line out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 */
3825
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003826 hdsp->control_register = HDSP_ClockModeMaster |
3827 HDSP_SPDIFInputCoaxial |
3828 hdsp_encode_latency(7) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 HDSP_LineOut;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003830
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831
3832 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
3833
3834#ifdef SNDRV_BIG_ENDIAN
3835 hdsp->control2_register = HDSP_BIGENDIAN_MODE;
3836#else
3837 hdsp->control2_register = 0;
3838#endif
Takashi Iwaib0b98112005-10-20 18:29:58 +02003839 if (hdsp->io_type == H9652)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840 snd_hdsp_9652_enable_mixer (hdsp);
Takashi Iwaib0b98112005-10-20 18:29:58 +02003841 else
3842 hdsp_write (hdsp, HDSP_control2Reg, hdsp->control2_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843
3844 hdsp_reset_hw_pointer(hdsp);
3845 hdsp_compute_period_size(hdsp);
3846
3847 /* silence everything */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003848
Takashi Iwaib0b98112005-10-20 18:29:58 +02003849 for (i = 0; i < HDSP_MATRIX_MIXER_SIZE; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 hdsp->mixer_matrix[i] = MINUS_INFINITY_GAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851
3852 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 +02003853 if (hdsp_write_gain (hdsp, i, MINUS_INFINITY_GAIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003856
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 /* H9632 specific defaults */
3858 if (hdsp->io_type == H9632) {
3859 hdsp->control_register |= (HDSP_DAGainPlus4dBu | HDSP_ADGainPlus4dBu | HDSP_PhoneGain0dB);
3860 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
3861 }
3862
3863 /* set a default rate so that the channel map is set up.
3864 */
3865
3866 hdsp_set_rate(hdsp, 48000, 1);
3867
3868 return 0;
3869}
3870
3871static void hdsp_midi_tasklet(unsigned long arg)
3872{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003873 struct hdsp *hdsp = (struct hdsp *)arg;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003874
Takashi Iwaib0b98112005-10-20 18:29:58 +02003875 if (hdsp->midi[0].pending)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 snd_hdsp_midi_input_read (&hdsp->midi[0]);
Takashi Iwaib0b98112005-10-20 18:29:58 +02003877 if (hdsp->midi[1].pending)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 snd_hdsp_midi_input_read (&hdsp->midi[1]);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003879}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880
David Howells7d12e782006-10-05 14:55:46 +01003881static irqreturn_t snd_hdsp_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003883 struct hdsp *hdsp = (struct hdsp *) dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884 unsigned int status;
3885 int audio;
3886 int midi0;
3887 int midi1;
3888 unsigned int midi0status;
3889 unsigned int midi1status;
3890 int schedule = 0;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003891
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 status = hdsp_read(hdsp, HDSP_statusRegister);
3893
3894 audio = status & HDSP_audioIRQPending;
3895 midi0 = status & HDSP_midi0IRQPending;
3896 midi1 = status & HDSP_midi1IRQPending;
3897
Takashi Iwaib0b98112005-10-20 18:29:58 +02003898 if (!audio && !midi0 && !midi1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900
3901 hdsp_write(hdsp, HDSP_interruptConfirmation, 0);
3902
3903 midi0status = hdsp_read (hdsp, HDSP_midiStatusIn0) & 0xff;
3904 midi1status = hdsp_read (hdsp, HDSP_midiStatusIn1) & 0xff;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003905
Takashi Iwaic2503cd2009-03-05 09:37:40 +01003906 if (!(hdsp->state & HDSP_InitializationComplete))
3907 return IRQ_HANDLED;
3908
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 if (audio) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02003910 if (hdsp->capture_substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911 snd_pcm_period_elapsed(hdsp->pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003912
Takashi Iwaib0b98112005-10-20 18:29:58 +02003913 if (hdsp->playback_substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 snd_pcm_period_elapsed(hdsp->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003916
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 if (midi0 && midi0status) {
3918 if (hdsp->use_midi_tasklet) {
3919 /* we disable interrupts for this input until processing is done */
3920 hdsp->control_register &= ~HDSP_Midi0InterruptEnable;
3921 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
3922 hdsp->midi[0].pending = 1;
3923 schedule = 1;
3924 } else {
3925 snd_hdsp_midi_input_read (&hdsp->midi[0]);
3926 }
3927 }
Florian Faber28b26e12010-12-01 12:14:47 +01003928 if (hdsp->io_type != Multiface && hdsp->io_type != RPM && hdsp->io_type != H9632 && midi1 && midi1status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 if (hdsp->use_midi_tasklet) {
3930 /* we disable interrupts for this input until processing is done */
3931 hdsp->control_register &= ~HDSP_Midi1InterruptEnable;
3932 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
3933 hdsp->midi[1].pending = 1;
3934 schedule = 1;
3935 } else {
3936 snd_hdsp_midi_input_read (&hdsp->midi[1]);
3937 }
3938 }
3939 if (hdsp->use_midi_tasklet && schedule)
Takashi Iwai1f041282008-12-18 12:17:55 +01003940 tasklet_schedule(&hdsp->midi_tasklet);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 return IRQ_HANDLED;
3942}
3943
Takashi Iwai55e957d2005-11-17 14:52:13 +01003944static snd_pcm_uframes_t snd_hdsp_hw_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003946 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 return hdsp_hw_pointer(hdsp);
3948}
3949
Takashi Iwai55e957d2005-11-17 14:52:13 +01003950static char *hdsp_channel_buffer_location(struct hdsp *hdsp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 int stream,
3952 int channel)
3953
3954{
3955 int mapped_channel;
3956
Takashi Iwaida3cec32008-08-08 17:12:14 +02003957 if (snd_BUG_ON(channel < 0 || channel >= hdsp->max_channels))
3958 return NULL;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003959
Takashi Iwaib0b98112005-10-20 18:29:58 +02003960 if ((mapped_channel = hdsp->channel_map[channel]) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 return NULL;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003962
Takashi Iwaib0b98112005-10-20 18:29:58 +02003963 if (stream == SNDRV_PCM_STREAM_CAPTURE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 return hdsp->capture_buffer + (mapped_channel * HDSP_CHANNEL_BUFFER_BYTES);
Takashi Iwaib0b98112005-10-20 18:29:58 +02003965 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 return hdsp->playback_buffer + (mapped_channel * HDSP_CHANNEL_BUFFER_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967}
3968
Takashi Iwai55e957d2005-11-17 14:52:13 +01003969static int snd_hdsp_playback_copy(struct snd_pcm_substream *substream, int channel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 snd_pcm_uframes_t pos, void __user *src, snd_pcm_uframes_t count)
3971{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003972 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 char *channel_buf;
3974
Takashi Iwaida3cec32008-08-08 17:12:14 +02003975 if (snd_BUG_ON(pos + count > HDSP_CHANNEL_BUFFER_BYTES / 4))
3976 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977
3978 channel_buf = hdsp_channel_buffer_location (hdsp, substream->pstr->stream, channel);
Takashi Iwaida3cec32008-08-08 17:12:14 +02003979 if (snd_BUG_ON(!channel_buf))
3980 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 if (copy_from_user(channel_buf + pos * 4, src, count * 4))
3982 return -EFAULT;
3983 return count;
3984}
3985
Takashi Iwai55e957d2005-11-17 14:52:13 +01003986static int snd_hdsp_capture_copy(struct snd_pcm_substream *substream, int channel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 snd_pcm_uframes_t pos, void __user *dst, snd_pcm_uframes_t count)
3988{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003989 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 char *channel_buf;
3991
Takashi Iwaida3cec32008-08-08 17:12:14 +02003992 if (snd_BUG_ON(pos + count > HDSP_CHANNEL_BUFFER_BYTES / 4))
3993 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994
3995 channel_buf = hdsp_channel_buffer_location (hdsp, substream->pstr->stream, channel);
Takashi Iwaida3cec32008-08-08 17:12:14 +02003996 if (snd_BUG_ON(!channel_buf))
3997 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 if (copy_to_user(dst, channel_buf + pos * 4, count * 4))
3999 return -EFAULT;
4000 return count;
4001}
4002
Takashi Iwai55e957d2005-11-17 14:52:13 +01004003static int snd_hdsp_hw_silence(struct snd_pcm_substream *substream, int channel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 snd_pcm_uframes_t pos, snd_pcm_uframes_t count)
4005{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004006 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 char *channel_buf;
4008
4009 channel_buf = hdsp_channel_buffer_location (hdsp, substream->pstr->stream, channel);
Takashi Iwaida3cec32008-08-08 17:12:14 +02004010 if (snd_BUG_ON(!channel_buf))
4011 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 memset(channel_buf + pos * 4, 0, count * 4);
4013 return count;
4014}
4015
Takashi Iwai55e957d2005-11-17 14:52:13 +01004016static int snd_hdsp_reset(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004018 struct snd_pcm_runtime *runtime = substream->runtime;
4019 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
4020 struct snd_pcm_substream *other;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
4022 other = hdsp->capture_substream;
4023 else
4024 other = hdsp->playback_substream;
4025 if (hdsp->running)
4026 runtime->status->hw_ptr = hdsp_hw_pointer(hdsp);
4027 else
4028 runtime->status->hw_ptr = 0;
4029 if (other) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004030 struct snd_pcm_substream *s;
4031 struct snd_pcm_runtime *oruntime = other->runtime;
Takashi Iwaief991b92007-02-22 12:52:53 +01004032 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033 if (s == other) {
4034 oruntime->status->hw_ptr = runtime->status->hw_ptr;
4035 break;
4036 }
4037 }
4038 }
4039 return 0;
4040}
4041
Takashi Iwai55e957d2005-11-17 14:52:13 +01004042static int snd_hdsp_hw_params(struct snd_pcm_substream *substream,
4043 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004045 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 int err;
4047 pid_t this_pid;
4048 pid_t other_pid;
4049
Takashi Iwaib0b98112005-10-20 18:29:58 +02004050 if (hdsp_check_for_iobox (hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052
Takashi Iwaib0b98112005-10-20 18:29:58 +02004053 if (hdsp_check_for_firmware(hdsp, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055
4056 spin_lock_irq(&hdsp->lock);
4057
4058 if (substream->pstr->stream == SNDRV_PCM_STREAM_PLAYBACK) {
4059 hdsp->control_register &= ~(HDSP_SPDIFProfessional | HDSP_SPDIFNonAudio | HDSP_SPDIFEmphasis);
4060 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register |= hdsp->creg_spdif_stream);
4061 this_pid = hdsp->playback_pid;
4062 other_pid = hdsp->capture_pid;
4063 } else {
4064 this_pid = hdsp->capture_pid;
4065 other_pid = hdsp->playback_pid;
4066 }
4067
4068 if ((other_pid > 0) && (this_pid != other_pid)) {
4069
4070 /* The other stream is open, and not by the same
4071 task as this one. Make sure that the parameters
4072 that matter are the same.
4073 */
4074
4075 if (params_rate(params) != hdsp->system_sample_rate) {
4076 spin_unlock_irq(&hdsp->lock);
4077 _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_RATE);
4078 return -EBUSY;
4079 }
4080
4081 if (params_period_size(params) != hdsp->period_bytes / 4) {
4082 spin_unlock_irq(&hdsp->lock);
4083 _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
4084 return -EBUSY;
4085 }
4086
4087 /* We're fine. */
4088
4089 spin_unlock_irq(&hdsp->lock);
4090 return 0;
4091
4092 } else {
4093 spin_unlock_irq(&hdsp->lock);
4094 }
4095
4096 /* how to make sure that the rate matches an externally-set one ?
4097 */
4098
4099 spin_lock_irq(&hdsp->lock);
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02004100 if (! hdsp->clock_source_locked) {
4101 if ((err = hdsp_set_rate(hdsp, params_rate(params), 0)) < 0) {
4102 spin_unlock_irq(&hdsp->lock);
4103 _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_RATE);
4104 return err;
4105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 }
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02004107 spin_unlock_irq(&hdsp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108
4109 if ((err = hdsp_set_interrupt_interval(hdsp, params_period_size(params))) < 0) {
4110 _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
4111 return err;
4112 }
4113
4114 return 0;
4115}
4116
Takashi Iwai55e957d2005-11-17 14:52:13 +01004117static int snd_hdsp_channel_info(struct snd_pcm_substream *substream,
4118 struct snd_pcm_channel_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004120 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 int mapped_channel;
4122
Takashi Iwaida3cec32008-08-08 17:12:14 +02004123 if (snd_BUG_ON(info->channel >= hdsp->max_channels))
4124 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125
Takashi Iwaib0b98112005-10-20 18:29:58 +02004126 if ((mapped_channel = hdsp->channel_map[info->channel]) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128
4129 info->offset = mapped_channel * HDSP_CHANNEL_BUFFER_BYTES;
4130 info->first = 0;
4131 info->step = 32;
4132 return 0;
4133}
4134
Takashi Iwai55e957d2005-11-17 14:52:13 +01004135static int snd_hdsp_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 unsigned int cmd, void *arg)
4137{
4138 switch (cmd) {
4139 case SNDRV_PCM_IOCTL1_RESET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 return snd_hdsp_reset(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
Takashi Iwaib0b98112005-10-20 18:29:58 +02004142 return snd_hdsp_channel_info(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 default:
4144 break;
4145 }
4146
4147 return snd_pcm_lib_ioctl(substream, cmd, arg);
4148}
4149
Takashi Iwai55e957d2005-11-17 14:52:13 +01004150static int snd_hdsp_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004152 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
4153 struct snd_pcm_substream *other;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 int running;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004155
Takashi Iwaib0b98112005-10-20 18:29:58 +02004156 if (hdsp_check_for_iobox (hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158
Takashi Iwai311e70a2006-09-06 12:13:37 +02004159 if (hdsp_check_for_firmware(hdsp, 0)) /* no auto-loading in trigger */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161
4162 spin_lock(&hdsp->lock);
4163 running = hdsp->running;
4164 switch (cmd) {
4165 case SNDRV_PCM_TRIGGER_START:
4166 running |= 1 << substream->stream;
4167 break;
4168 case SNDRV_PCM_TRIGGER_STOP:
4169 running &= ~(1 << substream->stream);
4170 break;
4171 default:
4172 snd_BUG();
4173 spin_unlock(&hdsp->lock);
4174 return -EINVAL;
4175 }
4176 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
4177 other = hdsp->capture_substream;
4178 else
4179 other = hdsp->playback_substream;
4180
4181 if (other) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004182 struct snd_pcm_substream *s;
Takashi Iwaief991b92007-02-22 12:52:53 +01004183 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 if (s == other) {
4185 snd_pcm_trigger_done(s, substream);
4186 if (cmd == SNDRV_PCM_TRIGGER_START)
4187 running |= 1 << s->stream;
4188 else
4189 running &= ~(1 << s->stream);
4190 goto _ok;
4191 }
4192 }
4193 if (cmd == SNDRV_PCM_TRIGGER_START) {
4194 if (!(running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) &&
4195 substream->stream == SNDRV_PCM_STREAM_CAPTURE)
4196 hdsp_silence_playback(hdsp);
4197 } else {
4198 if (running &&
4199 substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
4200 hdsp_silence_playback(hdsp);
4201 }
4202 } else {
4203 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
4204 hdsp_silence_playback(hdsp);
4205 }
4206 _ok:
4207 snd_pcm_trigger_done(substream, substream);
4208 if (!hdsp->running && running)
4209 hdsp_start_audio(hdsp);
4210 else if (hdsp->running && !running)
4211 hdsp_stop_audio(hdsp);
4212 hdsp->running = running;
4213 spin_unlock(&hdsp->lock);
4214
4215 return 0;
4216}
4217
Takashi Iwai55e957d2005-11-17 14:52:13 +01004218static int snd_hdsp_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004220 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 int result = 0;
4222
Takashi Iwaib0b98112005-10-20 18:29:58 +02004223 if (hdsp_check_for_iobox (hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225
Takashi Iwaib0b98112005-10-20 18:29:58 +02004226 if (hdsp_check_for_firmware(hdsp, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228
4229 spin_lock_irq(&hdsp->lock);
4230 if (!hdsp->running)
4231 hdsp_reset_hw_pointer(hdsp);
4232 spin_unlock_irq(&hdsp->lock);
4233 return result;
4234}
4235
Takashi Iwai55e957d2005-11-17 14:52:13 +01004236static struct snd_pcm_hardware snd_hdsp_playback_subinfo =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237{
4238 .info = (SNDRV_PCM_INFO_MMAP |
4239 SNDRV_PCM_INFO_MMAP_VALID |
4240 SNDRV_PCM_INFO_NONINTERLEAVED |
4241 SNDRV_PCM_INFO_SYNC_START |
4242 SNDRV_PCM_INFO_DOUBLE),
4243#ifdef SNDRV_BIG_ENDIAN
4244 .formats = SNDRV_PCM_FMTBIT_S32_BE,
4245#else
4246 .formats = SNDRV_PCM_FMTBIT_S32_LE,
4247#endif
4248 .rates = (SNDRV_PCM_RATE_32000 |
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004249 SNDRV_PCM_RATE_44100 |
4250 SNDRV_PCM_RATE_48000 |
4251 SNDRV_PCM_RATE_64000 |
4252 SNDRV_PCM_RATE_88200 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 SNDRV_PCM_RATE_96000),
4254 .rate_min = 32000,
4255 .rate_max = 96000,
Florian Faber28b26e12010-12-01 12:14:47 +01004256 .channels_min = 6,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257 .channels_max = HDSP_MAX_CHANNELS,
4258 .buffer_bytes_max = HDSP_CHANNEL_BUFFER_BYTES * HDSP_MAX_CHANNELS,
4259 .period_bytes_min = (64 * 4) * 10,
4260 .period_bytes_max = (8192 * 4) * HDSP_MAX_CHANNELS,
4261 .periods_min = 2,
4262 .periods_max = 2,
4263 .fifo_size = 0
4264};
4265
Takashi Iwai55e957d2005-11-17 14:52:13 +01004266static struct snd_pcm_hardware snd_hdsp_capture_subinfo =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267{
4268 .info = (SNDRV_PCM_INFO_MMAP |
4269 SNDRV_PCM_INFO_MMAP_VALID |
4270 SNDRV_PCM_INFO_NONINTERLEAVED |
4271 SNDRV_PCM_INFO_SYNC_START),
4272#ifdef SNDRV_BIG_ENDIAN
4273 .formats = SNDRV_PCM_FMTBIT_S32_BE,
4274#else
4275 .formats = SNDRV_PCM_FMTBIT_S32_LE,
4276#endif
4277 .rates = (SNDRV_PCM_RATE_32000 |
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004278 SNDRV_PCM_RATE_44100 |
4279 SNDRV_PCM_RATE_48000 |
4280 SNDRV_PCM_RATE_64000 |
4281 SNDRV_PCM_RATE_88200 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 SNDRV_PCM_RATE_96000),
4283 .rate_min = 32000,
4284 .rate_max = 96000,
Florian Faber28b26e12010-12-01 12:14:47 +01004285 .channels_min = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 .channels_max = HDSP_MAX_CHANNELS,
4287 .buffer_bytes_max = HDSP_CHANNEL_BUFFER_BYTES * HDSP_MAX_CHANNELS,
4288 .period_bytes_min = (64 * 4) * 10,
4289 .period_bytes_max = (8192 * 4) * HDSP_MAX_CHANNELS,
4290 .periods_min = 2,
4291 .periods_max = 2,
4292 .fifo_size = 0
4293};
4294
4295static unsigned int hdsp_period_sizes[] = { 64, 128, 256, 512, 1024, 2048, 4096, 8192 };
4296
Takashi Iwai55e957d2005-11-17 14:52:13 +01004297static struct snd_pcm_hw_constraint_list hdsp_hw_constraints_period_sizes = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 .count = ARRAY_SIZE(hdsp_period_sizes),
4299 .list = hdsp_period_sizes,
4300 .mask = 0
4301};
4302
4303static unsigned int hdsp_9632_sample_rates[] = { 32000, 44100, 48000, 64000, 88200, 96000, 128000, 176400, 192000 };
4304
Takashi Iwai55e957d2005-11-17 14:52:13 +01004305static struct snd_pcm_hw_constraint_list hdsp_hw_constraints_9632_sample_rates = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 .count = ARRAY_SIZE(hdsp_9632_sample_rates),
4307 .list = hdsp_9632_sample_rates,
4308 .mask = 0
4309};
4310
Takashi Iwai55e957d2005-11-17 14:52:13 +01004311static int snd_hdsp_hw_rule_in_channels(struct snd_pcm_hw_params *params,
4312 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004314 struct hdsp *hdsp = rule->private;
4315 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 if (hdsp->io_type == H9632) {
4317 unsigned int list[3];
4318 list[0] = hdsp->qs_in_channels;
4319 list[1] = hdsp->ds_in_channels;
4320 list[2] = hdsp->ss_in_channels;
4321 return snd_interval_list(c, 3, list, 0);
4322 } else {
4323 unsigned int list[2];
4324 list[0] = hdsp->ds_in_channels;
4325 list[1] = hdsp->ss_in_channels;
4326 return snd_interval_list(c, 2, list, 0);
4327 }
4328}
4329
Takashi Iwai55e957d2005-11-17 14:52:13 +01004330static int snd_hdsp_hw_rule_out_channels(struct snd_pcm_hw_params *params,
4331 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332{
4333 unsigned int list[3];
Takashi Iwai55e957d2005-11-17 14:52:13 +01004334 struct hdsp *hdsp = rule->private;
4335 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 if (hdsp->io_type == H9632) {
4337 list[0] = hdsp->qs_out_channels;
4338 list[1] = hdsp->ds_out_channels;
4339 list[2] = hdsp->ss_out_channels;
4340 return snd_interval_list(c, 3, list, 0);
4341 } else {
4342 list[0] = hdsp->ds_out_channels;
4343 list[1] = hdsp->ss_out_channels;
4344 }
4345 return snd_interval_list(c, 2, list, 0);
4346}
4347
Takashi Iwai55e957d2005-11-17 14:52:13 +01004348static int snd_hdsp_hw_rule_in_channels_rate(struct snd_pcm_hw_params *params,
4349 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004351 struct hdsp *hdsp = rule->private;
4352 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
4353 struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 if (r->min > 96000 && hdsp->io_type == H9632) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004355 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 .min = hdsp->qs_in_channels,
4357 .max = hdsp->qs_in_channels,
4358 .integer = 1,
4359 };
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004360 return snd_interval_refine(c, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 } else if (r->min > 48000 && r->max <= 96000) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004362 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 .min = hdsp->ds_in_channels,
4364 .max = hdsp->ds_in_channels,
4365 .integer = 1,
4366 };
4367 return snd_interval_refine(c, &t);
4368 } else if (r->max < 64000) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004369 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 .min = hdsp->ss_in_channels,
4371 .max = hdsp->ss_in_channels,
4372 .integer = 1,
4373 };
4374 return snd_interval_refine(c, &t);
4375 }
4376 return 0;
4377}
4378
Takashi Iwai55e957d2005-11-17 14:52:13 +01004379static int snd_hdsp_hw_rule_out_channels_rate(struct snd_pcm_hw_params *params,
4380 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004382 struct hdsp *hdsp = rule->private;
4383 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
4384 struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 if (r->min > 96000 && hdsp->io_type == H9632) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004386 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387 .min = hdsp->qs_out_channels,
4388 .max = hdsp->qs_out_channels,
4389 .integer = 1,
4390 };
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004391 return snd_interval_refine(c, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 } else if (r->min > 48000 && r->max <= 96000) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004393 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 .min = hdsp->ds_out_channels,
4395 .max = hdsp->ds_out_channels,
4396 .integer = 1,
4397 };
4398 return snd_interval_refine(c, &t);
4399 } else if (r->max < 64000) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004400 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 .min = hdsp->ss_out_channels,
4402 .max = hdsp->ss_out_channels,
4403 .integer = 1,
4404 };
4405 return snd_interval_refine(c, &t);
4406 }
4407 return 0;
4408}
4409
Takashi Iwai55e957d2005-11-17 14:52:13 +01004410static int snd_hdsp_hw_rule_rate_out_channels(struct snd_pcm_hw_params *params,
4411 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004413 struct hdsp *hdsp = rule->private;
4414 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
4415 struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 if (c->min >= hdsp->ss_out_channels) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004417 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 .min = 32000,
4419 .max = 48000,
4420 .integer = 1,
4421 };
4422 return snd_interval_refine(r, &t);
4423 } else if (c->max <= hdsp->qs_out_channels && hdsp->io_type == H9632) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004424 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 .min = 128000,
4426 .max = 192000,
4427 .integer = 1,
4428 };
4429 return snd_interval_refine(r, &t);
4430 } else if (c->max <= hdsp->ds_out_channels) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004431 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 .min = 64000,
4433 .max = 96000,
4434 .integer = 1,
4435 };
4436 return snd_interval_refine(r, &t);
4437 }
4438 return 0;
4439}
4440
Takashi Iwai55e957d2005-11-17 14:52:13 +01004441static int snd_hdsp_hw_rule_rate_in_channels(struct snd_pcm_hw_params *params,
4442 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004444 struct hdsp *hdsp = rule->private;
4445 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
4446 struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 if (c->min >= hdsp->ss_in_channels) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004448 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449 .min = 32000,
4450 .max = 48000,
4451 .integer = 1,
4452 };
4453 return snd_interval_refine(r, &t);
4454 } else if (c->max <= hdsp->qs_in_channels && hdsp->io_type == H9632) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004455 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456 .min = 128000,
4457 .max = 192000,
4458 .integer = 1,
4459 };
4460 return snd_interval_refine(r, &t);
4461 } else if (c->max <= hdsp->ds_in_channels) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004462 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 .min = 64000,
4464 .max = 96000,
4465 .integer = 1,
4466 };
4467 return snd_interval_refine(r, &t);
4468 }
4469 return 0;
4470}
4471
Takashi Iwai55e957d2005-11-17 14:52:13 +01004472static int snd_hdsp_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004474 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
4475 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476
Takashi Iwaib0b98112005-10-20 18:29:58 +02004477 if (hdsp_check_for_iobox (hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479
Takashi Iwaib0b98112005-10-20 18:29:58 +02004480 if (hdsp_check_for_firmware(hdsp, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482
4483 spin_lock_irq(&hdsp->lock);
4484
4485 snd_pcm_set_sync(substream);
4486
4487 runtime->hw = snd_hdsp_playback_subinfo;
4488 runtime->dma_area = hdsp->playback_buffer;
4489 runtime->dma_bytes = HDSP_DMA_AREA_BYTES;
4490
4491 hdsp->playback_pid = current->pid;
4492 hdsp->playback_substream = substream;
4493
4494 spin_unlock_irq(&hdsp->lock);
4495
4496 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
4497 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 +02004498 if (hdsp->clock_source_locked) {
4499 runtime->hw.rate_min = runtime->hw.rate_max = hdsp->system_sample_rate;
4500 } else if (hdsp->io_type == H9632) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 runtime->hw.rate_max = 192000;
4502 runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
4503 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hdsp_hw_constraints_9632_sample_rates);
4504 }
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02004505 if (hdsp->io_type == H9632) {
4506 runtime->hw.channels_min = hdsp->qs_out_channels;
4507 runtime->hw.channels_max = hdsp->ss_out_channels;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004508 }
4509
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
4511 snd_hdsp_hw_rule_out_channels, hdsp,
4512 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
4513 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
4514 snd_hdsp_hw_rule_out_channels_rate, hdsp,
4515 SNDRV_PCM_HW_PARAM_RATE, -1);
4516 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
4517 snd_hdsp_hw_rule_rate_out_channels, hdsp,
4518 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
4519
Florian Faber28b26e12010-12-01 12:14:47 +01004520 if (RPM != hdsp->io_type) {
4521 hdsp->creg_spdif_stream = hdsp->creg_spdif;
4522 hdsp->spdif_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
4523 snd_ctl_notify(hdsp->card, SNDRV_CTL_EVENT_MASK_VALUE |
4524 SNDRV_CTL_EVENT_MASK_INFO, &hdsp->spdif_ctl->id);
4525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 return 0;
4527}
4528
Takashi Iwai55e957d2005-11-17 14:52:13 +01004529static int snd_hdsp_playback_release(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004531 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532
4533 spin_lock_irq(&hdsp->lock);
4534
4535 hdsp->playback_pid = -1;
4536 hdsp->playback_substream = NULL;
4537
4538 spin_unlock_irq(&hdsp->lock);
4539
Florian Faber28b26e12010-12-01 12:14:47 +01004540 if (RPM != hdsp->io_type) {
4541 hdsp->spdif_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
4542 snd_ctl_notify(hdsp->card, SNDRV_CTL_EVENT_MASK_VALUE |
4543 SNDRV_CTL_EVENT_MASK_INFO, &hdsp->spdif_ctl->id);
4544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545 return 0;
4546}
4547
4548
Takashi Iwai55e957d2005-11-17 14:52:13 +01004549static int snd_hdsp_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004551 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
4552 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553
Takashi Iwaib0b98112005-10-20 18:29:58 +02004554 if (hdsp_check_for_iobox (hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556
Takashi Iwaib0b98112005-10-20 18:29:58 +02004557 if (hdsp_check_for_firmware(hdsp, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559
4560 spin_lock_irq(&hdsp->lock);
4561
4562 snd_pcm_set_sync(substream);
4563
4564 runtime->hw = snd_hdsp_capture_subinfo;
4565 runtime->dma_area = hdsp->capture_buffer;
4566 runtime->dma_bytes = HDSP_DMA_AREA_BYTES;
4567
4568 hdsp->capture_pid = current->pid;
4569 hdsp->capture_substream = substream;
4570
4571 spin_unlock_irq(&hdsp->lock);
4572
4573 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
4574 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, &hdsp_hw_constraints_period_sizes);
4575 if (hdsp->io_type == H9632) {
4576 runtime->hw.channels_min = hdsp->qs_in_channels;
4577 runtime->hw.channels_max = hdsp->ss_in_channels;
4578 runtime->hw.rate_max = 192000;
4579 runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
4580 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hdsp_hw_constraints_9632_sample_rates);
4581 }
4582 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
4583 snd_hdsp_hw_rule_in_channels, hdsp,
4584 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
4585 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
4586 snd_hdsp_hw_rule_in_channels_rate, hdsp,
4587 SNDRV_PCM_HW_PARAM_RATE, -1);
4588 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
4589 snd_hdsp_hw_rule_rate_in_channels, hdsp,
4590 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
4591 return 0;
4592}
4593
Takashi Iwai55e957d2005-11-17 14:52:13 +01004594static int snd_hdsp_capture_release(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004596 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597
4598 spin_lock_irq(&hdsp->lock);
4599
4600 hdsp->capture_pid = -1;
4601 hdsp->capture_substream = NULL;
4602
4603 spin_unlock_irq(&hdsp->lock);
4604 return 0;
4605}
4606
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607/* helper functions for copying meter values */
4608static inline int copy_u32_le(void __user *dest, void __iomem *src)
4609{
4610 u32 val = readl(src);
4611 return copy_to_user(dest, &val, 4);
4612}
4613
4614static inline int copy_u64_le(void __user *dest, void __iomem *src_low, void __iomem *src_high)
4615{
4616 u32 rms_low, rms_high;
4617 u64 rms;
4618 rms_low = readl(src_low);
4619 rms_high = readl(src_high);
4620 rms = ((u64)rms_high << 32) | rms_low;
4621 return copy_to_user(dest, &rms, 8);
4622}
4623
4624static inline int copy_u48_le(void __user *dest, void __iomem *src_low, void __iomem *src_high)
4625{
4626 u32 rms_low, rms_high;
4627 u64 rms;
4628 rms_low = readl(src_low) & 0xffffff00;
4629 rms_high = readl(src_high) & 0xffffff00;
4630 rms = ((u64)rms_high << 32) | rms_low;
4631 return copy_to_user(dest, &rms, 8);
4632}
4633
Takashi Iwai55e957d2005-11-17 14:52:13 +01004634static int hdsp_9652_get_peak(struct hdsp *hdsp, struct hdsp_peak_rms __user *peak_rms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635{
4636 int doublespeed = 0;
4637 int i, j, channels, ofs;
4638
4639 if (hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DoubleSpeedStatus)
4640 doublespeed = 1;
4641 channels = doublespeed ? 14 : 26;
4642 for (i = 0, j = 0; i < 26; ++i) {
4643 if (doublespeed && (i & 4))
4644 continue;
4645 ofs = HDSP_9652_peakBase - j * 4;
4646 if (copy_u32_le(&peak_rms->input_peaks[i], hdsp->iobase + ofs))
4647 return -EFAULT;
4648 ofs -= channels * 4;
4649 if (copy_u32_le(&peak_rms->playback_peaks[i], hdsp->iobase + ofs))
4650 return -EFAULT;
4651 ofs -= channels * 4;
4652 if (copy_u32_le(&peak_rms->output_peaks[i], hdsp->iobase + ofs))
4653 return -EFAULT;
4654 ofs = HDSP_9652_rmsBase + j * 8;
4655 if (copy_u48_le(&peak_rms->input_rms[i], hdsp->iobase + ofs,
4656 hdsp->iobase + ofs + 4))
4657 return -EFAULT;
4658 ofs += channels * 8;
4659 if (copy_u48_le(&peak_rms->playback_rms[i], hdsp->iobase + ofs,
4660 hdsp->iobase + ofs + 4))
4661 return -EFAULT;
4662 ofs += channels * 8;
4663 if (copy_u48_le(&peak_rms->output_rms[i], hdsp->iobase + ofs,
4664 hdsp->iobase + ofs + 4))
4665 return -EFAULT;
4666 j++;
4667 }
4668 return 0;
4669}
4670
Takashi Iwai55e957d2005-11-17 14:52:13 +01004671static int hdsp_9632_get_peak(struct hdsp *hdsp, struct hdsp_peak_rms __user *peak_rms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672{
4673 int i, j;
Takashi Iwai55e957d2005-11-17 14:52:13 +01004674 struct hdsp_9632_meters __iomem *m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675 int doublespeed = 0;
4676
4677 if (hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DoubleSpeedStatus)
4678 doublespeed = 1;
Takashi Iwai55e957d2005-11-17 14:52:13 +01004679 m = (struct hdsp_9632_meters __iomem *)(hdsp->iobase+HDSP_9632_metersBase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 for (i = 0, j = 0; i < 16; ++i, ++j) {
4681 if (copy_u32_le(&peak_rms->input_peaks[i], &m->input_peak[j]))
4682 return -EFAULT;
4683 if (copy_u32_le(&peak_rms->playback_peaks[i], &m->playback_peak[j]))
4684 return -EFAULT;
4685 if (copy_u32_le(&peak_rms->output_peaks[i], &m->output_peak[j]))
4686 return -EFAULT;
4687 if (copy_u64_le(&peak_rms->input_rms[i], &m->input_rms_low[j],
4688 &m->input_rms_high[j]))
4689 return -EFAULT;
4690 if (copy_u64_le(&peak_rms->playback_rms[i], &m->playback_rms_low[j],
4691 &m->playback_rms_high[j]))
4692 return -EFAULT;
4693 if (copy_u64_le(&peak_rms->output_rms[i], &m->output_rms_low[j],
4694 &m->output_rms_high[j]))
4695 return -EFAULT;
4696 if (doublespeed && i == 3) i += 4;
4697 }
4698 return 0;
4699}
4700
Takashi Iwai55e957d2005-11-17 14:52:13 +01004701static int hdsp_get_peak(struct hdsp *hdsp, struct hdsp_peak_rms __user *peak_rms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702{
4703 int i;
4704
4705 for (i = 0; i < 26; i++) {
4706 if (copy_u32_le(&peak_rms->playback_peaks[i],
4707 hdsp->iobase + HDSP_playbackPeakLevel + i * 4))
4708 return -EFAULT;
4709 if (copy_u32_le(&peak_rms->input_peaks[i],
4710 hdsp->iobase + HDSP_inputPeakLevel + i * 4))
4711 return -EFAULT;
4712 }
4713 for (i = 0; i < 28; i++) {
4714 if (copy_u32_le(&peak_rms->output_peaks[i],
4715 hdsp->iobase + HDSP_outputPeakLevel + i * 4))
4716 return -EFAULT;
4717 }
4718 for (i = 0; i < 26; ++i) {
4719 if (copy_u64_le(&peak_rms->playback_rms[i],
4720 hdsp->iobase + HDSP_playbackRmsLevel + i * 8 + 4,
4721 hdsp->iobase + HDSP_playbackRmsLevel + i * 8))
4722 return -EFAULT;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004723 if (copy_u64_le(&peak_rms->input_rms[i],
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 hdsp->iobase + HDSP_inputRmsLevel + i * 8 + 4,
4725 hdsp->iobase + HDSP_inputRmsLevel + i * 8))
4726 return -EFAULT;
4727 }
4728 return 0;
4729}
4730
Takashi Iwai55e957d2005-11-17 14:52:13 +01004731static 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 -07004732{
Joe Perches9fe856e2010-09-04 18:52:54 -07004733 struct hdsp *hdsp = hw->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734 void __user *argp = (void __user *)arg;
Tim Blechmann3ae7e2e2008-11-08 14:42:18 +01004735 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736
4737 switch (cmd) {
4738 case SNDRV_HDSP_IOCTL_GET_PEAK_RMS: {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004739 struct hdsp_peak_rms __user *peak_rms = (struct hdsp_peak_rms __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740
Tim Blechmann3ae7e2e2008-11-08 14:42:18 +01004741 err = hdsp_check_for_iobox(hdsp);
4742 if (err < 0)
4743 return err;
4744
4745 err = hdsp_check_for_firmware(hdsp, 1);
4746 if (err < 0)
4747 return err;
4748
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749 if (!(hdsp->state & HDSP_FirmwareLoaded)) {
4750 snd_printk(KERN_ERR "Hammerfall-DSP: firmware needs to be uploaded to the card.\n");
4751 return -EINVAL;
4752 }
4753
4754 switch (hdsp->io_type) {
4755 case H9652:
4756 return hdsp_9652_get_peak(hdsp, peak_rms);
4757 case H9632:
4758 return hdsp_9632_get_peak(hdsp, peak_rms);
4759 default:
4760 return hdsp_get_peak(hdsp, peak_rms);
4761 }
4762 }
4763 case SNDRV_HDSP_IOCTL_GET_CONFIG_INFO: {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004764 struct hdsp_config_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765 unsigned long flags;
4766 int i;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004767
Tim Blechmann3ae7e2e2008-11-08 14:42:18 +01004768 err = hdsp_check_for_iobox(hdsp);
4769 if (err < 0)
4770 return err;
4771
4772 err = hdsp_check_for_firmware(hdsp, 1);
4773 if (err < 0)
4774 return err;
4775
Dan Rosenberge68d3b32010-09-25 11:07:27 -04004776 memset(&info, 0, sizeof(info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 spin_lock_irqsave(&hdsp->lock, flags);
4778 info.pref_sync_ref = (unsigned char)hdsp_pref_sync_ref(hdsp);
4779 info.wordclock_sync_check = (unsigned char)hdsp_wc_sync_check(hdsp);
Takashi Iwaib0b98112005-10-20 18:29:58 +02004780 if (hdsp->io_type != H9632)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 info.adatsync_sync_check = (unsigned char)hdsp_adatsync_sync_check(hdsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 info.spdif_sync_check = (unsigned char)hdsp_spdif_sync_check(hdsp);
Florian Faber28b26e12010-12-01 12:14:47 +01004783 for (i = 0; i < ((hdsp->io_type != Multiface && hdsp->io_type != RPM && hdsp->io_type != H9632) ? 3 : 1); ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 info.adat_sync_check[i] = (unsigned char)hdsp_adat_sync_check(hdsp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785 info.spdif_in = (unsigned char)hdsp_spdif_in(hdsp);
Adrian Knoth4833c672013-01-15 18:52:22 +01004786 info.spdif_out = (unsigned char)hdsp_toggle_setting(hdsp,
4787 HDSP_SPDIFOpticalOut);
4788 info.spdif_professional = (unsigned char)
4789 hdsp_toggle_setting(hdsp, HDSP_SPDIFProfessional);
4790 info.spdif_emphasis = (unsigned char)
4791 hdsp_toggle_setting(hdsp, HDSP_SPDIFEmphasis);
4792 info.spdif_nonaudio = (unsigned char)
4793 hdsp_toggle_setting(hdsp, HDSP_SPDIFNonAudio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 info.spdif_sample_rate = hdsp_spdif_sample_rate(hdsp);
4795 info.system_sample_rate = hdsp->system_sample_rate;
4796 info.autosync_sample_rate = hdsp_external_sample_rate(hdsp);
4797 info.system_clock_mode = (unsigned char)hdsp_system_clock_mode(hdsp);
4798 info.clock_source = (unsigned char)hdsp_clock_source(hdsp);
4799 info.autosync_ref = (unsigned char)hdsp_autosync_ref(hdsp);
Adrian Knoth4833c672013-01-15 18:52:22 +01004800 info.line_out = (unsigned char)
4801 hdsp_toggle_setting(hdsp, HDSP_LineOut);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802 if (hdsp->io_type == H9632) {
4803 info.da_gain = (unsigned char)hdsp_da_gain(hdsp);
4804 info.ad_gain = (unsigned char)hdsp_ad_gain(hdsp);
4805 info.phone_gain = (unsigned char)hdsp_phone_gain(hdsp);
Adrian Knoth4833c672013-01-15 18:52:22 +01004806 info.xlr_breakout_cable =
4807 (unsigned char)hdsp_toggle_setting(hdsp,
4808 HDSP_XLRBreakoutCable);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004809
Florian Faber28b26e12010-12-01 12:14:47 +01004810 } else if (hdsp->io_type == RPM) {
4811 info.da_gain = (unsigned char) hdsp_rpm_input12(hdsp);
4812 info.ad_gain = (unsigned char) hdsp_rpm_input34(hdsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813 }
Takashi Iwaib0b98112005-10-20 18:29:58 +02004814 if (hdsp->io_type == H9632 || hdsp->io_type == H9652)
Adrian Knoth4833c672013-01-15 18:52:22 +01004815 info.analog_extension_board =
4816 (unsigned char)hdsp_toggle_setting(hdsp,
4817 HDSP_AnalogExtensionBoard);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818 spin_unlock_irqrestore(&hdsp->lock, flags);
4819 if (copy_to_user(argp, &info, sizeof(info)))
4820 return -EFAULT;
4821 break;
4822 }
4823 case SNDRV_HDSP_IOCTL_GET_9632_AEB: {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004824 struct hdsp_9632_aeb h9632_aeb;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004825
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826 if (hdsp->io_type != H9632) return -EINVAL;
4827 h9632_aeb.aebi = hdsp->ss_in_channels - H9632_SS_CHANNELS;
4828 h9632_aeb.aebo = hdsp->ss_out_channels - H9632_SS_CHANNELS;
4829 if (copy_to_user(argp, &h9632_aeb, sizeof(h9632_aeb)))
4830 return -EFAULT;
4831 break;
4832 }
4833 case SNDRV_HDSP_IOCTL_GET_VERSION: {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004834 struct hdsp_version hdsp_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835 int err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004836
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837 if (hdsp->io_type == H9652 || hdsp->io_type == H9632) return -EINVAL;
4838 if (hdsp->io_type == Undefined) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02004839 if ((err = hdsp_get_iobox_version(hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841 }
Dan Carpenterd14df332013-10-16 11:44:25 +03004842 memset(&hdsp_version, 0, sizeof(hdsp_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843 hdsp_version.io_type = hdsp->io_type;
4844 hdsp_version.firmware_rev = hdsp->firmware_rev;
Takashi Iwaib0b98112005-10-20 18:29:58 +02004845 if ((err = copy_to_user(argp, &hdsp_version, sizeof(hdsp_version))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 break;
4848 }
4849 case SNDRV_HDSP_IOCTL_UPLOAD_FIRMWARE: {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004850 struct hdsp_firmware __user *firmware;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851 u32 __user *firmware_data;
4852 int err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004853
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 if (hdsp->io_type == H9652 || hdsp->io_type == H9632) return -EINVAL;
4855 /* SNDRV_HDSP_IOCTL_GET_VERSION must have been called */
4856 if (hdsp->io_type == Undefined) return -EINVAL;
4857
4858 if (hdsp->state & (HDSP_FirmwareCached | HDSP_FirmwareLoaded))
4859 return -EBUSY;
4860
Takashi Iwaib0b98112005-10-20 18:29:58 +02004861 snd_printk(KERN_INFO "Hammerfall-DSP: initializing firmware upload\n");
Takashi Iwai55e957d2005-11-17 14:52:13 +01004862 firmware = (struct hdsp_firmware __user *)argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863
Takashi Iwaib0b98112005-10-20 18:29:58 +02004864 if (get_user(firmware_data, &firmware->firmware_data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865 return -EFAULT;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004866
Takashi Iwaib0b98112005-10-20 18:29:58 +02004867 if (hdsp_check_for_iobox (hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869
Takashi Iwai90caaef2012-11-22 16:55:11 +01004870 if (!hdsp->fw_uploaded) {
4871 hdsp->fw_uploaded = vmalloc(HDSP_FIRMWARE_SIZE);
4872 if (!hdsp->fw_uploaded)
4873 return -ENOMEM;
4874 }
4875
4876 if (copy_from_user(hdsp->fw_uploaded, firmware_data,
4877 HDSP_FIRMWARE_SIZE)) {
4878 vfree(hdsp->fw_uploaded);
4879 hdsp->fw_uploaded = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 return -EFAULT;
Takashi Iwai90caaef2012-11-22 16:55:11 +01004881 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004882
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883 hdsp->state |= HDSP_FirmwareCached;
4884
Takashi Iwaib0b98112005-10-20 18:29:58 +02004885 if ((err = snd_hdsp_load_firmware_from_cache(hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886 return err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004887
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888 if (!(hdsp->state & HDSP_InitializationComplete)) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02004889 if ((err = snd_hdsp_enable_io(hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890 return err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004891
4892 snd_hdsp_initialize_channels(hdsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893 snd_hdsp_initialize_midi_flush(hdsp);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004894
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895 if ((err = snd_hdsp_create_alsa_devices(hdsp->card, hdsp)) < 0) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02004896 snd_printk(KERN_ERR "Hammerfall-DSP: error creating alsa devices\n");
4897 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898 }
4899 }
4900 break;
4901 }
4902 case SNDRV_HDSP_IOCTL_GET_MIXER: {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004903 struct hdsp_mixer __user *mixer = (struct hdsp_mixer __user *)argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904 if (copy_to_user(mixer->matrix, hdsp->mixer_matrix, sizeof(unsigned short)*HDSP_MATRIX_MIXER_SIZE))
4905 return -EFAULT;
4906 break;
4907 }
4908 default:
4909 return -EINVAL;
4910 }
4911 return 0;
4912}
4913
Takashi Iwai55e957d2005-11-17 14:52:13 +01004914static struct snd_pcm_ops snd_hdsp_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 .open = snd_hdsp_playback_open,
4916 .close = snd_hdsp_playback_release,
4917 .ioctl = snd_hdsp_ioctl,
4918 .hw_params = snd_hdsp_hw_params,
4919 .prepare = snd_hdsp_prepare,
4920 .trigger = snd_hdsp_trigger,
4921 .pointer = snd_hdsp_hw_pointer,
4922 .copy = snd_hdsp_playback_copy,
4923 .silence = snd_hdsp_hw_silence,
4924};
4925
Takashi Iwai55e957d2005-11-17 14:52:13 +01004926static struct snd_pcm_ops snd_hdsp_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927 .open = snd_hdsp_capture_open,
4928 .close = snd_hdsp_capture_release,
4929 .ioctl = snd_hdsp_ioctl,
4930 .hw_params = snd_hdsp_hw_params,
4931 .prepare = snd_hdsp_prepare,
4932 .trigger = snd_hdsp_trigger,
4933 .pointer = snd_hdsp_hw_pointer,
4934 .copy = snd_hdsp_capture_copy,
4935};
4936
Takashi Iwai92eed662008-02-22 18:35:56 +01004937static int snd_hdsp_create_hwdep(struct snd_card *card, struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004939 struct snd_hwdep *hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940 int err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004941
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 if ((err = snd_hwdep_new(card, "HDSP hwdep", 0, &hw)) < 0)
4943 return err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004944
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 hdsp->hwdep = hw;
4946 hw->private_data = hdsp;
4947 strcpy(hw->name, "HDSP hwdep interface");
4948
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 hw->ops.ioctl = snd_hdsp_hwdep_ioctl;
Andre Schramm42eb9232012-05-07 18:52:51 +02004950 hw->ops.ioctl_compat = snd_hdsp_hwdep_ioctl;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004951
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952 return 0;
4953}
4954
Takashi Iwai55e957d2005-11-17 14:52:13 +01004955static int snd_hdsp_create_pcm(struct snd_card *card, struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004957 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958 int err;
4959
4960 if ((err = snd_pcm_new(card, hdsp->card_name, 0, 1, 1, &pcm)) < 0)
4961 return err;
4962
4963 hdsp->pcm = pcm;
4964 pcm->private_data = hdsp;
4965 strcpy(pcm->name, hdsp->card_name);
4966
4967 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_hdsp_playback_ops);
4968 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_hdsp_capture_ops);
4969
4970 pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
4971
4972 return 0;
4973}
4974
Takashi Iwai55e957d2005-11-17 14:52:13 +01004975static void snd_hdsp_9652_enable_mixer (struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976{
4977 hdsp->control2_register |= HDSP_9652_ENABLE_MIXER;
4978 hdsp_write (hdsp, HDSP_control2Reg, hdsp->control2_register);
4979}
4980
Takashi Iwai55e957d2005-11-17 14:52:13 +01004981static int snd_hdsp_enable_io (struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982{
4983 int i;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004984
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985 if (hdsp_fifo_wait (hdsp, 0, 100)) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02004986 snd_printk(KERN_ERR "Hammerfall-DSP: enable_io fifo_wait failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987 return -EIO;
4988 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004989
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990 for (i = 0; i < hdsp->max_channels; ++i) {
4991 hdsp_write (hdsp, HDSP_inputEnable + (4 * i), 1);
4992 hdsp_write (hdsp, HDSP_outputEnable + (4 * i), 1);
4993 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004994
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 return 0;
4996}
4997
Takashi Iwai55e957d2005-11-17 14:52:13 +01004998static void snd_hdsp_initialize_channels(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999{
5000 int status, aebi_channels, aebo_channels;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005001
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002 switch (hdsp->io_type) {
5003 case Digiface:
5004 hdsp->card_name = "RME Hammerfall DSP + Digiface";
5005 hdsp->ss_in_channels = hdsp->ss_out_channels = DIGIFACE_SS_CHANNELS;
5006 hdsp->ds_in_channels = hdsp->ds_out_channels = DIGIFACE_DS_CHANNELS;
5007 break;
5008
5009 case H9652:
5010 hdsp->card_name = "RME Hammerfall HDSP 9652";
5011 hdsp->ss_in_channels = hdsp->ss_out_channels = H9652_SS_CHANNELS;
5012 hdsp->ds_in_channels = hdsp->ds_out_channels = H9652_DS_CHANNELS;
5013 break;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005014
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015 case H9632:
5016 status = hdsp_read(hdsp, HDSP_statusRegister);
5017 /* HDSP_AEBx bits are low when AEB are connected */
5018 aebi_channels = (status & HDSP_AEBI) ? 0 : 4;
5019 aebo_channels = (status & HDSP_AEBO) ? 0 : 4;
5020 hdsp->card_name = "RME Hammerfall HDSP 9632";
5021 hdsp->ss_in_channels = H9632_SS_CHANNELS+aebi_channels;
5022 hdsp->ds_in_channels = H9632_DS_CHANNELS+aebi_channels;
5023 hdsp->qs_in_channels = H9632_QS_CHANNELS+aebi_channels;
5024 hdsp->ss_out_channels = H9632_SS_CHANNELS+aebo_channels;
5025 hdsp->ds_out_channels = H9632_DS_CHANNELS+aebo_channels;
5026 hdsp->qs_out_channels = H9632_QS_CHANNELS+aebo_channels;
5027 break;
5028
5029 case Multiface:
5030 hdsp->card_name = "RME Hammerfall DSP + Multiface";
5031 hdsp->ss_in_channels = hdsp->ss_out_channels = MULTIFACE_SS_CHANNELS;
5032 hdsp->ds_in_channels = hdsp->ds_out_channels = MULTIFACE_DS_CHANNELS;
5033 break;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005034
Florian Faber28b26e12010-12-01 12:14:47 +01005035 case RPM:
5036 hdsp->card_name = "RME Hammerfall DSP + RPM";
5037 hdsp->ss_in_channels = RPM_CHANNELS-1;
5038 hdsp->ss_out_channels = RPM_CHANNELS;
5039 hdsp->ds_in_channels = RPM_CHANNELS-1;
5040 hdsp->ds_out_channels = RPM_CHANNELS;
5041 break;
5042
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 default:
5044 /* should never get here */
5045 break;
5046 }
5047}
5048
Takashi Iwai55e957d2005-11-17 14:52:13 +01005049static void snd_hdsp_initialize_midi_flush (struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050{
5051 snd_hdsp_flush_midi_input (hdsp, 0);
5052 snd_hdsp_flush_midi_input (hdsp, 1);
5053}
5054
Takashi Iwai55e957d2005-11-17 14:52:13 +01005055static int snd_hdsp_create_alsa_devices(struct snd_card *card, struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056{
5057 int err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005058
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059 if ((err = snd_hdsp_create_pcm(card, hdsp)) < 0) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02005060 snd_printk(KERN_ERR "Hammerfall-DSP: Error creating pcm interface\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061 return err;
5062 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005063
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064
5065 if ((err = snd_hdsp_create_midi(card, hdsp, 0)) < 0) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02005066 snd_printk(KERN_ERR "Hammerfall-DSP: Error creating first midi interface\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067 return err;
5068 }
5069
5070 if (hdsp->io_type == Digiface || hdsp->io_type == H9652) {
5071 if ((err = snd_hdsp_create_midi(card, hdsp, 1)) < 0) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02005072 snd_printk(KERN_ERR "Hammerfall-DSP: Error creating second midi interface\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073 return err;
5074 }
5075 }
5076
5077 if ((err = snd_hdsp_create_controls(card, hdsp)) < 0) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02005078 snd_printk(KERN_ERR "Hammerfall-DSP: Error creating ctl interface\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079 return err;
5080 }
5081
5082 snd_hdsp_proc_init(hdsp);
5083
5084 hdsp->system_sample_rate = -1;
5085 hdsp->playback_pid = -1;
5086 hdsp->capture_pid = -1;
5087 hdsp->capture_substream = NULL;
5088 hdsp->playback_substream = NULL;
5089
5090 if ((err = snd_hdsp_set_defaults(hdsp)) < 0) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02005091 snd_printk(KERN_ERR "Hammerfall-DSP: Error setting default values\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092 return err;
5093 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005094
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 if (!(hdsp->state & HDSP_InitializationComplete)) {
Clemens Ladischb73c1c12005-09-02 08:49:21 +02005096 strcpy(card->shortname, "Hammerfall DSP");
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005097 sprintf(card->longname, "%s at 0x%lx, irq %d", hdsp->card_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098 hdsp->port, hdsp->irq);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005099
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100 if ((err = snd_card_register(card)) < 0) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02005101 snd_printk(KERN_ERR "Hammerfall-DSP: error registering card\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102 return err;
5103 }
5104 hdsp->state |= HDSP_InitializationComplete;
5105 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005106
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107 return 0;
5108}
5109
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110/* load firmware via hotplug fw loader */
Takashi Iwai92eed662008-02-22 18:35:56 +01005111static int hdsp_request_fw_loader(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112{
5113 const char *fwfile;
5114 const struct firmware *fw;
5115 int err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005116
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117 if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
5118 return 0;
5119 if (hdsp->io_type == Undefined) {
5120 if ((err = hdsp_get_iobox_version(hdsp)) < 0)
5121 return err;
5122 if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
5123 return 0;
5124 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005125
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126 /* caution: max length of firmware filename is 30! */
5127 switch (hdsp->io_type) {
Florian Faber28b26e12010-12-01 12:14:47 +01005128 case RPM:
5129 fwfile = "rpm_firmware.bin";
5130 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131 case Multiface:
5132 if (hdsp->firmware_rev == 0xa)
5133 fwfile = "multiface_firmware.bin";
5134 else
5135 fwfile = "multiface_firmware_rev11.bin";
5136 break;
5137 case Digiface:
5138 if (hdsp->firmware_rev == 0xa)
5139 fwfile = "digiface_firmware.bin";
5140 else
5141 fwfile = "digiface_firmware_rev11.bin";
5142 break;
5143 default:
5144 snd_printk(KERN_ERR "Hammerfall-DSP: invalid io_type %d\n", hdsp->io_type);
5145 return -EINVAL;
5146 }
5147
5148 if (request_firmware(&fw, fwfile, &hdsp->pci->dev)) {
5149 snd_printk(KERN_ERR "Hammerfall-DSP: cannot load firmware %s\n", fwfile);
5150 return -ENOENT;
5151 }
Takashi Iwai90caaef2012-11-22 16:55:11 +01005152 if (fw->size < HDSP_FIRMWARE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153 snd_printk(KERN_ERR "Hammerfall-DSP: too short firmware size %d (expected %d)\n",
Takashi Iwai90caaef2012-11-22 16:55:11 +01005154 (int)fw->size, HDSP_FIRMWARE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155 return -EINVAL;
5156 }
Thomas Charbonnel7679a032005-04-25 11:35:29 +02005157
Takashi Iwai90caaef2012-11-22 16:55:11 +01005158 hdsp->firmware = fw;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005159
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160 hdsp->state |= HDSP_FirmwareCached;
5161
5162 if ((err = snd_hdsp_load_firmware_from_cache(hdsp)) < 0)
5163 return err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005164
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165 if (!(hdsp->state & HDSP_InitializationComplete)) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02005166 if ((err = snd_hdsp_enable_io(hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168
5169 if ((err = snd_hdsp_create_hwdep(hdsp->card, hdsp)) < 0) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02005170 snd_printk(KERN_ERR "Hammerfall-DSP: error creating hwdep device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171 return err;
5172 }
5173 snd_hdsp_initialize_channels(hdsp);
5174 snd_hdsp_initialize_midi_flush(hdsp);
5175 if ((err = snd_hdsp_create_alsa_devices(hdsp->card, hdsp)) < 0) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02005176 snd_printk(KERN_ERR "Hammerfall-DSP: error creating alsa devices\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177 return err;
5178 }
5179 }
5180 return 0;
5181}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182
Bill Pembertone23e7a12012-12-06 12:35:10 -05005183static int snd_hdsp_create(struct snd_card *card,
5184 struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185{
5186 struct pci_dev *pci = hdsp->pci;
5187 int err;
5188 int is_9652 = 0;
5189 int is_9632 = 0;
5190
5191 hdsp->irq = -1;
5192 hdsp->state = 0;
5193 hdsp->midi[0].rmidi = NULL;
5194 hdsp->midi[1].rmidi = NULL;
5195 hdsp->midi[0].input = NULL;
5196 hdsp->midi[1].input = NULL;
5197 hdsp->midi[0].output = NULL;
5198 hdsp->midi[1].output = NULL;
5199 hdsp->midi[0].pending = 0;
5200 hdsp->midi[1].pending = 0;
5201 spin_lock_init(&hdsp->midi[0].lock);
5202 spin_lock_init(&hdsp->midi[1].lock);
5203 hdsp->iobase = NULL;
5204 hdsp->control_register = 0;
5205 hdsp->control2_register = 0;
5206 hdsp->io_type = Undefined;
5207 hdsp->max_channels = 26;
5208
5209 hdsp->card = card;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005210
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211 spin_lock_init(&hdsp->lock);
5212
5213 tasklet_init(&hdsp->midi_tasklet, hdsp_midi_tasklet, (unsigned long)hdsp);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005214
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215 pci_read_config_word(hdsp->pci, PCI_CLASS_REVISION, &hdsp->firmware_rev);
5216 hdsp->firmware_rev &= 0xff;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005217
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218 /* From Martin Bjoernsen :
5219 "It is important that the card's latency timer register in
5220 the PCI configuration space is set to a value much larger
5221 than 0 by the computer's BIOS or the driver.
5222 The windows driver always sets this 8 bit register [...]
5223 to its maximum 255 to avoid problems with some computers."
5224 */
5225 pci_write_config_byte(hdsp->pci, PCI_LATENCY_TIMER, 0xFF);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005226
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227 strcpy(card->driver, "H-DSP");
5228 strcpy(card->mixername, "Xilinx FPGA");
5229
Takashi Iwaib0b98112005-10-20 18:29:58 +02005230 if (hdsp->firmware_rev < 0xa)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231 return -ENODEV;
Takashi Iwaib0b98112005-10-20 18:29:58 +02005232 else if (hdsp->firmware_rev < 0x64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233 hdsp->card_name = "RME Hammerfall DSP";
Takashi Iwaib0b98112005-10-20 18:29:58 +02005234 else if (hdsp->firmware_rev < 0x96) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235 hdsp->card_name = "RME HDSP 9652";
5236 is_9652 = 1;
5237 } else {
5238 hdsp->card_name = "RME HDSP 9632";
5239 hdsp->max_channels = 16;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005240 is_9632 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005241 }
5242
Takashi Iwaib0b98112005-10-20 18:29:58 +02005243 if ((err = pci_enable_device(pci)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245
5246 pci_set_master(hdsp->pci);
5247
5248 if ((err = pci_request_regions(pci, "hdsp")) < 0)
5249 return err;
5250 hdsp->port = pci_resource_start(pci, 0);
5251 if ((hdsp->iobase = ioremap_nocache(hdsp->port, HDSP_IO_EXTENT)) == NULL) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02005252 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 -07005253 return -EBUSY;
5254 }
5255
Takashi Iwai437a5a42006-11-21 12:14:23 +01005256 if (request_irq(pci->irq, snd_hdsp_interrupt, IRQF_SHARED,
Takashi Iwai934c2b62011-06-10 16:36:37 +02005257 KBUILD_MODNAME, hdsp)) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02005258 snd_printk(KERN_ERR "Hammerfall-DSP: unable to use IRQ %d\n", pci->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259 return -EBUSY;
5260 }
5261
5262 hdsp->irq = pci->irq;
Remy Bruno176546a2006-10-16 12:32:53 +02005263 hdsp->precise_ptr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005264 hdsp->use_midi_tasklet = 1;
Remy Brunod7923b22006-10-17 12:41:56 +02005265 hdsp->dds_value = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266
Takashi Iwaib0b98112005-10-20 18:29:58 +02005267 if ((err = snd_hdsp_initialize_memory(hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268 return err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005269
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 if (!is_9652 && !is_9632) {
Tim Blechmanne588ed82009-02-20 19:30:35 +01005271 /* we wait a maximum of 10 seconds to let freshly
5272 * inserted cardbus cards do their hardware init */
5273 err = hdsp_wait_for_iobox(hdsp, 1000, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274
Tim Blechmann00c9ddd2008-11-09 12:50:52 +01005275 if (err < 0)
5276 return err;
5277
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278 if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02005279 if ((err = hdsp_request_fw_loader(hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280 /* we don't fail as this can happen
5281 if userspace is not ready for
5282 firmware upload
5283 */
Takashi Iwaib0b98112005-10-20 18:29:58 +02005284 snd_printk(KERN_ERR "Hammerfall-DSP: couldn't get firmware from userspace. try using hdsploader\n");
5285 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286 /* init is complete, we return */
5287 return 0;
Tim Blechmann00c9ddd2008-11-09 12:50:52 +01005288 /* we defer initialization */
Takashi Iwaib0b98112005-10-20 18:29:58 +02005289 snd_printk(KERN_INFO "Hammerfall-DSP: card initialization pending : waiting for firmware\n");
5290 if ((err = snd_hdsp_create_hwdep(card, hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292 return 0;
5293 } else {
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005294 snd_printk(KERN_INFO "Hammerfall-DSP: Firmware already present, initializing card.\n");
Florian Faber28b26e12010-12-01 12:14:47 +01005295 if (hdsp_read(hdsp, HDSP_status2Register) & HDSP_version2)
5296 hdsp->io_type = RPM;
5297 else if (hdsp_read(hdsp, HDSP_status2Register) & HDSP_version1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298 hdsp->io_type = Multiface;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005299 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300 hdsp->io_type = Digiface;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301 }
5302 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005303
Takashi Iwaib0b98112005-10-20 18:29:58 +02005304 if ((err = snd_hdsp_enable_io(hdsp)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305 return err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005306
Takashi Iwaib0b98112005-10-20 18:29:58 +02005307 if (is_9652)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308 hdsp->io_type = H9652;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005309
Takashi Iwaib0b98112005-10-20 18:29:58 +02005310 if (is_9632)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311 hdsp->io_type = H9632;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312
Takashi Iwaib0b98112005-10-20 18:29:58 +02005313 if ((err = snd_hdsp_create_hwdep(card, hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314 return err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005315
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 snd_hdsp_initialize_channels(hdsp);
5317 snd_hdsp_initialize_midi_flush(hdsp);
5318
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005319 hdsp->state |= HDSP_FirmwareLoaded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320
Takashi Iwaib0b98112005-10-20 18:29:58 +02005321 if ((err = snd_hdsp_create_alsa_devices(card, hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005324 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325}
5326
Takashi Iwai55e957d2005-11-17 14:52:13 +01005327static int snd_hdsp_free(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328{
5329 if (hdsp->port) {
5330 /* stop the audio, and cancel all interrupts */
5331 tasklet_kill(&hdsp->midi_tasklet);
5332 hdsp->control_register &= ~(HDSP_Start|HDSP_AudioInterruptEnable|HDSP_Midi0InterruptEnable|HDSP_Midi1InterruptEnable);
5333 hdsp_write (hdsp, HDSP_controlRegister, hdsp->control_register);
5334 }
5335
5336 if (hdsp->irq >= 0)
5337 free_irq(hdsp->irq, (void *)hdsp);
5338
5339 snd_hdsp_free_buffers(hdsp);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005340
Takashi Iwai90caaef2012-11-22 16:55:11 +01005341 if (hdsp->firmware)
5342 release_firmware(hdsp->firmware);
5343 vfree(hdsp->fw_uploaded);
5344
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345 if (hdsp->iobase)
5346 iounmap(hdsp->iobase);
5347
5348 if (hdsp->port)
5349 pci_release_regions(hdsp->pci);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005350
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351 pci_disable_device(hdsp->pci);
5352 return 0;
5353}
5354
Takashi Iwai55e957d2005-11-17 14:52:13 +01005355static void snd_hdsp_card_free(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356{
Joe Perches9fe856e2010-09-04 18:52:54 -07005357 struct hdsp *hdsp = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358
5359 if (hdsp)
5360 snd_hdsp_free(hdsp);
5361}
5362
Bill Pembertone23e7a12012-12-06 12:35:10 -05005363static int snd_hdsp_probe(struct pci_dev *pci,
5364 const struct pci_device_id *pci_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365{
5366 static int dev;
Takashi Iwai55e957d2005-11-17 14:52:13 +01005367 struct hdsp *hdsp;
5368 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369 int err;
5370
5371 if (dev >= SNDRV_CARDS)
5372 return -ENODEV;
5373 if (!enable[dev]) {
5374 dev++;
5375 return -ENOENT;
5376 }
5377
Takashi Iwai60c57722014-01-29 14:20:19 +01005378 err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
5379 sizeof(struct hdsp), &card);
Takashi Iwaie58de7b2008-12-28 16:44:30 +01005380 if (err < 0)
5381 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382
Joe Perches9fe856e2010-09-04 18:52:54 -07005383 hdsp = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384 card->private_free = snd_hdsp_card_free;
5385 hdsp->dev = dev;
5386 hdsp->pci = pci;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387
5388 if ((err = snd_hdsp_create(card, hdsp)) < 0) {
5389 snd_card_free(card);
5390 return err;
5391 }
5392
5393 strcpy(card->shortname, "Hammerfall DSP");
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005394 sprintf(card->longname, "%s at 0x%lx, irq %d", hdsp->card_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395 hdsp->port, hdsp->irq);
5396
5397 if ((err = snd_card_register(card)) < 0) {
5398 snd_card_free(card);
5399 return err;
5400 }
5401 pci_set_drvdata(pci, card);
5402 dev++;
5403 return 0;
5404}
5405
Bill Pembertone23e7a12012-12-06 12:35:10 -05005406static void snd_hdsp_remove(struct pci_dev *pci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407{
5408 snd_card_free(pci_get_drvdata(pci));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409}
5410
Takashi Iwaie9f66d92012-04-24 12:25:00 +02005411static struct pci_driver hdsp_driver = {
Takashi Iwai3733e422011-06-10 16:20:20 +02005412 .name = KBUILD_MODNAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413 .id_table = snd_hdsp_ids,
5414 .probe = snd_hdsp_probe,
Bill Pembertone23e7a12012-12-06 12:35:10 -05005415 .remove = snd_hdsp_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416};
5417
Takashi Iwaie9f66d92012-04-24 12:25:00 +02005418module_pci_driver(hdsp_driver);