blob: 866d68461b83f25061f4e85af3df3b2840b3a339 [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_get_reserved_buf(dmab, snd_dma_pci_buf_id(pci))) {
588 if (dmab->bytes >= size)
589 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 }
Takashi Iwaib6a96912005-05-30 18:27:03 +0200591 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
592 size, dmab) < 0)
593 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return 0;
595}
596
597static void snd_hammerfall_free_buffer(struct snd_dma_buffer *dmab, struct pci_dev *pci)
598{
Takashi Iwaib6a96912005-05-30 18:27:03 +0200599 if (dmab->area) {
600 dmab->dev.dev = NULL; /* make it anonymous */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 snd_dma_reserve_buf(dmab, snd_dma_pci_buf_id(pci));
Takashi Iwaib6a96912005-05-30 18:27:03 +0200602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603}
604
605
Alexey Dobriyancebe41d2010-02-06 00:21:03 +0200606static DEFINE_PCI_DEVICE_TABLE(snd_hdsp_ids) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 {
608 .vendor = PCI_VENDOR_ID_XILINX,
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100609 .device = PCI_DEVICE_ID_XILINX_HAMMERFALL_DSP,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 .subvendor = PCI_ANY_ID,
611 .subdevice = PCI_ANY_ID,
612 }, /* RME Hammerfall-DSP */
613 { 0, },
614};
615
616MODULE_DEVICE_TABLE(pci, snd_hdsp_ids);
617
618/* prototypes */
Takashi Iwai55e957d2005-11-17 14:52:13 +0100619static int snd_hdsp_create_alsa_devices(struct snd_card *card, struct hdsp *hdsp);
620static int snd_hdsp_create_pcm(struct snd_card *card, struct hdsp *hdsp);
621static int snd_hdsp_enable_io (struct hdsp *hdsp);
622static void snd_hdsp_initialize_midi_flush (struct hdsp *hdsp);
623static void snd_hdsp_initialize_channels (struct hdsp *hdsp);
624static int hdsp_fifo_wait(struct hdsp *hdsp, int count, int timeout);
625static int hdsp_autosync_ref(struct hdsp *hdsp);
626static int snd_hdsp_set_defaults(struct hdsp *hdsp);
627static void snd_hdsp_9652_enable_mixer (struct hdsp *hdsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Takashi Iwai55e957d2005-11-17 14:52:13 +0100629static int hdsp_playback_to_output_key (struct hdsp *hdsp, int in, int out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
Remy Brunoa3a68c82007-08-31 12:33:54 +0200631 switch (hdsp->io_type) {
632 case Multiface:
633 case Digiface:
Florian Faber28b26e12010-12-01 12:14:47 +0100634 case RPM:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 default:
Andreas Degert192b8e32008-01-16 15:59:48 +0100636 if (hdsp->firmware_rev == 0xa)
637 return (64 * out) + (32 + (in));
638 else
639 return (52 * out) + (26 + (in));
Remy Brunoa3a68c82007-08-31 12:33:54 +0200640 case H9632:
641 return (32 * out) + (16 + (in));
642 case H9652:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 return (52 * out) + (26 + (in));
644 }
645}
646
Takashi Iwai55e957d2005-11-17 14:52:13 +0100647static int hdsp_input_to_output_key (struct hdsp *hdsp, int in, int out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
Remy Brunoa3a68c82007-08-31 12:33:54 +0200649 switch (hdsp->io_type) {
650 case Multiface:
651 case Digiface:
Florian Faber28b26e12010-12-01 12:14:47 +0100652 case RPM:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 default:
Andreas Degert192b8e32008-01-16 15:59:48 +0100654 if (hdsp->firmware_rev == 0xa)
655 return (64 * out) + in;
656 else
657 return (52 * out) + in;
Remy Brunoa3a68c82007-08-31 12:33:54 +0200658 case H9632:
659 return (32 * out) + in;
660 case H9652:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 return (52 * out) + in;
662 }
663}
664
Takashi Iwai55e957d2005-11-17 14:52:13 +0100665static void hdsp_write(struct hdsp *hdsp, int reg, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
667 writel(val, hdsp->iobase + reg);
668}
669
Takashi Iwai55e957d2005-11-17 14:52:13 +0100670static unsigned int hdsp_read(struct hdsp *hdsp, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
672 return readl (hdsp->iobase + reg);
673}
674
Takashi Iwai55e957d2005-11-17 14:52:13 +0100675static int hdsp_check_for_iobox (struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
Adrian Knoth0c2bc7c2013-01-15 18:52:20 +0100677 int i;
678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 if (hdsp->io_type == H9652 || hdsp->io_type == H9632) return 0;
Adrian Knoth0c2bc7c2013-01-15 18:52:20 +0100680 for (i = 0; i < 500; i++) {
681 if (0 == (hdsp_read(hdsp, HDSP_statusRegister) &
682 HDSP_ConfigError)) {
683 if (i) {
684 snd_printd("Hammerfall-DSP: IO box found after %d ms\n",
685 (20 * i));
686 }
687 return 0;
688 }
689 msleep(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 }
Adrian Knoth0c2bc7c2013-01-15 18:52:20 +0100691 snd_printk(KERN_ERR "Hammerfall-DSP: no IO box connected!\n");
692 hdsp->state &= ~HDSP_FirmwareLoaded;
693 return -EIO;
Tim Blechmanne588ed82009-02-20 19:30:35 +0100694}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Tim Blechmanne588ed82009-02-20 19:30:35 +0100696static int hdsp_wait_for_iobox(struct hdsp *hdsp, unsigned int loops,
697 unsigned int delay)
698{
699 unsigned int i;
700
701 if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
702 return 0;
703
704 for (i = 0; i != loops; ++i) {
705 if (hdsp_read(hdsp, HDSP_statusRegister) & HDSP_ConfigError)
706 msleep(delay);
707 else {
708 snd_printd("Hammerfall-DSP: iobox found after %ums!\n",
709 i * delay);
710 return 0;
711 }
712 }
713
Florian Faber28b26e12010-12-01 12:14:47 +0100714 snd_printk("Hammerfall-DSP: no IO box connected!\n");
Tim Blechmanne588ed82009-02-20 19:30:35 +0100715 hdsp->state &= ~HDSP_FirmwareLoaded;
716 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717}
718
Takashi Iwai55e957d2005-11-17 14:52:13 +0100719static int snd_hdsp_load_firmware_from_cache(struct hdsp *hdsp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
721 int i;
722 unsigned long flags;
Takashi Iwai90caaef2012-11-22 16:55:11 +0100723 const u32 *cache;
724
725 if (hdsp->fw_uploaded)
726 cache = hdsp->fw_uploaded;
727 else {
728 if (!hdsp->firmware)
729 return -ENODEV;
730 cache = (u32 *)hdsp->firmware->data;
731 if (!cache)
732 return -ENODEV;
733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735 if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 snd_printk ("Hammerfall-DSP: loading firmware\n");
738
739 hdsp_write (hdsp, HDSP_control2Reg, HDSP_S_PROGRAM);
740 hdsp_write (hdsp, HDSP_fifoData, 0);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 if (hdsp_fifo_wait (hdsp, 0, HDSP_LONG_WAIT)) {
743 snd_printk ("Hammerfall-DSP: timeout waiting for download preparation\n");
Adrian Knoth0c2bc7c2013-01-15 18:52:20 +0100744 hdsp_write(hdsp, HDSP_control2Reg, HDSP_S200);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 return -EIO;
746 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 hdsp_write (hdsp, HDSP_control2Reg, HDSP_S_LOAD);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100749
Takashi Iwai90caaef2012-11-22 16:55:11 +0100750 for (i = 0; i < HDSP_FIRMWARE_SIZE / 4; ++i) {
751 hdsp_write(hdsp, HDSP_fifoData, cache[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 if (hdsp_fifo_wait (hdsp, 127, HDSP_LONG_WAIT)) {
753 snd_printk ("Hammerfall-DSP: timeout during firmware loading\n");
Adrian Knoth0c2bc7c2013-01-15 18:52:20 +0100754 hdsp_write(hdsp, HDSP_control2Reg, HDSP_S200);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return -EIO;
756 }
757 }
758
Adrian Knoth0c2bc7c2013-01-15 18:52:20 +0100759 hdsp_fifo_wait(hdsp, 3, HDSP_LONG_WAIT);
760 hdsp_write(hdsp, HDSP_control2Reg, HDSP_S200);
761
Takashi Iwaib0b981192005-10-20 18:29:58 +0200762 ssleep(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763#ifdef SNDRV_BIG_ENDIAN
764 hdsp->control2_register = HDSP_BIGENDIAN_MODE;
765#else
766 hdsp->control2_register = 0;
767#endif
768 hdsp_write (hdsp, HDSP_control2Reg, hdsp->control2_register);
769 snd_printk ("Hammerfall-DSP: finished firmware loading\n");
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
772 if (hdsp->state & HDSP_InitializationComplete) {
Takashi Iwaib0b981192005-10-20 18:29:58 +0200773 snd_printk(KERN_INFO "Hammerfall-DSP: firmware loaded from cache, restoring defaults\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 spin_lock_irqsave(&hdsp->lock, flags);
775 snd_hdsp_set_defaults(hdsp);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100776 spin_unlock_irqrestore(&hdsp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 hdsp->state |= HDSP_FirmwareLoaded;
780
781 return 0;
782}
783
Takashi Iwai55e957d2005-11-17 14:52:13 +0100784static int hdsp_get_iobox_version (struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
786 if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100787
Adrian Knoth0c2bc7c2013-01-15 18:52:20 +0100788 hdsp_write(hdsp, HDSP_control2Reg, HDSP_S_LOAD);
789 hdsp_write(hdsp, HDSP_fifoData, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Adrian Knoth0c2bc7c2013-01-15 18:52:20 +0100791 if (hdsp_fifo_wait(hdsp, 0, HDSP_SHORT_WAIT) < 0) {
792 hdsp_write(hdsp, HDSP_control2Reg, HDSP_S300);
Florian Faber28b26e12010-12-01 12:14:47 +0100793 hdsp_write(hdsp, HDSP_control2Reg, HDSP_S_LOAD);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100794 }
Adrian Knoth0c2bc7c2013-01-15 18:52:20 +0100795
796 hdsp_write(hdsp, HDSP_control2Reg, HDSP_S200 | HDSP_PROGRAM);
797 hdsp_write (hdsp, HDSP_fifoData, 0);
798 if (hdsp_fifo_wait(hdsp, 0, HDSP_SHORT_WAIT) < 0) {
799 hdsp->io_type = Multiface;
800 snd_printk("Hammerfall-DSP: Multiface found\n");
801 return 0;
802 }
803
804 hdsp_write(hdsp, HDSP_control2Reg, HDSP_S_LOAD);
805 hdsp_write(hdsp, HDSP_fifoData, 0);
806 if (hdsp_fifo_wait(hdsp, 0, HDSP_SHORT_WAIT) == 0) {
807 hdsp->io_type = Digiface;
808 snd_printk("Hammerfall-DSP: Digiface found\n");
809 return 0;
810 }
811
812 hdsp_write(hdsp, HDSP_control2Reg, HDSP_S300);
813 hdsp_write(hdsp, HDSP_control2Reg, HDSP_S_LOAD);
814 hdsp_write(hdsp, HDSP_fifoData, 0);
815 if (hdsp_fifo_wait(hdsp, 0, HDSP_SHORT_WAIT) == 0) {
816 hdsp->io_type = Multiface;
817 snd_printk("Hammerfall-DSP: Multiface found\n");
818 return 0;
819 }
820
821 hdsp_write(hdsp, HDSP_control2Reg, HDSP_S300);
822 hdsp_write(hdsp, HDSP_control2Reg, HDSP_S_LOAD);
823 hdsp_write(hdsp, HDSP_fifoData, 0);
824 if (hdsp_fifo_wait(hdsp, 0, HDSP_SHORT_WAIT) < 0) {
825 hdsp->io_type = Multiface;
826 snd_printk("Hammerfall-DSP: Multiface found\n");
827 return 0;
828 }
829
830 hdsp->io_type = RPM;
831 snd_printk("Hammerfall-DSP: RPM found\n");
832 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 } else {
834 /* firmware was already loaded, get iobox type */
Florian Faber28b26e12010-12-01 12:14:47 +0100835 if (hdsp_read(hdsp, HDSP_status2Register) & HDSP_version2)
836 hdsp->io_type = RPM;
837 else if (hdsp_read(hdsp, HDSP_status2Register) & HDSP_version1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 hdsp->io_type = Multiface;
Takashi Iwaib0b981192005-10-20 18:29:58 +0200839 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 hdsp->io_type = Digiface;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 }
842 return 0;
843}
844
845
Takashi Iwai92eed662008-02-22 18:35:56 +0100846static int hdsp_request_fw_loader(struct hdsp *hdsp);
Takashi Iwai311e70a2006-09-06 12:13:37 +0200847
848static int hdsp_check_for_firmware (struct hdsp *hdsp, int load_on_demand)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
Takashi Iwai311e70a2006-09-06 12:13:37 +0200850 if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
851 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 hdsp->state &= ~HDSP_FirmwareLoaded;
Takashi Iwai311e70a2006-09-06 12:13:37 +0200854 if (! load_on_demand)
Takashi Iwaib0b981192005-10-20 18:29:58 +0200855 return -EIO;
Takashi Iwai311e70a2006-09-06 12:13:37 +0200856 snd_printk(KERN_ERR "Hammerfall-DSP: firmware not present.\n");
Takashi Iwaib0b981192005-10-20 18:29:58 +0200857 /* try to load firmware */
Takashi Iwai311e70a2006-09-06 12:13:37 +0200858 if (! (hdsp->state & HDSP_FirmwareCached)) {
Takashi Iwai311e70a2006-09-06 12:13:37 +0200859 if (! hdsp_request_fw_loader(hdsp))
860 return 0;
Takashi Iwai311e70a2006-09-06 12:13:37 +0200861 snd_printk(KERN_ERR
862 "Hammerfall-DSP: No firmware loaded nor "
863 "cached, please upload firmware.\n");
864 return -EIO;
Takashi Iwaib0b981192005-10-20 18:29:58 +0200865 }
Takashi Iwai311e70a2006-09-06 12:13:37 +0200866 if (snd_hdsp_load_firmware_from_cache(hdsp) != 0) {
867 snd_printk(KERN_ERR
868 "Hammerfall-DSP: Firmware loading from "
869 "cache failed, please upload manually.\n");
870 return -EIO;
871 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 }
873 return 0;
874}
875
876
Takashi Iwai55e957d2005-11-17 14:52:13 +0100877static int hdsp_fifo_wait(struct hdsp *hdsp, int count, int timeout)
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100878{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 int i;
880
881 /* the fifoStatus registers reports on how many words
882 are available in the command FIFO.
883 */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 for (i = 0; i < timeout; i++) {
886
887 if ((int)(hdsp_read (hdsp, HDSP_fifoStatus) & 0xff) <= count)
888 return 0;
889
890 /* not very friendly, but we only do this during a firmware
891 load and changing the mixer, so we just put up with it.
892 */
893
894 udelay (100);
895 }
896
897 snd_printk ("Hammerfall-DSP: wait for FIFO status <= %d failed after %d iterations\n",
898 count, timeout);
899 return -1;
900}
901
Takashi Iwai55e957d2005-11-17 14:52:13 +0100902static int hdsp_read_gain (struct hdsp *hdsp, unsigned int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
Takashi Iwaib0b981192005-10-20 18:29:58 +0200904 if (addr >= HDSP_MATRIX_MIXER_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 return 0;
Takashi Iwaib0b981192005-10-20 18:29:58 +0200906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 return hdsp->mixer_matrix[addr];
908}
909
Takashi Iwai55e957d2005-11-17 14:52:13 +0100910static int hdsp_write_gain(struct hdsp *hdsp, unsigned int addr, unsigned short data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
912 unsigned int ad;
913
914 if (addr >= HDSP_MATRIX_MIXER_SIZE)
915 return -1;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 if (hdsp->io_type == H9652 || hdsp->io_type == H9632) {
918
919 /* from martin bjornsen:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100920
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 "You can only write dwords to the
922 mixer memory which contain two
923 mixer values in the low and high
924 word. So if you want to change
925 value 0 you have to read value 1
926 from the cache and write both to
927 the first dword in the mixer
928 memory."
929 */
930
Takashi Iwaib0b981192005-10-20 18:29:58 +0200931 if (hdsp->io_type == H9632 && addr >= 512)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Takashi Iwaib0b981192005-10-20 18:29:58 +0200934 if (hdsp->io_type == H9652 && addr >= 1352)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
937 hdsp->mixer_matrix[addr] = data;
938
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 /* `addr' addresses a 16-bit wide address, but
941 the address space accessed via hdsp_write
942 uses byte offsets. put another way, addr
943 varies from 0 to 1351, but to access the
944 corresponding memory location, we need
945 to access 0 to 2703 ...
946 */
947 ad = addr/2;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100948
949 hdsp_write (hdsp, 4096 + (ad*4),
950 (hdsp->mixer_matrix[(addr&0x7fe)+1] << 16) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 hdsp->mixer_matrix[addr&0x7fe]);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 return 0;
954
955 } else {
956
957 ad = (addr << 16) + data;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100958
Takashi Iwaib0b981192005-10-20 18:29:58 +0200959 if (hdsp_fifo_wait(hdsp, 127, HDSP_LONG_WAIT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
962 hdsp_write (hdsp, HDSP_fifoData, ad);
963 hdsp->mixer_matrix[addr] = data;
964
965 }
966
967 return 0;
968}
969
Takashi Iwai55e957d2005-11-17 14:52:13 +0100970static int snd_hdsp_use_is_exclusive(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
972 unsigned long flags;
973 int ret = 1;
974
975 spin_lock_irqsave(&hdsp->lock, flags);
976 if ((hdsp->playback_pid != hdsp->capture_pid) &&
Takashi Iwaib0b981192005-10-20 18:29:58 +0200977 (hdsp->playback_pid >= 0) && (hdsp->capture_pid >= 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 spin_unlock_irqrestore(&hdsp->lock, flags);
980 return ret;
981}
982
Takashi Iwai55e957d2005-11-17 14:52:13 +0100983static int hdsp_spdif_sample_rate(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984{
985 unsigned int status = hdsp_read(hdsp, HDSP_statusRegister);
986 unsigned int rate_bits = (status & HDSP_spdifFrequencyMask);
987
Remy Bruno47ba97f2008-02-22 17:57:02 +0100988 /* For the 9632, the mask is different */
989 if (hdsp->io_type == H9632)
990 rate_bits = (status & HDSP_spdifFrequencyMask_9632);
991
Takashi Iwaib0b981192005-10-20 18:29:58 +0200992 if (status & HDSP_SPDIFErrorFlag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 return 0;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 switch (rate_bits) {
996 case HDSP_spdifFrequency32KHz: return 32000;
997 case HDSP_spdifFrequency44_1KHz: return 44100;
998 case HDSP_spdifFrequency48KHz: return 48000;
999 case HDSP_spdifFrequency64KHz: return 64000;
1000 case HDSP_spdifFrequency88_2KHz: return 88200;
1001 case HDSP_spdifFrequency96KHz: return 96000;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001002 case HDSP_spdifFrequency128KHz:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 if (hdsp->io_type == H9632) return 128000;
1004 break;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001005 case HDSP_spdifFrequency176_4KHz:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 if (hdsp->io_type == H9632) return 176400;
1007 break;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001008 case HDSP_spdifFrequency192KHz:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 if (hdsp->io_type == H9632) return 192000;
1010 break;
1011 default:
1012 break;
1013 }
1014 snd_printk ("Hammerfall-DSP: unknown spdif frequency status; bits = 0x%x, status = 0x%x\n", rate_bits, status);
1015 return 0;
1016}
1017
Remy Bruno47ba97f2008-02-22 17:57:02 +01001018static int hdsp_external_sample_rate(struct hdsp *hdsp)
1019{
1020 unsigned int status2 = hdsp_read(hdsp, HDSP_status2Register);
1021 unsigned int rate_bits = status2 & HDSP_systemFrequencyMask;
1022
1023 /* For the 9632 card, there seems to be no bit for indicating external
1024 * sample rate greater than 96kHz. The card reports the corresponding
1025 * single speed. So the best means seems to get spdif rate when
1026 * autosync reference is spdif */
1027 if (hdsp->io_type == H9632 &&
1028 hdsp_autosync_ref(hdsp) == HDSP_AUTOSYNC_FROM_SPDIF)
1029 return hdsp_spdif_sample_rate(hdsp);
1030
1031 switch (rate_bits) {
1032 case HDSP_systemFrequency32: return 32000;
1033 case HDSP_systemFrequency44_1: return 44100;
1034 case HDSP_systemFrequency48: return 48000;
1035 case HDSP_systemFrequency64: return 64000;
1036 case HDSP_systemFrequency88_2: return 88200;
1037 case HDSP_systemFrequency96: return 96000;
1038 default:
1039 return 0;
1040 }
1041}
1042
Takashi Iwai55e957d2005-11-17 14:52:13 +01001043static void hdsp_compute_period_size(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
1045 hdsp->period_bytes = 1 << ((hdsp_decode_latency(hdsp->control_register) + 8));
1046}
1047
Takashi Iwai55e957d2005-11-17 14:52:13 +01001048static snd_pcm_uframes_t hdsp_hw_pointer(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
1050 int position;
1051
1052 position = hdsp_read(hdsp, HDSP_statusRegister);
1053
Takashi Iwaib0b981192005-10-20 18:29:58 +02001054 if (!hdsp->precise_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 return (position & HDSP_BufferID) ? (hdsp->period_bytes / 4) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057 position &= HDSP_BufferPositionMask;
1058 position /= 4;
1059 position &= (hdsp->period_bytes/2) - 1;
1060 return position;
1061}
1062
Takashi Iwai55e957d2005-11-17 14:52:13 +01001063static void hdsp_reset_hw_pointer(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
1065 hdsp_write (hdsp, HDSP_resetPointer, 0);
Remy Brunod7923b22006-10-17 12:41:56 +02001066 if (hdsp->io_type == H9632 && hdsp->firmware_rev >= 152)
1067 /* HDSP_resetPointer = HDSP_freqReg, which is strange and
1068 * requires (?) to write again DDS value after a reset pointer
1069 * (at least, it works like this) */
1070 hdsp_write (hdsp, HDSP_freqReg, hdsp->dds_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071}
1072
Takashi Iwai55e957d2005-11-17 14:52:13 +01001073static void hdsp_start_audio(struct hdsp *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
1075 s->control_register |= (HDSP_AudioInterruptEnable | HDSP_Start);
1076 hdsp_write(s, HDSP_controlRegister, s->control_register);
1077}
1078
Takashi Iwai55e957d2005-11-17 14:52:13 +01001079static void hdsp_stop_audio(struct hdsp *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
1081 s->control_register &= ~(HDSP_Start | HDSP_AudioInterruptEnable);
1082 hdsp_write(s, HDSP_controlRegister, s->control_register);
1083}
1084
Takashi Iwai55e957d2005-11-17 14:52:13 +01001085static void hdsp_silence_playback(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086{
1087 memset(hdsp->playback_buffer, 0, HDSP_DMA_AREA_BYTES);
1088}
1089
Takashi Iwai55e957d2005-11-17 14:52:13 +01001090static int hdsp_set_interrupt_interval(struct hdsp *s, unsigned int frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091{
1092 int n;
1093
1094 spin_lock_irq(&s->lock);
1095
1096 frames >>= 7;
1097 n = 0;
1098 while (frames) {
1099 n++;
1100 frames >>= 1;
1101 }
1102
1103 s->control_register &= ~HDSP_LatencyMask;
1104 s->control_register |= hdsp_encode_latency(n);
1105
1106 hdsp_write(s, HDSP_controlRegister, s->control_register);
1107
1108 hdsp_compute_period_size(s);
1109
1110 spin_unlock_irq(&s->lock);
1111
1112 return 0;
1113}
1114
Remy Brunod7923b22006-10-17 12:41:56 +02001115static void hdsp_set_dds_value(struct hdsp *hdsp, int rate)
1116{
1117 u64 n;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001118
Remy Brunod7923b22006-10-17 12:41:56 +02001119 if (rate >= 112000)
1120 rate /= 4;
1121 else if (rate >= 56000)
1122 rate /= 2;
1123
Julian Cablee4b60882007-03-19 11:44:40 +01001124 n = DDS_NUMERATOR;
Takashi Iwai3f7440a2009-06-05 17:40:04 +02001125 n = div_u64(n, rate);
Remy Brunod7923b22006-10-17 12:41:56 +02001126 /* n should be less than 2^32 for being written to FREQ register */
Takashi Iwaida3cec32008-08-08 17:12:14 +02001127 snd_BUG_ON(n >> 32);
Remy Brunod7923b22006-10-17 12:41:56 +02001128 /* HDSP_freqReg and HDSP_resetPointer are the same, so keep the DDS
1129 value to write it after a reset */
1130 hdsp->dds_value = n;
1131 hdsp_write(hdsp, HDSP_freqReg, hdsp->dds_value);
1132}
1133
Takashi Iwai55e957d2005-11-17 14:52:13 +01001134static int hdsp_set_rate(struct hdsp *hdsp, int rate, int called_internally)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135{
1136 int reject_if_open = 0;
1137 int current_rate;
1138 int rate_bits;
1139
1140 /* ASSUMPTION: hdsp->lock is either held, or
1141 there is no need for it (e.g. during module
1142 initialization).
1143 */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001144
1145 if (!(hdsp->control_register & HDSP_ClockModeMaster)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 if (called_internally) {
1147 /* request from ctl or card initialization */
Takashi Iwaib0b981192005-10-20 18:29:58 +02001148 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 -07001149 return -1;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001150 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 /* hw_param request while in AutoSync mode */
1152 int external_freq = hdsp_external_sample_rate(hdsp);
1153 int spdif_freq = hdsp_spdif_sample_rate(hdsp);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001154
Takashi Iwaib0b981192005-10-20 18:29:58 +02001155 if ((spdif_freq == external_freq*2) && (hdsp_autosync_ref(hdsp) >= HDSP_AUTOSYNC_FROM_ADAT1))
1156 snd_printk(KERN_INFO "Hammerfall-DSP: Detected ADAT in double speed mode\n");
1157 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 +01001158 snd_printk(KERN_INFO "Hammerfall-DSP: Detected ADAT in quad speed mode\n");
Takashi Iwaib0b981192005-10-20 18:29:58 +02001159 else if (rate != external_freq) {
1160 snd_printk(KERN_INFO "Hammerfall-DSP: No AutoSync source for requested rate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 return -1;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001162 }
1163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 }
1165
1166 current_rate = hdsp->system_sample_rate;
1167
1168 /* Changing from a "single speed" to a "double speed" rate is
1169 not allowed if any substreams are open. This is because
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001170 such a change causes a shift in the location of
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 the DMA buffers and a reduction in the number of available
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001172 buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
1174 Note that a similar but essentially insoluble problem
1175 exists for externally-driven rate changes. All we can do
1176 is to flag rate changes in the read/write routines. */
1177
Takashi Iwaib0b981192005-10-20 18:29:58 +02001178 if (rate > 96000 && hdsp->io_type != H9632)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 return -EINVAL;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 switch (rate) {
1182 case 32000:
Takashi Iwaib0b981192005-10-20 18:29:58 +02001183 if (current_rate > 48000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 rate_bits = HDSP_Frequency32KHz;
1186 break;
1187 case 44100:
Takashi Iwaib0b981192005-10-20 18:29:58 +02001188 if (current_rate > 48000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 rate_bits = HDSP_Frequency44_1KHz;
1191 break;
1192 case 48000:
Takashi Iwaib0b981192005-10-20 18:29:58 +02001193 if (current_rate > 48000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 rate_bits = HDSP_Frequency48KHz;
1196 break;
1197 case 64000:
Takashi Iwaib0b981192005-10-20 18:29:58 +02001198 if (current_rate <= 48000 || current_rate > 96000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 rate_bits = HDSP_Frequency64KHz;
1201 break;
1202 case 88200:
Takashi Iwaib0b981192005-10-20 18:29:58 +02001203 if (current_rate <= 48000 || current_rate > 96000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 rate_bits = HDSP_Frequency88_2KHz;
1206 break;
1207 case 96000:
Takashi Iwaib0b981192005-10-20 18:29:58 +02001208 if (current_rate <= 48000 || current_rate > 96000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 rate_bits = HDSP_Frequency96KHz;
1211 break;
1212 case 128000:
Takashi Iwaib0b981192005-10-20 18:29:58 +02001213 if (current_rate < 128000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 rate_bits = HDSP_Frequency128KHz;
1216 break;
1217 case 176400:
Takashi Iwaib0b981192005-10-20 18:29:58 +02001218 if (current_rate < 128000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 rate_bits = HDSP_Frequency176_4KHz;
1221 break;
1222 case 192000:
Takashi Iwaib0b981192005-10-20 18:29:58 +02001223 if (current_rate < 128000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 rate_bits = HDSP_Frequency192KHz;
1226 break;
1227 default:
1228 return -EINVAL;
1229 }
1230
1231 if (reject_if_open && (hdsp->capture_pid >= 0 || hdsp->playback_pid >= 0)) {
1232 snd_printk ("Hammerfall-DSP: cannot change speed mode (capture PID = %d, playback PID = %d)\n",
1233 hdsp->capture_pid,
1234 hdsp->playback_pid);
1235 return -EBUSY;
1236 }
1237
1238 hdsp->control_register &= ~HDSP_FrequencyMask;
1239 hdsp->control_register |= rate_bits;
1240 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1241
Remy Brunod7923b22006-10-17 12:41:56 +02001242 /* For HDSP9632 rev 152, need to set DDS value in FREQ register */
1243 if (hdsp->io_type == H9632 && hdsp->firmware_rev >= 152)
1244 hdsp_set_dds_value(hdsp, rate);
1245
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 if (rate >= 128000) {
1247 hdsp->channel_map = channel_map_H9632_qs;
1248 } else if (rate > 48000) {
Takashi Iwaib0b981192005-10-20 18:29:58 +02001249 if (hdsp->io_type == H9632)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 hdsp->channel_map = channel_map_H9632_ds;
Takashi Iwaib0b981192005-10-20 18:29:58 +02001251 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 hdsp->channel_map = channel_map_ds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 } else {
1254 switch (hdsp->io_type) {
Florian Faber28b26e12010-12-01 12:14:47 +01001255 case RPM:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 case Multiface:
1257 hdsp->channel_map = channel_map_mf_ss;
1258 break;
1259 case Digiface:
1260 case H9652:
1261 hdsp->channel_map = channel_map_df_ss;
1262 break;
1263 case H9632:
1264 hdsp->channel_map = channel_map_H9632_ss;
1265 break;
1266 default:
1267 /* should never happen */
1268 break;
1269 }
1270 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 hdsp->system_sample_rate = rate;
1273
1274 return 0;
1275}
1276
1277/*----------------------------------------------------------------------------
1278 MIDI
1279 ----------------------------------------------------------------------------*/
1280
Takashi Iwai55e957d2005-11-17 14:52:13 +01001281static unsigned char snd_hdsp_midi_read_byte (struct hdsp *hdsp, int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
1283 /* the hardware already does the relevant bit-mask with 0xff */
Takashi Iwaib0b981192005-10-20 18:29:58 +02001284 if (id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 return hdsp_read(hdsp, HDSP_midiDataIn1);
Takashi Iwaib0b981192005-10-20 18:29:58 +02001286 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 return hdsp_read(hdsp, HDSP_midiDataIn0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288}
1289
Takashi Iwai55e957d2005-11-17 14:52:13 +01001290static void snd_hdsp_midi_write_byte (struct hdsp *hdsp, int id, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291{
1292 /* the hardware already does the relevant bit-mask with 0xff */
Takashi Iwaib0b981192005-10-20 18:29:58 +02001293 if (id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 hdsp_write(hdsp, HDSP_midiDataOut1, val);
Takashi Iwaib0b981192005-10-20 18:29:58 +02001295 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 hdsp_write(hdsp, HDSP_midiDataOut0, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297}
1298
Takashi Iwai55e957d2005-11-17 14:52:13 +01001299static int snd_hdsp_midi_input_available (struct hdsp *hdsp, int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
Takashi Iwaib0b981192005-10-20 18:29:58 +02001301 if (id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 return (hdsp_read(hdsp, HDSP_midiStatusIn1) & 0xff);
Takashi Iwaib0b981192005-10-20 18:29:58 +02001303 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 return (hdsp_read(hdsp, HDSP_midiStatusIn0) & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305}
1306
Takashi Iwai55e957d2005-11-17 14:52:13 +01001307static int snd_hdsp_midi_output_possible (struct hdsp *hdsp, int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308{
1309 int fifo_bytes_used;
1310
Takashi Iwaib0b981192005-10-20 18:29:58 +02001311 if (id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 fifo_bytes_used = hdsp_read(hdsp, HDSP_midiStatusOut1) & 0xff;
Takashi Iwaib0b981192005-10-20 18:29:58 +02001313 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 fifo_bytes_used = hdsp_read(hdsp, HDSP_midiStatusOut0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Takashi Iwaib0b981192005-10-20 18:29:58 +02001316 if (fifo_bytes_used < 128)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 return 128 - fifo_bytes_used;
Takashi Iwaib0b981192005-10-20 18:29:58 +02001318 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320}
1321
Takashi Iwai55e957d2005-11-17 14:52:13 +01001322static void snd_hdsp_flush_midi_input (struct hdsp *hdsp, int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323{
Takashi Iwaib0b981192005-10-20 18:29:58 +02001324 while (snd_hdsp_midi_input_available (hdsp, id))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 snd_hdsp_midi_read_byte (hdsp, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326}
1327
Takashi Iwai55e957d2005-11-17 14:52:13 +01001328static int snd_hdsp_midi_output_write (struct hdsp_midi *hmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329{
1330 unsigned long flags;
1331 int n_pending;
1332 int to_write;
1333 int i;
1334 unsigned char buf[128];
1335
1336 /* Output is not interrupt driven */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001337
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 spin_lock_irqsave (&hmidi->lock, flags);
1339 if (hmidi->output) {
1340 if (!snd_rawmidi_transmit_empty (hmidi->output)) {
1341 if ((n_pending = snd_hdsp_midi_output_possible (hmidi->hdsp, hmidi->id)) > 0) {
1342 if (n_pending > (int)sizeof (buf))
1343 n_pending = sizeof (buf);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001344
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 if ((to_write = snd_rawmidi_transmit (hmidi->output, buf, n_pending)) > 0) {
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001346 for (i = 0; i < to_write; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 snd_hdsp_midi_write_byte (hmidi->hdsp, hmidi->id, buf[i]);
1348 }
1349 }
1350 }
1351 }
1352 spin_unlock_irqrestore (&hmidi->lock, flags);
1353 return 0;
1354}
1355
Takashi Iwai55e957d2005-11-17 14:52:13 +01001356static int snd_hdsp_midi_input_read (struct hdsp_midi *hmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357{
1358 unsigned char buf[128]; /* this buffer is designed to match the MIDI input FIFO size */
1359 unsigned long flags;
1360 int n_pending;
1361 int i;
1362
1363 spin_lock_irqsave (&hmidi->lock, flags);
1364 if ((n_pending = snd_hdsp_midi_input_available (hmidi->hdsp, hmidi->id)) > 0) {
1365 if (hmidi->input) {
Takashi Iwaib0b981192005-10-20 18:29:58 +02001366 if (n_pending > (int)sizeof (buf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 n_pending = sizeof (buf);
Takashi Iwaib0b981192005-10-20 18:29:58 +02001368 for (i = 0; i < n_pending; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 buf[i] = snd_hdsp_midi_read_byte (hmidi->hdsp, hmidi->id);
Takashi Iwaib0b981192005-10-20 18:29:58 +02001370 if (n_pending)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 snd_rawmidi_receive (hmidi->input, buf, n_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 } else {
1373 /* flush the MIDI input FIFO */
Takashi Iwaib0b981192005-10-20 18:29:58 +02001374 while (--n_pending)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 snd_hdsp_midi_read_byte (hmidi->hdsp, hmidi->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 }
1377 }
1378 hmidi->pending = 0;
Takashi Iwaib0b981192005-10-20 18:29:58 +02001379 if (hmidi->id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 hmidi->hdsp->control_register |= HDSP_Midi1InterruptEnable;
Takashi Iwaib0b981192005-10-20 18:29:58 +02001381 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 hmidi->hdsp->control_register |= HDSP_Midi0InterruptEnable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 hdsp_write(hmidi->hdsp, HDSP_controlRegister, hmidi->hdsp->control_register);
1384 spin_unlock_irqrestore (&hmidi->lock, flags);
1385 return snd_hdsp_midi_output_write (hmidi);
1386}
1387
Takashi Iwai55e957d2005-11-17 14:52:13 +01001388static void snd_hdsp_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001390 struct hdsp *hdsp;
1391 struct hdsp_midi *hmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 unsigned long flags;
1393 u32 ie;
1394
Takashi Iwai55e957d2005-11-17 14:52:13 +01001395 hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 hdsp = hmidi->hdsp;
1397 ie = hmidi->id ? HDSP_Midi1InterruptEnable : HDSP_Midi0InterruptEnable;
1398 spin_lock_irqsave (&hdsp->lock, flags);
1399 if (up) {
1400 if (!(hdsp->control_register & ie)) {
1401 snd_hdsp_flush_midi_input (hdsp, hmidi->id);
1402 hdsp->control_register |= ie;
1403 }
1404 } else {
1405 hdsp->control_register &= ~ie;
1406 tasklet_kill(&hdsp->midi_tasklet);
1407 }
1408
1409 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1410 spin_unlock_irqrestore (&hdsp->lock, flags);
1411}
1412
1413static void snd_hdsp_midi_output_timer(unsigned long data)
1414{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001415 struct hdsp_midi *hmidi = (struct hdsp_midi *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 unsigned long flags;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001417
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 snd_hdsp_midi_output_write(hmidi);
1419 spin_lock_irqsave (&hmidi->lock, flags);
1420
1421 /* this does not bump hmidi->istimer, because the
1422 kernel automatically removed the timer when it
1423 expired, and we are now adding it back, thus
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001424 leaving istimer wherever it was set before.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 */
1426
1427 if (hmidi->istimer) {
1428 hmidi->timer.expires = 1 + jiffies;
1429 add_timer(&hmidi->timer);
1430 }
1431
1432 spin_unlock_irqrestore (&hmidi->lock, flags);
1433}
1434
Takashi Iwai55e957d2005-11-17 14:52:13 +01001435static void snd_hdsp_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001437 struct hdsp_midi *hmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 unsigned long flags;
1439
Takashi Iwai55e957d2005-11-17 14:52:13 +01001440 hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 spin_lock_irqsave (&hmidi->lock, flags);
1442 if (up) {
1443 if (!hmidi->istimer) {
1444 init_timer(&hmidi->timer);
1445 hmidi->timer.function = snd_hdsp_midi_output_timer;
1446 hmidi->timer.data = (unsigned long) hmidi;
1447 hmidi->timer.expires = 1 + jiffies;
1448 add_timer(&hmidi->timer);
1449 hmidi->istimer++;
1450 }
1451 } else {
Takashi Iwaib0b981192005-10-20 18:29:58 +02001452 if (hmidi->istimer && --hmidi->istimer <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 del_timer (&hmidi->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 }
1455 spin_unlock_irqrestore (&hmidi->lock, flags);
1456 if (up)
1457 snd_hdsp_midi_output_write(hmidi);
1458}
1459
Takashi Iwai55e957d2005-11-17 14:52:13 +01001460static int snd_hdsp_midi_input_open(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001462 struct hdsp_midi *hmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
Takashi Iwai55e957d2005-11-17 14:52:13 +01001464 hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 spin_lock_irq (&hmidi->lock);
1466 snd_hdsp_flush_midi_input (hmidi->hdsp, hmidi->id);
1467 hmidi->input = substream;
1468 spin_unlock_irq (&hmidi->lock);
1469
1470 return 0;
1471}
1472
Takashi Iwai55e957d2005-11-17 14:52:13 +01001473static int snd_hdsp_midi_output_open(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001475 struct hdsp_midi *hmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
Takashi Iwai55e957d2005-11-17 14:52:13 +01001477 hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 spin_lock_irq (&hmidi->lock);
1479 hmidi->output = substream;
1480 spin_unlock_irq (&hmidi->lock);
1481
1482 return 0;
1483}
1484
Takashi Iwai55e957d2005-11-17 14:52:13 +01001485static int snd_hdsp_midi_input_close(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001487 struct hdsp_midi *hmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
1489 snd_hdsp_midi_input_trigger (substream, 0);
1490
Takashi Iwai55e957d2005-11-17 14:52:13 +01001491 hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 spin_lock_irq (&hmidi->lock);
1493 hmidi->input = NULL;
1494 spin_unlock_irq (&hmidi->lock);
1495
1496 return 0;
1497}
1498
Takashi Iwai55e957d2005-11-17 14:52:13 +01001499static int snd_hdsp_midi_output_close(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001501 struct hdsp_midi *hmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
1503 snd_hdsp_midi_output_trigger (substream, 0);
1504
Takashi Iwai55e957d2005-11-17 14:52:13 +01001505 hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 spin_lock_irq (&hmidi->lock);
1507 hmidi->output = NULL;
1508 spin_unlock_irq (&hmidi->lock);
1509
1510 return 0;
1511}
1512
Takashi Iwai55e957d2005-11-17 14:52:13 +01001513static struct snd_rawmidi_ops snd_hdsp_midi_output =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514{
1515 .open = snd_hdsp_midi_output_open,
1516 .close = snd_hdsp_midi_output_close,
1517 .trigger = snd_hdsp_midi_output_trigger,
1518};
1519
Takashi Iwai55e957d2005-11-17 14:52:13 +01001520static struct snd_rawmidi_ops snd_hdsp_midi_input =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521{
1522 .open = snd_hdsp_midi_input_open,
1523 .close = snd_hdsp_midi_input_close,
1524 .trigger = snd_hdsp_midi_input_trigger,
1525};
1526
Takashi Iwaif40b6892006-07-05 16:51:05 +02001527static int snd_hdsp_create_midi (struct snd_card *card, struct hdsp *hdsp, int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528{
1529 char buf[32];
1530
1531 hdsp->midi[id].id = id;
1532 hdsp->midi[id].rmidi = NULL;
1533 hdsp->midi[id].input = NULL;
1534 hdsp->midi[id].output = NULL;
1535 hdsp->midi[id].hdsp = hdsp;
1536 hdsp->midi[id].istimer = 0;
1537 hdsp->midi[id].pending = 0;
1538 spin_lock_init (&hdsp->midi[id].lock);
1539
1540 sprintf (buf, "%s MIDI %d", card->shortname, id+1);
Takashi Iwaib0b981192005-10-20 18:29:58 +02001541 if (snd_rawmidi_new (card, buf, id, 1, 1, &hdsp->midi[id].rmidi) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Jaroslav Kysela972d4c52008-11-12 16:37:48 +01001544 sprintf(hdsp->midi[id].rmidi->name, "HDSP MIDI %d", id+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 hdsp->midi[id].rmidi->private_data = &hdsp->midi[id];
1546
1547 snd_rawmidi_set_ops (hdsp->midi[id].rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_hdsp_midi_output);
1548 snd_rawmidi_set_ops (hdsp->midi[id].rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_hdsp_midi_input);
1549
1550 hdsp->midi[id].rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT |
1551 SNDRV_RAWMIDI_INFO_INPUT |
1552 SNDRV_RAWMIDI_INFO_DUPLEX;
1553
1554 return 0;
1555}
1556
1557/*-----------------------------------------------------------------------------
1558 Control Interface
1559 ----------------------------------------------------------------------------*/
1560
Takashi Iwai55e957d2005-11-17 14:52:13 +01001561static u32 snd_hdsp_convert_from_aes(struct snd_aes_iec958 *aes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562{
1563 u32 val = 0;
1564 val |= (aes->status[0] & IEC958_AES0_PROFESSIONAL) ? HDSP_SPDIFProfessional : 0;
1565 val |= (aes->status[0] & IEC958_AES0_NONAUDIO) ? HDSP_SPDIFNonAudio : 0;
1566 if (val & HDSP_SPDIFProfessional)
1567 val |= (aes->status[0] & IEC958_AES0_PRO_EMPHASIS_5015) ? HDSP_SPDIFEmphasis : 0;
1568 else
1569 val |= (aes->status[0] & IEC958_AES0_CON_EMPHASIS_5015) ? HDSP_SPDIFEmphasis : 0;
1570 return val;
1571}
1572
Takashi Iwai55e957d2005-11-17 14:52:13 +01001573static void snd_hdsp_convert_to_aes(struct snd_aes_iec958 *aes, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574{
1575 aes->status[0] = ((val & HDSP_SPDIFProfessional) ? IEC958_AES0_PROFESSIONAL : 0) |
1576 ((val & HDSP_SPDIFNonAudio) ? IEC958_AES0_NONAUDIO : 0);
1577 if (val & HDSP_SPDIFProfessional)
1578 aes->status[0] |= (val & HDSP_SPDIFEmphasis) ? IEC958_AES0_PRO_EMPHASIS_5015 : 0;
1579 else
1580 aes->status[0] |= (val & HDSP_SPDIFEmphasis) ? IEC958_AES0_CON_EMPHASIS_5015 : 0;
1581}
1582
Takashi Iwai55e957d2005-11-17 14:52:13 +01001583static int snd_hdsp_control_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584{
1585 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1586 uinfo->count = 1;
1587 return 0;
1588}
1589
Takashi Iwai55e957d2005-11-17 14:52:13 +01001590static int snd_hdsp_control_spdif_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001592 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001593
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 snd_hdsp_convert_to_aes(&ucontrol->value.iec958, hdsp->creg_spdif);
1595 return 0;
1596}
1597
Takashi Iwai55e957d2005-11-17 14:52:13 +01001598static int snd_hdsp_control_spdif_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001600 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 int change;
1602 u32 val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001603
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 val = snd_hdsp_convert_from_aes(&ucontrol->value.iec958);
1605 spin_lock_irq(&hdsp->lock);
1606 change = val != hdsp->creg_spdif;
1607 hdsp->creg_spdif = val;
1608 spin_unlock_irq(&hdsp->lock);
1609 return change;
1610}
1611
Takashi Iwai55e957d2005-11-17 14:52:13 +01001612static int snd_hdsp_control_spdif_stream_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613{
1614 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1615 uinfo->count = 1;
1616 return 0;
1617}
1618
Takashi Iwai55e957d2005-11-17 14:52:13 +01001619static int snd_hdsp_control_spdif_stream_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001621 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001622
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 snd_hdsp_convert_to_aes(&ucontrol->value.iec958, hdsp->creg_spdif_stream);
1624 return 0;
1625}
1626
Takashi Iwai55e957d2005-11-17 14:52:13 +01001627static int snd_hdsp_control_spdif_stream_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001629 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 int change;
1631 u32 val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001632
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 val = snd_hdsp_convert_from_aes(&ucontrol->value.iec958);
1634 spin_lock_irq(&hdsp->lock);
1635 change = val != hdsp->creg_spdif_stream;
1636 hdsp->creg_spdif_stream = val;
1637 hdsp->control_register &= ~(HDSP_SPDIFProfessional | HDSP_SPDIFNonAudio | HDSP_SPDIFEmphasis);
1638 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register |= val);
1639 spin_unlock_irq(&hdsp->lock);
1640 return change;
1641}
1642
Takashi Iwai55e957d2005-11-17 14:52:13 +01001643static int snd_hdsp_control_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644{
1645 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1646 uinfo->count = 1;
1647 return 0;
1648}
1649
Takashi Iwai55e957d2005-11-17 14:52:13 +01001650static int snd_hdsp_control_spdif_mask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651{
1652 ucontrol->value.iec958.status[0] = kcontrol->private_value;
1653 return 0;
1654}
1655
1656#define HDSP_SPDIF_IN(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001657{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 .name = xname, \
1659 .index = xindex, \
1660 .info = snd_hdsp_info_spdif_in, \
1661 .get = snd_hdsp_get_spdif_in, \
1662 .put = snd_hdsp_put_spdif_in }
1663
Takashi Iwai55e957d2005-11-17 14:52:13 +01001664static unsigned int hdsp_spdif_in(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665{
1666 return hdsp_decode_spdif_in(hdsp->control_register & HDSP_SPDIFInputMask);
1667}
1668
Takashi Iwai55e957d2005-11-17 14:52:13 +01001669static int hdsp_set_spdif_input(struct hdsp *hdsp, int in)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670{
1671 hdsp->control_register &= ~HDSP_SPDIFInputMask;
1672 hdsp->control_register |= hdsp_encode_spdif_in(in);
1673 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1674 return 0;
1675}
1676
Takashi Iwai55e957d2005-11-17 14:52:13 +01001677static int snd_hdsp_info_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678{
1679 static char *texts[4] = {"Optical", "Coaxial", "Internal", "AES"};
Takashi Iwai55e957d2005-11-17 14:52:13 +01001680 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
1682 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1683 uinfo->count = 1;
1684 uinfo->value.enumerated.items = ((hdsp->io_type == H9632) ? 4 : 3);
1685 if (uinfo->value.enumerated.item > ((hdsp->io_type == H9632) ? 3 : 2))
1686 uinfo->value.enumerated.item = ((hdsp->io_type == H9632) ? 3 : 2);
1687 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1688 return 0;
1689}
1690
Takashi Iwai55e957d2005-11-17 14:52:13 +01001691static int snd_hdsp_get_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001693 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001694
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 ucontrol->value.enumerated.item[0] = hdsp_spdif_in(hdsp);
1696 return 0;
1697}
1698
Takashi Iwai55e957d2005-11-17 14:52:13 +01001699static int snd_hdsp_put_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001701 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 int change;
1703 unsigned int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001704
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 if (!snd_hdsp_use_is_exclusive(hdsp))
1706 return -EBUSY;
1707 val = ucontrol->value.enumerated.item[0] % ((hdsp->io_type == H9632) ? 4 : 3);
1708 spin_lock_irq(&hdsp->lock);
1709 change = val != hdsp_spdif_in(hdsp);
1710 if (change)
1711 hdsp_set_spdif_input(hdsp, val);
1712 spin_unlock_irq(&hdsp->lock);
1713 return change;
1714}
1715
1716#define HDSP_SPDIF_OUT(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001717{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 .info = snd_hdsp_info_spdif_bits, \
1719 .get = snd_hdsp_get_spdif_out, .put = snd_hdsp_put_spdif_out }
1720
Takashi Iwai55e957d2005-11-17 14:52:13 +01001721static int hdsp_spdif_out(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722{
1723 return (hdsp->control_register & HDSP_SPDIFOpticalOut) ? 1 : 0;
1724}
1725
Takashi Iwai55e957d2005-11-17 14:52:13 +01001726static int hdsp_set_spdif_output(struct hdsp *hdsp, int out)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727{
Takashi Iwaib0b981192005-10-20 18:29:58 +02001728 if (out)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 hdsp->control_register |= HDSP_SPDIFOpticalOut;
Takashi Iwaib0b981192005-10-20 18:29:58 +02001730 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 hdsp->control_register &= ~HDSP_SPDIFOpticalOut;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1733 return 0;
1734}
1735
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001736#define snd_hdsp_info_spdif_bits snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
Takashi Iwai55e957d2005-11-17 14:52:13 +01001738static int snd_hdsp_get_spdif_out(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001740 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001741
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 ucontrol->value.integer.value[0] = hdsp_spdif_out(hdsp);
1743 return 0;
1744}
1745
Takashi Iwai55e957d2005-11-17 14:52:13 +01001746static int snd_hdsp_put_spdif_out(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001748 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 int change;
1750 unsigned int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001751
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 if (!snd_hdsp_use_is_exclusive(hdsp))
1753 return -EBUSY;
1754 val = ucontrol->value.integer.value[0] & 1;
1755 spin_lock_irq(&hdsp->lock);
1756 change = (int)val != hdsp_spdif_out(hdsp);
1757 hdsp_set_spdif_output(hdsp, val);
1758 spin_unlock_irq(&hdsp->lock);
1759 return change;
1760}
1761
1762#define HDSP_SPDIF_PROFESSIONAL(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001763{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 .info = snd_hdsp_info_spdif_bits, \
1765 .get = snd_hdsp_get_spdif_professional, .put = snd_hdsp_put_spdif_professional }
1766
Takashi Iwai55e957d2005-11-17 14:52:13 +01001767static int hdsp_spdif_professional(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768{
1769 return (hdsp->control_register & HDSP_SPDIFProfessional) ? 1 : 0;
1770}
1771
Takashi Iwai55e957d2005-11-17 14:52:13 +01001772static int hdsp_set_spdif_professional(struct hdsp *hdsp, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773{
Takashi Iwaib0b981192005-10-20 18:29:58 +02001774 if (val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 hdsp->control_register |= HDSP_SPDIFProfessional;
Takashi Iwaib0b981192005-10-20 18:29:58 +02001776 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 hdsp->control_register &= ~HDSP_SPDIFProfessional;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1779 return 0;
1780}
1781
Takashi Iwai55e957d2005-11-17 14:52:13 +01001782static int snd_hdsp_get_spdif_professional(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001784 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001785
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 ucontrol->value.integer.value[0] = hdsp_spdif_professional(hdsp);
1787 return 0;
1788}
1789
Takashi Iwai55e957d2005-11-17 14:52:13 +01001790static int snd_hdsp_put_spdif_professional(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001792 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 int change;
1794 unsigned int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001795
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 if (!snd_hdsp_use_is_exclusive(hdsp))
1797 return -EBUSY;
1798 val = ucontrol->value.integer.value[0] & 1;
1799 spin_lock_irq(&hdsp->lock);
1800 change = (int)val != hdsp_spdif_professional(hdsp);
1801 hdsp_set_spdif_professional(hdsp, val);
1802 spin_unlock_irq(&hdsp->lock);
1803 return change;
1804}
1805
1806#define HDSP_SPDIF_EMPHASIS(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001807{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 .info = snd_hdsp_info_spdif_bits, \
1809 .get = snd_hdsp_get_spdif_emphasis, .put = snd_hdsp_put_spdif_emphasis }
1810
Takashi Iwai55e957d2005-11-17 14:52:13 +01001811static int hdsp_spdif_emphasis(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812{
1813 return (hdsp->control_register & HDSP_SPDIFEmphasis) ? 1 : 0;
1814}
1815
Takashi Iwai55e957d2005-11-17 14:52:13 +01001816static int hdsp_set_spdif_emphasis(struct hdsp *hdsp, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817{
Takashi Iwaib0b981192005-10-20 18:29:58 +02001818 if (val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 hdsp->control_register |= HDSP_SPDIFEmphasis;
Takashi Iwaib0b981192005-10-20 18:29:58 +02001820 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 hdsp->control_register &= ~HDSP_SPDIFEmphasis;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1823 return 0;
1824}
1825
Takashi Iwai55e957d2005-11-17 14:52:13 +01001826static int snd_hdsp_get_spdif_emphasis(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001828 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001829
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 ucontrol->value.integer.value[0] = hdsp_spdif_emphasis(hdsp);
1831 return 0;
1832}
1833
Takashi Iwai55e957d2005-11-17 14:52:13 +01001834static int snd_hdsp_put_spdif_emphasis(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001836 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 int change;
1838 unsigned int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001839
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 if (!snd_hdsp_use_is_exclusive(hdsp))
1841 return -EBUSY;
1842 val = ucontrol->value.integer.value[0] & 1;
1843 spin_lock_irq(&hdsp->lock);
1844 change = (int)val != hdsp_spdif_emphasis(hdsp);
1845 hdsp_set_spdif_emphasis(hdsp, val);
1846 spin_unlock_irq(&hdsp->lock);
1847 return change;
1848}
1849
1850#define HDSP_SPDIF_NON_AUDIO(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001851{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 .info = snd_hdsp_info_spdif_bits, \
1853 .get = snd_hdsp_get_spdif_nonaudio, .put = snd_hdsp_put_spdif_nonaudio }
1854
Takashi Iwai55e957d2005-11-17 14:52:13 +01001855static int hdsp_spdif_nonaudio(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856{
1857 return (hdsp->control_register & HDSP_SPDIFNonAudio) ? 1 : 0;
1858}
1859
Takashi Iwai55e957d2005-11-17 14:52:13 +01001860static int hdsp_set_spdif_nonaudio(struct hdsp *hdsp, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861{
Takashi Iwaib0b981192005-10-20 18:29:58 +02001862 if (val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 hdsp->control_register |= HDSP_SPDIFNonAudio;
Takashi Iwaib0b981192005-10-20 18:29:58 +02001864 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 hdsp->control_register &= ~HDSP_SPDIFNonAudio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1867 return 0;
1868}
1869
Takashi Iwai55e957d2005-11-17 14:52:13 +01001870static int snd_hdsp_get_spdif_nonaudio(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001872 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001873
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 ucontrol->value.integer.value[0] = hdsp_spdif_nonaudio(hdsp);
1875 return 0;
1876}
1877
Takashi Iwai55e957d2005-11-17 14:52:13 +01001878static int snd_hdsp_put_spdif_nonaudio(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001880 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 int change;
1882 unsigned int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001883
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 if (!snd_hdsp_use_is_exclusive(hdsp))
1885 return -EBUSY;
1886 val = ucontrol->value.integer.value[0] & 1;
1887 spin_lock_irq(&hdsp->lock);
1888 change = (int)val != hdsp_spdif_nonaudio(hdsp);
1889 hdsp_set_spdif_nonaudio(hdsp, val);
1890 spin_unlock_irq(&hdsp->lock);
1891 return change;
1892}
1893
1894#define HDSP_SPDIF_SAMPLE_RATE(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001895{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 .name = xname, \
1897 .index = xindex, \
1898 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
1899 .info = snd_hdsp_info_spdif_sample_rate, \
1900 .get = snd_hdsp_get_spdif_sample_rate \
1901}
1902
Takashi Iwai55e957d2005-11-17 14:52:13 +01001903static int snd_hdsp_info_spdif_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904{
1905 static char *texts[] = {"32000", "44100", "48000", "64000", "88200", "96000", "None", "128000", "176400", "192000"};
Takashi Iwai55e957d2005-11-17 14:52:13 +01001906 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
1908 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1909 uinfo->count = 1;
1910 uinfo->value.enumerated.items = (hdsp->io_type == H9632) ? 10 : 7;
1911 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1912 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
1913 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1914 return 0;
1915}
1916
Takashi Iwai55e957d2005-11-17 14:52:13 +01001917static int snd_hdsp_get_spdif_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001919 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001920
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 switch (hdsp_spdif_sample_rate(hdsp)) {
1922 case 32000:
1923 ucontrol->value.enumerated.item[0] = 0;
1924 break;
1925 case 44100:
1926 ucontrol->value.enumerated.item[0] = 1;
1927 break;
1928 case 48000:
1929 ucontrol->value.enumerated.item[0] = 2;
1930 break;
1931 case 64000:
1932 ucontrol->value.enumerated.item[0] = 3;
1933 break;
1934 case 88200:
1935 ucontrol->value.enumerated.item[0] = 4;
1936 break;
1937 case 96000:
1938 ucontrol->value.enumerated.item[0] = 5;
1939 break;
1940 case 128000:
1941 ucontrol->value.enumerated.item[0] = 7;
1942 break;
1943 case 176400:
1944 ucontrol->value.enumerated.item[0] = 8;
1945 break;
1946 case 192000:
1947 ucontrol->value.enumerated.item[0] = 9;
1948 break;
1949 default:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001950 ucontrol->value.enumerated.item[0] = 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 }
1952 return 0;
1953}
1954
1955#define HDSP_SYSTEM_SAMPLE_RATE(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001956{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 .name = xname, \
1958 .index = xindex, \
1959 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
1960 .info = snd_hdsp_info_system_sample_rate, \
1961 .get = snd_hdsp_get_system_sample_rate \
1962}
1963
Takashi Iwai55e957d2005-11-17 14:52:13 +01001964static int snd_hdsp_info_system_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965{
1966 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1967 uinfo->count = 1;
1968 return 0;
1969}
1970
Takashi Iwai55e957d2005-11-17 14:52:13 +01001971static int snd_hdsp_get_system_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001973 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001974
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 ucontrol->value.enumerated.item[0] = hdsp->system_sample_rate;
1976 return 0;
1977}
1978
1979#define HDSP_AUTOSYNC_SAMPLE_RATE(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001980{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 .name = xname, \
1982 .index = xindex, \
1983 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
1984 .info = snd_hdsp_info_autosync_sample_rate, \
1985 .get = snd_hdsp_get_autosync_sample_rate \
1986}
1987
Takashi Iwai55e957d2005-11-17 14:52:13 +01001988static int snd_hdsp_info_autosync_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001990 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001991 static char *texts[] = {"32000", "44100", "48000", "64000", "88200", "96000", "None", "128000", "176400", "192000"};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1993 uinfo->count = 1;
1994 uinfo->value.enumerated.items = (hdsp->io_type == H9632) ? 10 : 7 ;
1995 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1996 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
1997 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1998 return 0;
1999}
2000
Takashi Iwai55e957d2005-11-17 14:52:13 +01002001static int snd_hdsp_get_autosync_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002003 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002004
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 switch (hdsp_external_sample_rate(hdsp)) {
2006 case 32000:
2007 ucontrol->value.enumerated.item[0] = 0;
2008 break;
2009 case 44100:
2010 ucontrol->value.enumerated.item[0] = 1;
2011 break;
2012 case 48000:
2013 ucontrol->value.enumerated.item[0] = 2;
2014 break;
2015 case 64000:
2016 ucontrol->value.enumerated.item[0] = 3;
2017 break;
2018 case 88200:
2019 ucontrol->value.enumerated.item[0] = 4;
2020 break;
2021 case 96000:
2022 ucontrol->value.enumerated.item[0] = 5;
2023 break;
2024 case 128000:
2025 ucontrol->value.enumerated.item[0] = 7;
2026 break;
2027 case 176400:
2028 ucontrol->value.enumerated.item[0] = 8;
2029 break;
2030 case 192000:
2031 ucontrol->value.enumerated.item[0] = 9;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002032 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 default:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002034 ucontrol->value.enumerated.item[0] = 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 }
2036 return 0;
2037}
2038
2039#define HDSP_SYSTEM_CLOCK_MODE(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002040{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 .name = xname, \
2042 .index = xindex, \
2043 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
2044 .info = snd_hdsp_info_system_clock_mode, \
2045 .get = snd_hdsp_get_system_clock_mode \
2046}
2047
Takashi Iwai55e957d2005-11-17 14:52:13 +01002048static int hdsp_system_clock_mode(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049{
Takashi Iwaib0b981192005-10-20 18:29:58 +02002050 if (hdsp->control_register & HDSP_ClockModeMaster)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 return 0;
Takashi Iwaib0b981192005-10-20 18:29:58 +02002052 else if (hdsp_external_sample_rate(hdsp) != hdsp->system_sample_rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 return 1;
2055}
2056
Takashi Iwai55e957d2005-11-17 14:52:13 +01002057static int snd_hdsp_info_system_clock_mode(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058{
2059 static char *texts[] = {"Master", "Slave" };
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002060
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2062 uinfo->count = 1;
2063 uinfo->value.enumerated.items = 2;
2064 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2065 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2066 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2067 return 0;
2068}
2069
Takashi Iwai55e957d2005-11-17 14:52:13 +01002070static int snd_hdsp_get_system_clock_mode(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002072 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002073
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 ucontrol->value.enumerated.item[0] = hdsp_system_clock_mode(hdsp);
2075 return 0;
2076}
2077
2078#define HDSP_CLOCK_SOURCE(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002079{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 .name = xname, \
2081 .index = xindex, \
2082 .info = snd_hdsp_info_clock_source, \
2083 .get = snd_hdsp_get_clock_source, \
2084 .put = snd_hdsp_put_clock_source \
2085}
2086
Takashi Iwai55e957d2005-11-17 14:52:13 +01002087static int hdsp_clock_source(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088{
2089 if (hdsp->control_register & HDSP_ClockModeMaster) {
2090 switch (hdsp->system_sample_rate) {
2091 case 32000:
2092 return 1;
2093 case 44100:
2094 return 2;
2095 case 48000:
2096 return 3;
2097 case 64000:
2098 return 4;
2099 case 88200:
2100 return 5;
2101 case 96000:
2102 return 6;
2103 case 128000:
2104 return 7;
2105 case 176400:
2106 return 8;
2107 case 192000:
2108 return 9;
2109 default:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002110 return 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 }
2112 } else {
2113 return 0;
2114 }
2115}
2116
Takashi Iwai55e957d2005-11-17 14:52:13 +01002117static int hdsp_set_clock_source(struct hdsp *hdsp, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118{
2119 int rate;
2120 switch (mode) {
2121 case HDSP_CLOCK_SOURCE_AUTOSYNC:
2122 if (hdsp_external_sample_rate(hdsp) != 0) {
2123 if (!hdsp_set_rate(hdsp, hdsp_external_sample_rate(hdsp), 1)) {
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002124 hdsp->control_register &= ~HDSP_ClockModeMaster;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2126 return 0;
2127 }
2128 }
2129 return -1;
2130 case HDSP_CLOCK_SOURCE_INTERNAL_32KHZ:
2131 rate = 32000;
2132 break;
2133 case HDSP_CLOCK_SOURCE_INTERNAL_44_1KHZ:
2134 rate = 44100;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002135 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 case HDSP_CLOCK_SOURCE_INTERNAL_48KHZ:
2137 rate = 48000;
2138 break;
2139 case HDSP_CLOCK_SOURCE_INTERNAL_64KHZ:
2140 rate = 64000;
2141 break;
2142 case HDSP_CLOCK_SOURCE_INTERNAL_88_2KHZ:
2143 rate = 88200;
2144 break;
2145 case HDSP_CLOCK_SOURCE_INTERNAL_96KHZ:
2146 rate = 96000;
2147 break;
2148 case HDSP_CLOCK_SOURCE_INTERNAL_128KHZ:
2149 rate = 128000;
2150 break;
2151 case HDSP_CLOCK_SOURCE_INTERNAL_176_4KHZ:
2152 rate = 176400;
2153 break;
2154 case HDSP_CLOCK_SOURCE_INTERNAL_192KHZ:
2155 rate = 192000;
2156 break;
2157 default:
2158 rate = 48000;
2159 }
2160 hdsp->control_register |= HDSP_ClockModeMaster;
2161 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2162 hdsp_set_rate(hdsp, rate, 1);
2163 return 0;
2164}
2165
Takashi Iwai55e957d2005-11-17 14:52:13 +01002166static int snd_hdsp_info_clock_source(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167{
2168 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 +01002169 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002170
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2172 uinfo->count = 1;
2173 if (hdsp->io_type == H9632)
2174 uinfo->value.enumerated.items = 10;
2175 else
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002176 uinfo->value.enumerated.items = 7;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2178 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2179 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2180 return 0;
2181}
2182
Takashi Iwai55e957d2005-11-17 14:52:13 +01002183static int snd_hdsp_get_clock_source(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002185 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002186
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 ucontrol->value.enumerated.item[0] = hdsp_clock_source(hdsp);
2188 return 0;
2189}
2190
Takashi Iwai55e957d2005-11-17 14:52:13 +01002191static int snd_hdsp_put_clock_source(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002193 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 int change;
2195 int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002196
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 if (!snd_hdsp_use_is_exclusive(hdsp))
2198 return -EBUSY;
2199 val = ucontrol->value.enumerated.item[0];
2200 if (val < 0) val = 0;
2201 if (hdsp->io_type == H9632) {
Takashi Iwaib0b981192005-10-20 18:29:58 +02002202 if (val > 9)
2203 val = 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 } else {
Takashi Iwaib0b981192005-10-20 18:29:58 +02002205 if (val > 6)
2206 val = 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 }
2208 spin_lock_irq(&hdsp->lock);
Takashi Iwaib0b981192005-10-20 18:29:58 +02002209 if (val != hdsp_clock_source(hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 change = (hdsp_set_clock_source(hdsp, val) == 0) ? 1 : 0;
Takashi Iwaib0b981192005-10-20 18:29:58 +02002211 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 spin_unlock_irq(&hdsp->lock);
2214 return change;
2215}
2216
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002217#define snd_hdsp_info_clock_source_lock snd_ctl_boolean_mono_info
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02002218
Takashi Iwai55e957d2005-11-17 14:52:13 +01002219static int snd_hdsp_get_clock_source_lock(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02002220{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002221 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002222
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02002223 ucontrol->value.integer.value[0] = hdsp->clock_source_locked;
2224 return 0;
2225}
2226
Takashi Iwai55e957d2005-11-17 14:52:13 +01002227static int snd_hdsp_put_clock_source_lock(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02002228{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002229 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02002230 int change;
2231
2232 change = (int)ucontrol->value.integer.value[0] != hdsp->clock_source_locked;
2233 if (change)
Takashi Iwai4e98d6a2007-11-15 15:58:13 +01002234 hdsp->clock_source_locked = !!ucontrol->value.integer.value[0];
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02002235 return change;
2236}
2237
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238#define HDSP_DA_GAIN(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002239{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 .name = xname, \
2241 .index = xindex, \
2242 .info = snd_hdsp_info_da_gain, \
2243 .get = snd_hdsp_get_da_gain, \
2244 .put = snd_hdsp_put_da_gain \
2245}
2246
Takashi Iwai55e957d2005-11-17 14:52:13 +01002247static int hdsp_da_gain(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248{
2249 switch (hdsp->control_register & HDSP_DAGainMask) {
2250 case HDSP_DAGainHighGain:
2251 return 0;
2252 case HDSP_DAGainPlus4dBu:
2253 return 1;
2254 case HDSP_DAGainMinus10dBV:
2255 return 2;
2256 default:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002257 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 }
2259}
2260
Takashi Iwai55e957d2005-11-17 14:52:13 +01002261static int hdsp_set_da_gain(struct hdsp *hdsp, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262{
2263 hdsp->control_register &= ~HDSP_DAGainMask;
2264 switch (mode) {
2265 case 0:
2266 hdsp->control_register |= HDSP_DAGainHighGain;
2267 break;
2268 case 1:
2269 hdsp->control_register |= HDSP_DAGainPlus4dBu;
2270 break;
2271 case 2:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002272 hdsp->control_register |= HDSP_DAGainMinus10dBV;
2273 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 default:
2275 return -1;
2276
2277 }
2278 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2279 return 0;
2280}
2281
Takashi Iwai55e957d2005-11-17 14:52:13 +01002282static int snd_hdsp_info_da_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283{
2284 static char *texts[] = {"Hi Gain", "+4 dBu", "-10 dbV"};
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002285
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2287 uinfo->count = 1;
2288 uinfo->value.enumerated.items = 3;
2289 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2290 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2291 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2292 return 0;
2293}
2294
Takashi Iwai55e957d2005-11-17 14:52:13 +01002295static int snd_hdsp_get_da_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002297 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002298
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 ucontrol->value.enumerated.item[0] = hdsp_da_gain(hdsp);
2300 return 0;
2301}
2302
Takashi Iwai55e957d2005-11-17 14:52:13 +01002303static int snd_hdsp_put_da_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002305 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 int change;
2307 int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002308
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 if (!snd_hdsp_use_is_exclusive(hdsp))
2310 return -EBUSY;
2311 val = ucontrol->value.enumerated.item[0];
2312 if (val < 0) val = 0;
2313 if (val > 2) val = 2;
2314 spin_lock_irq(&hdsp->lock);
Takashi Iwaib0b981192005-10-20 18:29:58 +02002315 if (val != hdsp_da_gain(hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 change = (hdsp_set_da_gain(hdsp, val) == 0) ? 1 : 0;
Takashi Iwaib0b981192005-10-20 18:29:58 +02002317 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 spin_unlock_irq(&hdsp->lock);
2320 return change;
2321}
2322
2323#define HDSP_AD_GAIN(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002324{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 .name = xname, \
2326 .index = xindex, \
2327 .info = snd_hdsp_info_ad_gain, \
2328 .get = snd_hdsp_get_ad_gain, \
2329 .put = snd_hdsp_put_ad_gain \
2330}
2331
Takashi Iwai55e957d2005-11-17 14:52:13 +01002332static int hdsp_ad_gain(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333{
2334 switch (hdsp->control_register & HDSP_ADGainMask) {
2335 case HDSP_ADGainMinus10dBV:
2336 return 0;
2337 case HDSP_ADGainPlus4dBu:
2338 return 1;
2339 case HDSP_ADGainLowGain:
2340 return 2;
2341 default:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002342 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 }
2344}
2345
Takashi Iwai55e957d2005-11-17 14:52:13 +01002346static int hdsp_set_ad_gain(struct hdsp *hdsp, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347{
2348 hdsp->control_register &= ~HDSP_ADGainMask;
2349 switch (mode) {
2350 case 0:
2351 hdsp->control_register |= HDSP_ADGainMinus10dBV;
2352 break;
2353 case 1:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002354 hdsp->control_register |= HDSP_ADGainPlus4dBu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 break;
2356 case 2:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002357 hdsp->control_register |= HDSP_ADGainLowGain;
2358 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 default:
2360 return -1;
2361
2362 }
2363 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2364 return 0;
2365}
2366
Takashi Iwai55e957d2005-11-17 14:52:13 +01002367static int snd_hdsp_info_ad_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368{
2369 static char *texts[] = {"-10 dBV", "+4 dBu", "Lo Gain"};
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002370
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2372 uinfo->count = 1;
2373 uinfo->value.enumerated.items = 3;
2374 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2375 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2376 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2377 return 0;
2378}
2379
Takashi Iwai55e957d2005-11-17 14:52:13 +01002380static int snd_hdsp_get_ad_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002382 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002383
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 ucontrol->value.enumerated.item[0] = hdsp_ad_gain(hdsp);
2385 return 0;
2386}
2387
Takashi Iwai55e957d2005-11-17 14:52:13 +01002388static int snd_hdsp_put_ad_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002390 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 int change;
2392 int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002393
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 if (!snd_hdsp_use_is_exclusive(hdsp))
2395 return -EBUSY;
2396 val = ucontrol->value.enumerated.item[0];
2397 if (val < 0) val = 0;
2398 if (val > 2) val = 2;
2399 spin_lock_irq(&hdsp->lock);
Takashi Iwaib0b981192005-10-20 18:29:58 +02002400 if (val != hdsp_ad_gain(hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 change = (hdsp_set_ad_gain(hdsp, val) == 0) ? 1 : 0;
Takashi Iwaib0b981192005-10-20 18:29:58 +02002402 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 spin_unlock_irq(&hdsp->lock);
2405 return change;
2406}
2407
2408#define HDSP_PHONE_GAIN(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002409{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 .name = xname, \
2411 .index = xindex, \
2412 .info = snd_hdsp_info_phone_gain, \
2413 .get = snd_hdsp_get_phone_gain, \
2414 .put = snd_hdsp_put_phone_gain \
2415}
2416
Takashi Iwai55e957d2005-11-17 14:52:13 +01002417static int hdsp_phone_gain(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418{
2419 switch (hdsp->control_register & HDSP_PhoneGainMask) {
2420 case HDSP_PhoneGain0dB:
2421 return 0;
2422 case HDSP_PhoneGainMinus6dB:
2423 return 1;
2424 case HDSP_PhoneGainMinus12dB:
2425 return 2;
2426 default:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002427 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 }
2429}
2430
Takashi Iwai55e957d2005-11-17 14:52:13 +01002431static int hdsp_set_phone_gain(struct hdsp *hdsp, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432{
2433 hdsp->control_register &= ~HDSP_PhoneGainMask;
2434 switch (mode) {
2435 case 0:
2436 hdsp->control_register |= HDSP_PhoneGain0dB;
2437 break;
2438 case 1:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002439 hdsp->control_register |= HDSP_PhoneGainMinus6dB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 break;
2441 case 2:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002442 hdsp->control_register |= HDSP_PhoneGainMinus12dB;
2443 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 default:
2445 return -1;
2446
2447 }
2448 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2449 return 0;
2450}
2451
Takashi Iwai55e957d2005-11-17 14:52:13 +01002452static int snd_hdsp_info_phone_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453{
2454 static char *texts[] = {"0 dB", "-6 dB", "-12 dB"};
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002455
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2457 uinfo->count = 1;
2458 uinfo->value.enumerated.items = 3;
2459 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2460 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2461 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2462 return 0;
2463}
2464
Takashi Iwai55e957d2005-11-17 14:52:13 +01002465static int snd_hdsp_get_phone_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002467 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002468
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 ucontrol->value.enumerated.item[0] = hdsp_phone_gain(hdsp);
2470 return 0;
2471}
2472
Takashi Iwai55e957d2005-11-17 14:52:13 +01002473static int snd_hdsp_put_phone_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002475 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 int change;
2477 int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002478
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 if (!snd_hdsp_use_is_exclusive(hdsp))
2480 return -EBUSY;
2481 val = ucontrol->value.enumerated.item[0];
2482 if (val < 0) val = 0;
2483 if (val > 2) val = 2;
2484 spin_lock_irq(&hdsp->lock);
Takashi Iwaib0b981192005-10-20 18:29:58 +02002485 if (val != hdsp_phone_gain(hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 change = (hdsp_set_phone_gain(hdsp, val) == 0) ? 1 : 0;
Takashi Iwaib0b981192005-10-20 18:29:58 +02002487 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 spin_unlock_irq(&hdsp->lock);
2490 return change;
2491}
2492
2493#define HDSP_XLR_BREAKOUT_CABLE(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002494{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 .name = xname, \
2496 .index = xindex, \
2497 .info = snd_hdsp_info_xlr_breakout_cable, \
2498 .get = snd_hdsp_get_xlr_breakout_cable, \
2499 .put = snd_hdsp_put_xlr_breakout_cable \
2500}
2501
Takashi Iwai55e957d2005-11-17 14:52:13 +01002502static int hdsp_xlr_breakout_cable(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503{
Takashi Iwaib0b981192005-10-20 18:29:58 +02002504 if (hdsp->control_register & HDSP_XLRBreakoutCable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 return 0;
2507}
2508
Takashi Iwai55e957d2005-11-17 14:52:13 +01002509static int hdsp_set_xlr_breakout_cable(struct hdsp *hdsp, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510{
Takashi Iwaib0b981192005-10-20 18:29:58 +02002511 if (mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 hdsp->control_register |= HDSP_XLRBreakoutCable;
Takashi Iwaib0b981192005-10-20 18:29:58 +02002513 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 hdsp->control_register &= ~HDSP_XLRBreakoutCable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2516 return 0;
2517}
2518
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002519#define snd_hdsp_info_xlr_breakout_cable snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520
Takashi Iwai55e957d2005-11-17 14:52:13 +01002521static int snd_hdsp_get_xlr_breakout_cable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002523 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002524
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 ucontrol->value.enumerated.item[0] = hdsp_xlr_breakout_cable(hdsp);
2526 return 0;
2527}
2528
Takashi Iwai55e957d2005-11-17 14:52:13 +01002529static int snd_hdsp_put_xlr_breakout_cable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002531 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 int change;
2533 int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002534
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 if (!snd_hdsp_use_is_exclusive(hdsp))
2536 return -EBUSY;
2537 val = ucontrol->value.integer.value[0] & 1;
2538 spin_lock_irq(&hdsp->lock);
2539 change = (int)val != hdsp_xlr_breakout_cable(hdsp);
2540 hdsp_set_xlr_breakout_cable(hdsp, val);
2541 spin_unlock_irq(&hdsp->lock);
2542 return change;
2543}
2544
2545/* (De)activates old RME Analog Extension Board
2546 These are connected to the internal ADAT connector
2547 Switching this on desactivates external ADAT
2548*/
2549#define HDSP_AEB(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002550{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 .name = xname, \
2552 .index = xindex, \
2553 .info = snd_hdsp_info_aeb, \
2554 .get = snd_hdsp_get_aeb, \
2555 .put = snd_hdsp_put_aeb \
2556}
2557
Takashi Iwai55e957d2005-11-17 14:52:13 +01002558static int hdsp_aeb(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559{
Takashi Iwaib0b981192005-10-20 18:29:58 +02002560 if (hdsp->control_register & HDSP_AnalogExtensionBoard)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 return 0;
2563}
2564
Takashi Iwai55e957d2005-11-17 14:52:13 +01002565static int hdsp_set_aeb(struct hdsp *hdsp, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566{
Takashi Iwaib0b981192005-10-20 18:29:58 +02002567 if (mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 hdsp->control_register |= HDSP_AnalogExtensionBoard;
Takashi Iwaib0b981192005-10-20 18:29:58 +02002569 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 hdsp->control_register &= ~HDSP_AnalogExtensionBoard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2572 return 0;
2573}
2574
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002575#define snd_hdsp_info_aeb snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576
Takashi Iwai55e957d2005-11-17 14:52:13 +01002577static int snd_hdsp_get_aeb(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002579 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002580
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 ucontrol->value.enumerated.item[0] = hdsp_aeb(hdsp);
2582 return 0;
2583}
2584
Takashi Iwai55e957d2005-11-17 14:52:13 +01002585static int snd_hdsp_put_aeb(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 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_aeb(hdsp);
2596 hdsp_set_aeb(hdsp, val);
2597 spin_unlock_irq(&hdsp->lock);
2598 return change;
2599}
2600
2601#define HDSP_PREF_SYNC_REF(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002602{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 .name = xname, \
2604 .index = xindex, \
2605 .info = snd_hdsp_info_pref_sync_ref, \
2606 .get = snd_hdsp_get_pref_sync_ref, \
2607 .put = snd_hdsp_put_pref_sync_ref \
2608}
2609
Takashi Iwai55e957d2005-11-17 14:52:13 +01002610static int hdsp_pref_sync_ref(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611{
2612 /* Notice that this looks at the requested sync source,
2613 not the one actually in use.
2614 */
2615
2616 switch (hdsp->control_register & HDSP_SyncRefMask) {
2617 case HDSP_SyncRef_ADAT1:
2618 return HDSP_SYNC_FROM_ADAT1;
2619 case HDSP_SyncRef_ADAT2:
2620 return HDSP_SYNC_FROM_ADAT2;
2621 case HDSP_SyncRef_ADAT3:
2622 return HDSP_SYNC_FROM_ADAT3;
2623 case HDSP_SyncRef_SPDIF:
2624 return HDSP_SYNC_FROM_SPDIF;
2625 case HDSP_SyncRef_WORD:
2626 return HDSP_SYNC_FROM_WORD;
2627 case HDSP_SyncRef_ADAT_SYNC:
2628 return HDSP_SYNC_FROM_ADAT_SYNC;
2629 default:
2630 return HDSP_SYNC_FROM_WORD;
2631 }
2632 return 0;
2633}
2634
Takashi Iwai55e957d2005-11-17 14:52:13 +01002635static int hdsp_set_pref_sync_ref(struct hdsp *hdsp, int pref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636{
2637 hdsp->control_register &= ~HDSP_SyncRefMask;
2638 switch (pref) {
2639 case HDSP_SYNC_FROM_ADAT1:
2640 hdsp->control_register &= ~HDSP_SyncRefMask; /* clear SyncRef bits */
2641 break;
2642 case HDSP_SYNC_FROM_ADAT2:
2643 hdsp->control_register |= HDSP_SyncRef_ADAT2;
2644 break;
2645 case HDSP_SYNC_FROM_ADAT3:
2646 hdsp->control_register |= HDSP_SyncRef_ADAT3;
2647 break;
2648 case HDSP_SYNC_FROM_SPDIF:
2649 hdsp->control_register |= HDSP_SyncRef_SPDIF;
2650 break;
2651 case HDSP_SYNC_FROM_WORD:
2652 hdsp->control_register |= HDSP_SyncRef_WORD;
2653 break;
2654 case HDSP_SYNC_FROM_ADAT_SYNC:
2655 hdsp->control_register |= HDSP_SyncRef_ADAT_SYNC;
2656 break;
2657 default:
2658 return -1;
2659 }
2660 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2661 return 0;
2662}
2663
Takashi Iwai55e957d2005-11-17 14:52:13 +01002664static int snd_hdsp_info_pref_sync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665{
2666 static char *texts[] = {"Word", "IEC958", "ADAT1", "ADAT Sync", "ADAT2", "ADAT3" };
Takashi Iwai55e957d2005-11-17 14:52:13 +01002667 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002668
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2670 uinfo->count = 1;
2671
2672 switch (hdsp->io_type) {
2673 case Digiface:
2674 case H9652:
2675 uinfo->value.enumerated.items = 6;
2676 break;
2677 case Multiface:
2678 uinfo->value.enumerated.items = 4;
2679 break;
2680 case H9632:
2681 uinfo->value.enumerated.items = 3;
2682 break;
2683 default:
Takashi Iwai9badda02012-01-09 18:22:35 +01002684 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002686
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2688 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2689 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2690 return 0;
2691}
2692
Takashi Iwai55e957d2005-11-17 14:52:13 +01002693static int snd_hdsp_get_pref_sync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002695 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002696
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 ucontrol->value.enumerated.item[0] = hdsp_pref_sync_ref(hdsp);
2698 return 0;
2699}
2700
Takashi Iwai55e957d2005-11-17 14:52:13 +01002701static int snd_hdsp_put_pref_sync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002703 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 int change, max;
2705 unsigned int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002706
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 if (!snd_hdsp_use_is_exclusive(hdsp))
2708 return -EBUSY;
2709
2710 switch (hdsp->io_type) {
2711 case Digiface:
2712 case H9652:
2713 max = 6;
2714 break;
2715 case Multiface:
2716 max = 4;
2717 break;
2718 case H9632:
2719 max = 3;
2720 break;
2721 default:
2722 return -EIO;
2723 }
2724
2725 val = ucontrol->value.enumerated.item[0] % max;
2726 spin_lock_irq(&hdsp->lock);
2727 change = (int)val != hdsp_pref_sync_ref(hdsp);
2728 hdsp_set_pref_sync_ref(hdsp, val);
2729 spin_unlock_irq(&hdsp->lock);
2730 return change;
2731}
2732
2733#define HDSP_AUTOSYNC_REF(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002734{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 .name = xname, \
2736 .index = xindex, \
2737 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
2738 .info = snd_hdsp_info_autosync_ref, \
2739 .get = snd_hdsp_get_autosync_ref, \
2740}
2741
Takashi Iwai55e957d2005-11-17 14:52:13 +01002742static int hdsp_autosync_ref(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743{
2744 /* This looks at the autosync selected sync reference */
2745 unsigned int status2 = hdsp_read(hdsp, HDSP_status2Register);
2746
2747 switch (status2 & HDSP_SelSyncRefMask) {
2748 case HDSP_SelSyncRef_WORD:
2749 return HDSP_AUTOSYNC_FROM_WORD;
2750 case HDSP_SelSyncRef_ADAT_SYNC:
2751 return HDSP_AUTOSYNC_FROM_ADAT_SYNC;
2752 case HDSP_SelSyncRef_SPDIF:
2753 return HDSP_AUTOSYNC_FROM_SPDIF;
2754 case HDSP_SelSyncRefMask:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002755 return HDSP_AUTOSYNC_FROM_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 case HDSP_SelSyncRef_ADAT1:
2757 return HDSP_AUTOSYNC_FROM_ADAT1;
2758 case HDSP_SelSyncRef_ADAT2:
2759 return HDSP_AUTOSYNC_FROM_ADAT2;
2760 case HDSP_SelSyncRef_ADAT3:
2761 return HDSP_AUTOSYNC_FROM_ADAT3;
2762 default:
2763 return HDSP_AUTOSYNC_FROM_WORD;
2764 }
2765 return 0;
2766}
2767
Takashi Iwai55e957d2005-11-17 14:52:13 +01002768static int snd_hdsp_info_autosync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769{
2770 static char *texts[] = {"Word", "ADAT Sync", "IEC958", "None", "ADAT1", "ADAT2", "ADAT3" };
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002771
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2773 uinfo->count = 1;
2774 uinfo->value.enumerated.items = 7;
2775 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2776 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2777 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2778 return 0;
2779}
2780
Takashi Iwai55e957d2005-11-17 14:52:13 +01002781static int snd_hdsp_get_autosync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002783 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002784
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 ucontrol->value.enumerated.item[0] = hdsp_autosync_ref(hdsp);
2786 return 0;
2787}
2788
2789#define HDSP_LINE_OUT(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002790{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 .name = xname, \
2792 .index = xindex, \
2793 .info = snd_hdsp_info_line_out, \
2794 .get = snd_hdsp_get_line_out, \
2795 .put = snd_hdsp_put_line_out \
2796}
2797
Takashi Iwai55e957d2005-11-17 14:52:13 +01002798static int hdsp_line_out(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799{
2800 return (hdsp->control_register & HDSP_LineOut) ? 1 : 0;
2801}
2802
Takashi Iwai55e957d2005-11-17 14:52:13 +01002803static int hdsp_set_line_output(struct hdsp *hdsp, int out)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804{
Takashi Iwaib0b981192005-10-20 18:29:58 +02002805 if (out)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 hdsp->control_register |= HDSP_LineOut;
Takashi Iwaib0b981192005-10-20 18:29:58 +02002807 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 hdsp->control_register &= ~HDSP_LineOut;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2810 return 0;
2811}
2812
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002813#define snd_hdsp_info_line_out snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814
Takashi Iwai55e957d2005-11-17 14:52:13 +01002815static int snd_hdsp_get_line_out(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002817 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002818
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 spin_lock_irq(&hdsp->lock);
2820 ucontrol->value.integer.value[0] = hdsp_line_out(hdsp);
2821 spin_unlock_irq(&hdsp->lock);
2822 return 0;
2823}
2824
Takashi Iwai55e957d2005-11-17 14:52:13 +01002825static int snd_hdsp_put_line_out(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002827 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 int change;
2829 unsigned int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002830
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 if (!snd_hdsp_use_is_exclusive(hdsp))
2832 return -EBUSY;
2833 val = ucontrol->value.integer.value[0] & 1;
2834 spin_lock_irq(&hdsp->lock);
2835 change = (int)val != hdsp_line_out(hdsp);
2836 hdsp_set_line_output(hdsp, val);
2837 spin_unlock_irq(&hdsp->lock);
2838 return change;
2839}
2840
2841#define HDSP_PRECISE_POINTER(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002842{ .iface = SNDRV_CTL_ELEM_IFACE_CARD, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 .name = xname, \
2844 .index = xindex, \
2845 .info = snd_hdsp_info_precise_pointer, \
2846 .get = snd_hdsp_get_precise_pointer, \
2847 .put = snd_hdsp_put_precise_pointer \
2848}
2849
Takashi Iwai55e957d2005-11-17 14:52:13 +01002850static int hdsp_set_precise_pointer(struct hdsp *hdsp, int precise)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851{
Takashi Iwaib0b981192005-10-20 18:29:58 +02002852 if (precise)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 hdsp->precise_ptr = 1;
Takashi Iwaib0b981192005-10-20 18:29:58 +02002854 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 hdsp->precise_ptr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 return 0;
2857}
2858
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002859#define snd_hdsp_info_precise_pointer snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860
Takashi Iwai55e957d2005-11-17 14:52:13 +01002861static int snd_hdsp_get_precise_pointer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002863 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002864
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 spin_lock_irq(&hdsp->lock);
2866 ucontrol->value.integer.value[0] = hdsp->precise_ptr;
2867 spin_unlock_irq(&hdsp->lock);
2868 return 0;
2869}
2870
Takashi Iwai55e957d2005-11-17 14:52:13 +01002871static int snd_hdsp_put_precise_pointer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002873 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 int change;
2875 unsigned int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002876
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 if (!snd_hdsp_use_is_exclusive(hdsp))
2878 return -EBUSY;
2879 val = ucontrol->value.integer.value[0] & 1;
2880 spin_lock_irq(&hdsp->lock);
2881 change = (int)val != hdsp->precise_ptr;
2882 hdsp_set_precise_pointer(hdsp, val);
2883 spin_unlock_irq(&hdsp->lock);
2884 return change;
2885}
2886
2887#define HDSP_USE_MIDI_TASKLET(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002888{ .iface = SNDRV_CTL_ELEM_IFACE_CARD, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 .name = xname, \
2890 .index = xindex, \
2891 .info = snd_hdsp_info_use_midi_tasklet, \
2892 .get = snd_hdsp_get_use_midi_tasklet, \
2893 .put = snd_hdsp_put_use_midi_tasklet \
2894}
2895
Takashi Iwai55e957d2005-11-17 14:52:13 +01002896static int hdsp_set_use_midi_tasklet(struct hdsp *hdsp, int use_tasklet)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897{
Takashi Iwaib0b981192005-10-20 18:29:58 +02002898 if (use_tasklet)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 hdsp->use_midi_tasklet = 1;
Takashi Iwaib0b981192005-10-20 18:29:58 +02002900 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 hdsp->use_midi_tasklet = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 return 0;
2903}
2904
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002905#define snd_hdsp_info_use_midi_tasklet snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906
Takashi Iwai55e957d2005-11-17 14:52:13 +01002907static int snd_hdsp_get_use_midi_tasklet(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002909 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002910
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 spin_lock_irq(&hdsp->lock);
2912 ucontrol->value.integer.value[0] = hdsp->use_midi_tasklet;
2913 spin_unlock_irq(&hdsp->lock);
2914 return 0;
2915}
2916
Takashi Iwai55e957d2005-11-17 14:52:13 +01002917static int snd_hdsp_put_use_midi_tasklet(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002919 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 int change;
2921 unsigned int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002922
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 if (!snd_hdsp_use_is_exclusive(hdsp))
2924 return -EBUSY;
2925 val = ucontrol->value.integer.value[0] & 1;
2926 spin_lock_irq(&hdsp->lock);
2927 change = (int)val != hdsp->use_midi_tasklet;
2928 hdsp_set_use_midi_tasklet(hdsp, val);
2929 spin_unlock_irq(&hdsp->lock);
2930 return change;
2931}
2932
2933#define HDSP_MIXER(xname, xindex) \
2934{ .iface = SNDRV_CTL_ELEM_IFACE_HWDEP, \
2935 .name = xname, \
2936 .index = xindex, \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002937 .device = 0, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
2939 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
2940 .info = snd_hdsp_info_mixer, \
2941 .get = snd_hdsp_get_mixer, \
2942 .put = snd_hdsp_put_mixer \
2943}
2944
Takashi Iwai55e957d2005-11-17 14:52:13 +01002945static int snd_hdsp_info_mixer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946{
2947 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2948 uinfo->count = 3;
2949 uinfo->value.integer.min = 0;
2950 uinfo->value.integer.max = 65536;
2951 uinfo->value.integer.step = 1;
2952 return 0;
2953}
2954
Takashi Iwai55e957d2005-11-17 14:52:13 +01002955static int snd_hdsp_get_mixer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002957 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 int source;
2959 int destination;
2960 int addr;
2961
2962 source = ucontrol->value.integer.value[0];
2963 destination = ucontrol->value.integer.value[1];
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002964
Takashi Iwaib0b981192005-10-20 18:29:58 +02002965 if (source >= hdsp->max_channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 addr = hdsp_playback_to_output_key(hdsp,source-hdsp->max_channels,destination);
Takashi Iwaib0b981192005-10-20 18:29:58 +02002967 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 addr = hdsp_input_to_output_key(hdsp,source, destination);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002969
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 spin_lock_irq(&hdsp->lock);
2971 ucontrol->value.integer.value[2] = hdsp_read_gain (hdsp, addr);
2972 spin_unlock_irq(&hdsp->lock);
2973 return 0;
2974}
2975
Takashi Iwai55e957d2005-11-17 14:52:13 +01002976static int snd_hdsp_put_mixer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002978 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 int change;
2980 int source;
2981 int destination;
2982 int gain;
2983 int addr;
2984
2985 if (!snd_hdsp_use_is_exclusive(hdsp))
2986 return -EBUSY;
2987
2988 source = ucontrol->value.integer.value[0];
2989 destination = ucontrol->value.integer.value[1];
2990
Takashi Iwaib0b981192005-10-20 18:29:58 +02002991 if (source >= hdsp->max_channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 addr = hdsp_playback_to_output_key(hdsp,source-hdsp->max_channels, destination);
Takashi Iwaib0b981192005-10-20 18:29:58 +02002993 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 addr = hdsp_input_to_output_key(hdsp,source, destination);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995
2996 gain = ucontrol->value.integer.value[2];
2997
2998 spin_lock_irq(&hdsp->lock);
2999 change = gain != hdsp_read_gain(hdsp, addr);
3000 if (change)
3001 hdsp_write_gain(hdsp, addr, gain);
3002 spin_unlock_irq(&hdsp->lock);
3003 return change;
3004}
3005
3006#define HDSP_WC_SYNC_CHECK(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02003007{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 .name = xname, \
3009 .index = xindex, \
3010 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
3011 .info = snd_hdsp_info_sync_check, \
3012 .get = snd_hdsp_get_wc_sync_check \
3013}
3014
Takashi Iwai55e957d2005-11-17 14:52:13 +01003015static int snd_hdsp_info_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016{
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003017 static char *texts[] = {"No Lock", "Lock", "Sync" };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3019 uinfo->count = 1;
3020 uinfo->value.enumerated.items = 3;
3021 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
3022 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
3023 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
3024 return 0;
3025}
3026
Takashi Iwai55e957d2005-11-17 14:52:13 +01003027static int hdsp_wc_sync_check(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028{
3029 int status2 = hdsp_read(hdsp, HDSP_status2Register);
3030 if (status2 & HDSP_wc_lock) {
Takashi Iwaib0b981192005-10-20 18:29:58 +02003031 if (status2 & HDSP_wc_sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 return 2;
Takashi Iwaib0b981192005-10-20 18:29:58 +02003033 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 return 1;
Takashi Iwaib0b981192005-10-20 18:29:58 +02003035 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 return 0;
3038}
3039
Takashi Iwai55e957d2005-11-17 14:52:13 +01003040static int snd_hdsp_get_wc_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003042 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043
3044 ucontrol->value.enumerated.item[0] = hdsp_wc_sync_check(hdsp);
3045 return 0;
3046}
3047
3048#define HDSP_SPDIF_SYNC_CHECK(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02003049{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 .name = xname, \
3051 .index = xindex, \
3052 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
3053 .info = snd_hdsp_info_sync_check, \
3054 .get = snd_hdsp_get_spdif_sync_check \
3055}
3056
Takashi Iwai55e957d2005-11-17 14:52:13 +01003057static int hdsp_spdif_sync_check(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058{
3059 int status = hdsp_read(hdsp, HDSP_statusRegister);
Takashi Iwaib0b981192005-10-20 18:29:58 +02003060 if (status & HDSP_SPDIFErrorFlag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 return 0;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003062 else {
Takashi Iwaib0b981192005-10-20 18:29:58 +02003063 if (status & HDSP_SPDIFSync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 return 2;
Takashi Iwaib0b981192005-10-20 18:29:58 +02003065 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 }
3068 return 0;
3069}
3070
Takashi Iwai55e957d2005-11-17 14:52:13 +01003071static int snd_hdsp_get_spdif_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003073 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074
3075 ucontrol->value.enumerated.item[0] = hdsp_spdif_sync_check(hdsp);
3076 return 0;
3077}
3078
3079#define HDSP_ADATSYNC_SYNC_CHECK(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02003080{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 .name = xname, \
3082 .index = xindex, \
3083 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
3084 .info = snd_hdsp_info_sync_check, \
3085 .get = snd_hdsp_get_adatsync_sync_check \
3086}
3087
Takashi Iwai55e957d2005-11-17 14:52:13 +01003088static int hdsp_adatsync_sync_check(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089{
3090 int status = hdsp_read(hdsp, HDSP_statusRegister);
3091 if (status & HDSP_TimecodeLock) {
Takashi Iwaib0b981192005-10-20 18:29:58 +02003092 if (status & HDSP_TimecodeSync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 return 2;
Takashi Iwaib0b981192005-10-20 18:29:58 +02003094 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 return 1;
Takashi Iwaib0b981192005-10-20 18:29:58 +02003096 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 return 0;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003098}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099
Takashi Iwai55e957d2005-11-17 14:52:13 +01003100static int snd_hdsp_get_adatsync_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003102 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103
3104 ucontrol->value.enumerated.item[0] = hdsp_adatsync_sync_check(hdsp);
3105 return 0;
3106}
3107
3108#define HDSP_ADAT_SYNC_CHECK \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02003109{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
3111 .info = snd_hdsp_info_sync_check, \
3112 .get = snd_hdsp_get_adat_sync_check \
3113}
3114
Takashi Iwai55e957d2005-11-17 14:52:13 +01003115static int hdsp_adat_sync_check(struct hdsp *hdsp, int idx)
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003116{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 int status = hdsp_read(hdsp, HDSP_statusRegister);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003118
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 if (status & (HDSP_Lock0>>idx)) {
Takashi Iwaib0b981192005-10-20 18:29:58 +02003120 if (status & (HDSP_Sync0>>idx))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 return 2;
Takashi Iwaib0b981192005-10-20 18:29:58 +02003122 else
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003123 return 1;
Takashi Iwaib0b981192005-10-20 18:29:58 +02003124 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 return 0;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003126}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127
Takashi Iwai55e957d2005-11-17 14:52:13 +01003128static int snd_hdsp_get_adat_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129{
3130 int offset;
Takashi Iwai55e957d2005-11-17 14:52:13 +01003131 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132
3133 offset = ucontrol->id.index - 1;
Takashi Iwaida3cec32008-08-08 17:12:14 +02003134 snd_BUG_ON(offset < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135
3136 switch (hdsp->io_type) {
3137 case Digiface:
3138 case H9652:
3139 if (offset >= 3)
3140 return -EINVAL;
3141 break;
3142 case Multiface:
3143 case H9632:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003144 if (offset >= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 return -EINVAL;
3146 break;
3147 default:
3148 return -EIO;
3149 }
3150
3151 ucontrol->value.enumerated.item[0] = hdsp_adat_sync_check(hdsp, offset);
3152 return 0;
3153}
3154
Julian Cablee4b60882007-03-19 11:44:40 +01003155#define HDSP_DDS_OFFSET(xname, xindex) \
3156{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3157 .name = xname, \
3158 .index = xindex, \
3159 .info = snd_hdsp_info_dds_offset, \
3160 .get = snd_hdsp_get_dds_offset, \
3161 .put = snd_hdsp_put_dds_offset \
3162}
3163
3164static int hdsp_dds_offset(struct hdsp *hdsp)
3165{
3166 u64 n;
Julian Cablee4b60882007-03-19 11:44:40 +01003167 unsigned int dds_value = hdsp->dds_value;
3168 int system_sample_rate = hdsp->system_sample_rate;
3169
Takashi Iwai2a3988f2007-10-16 14:26:32 +02003170 if (!dds_value)
3171 return 0;
3172
Julian Cablee4b60882007-03-19 11:44:40 +01003173 n = DDS_NUMERATOR;
3174 /*
3175 * dds_value = n / rate
3176 * rate = n / dds_value
3177 */
Takashi Iwai3f7440a2009-06-05 17:40:04 +02003178 n = div_u64(n, dds_value);
Julian Cablee4b60882007-03-19 11:44:40 +01003179 if (system_sample_rate >= 112000)
3180 n *= 4;
3181 else if (system_sample_rate >= 56000)
3182 n *= 2;
3183 return ((int)n) - system_sample_rate;
3184}
3185
3186static int hdsp_set_dds_offset(struct hdsp *hdsp, int offset_hz)
3187{
3188 int rate = hdsp->system_sample_rate + offset_hz;
3189 hdsp_set_dds_value(hdsp, rate);
3190 return 0;
3191}
3192
3193static int snd_hdsp_info_dds_offset(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
3194{
3195 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3196 uinfo->count = 1;
3197 uinfo->value.integer.min = -5000;
3198 uinfo->value.integer.max = 5000;
3199 return 0;
3200}
3201
3202static int snd_hdsp_get_dds_offset(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3203{
3204 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003205
Julian Cablee4b60882007-03-19 11:44:40 +01003206 ucontrol->value.enumerated.item[0] = hdsp_dds_offset(hdsp);
3207 return 0;
3208}
3209
3210static int snd_hdsp_put_dds_offset(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3211{
3212 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
3213 int change;
3214 int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003215
Julian Cablee4b60882007-03-19 11:44:40 +01003216 if (!snd_hdsp_use_is_exclusive(hdsp))
3217 return -EBUSY;
3218 val = ucontrol->value.enumerated.item[0];
3219 spin_lock_irq(&hdsp->lock);
3220 if (val != hdsp_dds_offset(hdsp))
3221 change = (hdsp_set_dds_offset(hdsp, val) == 0) ? 1 : 0;
3222 else
3223 change = 0;
3224 spin_unlock_irq(&hdsp->lock);
3225 return change;
3226}
3227
Takashi Iwai55e957d2005-11-17 14:52:13 +01003228static struct snd_kcontrol_new snd_hdsp_9632_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229HDSP_DA_GAIN("DA Gain", 0),
3230HDSP_AD_GAIN("AD Gain", 0),
3231HDSP_PHONE_GAIN("Phones Gain", 0),
Julian Cablee4b60882007-03-19 11:44:40 +01003232HDSP_XLR_BREAKOUT_CABLE("XLR Breakout Cable", 0),
3233HDSP_DDS_OFFSET("DDS Sample Rate Offset", 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234};
3235
Takashi Iwai55e957d2005-11-17 14:52:13 +01003236static struct snd_kcontrol_new snd_hdsp_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237{
Clemens Ladisch5549d542005-08-03 13:50:30 +02003238 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
3240 .info = snd_hdsp_control_spdif_info,
3241 .get = snd_hdsp_control_spdif_get,
3242 .put = snd_hdsp_control_spdif_put,
3243},
3244{
3245 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
Clemens Ladisch5549d542005-08-03 13:50:30 +02003246 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
3248 .info = snd_hdsp_control_spdif_stream_info,
3249 .get = snd_hdsp_control_spdif_stream_get,
3250 .put = snd_hdsp_control_spdif_stream_put,
3251},
3252{
3253 .access = SNDRV_CTL_ELEM_ACCESS_READ,
Clemens Ladisch5549d542005-08-03 13:50:30 +02003254 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
3256 .info = snd_hdsp_control_spdif_mask_info,
3257 .get = snd_hdsp_control_spdif_mask_get,
3258 .private_value = IEC958_AES0_NONAUDIO |
3259 IEC958_AES0_PROFESSIONAL |
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003260 IEC958_AES0_CON_EMPHASIS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261},
3262{
3263 .access = SNDRV_CTL_ELEM_ACCESS_READ,
Clemens Ladisch5549d542005-08-03 13:50:30 +02003264 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
3266 .info = snd_hdsp_control_spdif_mask_info,
3267 .get = snd_hdsp_control_spdif_mask_get,
3268 .private_value = IEC958_AES0_NONAUDIO |
3269 IEC958_AES0_PROFESSIONAL |
3270 IEC958_AES0_PRO_EMPHASIS,
3271},
3272HDSP_MIXER("Mixer", 0),
3273HDSP_SPDIF_IN("IEC958 Input Connector", 0),
3274HDSP_SPDIF_OUT("IEC958 Output also on ADAT1", 0),
3275HDSP_SPDIF_PROFESSIONAL("IEC958 Professional Bit", 0),
3276HDSP_SPDIF_EMPHASIS("IEC958 Emphasis Bit", 0),
3277HDSP_SPDIF_NON_AUDIO("IEC958 Non-audio Bit", 0),
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003278/* 'Sample Clock Source' complies with the alsa control naming scheme */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279HDSP_CLOCK_SOURCE("Sample Clock Source", 0),
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02003280{
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02003281 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3282 .name = "Sample Clock Source Locking",
3283 .info = snd_hdsp_info_clock_source_lock,
3284 .get = snd_hdsp_get_clock_source_lock,
3285 .put = snd_hdsp_put_clock_source_lock,
3286},
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287HDSP_SYSTEM_CLOCK_MODE("System Clock Mode", 0),
3288HDSP_PREF_SYNC_REF("Preferred Sync Reference", 0),
3289HDSP_AUTOSYNC_REF("AutoSync Reference", 0),
3290HDSP_SPDIF_SAMPLE_RATE("SPDIF Sample Rate", 0),
3291HDSP_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
3292/* 'External Rate' complies with the alsa control naming scheme */
3293HDSP_AUTOSYNC_SAMPLE_RATE("External Rate", 0),
3294HDSP_WC_SYNC_CHECK("Word Clock Lock Status", 0),
3295HDSP_SPDIF_SYNC_CHECK("SPDIF Lock Status", 0),
3296HDSP_ADATSYNC_SYNC_CHECK("ADAT Sync Lock Status", 0),
3297HDSP_LINE_OUT("Line Out", 0),
3298HDSP_PRECISE_POINTER("Precise Pointer", 0),
3299HDSP_USE_MIDI_TASKLET("Use Midi Tasklet", 0),
3300};
3301
Florian Faber28b26e12010-12-01 12:14:47 +01003302
3303static int hdsp_rpm_input12(struct hdsp *hdsp)
3304{
3305 switch (hdsp->control_register & HDSP_RPM_Inp12) {
3306 case HDSP_RPM_Inp12_Phon_6dB:
3307 return 0;
3308 case HDSP_RPM_Inp12_Phon_n6dB:
3309 return 2;
3310 case HDSP_RPM_Inp12_Line_0dB:
3311 return 3;
3312 case HDSP_RPM_Inp12_Line_n6dB:
3313 return 4;
3314 }
3315 return 1;
3316}
3317
3318
3319static int snd_hdsp_get_rpm_input12(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3320{
3321 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
3322
3323 ucontrol->value.enumerated.item[0] = hdsp_rpm_input12(hdsp);
3324 return 0;
3325}
3326
3327
3328static int hdsp_set_rpm_input12(struct hdsp *hdsp, int mode)
3329{
3330 hdsp->control_register &= ~HDSP_RPM_Inp12;
3331 switch (mode) {
3332 case 0:
3333 hdsp->control_register |= HDSP_RPM_Inp12_Phon_6dB;
3334 break;
3335 case 1:
3336 break;
3337 case 2:
3338 hdsp->control_register |= HDSP_RPM_Inp12_Phon_n6dB;
3339 break;
3340 case 3:
3341 hdsp->control_register |= HDSP_RPM_Inp12_Line_0dB;
3342 break;
3343 case 4:
3344 hdsp->control_register |= HDSP_RPM_Inp12_Line_n6dB;
3345 break;
3346 default:
3347 return -1;
3348 }
3349
3350 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
3351 return 0;
3352}
3353
3354
3355static int snd_hdsp_put_rpm_input12(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3356{
3357 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
3358 int change;
3359 int val;
3360
3361 if (!snd_hdsp_use_is_exclusive(hdsp))
3362 return -EBUSY;
3363 val = ucontrol->value.enumerated.item[0];
3364 if (val < 0)
3365 val = 0;
3366 if (val > 4)
3367 val = 4;
3368 spin_lock_irq(&hdsp->lock);
3369 if (val != hdsp_rpm_input12(hdsp))
3370 change = (hdsp_set_rpm_input12(hdsp, val) == 0) ? 1 : 0;
3371 else
3372 change = 0;
3373 spin_unlock_irq(&hdsp->lock);
3374 return change;
3375}
3376
3377
3378static int snd_hdsp_info_rpm_input(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
3379{
3380 static char *texts[] = {"Phono +6dB", "Phono 0dB", "Phono -6dB", "Line 0dB", "Line -6dB"};
3381
3382 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3383 uinfo->count = 1;
3384 uinfo->value.enumerated.items = 5;
3385 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
3386 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
3387 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
3388 return 0;
3389}
3390
3391
3392static int hdsp_rpm_input34(struct hdsp *hdsp)
3393{
3394 switch (hdsp->control_register & HDSP_RPM_Inp34) {
3395 case HDSP_RPM_Inp34_Phon_6dB:
3396 return 0;
3397 case HDSP_RPM_Inp34_Phon_n6dB:
3398 return 2;
3399 case HDSP_RPM_Inp34_Line_0dB:
3400 return 3;
3401 case HDSP_RPM_Inp34_Line_n6dB:
3402 return 4;
3403 }
3404 return 1;
3405}
3406
3407
3408static int snd_hdsp_get_rpm_input34(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3409{
3410 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
3411
3412 ucontrol->value.enumerated.item[0] = hdsp_rpm_input34(hdsp);
3413 return 0;
3414}
3415
3416
3417static int hdsp_set_rpm_input34(struct hdsp *hdsp, int mode)
3418{
3419 hdsp->control_register &= ~HDSP_RPM_Inp34;
3420 switch (mode) {
3421 case 0:
3422 hdsp->control_register |= HDSP_RPM_Inp34_Phon_6dB;
3423 break;
3424 case 1:
3425 break;
3426 case 2:
3427 hdsp->control_register |= HDSP_RPM_Inp34_Phon_n6dB;
3428 break;
3429 case 3:
3430 hdsp->control_register |= HDSP_RPM_Inp34_Line_0dB;
3431 break;
3432 case 4:
3433 hdsp->control_register |= HDSP_RPM_Inp34_Line_n6dB;
3434 break;
3435 default:
3436 return -1;
3437 }
3438
3439 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
3440 return 0;
3441}
3442
3443
3444static int snd_hdsp_put_rpm_input34(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3445{
3446 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
3447 int change;
3448 int val;
3449
3450 if (!snd_hdsp_use_is_exclusive(hdsp))
3451 return -EBUSY;
3452 val = ucontrol->value.enumerated.item[0];
3453 if (val < 0)
3454 val = 0;
3455 if (val > 4)
3456 val = 4;
3457 spin_lock_irq(&hdsp->lock);
3458 if (val != hdsp_rpm_input34(hdsp))
3459 change = (hdsp_set_rpm_input34(hdsp, val) == 0) ? 1 : 0;
3460 else
3461 change = 0;
3462 spin_unlock_irq(&hdsp->lock);
3463 return change;
3464}
3465
3466
3467/* RPM Bypass switch */
3468static int hdsp_rpm_bypass(struct hdsp *hdsp)
3469{
3470 return (hdsp->control_register & HDSP_RPM_Bypass) ? 1 : 0;
3471}
3472
3473
3474static int snd_hdsp_get_rpm_bypass(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3475{
3476 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
3477
3478 ucontrol->value.integer.value[0] = hdsp_rpm_bypass(hdsp);
3479 return 0;
3480}
3481
3482
3483static int hdsp_set_rpm_bypass(struct hdsp *hdsp, int on)
3484{
3485 if (on)
3486 hdsp->control_register |= HDSP_RPM_Bypass;
3487 else
3488 hdsp->control_register &= ~HDSP_RPM_Bypass;
3489 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
3490 return 0;
3491}
3492
3493
3494static int snd_hdsp_put_rpm_bypass(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3495{
3496 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
3497 int change;
3498 unsigned int val;
3499
3500 if (!snd_hdsp_use_is_exclusive(hdsp))
3501 return -EBUSY;
3502 val = ucontrol->value.integer.value[0] & 1;
3503 spin_lock_irq(&hdsp->lock);
3504 change = (int)val != hdsp_rpm_bypass(hdsp);
3505 hdsp_set_rpm_bypass(hdsp, val);
3506 spin_unlock_irq(&hdsp->lock);
3507 return change;
3508}
3509
3510
3511static int snd_hdsp_info_rpm_bypass(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
3512{
3513 static char *texts[] = {"On", "Off"};
3514
3515 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3516 uinfo->count = 1;
3517 uinfo->value.enumerated.items = 2;
3518 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
3519 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
3520 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
3521 return 0;
3522}
3523
3524
3525/* RPM Disconnect switch */
3526static int hdsp_rpm_disconnect(struct hdsp *hdsp)
3527{
3528 return (hdsp->control_register & HDSP_RPM_Disconnect) ? 1 : 0;
3529}
3530
3531
3532static int snd_hdsp_get_rpm_disconnect(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3533{
3534 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
3535
3536 ucontrol->value.integer.value[0] = hdsp_rpm_disconnect(hdsp);
3537 return 0;
3538}
3539
3540
3541static int hdsp_set_rpm_disconnect(struct hdsp *hdsp, int on)
3542{
3543 if (on)
3544 hdsp->control_register |= HDSP_RPM_Disconnect;
3545 else
3546 hdsp->control_register &= ~HDSP_RPM_Disconnect;
3547 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
3548 return 0;
3549}
3550
3551
3552static int snd_hdsp_put_rpm_disconnect(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3553{
3554 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
3555 int change;
3556 unsigned int val;
3557
3558 if (!snd_hdsp_use_is_exclusive(hdsp))
3559 return -EBUSY;
3560 val = ucontrol->value.integer.value[0] & 1;
3561 spin_lock_irq(&hdsp->lock);
3562 change = (int)val != hdsp_rpm_disconnect(hdsp);
3563 hdsp_set_rpm_disconnect(hdsp, val);
3564 spin_unlock_irq(&hdsp->lock);
3565 return change;
3566}
3567
3568static int snd_hdsp_info_rpm_disconnect(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
3569{
3570 static char *texts[] = {"On", "Off"};
3571
3572 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3573 uinfo->count = 1;
3574 uinfo->value.enumerated.items = 2;
3575 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
3576 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
3577 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
3578 return 0;
3579}
3580
3581static struct snd_kcontrol_new snd_hdsp_rpm_controls[] = {
3582 {
3583 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3584 .name = "RPM Bypass",
3585 .get = snd_hdsp_get_rpm_bypass,
3586 .put = snd_hdsp_put_rpm_bypass,
3587 .info = snd_hdsp_info_rpm_bypass
3588 },
3589 {
3590 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3591 .name = "RPM Disconnect",
3592 .get = snd_hdsp_get_rpm_disconnect,
3593 .put = snd_hdsp_put_rpm_disconnect,
3594 .info = snd_hdsp_info_rpm_disconnect
3595 },
3596 {
3597 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3598 .name = "Input 1/2",
3599 .get = snd_hdsp_get_rpm_input12,
3600 .put = snd_hdsp_put_rpm_input12,
3601 .info = snd_hdsp_info_rpm_input
3602 },
3603 {
3604 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3605 .name = "Input 3/4",
3606 .get = snd_hdsp_get_rpm_input34,
3607 .put = snd_hdsp_put_rpm_input34,
3608 .info = snd_hdsp_info_rpm_input
3609 },
3610 HDSP_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
3611 HDSP_MIXER("Mixer", 0)
3612};
3613
Takashi Iwai55e957d2005-11-17 14:52:13 +01003614static struct snd_kcontrol_new snd_hdsp_96xx_aeb = HDSP_AEB("Analog Extension Board", 0);
3615static struct snd_kcontrol_new snd_hdsp_adat_sync_check = HDSP_ADAT_SYNC_CHECK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616
Takashi Iwai55e957d2005-11-17 14:52:13 +01003617static int snd_hdsp_create_controls(struct snd_card *card, struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618{
3619 unsigned int idx;
3620 int err;
Takashi Iwai55e957d2005-11-17 14:52:13 +01003621 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622
Florian Faber28b26e12010-12-01 12:14:47 +01003623 if (hdsp->io_type == RPM) {
3624 /* RPM Bypass, Disconnect and Input switches */
3625 for (idx = 0; idx < ARRAY_SIZE(snd_hdsp_rpm_controls); idx++) {
3626 err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_rpm_controls[idx], hdsp));
3627 if (err < 0)
3628 return err;
3629 }
3630 return 0;
3631 }
3632
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 for (idx = 0; idx < ARRAY_SIZE(snd_hdsp_controls); idx++) {
Takashi Iwaib0b981192005-10-20 18:29:58 +02003634 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_controls[idx], hdsp))) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 if (idx == 1) /* IEC958 (S/PDIF) Stream */
3637 hdsp->spdif_ctl = kctl;
3638 }
3639
3640 /* ADAT SyncCheck status */
3641 snd_hdsp_adat_sync_check.name = "ADAT Lock Status";
3642 snd_hdsp_adat_sync_check.index = 1;
Takashi Iwaib0b981192005-10-20 18:29:58 +02003643 if ((err = snd_ctl_add (card, kctl = snd_ctl_new1(&snd_hdsp_adat_sync_check, hdsp))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 if (hdsp->io_type == Digiface || hdsp->io_type == H9652) {
3646 for (idx = 1; idx < 3; ++idx) {
3647 snd_hdsp_adat_sync_check.index = idx+1;
Takashi Iwaib0b981192005-10-20 18:29:58 +02003648 if ((err = snd_ctl_add (card, kctl = snd_ctl_new1(&snd_hdsp_adat_sync_check, hdsp))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 }
3651 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003652
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 /* DA, AD and Phone gain and XLR breakout cable controls for H9632 cards */
3654 if (hdsp->io_type == H9632) {
3655 for (idx = 0; idx < ARRAY_SIZE(snd_hdsp_9632_controls); idx++) {
Takashi Iwaib0b981192005-10-20 18:29:58 +02003656 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_9632_controls[idx], hdsp))) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 }
3659 }
3660
3661 /* AEB control for H96xx card */
3662 if (hdsp->io_type == H9632 || hdsp->io_type == H9652) {
Takashi Iwaib0b981192005-10-20 18:29:58 +02003663 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_96xx_aeb, hdsp))) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 }
3666
3667 return 0;
3668}
3669
3670/*------------------------------------------------------------
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003671 /proc interface
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 ------------------------------------------------------------*/
3673
3674static void
Takashi Iwai55e957d2005-11-17 14:52:13 +01003675snd_hdsp_proc_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676{
Joe Perches9fe856e2010-09-04 18:52:54 -07003677 struct hdsp *hdsp = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 unsigned int status;
3679 unsigned int status2;
3680 char *pref_sync_ref;
3681 char *autosync_ref;
3682 char *system_clock_mode;
3683 char *clock_source;
3684 int x;
3685
Tim Blechmannc18bc9b2009-08-12 18:21:30 +02003686 status = hdsp_read(hdsp, HDSP_statusRegister);
3687 status2 = hdsp_read(hdsp, HDSP_status2Register);
3688
3689 snd_iprintf(buffer, "%s (Card #%d)\n", hdsp->card_name,
3690 hdsp->card->number + 1);
3691 snd_iprintf(buffer, "Buffers: capture %p playback %p\n",
3692 hdsp->capture_buffer, hdsp->playback_buffer);
3693 snd_iprintf(buffer, "IRQ: %d Registers bus: 0x%lx VM: 0x%lx\n",
3694 hdsp->irq, hdsp->port, (unsigned long)hdsp->iobase);
3695 snd_iprintf(buffer, "Control register: 0x%x\n", hdsp->control_register);
3696 snd_iprintf(buffer, "Control2 register: 0x%x\n",
3697 hdsp->control2_register);
3698 snd_iprintf(buffer, "Status register: 0x%x\n", status);
3699 snd_iprintf(buffer, "Status2 register: 0x%x\n", status2);
3700
3701 if (hdsp_check_for_iobox(hdsp)) {
3702 snd_iprintf(buffer, "No I/O box connected.\n"
3703 "Please connect one and upload firmware.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 return;
Tim Blechmannc18bc9b2009-08-12 18:21:30 +02003705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706
Takashi Iwaib0b981192005-10-20 18:29:58 +02003707 if (hdsp_check_for_firmware(hdsp, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 if (hdsp->state & HDSP_FirmwareCached) {
3709 if (snd_hdsp_load_firmware_from_cache(hdsp) != 0) {
Tim Blechmannc18bc9b2009-08-12 18:21:30 +02003710 snd_iprintf(buffer, "Firmware loading from "
3711 "cache failed, "
3712 "please upload manually.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 return;
3714 }
3715 } else {
Takashi Iwai311e70a2006-09-06 12:13:37 +02003716 int err = -EINVAL;
Takashi Iwai311e70a2006-09-06 12:13:37 +02003717 err = hdsp_request_fw_loader(hdsp);
Takashi Iwai311e70a2006-09-06 12:13:37 +02003718 if (err < 0) {
3719 snd_iprintf(buffer,
3720 "No firmware loaded nor cached, "
3721 "please upload firmware.\n");
3722 return;
3723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 }
3725 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003726
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 snd_iprintf(buffer, "FIFO status: %d\n", hdsp_read(hdsp, HDSP_fifoStatus) & 0xff);
3728 snd_iprintf(buffer, "MIDI1 Output status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusOut0));
3729 snd_iprintf(buffer, "MIDI1 Input status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusIn0));
3730 snd_iprintf(buffer, "MIDI2 Output status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusOut1));
3731 snd_iprintf(buffer, "MIDI2 Input status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusIn1));
3732 snd_iprintf(buffer, "Use Midi Tasklet: %s\n", hdsp->use_midi_tasklet ? "on" : "off");
3733
3734 snd_iprintf(buffer, "\n");
3735
3736 x = 1 << (6 + hdsp_decode_latency(hdsp->control_register & HDSP_LatencyMask));
3737
3738 snd_iprintf(buffer, "Buffer Size (Latency): %d samples (2 periods of %lu bytes)\n", x, (unsigned long) hdsp->period_bytes);
3739 snd_iprintf(buffer, "Hardware pointer (frames): %ld\n", hdsp_hw_pointer(hdsp));
3740 snd_iprintf(buffer, "Precise pointer: %s\n", hdsp->precise_ptr ? "on" : "off");
3741 snd_iprintf(buffer, "Line out: %s\n", (hdsp->control_register & HDSP_LineOut) ? "on" : "off");
3742
3743 snd_iprintf(buffer, "Firmware version: %d\n", (status2&HDSP_version0)|(status2&HDSP_version1)<<1|(status2&HDSP_version2)<<2);
3744
3745 snd_iprintf(buffer, "\n");
3746
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 switch (hdsp_clock_source(hdsp)) {
3748 case HDSP_CLOCK_SOURCE_AUTOSYNC:
3749 clock_source = "AutoSync";
3750 break;
3751 case HDSP_CLOCK_SOURCE_INTERNAL_32KHZ:
3752 clock_source = "Internal 32 kHz";
3753 break;
3754 case HDSP_CLOCK_SOURCE_INTERNAL_44_1KHZ:
3755 clock_source = "Internal 44.1 kHz";
3756 break;
3757 case HDSP_CLOCK_SOURCE_INTERNAL_48KHZ:
3758 clock_source = "Internal 48 kHz";
3759 break;
3760 case HDSP_CLOCK_SOURCE_INTERNAL_64KHZ:
3761 clock_source = "Internal 64 kHz";
3762 break;
3763 case HDSP_CLOCK_SOURCE_INTERNAL_88_2KHZ:
3764 clock_source = "Internal 88.2 kHz";
3765 break;
3766 case HDSP_CLOCK_SOURCE_INTERNAL_96KHZ:
3767 clock_source = "Internal 96 kHz";
3768 break;
3769 case HDSP_CLOCK_SOURCE_INTERNAL_128KHZ:
3770 clock_source = "Internal 128 kHz";
3771 break;
3772 case HDSP_CLOCK_SOURCE_INTERNAL_176_4KHZ:
3773 clock_source = "Internal 176.4 kHz";
3774 break;
3775 case HDSP_CLOCK_SOURCE_INTERNAL_192KHZ:
3776 clock_source = "Internal 192 kHz";
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003777 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 default:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003779 clock_source = "Error";
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 }
3781 snd_iprintf (buffer, "Sample Clock Source: %s\n", clock_source);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003782
Takashi Iwaib0b981192005-10-20 18:29:58 +02003783 if (hdsp_system_clock_mode(hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 system_clock_mode = "Slave";
Takashi Iwaib0b981192005-10-20 18:29:58 +02003785 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 system_clock_mode = "Master";
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003787
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 switch (hdsp_pref_sync_ref (hdsp)) {
3789 case HDSP_SYNC_FROM_WORD:
3790 pref_sync_ref = "Word Clock";
3791 break;
3792 case HDSP_SYNC_FROM_ADAT_SYNC:
3793 pref_sync_ref = "ADAT Sync";
3794 break;
3795 case HDSP_SYNC_FROM_SPDIF:
3796 pref_sync_ref = "SPDIF";
3797 break;
3798 case HDSP_SYNC_FROM_ADAT1:
3799 pref_sync_ref = "ADAT1";
3800 break;
3801 case HDSP_SYNC_FROM_ADAT2:
3802 pref_sync_ref = "ADAT2";
3803 break;
3804 case HDSP_SYNC_FROM_ADAT3:
3805 pref_sync_ref = "ADAT3";
3806 break;
3807 default:
3808 pref_sync_ref = "Word Clock";
3809 break;
3810 }
3811 snd_iprintf (buffer, "Preferred Sync Reference: %s\n", pref_sync_ref);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003812
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 switch (hdsp_autosync_ref (hdsp)) {
3814 case HDSP_AUTOSYNC_FROM_WORD:
3815 autosync_ref = "Word Clock";
3816 break;
3817 case HDSP_AUTOSYNC_FROM_ADAT_SYNC:
3818 autosync_ref = "ADAT Sync";
3819 break;
3820 case HDSP_AUTOSYNC_FROM_SPDIF:
3821 autosync_ref = "SPDIF";
3822 break;
3823 case HDSP_AUTOSYNC_FROM_NONE:
3824 autosync_ref = "None";
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003825 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 case HDSP_AUTOSYNC_FROM_ADAT1:
3827 autosync_ref = "ADAT1";
3828 break;
3829 case HDSP_AUTOSYNC_FROM_ADAT2:
3830 autosync_ref = "ADAT2";
3831 break;
3832 case HDSP_AUTOSYNC_FROM_ADAT3:
3833 autosync_ref = "ADAT3";
3834 break;
3835 default:
3836 autosync_ref = "---";
3837 break;
3838 }
3839 snd_iprintf (buffer, "AutoSync Reference: %s\n", autosync_ref);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003840
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841 snd_iprintf (buffer, "AutoSync Frequency: %d\n", hdsp_external_sample_rate(hdsp));
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003842
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 snd_iprintf (buffer, "System Clock Mode: %s\n", system_clock_mode);
3844
3845 snd_iprintf (buffer, "System Clock Frequency: %d\n", hdsp->system_sample_rate);
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02003846 snd_iprintf (buffer, "System Clock Locked: %s\n", hdsp->clock_source_locked ? "Yes" : "No");
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003847
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 snd_iprintf(buffer, "\n");
3849
Florian Faber28b26e12010-12-01 12:14:47 +01003850 if (hdsp->io_type != RPM) {
3851 switch (hdsp_spdif_in(hdsp)) {
3852 case HDSP_SPDIFIN_OPTICAL:
3853 snd_iprintf(buffer, "IEC958 input: Optical\n");
3854 break;
3855 case HDSP_SPDIFIN_COAXIAL:
3856 snd_iprintf(buffer, "IEC958 input: Coaxial\n");
3857 break;
3858 case HDSP_SPDIFIN_INTERNAL:
3859 snd_iprintf(buffer, "IEC958 input: Internal\n");
3860 break;
3861 case HDSP_SPDIFIN_AES:
3862 snd_iprintf(buffer, "IEC958 input: AES\n");
3863 break;
3864 default:
3865 snd_iprintf(buffer, "IEC958 input: ???\n");
3866 break;
3867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003869
Florian Faber28b26e12010-12-01 12:14:47 +01003870 if (RPM == hdsp->io_type) {
3871 if (hdsp->control_register & HDSP_RPM_Bypass)
3872 snd_iprintf(buffer, "RPM Bypass: disabled\n");
3873 else
3874 snd_iprintf(buffer, "RPM Bypass: enabled\n");
3875 if (hdsp->control_register & HDSP_RPM_Disconnect)
3876 snd_iprintf(buffer, "RPM disconnected\n");
3877 else
3878 snd_iprintf(buffer, "RPM connected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879
Florian Faber28b26e12010-12-01 12:14:47 +01003880 switch (hdsp->control_register & HDSP_RPM_Inp12) {
3881 case HDSP_RPM_Inp12_Phon_6dB:
3882 snd_iprintf(buffer, "Input 1/2: Phono, 6dB\n");
3883 break;
3884 case HDSP_RPM_Inp12_Phon_0dB:
3885 snd_iprintf(buffer, "Input 1/2: Phono, 0dB\n");
3886 break;
3887 case HDSP_RPM_Inp12_Phon_n6dB:
3888 snd_iprintf(buffer, "Input 1/2: Phono, -6dB\n");
3889 break;
3890 case HDSP_RPM_Inp12_Line_0dB:
3891 snd_iprintf(buffer, "Input 1/2: Line, 0dB\n");
3892 break;
3893 case HDSP_RPM_Inp12_Line_n6dB:
3894 snd_iprintf(buffer, "Input 1/2: Line, -6dB\n");
3895 break;
3896 default:
3897 snd_iprintf(buffer, "Input 1/2: ???\n");
3898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899
Florian Faber28b26e12010-12-01 12:14:47 +01003900 switch (hdsp->control_register & HDSP_RPM_Inp34) {
3901 case HDSP_RPM_Inp34_Phon_6dB:
3902 snd_iprintf(buffer, "Input 3/4: Phono, 6dB\n");
3903 break;
3904 case HDSP_RPM_Inp34_Phon_0dB:
3905 snd_iprintf(buffer, "Input 3/4: Phono, 0dB\n");
3906 break;
3907 case HDSP_RPM_Inp34_Phon_n6dB:
3908 snd_iprintf(buffer, "Input 3/4: Phono, -6dB\n");
3909 break;
3910 case HDSP_RPM_Inp34_Line_0dB:
3911 snd_iprintf(buffer, "Input 3/4: Line, 0dB\n");
3912 break;
3913 case HDSP_RPM_Inp34_Line_n6dB:
3914 snd_iprintf(buffer, "Input 3/4: Line, -6dB\n");
3915 break;
3916 default:
3917 snd_iprintf(buffer, "Input 3/4: ???\n");
3918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919
Florian Faber28b26e12010-12-01 12:14:47 +01003920 } else {
3921 if (hdsp->control_register & HDSP_SPDIFOpticalOut)
3922 snd_iprintf(buffer, "IEC958 output: Coaxial & ADAT1\n");
3923 else
3924 snd_iprintf(buffer, "IEC958 output: Coaxial only\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925
Florian Faber28b26e12010-12-01 12:14:47 +01003926 if (hdsp->control_register & HDSP_SPDIFProfessional)
3927 snd_iprintf(buffer, "IEC958 quality: Professional\n");
3928 else
3929 snd_iprintf(buffer, "IEC958 quality: Consumer\n");
3930
3931 if (hdsp->control_register & HDSP_SPDIFEmphasis)
3932 snd_iprintf(buffer, "IEC958 emphasis: on\n");
3933 else
3934 snd_iprintf(buffer, "IEC958 emphasis: off\n");
3935
3936 if (hdsp->control_register & HDSP_SPDIFNonAudio)
3937 snd_iprintf(buffer, "IEC958 NonAudio: on\n");
3938 else
3939 snd_iprintf(buffer, "IEC958 NonAudio: off\n");
3940 x = hdsp_spdif_sample_rate(hdsp);
3941 if (x != 0)
3942 snd_iprintf(buffer, "IEC958 sample rate: %d\n", x);
3943 else
3944 snd_iprintf(buffer, "IEC958 sample rate: Error flag set\n");
3945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 snd_iprintf(buffer, "\n");
3947
3948 /* Sync Check */
3949 x = status & HDSP_Sync0;
Takashi Iwaib0b981192005-10-20 18:29:58 +02003950 if (status & HDSP_Lock0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 snd_iprintf(buffer, "ADAT1: %s\n", x ? "Sync" : "Lock");
Takashi Iwaib0b981192005-10-20 18:29:58 +02003952 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 snd_iprintf(buffer, "ADAT1: No Lock\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954
3955 switch (hdsp->io_type) {
3956 case Digiface:
3957 case H9652:
3958 x = status & HDSP_Sync1;
Takashi Iwaib0b981192005-10-20 18:29:58 +02003959 if (status & HDSP_Lock1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960 snd_iprintf(buffer, "ADAT2: %s\n", x ? "Sync" : "Lock");
Takashi Iwaib0b981192005-10-20 18:29:58 +02003961 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 snd_iprintf(buffer, "ADAT2: No Lock\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 x = status & HDSP_Sync2;
Takashi Iwaib0b981192005-10-20 18:29:58 +02003964 if (status & HDSP_Lock2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 snd_iprintf(buffer, "ADAT3: %s\n", x ? "Sync" : "Lock");
Takashi Iwaib0b981192005-10-20 18:29:58 +02003966 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 snd_iprintf(buffer, "ADAT3: No Lock\n");
Takashi Iwaib0b981192005-10-20 18:29:58 +02003968 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 default:
3970 /* relax */
3971 break;
3972 }
3973
3974 x = status & HDSP_SPDIFSync;
Takashi Iwaib0b981192005-10-20 18:29:58 +02003975 if (status & HDSP_SPDIFErrorFlag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 snd_iprintf (buffer, "SPDIF: No Lock\n");
Takashi Iwaib0b981192005-10-20 18:29:58 +02003977 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 snd_iprintf (buffer, "SPDIF: %s\n", x ? "Sync" : "Lock");
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003979
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 x = status2 & HDSP_wc_sync;
Takashi Iwaib0b981192005-10-20 18:29:58 +02003981 if (status2 & HDSP_wc_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 snd_iprintf (buffer, "Word Clock: %s\n", x ? "Sync" : "Lock");
Takashi Iwaib0b981192005-10-20 18:29:58 +02003983 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 snd_iprintf (buffer, "Word Clock: No Lock\n");
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003985
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 x = status & HDSP_TimecodeSync;
Takashi Iwaib0b981192005-10-20 18:29:58 +02003987 if (status & HDSP_TimecodeLock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 snd_iprintf(buffer, "ADAT Sync: %s\n", x ? "Sync" : "Lock");
Takashi Iwaib0b981192005-10-20 18:29:58 +02003989 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 snd_iprintf(buffer, "ADAT Sync: No Lock\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991
3992 snd_iprintf(buffer, "\n");
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003993
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 /* Informations about H9632 specific controls */
3995 if (hdsp->io_type == H9632) {
3996 char *tmp;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003997
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 switch (hdsp_ad_gain(hdsp)) {
3999 case 0:
4000 tmp = "-10 dBV";
4001 break;
4002 case 1:
4003 tmp = "+4 dBu";
4004 break;
4005 default:
4006 tmp = "Lo Gain";
4007 break;
4008 }
4009 snd_iprintf(buffer, "AD Gain : %s\n", tmp);
4010
4011 switch (hdsp_da_gain(hdsp)) {
4012 case 0:
4013 tmp = "Hi Gain";
4014 break;
4015 case 1:
4016 tmp = "+4 dBu";
4017 break;
4018 default:
4019 tmp = "-10 dBV";
4020 break;
4021 }
4022 snd_iprintf(buffer, "DA Gain : %s\n", tmp);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004023
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 switch (hdsp_phone_gain(hdsp)) {
4025 case 0:
4026 tmp = "0 dB";
4027 break;
4028 case 1:
4029 tmp = "-6 dB";
4030 break;
4031 default:
4032 tmp = "-12 dB";
4033 break;
4034 }
4035 snd_iprintf(buffer, "Phones Gain : %s\n", tmp);
4036
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004037 snd_iprintf(buffer, "XLR Breakout Cable : %s\n", hdsp_xlr_breakout_cable(hdsp) ? "yes" : "no");
4038
Takashi Iwaib0b981192005-10-20 18:29:58 +02004039 if (hdsp->control_register & HDSP_AnalogExtensionBoard)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 snd_iprintf(buffer, "AEB : on (ADAT1 internal)\n");
Takashi Iwaib0b981192005-10-20 18:29:58 +02004041 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 snd_iprintf(buffer, "AEB : off (ADAT1 external)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 snd_iprintf(buffer, "\n");
4044 }
4045
4046}
4047
Randy Dunlap1374f8c2008-01-16 14:55:42 +01004048static void snd_hdsp_proc_init(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004050 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051
4052 if (! snd_card_proc_new(hdsp->card, "hdsp", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02004053 snd_info_set_text_ops(entry, hdsp, snd_hdsp_proc_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054}
4055
Takashi Iwai55e957d2005-11-17 14:52:13 +01004056static void snd_hdsp_free_buffers(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057{
4058 snd_hammerfall_free_buffer(&hdsp->capture_dma_buf, hdsp->pci);
4059 snd_hammerfall_free_buffer(&hdsp->playback_dma_buf, hdsp->pci);
4060}
4061
Bill Pembertone23e7a12012-12-06 12:35:10 -05004062static int snd_hdsp_initialize_memory(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063{
4064 unsigned long pb_bus, cb_bus;
4065
4066 if (snd_hammerfall_get_buffer(hdsp->pci, &hdsp->capture_dma_buf, HDSP_DMA_AREA_BYTES) < 0 ||
4067 snd_hammerfall_get_buffer(hdsp->pci, &hdsp->playback_dma_buf, HDSP_DMA_AREA_BYTES) < 0) {
4068 if (hdsp->capture_dma_buf.area)
4069 snd_dma_free_pages(&hdsp->capture_dma_buf);
4070 printk(KERN_ERR "%s: no buffers available\n", hdsp->card_name);
4071 return -ENOMEM;
4072 }
4073
4074 /* Align to bus-space 64K boundary */
4075
Clemens Ladisch7ab39922006-10-09 08:13:32 +02004076 cb_bus = ALIGN(hdsp->capture_dma_buf.addr, 0x10000ul);
4077 pb_bus = ALIGN(hdsp->playback_dma_buf.addr, 0x10000ul);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078
4079 /* Tell the card where it is */
4080
4081 hdsp_write(hdsp, HDSP_inputBufferAddress, cb_bus);
4082 hdsp_write(hdsp, HDSP_outputBufferAddress, pb_bus);
4083
4084 hdsp->capture_buffer = hdsp->capture_dma_buf.area + (cb_bus - hdsp->capture_dma_buf.addr);
4085 hdsp->playback_buffer = hdsp->playback_dma_buf.area + (pb_bus - hdsp->playback_dma_buf.addr);
4086
4087 return 0;
4088}
4089
Takashi Iwai55e957d2005-11-17 14:52:13 +01004090static int snd_hdsp_set_defaults(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091{
4092 unsigned int i;
4093
4094 /* ASSUMPTION: hdsp->lock is either held, or
4095 there is no need to hold it (e.g. during module
Joe Perches561de312007-12-18 13:13:47 +01004096 initialization).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097 */
4098
4099 /* set defaults:
4100
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004101 SPDIF Input via Coax
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 Master clock mode
4103 maximum latency (7 => 2^7 = 8192 samples, 64Kbyte buffer,
4104 which implies 2 4096 sample, 32Kbyte periods).
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004105 Enable line out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 */
4107
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004108 hdsp->control_register = HDSP_ClockModeMaster |
4109 HDSP_SPDIFInputCoaxial |
4110 hdsp_encode_latency(7) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 HDSP_LineOut;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004112
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113
4114 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
4115
4116#ifdef SNDRV_BIG_ENDIAN
4117 hdsp->control2_register = HDSP_BIGENDIAN_MODE;
4118#else
4119 hdsp->control2_register = 0;
4120#endif
Takashi Iwaib0b981192005-10-20 18:29:58 +02004121 if (hdsp->io_type == H9652)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 snd_hdsp_9652_enable_mixer (hdsp);
Takashi Iwaib0b981192005-10-20 18:29:58 +02004123 else
4124 hdsp_write (hdsp, HDSP_control2Reg, hdsp->control2_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125
4126 hdsp_reset_hw_pointer(hdsp);
4127 hdsp_compute_period_size(hdsp);
4128
4129 /* silence everything */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004130
Takashi Iwaib0b981192005-10-20 18:29:58 +02004131 for (i = 0; i < HDSP_MATRIX_MIXER_SIZE; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 hdsp->mixer_matrix[i] = MINUS_INFINITY_GAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133
4134 for (i = 0; i < ((hdsp->io_type == H9652 || hdsp->io_type == H9632) ? 1352 : HDSP_MATRIX_MIXER_SIZE); ++i) {
Takashi Iwaib0b981192005-10-20 18:29:58 +02004135 if (hdsp_write_gain (hdsp, i, MINUS_INFINITY_GAIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004138
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 /* H9632 specific defaults */
4140 if (hdsp->io_type == H9632) {
4141 hdsp->control_register |= (HDSP_DAGainPlus4dBu | HDSP_ADGainPlus4dBu | HDSP_PhoneGain0dB);
4142 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
4143 }
4144
4145 /* set a default rate so that the channel map is set up.
4146 */
4147
4148 hdsp_set_rate(hdsp, 48000, 1);
4149
4150 return 0;
4151}
4152
4153static void hdsp_midi_tasklet(unsigned long arg)
4154{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004155 struct hdsp *hdsp = (struct hdsp *)arg;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004156
Takashi Iwaib0b981192005-10-20 18:29:58 +02004157 if (hdsp->midi[0].pending)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 snd_hdsp_midi_input_read (&hdsp->midi[0]);
Takashi Iwaib0b981192005-10-20 18:29:58 +02004159 if (hdsp->midi[1].pending)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 snd_hdsp_midi_input_read (&hdsp->midi[1]);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004161}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162
David Howells7d12e782006-10-05 14:55:46 +01004163static irqreturn_t snd_hdsp_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004165 struct hdsp *hdsp = (struct hdsp *) dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 unsigned int status;
4167 int audio;
4168 int midi0;
4169 int midi1;
4170 unsigned int midi0status;
4171 unsigned int midi1status;
4172 int schedule = 0;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004173
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 status = hdsp_read(hdsp, HDSP_statusRegister);
4175
4176 audio = status & HDSP_audioIRQPending;
4177 midi0 = status & HDSP_midi0IRQPending;
4178 midi1 = status & HDSP_midi1IRQPending;
4179
Takashi Iwaib0b981192005-10-20 18:29:58 +02004180 if (!audio && !midi0 && !midi1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182
4183 hdsp_write(hdsp, HDSP_interruptConfirmation, 0);
4184
4185 midi0status = hdsp_read (hdsp, HDSP_midiStatusIn0) & 0xff;
4186 midi1status = hdsp_read (hdsp, HDSP_midiStatusIn1) & 0xff;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004187
Takashi Iwaic2503cd2009-03-05 09:37:40 +01004188 if (!(hdsp->state & HDSP_InitializationComplete))
4189 return IRQ_HANDLED;
4190
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 if (audio) {
Takashi Iwaib0b981192005-10-20 18:29:58 +02004192 if (hdsp->capture_substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 snd_pcm_period_elapsed(hdsp->pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004194
Takashi Iwaib0b981192005-10-20 18:29:58 +02004195 if (hdsp->playback_substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 snd_pcm_period_elapsed(hdsp->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004198
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 if (midi0 && midi0status) {
4200 if (hdsp->use_midi_tasklet) {
4201 /* we disable interrupts for this input until processing is done */
4202 hdsp->control_register &= ~HDSP_Midi0InterruptEnable;
4203 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
4204 hdsp->midi[0].pending = 1;
4205 schedule = 1;
4206 } else {
4207 snd_hdsp_midi_input_read (&hdsp->midi[0]);
4208 }
4209 }
Florian Faber28b26e12010-12-01 12:14:47 +01004210 if (hdsp->io_type != Multiface && hdsp->io_type != RPM && hdsp->io_type != H9632 && midi1 && midi1status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 if (hdsp->use_midi_tasklet) {
4212 /* we disable interrupts for this input until processing is done */
4213 hdsp->control_register &= ~HDSP_Midi1InterruptEnable;
4214 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
4215 hdsp->midi[1].pending = 1;
4216 schedule = 1;
4217 } else {
4218 snd_hdsp_midi_input_read (&hdsp->midi[1]);
4219 }
4220 }
4221 if (hdsp->use_midi_tasklet && schedule)
Takashi Iwai1f041282008-12-18 12:17:55 +01004222 tasklet_schedule(&hdsp->midi_tasklet);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 return IRQ_HANDLED;
4224}
4225
Takashi Iwai55e957d2005-11-17 14:52:13 +01004226static snd_pcm_uframes_t snd_hdsp_hw_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004228 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 return hdsp_hw_pointer(hdsp);
4230}
4231
Takashi Iwai55e957d2005-11-17 14:52:13 +01004232static char *hdsp_channel_buffer_location(struct hdsp *hdsp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 int stream,
4234 int channel)
4235
4236{
4237 int mapped_channel;
4238
Takashi Iwaida3cec32008-08-08 17:12:14 +02004239 if (snd_BUG_ON(channel < 0 || channel >= hdsp->max_channels))
4240 return NULL;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004241
Takashi Iwaib0b981192005-10-20 18:29:58 +02004242 if ((mapped_channel = hdsp->channel_map[channel]) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 return NULL;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004244
Takashi Iwaib0b981192005-10-20 18:29:58 +02004245 if (stream == SNDRV_PCM_STREAM_CAPTURE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 return hdsp->capture_buffer + (mapped_channel * HDSP_CHANNEL_BUFFER_BYTES);
Takashi Iwaib0b981192005-10-20 18:29:58 +02004247 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 return hdsp->playback_buffer + (mapped_channel * HDSP_CHANNEL_BUFFER_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249}
4250
Takashi Iwai55e957d2005-11-17 14:52:13 +01004251static int snd_hdsp_playback_copy(struct snd_pcm_substream *substream, int channel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 snd_pcm_uframes_t pos, void __user *src, snd_pcm_uframes_t count)
4253{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004254 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 char *channel_buf;
4256
Takashi Iwaida3cec32008-08-08 17:12:14 +02004257 if (snd_BUG_ON(pos + count > HDSP_CHANNEL_BUFFER_BYTES / 4))
4258 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259
4260 channel_buf = hdsp_channel_buffer_location (hdsp, substream->pstr->stream, channel);
Takashi Iwaida3cec32008-08-08 17:12:14 +02004261 if (snd_BUG_ON(!channel_buf))
4262 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 if (copy_from_user(channel_buf + pos * 4, src, count * 4))
4264 return -EFAULT;
4265 return count;
4266}
4267
Takashi Iwai55e957d2005-11-17 14:52:13 +01004268static int snd_hdsp_capture_copy(struct snd_pcm_substream *substream, int channel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 snd_pcm_uframes_t pos, void __user *dst, snd_pcm_uframes_t count)
4270{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004271 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 char *channel_buf;
4273
Takashi Iwaida3cec32008-08-08 17:12:14 +02004274 if (snd_BUG_ON(pos + count > HDSP_CHANNEL_BUFFER_BYTES / 4))
4275 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276
4277 channel_buf = hdsp_channel_buffer_location (hdsp, substream->pstr->stream, channel);
Takashi Iwaida3cec32008-08-08 17:12:14 +02004278 if (snd_BUG_ON(!channel_buf))
4279 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 if (copy_to_user(dst, channel_buf + pos * 4, count * 4))
4281 return -EFAULT;
4282 return count;
4283}
4284
Takashi Iwai55e957d2005-11-17 14:52:13 +01004285static int snd_hdsp_hw_silence(struct snd_pcm_substream *substream, int channel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 snd_pcm_uframes_t pos, snd_pcm_uframes_t count)
4287{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004288 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 char *channel_buf;
4290
4291 channel_buf = hdsp_channel_buffer_location (hdsp, substream->pstr->stream, channel);
Takashi Iwaida3cec32008-08-08 17:12:14 +02004292 if (snd_BUG_ON(!channel_buf))
4293 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 memset(channel_buf + pos * 4, 0, count * 4);
4295 return count;
4296}
4297
Takashi Iwai55e957d2005-11-17 14:52:13 +01004298static int snd_hdsp_reset(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004300 struct snd_pcm_runtime *runtime = substream->runtime;
4301 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
4302 struct snd_pcm_substream *other;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
4304 other = hdsp->capture_substream;
4305 else
4306 other = hdsp->playback_substream;
4307 if (hdsp->running)
4308 runtime->status->hw_ptr = hdsp_hw_pointer(hdsp);
4309 else
4310 runtime->status->hw_ptr = 0;
4311 if (other) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004312 struct snd_pcm_substream *s;
4313 struct snd_pcm_runtime *oruntime = other->runtime;
Takashi Iwaief991b92007-02-22 12:52:53 +01004314 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 if (s == other) {
4316 oruntime->status->hw_ptr = runtime->status->hw_ptr;
4317 break;
4318 }
4319 }
4320 }
4321 return 0;
4322}
4323
Takashi Iwai55e957d2005-11-17 14:52:13 +01004324static int snd_hdsp_hw_params(struct snd_pcm_substream *substream,
4325 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004327 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 int err;
4329 pid_t this_pid;
4330 pid_t other_pid;
4331
Takashi Iwaib0b981192005-10-20 18:29:58 +02004332 if (hdsp_check_for_iobox (hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334
Takashi Iwaib0b981192005-10-20 18:29:58 +02004335 if (hdsp_check_for_firmware(hdsp, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337
4338 spin_lock_irq(&hdsp->lock);
4339
4340 if (substream->pstr->stream == SNDRV_PCM_STREAM_PLAYBACK) {
4341 hdsp->control_register &= ~(HDSP_SPDIFProfessional | HDSP_SPDIFNonAudio | HDSP_SPDIFEmphasis);
4342 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register |= hdsp->creg_spdif_stream);
4343 this_pid = hdsp->playback_pid;
4344 other_pid = hdsp->capture_pid;
4345 } else {
4346 this_pid = hdsp->capture_pid;
4347 other_pid = hdsp->playback_pid;
4348 }
4349
4350 if ((other_pid > 0) && (this_pid != other_pid)) {
4351
4352 /* The other stream is open, and not by the same
4353 task as this one. Make sure that the parameters
4354 that matter are the same.
4355 */
4356
4357 if (params_rate(params) != hdsp->system_sample_rate) {
4358 spin_unlock_irq(&hdsp->lock);
4359 _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_RATE);
4360 return -EBUSY;
4361 }
4362
4363 if (params_period_size(params) != hdsp->period_bytes / 4) {
4364 spin_unlock_irq(&hdsp->lock);
4365 _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
4366 return -EBUSY;
4367 }
4368
4369 /* We're fine. */
4370
4371 spin_unlock_irq(&hdsp->lock);
4372 return 0;
4373
4374 } else {
4375 spin_unlock_irq(&hdsp->lock);
4376 }
4377
4378 /* how to make sure that the rate matches an externally-set one ?
4379 */
4380
4381 spin_lock_irq(&hdsp->lock);
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02004382 if (! hdsp->clock_source_locked) {
4383 if ((err = hdsp_set_rate(hdsp, params_rate(params), 0)) < 0) {
4384 spin_unlock_irq(&hdsp->lock);
4385 _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_RATE);
4386 return err;
4387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388 }
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02004389 spin_unlock_irq(&hdsp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390
4391 if ((err = hdsp_set_interrupt_interval(hdsp, params_period_size(params))) < 0) {
4392 _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
4393 return err;
4394 }
4395
4396 return 0;
4397}
4398
Takashi Iwai55e957d2005-11-17 14:52:13 +01004399static int snd_hdsp_channel_info(struct snd_pcm_substream *substream,
4400 struct snd_pcm_channel_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004402 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 int mapped_channel;
4404
Takashi Iwaida3cec32008-08-08 17:12:14 +02004405 if (snd_BUG_ON(info->channel >= hdsp->max_channels))
4406 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407
Takashi Iwaib0b981192005-10-20 18:29:58 +02004408 if ((mapped_channel = hdsp->channel_map[info->channel]) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410
4411 info->offset = mapped_channel * HDSP_CHANNEL_BUFFER_BYTES;
4412 info->first = 0;
4413 info->step = 32;
4414 return 0;
4415}
4416
Takashi Iwai55e957d2005-11-17 14:52:13 +01004417static int snd_hdsp_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 unsigned int cmd, void *arg)
4419{
4420 switch (cmd) {
4421 case SNDRV_PCM_IOCTL1_RESET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422 return snd_hdsp_reset(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
Takashi Iwaib0b981192005-10-20 18:29:58 +02004424 return snd_hdsp_channel_info(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 default:
4426 break;
4427 }
4428
4429 return snd_pcm_lib_ioctl(substream, cmd, arg);
4430}
4431
Takashi Iwai55e957d2005-11-17 14:52:13 +01004432static int snd_hdsp_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004434 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
4435 struct snd_pcm_substream *other;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 int running;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004437
Takashi Iwaib0b981192005-10-20 18:29:58 +02004438 if (hdsp_check_for_iobox (hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440
Takashi Iwai311e70a2006-09-06 12:13:37 +02004441 if (hdsp_check_for_firmware(hdsp, 0)) /* no auto-loading in trigger */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443
4444 spin_lock(&hdsp->lock);
4445 running = hdsp->running;
4446 switch (cmd) {
4447 case SNDRV_PCM_TRIGGER_START:
4448 running |= 1 << substream->stream;
4449 break;
4450 case SNDRV_PCM_TRIGGER_STOP:
4451 running &= ~(1 << substream->stream);
4452 break;
4453 default:
4454 snd_BUG();
4455 spin_unlock(&hdsp->lock);
4456 return -EINVAL;
4457 }
4458 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
4459 other = hdsp->capture_substream;
4460 else
4461 other = hdsp->playback_substream;
4462
4463 if (other) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004464 struct snd_pcm_substream *s;
Takashi Iwaief991b92007-02-22 12:52:53 +01004465 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 if (s == other) {
4467 snd_pcm_trigger_done(s, substream);
4468 if (cmd == SNDRV_PCM_TRIGGER_START)
4469 running |= 1 << s->stream;
4470 else
4471 running &= ~(1 << s->stream);
4472 goto _ok;
4473 }
4474 }
4475 if (cmd == SNDRV_PCM_TRIGGER_START) {
4476 if (!(running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) &&
4477 substream->stream == SNDRV_PCM_STREAM_CAPTURE)
4478 hdsp_silence_playback(hdsp);
4479 } else {
4480 if (running &&
4481 substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
4482 hdsp_silence_playback(hdsp);
4483 }
4484 } else {
4485 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
4486 hdsp_silence_playback(hdsp);
4487 }
4488 _ok:
4489 snd_pcm_trigger_done(substream, substream);
4490 if (!hdsp->running && running)
4491 hdsp_start_audio(hdsp);
4492 else if (hdsp->running && !running)
4493 hdsp_stop_audio(hdsp);
4494 hdsp->running = running;
4495 spin_unlock(&hdsp->lock);
4496
4497 return 0;
4498}
4499
Takashi Iwai55e957d2005-11-17 14:52:13 +01004500static int snd_hdsp_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004502 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503 int result = 0;
4504
Takashi Iwaib0b981192005-10-20 18:29:58 +02004505 if (hdsp_check_for_iobox (hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507
Takashi Iwaib0b981192005-10-20 18:29:58 +02004508 if (hdsp_check_for_firmware(hdsp, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510
4511 spin_lock_irq(&hdsp->lock);
4512 if (!hdsp->running)
4513 hdsp_reset_hw_pointer(hdsp);
4514 spin_unlock_irq(&hdsp->lock);
4515 return result;
4516}
4517
Takashi Iwai55e957d2005-11-17 14:52:13 +01004518static struct snd_pcm_hardware snd_hdsp_playback_subinfo =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519{
4520 .info = (SNDRV_PCM_INFO_MMAP |
4521 SNDRV_PCM_INFO_MMAP_VALID |
4522 SNDRV_PCM_INFO_NONINTERLEAVED |
4523 SNDRV_PCM_INFO_SYNC_START |
4524 SNDRV_PCM_INFO_DOUBLE),
4525#ifdef SNDRV_BIG_ENDIAN
4526 .formats = SNDRV_PCM_FMTBIT_S32_BE,
4527#else
4528 .formats = SNDRV_PCM_FMTBIT_S32_LE,
4529#endif
4530 .rates = (SNDRV_PCM_RATE_32000 |
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004531 SNDRV_PCM_RATE_44100 |
4532 SNDRV_PCM_RATE_48000 |
4533 SNDRV_PCM_RATE_64000 |
4534 SNDRV_PCM_RATE_88200 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 SNDRV_PCM_RATE_96000),
4536 .rate_min = 32000,
4537 .rate_max = 96000,
Florian Faber28b26e12010-12-01 12:14:47 +01004538 .channels_min = 6,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 .channels_max = HDSP_MAX_CHANNELS,
4540 .buffer_bytes_max = HDSP_CHANNEL_BUFFER_BYTES * HDSP_MAX_CHANNELS,
4541 .period_bytes_min = (64 * 4) * 10,
4542 .period_bytes_max = (8192 * 4) * HDSP_MAX_CHANNELS,
4543 .periods_min = 2,
4544 .periods_max = 2,
4545 .fifo_size = 0
4546};
4547
Takashi Iwai55e957d2005-11-17 14:52:13 +01004548static struct snd_pcm_hardware snd_hdsp_capture_subinfo =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549{
4550 .info = (SNDRV_PCM_INFO_MMAP |
4551 SNDRV_PCM_INFO_MMAP_VALID |
4552 SNDRV_PCM_INFO_NONINTERLEAVED |
4553 SNDRV_PCM_INFO_SYNC_START),
4554#ifdef SNDRV_BIG_ENDIAN
4555 .formats = SNDRV_PCM_FMTBIT_S32_BE,
4556#else
4557 .formats = SNDRV_PCM_FMTBIT_S32_LE,
4558#endif
4559 .rates = (SNDRV_PCM_RATE_32000 |
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004560 SNDRV_PCM_RATE_44100 |
4561 SNDRV_PCM_RATE_48000 |
4562 SNDRV_PCM_RATE_64000 |
4563 SNDRV_PCM_RATE_88200 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 SNDRV_PCM_RATE_96000),
4565 .rate_min = 32000,
4566 .rate_max = 96000,
Florian Faber28b26e12010-12-01 12:14:47 +01004567 .channels_min = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568 .channels_max = HDSP_MAX_CHANNELS,
4569 .buffer_bytes_max = HDSP_CHANNEL_BUFFER_BYTES * HDSP_MAX_CHANNELS,
4570 .period_bytes_min = (64 * 4) * 10,
4571 .period_bytes_max = (8192 * 4) * HDSP_MAX_CHANNELS,
4572 .periods_min = 2,
4573 .periods_max = 2,
4574 .fifo_size = 0
4575};
4576
4577static unsigned int hdsp_period_sizes[] = { 64, 128, 256, 512, 1024, 2048, 4096, 8192 };
4578
Takashi Iwai55e957d2005-11-17 14:52:13 +01004579static struct snd_pcm_hw_constraint_list hdsp_hw_constraints_period_sizes = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580 .count = ARRAY_SIZE(hdsp_period_sizes),
4581 .list = hdsp_period_sizes,
4582 .mask = 0
4583};
4584
4585static unsigned int hdsp_9632_sample_rates[] = { 32000, 44100, 48000, 64000, 88200, 96000, 128000, 176400, 192000 };
4586
Takashi Iwai55e957d2005-11-17 14:52:13 +01004587static struct snd_pcm_hw_constraint_list hdsp_hw_constraints_9632_sample_rates = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588 .count = ARRAY_SIZE(hdsp_9632_sample_rates),
4589 .list = hdsp_9632_sample_rates,
4590 .mask = 0
4591};
4592
Takashi Iwai55e957d2005-11-17 14:52:13 +01004593static int snd_hdsp_hw_rule_in_channels(struct snd_pcm_hw_params *params,
4594 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004596 struct hdsp *hdsp = rule->private;
4597 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598 if (hdsp->io_type == H9632) {
4599 unsigned int list[3];
4600 list[0] = hdsp->qs_in_channels;
4601 list[1] = hdsp->ds_in_channels;
4602 list[2] = hdsp->ss_in_channels;
4603 return snd_interval_list(c, 3, list, 0);
4604 } else {
4605 unsigned int list[2];
4606 list[0] = hdsp->ds_in_channels;
4607 list[1] = hdsp->ss_in_channels;
4608 return snd_interval_list(c, 2, list, 0);
4609 }
4610}
4611
Takashi Iwai55e957d2005-11-17 14:52:13 +01004612static int snd_hdsp_hw_rule_out_channels(struct snd_pcm_hw_params *params,
4613 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614{
4615 unsigned int list[3];
Takashi Iwai55e957d2005-11-17 14:52:13 +01004616 struct hdsp *hdsp = rule->private;
4617 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 if (hdsp->io_type == H9632) {
4619 list[0] = hdsp->qs_out_channels;
4620 list[1] = hdsp->ds_out_channels;
4621 list[2] = hdsp->ss_out_channels;
4622 return snd_interval_list(c, 3, list, 0);
4623 } else {
4624 list[0] = hdsp->ds_out_channels;
4625 list[1] = hdsp->ss_out_channels;
4626 }
4627 return snd_interval_list(c, 2, list, 0);
4628}
4629
Takashi Iwai55e957d2005-11-17 14:52:13 +01004630static int snd_hdsp_hw_rule_in_channels_rate(struct snd_pcm_hw_params *params,
4631 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004633 struct hdsp *hdsp = rule->private;
4634 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
4635 struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 if (r->min > 96000 && hdsp->io_type == H9632) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004637 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638 .min = hdsp->qs_in_channels,
4639 .max = hdsp->qs_in_channels,
4640 .integer = 1,
4641 };
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004642 return snd_interval_refine(c, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 } else if (r->min > 48000 && r->max <= 96000) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004644 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645 .min = hdsp->ds_in_channels,
4646 .max = hdsp->ds_in_channels,
4647 .integer = 1,
4648 };
4649 return snd_interval_refine(c, &t);
4650 } else if (r->max < 64000) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004651 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652 .min = hdsp->ss_in_channels,
4653 .max = hdsp->ss_in_channels,
4654 .integer = 1,
4655 };
4656 return snd_interval_refine(c, &t);
4657 }
4658 return 0;
4659}
4660
Takashi Iwai55e957d2005-11-17 14:52:13 +01004661static int snd_hdsp_hw_rule_out_channels_rate(struct snd_pcm_hw_params *params,
4662 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004664 struct hdsp *hdsp = rule->private;
4665 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
4666 struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667 if (r->min > 96000 && hdsp->io_type == H9632) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004668 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 .min = hdsp->qs_out_channels,
4670 .max = hdsp->qs_out_channels,
4671 .integer = 1,
4672 };
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004673 return snd_interval_refine(c, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 } else if (r->min > 48000 && r->max <= 96000) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004675 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676 .min = hdsp->ds_out_channels,
4677 .max = hdsp->ds_out_channels,
4678 .integer = 1,
4679 };
4680 return snd_interval_refine(c, &t);
4681 } else if (r->max < 64000) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004682 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683 .min = hdsp->ss_out_channels,
4684 .max = hdsp->ss_out_channels,
4685 .integer = 1,
4686 };
4687 return snd_interval_refine(c, &t);
4688 }
4689 return 0;
4690}
4691
Takashi Iwai55e957d2005-11-17 14:52:13 +01004692static int snd_hdsp_hw_rule_rate_out_channels(struct snd_pcm_hw_params *params,
4693 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004695 struct hdsp *hdsp = rule->private;
4696 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
4697 struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698 if (c->min >= hdsp->ss_out_channels) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004699 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 .min = 32000,
4701 .max = 48000,
4702 .integer = 1,
4703 };
4704 return snd_interval_refine(r, &t);
4705 } else if (c->max <= hdsp->qs_out_channels && hdsp->io_type == H9632) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004706 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 .min = 128000,
4708 .max = 192000,
4709 .integer = 1,
4710 };
4711 return snd_interval_refine(r, &t);
4712 } else if (c->max <= hdsp->ds_out_channels) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004713 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714 .min = 64000,
4715 .max = 96000,
4716 .integer = 1,
4717 };
4718 return snd_interval_refine(r, &t);
4719 }
4720 return 0;
4721}
4722
Takashi Iwai55e957d2005-11-17 14:52:13 +01004723static int snd_hdsp_hw_rule_rate_in_channels(struct snd_pcm_hw_params *params,
4724 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004726 struct hdsp *hdsp = rule->private;
4727 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
4728 struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 if (c->min >= hdsp->ss_in_channels) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004730 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731 .min = 32000,
4732 .max = 48000,
4733 .integer = 1,
4734 };
4735 return snd_interval_refine(r, &t);
4736 } else if (c->max <= hdsp->qs_in_channels && hdsp->io_type == H9632) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004737 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738 .min = 128000,
4739 .max = 192000,
4740 .integer = 1,
4741 };
4742 return snd_interval_refine(r, &t);
4743 } else if (c->max <= hdsp->ds_in_channels) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004744 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745 .min = 64000,
4746 .max = 96000,
4747 .integer = 1,
4748 };
4749 return snd_interval_refine(r, &t);
4750 }
4751 return 0;
4752}
4753
Takashi Iwai55e957d2005-11-17 14:52:13 +01004754static int snd_hdsp_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004756 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
4757 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758
Takashi Iwaib0b981192005-10-20 18:29:58 +02004759 if (hdsp_check_for_iobox (hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761
Takashi Iwaib0b981192005-10-20 18:29:58 +02004762 if (hdsp_check_for_firmware(hdsp, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764
4765 spin_lock_irq(&hdsp->lock);
4766
4767 snd_pcm_set_sync(substream);
4768
4769 runtime->hw = snd_hdsp_playback_subinfo;
4770 runtime->dma_area = hdsp->playback_buffer;
4771 runtime->dma_bytes = HDSP_DMA_AREA_BYTES;
4772
4773 hdsp->playback_pid = current->pid;
4774 hdsp->playback_substream = substream;
4775
4776 spin_unlock_irq(&hdsp->lock);
4777
4778 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
4779 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 +02004780 if (hdsp->clock_source_locked) {
4781 runtime->hw.rate_min = runtime->hw.rate_max = hdsp->system_sample_rate;
4782 } else if (hdsp->io_type == H9632) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 runtime->hw.rate_max = 192000;
4784 runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
4785 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hdsp_hw_constraints_9632_sample_rates);
4786 }
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02004787 if (hdsp->io_type == H9632) {
4788 runtime->hw.channels_min = hdsp->qs_out_channels;
4789 runtime->hw.channels_max = hdsp->ss_out_channels;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004790 }
4791
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
4793 snd_hdsp_hw_rule_out_channels, hdsp,
4794 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
4795 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
4796 snd_hdsp_hw_rule_out_channels_rate, hdsp,
4797 SNDRV_PCM_HW_PARAM_RATE, -1);
4798 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
4799 snd_hdsp_hw_rule_rate_out_channels, hdsp,
4800 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
4801
Florian Faber28b26e12010-12-01 12:14:47 +01004802 if (RPM != hdsp->io_type) {
4803 hdsp->creg_spdif_stream = hdsp->creg_spdif;
4804 hdsp->spdif_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
4805 snd_ctl_notify(hdsp->card, SNDRV_CTL_EVENT_MASK_VALUE |
4806 SNDRV_CTL_EVENT_MASK_INFO, &hdsp->spdif_ctl->id);
4807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808 return 0;
4809}
4810
Takashi Iwai55e957d2005-11-17 14:52:13 +01004811static int snd_hdsp_playback_release(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004813 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814
4815 spin_lock_irq(&hdsp->lock);
4816
4817 hdsp->playback_pid = -1;
4818 hdsp->playback_substream = NULL;
4819
4820 spin_unlock_irq(&hdsp->lock);
4821
Florian Faber28b26e12010-12-01 12:14:47 +01004822 if (RPM != hdsp->io_type) {
4823 hdsp->spdif_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
4824 snd_ctl_notify(hdsp->card, SNDRV_CTL_EVENT_MASK_VALUE |
4825 SNDRV_CTL_EVENT_MASK_INFO, &hdsp->spdif_ctl->id);
4826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827 return 0;
4828}
4829
4830
Takashi Iwai55e957d2005-11-17 14:52:13 +01004831static int snd_hdsp_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004833 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
4834 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835
Takashi Iwaib0b981192005-10-20 18:29:58 +02004836 if (hdsp_check_for_iobox (hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838
Takashi Iwaib0b981192005-10-20 18:29:58 +02004839 if (hdsp_check_for_firmware(hdsp, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841
4842 spin_lock_irq(&hdsp->lock);
4843
4844 snd_pcm_set_sync(substream);
4845
4846 runtime->hw = snd_hdsp_capture_subinfo;
4847 runtime->dma_area = hdsp->capture_buffer;
4848 runtime->dma_bytes = HDSP_DMA_AREA_BYTES;
4849
4850 hdsp->capture_pid = current->pid;
4851 hdsp->capture_substream = substream;
4852
4853 spin_unlock_irq(&hdsp->lock);
4854
4855 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
4856 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, &hdsp_hw_constraints_period_sizes);
4857 if (hdsp->io_type == H9632) {
4858 runtime->hw.channels_min = hdsp->qs_in_channels;
4859 runtime->hw.channels_max = hdsp->ss_in_channels;
4860 runtime->hw.rate_max = 192000;
4861 runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
4862 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hdsp_hw_constraints_9632_sample_rates);
4863 }
4864 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
4865 snd_hdsp_hw_rule_in_channels, hdsp,
4866 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
4867 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
4868 snd_hdsp_hw_rule_in_channels_rate, hdsp,
4869 SNDRV_PCM_HW_PARAM_RATE, -1);
4870 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
4871 snd_hdsp_hw_rule_rate_in_channels, hdsp,
4872 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
4873 return 0;
4874}
4875
Takashi Iwai55e957d2005-11-17 14:52:13 +01004876static int snd_hdsp_capture_release(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004878 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879
4880 spin_lock_irq(&hdsp->lock);
4881
4882 hdsp->capture_pid = -1;
4883 hdsp->capture_substream = NULL;
4884
4885 spin_unlock_irq(&hdsp->lock);
4886 return 0;
4887}
4888
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889/* helper functions for copying meter values */
4890static inline int copy_u32_le(void __user *dest, void __iomem *src)
4891{
4892 u32 val = readl(src);
4893 return copy_to_user(dest, &val, 4);
4894}
4895
4896static inline int copy_u64_le(void __user *dest, void __iomem *src_low, void __iomem *src_high)
4897{
4898 u32 rms_low, rms_high;
4899 u64 rms;
4900 rms_low = readl(src_low);
4901 rms_high = readl(src_high);
4902 rms = ((u64)rms_high << 32) | rms_low;
4903 return copy_to_user(dest, &rms, 8);
4904}
4905
4906static inline int copy_u48_le(void __user *dest, void __iomem *src_low, void __iomem *src_high)
4907{
4908 u32 rms_low, rms_high;
4909 u64 rms;
4910 rms_low = readl(src_low) & 0xffffff00;
4911 rms_high = readl(src_high) & 0xffffff00;
4912 rms = ((u64)rms_high << 32) | rms_low;
4913 return copy_to_user(dest, &rms, 8);
4914}
4915
Takashi Iwai55e957d2005-11-17 14:52:13 +01004916static int hdsp_9652_get_peak(struct hdsp *hdsp, struct hdsp_peak_rms __user *peak_rms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917{
4918 int doublespeed = 0;
4919 int i, j, channels, ofs;
4920
4921 if (hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DoubleSpeedStatus)
4922 doublespeed = 1;
4923 channels = doublespeed ? 14 : 26;
4924 for (i = 0, j = 0; i < 26; ++i) {
4925 if (doublespeed && (i & 4))
4926 continue;
4927 ofs = HDSP_9652_peakBase - j * 4;
4928 if (copy_u32_le(&peak_rms->input_peaks[i], hdsp->iobase + ofs))
4929 return -EFAULT;
4930 ofs -= channels * 4;
4931 if (copy_u32_le(&peak_rms->playback_peaks[i], hdsp->iobase + ofs))
4932 return -EFAULT;
4933 ofs -= channels * 4;
4934 if (copy_u32_le(&peak_rms->output_peaks[i], hdsp->iobase + ofs))
4935 return -EFAULT;
4936 ofs = HDSP_9652_rmsBase + j * 8;
4937 if (copy_u48_le(&peak_rms->input_rms[i], hdsp->iobase + ofs,
4938 hdsp->iobase + ofs + 4))
4939 return -EFAULT;
4940 ofs += channels * 8;
4941 if (copy_u48_le(&peak_rms->playback_rms[i], hdsp->iobase + ofs,
4942 hdsp->iobase + ofs + 4))
4943 return -EFAULT;
4944 ofs += channels * 8;
4945 if (copy_u48_le(&peak_rms->output_rms[i], hdsp->iobase + ofs,
4946 hdsp->iobase + ofs + 4))
4947 return -EFAULT;
4948 j++;
4949 }
4950 return 0;
4951}
4952
Takashi Iwai55e957d2005-11-17 14:52:13 +01004953static int hdsp_9632_get_peak(struct hdsp *hdsp, struct hdsp_peak_rms __user *peak_rms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954{
4955 int i, j;
Takashi Iwai55e957d2005-11-17 14:52:13 +01004956 struct hdsp_9632_meters __iomem *m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957 int doublespeed = 0;
4958
4959 if (hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DoubleSpeedStatus)
4960 doublespeed = 1;
Takashi Iwai55e957d2005-11-17 14:52:13 +01004961 m = (struct hdsp_9632_meters __iomem *)(hdsp->iobase+HDSP_9632_metersBase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962 for (i = 0, j = 0; i < 16; ++i, ++j) {
4963 if (copy_u32_le(&peak_rms->input_peaks[i], &m->input_peak[j]))
4964 return -EFAULT;
4965 if (copy_u32_le(&peak_rms->playback_peaks[i], &m->playback_peak[j]))
4966 return -EFAULT;
4967 if (copy_u32_le(&peak_rms->output_peaks[i], &m->output_peak[j]))
4968 return -EFAULT;
4969 if (copy_u64_le(&peak_rms->input_rms[i], &m->input_rms_low[j],
4970 &m->input_rms_high[j]))
4971 return -EFAULT;
4972 if (copy_u64_le(&peak_rms->playback_rms[i], &m->playback_rms_low[j],
4973 &m->playback_rms_high[j]))
4974 return -EFAULT;
4975 if (copy_u64_le(&peak_rms->output_rms[i], &m->output_rms_low[j],
4976 &m->output_rms_high[j]))
4977 return -EFAULT;
4978 if (doublespeed && i == 3) i += 4;
4979 }
4980 return 0;
4981}
4982
Takashi Iwai55e957d2005-11-17 14:52:13 +01004983static int hdsp_get_peak(struct hdsp *hdsp, struct hdsp_peak_rms __user *peak_rms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984{
4985 int i;
4986
4987 for (i = 0; i < 26; i++) {
4988 if (copy_u32_le(&peak_rms->playback_peaks[i],
4989 hdsp->iobase + HDSP_playbackPeakLevel + i * 4))
4990 return -EFAULT;
4991 if (copy_u32_le(&peak_rms->input_peaks[i],
4992 hdsp->iobase + HDSP_inputPeakLevel + i * 4))
4993 return -EFAULT;
4994 }
4995 for (i = 0; i < 28; i++) {
4996 if (copy_u32_le(&peak_rms->output_peaks[i],
4997 hdsp->iobase + HDSP_outputPeakLevel + i * 4))
4998 return -EFAULT;
4999 }
5000 for (i = 0; i < 26; ++i) {
5001 if (copy_u64_le(&peak_rms->playback_rms[i],
5002 hdsp->iobase + HDSP_playbackRmsLevel + i * 8 + 4,
5003 hdsp->iobase + HDSP_playbackRmsLevel + i * 8))
5004 return -EFAULT;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005005 if (copy_u64_le(&peak_rms->input_rms[i],
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006 hdsp->iobase + HDSP_inputRmsLevel + i * 8 + 4,
5007 hdsp->iobase + HDSP_inputRmsLevel + i * 8))
5008 return -EFAULT;
5009 }
5010 return 0;
5011}
5012
Takashi Iwai55e957d2005-11-17 14:52:13 +01005013static 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 -07005014{
Joe Perches9fe856e2010-09-04 18:52:54 -07005015 struct hdsp *hdsp = hw->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016 void __user *argp = (void __user *)arg;
Tim Blechmann3ae7e2e2008-11-08 14:42:18 +01005017 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018
5019 switch (cmd) {
5020 case SNDRV_HDSP_IOCTL_GET_PEAK_RMS: {
Takashi Iwai55e957d2005-11-17 14:52:13 +01005021 struct hdsp_peak_rms __user *peak_rms = (struct hdsp_peak_rms __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022
Tim Blechmann3ae7e2e2008-11-08 14:42:18 +01005023 err = hdsp_check_for_iobox(hdsp);
5024 if (err < 0)
5025 return err;
5026
5027 err = hdsp_check_for_firmware(hdsp, 1);
5028 if (err < 0)
5029 return err;
5030
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031 if (!(hdsp->state & HDSP_FirmwareLoaded)) {
5032 snd_printk(KERN_ERR "Hammerfall-DSP: firmware needs to be uploaded to the card.\n");
5033 return -EINVAL;
5034 }
5035
5036 switch (hdsp->io_type) {
5037 case H9652:
5038 return hdsp_9652_get_peak(hdsp, peak_rms);
5039 case H9632:
5040 return hdsp_9632_get_peak(hdsp, peak_rms);
5041 default:
5042 return hdsp_get_peak(hdsp, peak_rms);
5043 }
5044 }
5045 case SNDRV_HDSP_IOCTL_GET_CONFIG_INFO: {
Takashi Iwai55e957d2005-11-17 14:52:13 +01005046 struct hdsp_config_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047 unsigned long flags;
5048 int i;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005049
Tim Blechmann3ae7e2e2008-11-08 14:42:18 +01005050 err = hdsp_check_for_iobox(hdsp);
5051 if (err < 0)
5052 return err;
5053
5054 err = hdsp_check_for_firmware(hdsp, 1);
5055 if (err < 0)
5056 return err;
5057
Dan Rosenberge68d3b32010-09-25 11:07:27 -04005058 memset(&info, 0, sizeof(info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059 spin_lock_irqsave(&hdsp->lock, flags);
5060 info.pref_sync_ref = (unsigned char)hdsp_pref_sync_ref(hdsp);
5061 info.wordclock_sync_check = (unsigned char)hdsp_wc_sync_check(hdsp);
Takashi Iwaib0b981192005-10-20 18:29:58 +02005062 if (hdsp->io_type != H9632)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063 info.adatsync_sync_check = (unsigned char)hdsp_adatsync_sync_check(hdsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064 info.spdif_sync_check = (unsigned char)hdsp_spdif_sync_check(hdsp);
Florian Faber28b26e12010-12-01 12:14:47 +01005065 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 -07005066 info.adat_sync_check[i] = (unsigned char)hdsp_adat_sync_check(hdsp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067 info.spdif_in = (unsigned char)hdsp_spdif_in(hdsp);
5068 info.spdif_out = (unsigned char)hdsp_spdif_out(hdsp);
5069 info.spdif_professional = (unsigned char)hdsp_spdif_professional(hdsp);
5070 info.spdif_emphasis = (unsigned char)hdsp_spdif_emphasis(hdsp);
5071 info.spdif_nonaudio = (unsigned char)hdsp_spdif_nonaudio(hdsp);
5072 info.spdif_sample_rate = hdsp_spdif_sample_rate(hdsp);
5073 info.system_sample_rate = hdsp->system_sample_rate;
5074 info.autosync_sample_rate = hdsp_external_sample_rate(hdsp);
5075 info.system_clock_mode = (unsigned char)hdsp_system_clock_mode(hdsp);
5076 info.clock_source = (unsigned char)hdsp_clock_source(hdsp);
5077 info.autosync_ref = (unsigned char)hdsp_autosync_ref(hdsp);
5078 info.line_out = (unsigned char)hdsp_line_out(hdsp);
5079 if (hdsp->io_type == H9632) {
5080 info.da_gain = (unsigned char)hdsp_da_gain(hdsp);
5081 info.ad_gain = (unsigned char)hdsp_ad_gain(hdsp);
5082 info.phone_gain = (unsigned char)hdsp_phone_gain(hdsp);
5083 info.xlr_breakout_cable = (unsigned char)hdsp_xlr_breakout_cable(hdsp);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005084
Florian Faber28b26e12010-12-01 12:14:47 +01005085 } else if (hdsp->io_type == RPM) {
5086 info.da_gain = (unsigned char) hdsp_rpm_input12(hdsp);
5087 info.ad_gain = (unsigned char) hdsp_rpm_input34(hdsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088 }
Takashi Iwaib0b981192005-10-20 18:29:58 +02005089 if (hdsp->io_type == H9632 || hdsp->io_type == H9652)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090 info.analog_extension_board = (unsigned char)hdsp_aeb(hdsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091 spin_unlock_irqrestore(&hdsp->lock, flags);
5092 if (copy_to_user(argp, &info, sizeof(info)))
5093 return -EFAULT;
5094 break;
5095 }
5096 case SNDRV_HDSP_IOCTL_GET_9632_AEB: {
Takashi Iwai55e957d2005-11-17 14:52:13 +01005097 struct hdsp_9632_aeb h9632_aeb;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005098
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099 if (hdsp->io_type != H9632) return -EINVAL;
5100 h9632_aeb.aebi = hdsp->ss_in_channels - H9632_SS_CHANNELS;
5101 h9632_aeb.aebo = hdsp->ss_out_channels - H9632_SS_CHANNELS;
5102 if (copy_to_user(argp, &h9632_aeb, sizeof(h9632_aeb)))
5103 return -EFAULT;
5104 break;
5105 }
5106 case SNDRV_HDSP_IOCTL_GET_VERSION: {
Takashi Iwai55e957d2005-11-17 14:52:13 +01005107 struct hdsp_version hdsp_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108 int err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005109
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110 if (hdsp->io_type == H9652 || hdsp->io_type == H9632) return -EINVAL;
5111 if (hdsp->io_type == Undefined) {
Takashi Iwaib0b981192005-10-20 18:29:58 +02005112 if ((err = hdsp_get_iobox_version(hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114 }
5115 hdsp_version.io_type = hdsp->io_type;
5116 hdsp_version.firmware_rev = hdsp->firmware_rev;
Takashi Iwaib0b981192005-10-20 18:29:58 +02005117 if ((err = copy_to_user(argp, &hdsp_version, sizeof(hdsp_version))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119 break;
5120 }
5121 case SNDRV_HDSP_IOCTL_UPLOAD_FIRMWARE: {
Takashi Iwai55e957d2005-11-17 14:52:13 +01005122 struct hdsp_firmware __user *firmware;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 u32 __user *firmware_data;
5124 int err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005125
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126 if (hdsp->io_type == H9652 || hdsp->io_type == H9632) return -EINVAL;
5127 /* SNDRV_HDSP_IOCTL_GET_VERSION must have been called */
5128 if (hdsp->io_type == Undefined) return -EINVAL;
5129
5130 if (hdsp->state & (HDSP_FirmwareCached | HDSP_FirmwareLoaded))
5131 return -EBUSY;
5132
Takashi Iwaib0b981192005-10-20 18:29:58 +02005133 snd_printk(KERN_INFO "Hammerfall-DSP: initializing firmware upload\n");
Takashi Iwai55e957d2005-11-17 14:52:13 +01005134 firmware = (struct hdsp_firmware __user *)argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135
Takashi Iwaib0b981192005-10-20 18:29:58 +02005136 if (get_user(firmware_data, &firmware->firmware_data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137 return -EFAULT;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005138
Takashi Iwaib0b981192005-10-20 18:29:58 +02005139 if (hdsp_check_for_iobox (hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141
Takashi Iwai90caaef2012-11-22 16:55:11 +01005142 if (!hdsp->fw_uploaded) {
5143 hdsp->fw_uploaded = vmalloc(HDSP_FIRMWARE_SIZE);
5144 if (!hdsp->fw_uploaded)
5145 return -ENOMEM;
5146 }
5147
5148 if (copy_from_user(hdsp->fw_uploaded, firmware_data,
5149 HDSP_FIRMWARE_SIZE)) {
5150 vfree(hdsp->fw_uploaded);
5151 hdsp->fw_uploaded = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152 return -EFAULT;
Takashi Iwai90caaef2012-11-22 16:55:11 +01005153 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005154
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155 hdsp->state |= HDSP_FirmwareCached;
5156
Takashi Iwaib0b981192005-10-20 18:29:58 +02005157 if ((err = snd_hdsp_load_firmware_from_cache(hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158 return err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005159
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160 if (!(hdsp->state & HDSP_InitializationComplete)) {
Takashi Iwaib0b981192005-10-20 18:29:58 +02005161 if ((err = snd_hdsp_enable_io(hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 return err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005163
5164 snd_hdsp_initialize_channels(hdsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165 snd_hdsp_initialize_midi_flush(hdsp);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005166
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167 if ((err = snd_hdsp_create_alsa_devices(hdsp->card, hdsp)) < 0) {
Takashi Iwaib0b981192005-10-20 18:29:58 +02005168 snd_printk(KERN_ERR "Hammerfall-DSP: error creating alsa devices\n");
5169 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170 }
5171 }
5172 break;
5173 }
5174 case SNDRV_HDSP_IOCTL_GET_MIXER: {
Takashi Iwai55e957d2005-11-17 14:52:13 +01005175 struct hdsp_mixer __user *mixer = (struct hdsp_mixer __user *)argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176 if (copy_to_user(mixer->matrix, hdsp->mixer_matrix, sizeof(unsigned short)*HDSP_MATRIX_MIXER_SIZE))
5177 return -EFAULT;
5178 break;
5179 }
5180 default:
5181 return -EINVAL;
5182 }
5183 return 0;
5184}
5185
Takashi Iwai55e957d2005-11-17 14:52:13 +01005186static struct snd_pcm_ops snd_hdsp_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187 .open = snd_hdsp_playback_open,
5188 .close = snd_hdsp_playback_release,
5189 .ioctl = snd_hdsp_ioctl,
5190 .hw_params = snd_hdsp_hw_params,
5191 .prepare = snd_hdsp_prepare,
5192 .trigger = snd_hdsp_trigger,
5193 .pointer = snd_hdsp_hw_pointer,
5194 .copy = snd_hdsp_playback_copy,
5195 .silence = snd_hdsp_hw_silence,
5196};
5197
Takashi Iwai55e957d2005-11-17 14:52:13 +01005198static struct snd_pcm_ops snd_hdsp_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199 .open = snd_hdsp_capture_open,
5200 .close = snd_hdsp_capture_release,
5201 .ioctl = snd_hdsp_ioctl,
5202 .hw_params = snd_hdsp_hw_params,
5203 .prepare = snd_hdsp_prepare,
5204 .trigger = snd_hdsp_trigger,
5205 .pointer = snd_hdsp_hw_pointer,
5206 .copy = snd_hdsp_capture_copy,
5207};
5208
Takashi Iwai92eed662008-02-22 18:35:56 +01005209static int snd_hdsp_create_hwdep(struct snd_card *card, struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210{
Takashi Iwai55e957d2005-11-17 14:52:13 +01005211 struct snd_hwdep *hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005212 int err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005213
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214 if ((err = snd_hwdep_new(card, "HDSP hwdep", 0, &hw)) < 0)
5215 return err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005216
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217 hdsp->hwdep = hw;
5218 hw->private_data = hdsp;
5219 strcpy(hw->name, "HDSP hwdep interface");
5220
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221 hw->ops.ioctl = snd_hdsp_hwdep_ioctl;
Andre Schramm42eb9232012-05-07 18:52:51 +02005222 hw->ops.ioctl_compat = snd_hdsp_hwdep_ioctl;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005223
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224 return 0;
5225}
5226
Takashi Iwai55e957d2005-11-17 14:52:13 +01005227static int snd_hdsp_create_pcm(struct snd_card *card, struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228{
Takashi Iwai55e957d2005-11-17 14:52:13 +01005229 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230 int err;
5231
5232 if ((err = snd_pcm_new(card, hdsp->card_name, 0, 1, 1, &pcm)) < 0)
5233 return err;
5234
5235 hdsp->pcm = pcm;
5236 pcm->private_data = hdsp;
5237 strcpy(pcm->name, hdsp->card_name);
5238
5239 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_hdsp_playback_ops);
5240 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_hdsp_capture_ops);
5241
5242 pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
5243
5244 return 0;
5245}
5246
Takashi Iwai55e957d2005-11-17 14:52:13 +01005247static void snd_hdsp_9652_enable_mixer (struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248{
5249 hdsp->control2_register |= HDSP_9652_ENABLE_MIXER;
5250 hdsp_write (hdsp, HDSP_control2Reg, hdsp->control2_register);
5251}
5252
Takashi Iwai55e957d2005-11-17 14:52:13 +01005253static int snd_hdsp_enable_io (struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254{
5255 int i;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005256
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257 if (hdsp_fifo_wait (hdsp, 0, 100)) {
Takashi Iwaib0b981192005-10-20 18:29:58 +02005258 snd_printk(KERN_ERR "Hammerfall-DSP: enable_io fifo_wait failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259 return -EIO;
5260 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005261
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262 for (i = 0; i < hdsp->max_channels; ++i) {
5263 hdsp_write (hdsp, HDSP_inputEnable + (4 * i), 1);
5264 hdsp_write (hdsp, HDSP_outputEnable + (4 * i), 1);
5265 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005266
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267 return 0;
5268}
5269
Takashi Iwai55e957d2005-11-17 14:52:13 +01005270static void snd_hdsp_initialize_channels(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271{
5272 int status, aebi_channels, aebo_channels;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005273
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274 switch (hdsp->io_type) {
5275 case Digiface:
5276 hdsp->card_name = "RME Hammerfall DSP + Digiface";
5277 hdsp->ss_in_channels = hdsp->ss_out_channels = DIGIFACE_SS_CHANNELS;
5278 hdsp->ds_in_channels = hdsp->ds_out_channels = DIGIFACE_DS_CHANNELS;
5279 break;
5280
5281 case H9652:
5282 hdsp->card_name = "RME Hammerfall HDSP 9652";
5283 hdsp->ss_in_channels = hdsp->ss_out_channels = H9652_SS_CHANNELS;
5284 hdsp->ds_in_channels = hdsp->ds_out_channels = H9652_DS_CHANNELS;
5285 break;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005286
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287 case H9632:
5288 status = hdsp_read(hdsp, HDSP_statusRegister);
5289 /* HDSP_AEBx bits are low when AEB are connected */
5290 aebi_channels = (status & HDSP_AEBI) ? 0 : 4;
5291 aebo_channels = (status & HDSP_AEBO) ? 0 : 4;
5292 hdsp->card_name = "RME Hammerfall HDSP 9632";
5293 hdsp->ss_in_channels = H9632_SS_CHANNELS+aebi_channels;
5294 hdsp->ds_in_channels = H9632_DS_CHANNELS+aebi_channels;
5295 hdsp->qs_in_channels = H9632_QS_CHANNELS+aebi_channels;
5296 hdsp->ss_out_channels = H9632_SS_CHANNELS+aebo_channels;
5297 hdsp->ds_out_channels = H9632_DS_CHANNELS+aebo_channels;
5298 hdsp->qs_out_channels = H9632_QS_CHANNELS+aebo_channels;
5299 break;
5300
5301 case Multiface:
5302 hdsp->card_name = "RME Hammerfall DSP + Multiface";
5303 hdsp->ss_in_channels = hdsp->ss_out_channels = MULTIFACE_SS_CHANNELS;
5304 hdsp->ds_in_channels = hdsp->ds_out_channels = MULTIFACE_DS_CHANNELS;
5305 break;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005306
Florian Faber28b26e12010-12-01 12:14:47 +01005307 case RPM:
5308 hdsp->card_name = "RME Hammerfall DSP + RPM";
5309 hdsp->ss_in_channels = RPM_CHANNELS-1;
5310 hdsp->ss_out_channels = RPM_CHANNELS;
5311 hdsp->ds_in_channels = RPM_CHANNELS-1;
5312 hdsp->ds_out_channels = RPM_CHANNELS;
5313 break;
5314
Linus Torvalds1da177e2005-04-16 15:20:36 -07005315 default:
5316 /* should never get here */
5317 break;
5318 }
5319}
5320
Takashi Iwai55e957d2005-11-17 14:52:13 +01005321static void snd_hdsp_initialize_midi_flush (struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322{
5323 snd_hdsp_flush_midi_input (hdsp, 0);
5324 snd_hdsp_flush_midi_input (hdsp, 1);
5325}
5326
Takashi Iwai55e957d2005-11-17 14:52:13 +01005327static int snd_hdsp_create_alsa_devices(struct snd_card *card, struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328{
5329 int err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005330
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331 if ((err = snd_hdsp_create_pcm(card, hdsp)) < 0) {
Takashi Iwaib0b981192005-10-20 18:29:58 +02005332 snd_printk(KERN_ERR "Hammerfall-DSP: Error creating pcm interface\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333 return err;
5334 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005335
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336
5337 if ((err = snd_hdsp_create_midi(card, hdsp, 0)) < 0) {
Takashi Iwaib0b981192005-10-20 18:29:58 +02005338 snd_printk(KERN_ERR "Hammerfall-DSP: Error creating first midi interface\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339 return err;
5340 }
5341
5342 if (hdsp->io_type == Digiface || hdsp->io_type == H9652) {
5343 if ((err = snd_hdsp_create_midi(card, hdsp, 1)) < 0) {
Takashi Iwaib0b981192005-10-20 18:29:58 +02005344 snd_printk(KERN_ERR "Hammerfall-DSP: Error creating second midi interface\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345 return err;
5346 }
5347 }
5348
5349 if ((err = snd_hdsp_create_controls(card, hdsp)) < 0) {
Takashi Iwaib0b981192005-10-20 18:29:58 +02005350 snd_printk(KERN_ERR "Hammerfall-DSP: Error creating ctl interface\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351 return err;
5352 }
5353
5354 snd_hdsp_proc_init(hdsp);
5355
5356 hdsp->system_sample_rate = -1;
5357 hdsp->playback_pid = -1;
5358 hdsp->capture_pid = -1;
5359 hdsp->capture_substream = NULL;
5360 hdsp->playback_substream = NULL;
5361
5362 if ((err = snd_hdsp_set_defaults(hdsp)) < 0) {
Takashi Iwaib0b981192005-10-20 18:29:58 +02005363 snd_printk(KERN_ERR "Hammerfall-DSP: Error setting default values\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364 return err;
5365 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005366
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367 if (!(hdsp->state & HDSP_InitializationComplete)) {
Clemens Ladischb73c1c12005-09-02 08:49:21 +02005368 strcpy(card->shortname, "Hammerfall DSP");
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005369 sprintf(card->longname, "%s at 0x%lx, irq %d", hdsp->card_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370 hdsp->port, hdsp->irq);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005371
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372 if ((err = snd_card_register(card)) < 0) {
Takashi Iwaib0b981192005-10-20 18:29:58 +02005373 snd_printk(KERN_ERR "Hammerfall-DSP: error registering card\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374 return err;
5375 }
5376 hdsp->state |= HDSP_InitializationComplete;
5377 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005378
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379 return 0;
5380}
5381
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382/* load firmware via hotplug fw loader */
Takashi Iwai92eed662008-02-22 18:35:56 +01005383static int hdsp_request_fw_loader(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384{
5385 const char *fwfile;
5386 const struct firmware *fw;
5387 int err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005388
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389 if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
5390 return 0;
5391 if (hdsp->io_type == Undefined) {
5392 if ((err = hdsp_get_iobox_version(hdsp)) < 0)
5393 return err;
5394 if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
5395 return 0;
5396 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005397
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398 /* caution: max length of firmware filename is 30! */
5399 switch (hdsp->io_type) {
Florian Faber28b26e12010-12-01 12:14:47 +01005400 case RPM:
5401 fwfile = "rpm_firmware.bin";
5402 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403 case Multiface:
5404 if (hdsp->firmware_rev == 0xa)
5405 fwfile = "multiface_firmware.bin";
5406 else
5407 fwfile = "multiface_firmware_rev11.bin";
5408 break;
5409 case Digiface:
5410 if (hdsp->firmware_rev == 0xa)
5411 fwfile = "digiface_firmware.bin";
5412 else
5413 fwfile = "digiface_firmware_rev11.bin";
5414 break;
5415 default:
5416 snd_printk(KERN_ERR "Hammerfall-DSP: invalid io_type %d\n", hdsp->io_type);
5417 return -EINVAL;
5418 }
5419
5420 if (request_firmware(&fw, fwfile, &hdsp->pci->dev)) {
5421 snd_printk(KERN_ERR "Hammerfall-DSP: cannot load firmware %s\n", fwfile);
5422 return -ENOENT;
5423 }
Takashi Iwai90caaef2012-11-22 16:55:11 +01005424 if (fw->size < HDSP_FIRMWARE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425 snd_printk(KERN_ERR "Hammerfall-DSP: too short firmware size %d (expected %d)\n",
Takashi Iwai90caaef2012-11-22 16:55:11 +01005426 (int)fw->size, HDSP_FIRMWARE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427 return -EINVAL;
5428 }
Thomas Charbonnel7679a032005-04-25 11:35:29 +02005429
Takashi Iwai90caaef2012-11-22 16:55:11 +01005430 hdsp->firmware = fw;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005431
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432 hdsp->state |= HDSP_FirmwareCached;
5433
5434 if ((err = snd_hdsp_load_firmware_from_cache(hdsp)) < 0)
5435 return err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005436
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437 if (!(hdsp->state & HDSP_InitializationComplete)) {
Takashi Iwaib0b981192005-10-20 18:29:58 +02005438 if ((err = snd_hdsp_enable_io(hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440
5441 if ((err = snd_hdsp_create_hwdep(hdsp->card, hdsp)) < 0) {
Takashi Iwaib0b981192005-10-20 18:29:58 +02005442 snd_printk(KERN_ERR "Hammerfall-DSP: error creating hwdep device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443 return err;
5444 }
5445 snd_hdsp_initialize_channels(hdsp);
5446 snd_hdsp_initialize_midi_flush(hdsp);
5447 if ((err = snd_hdsp_create_alsa_devices(hdsp->card, hdsp)) < 0) {
Takashi Iwaib0b981192005-10-20 18:29:58 +02005448 snd_printk(KERN_ERR "Hammerfall-DSP: error creating alsa devices\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449 return err;
5450 }
5451 }
5452 return 0;
5453}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454
Bill Pembertone23e7a12012-12-06 12:35:10 -05005455static int snd_hdsp_create(struct snd_card *card,
5456 struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457{
5458 struct pci_dev *pci = hdsp->pci;
5459 int err;
5460 int is_9652 = 0;
5461 int is_9632 = 0;
5462
5463 hdsp->irq = -1;
5464 hdsp->state = 0;
5465 hdsp->midi[0].rmidi = NULL;
5466 hdsp->midi[1].rmidi = NULL;
5467 hdsp->midi[0].input = NULL;
5468 hdsp->midi[1].input = NULL;
5469 hdsp->midi[0].output = NULL;
5470 hdsp->midi[1].output = NULL;
5471 hdsp->midi[0].pending = 0;
5472 hdsp->midi[1].pending = 0;
5473 spin_lock_init(&hdsp->midi[0].lock);
5474 spin_lock_init(&hdsp->midi[1].lock);
5475 hdsp->iobase = NULL;
5476 hdsp->control_register = 0;
5477 hdsp->control2_register = 0;
5478 hdsp->io_type = Undefined;
5479 hdsp->max_channels = 26;
5480
5481 hdsp->card = card;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005482
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483 spin_lock_init(&hdsp->lock);
5484
5485 tasklet_init(&hdsp->midi_tasklet, hdsp_midi_tasklet, (unsigned long)hdsp);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005486
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487 pci_read_config_word(hdsp->pci, PCI_CLASS_REVISION, &hdsp->firmware_rev);
5488 hdsp->firmware_rev &= 0xff;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005489
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490 /* From Martin Bjoernsen :
5491 "It is important that the card's latency timer register in
5492 the PCI configuration space is set to a value much larger
5493 than 0 by the computer's BIOS or the driver.
5494 The windows driver always sets this 8 bit register [...]
5495 to its maximum 255 to avoid problems with some computers."
5496 */
5497 pci_write_config_byte(hdsp->pci, PCI_LATENCY_TIMER, 0xFF);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005498
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499 strcpy(card->driver, "H-DSP");
5500 strcpy(card->mixername, "Xilinx FPGA");
5501
Takashi Iwaib0b981192005-10-20 18:29:58 +02005502 if (hdsp->firmware_rev < 0xa)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503 return -ENODEV;
Takashi Iwaib0b981192005-10-20 18:29:58 +02005504 else if (hdsp->firmware_rev < 0x64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505 hdsp->card_name = "RME Hammerfall DSP";
Takashi Iwaib0b981192005-10-20 18:29:58 +02005506 else if (hdsp->firmware_rev < 0x96) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507 hdsp->card_name = "RME HDSP 9652";
5508 is_9652 = 1;
5509 } else {
5510 hdsp->card_name = "RME HDSP 9632";
5511 hdsp->max_channels = 16;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005512 is_9632 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513 }
5514
Takashi Iwaib0b981192005-10-20 18:29:58 +02005515 if ((err = pci_enable_device(pci)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517
5518 pci_set_master(hdsp->pci);
5519
5520 if ((err = pci_request_regions(pci, "hdsp")) < 0)
5521 return err;
5522 hdsp->port = pci_resource_start(pci, 0);
5523 if ((hdsp->iobase = ioremap_nocache(hdsp->port, HDSP_IO_EXTENT)) == NULL) {
Takashi Iwaib0b981192005-10-20 18:29:58 +02005524 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 -07005525 return -EBUSY;
5526 }
5527
Takashi Iwai437a5a42006-11-21 12:14:23 +01005528 if (request_irq(pci->irq, snd_hdsp_interrupt, IRQF_SHARED,
Takashi Iwai934c2b62011-06-10 16:36:37 +02005529 KBUILD_MODNAME, hdsp)) {
Takashi Iwaib0b981192005-10-20 18:29:58 +02005530 snd_printk(KERN_ERR "Hammerfall-DSP: unable to use IRQ %d\n", pci->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531 return -EBUSY;
5532 }
5533
5534 hdsp->irq = pci->irq;
Remy Bruno176546a2006-10-16 12:32:53 +02005535 hdsp->precise_ptr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536 hdsp->use_midi_tasklet = 1;
Remy Brunod7923b22006-10-17 12:41:56 +02005537 hdsp->dds_value = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538
Takashi Iwaib0b981192005-10-20 18:29:58 +02005539 if ((err = snd_hdsp_initialize_memory(hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540 return err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005541
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542 if (!is_9652 && !is_9632) {
Tim Blechmanne588ed82009-02-20 19:30:35 +01005543 /* we wait a maximum of 10 seconds to let freshly
5544 * inserted cardbus cards do their hardware init */
5545 err = hdsp_wait_for_iobox(hdsp, 1000, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546
Tim Blechmann00c9ddd2008-11-09 12:50:52 +01005547 if (err < 0)
5548 return err;
5549
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550 if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
Takashi Iwaib0b981192005-10-20 18:29:58 +02005551 if ((err = hdsp_request_fw_loader(hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552 /* we don't fail as this can happen
5553 if userspace is not ready for
5554 firmware upload
5555 */
Takashi Iwaib0b981192005-10-20 18:29:58 +02005556 snd_printk(KERN_ERR "Hammerfall-DSP: couldn't get firmware from userspace. try using hdsploader\n");
5557 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558 /* init is complete, we return */
5559 return 0;
Tim Blechmann00c9ddd2008-11-09 12:50:52 +01005560 /* we defer initialization */
Takashi Iwaib0b981192005-10-20 18:29:58 +02005561 snd_printk(KERN_INFO "Hammerfall-DSP: card initialization pending : waiting for firmware\n");
5562 if ((err = snd_hdsp_create_hwdep(card, hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564 return 0;
5565 } else {
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005566 snd_printk(KERN_INFO "Hammerfall-DSP: Firmware already present, initializing card.\n");
Florian Faber28b26e12010-12-01 12:14:47 +01005567 if (hdsp_read(hdsp, HDSP_status2Register) & HDSP_version2)
5568 hdsp->io_type = RPM;
5569 else if (hdsp_read(hdsp, HDSP_status2Register) & HDSP_version1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570 hdsp->io_type = Multiface;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005571 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572 hdsp->io_type = Digiface;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 }
5574 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005575
Takashi Iwaib0b981192005-10-20 18:29:58 +02005576 if ((err = snd_hdsp_enable_io(hdsp)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577 return err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005578
Takashi Iwaib0b981192005-10-20 18:29:58 +02005579 if (is_9652)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580 hdsp->io_type = H9652;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005581
Takashi Iwaib0b981192005-10-20 18:29:58 +02005582 if (is_9632)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583 hdsp->io_type = H9632;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584
Takashi Iwaib0b981192005-10-20 18:29:58 +02005585 if ((err = snd_hdsp_create_hwdep(card, hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586 return err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005587
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588 snd_hdsp_initialize_channels(hdsp);
5589 snd_hdsp_initialize_midi_flush(hdsp);
5590
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005591 hdsp->state |= HDSP_FirmwareLoaded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592
Takashi Iwaib0b981192005-10-20 18:29:58 +02005593 if ((err = snd_hdsp_create_alsa_devices(card, hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005596 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597}
5598
Takashi Iwai55e957d2005-11-17 14:52:13 +01005599static int snd_hdsp_free(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600{
5601 if (hdsp->port) {
5602 /* stop the audio, and cancel all interrupts */
5603 tasklet_kill(&hdsp->midi_tasklet);
5604 hdsp->control_register &= ~(HDSP_Start|HDSP_AudioInterruptEnable|HDSP_Midi0InterruptEnable|HDSP_Midi1InterruptEnable);
5605 hdsp_write (hdsp, HDSP_controlRegister, hdsp->control_register);
5606 }
5607
5608 if (hdsp->irq >= 0)
5609 free_irq(hdsp->irq, (void *)hdsp);
5610
5611 snd_hdsp_free_buffers(hdsp);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005612
Takashi Iwai90caaef2012-11-22 16:55:11 +01005613 if (hdsp->firmware)
5614 release_firmware(hdsp->firmware);
5615 vfree(hdsp->fw_uploaded);
5616
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617 if (hdsp->iobase)
5618 iounmap(hdsp->iobase);
5619
5620 if (hdsp->port)
5621 pci_release_regions(hdsp->pci);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005622
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623 pci_disable_device(hdsp->pci);
5624 return 0;
5625}
5626
Takashi Iwai55e957d2005-11-17 14:52:13 +01005627static void snd_hdsp_card_free(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628{
Joe Perches9fe856e2010-09-04 18:52:54 -07005629 struct hdsp *hdsp = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630
5631 if (hdsp)
5632 snd_hdsp_free(hdsp);
5633}
5634
Bill Pembertone23e7a12012-12-06 12:35:10 -05005635static int snd_hdsp_probe(struct pci_dev *pci,
5636 const struct pci_device_id *pci_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637{
5638 static int dev;
Takashi Iwai55e957d2005-11-17 14:52:13 +01005639 struct hdsp *hdsp;
5640 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641 int err;
5642
5643 if (dev >= SNDRV_CARDS)
5644 return -ENODEV;
5645 if (!enable[dev]) {
5646 dev++;
5647 return -ENOENT;
5648 }
5649
Takashi Iwaie58de7b2008-12-28 16:44:30 +01005650 err = snd_card_create(index[dev], id[dev], THIS_MODULE,
5651 sizeof(struct hdsp), &card);
5652 if (err < 0)
5653 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654
Joe Perches9fe856e2010-09-04 18:52:54 -07005655 hdsp = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656 card->private_free = snd_hdsp_card_free;
5657 hdsp->dev = dev;
5658 hdsp->pci = pci;
5659 snd_card_set_dev(card, &pci->dev);
5660
5661 if ((err = snd_hdsp_create(card, hdsp)) < 0) {
5662 snd_card_free(card);
5663 return err;
5664 }
5665
5666 strcpy(card->shortname, "Hammerfall DSP");
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005667 sprintf(card->longname, "%s at 0x%lx, irq %d", hdsp->card_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668 hdsp->port, hdsp->irq);
5669
5670 if ((err = snd_card_register(card)) < 0) {
5671 snd_card_free(card);
5672 return err;
5673 }
5674 pci_set_drvdata(pci, card);
5675 dev++;
5676 return 0;
5677}
5678
Bill Pembertone23e7a12012-12-06 12:35:10 -05005679static void snd_hdsp_remove(struct pci_dev *pci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680{
5681 snd_card_free(pci_get_drvdata(pci));
5682 pci_set_drvdata(pci, NULL);
5683}
5684
Takashi Iwaie9f66d92012-04-24 12:25:00 +02005685static struct pci_driver hdsp_driver = {
Takashi Iwai3733e422011-06-10 16:20:20 +02005686 .name = KBUILD_MODNAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687 .id_table = snd_hdsp_ids,
5688 .probe = snd_hdsp_probe,
Bill Pembertone23e7a12012-12-06 12:35:10 -05005689 .remove = snd_hdsp_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005690};
5691
Takashi Iwaie9f66d92012-04-24 12:25:00 +02005692module_pci_driver(hdsp_driver);