xref: /openbmc/linux/sound/pci/rme9652/hdsp.c (revision e58de7baf7de11f01a675cbbf6ecc8a2758b9ca5)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *   ALSA driver for RME Hammerfall DSP audio interface(s)
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *      Copyright (c) 2002  Paul Davis
51da177e4SLinus Torvalds  *                          Marcus Andersson
61da177e4SLinus Torvalds  *                          Thomas Charbonnel
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  *   This program is free software; you can redistribute it and/or modify
91da177e4SLinus Torvalds  *   it under the terms of the GNU General Public License as published by
101da177e4SLinus Torvalds  *   the Free Software Foundation; either version 2 of the License, or
111da177e4SLinus Torvalds  *   (at your option) any later version.
121da177e4SLinus Torvalds  *
131da177e4SLinus Torvalds  *   This program is distributed in the hope that it will be useful,
141da177e4SLinus Torvalds  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
151da177e4SLinus Torvalds  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
161da177e4SLinus Torvalds  *   GNU General Public License for more details.
171da177e4SLinus Torvalds  *
181da177e4SLinus Torvalds  *   You should have received a copy of the GNU General Public License
191da177e4SLinus Torvalds  *   along with this program; if not, write to the Free Software
201da177e4SLinus Torvalds  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
211da177e4SLinus Torvalds  *
221da177e4SLinus Torvalds  */
231da177e4SLinus Torvalds 
241da177e4SLinus Torvalds #include <linux/init.h>
251da177e4SLinus Torvalds #include <linux/delay.h>
261da177e4SLinus Torvalds #include <linux/interrupt.h>
271da177e4SLinus Torvalds #include <linux/slab.h>
281da177e4SLinus Torvalds #include <linux/pci.h>
291da177e4SLinus Torvalds #include <linux/firmware.h>
301da177e4SLinus Torvalds #include <linux/moduleparam.h>
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds #include <sound/core.h>
331da177e4SLinus Torvalds #include <sound/control.h>
341da177e4SLinus Torvalds #include <sound/pcm.h>
351da177e4SLinus Torvalds #include <sound/info.h>
361da177e4SLinus Torvalds #include <sound/asoundef.h>
371da177e4SLinus Torvalds #include <sound/rawmidi.h>
381da177e4SLinus Torvalds #include <sound/hwdep.h>
391da177e4SLinus Torvalds #include <sound/initval.h>
401da177e4SLinus Torvalds #include <sound/hdsp.h>
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds #include <asm/byteorder.h>
431da177e4SLinus Torvalds #include <asm/current.h>
441da177e4SLinus Torvalds #include <asm/io.h>
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-MAX */
471da177e4SLinus Torvalds static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
481da177e4SLinus Torvalds static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;	/* Enable this card */
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds module_param_array(index, int, NULL, 0444);
511da177e4SLinus Torvalds MODULE_PARM_DESC(index, "Index value for RME Hammerfall DSP interface.");
521da177e4SLinus Torvalds module_param_array(id, charp, NULL, 0444);
531da177e4SLinus Torvalds MODULE_PARM_DESC(id, "ID string for RME Hammerfall DSP interface.");
541da177e4SLinus Torvalds module_param_array(enable, bool, NULL, 0444);
551da177e4SLinus Torvalds MODULE_PARM_DESC(enable, "Enable/disable specific Hammerfall DSP soundcards.");
561da177e4SLinus Torvalds MODULE_AUTHOR("Paul Davis <paul@linuxaudiosystems.com>, Marcus Andersson, Thomas Charbonnel <thomas@undata.org>");
571da177e4SLinus Torvalds MODULE_DESCRIPTION("RME Hammerfall DSP");
581da177e4SLinus Torvalds MODULE_LICENSE("GPL");
591da177e4SLinus Torvalds MODULE_SUPPORTED_DEVICE("{{RME Hammerfall-DSP},"
601da177e4SLinus Torvalds 	        "{RME HDSP-9652},"
611da177e4SLinus Torvalds 		"{RME HDSP-9632}}");
627e0af29dSClemens Ladisch #ifdef HDSP_FW_LOADER
637e0af29dSClemens Ladisch MODULE_FIRMWARE("multiface_firmware.bin");
647e0af29dSClemens Ladisch MODULE_FIRMWARE("multiface_firmware_rev11.bin");
657e0af29dSClemens Ladisch MODULE_FIRMWARE("digiface_firmware.bin");
667e0af29dSClemens Ladisch MODULE_FIRMWARE("digiface_firmware_rev11.bin");
677e0af29dSClemens Ladisch #endif
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds #define HDSP_MAX_CHANNELS        26
701da177e4SLinus Torvalds #define HDSP_MAX_DS_CHANNELS     14
711da177e4SLinus Torvalds #define HDSP_MAX_QS_CHANNELS     8
721da177e4SLinus Torvalds #define DIGIFACE_SS_CHANNELS     26
731da177e4SLinus Torvalds #define DIGIFACE_DS_CHANNELS     14
741da177e4SLinus Torvalds #define MULTIFACE_SS_CHANNELS    18
751da177e4SLinus Torvalds #define MULTIFACE_DS_CHANNELS    14
761da177e4SLinus Torvalds #define H9652_SS_CHANNELS        26
771da177e4SLinus Torvalds #define H9652_DS_CHANNELS        14
781da177e4SLinus Torvalds /* This does not include possible Analog Extension Boards
791da177e4SLinus Torvalds    AEBs are detected at card initialization
801da177e4SLinus Torvalds */
811da177e4SLinus Torvalds #define H9632_SS_CHANNELS	 12
821da177e4SLinus Torvalds #define H9632_DS_CHANNELS	 8
831da177e4SLinus Torvalds #define H9632_QS_CHANNELS	 4
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds /* Write registers. These are defined as byte-offsets from the iobase value.
861da177e4SLinus Torvalds  */
871da177e4SLinus Torvalds #define HDSP_resetPointer               0
88d7923b2aSRemy Bruno #define HDSP_freqReg			0
891da177e4SLinus Torvalds #define HDSP_outputBufferAddress	32
901da177e4SLinus Torvalds #define HDSP_inputBufferAddress		36
911da177e4SLinus Torvalds #define HDSP_controlRegister		64
921da177e4SLinus Torvalds #define HDSP_interruptConfirmation	96
931da177e4SLinus Torvalds #define HDSP_outputEnable	  	128
941da177e4SLinus Torvalds #define HDSP_control2Reg		256
951da177e4SLinus Torvalds #define HDSP_midiDataOut0  		352
961da177e4SLinus Torvalds #define HDSP_midiDataOut1  		356
971da177e4SLinus Torvalds #define HDSP_fifoData  			368
981da177e4SLinus Torvalds #define HDSP_inputEnable	 	384
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds /* Read registers. These are defined as byte-offsets from the iobase value
1011da177e4SLinus Torvalds  */
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds #define HDSP_statusRegister    0
1041da177e4SLinus Torvalds #define HDSP_timecode        128
1051da177e4SLinus Torvalds #define HDSP_status2Register 192
1061da177e4SLinus Torvalds #define HDSP_midiDataIn0     360
1071da177e4SLinus Torvalds #define HDSP_midiDataIn1     364
1081da177e4SLinus Torvalds #define HDSP_midiStatusOut0  384
1091da177e4SLinus Torvalds #define HDSP_midiStatusOut1  388
1101da177e4SLinus Torvalds #define HDSP_midiStatusIn0   392
1111da177e4SLinus Torvalds #define HDSP_midiStatusIn1   396
1121da177e4SLinus Torvalds #define HDSP_fifoStatus      400
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds /* the meters are regular i/o-mapped registers, but offset
1151da177e4SLinus Torvalds    considerably from the rest. the peak registers are reset
1161da177e4SLinus Torvalds    when read; the least-significant 4 bits are full-scale counters;
1171da177e4SLinus Torvalds    the actual peak value is in the most-significant 24 bits.
1181da177e4SLinus Torvalds */
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds #define HDSP_playbackPeakLevel  4096  /* 26 * 32 bit values */
1211da177e4SLinus Torvalds #define HDSP_inputPeakLevel     4224  /* 26 * 32 bit values */
1221da177e4SLinus Torvalds #define HDSP_outputPeakLevel    4352  /* (26+2) * 32 bit values */
1231da177e4SLinus Torvalds #define HDSP_playbackRmsLevel   4612  /* 26 * 64 bit values */
1241da177e4SLinus Torvalds #define HDSP_inputRmsLevel      4868  /* 26 * 64 bit values */
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds /* This is for H9652 cards
1281da177e4SLinus Torvalds    Peak values are read downward from the base
1291da177e4SLinus Torvalds    Rms values are read upward
1301da177e4SLinus Torvalds    There are rms values for the outputs too
1311da177e4SLinus Torvalds    26*3 values are read in ss mode
1321da177e4SLinus Torvalds    14*3 in ds mode, with no gap between values
1331da177e4SLinus Torvalds */
1341da177e4SLinus Torvalds #define HDSP_9652_peakBase	7164
1351da177e4SLinus Torvalds #define HDSP_9652_rmsBase	4096
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds /* c.f. the hdsp_9632_meters_t struct */
1381da177e4SLinus Torvalds #define HDSP_9632_metersBase	4096
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds #define HDSP_IO_EXTENT     7168
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds /* control2 register bits */
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds #define HDSP_TMS                0x01
1451da177e4SLinus Torvalds #define HDSP_TCK                0x02
1461da177e4SLinus Torvalds #define HDSP_TDI                0x04
1471da177e4SLinus Torvalds #define HDSP_JTAG               0x08
1481da177e4SLinus Torvalds #define HDSP_PWDN               0x10
1491da177e4SLinus Torvalds #define HDSP_PROGRAM	        0x020
1501da177e4SLinus Torvalds #define HDSP_CONFIG_MODE_0	0x040
1511da177e4SLinus Torvalds #define HDSP_CONFIG_MODE_1	0x080
1521da177e4SLinus Torvalds #define HDSP_VERSION_BIT	0x100
1531da177e4SLinus Torvalds #define HDSP_BIGENDIAN_MODE     0x200
1541da177e4SLinus Torvalds #define HDSP_RD_MULTIPLE        0x400
1551da177e4SLinus Torvalds #define HDSP_9652_ENABLE_MIXER  0x800
1561da177e4SLinus Torvalds #define HDSP_TDO                0x10000000
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds #define HDSP_S_PROGRAM     	(HDSP_PROGRAM|HDSP_CONFIG_MODE_0)
1591da177e4SLinus Torvalds #define HDSP_S_LOAD		(HDSP_PROGRAM|HDSP_CONFIG_MODE_1)
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds /* Control Register bits */
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds #define HDSP_Start                (1<<0)  /* start engine */
1641da177e4SLinus Torvalds #define HDSP_Latency0             (1<<1)  /* buffer size = 2^n where n is defined by Latency{2,1,0} */
1651da177e4SLinus Torvalds #define HDSP_Latency1             (1<<2)  /* [ see above ] */
1661da177e4SLinus Torvalds #define HDSP_Latency2             (1<<3)  /* [ see above ] */
1671da177e4SLinus Torvalds #define HDSP_ClockModeMaster      (1<<4)  /* 1=Master, 0=Slave/Autosync */
1681da177e4SLinus Torvalds #define HDSP_AudioInterruptEnable (1<<5)  /* what do you think ? */
1691da177e4SLinus Torvalds #define HDSP_Frequency0           (1<<6)  /* 0=44.1kHz/88.2kHz/176.4kHz 1=48kHz/96kHz/192kHz */
1701da177e4SLinus Torvalds #define HDSP_Frequency1           (1<<7)  /* 0=32kHz/64kHz/128kHz */
1711da177e4SLinus Torvalds #define HDSP_DoubleSpeed          (1<<8)  /* 0=normal speed, 1=double speed */
1721da177e4SLinus Torvalds #define HDSP_SPDIFProfessional    (1<<9)  /* 0=consumer, 1=professional */
1731da177e4SLinus Torvalds #define HDSP_SPDIFEmphasis        (1<<10) /* 0=none, 1=on */
1741da177e4SLinus Torvalds #define HDSP_SPDIFNonAudio        (1<<11) /* 0=off, 1=on */
1751da177e4SLinus Torvalds #define HDSP_SPDIFOpticalOut      (1<<12) /* 1=use 1st ADAT connector for SPDIF, 0=do not */
1761da177e4SLinus Torvalds #define HDSP_SyncRef2             (1<<13)
1771da177e4SLinus Torvalds #define HDSP_SPDIFInputSelect0    (1<<14)
1781da177e4SLinus Torvalds #define HDSP_SPDIFInputSelect1    (1<<15)
1791da177e4SLinus Torvalds #define HDSP_SyncRef0             (1<<16)
1801da177e4SLinus Torvalds #define HDSP_SyncRef1             (1<<17)
1811da177e4SLinus Torvalds #define HDSP_AnalogExtensionBoard (1<<18) /* For H9632 cards */
1821da177e4SLinus Torvalds #define HDSP_XLRBreakoutCable     (1<<20) /* For H9632 cards */
1831da177e4SLinus Torvalds #define HDSP_Midi0InterruptEnable (1<<22)
1841da177e4SLinus Torvalds #define HDSP_Midi1InterruptEnable (1<<23)
1851da177e4SLinus Torvalds #define HDSP_LineOut              (1<<24)
1861da177e4SLinus Torvalds #define HDSP_ADGain0		  (1<<25) /* From here : H9632 specific */
1871da177e4SLinus Torvalds #define HDSP_ADGain1		  (1<<26)
1881da177e4SLinus Torvalds #define HDSP_DAGain0		  (1<<27)
1891da177e4SLinus Torvalds #define HDSP_DAGain1		  (1<<28)
1901da177e4SLinus Torvalds #define HDSP_PhoneGain0		  (1<<29)
1911da177e4SLinus Torvalds #define HDSP_PhoneGain1		  (1<<30)
1921da177e4SLinus Torvalds #define HDSP_QuadSpeed	  	  (1<<31)
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds #define HDSP_ADGainMask       (HDSP_ADGain0|HDSP_ADGain1)
1951da177e4SLinus Torvalds #define HDSP_ADGainMinus10dBV  HDSP_ADGainMask
1961da177e4SLinus Torvalds #define HDSP_ADGainPlus4dBu   (HDSP_ADGain0)
1971da177e4SLinus Torvalds #define HDSP_ADGainLowGain     0
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds #define HDSP_DAGainMask         (HDSP_DAGain0|HDSP_DAGain1)
2001da177e4SLinus Torvalds #define HDSP_DAGainHighGain      HDSP_DAGainMask
2011da177e4SLinus Torvalds #define HDSP_DAGainPlus4dBu     (HDSP_DAGain0)
2021da177e4SLinus Torvalds #define HDSP_DAGainMinus10dBV    0
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds #define HDSP_PhoneGainMask      (HDSP_PhoneGain0|HDSP_PhoneGain1)
2051da177e4SLinus Torvalds #define HDSP_PhoneGain0dB        HDSP_PhoneGainMask
2061da177e4SLinus Torvalds #define HDSP_PhoneGainMinus6dB  (HDSP_PhoneGain0)
2071da177e4SLinus Torvalds #define HDSP_PhoneGainMinus12dB  0
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds #define HDSP_LatencyMask    (HDSP_Latency0|HDSP_Latency1|HDSP_Latency2)
2101da177e4SLinus Torvalds #define HDSP_FrequencyMask  (HDSP_Frequency0|HDSP_Frequency1|HDSP_DoubleSpeed|HDSP_QuadSpeed)
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds #define HDSP_SPDIFInputMask    (HDSP_SPDIFInputSelect0|HDSP_SPDIFInputSelect1)
2131da177e4SLinus Torvalds #define HDSP_SPDIFInputADAT1    0
2141da177e4SLinus Torvalds #define HDSP_SPDIFInputCoaxial (HDSP_SPDIFInputSelect0)
2151da177e4SLinus Torvalds #define HDSP_SPDIFInputCdrom   (HDSP_SPDIFInputSelect1)
2161da177e4SLinus Torvalds #define HDSP_SPDIFInputAES     (HDSP_SPDIFInputSelect0|HDSP_SPDIFInputSelect1)
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds #define HDSP_SyncRefMask        (HDSP_SyncRef0|HDSP_SyncRef1|HDSP_SyncRef2)
2191da177e4SLinus Torvalds #define HDSP_SyncRef_ADAT1       0
2201da177e4SLinus Torvalds #define HDSP_SyncRef_ADAT2      (HDSP_SyncRef0)
2211da177e4SLinus Torvalds #define HDSP_SyncRef_ADAT3      (HDSP_SyncRef1)
2221da177e4SLinus Torvalds #define HDSP_SyncRef_SPDIF      (HDSP_SyncRef0|HDSP_SyncRef1)
2231da177e4SLinus Torvalds #define HDSP_SyncRef_WORD       (HDSP_SyncRef2)
2241da177e4SLinus Torvalds #define HDSP_SyncRef_ADAT_SYNC  (HDSP_SyncRef0|HDSP_SyncRef2)
2251da177e4SLinus Torvalds 
2261da177e4SLinus Torvalds /* Sample Clock Sources */
2271da177e4SLinus Torvalds 
2281da177e4SLinus Torvalds #define HDSP_CLOCK_SOURCE_AUTOSYNC           0
2291da177e4SLinus Torvalds #define HDSP_CLOCK_SOURCE_INTERNAL_32KHZ     1
2301da177e4SLinus Torvalds #define HDSP_CLOCK_SOURCE_INTERNAL_44_1KHZ   2
2311da177e4SLinus Torvalds #define HDSP_CLOCK_SOURCE_INTERNAL_48KHZ     3
2321da177e4SLinus Torvalds #define HDSP_CLOCK_SOURCE_INTERNAL_64KHZ     4
2331da177e4SLinus Torvalds #define HDSP_CLOCK_SOURCE_INTERNAL_88_2KHZ   5
2341da177e4SLinus Torvalds #define HDSP_CLOCK_SOURCE_INTERNAL_96KHZ     6
2351da177e4SLinus Torvalds #define HDSP_CLOCK_SOURCE_INTERNAL_128KHZ    7
2361da177e4SLinus Torvalds #define HDSP_CLOCK_SOURCE_INTERNAL_176_4KHZ  8
2371da177e4SLinus Torvalds #define HDSP_CLOCK_SOURCE_INTERNAL_192KHZ    9
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds /* Preferred sync reference choices - used by "pref_sync_ref" control switch */
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds #define HDSP_SYNC_FROM_WORD      0
2421da177e4SLinus Torvalds #define HDSP_SYNC_FROM_SPDIF     1
2431da177e4SLinus Torvalds #define HDSP_SYNC_FROM_ADAT1     2
2441da177e4SLinus Torvalds #define HDSP_SYNC_FROM_ADAT_SYNC 3
2451da177e4SLinus Torvalds #define HDSP_SYNC_FROM_ADAT2     4
2461da177e4SLinus Torvalds #define HDSP_SYNC_FROM_ADAT3     5
2471da177e4SLinus Torvalds 
2481da177e4SLinus Torvalds /* SyncCheck status */
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds #define HDSP_SYNC_CHECK_NO_LOCK 0
2511da177e4SLinus Torvalds #define HDSP_SYNC_CHECK_LOCK    1
2521da177e4SLinus Torvalds #define HDSP_SYNC_CHECK_SYNC	2
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds /* AutoSync references - used by "autosync_ref" control switch */
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds #define HDSP_AUTOSYNC_FROM_WORD      0
2571da177e4SLinus Torvalds #define HDSP_AUTOSYNC_FROM_ADAT_SYNC 1
2581da177e4SLinus Torvalds #define HDSP_AUTOSYNC_FROM_SPDIF     2
2591da177e4SLinus Torvalds #define HDSP_AUTOSYNC_FROM_NONE	     3
2601da177e4SLinus Torvalds #define HDSP_AUTOSYNC_FROM_ADAT1     4
2611da177e4SLinus Torvalds #define HDSP_AUTOSYNC_FROM_ADAT2     5
2621da177e4SLinus Torvalds #define HDSP_AUTOSYNC_FROM_ADAT3     6
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds /* Possible sources of S/PDIF input */
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds #define HDSP_SPDIFIN_OPTICAL  0	/* optical  (ADAT1) */
2671da177e4SLinus Torvalds #define HDSP_SPDIFIN_COAXIAL  1	/* coaxial (RCA) */
2681da177e4SLinus Torvalds #define HDSP_SPDIFIN_INTERNAL 2	/* internal (CDROM) */
2691da177e4SLinus Torvalds #define HDSP_SPDIFIN_AES      3 /* xlr for H9632 (AES)*/
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds #define HDSP_Frequency32KHz    HDSP_Frequency0
2721da177e4SLinus Torvalds #define HDSP_Frequency44_1KHz  HDSP_Frequency1
2731da177e4SLinus Torvalds #define HDSP_Frequency48KHz    (HDSP_Frequency1|HDSP_Frequency0)
2741da177e4SLinus Torvalds #define HDSP_Frequency64KHz    (HDSP_DoubleSpeed|HDSP_Frequency0)
2751da177e4SLinus Torvalds #define HDSP_Frequency88_2KHz  (HDSP_DoubleSpeed|HDSP_Frequency1)
2761da177e4SLinus Torvalds #define HDSP_Frequency96KHz    (HDSP_DoubleSpeed|HDSP_Frequency1|HDSP_Frequency0)
2771da177e4SLinus Torvalds /* For H9632 cards */
2781da177e4SLinus Torvalds #define HDSP_Frequency128KHz   (HDSP_QuadSpeed|HDSP_DoubleSpeed|HDSP_Frequency0)
2791da177e4SLinus Torvalds #define HDSP_Frequency176_4KHz (HDSP_QuadSpeed|HDSP_DoubleSpeed|HDSP_Frequency1)
2801da177e4SLinus Torvalds #define HDSP_Frequency192KHz   (HDSP_QuadSpeed|HDSP_DoubleSpeed|HDSP_Frequency1|HDSP_Frequency0)
281e4b6088cSJulian Cable /* RME says n = 104857600000000, but in the windows MADI driver, I see:
282e4b6088cSJulian Cable 	return 104857600000000 / rate; // 100 MHz
283e4b6088cSJulian Cable 	return 110100480000000 / rate; // 105 MHz
284e4b6088cSJulian Cable */
285e4b6088cSJulian Cable #define DDS_NUMERATOR 104857600000000ULL;  /*  =  2^20 * 10^8 */
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds #define hdsp_encode_latency(x)       (((x)<<1) & HDSP_LatencyMask)
2881da177e4SLinus Torvalds #define hdsp_decode_latency(x)       (((x) & HDSP_LatencyMask)>>1)
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds #define hdsp_encode_spdif_in(x) (((x)&0x3)<<14)
2911da177e4SLinus Torvalds #define hdsp_decode_spdif_in(x) (((x)>>14)&0x3)
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds /* Status Register bits */
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds #define HDSP_audioIRQPending    (1<<0)
2961da177e4SLinus Torvalds #define HDSP_Lock2              (1<<1)     /* this is for Digiface and H9652 */
2971da177e4SLinus Torvalds #define HDSP_spdifFrequency3	HDSP_Lock2 /* this is for H9632 only */
2981da177e4SLinus Torvalds #define HDSP_Lock1              (1<<2)
2991da177e4SLinus Torvalds #define HDSP_Lock0              (1<<3)
3001da177e4SLinus Torvalds #define HDSP_SPDIFSync          (1<<4)
3011da177e4SLinus Torvalds #define HDSP_TimecodeLock       (1<<5)
3021da177e4SLinus Torvalds #define HDSP_BufferPositionMask 0x000FFC0 /* Bit 6..15 : h/w buffer pointer */
3031da177e4SLinus Torvalds #define HDSP_Sync2              (1<<16)
3041da177e4SLinus Torvalds #define HDSP_Sync1              (1<<17)
3051da177e4SLinus Torvalds #define HDSP_Sync0              (1<<18)
3061da177e4SLinus Torvalds #define HDSP_DoubleSpeedStatus  (1<<19)
3071da177e4SLinus Torvalds #define HDSP_ConfigError        (1<<20)
3081da177e4SLinus Torvalds #define HDSP_DllError           (1<<21)
3091da177e4SLinus Torvalds #define HDSP_spdifFrequency0    (1<<22)
3101da177e4SLinus Torvalds #define HDSP_spdifFrequency1    (1<<23)
3111da177e4SLinus Torvalds #define HDSP_spdifFrequency2    (1<<24)
3121da177e4SLinus Torvalds #define HDSP_SPDIFErrorFlag     (1<<25)
3131da177e4SLinus Torvalds #define HDSP_BufferID           (1<<26)
3141da177e4SLinus Torvalds #define HDSP_TimecodeSync       (1<<27)
3151da177e4SLinus Torvalds #define HDSP_AEBO          	(1<<28) /* H9632 specific Analog Extension Boards */
3161da177e4SLinus Torvalds #define HDSP_AEBI		(1<<29) /* 0 = present, 1 = absent */
3171da177e4SLinus Torvalds #define HDSP_midi0IRQPending    (1<<30)
3181da177e4SLinus Torvalds #define HDSP_midi1IRQPending    (1<<31)
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds #define HDSP_spdifFrequencyMask    (HDSP_spdifFrequency0|HDSP_spdifFrequency1|HDSP_spdifFrequency2)
32147ba97f8SRemy Bruno #define HDSP_spdifFrequencyMask_9632 (HDSP_spdifFrequency0|\
32247ba97f8SRemy Bruno 				      HDSP_spdifFrequency1|\
32347ba97f8SRemy Bruno 				      HDSP_spdifFrequency2|\
32447ba97f8SRemy Bruno 				      HDSP_spdifFrequency3)
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds #define HDSP_spdifFrequency32KHz   (HDSP_spdifFrequency0)
3271da177e4SLinus Torvalds #define HDSP_spdifFrequency44_1KHz (HDSP_spdifFrequency1)
3281da177e4SLinus Torvalds #define HDSP_spdifFrequency48KHz   (HDSP_spdifFrequency0|HDSP_spdifFrequency1)
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds #define HDSP_spdifFrequency64KHz   (HDSP_spdifFrequency2)
3311da177e4SLinus Torvalds #define HDSP_spdifFrequency88_2KHz (HDSP_spdifFrequency0|HDSP_spdifFrequency2)
3321da177e4SLinus Torvalds #define HDSP_spdifFrequency96KHz   (HDSP_spdifFrequency2|HDSP_spdifFrequency1)
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds /* This is for H9632 cards */
33547ba97f8SRemy Bruno #define HDSP_spdifFrequency128KHz   (HDSP_spdifFrequency0|\
33647ba97f8SRemy Bruno 				     HDSP_spdifFrequency1|\
33747ba97f8SRemy Bruno 				     HDSP_spdifFrequency2)
3381da177e4SLinus Torvalds #define HDSP_spdifFrequency176_4KHz HDSP_spdifFrequency3
3391da177e4SLinus Torvalds #define HDSP_spdifFrequency192KHz   (HDSP_spdifFrequency3|HDSP_spdifFrequency0)
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds /* Status2 Register bits */
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds #define HDSP_version0     (1<<0)
3441da177e4SLinus Torvalds #define HDSP_version1     (1<<1)
3451da177e4SLinus Torvalds #define HDSP_version2     (1<<2)
3461da177e4SLinus Torvalds #define HDSP_wc_lock      (1<<3)
3471da177e4SLinus Torvalds #define HDSP_wc_sync      (1<<4)
3481da177e4SLinus Torvalds #define HDSP_inp_freq0    (1<<5)
3491da177e4SLinus Torvalds #define HDSP_inp_freq1    (1<<6)
3501da177e4SLinus Torvalds #define HDSP_inp_freq2    (1<<7)
3511da177e4SLinus Torvalds #define HDSP_SelSyncRef0  (1<<8)
3521da177e4SLinus Torvalds #define HDSP_SelSyncRef1  (1<<9)
3531da177e4SLinus Torvalds #define HDSP_SelSyncRef2  (1<<10)
3541da177e4SLinus Torvalds 
3551da177e4SLinus Torvalds #define HDSP_wc_valid (HDSP_wc_lock|HDSP_wc_sync)
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds #define HDSP_systemFrequencyMask (HDSP_inp_freq0|HDSP_inp_freq1|HDSP_inp_freq2)
3581da177e4SLinus Torvalds #define HDSP_systemFrequency32   (HDSP_inp_freq0)
3591da177e4SLinus Torvalds #define HDSP_systemFrequency44_1 (HDSP_inp_freq1)
3601da177e4SLinus Torvalds #define HDSP_systemFrequency48   (HDSP_inp_freq0|HDSP_inp_freq1)
3611da177e4SLinus Torvalds #define HDSP_systemFrequency64   (HDSP_inp_freq2)
3621da177e4SLinus Torvalds #define HDSP_systemFrequency88_2 (HDSP_inp_freq0|HDSP_inp_freq2)
3631da177e4SLinus Torvalds #define HDSP_systemFrequency96   (HDSP_inp_freq1|HDSP_inp_freq2)
3641da177e4SLinus Torvalds /* FIXME : more values for 9632 cards ? */
3651da177e4SLinus Torvalds 
3661da177e4SLinus Torvalds #define HDSP_SelSyncRefMask        (HDSP_SelSyncRef0|HDSP_SelSyncRef1|HDSP_SelSyncRef2)
3671da177e4SLinus Torvalds #define HDSP_SelSyncRef_ADAT1      0
3681da177e4SLinus Torvalds #define HDSP_SelSyncRef_ADAT2      (HDSP_SelSyncRef0)
3691da177e4SLinus Torvalds #define HDSP_SelSyncRef_ADAT3      (HDSP_SelSyncRef1)
3701da177e4SLinus Torvalds #define HDSP_SelSyncRef_SPDIF      (HDSP_SelSyncRef0|HDSP_SelSyncRef1)
3711da177e4SLinus Torvalds #define HDSP_SelSyncRef_WORD       (HDSP_SelSyncRef2)
3721da177e4SLinus Torvalds #define HDSP_SelSyncRef_ADAT_SYNC  (HDSP_SelSyncRef0|HDSP_SelSyncRef2)
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds /* Card state flags */
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds #define HDSP_InitializationComplete  (1<<0)
3771da177e4SLinus Torvalds #define HDSP_FirmwareLoaded	     (1<<1)
3781da177e4SLinus Torvalds #define HDSP_FirmwareCached	     (1<<2)
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds /* FIFO wait times, defined in terms of 1/10ths of msecs */
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds #define HDSP_LONG_WAIT	 5000
3831da177e4SLinus Torvalds #define HDSP_SHORT_WAIT  30
3841da177e4SLinus Torvalds 
3851da177e4SLinus Torvalds #define UNITY_GAIN                       32768
3861da177e4SLinus Torvalds #define MINUS_INFINITY_GAIN              0
3871da177e4SLinus Torvalds 
3881da177e4SLinus Torvalds /* the size of a substream (1 mono data stream) */
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds #define HDSP_CHANNEL_BUFFER_SAMPLES  (16*1024)
3911da177e4SLinus Torvalds #define HDSP_CHANNEL_BUFFER_BYTES    (4*HDSP_CHANNEL_BUFFER_SAMPLES)
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds /* the size of the area we need to allocate for DMA transfers. the
3941da177e4SLinus Torvalds    size is the same regardless of the number of channels - the
3951da177e4SLinus Torvalds    Multiface still uses the same memory area.
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds    Note that we allocate 1 more channel than is apparently needed
3981da177e4SLinus Torvalds    because the h/w seems to write 1 byte beyond the end of the last
3991da177e4SLinus Torvalds    page. Sigh.
4001da177e4SLinus Torvalds */
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds #define HDSP_DMA_AREA_BYTES ((HDSP_MAX_CHANNELS+1) * HDSP_CHANNEL_BUFFER_BYTES)
4031da177e4SLinus Torvalds #define HDSP_DMA_AREA_KILOBYTES (HDSP_DMA_AREA_BYTES/1024)
4041da177e4SLinus Torvalds 
4051da177e4SLinus Torvalds /* use hotplug firmeare loader? */
4061da177e4SLinus Torvalds #if defined(CONFIG_FW_LOADER) || defined(CONFIG_FW_LOADER_MODULE)
407140432fdSTakashi Iwai #if !defined(HDSP_USE_HWDEP_LOADER) && !defined(CONFIG_SND_HDSP)
4081da177e4SLinus Torvalds #define HDSP_FW_LOADER
4091da177e4SLinus Torvalds #endif
4101da177e4SLinus Torvalds #endif
4111da177e4SLinus Torvalds 
41255e957d8STakashi Iwai struct hdsp_9632_meters {
4131da177e4SLinus Torvalds     u32 input_peak[16];
4141da177e4SLinus Torvalds     u32 playback_peak[16];
4151da177e4SLinus Torvalds     u32 output_peak[16];
4161da177e4SLinus Torvalds     u32 xxx_peak[16];
4171da177e4SLinus Torvalds     u32 padding[64];
4181da177e4SLinus Torvalds     u32 input_rms_low[16];
4191da177e4SLinus Torvalds     u32 playback_rms_low[16];
4201da177e4SLinus Torvalds     u32 output_rms_low[16];
4211da177e4SLinus Torvalds     u32 xxx_rms_low[16];
4221da177e4SLinus Torvalds     u32 input_rms_high[16];
4231da177e4SLinus Torvalds     u32 playback_rms_high[16];
4241da177e4SLinus Torvalds     u32 output_rms_high[16];
4251da177e4SLinus Torvalds     u32 xxx_rms_high[16];
4261da177e4SLinus Torvalds };
4271da177e4SLinus Torvalds 
42855e957d8STakashi Iwai struct hdsp_midi {
42955e957d8STakashi Iwai     struct hdsp             *hdsp;
4301da177e4SLinus Torvalds     int                      id;
43155e957d8STakashi Iwai     struct snd_rawmidi           *rmidi;
43255e957d8STakashi Iwai     struct snd_rawmidi_substream *input;
43355e957d8STakashi Iwai     struct snd_rawmidi_substream *output;
4341da177e4SLinus Torvalds     char                     istimer; /* timer in use */
4351da177e4SLinus Torvalds     struct timer_list	     timer;
4361da177e4SLinus Torvalds     spinlock_t               lock;
4371da177e4SLinus Torvalds     int			     pending;
4381da177e4SLinus Torvalds };
4391da177e4SLinus Torvalds 
44055e957d8STakashi Iwai struct hdsp {
4411da177e4SLinus Torvalds 	spinlock_t            lock;
44255e957d8STakashi Iwai 	struct snd_pcm_substream *capture_substream;
44355e957d8STakashi Iwai 	struct snd_pcm_substream *playback_substream;
44455e957d8STakashi Iwai         struct hdsp_midi      midi[2];
4451da177e4SLinus Torvalds 	struct tasklet_struct midi_tasklet;
4461da177e4SLinus Torvalds 	int		      use_midi_tasklet;
4471da177e4SLinus Torvalds 	int                   precise_ptr;
4481da177e4SLinus Torvalds 	u32                   control_register;	     /* cached value */
4491da177e4SLinus Torvalds 	u32                   control2_register;     /* cached value */
4501da177e4SLinus Torvalds 	u32                   creg_spdif;
4511da177e4SLinus Torvalds 	u32                   creg_spdif_stream;
452e3ea4d89STakashi Iwai 	int                   clock_source_locked;
4531da177e4SLinus Torvalds 	char                 *card_name;	     /* digiface/multiface */
45455e957d8STakashi Iwai 	enum HDSP_IO_Type     io_type;               /* ditto, but for code use */
4551da177e4SLinus Torvalds         unsigned short        firmware_rev;
4561da177e4SLinus Torvalds 	unsigned short	      state;		     /* stores state bits */
4571da177e4SLinus Torvalds 	u32		      firmware_cache[24413]; /* this helps recover from accidental iobox power failure */
4581da177e4SLinus Torvalds 	size_t                period_bytes; 	     /* guess what this is */
4591da177e4SLinus Torvalds 	unsigned char	      max_channels;
4601da177e4SLinus Torvalds 	unsigned char	      qs_in_channels;	     /* quad speed mode for H9632 */
4611da177e4SLinus Torvalds 	unsigned char         ds_in_channels;
4621da177e4SLinus Torvalds 	unsigned char         ss_in_channels;	    /* different for multiface/digiface */
4631da177e4SLinus Torvalds 	unsigned char	      qs_out_channels;
4641da177e4SLinus Torvalds 	unsigned char         ds_out_channels;
4651da177e4SLinus Torvalds 	unsigned char         ss_out_channels;
4661da177e4SLinus Torvalds 
4671da177e4SLinus Torvalds 	struct snd_dma_buffer capture_dma_buf;
4681da177e4SLinus Torvalds 	struct snd_dma_buffer playback_dma_buf;
4691da177e4SLinus Torvalds 	unsigned char        *capture_buffer;	    /* suitably aligned address */
4701da177e4SLinus Torvalds 	unsigned char        *playback_buffer;	    /* suitably aligned address */
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds 	pid_t                 capture_pid;
4731da177e4SLinus Torvalds 	pid_t                 playback_pid;
4741da177e4SLinus Torvalds 	int                   running;
4751da177e4SLinus Torvalds 	int                   system_sample_rate;
4761da177e4SLinus Torvalds 	char                 *channel_map;
4771da177e4SLinus Torvalds 	int                   dev;
4781da177e4SLinus Torvalds 	int                   irq;
4791da177e4SLinus Torvalds 	unsigned long         port;
4801da177e4SLinus Torvalds         void __iomem         *iobase;
48155e957d8STakashi Iwai 	struct snd_card *card;
48255e957d8STakashi Iwai 	struct snd_pcm *pcm;
48355e957d8STakashi Iwai 	struct snd_hwdep          *hwdep;
4841da177e4SLinus Torvalds 	struct pci_dev       *pci;
48555e957d8STakashi Iwai 	struct snd_kcontrol *spdif_ctl;
4861da177e4SLinus Torvalds         unsigned short        mixer_matrix[HDSP_MATRIX_MIXER_SIZE];
487d7923b2aSRemy Bruno 	unsigned int          dds_value; /* last value written to freq register */
4881da177e4SLinus Torvalds };
4891da177e4SLinus Torvalds 
4901da177e4SLinus Torvalds /* These tables map the ALSA channels 1..N to the channels that we
4911da177e4SLinus Torvalds    need to use in order to find the relevant channel buffer. RME
4921da177e4SLinus Torvalds    refer to this kind of mapping as between "the ADAT channel and
4931da177e4SLinus Torvalds    the DMA channel." We index it using the logical audio channel,
4941da177e4SLinus Torvalds    and the value is the DMA channel (i.e. channel buffer number)
4951da177e4SLinus Torvalds    where the data for that channel can be read/written from/to.
4961da177e4SLinus Torvalds */
4971da177e4SLinus Torvalds 
4981da177e4SLinus Torvalds static char channel_map_df_ss[HDSP_MAX_CHANNELS] = {
4991da177e4SLinus Torvalds 	0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
5001da177e4SLinus Torvalds 	18, 19, 20, 21, 22, 23, 24, 25
5011da177e4SLinus Torvalds };
5021da177e4SLinus Torvalds 
5031da177e4SLinus Torvalds static char channel_map_mf_ss[HDSP_MAX_CHANNELS] = { /* Multiface */
5041da177e4SLinus Torvalds 	/* Analog */
5051da177e4SLinus Torvalds 	0, 1, 2, 3, 4, 5, 6, 7,
5061da177e4SLinus Torvalds 	/* ADAT 2 */
5071da177e4SLinus Torvalds 	16, 17, 18, 19, 20, 21, 22, 23,
5081da177e4SLinus Torvalds 	/* SPDIF */
5091da177e4SLinus Torvalds 	24, 25,
5101da177e4SLinus Torvalds 	-1, -1, -1, -1, -1, -1, -1, -1
5111da177e4SLinus Torvalds };
5121da177e4SLinus Torvalds 
5131da177e4SLinus Torvalds static char channel_map_ds[HDSP_MAX_CHANNELS] = {
5141da177e4SLinus Torvalds 	/* ADAT channels are remapped */
5151da177e4SLinus Torvalds 	1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23,
5161da177e4SLinus Torvalds 	/* channels 12 and 13 are S/PDIF */
5171da177e4SLinus Torvalds 	24, 25,
5181da177e4SLinus Torvalds 	/* others don't exist */
5191da177e4SLinus Torvalds 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
5201da177e4SLinus Torvalds };
5211da177e4SLinus Torvalds 
5221da177e4SLinus Torvalds static char channel_map_H9632_ss[HDSP_MAX_CHANNELS] = {
5231da177e4SLinus Torvalds 	/* ADAT channels */
5241da177e4SLinus Torvalds 	0, 1, 2, 3, 4, 5, 6, 7,
5251da177e4SLinus Torvalds 	/* SPDIF */
5261da177e4SLinus Torvalds 	8, 9,
5271da177e4SLinus Torvalds 	/* Analog */
5281da177e4SLinus Torvalds 	10, 11,
5291da177e4SLinus Torvalds 	/* AO4S-192 and AI4S-192 extension boards */
5301da177e4SLinus Torvalds 	12, 13, 14, 15,
5311da177e4SLinus Torvalds 	/* others don't exist */
5321da177e4SLinus Torvalds 	-1, -1, -1, -1, -1, -1, -1, -1,
5331da177e4SLinus Torvalds 	-1, -1
5341da177e4SLinus Torvalds };
5351da177e4SLinus Torvalds 
5361da177e4SLinus Torvalds static char channel_map_H9632_ds[HDSP_MAX_CHANNELS] = {
5371da177e4SLinus Torvalds 	/* ADAT */
5381da177e4SLinus Torvalds 	1, 3, 5, 7,
5391da177e4SLinus Torvalds 	/* SPDIF */
5401da177e4SLinus Torvalds 	8, 9,
5411da177e4SLinus Torvalds 	/* Analog */
5421da177e4SLinus Torvalds 	10, 11,
5431da177e4SLinus Torvalds 	/* AO4S-192 and AI4S-192 extension boards */
5441da177e4SLinus Torvalds 	12, 13, 14, 15,
5451da177e4SLinus Torvalds 	/* others don't exist */
5461da177e4SLinus Torvalds 	-1, -1, -1, -1, -1, -1, -1, -1,
5471da177e4SLinus Torvalds 	-1, -1, -1, -1, -1, -1
5481da177e4SLinus Torvalds };
5491da177e4SLinus Torvalds 
5501da177e4SLinus Torvalds static char channel_map_H9632_qs[HDSP_MAX_CHANNELS] = {
5511da177e4SLinus Torvalds 	/* ADAT is disabled in this mode */
5521da177e4SLinus Torvalds 	/* SPDIF */
5531da177e4SLinus Torvalds 	8, 9,
5541da177e4SLinus Torvalds 	/* Analog */
5551da177e4SLinus Torvalds 	10, 11,
5561da177e4SLinus Torvalds 	/* AO4S-192 and AI4S-192 extension boards */
5571da177e4SLinus Torvalds 	12, 13, 14, 15,
5581da177e4SLinus Torvalds 	/* others don't exist */
5591da177e4SLinus Torvalds 	-1, -1, -1, -1, -1, -1, -1, -1,
5601da177e4SLinus Torvalds 	-1, -1, -1, -1, -1, -1, -1, -1,
5611da177e4SLinus Torvalds 	-1, -1
5621da177e4SLinus Torvalds };
5631da177e4SLinus Torvalds 
5641da177e4SLinus Torvalds static int snd_hammerfall_get_buffer(struct pci_dev *pci, struct snd_dma_buffer *dmab, size_t size)
5651da177e4SLinus Torvalds {
5661da177e4SLinus Torvalds 	dmab->dev.type = SNDRV_DMA_TYPE_DEV;
5671da177e4SLinus Torvalds 	dmab->dev.dev = snd_dma_pci_data(pci);
568b6a96915STakashi Iwai 	if (snd_dma_get_reserved_buf(dmab, snd_dma_pci_buf_id(pci))) {
569b6a96915STakashi Iwai 		if (dmab->bytes >= size)
570b6a96915STakashi Iwai 			return 0;
571b6a96915STakashi Iwai 	}
5721da177e4SLinus Torvalds 	if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
5731da177e4SLinus Torvalds 				size, dmab) < 0)
5741da177e4SLinus Torvalds 		return -ENOMEM;
5751da177e4SLinus Torvalds 	return 0;
5761da177e4SLinus Torvalds }
5771da177e4SLinus Torvalds 
5781da177e4SLinus Torvalds static void snd_hammerfall_free_buffer(struct snd_dma_buffer *dmab, struct pci_dev *pci)
5791da177e4SLinus Torvalds {
580b6a96915STakashi Iwai 	if (dmab->area) {
581b6a96915STakashi Iwai 		dmab->dev.dev = NULL; /* make it anonymous */
5821da177e4SLinus Torvalds 		snd_dma_reserve_buf(dmab, snd_dma_pci_buf_id(pci));
5831da177e4SLinus Torvalds 	}
584b6a96915STakashi Iwai }
5851da177e4SLinus Torvalds 
5861da177e4SLinus Torvalds 
587f40b6890STakashi Iwai static struct pci_device_id snd_hdsp_ids[] = {
5881da177e4SLinus Torvalds 	{
5891da177e4SLinus Torvalds 		.vendor = PCI_VENDOR_ID_XILINX,
5901da177e4SLinus Torvalds 		.device = PCI_DEVICE_ID_XILINX_HAMMERFALL_DSP,
5911da177e4SLinus Torvalds 		.subvendor = PCI_ANY_ID,
5921da177e4SLinus Torvalds 		.subdevice = PCI_ANY_ID,
5931da177e4SLinus Torvalds 	}, /* RME Hammerfall-DSP */
5941da177e4SLinus Torvalds 	{ 0, },
5951da177e4SLinus Torvalds };
5961da177e4SLinus Torvalds 
5971da177e4SLinus Torvalds MODULE_DEVICE_TABLE(pci, snd_hdsp_ids);
5981da177e4SLinus Torvalds 
5991da177e4SLinus Torvalds /* prototypes */
60055e957d8STakashi Iwai static int snd_hdsp_create_alsa_devices(struct snd_card *card, struct hdsp *hdsp);
60155e957d8STakashi Iwai static int snd_hdsp_create_pcm(struct snd_card *card, struct hdsp *hdsp);
60255e957d8STakashi Iwai static int snd_hdsp_enable_io (struct hdsp *hdsp);
60355e957d8STakashi Iwai static void snd_hdsp_initialize_midi_flush (struct hdsp *hdsp);
60455e957d8STakashi Iwai static void snd_hdsp_initialize_channels (struct hdsp *hdsp);
60555e957d8STakashi Iwai static int hdsp_fifo_wait(struct hdsp *hdsp, int count, int timeout);
60655e957d8STakashi Iwai static int hdsp_autosync_ref(struct hdsp *hdsp);
60755e957d8STakashi Iwai static int snd_hdsp_set_defaults(struct hdsp *hdsp);
60855e957d8STakashi Iwai static void snd_hdsp_9652_enable_mixer (struct hdsp *hdsp);
6091da177e4SLinus Torvalds 
61055e957d8STakashi Iwai static int hdsp_playback_to_output_key (struct hdsp *hdsp, int in, int out)
6111da177e4SLinus Torvalds {
612a3a68c85SRemy Bruno 	switch (hdsp->io_type) {
613a3a68c85SRemy Bruno 	case Multiface:
614a3a68c85SRemy Bruno 	case Digiface:
6151da177e4SLinus Torvalds 	default:
616192b8e39SAndreas Degert 		if (hdsp->firmware_rev == 0xa)
617a3a68c85SRemy Bruno 			return (64 * out) + (32 + (in));
618192b8e39SAndreas Degert 		else
619192b8e39SAndreas Degert 			return (52 * out) + (26 + (in));
620a3a68c85SRemy Bruno 	case H9632:
621a3a68c85SRemy Bruno 		return (32 * out) + (16 + (in));
622a3a68c85SRemy Bruno 	case H9652:
6231da177e4SLinus Torvalds 		return (52 * out) + (26 + (in));
6241da177e4SLinus Torvalds 	}
6251da177e4SLinus Torvalds }
6261da177e4SLinus Torvalds 
62755e957d8STakashi Iwai static int hdsp_input_to_output_key (struct hdsp *hdsp, int in, int out)
6281da177e4SLinus Torvalds {
629a3a68c85SRemy Bruno 	switch (hdsp->io_type) {
630a3a68c85SRemy Bruno 	case Multiface:
631a3a68c85SRemy Bruno 	case Digiface:
6321da177e4SLinus Torvalds 	default:
633192b8e39SAndreas Degert 		if (hdsp->firmware_rev == 0xa)
634a3a68c85SRemy Bruno 			return (64 * out) + in;
635192b8e39SAndreas Degert 		else
636192b8e39SAndreas Degert 			return (52 * out) + in;
637a3a68c85SRemy Bruno 	case H9632:
638a3a68c85SRemy Bruno 		return (32 * out) + in;
639a3a68c85SRemy Bruno 	case H9652:
6401da177e4SLinus Torvalds 		return (52 * out) + in;
6411da177e4SLinus Torvalds 	}
6421da177e4SLinus Torvalds }
6431da177e4SLinus Torvalds 
64455e957d8STakashi Iwai static void hdsp_write(struct hdsp *hdsp, int reg, int val)
6451da177e4SLinus Torvalds {
6461da177e4SLinus Torvalds 	writel(val, hdsp->iobase + reg);
6471da177e4SLinus Torvalds }
6481da177e4SLinus Torvalds 
64955e957d8STakashi Iwai static unsigned int hdsp_read(struct hdsp *hdsp, int reg)
6501da177e4SLinus Torvalds {
6511da177e4SLinus Torvalds 	return readl (hdsp->iobase + reg);
6521da177e4SLinus Torvalds }
6531da177e4SLinus Torvalds 
65455e957d8STakashi Iwai static int hdsp_check_for_iobox (struct hdsp *hdsp)
6551da177e4SLinus Torvalds {
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds 	if (hdsp->io_type == H9652 || hdsp->io_type == H9632) return 0;
6581da177e4SLinus Torvalds 	if (hdsp_read (hdsp, HDSP_statusRegister) & HDSP_ConfigError) {
6591da177e4SLinus Torvalds 		snd_printk ("Hammerfall-DSP: no Digiface or Multiface connected!\n");
6601da177e4SLinus Torvalds 		hdsp->state &= ~HDSP_FirmwareLoaded;
6611da177e4SLinus Torvalds 		return -EIO;
6621da177e4SLinus Torvalds 	}
6631da177e4SLinus Torvalds 	return 0;
6641da177e4SLinus Torvalds 
6651da177e4SLinus Torvalds }
6661da177e4SLinus Torvalds 
66755e957d8STakashi Iwai static int snd_hdsp_load_firmware_from_cache(struct hdsp *hdsp) {
6681da177e4SLinus Torvalds 
6691da177e4SLinus Torvalds 	int i;
6701da177e4SLinus Torvalds 	unsigned long flags;
6711da177e4SLinus Torvalds 
6721da177e4SLinus Torvalds 	if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
6731da177e4SLinus Torvalds 
6741da177e4SLinus Torvalds 		snd_printk ("Hammerfall-DSP: loading firmware\n");
6751da177e4SLinus Torvalds 
6761da177e4SLinus Torvalds 		hdsp_write (hdsp, HDSP_control2Reg, HDSP_S_PROGRAM);
6771da177e4SLinus Torvalds 		hdsp_write (hdsp, HDSP_fifoData, 0);
6781da177e4SLinus Torvalds 
6791da177e4SLinus Torvalds 		if (hdsp_fifo_wait (hdsp, 0, HDSP_LONG_WAIT)) {
6801da177e4SLinus Torvalds 			snd_printk ("Hammerfall-DSP: timeout waiting for download preparation\n");
6811da177e4SLinus Torvalds 			return -EIO;
6821da177e4SLinus Torvalds 		}
6831da177e4SLinus Torvalds 
6841da177e4SLinus Torvalds 		hdsp_write (hdsp, HDSP_control2Reg, HDSP_S_LOAD);
6851da177e4SLinus Torvalds 
6861da177e4SLinus Torvalds 		for (i = 0; i < 24413; ++i) {
6871da177e4SLinus Torvalds 			hdsp_write(hdsp, HDSP_fifoData, hdsp->firmware_cache[i]);
6881da177e4SLinus Torvalds 			if (hdsp_fifo_wait (hdsp, 127, HDSP_LONG_WAIT)) {
6891da177e4SLinus Torvalds 				snd_printk ("Hammerfall-DSP: timeout during firmware loading\n");
6901da177e4SLinus Torvalds 				return -EIO;
6911da177e4SLinus Torvalds 			}
6921da177e4SLinus Torvalds 		}
6931da177e4SLinus Torvalds 
694ef21ca24SNishanth Aravamudan 		ssleep(3);
6951da177e4SLinus Torvalds 
6961da177e4SLinus Torvalds 		if (hdsp_fifo_wait (hdsp, 0, HDSP_LONG_WAIT)) {
6971da177e4SLinus Torvalds 			snd_printk ("Hammerfall-DSP: timeout at end of firmware loading\n");
6981da177e4SLinus Torvalds 		    	return -EIO;
6991da177e4SLinus Torvalds 		}
7001da177e4SLinus Torvalds 
7011da177e4SLinus Torvalds #ifdef SNDRV_BIG_ENDIAN
7021da177e4SLinus Torvalds 		hdsp->control2_register = HDSP_BIGENDIAN_MODE;
7031da177e4SLinus Torvalds #else
7041da177e4SLinus Torvalds 		hdsp->control2_register = 0;
7051da177e4SLinus Torvalds #endif
7061da177e4SLinus Torvalds 		hdsp_write (hdsp, HDSP_control2Reg, hdsp->control2_register);
7071da177e4SLinus Torvalds 		snd_printk ("Hammerfall-DSP: finished firmware loading\n");
7081da177e4SLinus Torvalds 
7091da177e4SLinus Torvalds 	}
7101da177e4SLinus Torvalds 	if (hdsp->state & HDSP_InitializationComplete) {
711b0b98119STakashi Iwai 		snd_printk(KERN_INFO "Hammerfall-DSP: firmware loaded from cache, restoring defaults\n");
7121da177e4SLinus Torvalds 		spin_lock_irqsave(&hdsp->lock, flags);
7131da177e4SLinus Torvalds 		snd_hdsp_set_defaults(hdsp);
7141da177e4SLinus Torvalds 		spin_unlock_irqrestore(&hdsp->lock, flags);
7151da177e4SLinus Torvalds 	}
7161da177e4SLinus Torvalds 
7171da177e4SLinus Torvalds 	hdsp->state |= HDSP_FirmwareLoaded;
7181da177e4SLinus Torvalds 
7191da177e4SLinus Torvalds 	return 0;
7201da177e4SLinus Torvalds }
7211da177e4SLinus Torvalds 
72255e957d8STakashi Iwai static int hdsp_get_iobox_version (struct hdsp *hdsp)
7231da177e4SLinus Torvalds {
7241da177e4SLinus Torvalds 	if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
7251da177e4SLinus Torvalds 
7261da177e4SLinus Torvalds 		hdsp_write (hdsp, HDSP_control2Reg, HDSP_PROGRAM);
7271da177e4SLinus Torvalds 		hdsp_write (hdsp, HDSP_fifoData, 0);
728b0b98119STakashi Iwai 		if (hdsp_fifo_wait (hdsp, 0, HDSP_SHORT_WAIT) < 0)
7291da177e4SLinus Torvalds 			return -EIO;
7301da177e4SLinus Torvalds 
7311da177e4SLinus Torvalds 		hdsp_write (hdsp, HDSP_control2Reg, HDSP_S_LOAD);
7321da177e4SLinus Torvalds 		hdsp_write (hdsp, HDSP_fifoData, 0);
7331da177e4SLinus Torvalds 
7341da177e4SLinus Torvalds 		if (hdsp_fifo_wait (hdsp, 0, HDSP_SHORT_WAIT)) {
7351da177e4SLinus Torvalds 			hdsp->io_type = Multiface;
7361da177e4SLinus Torvalds 			hdsp_write (hdsp, HDSP_control2Reg, HDSP_VERSION_BIT);
7371da177e4SLinus Torvalds 			hdsp_write (hdsp, HDSP_control2Reg, HDSP_S_LOAD);
7381da177e4SLinus Torvalds 			hdsp_fifo_wait (hdsp, 0, HDSP_SHORT_WAIT);
7391da177e4SLinus Torvalds 		} else {
7401da177e4SLinus Torvalds 			hdsp->io_type = Digiface;
7411da177e4SLinus Torvalds 		}
7421da177e4SLinus Torvalds 	} else {
7431da177e4SLinus Torvalds 		/* firmware was already loaded, get iobox type */
744b0b98119STakashi Iwai 		if (hdsp_read(hdsp, HDSP_status2Register) & HDSP_version1)
7451da177e4SLinus Torvalds 			hdsp->io_type = Multiface;
746b0b98119STakashi Iwai 		else
7471da177e4SLinus Torvalds 			hdsp->io_type = Digiface;
7481da177e4SLinus Torvalds 	}
7491da177e4SLinus Torvalds 	return 0;
7501da177e4SLinus Torvalds }
7511da177e4SLinus Torvalds 
7521da177e4SLinus Torvalds 
753311e70a4STakashi Iwai #ifdef HDSP_FW_LOADER
75492eed66dSTakashi Iwai static int hdsp_request_fw_loader(struct hdsp *hdsp);
755311e70a4STakashi Iwai #endif
756311e70a4STakashi Iwai 
757311e70a4STakashi Iwai static int hdsp_check_for_firmware (struct hdsp *hdsp, int load_on_demand)
7581da177e4SLinus Torvalds {
759311e70a4STakashi Iwai 	if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
760311e70a4STakashi Iwai 		return 0;
7611da177e4SLinus Torvalds 	if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
7621da177e4SLinus Torvalds 		hdsp->state &= ~HDSP_FirmwareLoaded;
763311e70a4STakashi Iwai 		if (! load_on_demand)
764b0b98119STakashi Iwai 			return -EIO;
765311e70a4STakashi Iwai 		snd_printk(KERN_ERR "Hammerfall-DSP: firmware not present.\n");
766b0b98119STakashi Iwai 		/* try to load firmware */
767311e70a4STakashi Iwai 		if (! (hdsp->state & HDSP_FirmwareCached)) {
768311e70a4STakashi Iwai #ifdef HDSP_FW_LOADER
769311e70a4STakashi Iwai 			if (! hdsp_request_fw_loader(hdsp))
770311e70a4STakashi Iwai 				return 0;
771311e70a4STakashi Iwai #endif
772311e70a4STakashi Iwai 			snd_printk(KERN_ERR
773311e70a4STakashi Iwai 				   "Hammerfall-DSP: No firmware loaded nor "
774311e70a4STakashi Iwai 				   "cached, please upload firmware.\n");
7751da177e4SLinus Torvalds 			return -EIO;
7761da177e4SLinus Torvalds 		}
777311e70a4STakashi Iwai 		if (snd_hdsp_load_firmware_from_cache(hdsp) != 0) {
778311e70a4STakashi Iwai 			snd_printk(KERN_ERR
779311e70a4STakashi Iwai 				   "Hammerfall-DSP: Firmware loading from "
780311e70a4STakashi Iwai 				   "cache failed, please upload manually.\n");
781311e70a4STakashi Iwai 			return -EIO;
782311e70a4STakashi Iwai 		}
783311e70a4STakashi Iwai 	}
7841da177e4SLinus Torvalds 	return 0;
7851da177e4SLinus Torvalds }
7861da177e4SLinus Torvalds 
7871da177e4SLinus Torvalds 
78855e957d8STakashi Iwai static int hdsp_fifo_wait(struct hdsp *hdsp, int count, int timeout)
7891da177e4SLinus Torvalds {
7901da177e4SLinus Torvalds 	int i;
7911da177e4SLinus Torvalds 
7921da177e4SLinus Torvalds 	/* the fifoStatus registers reports on how many words
7931da177e4SLinus Torvalds 	   are available in the command FIFO.
7941da177e4SLinus Torvalds 	*/
7951da177e4SLinus Torvalds 
7961da177e4SLinus Torvalds 	for (i = 0; i < timeout; i++) {
7971da177e4SLinus Torvalds 
7981da177e4SLinus Torvalds 		if ((int)(hdsp_read (hdsp, HDSP_fifoStatus) & 0xff) <= count)
7991da177e4SLinus Torvalds 			return 0;
8001da177e4SLinus Torvalds 
8011da177e4SLinus Torvalds 		/* not very friendly, but we only do this during a firmware
8021da177e4SLinus Torvalds 		   load and changing the mixer, so we just put up with it.
8031da177e4SLinus Torvalds 		*/
8041da177e4SLinus Torvalds 
8051da177e4SLinus Torvalds 		udelay (100);
8061da177e4SLinus Torvalds 	}
8071da177e4SLinus Torvalds 
8081da177e4SLinus Torvalds 	snd_printk ("Hammerfall-DSP: wait for FIFO status <= %d failed after %d iterations\n",
8091da177e4SLinus Torvalds 		    count, timeout);
8101da177e4SLinus Torvalds 	return -1;
8111da177e4SLinus Torvalds }
8121da177e4SLinus Torvalds 
81355e957d8STakashi Iwai static int hdsp_read_gain (struct hdsp *hdsp, unsigned int addr)
8141da177e4SLinus Torvalds {
815b0b98119STakashi Iwai 	if (addr >= HDSP_MATRIX_MIXER_SIZE)
8161da177e4SLinus Torvalds 		return 0;
817b0b98119STakashi Iwai 
8181da177e4SLinus Torvalds 	return hdsp->mixer_matrix[addr];
8191da177e4SLinus Torvalds }
8201da177e4SLinus Torvalds 
82155e957d8STakashi Iwai static int hdsp_write_gain(struct hdsp *hdsp, unsigned int addr, unsigned short data)
8221da177e4SLinus Torvalds {
8231da177e4SLinus Torvalds 	unsigned int ad;
8241da177e4SLinus Torvalds 
8251da177e4SLinus Torvalds 	if (addr >= HDSP_MATRIX_MIXER_SIZE)
8261da177e4SLinus Torvalds 		return -1;
8271da177e4SLinus Torvalds 
8281da177e4SLinus Torvalds 	if (hdsp->io_type == H9652 || hdsp->io_type == H9632) {
8291da177e4SLinus Torvalds 
8301da177e4SLinus Torvalds 		/* from martin bjornsen:
8311da177e4SLinus Torvalds 
8321da177e4SLinus Torvalds 		   "You can only write dwords to the
8331da177e4SLinus Torvalds 		   mixer memory which contain two
8341da177e4SLinus Torvalds 		   mixer values in the low and high
8351da177e4SLinus Torvalds 		   word. So if you want to change
8361da177e4SLinus Torvalds 		   value 0 you have to read value 1
8371da177e4SLinus Torvalds 		   from the cache and write both to
8381da177e4SLinus Torvalds 		   the first dword in the mixer
8391da177e4SLinus Torvalds 		   memory."
8401da177e4SLinus Torvalds 		*/
8411da177e4SLinus Torvalds 
842b0b98119STakashi Iwai 		if (hdsp->io_type == H9632 && addr >= 512)
8431da177e4SLinus Torvalds 			return 0;
8441da177e4SLinus Torvalds 
845b0b98119STakashi Iwai 		if (hdsp->io_type == H9652 && addr >= 1352)
8461da177e4SLinus Torvalds 			return 0;
8471da177e4SLinus Torvalds 
8481da177e4SLinus Torvalds 		hdsp->mixer_matrix[addr] = data;
8491da177e4SLinus Torvalds 
8501da177e4SLinus Torvalds 
8511da177e4SLinus Torvalds 		/* `addr' addresses a 16-bit wide address, but
8521da177e4SLinus Torvalds 		   the address space accessed via hdsp_write
8531da177e4SLinus Torvalds 		   uses byte offsets. put another way, addr
8541da177e4SLinus Torvalds 		   varies from 0 to 1351, but to access the
8551da177e4SLinus Torvalds 		   corresponding memory location, we need
8561da177e4SLinus Torvalds 		   to access 0 to 2703 ...
8571da177e4SLinus Torvalds 		*/
8581da177e4SLinus Torvalds 		ad = addr/2;
8591da177e4SLinus Torvalds 
8601da177e4SLinus Torvalds 		hdsp_write (hdsp, 4096 + (ad*4),
8611da177e4SLinus Torvalds 			    (hdsp->mixer_matrix[(addr&0x7fe)+1] << 16) +
8621da177e4SLinus Torvalds 			    hdsp->mixer_matrix[addr&0x7fe]);
8631da177e4SLinus Torvalds 
8641da177e4SLinus Torvalds 		return 0;
8651da177e4SLinus Torvalds 
8661da177e4SLinus Torvalds 	} else {
8671da177e4SLinus Torvalds 
8681da177e4SLinus Torvalds 		ad = (addr << 16) + data;
8691da177e4SLinus Torvalds 
870b0b98119STakashi Iwai 		if (hdsp_fifo_wait(hdsp, 127, HDSP_LONG_WAIT))
8711da177e4SLinus Torvalds 			return -1;
8721da177e4SLinus Torvalds 
8731da177e4SLinus Torvalds 		hdsp_write (hdsp, HDSP_fifoData, ad);
8741da177e4SLinus Torvalds 		hdsp->mixer_matrix[addr] = data;
8751da177e4SLinus Torvalds 
8761da177e4SLinus Torvalds 	}
8771da177e4SLinus Torvalds 
8781da177e4SLinus Torvalds 	return 0;
8791da177e4SLinus Torvalds }
8801da177e4SLinus Torvalds 
88155e957d8STakashi Iwai static int snd_hdsp_use_is_exclusive(struct hdsp *hdsp)
8821da177e4SLinus Torvalds {
8831da177e4SLinus Torvalds 	unsigned long flags;
8841da177e4SLinus Torvalds 	int ret = 1;
8851da177e4SLinus Torvalds 
8861da177e4SLinus Torvalds 	spin_lock_irqsave(&hdsp->lock, flags);
8871da177e4SLinus Torvalds 	if ((hdsp->playback_pid != hdsp->capture_pid) &&
888b0b98119STakashi Iwai 	    (hdsp->playback_pid >= 0) && (hdsp->capture_pid >= 0))
8891da177e4SLinus Torvalds 		ret = 0;
8901da177e4SLinus Torvalds 	spin_unlock_irqrestore(&hdsp->lock, flags);
8911da177e4SLinus Torvalds 	return ret;
8921da177e4SLinus Torvalds }
8931da177e4SLinus Torvalds 
89455e957d8STakashi Iwai static int hdsp_spdif_sample_rate(struct hdsp *hdsp)
8951da177e4SLinus Torvalds {
8961da177e4SLinus Torvalds 	unsigned int status = hdsp_read(hdsp, HDSP_statusRegister);
8971da177e4SLinus Torvalds 	unsigned int rate_bits = (status & HDSP_spdifFrequencyMask);
8981da177e4SLinus Torvalds 
89947ba97f8SRemy Bruno 	/* For the 9632, the mask is different */
90047ba97f8SRemy Bruno 	if (hdsp->io_type == H9632)
90147ba97f8SRemy Bruno 		 rate_bits = (status & HDSP_spdifFrequencyMask_9632);
90247ba97f8SRemy Bruno 
903b0b98119STakashi Iwai 	if (status & HDSP_SPDIFErrorFlag)
9041da177e4SLinus Torvalds 		return 0;
9051da177e4SLinus Torvalds 
9061da177e4SLinus Torvalds 	switch (rate_bits) {
9071da177e4SLinus Torvalds 	case HDSP_spdifFrequency32KHz: return 32000;
9081da177e4SLinus Torvalds 	case HDSP_spdifFrequency44_1KHz: return 44100;
9091da177e4SLinus Torvalds 	case HDSP_spdifFrequency48KHz: return 48000;
9101da177e4SLinus Torvalds 	case HDSP_spdifFrequency64KHz: return 64000;
9111da177e4SLinus Torvalds 	case HDSP_spdifFrequency88_2KHz: return 88200;
9121da177e4SLinus Torvalds 	case HDSP_spdifFrequency96KHz: return 96000;
9131da177e4SLinus Torvalds 	case HDSP_spdifFrequency128KHz:
9141da177e4SLinus Torvalds 		if (hdsp->io_type == H9632) return 128000;
9151da177e4SLinus Torvalds 		break;
9161da177e4SLinus Torvalds 	case HDSP_spdifFrequency176_4KHz:
9171da177e4SLinus Torvalds 		if (hdsp->io_type == H9632) return 176400;
9181da177e4SLinus Torvalds 		break;
9191da177e4SLinus Torvalds 	case HDSP_spdifFrequency192KHz:
9201da177e4SLinus Torvalds 		if (hdsp->io_type == H9632) return 192000;
9211da177e4SLinus Torvalds 		break;
9221da177e4SLinus Torvalds 	default:
9231da177e4SLinus Torvalds 		break;
9241da177e4SLinus Torvalds 	}
9251da177e4SLinus Torvalds 	snd_printk ("Hammerfall-DSP: unknown spdif frequency status; bits = 0x%x, status = 0x%x\n", rate_bits, status);
9261da177e4SLinus Torvalds 	return 0;
9271da177e4SLinus Torvalds }
9281da177e4SLinus Torvalds 
92947ba97f8SRemy Bruno static int hdsp_external_sample_rate(struct hdsp *hdsp)
93047ba97f8SRemy Bruno {
93147ba97f8SRemy Bruno 	unsigned int status2 = hdsp_read(hdsp, HDSP_status2Register);
93247ba97f8SRemy Bruno 	unsigned int rate_bits = status2 & HDSP_systemFrequencyMask;
93347ba97f8SRemy Bruno 
93447ba97f8SRemy Bruno 	/* For the 9632 card, there seems to be no bit for indicating external
93547ba97f8SRemy Bruno 	 * sample rate greater than 96kHz. The card reports the corresponding
93647ba97f8SRemy Bruno 	 * single speed. So the best means seems to get spdif rate when
93747ba97f8SRemy Bruno 	 * autosync reference is spdif */
93847ba97f8SRemy Bruno 	if (hdsp->io_type == H9632 &&
93947ba97f8SRemy Bruno 	    hdsp_autosync_ref(hdsp) == HDSP_AUTOSYNC_FROM_SPDIF)
94047ba97f8SRemy Bruno 		 return hdsp_spdif_sample_rate(hdsp);
94147ba97f8SRemy Bruno 
94247ba97f8SRemy Bruno 	switch (rate_bits) {
94347ba97f8SRemy Bruno 	case HDSP_systemFrequency32:   return 32000;
94447ba97f8SRemy Bruno 	case HDSP_systemFrequency44_1: return 44100;
94547ba97f8SRemy Bruno 	case HDSP_systemFrequency48:   return 48000;
94647ba97f8SRemy Bruno 	case HDSP_systemFrequency64:   return 64000;
94747ba97f8SRemy Bruno 	case HDSP_systemFrequency88_2: return 88200;
94847ba97f8SRemy Bruno 	case HDSP_systemFrequency96:   return 96000;
94947ba97f8SRemy Bruno 	default:
95047ba97f8SRemy Bruno 		return 0;
95147ba97f8SRemy Bruno 	}
95247ba97f8SRemy Bruno }
95347ba97f8SRemy Bruno 
95455e957d8STakashi Iwai static void hdsp_compute_period_size(struct hdsp *hdsp)
9551da177e4SLinus Torvalds {
9561da177e4SLinus Torvalds 	hdsp->period_bytes = 1 << ((hdsp_decode_latency(hdsp->control_register) + 8));
9571da177e4SLinus Torvalds }
9581da177e4SLinus Torvalds 
95955e957d8STakashi Iwai static snd_pcm_uframes_t hdsp_hw_pointer(struct hdsp *hdsp)
9601da177e4SLinus Torvalds {
9611da177e4SLinus Torvalds 	int position;
9621da177e4SLinus Torvalds 
9631da177e4SLinus Torvalds 	position = hdsp_read(hdsp, HDSP_statusRegister);
9641da177e4SLinus Torvalds 
965b0b98119STakashi Iwai 	if (!hdsp->precise_ptr)
9661da177e4SLinus Torvalds 		return (position & HDSP_BufferID) ? (hdsp->period_bytes / 4) : 0;
9671da177e4SLinus Torvalds 
9681da177e4SLinus Torvalds 	position &= HDSP_BufferPositionMask;
9691da177e4SLinus Torvalds 	position /= 4;
9701da177e4SLinus Torvalds 	position &= (hdsp->period_bytes/2) - 1;
9711da177e4SLinus Torvalds 	return position;
9721da177e4SLinus Torvalds }
9731da177e4SLinus Torvalds 
97455e957d8STakashi Iwai static void hdsp_reset_hw_pointer(struct hdsp *hdsp)
9751da177e4SLinus Torvalds {
9761da177e4SLinus Torvalds 	hdsp_write (hdsp, HDSP_resetPointer, 0);
977d7923b2aSRemy Bruno 	if (hdsp->io_type == H9632 && hdsp->firmware_rev >= 152)
978d7923b2aSRemy Bruno 		/* HDSP_resetPointer = HDSP_freqReg, which is strange and
979d7923b2aSRemy Bruno 		 * requires (?) to write again DDS value after a reset pointer
980d7923b2aSRemy Bruno 		 * (at least, it works like this) */
981d7923b2aSRemy Bruno 		hdsp_write (hdsp, HDSP_freqReg, hdsp->dds_value);
9821da177e4SLinus Torvalds }
9831da177e4SLinus Torvalds 
98455e957d8STakashi Iwai static void hdsp_start_audio(struct hdsp *s)
9851da177e4SLinus Torvalds {
9861da177e4SLinus Torvalds 	s->control_register |= (HDSP_AudioInterruptEnable | HDSP_Start);
9871da177e4SLinus Torvalds 	hdsp_write(s, HDSP_controlRegister, s->control_register);
9881da177e4SLinus Torvalds }
9891da177e4SLinus Torvalds 
99055e957d8STakashi Iwai static void hdsp_stop_audio(struct hdsp *s)
9911da177e4SLinus Torvalds {
9921da177e4SLinus Torvalds 	s->control_register &= ~(HDSP_Start | HDSP_AudioInterruptEnable);
9931da177e4SLinus Torvalds 	hdsp_write(s, HDSP_controlRegister, s->control_register);
9941da177e4SLinus Torvalds }
9951da177e4SLinus Torvalds 
99655e957d8STakashi Iwai static void hdsp_silence_playback(struct hdsp *hdsp)
9971da177e4SLinus Torvalds {
9981da177e4SLinus Torvalds 	memset(hdsp->playback_buffer, 0, HDSP_DMA_AREA_BYTES);
9991da177e4SLinus Torvalds }
10001da177e4SLinus Torvalds 
100155e957d8STakashi Iwai static int hdsp_set_interrupt_interval(struct hdsp *s, unsigned int frames)
10021da177e4SLinus Torvalds {
10031da177e4SLinus Torvalds 	int n;
10041da177e4SLinus Torvalds 
10051da177e4SLinus Torvalds 	spin_lock_irq(&s->lock);
10061da177e4SLinus Torvalds 
10071da177e4SLinus Torvalds 	frames >>= 7;
10081da177e4SLinus Torvalds 	n = 0;
10091da177e4SLinus Torvalds 	while (frames) {
10101da177e4SLinus Torvalds 		n++;
10111da177e4SLinus Torvalds 		frames >>= 1;
10121da177e4SLinus Torvalds 	}
10131da177e4SLinus Torvalds 
10141da177e4SLinus Torvalds 	s->control_register &= ~HDSP_LatencyMask;
10151da177e4SLinus Torvalds 	s->control_register |= hdsp_encode_latency(n);
10161da177e4SLinus Torvalds 
10171da177e4SLinus Torvalds 	hdsp_write(s, HDSP_controlRegister, s->control_register);
10181da177e4SLinus Torvalds 
10191da177e4SLinus Torvalds 	hdsp_compute_period_size(s);
10201da177e4SLinus Torvalds 
10211da177e4SLinus Torvalds 	spin_unlock_irq(&s->lock);
10221da177e4SLinus Torvalds 
10231da177e4SLinus Torvalds 	return 0;
10241da177e4SLinus Torvalds }
10251da177e4SLinus Torvalds 
1026d7923b2aSRemy Bruno static void hdsp_set_dds_value(struct hdsp *hdsp, int rate)
1027d7923b2aSRemy Bruno {
1028d7923b2aSRemy Bruno 	u64 n;
1029d7923b2aSRemy Bruno 	u32 r;
1030d7923b2aSRemy Bruno 
1031d7923b2aSRemy Bruno 	if (rate >= 112000)
1032d7923b2aSRemy Bruno 		rate /= 4;
1033d7923b2aSRemy Bruno 	else if (rate >= 56000)
1034d7923b2aSRemy Bruno 		rate /= 2;
1035d7923b2aSRemy Bruno 
1036e4b6088cSJulian Cable 	n = DDS_NUMERATOR;
1037d7923b2aSRemy Bruno 	div64_32(&n, rate, &r);
1038d7923b2aSRemy Bruno 	/* n should be less than 2^32 for being written to FREQ register */
1039da3cec35STakashi Iwai 	snd_BUG_ON(n >> 32);
1040d7923b2aSRemy Bruno 	/* HDSP_freqReg and HDSP_resetPointer are the same, so keep the DDS
1041d7923b2aSRemy Bruno 	   value to write it after a reset */
1042d7923b2aSRemy Bruno 	hdsp->dds_value = n;
1043d7923b2aSRemy Bruno 	hdsp_write(hdsp, HDSP_freqReg, hdsp->dds_value);
1044d7923b2aSRemy Bruno }
1045d7923b2aSRemy Bruno 
104655e957d8STakashi Iwai static int hdsp_set_rate(struct hdsp *hdsp, int rate, int called_internally)
10471da177e4SLinus Torvalds {
10481da177e4SLinus Torvalds 	int reject_if_open = 0;
10491da177e4SLinus Torvalds 	int current_rate;
10501da177e4SLinus Torvalds 	int rate_bits;
10511da177e4SLinus Torvalds 
10521da177e4SLinus Torvalds 	/* ASSUMPTION: hdsp->lock is either held, or
10531da177e4SLinus Torvalds 	   there is no need for it (e.g. during module
10541da177e4SLinus Torvalds 	   initialization).
10551da177e4SLinus Torvalds 	*/
10561da177e4SLinus Torvalds 
10571da177e4SLinus Torvalds 	if (!(hdsp->control_register & HDSP_ClockModeMaster)) {
10581da177e4SLinus Torvalds 		if (called_internally) {
10591da177e4SLinus Torvalds 			/* request from ctl or card initialization */
1060b0b98119STakashi Iwai 			snd_printk(KERN_ERR "Hammerfall-DSP: device is not running as a clock master: cannot set sample rate.\n");
10611da177e4SLinus Torvalds 			return -1;
10621da177e4SLinus Torvalds 		} else {
10631da177e4SLinus Torvalds 			/* hw_param request while in AutoSync mode */
10641da177e4SLinus Torvalds 			int external_freq = hdsp_external_sample_rate(hdsp);
10651da177e4SLinus Torvalds 			int spdif_freq = hdsp_spdif_sample_rate(hdsp);
10661da177e4SLinus Torvalds 
1067b0b98119STakashi Iwai 			if ((spdif_freq == external_freq*2) && (hdsp_autosync_ref(hdsp) >= HDSP_AUTOSYNC_FROM_ADAT1))
1068b0b98119STakashi Iwai 				snd_printk(KERN_INFO "Hammerfall-DSP: Detected ADAT in double speed mode\n");
1069b0b98119STakashi Iwai 			else if (hdsp->io_type == H9632 && (spdif_freq == external_freq*4) && (hdsp_autosync_ref(hdsp) >= HDSP_AUTOSYNC_FROM_ADAT1))
1070b0b98119STakashi Iwai 				snd_printk(KERN_INFO "Hammerfall-DSP: Detected ADAT in quad speed mode\n");
1071b0b98119STakashi Iwai 			else if (rate != external_freq) {
1072b0b98119STakashi Iwai 				snd_printk(KERN_INFO "Hammerfall-DSP: No AutoSync source for requested rate\n");
10731da177e4SLinus Torvalds 				return -1;
10741da177e4SLinus Torvalds 			}
10751da177e4SLinus Torvalds 		}
10761da177e4SLinus Torvalds 	}
10771da177e4SLinus Torvalds 
10781da177e4SLinus Torvalds 	current_rate = hdsp->system_sample_rate;
10791da177e4SLinus Torvalds 
10801da177e4SLinus Torvalds 	/* Changing from a "single speed" to a "double speed" rate is
10811da177e4SLinus Torvalds 	   not allowed if any substreams are open. This is because
10821da177e4SLinus Torvalds 	   such a change causes a shift in the location of
10831da177e4SLinus Torvalds 	   the DMA buffers and a reduction in the number of available
10841da177e4SLinus Torvalds 	   buffers.
10851da177e4SLinus Torvalds 
10861da177e4SLinus Torvalds 	   Note that a similar but essentially insoluble problem
10871da177e4SLinus Torvalds 	   exists for externally-driven rate changes. All we can do
10881da177e4SLinus Torvalds 	   is to flag rate changes in the read/write routines.  */
10891da177e4SLinus Torvalds 
1090b0b98119STakashi Iwai 	if (rate > 96000 && hdsp->io_type != H9632)
10911da177e4SLinus Torvalds 		return -EINVAL;
10921da177e4SLinus Torvalds 
10931da177e4SLinus Torvalds 	switch (rate) {
10941da177e4SLinus Torvalds 	case 32000:
1095b0b98119STakashi Iwai 		if (current_rate > 48000)
10961da177e4SLinus Torvalds 			reject_if_open = 1;
10971da177e4SLinus Torvalds 		rate_bits = HDSP_Frequency32KHz;
10981da177e4SLinus Torvalds 		break;
10991da177e4SLinus Torvalds 	case 44100:
1100b0b98119STakashi Iwai 		if (current_rate > 48000)
11011da177e4SLinus Torvalds 			reject_if_open = 1;
11021da177e4SLinus Torvalds 		rate_bits = HDSP_Frequency44_1KHz;
11031da177e4SLinus Torvalds 		break;
11041da177e4SLinus Torvalds 	case 48000:
1105b0b98119STakashi Iwai 		if (current_rate > 48000)
11061da177e4SLinus Torvalds 			reject_if_open = 1;
11071da177e4SLinus Torvalds 		rate_bits = HDSP_Frequency48KHz;
11081da177e4SLinus Torvalds 		break;
11091da177e4SLinus Torvalds 	case 64000:
1110b0b98119STakashi Iwai 		if (current_rate <= 48000 || current_rate > 96000)
11111da177e4SLinus Torvalds 			reject_if_open = 1;
11121da177e4SLinus Torvalds 		rate_bits = HDSP_Frequency64KHz;
11131da177e4SLinus Torvalds 		break;
11141da177e4SLinus Torvalds 	case 88200:
1115b0b98119STakashi Iwai 		if (current_rate <= 48000 || current_rate > 96000)
11161da177e4SLinus Torvalds 			reject_if_open = 1;
11171da177e4SLinus Torvalds 		rate_bits = HDSP_Frequency88_2KHz;
11181da177e4SLinus Torvalds 		break;
11191da177e4SLinus Torvalds 	case 96000:
1120b0b98119STakashi Iwai 		if (current_rate <= 48000 || current_rate > 96000)
11211da177e4SLinus Torvalds 			reject_if_open = 1;
11221da177e4SLinus Torvalds 		rate_bits = HDSP_Frequency96KHz;
11231da177e4SLinus Torvalds 		break;
11241da177e4SLinus Torvalds 	case 128000:
1125b0b98119STakashi Iwai 		if (current_rate < 128000)
11261da177e4SLinus Torvalds 			reject_if_open = 1;
11271da177e4SLinus Torvalds 		rate_bits = HDSP_Frequency128KHz;
11281da177e4SLinus Torvalds 		break;
11291da177e4SLinus Torvalds 	case 176400:
1130b0b98119STakashi Iwai 		if (current_rate < 128000)
11311da177e4SLinus Torvalds 			reject_if_open = 1;
11321da177e4SLinus Torvalds 		rate_bits = HDSP_Frequency176_4KHz;
11331da177e4SLinus Torvalds 		break;
11341da177e4SLinus Torvalds 	case 192000:
1135b0b98119STakashi Iwai 		if (current_rate < 128000)
11361da177e4SLinus Torvalds 			reject_if_open = 1;
11371da177e4SLinus Torvalds 		rate_bits = HDSP_Frequency192KHz;
11381da177e4SLinus Torvalds 		break;
11391da177e4SLinus Torvalds 	default:
11401da177e4SLinus Torvalds 		return -EINVAL;
11411da177e4SLinus Torvalds 	}
11421da177e4SLinus Torvalds 
11431da177e4SLinus Torvalds 	if (reject_if_open && (hdsp->capture_pid >= 0 || hdsp->playback_pid >= 0)) {
11441da177e4SLinus Torvalds 		snd_printk ("Hammerfall-DSP: cannot change speed mode (capture PID = %d, playback PID = %d)\n",
11451da177e4SLinus Torvalds 			    hdsp->capture_pid,
11461da177e4SLinus Torvalds 			    hdsp->playback_pid);
11471da177e4SLinus Torvalds 		return -EBUSY;
11481da177e4SLinus Torvalds 	}
11491da177e4SLinus Torvalds 
11501da177e4SLinus Torvalds 	hdsp->control_register &= ~HDSP_FrequencyMask;
11511da177e4SLinus Torvalds 	hdsp->control_register |= rate_bits;
11521da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
11531da177e4SLinus Torvalds 
1154d7923b2aSRemy Bruno 	/* For HDSP9632 rev 152, need to set DDS value in FREQ register */
1155d7923b2aSRemy Bruno 	if (hdsp->io_type == H9632 && hdsp->firmware_rev >= 152)
1156d7923b2aSRemy Bruno 		hdsp_set_dds_value(hdsp, rate);
1157d7923b2aSRemy Bruno 
11581da177e4SLinus Torvalds 	if (rate >= 128000) {
11591da177e4SLinus Torvalds 		hdsp->channel_map = channel_map_H9632_qs;
11601da177e4SLinus Torvalds 	} else if (rate > 48000) {
1161b0b98119STakashi Iwai 		if (hdsp->io_type == H9632)
11621da177e4SLinus Torvalds 			hdsp->channel_map = channel_map_H9632_ds;
1163b0b98119STakashi Iwai 		else
11641da177e4SLinus Torvalds 			hdsp->channel_map = channel_map_ds;
11651da177e4SLinus Torvalds 	} else {
11661da177e4SLinus Torvalds 		switch (hdsp->io_type) {
11671da177e4SLinus Torvalds 		case Multiface:
11681da177e4SLinus Torvalds 			hdsp->channel_map = channel_map_mf_ss;
11691da177e4SLinus Torvalds 			break;
11701da177e4SLinus Torvalds 		case Digiface:
11711da177e4SLinus Torvalds 		case H9652:
11721da177e4SLinus Torvalds 			hdsp->channel_map = channel_map_df_ss;
11731da177e4SLinus Torvalds 			break;
11741da177e4SLinus Torvalds 		case H9632:
11751da177e4SLinus Torvalds 			hdsp->channel_map = channel_map_H9632_ss;
11761da177e4SLinus Torvalds 			break;
11771da177e4SLinus Torvalds 		default:
11781da177e4SLinus Torvalds 			/* should never happen */
11791da177e4SLinus Torvalds 			break;
11801da177e4SLinus Torvalds 		}
11811da177e4SLinus Torvalds 	}
11821da177e4SLinus Torvalds 
11831da177e4SLinus Torvalds 	hdsp->system_sample_rate = rate;
11841da177e4SLinus Torvalds 
11851da177e4SLinus Torvalds 	return 0;
11861da177e4SLinus Torvalds }
11871da177e4SLinus Torvalds 
11881da177e4SLinus Torvalds /*----------------------------------------------------------------------------
11891da177e4SLinus Torvalds    MIDI
11901da177e4SLinus Torvalds   ----------------------------------------------------------------------------*/
11911da177e4SLinus Torvalds 
119255e957d8STakashi Iwai static unsigned char snd_hdsp_midi_read_byte (struct hdsp *hdsp, int id)
11931da177e4SLinus Torvalds {
11941da177e4SLinus Torvalds 	/* the hardware already does the relevant bit-mask with 0xff */
1195b0b98119STakashi Iwai 	if (id)
11961da177e4SLinus Torvalds 		return hdsp_read(hdsp, HDSP_midiDataIn1);
1197b0b98119STakashi Iwai 	else
11981da177e4SLinus Torvalds 		return hdsp_read(hdsp, HDSP_midiDataIn0);
11991da177e4SLinus Torvalds }
12001da177e4SLinus Torvalds 
120155e957d8STakashi Iwai static void snd_hdsp_midi_write_byte (struct hdsp *hdsp, int id, int val)
12021da177e4SLinus Torvalds {
12031da177e4SLinus Torvalds 	/* the hardware already does the relevant bit-mask with 0xff */
1204b0b98119STakashi Iwai 	if (id)
12051da177e4SLinus Torvalds 		hdsp_write(hdsp, HDSP_midiDataOut1, val);
1206b0b98119STakashi Iwai 	else
12071da177e4SLinus Torvalds 		hdsp_write(hdsp, HDSP_midiDataOut0, val);
12081da177e4SLinus Torvalds }
12091da177e4SLinus Torvalds 
121055e957d8STakashi Iwai static int snd_hdsp_midi_input_available (struct hdsp *hdsp, int id)
12111da177e4SLinus Torvalds {
1212b0b98119STakashi Iwai 	if (id)
12131da177e4SLinus Torvalds 		return (hdsp_read(hdsp, HDSP_midiStatusIn1) & 0xff);
1214b0b98119STakashi Iwai 	else
12151da177e4SLinus Torvalds 		return (hdsp_read(hdsp, HDSP_midiStatusIn0) & 0xff);
12161da177e4SLinus Torvalds }
12171da177e4SLinus Torvalds 
121855e957d8STakashi Iwai static int snd_hdsp_midi_output_possible (struct hdsp *hdsp, int id)
12191da177e4SLinus Torvalds {
12201da177e4SLinus Torvalds 	int fifo_bytes_used;
12211da177e4SLinus Torvalds 
1222b0b98119STakashi Iwai 	if (id)
12231da177e4SLinus Torvalds 		fifo_bytes_used = hdsp_read(hdsp, HDSP_midiStatusOut1) & 0xff;
1224b0b98119STakashi Iwai 	else
12251da177e4SLinus Torvalds 		fifo_bytes_used = hdsp_read(hdsp, HDSP_midiStatusOut0) & 0xff;
12261da177e4SLinus Torvalds 
1227b0b98119STakashi Iwai 	if (fifo_bytes_used < 128)
12281da177e4SLinus Torvalds 		return  128 - fifo_bytes_used;
1229b0b98119STakashi Iwai 	else
12301da177e4SLinus Torvalds 		return 0;
12311da177e4SLinus Torvalds }
12321da177e4SLinus Torvalds 
123355e957d8STakashi Iwai static void snd_hdsp_flush_midi_input (struct hdsp *hdsp, int id)
12341da177e4SLinus Torvalds {
1235b0b98119STakashi Iwai 	while (snd_hdsp_midi_input_available (hdsp, id))
12361da177e4SLinus Torvalds 		snd_hdsp_midi_read_byte (hdsp, id);
12371da177e4SLinus Torvalds }
12381da177e4SLinus Torvalds 
123955e957d8STakashi Iwai static int snd_hdsp_midi_output_write (struct hdsp_midi *hmidi)
12401da177e4SLinus Torvalds {
12411da177e4SLinus Torvalds 	unsigned long flags;
12421da177e4SLinus Torvalds 	int n_pending;
12431da177e4SLinus Torvalds 	int to_write;
12441da177e4SLinus Torvalds 	int i;
12451da177e4SLinus Torvalds 	unsigned char buf[128];
12461da177e4SLinus Torvalds 
12471da177e4SLinus Torvalds 	/* Output is not interrupt driven */
12481da177e4SLinus Torvalds 
12491da177e4SLinus Torvalds 	spin_lock_irqsave (&hmidi->lock, flags);
12501da177e4SLinus Torvalds 	if (hmidi->output) {
12511da177e4SLinus Torvalds 		if (!snd_rawmidi_transmit_empty (hmidi->output)) {
12521da177e4SLinus Torvalds 			if ((n_pending = snd_hdsp_midi_output_possible (hmidi->hdsp, hmidi->id)) > 0) {
12531da177e4SLinus Torvalds 				if (n_pending > (int)sizeof (buf))
12541da177e4SLinus Torvalds 					n_pending = sizeof (buf);
12551da177e4SLinus Torvalds 
12561da177e4SLinus Torvalds 				if ((to_write = snd_rawmidi_transmit (hmidi->output, buf, n_pending)) > 0) {
12571da177e4SLinus Torvalds 					for (i = 0; i < to_write; ++i)
12581da177e4SLinus Torvalds 						snd_hdsp_midi_write_byte (hmidi->hdsp, hmidi->id, buf[i]);
12591da177e4SLinus Torvalds 				}
12601da177e4SLinus Torvalds 			}
12611da177e4SLinus Torvalds 		}
12621da177e4SLinus Torvalds 	}
12631da177e4SLinus Torvalds 	spin_unlock_irqrestore (&hmidi->lock, flags);
12641da177e4SLinus Torvalds 	return 0;
12651da177e4SLinus Torvalds }
12661da177e4SLinus Torvalds 
126755e957d8STakashi Iwai static int snd_hdsp_midi_input_read (struct hdsp_midi *hmidi)
12681da177e4SLinus Torvalds {
12691da177e4SLinus Torvalds 	unsigned char buf[128]; /* this buffer is designed to match the MIDI input FIFO size */
12701da177e4SLinus Torvalds 	unsigned long flags;
12711da177e4SLinus Torvalds 	int n_pending;
12721da177e4SLinus Torvalds 	int i;
12731da177e4SLinus Torvalds 
12741da177e4SLinus Torvalds 	spin_lock_irqsave (&hmidi->lock, flags);
12751da177e4SLinus Torvalds 	if ((n_pending = snd_hdsp_midi_input_available (hmidi->hdsp, hmidi->id)) > 0) {
12761da177e4SLinus Torvalds 		if (hmidi->input) {
1277b0b98119STakashi Iwai 			if (n_pending > (int)sizeof (buf))
12781da177e4SLinus Torvalds 				n_pending = sizeof (buf);
1279b0b98119STakashi Iwai 			for (i = 0; i < n_pending; ++i)
12801da177e4SLinus Torvalds 				buf[i] = snd_hdsp_midi_read_byte (hmidi->hdsp, hmidi->id);
1281b0b98119STakashi Iwai 			if (n_pending)
12821da177e4SLinus Torvalds 				snd_rawmidi_receive (hmidi->input, buf, n_pending);
12831da177e4SLinus Torvalds 		} else {
12841da177e4SLinus Torvalds 			/* flush the MIDI input FIFO */
1285b0b98119STakashi Iwai 			while (--n_pending)
12861da177e4SLinus Torvalds 				snd_hdsp_midi_read_byte (hmidi->hdsp, hmidi->id);
12871da177e4SLinus Torvalds 		}
12881da177e4SLinus Torvalds 	}
12891da177e4SLinus Torvalds 	hmidi->pending = 0;
1290b0b98119STakashi Iwai 	if (hmidi->id)
12911da177e4SLinus Torvalds 		hmidi->hdsp->control_register |= HDSP_Midi1InterruptEnable;
1292b0b98119STakashi Iwai 	else
12931da177e4SLinus Torvalds 		hmidi->hdsp->control_register |= HDSP_Midi0InterruptEnable;
12941da177e4SLinus Torvalds 	hdsp_write(hmidi->hdsp, HDSP_controlRegister, hmidi->hdsp->control_register);
12951da177e4SLinus Torvalds 	spin_unlock_irqrestore (&hmidi->lock, flags);
12961da177e4SLinus Torvalds 	return snd_hdsp_midi_output_write (hmidi);
12971da177e4SLinus Torvalds }
12981da177e4SLinus Torvalds 
129955e957d8STakashi Iwai static void snd_hdsp_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
13001da177e4SLinus Torvalds {
130155e957d8STakashi Iwai 	struct hdsp *hdsp;
130255e957d8STakashi Iwai 	struct hdsp_midi *hmidi;
13031da177e4SLinus Torvalds 	unsigned long flags;
13041da177e4SLinus Torvalds 	u32 ie;
13051da177e4SLinus Torvalds 
130655e957d8STakashi Iwai 	hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
13071da177e4SLinus Torvalds 	hdsp = hmidi->hdsp;
13081da177e4SLinus Torvalds 	ie = hmidi->id ? HDSP_Midi1InterruptEnable : HDSP_Midi0InterruptEnable;
13091da177e4SLinus Torvalds 	spin_lock_irqsave (&hdsp->lock, flags);
13101da177e4SLinus Torvalds 	if (up) {
13111da177e4SLinus Torvalds 		if (!(hdsp->control_register & ie)) {
13121da177e4SLinus Torvalds 			snd_hdsp_flush_midi_input (hdsp, hmidi->id);
13131da177e4SLinus Torvalds 			hdsp->control_register |= ie;
13141da177e4SLinus Torvalds 		}
13151da177e4SLinus Torvalds 	} else {
13161da177e4SLinus Torvalds 		hdsp->control_register &= ~ie;
13171da177e4SLinus Torvalds 		tasklet_kill(&hdsp->midi_tasklet);
13181da177e4SLinus Torvalds 	}
13191da177e4SLinus Torvalds 
13201da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
13211da177e4SLinus Torvalds 	spin_unlock_irqrestore (&hdsp->lock, flags);
13221da177e4SLinus Torvalds }
13231da177e4SLinus Torvalds 
13241da177e4SLinus Torvalds static void snd_hdsp_midi_output_timer(unsigned long data)
13251da177e4SLinus Torvalds {
132655e957d8STakashi Iwai 	struct hdsp_midi *hmidi = (struct hdsp_midi *) data;
13271da177e4SLinus Torvalds 	unsigned long flags;
13281da177e4SLinus Torvalds 
13291da177e4SLinus Torvalds 	snd_hdsp_midi_output_write(hmidi);
13301da177e4SLinus Torvalds 	spin_lock_irqsave (&hmidi->lock, flags);
13311da177e4SLinus Torvalds 
13321da177e4SLinus Torvalds 	/* this does not bump hmidi->istimer, because the
13331da177e4SLinus Torvalds 	   kernel automatically removed the timer when it
13341da177e4SLinus Torvalds 	   expired, and we are now adding it back, thus
13351da177e4SLinus Torvalds 	   leaving istimer wherever it was set before.
13361da177e4SLinus Torvalds 	*/
13371da177e4SLinus Torvalds 
13381da177e4SLinus Torvalds 	if (hmidi->istimer) {
13391da177e4SLinus Torvalds 		hmidi->timer.expires = 1 + jiffies;
13401da177e4SLinus Torvalds 		add_timer(&hmidi->timer);
13411da177e4SLinus Torvalds 	}
13421da177e4SLinus Torvalds 
13431da177e4SLinus Torvalds 	spin_unlock_irqrestore (&hmidi->lock, flags);
13441da177e4SLinus Torvalds }
13451da177e4SLinus Torvalds 
134655e957d8STakashi Iwai static void snd_hdsp_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
13471da177e4SLinus Torvalds {
134855e957d8STakashi Iwai 	struct hdsp_midi *hmidi;
13491da177e4SLinus Torvalds 	unsigned long flags;
13501da177e4SLinus Torvalds 
135155e957d8STakashi Iwai 	hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
13521da177e4SLinus Torvalds 	spin_lock_irqsave (&hmidi->lock, flags);
13531da177e4SLinus Torvalds 	if (up) {
13541da177e4SLinus Torvalds 		if (!hmidi->istimer) {
13551da177e4SLinus Torvalds 			init_timer(&hmidi->timer);
13561da177e4SLinus Torvalds 			hmidi->timer.function = snd_hdsp_midi_output_timer;
13571da177e4SLinus Torvalds 			hmidi->timer.data = (unsigned long) hmidi;
13581da177e4SLinus Torvalds 			hmidi->timer.expires = 1 + jiffies;
13591da177e4SLinus Torvalds 			add_timer(&hmidi->timer);
13601da177e4SLinus Torvalds 			hmidi->istimer++;
13611da177e4SLinus Torvalds 		}
13621da177e4SLinus Torvalds 	} else {
1363b0b98119STakashi Iwai 		if (hmidi->istimer && --hmidi->istimer <= 0)
13641da177e4SLinus Torvalds 			del_timer (&hmidi->timer);
13651da177e4SLinus Torvalds 	}
13661da177e4SLinus Torvalds 	spin_unlock_irqrestore (&hmidi->lock, flags);
13671da177e4SLinus Torvalds 	if (up)
13681da177e4SLinus Torvalds 		snd_hdsp_midi_output_write(hmidi);
13691da177e4SLinus Torvalds }
13701da177e4SLinus Torvalds 
137155e957d8STakashi Iwai static int snd_hdsp_midi_input_open(struct snd_rawmidi_substream *substream)
13721da177e4SLinus Torvalds {
137355e957d8STakashi Iwai 	struct hdsp_midi *hmidi;
13741da177e4SLinus Torvalds 
137555e957d8STakashi Iwai 	hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
13761da177e4SLinus Torvalds 	spin_lock_irq (&hmidi->lock);
13771da177e4SLinus Torvalds 	snd_hdsp_flush_midi_input (hmidi->hdsp, hmidi->id);
13781da177e4SLinus Torvalds 	hmidi->input = substream;
13791da177e4SLinus Torvalds 	spin_unlock_irq (&hmidi->lock);
13801da177e4SLinus Torvalds 
13811da177e4SLinus Torvalds 	return 0;
13821da177e4SLinus Torvalds }
13831da177e4SLinus Torvalds 
138455e957d8STakashi Iwai static int snd_hdsp_midi_output_open(struct snd_rawmidi_substream *substream)
13851da177e4SLinus Torvalds {
138655e957d8STakashi Iwai 	struct hdsp_midi *hmidi;
13871da177e4SLinus Torvalds 
138855e957d8STakashi Iwai 	hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
13891da177e4SLinus Torvalds 	spin_lock_irq (&hmidi->lock);
13901da177e4SLinus Torvalds 	hmidi->output = substream;
13911da177e4SLinus Torvalds 	spin_unlock_irq (&hmidi->lock);
13921da177e4SLinus Torvalds 
13931da177e4SLinus Torvalds 	return 0;
13941da177e4SLinus Torvalds }
13951da177e4SLinus Torvalds 
139655e957d8STakashi Iwai static int snd_hdsp_midi_input_close(struct snd_rawmidi_substream *substream)
13971da177e4SLinus Torvalds {
139855e957d8STakashi Iwai 	struct hdsp_midi *hmidi;
13991da177e4SLinus Torvalds 
14001da177e4SLinus Torvalds 	snd_hdsp_midi_input_trigger (substream, 0);
14011da177e4SLinus Torvalds 
140255e957d8STakashi Iwai 	hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
14031da177e4SLinus Torvalds 	spin_lock_irq (&hmidi->lock);
14041da177e4SLinus Torvalds 	hmidi->input = NULL;
14051da177e4SLinus Torvalds 	spin_unlock_irq (&hmidi->lock);
14061da177e4SLinus Torvalds 
14071da177e4SLinus Torvalds 	return 0;
14081da177e4SLinus Torvalds }
14091da177e4SLinus Torvalds 
141055e957d8STakashi Iwai static int snd_hdsp_midi_output_close(struct snd_rawmidi_substream *substream)
14111da177e4SLinus Torvalds {
141255e957d8STakashi Iwai 	struct hdsp_midi *hmidi;
14131da177e4SLinus Torvalds 
14141da177e4SLinus Torvalds 	snd_hdsp_midi_output_trigger (substream, 0);
14151da177e4SLinus Torvalds 
141655e957d8STakashi Iwai 	hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
14171da177e4SLinus Torvalds 	spin_lock_irq (&hmidi->lock);
14181da177e4SLinus Torvalds 	hmidi->output = NULL;
14191da177e4SLinus Torvalds 	spin_unlock_irq (&hmidi->lock);
14201da177e4SLinus Torvalds 
14211da177e4SLinus Torvalds 	return 0;
14221da177e4SLinus Torvalds }
14231da177e4SLinus Torvalds 
142455e957d8STakashi Iwai static struct snd_rawmidi_ops snd_hdsp_midi_output =
14251da177e4SLinus Torvalds {
14261da177e4SLinus Torvalds 	.open =		snd_hdsp_midi_output_open,
14271da177e4SLinus Torvalds 	.close =	snd_hdsp_midi_output_close,
14281da177e4SLinus Torvalds 	.trigger =	snd_hdsp_midi_output_trigger,
14291da177e4SLinus Torvalds };
14301da177e4SLinus Torvalds 
143155e957d8STakashi Iwai static struct snd_rawmidi_ops snd_hdsp_midi_input =
14321da177e4SLinus Torvalds {
14331da177e4SLinus Torvalds 	.open =		snd_hdsp_midi_input_open,
14341da177e4SLinus Torvalds 	.close =	snd_hdsp_midi_input_close,
14351da177e4SLinus Torvalds 	.trigger =	snd_hdsp_midi_input_trigger,
14361da177e4SLinus Torvalds };
14371da177e4SLinus Torvalds 
1438f40b6890STakashi Iwai static int snd_hdsp_create_midi (struct snd_card *card, struct hdsp *hdsp, int id)
14391da177e4SLinus Torvalds {
14401da177e4SLinus Torvalds 	char buf[32];
14411da177e4SLinus Torvalds 
14421da177e4SLinus Torvalds 	hdsp->midi[id].id = id;
14431da177e4SLinus Torvalds 	hdsp->midi[id].rmidi = NULL;
14441da177e4SLinus Torvalds 	hdsp->midi[id].input = NULL;
14451da177e4SLinus Torvalds 	hdsp->midi[id].output = NULL;
14461da177e4SLinus Torvalds 	hdsp->midi[id].hdsp = hdsp;
14471da177e4SLinus Torvalds 	hdsp->midi[id].istimer = 0;
14481da177e4SLinus Torvalds 	hdsp->midi[id].pending = 0;
14491da177e4SLinus Torvalds 	spin_lock_init (&hdsp->midi[id].lock);
14501da177e4SLinus Torvalds 
14511da177e4SLinus Torvalds 	sprintf (buf, "%s MIDI %d", card->shortname, id+1);
1452b0b98119STakashi Iwai 	if (snd_rawmidi_new (card, buf, id, 1, 1, &hdsp->midi[id].rmidi) < 0)
14531da177e4SLinus Torvalds 		return -1;
14541da177e4SLinus Torvalds 
1455972d4c50SJaroslav Kysela 	sprintf(hdsp->midi[id].rmidi->name, "HDSP MIDI %d", id+1);
14561da177e4SLinus Torvalds 	hdsp->midi[id].rmidi->private_data = &hdsp->midi[id];
14571da177e4SLinus Torvalds 
14581da177e4SLinus Torvalds 	snd_rawmidi_set_ops (hdsp->midi[id].rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_hdsp_midi_output);
14591da177e4SLinus Torvalds 	snd_rawmidi_set_ops (hdsp->midi[id].rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_hdsp_midi_input);
14601da177e4SLinus Torvalds 
14611da177e4SLinus Torvalds 	hdsp->midi[id].rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT |
14621da177e4SLinus Torvalds 		SNDRV_RAWMIDI_INFO_INPUT |
14631da177e4SLinus Torvalds 		SNDRV_RAWMIDI_INFO_DUPLEX;
14641da177e4SLinus Torvalds 
14651da177e4SLinus Torvalds 	return 0;
14661da177e4SLinus Torvalds }
14671da177e4SLinus Torvalds 
14681da177e4SLinus Torvalds /*-----------------------------------------------------------------------------
14691da177e4SLinus Torvalds   Control Interface
14701da177e4SLinus Torvalds   ----------------------------------------------------------------------------*/
14711da177e4SLinus Torvalds 
147255e957d8STakashi Iwai static u32 snd_hdsp_convert_from_aes(struct snd_aes_iec958 *aes)
14731da177e4SLinus Torvalds {
14741da177e4SLinus Torvalds 	u32 val = 0;
14751da177e4SLinus Torvalds 	val |= (aes->status[0] & IEC958_AES0_PROFESSIONAL) ? HDSP_SPDIFProfessional : 0;
14761da177e4SLinus Torvalds 	val |= (aes->status[0] & IEC958_AES0_NONAUDIO) ? HDSP_SPDIFNonAudio : 0;
14771da177e4SLinus Torvalds 	if (val & HDSP_SPDIFProfessional)
14781da177e4SLinus Torvalds 		val |= (aes->status[0] & IEC958_AES0_PRO_EMPHASIS_5015) ? HDSP_SPDIFEmphasis : 0;
14791da177e4SLinus Torvalds 	else
14801da177e4SLinus Torvalds 		val |= (aes->status[0] & IEC958_AES0_CON_EMPHASIS_5015) ? HDSP_SPDIFEmphasis : 0;
14811da177e4SLinus Torvalds 	return val;
14821da177e4SLinus Torvalds }
14831da177e4SLinus Torvalds 
148455e957d8STakashi Iwai static void snd_hdsp_convert_to_aes(struct snd_aes_iec958 *aes, u32 val)
14851da177e4SLinus Torvalds {
14861da177e4SLinus Torvalds 	aes->status[0] = ((val & HDSP_SPDIFProfessional) ? IEC958_AES0_PROFESSIONAL : 0) |
14871da177e4SLinus Torvalds 			 ((val & HDSP_SPDIFNonAudio) ? IEC958_AES0_NONAUDIO : 0);
14881da177e4SLinus Torvalds 	if (val & HDSP_SPDIFProfessional)
14891da177e4SLinus Torvalds 		aes->status[0] |= (val & HDSP_SPDIFEmphasis) ? IEC958_AES0_PRO_EMPHASIS_5015 : 0;
14901da177e4SLinus Torvalds 	else
14911da177e4SLinus Torvalds 		aes->status[0] |= (val & HDSP_SPDIFEmphasis) ? IEC958_AES0_CON_EMPHASIS_5015 : 0;
14921da177e4SLinus Torvalds }
14931da177e4SLinus Torvalds 
149455e957d8STakashi Iwai static int snd_hdsp_control_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
14951da177e4SLinus Torvalds {
14961da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
14971da177e4SLinus Torvalds 	uinfo->count = 1;
14981da177e4SLinus Torvalds 	return 0;
14991da177e4SLinus Torvalds }
15001da177e4SLinus Torvalds 
150155e957d8STakashi Iwai static int snd_hdsp_control_spdif_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
15021da177e4SLinus Torvalds {
150355e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
15041da177e4SLinus Torvalds 
15051da177e4SLinus Torvalds 	snd_hdsp_convert_to_aes(&ucontrol->value.iec958, hdsp->creg_spdif);
15061da177e4SLinus Torvalds 	return 0;
15071da177e4SLinus Torvalds }
15081da177e4SLinus Torvalds 
150955e957d8STakashi Iwai static int snd_hdsp_control_spdif_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
15101da177e4SLinus Torvalds {
151155e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
15121da177e4SLinus Torvalds 	int change;
15131da177e4SLinus Torvalds 	u32 val;
15141da177e4SLinus Torvalds 
15151da177e4SLinus Torvalds 	val = snd_hdsp_convert_from_aes(&ucontrol->value.iec958);
15161da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
15171da177e4SLinus Torvalds 	change = val != hdsp->creg_spdif;
15181da177e4SLinus Torvalds 	hdsp->creg_spdif = val;
15191da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
15201da177e4SLinus Torvalds 	return change;
15211da177e4SLinus Torvalds }
15221da177e4SLinus Torvalds 
152355e957d8STakashi Iwai static int snd_hdsp_control_spdif_stream_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
15241da177e4SLinus Torvalds {
15251da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
15261da177e4SLinus Torvalds 	uinfo->count = 1;
15271da177e4SLinus Torvalds 	return 0;
15281da177e4SLinus Torvalds }
15291da177e4SLinus Torvalds 
153055e957d8STakashi Iwai static int snd_hdsp_control_spdif_stream_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
15311da177e4SLinus Torvalds {
153255e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
15331da177e4SLinus Torvalds 
15341da177e4SLinus Torvalds 	snd_hdsp_convert_to_aes(&ucontrol->value.iec958, hdsp->creg_spdif_stream);
15351da177e4SLinus Torvalds 	return 0;
15361da177e4SLinus Torvalds }
15371da177e4SLinus Torvalds 
153855e957d8STakashi Iwai static int snd_hdsp_control_spdif_stream_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
15391da177e4SLinus Torvalds {
154055e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
15411da177e4SLinus Torvalds 	int change;
15421da177e4SLinus Torvalds 	u32 val;
15431da177e4SLinus Torvalds 
15441da177e4SLinus Torvalds 	val = snd_hdsp_convert_from_aes(&ucontrol->value.iec958);
15451da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
15461da177e4SLinus Torvalds 	change = val != hdsp->creg_spdif_stream;
15471da177e4SLinus Torvalds 	hdsp->creg_spdif_stream = val;
15481da177e4SLinus Torvalds 	hdsp->control_register &= ~(HDSP_SPDIFProfessional | HDSP_SPDIFNonAudio | HDSP_SPDIFEmphasis);
15491da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register |= val);
15501da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
15511da177e4SLinus Torvalds 	return change;
15521da177e4SLinus Torvalds }
15531da177e4SLinus Torvalds 
155455e957d8STakashi Iwai static int snd_hdsp_control_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
15551da177e4SLinus Torvalds {
15561da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
15571da177e4SLinus Torvalds 	uinfo->count = 1;
15581da177e4SLinus Torvalds 	return 0;
15591da177e4SLinus Torvalds }
15601da177e4SLinus Torvalds 
156155e957d8STakashi Iwai static int snd_hdsp_control_spdif_mask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
15621da177e4SLinus Torvalds {
15631da177e4SLinus Torvalds 	ucontrol->value.iec958.status[0] = kcontrol->private_value;
15641da177e4SLinus Torvalds 	return 0;
15651da177e4SLinus Torvalds }
15661da177e4SLinus Torvalds 
15671da177e4SLinus Torvalds #define HDSP_SPDIF_IN(xname, xindex) \
156867ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
15691da177e4SLinus Torvalds   .name = xname, \
15701da177e4SLinus Torvalds   .index = xindex, \
15711da177e4SLinus Torvalds   .info = snd_hdsp_info_spdif_in, \
15721da177e4SLinus Torvalds   .get = snd_hdsp_get_spdif_in, \
15731da177e4SLinus Torvalds   .put = snd_hdsp_put_spdif_in }
15741da177e4SLinus Torvalds 
157555e957d8STakashi Iwai static unsigned int hdsp_spdif_in(struct hdsp *hdsp)
15761da177e4SLinus Torvalds {
15771da177e4SLinus Torvalds 	return hdsp_decode_spdif_in(hdsp->control_register & HDSP_SPDIFInputMask);
15781da177e4SLinus Torvalds }
15791da177e4SLinus Torvalds 
158055e957d8STakashi Iwai static int hdsp_set_spdif_input(struct hdsp *hdsp, int in)
15811da177e4SLinus Torvalds {
15821da177e4SLinus Torvalds 	hdsp->control_register &= ~HDSP_SPDIFInputMask;
15831da177e4SLinus Torvalds 	hdsp->control_register |= hdsp_encode_spdif_in(in);
15841da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
15851da177e4SLinus Torvalds 	return 0;
15861da177e4SLinus Torvalds }
15871da177e4SLinus Torvalds 
158855e957d8STakashi Iwai static int snd_hdsp_info_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
15891da177e4SLinus Torvalds {
15901da177e4SLinus Torvalds 	static char *texts[4] = {"Optical", "Coaxial", "Internal", "AES"};
159155e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
15921da177e4SLinus Torvalds 
15931da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
15941da177e4SLinus Torvalds 	uinfo->count = 1;
15951da177e4SLinus Torvalds 	uinfo->value.enumerated.items = ((hdsp->io_type == H9632) ? 4 : 3);
15961da177e4SLinus Torvalds 	if (uinfo->value.enumerated.item > ((hdsp->io_type == H9632) ? 3 : 2))
15971da177e4SLinus Torvalds 		uinfo->value.enumerated.item = ((hdsp->io_type == H9632) ? 3 : 2);
15981da177e4SLinus Torvalds 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
15991da177e4SLinus Torvalds 	return 0;
16001da177e4SLinus Torvalds }
16011da177e4SLinus Torvalds 
160255e957d8STakashi Iwai static int snd_hdsp_get_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
16031da177e4SLinus Torvalds {
160455e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
16051da177e4SLinus Torvalds 
16061da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp_spdif_in(hdsp);
16071da177e4SLinus Torvalds 	return 0;
16081da177e4SLinus Torvalds }
16091da177e4SLinus Torvalds 
161055e957d8STakashi Iwai static int snd_hdsp_put_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
16111da177e4SLinus Torvalds {
161255e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
16131da177e4SLinus Torvalds 	int change;
16141da177e4SLinus Torvalds 	unsigned int val;
16151da177e4SLinus Torvalds 
16161da177e4SLinus Torvalds 	if (!snd_hdsp_use_is_exclusive(hdsp))
16171da177e4SLinus Torvalds 		return -EBUSY;
16181da177e4SLinus Torvalds 	val = ucontrol->value.enumerated.item[0] % ((hdsp->io_type == H9632) ? 4 : 3);
16191da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
16201da177e4SLinus Torvalds 	change = val != hdsp_spdif_in(hdsp);
16211da177e4SLinus Torvalds 	if (change)
16221da177e4SLinus Torvalds 		hdsp_set_spdif_input(hdsp, val);
16231da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
16241da177e4SLinus Torvalds 	return change;
16251da177e4SLinus Torvalds }
16261da177e4SLinus Torvalds 
16271da177e4SLinus Torvalds #define HDSP_SPDIF_OUT(xname, xindex) \
162867ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
16291da177e4SLinus Torvalds   .info = snd_hdsp_info_spdif_bits, \
16301da177e4SLinus Torvalds   .get = snd_hdsp_get_spdif_out, .put = snd_hdsp_put_spdif_out }
16311da177e4SLinus Torvalds 
163255e957d8STakashi Iwai static int hdsp_spdif_out(struct hdsp *hdsp)
16331da177e4SLinus Torvalds {
16341da177e4SLinus Torvalds 	return (hdsp->control_register & HDSP_SPDIFOpticalOut) ? 1 : 0;
16351da177e4SLinus Torvalds }
16361da177e4SLinus Torvalds 
163755e957d8STakashi Iwai static int hdsp_set_spdif_output(struct hdsp *hdsp, int out)
16381da177e4SLinus Torvalds {
1639b0b98119STakashi Iwai 	if (out)
16401da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_SPDIFOpticalOut;
1641b0b98119STakashi Iwai 	else
16421da177e4SLinus Torvalds 		hdsp->control_register &= ~HDSP_SPDIFOpticalOut;
16431da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
16441da177e4SLinus Torvalds 	return 0;
16451da177e4SLinus Torvalds }
16461da177e4SLinus Torvalds 
1647a5ce8890STakashi Iwai #define snd_hdsp_info_spdif_bits	snd_ctl_boolean_mono_info
16481da177e4SLinus Torvalds 
164955e957d8STakashi Iwai static int snd_hdsp_get_spdif_out(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
16501da177e4SLinus Torvalds {
165155e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
16521da177e4SLinus Torvalds 
16531da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = hdsp_spdif_out(hdsp);
16541da177e4SLinus Torvalds 	return 0;
16551da177e4SLinus Torvalds }
16561da177e4SLinus Torvalds 
165755e957d8STakashi Iwai static int snd_hdsp_put_spdif_out(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
16581da177e4SLinus Torvalds {
165955e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
16601da177e4SLinus Torvalds 	int change;
16611da177e4SLinus Torvalds 	unsigned int val;
16621da177e4SLinus Torvalds 
16631da177e4SLinus Torvalds 	if (!snd_hdsp_use_is_exclusive(hdsp))
16641da177e4SLinus Torvalds 		return -EBUSY;
16651da177e4SLinus Torvalds 	val = ucontrol->value.integer.value[0] & 1;
16661da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
16671da177e4SLinus Torvalds 	change = (int)val != hdsp_spdif_out(hdsp);
16681da177e4SLinus Torvalds 	hdsp_set_spdif_output(hdsp, val);
16691da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
16701da177e4SLinus Torvalds 	return change;
16711da177e4SLinus Torvalds }
16721da177e4SLinus Torvalds 
16731da177e4SLinus Torvalds #define HDSP_SPDIF_PROFESSIONAL(xname, xindex) \
167467ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
16751da177e4SLinus Torvalds   .info = snd_hdsp_info_spdif_bits, \
16761da177e4SLinus Torvalds   .get = snd_hdsp_get_spdif_professional, .put = snd_hdsp_put_spdif_professional }
16771da177e4SLinus Torvalds 
167855e957d8STakashi Iwai static int hdsp_spdif_professional(struct hdsp *hdsp)
16791da177e4SLinus Torvalds {
16801da177e4SLinus Torvalds 	return (hdsp->control_register & HDSP_SPDIFProfessional) ? 1 : 0;
16811da177e4SLinus Torvalds }
16821da177e4SLinus Torvalds 
168355e957d8STakashi Iwai static int hdsp_set_spdif_professional(struct hdsp *hdsp, int val)
16841da177e4SLinus Torvalds {
1685b0b98119STakashi Iwai 	if (val)
16861da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_SPDIFProfessional;
1687b0b98119STakashi Iwai 	else
16881da177e4SLinus Torvalds 		hdsp->control_register &= ~HDSP_SPDIFProfessional;
16891da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
16901da177e4SLinus Torvalds 	return 0;
16911da177e4SLinus Torvalds }
16921da177e4SLinus Torvalds 
169355e957d8STakashi Iwai static int snd_hdsp_get_spdif_professional(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
16941da177e4SLinus Torvalds {
169555e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
16961da177e4SLinus Torvalds 
16971da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = hdsp_spdif_professional(hdsp);
16981da177e4SLinus Torvalds 	return 0;
16991da177e4SLinus Torvalds }
17001da177e4SLinus Torvalds 
170155e957d8STakashi Iwai static int snd_hdsp_put_spdif_professional(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
17021da177e4SLinus Torvalds {
170355e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
17041da177e4SLinus Torvalds 	int change;
17051da177e4SLinus Torvalds 	unsigned int val;
17061da177e4SLinus Torvalds 
17071da177e4SLinus Torvalds 	if (!snd_hdsp_use_is_exclusive(hdsp))
17081da177e4SLinus Torvalds 		return -EBUSY;
17091da177e4SLinus Torvalds 	val = ucontrol->value.integer.value[0] & 1;
17101da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
17111da177e4SLinus Torvalds 	change = (int)val != hdsp_spdif_professional(hdsp);
17121da177e4SLinus Torvalds 	hdsp_set_spdif_professional(hdsp, val);
17131da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
17141da177e4SLinus Torvalds 	return change;
17151da177e4SLinus Torvalds }
17161da177e4SLinus Torvalds 
17171da177e4SLinus Torvalds #define HDSP_SPDIF_EMPHASIS(xname, xindex) \
171867ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
17191da177e4SLinus Torvalds   .info = snd_hdsp_info_spdif_bits, \
17201da177e4SLinus Torvalds   .get = snd_hdsp_get_spdif_emphasis, .put = snd_hdsp_put_spdif_emphasis }
17211da177e4SLinus Torvalds 
172255e957d8STakashi Iwai static int hdsp_spdif_emphasis(struct hdsp *hdsp)
17231da177e4SLinus Torvalds {
17241da177e4SLinus Torvalds 	return (hdsp->control_register & HDSP_SPDIFEmphasis) ? 1 : 0;
17251da177e4SLinus Torvalds }
17261da177e4SLinus Torvalds 
172755e957d8STakashi Iwai static int hdsp_set_spdif_emphasis(struct hdsp *hdsp, int val)
17281da177e4SLinus Torvalds {
1729b0b98119STakashi Iwai 	if (val)
17301da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_SPDIFEmphasis;
1731b0b98119STakashi Iwai 	else
17321da177e4SLinus Torvalds 		hdsp->control_register &= ~HDSP_SPDIFEmphasis;
17331da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
17341da177e4SLinus Torvalds 	return 0;
17351da177e4SLinus Torvalds }
17361da177e4SLinus Torvalds 
173755e957d8STakashi Iwai static int snd_hdsp_get_spdif_emphasis(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
17381da177e4SLinus Torvalds {
173955e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
17401da177e4SLinus Torvalds 
17411da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = hdsp_spdif_emphasis(hdsp);
17421da177e4SLinus Torvalds 	return 0;
17431da177e4SLinus Torvalds }
17441da177e4SLinus Torvalds 
174555e957d8STakashi Iwai static int snd_hdsp_put_spdif_emphasis(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
17461da177e4SLinus Torvalds {
174755e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
17481da177e4SLinus Torvalds 	int change;
17491da177e4SLinus Torvalds 	unsigned int val;
17501da177e4SLinus Torvalds 
17511da177e4SLinus Torvalds 	if (!snd_hdsp_use_is_exclusive(hdsp))
17521da177e4SLinus Torvalds 		return -EBUSY;
17531da177e4SLinus Torvalds 	val = ucontrol->value.integer.value[0] & 1;
17541da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
17551da177e4SLinus Torvalds 	change = (int)val != hdsp_spdif_emphasis(hdsp);
17561da177e4SLinus Torvalds 	hdsp_set_spdif_emphasis(hdsp, val);
17571da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
17581da177e4SLinus Torvalds 	return change;
17591da177e4SLinus Torvalds }
17601da177e4SLinus Torvalds 
17611da177e4SLinus Torvalds #define HDSP_SPDIF_NON_AUDIO(xname, xindex) \
176267ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
17631da177e4SLinus Torvalds   .info = snd_hdsp_info_spdif_bits, \
17641da177e4SLinus Torvalds   .get = snd_hdsp_get_spdif_nonaudio, .put = snd_hdsp_put_spdif_nonaudio }
17651da177e4SLinus Torvalds 
176655e957d8STakashi Iwai static int hdsp_spdif_nonaudio(struct hdsp *hdsp)
17671da177e4SLinus Torvalds {
17681da177e4SLinus Torvalds 	return (hdsp->control_register & HDSP_SPDIFNonAudio) ? 1 : 0;
17691da177e4SLinus Torvalds }
17701da177e4SLinus Torvalds 
177155e957d8STakashi Iwai static int hdsp_set_spdif_nonaudio(struct hdsp *hdsp, int val)
17721da177e4SLinus Torvalds {
1773b0b98119STakashi Iwai 	if (val)
17741da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_SPDIFNonAudio;
1775b0b98119STakashi Iwai 	else
17761da177e4SLinus Torvalds 		hdsp->control_register &= ~HDSP_SPDIFNonAudio;
17771da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
17781da177e4SLinus Torvalds 	return 0;
17791da177e4SLinus Torvalds }
17801da177e4SLinus Torvalds 
178155e957d8STakashi Iwai static int snd_hdsp_get_spdif_nonaudio(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
17821da177e4SLinus Torvalds {
178355e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
17841da177e4SLinus Torvalds 
17851da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = hdsp_spdif_nonaudio(hdsp);
17861da177e4SLinus Torvalds 	return 0;
17871da177e4SLinus Torvalds }
17881da177e4SLinus Torvalds 
178955e957d8STakashi Iwai static int snd_hdsp_put_spdif_nonaudio(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
17901da177e4SLinus Torvalds {
179155e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
17921da177e4SLinus Torvalds 	int change;
17931da177e4SLinus Torvalds 	unsigned int val;
17941da177e4SLinus Torvalds 
17951da177e4SLinus Torvalds 	if (!snd_hdsp_use_is_exclusive(hdsp))
17961da177e4SLinus Torvalds 		return -EBUSY;
17971da177e4SLinus Torvalds 	val = ucontrol->value.integer.value[0] & 1;
17981da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
17991da177e4SLinus Torvalds 	change = (int)val != hdsp_spdif_nonaudio(hdsp);
18001da177e4SLinus Torvalds 	hdsp_set_spdif_nonaudio(hdsp, val);
18011da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
18021da177e4SLinus Torvalds 	return change;
18031da177e4SLinus Torvalds }
18041da177e4SLinus Torvalds 
18051da177e4SLinus Torvalds #define HDSP_SPDIF_SAMPLE_RATE(xname, xindex) \
180667ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
18071da177e4SLinus Torvalds   .name = xname, \
18081da177e4SLinus Torvalds   .index = xindex, \
18091da177e4SLinus Torvalds   .access = SNDRV_CTL_ELEM_ACCESS_READ, \
18101da177e4SLinus Torvalds   .info = snd_hdsp_info_spdif_sample_rate, \
18111da177e4SLinus Torvalds   .get = snd_hdsp_get_spdif_sample_rate \
18121da177e4SLinus Torvalds }
18131da177e4SLinus Torvalds 
181455e957d8STakashi Iwai static int snd_hdsp_info_spdif_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
18151da177e4SLinus Torvalds {
18161da177e4SLinus Torvalds 	static char *texts[] = {"32000", "44100", "48000", "64000", "88200", "96000", "None", "128000", "176400", "192000"};
181755e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
18181da177e4SLinus Torvalds 
18191da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
18201da177e4SLinus Torvalds 	uinfo->count = 1;
18211da177e4SLinus Torvalds 	uinfo->value.enumerated.items = (hdsp->io_type == H9632) ? 10 : 7;
18221da177e4SLinus Torvalds 	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
18231da177e4SLinus Torvalds 		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
18241da177e4SLinus Torvalds 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
18251da177e4SLinus Torvalds 	return 0;
18261da177e4SLinus Torvalds }
18271da177e4SLinus Torvalds 
182855e957d8STakashi Iwai static int snd_hdsp_get_spdif_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
18291da177e4SLinus Torvalds {
183055e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
18311da177e4SLinus Torvalds 
18321da177e4SLinus Torvalds 	switch (hdsp_spdif_sample_rate(hdsp)) {
18331da177e4SLinus Torvalds 	case 32000:
18341da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 0;
18351da177e4SLinus Torvalds 		break;
18361da177e4SLinus Torvalds 	case 44100:
18371da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 1;
18381da177e4SLinus Torvalds 		break;
18391da177e4SLinus Torvalds 	case 48000:
18401da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 2;
18411da177e4SLinus Torvalds 		break;
18421da177e4SLinus Torvalds 	case 64000:
18431da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 3;
18441da177e4SLinus Torvalds 		break;
18451da177e4SLinus Torvalds 	case 88200:
18461da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 4;
18471da177e4SLinus Torvalds 		break;
18481da177e4SLinus Torvalds 	case 96000:
18491da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 5;
18501da177e4SLinus Torvalds 		break;
18511da177e4SLinus Torvalds 	case 128000:
18521da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 7;
18531da177e4SLinus Torvalds 		break;
18541da177e4SLinus Torvalds 	case 176400:
18551da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 8;
18561da177e4SLinus Torvalds 		break;
18571da177e4SLinus Torvalds 	case 192000:
18581da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 9;
18591da177e4SLinus Torvalds 		break;
18601da177e4SLinus Torvalds 	default:
18611da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 6;
18621da177e4SLinus Torvalds 	}
18631da177e4SLinus Torvalds 	return 0;
18641da177e4SLinus Torvalds }
18651da177e4SLinus Torvalds 
18661da177e4SLinus Torvalds #define HDSP_SYSTEM_SAMPLE_RATE(xname, xindex) \
186767ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
18681da177e4SLinus Torvalds   .name = xname, \
18691da177e4SLinus Torvalds   .index = xindex, \
18701da177e4SLinus Torvalds   .access = SNDRV_CTL_ELEM_ACCESS_READ, \
18711da177e4SLinus Torvalds   .info = snd_hdsp_info_system_sample_rate, \
18721da177e4SLinus Torvalds   .get = snd_hdsp_get_system_sample_rate \
18731da177e4SLinus Torvalds }
18741da177e4SLinus Torvalds 
187555e957d8STakashi Iwai static int snd_hdsp_info_system_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
18761da177e4SLinus Torvalds {
18771da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
18781da177e4SLinus Torvalds 	uinfo->count = 1;
18791da177e4SLinus Torvalds 	return 0;
18801da177e4SLinus Torvalds }
18811da177e4SLinus Torvalds 
188255e957d8STakashi Iwai static int snd_hdsp_get_system_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
18831da177e4SLinus Torvalds {
188455e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
18851da177e4SLinus Torvalds 
18861da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp->system_sample_rate;
18871da177e4SLinus Torvalds 	return 0;
18881da177e4SLinus Torvalds }
18891da177e4SLinus Torvalds 
18901da177e4SLinus Torvalds #define HDSP_AUTOSYNC_SAMPLE_RATE(xname, xindex) \
189167ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
18921da177e4SLinus Torvalds   .name = xname, \
18931da177e4SLinus Torvalds   .index = xindex, \
18941da177e4SLinus Torvalds   .access = SNDRV_CTL_ELEM_ACCESS_READ, \
18951da177e4SLinus Torvalds   .info = snd_hdsp_info_autosync_sample_rate, \
18961da177e4SLinus Torvalds   .get = snd_hdsp_get_autosync_sample_rate \
18971da177e4SLinus Torvalds }
18981da177e4SLinus Torvalds 
189955e957d8STakashi Iwai static int snd_hdsp_info_autosync_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
19001da177e4SLinus Torvalds {
190155e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
19021da177e4SLinus Torvalds 	static char *texts[] = {"32000", "44100", "48000", "64000", "88200", "96000", "None", "128000", "176400", "192000"};
19031da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
19041da177e4SLinus Torvalds 	uinfo->count = 1;
19051da177e4SLinus Torvalds 	uinfo->value.enumerated.items = (hdsp->io_type == H9632) ? 10 : 7 ;
19061da177e4SLinus Torvalds 	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
19071da177e4SLinus Torvalds 		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
19081da177e4SLinus Torvalds 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
19091da177e4SLinus Torvalds 	return 0;
19101da177e4SLinus Torvalds }
19111da177e4SLinus Torvalds 
191255e957d8STakashi Iwai static int snd_hdsp_get_autosync_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
19131da177e4SLinus Torvalds {
191455e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
19151da177e4SLinus Torvalds 
19161da177e4SLinus Torvalds 	switch (hdsp_external_sample_rate(hdsp)) {
19171da177e4SLinus Torvalds 	case 32000:
19181da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 0;
19191da177e4SLinus Torvalds 		break;
19201da177e4SLinus Torvalds 	case 44100:
19211da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 1;
19221da177e4SLinus Torvalds 		break;
19231da177e4SLinus Torvalds 	case 48000:
19241da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 2;
19251da177e4SLinus Torvalds 		break;
19261da177e4SLinus Torvalds 	case 64000:
19271da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 3;
19281da177e4SLinus Torvalds 		break;
19291da177e4SLinus Torvalds 	case 88200:
19301da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 4;
19311da177e4SLinus Torvalds 		break;
19321da177e4SLinus Torvalds 	case 96000:
19331da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 5;
19341da177e4SLinus Torvalds 		break;
19351da177e4SLinus Torvalds 	case 128000:
19361da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 7;
19371da177e4SLinus Torvalds 		break;
19381da177e4SLinus Torvalds 	case 176400:
19391da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 8;
19401da177e4SLinus Torvalds 		break;
19411da177e4SLinus Torvalds 	case 192000:
19421da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 9;
19431da177e4SLinus Torvalds 		break;
19441da177e4SLinus Torvalds 	default:
19451da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 6;
19461da177e4SLinus Torvalds 	}
19471da177e4SLinus Torvalds 	return 0;
19481da177e4SLinus Torvalds }
19491da177e4SLinus Torvalds 
19501da177e4SLinus Torvalds #define HDSP_SYSTEM_CLOCK_MODE(xname, xindex) \
195167ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
19521da177e4SLinus Torvalds   .name = xname, \
19531da177e4SLinus Torvalds   .index = xindex, \
19541da177e4SLinus Torvalds   .access = SNDRV_CTL_ELEM_ACCESS_READ, \
19551da177e4SLinus Torvalds   .info = snd_hdsp_info_system_clock_mode, \
19561da177e4SLinus Torvalds   .get = snd_hdsp_get_system_clock_mode \
19571da177e4SLinus Torvalds }
19581da177e4SLinus Torvalds 
195955e957d8STakashi Iwai static int hdsp_system_clock_mode(struct hdsp *hdsp)
19601da177e4SLinus Torvalds {
1961b0b98119STakashi Iwai 	if (hdsp->control_register & HDSP_ClockModeMaster)
19621da177e4SLinus Torvalds 		return 0;
1963b0b98119STakashi Iwai 	else if (hdsp_external_sample_rate(hdsp) != hdsp->system_sample_rate)
19641da177e4SLinus Torvalds 			return 0;
19651da177e4SLinus Torvalds 	return 1;
19661da177e4SLinus Torvalds }
19671da177e4SLinus Torvalds 
196855e957d8STakashi Iwai static int snd_hdsp_info_system_clock_mode(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
19691da177e4SLinus Torvalds {
19701da177e4SLinus Torvalds 	static char *texts[] = {"Master", "Slave" };
19711da177e4SLinus Torvalds 
19721da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
19731da177e4SLinus Torvalds 	uinfo->count = 1;
19741da177e4SLinus Torvalds 	uinfo->value.enumerated.items = 2;
19751da177e4SLinus Torvalds 	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
19761da177e4SLinus Torvalds 		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
19771da177e4SLinus Torvalds 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
19781da177e4SLinus Torvalds 	return 0;
19791da177e4SLinus Torvalds }
19801da177e4SLinus Torvalds 
198155e957d8STakashi Iwai static int snd_hdsp_get_system_clock_mode(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
19821da177e4SLinus Torvalds {
198355e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
19841da177e4SLinus Torvalds 
19851da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp_system_clock_mode(hdsp);
19861da177e4SLinus Torvalds 	return 0;
19871da177e4SLinus Torvalds }
19881da177e4SLinus Torvalds 
19891da177e4SLinus Torvalds #define HDSP_CLOCK_SOURCE(xname, xindex) \
199067ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
19911da177e4SLinus Torvalds   .name = xname, \
19921da177e4SLinus Torvalds   .index = xindex, \
19931da177e4SLinus Torvalds   .info = snd_hdsp_info_clock_source, \
19941da177e4SLinus Torvalds   .get = snd_hdsp_get_clock_source, \
19951da177e4SLinus Torvalds   .put = snd_hdsp_put_clock_source \
19961da177e4SLinus Torvalds }
19971da177e4SLinus Torvalds 
199855e957d8STakashi Iwai static int hdsp_clock_source(struct hdsp *hdsp)
19991da177e4SLinus Torvalds {
20001da177e4SLinus Torvalds 	if (hdsp->control_register & HDSP_ClockModeMaster) {
20011da177e4SLinus Torvalds 		switch (hdsp->system_sample_rate) {
20021da177e4SLinus Torvalds 		case 32000:
20031da177e4SLinus Torvalds 			return 1;
20041da177e4SLinus Torvalds 		case 44100:
20051da177e4SLinus Torvalds 			return 2;
20061da177e4SLinus Torvalds 		case 48000:
20071da177e4SLinus Torvalds 			return 3;
20081da177e4SLinus Torvalds 		case 64000:
20091da177e4SLinus Torvalds 			return 4;
20101da177e4SLinus Torvalds 		case 88200:
20111da177e4SLinus Torvalds 			return 5;
20121da177e4SLinus Torvalds 		case 96000:
20131da177e4SLinus Torvalds 			return 6;
20141da177e4SLinus Torvalds 		case 128000:
20151da177e4SLinus Torvalds 			return 7;
20161da177e4SLinus Torvalds 		case 176400:
20171da177e4SLinus Torvalds 			return 8;
20181da177e4SLinus Torvalds 		case 192000:
20191da177e4SLinus Torvalds 			return 9;
20201da177e4SLinus Torvalds 		default:
20211da177e4SLinus Torvalds 			return 3;
20221da177e4SLinus Torvalds 		}
20231da177e4SLinus Torvalds 	} else {
20241da177e4SLinus Torvalds 		return 0;
20251da177e4SLinus Torvalds 	}
20261da177e4SLinus Torvalds }
20271da177e4SLinus Torvalds 
202855e957d8STakashi Iwai static int hdsp_set_clock_source(struct hdsp *hdsp, int mode)
20291da177e4SLinus Torvalds {
20301da177e4SLinus Torvalds 	int rate;
20311da177e4SLinus Torvalds 	switch (mode) {
20321da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_AUTOSYNC:
20331da177e4SLinus Torvalds 		if (hdsp_external_sample_rate(hdsp) != 0) {
20341da177e4SLinus Torvalds 		    if (!hdsp_set_rate(hdsp, hdsp_external_sample_rate(hdsp), 1)) {
20351da177e4SLinus Torvalds 			hdsp->control_register &= ~HDSP_ClockModeMaster;
20361da177e4SLinus Torvalds 			hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
20371da177e4SLinus Torvalds 			return 0;
20381da177e4SLinus Torvalds 		    }
20391da177e4SLinus Torvalds 		}
20401da177e4SLinus Torvalds 		return -1;
20411da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_32KHZ:
20421da177e4SLinus Torvalds 		rate = 32000;
20431da177e4SLinus Torvalds 		break;
20441da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_44_1KHZ:
20451da177e4SLinus Torvalds 		rate = 44100;
20461da177e4SLinus Torvalds 		break;
20471da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_48KHZ:
20481da177e4SLinus Torvalds 		rate = 48000;
20491da177e4SLinus Torvalds 		break;
20501da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_64KHZ:
20511da177e4SLinus Torvalds 		rate = 64000;
20521da177e4SLinus Torvalds 		break;
20531da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_88_2KHZ:
20541da177e4SLinus Torvalds 		rate = 88200;
20551da177e4SLinus Torvalds 		break;
20561da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_96KHZ:
20571da177e4SLinus Torvalds 		rate = 96000;
20581da177e4SLinus Torvalds 		break;
20591da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_128KHZ:
20601da177e4SLinus Torvalds 		rate = 128000;
20611da177e4SLinus Torvalds 		break;
20621da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_176_4KHZ:
20631da177e4SLinus Torvalds 		rate = 176400;
20641da177e4SLinus Torvalds 		break;
20651da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_192KHZ:
20661da177e4SLinus Torvalds 		rate = 192000;
20671da177e4SLinus Torvalds 		break;
20681da177e4SLinus Torvalds 	default:
20691da177e4SLinus Torvalds 		rate = 48000;
20701da177e4SLinus Torvalds 	}
20711da177e4SLinus Torvalds 	hdsp->control_register |= HDSP_ClockModeMaster;
20721da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
20731da177e4SLinus Torvalds 	hdsp_set_rate(hdsp, rate, 1);
20741da177e4SLinus Torvalds 	return 0;
20751da177e4SLinus Torvalds }
20761da177e4SLinus Torvalds 
207755e957d8STakashi Iwai static int snd_hdsp_info_clock_source(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
20781da177e4SLinus Torvalds {
20791da177e4SLinus Torvalds 	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" };
208055e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
20811da177e4SLinus Torvalds 
20821da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
20831da177e4SLinus Torvalds 	uinfo->count = 1;
20841da177e4SLinus Torvalds 	if (hdsp->io_type == H9632)
20851da177e4SLinus Torvalds 	    uinfo->value.enumerated.items = 10;
20861da177e4SLinus Torvalds 	else
20871da177e4SLinus Torvalds 	    uinfo->value.enumerated.items = 7;
20881da177e4SLinus Torvalds 	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
20891da177e4SLinus Torvalds 		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
20901da177e4SLinus Torvalds 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
20911da177e4SLinus Torvalds 	return 0;
20921da177e4SLinus Torvalds }
20931da177e4SLinus Torvalds 
209455e957d8STakashi Iwai static int snd_hdsp_get_clock_source(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
20951da177e4SLinus Torvalds {
209655e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
20971da177e4SLinus Torvalds 
20981da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp_clock_source(hdsp);
20991da177e4SLinus Torvalds 	return 0;
21001da177e4SLinus Torvalds }
21011da177e4SLinus Torvalds 
210255e957d8STakashi Iwai static int snd_hdsp_put_clock_source(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
21031da177e4SLinus Torvalds {
210455e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
21051da177e4SLinus Torvalds 	int change;
21061da177e4SLinus Torvalds 	int val;
21071da177e4SLinus Torvalds 
21081da177e4SLinus Torvalds 	if (!snd_hdsp_use_is_exclusive(hdsp))
21091da177e4SLinus Torvalds 		return -EBUSY;
21101da177e4SLinus Torvalds 	val = ucontrol->value.enumerated.item[0];
21111da177e4SLinus Torvalds 	if (val < 0) val = 0;
21121da177e4SLinus Torvalds 	if (hdsp->io_type == H9632) {
2113b0b98119STakashi Iwai 		if (val > 9)
2114b0b98119STakashi Iwai 			val = 9;
21151da177e4SLinus Torvalds 	} else {
2116b0b98119STakashi Iwai 		if (val > 6)
2117b0b98119STakashi Iwai 			val = 6;
21181da177e4SLinus Torvalds 	}
21191da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
2120b0b98119STakashi Iwai 	if (val != hdsp_clock_source(hdsp))
21211da177e4SLinus Torvalds 		change = (hdsp_set_clock_source(hdsp, val) == 0) ? 1 : 0;
2122b0b98119STakashi Iwai 	else
21231da177e4SLinus Torvalds 		change = 0;
21241da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
21251da177e4SLinus Torvalds 	return change;
21261da177e4SLinus Torvalds }
21271da177e4SLinus Torvalds 
2128a5ce8890STakashi Iwai #define snd_hdsp_info_clock_source_lock		snd_ctl_boolean_mono_info
2129e3ea4d89STakashi Iwai 
213055e957d8STakashi Iwai static int snd_hdsp_get_clock_source_lock(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2131e3ea4d89STakashi Iwai {
213255e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2133e3ea4d89STakashi Iwai 
2134e3ea4d89STakashi Iwai 	ucontrol->value.integer.value[0] = hdsp->clock_source_locked;
2135e3ea4d89STakashi Iwai 	return 0;
2136e3ea4d89STakashi Iwai }
2137e3ea4d89STakashi Iwai 
213855e957d8STakashi Iwai static int snd_hdsp_put_clock_source_lock(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2139e3ea4d89STakashi Iwai {
214055e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2141e3ea4d89STakashi Iwai 	int change;
2142e3ea4d89STakashi Iwai 
2143e3ea4d89STakashi Iwai 	change = (int)ucontrol->value.integer.value[0] != hdsp->clock_source_locked;
2144e3ea4d89STakashi Iwai 	if (change)
21454e98d6a7STakashi Iwai 		hdsp->clock_source_locked = !!ucontrol->value.integer.value[0];
2146e3ea4d89STakashi Iwai 	return change;
2147e3ea4d89STakashi Iwai }
2148e3ea4d89STakashi Iwai 
21491da177e4SLinus Torvalds #define HDSP_DA_GAIN(xname, xindex) \
215067ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
21511da177e4SLinus Torvalds   .name = xname, \
21521da177e4SLinus Torvalds   .index = xindex, \
21531da177e4SLinus Torvalds   .info = snd_hdsp_info_da_gain, \
21541da177e4SLinus Torvalds   .get = snd_hdsp_get_da_gain, \
21551da177e4SLinus Torvalds   .put = snd_hdsp_put_da_gain \
21561da177e4SLinus Torvalds }
21571da177e4SLinus Torvalds 
215855e957d8STakashi Iwai static int hdsp_da_gain(struct hdsp *hdsp)
21591da177e4SLinus Torvalds {
21601da177e4SLinus Torvalds 	switch (hdsp->control_register & HDSP_DAGainMask) {
21611da177e4SLinus Torvalds 	case HDSP_DAGainHighGain:
21621da177e4SLinus Torvalds 		return 0;
21631da177e4SLinus Torvalds 	case HDSP_DAGainPlus4dBu:
21641da177e4SLinus Torvalds 		return 1;
21651da177e4SLinus Torvalds 	case HDSP_DAGainMinus10dBV:
21661da177e4SLinus Torvalds 		return 2;
21671da177e4SLinus Torvalds 	default:
21681da177e4SLinus Torvalds 		return 1;
21691da177e4SLinus Torvalds 	}
21701da177e4SLinus Torvalds }
21711da177e4SLinus Torvalds 
217255e957d8STakashi Iwai static int hdsp_set_da_gain(struct hdsp *hdsp, int mode)
21731da177e4SLinus Torvalds {
21741da177e4SLinus Torvalds 	hdsp->control_register &= ~HDSP_DAGainMask;
21751da177e4SLinus Torvalds 	switch (mode) {
21761da177e4SLinus Torvalds 	case 0:
21771da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_DAGainHighGain;
21781da177e4SLinus Torvalds 		break;
21791da177e4SLinus Torvalds 	case 1:
21801da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_DAGainPlus4dBu;
21811da177e4SLinus Torvalds 		break;
21821da177e4SLinus Torvalds 	case 2:
21831da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_DAGainMinus10dBV;
21841da177e4SLinus Torvalds 		break;
21851da177e4SLinus Torvalds 	default:
21861da177e4SLinus Torvalds 		return -1;
21871da177e4SLinus Torvalds 
21881da177e4SLinus Torvalds 	}
21891da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
21901da177e4SLinus Torvalds 	return 0;
21911da177e4SLinus Torvalds }
21921da177e4SLinus Torvalds 
219355e957d8STakashi Iwai static int snd_hdsp_info_da_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
21941da177e4SLinus Torvalds {
21951da177e4SLinus Torvalds 	static char *texts[] = {"Hi Gain", "+4 dBu", "-10 dbV"};
21961da177e4SLinus Torvalds 
21971da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
21981da177e4SLinus Torvalds 	uinfo->count = 1;
21991da177e4SLinus Torvalds 	uinfo->value.enumerated.items = 3;
22001da177e4SLinus Torvalds 	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
22011da177e4SLinus Torvalds 		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
22021da177e4SLinus Torvalds 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
22031da177e4SLinus Torvalds 	return 0;
22041da177e4SLinus Torvalds }
22051da177e4SLinus Torvalds 
220655e957d8STakashi Iwai static int snd_hdsp_get_da_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
22071da177e4SLinus Torvalds {
220855e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
22091da177e4SLinus Torvalds 
22101da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp_da_gain(hdsp);
22111da177e4SLinus Torvalds 	return 0;
22121da177e4SLinus Torvalds }
22131da177e4SLinus Torvalds 
221455e957d8STakashi Iwai static int snd_hdsp_put_da_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
22151da177e4SLinus Torvalds {
221655e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
22171da177e4SLinus Torvalds 	int change;
22181da177e4SLinus Torvalds 	int val;
22191da177e4SLinus Torvalds 
22201da177e4SLinus Torvalds 	if (!snd_hdsp_use_is_exclusive(hdsp))
22211da177e4SLinus Torvalds 		return -EBUSY;
22221da177e4SLinus Torvalds 	val = ucontrol->value.enumerated.item[0];
22231da177e4SLinus Torvalds 	if (val < 0) val = 0;
22241da177e4SLinus Torvalds 	if (val > 2) val = 2;
22251da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
2226b0b98119STakashi Iwai 	if (val != hdsp_da_gain(hdsp))
22271da177e4SLinus Torvalds 		change = (hdsp_set_da_gain(hdsp, val) == 0) ? 1 : 0;
2228b0b98119STakashi Iwai 	else
22291da177e4SLinus Torvalds 		change = 0;
22301da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
22311da177e4SLinus Torvalds 	return change;
22321da177e4SLinus Torvalds }
22331da177e4SLinus Torvalds 
22341da177e4SLinus Torvalds #define HDSP_AD_GAIN(xname, xindex) \
223567ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
22361da177e4SLinus Torvalds   .name = xname, \
22371da177e4SLinus Torvalds   .index = xindex, \
22381da177e4SLinus Torvalds   .info = snd_hdsp_info_ad_gain, \
22391da177e4SLinus Torvalds   .get = snd_hdsp_get_ad_gain, \
22401da177e4SLinus Torvalds   .put = snd_hdsp_put_ad_gain \
22411da177e4SLinus Torvalds }
22421da177e4SLinus Torvalds 
224355e957d8STakashi Iwai static int hdsp_ad_gain(struct hdsp *hdsp)
22441da177e4SLinus Torvalds {
22451da177e4SLinus Torvalds 	switch (hdsp->control_register & HDSP_ADGainMask) {
22461da177e4SLinus Torvalds 	case HDSP_ADGainMinus10dBV:
22471da177e4SLinus Torvalds 		return 0;
22481da177e4SLinus Torvalds 	case HDSP_ADGainPlus4dBu:
22491da177e4SLinus Torvalds 		return 1;
22501da177e4SLinus Torvalds 	case HDSP_ADGainLowGain:
22511da177e4SLinus Torvalds 		return 2;
22521da177e4SLinus Torvalds 	default:
22531da177e4SLinus Torvalds 		return 1;
22541da177e4SLinus Torvalds 	}
22551da177e4SLinus Torvalds }
22561da177e4SLinus Torvalds 
225755e957d8STakashi Iwai static int hdsp_set_ad_gain(struct hdsp *hdsp, int mode)
22581da177e4SLinus Torvalds {
22591da177e4SLinus Torvalds 	hdsp->control_register &= ~HDSP_ADGainMask;
22601da177e4SLinus Torvalds 	switch (mode) {
22611da177e4SLinus Torvalds 	case 0:
22621da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_ADGainMinus10dBV;
22631da177e4SLinus Torvalds 		break;
22641da177e4SLinus Torvalds 	case 1:
22651da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_ADGainPlus4dBu;
22661da177e4SLinus Torvalds 		break;
22671da177e4SLinus Torvalds 	case 2:
22681da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_ADGainLowGain;
22691da177e4SLinus Torvalds 		break;
22701da177e4SLinus Torvalds 	default:
22711da177e4SLinus Torvalds 		return -1;
22721da177e4SLinus Torvalds 
22731da177e4SLinus Torvalds 	}
22741da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
22751da177e4SLinus Torvalds 	return 0;
22761da177e4SLinus Torvalds }
22771da177e4SLinus Torvalds 
227855e957d8STakashi Iwai static int snd_hdsp_info_ad_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
22791da177e4SLinus Torvalds {
22801da177e4SLinus Torvalds 	static char *texts[] = {"-10 dBV", "+4 dBu", "Lo Gain"};
22811da177e4SLinus Torvalds 
22821da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
22831da177e4SLinus Torvalds 	uinfo->count = 1;
22841da177e4SLinus Torvalds 	uinfo->value.enumerated.items = 3;
22851da177e4SLinus Torvalds 	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
22861da177e4SLinus Torvalds 		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
22871da177e4SLinus Torvalds 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
22881da177e4SLinus Torvalds 	return 0;
22891da177e4SLinus Torvalds }
22901da177e4SLinus Torvalds 
229155e957d8STakashi Iwai static int snd_hdsp_get_ad_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
22921da177e4SLinus Torvalds {
229355e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
22941da177e4SLinus Torvalds 
22951da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp_ad_gain(hdsp);
22961da177e4SLinus Torvalds 	return 0;
22971da177e4SLinus Torvalds }
22981da177e4SLinus Torvalds 
229955e957d8STakashi Iwai static int snd_hdsp_put_ad_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
23001da177e4SLinus Torvalds {
230155e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
23021da177e4SLinus Torvalds 	int change;
23031da177e4SLinus Torvalds 	int val;
23041da177e4SLinus Torvalds 
23051da177e4SLinus Torvalds 	if (!snd_hdsp_use_is_exclusive(hdsp))
23061da177e4SLinus Torvalds 		return -EBUSY;
23071da177e4SLinus Torvalds 	val = ucontrol->value.enumerated.item[0];
23081da177e4SLinus Torvalds 	if (val < 0) val = 0;
23091da177e4SLinus Torvalds 	if (val > 2) val = 2;
23101da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
2311b0b98119STakashi Iwai 	if (val != hdsp_ad_gain(hdsp))
23121da177e4SLinus Torvalds 		change = (hdsp_set_ad_gain(hdsp, val) == 0) ? 1 : 0;
2313b0b98119STakashi Iwai 	else
23141da177e4SLinus Torvalds 		change = 0;
23151da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
23161da177e4SLinus Torvalds 	return change;
23171da177e4SLinus Torvalds }
23181da177e4SLinus Torvalds 
23191da177e4SLinus Torvalds #define HDSP_PHONE_GAIN(xname, xindex) \
232067ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
23211da177e4SLinus Torvalds   .name = xname, \
23221da177e4SLinus Torvalds   .index = xindex, \
23231da177e4SLinus Torvalds   .info = snd_hdsp_info_phone_gain, \
23241da177e4SLinus Torvalds   .get = snd_hdsp_get_phone_gain, \
23251da177e4SLinus Torvalds   .put = snd_hdsp_put_phone_gain \
23261da177e4SLinus Torvalds }
23271da177e4SLinus Torvalds 
232855e957d8STakashi Iwai static int hdsp_phone_gain(struct hdsp *hdsp)
23291da177e4SLinus Torvalds {
23301da177e4SLinus Torvalds 	switch (hdsp->control_register & HDSP_PhoneGainMask) {
23311da177e4SLinus Torvalds 	case HDSP_PhoneGain0dB:
23321da177e4SLinus Torvalds 		return 0;
23331da177e4SLinus Torvalds 	case HDSP_PhoneGainMinus6dB:
23341da177e4SLinus Torvalds 		return 1;
23351da177e4SLinus Torvalds 	case HDSP_PhoneGainMinus12dB:
23361da177e4SLinus Torvalds 		return 2;
23371da177e4SLinus Torvalds 	default:
23381da177e4SLinus Torvalds 		return 0;
23391da177e4SLinus Torvalds 	}
23401da177e4SLinus Torvalds }
23411da177e4SLinus Torvalds 
234255e957d8STakashi Iwai static int hdsp_set_phone_gain(struct hdsp *hdsp, int mode)
23431da177e4SLinus Torvalds {
23441da177e4SLinus Torvalds 	hdsp->control_register &= ~HDSP_PhoneGainMask;
23451da177e4SLinus Torvalds 	switch (mode) {
23461da177e4SLinus Torvalds 	case 0:
23471da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_PhoneGain0dB;
23481da177e4SLinus Torvalds 		break;
23491da177e4SLinus Torvalds 	case 1:
23501da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_PhoneGainMinus6dB;
23511da177e4SLinus Torvalds 		break;
23521da177e4SLinus Torvalds 	case 2:
23531da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_PhoneGainMinus12dB;
23541da177e4SLinus Torvalds 		break;
23551da177e4SLinus Torvalds 	default:
23561da177e4SLinus Torvalds 		return -1;
23571da177e4SLinus Torvalds 
23581da177e4SLinus Torvalds 	}
23591da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
23601da177e4SLinus Torvalds 	return 0;
23611da177e4SLinus Torvalds }
23621da177e4SLinus Torvalds 
236355e957d8STakashi Iwai static int snd_hdsp_info_phone_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
23641da177e4SLinus Torvalds {
23651da177e4SLinus Torvalds 	static char *texts[] = {"0 dB", "-6 dB", "-12 dB"};
23661da177e4SLinus Torvalds 
23671da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
23681da177e4SLinus Torvalds 	uinfo->count = 1;
23691da177e4SLinus Torvalds 	uinfo->value.enumerated.items = 3;
23701da177e4SLinus Torvalds 	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
23711da177e4SLinus Torvalds 		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
23721da177e4SLinus Torvalds 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
23731da177e4SLinus Torvalds 	return 0;
23741da177e4SLinus Torvalds }
23751da177e4SLinus Torvalds 
237655e957d8STakashi Iwai static int snd_hdsp_get_phone_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
23771da177e4SLinus Torvalds {
237855e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
23791da177e4SLinus Torvalds 
23801da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp_phone_gain(hdsp);
23811da177e4SLinus Torvalds 	return 0;
23821da177e4SLinus Torvalds }
23831da177e4SLinus Torvalds 
238455e957d8STakashi Iwai static int snd_hdsp_put_phone_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
23851da177e4SLinus Torvalds {
238655e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
23871da177e4SLinus Torvalds 	int change;
23881da177e4SLinus Torvalds 	int val;
23891da177e4SLinus Torvalds 
23901da177e4SLinus Torvalds 	if (!snd_hdsp_use_is_exclusive(hdsp))
23911da177e4SLinus Torvalds 		return -EBUSY;
23921da177e4SLinus Torvalds 	val = ucontrol->value.enumerated.item[0];
23931da177e4SLinus Torvalds 	if (val < 0) val = 0;
23941da177e4SLinus Torvalds 	if (val > 2) val = 2;
23951da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
2396b0b98119STakashi Iwai 	if (val != hdsp_phone_gain(hdsp))
23971da177e4SLinus Torvalds 		change = (hdsp_set_phone_gain(hdsp, val) == 0) ? 1 : 0;
2398b0b98119STakashi Iwai 	else
23991da177e4SLinus Torvalds 		change = 0;
24001da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
24011da177e4SLinus Torvalds 	return change;
24021da177e4SLinus Torvalds }
24031da177e4SLinus Torvalds 
24041da177e4SLinus Torvalds #define HDSP_XLR_BREAKOUT_CABLE(xname, xindex) \
240567ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
24061da177e4SLinus Torvalds   .name = xname, \
24071da177e4SLinus Torvalds   .index = xindex, \
24081da177e4SLinus Torvalds   .info = snd_hdsp_info_xlr_breakout_cable, \
24091da177e4SLinus Torvalds   .get = snd_hdsp_get_xlr_breakout_cable, \
24101da177e4SLinus Torvalds   .put = snd_hdsp_put_xlr_breakout_cable \
24111da177e4SLinus Torvalds }
24121da177e4SLinus Torvalds 
241355e957d8STakashi Iwai static int hdsp_xlr_breakout_cable(struct hdsp *hdsp)
24141da177e4SLinus Torvalds {
2415b0b98119STakashi Iwai 	if (hdsp->control_register & HDSP_XLRBreakoutCable)
24161da177e4SLinus Torvalds 		return 1;
24171da177e4SLinus Torvalds 	return 0;
24181da177e4SLinus Torvalds }
24191da177e4SLinus Torvalds 
242055e957d8STakashi Iwai static int hdsp_set_xlr_breakout_cable(struct hdsp *hdsp, int mode)
24211da177e4SLinus Torvalds {
2422b0b98119STakashi Iwai 	if (mode)
24231da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_XLRBreakoutCable;
2424b0b98119STakashi Iwai 	else
24251da177e4SLinus Torvalds 		hdsp->control_register &= ~HDSP_XLRBreakoutCable;
24261da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
24271da177e4SLinus Torvalds 	return 0;
24281da177e4SLinus Torvalds }
24291da177e4SLinus Torvalds 
2430a5ce8890STakashi Iwai #define snd_hdsp_info_xlr_breakout_cable	snd_ctl_boolean_mono_info
24311da177e4SLinus Torvalds 
243255e957d8STakashi Iwai static int snd_hdsp_get_xlr_breakout_cable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
24331da177e4SLinus Torvalds {
243455e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
24351da177e4SLinus Torvalds 
24361da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp_xlr_breakout_cable(hdsp);
24371da177e4SLinus Torvalds 	return 0;
24381da177e4SLinus Torvalds }
24391da177e4SLinus Torvalds 
244055e957d8STakashi Iwai static int snd_hdsp_put_xlr_breakout_cable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
24411da177e4SLinus Torvalds {
244255e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
24431da177e4SLinus Torvalds 	int change;
24441da177e4SLinus Torvalds 	int val;
24451da177e4SLinus Torvalds 
24461da177e4SLinus Torvalds 	if (!snd_hdsp_use_is_exclusive(hdsp))
24471da177e4SLinus Torvalds 		return -EBUSY;
24481da177e4SLinus Torvalds 	val = ucontrol->value.integer.value[0] & 1;
24491da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
24501da177e4SLinus Torvalds 	change = (int)val != hdsp_xlr_breakout_cable(hdsp);
24511da177e4SLinus Torvalds 	hdsp_set_xlr_breakout_cable(hdsp, val);
24521da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
24531da177e4SLinus Torvalds 	return change;
24541da177e4SLinus Torvalds }
24551da177e4SLinus Torvalds 
24561da177e4SLinus Torvalds /* (De)activates old RME Analog Extension Board
24571da177e4SLinus Torvalds    These are connected to the internal ADAT connector
24581da177e4SLinus Torvalds    Switching this on desactivates external ADAT
24591da177e4SLinus Torvalds */
24601da177e4SLinus Torvalds #define HDSP_AEB(xname, xindex) \
246167ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
24621da177e4SLinus Torvalds   .name = xname, \
24631da177e4SLinus Torvalds   .index = xindex, \
24641da177e4SLinus Torvalds   .info = snd_hdsp_info_aeb, \
24651da177e4SLinus Torvalds   .get = snd_hdsp_get_aeb, \
24661da177e4SLinus Torvalds   .put = snd_hdsp_put_aeb \
24671da177e4SLinus Torvalds }
24681da177e4SLinus Torvalds 
246955e957d8STakashi Iwai static int hdsp_aeb(struct hdsp *hdsp)
24701da177e4SLinus Torvalds {
2471b0b98119STakashi Iwai 	if (hdsp->control_register & HDSP_AnalogExtensionBoard)
24721da177e4SLinus Torvalds 		return 1;
24731da177e4SLinus Torvalds 	return 0;
24741da177e4SLinus Torvalds }
24751da177e4SLinus Torvalds 
247655e957d8STakashi Iwai static int hdsp_set_aeb(struct hdsp *hdsp, int mode)
24771da177e4SLinus Torvalds {
2478b0b98119STakashi Iwai 	if (mode)
24791da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_AnalogExtensionBoard;
2480b0b98119STakashi Iwai 	else
24811da177e4SLinus Torvalds 		hdsp->control_register &= ~HDSP_AnalogExtensionBoard;
24821da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
24831da177e4SLinus Torvalds 	return 0;
24841da177e4SLinus Torvalds }
24851da177e4SLinus Torvalds 
2486a5ce8890STakashi Iwai #define snd_hdsp_info_aeb		snd_ctl_boolean_mono_info
24871da177e4SLinus Torvalds 
248855e957d8STakashi Iwai static int snd_hdsp_get_aeb(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
24891da177e4SLinus Torvalds {
249055e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
24911da177e4SLinus Torvalds 
24921da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp_aeb(hdsp);
24931da177e4SLinus Torvalds 	return 0;
24941da177e4SLinus Torvalds }
24951da177e4SLinus Torvalds 
249655e957d8STakashi Iwai static int snd_hdsp_put_aeb(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
24971da177e4SLinus Torvalds {
249855e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
24991da177e4SLinus Torvalds 	int change;
25001da177e4SLinus Torvalds 	int val;
25011da177e4SLinus Torvalds 
25021da177e4SLinus Torvalds 	if (!snd_hdsp_use_is_exclusive(hdsp))
25031da177e4SLinus Torvalds 		return -EBUSY;
25041da177e4SLinus Torvalds 	val = ucontrol->value.integer.value[0] & 1;
25051da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
25061da177e4SLinus Torvalds 	change = (int)val != hdsp_aeb(hdsp);
25071da177e4SLinus Torvalds 	hdsp_set_aeb(hdsp, val);
25081da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
25091da177e4SLinus Torvalds 	return change;
25101da177e4SLinus Torvalds }
25111da177e4SLinus Torvalds 
25121da177e4SLinus Torvalds #define HDSP_PREF_SYNC_REF(xname, xindex) \
251367ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
25141da177e4SLinus Torvalds   .name = xname, \
25151da177e4SLinus Torvalds   .index = xindex, \
25161da177e4SLinus Torvalds   .info = snd_hdsp_info_pref_sync_ref, \
25171da177e4SLinus Torvalds   .get = snd_hdsp_get_pref_sync_ref, \
25181da177e4SLinus Torvalds   .put = snd_hdsp_put_pref_sync_ref \
25191da177e4SLinus Torvalds }
25201da177e4SLinus Torvalds 
252155e957d8STakashi Iwai static int hdsp_pref_sync_ref(struct hdsp *hdsp)
25221da177e4SLinus Torvalds {
25231da177e4SLinus Torvalds 	/* Notice that this looks at the requested sync source,
25241da177e4SLinus Torvalds 	   not the one actually in use.
25251da177e4SLinus Torvalds 	*/
25261da177e4SLinus Torvalds 
25271da177e4SLinus Torvalds 	switch (hdsp->control_register & HDSP_SyncRefMask) {
25281da177e4SLinus Torvalds 	case HDSP_SyncRef_ADAT1:
25291da177e4SLinus Torvalds 		return HDSP_SYNC_FROM_ADAT1;
25301da177e4SLinus Torvalds 	case HDSP_SyncRef_ADAT2:
25311da177e4SLinus Torvalds 		return HDSP_SYNC_FROM_ADAT2;
25321da177e4SLinus Torvalds 	case HDSP_SyncRef_ADAT3:
25331da177e4SLinus Torvalds 		return HDSP_SYNC_FROM_ADAT3;
25341da177e4SLinus Torvalds 	case HDSP_SyncRef_SPDIF:
25351da177e4SLinus Torvalds 		return HDSP_SYNC_FROM_SPDIF;
25361da177e4SLinus Torvalds 	case HDSP_SyncRef_WORD:
25371da177e4SLinus Torvalds 		return HDSP_SYNC_FROM_WORD;
25381da177e4SLinus Torvalds 	case HDSP_SyncRef_ADAT_SYNC:
25391da177e4SLinus Torvalds 		return HDSP_SYNC_FROM_ADAT_SYNC;
25401da177e4SLinus Torvalds 	default:
25411da177e4SLinus Torvalds 		return HDSP_SYNC_FROM_WORD;
25421da177e4SLinus Torvalds 	}
25431da177e4SLinus Torvalds 	return 0;
25441da177e4SLinus Torvalds }
25451da177e4SLinus Torvalds 
254655e957d8STakashi Iwai static int hdsp_set_pref_sync_ref(struct hdsp *hdsp, int pref)
25471da177e4SLinus Torvalds {
25481da177e4SLinus Torvalds 	hdsp->control_register &= ~HDSP_SyncRefMask;
25491da177e4SLinus Torvalds 	switch (pref) {
25501da177e4SLinus Torvalds 	case HDSP_SYNC_FROM_ADAT1:
25511da177e4SLinus Torvalds 		hdsp->control_register &= ~HDSP_SyncRefMask; /* clear SyncRef bits */
25521da177e4SLinus Torvalds 		break;
25531da177e4SLinus Torvalds 	case HDSP_SYNC_FROM_ADAT2:
25541da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_SyncRef_ADAT2;
25551da177e4SLinus Torvalds 		break;
25561da177e4SLinus Torvalds 	case HDSP_SYNC_FROM_ADAT3:
25571da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_SyncRef_ADAT3;
25581da177e4SLinus Torvalds 		break;
25591da177e4SLinus Torvalds 	case HDSP_SYNC_FROM_SPDIF:
25601da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_SyncRef_SPDIF;
25611da177e4SLinus Torvalds 		break;
25621da177e4SLinus Torvalds 	case HDSP_SYNC_FROM_WORD:
25631da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_SyncRef_WORD;
25641da177e4SLinus Torvalds 		break;
25651da177e4SLinus Torvalds 	case HDSP_SYNC_FROM_ADAT_SYNC:
25661da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_SyncRef_ADAT_SYNC;
25671da177e4SLinus Torvalds 		break;
25681da177e4SLinus Torvalds 	default:
25691da177e4SLinus Torvalds 		return -1;
25701da177e4SLinus Torvalds 	}
25711da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
25721da177e4SLinus Torvalds 	return 0;
25731da177e4SLinus Torvalds }
25741da177e4SLinus Torvalds 
257555e957d8STakashi Iwai static int snd_hdsp_info_pref_sync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
25761da177e4SLinus Torvalds {
25771da177e4SLinus Torvalds 	static char *texts[] = {"Word", "IEC958", "ADAT1", "ADAT Sync", "ADAT2", "ADAT3" };
257855e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
25791da177e4SLinus Torvalds 
25801da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
25811da177e4SLinus Torvalds 	uinfo->count = 1;
25821da177e4SLinus Torvalds 
25831da177e4SLinus Torvalds 	switch (hdsp->io_type) {
25841da177e4SLinus Torvalds 	case Digiface:
25851da177e4SLinus Torvalds 	case H9652:
25861da177e4SLinus Torvalds 		uinfo->value.enumerated.items = 6;
25871da177e4SLinus Torvalds 		break;
25881da177e4SLinus Torvalds 	case Multiface:
25891da177e4SLinus Torvalds 		uinfo->value.enumerated.items = 4;
25901da177e4SLinus Torvalds 		break;
25911da177e4SLinus Torvalds 	case H9632:
25921da177e4SLinus Torvalds 		uinfo->value.enumerated.items = 3;
25931da177e4SLinus Torvalds 		break;
25941da177e4SLinus Torvalds 	default:
25951da177e4SLinus Torvalds 		uinfo->value.enumerated.items = 0;
25961da177e4SLinus Torvalds 		break;
25971da177e4SLinus Torvalds 	}
25981da177e4SLinus Torvalds 
25991da177e4SLinus Torvalds 	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
26001da177e4SLinus Torvalds 		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
26011da177e4SLinus Torvalds 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
26021da177e4SLinus Torvalds 	return 0;
26031da177e4SLinus Torvalds }
26041da177e4SLinus Torvalds 
260555e957d8STakashi Iwai static int snd_hdsp_get_pref_sync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
26061da177e4SLinus Torvalds {
260755e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
26081da177e4SLinus Torvalds 
26091da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp_pref_sync_ref(hdsp);
26101da177e4SLinus Torvalds 	return 0;
26111da177e4SLinus Torvalds }
26121da177e4SLinus Torvalds 
261355e957d8STakashi Iwai static int snd_hdsp_put_pref_sync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
26141da177e4SLinus Torvalds {
261555e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
26161da177e4SLinus Torvalds 	int change, max;
26171da177e4SLinus Torvalds 	unsigned int val;
26181da177e4SLinus Torvalds 
26191da177e4SLinus Torvalds 	if (!snd_hdsp_use_is_exclusive(hdsp))
26201da177e4SLinus Torvalds 		return -EBUSY;
26211da177e4SLinus Torvalds 
26221da177e4SLinus Torvalds 	switch (hdsp->io_type) {
26231da177e4SLinus Torvalds 	case Digiface:
26241da177e4SLinus Torvalds 	case H9652:
26251da177e4SLinus Torvalds 		max = 6;
26261da177e4SLinus Torvalds 		break;
26271da177e4SLinus Torvalds 	case Multiface:
26281da177e4SLinus Torvalds 		max = 4;
26291da177e4SLinus Torvalds 		break;
26301da177e4SLinus Torvalds 	case H9632:
26311da177e4SLinus Torvalds 		max = 3;
26321da177e4SLinus Torvalds 		break;
26331da177e4SLinus Torvalds 	default:
26341da177e4SLinus Torvalds 		return -EIO;
26351da177e4SLinus Torvalds 	}
26361da177e4SLinus Torvalds 
26371da177e4SLinus Torvalds 	val = ucontrol->value.enumerated.item[0] % max;
26381da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
26391da177e4SLinus Torvalds 	change = (int)val != hdsp_pref_sync_ref(hdsp);
26401da177e4SLinus Torvalds 	hdsp_set_pref_sync_ref(hdsp, val);
26411da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
26421da177e4SLinus Torvalds 	return change;
26431da177e4SLinus Torvalds }
26441da177e4SLinus Torvalds 
26451da177e4SLinus Torvalds #define HDSP_AUTOSYNC_REF(xname, xindex) \
264667ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
26471da177e4SLinus Torvalds   .name = xname, \
26481da177e4SLinus Torvalds   .index = xindex, \
26491da177e4SLinus Torvalds   .access = SNDRV_CTL_ELEM_ACCESS_READ, \
26501da177e4SLinus Torvalds   .info = snd_hdsp_info_autosync_ref, \
26511da177e4SLinus Torvalds   .get = snd_hdsp_get_autosync_ref, \
26521da177e4SLinus Torvalds }
26531da177e4SLinus Torvalds 
265455e957d8STakashi Iwai static int hdsp_autosync_ref(struct hdsp *hdsp)
26551da177e4SLinus Torvalds {
26561da177e4SLinus Torvalds 	/* This looks at the autosync selected sync reference */
26571da177e4SLinus Torvalds 	unsigned int status2 = hdsp_read(hdsp, HDSP_status2Register);
26581da177e4SLinus Torvalds 
26591da177e4SLinus Torvalds 	switch (status2 & HDSP_SelSyncRefMask) {
26601da177e4SLinus Torvalds 	case HDSP_SelSyncRef_WORD:
26611da177e4SLinus Torvalds 		return HDSP_AUTOSYNC_FROM_WORD;
26621da177e4SLinus Torvalds 	case HDSP_SelSyncRef_ADAT_SYNC:
26631da177e4SLinus Torvalds 		return HDSP_AUTOSYNC_FROM_ADAT_SYNC;
26641da177e4SLinus Torvalds 	case HDSP_SelSyncRef_SPDIF:
26651da177e4SLinus Torvalds 		return HDSP_AUTOSYNC_FROM_SPDIF;
26661da177e4SLinus Torvalds 	case HDSP_SelSyncRefMask:
26671da177e4SLinus Torvalds 		return HDSP_AUTOSYNC_FROM_NONE;
26681da177e4SLinus Torvalds 	case HDSP_SelSyncRef_ADAT1:
26691da177e4SLinus Torvalds 		return HDSP_AUTOSYNC_FROM_ADAT1;
26701da177e4SLinus Torvalds 	case HDSP_SelSyncRef_ADAT2:
26711da177e4SLinus Torvalds 		return HDSP_AUTOSYNC_FROM_ADAT2;
26721da177e4SLinus Torvalds 	case HDSP_SelSyncRef_ADAT3:
26731da177e4SLinus Torvalds 		return HDSP_AUTOSYNC_FROM_ADAT3;
26741da177e4SLinus Torvalds 	default:
26751da177e4SLinus Torvalds 		return HDSP_AUTOSYNC_FROM_WORD;
26761da177e4SLinus Torvalds 	}
26771da177e4SLinus Torvalds 	return 0;
26781da177e4SLinus Torvalds }
26791da177e4SLinus Torvalds 
268055e957d8STakashi Iwai static int snd_hdsp_info_autosync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
26811da177e4SLinus Torvalds {
26821da177e4SLinus Torvalds 	static char *texts[] = {"Word", "ADAT Sync", "IEC958", "None", "ADAT1", "ADAT2", "ADAT3" };
26831da177e4SLinus Torvalds 
26841da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
26851da177e4SLinus Torvalds 	uinfo->count = 1;
26861da177e4SLinus Torvalds 	uinfo->value.enumerated.items = 7;
26871da177e4SLinus Torvalds 	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
26881da177e4SLinus Torvalds 		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
26891da177e4SLinus Torvalds 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
26901da177e4SLinus Torvalds 	return 0;
26911da177e4SLinus Torvalds }
26921da177e4SLinus Torvalds 
269355e957d8STakashi Iwai static int snd_hdsp_get_autosync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
26941da177e4SLinus Torvalds {
269555e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
26961da177e4SLinus Torvalds 
26971da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp_autosync_ref(hdsp);
26981da177e4SLinus Torvalds 	return 0;
26991da177e4SLinus Torvalds }
27001da177e4SLinus Torvalds 
27011da177e4SLinus Torvalds #define HDSP_LINE_OUT(xname, xindex) \
270267ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
27031da177e4SLinus Torvalds   .name = xname, \
27041da177e4SLinus Torvalds   .index = xindex, \
27051da177e4SLinus Torvalds   .info = snd_hdsp_info_line_out, \
27061da177e4SLinus Torvalds   .get = snd_hdsp_get_line_out, \
27071da177e4SLinus Torvalds   .put = snd_hdsp_put_line_out \
27081da177e4SLinus Torvalds }
27091da177e4SLinus Torvalds 
271055e957d8STakashi Iwai static int hdsp_line_out(struct hdsp *hdsp)
27111da177e4SLinus Torvalds {
27121da177e4SLinus Torvalds 	return (hdsp->control_register & HDSP_LineOut) ? 1 : 0;
27131da177e4SLinus Torvalds }
27141da177e4SLinus Torvalds 
271555e957d8STakashi Iwai static int hdsp_set_line_output(struct hdsp *hdsp, int out)
27161da177e4SLinus Torvalds {
2717b0b98119STakashi Iwai 	if (out)
27181da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_LineOut;
2719b0b98119STakashi Iwai 	else
27201da177e4SLinus Torvalds 		hdsp->control_register &= ~HDSP_LineOut;
27211da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
27221da177e4SLinus Torvalds 	return 0;
27231da177e4SLinus Torvalds }
27241da177e4SLinus Torvalds 
2725a5ce8890STakashi Iwai #define snd_hdsp_info_line_out		snd_ctl_boolean_mono_info
27261da177e4SLinus Torvalds 
272755e957d8STakashi Iwai static int snd_hdsp_get_line_out(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
27281da177e4SLinus Torvalds {
272955e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
27301da177e4SLinus Torvalds 
27311da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
27321da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = hdsp_line_out(hdsp);
27331da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
27341da177e4SLinus Torvalds 	return 0;
27351da177e4SLinus Torvalds }
27361da177e4SLinus Torvalds 
273755e957d8STakashi Iwai static int snd_hdsp_put_line_out(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
27381da177e4SLinus Torvalds {
273955e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
27401da177e4SLinus Torvalds 	int change;
27411da177e4SLinus Torvalds 	unsigned int val;
27421da177e4SLinus Torvalds 
27431da177e4SLinus Torvalds 	if (!snd_hdsp_use_is_exclusive(hdsp))
27441da177e4SLinus Torvalds 		return -EBUSY;
27451da177e4SLinus Torvalds 	val = ucontrol->value.integer.value[0] & 1;
27461da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
27471da177e4SLinus Torvalds 	change = (int)val != hdsp_line_out(hdsp);
27481da177e4SLinus Torvalds 	hdsp_set_line_output(hdsp, val);
27491da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
27501da177e4SLinus Torvalds 	return change;
27511da177e4SLinus Torvalds }
27521da177e4SLinus Torvalds 
27531da177e4SLinus Torvalds #define HDSP_PRECISE_POINTER(xname, xindex) \
275467ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_CARD, \
27551da177e4SLinus Torvalds   .name = xname, \
27561da177e4SLinus Torvalds   .index = xindex, \
27571da177e4SLinus Torvalds   .info = snd_hdsp_info_precise_pointer, \
27581da177e4SLinus Torvalds   .get = snd_hdsp_get_precise_pointer, \
27591da177e4SLinus Torvalds   .put = snd_hdsp_put_precise_pointer \
27601da177e4SLinus Torvalds }
27611da177e4SLinus Torvalds 
276255e957d8STakashi Iwai static int hdsp_set_precise_pointer(struct hdsp *hdsp, int precise)
27631da177e4SLinus Torvalds {
2764b0b98119STakashi Iwai 	if (precise)
27651da177e4SLinus Torvalds 		hdsp->precise_ptr = 1;
2766b0b98119STakashi Iwai 	else
27671da177e4SLinus Torvalds 		hdsp->precise_ptr = 0;
27681da177e4SLinus Torvalds 	return 0;
27691da177e4SLinus Torvalds }
27701da177e4SLinus Torvalds 
2771a5ce8890STakashi Iwai #define snd_hdsp_info_precise_pointer		snd_ctl_boolean_mono_info
27721da177e4SLinus Torvalds 
277355e957d8STakashi Iwai static int snd_hdsp_get_precise_pointer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
27741da177e4SLinus Torvalds {
277555e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
27761da177e4SLinus Torvalds 
27771da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
27781da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = hdsp->precise_ptr;
27791da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
27801da177e4SLinus Torvalds 	return 0;
27811da177e4SLinus Torvalds }
27821da177e4SLinus Torvalds 
278355e957d8STakashi Iwai static int snd_hdsp_put_precise_pointer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
27841da177e4SLinus Torvalds {
278555e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
27861da177e4SLinus Torvalds 	int change;
27871da177e4SLinus Torvalds 	unsigned int val;
27881da177e4SLinus Torvalds 
27891da177e4SLinus Torvalds 	if (!snd_hdsp_use_is_exclusive(hdsp))
27901da177e4SLinus Torvalds 		return -EBUSY;
27911da177e4SLinus Torvalds 	val = ucontrol->value.integer.value[0] & 1;
27921da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
27931da177e4SLinus Torvalds 	change = (int)val != hdsp->precise_ptr;
27941da177e4SLinus Torvalds 	hdsp_set_precise_pointer(hdsp, val);
27951da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
27961da177e4SLinus Torvalds 	return change;
27971da177e4SLinus Torvalds }
27981da177e4SLinus Torvalds 
27991da177e4SLinus Torvalds #define HDSP_USE_MIDI_TASKLET(xname, xindex) \
280067ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_CARD, \
28011da177e4SLinus Torvalds   .name = xname, \
28021da177e4SLinus Torvalds   .index = xindex, \
28031da177e4SLinus Torvalds   .info = snd_hdsp_info_use_midi_tasklet, \
28041da177e4SLinus Torvalds   .get = snd_hdsp_get_use_midi_tasklet, \
28051da177e4SLinus Torvalds   .put = snd_hdsp_put_use_midi_tasklet \
28061da177e4SLinus Torvalds }
28071da177e4SLinus Torvalds 
280855e957d8STakashi Iwai static int hdsp_set_use_midi_tasklet(struct hdsp *hdsp, int use_tasklet)
28091da177e4SLinus Torvalds {
2810b0b98119STakashi Iwai 	if (use_tasklet)
28111da177e4SLinus Torvalds 		hdsp->use_midi_tasklet = 1;
2812b0b98119STakashi Iwai 	else
28131da177e4SLinus Torvalds 		hdsp->use_midi_tasklet = 0;
28141da177e4SLinus Torvalds 	return 0;
28151da177e4SLinus Torvalds }
28161da177e4SLinus Torvalds 
2817a5ce8890STakashi Iwai #define snd_hdsp_info_use_midi_tasklet		snd_ctl_boolean_mono_info
28181da177e4SLinus Torvalds 
281955e957d8STakashi Iwai static int snd_hdsp_get_use_midi_tasklet(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
28201da177e4SLinus Torvalds {
282155e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
28221da177e4SLinus Torvalds 
28231da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
28241da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = hdsp->use_midi_tasklet;
28251da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
28261da177e4SLinus Torvalds 	return 0;
28271da177e4SLinus Torvalds }
28281da177e4SLinus Torvalds 
282955e957d8STakashi Iwai static int snd_hdsp_put_use_midi_tasklet(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
28301da177e4SLinus Torvalds {
283155e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
28321da177e4SLinus Torvalds 	int change;
28331da177e4SLinus Torvalds 	unsigned int val;
28341da177e4SLinus Torvalds 
28351da177e4SLinus Torvalds 	if (!snd_hdsp_use_is_exclusive(hdsp))
28361da177e4SLinus Torvalds 		return -EBUSY;
28371da177e4SLinus Torvalds 	val = ucontrol->value.integer.value[0] & 1;
28381da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
28391da177e4SLinus Torvalds 	change = (int)val != hdsp->use_midi_tasklet;
28401da177e4SLinus Torvalds 	hdsp_set_use_midi_tasklet(hdsp, val);
28411da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
28421da177e4SLinus Torvalds 	return change;
28431da177e4SLinus Torvalds }
28441da177e4SLinus Torvalds 
28451da177e4SLinus Torvalds #define HDSP_MIXER(xname, xindex) \
28461da177e4SLinus Torvalds { .iface = SNDRV_CTL_ELEM_IFACE_HWDEP, \
28471da177e4SLinus Torvalds   .name = xname, \
28481da177e4SLinus Torvalds   .index = xindex, \
284967ed4161SClemens Ladisch   .device = 0, \
28501da177e4SLinus Torvalds   .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
28511da177e4SLinus Torvalds 		 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
28521da177e4SLinus Torvalds   .info = snd_hdsp_info_mixer, \
28531da177e4SLinus Torvalds   .get = snd_hdsp_get_mixer, \
28541da177e4SLinus Torvalds   .put = snd_hdsp_put_mixer \
28551da177e4SLinus Torvalds }
28561da177e4SLinus Torvalds 
285755e957d8STakashi Iwai static int snd_hdsp_info_mixer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
28581da177e4SLinus Torvalds {
28591da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
28601da177e4SLinus Torvalds 	uinfo->count = 3;
28611da177e4SLinus Torvalds 	uinfo->value.integer.min = 0;
28621da177e4SLinus Torvalds 	uinfo->value.integer.max = 65536;
28631da177e4SLinus Torvalds 	uinfo->value.integer.step = 1;
28641da177e4SLinus Torvalds 	return 0;
28651da177e4SLinus Torvalds }
28661da177e4SLinus Torvalds 
286755e957d8STakashi Iwai static int snd_hdsp_get_mixer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
28681da177e4SLinus Torvalds {
286955e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
28701da177e4SLinus Torvalds 	int source;
28711da177e4SLinus Torvalds 	int destination;
28721da177e4SLinus Torvalds 	int addr;
28731da177e4SLinus Torvalds 
28741da177e4SLinus Torvalds 	source = ucontrol->value.integer.value[0];
28751da177e4SLinus Torvalds 	destination = ucontrol->value.integer.value[1];
28761da177e4SLinus Torvalds 
2877b0b98119STakashi Iwai 	if (source >= hdsp->max_channels)
28781da177e4SLinus Torvalds 		addr = hdsp_playback_to_output_key(hdsp,source-hdsp->max_channels,destination);
2879b0b98119STakashi Iwai 	else
28801da177e4SLinus Torvalds 		addr = hdsp_input_to_output_key(hdsp,source, destination);
28811da177e4SLinus Torvalds 
28821da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
28831da177e4SLinus Torvalds 	ucontrol->value.integer.value[2] = hdsp_read_gain (hdsp, addr);
28841da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
28851da177e4SLinus Torvalds 	return 0;
28861da177e4SLinus Torvalds }
28871da177e4SLinus Torvalds 
288855e957d8STakashi Iwai static int snd_hdsp_put_mixer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
28891da177e4SLinus Torvalds {
289055e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
28911da177e4SLinus Torvalds 	int change;
28921da177e4SLinus Torvalds 	int source;
28931da177e4SLinus Torvalds 	int destination;
28941da177e4SLinus Torvalds 	int gain;
28951da177e4SLinus Torvalds 	int addr;
28961da177e4SLinus Torvalds 
28971da177e4SLinus Torvalds 	if (!snd_hdsp_use_is_exclusive(hdsp))
28981da177e4SLinus Torvalds 		return -EBUSY;
28991da177e4SLinus Torvalds 
29001da177e4SLinus Torvalds 	source = ucontrol->value.integer.value[0];
29011da177e4SLinus Torvalds 	destination = ucontrol->value.integer.value[1];
29021da177e4SLinus Torvalds 
2903b0b98119STakashi Iwai 	if (source >= hdsp->max_channels)
29041da177e4SLinus Torvalds 		addr = hdsp_playback_to_output_key(hdsp,source-hdsp->max_channels, destination);
2905b0b98119STakashi Iwai 	else
29061da177e4SLinus Torvalds 		addr = hdsp_input_to_output_key(hdsp,source, destination);
29071da177e4SLinus Torvalds 
29081da177e4SLinus Torvalds 	gain = ucontrol->value.integer.value[2];
29091da177e4SLinus Torvalds 
29101da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
29111da177e4SLinus Torvalds 	change = gain != hdsp_read_gain(hdsp, addr);
29121da177e4SLinus Torvalds 	if (change)
29131da177e4SLinus Torvalds 		hdsp_write_gain(hdsp, addr, gain);
29141da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
29151da177e4SLinus Torvalds 	return change;
29161da177e4SLinus Torvalds }
29171da177e4SLinus Torvalds 
29181da177e4SLinus Torvalds #define HDSP_WC_SYNC_CHECK(xname, xindex) \
291967ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
29201da177e4SLinus Torvalds   .name = xname, \
29211da177e4SLinus Torvalds   .index = xindex, \
29221da177e4SLinus Torvalds   .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
29231da177e4SLinus Torvalds   .info = snd_hdsp_info_sync_check, \
29241da177e4SLinus Torvalds   .get = snd_hdsp_get_wc_sync_check \
29251da177e4SLinus Torvalds }
29261da177e4SLinus Torvalds 
292755e957d8STakashi Iwai static int snd_hdsp_info_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
29281da177e4SLinus Torvalds {
29291da177e4SLinus Torvalds 	static char *texts[] = {"No Lock", "Lock", "Sync" };
29301da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
29311da177e4SLinus Torvalds 	uinfo->count = 1;
29321da177e4SLinus Torvalds 	uinfo->value.enumerated.items = 3;
29331da177e4SLinus Torvalds 	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
29341da177e4SLinus Torvalds 		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
29351da177e4SLinus Torvalds 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
29361da177e4SLinus Torvalds 	return 0;
29371da177e4SLinus Torvalds }
29381da177e4SLinus Torvalds 
293955e957d8STakashi Iwai static int hdsp_wc_sync_check(struct hdsp *hdsp)
29401da177e4SLinus Torvalds {
29411da177e4SLinus Torvalds 	int status2 = hdsp_read(hdsp, HDSP_status2Register);
29421da177e4SLinus Torvalds 	if (status2 & HDSP_wc_lock) {
2943b0b98119STakashi Iwai 		if (status2 & HDSP_wc_sync)
29441da177e4SLinus Torvalds 			return 2;
2945b0b98119STakashi Iwai 		else
29461da177e4SLinus Torvalds 			 return 1;
2947b0b98119STakashi Iwai 	} else
29481da177e4SLinus Torvalds 		return 0;
29491da177e4SLinus Torvalds 	return 0;
29501da177e4SLinus Torvalds }
29511da177e4SLinus Torvalds 
295255e957d8STakashi Iwai static int snd_hdsp_get_wc_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
29531da177e4SLinus Torvalds {
295455e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
29551da177e4SLinus Torvalds 
29561da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp_wc_sync_check(hdsp);
29571da177e4SLinus Torvalds 	return 0;
29581da177e4SLinus Torvalds }
29591da177e4SLinus Torvalds 
29601da177e4SLinus Torvalds #define HDSP_SPDIF_SYNC_CHECK(xname, xindex) \
296167ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
29621da177e4SLinus Torvalds   .name = xname, \
29631da177e4SLinus Torvalds   .index = xindex, \
29641da177e4SLinus Torvalds   .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
29651da177e4SLinus Torvalds   .info = snd_hdsp_info_sync_check, \
29661da177e4SLinus Torvalds   .get = snd_hdsp_get_spdif_sync_check \
29671da177e4SLinus Torvalds }
29681da177e4SLinus Torvalds 
296955e957d8STakashi Iwai static int hdsp_spdif_sync_check(struct hdsp *hdsp)
29701da177e4SLinus Torvalds {
29711da177e4SLinus Torvalds 	int status = hdsp_read(hdsp, HDSP_statusRegister);
2972b0b98119STakashi Iwai 	if (status & HDSP_SPDIFErrorFlag)
29731da177e4SLinus Torvalds 		return 0;
2974b0b98119STakashi Iwai 	else {
2975b0b98119STakashi Iwai 		if (status & HDSP_SPDIFSync)
29761da177e4SLinus Torvalds 			return 2;
2977b0b98119STakashi Iwai 		else
29781da177e4SLinus Torvalds 			return 1;
29791da177e4SLinus Torvalds 	}
29801da177e4SLinus Torvalds 	return 0;
29811da177e4SLinus Torvalds }
29821da177e4SLinus Torvalds 
298355e957d8STakashi Iwai static int snd_hdsp_get_spdif_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
29841da177e4SLinus Torvalds {
298555e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
29861da177e4SLinus Torvalds 
29871da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp_spdif_sync_check(hdsp);
29881da177e4SLinus Torvalds 	return 0;
29891da177e4SLinus Torvalds }
29901da177e4SLinus Torvalds 
29911da177e4SLinus Torvalds #define HDSP_ADATSYNC_SYNC_CHECK(xname, xindex) \
299267ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
29931da177e4SLinus Torvalds   .name = xname, \
29941da177e4SLinus Torvalds   .index = xindex, \
29951da177e4SLinus Torvalds   .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
29961da177e4SLinus Torvalds   .info = snd_hdsp_info_sync_check, \
29971da177e4SLinus Torvalds   .get = snd_hdsp_get_adatsync_sync_check \
29981da177e4SLinus Torvalds }
29991da177e4SLinus Torvalds 
300055e957d8STakashi Iwai static int hdsp_adatsync_sync_check(struct hdsp *hdsp)
30011da177e4SLinus Torvalds {
30021da177e4SLinus Torvalds 	int status = hdsp_read(hdsp, HDSP_statusRegister);
30031da177e4SLinus Torvalds 	if (status & HDSP_TimecodeLock) {
3004b0b98119STakashi Iwai 		if (status & HDSP_TimecodeSync)
30051da177e4SLinus Torvalds 			return 2;
3006b0b98119STakashi Iwai 		else
30071da177e4SLinus Torvalds 			return 1;
3008b0b98119STakashi Iwai 	} else
30091da177e4SLinus Torvalds 		return 0;
30101da177e4SLinus Torvalds }
30111da177e4SLinus Torvalds 
301255e957d8STakashi Iwai static int snd_hdsp_get_adatsync_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
30131da177e4SLinus Torvalds {
301455e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
30151da177e4SLinus Torvalds 
30161da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp_adatsync_sync_check(hdsp);
30171da177e4SLinus Torvalds 	return 0;
30181da177e4SLinus Torvalds }
30191da177e4SLinus Torvalds 
30201da177e4SLinus Torvalds #define HDSP_ADAT_SYNC_CHECK \
302167ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
30221da177e4SLinus Torvalds   .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
30231da177e4SLinus Torvalds   .info = snd_hdsp_info_sync_check, \
30241da177e4SLinus Torvalds   .get = snd_hdsp_get_adat_sync_check \
30251da177e4SLinus Torvalds }
30261da177e4SLinus Torvalds 
302755e957d8STakashi Iwai static int hdsp_adat_sync_check(struct hdsp *hdsp, int idx)
30281da177e4SLinus Torvalds {
30291da177e4SLinus Torvalds 	int status = hdsp_read(hdsp, HDSP_statusRegister);
30301da177e4SLinus Torvalds 
30311da177e4SLinus Torvalds 	if (status & (HDSP_Lock0>>idx)) {
3032b0b98119STakashi Iwai 		if (status & (HDSP_Sync0>>idx))
30331da177e4SLinus Torvalds 			return 2;
3034b0b98119STakashi Iwai 		else
30351da177e4SLinus Torvalds 			return 1;
3036b0b98119STakashi Iwai 	} else
30371da177e4SLinus Torvalds 		return 0;
30381da177e4SLinus Torvalds }
30391da177e4SLinus Torvalds 
304055e957d8STakashi Iwai static int snd_hdsp_get_adat_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
30411da177e4SLinus Torvalds {
30421da177e4SLinus Torvalds 	int offset;
304355e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
30441da177e4SLinus Torvalds 
30451da177e4SLinus Torvalds 	offset = ucontrol->id.index - 1;
3046da3cec35STakashi Iwai 	snd_BUG_ON(offset < 0);
30471da177e4SLinus Torvalds 
30481da177e4SLinus Torvalds 	switch (hdsp->io_type) {
30491da177e4SLinus Torvalds 	case Digiface:
30501da177e4SLinus Torvalds 	case H9652:
30511da177e4SLinus Torvalds 		if (offset >= 3)
30521da177e4SLinus Torvalds 			return -EINVAL;
30531da177e4SLinus Torvalds 		break;
30541da177e4SLinus Torvalds 	case Multiface:
30551da177e4SLinus Torvalds 	case H9632:
30561da177e4SLinus Torvalds 		if (offset >= 1)
30571da177e4SLinus Torvalds 			return -EINVAL;
30581da177e4SLinus Torvalds 		break;
30591da177e4SLinus Torvalds 	default:
30601da177e4SLinus Torvalds 		return -EIO;
30611da177e4SLinus Torvalds 	}
30621da177e4SLinus Torvalds 
30631da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp_adat_sync_check(hdsp, offset);
30641da177e4SLinus Torvalds 	return 0;
30651da177e4SLinus Torvalds }
30661da177e4SLinus Torvalds 
3067e4b6088cSJulian Cable #define HDSP_DDS_OFFSET(xname, xindex) \
3068e4b6088cSJulian Cable { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3069e4b6088cSJulian Cable   .name = xname, \
3070e4b6088cSJulian Cable   .index = xindex, \
3071e4b6088cSJulian Cable   .info = snd_hdsp_info_dds_offset, \
3072e4b6088cSJulian Cable   .get = snd_hdsp_get_dds_offset, \
3073e4b6088cSJulian Cable   .put = snd_hdsp_put_dds_offset \
3074e4b6088cSJulian Cable }
3075e4b6088cSJulian Cable 
3076e4b6088cSJulian Cable static int hdsp_dds_offset(struct hdsp *hdsp)
3077e4b6088cSJulian Cable {
3078e4b6088cSJulian Cable 	u64 n;
3079e4b6088cSJulian Cable 	u32 r;
3080e4b6088cSJulian Cable 	unsigned int dds_value = hdsp->dds_value;
3081e4b6088cSJulian Cable 	int system_sample_rate = hdsp->system_sample_rate;
3082e4b6088cSJulian Cable 
30832a3988f6STakashi Iwai 	if (!dds_value)
30842a3988f6STakashi Iwai 		return 0;
30852a3988f6STakashi Iwai 
3086e4b6088cSJulian Cable 	n = DDS_NUMERATOR;
3087e4b6088cSJulian Cable 	/*
3088e4b6088cSJulian Cable 	 * dds_value = n / rate
3089e4b6088cSJulian Cable 	 * rate = n / dds_value
3090e4b6088cSJulian Cable 	 */
3091e4b6088cSJulian Cable 	div64_32(&n, dds_value, &r);
3092e4b6088cSJulian Cable 	if (system_sample_rate >= 112000)
3093e4b6088cSJulian Cable 		n *= 4;
3094e4b6088cSJulian Cable 	else if (system_sample_rate >= 56000)
3095e4b6088cSJulian Cable 		n *= 2;
3096e4b6088cSJulian Cable 	return ((int)n) - system_sample_rate;
3097e4b6088cSJulian Cable }
3098e4b6088cSJulian Cable 
3099e4b6088cSJulian Cable static int hdsp_set_dds_offset(struct hdsp *hdsp, int offset_hz)
3100e4b6088cSJulian Cable {
3101e4b6088cSJulian Cable 	int rate = hdsp->system_sample_rate + offset_hz;
3102e4b6088cSJulian Cable 	hdsp_set_dds_value(hdsp, rate);
3103e4b6088cSJulian Cable 	return 0;
3104e4b6088cSJulian Cable }
3105e4b6088cSJulian Cable 
3106e4b6088cSJulian Cable static int snd_hdsp_info_dds_offset(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
3107e4b6088cSJulian Cable {
3108e4b6088cSJulian Cable 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3109e4b6088cSJulian Cable 	uinfo->count = 1;
3110e4b6088cSJulian Cable 	uinfo->value.integer.min = -5000;
3111e4b6088cSJulian Cable 	uinfo->value.integer.max = 5000;
3112e4b6088cSJulian Cable 	return 0;
3113e4b6088cSJulian Cable }
3114e4b6088cSJulian Cable 
3115e4b6088cSJulian Cable static int snd_hdsp_get_dds_offset(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3116e4b6088cSJulian Cable {
3117e4b6088cSJulian Cable 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
3118e4b6088cSJulian Cable 
3119e4b6088cSJulian Cable 	ucontrol->value.enumerated.item[0] = hdsp_dds_offset(hdsp);
3120e4b6088cSJulian Cable 	return 0;
3121e4b6088cSJulian Cable }
3122e4b6088cSJulian Cable 
3123e4b6088cSJulian Cable static int snd_hdsp_put_dds_offset(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3124e4b6088cSJulian Cable {
3125e4b6088cSJulian Cable 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
3126e4b6088cSJulian Cable 	int change;
3127e4b6088cSJulian Cable 	int val;
3128e4b6088cSJulian Cable 
3129e4b6088cSJulian Cable 	if (!snd_hdsp_use_is_exclusive(hdsp))
3130e4b6088cSJulian Cable 		return -EBUSY;
3131e4b6088cSJulian Cable 	val = ucontrol->value.enumerated.item[0];
3132e4b6088cSJulian Cable 	spin_lock_irq(&hdsp->lock);
3133e4b6088cSJulian Cable 	if (val != hdsp_dds_offset(hdsp))
3134e4b6088cSJulian Cable 		change = (hdsp_set_dds_offset(hdsp, val) == 0) ? 1 : 0;
3135e4b6088cSJulian Cable 	else
3136e4b6088cSJulian Cable 		change = 0;
3137e4b6088cSJulian Cable 	spin_unlock_irq(&hdsp->lock);
3138e4b6088cSJulian Cable 	return change;
3139e4b6088cSJulian Cable }
3140e4b6088cSJulian Cable 
314155e957d8STakashi Iwai static struct snd_kcontrol_new snd_hdsp_9632_controls[] = {
31421da177e4SLinus Torvalds HDSP_DA_GAIN("DA Gain", 0),
31431da177e4SLinus Torvalds HDSP_AD_GAIN("AD Gain", 0),
31441da177e4SLinus Torvalds HDSP_PHONE_GAIN("Phones Gain", 0),
3145e4b6088cSJulian Cable HDSP_XLR_BREAKOUT_CABLE("XLR Breakout Cable", 0),
3146e4b6088cSJulian Cable HDSP_DDS_OFFSET("DDS Sample Rate Offset", 0)
31471da177e4SLinus Torvalds };
31481da177e4SLinus Torvalds 
314955e957d8STakashi Iwai static struct snd_kcontrol_new snd_hdsp_controls[] = {
31501da177e4SLinus Torvalds {
31515549d549SClemens Ladisch 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
31521da177e4SLinus Torvalds 	.name =		SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
31531da177e4SLinus Torvalds 	.info =		snd_hdsp_control_spdif_info,
31541da177e4SLinus Torvalds 	.get =		snd_hdsp_control_spdif_get,
31551da177e4SLinus Torvalds 	.put =		snd_hdsp_control_spdif_put,
31561da177e4SLinus Torvalds },
31571da177e4SLinus Torvalds {
31581da177e4SLinus Torvalds 	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
31595549d549SClemens Ladisch 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
31601da177e4SLinus Torvalds 	.name =		SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
31611da177e4SLinus Torvalds 	.info =		snd_hdsp_control_spdif_stream_info,
31621da177e4SLinus Torvalds 	.get =		snd_hdsp_control_spdif_stream_get,
31631da177e4SLinus Torvalds 	.put =		snd_hdsp_control_spdif_stream_put,
31641da177e4SLinus Torvalds },
31651da177e4SLinus Torvalds {
31661da177e4SLinus Torvalds 	.access =	SNDRV_CTL_ELEM_ACCESS_READ,
31675549d549SClemens Ladisch 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
31681da177e4SLinus Torvalds 	.name =		SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
31691da177e4SLinus Torvalds 	.info =		snd_hdsp_control_spdif_mask_info,
31701da177e4SLinus Torvalds 	.get =		snd_hdsp_control_spdif_mask_get,
31711da177e4SLinus Torvalds 	.private_value = IEC958_AES0_NONAUDIO |
31721da177e4SLinus Torvalds   			 IEC958_AES0_PROFESSIONAL |
31731da177e4SLinus Torvalds 			 IEC958_AES0_CON_EMPHASIS,
31741da177e4SLinus Torvalds },
31751da177e4SLinus Torvalds {
31761da177e4SLinus Torvalds 	.access =	SNDRV_CTL_ELEM_ACCESS_READ,
31775549d549SClemens Ladisch 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
31781da177e4SLinus Torvalds 	.name =		SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
31791da177e4SLinus Torvalds 	.info =		snd_hdsp_control_spdif_mask_info,
31801da177e4SLinus Torvalds 	.get =		snd_hdsp_control_spdif_mask_get,
31811da177e4SLinus Torvalds 	.private_value = IEC958_AES0_NONAUDIO |
31821da177e4SLinus Torvalds 			 IEC958_AES0_PROFESSIONAL |
31831da177e4SLinus Torvalds 			 IEC958_AES0_PRO_EMPHASIS,
31841da177e4SLinus Torvalds },
31851da177e4SLinus Torvalds HDSP_MIXER("Mixer", 0),
31861da177e4SLinus Torvalds HDSP_SPDIF_IN("IEC958 Input Connector", 0),
31871da177e4SLinus Torvalds HDSP_SPDIF_OUT("IEC958 Output also on ADAT1", 0),
31881da177e4SLinus Torvalds HDSP_SPDIF_PROFESSIONAL("IEC958 Professional Bit", 0),
31891da177e4SLinus Torvalds HDSP_SPDIF_EMPHASIS("IEC958 Emphasis Bit", 0),
31901da177e4SLinus Torvalds HDSP_SPDIF_NON_AUDIO("IEC958 Non-audio Bit", 0),
31911da177e4SLinus Torvalds /* 'Sample Clock Source' complies with the alsa control naming scheme */
31921da177e4SLinus Torvalds HDSP_CLOCK_SOURCE("Sample Clock Source", 0),
3193e3ea4d89STakashi Iwai {
3194e3ea4d89STakashi Iwai 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3195e3ea4d89STakashi Iwai 	.name = "Sample Clock Source Locking",
3196e3ea4d89STakashi Iwai 	.info = snd_hdsp_info_clock_source_lock,
3197e3ea4d89STakashi Iwai 	.get = snd_hdsp_get_clock_source_lock,
3198e3ea4d89STakashi Iwai 	.put = snd_hdsp_put_clock_source_lock,
3199e3ea4d89STakashi Iwai },
32001da177e4SLinus Torvalds HDSP_SYSTEM_CLOCK_MODE("System Clock Mode", 0),
32011da177e4SLinus Torvalds HDSP_PREF_SYNC_REF("Preferred Sync Reference", 0),
32021da177e4SLinus Torvalds HDSP_AUTOSYNC_REF("AutoSync Reference", 0),
32031da177e4SLinus Torvalds HDSP_SPDIF_SAMPLE_RATE("SPDIF Sample Rate", 0),
32041da177e4SLinus Torvalds HDSP_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
32051da177e4SLinus Torvalds /* 'External Rate' complies with the alsa control naming scheme */
32061da177e4SLinus Torvalds HDSP_AUTOSYNC_SAMPLE_RATE("External Rate", 0),
32071da177e4SLinus Torvalds HDSP_WC_SYNC_CHECK("Word Clock Lock Status", 0),
32081da177e4SLinus Torvalds HDSP_SPDIF_SYNC_CHECK("SPDIF Lock Status", 0),
32091da177e4SLinus Torvalds HDSP_ADATSYNC_SYNC_CHECK("ADAT Sync Lock Status", 0),
32101da177e4SLinus Torvalds HDSP_LINE_OUT("Line Out", 0),
32111da177e4SLinus Torvalds HDSP_PRECISE_POINTER("Precise Pointer", 0),
32121da177e4SLinus Torvalds HDSP_USE_MIDI_TASKLET("Use Midi Tasklet", 0),
32131da177e4SLinus Torvalds };
32141da177e4SLinus Torvalds 
321555e957d8STakashi Iwai static struct snd_kcontrol_new snd_hdsp_96xx_aeb = HDSP_AEB("Analog Extension Board", 0);
321655e957d8STakashi Iwai static struct snd_kcontrol_new snd_hdsp_adat_sync_check = HDSP_ADAT_SYNC_CHECK;
32171da177e4SLinus Torvalds 
321855e957d8STakashi Iwai static int snd_hdsp_create_controls(struct snd_card *card, struct hdsp *hdsp)
32191da177e4SLinus Torvalds {
32201da177e4SLinus Torvalds 	unsigned int idx;
32211da177e4SLinus Torvalds 	int err;
322255e957d8STakashi Iwai 	struct snd_kcontrol *kctl;
32231da177e4SLinus Torvalds 
32241da177e4SLinus Torvalds 	for (idx = 0; idx < ARRAY_SIZE(snd_hdsp_controls); idx++) {
3225b0b98119STakashi Iwai 		if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_controls[idx], hdsp))) < 0)
32261da177e4SLinus Torvalds 			return err;
32271da177e4SLinus Torvalds 		if (idx == 1)	/* IEC958 (S/PDIF) Stream */
32281da177e4SLinus Torvalds 			hdsp->spdif_ctl = kctl;
32291da177e4SLinus Torvalds 	}
32301da177e4SLinus Torvalds 
32311da177e4SLinus Torvalds 	/* ADAT SyncCheck status */
32321da177e4SLinus Torvalds 	snd_hdsp_adat_sync_check.name = "ADAT Lock Status";
32331da177e4SLinus Torvalds 	snd_hdsp_adat_sync_check.index = 1;
3234b0b98119STakashi Iwai 	if ((err = snd_ctl_add (card, kctl = snd_ctl_new1(&snd_hdsp_adat_sync_check, hdsp))))
32351da177e4SLinus Torvalds 		return err;
32361da177e4SLinus Torvalds 	if (hdsp->io_type == Digiface || hdsp->io_type == H9652) {
32371da177e4SLinus Torvalds 		for (idx = 1; idx < 3; ++idx) {
32381da177e4SLinus Torvalds 			snd_hdsp_adat_sync_check.index = idx+1;
3239b0b98119STakashi Iwai 			if ((err = snd_ctl_add (card, kctl = snd_ctl_new1(&snd_hdsp_adat_sync_check, hdsp))))
32401da177e4SLinus Torvalds 				return err;
32411da177e4SLinus Torvalds 		}
32421da177e4SLinus Torvalds 	}
32431da177e4SLinus Torvalds 
32441da177e4SLinus Torvalds 	/* DA, AD and Phone gain and XLR breakout cable controls for H9632 cards */
32451da177e4SLinus Torvalds 	if (hdsp->io_type == H9632) {
32461da177e4SLinus Torvalds 		for (idx = 0; idx < ARRAY_SIZE(snd_hdsp_9632_controls); idx++) {
3247b0b98119STakashi Iwai 			if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_9632_controls[idx], hdsp))) < 0)
32481da177e4SLinus Torvalds 				return err;
32491da177e4SLinus Torvalds 		}
32501da177e4SLinus Torvalds 	}
32511da177e4SLinus Torvalds 
32521da177e4SLinus Torvalds 	/* AEB control for H96xx card */
32531da177e4SLinus Torvalds 	if (hdsp->io_type == H9632 || hdsp->io_type == H9652) {
3254b0b98119STakashi Iwai 		if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_96xx_aeb, hdsp))) < 0)
32551da177e4SLinus Torvalds 				return err;
32561da177e4SLinus Torvalds 	}
32571da177e4SLinus Torvalds 
32581da177e4SLinus Torvalds 	return 0;
32591da177e4SLinus Torvalds }
32601da177e4SLinus Torvalds 
32611da177e4SLinus Torvalds /*------------------------------------------------------------
32621da177e4SLinus Torvalds    /proc interface
32631da177e4SLinus Torvalds  ------------------------------------------------------------*/
32641da177e4SLinus Torvalds 
32651da177e4SLinus Torvalds static void
326655e957d8STakashi Iwai snd_hdsp_proc_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
32671da177e4SLinus Torvalds {
326855e957d8STakashi Iwai 	struct hdsp *hdsp = (struct hdsp *) entry->private_data;
32691da177e4SLinus Torvalds 	unsigned int status;
32701da177e4SLinus Torvalds 	unsigned int status2;
32711da177e4SLinus Torvalds 	char *pref_sync_ref;
32721da177e4SLinus Torvalds 	char *autosync_ref;
32731da177e4SLinus Torvalds 	char *system_clock_mode;
32741da177e4SLinus Torvalds 	char *clock_source;
32751da177e4SLinus Torvalds 	int x;
32761da177e4SLinus Torvalds 
3277ecb594e6SRemy Bruno 	if (hdsp_check_for_iobox (hdsp)) {
32781da177e4SLinus Torvalds 		snd_iprintf(buffer, "No I/O box connected.\nPlease connect one and upload firmware.\n");
32791da177e4SLinus Torvalds 		return;
3280ecb594e6SRemy Bruno         }
32811da177e4SLinus Torvalds 
3282b0b98119STakashi Iwai 	if (hdsp_check_for_firmware(hdsp, 0)) {
32831da177e4SLinus Torvalds 		if (hdsp->state & HDSP_FirmwareCached) {
32841da177e4SLinus Torvalds 			if (snd_hdsp_load_firmware_from_cache(hdsp) != 0) {
32851da177e4SLinus Torvalds 				snd_iprintf(buffer, "Firmware loading from cache failed, please upload manually.\n");
32861da177e4SLinus Torvalds 				return;
32871da177e4SLinus Torvalds 			}
32881da177e4SLinus Torvalds 		} else {
3289311e70a4STakashi Iwai 			int err = -EINVAL;
3290311e70a4STakashi Iwai #ifdef HDSP_FW_LOADER
3291311e70a4STakashi Iwai 			err = hdsp_request_fw_loader(hdsp);
3292311e70a4STakashi Iwai #endif
3293311e70a4STakashi Iwai 			if (err < 0) {
3294311e70a4STakashi Iwai 				snd_iprintf(buffer,
3295311e70a4STakashi Iwai 					    "No firmware loaded nor cached, "
3296311e70a4STakashi Iwai 					    "please upload firmware.\n");
32971da177e4SLinus Torvalds 				return;
32981da177e4SLinus Torvalds 			}
32991da177e4SLinus Torvalds 		}
3300311e70a4STakashi Iwai 	}
33011da177e4SLinus Torvalds 
33021da177e4SLinus Torvalds 	status = hdsp_read(hdsp, HDSP_statusRegister);
33031da177e4SLinus Torvalds 	status2 = hdsp_read(hdsp, HDSP_status2Register);
33041da177e4SLinus Torvalds 
33051da177e4SLinus Torvalds 	snd_iprintf(buffer, "%s (Card #%d)\n", hdsp->card_name, hdsp->card->number + 1);
33061da177e4SLinus Torvalds 	snd_iprintf(buffer, "Buffers: capture %p playback %p\n",
33071da177e4SLinus Torvalds 		    hdsp->capture_buffer, hdsp->playback_buffer);
33081da177e4SLinus Torvalds 	snd_iprintf(buffer, "IRQ: %d Registers bus: 0x%lx VM: 0x%lx\n",
33091da177e4SLinus Torvalds 		    hdsp->irq, hdsp->port, (unsigned long)hdsp->iobase);
33101da177e4SLinus Torvalds 	snd_iprintf(buffer, "Control register: 0x%x\n", hdsp->control_register);
33111da177e4SLinus Torvalds 	snd_iprintf(buffer, "Control2 register: 0x%x\n", hdsp->control2_register);
33121da177e4SLinus Torvalds 	snd_iprintf(buffer, "Status register: 0x%x\n", status);
33131da177e4SLinus Torvalds 	snd_iprintf(buffer, "Status2 register: 0x%x\n", status2);
33141da177e4SLinus Torvalds 	snd_iprintf(buffer, "FIFO status: %d\n", hdsp_read(hdsp, HDSP_fifoStatus) & 0xff);
33151da177e4SLinus Torvalds 	snd_iprintf(buffer, "MIDI1 Output status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusOut0));
33161da177e4SLinus Torvalds 	snd_iprintf(buffer, "MIDI1 Input status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusIn0));
33171da177e4SLinus Torvalds 	snd_iprintf(buffer, "MIDI2 Output status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusOut1));
33181da177e4SLinus Torvalds 	snd_iprintf(buffer, "MIDI2 Input status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusIn1));
33191da177e4SLinus Torvalds 	snd_iprintf(buffer, "Use Midi Tasklet: %s\n", hdsp->use_midi_tasklet ? "on" : "off");
33201da177e4SLinus Torvalds 
33211da177e4SLinus Torvalds 	snd_iprintf(buffer, "\n");
33221da177e4SLinus Torvalds 
33231da177e4SLinus Torvalds 	x = 1 << (6 + hdsp_decode_latency(hdsp->control_register & HDSP_LatencyMask));
33241da177e4SLinus Torvalds 
33251da177e4SLinus Torvalds 	snd_iprintf(buffer, "Buffer Size (Latency): %d samples (2 periods of %lu bytes)\n", x, (unsigned long) hdsp->period_bytes);
33261da177e4SLinus Torvalds 	snd_iprintf(buffer, "Hardware pointer (frames): %ld\n", hdsp_hw_pointer(hdsp));
33271da177e4SLinus Torvalds 	snd_iprintf(buffer, "Precise pointer: %s\n", hdsp->precise_ptr ? "on" : "off");
33281da177e4SLinus Torvalds 	snd_iprintf(buffer, "Line out: %s\n", (hdsp->control_register & HDSP_LineOut) ? "on" : "off");
33291da177e4SLinus Torvalds 
33301da177e4SLinus Torvalds 	snd_iprintf(buffer, "Firmware version: %d\n", (status2&HDSP_version0)|(status2&HDSP_version1)<<1|(status2&HDSP_version2)<<2);
33311da177e4SLinus Torvalds 
33321da177e4SLinus Torvalds 	snd_iprintf(buffer, "\n");
33331da177e4SLinus Torvalds 
33341da177e4SLinus Torvalds 
33351da177e4SLinus Torvalds 	switch (hdsp_clock_source(hdsp)) {
33361da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_AUTOSYNC:
33371da177e4SLinus Torvalds 		clock_source = "AutoSync";
33381da177e4SLinus Torvalds 		break;
33391da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_32KHZ:
33401da177e4SLinus Torvalds 		clock_source = "Internal 32 kHz";
33411da177e4SLinus Torvalds 		break;
33421da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_44_1KHZ:
33431da177e4SLinus Torvalds 		clock_source = "Internal 44.1 kHz";
33441da177e4SLinus Torvalds 		break;
33451da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_48KHZ:
33461da177e4SLinus Torvalds 		clock_source = "Internal 48 kHz";
33471da177e4SLinus Torvalds 		break;
33481da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_64KHZ:
33491da177e4SLinus Torvalds 		clock_source = "Internal 64 kHz";
33501da177e4SLinus Torvalds 		break;
33511da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_88_2KHZ:
33521da177e4SLinus Torvalds 		clock_source = "Internal 88.2 kHz";
33531da177e4SLinus Torvalds 		break;
33541da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_96KHZ:
33551da177e4SLinus Torvalds 		clock_source = "Internal 96 kHz";
33561da177e4SLinus Torvalds 		break;
33571da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_128KHZ:
33581da177e4SLinus Torvalds 		clock_source = "Internal 128 kHz";
33591da177e4SLinus Torvalds 		break;
33601da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_176_4KHZ:
33611da177e4SLinus Torvalds 		clock_source = "Internal 176.4 kHz";
33621da177e4SLinus Torvalds 		break;
33631da177e4SLinus Torvalds 		case HDSP_CLOCK_SOURCE_INTERNAL_192KHZ:
33641da177e4SLinus Torvalds 		clock_source = "Internal 192 kHz";
33651da177e4SLinus Torvalds 		break;
33661da177e4SLinus Torvalds 	default:
33671da177e4SLinus Torvalds 		clock_source = "Error";
33681da177e4SLinus Torvalds 	}
33691da177e4SLinus Torvalds 	snd_iprintf (buffer, "Sample Clock Source: %s\n", clock_source);
33701da177e4SLinus Torvalds 
3371b0b98119STakashi Iwai 	if (hdsp_system_clock_mode(hdsp))
33721da177e4SLinus Torvalds 		system_clock_mode = "Slave";
3373b0b98119STakashi Iwai 	else
33741da177e4SLinus Torvalds 		system_clock_mode = "Master";
33751da177e4SLinus Torvalds 
33761da177e4SLinus Torvalds 	switch (hdsp_pref_sync_ref (hdsp)) {
33771da177e4SLinus Torvalds 	case HDSP_SYNC_FROM_WORD:
33781da177e4SLinus Torvalds 		pref_sync_ref = "Word Clock";
33791da177e4SLinus Torvalds 		break;
33801da177e4SLinus Torvalds 	case HDSP_SYNC_FROM_ADAT_SYNC:
33811da177e4SLinus Torvalds 		pref_sync_ref = "ADAT Sync";
33821da177e4SLinus Torvalds 		break;
33831da177e4SLinus Torvalds 	case HDSP_SYNC_FROM_SPDIF:
33841da177e4SLinus Torvalds 		pref_sync_ref = "SPDIF";
33851da177e4SLinus Torvalds 		break;
33861da177e4SLinus Torvalds 	case HDSP_SYNC_FROM_ADAT1:
33871da177e4SLinus Torvalds 		pref_sync_ref = "ADAT1";
33881da177e4SLinus Torvalds 		break;
33891da177e4SLinus Torvalds 	case HDSP_SYNC_FROM_ADAT2:
33901da177e4SLinus Torvalds 		pref_sync_ref = "ADAT2";
33911da177e4SLinus Torvalds 		break;
33921da177e4SLinus Torvalds 	case HDSP_SYNC_FROM_ADAT3:
33931da177e4SLinus Torvalds 		pref_sync_ref = "ADAT3";
33941da177e4SLinus Torvalds 		break;
33951da177e4SLinus Torvalds 	default:
33961da177e4SLinus Torvalds 		pref_sync_ref = "Word Clock";
33971da177e4SLinus Torvalds 		break;
33981da177e4SLinus Torvalds 	}
33991da177e4SLinus Torvalds 	snd_iprintf (buffer, "Preferred Sync Reference: %s\n", pref_sync_ref);
34001da177e4SLinus Torvalds 
34011da177e4SLinus Torvalds 	switch (hdsp_autosync_ref (hdsp)) {
34021da177e4SLinus Torvalds 	case HDSP_AUTOSYNC_FROM_WORD:
34031da177e4SLinus Torvalds 		autosync_ref = "Word Clock";
34041da177e4SLinus Torvalds 		break;
34051da177e4SLinus Torvalds 	case HDSP_AUTOSYNC_FROM_ADAT_SYNC:
34061da177e4SLinus Torvalds 		autosync_ref = "ADAT Sync";
34071da177e4SLinus Torvalds 		break;
34081da177e4SLinus Torvalds 	case HDSP_AUTOSYNC_FROM_SPDIF:
34091da177e4SLinus Torvalds 		autosync_ref = "SPDIF";
34101da177e4SLinus Torvalds 		break;
34111da177e4SLinus Torvalds 	case HDSP_AUTOSYNC_FROM_NONE:
34121da177e4SLinus Torvalds 		autosync_ref = "None";
34131da177e4SLinus Torvalds 		break;
34141da177e4SLinus Torvalds 	case HDSP_AUTOSYNC_FROM_ADAT1:
34151da177e4SLinus Torvalds 		autosync_ref = "ADAT1";
34161da177e4SLinus Torvalds 		break;
34171da177e4SLinus Torvalds 	case HDSP_AUTOSYNC_FROM_ADAT2:
34181da177e4SLinus Torvalds 		autosync_ref = "ADAT2";
34191da177e4SLinus Torvalds 		break;
34201da177e4SLinus Torvalds 	case HDSP_AUTOSYNC_FROM_ADAT3:
34211da177e4SLinus Torvalds 		autosync_ref = "ADAT3";
34221da177e4SLinus Torvalds 		break;
34231da177e4SLinus Torvalds 	default:
34241da177e4SLinus Torvalds 		autosync_ref = "---";
34251da177e4SLinus Torvalds 		break;
34261da177e4SLinus Torvalds 	}
34271da177e4SLinus Torvalds 	snd_iprintf (buffer, "AutoSync Reference: %s\n", autosync_ref);
34281da177e4SLinus Torvalds 
34291da177e4SLinus Torvalds 	snd_iprintf (buffer, "AutoSync Frequency: %d\n", hdsp_external_sample_rate(hdsp));
34301da177e4SLinus Torvalds 
34311da177e4SLinus Torvalds 	snd_iprintf (buffer, "System Clock Mode: %s\n", system_clock_mode);
34321da177e4SLinus Torvalds 
34331da177e4SLinus Torvalds 	snd_iprintf (buffer, "System Clock Frequency: %d\n", hdsp->system_sample_rate);
3434e3ea4d89STakashi Iwai 	snd_iprintf (buffer, "System Clock Locked: %s\n", hdsp->clock_source_locked ? "Yes" : "No");
34351da177e4SLinus Torvalds 
34361da177e4SLinus Torvalds 	snd_iprintf(buffer, "\n");
34371da177e4SLinus Torvalds 
34381da177e4SLinus Torvalds 	switch (hdsp_spdif_in(hdsp)) {
34391da177e4SLinus Torvalds 	case HDSP_SPDIFIN_OPTICAL:
34401da177e4SLinus Torvalds 		snd_iprintf(buffer, "IEC958 input: Optical\n");
34411da177e4SLinus Torvalds 		break;
34421da177e4SLinus Torvalds 	case HDSP_SPDIFIN_COAXIAL:
34431da177e4SLinus Torvalds 		snd_iprintf(buffer, "IEC958 input: Coaxial\n");
34441da177e4SLinus Torvalds 		break;
34451da177e4SLinus Torvalds 	case HDSP_SPDIFIN_INTERNAL:
34461da177e4SLinus Torvalds 		snd_iprintf(buffer, "IEC958 input: Internal\n");
34471da177e4SLinus Torvalds 		break;
34481da177e4SLinus Torvalds 	case HDSP_SPDIFIN_AES:
34491da177e4SLinus Torvalds 		snd_iprintf(buffer, "IEC958 input: AES\n");
34501da177e4SLinus Torvalds 		break;
34511da177e4SLinus Torvalds 	default:
34521da177e4SLinus Torvalds 		snd_iprintf(buffer, "IEC958 input: ???\n");
34531da177e4SLinus Torvalds 		break;
34541da177e4SLinus Torvalds 	}
34551da177e4SLinus Torvalds 
3456b0b98119STakashi Iwai 	if (hdsp->control_register & HDSP_SPDIFOpticalOut)
34571da177e4SLinus Torvalds 		snd_iprintf(buffer, "IEC958 output: Coaxial & ADAT1\n");
3458b0b98119STakashi Iwai 	else
34591da177e4SLinus Torvalds 		snd_iprintf(buffer, "IEC958 output: Coaxial only\n");
34601da177e4SLinus Torvalds 
3461b0b98119STakashi Iwai 	if (hdsp->control_register & HDSP_SPDIFProfessional)
34621da177e4SLinus Torvalds 		snd_iprintf(buffer, "IEC958 quality: Professional\n");
3463b0b98119STakashi Iwai 	else
34641da177e4SLinus Torvalds 		snd_iprintf(buffer, "IEC958 quality: Consumer\n");
34651da177e4SLinus Torvalds 
3466b0b98119STakashi Iwai 	if (hdsp->control_register & HDSP_SPDIFEmphasis)
34671da177e4SLinus Torvalds 		snd_iprintf(buffer, "IEC958 emphasis: on\n");
3468b0b98119STakashi Iwai 	else
34691da177e4SLinus Torvalds 		snd_iprintf(buffer, "IEC958 emphasis: off\n");
34701da177e4SLinus Torvalds 
3471b0b98119STakashi Iwai 	if (hdsp->control_register & HDSP_SPDIFNonAudio)
34721da177e4SLinus Torvalds 		snd_iprintf(buffer, "IEC958 NonAudio: on\n");
3473b0b98119STakashi Iwai 	else
34741da177e4SLinus Torvalds 		snd_iprintf(buffer, "IEC958 NonAudio: off\n");
3475b0b98119STakashi Iwai 	if ((x = hdsp_spdif_sample_rate (hdsp)) != 0)
34761da177e4SLinus Torvalds 		snd_iprintf (buffer, "IEC958 sample rate: %d\n", x);
3477b0b98119STakashi Iwai 	else
34781da177e4SLinus Torvalds 		snd_iprintf (buffer, "IEC958 sample rate: Error flag set\n");
34791da177e4SLinus Torvalds 
34801da177e4SLinus Torvalds 	snd_iprintf(buffer, "\n");
34811da177e4SLinus Torvalds 
34821da177e4SLinus Torvalds 	/* Sync Check */
34831da177e4SLinus Torvalds 	x = status & HDSP_Sync0;
3484b0b98119STakashi Iwai 	if (status & HDSP_Lock0)
34851da177e4SLinus Torvalds 		snd_iprintf(buffer, "ADAT1: %s\n", x ? "Sync" : "Lock");
3486b0b98119STakashi Iwai 	else
34871da177e4SLinus Torvalds 		snd_iprintf(buffer, "ADAT1: No Lock\n");
34881da177e4SLinus Torvalds 
34891da177e4SLinus Torvalds 	switch (hdsp->io_type) {
34901da177e4SLinus Torvalds 	case Digiface:
34911da177e4SLinus Torvalds 	case H9652:
34921da177e4SLinus Torvalds 		x = status & HDSP_Sync1;
3493b0b98119STakashi Iwai 		if (status & HDSP_Lock1)
34941da177e4SLinus Torvalds 			snd_iprintf(buffer, "ADAT2: %s\n", x ? "Sync" : "Lock");
3495b0b98119STakashi Iwai 		else
34961da177e4SLinus Torvalds 			snd_iprintf(buffer, "ADAT2: No Lock\n");
34971da177e4SLinus Torvalds 		x = status & HDSP_Sync2;
3498b0b98119STakashi Iwai 		if (status & HDSP_Lock2)
34991da177e4SLinus Torvalds 			snd_iprintf(buffer, "ADAT3: %s\n", x ? "Sync" : "Lock");
3500b0b98119STakashi Iwai 		else
35011da177e4SLinus Torvalds 			snd_iprintf(buffer, "ADAT3: No Lock\n");
3502b0b98119STakashi Iwai 		break;
35031da177e4SLinus Torvalds 	default:
35041da177e4SLinus Torvalds 		/* relax */
35051da177e4SLinus Torvalds 		break;
35061da177e4SLinus Torvalds 	}
35071da177e4SLinus Torvalds 
35081da177e4SLinus Torvalds 	x = status & HDSP_SPDIFSync;
3509b0b98119STakashi Iwai 	if (status & HDSP_SPDIFErrorFlag)
35101da177e4SLinus Torvalds 		snd_iprintf (buffer, "SPDIF: No Lock\n");
3511b0b98119STakashi Iwai 	else
35121da177e4SLinus Torvalds 		snd_iprintf (buffer, "SPDIF: %s\n", x ? "Sync" : "Lock");
35131da177e4SLinus Torvalds 
35141da177e4SLinus Torvalds 	x = status2 & HDSP_wc_sync;
3515b0b98119STakashi Iwai 	if (status2 & HDSP_wc_lock)
35161da177e4SLinus Torvalds 		snd_iprintf (buffer, "Word Clock: %s\n", x ? "Sync" : "Lock");
3517b0b98119STakashi Iwai 	else
35181da177e4SLinus Torvalds 		snd_iprintf (buffer, "Word Clock: No Lock\n");
35191da177e4SLinus Torvalds 
35201da177e4SLinus Torvalds 	x = status & HDSP_TimecodeSync;
3521b0b98119STakashi Iwai 	if (status & HDSP_TimecodeLock)
35221da177e4SLinus Torvalds 		snd_iprintf(buffer, "ADAT Sync: %s\n", x ? "Sync" : "Lock");
3523b0b98119STakashi Iwai 	else
35241da177e4SLinus Torvalds 		snd_iprintf(buffer, "ADAT Sync: No Lock\n");
35251da177e4SLinus Torvalds 
35261da177e4SLinus Torvalds 	snd_iprintf(buffer, "\n");
35271da177e4SLinus Torvalds 
35281da177e4SLinus Torvalds 	/* Informations about H9632 specific controls */
35291da177e4SLinus Torvalds 	if (hdsp->io_type == H9632) {
35301da177e4SLinus Torvalds 		char *tmp;
35311da177e4SLinus Torvalds 
35321da177e4SLinus Torvalds 		switch (hdsp_ad_gain(hdsp)) {
35331da177e4SLinus Torvalds 		case 0:
35341da177e4SLinus Torvalds 			tmp = "-10 dBV";
35351da177e4SLinus Torvalds 			break;
35361da177e4SLinus Torvalds 		case 1:
35371da177e4SLinus Torvalds 			tmp = "+4 dBu";
35381da177e4SLinus Torvalds 			break;
35391da177e4SLinus Torvalds 		default:
35401da177e4SLinus Torvalds 			tmp = "Lo Gain";
35411da177e4SLinus Torvalds 			break;
35421da177e4SLinus Torvalds 		}
35431da177e4SLinus Torvalds 		snd_iprintf(buffer, "AD Gain : %s\n", tmp);
35441da177e4SLinus Torvalds 
35451da177e4SLinus Torvalds 		switch (hdsp_da_gain(hdsp)) {
35461da177e4SLinus Torvalds 		case 0:
35471da177e4SLinus Torvalds 			tmp = "Hi Gain";
35481da177e4SLinus Torvalds 			break;
35491da177e4SLinus Torvalds 		case 1:
35501da177e4SLinus Torvalds 			tmp = "+4 dBu";
35511da177e4SLinus Torvalds 			break;
35521da177e4SLinus Torvalds 		default:
35531da177e4SLinus Torvalds 			tmp = "-10 dBV";
35541da177e4SLinus Torvalds 			break;
35551da177e4SLinus Torvalds 		}
35561da177e4SLinus Torvalds 		snd_iprintf(buffer, "DA Gain : %s\n", tmp);
35571da177e4SLinus Torvalds 
35581da177e4SLinus Torvalds 		switch (hdsp_phone_gain(hdsp)) {
35591da177e4SLinus Torvalds 		case 0:
35601da177e4SLinus Torvalds 			tmp = "0 dB";
35611da177e4SLinus Torvalds 			break;
35621da177e4SLinus Torvalds 		case 1:
35631da177e4SLinus Torvalds 			tmp = "-6 dB";
35641da177e4SLinus Torvalds 			break;
35651da177e4SLinus Torvalds 		default:
35661da177e4SLinus Torvalds 			tmp = "-12 dB";
35671da177e4SLinus Torvalds 			break;
35681da177e4SLinus Torvalds 		}
35691da177e4SLinus Torvalds 		snd_iprintf(buffer, "Phones Gain : %s\n", tmp);
35701da177e4SLinus Torvalds 
35711da177e4SLinus Torvalds 		snd_iprintf(buffer, "XLR Breakout Cable : %s\n", hdsp_xlr_breakout_cable(hdsp) ? "yes" : "no");
35721da177e4SLinus Torvalds 
3573b0b98119STakashi Iwai 		if (hdsp->control_register & HDSP_AnalogExtensionBoard)
35741da177e4SLinus Torvalds 			snd_iprintf(buffer, "AEB : on (ADAT1 internal)\n");
3575b0b98119STakashi Iwai 		else
35761da177e4SLinus Torvalds 			snd_iprintf(buffer, "AEB : off (ADAT1 external)\n");
35771da177e4SLinus Torvalds 		snd_iprintf(buffer, "\n");
35781da177e4SLinus Torvalds 	}
35791da177e4SLinus Torvalds 
35801da177e4SLinus Torvalds }
35811da177e4SLinus Torvalds 
35821374f8ceSRandy Dunlap static void snd_hdsp_proc_init(struct hdsp *hdsp)
35831da177e4SLinus Torvalds {
358455e957d8STakashi Iwai 	struct snd_info_entry *entry;
35851da177e4SLinus Torvalds 
35861da177e4SLinus Torvalds 	if (! snd_card_proc_new(hdsp->card, "hdsp", &entry))
3587bf850204STakashi Iwai 		snd_info_set_text_ops(entry, hdsp, snd_hdsp_proc_read);
35881da177e4SLinus Torvalds }
35891da177e4SLinus Torvalds 
359055e957d8STakashi Iwai static void snd_hdsp_free_buffers(struct hdsp *hdsp)
35911da177e4SLinus Torvalds {
35921da177e4SLinus Torvalds 	snd_hammerfall_free_buffer(&hdsp->capture_dma_buf, hdsp->pci);
35931da177e4SLinus Torvalds 	snd_hammerfall_free_buffer(&hdsp->playback_dma_buf, hdsp->pci);
35941da177e4SLinus Torvalds }
35951da177e4SLinus Torvalds 
359655e957d8STakashi Iwai static int __devinit snd_hdsp_initialize_memory(struct hdsp *hdsp)
35971da177e4SLinus Torvalds {
35981da177e4SLinus Torvalds 	unsigned long pb_bus, cb_bus;
35991da177e4SLinus Torvalds 
36001da177e4SLinus Torvalds 	if (snd_hammerfall_get_buffer(hdsp->pci, &hdsp->capture_dma_buf, HDSP_DMA_AREA_BYTES) < 0 ||
36011da177e4SLinus Torvalds 	    snd_hammerfall_get_buffer(hdsp->pci, &hdsp->playback_dma_buf, HDSP_DMA_AREA_BYTES) < 0) {
36021da177e4SLinus Torvalds 		if (hdsp->capture_dma_buf.area)
36031da177e4SLinus Torvalds 			snd_dma_free_pages(&hdsp->capture_dma_buf);
36041da177e4SLinus Torvalds 		printk(KERN_ERR "%s: no buffers available\n", hdsp->card_name);
36051da177e4SLinus Torvalds 		return -ENOMEM;
36061da177e4SLinus Torvalds 	}
36071da177e4SLinus Torvalds 
36081da177e4SLinus Torvalds 	/* Align to bus-space 64K boundary */
36091da177e4SLinus Torvalds 
36107ab39926SClemens Ladisch 	cb_bus = ALIGN(hdsp->capture_dma_buf.addr, 0x10000ul);
36117ab39926SClemens Ladisch 	pb_bus = ALIGN(hdsp->playback_dma_buf.addr, 0x10000ul);
36121da177e4SLinus Torvalds 
36131da177e4SLinus Torvalds 	/* Tell the card where it is */
36141da177e4SLinus Torvalds 
36151da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_inputBufferAddress, cb_bus);
36161da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_outputBufferAddress, pb_bus);
36171da177e4SLinus Torvalds 
36181da177e4SLinus Torvalds 	hdsp->capture_buffer = hdsp->capture_dma_buf.area + (cb_bus - hdsp->capture_dma_buf.addr);
36191da177e4SLinus Torvalds 	hdsp->playback_buffer = hdsp->playback_dma_buf.area + (pb_bus - hdsp->playback_dma_buf.addr);
36201da177e4SLinus Torvalds 
36211da177e4SLinus Torvalds 	return 0;
36221da177e4SLinus Torvalds }
36231da177e4SLinus Torvalds 
362455e957d8STakashi Iwai static int snd_hdsp_set_defaults(struct hdsp *hdsp)
36251da177e4SLinus Torvalds {
36261da177e4SLinus Torvalds 	unsigned int i;
36271da177e4SLinus Torvalds 
36281da177e4SLinus Torvalds 	/* ASSUMPTION: hdsp->lock is either held, or
36291da177e4SLinus Torvalds 	   there is no need to hold it (e.g. during module
3630561de31aSJoe Perches 	   initialization).
36311da177e4SLinus Torvalds 	 */
36321da177e4SLinus Torvalds 
36331da177e4SLinus Torvalds 	/* set defaults:
36341da177e4SLinus Torvalds 
36351da177e4SLinus Torvalds 	   SPDIF Input via Coax
36361da177e4SLinus Torvalds 	   Master clock mode
36371da177e4SLinus Torvalds 	   maximum latency (7 => 2^7 = 8192 samples, 64Kbyte buffer,
36381da177e4SLinus Torvalds 	                    which implies 2 4096 sample, 32Kbyte periods).
36391da177e4SLinus Torvalds            Enable line out.
36401da177e4SLinus Torvalds 	 */
36411da177e4SLinus Torvalds 
36421da177e4SLinus Torvalds 	hdsp->control_register = HDSP_ClockModeMaster |
36431da177e4SLinus Torvalds 		                 HDSP_SPDIFInputCoaxial |
36441da177e4SLinus Torvalds 		                 hdsp_encode_latency(7) |
36451da177e4SLinus Torvalds 		                 HDSP_LineOut;
36461da177e4SLinus Torvalds 
36471da177e4SLinus Torvalds 
36481da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
36491da177e4SLinus Torvalds 
36501da177e4SLinus Torvalds #ifdef SNDRV_BIG_ENDIAN
36511da177e4SLinus Torvalds 	hdsp->control2_register = HDSP_BIGENDIAN_MODE;
36521da177e4SLinus Torvalds #else
36531da177e4SLinus Torvalds 	hdsp->control2_register = 0;
36541da177e4SLinus Torvalds #endif
3655b0b98119STakashi Iwai 	if (hdsp->io_type == H9652)
36561da177e4SLinus Torvalds 	        snd_hdsp_9652_enable_mixer (hdsp);
3657b0b98119STakashi Iwai 	else
36581da177e4SLinus Torvalds 		hdsp_write (hdsp, HDSP_control2Reg, hdsp->control2_register);
36591da177e4SLinus Torvalds 
36601da177e4SLinus Torvalds 	hdsp_reset_hw_pointer(hdsp);
36611da177e4SLinus Torvalds 	hdsp_compute_period_size(hdsp);
36621da177e4SLinus Torvalds 
36631da177e4SLinus Torvalds 	/* silence everything */
36641da177e4SLinus Torvalds 
3665b0b98119STakashi Iwai 	for (i = 0; i < HDSP_MATRIX_MIXER_SIZE; ++i)
36661da177e4SLinus Torvalds 		hdsp->mixer_matrix[i] = MINUS_INFINITY_GAIN;
36671da177e4SLinus Torvalds 
36681da177e4SLinus Torvalds 	for (i = 0; i < ((hdsp->io_type == H9652 || hdsp->io_type == H9632) ? 1352 : HDSP_MATRIX_MIXER_SIZE); ++i) {
3669b0b98119STakashi Iwai 		if (hdsp_write_gain (hdsp, i, MINUS_INFINITY_GAIN))
36701da177e4SLinus Torvalds 			return -EIO;
36711da177e4SLinus Torvalds 	}
36721da177e4SLinus Torvalds 
36731da177e4SLinus Torvalds 	/* H9632 specific defaults */
36741da177e4SLinus Torvalds 	if (hdsp->io_type == H9632) {
36751da177e4SLinus Torvalds 		hdsp->control_register |= (HDSP_DAGainPlus4dBu | HDSP_ADGainPlus4dBu | HDSP_PhoneGain0dB);
36761da177e4SLinus Torvalds 		hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
36771da177e4SLinus Torvalds 	}
36781da177e4SLinus Torvalds 
36791da177e4SLinus Torvalds 	/* set a default rate so that the channel map is set up.
36801da177e4SLinus Torvalds 	 */
36811da177e4SLinus Torvalds 
36821da177e4SLinus Torvalds 	hdsp_set_rate(hdsp, 48000, 1);
36831da177e4SLinus Torvalds 
36841da177e4SLinus Torvalds 	return 0;
36851da177e4SLinus Torvalds }
36861da177e4SLinus Torvalds 
36871da177e4SLinus Torvalds static void hdsp_midi_tasklet(unsigned long arg)
36881da177e4SLinus Torvalds {
368955e957d8STakashi Iwai 	struct hdsp *hdsp = (struct hdsp *)arg;
36901da177e4SLinus Torvalds 
3691b0b98119STakashi Iwai 	if (hdsp->midi[0].pending)
36921da177e4SLinus Torvalds 		snd_hdsp_midi_input_read (&hdsp->midi[0]);
3693b0b98119STakashi Iwai 	if (hdsp->midi[1].pending)
36941da177e4SLinus Torvalds 		snd_hdsp_midi_input_read (&hdsp->midi[1]);
36951da177e4SLinus Torvalds }
36961da177e4SLinus Torvalds 
36977d12e780SDavid Howells static irqreturn_t snd_hdsp_interrupt(int irq, void *dev_id)
36981da177e4SLinus Torvalds {
369955e957d8STakashi Iwai 	struct hdsp *hdsp = (struct hdsp *) dev_id;
37001da177e4SLinus Torvalds 	unsigned int status;
37011da177e4SLinus Torvalds 	int audio;
37021da177e4SLinus Torvalds 	int midi0;
37031da177e4SLinus Torvalds 	int midi1;
37041da177e4SLinus Torvalds 	unsigned int midi0status;
37051da177e4SLinus Torvalds 	unsigned int midi1status;
37061da177e4SLinus Torvalds 	int schedule = 0;
37071da177e4SLinus Torvalds 
37081da177e4SLinus Torvalds 	status = hdsp_read(hdsp, HDSP_statusRegister);
37091da177e4SLinus Torvalds 
37101da177e4SLinus Torvalds 	audio = status & HDSP_audioIRQPending;
37111da177e4SLinus Torvalds 	midi0 = status & HDSP_midi0IRQPending;
37121da177e4SLinus Torvalds 	midi1 = status & HDSP_midi1IRQPending;
37131da177e4SLinus Torvalds 
3714b0b98119STakashi Iwai 	if (!audio && !midi0 && !midi1)
37151da177e4SLinus Torvalds 		return IRQ_NONE;
37161da177e4SLinus Torvalds 
37171da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_interruptConfirmation, 0);
37181da177e4SLinus Torvalds 
37191da177e4SLinus Torvalds 	midi0status = hdsp_read (hdsp, HDSP_midiStatusIn0) & 0xff;
37201da177e4SLinus Torvalds 	midi1status = hdsp_read (hdsp, HDSP_midiStatusIn1) & 0xff;
37211da177e4SLinus Torvalds 
37221da177e4SLinus Torvalds 	if (audio) {
3723b0b98119STakashi Iwai 		if (hdsp->capture_substream)
37241da177e4SLinus Torvalds 			snd_pcm_period_elapsed(hdsp->pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream);
37251da177e4SLinus Torvalds 
3726b0b98119STakashi Iwai 		if (hdsp->playback_substream)
37271da177e4SLinus Torvalds 			snd_pcm_period_elapsed(hdsp->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream);
37281da177e4SLinus Torvalds 	}
37291da177e4SLinus Torvalds 
37301da177e4SLinus Torvalds 	if (midi0 && midi0status) {
37311da177e4SLinus Torvalds 		if (hdsp->use_midi_tasklet) {
37321da177e4SLinus Torvalds 			/* we disable interrupts for this input until processing is done */
37331da177e4SLinus Torvalds 			hdsp->control_register &= ~HDSP_Midi0InterruptEnable;
37341da177e4SLinus Torvalds 			hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
37351da177e4SLinus Torvalds 			hdsp->midi[0].pending = 1;
37361da177e4SLinus Torvalds 			schedule = 1;
37371da177e4SLinus Torvalds 		} else {
37381da177e4SLinus Torvalds 			snd_hdsp_midi_input_read (&hdsp->midi[0]);
37391da177e4SLinus Torvalds 		}
37401da177e4SLinus Torvalds 	}
37411da177e4SLinus Torvalds 	if (hdsp->io_type != Multiface && hdsp->io_type != H9632 && midi1 && midi1status) {
37421da177e4SLinus Torvalds 		if (hdsp->use_midi_tasklet) {
37431da177e4SLinus Torvalds 			/* we disable interrupts for this input until processing is done */
37441da177e4SLinus Torvalds 			hdsp->control_register &= ~HDSP_Midi1InterruptEnable;
37451da177e4SLinus Torvalds 			hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
37461da177e4SLinus Torvalds 			hdsp->midi[1].pending = 1;
37471da177e4SLinus Torvalds 			schedule = 1;
37481da177e4SLinus Torvalds 		} else {
37491da177e4SLinus Torvalds 			snd_hdsp_midi_input_read (&hdsp->midi[1]);
37501da177e4SLinus Torvalds 		}
37511da177e4SLinus Torvalds 	}
37521da177e4SLinus Torvalds 	if (hdsp->use_midi_tasklet && schedule)
37531f04128aSTakashi Iwai 		tasklet_schedule(&hdsp->midi_tasklet);
37541da177e4SLinus Torvalds 	return IRQ_HANDLED;
37551da177e4SLinus Torvalds }
37561da177e4SLinus Torvalds 
375755e957d8STakashi Iwai static snd_pcm_uframes_t snd_hdsp_hw_pointer(struct snd_pcm_substream *substream)
37581da177e4SLinus Torvalds {
375955e957d8STakashi Iwai 	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
37601da177e4SLinus Torvalds 	return hdsp_hw_pointer(hdsp);
37611da177e4SLinus Torvalds }
37621da177e4SLinus Torvalds 
376355e957d8STakashi Iwai static char *hdsp_channel_buffer_location(struct hdsp *hdsp,
37641da177e4SLinus Torvalds 					     int stream,
37651da177e4SLinus Torvalds 					     int channel)
37661da177e4SLinus Torvalds 
37671da177e4SLinus Torvalds {
37681da177e4SLinus Torvalds 	int mapped_channel;
37691da177e4SLinus Torvalds 
3770da3cec35STakashi Iwai         if (snd_BUG_ON(channel < 0 || channel >= hdsp->max_channels))
3771da3cec35STakashi Iwai 		return NULL;
37721da177e4SLinus Torvalds 
3773b0b98119STakashi Iwai 	if ((mapped_channel = hdsp->channel_map[channel]) < 0)
37741da177e4SLinus Torvalds 		return NULL;
37751da177e4SLinus Torvalds 
3776b0b98119STakashi Iwai 	if (stream == SNDRV_PCM_STREAM_CAPTURE)
37771da177e4SLinus Torvalds 		return hdsp->capture_buffer + (mapped_channel * HDSP_CHANNEL_BUFFER_BYTES);
3778b0b98119STakashi Iwai 	else
37791da177e4SLinus Torvalds 		return hdsp->playback_buffer + (mapped_channel * HDSP_CHANNEL_BUFFER_BYTES);
37801da177e4SLinus Torvalds }
37811da177e4SLinus Torvalds 
378255e957d8STakashi Iwai static int snd_hdsp_playback_copy(struct snd_pcm_substream *substream, int channel,
37831da177e4SLinus Torvalds 				  snd_pcm_uframes_t pos, void __user *src, snd_pcm_uframes_t count)
37841da177e4SLinus Torvalds {
378555e957d8STakashi Iwai 	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
37861da177e4SLinus Torvalds 	char *channel_buf;
37871da177e4SLinus Torvalds 
3788da3cec35STakashi Iwai 	if (snd_BUG_ON(pos + count > HDSP_CHANNEL_BUFFER_BYTES / 4))
3789da3cec35STakashi Iwai 		return -EINVAL;
37901da177e4SLinus Torvalds 
37911da177e4SLinus Torvalds 	channel_buf = hdsp_channel_buffer_location (hdsp, substream->pstr->stream, channel);
3792da3cec35STakashi Iwai 	if (snd_BUG_ON(!channel_buf))
3793da3cec35STakashi Iwai 		return -EIO;
37941da177e4SLinus Torvalds 	if (copy_from_user(channel_buf + pos * 4, src, count * 4))
37951da177e4SLinus Torvalds 		return -EFAULT;
37961da177e4SLinus Torvalds 	return count;
37971da177e4SLinus Torvalds }
37981da177e4SLinus Torvalds 
379955e957d8STakashi Iwai static int snd_hdsp_capture_copy(struct snd_pcm_substream *substream, int channel,
38001da177e4SLinus Torvalds 				 snd_pcm_uframes_t pos, void __user *dst, snd_pcm_uframes_t count)
38011da177e4SLinus Torvalds {
380255e957d8STakashi Iwai 	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
38031da177e4SLinus Torvalds 	char *channel_buf;
38041da177e4SLinus Torvalds 
3805da3cec35STakashi Iwai 	if (snd_BUG_ON(pos + count > HDSP_CHANNEL_BUFFER_BYTES / 4))
3806da3cec35STakashi Iwai 		return -EINVAL;
38071da177e4SLinus Torvalds 
38081da177e4SLinus Torvalds 	channel_buf = hdsp_channel_buffer_location (hdsp, substream->pstr->stream, channel);
3809da3cec35STakashi Iwai 	if (snd_BUG_ON(!channel_buf))
3810da3cec35STakashi Iwai 		return -EIO;
38111da177e4SLinus Torvalds 	if (copy_to_user(dst, channel_buf + pos * 4, count * 4))
38121da177e4SLinus Torvalds 		return -EFAULT;
38131da177e4SLinus Torvalds 	return count;
38141da177e4SLinus Torvalds }
38151da177e4SLinus Torvalds 
381655e957d8STakashi Iwai static int snd_hdsp_hw_silence(struct snd_pcm_substream *substream, int channel,
38171da177e4SLinus Torvalds 				  snd_pcm_uframes_t pos, snd_pcm_uframes_t count)
38181da177e4SLinus Torvalds {
381955e957d8STakashi Iwai 	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
38201da177e4SLinus Torvalds 	char *channel_buf;
38211da177e4SLinus Torvalds 
38221da177e4SLinus Torvalds 	channel_buf = hdsp_channel_buffer_location (hdsp, substream->pstr->stream, channel);
3823da3cec35STakashi Iwai 	if (snd_BUG_ON(!channel_buf))
3824da3cec35STakashi Iwai 		return -EIO;
38251da177e4SLinus Torvalds 	memset(channel_buf + pos * 4, 0, count * 4);
38261da177e4SLinus Torvalds 	return count;
38271da177e4SLinus Torvalds }
38281da177e4SLinus Torvalds 
382955e957d8STakashi Iwai static int snd_hdsp_reset(struct snd_pcm_substream *substream)
38301da177e4SLinus Torvalds {
383155e957d8STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
383255e957d8STakashi Iwai 	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
383355e957d8STakashi Iwai 	struct snd_pcm_substream *other;
38341da177e4SLinus Torvalds 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
38351da177e4SLinus Torvalds 		other = hdsp->capture_substream;
38361da177e4SLinus Torvalds 	else
38371da177e4SLinus Torvalds 		other = hdsp->playback_substream;
38381da177e4SLinus Torvalds 	if (hdsp->running)
38391da177e4SLinus Torvalds 		runtime->status->hw_ptr = hdsp_hw_pointer(hdsp);
38401da177e4SLinus Torvalds 	else
38411da177e4SLinus Torvalds 		runtime->status->hw_ptr = 0;
38421da177e4SLinus Torvalds 	if (other) {
384355e957d8STakashi Iwai 		struct snd_pcm_substream *s;
384455e957d8STakashi Iwai 		struct snd_pcm_runtime *oruntime = other->runtime;
3845ef991b95STakashi Iwai 		snd_pcm_group_for_each_entry(s, substream) {
38461da177e4SLinus Torvalds 			if (s == other) {
38471da177e4SLinus Torvalds 				oruntime->status->hw_ptr = runtime->status->hw_ptr;
38481da177e4SLinus Torvalds 				break;
38491da177e4SLinus Torvalds 			}
38501da177e4SLinus Torvalds 		}
38511da177e4SLinus Torvalds 	}
38521da177e4SLinus Torvalds 	return 0;
38531da177e4SLinus Torvalds }
38541da177e4SLinus Torvalds 
385555e957d8STakashi Iwai static int snd_hdsp_hw_params(struct snd_pcm_substream *substream,
385655e957d8STakashi Iwai 				 struct snd_pcm_hw_params *params)
38571da177e4SLinus Torvalds {
385855e957d8STakashi Iwai 	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
38591da177e4SLinus Torvalds 	int err;
38601da177e4SLinus Torvalds 	pid_t this_pid;
38611da177e4SLinus Torvalds 	pid_t other_pid;
38621da177e4SLinus Torvalds 
3863b0b98119STakashi Iwai 	if (hdsp_check_for_iobox (hdsp))
38641da177e4SLinus Torvalds 		return -EIO;
38651da177e4SLinus Torvalds 
3866b0b98119STakashi Iwai 	if (hdsp_check_for_firmware(hdsp, 1))
38671da177e4SLinus Torvalds 		return -EIO;
38681da177e4SLinus Torvalds 
38691da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
38701da177e4SLinus Torvalds 
38711da177e4SLinus Torvalds 	if (substream->pstr->stream == SNDRV_PCM_STREAM_PLAYBACK) {
38721da177e4SLinus Torvalds 		hdsp->control_register &= ~(HDSP_SPDIFProfessional | HDSP_SPDIFNonAudio | HDSP_SPDIFEmphasis);
38731da177e4SLinus Torvalds 		hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register |= hdsp->creg_spdif_stream);
38741da177e4SLinus Torvalds 		this_pid = hdsp->playback_pid;
38751da177e4SLinus Torvalds 		other_pid = hdsp->capture_pid;
38761da177e4SLinus Torvalds 	} else {
38771da177e4SLinus Torvalds 		this_pid = hdsp->capture_pid;
38781da177e4SLinus Torvalds 		other_pid = hdsp->playback_pid;
38791da177e4SLinus Torvalds 	}
38801da177e4SLinus Torvalds 
38811da177e4SLinus Torvalds 	if ((other_pid > 0) && (this_pid != other_pid)) {
38821da177e4SLinus Torvalds 
38831da177e4SLinus Torvalds 		/* The other stream is open, and not by the same
38841da177e4SLinus Torvalds 		   task as this one. Make sure that the parameters
38851da177e4SLinus Torvalds 		   that matter are the same.
38861da177e4SLinus Torvalds 		 */
38871da177e4SLinus Torvalds 
38881da177e4SLinus Torvalds 		if (params_rate(params) != hdsp->system_sample_rate) {
38891da177e4SLinus Torvalds 			spin_unlock_irq(&hdsp->lock);
38901da177e4SLinus Torvalds 			_snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_RATE);
38911da177e4SLinus Torvalds 			return -EBUSY;
38921da177e4SLinus Torvalds 		}
38931da177e4SLinus Torvalds 
38941da177e4SLinus Torvalds 		if (params_period_size(params) != hdsp->period_bytes / 4) {
38951da177e4SLinus Torvalds 			spin_unlock_irq(&hdsp->lock);
38961da177e4SLinus Torvalds 			_snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
38971da177e4SLinus Torvalds 			return -EBUSY;
38981da177e4SLinus Torvalds 		}
38991da177e4SLinus Torvalds 
39001da177e4SLinus Torvalds 		/* We're fine. */
39011da177e4SLinus Torvalds 
39021da177e4SLinus Torvalds 		spin_unlock_irq(&hdsp->lock);
39031da177e4SLinus Torvalds  		return 0;
39041da177e4SLinus Torvalds 
39051da177e4SLinus Torvalds 	} else {
39061da177e4SLinus Torvalds 		spin_unlock_irq(&hdsp->lock);
39071da177e4SLinus Torvalds 	}
39081da177e4SLinus Torvalds 
39091da177e4SLinus Torvalds 	/* how to make sure that the rate matches an externally-set one ?
39101da177e4SLinus Torvalds 	 */
39111da177e4SLinus Torvalds 
39121da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
3913e3ea4d89STakashi Iwai 	if (! hdsp->clock_source_locked) {
39141da177e4SLinus Torvalds 		if ((err = hdsp_set_rate(hdsp, params_rate(params), 0)) < 0) {
39151da177e4SLinus Torvalds 			spin_unlock_irq(&hdsp->lock);
39161da177e4SLinus Torvalds 			_snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_RATE);
39171da177e4SLinus Torvalds 			return err;
39181da177e4SLinus Torvalds 		}
3919e3ea4d89STakashi Iwai 	}
3920e3ea4d89STakashi Iwai 	spin_unlock_irq(&hdsp->lock);
39211da177e4SLinus Torvalds 
39221da177e4SLinus Torvalds 	if ((err = hdsp_set_interrupt_interval(hdsp, params_period_size(params))) < 0) {
39231da177e4SLinus Torvalds 		_snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
39241da177e4SLinus Torvalds 		return err;
39251da177e4SLinus Torvalds 	}
39261da177e4SLinus Torvalds 
39271da177e4SLinus Torvalds 	return 0;
39281da177e4SLinus Torvalds }
39291da177e4SLinus Torvalds 
393055e957d8STakashi Iwai static int snd_hdsp_channel_info(struct snd_pcm_substream *substream,
393155e957d8STakashi Iwai 				    struct snd_pcm_channel_info *info)
39321da177e4SLinus Torvalds {
393355e957d8STakashi Iwai 	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
39341da177e4SLinus Torvalds 	int mapped_channel;
39351da177e4SLinus Torvalds 
3936da3cec35STakashi Iwai 	if (snd_BUG_ON(info->channel >= hdsp->max_channels))
3937da3cec35STakashi Iwai 		return -EINVAL;
39381da177e4SLinus Torvalds 
3939b0b98119STakashi Iwai 	if ((mapped_channel = hdsp->channel_map[info->channel]) < 0)
39401da177e4SLinus Torvalds 		return -EINVAL;
39411da177e4SLinus Torvalds 
39421da177e4SLinus Torvalds 	info->offset = mapped_channel * HDSP_CHANNEL_BUFFER_BYTES;
39431da177e4SLinus Torvalds 	info->first = 0;
39441da177e4SLinus Torvalds 	info->step = 32;
39451da177e4SLinus Torvalds 	return 0;
39461da177e4SLinus Torvalds }
39471da177e4SLinus Torvalds 
394855e957d8STakashi Iwai static int snd_hdsp_ioctl(struct snd_pcm_substream *substream,
39491da177e4SLinus Torvalds 			     unsigned int cmd, void *arg)
39501da177e4SLinus Torvalds {
39511da177e4SLinus Torvalds 	switch (cmd) {
39521da177e4SLinus Torvalds 	case SNDRV_PCM_IOCTL1_RESET:
39531da177e4SLinus Torvalds 		return snd_hdsp_reset(substream);
39541da177e4SLinus Torvalds 	case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
3955b0b98119STakashi Iwai 		return snd_hdsp_channel_info(substream, arg);
39561da177e4SLinus Torvalds 	default:
39571da177e4SLinus Torvalds 		break;
39581da177e4SLinus Torvalds 	}
39591da177e4SLinus Torvalds 
39601da177e4SLinus Torvalds 	return snd_pcm_lib_ioctl(substream, cmd, arg);
39611da177e4SLinus Torvalds }
39621da177e4SLinus Torvalds 
396355e957d8STakashi Iwai static int snd_hdsp_trigger(struct snd_pcm_substream *substream, int cmd)
39641da177e4SLinus Torvalds {
396555e957d8STakashi Iwai 	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
396655e957d8STakashi Iwai 	struct snd_pcm_substream *other;
39671da177e4SLinus Torvalds 	int running;
39681da177e4SLinus Torvalds 
3969b0b98119STakashi Iwai 	if (hdsp_check_for_iobox (hdsp))
39701da177e4SLinus Torvalds 		return -EIO;
39711da177e4SLinus Torvalds 
3972311e70a4STakashi Iwai 	if (hdsp_check_for_firmware(hdsp, 0)) /* no auto-loading in trigger */
39731da177e4SLinus Torvalds 		return -EIO;
39741da177e4SLinus Torvalds 
39751da177e4SLinus Torvalds 	spin_lock(&hdsp->lock);
39761da177e4SLinus Torvalds 	running = hdsp->running;
39771da177e4SLinus Torvalds 	switch (cmd) {
39781da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_START:
39791da177e4SLinus Torvalds 		running |= 1 << substream->stream;
39801da177e4SLinus Torvalds 		break;
39811da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_STOP:
39821da177e4SLinus Torvalds 		running &= ~(1 << substream->stream);
39831da177e4SLinus Torvalds 		break;
39841da177e4SLinus Torvalds 	default:
39851da177e4SLinus Torvalds 		snd_BUG();
39861da177e4SLinus Torvalds 		spin_unlock(&hdsp->lock);
39871da177e4SLinus Torvalds 		return -EINVAL;
39881da177e4SLinus Torvalds 	}
39891da177e4SLinus Torvalds 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
39901da177e4SLinus Torvalds 		other = hdsp->capture_substream;
39911da177e4SLinus Torvalds 	else
39921da177e4SLinus Torvalds 		other = hdsp->playback_substream;
39931da177e4SLinus Torvalds 
39941da177e4SLinus Torvalds 	if (other) {
399555e957d8STakashi Iwai 		struct snd_pcm_substream *s;
3996ef991b95STakashi Iwai 		snd_pcm_group_for_each_entry(s, substream) {
39971da177e4SLinus Torvalds 			if (s == other) {
39981da177e4SLinus Torvalds 				snd_pcm_trigger_done(s, substream);
39991da177e4SLinus Torvalds 				if (cmd == SNDRV_PCM_TRIGGER_START)
40001da177e4SLinus Torvalds 					running |= 1 << s->stream;
40011da177e4SLinus Torvalds 				else
40021da177e4SLinus Torvalds 					running &= ~(1 << s->stream);
40031da177e4SLinus Torvalds 				goto _ok;
40041da177e4SLinus Torvalds 			}
40051da177e4SLinus Torvalds 		}
40061da177e4SLinus Torvalds 		if (cmd == SNDRV_PCM_TRIGGER_START) {
40071da177e4SLinus Torvalds 			if (!(running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) &&
40081da177e4SLinus Torvalds 			    substream->stream == SNDRV_PCM_STREAM_CAPTURE)
40091da177e4SLinus Torvalds 				hdsp_silence_playback(hdsp);
40101da177e4SLinus Torvalds 		} else {
40111da177e4SLinus Torvalds 			if (running &&
40121da177e4SLinus Torvalds 			    substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
40131da177e4SLinus Torvalds 				hdsp_silence_playback(hdsp);
40141da177e4SLinus Torvalds 		}
40151da177e4SLinus Torvalds 	} else {
40161da177e4SLinus Torvalds 		if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
40171da177e4SLinus Torvalds 				hdsp_silence_playback(hdsp);
40181da177e4SLinus Torvalds 	}
40191da177e4SLinus Torvalds  _ok:
40201da177e4SLinus Torvalds 	snd_pcm_trigger_done(substream, substream);
40211da177e4SLinus Torvalds 	if (!hdsp->running && running)
40221da177e4SLinus Torvalds 		hdsp_start_audio(hdsp);
40231da177e4SLinus Torvalds 	else if (hdsp->running && !running)
40241da177e4SLinus Torvalds 		hdsp_stop_audio(hdsp);
40251da177e4SLinus Torvalds 	hdsp->running = running;
40261da177e4SLinus Torvalds 	spin_unlock(&hdsp->lock);
40271da177e4SLinus Torvalds 
40281da177e4SLinus Torvalds 	return 0;
40291da177e4SLinus Torvalds }
40301da177e4SLinus Torvalds 
403155e957d8STakashi Iwai static int snd_hdsp_prepare(struct snd_pcm_substream *substream)
40321da177e4SLinus Torvalds {
403355e957d8STakashi Iwai 	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
40341da177e4SLinus Torvalds 	int result = 0;
40351da177e4SLinus Torvalds 
4036b0b98119STakashi Iwai 	if (hdsp_check_for_iobox (hdsp))
40371da177e4SLinus Torvalds 		return -EIO;
40381da177e4SLinus Torvalds 
4039b0b98119STakashi Iwai 	if (hdsp_check_for_firmware(hdsp, 1))
40401da177e4SLinus Torvalds 		return -EIO;
40411da177e4SLinus Torvalds 
40421da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
40431da177e4SLinus Torvalds 	if (!hdsp->running)
40441da177e4SLinus Torvalds 		hdsp_reset_hw_pointer(hdsp);
40451da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
40461da177e4SLinus Torvalds 	return result;
40471da177e4SLinus Torvalds }
40481da177e4SLinus Torvalds 
404955e957d8STakashi Iwai static struct snd_pcm_hardware snd_hdsp_playback_subinfo =
40501da177e4SLinus Torvalds {
40511da177e4SLinus Torvalds 	.info =			(SNDRV_PCM_INFO_MMAP |
40521da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_MMAP_VALID |
40531da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_NONINTERLEAVED |
40541da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_SYNC_START |
40551da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_DOUBLE),
40561da177e4SLinus Torvalds #ifdef SNDRV_BIG_ENDIAN
40571da177e4SLinus Torvalds 	.formats =		SNDRV_PCM_FMTBIT_S32_BE,
40581da177e4SLinus Torvalds #else
40591da177e4SLinus Torvalds 	.formats =		SNDRV_PCM_FMTBIT_S32_LE,
40601da177e4SLinus Torvalds #endif
40611da177e4SLinus Torvalds 	.rates =		(SNDRV_PCM_RATE_32000 |
40621da177e4SLinus Torvalds 				 SNDRV_PCM_RATE_44100 |
40631da177e4SLinus Torvalds 				 SNDRV_PCM_RATE_48000 |
40641da177e4SLinus Torvalds 				 SNDRV_PCM_RATE_64000 |
40651da177e4SLinus Torvalds 				 SNDRV_PCM_RATE_88200 |
40661da177e4SLinus Torvalds 				 SNDRV_PCM_RATE_96000),
40671da177e4SLinus Torvalds 	.rate_min =		32000,
40681da177e4SLinus Torvalds 	.rate_max =		96000,
40691da177e4SLinus Torvalds 	.channels_min =		14,
40701da177e4SLinus Torvalds 	.channels_max =		HDSP_MAX_CHANNELS,
40711da177e4SLinus Torvalds 	.buffer_bytes_max =	HDSP_CHANNEL_BUFFER_BYTES * HDSP_MAX_CHANNELS,
40721da177e4SLinus Torvalds 	.period_bytes_min =	(64 * 4) * 10,
40731da177e4SLinus Torvalds 	.period_bytes_max =	(8192 * 4) * HDSP_MAX_CHANNELS,
40741da177e4SLinus Torvalds 	.periods_min =		2,
40751da177e4SLinus Torvalds 	.periods_max =		2,
40761da177e4SLinus Torvalds 	.fifo_size =		0
40771da177e4SLinus Torvalds };
40781da177e4SLinus Torvalds 
407955e957d8STakashi Iwai static struct snd_pcm_hardware snd_hdsp_capture_subinfo =
40801da177e4SLinus Torvalds {
40811da177e4SLinus Torvalds 	.info =			(SNDRV_PCM_INFO_MMAP |
40821da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_MMAP_VALID |
40831da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_NONINTERLEAVED |
40841da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_SYNC_START),
40851da177e4SLinus Torvalds #ifdef SNDRV_BIG_ENDIAN
40861da177e4SLinus Torvalds 	.formats =		SNDRV_PCM_FMTBIT_S32_BE,
40871da177e4SLinus Torvalds #else
40881da177e4SLinus Torvalds 	.formats =		SNDRV_PCM_FMTBIT_S32_LE,
40891da177e4SLinus Torvalds #endif
40901da177e4SLinus Torvalds 	.rates =		(SNDRV_PCM_RATE_32000 |
40911da177e4SLinus Torvalds 				 SNDRV_PCM_RATE_44100 |
40921da177e4SLinus Torvalds 				 SNDRV_PCM_RATE_48000 |
40931da177e4SLinus Torvalds 				 SNDRV_PCM_RATE_64000 |
40941da177e4SLinus Torvalds 				 SNDRV_PCM_RATE_88200 |
40951da177e4SLinus Torvalds 				 SNDRV_PCM_RATE_96000),
40961da177e4SLinus Torvalds 	.rate_min =		32000,
40971da177e4SLinus Torvalds 	.rate_max =		96000,
40981da177e4SLinus Torvalds 	.channels_min =		14,
40991da177e4SLinus Torvalds 	.channels_max =		HDSP_MAX_CHANNELS,
41001da177e4SLinus Torvalds 	.buffer_bytes_max =	HDSP_CHANNEL_BUFFER_BYTES * HDSP_MAX_CHANNELS,
41011da177e4SLinus Torvalds 	.period_bytes_min =	(64 * 4) * 10,
41021da177e4SLinus Torvalds 	.period_bytes_max =	(8192 * 4) * HDSP_MAX_CHANNELS,
41031da177e4SLinus Torvalds 	.periods_min =		2,
41041da177e4SLinus Torvalds 	.periods_max =		2,
41051da177e4SLinus Torvalds 	.fifo_size =		0
41061da177e4SLinus Torvalds };
41071da177e4SLinus Torvalds 
41081da177e4SLinus Torvalds static unsigned int hdsp_period_sizes[] = { 64, 128, 256, 512, 1024, 2048, 4096, 8192 };
41091da177e4SLinus Torvalds 
411055e957d8STakashi Iwai static struct snd_pcm_hw_constraint_list hdsp_hw_constraints_period_sizes = {
41111da177e4SLinus Torvalds 	.count = ARRAY_SIZE(hdsp_period_sizes),
41121da177e4SLinus Torvalds 	.list = hdsp_period_sizes,
41131da177e4SLinus Torvalds 	.mask = 0
41141da177e4SLinus Torvalds };
41151da177e4SLinus Torvalds 
41161da177e4SLinus Torvalds static unsigned int hdsp_9632_sample_rates[] = { 32000, 44100, 48000, 64000, 88200, 96000, 128000, 176400, 192000 };
41171da177e4SLinus Torvalds 
411855e957d8STakashi Iwai static struct snd_pcm_hw_constraint_list hdsp_hw_constraints_9632_sample_rates = {
41191da177e4SLinus Torvalds 	.count = ARRAY_SIZE(hdsp_9632_sample_rates),
41201da177e4SLinus Torvalds 	.list = hdsp_9632_sample_rates,
41211da177e4SLinus Torvalds 	.mask = 0
41221da177e4SLinus Torvalds };
41231da177e4SLinus Torvalds 
412455e957d8STakashi Iwai static int snd_hdsp_hw_rule_in_channels(struct snd_pcm_hw_params *params,
412555e957d8STakashi Iwai 					struct snd_pcm_hw_rule *rule)
41261da177e4SLinus Torvalds {
412755e957d8STakashi Iwai 	struct hdsp *hdsp = rule->private;
412855e957d8STakashi Iwai 	struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
41291da177e4SLinus Torvalds 	if (hdsp->io_type == H9632) {
41301da177e4SLinus Torvalds 		unsigned int list[3];
41311da177e4SLinus Torvalds 		list[0] = hdsp->qs_in_channels;
41321da177e4SLinus Torvalds 		list[1] = hdsp->ds_in_channels;
41331da177e4SLinus Torvalds 		list[2] = hdsp->ss_in_channels;
41341da177e4SLinus Torvalds 		return snd_interval_list(c, 3, list, 0);
41351da177e4SLinus Torvalds 	} else {
41361da177e4SLinus Torvalds 		unsigned int list[2];
41371da177e4SLinus Torvalds 		list[0] = hdsp->ds_in_channels;
41381da177e4SLinus Torvalds 		list[1] = hdsp->ss_in_channels;
41391da177e4SLinus Torvalds 		return snd_interval_list(c, 2, list, 0);
41401da177e4SLinus Torvalds 	}
41411da177e4SLinus Torvalds }
41421da177e4SLinus Torvalds 
414355e957d8STakashi Iwai static int snd_hdsp_hw_rule_out_channels(struct snd_pcm_hw_params *params,
414455e957d8STakashi Iwai 					struct snd_pcm_hw_rule *rule)
41451da177e4SLinus Torvalds {
41461da177e4SLinus Torvalds 	unsigned int list[3];
414755e957d8STakashi Iwai 	struct hdsp *hdsp = rule->private;
414855e957d8STakashi Iwai 	struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
41491da177e4SLinus Torvalds 	if (hdsp->io_type == H9632) {
41501da177e4SLinus Torvalds 		list[0] = hdsp->qs_out_channels;
41511da177e4SLinus Torvalds 		list[1] = hdsp->ds_out_channels;
41521da177e4SLinus Torvalds 		list[2] = hdsp->ss_out_channels;
41531da177e4SLinus Torvalds 		return snd_interval_list(c, 3, list, 0);
41541da177e4SLinus Torvalds 	} else {
41551da177e4SLinus Torvalds 		list[0] = hdsp->ds_out_channels;
41561da177e4SLinus Torvalds 		list[1] = hdsp->ss_out_channels;
41571da177e4SLinus Torvalds 	}
41581da177e4SLinus Torvalds 	return snd_interval_list(c, 2, list, 0);
41591da177e4SLinus Torvalds }
41601da177e4SLinus Torvalds 
416155e957d8STakashi Iwai static int snd_hdsp_hw_rule_in_channels_rate(struct snd_pcm_hw_params *params,
416255e957d8STakashi Iwai 					     struct snd_pcm_hw_rule *rule)
41631da177e4SLinus Torvalds {
416455e957d8STakashi Iwai 	struct hdsp *hdsp = rule->private;
416555e957d8STakashi Iwai 	struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
416655e957d8STakashi Iwai 	struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
41671da177e4SLinus Torvalds 	if (r->min > 96000 && hdsp->io_type == H9632) {
416855e957d8STakashi Iwai 		struct snd_interval t = {
41691da177e4SLinus Torvalds 			.min = hdsp->qs_in_channels,
41701da177e4SLinus Torvalds 			.max = hdsp->qs_in_channels,
41711da177e4SLinus Torvalds 			.integer = 1,
41721da177e4SLinus Torvalds 		};
41731da177e4SLinus Torvalds 		return snd_interval_refine(c, &t);
41741da177e4SLinus Torvalds 	} else if (r->min > 48000 && r->max <= 96000) {
417555e957d8STakashi Iwai 		struct snd_interval t = {
41761da177e4SLinus Torvalds 			.min = hdsp->ds_in_channels,
41771da177e4SLinus Torvalds 			.max = hdsp->ds_in_channels,
41781da177e4SLinus Torvalds 			.integer = 1,
41791da177e4SLinus Torvalds 		};
41801da177e4SLinus Torvalds 		return snd_interval_refine(c, &t);
41811da177e4SLinus Torvalds 	} else if (r->max < 64000) {
418255e957d8STakashi Iwai 		struct snd_interval t = {
41831da177e4SLinus Torvalds 			.min = hdsp->ss_in_channels,
41841da177e4SLinus Torvalds 			.max = hdsp->ss_in_channels,
41851da177e4SLinus Torvalds 			.integer = 1,
41861da177e4SLinus Torvalds 		};
41871da177e4SLinus Torvalds 		return snd_interval_refine(c, &t);
41881da177e4SLinus Torvalds 	}
41891da177e4SLinus Torvalds 	return 0;
41901da177e4SLinus Torvalds }
41911da177e4SLinus Torvalds 
419255e957d8STakashi Iwai static int snd_hdsp_hw_rule_out_channels_rate(struct snd_pcm_hw_params *params,
419355e957d8STakashi Iwai 					     struct snd_pcm_hw_rule *rule)
41941da177e4SLinus Torvalds {
419555e957d8STakashi Iwai 	struct hdsp *hdsp = rule->private;
419655e957d8STakashi Iwai 	struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
419755e957d8STakashi Iwai 	struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
41981da177e4SLinus Torvalds 	if (r->min > 96000 && hdsp->io_type == H9632) {
419955e957d8STakashi Iwai 		struct snd_interval t = {
42001da177e4SLinus Torvalds 			.min = hdsp->qs_out_channels,
42011da177e4SLinus Torvalds 			.max = hdsp->qs_out_channels,
42021da177e4SLinus Torvalds 			.integer = 1,
42031da177e4SLinus Torvalds 		};
42041da177e4SLinus Torvalds 		return snd_interval_refine(c, &t);
42051da177e4SLinus Torvalds 	} else if (r->min > 48000 && r->max <= 96000) {
420655e957d8STakashi Iwai 		struct snd_interval t = {
42071da177e4SLinus Torvalds 			.min = hdsp->ds_out_channels,
42081da177e4SLinus Torvalds 			.max = hdsp->ds_out_channels,
42091da177e4SLinus Torvalds 			.integer = 1,
42101da177e4SLinus Torvalds 		};
42111da177e4SLinus Torvalds 		return snd_interval_refine(c, &t);
42121da177e4SLinus Torvalds 	} else if (r->max < 64000) {
421355e957d8STakashi Iwai 		struct snd_interval t = {
42141da177e4SLinus Torvalds 			.min = hdsp->ss_out_channels,
42151da177e4SLinus Torvalds 			.max = hdsp->ss_out_channels,
42161da177e4SLinus Torvalds 			.integer = 1,
42171da177e4SLinus Torvalds 		};
42181da177e4SLinus Torvalds 		return snd_interval_refine(c, &t);
42191da177e4SLinus Torvalds 	}
42201da177e4SLinus Torvalds 	return 0;
42211da177e4SLinus Torvalds }
42221da177e4SLinus Torvalds 
422355e957d8STakashi Iwai static int snd_hdsp_hw_rule_rate_out_channels(struct snd_pcm_hw_params *params,
422455e957d8STakashi Iwai 					     struct snd_pcm_hw_rule *rule)
42251da177e4SLinus Torvalds {
422655e957d8STakashi Iwai 	struct hdsp *hdsp = rule->private;
422755e957d8STakashi Iwai 	struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
422855e957d8STakashi Iwai 	struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
42291da177e4SLinus Torvalds 	if (c->min >= hdsp->ss_out_channels) {
423055e957d8STakashi Iwai 		struct snd_interval t = {
42311da177e4SLinus Torvalds 			.min = 32000,
42321da177e4SLinus Torvalds 			.max = 48000,
42331da177e4SLinus Torvalds 			.integer = 1,
42341da177e4SLinus Torvalds 		};
42351da177e4SLinus Torvalds 		return snd_interval_refine(r, &t);
42361da177e4SLinus Torvalds 	} else if (c->max <= hdsp->qs_out_channels && hdsp->io_type == H9632) {
423755e957d8STakashi Iwai 		struct snd_interval t = {
42381da177e4SLinus Torvalds 			.min = 128000,
42391da177e4SLinus Torvalds 			.max = 192000,
42401da177e4SLinus Torvalds 			.integer = 1,
42411da177e4SLinus Torvalds 		};
42421da177e4SLinus Torvalds 		return snd_interval_refine(r, &t);
42431da177e4SLinus Torvalds 	} else if (c->max <= hdsp->ds_out_channels) {
424455e957d8STakashi Iwai 		struct snd_interval t = {
42451da177e4SLinus Torvalds 			.min = 64000,
42461da177e4SLinus Torvalds 			.max = 96000,
42471da177e4SLinus Torvalds 			.integer = 1,
42481da177e4SLinus Torvalds 		};
42491da177e4SLinus Torvalds 		return snd_interval_refine(r, &t);
42501da177e4SLinus Torvalds 	}
42511da177e4SLinus Torvalds 	return 0;
42521da177e4SLinus Torvalds }
42531da177e4SLinus Torvalds 
425455e957d8STakashi Iwai static int snd_hdsp_hw_rule_rate_in_channels(struct snd_pcm_hw_params *params,
425555e957d8STakashi Iwai 					     struct snd_pcm_hw_rule *rule)
42561da177e4SLinus Torvalds {
425755e957d8STakashi Iwai 	struct hdsp *hdsp = rule->private;
425855e957d8STakashi Iwai 	struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
425955e957d8STakashi Iwai 	struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
42601da177e4SLinus Torvalds 	if (c->min >= hdsp->ss_in_channels) {
426155e957d8STakashi Iwai 		struct snd_interval t = {
42621da177e4SLinus Torvalds 			.min = 32000,
42631da177e4SLinus Torvalds 			.max = 48000,
42641da177e4SLinus Torvalds 			.integer = 1,
42651da177e4SLinus Torvalds 		};
42661da177e4SLinus Torvalds 		return snd_interval_refine(r, &t);
42671da177e4SLinus Torvalds 	} else if (c->max <= hdsp->qs_in_channels && hdsp->io_type == H9632) {
426855e957d8STakashi Iwai 		struct snd_interval t = {
42691da177e4SLinus Torvalds 			.min = 128000,
42701da177e4SLinus Torvalds 			.max = 192000,
42711da177e4SLinus Torvalds 			.integer = 1,
42721da177e4SLinus Torvalds 		};
42731da177e4SLinus Torvalds 		return snd_interval_refine(r, &t);
42741da177e4SLinus Torvalds 	} else if (c->max <= hdsp->ds_in_channels) {
427555e957d8STakashi Iwai 		struct snd_interval t = {
42761da177e4SLinus Torvalds 			.min = 64000,
42771da177e4SLinus Torvalds 			.max = 96000,
42781da177e4SLinus Torvalds 			.integer = 1,
42791da177e4SLinus Torvalds 		};
42801da177e4SLinus Torvalds 		return snd_interval_refine(r, &t);
42811da177e4SLinus Torvalds 	}
42821da177e4SLinus Torvalds 	return 0;
42831da177e4SLinus Torvalds }
42841da177e4SLinus Torvalds 
428555e957d8STakashi Iwai static int snd_hdsp_playback_open(struct snd_pcm_substream *substream)
42861da177e4SLinus Torvalds {
428755e957d8STakashi Iwai 	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
428855e957d8STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
42891da177e4SLinus Torvalds 
4290b0b98119STakashi Iwai 	if (hdsp_check_for_iobox (hdsp))
42911da177e4SLinus Torvalds 		return -EIO;
42921da177e4SLinus Torvalds 
4293b0b98119STakashi Iwai 	if (hdsp_check_for_firmware(hdsp, 1))
42941da177e4SLinus Torvalds 		return -EIO;
42951da177e4SLinus Torvalds 
42961da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
42971da177e4SLinus Torvalds 
42981da177e4SLinus Torvalds 	snd_pcm_set_sync(substream);
42991da177e4SLinus Torvalds 
43001da177e4SLinus Torvalds         runtime->hw = snd_hdsp_playback_subinfo;
43011da177e4SLinus Torvalds 	runtime->dma_area = hdsp->playback_buffer;
43021da177e4SLinus Torvalds 	runtime->dma_bytes = HDSP_DMA_AREA_BYTES;
43031da177e4SLinus Torvalds 
43041da177e4SLinus Torvalds 	hdsp->playback_pid = current->pid;
43051da177e4SLinus Torvalds 	hdsp->playback_substream = substream;
43061da177e4SLinus Torvalds 
43071da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
43081da177e4SLinus Torvalds 
43091da177e4SLinus Torvalds 	snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
43101da177e4SLinus Torvalds 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, &hdsp_hw_constraints_period_sizes);
4311e3ea4d89STakashi Iwai 	if (hdsp->clock_source_locked) {
4312e3ea4d89STakashi Iwai 		runtime->hw.rate_min = runtime->hw.rate_max = hdsp->system_sample_rate;
4313e3ea4d89STakashi Iwai 	} else if (hdsp->io_type == H9632) {
43141da177e4SLinus Torvalds 		runtime->hw.rate_max = 192000;
43151da177e4SLinus Torvalds 		runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
43161da177e4SLinus Torvalds 		snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hdsp_hw_constraints_9632_sample_rates);
43171da177e4SLinus Torvalds 	}
4318e3ea4d89STakashi Iwai 	if (hdsp->io_type == H9632) {
4319e3ea4d89STakashi Iwai 		runtime->hw.channels_min = hdsp->qs_out_channels;
4320e3ea4d89STakashi Iwai 		runtime->hw.channels_max = hdsp->ss_out_channels;
4321e3ea4d89STakashi Iwai 	}
43221da177e4SLinus Torvalds 
43231da177e4SLinus Torvalds 	snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
43241da177e4SLinus Torvalds 			     snd_hdsp_hw_rule_out_channels, hdsp,
43251da177e4SLinus Torvalds 			     SNDRV_PCM_HW_PARAM_CHANNELS, -1);
43261da177e4SLinus Torvalds 	snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
43271da177e4SLinus Torvalds 			     snd_hdsp_hw_rule_out_channels_rate, hdsp,
43281da177e4SLinus Torvalds 			     SNDRV_PCM_HW_PARAM_RATE, -1);
43291da177e4SLinus Torvalds 	snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
43301da177e4SLinus Torvalds 			     snd_hdsp_hw_rule_rate_out_channels, hdsp,
43311da177e4SLinus Torvalds 			     SNDRV_PCM_HW_PARAM_CHANNELS, -1);
43321da177e4SLinus Torvalds 
43331da177e4SLinus Torvalds 	hdsp->creg_spdif_stream = hdsp->creg_spdif;
43341da177e4SLinus Torvalds 	hdsp->spdif_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
43351da177e4SLinus Torvalds 	snd_ctl_notify(hdsp->card, SNDRV_CTL_EVENT_MASK_VALUE |
43361da177e4SLinus Torvalds 		       SNDRV_CTL_EVENT_MASK_INFO, &hdsp->spdif_ctl->id);
43371da177e4SLinus Torvalds 	return 0;
43381da177e4SLinus Torvalds }
43391da177e4SLinus Torvalds 
434055e957d8STakashi Iwai static int snd_hdsp_playback_release(struct snd_pcm_substream *substream)
43411da177e4SLinus Torvalds {
434255e957d8STakashi Iwai 	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
43431da177e4SLinus Torvalds 
43441da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
43451da177e4SLinus Torvalds 
43461da177e4SLinus Torvalds 	hdsp->playback_pid = -1;
43471da177e4SLinus Torvalds 	hdsp->playback_substream = NULL;
43481da177e4SLinus Torvalds 
43491da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
43501da177e4SLinus Torvalds 
43511da177e4SLinus Torvalds 	hdsp->spdif_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
43521da177e4SLinus Torvalds 	snd_ctl_notify(hdsp->card, SNDRV_CTL_EVENT_MASK_VALUE |
43531da177e4SLinus Torvalds 		       SNDRV_CTL_EVENT_MASK_INFO, &hdsp->spdif_ctl->id);
43541da177e4SLinus Torvalds 	return 0;
43551da177e4SLinus Torvalds }
43561da177e4SLinus Torvalds 
43571da177e4SLinus Torvalds 
435855e957d8STakashi Iwai static int snd_hdsp_capture_open(struct snd_pcm_substream *substream)
43591da177e4SLinus Torvalds {
436055e957d8STakashi Iwai 	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
436155e957d8STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
43621da177e4SLinus Torvalds 
4363b0b98119STakashi Iwai 	if (hdsp_check_for_iobox (hdsp))
43641da177e4SLinus Torvalds 		return -EIO;
43651da177e4SLinus Torvalds 
4366b0b98119STakashi Iwai 	if (hdsp_check_for_firmware(hdsp, 1))
43671da177e4SLinus Torvalds 		return -EIO;
43681da177e4SLinus Torvalds 
43691da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
43701da177e4SLinus Torvalds 
43711da177e4SLinus Torvalds 	snd_pcm_set_sync(substream);
43721da177e4SLinus Torvalds 
43731da177e4SLinus Torvalds 	runtime->hw = snd_hdsp_capture_subinfo;
43741da177e4SLinus Torvalds 	runtime->dma_area = hdsp->capture_buffer;
43751da177e4SLinus Torvalds 	runtime->dma_bytes = HDSP_DMA_AREA_BYTES;
43761da177e4SLinus Torvalds 
43771da177e4SLinus Torvalds 	hdsp->capture_pid = current->pid;
43781da177e4SLinus Torvalds 	hdsp->capture_substream = substream;
43791da177e4SLinus Torvalds 
43801da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
43811da177e4SLinus Torvalds 
43821da177e4SLinus Torvalds 	snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
43831da177e4SLinus Torvalds 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, &hdsp_hw_constraints_period_sizes);
43841da177e4SLinus Torvalds 	if (hdsp->io_type == H9632) {
43851da177e4SLinus Torvalds 		runtime->hw.channels_min = hdsp->qs_in_channels;
43861da177e4SLinus Torvalds 		runtime->hw.channels_max = hdsp->ss_in_channels;
43871da177e4SLinus Torvalds 		runtime->hw.rate_max = 192000;
43881da177e4SLinus Torvalds 		runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
43891da177e4SLinus Torvalds 		snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hdsp_hw_constraints_9632_sample_rates);
43901da177e4SLinus Torvalds 	}
43911da177e4SLinus Torvalds 	snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
43921da177e4SLinus Torvalds 			     snd_hdsp_hw_rule_in_channels, hdsp,
43931da177e4SLinus Torvalds 			     SNDRV_PCM_HW_PARAM_CHANNELS, -1);
43941da177e4SLinus Torvalds 	snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
43951da177e4SLinus Torvalds 			     snd_hdsp_hw_rule_in_channels_rate, hdsp,
43961da177e4SLinus Torvalds 			     SNDRV_PCM_HW_PARAM_RATE, -1);
43971da177e4SLinus Torvalds 	snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
43981da177e4SLinus Torvalds 			     snd_hdsp_hw_rule_rate_in_channels, hdsp,
43991da177e4SLinus Torvalds 			     SNDRV_PCM_HW_PARAM_CHANNELS, -1);
44001da177e4SLinus Torvalds 	return 0;
44011da177e4SLinus Torvalds }
44021da177e4SLinus Torvalds 
440355e957d8STakashi Iwai static int snd_hdsp_capture_release(struct snd_pcm_substream *substream)
44041da177e4SLinus Torvalds {
440555e957d8STakashi Iwai 	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
44061da177e4SLinus Torvalds 
44071da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
44081da177e4SLinus Torvalds 
44091da177e4SLinus Torvalds 	hdsp->capture_pid = -1;
44101da177e4SLinus Torvalds 	hdsp->capture_substream = NULL;
44111da177e4SLinus Torvalds 
44121da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
44131da177e4SLinus Torvalds 	return 0;
44141da177e4SLinus Torvalds }
44151da177e4SLinus Torvalds 
441655e957d8STakashi Iwai static int snd_hdsp_hwdep_dummy_op(struct snd_hwdep *hw, struct file *file)
44171da177e4SLinus Torvalds {
44181da177e4SLinus Torvalds 	/* we have nothing to initialize but the call is required */
44191da177e4SLinus Torvalds 	return 0;
44201da177e4SLinus Torvalds }
44211da177e4SLinus Torvalds 
44221da177e4SLinus Torvalds 
44231da177e4SLinus Torvalds /* helper functions for copying meter values */
44241da177e4SLinus Torvalds static inline int copy_u32_le(void __user *dest, void __iomem *src)
44251da177e4SLinus Torvalds {
44261da177e4SLinus Torvalds 	u32 val = readl(src);
44271da177e4SLinus Torvalds 	return copy_to_user(dest, &val, 4);
44281da177e4SLinus Torvalds }
44291da177e4SLinus Torvalds 
44301da177e4SLinus Torvalds static inline int copy_u64_le(void __user *dest, void __iomem *src_low, void __iomem *src_high)
44311da177e4SLinus Torvalds {
44321da177e4SLinus Torvalds 	u32 rms_low, rms_high;
44331da177e4SLinus Torvalds 	u64 rms;
44341da177e4SLinus Torvalds 	rms_low = readl(src_low);
44351da177e4SLinus Torvalds 	rms_high = readl(src_high);
44361da177e4SLinus Torvalds 	rms = ((u64)rms_high << 32) | rms_low;
44371da177e4SLinus Torvalds 	return copy_to_user(dest, &rms, 8);
44381da177e4SLinus Torvalds }
44391da177e4SLinus Torvalds 
44401da177e4SLinus Torvalds static inline int copy_u48_le(void __user *dest, void __iomem *src_low, void __iomem *src_high)
44411da177e4SLinus Torvalds {
44421da177e4SLinus Torvalds 	u32 rms_low, rms_high;
44431da177e4SLinus Torvalds 	u64 rms;
44441da177e4SLinus Torvalds 	rms_low = readl(src_low) & 0xffffff00;
44451da177e4SLinus Torvalds 	rms_high = readl(src_high) & 0xffffff00;
44461da177e4SLinus Torvalds 	rms = ((u64)rms_high << 32) | rms_low;
44471da177e4SLinus Torvalds 	return copy_to_user(dest, &rms, 8);
44481da177e4SLinus Torvalds }
44491da177e4SLinus Torvalds 
445055e957d8STakashi Iwai static int hdsp_9652_get_peak(struct hdsp *hdsp, struct hdsp_peak_rms __user *peak_rms)
44511da177e4SLinus Torvalds {
44521da177e4SLinus Torvalds 	int doublespeed = 0;
44531da177e4SLinus Torvalds 	int i, j, channels, ofs;
44541da177e4SLinus Torvalds 
44551da177e4SLinus Torvalds 	if (hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DoubleSpeedStatus)
44561da177e4SLinus Torvalds 		doublespeed = 1;
44571da177e4SLinus Torvalds 	channels = doublespeed ? 14 : 26;
44581da177e4SLinus Torvalds 	for (i = 0, j = 0; i < 26; ++i) {
44591da177e4SLinus Torvalds 		if (doublespeed && (i & 4))
44601da177e4SLinus Torvalds 			continue;
44611da177e4SLinus Torvalds 		ofs = HDSP_9652_peakBase - j * 4;
44621da177e4SLinus Torvalds 		if (copy_u32_le(&peak_rms->input_peaks[i], hdsp->iobase + ofs))
44631da177e4SLinus Torvalds 			return -EFAULT;
44641da177e4SLinus Torvalds 		ofs -= channels * 4;
44651da177e4SLinus Torvalds 		if (copy_u32_le(&peak_rms->playback_peaks[i], hdsp->iobase + ofs))
44661da177e4SLinus Torvalds 			return -EFAULT;
44671da177e4SLinus Torvalds 		ofs -= channels * 4;
44681da177e4SLinus Torvalds 		if (copy_u32_le(&peak_rms->output_peaks[i], hdsp->iobase + ofs))
44691da177e4SLinus Torvalds 			return -EFAULT;
44701da177e4SLinus Torvalds 		ofs = HDSP_9652_rmsBase + j * 8;
44711da177e4SLinus Torvalds 		if (copy_u48_le(&peak_rms->input_rms[i], hdsp->iobase + ofs,
44721da177e4SLinus Torvalds 				hdsp->iobase + ofs + 4))
44731da177e4SLinus Torvalds 			return -EFAULT;
44741da177e4SLinus Torvalds 		ofs += channels * 8;
44751da177e4SLinus Torvalds 		if (copy_u48_le(&peak_rms->playback_rms[i], hdsp->iobase + ofs,
44761da177e4SLinus Torvalds 				hdsp->iobase + ofs + 4))
44771da177e4SLinus Torvalds 			return -EFAULT;
44781da177e4SLinus Torvalds 		ofs += channels * 8;
44791da177e4SLinus Torvalds 		if (copy_u48_le(&peak_rms->output_rms[i], hdsp->iobase + ofs,
44801da177e4SLinus Torvalds 				hdsp->iobase + ofs + 4))
44811da177e4SLinus Torvalds 			return -EFAULT;
44821da177e4SLinus Torvalds 		j++;
44831da177e4SLinus Torvalds 	}
44841da177e4SLinus Torvalds 	return 0;
44851da177e4SLinus Torvalds }
44861da177e4SLinus Torvalds 
448755e957d8STakashi Iwai static int hdsp_9632_get_peak(struct hdsp *hdsp, struct hdsp_peak_rms __user *peak_rms)
44881da177e4SLinus Torvalds {
44891da177e4SLinus Torvalds 	int i, j;
449055e957d8STakashi Iwai 	struct hdsp_9632_meters __iomem *m;
44911da177e4SLinus Torvalds 	int doublespeed = 0;
44921da177e4SLinus Torvalds 
44931da177e4SLinus Torvalds 	if (hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DoubleSpeedStatus)
44941da177e4SLinus Torvalds 		doublespeed = 1;
449555e957d8STakashi Iwai 	m = (struct hdsp_9632_meters __iomem *)(hdsp->iobase+HDSP_9632_metersBase);
44961da177e4SLinus Torvalds 	for (i = 0, j = 0; i < 16; ++i, ++j) {
44971da177e4SLinus Torvalds 		if (copy_u32_le(&peak_rms->input_peaks[i], &m->input_peak[j]))
44981da177e4SLinus Torvalds 			return -EFAULT;
44991da177e4SLinus Torvalds 		if (copy_u32_le(&peak_rms->playback_peaks[i], &m->playback_peak[j]))
45001da177e4SLinus Torvalds 			return -EFAULT;
45011da177e4SLinus Torvalds 		if (copy_u32_le(&peak_rms->output_peaks[i], &m->output_peak[j]))
45021da177e4SLinus Torvalds 			return -EFAULT;
45031da177e4SLinus Torvalds 		if (copy_u64_le(&peak_rms->input_rms[i], &m->input_rms_low[j],
45041da177e4SLinus Torvalds 				&m->input_rms_high[j]))
45051da177e4SLinus Torvalds 			return -EFAULT;
45061da177e4SLinus Torvalds 		if (copy_u64_le(&peak_rms->playback_rms[i], &m->playback_rms_low[j],
45071da177e4SLinus Torvalds 				&m->playback_rms_high[j]))
45081da177e4SLinus Torvalds 			return -EFAULT;
45091da177e4SLinus Torvalds 		if (copy_u64_le(&peak_rms->output_rms[i], &m->output_rms_low[j],
45101da177e4SLinus Torvalds 				&m->output_rms_high[j]))
45111da177e4SLinus Torvalds 			return -EFAULT;
45121da177e4SLinus Torvalds 		if (doublespeed && i == 3) i += 4;
45131da177e4SLinus Torvalds 	}
45141da177e4SLinus Torvalds 	return 0;
45151da177e4SLinus Torvalds }
45161da177e4SLinus Torvalds 
451755e957d8STakashi Iwai static int hdsp_get_peak(struct hdsp *hdsp, struct hdsp_peak_rms __user *peak_rms)
45181da177e4SLinus Torvalds {
45191da177e4SLinus Torvalds 	int i;
45201da177e4SLinus Torvalds 
45211da177e4SLinus Torvalds 	for (i = 0; i < 26; i++) {
45221da177e4SLinus Torvalds 		if (copy_u32_le(&peak_rms->playback_peaks[i],
45231da177e4SLinus Torvalds 				hdsp->iobase + HDSP_playbackPeakLevel + i * 4))
45241da177e4SLinus Torvalds 			return -EFAULT;
45251da177e4SLinus Torvalds 		if (copy_u32_le(&peak_rms->input_peaks[i],
45261da177e4SLinus Torvalds 				hdsp->iobase + HDSP_inputPeakLevel + i * 4))
45271da177e4SLinus Torvalds 			return -EFAULT;
45281da177e4SLinus Torvalds 	}
45291da177e4SLinus Torvalds 	for (i = 0; i < 28; i++) {
45301da177e4SLinus Torvalds 		if (copy_u32_le(&peak_rms->output_peaks[i],
45311da177e4SLinus Torvalds 				hdsp->iobase + HDSP_outputPeakLevel + i * 4))
45321da177e4SLinus Torvalds 			return -EFAULT;
45331da177e4SLinus Torvalds 	}
45341da177e4SLinus Torvalds 	for (i = 0; i < 26; ++i) {
45351da177e4SLinus Torvalds 		if (copy_u64_le(&peak_rms->playback_rms[i],
45361da177e4SLinus Torvalds 				hdsp->iobase + HDSP_playbackRmsLevel + i * 8 + 4,
45371da177e4SLinus Torvalds 				hdsp->iobase + HDSP_playbackRmsLevel + i * 8))
45381da177e4SLinus Torvalds 			return -EFAULT;
45391da177e4SLinus Torvalds 		if (copy_u64_le(&peak_rms->input_rms[i],
45401da177e4SLinus Torvalds 				hdsp->iobase + HDSP_inputRmsLevel + i * 8 + 4,
45411da177e4SLinus Torvalds 				hdsp->iobase + HDSP_inputRmsLevel + i * 8))
45421da177e4SLinus Torvalds 			return -EFAULT;
45431da177e4SLinus Torvalds 	}
45441da177e4SLinus Torvalds 	return 0;
45451da177e4SLinus Torvalds }
45461da177e4SLinus Torvalds 
454755e957d8STakashi Iwai static int snd_hdsp_hwdep_ioctl(struct snd_hwdep *hw, struct file *file, unsigned int cmd, unsigned long arg)
45481da177e4SLinus Torvalds {
454955e957d8STakashi Iwai 	struct hdsp *hdsp = (struct hdsp *)hw->private_data;
45501da177e4SLinus Torvalds 	void __user *argp = (void __user *)arg;
45513ae7e2e2STim Blechmann 	int err;
45521da177e4SLinus Torvalds 
45531da177e4SLinus Torvalds 	switch (cmd) {
45541da177e4SLinus Torvalds 	case SNDRV_HDSP_IOCTL_GET_PEAK_RMS: {
455555e957d8STakashi Iwai 		struct hdsp_peak_rms __user *peak_rms = (struct hdsp_peak_rms __user *)arg;
45561da177e4SLinus Torvalds 
45573ae7e2e2STim Blechmann 		err = hdsp_check_for_iobox(hdsp);
45583ae7e2e2STim Blechmann 		if (err < 0)
45593ae7e2e2STim Blechmann 			return err;
45603ae7e2e2STim Blechmann 
45613ae7e2e2STim Blechmann 		err = hdsp_check_for_firmware(hdsp, 1);
45623ae7e2e2STim Blechmann 		if (err < 0)
45633ae7e2e2STim Blechmann 			return err;
45643ae7e2e2STim Blechmann 
45651da177e4SLinus Torvalds 		if (!(hdsp->state & HDSP_FirmwareLoaded)) {
45661da177e4SLinus Torvalds 			snd_printk(KERN_ERR "Hammerfall-DSP: firmware needs to be uploaded to the card.\n");
45671da177e4SLinus Torvalds 			return -EINVAL;
45681da177e4SLinus Torvalds 		}
45691da177e4SLinus Torvalds 
45701da177e4SLinus Torvalds 		switch (hdsp->io_type) {
45711da177e4SLinus Torvalds 		case H9652:
45721da177e4SLinus Torvalds 			return hdsp_9652_get_peak(hdsp, peak_rms);
45731da177e4SLinus Torvalds 		case H9632:
45741da177e4SLinus Torvalds 			return hdsp_9632_get_peak(hdsp, peak_rms);
45751da177e4SLinus Torvalds 		default:
45761da177e4SLinus Torvalds 			return hdsp_get_peak(hdsp, peak_rms);
45771da177e4SLinus Torvalds 		}
45781da177e4SLinus Torvalds 	}
45791da177e4SLinus Torvalds 	case SNDRV_HDSP_IOCTL_GET_CONFIG_INFO: {
458055e957d8STakashi Iwai 		struct hdsp_config_info info;
45811da177e4SLinus Torvalds 		unsigned long flags;
45821da177e4SLinus Torvalds 		int i;
45831da177e4SLinus Torvalds 
45843ae7e2e2STim Blechmann 		err = hdsp_check_for_iobox(hdsp);
45853ae7e2e2STim Blechmann 		if (err < 0)
45863ae7e2e2STim Blechmann 			return err;
45873ae7e2e2STim Blechmann 
45883ae7e2e2STim Blechmann 		err = hdsp_check_for_firmware(hdsp, 1);
45893ae7e2e2STim Blechmann 		if (err < 0)
45903ae7e2e2STim Blechmann 			return err;
45913ae7e2e2STim Blechmann 
45921da177e4SLinus Torvalds 		spin_lock_irqsave(&hdsp->lock, flags);
45931da177e4SLinus Torvalds 		info.pref_sync_ref = (unsigned char)hdsp_pref_sync_ref(hdsp);
45941da177e4SLinus Torvalds 		info.wordclock_sync_check = (unsigned char)hdsp_wc_sync_check(hdsp);
4595b0b98119STakashi Iwai 		if (hdsp->io_type != H9632)
45961da177e4SLinus Torvalds 		    info.adatsync_sync_check = (unsigned char)hdsp_adatsync_sync_check(hdsp);
45971da177e4SLinus Torvalds 		info.spdif_sync_check = (unsigned char)hdsp_spdif_sync_check(hdsp);
4598b0b98119STakashi Iwai 		for (i = 0; i < ((hdsp->io_type != Multiface && hdsp->io_type != H9632) ? 3 : 1); ++i)
45991da177e4SLinus Torvalds 			info.adat_sync_check[i] = (unsigned char)hdsp_adat_sync_check(hdsp, i);
46001da177e4SLinus Torvalds 		info.spdif_in = (unsigned char)hdsp_spdif_in(hdsp);
46011da177e4SLinus Torvalds 		info.spdif_out = (unsigned char)hdsp_spdif_out(hdsp);
46021da177e4SLinus Torvalds 		info.spdif_professional = (unsigned char)hdsp_spdif_professional(hdsp);
46031da177e4SLinus Torvalds 		info.spdif_emphasis = (unsigned char)hdsp_spdif_emphasis(hdsp);
46041da177e4SLinus Torvalds 		info.spdif_nonaudio = (unsigned char)hdsp_spdif_nonaudio(hdsp);
46051da177e4SLinus Torvalds 		info.spdif_sample_rate = hdsp_spdif_sample_rate(hdsp);
46061da177e4SLinus Torvalds 		info.system_sample_rate = hdsp->system_sample_rate;
46071da177e4SLinus Torvalds 		info.autosync_sample_rate = hdsp_external_sample_rate(hdsp);
46081da177e4SLinus Torvalds 		info.system_clock_mode = (unsigned char)hdsp_system_clock_mode(hdsp);
46091da177e4SLinus Torvalds 		info.clock_source = (unsigned char)hdsp_clock_source(hdsp);
46101da177e4SLinus Torvalds 		info.autosync_ref = (unsigned char)hdsp_autosync_ref(hdsp);
46111da177e4SLinus Torvalds 		info.line_out = (unsigned char)hdsp_line_out(hdsp);
46121da177e4SLinus Torvalds 		if (hdsp->io_type == H9632) {
46131da177e4SLinus Torvalds 			info.da_gain = (unsigned char)hdsp_da_gain(hdsp);
46141da177e4SLinus Torvalds 			info.ad_gain = (unsigned char)hdsp_ad_gain(hdsp);
46151da177e4SLinus Torvalds 			info.phone_gain = (unsigned char)hdsp_phone_gain(hdsp);
46161da177e4SLinus Torvalds 			info.xlr_breakout_cable = (unsigned char)hdsp_xlr_breakout_cable(hdsp);
46171da177e4SLinus Torvalds 
46181da177e4SLinus Torvalds 		}
4619b0b98119STakashi Iwai 		if (hdsp->io_type == H9632 || hdsp->io_type == H9652)
46201da177e4SLinus Torvalds 			info.analog_extension_board = (unsigned char)hdsp_aeb(hdsp);
46211da177e4SLinus Torvalds 		spin_unlock_irqrestore(&hdsp->lock, flags);
46221da177e4SLinus Torvalds 		if (copy_to_user(argp, &info, sizeof(info)))
46231da177e4SLinus Torvalds 			return -EFAULT;
46241da177e4SLinus Torvalds 		break;
46251da177e4SLinus Torvalds 	}
46261da177e4SLinus Torvalds 	case SNDRV_HDSP_IOCTL_GET_9632_AEB: {
462755e957d8STakashi Iwai 		struct hdsp_9632_aeb h9632_aeb;
46281da177e4SLinus Torvalds 
46291da177e4SLinus Torvalds 		if (hdsp->io_type != H9632) return -EINVAL;
46301da177e4SLinus Torvalds 		h9632_aeb.aebi = hdsp->ss_in_channels - H9632_SS_CHANNELS;
46311da177e4SLinus Torvalds 		h9632_aeb.aebo = hdsp->ss_out_channels - H9632_SS_CHANNELS;
46321da177e4SLinus Torvalds 		if (copy_to_user(argp, &h9632_aeb, sizeof(h9632_aeb)))
46331da177e4SLinus Torvalds 			return -EFAULT;
46341da177e4SLinus Torvalds 		break;
46351da177e4SLinus Torvalds 	}
46361da177e4SLinus Torvalds 	case SNDRV_HDSP_IOCTL_GET_VERSION: {
463755e957d8STakashi Iwai 		struct hdsp_version hdsp_version;
46381da177e4SLinus Torvalds 		int err;
46391da177e4SLinus Torvalds 
46401da177e4SLinus Torvalds 		if (hdsp->io_type == H9652 || hdsp->io_type == H9632) return -EINVAL;
46411da177e4SLinus Torvalds 		if (hdsp->io_type == Undefined) {
4642b0b98119STakashi Iwai 			if ((err = hdsp_get_iobox_version(hdsp)) < 0)
46431da177e4SLinus Torvalds 				return err;
46441da177e4SLinus Torvalds 		}
46451da177e4SLinus Torvalds 		hdsp_version.io_type = hdsp->io_type;
46461da177e4SLinus Torvalds 		hdsp_version.firmware_rev = hdsp->firmware_rev;
4647b0b98119STakashi Iwai 		if ((err = copy_to_user(argp, &hdsp_version, sizeof(hdsp_version))))
46481da177e4SLinus Torvalds 		    	return -EFAULT;
46491da177e4SLinus Torvalds 		break;
46501da177e4SLinus Torvalds 	}
46511da177e4SLinus Torvalds 	case SNDRV_HDSP_IOCTL_UPLOAD_FIRMWARE: {
465255e957d8STakashi Iwai 		struct hdsp_firmware __user *firmware;
46531da177e4SLinus Torvalds 		u32 __user *firmware_data;
46541da177e4SLinus Torvalds 		int err;
46551da177e4SLinus Torvalds 
46561da177e4SLinus Torvalds 		if (hdsp->io_type == H9652 || hdsp->io_type == H9632) return -EINVAL;
46571da177e4SLinus Torvalds 		/* SNDRV_HDSP_IOCTL_GET_VERSION must have been called */
46581da177e4SLinus Torvalds 		if (hdsp->io_type == Undefined) return -EINVAL;
46591da177e4SLinus Torvalds 
46601da177e4SLinus Torvalds 		if (hdsp->state & (HDSP_FirmwareCached | HDSP_FirmwareLoaded))
46611da177e4SLinus Torvalds 			return -EBUSY;
46621da177e4SLinus Torvalds 
4663b0b98119STakashi Iwai 		snd_printk(KERN_INFO "Hammerfall-DSP: initializing firmware upload\n");
466455e957d8STakashi Iwai 		firmware = (struct hdsp_firmware __user *)argp;
46651da177e4SLinus Torvalds 
4666b0b98119STakashi Iwai 		if (get_user(firmware_data, &firmware->firmware_data))
46671da177e4SLinus Torvalds 			return -EFAULT;
46681da177e4SLinus Torvalds 
4669b0b98119STakashi Iwai 		if (hdsp_check_for_iobox (hdsp))
46701da177e4SLinus Torvalds 			return -EIO;
46711da177e4SLinus Torvalds 
4672b0b98119STakashi Iwai 		if (copy_from_user(hdsp->firmware_cache, firmware_data, sizeof(hdsp->firmware_cache)) != 0)
46731da177e4SLinus Torvalds 			return -EFAULT;
46741da177e4SLinus Torvalds 
46751da177e4SLinus Torvalds 		hdsp->state |= HDSP_FirmwareCached;
46761da177e4SLinus Torvalds 
4677b0b98119STakashi Iwai 		if ((err = snd_hdsp_load_firmware_from_cache(hdsp)) < 0)
46781da177e4SLinus Torvalds 			return err;
46791da177e4SLinus Torvalds 
46801da177e4SLinus Torvalds 		if (!(hdsp->state & HDSP_InitializationComplete)) {
4681b0b98119STakashi Iwai 			if ((err = snd_hdsp_enable_io(hdsp)) < 0)
46821da177e4SLinus Torvalds 				return err;
46831da177e4SLinus Torvalds 
46841da177e4SLinus Torvalds 			snd_hdsp_initialize_channels(hdsp);
46851da177e4SLinus Torvalds 			snd_hdsp_initialize_midi_flush(hdsp);
46861da177e4SLinus Torvalds 
46871da177e4SLinus Torvalds 			if ((err = snd_hdsp_create_alsa_devices(hdsp->card, hdsp)) < 0) {
4688b0b98119STakashi Iwai 				snd_printk(KERN_ERR "Hammerfall-DSP: error creating alsa devices\n");
46891da177e4SLinus Torvalds 				return err;
46901da177e4SLinus Torvalds 			}
46911da177e4SLinus Torvalds 		}
46921da177e4SLinus Torvalds 		break;
46931da177e4SLinus Torvalds 	}
46941da177e4SLinus Torvalds 	case SNDRV_HDSP_IOCTL_GET_MIXER: {
469555e957d8STakashi Iwai 		struct hdsp_mixer __user *mixer = (struct hdsp_mixer __user *)argp;
46961da177e4SLinus Torvalds 		if (copy_to_user(mixer->matrix, hdsp->mixer_matrix, sizeof(unsigned short)*HDSP_MATRIX_MIXER_SIZE))
46971da177e4SLinus Torvalds 			return -EFAULT;
46981da177e4SLinus Torvalds 		break;
46991da177e4SLinus Torvalds 	}
47001da177e4SLinus Torvalds 	default:
47011da177e4SLinus Torvalds 		return -EINVAL;
47021da177e4SLinus Torvalds 	}
47031da177e4SLinus Torvalds 	return 0;
47041da177e4SLinus Torvalds }
47051da177e4SLinus Torvalds 
470655e957d8STakashi Iwai static struct snd_pcm_ops snd_hdsp_playback_ops = {
47071da177e4SLinus Torvalds 	.open =		snd_hdsp_playback_open,
47081da177e4SLinus Torvalds 	.close =	snd_hdsp_playback_release,
47091da177e4SLinus Torvalds 	.ioctl =	snd_hdsp_ioctl,
47101da177e4SLinus Torvalds 	.hw_params =	snd_hdsp_hw_params,
47111da177e4SLinus Torvalds 	.prepare =	snd_hdsp_prepare,
47121da177e4SLinus Torvalds 	.trigger =	snd_hdsp_trigger,
47131da177e4SLinus Torvalds 	.pointer =	snd_hdsp_hw_pointer,
47141da177e4SLinus Torvalds 	.copy =		snd_hdsp_playback_copy,
47151da177e4SLinus Torvalds 	.silence =	snd_hdsp_hw_silence,
47161da177e4SLinus Torvalds };
47171da177e4SLinus Torvalds 
471855e957d8STakashi Iwai static struct snd_pcm_ops snd_hdsp_capture_ops = {
47191da177e4SLinus Torvalds 	.open =		snd_hdsp_capture_open,
47201da177e4SLinus Torvalds 	.close =	snd_hdsp_capture_release,
47211da177e4SLinus Torvalds 	.ioctl =	snd_hdsp_ioctl,
47221da177e4SLinus Torvalds 	.hw_params =	snd_hdsp_hw_params,
47231da177e4SLinus Torvalds 	.prepare =	snd_hdsp_prepare,
47241da177e4SLinus Torvalds 	.trigger =	snd_hdsp_trigger,
47251da177e4SLinus Torvalds 	.pointer =	snd_hdsp_hw_pointer,
47261da177e4SLinus Torvalds 	.copy =		snd_hdsp_capture_copy,
47271da177e4SLinus Torvalds };
47281da177e4SLinus Torvalds 
472992eed66dSTakashi Iwai static int snd_hdsp_create_hwdep(struct snd_card *card, struct hdsp *hdsp)
47301da177e4SLinus Torvalds {
473155e957d8STakashi Iwai 	struct snd_hwdep *hw;
47321da177e4SLinus Torvalds 	int err;
47331da177e4SLinus Torvalds 
47341da177e4SLinus Torvalds 	if ((err = snd_hwdep_new(card, "HDSP hwdep", 0, &hw)) < 0)
47351da177e4SLinus Torvalds 		return err;
47361da177e4SLinus Torvalds 
47371da177e4SLinus Torvalds 	hdsp->hwdep = hw;
47381da177e4SLinus Torvalds 	hw->private_data = hdsp;
47391da177e4SLinus Torvalds 	strcpy(hw->name, "HDSP hwdep interface");
47401da177e4SLinus Torvalds 
47411da177e4SLinus Torvalds 	hw->ops.open = snd_hdsp_hwdep_dummy_op;
47421da177e4SLinus Torvalds 	hw->ops.ioctl = snd_hdsp_hwdep_ioctl;
47431da177e4SLinus Torvalds 	hw->ops.release = snd_hdsp_hwdep_dummy_op;
47441da177e4SLinus Torvalds 
47451da177e4SLinus Torvalds 	return 0;
47461da177e4SLinus Torvalds }
47471da177e4SLinus Torvalds 
474855e957d8STakashi Iwai static int snd_hdsp_create_pcm(struct snd_card *card, struct hdsp *hdsp)
47491da177e4SLinus Torvalds {
475055e957d8STakashi Iwai 	struct snd_pcm *pcm;
47511da177e4SLinus Torvalds 	int err;
47521da177e4SLinus Torvalds 
47531da177e4SLinus Torvalds 	if ((err = snd_pcm_new(card, hdsp->card_name, 0, 1, 1, &pcm)) < 0)
47541da177e4SLinus Torvalds 		return err;
47551da177e4SLinus Torvalds 
47561da177e4SLinus Torvalds 	hdsp->pcm = pcm;
47571da177e4SLinus Torvalds 	pcm->private_data = hdsp;
47581da177e4SLinus Torvalds 	strcpy(pcm->name, hdsp->card_name);
47591da177e4SLinus Torvalds 
47601da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_hdsp_playback_ops);
47611da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_hdsp_capture_ops);
47621da177e4SLinus Torvalds 
47631da177e4SLinus Torvalds 	pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
47641da177e4SLinus Torvalds 
47651da177e4SLinus Torvalds 	return 0;
47661da177e4SLinus Torvalds }
47671da177e4SLinus Torvalds 
476855e957d8STakashi Iwai static void snd_hdsp_9652_enable_mixer (struct hdsp *hdsp)
47691da177e4SLinus Torvalds {
47701da177e4SLinus Torvalds         hdsp->control2_register |= HDSP_9652_ENABLE_MIXER;
47711da177e4SLinus Torvalds 	hdsp_write (hdsp, HDSP_control2Reg, hdsp->control2_register);
47721da177e4SLinus Torvalds }
47731da177e4SLinus Torvalds 
477455e957d8STakashi Iwai static int snd_hdsp_enable_io (struct hdsp *hdsp)
47751da177e4SLinus Torvalds {
47761da177e4SLinus Torvalds 	int i;
47771da177e4SLinus Torvalds 
47781da177e4SLinus Torvalds 	if (hdsp_fifo_wait (hdsp, 0, 100)) {
4779b0b98119STakashi Iwai 		snd_printk(KERN_ERR "Hammerfall-DSP: enable_io fifo_wait failed\n");
47801da177e4SLinus Torvalds 		return -EIO;
47811da177e4SLinus Torvalds 	}
47821da177e4SLinus Torvalds 
47831da177e4SLinus Torvalds 	for (i = 0; i < hdsp->max_channels; ++i) {
47841da177e4SLinus Torvalds 		hdsp_write (hdsp, HDSP_inputEnable + (4 * i), 1);
47851da177e4SLinus Torvalds 		hdsp_write (hdsp, HDSP_outputEnable + (4 * i), 1);
47861da177e4SLinus Torvalds 	}
47871da177e4SLinus Torvalds 
47881da177e4SLinus Torvalds 	return 0;
47891da177e4SLinus Torvalds }
47901da177e4SLinus Torvalds 
479155e957d8STakashi Iwai static void snd_hdsp_initialize_channels(struct hdsp *hdsp)
47921da177e4SLinus Torvalds {
47931da177e4SLinus Torvalds 	int status, aebi_channels, aebo_channels;
47941da177e4SLinus Torvalds 
47951da177e4SLinus Torvalds 	switch (hdsp->io_type) {
47961da177e4SLinus Torvalds 	case Digiface:
47971da177e4SLinus Torvalds 		hdsp->card_name = "RME Hammerfall DSP + Digiface";
47981da177e4SLinus Torvalds 		hdsp->ss_in_channels = hdsp->ss_out_channels = DIGIFACE_SS_CHANNELS;
47991da177e4SLinus Torvalds 		hdsp->ds_in_channels = hdsp->ds_out_channels = DIGIFACE_DS_CHANNELS;
48001da177e4SLinus Torvalds 		break;
48011da177e4SLinus Torvalds 
48021da177e4SLinus Torvalds 	case H9652:
48031da177e4SLinus Torvalds 		hdsp->card_name = "RME Hammerfall HDSP 9652";
48041da177e4SLinus Torvalds 		hdsp->ss_in_channels = hdsp->ss_out_channels = H9652_SS_CHANNELS;
48051da177e4SLinus Torvalds 		hdsp->ds_in_channels = hdsp->ds_out_channels = H9652_DS_CHANNELS;
48061da177e4SLinus Torvalds 		break;
48071da177e4SLinus Torvalds 
48081da177e4SLinus Torvalds 	case H9632:
48091da177e4SLinus Torvalds 		status = hdsp_read(hdsp, HDSP_statusRegister);
48101da177e4SLinus Torvalds 		/* HDSP_AEBx bits are low when AEB are connected */
48111da177e4SLinus Torvalds 		aebi_channels = (status & HDSP_AEBI) ? 0 : 4;
48121da177e4SLinus Torvalds 		aebo_channels = (status & HDSP_AEBO) ? 0 : 4;
48131da177e4SLinus Torvalds 		hdsp->card_name = "RME Hammerfall HDSP 9632";
48141da177e4SLinus Torvalds 		hdsp->ss_in_channels = H9632_SS_CHANNELS+aebi_channels;
48151da177e4SLinus Torvalds 		hdsp->ds_in_channels = H9632_DS_CHANNELS+aebi_channels;
48161da177e4SLinus Torvalds 		hdsp->qs_in_channels = H9632_QS_CHANNELS+aebi_channels;
48171da177e4SLinus Torvalds 		hdsp->ss_out_channels = H9632_SS_CHANNELS+aebo_channels;
48181da177e4SLinus Torvalds 		hdsp->ds_out_channels = H9632_DS_CHANNELS+aebo_channels;
48191da177e4SLinus Torvalds 		hdsp->qs_out_channels = H9632_QS_CHANNELS+aebo_channels;
48201da177e4SLinus Torvalds 		break;
48211da177e4SLinus Torvalds 
48221da177e4SLinus Torvalds 	case Multiface:
48231da177e4SLinus Torvalds 		hdsp->card_name = "RME Hammerfall DSP + Multiface";
48241da177e4SLinus Torvalds 		hdsp->ss_in_channels = hdsp->ss_out_channels = MULTIFACE_SS_CHANNELS;
48251da177e4SLinus Torvalds 		hdsp->ds_in_channels = hdsp->ds_out_channels = MULTIFACE_DS_CHANNELS;
48261da177e4SLinus Torvalds 		break;
48271da177e4SLinus Torvalds 
48281da177e4SLinus Torvalds 	default:
48291da177e4SLinus Torvalds  		/* should never get here */
48301da177e4SLinus Torvalds 		break;
48311da177e4SLinus Torvalds 	}
48321da177e4SLinus Torvalds }
48331da177e4SLinus Torvalds 
483455e957d8STakashi Iwai static void snd_hdsp_initialize_midi_flush (struct hdsp *hdsp)
48351da177e4SLinus Torvalds {
48361da177e4SLinus Torvalds 	snd_hdsp_flush_midi_input (hdsp, 0);
48371da177e4SLinus Torvalds 	snd_hdsp_flush_midi_input (hdsp, 1);
48381da177e4SLinus Torvalds }
48391da177e4SLinus Torvalds 
484055e957d8STakashi Iwai static int snd_hdsp_create_alsa_devices(struct snd_card *card, struct hdsp *hdsp)
48411da177e4SLinus Torvalds {
48421da177e4SLinus Torvalds 	int err;
48431da177e4SLinus Torvalds 
48441da177e4SLinus Torvalds 	if ((err = snd_hdsp_create_pcm(card, hdsp)) < 0) {
4845b0b98119STakashi Iwai 		snd_printk(KERN_ERR "Hammerfall-DSP: Error creating pcm interface\n");
48461da177e4SLinus Torvalds 		return err;
48471da177e4SLinus Torvalds 	}
48481da177e4SLinus Torvalds 
48491da177e4SLinus Torvalds 
48501da177e4SLinus Torvalds 	if ((err = snd_hdsp_create_midi(card, hdsp, 0)) < 0) {
4851b0b98119STakashi Iwai 		snd_printk(KERN_ERR "Hammerfall-DSP: Error creating first midi interface\n");
48521da177e4SLinus Torvalds 		return err;
48531da177e4SLinus Torvalds 	}
48541da177e4SLinus Torvalds 
48551da177e4SLinus Torvalds 	if (hdsp->io_type == Digiface || hdsp->io_type == H9652) {
48561da177e4SLinus Torvalds 		if ((err = snd_hdsp_create_midi(card, hdsp, 1)) < 0) {
4857b0b98119STakashi Iwai 			snd_printk(KERN_ERR "Hammerfall-DSP: Error creating second midi interface\n");
48581da177e4SLinus Torvalds 			return err;
48591da177e4SLinus Torvalds 		}
48601da177e4SLinus Torvalds 	}
48611da177e4SLinus Torvalds 
48621da177e4SLinus Torvalds 	if ((err = snd_hdsp_create_controls(card, hdsp)) < 0) {
4863b0b98119STakashi Iwai 		snd_printk(KERN_ERR "Hammerfall-DSP: Error creating ctl interface\n");
48641da177e4SLinus Torvalds 		return err;
48651da177e4SLinus Torvalds 	}
48661da177e4SLinus Torvalds 
48671da177e4SLinus Torvalds 	snd_hdsp_proc_init(hdsp);
48681da177e4SLinus Torvalds 
48691da177e4SLinus Torvalds 	hdsp->system_sample_rate = -1;
48701da177e4SLinus Torvalds 	hdsp->playback_pid = -1;
48711da177e4SLinus Torvalds 	hdsp->capture_pid = -1;
48721da177e4SLinus Torvalds 	hdsp->capture_substream = NULL;
48731da177e4SLinus Torvalds 	hdsp->playback_substream = NULL;
48741da177e4SLinus Torvalds 
48751da177e4SLinus Torvalds 	if ((err = snd_hdsp_set_defaults(hdsp)) < 0) {
4876b0b98119STakashi Iwai 		snd_printk(KERN_ERR "Hammerfall-DSP: Error setting default values\n");
48771da177e4SLinus Torvalds 		return err;
48781da177e4SLinus Torvalds 	}
48791da177e4SLinus Torvalds 
48801da177e4SLinus Torvalds 	if (!(hdsp->state & HDSP_InitializationComplete)) {
4881b73c1c12SClemens Ladisch 		strcpy(card->shortname, "Hammerfall DSP");
48821da177e4SLinus Torvalds 		sprintf(card->longname, "%s at 0x%lx, irq %d", hdsp->card_name,
48831da177e4SLinus Torvalds 			hdsp->port, hdsp->irq);
48841da177e4SLinus Torvalds 
48851da177e4SLinus Torvalds 		if ((err = snd_card_register(card)) < 0) {
4886b0b98119STakashi Iwai 			snd_printk(KERN_ERR "Hammerfall-DSP: error registering card\n");
48871da177e4SLinus Torvalds 			return err;
48881da177e4SLinus Torvalds 		}
48891da177e4SLinus Torvalds 		hdsp->state |= HDSP_InitializationComplete;
48901da177e4SLinus Torvalds 	}
48911da177e4SLinus Torvalds 
48921da177e4SLinus Torvalds 	return 0;
48931da177e4SLinus Torvalds }
48941da177e4SLinus Torvalds 
48951da177e4SLinus Torvalds #ifdef HDSP_FW_LOADER
48961da177e4SLinus Torvalds /* load firmware via hotplug fw loader */
489792eed66dSTakashi Iwai static int hdsp_request_fw_loader(struct hdsp *hdsp)
48981da177e4SLinus Torvalds {
48991da177e4SLinus Torvalds 	const char *fwfile;
49001da177e4SLinus Torvalds 	const struct firmware *fw;
49011da177e4SLinus Torvalds 	int err;
49021da177e4SLinus Torvalds 
49031da177e4SLinus Torvalds 	if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
49041da177e4SLinus Torvalds 		return 0;
49051da177e4SLinus Torvalds 	if (hdsp->io_type == Undefined) {
49061da177e4SLinus Torvalds 		if ((err = hdsp_get_iobox_version(hdsp)) < 0)
49071da177e4SLinus Torvalds 			return err;
49081da177e4SLinus Torvalds 		if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
49091da177e4SLinus Torvalds 			return 0;
49101da177e4SLinus Torvalds 	}
49111da177e4SLinus Torvalds 
49121da177e4SLinus Torvalds 	/* caution: max length of firmware filename is 30! */
49131da177e4SLinus Torvalds 	switch (hdsp->io_type) {
49141da177e4SLinus Torvalds 	case Multiface:
49151da177e4SLinus Torvalds 		if (hdsp->firmware_rev == 0xa)
49161da177e4SLinus Torvalds 			fwfile = "multiface_firmware.bin";
49171da177e4SLinus Torvalds 		else
49181da177e4SLinus Torvalds 			fwfile = "multiface_firmware_rev11.bin";
49191da177e4SLinus Torvalds 		break;
49201da177e4SLinus Torvalds 	case Digiface:
49211da177e4SLinus Torvalds 		if (hdsp->firmware_rev == 0xa)
49221da177e4SLinus Torvalds 			fwfile = "digiface_firmware.bin";
49231da177e4SLinus Torvalds 		else
49241da177e4SLinus Torvalds 			fwfile = "digiface_firmware_rev11.bin";
49251da177e4SLinus Torvalds 		break;
49261da177e4SLinus Torvalds 	default:
49271da177e4SLinus Torvalds 		snd_printk(KERN_ERR "Hammerfall-DSP: invalid io_type %d\n", hdsp->io_type);
49281da177e4SLinus Torvalds 		return -EINVAL;
49291da177e4SLinus Torvalds 	}
49301da177e4SLinus Torvalds 
49311da177e4SLinus Torvalds 	if (request_firmware(&fw, fwfile, &hdsp->pci->dev)) {
49321da177e4SLinus Torvalds 		snd_printk(KERN_ERR "Hammerfall-DSP: cannot load firmware %s\n", fwfile);
49331da177e4SLinus Torvalds 		return -ENOENT;
49341da177e4SLinus Torvalds 	}
49351da177e4SLinus Torvalds 	if (fw->size < sizeof(hdsp->firmware_cache)) {
49361da177e4SLinus Torvalds 		snd_printk(KERN_ERR "Hammerfall-DSP: too short firmware size %d (expected %d)\n",
49371da177e4SLinus Torvalds 			   (int)fw->size, (int)sizeof(hdsp->firmware_cache));
49381da177e4SLinus Torvalds 		release_firmware(fw);
49391da177e4SLinus Torvalds 		return -EINVAL;
49401da177e4SLinus Torvalds 	}
49417679a030SThomas Charbonnel 
49421da177e4SLinus Torvalds 	memcpy(hdsp->firmware_cache, fw->data, sizeof(hdsp->firmware_cache));
49437679a030SThomas Charbonnel 
49441da177e4SLinus Torvalds 	release_firmware(fw);
49451da177e4SLinus Torvalds 
49461da177e4SLinus Torvalds 	hdsp->state |= HDSP_FirmwareCached;
49471da177e4SLinus Torvalds 
49481da177e4SLinus Torvalds 	if ((err = snd_hdsp_load_firmware_from_cache(hdsp)) < 0)
49491da177e4SLinus Torvalds 		return err;
49501da177e4SLinus Torvalds 
49511da177e4SLinus Torvalds 	if (!(hdsp->state & HDSP_InitializationComplete)) {
4952b0b98119STakashi Iwai 		if ((err = snd_hdsp_enable_io(hdsp)) < 0)
49531da177e4SLinus Torvalds 			return err;
49541da177e4SLinus Torvalds 
49551da177e4SLinus Torvalds 		if ((err = snd_hdsp_create_hwdep(hdsp->card, hdsp)) < 0) {
4956b0b98119STakashi Iwai 			snd_printk(KERN_ERR "Hammerfall-DSP: error creating hwdep device\n");
49571da177e4SLinus Torvalds 			return err;
49581da177e4SLinus Torvalds 		}
49591da177e4SLinus Torvalds 		snd_hdsp_initialize_channels(hdsp);
49601da177e4SLinus Torvalds 		snd_hdsp_initialize_midi_flush(hdsp);
49611da177e4SLinus Torvalds 		if ((err = snd_hdsp_create_alsa_devices(hdsp->card, hdsp)) < 0) {
4962b0b98119STakashi Iwai 			snd_printk(KERN_ERR "Hammerfall-DSP: error creating alsa devices\n");
49631da177e4SLinus Torvalds 			return err;
49641da177e4SLinus Torvalds 		}
49651da177e4SLinus Torvalds 	}
49661da177e4SLinus Torvalds 	return 0;
49671da177e4SLinus Torvalds }
49681da177e4SLinus Torvalds #endif
49691da177e4SLinus Torvalds 
497055e957d8STakashi Iwai static int __devinit snd_hdsp_create(struct snd_card *card,
497155e957d8STakashi Iwai 				     struct hdsp *hdsp)
49721da177e4SLinus Torvalds {
49731da177e4SLinus Torvalds 	struct pci_dev *pci = hdsp->pci;
49741da177e4SLinus Torvalds 	int err;
49751da177e4SLinus Torvalds 	int is_9652 = 0;
49761da177e4SLinus Torvalds 	int is_9632 = 0;
49771da177e4SLinus Torvalds 
49781da177e4SLinus Torvalds 	hdsp->irq = -1;
49791da177e4SLinus Torvalds 	hdsp->state = 0;
49801da177e4SLinus Torvalds 	hdsp->midi[0].rmidi = NULL;
49811da177e4SLinus Torvalds 	hdsp->midi[1].rmidi = NULL;
49821da177e4SLinus Torvalds 	hdsp->midi[0].input = NULL;
49831da177e4SLinus Torvalds 	hdsp->midi[1].input = NULL;
49841da177e4SLinus Torvalds 	hdsp->midi[0].output = NULL;
49851da177e4SLinus Torvalds 	hdsp->midi[1].output = NULL;
49861da177e4SLinus Torvalds 	hdsp->midi[0].pending = 0;
49871da177e4SLinus Torvalds 	hdsp->midi[1].pending = 0;
49881da177e4SLinus Torvalds 	spin_lock_init(&hdsp->midi[0].lock);
49891da177e4SLinus Torvalds 	spin_lock_init(&hdsp->midi[1].lock);
49901da177e4SLinus Torvalds 	hdsp->iobase = NULL;
49911da177e4SLinus Torvalds 	hdsp->control_register = 0;
49921da177e4SLinus Torvalds 	hdsp->control2_register = 0;
49931da177e4SLinus Torvalds 	hdsp->io_type = Undefined;
49941da177e4SLinus Torvalds 	hdsp->max_channels = 26;
49951da177e4SLinus Torvalds 
49961da177e4SLinus Torvalds 	hdsp->card = card;
49971da177e4SLinus Torvalds 
49981da177e4SLinus Torvalds 	spin_lock_init(&hdsp->lock);
49991da177e4SLinus Torvalds 
50001da177e4SLinus Torvalds 	tasklet_init(&hdsp->midi_tasklet, hdsp_midi_tasklet, (unsigned long)hdsp);
50011da177e4SLinus Torvalds 
50021da177e4SLinus Torvalds 	pci_read_config_word(hdsp->pci, PCI_CLASS_REVISION, &hdsp->firmware_rev);
50031da177e4SLinus Torvalds 	hdsp->firmware_rev &= 0xff;
50041da177e4SLinus Torvalds 
50051da177e4SLinus Torvalds 	/* From Martin Bjoernsen :
50061da177e4SLinus Torvalds 	    "It is important that the card's latency timer register in
50071da177e4SLinus Torvalds 	    the PCI configuration space is set to a value much larger
50081da177e4SLinus Torvalds 	    than 0 by the computer's BIOS or the driver.
50091da177e4SLinus Torvalds 	    The windows driver always sets this 8 bit register [...]
50101da177e4SLinus Torvalds 	    to its maximum 255 to avoid problems with some computers."
50111da177e4SLinus Torvalds 	*/
50121da177e4SLinus Torvalds 	pci_write_config_byte(hdsp->pci, PCI_LATENCY_TIMER, 0xFF);
50131da177e4SLinus Torvalds 
50141da177e4SLinus Torvalds 	strcpy(card->driver, "H-DSP");
50151da177e4SLinus Torvalds 	strcpy(card->mixername, "Xilinx FPGA");
50161da177e4SLinus Torvalds 
5017b0b98119STakashi Iwai 	if (hdsp->firmware_rev < 0xa)
50181da177e4SLinus Torvalds 		return -ENODEV;
5019b0b98119STakashi Iwai 	else if (hdsp->firmware_rev < 0x64)
50201da177e4SLinus Torvalds 		hdsp->card_name = "RME Hammerfall DSP";
5021b0b98119STakashi Iwai 	else if (hdsp->firmware_rev < 0x96) {
50221da177e4SLinus Torvalds 		hdsp->card_name = "RME HDSP 9652";
50231da177e4SLinus Torvalds 		is_9652 = 1;
50241da177e4SLinus Torvalds 	} else {
50251da177e4SLinus Torvalds 		hdsp->card_name = "RME HDSP 9632";
50261da177e4SLinus Torvalds 		hdsp->max_channels = 16;
50271da177e4SLinus Torvalds 		is_9632 = 1;
50281da177e4SLinus Torvalds 	}
50291da177e4SLinus Torvalds 
5030b0b98119STakashi Iwai 	if ((err = pci_enable_device(pci)) < 0)
50311da177e4SLinus Torvalds 		return err;
50321da177e4SLinus Torvalds 
50331da177e4SLinus Torvalds 	pci_set_master(hdsp->pci);
50341da177e4SLinus Torvalds 
50351da177e4SLinus Torvalds 	if ((err = pci_request_regions(pci, "hdsp")) < 0)
50361da177e4SLinus Torvalds 		return err;
50371da177e4SLinus Torvalds 	hdsp->port = pci_resource_start(pci, 0);
50381da177e4SLinus Torvalds 	if ((hdsp->iobase = ioremap_nocache(hdsp->port, HDSP_IO_EXTENT)) == NULL) {
5039b0b98119STakashi Iwai 		snd_printk(KERN_ERR "Hammerfall-DSP: unable to remap region 0x%lx-0x%lx\n", hdsp->port, hdsp->port + HDSP_IO_EXTENT - 1);
50401da177e4SLinus Torvalds 		return -EBUSY;
50411da177e4SLinus Torvalds 	}
50421da177e4SLinus Torvalds 
5043437a5a46STakashi Iwai 	if (request_irq(pci->irq, snd_hdsp_interrupt, IRQF_SHARED,
5044437a5a46STakashi Iwai 			"hdsp", hdsp)) {
5045b0b98119STakashi Iwai 		snd_printk(KERN_ERR "Hammerfall-DSP: unable to use IRQ %d\n", pci->irq);
50461da177e4SLinus Torvalds 		return -EBUSY;
50471da177e4SLinus Torvalds 	}
50481da177e4SLinus Torvalds 
50491da177e4SLinus Torvalds 	hdsp->irq = pci->irq;
5050176546abSRemy Bruno 	hdsp->precise_ptr = 0;
50511da177e4SLinus Torvalds 	hdsp->use_midi_tasklet = 1;
5052d7923b2aSRemy Bruno 	hdsp->dds_value = 0;
50531da177e4SLinus Torvalds 
5054b0b98119STakashi Iwai 	if ((err = snd_hdsp_initialize_memory(hdsp)) < 0)
50551da177e4SLinus Torvalds 		return err;
50561da177e4SLinus Torvalds 
50571da177e4SLinus Torvalds 	if (!is_9652 && !is_9632) {
50581da177e4SLinus Torvalds 		/* we wait 2 seconds to let freshly inserted cardbus cards do their hardware init */
5059ef21ca24SNishanth Aravamudan 		ssleep(2);
50601da177e4SLinus Torvalds 
506100c9ddd1STim Blechmann 		err = hdsp_check_for_iobox(hdsp);
506200c9ddd1STim Blechmann 		if (err < 0)
506300c9ddd1STim Blechmann 			return err;
506400c9ddd1STim Blechmann 
50651da177e4SLinus Torvalds 		if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
50661da177e4SLinus Torvalds #ifdef HDSP_FW_LOADER
5067b0b98119STakashi Iwai 			if ((err = hdsp_request_fw_loader(hdsp)) < 0)
50681da177e4SLinus Torvalds 				/* we don't fail as this can happen
50691da177e4SLinus Torvalds 				   if userspace is not ready for
50701da177e4SLinus Torvalds 				   firmware upload
50711da177e4SLinus Torvalds 				*/
5072b0b98119STakashi Iwai 				snd_printk(KERN_ERR "Hammerfall-DSP: couldn't get firmware from userspace. try using hdsploader\n");
5073b0b98119STakashi Iwai 			else
50741da177e4SLinus Torvalds 				/* init is complete, we return */
50751da177e4SLinus Torvalds 				return 0;
50761da177e4SLinus Torvalds #endif
507700c9ddd1STim Blechmann 			/* we defer initialization */
5078b0b98119STakashi Iwai 			snd_printk(KERN_INFO "Hammerfall-DSP: card initialization pending : waiting for firmware\n");
5079b0b98119STakashi Iwai 			if ((err = snd_hdsp_create_hwdep(card, hdsp)) < 0)
50801da177e4SLinus Torvalds 				return err;
50811da177e4SLinus Torvalds 			return 0;
50821da177e4SLinus Torvalds 		} else {
5083b0b98119STakashi Iwai 			snd_printk(KERN_INFO "Hammerfall-DSP: Firmware already present, initializing card.\n");
5084b0b98119STakashi Iwai 			if (hdsp_read(hdsp, HDSP_status2Register) & HDSP_version1)
50851da177e4SLinus Torvalds 				hdsp->io_type = Multiface;
5086b0b98119STakashi Iwai 			else
50871da177e4SLinus Torvalds 				hdsp->io_type = Digiface;
50881da177e4SLinus Torvalds 		}
50891da177e4SLinus Torvalds 	}
50901da177e4SLinus Torvalds 
5091b0b98119STakashi Iwai 	if ((err = snd_hdsp_enable_io(hdsp)) != 0)
50921da177e4SLinus Torvalds 		return err;
50931da177e4SLinus Torvalds 
5094b0b98119STakashi Iwai 	if (is_9652)
50951da177e4SLinus Torvalds 	        hdsp->io_type = H9652;
50961da177e4SLinus Torvalds 
5097b0b98119STakashi Iwai 	if (is_9632)
50981da177e4SLinus Torvalds 		hdsp->io_type = H9632;
50991da177e4SLinus Torvalds 
5100b0b98119STakashi Iwai 	if ((err = snd_hdsp_create_hwdep(card, hdsp)) < 0)
51011da177e4SLinus Torvalds 		return err;
51021da177e4SLinus Torvalds 
51031da177e4SLinus Torvalds 	snd_hdsp_initialize_channels(hdsp);
51041da177e4SLinus Torvalds 	snd_hdsp_initialize_midi_flush(hdsp);
51051da177e4SLinus Torvalds 
51061da177e4SLinus Torvalds 	hdsp->state |= HDSP_FirmwareLoaded;
51071da177e4SLinus Torvalds 
5108b0b98119STakashi Iwai 	if ((err = snd_hdsp_create_alsa_devices(card, hdsp)) < 0)
51091da177e4SLinus Torvalds 		return err;
51101da177e4SLinus Torvalds 
51111da177e4SLinus Torvalds 	return 0;
51121da177e4SLinus Torvalds }
51131da177e4SLinus Torvalds 
511455e957d8STakashi Iwai static int snd_hdsp_free(struct hdsp *hdsp)
51151da177e4SLinus Torvalds {
51161da177e4SLinus Torvalds 	if (hdsp->port) {
51171da177e4SLinus Torvalds 		/* stop the audio, and cancel all interrupts */
51181da177e4SLinus Torvalds 		tasklet_kill(&hdsp->midi_tasklet);
51191da177e4SLinus Torvalds 		hdsp->control_register &= ~(HDSP_Start|HDSP_AudioInterruptEnable|HDSP_Midi0InterruptEnable|HDSP_Midi1InterruptEnable);
51201da177e4SLinus Torvalds 		hdsp_write (hdsp, HDSP_controlRegister, hdsp->control_register);
51211da177e4SLinus Torvalds 	}
51221da177e4SLinus Torvalds 
51231da177e4SLinus Torvalds 	if (hdsp->irq >= 0)
51241da177e4SLinus Torvalds 		free_irq(hdsp->irq, (void *)hdsp);
51251da177e4SLinus Torvalds 
51261da177e4SLinus Torvalds 	snd_hdsp_free_buffers(hdsp);
51271da177e4SLinus Torvalds 
51281da177e4SLinus Torvalds 	if (hdsp->iobase)
51291da177e4SLinus Torvalds 		iounmap(hdsp->iobase);
51301da177e4SLinus Torvalds 
51311da177e4SLinus Torvalds 	if (hdsp->port)
51321da177e4SLinus Torvalds 		pci_release_regions(hdsp->pci);
51331da177e4SLinus Torvalds 
51341da177e4SLinus Torvalds 	pci_disable_device(hdsp->pci);
51351da177e4SLinus Torvalds 	return 0;
51361da177e4SLinus Torvalds }
51371da177e4SLinus Torvalds 
513855e957d8STakashi Iwai static void snd_hdsp_card_free(struct snd_card *card)
51391da177e4SLinus Torvalds {
514055e957d8STakashi Iwai 	struct hdsp *hdsp = (struct hdsp *) card->private_data;
51411da177e4SLinus Torvalds 
51421da177e4SLinus Torvalds 	if (hdsp)
51431da177e4SLinus Torvalds 		snd_hdsp_free(hdsp);
51441da177e4SLinus Torvalds }
51451da177e4SLinus Torvalds 
51461da177e4SLinus Torvalds static int __devinit snd_hdsp_probe(struct pci_dev *pci,
51471da177e4SLinus Torvalds 				    const struct pci_device_id *pci_id)
51481da177e4SLinus Torvalds {
51491da177e4SLinus Torvalds 	static int dev;
515055e957d8STakashi Iwai 	struct hdsp *hdsp;
515155e957d8STakashi Iwai 	struct snd_card *card;
51521da177e4SLinus Torvalds 	int err;
51531da177e4SLinus Torvalds 
51541da177e4SLinus Torvalds 	if (dev >= SNDRV_CARDS)
51551da177e4SLinus Torvalds 		return -ENODEV;
51561da177e4SLinus Torvalds 	if (!enable[dev]) {
51571da177e4SLinus Torvalds 		dev++;
51581da177e4SLinus Torvalds 		return -ENOENT;
51591da177e4SLinus Torvalds 	}
51601da177e4SLinus Torvalds 
5161*e58de7baSTakashi Iwai 	err = snd_card_create(index[dev], id[dev], THIS_MODULE,
5162*e58de7baSTakashi Iwai 			      sizeof(struct hdsp), &card);
5163*e58de7baSTakashi Iwai 	if (err < 0)
5164*e58de7baSTakashi Iwai 		return err;
51651da177e4SLinus Torvalds 
516655e957d8STakashi Iwai 	hdsp = (struct hdsp *) card->private_data;
51671da177e4SLinus Torvalds 	card->private_free = snd_hdsp_card_free;
51681da177e4SLinus Torvalds 	hdsp->dev = dev;
51691da177e4SLinus Torvalds 	hdsp->pci = pci;
51701da177e4SLinus Torvalds 	snd_card_set_dev(card, &pci->dev);
51711da177e4SLinus Torvalds 
51721da177e4SLinus Torvalds 	if ((err = snd_hdsp_create(card, hdsp)) < 0) {
51731da177e4SLinus Torvalds 		snd_card_free(card);
51741da177e4SLinus Torvalds 		return err;
51751da177e4SLinus Torvalds 	}
51761da177e4SLinus Torvalds 
51771da177e4SLinus Torvalds 	strcpy(card->shortname, "Hammerfall DSP");
51781da177e4SLinus Torvalds 	sprintf(card->longname, "%s at 0x%lx, irq %d", hdsp->card_name,
51791da177e4SLinus Torvalds 		hdsp->port, hdsp->irq);
51801da177e4SLinus Torvalds 
51811da177e4SLinus Torvalds 	if ((err = snd_card_register(card)) < 0) {
51821da177e4SLinus Torvalds 		snd_card_free(card);
51831da177e4SLinus Torvalds 		return err;
51841da177e4SLinus Torvalds 	}
51851da177e4SLinus Torvalds 	pci_set_drvdata(pci, card);
51861da177e4SLinus Torvalds 	dev++;
51871da177e4SLinus Torvalds 	return 0;
51881da177e4SLinus Torvalds }
51891da177e4SLinus Torvalds 
51901da177e4SLinus Torvalds static void __devexit snd_hdsp_remove(struct pci_dev *pci)
51911da177e4SLinus Torvalds {
51921da177e4SLinus Torvalds 	snd_card_free(pci_get_drvdata(pci));
51931da177e4SLinus Torvalds 	pci_set_drvdata(pci, NULL);
51941da177e4SLinus Torvalds }
51951da177e4SLinus Torvalds 
51961da177e4SLinus Torvalds static struct pci_driver driver = {
51971da177e4SLinus Torvalds 	.name =     "RME Hammerfall DSP",
51981da177e4SLinus Torvalds 	.id_table = snd_hdsp_ids,
51991da177e4SLinus Torvalds 	.probe =    snd_hdsp_probe,
52001da177e4SLinus Torvalds 	.remove = __devexit_p(snd_hdsp_remove),
52011da177e4SLinus Torvalds };
52021da177e4SLinus Torvalds 
52031da177e4SLinus Torvalds static int __init alsa_card_hdsp_init(void)
52041da177e4SLinus Torvalds {
520501d25d46STakashi Iwai 	return pci_register_driver(&driver);
52061da177e4SLinus Torvalds }
52071da177e4SLinus Torvalds 
52081da177e4SLinus Torvalds static void __exit alsa_card_hdsp_exit(void)
52091da177e4SLinus Torvalds {
52101da177e4SLinus Torvalds 	pci_unregister_driver(&driver);
52111da177e4SLinus Torvalds }
52121da177e4SLinus Torvalds 
52131da177e4SLinus Torvalds module_init(alsa_card_hdsp_init)
52141da177e4SLinus Torvalds module_exit(alsa_card_hdsp_exit)
5215