blob: b8ddbb18f141a5241c5eb881d6a06968e798f87b [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#include <sound/core.h>
33#include <sound/control.h>
34#include <sound/pcm.h>
35#include <sound/info.h>
36#include <sound/asoundef.h>
37#include <sound/rawmidi.h>
38#include <sound/hwdep.h>
39#include <sound/initval.h>
40#include <sound/hdsp.h>
41
42#include <asm/byteorder.h>
43#include <asm/current.h>
44#include <asm/io.h>
45
46static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
47static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
Rusty Russella67ff6a2011-12-15 13:49:36 +103048static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50module_param_array(index, int, NULL, 0444);
51MODULE_PARM_DESC(index, "Index value for RME Hammerfall DSP interface.");
52module_param_array(id, charp, NULL, 0444);
53MODULE_PARM_DESC(id, "ID string for RME Hammerfall DSP interface.");
54module_param_array(enable, bool, NULL, 0444);
55MODULE_PARM_DESC(enable, "Enable/disable specific Hammerfall DSP soundcards.");
56MODULE_AUTHOR("Paul Davis <paul@linuxaudiosystems.com>, Marcus Andersson, Thomas Charbonnel <thomas@undata.org>");
57MODULE_DESCRIPTION("RME Hammerfall DSP");
58MODULE_LICENSE("GPL");
59MODULE_SUPPORTED_DEVICE("{{RME Hammerfall-DSP},"
60 "{RME HDSP-9652},"
61 "{RME HDSP-9632}}");
Florian Faber28b26e12010-12-01 12:14:47 +010062MODULE_FIRMWARE("rpm_firmware.bin");
Clemens Ladisch7e0af292007-05-03 17:59:54 +020063MODULE_FIRMWARE("multiface_firmware.bin");
64MODULE_FIRMWARE("multiface_firmware_rev11.bin");
65MODULE_FIRMWARE("digiface_firmware.bin");
66MODULE_FIRMWARE("digiface_firmware_rev11.bin");
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68#define HDSP_MAX_CHANNELS 26
69#define HDSP_MAX_DS_CHANNELS 14
70#define HDSP_MAX_QS_CHANNELS 8
71#define DIGIFACE_SS_CHANNELS 26
72#define DIGIFACE_DS_CHANNELS 14
73#define MULTIFACE_SS_CHANNELS 18
74#define MULTIFACE_DS_CHANNELS 14
75#define H9652_SS_CHANNELS 26
76#define H9652_DS_CHANNELS 14
77/* This does not include possible Analog Extension Boards
78 AEBs are detected at card initialization
79*/
80#define H9632_SS_CHANNELS 12
81#define H9632_DS_CHANNELS 8
82#define H9632_QS_CHANNELS 4
Florian Faber28b26e12010-12-01 12:14:47 +010083#define RPM_CHANNELS 6
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85/* Write registers. These are defined as byte-offsets from the iobase value.
86 */
87#define HDSP_resetPointer 0
Remy Brunod7923b22006-10-17 12:41:56 +020088#define HDSP_freqReg 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#define HDSP_outputBufferAddress 32
90#define HDSP_inputBufferAddress 36
91#define HDSP_controlRegister 64
92#define HDSP_interruptConfirmation 96
93#define HDSP_outputEnable 128
94#define HDSP_control2Reg 256
95#define HDSP_midiDataOut0 352
96#define HDSP_midiDataOut1 356
97#define HDSP_fifoData 368
98#define HDSP_inputEnable 384
99
100/* Read registers. These are defined as byte-offsets from the iobase value
101 */
102
103#define HDSP_statusRegister 0
104#define HDSP_timecode 128
105#define HDSP_status2Register 192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#define HDSP_midiDataIn0 360
107#define HDSP_midiDataIn1 364
108#define HDSP_midiStatusOut0 384
109#define HDSP_midiStatusOut1 388
110#define HDSP_midiStatusIn0 392
111#define HDSP_midiStatusIn1 396
112#define HDSP_fifoStatus 400
113
114/* the meters are regular i/o-mapped registers, but offset
115 considerably from the rest. the peak registers are reset
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100116 when read; the least-significant 4 bits are full-scale counters;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 the actual peak value is in the most-significant 24 bits.
118*/
119
120#define HDSP_playbackPeakLevel 4096 /* 26 * 32 bit values */
121#define HDSP_inputPeakLevel 4224 /* 26 * 32 bit values */
122#define HDSP_outputPeakLevel 4352 /* (26+2) * 32 bit values */
123#define HDSP_playbackRmsLevel 4612 /* 26 * 64 bit values */
124#define HDSP_inputRmsLevel 4868 /* 26 * 64 bit values */
125
126
127/* This is for H9652 cards
128 Peak values are read downward from the base
129 Rms values are read upward
130 There are rms values for the outputs too
131 26*3 values are read in ss mode
132 14*3 in ds mode, with no gap between values
133*/
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100134#define HDSP_9652_peakBase 7164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135#define HDSP_9652_rmsBase 4096
136
137/* c.f. the hdsp_9632_meters_t struct */
138#define HDSP_9632_metersBase 4096
139
140#define HDSP_IO_EXTENT 7168
141
142/* control2 register bits */
143
144#define HDSP_TMS 0x01
145#define HDSP_TCK 0x02
146#define HDSP_TDI 0x04
147#define HDSP_JTAG 0x08
148#define HDSP_PWDN 0x10
149#define HDSP_PROGRAM 0x020
150#define HDSP_CONFIG_MODE_0 0x040
151#define HDSP_CONFIG_MODE_1 0x080
Adrian Knotha3466862011-10-27 21:57:53 +0200152#define HDSP_VERSION_BIT (0x100 | HDSP_S_LOAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153#define HDSP_BIGENDIAN_MODE 0x200
154#define HDSP_RD_MULTIPLE 0x400
155#define HDSP_9652_ENABLE_MIXER 0x800
156#define HDSP_TDO 0x10000000
157
158#define HDSP_S_PROGRAM (HDSP_PROGRAM|HDSP_CONFIG_MODE_0)
159#define HDSP_S_LOAD (HDSP_PROGRAM|HDSP_CONFIG_MODE_1)
160
161/* Control Register bits */
162
163#define HDSP_Start (1<<0) /* start engine */
164#define HDSP_Latency0 (1<<1) /* buffer size = 2^n where n is defined by Latency{2,1,0} */
165#define HDSP_Latency1 (1<<2) /* [ see above ] */
166#define HDSP_Latency2 (1<<3) /* [ see above ] */
167#define HDSP_ClockModeMaster (1<<4) /* 1=Master, 0=Slave/Autosync */
168#define HDSP_AudioInterruptEnable (1<<5) /* what do you think ? */
169#define HDSP_Frequency0 (1<<6) /* 0=44.1kHz/88.2kHz/176.4kHz 1=48kHz/96kHz/192kHz */
170#define HDSP_Frequency1 (1<<7) /* 0=32kHz/64kHz/128kHz */
171#define HDSP_DoubleSpeed (1<<8) /* 0=normal speed, 1=double speed */
172#define HDSP_SPDIFProfessional (1<<9) /* 0=consumer, 1=professional */
173#define HDSP_SPDIFEmphasis (1<<10) /* 0=none, 1=on */
174#define HDSP_SPDIFNonAudio (1<<11) /* 0=off, 1=on */
175#define HDSP_SPDIFOpticalOut (1<<12) /* 1=use 1st ADAT connector for SPDIF, 0=do not */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100176#define HDSP_SyncRef2 (1<<13)
177#define HDSP_SPDIFInputSelect0 (1<<14)
178#define HDSP_SPDIFInputSelect1 (1<<15)
179#define HDSP_SyncRef0 (1<<16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180#define HDSP_SyncRef1 (1<<17)
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100181#define HDSP_AnalogExtensionBoard (1<<18) /* For H9632 cards */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182#define HDSP_XLRBreakoutCable (1<<20) /* For H9632 cards */
183#define HDSP_Midi0InterruptEnable (1<<22)
184#define HDSP_Midi1InterruptEnable (1<<23)
185#define HDSP_LineOut (1<<24)
186#define HDSP_ADGain0 (1<<25) /* From here : H9632 specific */
187#define HDSP_ADGain1 (1<<26)
188#define HDSP_DAGain0 (1<<27)
189#define HDSP_DAGain1 (1<<28)
190#define HDSP_PhoneGain0 (1<<29)
191#define HDSP_PhoneGain1 (1<<30)
192#define HDSP_QuadSpeed (1<<31)
193
Florian Faber28b26e12010-12-01 12:14:47 +0100194/* RPM uses some of the registers for special purposes */
195#define HDSP_RPM_Inp12 0x04A00
196#define HDSP_RPM_Inp12_Phon_6dB 0x00800 /* Dolby */
197#define HDSP_RPM_Inp12_Phon_0dB 0x00000 /* .. */
198#define HDSP_RPM_Inp12_Phon_n6dB 0x04000 /* inp_0 */
199#define HDSP_RPM_Inp12_Line_0dB 0x04200 /* Dolby+PRO */
200#define HDSP_RPM_Inp12_Line_n6dB 0x00200 /* PRO */
201
202#define HDSP_RPM_Inp34 0x32000
203#define HDSP_RPM_Inp34_Phon_6dB 0x20000 /* SyncRef1 */
204#define HDSP_RPM_Inp34_Phon_0dB 0x00000 /* .. */
205#define HDSP_RPM_Inp34_Phon_n6dB 0x02000 /* SyncRef2 */
206#define HDSP_RPM_Inp34_Line_0dB 0x30000 /* SyncRef1+SyncRef0 */
207#define HDSP_RPM_Inp34_Line_n6dB 0x10000 /* SyncRef0 */
208
209#define HDSP_RPM_Bypass 0x01000
210
211#define HDSP_RPM_Disconnect 0x00001
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213#define HDSP_ADGainMask (HDSP_ADGain0|HDSP_ADGain1)
214#define HDSP_ADGainMinus10dBV HDSP_ADGainMask
215#define HDSP_ADGainPlus4dBu (HDSP_ADGain0)
216#define HDSP_ADGainLowGain 0
217
218#define HDSP_DAGainMask (HDSP_DAGain0|HDSP_DAGain1)
219#define HDSP_DAGainHighGain HDSP_DAGainMask
220#define HDSP_DAGainPlus4dBu (HDSP_DAGain0)
221#define HDSP_DAGainMinus10dBV 0
222
223#define HDSP_PhoneGainMask (HDSP_PhoneGain0|HDSP_PhoneGain1)
224#define HDSP_PhoneGain0dB HDSP_PhoneGainMask
225#define HDSP_PhoneGainMinus6dB (HDSP_PhoneGain0)
226#define HDSP_PhoneGainMinus12dB 0
227
228#define HDSP_LatencyMask (HDSP_Latency0|HDSP_Latency1|HDSP_Latency2)
229#define HDSP_FrequencyMask (HDSP_Frequency0|HDSP_Frequency1|HDSP_DoubleSpeed|HDSP_QuadSpeed)
230
231#define HDSP_SPDIFInputMask (HDSP_SPDIFInputSelect0|HDSP_SPDIFInputSelect1)
232#define HDSP_SPDIFInputADAT1 0
233#define HDSP_SPDIFInputCoaxial (HDSP_SPDIFInputSelect0)
234#define HDSP_SPDIFInputCdrom (HDSP_SPDIFInputSelect1)
235#define HDSP_SPDIFInputAES (HDSP_SPDIFInputSelect0|HDSP_SPDIFInputSelect1)
236
237#define HDSP_SyncRefMask (HDSP_SyncRef0|HDSP_SyncRef1|HDSP_SyncRef2)
238#define HDSP_SyncRef_ADAT1 0
239#define HDSP_SyncRef_ADAT2 (HDSP_SyncRef0)
240#define HDSP_SyncRef_ADAT3 (HDSP_SyncRef1)
241#define HDSP_SyncRef_SPDIF (HDSP_SyncRef0|HDSP_SyncRef1)
242#define HDSP_SyncRef_WORD (HDSP_SyncRef2)
243#define HDSP_SyncRef_ADAT_SYNC (HDSP_SyncRef0|HDSP_SyncRef2)
244
245/* Sample Clock Sources */
246
247#define HDSP_CLOCK_SOURCE_AUTOSYNC 0
248#define HDSP_CLOCK_SOURCE_INTERNAL_32KHZ 1
249#define HDSP_CLOCK_SOURCE_INTERNAL_44_1KHZ 2
250#define HDSP_CLOCK_SOURCE_INTERNAL_48KHZ 3
251#define HDSP_CLOCK_SOURCE_INTERNAL_64KHZ 4
252#define HDSP_CLOCK_SOURCE_INTERNAL_88_2KHZ 5
253#define HDSP_CLOCK_SOURCE_INTERNAL_96KHZ 6
254#define HDSP_CLOCK_SOURCE_INTERNAL_128KHZ 7
255#define HDSP_CLOCK_SOURCE_INTERNAL_176_4KHZ 8
256#define HDSP_CLOCK_SOURCE_INTERNAL_192KHZ 9
257
258/* Preferred sync reference choices - used by "pref_sync_ref" control switch */
259
260#define HDSP_SYNC_FROM_WORD 0
261#define HDSP_SYNC_FROM_SPDIF 1
262#define HDSP_SYNC_FROM_ADAT1 2
263#define HDSP_SYNC_FROM_ADAT_SYNC 3
264#define HDSP_SYNC_FROM_ADAT2 4
265#define HDSP_SYNC_FROM_ADAT3 5
266
267/* SyncCheck status */
268
269#define HDSP_SYNC_CHECK_NO_LOCK 0
270#define HDSP_SYNC_CHECK_LOCK 1
271#define HDSP_SYNC_CHECK_SYNC 2
272
273/* AutoSync references - used by "autosync_ref" control switch */
274
275#define HDSP_AUTOSYNC_FROM_WORD 0
276#define HDSP_AUTOSYNC_FROM_ADAT_SYNC 1
277#define HDSP_AUTOSYNC_FROM_SPDIF 2
278#define HDSP_AUTOSYNC_FROM_NONE 3
279#define HDSP_AUTOSYNC_FROM_ADAT1 4
280#define HDSP_AUTOSYNC_FROM_ADAT2 5
281#define HDSP_AUTOSYNC_FROM_ADAT3 6
282
283/* Possible sources of S/PDIF input */
284
285#define HDSP_SPDIFIN_OPTICAL 0 /* optical (ADAT1) */
286#define HDSP_SPDIFIN_COAXIAL 1 /* coaxial (RCA) */
287#define HDSP_SPDIFIN_INTERNAL 2 /* internal (CDROM) */
288#define HDSP_SPDIFIN_AES 3 /* xlr for H9632 (AES)*/
289
290#define HDSP_Frequency32KHz HDSP_Frequency0
291#define HDSP_Frequency44_1KHz HDSP_Frequency1
292#define HDSP_Frequency48KHz (HDSP_Frequency1|HDSP_Frequency0)
293#define HDSP_Frequency64KHz (HDSP_DoubleSpeed|HDSP_Frequency0)
294#define HDSP_Frequency88_2KHz (HDSP_DoubleSpeed|HDSP_Frequency1)
295#define HDSP_Frequency96KHz (HDSP_DoubleSpeed|HDSP_Frequency1|HDSP_Frequency0)
296/* For H9632 cards */
297#define HDSP_Frequency128KHz (HDSP_QuadSpeed|HDSP_DoubleSpeed|HDSP_Frequency0)
298#define HDSP_Frequency176_4KHz (HDSP_QuadSpeed|HDSP_DoubleSpeed|HDSP_Frequency1)
299#define HDSP_Frequency192KHz (HDSP_QuadSpeed|HDSP_DoubleSpeed|HDSP_Frequency1|HDSP_Frequency0)
Julian Cablee4b60882007-03-19 11:44:40 +0100300/* RME says n = 104857600000000, but in the windows MADI driver, I see:
301 return 104857600000000 / rate; // 100 MHz
302 return 110100480000000 / rate; // 105 MHz
303*/
304#define DDS_NUMERATOR 104857600000000ULL; /* = 2^20 * 10^8 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306#define hdsp_encode_latency(x) (((x)<<1) & HDSP_LatencyMask)
307#define hdsp_decode_latency(x) (((x) & HDSP_LatencyMask)>>1)
308
309#define hdsp_encode_spdif_in(x) (((x)&0x3)<<14)
310#define hdsp_decode_spdif_in(x) (((x)>>14)&0x3)
311
312/* Status Register bits */
313
314#define HDSP_audioIRQPending (1<<0)
315#define HDSP_Lock2 (1<<1) /* this is for Digiface and H9652 */
316#define HDSP_spdifFrequency3 HDSP_Lock2 /* this is for H9632 only */
317#define HDSP_Lock1 (1<<2)
318#define HDSP_Lock0 (1<<3)
319#define HDSP_SPDIFSync (1<<4)
320#define HDSP_TimecodeLock (1<<5)
321#define HDSP_BufferPositionMask 0x000FFC0 /* Bit 6..15 : h/w buffer pointer */
322#define HDSP_Sync2 (1<<16)
323#define HDSP_Sync1 (1<<17)
324#define HDSP_Sync0 (1<<18)
325#define HDSP_DoubleSpeedStatus (1<<19)
326#define HDSP_ConfigError (1<<20)
327#define HDSP_DllError (1<<21)
328#define HDSP_spdifFrequency0 (1<<22)
329#define HDSP_spdifFrequency1 (1<<23)
330#define HDSP_spdifFrequency2 (1<<24)
331#define HDSP_SPDIFErrorFlag (1<<25)
332#define HDSP_BufferID (1<<26)
333#define HDSP_TimecodeSync (1<<27)
334#define HDSP_AEBO (1<<28) /* H9632 specific Analog Extension Boards */
335#define HDSP_AEBI (1<<29) /* 0 = present, 1 = absent */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100336#define HDSP_midi0IRQPending (1<<30)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337#define HDSP_midi1IRQPending (1<<31)
338
339#define HDSP_spdifFrequencyMask (HDSP_spdifFrequency0|HDSP_spdifFrequency1|HDSP_spdifFrequency2)
Remy Bruno47ba97f2008-02-22 17:57:02 +0100340#define HDSP_spdifFrequencyMask_9632 (HDSP_spdifFrequency0|\
341 HDSP_spdifFrequency1|\
342 HDSP_spdifFrequency2|\
343 HDSP_spdifFrequency3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345#define HDSP_spdifFrequency32KHz (HDSP_spdifFrequency0)
346#define HDSP_spdifFrequency44_1KHz (HDSP_spdifFrequency1)
347#define HDSP_spdifFrequency48KHz (HDSP_spdifFrequency0|HDSP_spdifFrequency1)
348
349#define HDSP_spdifFrequency64KHz (HDSP_spdifFrequency2)
350#define HDSP_spdifFrequency88_2KHz (HDSP_spdifFrequency0|HDSP_spdifFrequency2)
351#define HDSP_spdifFrequency96KHz (HDSP_spdifFrequency2|HDSP_spdifFrequency1)
352
353/* This is for H9632 cards */
Remy Bruno47ba97f2008-02-22 17:57:02 +0100354#define HDSP_spdifFrequency128KHz (HDSP_spdifFrequency0|\
355 HDSP_spdifFrequency1|\
356 HDSP_spdifFrequency2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357#define HDSP_spdifFrequency176_4KHz HDSP_spdifFrequency3
358#define HDSP_spdifFrequency192KHz (HDSP_spdifFrequency3|HDSP_spdifFrequency0)
359
360/* Status2 Register bits */
361
362#define HDSP_version0 (1<<0)
363#define HDSP_version1 (1<<1)
364#define HDSP_version2 (1<<2)
365#define HDSP_wc_lock (1<<3)
366#define HDSP_wc_sync (1<<4)
367#define HDSP_inp_freq0 (1<<5)
368#define HDSP_inp_freq1 (1<<6)
369#define HDSP_inp_freq2 (1<<7)
370#define HDSP_SelSyncRef0 (1<<8)
371#define HDSP_SelSyncRef1 (1<<9)
372#define HDSP_SelSyncRef2 (1<<10)
373
374#define HDSP_wc_valid (HDSP_wc_lock|HDSP_wc_sync)
375
376#define HDSP_systemFrequencyMask (HDSP_inp_freq0|HDSP_inp_freq1|HDSP_inp_freq2)
377#define HDSP_systemFrequency32 (HDSP_inp_freq0)
378#define HDSP_systemFrequency44_1 (HDSP_inp_freq1)
379#define HDSP_systemFrequency48 (HDSP_inp_freq0|HDSP_inp_freq1)
380#define HDSP_systemFrequency64 (HDSP_inp_freq2)
381#define HDSP_systemFrequency88_2 (HDSP_inp_freq0|HDSP_inp_freq2)
382#define HDSP_systemFrequency96 (HDSP_inp_freq1|HDSP_inp_freq2)
383/* FIXME : more values for 9632 cards ? */
384
385#define HDSP_SelSyncRefMask (HDSP_SelSyncRef0|HDSP_SelSyncRef1|HDSP_SelSyncRef2)
386#define HDSP_SelSyncRef_ADAT1 0
387#define HDSP_SelSyncRef_ADAT2 (HDSP_SelSyncRef0)
388#define HDSP_SelSyncRef_ADAT3 (HDSP_SelSyncRef1)
389#define HDSP_SelSyncRef_SPDIF (HDSP_SelSyncRef0|HDSP_SelSyncRef1)
390#define HDSP_SelSyncRef_WORD (HDSP_SelSyncRef2)
391#define HDSP_SelSyncRef_ADAT_SYNC (HDSP_SelSyncRef0|HDSP_SelSyncRef2)
392
393/* Card state flags */
394
395#define HDSP_InitializationComplete (1<<0)
396#define HDSP_FirmwareLoaded (1<<1)
397#define HDSP_FirmwareCached (1<<2)
398
399/* FIFO wait times, defined in terms of 1/10ths of msecs */
400
401#define HDSP_LONG_WAIT 5000
402#define HDSP_SHORT_WAIT 30
403
404#define UNITY_GAIN 32768
405#define MINUS_INFINITY_GAIN 0
406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407/* the size of a substream (1 mono data stream) */
408
409#define HDSP_CHANNEL_BUFFER_SAMPLES (16*1024)
410#define HDSP_CHANNEL_BUFFER_BYTES (4*HDSP_CHANNEL_BUFFER_SAMPLES)
411
412/* the size of the area we need to allocate for DMA transfers. the
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100413 size is the same regardless of the number of channels - the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 Multiface still uses the same memory area.
415
416 Note that we allocate 1 more channel than is apparently needed
417 because the h/w seems to write 1 byte beyond the end of the last
418 page. Sigh.
419*/
420
421#define HDSP_DMA_AREA_BYTES ((HDSP_MAX_CHANNELS+1) * HDSP_CHANNEL_BUFFER_BYTES)
422#define HDSP_DMA_AREA_KILOBYTES (HDSP_DMA_AREA_BYTES/1024)
423
Takashi Iwai90caaef2012-11-22 16:55:11 +0100424#define HDSP_FIRMWARE_SIZE (24413 * 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Takashi Iwai55e957d2005-11-17 14:52:13 +0100426struct hdsp_9632_meters {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 u32 input_peak[16];
428 u32 playback_peak[16];
429 u32 output_peak[16];
430 u32 xxx_peak[16];
431 u32 padding[64];
432 u32 input_rms_low[16];
433 u32 playback_rms_low[16];
434 u32 output_rms_low[16];
435 u32 xxx_rms_low[16];
436 u32 input_rms_high[16];
437 u32 playback_rms_high[16];
438 u32 output_rms_high[16];
439 u32 xxx_rms_high[16];
440};
441
Takashi Iwai55e957d2005-11-17 14:52:13 +0100442struct hdsp_midi {
443 struct hdsp *hdsp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 int id;
Takashi Iwai55e957d2005-11-17 14:52:13 +0100445 struct snd_rawmidi *rmidi;
446 struct snd_rawmidi_substream *input;
447 struct snd_rawmidi_substream *output;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 char istimer; /* timer in use */
449 struct timer_list timer;
450 spinlock_t lock;
451 int pending;
452};
453
Takashi Iwai55e957d2005-11-17 14:52:13 +0100454struct hdsp {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 spinlock_t lock;
Takashi Iwai55e957d2005-11-17 14:52:13 +0100456 struct snd_pcm_substream *capture_substream;
457 struct snd_pcm_substream *playback_substream;
458 struct hdsp_midi midi[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 struct tasklet_struct midi_tasklet;
460 int use_midi_tasklet;
461 int precise_ptr;
462 u32 control_register; /* cached value */
463 u32 control2_register; /* cached value */
464 u32 creg_spdif;
465 u32 creg_spdif_stream;
Takashi Iwaie3ea4d82005-07-04 18:12:39 +0200466 int clock_source_locked;
Florian Faber28b26e12010-12-01 12:14:47 +0100467 char *card_name; /* digiface/multiface/rpm */
Takashi Iwai55e957d2005-11-17 14:52:13 +0100468 enum HDSP_IO_Type io_type; /* ditto, but for code use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 unsigned short firmware_rev;
470 unsigned short state; /* stores state bits */
Takashi Iwai90caaef2012-11-22 16:55:11 +0100471 const struct firmware *firmware;
472 u32 *fw_uploaded;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 size_t period_bytes; /* guess what this is */
474 unsigned char max_channels;
475 unsigned char qs_in_channels; /* quad speed mode for H9632 */
476 unsigned char ds_in_channels;
477 unsigned char ss_in_channels; /* different for multiface/digiface */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100478 unsigned char qs_out_channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 unsigned char ds_out_channels;
480 unsigned char ss_out_channels;
481
482 struct snd_dma_buffer capture_dma_buf;
483 struct snd_dma_buffer playback_dma_buf;
484 unsigned char *capture_buffer; /* suitably aligned address */
485 unsigned char *playback_buffer; /* suitably aligned address */
486
487 pid_t capture_pid;
488 pid_t playback_pid;
489 int running;
490 int system_sample_rate;
491 char *channel_map;
492 int dev;
493 int irq;
494 unsigned long port;
495 void __iomem *iobase;
Takashi Iwai55e957d2005-11-17 14:52:13 +0100496 struct snd_card *card;
497 struct snd_pcm *pcm;
498 struct snd_hwdep *hwdep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 struct pci_dev *pci;
Takashi Iwai55e957d2005-11-17 14:52:13 +0100500 struct snd_kcontrol *spdif_ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 unsigned short mixer_matrix[HDSP_MATRIX_MIXER_SIZE];
Remy Brunod7923b22006-10-17 12:41:56 +0200502 unsigned int dds_value; /* last value written to freq register */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503};
504
505/* These tables map the ALSA channels 1..N to the channels that we
506 need to use in order to find the relevant channel buffer. RME
507 refer to this kind of mapping as between "the ADAT channel and
508 the DMA channel." We index it using the logical audio channel,
509 and the value is the DMA channel (i.e. channel buffer number)
510 where the data for that channel can be read/written from/to.
511*/
512
513static char channel_map_df_ss[HDSP_MAX_CHANNELS] = {
514 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
515 18, 19, 20, 21, 22, 23, 24, 25
516};
517
518static char channel_map_mf_ss[HDSP_MAX_CHANNELS] = { /* Multiface */
519 /* Analog */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100520 0, 1, 2, 3, 4, 5, 6, 7,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 /* ADAT 2 */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100522 16, 17, 18, 19, 20, 21, 22, 23,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 /* SPDIF */
524 24, 25,
525 -1, -1, -1, -1, -1, -1, -1, -1
526};
527
528static char channel_map_ds[HDSP_MAX_CHANNELS] = {
529 /* ADAT channels are remapped */
530 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23,
531 /* channels 12 and 13 are S/PDIF */
532 24, 25,
533 /* others don't exist */
534 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
535};
536
537static char channel_map_H9632_ss[HDSP_MAX_CHANNELS] = {
538 /* ADAT channels */
539 0, 1, 2, 3, 4, 5, 6, 7,
540 /* SPDIF */
541 8, 9,
542 /* Analog */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100543 10, 11,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 /* AO4S-192 and AI4S-192 extension boards */
545 12, 13, 14, 15,
546 /* others don't exist */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100547 -1, -1, -1, -1, -1, -1, -1, -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 -1, -1
549};
550
551static char channel_map_H9632_ds[HDSP_MAX_CHANNELS] = {
552 /* ADAT */
553 1, 3, 5, 7,
554 /* SPDIF */
555 8, 9,
556 /* Analog */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100557 10, 11,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 /* AO4S-192 and AI4S-192 extension boards */
559 12, 13, 14, 15,
560 /* others don't exist */
561 -1, -1, -1, -1, -1, -1, -1, -1,
562 -1, -1, -1, -1, -1, -1
563};
564
565static char channel_map_H9632_qs[HDSP_MAX_CHANNELS] = {
566 /* ADAT is disabled in this mode */
567 /* SPDIF */
568 8, 9,
569 /* Analog */
570 10, 11,
571 /* AO4S-192 and AI4S-192 extension boards */
572 12, 13, 14, 15,
573 /* others don't exist */
574 -1, -1, -1, -1, -1, -1, -1, -1,
575 -1, -1, -1, -1, -1, -1, -1, -1,
576 -1, -1
577};
578
579static int snd_hammerfall_get_buffer(struct pci_dev *pci, struct snd_dma_buffer *dmab, size_t size)
580{
581 dmab->dev.type = SNDRV_DMA_TYPE_DEV;
582 dmab->dev.dev = snd_dma_pci_data(pci);
Takashi Iwaib6a96912005-05-30 18:27:03 +0200583 if (snd_dma_get_reserved_buf(dmab, snd_dma_pci_buf_id(pci))) {
584 if (dmab->bytes >= size)
585 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
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 Iwaib6a96912005-05-30 18:27:03 +0200595 if (dmab->area) {
596 dmab->dev.dev = NULL; /* make it anonymous */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 snd_dma_reserve_buf(dmab, snd_dma_pci_buf_id(pci));
Takashi Iwaib6a96912005-05-30 18:27:03 +0200598 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599}
600
601
Alexey Dobriyancebe41d2010-02-06 00:21:03 +0200602static DEFINE_PCI_DEVICE_TABLE(snd_hdsp_ids) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 {
604 .vendor = PCI_VENDOR_ID_XILINX,
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100605 .device = PCI_DEVICE_ID_XILINX_HAMMERFALL_DSP,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 .subvendor = PCI_ANY_ID,
607 .subdevice = PCI_ANY_ID,
608 }, /* RME Hammerfall-DSP */
609 { 0, },
610};
611
612MODULE_DEVICE_TABLE(pci, snd_hdsp_ids);
613
614/* prototypes */
Takashi Iwai55e957d2005-11-17 14:52:13 +0100615static int snd_hdsp_create_alsa_devices(struct snd_card *card, struct hdsp *hdsp);
616static int snd_hdsp_create_pcm(struct snd_card *card, struct hdsp *hdsp);
617static int snd_hdsp_enable_io (struct hdsp *hdsp);
618static void snd_hdsp_initialize_midi_flush (struct hdsp *hdsp);
619static void snd_hdsp_initialize_channels (struct hdsp *hdsp);
620static int hdsp_fifo_wait(struct hdsp *hdsp, int count, int timeout);
621static int hdsp_autosync_ref(struct hdsp *hdsp);
622static int snd_hdsp_set_defaults(struct hdsp *hdsp);
623static void snd_hdsp_9652_enable_mixer (struct hdsp *hdsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Takashi Iwai55e957d2005-11-17 14:52:13 +0100625static int hdsp_playback_to_output_key (struct hdsp *hdsp, int in, int out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
Remy Brunoa3a68c82007-08-31 12:33:54 +0200627 switch (hdsp->io_type) {
628 case Multiface:
629 case Digiface:
Florian Faber28b26e12010-12-01 12:14:47 +0100630 case RPM:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 default:
Andreas Degert192b8e32008-01-16 15:59:48 +0100632 if (hdsp->firmware_rev == 0xa)
633 return (64 * out) + (32 + (in));
634 else
635 return (52 * out) + (26 + (in));
Remy Brunoa3a68c82007-08-31 12:33:54 +0200636 case H9632:
637 return (32 * out) + (16 + (in));
638 case H9652:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 return (52 * out) + (26 + (in));
640 }
641}
642
Takashi Iwai55e957d2005-11-17 14:52:13 +0100643static int hdsp_input_to_output_key (struct hdsp *hdsp, int in, int out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
Remy Brunoa3a68c82007-08-31 12:33:54 +0200645 switch (hdsp->io_type) {
646 case Multiface:
647 case Digiface:
Florian Faber28b26e12010-12-01 12:14:47 +0100648 case RPM:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 default:
Andreas Degert192b8e32008-01-16 15:59:48 +0100650 if (hdsp->firmware_rev == 0xa)
651 return (64 * out) + in;
652 else
653 return (52 * out) + in;
Remy Brunoa3a68c82007-08-31 12:33:54 +0200654 case H9632:
655 return (32 * out) + in;
656 case H9652:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 return (52 * out) + in;
658 }
659}
660
Takashi Iwai55e957d2005-11-17 14:52:13 +0100661static void hdsp_write(struct hdsp *hdsp, int reg, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
663 writel(val, hdsp->iobase + reg);
664}
665
Takashi Iwai55e957d2005-11-17 14:52:13 +0100666static unsigned int hdsp_read(struct hdsp *hdsp, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
668 return readl (hdsp->iobase + reg);
669}
670
Takashi Iwai55e957d2005-11-17 14:52:13 +0100671static int hdsp_check_for_iobox (struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 if (hdsp->io_type == H9652 || hdsp->io_type == H9632) return 0;
674 if (hdsp_read (hdsp, HDSP_statusRegister) & HDSP_ConfigError) {
Florian Faber28b26e12010-12-01 12:14:47 +0100675 snd_printk("Hammerfall-DSP: no IO box connected!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 hdsp->state &= ~HDSP_FirmwareLoaded;
677 return -EIO;
678 }
679 return 0;
Tim Blechmanne588ed82009-02-20 19:30:35 +0100680}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Tim Blechmanne588ed82009-02-20 19:30:35 +0100682static int hdsp_wait_for_iobox(struct hdsp *hdsp, unsigned int loops,
683 unsigned int delay)
684{
685 unsigned int i;
686
687 if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
688 return 0;
689
690 for (i = 0; i != loops; ++i) {
691 if (hdsp_read(hdsp, HDSP_statusRegister) & HDSP_ConfigError)
692 msleep(delay);
693 else {
694 snd_printd("Hammerfall-DSP: iobox found after %ums!\n",
695 i * delay);
696 return 0;
697 }
698 }
699
Florian Faber28b26e12010-12-01 12:14:47 +0100700 snd_printk("Hammerfall-DSP: no IO box connected!\n");
Tim Blechmanne588ed82009-02-20 19:30:35 +0100701 hdsp->state &= ~HDSP_FirmwareLoaded;
702 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
704
Takashi Iwai55e957d2005-11-17 14:52:13 +0100705static int snd_hdsp_load_firmware_from_cache(struct hdsp *hdsp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707 int i;
708 unsigned long flags;
Takashi Iwai90caaef2012-11-22 16:55:11 +0100709 const u32 *cache;
710
711 if (hdsp->fw_uploaded)
712 cache = hdsp->fw_uploaded;
713 else {
714 if (!hdsp->firmware)
715 return -ENODEV;
716 cache = (u32 *)hdsp->firmware->data;
717 if (!cache)
718 return -ENODEV;
719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
721 if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 snd_printk ("Hammerfall-DSP: loading firmware\n");
724
725 hdsp_write (hdsp, HDSP_control2Reg, HDSP_S_PROGRAM);
726 hdsp_write (hdsp, HDSP_fifoData, 0);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 if (hdsp_fifo_wait (hdsp, 0, HDSP_LONG_WAIT)) {
729 snd_printk ("Hammerfall-DSP: timeout waiting for download preparation\n");
730 return -EIO;
731 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 hdsp_write (hdsp, HDSP_control2Reg, HDSP_S_LOAD);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100734
Takashi Iwai90caaef2012-11-22 16:55:11 +0100735 for (i = 0; i < HDSP_FIRMWARE_SIZE / 4; ++i) {
736 hdsp_write(hdsp, HDSP_fifoData, cache[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 if (hdsp_fifo_wait (hdsp, 127, HDSP_LONG_WAIT)) {
738 snd_printk ("Hammerfall-DSP: timeout during firmware loading\n");
739 return -EIO;
740 }
741 }
742
Takashi Iwaib0b981192005-10-20 18:29:58 +0200743 ssleep(3);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 if (hdsp_fifo_wait (hdsp, 0, HDSP_LONG_WAIT)) {
746 snd_printk ("Hammerfall-DSP: timeout at end of firmware loading\n");
747 return -EIO;
748 }
749
750#ifdef SNDRV_BIG_ENDIAN
751 hdsp->control2_register = HDSP_BIGENDIAN_MODE;
752#else
753 hdsp->control2_register = 0;
754#endif
755 hdsp_write (hdsp, HDSP_control2Reg, hdsp->control2_register);
756 snd_printk ("Hammerfall-DSP: finished firmware loading\n");
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 }
759 if (hdsp->state & HDSP_InitializationComplete) {
Takashi Iwaib0b981192005-10-20 18:29:58 +0200760 snd_printk(KERN_INFO "Hammerfall-DSP: firmware loaded from cache, restoring defaults\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 spin_lock_irqsave(&hdsp->lock, flags);
762 snd_hdsp_set_defaults(hdsp);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100763 spin_unlock_irqrestore(&hdsp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 hdsp->state |= HDSP_FirmwareLoaded;
767
768 return 0;
769}
770
Takashi Iwai55e957d2005-11-17 14:52:13 +0100771static int hdsp_get_iobox_version (struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
773 if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 hdsp_write (hdsp, HDSP_control2Reg, HDSP_PROGRAM);
776 hdsp_write (hdsp, HDSP_fifoData, 0);
Takashi Iwaib0b981192005-10-20 18:29:58 +0200777 if (hdsp_fifo_wait (hdsp, 0, HDSP_SHORT_WAIT) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 hdsp_write (hdsp, HDSP_control2Reg, HDSP_S_LOAD);
781 hdsp_write (hdsp, HDSP_fifoData, 0);
782
Florian Faber28b26e12010-12-01 12:14:47 +0100783 if (hdsp_fifo_wait(hdsp, 0, HDSP_SHORT_WAIT)) {
784 hdsp_write(hdsp, HDSP_control2Reg, HDSP_VERSION_BIT);
785 hdsp_write(hdsp, HDSP_control2Reg, HDSP_S_LOAD);
786 if (hdsp_fifo_wait(hdsp, 0, HDSP_SHORT_WAIT))
787 hdsp->io_type = RPM;
788 else
789 hdsp->io_type = Multiface;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 } else {
791 hdsp->io_type = Digiface;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100792 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 } else {
794 /* firmware was already loaded, get iobox type */
Florian Faber28b26e12010-12-01 12:14:47 +0100795 if (hdsp_read(hdsp, HDSP_status2Register) & HDSP_version2)
796 hdsp->io_type = RPM;
797 else if (hdsp_read(hdsp, HDSP_status2Register) & HDSP_version1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 hdsp->io_type = Multiface;
Takashi Iwaib0b981192005-10-20 18:29:58 +0200799 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 hdsp->io_type = Digiface;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 }
802 return 0;
803}
804
805
Takashi Iwai92eed662008-02-22 18:35:56 +0100806static int hdsp_request_fw_loader(struct hdsp *hdsp);
Takashi Iwai311e70a2006-09-06 12:13:37 +0200807
808static int hdsp_check_for_firmware (struct hdsp *hdsp, int load_on_demand)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
Takashi Iwai311e70a2006-09-06 12:13:37 +0200810 if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
811 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 hdsp->state &= ~HDSP_FirmwareLoaded;
Takashi Iwai311e70a2006-09-06 12:13:37 +0200814 if (! load_on_demand)
Takashi Iwaib0b981192005-10-20 18:29:58 +0200815 return -EIO;
Takashi Iwai311e70a2006-09-06 12:13:37 +0200816 snd_printk(KERN_ERR "Hammerfall-DSP: firmware not present.\n");
Takashi Iwaib0b981192005-10-20 18:29:58 +0200817 /* try to load firmware */
Takashi Iwai311e70a2006-09-06 12:13:37 +0200818 if (! (hdsp->state & HDSP_FirmwareCached)) {
Takashi Iwai311e70a2006-09-06 12:13:37 +0200819 if (! hdsp_request_fw_loader(hdsp))
820 return 0;
Takashi Iwai311e70a2006-09-06 12:13:37 +0200821 snd_printk(KERN_ERR
822 "Hammerfall-DSP: No firmware loaded nor "
823 "cached, please upload firmware.\n");
824 return -EIO;
Takashi Iwaib0b981192005-10-20 18:29:58 +0200825 }
Takashi Iwai311e70a2006-09-06 12:13:37 +0200826 if (snd_hdsp_load_firmware_from_cache(hdsp) != 0) {
827 snd_printk(KERN_ERR
828 "Hammerfall-DSP: Firmware loading from "
829 "cache failed, please upload manually.\n");
830 return -EIO;
831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 }
833 return 0;
834}
835
836
Takashi Iwai55e957d2005-11-17 14:52:13 +0100837static int hdsp_fifo_wait(struct hdsp *hdsp, int count, int timeout)
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100838{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 int i;
840
841 /* the fifoStatus registers reports on how many words
842 are available in the command FIFO.
843 */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 for (i = 0; i < timeout; i++) {
846
847 if ((int)(hdsp_read (hdsp, HDSP_fifoStatus) & 0xff) <= count)
848 return 0;
849
850 /* not very friendly, but we only do this during a firmware
851 load and changing the mixer, so we just put up with it.
852 */
853
854 udelay (100);
855 }
856
857 snd_printk ("Hammerfall-DSP: wait for FIFO status <= %d failed after %d iterations\n",
858 count, timeout);
859 return -1;
860}
861
Takashi Iwai55e957d2005-11-17 14:52:13 +0100862static int hdsp_read_gain (struct hdsp *hdsp, unsigned int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
Takashi Iwaib0b981192005-10-20 18:29:58 +0200864 if (addr >= HDSP_MATRIX_MIXER_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 return 0;
Takashi Iwaib0b981192005-10-20 18:29:58 +0200866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 return hdsp->mixer_matrix[addr];
868}
869
Takashi Iwai55e957d2005-11-17 14:52:13 +0100870static int hdsp_write_gain(struct hdsp *hdsp, unsigned int addr, unsigned short data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
872 unsigned int ad;
873
874 if (addr >= HDSP_MATRIX_MIXER_SIZE)
875 return -1;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 if (hdsp->io_type == H9652 || hdsp->io_type == H9632) {
878
879 /* from martin bjornsen:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 "You can only write dwords to the
882 mixer memory which contain two
883 mixer values in the low and high
884 word. So if you want to change
885 value 0 you have to read value 1
886 from the cache and write both to
887 the first dword in the mixer
888 memory."
889 */
890
Takashi Iwaib0b981192005-10-20 18:29:58 +0200891 if (hdsp->io_type == H9632 && addr >= 512)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Takashi Iwaib0b981192005-10-20 18:29:58 +0200894 if (hdsp->io_type == H9652 && addr >= 1352)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
897 hdsp->mixer_matrix[addr] = data;
898
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 /* `addr' addresses a 16-bit wide address, but
901 the address space accessed via hdsp_write
902 uses byte offsets. put another way, addr
903 varies from 0 to 1351, but to access the
904 corresponding memory location, we need
905 to access 0 to 2703 ...
906 */
907 ad = addr/2;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100908
909 hdsp_write (hdsp, 4096 + (ad*4),
910 (hdsp->mixer_matrix[(addr&0x7fe)+1] << 16) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 hdsp->mixer_matrix[addr&0x7fe]);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 return 0;
914
915 } else {
916
917 ad = (addr << 16) + data;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100918
Takashi Iwaib0b981192005-10-20 18:29:58 +0200919 if (hdsp_fifo_wait(hdsp, 127, HDSP_LONG_WAIT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
922 hdsp_write (hdsp, HDSP_fifoData, ad);
923 hdsp->mixer_matrix[addr] = data;
924
925 }
926
927 return 0;
928}
929
Takashi Iwai55e957d2005-11-17 14:52:13 +0100930static int snd_hdsp_use_is_exclusive(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931{
932 unsigned long flags;
933 int ret = 1;
934
935 spin_lock_irqsave(&hdsp->lock, flags);
936 if ((hdsp->playback_pid != hdsp->capture_pid) &&
Takashi Iwaib0b981192005-10-20 18:29:58 +0200937 (hdsp->playback_pid >= 0) && (hdsp->capture_pid >= 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 spin_unlock_irqrestore(&hdsp->lock, flags);
940 return ret;
941}
942
Takashi Iwai55e957d2005-11-17 14:52:13 +0100943static int hdsp_spdif_sample_rate(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944{
945 unsigned int status = hdsp_read(hdsp, HDSP_statusRegister);
946 unsigned int rate_bits = (status & HDSP_spdifFrequencyMask);
947
Remy Bruno47ba97f2008-02-22 17:57:02 +0100948 /* For the 9632, the mask is different */
949 if (hdsp->io_type == H9632)
950 rate_bits = (status & HDSP_spdifFrequencyMask_9632);
951
Takashi Iwaib0b981192005-10-20 18:29:58 +0200952 if (status & HDSP_SPDIFErrorFlag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 return 0;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 switch (rate_bits) {
956 case HDSP_spdifFrequency32KHz: return 32000;
957 case HDSP_spdifFrequency44_1KHz: return 44100;
958 case HDSP_spdifFrequency48KHz: return 48000;
959 case HDSP_spdifFrequency64KHz: return 64000;
960 case HDSP_spdifFrequency88_2KHz: return 88200;
961 case HDSP_spdifFrequency96KHz: return 96000;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100962 case HDSP_spdifFrequency128KHz:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 if (hdsp->io_type == H9632) return 128000;
964 break;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100965 case HDSP_spdifFrequency176_4KHz:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 if (hdsp->io_type == H9632) return 176400;
967 break;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +0100968 case HDSP_spdifFrequency192KHz:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 if (hdsp->io_type == H9632) return 192000;
970 break;
971 default:
972 break;
973 }
974 snd_printk ("Hammerfall-DSP: unknown spdif frequency status; bits = 0x%x, status = 0x%x\n", rate_bits, status);
975 return 0;
976}
977
Remy Bruno47ba97f2008-02-22 17:57:02 +0100978static int hdsp_external_sample_rate(struct hdsp *hdsp)
979{
980 unsigned int status2 = hdsp_read(hdsp, HDSP_status2Register);
981 unsigned int rate_bits = status2 & HDSP_systemFrequencyMask;
982
983 /* For the 9632 card, there seems to be no bit for indicating external
984 * sample rate greater than 96kHz. The card reports the corresponding
985 * single speed. So the best means seems to get spdif rate when
986 * autosync reference is spdif */
987 if (hdsp->io_type == H9632 &&
988 hdsp_autosync_ref(hdsp) == HDSP_AUTOSYNC_FROM_SPDIF)
989 return hdsp_spdif_sample_rate(hdsp);
990
991 switch (rate_bits) {
992 case HDSP_systemFrequency32: return 32000;
993 case HDSP_systemFrequency44_1: return 44100;
994 case HDSP_systemFrequency48: return 48000;
995 case HDSP_systemFrequency64: return 64000;
996 case HDSP_systemFrequency88_2: return 88200;
997 case HDSP_systemFrequency96: return 96000;
998 default:
999 return 0;
1000 }
1001}
1002
Takashi Iwai55e957d2005-11-17 14:52:13 +01001003static void hdsp_compute_period_size(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
1005 hdsp->period_bytes = 1 << ((hdsp_decode_latency(hdsp->control_register) + 8));
1006}
1007
Takashi Iwai55e957d2005-11-17 14:52:13 +01001008static snd_pcm_uframes_t hdsp_hw_pointer(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
1010 int position;
1011
1012 position = hdsp_read(hdsp, HDSP_statusRegister);
1013
Takashi Iwaib0b981192005-10-20 18:29:58 +02001014 if (!hdsp->precise_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 return (position & HDSP_BufferID) ? (hdsp->period_bytes / 4) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
1017 position &= HDSP_BufferPositionMask;
1018 position /= 4;
1019 position &= (hdsp->period_bytes/2) - 1;
1020 return position;
1021}
1022
Takashi Iwai55e957d2005-11-17 14:52:13 +01001023static void hdsp_reset_hw_pointer(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
1025 hdsp_write (hdsp, HDSP_resetPointer, 0);
Remy Brunod7923b22006-10-17 12:41:56 +02001026 if (hdsp->io_type == H9632 && hdsp->firmware_rev >= 152)
1027 /* HDSP_resetPointer = HDSP_freqReg, which is strange and
1028 * requires (?) to write again DDS value after a reset pointer
1029 * (at least, it works like this) */
1030 hdsp_write (hdsp, HDSP_freqReg, hdsp->dds_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031}
1032
Takashi Iwai55e957d2005-11-17 14:52:13 +01001033static void hdsp_start_audio(struct hdsp *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
1035 s->control_register |= (HDSP_AudioInterruptEnable | HDSP_Start);
1036 hdsp_write(s, HDSP_controlRegister, s->control_register);
1037}
1038
Takashi Iwai55e957d2005-11-17 14:52:13 +01001039static void hdsp_stop_audio(struct hdsp *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
1041 s->control_register &= ~(HDSP_Start | HDSP_AudioInterruptEnable);
1042 hdsp_write(s, HDSP_controlRegister, s->control_register);
1043}
1044
Takashi Iwai55e957d2005-11-17 14:52:13 +01001045static void hdsp_silence_playback(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
1047 memset(hdsp->playback_buffer, 0, HDSP_DMA_AREA_BYTES);
1048}
1049
Takashi Iwai55e957d2005-11-17 14:52:13 +01001050static int hdsp_set_interrupt_interval(struct hdsp *s, unsigned int frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051{
1052 int n;
1053
1054 spin_lock_irq(&s->lock);
1055
1056 frames >>= 7;
1057 n = 0;
1058 while (frames) {
1059 n++;
1060 frames >>= 1;
1061 }
1062
1063 s->control_register &= ~HDSP_LatencyMask;
1064 s->control_register |= hdsp_encode_latency(n);
1065
1066 hdsp_write(s, HDSP_controlRegister, s->control_register);
1067
1068 hdsp_compute_period_size(s);
1069
1070 spin_unlock_irq(&s->lock);
1071
1072 return 0;
1073}
1074
Remy Brunod7923b22006-10-17 12:41:56 +02001075static void hdsp_set_dds_value(struct hdsp *hdsp, int rate)
1076{
1077 u64 n;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001078
Remy Brunod7923b22006-10-17 12:41:56 +02001079 if (rate >= 112000)
1080 rate /= 4;
1081 else if (rate >= 56000)
1082 rate /= 2;
1083
Julian Cablee4b60882007-03-19 11:44:40 +01001084 n = DDS_NUMERATOR;
Takashi Iwai3f7440a2009-06-05 17:40:04 +02001085 n = div_u64(n, rate);
Remy Brunod7923b22006-10-17 12:41:56 +02001086 /* n should be less than 2^32 for being written to FREQ register */
Takashi Iwaida3cec32008-08-08 17:12:14 +02001087 snd_BUG_ON(n >> 32);
Remy Brunod7923b22006-10-17 12:41:56 +02001088 /* HDSP_freqReg and HDSP_resetPointer are the same, so keep the DDS
1089 value to write it after a reset */
1090 hdsp->dds_value = n;
1091 hdsp_write(hdsp, HDSP_freqReg, hdsp->dds_value);
1092}
1093
Takashi Iwai55e957d2005-11-17 14:52:13 +01001094static int hdsp_set_rate(struct hdsp *hdsp, int rate, int called_internally)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095{
1096 int reject_if_open = 0;
1097 int current_rate;
1098 int rate_bits;
1099
1100 /* ASSUMPTION: hdsp->lock is either held, or
1101 there is no need for it (e.g. during module
1102 initialization).
1103 */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001104
1105 if (!(hdsp->control_register & HDSP_ClockModeMaster)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 if (called_internally) {
1107 /* request from ctl or card initialization */
Takashi Iwaib0b981192005-10-20 18:29:58 +02001108 snd_printk(KERN_ERR "Hammerfall-DSP: device is not running as a clock master: cannot set sample rate.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 return -1;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001110 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 /* hw_param request while in AutoSync mode */
1112 int external_freq = hdsp_external_sample_rate(hdsp);
1113 int spdif_freq = hdsp_spdif_sample_rate(hdsp);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001114
Takashi Iwaib0b981192005-10-20 18:29:58 +02001115 if ((spdif_freq == external_freq*2) && (hdsp_autosync_ref(hdsp) >= HDSP_AUTOSYNC_FROM_ADAT1))
1116 snd_printk(KERN_INFO "Hammerfall-DSP: Detected ADAT in double speed mode\n");
1117 else if (hdsp->io_type == H9632 && (spdif_freq == external_freq*4) && (hdsp_autosync_ref(hdsp) >= HDSP_AUTOSYNC_FROM_ADAT1))
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001118 snd_printk(KERN_INFO "Hammerfall-DSP: Detected ADAT in quad speed mode\n");
Takashi Iwaib0b981192005-10-20 18:29:58 +02001119 else if (rate != external_freq) {
1120 snd_printk(KERN_INFO "Hammerfall-DSP: No AutoSync source for requested rate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 return -1;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001122 }
1123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 }
1125
1126 current_rate = hdsp->system_sample_rate;
1127
1128 /* Changing from a "single speed" to a "double speed" rate is
1129 not allowed if any substreams are open. This is because
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001130 such a change causes a shift in the location of
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 the DMA buffers and a reduction in the number of available
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001132 buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
1134 Note that a similar but essentially insoluble problem
1135 exists for externally-driven rate changes. All we can do
1136 is to flag rate changes in the read/write routines. */
1137
Takashi Iwaib0b981192005-10-20 18:29:58 +02001138 if (rate > 96000 && hdsp->io_type != H9632)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 return -EINVAL;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 switch (rate) {
1142 case 32000:
Takashi Iwaib0b981192005-10-20 18:29:58 +02001143 if (current_rate > 48000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 rate_bits = HDSP_Frequency32KHz;
1146 break;
1147 case 44100:
Takashi Iwaib0b981192005-10-20 18:29:58 +02001148 if (current_rate > 48000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 rate_bits = HDSP_Frequency44_1KHz;
1151 break;
1152 case 48000:
Takashi Iwaib0b981192005-10-20 18:29:58 +02001153 if (current_rate > 48000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 rate_bits = HDSP_Frequency48KHz;
1156 break;
1157 case 64000:
Takashi Iwaib0b981192005-10-20 18:29:58 +02001158 if (current_rate <= 48000 || current_rate > 96000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 rate_bits = HDSP_Frequency64KHz;
1161 break;
1162 case 88200:
Takashi Iwaib0b981192005-10-20 18:29:58 +02001163 if (current_rate <= 48000 || current_rate > 96000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 rate_bits = HDSP_Frequency88_2KHz;
1166 break;
1167 case 96000:
Takashi Iwaib0b981192005-10-20 18:29:58 +02001168 if (current_rate <= 48000 || current_rate > 96000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 rate_bits = HDSP_Frequency96KHz;
1171 break;
1172 case 128000:
Takashi Iwaib0b981192005-10-20 18:29:58 +02001173 if (current_rate < 128000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 rate_bits = HDSP_Frequency128KHz;
1176 break;
1177 case 176400:
Takashi Iwaib0b981192005-10-20 18:29:58 +02001178 if (current_rate < 128000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 rate_bits = HDSP_Frequency176_4KHz;
1181 break;
1182 case 192000:
Takashi Iwaib0b981192005-10-20 18:29:58 +02001183 if (current_rate < 128000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 reject_if_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 rate_bits = HDSP_Frequency192KHz;
1186 break;
1187 default:
1188 return -EINVAL;
1189 }
1190
1191 if (reject_if_open && (hdsp->capture_pid >= 0 || hdsp->playback_pid >= 0)) {
1192 snd_printk ("Hammerfall-DSP: cannot change speed mode (capture PID = %d, playback PID = %d)\n",
1193 hdsp->capture_pid,
1194 hdsp->playback_pid);
1195 return -EBUSY;
1196 }
1197
1198 hdsp->control_register &= ~HDSP_FrequencyMask;
1199 hdsp->control_register |= rate_bits;
1200 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1201
Remy Brunod7923b22006-10-17 12:41:56 +02001202 /* For HDSP9632 rev 152, need to set DDS value in FREQ register */
1203 if (hdsp->io_type == H9632 && hdsp->firmware_rev >= 152)
1204 hdsp_set_dds_value(hdsp, rate);
1205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 if (rate >= 128000) {
1207 hdsp->channel_map = channel_map_H9632_qs;
1208 } else if (rate > 48000) {
Takashi Iwaib0b981192005-10-20 18:29:58 +02001209 if (hdsp->io_type == H9632)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 hdsp->channel_map = channel_map_H9632_ds;
Takashi Iwaib0b981192005-10-20 18:29:58 +02001211 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 hdsp->channel_map = channel_map_ds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 } else {
1214 switch (hdsp->io_type) {
Florian Faber28b26e12010-12-01 12:14:47 +01001215 case RPM:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 case Multiface:
1217 hdsp->channel_map = channel_map_mf_ss;
1218 break;
1219 case Digiface:
1220 case H9652:
1221 hdsp->channel_map = channel_map_df_ss;
1222 break;
1223 case H9632:
1224 hdsp->channel_map = channel_map_H9632_ss;
1225 break;
1226 default:
1227 /* should never happen */
1228 break;
1229 }
1230 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 hdsp->system_sample_rate = rate;
1233
1234 return 0;
1235}
1236
1237/*----------------------------------------------------------------------------
1238 MIDI
1239 ----------------------------------------------------------------------------*/
1240
Takashi Iwai55e957d2005-11-17 14:52:13 +01001241static unsigned char snd_hdsp_midi_read_byte (struct hdsp *hdsp, int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242{
1243 /* the hardware already does the relevant bit-mask with 0xff */
Takashi Iwaib0b981192005-10-20 18:29:58 +02001244 if (id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 return hdsp_read(hdsp, HDSP_midiDataIn1);
Takashi Iwaib0b981192005-10-20 18:29:58 +02001246 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 return hdsp_read(hdsp, HDSP_midiDataIn0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248}
1249
Takashi Iwai55e957d2005-11-17 14:52:13 +01001250static void snd_hdsp_midi_write_byte (struct hdsp *hdsp, int id, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251{
1252 /* the hardware already does the relevant bit-mask with 0xff */
Takashi Iwaib0b981192005-10-20 18:29:58 +02001253 if (id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 hdsp_write(hdsp, HDSP_midiDataOut1, val);
Takashi Iwaib0b981192005-10-20 18:29:58 +02001255 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 hdsp_write(hdsp, HDSP_midiDataOut0, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257}
1258
Takashi Iwai55e957d2005-11-17 14:52:13 +01001259static int snd_hdsp_midi_input_available (struct hdsp *hdsp, int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260{
Takashi Iwaib0b981192005-10-20 18:29:58 +02001261 if (id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 return (hdsp_read(hdsp, HDSP_midiStatusIn1) & 0xff);
Takashi Iwaib0b981192005-10-20 18:29:58 +02001263 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 return (hdsp_read(hdsp, HDSP_midiStatusIn0) & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265}
1266
Takashi Iwai55e957d2005-11-17 14:52:13 +01001267static int snd_hdsp_midi_output_possible (struct hdsp *hdsp, int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
1269 int fifo_bytes_used;
1270
Takashi Iwaib0b981192005-10-20 18:29:58 +02001271 if (id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 fifo_bytes_used = hdsp_read(hdsp, HDSP_midiStatusOut1) & 0xff;
Takashi Iwaib0b981192005-10-20 18:29:58 +02001273 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 fifo_bytes_used = hdsp_read(hdsp, HDSP_midiStatusOut0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Takashi Iwaib0b981192005-10-20 18:29:58 +02001276 if (fifo_bytes_used < 128)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 return 128 - fifo_bytes_used;
Takashi Iwaib0b981192005-10-20 18:29:58 +02001278 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280}
1281
Takashi Iwai55e957d2005-11-17 14:52:13 +01001282static void snd_hdsp_flush_midi_input (struct hdsp *hdsp, int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283{
Takashi Iwaib0b981192005-10-20 18:29:58 +02001284 while (snd_hdsp_midi_input_available (hdsp, id))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 snd_hdsp_midi_read_byte (hdsp, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286}
1287
Takashi Iwai55e957d2005-11-17 14:52:13 +01001288static int snd_hdsp_midi_output_write (struct hdsp_midi *hmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289{
1290 unsigned long flags;
1291 int n_pending;
1292 int to_write;
1293 int i;
1294 unsigned char buf[128];
1295
1296 /* Output is not interrupt driven */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 spin_lock_irqsave (&hmidi->lock, flags);
1299 if (hmidi->output) {
1300 if (!snd_rawmidi_transmit_empty (hmidi->output)) {
1301 if ((n_pending = snd_hdsp_midi_output_possible (hmidi->hdsp, hmidi->id)) > 0) {
1302 if (n_pending > (int)sizeof (buf))
1303 n_pending = sizeof (buf);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 if ((to_write = snd_rawmidi_transmit (hmidi->output, buf, n_pending)) > 0) {
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001306 for (i = 0; i < to_write; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 snd_hdsp_midi_write_byte (hmidi->hdsp, hmidi->id, buf[i]);
1308 }
1309 }
1310 }
1311 }
1312 spin_unlock_irqrestore (&hmidi->lock, flags);
1313 return 0;
1314}
1315
Takashi Iwai55e957d2005-11-17 14:52:13 +01001316static int snd_hdsp_midi_input_read (struct hdsp_midi *hmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317{
1318 unsigned char buf[128]; /* this buffer is designed to match the MIDI input FIFO size */
1319 unsigned long flags;
1320 int n_pending;
1321 int i;
1322
1323 spin_lock_irqsave (&hmidi->lock, flags);
1324 if ((n_pending = snd_hdsp_midi_input_available (hmidi->hdsp, hmidi->id)) > 0) {
1325 if (hmidi->input) {
Takashi Iwaib0b981192005-10-20 18:29:58 +02001326 if (n_pending > (int)sizeof (buf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 n_pending = sizeof (buf);
Takashi Iwaib0b981192005-10-20 18:29:58 +02001328 for (i = 0; i < n_pending; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 buf[i] = snd_hdsp_midi_read_byte (hmidi->hdsp, hmidi->id);
Takashi Iwaib0b981192005-10-20 18:29:58 +02001330 if (n_pending)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 snd_rawmidi_receive (hmidi->input, buf, n_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 } else {
1333 /* flush the MIDI input FIFO */
Takashi Iwaib0b981192005-10-20 18:29:58 +02001334 while (--n_pending)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 snd_hdsp_midi_read_byte (hmidi->hdsp, hmidi->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 }
1337 }
1338 hmidi->pending = 0;
Takashi Iwaib0b981192005-10-20 18:29:58 +02001339 if (hmidi->id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 hmidi->hdsp->control_register |= HDSP_Midi1InterruptEnable;
Takashi Iwaib0b981192005-10-20 18:29:58 +02001341 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 hmidi->hdsp->control_register |= HDSP_Midi0InterruptEnable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 hdsp_write(hmidi->hdsp, HDSP_controlRegister, hmidi->hdsp->control_register);
1344 spin_unlock_irqrestore (&hmidi->lock, flags);
1345 return snd_hdsp_midi_output_write (hmidi);
1346}
1347
Takashi Iwai55e957d2005-11-17 14:52:13 +01001348static void snd_hdsp_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001350 struct hdsp *hdsp;
1351 struct hdsp_midi *hmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 unsigned long flags;
1353 u32 ie;
1354
Takashi Iwai55e957d2005-11-17 14:52:13 +01001355 hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 hdsp = hmidi->hdsp;
1357 ie = hmidi->id ? HDSP_Midi1InterruptEnable : HDSP_Midi0InterruptEnable;
1358 spin_lock_irqsave (&hdsp->lock, flags);
1359 if (up) {
1360 if (!(hdsp->control_register & ie)) {
1361 snd_hdsp_flush_midi_input (hdsp, hmidi->id);
1362 hdsp->control_register |= ie;
1363 }
1364 } else {
1365 hdsp->control_register &= ~ie;
1366 tasklet_kill(&hdsp->midi_tasklet);
1367 }
1368
1369 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1370 spin_unlock_irqrestore (&hdsp->lock, flags);
1371}
1372
1373static void snd_hdsp_midi_output_timer(unsigned long data)
1374{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001375 struct hdsp_midi *hmidi = (struct hdsp_midi *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 unsigned long flags;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001377
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 snd_hdsp_midi_output_write(hmidi);
1379 spin_lock_irqsave (&hmidi->lock, flags);
1380
1381 /* this does not bump hmidi->istimer, because the
1382 kernel automatically removed the timer when it
1383 expired, and we are now adding it back, thus
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001384 leaving istimer wherever it was set before.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 */
1386
1387 if (hmidi->istimer) {
1388 hmidi->timer.expires = 1 + jiffies;
1389 add_timer(&hmidi->timer);
1390 }
1391
1392 spin_unlock_irqrestore (&hmidi->lock, flags);
1393}
1394
Takashi Iwai55e957d2005-11-17 14:52:13 +01001395static void snd_hdsp_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001397 struct hdsp_midi *hmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 unsigned long flags;
1399
Takashi Iwai55e957d2005-11-17 14:52:13 +01001400 hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 spin_lock_irqsave (&hmidi->lock, flags);
1402 if (up) {
1403 if (!hmidi->istimer) {
1404 init_timer(&hmidi->timer);
1405 hmidi->timer.function = snd_hdsp_midi_output_timer;
1406 hmidi->timer.data = (unsigned long) hmidi;
1407 hmidi->timer.expires = 1 + jiffies;
1408 add_timer(&hmidi->timer);
1409 hmidi->istimer++;
1410 }
1411 } else {
Takashi Iwaib0b981192005-10-20 18:29:58 +02001412 if (hmidi->istimer && --hmidi->istimer <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 del_timer (&hmidi->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 }
1415 spin_unlock_irqrestore (&hmidi->lock, flags);
1416 if (up)
1417 snd_hdsp_midi_output_write(hmidi);
1418}
1419
Takashi Iwai55e957d2005-11-17 14:52:13 +01001420static int snd_hdsp_midi_input_open(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001422 struct hdsp_midi *hmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
Takashi Iwai55e957d2005-11-17 14:52:13 +01001424 hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 spin_lock_irq (&hmidi->lock);
1426 snd_hdsp_flush_midi_input (hmidi->hdsp, hmidi->id);
1427 hmidi->input = substream;
1428 spin_unlock_irq (&hmidi->lock);
1429
1430 return 0;
1431}
1432
Takashi Iwai55e957d2005-11-17 14:52:13 +01001433static int snd_hdsp_midi_output_open(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001435 struct hdsp_midi *hmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
Takashi Iwai55e957d2005-11-17 14:52:13 +01001437 hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 spin_lock_irq (&hmidi->lock);
1439 hmidi->output = substream;
1440 spin_unlock_irq (&hmidi->lock);
1441
1442 return 0;
1443}
1444
Takashi Iwai55e957d2005-11-17 14:52:13 +01001445static int snd_hdsp_midi_input_close(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001447 struct hdsp_midi *hmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
1449 snd_hdsp_midi_input_trigger (substream, 0);
1450
Takashi Iwai55e957d2005-11-17 14:52:13 +01001451 hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 spin_lock_irq (&hmidi->lock);
1453 hmidi->input = NULL;
1454 spin_unlock_irq (&hmidi->lock);
1455
1456 return 0;
1457}
1458
Takashi Iwai55e957d2005-11-17 14:52:13 +01001459static int snd_hdsp_midi_output_close(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001461 struct hdsp_midi *hmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
1463 snd_hdsp_midi_output_trigger (substream, 0);
1464
Takashi Iwai55e957d2005-11-17 14:52:13 +01001465 hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 spin_lock_irq (&hmidi->lock);
1467 hmidi->output = NULL;
1468 spin_unlock_irq (&hmidi->lock);
1469
1470 return 0;
1471}
1472
Takashi Iwai55e957d2005-11-17 14:52:13 +01001473static struct snd_rawmidi_ops snd_hdsp_midi_output =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474{
1475 .open = snd_hdsp_midi_output_open,
1476 .close = snd_hdsp_midi_output_close,
1477 .trigger = snd_hdsp_midi_output_trigger,
1478};
1479
Takashi Iwai55e957d2005-11-17 14:52:13 +01001480static struct snd_rawmidi_ops snd_hdsp_midi_input =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481{
1482 .open = snd_hdsp_midi_input_open,
1483 .close = snd_hdsp_midi_input_close,
1484 .trigger = snd_hdsp_midi_input_trigger,
1485};
1486
Takashi Iwaif40b6892006-07-05 16:51:05 +02001487static int snd_hdsp_create_midi (struct snd_card *card, struct hdsp *hdsp, int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488{
1489 char buf[32];
1490
1491 hdsp->midi[id].id = id;
1492 hdsp->midi[id].rmidi = NULL;
1493 hdsp->midi[id].input = NULL;
1494 hdsp->midi[id].output = NULL;
1495 hdsp->midi[id].hdsp = hdsp;
1496 hdsp->midi[id].istimer = 0;
1497 hdsp->midi[id].pending = 0;
1498 spin_lock_init (&hdsp->midi[id].lock);
1499
1500 sprintf (buf, "%s MIDI %d", card->shortname, id+1);
Takashi Iwaib0b981192005-10-20 18:29:58 +02001501 if (snd_rawmidi_new (card, buf, id, 1, 1, &hdsp->midi[id].rmidi) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Jaroslav Kysela972d4c52008-11-12 16:37:48 +01001504 sprintf(hdsp->midi[id].rmidi->name, "HDSP MIDI %d", id+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 hdsp->midi[id].rmidi->private_data = &hdsp->midi[id];
1506
1507 snd_rawmidi_set_ops (hdsp->midi[id].rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_hdsp_midi_output);
1508 snd_rawmidi_set_ops (hdsp->midi[id].rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_hdsp_midi_input);
1509
1510 hdsp->midi[id].rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT |
1511 SNDRV_RAWMIDI_INFO_INPUT |
1512 SNDRV_RAWMIDI_INFO_DUPLEX;
1513
1514 return 0;
1515}
1516
1517/*-----------------------------------------------------------------------------
1518 Control Interface
1519 ----------------------------------------------------------------------------*/
1520
Takashi Iwai55e957d2005-11-17 14:52:13 +01001521static u32 snd_hdsp_convert_from_aes(struct snd_aes_iec958 *aes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522{
1523 u32 val = 0;
1524 val |= (aes->status[0] & IEC958_AES0_PROFESSIONAL) ? HDSP_SPDIFProfessional : 0;
1525 val |= (aes->status[0] & IEC958_AES0_NONAUDIO) ? HDSP_SPDIFNonAudio : 0;
1526 if (val & HDSP_SPDIFProfessional)
1527 val |= (aes->status[0] & IEC958_AES0_PRO_EMPHASIS_5015) ? HDSP_SPDIFEmphasis : 0;
1528 else
1529 val |= (aes->status[0] & IEC958_AES0_CON_EMPHASIS_5015) ? HDSP_SPDIFEmphasis : 0;
1530 return val;
1531}
1532
Takashi Iwai55e957d2005-11-17 14:52:13 +01001533static void snd_hdsp_convert_to_aes(struct snd_aes_iec958 *aes, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534{
1535 aes->status[0] = ((val & HDSP_SPDIFProfessional) ? IEC958_AES0_PROFESSIONAL : 0) |
1536 ((val & HDSP_SPDIFNonAudio) ? IEC958_AES0_NONAUDIO : 0);
1537 if (val & HDSP_SPDIFProfessional)
1538 aes->status[0] |= (val & HDSP_SPDIFEmphasis) ? IEC958_AES0_PRO_EMPHASIS_5015 : 0;
1539 else
1540 aes->status[0] |= (val & HDSP_SPDIFEmphasis) ? IEC958_AES0_CON_EMPHASIS_5015 : 0;
1541}
1542
Takashi Iwai55e957d2005-11-17 14:52:13 +01001543static int snd_hdsp_control_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544{
1545 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1546 uinfo->count = 1;
1547 return 0;
1548}
1549
Takashi Iwai55e957d2005-11-17 14:52:13 +01001550static int snd_hdsp_control_spdif_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001552 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001553
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 snd_hdsp_convert_to_aes(&ucontrol->value.iec958, hdsp->creg_spdif);
1555 return 0;
1556}
1557
Takashi Iwai55e957d2005-11-17 14:52:13 +01001558static int snd_hdsp_control_spdif_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001560 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 int change;
1562 u32 val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001563
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 val = snd_hdsp_convert_from_aes(&ucontrol->value.iec958);
1565 spin_lock_irq(&hdsp->lock);
1566 change = val != hdsp->creg_spdif;
1567 hdsp->creg_spdif = val;
1568 spin_unlock_irq(&hdsp->lock);
1569 return change;
1570}
1571
Takashi Iwai55e957d2005-11-17 14:52:13 +01001572static int snd_hdsp_control_spdif_stream_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573{
1574 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1575 uinfo->count = 1;
1576 return 0;
1577}
1578
Takashi Iwai55e957d2005-11-17 14:52:13 +01001579static int snd_hdsp_control_spdif_stream_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001581 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001582
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 snd_hdsp_convert_to_aes(&ucontrol->value.iec958, hdsp->creg_spdif_stream);
1584 return 0;
1585}
1586
Takashi Iwai55e957d2005-11-17 14:52:13 +01001587static int snd_hdsp_control_spdif_stream_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001589 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 int change;
1591 u32 val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001592
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 val = snd_hdsp_convert_from_aes(&ucontrol->value.iec958);
1594 spin_lock_irq(&hdsp->lock);
1595 change = val != hdsp->creg_spdif_stream;
1596 hdsp->creg_spdif_stream = val;
1597 hdsp->control_register &= ~(HDSP_SPDIFProfessional | HDSP_SPDIFNonAudio | HDSP_SPDIFEmphasis);
1598 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register |= val);
1599 spin_unlock_irq(&hdsp->lock);
1600 return change;
1601}
1602
Takashi Iwai55e957d2005-11-17 14:52:13 +01001603static int snd_hdsp_control_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604{
1605 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1606 uinfo->count = 1;
1607 return 0;
1608}
1609
Takashi Iwai55e957d2005-11-17 14:52:13 +01001610static int snd_hdsp_control_spdif_mask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611{
1612 ucontrol->value.iec958.status[0] = kcontrol->private_value;
1613 return 0;
1614}
1615
1616#define HDSP_SPDIF_IN(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001617{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 .name = xname, \
1619 .index = xindex, \
1620 .info = snd_hdsp_info_spdif_in, \
1621 .get = snd_hdsp_get_spdif_in, \
1622 .put = snd_hdsp_put_spdif_in }
1623
Takashi Iwai55e957d2005-11-17 14:52:13 +01001624static unsigned int hdsp_spdif_in(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625{
1626 return hdsp_decode_spdif_in(hdsp->control_register & HDSP_SPDIFInputMask);
1627}
1628
Takashi Iwai55e957d2005-11-17 14:52:13 +01001629static int hdsp_set_spdif_input(struct hdsp *hdsp, int in)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630{
1631 hdsp->control_register &= ~HDSP_SPDIFInputMask;
1632 hdsp->control_register |= hdsp_encode_spdif_in(in);
1633 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1634 return 0;
1635}
1636
Takashi Iwai55e957d2005-11-17 14:52:13 +01001637static int snd_hdsp_info_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638{
1639 static char *texts[4] = {"Optical", "Coaxial", "Internal", "AES"};
Takashi Iwai55e957d2005-11-17 14:52:13 +01001640 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
1642 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1643 uinfo->count = 1;
1644 uinfo->value.enumerated.items = ((hdsp->io_type == H9632) ? 4 : 3);
1645 if (uinfo->value.enumerated.item > ((hdsp->io_type == H9632) ? 3 : 2))
1646 uinfo->value.enumerated.item = ((hdsp->io_type == H9632) ? 3 : 2);
1647 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1648 return 0;
1649}
1650
Takashi Iwai55e957d2005-11-17 14:52:13 +01001651static int snd_hdsp_get_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001653 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001654
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 ucontrol->value.enumerated.item[0] = hdsp_spdif_in(hdsp);
1656 return 0;
1657}
1658
Takashi Iwai55e957d2005-11-17 14:52:13 +01001659static int snd_hdsp_put_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001661 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 int change;
1663 unsigned int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001664
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 if (!snd_hdsp_use_is_exclusive(hdsp))
1666 return -EBUSY;
1667 val = ucontrol->value.enumerated.item[0] % ((hdsp->io_type == H9632) ? 4 : 3);
1668 spin_lock_irq(&hdsp->lock);
1669 change = val != hdsp_spdif_in(hdsp);
1670 if (change)
1671 hdsp_set_spdif_input(hdsp, val);
1672 spin_unlock_irq(&hdsp->lock);
1673 return change;
1674}
1675
1676#define HDSP_SPDIF_OUT(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001677{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 .info = snd_hdsp_info_spdif_bits, \
1679 .get = snd_hdsp_get_spdif_out, .put = snd_hdsp_put_spdif_out }
1680
Takashi Iwai55e957d2005-11-17 14:52:13 +01001681static int hdsp_spdif_out(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682{
1683 return (hdsp->control_register & HDSP_SPDIFOpticalOut) ? 1 : 0;
1684}
1685
Takashi Iwai55e957d2005-11-17 14:52:13 +01001686static int hdsp_set_spdif_output(struct hdsp *hdsp, int out)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687{
Takashi Iwaib0b981192005-10-20 18:29:58 +02001688 if (out)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 hdsp->control_register |= HDSP_SPDIFOpticalOut;
Takashi Iwaib0b981192005-10-20 18:29:58 +02001690 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 hdsp->control_register &= ~HDSP_SPDIFOpticalOut;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1693 return 0;
1694}
1695
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001696#define snd_hdsp_info_spdif_bits snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
Takashi Iwai55e957d2005-11-17 14:52:13 +01001698static int snd_hdsp_get_spdif_out(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001700 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001701
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 ucontrol->value.integer.value[0] = hdsp_spdif_out(hdsp);
1703 return 0;
1704}
1705
Takashi Iwai55e957d2005-11-17 14:52:13 +01001706static int snd_hdsp_put_spdif_out(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001708 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 int change;
1710 unsigned int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001711
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 if (!snd_hdsp_use_is_exclusive(hdsp))
1713 return -EBUSY;
1714 val = ucontrol->value.integer.value[0] & 1;
1715 spin_lock_irq(&hdsp->lock);
1716 change = (int)val != hdsp_spdif_out(hdsp);
1717 hdsp_set_spdif_output(hdsp, val);
1718 spin_unlock_irq(&hdsp->lock);
1719 return change;
1720}
1721
1722#define HDSP_SPDIF_PROFESSIONAL(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001723{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 .info = snd_hdsp_info_spdif_bits, \
1725 .get = snd_hdsp_get_spdif_professional, .put = snd_hdsp_put_spdif_professional }
1726
Takashi Iwai55e957d2005-11-17 14:52:13 +01001727static int hdsp_spdif_professional(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728{
1729 return (hdsp->control_register & HDSP_SPDIFProfessional) ? 1 : 0;
1730}
1731
Takashi Iwai55e957d2005-11-17 14:52:13 +01001732static int hdsp_set_spdif_professional(struct hdsp *hdsp, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733{
Takashi Iwaib0b981192005-10-20 18:29:58 +02001734 if (val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 hdsp->control_register |= HDSP_SPDIFProfessional;
Takashi Iwaib0b981192005-10-20 18:29:58 +02001736 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 hdsp->control_register &= ~HDSP_SPDIFProfessional;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1739 return 0;
1740}
1741
Takashi Iwai55e957d2005-11-17 14:52:13 +01001742static int snd_hdsp_get_spdif_professional(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001744 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001745
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 ucontrol->value.integer.value[0] = hdsp_spdif_professional(hdsp);
1747 return 0;
1748}
1749
Takashi Iwai55e957d2005-11-17 14:52:13 +01001750static int snd_hdsp_put_spdif_professional(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001752 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 int change;
1754 unsigned int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001755
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 if (!snd_hdsp_use_is_exclusive(hdsp))
1757 return -EBUSY;
1758 val = ucontrol->value.integer.value[0] & 1;
1759 spin_lock_irq(&hdsp->lock);
1760 change = (int)val != hdsp_spdif_professional(hdsp);
1761 hdsp_set_spdif_professional(hdsp, val);
1762 spin_unlock_irq(&hdsp->lock);
1763 return change;
1764}
1765
1766#define HDSP_SPDIF_EMPHASIS(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001767{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 .info = snd_hdsp_info_spdif_bits, \
1769 .get = snd_hdsp_get_spdif_emphasis, .put = snd_hdsp_put_spdif_emphasis }
1770
Takashi Iwai55e957d2005-11-17 14:52:13 +01001771static int hdsp_spdif_emphasis(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772{
1773 return (hdsp->control_register & HDSP_SPDIFEmphasis) ? 1 : 0;
1774}
1775
Takashi Iwai55e957d2005-11-17 14:52:13 +01001776static int hdsp_set_spdif_emphasis(struct hdsp *hdsp, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777{
Takashi Iwaib0b981192005-10-20 18:29:58 +02001778 if (val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 hdsp->control_register |= HDSP_SPDIFEmphasis;
Takashi Iwaib0b981192005-10-20 18:29:58 +02001780 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 hdsp->control_register &= ~HDSP_SPDIFEmphasis;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1783 return 0;
1784}
1785
Takashi Iwai55e957d2005-11-17 14:52:13 +01001786static int snd_hdsp_get_spdif_emphasis(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001788 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001789
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 ucontrol->value.integer.value[0] = hdsp_spdif_emphasis(hdsp);
1791 return 0;
1792}
1793
Takashi Iwai55e957d2005-11-17 14:52:13 +01001794static int snd_hdsp_put_spdif_emphasis(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001796 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 int change;
1798 unsigned int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001799
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 if (!snd_hdsp_use_is_exclusive(hdsp))
1801 return -EBUSY;
1802 val = ucontrol->value.integer.value[0] & 1;
1803 spin_lock_irq(&hdsp->lock);
1804 change = (int)val != hdsp_spdif_emphasis(hdsp);
1805 hdsp_set_spdif_emphasis(hdsp, val);
1806 spin_unlock_irq(&hdsp->lock);
1807 return change;
1808}
1809
1810#define HDSP_SPDIF_NON_AUDIO(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001811{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 .info = snd_hdsp_info_spdif_bits, \
1813 .get = snd_hdsp_get_spdif_nonaudio, .put = snd_hdsp_put_spdif_nonaudio }
1814
Takashi Iwai55e957d2005-11-17 14:52:13 +01001815static int hdsp_spdif_nonaudio(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816{
1817 return (hdsp->control_register & HDSP_SPDIFNonAudio) ? 1 : 0;
1818}
1819
Takashi Iwai55e957d2005-11-17 14:52:13 +01001820static int hdsp_set_spdif_nonaudio(struct hdsp *hdsp, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821{
Takashi Iwaib0b981192005-10-20 18:29:58 +02001822 if (val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 hdsp->control_register |= HDSP_SPDIFNonAudio;
Takashi Iwaib0b981192005-10-20 18:29:58 +02001824 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 hdsp->control_register &= ~HDSP_SPDIFNonAudio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
1827 return 0;
1828}
1829
Takashi Iwai55e957d2005-11-17 14:52:13 +01001830static int snd_hdsp_get_spdif_nonaudio(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001832 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001833
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 ucontrol->value.integer.value[0] = hdsp_spdif_nonaudio(hdsp);
1835 return 0;
1836}
1837
Takashi Iwai55e957d2005-11-17 14:52:13 +01001838static int snd_hdsp_put_spdif_nonaudio(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001840 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 int change;
1842 unsigned int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001843
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 if (!snd_hdsp_use_is_exclusive(hdsp))
1845 return -EBUSY;
1846 val = ucontrol->value.integer.value[0] & 1;
1847 spin_lock_irq(&hdsp->lock);
1848 change = (int)val != hdsp_spdif_nonaudio(hdsp);
1849 hdsp_set_spdif_nonaudio(hdsp, val);
1850 spin_unlock_irq(&hdsp->lock);
1851 return change;
1852}
1853
1854#define HDSP_SPDIF_SAMPLE_RATE(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001855{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 .name = xname, \
1857 .index = xindex, \
1858 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
1859 .info = snd_hdsp_info_spdif_sample_rate, \
1860 .get = snd_hdsp_get_spdif_sample_rate \
1861}
1862
Takashi Iwai55e957d2005-11-17 14:52:13 +01001863static int snd_hdsp_info_spdif_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864{
1865 static char *texts[] = {"32000", "44100", "48000", "64000", "88200", "96000", "None", "128000", "176400", "192000"};
Takashi Iwai55e957d2005-11-17 14:52:13 +01001866 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
1868 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1869 uinfo->count = 1;
1870 uinfo->value.enumerated.items = (hdsp->io_type == H9632) ? 10 : 7;
1871 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1872 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
1873 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1874 return 0;
1875}
1876
Takashi Iwai55e957d2005-11-17 14:52:13 +01001877static int snd_hdsp_get_spdif_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001879 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001880
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 switch (hdsp_spdif_sample_rate(hdsp)) {
1882 case 32000:
1883 ucontrol->value.enumerated.item[0] = 0;
1884 break;
1885 case 44100:
1886 ucontrol->value.enumerated.item[0] = 1;
1887 break;
1888 case 48000:
1889 ucontrol->value.enumerated.item[0] = 2;
1890 break;
1891 case 64000:
1892 ucontrol->value.enumerated.item[0] = 3;
1893 break;
1894 case 88200:
1895 ucontrol->value.enumerated.item[0] = 4;
1896 break;
1897 case 96000:
1898 ucontrol->value.enumerated.item[0] = 5;
1899 break;
1900 case 128000:
1901 ucontrol->value.enumerated.item[0] = 7;
1902 break;
1903 case 176400:
1904 ucontrol->value.enumerated.item[0] = 8;
1905 break;
1906 case 192000:
1907 ucontrol->value.enumerated.item[0] = 9;
1908 break;
1909 default:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001910 ucontrol->value.enumerated.item[0] = 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 }
1912 return 0;
1913}
1914
1915#define HDSP_SYSTEM_SAMPLE_RATE(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001916{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 .name = xname, \
1918 .index = xindex, \
1919 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
1920 .info = snd_hdsp_info_system_sample_rate, \
1921 .get = snd_hdsp_get_system_sample_rate \
1922}
1923
Takashi Iwai55e957d2005-11-17 14:52:13 +01001924static int snd_hdsp_info_system_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925{
1926 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1927 uinfo->count = 1;
1928 return 0;
1929}
1930
Takashi Iwai55e957d2005-11-17 14:52:13 +01001931static int snd_hdsp_get_system_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001933 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001934
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 ucontrol->value.enumerated.item[0] = hdsp->system_sample_rate;
1936 return 0;
1937}
1938
1939#define HDSP_AUTOSYNC_SAMPLE_RATE(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001940{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 .name = xname, \
1942 .index = xindex, \
1943 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
1944 .info = snd_hdsp_info_autosync_sample_rate, \
1945 .get = snd_hdsp_get_autosync_sample_rate \
1946}
1947
Takashi Iwai55e957d2005-11-17 14:52:13 +01001948static int snd_hdsp_info_autosync_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001950 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001951 static char *texts[] = {"32000", "44100", "48000", "64000", "88200", "96000", "None", "128000", "176400", "192000"};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1953 uinfo->count = 1;
1954 uinfo->value.enumerated.items = (hdsp->io_type == H9632) ? 10 : 7 ;
1955 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1956 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
1957 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1958 return 0;
1959}
1960
Takashi Iwai55e957d2005-11-17 14:52:13 +01001961static int snd_hdsp_get_autosync_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962{
Takashi Iwai55e957d2005-11-17 14:52:13 +01001963 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001964
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 switch (hdsp_external_sample_rate(hdsp)) {
1966 case 32000:
1967 ucontrol->value.enumerated.item[0] = 0;
1968 break;
1969 case 44100:
1970 ucontrol->value.enumerated.item[0] = 1;
1971 break;
1972 case 48000:
1973 ucontrol->value.enumerated.item[0] = 2;
1974 break;
1975 case 64000:
1976 ucontrol->value.enumerated.item[0] = 3;
1977 break;
1978 case 88200:
1979 ucontrol->value.enumerated.item[0] = 4;
1980 break;
1981 case 96000:
1982 ucontrol->value.enumerated.item[0] = 5;
1983 break;
1984 case 128000:
1985 ucontrol->value.enumerated.item[0] = 7;
1986 break;
1987 case 176400:
1988 ucontrol->value.enumerated.item[0] = 8;
1989 break;
1990 case 192000:
1991 ucontrol->value.enumerated.item[0] = 9;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001992 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 default:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01001994 ucontrol->value.enumerated.item[0] = 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 }
1996 return 0;
1997}
1998
1999#define HDSP_SYSTEM_CLOCK_MODE(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002000{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 .name = xname, \
2002 .index = xindex, \
2003 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
2004 .info = snd_hdsp_info_system_clock_mode, \
2005 .get = snd_hdsp_get_system_clock_mode \
2006}
2007
Takashi Iwai55e957d2005-11-17 14:52:13 +01002008static int hdsp_system_clock_mode(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009{
Takashi Iwaib0b981192005-10-20 18:29:58 +02002010 if (hdsp->control_register & HDSP_ClockModeMaster)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 return 0;
Takashi Iwaib0b981192005-10-20 18:29:58 +02002012 else if (hdsp_external_sample_rate(hdsp) != hdsp->system_sample_rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 return 1;
2015}
2016
Takashi Iwai55e957d2005-11-17 14:52:13 +01002017static int snd_hdsp_info_system_clock_mode(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018{
2019 static char *texts[] = {"Master", "Slave" };
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002020
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2022 uinfo->count = 1;
2023 uinfo->value.enumerated.items = 2;
2024 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2025 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2026 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2027 return 0;
2028}
2029
Takashi Iwai55e957d2005-11-17 14:52:13 +01002030static int snd_hdsp_get_system_clock_mode(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002032 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002033
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 ucontrol->value.enumerated.item[0] = hdsp_system_clock_mode(hdsp);
2035 return 0;
2036}
2037
2038#define HDSP_CLOCK_SOURCE(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002039{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 .name = xname, \
2041 .index = xindex, \
2042 .info = snd_hdsp_info_clock_source, \
2043 .get = snd_hdsp_get_clock_source, \
2044 .put = snd_hdsp_put_clock_source \
2045}
2046
Takashi Iwai55e957d2005-11-17 14:52:13 +01002047static int hdsp_clock_source(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048{
2049 if (hdsp->control_register & HDSP_ClockModeMaster) {
2050 switch (hdsp->system_sample_rate) {
2051 case 32000:
2052 return 1;
2053 case 44100:
2054 return 2;
2055 case 48000:
2056 return 3;
2057 case 64000:
2058 return 4;
2059 case 88200:
2060 return 5;
2061 case 96000:
2062 return 6;
2063 case 128000:
2064 return 7;
2065 case 176400:
2066 return 8;
2067 case 192000:
2068 return 9;
2069 default:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002070 return 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 }
2072 } else {
2073 return 0;
2074 }
2075}
2076
Takashi Iwai55e957d2005-11-17 14:52:13 +01002077static int hdsp_set_clock_source(struct hdsp *hdsp, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078{
2079 int rate;
2080 switch (mode) {
2081 case HDSP_CLOCK_SOURCE_AUTOSYNC:
2082 if (hdsp_external_sample_rate(hdsp) != 0) {
2083 if (!hdsp_set_rate(hdsp, hdsp_external_sample_rate(hdsp), 1)) {
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002084 hdsp->control_register &= ~HDSP_ClockModeMaster;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2086 return 0;
2087 }
2088 }
2089 return -1;
2090 case HDSP_CLOCK_SOURCE_INTERNAL_32KHZ:
2091 rate = 32000;
2092 break;
2093 case HDSP_CLOCK_SOURCE_INTERNAL_44_1KHZ:
2094 rate = 44100;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002095 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 case HDSP_CLOCK_SOURCE_INTERNAL_48KHZ:
2097 rate = 48000;
2098 break;
2099 case HDSP_CLOCK_SOURCE_INTERNAL_64KHZ:
2100 rate = 64000;
2101 break;
2102 case HDSP_CLOCK_SOURCE_INTERNAL_88_2KHZ:
2103 rate = 88200;
2104 break;
2105 case HDSP_CLOCK_SOURCE_INTERNAL_96KHZ:
2106 rate = 96000;
2107 break;
2108 case HDSP_CLOCK_SOURCE_INTERNAL_128KHZ:
2109 rate = 128000;
2110 break;
2111 case HDSP_CLOCK_SOURCE_INTERNAL_176_4KHZ:
2112 rate = 176400;
2113 break;
2114 case HDSP_CLOCK_SOURCE_INTERNAL_192KHZ:
2115 rate = 192000;
2116 break;
2117 default:
2118 rate = 48000;
2119 }
2120 hdsp->control_register |= HDSP_ClockModeMaster;
2121 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2122 hdsp_set_rate(hdsp, rate, 1);
2123 return 0;
2124}
2125
Takashi Iwai55e957d2005-11-17 14:52:13 +01002126static int snd_hdsp_info_clock_source(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127{
2128 static char *texts[] = {"AutoSync", "Internal 32.0 kHz", "Internal 44.1 kHz", "Internal 48.0 kHz", "Internal 64.0 kHz", "Internal 88.2 kHz", "Internal 96.0 kHz", "Internal 128 kHz", "Internal 176.4 kHz", "Internal 192.0 KHz" };
Takashi Iwai55e957d2005-11-17 14:52:13 +01002129 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002130
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2132 uinfo->count = 1;
2133 if (hdsp->io_type == H9632)
2134 uinfo->value.enumerated.items = 10;
2135 else
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002136 uinfo->value.enumerated.items = 7;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2138 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2139 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2140 return 0;
2141}
2142
Takashi Iwai55e957d2005-11-17 14:52:13 +01002143static int snd_hdsp_get_clock_source(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002145 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002146
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 ucontrol->value.enumerated.item[0] = hdsp_clock_source(hdsp);
2148 return 0;
2149}
2150
Takashi Iwai55e957d2005-11-17 14:52:13 +01002151static int snd_hdsp_put_clock_source(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002153 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 int change;
2155 int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002156
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 if (!snd_hdsp_use_is_exclusive(hdsp))
2158 return -EBUSY;
2159 val = ucontrol->value.enumerated.item[0];
2160 if (val < 0) val = 0;
2161 if (hdsp->io_type == H9632) {
Takashi Iwaib0b981192005-10-20 18:29:58 +02002162 if (val > 9)
2163 val = 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 } else {
Takashi Iwaib0b981192005-10-20 18:29:58 +02002165 if (val > 6)
2166 val = 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 }
2168 spin_lock_irq(&hdsp->lock);
Takashi Iwaib0b981192005-10-20 18:29:58 +02002169 if (val != hdsp_clock_source(hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 change = (hdsp_set_clock_source(hdsp, val) == 0) ? 1 : 0;
Takashi Iwaib0b981192005-10-20 18:29:58 +02002171 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 spin_unlock_irq(&hdsp->lock);
2174 return change;
2175}
2176
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002177#define snd_hdsp_info_clock_source_lock snd_ctl_boolean_mono_info
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02002178
Takashi Iwai55e957d2005-11-17 14:52:13 +01002179static int snd_hdsp_get_clock_source_lock(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02002180{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002181 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002182
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02002183 ucontrol->value.integer.value[0] = hdsp->clock_source_locked;
2184 return 0;
2185}
2186
Takashi Iwai55e957d2005-11-17 14:52:13 +01002187static int snd_hdsp_put_clock_source_lock(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02002188{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002189 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02002190 int change;
2191
2192 change = (int)ucontrol->value.integer.value[0] != hdsp->clock_source_locked;
2193 if (change)
Takashi Iwai4e98d6a2007-11-15 15:58:13 +01002194 hdsp->clock_source_locked = !!ucontrol->value.integer.value[0];
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02002195 return change;
2196}
2197
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198#define HDSP_DA_GAIN(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002199{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 .name = xname, \
2201 .index = xindex, \
2202 .info = snd_hdsp_info_da_gain, \
2203 .get = snd_hdsp_get_da_gain, \
2204 .put = snd_hdsp_put_da_gain \
2205}
2206
Takashi Iwai55e957d2005-11-17 14:52:13 +01002207static int hdsp_da_gain(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208{
2209 switch (hdsp->control_register & HDSP_DAGainMask) {
2210 case HDSP_DAGainHighGain:
2211 return 0;
2212 case HDSP_DAGainPlus4dBu:
2213 return 1;
2214 case HDSP_DAGainMinus10dBV:
2215 return 2;
2216 default:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002217 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 }
2219}
2220
Takashi Iwai55e957d2005-11-17 14:52:13 +01002221static int hdsp_set_da_gain(struct hdsp *hdsp, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222{
2223 hdsp->control_register &= ~HDSP_DAGainMask;
2224 switch (mode) {
2225 case 0:
2226 hdsp->control_register |= HDSP_DAGainHighGain;
2227 break;
2228 case 1:
2229 hdsp->control_register |= HDSP_DAGainPlus4dBu;
2230 break;
2231 case 2:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002232 hdsp->control_register |= HDSP_DAGainMinus10dBV;
2233 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 default:
2235 return -1;
2236
2237 }
2238 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2239 return 0;
2240}
2241
Takashi Iwai55e957d2005-11-17 14:52:13 +01002242static int snd_hdsp_info_da_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243{
2244 static char *texts[] = {"Hi Gain", "+4 dBu", "-10 dbV"};
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002245
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2247 uinfo->count = 1;
2248 uinfo->value.enumerated.items = 3;
2249 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2250 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2251 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2252 return 0;
2253}
2254
Takashi Iwai55e957d2005-11-17 14:52:13 +01002255static int snd_hdsp_get_da_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002257 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002258
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 ucontrol->value.enumerated.item[0] = hdsp_da_gain(hdsp);
2260 return 0;
2261}
2262
Takashi Iwai55e957d2005-11-17 14:52:13 +01002263static int snd_hdsp_put_da_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002265 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 int change;
2267 int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002268
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 if (!snd_hdsp_use_is_exclusive(hdsp))
2270 return -EBUSY;
2271 val = ucontrol->value.enumerated.item[0];
2272 if (val < 0) val = 0;
2273 if (val > 2) val = 2;
2274 spin_lock_irq(&hdsp->lock);
Takashi Iwaib0b981192005-10-20 18:29:58 +02002275 if (val != hdsp_da_gain(hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 change = (hdsp_set_da_gain(hdsp, val) == 0) ? 1 : 0;
Takashi Iwaib0b981192005-10-20 18:29:58 +02002277 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 spin_unlock_irq(&hdsp->lock);
2280 return change;
2281}
2282
2283#define HDSP_AD_GAIN(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002284{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 .name = xname, \
2286 .index = xindex, \
2287 .info = snd_hdsp_info_ad_gain, \
2288 .get = snd_hdsp_get_ad_gain, \
2289 .put = snd_hdsp_put_ad_gain \
2290}
2291
Takashi Iwai55e957d2005-11-17 14:52:13 +01002292static int hdsp_ad_gain(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293{
2294 switch (hdsp->control_register & HDSP_ADGainMask) {
2295 case HDSP_ADGainMinus10dBV:
2296 return 0;
2297 case HDSP_ADGainPlus4dBu:
2298 return 1;
2299 case HDSP_ADGainLowGain:
2300 return 2;
2301 default:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002302 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 }
2304}
2305
Takashi Iwai55e957d2005-11-17 14:52:13 +01002306static int hdsp_set_ad_gain(struct hdsp *hdsp, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307{
2308 hdsp->control_register &= ~HDSP_ADGainMask;
2309 switch (mode) {
2310 case 0:
2311 hdsp->control_register |= HDSP_ADGainMinus10dBV;
2312 break;
2313 case 1:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002314 hdsp->control_register |= HDSP_ADGainPlus4dBu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 break;
2316 case 2:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002317 hdsp->control_register |= HDSP_ADGainLowGain;
2318 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 default:
2320 return -1;
2321
2322 }
2323 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2324 return 0;
2325}
2326
Takashi Iwai55e957d2005-11-17 14:52:13 +01002327static int snd_hdsp_info_ad_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328{
2329 static char *texts[] = {"-10 dBV", "+4 dBu", "Lo Gain"};
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002330
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2332 uinfo->count = 1;
2333 uinfo->value.enumerated.items = 3;
2334 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2335 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2336 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2337 return 0;
2338}
2339
Takashi Iwai55e957d2005-11-17 14:52:13 +01002340static int snd_hdsp_get_ad_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002342 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002343
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 ucontrol->value.enumerated.item[0] = hdsp_ad_gain(hdsp);
2345 return 0;
2346}
2347
Takashi Iwai55e957d2005-11-17 14:52:13 +01002348static int snd_hdsp_put_ad_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002350 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 int change;
2352 int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002353
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 if (!snd_hdsp_use_is_exclusive(hdsp))
2355 return -EBUSY;
2356 val = ucontrol->value.enumerated.item[0];
2357 if (val < 0) val = 0;
2358 if (val > 2) val = 2;
2359 spin_lock_irq(&hdsp->lock);
Takashi Iwaib0b981192005-10-20 18:29:58 +02002360 if (val != hdsp_ad_gain(hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 change = (hdsp_set_ad_gain(hdsp, val) == 0) ? 1 : 0;
Takashi Iwaib0b981192005-10-20 18:29:58 +02002362 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 spin_unlock_irq(&hdsp->lock);
2365 return change;
2366}
2367
2368#define HDSP_PHONE_GAIN(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002369{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 .name = xname, \
2371 .index = xindex, \
2372 .info = snd_hdsp_info_phone_gain, \
2373 .get = snd_hdsp_get_phone_gain, \
2374 .put = snd_hdsp_put_phone_gain \
2375}
2376
Takashi Iwai55e957d2005-11-17 14:52:13 +01002377static int hdsp_phone_gain(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378{
2379 switch (hdsp->control_register & HDSP_PhoneGainMask) {
2380 case HDSP_PhoneGain0dB:
2381 return 0;
2382 case HDSP_PhoneGainMinus6dB:
2383 return 1;
2384 case HDSP_PhoneGainMinus12dB:
2385 return 2;
2386 default:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002387 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 }
2389}
2390
Takashi Iwai55e957d2005-11-17 14:52:13 +01002391static int hdsp_set_phone_gain(struct hdsp *hdsp, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392{
2393 hdsp->control_register &= ~HDSP_PhoneGainMask;
2394 switch (mode) {
2395 case 0:
2396 hdsp->control_register |= HDSP_PhoneGain0dB;
2397 break;
2398 case 1:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002399 hdsp->control_register |= HDSP_PhoneGainMinus6dB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 break;
2401 case 2:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002402 hdsp->control_register |= HDSP_PhoneGainMinus12dB;
2403 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 default:
2405 return -1;
2406
2407 }
2408 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2409 return 0;
2410}
2411
Takashi Iwai55e957d2005-11-17 14:52:13 +01002412static int snd_hdsp_info_phone_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413{
2414 static char *texts[] = {"0 dB", "-6 dB", "-12 dB"};
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002415
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2417 uinfo->count = 1;
2418 uinfo->value.enumerated.items = 3;
2419 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2420 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2421 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2422 return 0;
2423}
2424
Takashi Iwai55e957d2005-11-17 14:52:13 +01002425static int snd_hdsp_get_phone_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002427 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002428
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 ucontrol->value.enumerated.item[0] = hdsp_phone_gain(hdsp);
2430 return 0;
2431}
2432
Takashi Iwai55e957d2005-11-17 14:52:13 +01002433static int snd_hdsp_put_phone_gain(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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 int change;
2437 int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002438
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 if (!snd_hdsp_use_is_exclusive(hdsp))
2440 return -EBUSY;
2441 val = ucontrol->value.enumerated.item[0];
2442 if (val < 0) val = 0;
2443 if (val > 2) val = 2;
2444 spin_lock_irq(&hdsp->lock);
Takashi Iwaib0b981192005-10-20 18:29:58 +02002445 if (val != hdsp_phone_gain(hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 change = (hdsp_set_phone_gain(hdsp, val) == 0) ? 1 : 0;
Takashi Iwaib0b981192005-10-20 18:29:58 +02002447 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 spin_unlock_irq(&hdsp->lock);
2450 return change;
2451}
2452
2453#define HDSP_XLR_BREAKOUT_CABLE(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002454{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 .name = xname, \
2456 .index = xindex, \
2457 .info = snd_hdsp_info_xlr_breakout_cable, \
2458 .get = snd_hdsp_get_xlr_breakout_cable, \
2459 .put = snd_hdsp_put_xlr_breakout_cable \
2460}
2461
Takashi Iwai55e957d2005-11-17 14:52:13 +01002462static int hdsp_xlr_breakout_cable(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463{
Takashi Iwaib0b981192005-10-20 18:29:58 +02002464 if (hdsp->control_register & HDSP_XLRBreakoutCable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 return 0;
2467}
2468
Takashi Iwai55e957d2005-11-17 14:52:13 +01002469static int hdsp_set_xlr_breakout_cable(struct hdsp *hdsp, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470{
Takashi Iwaib0b981192005-10-20 18:29:58 +02002471 if (mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 hdsp->control_register |= HDSP_XLRBreakoutCable;
Takashi Iwaib0b981192005-10-20 18:29:58 +02002473 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 hdsp->control_register &= ~HDSP_XLRBreakoutCable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2476 return 0;
2477}
2478
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002479#define snd_hdsp_info_xlr_breakout_cable snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480
Takashi Iwai55e957d2005-11-17 14:52:13 +01002481static int snd_hdsp_get_xlr_breakout_cable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002483 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002484
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 ucontrol->value.enumerated.item[0] = hdsp_xlr_breakout_cable(hdsp);
2486 return 0;
2487}
2488
Takashi Iwai55e957d2005-11-17 14:52:13 +01002489static int snd_hdsp_put_xlr_breakout_cable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002491 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 int change;
2493 int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002494
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 if (!snd_hdsp_use_is_exclusive(hdsp))
2496 return -EBUSY;
2497 val = ucontrol->value.integer.value[0] & 1;
2498 spin_lock_irq(&hdsp->lock);
2499 change = (int)val != hdsp_xlr_breakout_cable(hdsp);
2500 hdsp_set_xlr_breakout_cable(hdsp, val);
2501 spin_unlock_irq(&hdsp->lock);
2502 return change;
2503}
2504
2505/* (De)activates old RME Analog Extension Board
2506 These are connected to the internal ADAT connector
2507 Switching this on desactivates external ADAT
2508*/
2509#define HDSP_AEB(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002510{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 .name = xname, \
2512 .index = xindex, \
2513 .info = snd_hdsp_info_aeb, \
2514 .get = snd_hdsp_get_aeb, \
2515 .put = snd_hdsp_put_aeb \
2516}
2517
Takashi Iwai55e957d2005-11-17 14:52:13 +01002518static int hdsp_aeb(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519{
Takashi Iwaib0b981192005-10-20 18:29:58 +02002520 if (hdsp->control_register & HDSP_AnalogExtensionBoard)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 return 0;
2523}
2524
Takashi Iwai55e957d2005-11-17 14:52:13 +01002525static int hdsp_set_aeb(struct hdsp *hdsp, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526{
Takashi Iwaib0b981192005-10-20 18:29:58 +02002527 if (mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 hdsp->control_register |= HDSP_AnalogExtensionBoard;
Takashi Iwaib0b981192005-10-20 18:29:58 +02002529 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 hdsp->control_register &= ~HDSP_AnalogExtensionBoard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2532 return 0;
2533}
2534
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002535#define snd_hdsp_info_aeb snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536
Takashi Iwai55e957d2005-11-17 14:52:13 +01002537static int snd_hdsp_get_aeb(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002539 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002540
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 ucontrol->value.enumerated.item[0] = hdsp_aeb(hdsp);
2542 return 0;
2543}
2544
Takashi Iwai55e957d2005-11-17 14:52:13 +01002545static int snd_hdsp_put_aeb(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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 int change;
2549 int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002550
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 if (!snd_hdsp_use_is_exclusive(hdsp))
2552 return -EBUSY;
2553 val = ucontrol->value.integer.value[0] & 1;
2554 spin_lock_irq(&hdsp->lock);
2555 change = (int)val != hdsp_aeb(hdsp);
2556 hdsp_set_aeb(hdsp, val);
2557 spin_unlock_irq(&hdsp->lock);
2558 return change;
2559}
2560
2561#define HDSP_PREF_SYNC_REF(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002562{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 .name = xname, \
2564 .index = xindex, \
2565 .info = snd_hdsp_info_pref_sync_ref, \
2566 .get = snd_hdsp_get_pref_sync_ref, \
2567 .put = snd_hdsp_put_pref_sync_ref \
2568}
2569
Takashi Iwai55e957d2005-11-17 14:52:13 +01002570static int hdsp_pref_sync_ref(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571{
2572 /* Notice that this looks at the requested sync source,
2573 not the one actually in use.
2574 */
2575
2576 switch (hdsp->control_register & HDSP_SyncRefMask) {
2577 case HDSP_SyncRef_ADAT1:
2578 return HDSP_SYNC_FROM_ADAT1;
2579 case HDSP_SyncRef_ADAT2:
2580 return HDSP_SYNC_FROM_ADAT2;
2581 case HDSP_SyncRef_ADAT3:
2582 return HDSP_SYNC_FROM_ADAT3;
2583 case HDSP_SyncRef_SPDIF:
2584 return HDSP_SYNC_FROM_SPDIF;
2585 case HDSP_SyncRef_WORD:
2586 return HDSP_SYNC_FROM_WORD;
2587 case HDSP_SyncRef_ADAT_SYNC:
2588 return HDSP_SYNC_FROM_ADAT_SYNC;
2589 default:
2590 return HDSP_SYNC_FROM_WORD;
2591 }
2592 return 0;
2593}
2594
Takashi Iwai55e957d2005-11-17 14:52:13 +01002595static int hdsp_set_pref_sync_ref(struct hdsp *hdsp, int pref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596{
2597 hdsp->control_register &= ~HDSP_SyncRefMask;
2598 switch (pref) {
2599 case HDSP_SYNC_FROM_ADAT1:
2600 hdsp->control_register &= ~HDSP_SyncRefMask; /* clear SyncRef bits */
2601 break;
2602 case HDSP_SYNC_FROM_ADAT2:
2603 hdsp->control_register |= HDSP_SyncRef_ADAT2;
2604 break;
2605 case HDSP_SYNC_FROM_ADAT3:
2606 hdsp->control_register |= HDSP_SyncRef_ADAT3;
2607 break;
2608 case HDSP_SYNC_FROM_SPDIF:
2609 hdsp->control_register |= HDSP_SyncRef_SPDIF;
2610 break;
2611 case HDSP_SYNC_FROM_WORD:
2612 hdsp->control_register |= HDSP_SyncRef_WORD;
2613 break;
2614 case HDSP_SYNC_FROM_ADAT_SYNC:
2615 hdsp->control_register |= HDSP_SyncRef_ADAT_SYNC;
2616 break;
2617 default:
2618 return -1;
2619 }
2620 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2621 return 0;
2622}
2623
Takashi Iwai55e957d2005-11-17 14:52:13 +01002624static int snd_hdsp_info_pref_sync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625{
2626 static char *texts[] = {"Word", "IEC958", "ADAT1", "ADAT Sync", "ADAT2", "ADAT3" };
Takashi Iwai55e957d2005-11-17 14:52:13 +01002627 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002628
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2630 uinfo->count = 1;
2631
2632 switch (hdsp->io_type) {
2633 case Digiface:
2634 case H9652:
2635 uinfo->value.enumerated.items = 6;
2636 break;
2637 case Multiface:
2638 uinfo->value.enumerated.items = 4;
2639 break;
2640 case H9632:
2641 uinfo->value.enumerated.items = 3;
2642 break;
2643 default:
Takashi Iwai9badda02012-01-09 18:22:35 +01002644 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002646
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2648 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2649 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2650 return 0;
2651}
2652
Takashi Iwai55e957d2005-11-17 14:52:13 +01002653static int snd_hdsp_get_pref_sync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002655 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002656
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 ucontrol->value.enumerated.item[0] = hdsp_pref_sync_ref(hdsp);
2658 return 0;
2659}
2660
Takashi Iwai55e957d2005-11-17 14:52:13 +01002661static int snd_hdsp_put_pref_sync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002663 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 int change, max;
2665 unsigned int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002666
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 if (!snd_hdsp_use_is_exclusive(hdsp))
2668 return -EBUSY;
2669
2670 switch (hdsp->io_type) {
2671 case Digiface:
2672 case H9652:
2673 max = 6;
2674 break;
2675 case Multiface:
2676 max = 4;
2677 break;
2678 case H9632:
2679 max = 3;
2680 break;
2681 default:
2682 return -EIO;
2683 }
2684
2685 val = ucontrol->value.enumerated.item[0] % max;
2686 spin_lock_irq(&hdsp->lock);
2687 change = (int)val != hdsp_pref_sync_ref(hdsp);
2688 hdsp_set_pref_sync_ref(hdsp, val);
2689 spin_unlock_irq(&hdsp->lock);
2690 return change;
2691}
2692
2693#define HDSP_AUTOSYNC_REF(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002694{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 .name = xname, \
2696 .index = xindex, \
2697 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
2698 .info = snd_hdsp_info_autosync_ref, \
2699 .get = snd_hdsp_get_autosync_ref, \
2700}
2701
Takashi Iwai55e957d2005-11-17 14:52:13 +01002702static int hdsp_autosync_ref(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703{
2704 /* This looks at the autosync selected sync reference */
2705 unsigned int status2 = hdsp_read(hdsp, HDSP_status2Register);
2706
2707 switch (status2 & HDSP_SelSyncRefMask) {
2708 case HDSP_SelSyncRef_WORD:
2709 return HDSP_AUTOSYNC_FROM_WORD;
2710 case HDSP_SelSyncRef_ADAT_SYNC:
2711 return HDSP_AUTOSYNC_FROM_ADAT_SYNC;
2712 case HDSP_SelSyncRef_SPDIF:
2713 return HDSP_AUTOSYNC_FROM_SPDIF;
2714 case HDSP_SelSyncRefMask:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002715 return HDSP_AUTOSYNC_FROM_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 case HDSP_SelSyncRef_ADAT1:
2717 return HDSP_AUTOSYNC_FROM_ADAT1;
2718 case HDSP_SelSyncRef_ADAT2:
2719 return HDSP_AUTOSYNC_FROM_ADAT2;
2720 case HDSP_SelSyncRef_ADAT3:
2721 return HDSP_AUTOSYNC_FROM_ADAT3;
2722 default:
2723 return HDSP_AUTOSYNC_FROM_WORD;
2724 }
2725 return 0;
2726}
2727
Takashi Iwai55e957d2005-11-17 14:52:13 +01002728static int snd_hdsp_info_autosync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729{
2730 static char *texts[] = {"Word", "ADAT Sync", "IEC958", "None", "ADAT1", "ADAT2", "ADAT3" };
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002731
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2733 uinfo->count = 1;
2734 uinfo->value.enumerated.items = 7;
2735 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2736 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2737 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2738 return 0;
2739}
2740
Takashi Iwai55e957d2005-11-17 14:52:13 +01002741static int snd_hdsp_get_autosync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002743 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002744
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 ucontrol->value.enumerated.item[0] = hdsp_autosync_ref(hdsp);
2746 return 0;
2747}
2748
2749#define HDSP_LINE_OUT(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002750{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 .name = xname, \
2752 .index = xindex, \
2753 .info = snd_hdsp_info_line_out, \
2754 .get = snd_hdsp_get_line_out, \
2755 .put = snd_hdsp_put_line_out \
2756}
2757
Takashi Iwai55e957d2005-11-17 14:52:13 +01002758static int hdsp_line_out(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759{
2760 return (hdsp->control_register & HDSP_LineOut) ? 1 : 0;
2761}
2762
Takashi Iwai55e957d2005-11-17 14:52:13 +01002763static int hdsp_set_line_output(struct hdsp *hdsp, int out)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764{
Takashi Iwaib0b981192005-10-20 18:29:58 +02002765 if (out)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 hdsp->control_register |= HDSP_LineOut;
Takashi Iwaib0b981192005-10-20 18:29:58 +02002767 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 hdsp->control_register &= ~HDSP_LineOut;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
2770 return 0;
2771}
2772
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002773#define snd_hdsp_info_line_out snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774
Takashi Iwai55e957d2005-11-17 14:52:13 +01002775static int snd_hdsp_get_line_out(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002777 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002778
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 spin_lock_irq(&hdsp->lock);
2780 ucontrol->value.integer.value[0] = hdsp_line_out(hdsp);
2781 spin_unlock_irq(&hdsp->lock);
2782 return 0;
2783}
2784
Takashi Iwai55e957d2005-11-17 14:52:13 +01002785static int snd_hdsp_put_line_out(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002787 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 int change;
2789 unsigned int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002790
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 if (!snd_hdsp_use_is_exclusive(hdsp))
2792 return -EBUSY;
2793 val = ucontrol->value.integer.value[0] & 1;
2794 spin_lock_irq(&hdsp->lock);
2795 change = (int)val != hdsp_line_out(hdsp);
2796 hdsp_set_line_output(hdsp, val);
2797 spin_unlock_irq(&hdsp->lock);
2798 return change;
2799}
2800
2801#define HDSP_PRECISE_POINTER(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002802{ .iface = SNDRV_CTL_ELEM_IFACE_CARD, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 .name = xname, \
2804 .index = xindex, \
2805 .info = snd_hdsp_info_precise_pointer, \
2806 .get = snd_hdsp_get_precise_pointer, \
2807 .put = snd_hdsp_put_precise_pointer \
2808}
2809
Takashi Iwai55e957d2005-11-17 14:52:13 +01002810static int hdsp_set_precise_pointer(struct hdsp *hdsp, int precise)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811{
Takashi Iwaib0b981192005-10-20 18:29:58 +02002812 if (precise)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 hdsp->precise_ptr = 1;
Takashi Iwaib0b981192005-10-20 18:29:58 +02002814 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 hdsp->precise_ptr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 return 0;
2817}
2818
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002819#define snd_hdsp_info_precise_pointer snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820
Takashi Iwai55e957d2005-11-17 14:52:13 +01002821static int snd_hdsp_get_precise_pointer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002823 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002824
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 spin_lock_irq(&hdsp->lock);
2826 ucontrol->value.integer.value[0] = hdsp->precise_ptr;
2827 spin_unlock_irq(&hdsp->lock);
2828 return 0;
2829}
2830
Takashi Iwai55e957d2005-11-17 14:52:13 +01002831static int snd_hdsp_put_precise_pointer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002833 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 int change;
2835 unsigned int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002836
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 if (!snd_hdsp_use_is_exclusive(hdsp))
2838 return -EBUSY;
2839 val = ucontrol->value.integer.value[0] & 1;
2840 spin_lock_irq(&hdsp->lock);
2841 change = (int)val != hdsp->precise_ptr;
2842 hdsp_set_precise_pointer(hdsp, val);
2843 spin_unlock_irq(&hdsp->lock);
2844 return change;
2845}
2846
2847#define HDSP_USE_MIDI_TASKLET(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002848{ .iface = SNDRV_CTL_ELEM_IFACE_CARD, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 .name = xname, \
2850 .index = xindex, \
2851 .info = snd_hdsp_info_use_midi_tasklet, \
2852 .get = snd_hdsp_get_use_midi_tasklet, \
2853 .put = snd_hdsp_put_use_midi_tasklet \
2854}
2855
Takashi Iwai55e957d2005-11-17 14:52:13 +01002856static int hdsp_set_use_midi_tasklet(struct hdsp *hdsp, int use_tasklet)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857{
Takashi Iwaib0b981192005-10-20 18:29:58 +02002858 if (use_tasklet)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 hdsp->use_midi_tasklet = 1;
Takashi Iwaib0b981192005-10-20 18:29:58 +02002860 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 hdsp->use_midi_tasklet = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 return 0;
2863}
2864
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002865#define snd_hdsp_info_use_midi_tasklet snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866
Takashi Iwai55e957d2005-11-17 14:52:13 +01002867static int snd_hdsp_get_use_midi_tasklet(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002869 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002870
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 spin_lock_irq(&hdsp->lock);
2872 ucontrol->value.integer.value[0] = hdsp->use_midi_tasklet;
2873 spin_unlock_irq(&hdsp->lock);
2874 return 0;
2875}
2876
Takashi Iwai55e957d2005-11-17 14:52:13 +01002877static int snd_hdsp_put_use_midi_tasklet(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002879 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 int change;
2881 unsigned int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002882
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 if (!snd_hdsp_use_is_exclusive(hdsp))
2884 return -EBUSY;
2885 val = ucontrol->value.integer.value[0] & 1;
2886 spin_lock_irq(&hdsp->lock);
2887 change = (int)val != hdsp->use_midi_tasklet;
2888 hdsp_set_use_midi_tasklet(hdsp, val);
2889 spin_unlock_irq(&hdsp->lock);
2890 return change;
2891}
2892
2893#define HDSP_MIXER(xname, xindex) \
2894{ .iface = SNDRV_CTL_ELEM_IFACE_HWDEP, \
2895 .name = xname, \
2896 .index = xindex, \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002897 .device = 0, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
2899 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
2900 .info = snd_hdsp_info_mixer, \
2901 .get = snd_hdsp_get_mixer, \
2902 .put = snd_hdsp_put_mixer \
2903}
2904
Takashi Iwai55e957d2005-11-17 14:52:13 +01002905static int snd_hdsp_info_mixer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906{
2907 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2908 uinfo->count = 3;
2909 uinfo->value.integer.min = 0;
2910 uinfo->value.integer.max = 65536;
2911 uinfo->value.integer.step = 1;
2912 return 0;
2913}
2914
Takashi Iwai55e957d2005-11-17 14:52:13 +01002915static int snd_hdsp_get_mixer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002917 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 int source;
2919 int destination;
2920 int addr;
2921
2922 source = ucontrol->value.integer.value[0];
2923 destination = ucontrol->value.integer.value[1];
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002924
Takashi Iwaib0b981192005-10-20 18:29:58 +02002925 if (source >= hdsp->max_channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 addr = hdsp_playback_to_output_key(hdsp,source-hdsp->max_channels,destination);
Takashi Iwaib0b981192005-10-20 18:29:58 +02002927 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 addr = hdsp_input_to_output_key(hdsp,source, destination);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002929
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 spin_lock_irq(&hdsp->lock);
2931 ucontrol->value.integer.value[2] = hdsp_read_gain (hdsp, addr);
2932 spin_unlock_irq(&hdsp->lock);
2933 return 0;
2934}
2935
Takashi Iwai55e957d2005-11-17 14:52:13 +01002936static int snd_hdsp_put_mixer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937{
Takashi Iwai55e957d2005-11-17 14:52:13 +01002938 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 int change;
2940 int source;
2941 int destination;
2942 int gain;
2943 int addr;
2944
2945 if (!snd_hdsp_use_is_exclusive(hdsp))
2946 return -EBUSY;
2947
2948 source = ucontrol->value.integer.value[0];
2949 destination = ucontrol->value.integer.value[1];
2950
Takashi Iwaib0b981192005-10-20 18:29:58 +02002951 if (source >= hdsp->max_channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 addr = hdsp_playback_to_output_key(hdsp,source-hdsp->max_channels, destination);
Takashi Iwaib0b981192005-10-20 18:29:58 +02002953 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 addr = hdsp_input_to_output_key(hdsp,source, destination);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955
2956 gain = ucontrol->value.integer.value[2];
2957
2958 spin_lock_irq(&hdsp->lock);
2959 change = gain != hdsp_read_gain(hdsp, addr);
2960 if (change)
2961 hdsp_write_gain(hdsp, addr, gain);
2962 spin_unlock_irq(&hdsp->lock);
2963 return change;
2964}
2965
2966#define HDSP_WC_SYNC_CHECK(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002967{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 .name = xname, \
2969 .index = xindex, \
2970 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
2971 .info = snd_hdsp_info_sync_check, \
2972 .get = snd_hdsp_get_wc_sync_check \
2973}
2974
Takashi Iwai55e957d2005-11-17 14:52:13 +01002975static int snd_hdsp_info_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976{
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01002977 static char *texts[] = {"No Lock", "Lock", "Sync" };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2979 uinfo->count = 1;
2980 uinfo->value.enumerated.items = 3;
2981 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2982 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2983 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2984 return 0;
2985}
2986
Takashi Iwai55e957d2005-11-17 14:52:13 +01002987static int hdsp_wc_sync_check(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988{
2989 int status2 = hdsp_read(hdsp, HDSP_status2Register);
2990 if (status2 & HDSP_wc_lock) {
Takashi Iwaib0b981192005-10-20 18:29:58 +02002991 if (status2 & HDSP_wc_sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 return 2;
Takashi Iwaib0b981192005-10-20 18:29:58 +02002993 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 return 1;
Takashi Iwaib0b981192005-10-20 18:29:58 +02002995 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 return 0;
2998}
2999
Takashi Iwai55e957d2005-11-17 14:52:13 +01003000static int snd_hdsp_get_wc_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003002 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003
3004 ucontrol->value.enumerated.item[0] = hdsp_wc_sync_check(hdsp);
3005 return 0;
3006}
3007
3008#define HDSP_SPDIF_SYNC_CHECK(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02003009{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 .name = xname, \
3011 .index = xindex, \
3012 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
3013 .info = snd_hdsp_info_sync_check, \
3014 .get = snd_hdsp_get_spdif_sync_check \
3015}
3016
Takashi Iwai55e957d2005-11-17 14:52:13 +01003017static int hdsp_spdif_sync_check(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018{
3019 int status = hdsp_read(hdsp, HDSP_statusRegister);
Takashi Iwaib0b981192005-10-20 18:29:58 +02003020 if (status & HDSP_SPDIFErrorFlag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 return 0;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003022 else {
Takashi Iwaib0b981192005-10-20 18:29:58 +02003023 if (status & HDSP_SPDIFSync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 return 2;
Takashi Iwaib0b981192005-10-20 18:29:58 +02003025 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 }
3028 return 0;
3029}
3030
Takashi Iwai55e957d2005-11-17 14:52:13 +01003031static int snd_hdsp_get_spdif_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003033 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034
3035 ucontrol->value.enumerated.item[0] = hdsp_spdif_sync_check(hdsp);
3036 return 0;
3037}
3038
3039#define HDSP_ADATSYNC_SYNC_CHECK(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02003040{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 .name = xname, \
3042 .index = xindex, \
3043 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
3044 .info = snd_hdsp_info_sync_check, \
3045 .get = snd_hdsp_get_adatsync_sync_check \
3046}
3047
Takashi Iwai55e957d2005-11-17 14:52:13 +01003048static int hdsp_adatsync_sync_check(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049{
3050 int status = hdsp_read(hdsp, HDSP_statusRegister);
3051 if (status & HDSP_TimecodeLock) {
Takashi Iwaib0b981192005-10-20 18:29:58 +02003052 if (status & HDSP_TimecodeSync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 return 2;
Takashi Iwaib0b981192005-10-20 18:29:58 +02003054 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 return 1;
Takashi Iwaib0b981192005-10-20 18:29:58 +02003056 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 return 0;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003058}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059
Takashi Iwai55e957d2005-11-17 14:52:13 +01003060static int snd_hdsp_get_adatsync_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061{
Takashi Iwai55e957d2005-11-17 14:52:13 +01003062 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063
3064 ucontrol->value.enumerated.item[0] = hdsp_adatsync_sync_check(hdsp);
3065 return 0;
3066}
3067
3068#define HDSP_ADAT_SYNC_CHECK \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02003069{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
3071 .info = snd_hdsp_info_sync_check, \
3072 .get = snd_hdsp_get_adat_sync_check \
3073}
3074
Takashi Iwai55e957d2005-11-17 14:52:13 +01003075static int hdsp_adat_sync_check(struct hdsp *hdsp, int idx)
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003076{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 int status = hdsp_read(hdsp, HDSP_statusRegister);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003078
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 if (status & (HDSP_Lock0>>idx)) {
Takashi Iwaib0b981192005-10-20 18:29:58 +02003080 if (status & (HDSP_Sync0>>idx))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 return 2;
Takashi Iwaib0b981192005-10-20 18:29:58 +02003082 else
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003083 return 1;
Takashi Iwaib0b981192005-10-20 18:29:58 +02003084 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 return 0;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003086}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087
Takashi Iwai55e957d2005-11-17 14:52:13 +01003088static int snd_hdsp_get_adat_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089{
3090 int offset;
Takashi Iwai55e957d2005-11-17 14:52:13 +01003091 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092
3093 offset = ucontrol->id.index - 1;
Takashi Iwaida3cec32008-08-08 17:12:14 +02003094 snd_BUG_ON(offset < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095
3096 switch (hdsp->io_type) {
3097 case Digiface:
3098 case H9652:
3099 if (offset >= 3)
3100 return -EINVAL;
3101 break;
3102 case Multiface:
3103 case H9632:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003104 if (offset >= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 return -EINVAL;
3106 break;
3107 default:
3108 return -EIO;
3109 }
3110
3111 ucontrol->value.enumerated.item[0] = hdsp_adat_sync_check(hdsp, offset);
3112 return 0;
3113}
3114
Julian Cablee4b60882007-03-19 11:44:40 +01003115#define HDSP_DDS_OFFSET(xname, xindex) \
3116{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3117 .name = xname, \
3118 .index = xindex, \
3119 .info = snd_hdsp_info_dds_offset, \
3120 .get = snd_hdsp_get_dds_offset, \
3121 .put = snd_hdsp_put_dds_offset \
3122}
3123
3124static int hdsp_dds_offset(struct hdsp *hdsp)
3125{
3126 u64 n;
Julian Cablee4b60882007-03-19 11:44:40 +01003127 unsigned int dds_value = hdsp->dds_value;
3128 int system_sample_rate = hdsp->system_sample_rate;
3129
Takashi Iwai2a3988f2007-10-16 14:26:32 +02003130 if (!dds_value)
3131 return 0;
3132
Julian Cablee4b60882007-03-19 11:44:40 +01003133 n = DDS_NUMERATOR;
3134 /*
3135 * dds_value = n / rate
3136 * rate = n / dds_value
3137 */
Takashi Iwai3f7440a2009-06-05 17:40:04 +02003138 n = div_u64(n, dds_value);
Julian Cablee4b60882007-03-19 11:44:40 +01003139 if (system_sample_rate >= 112000)
3140 n *= 4;
3141 else if (system_sample_rate >= 56000)
3142 n *= 2;
3143 return ((int)n) - system_sample_rate;
3144}
3145
3146static int hdsp_set_dds_offset(struct hdsp *hdsp, int offset_hz)
3147{
3148 int rate = hdsp->system_sample_rate + offset_hz;
3149 hdsp_set_dds_value(hdsp, rate);
3150 return 0;
3151}
3152
3153static int snd_hdsp_info_dds_offset(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
3154{
3155 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3156 uinfo->count = 1;
3157 uinfo->value.integer.min = -5000;
3158 uinfo->value.integer.max = 5000;
3159 return 0;
3160}
3161
3162static int snd_hdsp_get_dds_offset(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3163{
3164 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003165
Julian Cablee4b60882007-03-19 11:44:40 +01003166 ucontrol->value.enumerated.item[0] = hdsp_dds_offset(hdsp);
3167 return 0;
3168}
3169
3170static int snd_hdsp_put_dds_offset(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3171{
3172 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
3173 int change;
3174 int val;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003175
Julian Cablee4b60882007-03-19 11:44:40 +01003176 if (!snd_hdsp_use_is_exclusive(hdsp))
3177 return -EBUSY;
3178 val = ucontrol->value.enumerated.item[0];
3179 spin_lock_irq(&hdsp->lock);
3180 if (val != hdsp_dds_offset(hdsp))
3181 change = (hdsp_set_dds_offset(hdsp, val) == 0) ? 1 : 0;
3182 else
3183 change = 0;
3184 spin_unlock_irq(&hdsp->lock);
3185 return change;
3186}
3187
Takashi Iwai55e957d2005-11-17 14:52:13 +01003188static struct snd_kcontrol_new snd_hdsp_9632_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189HDSP_DA_GAIN("DA Gain", 0),
3190HDSP_AD_GAIN("AD Gain", 0),
3191HDSP_PHONE_GAIN("Phones Gain", 0),
Julian Cablee4b60882007-03-19 11:44:40 +01003192HDSP_XLR_BREAKOUT_CABLE("XLR Breakout Cable", 0),
3193HDSP_DDS_OFFSET("DDS Sample Rate Offset", 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194};
3195
Takashi Iwai55e957d2005-11-17 14:52:13 +01003196static struct snd_kcontrol_new snd_hdsp_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197{
Clemens Ladisch5549d542005-08-03 13:50:30 +02003198 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
3200 .info = snd_hdsp_control_spdif_info,
3201 .get = snd_hdsp_control_spdif_get,
3202 .put = snd_hdsp_control_spdif_put,
3203},
3204{
3205 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
Clemens Ladisch5549d542005-08-03 13:50:30 +02003206 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
3208 .info = snd_hdsp_control_spdif_stream_info,
3209 .get = snd_hdsp_control_spdif_stream_get,
3210 .put = snd_hdsp_control_spdif_stream_put,
3211},
3212{
3213 .access = SNDRV_CTL_ELEM_ACCESS_READ,
Clemens Ladisch5549d542005-08-03 13:50:30 +02003214 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
3216 .info = snd_hdsp_control_spdif_mask_info,
3217 .get = snd_hdsp_control_spdif_mask_get,
3218 .private_value = IEC958_AES0_NONAUDIO |
3219 IEC958_AES0_PROFESSIONAL |
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003220 IEC958_AES0_CON_EMPHASIS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221},
3222{
3223 .access = SNDRV_CTL_ELEM_ACCESS_READ,
Clemens Ladisch5549d542005-08-03 13:50:30 +02003224 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
3226 .info = snd_hdsp_control_spdif_mask_info,
3227 .get = snd_hdsp_control_spdif_mask_get,
3228 .private_value = IEC958_AES0_NONAUDIO |
3229 IEC958_AES0_PROFESSIONAL |
3230 IEC958_AES0_PRO_EMPHASIS,
3231},
3232HDSP_MIXER("Mixer", 0),
3233HDSP_SPDIF_IN("IEC958 Input Connector", 0),
3234HDSP_SPDIF_OUT("IEC958 Output also on ADAT1", 0),
3235HDSP_SPDIF_PROFESSIONAL("IEC958 Professional Bit", 0),
3236HDSP_SPDIF_EMPHASIS("IEC958 Emphasis Bit", 0),
3237HDSP_SPDIF_NON_AUDIO("IEC958 Non-audio Bit", 0),
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003238/* 'Sample Clock Source' complies with the alsa control naming scheme */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239HDSP_CLOCK_SOURCE("Sample Clock Source", 0),
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02003240{
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02003241 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3242 .name = "Sample Clock Source Locking",
3243 .info = snd_hdsp_info_clock_source_lock,
3244 .get = snd_hdsp_get_clock_source_lock,
3245 .put = snd_hdsp_put_clock_source_lock,
3246},
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247HDSP_SYSTEM_CLOCK_MODE("System Clock Mode", 0),
3248HDSP_PREF_SYNC_REF("Preferred Sync Reference", 0),
3249HDSP_AUTOSYNC_REF("AutoSync Reference", 0),
3250HDSP_SPDIF_SAMPLE_RATE("SPDIF Sample Rate", 0),
3251HDSP_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
3252/* 'External Rate' complies with the alsa control naming scheme */
3253HDSP_AUTOSYNC_SAMPLE_RATE("External Rate", 0),
3254HDSP_WC_SYNC_CHECK("Word Clock Lock Status", 0),
3255HDSP_SPDIF_SYNC_CHECK("SPDIF Lock Status", 0),
3256HDSP_ADATSYNC_SYNC_CHECK("ADAT Sync Lock Status", 0),
3257HDSP_LINE_OUT("Line Out", 0),
3258HDSP_PRECISE_POINTER("Precise Pointer", 0),
3259HDSP_USE_MIDI_TASKLET("Use Midi Tasklet", 0),
3260};
3261
Florian Faber28b26e12010-12-01 12:14:47 +01003262
3263static int hdsp_rpm_input12(struct hdsp *hdsp)
3264{
3265 switch (hdsp->control_register & HDSP_RPM_Inp12) {
3266 case HDSP_RPM_Inp12_Phon_6dB:
3267 return 0;
3268 case HDSP_RPM_Inp12_Phon_n6dB:
3269 return 2;
3270 case HDSP_RPM_Inp12_Line_0dB:
3271 return 3;
3272 case HDSP_RPM_Inp12_Line_n6dB:
3273 return 4;
3274 }
3275 return 1;
3276}
3277
3278
3279static int snd_hdsp_get_rpm_input12(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3280{
3281 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
3282
3283 ucontrol->value.enumerated.item[0] = hdsp_rpm_input12(hdsp);
3284 return 0;
3285}
3286
3287
3288static int hdsp_set_rpm_input12(struct hdsp *hdsp, int mode)
3289{
3290 hdsp->control_register &= ~HDSP_RPM_Inp12;
3291 switch (mode) {
3292 case 0:
3293 hdsp->control_register |= HDSP_RPM_Inp12_Phon_6dB;
3294 break;
3295 case 1:
3296 break;
3297 case 2:
3298 hdsp->control_register |= HDSP_RPM_Inp12_Phon_n6dB;
3299 break;
3300 case 3:
3301 hdsp->control_register |= HDSP_RPM_Inp12_Line_0dB;
3302 break;
3303 case 4:
3304 hdsp->control_register |= HDSP_RPM_Inp12_Line_n6dB;
3305 break;
3306 default:
3307 return -1;
3308 }
3309
3310 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
3311 return 0;
3312}
3313
3314
3315static int snd_hdsp_put_rpm_input12(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3316{
3317 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
3318 int change;
3319 int val;
3320
3321 if (!snd_hdsp_use_is_exclusive(hdsp))
3322 return -EBUSY;
3323 val = ucontrol->value.enumerated.item[0];
3324 if (val < 0)
3325 val = 0;
3326 if (val > 4)
3327 val = 4;
3328 spin_lock_irq(&hdsp->lock);
3329 if (val != hdsp_rpm_input12(hdsp))
3330 change = (hdsp_set_rpm_input12(hdsp, val) == 0) ? 1 : 0;
3331 else
3332 change = 0;
3333 spin_unlock_irq(&hdsp->lock);
3334 return change;
3335}
3336
3337
3338static int snd_hdsp_info_rpm_input(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
3339{
3340 static char *texts[] = {"Phono +6dB", "Phono 0dB", "Phono -6dB", "Line 0dB", "Line -6dB"};
3341
3342 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3343 uinfo->count = 1;
3344 uinfo->value.enumerated.items = 5;
3345 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
3346 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
3347 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
3348 return 0;
3349}
3350
3351
3352static int hdsp_rpm_input34(struct hdsp *hdsp)
3353{
3354 switch (hdsp->control_register & HDSP_RPM_Inp34) {
3355 case HDSP_RPM_Inp34_Phon_6dB:
3356 return 0;
3357 case HDSP_RPM_Inp34_Phon_n6dB:
3358 return 2;
3359 case HDSP_RPM_Inp34_Line_0dB:
3360 return 3;
3361 case HDSP_RPM_Inp34_Line_n6dB:
3362 return 4;
3363 }
3364 return 1;
3365}
3366
3367
3368static int snd_hdsp_get_rpm_input34(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3369{
3370 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
3371
3372 ucontrol->value.enumerated.item[0] = hdsp_rpm_input34(hdsp);
3373 return 0;
3374}
3375
3376
3377static int hdsp_set_rpm_input34(struct hdsp *hdsp, int mode)
3378{
3379 hdsp->control_register &= ~HDSP_RPM_Inp34;
3380 switch (mode) {
3381 case 0:
3382 hdsp->control_register |= HDSP_RPM_Inp34_Phon_6dB;
3383 break;
3384 case 1:
3385 break;
3386 case 2:
3387 hdsp->control_register |= HDSP_RPM_Inp34_Phon_n6dB;
3388 break;
3389 case 3:
3390 hdsp->control_register |= HDSP_RPM_Inp34_Line_0dB;
3391 break;
3392 case 4:
3393 hdsp->control_register |= HDSP_RPM_Inp34_Line_n6dB;
3394 break;
3395 default:
3396 return -1;
3397 }
3398
3399 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
3400 return 0;
3401}
3402
3403
3404static int snd_hdsp_put_rpm_input34(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3405{
3406 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
3407 int change;
3408 int val;
3409
3410 if (!snd_hdsp_use_is_exclusive(hdsp))
3411 return -EBUSY;
3412 val = ucontrol->value.enumerated.item[0];
3413 if (val < 0)
3414 val = 0;
3415 if (val > 4)
3416 val = 4;
3417 spin_lock_irq(&hdsp->lock);
3418 if (val != hdsp_rpm_input34(hdsp))
3419 change = (hdsp_set_rpm_input34(hdsp, val) == 0) ? 1 : 0;
3420 else
3421 change = 0;
3422 spin_unlock_irq(&hdsp->lock);
3423 return change;
3424}
3425
3426
3427/* RPM Bypass switch */
3428static int hdsp_rpm_bypass(struct hdsp *hdsp)
3429{
3430 return (hdsp->control_register & HDSP_RPM_Bypass) ? 1 : 0;
3431}
3432
3433
3434static int snd_hdsp_get_rpm_bypass(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3435{
3436 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
3437
3438 ucontrol->value.integer.value[0] = hdsp_rpm_bypass(hdsp);
3439 return 0;
3440}
3441
3442
3443static int hdsp_set_rpm_bypass(struct hdsp *hdsp, int on)
3444{
3445 if (on)
3446 hdsp->control_register |= HDSP_RPM_Bypass;
3447 else
3448 hdsp->control_register &= ~HDSP_RPM_Bypass;
3449 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
3450 return 0;
3451}
3452
3453
3454static int snd_hdsp_put_rpm_bypass(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3455{
3456 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
3457 int change;
3458 unsigned int val;
3459
3460 if (!snd_hdsp_use_is_exclusive(hdsp))
3461 return -EBUSY;
3462 val = ucontrol->value.integer.value[0] & 1;
3463 spin_lock_irq(&hdsp->lock);
3464 change = (int)val != hdsp_rpm_bypass(hdsp);
3465 hdsp_set_rpm_bypass(hdsp, val);
3466 spin_unlock_irq(&hdsp->lock);
3467 return change;
3468}
3469
3470
3471static int snd_hdsp_info_rpm_bypass(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
3472{
3473 static char *texts[] = {"On", "Off"};
3474
3475 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3476 uinfo->count = 1;
3477 uinfo->value.enumerated.items = 2;
3478 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
3479 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
3480 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
3481 return 0;
3482}
3483
3484
3485/* RPM Disconnect switch */
3486static int hdsp_rpm_disconnect(struct hdsp *hdsp)
3487{
3488 return (hdsp->control_register & HDSP_RPM_Disconnect) ? 1 : 0;
3489}
3490
3491
3492static int snd_hdsp_get_rpm_disconnect(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3493{
3494 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
3495
3496 ucontrol->value.integer.value[0] = hdsp_rpm_disconnect(hdsp);
3497 return 0;
3498}
3499
3500
3501static int hdsp_set_rpm_disconnect(struct hdsp *hdsp, int on)
3502{
3503 if (on)
3504 hdsp->control_register |= HDSP_RPM_Disconnect;
3505 else
3506 hdsp->control_register &= ~HDSP_RPM_Disconnect;
3507 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
3508 return 0;
3509}
3510
3511
3512static int snd_hdsp_put_rpm_disconnect(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3513{
3514 struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
3515 int change;
3516 unsigned int val;
3517
3518 if (!snd_hdsp_use_is_exclusive(hdsp))
3519 return -EBUSY;
3520 val = ucontrol->value.integer.value[0] & 1;
3521 spin_lock_irq(&hdsp->lock);
3522 change = (int)val != hdsp_rpm_disconnect(hdsp);
3523 hdsp_set_rpm_disconnect(hdsp, val);
3524 spin_unlock_irq(&hdsp->lock);
3525 return change;
3526}
3527
3528static int snd_hdsp_info_rpm_disconnect(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
3529{
3530 static char *texts[] = {"On", "Off"};
3531
3532 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3533 uinfo->count = 1;
3534 uinfo->value.enumerated.items = 2;
3535 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
3536 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
3537 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
3538 return 0;
3539}
3540
3541static struct snd_kcontrol_new snd_hdsp_rpm_controls[] = {
3542 {
3543 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3544 .name = "RPM Bypass",
3545 .get = snd_hdsp_get_rpm_bypass,
3546 .put = snd_hdsp_put_rpm_bypass,
3547 .info = snd_hdsp_info_rpm_bypass
3548 },
3549 {
3550 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3551 .name = "RPM Disconnect",
3552 .get = snd_hdsp_get_rpm_disconnect,
3553 .put = snd_hdsp_put_rpm_disconnect,
3554 .info = snd_hdsp_info_rpm_disconnect
3555 },
3556 {
3557 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3558 .name = "Input 1/2",
3559 .get = snd_hdsp_get_rpm_input12,
3560 .put = snd_hdsp_put_rpm_input12,
3561 .info = snd_hdsp_info_rpm_input
3562 },
3563 {
3564 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3565 .name = "Input 3/4",
3566 .get = snd_hdsp_get_rpm_input34,
3567 .put = snd_hdsp_put_rpm_input34,
3568 .info = snd_hdsp_info_rpm_input
3569 },
3570 HDSP_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
3571 HDSP_MIXER("Mixer", 0)
3572};
3573
Takashi Iwai55e957d2005-11-17 14:52:13 +01003574static struct snd_kcontrol_new snd_hdsp_96xx_aeb = HDSP_AEB("Analog Extension Board", 0);
3575static struct snd_kcontrol_new snd_hdsp_adat_sync_check = HDSP_ADAT_SYNC_CHECK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576
Takashi Iwai55e957d2005-11-17 14:52:13 +01003577static int snd_hdsp_create_controls(struct snd_card *card, struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578{
3579 unsigned int idx;
3580 int err;
Takashi Iwai55e957d2005-11-17 14:52:13 +01003581 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582
Florian Faber28b26e12010-12-01 12:14:47 +01003583 if (hdsp->io_type == RPM) {
3584 /* RPM Bypass, Disconnect and Input switches */
3585 for (idx = 0; idx < ARRAY_SIZE(snd_hdsp_rpm_controls); idx++) {
3586 err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_rpm_controls[idx], hdsp));
3587 if (err < 0)
3588 return err;
3589 }
3590 return 0;
3591 }
3592
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 for (idx = 0; idx < ARRAY_SIZE(snd_hdsp_controls); idx++) {
Takashi Iwaib0b981192005-10-20 18:29:58 +02003594 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_controls[idx], hdsp))) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 if (idx == 1) /* IEC958 (S/PDIF) Stream */
3597 hdsp->spdif_ctl = kctl;
3598 }
3599
3600 /* ADAT SyncCheck status */
3601 snd_hdsp_adat_sync_check.name = "ADAT Lock Status";
3602 snd_hdsp_adat_sync_check.index = 1;
Takashi Iwaib0b981192005-10-20 18:29:58 +02003603 if ((err = snd_ctl_add (card, kctl = snd_ctl_new1(&snd_hdsp_adat_sync_check, hdsp))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 if (hdsp->io_type == Digiface || hdsp->io_type == H9652) {
3606 for (idx = 1; idx < 3; ++idx) {
3607 snd_hdsp_adat_sync_check.index = idx+1;
Takashi Iwaib0b981192005-10-20 18:29:58 +02003608 if ((err = snd_ctl_add (card, kctl = snd_ctl_new1(&snd_hdsp_adat_sync_check, hdsp))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 }
3611 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003612
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 /* DA, AD and Phone gain and XLR breakout cable controls for H9632 cards */
3614 if (hdsp->io_type == H9632) {
3615 for (idx = 0; idx < ARRAY_SIZE(snd_hdsp_9632_controls); idx++) {
Takashi Iwaib0b981192005-10-20 18:29:58 +02003616 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_9632_controls[idx], hdsp))) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 }
3619 }
3620
3621 /* AEB control for H96xx card */
3622 if (hdsp->io_type == H9632 || hdsp->io_type == H9652) {
Takashi Iwaib0b981192005-10-20 18:29:58 +02003623 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_96xx_aeb, hdsp))) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 }
3626
3627 return 0;
3628}
3629
3630/*------------------------------------------------------------
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003631 /proc interface
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 ------------------------------------------------------------*/
3633
3634static void
Takashi Iwai55e957d2005-11-17 14:52:13 +01003635snd_hdsp_proc_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636{
Joe Perches9fe856e2010-09-04 18:52:54 -07003637 struct hdsp *hdsp = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 unsigned int status;
3639 unsigned int status2;
3640 char *pref_sync_ref;
3641 char *autosync_ref;
3642 char *system_clock_mode;
3643 char *clock_source;
3644 int x;
3645
Tim Blechmannc18bc9b2009-08-12 18:21:30 +02003646 status = hdsp_read(hdsp, HDSP_statusRegister);
3647 status2 = hdsp_read(hdsp, HDSP_status2Register);
3648
3649 snd_iprintf(buffer, "%s (Card #%d)\n", hdsp->card_name,
3650 hdsp->card->number + 1);
3651 snd_iprintf(buffer, "Buffers: capture %p playback %p\n",
3652 hdsp->capture_buffer, hdsp->playback_buffer);
3653 snd_iprintf(buffer, "IRQ: %d Registers bus: 0x%lx VM: 0x%lx\n",
3654 hdsp->irq, hdsp->port, (unsigned long)hdsp->iobase);
3655 snd_iprintf(buffer, "Control register: 0x%x\n", hdsp->control_register);
3656 snd_iprintf(buffer, "Control2 register: 0x%x\n",
3657 hdsp->control2_register);
3658 snd_iprintf(buffer, "Status register: 0x%x\n", status);
3659 snd_iprintf(buffer, "Status2 register: 0x%x\n", status2);
3660
3661 if (hdsp_check_for_iobox(hdsp)) {
3662 snd_iprintf(buffer, "No I/O box connected.\n"
3663 "Please connect one and upload firmware.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 return;
Tim Blechmannc18bc9b2009-08-12 18:21:30 +02003665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666
Takashi Iwaib0b981192005-10-20 18:29:58 +02003667 if (hdsp_check_for_firmware(hdsp, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 if (hdsp->state & HDSP_FirmwareCached) {
3669 if (snd_hdsp_load_firmware_from_cache(hdsp) != 0) {
Tim Blechmannc18bc9b2009-08-12 18:21:30 +02003670 snd_iprintf(buffer, "Firmware loading from "
3671 "cache failed, "
3672 "please upload manually.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 return;
3674 }
3675 } else {
Takashi Iwai311e70a2006-09-06 12:13:37 +02003676 int err = -EINVAL;
Takashi Iwai311e70a2006-09-06 12:13:37 +02003677 err = hdsp_request_fw_loader(hdsp);
Takashi Iwai311e70a2006-09-06 12:13:37 +02003678 if (err < 0) {
3679 snd_iprintf(buffer,
3680 "No firmware loaded nor cached, "
3681 "please upload firmware.\n");
3682 return;
3683 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 }
3685 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003686
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 snd_iprintf(buffer, "FIFO status: %d\n", hdsp_read(hdsp, HDSP_fifoStatus) & 0xff);
3688 snd_iprintf(buffer, "MIDI1 Output status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusOut0));
3689 snd_iprintf(buffer, "MIDI1 Input status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusIn0));
3690 snd_iprintf(buffer, "MIDI2 Output status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusOut1));
3691 snd_iprintf(buffer, "MIDI2 Input status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusIn1));
3692 snd_iprintf(buffer, "Use Midi Tasklet: %s\n", hdsp->use_midi_tasklet ? "on" : "off");
3693
3694 snd_iprintf(buffer, "\n");
3695
3696 x = 1 << (6 + hdsp_decode_latency(hdsp->control_register & HDSP_LatencyMask));
3697
3698 snd_iprintf(buffer, "Buffer Size (Latency): %d samples (2 periods of %lu bytes)\n", x, (unsigned long) hdsp->period_bytes);
3699 snd_iprintf(buffer, "Hardware pointer (frames): %ld\n", hdsp_hw_pointer(hdsp));
3700 snd_iprintf(buffer, "Precise pointer: %s\n", hdsp->precise_ptr ? "on" : "off");
3701 snd_iprintf(buffer, "Line out: %s\n", (hdsp->control_register & HDSP_LineOut) ? "on" : "off");
3702
3703 snd_iprintf(buffer, "Firmware version: %d\n", (status2&HDSP_version0)|(status2&HDSP_version1)<<1|(status2&HDSP_version2)<<2);
3704
3705 snd_iprintf(buffer, "\n");
3706
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 switch (hdsp_clock_source(hdsp)) {
3708 case HDSP_CLOCK_SOURCE_AUTOSYNC:
3709 clock_source = "AutoSync";
3710 break;
3711 case HDSP_CLOCK_SOURCE_INTERNAL_32KHZ:
3712 clock_source = "Internal 32 kHz";
3713 break;
3714 case HDSP_CLOCK_SOURCE_INTERNAL_44_1KHZ:
3715 clock_source = "Internal 44.1 kHz";
3716 break;
3717 case HDSP_CLOCK_SOURCE_INTERNAL_48KHZ:
3718 clock_source = "Internal 48 kHz";
3719 break;
3720 case HDSP_CLOCK_SOURCE_INTERNAL_64KHZ:
3721 clock_source = "Internal 64 kHz";
3722 break;
3723 case HDSP_CLOCK_SOURCE_INTERNAL_88_2KHZ:
3724 clock_source = "Internal 88.2 kHz";
3725 break;
3726 case HDSP_CLOCK_SOURCE_INTERNAL_96KHZ:
3727 clock_source = "Internal 96 kHz";
3728 break;
3729 case HDSP_CLOCK_SOURCE_INTERNAL_128KHZ:
3730 clock_source = "Internal 128 kHz";
3731 break;
3732 case HDSP_CLOCK_SOURCE_INTERNAL_176_4KHZ:
3733 clock_source = "Internal 176.4 kHz";
3734 break;
3735 case HDSP_CLOCK_SOURCE_INTERNAL_192KHZ:
3736 clock_source = "Internal 192 kHz";
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003737 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 default:
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003739 clock_source = "Error";
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 }
3741 snd_iprintf (buffer, "Sample Clock Source: %s\n", clock_source);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003742
Takashi Iwaib0b981192005-10-20 18:29:58 +02003743 if (hdsp_system_clock_mode(hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 system_clock_mode = "Slave";
Takashi Iwaib0b981192005-10-20 18:29:58 +02003745 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746 system_clock_mode = "Master";
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003747
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 switch (hdsp_pref_sync_ref (hdsp)) {
3749 case HDSP_SYNC_FROM_WORD:
3750 pref_sync_ref = "Word Clock";
3751 break;
3752 case HDSP_SYNC_FROM_ADAT_SYNC:
3753 pref_sync_ref = "ADAT Sync";
3754 break;
3755 case HDSP_SYNC_FROM_SPDIF:
3756 pref_sync_ref = "SPDIF";
3757 break;
3758 case HDSP_SYNC_FROM_ADAT1:
3759 pref_sync_ref = "ADAT1";
3760 break;
3761 case HDSP_SYNC_FROM_ADAT2:
3762 pref_sync_ref = "ADAT2";
3763 break;
3764 case HDSP_SYNC_FROM_ADAT3:
3765 pref_sync_ref = "ADAT3";
3766 break;
3767 default:
3768 pref_sync_ref = "Word Clock";
3769 break;
3770 }
3771 snd_iprintf (buffer, "Preferred Sync Reference: %s\n", pref_sync_ref);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003772
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 switch (hdsp_autosync_ref (hdsp)) {
3774 case HDSP_AUTOSYNC_FROM_WORD:
3775 autosync_ref = "Word Clock";
3776 break;
3777 case HDSP_AUTOSYNC_FROM_ADAT_SYNC:
3778 autosync_ref = "ADAT Sync";
3779 break;
3780 case HDSP_AUTOSYNC_FROM_SPDIF:
3781 autosync_ref = "SPDIF";
3782 break;
3783 case HDSP_AUTOSYNC_FROM_NONE:
3784 autosync_ref = "None";
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003785 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 case HDSP_AUTOSYNC_FROM_ADAT1:
3787 autosync_ref = "ADAT1";
3788 break;
3789 case HDSP_AUTOSYNC_FROM_ADAT2:
3790 autosync_ref = "ADAT2";
3791 break;
3792 case HDSP_AUTOSYNC_FROM_ADAT3:
3793 autosync_ref = "ADAT3";
3794 break;
3795 default:
3796 autosync_ref = "---";
3797 break;
3798 }
3799 snd_iprintf (buffer, "AutoSync Reference: %s\n", autosync_ref);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003800
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 snd_iprintf (buffer, "AutoSync Frequency: %d\n", hdsp_external_sample_rate(hdsp));
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003802
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 snd_iprintf (buffer, "System Clock Mode: %s\n", system_clock_mode);
3804
3805 snd_iprintf (buffer, "System Clock Frequency: %d\n", hdsp->system_sample_rate);
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02003806 snd_iprintf (buffer, "System Clock Locked: %s\n", hdsp->clock_source_locked ? "Yes" : "No");
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003807
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 snd_iprintf(buffer, "\n");
3809
Florian Faber28b26e12010-12-01 12:14:47 +01003810 if (hdsp->io_type != RPM) {
3811 switch (hdsp_spdif_in(hdsp)) {
3812 case HDSP_SPDIFIN_OPTICAL:
3813 snd_iprintf(buffer, "IEC958 input: Optical\n");
3814 break;
3815 case HDSP_SPDIFIN_COAXIAL:
3816 snd_iprintf(buffer, "IEC958 input: Coaxial\n");
3817 break;
3818 case HDSP_SPDIFIN_INTERNAL:
3819 snd_iprintf(buffer, "IEC958 input: Internal\n");
3820 break;
3821 case HDSP_SPDIFIN_AES:
3822 snd_iprintf(buffer, "IEC958 input: AES\n");
3823 break;
3824 default:
3825 snd_iprintf(buffer, "IEC958 input: ???\n");
3826 break;
3827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003829
Florian Faber28b26e12010-12-01 12:14:47 +01003830 if (RPM == hdsp->io_type) {
3831 if (hdsp->control_register & HDSP_RPM_Bypass)
3832 snd_iprintf(buffer, "RPM Bypass: disabled\n");
3833 else
3834 snd_iprintf(buffer, "RPM Bypass: enabled\n");
3835 if (hdsp->control_register & HDSP_RPM_Disconnect)
3836 snd_iprintf(buffer, "RPM disconnected\n");
3837 else
3838 snd_iprintf(buffer, "RPM connected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839
Florian Faber28b26e12010-12-01 12:14:47 +01003840 switch (hdsp->control_register & HDSP_RPM_Inp12) {
3841 case HDSP_RPM_Inp12_Phon_6dB:
3842 snd_iprintf(buffer, "Input 1/2: Phono, 6dB\n");
3843 break;
3844 case HDSP_RPM_Inp12_Phon_0dB:
3845 snd_iprintf(buffer, "Input 1/2: Phono, 0dB\n");
3846 break;
3847 case HDSP_RPM_Inp12_Phon_n6dB:
3848 snd_iprintf(buffer, "Input 1/2: Phono, -6dB\n");
3849 break;
3850 case HDSP_RPM_Inp12_Line_0dB:
3851 snd_iprintf(buffer, "Input 1/2: Line, 0dB\n");
3852 break;
3853 case HDSP_RPM_Inp12_Line_n6dB:
3854 snd_iprintf(buffer, "Input 1/2: Line, -6dB\n");
3855 break;
3856 default:
3857 snd_iprintf(buffer, "Input 1/2: ???\n");
3858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859
Florian Faber28b26e12010-12-01 12:14:47 +01003860 switch (hdsp->control_register & HDSP_RPM_Inp34) {
3861 case HDSP_RPM_Inp34_Phon_6dB:
3862 snd_iprintf(buffer, "Input 3/4: Phono, 6dB\n");
3863 break;
3864 case HDSP_RPM_Inp34_Phon_0dB:
3865 snd_iprintf(buffer, "Input 3/4: Phono, 0dB\n");
3866 break;
3867 case HDSP_RPM_Inp34_Phon_n6dB:
3868 snd_iprintf(buffer, "Input 3/4: Phono, -6dB\n");
3869 break;
3870 case HDSP_RPM_Inp34_Line_0dB:
3871 snd_iprintf(buffer, "Input 3/4: Line, 0dB\n");
3872 break;
3873 case HDSP_RPM_Inp34_Line_n6dB:
3874 snd_iprintf(buffer, "Input 3/4: Line, -6dB\n");
3875 break;
3876 default:
3877 snd_iprintf(buffer, "Input 3/4: ???\n");
3878 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879
Florian Faber28b26e12010-12-01 12:14:47 +01003880 } else {
3881 if (hdsp->control_register & HDSP_SPDIFOpticalOut)
3882 snd_iprintf(buffer, "IEC958 output: Coaxial & ADAT1\n");
3883 else
3884 snd_iprintf(buffer, "IEC958 output: Coaxial only\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885
Florian Faber28b26e12010-12-01 12:14:47 +01003886 if (hdsp->control_register & HDSP_SPDIFProfessional)
3887 snd_iprintf(buffer, "IEC958 quality: Professional\n");
3888 else
3889 snd_iprintf(buffer, "IEC958 quality: Consumer\n");
3890
3891 if (hdsp->control_register & HDSP_SPDIFEmphasis)
3892 snd_iprintf(buffer, "IEC958 emphasis: on\n");
3893 else
3894 snd_iprintf(buffer, "IEC958 emphasis: off\n");
3895
3896 if (hdsp->control_register & HDSP_SPDIFNonAudio)
3897 snd_iprintf(buffer, "IEC958 NonAudio: on\n");
3898 else
3899 snd_iprintf(buffer, "IEC958 NonAudio: off\n");
3900 x = hdsp_spdif_sample_rate(hdsp);
3901 if (x != 0)
3902 snd_iprintf(buffer, "IEC958 sample rate: %d\n", x);
3903 else
3904 snd_iprintf(buffer, "IEC958 sample rate: Error flag set\n");
3905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906 snd_iprintf(buffer, "\n");
3907
3908 /* Sync Check */
3909 x = status & HDSP_Sync0;
Takashi Iwaib0b981192005-10-20 18:29:58 +02003910 if (status & HDSP_Lock0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911 snd_iprintf(buffer, "ADAT1: %s\n", x ? "Sync" : "Lock");
Takashi Iwaib0b981192005-10-20 18:29:58 +02003912 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 snd_iprintf(buffer, "ADAT1: No Lock\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914
3915 switch (hdsp->io_type) {
3916 case Digiface:
3917 case H9652:
3918 x = status & HDSP_Sync1;
Takashi Iwaib0b981192005-10-20 18:29:58 +02003919 if (status & HDSP_Lock1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920 snd_iprintf(buffer, "ADAT2: %s\n", x ? "Sync" : "Lock");
Takashi Iwaib0b981192005-10-20 18:29:58 +02003921 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922 snd_iprintf(buffer, "ADAT2: No Lock\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 x = status & HDSP_Sync2;
Takashi Iwaib0b981192005-10-20 18:29:58 +02003924 if (status & HDSP_Lock2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 snd_iprintf(buffer, "ADAT3: %s\n", x ? "Sync" : "Lock");
Takashi Iwaib0b981192005-10-20 18:29:58 +02003926 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 snd_iprintf(buffer, "ADAT3: No Lock\n");
Takashi Iwaib0b981192005-10-20 18:29:58 +02003928 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 default:
3930 /* relax */
3931 break;
3932 }
3933
3934 x = status & HDSP_SPDIFSync;
Takashi Iwaib0b981192005-10-20 18:29:58 +02003935 if (status & HDSP_SPDIFErrorFlag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 snd_iprintf (buffer, "SPDIF: No Lock\n");
Takashi Iwaib0b981192005-10-20 18:29:58 +02003937 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 snd_iprintf (buffer, "SPDIF: %s\n", x ? "Sync" : "Lock");
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003939
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 x = status2 & HDSP_wc_sync;
Takashi Iwaib0b981192005-10-20 18:29:58 +02003941 if (status2 & HDSP_wc_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 snd_iprintf (buffer, "Word Clock: %s\n", x ? "Sync" : "Lock");
Takashi Iwaib0b981192005-10-20 18:29:58 +02003943 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 snd_iprintf (buffer, "Word Clock: No Lock\n");
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003945
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 x = status & HDSP_TimecodeSync;
Takashi Iwaib0b981192005-10-20 18:29:58 +02003947 if (status & HDSP_TimecodeLock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 snd_iprintf(buffer, "ADAT Sync: %s\n", x ? "Sync" : "Lock");
Takashi Iwaib0b981192005-10-20 18:29:58 +02003949 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 snd_iprintf(buffer, "ADAT Sync: No Lock\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951
3952 snd_iprintf(buffer, "\n");
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003953
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 /* Informations about H9632 specific controls */
3955 if (hdsp->io_type == H9632) {
3956 char *tmp;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003957
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 switch (hdsp_ad_gain(hdsp)) {
3959 case 0:
3960 tmp = "-10 dBV";
3961 break;
3962 case 1:
3963 tmp = "+4 dBu";
3964 break;
3965 default:
3966 tmp = "Lo Gain";
3967 break;
3968 }
3969 snd_iprintf(buffer, "AD Gain : %s\n", tmp);
3970
3971 switch (hdsp_da_gain(hdsp)) {
3972 case 0:
3973 tmp = "Hi Gain";
3974 break;
3975 case 1:
3976 tmp = "+4 dBu";
3977 break;
3978 default:
3979 tmp = "-10 dBV";
3980 break;
3981 }
3982 snd_iprintf(buffer, "DA Gain : %s\n", tmp);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003983
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 switch (hdsp_phone_gain(hdsp)) {
3985 case 0:
3986 tmp = "0 dB";
3987 break;
3988 case 1:
3989 tmp = "-6 dB";
3990 break;
3991 default:
3992 tmp = "-12 dB";
3993 break;
3994 }
3995 snd_iprintf(buffer, "Phones Gain : %s\n", tmp);
3996
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01003997 snd_iprintf(buffer, "XLR Breakout Cable : %s\n", hdsp_xlr_breakout_cable(hdsp) ? "yes" : "no");
3998
Takashi Iwaib0b981192005-10-20 18:29:58 +02003999 if (hdsp->control_register & HDSP_AnalogExtensionBoard)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 snd_iprintf(buffer, "AEB : on (ADAT1 internal)\n");
Takashi Iwaib0b981192005-10-20 18:29:58 +02004001 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 snd_iprintf(buffer, "AEB : off (ADAT1 external)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 snd_iprintf(buffer, "\n");
4004 }
4005
4006}
4007
Randy Dunlap1374f8c2008-01-16 14:55:42 +01004008static void snd_hdsp_proc_init(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004010 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011
4012 if (! snd_card_proc_new(hdsp->card, "hdsp", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02004013 snd_info_set_text_ops(entry, hdsp, snd_hdsp_proc_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014}
4015
Takashi Iwai55e957d2005-11-17 14:52:13 +01004016static void snd_hdsp_free_buffers(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017{
4018 snd_hammerfall_free_buffer(&hdsp->capture_dma_buf, hdsp->pci);
4019 snd_hammerfall_free_buffer(&hdsp->playback_dma_buf, hdsp->pci);
4020}
4021
Takashi Iwai55e957d2005-11-17 14:52:13 +01004022static int __devinit snd_hdsp_initialize_memory(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023{
4024 unsigned long pb_bus, cb_bus;
4025
4026 if (snd_hammerfall_get_buffer(hdsp->pci, &hdsp->capture_dma_buf, HDSP_DMA_AREA_BYTES) < 0 ||
4027 snd_hammerfall_get_buffer(hdsp->pci, &hdsp->playback_dma_buf, HDSP_DMA_AREA_BYTES) < 0) {
4028 if (hdsp->capture_dma_buf.area)
4029 snd_dma_free_pages(&hdsp->capture_dma_buf);
4030 printk(KERN_ERR "%s: no buffers available\n", hdsp->card_name);
4031 return -ENOMEM;
4032 }
4033
4034 /* Align to bus-space 64K boundary */
4035
Clemens Ladisch7ab39922006-10-09 08:13:32 +02004036 cb_bus = ALIGN(hdsp->capture_dma_buf.addr, 0x10000ul);
4037 pb_bus = ALIGN(hdsp->playback_dma_buf.addr, 0x10000ul);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038
4039 /* Tell the card where it is */
4040
4041 hdsp_write(hdsp, HDSP_inputBufferAddress, cb_bus);
4042 hdsp_write(hdsp, HDSP_outputBufferAddress, pb_bus);
4043
4044 hdsp->capture_buffer = hdsp->capture_dma_buf.area + (cb_bus - hdsp->capture_dma_buf.addr);
4045 hdsp->playback_buffer = hdsp->playback_dma_buf.area + (pb_bus - hdsp->playback_dma_buf.addr);
4046
4047 return 0;
4048}
4049
Takashi Iwai55e957d2005-11-17 14:52:13 +01004050static int snd_hdsp_set_defaults(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051{
4052 unsigned int i;
4053
4054 /* ASSUMPTION: hdsp->lock is either held, or
4055 there is no need to hold it (e.g. during module
Joe Perches561de312007-12-18 13:13:47 +01004056 initialization).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 */
4058
4059 /* set defaults:
4060
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004061 SPDIF Input via Coax
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 Master clock mode
4063 maximum latency (7 => 2^7 = 8192 samples, 64Kbyte buffer,
4064 which implies 2 4096 sample, 32Kbyte periods).
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004065 Enable line out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 */
4067
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004068 hdsp->control_register = HDSP_ClockModeMaster |
4069 HDSP_SPDIFInputCoaxial |
4070 hdsp_encode_latency(7) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 HDSP_LineOut;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004072
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073
4074 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
4075
4076#ifdef SNDRV_BIG_ENDIAN
4077 hdsp->control2_register = HDSP_BIGENDIAN_MODE;
4078#else
4079 hdsp->control2_register = 0;
4080#endif
Takashi Iwaib0b981192005-10-20 18:29:58 +02004081 if (hdsp->io_type == H9652)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082 snd_hdsp_9652_enable_mixer (hdsp);
Takashi Iwaib0b981192005-10-20 18:29:58 +02004083 else
4084 hdsp_write (hdsp, HDSP_control2Reg, hdsp->control2_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085
4086 hdsp_reset_hw_pointer(hdsp);
4087 hdsp_compute_period_size(hdsp);
4088
4089 /* silence everything */
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004090
Takashi Iwaib0b981192005-10-20 18:29:58 +02004091 for (i = 0; i < HDSP_MATRIX_MIXER_SIZE; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 hdsp->mixer_matrix[i] = MINUS_INFINITY_GAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093
4094 for (i = 0; i < ((hdsp->io_type == H9652 || hdsp->io_type == H9632) ? 1352 : HDSP_MATRIX_MIXER_SIZE); ++i) {
Takashi Iwaib0b981192005-10-20 18:29:58 +02004095 if (hdsp_write_gain (hdsp, i, MINUS_INFINITY_GAIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004098
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 /* H9632 specific defaults */
4100 if (hdsp->io_type == H9632) {
4101 hdsp->control_register |= (HDSP_DAGainPlus4dBu | HDSP_ADGainPlus4dBu | HDSP_PhoneGain0dB);
4102 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
4103 }
4104
4105 /* set a default rate so that the channel map is set up.
4106 */
4107
4108 hdsp_set_rate(hdsp, 48000, 1);
4109
4110 return 0;
4111}
4112
4113static void hdsp_midi_tasklet(unsigned long arg)
4114{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004115 struct hdsp *hdsp = (struct hdsp *)arg;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004116
Takashi Iwaib0b981192005-10-20 18:29:58 +02004117 if (hdsp->midi[0].pending)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 snd_hdsp_midi_input_read (&hdsp->midi[0]);
Takashi Iwaib0b981192005-10-20 18:29:58 +02004119 if (hdsp->midi[1].pending)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 snd_hdsp_midi_input_read (&hdsp->midi[1]);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004121}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122
David Howells7d12e782006-10-05 14:55:46 +01004123static irqreturn_t snd_hdsp_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004125 struct hdsp *hdsp = (struct hdsp *) dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 unsigned int status;
4127 int audio;
4128 int midi0;
4129 int midi1;
4130 unsigned int midi0status;
4131 unsigned int midi1status;
4132 int schedule = 0;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004133
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 status = hdsp_read(hdsp, HDSP_statusRegister);
4135
4136 audio = status & HDSP_audioIRQPending;
4137 midi0 = status & HDSP_midi0IRQPending;
4138 midi1 = status & HDSP_midi1IRQPending;
4139
Takashi Iwaib0b981192005-10-20 18:29:58 +02004140 if (!audio && !midi0 && !midi1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142
4143 hdsp_write(hdsp, HDSP_interruptConfirmation, 0);
4144
4145 midi0status = hdsp_read (hdsp, HDSP_midiStatusIn0) & 0xff;
4146 midi1status = hdsp_read (hdsp, HDSP_midiStatusIn1) & 0xff;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004147
Takashi Iwaic2503cd2009-03-05 09:37:40 +01004148 if (!(hdsp->state & HDSP_InitializationComplete))
4149 return IRQ_HANDLED;
4150
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 if (audio) {
Takashi Iwaib0b981192005-10-20 18:29:58 +02004152 if (hdsp->capture_substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 snd_pcm_period_elapsed(hdsp->pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004154
Takashi Iwaib0b981192005-10-20 18:29:58 +02004155 if (hdsp->playback_substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 snd_pcm_period_elapsed(hdsp->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004158
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 if (midi0 && midi0status) {
4160 if (hdsp->use_midi_tasklet) {
4161 /* we disable interrupts for this input until processing is done */
4162 hdsp->control_register &= ~HDSP_Midi0InterruptEnable;
4163 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
4164 hdsp->midi[0].pending = 1;
4165 schedule = 1;
4166 } else {
4167 snd_hdsp_midi_input_read (&hdsp->midi[0]);
4168 }
4169 }
Florian Faber28b26e12010-12-01 12:14:47 +01004170 if (hdsp->io_type != Multiface && hdsp->io_type != RPM && hdsp->io_type != H9632 && midi1 && midi1status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 if (hdsp->use_midi_tasklet) {
4172 /* we disable interrupts for this input until processing is done */
4173 hdsp->control_register &= ~HDSP_Midi1InterruptEnable;
4174 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
4175 hdsp->midi[1].pending = 1;
4176 schedule = 1;
4177 } else {
4178 snd_hdsp_midi_input_read (&hdsp->midi[1]);
4179 }
4180 }
4181 if (hdsp->use_midi_tasklet && schedule)
Takashi Iwai1f041282008-12-18 12:17:55 +01004182 tasklet_schedule(&hdsp->midi_tasklet);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 return IRQ_HANDLED;
4184}
4185
Takashi Iwai55e957d2005-11-17 14:52:13 +01004186static snd_pcm_uframes_t snd_hdsp_hw_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004188 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 return hdsp_hw_pointer(hdsp);
4190}
4191
Takashi Iwai55e957d2005-11-17 14:52:13 +01004192static char *hdsp_channel_buffer_location(struct hdsp *hdsp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 int stream,
4194 int channel)
4195
4196{
4197 int mapped_channel;
4198
Takashi Iwaida3cec32008-08-08 17:12:14 +02004199 if (snd_BUG_ON(channel < 0 || channel >= hdsp->max_channels))
4200 return NULL;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004201
Takashi Iwaib0b981192005-10-20 18:29:58 +02004202 if ((mapped_channel = hdsp->channel_map[channel]) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 return NULL;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004204
Takashi Iwaib0b981192005-10-20 18:29:58 +02004205 if (stream == SNDRV_PCM_STREAM_CAPTURE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 return hdsp->capture_buffer + (mapped_channel * HDSP_CHANNEL_BUFFER_BYTES);
Takashi Iwaib0b981192005-10-20 18:29:58 +02004207 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 return hdsp->playback_buffer + (mapped_channel * HDSP_CHANNEL_BUFFER_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209}
4210
Takashi Iwai55e957d2005-11-17 14:52:13 +01004211static int snd_hdsp_playback_copy(struct snd_pcm_substream *substream, int channel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 snd_pcm_uframes_t pos, void __user *src, snd_pcm_uframes_t count)
4213{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004214 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 char *channel_buf;
4216
Takashi Iwaida3cec32008-08-08 17:12:14 +02004217 if (snd_BUG_ON(pos + count > HDSP_CHANNEL_BUFFER_BYTES / 4))
4218 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219
4220 channel_buf = hdsp_channel_buffer_location (hdsp, substream->pstr->stream, channel);
Takashi Iwaida3cec32008-08-08 17:12:14 +02004221 if (snd_BUG_ON(!channel_buf))
4222 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 if (copy_from_user(channel_buf + pos * 4, src, count * 4))
4224 return -EFAULT;
4225 return count;
4226}
4227
Takashi Iwai55e957d2005-11-17 14:52:13 +01004228static int snd_hdsp_capture_copy(struct snd_pcm_substream *substream, int channel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 snd_pcm_uframes_t pos, void __user *dst, snd_pcm_uframes_t count)
4230{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004231 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 char *channel_buf;
4233
Takashi Iwaida3cec32008-08-08 17:12:14 +02004234 if (snd_BUG_ON(pos + count > HDSP_CHANNEL_BUFFER_BYTES / 4))
4235 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236
4237 channel_buf = hdsp_channel_buffer_location (hdsp, substream->pstr->stream, channel);
Takashi Iwaida3cec32008-08-08 17:12:14 +02004238 if (snd_BUG_ON(!channel_buf))
4239 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 if (copy_to_user(dst, channel_buf + pos * 4, count * 4))
4241 return -EFAULT;
4242 return count;
4243}
4244
Takashi Iwai55e957d2005-11-17 14:52:13 +01004245static int snd_hdsp_hw_silence(struct snd_pcm_substream *substream, int channel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 snd_pcm_uframes_t pos, snd_pcm_uframes_t count)
4247{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004248 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249 char *channel_buf;
4250
4251 channel_buf = hdsp_channel_buffer_location (hdsp, substream->pstr->stream, channel);
Takashi Iwaida3cec32008-08-08 17:12:14 +02004252 if (snd_BUG_ON(!channel_buf))
4253 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 memset(channel_buf + pos * 4, 0, count * 4);
4255 return count;
4256}
4257
Takashi Iwai55e957d2005-11-17 14:52:13 +01004258static int snd_hdsp_reset(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004260 struct snd_pcm_runtime *runtime = substream->runtime;
4261 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
4262 struct snd_pcm_substream *other;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
4264 other = hdsp->capture_substream;
4265 else
4266 other = hdsp->playback_substream;
4267 if (hdsp->running)
4268 runtime->status->hw_ptr = hdsp_hw_pointer(hdsp);
4269 else
4270 runtime->status->hw_ptr = 0;
4271 if (other) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004272 struct snd_pcm_substream *s;
4273 struct snd_pcm_runtime *oruntime = other->runtime;
Takashi Iwaief991b92007-02-22 12:52:53 +01004274 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 if (s == other) {
4276 oruntime->status->hw_ptr = runtime->status->hw_ptr;
4277 break;
4278 }
4279 }
4280 }
4281 return 0;
4282}
4283
Takashi Iwai55e957d2005-11-17 14:52:13 +01004284static int snd_hdsp_hw_params(struct snd_pcm_substream *substream,
4285 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004287 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 int err;
4289 pid_t this_pid;
4290 pid_t other_pid;
4291
Takashi Iwaib0b981192005-10-20 18:29:58 +02004292 if (hdsp_check_for_iobox (hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294
Takashi Iwaib0b981192005-10-20 18:29:58 +02004295 if (hdsp_check_for_firmware(hdsp, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297
4298 spin_lock_irq(&hdsp->lock);
4299
4300 if (substream->pstr->stream == SNDRV_PCM_STREAM_PLAYBACK) {
4301 hdsp->control_register &= ~(HDSP_SPDIFProfessional | HDSP_SPDIFNonAudio | HDSP_SPDIFEmphasis);
4302 hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register |= hdsp->creg_spdif_stream);
4303 this_pid = hdsp->playback_pid;
4304 other_pid = hdsp->capture_pid;
4305 } else {
4306 this_pid = hdsp->capture_pid;
4307 other_pid = hdsp->playback_pid;
4308 }
4309
4310 if ((other_pid > 0) && (this_pid != other_pid)) {
4311
4312 /* The other stream is open, and not by the same
4313 task as this one. Make sure that the parameters
4314 that matter are the same.
4315 */
4316
4317 if (params_rate(params) != hdsp->system_sample_rate) {
4318 spin_unlock_irq(&hdsp->lock);
4319 _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_RATE);
4320 return -EBUSY;
4321 }
4322
4323 if (params_period_size(params) != hdsp->period_bytes / 4) {
4324 spin_unlock_irq(&hdsp->lock);
4325 _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
4326 return -EBUSY;
4327 }
4328
4329 /* We're fine. */
4330
4331 spin_unlock_irq(&hdsp->lock);
4332 return 0;
4333
4334 } else {
4335 spin_unlock_irq(&hdsp->lock);
4336 }
4337
4338 /* how to make sure that the rate matches an externally-set one ?
4339 */
4340
4341 spin_lock_irq(&hdsp->lock);
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02004342 if (! hdsp->clock_source_locked) {
4343 if ((err = hdsp_set_rate(hdsp, params_rate(params), 0)) < 0) {
4344 spin_unlock_irq(&hdsp->lock);
4345 _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_RATE);
4346 return err;
4347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348 }
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02004349 spin_unlock_irq(&hdsp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350
4351 if ((err = hdsp_set_interrupt_interval(hdsp, params_period_size(params))) < 0) {
4352 _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
4353 return err;
4354 }
4355
4356 return 0;
4357}
4358
Takashi Iwai55e957d2005-11-17 14:52:13 +01004359static int snd_hdsp_channel_info(struct snd_pcm_substream *substream,
4360 struct snd_pcm_channel_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004362 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 int mapped_channel;
4364
Takashi Iwaida3cec32008-08-08 17:12:14 +02004365 if (snd_BUG_ON(info->channel >= hdsp->max_channels))
4366 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367
Takashi Iwaib0b981192005-10-20 18:29:58 +02004368 if ((mapped_channel = hdsp->channel_map[info->channel]) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370
4371 info->offset = mapped_channel * HDSP_CHANNEL_BUFFER_BYTES;
4372 info->first = 0;
4373 info->step = 32;
4374 return 0;
4375}
4376
Takashi Iwai55e957d2005-11-17 14:52:13 +01004377static int snd_hdsp_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 unsigned int cmd, void *arg)
4379{
4380 switch (cmd) {
4381 case SNDRV_PCM_IOCTL1_RESET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 return snd_hdsp_reset(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
Takashi Iwaib0b981192005-10-20 18:29:58 +02004384 return snd_hdsp_channel_info(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 default:
4386 break;
4387 }
4388
4389 return snd_pcm_lib_ioctl(substream, cmd, arg);
4390}
4391
Takashi Iwai55e957d2005-11-17 14:52:13 +01004392static int snd_hdsp_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004394 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
4395 struct snd_pcm_substream *other;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 int running;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004397
Takashi Iwaib0b981192005-10-20 18:29:58 +02004398 if (hdsp_check_for_iobox (hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400
Takashi Iwai311e70a2006-09-06 12:13:37 +02004401 if (hdsp_check_for_firmware(hdsp, 0)) /* no auto-loading in trigger */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403
4404 spin_lock(&hdsp->lock);
4405 running = hdsp->running;
4406 switch (cmd) {
4407 case SNDRV_PCM_TRIGGER_START:
4408 running |= 1 << substream->stream;
4409 break;
4410 case SNDRV_PCM_TRIGGER_STOP:
4411 running &= ~(1 << substream->stream);
4412 break;
4413 default:
4414 snd_BUG();
4415 spin_unlock(&hdsp->lock);
4416 return -EINVAL;
4417 }
4418 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
4419 other = hdsp->capture_substream;
4420 else
4421 other = hdsp->playback_substream;
4422
4423 if (other) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004424 struct snd_pcm_substream *s;
Takashi Iwaief991b92007-02-22 12:52:53 +01004425 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 if (s == other) {
4427 snd_pcm_trigger_done(s, substream);
4428 if (cmd == SNDRV_PCM_TRIGGER_START)
4429 running |= 1 << s->stream;
4430 else
4431 running &= ~(1 << s->stream);
4432 goto _ok;
4433 }
4434 }
4435 if (cmd == SNDRV_PCM_TRIGGER_START) {
4436 if (!(running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) &&
4437 substream->stream == SNDRV_PCM_STREAM_CAPTURE)
4438 hdsp_silence_playback(hdsp);
4439 } else {
4440 if (running &&
4441 substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
4442 hdsp_silence_playback(hdsp);
4443 }
4444 } else {
4445 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
4446 hdsp_silence_playback(hdsp);
4447 }
4448 _ok:
4449 snd_pcm_trigger_done(substream, substream);
4450 if (!hdsp->running && running)
4451 hdsp_start_audio(hdsp);
4452 else if (hdsp->running && !running)
4453 hdsp_stop_audio(hdsp);
4454 hdsp->running = running;
4455 spin_unlock(&hdsp->lock);
4456
4457 return 0;
4458}
4459
Takashi Iwai55e957d2005-11-17 14:52:13 +01004460static int snd_hdsp_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004462 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 int result = 0;
4464
Takashi Iwaib0b981192005-10-20 18:29:58 +02004465 if (hdsp_check_for_iobox (hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467
Takashi Iwaib0b981192005-10-20 18:29:58 +02004468 if (hdsp_check_for_firmware(hdsp, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470
4471 spin_lock_irq(&hdsp->lock);
4472 if (!hdsp->running)
4473 hdsp_reset_hw_pointer(hdsp);
4474 spin_unlock_irq(&hdsp->lock);
4475 return result;
4476}
4477
Takashi Iwai55e957d2005-11-17 14:52:13 +01004478static struct snd_pcm_hardware snd_hdsp_playback_subinfo =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479{
4480 .info = (SNDRV_PCM_INFO_MMAP |
4481 SNDRV_PCM_INFO_MMAP_VALID |
4482 SNDRV_PCM_INFO_NONINTERLEAVED |
4483 SNDRV_PCM_INFO_SYNC_START |
4484 SNDRV_PCM_INFO_DOUBLE),
4485#ifdef SNDRV_BIG_ENDIAN
4486 .formats = SNDRV_PCM_FMTBIT_S32_BE,
4487#else
4488 .formats = SNDRV_PCM_FMTBIT_S32_LE,
4489#endif
4490 .rates = (SNDRV_PCM_RATE_32000 |
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004491 SNDRV_PCM_RATE_44100 |
4492 SNDRV_PCM_RATE_48000 |
4493 SNDRV_PCM_RATE_64000 |
4494 SNDRV_PCM_RATE_88200 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495 SNDRV_PCM_RATE_96000),
4496 .rate_min = 32000,
4497 .rate_max = 96000,
Florian Faber28b26e12010-12-01 12:14:47 +01004498 .channels_min = 6,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499 .channels_max = HDSP_MAX_CHANNELS,
4500 .buffer_bytes_max = HDSP_CHANNEL_BUFFER_BYTES * HDSP_MAX_CHANNELS,
4501 .period_bytes_min = (64 * 4) * 10,
4502 .period_bytes_max = (8192 * 4) * HDSP_MAX_CHANNELS,
4503 .periods_min = 2,
4504 .periods_max = 2,
4505 .fifo_size = 0
4506};
4507
Takashi Iwai55e957d2005-11-17 14:52:13 +01004508static struct snd_pcm_hardware snd_hdsp_capture_subinfo =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509{
4510 .info = (SNDRV_PCM_INFO_MMAP |
4511 SNDRV_PCM_INFO_MMAP_VALID |
4512 SNDRV_PCM_INFO_NONINTERLEAVED |
4513 SNDRV_PCM_INFO_SYNC_START),
4514#ifdef SNDRV_BIG_ENDIAN
4515 .formats = SNDRV_PCM_FMTBIT_S32_BE,
4516#else
4517 .formats = SNDRV_PCM_FMTBIT_S32_LE,
4518#endif
4519 .rates = (SNDRV_PCM_RATE_32000 |
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004520 SNDRV_PCM_RATE_44100 |
4521 SNDRV_PCM_RATE_48000 |
4522 SNDRV_PCM_RATE_64000 |
4523 SNDRV_PCM_RATE_88200 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 SNDRV_PCM_RATE_96000),
4525 .rate_min = 32000,
4526 .rate_max = 96000,
Florian Faber28b26e12010-12-01 12:14:47 +01004527 .channels_min = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 .channels_max = HDSP_MAX_CHANNELS,
4529 .buffer_bytes_max = HDSP_CHANNEL_BUFFER_BYTES * HDSP_MAX_CHANNELS,
4530 .period_bytes_min = (64 * 4) * 10,
4531 .period_bytes_max = (8192 * 4) * HDSP_MAX_CHANNELS,
4532 .periods_min = 2,
4533 .periods_max = 2,
4534 .fifo_size = 0
4535};
4536
4537static unsigned int hdsp_period_sizes[] = { 64, 128, 256, 512, 1024, 2048, 4096, 8192 };
4538
Takashi Iwai55e957d2005-11-17 14:52:13 +01004539static struct snd_pcm_hw_constraint_list hdsp_hw_constraints_period_sizes = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540 .count = ARRAY_SIZE(hdsp_period_sizes),
4541 .list = hdsp_period_sizes,
4542 .mask = 0
4543};
4544
4545static unsigned int hdsp_9632_sample_rates[] = { 32000, 44100, 48000, 64000, 88200, 96000, 128000, 176400, 192000 };
4546
Takashi Iwai55e957d2005-11-17 14:52:13 +01004547static struct snd_pcm_hw_constraint_list hdsp_hw_constraints_9632_sample_rates = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548 .count = ARRAY_SIZE(hdsp_9632_sample_rates),
4549 .list = hdsp_9632_sample_rates,
4550 .mask = 0
4551};
4552
Takashi Iwai55e957d2005-11-17 14:52:13 +01004553static int snd_hdsp_hw_rule_in_channels(struct snd_pcm_hw_params *params,
4554 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004556 struct hdsp *hdsp = rule->private;
4557 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 if (hdsp->io_type == H9632) {
4559 unsigned int list[3];
4560 list[0] = hdsp->qs_in_channels;
4561 list[1] = hdsp->ds_in_channels;
4562 list[2] = hdsp->ss_in_channels;
4563 return snd_interval_list(c, 3, list, 0);
4564 } else {
4565 unsigned int list[2];
4566 list[0] = hdsp->ds_in_channels;
4567 list[1] = hdsp->ss_in_channels;
4568 return snd_interval_list(c, 2, list, 0);
4569 }
4570}
4571
Takashi Iwai55e957d2005-11-17 14:52:13 +01004572static int snd_hdsp_hw_rule_out_channels(struct snd_pcm_hw_params *params,
4573 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574{
4575 unsigned int list[3];
Takashi Iwai55e957d2005-11-17 14:52:13 +01004576 struct hdsp *hdsp = rule->private;
4577 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578 if (hdsp->io_type == H9632) {
4579 list[0] = hdsp->qs_out_channels;
4580 list[1] = hdsp->ds_out_channels;
4581 list[2] = hdsp->ss_out_channels;
4582 return snd_interval_list(c, 3, list, 0);
4583 } else {
4584 list[0] = hdsp->ds_out_channels;
4585 list[1] = hdsp->ss_out_channels;
4586 }
4587 return snd_interval_list(c, 2, list, 0);
4588}
4589
Takashi Iwai55e957d2005-11-17 14:52:13 +01004590static int snd_hdsp_hw_rule_in_channels_rate(struct snd_pcm_hw_params *params,
4591 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004593 struct hdsp *hdsp = rule->private;
4594 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
4595 struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 if (r->min > 96000 && hdsp->io_type == H9632) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004597 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598 .min = hdsp->qs_in_channels,
4599 .max = hdsp->qs_in_channels,
4600 .integer = 1,
4601 };
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004602 return snd_interval_refine(c, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 } else if (r->min > 48000 && r->max <= 96000) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004604 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605 .min = hdsp->ds_in_channels,
4606 .max = hdsp->ds_in_channels,
4607 .integer = 1,
4608 };
4609 return snd_interval_refine(c, &t);
4610 } else if (r->max < 64000) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004611 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 .min = hdsp->ss_in_channels,
4613 .max = hdsp->ss_in_channels,
4614 .integer = 1,
4615 };
4616 return snd_interval_refine(c, &t);
4617 }
4618 return 0;
4619}
4620
Takashi Iwai55e957d2005-11-17 14:52:13 +01004621static int snd_hdsp_hw_rule_out_channels_rate(struct snd_pcm_hw_params *params,
4622 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004624 struct hdsp *hdsp = rule->private;
4625 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
4626 struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627 if (r->min > 96000 && hdsp->io_type == H9632) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004628 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629 .min = hdsp->qs_out_channels,
4630 .max = hdsp->qs_out_channels,
4631 .integer = 1,
4632 };
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004633 return snd_interval_refine(c, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 } else if (r->min > 48000 && r->max <= 96000) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004635 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 .min = hdsp->ds_out_channels,
4637 .max = hdsp->ds_out_channels,
4638 .integer = 1,
4639 };
4640 return snd_interval_refine(c, &t);
4641 } else if (r->max < 64000) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004642 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 .min = hdsp->ss_out_channels,
4644 .max = hdsp->ss_out_channels,
4645 .integer = 1,
4646 };
4647 return snd_interval_refine(c, &t);
4648 }
4649 return 0;
4650}
4651
Takashi Iwai55e957d2005-11-17 14:52:13 +01004652static int snd_hdsp_hw_rule_rate_out_channels(struct snd_pcm_hw_params *params,
4653 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004655 struct hdsp *hdsp = rule->private;
4656 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
4657 struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658 if (c->min >= hdsp->ss_out_channels) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004659 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660 .min = 32000,
4661 .max = 48000,
4662 .integer = 1,
4663 };
4664 return snd_interval_refine(r, &t);
4665 } else if (c->max <= hdsp->qs_out_channels && hdsp->io_type == H9632) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004666 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667 .min = 128000,
4668 .max = 192000,
4669 .integer = 1,
4670 };
4671 return snd_interval_refine(r, &t);
4672 } else if (c->max <= hdsp->ds_out_channels) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004673 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 .min = 64000,
4675 .max = 96000,
4676 .integer = 1,
4677 };
4678 return snd_interval_refine(r, &t);
4679 }
4680 return 0;
4681}
4682
Takashi Iwai55e957d2005-11-17 14:52:13 +01004683static int snd_hdsp_hw_rule_rate_in_channels(struct snd_pcm_hw_params *params,
4684 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004686 struct hdsp *hdsp = rule->private;
4687 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
4688 struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 if (c->min >= hdsp->ss_in_channels) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004690 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 .min = 32000,
4692 .max = 48000,
4693 .integer = 1,
4694 };
4695 return snd_interval_refine(r, &t);
4696 } else if (c->max <= hdsp->qs_in_channels && hdsp->io_type == H9632) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004697 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698 .min = 128000,
4699 .max = 192000,
4700 .integer = 1,
4701 };
4702 return snd_interval_refine(r, &t);
4703 } else if (c->max <= hdsp->ds_in_channels) {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004704 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705 .min = 64000,
4706 .max = 96000,
4707 .integer = 1,
4708 };
4709 return snd_interval_refine(r, &t);
4710 }
4711 return 0;
4712}
4713
Takashi Iwai55e957d2005-11-17 14:52:13 +01004714static int snd_hdsp_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004716 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
4717 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718
Takashi Iwaib0b981192005-10-20 18:29:58 +02004719 if (hdsp_check_for_iobox (hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721
Takashi Iwaib0b981192005-10-20 18:29:58 +02004722 if (hdsp_check_for_firmware(hdsp, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724
4725 spin_lock_irq(&hdsp->lock);
4726
4727 snd_pcm_set_sync(substream);
4728
4729 runtime->hw = snd_hdsp_playback_subinfo;
4730 runtime->dma_area = hdsp->playback_buffer;
4731 runtime->dma_bytes = HDSP_DMA_AREA_BYTES;
4732
4733 hdsp->playback_pid = current->pid;
4734 hdsp->playback_substream = substream;
4735
4736 spin_unlock_irq(&hdsp->lock);
4737
4738 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
4739 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 +02004740 if (hdsp->clock_source_locked) {
4741 runtime->hw.rate_min = runtime->hw.rate_max = hdsp->system_sample_rate;
4742 } else if (hdsp->io_type == H9632) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743 runtime->hw.rate_max = 192000;
4744 runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
4745 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hdsp_hw_constraints_9632_sample_rates);
4746 }
Takashi Iwaie3ea4d82005-07-04 18:12:39 +02004747 if (hdsp->io_type == H9632) {
4748 runtime->hw.channels_min = hdsp->qs_out_channels;
4749 runtime->hw.channels_max = hdsp->ss_out_channels;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004750 }
4751
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
4753 snd_hdsp_hw_rule_out_channels, hdsp,
4754 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
4755 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
4756 snd_hdsp_hw_rule_out_channels_rate, hdsp,
4757 SNDRV_PCM_HW_PARAM_RATE, -1);
4758 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
4759 snd_hdsp_hw_rule_rate_out_channels, hdsp,
4760 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
4761
Florian Faber28b26e12010-12-01 12:14:47 +01004762 if (RPM != hdsp->io_type) {
4763 hdsp->creg_spdif_stream = hdsp->creg_spdif;
4764 hdsp->spdif_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
4765 snd_ctl_notify(hdsp->card, SNDRV_CTL_EVENT_MASK_VALUE |
4766 SNDRV_CTL_EVENT_MASK_INFO, &hdsp->spdif_ctl->id);
4767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768 return 0;
4769}
4770
Takashi Iwai55e957d2005-11-17 14:52:13 +01004771static int snd_hdsp_playback_release(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004773 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774
4775 spin_lock_irq(&hdsp->lock);
4776
4777 hdsp->playback_pid = -1;
4778 hdsp->playback_substream = NULL;
4779
4780 spin_unlock_irq(&hdsp->lock);
4781
Florian Faber28b26e12010-12-01 12:14:47 +01004782 if (RPM != hdsp->io_type) {
4783 hdsp->spdif_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
4784 snd_ctl_notify(hdsp->card, SNDRV_CTL_EVENT_MASK_VALUE |
4785 SNDRV_CTL_EVENT_MASK_INFO, &hdsp->spdif_ctl->id);
4786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787 return 0;
4788}
4789
4790
Takashi Iwai55e957d2005-11-17 14:52:13 +01004791static int snd_hdsp_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004793 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
4794 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795
Takashi Iwaib0b981192005-10-20 18:29:58 +02004796 if (hdsp_check_for_iobox (hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798
Takashi Iwaib0b981192005-10-20 18:29:58 +02004799 if (hdsp_check_for_firmware(hdsp, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801
4802 spin_lock_irq(&hdsp->lock);
4803
4804 snd_pcm_set_sync(substream);
4805
4806 runtime->hw = snd_hdsp_capture_subinfo;
4807 runtime->dma_area = hdsp->capture_buffer;
4808 runtime->dma_bytes = HDSP_DMA_AREA_BYTES;
4809
4810 hdsp->capture_pid = current->pid;
4811 hdsp->capture_substream = substream;
4812
4813 spin_unlock_irq(&hdsp->lock);
4814
4815 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
4816 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, &hdsp_hw_constraints_period_sizes);
4817 if (hdsp->io_type == H9632) {
4818 runtime->hw.channels_min = hdsp->qs_in_channels;
4819 runtime->hw.channels_max = hdsp->ss_in_channels;
4820 runtime->hw.rate_max = 192000;
4821 runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
4822 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hdsp_hw_constraints_9632_sample_rates);
4823 }
4824 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
4825 snd_hdsp_hw_rule_in_channels, hdsp,
4826 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
4827 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
4828 snd_hdsp_hw_rule_in_channels_rate, hdsp,
4829 SNDRV_PCM_HW_PARAM_RATE, -1);
4830 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
4831 snd_hdsp_hw_rule_rate_in_channels, hdsp,
4832 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
4833 return 0;
4834}
4835
Takashi Iwai55e957d2005-11-17 14:52:13 +01004836static int snd_hdsp_capture_release(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837{
Takashi Iwai55e957d2005-11-17 14:52:13 +01004838 struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839
4840 spin_lock_irq(&hdsp->lock);
4841
4842 hdsp->capture_pid = -1;
4843 hdsp->capture_substream = NULL;
4844
4845 spin_unlock_irq(&hdsp->lock);
4846 return 0;
4847}
4848
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849/* helper functions for copying meter values */
4850static inline int copy_u32_le(void __user *dest, void __iomem *src)
4851{
4852 u32 val = readl(src);
4853 return copy_to_user(dest, &val, 4);
4854}
4855
4856static inline int copy_u64_le(void __user *dest, void __iomem *src_low, void __iomem *src_high)
4857{
4858 u32 rms_low, rms_high;
4859 u64 rms;
4860 rms_low = readl(src_low);
4861 rms_high = readl(src_high);
4862 rms = ((u64)rms_high << 32) | rms_low;
4863 return copy_to_user(dest, &rms, 8);
4864}
4865
4866static inline int copy_u48_le(void __user *dest, void __iomem *src_low, void __iomem *src_high)
4867{
4868 u32 rms_low, rms_high;
4869 u64 rms;
4870 rms_low = readl(src_low) & 0xffffff00;
4871 rms_high = readl(src_high) & 0xffffff00;
4872 rms = ((u64)rms_high << 32) | rms_low;
4873 return copy_to_user(dest, &rms, 8);
4874}
4875
Takashi Iwai55e957d2005-11-17 14:52:13 +01004876static int hdsp_9652_get_peak(struct hdsp *hdsp, struct hdsp_peak_rms __user *peak_rms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877{
4878 int doublespeed = 0;
4879 int i, j, channels, ofs;
4880
4881 if (hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DoubleSpeedStatus)
4882 doublespeed = 1;
4883 channels = doublespeed ? 14 : 26;
4884 for (i = 0, j = 0; i < 26; ++i) {
4885 if (doublespeed && (i & 4))
4886 continue;
4887 ofs = HDSP_9652_peakBase - j * 4;
4888 if (copy_u32_le(&peak_rms->input_peaks[i], hdsp->iobase + ofs))
4889 return -EFAULT;
4890 ofs -= channels * 4;
4891 if (copy_u32_le(&peak_rms->playback_peaks[i], hdsp->iobase + ofs))
4892 return -EFAULT;
4893 ofs -= channels * 4;
4894 if (copy_u32_le(&peak_rms->output_peaks[i], hdsp->iobase + ofs))
4895 return -EFAULT;
4896 ofs = HDSP_9652_rmsBase + j * 8;
4897 if (copy_u48_le(&peak_rms->input_rms[i], hdsp->iobase + ofs,
4898 hdsp->iobase + ofs + 4))
4899 return -EFAULT;
4900 ofs += channels * 8;
4901 if (copy_u48_le(&peak_rms->playback_rms[i], hdsp->iobase + ofs,
4902 hdsp->iobase + ofs + 4))
4903 return -EFAULT;
4904 ofs += channels * 8;
4905 if (copy_u48_le(&peak_rms->output_rms[i], hdsp->iobase + ofs,
4906 hdsp->iobase + ofs + 4))
4907 return -EFAULT;
4908 j++;
4909 }
4910 return 0;
4911}
4912
Takashi Iwai55e957d2005-11-17 14:52:13 +01004913static int hdsp_9632_get_peak(struct hdsp *hdsp, struct hdsp_peak_rms __user *peak_rms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914{
4915 int i, j;
Takashi Iwai55e957d2005-11-17 14:52:13 +01004916 struct hdsp_9632_meters __iomem *m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917 int doublespeed = 0;
4918
4919 if (hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DoubleSpeedStatus)
4920 doublespeed = 1;
Takashi Iwai55e957d2005-11-17 14:52:13 +01004921 m = (struct hdsp_9632_meters __iomem *)(hdsp->iobase+HDSP_9632_metersBase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922 for (i = 0, j = 0; i < 16; ++i, ++j) {
4923 if (copy_u32_le(&peak_rms->input_peaks[i], &m->input_peak[j]))
4924 return -EFAULT;
4925 if (copy_u32_le(&peak_rms->playback_peaks[i], &m->playback_peak[j]))
4926 return -EFAULT;
4927 if (copy_u32_le(&peak_rms->output_peaks[i], &m->output_peak[j]))
4928 return -EFAULT;
4929 if (copy_u64_le(&peak_rms->input_rms[i], &m->input_rms_low[j],
4930 &m->input_rms_high[j]))
4931 return -EFAULT;
4932 if (copy_u64_le(&peak_rms->playback_rms[i], &m->playback_rms_low[j],
4933 &m->playback_rms_high[j]))
4934 return -EFAULT;
4935 if (copy_u64_le(&peak_rms->output_rms[i], &m->output_rms_low[j],
4936 &m->output_rms_high[j]))
4937 return -EFAULT;
4938 if (doublespeed && i == 3) i += 4;
4939 }
4940 return 0;
4941}
4942
Takashi Iwai55e957d2005-11-17 14:52:13 +01004943static int hdsp_get_peak(struct hdsp *hdsp, struct hdsp_peak_rms __user *peak_rms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944{
4945 int i;
4946
4947 for (i = 0; i < 26; i++) {
4948 if (copy_u32_le(&peak_rms->playback_peaks[i],
4949 hdsp->iobase + HDSP_playbackPeakLevel + i * 4))
4950 return -EFAULT;
4951 if (copy_u32_le(&peak_rms->input_peaks[i],
4952 hdsp->iobase + HDSP_inputPeakLevel + i * 4))
4953 return -EFAULT;
4954 }
4955 for (i = 0; i < 28; i++) {
4956 if (copy_u32_le(&peak_rms->output_peaks[i],
4957 hdsp->iobase + HDSP_outputPeakLevel + i * 4))
4958 return -EFAULT;
4959 }
4960 for (i = 0; i < 26; ++i) {
4961 if (copy_u64_le(&peak_rms->playback_rms[i],
4962 hdsp->iobase + HDSP_playbackRmsLevel + i * 8 + 4,
4963 hdsp->iobase + HDSP_playbackRmsLevel + i * 8))
4964 return -EFAULT;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01004965 if (copy_u64_le(&peak_rms->input_rms[i],
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966 hdsp->iobase + HDSP_inputRmsLevel + i * 8 + 4,
4967 hdsp->iobase + HDSP_inputRmsLevel + i * 8))
4968 return -EFAULT;
4969 }
4970 return 0;
4971}
4972
Takashi Iwai55e957d2005-11-17 14:52:13 +01004973static 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 -07004974{
Joe Perches9fe856e2010-09-04 18:52:54 -07004975 struct hdsp *hdsp = hw->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976 void __user *argp = (void __user *)arg;
Tim Blechmann3ae7e2e2008-11-08 14:42:18 +01004977 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978
4979 switch (cmd) {
4980 case SNDRV_HDSP_IOCTL_GET_PEAK_RMS: {
Takashi Iwai55e957d2005-11-17 14:52:13 +01004981 struct hdsp_peak_rms __user *peak_rms = (struct hdsp_peak_rms __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982
Tim Blechmann3ae7e2e2008-11-08 14:42:18 +01004983 err = hdsp_check_for_iobox(hdsp);
4984 if (err < 0)
4985 return err;
4986
4987 err = hdsp_check_for_firmware(hdsp, 1);
4988 if (err < 0)
4989 return err;
4990
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 if (!(hdsp->state & HDSP_FirmwareLoaded)) {
4992 snd_printk(KERN_ERR "Hammerfall-DSP: firmware needs to be uploaded to the card.\n");
4993 return -EINVAL;
4994 }
4995
4996 switch (hdsp->io_type) {
4997 case H9652:
4998 return hdsp_9652_get_peak(hdsp, peak_rms);
4999 case H9632:
5000 return hdsp_9632_get_peak(hdsp, peak_rms);
5001 default:
5002 return hdsp_get_peak(hdsp, peak_rms);
5003 }
5004 }
5005 case SNDRV_HDSP_IOCTL_GET_CONFIG_INFO: {
Takashi Iwai55e957d2005-11-17 14:52:13 +01005006 struct hdsp_config_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007 unsigned long flags;
5008 int i;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005009
Tim Blechmann3ae7e2e2008-11-08 14:42:18 +01005010 err = hdsp_check_for_iobox(hdsp);
5011 if (err < 0)
5012 return err;
5013
5014 err = hdsp_check_for_firmware(hdsp, 1);
5015 if (err < 0)
5016 return err;
5017
Dan Rosenberge68d3b32010-09-25 11:07:27 -04005018 memset(&info, 0, sizeof(info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 spin_lock_irqsave(&hdsp->lock, flags);
5020 info.pref_sync_ref = (unsigned char)hdsp_pref_sync_ref(hdsp);
5021 info.wordclock_sync_check = (unsigned char)hdsp_wc_sync_check(hdsp);
Takashi Iwaib0b981192005-10-20 18:29:58 +02005022 if (hdsp->io_type != H9632)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023 info.adatsync_sync_check = (unsigned char)hdsp_adatsync_sync_check(hdsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024 info.spdif_sync_check = (unsigned char)hdsp_spdif_sync_check(hdsp);
Florian Faber28b26e12010-12-01 12:14:47 +01005025 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 -07005026 info.adat_sync_check[i] = (unsigned char)hdsp_adat_sync_check(hdsp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 info.spdif_in = (unsigned char)hdsp_spdif_in(hdsp);
5028 info.spdif_out = (unsigned char)hdsp_spdif_out(hdsp);
5029 info.spdif_professional = (unsigned char)hdsp_spdif_professional(hdsp);
5030 info.spdif_emphasis = (unsigned char)hdsp_spdif_emphasis(hdsp);
5031 info.spdif_nonaudio = (unsigned char)hdsp_spdif_nonaudio(hdsp);
5032 info.spdif_sample_rate = hdsp_spdif_sample_rate(hdsp);
5033 info.system_sample_rate = hdsp->system_sample_rate;
5034 info.autosync_sample_rate = hdsp_external_sample_rate(hdsp);
5035 info.system_clock_mode = (unsigned char)hdsp_system_clock_mode(hdsp);
5036 info.clock_source = (unsigned char)hdsp_clock_source(hdsp);
5037 info.autosync_ref = (unsigned char)hdsp_autosync_ref(hdsp);
5038 info.line_out = (unsigned char)hdsp_line_out(hdsp);
5039 if (hdsp->io_type == H9632) {
5040 info.da_gain = (unsigned char)hdsp_da_gain(hdsp);
5041 info.ad_gain = (unsigned char)hdsp_ad_gain(hdsp);
5042 info.phone_gain = (unsigned char)hdsp_phone_gain(hdsp);
5043 info.xlr_breakout_cable = (unsigned char)hdsp_xlr_breakout_cable(hdsp);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005044
Florian Faber28b26e12010-12-01 12:14:47 +01005045 } else if (hdsp->io_type == RPM) {
5046 info.da_gain = (unsigned char) hdsp_rpm_input12(hdsp);
5047 info.ad_gain = (unsigned char) hdsp_rpm_input34(hdsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048 }
Takashi Iwaib0b981192005-10-20 18:29:58 +02005049 if (hdsp->io_type == H9632 || hdsp->io_type == H9652)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050 info.analog_extension_board = (unsigned char)hdsp_aeb(hdsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051 spin_unlock_irqrestore(&hdsp->lock, flags);
5052 if (copy_to_user(argp, &info, sizeof(info)))
5053 return -EFAULT;
5054 break;
5055 }
5056 case SNDRV_HDSP_IOCTL_GET_9632_AEB: {
Takashi Iwai55e957d2005-11-17 14:52:13 +01005057 struct hdsp_9632_aeb h9632_aeb;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005058
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059 if (hdsp->io_type != H9632) return -EINVAL;
5060 h9632_aeb.aebi = hdsp->ss_in_channels - H9632_SS_CHANNELS;
5061 h9632_aeb.aebo = hdsp->ss_out_channels - H9632_SS_CHANNELS;
5062 if (copy_to_user(argp, &h9632_aeb, sizeof(h9632_aeb)))
5063 return -EFAULT;
5064 break;
5065 }
5066 case SNDRV_HDSP_IOCTL_GET_VERSION: {
Takashi Iwai55e957d2005-11-17 14:52:13 +01005067 struct hdsp_version hdsp_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068 int err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005069
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070 if (hdsp->io_type == H9652 || hdsp->io_type == H9632) return -EINVAL;
5071 if (hdsp->io_type == Undefined) {
Takashi Iwaib0b981192005-10-20 18:29:58 +02005072 if ((err = hdsp_get_iobox_version(hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 }
5075 hdsp_version.io_type = hdsp->io_type;
5076 hdsp_version.firmware_rev = hdsp->firmware_rev;
Takashi Iwaib0b981192005-10-20 18:29:58 +02005077 if ((err = copy_to_user(argp, &hdsp_version, sizeof(hdsp_version))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079 break;
5080 }
5081 case SNDRV_HDSP_IOCTL_UPLOAD_FIRMWARE: {
Takashi Iwai55e957d2005-11-17 14:52:13 +01005082 struct hdsp_firmware __user *firmware;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083 u32 __user *firmware_data;
5084 int err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005085
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086 if (hdsp->io_type == H9652 || hdsp->io_type == H9632) return -EINVAL;
5087 /* SNDRV_HDSP_IOCTL_GET_VERSION must have been called */
5088 if (hdsp->io_type == Undefined) return -EINVAL;
5089
5090 if (hdsp->state & (HDSP_FirmwareCached | HDSP_FirmwareLoaded))
5091 return -EBUSY;
5092
Takashi Iwaib0b981192005-10-20 18:29:58 +02005093 snd_printk(KERN_INFO "Hammerfall-DSP: initializing firmware upload\n");
Takashi Iwai55e957d2005-11-17 14:52:13 +01005094 firmware = (struct hdsp_firmware __user *)argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095
Takashi Iwaib0b981192005-10-20 18:29:58 +02005096 if (get_user(firmware_data, &firmware->firmware_data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097 return -EFAULT;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005098
Takashi Iwaib0b981192005-10-20 18:29:58 +02005099 if (hdsp_check_for_iobox (hdsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101
Takashi Iwai90caaef2012-11-22 16:55:11 +01005102 if (!hdsp->fw_uploaded) {
5103 hdsp->fw_uploaded = vmalloc(HDSP_FIRMWARE_SIZE);
5104 if (!hdsp->fw_uploaded)
5105 return -ENOMEM;
5106 }
5107
5108 if (copy_from_user(hdsp->fw_uploaded, firmware_data,
5109 HDSP_FIRMWARE_SIZE)) {
5110 vfree(hdsp->fw_uploaded);
5111 hdsp->fw_uploaded = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112 return -EFAULT;
Takashi Iwai90caaef2012-11-22 16:55:11 +01005113 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005114
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115 hdsp->state |= HDSP_FirmwareCached;
5116
Takashi Iwaib0b981192005-10-20 18:29:58 +02005117 if ((err = snd_hdsp_load_firmware_from_cache(hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118 return err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005119
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120 if (!(hdsp->state & HDSP_InitializationComplete)) {
Takashi Iwaib0b981192005-10-20 18:29:58 +02005121 if ((err = snd_hdsp_enable_io(hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122 return err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005123
5124 snd_hdsp_initialize_channels(hdsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125 snd_hdsp_initialize_midi_flush(hdsp);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005126
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127 if ((err = snd_hdsp_create_alsa_devices(hdsp->card, hdsp)) < 0) {
Takashi Iwaib0b981192005-10-20 18:29:58 +02005128 snd_printk(KERN_ERR "Hammerfall-DSP: error creating alsa devices\n");
5129 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130 }
5131 }
5132 break;
5133 }
5134 case SNDRV_HDSP_IOCTL_GET_MIXER: {
Takashi Iwai55e957d2005-11-17 14:52:13 +01005135 struct hdsp_mixer __user *mixer = (struct hdsp_mixer __user *)argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136 if (copy_to_user(mixer->matrix, hdsp->mixer_matrix, sizeof(unsigned short)*HDSP_MATRIX_MIXER_SIZE))
5137 return -EFAULT;
5138 break;
5139 }
5140 default:
5141 return -EINVAL;
5142 }
5143 return 0;
5144}
5145
Takashi Iwai55e957d2005-11-17 14:52:13 +01005146static struct snd_pcm_ops snd_hdsp_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147 .open = snd_hdsp_playback_open,
5148 .close = snd_hdsp_playback_release,
5149 .ioctl = snd_hdsp_ioctl,
5150 .hw_params = snd_hdsp_hw_params,
5151 .prepare = snd_hdsp_prepare,
5152 .trigger = snd_hdsp_trigger,
5153 .pointer = snd_hdsp_hw_pointer,
5154 .copy = snd_hdsp_playback_copy,
5155 .silence = snd_hdsp_hw_silence,
5156};
5157
Takashi Iwai55e957d2005-11-17 14:52:13 +01005158static struct snd_pcm_ops snd_hdsp_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159 .open = snd_hdsp_capture_open,
5160 .close = snd_hdsp_capture_release,
5161 .ioctl = snd_hdsp_ioctl,
5162 .hw_params = snd_hdsp_hw_params,
5163 .prepare = snd_hdsp_prepare,
5164 .trigger = snd_hdsp_trigger,
5165 .pointer = snd_hdsp_hw_pointer,
5166 .copy = snd_hdsp_capture_copy,
5167};
5168
Takashi Iwai92eed662008-02-22 18:35:56 +01005169static int snd_hdsp_create_hwdep(struct snd_card *card, struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170{
Takashi Iwai55e957d2005-11-17 14:52:13 +01005171 struct snd_hwdep *hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172 int err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005173
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174 if ((err = snd_hwdep_new(card, "HDSP hwdep", 0, &hw)) < 0)
5175 return err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005176
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177 hdsp->hwdep = hw;
5178 hw->private_data = hdsp;
5179 strcpy(hw->name, "HDSP hwdep interface");
5180
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181 hw->ops.ioctl = snd_hdsp_hwdep_ioctl;
Andre Schramm42eb9232012-05-07 18:52:51 +02005182 hw->ops.ioctl_compat = snd_hdsp_hwdep_ioctl;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005183
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184 return 0;
5185}
5186
Takashi Iwai55e957d2005-11-17 14:52:13 +01005187static int snd_hdsp_create_pcm(struct snd_card *card, struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188{
Takashi Iwai55e957d2005-11-17 14:52:13 +01005189 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190 int err;
5191
5192 if ((err = snd_pcm_new(card, hdsp->card_name, 0, 1, 1, &pcm)) < 0)
5193 return err;
5194
5195 hdsp->pcm = pcm;
5196 pcm->private_data = hdsp;
5197 strcpy(pcm->name, hdsp->card_name);
5198
5199 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_hdsp_playback_ops);
5200 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_hdsp_capture_ops);
5201
5202 pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
5203
5204 return 0;
5205}
5206
Takashi Iwai55e957d2005-11-17 14:52:13 +01005207static void snd_hdsp_9652_enable_mixer (struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208{
5209 hdsp->control2_register |= HDSP_9652_ENABLE_MIXER;
5210 hdsp_write (hdsp, HDSP_control2Reg, hdsp->control2_register);
5211}
5212
Takashi Iwai55e957d2005-11-17 14:52:13 +01005213static int snd_hdsp_enable_io (struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214{
5215 int i;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005216
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217 if (hdsp_fifo_wait (hdsp, 0, 100)) {
Takashi Iwaib0b981192005-10-20 18:29:58 +02005218 snd_printk(KERN_ERR "Hammerfall-DSP: enable_io fifo_wait failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219 return -EIO;
5220 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005221
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222 for (i = 0; i < hdsp->max_channels; ++i) {
5223 hdsp_write (hdsp, HDSP_inputEnable + (4 * i), 1);
5224 hdsp_write (hdsp, HDSP_outputEnable + (4 * i), 1);
5225 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005226
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227 return 0;
5228}
5229
Takashi Iwai55e957d2005-11-17 14:52:13 +01005230static void snd_hdsp_initialize_channels(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231{
5232 int status, aebi_channels, aebo_channels;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005233
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234 switch (hdsp->io_type) {
5235 case Digiface:
5236 hdsp->card_name = "RME Hammerfall DSP + Digiface";
5237 hdsp->ss_in_channels = hdsp->ss_out_channels = DIGIFACE_SS_CHANNELS;
5238 hdsp->ds_in_channels = hdsp->ds_out_channels = DIGIFACE_DS_CHANNELS;
5239 break;
5240
5241 case H9652:
5242 hdsp->card_name = "RME Hammerfall HDSP 9652";
5243 hdsp->ss_in_channels = hdsp->ss_out_channels = H9652_SS_CHANNELS;
5244 hdsp->ds_in_channels = hdsp->ds_out_channels = H9652_DS_CHANNELS;
5245 break;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005246
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247 case H9632:
5248 status = hdsp_read(hdsp, HDSP_statusRegister);
5249 /* HDSP_AEBx bits are low when AEB are connected */
5250 aebi_channels = (status & HDSP_AEBI) ? 0 : 4;
5251 aebo_channels = (status & HDSP_AEBO) ? 0 : 4;
5252 hdsp->card_name = "RME Hammerfall HDSP 9632";
5253 hdsp->ss_in_channels = H9632_SS_CHANNELS+aebi_channels;
5254 hdsp->ds_in_channels = H9632_DS_CHANNELS+aebi_channels;
5255 hdsp->qs_in_channels = H9632_QS_CHANNELS+aebi_channels;
5256 hdsp->ss_out_channels = H9632_SS_CHANNELS+aebo_channels;
5257 hdsp->ds_out_channels = H9632_DS_CHANNELS+aebo_channels;
5258 hdsp->qs_out_channels = H9632_QS_CHANNELS+aebo_channels;
5259 break;
5260
5261 case Multiface:
5262 hdsp->card_name = "RME Hammerfall DSP + Multiface";
5263 hdsp->ss_in_channels = hdsp->ss_out_channels = MULTIFACE_SS_CHANNELS;
5264 hdsp->ds_in_channels = hdsp->ds_out_channels = MULTIFACE_DS_CHANNELS;
5265 break;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005266
Florian Faber28b26e12010-12-01 12:14:47 +01005267 case RPM:
5268 hdsp->card_name = "RME Hammerfall DSP + RPM";
5269 hdsp->ss_in_channels = RPM_CHANNELS-1;
5270 hdsp->ss_out_channels = RPM_CHANNELS;
5271 hdsp->ds_in_channels = RPM_CHANNELS-1;
5272 hdsp->ds_out_channels = RPM_CHANNELS;
5273 break;
5274
Linus Torvalds1da177e2005-04-16 15:20:36 -07005275 default:
5276 /* should never get here */
5277 break;
5278 }
5279}
5280
Takashi Iwai55e957d2005-11-17 14:52:13 +01005281static void snd_hdsp_initialize_midi_flush (struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282{
5283 snd_hdsp_flush_midi_input (hdsp, 0);
5284 snd_hdsp_flush_midi_input (hdsp, 1);
5285}
5286
Takashi Iwai55e957d2005-11-17 14:52:13 +01005287static int snd_hdsp_create_alsa_devices(struct snd_card *card, struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005288{
5289 int err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005290
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291 if ((err = snd_hdsp_create_pcm(card, hdsp)) < 0) {
Takashi Iwaib0b981192005-10-20 18:29:58 +02005292 snd_printk(KERN_ERR "Hammerfall-DSP: Error creating pcm interface\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293 return err;
5294 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005295
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296
5297 if ((err = snd_hdsp_create_midi(card, hdsp, 0)) < 0) {
Takashi Iwaib0b981192005-10-20 18:29:58 +02005298 snd_printk(KERN_ERR "Hammerfall-DSP: Error creating first midi interface\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299 return err;
5300 }
5301
5302 if (hdsp->io_type == Digiface || hdsp->io_type == H9652) {
5303 if ((err = snd_hdsp_create_midi(card, hdsp, 1)) < 0) {
Takashi Iwaib0b981192005-10-20 18:29:58 +02005304 snd_printk(KERN_ERR "Hammerfall-DSP: Error creating second midi interface\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305 return err;
5306 }
5307 }
5308
5309 if ((err = snd_hdsp_create_controls(card, hdsp)) < 0) {
Takashi Iwaib0b981192005-10-20 18:29:58 +02005310 snd_printk(KERN_ERR "Hammerfall-DSP: Error creating ctl interface\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311 return err;
5312 }
5313
5314 snd_hdsp_proc_init(hdsp);
5315
5316 hdsp->system_sample_rate = -1;
5317 hdsp->playback_pid = -1;
5318 hdsp->capture_pid = -1;
5319 hdsp->capture_substream = NULL;
5320 hdsp->playback_substream = NULL;
5321
5322 if ((err = snd_hdsp_set_defaults(hdsp)) < 0) {
Takashi Iwaib0b981192005-10-20 18:29:58 +02005323 snd_printk(KERN_ERR "Hammerfall-DSP: Error setting default values\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324 return err;
5325 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005326
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327 if (!(hdsp->state & HDSP_InitializationComplete)) {
Clemens Ladischb73c1c12005-09-02 08:49:21 +02005328 strcpy(card->shortname, "Hammerfall DSP");
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005329 sprintf(card->longname, "%s at 0x%lx, irq %d", hdsp->card_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330 hdsp->port, hdsp->irq);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005331
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332 if ((err = snd_card_register(card)) < 0) {
Takashi Iwaib0b981192005-10-20 18:29:58 +02005333 snd_printk(KERN_ERR "Hammerfall-DSP: error registering card\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334 return err;
5335 }
5336 hdsp->state |= HDSP_InitializationComplete;
5337 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005338
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339 return 0;
5340}
5341
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342/* load firmware via hotplug fw loader */
Takashi Iwai92eed662008-02-22 18:35:56 +01005343static int hdsp_request_fw_loader(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344{
5345 const char *fwfile;
5346 const struct firmware *fw;
5347 int err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005348
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349 if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
5350 return 0;
5351 if (hdsp->io_type == Undefined) {
5352 if ((err = hdsp_get_iobox_version(hdsp)) < 0)
5353 return err;
5354 if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
5355 return 0;
5356 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005357
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358 /* caution: max length of firmware filename is 30! */
5359 switch (hdsp->io_type) {
Florian Faber28b26e12010-12-01 12:14:47 +01005360 case RPM:
5361 fwfile = "rpm_firmware.bin";
5362 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363 case Multiface:
5364 if (hdsp->firmware_rev == 0xa)
5365 fwfile = "multiface_firmware.bin";
5366 else
5367 fwfile = "multiface_firmware_rev11.bin";
5368 break;
5369 case Digiface:
5370 if (hdsp->firmware_rev == 0xa)
5371 fwfile = "digiface_firmware.bin";
5372 else
5373 fwfile = "digiface_firmware_rev11.bin";
5374 break;
5375 default:
5376 snd_printk(KERN_ERR "Hammerfall-DSP: invalid io_type %d\n", hdsp->io_type);
5377 return -EINVAL;
5378 }
5379
5380 if (request_firmware(&fw, fwfile, &hdsp->pci->dev)) {
5381 snd_printk(KERN_ERR "Hammerfall-DSP: cannot load firmware %s\n", fwfile);
5382 return -ENOENT;
5383 }
Takashi Iwai90caaef2012-11-22 16:55:11 +01005384 if (fw->size < HDSP_FIRMWARE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385 snd_printk(KERN_ERR "Hammerfall-DSP: too short firmware size %d (expected %d)\n",
Takashi Iwai90caaef2012-11-22 16:55:11 +01005386 (int)fw->size, HDSP_FIRMWARE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387 return -EINVAL;
5388 }
Thomas Charbonnel7679a032005-04-25 11:35:29 +02005389
Takashi Iwai90caaef2012-11-22 16:55:11 +01005390 hdsp->firmware = fw;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005391
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392 hdsp->state |= HDSP_FirmwareCached;
5393
5394 if ((err = snd_hdsp_load_firmware_from_cache(hdsp)) < 0)
5395 return err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005396
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397 if (!(hdsp->state & HDSP_InitializationComplete)) {
Takashi Iwaib0b981192005-10-20 18:29:58 +02005398 if ((err = snd_hdsp_enable_io(hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400
5401 if ((err = snd_hdsp_create_hwdep(hdsp->card, hdsp)) < 0) {
Takashi Iwaib0b981192005-10-20 18:29:58 +02005402 snd_printk(KERN_ERR "Hammerfall-DSP: error creating hwdep device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403 return err;
5404 }
5405 snd_hdsp_initialize_channels(hdsp);
5406 snd_hdsp_initialize_midi_flush(hdsp);
5407 if ((err = snd_hdsp_create_alsa_devices(hdsp->card, hdsp)) < 0) {
Takashi Iwaib0b981192005-10-20 18:29:58 +02005408 snd_printk(KERN_ERR "Hammerfall-DSP: error creating alsa devices\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409 return err;
5410 }
5411 }
5412 return 0;
5413}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414
Takashi Iwai55e957d2005-11-17 14:52:13 +01005415static int __devinit snd_hdsp_create(struct snd_card *card,
5416 struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417{
5418 struct pci_dev *pci = hdsp->pci;
5419 int err;
5420 int is_9652 = 0;
5421 int is_9632 = 0;
5422
5423 hdsp->irq = -1;
5424 hdsp->state = 0;
5425 hdsp->midi[0].rmidi = NULL;
5426 hdsp->midi[1].rmidi = NULL;
5427 hdsp->midi[0].input = NULL;
5428 hdsp->midi[1].input = NULL;
5429 hdsp->midi[0].output = NULL;
5430 hdsp->midi[1].output = NULL;
5431 hdsp->midi[0].pending = 0;
5432 hdsp->midi[1].pending = 0;
5433 spin_lock_init(&hdsp->midi[0].lock);
5434 spin_lock_init(&hdsp->midi[1].lock);
5435 hdsp->iobase = NULL;
5436 hdsp->control_register = 0;
5437 hdsp->control2_register = 0;
5438 hdsp->io_type = Undefined;
5439 hdsp->max_channels = 26;
5440
5441 hdsp->card = card;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005442
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443 spin_lock_init(&hdsp->lock);
5444
5445 tasklet_init(&hdsp->midi_tasklet, hdsp_midi_tasklet, (unsigned long)hdsp);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005446
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447 pci_read_config_word(hdsp->pci, PCI_CLASS_REVISION, &hdsp->firmware_rev);
5448 hdsp->firmware_rev &= 0xff;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005449
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450 /* From Martin Bjoernsen :
5451 "It is important that the card's latency timer register in
5452 the PCI configuration space is set to a value much larger
5453 than 0 by the computer's BIOS or the driver.
5454 The windows driver always sets this 8 bit register [...]
5455 to its maximum 255 to avoid problems with some computers."
5456 */
5457 pci_write_config_byte(hdsp->pci, PCI_LATENCY_TIMER, 0xFF);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005458
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459 strcpy(card->driver, "H-DSP");
5460 strcpy(card->mixername, "Xilinx FPGA");
5461
Takashi Iwaib0b981192005-10-20 18:29:58 +02005462 if (hdsp->firmware_rev < 0xa)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463 return -ENODEV;
Takashi Iwaib0b981192005-10-20 18:29:58 +02005464 else if (hdsp->firmware_rev < 0x64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465 hdsp->card_name = "RME Hammerfall DSP";
Takashi Iwaib0b981192005-10-20 18:29:58 +02005466 else if (hdsp->firmware_rev < 0x96) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 hdsp->card_name = "RME HDSP 9652";
5468 is_9652 = 1;
5469 } else {
5470 hdsp->card_name = "RME HDSP 9632";
5471 hdsp->max_channels = 16;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005472 is_9632 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473 }
5474
Takashi Iwaib0b981192005-10-20 18:29:58 +02005475 if ((err = pci_enable_device(pci)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477
5478 pci_set_master(hdsp->pci);
5479
5480 if ((err = pci_request_regions(pci, "hdsp")) < 0)
5481 return err;
5482 hdsp->port = pci_resource_start(pci, 0);
5483 if ((hdsp->iobase = ioremap_nocache(hdsp->port, HDSP_IO_EXTENT)) == NULL) {
Takashi Iwaib0b981192005-10-20 18:29:58 +02005484 snd_printk(KERN_ERR "Hammerfall-DSP: unable to remap region 0x%lx-0x%lx\n", hdsp->port, hdsp->port + HDSP_IO_EXTENT - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485 return -EBUSY;
5486 }
5487
Takashi Iwai437a5a42006-11-21 12:14:23 +01005488 if (request_irq(pci->irq, snd_hdsp_interrupt, IRQF_SHARED,
Takashi Iwai934c2b62011-06-10 16:36:37 +02005489 KBUILD_MODNAME, hdsp)) {
Takashi Iwaib0b981192005-10-20 18:29:58 +02005490 snd_printk(KERN_ERR "Hammerfall-DSP: unable to use IRQ %d\n", pci->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491 return -EBUSY;
5492 }
5493
5494 hdsp->irq = pci->irq;
Remy Bruno176546a2006-10-16 12:32:53 +02005495 hdsp->precise_ptr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496 hdsp->use_midi_tasklet = 1;
Remy Brunod7923b22006-10-17 12:41:56 +02005497 hdsp->dds_value = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498
Takashi Iwaib0b981192005-10-20 18:29:58 +02005499 if ((err = snd_hdsp_initialize_memory(hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500 return err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005501
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 if (!is_9652 && !is_9632) {
Tim Blechmanne588ed82009-02-20 19:30:35 +01005503 /* we wait a maximum of 10 seconds to let freshly
5504 * inserted cardbus cards do their hardware init */
5505 err = hdsp_wait_for_iobox(hdsp, 1000, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506
Tim Blechmann00c9ddd2008-11-09 12:50:52 +01005507 if (err < 0)
5508 return err;
5509
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510 if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
Takashi Iwaib0b981192005-10-20 18:29:58 +02005511 if ((err = hdsp_request_fw_loader(hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512 /* we don't fail as this can happen
5513 if userspace is not ready for
5514 firmware upload
5515 */
Takashi Iwaib0b981192005-10-20 18:29:58 +02005516 snd_printk(KERN_ERR "Hammerfall-DSP: couldn't get firmware from userspace. try using hdsploader\n");
5517 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518 /* init is complete, we return */
5519 return 0;
Tim Blechmann00c9ddd2008-11-09 12:50:52 +01005520 /* we defer initialization */
Takashi Iwaib0b981192005-10-20 18:29:58 +02005521 snd_printk(KERN_INFO "Hammerfall-DSP: card initialization pending : waiting for firmware\n");
5522 if ((err = snd_hdsp_create_hwdep(card, hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524 return 0;
5525 } else {
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005526 snd_printk(KERN_INFO "Hammerfall-DSP: Firmware already present, initializing card.\n");
Florian Faber28b26e12010-12-01 12:14:47 +01005527 if (hdsp_read(hdsp, HDSP_status2Register) & HDSP_version2)
5528 hdsp->io_type = RPM;
5529 else if (hdsp_read(hdsp, HDSP_status2Register) & HDSP_version1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530 hdsp->io_type = Multiface;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005531 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532 hdsp->io_type = Digiface;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533 }
5534 }
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005535
Takashi Iwaib0b981192005-10-20 18:29:58 +02005536 if ((err = snd_hdsp_enable_io(hdsp)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537 return err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005538
Takashi Iwaib0b981192005-10-20 18:29:58 +02005539 if (is_9652)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540 hdsp->io_type = H9652;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005541
Takashi Iwaib0b981192005-10-20 18:29:58 +02005542 if (is_9632)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543 hdsp->io_type = H9632;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544
Takashi Iwaib0b981192005-10-20 18:29:58 +02005545 if ((err = snd_hdsp_create_hwdep(card, hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546 return err;
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005547
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548 snd_hdsp_initialize_channels(hdsp);
5549 snd_hdsp_initialize_midi_flush(hdsp);
5550
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005551 hdsp->state |= HDSP_FirmwareLoaded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552
Takashi Iwaib0b981192005-10-20 18:29:58 +02005553 if ((err = snd_hdsp_create_alsa_devices(card, hdsp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005556 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557}
5558
Takashi Iwai55e957d2005-11-17 14:52:13 +01005559static int snd_hdsp_free(struct hdsp *hdsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005560{
5561 if (hdsp->port) {
5562 /* stop the audio, and cancel all interrupts */
5563 tasklet_kill(&hdsp->midi_tasklet);
5564 hdsp->control_register &= ~(HDSP_Start|HDSP_AudioInterruptEnable|HDSP_Midi0InterruptEnable|HDSP_Midi1InterruptEnable);
5565 hdsp_write (hdsp, HDSP_controlRegister, hdsp->control_register);
5566 }
5567
5568 if (hdsp->irq >= 0)
5569 free_irq(hdsp->irq, (void *)hdsp);
5570
5571 snd_hdsp_free_buffers(hdsp);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005572
Takashi Iwai90caaef2012-11-22 16:55:11 +01005573 if (hdsp->firmware)
5574 release_firmware(hdsp->firmware);
5575 vfree(hdsp->fw_uploaded);
5576
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577 if (hdsp->iobase)
5578 iounmap(hdsp->iobase);
5579
5580 if (hdsp->port)
5581 pci_release_regions(hdsp->pci);
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005582
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583 pci_disable_device(hdsp->pci);
5584 return 0;
5585}
5586
Takashi Iwai55e957d2005-11-17 14:52:13 +01005587static void snd_hdsp_card_free(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588{
Joe Perches9fe856e2010-09-04 18:52:54 -07005589 struct hdsp *hdsp = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590
5591 if (hdsp)
5592 snd_hdsp_free(hdsp);
5593}
5594
5595static int __devinit snd_hdsp_probe(struct pci_dev *pci,
5596 const struct pci_device_id *pci_id)
5597{
5598 static int dev;
Takashi Iwai55e957d2005-11-17 14:52:13 +01005599 struct hdsp *hdsp;
5600 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601 int err;
5602
5603 if (dev >= SNDRV_CARDS)
5604 return -ENODEV;
5605 if (!enable[dev]) {
5606 dev++;
5607 return -ENOENT;
5608 }
5609
Takashi Iwaie58de7b2008-12-28 16:44:30 +01005610 err = snd_card_create(index[dev], id[dev], THIS_MODULE,
5611 sizeof(struct hdsp), &card);
5612 if (err < 0)
5613 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614
Joe Perches9fe856e2010-09-04 18:52:54 -07005615 hdsp = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616 card->private_free = snd_hdsp_card_free;
5617 hdsp->dev = dev;
5618 hdsp->pci = pci;
5619 snd_card_set_dev(card, &pci->dev);
5620
5621 if ((err = snd_hdsp_create(card, hdsp)) < 0) {
5622 snd_card_free(card);
5623 return err;
5624 }
5625
5626 strcpy(card->shortname, "Hammerfall DSP");
Tim Blechmannf9ffc5d2009-02-20 19:38:16 +01005627 sprintf(card->longname, "%s at 0x%lx, irq %d", hdsp->card_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628 hdsp->port, hdsp->irq);
5629
5630 if ((err = snd_card_register(card)) < 0) {
5631 snd_card_free(card);
5632 return err;
5633 }
5634 pci_set_drvdata(pci, card);
5635 dev++;
5636 return 0;
5637}
5638
5639static void __devexit snd_hdsp_remove(struct pci_dev *pci)
5640{
5641 snd_card_free(pci_get_drvdata(pci));
5642 pci_set_drvdata(pci, NULL);
5643}
5644
Takashi Iwaie9f66d92012-04-24 12:25:00 +02005645static struct pci_driver hdsp_driver = {
Takashi Iwai3733e422011-06-10 16:20:20 +02005646 .name = KBUILD_MODNAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647 .id_table = snd_hdsp_ids,
5648 .probe = snd_hdsp_probe,
5649 .remove = __devexit_p(snd_hdsp_remove),
5650};
5651
Takashi Iwaie9f66d92012-04-24 12:25:00 +02005652module_pci_driver(hdsp_driver);