xref: /openbmc/linux/sound/pci/rme9652/hdsp.c (revision a3a68c85bfb7acc874ce6d334964b95943d4ed43)
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 <sound/driver.h>
251da177e4SLinus Torvalds #include <linux/init.h>
261da177e4SLinus Torvalds #include <linux/delay.h>
271da177e4SLinus Torvalds #include <linux/interrupt.h>
281da177e4SLinus Torvalds #include <linux/slab.h>
291da177e4SLinus Torvalds #include <linux/pci.h>
301da177e4SLinus Torvalds #include <linux/firmware.h>
311da177e4SLinus Torvalds #include <linux/moduleparam.h>
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds #include <sound/core.h>
341da177e4SLinus Torvalds #include <sound/control.h>
351da177e4SLinus Torvalds #include <sound/pcm.h>
361da177e4SLinus Torvalds #include <sound/info.h>
371da177e4SLinus Torvalds #include <sound/asoundef.h>
381da177e4SLinus Torvalds #include <sound/rawmidi.h>
391da177e4SLinus Torvalds #include <sound/hwdep.h>
401da177e4SLinus Torvalds #include <sound/initval.h>
411da177e4SLinus Torvalds #include <sound/hdsp.h>
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds #include <asm/byteorder.h>
441da177e4SLinus Torvalds #include <asm/current.h>
451da177e4SLinus Torvalds #include <asm/io.h>
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-MAX */
481da177e4SLinus Torvalds static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
491da177e4SLinus Torvalds static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;	/* Enable this card */
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds module_param_array(index, int, NULL, 0444);
521da177e4SLinus Torvalds MODULE_PARM_DESC(index, "Index value for RME Hammerfall DSP interface.");
531da177e4SLinus Torvalds module_param_array(id, charp, NULL, 0444);
541da177e4SLinus Torvalds MODULE_PARM_DESC(id, "ID string for RME Hammerfall DSP interface.");
551da177e4SLinus Torvalds module_param_array(enable, bool, NULL, 0444);
561da177e4SLinus Torvalds MODULE_PARM_DESC(enable, "Enable/disable specific Hammerfall DSP soundcards.");
571da177e4SLinus Torvalds MODULE_AUTHOR("Paul Davis <paul@linuxaudiosystems.com>, Marcus Andersson, Thomas Charbonnel <thomas@undata.org>");
581da177e4SLinus Torvalds MODULE_DESCRIPTION("RME Hammerfall DSP");
591da177e4SLinus Torvalds MODULE_LICENSE("GPL");
601da177e4SLinus Torvalds MODULE_SUPPORTED_DEVICE("{{RME Hammerfall-DSP},"
611da177e4SLinus Torvalds 	        "{RME HDSP-9652},"
621da177e4SLinus Torvalds 		"{RME HDSP-9632}}");
637e0af29dSClemens Ladisch #ifdef HDSP_FW_LOADER
647e0af29dSClemens Ladisch MODULE_FIRMWARE("multiface_firmware.bin");
657e0af29dSClemens Ladisch MODULE_FIRMWARE("multiface_firmware_rev11.bin");
667e0af29dSClemens Ladisch MODULE_FIRMWARE("digiface_firmware.bin");
677e0af29dSClemens Ladisch MODULE_FIRMWARE("digiface_firmware_rev11.bin");
687e0af29dSClemens Ladisch #endif
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds #define HDSP_MAX_CHANNELS        26
711da177e4SLinus Torvalds #define HDSP_MAX_DS_CHANNELS     14
721da177e4SLinus Torvalds #define HDSP_MAX_QS_CHANNELS     8
731da177e4SLinus Torvalds #define DIGIFACE_SS_CHANNELS     26
741da177e4SLinus Torvalds #define DIGIFACE_DS_CHANNELS     14
751da177e4SLinus Torvalds #define MULTIFACE_SS_CHANNELS    18
761da177e4SLinus Torvalds #define MULTIFACE_DS_CHANNELS    14
771da177e4SLinus Torvalds #define H9652_SS_CHANNELS        26
781da177e4SLinus Torvalds #define H9652_DS_CHANNELS        14
791da177e4SLinus Torvalds /* This does not include possible Analog Extension Boards
801da177e4SLinus Torvalds    AEBs are detected at card initialization
811da177e4SLinus Torvalds */
821da177e4SLinus Torvalds #define H9632_SS_CHANNELS	 12
831da177e4SLinus Torvalds #define H9632_DS_CHANNELS	 8
841da177e4SLinus Torvalds #define H9632_QS_CHANNELS	 4
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds /* Write registers. These are defined as byte-offsets from the iobase value.
871da177e4SLinus Torvalds  */
881da177e4SLinus Torvalds #define HDSP_resetPointer               0
89d7923b2aSRemy Bruno #define HDSP_freqReg			0
901da177e4SLinus Torvalds #define HDSP_outputBufferAddress	32
911da177e4SLinus Torvalds #define HDSP_inputBufferAddress		36
921da177e4SLinus Torvalds #define HDSP_controlRegister		64
931da177e4SLinus Torvalds #define HDSP_interruptConfirmation	96
941da177e4SLinus Torvalds #define HDSP_outputEnable	  	128
951da177e4SLinus Torvalds #define HDSP_control2Reg		256
961da177e4SLinus Torvalds #define HDSP_midiDataOut0  		352
971da177e4SLinus Torvalds #define HDSP_midiDataOut1  		356
981da177e4SLinus Torvalds #define HDSP_fifoData  			368
991da177e4SLinus Torvalds #define HDSP_inputEnable	 	384
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds /* Read registers. These are defined as byte-offsets from the iobase value
1021da177e4SLinus Torvalds  */
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds #define HDSP_statusRegister    0
1051da177e4SLinus Torvalds #define HDSP_timecode        128
1061da177e4SLinus Torvalds #define HDSP_status2Register 192
1071da177e4SLinus Torvalds #define HDSP_midiDataOut0    352
1081da177e4SLinus Torvalds #define HDSP_midiDataOut1    356
1091da177e4SLinus Torvalds #define HDSP_midiDataIn0     360
1101da177e4SLinus Torvalds #define HDSP_midiDataIn1     364
1111da177e4SLinus Torvalds #define HDSP_midiStatusOut0  384
1121da177e4SLinus Torvalds #define HDSP_midiStatusOut1  388
1131da177e4SLinus Torvalds #define HDSP_midiStatusIn0   392
1141da177e4SLinus Torvalds #define HDSP_midiStatusIn1   396
1151da177e4SLinus Torvalds #define HDSP_fifoStatus      400
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds /* the meters are regular i/o-mapped registers, but offset
1181da177e4SLinus Torvalds    considerably from the rest. the peak registers are reset
1191da177e4SLinus Torvalds    when read; the least-significant 4 bits are full-scale counters;
1201da177e4SLinus Torvalds    the actual peak value is in the most-significant 24 bits.
1211da177e4SLinus Torvalds */
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds #define HDSP_playbackPeakLevel  4096  /* 26 * 32 bit values */
1241da177e4SLinus Torvalds #define HDSP_inputPeakLevel     4224  /* 26 * 32 bit values */
1251da177e4SLinus Torvalds #define HDSP_outputPeakLevel    4352  /* (26+2) * 32 bit values */
1261da177e4SLinus Torvalds #define HDSP_playbackRmsLevel   4612  /* 26 * 64 bit values */
1271da177e4SLinus Torvalds #define HDSP_inputRmsLevel      4868  /* 26 * 64 bit values */
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds /* This is for H9652 cards
1311da177e4SLinus Torvalds    Peak values are read downward from the base
1321da177e4SLinus Torvalds    Rms values are read upward
1331da177e4SLinus Torvalds    There are rms values for the outputs too
1341da177e4SLinus Torvalds    26*3 values are read in ss mode
1351da177e4SLinus Torvalds    14*3 in ds mode, with no gap between values
1361da177e4SLinus Torvalds */
1371da177e4SLinus Torvalds #define HDSP_9652_peakBase	7164
1381da177e4SLinus Torvalds #define HDSP_9652_rmsBase	4096
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds /* c.f. the hdsp_9632_meters_t struct */
1411da177e4SLinus Torvalds #define HDSP_9632_metersBase	4096
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds #define HDSP_IO_EXTENT     7168
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds /* control2 register bits */
1461da177e4SLinus Torvalds 
1471da177e4SLinus Torvalds #define HDSP_TMS                0x01
1481da177e4SLinus Torvalds #define HDSP_TCK                0x02
1491da177e4SLinus Torvalds #define HDSP_TDI                0x04
1501da177e4SLinus Torvalds #define HDSP_JTAG               0x08
1511da177e4SLinus Torvalds #define HDSP_PWDN               0x10
1521da177e4SLinus Torvalds #define HDSP_PROGRAM	        0x020
1531da177e4SLinus Torvalds #define HDSP_CONFIG_MODE_0	0x040
1541da177e4SLinus Torvalds #define HDSP_CONFIG_MODE_1	0x080
1551da177e4SLinus Torvalds #define HDSP_VERSION_BIT	0x100
1561da177e4SLinus Torvalds #define HDSP_BIGENDIAN_MODE     0x200
1571da177e4SLinus Torvalds #define HDSP_RD_MULTIPLE        0x400
1581da177e4SLinus Torvalds #define HDSP_9652_ENABLE_MIXER  0x800
1591da177e4SLinus Torvalds #define HDSP_TDO                0x10000000
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds #define HDSP_S_PROGRAM     	(HDSP_PROGRAM|HDSP_CONFIG_MODE_0)
1621da177e4SLinus Torvalds #define HDSP_S_LOAD		(HDSP_PROGRAM|HDSP_CONFIG_MODE_1)
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds /* Control Register bits */
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds #define HDSP_Start                (1<<0)  /* start engine */
1671da177e4SLinus Torvalds #define HDSP_Latency0             (1<<1)  /* buffer size = 2^n where n is defined by Latency{2,1,0} */
1681da177e4SLinus Torvalds #define HDSP_Latency1             (1<<2)  /* [ see above ] */
1691da177e4SLinus Torvalds #define HDSP_Latency2             (1<<3)  /* [ see above ] */
1701da177e4SLinus Torvalds #define HDSP_ClockModeMaster      (1<<4)  /* 1=Master, 0=Slave/Autosync */
1711da177e4SLinus Torvalds #define HDSP_AudioInterruptEnable (1<<5)  /* what do you think ? */
1721da177e4SLinus Torvalds #define HDSP_Frequency0           (1<<6)  /* 0=44.1kHz/88.2kHz/176.4kHz 1=48kHz/96kHz/192kHz */
1731da177e4SLinus Torvalds #define HDSP_Frequency1           (1<<7)  /* 0=32kHz/64kHz/128kHz */
1741da177e4SLinus Torvalds #define HDSP_DoubleSpeed          (1<<8)  /* 0=normal speed, 1=double speed */
1751da177e4SLinus Torvalds #define HDSP_SPDIFProfessional    (1<<9)  /* 0=consumer, 1=professional */
1761da177e4SLinus Torvalds #define HDSP_SPDIFEmphasis        (1<<10) /* 0=none, 1=on */
1771da177e4SLinus Torvalds #define HDSP_SPDIFNonAudio        (1<<11) /* 0=off, 1=on */
1781da177e4SLinus Torvalds #define HDSP_SPDIFOpticalOut      (1<<12) /* 1=use 1st ADAT connector for SPDIF, 0=do not */
1791da177e4SLinus Torvalds #define HDSP_SyncRef2             (1<<13)
1801da177e4SLinus Torvalds #define HDSP_SPDIFInputSelect0    (1<<14)
1811da177e4SLinus Torvalds #define HDSP_SPDIFInputSelect1    (1<<15)
1821da177e4SLinus Torvalds #define HDSP_SyncRef0             (1<<16)
1831da177e4SLinus Torvalds #define HDSP_SyncRef1             (1<<17)
1841da177e4SLinus Torvalds #define HDSP_AnalogExtensionBoard (1<<18) /* For H9632 cards */
1851da177e4SLinus Torvalds #define HDSP_XLRBreakoutCable     (1<<20) /* For H9632 cards */
1861da177e4SLinus Torvalds #define HDSP_Midi0InterruptEnable (1<<22)
1871da177e4SLinus Torvalds #define HDSP_Midi1InterruptEnable (1<<23)
1881da177e4SLinus Torvalds #define HDSP_LineOut              (1<<24)
1891da177e4SLinus Torvalds #define HDSP_ADGain0		  (1<<25) /* From here : H9632 specific */
1901da177e4SLinus Torvalds #define HDSP_ADGain1		  (1<<26)
1911da177e4SLinus Torvalds #define HDSP_DAGain0		  (1<<27)
1921da177e4SLinus Torvalds #define HDSP_DAGain1		  (1<<28)
1931da177e4SLinus Torvalds #define HDSP_PhoneGain0		  (1<<29)
1941da177e4SLinus Torvalds #define HDSP_PhoneGain1		  (1<<30)
1951da177e4SLinus Torvalds #define HDSP_QuadSpeed	  	  (1<<31)
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds #define HDSP_ADGainMask       (HDSP_ADGain0|HDSP_ADGain1)
1981da177e4SLinus Torvalds #define HDSP_ADGainMinus10dBV  HDSP_ADGainMask
1991da177e4SLinus Torvalds #define HDSP_ADGainPlus4dBu   (HDSP_ADGain0)
2001da177e4SLinus Torvalds #define HDSP_ADGainLowGain     0
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds #define HDSP_DAGainMask         (HDSP_DAGain0|HDSP_DAGain1)
2031da177e4SLinus Torvalds #define HDSP_DAGainHighGain      HDSP_DAGainMask
2041da177e4SLinus Torvalds #define HDSP_DAGainPlus4dBu     (HDSP_DAGain0)
2051da177e4SLinus Torvalds #define HDSP_DAGainMinus10dBV    0
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds #define HDSP_PhoneGainMask      (HDSP_PhoneGain0|HDSP_PhoneGain1)
2081da177e4SLinus Torvalds #define HDSP_PhoneGain0dB        HDSP_PhoneGainMask
2091da177e4SLinus Torvalds #define HDSP_PhoneGainMinus6dB  (HDSP_PhoneGain0)
2101da177e4SLinus Torvalds #define HDSP_PhoneGainMinus12dB  0
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds #define HDSP_LatencyMask    (HDSP_Latency0|HDSP_Latency1|HDSP_Latency2)
2131da177e4SLinus Torvalds #define HDSP_FrequencyMask  (HDSP_Frequency0|HDSP_Frequency1|HDSP_DoubleSpeed|HDSP_QuadSpeed)
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds #define HDSP_SPDIFInputMask    (HDSP_SPDIFInputSelect0|HDSP_SPDIFInputSelect1)
2161da177e4SLinus Torvalds #define HDSP_SPDIFInputADAT1    0
2171da177e4SLinus Torvalds #define HDSP_SPDIFInputCoaxial (HDSP_SPDIFInputSelect0)
2181da177e4SLinus Torvalds #define HDSP_SPDIFInputCdrom   (HDSP_SPDIFInputSelect1)
2191da177e4SLinus Torvalds #define HDSP_SPDIFInputAES     (HDSP_SPDIFInputSelect0|HDSP_SPDIFInputSelect1)
2201da177e4SLinus Torvalds 
2211da177e4SLinus Torvalds #define HDSP_SyncRefMask        (HDSP_SyncRef0|HDSP_SyncRef1|HDSP_SyncRef2)
2221da177e4SLinus Torvalds #define HDSP_SyncRef_ADAT1       0
2231da177e4SLinus Torvalds #define HDSP_SyncRef_ADAT2      (HDSP_SyncRef0)
2241da177e4SLinus Torvalds #define HDSP_SyncRef_ADAT3      (HDSP_SyncRef1)
2251da177e4SLinus Torvalds #define HDSP_SyncRef_SPDIF      (HDSP_SyncRef0|HDSP_SyncRef1)
2261da177e4SLinus Torvalds #define HDSP_SyncRef_WORD       (HDSP_SyncRef2)
2271da177e4SLinus Torvalds #define HDSP_SyncRef_ADAT_SYNC  (HDSP_SyncRef0|HDSP_SyncRef2)
2281da177e4SLinus Torvalds 
2291da177e4SLinus Torvalds /* Sample Clock Sources */
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds #define HDSP_CLOCK_SOURCE_AUTOSYNC           0
2321da177e4SLinus Torvalds #define HDSP_CLOCK_SOURCE_INTERNAL_32KHZ     1
2331da177e4SLinus Torvalds #define HDSP_CLOCK_SOURCE_INTERNAL_44_1KHZ   2
2341da177e4SLinus Torvalds #define HDSP_CLOCK_SOURCE_INTERNAL_48KHZ     3
2351da177e4SLinus Torvalds #define HDSP_CLOCK_SOURCE_INTERNAL_64KHZ     4
2361da177e4SLinus Torvalds #define HDSP_CLOCK_SOURCE_INTERNAL_88_2KHZ   5
2371da177e4SLinus Torvalds #define HDSP_CLOCK_SOURCE_INTERNAL_96KHZ     6
2381da177e4SLinus Torvalds #define HDSP_CLOCK_SOURCE_INTERNAL_128KHZ    7
2391da177e4SLinus Torvalds #define HDSP_CLOCK_SOURCE_INTERNAL_176_4KHZ  8
2401da177e4SLinus Torvalds #define HDSP_CLOCK_SOURCE_INTERNAL_192KHZ    9
2411da177e4SLinus Torvalds 
2421da177e4SLinus Torvalds /* Preferred sync reference choices - used by "pref_sync_ref" control switch */
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds #define HDSP_SYNC_FROM_WORD      0
2451da177e4SLinus Torvalds #define HDSP_SYNC_FROM_SPDIF     1
2461da177e4SLinus Torvalds #define HDSP_SYNC_FROM_ADAT1     2
2471da177e4SLinus Torvalds #define HDSP_SYNC_FROM_ADAT_SYNC 3
2481da177e4SLinus Torvalds #define HDSP_SYNC_FROM_ADAT2     4
2491da177e4SLinus Torvalds #define HDSP_SYNC_FROM_ADAT3     5
2501da177e4SLinus Torvalds 
2511da177e4SLinus Torvalds /* SyncCheck status */
2521da177e4SLinus Torvalds 
2531da177e4SLinus Torvalds #define HDSP_SYNC_CHECK_NO_LOCK 0
2541da177e4SLinus Torvalds #define HDSP_SYNC_CHECK_LOCK    1
2551da177e4SLinus Torvalds #define HDSP_SYNC_CHECK_SYNC	2
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds /* AutoSync references - used by "autosync_ref" control switch */
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds #define HDSP_AUTOSYNC_FROM_WORD      0
2601da177e4SLinus Torvalds #define HDSP_AUTOSYNC_FROM_ADAT_SYNC 1
2611da177e4SLinus Torvalds #define HDSP_AUTOSYNC_FROM_SPDIF     2
2621da177e4SLinus Torvalds #define HDSP_AUTOSYNC_FROM_NONE	     3
2631da177e4SLinus Torvalds #define HDSP_AUTOSYNC_FROM_ADAT1     4
2641da177e4SLinus Torvalds #define HDSP_AUTOSYNC_FROM_ADAT2     5
2651da177e4SLinus Torvalds #define HDSP_AUTOSYNC_FROM_ADAT3     6
2661da177e4SLinus Torvalds 
2671da177e4SLinus Torvalds /* Possible sources of S/PDIF input */
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds #define HDSP_SPDIFIN_OPTICAL  0	/* optical  (ADAT1) */
2701da177e4SLinus Torvalds #define HDSP_SPDIFIN_COAXIAL  1	/* coaxial (RCA) */
2711da177e4SLinus Torvalds #define HDSP_SPDIFIN_INTERNAL 2	/* internal (CDROM) */
2721da177e4SLinus Torvalds #define HDSP_SPDIFIN_AES      3 /* xlr for H9632 (AES)*/
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds #define HDSP_Frequency32KHz    HDSP_Frequency0
2751da177e4SLinus Torvalds #define HDSP_Frequency44_1KHz  HDSP_Frequency1
2761da177e4SLinus Torvalds #define HDSP_Frequency48KHz    (HDSP_Frequency1|HDSP_Frequency0)
2771da177e4SLinus Torvalds #define HDSP_Frequency64KHz    (HDSP_DoubleSpeed|HDSP_Frequency0)
2781da177e4SLinus Torvalds #define HDSP_Frequency88_2KHz  (HDSP_DoubleSpeed|HDSP_Frequency1)
2791da177e4SLinus Torvalds #define HDSP_Frequency96KHz    (HDSP_DoubleSpeed|HDSP_Frequency1|HDSP_Frequency0)
2801da177e4SLinus Torvalds /* For H9632 cards */
2811da177e4SLinus Torvalds #define HDSP_Frequency128KHz   (HDSP_QuadSpeed|HDSP_DoubleSpeed|HDSP_Frequency0)
2821da177e4SLinus Torvalds #define HDSP_Frequency176_4KHz (HDSP_QuadSpeed|HDSP_DoubleSpeed|HDSP_Frequency1)
2831da177e4SLinus Torvalds #define HDSP_Frequency192KHz   (HDSP_QuadSpeed|HDSP_DoubleSpeed|HDSP_Frequency1|HDSP_Frequency0)
284e4b6088cSJulian Cable /* RME says n = 104857600000000, but in the windows MADI driver, I see:
285e4b6088cSJulian Cable 	return 104857600000000 / rate; // 100 MHz
286e4b6088cSJulian Cable 	return 110100480000000 / rate; // 105 MHz
287e4b6088cSJulian Cable */
288e4b6088cSJulian Cable #define DDS_NUMERATOR 104857600000000ULL;  /*  =  2^20 * 10^8 */
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds #define hdsp_encode_latency(x)       (((x)<<1) & HDSP_LatencyMask)
2911da177e4SLinus Torvalds #define hdsp_decode_latency(x)       (((x) & HDSP_LatencyMask)>>1)
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds #define hdsp_encode_spdif_in(x) (((x)&0x3)<<14)
2941da177e4SLinus Torvalds #define hdsp_decode_spdif_in(x) (((x)>>14)&0x3)
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds /* Status Register bits */
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds #define HDSP_audioIRQPending    (1<<0)
2991da177e4SLinus Torvalds #define HDSP_Lock2              (1<<1)     /* this is for Digiface and H9652 */
3001da177e4SLinus Torvalds #define HDSP_spdifFrequency3	HDSP_Lock2 /* this is for H9632 only */
3011da177e4SLinus Torvalds #define HDSP_Lock1              (1<<2)
3021da177e4SLinus Torvalds #define HDSP_Lock0              (1<<3)
3031da177e4SLinus Torvalds #define HDSP_SPDIFSync          (1<<4)
3041da177e4SLinus Torvalds #define HDSP_TimecodeLock       (1<<5)
3051da177e4SLinus Torvalds #define HDSP_BufferPositionMask 0x000FFC0 /* Bit 6..15 : h/w buffer pointer */
3061da177e4SLinus Torvalds #define HDSP_Sync2              (1<<16)
3071da177e4SLinus Torvalds #define HDSP_Sync1              (1<<17)
3081da177e4SLinus Torvalds #define HDSP_Sync0              (1<<18)
3091da177e4SLinus Torvalds #define HDSP_DoubleSpeedStatus  (1<<19)
3101da177e4SLinus Torvalds #define HDSP_ConfigError        (1<<20)
3111da177e4SLinus Torvalds #define HDSP_DllError           (1<<21)
3121da177e4SLinus Torvalds #define HDSP_spdifFrequency0    (1<<22)
3131da177e4SLinus Torvalds #define HDSP_spdifFrequency1    (1<<23)
3141da177e4SLinus Torvalds #define HDSP_spdifFrequency2    (1<<24)
3151da177e4SLinus Torvalds #define HDSP_SPDIFErrorFlag     (1<<25)
3161da177e4SLinus Torvalds #define HDSP_BufferID           (1<<26)
3171da177e4SLinus Torvalds #define HDSP_TimecodeSync       (1<<27)
3181da177e4SLinus Torvalds #define HDSP_AEBO          	(1<<28) /* H9632 specific Analog Extension Boards */
3191da177e4SLinus Torvalds #define HDSP_AEBI		(1<<29) /* 0 = present, 1 = absent */
3201da177e4SLinus Torvalds #define HDSP_midi0IRQPending    (1<<30)
3211da177e4SLinus Torvalds #define HDSP_midi1IRQPending    (1<<31)
3221da177e4SLinus Torvalds 
3231da177e4SLinus Torvalds #define HDSP_spdifFrequencyMask    (HDSP_spdifFrequency0|HDSP_spdifFrequency1|HDSP_spdifFrequency2)
3241da177e4SLinus Torvalds 
3251da177e4SLinus Torvalds #define HDSP_spdifFrequency32KHz   (HDSP_spdifFrequency0)
3261da177e4SLinus Torvalds #define HDSP_spdifFrequency44_1KHz (HDSP_spdifFrequency1)
3271da177e4SLinus Torvalds #define HDSP_spdifFrequency48KHz   (HDSP_spdifFrequency0|HDSP_spdifFrequency1)
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds #define HDSP_spdifFrequency64KHz   (HDSP_spdifFrequency2)
3301da177e4SLinus Torvalds #define HDSP_spdifFrequency88_2KHz (HDSP_spdifFrequency0|HDSP_spdifFrequency2)
3311da177e4SLinus Torvalds #define HDSP_spdifFrequency96KHz   (HDSP_spdifFrequency2|HDSP_spdifFrequency1)
3321da177e4SLinus Torvalds 
3331da177e4SLinus Torvalds /* This is for H9632 cards */
3341da177e4SLinus Torvalds #define HDSP_spdifFrequency128KHz   HDSP_spdifFrequencyMask
3351da177e4SLinus Torvalds #define HDSP_spdifFrequency176_4KHz HDSP_spdifFrequency3
3361da177e4SLinus Torvalds #define HDSP_spdifFrequency192KHz   (HDSP_spdifFrequency3|HDSP_spdifFrequency0)
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds /* Status2 Register bits */
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds #define HDSP_version0     (1<<0)
3411da177e4SLinus Torvalds #define HDSP_version1     (1<<1)
3421da177e4SLinus Torvalds #define HDSP_version2     (1<<2)
3431da177e4SLinus Torvalds #define HDSP_wc_lock      (1<<3)
3441da177e4SLinus Torvalds #define HDSP_wc_sync      (1<<4)
3451da177e4SLinus Torvalds #define HDSP_inp_freq0    (1<<5)
3461da177e4SLinus Torvalds #define HDSP_inp_freq1    (1<<6)
3471da177e4SLinus Torvalds #define HDSP_inp_freq2    (1<<7)
3481da177e4SLinus Torvalds #define HDSP_SelSyncRef0  (1<<8)
3491da177e4SLinus Torvalds #define HDSP_SelSyncRef1  (1<<9)
3501da177e4SLinus Torvalds #define HDSP_SelSyncRef2  (1<<10)
3511da177e4SLinus Torvalds 
3521da177e4SLinus Torvalds #define HDSP_wc_valid (HDSP_wc_lock|HDSP_wc_sync)
3531da177e4SLinus Torvalds 
3541da177e4SLinus Torvalds #define HDSP_systemFrequencyMask (HDSP_inp_freq0|HDSP_inp_freq1|HDSP_inp_freq2)
3551da177e4SLinus Torvalds #define HDSP_systemFrequency32   (HDSP_inp_freq0)
3561da177e4SLinus Torvalds #define HDSP_systemFrequency44_1 (HDSP_inp_freq1)
3571da177e4SLinus Torvalds #define HDSP_systemFrequency48   (HDSP_inp_freq0|HDSP_inp_freq1)
3581da177e4SLinus Torvalds #define HDSP_systemFrequency64   (HDSP_inp_freq2)
3591da177e4SLinus Torvalds #define HDSP_systemFrequency88_2 (HDSP_inp_freq0|HDSP_inp_freq2)
3601da177e4SLinus Torvalds #define HDSP_systemFrequency96   (HDSP_inp_freq1|HDSP_inp_freq2)
3611da177e4SLinus Torvalds /* FIXME : more values for 9632 cards ? */
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds #define HDSP_SelSyncRefMask        (HDSP_SelSyncRef0|HDSP_SelSyncRef1|HDSP_SelSyncRef2)
3641da177e4SLinus Torvalds #define HDSP_SelSyncRef_ADAT1      0
3651da177e4SLinus Torvalds #define HDSP_SelSyncRef_ADAT2      (HDSP_SelSyncRef0)
3661da177e4SLinus Torvalds #define HDSP_SelSyncRef_ADAT3      (HDSP_SelSyncRef1)
3671da177e4SLinus Torvalds #define HDSP_SelSyncRef_SPDIF      (HDSP_SelSyncRef0|HDSP_SelSyncRef1)
3681da177e4SLinus Torvalds #define HDSP_SelSyncRef_WORD       (HDSP_SelSyncRef2)
3691da177e4SLinus Torvalds #define HDSP_SelSyncRef_ADAT_SYNC  (HDSP_SelSyncRef0|HDSP_SelSyncRef2)
3701da177e4SLinus Torvalds 
3711da177e4SLinus Torvalds /* Card state flags */
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds #define HDSP_InitializationComplete  (1<<0)
3741da177e4SLinus Torvalds #define HDSP_FirmwareLoaded	     (1<<1)
3751da177e4SLinus Torvalds #define HDSP_FirmwareCached	     (1<<2)
3761da177e4SLinus Torvalds 
3771da177e4SLinus Torvalds /* FIFO wait times, defined in terms of 1/10ths of msecs */
3781da177e4SLinus Torvalds 
3791da177e4SLinus Torvalds #define HDSP_LONG_WAIT	 5000
3801da177e4SLinus Torvalds #define HDSP_SHORT_WAIT  30
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds #define UNITY_GAIN                       32768
3831da177e4SLinus Torvalds #define MINUS_INFINITY_GAIN              0
3841da177e4SLinus Torvalds 
3851da177e4SLinus Torvalds /* the size of a substream (1 mono data stream) */
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds #define HDSP_CHANNEL_BUFFER_SAMPLES  (16*1024)
3881da177e4SLinus Torvalds #define HDSP_CHANNEL_BUFFER_BYTES    (4*HDSP_CHANNEL_BUFFER_SAMPLES)
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds /* the size of the area we need to allocate for DMA transfers. the
3911da177e4SLinus Torvalds    size is the same regardless of the number of channels - the
3921da177e4SLinus Torvalds    Multiface still uses the same memory area.
3931da177e4SLinus Torvalds 
3941da177e4SLinus Torvalds    Note that we allocate 1 more channel than is apparently needed
3951da177e4SLinus Torvalds    because the h/w seems to write 1 byte beyond the end of the last
3961da177e4SLinus Torvalds    page. Sigh.
3971da177e4SLinus Torvalds */
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds #define HDSP_DMA_AREA_BYTES ((HDSP_MAX_CHANNELS+1) * HDSP_CHANNEL_BUFFER_BYTES)
4001da177e4SLinus Torvalds #define HDSP_DMA_AREA_KILOBYTES (HDSP_DMA_AREA_BYTES/1024)
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds /* use hotplug firmeare loader? */
4031da177e4SLinus Torvalds #if defined(CONFIG_FW_LOADER) || defined(CONFIG_FW_LOADER_MODULE)
404140432fdSTakashi Iwai #if !defined(HDSP_USE_HWDEP_LOADER) && !defined(CONFIG_SND_HDSP)
4051da177e4SLinus Torvalds #define HDSP_FW_LOADER
4061da177e4SLinus Torvalds #endif
4071da177e4SLinus Torvalds #endif
4081da177e4SLinus Torvalds 
40955e957d8STakashi Iwai struct hdsp_9632_meters {
4101da177e4SLinus Torvalds     u32 input_peak[16];
4111da177e4SLinus Torvalds     u32 playback_peak[16];
4121da177e4SLinus Torvalds     u32 output_peak[16];
4131da177e4SLinus Torvalds     u32 xxx_peak[16];
4141da177e4SLinus Torvalds     u32 padding[64];
4151da177e4SLinus Torvalds     u32 input_rms_low[16];
4161da177e4SLinus Torvalds     u32 playback_rms_low[16];
4171da177e4SLinus Torvalds     u32 output_rms_low[16];
4181da177e4SLinus Torvalds     u32 xxx_rms_low[16];
4191da177e4SLinus Torvalds     u32 input_rms_high[16];
4201da177e4SLinus Torvalds     u32 playback_rms_high[16];
4211da177e4SLinus Torvalds     u32 output_rms_high[16];
4221da177e4SLinus Torvalds     u32 xxx_rms_high[16];
4231da177e4SLinus Torvalds };
4241da177e4SLinus Torvalds 
42555e957d8STakashi Iwai struct hdsp_midi {
42655e957d8STakashi Iwai     struct hdsp             *hdsp;
4271da177e4SLinus Torvalds     int                      id;
42855e957d8STakashi Iwai     struct snd_rawmidi           *rmidi;
42955e957d8STakashi Iwai     struct snd_rawmidi_substream *input;
43055e957d8STakashi Iwai     struct snd_rawmidi_substream *output;
4311da177e4SLinus Torvalds     char                     istimer; /* timer in use */
4321da177e4SLinus Torvalds     struct timer_list	     timer;
4331da177e4SLinus Torvalds     spinlock_t               lock;
4341da177e4SLinus Torvalds     int			     pending;
4351da177e4SLinus Torvalds };
4361da177e4SLinus Torvalds 
43755e957d8STakashi Iwai struct hdsp {
4381da177e4SLinus Torvalds 	spinlock_t            lock;
43955e957d8STakashi Iwai 	struct snd_pcm_substream *capture_substream;
44055e957d8STakashi Iwai 	struct snd_pcm_substream *playback_substream;
44155e957d8STakashi Iwai         struct hdsp_midi      midi[2];
4421da177e4SLinus Torvalds 	struct tasklet_struct midi_tasklet;
4431da177e4SLinus Torvalds 	int		      use_midi_tasklet;
4441da177e4SLinus Torvalds 	int                   precise_ptr;
4451da177e4SLinus Torvalds 	u32                   control_register;	     /* cached value */
4461da177e4SLinus Torvalds 	u32                   control2_register;     /* cached value */
4471da177e4SLinus Torvalds 	u32                   creg_spdif;
4481da177e4SLinus Torvalds 	u32                   creg_spdif_stream;
449e3ea4d89STakashi Iwai 	int                   clock_source_locked;
4501da177e4SLinus Torvalds 	char                 *card_name;	     /* digiface/multiface */
45155e957d8STakashi Iwai 	enum HDSP_IO_Type     io_type;               /* ditto, but for code use */
4521da177e4SLinus Torvalds         unsigned short        firmware_rev;
4531da177e4SLinus Torvalds 	unsigned short	      state;		     /* stores state bits */
4541da177e4SLinus Torvalds 	u32		      firmware_cache[24413]; /* this helps recover from accidental iobox power failure */
4551da177e4SLinus Torvalds 	size_t                period_bytes; 	     /* guess what this is */
4561da177e4SLinus Torvalds 	unsigned char	      max_channels;
4571da177e4SLinus Torvalds 	unsigned char	      qs_in_channels;	     /* quad speed mode for H9632 */
4581da177e4SLinus Torvalds 	unsigned char         ds_in_channels;
4591da177e4SLinus Torvalds 	unsigned char         ss_in_channels;	    /* different for multiface/digiface */
4601da177e4SLinus Torvalds 	unsigned char	      qs_out_channels;
4611da177e4SLinus Torvalds 	unsigned char         ds_out_channels;
4621da177e4SLinus Torvalds 	unsigned char         ss_out_channels;
4631da177e4SLinus Torvalds 
4641da177e4SLinus Torvalds 	struct snd_dma_buffer capture_dma_buf;
4651da177e4SLinus Torvalds 	struct snd_dma_buffer playback_dma_buf;
4661da177e4SLinus Torvalds 	unsigned char        *capture_buffer;	    /* suitably aligned address */
4671da177e4SLinus Torvalds 	unsigned char        *playback_buffer;	    /* suitably aligned address */
4681da177e4SLinus Torvalds 
4691da177e4SLinus Torvalds 	pid_t                 capture_pid;
4701da177e4SLinus Torvalds 	pid_t                 playback_pid;
4711da177e4SLinus Torvalds 	int                   running;
4721da177e4SLinus Torvalds 	int                   system_sample_rate;
4731da177e4SLinus Torvalds 	char                 *channel_map;
4741da177e4SLinus Torvalds 	int                   dev;
4751da177e4SLinus Torvalds 	int                   irq;
4761da177e4SLinus Torvalds 	unsigned long         port;
4771da177e4SLinus Torvalds         void __iomem         *iobase;
47855e957d8STakashi Iwai 	struct snd_card *card;
47955e957d8STakashi Iwai 	struct snd_pcm *pcm;
48055e957d8STakashi Iwai 	struct snd_hwdep          *hwdep;
4811da177e4SLinus Torvalds 	struct pci_dev       *pci;
48255e957d8STakashi Iwai 	struct snd_kcontrol *spdif_ctl;
4831da177e4SLinus Torvalds         unsigned short        mixer_matrix[HDSP_MATRIX_MIXER_SIZE];
484d7923b2aSRemy Bruno 	unsigned int          dds_value; /* last value written to freq register */
4851da177e4SLinus Torvalds };
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds /* These tables map the ALSA channels 1..N to the channels that we
4881da177e4SLinus Torvalds    need to use in order to find the relevant channel buffer. RME
4891da177e4SLinus Torvalds    refer to this kind of mapping as between "the ADAT channel and
4901da177e4SLinus Torvalds    the DMA channel." We index it using the logical audio channel,
4911da177e4SLinus Torvalds    and the value is the DMA channel (i.e. channel buffer number)
4921da177e4SLinus Torvalds    where the data for that channel can be read/written from/to.
4931da177e4SLinus Torvalds */
4941da177e4SLinus Torvalds 
4951da177e4SLinus Torvalds static char channel_map_df_ss[HDSP_MAX_CHANNELS] = {
4961da177e4SLinus Torvalds 	0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
4971da177e4SLinus Torvalds 	18, 19, 20, 21, 22, 23, 24, 25
4981da177e4SLinus Torvalds };
4991da177e4SLinus Torvalds 
5001da177e4SLinus Torvalds static char channel_map_mf_ss[HDSP_MAX_CHANNELS] = { /* Multiface */
5011da177e4SLinus Torvalds 	/* Analog */
5021da177e4SLinus Torvalds 	0, 1, 2, 3, 4, 5, 6, 7,
5031da177e4SLinus Torvalds 	/* ADAT 2 */
5041da177e4SLinus Torvalds 	16, 17, 18, 19, 20, 21, 22, 23,
5051da177e4SLinus Torvalds 	/* SPDIF */
5061da177e4SLinus Torvalds 	24, 25,
5071da177e4SLinus Torvalds 	-1, -1, -1, -1, -1, -1, -1, -1
5081da177e4SLinus Torvalds };
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds static char channel_map_ds[HDSP_MAX_CHANNELS] = {
5111da177e4SLinus Torvalds 	/* ADAT channels are remapped */
5121da177e4SLinus Torvalds 	1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23,
5131da177e4SLinus Torvalds 	/* channels 12 and 13 are S/PDIF */
5141da177e4SLinus Torvalds 	24, 25,
5151da177e4SLinus Torvalds 	/* others don't exist */
5161da177e4SLinus Torvalds 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
5171da177e4SLinus Torvalds };
5181da177e4SLinus Torvalds 
5191da177e4SLinus Torvalds static char channel_map_H9632_ss[HDSP_MAX_CHANNELS] = {
5201da177e4SLinus Torvalds 	/* ADAT channels */
5211da177e4SLinus Torvalds 	0, 1, 2, 3, 4, 5, 6, 7,
5221da177e4SLinus Torvalds 	/* SPDIF */
5231da177e4SLinus Torvalds 	8, 9,
5241da177e4SLinus Torvalds 	/* Analog */
5251da177e4SLinus Torvalds 	10, 11,
5261da177e4SLinus Torvalds 	/* AO4S-192 and AI4S-192 extension boards */
5271da177e4SLinus Torvalds 	12, 13, 14, 15,
5281da177e4SLinus Torvalds 	/* others don't exist */
5291da177e4SLinus Torvalds 	-1, -1, -1, -1, -1, -1, -1, -1,
5301da177e4SLinus Torvalds 	-1, -1
5311da177e4SLinus Torvalds };
5321da177e4SLinus Torvalds 
5331da177e4SLinus Torvalds static char channel_map_H9632_ds[HDSP_MAX_CHANNELS] = {
5341da177e4SLinus Torvalds 	/* ADAT */
5351da177e4SLinus Torvalds 	1, 3, 5, 7,
5361da177e4SLinus Torvalds 	/* SPDIF */
5371da177e4SLinus Torvalds 	8, 9,
5381da177e4SLinus Torvalds 	/* Analog */
5391da177e4SLinus Torvalds 	10, 11,
5401da177e4SLinus Torvalds 	/* AO4S-192 and AI4S-192 extension boards */
5411da177e4SLinus Torvalds 	12, 13, 14, 15,
5421da177e4SLinus Torvalds 	/* others don't exist */
5431da177e4SLinus Torvalds 	-1, -1, -1, -1, -1, -1, -1, -1,
5441da177e4SLinus Torvalds 	-1, -1, -1, -1, -1, -1
5451da177e4SLinus Torvalds };
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds static char channel_map_H9632_qs[HDSP_MAX_CHANNELS] = {
5481da177e4SLinus Torvalds 	/* ADAT is disabled in this mode */
5491da177e4SLinus Torvalds 	/* SPDIF */
5501da177e4SLinus Torvalds 	8, 9,
5511da177e4SLinus Torvalds 	/* Analog */
5521da177e4SLinus Torvalds 	10, 11,
5531da177e4SLinus Torvalds 	/* AO4S-192 and AI4S-192 extension boards */
5541da177e4SLinus Torvalds 	12, 13, 14, 15,
5551da177e4SLinus Torvalds 	/* others don't exist */
5561da177e4SLinus Torvalds 	-1, -1, -1, -1, -1, -1, -1, -1,
5571da177e4SLinus Torvalds 	-1, -1, -1, -1, -1, -1, -1, -1,
5581da177e4SLinus Torvalds 	-1, -1
5591da177e4SLinus Torvalds };
5601da177e4SLinus Torvalds 
5611da177e4SLinus Torvalds static int snd_hammerfall_get_buffer(struct pci_dev *pci, struct snd_dma_buffer *dmab, size_t size)
5621da177e4SLinus Torvalds {
5631da177e4SLinus Torvalds 	dmab->dev.type = SNDRV_DMA_TYPE_DEV;
5641da177e4SLinus Torvalds 	dmab->dev.dev = snd_dma_pci_data(pci);
565b6a96915STakashi Iwai 	if (snd_dma_get_reserved_buf(dmab, snd_dma_pci_buf_id(pci))) {
566b6a96915STakashi Iwai 		if (dmab->bytes >= size)
567b6a96915STakashi Iwai 			return 0;
568b6a96915STakashi Iwai 	}
5691da177e4SLinus Torvalds 	if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
5701da177e4SLinus Torvalds 				size, dmab) < 0)
5711da177e4SLinus Torvalds 		return -ENOMEM;
5721da177e4SLinus Torvalds 	return 0;
5731da177e4SLinus Torvalds }
5741da177e4SLinus Torvalds 
5751da177e4SLinus Torvalds static void snd_hammerfall_free_buffer(struct snd_dma_buffer *dmab, struct pci_dev *pci)
5761da177e4SLinus Torvalds {
577b6a96915STakashi Iwai 	if (dmab->area) {
578b6a96915STakashi Iwai 		dmab->dev.dev = NULL; /* make it anonymous */
5791da177e4SLinus Torvalds 		snd_dma_reserve_buf(dmab, snd_dma_pci_buf_id(pci));
5801da177e4SLinus Torvalds 	}
581b6a96915STakashi Iwai }
5821da177e4SLinus Torvalds 
5831da177e4SLinus Torvalds 
584f40b6890STakashi Iwai static struct pci_device_id snd_hdsp_ids[] = {
5851da177e4SLinus Torvalds 	{
5861da177e4SLinus Torvalds 		.vendor = PCI_VENDOR_ID_XILINX,
5871da177e4SLinus Torvalds 		.device = PCI_DEVICE_ID_XILINX_HAMMERFALL_DSP,
5881da177e4SLinus Torvalds 		.subvendor = PCI_ANY_ID,
5891da177e4SLinus Torvalds 		.subdevice = PCI_ANY_ID,
5901da177e4SLinus Torvalds 	}, /* RME Hammerfall-DSP */
5911da177e4SLinus Torvalds 	{ 0, },
5921da177e4SLinus Torvalds };
5931da177e4SLinus Torvalds 
5941da177e4SLinus Torvalds MODULE_DEVICE_TABLE(pci, snd_hdsp_ids);
5951da177e4SLinus Torvalds 
5961da177e4SLinus Torvalds /* prototypes */
59755e957d8STakashi Iwai static int snd_hdsp_create_alsa_devices(struct snd_card *card, struct hdsp *hdsp);
59855e957d8STakashi Iwai static int snd_hdsp_create_pcm(struct snd_card *card, struct hdsp *hdsp);
59955e957d8STakashi Iwai static int snd_hdsp_enable_io (struct hdsp *hdsp);
60055e957d8STakashi Iwai static void snd_hdsp_initialize_midi_flush (struct hdsp *hdsp);
60155e957d8STakashi Iwai static void snd_hdsp_initialize_channels (struct hdsp *hdsp);
60255e957d8STakashi Iwai static int hdsp_fifo_wait(struct hdsp *hdsp, int count, int timeout);
60355e957d8STakashi Iwai static int hdsp_autosync_ref(struct hdsp *hdsp);
60455e957d8STakashi Iwai static int snd_hdsp_set_defaults(struct hdsp *hdsp);
60555e957d8STakashi Iwai static void snd_hdsp_9652_enable_mixer (struct hdsp *hdsp);
6061da177e4SLinus Torvalds 
60755e957d8STakashi Iwai static int hdsp_playback_to_output_key (struct hdsp *hdsp, int in, int out)
6081da177e4SLinus Torvalds {
609*a3a68c85SRemy Bruno 	switch (hdsp->io_type) {
610*a3a68c85SRemy Bruno 	case Multiface:
611*a3a68c85SRemy Bruno 	case Digiface:
6121da177e4SLinus Torvalds 	default:
613*a3a68c85SRemy Bruno 		return (64 * out) + (32 + (in));
614*a3a68c85SRemy Bruno 	case H9632:
615*a3a68c85SRemy Bruno 		return (32 * out) + (16 + (in));
616*a3a68c85SRemy Bruno 	case H9652:
6171da177e4SLinus Torvalds 		return (52 * out) + (26 + (in));
6181da177e4SLinus Torvalds 	}
6191da177e4SLinus Torvalds }
6201da177e4SLinus Torvalds 
62155e957d8STakashi Iwai static int hdsp_input_to_output_key (struct hdsp *hdsp, int in, int out)
6221da177e4SLinus Torvalds {
623*a3a68c85SRemy Bruno 	switch (hdsp->io_type) {
624*a3a68c85SRemy Bruno 	case Multiface:
625*a3a68c85SRemy Bruno 	case Digiface:
6261da177e4SLinus Torvalds 	default:
627*a3a68c85SRemy Bruno 		return (64 * out) + in;
628*a3a68c85SRemy Bruno 	case H9632:
629*a3a68c85SRemy Bruno 		return (32 * out) + in;
630*a3a68c85SRemy Bruno 	case H9652:
6311da177e4SLinus Torvalds 		return (52 * out) + in;
6321da177e4SLinus Torvalds 	}
6331da177e4SLinus Torvalds }
6341da177e4SLinus Torvalds 
63555e957d8STakashi Iwai static void hdsp_write(struct hdsp *hdsp, int reg, int val)
6361da177e4SLinus Torvalds {
6371da177e4SLinus Torvalds 	writel(val, hdsp->iobase + reg);
6381da177e4SLinus Torvalds }
6391da177e4SLinus Torvalds 
64055e957d8STakashi Iwai static unsigned int hdsp_read(struct hdsp *hdsp, int reg)
6411da177e4SLinus Torvalds {
6421da177e4SLinus Torvalds 	return readl (hdsp->iobase + reg);
6431da177e4SLinus Torvalds }
6441da177e4SLinus Torvalds 
64555e957d8STakashi Iwai static int hdsp_check_for_iobox (struct hdsp *hdsp)
6461da177e4SLinus Torvalds {
6471da177e4SLinus Torvalds 
6481da177e4SLinus Torvalds 	if (hdsp->io_type == H9652 || hdsp->io_type == H9632) return 0;
6491da177e4SLinus Torvalds 	if (hdsp_read (hdsp, HDSP_statusRegister) & HDSP_ConfigError) {
6501da177e4SLinus Torvalds 		snd_printk ("Hammerfall-DSP: no Digiface or Multiface connected!\n");
6511da177e4SLinus Torvalds 		hdsp->state &= ~HDSP_FirmwareLoaded;
6521da177e4SLinus Torvalds 		return -EIO;
6531da177e4SLinus Torvalds 	}
6541da177e4SLinus Torvalds 	return 0;
6551da177e4SLinus Torvalds 
6561da177e4SLinus Torvalds }
6571da177e4SLinus Torvalds 
65855e957d8STakashi Iwai static int snd_hdsp_load_firmware_from_cache(struct hdsp *hdsp) {
6591da177e4SLinus Torvalds 
6601da177e4SLinus Torvalds 	int i;
6611da177e4SLinus Torvalds 	unsigned long flags;
6621da177e4SLinus Torvalds 
6631da177e4SLinus Torvalds 	if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
6641da177e4SLinus Torvalds 
6651da177e4SLinus Torvalds 		snd_printk ("Hammerfall-DSP: loading firmware\n");
6661da177e4SLinus Torvalds 
6671da177e4SLinus Torvalds 		hdsp_write (hdsp, HDSP_control2Reg, HDSP_S_PROGRAM);
6681da177e4SLinus Torvalds 		hdsp_write (hdsp, HDSP_fifoData, 0);
6691da177e4SLinus Torvalds 
6701da177e4SLinus Torvalds 		if (hdsp_fifo_wait (hdsp, 0, HDSP_LONG_WAIT)) {
6711da177e4SLinus Torvalds 			snd_printk ("Hammerfall-DSP: timeout waiting for download preparation\n");
6721da177e4SLinus Torvalds 			return -EIO;
6731da177e4SLinus Torvalds 		}
6741da177e4SLinus Torvalds 
6751da177e4SLinus Torvalds 		hdsp_write (hdsp, HDSP_control2Reg, HDSP_S_LOAD);
6761da177e4SLinus Torvalds 
6771da177e4SLinus Torvalds 		for (i = 0; i < 24413; ++i) {
6781da177e4SLinus Torvalds 			hdsp_write(hdsp, HDSP_fifoData, hdsp->firmware_cache[i]);
6791da177e4SLinus Torvalds 			if (hdsp_fifo_wait (hdsp, 127, HDSP_LONG_WAIT)) {
6801da177e4SLinus Torvalds 				snd_printk ("Hammerfall-DSP: timeout during firmware loading\n");
6811da177e4SLinus Torvalds 				return -EIO;
6821da177e4SLinus Torvalds 			}
6831da177e4SLinus Torvalds 		}
6841da177e4SLinus Torvalds 
685ef21ca24SNishanth Aravamudan 		ssleep(3);
6861da177e4SLinus Torvalds 
6871da177e4SLinus Torvalds 		if (hdsp_fifo_wait (hdsp, 0, HDSP_LONG_WAIT)) {
6881da177e4SLinus Torvalds 			snd_printk ("Hammerfall-DSP: timeout at end of firmware loading\n");
6891da177e4SLinus Torvalds 		    	return -EIO;
6901da177e4SLinus Torvalds 		}
6911da177e4SLinus Torvalds 
6921da177e4SLinus Torvalds #ifdef SNDRV_BIG_ENDIAN
6931da177e4SLinus Torvalds 		hdsp->control2_register = HDSP_BIGENDIAN_MODE;
6941da177e4SLinus Torvalds #else
6951da177e4SLinus Torvalds 		hdsp->control2_register = 0;
6961da177e4SLinus Torvalds #endif
6971da177e4SLinus Torvalds 		hdsp_write (hdsp, HDSP_control2Reg, hdsp->control2_register);
6981da177e4SLinus Torvalds 		snd_printk ("Hammerfall-DSP: finished firmware loading\n");
6991da177e4SLinus Torvalds 
7001da177e4SLinus Torvalds 	}
7011da177e4SLinus Torvalds 	if (hdsp->state & HDSP_InitializationComplete) {
702b0b98119STakashi Iwai 		snd_printk(KERN_INFO "Hammerfall-DSP: firmware loaded from cache, restoring defaults\n");
7031da177e4SLinus Torvalds 		spin_lock_irqsave(&hdsp->lock, flags);
7041da177e4SLinus Torvalds 		snd_hdsp_set_defaults(hdsp);
7051da177e4SLinus Torvalds 		spin_unlock_irqrestore(&hdsp->lock, flags);
7061da177e4SLinus Torvalds 	}
7071da177e4SLinus Torvalds 
7081da177e4SLinus Torvalds 	hdsp->state |= HDSP_FirmwareLoaded;
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds 	return 0;
7111da177e4SLinus Torvalds }
7121da177e4SLinus Torvalds 
71355e957d8STakashi Iwai static int hdsp_get_iobox_version (struct hdsp *hdsp)
7141da177e4SLinus Torvalds {
7151da177e4SLinus Torvalds 	if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
7161da177e4SLinus Torvalds 
7171da177e4SLinus Torvalds 		hdsp_write (hdsp, HDSP_control2Reg, HDSP_PROGRAM);
7181da177e4SLinus Torvalds 		hdsp_write (hdsp, HDSP_fifoData, 0);
719b0b98119STakashi Iwai 		if (hdsp_fifo_wait (hdsp, 0, HDSP_SHORT_WAIT) < 0)
7201da177e4SLinus Torvalds 			return -EIO;
7211da177e4SLinus Torvalds 
7221da177e4SLinus Torvalds 		hdsp_write (hdsp, HDSP_control2Reg, HDSP_S_LOAD);
7231da177e4SLinus Torvalds 		hdsp_write (hdsp, HDSP_fifoData, 0);
7241da177e4SLinus Torvalds 
7251da177e4SLinus Torvalds 		if (hdsp_fifo_wait (hdsp, 0, HDSP_SHORT_WAIT)) {
7261da177e4SLinus Torvalds 			hdsp->io_type = Multiface;
7271da177e4SLinus Torvalds 			hdsp_write (hdsp, HDSP_control2Reg, HDSP_VERSION_BIT);
7281da177e4SLinus Torvalds 			hdsp_write (hdsp, HDSP_control2Reg, HDSP_S_LOAD);
7291da177e4SLinus Torvalds 			hdsp_fifo_wait (hdsp, 0, HDSP_SHORT_WAIT);
7301da177e4SLinus Torvalds 		} else {
7311da177e4SLinus Torvalds 			hdsp->io_type = Digiface;
7321da177e4SLinus Torvalds 		}
7331da177e4SLinus Torvalds 	} else {
7341da177e4SLinus Torvalds 		/* firmware was already loaded, get iobox type */
735b0b98119STakashi Iwai 		if (hdsp_read(hdsp, HDSP_status2Register) & HDSP_version1)
7361da177e4SLinus Torvalds 			hdsp->io_type = Multiface;
737b0b98119STakashi Iwai 		else
7381da177e4SLinus Torvalds 			hdsp->io_type = Digiface;
7391da177e4SLinus Torvalds 	}
7401da177e4SLinus Torvalds 	return 0;
7411da177e4SLinus Torvalds }
7421da177e4SLinus Torvalds 
7431da177e4SLinus Torvalds 
744311e70a4STakashi Iwai #ifdef HDSP_FW_LOADER
745311e70a4STakashi Iwai static int __devinit hdsp_request_fw_loader(struct hdsp *hdsp);
746311e70a4STakashi Iwai #endif
747311e70a4STakashi Iwai 
748311e70a4STakashi Iwai static int hdsp_check_for_firmware (struct hdsp *hdsp, int load_on_demand)
7491da177e4SLinus Torvalds {
750311e70a4STakashi Iwai 	if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
751311e70a4STakashi Iwai 		return 0;
7521da177e4SLinus Torvalds 	if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
7531da177e4SLinus Torvalds 		hdsp->state &= ~HDSP_FirmwareLoaded;
754311e70a4STakashi Iwai 		if (! load_on_demand)
755b0b98119STakashi Iwai 			return -EIO;
756311e70a4STakashi Iwai 		snd_printk(KERN_ERR "Hammerfall-DSP: firmware not present.\n");
757b0b98119STakashi Iwai 		/* try to load firmware */
758311e70a4STakashi Iwai 		if (! (hdsp->state & HDSP_FirmwareCached)) {
759311e70a4STakashi Iwai #ifdef HDSP_FW_LOADER
760311e70a4STakashi Iwai 			if (! hdsp_request_fw_loader(hdsp))
761311e70a4STakashi Iwai 				return 0;
762311e70a4STakashi Iwai #endif
763311e70a4STakashi Iwai 			snd_printk(KERN_ERR
764311e70a4STakashi Iwai 				   "Hammerfall-DSP: No firmware loaded nor "
765311e70a4STakashi Iwai 				   "cached, please upload firmware.\n");
7661da177e4SLinus Torvalds 			return -EIO;
7671da177e4SLinus Torvalds 		}
768311e70a4STakashi Iwai 		if (snd_hdsp_load_firmware_from_cache(hdsp) != 0) {
769311e70a4STakashi Iwai 			snd_printk(KERN_ERR
770311e70a4STakashi Iwai 				   "Hammerfall-DSP: Firmware loading from "
771311e70a4STakashi Iwai 				   "cache failed, please upload manually.\n");
772311e70a4STakashi Iwai 			return -EIO;
773311e70a4STakashi Iwai 		}
774311e70a4STakashi Iwai 	}
7751da177e4SLinus Torvalds 	return 0;
7761da177e4SLinus Torvalds }
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds 
77955e957d8STakashi Iwai static int hdsp_fifo_wait(struct hdsp *hdsp, int count, int timeout)
7801da177e4SLinus Torvalds {
7811da177e4SLinus Torvalds 	int i;
7821da177e4SLinus Torvalds 
7831da177e4SLinus Torvalds 	/* the fifoStatus registers reports on how many words
7841da177e4SLinus Torvalds 	   are available in the command FIFO.
7851da177e4SLinus Torvalds 	*/
7861da177e4SLinus Torvalds 
7871da177e4SLinus Torvalds 	for (i = 0; i < timeout; i++) {
7881da177e4SLinus Torvalds 
7891da177e4SLinus Torvalds 		if ((int)(hdsp_read (hdsp, HDSP_fifoStatus) & 0xff) <= count)
7901da177e4SLinus Torvalds 			return 0;
7911da177e4SLinus Torvalds 
7921da177e4SLinus Torvalds 		/* not very friendly, but we only do this during a firmware
7931da177e4SLinus Torvalds 		   load and changing the mixer, so we just put up with it.
7941da177e4SLinus Torvalds 		*/
7951da177e4SLinus Torvalds 
7961da177e4SLinus Torvalds 		udelay (100);
7971da177e4SLinus Torvalds 	}
7981da177e4SLinus Torvalds 
7991da177e4SLinus Torvalds 	snd_printk ("Hammerfall-DSP: wait for FIFO status <= %d failed after %d iterations\n",
8001da177e4SLinus Torvalds 		    count, timeout);
8011da177e4SLinus Torvalds 	return -1;
8021da177e4SLinus Torvalds }
8031da177e4SLinus Torvalds 
80455e957d8STakashi Iwai static int hdsp_read_gain (struct hdsp *hdsp, unsigned int addr)
8051da177e4SLinus Torvalds {
806b0b98119STakashi Iwai 	if (addr >= HDSP_MATRIX_MIXER_SIZE)
8071da177e4SLinus Torvalds 		return 0;
808b0b98119STakashi Iwai 
8091da177e4SLinus Torvalds 	return hdsp->mixer_matrix[addr];
8101da177e4SLinus Torvalds }
8111da177e4SLinus Torvalds 
81255e957d8STakashi Iwai static int hdsp_write_gain(struct hdsp *hdsp, unsigned int addr, unsigned short data)
8131da177e4SLinus Torvalds {
8141da177e4SLinus Torvalds 	unsigned int ad;
8151da177e4SLinus Torvalds 
8161da177e4SLinus Torvalds 	if (addr >= HDSP_MATRIX_MIXER_SIZE)
8171da177e4SLinus Torvalds 		return -1;
8181da177e4SLinus Torvalds 
8191da177e4SLinus Torvalds 	if (hdsp->io_type == H9652 || hdsp->io_type == H9632) {
8201da177e4SLinus Torvalds 
8211da177e4SLinus Torvalds 		/* from martin bjornsen:
8221da177e4SLinus Torvalds 
8231da177e4SLinus Torvalds 		   "You can only write dwords to the
8241da177e4SLinus Torvalds 		   mixer memory which contain two
8251da177e4SLinus Torvalds 		   mixer values in the low and high
8261da177e4SLinus Torvalds 		   word. So if you want to change
8271da177e4SLinus Torvalds 		   value 0 you have to read value 1
8281da177e4SLinus Torvalds 		   from the cache and write both to
8291da177e4SLinus Torvalds 		   the first dword in the mixer
8301da177e4SLinus Torvalds 		   memory."
8311da177e4SLinus Torvalds 		*/
8321da177e4SLinus Torvalds 
833b0b98119STakashi Iwai 		if (hdsp->io_type == H9632 && addr >= 512)
8341da177e4SLinus Torvalds 			return 0;
8351da177e4SLinus Torvalds 
836b0b98119STakashi Iwai 		if (hdsp->io_type == H9652 && addr >= 1352)
8371da177e4SLinus Torvalds 			return 0;
8381da177e4SLinus Torvalds 
8391da177e4SLinus Torvalds 		hdsp->mixer_matrix[addr] = data;
8401da177e4SLinus Torvalds 
8411da177e4SLinus Torvalds 
8421da177e4SLinus Torvalds 		/* `addr' addresses a 16-bit wide address, but
8431da177e4SLinus Torvalds 		   the address space accessed via hdsp_write
8441da177e4SLinus Torvalds 		   uses byte offsets. put another way, addr
8451da177e4SLinus Torvalds 		   varies from 0 to 1351, but to access the
8461da177e4SLinus Torvalds 		   corresponding memory location, we need
8471da177e4SLinus Torvalds 		   to access 0 to 2703 ...
8481da177e4SLinus Torvalds 		*/
8491da177e4SLinus Torvalds 		ad = addr/2;
8501da177e4SLinus Torvalds 
8511da177e4SLinus Torvalds 		hdsp_write (hdsp, 4096 + (ad*4),
8521da177e4SLinus Torvalds 			    (hdsp->mixer_matrix[(addr&0x7fe)+1] << 16) +
8531da177e4SLinus Torvalds 			    hdsp->mixer_matrix[addr&0x7fe]);
8541da177e4SLinus Torvalds 
8551da177e4SLinus Torvalds 		return 0;
8561da177e4SLinus Torvalds 
8571da177e4SLinus Torvalds 	} else {
8581da177e4SLinus Torvalds 
8591da177e4SLinus Torvalds 		ad = (addr << 16) + data;
8601da177e4SLinus Torvalds 
861b0b98119STakashi Iwai 		if (hdsp_fifo_wait(hdsp, 127, HDSP_LONG_WAIT))
8621da177e4SLinus Torvalds 			return -1;
8631da177e4SLinus Torvalds 
8641da177e4SLinus Torvalds 		hdsp_write (hdsp, HDSP_fifoData, ad);
8651da177e4SLinus Torvalds 		hdsp->mixer_matrix[addr] = data;
8661da177e4SLinus Torvalds 
8671da177e4SLinus Torvalds 	}
8681da177e4SLinus Torvalds 
8691da177e4SLinus Torvalds 	return 0;
8701da177e4SLinus Torvalds }
8711da177e4SLinus Torvalds 
87255e957d8STakashi Iwai static int snd_hdsp_use_is_exclusive(struct hdsp *hdsp)
8731da177e4SLinus Torvalds {
8741da177e4SLinus Torvalds 	unsigned long flags;
8751da177e4SLinus Torvalds 	int ret = 1;
8761da177e4SLinus Torvalds 
8771da177e4SLinus Torvalds 	spin_lock_irqsave(&hdsp->lock, flags);
8781da177e4SLinus Torvalds 	if ((hdsp->playback_pid != hdsp->capture_pid) &&
879b0b98119STakashi Iwai 	    (hdsp->playback_pid >= 0) && (hdsp->capture_pid >= 0))
8801da177e4SLinus Torvalds 		ret = 0;
8811da177e4SLinus Torvalds 	spin_unlock_irqrestore(&hdsp->lock, flags);
8821da177e4SLinus Torvalds 	return ret;
8831da177e4SLinus Torvalds }
8841da177e4SLinus Torvalds 
88555e957d8STakashi Iwai static int hdsp_external_sample_rate (struct hdsp *hdsp)
8861da177e4SLinus Torvalds {
8871da177e4SLinus Torvalds 	unsigned int status2 = hdsp_read(hdsp, HDSP_status2Register);
8881da177e4SLinus Torvalds 	unsigned int rate_bits = status2 & HDSP_systemFrequencyMask;
8891da177e4SLinus Torvalds 
8901da177e4SLinus Torvalds 	switch (rate_bits) {
8911da177e4SLinus Torvalds 	case HDSP_systemFrequency32:   return 32000;
8921da177e4SLinus Torvalds 	case HDSP_systemFrequency44_1: return 44100;
8931da177e4SLinus Torvalds 	case HDSP_systemFrequency48:   return 48000;
8941da177e4SLinus Torvalds 	case HDSP_systemFrequency64:   return 64000;
8951da177e4SLinus Torvalds 	case HDSP_systemFrequency88_2: return 88200;
8961da177e4SLinus Torvalds 	case HDSP_systemFrequency96:   return 96000;
8971da177e4SLinus Torvalds 	default:
8981da177e4SLinus Torvalds 		return 0;
8991da177e4SLinus Torvalds 	}
9001da177e4SLinus Torvalds }
9011da177e4SLinus Torvalds 
90255e957d8STakashi Iwai static int hdsp_spdif_sample_rate(struct hdsp *hdsp)
9031da177e4SLinus Torvalds {
9041da177e4SLinus Torvalds 	unsigned int status = hdsp_read(hdsp, HDSP_statusRegister);
9051da177e4SLinus Torvalds 	unsigned int rate_bits = (status & HDSP_spdifFrequencyMask);
9061da177e4SLinus Torvalds 
907b0b98119STakashi Iwai 	if (status & HDSP_SPDIFErrorFlag)
9081da177e4SLinus Torvalds 		return 0;
9091da177e4SLinus Torvalds 
9101da177e4SLinus Torvalds 	switch (rate_bits) {
9111da177e4SLinus Torvalds 	case HDSP_spdifFrequency32KHz: return 32000;
9121da177e4SLinus Torvalds 	case HDSP_spdifFrequency44_1KHz: return 44100;
9131da177e4SLinus Torvalds 	case HDSP_spdifFrequency48KHz: return 48000;
9141da177e4SLinus Torvalds 	case HDSP_spdifFrequency64KHz: return 64000;
9151da177e4SLinus Torvalds 	case HDSP_spdifFrequency88_2KHz: return 88200;
9161da177e4SLinus Torvalds 	case HDSP_spdifFrequency96KHz: return 96000;
9171da177e4SLinus Torvalds 	case HDSP_spdifFrequency128KHz:
9181da177e4SLinus Torvalds 		if (hdsp->io_type == H9632) return 128000;
9191da177e4SLinus Torvalds 		break;
9201da177e4SLinus Torvalds 	case HDSP_spdifFrequency176_4KHz:
9211da177e4SLinus Torvalds 		if (hdsp->io_type == H9632) return 176400;
9221da177e4SLinus Torvalds 		break;
9231da177e4SLinus Torvalds 	case HDSP_spdifFrequency192KHz:
9241da177e4SLinus Torvalds 		if (hdsp->io_type == H9632) return 192000;
9251da177e4SLinus Torvalds 		break;
9261da177e4SLinus Torvalds 	default:
9271da177e4SLinus Torvalds 		break;
9281da177e4SLinus Torvalds 	}
9291da177e4SLinus Torvalds 	snd_printk ("Hammerfall-DSP: unknown spdif frequency status; bits = 0x%x, status = 0x%x\n", rate_bits, status);
9301da177e4SLinus Torvalds 	return 0;
9311da177e4SLinus Torvalds }
9321da177e4SLinus Torvalds 
93355e957d8STakashi Iwai static void hdsp_compute_period_size(struct hdsp *hdsp)
9341da177e4SLinus Torvalds {
9351da177e4SLinus Torvalds 	hdsp->period_bytes = 1 << ((hdsp_decode_latency(hdsp->control_register) + 8));
9361da177e4SLinus Torvalds }
9371da177e4SLinus Torvalds 
93855e957d8STakashi Iwai static snd_pcm_uframes_t hdsp_hw_pointer(struct hdsp *hdsp)
9391da177e4SLinus Torvalds {
9401da177e4SLinus Torvalds 	int position;
9411da177e4SLinus Torvalds 
9421da177e4SLinus Torvalds 	position = hdsp_read(hdsp, HDSP_statusRegister);
9431da177e4SLinus Torvalds 
944b0b98119STakashi Iwai 	if (!hdsp->precise_ptr)
9451da177e4SLinus Torvalds 		return (position & HDSP_BufferID) ? (hdsp->period_bytes / 4) : 0;
9461da177e4SLinus Torvalds 
9471da177e4SLinus Torvalds 	position &= HDSP_BufferPositionMask;
9481da177e4SLinus Torvalds 	position /= 4;
9491da177e4SLinus Torvalds 	position &= (hdsp->period_bytes/2) - 1;
9501da177e4SLinus Torvalds 	return position;
9511da177e4SLinus Torvalds }
9521da177e4SLinus Torvalds 
95355e957d8STakashi Iwai static void hdsp_reset_hw_pointer(struct hdsp *hdsp)
9541da177e4SLinus Torvalds {
9551da177e4SLinus Torvalds 	hdsp_write (hdsp, HDSP_resetPointer, 0);
956d7923b2aSRemy Bruno 	if (hdsp->io_type == H9632 && hdsp->firmware_rev >= 152)
957d7923b2aSRemy Bruno 		/* HDSP_resetPointer = HDSP_freqReg, which is strange and
958d7923b2aSRemy Bruno 		 * requires (?) to write again DDS value after a reset pointer
959d7923b2aSRemy Bruno 		 * (at least, it works like this) */
960d7923b2aSRemy Bruno 		hdsp_write (hdsp, HDSP_freqReg, hdsp->dds_value);
9611da177e4SLinus Torvalds }
9621da177e4SLinus Torvalds 
96355e957d8STakashi Iwai static void hdsp_start_audio(struct hdsp *s)
9641da177e4SLinus Torvalds {
9651da177e4SLinus Torvalds 	s->control_register |= (HDSP_AudioInterruptEnable | HDSP_Start);
9661da177e4SLinus Torvalds 	hdsp_write(s, HDSP_controlRegister, s->control_register);
9671da177e4SLinus Torvalds }
9681da177e4SLinus Torvalds 
96955e957d8STakashi Iwai static void hdsp_stop_audio(struct hdsp *s)
9701da177e4SLinus Torvalds {
9711da177e4SLinus Torvalds 	s->control_register &= ~(HDSP_Start | HDSP_AudioInterruptEnable);
9721da177e4SLinus Torvalds 	hdsp_write(s, HDSP_controlRegister, s->control_register);
9731da177e4SLinus Torvalds }
9741da177e4SLinus Torvalds 
97555e957d8STakashi Iwai static void hdsp_silence_playback(struct hdsp *hdsp)
9761da177e4SLinus Torvalds {
9771da177e4SLinus Torvalds 	memset(hdsp->playback_buffer, 0, HDSP_DMA_AREA_BYTES);
9781da177e4SLinus Torvalds }
9791da177e4SLinus Torvalds 
98055e957d8STakashi Iwai static int hdsp_set_interrupt_interval(struct hdsp *s, unsigned int frames)
9811da177e4SLinus Torvalds {
9821da177e4SLinus Torvalds 	int n;
9831da177e4SLinus Torvalds 
9841da177e4SLinus Torvalds 	spin_lock_irq(&s->lock);
9851da177e4SLinus Torvalds 
9861da177e4SLinus Torvalds 	frames >>= 7;
9871da177e4SLinus Torvalds 	n = 0;
9881da177e4SLinus Torvalds 	while (frames) {
9891da177e4SLinus Torvalds 		n++;
9901da177e4SLinus Torvalds 		frames >>= 1;
9911da177e4SLinus Torvalds 	}
9921da177e4SLinus Torvalds 
9931da177e4SLinus Torvalds 	s->control_register &= ~HDSP_LatencyMask;
9941da177e4SLinus Torvalds 	s->control_register |= hdsp_encode_latency(n);
9951da177e4SLinus Torvalds 
9961da177e4SLinus Torvalds 	hdsp_write(s, HDSP_controlRegister, s->control_register);
9971da177e4SLinus Torvalds 
9981da177e4SLinus Torvalds 	hdsp_compute_period_size(s);
9991da177e4SLinus Torvalds 
10001da177e4SLinus Torvalds 	spin_unlock_irq(&s->lock);
10011da177e4SLinus Torvalds 
10021da177e4SLinus Torvalds 	return 0;
10031da177e4SLinus Torvalds }
10041da177e4SLinus Torvalds 
1005d7923b2aSRemy Bruno static void hdsp_set_dds_value(struct hdsp *hdsp, int rate)
1006d7923b2aSRemy Bruno {
1007d7923b2aSRemy Bruno 	u64 n;
1008d7923b2aSRemy Bruno 	u32 r;
1009d7923b2aSRemy Bruno 
1010d7923b2aSRemy Bruno 	if (rate >= 112000)
1011d7923b2aSRemy Bruno 		rate /= 4;
1012d7923b2aSRemy Bruno 	else if (rate >= 56000)
1013d7923b2aSRemy Bruno 		rate /= 2;
1014d7923b2aSRemy Bruno 
1015e4b6088cSJulian Cable 	n = DDS_NUMERATOR;
1016d7923b2aSRemy Bruno 	div64_32(&n, rate, &r);
1017d7923b2aSRemy Bruno 	/* n should be less than 2^32 for being written to FREQ register */
1018d7923b2aSRemy Bruno 	snd_assert((n >> 32) == 0);
1019d7923b2aSRemy Bruno 	/* HDSP_freqReg and HDSP_resetPointer are the same, so keep the DDS
1020d7923b2aSRemy Bruno 	   value to write it after a reset */
1021d7923b2aSRemy Bruno 	hdsp->dds_value = n;
1022d7923b2aSRemy Bruno 	hdsp_write(hdsp, HDSP_freqReg, hdsp->dds_value);
1023d7923b2aSRemy Bruno }
1024d7923b2aSRemy Bruno 
102555e957d8STakashi Iwai static int hdsp_set_rate(struct hdsp *hdsp, int rate, int called_internally)
10261da177e4SLinus Torvalds {
10271da177e4SLinus Torvalds 	int reject_if_open = 0;
10281da177e4SLinus Torvalds 	int current_rate;
10291da177e4SLinus Torvalds 	int rate_bits;
10301da177e4SLinus Torvalds 
10311da177e4SLinus Torvalds 	/* ASSUMPTION: hdsp->lock is either held, or
10321da177e4SLinus Torvalds 	   there is no need for it (e.g. during module
10331da177e4SLinus Torvalds 	   initialization).
10341da177e4SLinus Torvalds 	*/
10351da177e4SLinus Torvalds 
10361da177e4SLinus Torvalds 	if (!(hdsp->control_register & HDSP_ClockModeMaster)) {
10371da177e4SLinus Torvalds 		if (called_internally) {
10381da177e4SLinus Torvalds 			/* request from ctl or card initialization */
1039b0b98119STakashi Iwai 			snd_printk(KERN_ERR "Hammerfall-DSP: device is not running as a clock master: cannot set sample rate.\n");
10401da177e4SLinus Torvalds 			return -1;
10411da177e4SLinus Torvalds 		} else {
10421da177e4SLinus Torvalds 			/* hw_param request while in AutoSync mode */
10431da177e4SLinus Torvalds 			int external_freq = hdsp_external_sample_rate(hdsp);
10441da177e4SLinus Torvalds 			int spdif_freq = hdsp_spdif_sample_rate(hdsp);
10451da177e4SLinus Torvalds 
1046b0b98119STakashi Iwai 			if ((spdif_freq == external_freq*2) && (hdsp_autosync_ref(hdsp) >= HDSP_AUTOSYNC_FROM_ADAT1))
1047b0b98119STakashi Iwai 				snd_printk(KERN_INFO "Hammerfall-DSP: Detected ADAT in double speed mode\n");
1048b0b98119STakashi Iwai 			else if (hdsp->io_type == H9632 && (spdif_freq == external_freq*4) && (hdsp_autosync_ref(hdsp) >= HDSP_AUTOSYNC_FROM_ADAT1))
1049b0b98119STakashi Iwai 				snd_printk(KERN_INFO "Hammerfall-DSP: Detected ADAT in quad speed mode\n");
1050b0b98119STakashi Iwai 			else if (rate != external_freq) {
1051b0b98119STakashi Iwai 				snd_printk(KERN_INFO "Hammerfall-DSP: No AutoSync source for requested rate\n");
10521da177e4SLinus Torvalds 				return -1;
10531da177e4SLinus Torvalds 			}
10541da177e4SLinus Torvalds 		}
10551da177e4SLinus Torvalds 	}
10561da177e4SLinus Torvalds 
10571da177e4SLinus Torvalds 	current_rate = hdsp->system_sample_rate;
10581da177e4SLinus Torvalds 
10591da177e4SLinus Torvalds 	/* Changing from a "single speed" to a "double speed" rate is
10601da177e4SLinus Torvalds 	   not allowed if any substreams are open. This is because
10611da177e4SLinus Torvalds 	   such a change causes a shift in the location of
10621da177e4SLinus Torvalds 	   the DMA buffers and a reduction in the number of available
10631da177e4SLinus Torvalds 	   buffers.
10641da177e4SLinus Torvalds 
10651da177e4SLinus Torvalds 	   Note that a similar but essentially insoluble problem
10661da177e4SLinus Torvalds 	   exists for externally-driven rate changes. All we can do
10671da177e4SLinus Torvalds 	   is to flag rate changes in the read/write routines.  */
10681da177e4SLinus Torvalds 
1069b0b98119STakashi Iwai 	if (rate > 96000 && hdsp->io_type != H9632)
10701da177e4SLinus Torvalds 		return -EINVAL;
10711da177e4SLinus Torvalds 
10721da177e4SLinus Torvalds 	switch (rate) {
10731da177e4SLinus Torvalds 	case 32000:
1074b0b98119STakashi Iwai 		if (current_rate > 48000)
10751da177e4SLinus Torvalds 			reject_if_open = 1;
10761da177e4SLinus Torvalds 		rate_bits = HDSP_Frequency32KHz;
10771da177e4SLinus Torvalds 		break;
10781da177e4SLinus Torvalds 	case 44100:
1079b0b98119STakashi Iwai 		if (current_rate > 48000)
10801da177e4SLinus Torvalds 			reject_if_open = 1;
10811da177e4SLinus Torvalds 		rate_bits = HDSP_Frequency44_1KHz;
10821da177e4SLinus Torvalds 		break;
10831da177e4SLinus Torvalds 	case 48000:
1084b0b98119STakashi Iwai 		if (current_rate > 48000)
10851da177e4SLinus Torvalds 			reject_if_open = 1;
10861da177e4SLinus Torvalds 		rate_bits = HDSP_Frequency48KHz;
10871da177e4SLinus Torvalds 		break;
10881da177e4SLinus Torvalds 	case 64000:
1089b0b98119STakashi Iwai 		if (current_rate <= 48000 || current_rate > 96000)
10901da177e4SLinus Torvalds 			reject_if_open = 1;
10911da177e4SLinus Torvalds 		rate_bits = HDSP_Frequency64KHz;
10921da177e4SLinus Torvalds 		break;
10931da177e4SLinus Torvalds 	case 88200:
1094b0b98119STakashi Iwai 		if (current_rate <= 48000 || current_rate > 96000)
10951da177e4SLinus Torvalds 			reject_if_open = 1;
10961da177e4SLinus Torvalds 		rate_bits = HDSP_Frequency88_2KHz;
10971da177e4SLinus Torvalds 		break;
10981da177e4SLinus Torvalds 	case 96000:
1099b0b98119STakashi Iwai 		if (current_rate <= 48000 || current_rate > 96000)
11001da177e4SLinus Torvalds 			reject_if_open = 1;
11011da177e4SLinus Torvalds 		rate_bits = HDSP_Frequency96KHz;
11021da177e4SLinus Torvalds 		break;
11031da177e4SLinus Torvalds 	case 128000:
1104b0b98119STakashi Iwai 		if (current_rate < 128000)
11051da177e4SLinus Torvalds 			reject_if_open = 1;
11061da177e4SLinus Torvalds 		rate_bits = HDSP_Frequency128KHz;
11071da177e4SLinus Torvalds 		break;
11081da177e4SLinus Torvalds 	case 176400:
1109b0b98119STakashi Iwai 		if (current_rate < 128000)
11101da177e4SLinus Torvalds 			reject_if_open = 1;
11111da177e4SLinus Torvalds 		rate_bits = HDSP_Frequency176_4KHz;
11121da177e4SLinus Torvalds 		break;
11131da177e4SLinus Torvalds 	case 192000:
1114b0b98119STakashi Iwai 		if (current_rate < 128000)
11151da177e4SLinus Torvalds 			reject_if_open = 1;
11161da177e4SLinus Torvalds 		rate_bits = HDSP_Frequency192KHz;
11171da177e4SLinus Torvalds 		break;
11181da177e4SLinus Torvalds 	default:
11191da177e4SLinus Torvalds 		return -EINVAL;
11201da177e4SLinus Torvalds 	}
11211da177e4SLinus Torvalds 
11221da177e4SLinus Torvalds 	if (reject_if_open && (hdsp->capture_pid >= 0 || hdsp->playback_pid >= 0)) {
11231da177e4SLinus Torvalds 		snd_printk ("Hammerfall-DSP: cannot change speed mode (capture PID = %d, playback PID = %d)\n",
11241da177e4SLinus Torvalds 			    hdsp->capture_pid,
11251da177e4SLinus Torvalds 			    hdsp->playback_pid);
11261da177e4SLinus Torvalds 		return -EBUSY;
11271da177e4SLinus Torvalds 	}
11281da177e4SLinus Torvalds 
11291da177e4SLinus Torvalds 	hdsp->control_register &= ~HDSP_FrequencyMask;
11301da177e4SLinus Torvalds 	hdsp->control_register |= rate_bits;
11311da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
11321da177e4SLinus Torvalds 
1133d7923b2aSRemy Bruno 	/* For HDSP9632 rev 152, need to set DDS value in FREQ register */
1134d7923b2aSRemy Bruno 	if (hdsp->io_type == H9632 && hdsp->firmware_rev >= 152)
1135d7923b2aSRemy Bruno 		hdsp_set_dds_value(hdsp, rate);
1136d7923b2aSRemy Bruno 
11371da177e4SLinus Torvalds 	if (rate >= 128000) {
11381da177e4SLinus Torvalds 		hdsp->channel_map = channel_map_H9632_qs;
11391da177e4SLinus Torvalds 	} else if (rate > 48000) {
1140b0b98119STakashi Iwai 		if (hdsp->io_type == H9632)
11411da177e4SLinus Torvalds 			hdsp->channel_map = channel_map_H9632_ds;
1142b0b98119STakashi Iwai 		else
11431da177e4SLinus Torvalds 			hdsp->channel_map = channel_map_ds;
11441da177e4SLinus Torvalds 	} else {
11451da177e4SLinus Torvalds 		switch (hdsp->io_type) {
11461da177e4SLinus Torvalds 		case Multiface:
11471da177e4SLinus Torvalds 			hdsp->channel_map = channel_map_mf_ss;
11481da177e4SLinus Torvalds 			break;
11491da177e4SLinus Torvalds 		case Digiface:
11501da177e4SLinus Torvalds 		case H9652:
11511da177e4SLinus Torvalds 			hdsp->channel_map = channel_map_df_ss;
11521da177e4SLinus Torvalds 			break;
11531da177e4SLinus Torvalds 		case H9632:
11541da177e4SLinus Torvalds 			hdsp->channel_map = channel_map_H9632_ss;
11551da177e4SLinus Torvalds 			break;
11561da177e4SLinus Torvalds 		default:
11571da177e4SLinus Torvalds 			/* should never happen */
11581da177e4SLinus Torvalds 			break;
11591da177e4SLinus Torvalds 		}
11601da177e4SLinus Torvalds 	}
11611da177e4SLinus Torvalds 
11621da177e4SLinus Torvalds 	hdsp->system_sample_rate = rate;
11631da177e4SLinus Torvalds 
11641da177e4SLinus Torvalds 	return 0;
11651da177e4SLinus Torvalds }
11661da177e4SLinus Torvalds 
11671da177e4SLinus Torvalds /*----------------------------------------------------------------------------
11681da177e4SLinus Torvalds    MIDI
11691da177e4SLinus Torvalds   ----------------------------------------------------------------------------*/
11701da177e4SLinus Torvalds 
117155e957d8STakashi Iwai static unsigned char snd_hdsp_midi_read_byte (struct hdsp *hdsp, int id)
11721da177e4SLinus Torvalds {
11731da177e4SLinus Torvalds 	/* the hardware already does the relevant bit-mask with 0xff */
1174b0b98119STakashi Iwai 	if (id)
11751da177e4SLinus Torvalds 		return hdsp_read(hdsp, HDSP_midiDataIn1);
1176b0b98119STakashi Iwai 	else
11771da177e4SLinus Torvalds 		return hdsp_read(hdsp, HDSP_midiDataIn0);
11781da177e4SLinus Torvalds }
11791da177e4SLinus Torvalds 
118055e957d8STakashi Iwai static void snd_hdsp_midi_write_byte (struct hdsp *hdsp, int id, int val)
11811da177e4SLinus Torvalds {
11821da177e4SLinus Torvalds 	/* the hardware already does the relevant bit-mask with 0xff */
1183b0b98119STakashi Iwai 	if (id)
11841da177e4SLinus Torvalds 		hdsp_write(hdsp, HDSP_midiDataOut1, val);
1185b0b98119STakashi Iwai 	else
11861da177e4SLinus Torvalds 		hdsp_write(hdsp, HDSP_midiDataOut0, val);
11871da177e4SLinus Torvalds }
11881da177e4SLinus Torvalds 
118955e957d8STakashi Iwai static int snd_hdsp_midi_input_available (struct hdsp *hdsp, int id)
11901da177e4SLinus Torvalds {
1191b0b98119STakashi Iwai 	if (id)
11921da177e4SLinus Torvalds 		return (hdsp_read(hdsp, HDSP_midiStatusIn1) & 0xff);
1193b0b98119STakashi Iwai 	else
11941da177e4SLinus Torvalds 		return (hdsp_read(hdsp, HDSP_midiStatusIn0) & 0xff);
11951da177e4SLinus Torvalds }
11961da177e4SLinus Torvalds 
119755e957d8STakashi Iwai static int snd_hdsp_midi_output_possible (struct hdsp *hdsp, int id)
11981da177e4SLinus Torvalds {
11991da177e4SLinus Torvalds 	int fifo_bytes_used;
12001da177e4SLinus Torvalds 
1201b0b98119STakashi Iwai 	if (id)
12021da177e4SLinus Torvalds 		fifo_bytes_used = hdsp_read(hdsp, HDSP_midiStatusOut1) & 0xff;
1203b0b98119STakashi Iwai 	else
12041da177e4SLinus Torvalds 		fifo_bytes_used = hdsp_read(hdsp, HDSP_midiStatusOut0) & 0xff;
12051da177e4SLinus Torvalds 
1206b0b98119STakashi Iwai 	if (fifo_bytes_used < 128)
12071da177e4SLinus Torvalds 		return  128 - fifo_bytes_used;
1208b0b98119STakashi Iwai 	else
12091da177e4SLinus Torvalds 		return 0;
12101da177e4SLinus Torvalds }
12111da177e4SLinus Torvalds 
121255e957d8STakashi Iwai static void snd_hdsp_flush_midi_input (struct hdsp *hdsp, int id)
12131da177e4SLinus Torvalds {
1214b0b98119STakashi Iwai 	while (snd_hdsp_midi_input_available (hdsp, id))
12151da177e4SLinus Torvalds 		snd_hdsp_midi_read_byte (hdsp, id);
12161da177e4SLinus Torvalds }
12171da177e4SLinus Torvalds 
121855e957d8STakashi Iwai static int snd_hdsp_midi_output_write (struct hdsp_midi *hmidi)
12191da177e4SLinus Torvalds {
12201da177e4SLinus Torvalds 	unsigned long flags;
12211da177e4SLinus Torvalds 	int n_pending;
12221da177e4SLinus Torvalds 	int to_write;
12231da177e4SLinus Torvalds 	int i;
12241da177e4SLinus Torvalds 	unsigned char buf[128];
12251da177e4SLinus Torvalds 
12261da177e4SLinus Torvalds 	/* Output is not interrupt driven */
12271da177e4SLinus Torvalds 
12281da177e4SLinus Torvalds 	spin_lock_irqsave (&hmidi->lock, flags);
12291da177e4SLinus Torvalds 	if (hmidi->output) {
12301da177e4SLinus Torvalds 		if (!snd_rawmidi_transmit_empty (hmidi->output)) {
12311da177e4SLinus Torvalds 			if ((n_pending = snd_hdsp_midi_output_possible (hmidi->hdsp, hmidi->id)) > 0) {
12321da177e4SLinus Torvalds 				if (n_pending > (int)sizeof (buf))
12331da177e4SLinus Torvalds 					n_pending = sizeof (buf);
12341da177e4SLinus Torvalds 
12351da177e4SLinus Torvalds 				if ((to_write = snd_rawmidi_transmit (hmidi->output, buf, n_pending)) > 0) {
12361da177e4SLinus Torvalds 					for (i = 0; i < to_write; ++i)
12371da177e4SLinus Torvalds 						snd_hdsp_midi_write_byte (hmidi->hdsp, hmidi->id, buf[i]);
12381da177e4SLinus Torvalds 				}
12391da177e4SLinus Torvalds 			}
12401da177e4SLinus Torvalds 		}
12411da177e4SLinus Torvalds 	}
12421da177e4SLinus Torvalds 	spin_unlock_irqrestore (&hmidi->lock, flags);
12431da177e4SLinus Torvalds 	return 0;
12441da177e4SLinus Torvalds }
12451da177e4SLinus Torvalds 
124655e957d8STakashi Iwai static int snd_hdsp_midi_input_read (struct hdsp_midi *hmidi)
12471da177e4SLinus Torvalds {
12481da177e4SLinus Torvalds 	unsigned char buf[128]; /* this buffer is designed to match the MIDI input FIFO size */
12491da177e4SLinus Torvalds 	unsigned long flags;
12501da177e4SLinus Torvalds 	int n_pending;
12511da177e4SLinus Torvalds 	int i;
12521da177e4SLinus Torvalds 
12531da177e4SLinus Torvalds 	spin_lock_irqsave (&hmidi->lock, flags);
12541da177e4SLinus Torvalds 	if ((n_pending = snd_hdsp_midi_input_available (hmidi->hdsp, hmidi->id)) > 0) {
12551da177e4SLinus Torvalds 		if (hmidi->input) {
1256b0b98119STakashi Iwai 			if (n_pending > (int)sizeof (buf))
12571da177e4SLinus Torvalds 				n_pending = sizeof (buf);
1258b0b98119STakashi Iwai 			for (i = 0; i < n_pending; ++i)
12591da177e4SLinus Torvalds 				buf[i] = snd_hdsp_midi_read_byte (hmidi->hdsp, hmidi->id);
1260b0b98119STakashi Iwai 			if (n_pending)
12611da177e4SLinus Torvalds 				snd_rawmidi_receive (hmidi->input, buf, n_pending);
12621da177e4SLinus Torvalds 		} else {
12631da177e4SLinus Torvalds 			/* flush the MIDI input FIFO */
1264b0b98119STakashi Iwai 			while (--n_pending)
12651da177e4SLinus Torvalds 				snd_hdsp_midi_read_byte (hmidi->hdsp, hmidi->id);
12661da177e4SLinus Torvalds 		}
12671da177e4SLinus Torvalds 	}
12681da177e4SLinus Torvalds 	hmidi->pending = 0;
1269b0b98119STakashi Iwai 	if (hmidi->id)
12701da177e4SLinus Torvalds 		hmidi->hdsp->control_register |= HDSP_Midi1InterruptEnable;
1271b0b98119STakashi Iwai 	else
12721da177e4SLinus Torvalds 		hmidi->hdsp->control_register |= HDSP_Midi0InterruptEnable;
12731da177e4SLinus Torvalds 	hdsp_write(hmidi->hdsp, HDSP_controlRegister, hmidi->hdsp->control_register);
12741da177e4SLinus Torvalds 	spin_unlock_irqrestore (&hmidi->lock, flags);
12751da177e4SLinus Torvalds 	return snd_hdsp_midi_output_write (hmidi);
12761da177e4SLinus Torvalds }
12771da177e4SLinus Torvalds 
127855e957d8STakashi Iwai static void snd_hdsp_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
12791da177e4SLinus Torvalds {
128055e957d8STakashi Iwai 	struct hdsp *hdsp;
128155e957d8STakashi Iwai 	struct hdsp_midi *hmidi;
12821da177e4SLinus Torvalds 	unsigned long flags;
12831da177e4SLinus Torvalds 	u32 ie;
12841da177e4SLinus Torvalds 
128555e957d8STakashi Iwai 	hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
12861da177e4SLinus Torvalds 	hdsp = hmidi->hdsp;
12871da177e4SLinus Torvalds 	ie = hmidi->id ? HDSP_Midi1InterruptEnable : HDSP_Midi0InterruptEnable;
12881da177e4SLinus Torvalds 	spin_lock_irqsave (&hdsp->lock, flags);
12891da177e4SLinus Torvalds 	if (up) {
12901da177e4SLinus Torvalds 		if (!(hdsp->control_register & ie)) {
12911da177e4SLinus Torvalds 			snd_hdsp_flush_midi_input (hdsp, hmidi->id);
12921da177e4SLinus Torvalds 			hdsp->control_register |= ie;
12931da177e4SLinus Torvalds 		}
12941da177e4SLinus Torvalds 	} else {
12951da177e4SLinus Torvalds 		hdsp->control_register &= ~ie;
12961da177e4SLinus Torvalds 		tasklet_kill(&hdsp->midi_tasklet);
12971da177e4SLinus Torvalds 	}
12981da177e4SLinus Torvalds 
12991da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
13001da177e4SLinus Torvalds 	spin_unlock_irqrestore (&hdsp->lock, flags);
13011da177e4SLinus Torvalds }
13021da177e4SLinus Torvalds 
13031da177e4SLinus Torvalds static void snd_hdsp_midi_output_timer(unsigned long data)
13041da177e4SLinus Torvalds {
130555e957d8STakashi Iwai 	struct hdsp_midi *hmidi = (struct hdsp_midi *) data;
13061da177e4SLinus Torvalds 	unsigned long flags;
13071da177e4SLinus Torvalds 
13081da177e4SLinus Torvalds 	snd_hdsp_midi_output_write(hmidi);
13091da177e4SLinus Torvalds 	spin_lock_irqsave (&hmidi->lock, flags);
13101da177e4SLinus Torvalds 
13111da177e4SLinus Torvalds 	/* this does not bump hmidi->istimer, because the
13121da177e4SLinus Torvalds 	   kernel automatically removed the timer when it
13131da177e4SLinus Torvalds 	   expired, and we are now adding it back, thus
13141da177e4SLinus Torvalds 	   leaving istimer wherever it was set before.
13151da177e4SLinus Torvalds 	*/
13161da177e4SLinus Torvalds 
13171da177e4SLinus Torvalds 	if (hmidi->istimer) {
13181da177e4SLinus Torvalds 		hmidi->timer.expires = 1 + jiffies;
13191da177e4SLinus Torvalds 		add_timer(&hmidi->timer);
13201da177e4SLinus Torvalds 	}
13211da177e4SLinus Torvalds 
13221da177e4SLinus Torvalds 	spin_unlock_irqrestore (&hmidi->lock, flags);
13231da177e4SLinus Torvalds }
13241da177e4SLinus Torvalds 
132555e957d8STakashi Iwai static void snd_hdsp_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
13261da177e4SLinus Torvalds {
132755e957d8STakashi Iwai 	struct hdsp_midi *hmidi;
13281da177e4SLinus Torvalds 	unsigned long flags;
13291da177e4SLinus Torvalds 
133055e957d8STakashi Iwai 	hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
13311da177e4SLinus Torvalds 	spin_lock_irqsave (&hmidi->lock, flags);
13321da177e4SLinus Torvalds 	if (up) {
13331da177e4SLinus Torvalds 		if (!hmidi->istimer) {
13341da177e4SLinus Torvalds 			init_timer(&hmidi->timer);
13351da177e4SLinus Torvalds 			hmidi->timer.function = snd_hdsp_midi_output_timer;
13361da177e4SLinus Torvalds 			hmidi->timer.data = (unsigned long) hmidi;
13371da177e4SLinus Torvalds 			hmidi->timer.expires = 1 + jiffies;
13381da177e4SLinus Torvalds 			add_timer(&hmidi->timer);
13391da177e4SLinus Torvalds 			hmidi->istimer++;
13401da177e4SLinus Torvalds 		}
13411da177e4SLinus Torvalds 	} else {
1342b0b98119STakashi Iwai 		if (hmidi->istimer && --hmidi->istimer <= 0)
13431da177e4SLinus Torvalds 			del_timer (&hmidi->timer);
13441da177e4SLinus Torvalds 	}
13451da177e4SLinus Torvalds 	spin_unlock_irqrestore (&hmidi->lock, flags);
13461da177e4SLinus Torvalds 	if (up)
13471da177e4SLinus Torvalds 		snd_hdsp_midi_output_write(hmidi);
13481da177e4SLinus Torvalds }
13491da177e4SLinus Torvalds 
135055e957d8STakashi Iwai static int snd_hdsp_midi_input_open(struct snd_rawmidi_substream *substream)
13511da177e4SLinus Torvalds {
135255e957d8STakashi Iwai 	struct hdsp_midi *hmidi;
13531da177e4SLinus Torvalds 
135455e957d8STakashi Iwai 	hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
13551da177e4SLinus Torvalds 	spin_lock_irq (&hmidi->lock);
13561da177e4SLinus Torvalds 	snd_hdsp_flush_midi_input (hmidi->hdsp, hmidi->id);
13571da177e4SLinus Torvalds 	hmidi->input = substream;
13581da177e4SLinus Torvalds 	spin_unlock_irq (&hmidi->lock);
13591da177e4SLinus Torvalds 
13601da177e4SLinus Torvalds 	return 0;
13611da177e4SLinus Torvalds }
13621da177e4SLinus Torvalds 
136355e957d8STakashi Iwai static int snd_hdsp_midi_output_open(struct snd_rawmidi_substream *substream)
13641da177e4SLinus Torvalds {
136555e957d8STakashi Iwai 	struct hdsp_midi *hmidi;
13661da177e4SLinus Torvalds 
136755e957d8STakashi Iwai 	hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
13681da177e4SLinus Torvalds 	spin_lock_irq (&hmidi->lock);
13691da177e4SLinus Torvalds 	hmidi->output = substream;
13701da177e4SLinus Torvalds 	spin_unlock_irq (&hmidi->lock);
13711da177e4SLinus Torvalds 
13721da177e4SLinus Torvalds 	return 0;
13731da177e4SLinus Torvalds }
13741da177e4SLinus Torvalds 
137555e957d8STakashi Iwai static int snd_hdsp_midi_input_close(struct snd_rawmidi_substream *substream)
13761da177e4SLinus Torvalds {
137755e957d8STakashi Iwai 	struct hdsp_midi *hmidi;
13781da177e4SLinus Torvalds 
13791da177e4SLinus Torvalds 	snd_hdsp_midi_input_trigger (substream, 0);
13801da177e4SLinus Torvalds 
138155e957d8STakashi Iwai 	hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
13821da177e4SLinus Torvalds 	spin_lock_irq (&hmidi->lock);
13831da177e4SLinus Torvalds 	hmidi->input = NULL;
13841da177e4SLinus Torvalds 	spin_unlock_irq (&hmidi->lock);
13851da177e4SLinus Torvalds 
13861da177e4SLinus Torvalds 	return 0;
13871da177e4SLinus Torvalds }
13881da177e4SLinus Torvalds 
138955e957d8STakashi Iwai static int snd_hdsp_midi_output_close(struct snd_rawmidi_substream *substream)
13901da177e4SLinus Torvalds {
139155e957d8STakashi Iwai 	struct hdsp_midi *hmidi;
13921da177e4SLinus Torvalds 
13931da177e4SLinus Torvalds 	snd_hdsp_midi_output_trigger (substream, 0);
13941da177e4SLinus Torvalds 
139555e957d8STakashi Iwai 	hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
13961da177e4SLinus Torvalds 	spin_lock_irq (&hmidi->lock);
13971da177e4SLinus Torvalds 	hmidi->output = NULL;
13981da177e4SLinus Torvalds 	spin_unlock_irq (&hmidi->lock);
13991da177e4SLinus Torvalds 
14001da177e4SLinus Torvalds 	return 0;
14011da177e4SLinus Torvalds }
14021da177e4SLinus Torvalds 
140355e957d8STakashi Iwai static struct snd_rawmidi_ops snd_hdsp_midi_output =
14041da177e4SLinus Torvalds {
14051da177e4SLinus Torvalds 	.open =		snd_hdsp_midi_output_open,
14061da177e4SLinus Torvalds 	.close =	snd_hdsp_midi_output_close,
14071da177e4SLinus Torvalds 	.trigger =	snd_hdsp_midi_output_trigger,
14081da177e4SLinus Torvalds };
14091da177e4SLinus Torvalds 
141055e957d8STakashi Iwai static struct snd_rawmidi_ops snd_hdsp_midi_input =
14111da177e4SLinus Torvalds {
14121da177e4SLinus Torvalds 	.open =		snd_hdsp_midi_input_open,
14131da177e4SLinus Torvalds 	.close =	snd_hdsp_midi_input_close,
14141da177e4SLinus Torvalds 	.trigger =	snd_hdsp_midi_input_trigger,
14151da177e4SLinus Torvalds };
14161da177e4SLinus Torvalds 
1417f40b6890STakashi Iwai static int snd_hdsp_create_midi (struct snd_card *card, struct hdsp *hdsp, int id)
14181da177e4SLinus Torvalds {
14191da177e4SLinus Torvalds 	char buf[32];
14201da177e4SLinus Torvalds 
14211da177e4SLinus Torvalds 	hdsp->midi[id].id = id;
14221da177e4SLinus Torvalds 	hdsp->midi[id].rmidi = NULL;
14231da177e4SLinus Torvalds 	hdsp->midi[id].input = NULL;
14241da177e4SLinus Torvalds 	hdsp->midi[id].output = NULL;
14251da177e4SLinus Torvalds 	hdsp->midi[id].hdsp = hdsp;
14261da177e4SLinus Torvalds 	hdsp->midi[id].istimer = 0;
14271da177e4SLinus Torvalds 	hdsp->midi[id].pending = 0;
14281da177e4SLinus Torvalds 	spin_lock_init (&hdsp->midi[id].lock);
14291da177e4SLinus Torvalds 
14301da177e4SLinus Torvalds 	sprintf (buf, "%s MIDI %d", card->shortname, id+1);
1431b0b98119STakashi Iwai 	if (snd_rawmidi_new (card, buf, id, 1, 1, &hdsp->midi[id].rmidi) < 0)
14321da177e4SLinus Torvalds 		return -1;
14331da177e4SLinus Torvalds 
14341da177e4SLinus Torvalds 	sprintf (hdsp->midi[id].rmidi->name, "%s MIDI %d", card->id, id+1);
14351da177e4SLinus Torvalds 	hdsp->midi[id].rmidi->private_data = &hdsp->midi[id];
14361da177e4SLinus Torvalds 
14371da177e4SLinus Torvalds 	snd_rawmidi_set_ops (hdsp->midi[id].rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_hdsp_midi_output);
14381da177e4SLinus Torvalds 	snd_rawmidi_set_ops (hdsp->midi[id].rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_hdsp_midi_input);
14391da177e4SLinus Torvalds 
14401da177e4SLinus Torvalds 	hdsp->midi[id].rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT |
14411da177e4SLinus Torvalds 		SNDRV_RAWMIDI_INFO_INPUT |
14421da177e4SLinus Torvalds 		SNDRV_RAWMIDI_INFO_DUPLEX;
14431da177e4SLinus Torvalds 
14441da177e4SLinus Torvalds 	return 0;
14451da177e4SLinus Torvalds }
14461da177e4SLinus Torvalds 
14471da177e4SLinus Torvalds /*-----------------------------------------------------------------------------
14481da177e4SLinus Torvalds   Control Interface
14491da177e4SLinus Torvalds   ----------------------------------------------------------------------------*/
14501da177e4SLinus Torvalds 
145155e957d8STakashi Iwai static u32 snd_hdsp_convert_from_aes(struct snd_aes_iec958 *aes)
14521da177e4SLinus Torvalds {
14531da177e4SLinus Torvalds 	u32 val = 0;
14541da177e4SLinus Torvalds 	val |= (aes->status[0] & IEC958_AES0_PROFESSIONAL) ? HDSP_SPDIFProfessional : 0;
14551da177e4SLinus Torvalds 	val |= (aes->status[0] & IEC958_AES0_NONAUDIO) ? HDSP_SPDIFNonAudio : 0;
14561da177e4SLinus Torvalds 	if (val & HDSP_SPDIFProfessional)
14571da177e4SLinus Torvalds 		val |= (aes->status[0] & IEC958_AES0_PRO_EMPHASIS_5015) ? HDSP_SPDIFEmphasis : 0;
14581da177e4SLinus Torvalds 	else
14591da177e4SLinus Torvalds 		val |= (aes->status[0] & IEC958_AES0_CON_EMPHASIS_5015) ? HDSP_SPDIFEmphasis : 0;
14601da177e4SLinus Torvalds 	return val;
14611da177e4SLinus Torvalds }
14621da177e4SLinus Torvalds 
146355e957d8STakashi Iwai static void snd_hdsp_convert_to_aes(struct snd_aes_iec958 *aes, u32 val)
14641da177e4SLinus Torvalds {
14651da177e4SLinus Torvalds 	aes->status[0] = ((val & HDSP_SPDIFProfessional) ? IEC958_AES0_PROFESSIONAL : 0) |
14661da177e4SLinus Torvalds 			 ((val & HDSP_SPDIFNonAudio) ? IEC958_AES0_NONAUDIO : 0);
14671da177e4SLinus Torvalds 	if (val & HDSP_SPDIFProfessional)
14681da177e4SLinus Torvalds 		aes->status[0] |= (val & HDSP_SPDIFEmphasis) ? IEC958_AES0_PRO_EMPHASIS_5015 : 0;
14691da177e4SLinus Torvalds 	else
14701da177e4SLinus Torvalds 		aes->status[0] |= (val & HDSP_SPDIFEmphasis) ? IEC958_AES0_CON_EMPHASIS_5015 : 0;
14711da177e4SLinus Torvalds }
14721da177e4SLinus Torvalds 
147355e957d8STakashi Iwai static int snd_hdsp_control_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
14741da177e4SLinus Torvalds {
14751da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
14761da177e4SLinus Torvalds 	uinfo->count = 1;
14771da177e4SLinus Torvalds 	return 0;
14781da177e4SLinus Torvalds }
14791da177e4SLinus Torvalds 
148055e957d8STakashi Iwai static int snd_hdsp_control_spdif_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
14811da177e4SLinus Torvalds {
148255e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
14831da177e4SLinus Torvalds 
14841da177e4SLinus Torvalds 	snd_hdsp_convert_to_aes(&ucontrol->value.iec958, hdsp->creg_spdif);
14851da177e4SLinus Torvalds 	return 0;
14861da177e4SLinus Torvalds }
14871da177e4SLinus Torvalds 
148855e957d8STakashi Iwai static int snd_hdsp_control_spdif_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
14891da177e4SLinus Torvalds {
149055e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
14911da177e4SLinus Torvalds 	int change;
14921da177e4SLinus Torvalds 	u32 val;
14931da177e4SLinus Torvalds 
14941da177e4SLinus Torvalds 	val = snd_hdsp_convert_from_aes(&ucontrol->value.iec958);
14951da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
14961da177e4SLinus Torvalds 	change = val != hdsp->creg_spdif;
14971da177e4SLinus Torvalds 	hdsp->creg_spdif = val;
14981da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
14991da177e4SLinus Torvalds 	return change;
15001da177e4SLinus Torvalds }
15011da177e4SLinus Torvalds 
150255e957d8STakashi Iwai static int snd_hdsp_control_spdif_stream_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
15031da177e4SLinus Torvalds {
15041da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
15051da177e4SLinus Torvalds 	uinfo->count = 1;
15061da177e4SLinus Torvalds 	return 0;
15071da177e4SLinus Torvalds }
15081da177e4SLinus Torvalds 
150955e957d8STakashi Iwai static int snd_hdsp_control_spdif_stream_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
15101da177e4SLinus Torvalds {
151155e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
15121da177e4SLinus Torvalds 
15131da177e4SLinus Torvalds 	snd_hdsp_convert_to_aes(&ucontrol->value.iec958, hdsp->creg_spdif_stream);
15141da177e4SLinus Torvalds 	return 0;
15151da177e4SLinus Torvalds }
15161da177e4SLinus Torvalds 
151755e957d8STakashi Iwai static int snd_hdsp_control_spdif_stream_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
15181da177e4SLinus Torvalds {
151955e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
15201da177e4SLinus Torvalds 	int change;
15211da177e4SLinus Torvalds 	u32 val;
15221da177e4SLinus Torvalds 
15231da177e4SLinus Torvalds 	val = snd_hdsp_convert_from_aes(&ucontrol->value.iec958);
15241da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
15251da177e4SLinus Torvalds 	change = val != hdsp->creg_spdif_stream;
15261da177e4SLinus Torvalds 	hdsp->creg_spdif_stream = val;
15271da177e4SLinus Torvalds 	hdsp->control_register &= ~(HDSP_SPDIFProfessional | HDSP_SPDIFNonAudio | HDSP_SPDIFEmphasis);
15281da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register |= val);
15291da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
15301da177e4SLinus Torvalds 	return change;
15311da177e4SLinus Torvalds }
15321da177e4SLinus Torvalds 
153355e957d8STakashi Iwai static int snd_hdsp_control_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
15341da177e4SLinus Torvalds {
15351da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
15361da177e4SLinus Torvalds 	uinfo->count = 1;
15371da177e4SLinus Torvalds 	return 0;
15381da177e4SLinus Torvalds }
15391da177e4SLinus Torvalds 
154055e957d8STakashi Iwai static int snd_hdsp_control_spdif_mask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
15411da177e4SLinus Torvalds {
15421da177e4SLinus Torvalds 	ucontrol->value.iec958.status[0] = kcontrol->private_value;
15431da177e4SLinus Torvalds 	return 0;
15441da177e4SLinus Torvalds }
15451da177e4SLinus Torvalds 
15461da177e4SLinus Torvalds #define HDSP_SPDIF_IN(xname, xindex) \
154767ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
15481da177e4SLinus Torvalds   .name = xname, \
15491da177e4SLinus Torvalds   .index = xindex, \
15501da177e4SLinus Torvalds   .info = snd_hdsp_info_spdif_in, \
15511da177e4SLinus Torvalds   .get = snd_hdsp_get_spdif_in, \
15521da177e4SLinus Torvalds   .put = snd_hdsp_put_spdif_in }
15531da177e4SLinus Torvalds 
155455e957d8STakashi Iwai static unsigned int hdsp_spdif_in(struct hdsp *hdsp)
15551da177e4SLinus Torvalds {
15561da177e4SLinus Torvalds 	return hdsp_decode_spdif_in(hdsp->control_register & HDSP_SPDIFInputMask);
15571da177e4SLinus Torvalds }
15581da177e4SLinus Torvalds 
155955e957d8STakashi Iwai static int hdsp_set_spdif_input(struct hdsp *hdsp, int in)
15601da177e4SLinus Torvalds {
15611da177e4SLinus Torvalds 	hdsp->control_register &= ~HDSP_SPDIFInputMask;
15621da177e4SLinus Torvalds 	hdsp->control_register |= hdsp_encode_spdif_in(in);
15631da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
15641da177e4SLinus Torvalds 	return 0;
15651da177e4SLinus Torvalds }
15661da177e4SLinus Torvalds 
156755e957d8STakashi Iwai static int snd_hdsp_info_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
15681da177e4SLinus Torvalds {
15691da177e4SLinus Torvalds 	static char *texts[4] = {"Optical", "Coaxial", "Internal", "AES"};
157055e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
15711da177e4SLinus Torvalds 
15721da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
15731da177e4SLinus Torvalds 	uinfo->count = 1;
15741da177e4SLinus Torvalds 	uinfo->value.enumerated.items = ((hdsp->io_type == H9632) ? 4 : 3);
15751da177e4SLinus Torvalds 	if (uinfo->value.enumerated.item > ((hdsp->io_type == H9632) ? 3 : 2))
15761da177e4SLinus Torvalds 		uinfo->value.enumerated.item = ((hdsp->io_type == H9632) ? 3 : 2);
15771da177e4SLinus Torvalds 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
15781da177e4SLinus Torvalds 	return 0;
15791da177e4SLinus Torvalds }
15801da177e4SLinus Torvalds 
158155e957d8STakashi Iwai static int snd_hdsp_get_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
15821da177e4SLinus Torvalds {
158355e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
15841da177e4SLinus Torvalds 
15851da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp_spdif_in(hdsp);
15861da177e4SLinus Torvalds 	return 0;
15871da177e4SLinus Torvalds }
15881da177e4SLinus Torvalds 
158955e957d8STakashi Iwai static int snd_hdsp_put_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
15901da177e4SLinus Torvalds {
159155e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
15921da177e4SLinus Torvalds 	int change;
15931da177e4SLinus Torvalds 	unsigned int val;
15941da177e4SLinus Torvalds 
15951da177e4SLinus Torvalds 	if (!snd_hdsp_use_is_exclusive(hdsp))
15961da177e4SLinus Torvalds 		return -EBUSY;
15971da177e4SLinus Torvalds 	val = ucontrol->value.enumerated.item[0] % ((hdsp->io_type == H9632) ? 4 : 3);
15981da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
15991da177e4SLinus Torvalds 	change = val != hdsp_spdif_in(hdsp);
16001da177e4SLinus Torvalds 	if (change)
16011da177e4SLinus Torvalds 		hdsp_set_spdif_input(hdsp, val);
16021da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
16031da177e4SLinus Torvalds 	return change;
16041da177e4SLinus Torvalds }
16051da177e4SLinus Torvalds 
16061da177e4SLinus Torvalds #define HDSP_SPDIF_OUT(xname, xindex) \
160767ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
16081da177e4SLinus Torvalds   .info = snd_hdsp_info_spdif_bits, \
16091da177e4SLinus Torvalds   .get = snd_hdsp_get_spdif_out, .put = snd_hdsp_put_spdif_out }
16101da177e4SLinus Torvalds 
161155e957d8STakashi Iwai static int hdsp_spdif_out(struct hdsp *hdsp)
16121da177e4SLinus Torvalds {
16131da177e4SLinus Torvalds 	return (hdsp->control_register & HDSP_SPDIFOpticalOut) ? 1 : 0;
16141da177e4SLinus Torvalds }
16151da177e4SLinus Torvalds 
161655e957d8STakashi Iwai static int hdsp_set_spdif_output(struct hdsp *hdsp, int out)
16171da177e4SLinus Torvalds {
1618b0b98119STakashi Iwai 	if (out)
16191da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_SPDIFOpticalOut;
1620b0b98119STakashi Iwai 	else
16211da177e4SLinus Torvalds 		hdsp->control_register &= ~HDSP_SPDIFOpticalOut;
16221da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
16231da177e4SLinus Torvalds 	return 0;
16241da177e4SLinus Torvalds }
16251da177e4SLinus Torvalds 
1626a5ce8890STakashi Iwai #define snd_hdsp_info_spdif_bits	snd_ctl_boolean_mono_info
16271da177e4SLinus Torvalds 
162855e957d8STakashi Iwai static int snd_hdsp_get_spdif_out(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
16291da177e4SLinus Torvalds {
163055e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
16311da177e4SLinus Torvalds 
16321da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = hdsp_spdif_out(hdsp);
16331da177e4SLinus Torvalds 	return 0;
16341da177e4SLinus Torvalds }
16351da177e4SLinus Torvalds 
163655e957d8STakashi Iwai static int snd_hdsp_put_spdif_out(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
16371da177e4SLinus Torvalds {
163855e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
16391da177e4SLinus Torvalds 	int change;
16401da177e4SLinus Torvalds 	unsigned int val;
16411da177e4SLinus Torvalds 
16421da177e4SLinus Torvalds 	if (!snd_hdsp_use_is_exclusive(hdsp))
16431da177e4SLinus Torvalds 		return -EBUSY;
16441da177e4SLinus Torvalds 	val = ucontrol->value.integer.value[0] & 1;
16451da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
16461da177e4SLinus Torvalds 	change = (int)val != hdsp_spdif_out(hdsp);
16471da177e4SLinus Torvalds 	hdsp_set_spdif_output(hdsp, val);
16481da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
16491da177e4SLinus Torvalds 	return change;
16501da177e4SLinus Torvalds }
16511da177e4SLinus Torvalds 
16521da177e4SLinus Torvalds #define HDSP_SPDIF_PROFESSIONAL(xname, xindex) \
165367ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
16541da177e4SLinus Torvalds   .info = snd_hdsp_info_spdif_bits, \
16551da177e4SLinus Torvalds   .get = snd_hdsp_get_spdif_professional, .put = snd_hdsp_put_spdif_professional }
16561da177e4SLinus Torvalds 
165755e957d8STakashi Iwai static int hdsp_spdif_professional(struct hdsp *hdsp)
16581da177e4SLinus Torvalds {
16591da177e4SLinus Torvalds 	return (hdsp->control_register & HDSP_SPDIFProfessional) ? 1 : 0;
16601da177e4SLinus Torvalds }
16611da177e4SLinus Torvalds 
166255e957d8STakashi Iwai static int hdsp_set_spdif_professional(struct hdsp *hdsp, int val)
16631da177e4SLinus Torvalds {
1664b0b98119STakashi Iwai 	if (val)
16651da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_SPDIFProfessional;
1666b0b98119STakashi Iwai 	else
16671da177e4SLinus Torvalds 		hdsp->control_register &= ~HDSP_SPDIFProfessional;
16681da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
16691da177e4SLinus Torvalds 	return 0;
16701da177e4SLinus Torvalds }
16711da177e4SLinus Torvalds 
167255e957d8STakashi Iwai static int snd_hdsp_get_spdif_professional(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
16731da177e4SLinus Torvalds {
167455e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
16751da177e4SLinus Torvalds 
16761da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = hdsp_spdif_professional(hdsp);
16771da177e4SLinus Torvalds 	return 0;
16781da177e4SLinus Torvalds }
16791da177e4SLinus Torvalds 
168055e957d8STakashi Iwai static int snd_hdsp_put_spdif_professional(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
16811da177e4SLinus Torvalds {
168255e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
16831da177e4SLinus Torvalds 	int change;
16841da177e4SLinus Torvalds 	unsigned int val;
16851da177e4SLinus Torvalds 
16861da177e4SLinus Torvalds 	if (!snd_hdsp_use_is_exclusive(hdsp))
16871da177e4SLinus Torvalds 		return -EBUSY;
16881da177e4SLinus Torvalds 	val = ucontrol->value.integer.value[0] & 1;
16891da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
16901da177e4SLinus Torvalds 	change = (int)val != hdsp_spdif_professional(hdsp);
16911da177e4SLinus Torvalds 	hdsp_set_spdif_professional(hdsp, val);
16921da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
16931da177e4SLinus Torvalds 	return change;
16941da177e4SLinus Torvalds }
16951da177e4SLinus Torvalds 
16961da177e4SLinus Torvalds #define HDSP_SPDIF_EMPHASIS(xname, xindex) \
169767ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
16981da177e4SLinus Torvalds   .info = snd_hdsp_info_spdif_bits, \
16991da177e4SLinus Torvalds   .get = snd_hdsp_get_spdif_emphasis, .put = snd_hdsp_put_spdif_emphasis }
17001da177e4SLinus Torvalds 
170155e957d8STakashi Iwai static int hdsp_spdif_emphasis(struct hdsp *hdsp)
17021da177e4SLinus Torvalds {
17031da177e4SLinus Torvalds 	return (hdsp->control_register & HDSP_SPDIFEmphasis) ? 1 : 0;
17041da177e4SLinus Torvalds }
17051da177e4SLinus Torvalds 
170655e957d8STakashi Iwai static int hdsp_set_spdif_emphasis(struct hdsp *hdsp, int val)
17071da177e4SLinus Torvalds {
1708b0b98119STakashi Iwai 	if (val)
17091da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_SPDIFEmphasis;
1710b0b98119STakashi Iwai 	else
17111da177e4SLinus Torvalds 		hdsp->control_register &= ~HDSP_SPDIFEmphasis;
17121da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
17131da177e4SLinus Torvalds 	return 0;
17141da177e4SLinus Torvalds }
17151da177e4SLinus Torvalds 
171655e957d8STakashi Iwai static int snd_hdsp_get_spdif_emphasis(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
17171da177e4SLinus Torvalds {
171855e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
17191da177e4SLinus Torvalds 
17201da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = hdsp_spdif_emphasis(hdsp);
17211da177e4SLinus Torvalds 	return 0;
17221da177e4SLinus Torvalds }
17231da177e4SLinus Torvalds 
172455e957d8STakashi Iwai static int snd_hdsp_put_spdif_emphasis(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
17251da177e4SLinus Torvalds {
172655e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
17271da177e4SLinus Torvalds 	int change;
17281da177e4SLinus Torvalds 	unsigned int val;
17291da177e4SLinus Torvalds 
17301da177e4SLinus Torvalds 	if (!snd_hdsp_use_is_exclusive(hdsp))
17311da177e4SLinus Torvalds 		return -EBUSY;
17321da177e4SLinus Torvalds 	val = ucontrol->value.integer.value[0] & 1;
17331da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
17341da177e4SLinus Torvalds 	change = (int)val != hdsp_spdif_emphasis(hdsp);
17351da177e4SLinus Torvalds 	hdsp_set_spdif_emphasis(hdsp, val);
17361da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
17371da177e4SLinus Torvalds 	return change;
17381da177e4SLinus Torvalds }
17391da177e4SLinus Torvalds 
17401da177e4SLinus Torvalds #define HDSP_SPDIF_NON_AUDIO(xname, xindex) \
174167ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
17421da177e4SLinus Torvalds   .info = snd_hdsp_info_spdif_bits, \
17431da177e4SLinus Torvalds   .get = snd_hdsp_get_spdif_nonaudio, .put = snd_hdsp_put_spdif_nonaudio }
17441da177e4SLinus Torvalds 
174555e957d8STakashi Iwai static int hdsp_spdif_nonaudio(struct hdsp *hdsp)
17461da177e4SLinus Torvalds {
17471da177e4SLinus Torvalds 	return (hdsp->control_register & HDSP_SPDIFNonAudio) ? 1 : 0;
17481da177e4SLinus Torvalds }
17491da177e4SLinus Torvalds 
175055e957d8STakashi Iwai static int hdsp_set_spdif_nonaudio(struct hdsp *hdsp, int val)
17511da177e4SLinus Torvalds {
1752b0b98119STakashi Iwai 	if (val)
17531da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_SPDIFNonAudio;
1754b0b98119STakashi Iwai 	else
17551da177e4SLinus Torvalds 		hdsp->control_register &= ~HDSP_SPDIFNonAudio;
17561da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
17571da177e4SLinus Torvalds 	return 0;
17581da177e4SLinus Torvalds }
17591da177e4SLinus Torvalds 
176055e957d8STakashi Iwai static int snd_hdsp_get_spdif_nonaudio(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
17611da177e4SLinus Torvalds {
176255e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
17631da177e4SLinus Torvalds 
17641da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = hdsp_spdif_nonaudio(hdsp);
17651da177e4SLinus Torvalds 	return 0;
17661da177e4SLinus Torvalds }
17671da177e4SLinus Torvalds 
176855e957d8STakashi Iwai static int snd_hdsp_put_spdif_nonaudio(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
17691da177e4SLinus Torvalds {
177055e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
17711da177e4SLinus Torvalds 	int change;
17721da177e4SLinus Torvalds 	unsigned int val;
17731da177e4SLinus Torvalds 
17741da177e4SLinus Torvalds 	if (!snd_hdsp_use_is_exclusive(hdsp))
17751da177e4SLinus Torvalds 		return -EBUSY;
17761da177e4SLinus Torvalds 	val = ucontrol->value.integer.value[0] & 1;
17771da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
17781da177e4SLinus Torvalds 	change = (int)val != hdsp_spdif_nonaudio(hdsp);
17791da177e4SLinus Torvalds 	hdsp_set_spdif_nonaudio(hdsp, val);
17801da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
17811da177e4SLinus Torvalds 	return change;
17821da177e4SLinus Torvalds }
17831da177e4SLinus Torvalds 
17841da177e4SLinus Torvalds #define HDSP_SPDIF_SAMPLE_RATE(xname, xindex) \
178567ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
17861da177e4SLinus Torvalds   .name = xname, \
17871da177e4SLinus Torvalds   .index = xindex, \
17881da177e4SLinus Torvalds   .access = SNDRV_CTL_ELEM_ACCESS_READ, \
17891da177e4SLinus Torvalds   .info = snd_hdsp_info_spdif_sample_rate, \
17901da177e4SLinus Torvalds   .get = snd_hdsp_get_spdif_sample_rate \
17911da177e4SLinus Torvalds }
17921da177e4SLinus Torvalds 
179355e957d8STakashi Iwai static int snd_hdsp_info_spdif_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
17941da177e4SLinus Torvalds {
17951da177e4SLinus Torvalds 	static char *texts[] = {"32000", "44100", "48000", "64000", "88200", "96000", "None", "128000", "176400", "192000"};
179655e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
17971da177e4SLinus Torvalds 
17981da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
17991da177e4SLinus Torvalds 	uinfo->count = 1;
18001da177e4SLinus Torvalds 	uinfo->value.enumerated.items = (hdsp->io_type == H9632) ? 10 : 7;
18011da177e4SLinus Torvalds 	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
18021da177e4SLinus Torvalds 		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
18031da177e4SLinus Torvalds 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
18041da177e4SLinus Torvalds 	return 0;
18051da177e4SLinus Torvalds }
18061da177e4SLinus Torvalds 
180755e957d8STakashi Iwai static int snd_hdsp_get_spdif_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
18081da177e4SLinus Torvalds {
180955e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
18101da177e4SLinus Torvalds 
18111da177e4SLinus Torvalds 	switch (hdsp_spdif_sample_rate(hdsp)) {
18121da177e4SLinus Torvalds 	case 32000:
18131da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 0;
18141da177e4SLinus Torvalds 		break;
18151da177e4SLinus Torvalds 	case 44100:
18161da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 1;
18171da177e4SLinus Torvalds 		break;
18181da177e4SLinus Torvalds 	case 48000:
18191da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 2;
18201da177e4SLinus Torvalds 		break;
18211da177e4SLinus Torvalds 	case 64000:
18221da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 3;
18231da177e4SLinus Torvalds 		break;
18241da177e4SLinus Torvalds 	case 88200:
18251da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 4;
18261da177e4SLinus Torvalds 		break;
18271da177e4SLinus Torvalds 	case 96000:
18281da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 5;
18291da177e4SLinus Torvalds 		break;
18301da177e4SLinus Torvalds 	case 128000:
18311da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 7;
18321da177e4SLinus Torvalds 		break;
18331da177e4SLinus Torvalds 	case 176400:
18341da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 8;
18351da177e4SLinus Torvalds 		break;
18361da177e4SLinus Torvalds 	case 192000:
18371da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 9;
18381da177e4SLinus Torvalds 		break;
18391da177e4SLinus Torvalds 	default:
18401da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 6;
18411da177e4SLinus Torvalds 	}
18421da177e4SLinus Torvalds 	return 0;
18431da177e4SLinus Torvalds }
18441da177e4SLinus Torvalds 
18451da177e4SLinus Torvalds #define HDSP_SYSTEM_SAMPLE_RATE(xname, xindex) \
184667ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
18471da177e4SLinus Torvalds   .name = xname, \
18481da177e4SLinus Torvalds   .index = xindex, \
18491da177e4SLinus Torvalds   .access = SNDRV_CTL_ELEM_ACCESS_READ, \
18501da177e4SLinus Torvalds   .info = snd_hdsp_info_system_sample_rate, \
18511da177e4SLinus Torvalds   .get = snd_hdsp_get_system_sample_rate \
18521da177e4SLinus Torvalds }
18531da177e4SLinus Torvalds 
185455e957d8STakashi Iwai static int snd_hdsp_info_system_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
18551da177e4SLinus Torvalds {
18561da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
18571da177e4SLinus Torvalds 	uinfo->count = 1;
18581da177e4SLinus Torvalds 	return 0;
18591da177e4SLinus Torvalds }
18601da177e4SLinus Torvalds 
186155e957d8STakashi Iwai static int snd_hdsp_get_system_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
18621da177e4SLinus Torvalds {
186355e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
18641da177e4SLinus Torvalds 
18651da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp->system_sample_rate;
18661da177e4SLinus Torvalds 	return 0;
18671da177e4SLinus Torvalds }
18681da177e4SLinus Torvalds 
18691da177e4SLinus Torvalds #define HDSP_AUTOSYNC_SAMPLE_RATE(xname, xindex) \
187067ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
18711da177e4SLinus Torvalds   .name = xname, \
18721da177e4SLinus Torvalds   .index = xindex, \
18731da177e4SLinus Torvalds   .access = SNDRV_CTL_ELEM_ACCESS_READ, \
18741da177e4SLinus Torvalds   .info = snd_hdsp_info_autosync_sample_rate, \
18751da177e4SLinus Torvalds   .get = snd_hdsp_get_autosync_sample_rate \
18761da177e4SLinus Torvalds }
18771da177e4SLinus Torvalds 
187855e957d8STakashi Iwai static int snd_hdsp_info_autosync_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
18791da177e4SLinus Torvalds {
188055e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
18811da177e4SLinus Torvalds 	static char *texts[] = {"32000", "44100", "48000", "64000", "88200", "96000", "None", "128000", "176400", "192000"};
18821da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
18831da177e4SLinus Torvalds 	uinfo->count = 1;
18841da177e4SLinus Torvalds 	uinfo->value.enumerated.items = (hdsp->io_type == H9632) ? 10 : 7 ;
18851da177e4SLinus Torvalds 	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
18861da177e4SLinus Torvalds 		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
18871da177e4SLinus Torvalds 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
18881da177e4SLinus Torvalds 	return 0;
18891da177e4SLinus Torvalds }
18901da177e4SLinus Torvalds 
189155e957d8STakashi Iwai static int snd_hdsp_get_autosync_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
18921da177e4SLinus Torvalds {
189355e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
18941da177e4SLinus Torvalds 
18951da177e4SLinus Torvalds 	switch (hdsp_external_sample_rate(hdsp)) {
18961da177e4SLinus Torvalds 	case 32000:
18971da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 0;
18981da177e4SLinus Torvalds 		break;
18991da177e4SLinus Torvalds 	case 44100:
19001da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 1;
19011da177e4SLinus Torvalds 		break;
19021da177e4SLinus Torvalds 	case 48000:
19031da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 2;
19041da177e4SLinus Torvalds 		break;
19051da177e4SLinus Torvalds 	case 64000:
19061da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 3;
19071da177e4SLinus Torvalds 		break;
19081da177e4SLinus Torvalds 	case 88200:
19091da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 4;
19101da177e4SLinus Torvalds 		break;
19111da177e4SLinus Torvalds 	case 96000:
19121da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 5;
19131da177e4SLinus Torvalds 		break;
19141da177e4SLinus Torvalds 	case 128000:
19151da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 7;
19161da177e4SLinus Torvalds 		break;
19171da177e4SLinus Torvalds 	case 176400:
19181da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 8;
19191da177e4SLinus Torvalds 		break;
19201da177e4SLinus Torvalds 	case 192000:
19211da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 9;
19221da177e4SLinus Torvalds 		break;
19231da177e4SLinus Torvalds 	default:
19241da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 6;
19251da177e4SLinus Torvalds 	}
19261da177e4SLinus Torvalds 	return 0;
19271da177e4SLinus Torvalds }
19281da177e4SLinus Torvalds 
19291da177e4SLinus Torvalds #define HDSP_SYSTEM_CLOCK_MODE(xname, xindex) \
193067ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
19311da177e4SLinus Torvalds   .name = xname, \
19321da177e4SLinus Torvalds   .index = xindex, \
19331da177e4SLinus Torvalds   .access = SNDRV_CTL_ELEM_ACCESS_READ, \
19341da177e4SLinus Torvalds   .info = snd_hdsp_info_system_clock_mode, \
19351da177e4SLinus Torvalds   .get = snd_hdsp_get_system_clock_mode \
19361da177e4SLinus Torvalds }
19371da177e4SLinus Torvalds 
193855e957d8STakashi Iwai static int hdsp_system_clock_mode(struct hdsp *hdsp)
19391da177e4SLinus Torvalds {
1940b0b98119STakashi Iwai 	if (hdsp->control_register & HDSP_ClockModeMaster)
19411da177e4SLinus Torvalds 		return 0;
1942b0b98119STakashi Iwai 	else if (hdsp_external_sample_rate(hdsp) != hdsp->system_sample_rate)
19431da177e4SLinus Torvalds 			return 0;
19441da177e4SLinus Torvalds 	return 1;
19451da177e4SLinus Torvalds }
19461da177e4SLinus Torvalds 
194755e957d8STakashi Iwai static int snd_hdsp_info_system_clock_mode(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
19481da177e4SLinus Torvalds {
19491da177e4SLinus Torvalds 	static char *texts[] = {"Master", "Slave" };
19501da177e4SLinus Torvalds 
19511da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
19521da177e4SLinus Torvalds 	uinfo->count = 1;
19531da177e4SLinus Torvalds 	uinfo->value.enumerated.items = 2;
19541da177e4SLinus Torvalds 	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
19551da177e4SLinus Torvalds 		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
19561da177e4SLinus Torvalds 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
19571da177e4SLinus Torvalds 	return 0;
19581da177e4SLinus Torvalds }
19591da177e4SLinus Torvalds 
196055e957d8STakashi Iwai static int snd_hdsp_get_system_clock_mode(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
19611da177e4SLinus Torvalds {
196255e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
19631da177e4SLinus Torvalds 
19641da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp_system_clock_mode(hdsp);
19651da177e4SLinus Torvalds 	return 0;
19661da177e4SLinus Torvalds }
19671da177e4SLinus Torvalds 
19681da177e4SLinus Torvalds #define HDSP_CLOCK_SOURCE(xname, xindex) \
196967ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
19701da177e4SLinus Torvalds   .name = xname, \
19711da177e4SLinus Torvalds   .index = xindex, \
19721da177e4SLinus Torvalds   .info = snd_hdsp_info_clock_source, \
19731da177e4SLinus Torvalds   .get = snd_hdsp_get_clock_source, \
19741da177e4SLinus Torvalds   .put = snd_hdsp_put_clock_source \
19751da177e4SLinus Torvalds }
19761da177e4SLinus Torvalds 
197755e957d8STakashi Iwai static int hdsp_clock_source(struct hdsp *hdsp)
19781da177e4SLinus Torvalds {
19791da177e4SLinus Torvalds 	if (hdsp->control_register & HDSP_ClockModeMaster) {
19801da177e4SLinus Torvalds 		switch (hdsp->system_sample_rate) {
19811da177e4SLinus Torvalds 		case 32000:
19821da177e4SLinus Torvalds 			return 1;
19831da177e4SLinus Torvalds 		case 44100:
19841da177e4SLinus Torvalds 			return 2;
19851da177e4SLinus Torvalds 		case 48000:
19861da177e4SLinus Torvalds 			return 3;
19871da177e4SLinus Torvalds 		case 64000:
19881da177e4SLinus Torvalds 			return 4;
19891da177e4SLinus Torvalds 		case 88200:
19901da177e4SLinus Torvalds 			return 5;
19911da177e4SLinus Torvalds 		case 96000:
19921da177e4SLinus Torvalds 			return 6;
19931da177e4SLinus Torvalds 		case 128000:
19941da177e4SLinus Torvalds 			return 7;
19951da177e4SLinus Torvalds 		case 176400:
19961da177e4SLinus Torvalds 			return 8;
19971da177e4SLinus Torvalds 		case 192000:
19981da177e4SLinus Torvalds 			return 9;
19991da177e4SLinus Torvalds 		default:
20001da177e4SLinus Torvalds 			return 3;
20011da177e4SLinus Torvalds 		}
20021da177e4SLinus Torvalds 	} else {
20031da177e4SLinus Torvalds 		return 0;
20041da177e4SLinus Torvalds 	}
20051da177e4SLinus Torvalds }
20061da177e4SLinus Torvalds 
200755e957d8STakashi Iwai static int hdsp_set_clock_source(struct hdsp *hdsp, int mode)
20081da177e4SLinus Torvalds {
20091da177e4SLinus Torvalds 	int rate;
20101da177e4SLinus Torvalds 	switch (mode) {
20111da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_AUTOSYNC:
20121da177e4SLinus Torvalds 		if (hdsp_external_sample_rate(hdsp) != 0) {
20131da177e4SLinus Torvalds 		    if (!hdsp_set_rate(hdsp, hdsp_external_sample_rate(hdsp), 1)) {
20141da177e4SLinus Torvalds 			hdsp->control_register &= ~HDSP_ClockModeMaster;
20151da177e4SLinus Torvalds 			hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
20161da177e4SLinus Torvalds 			return 0;
20171da177e4SLinus Torvalds 		    }
20181da177e4SLinus Torvalds 		}
20191da177e4SLinus Torvalds 		return -1;
20201da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_32KHZ:
20211da177e4SLinus Torvalds 		rate = 32000;
20221da177e4SLinus Torvalds 		break;
20231da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_44_1KHZ:
20241da177e4SLinus Torvalds 		rate = 44100;
20251da177e4SLinus Torvalds 		break;
20261da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_48KHZ:
20271da177e4SLinus Torvalds 		rate = 48000;
20281da177e4SLinus Torvalds 		break;
20291da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_64KHZ:
20301da177e4SLinus Torvalds 		rate = 64000;
20311da177e4SLinus Torvalds 		break;
20321da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_88_2KHZ:
20331da177e4SLinus Torvalds 		rate = 88200;
20341da177e4SLinus Torvalds 		break;
20351da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_96KHZ:
20361da177e4SLinus Torvalds 		rate = 96000;
20371da177e4SLinus Torvalds 		break;
20381da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_128KHZ:
20391da177e4SLinus Torvalds 		rate = 128000;
20401da177e4SLinus Torvalds 		break;
20411da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_176_4KHZ:
20421da177e4SLinus Torvalds 		rate = 176400;
20431da177e4SLinus Torvalds 		break;
20441da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_192KHZ:
20451da177e4SLinus Torvalds 		rate = 192000;
20461da177e4SLinus Torvalds 		break;
20471da177e4SLinus Torvalds 	default:
20481da177e4SLinus Torvalds 		rate = 48000;
20491da177e4SLinus Torvalds 	}
20501da177e4SLinus Torvalds 	hdsp->control_register |= HDSP_ClockModeMaster;
20511da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
20521da177e4SLinus Torvalds 	hdsp_set_rate(hdsp, rate, 1);
20531da177e4SLinus Torvalds 	return 0;
20541da177e4SLinus Torvalds }
20551da177e4SLinus Torvalds 
205655e957d8STakashi Iwai static int snd_hdsp_info_clock_source(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
20571da177e4SLinus Torvalds {
20581da177e4SLinus 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" };
205955e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
20601da177e4SLinus Torvalds 
20611da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
20621da177e4SLinus Torvalds 	uinfo->count = 1;
20631da177e4SLinus Torvalds 	if (hdsp->io_type == H9632)
20641da177e4SLinus Torvalds 	    uinfo->value.enumerated.items = 10;
20651da177e4SLinus Torvalds 	else
20661da177e4SLinus Torvalds 	    uinfo->value.enumerated.items = 7;
20671da177e4SLinus Torvalds 	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
20681da177e4SLinus Torvalds 		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
20691da177e4SLinus Torvalds 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
20701da177e4SLinus Torvalds 	return 0;
20711da177e4SLinus Torvalds }
20721da177e4SLinus Torvalds 
207355e957d8STakashi Iwai static int snd_hdsp_get_clock_source(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
20741da177e4SLinus Torvalds {
207555e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
20761da177e4SLinus Torvalds 
20771da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp_clock_source(hdsp);
20781da177e4SLinus Torvalds 	return 0;
20791da177e4SLinus Torvalds }
20801da177e4SLinus Torvalds 
208155e957d8STakashi Iwai static int snd_hdsp_put_clock_source(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
20821da177e4SLinus Torvalds {
208355e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
20841da177e4SLinus Torvalds 	int change;
20851da177e4SLinus Torvalds 	int val;
20861da177e4SLinus Torvalds 
20871da177e4SLinus Torvalds 	if (!snd_hdsp_use_is_exclusive(hdsp))
20881da177e4SLinus Torvalds 		return -EBUSY;
20891da177e4SLinus Torvalds 	val = ucontrol->value.enumerated.item[0];
20901da177e4SLinus Torvalds 	if (val < 0) val = 0;
20911da177e4SLinus Torvalds 	if (hdsp->io_type == H9632) {
2092b0b98119STakashi Iwai 		if (val > 9)
2093b0b98119STakashi Iwai 			val = 9;
20941da177e4SLinus Torvalds 	} else {
2095b0b98119STakashi Iwai 		if (val > 6)
2096b0b98119STakashi Iwai 			val = 6;
20971da177e4SLinus Torvalds 	}
20981da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
2099b0b98119STakashi Iwai 	if (val != hdsp_clock_source(hdsp))
21001da177e4SLinus Torvalds 		change = (hdsp_set_clock_source(hdsp, val) == 0) ? 1 : 0;
2101b0b98119STakashi Iwai 	else
21021da177e4SLinus Torvalds 		change = 0;
21031da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
21041da177e4SLinus Torvalds 	return change;
21051da177e4SLinus Torvalds }
21061da177e4SLinus Torvalds 
2107a5ce8890STakashi Iwai #define snd_hdsp_info_clock_source_lock		snd_ctl_boolean_mono_info
2108e3ea4d89STakashi Iwai 
210955e957d8STakashi Iwai static int snd_hdsp_get_clock_source_lock(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2110e3ea4d89STakashi Iwai {
211155e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2112e3ea4d89STakashi Iwai 
2113e3ea4d89STakashi Iwai 	ucontrol->value.integer.value[0] = hdsp->clock_source_locked;
2114e3ea4d89STakashi Iwai 	return 0;
2115e3ea4d89STakashi Iwai }
2116e3ea4d89STakashi Iwai 
211755e957d8STakashi Iwai static int snd_hdsp_put_clock_source_lock(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2118e3ea4d89STakashi Iwai {
211955e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2120e3ea4d89STakashi Iwai 	int change;
2121e3ea4d89STakashi Iwai 
2122e3ea4d89STakashi Iwai 	change = (int)ucontrol->value.integer.value[0] != hdsp->clock_source_locked;
2123e3ea4d89STakashi Iwai 	if (change)
2124e3ea4d89STakashi Iwai 		hdsp->clock_source_locked = ucontrol->value.integer.value[0];
2125e3ea4d89STakashi Iwai 	return change;
2126e3ea4d89STakashi Iwai }
2127e3ea4d89STakashi Iwai 
21281da177e4SLinus Torvalds #define HDSP_DA_GAIN(xname, xindex) \
212967ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
21301da177e4SLinus Torvalds   .name = xname, \
21311da177e4SLinus Torvalds   .index = xindex, \
21321da177e4SLinus Torvalds   .info = snd_hdsp_info_da_gain, \
21331da177e4SLinus Torvalds   .get = snd_hdsp_get_da_gain, \
21341da177e4SLinus Torvalds   .put = snd_hdsp_put_da_gain \
21351da177e4SLinus Torvalds }
21361da177e4SLinus Torvalds 
213755e957d8STakashi Iwai static int hdsp_da_gain(struct hdsp *hdsp)
21381da177e4SLinus Torvalds {
21391da177e4SLinus Torvalds 	switch (hdsp->control_register & HDSP_DAGainMask) {
21401da177e4SLinus Torvalds 	case HDSP_DAGainHighGain:
21411da177e4SLinus Torvalds 		return 0;
21421da177e4SLinus Torvalds 	case HDSP_DAGainPlus4dBu:
21431da177e4SLinus Torvalds 		return 1;
21441da177e4SLinus Torvalds 	case HDSP_DAGainMinus10dBV:
21451da177e4SLinus Torvalds 		return 2;
21461da177e4SLinus Torvalds 	default:
21471da177e4SLinus Torvalds 		return 1;
21481da177e4SLinus Torvalds 	}
21491da177e4SLinus Torvalds }
21501da177e4SLinus Torvalds 
215155e957d8STakashi Iwai static int hdsp_set_da_gain(struct hdsp *hdsp, int mode)
21521da177e4SLinus Torvalds {
21531da177e4SLinus Torvalds 	hdsp->control_register &= ~HDSP_DAGainMask;
21541da177e4SLinus Torvalds 	switch (mode) {
21551da177e4SLinus Torvalds 	case 0:
21561da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_DAGainHighGain;
21571da177e4SLinus Torvalds 		break;
21581da177e4SLinus Torvalds 	case 1:
21591da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_DAGainPlus4dBu;
21601da177e4SLinus Torvalds 		break;
21611da177e4SLinus Torvalds 	case 2:
21621da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_DAGainMinus10dBV;
21631da177e4SLinus Torvalds 		break;
21641da177e4SLinus Torvalds 	default:
21651da177e4SLinus Torvalds 		return -1;
21661da177e4SLinus Torvalds 
21671da177e4SLinus Torvalds 	}
21681da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
21691da177e4SLinus Torvalds 	return 0;
21701da177e4SLinus Torvalds }
21711da177e4SLinus Torvalds 
217255e957d8STakashi Iwai static int snd_hdsp_info_da_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
21731da177e4SLinus Torvalds {
21741da177e4SLinus Torvalds 	static char *texts[] = {"Hi Gain", "+4 dBu", "-10 dbV"};
21751da177e4SLinus Torvalds 
21761da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
21771da177e4SLinus Torvalds 	uinfo->count = 1;
21781da177e4SLinus Torvalds 	uinfo->value.enumerated.items = 3;
21791da177e4SLinus Torvalds 	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
21801da177e4SLinus Torvalds 		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
21811da177e4SLinus Torvalds 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
21821da177e4SLinus Torvalds 	return 0;
21831da177e4SLinus Torvalds }
21841da177e4SLinus Torvalds 
218555e957d8STakashi Iwai static int snd_hdsp_get_da_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
21861da177e4SLinus Torvalds {
218755e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
21881da177e4SLinus Torvalds 
21891da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp_da_gain(hdsp);
21901da177e4SLinus Torvalds 	return 0;
21911da177e4SLinus Torvalds }
21921da177e4SLinus Torvalds 
219355e957d8STakashi Iwai static int snd_hdsp_put_da_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
21941da177e4SLinus Torvalds {
219555e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
21961da177e4SLinus Torvalds 	int change;
21971da177e4SLinus Torvalds 	int val;
21981da177e4SLinus Torvalds 
21991da177e4SLinus Torvalds 	if (!snd_hdsp_use_is_exclusive(hdsp))
22001da177e4SLinus Torvalds 		return -EBUSY;
22011da177e4SLinus Torvalds 	val = ucontrol->value.enumerated.item[0];
22021da177e4SLinus Torvalds 	if (val < 0) val = 0;
22031da177e4SLinus Torvalds 	if (val > 2) val = 2;
22041da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
2205b0b98119STakashi Iwai 	if (val != hdsp_da_gain(hdsp))
22061da177e4SLinus Torvalds 		change = (hdsp_set_da_gain(hdsp, val) == 0) ? 1 : 0;
2207b0b98119STakashi Iwai 	else
22081da177e4SLinus Torvalds 		change = 0;
22091da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
22101da177e4SLinus Torvalds 	return change;
22111da177e4SLinus Torvalds }
22121da177e4SLinus Torvalds 
22131da177e4SLinus Torvalds #define HDSP_AD_GAIN(xname, xindex) \
221467ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
22151da177e4SLinus Torvalds   .name = xname, \
22161da177e4SLinus Torvalds   .index = xindex, \
22171da177e4SLinus Torvalds   .info = snd_hdsp_info_ad_gain, \
22181da177e4SLinus Torvalds   .get = snd_hdsp_get_ad_gain, \
22191da177e4SLinus Torvalds   .put = snd_hdsp_put_ad_gain \
22201da177e4SLinus Torvalds }
22211da177e4SLinus Torvalds 
222255e957d8STakashi Iwai static int hdsp_ad_gain(struct hdsp *hdsp)
22231da177e4SLinus Torvalds {
22241da177e4SLinus Torvalds 	switch (hdsp->control_register & HDSP_ADGainMask) {
22251da177e4SLinus Torvalds 	case HDSP_ADGainMinus10dBV:
22261da177e4SLinus Torvalds 		return 0;
22271da177e4SLinus Torvalds 	case HDSP_ADGainPlus4dBu:
22281da177e4SLinus Torvalds 		return 1;
22291da177e4SLinus Torvalds 	case HDSP_ADGainLowGain:
22301da177e4SLinus Torvalds 		return 2;
22311da177e4SLinus Torvalds 	default:
22321da177e4SLinus Torvalds 		return 1;
22331da177e4SLinus Torvalds 	}
22341da177e4SLinus Torvalds }
22351da177e4SLinus Torvalds 
223655e957d8STakashi Iwai static int hdsp_set_ad_gain(struct hdsp *hdsp, int mode)
22371da177e4SLinus Torvalds {
22381da177e4SLinus Torvalds 	hdsp->control_register &= ~HDSP_ADGainMask;
22391da177e4SLinus Torvalds 	switch (mode) {
22401da177e4SLinus Torvalds 	case 0:
22411da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_ADGainMinus10dBV;
22421da177e4SLinus Torvalds 		break;
22431da177e4SLinus Torvalds 	case 1:
22441da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_ADGainPlus4dBu;
22451da177e4SLinus Torvalds 		break;
22461da177e4SLinus Torvalds 	case 2:
22471da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_ADGainLowGain;
22481da177e4SLinus Torvalds 		break;
22491da177e4SLinus Torvalds 	default:
22501da177e4SLinus Torvalds 		return -1;
22511da177e4SLinus Torvalds 
22521da177e4SLinus Torvalds 	}
22531da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
22541da177e4SLinus Torvalds 	return 0;
22551da177e4SLinus Torvalds }
22561da177e4SLinus Torvalds 
225755e957d8STakashi Iwai static int snd_hdsp_info_ad_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
22581da177e4SLinus Torvalds {
22591da177e4SLinus Torvalds 	static char *texts[] = {"-10 dBV", "+4 dBu", "Lo Gain"};
22601da177e4SLinus Torvalds 
22611da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
22621da177e4SLinus Torvalds 	uinfo->count = 1;
22631da177e4SLinus Torvalds 	uinfo->value.enumerated.items = 3;
22641da177e4SLinus Torvalds 	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
22651da177e4SLinus Torvalds 		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
22661da177e4SLinus Torvalds 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
22671da177e4SLinus Torvalds 	return 0;
22681da177e4SLinus Torvalds }
22691da177e4SLinus Torvalds 
227055e957d8STakashi Iwai static int snd_hdsp_get_ad_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
22711da177e4SLinus Torvalds {
227255e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
22731da177e4SLinus Torvalds 
22741da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp_ad_gain(hdsp);
22751da177e4SLinus Torvalds 	return 0;
22761da177e4SLinus Torvalds }
22771da177e4SLinus Torvalds 
227855e957d8STakashi Iwai static int snd_hdsp_put_ad_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
22791da177e4SLinus Torvalds {
228055e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
22811da177e4SLinus Torvalds 	int change;
22821da177e4SLinus Torvalds 	int val;
22831da177e4SLinus Torvalds 
22841da177e4SLinus Torvalds 	if (!snd_hdsp_use_is_exclusive(hdsp))
22851da177e4SLinus Torvalds 		return -EBUSY;
22861da177e4SLinus Torvalds 	val = ucontrol->value.enumerated.item[0];
22871da177e4SLinus Torvalds 	if (val < 0) val = 0;
22881da177e4SLinus Torvalds 	if (val > 2) val = 2;
22891da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
2290b0b98119STakashi Iwai 	if (val != hdsp_ad_gain(hdsp))
22911da177e4SLinus Torvalds 		change = (hdsp_set_ad_gain(hdsp, val) == 0) ? 1 : 0;
2292b0b98119STakashi Iwai 	else
22931da177e4SLinus Torvalds 		change = 0;
22941da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
22951da177e4SLinus Torvalds 	return change;
22961da177e4SLinus Torvalds }
22971da177e4SLinus Torvalds 
22981da177e4SLinus Torvalds #define HDSP_PHONE_GAIN(xname, xindex) \
229967ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
23001da177e4SLinus Torvalds   .name = xname, \
23011da177e4SLinus Torvalds   .index = xindex, \
23021da177e4SLinus Torvalds   .info = snd_hdsp_info_phone_gain, \
23031da177e4SLinus Torvalds   .get = snd_hdsp_get_phone_gain, \
23041da177e4SLinus Torvalds   .put = snd_hdsp_put_phone_gain \
23051da177e4SLinus Torvalds }
23061da177e4SLinus Torvalds 
230755e957d8STakashi Iwai static int hdsp_phone_gain(struct hdsp *hdsp)
23081da177e4SLinus Torvalds {
23091da177e4SLinus Torvalds 	switch (hdsp->control_register & HDSP_PhoneGainMask) {
23101da177e4SLinus Torvalds 	case HDSP_PhoneGain0dB:
23111da177e4SLinus Torvalds 		return 0;
23121da177e4SLinus Torvalds 	case HDSP_PhoneGainMinus6dB:
23131da177e4SLinus Torvalds 		return 1;
23141da177e4SLinus Torvalds 	case HDSP_PhoneGainMinus12dB:
23151da177e4SLinus Torvalds 		return 2;
23161da177e4SLinus Torvalds 	default:
23171da177e4SLinus Torvalds 		return 0;
23181da177e4SLinus Torvalds 	}
23191da177e4SLinus Torvalds }
23201da177e4SLinus Torvalds 
232155e957d8STakashi Iwai static int hdsp_set_phone_gain(struct hdsp *hdsp, int mode)
23221da177e4SLinus Torvalds {
23231da177e4SLinus Torvalds 	hdsp->control_register &= ~HDSP_PhoneGainMask;
23241da177e4SLinus Torvalds 	switch (mode) {
23251da177e4SLinus Torvalds 	case 0:
23261da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_PhoneGain0dB;
23271da177e4SLinus Torvalds 		break;
23281da177e4SLinus Torvalds 	case 1:
23291da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_PhoneGainMinus6dB;
23301da177e4SLinus Torvalds 		break;
23311da177e4SLinus Torvalds 	case 2:
23321da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_PhoneGainMinus12dB;
23331da177e4SLinus Torvalds 		break;
23341da177e4SLinus Torvalds 	default:
23351da177e4SLinus Torvalds 		return -1;
23361da177e4SLinus Torvalds 
23371da177e4SLinus Torvalds 	}
23381da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
23391da177e4SLinus Torvalds 	return 0;
23401da177e4SLinus Torvalds }
23411da177e4SLinus Torvalds 
234255e957d8STakashi Iwai static int snd_hdsp_info_phone_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
23431da177e4SLinus Torvalds {
23441da177e4SLinus Torvalds 	static char *texts[] = {"0 dB", "-6 dB", "-12 dB"};
23451da177e4SLinus Torvalds 
23461da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
23471da177e4SLinus Torvalds 	uinfo->count = 1;
23481da177e4SLinus Torvalds 	uinfo->value.enumerated.items = 3;
23491da177e4SLinus Torvalds 	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
23501da177e4SLinus Torvalds 		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
23511da177e4SLinus Torvalds 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
23521da177e4SLinus Torvalds 	return 0;
23531da177e4SLinus Torvalds }
23541da177e4SLinus Torvalds 
235555e957d8STakashi Iwai static int snd_hdsp_get_phone_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
23561da177e4SLinus Torvalds {
235755e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
23581da177e4SLinus Torvalds 
23591da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp_phone_gain(hdsp);
23601da177e4SLinus Torvalds 	return 0;
23611da177e4SLinus Torvalds }
23621da177e4SLinus Torvalds 
236355e957d8STakashi Iwai static int snd_hdsp_put_phone_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
23641da177e4SLinus Torvalds {
236555e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
23661da177e4SLinus Torvalds 	int change;
23671da177e4SLinus Torvalds 	int val;
23681da177e4SLinus Torvalds 
23691da177e4SLinus Torvalds 	if (!snd_hdsp_use_is_exclusive(hdsp))
23701da177e4SLinus Torvalds 		return -EBUSY;
23711da177e4SLinus Torvalds 	val = ucontrol->value.enumerated.item[0];
23721da177e4SLinus Torvalds 	if (val < 0) val = 0;
23731da177e4SLinus Torvalds 	if (val > 2) val = 2;
23741da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
2375b0b98119STakashi Iwai 	if (val != hdsp_phone_gain(hdsp))
23761da177e4SLinus Torvalds 		change = (hdsp_set_phone_gain(hdsp, val) == 0) ? 1 : 0;
2377b0b98119STakashi Iwai 	else
23781da177e4SLinus Torvalds 		change = 0;
23791da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
23801da177e4SLinus Torvalds 	return change;
23811da177e4SLinus Torvalds }
23821da177e4SLinus Torvalds 
23831da177e4SLinus Torvalds #define HDSP_XLR_BREAKOUT_CABLE(xname, xindex) \
238467ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
23851da177e4SLinus Torvalds   .name = xname, \
23861da177e4SLinus Torvalds   .index = xindex, \
23871da177e4SLinus Torvalds   .info = snd_hdsp_info_xlr_breakout_cable, \
23881da177e4SLinus Torvalds   .get = snd_hdsp_get_xlr_breakout_cable, \
23891da177e4SLinus Torvalds   .put = snd_hdsp_put_xlr_breakout_cable \
23901da177e4SLinus Torvalds }
23911da177e4SLinus Torvalds 
239255e957d8STakashi Iwai static int hdsp_xlr_breakout_cable(struct hdsp *hdsp)
23931da177e4SLinus Torvalds {
2394b0b98119STakashi Iwai 	if (hdsp->control_register & HDSP_XLRBreakoutCable)
23951da177e4SLinus Torvalds 		return 1;
23961da177e4SLinus Torvalds 	return 0;
23971da177e4SLinus Torvalds }
23981da177e4SLinus Torvalds 
239955e957d8STakashi Iwai static int hdsp_set_xlr_breakout_cable(struct hdsp *hdsp, int mode)
24001da177e4SLinus Torvalds {
2401b0b98119STakashi Iwai 	if (mode)
24021da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_XLRBreakoutCable;
2403b0b98119STakashi Iwai 	else
24041da177e4SLinus Torvalds 		hdsp->control_register &= ~HDSP_XLRBreakoutCable;
24051da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
24061da177e4SLinus Torvalds 	return 0;
24071da177e4SLinus Torvalds }
24081da177e4SLinus Torvalds 
2409a5ce8890STakashi Iwai #define snd_hdsp_info_xlr_breakout_cable	snd_ctl_boolean_mono_info
24101da177e4SLinus Torvalds 
241155e957d8STakashi Iwai static int snd_hdsp_get_xlr_breakout_cable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
24121da177e4SLinus Torvalds {
241355e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
24141da177e4SLinus Torvalds 
24151da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp_xlr_breakout_cable(hdsp);
24161da177e4SLinus Torvalds 	return 0;
24171da177e4SLinus Torvalds }
24181da177e4SLinus Torvalds 
241955e957d8STakashi Iwai static int snd_hdsp_put_xlr_breakout_cable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
24201da177e4SLinus Torvalds {
242155e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
24221da177e4SLinus Torvalds 	int change;
24231da177e4SLinus Torvalds 	int val;
24241da177e4SLinus Torvalds 
24251da177e4SLinus Torvalds 	if (!snd_hdsp_use_is_exclusive(hdsp))
24261da177e4SLinus Torvalds 		return -EBUSY;
24271da177e4SLinus Torvalds 	val = ucontrol->value.integer.value[0] & 1;
24281da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
24291da177e4SLinus Torvalds 	change = (int)val != hdsp_xlr_breakout_cable(hdsp);
24301da177e4SLinus Torvalds 	hdsp_set_xlr_breakout_cable(hdsp, val);
24311da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
24321da177e4SLinus Torvalds 	return change;
24331da177e4SLinus Torvalds }
24341da177e4SLinus Torvalds 
24351da177e4SLinus Torvalds /* (De)activates old RME Analog Extension Board
24361da177e4SLinus Torvalds    These are connected to the internal ADAT connector
24371da177e4SLinus Torvalds    Switching this on desactivates external ADAT
24381da177e4SLinus Torvalds */
24391da177e4SLinus Torvalds #define HDSP_AEB(xname, xindex) \
244067ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
24411da177e4SLinus Torvalds   .name = xname, \
24421da177e4SLinus Torvalds   .index = xindex, \
24431da177e4SLinus Torvalds   .info = snd_hdsp_info_aeb, \
24441da177e4SLinus Torvalds   .get = snd_hdsp_get_aeb, \
24451da177e4SLinus Torvalds   .put = snd_hdsp_put_aeb \
24461da177e4SLinus Torvalds }
24471da177e4SLinus Torvalds 
244855e957d8STakashi Iwai static int hdsp_aeb(struct hdsp *hdsp)
24491da177e4SLinus Torvalds {
2450b0b98119STakashi Iwai 	if (hdsp->control_register & HDSP_AnalogExtensionBoard)
24511da177e4SLinus Torvalds 		return 1;
24521da177e4SLinus Torvalds 	return 0;
24531da177e4SLinus Torvalds }
24541da177e4SLinus Torvalds 
245555e957d8STakashi Iwai static int hdsp_set_aeb(struct hdsp *hdsp, int mode)
24561da177e4SLinus Torvalds {
2457b0b98119STakashi Iwai 	if (mode)
24581da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_AnalogExtensionBoard;
2459b0b98119STakashi Iwai 	else
24601da177e4SLinus Torvalds 		hdsp->control_register &= ~HDSP_AnalogExtensionBoard;
24611da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
24621da177e4SLinus Torvalds 	return 0;
24631da177e4SLinus Torvalds }
24641da177e4SLinus Torvalds 
2465a5ce8890STakashi Iwai #define snd_hdsp_info_aeb		snd_ctl_boolean_mono_info
24661da177e4SLinus Torvalds 
246755e957d8STakashi Iwai static int snd_hdsp_get_aeb(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
24681da177e4SLinus Torvalds {
246955e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
24701da177e4SLinus Torvalds 
24711da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp_aeb(hdsp);
24721da177e4SLinus Torvalds 	return 0;
24731da177e4SLinus Torvalds }
24741da177e4SLinus Torvalds 
247555e957d8STakashi Iwai static int snd_hdsp_put_aeb(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
24761da177e4SLinus Torvalds {
247755e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
24781da177e4SLinus Torvalds 	int change;
24791da177e4SLinus Torvalds 	int val;
24801da177e4SLinus Torvalds 
24811da177e4SLinus Torvalds 	if (!snd_hdsp_use_is_exclusive(hdsp))
24821da177e4SLinus Torvalds 		return -EBUSY;
24831da177e4SLinus Torvalds 	val = ucontrol->value.integer.value[0] & 1;
24841da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
24851da177e4SLinus Torvalds 	change = (int)val != hdsp_aeb(hdsp);
24861da177e4SLinus Torvalds 	hdsp_set_aeb(hdsp, val);
24871da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
24881da177e4SLinus Torvalds 	return change;
24891da177e4SLinus Torvalds }
24901da177e4SLinus Torvalds 
24911da177e4SLinus Torvalds #define HDSP_PREF_SYNC_REF(xname, xindex) \
249267ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
24931da177e4SLinus Torvalds   .name = xname, \
24941da177e4SLinus Torvalds   .index = xindex, \
24951da177e4SLinus Torvalds   .info = snd_hdsp_info_pref_sync_ref, \
24961da177e4SLinus Torvalds   .get = snd_hdsp_get_pref_sync_ref, \
24971da177e4SLinus Torvalds   .put = snd_hdsp_put_pref_sync_ref \
24981da177e4SLinus Torvalds }
24991da177e4SLinus Torvalds 
250055e957d8STakashi Iwai static int hdsp_pref_sync_ref(struct hdsp *hdsp)
25011da177e4SLinus Torvalds {
25021da177e4SLinus Torvalds 	/* Notice that this looks at the requested sync source,
25031da177e4SLinus Torvalds 	   not the one actually in use.
25041da177e4SLinus Torvalds 	*/
25051da177e4SLinus Torvalds 
25061da177e4SLinus Torvalds 	switch (hdsp->control_register & HDSP_SyncRefMask) {
25071da177e4SLinus Torvalds 	case HDSP_SyncRef_ADAT1:
25081da177e4SLinus Torvalds 		return HDSP_SYNC_FROM_ADAT1;
25091da177e4SLinus Torvalds 	case HDSP_SyncRef_ADAT2:
25101da177e4SLinus Torvalds 		return HDSP_SYNC_FROM_ADAT2;
25111da177e4SLinus Torvalds 	case HDSP_SyncRef_ADAT3:
25121da177e4SLinus Torvalds 		return HDSP_SYNC_FROM_ADAT3;
25131da177e4SLinus Torvalds 	case HDSP_SyncRef_SPDIF:
25141da177e4SLinus Torvalds 		return HDSP_SYNC_FROM_SPDIF;
25151da177e4SLinus Torvalds 	case HDSP_SyncRef_WORD:
25161da177e4SLinus Torvalds 		return HDSP_SYNC_FROM_WORD;
25171da177e4SLinus Torvalds 	case HDSP_SyncRef_ADAT_SYNC:
25181da177e4SLinus Torvalds 		return HDSP_SYNC_FROM_ADAT_SYNC;
25191da177e4SLinus Torvalds 	default:
25201da177e4SLinus Torvalds 		return HDSP_SYNC_FROM_WORD;
25211da177e4SLinus Torvalds 	}
25221da177e4SLinus Torvalds 	return 0;
25231da177e4SLinus Torvalds }
25241da177e4SLinus Torvalds 
252555e957d8STakashi Iwai static int hdsp_set_pref_sync_ref(struct hdsp *hdsp, int pref)
25261da177e4SLinus Torvalds {
25271da177e4SLinus Torvalds 	hdsp->control_register &= ~HDSP_SyncRefMask;
25281da177e4SLinus Torvalds 	switch (pref) {
25291da177e4SLinus Torvalds 	case HDSP_SYNC_FROM_ADAT1:
25301da177e4SLinus Torvalds 		hdsp->control_register &= ~HDSP_SyncRefMask; /* clear SyncRef bits */
25311da177e4SLinus Torvalds 		break;
25321da177e4SLinus Torvalds 	case HDSP_SYNC_FROM_ADAT2:
25331da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_SyncRef_ADAT2;
25341da177e4SLinus Torvalds 		break;
25351da177e4SLinus Torvalds 	case HDSP_SYNC_FROM_ADAT3:
25361da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_SyncRef_ADAT3;
25371da177e4SLinus Torvalds 		break;
25381da177e4SLinus Torvalds 	case HDSP_SYNC_FROM_SPDIF:
25391da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_SyncRef_SPDIF;
25401da177e4SLinus Torvalds 		break;
25411da177e4SLinus Torvalds 	case HDSP_SYNC_FROM_WORD:
25421da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_SyncRef_WORD;
25431da177e4SLinus Torvalds 		break;
25441da177e4SLinus Torvalds 	case HDSP_SYNC_FROM_ADAT_SYNC:
25451da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_SyncRef_ADAT_SYNC;
25461da177e4SLinus Torvalds 		break;
25471da177e4SLinus Torvalds 	default:
25481da177e4SLinus Torvalds 		return -1;
25491da177e4SLinus Torvalds 	}
25501da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
25511da177e4SLinus Torvalds 	return 0;
25521da177e4SLinus Torvalds }
25531da177e4SLinus Torvalds 
255455e957d8STakashi Iwai static int snd_hdsp_info_pref_sync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
25551da177e4SLinus Torvalds {
25561da177e4SLinus Torvalds 	static char *texts[] = {"Word", "IEC958", "ADAT1", "ADAT Sync", "ADAT2", "ADAT3" };
255755e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
25581da177e4SLinus Torvalds 
25591da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
25601da177e4SLinus Torvalds 	uinfo->count = 1;
25611da177e4SLinus Torvalds 
25621da177e4SLinus Torvalds 	switch (hdsp->io_type) {
25631da177e4SLinus Torvalds 	case Digiface:
25641da177e4SLinus Torvalds 	case H9652:
25651da177e4SLinus Torvalds 		uinfo->value.enumerated.items = 6;
25661da177e4SLinus Torvalds 		break;
25671da177e4SLinus Torvalds 	case Multiface:
25681da177e4SLinus Torvalds 		uinfo->value.enumerated.items = 4;
25691da177e4SLinus Torvalds 		break;
25701da177e4SLinus Torvalds 	case H9632:
25711da177e4SLinus Torvalds 		uinfo->value.enumerated.items = 3;
25721da177e4SLinus Torvalds 		break;
25731da177e4SLinus Torvalds 	default:
25741da177e4SLinus Torvalds 		uinfo->value.enumerated.items = 0;
25751da177e4SLinus Torvalds 		break;
25761da177e4SLinus Torvalds 	}
25771da177e4SLinus Torvalds 
25781da177e4SLinus Torvalds 	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
25791da177e4SLinus Torvalds 		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
25801da177e4SLinus Torvalds 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
25811da177e4SLinus Torvalds 	return 0;
25821da177e4SLinus Torvalds }
25831da177e4SLinus Torvalds 
258455e957d8STakashi Iwai static int snd_hdsp_get_pref_sync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
25851da177e4SLinus Torvalds {
258655e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
25871da177e4SLinus Torvalds 
25881da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp_pref_sync_ref(hdsp);
25891da177e4SLinus Torvalds 	return 0;
25901da177e4SLinus Torvalds }
25911da177e4SLinus Torvalds 
259255e957d8STakashi Iwai static int snd_hdsp_put_pref_sync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
25931da177e4SLinus Torvalds {
259455e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
25951da177e4SLinus Torvalds 	int change, max;
25961da177e4SLinus Torvalds 	unsigned int val;
25971da177e4SLinus Torvalds 
25981da177e4SLinus Torvalds 	if (!snd_hdsp_use_is_exclusive(hdsp))
25991da177e4SLinus Torvalds 		return -EBUSY;
26001da177e4SLinus Torvalds 
26011da177e4SLinus Torvalds 	switch (hdsp->io_type) {
26021da177e4SLinus Torvalds 	case Digiface:
26031da177e4SLinus Torvalds 	case H9652:
26041da177e4SLinus Torvalds 		max = 6;
26051da177e4SLinus Torvalds 		break;
26061da177e4SLinus Torvalds 	case Multiface:
26071da177e4SLinus Torvalds 		max = 4;
26081da177e4SLinus Torvalds 		break;
26091da177e4SLinus Torvalds 	case H9632:
26101da177e4SLinus Torvalds 		max = 3;
26111da177e4SLinus Torvalds 		break;
26121da177e4SLinus Torvalds 	default:
26131da177e4SLinus Torvalds 		return -EIO;
26141da177e4SLinus Torvalds 	}
26151da177e4SLinus Torvalds 
26161da177e4SLinus Torvalds 	val = ucontrol->value.enumerated.item[0] % max;
26171da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
26181da177e4SLinus Torvalds 	change = (int)val != hdsp_pref_sync_ref(hdsp);
26191da177e4SLinus Torvalds 	hdsp_set_pref_sync_ref(hdsp, val);
26201da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
26211da177e4SLinus Torvalds 	return change;
26221da177e4SLinus Torvalds }
26231da177e4SLinus Torvalds 
26241da177e4SLinus Torvalds #define HDSP_AUTOSYNC_REF(xname, xindex) \
262567ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
26261da177e4SLinus Torvalds   .name = xname, \
26271da177e4SLinus Torvalds   .index = xindex, \
26281da177e4SLinus Torvalds   .access = SNDRV_CTL_ELEM_ACCESS_READ, \
26291da177e4SLinus Torvalds   .info = snd_hdsp_info_autosync_ref, \
26301da177e4SLinus Torvalds   .get = snd_hdsp_get_autosync_ref, \
26311da177e4SLinus Torvalds }
26321da177e4SLinus Torvalds 
263355e957d8STakashi Iwai static int hdsp_autosync_ref(struct hdsp *hdsp)
26341da177e4SLinus Torvalds {
26351da177e4SLinus Torvalds 	/* This looks at the autosync selected sync reference */
26361da177e4SLinus Torvalds 	unsigned int status2 = hdsp_read(hdsp, HDSP_status2Register);
26371da177e4SLinus Torvalds 
26381da177e4SLinus Torvalds 	switch (status2 & HDSP_SelSyncRefMask) {
26391da177e4SLinus Torvalds 	case HDSP_SelSyncRef_WORD:
26401da177e4SLinus Torvalds 		return HDSP_AUTOSYNC_FROM_WORD;
26411da177e4SLinus Torvalds 	case HDSP_SelSyncRef_ADAT_SYNC:
26421da177e4SLinus Torvalds 		return HDSP_AUTOSYNC_FROM_ADAT_SYNC;
26431da177e4SLinus Torvalds 	case HDSP_SelSyncRef_SPDIF:
26441da177e4SLinus Torvalds 		return HDSP_AUTOSYNC_FROM_SPDIF;
26451da177e4SLinus Torvalds 	case HDSP_SelSyncRefMask:
26461da177e4SLinus Torvalds 		return HDSP_AUTOSYNC_FROM_NONE;
26471da177e4SLinus Torvalds 	case HDSP_SelSyncRef_ADAT1:
26481da177e4SLinus Torvalds 		return HDSP_AUTOSYNC_FROM_ADAT1;
26491da177e4SLinus Torvalds 	case HDSP_SelSyncRef_ADAT2:
26501da177e4SLinus Torvalds 		return HDSP_AUTOSYNC_FROM_ADAT2;
26511da177e4SLinus Torvalds 	case HDSP_SelSyncRef_ADAT3:
26521da177e4SLinus Torvalds 		return HDSP_AUTOSYNC_FROM_ADAT3;
26531da177e4SLinus Torvalds 	default:
26541da177e4SLinus Torvalds 		return HDSP_AUTOSYNC_FROM_WORD;
26551da177e4SLinus Torvalds 	}
26561da177e4SLinus Torvalds 	return 0;
26571da177e4SLinus Torvalds }
26581da177e4SLinus Torvalds 
265955e957d8STakashi Iwai static int snd_hdsp_info_autosync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
26601da177e4SLinus Torvalds {
26611da177e4SLinus Torvalds 	static char *texts[] = {"Word", "ADAT Sync", "IEC958", "None", "ADAT1", "ADAT2", "ADAT3" };
26621da177e4SLinus Torvalds 
26631da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
26641da177e4SLinus Torvalds 	uinfo->count = 1;
26651da177e4SLinus Torvalds 	uinfo->value.enumerated.items = 7;
26661da177e4SLinus Torvalds 	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
26671da177e4SLinus Torvalds 		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
26681da177e4SLinus Torvalds 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
26691da177e4SLinus Torvalds 	return 0;
26701da177e4SLinus Torvalds }
26711da177e4SLinus Torvalds 
267255e957d8STakashi Iwai static int snd_hdsp_get_autosync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
26731da177e4SLinus Torvalds {
267455e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
26751da177e4SLinus Torvalds 
26761da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp_autosync_ref(hdsp);
26771da177e4SLinus Torvalds 	return 0;
26781da177e4SLinus Torvalds }
26791da177e4SLinus Torvalds 
26801da177e4SLinus Torvalds #define HDSP_LINE_OUT(xname, xindex) \
268167ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
26821da177e4SLinus Torvalds   .name = xname, \
26831da177e4SLinus Torvalds   .index = xindex, \
26841da177e4SLinus Torvalds   .info = snd_hdsp_info_line_out, \
26851da177e4SLinus Torvalds   .get = snd_hdsp_get_line_out, \
26861da177e4SLinus Torvalds   .put = snd_hdsp_put_line_out \
26871da177e4SLinus Torvalds }
26881da177e4SLinus Torvalds 
268955e957d8STakashi Iwai static int hdsp_line_out(struct hdsp *hdsp)
26901da177e4SLinus Torvalds {
26911da177e4SLinus Torvalds 	return (hdsp->control_register & HDSP_LineOut) ? 1 : 0;
26921da177e4SLinus Torvalds }
26931da177e4SLinus Torvalds 
269455e957d8STakashi Iwai static int hdsp_set_line_output(struct hdsp *hdsp, int out)
26951da177e4SLinus Torvalds {
2696b0b98119STakashi Iwai 	if (out)
26971da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_LineOut;
2698b0b98119STakashi Iwai 	else
26991da177e4SLinus Torvalds 		hdsp->control_register &= ~HDSP_LineOut;
27001da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
27011da177e4SLinus Torvalds 	return 0;
27021da177e4SLinus Torvalds }
27031da177e4SLinus Torvalds 
2704a5ce8890STakashi Iwai #define snd_hdsp_info_line_out		snd_ctl_boolean_mono_info
27051da177e4SLinus Torvalds 
270655e957d8STakashi Iwai static int snd_hdsp_get_line_out(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
27071da177e4SLinus Torvalds {
270855e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
27091da177e4SLinus Torvalds 
27101da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
27111da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = hdsp_line_out(hdsp);
27121da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
27131da177e4SLinus Torvalds 	return 0;
27141da177e4SLinus Torvalds }
27151da177e4SLinus Torvalds 
271655e957d8STakashi Iwai static int snd_hdsp_put_line_out(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
27171da177e4SLinus Torvalds {
271855e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
27191da177e4SLinus Torvalds 	int change;
27201da177e4SLinus Torvalds 	unsigned int val;
27211da177e4SLinus Torvalds 
27221da177e4SLinus Torvalds 	if (!snd_hdsp_use_is_exclusive(hdsp))
27231da177e4SLinus Torvalds 		return -EBUSY;
27241da177e4SLinus Torvalds 	val = ucontrol->value.integer.value[0] & 1;
27251da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
27261da177e4SLinus Torvalds 	change = (int)val != hdsp_line_out(hdsp);
27271da177e4SLinus Torvalds 	hdsp_set_line_output(hdsp, val);
27281da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
27291da177e4SLinus Torvalds 	return change;
27301da177e4SLinus Torvalds }
27311da177e4SLinus Torvalds 
27321da177e4SLinus Torvalds #define HDSP_PRECISE_POINTER(xname, xindex) \
273367ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_CARD, \
27341da177e4SLinus Torvalds   .name = xname, \
27351da177e4SLinus Torvalds   .index = xindex, \
27361da177e4SLinus Torvalds   .info = snd_hdsp_info_precise_pointer, \
27371da177e4SLinus Torvalds   .get = snd_hdsp_get_precise_pointer, \
27381da177e4SLinus Torvalds   .put = snd_hdsp_put_precise_pointer \
27391da177e4SLinus Torvalds }
27401da177e4SLinus Torvalds 
274155e957d8STakashi Iwai static int hdsp_set_precise_pointer(struct hdsp *hdsp, int precise)
27421da177e4SLinus Torvalds {
2743b0b98119STakashi Iwai 	if (precise)
27441da177e4SLinus Torvalds 		hdsp->precise_ptr = 1;
2745b0b98119STakashi Iwai 	else
27461da177e4SLinus Torvalds 		hdsp->precise_ptr = 0;
27471da177e4SLinus Torvalds 	return 0;
27481da177e4SLinus Torvalds }
27491da177e4SLinus Torvalds 
2750a5ce8890STakashi Iwai #define snd_hdsp_info_precise_pointer		snd_ctl_boolean_mono_info
27511da177e4SLinus Torvalds 
275255e957d8STakashi Iwai static int snd_hdsp_get_precise_pointer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
27531da177e4SLinus Torvalds {
275455e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
27551da177e4SLinus Torvalds 
27561da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
27571da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = hdsp->precise_ptr;
27581da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
27591da177e4SLinus Torvalds 	return 0;
27601da177e4SLinus Torvalds }
27611da177e4SLinus Torvalds 
276255e957d8STakashi Iwai static int snd_hdsp_put_precise_pointer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
27631da177e4SLinus Torvalds {
276455e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
27651da177e4SLinus Torvalds 	int change;
27661da177e4SLinus Torvalds 	unsigned int val;
27671da177e4SLinus Torvalds 
27681da177e4SLinus Torvalds 	if (!snd_hdsp_use_is_exclusive(hdsp))
27691da177e4SLinus Torvalds 		return -EBUSY;
27701da177e4SLinus Torvalds 	val = ucontrol->value.integer.value[0] & 1;
27711da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
27721da177e4SLinus Torvalds 	change = (int)val != hdsp->precise_ptr;
27731da177e4SLinus Torvalds 	hdsp_set_precise_pointer(hdsp, val);
27741da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
27751da177e4SLinus Torvalds 	return change;
27761da177e4SLinus Torvalds }
27771da177e4SLinus Torvalds 
27781da177e4SLinus Torvalds #define HDSP_USE_MIDI_TASKLET(xname, xindex) \
277967ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_CARD, \
27801da177e4SLinus Torvalds   .name = xname, \
27811da177e4SLinus Torvalds   .index = xindex, \
27821da177e4SLinus Torvalds   .info = snd_hdsp_info_use_midi_tasklet, \
27831da177e4SLinus Torvalds   .get = snd_hdsp_get_use_midi_tasklet, \
27841da177e4SLinus Torvalds   .put = snd_hdsp_put_use_midi_tasklet \
27851da177e4SLinus Torvalds }
27861da177e4SLinus Torvalds 
278755e957d8STakashi Iwai static int hdsp_set_use_midi_tasklet(struct hdsp *hdsp, int use_tasklet)
27881da177e4SLinus Torvalds {
2789b0b98119STakashi Iwai 	if (use_tasklet)
27901da177e4SLinus Torvalds 		hdsp->use_midi_tasklet = 1;
2791b0b98119STakashi Iwai 	else
27921da177e4SLinus Torvalds 		hdsp->use_midi_tasklet = 0;
27931da177e4SLinus Torvalds 	return 0;
27941da177e4SLinus Torvalds }
27951da177e4SLinus Torvalds 
2796a5ce8890STakashi Iwai #define snd_hdsp_info_use_midi_tasklet		snd_ctl_boolean_mono_info
27971da177e4SLinus Torvalds 
279855e957d8STakashi Iwai static int snd_hdsp_get_use_midi_tasklet(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
27991da177e4SLinus Torvalds {
280055e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
28011da177e4SLinus Torvalds 
28021da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
28031da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = hdsp->use_midi_tasklet;
28041da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
28051da177e4SLinus Torvalds 	return 0;
28061da177e4SLinus Torvalds }
28071da177e4SLinus Torvalds 
280855e957d8STakashi Iwai static int snd_hdsp_put_use_midi_tasklet(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
28091da177e4SLinus Torvalds {
281055e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
28111da177e4SLinus Torvalds 	int change;
28121da177e4SLinus Torvalds 	unsigned int val;
28131da177e4SLinus Torvalds 
28141da177e4SLinus Torvalds 	if (!snd_hdsp_use_is_exclusive(hdsp))
28151da177e4SLinus Torvalds 		return -EBUSY;
28161da177e4SLinus Torvalds 	val = ucontrol->value.integer.value[0] & 1;
28171da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
28181da177e4SLinus Torvalds 	change = (int)val != hdsp->use_midi_tasklet;
28191da177e4SLinus Torvalds 	hdsp_set_use_midi_tasklet(hdsp, val);
28201da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
28211da177e4SLinus Torvalds 	return change;
28221da177e4SLinus Torvalds }
28231da177e4SLinus Torvalds 
28241da177e4SLinus Torvalds #define HDSP_MIXER(xname, xindex) \
28251da177e4SLinus Torvalds { .iface = SNDRV_CTL_ELEM_IFACE_HWDEP, \
28261da177e4SLinus Torvalds   .name = xname, \
28271da177e4SLinus Torvalds   .index = xindex, \
282867ed4161SClemens Ladisch   .device = 0, \
28291da177e4SLinus Torvalds   .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
28301da177e4SLinus Torvalds 		 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
28311da177e4SLinus Torvalds   .info = snd_hdsp_info_mixer, \
28321da177e4SLinus Torvalds   .get = snd_hdsp_get_mixer, \
28331da177e4SLinus Torvalds   .put = snd_hdsp_put_mixer \
28341da177e4SLinus Torvalds }
28351da177e4SLinus Torvalds 
283655e957d8STakashi Iwai static int snd_hdsp_info_mixer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
28371da177e4SLinus Torvalds {
28381da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
28391da177e4SLinus Torvalds 	uinfo->count = 3;
28401da177e4SLinus Torvalds 	uinfo->value.integer.min = 0;
28411da177e4SLinus Torvalds 	uinfo->value.integer.max = 65536;
28421da177e4SLinus Torvalds 	uinfo->value.integer.step = 1;
28431da177e4SLinus Torvalds 	return 0;
28441da177e4SLinus Torvalds }
28451da177e4SLinus Torvalds 
284655e957d8STakashi Iwai static int snd_hdsp_get_mixer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
28471da177e4SLinus Torvalds {
284855e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
28491da177e4SLinus Torvalds 	int source;
28501da177e4SLinus Torvalds 	int destination;
28511da177e4SLinus Torvalds 	int addr;
28521da177e4SLinus Torvalds 
28531da177e4SLinus Torvalds 	source = ucontrol->value.integer.value[0];
28541da177e4SLinus Torvalds 	destination = ucontrol->value.integer.value[1];
28551da177e4SLinus Torvalds 
2856b0b98119STakashi Iwai 	if (source >= hdsp->max_channels)
28571da177e4SLinus Torvalds 		addr = hdsp_playback_to_output_key(hdsp,source-hdsp->max_channels,destination);
2858b0b98119STakashi Iwai 	else
28591da177e4SLinus Torvalds 		addr = hdsp_input_to_output_key(hdsp,source, destination);
28601da177e4SLinus Torvalds 
28611da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
28621da177e4SLinus Torvalds 	ucontrol->value.integer.value[2] = hdsp_read_gain (hdsp, addr);
28631da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
28641da177e4SLinus Torvalds 	return 0;
28651da177e4SLinus Torvalds }
28661da177e4SLinus Torvalds 
286755e957d8STakashi Iwai static int snd_hdsp_put_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 change;
28711da177e4SLinus Torvalds 	int source;
28721da177e4SLinus Torvalds 	int destination;
28731da177e4SLinus Torvalds 	int gain;
28741da177e4SLinus Torvalds 	int addr;
28751da177e4SLinus Torvalds 
28761da177e4SLinus Torvalds 	if (!snd_hdsp_use_is_exclusive(hdsp))
28771da177e4SLinus Torvalds 		return -EBUSY;
28781da177e4SLinus Torvalds 
28791da177e4SLinus Torvalds 	source = ucontrol->value.integer.value[0];
28801da177e4SLinus Torvalds 	destination = ucontrol->value.integer.value[1];
28811da177e4SLinus Torvalds 
2882b0b98119STakashi Iwai 	if (source >= hdsp->max_channels)
28831da177e4SLinus Torvalds 		addr = hdsp_playback_to_output_key(hdsp,source-hdsp->max_channels, destination);
2884b0b98119STakashi Iwai 	else
28851da177e4SLinus Torvalds 		addr = hdsp_input_to_output_key(hdsp,source, destination);
28861da177e4SLinus Torvalds 
28871da177e4SLinus Torvalds 	gain = ucontrol->value.integer.value[2];
28881da177e4SLinus Torvalds 
28891da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
28901da177e4SLinus Torvalds 	change = gain != hdsp_read_gain(hdsp, addr);
28911da177e4SLinus Torvalds 	if (change)
28921da177e4SLinus Torvalds 		hdsp_write_gain(hdsp, addr, gain);
28931da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
28941da177e4SLinus Torvalds 	return change;
28951da177e4SLinus Torvalds }
28961da177e4SLinus Torvalds 
28971da177e4SLinus Torvalds #define HDSP_WC_SYNC_CHECK(xname, xindex) \
289867ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
28991da177e4SLinus Torvalds   .name = xname, \
29001da177e4SLinus Torvalds   .index = xindex, \
29011da177e4SLinus Torvalds   .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
29021da177e4SLinus Torvalds   .info = snd_hdsp_info_sync_check, \
29031da177e4SLinus Torvalds   .get = snd_hdsp_get_wc_sync_check \
29041da177e4SLinus Torvalds }
29051da177e4SLinus Torvalds 
290655e957d8STakashi Iwai static int snd_hdsp_info_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
29071da177e4SLinus Torvalds {
29081da177e4SLinus Torvalds 	static char *texts[] = {"No Lock", "Lock", "Sync" };
29091da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
29101da177e4SLinus Torvalds 	uinfo->count = 1;
29111da177e4SLinus Torvalds 	uinfo->value.enumerated.items = 3;
29121da177e4SLinus Torvalds 	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
29131da177e4SLinus Torvalds 		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
29141da177e4SLinus Torvalds 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
29151da177e4SLinus Torvalds 	return 0;
29161da177e4SLinus Torvalds }
29171da177e4SLinus Torvalds 
291855e957d8STakashi Iwai static int hdsp_wc_sync_check(struct hdsp *hdsp)
29191da177e4SLinus Torvalds {
29201da177e4SLinus Torvalds 	int status2 = hdsp_read(hdsp, HDSP_status2Register);
29211da177e4SLinus Torvalds 	if (status2 & HDSP_wc_lock) {
2922b0b98119STakashi Iwai 		if (status2 & HDSP_wc_sync)
29231da177e4SLinus Torvalds 			return 2;
2924b0b98119STakashi Iwai 		else
29251da177e4SLinus Torvalds 			 return 1;
2926b0b98119STakashi Iwai 	} else
29271da177e4SLinus Torvalds 		return 0;
29281da177e4SLinus Torvalds 	return 0;
29291da177e4SLinus Torvalds }
29301da177e4SLinus Torvalds 
293155e957d8STakashi Iwai static int snd_hdsp_get_wc_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
29321da177e4SLinus Torvalds {
293355e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
29341da177e4SLinus Torvalds 
29351da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp_wc_sync_check(hdsp);
29361da177e4SLinus Torvalds 	return 0;
29371da177e4SLinus Torvalds }
29381da177e4SLinus Torvalds 
29391da177e4SLinus Torvalds #define HDSP_SPDIF_SYNC_CHECK(xname, xindex) \
294067ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
29411da177e4SLinus Torvalds   .name = xname, \
29421da177e4SLinus Torvalds   .index = xindex, \
29431da177e4SLinus Torvalds   .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
29441da177e4SLinus Torvalds   .info = snd_hdsp_info_sync_check, \
29451da177e4SLinus Torvalds   .get = snd_hdsp_get_spdif_sync_check \
29461da177e4SLinus Torvalds }
29471da177e4SLinus Torvalds 
294855e957d8STakashi Iwai static int hdsp_spdif_sync_check(struct hdsp *hdsp)
29491da177e4SLinus Torvalds {
29501da177e4SLinus Torvalds 	int status = hdsp_read(hdsp, HDSP_statusRegister);
2951b0b98119STakashi Iwai 	if (status & HDSP_SPDIFErrorFlag)
29521da177e4SLinus Torvalds 		return 0;
2953b0b98119STakashi Iwai 	else {
2954b0b98119STakashi Iwai 		if (status & HDSP_SPDIFSync)
29551da177e4SLinus Torvalds 			return 2;
2956b0b98119STakashi Iwai 		else
29571da177e4SLinus Torvalds 			return 1;
29581da177e4SLinus Torvalds 	}
29591da177e4SLinus Torvalds 	return 0;
29601da177e4SLinus Torvalds }
29611da177e4SLinus Torvalds 
296255e957d8STakashi Iwai static int snd_hdsp_get_spdif_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
29631da177e4SLinus Torvalds {
296455e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
29651da177e4SLinus Torvalds 
29661da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp_spdif_sync_check(hdsp);
29671da177e4SLinus Torvalds 	return 0;
29681da177e4SLinus Torvalds }
29691da177e4SLinus Torvalds 
29701da177e4SLinus Torvalds #define HDSP_ADATSYNC_SYNC_CHECK(xname, xindex) \
297167ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
29721da177e4SLinus Torvalds   .name = xname, \
29731da177e4SLinus Torvalds   .index = xindex, \
29741da177e4SLinus Torvalds   .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
29751da177e4SLinus Torvalds   .info = snd_hdsp_info_sync_check, \
29761da177e4SLinus Torvalds   .get = snd_hdsp_get_adatsync_sync_check \
29771da177e4SLinus Torvalds }
29781da177e4SLinus Torvalds 
297955e957d8STakashi Iwai static int hdsp_adatsync_sync_check(struct hdsp *hdsp)
29801da177e4SLinus Torvalds {
29811da177e4SLinus Torvalds 	int status = hdsp_read(hdsp, HDSP_statusRegister);
29821da177e4SLinus Torvalds 	if (status & HDSP_TimecodeLock) {
2983b0b98119STakashi Iwai 		if (status & HDSP_TimecodeSync)
29841da177e4SLinus Torvalds 			return 2;
2985b0b98119STakashi Iwai 		else
29861da177e4SLinus Torvalds 			return 1;
2987b0b98119STakashi Iwai 	} else
29881da177e4SLinus Torvalds 		return 0;
29891da177e4SLinus Torvalds }
29901da177e4SLinus Torvalds 
299155e957d8STakashi Iwai static int snd_hdsp_get_adatsync_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
29921da177e4SLinus Torvalds {
299355e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
29941da177e4SLinus Torvalds 
29951da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp_adatsync_sync_check(hdsp);
29961da177e4SLinus Torvalds 	return 0;
29971da177e4SLinus Torvalds }
29981da177e4SLinus Torvalds 
29991da177e4SLinus Torvalds #define HDSP_ADAT_SYNC_CHECK \
300067ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
30011da177e4SLinus Torvalds   .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
30021da177e4SLinus Torvalds   .info = snd_hdsp_info_sync_check, \
30031da177e4SLinus Torvalds   .get = snd_hdsp_get_adat_sync_check \
30041da177e4SLinus Torvalds }
30051da177e4SLinus Torvalds 
300655e957d8STakashi Iwai static int hdsp_adat_sync_check(struct hdsp *hdsp, int idx)
30071da177e4SLinus Torvalds {
30081da177e4SLinus Torvalds 	int status = hdsp_read(hdsp, HDSP_statusRegister);
30091da177e4SLinus Torvalds 
30101da177e4SLinus Torvalds 	if (status & (HDSP_Lock0>>idx)) {
3011b0b98119STakashi Iwai 		if (status & (HDSP_Sync0>>idx))
30121da177e4SLinus Torvalds 			return 2;
3013b0b98119STakashi Iwai 		else
30141da177e4SLinus Torvalds 			return 1;
3015b0b98119STakashi Iwai 	} else
30161da177e4SLinus Torvalds 		return 0;
30171da177e4SLinus Torvalds }
30181da177e4SLinus Torvalds 
301955e957d8STakashi Iwai static int snd_hdsp_get_adat_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
30201da177e4SLinus Torvalds {
30211da177e4SLinus Torvalds 	int offset;
302255e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
30231da177e4SLinus Torvalds 
30241da177e4SLinus Torvalds 	offset = ucontrol->id.index - 1;
30251da177e4SLinus Torvalds 	snd_assert(offset >= 0);
30261da177e4SLinus Torvalds 
30271da177e4SLinus Torvalds 	switch (hdsp->io_type) {
30281da177e4SLinus Torvalds 	case Digiface:
30291da177e4SLinus Torvalds 	case H9652:
30301da177e4SLinus Torvalds 		if (offset >= 3)
30311da177e4SLinus Torvalds 			return -EINVAL;
30321da177e4SLinus Torvalds 		break;
30331da177e4SLinus Torvalds 	case Multiface:
30341da177e4SLinus Torvalds 	case H9632:
30351da177e4SLinus Torvalds 		if (offset >= 1)
30361da177e4SLinus Torvalds 			return -EINVAL;
30371da177e4SLinus Torvalds 		break;
30381da177e4SLinus Torvalds 	default:
30391da177e4SLinus Torvalds 		return -EIO;
30401da177e4SLinus Torvalds 	}
30411da177e4SLinus Torvalds 
30421da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp_adat_sync_check(hdsp, offset);
30431da177e4SLinus Torvalds 	return 0;
30441da177e4SLinus Torvalds }
30451da177e4SLinus Torvalds 
3046e4b6088cSJulian Cable #define HDSP_DDS_OFFSET(xname, xindex) \
3047e4b6088cSJulian Cable { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3048e4b6088cSJulian Cable   .name = xname, \
3049e4b6088cSJulian Cable   .index = xindex, \
3050e4b6088cSJulian Cable   .info = snd_hdsp_info_dds_offset, \
3051e4b6088cSJulian Cable   .get = snd_hdsp_get_dds_offset, \
3052e4b6088cSJulian Cable   .put = snd_hdsp_put_dds_offset \
3053e4b6088cSJulian Cable }
3054e4b6088cSJulian Cable 
3055e4b6088cSJulian Cable static int hdsp_dds_offset(struct hdsp *hdsp)
3056e4b6088cSJulian Cable {
3057e4b6088cSJulian Cable 	u64 n;
3058e4b6088cSJulian Cable 	u32 r;
3059e4b6088cSJulian Cable 	unsigned int dds_value = hdsp->dds_value;
3060e4b6088cSJulian Cable 	int system_sample_rate = hdsp->system_sample_rate;
3061e4b6088cSJulian Cable 
3062e4b6088cSJulian Cable 	n = DDS_NUMERATOR;
3063e4b6088cSJulian Cable 	/*
3064e4b6088cSJulian Cable 	 * dds_value = n / rate
3065e4b6088cSJulian Cable 	 * rate = n / dds_value
3066e4b6088cSJulian Cable 	 */
3067e4b6088cSJulian Cable 	div64_32(&n, dds_value, &r);
3068e4b6088cSJulian Cable 	if (system_sample_rate >= 112000)
3069e4b6088cSJulian Cable 		n *= 4;
3070e4b6088cSJulian Cable 	else if (system_sample_rate >= 56000)
3071e4b6088cSJulian Cable 		n *= 2;
3072e4b6088cSJulian Cable 	return ((int)n) - system_sample_rate;
3073e4b6088cSJulian Cable }
3074e4b6088cSJulian Cable 
3075e4b6088cSJulian Cable static int hdsp_set_dds_offset(struct hdsp *hdsp, int offset_hz)
3076e4b6088cSJulian Cable {
3077e4b6088cSJulian Cable 	int rate = hdsp->system_sample_rate + offset_hz;
3078e4b6088cSJulian Cable 	hdsp_set_dds_value(hdsp, rate);
3079e4b6088cSJulian Cable 	return 0;
3080e4b6088cSJulian Cable }
3081e4b6088cSJulian Cable 
3082e4b6088cSJulian Cable static int snd_hdsp_info_dds_offset(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
3083e4b6088cSJulian Cable {
3084e4b6088cSJulian Cable 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3085e4b6088cSJulian Cable 	uinfo->count = 1;
3086e4b6088cSJulian Cable 	uinfo->value.integer.min = -5000;
3087e4b6088cSJulian Cable 	uinfo->value.integer.max = 5000;
3088e4b6088cSJulian Cable 	return 0;
3089e4b6088cSJulian Cable }
3090e4b6088cSJulian Cable 
3091e4b6088cSJulian Cable static int snd_hdsp_get_dds_offset(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3092e4b6088cSJulian Cable {
3093e4b6088cSJulian Cable 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
3094e4b6088cSJulian Cable 
3095e4b6088cSJulian Cable 	ucontrol->value.enumerated.item[0] = hdsp_dds_offset(hdsp);
3096e4b6088cSJulian Cable 	return 0;
3097e4b6088cSJulian Cable }
3098e4b6088cSJulian Cable 
3099e4b6088cSJulian Cable static int snd_hdsp_put_dds_offset(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3100e4b6088cSJulian Cable {
3101e4b6088cSJulian Cable 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
3102e4b6088cSJulian Cable 	int change;
3103e4b6088cSJulian Cable 	int val;
3104e4b6088cSJulian Cable 
3105e4b6088cSJulian Cable 	if (!snd_hdsp_use_is_exclusive(hdsp))
3106e4b6088cSJulian Cable 		return -EBUSY;
3107e4b6088cSJulian Cable 	val = ucontrol->value.enumerated.item[0];
3108e4b6088cSJulian Cable 	spin_lock_irq(&hdsp->lock);
3109e4b6088cSJulian Cable 	if (val != hdsp_dds_offset(hdsp))
3110e4b6088cSJulian Cable 		change = (hdsp_set_dds_offset(hdsp, val) == 0) ? 1 : 0;
3111e4b6088cSJulian Cable 	else
3112e4b6088cSJulian Cable 		change = 0;
3113e4b6088cSJulian Cable 	spin_unlock_irq(&hdsp->lock);
3114e4b6088cSJulian Cable 	return change;
3115e4b6088cSJulian Cable }
3116e4b6088cSJulian Cable 
311755e957d8STakashi Iwai static struct snd_kcontrol_new snd_hdsp_9632_controls[] = {
31181da177e4SLinus Torvalds HDSP_DA_GAIN("DA Gain", 0),
31191da177e4SLinus Torvalds HDSP_AD_GAIN("AD Gain", 0),
31201da177e4SLinus Torvalds HDSP_PHONE_GAIN("Phones Gain", 0),
3121e4b6088cSJulian Cable HDSP_XLR_BREAKOUT_CABLE("XLR Breakout Cable", 0),
3122e4b6088cSJulian Cable HDSP_DDS_OFFSET("DDS Sample Rate Offset", 0)
31231da177e4SLinus Torvalds };
31241da177e4SLinus Torvalds 
312555e957d8STakashi Iwai static struct snd_kcontrol_new snd_hdsp_controls[] = {
31261da177e4SLinus Torvalds {
31275549d549SClemens Ladisch 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
31281da177e4SLinus Torvalds 	.name =		SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
31291da177e4SLinus Torvalds 	.info =		snd_hdsp_control_spdif_info,
31301da177e4SLinus Torvalds 	.get =		snd_hdsp_control_spdif_get,
31311da177e4SLinus Torvalds 	.put =		snd_hdsp_control_spdif_put,
31321da177e4SLinus Torvalds },
31331da177e4SLinus Torvalds {
31341da177e4SLinus Torvalds 	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
31355549d549SClemens Ladisch 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
31361da177e4SLinus Torvalds 	.name =		SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
31371da177e4SLinus Torvalds 	.info =		snd_hdsp_control_spdif_stream_info,
31381da177e4SLinus Torvalds 	.get =		snd_hdsp_control_spdif_stream_get,
31391da177e4SLinus Torvalds 	.put =		snd_hdsp_control_spdif_stream_put,
31401da177e4SLinus Torvalds },
31411da177e4SLinus Torvalds {
31421da177e4SLinus Torvalds 	.access =	SNDRV_CTL_ELEM_ACCESS_READ,
31435549d549SClemens Ladisch 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
31441da177e4SLinus Torvalds 	.name =		SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
31451da177e4SLinus Torvalds 	.info =		snd_hdsp_control_spdif_mask_info,
31461da177e4SLinus Torvalds 	.get =		snd_hdsp_control_spdif_mask_get,
31471da177e4SLinus Torvalds 	.private_value = IEC958_AES0_NONAUDIO |
31481da177e4SLinus Torvalds   			 IEC958_AES0_PROFESSIONAL |
31491da177e4SLinus Torvalds 			 IEC958_AES0_CON_EMPHASIS,
31501da177e4SLinus Torvalds },
31511da177e4SLinus Torvalds {
31521da177e4SLinus Torvalds 	.access =	SNDRV_CTL_ELEM_ACCESS_READ,
31535549d549SClemens Ladisch 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
31541da177e4SLinus Torvalds 	.name =		SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
31551da177e4SLinus Torvalds 	.info =		snd_hdsp_control_spdif_mask_info,
31561da177e4SLinus Torvalds 	.get =		snd_hdsp_control_spdif_mask_get,
31571da177e4SLinus Torvalds 	.private_value = IEC958_AES0_NONAUDIO |
31581da177e4SLinus Torvalds 			 IEC958_AES0_PROFESSIONAL |
31591da177e4SLinus Torvalds 			 IEC958_AES0_PRO_EMPHASIS,
31601da177e4SLinus Torvalds },
31611da177e4SLinus Torvalds HDSP_MIXER("Mixer", 0),
31621da177e4SLinus Torvalds HDSP_SPDIF_IN("IEC958 Input Connector", 0),
31631da177e4SLinus Torvalds HDSP_SPDIF_OUT("IEC958 Output also on ADAT1", 0),
31641da177e4SLinus Torvalds HDSP_SPDIF_PROFESSIONAL("IEC958 Professional Bit", 0),
31651da177e4SLinus Torvalds HDSP_SPDIF_EMPHASIS("IEC958 Emphasis Bit", 0),
31661da177e4SLinus Torvalds HDSP_SPDIF_NON_AUDIO("IEC958 Non-audio Bit", 0),
31671da177e4SLinus Torvalds /* 'Sample Clock Source' complies with the alsa control naming scheme */
31681da177e4SLinus Torvalds HDSP_CLOCK_SOURCE("Sample Clock Source", 0),
3169e3ea4d89STakashi Iwai {
3170e3ea4d89STakashi Iwai 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3171e3ea4d89STakashi Iwai 	.name = "Sample Clock Source Locking",
3172e3ea4d89STakashi Iwai 	.info = snd_hdsp_info_clock_source_lock,
3173e3ea4d89STakashi Iwai 	.get = snd_hdsp_get_clock_source_lock,
3174e3ea4d89STakashi Iwai 	.put = snd_hdsp_put_clock_source_lock,
3175e3ea4d89STakashi Iwai },
31761da177e4SLinus Torvalds HDSP_SYSTEM_CLOCK_MODE("System Clock Mode", 0),
31771da177e4SLinus Torvalds HDSP_PREF_SYNC_REF("Preferred Sync Reference", 0),
31781da177e4SLinus Torvalds HDSP_AUTOSYNC_REF("AutoSync Reference", 0),
31791da177e4SLinus Torvalds HDSP_SPDIF_SAMPLE_RATE("SPDIF Sample Rate", 0),
31801da177e4SLinus Torvalds HDSP_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
31811da177e4SLinus Torvalds /* 'External Rate' complies with the alsa control naming scheme */
31821da177e4SLinus Torvalds HDSP_AUTOSYNC_SAMPLE_RATE("External Rate", 0),
31831da177e4SLinus Torvalds HDSP_WC_SYNC_CHECK("Word Clock Lock Status", 0),
31841da177e4SLinus Torvalds HDSP_SPDIF_SYNC_CHECK("SPDIF Lock Status", 0),
31851da177e4SLinus Torvalds HDSP_ADATSYNC_SYNC_CHECK("ADAT Sync Lock Status", 0),
31861da177e4SLinus Torvalds HDSP_LINE_OUT("Line Out", 0),
31871da177e4SLinus Torvalds HDSP_PRECISE_POINTER("Precise Pointer", 0),
31881da177e4SLinus Torvalds HDSP_USE_MIDI_TASKLET("Use Midi Tasklet", 0),
31891da177e4SLinus Torvalds };
31901da177e4SLinus Torvalds 
319155e957d8STakashi Iwai static struct snd_kcontrol_new snd_hdsp_96xx_aeb = HDSP_AEB("Analog Extension Board", 0);
319255e957d8STakashi Iwai static struct snd_kcontrol_new snd_hdsp_adat_sync_check = HDSP_ADAT_SYNC_CHECK;
31931da177e4SLinus Torvalds 
319455e957d8STakashi Iwai static int snd_hdsp_create_controls(struct snd_card *card, struct hdsp *hdsp)
31951da177e4SLinus Torvalds {
31961da177e4SLinus Torvalds 	unsigned int idx;
31971da177e4SLinus Torvalds 	int err;
319855e957d8STakashi Iwai 	struct snd_kcontrol *kctl;
31991da177e4SLinus Torvalds 
32001da177e4SLinus Torvalds 	for (idx = 0; idx < ARRAY_SIZE(snd_hdsp_controls); idx++) {
3201b0b98119STakashi Iwai 		if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_controls[idx], hdsp))) < 0)
32021da177e4SLinus Torvalds 			return err;
32031da177e4SLinus Torvalds 		if (idx == 1)	/* IEC958 (S/PDIF) Stream */
32041da177e4SLinus Torvalds 			hdsp->spdif_ctl = kctl;
32051da177e4SLinus Torvalds 	}
32061da177e4SLinus Torvalds 
32071da177e4SLinus Torvalds 	/* ADAT SyncCheck status */
32081da177e4SLinus Torvalds 	snd_hdsp_adat_sync_check.name = "ADAT Lock Status";
32091da177e4SLinus Torvalds 	snd_hdsp_adat_sync_check.index = 1;
3210b0b98119STakashi Iwai 	if ((err = snd_ctl_add (card, kctl = snd_ctl_new1(&snd_hdsp_adat_sync_check, hdsp))))
32111da177e4SLinus Torvalds 		return err;
32121da177e4SLinus Torvalds 	if (hdsp->io_type == Digiface || hdsp->io_type == H9652) {
32131da177e4SLinus Torvalds 		for (idx = 1; idx < 3; ++idx) {
32141da177e4SLinus Torvalds 			snd_hdsp_adat_sync_check.index = idx+1;
3215b0b98119STakashi Iwai 			if ((err = snd_ctl_add (card, kctl = snd_ctl_new1(&snd_hdsp_adat_sync_check, hdsp))))
32161da177e4SLinus Torvalds 				return err;
32171da177e4SLinus Torvalds 		}
32181da177e4SLinus Torvalds 	}
32191da177e4SLinus Torvalds 
32201da177e4SLinus Torvalds 	/* DA, AD and Phone gain and XLR breakout cable controls for H9632 cards */
32211da177e4SLinus Torvalds 	if (hdsp->io_type == H9632) {
32221da177e4SLinus Torvalds 		for (idx = 0; idx < ARRAY_SIZE(snd_hdsp_9632_controls); idx++) {
3223b0b98119STakashi Iwai 			if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_9632_controls[idx], hdsp))) < 0)
32241da177e4SLinus Torvalds 				return err;
32251da177e4SLinus Torvalds 		}
32261da177e4SLinus Torvalds 	}
32271da177e4SLinus Torvalds 
32281da177e4SLinus Torvalds 	/* AEB control for H96xx card */
32291da177e4SLinus Torvalds 	if (hdsp->io_type == H9632 || hdsp->io_type == H9652) {
3230b0b98119STakashi Iwai 		if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_96xx_aeb, hdsp))) < 0)
32311da177e4SLinus Torvalds 				return err;
32321da177e4SLinus Torvalds 	}
32331da177e4SLinus Torvalds 
32341da177e4SLinus Torvalds 	return 0;
32351da177e4SLinus Torvalds }
32361da177e4SLinus Torvalds 
32371da177e4SLinus Torvalds /*------------------------------------------------------------
32381da177e4SLinus Torvalds    /proc interface
32391da177e4SLinus Torvalds  ------------------------------------------------------------*/
32401da177e4SLinus Torvalds 
32411da177e4SLinus Torvalds static void
324255e957d8STakashi Iwai snd_hdsp_proc_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
32431da177e4SLinus Torvalds {
324455e957d8STakashi Iwai 	struct hdsp *hdsp = (struct hdsp *) entry->private_data;
32451da177e4SLinus Torvalds 	unsigned int status;
32461da177e4SLinus Torvalds 	unsigned int status2;
32471da177e4SLinus Torvalds 	char *pref_sync_ref;
32481da177e4SLinus Torvalds 	char *autosync_ref;
32491da177e4SLinus Torvalds 	char *system_clock_mode;
32501da177e4SLinus Torvalds 	char *clock_source;
32511da177e4SLinus Torvalds 	int x;
32521da177e4SLinus Torvalds 
3253ecb594e6SRemy Bruno 	if (hdsp_check_for_iobox (hdsp)) {
32541da177e4SLinus Torvalds 		snd_iprintf(buffer, "No I/O box connected.\nPlease connect one and upload firmware.\n");
32551da177e4SLinus Torvalds 		return;
3256ecb594e6SRemy Bruno         }
32571da177e4SLinus Torvalds 
3258b0b98119STakashi Iwai 	if (hdsp_check_for_firmware(hdsp, 0)) {
32591da177e4SLinus Torvalds 		if (hdsp->state & HDSP_FirmwareCached) {
32601da177e4SLinus Torvalds 			if (snd_hdsp_load_firmware_from_cache(hdsp) != 0) {
32611da177e4SLinus Torvalds 				snd_iprintf(buffer, "Firmware loading from cache failed, please upload manually.\n");
32621da177e4SLinus Torvalds 				return;
32631da177e4SLinus Torvalds 			}
32641da177e4SLinus Torvalds 		} else {
3265311e70a4STakashi Iwai 			int err = -EINVAL;
3266311e70a4STakashi Iwai #ifdef HDSP_FW_LOADER
3267311e70a4STakashi Iwai 			err = hdsp_request_fw_loader(hdsp);
3268311e70a4STakashi Iwai #endif
3269311e70a4STakashi Iwai 			if (err < 0) {
3270311e70a4STakashi Iwai 				snd_iprintf(buffer,
3271311e70a4STakashi Iwai 					    "No firmware loaded nor cached, "
3272311e70a4STakashi Iwai 					    "please upload firmware.\n");
32731da177e4SLinus Torvalds 				return;
32741da177e4SLinus Torvalds 			}
32751da177e4SLinus Torvalds 		}
3276311e70a4STakashi Iwai 	}
32771da177e4SLinus Torvalds 
32781da177e4SLinus Torvalds 	status = hdsp_read(hdsp, HDSP_statusRegister);
32791da177e4SLinus Torvalds 	status2 = hdsp_read(hdsp, HDSP_status2Register);
32801da177e4SLinus Torvalds 
32811da177e4SLinus Torvalds 	snd_iprintf(buffer, "%s (Card #%d)\n", hdsp->card_name, hdsp->card->number + 1);
32821da177e4SLinus Torvalds 	snd_iprintf(buffer, "Buffers: capture %p playback %p\n",
32831da177e4SLinus Torvalds 		    hdsp->capture_buffer, hdsp->playback_buffer);
32841da177e4SLinus Torvalds 	snd_iprintf(buffer, "IRQ: %d Registers bus: 0x%lx VM: 0x%lx\n",
32851da177e4SLinus Torvalds 		    hdsp->irq, hdsp->port, (unsigned long)hdsp->iobase);
32861da177e4SLinus Torvalds 	snd_iprintf(buffer, "Control register: 0x%x\n", hdsp->control_register);
32871da177e4SLinus Torvalds 	snd_iprintf(buffer, "Control2 register: 0x%x\n", hdsp->control2_register);
32881da177e4SLinus Torvalds 	snd_iprintf(buffer, "Status register: 0x%x\n", status);
32891da177e4SLinus Torvalds 	snd_iprintf(buffer, "Status2 register: 0x%x\n", status2);
32901da177e4SLinus Torvalds 	snd_iprintf(buffer, "FIFO status: %d\n", hdsp_read(hdsp, HDSP_fifoStatus) & 0xff);
32911da177e4SLinus Torvalds 	snd_iprintf(buffer, "MIDI1 Output status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusOut0));
32921da177e4SLinus Torvalds 	snd_iprintf(buffer, "MIDI1 Input status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusIn0));
32931da177e4SLinus Torvalds 	snd_iprintf(buffer, "MIDI2 Output status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusOut1));
32941da177e4SLinus Torvalds 	snd_iprintf(buffer, "MIDI2 Input status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusIn1));
32951da177e4SLinus Torvalds 	snd_iprintf(buffer, "Use Midi Tasklet: %s\n", hdsp->use_midi_tasklet ? "on" : "off");
32961da177e4SLinus Torvalds 
32971da177e4SLinus Torvalds 	snd_iprintf(buffer, "\n");
32981da177e4SLinus Torvalds 
32991da177e4SLinus Torvalds 	x = 1 << (6 + hdsp_decode_latency(hdsp->control_register & HDSP_LatencyMask));
33001da177e4SLinus Torvalds 
33011da177e4SLinus Torvalds 	snd_iprintf(buffer, "Buffer Size (Latency): %d samples (2 periods of %lu bytes)\n", x, (unsigned long) hdsp->period_bytes);
33021da177e4SLinus Torvalds 	snd_iprintf(buffer, "Hardware pointer (frames): %ld\n", hdsp_hw_pointer(hdsp));
33031da177e4SLinus Torvalds 	snd_iprintf(buffer, "Precise pointer: %s\n", hdsp->precise_ptr ? "on" : "off");
33041da177e4SLinus Torvalds 	snd_iprintf(buffer, "Line out: %s\n", (hdsp->control_register & HDSP_LineOut) ? "on" : "off");
33051da177e4SLinus Torvalds 
33061da177e4SLinus Torvalds 	snd_iprintf(buffer, "Firmware version: %d\n", (status2&HDSP_version0)|(status2&HDSP_version1)<<1|(status2&HDSP_version2)<<2);
33071da177e4SLinus Torvalds 
33081da177e4SLinus Torvalds 	snd_iprintf(buffer, "\n");
33091da177e4SLinus Torvalds 
33101da177e4SLinus Torvalds 
33111da177e4SLinus Torvalds 	switch (hdsp_clock_source(hdsp)) {
33121da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_AUTOSYNC:
33131da177e4SLinus Torvalds 		clock_source = "AutoSync";
33141da177e4SLinus Torvalds 		break;
33151da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_32KHZ:
33161da177e4SLinus Torvalds 		clock_source = "Internal 32 kHz";
33171da177e4SLinus Torvalds 		break;
33181da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_44_1KHZ:
33191da177e4SLinus Torvalds 		clock_source = "Internal 44.1 kHz";
33201da177e4SLinus Torvalds 		break;
33211da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_48KHZ:
33221da177e4SLinus Torvalds 		clock_source = "Internal 48 kHz";
33231da177e4SLinus Torvalds 		break;
33241da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_64KHZ:
33251da177e4SLinus Torvalds 		clock_source = "Internal 64 kHz";
33261da177e4SLinus Torvalds 		break;
33271da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_88_2KHZ:
33281da177e4SLinus Torvalds 		clock_source = "Internal 88.2 kHz";
33291da177e4SLinus Torvalds 		break;
33301da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_96KHZ:
33311da177e4SLinus Torvalds 		clock_source = "Internal 96 kHz";
33321da177e4SLinus Torvalds 		break;
33331da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_128KHZ:
33341da177e4SLinus Torvalds 		clock_source = "Internal 128 kHz";
33351da177e4SLinus Torvalds 		break;
33361da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_176_4KHZ:
33371da177e4SLinus Torvalds 		clock_source = "Internal 176.4 kHz";
33381da177e4SLinus Torvalds 		break;
33391da177e4SLinus Torvalds 		case HDSP_CLOCK_SOURCE_INTERNAL_192KHZ:
33401da177e4SLinus Torvalds 		clock_source = "Internal 192 kHz";
33411da177e4SLinus Torvalds 		break;
33421da177e4SLinus Torvalds 	default:
33431da177e4SLinus Torvalds 		clock_source = "Error";
33441da177e4SLinus Torvalds 	}
33451da177e4SLinus Torvalds 	snd_iprintf (buffer, "Sample Clock Source: %s\n", clock_source);
33461da177e4SLinus Torvalds 
3347b0b98119STakashi Iwai 	if (hdsp_system_clock_mode(hdsp))
33481da177e4SLinus Torvalds 		system_clock_mode = "Slave";
3349b0b98119STakashi Iwai 	else
33501da177e4SLinus Torvalds 		system_clock_mode = "Master";
33511da177e4SLinus Torvalds 
33521da177e4SLinus Torvalds 	switch (hdsp_pref_sync_ref (hdsp)) {
33531da177e4SLinus Torvalds 	case HDSP_SYNC_FROM_WORD:
33541da177e4SLinus Torvalds 		pref_sync_ref = "Word Clock";
33551da177e4SLinus Torvalds 		break;
33561da177e4SLinus Torvalds 	case HDSP_SYNC_FROM_ADAT_SYNC:
33571da177e4SLinus Torvalds 		pref_sync_ref = "ADAT Sync";
33581da177e4SLinus Torvalds 		break;
33591da177e4SLinus Torvalds 	case HDSP_SYNC_FROM_SPDIF:
33601da177e4SLinus Torvalds 		pref_sync_ref = "SPDIF";
33611da177e4SLinus Torvalds 		break;
33621da177e4SLinus Torvalds 	case HDSP_SYNC_FROM_ADAT1:
33631da177e4SLinus Torvalds 		pref_sync_ref = "ADAT1";
33641da177e4SLinus Torvalds 		break;
33651da177e4SLinus Torvalds 	case HDSP_SYNC_FROM_ADAT2:
33661da177e4SLinus Torvalds 		pref_sync_ref = "ADAT2";
33671da177e4SLinus Torvalds 		break;
33681da177e4SLinus Torvalds 	case HDSP_SYNC_FROM_ADAT3:
33691da177e4SLinus Torvalds 		pref_sync_ref = "ADAT3";
33701da177e4SLinus Torvalds 		break;
33711da177e4SLinus Torvalds 	default:
33721da177e4SLinus Torvalds 		pref_sync_ref = "Word Clock";
33731da177e4SLinus Torvalds 		break;
33741da177e4SLinus Torvalds 	}
33751da177e4SLinus Torvalds 	snd_iprintf (buffer, "Preferred Sync Reference: %s\n", pref_sync_ref);
33761da177e4SLinus Torvalds 
33771da177e4SLinus Torvalds 	switch (hdsp_autosync_ref (hdsp)) {
33781da177e4SLinus Torvalds 	case HDSP_AUTOSYNC_FROM_WORD:
33791da177e4SLinus Torvalds 		autosync_ref = "Word Clock";
33801da177e4SLinus Torvalds 		break;
33811da177e4SLinus Torvalds 	case HDSP_AUTOSYNC_FROM_ADAT_SYNC:
33821da177e4SLinus Torvalds 		autosync_ref = "ADAT Sync";
33831da177e4SLinus Torvalds 		break;
33841da177e4SLinus Torvalds 	case HDSP_AUTOSYNC_FROM_SPDIF:
33851da177e4SLinus Torvalds 		autosync_ref = "SPDIF";
33861da177e4SLinus Torvalds 		break;
33871da177e4SLinus Torvalds 	case HDSP_AUTOSYNC_FROM_NONE:
33881da177e4SLinus Torvalds 		autosync_ref = "None";
33891da177e4SLinus Torvalds 		break;
33901da177e4SLinus Torvalds 	case HDSP_AUTOSYNC_FROM_ADAT1:
33911da177e4SLinus Torvalds 		autosync_ref = "ADAT1";
33921da177e4SLinus Torvalds 		break;
33931da177e4SLinus Torvalds 	case HDSP_AUTOSYNC_FROM_ADAT2:
33941da177e4SLinus Torvalds 		autosync_ref = "ADAT2";
33951da177e4SLinus Torvalds 		break;
33961da177e4SLinus Torvalds 	case HDSP_AUTOSYNC_FROM_ADAT3:
33971da177e4SLinus Torvalds 		autosync_ref = "ADAT3";
33981da177e4SLinus Torvalds 		break;
33991da177e4SLinus Torvalds 	default:
34001da177e4SLinus Torvalds 		autosync_ref = "---";
34011da177e4SLinus Torvalds 		break;
34021da177e4SLinus Torvalds 	}
34031da177e4SLinus Torvalds 	snd_iprintf (buffer, "AutoSync Reference: %s\n", autosync_ref);
34041da177e4SLinus Torvalds 
34051da177e4SLinus Torvalds 	snd_iprintf (buffer, "AutoSync Frequency: %d\n", hdsp_external_sample_rate(hdsp));
34061da177e4SLinus Torvalds 
34071da177e4SLinus Torvalds 	snd_iprintf (buffer, "System Clock Mode: %s\n", system_clock_mode);
34081da177e4SLinus Torvalds 
34091da177e4SLinus Torvalds 	snd_iprintf (buffer, "System Clock Frequency: %d\n", hdsp->system_sample_rate);
3410e3ea4d89STakashi Iwai 	snd_iprintf (buffer, "System Clock Locked: %s\n", hdsp->clock_source_locked ? "Yes" : "No");
34111da177e4SLinus Torvalds 
34121da177e4SLinus Torvalds 	snd_iprintf(buffer, "\n");
34131da177e4SLinus Torvalds 
34141da177e4SLinus Torvalds 	switch (hdsp_spdif_in(hdsp)) {
34151da177e4SLinus Torvalds 	case HDSP_SPDIFIN_OPTICAL:
34161da177e4SLinus Torvalds 		snd_iprintf(buffer, "IEC958 input: Optical\n");
34171da177e4SLinus Torvalds 		break;
34181da177e4SLinus Torvalds 	case HDSP_SPDIFIN_COAXIAL:
34191da177e4SLinus Torvalds 		snd_iprintf(buffer, "IEC958 input: Coaxial\n");
34201da177e4SLinus Torvalds 		break;
34211da177e4SLinus Torvalds 	case HDSP_SPDIFIN_INTERNAL:
34221da177e4SLinus Torvalds 		snd_iprintf(buffer, "IEC958 input: Internal\n");
34231da177e4SLinus Torvalds 		break;
34241da177e4SLinus Torvalds 	case HDSP_SPDIFIN_AES:
34251da177e4SLinus Torvalds 		snd_iprintf(buffer, "IEC958 input: AES\n");
34261da177e4SLinus Torvalds 		break;
34271da177e4SLinus Torvalds 	default:
34281da177e4SLinus Torvalds 		snd_iprintf(buffer, "IEC958 input: ???\n");
34291da177e4SLinus Torvalds 		break;
34301da177e4SLinus Torvalds 	}
34311da177e4SLinus Torvalds 
3432b0b98119STakashi Iwai 	if (hdsp->control_register & HDSP_SPDIFOpticalOut)
34331da177e4SLinus Torvalds 		snd_iprintf(buffer, "IEC958 output: Coaxial & ADAT1\n");
3434b0b98119STakashi Iwai 	else
34351da177e4SLinus Torvalds 		snd_iprintf(buffer, "IEC958 output: Coaxial only\n");
34361da177e4SLinus Torvalds 
3437b0b98119STakashi Iwai 	if (hdsp->control_register & HDSP_SPDIFProfessional)
34381da177e4SLinus Torvalds 		snd_iprintf(buffer, "IEC958 quality: Professional\n");
3439b0b98119STakashi Iwai 	else
34401da177e4SLinus Torvalds 		snd_iprintf(buffer, "IEC958 quality: Consumer\n");
34411da177e4SLinus Torvalds 
3442b0b98119STakashi Iwai 	if (hdsp->control_register & HDSP_SPDIFEmphasis)
34431da177e4SLinus Torvalds 		snd_iprintf(buffer, "IEC958 emphasis: on\n");
3444b0b98119STakashi Iwai 	else
34451da177e4SLinus Torvalds 		snd_iprintf(buffer, "IEC958 emphasis: off\n");
34461da177e4SLinus Torvalds 
3447b0b98119STakashi Iwai 	if (hdsp->control_register & HDSP_SPDIFNonAudio)
34481da177e4SLinus Torvalds 		snd_iprintf(buffer, "IEC958 NonAudio: on\n");
3449b0b98119STakashi Iwai 	else
34501da177e4SLinus Torvalds 		snd_iprintf(buffer, "IEC958 NonAudio: off\n");
3451b0b98119STakashi Iwai 	if ((x = hdsp_spdif_sample_rate (hdsp)) != 0)
34521da177e4SLinus Torvalds 		snd_iprintf (buffer, "IEC958 sample rate: %d\n", x);
3453b0b98119STakashi Iwai 	else
34541da177e4SLinus Torvalds 		snd_iprintf (buffer, "IEC958 sample rate: Error flag set\n");
34551da177e4SLinus Torvalds 
34561da177e4SLinus Torvalds 	snd_iprintf(buffer, "\n");
34571da177e4SLinus Torvalds 
34581da177e4SLinus Torvalds 	/* Sync Check */
34591da177e4SLinus Torvalds 	x = status & HDSP_Sync0;
3460b0b98119STakashi Iwai 	if (status & HDSP_Lock0)
34611da177e4SLinus Torvalds 		snd_iprintf(buffer, "ADAT1: %s\n", x ? "Sync" : "Lock");
3462b0b98119STakashi Iwai 	else
34631da177e4SLinus Torvalds 		snd_iprintf(buffer, "ADAT1: No Lock\n");
34641da177e4SLinus Torvalds 
34651da177e4SLinus Torvalds 	switch (hdsp->io_type) {
34661da177e4SLinus Torvalds 	case Digiface:
34671da177e4SLinus Torvalds 	case H9652:
34681da177e4SLinus Torvalds 		x = status & HDSP_Sync1;
3469b0b98119STakashi Iwai 		if (status & HDSP_Lock1)
34701da177e4SLinus Torvalds 			snd_iprintf(buffer, "ADAT2: %s\n", x ? "Sync" : "Lock");
3471b0b98119STakashi Iwai 		else
34721da177e4SLinus Torvalds 			snd_iprintf(buffer, "ADAT2: No Lock\n");
34731da177e4SLinus Torvalds 		x = status & HDSP_Sync2;
3474b0b98119STakashi Iwai 		if (status & HDSP_Lock2)
34751da177e4SLinus Torvalds 			snd_iprintf(buffer, "ADAT3: %s\n", x ? "Sync" : "Lock");
3476b0b98119STakashi Iwai 		else
34771da177e4SLinus Torvalds 			snd_iprintf(buffer, "ADAT3: No Lock\n");
3478b0b98119STakashi Iwai 		break;
34791da177e4SLinus Torvalds 	default:
34801da177e4SLinus Torvalds 		/* relax */
34811da177e4SLinus Torvalds 		break;
34821da177e4SLinus Torvalds 	}
34831da177e4SLinus Torvalds 
34841da177e4SLinus Torvalds 	x = status & HDSP_SPDIFSync;
3485b0b98119STakashi Iwai 	if (status & HDSP_SPDIFErrorFlag)
34861da177e4SLinus Torvalds 		snd_iprintf (buffer, "SPDIF: No Lock\n");
3487b0b98119STakashi Iwai 	else
34881da177e4SLinus Torvalds 		snd_iprintf (buffer, "SPDIF: %s\n", x ? "Sync" : "Lock");
34891da177e4SLinus Torvalds 
34901da177e4SLinus Torvalds 	x = status2 & HDSP_wc_sync;
3491b0b98119STakashi Iwai 	if (status2 & HDSP_wc_lock)
34921da177e4SLinus Torvalds 		snd_iprintf (buffer, "Word Clock: %s\n", x ? "Sync" : "Lock");
3493b0b98119STakashi Iwai 	else
34941da177e4SLinus Torvalds 		snd_iprintf (buffer, "Word Clock: No Lock\n");
34951da177e4SLinus Torvalds 
34961da177e4SLinus Torvalds 	x = status & HDSP_TimecodeSync;
3497b0b98119STakashi Iwai 	if (status & HDSP_TimecodeLock)
34981da177e4SLinus Torvalds 		snd_iprintf(buffer, "ADAT Sync: %s\n", x ? "Sync" : "Lock");
3499b0b98119STakashi Iwai 	else
35001da177e4SLinus Torvalds 		snd_iprintf(buffer, "ADAT Sync: No Lock\n");
35011da177e4SLinus Torvalds 
35021da177e4SLinus Torvalds 	snd_iprintf(buffer, "\n");
35031da177e4SLinus Torvalds 
35041da177e4SLinus Torvalds 	/* Informations about H9632 specific controls */
35051da177e4SLinus Torvalds 	if (hdsp->io_type == H9632) {
35061da177e4SLinus Torvalds 		char *tmp;
35071da177e4SLinus Torvalds 
35081da177e4SLinus Torvalds 		switch (hdsp_ad_gain(hdsp)) {
35091da177e4SLinus Torvalds 		case 0:
35101da177e4SLinus Torvalds 			tmp = "-10 dBV";
35111da177e4SLinus Torvalds 			break;
35121da177e4SLinus Torvalds 		case 1:
35131da177e4SLinus Torvalds 			tmp = "+4 dBu";
35141da177e4SLinus Torvalds 			break;
35151da177e4SLinus Torvalds 		default:
35161da177e4SLinus Torvalds 			tmp = "Lo Gain";
35171da177e4SLinus Torvalds 			break;
35181da177e4SLinus Torvalds 		}
35191da177e4SLinus Torvalds 		snd_iprintf(buffer, "AD Gain : %s\n", tmp);
35201da177e4SLinus Torvalds 
35211da177e4SLinus Torvalds 		switch (hdsp_da_gain(hdsp)) {
35221da177e4SLinus Torvalds 		case 0:
35231da177e4SLinus Torvalds 			tmp = "Hi Gain";
35241da177e4SLinus Torvalds 			break;
35251da177e4SLinus Torvalds 		case 1:
35261da177e4SLinus Torvalds 			tmp = "+4 dBu";
35271da177e4SLinus Torvalds 			break;
35281da177e4SLinus Torvalds 		default:
35291da177e4SLinus Torvalds 			tmp = "-10 dBV";
35301da177e4SLinus Torvalds 			break;
35311da177e4SLinus Torvalds 		}
35321da177e4SLinus Torvalds 		snd_iprintf(buffer, "DA Gain : %s\n", tmp);
35331da177e4SLinus Torvalds 
35341da177e4SLinus Torvalds 		switch (hdsp_phone_gain(hdsp)) {
35351da177e4SLinus Torvalds 		case 0:
35361da177e4SLinus Torvalds 			tmp = "0 dB";
35371da177e4SLinus Torvalds 			break;
35381da177e4SLinus Torvalds 		case 1:
35391da177e4SLinus Torvalds 			tmp = "-6 dB";
35401da177e4SLinus Torvalds 			break;
35411da177e4SLinus Torvalds 		default:
35421da177e4SLinus Torvalds 			tmp = "-12 dB";
35431da177e4SLinus Torvalds 			break;
35441da177e4SLinus Torvalds 		}
35451da177e4SLinus Torvalds 		snd_iprintf(buffer, "Phones Gain : %s\n", tmp);
35461da177e4SLinus Torvalds 
35471da177e4SLinus Torvalds 		snd_iprintf(buffer, "XLR Breakout Cable : %s\n", hdsp_xlr_breakout_cable(hdsp) ? "yes" : "no");
35481da177e4SLinus Torvalds 
3549b0b98119STakashi Iwai 		if (hdsp->control_register & HDSP_AnalogExtensionBoard)
35501da177e4SLinus Torvalds 			snd_iprintf(buffer, "AEB : on (ADAT1 internal)\n");
3551b0b98119STakashi Iwai 		else
35521da177e4SLinus Torvalds 			snd_iprintf(buffer, "AEB : off (ADAT1 external)\n");
35531da177e4SLinus Torvalds 		snd_iprintf(buffer, "\n");
35541da177e4SLinus Torvalds 	}
35551da177e4SLinus Torvalds 
35561da177e4SLinus Torvalds }
35571da177e4SLinus Torvalds 
355855e957d8STakashi Iwai static void __devinit snd_hdsp_proc_init(struct hdsp *hdsp)
35591da177e4SLinus Torvalds {
356055e957d8STakashi Iwai 	struct snd_info_entry *entry;
35611da177e4SLinus Torvalds 
35621da177e4SLinus Torvalds 	if (! snd_card_proc_new(hdsp->card, "hdsp", &entry))
3563bf850204STakashi Iwai 		snd_info_set_text_ops(entry, hdsp, snd_hdsp_proc_read);
35641da177e4SLinus Torvalds }
35651da177e4SLinus Torvalds 
356655e957d8STakashi Iwai static void snd_hdsp_free_buffers(struct hdsp *hdsp)
35671da177e4SLinus Torvalds {
35681da177e4SLinus Torvalds 	snd_hammerfall_free_buffer(&hdsp->capture_dma_buf, hdsp->pci);
35691da177e4SLinus Torvalds 	snd_hammerfall_free_buffer(&hdsp->playback_dma_buf, hdsp->pci);
35701da177e4SLinus Torvalds }
35711da177e4SLinus Torvalds 
357255e957d8STakashi Iwai static int __devinit snd_hdsp_initialize_memory(struct hdsp *hdsp)
35731da177e4SLinus Torvalds {
35741da177e4SLinus Torvalds 	unsigned long pb_bus, cb_bus;
35751da177e4SLinus Torvalds 
35761da177e4SLinus Torvalds 	if (snd_hammerfall_get_buffer(hdsp->pci, &hdsp->capture_dma_buf, HDSP_DMA_AREA_BYTES) < 0 ||
35771da177e4SLinus Torvalds 	    snd_hammerfall_get_buffer(hdsp->pci, &hdsp->playback_dma_buf, HDSP_DMA_AREA_BYTES) < 0) {
35781da177e4SLinus Torvalds 		if (hdsp->capture_dma_buf.area)
35791da177e4SLinus Torvalds 			snd_dma_free_pages(&hdsp->capture_dma_buf);
35801da177e4SLinus Torvalds 		printk(KERN_ERR "%s: no buffers available\n", hdsp->card_name);
35811da177e4SLinus Torvalds 		return -ENOMEM;
35821da177e4SLinus Torvalds 	}
35831da177e4SLinus Torvalds 
35841da177e4SLinus Torvalds 	/* Align to bus-space 64K boundary */
35851da177e4SLinus Torvalds 
35867ab39926SClemens Ladisch 	cb_bus = ALIGN(hdsp->capture_dma_buf.addr, 0x10000ul);
35877ab39926SClemens Ladisch 	pb_bus = ALIGN(hdsp->playback_dma_buf.addr, 0x10000ul);
35881da177e4SLinus Torvalds 
35891da177e4SLinus Torvalds 	/* Tell the card where it is */
35901da177e4SLinus Torvalds 
35911da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_inputBufferAddress, cb_bus);
35921da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_outputBufferAddress, pb_bus);
35931da177e4SLinus Torvalds 
35941da177e4SLinus Torvalds 	hdsp->capture_buffer = hdsp->capture_dma_buf.area + (cb_bus - hdsp->capture_dma_buf.addr);
35951da177e4SLinus Torvalds 	hdsp->playback_buffer = hdsp->playback_dma_buf.area + (pb_bus - hdsp->playback_dma_buf.addr);
35961da177e4SLinus Torvalds 
35971da177e4SLinus Torvalds 	return 0;
35981da177e4SLinus Torvalds }
35991da177e4SLinus Torvalds 
360055e957d8STakashi Iwai static int snd_hdsp_set_defaults(struct hdsp *hdsp)
36011da177e4SLinus Torvalds {
36021da177e4SLinus Torvalds 	unsigned int i;
36031da177e4SLinus Torvalds 
36041da177e4SLinus Torvalds 	/* ASSUMPTION: hdsp->lock is either held, or
36051da177e4SLinus Torvalds 	   there is no need to hold it (e.g. during module
36061da177e4SLinus Torvalds 	   initalization).
36071da177e4SLinus Torvalds 	 */
36081da177e4SLinus Torvalds 
36091da177e4SLinus Torvalds 	/* set defaults:
36101da177e4SLinus Torvalds 
36111da177e4SLinus Torvalds 	   SPDIF Input via Coax
36121da177e4SLinus Torvalds 	   Master clock mode
36131da177e4SLinus Torvalds 	   maximum latency (7 => 2^7 = 8192 samples, 64Kbyte buffer,
36141da177e4SLinus Torvalds 	                    which implies 2 4096 sample, 32Kbyte periods).
36151da177e4SLinus Torvalds            Enable line out.
36161da177e4SLinus Torvalds 	 */
36171da177e4SLinus Torvalds 
36181da177e4SLinus Torvalds 	hdsp->control_register = HDSP_ClockModeMaster |
36191da177e4SLinus Torvalds 		                 HDSP_SPDIFInputCoaxial |
36201da177e4SLinus Torvalds 		                 hdsp_encode_latency(7) |
36211da177e4SLinus Torvalds 		                 HDSP_LineOut;
36221da177e4SLinus Torvalds 
36231da177e4SLinus Torvalds 
36241da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
36251da177e4SLinus Torvalds 
36261da177e4SLinus Torvalds #ifdef SNDRV_BIG_ENDIAN
36271da177e4SLinus Torvalds 	hdsp->control2_register = HDSP_BIGENDIAN_MODE;
36281da177e4SLinus Torvalds #else
36291da177e4SLinus Torvalds 	hdsp->control2_register = 0;
36301da177e4SLinus Torvalds #endif
3631b0b98119STakashi Iwai 	if (hdsp->io_type == H9652)
36321da177e4SLinus Torvalds 	        snd_hdsp_9652_enable_mixer (hdsp);
3633b0b98119STakashi Iwai 	else
36341da177e4SLinus Torvalds 		hdsp_write (hdsp, HDSP_control2Reg, hdsp->control2_register);
36351da177e4SLinus Torvalds 
36361da177e4SLinus Torvalds 	hdsp_reset_hw_pointer(hdsp);
36371da177e4SLinus Torvalds 	hdsp_compute_period_size(hdsp);
36381da177e4SLinus Torvalds 
36391da177e4SLinus Torvalds 	/* silence everything */
36401da177e4SLinus Torvalds 
3641b0b98119STakashi Iwai 	for (i = 0; i < HDSP_MATRIX_MIXER_SIZE; ++i)
36421da177e4SLinus Torvalds 		hdsp->mixer_matrix[i] = MINUS_INFINITY_GAIN;
36431da177e4SLinus Torvalds 
36441da177e4SLinus Torvalds 	for (i = 0; i < ((hdsp->io_type == H9652 || hdsp->io_type == H9632) ? 1352 : HDSP_MATRIX_MIXER_SIZE); ++i) {
3645b0b98119STakashi Iwai 		if (hdsp_write_gain (hdsp, i, MINUS_INFINITY_GAIN))
36461da177e4SLinus Torvalds 			return -EIO;
36471da177e4SLinus Torvalds 	}
36481da177e4SLinus Torvalds 
36491da177e4SLinus Torvalds 	/* H9632 specific defaults */
36501da177e4SLinus Torvalds 	if (hdsp->io_type == H9632) {
36511da177e4SLinus Torvalds 		hdsp->control_register |= (HDSP_DAGainPlus4dBu | HDSP_ADGainPlus4dBu | HDSP_PhoneGain0dB);
36521da177e4SLinus Torvalds 		hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
36531da177e4SLinus Torvalds 	}
36541da177e4SLinus Torvalds 
36551da177e4SLinus Torvalds 	/* set a default rate so that the channel map is set up.
36561da177e4SLinus Torvalds 	 */
36571da177e4SLinus Torvalds 
36581da177e4SLinus Torvalds 	hdsp_set_rate(hdsp, 48000, 1);
36591da177e4SLinus Torvalds 
36601da177e4SLinus Torvalds 	return 0;
36611da177e4SLinus Torvalds }
36621da177e4SLinus Torvalds 
36631da177e4SLinus Torvalds static void hdsp_midi_tasklet(unsigned long arg)
36641da177e4SLinus Torvalds {
366555e957d8STakashi Iwai 	struct hdsp *hdsp = (struct hdsp *)arg;
36661da177e4SLinus Torvalds 
3667b0b98119STakashi Iwai 	if (hdsp->midi[0].pending)
36681da177e4SLinus Torvalds 		snd_hdsp_midi_input_read (&hdsp->midi[0]);
3669b0b98119STakashi Iwai 	if (hdsp->midi[1].pending)
36701da177e4SLinus Torvalds 		snd_hdsp_midi_input_read (&hdsp->midi[1]);
36711da177e4SLinus Torvalds }
36721da177e4SLinus Torvalds 
36737d12e780SDavid Howells static irqreturn_t snd_hdsp_interrupt(int irq, void *dev_id)
36741da177e4SLinus Torvalds {
367555e957d8STakashi Iwai 	struct hdsp *hdsp = (struct hdsp *) dev_id;
36761da177e4SLinus Torvalds 	unsigned int status;
36771da177e4SLinus Torvalds 	int audio;
36781da177e4SLinus Torvalds 	int midi0;
36791da177e4SLinus Torvalds 	int midi1;
36801da177e4SLinus Torvalds 	unsigned int midi0status;
36811da177e4SLinus Torvalds 	unsigned int midi1status;
36821da177e4SLinus Torvalds 	int schedule = 0;
36831da177e4SLinus Torvalds 
36841da177e4SLinus Torvalds 	status = hdsp_read(hdsp, HDSP_statusRegister);
36851da177e4SLinus Torvalds 
36861da177e4SLinus Torvalds 	audio = status & HDSP_audioIRQPending;
36871da177e4SLinus Torvalds 	midi0 = status & HDSP_midi0IRQPending;
36881da177e4SLinus Torvalds 	midi1 = status & HDSP_midi1IRQPending;
36891da177e4SLinus Torvalds 
3690b0b98119STakashi Iwai 	if (!audio && !midi0 && !midi1)
36911da177e4SLinus Torvalds 		return IRQ_NONE;
36921da177e4SLinus Torvalds 
36931da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_interruptConfirmation, 0);
36941da177e4SLinus Torvalds 
36951da177e4SLinus Torvalds 	midi0status = hdsp_read (hdsp, HDSP_midiStatusIn0) & 0xff;
36961da177e4SLinus Torvalds 	midi1status = hdsp_read (hdsp, HDSP_midiStatusIn1) & 0xff;
36971da177e4SLinus Torvalds 
36981da177e4SLinus Torvalds 	if (audio) {
3699b0b98119STakashi Iwai 		if (hdsp->capture_substream)
37001da177e4SLinus Torvalds 			snd_pcm_period_elapsed(hdsp->pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream);
37011da177e4SLinus Torvalds 
3702b0b98119STakashi Iwai 		if (hdsp->playback_substream)
37031da177e4SLinus Torvalds 			snd_pcm_period_elapsed(hdsp->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream);
37041da177e4SLinus Torvalds 	}
37051da177e4SLinus Torvalds 
37061da177e4SLinus Torvalds 	if (midi0 && midi0status) {
37071da177e4SLinus Torvalds 		if (hdsp->use_midi_tasklet) {
37081da177e4SLinus Torvalds 			/* we disable interrupts for this input until processing is done */
37091da177e4SLinus Torvalds 			hdsp->control_register &= ~HDSP_Midi0InterruptEnable;
37101da177e4SLinus Torvalds 			hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
37111da177e4SLinus Torvalds 			hdsp->midi[0].pending = 1;
37121da177e4SLinus Torvalds 			schedule = 1;
37131da177e4SLinus Torvalds 		} else {
37141da177e4SLinus Torvalds 			snd_hdsp_midi_input_read (&hdsp->midi[0]);
37151da177e4SLinus Torvalds 		}
37161da177e4SLinus Torvalds 	}
37171da177e4SLinus Torvalds 	if (hdsp->io_type != Multiface && hdsp->io_type != H9632 && midi1 && midi1status) {
37181da177e4SLinus Torvalds 		if (hdsp->use_midi_tasklet) {
37191da177e4SLinus Torvalds 			/* we disable interrupts for this input until processing is done */
37201da177e4SLinus Torvalds 			hdsp->control_register &= ~HDSP_Midi1InterruptEnable;
37211da177e4SLinus Torvalds 			hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
37221da177e4SLinus Torvalds 			hdsp->midi[1].pending = 1;
37231da177e4SLinus Torvalds 			schedule = 1;
37241da177e4SLinus Torvalds 		} else {
37251da177e4SLinus Torvalds 			snd_hdsp_midi_input_read (&hdsp->midi[1]);
37261da177e4SLinus Torvalds 		}
37271da177e4SLinus Torvalds 	}
37281da177e4SLinus Torvalds 	if (hdsp->use_midi_tasklet && schedule)
37291da177e4SLinus Torvalds 		tasklet_hi_schedule(&hdsp->midi_tasklet);
37301da177e4SLinus Torvalds 	return IRQ_HANDLED;
37311da177e4SLinus Torvalds }
37321da177e4SLinus Torvalds 
373355e957d8STakashi Iwai static snd_pcm_uframes_t snd_hdsp_hw_pointer(struct snd_pcm_substream *substream)
37341da177e4SLinus Torvalds {
373555e957d8STakashi Iwai 	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
37361da177e4SLinus Torvalds 	return hdsp_hw_pointer(hdsp);
37371da177e4SLinus Torvalds }
37381da177e4SLinus Torvalds 
373955e957d8STakashi Iwai static char *hdsp_channel_buffer_location(struct hdsp *hdsp,
37401da177e4SLinus Torvalds 					     int stream,
37411da177e4SLinus Torvalds 					     int channel)
37421da177e4SLinus Torvalds 
37431da177e4SLinus Torvalds {
37441da177e4SLinus Torvalds 	int mapped_channel;
37451da177e4SLinus Torvalds 
37461da177e4SLinus Torvalds         snd_assert(channel >= 0 && channel < hdsp->max_channels, return NULL);
37471da177e4SLinus Torvalds 
3748b0b98119STakashi Iwai 	if ((mapped_channel = hdsp->channel_map[channel]) < 0)
37491da177e4SLinus Torvalds 		return NULL;
37501da177e4SLinus Torvalds 
3751b0b98119STakashi Iwai 	if (stream == SNDRV_PCM_STREAM_CAPTURE)
37521da177e4SLinus Torvalds 		return hdsp->capture_buffer + (mapped_channel * HDSP_CHANNEL_BUFFER_BYTES);
3753b0b98119STakashi Iwai 	else
37541da177e4SLinus Torvalds 		return hdsp->playback_buffer + (mapped_channel * HDSP_CHANNEL_BUFFER_BYTES);
37551da177e4SLinus Torvalds }
37561da177e4SLinus Torvalds 
375755e957d8STakashi Iwai static int snd_hdsp_playback_copy(struct snd_pcm_substream *substream, int channel,
37581da177e4SLinus Torvalds 				  snd_pcm_uframes_t pos, void __user *src, snd_pcm_uframes_t count)
37591da177e4SLinus Torvalds {
376055e957d8STakashi Iwai 	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
37611da177e4SLinus Torvalds 	char *channel_buf;
37621da177e4SLinus Torvalds 
37631da177e4SLinus Torvalds 	snd_assert(pos + count <= HDSP_CHANNEL_BUFFER_BYTES / 4, return -EINVAL);
37641da177e4SLinus Torvalds 
37651da177e4SLinus Torvalds 	channel_buf = hdsp_channel_buffer_location (hdsp, substream->pstr->stream, channel);
37661da177e4SLinus Torvalds 	snd_assert(channel_buf != NULL, return -EIO);
37671da177e4SLinus Torvalds 	if (copy_from_user(channel_buf + pos * 4, src, count * 4))
37681da177e4SLinus Torvalds 		return -EFAULT;
37691da177e4SLinus Torvalds 	return count;
37701da177e4SLinus Torvalds }
37711da177e4SLinus Torvalds 
377255e957d8STakashi Iwai static int snd_hdsp_capture_copy(struct snd_pcm_substream *substream, int channel,
37731da177e4SLinus Torvalds 				 snd_pcm_uframes_t pos, void __user *dst, snd_pcm_uframes_t count)
37741da177e4SLinus Torvalds {
377555e957d8STakashi Iwai 	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
37761da177e4SLinus Torvalds 	char *channel_buf;
37771da177e4SLinus Torvalds 
37781da177e4SLinus Torvalds 	snd_assert(pos + count <= HDSP_CHANNEL_BUFFER_BYTES / 4, return -EINVAL);
37791da177e4SLinus Torvalds 
37801da177e4SLinus Torvalds 	channel_buf = hdsp_channel_buffer_location (hdsp, substream->pstr->stream, channel);
37811da177e4SLinus Torvalds 	snd_assert(channel_buf != NULL, return -EIO);
37821da177e4SLinus Torvalds 	if (copy_to_user(dst, channel_buf + pos * 4, count * 4))
37831da177e4SLinus Torvalds 		return -EFAULT;
37841da177e4SLinus Torvalds 	return count;
37851da177e4SLinus Torvalds }
37861da177e4SLinus Torvalds 
378755e957d8STakashi Iwai static int snd_hdsp_hw_silence(struct snd_pcm_substream *substream, int channel,
37881da177e4SLinus Torvalds 				  snd_pcm_uframes_t pos, snd_pcm_uframes_t count)
37891da177e4SLinus Torvalds {
379055e957d8STakashi Iwai 	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
37911da177e4SLinus Torvalds 	char *channel_buf;
37921da177e4SLinus Torvalds 
37931da177e4SLinus Torvalds 	channel_buf = hdsp_channel_buffer_location (hdsp, substream->pstr->stream, channel);
37941da177e4SLinus Torvalds 	snd_assert(channel_buf != NULL, return -EIO);
37951da177e4SLinus Torvalds 	memset(channel_buf + pos * 4, 0, count * 4);
37961da177e4SLinus Torvalds 	return count;
37971da177e4SLinus Torvalds }
37981da177e4SLinus Torvalds 
379955e957d8STakashi Iwai static int snd_hdsp_reset(struct snd_pcm_substream *substream)
38001da177e4SLinus Torvalds {
380155e957d8STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
380255e957d8STakashi Iwai 	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
380355e957d8STakashi Iwai 	struct snd_pcm_substream *other;
38041da177e4SLinus Torvalds 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
38051da177e4SLinus Torvalds 		other = hdsp->capture_substream;
38061da177e4SLinus Torvalds 	else
38071da177e4SLinus Torvalds 		other = hdsp->playback_substream;
38081da177e4SLinus Torvalds 	if (hdsp->running)
38091da177e4SLinus Torvalds 		runtime->status->hw_ptr = hdsp_hw_pointer(hdsp);
38101da177e4SLinus Torvalds 	else
38111da177e4SLinus Torvalds 		runtime->status->hw_ptr = 0;
38121da177e4SLinus Torvalds 	if (other) {
381355e957d8STakashi Iwai 		struct snd_pcm_substream *s;
381455e957d8STakashi Iwai 		struct snd_pcm_runtime *oruntime = other->runtime;
3815ef991b95STakashi Iwai 		snd_pcm_group_for_each_entry(s, substream) {
38161da177e4SLinus Torvalds 			if (s == other) {
38171da177e4SLinus Torvalds 				oruntime->status->hw_ptr = runtime->status->hw_ptr;
38181da177e4SLinus Torvalds 				break;
38191da177e4SLinus Torvalds 			}
38201da177e4SLinus Torvalds 		}
38211da177e4SLinus Torvalds 	}
38221da177e4SLinus Torvalds 	return 0;
38231da177e4SLinus Torvalds }
38241da177e4SLinus Torvalds 
382555e957d8STakashi Iwai static int snd_hdsp_hw_params(struct snd_pcm_substream *substream,
382655e957d8STakashi Iwai 				 struct snd_pcm_hw_params *params)
38271da177e4SLinus Torvalds {
382855e957d8STakashi Iwai 	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
38291da177e4SLinus Torvalds 	int err;
38301da177e4SLinus Torvalds 	pid_t this_pid;
38311da177e4SLinus Torvalds 	pid_t other_pid;
38321da177e4SLinus Torvalds 
3833b0b98119STakashi Iwai 	if (hdsp_check_for_iobox (hdsp))
38341da177e4SLinus Torvalds 		return -EIO;
38351da177e4SLinus Torvalds 
3836b0b98119STakashi Iwai 	if (hdsp_check_for_firmware(hdsp, 1))
38371da177e4SLinus Torvalds 		return -EIO;
38381da177e4SLinus Torvalds 
38391da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
38401da177e4SLinus Torvalds 
38411da177e4SLinus Torvalds 	if (substream->pstr->stream == SNDRV_PCM_STREAM_PLAYBACK) {
38421da177e4SLinus Torvalds 		hdsp->control_register &= ~(HDSP_SPDIFProfessional | HDSP_SPDIFNonAudio | HDSP_SPDIFEmphasis);
38431da177e4SLinus Torvalds 		hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register |= hdsp->creg_spdif_stream);
38441da177e4SLinus Torvalds 		this_pid = hdsp->playback_pid;
38451da177e4SLinus Torvalds 		other_pid = hdsp->capture_pid;
38461da177e4SLinus Torvalds 	} else {
38471da177e4SLinus Torvalds 		this_pid = hdsp->capture_pid;
38481da177e4SLinus Torvalds 		other_pid = hdsp->playback_pid;
38491da177e4SLinus Torvalds 	}
38501da177e4SLinus Torvalds 
38511da177e4SLinus Torvalds 	if ((other_pid > 0) && (this_pid != other_pid)) {
38521da177e4SLinus Torvalds 
38531da177e4SLinus Torvalds 		/* The other stream is open, and not by the same
38541da177e4SLinus Torvalds 		   task as this one. Make sure that the parameters
38551da177e4SLinus Torvalds 		   that matter are the same.
38561da177e4SLinus Torvalds 		 */
38571da177e4SLinus Torvalds 
38581da177e4SLinus Torvalds 		if (params_rate(params) != hdsp->system_sample_rate) {
38591da177e4SLinus Torvalds 			spin_unlock_irq(&hdsp->lock);
38601da177e4SLinus Torvalds 			_snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_RATE);
38611da177e4SLinus Torvalds 			return -EBUSY;
38621da177e4SLinus Torvalds 		}
38631da177e4SLinus Torvalds 
38641da177e4SLinus Torvalds 		if (params_period_size(params) != hdsp->period_bytes / 4) {
38651da177e4SLinus Torvalds 			spin_unlock_irq(&hdsp->lock);
38661da177e4SLinus Torvalds 			_snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
38671da177e4SLinus Torvalds 			return -EBUSY;
38681da177e4SLinus Torvalds 		}
38691da177e4SLinus Torvalds 
38701da177e4SLinus Torvalds 		/* We're fine. */
38711da177e4SLinus Torvalds 
38721da177e4SLinus Torvalds 		spin_unlock_irq(&hdsp->lock);
38731da177e4SLinus Torvalds  		return 0;
38741da177e4SLinus Torvalds 
38751da177e4SLinus Torvalds 	} else {
38761da177e4SLinus Torvalds 		spin_unlock_irq(&hdsp->lock);
38771da177e4SLinus Torvalds 	}
38781da177e4SLinus Torvalds 
38791da177e4SLinus Torvalds 	/* how to make sure that the rate matches an externally-set one ?
38801da177e4SLinus Torvalds 	 */
38811da177e4SLinus Torvalds 
38821da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
3883e3ea4d89STakashi Iwai 	if (! hdsp->clock_source_locked) {
38841da177e4SLinus Torvalds 		if ((err = hdsp_set_rate(hdsp, params_rate(params), 0)) < 0) {
38851da177e4SLinus Torvalds 			spin_unlock_irq(&hdsp->lock);
38861da177e4SLinus Torvalds 			_snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_RATE);
38871da177e4SLinus Torvalds 			return err;
38881da177e4SLinus Torvalds 		}
3889e3ea4d89STakashi Iwai 	}
3890e3ea4d89STakashi Iwai 	spin_unlock_irq(&hdsp->lock);
38911da177e4SLinus Torvalds 
38921da177e4SLinus Torvalds 	if ((err = hdsp_set_interrupt_interval(hdsp, params_period_size(params))) < 0) {
38931da177e4SLinus Torvalds 		_snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
38941da177e4SLinus Torvalds 		return err;
38951da177e4SLinus Torvalds 	}
38961da177e4SLinus Torvalds 
38971da177e4SLinus Torvalds 	return 0;
38981da177e4SLinus Torvalds }
38991da177e4SLinus Torvalds 
390055e957d8STakashi Iwai static int snd_hdsp_channel_info(struct snd_pcm_substream *substream,
390155e957d8STakashi Iwai 				    struct snd_pcm_channel_info *info)
39021da177e4SLinus Torvalds {
390355e957d8STakashi Iwai 	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
39041da177e4SLinus Torvalds 	int mapped_channel;
39051da177e4SLinus Torvalds 
39061da177e4SLinus Torvalds 	snd_assert(info->channel < hdsp->max_channels, return -EINVAL);
39071da177e4SLinus Torvalds 
3908b0b98119STakashi Iwai 	if ((mapped_channel = hdsp->channel_map[info->channel]) < 0)
39091da177e4SLinus Torvalds 		return -EINVAL;
39101da177e4SLinus Torvalds 
39111da177e4SLinus Torvalds 	info->offset = mapped_channel * HDSP_CHANNEL_BUFFER_BYTES;
39121da177e4SLinus Torvalds 	info->first = 0;
39131da177e4SLinus Torvalds 	info->step = 32;
39141da177e4SLinus Torvalds 	return 0;
39151da177e4SLinus Torvalds }
39161da177e4SLinus Torvalds 
391755e957d8STakashi Iwai static int snd_hdsp_ioctl(struct snd_pcm_substream *substream,
39181da177e4SLinus Torvalds 			     unsigned int cmd, void *arg)
39191da177e4SLinus Torvalds {
39201da177e4SLinus Torvalds 	switch (cmd) {
39211da177e4SLinus Torvalds 	case SNDRV_PCM_IOCTL1_RESET:
39221da177e4SLinus Torvalds 		return snd_hdsp_reset(substream);
39231da177e4SLinus Torvalds 	case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
3924b0b98119STakashi Iwai 		return snd_hdsp_channel_info(substream, arg);
39251da177e4SLinus Torvalds 	default:
39261da177e4SLinus Torvalds 		break;
39271da177e4SLinus Torvalds 	}
39281da177e4SLinus Torvalds 
39291da177e4SLinus Torvalds 	return snd_pcm_lib_ioctl(substream, cmd, arg);
39301da177e4SLinus Torvalds }
39311da177e4SLinus Torvalds 
393255e957d8STakashi Iwai static int snd_hdsp_trigger(struct snd_pcm_substream *substream, int cmd)
39331da177e4SLinus Torvalds {
393455e957d8STakashi Iwai 	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
393555e957d8STakashi Iwai 	struct snd_pcm_substream *other;
39361da177e4SLinus Torvalds 	int running;
39371da177e4SLinus Torvalds 
3938b0b98119STakashi Iwai 	if (hdsp_check_for_iobox (hdsp))
39391da177e4SLinus Torvalds 		return -EIO;
39401da177e4SLinus Torvalds 
3941311e70a4STakashi Iwai 	if (hdsp_check_for_firmware(hdsp, 0)) /* no auto-loading in trigger */
39421da177e4SLinus Torvalds 		return -EIO;
39431da177e4SLinus Torvalds 
39441da177e4SLinus Torvalds 	spin_lock(&hdsp->lock);
39451da177e4SLinus Torvalds 	running = hdsp->running;
39461da177e4SLinus Torvalds 	switch (cmd) {
39471da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_START:
39481da177e4SLinus Torvalds 		running |= 1 << substream->stream;
39491da177e4SLinus Torvalds 		break;
39501da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_STOP:
39511da177e4SLinus Torvalds 		running &= ~(1 << substream->stream);
39521da177e4SLinus Torvalds 		break;
39531da177e4SLinus Torvalds 	default:
39541da177e4SLinus Torvalds 		snd_BUG();
39551da177e4SLinus Torvalds 		spin_unlock(&hdsp->lock);
39561da177e4SLinus Torvalds 		return -EINVAL;
39571da177e4SLinus Torvalds 	}
39581da177e4SLinus Torvalds 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
39591da177e4SLinus Torvalds 		other = hdsp->capture_substream;
39601da177e4SLinus Torvalds 	else
39611da177e4SLinus Torvalds 		other = hdsp->playback_substream;
39621da177e4SLinus Torvalds 
39631da177e4SLinus Torvalds 	if (other) {
396455e957d8STakashi Iwai 		struct snd_pcm_substream *s;
3965ef991b95STakashi Iwai 		snd_pcm_group_for_each_entry(s, substream) {
39661da177e4SLinus Torvalds 			if (s == other) {
39671da177e4SLinus Torvalds 				snd_pcm_trigger_done(s, substream);
39681da177e4SLinus Torvalds 				if (cmd == SNDRV_PCM_TRIGGER_START)
39691da177e4SLinus Torvalds 					running |= 1 << s->stream;
39701da177e4SLinus Torvalds 				else
39711da177e4SLinus Torvalds 					running &= ~(1 << s->stream);
39721da177e4SLinus Torvalds 				goto _ok;
39731da177e4SLinus Torvalds 			}
39741da177e4SLinus Torvalds 		}
39751da177e4SLinus Torvalds 		if (cmd == SNDRV_PCM_TRIGGER_START) {
39761da177e4SLinus Torvalds 			if (!(running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) &&
39771da177e4SLinus Torvalds 			    substream->stream == SNDRV_PCM_STREAM_CAPTURE)
39781da177e4SLinus Torvalds 				hdsp_silence_playback(hdsp);
39791da177e4SLinus Torvalds 		} else {
39801da177e4SLinus Torvalds 			if (running &&
39811da177e4SLinus Torvalds 			    substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
39821da177e4SLinus Torvalds 				hdsp_silence_playback(hdsp);
39831da177e4SLinus Torvalds 		}
39841da177e4SLinus Torvalds 	} else {
39851da177e4SLinus Torvalds 		if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
39861da177e4SLinus Torvalds 				hdsp_silence_playback(hdsp);
39871da177e4SLinus Torvalds 	}
39881da177e4SLinus Torvalds  _ok:
39891da177e4SLinus Torvalds 	snd_pcm_trigger_done(substream, substream);
39901da177e4SLinus Torvalds 	if (!hdsp->running && running)
39911da177e4SLinus Torvalds 		hdsp_start_audio(hdsp);
39921da177e4SLinus Torvalds 	else if (hdsp->running && !running)
39931da177e4SLinus Torvalds 		hdsp_stop_audio(hdsp);
39941da177e4SLinus Torvalds 	hdsp->running = running;
39951da177e4SLinus Torvalds 	spin_unlock(&hdsp->lock);
39961da177e4SLinus Torvalds 
39971da177e4SLinus Torvalds 	return 0;
39981da177e4SLinus Torvalds }
39991da177e4SLinus Torvalds 
400055e957d8STakashi Iwai static int snd_hdsp_prepare(struct snd_pcm_substream *substream)
40011da177e4SLinus Torvalds {
400255e957d8STakashi Iwai 	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
40031da177e4SLinus Torvalds 	int result = 0;
40041da177e4SLinus Torvalds 
4005b0b98119STakashi Iwai 	if (hdsp_check_for_iobox (hdsp))
40061da177e4SLinus Torvalds 		return -EIO;
40071da177e4SLinus Torvalds 
4008b0b98119STakashi Iwai 	if (hdsp_check_for_firmware(hdsp, 1))
40091da177e4SLinus Torvalds 		return -EIO;
40101da177e4SLinus Torvalds 
40111da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
40121da177e4SLinus Torvalds 	if (!hdsp->running)
40131da177e4SLinus Torvalds 		hdsp_reset_hw_pointer(hdsp);
40141da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
40151da177e4SLinus Torvalds 	return result;
40161da177e4SLinus Torvalds }
40171da177e4SLinus Torvalds 
401855e957d8STakashi Iwai static struct snd_pcm_hardware snd_hdsp_playback_subinfo =
40191da177e4SLinus Torvalds {
40201da177e4SLinus Torvalds 	.info =			(SNDRV_PCM_INFO_MMAP |
40211da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_MMAP_VALID |
40221da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_NONINTERLEAVED |
40231da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_SYNC_START |
40241da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_DOUBLE),
40251da177e4SLinus Torvalds #ifdef SNDRV_BIG_ENDIAN
40261da177e4SLinus Torvalds 	.formats =		SNDRV_PCM_FMTBIT_S32_BE,
40271da177e4SLinus Torvalds #else
40281da177e4SLinus Torvalds 	.formats =		SNDRV_PCM_FMTBIT_S32_LE,
40291da177e4SLinus Torvalds #endif
40301da177e4SLinus Torvalds 	.rates =		(SNDRV_PCM_RATE_32000 |
40311da177e4SLinus Torvalds 				 SNDRV_PCM_RATE_44100 |
40321da177e4SLinus Torvalds 				 SNDRV_PCM_RATE_48000 |
40331da177e4SLinus Torvalds 				 SNDRV_PCM_RATE_64000 |
40341da177e4SLinus Torvalds 				 SNDRV_PCM_RATE_88200 |
40351da177e4SLinus Torvalds 				 SNDRV_PCM_RATE_96000),
40361da177e4SLinus Torvalds 	.rate_min =		32000,
40371da177e4SLinus Torvalds 	.rate_max =		96000,
40381da177e4SLinus Torvalds 	.channels_min =		14,
40391da177e4SLinus Torvalds 	.channels_max =		HDSP_MAX_CHANNELS,
40401da177e4SLinus Torvalds 	.buffer_bytes_max =	HDSP_CHANNEL_BUFFER_BYTES * HDSP_MAX_CHANNELS,
40411da177e4SLinus Torvalds 	.period_bytes_min =	(64 * 4) * 10,
40421da177e4SLinus Torvalds 	.period_bytes_max =	(8192 * 4) * HDSP_MAX_CHANNELS,
40431da177e4SLinus Torvalds 	.periods_min =		2,
40441da177e4SLinus Torvalds 	.periods_max =		2,
40451da177e4SLinus Torvalds 	.fifo_size =		0
40461da177e4SLinus Torvalds };
40471da177e4SLinus Torvalds 
404855e957d8STakashi Iwai static struct snd_pcm_hardware snd_hdsp_capture_subinfo =
40491da177e4SLinus Torvalds {
40501da177e4SLinus Torvalds 	.info =			(SNDRV_PCM_INFO_MMAP |
40511da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_MMAP_VALID |
40521da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_NONINTERLEAVED |
40531da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_SYNC_START),
40541da177e4SLinus Torvalds #ifdef SNDRV_BIG_ENDIAN
40551da177e4SLinus Torvalds 	.formats =		SNDRV_PCM_FMTBIT_S32_BE,
40561da177e4SLinus Torvalds #else
40571da177e4SLinus Torvalds 	.formats =		SNDRV_PCM_FMTBIT_S32_LE,
40581da177e4SLinus Torvalds #endif
40591da177e4SLinus Torvalds 	.rates =		(SNDRV_PCM_RATE_32000 |
40601da177e4SLinus Torvalds 				 SNDRV_PCM_RATE_44100 |
40611da177e4SLinus Torvalds 				 SNDRV_PCM_RATE_48000 |
40621da177e4SLinus Torvalds 				 SNDRV_PCM_RATE_64000 |
40631da177e4SLinus Torvalds 				 SNDRV_PCM_RATE_88200 |
40641da177e4SLinus Torvalds 				 SNDRV_PCM_RATE_96000),
40651da177e4SLinus Torvalds 	.rate_min =		32000,
40661da177e4SLinus Torvalds 	.rate_max =		96000,
40671da177e4SLinus Torvalds 	.channels_min =		14,
40681da177e4SLinus Torvalds 	.channels_max =		HDSP_MAX_CHANNELS,
40691da177e4SLinus Torvalds 	.buffer_bytes_max =	HDSP_CHANNEL_BUFFER_BYTES * HDSP_MAX_CHANNELS,
40701da177e4SLinus Torvalds 	.period_bytes_min =	(64 * 4) * 10,
40711da177e4SLinus Torvalds 	.period_bytes_max =	(8192 * 4) * HDSP_MAX_CHANNELS,
40721da177e4SLinus Torvalds 	.periods_min =		2,
40731da177e4SLinus Torvalds 	.periods_max =		2,
40741da177e4SLinus Torvalds 	.fifo_size =		0
40751da177e4SLinus Torvalds };
40761da177e4SLinus Torvalds 
40771da177e4SLinus Torvalds static unsigned int hdsp_period_sizes[] = { 64, 128, 256, 512, 1024, 2048, 4096, 8192 };
40781da177e4SLinus Torvalds 
407955e957d8STakashi Iwai static struct snd_pcm_hw_constraint_list hdsp_hw_constraints_period_sizes = {
40801da177e4SLinus Torvalds 	.count = ARRAY_SIZE(hdsp_period_sizes),
40811da177e4SLinus Torvalds 	.list = hdsp_period_sizes,
40821da177e4SLinus Torvalds 	.mask = 0
40831da177e4SLinus Torvalds };
40841da177e4SLinus Torvalds 
40851da177e4SLinus Torvalds static unsigned int hdsp_9632_sample_rates[] = { 32000, 44100, 48000, 64000, 88200, 96000, 128000, 176400, 192000 };
40861da177e4SLinus Torvalds 
408755e957d8STakashi Iwai static struct snd_pcm_hw_constraint_list hdsp_hw_constraints_9632_sample_rates = {
40881da177e4SLinus Torvalds 	.count = ARRAY_SIZE(hdsp_9632_sample_rates),
40891da177e4SLinus Torvalds 	.list = hdsp_9632_sample_rates,
40901da177e4SLinus Torvalds 	.mask = 0
40911da177e4SLinus Torvalds };
40921da177e4SLinus Torvalds 
409355e957d8STakashi Iwai static int snd_hdsp_hw_rule_in_channels(struct snd_pcm_hw_params *params,
409455e957d8STakashi Iwai 					struct snd_pcm_hw_rule *rule)
40951da177e4SLinus Torvalds {
409655e957d8STakashi Iwai 	struct hdsp *hdsp = rule->private;
409755e957d8STakashi Iwai 	struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
40981da177e4SLinus Torvalds 	if (hdsp->io_type == H9632) {
40991da177e4SLinus Torvalds 		unsigned int list[3];
41001da177e4SLinus Torvalds 		list[0] = hdsp->qs_in_channels;
41011da177e4SLinus Torvalds 		list[1] = hdsp->ds_in_channels;
41021da177e4SLinus Torvalds 		list[2] = hdsp->ss_in_channels;
41031da177e4SLinus Torvalds 		return snd_interval_list(c, 3, list, 0);
41041da177e4SLinus Torvalds 	} else {
41051da177e4SLinus Torvalds 		unsigned int list[2];
41061da177e4SLinus Torvalds 		list[0] = hdsp->ds_in_channels;
41071da177e4SLinus Torvalds 		list[1] = hdsp->ss_in_channels;
41081da177e4SLinus Torvalds 		return snd_interval_list(c, 2, list, 0);
41091da177e4SLinus Torvalds 	}
41101da177e4SLinus Torvalds }
41111da177e4SLinus Torvalds 
411255e957d8STakashi Iwai static int snd_hdsp_hw_rule_out_channels(struct snd_pcm_hw_params *params,
411355e957d8STakashi Iwai 					struct snd_pcm_hw_rule *rule)
41141da177e4SLinus Torvalds {
41151da177e4SLinus Torvalds 	unsigned int list[3];
411655e957d8STakashi Iwai 	struct hdsp *hdsp = rule->private;
411755e957d8STakashi Iwai 	struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
41181da177e4SLinus Torvalds 	if (hdsp->io_type == H9632) {
41191da177e4SLinus Torvalds 		list[0] = hdsp->qs_out_channels;
41201da177e4SLinus Torvalds 		list[1] = hdsp->ds_out_channels;
41211da177e4SLinus Torvalds 		list[2] = hdsp->ss_out_channels;
41221da177e4SLinus Torvalds 		return snd_interval_list(c, 3, list, 0);
41231da177e4SLinus Torvalds 	} else {
41241da177e4SLinus Torvalds 		list[0] = hdsp->ds_out_channels;
41251da177e4SLinus Torvalds 		list[1] = hdsp->ss_out_channels;
41261da177e4SLinus Torvalds 	}
41271da177e4SLinus Torvalds 	return snd_interval_list(c, 2, list, 0);
41281da177e4SLinus Torvalds }
41291da177e4SLinus Torvalds 
413055e957d8STakashi Iwai static int snd_hdsp_hw_rule_in_channels_rate(struct snd_pcm_hw_params *params,
413155e957d8STakashi Iwai 					     struct snd_pcm_hw_rule *rule)
41321da177e4SLinus Torvalds {
413355e957d8STakashi Iwai 	struct hdsp *hdsp = rule->private;
413455e957d8STakashi Iwai 	struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
413555e957d8STakashi Iwai 	struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
41361da177e4SLinus Torvalds 	if (r->min > 96000 && hdsp->io_type == H9632) {
413755e957d8STakashi Iwai 		struct snd_interval t = {
41381da177e4SLinus Torvalds 			.min = hdsp->qs_in_channels,
41391da177e4SLinus Torvalds 			.max = hdsp->qs_in_channels,
41401da177e4SLinus Torvalds 			.integer = 1,
41411da177e4SLinus Torvalds 		};
41421da177e4SLinus Torvalds 		return snd_interval_refine(c, &t);
41431da177e4SLinus Torvalds 	} else if (r->min > 48000 && r->max <= 96000) {
414455e957d8STakashi Iwai 		struct snd_interval t = {
41451da177e4SLinus Torvalds 			.min = hdsp->ds_in_channels,
41461da177e4SLinus Torvalds 			.max = hdsp->ds_in_channels,
41471da177e4SLinus Torvalds 			.integer = 1,
41481da177e4SLinus Torvalds 		};
41491da177e4SLinus Torvalds 		return snd_interval_refine(c, &t);
41501da177e4SLinus Torvalds 	} else if (r->max < 64000) {
415155e957d8STakashi Iwai 		struct snd_interval t = {
41521da177e4SLinus Torvalds 			.min = hdsp->ss_in_channels,
41531da177e4SLinus Torvalds 			.max = hdsp->ss_in_channels,
41541da177e4SLinus Torvalds 			.integer = 1,
41551da177e4SLinus Torvalds 		};
41561da177e4SLinus Torvalds 		return snd_interval_refine(c, &t);
41571da177e4SLinus Torvalds 	}
41581da177e4SLinus Torvalds 	return 0;
41591da177e4SLinus Torvalds }
41601da177e4SLinus Torvalds 
416155e957d8STakashi Iwai static int snd_hdsp_hw_rule_out_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_out_channels,
41701da177e4SLinus Torvalds 			.max = hdsp->qs_out_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_out_channels,
41771da177e4SLinus Torvalds 			.max = hdsp->ds_out_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_out_channels,
41841da177e4SLinus Torvalds 			.max = hdsp->ss_out_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_rate_out_channels(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 (c->min >= hdsp->ss_out_channels) {
419955e957d8STakashi Iwai 		struct snd_interval t = {
42001da177e4SLinus Torvalds 			.min = 32000,
42011da177e4SLinus Torvalds 			.max = 48000,
42021da177e4SLinus Torvalds 			.integer = 1,
42031da177e4SLinus Torvalds 		};
42041da177e4SLinus Torvalds 		return snd_interval_refine(r, &t);
42051da177e4SLinus Torvalds 	} else if (c->max <= hdsp->qs_out_channels && hdsp->io_type == H9632) {
420655e957d8STakashi Iwai 		struct snd_interval t = {
42071da177e4SLinus Torvalds 			.min = 128000,
42081da177e4SLinus Torvalds 			.max = 192000,
42091da177e4SLinus Torvalds 			.integer = 1,
42101da177e4SLinus Torvalds 		};
42111da177e4SLinus Torvalds 		return snd_interval_refine(r, &t);
42121da177e4SLinus Torvalds 	} else if (c->max <= hdsp->ds_out_channels) {
421355e957d8STakashi Iwai 		struct snd_interval t = {
42141da177e4SLinus Torvalds 			.min = 64000,
42151da177e4SLinus Torvalds 			.max = 96000,
42161da177e4SLinus Torvalds 			.integer = 1,
42171da177e4SLinus Torvalds 		};
42181da177e4SLinus Torvalds 		return snd_interval_refine(r, &t);
42191da177e4SLinus Torvalds 	}
42201da177e4SLinus Torvalds 	return 0;
42211da177e4SLinus Torvalds }
42221da177e4SLinus Torvalds 
422355e957d8STakashi Iwai static int snd_hdsp_hw_rule_rate_in_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_in_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_in_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_in_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_playback_open(struct snd_pcm_substream *substream)
42551da177e4SLinus Torvalds {
425655e957d8STakashi Iwai 	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
425755e957d8STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
42581da177e4SLinus Torvalds 
4259b0b98119STakashi Iwai 	if (hdsp_check_for_iobox (hdsp))
42601da177e4SLinus Torvalds 		return -EIO;
42611da177e4SLinus Torvalds 
4262b0b98119STakashi Iwai 	if (hdsp_check_for_firmware(hdsp, 1))
42631da177e4SLinus Torvalds 		return -EIO;
42641da177e4SLinus Torvalds 
42651da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
42661da177e4SLinus Torvalds 
42671da177e4SLinus Torvalds 	snd_pcm_set_sync(substream);
42681da177e4SLinus Torvalds 
42691da177e4SLinus Torvalds         runtime->hw = snd_hdsp_playback_subinfo;
42701da177e4SLinus Torvalds 	runtime->dma_area = hdsp->playback_buffer;
42711da177e4SLinus Torvalds 	runtime->dma_bytes = HDSP_DMA_AREA_BYTES;
42721da177e4SLinus Torvalds 
42731da177e4SLinus Torvalds 	hdsp->playback_pid = current->pid;
42741da177e4SLinus Torvalds 	hdsp->playback_substream = substream;
42751da177e4SLinus Torvalds 
42761da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
42771da177e4SLinus Torvalds 
42781da177e4SLinus Torvalds 	snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
42791da177e4SLinus Torvalds 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, &hdsp_hw_constraints_period_sizes);
4280e3ea4d89STakashi Iwai 	if (hdsp->clock_source_locked) {
4281e3ea4d89STakashi Iwai 		runtime->hw.rate_min = runtime->hw.rate_max = hdsp->system_sample_rate;
4282e3ea4d89STakashi Iwai 	} else if (hdsp->io_type == H9632) {
42831da177e4SLinus Torvalds 		runtime->hw.rate_max = 192000;
42841da177e4SLinus Torvalds 		runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
42851da177e4SLinus Torvalds 		snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hdsp_hw_constraints_9632_sample_rates);
42861da177e4SLinus Torvalds 	}
4287e3ea4d89STakashi Iwai 	if (hdsp->io_type == H9632) {
4288e3ea4d89STakashi Iwai 		runtime->hw.channels_min = hdsp->qs_out_channels;
4289e3ea4d89STakashi Iwai 		runtime->hw.channels_max = hdsp->ss_out_channels;
4290e3ea4d89STakashi Iwai 	}
42911da177e4SLinus Torvalds 
42921da177e4SLinus Torvalds 	snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
42931da177e4SLinus Torvalds 			     snd_hdsp_hw_rule_out_channels, hdsp,
42941da177e4SLinus Torvalds 			     SNDRV_PCM_HW_PARAM_CHANNELS, -1);
42951da177e4SLinus Torvalds 	snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
42961da177e4SLinus Torvalds 			     snd_hdsp_hw_rule_out_channels_rate, hdsp,
42971da177e4SLinus Torvalds 			     SNDRV_PCM_HW_PARAM_RATE, -1);
42981da177e4SLinus Torvalds 	snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
42991da177e4SLinus Torvalds 			     snd_hdsp_hw_rule_rate_out_channels, hdsp,
43001da177e4SLinus Torvalds 			     SNDRV_PCM_HW_PARAM_CHANNELS, -1);
43011da177e4SLinus Torvalds 
43021da177e4SLinus Torvalds 	hdsp->creg_spdif_stream = hdsp->creg_spdif;
43031da177e4SLinus Torvalds 	hdsp->spdif_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
43041da177e4SLinus Torvalds 	snd_ctl_notify(hdsp->card, SNDRV_CTL_EVENT_MASK_VALUE |
43051da177e4SLinus Torvalds 		       SNDRV_CTL_EVENT_MASK_INFO, &hdsp->spdif_ctl->id);
43061da177e4SLinus Torvalds 	return 0;
43071da177e4SLinus Torvalds }
43081da177e4SLinus Torvalds 
430955e957d8STakashi Iwai static int snd_hdsp_playback_release(struct snd_pcm_substream *substream)
43101da177e4SLinus Torvalds {
431155e957d8STakashi Iwai 	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
43121da177e4SLinus Torvalds 
43131da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
43141da177e4SLinus Torvalds 
43151da177e4SLinus Torvalds 	hdsp->playback_pid = -1;
43161da177e4SLinus Torvalds 	hdsp->playback_substream = NULL;
43171da177e4SLinus Torvalds 
43181da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
43191da177e4SLinus Torvalds 
43201da177e4SLinus Torvalds 	hdsp->spdif_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
43211da177e4SLinus Torvalds 	snd_ctl_notify(hdsp->card, SNDRV_CTL_EVENT_MASK_VALUE |
43221da177e4SLinus Torvalds 		       SNDRV_CTL_EVENT_MASK_INFO, &hdsp->spdif_ctl->id);
43231da177e4SLinus Torvalds 	return 0;
43241da177e4SLinus Torvalds }
43251da177e4SLinus Torvalds 
43261da177e4SLinus Torvalds 
432755e957d8STakashi Iwai static int snd_hdsp_capture_open(struct snd_pcm_substream *substream)
43281da177e4SLinus Torvalds {
432955e957d8STakashi Iwai 	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
433055e957d8STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
43311da177e4SLinus Torvalds 
4332b0b98119STakashi Iwai 	if (hdsp_check_for_iobox (hdsp))
43331da177e4SLinus Torvalds 		return -EIO;
43341da177e4SLinus Torvalds 
4335b0b98119STakashi Iwai 	if (hdsp_check_for_firmware(hdsp, 1))
43361da177e4SLinus Torvalds 		return -EIO;
43371da177e4SLinus Torvalds 
43381da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
43391da177e4SLinus Torvalds 
43401da177e4SLinus Torvalds 	snd_pcm_set_sync(substream);
43411da177e4SLinus Torvalds 
43421da177e4SLinus Torvalds 	runtime->hw = snd_hdsp_capture_subinfo;
43431da177e4SLinus Torvalds 	runtime->dma_area = hdsp->capture_buffer;
43441da177e4SLinus Torvalds 	runtime->dma_bytes = HDSP_DMA_AREA_BYTES;
43451da177e4SLinus Torvalds 
43461da177e4SLinus Torvalds 	hdsp->capture_pid = current->pid;
43471da177e4SLinus Torvalds 	hdsp->capture_substream = substream;
43481da177e4SLinus Torvalds 
43491da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
43501da177e4SLinus Torvalds 
43511da177e4SLinus Torvalds 	snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
43521da177e4SLinus Torvalds 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, &hdsp_hw_constraints_period_sizes);
43531da177e4SLinus Torvalds 	if (hdsp->io_type == H9632) {
43541da177e4SLinus Torvalds 		runtime->hw.channels_min = hdsp->qs_in_channels;
43551da177e4SLinus Torvalds 		runtime->hw.channels_max = hdsp->ss_in_channels;
43561da177e4SLinus Torvalds 		runtime->hw.rate_max = 192000;
43571da177e4SLinus Torvalds 		runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
43581da177e4SLinus Torvalds 		snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hdsp_hw_constraints_9632_sample_rates);
43591da177e4SLinus Torvalds 	}
43601da177e4SLinus Torvalds 	snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
43611da177e4SLinus Torvalds 			     snd_hdsp_hw_rule_in_channels, hdsp,
43621da177e4SLinus Torvalds 			     SNDRV_PCM_HW_PARAM_CHANNELS, -1);
43631da177e4SLinus Torvalds 	snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
43641da177e4SLinus Torvalds 			     snd_hdsp_hw_rule_in_channels_rate, hdsp,
43651da177e4SLinus Torvalds 			     SNDRV_PCM_HW_PARAM_RATE, -1);
43661da177e4SLinus Torvalds 	snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
43671da177e4SLinus Torvalds 			     snd_hdsp_hw_rule_rate_in_channels, hdsp,
43681da177e4SLinus Torvalds 			     SNDRV_PCM_HW_PARAM_CHANNELS, -1);
43691da177e4SLinus Torvalds 	return 0;
43701da177e4SLinus Torvalds }
43711da177e4SLinus Torvalds 
437255e957d8STakashi Iwai static int snd_hdsp_capture_release(struct snd_pcm_substream *substream)
43731da177e4SLinus Torvalds {
437455e957d8STakashi Iwai 	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
43751da177e4SLinus Torvalds 
43761da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
43771da177e4SLinus Torvalds 
43781da177e4SLinus Torvalds 	hdsp->capture_pid = -1;
43791da177e4SLinus Torvalds 	hdsp->capture_substream = NULL;
43801da177e4SLinus Torvalds 
43811da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
43821da177e4SLinus Torvalds 	return 0;
43831da177e4SLinus Torvalds }
43841da177e4SLinus Torvalds 
438555e957d8STakashi Iwai static int snd_hdsp_hwdep_dummy_op(struct snd_hwdep *hw, struct file *file)
43861da177e4SLinus Torvalds {
43871da177e4SLinus Torvalds 	/* we have nothing to initialize but the call is required */
43881da177e4SLinus Torvalds 	return 0;
43891da177e4SLinus Torvalds }
43901da177e4SLinus Torvalds 
43911da177e4SLinus Torvalds 
43921da177e4SLinus Torvalds /* helper functions for copying meter values */
43931da177e4SLinus Torvalds static inline int copy_u32_le(void __user *dest, void __iomem *src)
43941da177e4SLinus Torvalds {
43951da177e4SLinus Torvalds 	u32 val = readl(src);
43961da177e4SLinus Torvalds 	return copy_to_user(dest, &val, 4);
43971da177e4SLinus Torvalds }
43981da177e4SLinus Torvalds 
43991da177e4SLinus Torvalds static inline int copy_u64_le(void __user *dest, void __iomem *src_low, void __iomem *src_high)
44001da177e4SLinus Torvalds {
44011da177e4SLinus Torvalds 	u32 rms_low, rms_high;
44021da177e4SLinus Torvalds 	u64 rms;
44031da177e4SLinus Torvalds 	rms_low = readl(src_low);
44041da177e4SLinus Torvalds 	rms_high = readl(src_high);
44051da177e4SLinus Torvalds 	rms = ((u64)rms_high << 32) | rms_low;
44061da177e4SLinus Torvalds 	return copy_to_user(dest, &rms, 8);
44071da177e4SLinus Torvalds }
44081da177e4SLinus Torvalds 
44091da177e4SLinus Torvalds static inline int copy_u48_le(void __user *dest, void __iomem *src_low, void __iomem *src_high)
44101da177e4SLinus Torvalds {
44111da177e4SLinus Torvalds 	u32 rms_low, rms_high;
44121da177e4SLinus Torvalds 	u64 rms;
44131da177e4SLinus Torvalds 	rms_low = readl(src_low) & 0xffffff00;
44141da177e4SLinus Torvalds 	rms_high = readl(src_high) & 0xffffff00;
44151da177e4SLinus Torvalds 	rms = ((u64)rms_high << 32) | rms_low;
44161da177e4SLinus Torvalds 	return copy_to_user(dest, &rms, 8);
44171da177e4SLinus Torvalds }
44181da177e4SLinus Torvalds 
441955e957d8STakashi Iwai static int hdsp_9652_get_peak(struct hdsp *hdsp, struct hdsp_peak_rms __user *peak_rms)
44201da177e4SLinus Torvalds {
44211da177e4SLinus Torvalds 	int doublespeed = 0;
44221da177e4SLinus Torvalds 	int i, j, channels, ofs;
44231da177e4SLinus Torvalds 
44241da177e4SLinus Torvalds 	if (hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DoubleSpeedStatus)
44251da177e4SLinus Torvalds 		doublespeed = 1;
44261da177e4SLinus Torvalds 	channels = doublespeed ? 14 : 26;
44271da177e4SLinus Torvalds 	for (i = 0, j = 0; i < 26; ++i) {
44281da177e4SLinus Torvalds 		if (doublespeed && (i & 4))
44291da177e4SLinus Torvalds 			continue;
44301da177e4SLinus Torvalds 		ofs = HDSP_9652_peakBase - j * 4;
44311da177e4SLinus Torvalds 		if (copy_u32_le(&peak_rms->input_peaks[i], hdsp->iobase + ofs))
44321da177e4SLinus Torvalds 			return -EFAULT;
44331da177e4SLinus Torvalds 		ofs -= channels * 4;
44341da177e4SLinus Torvalds 		if (copy_u32_le(&peak_rms->playback_peaks[i], hdsp->iobase + ofs))
44351da177e4SLinus Torvalds 			return -EFAULT;
44361da177e4SLinus Torvalds 		ofs -= channels * 4;
44371da177e4SLinus Torvalds 		if (copy_u32_le(&peak_rms->output_peaks[i], hdsp->iobase + ofs))
44381da177e4SLinus Torvalds 			return -EFAULT;
44391da177e4SLinus Torvalds 		ofs = HDSP_9652_rmsBase + j * 8;
44401da177e4SLinus Torvalds 		if (copy_u48_le(&peak_rms->input_rms[i], hdsp->iobase + ofs,
44411da177e4SLinus Torvalds 				hdsp->iobase + ofs + 4))
44421da177e4SLinus Torvalds 			return -EFAULT;
44431da177e4SLinus Torvalds 		ofs += channels * 8;
44441da177e4SLinus Torvalds 		if (copy_u48_le(&peak_rms->playback_rms[i], hdsp->iobase + ofs,
44451da177e4SLinus Torvalds 				hdsp->iobase + ofs + 4))
44461da177e4SLinus Torvalds 			return -EFAULT;
44471da177e4SLinus Torvalds 		ofs += channels * 8;
44481da177e4SLinus Torvalds 		if (copy_u48_le(&peak_rms->output_rms[i], hdsp->iobase + ofs,
44491da177e4SLinus Torvalds 				hdsp->iobase + ofs + 4))
44501da177e4SLinus Torvalds 			return -EFAULT;
44511da177e4SLinus Torvalds 		j++;
44521da177e4SLinus Torvalds 	}
44531da177e4SLinus Torvalds 	return 0;
44541da177e4SLinus Torvalds }
44551da177e4SLinus Torvalds 
445655e957d8STakashi Iwai static int hdsp_9632_get_peak(struct hdsp *hdsp, struct hdsp_peak_rms __user *peak_rms)
44571da177e4SLinus Torvalds {
44581da177e4SLinus Torvalds 	int i, j;
445955e957d8STakashi Iwai 	struct hdsp_9632_meters __iomem *m;
44601da177e4SLinus Torvalds 	int doublespeed = 0;
44611da177e4SLinus Torvalds 
44621da177e4SLinus Torvalds 	if (hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DoubleSpeedStatus)
44631da177e4SLinus Torvalds 		doublespeed = 1;
446455e957d8STakashi Iwai 	m = (struct hdsp_9632_meters __iomem *)(hdsp->iobase+HDSP_9632_metersBase);
44651da177e4SLinus Torvalds 	for (i = 0, j = 0; i < 16; ++i, ++j) {
44661da177e4SLinus Torvalds 		if (copy_u32_le(&peak_rms->input_peaks[i], &m->input_peak[j]))
44671da177e4SLinus Torvalds 			return -EFAULT;
44681da177e4SLinus Torvalds 		if (copy_u32_le(&peak_rms->playback_peaks[i], &m->playback_peak[j]))
44691da177e4SLinus Torvalds 			return -EFAULT;
44701da177e4SLinus Torvalds 		if (copy_u32_le(&peak_rms->output_peaks[i], &m->output_peak[j]))
44711da177e4SLinus Torvalds 			return -EFAULT;
44721da177e4SLinus Torvalds 		if (copy_u64_le(&peak_rms->input_rms[i], &m->input_rms_low[j],
44731da177e4SLinus Torvalds 				&m->input_rms_high[j]))
44741da177e4SLinus Torvalds 			return -EFAULT;
44751da177e4SLinus Torvalds 		if (copy_u64_le(&peak_rms->playback_rms[i], &m->playback_rms_low[j],
44761da177e4SLinus Torvalds 				&m->playback_rms_high[j]))
44771da177e4SLinus Torvalds 			return -EFAULT;
44781da177e4SLinus Torvalds 		if (copy_u64_le(&peak_rms->output_rms[i], &m->output_rms_low[j],
44791da177e4SLinus Torvalds 				&m->output_rms_high[j]))
44801da177e4SLinus Torvalds 			return -EFAULT;
44811da177e4SLinus Torvalds 		if (doublespeed && i == 3) i += 4;
44821da177e4SLinus Torvalds 	}
44831da177e4SLinus Torvalds 	return 0;
44841da177e4SLinus Torvalds }
44851da177e4SLinus Torvalds 
448655e957d8STakashi Iwai static int hdsp_get_peak(struct hdsp *hdsp, struct hdsp_peak_rms __user *peak_rms)
44871da177e4SLinus Torvalds {
44881da177e4SLinus Torvalds 	int i;
44891da177e4SLinus Torvalds 
44901da177e4SLinus Torvalds 	for (i = 0; i < 26; i++) {
44911da177e4SLinus Torvalds 		if (copy_u32_le(&peak_rms->playback_peaks[i],
44921da177e4SLinus Torvalds 				hdsp->iobase + HDSP_playbackPeakLevel + i * 4))
44931da177e4SLinus Torvalds 			return -EFAULT;
44941da177e4SLinus Torvalds 		if (copy_u32_le(&peak_rms->input_peaks[i],
44951da177e4SLinus Torvalds 				hdsp->iobase + HDSP_inputPeakLevel + i * 4))
44961da177e4SLinus Torvalds 			return -EFAULT;
44971da177e4SLinus Torvalds 	}
44981da177e4SLinus Torvalds 	for (i = 0; i < 28; i++) {
44991da177e4SLinus Torvalds 		if (copy_u32_le(&peak_rms->output_peaks[i],
45001da177e4SLinus Torvalds 				hdsp->iobase + HDSP_outputPeakLevel + i * 4))
45011da177e4SLinus Torvalds 			return -EFAULT;
45021da177e4SLinus Torvalds 	}
45031da177e4SLinus Torvalds 	for (i = 0; i < 26; ++i) {
45041da177e4SLinus Torvalds 		if (copy_u64_le(&peak_rms->playback_rms[i],
45051da177e4SLinus Torvalds 				hdsp->iobase + HDSP_playbackRmsLevel + i * 8 + 4,
45061da177e4SLinus Torvalds 				hdsp->iobase + HDSP_playbackRmsLevel + i * 8))
45071da177e4SLinus Torvalds 			return -EFAULT;
45081da177e4SLinus Torvalds 		if (copy_u64_le(&peak_rms->input_rms[i],
45091da177e4SLinus Torvalds 				hdsp->iobase + HDSP_inputRmsLevel + i * 8 + 4,
45101da177e4SLinus Torvalds 				hdsp->iobase + HDSP_inputRmsLevel + i * 8))
45111da177e4SLinus Torvalds 			return -EFAULT;
45121da177e4SLinus Torvalds 	}
45131da177e4SLinus Torvalds 	return 0;
45141da177e4SLinus Torvalds }
45151da177e4SLinus Torvalds 
451655e957d8STakashi Iwai static int snd_hdsp_hwdep_ioctl(struct snd_hwdep *hw, struct file *file, unsigned int cmd, unsigned long arg)
45171da177e4SLinus Torvalds {
451855e957d8STakashi Iwai 	struct hdsp *hdsp = (struct hdsp *)hw->private_data;
45191da177e4SLinus Torvalds 	void __user *argp = (void __user *)arg;
45201da177e4SLinus Torvalds 
45211da177e4SLinus Torvalds 	switch (cmd) {
45221da177e4SLinus Torvalds 	case SNDRV_HDSP_IOCTL_GET_PEAK_RMS: {
452355e957d8STakashi Iwai 		struct hdsp_peak_rms __user *peak_rms = (struct hdsp_peak_rms __user *)arg;
45241da177e4SLinus Torvalds 
45251da177e4SLinus Torvalds 		if (!(hdsp->state & HDSP_FirmwareLoaded)) {
45261da177e4SLinus Torvalds 			snd_printk(KERN_ERR "Hammerfall-DSP: firmware needs to be uploaded to the card.\n");
45271da177e4SLinus Torvalds 			return -EINVAL;
45281da177e4SLinus Torvalds 		}
45291da177e4SLinus Torvalds 
45301da177e4SLinus Torvalds 		switch (hdsp->io_type) {
45311da177e4SLinus Torvalds 		case H9652:
45321da177e4SLinus Torvalds 			return hdsp_9652_get_peak(hdsp, peak_rms);
45331da177e4SLinus Torvalds 		case H9632:
45341da177e4SLinus Torvalds 			return hdsp_9632_get_peak(hdsp, peak_rms);
45351da177e4SLinus Torvalds 		default:
45361da177e4SLinus Torvalds 			return hdsp_get_peak(hdsp, peak_rms);
45371da177e4SLinus Torvalds 		}
45381da177e4SLinus Torvalds 	}
45391da177e4SLinus Torvalds 	case SNDRV_HDSP_IOCTL_GET_CONFIG_INFO: {
454055e957d8STakashi Iwai 		struct hdsp_config_info info;
45411da177e4SLinus Torvalds 		unsigned long flags;
45421da177e4SLinus Torvalds 		int i;
45431da177e4SLinus Torvalds 
45441da177e4SLinus Torvalds 		if (!(hdsp->state & HDSP_FirmwareLoaded)) {
4545b0b98119STakashi Iwai 			snd_printk(KERN_ERR "Hammerfall-DSP: Firmware needs to be uploaded to the card.\n");
45461da177e4SLinus Torvalds 			return -EINVAL;
45471da177e4SLinus Torvalds 		}
45481da177e4SLinus Torvalds 		spin_lock_irqsave(&hdsp->lock, flags);
45491da177e4SLinus Torvalds 		info.pref_sync_ref = (unsigned char)hdsp_pref_sync_ref(hdsp);
45501da177e4SLinus Torvalds 		info.wordclock_sync_check = (unsigned char)hdsp_wc_sync_check(hdsp);
4551b0b98119STakashi Iwai 		if (hdsp->io_type != H9632)
45521da177e4SLinus Torvalds 		    info.adatsync_sync_check = (unsigned char)hdsp_adatsync_sync_check(hdsp);
45531da177e4SLinus Torvalds 		info.spdif_sync_check = (unsigned char)hdsp_spdif_sync_check(hdsp);
4554b0b98119STakashi Iwai 		for (i = 0; i < ((hdsp->io_type != Multiface && hdsp->io_type != H9632) ? 3 : 1); ++i)
45551da177e4SLinus Torvalds 			info.adat_sync_check[i] = (unsigned char)hdsp_adat_sync_check(hdsp, i);
45561da177e4SLinus Torvalds 		info.spdif_in = (unsigned char)hdsp_spdif_in(hdsp);
45571da177e4SLinus Torvalds 		info.spdif_out = (unsigned char)hdsp_spdif_out(hdsp);
45581da177e4SLinus Torvalds 		info.spdif_professional = (unsigned char)hdsp_spdif_professional(hdsp);
45591da177e4SLinus Torvalds 		info.spdif_emphasis = (unsigned char)hdsp_spdif_emphasis(hdsp);
45601da177e4SLinus Torvalds 		info.spdif_nonaudio = (unsigned char)hdsp_spdif_nonaudio(hdsp);
45611da177e4SLinus Torvalds 		info.spdif_sample_rate = hdsp_spdif_sample_rate(hdsp);
45621da177e4SLinus Torvalds 		info.system_sample_rate = hdsp->system_sample_rate;
45631da177e4SLinus Torvalds 		info.autosync_sample_rate = hdsp_external_sample_rate(hdsp);
45641da177e4SLinus Torvalds 		info.system_clock_mode = (unsigned char)hdsp_system_clock_mode(hdsp);
45651da177e4SLinus Torvalds 		info.clock_source = (unsigned char)hdsp_clock_source(hdsp);
45661da177e4SLinus Torvalds 		info.autosync_ref = (unsigned char)hdsp_autosync_ref(hdsp);
45671da177e4SLinus Torvalds 		info.line_out = (unsigned char)hdsp_line_out(hdsp);
45681da177e4SLinus Torvalds 		if (hdsp->io_type == H9632) {
45691da177e4SLinus Torvalds 			info.da_gain = (unsigned char)hdsp_da_gain(hdsp);
45701da177e4SLinus Torvalds 			info.ad_gain = (unsigned char)hdsp_ad_gain(hdsp);
45711da177e4SLinus Torvalds 			info.phone_gain = (unsigned char)hdsp_phone_gain(hdsp);
45721da177e4SLinus Torvalds 			info.xlr_breakout_cable = (unsigned char)hdsp_xlr_breakout_cable(hdsp);
45731da177e4SLinus Torvalds 
45741da177e4SLinus Torvalds 		}
4575b0b98119STakashi Iwai 		if (hdsp->io_type == H9632 || hdsp->io_type == H9652)
45761da177e4SLinus Torvalds 			info.analog_extension_board = (unsigned char)hdsp_aeb(hdsp);
45771da177e4SLinus Torvalds 		spin_unlock_irqrestore(&hdsp->lock, flags);
45781da177e4SLinus Torvalds 		if (copy_to_user(argp, &info, sizeof(info)))
45791da177e4SLinus Torvalds 			return -EFAULT;
45801da177e4SLinus Torvalds 		break;
45811da177e4SLinus Torvalds 	}
45821da177e4SLinus Torvalds 	case SNDRV_HDSP_IOCTL_GET_9632_AEB: {
458355e957d8STakashi Iwai 		struct hdsp_9632_aeb h9632_aeb;
45841da177e4SLinus Torvalds 
45851da177e4SLinus Torvalds 		if (hdsp->io_type != H9632) return -EINVAL;
45861da177e4SLinus Torvalds 		h9632_aeb.aebi = hdsp->ss_in_channels - H9632_SS_CHANNELS;
45871da177e4SLinus Torvalds 		h9632_aeb.aebo = hdsp->ss_out_channels - H9632_SS_CHANNELS;
45881da177e4SLinus Torvalds 		if (copy_to_user(argp, &h9632_aeb, sizeof(h9632_aeb)))
45891da177e4SLinus Torvalds 			return -EFAULT;
45901da177e4SLinus Torvalds 		break;
45911da177e4SLinus Torvalds 	}
45921da177e4SLinus Torvalds 	case SNDRV_HDSP_IOCTL_GET_VERSION: {
459355e957d8STakashi Iwai 		struct hdsp_version hdsp_version;
45941da177e4SLinus Torvalds 		int err;
45951da177e4SLinus Torvalds 
45961da177e4SLinus Torvalds 		if (hdsp->io_type == H9652 || hdsp->io_type == H9632) return -EINVAL;
45971da177e4SLinus Torvalds 		if (hdsp->io_type == Undefined) {
4598b0b98119STakashi Iwai 			if ((err = hdsp_get_iobox_version(hdsp)) < 0)
45991da177e4SLinus Torvalds 				return err;
46001da177e4SLinus Torvalds 		}
46011da177e4SLinus Torvalds 		hdsp_version.io_type = hdsp->io_type;
46021da177e4SLinus Torvalds 		hdsp_version.firmware_rev = hdsp->firmware_rev;
4603b0b98119STakashi Iwai 		if ((err = copy_to_user(argp, &hdsp_version, sizeof(hdsp_version))))
46041da177e4SLinus Torvalds 		    	return -EFAULT;
46051da177e4SLinus Torvalds 		break;
46061da177e4SLinus Torvalds 	}
46071da177e4SLinus Torvalds 	case SNDRV_HDSP_IOCTL_UPLOAD_FIRMWARE: {
460855e957d8STakashi Iwai 		struct hdsp_firmware __user *firmware;
46091da177e4SLinus Torvalds 		u32 __user *firmware_data;
46101da177e4SLinus Torvalds 		int err;
46111da177e4SLinus Torvalds 
46121da177e4SLinus Torvalds 		if (hdsp->io_type == H9652 || hdsp->io_type == H9632) return -EINVAL;
46131da177e4SLinus Torvalds 		/* SNDRV_HDSP_IOCTL_GET_VERSION must have been called */
46141da177e4SLinus Torvalds 		if (hdsp->io_type == Undefined) return -EINVAL;
46151da177e4SLinus Torvalds 
46161da177e4SLinus Torvalds 		if (hdsp->state & (HDSP_FirmwareCached | HDSP_FirmwareLoaded))
46171da177e4SLinus Torvalds 			return -EBUSY;
46181da177e4SLinus Torvalds 
4619b0b98119STakashi Iwai 		snd_printk(KERN_INFO "Hammerfall-DSP: initializing firmware upload\n");
462055e957d8STakashi Iwai 		firmware = (struct hdsp_firmware __user *)argp;
46211da177e4SLinus Torvalds 
4622b0b98119STakashi Iwai 		if (get_user(firmware_data, &firmware->firmware_data))
46231da177e4SLinus Torvalds 			return -EFAULT;
46241da177e4SLinus Torvalds 
4625b0b98119STakashi Iwai 		if (hdsp_check_for_iobox (hdsp))
46261da177e4SLinus Torvalds 			return -EIO;
46271da177e4SLinus Torvalds 
4628b0b98119STakashi Iwai 		if (copy_from_user(hdsp->firmware_cache, firmware_data, sizeof(hdsp->firmware_cache)) != 0)
46291da177e4SLinus Torvalds 			return -EFAULT;
46301da177e4SLinus Torvalds 
46311da177e4SLinus Torvalds 		hdsp->state |= HDSP_FirmwareCached;
46321da177e4SLinus Torvalds 
4633b0b98119STakashi Iwai 		if ((err = snd_hdsp_load_firmware_from_cache(hdsp)) < 0)
46341da177e4SLinus Torvalds 			return err;
46351da177e4SLinus Torvalds 
46361da177e4SLinus Torvalds 		if (!(hdsp->state & HDSP_InitializationComplete)) {
4637b0b98119STakashi Iwai 			if ((err = snd_hdsp_enable_io(hdsp)) < 0)
46381da177e4SLinus Torvalds 				return err;
46391da177e4SLinus Torvalds 
46401da177e4SLinus Torvalds 			snd_hdsp_initialize_channels(hdsp);
46411da177e4SLinus Torvalds 			snd_hdsp_initialize_midi_flush(hdsp);
46421da177e4SLinus Torvalds 
46431da177e4SLinus Torvalds 			if ((err = snd_hdsp_create_alsa_devices(hdsp->card, hdsp)) < 0) {
4644b0b98119STakashi Iwai 				snd_printk(KERN_ERR "Hammerfall-DSP: error creating alsa devices\n");
46451da177e4SLinus Torvalds 				return err;
46461da177e4SLinus Torvalds 			}
46471da177e4SLinus Torvalds 		}
46481da177e4SLinus Torvalds 		break;
46491da177e4SLinus Torvalds 	}
46501da177e4SLinus Torvalds 	case SNDRV_HDSP_IOCTL_GET_MIXER: {
465155e957d8STakashi Iwai 		struct hdsp_mixer __user *mixer = (struct hdsp_mixer __user *)argp;
46521da177e4SLinus Torvalds 		if (copy_to_user(mixer->matrix, hdsp->mixer_matrix, sizeof(unsigned short)*HDSP_MATRIX_MIXER_SIZE))
46531da177e4SLinus Torvalds 			return -EFAULT;
46541da177e4SLinus Torvalds 		break;
46551da177e4SLinus Torvalds 	}
46561da177e4SLinus Torvalds 	default:
46571da177e4SLinus Torvalds 		return -EINVAL;
46581da177e4SLinus Torvalds 	}
46591da177e4SLinus Torvalds 	return 0;
46601da177e4SLinus Torvalds }
46611da177e4SLinus Torvalds 
466255e957d8STakashi Iwai static struct snd_pcm_ops snd_hdsp_playback_ops = {
46631da177e4SLinus Torvalds 	.open =		snd_hdsp_playback_open,
46641da177e4SLinus Torvalds 	.close =	snd_hdsp_playback_release,
46651da177e4SLinus Torvalds 	.ioctl =	snd_hdsp_ioctl,
46661da177e4SLinus Torvalds 	.hw_params =	snd_hdsp_hw_params,
46671da177e4SLinus Torvalds 	.prepare =	snd_hdsp_prepare,
46681da177e4SLinus Torvalds 	.trigger =	snd_hdsp_trigger,
46691da177e4SLinus Torvalds 	.pointer =	snd_hdsp_hw_pointer,
46701da177e4SLinus Torvalds 	.copy =		snd_hdsp_playback_copy,
46711da177e4SLinus Torvalds 	.silence =	snd_hdsp_hw_silence,
46721da177e4SLinus Torvalds };
46731da177e4SLinus Torvalds 
467455e957d8STakashi Iwai static struct snd_pcm_ops snd_hdsp_capture_ops = {
46751da177e4SLinus Torvalds 	.open =		snd_hdsp_capture_open,
46761da177e4SLinus Torvalds 	.close =	snd_hdsp_capture_release,
46771da177e4SLinus Torvalds 	.ioctl =	snd_hdsp_ioctl,
46781da177e4SLinus Torvalds 	.hw_params =	snd_hdsp_hw_params,
46791da177e4SLinus Torvalds 	.prepare =	snd_hdsp_prepare,
46801da177e4SLinus Torvalds 	.trigger =	snd_hdsp_trigger,
46811da177e4SLinus Torvalds 	.pointer =	snd_hdsp_hw_pointer,
46821da177e4SLinus Torvalds 	.copy =		snd_hdsp_capture_copy,
46831da177e4SLinus Torvalds };
46841da177e4SLinus Torvalds 
468555e957d8STakashi Iwai static int __devinit snd_hdsp_create_hwdep(struct snd_card *card,
468655e957d8STakashi Iwai 					   struct hdsp *hdsp)
46871da177e4SLinus Torvalds {
468855e957d8STakashi Iwai 	struct snd_hwdep *hw;
46891da177e4SLinus Torvalds 	int err;
46901da177e4SLinus Torvalds 
46911da177e4SLinus Torvalds 	if ((err = snd_hwdep_new(card, "HDSP hwdep", 0, &hw)) < 0)
46921da177e4SLinus Torvalds 		return err;
46931da177e4SLinus Torvalds 
46941da177e4SLinus Torvalds 	hdsp->hwdep = hw;
46951da177e4SLinus Torvalds 	hw->private_data = hdsp;
46961da177e4SLinus Torvalds 	strcpy(hw->name, "HDSP hwdep interface");
46971da177e4SLinus Torvalds 
46981da177e4SLinus Torvalds 	hw->ops.open = snd_hdsp_hwdep_dummy_op;
46991da177e4SLinus Torvalds 	hw->ops.ioctl = snd_hdsp_hwdep_ioctl;
47001da177e4SLinus Torvalds 	hw->ops.release = snd_hdsp_hwdep_dummy_op;
47011da177e4SLinus Torvalds 
47021da177e4SLinus Torvalds 	return 0;
47031da177e4SLinus Torvalds }
47041da177e4SLinus Torvalds 
470555e957d8STakashi Iwai static int snd_hdsp_create_pcm(struct snd_card *card, struct hdsp *hdsp)
47061da177e4SLinus Torvalds {
470755e957d8STakashi Iwai 	struct snd_pcm *pcm;
47081da177e4SLinus Torvalds 	int err;
47091da177e4SLinus Torvalds 
47101da177e4SLinus Torvalds 	if ((err = snd_pcm_new(card, hdsp->card_name, 0, 1, 1, &pcm)) < 0)
47111da177e4SLinus Torvalds 		return err;
47121da177e4SLinus Torvalds 
47131da177e4SLinus Torvalds 	hdsp->pcm = pcm;
47141da177e4SLinus Torvalds 	pcm->private_data = hdsp;
47151da177e4SLinus Torvalds 	strcpy(pcm->name, hdsp->card_name);
47161da177e4SLinus Torvalds 
47171da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_hdsp_playback_ops);
47181da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_hdsp_capture_ops);
47191da177e4SLinus Torvalds 
47201da177e4SLinus Torvalds 	pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
47211da177e4SLinus Torvalds 
47221da177e4SLinus Torvalds 	return 0;
47231da177e4SLinus Torvalds }
47241da177e4SLinus Torvalds 
472555e957d8STakashi Iwai static void snd_hdsp_9652_enable_mixer (struct hdsp *hdsp)
47261da177e4SLinus Torvalds {
47271da177e4SLinus Torvalds         hdsp->control2_register |= HDSP_9652_ENABLE_MIXER;
47281da177e4SLinus Torvalds 	hdsp_write (hdsp, HDSP_control2Reg, hdsp->control2_register);
47291da177e4SLinus Torvalds }
47301da177e4SLinus Torvalds 
473155e957d8STakashi Iwai static int snd_hdsp_enable_io (struct hdsp *hdsp)
47321da177e4SLinus Torvalds {
47331da177e4SLinus Torvalds 	int i;
47341da177e4SLinus Torvalds 
47351da177e4SLinus Torvalds 	if (hdsp_fifo_wait (hdsp, 0, 100)) {
4736b0b98119STakashi Iwai 		snd_printk(KERN_ERR "Hammerfall-DSP: enable_io fifo_wait failed\n");
47371da177e4SLinus Torvalds 		return -EIO;
47381da177e4SLinus Torvalds 	}
47391da177e4SLinus Torvalds 
47401da177e4SLinus Torvalds 	for (i = 0; i < hdsp->max_channels; ++i) {
47411da177e4SLinus Torvalds 		hdsp_write (hdsp, HDSP_inputEnable + (4 * i), 1);
47421da177e4SLinus Torvalds 		hdsp_write (hdsp, HDSP_outputEnable + (4 * i), 1);
47431da177e4SLinus Torvalds 	}
47441da177e4SLinus Torvalds 
47451da177e4SLinus Torvalds 	return 0;
47461da177e4SLinus Torvalds }
47471da177e4SLinus Torvalds 
474855e957d8STakashi Iwai static void snd_hdsp_initialize_channels(struct hdsp *hdsp)
47491da177e4SLinus Torvalds {
47501da177e4SLinus Torvalds 	int status, aebi_channels, aebo_channels;
47511da177e4SLinus Torvalds 
47521da177e4SLinus Torvalds 	switch (hdsp->io_type) {
47531da177e4SLinus Torvalds 	case Digiface:
47541da177e4SLinus Torvalds 		hdsp->card_name = "RME Hammerfall DSP + Digiface";
47551da177e4SLinus Torvalds 		hdsp->ss_in_channels = hdsp->ss_out_channels = DIGIFACE_SS_CHANNELS;
47561da177e4SLinus Torvalds 		hdsp->ds_in_channels = hdsp->ds_out_channels = DIGIFACE_DS_CHANNELS;
47571da177e4SLinus Torvalds 		break;
47581da177e4SLinus Torvalds 
47591da177e4SLinus Torvalds 	case H9652:
47601da177e4SLinus Torvalds 		hdsp->card_name = "RME Hammerfall HDSP 9652";
47611da177e4SLinus Torvalds 		hdsp->ss_in_channels = hdsp->ss_out_channels = H9652_SS_CHANNELS;
47621da177e4SLinus Torvalds 		hdsp->ds_in_channels = hdsp->ds_out_channels = H9652_DS_CHANNELS;
47631da177e4SLinus Torvalds 		break;
47641da177e4SLinus Torvalds 
47651da177e4SLinus Torvalds 	case H9632:
47661da177e4SLinus Torvalds 		status = hdsp_read(hdsp, HDSP_statusRegister);
47671da177e4SLinus Torvalds 		/* HDSP_AEBx bits are low when AEB are connected */
47681da177e4SLinus Torvalds 		aebi_channels = (status & HDSP_AEBI) ? 0 : 4;
47691da177e4SLinus Torvalds 		aebo_channels = (status & HDSP_AEBO) ? 0 : 4;
47701da177e4SLinus Torvalds 		hdsp->card_name = "RME Hammerfall HDSP 9632";
47711da177e4SLinus Torvalds 		hdsp->ss_in_channels = H9632_SS_CHANNELS+aebi_channels;
47721da177e4SLinus Torvalds 		hdsp->ds_in_channels = H9632_DS_CHANNELS+aebi_channels;
47731da177e4SLinus Torvalds 		hdsp->qs_in_channels = H9632_QS_CHANNELS+aebi_channels;
47741da177e4SLinus Torvalds 		hdsp->ss_out_channels = H9632_SS_CHANNELS+aebo_channels;
47751da177e4SLinus Torvalds 		hdsp->ds_out_channels = H9632_DS_CHANNELS+aebo_channels;
47761da177e4SLinus Torvalds 		hdsp->qs_out_channels = H9632_QS_CHANNELS+aebo_channels;
47771da177e4SLinus Torvalds 		break;
47781da177e4SLinus Torvalds 
47791da177e4SLinus Torvalds 	case Multiface:
47801da177e4SLinus Torvalds 		hdsp->card_name = "RME Hammerfall DSP + Multiface";
47811da177e4SLinus Torvalds 		hdsp->ss_in_channels = hdsp->ss_out_channels = MULTIFACE_SS_CHANNELS;
47821da177e4SLinus Torvalds 		hdsp->ds_in_channels = hdsp->ds_out_channels = MULTIFACE_DS_CHANNELS;
47831da177e4SLinus Torvalds 		break;
47841da177e4SLinus Torvalds 
47851da177e4SLinus Torvalds 	default:
47861da177e4SLinus Torvalds  		/* should never get here */
47871da177e4SLinus Torvalds 		break;
47881da177e4SLinus Torvalds 	}
47891da177e4SLinus Torvalds }
47901da177e4SLinus Torvalds 
479155e957d8STakashi Iwai static void snd_hdsp_initialize_midi_flush (struct hdsp *hdsp)
47921da177e4SLinus Torvalds {
47931da177e4SLinus Torvalds 	snd_hdsp_flush_midi_input (hdsp, 0);
47941da177e4SLinus Torvalds 	snd_hdsp_flush_midi_input (hdsp, 1);
47951da177e4SLinus Torvalds }
47961da177e4SLinus Torvalds 
479755e957d8STakashi Iwai static int snd_hdsp_create_alsa_devices(struct snd_card *card, struct hdsp *hdsp)
47981da177e4SLinus Torvalds {
47991da177e4SLinus Torvalds 	int err;
48001da177e4SLinus Torvalds 
48011da177e4SLinus Torvalds 	if ((err = snd_hdsp_create_pcm(card, hdsp)) < 0) {
4802b0b98119STakashi Iwai 		snd_printk(KERN_ERR "Hammerfall-DSP: Error creating pcm interface\n");
48031da177e4SLinus Torvalds 		return err;
48041da177e4SLinus Torvalds 	}
48051da177e4SLinus Torvalds 
48061da177e4SLinus Torvalds 
48071da177e4SLinus Torvalds 	if ((err = snd_hdsp_create_midi(card, hdsp, 0)) < 0) {
4808b0b98119STakashi Iwai 		snd_printk(KERN_ERR "Hammerfall-DSP: Error creating first midi interface\n");
48091da177e4SLinus Torvalds 		return err;
48101da177e4SLinus Torvalds 	}
48111da177e4SLinus Torvalds 
48121da177e4SLinus Torvalds 	if (hdsp->io_type == Digiface || hdsp->io_type == H9652) {
48131da177e4SLinus Torvalds 		if ((err = snd_hdsp_create_midi(card, hdsp, 1)) < 0) {
4814b0b98119STakashi Iwai 			snd_printk(KERN_ERR "Hammerfall-DSP: Error creating second midi interface\n");
48151da177e4SLinus Torvalds 			return err;
48161da177e4SLinus Torvalds 		}
48171da177e4SLinus Torvalds 	}
48181da177e4SLinus Torvalds 
48191da177e4SLinus Torvalds 	if ((err = snd_hdsp_create_controls(card, hdsp)) < 0) {
4820b0b98119STakashi Iwai 		snd_printk(KERN_ERR "Hammerfall-DSP: Error creating ctl interface\n");
48211da177e4SLinus Torvalds 		return err;
48221da177e4SLinus Torvalds 	}
48231da177e4SLinus Torvalds 
48241da177e4SLinus Torvalds 	snd_hdsp_proc_init(hdsp);
48251da177e4SLinus Torvalds 
48261da177e4SLinus Torvalds 	hdsp->system_sample_rate = -1;
48271da177e4SLinus Torvalds 	hdsp->playback_pid = -1;
48281da177e4SLinus Torvalds 	hdsp->capture_pid = -1;
48291da177e4SLinus Torvalds 	hdsp->capture_substream = NULL;
48301da177e4SLinus Torvalds 	hdsp->playback_substream = NULL;
48311da177e4SLinus Torvalds 
48321da177e4SLinus Torvalds 	if ((err = snd_hdsp_set_defaults(hdsp)) < 0) {
4833b0b98119STakashi Iwai 		snd_printk(KERN_ERR "Hammerfall-DSP: Error setting default values\n");
48341da177e4SLinus Torvalds 		return err;
48351da177e4SLinus Torvalds 	}
48361da177e4SLinus Torvalds 
48371da177e4SLinus Torvalds 	if (!(hdsp->state & HDSP_InitializationComplete)) {
4838b73c1c12SClemens Ladisch 		strcpy(card->shortname, "Hammerfall DSP");
48391da177e4SLinus Torvalds 		sprintf(card->longname, "%s at 0x%lx, irq %d", hdsp->card_name,
48401da177e4SLinus Torvalds 			hdsp->port, hdsp->irq);
48411da177e4SLinus Torvalds 
48421da177e4SLinus Torvalds 		if ((err = snd_card_register(card)) < 0) {
4843b0b98119STakashi Iwai 			snd_printk(KERN_ERR "Hammerfall-DSP: error registering card\n");
48441da177e4SLinus Torvalds 			return err;
48451da177e4SLinus Torvalds 		}
48461da177e4SLinus Torvalds 		hdsp->state |= HDSP_InitializationComplete;
48471da177e4SLinus Torvalds 	}
48481da177e4SLinus Torvalds 
48491da177e4SLinus Torvalds 	return 0;
48501da177e4SLinus Torvalds }
48511da177e4SLinus Torvalds 
48521da177e4SLinus Torvalds #ifdef HDSP_FW_LOADER
48531da177e4SLinus Torvalds /* load firmware via hotplug fw loader */
485455e957d8STakashi Iwai static int __devinit hdsp_request_fw_loader(struct hdsp *hdsp)
48551da177e4SLinus Torvalds {
48561da177e4SLinus Torvalds 	const char *fwfile;
48571da177e4SLinus Torvalds 	const struct firmware *fw;
48581da177e4SLinus Torvalds 	int err;
48591da177e4SLinus Torvalds 
48601da177e4SLinus Torvalds 	if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
48611da177e4SLinus Torvalds 		return 0;
48621da177e4SLinus Torvalds 	if (hdsp->io_type == Undefined) {
48631da177e4SLinus Torvalds 		if ((err = hdsp_get_iobox_version(hdsp)) < 0)
48641da177e4SLinus Torvalds 			return err;
48651da177e4SLinus Torvalds 		if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
48661da177e4SLinus Torvalds 			return 0;
48671da177e4SLinus Torvalds 	}
48681da177e4SLinus Torvalds 
48691da177e4SLinus Torvalds 	/* caution: max length of firmware filename is 30! */
48701da177e4SLinus Torvalds 	switch (hdsp->io_type) {
48711da177e4SLinus Torvalds 	case Multiface:
48721da177e4SLinus Torvalds 		if (hdsp->firmware_rev == 0xa)
48731da177e4SLinus Torvalds 			fwfile = "multiface_firmware.bin";
48741da177e4SLinus Torvalds 		else
48751da177e4SLinus Torvalds 			fwfile = "multiface_firmware_rev11.bin";
48761da177e4SLinus Torvalds 		break;
48771da177e4SLinus Torvalds 	case Digiface:
48781da177e4SLinus Torvalds 		if (hdsp->firmware_rev == 0xa)
48791da177e4SLinus Torvalds 			fwfile = "digiface_firmware.bin";
48801da177e4SLinus Torvalds 		else
48811da177e4SLinus Torvalds 			fwfile = "digiface_firmware_rev11.bin";
48821da177e4SLinus Torvalds 		break;
48831da177e4SLinus Torvalds 	default:
48841da177e4SLinus Torvalds 		snd_printk(KERN_ERR "Hammerfall-DSP: invalid io_type %d\n", hdsp->io_type);
48851da177e4SLinus Torvalds 		return -EINVAL;
48861da177e4SLinus Torvalds 	}
48871da177e4SLinus Torvalds 
48881da177e4SLinus Torvalds 	if (request_firmware(&fw, fwfile, &hdsp->pci->dev)) {
48891da177e4SLinus Torvalds 		snd_printk(KERN_ERR "Hammerfall-DSP: cannot load firmware %s\n", fwfile);
48901da177e4SLinus Torvalds 		return -ENOENT;
48911da177e4SLinus Torvalds 	}
48921da177e4SLinus Torvalds 	if (fw->size < sizeof(hdsp->firmware_cache)) {
48931da177e4SLinus Torvalds 		snd_printk(KERN_ERR "Hammerfall-DSP: too short firmware size %d (expected %d)\n",
48941da177e4SLinus Torvalds 			   (int)fw->size, (int)sizeof(hdsp->firmware_cache));
48951da177e4SLinus Torvalds 		release_firmware(fw);
48961da177e4SLinus Torvalds 		return -EINVAL;
48971da177e4SLinus Torvalds 	}
48987679a030SThomas Charbonnel 
48991da177e4SLinus Torvalds 	memcpy(hdsp->firmware_cache, fw->data, sizeof(hdsp->firmware_cache));
49007679a030SThomas Charbonnel 
49011da177e4SLinus Torvalds 	release_firmware(fw);
49021da177e4SLinus Torvalds 
49031da177e4SLinus Torvalds 	hdsp->state |= HDSP_FirmwareCached;
49041da177e4SLinus Torvalds 
49051da177e4SLinus Torvalds 	if ((err = snd_hdsp_load_firmware_from_cache(hdsp)) < 0)
49061da177e4SLinus Torvalds 		return err;
49071da177e4SLinus Torvalds 
49081da177e4SLinus Torvalds 	if (!(hdsp->state & HDSP_InitializationComplete)) {
4909b0b98119STakashi Iwai 		if ((err = snd_hdsp_enable_io(hdsp)) < 0)
49101da177e4SLinus Torvalds 			return err;
49111da177e4SLinus Torvalds 
49121da177e4SLinus Torvalds 		if ((err = snd_hdsp_create_hwdep(hdsp->card, hdsp)) < 0) {
4913b0b98119STakashi Iwai 			snd_printk(KERN_ERR "Hammerfall-DSP: error creating hwdep device\n");
49141da177e4SLinus Torvalds 			return err;
49151da177e4SLinus Torvalds 		}
49161da177e4SLinus Torvalds 		snd_hdsp_initialize_channels(hdsp);
49171da177e4SLinus Torvalds 		snd_hdsp_initialize_midi_flush(hdsp);
49181da177e4SLinus Torvalds 		if ((err = snd_hdsp_create_alsa_devices(hdsp->card, hdsp)) < 0) {
4919b0b98119STakashi Iwai 			snd_printk(KERN_ERR "Hammerfall-DSP: error creating alsa devices\n");
49201da177e4SLinus Torvalds 			return err;
49211da177e4SLinus Torvalds 		}
49221da177e4SLinus Torvalds 	}
49231da177e4SLinus Torvalds 	return 0;
49241da177e4SLinus Torvalds }
49251da177e4SLinus Torvalds #endif
49261da177e4SLinus Torvalds 
492755e957d8STakashi Iwai static int __devinit snd_hdsp_create(struct snd_card *card,
492855e957d8STakashi Iwai 				     struct hdsp *hdsp)
49291da177e4SLinus Torvalds {
49301da177e4SLinus Torvalds 	struct pci_dev *pci = hdsp->pci;
49311da177e4SLinus Torvalds 	int err;
49321da177e4SLinus Torvalds 	int is_9652 = 0;
49331da177e4SLinus Torvalds 	int is_9632 = 0;
49341da177e4SLinus Torvalds 
49351da177e4SLinus Torvalds 	hdsp->irq = -1;
49361da177e4SLinus Torvalds 	hdsp->state = 0;
49371da177e4SLinus Torvalds 	hdsp->midi[0].rmidi = NULL;
49381da177e4SLinus Torvalds 	hdsp->midi[1].rmidi = NULL;
49391da177e4SLinus Torvalds 	hdsp->midi[0].input = NULL;
49401da177e4SLinus Torvalds 	hdsp->midi[1].input = NULL;
49411da177e4SLinus Torvalds 	hdsp->midi[0].output = NULL;
49421da177e4SLinus Torvalds 	hdsp->midi[1].output = NULL;
49431da177e4SLinus Torvalds 	hdsp->midi[0].pending = 0;
49441da177e4SLinus Torvalds 	hdsp->midi[1].pending = 0;
49451da177e4SLinus Torvalds 	spin_lock_init(&hdsp->midi[0].lock);
49461da177e4SLinus Torvalds 	spin_lock_init(&hdsp->midi[1].lock);
49471da177e4SLinus Torvalds 	hdsp->iobase = NULL;
49481da177e4SLinus Torvalds 	hdsp->control_register = 0;
49491da177e4SLinus Torvalds 	hdsp->control2_register = 0;
49501da177e4SLinus Torvalds 	hdsp->io_type = Undefined;
49511da177e4SLinus Torvalds 	hdsp->max_channels = 26;
49521da177e4SLinus Torvalds 
49531da177e4SLinus Torvalds 	hdsp->card = card;
49541da177e4SLinus Torvalds 
49551da177e4SLinus Torvalds 	spin_lock_init(&hdsp->lock);
49561da177e4SLinus Torvalds 
49571da177e4SLinus Torvalds 	tasklet_init(&hdsp->midi_tasklet, hdsp_midi_tasklet, (unsigned long)hdsp);
49581da177e4SLinus Torvalds 
49591da177e4SLinus Torvalds 	pci_read_config_word(hdsp->pci, PCI_CLASS_REVISION, &hdsp->firmware_rev);
49601da177e4SLinus Torvalds 	hdsp->firmware_rev &= 0xff;
49611da177e4SLinus Torvalds 
49621da177e4SLinus Torvalds 	/* From Martin Bjoernsen :
49631da177e4SLinus Torvalds 	    "It is important that the card's latency timer register in
49641da177e4SLinus Torvalds 	    the PCI configuration space is set to a value much larger
49651da177e4SLinus Torvalds 	    than 0 by the computer's BIOS or the driver.
49661da177e4SLinus Torvalds 	    The windows driver always sets this 8 bit register [...]
49671da177e4SLinus Torvalds 	    to its maximum 255 to avoid problems with some computers."
49681da177e4SLinus Torvalds 	*/
49691da177e4SLinus Torvalds 	pci_write_config_byte(hdsp->pci, PCI_LATENCY_TIMER, 0xFF);
49701da177e4SLinus Torvalds 
49711da177e4SLinus Torvalds 	strcpy(card->driver, "H-DSP");
49721da177e4SLinus Torvalds 	strcpy(card->mixername, "Xilinx FPGA");
49731da177e4SLinus Torvalds 
4974b0b98119STakashi Iwai 	if (hdsp->firmware_rev < 0xa)
49751da177e4SLinus Torvalds 		return -ENODEV;
4976b0b98119STakashi Iwai 	else if (hdsp->firmware_rev < 0x64)
49771da177e4SLinus Torvalds 		hdsp->card_name = "RME Hammerfall DSP";
4978b0b98119STakashi Iwai 	else if (hdsp->firmware_rev < 0x96) {
49791da177e4SLinus Torvalds 		hdsp->card_name = "RME HDSP 9652";
49801da177e4SLinus Torvalds 		is_9652 = 1;
49811da177e4SLinus Torvalds 	} else {
49821da177e4SLinus Torvalds 		hdsp->card_name = "RME HDSP 9632";
49831da177e4SLinus Torvalds 		hdsp->max_channels = 16;
49841da177e4SLinus Torvalds 		is_9632 = 1;
49851da177e4SLinus Torvalds 	}
49861da177e4SLinus Torvalds 
4987b0b98119STakashi Iwai 	if ((err = pci_enable_device(pci)) < 0)
49881da177e4SLinus Torvalds 		return err;
49891da177e4SLinus Torvalds 
49901da177e4SLinus Torvalds 	pci_set_master(hdsp->pci);
49911da177e4SLinus Torvalds 
49921da177e4SLinus Torvalds 	if ((err = pci_request_regions(pci, "hdsp")) < 0)
49931da177e4SLinus Torvalds 		return err;
49941da177e4SLinus Torvalds 	hdsp->port = pci_resource_start(pci, 0);
49951da177e4SLinus Torvalds 	if ((hdsp->iobase = ioremap_nocache(hdsp->port, HDSP_IO_EXTENT)) == NULL) {
4996b0b98119STakashi Iwai 		snd_printk(KERN_ERR "Hammerfall-DSP: unable to remap region 0x%lx-0x%lx\n", hdsp->port, hdsp->port + HDSP_IO_EXTENT - 1);
49971da177e4SLinus Torvalds 		return -EBUSY;
49981da177e4SLinus Torvalds 	}
49991da177e4SLinus Torvalds 
5000437a5a46STakashi Iwai 	if (request_irq(pci->irq, snd_hdsp_interrupt, IRQF_SHARED,
5001437a5a46STakashi Iwai 			"hdsp", hdsp)) {
5002b0b98119STakashi Iwai 		snd_printk(KERN_ERR "Hammerfall-DSP: unable to use IRQ %d\n", pci->irq);
50031da177e4SLinus Torvalds 		return -EBUSY;
50041da177e4SLinus Torvalds 	}
50051da177e4SLinus Torvalds 
50061da177e4SLinus Torvalds 	hdsp->irq = pci->irq;
5007176546abSRemy Bruno 	hdsp->precise_ptr = 0;
50081da177e4SLinus Torvalds 	hdsp->use_midi_tasklet = 1;
5009d7923b2aSRemy Bruno 	hdsp->dds_value = 0;
50101da177e4SLinus Torvalds 
5011b0b98119STakashi Iwai 	if ((err = snd_hdsp_initialize_memory(hdsp)) < 0)
50121da177e4SLinus Torvalds 		return err;
50131da177e4SLinus Torvalds 
50141da177e4SLinus Torvalds 	if (!is_9652 && !is_9632) {
50151da177e4SLinus Torvalds 		/* we wait 2 seconds to let freshly inserted cardbus cards do their hardware init */
5016ef21ca24SNishanth Aravamudan 		ssleep(2);
50171da177e4SLinus Torvalds 
50181da177e4SLinus Torvalds 		if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
50191da177e4SLinus Torvalds #ifdef HDSP_FW_LOADER
5020b0b98119STakashi Iwai 			if ((err = hdsp_request_fw_loader(hdsp)) < 0)
50211da177e4SLinus Torvalds 				/* we don't fail as this can happen
50221da177e4SLinus Torvalds 				   if userspace is not ready for
50231da177e4SLinus Torvalds 				   firmware upload
50241da177e4SLinus Torvalds 				*/
5025b0b98119STakashi Iwai 				snd_printk(KERN_ERR "Hammerfall-DSP: couldn't get firmware from userspace. try using hdsploader\n");
5026b0b98119STakashi Iwai 			else
50271da177e4SLinus Torvalds 				/* init is complete, we return */
50281da177e4SLinus Torvalds 				return 0;
50291da177e4SLinus Torvalds #endif
50301da177e4SLinus Torvalds 			/* no iobox connected, we defer initialization */
5031b0b98119STakashi Iwai 			snd_printk(KERN_INFO "Hammerfall-DSP: card initialization pending : waiting for firmware\n");
5032b0b98119STakashi Iwai 			if ((err = snd_hdsp_create_hwdep(card, hdsp)) < 0)
50331da177e4SLinus Torvalds 				return err;
50341da177e4SLinus Torvalds 			return 0;
50351da177e4SLinus Torvalds 		} else {
5036b0b98119STakashi Iwai 			snd_printk(KERN_INFO "Hammerfall-DSP: Firmware already present, initializing card.\n");
5037b0b98119STakashi Iwai 			if (hdsp_read(hdsp, HDSP_status2Register) & HDSP_version1)
50381da177e4SLinus Torvalds 				hdsp->io_type = Multiface;
5039b0b98119STakashi Iwai 			else
50401da177e4SLinus Torvalds 				hdsp->io_type = Digiface;
50411da177e4SLinus Torvalds 		}
50421da177e4SLinus Torvalds 	}
50431da177e4SLinus Torvalds 
5044b0b98119STakashi Iwai 	if ((err = snd_hdsp_enable_io(hdsp)) != 0)
50451da177e4SLinus Torvalds 		return err;
50461da177e4SLinus Torvalds 
5047b0b98119STakashi Iwai 	if (is_9652)
50481da177e4SLinus Torvalds 	        hdsp->io_type = H9652;
50491da177e4SLinus Torvalds 
5050b0b98119STakashi Iwai 	if (is_9632)
50511da177e4SLinus Torvalds 		hdsp->io_type = H9632;
50521da177e4SLinus Torvalds 
5053b0b98119STakashi Iwai 	if ((err = snd_hdsp_create_hwdep(card, hdsp)) < 0)
50541da177e4SLinus Torvalds 		return err;
50551da177e4SLinus Torvalds 
50561da177e4SLinus Torvalds 	snd_hdsp_initialize_channels(hdsp);
50571da177e4SLinus Torvalds 	snd_hdsp_initialize_midi_flush(hdsp);
50581da177e4SLinus Torvalds 
50591da177e4SLinus Torvalds 	hdsp->state |= HDSP_FirmwareLoaded;
50601da177e4SLinus Torvalds 
5061b0b98119STakashi Iwai 	if ((err = snd_hdsp_create_alsa_devices(card, hdsp)) < 0)
50621da177e4SLinus Torvalds 		return err;
50631da177e4SLinus Torvalds 
50641da177e4SLinus Torvalds 	return 0;
50651da177e4SLinus Torvalds }
50661da177e4SLinus Torvalds 
506755e957d8STakashi Iwai static int snd_hdsp_free(struct hdsp *hdsp)
50681da177e4SLinus Torvalds {
50691da177e4SLinus Torvalds 	if (hdsp->port) {
50701da177e4SLinus Torvalds 		/* stop the audio, and cancel all interrupts */
50711da177e4SLinus Torvalds 		tasklet_kill(&hdsp->midi_tasklet);
50721da177e4SLinus Torvalds 		hdsp->control_register &= ~(HDSP_Start|HDSP_AudioInterruptEnable|HDSP_Midi0InterruptEnable|HDSP_Midi1InterruptEnable);
50731da177e4SLinus Torvalds 		hdsp_write (hdsp, HDSP_controlRegister, hdsp->control_register);
50741da177e4SLinus Torvalds 	}
50751da177e4SLinus Torvalds 
50761da177e4SLinus Torvalds 	if (hdsp->irq >= 0)
50771da177e4SLinus Torvalds 		free_irq(hdsp->irq, (void *)hdsp);
50781da177e4SLinus Torvalds 
50791da177e4SLinus Torvalds 	snd_hdsp_free_buffers(hdsp);
50801da177e4SLinus Torvalds 
50811da177e4SLinus Torvalds 	if (hdsp->iobase)
50821da177e4SLinus Torvalds 		iounmap(hdsp->iobase);
50831da177e4SLinus Torvalds 
50841da177e4SLinus Torvalds 	if (hdsp->port)
50851da177e4SLinus Torvalds 		pci_release_regions(hdsp->pci);
50861da177e4SLinus Torvalds 
50871da177e4SLinus Torvalds 	pci_disable_device(hdsp->pci);
50881da177e4SLinus Torvalds 	return 0;
50891da177e4SLinus Torvalds }
50901da177e4SLinus Torvalds 
509155e957d8STakashi Iwai static void snd_hdsp_card_free(struct snd_card *card)
50921da177e4SLinus Torvalds {
509355e957d8STakashi Iwai 	struct hdsp *hdsp = (struct hdsp *) card->private_data;
50941da177e4SLinus Torvalds 
50951da177e4SLinus Torvalds 	if (hdsp)
50961da177e4SLinus Torvalds 		snd_hdsp_free(hdsp);
50971da177e4SLinus Torvalds }
50981da177e4SLinus Torvalds 
50991da177e4SLinus Torvalds static int __devinit snd_hdsp_probe(struct pci_dev *pci,
51001da177e4SLinus Torvalds 				    const struct pci_device_id *pci_id)
51011da177e4SLinus Torvalds {
51021da177e4SLinus Torvalds 	static int dev;
510355e957d8STakashi Iwai 	struct hdsp *hdsp;
510455e957d8STakashi Iwai 	struct snd_card *card;
51051da177e4SLinus Torvalds 	int err;
51061da177e4SLinus Torvalds 
51071da177e4SLinus Torvalds 	if (dev >= SNDRV_CARDS)
51081da177e4SLinus Torvalds 		return -ENODEV;
51091da177e4SLinus Torvalds 	if (!enable[dev]) {
51101da177e4SLinus Torvalds 		dev++;
51111da177e4SLinus Torvalds 		return -ENOENT;
51121da177e4SLinus Torvalds 	}
51131da177e4SLinus Torvalds 
511455e957d8STakashi Iwai 	if (!(card = snd_card_new(index[dev], id[dev], THIS_MODULE, sizeof(struct hdsp))))
51151da177e4SLinus Torvalds 		return -ENOMEM;
51161da177e4SLinus Torvalds 
511755e957d8STakashi Iwai 	hdsp = (struct hdsp *) card->private_data;
51181da177e4SLinus Torvalds 	card->private_free = snd_hdsp_card_free;
51191da177e4SLinus Torvalds 	hdsp->dev = dev;
51201da177e4SLinus Torvalds 	hdsp->pci = pci;
51211da177e4SLinus Torvalds 	snd_card_set_dev(card, &pci->dev);
51221da177e4SLinus Torvalds 
51231da177e4SLinus Torvalds 	if ((err = snd_hdsp_create(card, hdsp)) < 0) {
51241da177e4SLinus Torvalds 		snd_card_free(card);
51251da177e4SLinus Torvalds 		return err;
51261da177e4SLinus Torvalds 	}
51271da177e4SLinus Torvalds 
51281da177e4SLinus Torvalds 	strcpy(card->shortname, "Hammerfall DSP");
51291da177e4SLinus Torvalds 	sprintf(card->longname, "%s at 0x%lx, irq %d", hdsp->card_name,
51301da177e4SLinus Torvalds 		hdsp->port, hdsp->irq);
51311da177e4SLinus Torvalds 
51321da177e4SLinus Torvalds 	if ((err = snd_card_register(card)) < 0) {
51331da177e4SLinus Torvalds 		snd_card_free(card);
51341da177e4SLinus Torvalds 		return err;
51351da177e4SLinus Torvalds 	}
51361da177e4SLinus Torvalds 	pci_set_drvdata(pci, card);
51371da177e4SLinus Torvalds 	dev++;
51381da177e4SLinus Torvalds 	return 0;
51391da177e4SLinus Torvalds }
51401da177e4SLinus Torvalds 
51411da177e4SLinus Torvalds static void __devexit snd_hdsp_remove(struct pci_dev *pci)
51421da177e4SLinus Torvalds {
51431da177e4SLinus Torvalds 	snd_card_free(pci_get_drvdata(pci));
51441da177e4SLinus Torvalds 	pci_set_drvdata(pci, NULL);
51451da177e4SLinus Torvalds }
51461da177e4SLinus Torvalds 
51471da177e4SLinus Torvalds static struct pci_driver driver = {
51481da177e4SLinus Torvalds 	.name =     "RME Hammerfall DSP",
51491da177e4SLinus Torvalds 	.id_table = snd_hdsp_ids,
51501da177e4SLinus Torvalds 	.probe =    snd_hdsp_probe,
51511da177e4SLinus Torvalds 	.remove = __devexit_p(snd_hdsp_remove),
51521da177e4SLinus Torvalds };
51531da177e4SLinus Torvalds 
51541da177e4SLinus Torvalds static int __init alsa_card_hdsp_init(void)
51551da177e4SLinus Torvalds {
515601d25d46STakashi Iwai 	return pci_register_driver(&driver);
51571da177e4SLinus Torvalds }
51581da177e4SLinus Torvalds 
51591da177e4SLinus Torvalds static void __exit alsa_card_hdsp_exit(void)
51601da177e4SLinus Torvalds {
51611da177e4SLinus Torvalds 	pci_unregister_driver(&driver);
51621da177e4SLinus Torvalds }
51631da177e4SLinus Torvalds 
51641da177e4SLinus Torvalds module_init(alsa_card_hdsp_init)
51651da177e4SLinus Torvalds module_exit(alsa_card_hdsp_exit)
5166