blob: cf5a6c8b9a63a36aee58b438e58190be9f6231df [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ALSA driver for RME Hammerfall DSP audio interface(s)
3 *
4 * Copyright (c) 2002 Paul Davis
5 * Marcus Andersson
6 * Thomas Charbonnel
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/init.h>
25#include <linux/delay.h>
26#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/pci.h>
28#include <linux/firmware.h>
Paul Gortmaker65a77212011-07-15 13:13:37 -040029#include <linux/module.h>
Takashi Iwai3f7440a2009-06-05 17:40:04 +020030#include <linux/math64.h>
Takashi Iwai82329322012-11-22 21:19:18 +010031#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#include <sound/core.h>
34#include <sound/control.h>
35#include <sound/pcm.h>
36#include <sound/info.h>
37#include <sound/asoundef.h>
38#include <sound/rawmidi.h>
39#include <sound/hwdep.h>
40#include <sound/initval.h>
41#include <sound/hdsp.h>
42
43#include <asm/byteorder.h>
44#include <asm/current.h>
45#include <asm/io.h>
46
47static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
48static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
Rusty Russella67ff6a2011-12-15 13:49:36 +103049static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51module_param_array(index, int, NULL, 0444);
52MODULE_PARM_DESC(index, "Index value for RME Hammerfall DSP interface.");
53module_param_array(id, charp, NULL, 0444);
54MODULE_PARM_DESC(id, "ID string for RME Hammerfall DSP interface.");
55module_param_array(enable, bool, NULL, 0444);
56MODULE_PARM_DESC(enable, "Enable/disable specific Hammerfall DSP soundcards.");
57MODULE_AUTHOR("Paul Davis <paul@linuxaudiosystems.com>, Marcus Andersson, Thomas Charbonnel <thomas@undata.org>");
58MODULE_DESCRIPTION("RME Hammerfall DSP");
59MODULE_LICENSE("GPL");
60MODULE_SUPPORTED_DEVICE("{{RME Hammerfall-DSP},"
61 "{RME HDSP-9652},"
62 "{RME HDSP-9632}}");
Florian Faber28b26e12010-12-01 12:14:47 +010063MODULE_FIRMWARE("rpm_firmware.bin");
Clemens Ladisch7e0af292007-05-03 17:59:54 +020064MODULE_FIRMWARE("multiface_firmware.bin");
65MODULE_FIRMWARE("multiface_firmware_rev11.bin");
66MODULE_FIRMWARE("digiface_firmware.bin");
67MODULE_FIRMWARE("digiface_firmware_rev11.bin");
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69#define HDSP_MAX_CHANNELS 26
70#define HDSP_MAX_DS_CHANNELS 14
71#define HDSP_MAX_QS_CHANNELS 8
72#define DIGIFACE_SS_CHANNELS 26
73#define DIGIFACE_DS_CHANNELS 14
74#define MULTIFACE_SS_CHANNELS 18
75#define MULTIFACE_DS_CHANNELS 14
76#define H9652_SS_CHANNELS 26
77#define H9652_DS_CHANNELS 14
78/* This does not include possible Analog Extension Boards
79 AEBs are detected at card initialization
80*/
81#define H9632_SS_CHANNELS 12
82#define H9632_DS_CHANNELS 8
83#define H9632_QS_CHANNELS 4
Florian Faber28b26e12010-12-01 12:14:47 +010084#define RPM_CHANNELS 6
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86/* Write registers. These are defined as byte-offsets from the iobase value.
87 */
88#define HDSP_resetPointer 0
Remy Brunod7923b22006-10-17 12:41:56 +020089#define HDSP_freqReg 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#define HDSP_outputBufferAddress 32
91#define HDSP_inputBufferAddress 36
92#define HDSP_controlRegister 64
93#define HDSP_interruptConfirmation 96
94#define HDSP_outputEnable 128
95#define HDSP_control2Reg 256
96#define HDSP_midiDataOut0 352
97#define HDSP_midiDataOut1 356
98#define HDSP_fifoData 368
99#define HDSP_inputEnable 384
100
101/* Read registers. These are defined as byte-offsets from the iobase value
102 */
103
104#define HDSP_statusRegister 0
105#define HDSP_timecode 128
106#define HDSP_status2Register 192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107#define HDSP_midiDataIn0 360
108#define HDSP_midiDataIn1 364
109#define HDSP_midiStatusOut0 384
110#define HDSP_midiStatusOut1 388
111#define HDSP_midiStatusIn0 392
112#define HDSP_midiStatusIn1 396
113#define HDSP_fifoStatus 400
114
115/* the meters are regular i/o-mapped registers, but offset
116 considerably from the rest. the peak registers are reset
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100117 when read; the least-significant 4 bits are full-scale counters;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 the actual peak value is in the most-significant 24 bits.
119*/
120
121#define HDSP_playbackPeakLevel 4096 /* 26 * 32 bit values */
122#define HDSP_inputPeakLevel 4224 /* 26 * 32 bit values */
123#define HDSP_outputPeakLevel 4352 /* (26+2) * 32 bit values */
124#define HDSP_playbackRmsLevel 4612 /* 26 * 64 bit values */
125#define HDSP_inputRmsLevel 4868 /* 26 * 64 bit values */
126
127
128/* This is for H9652 cards
129 Peak values are read downward from the base
130 Rms values are read upward
131 There are rms values for the outputs too
132 26*3 values are read in ss mode
133 14*3 in ds mode, with no gap between values
134*/
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100135#define HDSP_9652_peakBase 7164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136#define HDSP_9652_rmsBase 4096
137
138/* c.f. the hdsp_9632_meters_t struct */
139#define HDSP_9632_metersBase 4096
140
141#define HDSP_IO_EXTENT 7168
142
143/* control2 register bits */
144
145#define HDSP_TMS 0x01
146#define HDSP_TCK 0x02
147#define HDSP_TDI 0x04
148#define HDSP_JTAG 0x08
149#define HDSP_PWDN 0x10
150#define HDSP_PROGRAM 0x020
151#define HDSP_CONFIG_MODE_0 0x040
152#define HDSP_CONFIG_MODE_1 0x080
Adrian Knotha3466862011-10-27 21:57:53 +0200153#define HDSP_VERSION_BIT (0x100 | HDSP_S_LOAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154#define HDSP_BIGENDIAN_MODE 0x200
155#define HDSP_RD_MULTIPLE 0x400
156#define HDSP_9652_ENABLE_MIXER 0x800
Adrian Knoth0c2bc7c2013-01-15 18:52:20 +0100157#define HDSP_S200 0x800
158#define HDSP_S300 (0x100 | HDSP_S200) /* dummy, purpose of 0x100 unknown */
159#define HDSP_CYCLIC_MODE 0x1000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160#define HDSP_TDO 0x10000000
161
Adrian Knoth0c2bc7c2013-01-15 18:52:20 +0100162#define HDSP_S_PROGRAM (HDSP_CYCLIC_MODE|HDSP_PROGRAM|HDSP_CONFIG_MODE_0)
163#define HDSP_S_LOAD (HDSP_CYCLIC_MODE|HDSP_PROGRAM|HDSP_CONFIG_MODE_1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165/* Control Register bits */
166
167#define HDSP_Start (1<<0) /* start engine */
168#define HDSP_Latency0 (1<<1) /* buffer size = 2^n where n is defined by Latency{2,1,0} */
169#define HDSP_Latency1 (1<<2) /* [ see above ] */
170#define HDSP_Latency2 (1<<3) /* [ see above ] */
171#define HDSP_ClockModeMaster (1<<4) /* 1=Master, 0=Slave/Autosync */
172#define HDSP_AudioInterruptEnable (1<<5) /* what do you think ? */
173#define HDSP_Frequency0 (1<<6) /* 0=44.1kHz/88.2kHz/176.4kHz 1=48kHz/96kHz/192kHz */
174#define HDSP_Frequency1 (1<<7) /* 0=32kHz/64kHz/128kHz */
175#define HDSP_DoubleSpeed (1<<8) /* 0=normal speed, 1=double speed */
176#define HDSP_SPDIFProfessional (1<<9) /* 0=consumer, 1=professional */
177#define HDSP_SPDIFEmphasis (1<<10) /* 0=none, 1=on */
178#define HDSP_SPDIFNonAudio (1<<11) /* 0=off, 1=on */
179#define HDSP_SPDIFOpticalOut (1<<12) /* 1=use 1st ADAT connector for SPDIF, 0=do not */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100180#define HDSP_SyncRef2 (1<<13)
181#define HDSP_SPDIFInputSelect0 (1<<14)
182#define HDSP_SPDIFInputSelect1 (1<<15)
183#define HDSP_SyncRef0 (1<<16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184#define HDSP_SyncRef1 (1<<17)
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100185#define HDSP_AnalogExtensionBoard (1<<18) /* For H9632 cards */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186#define HDSP_XLRBreakoutCable (1<<20) /* For H9632 cards */
187#define HDSP_Midi0InterruptEnable (1<<22)
188#define HDSP_Midi1InterruptEnable (1<<23)
189#define HDSP_LineOut (1<<24)
190#define HDSP_ADGain0 (1<<25) /* From here : H9632 specific */
191#define HDSP_ADGain1 (1<<26)
192#define HDSP_DAGain0 (1<<27)
193#define HDSP_DAGain1 (1<<28)
194#define HDSP_PhoneGain0 (1<<29)
195#define HDSP_PhoneGain1 (1<<30)
196#define HDSP_QuadSpeed (1<<31)
197
Florian Faber28b26e12010-12-01 12:14:47 +0100198/* RPM uses some of the registers for special purposes */
199#define HDSP_RPM_Inp12 0x04A00
200#define HDSP_RPM_Inp12_Phon_6dB 0x00800 /* Dolby */
201#define HDSP_RPM_Inp12_Phon_0dB 0x00000 /* .. */
202#define HDSP_RPM_Inp12_Phon_n6dB 0x04000 /* inp_0 */
203#define HDSP_RPM_Inp12_Line_0dB 0x04200 /* Dolby+PRO */
204#define HDSP_RPM_Inp12_Line_n6dB 0x00200 /* PRO */
205
206#define HDSP_RPM_Inp34 0x32000
207#define HDSP_RPM_Inp34_Phon_6dB 0x20000 /* SyncRef1 */
208#define HDSP_RPM_Inp34_Phon_0dB 0x00000 /* .. */
209#define HDSP_RPM_Inp34_Phon_n6dB 0x02000 /* SyncRef2 */
210#define HDSP_RPM_Inp34_Line_0dB 0x30000 /* SyncRef1+SyncRef0 */
211#define HDSP_RPM_Inp34_Line_n6dB 0x10000 /* SyncRef0 */
212
213#define HDSP_RPM_Bypass 0x01000
214
215#define HDSP_RPM_Disconnect 0x00001
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217#define HDSP_ADGainMask (HDSP_ADGain0|HDSP_ADGain1)
218#define HDSP_ADGainMinus10dBV HDSP_ADGainMask
219#define HDSP_ADGainPlus4dBu (HDSP_ADGain0)
220#define HDSP_ADGainLowGain 0
221
222#define HDSP_DAGainMask (HDSP_DAGain0|HDSP_DAGain1)
223#define HDSP_DAGainHighGain HDSP_DAGainMask
224#define HDSP_DAGainPlus4dBu (HDSP_DAGain0)
225#define HDSP_DAGainMinus10dBV 0
226
227#define HDSP_PhoneGainMask (HDSP_PhoneGain0|HDSP_PhoneGain1)
228#define HDSP_PhoneGain0dB HDSP_PhoneGainMask
229#define HDSP_PhoneGainMinus6dB (HDSP_PhoneGain0)
230#define HDSP_PhoneGainMinus12dB 0
231
232#define HDSP_LatencyMask (HDSP_Latency0|HDSP_Latency1|HDSP_Latency2)
233#define HDSP_FrequencyMask (HDSP_Frequency0|HDSP_Frequency1|HDSP_DoubleSpeed|HDSP_QuadSpeed)
234
235#define HDSP_SPDIFInputMask (HDSP_SPDIFInputSelect0|HDSP_SPDIFInputSelect1)
236#define HDSP_SPDIFInputADAT1 0
237#define HDSP_SPDIFInputCoaxial (HDSP_SPDIFInputSelect0)
238#define HDSP_SPDIFInputCdrom (HDSP_SPDIFInputSelect1)
239#define HDSP_SPDIFInputAES (HDSP_SPDIFInputSelect0|HDSP_SPDIFInputSelect1)
240
241#define HDSP_SyncRefMask (HDSP_SyncRef0|HDSP_SyncRef1|HDSP_SyncRef2)
242#define HDSP_SyncRef_ADAT1 0
243#define HDSP_SyncRef_ADAT2 (HDSP_SyncRef0)
244#define HDSP_SyncRef_ADAT3 (HDSP_SyncRef1)
245#define HDSP_SyncRef_SPDIF (HDSP_SyncRef0|HDSP_SyncRef1)
246#define HDSP_SyncRef_WORD (HDSP_SyncRef2)
247#define HDSP_SyncRef_ADAT_SYNC (HDSP_SyncRef0|HDSP_SyncRef2)
248
249/* Sample Clock Sources */
250
251#define HDSP_CLOCK_SOURCE_AUTOSYNC 0
252#define HDSP_CLOCK_SOURCE_INTERNAL_32KHZ 1
253#define HDSP_CLOCK_SOURCE_INTERNAL_44_1KHZ 2
254#define HDSP_CLOCK_SOURCE_INTERNAL_48KHZ 3
255#define HDSP_CLOCK_SOURCE_INTERNAL_64KHZ 4
256#define HDSP_CLOCK_SOURCE_INTERNAL_88_2KHZ 5
257#define HDSP_CLOCK_SOURCE_INTERNAL_96KHZ 6
258#define HDSP_CLOCK_SOURCE_INTERNAL_128KHZ 7
259#define HDSP_CLOCK_SOURCE_INTERNAL_176_4KHZ 8
260#define HDSP_CLOCK_SOURCE_INTERNAL_192KHZ 9
261
262/* Preferred sync reference choices - used by "pref_sync_ref" control switch */
263
264#define HDSP_SYNC_FROM_WORD 0
265#define HDSP_SYNC_FROM_SPDIF 1
266#define HDSP_SYNC_FROM_ADAT1 2
267#define HDSP_SYNC_FROM_ADAT_SYNC 3
268#define HDSP_SYNC_FROM_ADAT2 4
269#define HDSP_SYNC_FROM_ADAT3 5
270
271/* SyncCheck status */
272
273#define HDSP_SYNC_CHECK_NO_LOCK 0
274#define HDSP_SYNC_CHECK_LOCK 1
275#define HDSP_SYNC_CHECK_SYNC 2
276
277/* AutoSync references - used by "autosync_ref" control switch */
278
279#define HDSP_AUTOSYNC_FROM_WORD 0
280#define HDSP_AUTOSYNC_FROM_ADAT_SYNC 1
281#define HDSP_AUTOSYNC_FROM_SPDIF 2
282#define HDSP_AUTOSYNC_FROM_NONE 3
283#define HDSP_AUTOSYNC_FROM_ADAT1 4
284#define HDSP_AUTOSYNC_FROM_ADAT2 5
285#define HDSP_AUTOSYNC_FROM_ADAT3 6
286
287/* Possible sources of S/PDIF input */
288
289#define HDSP_SPDIFIN_OPTICAL 0 /* optical (ADAT1) */
290#define HDSP_SPDIFIN_COAXIAL 1 /* coaxial (RCA) */
291#define HDSP_SPDIFIN_INTERNAL 2 /* internal (CDROM) */
292#define HDSP_SPDIFIN_AES 3 /* xlr for H9632 (AES)*/
293
294#define HDSP_Frequency32KHz HDSP_Frequency0
295#define HDSP_Frequency44_1KHz HDSP_Frequency1
296#define HDSP_Frequency48KHz (HDSP_Frequency1|HDSP_Frequency0)
297#define HDSP_Frequency64KHz (HDSP_DoubleSpeed|HDSP_Frequency0)
298#define HDSP_Frequency88_2KHz (HDSP_DoubleSpeed|HDSP_Frequency1)
299#define HDSP_Frequency96KHz (HDSP_DoubleSpeed|HDSP_Frequency1|HDSP_Frequency0)
300/* For H9632 cards */
301#define HDSP_Frequency128KHz (HDSP_QuadSpeed|HDSP_DoubleSpeed|HDSP_Frequency0)
302#define HDSP_Frequency176_4KHz (HDSP_QuadSpeed|HDSP_DoubleSpeed|HDSP_Frequency1)
303#define HDSP_Frequency192KHz (HDSP_QuadSpeed|HDSP_DoubleSpeed|HDSP_Frequency1|HDSP_Frequency0)
Julian Cablee4b60882007-03-19 11:44:40 +0100304/* RME says n = 104857600000000, but in the windows MADI driver, I see:
305 return 104857600000000 / rate; // 100 MHz
306 return 110100480000000 / rate; // 105 MHz
307*/
308#define DDS_NUMERATOR 104857600000000ULL; /* = 2^20 * 10^8 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310#define hdsp_encode_latency(x) (((x)<<1) & HDSP_LatencyMask)
311#define hdsp_decode_latency(x) (((x) & HDSP_LatencyMask)>>1)
312
313#define hdsp_encode_spdif_in(x) (((x)&0x3)<<14)
314#define hdsp_decode_spdif_in(x) (((x)>>14)&0x3)
315
316/* Status Register bits */
317
318#define HDSP_audioIRQPending (1<<0)
319#define HDSP_Lock2 (1<<1) /* this is for Digiface and H9652 */
320#define HDSP_spdifFrequency3 HDSP_Lock2 /* this is for H9632 only */
321#define HDSP_Lock1 (1<<2)
322#define HDSP_Lock0 (1<<3)
323#define HDSP_SPDIFSync (1<<4)
324#define HDSP_TimecodeLock (1<<5)
325#define HDSP_BufferPositionMask 0x000FFC0 /* Bit 6..15 : h/w buffer pointer */
326#define HDSP_Sync2 (1<<16)
327#define HDSP_Sync1 (1<<17)
328#define HDSP_Sync0 (1<<18)
329#define HDSP_DoubleSpeedStatus (1<<19)
330#define HDSP_ConfigError (1<<20)
331#define HDSP_DllError (1<<21)
332#define HDSP_spdifFrequency0 (1<<22)
333#define HDSP_spdifFrequency1 (1<<23)
334#define HDSP_spdifFrequency2 (1<<24)
335#define HDSP_SPDIFErrorFlag (1<<25)
336#define HDSP_BufferID (1<<26)
337#define HDSP_TimecodeSync (1<<27)
338#define HDSP_AEBO (1<<28) /* H9632 specific Analog Extension Boards */
339#define HDSP_AEBI (1<<29) /* 0 = present, 1 = absent */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100340#define HDSP_midi0IRQPending (1<<30)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341#define HDSP_midi1IRQPending (1<<31)
342
343#define HDSP_spdifFrequencyMask (HDSP_spdifFrequency0|HDSP_spdifFrequency1|HDSP_spdifFrequency2)
Remy Bruno47ba97f2008-02-22 17:57:02 +0100344#define HDSP_spdifFrequencyMask_9632 (HDSP_spdifFrequency0|\
345 HDSP_spdifFrequency1|\
346 HDSP_spdifFrequency2|\
347 HDSP_spdifFrequency3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349#define HDSP_spdifFrequency32KHz (HDSP_spdifFrequency0)
350#define HDSP_spdifFrequency44_1KHz (HDSP_spdifFrequency1)
351#define HDSP_spdifFrequency48KHz (HDSP_spdifFrequency0|HDSP_spdifFrequency1)
352
353#define HDSP_spdifFrequency64KHz (HDSP_spdifFrequency2)
354#define HDSP_spdifFrequency88_2KHz (HDSP_spdifFrequency0|HDSP_spdifFrequency2)
355#define HDSP_spdifFrequency96KHz (HDSP_spdifFrequency2|HDSP_spdifFrequency1)
356
357/* This is for H9632 cards */
Remy Bruno47ba97f2008-02-22 17:57:02 +0100358#define HDSP_spdifFrequency128KHz (HDSP_spdifFrequency0|\
359 HDSP_spdifFrequency1|\
360 HDSP_spdifFrequency2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361#define HDSP_spdifFrequency176_4KHz HDSP_spdifFrequency3
362#define HDSP_spdifFrequency192KHz (HDSP_spdifFrequency3|HDSP_spdifFrequency0)
363
364/* Status2 Register bits */
365
366#define HDSP_version0 (1<<0)
367#define HDSP_version1 (1<<1)
368#define HDSP_version2 (1<<2)
369#define HDSP_wc_lock (1<<3)
370#define HDSP_wc_sync (1<<4)
371#define HDSP_inp_freq0 (1<<5)
372#define HDSP_inp_freq1 (1<<6)
373#define HDSP_inp_freq2 (1<<7)
374#define HDSP_SelSyncRef0 (1<<8)
375#define HDSP_SelSyncRef1 (1<<9)
376#define HDSP_SelSyncRef2 (1<<10)
377
378#define HDSP_wc_valid (HDSP_wc_lock|HDSP_wc_sync)
379
380#define HDSP_systemFrequencyMask (HDSP_inp_freq0|HDSP_inp_freq1|HDSP_inp_freq2)
381#define HDSP_systemFrequency32 (HDSP_inp_freq0)
382#define HDSP_systemFrequency44_1 (HDSP_inp_freq1)
383#define HDSP_systemFrequency48 (HDSP_inp_freq0|HDSP_inp_freq1)
384#define HDSP_systemFrequency64 (HDSP_inp_freq2)
385#define HDSP_systemFrequency88_2 (HDSP_inp_freq0|HDSP_inp_freq2)
386#define HDSP_systemFrequency96 (HDSP_inp_freq1|HDSP_inp_freq2)
387/* FIXME : more values for 9632 cards ? */
388
389#define HDSP_SelSyncRefMask (HDSP_SelSyncRef0|HDSP_SelSyncRef1|HDSP_SelSyncRef2)
390#define HDSP_SelSyncRef_ADAT1 0
391#define HDSP_SelSyncRef_ADAT2 (HDSP_SelSyncRef0)
392#define HDSP_SelSyncRef_ADAT3 (HDSP_SelSyncRef1)
393#define HDSP_SelSyncRef_SPDIF (HDSP_SelSyncRef0|HDSP_SelSyncRef1)
394#define HDSP_SelSyncRef_WORD (HDSP_SelSyncRef2)
395#define HDSP_SelSyncRef_ADAT_SYNC (HDSP_SelSyncRef0|HDSP_SelSyncRef2)
396
397/* Card state flags */
398
399#define HDSP_InitializationComplete (1<<0)
400#define HDSP_FirmwareLoaded (1<<1)
401#define HDSP_FirmwareCached (1<<2)
402
403/* FIFO wait times, defined in terms of 1/10ths of msecs */
404
405#define HDSP_LONG_WAIT 5000
406#define HDSP_SHORT_WAIT 30
407
408#define UNITY_GAIN 32768
409#define MINUS_INFINITY_GAIN 0
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411/* the size of a substream (1 mono data stream) */
412
413#define HDSP_CHANNEL_BUFFER_SAMPLES (16*1024)
414#define HDSP_CHANNEL_BUFFER_BYTES (4*HDSP_CHANNEL_BUFFER_SAMPLES)
415
416/* the size of the area we need to allocate for DMA transfers. the
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100417 size is the same regardless of the number of channels - the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 Multiface still uses the same memory area.
419
420 Note that we allocate 1 more channel than is apparently needed
421 because the h/w seems to write 1 byte beyond the end of the last
422 page. Sigh.
423*/
424
425#define HDSP_DMA_AREA_BYTES ((HDSP_MAX_CHANNELS+1) * HDSP_CHANNEL_BUFFER_BYTES)
426#define HDSP_DMA_AREA_KILOBYTES (HDSP_DMA_AREA_BYTES/1024)
427
Takashi Iwai90caaef2012-11-22 16:55:11 +0100428#define HDSP_FIRMWARE_SIZE (24413 * 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Takashi Iwai55e957d2005-11-17 14:52:13 +0100430struct hdsp_9632_meters {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 u32 input_peak[16];
432 u32 playback_peak[16];
433 u32 output_peak[16];
434 u32 xxx_peak[16];
435 u32 padding[64];
436 u32 input_rms_low[16];
437 u32 playback_rms_low[16];
438 u32 output_rms_low[16];
439 u32 xxx_rms_low[16];
440 u32 input_rms_high[16];
441 u32 playback_rms_high[16];
442 u32 output_rms_high[16];
443 u32 xxx_rms_high[16];
444};
445
Takashi Iwai55e957d2005-11-17 14:52:13 +0100446struct hdsp_midi {
447 struct hdsp *hdsp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 int id;
Takashi Iwai55e957d2005-11-17 14:52:13 +0100449 struct snd_rawmidi *rmidi;
450 struct snd_rawmidi_substream *input;
451 struct snd_rawmidi_substream *output;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 char istimer; /* timer in use */
453 struct timer_list timer;
454 spinlock_t lock;
455 int pending;
456};
457
Takashi Iwai55e957d2005-11-17 14:52:13 +0100458struct hdsp {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 spinlock_t lock;
Takashi Iwai55e957d2005-11-17 14:52:13 +0100460 struct snd_pcm_substream *capture_substream;
461 struct snd_pcm_substream *playback_substream;
462 struct hdsp_midi midi[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 struct tasklet_struct midi_tasklet;
464 int use_midi_tasklet;
465 int precise_ptr;
466 u32 control_register; /* cached value */
467 u32 control2_register; /* cached value */
468 u32 creg_spdif;
469 u32 creg_spdif_stream;
Takashi Iwaie3ea4d82005-07-04 18:12:39 +0200470 int clock_source_locked;
Florian Faber28b26e12010-12-01 12:14:47 +0100471 char *card_name; /* digiface/multiface/rpm */
Takashi Iwai55e957d2005-11-17 14:52:13 +0100472 enum HDSP_IO_Type io_type; /* ditto, but for code use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 unsigned short firmware_rev;
474 unsigned short state; /* stores state bits */
Takashi Iwai90caaef2012-11-22 16:55:11 +0100475 const struct firmware *firmware;
476 u32 *fw_uploaded;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 size_t period_bytes; /* guess what this is */
478 unsigned char max_channels;
479 unsigned char qs_in_channels; /* quad speed mode for H9632 */
480 unsigned char ds_in_channels;
481 unsigned char ss_in_channels; /* different for multiface/digiface */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100482 unsigned char qs_out_channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 unsigned char ds_out_channels;
484 unsigned char ss_out_channels;
485
486 struct snd_dma_buffer capture_dma_buf;
487 struct snd_dma_buffer playback_dma_buf;
488 unsigned char *capture_buffer; /* suitably aligned address */
489 unsigned char *playback_buffer; /* suitably aligned address */
490
491 pid_t capture_pid;
492 pid_t playback_pid;
493 int running;
494 int system_sample_rate;
495 char *channel_map;
496 int dev;
497 int irq;
498 unsigned long port;
499 void __iomem *iobase;
Takashi Iwai55e957d2005-11-17 14:52:13 +0100500 struct snd_card *card;
501 struct snd_pcm *pcm;
502 struct snd_hwdep *hwdep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 struct pci_dev *pci;
Takashi Iwai55e957d2005-11-17 14:52:13 +0100504 struct snd_kcontrol *spdif_ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 unsigned short mixer_matrix[HDSP_MATRIX_MIXER_SIZE];
Remy Brunod7923b22006-10-17 12:41:56 +0200506 unsigned int dds_value; /* last value written to freq register */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507};
508
509/* These tables map the ALSA channels 1..N to the channels that we
510 need to use in order to find the relevant channel buffer. RME
511 refer to this kind of mapping as between "the ADAT channel and
512 the DMA channel." We index it using the logical audio channel,
513 and the value is the DMA channel (i.e. channel buffer number)
514 where the data for that channel can be read/written from/to.
515*/
516
517static char channel_map_df_ss[HDSP_MAX_CHANNELS] = {
518 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
519 18, 19, 20, 21, 22, 23, 24, 25
520};
521
522static char channel_map_mf_ss[HDSP_MAX_CHANNELS] = { /* Multiface */
523 /* Analog */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100524 0, 1, 2, 3, 4, 5, 6, 7,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 /* ADAT 2 */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100526 16, 17, 18, 19, 20, 21, 22, 23,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 /* SPDIF */
528 24, 25,
529 -1, -1, -1, -1, -1, -1, -1, -1
530};
531
532static char channel_map_ds[HDSP_MAX_CHANNELS] = {
533 /* ADAT channels are remapped */
534 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23,
535 /* channels 12 and 13 are S/PDIF */
536 24, 25,
537 /* others don't exist */
538 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
539};
540
541static char channel_map_H9632_ss[HDSP_MAX_CHANNELS] = {
542 /* ADAT channels */
543 0, 1, 2, 3, 4, 5, 6, 7,
544 /* SPDIF */
545 8, 9,
546 /* Analog */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100547 10, 11,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 /* AO4S-192 and AI4S-192 extension boards */
549 12, 13, 14, 15,
550 /* others don't exist */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100551 -1, -1, -1, -1, -1, -1, -1, -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 -1, -1
553};
554
555static char channel_map_H9632_ds[HDSP_MAX_CHANNELS] = {
556 /* ADAT */
557 1, 3, 5, 7,
558 /* SPDIF */
559 8, 9,
560 /* Analog */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100561 10, 11,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 /* AO4S-192 and AI4S-192 extension boards */
563 12, 13, 14, 15,
564 /* others don't exist */
565 -1, -1, -1, -1, -1, -1, -1, -1,
566 -1, -1, -1, -1, -1, -1
567};
568
569static char channel_map_H9632_qs[HDSP_MAX_CHANNELS] = {
570 /* ADAT is disabled in this mode */
571 /* SPDIF */
572 8, 9,
573 /* Analog */
574 10, 11,
575 /* AO4S-192 and AI4S-192 extension boards */
576 12, 13, 14, 15,
577 /* others don't exist */
578 -1, -1, -1, -1, -1, -1, -1, -1,
579 -1, -1, -1, -1, -1, -1, -1, -1,
580 -1, -1
581};
582
583static int snd_hammerfall_get_buffer(struct pci_dev *pci, struct snd_dma_buffer *dmab, size_t size)
584{
585 dmab->dev.type = SNDRV_DMA_TYPE_DEV;
586 dmab->dev.dev = snd_dma_pci_data(pci);
Takashi Iwaib6a96912005-05-30 18:27:03 +0200587 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
588 size, dmab) < 0)
589 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 return 0;
591}
592
593static void snd_hammerfall_free_buffer(struct snd_dma_buffer *dmab, struct pci_dev *pci)
594{
Takashi Iwai47d98c02014-01-08 16:12:25 +0100595 if (dmab->area)
596 snd_dma_free_pages(dmab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597}
598
599
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
1431 if (hmidi->istimer) {
1432 hmidi->timer.expires = 1 + jiffies;
1433 add_timer(&hmidi->timer);
1434 }
1435
1436 spin_unlock_irqrestore (&hmidi->lock, flags);
1437}
1438
Takashi Iwai55e957d2005-11-17 14:52:13 +01001439static void snd_hdsp_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001441 struct hdsp_midi *hmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 unsigned long flags;
1443
Takashi Iwai55e957d2005-11-17 14:52:13 +01001444 hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 spin_lock_irqsave (&hmidi->lock, flags);
1446 if (up) {
1447 if (!hmidi->istimer) {
1448 init_timer(&hmidi->timer);
1449 hmidi->timer.function = snd_hdsp_midi_output_timer;
1450 hmidi->timer.data = (unsigned long) hmidi;
1451 hmidi->timer.expires = 1 + jiffies;
1452 add_timer(&hmidi->timer);
1453 hmidi->istimer++;
1454 }
1455 } else {
Takashi Iwaib0b98112005-10-20 18:29:58 +02001456 if (hmidi->istimer && --hmidi->istimer <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 del_timer (&hmidi->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 }
1459 spin_unlock_irqrestore (&hmidi->lock, flags);
1460 if (up)
1461 snd_hdsp_midi_output_write(hmidi);
1462}
1463
Takashi Iwai55e957d2005-11-17 14:52:13 +01001464static int snd_hdsp_midi_input_open(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001466 struct hdsp_midi *hmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
Takashi Iwai55e957d2005-11-17 14:52:13 +01001468 hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 spin_lock_irq (&hmidi->lock);
1470 snd_hdsp_flush_midi_input (hmidi->hdsp, hmidi->id);
1471 hmidi->input = substream;
1472 spin_unlock_irq (&hmidi->lock);
1473
1474 return 0;
1475}
1476
Takashi Iwai55e957d2005-11-17 14:52:13 +01001477static int snd_hdsp_midi_output_open(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001479 struct hdsp_midi *hmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Takashi Iwai55e957d2005-11-17 14:52:13 +01001481 hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 spin_lock_irq (&hmidi->lock);
1483 hmidi->output = substream;
1484 spin_unlock_irq (&hmidi->lock);
1485
1486 return 0;
1487}
1488
Takashi Iwai55e957d2005-11-17 14:52:13 +01001489static int snd_hdsp_midi_input_close(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001491 struct hdsp_midi *hmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
1493 snd_hdsp_midi_input_trigger (substream, 0);
1494
Takashi Iwai55e957d2005-11-17 14:52:13 +01001495 hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 spin_lock_irq (&hmidi->lock);
1497 hmidi->input = NULL;
1498 spin_unlock_irq (&hmidi->lock);
1499
1500 return 0;
1501}
1502
Takashi Iwai55e957d2005-11-17 14:52:13 +01001503static int snd_hdsp_midi_output_close(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001505 struct hdsp_midi *hmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
1507 snd_hdsp_midi_output_trigger (substream, 0);
1508
Takashi Iwai55e957d2005-11-17 14:52:13 +01001509 hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 spin_lock_irq (&hmidi->lock);
1511 hmidi->output = NULL;
1512 spin_unlock_irq (&hmidi->lock);
1513
1514 return 0;
1515}
1516
Takashi Iwai55e957d2005-11-17 14:52:13 +01001517static struct snd_rawmidi_ops snd_hdsp_midi_output =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518{
1519 .open = snd_hdsp_midi_output_open,
1520 .close = snd_hdsp_midi_output_close,
1521 .trigger = snd_hdsp_midi_output_trigger,
1522};
1523
Takashi Iwai55e957d2005-11-17 14:52:13 +01001524static struct snd_rawmidi_ops snd_hdsp_midi_input =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525{
1526 .open = snd_hdsp_midi_input_open,
1527 .close = snd_hdsp_midi_input_close,
1528 .trigger = snd_hdsp_midi_input_trigger,
1529};
1530
Takashi Iwaif40b6892006-07-05 16:51:05 +02001531static int snd_hdsp_create_midi (struct snd_card *card, struct hdsp *hdsp, int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532{
1533 char buf[32];
1534
1535 hdsp->midi[id].id = id;
1536 hdsp->midi[id].rmidi = NULL;
1537 hdsp->midi[id].input = NULL;
1538 hdsp->midi[id].output = NULL;
1539 hdsp->midi[id].hdsp = hdsp;
1540 hdsp->midi[id].istimer = 0;
1541 hdsp->midi[id].pending = 0;
1542 spin_lock_init (&hdsp->midi[id].lock);
1543
1544 sprintf (buf, "%s MIDI %d", card->shortname, id+1);
Takashi Iwaib0b98112005-10-20 18:29:58 +02001545 if (snd_rawmidi_new (card, buf, id, 1, 1, &hdsp->midi[id].rmidi) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
Jaroslav Kysela972d4c52008-11-12 16:37:48 +01001548 sprintf(hdsp->midi[id].rmidi->name, "HDSP MIDI %d", id+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 hdsp->midi[id].rmidi->private_data = &hdsp->midi[id];
1550
1551 snd_rawmidi_set_ops (hdsp->midi[id].rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_hdsp_midi_output);
1552 snd_rawmidi_set_ops (hdsp->midi[id].rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_hdsp_midi_input);
1553
1554 hdsp->midi[id].rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT |
1555 SNDRV_RAWMIDI_INFO_INPUT |
1556 SNDRV_RAWMIDI_INFO_DUPLEX;
1557
1558 return 0;
1559}
1560
1561/*-----------------------------------------------------------------------------
1562 Control Interface
1563 ----------------------------------------------------------------------------*/
1564
Takashi Iwai55e957d2005-11-17 14:52:13 +01001565static u32 snd_hdsp_convert_from_aes(struct snd_aes_iec958 *aes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566{
1567 u32 val = 0;
1568 val |= (aes->status[0] & IEC958_AES0_PROFESSIONAL) ? HDSP_SPDIFProfessional : 0;
1569 val |= (aes->status[0] & IEC958_AES0_NONAUDIO) ? HDSP_SPDIFNonAudio : 0;
1570 if (val & HDSP_SPDIFProfessional)
1571 val |= (aes->status[0] & IEC958_AES0_PRO_EMPHASIS_5015) ? HDSP_SPDIFEmphasis : 0;
1572 else
1573 val |= (aes->status[0] & IEC958_AES0_CON_EMPHASIS_5015) ? HDSP_SPDIFEmphasis : 0;
1574 return val;
1575}
1576
Takashi Iwai55e957d2005-11-17 14:52:13 +01001577static void snd_hdsp_convert_to_aes(struct snd_aes_iec958 *aes, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578{
1579 aes->status[0] = ((val & HDSP_SPDIFProfessional) ? IEC958_AES0_PROFESSIONAL : 0) |
1580 ((val & HDSP_SPDIFNonAudio) ? IEC958_AES0_NONAUDIO : 0);
1581 if (val & HDSP_SPDIFProfessional)
1582 aes->status[0] |= (val & HDSP_SPDIFEmphasis) ? IEC958_AES0_PRO_EMPHASIS_5015 : 0;
1583 else
1584 aes->status[0] |= (val & HDSP_SPDIFEmphasis) ? IEC958_AES0_CON_EMPHASIS_5015 : 0;
1585}
1586
Takashi Iwai55e957d2005-11-17 14:52:13 +01001587static int snd_hdsp_control_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588{
1589 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1590 uinfo->count = 1;
1591 return 0;
1592}
1593
Takashi Iwai55e957d2005-11-17 14:52:13 +01001594static int snd_hdsp_control_spdif_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001596 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001597
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 snd_hdsp_convert_to_aes(&ucontrol->value.iec958, hdsp->creg_spdif);
1599 return 0;
1600}
1601
Takashi Iwai55e957d2005-11-17 14:52:13 +01001602static int snd_hdsp_control_spdif_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001604 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 int change;
1606 u32 val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001607
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 val = snd_hdsp_convert_from_aes(&ucontrol->value.iec958);
1609 spin_lock_irq(&hdsp->lock);
1610 change = val != hdsp->creg_spdif;
1611 hdsp->creg_spdif = val;
1612 spin_unlock_irq(&hdsp->lock);
1613 return change;
1614}
1615
Takashi Iwai55e957d2005-11-17 14:52:13 +01001616static int snd_hdsp_control_spdif_stream_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617{
1618 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1619 uinfo->count = 1;
1620 return 0;
1621}
1622
Takashi Iwai55e957d2005-11-17 14:52:13 +01001623static int snd_hdsp_control_spdif_stream_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001625 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001626
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 snd_hdsp_convert_to_aes(&ucontrol->value.iec958, hdsp->creg_spdif_stream);
1628 return 0;
1629}
1630
Takashi Iwai55e957d2005-11-17 14:52:13 +01001631static int snd_hdsp_control_spdif_stream_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001633 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 int change;
1635 u32 val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001636
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 val = snd_hdsp_convert_from_aes(&ucontrol->value.iec958);
1638 spin_lock_irq(&hdsp->lock);
1639 change = val != hdsp->creg_spdif_stream;
1640 hdsp->creg_spdif_stream = val;
1641 hdsp->control_register &= ~(HDSP_SPDIFProfessional | HDSP_SPDIFNonAudio | HDSP_SPDIFEmphasis);
1642 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register |= val);
1643 spin_unlock_irq(&hdsp->lock);
1644 return change;
1645}
1646
Takashi Iwai55e957d2005-11-17 14:52:13 +01001647static int snd_hdsp_control_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648{
1649 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1650 uinfo->count = 1;
1651 return 0;
1652}
1653
Takashi Iwai55e957d2005-11-17 14:52:13 +01001654static int snd_hdsp_control_spdif_mask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655{
1656 ucontrol->value.iec958.status[0] = kcontrol->private_value;
1657 return 0;
1658}
1659
1660#define HDSP_SPDIF_IN(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001661{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 .name = xname, \
1663 .index = xindex, \
1664 .info = snd_hdsp_info_spdif_in, \
1665 .get = snd_hdsp_get_spdif_in, \
1666 .put = snd_hdsp_put_spdif_in }
1667
Takashi Iwai55e957d2005-11-17 14:52:13 +01001668static unsigned int hdsp_spdif_in(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669{
1670 return hdsp_decode_spdif_in(hdsp->control_register & HDSP_SPDIFInputMask);
1671}
1672
Takashi Iwai55e957d2005-11-17 14:52:13 +01001673static int hdsp_set_spdif_input(struct hdsp *hdsp, int in)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674{
1675 hdsp->control_register &= ~HDSP_SPDIFInputMask;
1676 hdsp->control_register |= hdsp_encode_spdif_in(in);
1677 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1678 return 0;
1679}
1680
Takashi Iwai55e957d2005-11-17 14:52:13 +01001681static int snd_hdsp_info_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682{
Takashi Iwai8d678da2014-10-20 18:19:34 +02001683 static const char * const texts[4] = {
1684 "Optical", "Coaxial", "Internal", "AES"
1685 };
Takashi Iwai55e957d2005-11-17 14:52:13 +01001686 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
Takashi Iwai8d678da2014-10-20 18:19:34 +02001688 return snd_ctl_enum_info(uinfo, 1, (hdsp->io_type == H9632) ? 4 : 3,
1689 texts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690}
1691
Takashi Iwai55e957d2005-11-17 14:52:13 +01001692static int snd_hdsp_get_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001694 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001695
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 ucontrol->value.enumerated.item[0] = hdsp_spdif_in(hdsp);
1697 return 0;
1698}
1699
Takashi Iwai55e957d2005-11-17 14:52:13 +01001700static int snd_hdsp_put_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001702 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 int change;
1704 unsigned int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001705
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 if (!snd_hdsp_use_is_exclusive(hdsp))
1707 return -EBUSY;
1708 val = ucontrol->value.enumerated.item[0] % ((hdsp->io_type == H9632) ? 4 : 3);
1709 spin_lock_irq(&hdsp->lock);
1710 change = val != hdsp_spdif_in(hdsp);
1711 if (change)
1712 hdsp_set_spdif_input(hdsp, val);
1713 spin_unlock_irq(&hdsp->lock);
1714 return change;
1715}
1716
Adrian Knoth66d92442013-01-15 18:52:21 +01001717#define HDSP_TOGGLE_SETTING(xname, xindex) \
1718{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1719 .name = xname, \
1720 .private_value = xindex, \
1721 .info = snd_hdsp_info_toggle_setting, \
1722 .get = snd_hdsp_get_toggle_setting, \
1723 .put = snd_hdsp_put_toggle_setting \
1724}
1725
1726static int hdsp_toggle_setting(struct hdsp *hdsp, u32 regmask)
1727{
1728 return (hdsp->control_register & regmask) ? 1 : 0;
1729}
1730
1731static int hdsp_set_toggle_setting(struct hdsp *hdsp, u32 regmask, int out)
1732{
1733 if (out)
1734 hdsp->control_register |= regmask;
1735 else
1736 hdsp->control_register &= ~regmask;
1737 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1738
1739 return 0;
1740}
1741
1742#define snd_hdsp_info_toggle_setting snd_ctl_boolean_mono_info
1743
1744static int snd_hdsp_get_toggle_setting(struct snd_kcontrol *kcontrol,
1745 struct snd_ctl_elem_value *ucontrol)
1746{
1747 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
1748 u32 regmask = kcontrol->private_value;
1749
1750 spin_lock_irq(&hdsp->lock);
1751 ucontrol->value.integer.value[0] = hdsp_toggle_setting(hdsp, regmask);
1752 spin_unlock_irq(&hdsp->lock);
1753 return 0;
1754}
1755
1756static int snd_hdsp_put_toggle_setting(struct snd_kcontrol *kcontrol,
1757 struct snd_ctl_elem_value *ucontrol)
1758{
1759 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
1760 u32 regmask = kcontrol->private_value;
1761 int change;
1762 unsigned int val;
1763
1764 if (!snd_hdsp_use_is_exclusive(hdsp))
1765 return -EBUSY;
1766 val = ucontrol->value.integer.value[0] & 1;
1767 spin_lock_irq(&hdsp->lock);
1768 change = (int) val != hdsp_toggle_setting(hdsp, regmask);
1769 if (change)
1770 hdsp_set_toggle_setting(hdsp, regmask, val);
1771 spin_unlock_irq(&hdsp->lock);
1772 return change;
1773}
1774
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775#define HDSP_SPDIF_SAMPLE_RATE(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001776{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 .name = xname, \
1778 .index = xindex, \
1779 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
1780 .info = snd_hdsp_info_spdif_sample_rate, \
1781 .get = snd_hdsp_get_spdif_sample_rate \
1782}
1783
Takashi Iwai55e957d2005-11-17 14:52:13 +01001784static int snd_hdsp_info_spdif_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785{
Takashi Iwai8d678da2014-10-20 18:19:34 +02001786 static const char * const texts[] = {
1787 "32000", "44100", "48000", "64000", "88200", "96000",
1788 "None", "128000", "176400", "192000"
1789 };
Takashi Iwai55e957d2005-11-17 14:52:13 +01001790 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Takashi Iwai8d678da2014-10-20 18:19:34 +02001792 return snd_ctl_enum_info(uinfo, 1, (hdsp->io_type == H9632) ? 10 : 7,
1793 texts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794}
1795
Takashi Iwai55e957d2005-11-17 14:52:13 +01001796static int snd_hdsp_get_spdif_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001798 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001799
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 switch (hdsp_spdif_sample_rate(hdsp)) {
1801 case 32000:
1802 ucontrol->value.enumerated.item[0] = 0;
1803 break;
1804 case 44100:
1805 ucontrol->value.enumerated.item[0] = 1;
1806 break;
1807 case 48000:
1808 ucontrol->value.enumerated.item[0] = 2;
1809 break;
1810 case 64000:
1811 ucontrol->value.enumerated.item[0] = 3;
1812 break;
1813 case 88200:
1814 ucontrol->value.enumerated.item[0] = 4;
1815 break;
1816 case 96000:
1817 ucontrol->value.enumerated.item[0] = 5;
1818 break;
1819 case 128000:
1820 ucontrol->value.enumerated.item[0] = 7;
1821 break;
1822 case 176400:
1823 ucontrol->value.enumerated.item[0] = 8;
1824 break;
1825 case 192000:
1826 ucontrol->value.enumerated.item[0] = 9;
1827 break;
1828 default:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001829 ucontrol->value.enumerated.item[0] = 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 }
1831 return 0;
1832}
1833
1834#define HDSP_SYSTEM_SAMPLE_RATE(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001835{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 .name = xname, \
1837 .index = xindex, \
1838 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
1839 .info = snd_hdsp_info_system_sample_rate, \
1840 .get = snd_hdsp_get_system_sample_rate \
1841}
1842
Takashi Iwai55e957d2005-11-17 14:52:13 +01001843static int snd_hdsp_info_system_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844{
1845 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1846 uinfo->count = 1;
1847 return 0;
1848}
1849
Takashi Iwai55e957d2005-11-17 14:52:13 +01001850static int snd_hdsp_get_system_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001852 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001853
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 ucontrol->value.enumerated.item[0] = hdsp->system_sample_rate;
1855 return 0;
1856}
1857
1858#define HDSP_AUTOSYNC_SAMPLE_RATE(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001859{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 .name = xname, \
1861 .index = xindex, \
1862 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
1863 .info = snd_hdsp_info_autosync_sample_rate, \
1864 .get = snd_hdsp_get_autosync_sample_rate \
1865}
1866
Takashi Iwai55e957d2005-11-17 14:52:13 +01001867static int snd_hdsp_info_autosync_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001869 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Takashi Iwai8d678da2014-10-20 18:19:34 +02001870 static const char * const texts[] = {
1871 "32000", "44100", "48000", "64000", "88200", "96000",
1872 "None", "128000", "176400", "192000"
1873 };
1874
1875 return snd_ctl_enum_info(uinfo, 1, (hdsp->io_type == H9632) ? 10 : 7,
1876 texts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877}
1878
Takashi Iwai55e957d2005-11-17 14:52:13 +01001879static int snd_hdsp_get_autosync_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001881 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001882
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 switch (hdsp_external_sample_rate(hdsp)) {
1884 case 32000:
1885 ucontrol->value.enumerated.item[0] = 0;
1886 break;
1887 case 44100:
1888 ucontrol->value.enumerated.item[0] = 1;
1889 break;
1890 case 48000:
1891 ucontrol->value.enumerated.item[0] = 2;
1892 break;
1893 case 64000:
1894 ucontrol->value.enumerated.item[0] = 3;
1895 break;
1896 case 88200:
1897 ucontrol->value.enumerated.item[0] = 4;
1898 break;
1899 case 96000:
1900 ucontrol->value.enumerated.item[0] = 5;
1901 break;
1902 case 128000:
1903 ucontrol->value.enumerated.item[0] = 7;
1904 break;
1905 case 176400:
1906 ucontrol->value.enumerated.item[0] = 8;
1907 break;
1908 case 192000:
1909 ucontrol->value.enumerated.item[0] = 9;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001910 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 default:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001912 ucontrol->value.enumerated.item[0] = 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 }
1914 return 0;
1915}
1916
1917#define HDSP_SYSTEM_CLOCK_MODE(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001918{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 .name = xname, \
1920 .index = xindex, \
1921 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
1922 .info = snd_hdsp_info_system_clock_mode, \
1923 .get = snd_hdsp_get_system_clock_mode \
1924}
1925
Takashi Iwai55e957d2005-11-17 14:52:13 +01001926static int hdsp_system_clock_mode(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927{
Takashi Iwaib0b98112005-10-20 18:29:58 +02001928 if (hdsp->control_register & HDSP_ClockModeMaster)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 return 0;
Takashi Iwaib0b98112005-10-20 18:29:58 +02001930 else if (hdsp_external_sample_rate(hdsp) != hdsp->system_sample_rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 return 1;
1933}
1934
Takashi Iwai55e957d2005-11-17 14:52:13 +01001935static int snd_hdsp_info_system_clock_mode(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936{
Takashi Iwai8d678da2014-10-20 18:19:34 +02001937 static const char * const texts[] = {"Master", "Slave" };
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001938
Takashi Iwai8d678da2014-10-20 18:19:34 +02001939 return snd_ctl_enum_info(uinfo, 1, 2, texts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940}
1941
Takashi Iwai55e957d2005-11-17 14:52:13 +01001942static int snd_hdsp_get_system_clock_mode(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001944 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001945
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 ucontrol->value.enumerated.item[0] = hdsp_system_clock_mode(hdsp);
1947 return 0;
1948}
1949
1950#define HDSP_CLOCK_SOURCE(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001951{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 .name = xname, \
1953 .index = xindex, \
1954 .info = snd_hdsp_info_clock_source, \
1955 .get = snd_hdsp_get_clock_source, \
1956 .put = snd_hdsp_put_clock_source \
1957}
1958
Takashi Iwai55e957d2005-11-17 14:52:13 +01001959static int hdsp_clock_source(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960{
1961 if (hdsp->control_register & HDSP_ClockModeMaster) {
1962 switch (hdsp->system_sample_rate) {
1963 case 32000:
1964 return 1;
1965 case 44100:
1966 return 2;
1967 case 48000:
1968 return 3;
1969 case 64000:
1970 return 4;
1971 case 88200:
1972 return 5;
1973 case 96000:
1974 return 6;
1975 case 128000:
1976 return 7;
1977 case 176400:
1978 return 8;
1979 case 192000:
1980 return 9;
1981 default:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001982 return 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 }
1984 } else {
1985 return 0;
1986 }
1987}
1988
Takashi Iwai55e957d2005-11-17 14:52:13 +01001989static int hdsp_set_clock_source(struct hdsp *hdsp, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990{
1991 int rate;
1992 switch (mode) {
1993 case HDSP_CLOCK_SOURCE_AUTOSYNC:
1994 if (hdsp_external_sample_rate(hdsp) != 0) {
1995 if (!hdsp_set_rate(hdsp, hdsp_external_sample_rate(hdsp), 1)) {
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001996 hdsp->control_register &= ~HDSP_ClockModeMaster;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1998 return 0;
1999 }
2000 }
2001 return -1;
2002 case HDSP_CLOCK_SOURCE_INTERNAL_32KHZ:
2003 rate = 32000;
2004 break;
2005 case HDSP_CLOCK_SOURCE_INTERNAL_44_1KHZ:
2006 rate = 44100;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002007 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 case HDSP_CLOCK_SOURCE_INTERNAL_48KHZ:
2009 rate = 48000;
2010 break;
2011 case HDSP_CLOCK_SOURCE_INTERNAL_64KHZ:
2012 rate = 64000;
2013 break;
2014 case HDSP_CLOCK_SOURCE_INTERNAL_88_2KHZ:
2015 rate = 88200;
2016 break;
2017 case HDSP_CLOCK_SOURCE_INTERNAL_96KHZ:
2018 rate = 96000;
2019 break;
2020 case HDSP_CLOCK_SOURCE_INTERNAL_128KHZ:
2021 rate = 128000;
2022 break;
2023 case HDSP_CLOCK_SOURCE_INTERNAL_176_4KHZ:
2024 rate = 176400;
2025 break;
2026 case HDSP_CLOCK_SOURCE_INTERNAL_192KHZ:
2027 rate = 192000;
2028 break;
2029 default:
2030 rate = 48000;
2031 }
2032 hdsp->control_register |= HDSP_ClockModeMaster;
2033 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2034 hdsp_set_rate(hdsp, rate, 1);
2035 return 0;
2036}
2037
Takashi Iwai55e957d2005-11-17 14:52:13 +01002038static int snd_hdsp_info_clock_source(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039{
Takashi Iwai8d678da2014-10-20 18:19:34 +02002040 static const char * const texts[] = {
2041 "AutoSync", "Internal 32.0 kHz", "Internal 44.1 kHz",
2042 "Internal 48.0 kHz", "Internal 64.0 kHz", "Internal 88.2 kHz",
2043 "Internal 96.0 kHz", "Internal 128 kHz", "Internal 176.4 kHz",
2044 "Internal 192.0 KHz"
2045 };
Takashi Iwai55e957d2005-11-17 14:52:13 +01002046 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002047
Takashi Iwai8d678da2014-10-20 18:19:34 +02002048 return snd_ctl_enum_info(uinfo, 1, (hdsp->io_type == H9632) ? 10 : 7,
2049 texts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050}
2051
Takashi Iwai55e957d2005-11-17 14:52:13 +01002052static int snd_hdsp_get_clock_source(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002054 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002055
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 ucontrol->value.enumerated.item[0] = hdsp_clock_source(hdsp);
2057 return 0;
2058}
2059
Takashi Iwai55e957d2005-11-17 14:52:13 +01002060static int snd_hdsp_put_clock_source(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002062 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 int change;
2064 int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002065
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 if (!snd_hdsp_use_is_exclusive(hdsp))
2067 return -EBUSY;
2068 val = ucontrol->value.enumerated.item[0];
2069 if (val < 0) val = 0;
2070 if (hdsp->io_type == H9632) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02002071 if (val > 9)
2072 val = 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 } else {
Takashi Iwaib0b98112005-10-20 18:29:58 +02002074 if (val > 6)
2075 val = 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 }
2077 spin_lock_irq(&hdsp->lock);
Takashi Iwaib0b98112005-10-20 18:29:58 +02002078 if (val != hdsp_clock_source(hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 change = (hdsp_set_clock_source(hdsp, val) == 0) ? 1 : 0;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002080 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 spin_unlock_irq(&hdsp->lock);
2083 return change;
2084}
2085
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002086#define snd_hdsp_info_clock_source_lock snd_ctl_boolean_mono_info
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02002087
Takashi Iwai55e957d2005-11-17 14:52:13 +01002088static int snd_hdsp_get_clock_source_lock(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02002089{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002090 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002091
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02002092 ucontrol->value.integer.value[0] = hdsp->clock_source_locked;
2093 return 0;
2094}
2095
Takashi Iwai55e957d2005-11-17 14:52:13 +01002096static int snd_hdsp_put_clock_source_lock(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02002097{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002098 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02002099 int change;
2100
2101 change = (int)ucontrol->value.integer.value[0] != hdsp->clock_source_locked;
2102 if (change)
Takashi Iwai4e98d6a2007-11-15 15:58:13 +01002103 hdsp->clock_source_locked = !!ucontrol->value.integer.value[0];
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02002104 return change;
2105}
2106
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107#define HDSP_DA_GAIN(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002108{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 .name = xname, \
2110 .index = xindex, \
2111 .info = snd_hdsp_info_da_gain, \
2112 .get = snd_hdsp_get_da_gain, \
2113 .put = snd_hdsp_put_da_gain \
2114}
2115
Takashi Iwai55e957d2005-11-17 14:52:13 +01002116static int hdsp_da_gain(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117{
2118 switch (hdsp->control_register & HDSP_DAGainMask) {
2119 case HDSP_DAGainHighGain:
2120 return 0;
2121 case HDSP_DAGainPlus4dBu:
2122 return 1;
2123 case HDSP_DAGainMinus10dBV:
2124 return 2;
2125 default:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002126 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 }
2128}
2129
Takashi Iwai55e957d2005-11-17 14:52:13 +01002130static int hdsp_set_da_gain(struct hdsp *hdsp, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131{
2132 hdsp->control_register &= ~HDSP_DAGainMask;
2133 switch (mode) {
2134 case 0:
2135 hdsp->control_register |= HDSP_DAGainHighGain;
2136 break;
2137 case 1:
2138 hdsp->control_register |= HDSP_DAGainPlus4dBu;
2139 break;
2140 case 2:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002141 hdsp->control_register |= HDSP_DAGainMinus10dBV;
2142 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 default:
2144 return -1;
2145
2146 }
2147 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2148 return 0;
2149}
2150
Takashi Iwai55e957d2005-11-17 14:52:13 +01002151static int snd_hdsp_info_da_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152{
Takashi Iwai8d678da2014-10-20 18:19:34 +02002153 static const char * const texts[] = {"Hi Gain", "+4 dBu", "-10 dbV"};
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002154
Takashi Iwai8d678da2014-10-20 18:19:34 +02002155 return snd_ctl_enum_info(uinfo, 1, 3, texts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156}
2157
Takashi Iwai55e957d2005-11-17 14:52:13 +01002158static int snd_hdsp_get_da_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002160 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002161
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 ucontrol->value.enumerated.item[0] = hdsp_da_gain(hdsp);
2163 return 0;
2164}
2165
Takashi Iwai55e957d2005-11-17 14:52:13 +01002166static int snd_hdsp_put_da_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002168 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 int change;
2170 int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002171
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 if (!snd_hdsp_use_is_exclusive(hdsp))
2173 return -EBUSY;
2174 val = ucontrol->value.enumerated.item[0];
2175 if (val < 0) val = 0;
2176 if (val > 2) val = 2;
2177 spin_lock_irq(&hdsp->lock);
Takashi Iwaib0b98112005-10-20 18:29:58 +02002178 if (val != hdsp_da_gain(hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 change = (hdsp_set_da_gain(hdsp, val) == 0) ? 1 : 0;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002180 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 spin_unlock_irq(&hdsp->lock);
2183 return change;
2184}
2185
2186#define HDSP_AD_GAIN(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002187{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 .name = xname, \
2189 .index = xindex, \
2190 .info = snd_hdsp_info_ad_gain, \
2191 .get = snd_hdsp_get_ad_gain, \
2192 .put = snd_hdsp_put_ad_gain \
2193}
2194
Takashi Iwai55e957d2005-11-17 14:52:13 +01002195static int hdsp_ad_gain(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196{
2197 switch (hdsp->control_register & HDSP_ADGainMask) {
2198 case HDSP_ADGainMinus10dBV:
2199 return 0;
2200 case HDSP_ADGainPlus4dBu:
2201 return 1;
2202 case HDSP_ADGainLowGain:
2203 return 2;
2204 default:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002205 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 }
2207}
2208
Takashi Iwai55e957d2005-11-17 14:52:13 +01002209static int hdsp_set_ad_gain(struct hdsp *hdsp, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210{
2211 hdsp->control_register &= ~HDSP_ADGainMask;
2212 switch (mode) {
2213 case 0:
2214 hdsp->control_register |= HDSP_ADGainMinus10dBV;
2215 break;
2216 case 1:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002217 hdsp->control_register |= HDSP_ADGainPlus4dBu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 break;
2219 case 2:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002220 hdsp->control_register |= HDSP_ADGainLowGain;
2221 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 default:
2223 return -1;
2224
2225 }
2226 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2227 return 0;
2228}
2229
Takashi Iwai55e957d2005-11-17 14:52:13 +01002230static int snd_hdsp_info_ad_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231{
Takashi Iwai8d678da2014-10-20 18:19:34 +02002232 static const char * const texts[] = {"-10 dBV", "+4 dBu", "Lo Gain"};
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002233
Takashi Iwai8d678da2014-10-20 18:19:34 +02002234 return snd_ctl_enum_info(uinfo, 1, 3, texts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235}
2236
Takashi Iwai55e957d2005-11-17 14:52:13 +01002237static int snd_hdsp_get_ad_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002239 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002240
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 ucontrol->value.enumerated.item[0] = hdsp_ad_gain(hdsp);
2242 return 0;
2243}
2244
Takashi Iwai55e957d2005-11-17 14:52:13 +01002245static int snd_hdsp_put_ad_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002247 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 int change;
2249 int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002250
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 if (!snd_hdsp_use_is_exclusive(hdsp))
2252 return -EBUSY;
2253 val = ucontrol->value.enumerated.item[0];
2254 if (val < 0) val = 0;
2255 if (val > 2) val = 2;
2256 spin_lock_irq(&hdsp->lock);
Takashi Iwaib0b98112005-10-20 18:29:58 +02002257 if (val != hdsp_ad_gain(hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 change = (hdsp_set_ad_gain(hdsp, val) == 0) ? 1 : 0;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002259 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 spin_unlock_irq(&hdsp->lock);
2262 return change;
2263}
2264
2265#define HDSP_PHONE_GAIN(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002266{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 .name = xname, \
2268 .index = xindex, \
2269 .info = snd_hdsp_info_phone_gain, \
2270 .get = snd_hdsp_get_phone_gain, \
2271 .put = snd_hdsp_put_phone_gain \
2272}
2273
Takashi Iwai55e957d2005-11-17 14:52:13 +01002274static int hdsp_phone_gain(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275{
2276 switch (hdsp->control_register & HDSP_PhoneGainMask) {
2277 case HDSP_PhoneGain0dB:
2278 return 0;
2279 case HDSP_PhoneGainMinus6dB:
2280 return 1;
2281 case HDSP_PhoneGainMinus12dB:
2282 return 2;
2283 default:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002284 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 }
2286}
2287
Takashi Iwai55e957d2005-11-17 14:52:13 +01002288static int hdsp_set_phone_gain(struct hdsp *hdsp, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289{
2290 hdsp->control_register &= ~HDSP_PhoneGainMask;
2291 switch (mode) {
2292 case 0:
2293 hdsp->control_register |= HDSP_PhoneGain0dB;
2294 break;
2295 case 1:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002296 hdsp->control_register |= HDSP_PhoneGainMinus6dB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 break;
2298 case 2:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002299 hdsp->control_register |= HDSP_PhoneGainMinus12dB;
2300 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 default:
2302 return -1;
2303
2304 }
2305 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2306 return 0;
2307}
2308
Takashi Iwai55e957d2005-11-17 14:52:13 +01002309static int snd_hdsp_info_phone_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310{
Takashi Iwai8d678da2014-10-20 18:19:34 +02002311 static const char * const texts[] = {"0 dB", "-6 dB", "-12 dB"};
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002312
Takashi Iwai8d678da2014-10-20 18:19:34 +02002313 return snd_ctl_enum_info(uinfo, 1, 3, texts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314}
2315
Takashi Iwai55e957d2005-11-17 14:52:13 +01002316static int snd_hdsp_get_phone_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002318 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002319
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 ucontrol->value.enumerated.item[0] = hdsp_phone_gain(hdsp);
2321 return 0;
2322}
2323
Takashi Iwai55e957d2005-11-17 14:52:13 +01002324static int snd_hdsp_put_phone_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002326 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 int change;
2328 int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002329
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 if (!snd_hdsp_use_is_exclusive(hdsp))
2331 return -EBUSY;
2332 val = ucontrol->value.enumerated.item[0];
2333 if (val < 0) val = 0;
2334 if (val > 2) val = 2;
2335 spin_lock_irq(&hdsp->lock);
Takashi Iwaib0b98112005-10-20 18:29:58 +02002336 if (val != hdsp_phone_gain(hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 change = (hdsp_set_phone_gain(hdsp, val) == 0) ? 1 : 0;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002338 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 spin_unlock_irq(&hdsp->lock);
2341 return change;
2342}
2343
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344#define HDSP_PREF_SYNC_REF(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002345{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 .name = xname, \
2347 .index = xindex, \
2348 .info = snd_hdsp_info_pref_sync_ref, \
2349 .get = snd_hdsp_get_pref_sync_ref, \
2350 .put = snd_hdsp_put_pref_sync_ref \
2351}
2352
Takashi Iwai55e957d2005-11-17 14:52:13 +01002353static int hdsp_pref_sync_ref(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354{
2355 /* Notice that this looks at the requested sync source,
2356 not the one actually in use.
2357 */
2358
2359 switch (hdsp->control_register & HDSP_SyncRefMask) {
2360 case HDSP_SyncRef_ADAT1:
2361 return HDSP_SYNC_FROM_ADAT1;
2362 case HDSP_SyncRef_ADAT2:
2363 return HDSP_SYNC_FROM_ADAT2;
2364 case HDSP_SyncRef_ADAT3:
2365 return HDSP_SYNC_FROM_ADAT3;
2366 case HDSP_SyncRef_SPDIF:
2367 return HDSP_SYNC_FROM_SPDIF;
2368 case HDSP_SyncRef_WORD:
2369 return HDSP_SYNC_FROM_WORD;
2370 case HDSP_SyncRef_ADAT_SYNC:
2371 return HDSP_SYNC_FROM_ADAT_SYNC;
2372 default:
2373 return HDSP_SYNC_FROM_WORD;
2374 }
2375 return 0;
2376}
2377
Takashi Iwai55e957d2005-11-17 14:52:13 +01002378static int hdsp_set_pref_sync_ref(struct hdsp *hdsp, int pref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379{
2380 hdsp->control_register &= ~HDSP_SyncRefMask;
2381 switch (pref) {
2382 case HDSP_SYNC_FROM_ADAT1:
2383 hdsp->control_register &= ~HDSP_SyncRefMask; /* clear SyncRef bits */
2384 break;
2385 case HDSP_SYNC_FROM_ADAT2:
2386 hdsp->control_register |= HDSP_SyncRef_ADAT2;
2387 break;
2388 case HDSP_SYNC_FROM_ADAT3:
2389 hdsp->control_register |= HDSP_SyncRef_ADAT3;
2390 break;
2391 case HDSP_SYNC_FROM_SPDIF:
2392 hdsp->control_register |= HDSP_SyncRef_SPDIF;
2393 break;
2394 case HDSP_SYNC_FROM_WORD:
2395 hdsp->control_register |= HDSP_SyncRef_WORD;
2396 break;
2397 case HDSP_SYNC_FROM_ADAT_SYNC:
2398 hdsp->control_register |= HDSP_SyncRef_ADAT_SYNC;
2399 break;
2400 default:
2401 return -1;
2402 }
2403 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2404 return 0;
2405}
2406
Takashi Iwai55e957d2005-11-17 14:52:13 +01002407static int snd_hdsp_info_pref_sync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408{
Takashi Iwai8d678da2014-10-20 18:19:34 +02002409 static const char * const texts[] = {
2410 "Word", "IEC958", "ADAT1", "ADAT Sync", "ADAT2", "ADAT3"
2411 };
Takashi Iwai55e957d2005-11-17 14:52:13 +01002412 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Takashi Iwai8d678da2014-10-20 18:19:34 +02002413 int num_items;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
2415 switch (hdsp->io_type) {
2416 case Digiface:
2417 case H9652:
Takashi Iwai8d678da2014-10-20 18:19:34 +02002418 num_items = 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 break;
2420 case Multiface:
Takashi Iwai8d678da2014-10-20 18:19:34 +02002421 num_items = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 break;
2423 case H9632:
Takashi Iwai8d678da2014-10-20 18:19:34 +02002424 num_items = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 break;
2426 default:
Takashi Iwai9badda02012-01-09 18:22:35 +01002427 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002429
Takashi Iwai8d678da2014-10-20 18:19:34 +02002430 return snd_ctl_enum_info(uinfo, 1, num_items, texts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431}
2432
Takashi Iwai55e957d2005-11-17 14:52:13 +01002433static int snd_hdsp_get_pref_sync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002435 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002436
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 ucontrol->value.enumerated.item[0] = hdsp_pref_sync_ref(hdsp);
2438 return 0;
2439}
2440
Takashi Iwai55e957d2005-11-17 14:52:13 +01002441static int snd_hdsp_put_pref_sync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002443 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 int change, max;
2445 unsigned int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002446
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 if (!snd_hdsp_use_is_exclusive(hdsp))
2448 return -EBUSY;
2449
2450 switch (hdsp->io_type) {
2451 case Digiface:
2452 case H9652:
2453 max = 6;
2454 break;
2455 case Multiface:
2456 max = 4;
2457 break;
2458 case H9632:
2459 max = 3;
2460 break;
2461 default:
2462 return -EIO;
2463 }
2464
2465 val = ucontrol->value.enumerated.item[0] % max;
2466 spin_lock_irq(&hdsp->lock);
2467 change = (int)val != hdsp_pref_sync_ref(hdsp);
2468 hdsp_set_pref_sync_ref(hdsp, val);
2469 spin_unlock_irq(&hdsp->lock);
2470 return change;
2471}
2472
2473#define HDSP_AUTOSYNC_REF(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002474{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 .name = xname, \
2476 .index = xindex, \
2477 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
2478 .info = snd_hdsp_info_autosync_ref, \
2479 .get = snd_hdsp_get_autosync_ref, \
2480}
2481
Takashi Iwai55e957d2005-11-17 14:52:13 +01002482static int hdsp_autosync_ref(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483{
2484 /* This looks at the autosync selected sync reference */
2485 unsigned int status2 = hdsp_read(hdsp, HDSP_status2Register);
2486
2487 switch (status2 & HDSP_SelSyncRefMask) {
2488 case HDSP_SelSyncRef_WORD:
2489 return HDSP_AUTOSYNC_FROM_WORD;
2490 case HDSP_SelSyncRef_ADAT_SYNC:
2491 return HDSP_AUTOSYNC_FROM_ADAT_SYNC;
2492 case HDSP_SelSyncRef_SPDIF:
2493 return HDSP_AUTOSYNC_FROM_SPDIF;
2494 case HDSP_SelSyncRefMask:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002495 return HDSP_AUTOSYNC_FROM_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 case HDSP_SelSyncRef_ADAT1:
2497 return HDSP_AUTOSYNC_FROM_ADAT1;
2498 case HDSP_SelSyncRef_ADAT2:
2499 return HDSP_AUTOSYNC_FROM_ADAT2;
2500 case HDSP_SelSyncRef_ADAT3:
2501 return HDSP_AUTOSYNC_FROM_ADAT3;
2502 default:
2503 return HDSP_AUTOSYNC_FROM_WORD;
2504 }
2505 return 0;
2506}
2507
Takashi Iwai55e957d2005-11-17 14:52:13 +01002508static int snd_hdsp_info_autosync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509{
Takashi Iwai8d678da2014-10-20 18:19:34 +02002510 static const char * const texts[] = {
2511 "Word", "ADAT Sync", "IEC958", "None", "ADAT1", "ADAT2", "ADAT3"
2512 };
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002513
Takashi Iwai8d678da2014-10-20 18:19:34 +02002514 return snd_ctl_enum_info(uinfo, 1, 7, texts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515}
2516
Takashi Iwai55e957d2005-11-17 14:52:13 +01002517static int snd_hdsp_get_autosync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002519 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002520
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 ucontrol->value.enumerated.item[0] = hdsp_autosync_ref(hdsp);
2522 return 0;
2523}
2524
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525#define HDSP_PRECISE_POINTER(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002526{ .iface = SNDRV_CTL_ELEM_IFACE_CARD, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 .name = xname, \
2528 .index = xindex, \
2529 .info = snd_hdsp_info_precise_pointer, \
2530 .get = snd_hdsp_get_precise_pointer, \
2531 .put = snd_hdsp_put_precise_pointer \
2532}
2533
Takashi Iwai55e957d2005-11-17 14:52:13 +01002534static int hdsp_set_precise_pointer(struct hdsp *hdsp, int precise)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535{
Takashi Iwaib0b98112005-10-20 18:29:58 +02002536 if (precise)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 hdsp->precise_ptr = 1;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002538 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 hdsp->precise_ptr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 return 0;
2541}
2542
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002543#define snd_hdsp_info_precise_pointer snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544
Takashi Iwai55e957d2005-11-17 14:52:13 +01002545static int snd_hdsp_get_precise_pointer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002547 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002548
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 spin_lock_irq(&hdsp->lock);
2550 ucontrol->value.integer.value[0] = hdsp->precise_ptr;
2551 spin_unlock_irq(&hdsp->lock);
2552 return 0;
2553}
2554
Takashi Iwai55e957d2005-11-17 14:52:13 +01002555static int snd_hdsp_put_precise_pointer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002557 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 int change;
2559 unsigned int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002560
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 if (!snd_hdsp_use_is_exclusive(hdsp))
2562 return -EBUSY;
2563 val = ucontrol->value.integer.value[0] & 1;
2564 spin_lock_irq(&hdsp->lock);
2565 change = (int)val != hdsp->precise_ptr;
2566 hdsp_set_precise_pointer(hdsp, val);
2567 spin_unlock_irq(&hdsp->lock);
2568 return change;
2569}
2570
2571#define HDSP_USE_MIDI_TASKLET(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002572{ .iface = SNDRV_CTL_ELEM_IFACE_CARD, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 .name = xname, \
2574 .index = xindex, \
2575 .info = snd_hdsp_info_use_midi_tasklet, \
2576 .get = snd_hdsp_get_use_midi_tasklet, \
2577 .put = snd_hdsp_put_use_midi_tasklet \
2578}
2579
Takashi Iwai55e957d2005-11-17 14:52:13 +01002580static int hdsp_set_use_midi_tasklet(struct hdsp *hdsp, int use_tasklet)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581{
Takashi Iwaib0b98112005-10-20 18:29:58 +02002582 if (use_tasklet)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 hdsp->use_midi_tasklet = 1;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002584 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 hdsp->use_midi_tasklet = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 return 0;
2587}
2588
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002589#define snd_hdsp_info_use_midi_tasklet snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590
Takashi Iwai55e957d2005-11-17 14:52:13 +01002591static int snd_hdsp_get_use_midi_tasklet(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002593 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002594
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 spin_lock_irq(&hdsp->lock);
2596 ucontrol->value.integer.value[0] = hdsp->use_midi_tasklet;
2597 spin_unlock_irq(&hdsp->lock);
2598 return 0;
2599}
2600
Takashi Iwai55e957d2005-11-17 14:52:13 +01002601static int snd_hdsp_put_use_midi_tasklet(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002603 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 int change;
2605 unsigned int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002606
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 if (!snd_hdsp_use_is_exclusive(hdsp))
2608 return -EBUSY;
2609 val = ucontrol->value.integer.value[0] & 1;
2610 spin_lock_irq(&hdsp->lock);
2611 change = (int)val != hdsp->use_midi_tasklet;
2612 hdsp_set_use_midi_tasklet(hdsp, val);
2613 spin_unlock_irq(&hdsp->lock);
2614 return change;
2615}
2616
2617#define HDSP_MIXER(xname, xindex) \
2618{ .iface = SNDRV_CTL_ELEM_IFACE_HWDEP, \
2619 .name = xname, \
2620 .index = xindex, \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002621 .device = 0, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
2623 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
2624 .info = snd_hdsp_info_mixer, \
2625 .get = snd_hdsp_get_mixer, \
2626 .put = snd_hdsp_put_mixer \
2627}
2628
Takashi Iwai55e957d2005-11-17 14:52:13 +01002629static int snd_hdsp_info_mixer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630{
2631 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2632 uinfo->count = 3;
2633 uinfo->value.integer.min = 0;
2634 uinfo->value.integer.max = 65536;
2635 uinfo->value.integer.step = 1;
2636 return 0;
2637}
2638
Takashi Iwai55e957d2005-11-17 14:52:13 +01002639static int snd_hdsp_get_mixer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002641 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 int source;
2643 int destination;
2644 int addr;
2645
2646 source = ucontrol->value.integer.value[0];
2647 destination = ucontrol->value.integer.value[1];
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002648
Takashi Iwaib0b98112005-10-20 18:29:58 +02002649 if (source >= hdsp->max_channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 addr = hdsp_playback_to_output_key(hdsp,source-hdsp->max_channels,destination);
Takashi Iwaib0b98112005-10-20 18:29:58 +02002651 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 addr = hdsp_input_to_output_key(hdsp,source, destination);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002653
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 spin_lock_irq(&hdsp->lock);
2655 ucontrol->value.integer.value[2] = hdsp_read_gain (hdsp, addr);
2656 spin_unlock_irq(&hdsp->lock);
2657 return 0;
2658}
2659
Takashi Iwai55e957d2005-11-17 14:52:13 +01002660static int snd_hdsp_put_mixer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002662 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 int change;
2664 int source;
2665 int destination;
2666 int gain;
2667 int addr;
2668
2669 if (!snd_hdsp_use_is_exclusive(hdsp))
2670 return -EBUSY;
2671
2672 source = ucontrol->value.integer.value[0];
2673 destination = ucontrol->value.integer.value[1];
2674
Takashi Iwaib0b98112005-10-20 18:29:58 +02002675 if (source >= hdsp->max_channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 addr = hdsp_playback_to_output_key(hdsp,source-hdsp->max_channels, destination);
Takashi Iwaib0b98112005-10-20 18:29:58 +02002677 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 addr = hdsp_input_to_output_key(hdsp,source, destination);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679
2680 gain = ucontrol->value.integer.value[2];
2681
2682 spin_lock_irq(&hdsp->lock);
2683 change = gain != hdsp_read_gain(hdsp, addr);
2684 if (change)
2685 hdsp_write_gain(hdsp, addr, gain);
2686 spin_unlock_irq(&hdsp->lock);
2687 return change;
2688}
2689
2690#define HDSP_WC_SYNC_CHECK(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002691{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 .name = xname, \
2693 .index = xindex, \
2694 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
2695 .info = snd_hdsp_info_sync_check, \
2696 .get = snd_hdsp_get_wc_sync_check \
2697}
2698
Takashi Iwai55e957d2005-11-17 14:52:13 +01002699static int snd_hdsp_info_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700{
Takashi Iwai8d678da2014-10-20 18:19:34 +02002701 static const char * const texts[] = {"No Lock", "Lock", "Sync" };
2702
2703 return snd_ctl_enum_info(uinfo, 1, 3, texts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704}
2705
Takashi Iwai55e957d2005-11-17 14:52:13 +01002706static int hdsp_wc_sync_check(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707{
2708 int status2 = hdsp_read(hdsp, HDSP_status2Register);
2709 if (status2 & HDSP_wc_lock) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02002710 if (status2 & HDSP_wc_sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 return 2;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002712 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 return 1;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002714 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 return 0;
2717}
2718
Takashi Iwai55e957d2005-11-17 14:52:13 +01002719static int snd_hdsp_get_wc_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002721 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722
2723 ucontrol->value.enumerated.item[0] = hdsp_wc_sync_check(hdsp);
2724 return 0;
2725}
2726
2727#define HDSP_SPDIF_SYNC_CHECK(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002728{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 .name = xname, \
2730 .index = xindex, \
2731 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
2732 .info = snd_hdsp_info_sync_check, \
2733 .get = snd_hdsp_get_spdif_sync_check \
2734}
2735
Takashi Iwai55e957d2005-11-17 14:52:13 +01002736static int hdsp_spdif_sync_check(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737{
2738 int status = hdsp_read(hdsp, HDSP_statusRegister);
Takashi Iwaib0b98112005-10-20 18:29:58 +02002739 if (status & HDSP_SPDIFErrorFlag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 return 0;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002741 else {
Takashi Iwaib0b98112005-10-20 18:29:58 +02002742 if (status & HDSP_SPDIFSync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 return 2;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002744 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 }
2747 return 0;
2748}
2749
Takashi Iwai55e957d2005-11-17 14:52:13 +01002750static int snd_hdsp_get_spdif_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002752 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753
2754 ucontrol->value.enumerated.item[0] = hdsp_spdif_sync_check(hdsp);
2755 return 0;
2756}
2757
2758#define HDSP_ADATSYNC_SYNC_CHECK(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002759{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 .name = xname, \
2761 .index = xindex, \
2762 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
2763 .info = snd_hdsp_info_sync_check, \
2764 .get = snd_hdsp_get_adatsync_sync_check \
2765}
2766
Takashi Iwai55e957d2005-11-17 14:52:13 +01002767static int hdsp_adatsync_sync_check(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768{
2769 int status = hdsp_read(hdsp, HDSP_statusRegister);
2770 if (status & HDSP_TimecodeLock) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02002771 if (status & HDSP_TimecodeSync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 return 2;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002773 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 return 1;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002775 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 return 0;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002777}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778
Takashi Iwai55e957d2005-11-17 14:52:13 +01002779static int snd_hdsp_get_adatsync_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002781 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782
2783 ucontrol->value.enumerated.item[0] = hdsp_adatsync_sync_check(hdsp);
2784 return 0;
2785}
2786
2787#define HDSP_ADAT_SYNC_CHECK \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002788{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
2790 .info = snd_hdsp_info_sync_check, \
2791 .get = snd_hdsp_get_adat_sync_check \
2792}
2793
Takashi Iwai55e957d2005-11-17 14:52:13 +01002794static int hdsp_adat_sync_check(struct hdsp *hdsp, int idx)
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002795{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 int status = hdsp_read(hdsp, HDSP_statusRegister);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002797
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 if (status & (HDSP_Lock0>>idx)) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02002799 if (status & (HDSP_Sync0>>idx))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 return 2;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002801 else
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002802 return 1;
Takashi Iwaib0b98112005-10-20 18:29:58 +02002803 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 return 0;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002805}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806
Takashi Iwai55e957d2005-11-17 14:52:13 +01002807static int snd_hdsp_get_adat_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808{
2809 int offset;
Takashi Iwai55e957d2005-11-17 14:52:13 +01002810 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811
2812 offset = ucontrol->id.index - 1;
Takashi Iwaida3cec32008-08-08 17:12:14 +02002813 snd_BUG_ON(offset < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814
2815 switch (hdsp->io_type) {
2816 case Digiface:
2817 case H9652:
2818 if (offset >= 3)
2819 return -EINVAL;
2820 break;
2821 case Multiface:
2822 case H9632:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002823 if (offset >= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 return -EINVAL;
2825 break;
2826 default:
2827 return -EIO;
2828 }
2829
2830 ucontrol->value.enumerated.item[0] = hdsp_adat_sync_check(hdsp, offset);
2831 return 0;
2832}
2833
Julian Cablee4b60882007-03-19 11:44:40 +01002834#define HDSP_DDS_OFFSET(xname, xindex) \
2835{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2836 .name = xname, \
2837 .index = xindex, \
2838 .info = snd_hdsp_info_dds_offset, \
2839 .get = snd_hdsp_get_dds_offset, \
2840 .put = snd_hdsp_put_dds_offset \
2841}
2842
2843static int hdsp_dds_offset(struct hdsp *hdsp)
2844{
2845 u64 n;
Julian Cablee4b60882007-03-19 11:44:40 +01002846 unsigned int dds_value = hdsp->dds_value;
2847 int system_sample_rate = hdsp->system_sample_rate;
2848
Takashi Iwai2a3988f2007-10-16 14:26:32 +02002849 if (!dds_value)
2850 return 0;
2851
Julian Cablee4b60882007-03-19 11:44:40 +01002852 n = DDS_NUMERATOR;
2853 /*
2854 * dds_value = n / rate
2855 * rate = n / dds_value
2856 */
Takashi Iwai3f7440a2009-06-05 17:40:04 +02002857 n = div_u64(n, dds_value);
Julian Cablee4b60882007-03-19 11:44:40 +01002858 if (system_sample_rate >= 112000)
2859 n *= 4;
2860 else if (system_sample_rate >= 56000)
2861 n *= 2;
2862 return ((int)n) - system_sample_rate;
2863}
2864
2865static int hdsp_set_dds_offset(struct hdsp *hdsp, int offset_hz)
2866{
2867 int rate = hdsp->system_sample_rate + offset_hz;
2868 hdsp_set_dds_value(hdsp, rate);
2869 return 0;
2870}
2871
2872static int snd_hdsp_info_dds_offset(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
2873{
2874 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2875 uinfo->count = 1;
2876 uinfo->value.integer.min = -5000;
2877 uinfo->value.integer.max = 5000;
2878 return 0;
2879}
2880
2881static int snd_hdsp_get_dds_offset(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2882{
2883 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002884
Julian Cablee4b60882007-03-19 11:44:40 +01002885 ucontrol->value.enumerated.item[0] = hdsp_dds_offset(hdsp);
2886 return 0;
2887}
2888
2889static int snd_hdsp_put_dds_offset(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2890{
2891 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2892 int change;
2893 int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002894
Julian Cablee4b60882007-03-19 11:44:40 +01002895 if (!snd_hdsp_use_is_exclusive(hdsp))
2896 return -EBUSY;
2897 val = ucontrol->value.enumerated.item[0];
2898 spin_lock_irq(&hdsp->lock);
2899 if (val != hdsp_dds_offset(hdsp))
2900 change = (hdsp_set_dds_offset(hdsp, val) == 0) ? 1 : 0;
2901 else
2902 change = 0;
2903 spin_unlock_irq(&hdsp->lock);
2904 return change;
2905}
2906
Takashi Iwai55e957d2005-11-17 14:52:13 +01002907static struct snd_kcontrol_new snd_hdsp_9632_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908HDSP_DA_GAIN("DA Gain", 0),
2909HDSP_AD_GAIN("AD Gain", 0),
2910HDSP_PHONE_GAIN("Phones Gain", 0),
Adrian Knoth4833c672013-01-15 18:52:22 +01002911HDSP_TOGGLE_SETTING("XLR Breakout Cable", HDSP_XLRBreakoutCable),
Julian Cablee4b60882007-03-19 11:44:40 +01002912HDSP_DDS_OFFSET("DDS Sample Rate Offset", 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913};
2914
Takashi Iwai55e957d2005-11-17 14:52:13 +01002915static struct snd_kcontrol_new snd_hdsp_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916{
Clemens Ladisch5549d542005-08-03 13:50:30 +02002917 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
2919 .info = snd_hdsp_control_spdif_info,
2920 .get = snd_hdsp_control_spdif_get,
2921 .put = snd_hdsp_control_spdif_put,
2922},
2923{
2924 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
Clemens Ladisch5549d542005-08-03 13:50:30 +02002925 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
2927 .info = snd_hdsp_control_spdif_stream_info,
2928 .get = snd_hdsp_control_spdif_stream_get,
2929 .put = snd_hdsp_control_spdif_stream_put,
2930},
2931{
2932 .access = SNDRV_CTL_ELEM_ACCESS_READ,
Clemens Ladisch5549d542005-08-03 13:50:30 +02002933 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
2935 .info = snd_hdsp_control_spdif_mask_info,
2936 .get = snd_hdsp_control_spdif_mask_get,
2937 .private_value = IEC958_AES0_NONAUDIO |
2938 IEC958_AES0_PROFESSIONAL |
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002939 IEC958_AES0_CON_EMPHASIS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940},
2941{
2942 .access = SNDRV_CTL_ELEM_ACCESS_READ,
Clemens Ladisch5549d542005-08-03 13:50:30 +02002943 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
2945 .info = snd_hdsp_control_spdif_mask_info,
2946 .get = snd_hdsp_control_spdif_mask_get,
2947 .private_value = IEC958_AES0_NONAUDIO |
2948 IEC958_AES0_PROFESSIONAL |
2949 IEC958_AES0_PRO_EMPHASIS,
2950},
2951HDSP_MIXER("Mixer", 0),
2952HDSP_SPDIF_IN("IEC958 Input Connector", 0),
Adrian Knoth4833c672013-01-15 18:52:22 +01002953HDSP_TOGGLE_SETTING("IEC958 Output also on ADAT1", HDSP_SPDIFOpticalOut),
2954HDSP_TOGGLE_SETTING("IEC958 Professional Bit", HDSP_SPDIFProfessional),
2955HDSP_TOGGLE_SETTING("IEC958 Emphasis Bit", HDSP_SPDIFEmphasis),
2956HDSP_TOGGLE_SETTING("IEC958 Non-audio Bit", HDSP_SPDIFNonAudio),
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002957/* 'Sample Clock Source' complies with the alsa control naming scheme */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958HDSP_CLOCK_SOURCE("Sample Clock Source", 0),
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02002959{
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02002960 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2961 .name = "Sample Clock Source Locking",
2962 .info = snd_hdsp_info_clock_source_lock,
2963 .get = snd_hdsp_get_clock_source_lock,
2964 .put = snd_hdsp_put_clock_source_lock,
2965},
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966HDSP_SYSTEM_CLOCK_MODE("System Clock Mode", 0),
2967HDSP_PREF_SYNC_REF("Preferred Sync Reference", 0),
2968HDSP_AUTOSYNC_REF("AutoSync Reference", 0),
2969HDSP_SPDIF_SAMPLE_RATE("SPDIF Sample Rate", 0),
2970HDSP_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
2971/* 'External Rate' complies with the alsa control naming scheme */
2972HDSP_AUTOSYNC_SAMPLE_RATE("External Rate", 0),
2973HDSP_WC_SYNC_CHECK("Word Clock Lock Status", 0),
2974HDSP_SPDIF_SYNC_CHECK("SPDIF Lock Status", 0),
2975HDSP_ADATSYNC_SYNC_CHECK("ADAT Sync Lock Status", 0),
Adrian Knoth4833c672013-01-15 18:52:22 +01002976HDSP_TOGGLE_SETTING("Line Out", HDSP_LineOut),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977HDSP_PRECISE_POINTER("Precise Pointer", 0),
2978HDSP_USE_MIDI_TASKLET("Use Midi Tasklet", 0),
2979};
2980
Florian Faber28b26e12010-12-01 12:14:47 +01002981
2982static int hdsp_rpm_input12(struct hdsp *hdsp)
2983{
2984 switch (hdsp->control_register & HDSP_RPM_Inp12) {
2985 case HDSP_RPM_Inp12_Phon_6dB:
2986 return 0;
2987 case HDSP_RPM_Inp12_Phon_n6dB:
2988 return 2;
2989 case HDSP_RPM_Inp12_Line_0dB:
2990 return 3;
2991 case HDSP_RPM_Inp12_Line_n6dB:
2992 return 4;
2993 }
2994 return 1;
2995}
2996
2997
2998static int snd_hdsp_get_rpm_input12(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2999{
3000 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
3001
3002 ucontrol->value.enumerated.item[0] = hdsp_rpm_input12(hdsp);
3003 return 0;
3004}
3005
3006
3007static int hdsp_set_rpm_input12(struct hdsp *hdsp, int mode)
3008{
3009 hdsp->control_register &= ~HDSP_RPM_Inp12;
3010 switch (mode) {
3011 case 0:
3012 hdsp->control_register |= HDSP_RPM_Inp12_Phon_6dB;
3013 break;
3014 case 1:
3015 break;
3016 case 2:
3017 hdsp->control_register |= HDSP_RPM_Inp12_Phon_n6dB;
3018 break;
3019 case 3:
3020 hdsp->control_register |= HDSP_RPM_Inp12_Line_0dB;
3021 break;
3022 case 4:
3023 hdsp->control_register |= HDSP_RPM_Inp12_Line_n6dB;
3024 break;
3025 default:
3026 return -1;
3027 }
3028
3029 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
3030 return 0;
3031}
3032
3033
3034static int snd_hdsp_put_rpm_input12(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3035{
3036 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
3037 int change;
3038 int val;
3039
3040 if (!snd_hdsp_use_is_exclusive(hdsp))
3041 return -EBUSY;
3042 val = ucontrol->value.enumerated.item[0];
3043 if (val < 0)
3044 val = 0;
3045 if (val > 4)
3046 val = 4;
3047 spin_lock_irq(&hdsp->lock);
3048 if (val != hdsp_rpm_input12(hdsp))
3049 change = (hdsp_set_rpm_input12(hdsp, val) == 0) ? 1 : 0;
3050 else
3051 change = 0;
3052 spin_unlock_irq(&hdsp->lock);
3053 return change;
3054}
3055
3056
3057static int snd_hdsp_info_rpm_input(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
3058{
Takashi Iwai8d678da2014-10-20 18:19:34 +02003059 static const char * const texts[] = {
3060 "Phono +6dB", "Phono 0dB", "Phono -6dB", "Line 0dB", "Line -6dB"
3061 };
Florian Faber28b26e12010-12-01 12:14:47 +01003062
Takashi Iwai8d678da2014-10-20 18:19:34 +02003063 return snd_ctl_enum_info(uinfo, 1, 5, texts);
Florian Faber28b26e12010-12-01 12:14:47 +01003064}
3065
3066
3067static int hdsp_rpm_input34(struct hdsp *hdsp)
3068{
3069 switch (hdsp->control_register & HDSP_RPM_Inp34) {
3070 case HDSP_RPM_Inp34_Phon_6dB:
3071 return 0;
3072 case HDSP_RPM_Inp34_Phon_n6dB:
3073 return 2;
3074 case HDSP_RPM_Inp34_Line_0dB:
3075 return 3;
3076 case HDSP_RPM_Inp34_Line_n6dB:
3077 return 4;
3078 }
3079 return 1;
3080}
3081
3082
3083static int snd_hdsp_get_rpm_input34(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3084{
3085 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
3086
3087 ucontrol->value.enumerated.item[0] = hdsp_rpm_input34(hdsp);
3088 return 0;
3089}
3090
3091
3092static int hdsp_set_rpm_input34(struct hdsp *hdsp, int mode)
3093{
3094 hdsp->control_register &= ~HDSP_RPM_Inp34;
3095 switch (mode) {
3096 case 0:
3097 hdsp->control_register |= HDSP_RPM_Inp34_Phon_6dB;
3098 break;
3099 case 1:
3100 break;
3101 case 2:
3102 hdsp->control_register |= HDSP_RPM_Inp34_Phon_n6dB;
3103 break;
3104 case 3:
3105 hdsp->control_register |= HDSP_RPM_Inp34_Line_0dB;
3106 break;
3107 case 4:
3108 hdsp->control_register |= HDSP_RPM_Inp34_Line_n6dB;
3109 break;
3110 default:
3111 return -1;
3112 }
3113
3114 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
3115 return 0;
3116}
3117
3118
3119static int snd_hdsp_put_rpm_input34(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3120{
3121 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
3122 int change;
3123 int val;
3124
3125 if (!snd_hdsp_use_is_exclusive(hdsp))
3126 return -EBUSY;
3127 val = ucontrol->value.enumerated.item[0];
3128 if (val < 0)
3129 val = 0;
3130 if (val > 4)
3131 val = 4;
3132 spin_lock_irq(&hdsp->lock);
3133 if (val != hdsp_rpm_input34(hdsp))
3134 change = (hdsp_set_rpm_input34(hdsp, val) == 0) ? 1 : 0;
3135 else
3136 change = 0;
3137 spin_unlock_irq(&hdsp->lock);
3138 return change;
3139}
3140
3141
3142/* RPM Bypass switch */
3143static int hdsp_rpm_bypass(struct hdsp *hdsp)
3144{
3145 return (hdsp->control_register & HDSP_RPM_Bypass) ? 1 : 0;
3146}
3147
3148
3149static int snd_hdsp_get_rpm_bypass(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3150{
3151 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
3152
3153 ucontrol->value.integer.value[0] = hdsp_rpm_bypass(hdsp);
3154 return 0;
3155}
3156
3157
3158static int hdsp_set_rpm_bypass(struct hdsp *hdsp, int on)
3159{
3160 if (on)
3161 hdsp->control_register |= HDSP_RPM_Bypass;
3162 else
3163 hdsp->control_register &= ~HDSP_RPM_Bypass;
3164 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
3165 return 0;
3166}
3167
3168
3169static int snd_hdsp_put_rpm_bypass(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3170{
3171 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
3172 int change;
3173 unsigned int val;
3174
3175 if (!snd_hdsp_use_is_exclusive(hdsp))
3176 return -EBUSY;
3177 val = ucontrol->value.integer.value[0] & 1;
3178 spin_lock_irq(&hdsp->lock);
3179 change = (int)val != hdsp_rpm_bypass(hdsp);
3180 hdsp_set_rpm_bypass(hdsp, val);
3181 spin_unlock_irq(&hdsp->lock);
3182 return change;
3183}
3184
3185
3186static int snd_hdsp_info_rpm_bypass(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
3187{
Takashi Iwai8d678da2014-10-20 18:19:34 +02003188 static const char * const texts[] = {"On", "Off"};
Florian Faber28b26e12010-12-01 12:14:47 +01003189
Takashi Iwai8d678da2014-10-20 18:19:34 +02003190 return snd_ctl_enum_info(uinfo, 1, 2, texts);
Florian Faber28b26e12010-12-01 12:14:47 +01003191}
3192
3193
3194/* RPM Disconnect switch */
3195static int hdsp_rpm_disconnect(struct hdsp *hdsp)
3196{
3197 return (hdsp->control_register & HDSP_RPM_Disconnect) ? 1 : 0;
3198}
3199
3200
3201static int snd_hdsp_get_rpm_disconnect(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3202{
3203 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
3204
3205 ucontrol->value.integer.value[0] = hdsp_rpm_disconnect(hdsp);
3206 return 0;
3207}
3208
3209
3210static int hdsp_set_rpm_disconnect(struct hdsp *hdsp, int on)
3211{
3212 if (on)
3213 hdsp->control_register |= HDSP_RPM_Disconnect;
3214 else
3215 hdsp->control_register &= ~HDSP_RPM_Disconnect;
3216 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
3217 return 0;
3218}
3219
3220
3221static int snd_hdsp_put_rpm_disconnect(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3222{
3223 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
3224 int change;
3225 unsigned int val;
3226
3227 if (!snd_hdsp_use_is_exclusive(hdsp))
3228 return -EBUSY;
3229 val = ucontrol->value.integer.value[0] & 1;
3230 spin_lock_irq(&hdsp->lock);
3231 change = (int)val != hdsp_rpm_disconnect(hdsp);
3232 hdsp_set_rpm_disconnect(hdsp, val);
3233 spin_unlock_irq(&hdsp->lock);
3234 return change;
3235}
3236
3237static int snd_hdsp_info_rpm_disconnect(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
3238{
Takashi Iwai8d678da2014-10-20 18:19:34 +02003239 static const char * const texts[] = {"On", "Off"};
Florian Faber28b26e12010-12-01 12:14:47 +01003240
Takashi Iwai8d678da2014-10-20 18:19:34 +02003241 return snd_ctl_enum_info(uinfo, 1, 2, texts);
Florian Faber28b26e12010-12-01 12:14:47 +01003242}
3243
3244static struct snd_kcontrol_new snd_hdsp_rpm_controls[] = {
3245 {
3246 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3247 .name = "RPM Bypass",
3248 .get = snd_hdsp_get_rpm_bypass,
3249 .put = snd_hdsp_put_rpm_bypass,
3250 .info = snd_hdsp_info_rpm_bypass
3251 },
3252 {
3253 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3254 .name = "RPM Disconnect",
3255 .get = snd_hdsp_get_rpm_disconnect,
3256 .put = snd_hdsp_put_rpm_disconnect,
3257 .info = snd_hdsp_info_rpm_disconnect
3258 },
3259 {
3260 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3261 .name = "Input 1/2",
3262 .get = snd_hdsp_get_rpm_input12,
3263 .put = snd_hdsp_put_rpm_input12,
3264 .info = snd_hdsp_info_rpm_input
3265 },
3266 {
3267 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3268 .name = "Input 3/4",
3269 .get = snd_hdsp_get_rpm_input34,
3270 .put = snd_hdsp_put_rpm_input34,
3271 .info = snd_hdsp_info_rpm_input
3272 },
3273 HDSP_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
3274 HDSP_MIXER("Mixer", 0)
3275};
3276
Adrian Knoth4833c672013-01-15 18:52:22 +01003277static struct snd_kcontrol_new snd_hdsp_96xx_aeb =
3278 HDSP_TOGGLE_SETTING("Analog Extension Board",
3279 HDSP_AnalogExtensionBoard);
Takashi Iwai55e957d2005-11-17 14:52:13 +01003280static struct snd_kcontrol_new snd_hdsp_adat_sync_check = HDSP_ADAT_SYNC_CHECK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281
Takashi Iwai55e957d2005-11-17 14:52:13 +01003282static int snd_hdsp_create_controls(struct snd_card *card, struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283{
3284 unsigned int idx;
3285 int err;
Takashi Iwai55e957d2005-11-17 14:52:13 +01003286 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287
Florian Faber28b26e12010-12-01 12:14:47 +01003288 if (hdsp->io_type == RPM) {
3289 /* RPM Bypass, Disconnect and Input switches */
3290 for (idx = 0; idx < ARRAY_SIZE(snd_hdsp_rpm_controls); idx++) {
3291 err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_rpm_controls[idx], hdsp));
3292 if (err < 0)
3293 return err;
3294 }
3295 return 0;
3296 }
3297
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 for (idx = 0; idx < ARRAY_SIZE(snd_hdsp_controls); idx++) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02003299 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_controls[idx], hdsp))) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 if (idx == 1) /* IEC958 (S/PDIF) Stream */
3302 hdsp->spdif_ctl = kctl;
3303 }
3304
3305 /* ADAT SyncCheck status */
3306 snd_hdsp_adat_sync_check.name = "ADAT Lock Status";
3307 snd_hdsp_adat_sync_check.index = 1;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003308 if ((err = snd_ctl_add (card, kctl = snd_ctl_new1(&snd_hdsp_adat_sync_check, hdsp))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 if (hdsp->io_type == Digiface || hdsp->io_type == H9652) {
3311 for (idx = 1; idx < 3; ++idx) {
3312 snd_hdsp_adat_sync_check.index = idx+1;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003313 if ((err = snd_ctl_add (card, kctl = snd_ctl_new1(&snd_hdsp_adat_sync_check, hdsp))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 }
3316 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003317
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 /* DA, AD and Phone gain and XLR breakout cable controls for H9632 cards */
3319 if (hdsp->io_type == H9632) {
3320 for (idx = 0; idx < ARRAY_SIZE(snd_hdsp_9632_controls); idx++) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02003321 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_9632_controls[idx], hdsp))) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 }
3324 }
3325
3326 /* AEB control for H96xx card */
3327 if (hdsp->io_type == H9632 || hdsp->io_type == H9652) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02003328 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_96xx_aeb, hdsp))) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 }
3331
3332 return 0;
3333}
3334
3335/*------------------------------------------------------------
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003336 /proc interface
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 ------------------------------------------------------------*/
3338
3339static void
Takashi Iwai55e957d2005-11-17 14:52:13 +01003340snd_hdsp_proc_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341{
Joe Perches9fe856e2010-09-04 18:52:54 -07003342 struct hdsp *hdsp = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 unsigned int status;
3344 unsigned int status2;
3345 char *pref_sync_ref;
3346 char *autosync_ref;
3347 char *system_clock_mode;
3348 char *clock_source;
3349 int x;
3350
Tim Blechmannc18bc9b2009-08-12 18:21:30 +02003351 status = hdsp_read(hdsp, HDSP_statusRegister);
3352 status2 = hdsp_read(hdsp, HDSP_status2Register);
3353
3354 snd_iprintf(buffer, "%s (Card #%d)\n", hdsp->card_name,
3355 hdsp->card->number + 1);
3356 snd_iprintf(buffer, "Buffers: capture %p playback %p\n",
3357 hdsp->capture_buffer, hdsp->playback_buffer);
3358 snd_iprintf(buffer, "IRQ: %d Registers bus: 0x%lx VM: 0x%lx\n",
3359 hdsp->irq, hdsp->port, (unsigned long)hdsp->iobase);
3360 snd_iprintf(buffer, "Control register: 0x%x\n", hdsp->control_register);
3361 snd_iprintf(buffer, "Control2 register: 0x%x\n",
3362 hdsp->control2_register);
3363 snd_iprintf(buffer, "Status register: 0x%x\n", status);
3364 snd_iprintf(buffer, "Status2 register: 0x%x\n", status2);
3365
3366 if (hdsp_check_for_iobox(hdsp)) {
3367 snd_iprintf(buffer, "No I/O box connected.\n"
3368 "Please connect one and upload firmware.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 return;
Tim Blechmannc18bc9b2009-08-12 18:21:30 +02003370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371
Takashi Iwaib0b98112005-10-20 18:29:58 +02003372 if (hdsp_check_for_firmware(hdsp, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 if (hdsp->state & HDSP_FirmwareCached) {
3374 if (snd_hdsp_load_firmware_from_cache(hdsp) != 0) {
Tim Blechmannc18bc9b2009-08-12 18:21:30 +02003375 snd_iprintf(buffer, "Firmware loading from "
3376 "cache failed, "
3377 "please upload manually.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 return;
3379 }
3380 } else {
Takashi Iwai311e70a2006-09-06 12:13:37 +02003381 int err = -EINVAL;
Takashi Iwai311e70a2006-09-06 12:13:37 +02003382 err = hdsp_request_fw_loader(hdsp);
Takashi Iwai311e70a2006-09-06 12:13:37 +02003383 if (err < 0) {
3384 snd_iprintf(buffer,
3385 "No firmware loaded nor cached, "
3386 "please upload firmware.\n");
3387 return;
3388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 }
3390 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003391
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 snd_iprintf(buffer, "FIFO status: %d\n", hdsp_read(hdsp, HDSP_fifoStatus) & 0xff);
3393 snd_iprintf(buffer, "MIDI1 Output status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusOut0));
3394 snd_iprintf(buffer, "MIDI1 Input status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusIn0));
3395 snd_iprintf(buffer, "MIDI2 Output status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusOut1));
3396 snd_iprintf(buffer, "MIDI2 Input status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusIn1));
3397 snd_iprintf(buffer, "Use Midi Tasklet: %s\n", hdsp->use_midi_tasklet ? "on" : "off");
3398
3399 snd_iprintf(buffer, "\n");
3400
3401 x = 1 << (6 + hdsp_decode_latency(hdsp->control_register & HDSP_LatencyMask));
3402
3403 snd_iprintf(buffer, "Buffer Size (Latency): %d samples (2 periods of %lu bytes)\n", x, (unsigned long) hdsp->period_bytes);
3404 snd_iprintf(buffer, "Hardware pointer (frames): %ld\n", hdsp_hw_pointer(hdsp));
3405 snd_iprintf(buffer, "Precise pointer: %s\n", hdsp->precise_ptr ? "on" : "off");
3406 snd_iprintf(buffer, "Line out: %s\n", (hdsp->control_register & HDSP_LineOut) ? "on" : "off");
3407
3408 snd_iprintf(buffer, "Firmware version: %d\n", (status2&HDSP_version0)|(status2&HDSP_version1)<<1|(status2&HDSP_version2)<<2);
3409
3410 snd_iprintf(buffer, "\n");
3411
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 switch (hdsp_clock_source(hdsp)) {
3413 case HDSP_CLOCK_SOURCE_AUTOSYNC:
3414 clock_source = "AutoSync";
3415 break;
3416 case HDSP_CLOCK_SOURCE_INTERNAL_32KHZ:
3417 clock_source = "Internal 32 kHz";
3418 break;
3419 case HDSP_CLOCK_SOURCE_INTERNAL_44_1KHZ:
3420 clock_source = "Internal 44.1 kHz";
3421 break;
3422 case HDSP_CLOCK_SOURCE_INTERNAL_48KHZ:
3423 clock_source = "Internal 48 kHz";
3424 break;
3425 case HDSP_CLOCK_SOURCE_INTERNAL_64KHZ:
3426 clock_source = "Internal 64 kHz";
3427 break;
3428 case HDSP_CLOCK_SOURCE_INTERNAL_88_2KHZ:
3429 clock_source = "Internal 88.2 kHz";
3430 break;
3431 case HDSP_CLOCK_SOURCE_INTERNAL_96KHZ:
3432 clock_source = "Internal 96 kHz";
3433 break;
3434 case HDSP_CLOCK_SOURCE_INTERNAL_128KHZ:
3435 clock_source = "Internal 128 kHz";
3436 break;
3437 case HDSP_CLOCK_SOURCE_INTERNAL_176_4KHZ:
3438 clock_source = "Internal 176.4 kHz";
3439 break;
3440 case HDSP_CLOCK_SOURCE_INTERNAL_192KHZ:
3441 clock_source = "Internal 192 kHz";
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003442 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 default:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003444 clock_source = "Error";
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 }
3446 snd_iprintf (buffer, "Sample Clock Source: %s\n", clock_source);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003447
Takashi Iwaib0b98112005-10-20 18:29:58 +02003448 if (hdsp_system_clock_mode(hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 system_clock_mode = "Slave";
Takashi Iwaib0b98112005-10-20 18:29:58 +02003450 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 system_clock_mode = "Master";
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003452
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 switch (hdsp_pref_sync_ref (hdsp)) {
3454 case HDSP_SYNC_FROM_WORD:
3455 pref_sync_ref = "Word Clock";
3456 break;
3457 case HDSP_SYNC_FROM_ADAT_SYNC:
3458 pref_sync_ref = "ADAT Sync";
3459 break;
3460 case HDSP_SYNC_FROM_SPDIF:
3461 pref_sync_ref = "SPDIF";
3462 break;
3463 case HDSP_SYNC_FROM_ADAT1:
3464 pref_sync_ref = "ADAT1";
3465 break;
3466 case HDSP_SYNC_FROM_ADAT2:
3467 pref_sync_ref = "ADAT2";
3468 break;
3469 case HDSP_SYNC_FROM_ADAT3:
3470 pref_sync_ref = "ADAT3";
3471 break;
3472 default:
3473 pref_sync_ref = "Word Clock";
3474 break;
3475 }
3476 snd_iprintf (buffer, "Preferred Sync Reference: %s\n", pref_sync_ref);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003477
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 switch (hdsp_autosync_ref (hdsp)) {
3479 case HDSP_AUTOSYNC_FROM_WORD:
3480 autosync_ref = "Word Clock";
3481 break;
3482 case HDSP_AUTOSYNC_FROM_ADAT_SYNC:
3483 autosync_ref = "ADAT Sync";
3484 break;
3485 case HDSP_AUTOSYNC_FROM_SPDIF:
3486 autosync_ref = "SPDIF";
3487 break;
3488 case HDSP_AUTOSYNC_FROM_NONE:
3489 autosync_ref = "None";
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003490 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 case HDSP_AUTOSYNC_FROM_ADAT1:
3492 autosync_ref = "ADAT1";
3493 break;
3494 case HDSP_AUTOSYNC_FROM_ADAT2:
3495 autosync_ref = "ADAT2";
3496 break;
3497 case HDSP_AUTOSYNC_FROM_ADAT3:
3498 autosync_ref = "ADAT3";
3499 break;
3500 default:
3501 autosync_ref = "---";
3502 break;
3503 }
3504 snd_iprintf (buffer, "AutoSync Reference: %s\n", autosync_ref);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003505
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 snd_iprintf (buffer, "AutoSync Frequency: %d\n", hdsp_external_sample_rate(hdsp));
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003507
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 snd_iprintf (buffer, "System Clock Mode: %s\n", system_clock_mode);
3509
3510 snd_iprintf (buffer, "System Clock Frequency: %d\n", hdsp->system_sample_rate);
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02003511 snd_iprintf (buffer, "System Clock Locked: %s\n", hdsp->clock_source_locked ? "Yes" : "No");
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003512
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 snd_iprintf(buffer, "\n");
3514
Florian Faber28b26e12010-12-01 12:14:47 +01003515 if (hdsp->io_type != RPM) {
3516 switch (hdsp_spdif_in(hdsp)) {
3517 case HDSP_SPDIFIN_OPTICAL:
3518 snd_iprintf(buffer, "IEC958 input: Optical\n");
3519 break;
3520 case HDSP_SPDIFIN_COAXIAL:
3521 snd_iprintf(buffer, "IEC958 input: Coaxial\n");
3522 break;
3523 case HDSP_SPDIFIN_INTERNAL:
3524 snd_iprintf(buffer, "IEC958 input: Internal\n");
3525 break;
3526 case HDSP_SPDIFIN_AES:
3527 snd_iprintf(buffer, "IEC958 input: AES\n");
3528 break;
3529 default:
3530 snd_iprintf(buffer, "IEC958 input: ???\n");
3531 break;
3532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003534
Florian Faber28b26e12010-12-01 12:14:47 +01003535 if (RPM == hdsp->io_type) {
3536 if (hdsp->control_register & HDSP_RPM_Bypass)
3537 snd_iprintf(buffer, "RPM Bypass: disabled\n");
3538 else
3539 snd_iprintf(buffer, "RPM Bypass: enabled\n");
3540 if (hdsp->control_register & HDSP_RPM_Disconnect)
3541 snd_iprintf(buffer, "RPM disconnected\n");
3542 else
3543 snd_iprintf(buffer, "RPM connected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544
Florian Faber28b26e12010-12-01 12:14:47 +01003545 switch (hdsp->control_register & HDSP_RPM_Inp12) {
3546 case HDSP_RPM_Inp12_Phon_6dB:
3547 snd_iprintf(buffer, "Input 1/2: Phono, 6dB\n");
3548 break;
3549 case HDSP_RPM_Inp12_Phon_0dB:
3550 snd_iprintf(buffer, "Input 1/2: Phono, 0dB\n");
3551 break;
3552 case HDSP_RPM_Inp12_Phon_n6dB:
3553 snd_iprintf(buffer, "Input 1/2: Phono, -6dB\n");
3554 break;
3555 case HDSP_RPM_Inp12_Line_0dB:
3556 snd_iprintf(buffer, "Input 1/2: Line, 0dB\n");
3557 break;
3558 case HDSP_RPM_Inp12_Line_n6dB:
3559 snd_iprintf(buffer, "Input 1/2: Line, -6dB\n");
3560 break;
3561 default:
3562 snd_iprintf(buffer, "Input 1/2: ???\n");
3563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564
Florian Faber28b26e12010-12-01 12:14:47 +01003565 switch (hdsp->control_register & HDSP_RPM_Inp34) {
3566 case HDSP_RPM_Inp34_Phon_6dB:
3567 snd_iprintf(buffer, "Input 3/4: Phono, 6dB\n");
3568 break;
3569 case HDSP_RPM_Inp34_Phon_0dB:
3570 snd_iprintf(buffer, "Input 3/4: Phono, 0dB\n");
3571 break;
3572 case HDSP_RPM_Inp34_Phon_n6dB:
3573 snd_iprintf(buffer, "Input 3/4: Phono, -6dB\n");
3574 break;
3575 case HDSP_RPM_Inp34_Line_0dB:
3576 snd_iprintf(buffer, "Input 3/4: Line, 0dB\n");
3577 break;
3578 case HDSP_RPM_Inp34_Line_n6dB:
3579 snd_iprintf(buffer, "Input 3/4: Line, -6dB\n");
3580 break;
3581 default:
3582 snd_iprintf(buffer, "Input 3/4: ???\n");
3583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584
Florian Faber28b26e12010-12-01 12:14:47 +01003585 } else {
3586 if (hdsp->control_register & HDSP_SPDIFOpticalOut)
3587 snd_iprintf(buffer, "IEC958 output: Coaxial & ADAT1\n");
3588 else
3589 snd_iprintf(buffer, "IEC958 output: Coaxial only\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590
Florian Faber28b26e12010-12-01 12:14:47 +01003591 if (hdsp->control_register & HDSP_SPDIFProfessional)
3592 snd_iprintf(buffer, "IEC958 quality: Professional\n");
3593 else
3594 snd_iprintf(buffer, "IEC958 quality: Consumer\n");
3595
3596 if (hdsp->control_register & HDSP_SPDIFEmphasis)
3597 snd_iprintf(buffer, "IEC958 emphasis: on\n");
3598 else
3599 snd_iprintf(buffer, "IEC958 emphasis: off\n");
3600
3601 if (hdsp->control_register & HDSP_SPDIFNonAudio)
3602 snd_iprintf(buffer, "IEC958 NonAudio: on\n");
3603 else
3604 snd_iprintf(buffer, "IEC958 NonAudio: off\n");
3605 x = hdsp_spdif_sample_rate(hdsp);
3606 if (x != 0)
3607 snd_iprintf(buffer, "IEC958 sample rate: %d\n", x);
3608 else
3609 snd_iprintf(buffer, "IEC958 sample rate: Error flag set\n");
3610 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 snd_iprintf(buffer, "\n");
3612
3613 /* Sync Check */
3614 x = status & HDSP_Sync0;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003615 if (status & HDSP_Lock0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 snd_iprintf(buffer, "ADAT1: %s\n", x ? "Sync" : "Lock");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003617 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 snd_iprintf(buffer, "ADAT1: No Lock\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619
3620 switch (hdsp->io_type) {
3621 case Digiface:
3622 case H9652:
3623 x = status & HDSP_Sync1;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003624 if (status & HDSP_Lock1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 snd_iprintf(buffer, "ADAT2: %s\n", x ? "Sync" : "Lock");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003626 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 snd_iprintf(buffer, "ADAT2: No Lock\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 x = status & HDSP_Sync2;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003629 if (status & HDSP_Lock2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 snd_iprintf(buffer, "ADAT3: %s\n", x ? "Sync" : "Lock");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003631 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 snd_iprintf(buffer, "ADAT3: No Lock\n");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003633 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 default:
3635 /* relax */
3636 break;
3637 }
3638
3639 x = status & HDSP_SPDIFSync;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003640 if (status & HDSP_SPDIFErrorFlag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 snd_iprintf (buffer, "SPDIF: No Lock\n");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003642 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 snd_iprintf (buffer, "SPDIF: %s\n", x ? "Sync" : "Lock");
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003644
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 x = status2 & HDSP_wc_sync;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003646 if (status2 & HDSP_wc_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 snd_iprintf (buffer, "Word Clock: %s\n", x ? "Sync" : "Lock");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003648 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 snd_iprintf (buffer, "Word Clock: No Lock\n");
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003650
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 x = status & HDSP_TimecodeSync;
Takashi Iwaib0b98112005-10-20 18:29:58 +02003652 if (status & HDSP_TimecodeLock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 snd_iprintf(buffer, "ADAT Sync: %s\n", x ? "Sync" : "Lock");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003654 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 snd_iprintf(buffer, "ADAT Sync: No Lock\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656
3657 snd_iprintf(buffer, "\n");
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003658
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 /* Informations about H9632 specific controls */
3660 if (hdsp->io_type == H9632) {
3661 char *tmp;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003662
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 switch (hdsp_ad_gain(hdsp)) {
3664 case 0:
3665 tmp = "-10 dBV";
3666 break;
3667 case 1:
3668 tmp = "+4 dBu";
3669 break;
3670 default:
3671 tmp = "Lo Gain";
3672 break;
3673 }
3674 snd_iprintf(buffer, "AD Gain : %s\n", tmp);
3675
3676 switch (hdsp_da_gain(hdsp)) {
3677 case 0:
3678 tmp = "Hi Gain";
3679 break;
3680 case 1:
3681 tmp = "+4 dBu";
3682 break;
3683 default:
3684 tmp = "-10 dBV";
3685 break;
3686 }
3687 snd_iprintf(buffer, "DA Gain : %s\n", tmp);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003688
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 switch (hdsp_phone_gain(hdsp)) {
3690 case 0:
3691 tmp = "0 dB";
3692 break;
3693 case 1:
3694 tmp = "-6 dB";
3695 break;
3696 default:
3697 tmp = "-12 dB";
3698 break;
3699 }
3700 snd_iprintf(buffer, "Phones Gain : %s\n", tmp);
3701
Adrian Knoth4833c672013-01-15 18:52:22 +01003702 snd_iprintf(buffer, "XLR Breakout Cable : %s\n",
3703 hdsp_toggle_setting(hdsp, HDSP_XLRBreakoutCable) ?
3704 "yes" : "no");
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003705
Takashi Iwaib0b98112005-10-20 18:29:58 +02003706 if (hdsp->control_register & HDSP_AnalogExtensionBoard)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 snd_iprintf(buffer, "AEB : on (ADAT1 internal)\n");
Takashi Iwaib0b98112005-10-20 18:29:58 +02003708 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 snd_iprintf(buffer, "AEB : off (ADAT1 external)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 snd_iprintf(buffer, "\n");
3711 }
3712
3713}
3714
Randy Dunlap1374f8c2008-01-16 14:55:42 +01003715static void snd_hdsp_proc_init(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003717 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718
3719 if (! snd_card_proc_new(hdsp->card, "hdsp", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02003720 snd_info_set_text_ops(entry, hdsp, snd_hdsp_proc_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721}
3722
Takashi Iwai55e957d2005-11-17 14:52:13 +01003723static void snd_hdsp_free_buffers(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724{
3725 snd_hammerfall_free_buffer(&hdsp->capture_dma_buf, hdsp->pci);
3726 snd_hammerfall_free_buffer(&hdsp->playback_dma_buf, hdsp->pci);
3727}
3728
Bill Pembertone23e7a12012-12-06 12:35:10 -05003729static int snd_hdsp_initialize_memory(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730{
3731 unsigned long pb_bus, cb_bus;
3732
3733 if (snd_hammerfall_get_buffer(hdsp->pci, &hdsp->capture_dma_buf, HDSP_DMA_AREA_BYTES) < 0 ||
3734 snd_hammerfall_get_buffer(hdsp->pci, &hdsp->playback_dma_buf, HDSP_DMA_AREA_BYTES) < 0) {
3735 if (hdsp->capture_dma_buf.area)
3736 snd_dma_free_pages(&hdsp->capture_dma_buf);
Takashi Iwaia54ba0f2014-02-26 12:05:11 +01003737 dev_err(hdsp->card->dev,
3738 "%s: no buffers available\n", hdsp->card_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 return -ENOMEM;
3740 }
3741
3742 /* Align to bus-space 64K boundary */
3743
Clemens Ladisch7ab39922006-10-09 08:13:32 +02003744 cb_bus = ALIGN(hdsp->capture_dma_buf.addr, 0x10000ul);
3745 pb_bus = ALIGN(hdsp->playback_dma_buf.addr, 0x10000ul);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746
3747 /* Tell the card where it is */
3748
3749 hdsp_write(hdsp, HDSP_inputBufferAddress, cb_bus);
3750 hdsp_write(hdsp, HDSP_outputBufferAddress, pb_bus);
3751
3752 hdsp->capture_buffer = hdsp->capture_dma_buf.area + (cb_bus - hdsp->capture_dma_buf.addr);
3753 hdsp->playback_buffer = hdsp->playback_dma_buf.area + (pb_bus - hdsp->playback_dma_buf.addr);
3754
3755 return 0;
3756}
3757
Takashi Iwai55e957d2005-11-17 14:52:13 +01003758static int snd_hdsp_set_defaults(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759{
3760 unsigned int i;
3761
3762 /* ASSUMPTION: hdsp->lock is either held, or
3763 there is no need to hold it (e.g. during module
Joe Perches561de312007-12-18 13:13:47 +01003764 initialization).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 */
3766
3767 /* set defaults:
3768
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003769 SPDIF Input via Coax
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 Master clock mode
3771 maximum latency (7 => 2^7 = 8192 samples, 64Kbyte buffer,
3772 which implies 2 4096 sample, 32Kbyte periods).
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003773 Enable line out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 */
3775
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003776 hdsp->control_register = HDSP_ClockModeMaster |
3777 HDSP_SPDIFInputCoaxial |
3778 hdsp_encode_latency(7) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 HDSP_LineOut;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003780
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781
3782 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
3783
3784#ifdef SNDRV_BIG_ENDIAN
3785 hdsp->control2_register = HDSP_BIGENDIAN_MODE;
3786#else
3787 hdsp->control2_register = 0;
3788#endif
Takashi Iwaib0b98112005-10-20 18:29:58 +02003789 if (hdsp->io_type == H9652)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 snd_hdsp_9652_enable_mixer (hdsp);
Takashi Iwaib0b98112005-10-20 18:29:58 +02003791 else
3792 hdsp_write (hdsp, HDSP_control2Reg, hdsp->control2_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793
3794 hdsp_reset_hw_pointer(hdsp);
3795 hdsp_compute_period_size(hdsp);
3796
3797 /* silence everything */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003798
Takashi Iwaib0b98112005-10-20 18:29:58 +02003799 for (i = 0; i < HDSP_MATRIX_MIXER_SIZE; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 hdsp->mixer_matrix[i] = MINUS_INFINITY_GAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801
3802 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 +02003803 if (hdsp_write_gain (hdsp, i, MINUS_INFINITY_GAIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003806
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 /* H9632 specific defaults */
3808 if (hdsp->io_type == H9632) {
3809 hdsp->control_register |= (HDSP_DAGainPlus4dBu | HDSP_ADGainPlus4dBu | HDSP_PhoneGain0dB);
3810 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
3811 }
3812
3813 /* set a default rate so that the channel map is set up.
3814 */
3815
3816 hdsp_set_rate(hdsp, 48000, 1);
3817
3818 return 0;
3819}
3820
3821static void hdsp_midi_tasklet(unsigned long arg)
3822{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003823 struct hdsp *hdsp = (struct hdsp *)arg;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003824
Takashi Iwaib0b98112005-10-20 18:29:58 +02003825 if (hdsp->midi[0].pending)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 snd_hdsp_midi_input_read (&hdsp->midi[0]);
Takashi Iwaib0b98112005-10-20 18:29:58 +02003827 if (hdsp->midi[1].pending)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 snd_hdsp_midi_input_read (&hdsp->midi[1]);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003829}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830
David Howells7d12e782006-10-05 14:55:46 +01003831static irqreturn_t snd_hdsp_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003833 struct hdsp *hdsp = (struct hdsp *) dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 unsigned int status;
3835 int audio;
3836 int midi0;
3837 int midi1;
3838 unsigned int midi0status;
3839 unsigned int midi1status;
3840 int schedule = 0;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003841
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842 status = hdsp_read(hdsp, HDSP_statusRegister);
3843
3844 audio = status & HDSP_audioIRQPending;
3845 midi0 = status & HDSP_midi0IRQPending;
3846 midi1 = status & HDSP_midi1IRQPending;
3847
Takashi Iwaib0b98112005-10-20 18:29:58 +02003848 if (!audio && !midi0 && !midi1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850
3851 hdsp_write(hdsp, HDSP_interruptConfirmation, 0);
3852
3853 midi0status = hdsp_read (hdsp, HDSP_midiStatusIn0) & 0xff;
3854 midi1status = hdsp_read (hdsp, HDSP_midiStatusIn1) & 0xff;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003855
Takashi Iwaic2503cd2009-03-05 09:37:40 +01003856 if (!(hdsp->state & HDSP_InitializationComplete))
3857 return IRQ_HANDLED;
3858
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859 if (audio) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02003860 if (hdsp->capture_substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861 snd_pcm_period_elapsed(hdsp->pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003862
Takashi Iwaib0b98112005-10-20 18:29:58 +02003863 if (hdsp->playback_substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 snd_pcm_period_elapsed(hdsp->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003866
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 if (midi0 && midi0status) {
3868 if (hdsp->use_midi_tasklet) {
3869 /* we disable interrupts for this input until processing is done */
3870 hdsp->control_register &= ~HDSP_Midi0InterruptEnable;
3871 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
3872 hdsp->midi[0].pending = 1;
3873 schedule = 1;
3874 } else {
3875 snd_hdsp_midi_input_read (&hdsp->midi[0]);
3876 }
3877 }
Florian Faber28b26e12010-12-01 12:14:47 +01003878 if (hdsp->io_type != Multiface && hdsp->io_type != RPM && hdsp->io_type != H9632 && midi1 && midi1status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879 if (hdsp->use_midi_tasklet) {
3880 /* we disable interrupts for this input until processing is done */
3881 hdsp->control_register &= ~HDSP_Midi1InterruptEnable;
3882 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
3883 hdsp->midi[1].pending = 1;
3884 schedule = 1;
3885 } else {
3886 snd_hdsp_midi_input_read (&hdsp->midi[1]);
3887 }
3888 }
3889 if (hdsp->use_midi_tasklet && schedule)
Takashi Iwai1f041282008-12-18 12:17:55 +01003890 tasklet_schedule(&hdsp->midi_tasklet);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 return IRQ_HANDLED;
3892}
3893
Takashi Iwai55e957d2005-11-17 14:52:13 +01003894static snd_pcm_uframes_t snd_hdsp_hw_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003896 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897 return hdsp_hw_pointer(hdsp);
3898}
3899
Takashi Iwai55e957d2005-11-17 14:52:13 +01003900static char *hdsp_channel_buffer_location(struct hdsp *hdsp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 int stream,
3902 int channel)
3903
3904{
3905 int mapped_channel;
3906
Takashi Iwaida3cec32008-08-08 17:12:14 +02003907 if (snd_BUG_ON(channel < 0 || channel >= hdsp->max_channels))
3908 return NULL;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003909
Takashi Iwaib0b98112005-10-20 18:29:58 +02003910 if ((mapped_channel = hdsp->channel_map[channel]) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911 return NULL;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003912
Takashi Iwaib0b98112005-10-20 18:29:58 +02003913 if (stream == SNDRV_PCM_STREAM_CAPTURE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 return hdsp->capture_buffer + (mapped_channel * HDSP_CHANNEL_BUFFER_BYTES);
Takashi Iwaib0b98112005-10-20 18:29:58 +02003915 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 return hdsp->playback_buffer + (mapped_channel * HDSP_CHANNEL_BUFFER_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917}
3918
Takashi Iwai55e957d2005-11-17 14:52:13 +01003919static int snd_hdsp_playback_copy(struct snd_pcm_substream *substream, int channel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920 snd_pcm_uframes_t pos, void __user *src, snd_pcm_uframes_t count)
3921{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003922 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 char *channel_buf;
3924
Takashi Iwaida3cec32008-08-08 17:12:14 +02003925 if (snd_BUG_ON(pos + count > HDSP_CHANNEL_BUFFER_BYTES / 4))
3926 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927
3928 channel_buf = hdsp_channel_buffer_location (hdsp, substream->pstr->stream, channel);
Takashi Iwaida3cec32008-08-08 17:12:14 +02003929 if (snd_BUG_ON(!channel_buf))
3930 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 if (copy_from_user(channel_buf + pos * 4, src, count * 4))
3932 return -EFAULT;
3933 return count;
3934}
3935
Takashi Iwai55e957d2005-11-17 14:52:13 +01003936static int snd_hdsp_capture_copy(struct snd_pcm_substream *substream, int channel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 snd_pcm_uframes_t pos, void __user *dst, snd_pcm_uframes_t count)
3938{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003939 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 char *channel_buf;
3941
Takashi Iwaida3cec32008-08-08 17:12:14 +02003942 if (snd_BUG_ON(pos + count > HDSP_CHANNEL_BUFFER_BYTES / 4))
3943 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944
3945 channel_buf = hdsp_channel_buffer_location (hdsp, substream->pstr->stream, channel);
Takashi Iwaida3cec32008-08-08 17:12:14 +02003946 if (snd_BUG_ON(!channel_buf))
3947 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 if (copy_to_user(dst, channel_buf + pos * 4, count * 4))
3949 return -EFAULT;
3950 return count;
3951}
3952
Takashi Iwai55e957d2005-11-17 14:52:13 +01003953static int snd_hdsp_hw_silence(struct snd_pcm_substream *substream, int channel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 snd_pcm_uframes_t pos, snd_pcm_uframes_t count)
3955{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003956 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 char *channel_buf;
3958
3959 channel_buf = hdsp_channel_buffer_location (hdsp, substream->pstr->stream, channel);
Takashi Iwaida3cec32008-08-08 17:12:14 +02003960 if (snd_BUG_ON(!channel_buf))
3961 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 memset(channel_buf + pos * 4, 0, count * 4);
3963 return count;
3964}
3965
Takashi Iwai55e957d2005-11-17 14:52:13 +01003966static int snd_hdsp_reset(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003968 struct snd_pcm_runtime *runtime = substream->runtime;
3969 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
3970 struct snd_pcm_substream *other;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3972 other = hdsp->capture_substream;
3973 else
3974 other = hdsp->playback_substream;
3975 if (hdsp->running)
3976 runtime->status->hw_ptr = hdsp_hw_pointer(hdsp);
3977 else
3978 runtime->status->hw_ptr = 0;
3979 if (other) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01003980 struct snd_pcm_substream *s;
3981 struct snd_pcm_runtime *oruntime = other->runtime;
Takashi Iwaief991b92007-02-22 12:52:53 +01003982 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 if (s == other) {
3984 oruntime->status->hw_ptr = runtime->status->hw_ptr;
3985 break;
3986 }
3987 }
3988 }
3989 return 0;
3990}
3991
Takashi Iwai55e957d2005-11-17 14:52:13 +01003992static int snd_hdsp_hw_params(struct snd_pcm_substream *substream,
3993 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003995 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 int err;
3997 pid_t this_pid;
3998 pid_t other_pid;
3999
Takashi Iwaib0b98112005-10-20 18:29:58 +02004000 if (hdsp_check_for_iobox (hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002
Takashi Iwaib0b98112005-10-20 18:29:58 +02004003 if (hdsp_check_for_firmware(hdsp, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005
4006 spin_lock_irq(&hdsp->lock);
4007
4008 if (substream->pstr->stream == SNDRV_PCM_STREAM_PLAYBACK) {
4009 hdsp->control_register &= ~(HDSP_SPDIFProfessional | HDSP_SPDIFNonAudio | HDSP_SPDIFEmphasis);
4010 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register |= hdsp->creg_spdif_stream);
4011 this_pid = hdsp->playback_pid;
4012 other_pid = hdsp->capture_pid;
4013 } else {
4014 this_pid = hdsp->capture_pid;
4015 other_pid = hdsp->playback_pid;
4016 }
4017
4018 if ((other_pid > 0) && (this_pid != other_pid)) {
4019
4020 /* The other stream is open, and not by the same
4021 task as this one. Make sure that the parameters
4022 that matter are the same.
4023 */
4024
4025 if (params_rate(params) != hdsp->system_sample_rate) {
4026 spin_unlock_irq(&hdsp->lock);
4027 _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_RATE);
4028 return -EBUSY;
4029 }
4030
4031 if (params_period_size(params) != hdsp->period_bytes / 4) {
4032 spin_unlock_irq(&hdsp->lock);
4033 _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
4034 return -EBUSY;
4035 }
4036
4037 /* We're fine. */
4038
4039 spin_unlock_irq(&hdsp->lock);
4040 return 0;
4041
4042 } else {
4043 spin_unlock_irq(&hdsp->lock);
4044 }
4045
4046 /* how to make sure that the rate matches an externally-set one ?
4047 */
4048
4049 spin_lock_irq(&hdsp->lock);
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02004050 if (! hdsp->clock_source_locked) {
4051 if ((err = hdsp_set_rate(hdsp, params_rate(params), 0)) < 0) {
4052 spin_unlock_irq(&hdsp->lock);
4053 _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_RATE);
4054 return err;
4055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 }
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02004057 spin_unlock_irq(&hdsp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058
4059 if ((err = hdsp_set_interrupt_interval(hdsp, params_period_size(params))) < 0) {
4060 _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
4061 return err;
4062 }
4063
4064 return 0;
4065}
4066
Takashi Iwai55e957d2005-11-17 14:52:13 +01004067static int snd_hdsp_channel_info(struct snd_pcm_substream *substream,
4068 struct snd_pcm_channel_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004070 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 int mapped_channel;
4072
Takashi Iwaida3cec32008-08-08 17:12:14 +02004073 if (snd_BUG_ON(info->channel >= hdsp->max_channels))
4074 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075
Takashi Iwaib0b98112005-10-20 18:29:58 +02004076 if ((mapped_channel = hdsp->channel_map[info->channel]) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078
4079 info->offset = mapped_channel * HDSP_CHANNEL_BUFFER_BYTES;
4080 info->first = 0;
4081 info->step = 32;
4082 return 0;
4083}
4084
Takashi Iwai55e957d2005-11-17 14:52:13 +01004085static int snd_hdsp_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 unsigned int cmd, void *arg)
4087{
4088 switch (cmd) {
4089 case SNDRV_PCM_IOCTL1_RESET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090 return snd_hdsp_reset(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
Takashi Iwaib0b98112005-10-20 18:29:58 +02004092 return snd_hdsp_channel_info(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 default:
4094 break;
4095 }
4096
4097 return snd_pcm_lib_ioctl(substream, cmd, arg);
4098}
4099
Takashi Iwai55e957d2005-11-17 14:52:13 +01004100static int snd_hdsp_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004102 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
4103 struct snd_pcm_substream *other;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 int running;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004105
Takashi Iwaib0b98112005-10-20 18:29:58 +02004106 if (hdsp_check_for_iobox (hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108
Takashi Iwai311e70a2006-09-06 12:13:37 +02004109 if (hdsp_check_for_firmware(hdsp, 0)) /* no auto-loading in trigger */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111
4112 spin_lock(&hdsp->lock);
4113 running = hdsp->running;
4114 switch (cmd) {
4115 case SNDRV_PCM_TRIGGER_START:
4116 running |= 1 << substream->stream;
4117 break;
4118 case SNDRV_PCM_TRIGGER_STOP:
4119 running &= ~(1 << substream->stream);
4120 break;
4121 default:
4122 snd_BUG();
4123 spin_unlock(&hdsp->lock);
4124 return -EINVAL;
4125 }
4126 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
4127 other = hdsp->capture_substream;
4128 else
4129 other = hdsp->playback_substream;
4130
4131 if (other) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004132 struct snd_pcm_substream *s;
Takashi Iwaief991b92007-02-22 12:52:53 +01004133 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 if (s == other) {
4135 snd_pcm_trigger_done(s, substream);
4136 if (cmd == SNDRV_PCM_TRIGGER_START)
4137 running |= 1 << s->stream;
4138 else
4139 running &= ~(1 << s->stream);
4140 goto _ok;
4141 }
4142 }
4143 if (cmd == SNDRV_PCM_TRIGGER_START) {
4144 if (!(running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) &&
4145 substream->stream == SNDRV_PCM_STREAM_CAPTURE)
4146 hdsp_silence_playback(hdsp);
4147 } else {
4148 if (running &&
4149 substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
4150 hdsp_silence_playback(hdsp);
4151 }
4152 } else {
4153 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
4154 hdsp_silence_playback(hdsp);
4155 }
4156 _ok:
4157 snd_pcm_trigger_done(substream, substream);
4158 if (!hdsp->running && running)
4159 hdsp_start_audio(hdsp);
4160 else if (hdsp->running && !running)
4161 hdsp_stop_audio(hdsp);
4162 hdsp->running = running;
4163 spin_unlock(&hdsp->lock);
4164
4165 return 0;
4166}
4167
Takashi Iwai55e957d2005-11-17 14:52:13 +01004168static int snd_hdsp_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004170 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 int result = 0;
4172
Takashi Iwaib0b98112005-10-20 18:29:58 +02004173 if (hdsp_check_for_iobox (hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175
Takashi Iwaib0b98112005-10-20 18:29:58 +02004176 if (hdsp_check_for_firmware(hdsp, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178
4179 spin_lock_irq(&hdsp->lock);
4180 if (!hdsp->running)
4181 hdsp_reset_hw_pointer(hdsp);
4182 spin_unlock_irq(&hdsp->lock);
4183 return result;
4184}
4185
Takashi Iwai55e957d2005-11-17 14:52:13 +01004186static struct snd_pcm_hardware snd_hdsp_playback_subinfo =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187{
4188 .info = (SNDRV_PCM_INFO_MMAP |
4189 SNDRV_PCM_INFO_MMAP_VALID |
4190 SNDRV_PCM_INFO_NONINTERLEAVED |
4191 SNDRV_PCM_INFO_SYNC_START |
4192 SNDRV_PCM_INFO_DOUBLE),
4193#ifdef SNDRV_BIG_ENDIAN
4194 .formats = SNDRV_PCM_FMTBIT_S32_BE,
4195#else
4196 .formats = SNDRV_PCM_FMTBIT_S32_LE,
4197#endif
4198 .rates = (SNDRV_PCM_RATE_32000 |
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004199 SNDRV_PCM_RATE_44100 |
4200 SNDRV_PCM_RATE_48000 |
4201 SNDRV_PCM_RATE_64000 |
4202 SNDRV_PCM_RATE_88200 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 SNDRV_PCM_RATE_96000),
4204 .rate_min = 32000,
4205 .rate_max = 96000,
Florian Faber28b26e12010-12-01 12:14:47 +01004206 .channels_min = 6,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 .channels_max = HDSP_MAX_CHANNELS,
4208 .buffer_bytes_max = HDSP_CHANNEL_BUFFER_BYTES * HDSP_MAX_CHANNELS,
4209 .period_bytes_min = (64 * 4) * 10,
4210 .period_bytes_max = (8192 * 4) * HDSP_MAX_CHANNELS,
4211 .periods_min = 2,
4212 .periods_max = 2,
4213 .fifo_size = 0
4214};
4215
Takashi Iwai55e957d2005-11-17 14:52:13 +01004216static struct snd_pcm_hardware snd_hdsp_capture_subinfo =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217{
4218 .info = (SNDRV_PCM_INFO_MMAP |
4219 SNDRV_PCM_INFO_MMAP_VALID |
4220 SNDRV_PCM_INFO_NONINTERLEAVED |
4221 SNDRV_PCM_INFO_SYNC_START),
4222#ifdef SNDRV_BIG_ENDIAN
4223 .formats = SNDRV_PCM_FMTBIT_S32_BE,
4224#else
4225 .formats = SNDRV_PCM_FMTBIT_S32_LE,
4226#endif
4227 .rates = (SNDRV_PCM_RATE_32000 |
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004228 SNDRV_PCM_RATE_44100 |
4229 SNDRV_PCM_RATE_48000 |
4230 SNDRV_PCM_RATE_64000 |
4231 SNDRV_PCM_RATE_88200 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 SNDRV_PCM_RATE_96000),
4233 .rate_min = 32000,
4234 .rate_max = 96000,
Florian Faber28b26e12010-12-01 12:14:47 +01004235 .channels_min = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 .channels_max = HDSP_MAX_CHANNELS,
4237 .buffer_bytes_max = HDSP_CHANNEL_BUFFER_BYTES * HDSP_MAX_CHANNELS,
4238 .period_bytes_min = (64 * 4) * 10,
4239 .period_bytes_max = (8192 * 4) * HDSP_MAX_CHANNELS,
4240 .periods_min = 2,
4241 .periods_max = 2,
4242 .fifo_size = 0
4243};
4244
4245static unsigned int hdsp_period_sizes[] = { 64, 128, 256, 512, 1024, 2048, 4096, 8192 };
4246
Takashi Iwai55e957d2005-11-17 14:52:13 +01004247static struct snd_pcm_hw_constraint_list hdsp_hw_constraints_period_sizes = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 .count = ARRAY_SIZE(hdsp_period_sizes),
4249 .list = hdsp_period_sizes,
4250 .mask = 0
4251};
4252
4253static unsigned int hdsp_9632_sample_rates[] = { 32000, 44100, 48000, 64000, 88200, 96000, 128000, 176400, 192000 };
4254
Takashi Iwai55e957d2005-11-17 14:52:13 +01004255static struct snd_pcm_hw_constraint_list hdsp_hw_constraints_9632_sample_rates = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 .count = ARRAY_SIZE(hdsp_9632_sample_rates),
4257 .list = hdsp_9632_sample_rates,
4258 .mask = 0
4259};
4260
Takashi Iwai55e957d2005-11-17 14:52:13 +01004261static int snd_hdsp_hw_rule_in_channels(struct snd_pcm_hw_params *params,
4262 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004264 struct hdsp *hdsp = rule->private;
4265 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 if (hdsp->io_type == H9632) {
4267 unsigned int list[3];
4268 list[0] = hdsp->qs_in_channels;
4269 list[1] = hdsp->ds_in_channels;
4270 list[2] = hdsp->ss_in_channels;
4271 return snd_interval_list(c, 3, list, 0);
4272 } else {
4273 unsigned int list[2];
4274 list[0] = hdsp->ds_in_channels;
4275 list[1] = hdsp->ss_in_channels;
4276 return snd_interval_list(c, 2, list, 0);
4277 }
4278}
4279
Takashi Iwai55e957d2005-11-17 14:52:13 +01004280static int snd_hdsp_hw_rule_out_channels(struct snd_pcm_hw_params *params,
4281 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282{
4283 unsigned int list[3];
Takashi Iwai55e957d2005-11-17 14:52:13 +01004284 struct hdsp *hdsp = rule->private;
4285 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 if (hdsp->io_type == H9632) {
4287 list[0] = hdsp->qs_out_channels;
4288 list[1] = hdsp->ds_out_channels;
4289 list[2] = hdsp->ss_out_channels;
4290 return snd_interval_list(c, 3, list, 0);
4291 } else {
4292 list[0] = hdsp->ds_out_channels;
4293 list[1] = hdsp->ss_out_channels;
4294 }
4295 return snd_interval_list(c, 2, list, 0);
4296}
4297
Takashi Iwai55e957d2005-11-17 14:52:13 +01004298static int snd_hdsp_hw_rule_in_channels_rate(struct snd_pcm_hw_params *params,
4299 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004301 struct hdsp *hdsp = rule->private;
4302 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
4303 struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 if (r->min > 96000 && hdsp->io_type == H9632) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004305 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 .min = hdsp->qs_in_channels,
4307 .max = hdsp->qs_in_channels,
4308 .integer = 1,
4309 };
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004310 return snd_interval_refine(c, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 } else if (r->min > 48000 && r->max <= 96000) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004312 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 .min = hdsp->ds_in_channels,
4314 .max = hdsp->ds_in_channels,
4315 .integer = 1,
4316 };
4317 return snd_interval_refine(c, &t);
4318 } else if (r->max < 64000) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004319 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 .min = hdsp->ss_in_channels,
4321 .max = hdsp->ss_in_channels,
4322 .integer = 1,
4323 };
4324 return snd_interval_refine(c, &t);
4325 }
4326 return 0;
4327}
4328
Takashi Iwai55e957d2005-11-17 14:52:13 +01004329static int snd_hdsp_hw_rule_out_channels_rate(struct snd_pcm_hw_params *params,
4330 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004332 struct hdsp *hdsp = rule->private;
4333 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
4334 struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 if (r->min > 96000 && hdsp->io_type == H9632) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004336 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 .min = hdsp->qs_out_channels,
4338 .max = hdsp->qs_out_channels,
4339 .integer = 1,
4340 };
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004341 return snd_interval_refine(c, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 } else if (r->min > 48000 && r->max <= 96000) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004343 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 .min = hdsp->ds_out_channels,
4345 .max = hdsp->ds_out_channels,
4346 .integer = 1,
4347 };
4348 return snd_interval_refine(c, &t);
4349 } else if (r->max < 64000) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004350 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 .min = hdsp->ss_out_channels,
4352 .max = hdsp->ss_out_channels,
4353 .integer = 1,
4354 };
4355 return snd_interval_refine(c, &t);
4356 }
4357 return 0;
4358}
4359
Takashi Iwai55e957d2005-11-17 14:52:13 +01004360static int snd_hdsp_hw_rule_rate_out_channels(struct snd_pcm_hw_params *params,
4361 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004363 struct hdsp *hdsp = rule->private;
4364 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
4365 struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366 if (c->min >= hdsp->ss_out_channels) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004367 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 .min = 32000,
4369 .max = 48000,
4370 .integer = 1,
4371 };
4372 return snd_interval_refine(r, &t);
4373 } else if (c->max <= hdsp->qs_out_channels && hdsp->io_type == H9632) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004374 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375 .min = 128000,
4376 .max = 192000,
4377 .integer = 1,
4378 };
4379 return snd_interval_refine(r, &t);
4380 } else if (c->max <= hdsp->ds_out_channels) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004381 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 .min = 64000,
4383 .max = 96000,
4384 .integer = 1,
4385 };
4386 return snd_interval_refine(r, &t);
4387 }
4388 return 0;
4389}
4390
Takashi Iwai55e957d2005-11-17 14:52:13 +01004391static int snd_hdsp_hw_rule_rate_in_channels(struct snd_pcm_hw_params *params,
4392 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004394 struct hdsp *hdsp = rule->private;
4395 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
4396 struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 if (c->min >= hdsp->ss_in_channels) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004398 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 .min = 32000,
4400 .max = 48000,
4401 .integer = 1,
4402 };
4403 return snd_interval_refine(r, &t);
4404 } else if (c->max <= hdsp->qs_in_channels && hdsp->io_type == H9632) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004405 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 .min = 128000,
4407 .max = 192000,
4408 .integer = 1,
4409 };
4410 return snd_interval_refine(r, &t);
4411 } else if (c->max <= hdsp->ds_in_channels) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004412 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 .min = 64000,
4414 .max = 96000,
4415 .integer = 1,
4416 };
4417 return snd_interval_refine(r, &t);
4418 }
4419 return 0;
4420}
4421
Takashi Iwai55e957d2005-11-17 14:52:13 +01004422static int snd_hdsp_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004424 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
4425 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426
Takashi Iwaib0b98112005-10-20 18:29:58 +02004427 if (hdsp_check_for_iobox (hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429
Takashi Iwaib0b98112005-10-20 18:29:58 +02004430 if (hdsp_check_for_firmware(hdsp, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432
4433 spin_lock_irq(&hdsp->lock);
4434
4435 snd_pcm_set_sync(substream);
4436
4437 runtime->hw = snd_hdsp_playback_subinfo;
4438 runtime->dma_area = hdsp->playback_buffer;
4439 runtime->dma_bytes = HDSP_DMA_AREA_BYTES;
4440
4441 hdsp->playback_pid = current->pid;
4442 hdsp->playback_substream = substream;
4443
4444 spin_unlock_irq(&hdsp->lock);
4445
4446 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
4447 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 +02004448 if (hdsp->clock_source_locked) {
4449 runtime->hw.rate_min = runtime->hw.rate_max = hdsp->system_sample_rate;
4450 } else if (hdsp->io_type == H9632) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 runtime->hw.rate_max = 192000;
4452 runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
4453 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hdsp_hw_constraints_9632_sample_rates);
4454 }
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02004455 if (hdsp->io_type == H9632) {
4456 runtime->hw.channels_min = hdsp->qs_out_channels;
4457 runtime->hw.channels_max = hdsp->ss_out_channels;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004458 }
4459
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
4461 snd_hdsp_hw_rule_out_channels, hdsp,
4462 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
4463 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
4464 snd_hdsp_hw_rule_out_channels_rate, hdsp,
4465 SNDRV_PCM_HW_PARAM_RATE, -1);
4466 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
4467 snd_hdsp_hw_rule_rate_out_channels, hdsp,
4468 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
4469
Florian Faber28b26e12010-12-01 12:14:47 +01004470 if (RPM != hdsp->io_type) {
4471 hdsp->creg_spdif_stream = hdsp->creg_spdif;
4472 hdsp->spdif_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
4473 snd_ctl_notify(hdsp->card, SNDRV_CTL_EVENT_MASK_VALUE |
4474 SNDRV_CTL_EVENT_MASK_INFO, &hdsp->spdif_ctl->id);
4475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 return 0;
4477}
4478
Takashi Iwai55e957d2005-11-17 14:52:13 +01004479static int snd_hdsp_playback_release(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004481 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482
4483 spin_lock_irq(&hdsp->lock);
4484
4485 hdsp->playback_pid = -1;
4486 hdsp->playback_substream = NULL;
4487
4488 spin_unlock_irq(&hdsp->lock);
4489
Florian Faber28b26e12010-12-01 12:14:47 +01004490 if (RPM != hdsp->io_type) {
4491 hdsp->spdif_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
4492 snd_ctl_notify(hdsp->card, SNDRV_CTL_EVENT_MASK_VALUE |
4493 SNDRV_CTL_EVENT_MASK_INFO, &hdsp->spdif_ctl->id);
4494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495 return 0;
4496}
4497
4498
Takashi Iwai55e957d2005-11-17 14:52:13 +01004499static int snd_hdsp_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004501 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
4502 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503
Takashi Iwaib0b98112005-10-20 18:29:58 +02004504 if (hdsp_check_for_iobox (hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506
Takashi Iwaib0b98112005-10-20 18:29:58 +02004507 if (hdsp_check_for_firmware(hdsp, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509
4510 spin_lock_irq(&hdsp->lock);
4511
4512 snd_pcm_set_sync(substream);
4513
4514 runtime->hw = snd_hdsp_capture_subinfo;
4515 runtime->dma_area = hdsp->capture_buffer;
4516 runtime->dma_bytes = HDSP_DMA_AREA_BYTES;
4517
4518 hdsp->capture_pid = current->pid;
4519 hdsp->capture_substream = substream;
4520
4521 spin_unlock_irq(&hdsp->lock);
4522
4523 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
4524 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, &hdsp_hw_constraints_period_sizes);
4525 if (hdsp->io_type == H9632) {
4526 runtime->hw.channels_min = hdsp->qs_in_channels;
4527 runtime->hw.channels_max = hdsp->ss_in_channels;
4528 runtime->hw.rate_max = 192000;
4529 runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
4530 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hdsp_hw_constraints_9632_sample_rates);
4531 }
4532 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
4533 snd_hdsp_hw_rule_in_channels, hdsp,
4534 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
4535 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
4536 snd_hdsp_hw_rule_in_channels_rate, hdsp,
4537 SNDRV_PCM_HW_PARAM_RATE, -1);
4538 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
4539 snd_hdsp_hw_rule_rate_in_channels, hdsp,
4540 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
4541 return 0;
4542}
4543
Takashi Iwai55e957d2005-11-17 14:52:13 +01004544static int snd_hdsp_capture_release(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004546 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547
4548 spin_lock_irq(&hdsp->lock);
4549
4550 hdsp->capture_pid = -1;
4551 hdsp->capture_substream = NULL;
4552
4553 spin_unlock_irq(&hdsp->lock);
4554 return 0;
4555}
4556
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557/* helper functions for copying meter values */
4558static inline int copy_u32_le(void __user *dest, void __iomem *src)
4559{
4560 u32 val = readl(src);
4561 return copy_to_user(dest, &val, 4);
4562}
4563
4564static inline int copy_u64_le(void __user *dest, void __iomem *src_low, void __iomem *src_high)
4565{
4566 u32 rms_low, rms_high;
4567 u64 rms;
4568 rms_low = readl(src_low);
4569 rms_high = readl(src_high);
4570 rms = ((u64)rms_high << 32) | rms_low;
4571 return copy_to_user(dest, &rms, 8);
4572}
4573
4574static inline int copy_u48_le(void __user *dest, void __iomem *src_low, void __iomem *src_high)
4575{
4576 u32 rms_low, rms_high;
4577 u64 rms;
4578 rms_low = readl(src_low) & 0xffffff00;
4579 rms_high = readl(src_high) & 0xffffff00;
4580 rms = ((u64)rms_high << 32) | rms_low;
4581 return copy_to_user(dest, &rms, 8);
4582}
4583
Takashi Iwai55e957d2005-11-17 14:52:13 +01004584static int hdsp_9652_get_peak(struct hdsp *hdsp, struct hdsp_peak_rms __user *peak_rms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585{
4586 int doublespeed = 0;
4587 int i, j, channels, ofs;
4588
4589 if (hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DoubleSpeedStatus)
4590 doublespeed = 1;
4591 channels = doublespeed ? 14 : 26;
4592 for (i = 0, j = 0; i < 26; ++i) {
4593 if (doublespeed && (i & 4))
4594 continue;
4595 ofs = HDSP_9652_peakBase - j * 4;
4596 if (copy_u32_le(&peak_rms->input_peaks[i], hdsp->iobase + ofs))
4597 return -EFAULT;
4598 ofs -= channels * 4;
4599 if (copy_u32_le(&peak_rms->playback_peaks[i], hdsp->iobase + ofs))
4600 return -EFAULT;
4601 ofs -= channels * 4;
4602 if (copy_u32_le(&peak_rms->output_peaks[i], hdsp->iobase + ofs))
4603 return -EFAULT;
4604 ofs = HDSP_9652_rmsBase + j * 8;
4605 if (copy_u48_le(&peak_rms->input_rms[i], hdsp->iobase + ofs,
4606 hdsp->iobase + ofs + 4))
4607 return -EFAULT;
4608 ofs += channels * 8;
4609 if (copy_u48_le(&peak_rms->playback_rms[i], hdsp->iobase + ofs,
4610 hdsp->iobase + ofs + 4))
4611 return -EFAULT;
4612 ofs += channels * 8;
4613 if (copy_u48_le(&peak_rms->output_rms[i], hdsp->iobase + ofs,
4614 hdsp->iobase + ofs + 4))
4615 return -EFAULT;
4616 j++;
4617 }
4618 return 0;
4619}
4620
Takashi Iwai55e957d2005-11-17 14:52:13 +01004621static int hdsp_9632_get_peak(struct hdsp *hdsp, struct hdsp_peak_rms __user *peak_rms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622{
4623 int i, j;
Takashi Iwai55e957d2005-11-17 14:52:13 +01004624 struct hdsp_9632_meters __iomem *m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625 int doublespeed = 0;
4626
4627 if (hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DoubleSpeedStatus)
4628 doublespeed = 1;
Takashi Iwai55e957d2005-11-17 14:52:13 +01004629 m = (struct hdsp_9632_meters __iomem *)(hdsp->iobase+HDSP_9632_metersBase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 for (i = 0, j = 0; i < 16; ++i, ++j) {
4631 if (copy_u32_le(&peak_rms->input_peaks[i], &m->input_peak[j]))
4632 return -EFAULT;
4633 if (copy_u32_le(&peak_rms->playback_peaks[i], &m->playback_peak[j]))
4634 return -EFAULT;
4635 if (copy_u32_le(&peak_rms->output_peaks[i], &m->output_peak[j]))
4636 return -EFAULT;
4637 if (copy_u64_le(&peak_rms->input_rms[i], &m->input_rms_low[j],
4638 &m->input_rms_high[j]))
4639 return -EFAULT;
4640 if (copy_u64_le(&peak_rms->playback_rms[i], &m->playback_rms_low[j],
4641 &m->playback_rms_high[j]))
4642 return -EFAULT;
4643 if (copy_u64_le(&peak_rms->output_rms[i], &m->output_rms_low[j],
4644 &m->output_rms_high[j]))
4645 return -EFAULT;
4646 if (doublespeed && i == 3) i += 4;
4647 }
4648 return 0;
4649}
4650
Takashi Iwai55e957d2005-11-17 14:52:13 +01004651static int hdsp_get_peak(struct hdsp *hdsp, struct hdsp_peak_rms __user *peak_rms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652{
4653 int i;
4654
4655 for (i = 0; i < 26; i++) {
4656 if (copy_u32_le(&peak_rms->playback_peaks[i],
4657 hdsp->iobase + HDSP_playbackPeakLevel + i * 4))
4658 return -EFAULT;
4659 if (copy_u32_le(&peak_rms->input_peaks[i],
4660 hdsp->iobase + HDSP_inputPeakLevel + i * 4))
4661 return -EFAULT;
4662 }
4663 for (i = 0; i < 28; i++) {
4664 if (copy_u32_le(&peak_rms->output_peaks[i],
4665 hdsp->iobase + HDSP_outputPeakLevel + i * 4))
4666 return -EFAULT;
4667 }
4668 for (i = 0; i < 26; ++i) {
4669 if (copy_u64_le(&peak_rms->playback_rms[i],
4670 hdsp->iobase + HDSP_playbackRmsLevel + i * 8 + 4,
4671 hdsp->iobase + HDSP_playbackRmsLevel + i * 8))
4672 return -EFAULT;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004673 if (copy_u64_le(&peak_rms->input_rms[i],
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 hdsp->iobase + HDSP_inputRmsLevel + i * 8 + 4,
4675 hdsp->iobase + HDSP_inputRmsLevel + i * 8))
4676 return -EFAULT;
4677 }
4678 return 0;
4679}
4680
Takashi Iwai55e957d2005-11-17 14:52:13 +01004681static 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 -07004682{
Joe Perches9fe856e2010-09-04 18:52:54 -07004683 struct hdsp *hdsp = hw->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 void __user *argp = (void __user *)arg;
Tim Blechmann3ae7e2e2008-11-08 14:42:18 +01004685 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686
4687 switch (cmd) {
4688 case SNDRV_HDSP_IOCTL_GET_PEAK_RMS: {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004689 struct hdsp_peak_rms __user *peak_rms = (struct hdsp_peak_rms __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690
Tim Blechmann3ae7e2e2008-11-08 14:42:18 +01004691 err = hdsp_check_for_iobox(hdsp);
4692 if (err < 0)
4693 return err;
4694
4695 err = hdsp_check_for_firmware(hdsp, 1);
4696 if (err < 0)
4697 return err;
4698
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 if (!(hdsp->state & HDSP_FirmwareLoaded)) {
Takashi Iwaia54ba0f2014-02-26 12:05:11 +01004700 dev_err(hdsp->card->dev,
4701 "firmware needs to be uploaded to the card.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702 return -EINVAL;
4703 }
4704
4705 switch (hdsp->io_type) {
4706 case H9652:
4707 return hdsp_9652_get_peak(hdsp, peak_rms);
4708 case H9632:
4709 return hdsp_9632_get_peak(hdsp, peak_rms);
4710 default:
4711 return hdsp_get_peak(hdsp, peak_rms);
4712 }
4713 }
4714 case SNDRV_HDSP_IOCTL_GET_CONFIG_INFO: {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004715 struct hdsp_config_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716 unsigned long flags;
4717 int i;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004718
Tim Blechmann3ae7e2e2008-11-08 14:42:18 +01004719 err = hdsp_check_for_iobox(hdsp);
4720 if (err < 0)
4721 return err;
4722
4723 err = hdsp_check_for_firmware(hdsp, 1);
4724 if (err < 0)
4725 return err;
4726
Dan Rosenberge68d3b32010-09-25 11:07:27 -04004727 memset(&info, 0, sizeof(info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728 spin_lock_irqsave(&hdsp->lock, flags);
4729 info.pref_sync_ref = (unsigned char)hdsp_pref_sync_ref(hdsp);
4730 info.wordclock_sync_check = (unsigned char)hdsp_wc_sync_check(hdsp);
Takashi Iwaib0b98112005-10-20 18:29:58 +02004731 if (hdsp->io_type != H9632)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732 info.adatsync_sync_check = (unsigned char)hdsp_adatsync_sync_check(hdsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 info.spdif_sync_check = (unsigned char)hdsp_spdif_sync_check(hdsp);
Florian Faber28b26e12010-12-01 12:14:47 +01004734 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 -07004735 info.adat_sync_check[i] = (unsigned char)hdsp_adat_sync_check(hdsp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 info.spdif_in = (unsigned char)hdsp_spdif_in(hdsp);
Adrian Knoth4833c672013-01-15 18:52:22 +01004737 info.spdif_out = (unsigned char)hdsp_toggle_setting(hdsp,
4738 HDSP_SPDIFOpticalOut);
4739 info.spdif_professional = (unsigned char)
4740 hdsp_toggle_setting(hdsp, HDSP_SPDIFProfessional);
4741 info.spdif_emphasis = (unsigned char)
4742 hdsp_toggle_setting(hdsp, HDSP_SPDIFEmphasis);
4743 info.spdif_nonaudio = (unsigned char)
4744 hdsp_toggle_setting(hdsp, HDSP_SPDIFNonAudio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745 info.spdif_sample_rate = hdsp_spdif_sample_rate(hdsp);
4746 info.system_sample_rate = hdsp->system_sample_rate;
4747 info.autosync_sample_rate = hdsp_external_sample_rate(hdsp);
4748 info.system_clock_mode = (unsigned char)hdsp_system_clock_mode(hdsp);
4749 info.clock_source = (unsigned char)hdsp_clock_source(hdsp);
4750 info.autosync_ref = (unsigned char)hdsp_autosync_ref(hdsp);
Adrian Knoth4833c672013-01-15 18:52:22 +01004751 info.line_out = (unsigned char)
4752 hdsp_toggle_setting(hdsp, HDSP_LineOut);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753 if (hdsp->io_type == H9632) {
4754 info.da_gain = (unsigned char)hdsp_da_gain(hdsp);
4755 info.ad_gain = (unsigned char)hdsp_ad_gain(hdsp);
4756 info.phone_gain = (unsigned char)hdsp_phone_gain(hdsp);
Adrian Knoth4833c672013-01-15 18:52:22 +01004757 info.xlr_breakout_cable =
4758 (unsigned char)hdsp_toggle_setting(hdsp,
4759 HDSP_XLRBreakoutCable);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004760
Florian Faber28b26e12010-12-01 12:14:47 +01004761 } else if (hdsp->io_type == RPM) {
4762 info.da_gain = (unsigned char) hdsp_rpm_input12(hdsp);
4763 info.ad_gain = (unsigned char) hdsp_rpm_input34(hdsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 }
Takashi Iwaib0b98112005-10-20 18:29:58 +02004765 if (hdsp->io_type == H9632 || hdsp->io_type == H9652)
Adrian Knoth4833c672013-01-15 18:52:22 +01004766 info.analog_extension_board =
4767 (unsigned char)hdsp_toggle_setting(hdsp,
4768 HDSP_AnalogExtensionBoard);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 spin_unlock_irqrestore(&hdsp->lock, flags);
4770 if (copy_to_user(argp, &info, sizeof(info)))
4771 return -EFAULT;
4772 break;
4773 }
4774 case SNDRV_HDSP_IOCTL_GET_9632_AEB: {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004775 struct hdsp_9632_aeb h9632_aeb;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004776
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 if (hdsp->io_type != H9632) return -EINVAL;
4778 h9632_aeb.aebi = hdsp->ss_in_channels - H9632_SS_CHANNELS;
4779 h9632_aeb.aebo = hdsp->ss_out_channels - H9632_SS_CHANNELS;
4780 if (copy_to_user(argp, &h9632_aeb, sizeof(h9632_aeb)))
4781 return -EFAULT;
4782 break;
4783 }
4784 case SNDRV_HDSP_IOCTL_GET_VERSION: {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004785 struct hdsp_version hdsp_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786 int err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004787
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788 if (hdsp->io_type == H9652 || hdsp->io_type == H9632) return -EINVAL;
4789 if (hdsp->io_type == Undefined) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02004790 if ((err = hdsp_get_iobox_version(hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792 }
Dan Carpenterd14df332013-10-16 11:44:25 +03004793 memset(&hdsp_version, 0, sizeof(hdsp_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 hdsp_version.io_type = hdsp->io_type;
4795 hdsp_version.firmware_rev = hdsp->firmware_rev;
Takashi Iwaib0b98112005-10-20 18:29:58 +02004796 if ((err = copy_to_user(argp, &hdsp_version, sizeof(hdsp_version))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 break;
4799 }
4800 case SNDRV_HDSP_IOCTL_UPLOAD_FIRMWARE: {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004801 struct hdsp_firmware __user *firmware;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802 u32 __user *firmware_data;
4803 int err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004804
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805 if (hdsp->io_type == H9652 || hdsp->io_type == H9632) return -EINVAL;
4806 /* SNDRV_HDSP_IOCTL_GET_VERSION must have been called */
4807 if (hdsp->io_type == Undefined) return -EINVAL;
4808
4809 if (hdsp->state & (HDSP_FirmwareCached | HDSP_FirmwareLoaded))
4810 return -EBUSY;
4811
Takashi Iwaia54ba0f2014-02-26 12:05:11 +01004812 dev_info(hdsp->card->dev,
4813 "initializing firmware upload\n");
Takashi Iwai55e957d2005-11-17 14:52:13 +01004814 firmware = (struct hdsp_firmware __user *)argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815
Takashi Iwaib0b98112005-10-20 18:29:58 +02004816 if (get_user(firmware_data, &firmware->firmware_data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817 return -EFAULT;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004818
Takashi Iwaib0b98112005-10-20 18:29:58 +02004819 if (hdsp_check_for_iobox (hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821
Takashi Iwai90caaef2012-11-22 16:55:11 +01004822 if (!hdsp->fw_uploaded) {
4823 hdsp->fw_uploaded = vmalloc(HDSP_FIRMWARE_SIZE);
4824 if (!hdsp->fw_uploaded)
4825 return -ENOMEM;
4826 }
4827
4828 if (copy_from_user(hdsp->fw_uploaded, firmware_data,
4829 HDSP_FIRMWARE_SIZE)) {
4830 vfree(hdsp->fw_uploaded);
4831 hdsp->fw_uploaded = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832 return -EFAULT;
Takashi Iwai90caaef2012-11-22 16:55:11 +01004833 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004834
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835 hdsp->state |= HDSP_FirmwareCached;
4836
Takashi Iwaib0b98112005-10-20 18:29:58 +02004837 if ((err = snd_hdsp_load_firmware_from_cache(hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838 return err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004839
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840 if (!(hdsp->state & HDSP_InitializationComplete)) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02004841 if ((err = snd_hdsp_enable_io(hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842 return err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004843
4844 snd_hdsp_initialize_channels(hdsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845 snd_hdsp_initialize_midi_flush(hdsp);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004846
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 if ((err = snd_hdsp_create_alsa_devices(hdsp->card, hdsp)) < 0) {
Takashi Iwaia54ba0f2014-02-26 12:05:11 +01004848 dev_err(hdsp->card->dev,
4849 "error creating alsa devices\n");
Takashi Iwaib0b98112005-10-20 18:29:58 +02004850 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851 }
4852 }
4853 break;
4854 }
4855 case SNDRV_HDSP_IOCTL_GET_MIXER: {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004856 struct hdsp_mixer __user *mixer = (struct hdsp_mixer __user *)argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857 if (copy_to_user(mixer->matrix, hdsp->mixer_matrix, sizeof(unsigned short)*HDSP_MATRIX_MIXER_SIZE))
4858 return -EFAULT;
4859 break;
4860 }
4861 default:
4862 return -EINVAL;
4863 }
4864 return 0;
4865}
4866
Takashi Iwai55e957d2005-11-17 14:52:13 +01004867static struct snd_pcm_ops snd_hdsp_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868 .open = snd_hdsp_playback_open,
4869 .close = snd_hdsp_playback_release,
4870 .ioctl = snd_hdsp_ioctl,
4871 .hw_params = snd_hdsp_hw_params,
4872 .prepare = snd_hdsp_prepare,
4873 .trigger = snd_hdsp_trigger,
4874 .pointer = snd_hdsp_hw_pointer,
4875 .copy = snd_hdsp_playback_copy,
4876 .silence = snd_hdsp_hw_silence,
4877};
4878
Takashi Iwai55e957d2005-11-17 14:52:13 +01004879static struct snd_pcm_ops snd_hdsp_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 .open = snd_hdsp_capture_open,
4881 .close = snd_hdsp_capture_release,
4882 .ioctl = snd_hdsp_ioctl,
4883 .hw_params = snd_hdsp_hw_params,
4884 .prepare = snd_hdsp_prepare,
4885 .trigger = snd_hdsp_trigger,
4886 .pointer = snd_hdsp_hw_pointer,
4887 .copy = snd_hdsp_capture_copy,
4888};
4889
Takashi Iwai92eed662008-02-22 18:35:56 +01004890static int snd_hdsp_create_hwdep(struct snd_card *card, struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004892 struct snd_hwdep *hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893 int err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004894
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895 if ((err = snd_hwdep_new(card, "HDSP hwdep", 0, &hw)) < 0)
4896 return err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004897
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898 hdsp->hwdep = hw;
4899 hw->private_data = hdsp;
4900 strcpy(hw->name, "HDSP hwdep interface");
4901
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902 hw->ops.ioctl = snd_hdsp_hwdep_ioctl;
Andre Schramm42eb9232012-05-07 18:52:51 +02004903 hw->ops.ioctl_compat = snd_hdsp_hwdep_ioctl;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004904
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905 return 0;
4906}
4907
Takashi Iwai55e957d2005-11-17 14:52:13 +01004908static int snd_hdsp_create_pcm(struct snd_card *card, struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004910 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911 int err;
4912
4913 if ((err = snd_pcm_new(card, hdsp->card_name, 0, 1, 1, &pcm)) < 0)
4914 return err;
4915
4916 hdsp->pcm = pcm;
4917 pcm->private_data = hdsp;
4918 strcpy(pcm->name, hdsp->card_name);
4919
4920 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_hdsp_playback_ops);
4921 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_hdsp_capture_ops);
4922
4923 pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
4924
4925 return 0;
4926}
4927
Takashi Iwai55e957d2005-11-17 14:52:13 +01004928static void snd_hdsp_9652_enable_mixer (struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929{
4930 hdsp->control2_register |= HDSP_9652_ENABLE_MIXER;
4931 hdsp_write (hdsp, HDSP_control2Reg, hdsp->control2_register);
4932}
4933
Takashi Iwai55e957d2005-11-17 14:52:13 +01004934static int snd_hdsp_enable_io (struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935{
4936 int i;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004937
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938 if (hdsp_fifo_wait (hdsp, 0, 100)) {
Takashi Iwaia54ba0f2014-02-26 12:05:11 +01004939 dev_err(hdsp->card->dev,
4940 "enable_io fifo_wait failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941 return -EIO;
4942 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004943
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 for (i = 0; i < hdsp->max_channels; ++i) {
4945 hdsp_write (hdsp, HDSP_inputEnable + (4 * i), 1);
4946 hdsp_write (hdsp, HDSP_outputEnable + (4 * i), 1);
4947 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004948
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 return 0;
4950}
4951
Takashi Iwai55e957d2005-11-17 14:52:13 +01004952static void snd_hdsp_initialize_channels(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953{
4954 int status, aebi_channels, aebo_channels;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004955
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 switch (hdsp->io_type) {
4957 case Digiface:
4958 hdsp->card_name = "RME Hammerfall DSP + Digiface";
4959 hdsp->ss_in_channels = hdsp->ss_out_channels = DIGIFACE_SS_CHANNELS;
4960 hdsp->ds_in_channels = hdsp->ds_out_channels = DIGIFACE_DS_CHANNELS;
4961 break;
4962
4963 case H9652:
4964 hdsp->card_name = "RME Hammerfall HDSP 9652";
4965 hdsp->ss_in_channels = hdsp->ss_out_channels = H9652_SS_CHANNELS;
4966 hdsp->ds_in_channels = hdsp->ds_out_channels = H9652_DS_CHANNELS;
4967 break;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004968
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 case H9632:
4970 status = hdsp_read(hdsp, HDSP_statusRegister);
4971 /* HDSP_AEBx bits are low when AEB are connected */
4972 aebi_channels = (status & HDSP_AEBI) ? 0 : 4;
4973 aebo_channels = (status & HDSP_AEBO) ? 0 : 4;
4974 hdsp->card_name = "RME Hammerfall HDSP 9632";
4975 hdsp->ss_in_channels = H9632_SS_CHANNELS+aebi_channels;
4976 hdsp->ds_in_channels = H9632_DS_CHANNELS+aebi_channels;
4977 hdsp->qs_in_channels = H9632_QS_CHANNELS+aebi_channels;
4978 hdsp->ss_out_channels = H9632_SS_CHANNELS+aebo_channels;
4979 hdsp->ds_out_channels = H9632_DS_CHANNELS+aebo_channels;
4980 hdsp->qs_out_channels = H9632_QS_CHANNELS+aebo_channels;
4981 break;
4982
4983 case Multiface:
4984 hdsp->card_name = "RME Hammerfall DSP + Multiface";
4985 hdsp->ss_in_channels = hdsp->ss_out_channels = MULTIFACE_SS_CHANNELS;
4986 hdsp->ds_in_channels = hdsp->ds_out_channels = MULTIFACE_DS_CHANNELS;
4987 break;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004988
Florian Faber28b26e12010-12-01 12:14:47 +01004989 case RPM:
4990 hdsp->card_name = "RME Hammerfall DSP + RPM";
4991 hdsp->ss_in_channels = RPM_CHANNELS-1;
4992 hdsp->ss_out_channels = RPM_CHANNELS;
4993 hdsp->ds_in_channels = RPM_CHANNELS-1;
4994 hdsp->ds_out_channels = RPM_CHANNELS;
4995 break;
4996
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997 default:
4998 /* should never get here */
4999 break;
5000 }
5001}
5002
Takashi Iwai55e957d2005-11-17 14:52:13 +01005003static void snd_hdsp_initialize_midi_flush (struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004{
5005 snd_hdsp_flush_midi_input (hdsp, 0);
5006 snd_hdsp_flush_midi_input (hdsp, 1);
5007}
5008
Takashi Iwai55e957d2005-11-17 14:52:13 +01005009static int snd_hdsp_create_alsa_devices(struct snd_card *card, struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010{
5011 int err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005012
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013 if ((err = snd_hdsp_create_pcm(card, hdsp)) < 0) {
Takashi Iwaia54ba0f2014-02-26 12:05:11 +01005014 dev_err(card->dev,
5015 "Error creating pcm interface\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016 return err;
5017 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005018
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019
5020 if ((err = snd_hdsp_create_midi(card, hdsp, 0)) < 0) {
Takashi Iwaia54ba0f2014-02-26 12:05:11 +01005021 dev_err(card->dev,
5022 "Error creating first midi interface\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023 return err;
5024 }
5025
5026 if (hdsp->io_type == Digiface || hdsp->io_type == H9652) {
5027 if ((err = snd_hdsp_create_midi(card, hdsp, 1)) < 0) {
Takashi Iwaia54ba0f2014-02-26 12:05:11 +01005028 dev_err(card->dev,
5029 "Error creating second midi interface\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030 return err;
5031 }
5032 }
5033
5034 if ((err = snd_hdsp_create_controls(card, hdsp)) < 0) {
Takashi Iwaia54ba0f2014-02-26 12:05:11 +01005035 dev_err(card->dev,
5036 "Error creating ctl interface\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037 return err;
5038 }
5039
5040 snd_hdsp_proc_init(hdsp);
5041
5042 hdsp->system_sample_rate = -1;
5043 hdsp->playback_pid = -1;
5044 hdsp->capture_pid = -1;
5045 hdsp->capture_substream = NULL;
5046 hdsp->playback_substream = NULL;
5047
5048 if ((err = snd_hdsp_set_defaults(hdsp)) < 0) {
Takashi Iwaia54ba0f2014-02-26 12:05:11 +01005049 dev_err(card->dev,
5050 "Error setting default values\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051 return err;
5052 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005053
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054 if (!(hdsp->state & HDSP_InitializationComplete)) {
Clemens Ladischb73c1c12005-09-02 08:49:21 +02005055 strcpy(card->shortname, "Hammerfall DSP");
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005056 sprintf(card->longname, "%s at 0x%lx, irq %d", hdsp->card_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057 hdsp->port, hdsp->irq);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005058
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059 if ((err = snd_card_register(card)) < 0) {
Takashi Iwaia54ba0f2014-02-26 12:05:11 +01005060 dev_err(card->dev,
5061 "error registering card\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062 return err;
5063 }
5064 hdsp->state |= HDSP_InitializationComplete;
5065 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005066
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067 return 0;
5068}
5069
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070/* load firmware via hotplug fw loader */
Takashi Iwai92eed662008-02-22 18:35:56 +01005071static int hdsp_request_fw_loader(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072{
5073 const char *fwfile;
5074 const struct firmware *fw;
5075 int err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005076
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077 if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
5078 return 0;
5079 if (hdsp->io_type == Undefined) {
5080 if ((err = hdsp_get_iobox_version(hdsp)) < 0)
5081 return err;
5082 if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
5083 return 0;
5084 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005085
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086 /* caution: max length of firmware filename is 30! */
5087 switch (hdsp->io_type) {
Florian Faber28b26e12010-12-01 12:14:47 +01005088 case RPM:
5089 fwfile = "rpm_firmware.bin";
5090 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091 case Multiface:
5092 if (hdsp->firmware_rev == 0xa)
5093 fwfile = "multiface_firmware.bin";
5094 else
5095 fwfile = "multiface_firmware_rev11.bin";
5096 break;
5097 case Digiface:
5098 if (hdsp->firmware_rev == 0xa)
5099 fwfile = "digiface_firmware.bin";
5100 else
5101 fwfile = "digiface_firmware_rev11.bin";
5102 break;
5103 default:
Takashi Iwaia54ba0f2014-02-26 12:05:11 +01005104 dev_err(hdsp->card->dev,
5105 "invalid io_type %d\n", hdsp->io_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106 return -EINVAL;
5107 }
5108
5109 if (request_firmware(&fw, fwfile, &hdsp->pci->dev)) {
Takashi Iwaia54ba0f2014-02-26 12:05:11 +01005110 dev_err(hdsp->card->dev,
5111 "cannot load firmware %s\n", fwfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112 return -ENOENT;
5113 }
Takashi Iwai90caaef2012-11-22 16:55:11 +01005114 if (fw->size < HDSP_FIRMWARE_SIZE) {
Takashi Iwaia54ba0f2014-02-26 12:05:11 +01005115 dev_err(hdsp->card->dev,
5116 "too short firmware size %d (expected %d)\n",
Takashi Iwai90caaef2012-11-22 16:55:11 +01005117 (int)fw->size, HDSP_FIRMWARE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118 return -EINVAL;
5119 }
Thomas Charbonnel7679a032005-04-25 11:35:29 +02005120
Takashi Iwai90caaef2012-11-22 16:55:11 +01005121 hdsp->firmware = fw;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005122
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 hdsp->state |= HDSP_FirmwareCached;
5124
5125 if ((err = snd_hdsp_load_firmware_from_cache(hdsp)) < 0)
5126 return err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005127
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128 if (!(hdsp->state & HDSP_InitializationComplete)) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02005129 if ((err = snd_hdsp_enable_io(hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131
5132 if ((err = snd_hdsp_create_hwdep(hdsp->card, hdsp)) < 0) {
Takashi Iwaia54ba0f2014-02-26 12:05:11 +01005133 dev_err(hdsp->card->dev,
5134 "error creating hwdep device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135 return err;
5136 }
5137 snd_hdsp_initialize_channels(hdsp);
5138 snd_hdsp_initialize_midi_flush(hdsp);
5139 if ((err = snd_hdsp_create_alsa_devices(hdsp->card, hdsp)) < 0) {
Takashi Iwaia54ba0f2014-02-26 12:05:11 +01005140 dev_err(hdsp->card->dev,
5141 "error creating alsa devices\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142 return err;
5143 }
5144 }
5145 return 0;
5146}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147
Bill Pembertone23e7a12012-12-06 12:35:10 -05005148static int snd_hdsp_create(struct snd_card *card,
5149 struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150{
5151 struct pci_dev *pci = hdsp->pci;
5152 int err;
5153 int is_9652 = 0;
5154 int is_9632 = 0;
5155
5156 hdsp->irq = -1;
5157 hdsp->state = 0;
5158 hdsp->midi[0].rmidi = NULL;
5159 hdsp->midi[1].rmidi = NULL;
5160 hdsp->midi[0].input = NULL;
5161 hdsp->midi[1].input = NULL;
5162 hdsp->midi[0].output = NULL;
5163 hdsp->midi[1].output = NULL;
5164 hdsp->midi[0].pending = 0;
5165 hdsp->midi[1].pending = 0;
5166 spin_lock_init(&hdsp->midi[0].lock);
5167 spin_lock_init(&hdsp->midi[1].lock);
5168 hdsp->iobase = NULL;
5169 hdsp->control_register = 0;
5170 hdsp->control2_register = 0;
5171 hdsp->io_type = Undefined;
5172 hdsp->max_channels = 26;
5173
5174 hdsp->card = card;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005175
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176 spin_lock_init(&hdsp->lock);
5177
5178 tasklet_init(&hdsp->midi_tasklet, hdsp_midi_tasklet, (unsigned long)hdsp);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005179
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180 pci_read_config_word(hdsp->pci, PCI_CLASS_REVISION, &hdsp->firmware_rev);
5181 hdsp->firmware_rev &= 0xff;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005182
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183 /* From Martin Bjoernsen :
5184 "It is important that the card's latency timer register in
5185 the PCI configuration space is set to a value much larger
5186 than 0 by the computer's BIOS or the driver.
5187 The windows driver always sets this 8 bit register [...]
5188 to its maximum 255 to avoid problems with some computers."
5189 */
5190 pci_write_config_byte(hdsp->pci, PCI_LATENCY_TIMER, 0xFF);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005191
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192 strcpy(card->driver, "H-DSP");
5193 strcpy(card->mixername, "Xilinx FPGA");
5194
Takashi Iwaib0b98112005-10-20 18:29:58 +02005195 if (hdsp->firmware_rev < 0xa)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196 return -ENODEV;
Takashi Iwaib0b98112005-10-20 18:29:58 +02005197 else if (hdsp->firmware_rev < 0x64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198 hdsp->card_name = "RME Hammerfall DSP";
Takashi Iwaib0b98112005-10-20 18:29:58 +02005199 else if (hdsp->firmware_rev < 0x96) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200 hdsp->card_name = "RME HDSP 9652";
5201 is_9652 = 1;
5202 } else {
5203 hdsp->card_name = "RME HDSP 9632";
5204 hdsp->max_channels = 16;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005205 is_9632 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206 }
5207
Takashi Iwaib0b98112005-10-20 18:29:58 +02005208 if ((err = pci_enable_device(pci)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210
5211 pci_set_master(hdsp->pci);
5212
5213 if ((err = pci_request_regions(pci, "hdsp")) < 0)
5214 return err;
5215 hdsp->port = pci_resource_start(pci, 0);
5216 if ((hdsp->iobase = ioremap_nocache(hdsp->port, HDSP_IO_EXTENT)) == NULL) {
Takashi Iwaia54ba0f2014-02-26 12:05:11 +01005217 dev_err(hdsp->card->dev, "unable to remap region 0x%lx-0x%lx\n",
5218 hdsp->port, hdsp->port + HDSP_IO_EXTENT - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219 return -EBUSY;
5220 }
5221
Takashi Iwai437a5a42006-11-21 12:14:23 +01005222 if (request_irq(pci->irq, snd_hdsp_interrupt, IRQF_SHARED,
Takashi Iwai934c2b62011-06-10 16:36:37 +02005223 KBUILD_MODNAME, hdsp)) {
Takashi Iwaia54ba0f2014-02-26 12:05:11 +01005224 dev_err(hdsp->card->dev, "unable to use IRQ %d\n", pci->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225 return -EBUSY;
5226 }
5227
5228 hdsp->irq = pci->irq;
Remy Bruno176546a2006-10-16 12:32:53 +02005229 hdsp->precise_ptr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230 hdsp->use_midi_tasklet = 1;
Remy Brunod7923b22006-10-17 12:41:56 +02005231 hdsp->dds_value = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232
Takashi Iwaib0b98112005-10-20 18:29:58 +02005233 if ((err = snd_hdsp_initialize_memory(hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234 return err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005235
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236 if (!is_9652 && !is_9632) {
Tim Blechmanne588ed82009-02-20 19:30:35 +01005237 /* we wait a maximum of 10 seconds to let freshly
5238 * inserted cardbus cards do their hardware init */
5239 err = hdsp_wait_for_iobox(hdsp, 1000, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240
Tim Blechmann00c9ddd2008-11-09 12:50:52 +01005241 if (err < 0)
5242 return err;
5243
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244 if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
Takashi Iwaib0b98112005-10-20 18:29:58 +02005245 if ((err = hdsp_request_fw_loader(hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246 /* we don't fail as this can happen
5247 if userspace is not ready for
5248 firmware upload
5249 */
Takashi Iwaia54ba0f2014-02-26 12:05:11 +01005250 dev_err(hdsp->card->dev,
5251 "couldn't get firmware from userspace. try using hdsploader\n");
Takashi Iwaib0b98112005-10-20 18:29:58 +02005252 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253 /* init is complete, we return */
5254 return 0;
Tim Blechmann00c9ddd2008-11-09 12:50:52 +01005255 /* we defer initialization */
Takashi Iwaia54ba0f2014-02-26 12:05:11 +01005256 dev_info(hdsp->card->dev,
5257 "card initialization pending : waiting for firmware\n");
Takashi Iwaib0b98112005-10-20 18:29:58 +02005258 if ((err = snd_hdsp_create_hwdep(card, hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260 return 0;
5261 } else {
Takashi Iwaia54ba0f2014-02-26 12:05:11 +01005262 dev_info(hdsp->card->dev,
5263 "Firmware already present, initializing card.\n");
Florian Faber28b26e12010-12-01 12:14:47 +01005264 if (hdsp_read(hdsp, HDSP_status2Register) & HDSP_version2)
5265 hdsp->io_type = RPM;
5266 else if (hdsp_read(hdsp, HDSP_status2Register) & HDSP_version1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267 hdsp->io_type = Multiface;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005268 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269 hdsp->io_type = Digiface;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 }
5271 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005272
Takashi Iwaib0b98112005-10-20 18:29:58 +02005273 if ((err = snd_hdsp_enable_io(hdsp)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274 return err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005275
Takashi Iwaib0b98112005-10-20 18:29:58 +02005276 if (is_9652)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277 hdsp->io_type = H9652;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005278
Takashi Iwaib0b98112005-10-20 18:29:58 +02005279 if (is_9632)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280 hdsp->io_type = H9632;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281
Takashi Iwaib0b98112005-10-20 18:29:58 +02005282 if ((err = snd_hdsp_create_hwdep(card, hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283 return err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005284
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285 snd_hdsp_initialize_channels(hdsp);
5286 snd_hdsp_initialize_midi_flush(hdsp);
5287
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005288 hdsp->state |= HDSP_FirmwareLoaded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289
Takashi Iwaib0b98112005-10-20 18:29:58 +02005290 if ((err = snd_hdsp_create_alsa_devices(card, hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005293 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294}
5295
Takashi Iwai55e957d2005-11-17 14:52:13 +01005296static int snd_hdsp_free(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297{
5298 if (hdsp->port) {
5299 /* stop the audio, and cancel all interrupts */
5300 tasklet_kill(&hdsp->midi_tasklet);
5301 hdsp->control_register &= ~(HDSP_Start|HDSP_AudioInterruptEnable|HDSP_Midi0InterruptEnable|HDSP_Midi1InterruptEnable);
5302 hdsp_write (hdsp, HDSP_controlRegister, hdsp->control_register);
5303 }
5304
5305 if (hdsp->irq >= 0)
5306 free_irq(hdsp->irq, (void *)hdsp);
5307
5308 snd_hdsp_free_buffers(hdsp);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005309
Markus Elfringc2836612014-11-17 13:04:14 +01005310 release_firmware(hdsp->firmware);
Takashi Iwai90caaef2012-11-22 16:55:11 +01005311 vfree(hdsp->fw_uploaded);
5312
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313 if (hdsp->iobase)
5314 iounmap(hdsp->iobase);
5315
5316 if (hdsp->port)
5317 pci_release_regions(hdsp->pci);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005318
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319 pci_disable_device(hdsp->pci);
5320 return 0;
5321}
5322
Takashi Iwai55e957d2005-11-17 14:52:13 +01005323static void snd_hdsp_card_free(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324{
Joe Perches9fe856e2010-09-04 18:52:54 -07005325 struct hdsp *hdsp = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326
5327 if (hdsp)
5328 snd_hdsp_free(hdsp);
5329}
5330
Bill Pembertone23e7a12012-12-06 12:35:10 -05005331static int snd_hdsp_probe(struct pci_dev *pci,
5332 const struct pci_device_id *pci_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333{
5334 static int dev;
Takashi Iwai55e957d2005-11-17 14:52:13 +01005335 struct hdsp *hdsp;
5336 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337 int err;
5338
5339 if (dev >= SNDRV_CARDS)
5340 return -ENODEV;
5341 if (!enable[dev]) {
5342 dev++;
5343 return -ENOENT;
5344 }
5345
Takashi Iwai60c57722014-01-29 14:20:19 +01005346 err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
5347 sizeof(struct hdsp), &card);
Takashi Iwaie58de7b2008-12-28 16:44:30 +01005348 if (err < 0)
5349 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350
Joe Perches9fe856e2010-09-04 18:52:54 -07005351 hdsp = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352 card->private_free = snd_hdsp_card_free;
5353 hdsp->dev = dev;
5354 hdsp->pci = pci;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355
5356 if ((err = snd_hdsp_create(card, hdsp)) < 0) {
5357 snd_card_free(card);
5358 return err;
5359 }
5360
5361 strcpy(card->shortname, "Hammerfall DSP");
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005362 sprintf(card->longname, "%s at 0x%lx, irq %d", hdsp->card_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363 hdsp->port, hdsp->irq);
5364
5365 if ((err = snd_card_register(card)) < 0) {
5366 snd_card_free(card);
5367 return err;
5368 }
5369 pci_set_drvdata(pci, card);
5370 dev++;
5371 return 0;
5372}
5373
Bill Pembertone23e7a12012-12-06 12:35:10 -05005374static void snd_hdsp_remove(struct pci_dev *pci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375{
5376 snd_card_free(pci_get_drvdata(pci));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377}
5378
Takashi Iwaie9f66d92012-04-24 12:25:00 +02005379static struct pci_driver hdsp_driver = {
Takashi Iwai3733e422011-06-10 16:20:20 +02005380 .name = KBUILD_MODNAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381 .id_table = snd_hdsp_ids,
5382 .probe = snd_hdsp_probe,
Bill Pembertone23e7a12012-12-06 12:35:10 -05005383 .remove = snd_hdsp_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384};
5385
Takashi Iwaie9f66d92012-04-24 12:25:00 +02005386module_pci_driver(hdsp_driver);