blob: c19e021ccf668af1aadec6377b1d3e1ebca9b090 [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>
Takashi Iwai6cbbfe12015-01-28 16:49:33 +010032#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34#include <sound/core.h>
35#include <sound/control.h>
36#include <sound/pcm.h>
37#include <sound/info.h>
38#include <sound/asoundef.h>
39#include <sound/rawmidi.h>
40#include <sound/hwdep.h>
41#include <sound/initval.h>
42#include <sound/hdsp.h>
43
44#include <asm/byteorder.h>
45#include <asm/current.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
48static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
Rusty Russella67ff6a2011-12-15 13:49:36 +103049static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51module_param_array(index, int, NULL, 0444);
52MODULE_PARM_DESC(index, "Index value for RME Hammerfall DSP interface.");
53module_param_array(id, charp, NULL, 0444);
54MODULE_PARM_DESC(id, "ID string for RME Hammerfall DSP interface.");
55module_param_array(enable, bool, NULL, 0444);
56MODULE_PARM_DESC(enable, "Enable/disable specific Hammerfall DSP soundcards.");
57MODULE_AUTHOR("Paul Davis <paul@linuxaudiosystems.com>, Marcus Andersson, Thomas Charbonnel <thomas@undata.org>");
58MODULE_DESCRIPTION("RME Hammerfall DSP");
59MODULE_LICENSE("GPL");
60MODULE_SUPPORTED_DEVICE("{{RME Hammerfall-DSP},"
61 "{RME HDSP-9652},"
62 "{RME HDSP-9632}}");
Florian Faber28b26e12010-12-01 12:14:47 +010063MODULE_FIRMWARE("rpm_firmware.bin");
Clemens Ladisch7e0af292007-05-03 17:59:54 +020064MODULE_FIRMWARE("multiface_firmware.bin");
65MODULE_FIRMWARE("multiface_firmware_rev11.bin");
66MODULE_FIRMWARE("digiface_firmware.bin");
67MODULE_FIRMWARE("digiface_firmware_rev11.bin");
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69#define HDSP_MAX_CHANNELS 26
70#define HDSP_MAX_DS_CHANNELS 14
71#define HDSP_MAX_QS_CHANNELS 8
72#define DIGIFACE_SS_CHANNELS 26
73#define DIGIFACE_DS_CHANNELS 14
74#define MULTIFACE_SS_CHANNELS 18
75#define MULTIFACE_DS_CHANNELS 14
76#define H9652_SS_CHANNELS 26
77#define H9652_DS_CHANNELS 14
78/* This does not include possible Analog Extension Boards
79 AEBs are detected at card initialization
80*/
81#define H9632_SS_CHANNELS 12
82#define H9632_DS_CHANNELS 8
83#define H9632_QS_CHANNELS 4
Florian Faber28b26e12010-12-01 12:14:47 +010084#define RPM_CHANNELS 6
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86/* Write registers. These are defined as byte-offsets from the iobase value.
87 */
88#define HDSP_resetPointer 0
Remy Brunod7923b22006-10-17 12:41:56 +020089#define HDSP_freqReg 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#define HDSP_outputBufferAddress 32
91#define HDSP_inputBufferAddress 36
92#define HDSP_controlRegister 64
93#define HDSP_interruptConfirmation 96
94#define HDSP_outputEnable 128
95#define HDSP_control2Reg 256
96#define HDSP_midiDataOut0 352
97#define HDSP_midiDataOut1 356
98#define HDSP_fifoData 368
99#define HDSP_inputEnable 384
100
101/* Read registers. These are defined as byte-offsets from the iobase value
102 */
103
104#define HDSP_statusRegister 0
105#define HDSP_timecode 128
106#define HDSP_status2Register 192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107#define HDSP_midiDataIn0 360
108#define HDSP_midiDataIn1 364
109#define HDSP_midiStatusOut0 384
110#define HDSP_midiStatusOut1 388
111#define HDSP_midiStatusIn0 392
112#define HDSP_midiStatusIn1 396
113#define HDSP_fifoStatus 400
114
115/* the meters are regular i/o-mapped registers, but offset
116 considerably from the rest. the peak registers are reset
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100117 when read; the least-significant 4 bits are full-scale counters;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 the actual peak value is in the most-significant 24 bits.
119*/
120
121#define HDSP_playbackPeakLevel 4096 /* 26 * 32 bit values */
122#define HDSP_inputPeakLevel 4224 /* 26 * 32 bit values */
123#define HDSP_outputPeakLevel 4352 /* (26+2) * 32 bit values */
124#define HDSP_playbackRmsLevel 4612 /* 26 * 64 bit values */
125#define HDSP_inputRmsLevel 4868 /* 26 * 64 bit values */
126
127
128/* This is for H9652 cards
129 Peak values are read downward from the base
130 Rms values are read upward
131 There are rms values for the outputs too
132 26*3 values are read in ss mode
133 14*3 in ds mode, with no gap between values
134*/
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100135#define HDSP_9652_peakBase 7164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136#define HDSP_9652_rmsBase 4096
137
138/* c.f. the hdsp_9632_meters_t struct */
139#define HDSP_9632_metersBase 4096
140
141#define HDSP_IO_EXTENT 7168
142
143/* control2 register bits */
144
145#define HDSP_TMS 0x01
146#define HDSP_TCK 0x02
147#define HDSP_TDI 0x04
148#define HDSP_JTAG 0x08
149#define HDSP_PWDN 0x10
150#define HDSP_PROGRAM 0x020
151#define HDSP_CONFIG_MODE_0 0x040
152#define HDSP_CONFIG_MODE_1 0x080
Adrian Knotha3466862011-10-27 21:57:53 +0200153#define HDSP_VERSION_BIT (0x100 | HDSP_S_LOAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154#define HDSP_BIGENDIAN_MODE 0x200
155#define HDSP_RD_MULTIPLE 0x400
156#define HDSP_9652_ENABLE_MIXER 0x800
Adrian Knoth0c2bc7c2013-01-15 18:52:20 +0100157#define HDSP_S200 0x800
158#define HDSP_S300 (0x100 | HDSP_S200) /* dummy, purpose of 0x100 unknown */
159#define HDSP_CYCLIC_MODE 0x1000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160#define HDSP_TDO 0x10000000
161
Adrian Knoth0c2bc7c2013-01-15 18:52:20 +0100162#define HDSP_S_PROGRAM (HDSP_CYCLIC_MODE|HDSP_PROGRAM|HDSP_CONFIG_MODE_0)
163#define HDSP_S_LOAD (HDSP_CYCLIC_MODE|HDSP_PROGRAM|HDSP_CONFIG_MODE_1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165/* Control Register bits */
166
167#define HDSP_Start (1<<0) /* start engine */
168#define HDSP_Latency0 (1<<1) /* buffer size = 2^n where n is defined by Latency{2,1,0} */
169#define HDSP_Latency1 (1<<2) /* [ see above ] */
170#define HDSP_Latency2 (1<<3) /* [ see above ] */
171#define HDSP_ClockModeMaster (1<<4) /* 1=Master, 0=Slave/Autosync */
172#define HDSP_AudioInterruptEnable (1<<5) /* what do you think ? */
173#define HDSP_Frequency0 (1<<6) /* 0=44.1kHz/88.2kHz/176.4kHz 1=48kHz/96kHz/192kHz */
174#define HDSP_Frequency1 (1<<7) /* 0=32kHz/64kHz/128kHz */
175#define HDSP_DoubleSpeed (1<<8) /* 0=normal speed, 1=double speed */
176#define HDSP_SPDIFProfessional (1<<9) /* 0=consumer, 1=professional */
177#define HDSP_SPDIFEmphasis (1<<10) /* 0=none, 1=on */
178#define HDSP_SPDIFNonAudio (1<<11) /* 0=off, 1=on */
179#define HDSP_SPDIFOpticalOut (1<<12) /* 1=use 1st ADAT connector for SPDIF, 0=do not */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100180#define HDSP_SyncRef2 (1<<13)
181#define HDSP_SPDIFInputSelect0 (1<<14)
182#define HDSP_SPDIFInputSelect1 (1<<15)
183#define HDSP_SyncRef0 (1<<16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184#define HDSP_SyncRef1 (1<<17)
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100185#define HDSP_AnalogExtensionBoard (1<<18) /* For H9632 cards */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186#define HDSP_XLRBreakoutCable (1<<20) /* For H9632 cards */
187#define HDSP_Midi0InterruptEnable (1<<22)
188#define HDSP_Midi1InterruptEnable (1<<23)
189#define HDSP_LineOut (1<<24)
190#define HDSP_ADGain0 (1<<25) /* From here : H9632 specific */
191#define HDSP_ADGain1 (1<<26)
192#define HDSP_DAGain0 (1<<27)
193#define HDSP_DAGain1 (1<<28)
194#define HDSP_PhoneGain0 (1<<29)
195#define HDSP_PhoneGain1 (1<<30)
196#define HDSP_QuadSpeed (1<<31)
197
Florian Faber28b26e12010-12-01 12:14:47 +0100198/* RPM uses some of the registers for special purposes */
199#define HDSP_RPM_Inp12 0x04A00
200#define HDSP_RPM_Inp12_Phon_6dB 0x00800 /* Dolby */
201#define HDSP_RPM_Inp12_Phon_0dB 0x00000 /* .. */
202#define HDSP_RPM_Inp12_Phon_n6dB 0x04000 /* inp_0 */
203#define HDSP_RPM_Inp12_Line_0dB 0x04200 /* Dolby+PRO */
204#define HDSP_RPM_Inp12_Line_n6dB 0x00200 /* PRO */
205
206#define HDSP_RPM_Inp34 0x32000
207#define HDSP_RPM_Inp34_Phon_6dB 0x20000 /* SyncRef1 */
208#define HDSP_RPM_Inp34_Phon_0dB 0x00000 /* .. */
209#define HDSP_RPM_Inp34_Phon_n6dB 0x02000 /* SyncRef2 */
210#define HDSP_RPM_Inp34_Line_0dB 0x30000 /* SyncRef1+SyncRef0 */
211#define HDSP_RPM_Inp34_Line_n6dB 0x10000 /* SyncRef0 */
212
213#define HDSP_RPM_Bypass 0x01000
214
215#define HDSP_RPM_Disconnect 0x00001
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217#define HDSP_ADGainMask (HDSP_ADGain0|HDSP_ADGain1)
218#define HDSP_ADGainMinus10dBV HDSP_ADGainMask
219#define HDSP_ADGainPlus4dBu (HDSP_ADGain0)
220#define HDSP_ADGainLowGain 0
221
222#define HDSP_DAGainMask (HDSP_DAGain0|HDSP_DAGain1)
223#define HDSP_DAGainHighGain HDSP_DAGainMask
224#define HDSP_DAGainPlus4dBu (HDSP_DAGain0)
225#define HDSP_DAGainMinus10dBV 0
226
227#define HDSP_PhoneGainMask (HDSP_PhoneGain0|HDSP_PhoneGain1)
228#define HDSP_PhoneGain0dB HDSP_PhoneGainMask
229#define HDSP_PhoneGainMinus6dB (HDSP_PhoneGain0)
230#define HDSP_PhoneGainMinus12dB 0
231
232#define HDSP_LatencyMask (HDSP_Latency0|HDSP_Latency1|HDSP_Latency2)
233#define HDSP_FrequencyMask (HDSP_Frequency0|HDSP_Frequency1|HDSP_DoubleSpeed|HDSP_QuadSpeed)
234
235#define HDSP_SPDIFInputMask (HDSP_SPDIFInputSelect0|HDSP_SPDIFInputSelect1)
236#define HDSP_SPDIFInputADAT1 0
237#define HDSP_SPDIFInputCoaxial (HDSP_SPDIFInputSelect0)
238#define HDSP_SPDIFInputCdrom (HDSP_SPDIFInputSelect1)
239#define HDSP_SPDIFInputAES (HDSP_SPDIFInputSelect0|HDSP_SPDIFInputSelect1)
240
241#define HDSP_SyncRefMask (HDSP_SyncRef0|HDSP_SyncRef1|HDSP_SyncRef2)
242#define HDSP_SyncRef_ADAT1 0
243#define HDSP_SyncRef_ADAT2 (HDSP_SyncRef0)
244#define HDSP_SyncRef_ADAT3 (HDSP_SyncRef1)
245#define HDSP_SyncRef_SPDIF (HDSP_SyncRef0|HDSP_SyncRef1)
246#define HDSP_SyncRef_WORD (HDSP_SyncRef2)
247#define HDSP_SyncRef_ADAT_SYNC (HDSP_SyncRef0|HDSP_SyncRef2)
248
249/* Sample Clock Sources */
250
251#define HDSP_CLOCK_SOURCE_AUTOSYNC 0
252#define HDSP_CLOCK_SOURCE_INTERNAL_32KHZ 1
253#define HDSP_CLOCK_SOURCE_INTERNAL_44_1KHZ 2
254#define HDSP_CLOCK_SOURCE_INTERNAL_48KHZ 3
255#define HDSP_CLOCK_SOURCE_INTERNAL_64KHZ 4
256#define HDSP_CLOCK_SOURCE_INTERNAL_88_2KHZ 5
257#define HDSP_CLOCK_SOURCE_INTERNAL_96KHZ 6
258#define HDSP_CLOCK_SOURCE_INTERNAL_128KHZ 7
259#define HDSP_CLOCK_SOURCE_INTERNAL_176_4KHZ 8
260#define HDSP_CLOCK_SOURCE_INTERNAL_192KHZ 9
261
262/* Preferred sync reference choices - used by "pref_sync_ref" control switch */
263
264#define HDSP_SYNC_FROM_WORD 0
265#define HDSP_SYNC_FROM_SPDIF 1
266#define HDSP_SYNC_FROM_ADAT1 2
267#define HDSP_SYNC_FROM_ADAT_SYNC 3
268#define HDSP_SYNC_FROM_ADAT2 4
269#define HDSP_SYNC_FROM_ADAT3 5
270
271/* SyncCheck status */
272
273#define HDSP_SYNC_CHECK_NO_LOCK 0
274#define HDSP_SYNC_CHECK_LOCK 1
275#define HDSP_SYNC_CHECK_SYNC 2
276
277/* AutoSync references - used by "autosync_ref" control switch */
278
279#define HDSP_AUTOSYNC_FROM_WORD 0
280#define HDSP_AUTOSYNC_FROM_ADAT_SYNC 1
281#define HDSP_AUTOSYNC_FROM_SPDIF 2
282#define HDSP_AUTOSYNC_FROM_NONE 3
283#define HDSP_AUTOSYNC_FROM_ADAT1 4
284#define HDSP_AUTOSYNC_FROM_ADAT2 5
285#define HDSP_AUTOSYNC_FROM_ADAT3 6
286
287/* Possible sources of S/PDIF input */
288
289#define HDSP_SPDIFIN_OPTICAL 0 /* optical (ADAT1) */
290#define HDSP_SPDIFIN_COAXIAL 1 /* coaxial (RCA) */
291#define HDSP_SPDIFIN_INTERNAL 2 /* internal (CDROM) */
292#define HDSP_SPDIFIN_AES 3 /* xlr for H9632 (AES)*/
293
294#define HDSP_Frequency32KHz HDSP_Frequency0
295#define HDSP_Frequency44_1KHz HDSP_Frequency1
296#define HDSP_Frequency48KHz (HDSP_Frequency1|HDSP_Frequency0)
297#define HDSP_Frequency64KHz (HDSP_DoubleSpeed|HDSP_Frequency0)
298#define HDSP_Frequency88_2KHz (HDSP_DoubleSpeed|HDSP_Frequency1)
299#define HDSP_Frequency96KHz (HDSP_DoubleSpeed|HDSP_Frequency1|HDSP_Frequency0)
300/* For H9632 cards */
301#define HDSP_Frequency128KHz (HDSP_QuadSpeed|HDSP_DoubleSpeed|HDSP_Frequency0)
302#define HDSP_Frequency176_4KHz (HDSP_QuadSpeed|HDSP_DoubleSpeed|HDSP_Frequency1)
303#define HDSP_Frequency192KHz (HDSP_QuadSpeed|HDSP_DoubleSpeed|HDSP_Frequency1|HDSP_Frequency0)
Julian Cablee4b60882007-03-19 11:44:40 +0100304/* RME says n = 104857600000000, but in the windows MADI driver, I see:
305 return 104857600000000 / rate; // 100 MHz
306 return 110100480000000 / rate; // 105 MHz
307*/
308#define DDS_NUMERATOR 104857600000000ULL; /* = 2^20 * 10^8 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310#define hdsp_encode_latency(x) (((x)<<1) & HDSP_LatencyMask)
311#define hdsp_decode_latency(x) (((x) & HDSP_LatencyMask)>>1)
312
313#define hdsp_encode_spdif_in(x) (((x)&0x3)<<14)
314#define hdsp_decode_spdif_in(x) (((x)>>14)&0x3)
315
316/* Status Register bits */
317
318#define HDSP_audioIRQPending (1<<0)
319#define HDSP_Lock2 (1<<1) /* this is for Digiface and H9652 */
320#define HDSP_spdifFrequency3 HDSP_Lock2 /* this is for H9632 only */
321#define HDSP_Lock1 (1<<2)
322#define HDSP_Lock0 (1<<3)
323#define HDSP_SPDIFSync (1<<4)
324#define HDSP_TimecodeLock (1<<5)
325#define HDSP_BufferPositionMask 0x000FFC0 /* Bit 6..15 : h/w buffer pointer */
326#define HDSP_Sync2 (1<<16)
327#define HDSP_Sync1 (1<<17)
328#define HDSP_Sync0 (1<<18)
329#define HDSP_DoubleSpeedStatus (1<<19)
330#define HDSP_ConfigError (1<<20)
331#define HDSP_DllError (1<<21)
332#define HDSP_spdifFrequency0 (1<<22)
333#define HDSP_spdifFrequency1 (1<<23)
334#define HDSP_spdifFrequency2 (1<<24)
335#define HDSP_SPDIFErrorFlag (1<<25)
336#define HDSP_BufferID (1<<26)
337#define HDSP_TimecodeSync (1<<27)
338#define HDSP_AEBO (1<<28) /* H9632 specific Analog Extension Boards */
339#define HDSP_AEBI (1<<29) /* 0 = present, 1 = absent */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100340#define HDSP_midi0IRQPending (1<<30)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341#define HDSP_midi1IRQPending (1<<31)
342
343#define HDSP_spdifFrequencyMask (HDSP_spdifFrequency0|HDSP_spdifFrequency1|HDSP_spdifFrequency2)
Remy Bruno47ba97f2008-02-22 17:57:02 +0100344#define HDSP_spdifFrequencyMask_9632 (HDSP_spdifFrequency0|\
345 HDSP_spdifFrequency1|\
346 HDSP_spdifFrequency2|\
347 HDSP_spdifFrequency3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349#define HDSP_spdifFrequency32KHz (HDSP_spdifFrequency0)
350#define HDSP_spdifFrequency44_1KHz (HDSP_spdifFrequency1)
351#define HDSP_spdifFrequency48KHz (HDSP_spdifFrequency0|HDSP_spdifFrequency1)
352
353#define HDSP_spdifFrequency64KHz (HDSP_spdifFrequency2)
354#define HDSP_spdifFrequency88_2KHz (HDSP_spdifFrequency0|HDSP_spdifFrequency2)
355#define HDSP_spdifFrequency96KHz (HDSP_spdifFrequency2|HDSP_spdifFrequency1)
356
357/* This is for H9632 cards */
Remy Bruno47ba97f2008-02-22 17:57:02 +0100358#define HDSP_spdifFrequency128KHz (HDSP_spdifFrequency0|\
359 HDSP_spdifFrequency1|\
360 HDSP_spdifFrequency2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361#define HDSP_spdifFrequency176_4KHz HDSP_spdifFrequency3
362#define HDSP_spdifFrequency192KHz (HDSP_spdifFrequency3|HDSP_spdifFrequency0)
363
364/* Status2 Register bits */
365
366#define HDSP_version0 (1<<0)
367#define HDSP_version1 (1<<1)
368#define HDSP_version2 (1<<2)
369#define HDSP_wc_lock (1<<3)
370#define HDSP_wc_sync (1<<4)
371#define HDSP_inp_freq0 (1<<5)
372#define HDSP_inp_freq1 (1<<6)
373#define HDSP_inp_freq2 (1<<7)
374#define HDSP_SelSyncRef0 (1<<8)
375#define HDSP_SelSyncRef1 (1<<9)
376#define HDSP_SelSyncRef2 (1<<10)
377
378#define HDSP_wc_valid (HDSP_wc_lock|HDSP_wc_sync)
379
380#define HDSP_systemFrequencyMask (HDSP_inp_freq0|HDSP_inp_freq1|HDSP_inp_freq2)
381#define HDSP_systemFrequency32 (HDSP_inp_freq0)
382#define HDSP_systemFrequency44_1 (HDSP_inp_freq1)
383#define HDSP_systemFrequency48 (HDSP_inp_freq0|HDSP_inp_freq1)
384#define HDSP_systemFrequency64 (HDSP_inp_freq2)
385#define HDSP_systemFrequency88_2 (HDSP_inp_freq0|HDSP_inp_freq2)
386#define HDSP_systemFrequency96 (HDSP_inp_freq1|HDSP_inp_freq2)
387/* FIXME : more values for 9632 cards ? */
388
389#define HDSP_SelSyncRefMask (HDSP_SelSyncRef0|HDSP_SelSyncRef1|HDSP_SelSyncRef2)
390#define HDSP_SelSyncRef_ADAT1 0
391#define HDSP_SelSyncRef_ADAT2 (HDSP_SelSyncRef0)
392#define HDSP_SelSyncRef_ADAT3 (HDSP_SelSyncRef1)
393#define HDSP_SelSyncRef_SPDIF (HDSP_SelSyncRef0|HDSP_SelSyncRef1)
394#define HDSP_SelSyncRef_WORD (HDSP_SelSyncRef2)
395#define HDSP_SelSyncRef_ADAT_SYNC (HDSP_SelSyncRef0|HDSP_SelSyncRef2)
396
397/* Card state flags */
398
399#define HDSP_InitializationComplete (1<<0)
400#define HDSP_FirmwareLoaded (1<<1)
401#define HDSP_FirmwareCached (1<<2)
402
403/* FIFO wait times, defined in terms of 1/10ths of msecs */
404
405#define HDSP_LONG_WAIT 5000
406#define HDSP_SHORT_WAIT 30
407
408#define UNITY_GAIN 32768
409#define MINUS_INFINITY_GAIN 0
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411/* the size of a substream (1 mono data stream) */
412
413#define HDSP_CHANNEL_BUFFER_SAMPLES (16*1024)
414#define HDSP_CHANNEL_BUFFER_BYTES (4*HDSP_CHANNEL_BUFFER_SAMPLES)
415
416/* the size of the area we need to allocate for DMA transfers. the
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100417 size is the same regardless of the number of channels - the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 Multiface still uses the same memory area.
419
420 Note that we allocate 1 more channel than is apparently needed
421 because the h/w seems to write 1 byte beyond the end of the last
422 page. Sigh.
423*/
424
425#define HDSP_DMA_AREA_BYTES ((HDSP_MAX_CHANNELS+1) * HDSP_CHANNEL_BUFFER_BYTES)
426#define HDSP_DMA_AREA_KILOBYTES (HDSP_DMA_AREA_BYTES/1024)
427
Takashi Iwai90caaef2012-11-22 16:55:11 +0100428#define HDSP_FIRMWARE_SIZE (24413 * 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Takashi Iwai55e957d2005-11-17 14:52:13 +0100430struct hdsp_9632_meters {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 u32 input_peak[16];
432 u32 playback_peak[16];
433 u32 output_peak[16];
434 u32 xxx_peak[16];
435 u32 padding[64];
436 u32 input_rms_low[16];
437 u32 playback_rms_low[16];
438 u32 output_rms_low[16];
439 u32 xxx_rms_low[16];
440 u32 input_rms_high[16];
441 u32 playback_rms_high[16];
442 u32 output_rms_high[16];
443 u32 xxx_rms_high[16];
444};
445
Takashi Iwai55e957d2005-11-17 14:52:13 +0100446struct hdsp_midi {
447 struct hdsp *hdsp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 int id;
Takashi Iwai55e957d2005-11-17 14:52:13 +0100449 struct snd_rawmidi *rmidi;
450 struct snd_rawmidi_substream *input;
451 struct snd_rawmidi_substream *output;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 char istimer; /* timer in use */
453 struct timer_list timer;
454 spinlock_t lock;
455 int pending;
456};
457
Takashi Iwai55e957d2005-11-17 14:52:13 +0100458struct hdsp {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 spinlock_t lock;
Takashi Iwai55e957d2005-11-17 14:52:13 +0100460 struct snd_pcm_substream *capture_substream;
461 struct snd_pcm_substream *playback_substream;
462 struct hdsp_midi midi[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 struct tasklet_struct midi_tasklet;
464 int use_midi_tasklet;
465 int precise_ptr;
466 u32 control_register; /* cached value */
467 u32 control2_register; /* cached value */
468 u32 creg_spdif;
469 u32 creg_spdif_stream;
Takashi Iwaie3ea4d82005-07-04 18:12:39 +0200470 int clock_source_locked;
Florian Faber28b26e12010-12-01 12:14:47 +0100471 char *card_name; /* digiface/multiface/rpm */
Takashi Iwai55e957d2005-11-17 14:52:13 +0100472 enum HDSP_IO_Type io_type; /* ditto, but for code use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 unsigned short firmware_rev;
474 unsigned short state; /* stores state bits */
Takashi Iwai90caaef2012-11-22 16:55:11 +0100475 const struct firmware *firmware;
476 u32 *fw_uploaded;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 size_t period_bytes; /* guess what this is */
478 unsigned char max_channels;
479 unsigned char qs_in_channels; /* quad speed mode for H9632 */
480 unsigned char ds_in_channels;
481 unsigned char ss_in_channels; /* different for multiface/digiface */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100482 unsigned char qs_out_channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 unsigned char ds_out_channels;
484 unsigned char ss_out_channels;
485
486 struct snd_dma_buffer capture_dma_buf;
487 struct snd_dma_buffer playback_dma_buf;
488 unsigned char *capture_buffer; /* suitably aligned address */
489 unsigned char *playback_buffer; /* suitably aligned address */
490
491 pid_t capture_pid;
492 pid_t playback_pid;
493 int running;
494 int system_sample_rate;
495 char *channel_map;
496 int dev;
497 int irq;
498 unsigned long port;
499 void __iomem *iobase;
Takashi Iwai55e957d2005-11-17 14:52:13 +0100500 struct snd_card *card;
501 struct snd_pcm *pcm;
502 struct snd_hwdep *hwdep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 struct pci_dev *pci;
Takashi Iwai55e957d2005-11-17 14:52:13 +0100504 struct snd_kcontrol *spdif_ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 unsigned short mixer_matrix[HDSP_MATRIX_MIXER_SIZE];
Remy Brunod7923b22006-10-17 12:41:56 +0200506 unsigned int dds_value; /* last value written to freq register */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507};
508
509/* These tables map the ALSA channels 1..N to the channels that we
510 need to use in order to find the relevant channel buffer. RME
511 refer to this kind of mapping as between "the ADAT channel and
512 the DMA channel." We index it using the logical audio channel,
513 and the value is the DMA channel (i.e. channel buffer number)
514 where the data for that channel can be read/written from/to.
515*/
516
517static char channel_map_df_ss[HDSP_MAX_CHANNELS] = {
518 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
519 18, 19, 20, 21, 22, 23, 24, 25
520};
521
522static char channel_map_mf_ss[HDSP_MAX_CHANNELS] = { /* Multiface */
523 /* Analog */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100524 0, 1, 2, 3, 4, 5, 6, 7,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 /* ADAT 2 */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100526 16, 17, 18, 19, 20, 21, 22, 23,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 /* SPDIF */
528 24, 25,
529 -1, -1, -1, -1, -1, -1, -1, -1
530};
531
532static char channel_map_ds[HDSP_MAX_CHANNELS] = {
533 /* ADAT channels are remapped */
534 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23,
535 /* channels 12 and 13 are S/PDIF */
536 24, 25,
537 /* others don't exist */
538 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
539};
540
541static char channel_map_H9632_ss[HDSP_MAX_CHANNELS] = {
542 /* ADAT channels */
543 0, 1, 2, 3, 4, 5, 6, 7,
544 /* SPDIF */
545 8, 9,
546 /* Analog */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100547 10, 11,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 /* AO4S-192 and AI4S-192 extension boards */
549 12, 13, 14, 15,
550 /* others don't exist */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100551 -1, -1, -1, -1, -1, -1, -1, -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 -1, -1
553};
554
555static char channel_map_H9632_ds[HDSP_MAX_CHANNELS] = {
556 /* ADAT */
557 1, 3, 5, 7,
558 /* SPDIF */
559 8, 9,
560 /* Analog */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100561 10, 11,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 /* AO4S-192 and AI4S-192 extension boards */
563 12, 13, 14, 15,
564 /* others don't exist */
565 -1, -1, -1, -1, -1, -1, -1, -1,
566 -1, -1, -1, -1, -1, -1
567};
568
569static char channel_map_H9632_qs[HDSP_MAX_CHANNELS] = {
570 /* ADAT is disabled in this mode */
571 /* SPDIF */
572 8, 9,
573 /* Analog */
574 10, 11,
575 /* AO4S-192 and AI4S-192 extension boards */
576 12, 13, 14, 15,
577 /* others don't exist */
578 -1, -1, -1, -1, -1, -1, -1, -1,
579 -1, -1, -1, -1, -1, -1, -1, -1,
580 -1, -1
581};
582
583static int snd_hammerfall_get_buffer(struct pci_dev *pci, struct snd_dma_buffer *dmab, size_t size)
584{
585 dmab->dev.type = SNDRV_DMA_TYPE_DEV;
586 dmab->dev.dev = snd_dma_pci_data(pci);
Takashi Iwaib6a96912005-05-30 18:27:03 +0200587 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
588 size, dmab) < 0)
589 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 return 0;
591}
592
593static void snd_hammerfall_free_buffer(struct snd_dma_buffer *dmab, struct pci_dev *pci)
594{
Takashi Iwai47d98c02014-01-08 16:12:25 +0100595 if (dmab->area)
596 snd_dma_free_pages(dmab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597}
598
599
Benoit Taine9baa3c32014-08-08 15:56:03 +0200600static const struct pci_device_id snd_hdsp_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 {
602 .vendor = PCI_VENDOR_ID_XILINX,
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100603 .device = PCI_DEVICE_ID_XILINX_HAMMERFALL_DSP,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 .subvendor = PCI_ANY_ID,
605 .subdevice = PCI_ANY_ID,
606 }, /* RME Hammerfall-DSP */
607 { 0, },
608};
609
610MODULE_DEVICE_TABLE(pci, snd_hdsp_ids);
611
612/* prototypes */
Takashi Iwai55e957d2005-11-17 14:52:13 +0100613static int snd_hdsp_create_alsa_devices(struct snd_card *card, struct hdsp *hdsp);
614static int snd_hdsp_create_pcm(struct snd_card *card, struct hdsp *hdsp);
615static int snd_hdsp_enable_io (struct hdsp *hdsp);
616static void snd_hdsp_initialize_midi_flush (struct hdsp *hdsp);
617static void snd_hdsp_initialize_channels (struct hdsp *hdsp);
618static int hdsp_fifo_wait(struct hdsp *hdsp, int count, int timeout);
619static int hdsp_autosync_ref(struct hdsp *hdsp);
620static int snd_hdsp_set_defaults(struct hdsp *hdsp);
621static void snd_hdsp_9652_enable_mixer (struct hdsp *hdsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Takashi Iwai55e957d2005-11-17 14:52:13 +0100623static int hdsp_playback_to_output_key (struct hdsp *hdsp, int in, int out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
Remy Brunoa3a68c82007-08-31 12:33:54 +0200625 switch (hdsp->io_type) {
626 case Multiface:
627 case Digiface:
Florian Faber28b26e12010-12-01 12:14:47 +0100628 case RPM:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 default:
Andreas Degert192b8e32008-01-16 15:59:48 +0100630 if (hdsp->firmware_rev == 0xa)
631 return (64 * out) + (32 + (in));
632 else
633 return (52 * out) + (26 + (in));
Remy Brunoa3a68c82007-08-31 12:33:54 +0200634 case H9632:
635 return (32 * out) + (16 + (in));
636 case H9652:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 return (52 * out) + (26 + (in));
638 }
639}
640
Takashi Iwai55e957d2005-11-17 14:52:13 +0100641static int hdsp_input_to_output_key (struct hdsp *hdsp, int in, int out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
Remy Brunoa3a68c82007-08-31 12:33:54 +0200643 switch (hdsp->io_type) {
644 case Multiface:
645 case Digiface:
Florian Faber28b26e12010-12-01 12:14:47 +0100646 case RPM:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 default:
Andreas Degert192b8e32008-01-16 15:59:48 +0100648 if (hdsp->firmware_rev == 0xa)
649 return (64 * out) + in;
650 else
651 return (52 * out) + in;
Remy Brunoa3a68c82007-08-31 12:33:54 +0200652 case H9632:
653 return (32 * out) + in;
654 case H9652:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return (52 * out) + in;
656 }
657}
658
Takashi Iwai55e957d2005-11-17 14:52:13 +0100659static void hdsp_write(struct hdsp *hdsp, int reg, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
661 writel(val, hdsp->iobase + reg);
662}
663
Takashi Iwai55e957d2005-11-17 14:52:13 +0100664static unsigned int hdsp_read(struct hdsp *hdsp, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
666 return readl (hdsp->iobase + reg);
667}
668
Takashi Iwai55e957d2005-11-17 14:52:13 +0100669static int hdsp_check_for_iobox (struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
Adrian Knoth0c2bc7c2013-01-15 18:52:20 +0100671 int i;
672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 if (hdsp->io_type == H9652 || hdsp->io_type == H9632) return 0;
Adrian Knoth0c2bc7c2013-01-15 18:52:20 +0100674 for (i = 0; i < 500; i++) {
675 if (0 == (hdsp_read(hdsp, HDSP_statusRegister) &
676 HDSP_ConfigError)) {
677 if (i) {
Takashi Iwaia54ba0f2014-02-26 12:05:11 +0100678 dev_dbg(hdsp->card->dev,
679 "IO box found after %d ms\n",
Adrian Knoth0c2bc7c2013-01-15 18:52:20 +0100680 (20 * i));
681 }
682 return 0;
683 }
684 msleep(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 }
Takashi Iwaia54ba0f2014-02-26 12:05:11 +0100686 dev_err(hdsp->card->dev, "no IO box connected!\n");
Adrian Knoth0c2bc7c2013-01-15 18:52:20 +0100687 hdsp->state &= ~HDSP_FirmwareLoaded;
688 return -EIO;
Tim Blechmanne588ed82009-02-20 19:30:35 +0100689}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Tim Blechmanne588ed82009-02-20 19:30:35 +0100691static int hdsp_wait_for_iobox(struct hdsp *hdsp, unsigned int loops,
692 unsigned int delay)
693{
694 unsigned int i;
695
696 if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
697 return 0;
698
699 for (i = 0; i != loops; ++i) {
700 if (hdsp_read(hdsp, HDSP_statusRegister) & HDSP_ConfigError)
701 msleep(delay);
702 else {
Takashi Iwaia54ba0f2014-02-26 12:05:11 +0100703 dev_dbg(hdsp->card->dev, "iobox found after %ums!\n",
Tim Blechmanne588ed82009-02-20 19:30:35 +0100704 i * delay);
705 return 0;
706 }
707 }
708
Takashi Iwaia54ba0f2014-02-26 12:05:11 +0100709 dev_info(hdsp->card->dev, "no IO box connected!\n");
Tim Blechmanne588ed82009-02-20 19:30:35 +0100710 hdsp->state &= ~HDSP_FirmwareLoaded;
711 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712}
713
Takashi Iwai55e957d2005-11-17 14:52:13 +0100714static int snd_hdsp_load_firmware_from_cache(struct hdsp *hdsp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 int i;
717 unsigned long flags;
Takashi Iwai90caaef2012-11-22 16:55:11 +0100718 const u32 *cache;
719
720 if (hdsp->fw_uploaded)
721 cache = hdsp->fw_uploaded;
722 else {
723 if (!hdsp->firmware)
724 return -ENODEV;
725 cache = (u32 *)hdsp->firmware->data;
726 if (!cache)
727 return -ENODEV;
728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730 if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100731
Takashi Iwaia54ba0f2014-02-26 12:05:11 +0100732 dev_info(hdsp->card->dev, "loading firmware\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
734 hdsp_write (hdsp, HDSP_control2Reg, HDSP_S_PROGRAM);
735 hdsp_write (hdsp, HDSP_fifoData, 0);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 if (hdsp_fifo_wait (hdsp, 0, HDSP_LONG_WAIT)) {
Takashi Iwaia54ba0f2014-02-26 12:05:11 +0100738 dev_info(hdsp->card->dev,
739 "timeout waiting for download preparation\n");
Adrian Knoth0c2bc7c2013-01-15 18:52:20 +0100740 hdsp_write(hdsp, HDSP_control2Reg, HDSP_S200);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 return -EIO;
742 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 hdsp_write (hdsp, HDSP_control2Reg, HDSP_S_LOAD);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100745
Takashi Iwai90caaef2012-11-22 16:55:11 +0100746 for (i = 0; i < HDSP_FIRMWARE_SIZE / 4; ++i) {
747 hdsp_write(hdsp, HDSP_fifoData, cache[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 if (hdsp_fifo_wait (hdsp, 127, HDSP_LONG_WAIT)) {
Takashi Iwaia54ba0f2014-02-26 12:05:11 +0100749 dev_info(hdsp->card->dev,
750 "timeout during firmware loading\n");
Adrian Knoth0c2bc7c2013-01-15 18:52:20 +0100751 hdsp_write(hdsp, HDSP_control2Reg, HDSP_S200);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 return -EIO;
753 }
754 }
755
Adrian Knoth0c2bc7c2013-01-15 18:52:20 +0100756 hdsp_fifo_wait(hdsp, 3, HDSP_LONG_WAIT);
757 hdsp_write(hdsp, HDSP_control2Reg, HDSP_S200);
758
Takashi Iwaib0b98112005-10-20 18:29:58 +0200759 ssleep(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760#ifdef SNDRV_BIG_ENDIAN
761 hdsp->control2_register = HDSP_BIGENDIAN_MODE;
762#else
763 hdsp->control2_register = 0;
764#endif
765 hdsp_write (hdsp, HDSP_control2Reg, hdsp->control2_register);
Takashi Iwaia54ba0f2014-02-26 12:05:11 +0100766 dev_info(hdsp->card->dev, "finished firmware loading\n");
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 }
769 if (hdsp->state & HDSP_InitializationComplete) {
Takashi Iwaia54ba0f2014-02-26 12:05:11 +0100770 dev_info(hdsp->card->dev,
771 "firmware loaded from cache, restoring defaults\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 spin_lock_irqsave(&hdsp->lock, flags);
773 snd_hdsp_set_defaults(hdsp);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100774 spin_unlock_irqrestore(&hdsp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 hdsp->state |= HDSP_FirmwareLoaded;
778
779 return 0;
780}
781
Takashi Iwai55e957d2005-11-17 14:52:13 +0100782static int hdsp_get_iobox_version (struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
784 if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100785
Adrian Knoth0c2bc7c2013-01-15 18:52:20 +0100786 hdsp_write(hdsp, HDSP_control2Reg, HDSP_S_LOAD);
787 hdsp_write(hdsp, HDSP_fifoData, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Adrian Knoth0c2bc7c2013-01-15 18:52:20 +0100789 if (hdsp_fifo_wait(hdsp, 0, HDSP_SHORT_WAIT) < 0) {
790 hdsp_write(hdsp, HDSP_control2Reg, HDSP_S300);
Florian Faber28b26e12010-12-01 12:14:47 +0100791 hdsp_write(hdsp, HDSP_control2Reg, HDSP_S_LOAD);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100792 }
Adrian Knoth0c2bc7c2013-01-15 18:52:20 +0100793
794 hdsp_write(hdsp, HDSP_control2Reg, HDSP_S200 | HDSP_PROGRAM);
795 hdsp_write (hdsp, HDSP_fifoData, 0);
796 if (hdsp_fifo_wait(hdsp, 0, HDSP_SHORT_WAIT) < 0) {
797 hdsp->io_type = Multiface;
Takashi Iwaia54ba0f2014-02-26 12:05:11 +0100798 dev_info(hdsp->card->dev, "Multiface found\n");
Adrian Knoth0c2bc7c2013-01-15 18:52:20 +0100799 return 0;
800 }
801
802 hdsp_write(hdsp, HDSP_control2Reg, HDSP_S_LOAD);
803 hdsp_write(hdsp, HDSP_fifoData, 0);
804 if (hdsp_fifo_wait(hdsp, 0, HDSP_SHORT_WAIT) == 0) {
805 hdsp->io_type = Digiface;
Takashi Iwaia54ba0f2014-02-26 12:05:11 +0100806 dev_info(hdsp->card->dev, "Digiface found\n");
Adrian Knoth0c2bc7c2013-01-15 18:52:20 +0100807 return 0;
808 }
809
810 hdsp_write(hdsp, HDSP_control2Reg, HDSP_S300);
811 hdsp_write(hdsp, HDSP_control2Reg, HDSP_S_LOAD);
812 hdsp_write(hdsp, HDSP_fifoData, 0);
813 if (hdsp_fifo_wait(hdsp, 0, HDSP_SHORT_WAIT) == 0) {
814 hdsp->io_type = Multiface;
Takashi Iwaia54ba0f2014-02-26 12:05:11 +0100815 dev_info(hdsp->card->dev, "Multiface found\n");
Adrian Knoth0c2bc7c2013-01-15 18:52:20 +0100816 return 0;
817 }
818
819 hdsp_write(hdsp, HDSP_control2Reg, HDSP_S300);
820 hdsp_write(hdsp, HDSP_control2Reg, HDSP_S_LOAD);
821 hdsp_write(hdsp, HDSP_fifoData, 0);
822 if (hdsp_fifo_wait(hdsp, 0, HDSP_SHORT_WAIT) < 0) {
823 hdsp->io_type = Multiface;
Takashi Iwaia54ba0f2014-02-26 12:05:11 +0100824 dev_info(hdsp->card->dev, "Multiface found\n");
Adrian Knoth0c2bc7c2013-01-15 18:52:20 +0100825 return 0;
826 }
827
828 hdsp->io_type = RPM;
Takashi Iwaia54ba0f2014-02-26 12:05:11 +0100829 dev_info(hdsp->card->dev, "RPM found\n");
Adrian Knoth0c2bc7c2013-01-15 18:52:20 +0100830 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 } else {
832 /* firmware was already loaded, get iobox type */
Florian Faber28b26e12010-12-01 12:14:47 +0100833 if (hdsp_read(hdsp, HDSP_status2Register) & HDSP_version2)
834 hdsp->io_type = RPM;
835 else if (hdsp_read(hdsp, HDSP_status2Register) & HDSP_version1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 hdsp->io_type = Multiface;
Takashi Iwaib0b98112005-10-20 18:29:58 +0200837 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 hdsp->io_type = Digiface;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 }
840 return 0;
841}
842
843
Takashi Iwai92eed662008-02-22 18:35:56 +0100844static int hdsp_request_fw_loader(struct hdsp *hdsp);
Takashi Iwai311e70a2006-09-06 12:13:37 +0200845
846static int hdsp_check_for_firmware (struct hdsp *hdsp, int load_on_demand)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
Takashi Iwai311e70a2006-09-06 12:13:37 +0200848 if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
849 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 hdsp->state &= ~HDSP_FirmwareLoaded;
Takashi Iwai311e70a2006-09-06 12:13:37 +0200852 if (! load_on_demand)
Takashi Iwaib0b98112005-10-20 18:29:58 +0200853 return -EIO;
Takashi Iwaia54ba0f2014-02-26 12:05:11 +0100854 dev_err(hdsp->card->dev, "firmware not present.\n");
Takashi Iwaib0b98112005-10-20 18:29:58 +0200855 /* try to load firmware */
Takashi Iwai311e70a2006-09-06 12:13:37 +0200856 if (! (hdsp->state & HDSP_FirmwareCached)) {
Takashi Iwai311e70a2006-09-06 12:13:37 +0200857 if (! hdsp_request_fw_loader(hdsp))
858 return 0;
Takashi Iwaia54ba0f2014-02-26 12:05:11 +0100859 dev_err(hdsp->card->dev,
860 "No firmware loaded nor cached, please upload firmware.\n");
Takashi Iwai311e70a2006-09-06 12:13:37 +0200861 return -EIO;
Takashi Iwaib0b98112005-10-20 18:29:58 +0200862 }
Takashi Iwai311e70a2006-09-06 12:13:37 +0200863 if (snd_hdsp_load_firmware_from_cache(hdsp) != 0) {
Takashi Iwaia54ba0f2014-02-26 12:05:11 +0100864 dev_err(hdsp->card->dev,
865 "Firmware loading from cache failed, please upload manually.\n");
Takashi Iwai311e70a2006-09-06 12:13:37 +0200866 return -EIO;
867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 }
869 return 0;
870}
871
872
Takashi Iwai55e957d2005-11-17 14:52:13 +0100873static int hdsp_fifo_wait(struct hdsp *hdsp, int count, int timeout)
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100874{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 int i;
876
877 /* the fifoStatus registers reports on how many words
878 are available in the command FIFO.
879 */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 for (i = 0; i < timeout; i++) {
882
883 if ((int)(hdsp_read (hdsp, HDSP_fifoStatus) & 0xff) <= count)
884 return 0;
885
886 /* not very friendly, but we only do this during a firmware
887 load and changing the mixer, so we just put up with it.
888 */
889
890 udelay (100);
891 }
892
Takashi Iwaia54ba0f2014-02-26 12:05:11 +0100893 dev_warn(hdsp->card->dev,
894 "wait for FIFO status <= %d failed after %d iterations\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 count, timeout);
896 return -1;
897}
898
Takashi Iwai55e957d2005-11-17 14:52:13 +0100899static int hdsp_read_gain (struct hdsp *hdsp, unsigned int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
Takashi Iwaib0b98112005-10-20 18:29:58 +0200901 if (addr >= HDSP_MATRIX_MIXER_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 return 0;
Takashi Iwaib0b98112005-10-20 18:29:58 +0200903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 return hdsp->mixer_matrix[addr];
905}
906
Takashi Iwai55e957d2005-11-17 14:52:13 +0100907static int hdsp_write_gain(struct hdsp *hdsp, unsigned int addr, unsigned short data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
909 unsigned int ad;
910
911 if (addr >= HDSP_MATRIX_MIXER_SIZE)
912 return -1;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100913
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 if (hdsp->io_type == H9652 || hdsp->io_type == H9632) {
915
916 /* from martin bjornsen:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100917
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 "You can only write dwords to the
919 mixer memory which contain two
920 mixer values in the low and high
921 word. So if you want to change
922 value 0 you have to read value 1
923 from the cache and write both to
924 the first dword in the mixer
925 memory."
926 */
927
Takashi Iwaib0b98112005-10-20 18:29:58 +0200928 if (hdsp->io_type == H9632 && addr >= 512)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Takashi Iwaib0b98112005-10-20 18:29:58 +0200931 if (hdsp->io_type == H9652 && addr >= 1352)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
934 hdsp->mixer_matrix[addr] = data;
935
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 /* `addr' addresses a 16-bit wide address, but
938 the address space accessed via hdsp_write
939 uses byte offsets. put another way, addr
940 varies from 0 to 1351, but to access the
941 corresponding memory location, we need
942 to access 0 to 2703 ...
943 */
944 ad = addr/2;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100945
946 hdsp_write (hdsp, 4096 + (ad*4),
947 (hdsp->mixer_matrix[(addr&0x7fe)+1] << 16) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 hdsp->mixer_matrix[addr&0x7fe]);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 return 0;
951
952 } else {
953
954 ad = (addr << 16) + data;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100955
Takashi Iwaib0b98112005-10-20 18:29:58 +0200956 if (hdsp_fifo_wait(hdsp, 127, HDSP_LONG_WAIT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 hdsp_write (hdsp, HDSP_fifoData, ad);
960 hdsp->mixer_matrix[addr] = data;
961
962 }
963
964 return 0;
965}
966
Takashi Iwai55e957d2005-11-17 14:52:13 +0100967static int snd_hdsp_use_is_exclusive(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
969 unsigned long flags;
970 int ret = 1;
971
972 spin_lock_irqsave(&hdsp->lock, flags);
973 if ((hdsp->playback_pid != hdsp->capture_pid) &&
Takashi Iwaib0b98112005-10-20 18:29:58 +0200974 (hdsp->playback_pid >= 0) && (hdsp->capture_pid >= 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 spin_unlock_irqrestore(&hdsp->lock, flags);
977 return ret;
978}
979
Takashi Iwai55e957d2005-11-17 14:52:13 +0100980static int hdsp_spdif_sample_rate(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981{
982 unsigned int status = hdsp_read(hdsp, HDSP_statusRegister);
983 unsigned int rate_bits = (status & HDSP_spdifFrequencyMask);
984
Remy Bruno47ba97f2008-02-22 17:57:02 +0100985 /* For the 9632, the mask is different */
986 if (hdsp->io_type == H9632)
987 rate_bits = (status & HDSP_spdifFrequencyMask_9632);
988
Takashi Iwaib0b98112005-10-20 18:29:58 +0200989 if (status & HDSP_SPDIFErrorFlag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 return 0;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 switch (rate_bits) {
993 case HDSP_spdifFrequency32KHz: return 32000;
994 case HDSP_spdifFrequency44_1KHz: return 44100;
995 case HDSP_spdifFrequency48KHz: return 48000;
996 case HDSP_spdifFrequency64KHz: return 64000;
997 case HDSP_spdifFrequency88_2KHz: return 88200;
998 case HDSP_spdifFrequency96KHz: return 96000;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100999 case HDSP_spdifFrequency128KHz:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 if (hdsp->io_type == H9632) return 128000;
1001 break;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001002 case HDSP_spdifFrequency176_4KHz:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 if (hdsp->io_type == H9632) return 176400;
1004 break;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001005 case HDSP_spdifFrequency192KHz:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 if (hdsp->io_type == H9632) return 192000;
1007 break;
1008 default:
1009 break;
1010 }
Takashi Iwaia54ba0f2014-02-26 12:05:11 +01001011 dev_warn(hdsp->card->dev,
1012 "unknown spdif frequency status; bits = 0x%x, status = 0x%x\n",
1013 rate_bits, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 return 0;
1015}
1016
Remy Bruno47ba97f2008-02-22 17:57:02 +01001017static int hdsp_external_sample_rate(struct hdsp *hdsp)
1018{
1019 unsigned int status2 = hdsp_read(hdsp, HDSP_status2Register);
1020 unsigned int rate_bits = status2 & HDSP_systemFrequencyMask;
1021
1022 /* For the 9632 card, there seems to be no bit for indicating external
1023 * sample rate greater than 96kHz. The card reports the corresponding
1024 * single speed. So the best means seems to get spdif rate when
1025 * autosync reference is spdif */
1026 if (hdsp->io_type == H9632 &&
1027 hdsp_autosync_ref(hdsp) == HDSP_AUTOSYNC_FROM_SPDIF)
1028 return hdsp_spdif_sample_rate(hdsp);
1029
1030 switch (rate_bits) {
1031 case HDSP_systemFrequency32: return 32000;
1032 case HDSP_systemFrequency44_1: return 44100;
1033 case HDSP_systemFrequency48: return 48000;
1034 case HDSP_systemFrequency64: return 64000;
1035 case HDSP_systemFrequency88_2: return 88200;
1036 case HDSP_systemFrequency96: return 96000;
1037 default:
1038 return 0;
1039 }
1040}
1041
Takashi Iwai55e957d2005-11-17 14:52:13 +01001042static void hdsp_compute_period_size(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
1044 hdsp->period_bytes = 1 << ((hdsp_decode_latency(hdsp->control_register) + 8));
1045}
1046
Takashi Iwai55e957d2005-11-17 14:52:13 +01001047static snd_pcm_uframes_t hdsp_hw_pointer(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048{
1049 int position;
1050
1051 position = hdsp_read(hdsp, HDSP_statusRegister);
1052
Takashi Iwaib0b98112005-10-20 18:29:58 +02001053 if (!hdsp->precise_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 return (position & HDSP_BufferID) ? (hdsp->period_bytes / 4) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
1056 position &= HDSP_BufferPositionMask;
1057 position /= 4;
1058 position &= (hdsp->period_bytes/2) - 1;
1059 return position;
1060}
1061
Takashi Iwai55e957d2005-11-17 14:52:13 +01001062static void hdsp_reset_hw_pointer(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063{
1064 hdsp_write (hdsp, HDSP_resetPointer, 0);
Remy Brunod7923b22006-10-17 12:41:56 +02001065 if (hdsp->io_type == H9632 && hdsp->firmware_rev >= 152)
1066 /* HDSP_resetPointer = HDSP_freqReg, which is strange and
1067 * requires (?) to write again DDS value after a reset pointer
1068 * (at least, it works like this) */
1069 hdsp_write (hdsp, HDSP_freqReg, hdsp->dds_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070}
1071
Takashi Iwai55e957d2005-11-17 14:52:13 +01001072static void hdsp_start_audio(struct hdsp *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073{
1074 s->control_register |= (HDSP_AudioInterruptEnable | HDSP_Start);
1075 hdsp_write(s, HDSP_controlRegister, s->control_register);
1076}
1077
Takashi Iwai55e957d2005-11-17 14:52:13 +01001078static void hdsp_stop_audio(struct hdsp *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
1080 s->control_register &= ~(HDSP_Start | HDSP_AudioInterruptEnable);
1081 hdsp_write(s, HDSP_controlRegister, s->control_register);
1082}
1083
Takashi Iwai55e957d2005-11-17 14:52:13 +01001084static void hdsp_silence_playback(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085{
1086 memset(hdsp->playback_buffer, 0, HDSP_DMA_AREA_BYTES);
1087}
1088
Takashi Iwai55e957d2005-11-17 14:52:13 +01001089static int hdsp_set_interrupt_interval(struct hdsp *s, unsigned int frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
1091 int n;
1092
1093 spin_lock_irq(&s->lock);
1094
1095 frames >>= 7;
1096 n = 0;
1097 while (frames) {
1098 n++;
1099 frames >>= 1;
1100 }
1101
1102 s->control_register &= ~HDSP_LatencyMask;
1103 s->control_register |= hdsp_encode_latency(n);
1104
1105 hdsp_write(s, HDSP_controlRegister, s->control_register);
1106
1107 hdsp_compute_period_size(s);
1108
1109 spin_unlock_irq(&s->lock);
1110
1111 return 0;
1112}
1113
Remy Brunod7923b22006-10-17 12:41:56 +02001114static void hdsp_set_dds_value(struct hdsp *hdsp, int rate)
1115{
1116 u64 n;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001117
Remy Brunod7923b22006-10-17 12:41:56 +02001118 if (rate >= 112000)
1119 rate /= 4;
1120 else if (rate >= 56000)
1121 rate /= 2;
1122
Julian Cablee4b60882007-03-19 11:44:40 +01001123 n = DDS_NUMERATOR;
Takashi Iwai3f7440a2009-06-05 17:40:04 +02001124 n = div_u64(n, rate);
Remy Brunod7923b22006-10-17 12:41:56 +02001125 /* n should be less than 2^32 for being written to FREQ register */
Takashi Iwaida3cec32008-08-08 17:12:14 +02001126 snd_BUG_ON(n >> 32);
Remy Brunod7923b22006-10-17 12:41:56 +02001127 /* HDSP_freqReg and HDSP_resetPointer are the same, so keep the DDS
1128 value to write it after a reset */
1129 hdsp->dds_value = n;
1130 hdsp_write(hdsp, HDSP_freqReg, hdsp->dds_value);
1131}
1132
Takashi Iwai55e957d2005-11-17 14:52:13 +01001133static int hdsp_set_rate(struct hdsp *hdsp, int rate, int called_internally)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134{
1135 int reject_if_open = 0;
1136 int current_rate;
1137 int rate_bits;
1138
1139 /* ASSUMPTION: hdsp->lock is either held, or
1140 there is no need for it (e.g. during module
1141 initialization).
1142 */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001143
1144 if (!(hdsp->control_register & HDSP_ClockModeMaster)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 if (called_internally) {
1146 /* request from ctl or card initialization */
Takashi Iwaia54ba0f2014-02-26 12:05:11 +01001147 dev_err(hdsp->card->dev,
1148 "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 Iwaib0b98112005-10-20 18:29:58 +02001155 if ((spdif_freq == external_freq*2) && (hdsp_autosync_ref(hdsp) >= HDSP_AUTOSYNC_FROM_ADAT1))
Takashi Iwaia54ba0f2014-02-26 12:05:11 +01001156 dev_info(hdsp->card->dev,
1157 "Detected ADAT in double speed mode\n");
Takashi Iwaib0b98112005-10-20 18:29:58 +02001158 else if (hdsp->io_type == H9632 && (spdif_freq == external_freq*4) && (hdsp_autosync_ref(hdsp) >= HDSP_AUTOSYNC_FROM_ADAT1))
Takashi Iwaia54ba0f2014-02-26 12:05:11 +01001159 dev_info(hdsp->card->dev,
1160 "Detected ADAT in quad speed mode\n");
Takashi Iwaib0b98112005-10-20 18:29:58 +02001161 else if (rate != external_freq) {
Takashi Iwaia54ba0f2014-02-26 12:05:11 +01001162 dev_info(hdsp->card->dev,
1163 "No AutoSync source for requested rate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 return -1;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001165 }
1166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 }
1168
1169 current_rate = hdsp->system_sample_rate;
1170
1171 /* Changing from a "single speed" to a "double speed" rate is
1172 not allowed if any substreams are open. This is because
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001173 such a change causes a shift in the location of
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 the DMA buffers and a reduction in the number of available
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001175 buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
1177 Note that a similar but essentially insoluble problem
1178 exists for externally-driven rate changes. All we can do
1179 is to flag rate changes in the read/write routines. */
1180
Takashi Iwaib0b98112005-10-20 18:29:58 +02001181 if (rate > 96000 && hdsp->io_type != H9632)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 return -EINVAL;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 switch (rate) {
1185 case 32000:
Takashi Iwaib0b98112005-10-20 18:29:58 +02001186 if (current_rate > 48000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 rate_bits = HDSP_Frequency32KHz;
1189 break;
1190 case 44100:
Takashi Iwaib0b98112005-10-20 18:29:58 +02001191 if (current_rate > 48000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 rate_bits = HDSP_Frequency44_1KHz;
1194 break;
1195 case 48000:
Takashi Iwaib0b98112005-10-20 18:29:58 +02001196 if (current_rate > 48000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 rate_bits = HDSP_Frequency48KHz;
1199 break;
1200 case 64000:
Takashi Iwaib0b98112005-10-20 18:29:58 +02001201 if (current_rate <= 48000 || current_rate > 96000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 rate_bits = HDSP_Frequency64KHz;
1204 break;
1205 case 88200:
Takashi Iwaib0b98112005-10-20 18:29:58 +02001206 if (current_rate <= 48000 || current_rate > 96000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 rate_bits = HDSP_Frequency88_2KHz;
1209 break;
1210 case 96000:
Takashi Iwaib0b98112005-10-20 18:29:58 +02001211 if (current_rate <= 48000 || current_rate > 96000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 rate_bits = HDSP_Frequency96KHz;
1214 break;
1215 case 128000:
Takashi Iwaib0b98112005-10-20 18:29:58 +02001216 if (current_rate < 128000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 rate_bits = HDSP_Frequency128KHz;
1219 break;
1220 case 176400:
Takashi Iwaib0b98112005-10-20 18:29:58 +02001221 if (current_rate < 128000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 rate_bits = HDSP_Frequency176_4KHz;
1224 break;
1225 case 192000:
Takashi Iwaib0b98112005-10-20 18:29:58 +02001226 if (current_rate < 128000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 rate_bits = HDSP_Frequency192KHz;
1229 break;
1230 default:
1231 return -EINVAL;
1232 }
1233
1234 if (reject_if_open && (hdsp->capture_pid >= 0 || hdsp->playback_pid >= 0)) {
Takashi Iwaia54ba0f2014-02-26 12:05:11 +01001235 dev_warn(hdsp->card->dev,
1236 "cannot change speed mode (capture PID = %d, playback PID = %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 hdsp->capture_pid,
1238 hdsp->playback_pid);
1239 return -EBUSY;
1240 }
1241
1242 hdsp->control_register &= ~HDSP_FrequencyMask;
1243 hdsp->control_register |= rate_bits;
1244 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1245
Remy Brunod7923b22006-10-17 12:41:56 +02001246 /* For HDSP9632 rev 152, need to set DDS value in FREQ register */
1247 if (hdsp->io_type == H9632 && hdsp->firmware_rev >= 152)
1248 hdsp_set_dds_value(hdsp, rate);
1249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 if (rate >= 128000) {
1251 hdsp->channel_map = channel_map_H9632_qs;
1252 } else if (rate > 48000) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02001253 if (hdsp->io_type == H9632)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 hdsp->channel_map = channel_map_H9632_ds;
Takashi Iwaib0b98112005-10-20 18:29:58 +02001255 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 hdsp->channel_map = channel_map_ds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 } else {
1258 switch (hdsp->io_type) {
Florian Faber28b26e12010-12-01 12:14:47 +01001259 case RPM:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 case Multiface:
1261 hdsp->channel_map = channel_map_mf_ss;
1262 break;
1263 case Digiface:
1264 case H9652:
1265 hdsp->channel_map = channel_map_df_ss;
1266 break;
1267 case H9632:
1268 hdsp->channel_map = channel_map_H9632_ss;
1269 break;
1270 default:
1271 /* should never happen */
1272 break;
1273 }
1274 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 hdsp->system_sample_rate = rate;
1277
1278 return 0;
1279}
1280
1281/*----------------------------------------------------------------------------
1282 MIDI
1283 ----------------------------------------------------------------------------*/
1284
Takashi Iwai55e957d2005-11-17 14:52:13 +01001285static unsigned char snd_hdsp_midi_read_byte (struct hdsp *hdsp, int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286{
1287 /* the hardware already does the relevant bit-mask with 0xff */
Takashi Iwaib0b98112005-10-20 18:29:58 +02001288 if (id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 return hdsp_read(hdsp, HDSP_midiDataIn1);
Takashi Iwaib0b98112005-10-20 18:29:58 +02001290 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 return hdsp_read(hdsp, HDSP_midiDataIn0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292}
1293
Takashi Iwai55e957d2005-11-17 14:52:13 +01001294static void snd_hdsp_midi_write_byte (struct hdsp *hdsp, int id, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295{
1296 /* the hardware already does the relevant bit-mask with 0xff */
Takashi Iwaib0b98112005-10-20 18:29:58 +02001297 if (id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 hdsp_write(hdsp, HDSP_midiDataOut1, val);
Takashi Iwaib0b98112005-10-20 18:29:58 +02001299 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 hdsp_write(hdsp, HDSP_midiDataOut0, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301}
1302
Takashi Iwai55e957d2005-11-17 14:52:13 +01001303static int snd_hdsp_midi_input_available (struct hdsp *hdsp, int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304{
Takashi Iwaib0b98112005-10-20 18:29:58 +02001305 if (id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 return (hdsp_read(hdsp, HDSP_midiStatusIn1) & 0xff);
Takashi Iwaib0b98112005-10-20 18:29:58 +02001307 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 return (hdsp_read(hdsp, HDSP_midiStatusIn0) & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309}
1310
Takashi Iwai55e957d2005-11-17 14:52:13 +01001311static int snd_hdsp_midi_output_possible (struct hdsp *hdsp, int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312{
1313 int fifo_bytes_used;
1314
Takashi Iwaib0b98112005-10-20 18:29:58 +02001315 if (id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 fifo_bytes_used = hdsp_read(hdsp, HDSP_midiStatusOut1) & 0xff;
Takashi Iwaib0b98112005-10-20 18:29:58 +02001317 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 fifo_bytes_used = hdsp_read(hdsp, HDSP_midiStatusOut0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Takashi Iwaib0b98112005-10-20 18:29:58 +02001320 if (fifo_bytes_used < 128)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 return 128 - fifo_bytes_used;
Takashi Iwaib0b98112005-10-20 18:29:58 +02001322 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324}
1325
Takashi Iwai55e957d2005-11-17 14:52:13 +01001326static void snd_hdsp_flush_midi_input (struct hdsp *hdsp, int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327{
Takashi Iwaib0b98112005-10-20 18:29:58 +02001328 while (snd_hdsp_midi_input_available (hdsp, id))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 snd_hdsp_midi_read_byte (hdsp, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330}
1331
Takashi Iwai55e957d2005-11-17 14:52:13 +01001332static int snd_hdsp_midi_output_write (struct hdsp_midi *hmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333{
1334 unsigned long flags;
1335 int n_pending;
1336 int to_write;
1337 int i;
1338 unsigned char buf[128];
1339
1340 /* Output is not interrupt driven */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001341
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 spin_lock_irqsave (&hmidi->lock, flags);
1343 if (hmidi->output) {
1344 if (!snd_rawmidi_transmit_empty (hmidi->output)) {
1345 if ((n_pending = snd_hdsp_midi_output_possible (hmidi->hdsp, hmidi->id)) > 0) {
1346 if (n_pending > (int)sizeof (buf))
1347 n_pending = sizeof (buf);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001348
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 if ((to_write = snd_rawmidi_transmit (hmidi->output, buf, n_pending)) > 0) {
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001350 for (i = 0; i < to_write; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 snd_hdsp_midi_write_byte (hmidi->hdsp, hmidi->id, buf[i]);
1352 }
1353 }
1354 }
1355 }
1356 spin_unlock_irqrestore (&hmidi->lock, flags);
1357 return 0;
1358}
1359
Takashi Iwai55e957d2005-11-17 14:52:13 +01001360static int snd_hdsp_midi_input_read (struct hdsp_midi *hmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361{
1362 unsigned char buf[128]; /* this buffer is designed to match the MIDI input FIFO size */
1363 unsigned long flags;
1364 int n_pending;
1365 int i;
1366
1367 spin_lock_irqsave (&hmidi->lock, flags);
1368 if ((n_pending = snd_hdsp_midi_input_available (hmidi->hdsp, hmidi->id)) > 0) {
1369 if (hmidi->input) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02001370 if (n_pending > (int)sizeof (buf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 n_pending = sizeof (buf);
Takashi Iwaib0b98112005-10-20 18:29:58 +02001372 for (i = 0; i < n_pending; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 buf[i] = snd_hdsp_midi_read_byte (hmidi->hdsp, hmidi->id);
Takashi Iwaib0b98112005-10-20 18:29:58 +02001374 if (n_pending)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 snd_rawmidi_receive (hmidi->input, buf, n_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 } else {
1377 /* flush the MIDI input FIFO */
Takashi Iwaib0b98112005-10-20 18:29:58 +02001378 while (--n_pending)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 snd_hdsp_midi_read_byte (hmidi->hdsp, hmidi->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 }
1381 }
1382 hmidi->pending = 0;
Takashi Iwaib0b98112005-10-20 18:29:58 +02001383 if (hmidi->id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 hmidi->hdsp->control_register |= HDSP_Midi1InterruptEnable;
Takashi Iwaib0b98112005-10-20 18:29:58 +02001385 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 hmidi->hdsp->control_register |= HDSP_Midi0InterruptEnable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 hdsp_write(hmidi->hdsp, HDSP_controlRegister, hmidi->hdsp->control_register);
1388 spin_unlock_irqrestore (&hmidi->lock, flags);
1389 return snd_hdsp_midi_output_write (hmidi);
1390}
1391
Takashi Iwai55e957d2005-11-17 14:52:13 +01001392static void snd_hdsp_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001394 struct hdsp *hdsp;
1395 struct hdsp_midi *hmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 unsigned long flags;
1397 u32 ie;
1398
Takashi Iwai55e957d2005-11-17 14:52:13 +01001399 hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 hdsp = hmidi->hdsp;
1401 ie = hmidi->id ? HDSP_Midi1InterruptEnable : HDSP_Midi0InterruptEnable;
1402 spin_lock_irqsave (&hdsp->lock, flags);
1403 if (up) {
1404 if (!(hdsp->control_register & ie)) {
1405 snd_hdsp_flush_midi_input (hdsp, hmidi->id);
1406 hdsp->control_register |= ie;
1407 }
1408 } else {
1409 hdsp->control_register &= ~ie;
1410 tasklet_kill(&hdsp->midi_tasklet);
1411 }
1412
1413 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1414 spin_unlock_irqrestore (&hdsp->lock, flags);
1415}
1416
1417static void snd_hdsp_midi_output_timer(unsigned long data)
1418{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001419 struct hdsp_midi *hmidi = (struct hdsp_midi *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 unsigned long flags;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001421
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 snd_hdsp_midi_output_write(hmidi);
1423 spin_lock_irqsave (&hmidi->lock, flags);
1424
1425 /* this does not bump hmidi->istimer, because the
1426 kernel automatically removed the timer when it
1427 expired, and we are now adding it back, thus
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001428 leaving istimer wherever it was set before.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 */
1430
Takashi Iwaic41c0092015-01-19 11:34:36 +01001431 if (hmidi->istimer)
1432 mod_timer(&hmidi->timer, 1 + jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
1434 spin_unlock_irqrestore (&hmidi->lock, flags);
1435}
1436
Takashi Iwai55e957d2005-11-17 14:52:13 +01001437static void snd_hdsp_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001439 struct hdsp_midi *hmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 unsigned long flags;
1441
Takashi Iwai55e957d2005-11-17 14:52:13 +01001442 hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 spin_lock_irqsave (&hmidi->lock, flags);
1444 if (up) {
1445 if (!hmidi->istimer) {
Takashi Iwaic41c0092015-01-19 11:34:36 +01001446 setup_timer(&hmidi->timer, snd_hdsp_midi_output_timer,
1447 (unsigned long) hmidi);
1448 mod_timer(&hmidi->timer, 1 + jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 hmidi->istimer++;
1450 }
1451 } else {
Takashi Iwaib0b98112005-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 Iwaib0b98112005-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{
Takashi Iwai8d678da2014-10-20 18:19:34 +02001679 static const char * const texts[4] = {
1680 "Optical", "Coaxial", "Internal", "AES"
1681 };
Takashi Iwai55e957d2005-11-17 14:52:13 +01001682 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
Takashi Iwai8d678da2014-10-20 18:19:34 +02001684 return snd_ctl_enum_info(uinfo, 1, (hdsp->io_type == H9632) ? 4 : 3,
1685 texts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686}
1687
Takashi Iwai55e957d2005-11-17 14:52:13 +01001688static int snd_hdsp_get_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001690 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001691
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 ucontrol->value.enumerated.item[0] = hdsp_spdif_in(hdsp);
1693 return 0;
1694}
1695
Takashi Iwai55e957d2005-11-17 14:52:13 +01001696static int snd_hdsp_put_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001698 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 int change;
1700 unsigned int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001701
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 if (!snd_hdsp_use_is_exclusive(hdsp))
1703 return -EBUSY;
1704 val = ucontrol->value.enumerated.item[0] % ((hdsp->io_type == H9632) ? 4 : 3);
1705 spin_lock_irq(&hdsp->lock);
1706 change = val != hdsp_spdif_in(hdsp);
1707 if (change)
1708 hdsp_set_spdif_input(hdsp, val);
1709 spin_unlock_irq(&hdsp->lock);
1710 return change;
1711}
1712
Adrian Knoth66d92442013-01-15 18:52:21 +01001713#define HDSP_TOGGLE_SETTING(xname, xindex) \
1714{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1715 .name = xname, \
1716 .private_value = xindex, \
1717 .info = snd_hdsp_info_toggle_setting, \
1718 .get = snd_hdsp_get_toggle_setting, \
1719 .put = snd_hdsp_put_toggle_setting \
1720}
1721
1722static int hdsp_toggle_setting(struct hdsp *hdsp, u32 regmask)
1723{
1724 return (hdsp->control_register & regmask) ? 1 : 0;
1725}
1726
1727static int hdsp_set_toggle_setting(struct hdsp *hdsp, u32 regmask, int out)
1728{
1729 if (out)
1730 hdsp->control_register |= regmask;
1731 else
1732 hdsp->control_register &= ~regmask;
1733 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1734
1735 return 0;
1736}
1737
1738#define snd_hdsp_info_toggle_setting snd_ctl_boolean_mono_info
1739
1740static int snd_hdsp_get_toggle_setting(struct snd_kcontrol *kcontrol,
1741 struct snd_ctl_elem_value *ucontrol)
1742{
1743 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
1744 u32 regmask = kcontrol->private_value;
1745
1746 spin_lock_irq(&hdsp->lock);
1747 ucontrol->value.integer.value[0] = hdsp_toggle_setting(hdsp, regmask);
1748 spin_unlock_irq(&hdsp->lock);
1749 return 0;
1750}
1751
1752static int snd_hdsp_put_toggle_setting(struct snd_kcontrol *kcontrol,
1753 struct snd_ctl_elem_value *ucontrol)
1754{
1755 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
1756 u32 regmask = kcontrol->private_value;
1757 int change;
1758 unsigned int val;
1759
1760 if (!snd_hdsp_use_is_exclusive(hdsp))
1761 return -EBUSY;
1762 val = ucontrol->value.integer.value[0] & 1;
1763 spin_lock_irq(&hdsp->lock);
1764 change = (int) val != hdsp_toggle_setting(hdsp, regmask);
1765 if (change)
1766 hdsp_set_toggle_setting(hdsp, regmask, val);
1767 spin_unlock_irq(&hdsp->lock);
1768 return change;
1769}
1770
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771#define HDSP_SPDIF_SAMPLE_RATE(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001772{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 .name = xname, \
1774 .index = xindex, \
1775 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
1776 .info = snd_hdsp_info_spdif_sample_rate, \
1777 .get = snd_hdsp_get_spdif_sample_rate \
1778}
1779
Takashi Iwai55e957d2005-11-17 14:52:13 +01001780static int snd_hdsp_info_spdif_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781{
Takashi Iwai8d678da2014-10-20 18:19:34 +02001782 static const char * const texts[] = {
1783 "32000", "44100", "48000", "64000", "88200", "96000",
1784 "None", "128000", "176400", "192000"
1785 };
Takashi Iwai55e957d2005-11-17 14:52:13 +01001786 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Takashi Iwai8d678da2014-10-20 18:19:34 +02001788 return snd_ctl_enum_info(uinfo, 1, (hdsp->io_type == H9632) ? 10 : 7,
1789 texts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790}
1791
Takashi Iwai55e957d2005-11-17 14:52:13 +01001792static int snd_hdsp_get_spdif_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001794 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001795
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 switch (hdsp_spdif_sample_rate(hdsp)) {
1797 case 32000:
1798 ucontrol->value.enumerated.item[0] = 0;
1799 break;
1800 case 44100:
1801 ucontrol->value.enumerated.item[0] = 1;
1802 break;
1803 case 48000:
1804 ucontrol->value.enumerated.item[0] = 2;
1805 break;
1806 case 64000:
1807 ucontrol->value.enumerated.item[0] = 3;
1808 break;
1809 case 88200:
1810 ucontrol->value.enumerated.item[0] = 4;
1811 break;
1812 case 96000:
1813 ucontrol->value.enumerated.item[0] = 5;
1814 break;
1815 case 128000:
1816 ucontrol->value.enumerated.item[0] = 7;
1817 break;
1818 case 176400:
1819 ucontrol->value.enumerated.item[0] = 8;
1820 break;
1821 case 192000:
1822 ucontrol->value.enumerated.item[0] = 9;
1823 break;
1824 default:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001825 ucontrol->value.enumerated.item[0] = 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 }
1827 return 0;
1828}
1829
1830#define HDSP_SYSTEM_SAMPLE_RATE(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001831{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 .name = xname, \
1833 .index = xindex, \
1834 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
1835 .info = snd_hdsp_info_system_sample_rate, \
1836 .get = snd_hdsp_get_system_sample_rate \
1837}
1838
Takashi Iwai55e957d2005-11-17 14:52:13 +01001839static int snd_hdsp_info_system_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840{
1841 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1842 uinfo->count = 1;
1843 return 0;
1844}
1845
Takashi Iwai55e957d2005-11-17 14:52:13 +01001846static int snd_hdsp_get_system_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001848 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001849
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 ucontrol->value.enumerated.item[0] = hdsp->system_sample_rate;
1851 return 0;
1852}
1853
1854#define HDSP_AUTOSYNC_SAMPLE_RATE(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001855{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 .name = xname, \
1857 .index = xindex, \
1858 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
1859 .info = snd_hdsp_info_autosync_sample_rate, \
1860 .get = snd_hdsp_get_autosync_sample_rate \
1861}
1862
Takashi Iwai55e957d2005-11-17 14:52:13 +01001863static int snd_hdsp_info_autosync_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001865 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Takashi Iwai8d678da2014-10-20 18:19:34 +02001866 static const char * const texts[] = {
1867 "32000", "44100", "48000", "64000", "88200", "96000",
1868 "None", "128000", "176400", "192000"
1869 };
1870
1871 return snd_ctl_enum_info(uinfo, 1, (hdsp->io_type == H9632) ? 10 : 7,
1872 texts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873}
1874
Takashi Iwai55e957d2005-11-17 14:52:13 +01001875static int snd_hdsp_get_autosync_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001877 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001878
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 switch (hdsp_external_sample_rate(hdsp)) {
1880 case 32000:
1881 ucontrol->value.enumerated.item[0] = 0;
1882 break;
1883 case 44100:
1884 ucontrol->value.enumerated.item[0] = 1;
1885 break;
1886 case 48000:
1887 ucontrol->value.enumerated.item[0] = 2;
1888 break;
1889 case 64000:
1890 ucontrol->value.enumerated.item[0] = 3;
1891 break;
1892 case 88200:
1893 ucontrol->value.enumerated.item[0] = 4;
1894 break;
1895 case 96000:
1896 ucontrol->value.enumerated.item[0] = 5;
1897 break;
1898 case 128000:
1899 ucontrol->value.enumerated.item[0] = 7;
1900 break;
1901 case 176400:
1902 ucontrol->value.enumerated.item[0] = 8;
1903 break;
1904 case 192000:
1905 ucontrol->value.enumerated.item[0] = 9;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001906 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 default:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001908 ucontrol->value.enumerated.item[0] = 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 }
1910 return 0;
1911}
1912
1913#define HDSP_SYSTEM_CLOCK_MODE(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001914{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 .name = xname, \
1916 .index = xindex, \
1917 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
1918 .info = snd_hdsp_info_system_clock_mode, \
1919 .get = snd_hdsp_get_system_clock_mode \
1920}
1921
Takashi Iwai55e957d2005-11-17 14:52:13 +01001922static int hdsp_system_clock_mode(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923{
Takashi Iwaib0b98112005-10-20 18:29:58 +02001924 if (hdsp->control_register & HDSP_ClockModeMaster)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 return 0;
Takashi Iwaib0b98112005-10-20 18:29:58 +02001926 else if (hdsp_external_sample_rate(hdsp) != hdsp->system_sample_rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 return 1;
1929}
1930
Takashi Iwai55e957d2005-11-17 14:52:13 +01001931static int snd_hdsp_info_system_clock_mode(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932{
Takashi Iwai8d678da2014-10-20 18:19:34 +02001933 static const char * const texts[] = {"Master", "Slave" };
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001934
Takashi Iwai8d678da2014-10-20 18:19:34 +02001935 return snd_ctl_enum_info(uinfo, 1, 2, texts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936}
1937
Takashi Iwai55e957d2005-11-17 14:52:13 +01001938static int snd_hdsp_get_system_clock_mode(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001940 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001941
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 ucontrol->value.enumerated.item[0] = hdsp_system_clock_mode(hdsp);
1943 return 0;
1944}
1945
1946#define HDSP_CLOCK_SOURCE(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001947{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 .name = xname, \
1949 .index = xindex, \
1950 .info = snd_hdsp_info_clock_source, \
1951 .get = snd_hdsp_get_clock_source, \
1952 .put = snd_hdsp_put_clock_source \
1953}
1954
Takashi Iwai55e957d2005-11-17 14:52:13 +01001955static int hdsp_clock_source(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956{
1957 if (hdsp->control_register & HDSP_ClockModeMaster) {
1958 switch (hdsp->system_sample_rate) {
1959 case 32000:
1960 return 1;
1961 case 44100:
1962 return 2;
1963 case 48000:
1964 return 3;
1965 case 64000:
1966 return 4;
1967 case 88200:
1968 return 5;
1969 case 96000:
1970 return 6;
1971 case 128000:
1972 return 7;
1973 case 176400:
1974 return 8;
1975 case 192000:
1976 return 9;
1977 default:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001978 return 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 }
1980 } else {
1981 return 0;
1982 }
1983}
1984
Takashi Iwai55e957d2005-11-17 14:52:13 +01001985static int hdsp_set_clock_source(struct hdsp *hdsp, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986{
1987 int rate;
1988 switch (mode) {
1989 case HDSP_CLOCK_SOURCE_AUTOSYNC:
1990 if (hdsp_external_sample_rate(hdsp) != 0) {
1991 if (!hdsp_set_rate(hdsp, hdsp_external_sample_rate(hdsp), 1)) {
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001992 hdsp->control_register &= ~HDSP_ClockModeMaster;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1994 return 0;
1995 }
1996 }
1997 return -1;
1998 case HDSP_CLOCK_SOURCE_INTERNAL_32KHZ:
1999 rate = 32000;
2000 break;
2001 case HDSP_CLOCK_SOURCE_INTERNAL_44_1KHZ:
2002 rate = 44100;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002003 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 case HDSP_CLOCK_SOURCE_INTERNAL_48KHZ:
2005 rate = 48000;
2006 break;
2007 case HDSP_CLOCK_SOURCE_INTERNAL_64KHZ:
2008 rate = 64000;
2009 break;
2010 case HDSP_CLOCK_SOURCE_INTERNAL_88_2KHZ:
2011 rate = 88200;
2012 break;
2013 case HDSP_CLOCK_SOURCE_INTERNAL_96KHZ:
2014 rate = 96000;
2015 break;
2016 case HDSP_CLOCK_SOURCE_INTERNAL_128KHZ:
2017 rate = 128000;
2018 break;
2019 case HDSP_CLOCK_SOURCE_INTERNAL_176_4KHZ:
2020 rate = 176400;
2021 break;
2022 case HDSP_CLOCK_SOURCE_INTERNAL_192KHZ:
2023 rate = 192000;
2024 break;
2025 default:
2026 rate = 48000;
2027 }
2028 hdsp->control_register |= HDSP_ClockModeMaster;
2029 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2030 hdsp_set_rate(hdsp, rate, 1);
2031 return 0;
2032}
2033
Takashi Iwai55e957d2005-11-17 14:52:13 +01002034static int snd_hdsp_info_clock_source(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035{
Takashi Iwai8d678da2014-10-20 18:19:34 +02002036 static const char * const texts[] = {
2037 "AutoSync", "Internal 32.0 kHz", "Internal 44.1 kHz",
2038 "Internal 48.0 kHz", "Internal 64.0 kHz", "Internal 88.2 kHz",
2039 "Internal 96.0 kHz", "Internal 128 kHz", "Internal 176.4 kHz",
2040 "Internal 192.0 KHz"
2041 };
Takashi Iwai55e957d2005-11-17 14:52:13 +01002042 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002043
Takashi Iwai8d678da2014-10-20 18:19:34 +02002044 return snd_ctl_enum_info(uinfo, 1, (hdsp->io_type == H9632) ? 10 : 7,
2045 texts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046}
2047
Takashi Iwai55e957d2005-11-17 14:52:13 +01002048static int snd_hdsp_get_clock_source(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002050 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002051
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 ucontrol->value.enumerated.item[0] = hdsp_clock_source(hdsp);
2053 return 0;
2054}
2055
Takashi Iwai55e957d2005-11-17 14:52:13 +01002056static int snd_hdsp_put_clock_source(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002058 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 int change;
2060 int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002061
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 if (!snd_hdsp_use_is_exclusive(hdsp))
2063 return -EBUSY;
2064 val = ucontrol->value.enumerated.item[0];
2065 if (val < 0) val = 0;
2066 if (hdsp->io_type == H9632) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02002067 if (val > 9)
2068 val = 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 } else {
Takashi Iwaib0b98112005-10-20 18:29:58 +02002070 if (val > 6)
2071 val = 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 }
2073 spin_lock_irq(&hdsp->lock);
Takashi Iwaib0b98112005-10-20 18:29:58 +02002074 if (val != hdsp_clock_source(hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 change = (hdsp_set_clock_source(hdsp, val) == 0) ? 1 : 0;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002076 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 spin_unlock_irq(&hdsp->lock);
2079 return change;
2080}
2081
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002082#define snd_hdsp_info_clock_source_lock snd_ctl_boolean_mono_info
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02002083
Takashi Iwai55e957d2005-11-17 14:52:13 +01002084static int snd_hdsp_get_clock_source_lock(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02002085{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002086 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002087
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02002088 ucontrol->value.integer.value[0] = hdsp->clock_source_locked;
2089 return 0;
2090}
2091
Takashi Iwai55e957d2005-11-17 14:52:13 +01002092static int snd_hdsp_put_clock_source_lock(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02002093{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002094 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02002095 int change;
2096
2097 change = (int)ucontrol->value.integer.value[0] != hdsp->clock_source_locked;
2098 if (change)
Takashi Iwai4e98d6a2007-11-15 15:58:13 +01002099 hdsp->clock_source_locked = !!ucontrol->value.integer.value[0];
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02002100 return change;
2101}
2102
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103#define HDSP_DA_GAIN(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002104{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 .name = xname, \
2106 .index = xindex, \
2107 .info = snd_hdsp_info_da_gain, \
2108 .get = snd_hdsp_get_da_gain, \
2109 .put = snd_hdsp_put_da_gain \
2110}
2111
Takashi Iwai55e957d2005-11-17 14:52:13 +01002112static int hdsp_da_gain(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113{
2114 switch (hdsp->control_register & HDSP_DAGainMask) {
2115 case HDSP_DAGainHighGain:
2116 return 0;
2117 case HDSP_DAGainPlus4dBu:
2118 return 1;
2119 case HDSP_DAGainMinus10dBV:
2120 return 2;
2121 default:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002122 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 }
2124}
2125
Takashi Iwai55e957d2005-11-17 14:52:13 +01002126static int hdsp_set_da_gain(struct hdsp *hdsp, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127{
2128 hdsp->control_register &= ~HDSP_DAGainMask;
2129 switch (mode) {
2130 case 0:
2131 hdsp->control_register |= HDSP_DAGainHighGain;
2132 break;
2133 case 1:
2134 hdsp->control_register |= HDSP_DAGainPlus4dBu;
2135 break;
2136 case 2:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002137 hdsp->control_register |= HDSP_DAGainMinus10dBV;
2138 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 default:
2140 return -1;
2141
2142 }
2143 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2144 return 0;
2145}
2146
Takashi Iwai55e957d2005-11-17 14:52:13 +01002147static int snd_hdsp_info_da_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148{
Takashi Iwai8d678da2014-10-20 18:19:34 +02002149 static const char * const texts[] = {"Hi Gain", "+4 dBu", "-10 dbV"};
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002150
Takashi Iwai8d678da2014-10-20 18:19:34 +02002151 return snd_ctl_enum_info(uinfo, 1, 3, texts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152}
2153
Takashi Iwai55e957d2005-11-17 14:52:13 +01002154static int snd_hdsp_get_da_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002156 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002157
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 ucontrol->value.enumerated.item[0] = hdsp_da_gain(hdsp);
2159 return 0;
2160}
2161
Takashi Iwai55e957d2005-11-17 14:52:13 +01002162static int snd_hdsp_put_da_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002164 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 int change;
2166 int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002167
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 if (!snd_hdsp_use_is_exclusive(hdsp))
2169 return -EBUSY;
2170 val = ucontrol->value.enumerated.item[0];
2171 if (val < 0) val = 0;
2172 if (val > 2) val = 2;
2173 spin_lock_irq(&hdsp->lock);
Takashi Iwaib0b98112005-10-20 18:29:58 +02002174 if (val != hdsp_da_gain(hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 change = (hdsp_set_da_gain(hdsp, val) == 0) ? 1 : 0;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002176 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 spin_unlock_irq(&hdsp->lock);
2179 return change;
2180}
2181
2182#define HDSP_AD_GAIN(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002183{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 .name = xname, \
2185 .index = xindex, \
2186 .info = snd_hdsp_info_ad_gain, \
2187 .get = snd_hdsp_get_ad_gain, \
2188 .put = snd_hdsp_put_ad_gain \
2189}
2190
Takashi Iwai55e957d2005-11-17 14:52:13 +01002191static int hdsp_ad_gain(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192{
2193 switch (hdsp->control_register & HDSP_ADGainMask) {
2194 case HDSP_ADGainMinus10dBV:
2195 return 0;
2196 case HDSP_ADGainPlus4dBu:
2197 return 1;
2198 case HDSP_ADGainLowGain:
2199 return 2;
2200 default:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002201 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 }
2203}
2204
Takashi Iwai55e957d2005-11-17 14:52:13 +01002205static int hdsp_set_ad_gain(struct hdsp *hdsp, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206{
2207 hdsp->control_register &= ~HDSP_ADGainMask;
2208 switch (mode) {
2209 case 0:
2210 hdsp->control_register |= HDSP_ADGainMinus10dBV;
2211 break;
2212 case 1:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002213 hdsp->control_register |= HDSP_ADGainPlus4dBu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 break;
2215 case 2:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002216 hdsp->control_register |= HDSP_ADGainLowGain;
2217 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 default:
2219 return -1;
2220
2221 }
2222 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2223 return 0;
2224}
2225
Takashi Iwai55e957d2005-11-17 14:52:13 +01002226static int snd_hdsp_info_ad_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227{
Takashi Iwai8d678da2014-10-20 18:19:34 +02002228 static const char * const texts[] = {"-10 dBV", "+4 dBu", "Lo Gain"};
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002229
Takashi Iwai8d678da2014-10-20 18:19:34 +02002230 return snd_ctl_enum_info(uinfo, 1, 3, texts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231}
2232
Takashi Iwai55e957d2005-11-17 14:52:13 +01002233static int snd_hdsp_get_ad_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002235 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002236
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 ucontrol->value.enumerated.item[0] = hdsp_ad_gain(hdsp);
2238 return 0;
2239}
2240
Takashi Iwai55e957d2005-11-17 14:52:13 +01002241static int snd_hdsp_put_ad_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002243 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 int change;
2245 int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002246
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 if (!snd_hdsp_use_is_exclusive(hdsp))
2248 return -EBUSY;
2249 val = ucontrol->value.enumerated.item[0];
2250 if (val < 0) val = 0;
2251 if (val > 2) val = 2;
2252 spin_lock_irq(&hdsp->lock);
Takashi Iwaib0b98112005-10-20 18:29:58 +02002253 if (val != hdsp_ad_gain(hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 change = (hdsp_set_ad_gain(hdsp, val) == 0) ? 1 : 0;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002255 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 spin_unlock_irq(&hdsp->lock);
2258 return change;
2259}
2260
2261#define HDSP_PHONE_GAIN(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002262{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 .name = xname, \
2264 .index = xindex, \
2265 .info = snd_hdsp_info_phone_gain, \
2266 .get = snd_hdsp_get_phone_gain, \
2267 .put = snd_hdsp_put_phone_gain \
2268}
2269
Takashi Iwai55e957d2005-11-17 14:52:13 +01002270static int hdsp_phone_gain(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271{
2272 switch (hdsp->control_register & HDSP_PhoneGainMask) {
2273 case HDSP_PhoneGain0dB:
2274 return 0;
2275 case HDSP_PhoneGainMinus6dB:
2276 return 1;
2277 case HDSP_PhoneGainMinus12dB:
2278 return 2;
2279 default:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002280 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 }
2282}
2283
Takashi Iwai55e957d2005-11-17 14:52:13 +01002284static int hdsp_set_phone_gain(struct hdsp *hdsp, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285{
2286 hdsp->control_register &= ~HDSP_PhoneGainMask;
2287 switch (mode) {
2288 case 0:
2289 hdsp->control_register |= HDSP_PhoneGain0dB;
2290 break;
2291 case 1:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002292 hdsp->control_register |= HDSP_PhoneGainMinus6dB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 break;
2294 case 2:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002295 hdsp->control_register |= HDSP_PhoneGainMinus12dB;
2296 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 default:
2298 return -1;
2299
2300 }
2301 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2302 return 0;
2303}
2304
Takashi Iwai55e957d2005-11-17 14:52:13 +01002305static int snd_hdsp_info_phone_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306{
Takashi Iwai8d678da2014-10-20 18:19:34 +02002307 static const char * const texts[] = {"0 dB", "-6 dB", "-12 dB"};
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002308
Takashi Iwai8d678da2014-10-20 18:19:34 +02002309 return snd_ctl_enum_info(uinfo, 1, 3, texts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310}
2311
Takashi Iwai55e957d2005-11-17 14:52:13 +01002312static int snd_hdsp_get_phone_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002314 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002315
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 ucontrol->value.enumerated.item[0] = hdsp_phone_gain(hdsp);
2317 return 0;
2318}
2319
Takashi Iwai55e957d2005-11-17 14:52:13 +01002320static int snd_hdsp_put_phone_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002322 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 int change;
2324 int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002325
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 if (!snd_hdsp_use_is_exclusive(hdsp))
2327 return -EBUSY;
2328 val = ucontrol->value.enumerated.item[0];
2329 if (val < 0) val = 0;
2330 if (val > 2) val = 2;
2331 spin_lock_irq(&hdsp->lock);
Takashi Iwaib0b98112005-10-20 18:29:58 +02002332 if (val != hdsp_phone_gain(hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 change = (hdsp_set_phone_gain(hdsp, val) == 0) ? 1 : 0;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002334 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 spin_unlock_irq(&hdsp->lock);
2337 return change;
2338}
2339
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340#define HDSP_PREF_SYNC_REF(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002341{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 .name = xname, \
2343 .index = xindex, \
2344 .info = snd_hdsp_info_pref_sync_ref, \
2345 .get = snd_hdsp_get_pref_sync_ref, \
2346 .put = snd_hdsp_put_pref_sync_ref \
2347}
2348
Takashi Iwai55e957d2005-11-17 14:52:13 +01002349static int hdsp_pref_sync_ref(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350{
2351 /* Notice that this looks at the requested sync source,
2352 not the one actually in use.
2353 */
2354
2355 switch (hdsp->control_register & HDSP_SyncRefMask) {
2356 case HDSP_SyncRef_ADAT1:
2357 return HDSP_SYNC_FROM_ADAT1;
2358 case HDSP_SyncRef_ADAT2:
2359 return HDSP_SYNC_FROM_ADAT2;
2360 case HDSP_SyncRef_ADAT3:
2361 return HDSP_SYNC_FROM_ADAT3;
2362 case HDSP_SyncRef_SPDIF:
2363 return HDSP_SYNC_FROM_SPDIF;
2364 case HDSP_SyncRef_WORD:
2365 return HDSP_SYNC_FROM_WORD;
2366 case HDSP_SyncRef_ADAT_SYNC:
2367 return HDSP_SYNC_FROM_ADAT_SYNC;
2368 default:
2369 return HDSP_SYNC_FROM_WORD;
2370 }
2371 return 0;
2372}
2373
Takashi Iwai55e957d2005-11-17 14:52:13 +01002374static int hdsp_set_pref_sync_ref(struct hdsp *hdsp, int pref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375{
2376 hdsp->control_register &= ~HDSP_SyncRefMask;
2377 switch (pref) {
2378 case HDSP_SYNC_FROM_ADAT1:
2379 hdsp->control_register &= ~HDSP_SyncRefMask; /* clear SyncRef bits */
2380 break;
2381 case HDSP_SYNC_FROM_ADAT2:
2382 hdsp->control_register |= HDSP_SyncRef_ADAT2;
2383 break;
2384 case HDSP_SYNC_FROM_ADAT3:
2385 hdsp->control_register |= HDSP_SyncRef_ADAT3;
2386 break;
2387 case HDSP_SYNC_FROM_SPDIF:
2388 hdsp->control_register |= HDSP_SyncRef_SPDIF;
2389 break;
2390 case HDSP_SYNC_FROM_WORD:
2391 hdsp->control_register |= HDSP_SyncRef_WORD;
2392 break;
2393 case HDSP_SYNC_FROM_ADAT_SYNC:
2394 hdsp->control_register |= HDSP_SyncRef_ADAT_SYNC;
2395 break;
2396 default:
2397 return -1;
2398 }
2399 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2400 return 0;
2401}
2402
Takashi Iwai55e957d2005-11-17 14:52:13 +01002403static int snd_hdsp_info_pref_sync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404{
Takashi Iwai8d678da2014-10-20 18:19:34 +02002405 static const char * const texts[] = {
2406 "Word", "IEC958", "ADAT1", "ADAT Sync", "ADAT2", "ADAT3"
2407 };
Takashi Iwai55e957d2005-11-17 14:52:13 +01002408 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Takashi Iwai8d678da2014-10-20 18:19:34 +02002409 int num_items;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410
2411 switch (hdsp->io_type) {
2412 case Digiface:
2413 case H9652:
Takashi Iwai8d678da2014-10-20 18:19:34 +02002414 num_items = 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 break;
2416 case Multiface:
Takashi Iwai8d678da2014-10-20 18:19:34 +02002417 num_items = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 break;
2419 case H9632:
Takashi Iwai8d678da2014-10-20 18:19:34 +02002420 num_items = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 break;
2422 default:
Takashi Iwai9badda02012-01-09 18:22:35 +01002423 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002425
Takashi Iwai8d678da2014-10-20 18:19:34 +02002426 return snd_ctl_enum_info(uinfo, 1, num_items, texts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427}
2428
Takashi Iwai55e957d2005-11-17 14:52:13 +01002429static int snd_hdsp_get_pref_sync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002431 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002432
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 ucontrol->value.enumerated.item[0] = hdsp_pref_sync_ref(hdsp);
2434 return 0;
2435}
2436
Takashi Iwai55e957d2005-11-17 14:52:13 +01002437static int snd_hdsp_put_pref_sync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002439 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 int change, max;
2441 unsigned int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002442
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 if (!snd_hdsp_use_is_exclusive(hdsp))
2444 return -EBUSY;
2445
2446 switch (hdsp->io_type) {
2447 case Digiface:
2448 case H9652:
2449 max = 6;
2450 break;
2451 case Multiface:
2452 max = 4;
2453 break;
2454 case H9632:
2455 max = 3;
2456 break;
2457 default:
2458 return -EIO;
2459 }
2460
2461 val = ucontrol->value.enumerated.item[0] % max;
2462 spin_lock_irq(&hdsp->lock);
2463 change = (int)val != hdsp_pref_sync_ref(hdsp);
2464 hdsp_set_pref_sync_ref(hdsp, val);
2465 spin_unlock_irq(&hdsp->lock);
2466 return change;
2467}
2468
2469#define HDSP_AUTOSYNC_REF(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002470{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 .name = xname, \
2472 .index = xindex, \
2473 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
2474 .info = snd_hdsp_info_autosync_ref, \
2475 .get = snd_hdsp_get_autosync_ref, \
2476}
2477
Takashi Iwai55e957d2005-11-17 14:52:13 +01002478static int hdsp_autosync_ref(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479{
2480 /* This looks at the autosync selected sync reference */
2481 unsigned int status2 = hdsp_read(hdsp, HDSP_status2Register);
2482
2483 switch (status2 & HDSP_SelSyncRefMask) {
2484 case HDSP_SelSyncRef_WORD:
2485 return HDSP_AUTOSYNC_FROM_WORD;
2486 case HDSP_SelSyncRef_ADAT_SYNC:
2487 return HDSP_AUTOSYNC_FROM_ADAT_SYNC;
2488 case HDSP_SelSyncRef_SPDIF:
2489 return HDSP_AUTOSYNC_FROM_SPDIF;
2490 case HDSP_SelSyncRefMask:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002491 return HDSP_AUTOSYNC_FROM_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 case HDSP_SelSyncRef_ADAT1:
2493 return HDSP_AUTOSYNC_FROM_ADAT1;
2494 case HDSP_SelSyncRef_ADAT2:
2495 return HDSP_AUTOSYNC_FROM_ADAT2;
2496 case HDSP_SelSyncRef_ADAT3:
2497 return HDSP_AUTOSYNC_FROM_ADAT3;
2498 default:
2499 return HDSP_AUTOSYNC_FROM_WORD;
2500 }
2501 return 0;
2502}
2503
Takashi Iwai55e957d2005-11-17 14:52:13 +01002504static int snd_hdsp_info_autosync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505{
Takashi Iwai8d678da2014-10-20 18:19:34 +02002506 static const char * const texts[] = {
2507 "Word", "ADAT Sync", "IEC958", "None", "ADAT1", "ADAT2", "ADAT3"
2508 };
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002509
Takashi Iwai8d678da2014-10-20 18:19:34 +02002510 return snd_ctl_enum_info(uinfo, 1, 7, texts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511}
2512
Takashi Iwai55e957d2005-11-17 14:52:13 +01002513static int snd_hdsp_get_autosync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002515 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002516
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 ucontrol->value.enumerated.item[0] = hdsp_autosync_ref(hdsp);
2518 return 0;
2519}
2520
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521#define HDSP_PRECISE_POINTER(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002522{ .iface = SNDRV_CTL_ELEM_IFACE_CARD, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 .name = xname, \
2524 .index = xindex, \
2525 .info = snd_hdsp_info_precise_pointer, \
2526 .get = snd_hdsp_get_precise_pointer, \
2527 .put = snd_hdsp_put_precise_pointer \
2528}
2529
Takashi Iwai55e957d2005-11-17 14:52:13 +01002530static int hdsp_set_precise_pointer(struct hdsp *hdsp, int precise)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531{
Takashi Iwaib0b98112005-10-20 18:29:58 +02002532 if (precise)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 hdsp->precise_ptr = 1;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002534 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 hdsp->precise_ptr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 return 0;
2537}
2538
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002539#define snd_hdsp_info_precise_pointer snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540
Takashi Iwai55e957d2005-11-17 14:52:13 +01002541static int snd_hdsp_get_precise_pointer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002543 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002544
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 spin_lock_irq(&hdsp->lock);
2546 ucontrol->value.integer.value[0] = hdsp->precise_ptr;
2547 spin_unlock_irq(&hdsp->lock);
2548 return 0;
2549}
2550
Takashi Iwai55e957d2005-11-17 14:52:13 +01002551static int snd_hdsp_put_precise_pointer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002553 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 int change;
2555 unsigned int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002556
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 if (!snd_hdsp_use_is_exclusive(hdsp))
2558 return -EBUSY;
2559 val = ucontrol->value.integer.value[0] & 1;
2560 spin_lock_irq(&hdsp->lock);
2561 change = (int)val != hdsp->precise_ptr;
2562 hdsp_set_precise_pointer(hdsp, val);
2563 spin_unlock_irq(&hdsp->lock);
2564 return change;
2565}
2566
2567#define HDSP_USE_MIDI_TASKLET(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002568{ .iface = SNDRV_CTL_ELEM_IFACE_CARD, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 .name = xname, \
2570 .index = xindex, \
2571 .info = snd_hdsp_info_use_midi_tasklet, \
2572 .get = snd_hdsp_get_use_midi_tasklet, \
2573 .put = snd_hdsp_put_use_midi_tasklet \
2574}
2575
Takashi Iwai55e957d2005-11-17 14:52:13 +01002576static int hdsp_set_use_midi_tasklet(struct hdsp *hdsp, int use_tasklet)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577{
Takashi Iwaib0b98112005-10-20 18:29:58 +02002578 if (use_tasklet)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 hdsp->use_midi_tasklet = 1;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002580 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 hdsp->use_midi_tasklet = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 return 0;
2583}
2584
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002585#define snd_hdsp_info_use_midi_tasklet snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586
Takashi Iwai55e957d2005-11-17 14:52:13 +01002587static int snd_hdsp_get_use_midi_tasklet(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002589 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002590
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 spin_lock_irq(&hdsp->lock);
2592 ucontrol->value.integer.value[0] = hdsp->use_midi_tasklet;
2593 spin_unlock_irq(&hdsp->lock);
2594 return 0;
2595}
2596
Takashi Iwai55e957d2005-11-17 14:52:13 +01002597static int snd_hdsp_put_use_midi_tasklet(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002599 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 int change;
2601 unsigned int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002602
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 if (!snd_hdsp_use_is_exclusive(hdsp))
2604 return -EBUSY;
2605 val = ucontrol->value.integer.value[0] & 1;
2606 spin_lock_irq(&hdsp->lock);
2607 change = (int)val != hdsp->use_midi_tasklet;
2608 hdsp_set_use_midi_tasklet(hdsp, val);
2609 spin_unlock_irq(&hdsp->lock);
2610 return change;
2611}
2612
2613#define HDSP_MIXER(xname, xindex) \
2614{ .iface = SNDRV_CTL_ELEM_IFACE_HWDEP, \
2615 .name = xname, \
2616 .index = xindex, \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002617 .device = 0, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
2619 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
2620 .info = snd_hdsp_info_mixer, \
2621 .get = snd_hdsp_get_mixer, \
2622 .put = snd_hdsp_put_mixer \
2623}
2624
Takashi Iwai55e957d2005-11-17 14:52:13 +01002625static int snd_hdsp_info_mixer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626{
2627 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2628 uinfo->count = 3;
2629 uinfo->value.integer.min = 0;
2630 uinfo->value.integer.max = 65536;
2631 uinfo->value.integer.step = 1;
2632 return 0;
2633}
2634
Takashi Iwai55e957d2005-11-17 14:52:13 +01002635static int snd_hdsp_get_mixer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002637 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 int source;
2639 int destination;
2640 int addr;
2641
2642 source = ucontrol->value.integer.value[0];
2643 destination = ucontrol->value.integer.value[1];
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002644
Takashi Iwaib0b98112005-10-20 18:29:58 +02002645 if (source >= hdsp->max_channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 addr = hdsp_playback_to_output_key(hdsp,source-hdsp->max_channels,destination);
Takashi Iwaib0b98112005-10-20 18:29:58 +02002647 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 addr = hdsp_input_to_output_key(hdsp,source, destination);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002649
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 spin_lock_irq(&hdsp->lock);
2651 ucontrol->value.integer.value[2] = hdsp_read_gain (hdsp, addr);
2652 spin_unlock_irq(&hdsp->lock);
2653 return 0;
2654}
2655
Takashi Iwai55e957d2005-11-17 14:52:13 +01002656static int snd_hdsp_put_mixer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002658 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 int change;
2660 int source;
2661 int destination;
2662 int gain;
2663 int addr;
2664
2665 if (!snd_hdsp_use_is_exclusive(hdsp))
2666 return -EBUSY;
2667
2668 source = ucontrol->value.integer.value[0];
2669 destination = ucontrol->value.integer.value[1];
2670
Takashi Iwaib0b98112005-10-20 18:29:58 +02002671 if (source >= hdsp->max_channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 addr = hdsp_playback_to_output_key(hdsp,source-hdsp->max_channels, destination);
Takashi Iwaib0b98112005-10-20 18:29:58 +02002673 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 addr = hdsp_input_to_output_key(hdsp,source, destination);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675
2676 gain = ucontrol->value.integer.value[2];
2677
2678 spin_lock_irq(&hdsp->lock);
2679 change = gain != hdsp_read_gain(hdsp, addr);
2680 if (change)
2681 hdsp_write_gain(hdsp, addr, gain);
2682 spin_unlock_irq(&hdsp->lock);
2683 return change;
2684}
2685
2686#define HDSP_WC_SYNC_CHECK(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002687{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 .name = xname, \
2689 .index = xindex, \
2690 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
2691 .info = snd_hdsp_info_sync_check, \
2692 .get = snd_hdsp_get_wc_sync_check \
2693}
2694
Takashi Iwai55e957d2005-11-17 14:52:13 +01002695static int snd_hdsp_info_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696{
Takashi Iwai8d678da2014-10-20 18:19:34 +02002697 static const char * const texts[] = {"No Lock", "Lock", "Sync" };
2698
2699 return snd_ctl_enum_info(uinfo, 1, 3, texts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700}
2701
Takashi Iwai55e957d2005-11-17 14:52:13 +01002702static int hdsp_wc_sync_check(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703{
2704 int status2 = hdsp_read(hdsp, HDSP_status2Register);
2705 if (status2 & HDSP_wc_lock) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02002706 if (status2 & HDSP_wc_sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 return 2;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002708 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 return 1;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002710 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 return 0;
2713}
2714
Takashi Iwai55e957d2005-11-17 14:52:13 +01002715static int snd_hdsp_get_wc_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002717 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718
2719 ucontrol->value.enumerated.item[0] = hdsp_wc_sync_check(hdsp);
2720 return 0;
2721}
2722
2723#define HDSP_SPDIF_SYNC_CHECK(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002724{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 .name = xname, \
2726 .index = xindex, \
2727 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
2728 .info = snd_hdsp_info_sync_check, \
2729 .get = snd_hdsp_get_spdif_sync_check \
2730}
2731
Takashi Iwai55e957d2005-11-17 14:52:13 +01002732static int hdsp_spdif_sync_check(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733{
2734 int status = hdsp_read(hdsp, HDSP_statusRegister);
Takashi Iwaib0b98112005-10-20 18:29:58 +02002735 if (status & HDSP_SPDIFErrorFlag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 return 0;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002737 else {
Takashi Iwaib0b98112005-10-20 18:29:58 +02002738 if (status & HDSP_SPDIFSync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 return 2;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002740 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 }
2743 return 0;
2744}
2745
Takashi Iwai55e957d2005-11-17 14:52:13 +01002746static int snd_hdsp_get_spdif_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002748 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749
2750 ucontrol->value.enumerated.item[0] = hdsp_spdif_sync_check(hdsp);
2751 return 0;
2752}
2753
2754#define HDSP_ADATSYNC_SYNC_CHECK(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002755{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 .name = xname, \
2757 .index = xindex, \
2758 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
2759 .info = snd_hdsp_info_sync_check, \
2760 .get = snd_hdsp_get_adatsync_sync_check \
2761}
2762
Takashi Iwai55e957d2005-11-17 14:52:13 +01002763static int hdsp_adatsync_sync_check(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764{
2765 int status = hdsp_read(hdsp, HDSP_statusRegister);
2766 if (status & HDSP_TimecodeLock) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02002767 if (status & HDSP_TimecodeSync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 return 2;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002769 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 return 1;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002771 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 return 0;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002773}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774
Takashi Iwai55e957d2005-11-17 14:52:13 +01002775static int snd_hdsp_get_adatsync_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002777 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778
2779 ucontrol->value.enumerated.item[0] = hdsp_adatsync_sync_check(hdsp);
2780 return 0;
2781}
2782
2783#define HDSP_ADAT_SYNC_CHECK \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002784{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
2786 .info = snd_hdsp_info_sync_check, \
2787 .get = snd_hdsp_get_adat_sync_check \
2788}
2789
Takashi Iwai55e957d2005-11-17 14:52:13 +01002790static int hdsp_adat_sync_check(struct hdsp *hdsp, int idx)
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002791{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 int status = hdsp_read(hdsp, HDSP_statusRegister);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002793
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 if (status & (HDSP_Lock0>>idx)) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02002795 if (status & (HDSP_Sync0>>idx))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 return 2;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002797 else
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002798 return 1;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002799 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 return 0;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002801}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802
Takashi Iwai55e957d2005-11-17 14:52:13 +01002803static int snd_hdsp_get_adat_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804{
2805 int offset;
Takashi Iwai55e957d2005-11-17 14:52:13 +01002806 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807
2808 offset = ucontrol->id.index - 1;
Takashi Iwaida3cec32008-08-08 17:12:14 +02002809 snd_BUG_ON(offset < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810
2811 switch (hdsp->io_type) {
2812 case Digiface:
2813 case H9652:
2814 if (offset >= 3)
2815 return -EINVAL;
2816 break;
2817 case Multiface:
2818 case H9632:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002819 if (offset >= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 return -EINVAL;
2821 break;
2822 default:
2823 return -EIO;
2824 }
2825
2826 ucontrol->value.enumerated.item[0] = hdsp_adat_sync_check(hdsp, offset);
2827 return 0;
2828}
2829
Julian Cablee4b60882007-03-19 11:44:40 +01002830#define HDSP_DDS_OFFSET(xname, xindex) \
2831{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2832 .name = xname, \
2833 .index = xindex, \
2834 .info = snd_hdsp_info_dds_offset, \
2835 .get = snd_hdsp_get_dds_offset, \
2836 .put = snd_hdsp_put_dds_offset \
2837}
2838
2839static int hdsp_dds_offset(struct hdsp *hdsp)
2840{
2841 u64 n;
Julian Cablee4b60882007-03-19 11:44:40 +01002842 unsigned int dds_value = hdsp->dds_value;
2843 int system_sample_rate = hdsp->system_sample_rate;
2844
Takashi Iwai2a3988f2007-10-16 14:26:32 +02002845 if (!dds_value)
2846 return 0;
2847
Julian Cablee4b60882007-03-19 11:44:40 +01002848 n = DDS_NUMERATOR;
2849 /*
2850 * dds_value = n / rate
2851 * rate = n / dds_value
2852 */
Takashi Iwai3f7440a2009-06-05 17:40:04 +02002853 n = div_u64(n, dds_value);
Julian Cablee4b60882007-03-19 11:44:40 +01002854 if (system_sample_rate >= 112000)
2855 n *= 4;
2856 else if (system_sample_rate >= 56000)
2857 n *= 2;
2858 return ((int)n) - system_sample_rate;
2859}
2860
2861static int hdsp_set_dds_offset(struct hdsp *hdsp, int offset_hz)
2862{
2863 int rate = hdsp->system_sample_rate + offset_hz;
2864 hdsp_set_dds_value(hdsp, rate);
2865 return 0;
2866}
2867
2868static int snd_hdsp_info_dds_offset(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
2869{
2870 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2871 uinfo->count = 1;
2872 uinfo->value.integer.min = -5000;
2873 uinfo->value.integer.max = 5000;
2874 return 0;
2875}
2876
2877static int snd_hdsp_get_dds_offset(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2878{
2879 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002880
Julian Cablee4b60882007-03-19 11:44:40 +01002881 ucontrol->value.enumerated.item[0] = hdsp_dds_offset(hdsp);
2882 return 0;
2883}
2884
2885static int snd_hdsp_put_dds_offset(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2886{
2887 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2888 int change;
2889 int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002890
Julian Cablee4b60882007-03-19 11:44:40 +01002891 if (!snd_hdsp_use_is_exclusive(hdsp))
2892 return -EBUSY;
2893 val = ucontrol->value.enumerated.item[0];
2894 spin_lock_irq(&hdsp->lock);
2895 if (val != hdsp_dds_offset(hdsp))
2896 change = (hdsp_set_dds_offset(hdsp, val) == 0) ? 1 : 0;
2897 else
2898 change = 0;
2899 spin_unlock_irq(&hdsp->lock);
2900 return change;
2901}
2902
Takashi Iwai55e957d2005-11-17 14:52:13 +01002903static struct snd_kcontrol_new snd_hdsp_9632_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904HDSP_DA_GAIN("DA Gain", 0),
2905HDSP_AD_GAIN("AD Gain", 0),
2906HDSP_PHONE_GAIN("Phones Gain", 0),
Adrian Knoth4833c672013-01-15 18:52:22 +01002907HDSP_TOGGLE_SETTING("XLR Breakout Cable", HDSP_XLRBreakoutCable),
Julian Cablee4b60882007-03-19 11:44:40 +01002908HDSP_DDS_OFFSET("DDS Sample Rate Offset", 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909};
2910
Takashi Iwai55e957d2005-11-17 14:52:13 +01002911static struct snd_kcontrol_new snd_hdsp_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912{
Clemens Ladisch5549d542005-08-03 13:50:30 +02002913 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
2915 .info = snd_hdsp_control_spdif_info,
2916 .get = snd_hdsp_control_spdif_get,
2917 .put = snd_hdsp_control_spdif_put,
2918},
2919{
2920 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
Clemens Ladisch5549d542005-08-03 13:50:30 +02002921 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
2923 .info = snd_hdsp_control_spdif_stream_info,
2924 .get = snd_hdsp_control_spdif_stream_get,
2925 .put = snd_hdsp_control_spdif_stream_put,
2926},
2927{
2928 .access = SNDRV_CTL_ELEM_ACCESS_READ,
Clemens Ladisch5549d542005-08-03 13:50:30 +02002929 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
2931 .info = snd_hdsp_control_spdif_mask_info,
2932 .get = snd_hdsp_control_spdif_mask_get,
2933 .private_value = IEC958_AES0_NONAUDIO |
2934 IEC958_AES0_PROFESSIONAL |
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002935 IEC958_AES0_CON_EMPHASIS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936},
2937{
2938 .access = SNDRV_CTL_ELEM_ACCESS_READ,
Clemens Ladisch5549d542005-08-03 13:50:30 +02002939 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
2941 .info = snd_hdsp_control_spdif_mask_info,
2942 .get = snd_hdsp_control_spdif_mask_get,
2943 .private_value = IEC958_AES0_NONAUDIO |
2944 IEC958_AES0_PROFESSIONAL |
2945 IEC958_AES0_PRO_EMPHASIS,
2946},
2947HDSP_MIXER("Mixer", 0),
2948HDSP_SPDIF_IN("IEC958 Input Connector", 0),
Adrian Knoth4833c672013-01-15 18:52:22 +01002949HDSP_TOGGLE_SETTING("IEC958 Output also on ADAT1", HDSP_SPDIFOpticalOut),
2950HDSP_TOGGLE_SETTING("IEC958 Professional Bit", HDSP_SPDIFProfessional),
2951HDSP_TOGGLE_SETTING("IEC958 Emphasis Bit", HDSP_SPDIFEmphasis),
2952HDSP_TOGGLE_SETTING("IEC958 Non-audio Bit", HDSP_SPDIFNonAudio),
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002953/* 'Sample Clock Source' complies with the alsa control naming scheme */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954HDSP_CLOCK_SOURCE("Sample Clock Source", 0),
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02002955{
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02002956 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2957 .name = "Sample Clock Source Locking",
2958 .info = snd_hdsp_info_clock_source_lock,
2959 .get = snd_hdsp_get_clock_source_lock,
2960 .put = snd_hdsp_put_clock_source_lock,
2961},
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962HDSP_SYSTEM_CLOCK_MODE("System Clock Mode", 0),
2963HDSP_PREF_SYNC_REF("Preferred Sync Reference", 0),
2964HDSP_AUTOSYNC_REF("AutoSync Reference", 0),
2965HDSP_SPDIF_SAMPLE_RATE("SPDIF Sample Rate", 0),
2966HDSP_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
2967/* 'External Rate' complies with the alsa control naming scheme */
2968HDSP_AUTOSYNC_SAMPLE_RATE("External Rate", 0),
2969HDSP_WC_SYNC_CHECK("Word Clock Lock Status", 0),
2970HDSP_SPDIF_SYNC_CHECK("SPDIF Lock Status", 0),
2971HDSP_ADATSYNC_SYNC_CHECK("ADAT Sync Lock Status", 0),
Adrian Knoth4833c672013-01-15 18:52:22 +01002972HDSP_TOGGLE_SETTING("Line Out", HDSP_LineOut),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973HDSP_PRECISE_POINTER("Precise Pointer", 0),
2974HDSP_USE_MIDI_TASKLET("Use Midi Tasklet", 0),
2975};
2976
Florian Faber28b26e12010-12-01 12:14:47 +01002977
2978static int hdsp_rpm_input12(struct hdsp *hdsp)
2979{
2980 switch (hdsp->control_register & HDSP_RPM_Inp12) {
2981 case HDSP_RPM_Inp12_Phon_6dB:
2982 return 0;
2983 case HDSP_RPM_Inp12_Phon_n6dB:
2984 return 2;
2985 case HDSP_RPM_Inp12_Line_0dB:
2986 return 3;
2987 case HDSP_RPM_Inp12_Line_n6dB:
2988 return 4;
2989 }
2990 return 1;
2991}
2992
2993
2994static int snd_hdsp_get_rpm_input12(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2995{
2996 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2997
2998 ucontrol->value.enumerated.item[0] = hdsp_rpm_input12(hdsp);
2999 return 0;
3000}
3001
3002
3003static int hdsp_set_rpm_input12(struct hdsp *hdsp, int mode)
3004{
3005 hdsp->control_register &= ~HDSP_RPM_Inp12;
3006 switch (mode) {
3007 case 0:
3008 hdsp->control_register |= HDSP_RPM_Inp12_Phon_6dB;
3009 break;
3010 case 1:
3011 break;
3012 case 2:
3013 hdsp->control_register |= HDSP_RPM_Inp12_Phon_n6dB;
3014 break;
3015 case 3:
3016 hdsp->control_register |= HDSP_RPM_Inp12_Line_0dB;
3017 break;
3018 case 4:
3019 hdsp->control_register |= HDSP_RPM_Inp12_Line_n6dB;
3020 break;
3021 default:
3022 return -1;
3023 }
3024
3025 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
3026 return 0;
3027}
3028
3029
3030static int snd_hdsp_put_rpm_input12(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3031{
3032 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
3033 int change;
3034 int val;
3035
3036 if (!snd_hdsp_use_is_exclusive(hdsp))
3037 return -EBUSY;
3038 val = ucontrol->value.enumerated.item[0];
3039 if (val < 0)
3040 val = 0;
3041 if (val > 4)
3042 val = 4;
3043 spin_lock_irq(&hdsp->lock);
3044 if (val != hdsp_rpm_input12(hdsp))
3045 change = (hdsp_set_rpm_input12(hdsp, val) == 0) ? 1 : 0;
3046 else
3047 change = 0;
3048 spin_unlock_irq(&hdsp->lock);
3049 return change;
3050}
3051
3052
3053static int snd_hdsp_info_rpm_input(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
3054{
Takashi Iwai8d678da2014-10-20 18:19:34 +02003055 static const char * const texts[] = {
3056 "Phono +6dB", "Phono 0dB", "Phono -6dB", "Line 0dB", "Line -6dB"
3057 };
Florian Faber28b26e12010-12-01 12:14:47 +01003058
Takashi Iwai8d678da2014-10-20 18:19:34 +02003059 return snd_ctl_enum_info(uinfo, 1, 5, texts);
Florian Faber28b26e12010-12-01 12:14:47 +01003060}
3061
3062
3063static int hdsp_rpm_input34(struct hdsp *hdsp)
3064{
3065 switch (hdsp->control_register & HDSP_RPM_Inp34) {
3066 case HDSP_RPM_Inp34_Phon_6dB:
3067 return 0;
3068 case HDSP_RPM_Inp34_Phon_n6dB:
3069 return 2;
3070 case HDSP_RPM_Inp34_Line_0dB:
3071 return 3;
3072 case HDSP_RPM_Inp34_Line_n6dB:
3073 return 4;
3074 }
3075 return 1;
3076}
3077
3078
3079static int snd_hdsp_get_rpm_input34(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3080{
3081 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
3082
3083 ucontrol->value.enumerated.item[0] = hdsp_rpm_input34(hdsp);
3084 return 0;
3085}
3086
3087
3088static int hdsp_set_rpm_input34(struct hdsp *hdsp, int mode)
3089{
3090 hdsp->control_register &= ~HDSP_RPM_Inp34;
3091 switch (mode) {
3092 case 0:
3093 hdsp->control_register |= HDSP_RPM_Inp34_Phon_6dB;
3094 break;
3095 case 1:
3096 break;
3097 case 2:
3098 hdsp->control_register |= HDSP_RPM_Inp34_Phon_n6dB;
3099 break;
3100 case 3:
3101 hdsp->control_register |= HDSP_RPM_Inp34_Line_0dB;
3102 break;
3103 case 4:
3104 hdsp->control_register |= HDSP_RPM_Inp34_Line_n6dB;
3105 break;
3106 default:
3107 return -1;
3108 }
3109
3110 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
3111 return 0;
3112}
3113
3114
3115static int snd_hdsp_put_rpm_input34(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3116{
3117 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
3118 int change;
3119 int val;
3120
3121 if (!snd_hdsp_use_is_exclusive(hdsp))
3122 return -EBUSY;
3123 val = ucontrol->value.enumerated.item[0];
3124 if (val < 0)
3125 val = 0;
3126 if (val > 4)
3127 val = 4;
3128 spin_lock_irq(&hdsp->lock);
3129 if (val != hdsp_rpm_input34(hdsp))
3130 change = (hdsp_set_rpm_input34(hdsp, val) == 0) ? 1 : 0;
3131 else
3132 change = 0;
3133 spin_unlock_irq(&hdsp->lock);
3134 return change;
3135}
3136
3137
3138/* RPM Bypass switch */
3139static int hdsp_rpm_bypass(struct hdsp *hdsp)
3140{
3141 return (hdsp->control_register & HDSP_RPM_Bypass) ? 1 : 0;
3142}
3143
3144
3145static int snd_hdsp_get_rpm_bypass(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3146{
3147 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
3148
3149 ucontrol->value.integer.value[0] = hdsp_rpm_bypass(hdsp);
3150 return 0;
3151}
3152
3153
3154static int hdsp_set_rpm_bypass(struct hdsp *hdsp, int on)
3155{
3156 if (on)
3157 hdsp->control_register |= HDSP_RPM_Bypass;
3158 else
3159 hdsp->control_register &= ~HDSP_RPM_Bypass;
3160 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
3161 return 0;
3162}
3163
3164
3165static int snd_hdsp_put_rpm_bypass(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3166{
3167 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
3168 int change;
3169 unsigned int val;
3170
3171 if (!snd_hdsp_use_is_exclusive(hdsp))
3172 return -EBUSY;
3173 val = ucontrol->value.integer.value[0] & 1;
3174 spin_lock_irq(&hdsp->lock);
3175 change = (int)val != hdsp_rpm_bypass(hdsp);
3176 hdsp_set_rpm_bypass(hdsp, val);
3177 spin_unlock_irq(&hdsp->lock);
3178 return change;
3179}
3180
3181
3182static int snd_hdsp_info_rpm_bypass(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
3183{
Takashi Iwai8d678da2014-10-20 18:19:34 +02003184 static const char * const texts[] = {"On", "Off"};
Florian Faber28b26e12010-12-01 12:14:47 +01003185
Takashi Iwai8d678da2014-10-20 18:19:34 +02003186 return snd_ctl_enum_info(uinfo, 1, 2, texts);
Florian Faber28b26e12010-12-01 12:14:47 +01003187}
3188
3189
3190/* RPM Disconnect switch */
3191static int hdsp_rpm_disconnect(struct hdsp *hdsp)
3192{
3193 return (hdsp->control_register & HDSP_RPM_Disconnect) ? 1 : 0;
3194}
3195
3196
3197static int snd_hdsp_get_rpm_disconnect(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3198{
3199 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
3200
3201 ucontrol->value.integer.value[0] = hdsp_rpm_disconnect(hdsp);
3202 return 0;
3203}
3204
3205
3206static int hdsp_set_rpm_disconnect(struct hdsp *hdsp, int on)
3207{
3208 if (on)
3209 hdsp->control_register |= HDSP_RPM_Disconnect;
3210 else
3211 hdsp->control_register &= ~HDSP_RPM_Disconnect;
3212 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
3213 return 0;
3214}
3215
3216
3217static int snd_hdsp_put_rpm_disconnect(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3218{
3219 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
3220 int change;
3221 unsigned int val;
3222
3223 if (!snd_hdsp_use_is_exclusive(hdsp))
3224 return -EBUSY;
3225 val = ucontrol->value.integer.value[0] & 1;
3226 spin_lock_irq(&hdsp->lock);
3227 change = (int)val != hdsp_rpm_disconnect(hdsp);
3228 hdsp_set_rpm_disconnect(hdsp, val);
3229 spin_unlock_irq(&hdsp->lock);
3230 return change;
3231}
3232
3233static int snd_hdsp_info_rpm_disconnect(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
3234{
Takashi Iwai8d678da2014-10-20 18:19:34 +02003235 static const char * const texts[] = {"On", "Off"};
Florian Faber28b26e12010-12-01 12:14:47 +01003236
Takashi Iwai8d678da2014-10-20 18:19:34 +02003237 return snd_ctl_enum_info(uinfo, 1, 2, texts);
Florian Faber28b26e12010-12-01 12:14:47 +01003238}
3239
3240static struct snd_kcontrol_new snd_hdsp_rpm_controls[] = {
3241 {
3242 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3243 .name = "RPM Bypass",
3244 .get = snd_hdsp_get_rpm_bypass,
3245 .put = snd_hdsp_put_rpm_bypass,
3246 .info = snd_hdsp_info_rpm_bypass
3247 },
3248 {
3249 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3250 .name = "RPM Disconnect",
3251 .get = snd_hdsp_get_rpm_disconnect,
3252 .put = snd_hdsp_put_rpm_disconnect,
3253 .info = snd_hdsp_info_rpm_disconnect
3254 },
3255 {
3256 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3257 .name = "Input 1/2",
3258 .get = snd_hdsp_get_rpm_input12,
3259 .put = snd_hdsp_put_rpm_input12,
3260 .info = snd_hdsp_info_rpm_input
3261 },
3262 {
3263 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3264 .name = "Input 3/4",
3265 .get = snd_hdsp_get_rpm_input34,
3266 .put = snd_hdsp_put_rpm_input34,
3267 .info = snd_hdsp_info_rpm_input
3268 },
3269 HDSP_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
3270 HDSP_MIXER("Mixer", 0)
3271};
3272
Adrian Knoth4833c672013-01-15 18:52:22 +01003273static struct snd_kcontrol_new snd_hdsp_96xx_aeb =
3274 HDSP_TOGGLE_SETTING("Analog Extension Board",
3275 HDSP_AnalogExtensionBoard);
Takashi Iwai55e957d2005-11-17 14:52:13 +01003276static struct snd_kcontrol_new snd_hdsp_adat_sync_check = HDSP_ADAT_SYNC_CHECK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277
Takashi Iwai55e957d2005-11-17 14:52:13 +01003278static int snd_hdsp_create_controls(struct snd_card *card, struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279{
3280 unsigned int idx;
3281 int err;
Takashi Iwai55e957d2005-11-17 14:52:13 +01003282 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283
Florian Faber28b26e12010-12-01 12:14:47 +01003284 if (hdsp->io_type == RPM) {
3285 /* RPM Bypass, Disconnect and Input switches */
3286 for (idx = 0; idx < ARRAY_SIZE(snd_hdsp_rpm_controls); idx++) {
3287 err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_rpm_controls[idx], hdsp));
3288 if (err < 0)
3289 return err;
3290 }
3291 return 0;
3292 }
3293
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 for (idx = 0; idx < ARRAY_SIZE(snd_hdsp_controls); idx++) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02003295 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_controls[idx], hdsp))) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 if (idx == 1) /* IEC958 (S/PDIF) Stream */
3298 hdsp->spdif_ctl = kctl;
3299 }
3300
3301 /* ADAT SyncCheck status */
3302 snd_hdsp_adat_sync_check.name = "ADAT Lock Status";
3303 snd_hdsp_adat_sync_check.index = 1;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003304 if ((err = snd_ctl_add (card, kctl = snd_ctl_new1(&snd_hdsp_adat_sync_check, hdsp))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 if (hdsp->io_type == Digiface || hdsp->io_type == H9652) {
3307 for (idx = 1; idx < 3; ++idx) {
3308 snd_hdsp_adat_sync_check.index = idx+1;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003309 if ((err = snd_ctl_add (card, kctl = snd_ctl_new1(&snd_hdsp_adat_sync_check, hdsp))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 }
3312 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003313
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 /* DA, AD and Phone gain and XLR breakout cable controls for H9632 cards */
3315 if (hdsp->io_type == H9632) {
3316 for (idx = 0; idx < ARRAY_SIZE(snd_hdsp_9632_controls); idx++) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02003317 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_9632_controls[idx], hdsp))) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 }
3320 }
3321
3322 /* AEB control for H96xx card */
3323 if (hdsp->io_type == H9632 || hdsp->io_type == H9652) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02003324 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_96xx_aeb, hdsp))) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 }
3327
3328 return 0;
3329}
3330
3331/*------------------------------------------------------------
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003332 /proc interface
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 ------------------------------------------------------------*/
3334
3335static void
Takashi Iwai55e957d2005-11-17 14:52:13 +01003336snd_hdsp_proc_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337{
Joe Perches9fe856e2010-09-04 18:52:54 -07003338 struct hdsp *hdsp = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 unsigned int status;
3340 unsigned int status2;
3341 char *pref_sync_ref;
3342 char *autosync_ref;
3343 char *system_clock_mode;
3344 char *clock_source;
3345 int x;
3346
Tim Blechmannc18bc9b2009-08-12 18:21:30 +02003347 status = hdsp_read(hdsp, HDSP_statusRegister);
3348 status2 = hdsp_read(hdsp, HDSP_status2Register);
3349
3350 snd_iprintf(buffer, "%s (Card #%d)\n", hdsp->card_name,
3351 hdsp->card->number + 1);
3352 snd_iprintf(buffer, "Buffers: capture %p playback %p\n",
3353 hdsp->capture_buffer, hdsp->playback_buffer);
3354 snd_iprintf(buffer, "IRQ: %d Registers bus: 0x%lx VM: 0x%lx\n",
3355 hdsp->irq, hdsp->port, (unsigned long)hdsp->iobase);
3356 snd_iprintf(buffer, "Control register: 0x%x\n", hdsp->control_register);
3357 snd_iprintf(buffer, "Control2 register: 0x%x\n",
3358 hdsp->control2_register);
3359 snd_iprintf(buffer, "Status register: 0x%x\n", status);
3360 snd_iprintf(buffer, "Status2 register: 0x%x\n", status2);
3361
3362 if (hdsp_check_for_iobox(hdsp)) {
3363 snd_iprintf(buffer, "No I/O box connected.\n"
3364 "Please connect one and upload firmware.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 return;
Tim Blechmannc18bc9b2009-08-12 18:21:30 +02003366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367
Takashi Iwaib0b98112005-10-20 18:29:58 +02003368 if (hdsp_check_for_firmware(hdsp, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 if (hdsp->state & HDSP_FirmwareCached) {
3370 if (snd_hdsp_load_firmware_from_cache(hdsp) != 0) {
Tim Blechmannc18bc9b2009-08-12 18:21:30 +02003371 snd_iprintf(buffer, "Firmware loading from "
3372 "cache failed, "
3373 "please upload manually.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 return;
3375 }
3376 } else {
Takashi Iwai311e70a2006-09-06 12:13:37 +02003377 int err = -EINVAL;
Takashi Iwai311e70a2006-09-06 12:13:37 +02003378 err = hdsp_request_fw_loader(hdsp);
Takashi Iwai311e70a2006-09-06 12:13:37 +02003379 if (err < 0) {
3380 snd_iprintf(buffer,
3381 "No firmware loaded nor cached, "
3382 "please upload firmware.\n");
3383 return;
3384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 }
3386 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003387
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 snd_iprintf(buffer, "FIFO status: %d\n", hdsp_read(hdsp, HDSP_fifoStatus) & 0xff);
3389 snd_iprintf(buffer, "MIDI1 Output status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusOut0));
3390 snd_iprintf(buffer, "MIDI1 Input status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusIn0));
3391 snd_iprintf(buffer, "MIDI2 Output status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusOut1));
3392 snd_iprintf(buffer, "MIDI2 Input status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusIn1));
3393 snd_iprintf(buffer, "Use Midi Tasklet: %s\n", hdsp->use_midi_tasklet ? "on" : "off");
3394
3395 snd_iprintf(buffer, "\n");
3396
3397 x = 1 << (6 + hdsp_decode_latency(hdsp->control_register & HDSP_LatencyMask));
3398
3399 snd_iprintf(buffer, "Buffer Size (Latency): %d samples (2 periods of %lu bytes)\n", x, (unsigned long) hdsp->period_bytes);
3400 snd_iprintf(buffer, "Hardware pointer (frames): %ld\n", hdsp_hw_pointer(hdsp));
3401 snd_iprintf(buffer, "Precise pointer: %s\n", hdsp->precise_ptr ? "on" : "off");
3402 snd_iprintf(buffer, "Line out: %s\n", (hdsp->control_register & HDSP_LineOut) ? "on" : "off");
3403
3404 snd_iprintf(buffer, "Firmware version: %d\n", (status2&HDSP_version0)|(status2&HDSP_version1)<<1|(status2&HDSP_version2)<<2);
3405
3406 snd_iprintf(buffer, "\n");
3407
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 switch (hdsp_clock_source(hdsp)) {
3409 case HDSP_CLOCK_SOURCE_AUTOSYNC:
3410 clock_source = "AutoSync";
3411 break;
3412 case HDSP_CLOCK_SOURCE_INTERNAL_32KHZ:
3413 clock_source = "Internal 32 kHz";
3414 break;
3415 case HDSP_CLOCK_SOURCE_INTERNAL_44_1KHZ:
3416 clock_source = "Internal 44.1 kHz";
3417 break;
3418 case HDSP_CLOCK_SOURCE_INTERNAL_48KHZ:
3419 clock_source = "Internal 48 kHz";
3420 break;
3421 case HDSP_CLOCK_SOURCE_INTERNAL_64KHZ:
3422 clock_source = "Internal 64 kHz";
3423 break;
3424 case HDSP_CLOCK_SOURCE_INTERNAL_88_2KHZ:
3425 clock_source = "Internal 88.2 kHz";
3426 break;
3427 case HDSP_CLOCK_SOURCE_INTERNAL_96KHZ:
3428 clock_source = "Internal 96 kHz";
3429 break;
3430 case HDSP_CLOCK_SOURCE_INTERNAL_128KHZ:
3431 clock_source = "Internal 128 kHz";
3432 break;
3433 case HDSP_CLOCK_SOURCE_INTERNAL_176_4KHZ:
3434 clock_source = "Internal 176.4 kHz";
3435 break;
3436 case HDSP_CLOCK_SOURCE_INTERNAL_192KHZ:
3437 clock_source = "Internal 192 kHz";
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003438 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 default:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003440 clock_source = "Error";
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 }
3442 snd_iprintf (buffer, "Sample Clock Source: %s\n", clock_source);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003443
Takashi Iwaib0b98112005-10-20 18:29:58 +02003444 if (hdsp_system_clock_mode(hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 system_clock_mode = "Slave";
Takashi Iwaib0b98112005-10-20 18:29:58 +02003446 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 system_clock_mode = "Master";
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003448
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 switch (hdsp_pref_sync_ref (hdsp)) {
3450 case HDSP_SYNC_FROM_WORD:
3451 pref_sync_ref = "Word Clock";
3452 break;
3453 case HDSP_SYNC_FROM_ADAT_SYNC:
3454 pref_sync_ref = "ADAT Sync";
3455 break;
3456 case HDSP_SYNC_FROM_SPDIF:
3457 pref_sync_ref = "SPDIF";
3458 break;
3459 case HDSP_SYNC_FROM_ADAT1:
3460 pref_sync_ref = "ADAT1";
3461 break;
3462 case HDSP_SYNC_FROM_ADAT2:
3463 pref_sync_ref = "ADAT2";
3464 break;
3465 case HDSP_SYNC_FROM_ADAT3:
3466 pref_sync_ref = "ADAT3";
3467 break;
3468 default:
3469 pref_sync_ref = "Word Clock";
3470 break;
3471 }
3472 snd_iprintf (buffer, "Preferred Sync Reference: %s\n", pref_sync_ref);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003473
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 switch (hdsp_autosync_ref (hdsp)) {
3475 case HDSP_AUTOSYNC_FROM_WORD:
3476 autosync_ref = "Word Clock";
3477 break;
3478 case HDSP_AUTOSYNC_FROM_ADAT_SYNC:
3479 autosync_ref = "ADAT Sync";
3480 break;
3481 case HDSP_AUTOSYNC_FROM_SPDIF:
3482 autosync_ref = "SPDIF";
3483 break;
3484 case HDSP_AUTOSYNC_FROM_NONE:
3485 autosync_ref = "None";
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003486 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 case HDSP_AUTOSYNC_FROM_ADAT1:
3488 autosync_ref = "ADAT1";
3489 break;
3490 case HDSP_AUTOSYNC_FROM_ADAT2:
3491 autosync_ref = "ADAT2";
3492 break;
3493 case HDSP_AUTOSYNC_FROM_ADAT3:
3494 autosync_ref = "ADAT3";
3495 break;
3496 default:
3497 autosync_ref = "---";
3498 break;
3499 }
3500 snd_iprintf (buffer, "AutoSync Reference: %s\n", autosync_ref);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003501
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 snd_iprintf (buffer, "AutoSync Frequency: %d\n", hdsp_external_sample_rate(hdsp));
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003503
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 snd_iprintf (buffer, "System Clock Mode: %s\n", system_clock_mode);
3505
3506 snd_iprintf (buffer, "System Clock Frequency: %d\n", hdsp->system_sample_rate);
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02003507 snd_iprintf (buffer, "System Clock Locked: %s\n", hdsp->clock_source_locked ? "Yes" : "No");
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003508
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 snd_iprintf(buffer, "\n");
3510
Florian Faber28b26e12010-12-01 12:14:47 +01003511 if (hdsp->io_type != RPM) {
3512 switch (hdsp_spdif_in(hdsp)) {
3513 case HDSP_SPDIFIN_OPTICAL:
3514 snd_iprintf(buffer, "IEC958 input: Optical\n");
3515 break;
3516 case HDSP_SPDIFIN_COAXIAL:
3517 snd_iprintf(buffer, "IEC958 input: Coaxial\n");
3518 break;
3519 case HDSP_SPDIFIN_INTERNAL:
3520 snd_iprintf(buffer, "IEC958 input: Internal\n");
3521 break;
3522 case HDSP_SPDIFIN_AES:
3523 snd_iprintf(buffer, "IEC958 input: AES\n");
3524 break;
3525 default:
3526 snd_iprintf(buffer, "IEC958 input: ???\n");
3527 break;
3528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003530
Florian Faber28b26e12010-12-01 12:14:47 +01003531 if (RPM == hdsp->io_type) {
3532 if (hdsp->control_register & HDSP_RPM_Bypass)
3533 snd_iprintf(buffer, "RPM Bypass: disabled\n");
3534 else
3535 snd_iprintf(buffer, "RPM Bypass: enabled\n");
3536 if (hdsp->control_register & HDSP_RPM_Disconnect)
3537 snd_iprintf(buffer, "RPM disconnected\n");
3538 else
3539 snd_iprintf(buffer, "RPM connected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540
Florian Faber28b26e12010-12-01 12:14:47 +01003541 switch (hdsp->control_register & HDSP_RPM_Inp12) {
3542 case HDSP_RPM_Inp12_Phon_6dB:
3543 snd_iprintf(buffer, "Input 1/2: Phono, 6dB\n");
3544 break;
3545 case HDSP_RPM_Inp12_Phon_0dB:
3546 snd_iprintf(buffer, "Input 1/2: Phono, 0dB\n");
3547 break;
3548 case HDSP_RPM_Inp12_Phon_n6dB:
3549 snd_iprintf(buffer, "Input 1/2: Phono, -6dB\n");
3550 break;
3551 case HDSP_RPM_Inp12_Line_0dB:
3552 snd_iprintf(buffer, "Input 1/2: Line, 0dB\n");
3553 break;
3554 case HDSP_RPM_Inp12_Line_n6dB:
3555 snd_iprintf(buffer, "Input 1/2: Line, -6dB\n");
3556 break;
3557 default:
3558 snd_iprintf(buffer, "Input 1/2: ???\n");
3559 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560
Florian Faber28b26e12010-12-01 12:14:47 +01003561 switch (hdsp->control_register & HDSP_RPM_Inp34) {
3562 case HDSP_RPM_Inp34_Phon_6dB:
3563 snd_iprintf(buffer, "Input 3/4: Phono, 6dB\n");
3564 break;
3565 case HDSP_RPM_Inp34_Phon_0dB:
3566 snd_iprintf(buffer, "Input 3/4: Phono, 0dB\n");
3567 break;
3568 case HDSP_RPM_Inp34_Phon_n6dB:
3569 snd_iprintf(buffer, "Input 3/4: Phono, -6dB\n");
3570 break;
3571 case HDSP_RPM_Inp34_Line_0dB:
3572 snd_iprintf(buffer, "Input 3/4: Line, 0dB\n");
3573 break;
3574 case HDSP_RPM_Inp34_Line_n6dB:
3575 snd_iprintf(buffer, "Input 3/4: Line, -6dB\n");
3576 break;
3577 default:
3578 snd_iprintf(buffer, "Input 3/4: ???\n");
3579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580
Florian Faber28b26e12010-12-01 12:14:47 +01003581 } else {
3582 if (hdsp->control_register & HDSP_SPDIFOpticalOut)
3583 snd_iprintf(buffer, "IEC958 output: Coaxial & ADAT1\n");
3584 else
3585 snd_iprintf(buffer, "IEC958 output: Coaxial only\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586
Florian Faber28b26e12010-12-01 12:14:47 +01003587 if (hdsp->control_register & HDSP_SPDIFProfessional)
3588 snd_iprintf(buffer, "IEC958 quality: Professional\n");
3589 else
3590 snd_iprintf(buffer, "IEC958 quality: Consumer\n");
3591
3592 if (hdsp->control_register & HDSP_SPDIFEmphasis)
3593 snd_iprintf(buffer, "IEC958 emphasis: on\n");
3594 else
3595 snd_iprintf(buffer, "IEC958 emphasis: off\n");
3596
3597 if (hdsp->control_register & HDSP_SPDIFNonAudio)
3598 snd_iprintf(buffer, "IEC958 NonAudio: on\n");
3599 else
3600 snd_iprintf(buffer, "IEC958 NonAudio: off\n");
3601 x = hdsp_spdif_sample_rate(hdsp);
3602 if (x != 0)
3603 snd_iprintf(buffer, "IEC958 sample rate: %d\n", x);
3604 else
3605 snd_iprintf(buffer, "IEC958 sample rate: Error flag set\n");
3606 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 snd_iprintf(buffer, "\n");
3608
3609 /* Sync Check */
3610 x = status & HDSP_Sync0;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003611 if (status & HDSP_Lock0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 snd_iprintf(buffer, "ADAT1: %s\n", x ? "Sync" : "Lock");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003613 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 snd_iprintf(buffer, "ADAT1: No Lock\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615
3616 switch (hdsp->io_type) {
3617 case Digiface:
3618 case H9652:
3619 x = status & HDSP_Sync1;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003620 if (status & HDSP_Lock1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 snd_iprintf(buffer, "ADAT2: %s\n", x ? "Sync" : "Lock");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003622 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 snd_iprintf(buffer, "ADAT2: No Lock\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 x = status & HDSP_Sync2;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003625 if (status & HDSP_Lock2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 snd_iprintf(buffer, "ADAT3: %s\n", x ? "Sync" : "Lock");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003627 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 snd_iprintf(buffer, "ADAT3: No Lock\n");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003629 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 default:
3631 /* relax */
3632 break;
3633 }
3634
3635 x = status & HDSP_SPDIFSync;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003636 if (status & HDSP_SPDIFErrorFlag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 snd_iprintf (buffer, "SPDIF: No Lock\n");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003638 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 snd_iprintf (buffer, "SPDIF: %s\n", x ? "Sync" : "Lock");
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003640
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 x = status2 & HDSP_wc_sync;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003642 if (status2 & HDSP_wc_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 snd_iprintf (buffer, "Word Clock: %s\n", x ? "Sync" : "Lock");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003644 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 snd_iprintf (buffer, "Word Clock: No Lock\n");
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003646
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 x = status & HDSP_TimecodeSync;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003648 if (status & HDSP_TimecodeLock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 snd_iprintf(buffer, "ADAT Sync: %s\n", x ? "Sync" : "Lock");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003650 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 snd_iprintf(buffer, "ADAT Sync: No Lock\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652
3653 snd_iprintf(buffer, "\n");
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003654
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 /* Informations about H9632 specific controls */
3656 if (hdsp->io_type == H9632) {
3657 char *tmp;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003658
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 switch (hdsp_ad_gain(hdsp)) {
3660 case 0:
3661 tmp = "-10 dBV";
3662 break;
3663 case 1:
3664 tmp = "+4 dBu";
3665 break;
3666 default:
3667 tmp = "Lo Gain";
3668 break;
3669 }
3670 snd_iprintf(buffer, "AD Gain : %s\n", tmp);
3671
3672 switch (hdsp_da_gain(hdsp)) {
3673 case 0:
3674 tmp = "Hi Gain";
3675 break;
3676 case 1:
3677 tmp = "+4 dBu";
3678 break;
3679 default:
3680 tmp = "-10 dBV";
3681 break;
3682 }
3683 snd_iprintf(buffer, "DA Gain : %s\n", tmp);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003684
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 switch (hdsp_phone_gain(hdsp)) {
3686 case 0:
3687 tmp = "0 dB";
3688 break;
3689 case 1:
3690 tmp = "-6 dB";
3691 break;
3692 default:
3693 tmp = "-12 dB";
3694 break;
3695 }
3696 snd_iprintf(buffer, "Phones Gain : %s\n", tmp);
3697
Adrian Knoth4833c672013-01-15 18:52:22 +01003698 snd_iprintf(buffer, "XLR Breakout Cable : %s\n",
3699 hdsp_toggle_setting(hdsp, HDSP_XLRBreakoutCable) ?
3700 "yes" : "no");
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003701
Takashi Iwaib0b98112005-10-20 18:29:58 +02003702 if (hdsp->control_register & HDSP_AnalogExtensionBoard)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 snd_iprintf(buffer, "AEB : on (ADAT1 internal)\n");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003704 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 snd_iprintf(buffer, "AEB : off (ADAT1 external)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 snd_iprintf(buffer, "\n");
3707 }
3708
3709}
3710
Randy Dunlap1374f8c2008-01-16 14:55:42 +01003711static void snd_hdsp_proc_init(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003713 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714
3715 if (! snd_card_proc_new(hdsp->card, "hdsp", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02003716 snd_info_set_text_ops(entry, hdsp, snd_hdsp_proc_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717}
3718
Takashi Iwai55e957d2005-11-17 14:52:13 +01003719static void snd_hdsp_free_buffers(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720{
3721 snd_hammerfall_free_buffer(&hdsp->capture_dma_buf, hdsp->pci);
3722 snd_hammerfall_free_buffer(&hdsp->playback_dma_buf, hdsp->pci);
3723}
3724
Bill Pembertone23e7a12012-12-06 12:35:10 -05003725static int snd_hdsp_initialize_memory(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726{
3727 unsigned long pb_bus, cb_bus;
3728
3729 if (snd_hammerfall_get_buffer(hdsp->pci, &hdsp->capture_dma_buf, HDSP_DMA_AREA_BYTES) < 0 ||
3730 snd_hammerfall_get_buffer(hdsp->pci, &hdsp->playback_dma_buf, HDSP_DMA_AREA_BYTES) < 0) {
3731 if (hdsp->capture_dma_buf.area)
3732 snd_dma_free_pages(&hdsp->capture_dma_buf);
Takashi Iwaia54ba0f2014-02-26 12:05:11 +01003733 dev_err(hdsp->card->dev,
3734 "%s: no buffers available\n", hdsp->card_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 return -ENOMEM;
3736 }
3737
3738 /* Align to bus-space 64K boundary */
3739
Clemens Ladisch7ab39922006-10-09 08:13:32 +02003740 cb_bus = ALIGN(hdsp->capture_dma_buf.addr, 0x10000ul);
3741 pb_bus = ALIGN(hdsp->playback_dma_buf.addr, 0x10000ul);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742
3743 /* Tell the card where it is */
3744
3745 hdsp_write(hdsp, HDSP_inputBufferAddress, cb_bus);
3746 hdsp_write(hdsp, HDSP_outputBufferAddress, pb_bus);
3747
3748 hdsp->capture_buffer = hdsp->capture_dma_buf.area + (cb_bus - hdsp->capture_dma_buf.addr);
3749 hdsp->playback_buffer = hdsp->playback_dma_buf.area + (pb_bus - hdsp->playback_dma_buf.addr);
3750
3751 return 0;
3752}
3753
Takashi Iwai55e957d2005-11-17 14:52:13 +01003754static int snd_hdsp_set_defaults(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755{
3756 unsigned int i;
3757
3758 /* ASSUMPTION: hdsp->lock is either held, or
3759 there is no need to hold it (e.g. during module
Joe Perches561de312007-12-18 13:13:47 +01003760 initialization).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 */
3762
3763 /* set defaults:
3764
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003765 SPDIF Input via Coax
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 Master clock mode
3767 maximum latency (7 => 2^7 = 8192 samples, 64Kbyte buffer,
3768 which implies 2 4096 sample, 32Kbyte periods).
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003769 Enable line out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 */
3771
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003772 hdsp->control_register = HDSP_ClockModeMaster |
3773 HDSP_SPDIFInputCoaxial |
3774 hdsp_encode_latency(7) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 HDSP_LineOut;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003776
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777
3778 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
3779
3780#ifdef SNDRV_BIG_ENDIAN
3781 hdsp->control2_register = HDSP_BIGENDIAN_MODE;
3782#else
3783 hdsp->control2_register = 0;
3784#endif
Takashi Iwaib0b98112005-10-20 18:29:58 +02003785 if (hdsp->io_type == H9652)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 snd_hdsp_9652_enable_mixer (hdsp);
Takashi Iwaib0b98112005-10-20 18:29:58 +02003787 else
3788 hdsp_write (hdsp, HDSP_control2Reg, hdsp->control2_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789
3790 hdsp_reset_hw_pointer(hdsp);
3791 hdsp_compute_period_size(hdsp);
3792
3793 /* silence everything */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003794
Takashi Iwaib0b98112005-10-20 18:29:58 +02003795 for (i = 0; i < HDSP_MATRIX_MIXER_SIZE; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 hdsp->mixer_matrix[i] = MINUS_INFINITY_GAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797
3798 for (i = 0; i < ((hdsp->io_type == H9652 || hdsp->io_type == H9632) ? 1352 : HDSP_MATRIX_MIXER_SIZE); ++i) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02003799 if (hdsp_write_gain (hdsp, i, MINUS_INFINITY_GAIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003802
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 /* H9632 specific defaults */
3804 if (hdsp->io_type == H9632) {
3805 hdsp->control_register |= (HDSP_DAGainPlus4dBu | HDSP_ADGainPlus4dBu | HDSP_PhoneGain0dB);
3806 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
3807 }
3808
3809 /* set a default rate so that the channel map is set up.
3810 */
3811
3812 hdsp_set_rate(hdsp, 48000, 1);
3813
3814 return 0;
3815}
3816
3817static void hdsp_midi_tasklet(unsigned long arg)
3818{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003819 struct hdsp *hdsp = (struct hdsp *)arg;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003820
Takashi Iwaib0b98112005-10-20 18:29:58 +02003821 if (hdsp->midi[0].pending)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 snd_hdsp_midi_input_read (&hdsp->midi[0]);
Takashi Iwaib0b98112005-10-20 18:29:58 +02003823 if (hdsp->midi[1].pending)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 snd_hdsp_midi_input_read (&hdsp->midi[1]);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003825}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826
David Howells7d12e782006-10-05 14:55:46 +01003827static irqreturn_t snd_hdsp_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003829 struct hdsp *hdsp = (struct hdsp *) dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 unsigned int status;
3831 int audio;
3832 int midi0;
3833 int midi1;
3834 unsigned int midi0status;
3835 unsigned int midi1status;
3836 int schedule = 0;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003837
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 status = hdsp_read(hdsp, HDSP_statusRegister);
3839
3840 audio = status & HDSP_audioIRQPending;
3841 midi0 = status & HDSP_midi0IRQPending;
3842 midi1 = status & HDSP_midi1IRQPending;
3843
Takashi Iwaib0b98112005-10-20 18:29:58 +02003844 if (!audio && !midi0 && !midi1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846
3847 hdsp_write(hdsp, HDSP_interruptConfirmation, 0);
3848
3849 midi0status = hdsp_read (hdsp, HDSP_midiStatusIn0) & 0xff;
3850 midi1status = hdsp_read (hdsp, HDSP_midiStatusIn1) & 0xff;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003851
Takashi Iwaic2503cd2009-03-05 09:37:40 +01003852 if (!(hdsp->state & HDSP_InitializationComplete))
3853 return IRQ_HANDLED;
3854
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 if (audio) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02003856 if (hdsp->capture_substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 snd_pcm_period_elapsed(hdsp->pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003858
Takashi Iwaib0b98112005-10-20 18:29:58 +02003859 if (hdsp->playback_substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860 snd_pcm_period_elapsed(hdsp->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003862
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 if (midi0 && midi0status) {
3864 if (hdsp->use_midi_tasklet) {
3865 /* we disable interrupts for this input until processing is done */
3866 hdsp->control_register &= ~HDSP_Midi0InterruptEnable;
3867 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
3868 hdsp->midi[0].pending = 1;
3869 schedule = 1;
3870 } else {
3871 snd_hdsp_midi_input_read (&hdsp->midi[0]);
3872 }
3873 }
Florian Faber28b26e12010-12-01 12:14:47 +01003874 if (hdsp->io_type != Multiface && hdsp->io_type != RPM && hdsp->io_type != H9632 && midi1 && midi1status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 if (hdsp->use_midi_tasklet) {
3876 /* we disable interrupts for this input until processing is done */
3877 hdsp->control_register &= ~HDSP_Midi1InterruptEnable;
3878 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
3879 hdsp->midi[1].pending = 1;
3880 schedule = 1;
3881 } else {
3882 snd_hdsp_midi_input_read (&hdsp->midi[1]);
3883 }
3884 }
3885 if (hdsp->use_midi_tasklet && schedule)
Takashi Iwai1f041282008-12-18 12:17:55 +01003886 tasklet_schedule(&hdsp->midi_tasklet);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887 return IRQ_HANDLED;
3888}
3889
Takashi Iwai55e957d2005-11-17 14:52:13 +01003890static snd_pcm_uframes_t snd_hdsp_hw_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003892 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 return hdsp_hw_pointer(hdsp);
3894}
3895
Takashi Iwai55e957d2005-11-17 14:52:13 +01003896static char *hdsp_channel_buffer_location(struct hdsp *hdsp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897 int stream,
3898 int channel)
3899
3900{
3901 int mapped_channel;
3902
Takashi Iwaida3cec32008-08-08 17:12:14 +02003903 if (snd_BUG_ON(channel < 0 || channel >= hdsp->max_channels))
3904 return NULL;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003905
Takashi Iwaib0b98112005-10-20 18:29:58 +02003906 if ((mapped_channel = hdsp->channel_map[channel]) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 return NULL;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003908
Takashi Iwaib0b98112005-10-20 18:29:58 +02003909 if (stream == SNDRV_PCM_STREAM_CAPTURE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 return hdsp->capture_buffer + (mapped_channel * HDSP_CHANNEL_BUFFER_BYTES);
Takashi Iwaib0b98112005-10-20 18:29:58 +02003911 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 return hdsp->playback_buffer + (mapped_channel * HDSP_CHANNEL_BUFFER_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913}
3914
Takashi Iwai55e957d2005-11-17 14:52:13 +01003915static int snd_hdsp_playback_copy(struct snd_pcm_substream *substream, int channel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 snd_pcm_uframes_t pos, void __user *src, snd_pcm_uframes_t count)
3917{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003918 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 char *channel_buf;
3920
Takashi Iwaida3cec32008-08-08 17:12:14 +02003921 if (snd_BUG_ON(pos + count > HDSP_CHANNEL_BUFFER_BYTES / 4))
3922 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923
3924 channel_buf = hdsp_channel_buffer_location (hdsp, substream->pstr->stream, channel);
Takashi Iwaida3cec32008-08-08 17:12:14 +02003925 if (snd_BUG_ON(!channel_buf))
3926 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 if (copy_from_user(channel_buf + pos * 4, src, count * 4))
3928 return -EFAULT;
3929 return count;
3930}
3931
Takashi Iwai55e957d2005-11-17 14:52:13 +01003932static int snd_hdsp_capture_copy(struct snd_pcm_substream *substream, int channel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 snd_pcm_uframes_t pos, void __user *dst, snd_pcm_uframes_t count)
3934{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003935 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 char *channel_buf;
3937
Takashi Iwaida3cec32008-08-08 17:12:14 +02003938 if (snd_BUG_ON(pos + count > HDSP_CHANNEL_BUFFER_BYTES / 4))
3939 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940
3941 channel_buf = hdsp_channel_buffer_location (hdsp, substream->pstr->stream, channel);
Takashi Iwaida3cec32008-08-08 17:12:14 +02003942 if (snd_BUG_ON(!channel_buf))
3943 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 if (copy_to_user(dst, channel_buf + pos * 4, count * 4))
3945 return -EFAULT;
3946 return count;
3947}
3948
Takashi Iwai55e957d2005-11-17 14:52:13 +01003949static int snd_hdsp_hw_silence(struct snd_pcm_substream *substream, int channel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 snd_pcm_uframes_t pos, snd_pcm_uframes_t count)
3951{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003952 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 char *channel_buf;
3954
3955 channel_buf = hdsp_channel_buffer_location (hdsp, substream->pstr->stream, channel);
Takashi Iwaida3cec32008-08-08 17:12:14 +02003956 if (snd_BUG_ON(!channel_buf))
3957 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 memset(channel_buf + pos * 4, 0, count * 4);
3959 return count;
3960}
3961
Takashi Iwai55e957d2005-11-17 14:52:13 +01003962static int snd_hdsp_reset(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003964 struct snd_pcm_runtime *runtime = substream->runtime;
3965 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
3966 struct snd_pcm_substream *other;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3968 other = hdsp->capture_substream;
3969 else
3970 other = hdsp->playback_substream;
3971 if (hdsp->running)
3972 runtime->status->hw_ptr = hdsp_hw_pointer(hdsp);
3973 else
3974 runtime->status->hw_ptr = 0;
3975 if (other) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01003976 struct snd_pcm_substream *s;
3977 struct snd_pcm_runtime *oruntime = other->runtime;
Takashi Iwaief991b92007-02-22 12:52:53 +01003978 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 if (s == other) {
3980 oruntime->status->hw_ptr = runtime->status->hw_ptr;
3981 break;
3982 }
3983 }
3984 }
3985 return 0;
3986}
3987
Takashi Iwai55e957d2005-11-17 14:52:13 +01003988static int snd_hdsp_hw_params(struct snd_pcm_substream *substream,
3989 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003991 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 int err;
3993 pid_t this_pid;
3994 pid_t other_pid;
3995
Takashi Iwaib0b98112005-10-20 18:29:58 +02003996 if (hdsp_check_for_iobox (hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998
Takashi Iwaib0b98112005-10-20 18:29:58 +02003999 if (hdsp_check_for_firmware(hdsp, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001
4002 spin_lock_irq(&hdsp->lock);
4003
4004 if (substream->pstr->stream == SNDRV_PCM_STREAM_PLAYBACK) {
4005 hdsp->control_register &= ~(HDSP_SPDIFProfessional | HDSP_SPDIFNonAudio | HDSP_SPDIFEmphasis);
4006 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register |= hdsp->creg_spdif_stream);
4007 this_pid = hdsp->playback_pid;
4008 other_pid = hdsp->capture_pid;
4009 } else {
4010 this_pid = hdsp->capture_pid;
4011 other_pid = hdsp->playback_pid;
4012 }
4013
4014 if ((other_pid > 0) && (this_pid != other_pid)) {
4015
4016 /* The other stream is open, and not by the same
4017 task as this one. Make sure that the parameters
4018 that matter are the same.
4019 */
4020
4021 if (params_rate(params) != hdsp->system_sample_rate) {
4022 spin_unlock_irq(&hdsp->lock);
4023 _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_RATE);
4024 return -EBUSY;
4025 }
4026
4027 if (params_period_size(params) != hdsp->period_bytes / 4) {
4028 spin_unlock_irq(&hdsp->lock);
4029 _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
4030 return -EBUSY;
4031 }
4032
4033 /* We're fine. */
4034
4035 spin_unlock_irq(&hdsp->lock);
4036 return 0;
4037
4038 } else {
4039 spin_unlock_irq(&hdsp->lock);
4040 }
4041
4042 /* how to make sure that the rate matches an externally-set one ?
4043 */
4044
4045 spin_lock_irq(&hdsp->lock);
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02004046 if (! hdsp->clock_source_locked) {
4047 if ((err = hdsp_set_rate(hdsp, params_rate(params), 0)) < 0) {
4048 spin_unlock_irq(&hdsp->lock);
4049 _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_RATE);
4050 return err;
4051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 }
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02004053 spin_unlock_irq(&hdsp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054
4055 if ((err = hdsp_set_interrupt_interval(hdsp, params_period_size(params))) < 0) {
4056 _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
4057 return err;
4058 }
4059
4060 return 0;
4061}
4062
Takashi Iwai55e957d2005-11-17 14:52:13 +01004063static int snd_hdsp_channel_info(struct snd_pcm_substream *substream,
4064 struct snd_pcm_channel_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004066 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 int mapped_channel;
4068
Takashi Iwaida3cec32008-08-08 17:12:14 +02004069 if (snd_BUG_ON(info->channel >= hdsp->max_channels))
4070 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071
Takashi Iwaib0b98112005-10-20 18:29:58 +02004072 if ((mapped_channel = hdsp->channel_map[info->channel]) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074
4075 info->offset = mapped_channel * HDSP_CHANNEL_BUFFER_BYTES;
4076 info->first = 0;
4077 info->step = 32;
4078 return 0;
4079}
4080
Takashi Iwai55e957d2005-11-17 14:52:13 +01004081static int snd_hdsp_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082 unsigned int cmd, void *arg)
4083{
4084 switch (cmd) {
4085 case SNDRV_PCM_IOCTL1_RESET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 return snd_hdsp_reset(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
Takashi Iwaib0b98112005-10-20 18:29:58 +02004088 return snd_hdsp_channel_info(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 default:
4090 break;
4091 }
4092
4093 return snd_pcm_lib_ioctl(substream, cmd, arg);
4094}
4095
Takashi Iwai55e957d2005-11-17 14:52:13 +01004096static int snd_hdsp_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004098 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
4099 struct snd_pcm_substream *other;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 int running;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004101
Takashi Iwaib0b98112005-10-20 18:29:58 +02004102 if (hdsp_check_for_iobox (hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104
Takashi Iwai311e70a2006-09-06 12:13:37 +02004105 if (hdsp_check_for_firmware(hdsp, 0)) /* no auto-loading in trigger */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107
4108 spin_lock(&hdsp->lock);
4109 running = hdsp->running;
4110 switch (cmd) {
4111 case SNDRV_PCM_TRIGGER_START:
4112 running |= 1 << substream->stream;
4113 break;
4114 case SNDRV_PCM_TRIGGER_STOP:
4115 running &= ~(1 << substream->stream);
4116 break;
4117 default:
4118 snd_BUG();
4119 spin_unlock(&hdsp->lock);
4120 return -EINVAL;
4121 }
4122 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
4123 other = hdsp->capture_substream;
4124 else
4125 other = hdsp->playback_substream;
4126
4127 if (other) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004128 struct snd_pcm_substream *s;
Takashi Iwaief991b92007-02-22 12:52:53 +01004129 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 if (s == other) {
4131 snd_pcm_trigger_done(s, substream);
4132 if (cmd == SNDRV_PCM_TRIGGER_START)
4133 running |= 1 << s->stream;
4134 else
4135 running &= ~(1 << s->stream);
4136 goto _ok;
4137 }
4138 }
4139 if (cmd == SNDRV_PCM_TRIGGER_START) {
4140 if (!(running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) &&
4141 substream->stream == SNDRV_PCM_STREAM_CAPTURE)
4142 hdsp_silence_playback(hdsp);
4143 } else {
4144 if (running &&
4145 substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
4146 hdsp_silence_playback(hdsp);
4147 }
4148 } else {
4149 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
4150 hdsp_silence_playback(hdsp);
4151 }
4152 _ok:
4153 snd_pcm_trigger_done(substream, substream);
4154 if (!hdsp->running && running)
4155 hdsp_start_audio(hdsp);
4156 else if (hdsp->running && !running)
4157 hdsp_stop_audio(hdsp);
4158 hdsp->running = running;
4159 spin_unlock(&hdsp->lock);
4160
4161 return 0;
4162}
4163
Takashi Iwai55e957d2005-11-17 14:52:13 +01004164static int snd_hdsp_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004166 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 int result = 0;
4168
Takashi Iwaib0b98112005-10-20 18:29:58 +02004169 if (hdsp_check_for_iobox (hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171
Takashi Iwaib0b98112005-10-20 18:29:58 +02004172 if (hdsp_check_for_firmware(hdsp, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174
4175 spin_lock_irq(&hdsp->lock);
4176 if (!hdsp->running)
4177 hdsp_reset_hw_pointer(hdsp);
4178 spin_unlock_irq(&hdsp->lock);
4179 return result;
4180}
4181
Takashi Iwai55e957d2005-11-17 14:52:13 +01004182static struct snd_pcm_hardware snd_hdsp_playback_subinfo =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183{
4184 .info = (SNDRV_PCM_INFO_MMAP |
4185 SNDRV_PCM_INFO_MMAP_VALID |
4186 SNDRV_PCM_INFO_NONINTERLEAVED |
4187 SNDRV_PCM_INFO_SYNC_START |
4188 SNDRV_PCM_INFO_DOUBLE),
4189#ifdef SNDRV_BIG_ENDIAN
4190 .formats = SNDRV_PCM_FMTBIT_S32_BE,
4191#else
4192 .formats = SNDRV_PCM_FMTBIT_S32_LE,
4193#endif
4194 .rates = (SNDRV_PCM_RATE_32000 |
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004195 SNDRV_PCM_RATE_44100 |
4196 SNDRV_PCM_RATE_48000 |
4197 SNDRV_PCM_RATE_64000 |
4198 SNDRV_PCM_RATE_88200 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 SNDRV_PCM_RATE_96000),
4200 .rate_min = 32000,
4201 .rate_max = 96000,
Florian Faber28b26e12010-12-01 12:14:47 +01004202 .channels_min = 6,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 .channels_max = HDSP_MAX_CHANNELS,
4204 .buffer_bytes_max = HDSP_CHANNEL_BUFFER_BYTES * HDSP_MAX_CHANNELS,
4205 .period_bytes_min = (64 * 4) * 10,
4206 .period_bytes_max = (8192 * 4) * HDSP_MAX_CHANNELS,
4207 .periods_min = 2,
4208 .periods_max = 2,
4209 .fifo_size = 0
4210};
4211
Takashi Iwai55e957d2005-11-17 14:52:13 +01004212static struct snd_pcm_hardware snd_hdsp_capture_subinfo =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213{
4214 .info = (SNDRV_PCM_INFO_MMAP |
4215 SNDRV_PCM_INFO_MMAP_VALID |
4216 SNDRV_PCM_INFO_NONINTERLEAVED |
4217 SNDRV_PCM_INFO_SYNC_START),
4218#ifdef SNDRV_BIG_ENDIAN
4219 .formats = SNDRV_PCM_FMTBIT_S32_BE,
4220#else
4221 .formats = SNDRV_PCM_FMTBIT_S32_LE,
4222#endif
4223 .rates = (SNDRV_PCM_RATE_32000 |
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004224 SNDRV_PCM_RATE_44100 |
4225 SNDRV_PCM_RATE_48000 |
4226 SNDRV_PCM_RATE_64000 |
4227 SNDRV_PCM_RATE_88200 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 SNDRV_PCM_RATE_96000),
4229 .rate_min = 32000,
4230 .rate_max = 96000,
Florian Faber28b26e12010-12-01 12:14:47 +01004231 .channels_min = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 .channels_max = HDSP_MAX_CHANNELS,
4233 .buffer_bytes_max = HDSP_CHANNEL_BUFFER_BYTES * HDSP_MAX_CHANNELS,
4234 .period_bytes_min = (64 * 4) * 10,
4235 .period_bytes_max = (8192 * 4) * HDSP_MAX_CHANNELS,
4236 .periods_min = 2,
4237 .periods_max = 2,
4238 .fifo_size = 0
4239};
4240
4241static unsigned int hdsp_period_sizes[] = { 64, 128, 256, 512, 1024, 2048, 4096, 8192 };
4242
Takashi Iwai55e957d2005-11-17 14:52:13 +01004243static struct snd_pcm_hw_constraint_list hdsp_hw_constraints_period_sizes = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244 .count = ARRAY_SIZE(hdsp_period_sizes),
4245 .list = hdsp_period_sizes,
4246 .mask = 0
4247};
4248
4249static unsigned int hdsp_9632_sample_rates[] = { 32000, 44100, 48000, 64000, 88200, 96000, 128000, 176400, 192000 };
4250
Takashi Iwai55e957d2005-11-17 14:52:13 +01004251static struct snd_pcm_hw_constraint_list hdsp_hw_constraints_9632_sample_rates = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 .count = ARRAY_SIZE(hdsp_9632_sample_rates),
4253 .list = hdsp_9632_sample_rates,
4254 .mask = 0
4255};
4256
Takashi Iwai55e957d2005-11-17 14:52:13 +01004257static int snd_hdsp_hw_rule_in_channels(struct snd_pcm_hw_params *params,
4258 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004260 struct hdsp *hdsp = rule->private;
4261 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 if (hdsp->io_type == H9632) {
4263 unsigned int list[3];
4264 list[0] = hdsp->qs_in_channels;
4265 list[1] = hdsp->ds_in_channels;
4266 list[2] = hdsp->ss_in_channels;
4267 return snd_interval_list(c, 3, list, 0);
4268 } else {
4269 unsigned int list[2];
4270 list[0] = hdsp->ds_in_channels;
4271 list[1] = hdsp->ss_in_channels;
4272 return snd_interval_list(c, 2, list, 0);
4273 }
4274}
4275
Takashi Iwai55e957d2005-11-17 14:52:13 +01004276static int snd_hdsp_hw_rule_out_channels(struct snd_pcm_hw_params *params,
4277 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278{
4279 unsigned int list[3];
Takashi Iwai55e957d2005-11-17 14:52:13 +01004280 struct hdsp *hdsp = rule->private;
4281 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 if (hdsp->io_type == H9632) {
4283 list[0] = hdsp->qs_out_channels;
4284 list[1] = hdsp->ds_out_channels;
4285 list[2] = hdsp->ss_out_channels;
4286 return snd_interval_list(c, 3, list, 0);
4287 } else {
4288 list[0] = hdsp->ds_out_channels;
4289 list[1] = hdsp->ss_out_channels;
4290 }
4291 return snd_interval_list(c, 2, list, 0);
4292}
4293
Takashi Iwai55e957d2005-11-17 14:52:13 +01004294static int snd_hdsp_hw_rule_in_channels_rate(struct snd_pcm_hw_params *params,
4295 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004297 struct hdsp *hdsp = rule->private;
4298 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
4299 struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 if (r->min > 96000 && hdsp->io_type == H9632) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004301 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 .min = hdsp->qs_in_channels,
4303 .max = hdsp->qs_in_channels,
4304 .integer = 1,
4305 };
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004306 return snd_interval_refine(c, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 } else if (r->min > 48000 && r->max <= 96000) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004308 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 .min = hdsp->ds_in_channels,
4310 .max = hdsp->ds_in_channels,
4311 .integer = 1,
4312 };
4313 return snd_interval_refine(c, &t);
4314 } else if (r->max < 64000) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004315 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 .min = hdsp->ss_in_channels,
4317 .max = hdsp->ss_in_channels,
4318 .integer = 1,
4319 };
4320 return snd_interval_refine(c, &t);
4321 }
4322 return 0;
4323}
4324
Takashi Iwai55e957d2005-11-17 14:52:13 +01004325static int snd_hdsp_hw_rule_out_channels_rate(struct snd_pcm_hw_params *params,
4326 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004328 struct hdsp *hdsp = rule->private;
4329 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
4330 struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 if (r->min > 96000 && hdsp->io_type == H9632) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004332 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 .min = hdsp->qs_out_channels,
4334 .max = hdsp->qs_out_channels,
4335 .integer = 1,
4336 };
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004337 return snd_interval_refine(c, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 } else if (r->min > 48000 && r->max <= 96000) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004339 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 .min = hdsp->ds_out_channels,
4341 .max = hdsp->ds_out_channels,
4342 .integer = 1,
4343 };
4344 return snd_interval_refine(c, &t);
4345 } else if (r->max < 64000) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004346 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 .min = hdsp->ss_out_channels,
4348 .max = hdsp->ss_out_channels,
4349 .integer = 1,
4350 };
4351 return snd_interval_refine(c, &t);
4352 }
4353 return 0;
4354}
4355
Takashi Iwai55e957d2005-11-17 14:52:13 +01004356static int snd_hdsp_hw_rule_rate_out_channels(struct snd_pcm_hw_params *params,
4357 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004359 struct hdsp *hdsp = rule->private;
4360 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
4361 struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 if (c->min >= hdsp->ss_out_channels) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004363 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364 .min = 32000,
4365 .max = 48000,
4366 .integer = 1,
4367 };
4368 return snd_interval_refine(r, &t);
4369 } else if (c->max <= hdsp->qs_out_channels && hdsp->io_type == H9632) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004370 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 .min = 128000,
4372 .max = 192000,
4373 .integer = 1,
4374 };
4375 return snd_interval_refine(r, &t);
4376 } else if (c->max <= hdsp->ds_out_channels) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004377 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 .min = 64000,
4379 .max = 96000,
4380 .integer = 1,
4381 };
4382 return snd_interval_refine(r, &t);
4383 }
4384 return 0;
4385}
4386
Takashi Iwai55e957d2005-11-17 14:52:13 +01004387static int snd_hdsp_hw_rule_rate_in_channels(struct snd_pcm_hw_params *params,
4388 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004390 struct hdsp *hdsp = rule->private;
4391 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
4392 struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 if (c->min >= hdsp->ss_in_channels) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004394 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 .min = 32000,
4396 .max = 48000,
4397 .integer = 1,
4398 };
4399 return snd_interval_refine(r, &t);
4400 } else if (c->max <= hdsp->qs_in_channels && hdsp->io_type == H9632) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004401 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402 .min = 128000,
4403 .max = 192000,
4404 .integer = 1,
4405 };
4406 return snd_interval_refine(r, &t);
4407 } else if (c->max <= hdsp->ds_in_channels) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004408 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 .min = 64000,
4410 .max = 96000,
4411 .integer = 1,
4412 };
4413 return snd_interval_refine(r, &t);
4414 }
4415 return 0;
4416}
4417
Takashi Iwai55e957d2005-11-17 14:52:13 +01004418static int snd_hdsp_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004420 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
4421 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422
Takashi Iwaib0b98112005-10-20 18:29:58 +02004423 if (hdsp_check_for_iobox (hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425
Takashi Iwaib0b98112005-10-20 18:29:58 +02004426 if (hdsp_check_for_firmware(hdsp, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428
4429 spin_lock_irq(&hdsp->lock);
4430
4431 snd_pcm_set_sync(substream);
4432
4433 runtime->hw = snd_hdsp_playback_subinfo;
4434 runtime->dma_area = hdsp->playback_buffer;
4435 runtime->dma_bytes = HDSP_DMA_AREA_BYTES;
4436
4437 hdsp->playback_pid = current->pid;
4438 hdsp->playback_substream = substream;
4439
4440 spin_unlock_irq(&hdsp->lock);
4441
4442 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
4443 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 +02004444 if (hdsp->clock_source_locked) {
4445 runtime->hw.rate_min = runtime->hw.rate_max = hdsp->system_sample_rate;
4446 } else if (hdsp->io_type == H9632) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 runtime->hw.rate_max = 192000;
4448 runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
4449 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hdsp_hw_constraints_9632_sample_rates);
4450 }
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02004451 if (hdsp->io_type == H9632) {
4452 runtime->hw.channels_min = hdsp->qs_out_channels;
4453 runtime->hw.channels_max = hdsp->ss_out_channels;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004454 }
4455
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
4457 snd_hdsp_hw_rule_out_channels, hdsp,
4458 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
4459 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
4460 snd_hdsp_hw_rule_out_channels_rate, hdsp,
4461 SNDRV_PCM_HW_PARAM_RATE, -1);
4462 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
4463 snd_hdsp_hw_rule_rate_out_channels, hdsp,
4464 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
4465
Florian Faber28b26e12010-12-01 12:14:47 +01004466 if (RPM != hdsp->io_type) {
4467 hdsp->creg_spdif_stream = hdsp->creg_spdif;
4468 hdsp->spdif_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
4469 snd_ctl_notify(hdsp->card, SNDRV_CTL_EVENT_MASK_VALUE |
4470 SNDRV_CTL_EVENT_MASK_INFO, &hdsp->spdif_ctl->id);
4471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472 return 0;
4473}
4474
Takashi Iwai55e957d2005-11-17 14:52:13 +01004475static int snd_hdsp_playback_release(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004477 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478
4479 spin_lock_irq(&hdsp->lock);
4480
4481 hdsp->playback_pid = -1;
4482 hdsp->playback_substream = NULL;
4483
4484 spin_unlock_irq(&hdsp->lock);
4485
Florian Faber28b26e12010-12-01 12:14:47 +01004486 if (RPM != hdsp->io_type) {
4487 hdsp->spdif_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
4488 snd_ctl_notify(hdsp->card, SNDRV_CTL_EVENT_MASK_VALUE |
4489 SNDRV_CTL_EVENT_MASK_INFO, &hdsp->spdif_ctl->id);
4490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 return 0;
4492}
4493
4494
Takashi Iwai55e957d2005-11-17 14:52:13 +01004495static int snd_hdsp_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004497 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
4498 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499
Takashi Iwaib0b98112005-10-20 18:29:58 +02004500 if (hdsp_check_for_iobox (hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502
Takashi Iwaib0b98112005-10-20 18:29:58 +02004503 if (hdsp_check_for_firmware(hdsp, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505
4506 spin_lock_irq(&hdsp->lock);
4507
4508 snd_pcm_set_sync(substream);
4509
4510 runtime->hw = snd_hdsp_capture_subinfo;
4511 runtime->dma_area = hdsp->capture_buffer;
4512 runtime->dma_bytes = HDSP_DMA_AREA_BYTES;
4513
4514 hdsp->capture_pid = current->pid;
4515 hdsp->capture_substream = substream;
4516
4517 spin_unlock_irq(&hdsp->lock);
4518
4519 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
4520 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, &hdsp_hw_constraints_period_sizes);
4521 if (hdsp->io_type == H9632) {
4522 runtime->hw.channels_min = hdsp->qs_in_channels;
4523 runtime->hw.channels_max = hdsp->ss_in_channels;
4524 runtime->hw.rate_max = 192000;
4525 runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
4526 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hdsp_hw_constraints_9632_sample_rates);
4527 }
4528 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
4529 snd_hdsp_hw_rule_in_channels, hdsp,
4530 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
4531 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
4532 snd_hdsp_hw_rule_in_channels_rate, hdsp,
4533 SNDRV_PCM_HW_PARAM_RATE, -1);
4534 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
4535 snd_hdsp_hw_rule_rate_in_channels, hdsp,
4536 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
4537 return 0;
4538}
4539
Takashi Iwai55e957d2005-11-17 14:52:13 +01004540static int snd_hdsp_capture_release(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004542 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543
4544 spin_lock_irq(&hdsp->lock);
4545
4546 hdsp->capture_pid = -1;
4547 hdsp->capture_substream = NULL;
4548
4549 spin_unlock_irq(&hdsp->lock);
4550 return 0;
4551}
4552
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553/* helper functions for copying meter values */
4554static inline int copy_u32_le(void __user *dest, void __iomem *src)
4555{
4556 u32 val = readl(src);
4557 return copy_to_user(dest, &val, 4);
4558}
4559
4560static inline int copy_u64_le(void __user *dest, void __iomem *src_low, void __iomem *src_high)
4561{
4562 u32 rms_low, rms_high;
4563 u64 rms;
4564 rms_low = readl(src_low);
4565 rms_high = readl(src_high);
4566 rms = ((u64)rms_high << 32) | rms_low;
4567 return copy_to_user(dest, &rms, 8);
4568}
4569
4570static inline int copy_u48_le(void __user *dest, void __iomem *src_low, void __iomem *src_high)
4571{
4572 u32 rms_low, rms_high;
4573 u64 rms;
4574 rms_low = readl(src_low) & 0xffffff00;
4575 rms_high = readl(src_high) & 0xffffff00;
4576 rms = ((u64)rms_high << 32) | rms_low;
4577 return copy_to_user(dest, &rms, 8);
4578}
4579
Takashi Iwai55e957d2005-11-17 14:52:13 +01004580static int hdsp_9652_get_peak(struct hdsp *hdsp, struct hdsp_peak_rms __user *peak_rms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581{
4582 int doublespeed = 0;
4583 int i, j, channels, ofs;
4584
4585 if (hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DoubleSpeedStatus)
4586 doublespeed = 1;
4587 channels = doublespeed ? 14 : 26;
4588 for (i = 0, j = 0; i < 26; ++i) {
4589 if (doublespeed && (i & 4))
4590 continue;
4591 ofs = HDSP_9652_peakBase - j * 4;
4592 if (copy_u32_le(&peak_rms->input_peaks[i], hdsp->iobase + ofs))
4593 return -EFAULT;
4594 ofs -= channels * 4;
4595 if (copy_u32_le(&peak_rms->playback_peaks[i], hdsp->iobase + ofs))
4596 return -EFAULT;
4597 ofs -= channels * 4;
4598 if (copy_u32_le(&peak_rms->output_peaks[i], hdsp->iobase + ofs))
4599 return -EFAULT;
4600 ofs = HDSP_9652_rmsBase + j * 8;
4601 if (copy_u48_le(&peak_rms->input_rms[i], hdsp->iobase + ofs,
4602 hdsp->iobase + ofs + 4))
4603 return -EFAULT;
4604 ofs += channels * 8;
4605 if (copy_u48_le(&peak_rms->playback_rms[i], hdsp->iobase + ofs,
4606 hdsp->iobase + ofs + 4))
4607 return -EFAULT;
4608 ofs += channels * 8;
4609 if (copy_u48_le(&peak_rms->output_rms[i], hdsp->iobase + ofs,
4610 hdsp->iobase + ofs + 4))
4611 return -EFAULT;
4612 j++;
4613 }
4614 return 0;
4615}
4616
Takashi Iwai55e957d2005-11-17 14:52:13 +01004617static int hdsp_9632_get_peak(struct hdsp *hdsp, struct hdsp_peak_rms __user *peak_rms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618{
4619 int i, j;
Takashi Iwai55e957d2005-11-17 14:52:13 +01004620 struct hdsp_9632_meters __iomem *m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 int doublespeed = 0;
4622
4623 if (hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DoubleSpeedStatus)
4624 doublespeed = 1;
Takashi Iwai55e957d2005-11-17 14:52:13 +01004625 m = (struct hdsp_9632_meters __iomem *)(hdsp->iobase+HDSP_9632_metersBase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626 for (i = 0, j = 0; i < 16; ++i, ++j) {
4627 if (copy_u32_le(&peak_rms->input_peaks[i], &m->input_peak[j]))
4628 return -EFAULT;
4629 if (copy_u32_le(&peak_rms->playback_peaks[i], &m->playback_peak[j]))
4630 return -EFAULT;
4631 if (copy_u32_le(&peak_rms->output_peaks[i], &m->output_peak[j]))
4632 return -EFAULT;
4633 if (copy_u64_le(&peak_rms->input_rms[i], &m->input_rms_low[j],
4634 &m->input_rms_high[j]))
4635 return -EFAULT;
4636 if (copy_u64_le(&peak_rms->playback_rms[i], &m->playback_rms_low[j],
4637 &m->playback_rms_high[j]))
4638 return -EFAULT;
4639 if (copy_u64_le(&peak_rms->output_rms[i], &m->output_rms_low[j],
4640 &m->output_rms_high[j]))
4641 return -EFAULT;
4642 if (doublespeed && i == 3) i += 4;
4643 }
4644 return 0;
4645}
4646
Takashi Iwai55e957d2005-11-17 14:52:13 +01004647static int hdsp_get_peak(struct hdsp *hdsp, struct hdsp_peak_rms __user *peak_rms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648{
4649 int i;
4650
4651 for (i = 0; i < 26; i++) {
4652 if (copy_u32_le(&peak_rms->playback_peaks[i],
4653 hdsp->iobase + HDSP_playbackPeakLevel + i * 4))
4654 return -EFAULT;
4655 if (copy_u32_le(&peak_rms->input_peaks[i],
4656 hdsp->iobase + HDSP_inputPeakLevel + i * 4))
4657 return -EFAULT;
4658 }
4659 for (i = 0; i < 28; i++) {
4660 if (copy_u32_le(&peak_rms->output_peaks[i],
4661 hdsp->iobase + HDSP_outputPeakLevel + i * 4))
4662 return -EFAULT;
4663 }
4664 for (i = 0; i < 26; ++i) {
4665 if (copy_u64_le(&peak_rms->playback_rms[i],
4666 hdsp->iobase + HDSP_playbackRmsLevel + i * 8 + 4,
4667 hdsp->iobase + HDSP_playbackRmsLevel + i * 8))
4668 return -EFAULT;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004669 if (copy_u64_le(&peak_rms->input_rms[i],
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670 hdsp->iobase + HDSP_inputRmsLevel + i * 8 + 4,
4671 hdsp->iobase + HDSP_inputRmsLevel + i * 8))
4672 return -EFAULT;
4673 }
4674 return 0;
4675}
4676
Takashi Iwai55e957d2005-11-17 14:52:13 +01004677static 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 -07004678{
Joe Perches9fe856e2010-09-04 18:52:54 -07004679 struct hdsp *hdsp = hw->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 void __user *argp = (void __user *)arg;
Tim Blechmann3ae7e2e2008-11-08 14:42:18 +01004681 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682
4683 switch (cmd) {
4684 case SNDRV_HDSP_IOCTL_GET_PEAK_RMS: {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004685 struct hdsp_peak_rms __user *peak_rms = (struct hdsp_peak_rms __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686
Tim Blechmann3ae7e2e2008-11-08 14:42:18 +01004687 err = hdsp_check_for_iobox(hdsp);
4688 if (err < 0)
4689 return err;
4690
4691 err = hdsp_check_for_firmware(hdsp, 1);
4692 if (err < 0)
4693 return err;
4694
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 if (!(hdsp->state & HDSP_FirmwareLoaded)) {
Takashi Iwaia54ba0f2014-02-26 12:05:11 +01004696 dev_err(hdsp->card->dev,
4697 "firmware needs to be uploaded to the card.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698 return -EINVAL;
4699 }
4700
4701 switch (hdsp->io_type) {
4702 case H9652:
4703 return hdsp_9652_get_peak(hdsp, peak_rms);
4704 case H9632:
4705 return hdsp_9632_get_peak(hdsp, peak_rms);
4706 default:
4707 return hdsp_get_peak(hdsp, peak_rms);
4708 }
4709 }
4710 case SNDRV_HDSP_IOCTL_GET_CONFIG_INFO: {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004711 struct hdsp_config_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 unsigned long flags;
4713 int i;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004714
Tim Blechmann3ae7e2e2008-11-08 14:42:18 +01004715 err = hdsp_check_for_iobox(hdsp);
4716 if (err < 0)
4717 return err;
4718
4719 err = hdsp_check_for_firmware(hdsp, 1);
4720 if (err < 0)
4721 return err;
4722
Dan Rosenberge68d3b32010-09-25 11:07:27 -04004723 memset(&info, 0, sizeof(info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 spin_lock_irqsave(&hdsp->lock, flags);
4725 info.pref_sync_ref = (unsigned char)hdsp_pref_sync_ref(hdsp);
4726 info.wordclock_sync_check = (unsigned char)hdsp_wc_sync_check(hdsp);
Takashi Iwaib0b98112005-10-20 18:29:58 +02004727 if (hdsp->io_type != H9632)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728 info.adatsync_sync_check = (unsigned char)hdsp_adatsync_sync_check(hdsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 info.spdif_sync_check = (unsigned char)hdsp_spdif_sync_check(hdsp);
Florian Faber28b26e12010-12-01 12:14:47 +01004730 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 -07004731 info.adat_sync_check[i] = (unsigned char)hdsp_adat_sync_check(hdsp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732 info.spdif_in = (unsigned char)hdsp_spdif_in(hdsp);
Adrian Knoth4833c672013-01-15 18:52:22 +01004733 info.spdif_out = (unsigned char)hdsp_toggle_setting(hdsp,
4734 HDSP_SPDIFOpticalOut);
4735 info.spdif_professional = (unsigned char)
4736 hdsp_toggle_setting(hdsp, HDSP_SPDIFProfessional);
4737 info.spdif_emphasis = (unsigned char)
4738 hdsp_toggle_setting(hdsp, HDSP_SPDIFEmphasis);
4739 info.spdif_nonaudio = (unsigned char)
4740 hdsp_toggle_setting(hdsp, HDSP_SPDIFNonAudio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 info.spdif_sample_rate = hdsp_spdif_sample_rate(hdsp);
4742 info.system_sample_rate = hdsp->system_sample_rate;
4743 info.autosync_sample_rate = hdsp_external_sample_rate(hdsp);
4744 info.system_clock_mode = (unsigned char)hdsp_system_clock_mode(hdsp);
4745 info.clock_source = (unsigned char)hdsp_clock_source(hdsp);
4746 info.autosync_ref = (unsigned char)hdsp_autosync_ref(hdsp);
Adrian Knoth4833c672013-01-15 18:52:22 +01004747 info.line_out = (unsigned char)
4748 hdsp_toggle_setting(hdsp, HDSP_LineOut);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749 if (hdsp->io_type == H9632) {
4750 info.da_gain = (unsigned char)hdsp_da_gain(hdsp);
4751 info.ad_gain = (unsigned char)hdsp_ad_gain(hdsp);
4752 info.phone_gain = (unsigned char)hdsp_phone_gain(hdsp);
Adrian Knoth4833c672013-01-15 18:52:22 +01004753 info.xlr_breakout_cable =
4754 (unsigned char)hdsp_toggle_setting(hdsp,
4755 HDSP_XLRBreakoutCable);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004756
Florian Faber28b26e12010-12-01 12:14:47 +01004757 } else if (hdsp->io_type == RPM) {
4758 info.da_gain = (unsigned char) hdsp_rpm_input12(hdsp);
4759 info.ad_gain = (unsigned char) hdsp_rpm_input34(hdsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 }
Takashi Iwaib0b98112005-10-20 18:29:58 +02004761 if (hdsp->io_type == H9632 || hdsp->io_type == H9652)
Adrian Knoth4833c672013-01-15 18:52:22 +01004762 info.analog_extension_board =
4763 (unsigned char)hdsp_toggle_setting(hdsp,
4764 HDSP_AnalogExtensionBoard);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765 spin_unlock_irqrestore(&hdsp->lock, flags);
4766 if (copy_to_user(argp, &info, sizeof(info)))
4767 return -EFAULT;
4768 break;
4769 }
4770 case SNDRV_HDSP_IOCTL_GET_9632_AEB: {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004771 struct hdsp_9632_aeb h9632_aeb;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004772
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773 if (hdsp->io_type != H9632) return -EINVAL;
4774 h9632_aeb.aebi = hdsp->ss_in_channels - H9632_SS_CHANNELS;
4775 h9632_aeb.aebo = hdsp->ss_out_channels - H9632_SS_CHANNELS;
4776 if (copy_to_user(argp, &h9632_aeb, sizeof(h9632_aeb)))
4777 return -EFAULT;
4778 break;
4779 }
4780 case SNDRV_HDSP_IOCTL_GET_VERSION: {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004781 struct hdsp_version hdsp_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 int err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004783
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 if (hdsp->io_type == H9652 || hdsp->io_type == H9632) return -EINVAL;
4785 if (hdsp->io_type == Undefined) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02004786 if ((err = hdsp_get_iobox_version(hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788 }
Dan Carpenterd14df332013-10-16 11:44:25 +03004789 memset(&hdsp_version, 0, sizeof(hdsp_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790 hdsp_version.io_type = hdsp->io_type;
4791 hdsp_version.firmware_rev = hdsp->firmware_rev;
Takashi Iwaib0b98112005-10-20 18:29:58 +02004792 if ((err = copy_to_user(argp, &hdsp_version, sizeof(hdsp_version))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 break;
4795 }
4796 case SNDRV_HDSP_IOCTL_UPLOAD_FIRMWARE: {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004797 struct hdsp_firmware __user *firmware;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 u32 __user *firmware_data;
4799 int err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004800
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801 if (hdsp->io_type == H9652 || hdsp->io_type == H9632) return -EINVAL;
4802 /* SNDRV_HDSP_IOCTL_GET_VERSION must have been called */
4803 if (hdsp->io_type == Undefined) return -EINVAL;
4804
4805 if (hdsp->state & (HDSP_FirmwareCached | HDSP_FirmwareLoaded))
4806 return -EBUSY;
4807
Takashi Iwaia54ba0f2014-02-26 12:05:11 +01004808 dev_info(hdsp->card->dev,
4809 "initializing firmware upload\n");
Takashi Iwai55e957d2005-11-17 14:52:13 +01004810 firmware = (struct hdsp_firmware __user *)argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811
Takashi Iwaib0b98112005-10-20 18:29:58 +02004812 if (get_user(firmware_data, &firmware->firmware_data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813 return -EFAULT;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004814
Takashi Iwaib0b98112005-10-20 18:29:58 +02004815 if (hdsp_check_for_iobox (hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817
Takashi Iwai90caaef2012-11-22 16:55:11 +01004818 if (!hdsp->fw_uploaded) {
4819 hdsp->fw_uploaded = vmalloc(HDSP_FIRMWARE_SIZE);
4820 if (!hdsp->fw_uploaded)
4821 return -ENOMEM;
4822 }
4823
4824 if (copy_from_user(hdsp->fw_uploaded, firmware_data,
4825 HDSP_FIRMWARE_SIZE)) {
4826 vfree(hdsp->fw_uploaded);
4827 hdsp->fw_uploaded = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828 return -EFAULT;
Takashi Iwai90caaef2012-11-22 16:55:11 +01004829 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004830
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 hdsp->state |= HDSP_FirmwareCached;
4832
Takashi Iwaib0b98112005-10-20 18:29:58 +02004833 if ((err = snd_hdsp_load_firmware_from_cache(hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834 return err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004835
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836 if (!(hdsp->state & HDSP_InitializationComplete)) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02004837 if ((err = snd_hdsp_enable_io(hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838 return err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004839
4840 snd_hdsp_initialize_channels(hdsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841 snd_hdsp_initialize_midi_flush(hdsp);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004842
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843 if ((err = snd_hdsp_create_alsa_devices(hdsp->card, hdsp)) < 0) {
Takashi Iwaia54ba0f2014-02-26 12:05:11 +01004844 dev_err(hdsp->card->dev,
4845 "error creating alsa devices\n");
Takashi Iwaib0b98112005-10-20 18:29:58 +02004846 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 }
4848 }
4849 break;
4850 }
4851 case SNDRV_HDSP_IOCTL_GET_MIXER: {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004852 struct hdsp_mixer __user *mixer = (struct hdsp_mixer __user *)argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853 if (copy_to_user(mixer->matrix, hdsp->mixer_matrix, sizeof(unsigned short)*HDSP_MATRIX_MIXER_SIZE))
4854 return -EFAULT;
4855 break;
4856 }
4857 default:
4858 return -EINVAL;
4859 }
4860 return 0;
4861}
4862
Takashi Iwai55e957d2005-11-17 14:52:13 +01004863static struct snd_pcm_ops snd_hdsp_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864 .open = snd_hdsp_playback_open,
4865 .close = snd_hdsp_playback_release,
4866 .ioctl = snd_hdsp_ioctl,
4867 .hw_params = snd_hdsp_hw_params,
4868 .prepare = snd_hdsp_prepare,
4869 .trigger = snd_hdsp_trigger,
4870 .pointer = snd_hdsp_hw_pointer,
4871 .copy = snd_hdsp_playback_copy,
4872 .silence = snd_hdsp_hw_silence,
4873};
4874
Takashi Iwai55e957d2005-11-17 14:52:13 +01004875static struct snd_pcm_ops snd_hdsp_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876 .open = snd_hdsp_capture_open,
4877 .close = snd_hdsp_capture_release,
4878 .ioctl = snd_hdsp_ioctl,
4879 .hw_params = snd_hdsp_hw_params,
4880 .prepare = snd_hdsp_prepare,
4881 .trigger = snd_hdsp_trigger,
4882 .pointer = snd_hdsp_hw_pointer,
4883 .copy = snd_hdsp_capture_copy,
4884};
4885
Takashi Iwai92eed662008-02-22 18:35:56 +01004886static int snd_hdsp_create_hwdep(struct snd_card *card, struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004888 struct snd_hwdep *hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889 int err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004890
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891 if ((err = snd_hwdep_new(card, "HDSP hwdep", 0, &hw)) < 0)
4892 return err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004893
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894 hdsp->hwdep = hw;
4895 hw->private_data = hdsp;
4896 strcpy(hw->name, "HDSP hwdep interface");
4897
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898 hw->ops.ioctl = snd_hdsp_hwdep_ioctl;
Andre Schramm42eb9232012-05-07 18:52:51 +02004899 hw->ops.ioctl_compat = snd_hdsp_hwdep_ioctl;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004900
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901 return 0;
4902}
4903
Takashi Iwai55e957d2005-11-17 14:52:13 +01004904static int snd_hdsp_create_pcm(struct snd_card *card, struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004906 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 int err;
4908
4909 if ((err = snd_pcm_new(card, hdsp->card_name, 0, 1, 1, &pcm)) < 0)
4910 return err;
4911
4912 hdsp->pcm = pcm;
4913 pcm->private_data = hdsp;
4914 strcpy(pcm->name, hdsp->card_name);
4915
4916 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_hdsp_playback_ops);
4917 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_hdsp_capture_ops);
4918
4919 pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
4920
4921 return 0;
4922}
4923
Takashi Iwai55e957d2005-11-17 14:52:13 +01004924static void snd_hdsp_9652_enable_mixer (struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925{
4926 hdsp->control2_register |= HDSP_9652_ENABLE_MIXER;
4927 hdsp_write (hdsp, HDSP_control2Reg, hdsp->control2_register);
4928}
4929
Takashi Iwai55e957d2005-11-17 14:52:13 +01004930static int snd_hdsp_enable_io (struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931{
4932 int i;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004933
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934 if (hdsp_fifo_wait (hdsp, 0, 100)) {
Takashi Iwaia54ba0f2014-02-26 12:05:11 +01004935 dev_err(hdsp->card->dev,
4936 "enable_io fifo_wait failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 return -EIO;
4938 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004939
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940 for (i = 0; i < hdsp->max_channels; ++i) {
4941 hdsp_write (hdsp, HDSP_inputEnable + (4 * i), 1);
4942 hdsp_write (hdsp, HDSP_outputEnable + (4 * i), 1);
4943 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004944
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 return 0;
4946}
4947
Takashi Iwai55e957d2005-11-17 14:52:13 +01004948static void snd_hdsp_initialize_channels(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949{
4950 int status, aebi_channels, aebo_channels;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004951
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952 switch (hdsp->io_type) {
4953 case Digiface:
4954 hdsp->card_name = "RME Hammerfall DSP + Digiface";
4955 hdsp->ss_in_channels = hdsp->ss_out_channels = DIGIFACE_SS_CHANNELS;
4956 hdsp->ds_in_channels = hdsp->ds_out_channels = DIGIFACE_DS_CHANNELS;
4957 break;
4958
4959 case H9652:
4960 hdsp->card_name = "RME Hammerfall HDSP 9652";
4961 hdsp->ss_in_channels = hdsp->ss_out_channels = H9652_SS_CHANNELS;
4962 hdsp->ds_in_channels = hdsp->ds_out_channels = H9652_DS_CHANNELS;
4963 break;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004964
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965 case H9632:
4966 status = hdsp_read(hdsp, HDSP_statusRegister);
4967 /* HDSP_AEBx bits are low when AEB are connected */
4968 aebi_channels = (status & HDSP_AEBI) ? 0 : 4;
4969 aebo_channels = (status & HDSP_AEBO) ? 0 : 4;
4970 hdsp->card_name = "RME Hammerfall HDSP 9632";
4971 hdsp->ss_in_channels = H9632_SS_CHANNELS+aebi_channels;
4972 hdsp->ds_in_channels = H9632_DS_CHANNELS+aebi_channels;
4973 hdsp->qs_in_channels = H9632_QS_CHANNELS+aebi_channels;
4974 hdsp->ss_out_channels = H9632_SS_CHANNELS+aebo_channels;
4975 hdsp->ds_out_channels = H9632_DS_CHANNELS+aebo_channels;
4976 hdsp->qs_out_channels = H9632_QS_CHANNELS+aebo_channels;
4977 break;
4978
4979 case Multiface:
4980 hdsp->card_name = "RME Hammerfall DSP + Multiface";
4981 hdsp->ss_in_channels = hdsp->ss_out_channels = MULTIFACE_SS_CHANNELS;
4982 hdsp->ds_in_channels = hdsp->ds_out_channels = MULTIFACE_DS_CHANNELS;
4983 break;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004984
Florian Faber28b26e12010-12-01 12:14:47 +01004985 case RPM:
4986 hdsp->card_name = "RME Hammerfall DSP + RPM";
4987 hdsp->ss_in_channels = RPM_CHANNELS-1;
4988 hdsp->ss_out_channels = RPM_CHANNELS;
4989 hdsp->ds_in_channels = RPM_CHANNELS-1;
4990 hdsp->ds_out_channels = RPM_CHANNELS;
4991 break;
4992
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993 default:
4994 /* should never get here */
4995 break;
4996 }
4997}
4998
Takashi Iwai55e957d2005-11-17 14:52:13 +01004999static void snd_hdsp_initialize_midi_flush (struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000{
5001 snd_hdsp_flush_midi_input (hdsp, 0);
5002 snd_hdsp_flush_midi_input (hdsp, 1);
5003}
5004
Takashi Iwai55e957d2005-11-17 14:52:13 +01005005static int snd_hdsp_create_alsa_devices(struct snd_card *card, struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006{
5007 int err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005008
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009 if ((err = snd_hdsp_create_pcm(card, hdsp)) < 0) {
Takashi Iwaia54ba0f2014-02-26 12:05:11 +01005010 dev_err(card->dev,
5011 "Error creating pcm interface\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012 return err;
5013 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005014
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015
5016 if ((err = snd_hdsp_create_midi(card, hdsp, 0)) < 0) {
Takashi Iwaia54ba0f2014-02-26 12:05:11 +01005017 dev_err(card->dev,
5018 "Error creating first midi interface\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 return err;
5020 }
5021
5022 if (hdsp->io_type == Digiface || hdsp->io_type == H9652) {
5023 if ((err = snd_hdsp_create_midi(card, hdsp, 1)) < 0) {
Takashi Iwaia54ba0f2014-02-26 12:05:11 +01005024 dev_err(card->dev,
5025 "Error creating second midi interface\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026 return err;
5027 }
5028 }
5029
5030 if ((err = snd_hdsp_create_controls(card, hdsp)) < 0) {
Takashi Iwaia54ba0f2014-02-26 12:05:11 +01005031 dev_err(card->dev,
5032 "Error creating ctl interface\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 return err;
5034 }
5035
5036 snd_hdsp_proc_init(hdsp);
5037
5038 hdsp->system_sample_rate = -1;
5039 hdsp->playback_pid = -1;
5040 hdsp->capture_pid = -1;
5041 hdsp->capture_substream = NULL;
5042 hdsp->playback_substream = NULL;
5043
5044 if ((err = snd_hdsp_set_defaults(hdsp)) < 0) {
Takashi Iwaia54ba0f2014-02-26 12:05:11 +01005045 dev_err(card->dev,
5046 "Error setting default values\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047 return err;
5048 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005049
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050 if (!(hdsp->state & HDSP_InitializationComplete)) {
Clemens Ladischb73c1c12005-09-02 08:49:21 +02005051 strcpy(card->shortname, "Hammerfall DSP");
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005052 sprintf(card->longname, "%s at 0x%lx, irq %d", hdsp->card_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053 hdsp->port, hdsp->irq);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005054
Linus Torvalds1da177e2005-04-16 15:20:36 -07005055 if ((err = snd_card_register(card)) < 0) {
Takashi Iwaia54ba0f2014-02-26 12:05:11 +01005056 dev_err(card->dev,
5057 "error registering card\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058 return err;
5059 }
5060 hdsp->state |= HDSP_InitializationComplete;
5061 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005062
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063 return 0;
5064}
5065
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066/* load firmware via hotplug fw loader */
Takashi Iwai92eed662008-02-22 18:35:56 +01005067static int hdsp_request_fw_loader(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068{
5069 const char *fwfile;
5070 const struct firmware *fw;
5071 int err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005072
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073 if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
5074 return 0;
5075 if (hdsp->io_type == Undefined) {
5076 if ((err = hdsp_get_iobox_version(hdsp)) < 0)
5077 return err;
5078 if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
5079 return 0;
5080 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005081
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082 /* caution: max length of firmware filename is 30! */
5083 switch (hdsp->io_type) {
Florian Faber28b26e12010-12-01 12:14:47 +01005084 case RPM:
5085 fwfile = "rpm_firmware.bin";
5086 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087 case Multiface:
5088 if (hdsp->firmware_rev == 0xa)
5089 fwfile = "multiface_firmware.bin";
5090 else
5091 fwfile = "multiface_firmware_rev11.bin";
5092 break;
5093 case Digiface:
5094 if (hdsp->firmware_rev == 0xa)
5095 fwfile = "digiface_firmware.bin";
5096 else
5097 fwfile = "digiface_firmware_rev11.bin";
5098 break;
5099 default:
Takashi Iwaia54ba0f2014-02-26 12:05:11 +01005100 dev_err(hdsp->card->dev,
5101 "invalid io_type %d\n", hdsp->io_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102 return -EINVAL;
5103 }
5104
5105 if (request_firmware(&fw, fwfile, &hdsp->pci->dev)) {
Takashi Iwaia54ba0f2014-02-26 12:05:11 +01005106 dev_err(hdsp->card->dev,
5107 "cannot load firmware %s\n", fwfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108 return -ENOENT;
5109 }
Takashi Iwai90caaef2012-11-22 16:55:11 +01005110 if (fw->size < HDSP_FIRMWARE_SIZE) {
Takashi Iwaia54ba0f2014-02-26 12:05:11 +01005111 dev_err(hdsp->card->dev,
5112 "too short firmware size %d (expected %d)\n",
Takashi Iwai90caaef2012-11-22 16:55:11 +01005113 (int)fw->size, HDSP_FIRMWARE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114 return -EINVAL;
5115 }
Thomas Charbonnel7679a032005-04-25 11:35:29 +02005116
Takashi Iwai90caaef2012-11-22 16:55:11 +01005117 hdsp->firmware = fw;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005118
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119 hdsp->state |= HDSP_FirmwareCached;
5120
5121 if ((err = snd_hdsp_load_firmware_from_cache(hdsp)) < 0)
5122 return err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005123
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124 if (!(hdsp->state & HDSP_InitializationComplete)) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02005125 if ((err = snd_hdsp_enable_io(hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127
5128 if ((err = snd_hdsp_create_hwdep(hdsp->card, hdsp)) < 0) {
Takashi Iwaia54ba0f2014-02-26 12:05:11 +01005129 dev_err(hdsp->card->dev,
5130 "error creating hwdep device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131 return err;
5132 }
5133 snd_hdsp_initialize_channels(hdsp);
5134 snd_hdsp_initialize_midi_flush(hdsp);
5135 if ((err = snd_hdsp_create_alsa_devices(hdsp->card, hdsp)) < 0) {
Takashi Iwaia54ba0f2014-02-26 12:05:11 +01005136 dev_err(hdsp->card->dev,
5137 "error creating alsa devices\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138 return err;
5139 }
5140 }
5141 return 0;
5142}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143
Bill Pembertone23e7a12012-12-06 12:35:10 -05005144static int snd_hdsp_create(struct snd_card *card,
5145 struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146{
5147 struct pci_dev *pci = hdsp->pci;
5148 int err;
5149 int is_9652 = 0;
5150 int is_9632 = 0;
5151
5152 hdsp->irq = -1;
5153 hdsp->state = 0;
5154 hdsp->midi[0].rmidi = NULL;
5155 hdsp->midi[1].rmidi = NULL;
5156 hdsp->midi[0].input = NULL;
5157 hdsp->midi[1].input = NULL;
5158 hdsp->midi[0].output = NULL;
5159 hdsp->midi[1].output = NULL;
5160 hdsp->midi[0].pending = 0;
5161 hdsp->midi[1].pending = 0;
5162 spin_lock_init(&hdsp->midi[0].lock);
5163 spin_lock_init(&hdsp->midi[1].lock);
5164 hdsp->iobase = NULL;
5165 hdsp->control_register = 0;
5166 hdsp->control2_register = 0;
5167 hdsp->io_type = Undefined;
5168 hdsp->max_channels = 26;
5169
5170 hdsp->card = card;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005171
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172 spin_lock_init(&hdsp->lock);
5173
5174 tasklet_init(&hdsp->midi_tasklet, hdsp_midi_tasklet, (unsigned long)hdsp);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005175
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176 pci_read_config_word(hdsp->pci, PCI_CLASS_REVISION, &hdsp->firmware_rev);
5177 hdsp->firmware_rev &= 0xff;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005178
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179 /* From Martin Bjoernsen :
5180 "It is important that the card's latency timer register in
5181 the PCI configuration space is set to a value much larger
5182 than 0 by the computer's BIOS or the driver.
5183 The windows driver always sets this 8 bit register [...]
5184 to its maximum 255 to avoid problems with some computers."
5185 */
5186 pci_write_config_byte(hdsp->pci, PCI_LATENCY_TIMER, 0xFF);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005187
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188 strcpy(card->driver, "H-DSP");
5189 strcpy(card->mixername, "Xilinx FPGA");
5190
Takashi Iwaib0b98112005-10-20 18:29:58 +02005191 if (hdsp->firmware_rev < 0xa)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192 return -ENODEV;
Takashi Iwaib0b98112005-10-20 18:29:58 +02005193 else if (hdsp->firmware_rev < 0x64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194 hdsp->card_name = "RME Hammerfall DSP";
Takashi Iwaib0b98112005-10-20 18:29:58 +02005195 else if (hdsp->firmware_rev < 0x96) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196 hdsp->card_name = "RME HDSP 9652";
5197 is_9652 = 1;
5198 } else {
5199 hdsp->card_name = "RME HDSP 9632";
5200 hdsp->max_channels = 16;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005201 is_9632 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202 }
5203
Takashi Iwaib0b98112005-10-20 18:29:58 +02005204 if ((err = pci_enable_device(pci)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206
5207 pci_set_master(hdsp->pci);
5208
5209 if ((err = pci_request_regions(pci, "hdsp")) < 0)
5210 return err;
5211 hdsp->port = pci_resource_start(pci, 0);
5212 if ((hdsp->iobase = ioremap_nocache(hdsp->port, HDSP_IO_EXTENT)) == NULL) {
Takashi Iwaia54ba0f2014-02-26 12:05:11 +01005213 dev_err(hdsp->card->dev, "unable to remap region 0x%lx-0x%lx\n",
5214 hdsp->port, hdsp->port + HDSP_IO_EXTENT - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215 return -EBUSY;
5216 }
5217
Takashi Iwai437a5a42006-11-21 12:14:23 +01005218 if (request_irq(pci->irq, snd_hdsp_interrupt, IRQF_SHARED,
Takashi Iwai934c2b62011-06-10 16:36:37 +02005219 KBUILD_MODNAME, hdsp)) {
Takashi Iwaia54ba0f2014-02-26 12:05:11 +01005220 dev_err(hdsp->card->dev, "unable to use IRQ %d\n", pci->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221 return -EBUSY;
5222 }
5223
5224 hdsp->irq = pci->irq;
Remy Bruno176546a2006-10-16 12:32:53 +02005225 hdsp->precise_ptr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226 hdsp->use_midi_tasklet = 1;
Remy Brunod7923b22006-10-17 12:41:56 +02005227 hdsp->dds_value = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228
Takashi Iwaib0b98112005-10-20 18:29:58 +02005229 if ((err = snd_hdsp_initialize_memory(hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230 return err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005231
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232 if (!is_9652 && !is_9632) {
Tim Blechmanne588ed82009-02-20 19:30:35 +01005233 /* we wait a maximum of 10 seconds to let freshly
5234 * inserted cardbus cards do their hardware init */
5235 err = hdsp_wait_for_iobox(hdsp, 1000, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236
Tim Blechmann00c9ddd2008-11-09 12:50:52 +01005237 if (err < 0)
5238 return err;
5239
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240 if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02005241 if ((err = hdsp_request_fw_loader(hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242 /* we don't fail as this can happen
5243 if userspace is not ready for
5244 firmware upload
5245 */
Takashi Iwaia54ba0f2014-02-26 12:05:11 +01005246 dev_err(hdsp->card->dev,
5247 "couldn't get firmware from userspace. try using hdsploader\n");
Takashi Iwaib0b98112005-10-20 18:29:58 +02005248 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249 /* init is complete, we return */
5250 return 0;
Tim Blechmann00c9ddd2008-11-09 12:50:52 +01005251 /* we defer initialization */
Takashi Iwaia54ba0f2014-02-26 12:05:11 +01005252 dev_info(hdsp->card->dev,
5253 "card initialization pending : waiting for firmware\n");
Takashi Iwaib0b98112005-10-20 18:29:58 +02005254 if ((err = snd_hdsp_create_hwdep(card, hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256 return 0;
5257 } else {
Takashi Iwaia54ba0f2014-02-26 12:05:11 +01005258 dev_info(hdsp->card->dev,
5259 "Firmware already present, initializing card.\n");
Florian Faber28b26e12010-12-01 12:14:47 +01005260 if (hdsp_read(hdsp, HDSP_status2Register) & HDSP_version2)
5261 hdsp->io_type = RPM;
5262 else if (hdsp_read(hdsp, HDSP_status2Register) & HDSP_version1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 hdsp->io_type = Multiface;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005264 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265 hdsp->io_type = Digiface;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266 }
5267 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005268
Takashi Iwaib0b98112005-10-20 18:29:58 +02005269 if ((err = snd_hdsp_enable_io(hdsp)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 return err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005271
Takashi Iwaib0b98112005-10-20 18:29:58 +02005272 if (is_9652)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273 hdsp->io_type = H9652;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005274
Takashi Iwaib0b98112005-10-20 18:29:58 +02005275 if (is_9632)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276 hdsp->io_type = H9632;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277
Takashi Iwaib0b98112005-10-20 18:29:58 +02005278 if ((err = snd_hdsp_create_hwdep(card, hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279 return err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005280
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281 snd_hdsp_initialize_channels(hdsp);
5282 snd_hdsp_initialize_midi_flush(hdsp);
5283
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005284 hdsp->state |= HDSP_FirmwareLoaded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285
Takashi Iwaib0b98112005-10-20 18:29:58 +02005286 if ((err = snd_hdsp_create_alsa_devices(card, hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005288
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005289 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290}
5291
Takashi Iwai55e957d2005-11-17 14:52:13 +01005292static int snd_hdsp_free(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293{
5294 if (hdsp->port) {
5295 /* stop the audio, and cancel all interrupts */
5296 tasklet_kill(&hdsp->midi_tasklet);
5297 hdsp->control_register &= ~(HDSP_Start|HDSP_AudioInterruptEnable|HDSP_Midi0InterruptEnable|HDSP_Midi1InterruptEnable);
5298 hdsp_write (hdsp, HDSP_controlRegister, hdsp->control_register);
5299 }
5300
5301 if (hdsp->irq >= 0)
5302 free_irq(hdsp->irq, (void *)hdsp);
5303
5304 snd_hdsp_free_buffers(hdsp);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005305
Markus Elfringc2836612014-11-17 13:04:14 +01005306 release_firmware(hdsp->firmware);
Takashi Iwai90caaef2012-11-22 16:55:11 +01005307 vfree(hdsp->fw_uploaded);
Markus Elfringff6defa2015-01-03 22:55:54 +01005308 iounmap(hdsp->iobase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309
5310 if (hdsp->port)
5311 pci_release_regions(hdsp->pci);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005312
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313 pci_disable_device(hdsp->pci);
5314 return 0;
5315}
5316
Takashi Iwai55e957d2005-11-17 14:52:13 +01005317static void snd_hdsp_card_free(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318{
Joe Perches9fe856e2010-09-04 18:52:54 -07005319 struct hdsp *hdsp = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320
5321 if (hdsp)
5322 snd_hdsp_free(hdsp);
5323}
5324
Bill Pembertone23e7a12012-12-06 12:35:10 -05005325static int snd_hdsp_probe(struct pci_dev *pci,
5326 const struct pci_device_id *pci_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327{
5328 static int dev;
Takashi Iwai55e957d2005-11-17 14:52:13 +01005329 struct hdsp *hdsp;
5330 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331 int err;
5332
5333 if (dev >= SNDRV_CARDS)
5334 return -ENODEV;
5335 if (!enable[dev]) {
5336 dev++;
5337 return -ENOENT;
5338 }
5339
Takashi Iwai60c57722014-01-29 14:20:19 +01005340 err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
5341 sizeof(struct hdsp), &card);
Takashi Iwaie58de7b2008-12-28 16:44:30 +01005342 if (err < 0)
5343 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344
Joe Perches9fe856e2010-09-04 18:52:54 -07005345 hdsp = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346 card->private_free = snd_hdsp_card_free;
5347 hdsp->dev = dev;
5348 hdsp->pci = pci;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349
5350 if ((err = snd_hdsp_create(card, hdsp)) < 0) {
5351 snd_card_free(card);
5352 return err;
5353 }
5354
5355 strcpy(card->shortname, "Hammerfall DSP");
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005356 sprintf(card->longname, "%s at 0x%lx, irq %d", hdsp->card_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 hdsp->port, hdsp->irq);
5358
5359 if ((err = snd_card_register(card)) < 0) {
5360 snd_card_free(card);
5361 return err;
5362 }
5363 pci_set_drvdata(pci, card);
5364 dev++;
5365 return 0;
5366}
5367
Bill Pembertone23e7a12012-12-06 12:35:10 -05005368static void snd_hdsp_remove(struct pci_dev *pci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369{
5370 snd_card_free(pci_get_drvdata(pci));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371}
5372
Takashi Iwaie9f66d92012-04-24 12:25:00 +02005373static struct pci_driver hdsp_driver = {
Takashi Iwai3733e422011-06-10 16:20:20 +02005374 .name = KBUILD_MODNAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375 .id_table = snd_hdsp_ids,
5376 .probe = snd_hdsp_probe,
Bill Pembertone23e7a12012-12-06 12:35:10 -05005377 .remove = snd_hdsp_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378};
5379
Takashi Iwaie9f66d92012-04-24 12:25:00 +02005380module_pci_driver(hdsp_driver);