xref: /openbmc/linux/sound/pci/rme9652/hdsp.c (revision 437a5a4606c12ab904793a7cad5b2062fc76c04e)
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}}");
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds #define HDSP_MAX_CHANNELS        26
651da177e4SLinus Torvalds #define HDSP_MAX_DS_CHANNELS     14
661da177e4SLinus Torvalds #define HDSP_MAX_QS_CHANNELS     8
671da177e4SLinus Torvalds #define DIGIFACE_SS_CHANNELS     26
681da177e4SLinus Torvalds #define DIGIFACE_DS_CHANNELS     14
691da177e4SLinus Torvalds #define MULTIFACE_SS_CHANNELS    18
701da177e4SLinus Torvalds #define MULTIFACE_DS_CHANNELS    14
711da177e4SLinus Torvalds #define H9652_SS_CHANNELS        26
721da177e4SLinus Torvalds #define H9652_DS_CHANNELS        14
731da177e4SLinus Torvalds /* This does not include possible Analog Extension Boards
741da177e4SLinus Torvalds    AEBs are detected at card initialization
751da177e4SLinus Torvalds */
761da177e4SLinus Torvalds #define H9632_SS_CHANNELS	 12
771da177e4SLinus Torvalds #define H9632_DS_CHANNELS	 8
781da177e4SLinus Torvalds #define H9632_QS_CHANNELS	 4
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds /* Write registers. These are defined as byte-offsets from the iobase value.
811da177e4SLinus Torvalds  */
821da177e4SLinus Torvalds #define HDSP_resetPointer               0
831da177e4SLinus Torvalds #define HDSP_outputBufferAddress	32
841da177e4SLinus Torvalds #define HDSP_inputBufferAddress		36
851da177e4SLinus Torvalds #define HDSP_controlRegister		64
861da177e4SLinus Torvalds #define HDSP_interruptConfirmation	96
871da177e4SLinus Torvalds #define HDSP_outputEnable	  	128
881da177e4SLinus Torvalds #define HDSP_control2Reg		256
891da177e4SLinus Torvalds #define HDSP_midiDataOut0  		352
901da177e4SLinus Torvalds #define HDSP_midiDataOut1  		356
911da177e4SLinus Torvalds #define HDSP_fifoData  			368
921da177e4SLinus Torvalds #define HDSP_inputEnable	 	384
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds /* Read registers. These are defined as byte-offsets from the iobase value
951da177e4SLinus Torvalds  */
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds #define HDSP_statusRegister    0
981da177e4SLinus Torvalds #define HDSP_timecode        128
991da177e4SLinus Torvalds #define HDSP_status2Register 192
1001da177e4SLinus Torvalds #define HDSP_midiDataOut0    352
1011da177e4SLinus Torvalds #define HDSP_midiDataOut1    356
1021da177e4SLinus Torvalds #define HDSP_midiDataIn0     360
1031da177e4SLinus Torvalds #define HDSP_midiDataIn1     364
1041da177e4SLinus Torvalds #define HDSP_midiStatusOut0  384
1051da177e4SLinus Torvalds #define HDSP_midiStatusOut1  388
1061da177e4SLinus Torvalds #define HDSP_midiStatusIn0   392
1071da177e4SLinus Torvalds #define HDSP_midiStatusIn1   396
1081da177e4SLinus Torvalds #define HDSP_fifoStatus      400
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds /* the meters are regular i/o-mapped registers, but offset
1111da177e4SLinus Torvalds    considerably from the rest. the peak registers are reset
1121da177e4SLinus Torvalds    when read; the least-significant 4 bits are full-scale counters;
1131da177e4SLinus Torvalds    the actual peak value is in the most-significant 24 bits.
1141da177e4SLinus Torvalds */
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds #define HDSP_playbackPeakLevel  4096  /* 26 * 32 bit values */
1171da177e4SLinus Torvalds #define HDSP_inputPeakLevel     4224  /* 26 * 32 bit values */
1181da177e4SLinus Torvalds #define HDSP_outputPeakLevel    4352  /* (26+2) * 32 bit values */
1191da177e4SLinus Torvalds #define HDSP_playbackRmsLevel   4612  /* 26 * 64 bit values */
1201da177e4SLinus Torvalds #define HDSP_inputRmsLevel      4868  /* 26 * 64 bit values */
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds /* This is for H9652 cards
1241da177e4SLinus Torvalds    Peak values are read downward from the base
1251da177e4SLinus Torvalds    Rms values are read upward
1261da177e4SLinus Torvalds    There are rms values for the outputs too
1271da177e4SLinus Torvalds    26*3 values are read in ss mode
1281da177e4SLinus Torvalds    14*3 in ds mode, with no gap between values
1291da177e4SLinus Torvalds */
1301da177e4SLinus Torvalds #define HDSP_9652_peakBase	7164
1311da177e4SLinus Torvalds #define HDSP_9652_rmsBase	4096
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds /* c.f. the hdsp_9632_meters_t struct */
1341da177e4SLinus Torvalds #define HDSP_9632_metersBase	4096
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds #define HDSP_IO_EXTENT     7168
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds /* control2 register bits */
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds #define HDSP_TMS                0x01
1411da177e4SLinus Torvalds #define HDSP_TCK                0x02
1421da177e4SLinus Torvalds #define HDSP_TDI                0x04
1431da177e4SLinus Torvalds #define HDSP_JTAG               0x08
1441da177e4SLinus Torvalds #define HDSP_PWDN               0x10
1451da177e4SLinus Torvalds #define HDSP_PROGRAM	        0x020
1461da177e4SLinus Torvalds #define HDSP_CONFIG_MODE_0	0x040
1471da177e4SLinus Torvalds #define HDSP_CONFIG_MODE_1	0x080
1481da177e4SLinus Torvalds #define HDSP_VERSION_BIT	0x100
1491da177e4SLinus Torvalds #define HDSP_BIGENDIAN_MODE     0x200
1501da177e4SLinus Torvalds #define HDSP_RD_MULTIPLE        0x400
1511da177e4SLinus Torvalds #define HDSP_9652_ENABLE_MIXER  0x800
1521da177e4SLinus Torvalds #define HDSP_TDO                0x10000000
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds #define HDSP_S_PROGRAM     	(HDSP_PROGRAM|HDSP_CONFIG_MODE_0)
1551da177e4SLinus Torvalds #define HDSP_S_LOAD		(HDSP_PROGRAM|HDSP_CONFIG_MODE_1)
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds /* Control Register bits */
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds #define HDSP_Start                (1<<0)  /* start engine */
1601da177e4SLinus Torvalds #define HDSP_Latency0             (1<<1)  /* buffer size = 2^n where n is defined by Latency{2,1,0} */
1611da177e4SLinus Torvalds #define HDSP_Latency1             (1<<2)  /* [ see above ] */
1621da177e4SLinus Torvalds #define HDSP_Latency2             (1<<3)  /* [ see above ] */
1631da177e4SLinus Torvalds #define HDSP_ClockModeMaster      (1<<4)  /* 1=Master, 0=Slave/Autosync */
1641da177e4SLinus Torvalds #define HDSP_AudioInterruptEnable (1<<5)  /* what do you think ? */
1651da177e4SLinus Torvalds #define HDSP_Frequency0           (1<<6)  /* 0=44.1kHz/88.2kHz/176.4kHz 1=48kHz/96kHz/192kHz */
1661da177e4SLinus Torvalds #define HDSP_Frequency1           (1<<7)  /* 0=32kHz/64kHz/128kHz */
1671da177e4SLinus Torvalds #define HDSP_DoubleSpeed          (1<<8)  /* 0=normal speed, 1=double speed */
1681da177e4SLinus Torvalds #define HDSP_SPDIFProfessional    (1<<9)  /* 0=consumer, 1=professional */
1691da177e4SLinus Torvalds #define HDSP_SPDIFEmphasis        (1<<10) /* 0=none, 1=on */
1701da177e4SLinus Torvalds #define HDSP_SPDIFNonAudio        (1<<11) /* 0=off, 1=on */
1711da177e4SLinus Torvalds #define HDSP_SPDIFOpticalOut      (1<<12) /* 1=use 1st ADAT connector for SPDIF, 0=do not */
1721da177e4SLinus Torvalds #define HDSP_SyncRef2             (1<<13)
1731da177e4SLinus Torvalds #define HDSP_SPDIFInputSelect0    (1<<14)
1741da177e4SLinus Torvalds #define HDSP_SPDIFInputSelect1    (1<<15)
1751da177e4SLinus Torvalds #define HDSP_SyncRef0             (1<<16)
1761da177e4SLinus Torvalds #define HDSP_SyncRef1             (1<<17)
1771da177e4SLinus Torvalds #define HDSP_AnalogExtensionBoard (1<<18) /* For H9632 cards */
1781da177e4SLinus Torvalds #define HDSP_XLRBreakoutCable     (1<<20) /* For H9632 cards */
1791da177e4SLinus Torvalds #define HDSP_Midi0InterruptEnable (1<<22)
1801da177e4SLinus Torvalds #define HDSP_Midi1InterruptEnable (1<<23)
1811da177e4SLinus Torvalds #define HDSP_LineOut              (1<<24)
1821da177e4SLinus Torvalds #define HDSP_ADGain0		  (1<<25) /* From here : H9632 specific */
1831da177e4SLinus Torvalds #define HDSP_ADGain1		  (1<<26)
1841da177e4SLinus Torvalds #define HDSP_DAGain0		  (1<<27)
1851da177e4SLinus Torvalds #define HDSP_DAGain1		  (1<<28)
1861da177e4SLinus Torvalds #define HDSP_PhoneGain0		  (1<<29)
1871da177e4SLinus Torvalds #define HDSP_PhoneGain1		  (1<<30)
1881da177e4SLinus Torvalds #define HDSP_QuadSpeed	  	  (1<<31)
1891da177e4SLinus Torvalds 
1901da177e4SLinus Torvalds #define HDSP_ADGainMask       (HDSP_ADGain0|HDSP_ADGain1)
1911da177e4SLinus Torvalds #define HDSP_ADGainMinus10dBV  HDSP_ADGainMask
1921da177e4SLinus Torvalds #define HDSP_ADGainPlus4dBu   (HDSP_ADGain0)
1931da177e4SLinus Torvalds #define HDSP_ADGainLowGain     0
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds #define HDSP_DAGainMask         (HDSP_DAGain0|HDSP_DAGain1)
1961da177e4SLinus Torvalds #define HDSP_DAGainHighGain      HDSP_DAGainMask
1971da177e4SLinus Torvalds #define HDSP_DAGainPlus4dBu     (HDSP_DAGain0)
1981da177e4SLinus Torvalds #define HDSP_DAGainMinus10dBV    0
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds #define HDSP_PhoneGainMask      (HDSP_PhoneGain0|HDSP_PhoneGain1)
2011da177e4SLinus Torvalds #define HDSP_PhoneGain0dB        HDSP_PhoneGainMask
2021da177e4SLinus Torvalds #define HDSP_PhoneGainMinus6dB  (HDSP_PhoneGain0)
2031da177e4SLinus Torvalds #define HDSP_PhoneGainMinus12dB  0
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds #define HDSP_LatencyMask    (HDSP_Latency0|HDSP_Latency1|HDSP_Latency2)
2061da177e4SLinus Torvalds #define HDSP_FrequencyMask  (HDSP_Frequency0|HDSP_Frequency1|HDSP_DoubleSpeed|HDSP_QuadSpeed)
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds #define HDSP_SPDIFInputMask    (HDSP_SPDIFInputSelect0|HDSP_SPDIFInputSelect1)
2091da177e4SLinus Torvalds #define HDSP_SPDIFInputADAT1    0
2101da177e4SLinus Torvalds #define HDSP_SPDIFInputCoaxial (HDSP_SPDIFInputSelect0)
2111da177e4SLinus Torvalds #define HDSP_SPDIFInputCdrom   (HDSP_SPDIFInputSelect1)
2121da177e4SLinus Torvalds #define HDSP_SPDIFInputAES     (HDSP_SPDIFInputSelect0|HDSP_SPDIFInputSelect1)
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds #define HDSP_SyncRefMask        (HDSP_SyncRef0|HDSP_SyncRef1|HDSP_SyncRef2)
2151da177e4SLinus Torvalds #define HDSP_SyncRef_ADAT1       0
2161da177e4SLinus Torvalds #define HDSP_SyncRef_ADAT2      (HDSP_SyncRef0)
2171da177e4SLinus Torvalds #define HDSP_SyncRef_ADAT3      (HDSP_SyncRef1)
2181da177e4SLinus Torvalds #define HDSP_SyncRef_SPDIF      (HDSP_SyncRef0|HDSP_SyncRef1)
2191da177e4SLinus Torvalds #define HDSP_SyncRef_WORD       (HDSP_SyncRef2)
2201da177e4SLinus Torvalds #define HDSP_SyncRef_ADAT_SYNC  (HDSP_SyncRef0|HDSP_SyncRef2)
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds /* Sample Clock Sources */
2231da177e4SLinus Torvalds 
2241da177e4SLinus Torvalds #define HDSP_CLOCK_SOURCE_AUTOSYNC           0
2251da177e4SLinus Torvalds #define HDSP_CLOCK_SOURCE_INTERNAL_32KHZ     1
2261da177e4SLinus Torvalds #define HDSP_CLOCK_SOURCE_INTERNAL_44_1KHZ   2
2271da177e4SLinus Torvalds #define HDSP_CLOCK_SOURCE_INTERNAL_48KHZ     3
2281da177e4SLinus Torvalds #define HDSP_CLOCK_SOURCE_INTERNAL_64KHZ     4
2291da177e4SLinus Torvalds #define HDSP_CLOCK_SOURCE_INTERNAL_88_2KHZ   5
2301da177e4SLinus Torvalds #define HDSP_CLOCK_SOURCE_INTERNAL_96KHZ     6
2311da177e4SLinus Torvalds #define HDSP_CLOCK_SOURCE_INTERNAL_128KHZ    7
2321da177e4SLinus Torvalds #define HDSP_CLOCK_SOURCE_INTERNAL_176_4KHZ  8
2331da177e4SLinus Torvalds #define HDSP_CLOCK_SOURCE_INTERNAL_192KHZ    9
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds /* Preferred sync reference choices - used by "pref_sync_ref" control switch */
2361da177e4SLinus Torvalds 
2371da177e4SLinus Torvalds #define HDSP_SYNC_FROM_WORD      0
2381da177e4SLinus Torvalds #define HDSP_SYNC_FROM_SPDIF     1
2391da177e4SLinus Torvalds #define HDSP_SYNC_FROM_ADAT1     2
2401da177e4SLinus Torvalds #define HDSP_SYNC_FROM_ADAT_SYNC 3
2411da177e4SLinus Torvalds #define HDSP_SYNC_FROM_ADAT2     4
2421da177e4SLinus Torvalds #define HDSP_SYNC_FROM_ADAT3     5
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds /* SyncCheck status */
2451da177e4SLinus Torvalds 
2461da177e4SLinus Torvalds #define HDSP_SYNC_CHECK_NO_LOCK 0
2471da177e4SLinus Torvalds #define HDSP_SYNC_CHECK_LOCK    1
2481da177e4SLinus Torvalds #define HDSP_SYNC_CHECK_SYNC	2
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds /* AutoSync references - used by "autosync_ref" control switch */
2511da177e4SLinus Torvalds 
2521da177e4SLinus Torvalds #define HDSP_AUTOSYNC_FROM_WORD      0
2531da177e4SLinus Torvalds #define HDSP_AUTOSYNC_FROM_ADAT_SYNC 1
2541da177e4SLinus Torvalds #define HDSP_AUTOSYNC_FROM_SPDIF     2
2551da177e4SLinus Torvalds #define HDSP_AUTOSYNC_FROM_NONE	     3
2561da177e4SLinus Torvalds #define HDSP_AUTOSYNC_FROM_ADAT1     4
2571da177e4SLinus Torvalds #define HDSP_AUTOSYNC_FROM_ADAT2     5
2581da177e4SLinus Torvalds #define HDSP_AUTOSYNC_FROM_ADAT3     6
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds /* Possible sources of S/PDIF input */
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds #define HDSP_SPDIFIN_OPTICAL  0	/* optical  (ADAT1) */
2631da177e4SLinus Torvalds #define HDSP_SPDIFIN_COAXIAL  1	/* coaxial (RCA) */
2641da177e4SLinus Torvalds #define HDSP_SPDIFIN_INTERNAL 2	/* internal (CDROM) */
2651da177e4SLinus Torvalds #define HDSP_SPDIFIN_AES      3 /* xlr for H9632 (AES)*/
2661da177e4SLinus Torvalds 
2671da177e4SLinus Torvalds #define HDSP_Frequency32KHz    HDSP_Frequency0
2681da177e4SLinus Torvalds #define HDSP_Frequency44_1KHz  HDSP_Frequency1
2691da177e4SLinus Torvalds #define HDSP_Frequency48KHz    (HDSP_Frequency1|HDSP_Frequency0)
2701da177e4SLinus Torvalds #define HDSP_Frequency64KHz    (HDSP_DoubleSpeed|HDSP_Frequency0)
2711da177e4SLinus Torvalds #define HDSP_Frequency88_2KHz  (HDSP_DoubleSpeed|HDSP_Frequency1)
2721da177e4SLinus Torvalds #define HDSP_Frequency96KHz    (HDSP_DoubleSpeed|HDSP_Frequency1|HDSP_Frequency0)
2731da177e4SLinus Torvalds /* For H9632 cards */
2741da177e4SLinus Torvalds #define HDSP_Frequency128KHz   (HDSP_QuadSpeed|HDSP_DoubleSpeed|HDSP_Frequency0)
2751da177e4SLinus Torvalds #define HDSP_Frequency176_4KHz (HDSP_QuadSpeed|HDSP_DoubleSpeed|HDSP_Frequency1)
2761da177e4SLinus Torvalds #define HDSP_Frequency192KHz   (HDSP_QuadSpeed|HDSP_DoubleSpeed|HDSP_Frequency1|HDSP_Frequency0)
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds #define hdsp_encode_latency(x)       (((x)<<1) & HDSP_LatencyMask)
2791da177e4SLinus Torvalds #define hdsp_decode_latency(x)       (((x) & HDSP_LatencyMask)>>1)
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds #define hdsp_encode_spdif_in(x) (((x)&0x3)<<14)
2821da177e4SLinus Torvalds #define hdsp_decode_spdif_in(x) (((x)>>14)&0x3)
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds /* Status Register bits */
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds #define HDSP_audioIRQPending    (1<<0)
2871da177e4SLinus Torvalds #define HDSP_Lock2              (1<<1)     /* this is for Digiface and H9652 */
2881da177e4SLinus Torvalds #define HDSP_spdifFrequency3	HDSP_Lock2 /* this is for H9632 only */
2891da177e4SLinus Torvalds #define HDSP_Lock1              (1<<2)
2901da177e4SLinus Torvalds #define HDSP_Lock0              (1<<3)
2911da177e4SLinus Torvalds #define HDSP_SPDIFSync          (1<<4)
2921da177e4SLinus Torvalds #define HDSP_TimecodeLock       (1<<5)
2931da177e4SLinus Torvalds #define HDSP_BufferPositionMask 0x000FFC0 /* Bit 6..15 : h/w buffer pointer */
2941da177e4SLinus Torvalds #define HDSP_Sync2              (1<<16)
2951da177e4SLinus Torvalds #define HDSP_Sync1              (1<<17)
2961da177e4SLinus Torvalds #define HDSP_Sync0              (1<<18)
2971da177e4SLinus Torvalds #define HDSP_DoubleSpeedStatus  (1<<19)
2981da177e4SLinus Torvalds #define HDSP_ConfigError        (1<<20)
2991da177e4SLinus Torvalds #define HDSP_DllError           (1<<21)
3001da177e4SLinus Torvalds #define HDSP_spdifFrequency0    (1<<22)
3011da177e4SLinus Torvalds #define HDSP_spdifFrequency1    (1<<23)
3021da177e4SLinus Torvalds #define HDSP_spdifFrequency2    (1<<24)
3031da177e4SLinus Torvalds #define HDSP_SPDIFErrorFlag     (1<<25)
3041da177e4SLinus Torvalds #define HDSP_BufferID           (1<<26)
3051da177e4SLinus Torvalds #define HDSP_TimecodeSync       (1<<27)
3061da177e4SLinus Torvalds #define HDSP_AEBO          	(1<<28) /* H9632 specific Analog Extension Boards */
3071da177e4SLinus Torvalds #define HDSP_AEBI		(1<<29) /* 0 = present, 1 = absent */
3081da177e4SLinus Torvalds #define HDSP_midi0IRQPending    (1<<30)
3091da177e4SLinus Torvalds #define HDSP_midi1IRQPending    (1<<31)
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds #define HDSP_spdifFrequencyMask    (HDSP_spdifFrequency0|HDSP_spdifFrequency1|HDSP_spdifFrequency2)
3121da177e4SLinus Torvalds 
3131da177e4SLinus Torvalds #define HDSP_spdifFrequency32KHz   (HDSP_spdifFrequency0)
3141da177e4SLinus Torvalds #define HDSP_spdifFrequency44_1KHz (HDSP_spdifFrequency1)
3151da177e4SLinus Torvalds #define HDSP_spdifFrequency48KHz   (HDSP_spdifFrequency0|HDSP_spdifFrequency1)
3161da177e4SLinus Torvalds 
3171da177e4SLinus Torvalds #define HDSP_spdifFrequency64KHz   (HDSP_spdifFrequency2)
3181da177e4SLinus Torvalds #define HDSP_spdifFrequency88_2KHz (HDSP_spdifFrequency0|HDSP_spdifFrequency2)
3191da177e4SLinus Torvalds #define HDSP_spdifFrequency96KHz   (HDSP_spdifFrequency2|HDSP_spdifFrequency1)
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds /* This is for H9632 cards */
3221da177e4SLinus Torvalds #define HDSP_spdifFrequency128KHz   HDSP_spdifFrequencyMask
3231da177e4SLinus Torvalds #define HDSP_spdifFrequency176_4KHz HDSP_spdifFrequency3
3241da177e4SLinus Torvalds #define HDSP_spdifFrequency192KHz   (HDSP_spdifFrequency3|HDSP_spdifFrequency0)
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds /* Status2 Register bits */
3271da177e4SLinus Torvalds 
3281da177e4SLinus Torvalds #define HDSP_version0     (1<<0)
3291da177e4SLinus Torvalds #define HDSP_version1     (1<<1)
3301da177e4SLinus Torvalds #define HDSP_version2     (1<<2)
3311da177e4SLinus Torvalds #define HDSP_wc_lock      (1<<3)
3321da177e4SLinus Torvalds #define HDSP_wc_sync      (1<<4)
3331da177e4SLinus Torvalds #define HDSP_inp_freq0    (1<<5)
3341da177e4SLinus Torvalds #define HDSP_inp_freq1    (1<<6)
3351da177e4SLinus Torvalds #define HDSP_inp_freq2    (1<<7)
3361da177e4SLinus Torvalds #define HDSP_SelSyncRef0  (1<<8)
3371da177e4SLinus Torvalds #define HDSP_SelSyncRef1  (1<<9)
3381da177e4SLinus Torvalds #define HDSP_SelSyncRef2  (1<<10)
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds #define HDSP_wc_valid (HDSP_wc_lock|HDSP_wc_sync)
3411da177e4SLinus Torvalds 
3421da177e4SLinus Torvalds #define HDSP_systemFrequencyMask (HDSP_inp_freq0|HDSP_inp_freq1|HDSP_inp_freq2)
3431da177e4SLinus Torvalds #define HDSP_systemFrequency32   (HDSP_inp_freq0)
3441da177e4SLinus Torvalds #define HDSP_systemFrequency44_1 (HDSP_inp_freq1)
3451da177e4SLinus Torvalds #define HDSP_systemFrequency48   (HDSP_inp_freq0|HDSP_inp_freq1)
3461da177e4SLinus Torvalds #define HDSP_systemFrequency64   (HDSP_inp_freq2)
3471da177e4SLinus Torvalds #define HDSP_systemFrequency88_2 (HDSP_inp_freq0|HDSP_inp_freq2)
3481da177e4SLinus Torvalds #define HDSP_systemFrequency96   (HDSP_inp_freq1|HDSP_inp_freq2)
3491da177e4SLinus Torvalds /* FIXME : more values for 9632 cards ? */
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds #define HDSP_SelSyncRefMask        (HDSP_SelSyncRef0|HDSP_SelSyncRef1|HDSP_SelSyncRef2)
3521da177e4SLinus Torvalds #define HDSP_SelSyncRef_ADAT1      0
3531da177e4SLinus Torvalds #define HDSP_SelSyncRef_ADAT2      (HDSP_SelSyncRef0)
3541da177e4SLinus Torvalds #define HDSP_SelSyncRef_ADAT3      (HDSP_SelSyncRef1)
3551da177e4SLinus Torvalds #define HDSP_SelSyncRef_SPDIF      (HDSP_SelSyncRef0|HDSP_SelSyncRef1)
3561da177e4SLinus Torvalds #define HDSP_SelSyncRef_WORD       (HDSP_SelSyncRef2)
3571da177e4SLinus Torvalds #define HDSP_SelSyncRef_ADAT_SYNC  (HDSP_SelSyncRef0|HDSP_SelSyncRef2)
3581da177e4SLinus Torvalds 
3591da177e4SLinus Torvalds /* Card state flags */
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds #define HDSP_InitializationComplete  (1<<0)
3621da177e4SLinus Torvalds #define HDSP_FirmwareLoaded	     (1<<1)
3631da177e4SLinus Torvalds #define HDSP_FirmwareCached	     (1<<2)
3641da177e4SLinus Torvalds 
3651da177e4SLinus Torvalds /* FIFO wait times, defined in terms of 1/10ths of msecs */
3661da177e4SLinus Torvalds 
3671da177e4SLinus Torvalds #define HDSP_LONG_WAIT	 5000
3681da177e4SLinus Torvalds #define HDSP_SHORT_WAIT  30
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds #define UNITY_GAIN                       32768
3711da177e4SLinus Torvalds #define MINUS_INFINITY_GAIN              0
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds /* the size of a substream (1 mono data stream) */
3741da177e4SLinus Torvalds 
3751da177e4SLinus Torvalds #define HDSP_CHANNEL_BUFFER_SAMPLES  (16*1024)
3761da177e4SLinus Torvalds #define HDSP_CHANNEL_BUFFER_BYTES    (4*HDSP_CHANNEL_BUFFER_SAMPLES)
3771da177e4SLinus Torvalds 
3781da177e4SLinus Torvalds /* the size of the area we need to allocate for DMA transfers. the
3791da177e4SLinus Torvalds    size is the same regardless of the number of channels - the
3801da177e4SLinus Torvalds    Multiface still uses the same memory area.
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds    Note that we allocate 1 more channel than is apparently needed
3831da177e4SLinus Torvalds    because the h/w seems to write 1 byte beyond the end of the last
3841da177e4SLinus Torvalds    page. Sigh.
3851da177e4SLinus Torvalds */
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds #define HDSP_DMA_AREA_BYTES ((HDSP_MAX_CHANNELS+1) * HDSP_CHANNEL_BUFFER_BYTES)
3881da177e4SLinus Torvalds #define HDSP_DMA_AREA_KILOBYTES (HDSP_DMA_AREA_BYTES/1024)
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds /* use hotplug firmeare loader? */
3911da177e4SLinus Torvalds #if defined(CONFIG_FW_LOADER) || defined(CONFIG_FW_LOADER_MODULE)
392140432fdSTakashi Iwai #if !defined(HDSP_USE_HWDEP_LOADER) && !defined(CONFIG_SND_HDSP)
3931da177e4SLinus Torvalds #define HDSP_FW_LOADER
3941da177e4SLinus Torvalds #endif
3951da177e4SLinus Torvalds #endif
3961da177e4SLinus Torvalds 
39755e957d8STakashi Iwai struct hdsp_9632_meters {
3981da177e4SLinus Torvalds     u32 input_peak[16];
3991da177e4SLinus Torvalds     u32 playback_peak[16];
4001da177e4SLinus Torvalds     u32 output_peak[16];
4011da177e4SLinus Torvalds     u32 xxx_peak[16];
4021da177e4SLinus Torvalds     u32 padding[64];
4031da177e4SLinus Torvalds     u32 input_rms_low[16];
4041da177e4SLinus Torvalds     u32 playback_rms_low[16];
4051da177e4SLinus Torvalds     u32 output_rms_low[16];
4061da177e4SLinus Torvalds     u32 xxx_rms_low[16];
4071da177e4SLinus Torvalds     u32 input_rms_high[16];
4081da177e4SLinus Torvalds     u32 playback_rms_high[16];
4091da177e4SLinus Torvalds     u32 output_rms_high[16];
4101da177e4SLinus Torvalds     u32 xxx_rms_high[16];
4111da177e4SLinus Torvalds };
4121da177e4SLinus Torvalds 
41355e957d8STakashi Iwai struct hdsp_midi {
41455e957d8STakashi Iwai     struct hdsp             *hdsp;
4151da177e4SLinus Torvalds     int                      id;
41655e957d8STakashi Iwai     struct snd_rawmidi           *rmidi;
41755e957d8STakashi Iwai     struct snd_rawmidi_substream *input;
41855e957d8STakashi Iwai     struct snd_rawmidi_substream *output;
4191da177e4SLinus Torvalds     char                     istimer; /* timer in use */
4201da177e4SLinus Torvalds     struct timer_list	     timer;
4211da177e4SLinus Torvalds     spinlock_t               lock;
4221da177e4SLinus Torvalds     int			     pending;
4231da177e4SLinus Torvalds };
4241da177e4SLinus Torvalds 
42555e957d8STakashi Iwai struct hdsp {
4261da177e4SLinus Torvalds 	spinlock_t            lock;
42755e957d8STakashi Iwai 	struct snd_pcm_substream *capture_substream;
42855e957d8STakashi Iwai 	struct snd_pcm_substream *playback_substream;
42955e957d8STakashi Iwai         struct hdsp_midi      midi[2];
4301da177e4SLinus Torvalds 	struct tasklet_struct midi_tasklet;
4311da177e4SLinus Torvalds 	int		      use_midi_tasklet;
4321da177e4SLinus Torvalds 	int                   precise_ptr;
4331da177e4SLinus Torvalds 	u32                   control_register;	     /* cached value */
4341da177e4SLinus Torvalds 	u32                   control2_register;     /* cached value */
4351da177e4SLinus Torvalds 	u32                   creg_spdif;
4361da177e4SLinus Torvalds 	u32                   creg_spdif_stream;
437e3ea4d89STakashi Iwai 	int                   clock_source_locked;
4381da177e4SLinus Torvalds 	char                 *card_name;	     /* digiface/multiface */
43955e957d8STakashi Iwai 	enum HDSP_IO_Type     io_type;               /* ditto, but for code use */
4401da177e4SLinus Torvalds         unsigned short        firmware_rev;
4411da177e4SLinus Torvalds 	unsigned short	      state;		     /* stores state bits */
4421da177e4SLinus Torvalds 	u32		      firmware_cache[24413]; /* this helps recover from accidental iobox power failure */
4431da177e4SLinus Torvalds 	size_t                period_bytes; 	     /* guess what this is */
4441da177e4SLinus Torvalds 	unsigned char	      max_channels;
4451da177e4SLinus Torvalds 	unsigned char	      qs_in_channels;	     /* quad speed mode for H9632 */
4461da177e4SLinus Torvalds 	unsigned char         ds_in_channels;
4471da177e4SLinus Torvalds 	unsigned char         ss_in_channels;	    /* different for multiface/digiface */
4481da177e4SLinus Torvalds 	unsigned char	      qs_out_channels;
4491da177e4SLinus Torvalds 	unsigned char         ds_out_channels;
4501da177e4SLinus Torvalds 	unsigned char         ss_out_channels;
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds 	struct snd_dma_buffer capture_dma_buf;
4531da177e4SLinus Torvalds 	struct snd_dma_buffer playback_dma_buf;
4541da177e4SLinus Torvalds 	unsigned char        *capture_buffer;	    /* suitably aligned address */
4551da177e4SLinus Torvalds 	unsigned char        *playback_buffer;	    /* suitably aligned address */
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds 	pid_t                 capture_pid;
4581da177e4SLinus Torvalds 	pid_t                 playback_pid;
4591da177e4SLinus Torvalds 	int                   running;
4601da177e4SLinus Torvalds 	int                   system_sample_rate;
4611da177e4SLinus Torvalds 	char                 *channel_map;
4621da177e4SLinus Torvalds 	int                   dev;
4631da177e4SLinus Torvalds 	int                   irq;
4641da177e4SLinus Torvalds 	unsigned long         port;
4651da177e4SLinus Torvalds         void __iomem         *iobase;
46655e957d8STakashi Iwai 	struct snd_card *card;
46755e957d8STakashi Iwai 	struct snd_pcm *pcm;
46855e957d8STakashi Iwai 	struct snd_hwdep          *hwdep;
4691da177e4SLinus Torvalds 	struct pci_dev       *pci;
47055e957d8STakashi Iwai 	struct snd_kcontrol *spdif_ctl;
4711da177e4SLinus Torvalds         unsigned short        mixer_matrix[HDSP_MATRIX_MIXER_SIZE];
4721da177e4SLinus Torvalds };
4731da177e4SLinus Torvalds 
4741da177e4SLinus Torvalds /* These tables map the ALSA channels 1..N to the channels that we
4751da177e4SLinus Torvalds    need to use in order to find the relevant channel buffer. RME
4761da177e4SLinus Torvalds    refer to this kind of mapping as between "the ADAT channel and
4771da177e4SLinus Torvalds    the DMA channel." We index it using the logical audio channel,
4781da177e4SLinus Torvalds    and the value is the DMA channel (i.e. channel buffer number)
4791da177e4SLinus Torvalds    where the data for that channel can be read/written from/to.
4801da177e4SLinus Torvalds */
4811da177e4SLinus Torvalds 
4821da177e4SLinus Torvalds static char channel_map_df_ss[HDSP_MAX_CHANNELS] = {
4831da177e4SLinus Torvalds 	0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
4841da177e4SLinus Torvalds 	18, 19, 20, 21, 22, 23, 24, 25
4851da177e4SLinus Torvalds };
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds static char channel_map_mf_ss[HDSP_MAX_CHANNELS] = { /* Multiface */
4881da177e4SLinus Torvalds 	/* Analog */
4891da177e4SLinus Torvalds 	0, 1, 2, 3, 4, 5, 6, 7,
4901da177e4SLinus Torvalds 	/* ADAT 2 */
4911da177e4SLinus Torvalds 	16, 17, 18, 19, 20, 21, 22, 23,
4921da177e4SLinus Torvalds 	/* SPDIF */
4931da177e4SLinus Torvalds 	24, 25,
4941da177e4SLinus Torvalds 	-1, -1, -1, -1, -1, -1, -1, -1
4951da177e4SLinus Torvalds };
4961da177e4SLinus Torvalds 
4971da177e4SLinus Torvalds static char channel_map_ds[HDSP_MAX_CHANNELS] = {
4981da177e4SLinus Torvalds 	/* ADAT channels are remapped */
4991da177e4SLinus Torvalds 	1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23,
5001da177e4SLinus Torvalds 	/* channels 12 and 13 are S/PDIF */
5011da177e4SLinus Torvalds 	24, 25,
5021da177e4SLinus Torvalds 	/* others don't exist */
5031da177e4SLinus Torvalds 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
5041da177e4SLinus Torvalds };
5051da177e4SLinus Torvalds 
5061da177e4SLinus Torvalds static char channel_map_H9632_ss[HDSP_MAX_CHANNELS] = {
5071da177e4SLinus Torvalds 	/* ADAT channels */
5081da177e4SLinus Torvalds 	0, 1, 2, 3, 4, 5, 6, 7,
5091da177e4SLinus Torvalds 	/* SPDIF */
5101da177e4SLinus Torvalds 	8, 9,
5111da177e4SLinus Torvalds 	/* Analog */
5121da177e4SLinus Torvalds 	10, 11,
5131da177e4SLinus Torvalds 	/* AO4S-192 and AI4S-192 extension boards */
5141da177e4SLinus Torvalds 	12, 13, 14, 15,
5151da177e4SLinus Torvalds 	/* others don't exist */
5161da177e4SLinus Torvalds 	-1, -1, -1, -1, -1, -1, -1, -1,
5171da177e4SLinus Torvalds 	-1, -1
5181da177e4SLinus Torvalds };
5191da177e4SLinus Torvalds 
5201da177e4SLinus Torvalds static char channel_map_H9632_ds[HDSP_MAX_CHANNELS] = {
5211da177e4SLinus Torvalds 	/* ADAT */
5221da177e4SLinus Torvalds 	1, 3, 5, 7,
5231da177e4SLinus Torvalds 	/* SPDIF */
5241da177e4SLinus Torvalds 	8, 9,
5251da177e4SLinus Torvalds 	/* Analog */
5261da177e4SLinus Torvalds 	10, 11,
5271da177e4SLinus Torvalds 	/* AO4S-192 and AI4S-192 extension boards */
5281da177e4SLinus Torvalds 	12, 13, 14, 15,
5291da177e4SLinus Torvalds 	/* others don't exist */
5301da177e4SLinus Torvalds 	-1, -1, -1, -1, -1, -1, -1, -1,
5311da177e4SLinus Torvalds 	-1, -1, -1, -1, -1, -1
5321da177e4SLinus Torvalds };
5331da177e4SLinus Torvalds 
5341da177e4SLinus Torvalds static char channel_map_H9632_qs[HDSP_MAX_CHANNELS] = {
5351da177e4SLinus Torvalds 	/* ADAT is disabled in this mode */
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, -1, -1,
5451da177e4SLinus Torvalds 	-1, -1
5461da177e4SLinus Torvalds };
5471da177e4SLinus Torvalds 
5481da177e4SLinus Torvalds static int snd_hammerfall_get_buffer(struct pci_dev *pci, struct snd_dma_buffer *dmab, size_t size)
5491da177e4SLinus Torvalds {
5501da177e4SLinus Torvalds 	dmab->dev.type = SNDRV_DMA_TYPE_DEV;
5511da177e4SLinus Torvalds 	dmab->dev.dev = snd_dma_pci_data(pci);
552b6a96915STakashi Iwai 	if (snd_dma_get_reserved_buf(dmab, snd_dma_pci_buf_id(pci))) {
553b6a96915STakashi Iwai 		if (dmab->bytes >= size)
554b6a96915STakashi Iwai 			return 0;
555b6a96915STakashi Iwai 	}
5561da177e4SLinus Torvalds 	if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
5571da177e4SLinus Torvalds 				size, dmab) < 0)
5581da177e4SLinus Torvalds 		return -ENOMEM;
5591da177e4SLinus Torvalds 	return 0;
5601da177e4SLinus Torvalds }
5611da177e4SLinus Torvalds 
5621da177e4SLinus Torvalds static void snd_hammerfall_free_buffer(struct snd_dma_buffer *dmab, struct pci_dev *pci)
5631da177e4SLinus Torvalds {
564b6a96915STakashi Iwai 	if (dmab->area) {
565b6a96915STakashi Iwai 		dmab->dev.dev = NULL; /* make it anonymous */
5661da177e4SLinus Torvalds 		snd_dma_reserve_buf(dmab, snd_dma_pci_buf_id(pci));
5671da177e4SLinus Torvalds 	}
568b6a96915STakashi Iwai }
5691da177e4SLinus Torvalds 
5701da177e4SLinus Torvalds 
571f40b6890STakashi Iwai static struct pci_device_id snd_hdsp_ids[] = {
5721da177e4SLinus Torvalds 	{
5731da177e4SLinus Torvalds 		.vendor = PCI_VENDOR_ID_XILINX,
5741da177e4SLinus Torvalds 		.device = PCI_DEVICE_ID_XILINX_HAMMERFALL_DSP,
5751da177e4SLinus Torvalds 		.subvendor = PCI_ANY_ID,
5761da177e4SLinus Torvalds 		.subdevice = PCI_ANY_ID,
5771da177e4SLinus Torvalds 	}, /* RME Hammerfall-DSP */
5781da177e4SLinus Torvalds 	{ 0, },
5791da177e4SLinus Torvalds };
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds MODULE_DEVICE_TABLE(pci, snd_hdsp_ids);
5821da177e4SLinus Torvalds 
5831da177e4SLinus Torvalds /* prototypes */
58455e957d8STakashi Iwai static int snd_hdsp_create_alsa_devices(struct snd_card *card, struct hdsp *hdsp);
58555e957d8STakashi Iwai static int snd_hdsp_create_pcm(struct snd_card *card, struct hdsp *hdsp);
58655e957d8STakashi Iwai static int snd_hdsp_enable_io (struct hdsp *hdsp);
58755e957d8STakashi Iwai static void snd_hdsp_initialize_midi_flush (struct hdsp *hdsp);
58855e957d8STakashi Iwai static void snd_hdsp_initialize_channels (struct hdsp *hdsp);
58955e957d8STakashi Iwai static int hdsp_fifo_wait(struct hdsp *hdsp, int count, int timeout);
59055e957d8STakashi Iwai static int hdsp_autosync_ref(struct hdsp *hdsp);
59155e957d8STakashi Iwai static int snd_hdsp_set_defaults(struct hdsp *hdsp);
59255e957d8STakashi Iwai static void snd_hdsp_9652_enable_mixer (struct hdsp *hdsp);
5931da177e4SLinus Torvalds 
59455e957d8STakashi Iwai static int hdsp_playback_to_output_key (struct hdsp *hdsp, int in, int out)
5951da177e4SLinus Torvalds {
5961da177e4SLinus Torvalds 	switch (hdsp->firmware_rev) {
5971da177e4SLinus Torvalds 	case 0xa:
5981da177e4SLinus Torvalds 		return (64 * out) + (32 + (in));
5991da177e4SLinus Torvalds 	case 0x96:
6001da177e4SLinus Torvalds 	case 0x97:
6011da177e4SLinus Torvalds 		return (32 * out) + (16 + (in));
6021da177e4SLinus Torvalds 	default:
6031da177e4SLinus Torvalds 		return (52 * out) + (26 + (in));
6041da177e4SLinus Torvalds 	}
6051da177e4SLinus Torvalds }
6061da177e4SLinus Torvalds 
60755e957d8STakashi Iwai static int hdsp_input_to_output_key (struct hdsp *hdsp, int in, int out)
6081da177e4SLinus Torvalds {
6091da177e4SLinus Torvalds 	switch (hdsp->firmware_rev) {
6101da177e4SLinus Torvalds 	case 0xa:
6111da177e4SLinus Torvalds 		return (64 * out) + in;
6121da177e4SLinus Torvalds 	case 0x96:
6131da177e4SLinus Torvalds 	case 0x97:
6141da177e4SLinus Torvalds 		return (32 * out) + in;
6151da177e4SLinus Torvalds 	default:
6161da177e4SLinus Torvalds 		return (52 * out) + in;
6171da177e4SLinus Torvalds 	}
6181da177e4SLinus Torvalds }
6191da177e4SLinus Torvalds 
62055e957d8STakashi Iwai static void hdsp_write(struct hdsp *hdsp, int reg, int val)
6211da177e4SLinus Torvalds {
6221da177e4SLinus Torvalds 	writel(val, hdsp->iobase + reg);
6231da177e4SLinus Torvalds }
6241da177e4SLinus Torvalds 
62555e957d8STakashi Iwai static unsigned int hdsp_read(struct hdsp *hdsp, int reg)
6261da177e4SLinus Torvalds {
6271da177e4SLinus Torvalds 	return readl (hdsp->iobase + reg);
6281da177e4SLinus Torvalds }
6291da177e4SLinus Torvalds 
63055e957d8STakashi Iwai static int hdsp_check_for_iobox (struct hdsp *hdsp)
6311da177e4SLinus Torvalds {
6321da177e4SLinus Torvalds 
6331da177e4SLinus Torvalds 	if (hdsp->io_type == H9652 || hdsp->io_type == H9632) return 0;
6341da177e4SLinus Torvalds 	if (hdsp_read (hdsp, HDSP_statusRegister) & HDSP_ConfigError) {
6351da177e4SLinus Torvalds 		snd_printk ("Hammerfall-DSP: no Digiface or Multiface connected!\n");
6361da177e4SLinus Torvalds 		hdsp->state &= ~HDSP_FirmwareLoaded;
6371da177e4SLinus Torvalds 		return -EIO;
6381da177e4SLinus Torvalds 	}
6391da177e4SLinus Torvalds 	return 0;
6401da177e4SLinus Torvalds 
6411da177e4SLinus Torvalds }
6421da177e4SLinus Torvalds 
64355e957d8STakashi Iwai static int snd_hdsp_load_firmware_from_cache(struct hdsp *hdsp) {
6441da177e4SLinus Torvalds 
6451da177e4SLinus Torvalds 	int i;
6461da177e4SLinus Torvalds 	unsigned long flags;
6471da177e4SLinus Torvalds 
6481da177e4SLinus Torvalds 	if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
6491da177e4SLinus Torvalds 
6501da177e4SLinus Torvalds 		snd_printk ("Hammerfall-DSP: loading firmware\n");
6511da177e4SLinus Torvalds 
6521da177e4SLinus Torvalds 		hdsp_write (hdsp, HDSP_control2Reg, HDSP_S_PROGRAM);
6531da177e4SLinus Torvalds 		hdsp_write (hdsp, HDSP_fifoData, 0);
6541da177e4SLinus Torvalds 
6551da177e4SLinus Torvalds 		if (hdsp_fifo_wait (hdsp, 0, HDSP_LONG_WAIT)) {
6561da177e4SLinus Torvalds 			snd_printk ("Hammerfall-DSP: timeout waiting for download preparation\n");
6571da177e4SLinus Torvalds 			return -EIO;
6581da177e4SLinus Torvalds 		}
6591da177e4SLinus Torvalds 
6601da177e4SLinus Torvalds 		hdsp_write (hdsp, HDSP_control2Reg, HDSP_S_LOAD);
6611da177e4SLinus Torvalds 
6621da177e4SLinus Torvalds 		for (i = 0; i < 24413; ++i) {
6631da177e4SLinus Torvalds 			hdsp_write(hdsp, HDSP_fifoData, hdsp->firmware_cache[i]);
6641da177e4SLinus Torvalds 			if (hdsp_fifo_wait (hdsp, 127, HDSP_LONG_WAIT)) {
6651da177e4SLinus Torvalds 				snd_printk ("Hammerfall-DSP: timeout during firmware loading\n");
6661da177e4SLinus Torvalds 				return -EIO;
6671da177e4SLinus Torvalds 			}
6681da177e4SLinus Torvalds 		}
6691da177e4SLinus Torvalds 
670ef21ca24SNishanth Aravamudan 		ssleep(3);
6711da177e4SLinus Torvalds 
6721da177e4SLinus Torvalds 		if (hdsp_fifo_wait (hdsp, 0, HDSP_LONG_WAIT)) {
6731da177e4SLinus Torvalds 			snd_printk ("Hammerfall-DSP: timeout at end of firmware loading\n");
6741da177e4SLinus Torvalds 		    	return -EIO;
6751da177e4SLinus Torvalds 		}
6761da177e4SLinus Torvalds 
6771da177e4SLinus Torvalds #ifdef SNDRV_BIG_ENDIAN
6781da177e4SLinus Torvalds 		hdsp->control2_register = HDSP_BIGENDIAN_MODE;
6791da177e4SLinus Torvalds #else
6801da177e4SLinus Torvalds 		hdsp->control2_register = 0;
6811da177e4SLinus Torvalds #endif
6821da177e4SLinus Torvalds 		hdsp_write (hdsp, HDSP_control2Reg, hdsp->control2_register);
6831da177e4SLinus Torvalds 		snd_printk ("Hammerfall-DSP: finished firmware loading\n");
6841da177e4SLinus Torvalds 
6851da177e4SLinus Torvalds 	}
6861da177e4SLinus Torvalds 	if (hdsp->state & HDSP_InitializationComplete) {
687b0b98119STakashi Iwai 		snd_printk(KERN_INFO "Hammerfall-DSP: firmware loaded from cache, restoring defaults\n");
6881da177e4SLinus Torvalds 		spin_lock_irqsave(&hdsp->lock, flags);
6891da177e4SLinus Torvalds 		snd_hdsp_set_defaults(hdsp);
6901da177e4SLinus Torvalds 		spin_unlock_irqrestore(&hdsp->lock, flags);
6911da177e4SLinus Torvalds 	}
6921da177e4SLinus Torvalds 
6931da177e4SLinus Torvalds 	hdsp->state |= HDSP_FirmwareLoaded;
6941da177e4SLinus Torvalds 
6951da177e4SLinus Torvalds 	return 0;
6961da177e4SLinus Torvalds }
6971da177e4SLinus Torvalds 
69855e957d8STakashi Iwai static int hdsp_get_iobox_version (struct hdsp *hdsp)
6991da177e4SLinus Torvalds {
7001da177e4SLinus Torvalds 	if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
7011da177e4SLinus Torvalds 
7021da177e4SLinus Torvalds 		hdsp_write (hdsp, HDSP_control2Reg, HDSP_PROGRAM);
7031da177e4SLinus Torvalds 		hdsp_write (hdsp, HDSP_fifoData, 0);
704b0b98119STakashi Iwai 		if (hdsp_fifo_wait (hdsp, 0, HDSP_SHORT_WAIT) < 0)
7051da177e4SLinus Torvalds 			return -EIO;
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds 		hdsp_write (hdsp, HDSP_control2Reg, HDSP_S_LOAD);
7081da177e4SLinus Torvalds 		hdsp_write (hdsp, HDSP_fifoData, 0);
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds 		if (hdsp_fifo_wait (hdsp, 0, HDSP_SHORT_WAIT)) {
7111da177e4SLinus Torvalds 			hdsp->io_type = Multiface;
7121da177e4SLinus Torvalds 			hdsp_write (hdsp, HDSP_control2Reg, HDSP_VERSION_BIT);
7131da177e4SLinus Torvalds 			hdsp_write (hdsp, HDSP_control2Reg, HDSP_S_LOAD);
7141da177e4SLinus Torvalds 			hdsp_fifo_wait (hdsp, 0, HDSP_SHORT_WAIT);
7151da177e4SLinus Torvalds 		} else {
7161da177e4SLinus Torvalds 			hdsp->io_type = Digiface;
7171da177e4SLinus Torvalds 		}
7181da177e4SLinus Torvalds 	} else {
7191da177e4SLinus Torvalds 		/* firmware was already loaded, get iobox type */
720b0b98119STakashi Iwai 		if (hdsp_read(hdsp, HDSP_status2Register) & HDSP_version1)
7211da177e4SLinus Torvalds 			hdsp->io_type = Multiface;
722b0b98119STakashi Iwai 		else
7231da177e4SLinus Torvalds 			hdsp->io_type = Digiface;
7241da177e4SLinus Torvalds 	}
7251da177e4SLinus Torvalds 	return 0;
7261da177e4SLinus Torvalds }
7271da177e4SLinus Torvalds 
7281da177e4SLinus Torvalds 
729311e70a4STakashi Iwai #ifdef HDSP_FW_LOADER
730311e70a4STakashi Iwai static int __devinit hdsp_request_fw_loader(struct hdsp *hdsp);
731311e70a4STakashi Iwai #endif
732311e70a4STakashi Iwai 
733311e70a4STakashi Iwai static int hdsp_check_for_firmware (struct hdsp *hdsp, int load_on_demand)
7341da177e4SLinus Torvalds {
735311e70a4STakashi Iwai 	if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
736311e70a4STakashi Iwai 		return 0;
7371da177e4SLinus Torvalds 	if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
7381da177e4SLinus Torvalds 		hdsp->state &= ~HDSP_FirmwareLoaded;
739311e70a4STakashi Iwai 		if (! load_on_demand)
740b0b98119STakashi Iwai 			return -EIO;
741311e70a4STakashi Iwai 		snd_printk(KERN_ERR "Hammerfall-DSP: firmware not present.\n");
742b0b98119STakashi Iwai 		/* try to load firmware */
743311e70a4STakashi Iwai 		if (! (hdsp->state & HDSP_FirmwareCached)) {
744311e70a4STakashi Iwai #ifdef HDSP_FW_LOADER
745311e70a4STakashi Iwai 			if (! hdsp_request_fw_loader(hdsp))
746311e70a4STakashi Iwai 				return 0;
747311e70a4STakashi Iwai #endif
748311e70a4STakashi Iwai 			snd_printk(KERN_ERR
749311e70a4STakashi Iwai 				   "Hammerfall-DSP: No firmware loaded nor "
750311e70a4STakashi Iwai 				   "cached, please upload firmware.\n");
7511da177e4SLinus Torvalds 			return -EIO;
7521da177e4SLinus Torvalds 		}
753311e70a4STakashi Iwai 		if (snd_hdsp_load_firmware_from_cache(hdsp) != 0) {
754311e70a4STakashi Iwai 			snd_printk(KERN_ERR
755311e70a4STakashi Iwai 				   "Hammerfall-DSP: Firmware loading from "
756311e70a4STakashi Iwai 				   "cache failed, please upload manually.\n");
757311e70a4STakashi Iwai 			return -EIO;
758311e70a4STakashi Iwai 		}
759311e70a4STakashi Iwai 	}
7601da177e4SLinus Torvalds 	return 0;
7611da177e4SLinus Torvalds }
7621da177e4SLinus Torvalds 
7631da177e4SLinus Torvalds 
76455e957d8STakashi Iwai static int hdsp_fifo_wait(struct hdsp *hdsp, int count, int timeout)
7651da177e4SLinus Torvalds {
7661da177e4SLinus Torvalds 	int i;
7671da177e4SLinus Torvalds 
7681da177e4SLinus Torvalds 	/* the fifoStatus registers reports on how many words
7691da177e4SLinus Torvalds 	   are available in the command FIFO.
7701da177e4SLinus Torvalds 	*/
7711da177e4SLinus Torvalds 
7721da177e4SLinus Torvalds 	for (i = 0; i < timeout; i++) {
7731da177e4SLinus Torvalds 
7741da177e4SLinus Torvalds 		if ((int)(hdsp_read (hdsp, HDSP_fifoStatus) & 0xff) <= count)
7751da177e4SLinus Torvalds 			return 0;
7761da177e4SLinus Torvalds 
7771da177e4SLinus Torvalds 		/* not very friendly, but we only do this during a firmware
7781da177e4SLinus Torvalds 		   load and changing the mixer, so we just put up with it.
7791da177e4SLinus Torvalds 		*/
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds 		udelay (100);
7821da177e4SLinus Torvalds 	}
7831da177e4SLinus Torvalds 
7841da177e4SLinus Torvalds 	snd_printk ("Hammerfall-DSP: wait for FIFO status <= %d failed after %d iterations\n",
7851da177e4SLinus Torvalds 		    count, timeout);
7861da177e4SLinus Torvalds 	return -1;
7871da177e4SLinus Torvalds }
7881da177e4SLinus Torvalds 
78955e957d8STakashi Iwai static int hdsp_read_gain (struct hdsp *hdsp, unsigned int addr)
7901da177e4SLinus Torvalds {
791b0b98119STakashi Iwai 	if (addr >= HDSP_MATRIX_MIXER_SIZE)
7921da177e4SLinus Torvalds 		return 0;
793b0b98119STakashi Iwai 
7941da177e4SLinus Torvalds 	return hdsp->mixer_matrix[addr];
7951da177e4SLinus Torvalds }
7961da177e4SLinus Torvalds 
79755e957d8STakashi Iwai static int hdsp_write_gain(struct hdsp *hdsp, unsigned int addr, unsigned short data)
7981da177e4SLinus Torvalds {
7991da177e4SLinus Torvalds 	unsigned int ad;
8001da177e4SLinus Torvalds 
8011da177e4SLinus Torvalds 	if (addr >= HDSP_MATRIX_MIXER_SIZE)
8021da177e4SLinus Torvalds 		return -1;
8031da177e4SLinus Torvalds 
8041da177e4SLinus Torvalds 	if (hdsp->io_type == H9652 || hdsp->io_type == H9632) {
8051da177e4SLinus Torvalds 
8061da177e4SLinus Torvalds 		/* from martin bjornsen:
8071da177e4SLinus Torvalds 
8081da177e4SLinus Torvalds 		   "You can only write dwords to the
8091da177e4SLinus Torvalds 		   mixer memory which contain two
8101da177e4SLinus Torvalds 		   mixer values in the low and high
8111da177e4SLinus Torvalds 		   word. So if you want to change
8121da177e4SLinus Torvalds 		   value 0 you have to read value 1
8131da177e4SLinus Torvalds 		   from the cache and write both to
8141da177e4SLinus Torvalds 		   the first dword in the mixer
8151da177e4SLinus Torvalds 		   memory."
8161da177e4SLinus Torvalds 		*/
8171da177e4SLinus Torvalds 
818b0b98119STakashi Iwai 		if (hdsp->io_type == H9632 && addr >= 512)
8191da177e4SLinus Torvalds 			return 0;
8201da177e4SLinus Torvalds 
821b0b98119STakashi Iwai 		if (hdsp->io_type == H9652 && addr >= 1352)
8221da177e4SLinus Torvalds 			return 0;
8231da177e4SLinus Torvalds 
8241da177e4SLinus Torvalds 		hdsp->mixer_matrix[addr] = data;
8251da177e4SLinus Torvalds 
8261da177e4SLinus Torvalds 
8271da177e4SLinus Torvalds 		/* `addr' addresses a 16-bit wide address, but
8281da177e4SLinus Torvalds 		   the address space accessed via hdsp_write
8291da177e4SLinus Torvalds 		   uses byte offsets. put another way, addr
8301da177e4SLinus Torvalds 		   varies from 0 to 1351, but to access the
8311da177e4SLinus Torvalds 		   corresponding memory location, we need
8321da177e4SLinus Torvalds 		   to access 0 to 2703 ...
8331da177e4SLinus Torvalds 		*/
8341da177e4SLinus Torvalds 		ad = addr/2;
8351da177e4SLinus Torvalds 
8361da177e4SLinus Torvalds 		hdsp_write (hdsp, 4096 + (ad*4),
8371da177e4SLinus Torvalds 			    (hdsp->mixer_matrix[(addr&0x7fe)+1] << 16) +
8381da177e4SLinus Torvalds 			    hdsp->mixer_matrix[addr&0x7fe]);
8391da177e4SLinus Torvalds 
8401da177e4SLinus Torvalds 		return 0;
8411da177e4SLinus Torvalds 
8421da177e4SLinus Torvalds 	} else {
8431da177e4SLinus Torvalds 
8441da177e4SLinus Torvalds 		ad = (addr << 16) + data;
8451da177e4SLinus Torvalds 
846b0b98119STakashi Iwai 		if (hdsp_fifo_wait(hdsp, 127, HDSP_LONG_WAIT))
8471da177e4SLinus Torvalds 			return -1;
8481da177e4SLinus Torvalds 
8491da177e4SLinus Torvalds 		hdsp_write (hdsp, HDSP_fifoData, ad);
8501da177e4SLinus Torvalds 		hdsp->mixer_matrix[addr] = data;
8511da177e4SLinus Torvalds 
8521da177e4SLinus Torvalds 	}
8531da177e4SLinus Torvalds 
8541da177e4SLinus Torvalds 	return 0;
8551da177e4SLinus Torvalds }
8561da177e4SLinus Torvalds 
85755e957d8STakashi Iwai static int snd_hdsp_use_is_exclusive(struct hdsp *hdsp)
8581da177e4SLinus Torvalds {
8591da177e4SLinus Torvalds 	unsigned long flags;
8601da177e4SLinus Torvalds 	int ret = 1;
8611da177e4SLinus Torvalds 
8621da177e4SLinus Torvalds 	spin_lock_irqsave(&hdsp->lock, flags);
8631da177e4SLinus Torvalds 	if ((hdsp->playback_pid != hdsp->capture_pid) &&
864b0b98119STakashi Iwai 	    (hdsp->playback_pid >= 0) && (hdsp->capture_pid >= 0))
8651da177e4SLinus Torvalds 		ret = 0;
8661da177e4SLinus Torvalds 	spin_unlock_irqrestore(&hdsp->lock, flags);
8671da177e4SLinus Torvalds 	return ret;
8681da177e4SLinus Torvalds }
8691da177e4SLinus Torvalds 
87055e957d8STakashi Iwai static int hdsp_external_sample_rate (struct hdsp *hdsp)
8711da177e4SLinus Torvalds {
8721da177e4SLinus Torvalds 	unsigned int status2 = hdsp_read(hdsp, HDSP_status2Register);
8731da177e4SLinus Torvalds 	unsigned int rate_bits = status2 & HDSP_systemFrequencyMask;
8741da177e4SLinus Torvalds 
8751da177e4SLinus Torvalds 	switch (rate_bits) {
8761da177e4SLinus Torvalds 	case HDSP_systemFrequency32:   return 32000;
8771da177e4SLinus Torvalds 	case HDSP_systemFrequency44_1: return 44100;
8781da177e4SLinus Torvalds 	case HDSP_systemFrequency48:   return 48000;
8791da177e4SLinus Torvalds 	case HDSP_systemFrequency64:   return 64000;
8801da177e4SLinus Torvalds 	case HDSP_systemFrequency88_2: return 88200;
8811da177e4SLinus Torvalds 	case HDSP_systemFrequency96:   return 96000;
8821da177e4SLinus Torvalds 	default:
8831da177e4SLinus Torvalds 		return 0;
8841da177e4SLinus Torvalds 	}
8851da177e4SLinus Torvalds }
8861da177e4SLinus Torvalds 
88755e957d8STakashi Iwai static int hdsp_spdif_sample_rate(struct hdsp *hdsp)
8881da177e4SLinus Torvalds {
8891da177e4SLinus Torvalds 	unsigned int status = hdsp_read(hdsp, HDSP_statusRegister);
8901da177e4SLinus Torvalds 	unsigned int rate_bits = (status & HDSP_spdifFrequencyMask);
8911da177e4SLinus Torvalds 
892b0b98119STakashi Iwai 	if (status & HDSP_SPDIFErrorFlag)
8931da177e4SLinus Torvalds 		return 0;
8941da177e4SLinus Torvalds 
8951da177e4SLinus Torvalds 	switch (rate_bits) {
8961da177e4SLinus Torvalds 	case HDSP_spdifFrequency32KHz: return 32000;
8971da177e4SLinus Torvalds 	case HDSP_spdifFrequency44_1KHz: return 44100;
8981da177e4SLinus Torvalds 	case HDSP_spdifFrequency48KHz: return 48000;
8991da177e4SLinus Torvalds 	case HDSP_spdifFrequency64KHz: return 64000;
9001da177e4SLinus Torvalds 	case HDSP_spdifFrequency88_2KHz: return 88200;
9011da177e4SLinus Torvalds 	case HDSP_spdifFrequency96KHz: return 96000;
9021da177e4SLinus Torvalds 	case HDSP_spdifFrequency128KHz:
9031da177e4SLinus Torvalds 		if (hdsp->io_type == H9632) return 128000;
9041da177e4SLinus Torvalds 		break;
9051da177e4SLinus Torvalds 	case HDSP_spdifFrequency176_4KHz:
9061da177e4SLinus Torvalds 		if (hdsp->io_type == H9632) return 176400;
9071da177e4SLinus Torvalds 		break;
9081da177e4SLinus Torvalds 	case HDSP_spdifFrequency192KHz:
9091da177e4SLinus Torvalds 		if (hdsp->io_type == H9632) return 192000;
9101da177e4SLinus Torvalds 		break;
9111da177e4SLinus Torvalds 	default:
9121da177e4SLinus Torvalds 		break;
9131da177e4SLinus Torvalds 	}
9141da177e4SLinus Torvalds 	snd_printk ("Hammerfall-DSP: unknown spdif frequency status; bits = 0x%x, status = 0x%x\n", rate_bits, status);
9151da177e4SLinus Torvalds 	return 0;
9161da177e4SLinus Torvalds }
9171da177e4SLinus Torvalds 
91855e957d8STakashi Iwai static void hdsp_compute_period_size(struct hdsp *hdsp)
9191da177e4SLinus Torvalds {
9201da177e4SLinus Torvalds 	hdsp->period_bytes = 1 << ((hdsp_decode_latency(hdsp->control_register) + 8));
9211da177e4SLinus Torvalds }
9221da177e4SLinus Torvalds 
92355e957d8STakashi Iwai static snd_pcm_uframes_t hdsp_hw_pointer(struct hdsp *hdsp)
9241da177e4SLinus Torvalds {
9251da177e4SLinus Torvalds 	int position;
9261da177e4SLinus Torvalds 
9271da177e4SLinus Torvalds 	position = hdsp_read(hdsp, HDSP_statusRegister);
9281da177e4SLinus Torvalds 
929b0b98119STakashi Iwai 	if (!hdsp->precise_ptr)
9301da177e4SLinus Torvalds 		return (position & HDSP_BufferID) ? (hdsp->period_bytes / 4) : 0;
9311da177e4SLinus Torvalds 
9321da177e4SLinus Torvalds 	position &= HDSP_BufferPositionMask;
9331da177e4SLinus Torvalds 	position /= 4;
9341da177e4SLinus Torvalds 	position &= (hdsp->period_bytes/2) - 1;
9351da177e4SLinus Torvalds 	return position;
9361da177e4SLinus Torvalds }
9371da177e4SLinus Torvalds 
93855e957d8STakashi Iwai static void hdsp_reset_hw_pointer(struct hdsp *hdsp)
9391da177e4SLinus Torvalds {
9401da177e4SLinus Torvalds 	hdsp_write (hdsp, HDSP_resetPointer, 0);
9411da177e4SLinus Torvalds }
9421da177e4SLinus Torvalds 
94355e957d8STakashi Iwai static void hdsp_start_audio(struct hdsp *s)
9441da177e4SLinus Torvalds {
9451da177e4SLinus Torvalds 	s->control_register |= (HDSP_AudioInterruptEnable | HDSP_Start);
9461da177e4SLinus Torvalds 	hdsp_write(s, HDSP_controlRegister, s->control_register);
9471da177e4SLinus Torvalds }
9481da177e4SLinus Torvalds 
94955e957d8STakashi Iwai static void hdsp_stop_audio(struct hdsp *s)
9501da177e4SLinus Torvalds {
9511da177e4SLinus Torvalds 	s->control_register &= ~(HDSP_Start | HDSP_AudioInterruptEnable);
9521da177e4SLinus Torvalds 	hdsp_write(s, HDSP_controlRegister, s->control_register);
9531da177e4SLinus Torvalds }
9541da177e4SLinus Torvalds 
95555e957d8STakashi Iwai static void hdsp_silence_playback(struct hdsp *hdsp)
9561da177e4SLinus Torvalds {
9571da177e4SLinus Torvalds 	memset(hdsp->playback_buffer, 0, HDSP_DMA_AREA_BYTES);
9581da177e4SLinus Torvalds }
9591da177e4SLinus Torvalds 
96055e957d8STakashi Iwai static int hdsp_set_interrupt_interval(struct hdsp *s, unsigned int frames)
9611da177e4SLinus Torvalds {
9621da177e4SLinus Torvalds 	int n;
9631da177e4SLinus Torvalds 
9641da177e4SLinus Torvalds 	spin_lock_irq(&s->lock);
9651da177e4SLinus Torvalds 
9661da177e4SLinus Torvalds 	frames >>= 7;
9671da177e4SLinus Torvalds 	n = 0;
9681da177e4SLinus Torvalds 	while (frames) {
9691da177e4SLinus Torvalds 		n++;
9701da177e4SLinus Torvalds 		frames >>= 1;
9711da177e4SLinus Torvalds 	}
9721da177e4SLinus Torvalds 
9731da177e4SLinus Torvalds 	s->control_register &= ~HDSP_LatencyMask;
9741da177e4SLinus Torvalds 	s->control_register |= hdsp_encode_latency(n);
9751da177e4SLinus Torvalds 
9761da177e4SLinus Torvalds 	hdsp_write(s, HDSP_controlRegister, s->control_register);
9771da177e4SLinus Torvalds 
9781da177e4SLinus Torvalds 	hdsp_compute_period_size(s);
9791da177e4SLinus Torvalds 
9801da177e4SLinus Torvalds 	spin_unlock_irq(&s->lock);
9811da177e4SLinus Torvalds 
9821da177e4SLinus Torvalds 	return 0;
9831da177e4SLinus Torvalds }
9841da177e4SLinus Torvalds 
98555e957d8STakashi Iwai static int hdsp_set_rate(struct hdsp *hdsp, int rate, int called_internally)
9861da177e4SLinus Torvalds {
9871da177e4SLinus Torvalds 	int reject_if_open = 0;
9881da177e4SLinus Torvalds 	int current_rate;
9891da177e4SLinus Torvalds 	int rate_bits;
9901da177e4SLinus Torvalds 
9911da177e4SLinus Torvalds 	/* ASSUMPTION: hdsp->lock is either held, or
9921da177e4SLinus Torvalds 	   there is no need for it (e.g. during module
9931da177e4SLinus Torvalds 	   initialization).
9941da177e4SLinus Torvalds 	*/
9951da177e4SLinus Torvalds 
9961da177e4SLinus Torvalds 	if (!(hdsp->control_register & HDSP_ClockModeMaster)) {
9971da177e4SLinus Torvalds 		if (called_internally) {
9981da177e4SLinus Torvalds 			/* request from ctl or card initialization */
999b0b98119STakashi Iwai 			snd_printk(KERN_ERR "Hammerfall-DSP: device is not running as a clock master: cannot set sample rate.\n");
10001da177e4SLinus Torvalds 			return -1;
10011da177e4SLinus Torvalds 		} else {
10021da177e4SLinus Torvalds 			/* hw_param request while in AutoSync mode */
10031da177e4SLinus Torvalds 			int external_freq = hdsp_external_sample_rate(hdsp);
10041da177e4SLinus Torvalds 			int spdif_freq = hdsp_spdif_sample_rate(hdsp);
10051da177e4SLinus Torvalds 
1006b0b98119STakashi Iwai 			if ((spdif_freq == external_freq*2) && (hdsp_autosync_ref(hdsp) >= HDSP_AUTOSYNC_FROM_ADAT1))
1007b0b98119STakashi Iwai 				snd_printk(KERN_INFO "Hammerfall-DSP: Detected ADAT in double speed mode\n");
1008b0b98119STakashi Iwai 			else if (hdsp->io_type == H9632 && (spdif_freq == external_freq*4) && (hdsp_autosync_ref(hdsp) >= HDSP_AUTOSYNC_FROM_ADAT1))
1009b0b98119STakashi Iwai 				snd_printk(KERN_INFO "Hammerfall-DSP: Detected ADAT in quad speed mode\n");
1010b0b98119STakashi Iwai 			else if (rate != external_freq) {
1011b0b98119STakashi Iwai 				snd_printk(KERN_INFO "Hammerfall-DSP: No AutoSync source for requested rate\n");
10121da177e4SLinus Torvalds 				return -1;
10131da177e4SLinus Torvalds 			}
10141da177e4SLinus Torvalds 		}
10151da177e4SLinus Torvalds 	}
10161da177e4SLinus Torvalds 
10171da177e4SLinus Torvalds 	current_rate = hdsp->system_sample_rate;
10181da177e4SLinus Torvalds 
10191da177e4SLinus Torvalds 	/* Changing from a "single speed" to a "double speed" rate is
10201da177e4SLinus Torvalds 	   not allowed if any substreams are open. This is because
10211da177e4SLinus Torvalds 	   such a change causes a shift in the location of
10221da177e4SLinus Torvalds 	   the DMA buffers and a reduction in the number of available
10231da177e4SLinus Torvalds 	   buffers.
10241da177e4SLinus Torvalds 
10251da177e4SLinus Torvalds 	   Note that a similar but essentially insoluble problem
10261da177e4SLinus Torvalds 	   exists for externally-driven rate changes. All we can do
10271da177e4SLinus Torvalds 	   is to flag rate changes in the read/write routines.  */
10281da177e4SLinus Torvalds 
1029b0b98119STakashi Iwai 	if (rate > 96000 && hdsp->io_type != H9632)
10301da177e4SLinus Torvalds 		return -EINVAL;
10311da177e4SLinus Torvalds 
10321da177e4SLinus Torvalds 	switch (rate) {
10331da177e4SLinus Torvalds 	case 32000:
1034b0b98119STakashi Iwai 		if (current_rate > 48000)
10351da177e4SLinus Torvalds 			reject_if_open = 1;
10361da177e4SLinus Torvalds 		rate_bits = HDSP_Frequency32KHz;
10371da177e4SLinus Torvalds 		break;
10381da177e4SLinus Torvalds 	case 44100:
1039b0b98119STakashi Iwai 		if (current_rate > 48000)
10401da177e4SLinus Torvalds 			reject_if_open = 1;
10411da177e4SLinus Torvalds 		rate_bits = HDSP_Frequency44_1KHz;
10421da177e4SLinus Torvalds 		break;
10431da177e4SLinus Torvalds 	case 48000:
1044b0b98119STakashi Iwai 		if (current_rate > 48000)
10451da177e4SLinus Torvalds 			reject_if_open = 1;
10461da177e4SLinus Torvalds 		rate_bits = HDSP_Frequency48KHz;
10471da177e4SLinus Torvalds 		break;
10481da177e4SLinus Torvalds 	case 64000:
1049b0b98119STakashi Iwai 		if (current_rate <= 48000 || current_rate > 96000)
10501da177e4SLinus Torvalds 			reject_if_open = 1;
10511da177e4SLinus Torvalds 		rate_bits = HDSP_Frequency64KHz;
10521da177e4SLinus Torvalds 		break;
10531da177e4SLinus Torvalds 	case 88200:
1054b0b98119STakashi Iwai 		if (current_rate <= 48000 || current_rate > 96000)
10551da177e4SLinus Torvalds 			reject_if_open = 1;
10561da177e4SLinus Torvalds 		rate_bits = HDSP_Frequency88_2KHz;
10571da177e4SLinus Torvalds 		break;
10581da177e4SLinus Torvalds 	case 96000:
1059b0b98119STakashi Iwai 		if (current_rate <= 48000 || current_rate > 96000)
10601da177e4SLinus Torvalds 			reject_if_open = 1;
10611da177e4SLinus Torvalds 		rate_bits = HDSP_Frequency96KHz;
10621da177e4SLinus Torvalds 		break;
10631da177e4SLinus Torvalds 	case 128000:
1064b0b98119STakashi Iwai 		if (current_rate < 128000)
10651da177e4SLinus Torvalds 			reject_if_open = 1;
10661da177e4SLinus Torvalds 		rate_bits = HDSP_Frequency128KHz;
10671da177e4SLinus Torvalds 		break;
10681da177e4SLinus Torvalds 	case 176400:
1069b0b98119STakashi Iwai 		if (current_rate < 128000)
10701da177e4SLinus Torvalds 			reject_if_open = 1;
10711da177e4SLinus Torvalds 		rate_bits = HDSP_Frequency176_4KHz;
10721da177e4SLinus Torvalds 		break;
10731da177e4SLinus Torvalds 	case 192000:
1074b0b98119STakashi Iwai 		if (current_rate < 128000)
10751da177e4SLinus Torvalds 			reject_if_open = 1;
10761da177e4SLinus Torvalds 		rate_bits = HDSP_Frequency192KHz;
10771da177e4SLinus Torvalds 		break;
10781da177e4SLinus Torvalds 	default:
10791da177e4SLinus Torvalds 		return -EINVAL;
10801da177e4SLinus Torvalds 	}
10811da177e4SLinus Torvalds 
10821da177e4SLinus Torvalds 	if (reject_if_open && (hdsp->capture_pid >= 0 || hdsp->playback_pid >= 0)) {
10831da177e4SLinus Torvalds 		snd_printk ("Hammerfall-DSP: cannot change speed mode (capture PID = %d, playback PID = %d)\n",
10841da177e4SLinus Torvalds 			    hdsp->capture_pid,
10851da177e4SLinus Torvalds 			    hdsp->playback_pid);
10861da177e4SLinus Torvalds 		return -EBUSY;
10871da177e4SLinus Torvalds 	}
10881da177e4SLinus Torvalds 
10891da177e4SLinus Torvalds 	hdsp->control_register &= ~HDSP_FrequencyMask;
10901da177e4SLinus Torvalds 	hdsp->control_register |= rate_bits;
10911da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
10921da177e4SLinus Torvalds 
10931da177e4SLinus Torvalds 	if (rate >= 128000) {
10941da177e4SLinus Torvalds 		hdsp->channel_map = channel_map_H9632_qs;
10951da177e4SLinus Torvalds 	} else if (rate > 48000) {
1096b0b98119STakashi Iwai 		if (hdsp->io_type == H9632)
10971da177e4SLinus Torvalds 			hdsp->channel_map = channel_map_H9632_ds;
1098b0b98119STakashi Iwai 		else
10991da177e4SLinus Torvalds 			hdsp->channel_map = channel_map_ds;
11001da177e4SLinus Torvalds 	} else {
11011da177e4SLinus Torvalds 		switch (hdsp->io_type) {
11021da177e4SLinus Torvalds 		case Multiface:
11031da177e4SLinus Torvalds 			hdsp->channel_map = channel_map_mf_ss;
11041da177e4SLinus Torvalds 			break;
11051da177e4SLinus Torvalds 		case Digiface:
11061da177e4SLinus Torvalds 		case H9652:
11071da177e4SLinus Torvalds 			hdsp->channel_map = channel_map_df_ss;
11081da177e4SLinus Torvalds 			break;
11091da177e4SLinus Torvalds 		case H9632:
11101da177e4SLinus Torvalds 			hdsp->channel_map = channel_map_H9632_ss;
11111da177e4SLinus Torvalds 			break;
11121da177e4SLinus Torvalds 		default:
11131da177e4SLinus Torvalds 			/* should never happen */
11141da177e4SLinus Torvalds 			break;
11151da177e4SLinus Torvalds 		}
11161da177e4SLinus Torvalds 	}
11171da177e4SLinus Torvalds 
11181da177e4SLinus Torvalds 	hdsp->system_sample_rate = rate;
11191da177e4SLinus Torvalds 
11201da177e4SLinus Torvalds 	return 0;
11211da177e4SLinus Torvalds }
11221da177e4SLinus Torvalds 
11231da177e4SLinus Torvalds /*----------------------------------------------------------------------------
11241da177e4SLinus Torvalds    MIDI
11251da177e4SLinus Torvalds   ----------------------------------------------------------------------------*/
11261da177e4SLinus Torvalds 
112755e957d8STakashi Iwai static unsigned char snd_hdsp_midi_read_byte (struct hdsp *hdsp, int id)
11281da177e4SLinus Torvalds {
11291da177e4SLinus Torvalds 	/* the hardware already does the relevant bit-mask with 0xff */
1130b0b98119STakashi Iwai 	if (id)
11311da177e4SLinus Torvalds 		return hdsp_read(hdsp, HDSP_midiDataIn1);
1132b0b98119STakashi Iwai 	else
11331da177e4SLinus Torvalds 		return hdsp_read(hdsp, HDSP_midiDataIn0);
11341da177e4SLinus Torvalds }
11351da177e4SLinus Torvalds 
113655e957d8STakashi Iwai static void snd_hdsp_midi_write_byte (struct hdsp *hdsp, int id, int val)
11371da177e4SLinus Torvalds {
11381da177e4SLinus Torvalds 	/* the hardware already does the relevant bit-mask with 0xff */
1139b0b98119STakashi Iwai 	if (id)
11401da177e4SLinus Torvalds 		hdsp_write(hdsp, HDSP_midiDataOut1, val);
1141b0b98119STakashi Iwai 	else
11421da177e4SLinus Torvalds 		hdsp_write(hdsp, HDSP_midiDataOut0, val);
11431da177e4SLinus Torvalds }
11441da177e4SLinus Torvalds 
114555e957d8STakashi Iwai static int snd_hdsp_midi_input_available (struct hdsp *hdsp, int id)
11461da177e4SLinus Torvalds {
1147b0b98119STakashi Iwai 	if (id)
11481da177e4SLinus Torvalds 		return (hdsp_read(hdsp, HDSP_midiStatusIn1) & 0xff);
1149b0b98119STakashi Iwai 	else
11501da177e4SLinus Torvalds 		return (hdsp_read(hdsp, HDSP_midiStatusIn0) & 0xff);
11511da177e4SLinus Torvalds }
11521da177e4SLinus Torvalds 
115355e957d8STakashi Iwai static int snd_hdsp_midi_output_possible (struct hdsp *hdsp, int id)
11541da177e4SLinus Torvalds {
11551da177e4SLinus Torvalds 	int fifo_bytes_used;
11561da177e4SLinus Torvalds 
1157b0b98119STakashi Iwai 	if (id)
11581da177e4SLinus Torvalds 		fifo_bytes_used = hdsp_read(hdsp, HDSP_midiStatusOut1) & 0xff;
1159b0b98119STakashi Iwai 	else
11601da177e4SLinus Torvalds 		fifo_bytes_used = hdsp_read(hdsp, HDSP_midiStatusOut0) & 0xff;
11611da177e4SLinus Torvalds 
1162b0b98119STakashi Iwai 	if (fifo_bytes_used < 128)
11631da177e4SLinus Torvalds 		return  128 - fifo_bytes_used;
1164b0b98119STakashi Iwai 	else
11651da177e4SLinus Torvalds 		return 0;
11661da177e4SLinus Torvalds }
11671da177e4SLinus Torvalds 
116855e957d8STakashi Iwai static void snd_hdsp_flush_midi_input (struct hdsp *hdsp, int id)
11691da177e4SLinus Torvalds {
1170b0b98119STakashi Iwai 	while (snd_hdsp_midi_input_available (hdsp, id))
11711da177e4SLinus Torvalds 		snd_hdsp_midi_read_byte (hdsp, id);
11721da177e4SLinus Torvalds }
11731da177e4SLinus Torvalds 
117455e957d8STakashi Iwai static int snd_hdsp_midi_output_write (struct hdsp_midi *hmidi)
11751da177e4SLinus Torvalds {
11761da177e4SLinus Torvalds 	unsigned long flags;
11771da177e4SLinus Torvalds 	int n_pending;
11781da177e4SLinus Torvalds 	int to_write;
11791da177e4SLinus Torvalds 	int i;
11801da177e4SLinus Torvalds 	unsigned char buf[128];
11811da177e4SLinus Torvalds 
11821da177e4SLinus Torvalds 	/* Output is not interrupt driven */
11831da177e4SLinus Torvalds 
11841da177e4SLinus Torvalds 	spin_lock_irqsave (&hmidi->lock, flags);
11851da177e4SLinus Torvalds 	if (hmidi->output) {
11861da177e4SLinus Torvalds 		if (!snd_rawmidi_transmit_empty (hmidi->output)) {
11871da177e4SLinus Torvalds 			if ((n_pending = snd_hdsp_midi_output_possible (hmidi->hdsp, hmidi->id)) > 0) {
11881da177e4SLinus Torvalds 				if (n_pending > (int)sizeof (buf))
11891da177e4SLinus Torvalds 					n_pending = sizeof (buf);
11901da177e4SLinus Torvalds 
11911da177e4SLinus Torvalds 				if ((to_write = snd_rawmidi_transmit (hmidi->output, buf, n_pending)) > 0) {
11921da177e4SLinus Torvalds 					for (i = 0; i < to_write; ++i)
11931da177e4SLinus Torvalds 						snd_hdsp_midi_write_byte (hmidi->hdsp, hmidi->id, buf[i]);
11941da177e4SLinus Torvalds 				}
11951da177e4SLinus Torvalds 			}
11961da177e4SLinus Torvalds 		}
11971da177e4SLinus Torvalds 	}
11981da177e4SLinus Torvalds 	spin_unlock_irqrestore (&hmidi->lock, flags);
11991da177e4SLinus Torvalds 	return 0;
12001da177e4SLinus Torvalds }
12011da177e4SLinus Torvalds 
120255e957d8STakashi Iwai static int snd_hdsp_midi_input_read (struct hdsp_midi *hmidi)
12031da177e4SLinus Torvalds {
12041da177e4SLinus Torvalds 	unsigned char buf[128]; /* this buffer is designed to match the MIDI input FIFO size */
12051da177e4SLinus Torvalds 	unsigned long flags;
12061da177e4SLinus Torvalds 	int n_pending;
12071da177e4SLinus Torvalds 	int i;
12081da177e4SLinus Torvalds 
12091da177e4SLinus Torvalds 	spin_lock_irqsave (&hmidi->lock, flags);
12101da177e4SLinus Torvalds 	if ((n_pending = snd_hdsp_midi_input_available (hmidi->hdsp, hmidi->id)) > 0) {
12111da177e4SLinus Torvalds 		if (hmidi->input) {
1212b0b98119STakashi Iwai 			if (n_pending > (int)sizeof (buf))
12131da177e4SLinus Torvalds 				n_pending = sizeof (buf);
1214b0b98119STakashi Iwai 			for (i = 0; i < n_pending; ++i)
12151da177e4SLinus Torvalds 				buf[i] = snd_hdsp_midi_read_byte (hmidi->hdsp, hmidi->id);
1216b0b98119STakashi Iwai 			if (n_pending)
12171da177e4SLinus Torvalds 				snd_rawmidi_receive (hmidi->input, buf, n_pending);
12181da177e4SLinus Torvalds 		} else {
12191da177e4SLinus Torvalds 			/* flush the MIDI input FIFO */
1220b0b98119STakashi Iwai 			while (--n_pending)
12211da177e4SLinus Torvalds 				snd_hdsp_midi_read_byte (hmidi->hdsp, hmidi->id);
12221da177e4SLinus Torvalds 		}
12231da177e4SLinus Torvalds 	}
12241da177e4SLinus Torvalds 	hmidi->pending = 0;
1225b0b98119STakashi Iwai 	if (hmidi->id)
12261da177e4SLinus Torvalds 		hmidi->hdsp->control_register |= HDSP_Midi1InterruptEnable;
1227b0b98119STakashi Iwai 	else
12281da177e4SLinus Torvalds 		hmidi->hdsp->control_register |= HDSP_Midi0InterruptEnable;
12291da177e4SLinus Torvalds 	hdsp_write(hmidi->hdsp, HDSP_controlRegister, hmidi->hdsp->control_register);
12301da177e4SLinus Torvalds 	spin_unlock_irqrestore (&hmidi->lock, flags);
12311da177e4SLinus Torvalds 	return snd_hdsp_midi_output_write (hmidi);
12321da177e4SLinus Torvalds }
12331da177e4SLinus Torvalds 
123455e957d8STakashi Iwai static void snd_hdsp_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
12351da177e4SLinus Torvalds {
123655e957d8STakashi Iwai 	struct hdsp *hdsp;
123755e957d8STakashi Iwai 	struct hdsp_midi *hmidi;
12381da177e4SLinus Torvalds 	unsigned long flags;
12391da177e4SLinus Torvalds 	u32 ie;
12401da177e4SLinus Torvalds 
124155e957d8STakashi Iwai 	hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
12421da177e4SLinus Torvalds 	hdsp = hmidi->hdsp;
12431da177e4SLinus Torvalds 	ie = hmidi->id ? HDSP_Midi1InterruptEnable : HDSP_Midi0InterruptEnable;
12441da177e4SLinus Torvalds 	spin_lock_irqsave (&hdsp->lock, flags);
12451da177e4SLinus Torvalds 	if (up) {
12461da177e4SLinus Torvalds 		if (!(hdsp->control_register & ie)) {
12471da177e4SLinus Torvalds 			snd_hdsp_flush_midi_input (hdsp, hmidi->id);
12481da177e4SLinus Torvalds 			hdsp->control_register |= ie;
12491da177e4SLinus Torvalds 		}
12501da177e4SLinus Torvalds 	} else {
12511da177e4SLinus Torvalds 		hdsp->control_register &= ~ie;
12521da177e4SLinus Torvalds 		tasklet_kill(&hdsp->midi_tasklet);
12531da177e4SLinus Torvalds 	}
12541da177e4SLinus Torvalds 
12551da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
12561da177e4SLinus Torvalds 	spin_unlock_irqrestore (&hdsp->lock, flags);
12571da177e4SLinus Torvalds }
12581da177e4SLinus Torvalds 
12591da177e4SLinus Torvalds static void snd_hdsp_midi_output_timer(unsigned long data)
12601da177e4SLinus Torvalds {
126155e957d8STakashi Iwai 	struct hdsp_midi *hmidi = (struct hdsp_midi *) data;
12621da177e4SLinus Torvalds 	unsigned long flags;
12631da177e4SLinus Torvalds 
12641da177e4SLinus Torvalds 	snd_hdsp_midi_output_write(hmidi);
12651da177e4SLinus Torvalds 	spin_lock_irqsave (&hmidi->lock, flags);
12661da177e4SLinus Torvalds 
12671da177e4SLinus Torvalds 	/* this does not bump hmidi->istimer, because the
12681da177e4SLinus Torvalds 	   kernel automatically removed the timer when it
12691da177e4SLinus Torvalds 	   expired, and we are now adding it back, thus
12701da177e4SLinus Torvalds 	   leaving istimer wherever it was set before.
12711da177e4SLinus Torvalds 	*/
12721da177e4SLinus Torvalds 
12731da177e4SLinus Torvalds 	if (hmidi->istimer) {
12741da177e4SLinus Torvalds 		hmidi->timer.expires = 1 + jiffies;
12751da177e4SLinus Torvalds 		add_timer(&hmidi->timer);
12761da177e4SLinus Torvalds 	}
12771da177e4SLinus Torvalds 
12781da177e4SLinus Torvalds 	spin_unlock_irqrestore (&hmidi->lock, flags);
12791da177e4SLinus Torvalds }
12801da177e4SLinus Torvalds 
128155e957d8STakashi Iwai static void snd_hdsp_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
12821da177e4SLinus Torvalds {
128355e957d8STakashi Iwai 	struct hdsp_midi *hmidi;
12841da177e4SLinus Torvalds 	unsigned long flags;
12851da177e4SLinus Torvalds 
128655e957d8STakashi Iwai 	hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
12871da177e4SLinus Torvalds 	spin_lock_irqsave (&hmidi->lock, flags);
12881da177e4SLinus Torvalds 	if (up) {
12891da177e4SLinus Torvalds 		if (!hmidi->istimer) {
12901da177e4SLinus Torvalds 			init_timer(&hmidi->timer);
12911da177e4SLinus Torvalds 			hmidi->timer.function = snd_hdsp_midi_output_timer;
12921da177e4SLinus Torvalds 			hmidi->timer.data = (unsigned long) hmidi;
12931da177e4SLinus Torvalds 			hmidi->timer.expires = 1 + jiffies;
12941da177e4SLinus Torvalds 			add_timer(&hmidi->timer);
12951da177e4SLinus Torvalds 			hmidi->istimer++;
12961da177e4SLinus Torvalds 		}
12971da177e4SLinus Torvalds 	} else {
1298b0b98119STakashi Iwai 		if (hmidi->istimer && --hmidi->istimer <= 0)
12991da177e4SLinus Torvalds 			del_timer (&hmidi->timer);
13001da177e4SLinus Torvalds 	}
13011da177e4SLinus Torvalds 	spin_unlock_irqrestore (&hmidi->lock, flags);
13021da177e4SLinus Torvalds 	if (up)
13031da177e4SLinus Torvalds 		snd_hdsp_midi_output_write(hmidi);
13041da177e4SLinus Torvalds }
13051da177e4SLinus Torvalds 
130655e957d8STakashi Iwai static int snd_hdsp_midi_input_open(struct snd_rawmidi_substream *substream)
13071da177e4SLinus Torvalds {
130855e957d8STakashi Iwai 	struct hdsp_midi *hmidi;
13091da177e4SLinus Torvalds 
131055e957d8STakashi Iwai 	hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
13111da177e4SLinus Torvalds 	spin_lock_irq (&hmidi->lock);
13121da177e4SLinus Torvalds 	snd_hdsp_flush_midi_input (hmidi->hdsp, hmidi->id);
13131da177e4SLinus Torvalds 	hmidi->input = substream;
13141da177e4SLinus Torvalds 	spin_unlock_irq (&hmidi->lock);
13151da177e4SLinus Torvalds 
13161da177e4SLinus Torvalds 	return 0;
13171da177e4SLinus Torvalds }
13181da177e4SLinus Torvalds 
131955e957d8STakashi Iwai static int snd_hdsp_midi_output_open(struct snd_rawmidi_substream *substream)
13201da177e4SLinus Torvalds {
132155e957d8STakashi Iwai 	struct hdsp_midi *hmidi;
13221da177e4SLinus Torvalds 
132355e957d8STakashi Iwai 	hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
13241da177e4SLinus Torvalds 	spin_lock_irq (&hmidi->lock);
13251da177e4SLinus Torvalds 	hmidi->output = substream;
13261da177e4SLinus Torvalds 	spin_unlock_irq (&hmidi->lock);
13271da177e4SLinus Torvalds 
13281da177e4SLinus Torvalds 	return 0;
13291da177e4SLinus Torvalds }
13301da177e4SLinus Torvalds 
133155e957d8STakashi Iwai static int snd_hdsp_midi_input_close(struct snd_rawmidi_substream *substream)
13321da177e4SLinus Torvalds {
133355e957d8STakashi Iwai 	struct hdsp_midi *hmidi;
13341da177e4SLinus Torvalds 
13351da177e4SLinus Torvalds 	snd_hdsp_midi_input_trigger (substream, 0);
13361da177e4SLinus Torvalds 
133755e957d8STakashi Iwai 	hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
13381da177e4SLinus Torvalds 	spin_lock_irq (&hmidi->lock);
13391da177e4SLinus Torvalds 	hmidi->input = NULL;
13401da177e4SLinus Torvalds 	spin_unlock_irq (&hmidi->lock);
13411da177e4SLinus Torvalds 
13421da177e4SLinus Torvalds 	return 0;
13431da177e4SLinus Torvalds }
13441da177e4SLinus Torvalds 
134555e957d8STakashi Iwai static int snd_hdsp_midi_output_close(struct snd_rawmidi_substream *substream)
13461da177e4SLinus Torvalds {
134755e957d8STakashi Iwai 	struct hdsp_midi *hmidi;
13481da177e4SLinus Torvalds 
13491da177e4SLinus Torvalds 	snd_hdsp_midi_output_trigger (substream, 0);
13501da177e4SLinus Torvalds 
135155e957d8STakashi Iwai 	hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
13521da177e4SLinus Torvalds 	spin_lock_irq (&hmidi->lock);
13531da177e4SLinus Torvalds 	hmidi->output = NULL;
13541da177e4SLinus Torvalds 	spin_unlock_irq (&hmidi->lock);
13551da177e4SLinus Torvalds 
13561da177e4SLinus Torvalds 	return 0;
13571da177e4SLinus Torvalds }
13581da177e4SLinus Torvalds 
135955e957d8STakashi Iwai static struct snd_rawmidi_ops snd_hdsp_midi_output =
13601da177e4SLinus Torvalds {
13611da177e4SLinus Torvalds 	.open =		snd_hdsp_midi_output_open,
13621da177e4SLinus Torvalds 	.close =	snd_hdsp_midi_output_close,
13631da177e4SLinus Torvalds 	.trigger =	snd_hdsp_midi_output_trigger,
13641da177e4SLinus Torvalds };
13651da177e4SLinus Torvalds 
136655e957d8STakashi Iwai static struct snd_rawmidi_ops snd_hdsp_midi_input =
13671da177e4SLinus Torvalds {
13681da177e4SLinus Torvalds 	.open =		snd_hdsp_midi_input_open,
13691da177e4SLinus Torvalds 	.close =	snd_hdsp_midi_input_close,
13701da177e4SLinus Torvalds 	.trigger =	snd_hdsp_midi_input_trigger,
13711da177e4SLinus Torvalds };
13721da177e4SLinus Torvalds 
1373f40b6890STakashi Iwai static int snd_hdsp_create_midi (struct snd_card *card, struct hdsp *hdsp, int id)
13741da177e4SLinus Torvalds {
13751da177e4SLinus Torvalds 	char buf[32];
13761da177e4SLinus Torvalds 
13771da177e4SLinus Torvalds 	hdsp->midi[id].id = id;
13781da177e4SLinus Torvalds 	hdsp->midi[id].rmidi = NULL;
13791da177e4SLinus Torvalds 	hdsp->midi[id].input = NULL;
13801da177e4SLinus Torvalds 	hdsp->midi[id].output = NULL;
13811da177e4SLinus Torvalds 	hdsp->midi[id].hdsp = hdsp;
13821da177e4SLinus Torvalds 	hdsp->midi[id].istimer = 0;
13831da177e4SLinus Torvalds 	hdsp->midi[id].pending = 0;
13841da177e4SLinus Torvalds 	spin_lock_init (&hdsp->midi[id].lock);
13851da177e4SLinus Torvalds 
13861da177e4SLinus Torvalds 	sprintf (buf, "%s MIDI %d", card->shortname, id+1);
1387b0b98119STakashi Iwai 	if (snd_rawmidi_new (card, buf, id, 1, 1, &hdsp->midi[id].rmidi) < 0)
13881da177e4SLinus Torvalds 		return -1;
13891da177e4SLinus Torvalds 
13901da177e4SLinus Torvalds 	sprintf (hdsp->midi[id].rmidi->name, "%s MIDI %d", card->id, id+1);
13911da177e4SLinus Torvalds 	hdsp->midi[id].rmidi->private_data = &hdsp->midi[id];
13921da177e4SLinus Torvalds 
13931da177e4SLinus Torvalds 	snd_rawmidi_set_ops (hdsp->midi[id].rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_hdsp_midi_output);
13941da177e4SLinus Torvalds 	snd_rawmidi_set_ops (hdsp->midi[id].rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_hdsp_midi_input);
13951da177e4SLinus Torvalds 
13961da177e4SLinus Torvalds 	hdsp->midi[id].rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT |
13971da177e4SLinus Torvalds 		SNDRV_RAWMIDI_INFO_INPUT |
13981da177e4SLinus Torvalds 		SNDRV_RAWMIDI_INFO_DUPLEX;
13991da177e4SLinus Torvalds 
14001da177e4SLinus Torvalds 	return 0;
14011da177e4SLinus Torvalds }
14021da177e4SLinus Torvalds 
14031da177e4SLinus Torvalds /*-----------------------------------------------------------------------------
14041da177e4SLinus Torvalds   Control Interface
14051da177e4SLinus Torvalds   ----------------------------------------------------------------------------*/
14061da177e4SLinus Torvalds 
140755e957d8STakashi Iwai static u32 snd_hdsp_convert_from_aes(struct snd_aes_iec958 *aes)
14081da177e4SLinus Torvalds {
14091da177e4SLinus Torvalds 	u32 val = 0;
14101da177e4SLinus Torvalds 	val |= (aes->status[0] & IEC958_AES0_PROFESSIONAL) ? HDSP_SPDIFProfessional : 0;
14111da177e4SLinus Torvalds 	val |= (aes->status[0] & IEC958_AES0_NONAUDIO) ? HDSP_SPDIFNonAudio : 0;
14121da177e4SLinus Torvalds 	if (val & HDSP_SPDIFProfessional)
14131da177e4SLinus Torvalds 		val |= (aes->status[0] & IEC958_AES0_PRO_EMPHASIS_5015) ? HDSP_SPDIFEmphasis : 0;
14141da177e4SLinus Torvalds 	else
14151da177e4SLinus Torvalds 		val |= (aes->status[0] & IEC958_AES0_CON_EMPHASIS_5015) ? HDSP_SPDIFEmphasis : 0;
14161da177e4SLinus Torvalds 	return val;
14171da177e4SLinus Torvalds }
14181da177e4SLinus Torvalds 
141955e957d8STakashi Iwai static void snd_hdsp_convert_to_aes(struct snd_aes_iec958 *aes, u32 val)
14201da177e4SLinus Torvalds {
14211da177e4SLinus Torvalds 	aes->status[0] = ((val & HDSP_SPDIFProfessional) ? IEC958_AES0_PROFESSIONAL : 0) |
14221da177e4SLinus Torvalds 			 ((val & HDSP_SPDIFNonAudio) ? IEC958_AES0_NONAUDIO : 0);
14231da177e4SLinus Torvalds 	if (val & HDSP_SPDIFProfessional)
14241da177e4SLinus Torvalds 		aes->status[0] |= (val & HDSP_SPDIFEmphasis) ? IEC958_AES0_PRO_EMPHASIS_5015 : 0;
14251da177e4SLinus Torvalds 	else
14261da177e4SLinus Torvalds 		aes->status[0] |= (val & HDSP_SPDIFEmphasis) ? IEC958_AES0_CON_EMPHASIS_5015 : 0;
14271da177e4SLinus Torvalds }
14281da177e4SLinus Torvalds 
142955e957d8STakashi Iwai static int snd_hdsp_control_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
14301da177e4SLinus Torvalds {
14311da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
14321da177e4SLinus Torvalds 	uinfo->count = 1;
14331da177e4SLinus Torvalds 	return 0;
14341da177e4SLinus Torvalds }
14351da177e4SLinus Torvalds 
143655e957d8STakashi Iwai static int snd_hdsp_control_spdif_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
14371da177e4SLinus Torvalds {
143855e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
14391da177e4SLinus Torvalds 
14401da177e4SLinus Torvalds 	snd_hdsp_convert_to_aes(&ucontrol->value.iec958, hdsp->creg_spdif);
14411da177e4SLinus Torvalds 	return 0;
14421da177e4SLinus Torvalds }
14431da177e4SLinus Torvalds 
144455e957d8STakashi Iwai static int snd_hdsp_control_spdif_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
14451da177e4SLinus Torvalds {
144655e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
14471da177e4SLinus Torvalds 	int change;
14481da177e4SLinus Torvalds 	u32 val;
14491da177e4SLinus Torvalds 
14501da177e4SLinus Torvalds 	val = snd_hdsp_convert_from_aes(&ucontrol->value.iec958);
14511da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
14521da177e4SLinus Torvalds 	change = val != hdsp->creg_spdif;
14531da177e4SLinus Torvalds 	hdsp->creg_spdif = val;
14541da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
14551da177e4SLinus Torvalds 	return change;
14561da177e4SLinus Torvalds }
14571da177e4SLinus Torvalds 
145855e957d8STakashi Iwai static int snd_hdsp_control_spdif_stream_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
14591da177e4SLinus Torvalds {
14601da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
14611da177e4SLinus Torvalds 	uinfo->count = 1;
14621da177e4SLinus Torvalds 	return 0;
14631da177e4SLinus Torvalds }
14641da177e4SLinus Torvalds 
146555e957d8STakashi Iwai static int snd_hdsp_control_spdif_stream_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
14661da177e4SLinus Torvalds {
146755e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
14681da177e4SLinus Torvalds 
14691da177e4SLinus Torvalds 	snd_hdsp_convert_to_aes(&ucontrol->value.iec958, hdsp->creg_spdif_stream);
14701da177e4SLinus Torvalds 	return 0;
14711da177e4SLinus Torvalds }
14721da177e4SLinus Torvalds 
147355e957d8STakashi Iwai static int snd_hdsp_control_spdif_stream_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
14741da177e4SLinus Torvalds {
147555e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
14761da177e4SLinus Torvalds 	int change;
14771da177e4SLinus Torvalds 	u32 val;
14781da177e4SLinus Torvalds 
14791da177e4SLinus Torvalds 	val = snd_hdsp_convert_from_aes(&ucontrol->value.iec958);
14801da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
14811da177e4SLinus Torvalds 	change = val != hdsp->creg_spdif_stream;
14821da177e4SLinus Torvalds 	hdsp->creg_spdif_stream = val;
14831da177e4SLinus Torvalds 	hdsp->control_register &= ~(HDSP_SPDIFProfessional | HDSP_SPDIFNonAudio | HDSP_SPDIFEmphasis);
14841da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register |= val);
14851da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
14861da177e4SLinus Torvalds 	return change;
14871da177e4SLinus Torvalds }
14881da177e4SLinus Torvalds 
148955e957d8STakashi Iwai static int snd_hdsp_control_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
14901da177e4SLinus Torvalds {
14911da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
14921da177e4SLinus Torvalds 	uinfo->count = 1;
14931da177e4SLinus Torvalds 	return 0;
14941da177e4SLinus Torvalds }
14951da177e4SLinus Torvalds 
149655e957d8STakashi Iwai static int snd_hdsp_control_spdif_mask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
14971da177e4SLinus Torvalds {
14981da177e4SLinus Torvalds 	ucontrol->value.iec958.status[0] = kcontrol->private_value;
14991da177e4SLinus Torvalds 	return 0;
15001da177e4SLinus Torvalds }
15011da177e4SLinus Torvalds 
15021da177e4SLinus Torvalds #define HDSP_SPDIF_IN(xname, xindex) \
150367ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
15041da177e4SLinus Torvalds   .name = xname, \
15051da177e4SLinus Torvalds   .index = xindex, \
15061da177e4SLinus Torvalds   .info = snd_hdsp_info_spdif_in, \
15071da177e4SLinus Torvalds   .get = snd_hdsp_get_spdif_in, \
15081da177e4SLinus Torvalds   .put = snd_hdsp_put_spdif_in }
15091da177e4SLinus Torvalds 
151055e957d8STakashi Iwai static unsigned int hdsp_spdif_in(struct hdsp *hdsp)
15111da177e4SLinus Torvalds {
15121da177e4SLinus Torvalds 	return hdsp_decode_spdif_in(hdsp->control_register & HDSP_SPDIFInputMask);
15131da177e4SLinus Torvalds }
15141da177e4SLinus Torvalds 
151555e957d8STakashi Iwai static int hdsp_set_spdif_input(struct hdsp *hdsp, int in)
15161da177e4SLinus Torvalds {
15171da177e4SLinus Torvalds 	hdsp->control_register &= ~HDSP_SPDIFInputMask;
15181da177e4SLinus Torvalds 	hdsp->control_register |= hdsp_encode_spdif_in(in);
15191da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
15201da177e4SLinus Torvalds 	return 0;
15211da177e4SLinus Torvalds }
15221da177e4SLinus Torvalds 
152355e957d8STakashi Iwai static int snd_hdsp_info_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
15241da177e4SLinus Torvalds {
15251da177e4SLinus Torvalds 	static char *texts[4] = {"Optical", "Coaxial", "Internal", "AES"};
152655e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
15271da177e4SLinus Torvalds 
15281da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
15291da177e4SLinus Torvalds 	uinfo->count = 1;
15301da177e4SLinus Torvalds 	uinfo->value.enumerated.items = ((hdsp->io_type == H9632) ? 4 : 3);
15311da177e4SLinus Torvalds 	if (uinfo->value.enumerated.item > ((hdsp->io_type == H9632) ? 3 : 2))
15321da177e4SLinus Torvalds 		uinfo->value.enumerated.item = ((hdsp->io_type == H9632) ? 3 : 2);
15331da177e4SLinus Torvalds 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
15341da177e4SLinus Torvalds 	return 0;
15351da177e4SLinus Torvalds }
15361da177e4SLinus Torvalds 
153755e957d8STakashi Iwai static int snd_hdsp_get_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
15381da177e4SLinus Torvalds {
153955e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
15401da177e4SLinus Torvalds 
15411da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp_spdif_in(hdsp);
15421da177e4SLinus Torvalds 	return 0;
15431da177e4SLinus Torvalds }
15441da177e4SLinus Torvalds 
154555e957d8STakashi Iwai static int snd_hdsp_put_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
15461da177e4SLinus Torvalds {
154755e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
15481da177e4SLinus Torvalds 	int change;
15491da177e4SLinus Torvalds 	unsigned int val;
15501da177e4SLinus Torvalds 
15511da177e4SLinus Torvalds 	if (!snd_hdsp_use_is_exclusive(hdsp))
15521da177e4SLinus Torvalds 		return -EBUSY;
15531da177e4SLinus Torvalds 	val = ucontrol->value.enumerated.item[0] % ((hdsp->io_type == H9632) ? 4 : 3);
15541da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
15551da177e4SLinus Torvalds 	change = val != hdsp_spdif_in(hdsp);
15561da177e4SLinus Torvalds 	if (change)
15571da177e4SLinus Torvalds 		hdsp_set_spdif_input(hdsp, val);
15581da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
15591da177e4SLinus Torvalds 	return change;
15601da177e4SLinus Torvalds }
15611da177e4SLinus Torvalds 
15621da177e4SLinus Torvalds #define HDSP_SPDIF_OUT(xname, xindex) \
156367ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
15641da177e4SLinus Torvalds   .info = snd_hdsp_info_spdif_bits, \
15651da177e4SLinus Torvalds   .get = snd_hdsp_get_spdif_out, .put = snd_hdsp_put_spdif_out }
15661da177e4SLinus Torvalds 
156755e957d8STakashi Iwai static int hdsp_spdif_out(struct hdsp *hdsp)
15681da177e4SLinus Torvalds {
15691da177e4SLinus Torvalds 	return (hdsp->control_register & HDSP_SPDIFOpticalOut) ? 1 : 0;
15701da177e4SLinus Torvalds }
15711da177e4SLinus Torvalds 
157255e957d8STakashi Iwai static int hdsp_set_spdif_output(struct hdsp *hdsp, int out)
15731da177e4SLinus Torvalds {
1574b0b98119STakashi Iwai 	if (out)
15751da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_SPDIFOpticalOut;
1576b0b98119STakashi Iwai 	else
15771da177e4SLinus Torvalds 		hdsp->control_register &= ~HDSP_SPDIFOpticalOut;
15781da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
15791da177e4SLinus Torvalds 	return 0;
15801da177e4SLinus Torvalds }
15811da177e4SLinus Torvalds 
158255e957d8STakashi Iwai static int snd_hdsp_info_spdif_bits(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
15831da177e4SLinus Torvalds {
15841da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
15851da177e4SLinus Torvalds 	uinfo->count = 1;
15861da177e4SLinus Torvalds 	uinfo->value.integer.min = 0;
15871da177e4SLinus Torvalds 	uinfo->value.integer.max = 1;
15881da177e4SLinus Torvalds 	return 0;
15891da177e4SLinus Torvalds }
15901da177e4SLinus Torvalds 
159155e957d8STakashi Iwai static int snd_hdsp_get_spdif_out(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
15921da177e4SLinus Torvalds {
159355e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
15941da177e4SLinus Torvalds 
15951da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = hdsp_spdif_out(hdsp);
15961da177e4SLinus Torvalds 	return 0;
15971da177e4SLinus Torvalds }
15981da177e4SLinus Torvalds 
159955e957d8STakashi Iwai static int snd_hdsp_put_spdif_out(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
16001da177e4SLinus Torvalds {
160155e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
16021da177e4SLinus Torvalds 	int change;
16031da177e4SLinus Torvalds 	unsigned int val;
16041da177e4SLinus Torvalds 
16051da177e4SLinus Torvalds 	if (!snd_hdsp_use_is_exclusive(hdsp))
16061da177e4SLinus Torvalds 		return -EBUSY;
16071da177e4SLinus Torvalds 	val = ucontrol->value.integer.value[0] & 1;
16081da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
16091da177e4SLinus Torvalds 	change = (int)val != hdsp_spdif_out(hdsp);
16101da177e4SLinus Torvalds 	hdsp_set_spdif_output(hdsp, val);
16111da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
16121da177e4SLinus Torvalds 	return change;
16131da177e4SLinus Torvalds }
16141da177e4SLinus Torvalds 
16151da177e4SLinus Torvalds #define HDSP_SPDIF_PROFESSIONAL(xname, xindex) \
161667ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
16171da177e4SLinus Torvalds   .info = snd_hdsp_info_spdif_bits, \
16181da177e4SLinus Torvalds   .get = snd_hdsp_get_spdif_professional, .put = snd_hdsp_put_spdif_professional }
16191da177e4SLinus Torvalds 
162055e957d8STakashi Iwai static int hdsp_spdif_professional(struct hdsp *hdsp)
16211da177e4SLinus Torvalds {
16221da177e4SLinus Torvalds 	return (hdsp->control_register & HDSP_SPDIFProfessional) ? 1 : 0;
16231da177e4SLinus Torvalds }
16241da177e4SLinus Torvalds 
162555e957d8STakashi Iwai static int hdsp_set_spdif_professional(struct hdsp *hdsp, int val)
16261da177e4SLinus Torvalds {
1627b0b98119STakashi Iwai 	if (val)
16281da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_SPDIFProfessional;
1629b0b98119STakashi Iwai 	else
16301da177e4SLinus Torvalds 		hdsp->control_register &= ~HDSP_SPDIFProfessional;
16311da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
16321da177e4SLinus Torvalds 	return 0;
16331da177e4SLinus Torvalds }
16341da177e4SLinus Torvalds 
163555e957d8STakashi Iwai static int snd_hdsp_get_spdif_professional(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
16361da177e4SLinus Torvalds {
163755e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
16381da177e4SLinus Torvalds 
16391da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = hdsp_spdif_professional(hdsp);
16401da177e4SLinus Torvalds 	return 0;
16411da177e4SLinus Torvalds }
16421da177e4SLinus Torvalds 
164355e957d8STakashi Iwai static int snd_hdsp_put_spdif_professional(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
16441da177e4SLinus Torvalds {
164555e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
16461da177e4SLinus Torvalds 	int change;
16471da177e4SLinus Torvalds 	unsigned int val;
16481da177e4SLinus Torvalds 
16491da177e4SLinus Torvalds 	if (!snd_hdsp_use_is_exclusive(hdsp))
16501da177e4SLinus Torvalds 		return -EBUSY;
16511da177e4SLinus Torvalds 	val = ucontrol->value.integer.value[0] & 1;
16521da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
16531da177e4SLinus Torvalds 	change = (int)val != hdsp_spdif_professional(hdsp);
16541da177e4SLinus Torvalds 	hdsp_set_spdif_professional(hdsp, val);
16551da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
16561da177e4SLinus Torvalds 	return change;
16571da177e4SLinus Torvalds }
16581da177e4SLinus Torvalds 
16591da177e4SLinus Torvalds #define HDSP_SPDIF_EMPHASIS(xname, xindex) \
166067ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
16611da177e4SLinus Torvalds   .info = snd_hdsp_info_spdif_bits, \
16621da177e4SLinus Torvalds   .get = snd_hdsp_get_spdif_emphasis, .put = snd_hdsp_put_spdif_emphasis }
16631da177e4SLinus Torvalds 
166455e957d8STakashi Iwai static int hdsp_spdif_emphasis(struct hdsp *hdsp)
16651da177e4SLinus Torvalds {
16661da177e4SLinus Torvalds 	return (hdsp->control_register & HDSP_SPDIFEmphasis) ? 1 : 0;
16671da177e4SLinus Torvalds }
16681da177e4SLinus Torvalds 
166955e957d8STakashi Iwai static int hdsp_set_spdif_emphasis(struct hdsp *hdsp, int val)
16701da177e4SLinus Torvalds {
1671b0b98119STakashi Iwai 	if (val)
16721da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_SPDIFEmphasis;
1673b0b98119STakashi Iwai 	else
16741da177e4SLinus Torvalds 		hdsp->control_register &= ~HDSP_SPDIFEmphasis;
16751da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
16761da177e4SLinus Torvalds 	return 0;
16771da177e4SLinus Torvalds }
16781da177e4SLinus Torvalds 
167955e957d8STakashi Iwai static int snd_hdsp_get_spdif_emphasis(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
16801da177e4SLinus Torvalds {
168155e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
16821da177e4SLinus Torvalds 
16831da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = hdsp_spdif_emphasis(hdsp);
16841da177e4SLinus Torvalds 	return 0;
16851da177e4SLinus Torvalds }
16861da177e4SLinus Torvalds 
168755e957d8STakashi Iwai static int snd_hdsp_put_spdif_emphasis(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
16881da177e4SLinus Torvalds {
168955e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
16901da177e4SLinus Torvalds 	int change;
16911da177e4SLinus Torvalds 	unsigned int val;
16921da177e4SLinus Torvalds 
16931da177e4SLinus Torvalds 	if (!snd_hdsp_use_is_exclusive(hdsp))
16941da177e4SLinus Torvalds 		return -EBUSY;
16951da177e4SLinus Torvalds 	val = ucontrol->value.integer.value[0] & 1;
16961da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
16971da177e4SLinus Torvalds 	change = (int)val != hdsp_spdif_emphasis(hdsp);
16981da177e4SLinus Torvalds 	hdsp_set_spdif_emphasis(hdsp, val);
16991da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
17001da177e4SLinus Torvalds 	return change;
17011da177e4SLinus Torvalds }
17021da177e4SLinus Torvalds 
17031da177e4SLinus Torvalds #define HDSP_SPDIF_NON_AUDIO(xname, xindex) \
170467ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
17051da177e4SLinus Torvalds   .info = snd_hdsp_info_spdif_bits, \
17061da177e4SLinus Torvalds   .get = snd_hdsp_get_spdif_nonaudio, .put = snd_hdsp_put_spdif_nonaudio }
17071da177e4SLinus Torvalds 
170855e957d8STakashi Iwai static int hdsp_spdif_nonaudio(struct hdsp *hdsp)
17091da177e4SLinus Torvalds {
17101da177e4SLinus Torvalds 	return (hdsp->control_register & HDSP_SPDIFNonAudio) ? 1 : 0;
17111da177e4SLinus Torvalds }
17121da177e4SLinus Torvalds 
171355e957d8STakashi Iwai static int hdsp_set_spdif_nonaudio(struct hdsp *hdsp, int val)
17141da177e4SLinus Torvalds {
1715b0b98119STakashi Iwai 	if (val)
17161da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_SPDIFNonAudio;
1717b0b98119STakashi Iwai 	else
17181da177e4SLinus Torvalds 		hdsp->control_register &= ~HDSP_SPDIFNonAudio;
17191da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
17201da177e4SLinus Torvalds 	return 0;
17211da177e4SLinus Torvalds }
17221da177e4SLinus Torvalds 
172355e957d8STakashi Iwai static int snd_hdsp_get_spdif_nonaudio(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
17241da177e4SLinus Torvalds {
172555e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
17261da177e4SLinus Torvalds 
17271da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = hdsp_spdif_nonaudio(hdsp);
17281da177e4SLinus Torvalds 	return 0;
17291da177e4SLinus Torvalds }
17301da177e4SLinus Torvalds 
173155e957d8STakashi Iwai static int snd_hdsp_put_spdif_nonaudio(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
17321da177e4SLinus Torvalds {
173355e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
17341da177e4SLinus Torvalds 	int change;
17351da177e4SLinus Torvalds 	unsigned int val;
17361da177e4SLinus Torvalds 
17371da177e4SLinus Torvalds 	if (!snd_hdsp_use_is_exclusive(hdsp))
17381da177e4SLinus Torvalds 		return -EBUSY;
17391da177e4SLinus Torvalds 	val = ucontrol->value.integer.value[0] & 1;
17401da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
17411da177e4SLinus Torvalds 	change = (int)val != hdsp_spdif_nonaudio(hdsp);
17421da177e4SLinus Torvalds 	hdsp_set_spdif_nonaudio(hdsp, val);
17431da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
17441da177e4SLinus Torvalds 	return change;
17451da177e4SLinus Torvalds }
17461da177e4SLinus Torvalds 
17471da177e4SLinus Torvalds #define HDSP_SPDIF_SAMPLE_RATE(xname, xindex) \
174867ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
17491da177e4SLinus Torvalds   .name = xname, \
17501da177e4SLinus Torvalds   .index = xindex, \
17511da177e4SLinus Torvalds   .access = SNDRV_CTL_ELEM_ACCESS_READ, \
17521da177e4SLinus Torvalds   .info = snd_hdsp_info_spdif_sample_rate, \
17531da177e4SLinus Torvalds   .get = snd_hdsp_get_spdif_sample_rate \
17541da177e4SLinus Torvalds }
17551da177e4SLinus Torvalds 
175655e957d8STakashi Iwai static int snd_hdsp_info_spdif_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
17571da177e4SLinus Torvalds {
17581da177e4SLinus Torvalds 	static char *texts[] = {"32000", "44100", "48000", "64000", "88200", "96000", "None", "128000", "176400", "192000"};
175955e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
17601da177e4SLinus Torvalds 
17611da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
17621da177e4SLinus Torvalds 	uinfo->count = 1;
17631da177e4SLinus Torvalds 	uinfo->value.enumerated.items = (hdsp->io_type == H9632) ? 10 : 7;
17641da177e4SLinus Torvalds 	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
17651da177e4SLinus Torvalds 		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
17661da177e4SLinus Torvalds 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
17671da177e4SLinus Torvalds 	return 0;
17681da177e4SLinus Torvalds }
17691da177e4SLinus Torvalds 
177055e957d8STakashi Iwai static int snd_hdsp_get_spdif_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
17711da177e4SLinus Torvalds {
177255e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
17731da177e4SLinus Torvalds 
17741da177e4SLinus Torvalds 	switch (hdsp_spdif_sample_rate(hdsp)) {
17751da177e4SLinus Torvalds 	case 32000:
17761da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 0;
17771da177e4SLinus Torvalds 		break;
17781da177e4SLinus Torvalds 	case 44100:
17791da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 1;
17801da177e4SLinus Torvalds 		break;
17811da177e4SLinus Torvalds 	case 48000:
17821da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 2;
17831da177e4SLinus Torvalds 		break;
17841da177e4SLinus Torvalds 	case 64000:
17851da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 3;
17861da177e4SLinus Torvalds 		break;
17871da177e4SLinus Torvalds 	case 88200:
17881da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 4;
17891da177e4SLinus Torvalds 		break;
17901da177e4SLinus Torvalds 	case 96000:
17911da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 5;
17921da177e4SLinus Torvalds 		break;
17931da177e4SLinus Torvalds 	case 128000:
17941da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 7;
17951da177e4SLinus Torvalds 		break;
17961da177e4SLinus Torvalds 	case 176400:
17971da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 8;
17981da177e4SLinus Torvalds 		break;
17991da177e4SLinus Torvalds 	case 192000:
18001da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 9;
18011da177e4SLinus Torvalds 		break;
18021da177e4SLinus Torvalds 	default:
18031da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 6;
18041da177e4SLinus Torvalds 	}
18051da177e4SLinus Torvalds 	return 0;
18061da177e4SLinus Torvalds }
18071da177e4SLinus Torvalds 
18081da177e4SLinus Torvalds #define HDSP_SYSTEM_SAMPLE_RATE(xname, xindex) \
180967ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
18101da177e4SLinus Torvalds   .name = xname, \
18111da177e4SLinus Torvalds   .index = xindex, \
18121da177e4SLinus Torvalds   .access = SNDRV_CTL_ELEM_ACCESS_READ, \
18131da177e4SLinus Torvalds   .info = snd_hdsp_info_system_sample_rate, \
18141da177e4SLinus Torvalds   .get = snd_hdsp_get_system_sample_rate \
18151da177e4SLinus Torvalds }
18161da177e4SLinus Torvalds 
181755e957d8STakashi Iwai static int snd_hdsp_info_system_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
18181da177e4SLinus Torvalds {
18191da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
18201da177e4SLinus Torvalds 	uinfo->count = 1;
18211da177e4SLinus Torvalds 	return 0;
18221da177e4SLinus Torvalds }
18231da177e4SLinus Torvalds 
182455e957d8STakashi Iwai static int snd_hdsp_get_system_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
18251da177e4SLinus Torvalds {
182655e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
18271da177e4SLinus Torvalds 
18281da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp->system_sample_rate;
18291da177e4SLinus Torvalds 	return 0;
18301da177e4SLinus Torvalds }
18311da177e4SLinus Torvalds 
18321da177e4SLinus Torvalds #define HDSP_AUTOSYNC_SAMPLE_RATE(xname, xindex) \
183367ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
18341da177e4SLinus Torvalds   .name = xname, \
18351da177e4SLinus Torvalds   .index = xindex, \
18361da177e4SLinus Torvalds   .access = SNDRV_CTL_ELEM_ACCESS_READ, \
18371da177e4SLinus Torvalds   .info = snd_hdsp_info_autosync_sample_rate, \
18381da177e4SLinus Torvalds   .get = snd_hdsp_get_autosync_sample_rate \
18391da177e4SLinus Torvalds }
18401da177e4SLinus Torvalds 
184155e957d8STakashi Iwai static int snd_hdsp_info_autosync_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
18421da177e4SLinus Torvalds {
184355e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
18441da177e4SLinus Torvalds 	static char *texts[] = {"32000", "44100", "48000", "64000", "88200", "96000", "None", "128000", "176400", "192000"};
18451da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
18461da177e4SLinus Torvalds 	uinfo->count = 1;
18471da177e4SLinus Torvalds 	uinfo->value.enumerated.items = (hdsp->io_type == H9632) ? 10 : 7 ;
18481da177e4SLinus Torvalds 	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
18491da177e4SLinus Torvalds 		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
18501da177e4SLinus Torvalds 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
18511da177e4SLinus Torvalds 	return 0;
18521da177e4SLinus Torvalds }
18531da177e4SLinus Torvalds 
185455e957d8STakashi Iwai static int snd_hdsp_get_autosync_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
18551da177e4SLinus Torvalds {
185655e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
18571da177e4SLinus Torvalds 
18581da177e4SLinus Torvalds 	switch (hdsp_external_sample_rate(hdsp)) {
18591da177e4SLinus Torvalds 	case 32000:
18601da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 0;
18611da177e4SLinus Torvalds 		break;
18621da177e4SLinus Torvalds 	case 44100:
18631da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 1;
18641da177e4SLinus Torvalds 		break;
18651da177e4SLinus Torvalds 	case 48000:
18661da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 2;
18671da177e4SLinus Torvalds 		break;
18681da177e4SLinus Torvalds 	case 64000:
18691da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 3;
18701da177e4SLinus Torvalds 		break;
18711da177e4SLinus Torvalds 	case 88200:
18721da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 4;
18731da177e4SLinus Torvalds 		break;
18741da177e4SLinus Torvalds 	case 96000:
18751da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 5;
18761da177e4SLinus Torvalds 		break;
18771da177e4SLinus Torvalds 	case 128000:
18781da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 7;
18791da177e4SLinus Torvalds 		break;
18801da177e4SLinus Torvalds 	case 176400:
18811da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 8;
18821da177e4SLinus Torvalds 		break;
18831da177e4SLinus Torvalds 	case 192000:
18841da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 9;
18851da177e4SLinus Torvalds 		break;
18861da177e4SLinus Torvalds 	default:
18871da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 6;
18881da177e4SLinus Torvalds 	}
18891da177e4SLinus Torvalds 	return 0;
18901da177e4SLinus Torvalds }
18911da177e4SLinus Torvalds 
18921da177e4SLinus Torvalds #define HDSP_SYSTEM_CLOCK_MODE(xname, xindex) \
189367ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
18941da177e4SLinus Torvalds   .name = xname, \
18951da177e4SLinus Torvalds   .index = xindex, \
18961da177e4SLinus Torvalds   .access = SNDRV_CTL_ELEM_ACCESS_READ, \
18971da177e4SLinus Torvalds   .info = snd_hdsp_info_system_clock_mode, \
18981da177e4SLinus Torvalds   .get = snd_hdsp_get_system_clock_mode \
18991da177e4SLinus Torvalds }
19001da177e4SLinus Torvalds 
190155e957d8STakashi Iwai static int hdsp_system_clock_mode(struct hdsp *hdsp)
19021da177e4SLinus Torvalds {
1903b0b98119STakashi Iwai 	if (hdsp->control_register & HDSP_ClockModeMaster)
19041da177e4SLinus Torvalds 		return 0;
1905b0b98119STakashi Iwai 	else if (hdsp_external_sample_rate(hdsp) != hdsp->system_sample_rate)
19061da177e4SLinus Torvalds 			return 0;
19071da177e4SLinus Torvalds 	return 1;
19081da177e4SLinus Torvalds }
19091da177e4SLinus Torvalds 
191055e957d8STakashi Iwai static int snd_hdsp_info_system_clock_mode(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
19111da177e4SLinus Torvalds {
19121da177e4SLinus Torvalds 	static char *texts[] = {"Master", "Slave" };
19131da177e4SLinus Torvalds 
19141da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
19151da177e4SLinus Torvalds 	uinfo->count = 1;
19161da177e4SLinus Torvalds 	uinfo->value.enumerated.items = 2;
19171da177e4SLinus Torvalds 	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
19181da177e4SLinus Torvalds 		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
19191da177e4SLinus Torvalds 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
19201da177e4SLinus Torvalds 	return 0;
19211da177e4SLinus Torvalds }
19221da177e4SLinus Torvalds 
192355e957d8STakashi Iwai static int snd_hdsp_get_system_clock_mode(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
19241da177e4SLinus Torvalds {
192555e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
19261da177e4SLinus Torvalds 
19271da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp_system_clock_mode(hdsp);
19281da177e4SLinus Torvalds 	return 0;
19291da177e4SLinus Torvalds }
19301da177e4SLinus Torvalds 
19311da177e4SLinus Torvalds #define HDSP_CLOCK_SOURCE(xname, xindex) \
193267ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
19331da177e4SLinus Torvalds   .name = xname, \
19341da177e4SLinus Torvalds   .index = xindex, \
19351da177e4SLinus Torvalds   .info = snd_hdsp_info_clock_source, \
19361da177e4SLinus Torvalds   .get = snd_hdsp_get_clock_source, \
19371da177e4SLinus Torvalds   .put = snd_hdsp_put_clock_source \
19381da177e4SLinus Torvalds }
19391da177e4SLinus Torvalds 
194055e957d8STakashi Iwai static int hdsp_clock_source(struct hdsp *hdsp)
19411da177e4SLinus Torvalds {
19421da177e4SLinus Torvalds 	if (hdsp->control_register & HDSP_ClockModeMaster) {
19431da177e4SLinus Torvalds 		switch (hdsp->system_sample_rate) {
19441da177e4SLinus Torvalds 		case 32000:
19451da177e4SLinus Torvalds 			return 1;
19461da177e4SLinus Torvalds 		case 44100:
19471da177e4SLinus Torvalds 			return 2;
19481da177e4SLinus Torvalds 		case 48000:
19491da177e4SLinus Torvalds 			return 3;
19501da177e4SLinus Torvalds 		case 64000:
19511da177e4SLinus Torvalds 			return 4;
19521da177e4SLinus Torvalds 		case 88200:
19531da177e4SLinus Torvalds 			return 5;
19541da177e4SLinus Torvalds 		case 96000:
19551da177e4SLinus Torvalds 			return 6;
19561da177e4SLinus Torvalds 		case 128000:
19571da177e4SLinus Torvalds 			return 7;
19581da177e4SLinus Torvalds 		case 176400:
19591da177e4SLinus Torvalds 			return 8;
19601da177e4SLinus Torvalds 		case 192000:
19611da177e4SLinus Torvalds 			return 9;
19621da177e4SLinus Torvalds 		default:
19631da177e4SLinus Torvalds 			return 3;
19641da177e4SLinus Torvalds 		}
19651da177e4SLinus Torvalds 	} else {
19661da177e4SLinus Torvalds 		return 0;
19671da177e4SLinus Torvalds 	}
19681da177e4SLinus Torvalds }
19691da177e4SLinus Torvalds 
197055e957d8STakashi Iwai static int hdsp_set_clock_source(struct hdsp *hdsp, int mode)
19711da177e4SLinus Torvalds {
19721da177e4SLinus Torvalds 	int rate;
19731da177e4SLinus Torvalds 	switch (mode) {
19741da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_AUTOSYNC:
19751da177e4SLinus Torvalds 		if (hdsp_external_sample_rate(hdsp) != 0) {
19761da177e4SLinus Torvalds 		    if (!hdsp_set_rate(hdsp, hdsp_external_sample_rate(hdsp), 1)) {
19771da177e4SLinus Torvalds 			hdsp->control_register &= ~HDSP_ClockModeMaster;
19781da177e4SLinus Torvalds 			hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
19791da177e4SLinus Torvalds 			return 0;
19801da177e4SLinus Torvalds 		    }
19811da177e4SLinus Torvalds 		}
19821da177e4SLinus Torvalds 		return -1;
19831da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_32KHZ:
19841da177e4SLinus Torvalds 		rate = 32000;
19851da177e4SLinus Torvalds 		break;
19861da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_44_1KHZ:
19871da177e4SLinus Torvalds 		rate = 44100;
19881da177e4SLinus Torvalds 		break;
19891da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_48KHZ:
19901da177e4SLinus Torvalds 		rate = 48000;
19911da177e4SLinus Torvalds 		break;
19921da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_64KHZ:
19931da177e4SLinus Torvalds 		rate = 64000;
19941da177e4SLinus Torvalds 		break;
19951da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_88_2KHZ:
19961da177e4SLinus Torvalds 		rate = 88200;
19971da177e4SLinus Torvalds 		break;
19981da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_96KHZ:
19991da177e4SLinus Torvalds 		rate = 96000;
20001da177e4SLinus Torvalds 		break;
20011da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_128KHZ:
20021da177e4SLinus Torvalds 		rate = 128000;
20031da177e4SLinus Torvalds 		break;
20041da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_176_4KHZ:
20051da177e4SLinus Torvalds 		rate = 176400;
20061da177e4SLinus Torvalds 		break;
20071da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_192KHZ:
20081da177e4SLinus Torvalds 		rate = 192000;
20091da177e4SLinus Torvalds 		break;
20101da177e4SLinus Torvalds 	default:
20111da177e4SLinus Torvalds 		rate = 48000;
20121da177e4SLinus Torvalds 	}
20131da177e4SLinus Torvalds 	hdsp->control_register |= HDSP_ClockModeMaster;
20141da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
20151da177e4SLinus Torvalds 	hdsp_set_rate(hdsp, rate, 1);
20161da177e4SLinus Torvalds 	return 0;
20171da177e4SLinus Torvalds }
20181da177e4SLinus Torvalds 
201955e957d8STakashi Iwai static int snd_hdsp_info_clock_source(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
20201da177e4SLinus Torvalds {
20211da177e4SLinus 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" };
202255e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
20231da177e4SLinus Torvalds 
20241da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
20251da177e4SLinus Torvalds 	uinfo->count = 1;
20261da177e4SLinus Torvalds 	if (hdsp->io_type == H9632)
20271da177e4SLinus Torvalds 	    uinfo->value.enumerated.items = 10;
20281da177e4SLinus Torvalds 	else
20291da177e4SLinus Torvalds 	    uinfo->value.enumerated.items = 7;
20301da177e4SLinus Torvalds 	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
20311da177e4SLinus Torvalds 		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
20321da177e4SLinus Torvalds 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
20331da177e4SLinus Torvalds 	return 0;
20341da177e4SLinus Torvalds }
20351da177e4SLinus Torvalds 
203655e957d8STakashi Iwai static int snd_hdsp_get_clock_source(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
20371da177e4SLinus Torvalds {
203855e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
20391da177e4SLinus Torvalds 
20401da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp_clock_source(hdsp);
20411da177e4SLinus Torvalds 	return 0;
20421da177e4SLinus Torvalds }
20431da177e4SLinus Torvalds 
204455e957d8STakashi Iwai static int snd_hdsp_put_clock_source(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
20451da177e4SLinus Torvalds {
204655e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
20471da177e4SLinus Torvalds 	int change;
20481da177e4SLinus Torvalds 	int val;
20491da177e4SLinus Torvalds 
20501da177e4SLinus Torvalds 	if (!snd_hdsp_use_is_exclusive(hdsp))
20511da177e4SLinus Torvalds 		return -EBUSY;
20521da177e4SLinus Torvalds 	val = ucontrol->value.enumerated.item[0];
20531da177e4SLinus Torvalds 	if (val < 0) val = 0;
20541da177e4SLinus Torvalds 	if (hdsp->io_type == H9632) {
2055b0b98119STakashi Iwai 		if (val > 9)
2056b0b98119STakashi Iwai 			val = 9;
20571da177e4SLinus Torvalds 	} else {
2058b0b98119STakashi Iwai 		if (val > 6)
2059b0b98119STakashi Iwai 			val = 6;
20601da177e4SLinus Torvalds 	}
20611da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
2062b0b98119STakashi Iwai 	if (val != hdsp_clock_source(hdsp))
20631da177e4SLinus Torvalds 		change = (hdsp_set_clock_source(hdsp, val) == 0) ? 1 : 0;
2064b0b98119STakashi Iwai 	else
20651da177e4SLinus Torvalds 		change = 0;
20661da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
20671da177e4SLinus Torvalds 	return change;
20681da177e4SLinus Torvalds }
20691da177e4SLinus Torvalds 
207055e957d8STakashi Iwai static int snd_hdsp_info_clock_source_lock(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
2071e3ea4d89STakashi Iwai {
2072e3ea4d89STakashi Iwai 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2073e3ea4d89STakashi Iwai 	uinfo->count = 1;
2074e3ea4d89STakashi Iwai 	uinfo->value.integer.min = 0;
2075e3ea4d89STakashi Iwai 	uinfo->value.integer.max = 1;
2076e3ea4d89STakashi Iwai 	return 0;
2077e3ea4d89STakashi Iwai }
2078e3ea4d89STakashi Iwai 
207955e957d8STakashi Iwai static int snd_hdsp_get_clock_source_lock(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2080e3ea4d89STakashi Iwai {
208155e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2082e3ea4d89STakashi Iwai 
2083e3ea4d89STakashi Iwai 	ucontrol->value.integer.value[0] = hdsp->clock_source_locked;
2084e3ea4d89STakashi Iwai 	return 0;
2085e3ea4d89STakashi Iwai }
2086e3ea4d89STakashi Iwai 
208755e957d8STakashi Iwai static int snd_hdsp_put_clock_source_lock(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2088e3ea4d89STakashi Iwai {
208955e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2090e3ea4d89STakashi Iwai 	int change;
2091e3ea4d89STakashi Iwai 
2092e3ea4d89STakashi Iwai 	change = (int)ucontrol->value.integer.value[0] != hdsp->clock_source_locked;
2093e3ea4d89STakashi Iwai 	if (change)
2094e3ea4d89STakashi Iwai 		hdsp->clock_source_locked = ucontrol->value.integer.value[0];
2095e3ea4d89STakashi Iwai 	return change;
2096e3ea4d89STakashi Iwai }
2097e3ea4d89STakashi Iwai 
20981da177e4SLinus Torvalds #define HDSP_DA_GAIN(xname, xindex) \
209967ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
21001da177e4SLinus Torvalds   .name = xname, \
21011da177e4SLinus Torvalds   .index = xindex, \
21021da177e4SLinus Torvalds   .info = snd_hdsp_info_da_gain, \
21031da177e4SLinus Torvalds   .get = snd_hdsp_get_da_gain, \
21041da177e4SLinus Torvalds   .put = snd_hdsp_put_da_gain \
21051da177e4SLinus Torvalds }
21061da177e4SLinus Torvalds 
210755e957d8STakashi Iwai static int hdsp_da_gain(struct hdsp *hdsp)
21081da177e4SLinus Torvalds {
21091da177e4SLinus Torvalds 	switch (hdsp->control_register & HDSP_DAGainMask) {
21101da177e4SLinus Torvalds 	case HDSP_DAGainHighGain:
21111da177e4SLinus Torvalds 		return 0;
21121da177e4SLinus Torvalds 	case HDSP_DAGainPlus4dBu:
21131da177e4SLinus Torvalds 		return 1;
21141da177e4SLinus Torvalds 	case HDSP_DAGainMinus10dBV:
21151da177e4SLinus Torvalds 		return 2;
21161da177e4SLinus Torvalds 	default:
21171da177e4SLinus Torvalds 		return 1;
21181da177e4SLinus Torvalds 	}
21191da177e4SLinus Torvalds }
21201da177e4SLinus Torvalds 
212155e957d8STakashi Iwai static int hdsp_set_da_gain(struct hdsp *hdsp, int mode)
21221da177e4SLinus Torvalds {
21231da177e4SLinus Torvalds 	hdsp->control_register &= ~HDSP_DAGainMask;
21241da177e4SLinus Torvalds 	switch (mode) {
21251da177e4SLinus Torvalds 	case 0:
21261da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_DAGainHighGain;
21271da177e4SLinus Torvalds 		break;
21281da177e4SLinus Torvalds 	case 1:
21291da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_DAGainPlus4dBu;
21301da177e4SLinus Torvalds 		break;
21311da177e4SLinus Torvalds 	case 2:
21321da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_DAGainMinus10dBV;
21331da177e4SLinus Torvalds 		break;
21341da177e4SLinus Torvalds 	default:
21351da177e4SLinus Torvalds 		return -1;
21361da177e4SLinus Torvalds 
21371da177e4SLinus Torvalds 	}
21381da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
21391da177e4SLinus Torvalds 	return 0;
21401da177e4SLinus Torvalds }
21411da177e4SLinus Torvalds 
214255e957d8STakashi Iwai static int snd_hdsp_info_da_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
21431da177e4SLinus Torvalds {
21441da177e4SLinus Torvalds 	static char *texts[] = {"Hi Gain", "+4 dBu", "-10 dbV"};
21451da177e4SLinus Torvalds 
21461da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
21471da177e4SLinus Torvalds 	uinfo->count = 1;
21481da177e4SLinus Torvalds 	uinfo->value.enumerated.items = 3;
21491da177e4SLinus Torvalds 	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
21501da177e4SLinus Torvalds 		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
21511da177e4SLinus Torvalds 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
21521da177e4SLinus Torvalds 	return 0;
21531da177e4SLinus Torvalds }
21541da177e4SLinus Torvalds 
215555e957d8STakashi Iwai static int snd_hdsp_get_da_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
21561da177e4SLinus Torvalds {
215755e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
21581da177e4SLinus Torvalds 
21591da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp_da_gain(hdsp);
21601da177e4SLinus Torvalds 	return 0;
21611da177e4SLinus Torvalds }
21621da177e4SLinus Torvalds 
216355e957d8STakashi Iwai static int snd_hdsp_put_da_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
21641da177e4SLinus Torvalds {
216555e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
21661da177e4SLinus Torvalds 	int change;
21671da177e4SLinus Torvalds 	int val;
21681da177e4SLinus Torvalds 
21691da177e4SLinus Torvalds 	if (!snd_hdsp_use_is_exclusive(hdsp))
21701da177e4SLinus Torvalds 		return -EBUSY;
21711da177e4SLinus Torvalds 	val = ucontrol->value.enumerated.item[0];
21721da177e4SLinus Torvalds 	if (val < 0) val = 0;
21731da177e4SLinus Torvalds 	if (val > 2) val = 2;
21741da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
2175b0b98119STakashi Iwai 	if (val != hdsp_da_gain(hdsp))
21761da177e4SLinus Torvalds 		change = (hdsp_set_da_gain(hdsp, val) == 0) ? 1 : 0;
2177b0b98119STakashi Iwai 	else
21781da177e4SLinus Torvalds 		change = 0;
21791da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
21801da177e4SLinus Torvalds 	return change;
21811da177e4SLinus Torvalds }
21821da177e4SLinus Torvalds 
21831da177e4SLinus Torvalds #define HDSP_AD_GAIN(xname, xindex) \
218467ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
21851da177e4SLinus Torvalds   .name = xname, \
21861da177e4SLinus Torvalds   .index = xindex, \
21871da177e4SLinus Torvalds   .info = snd_hdsp_info_ad_gain, \
21881da177e4SLinus Torvalds   .get = snd_hdsp_get_ad_gain, \
21891da177e4SLinus Torvalds   .put = snd_hdsp_put_ad_gain \
21901da177e4SLinus Torvalds }
21911da177e4SLinus Torvalds 
219255e957d8STakashi Iwai static int hdsp_ad_gain(struct hdsp *hdsp)
21931da177e4SLinus Torvalds {
21941da177e4SLinus Torvalds 	switch (hdsp->control_register & HDSP_ADGainMask) {
21951da177e4SLinus Torvalds 	case HDSP_ADGainMinus10dBV:
21961da177e4SLinus Torvalds 		return 0;
21971da177e4SLinus Torvalds 	case HDSP_ADGainPlus4dBu:
21981da177e4SLinus Torvalds 		return 1;
21991da177e4SLinus Torvalds 	case HDSP_ADGainLowGain:
22001da177e4SLinus Torvalds 		return 2;
22011da177e4SLinus Torvalds 	default:
22021da177e4SLinus Torvalds 		return 1;
22031da177e4SLinus Torvalds 	}
22041da177e4SLinus Torvalds }
22051da177e4SLinus Torvalds 
220655e957d8STakashi Iwai static int hdsp_set_ad_gain(struct hdsp *hdsp, int mode)
22071da177e4SLinus Torvalds {
22081da177e4SLinus Torvalds 	hdsp->control_register &= ~HDSP_ADGainMask;
22091da177e4SLinus Torvalds 	switch (mode) {
22101da177e4SLinus Torvalds 	case 0:
22111da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_ADGainMinus10dBV;
22121da177e4SLinus Torvalds 		break;
22131da177e4SLinus Torvalds 	case 1:
22141da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_ADGainPlus4dBu;
22151da177e4SLinus Torvalds 		break;
22161da177e4SLinus Torvalds 	case 2:
22171da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_ADGainLowGain;
22181da177e4SLinus Torvalds 		break;
22191da177e4SLinus Torvalds 	default:
22201da177e4SLinus Torvalds 		return -1;
22211da177e4SLinus Torvalds 
22221da177e4SLinus Torvalds 	}
22231da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
22241da177e4SLinus Torvalds 	return 0;
22251da177e4SLinus Torvalds }
22261da177e4SLinus Torvalds 
222755e957d8STakashi Iwai static int snd_hdsp_info_ad_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
22281da177e4SLinus Torvalds {
22291da177e4SLinus Torvalds 	static char *texts[] = {"-10 dBV", "+4 dBu", "Lo Gain"};
22301da177e4SLinus Torvalds 
22311da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
22321da177e4SLinus Torvalds 	uinfo->count = 1;
22331da177e4SLinus Torvalds 	uinfo->value.enumerated.items = 3;
22341da177e4SLinus Torvalds 	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
22351da177e4SLinus Torvalds 		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
22361da177e4SLinus Torvalds 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
22371da177e4SLinus Torvalds 	return 0;
22381da177e4SLinus Torvalds }
22391da177e4SLinus Torvalds 
224055e957d8STakashi Iwai static int snd_hdsp_get_ad_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
22411da177e4SLinus Torvalds {
224255e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
22431da177e4SLinus Torvalds 
22441da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp_ad_gain(hdsp);
22451da177e4SLinus Torvalds 	return 0;
22461da177e4SLinus Torvalds }
22471da177e4SLinus Torvalds 
224855e957d8STakashi Iwai static int snd_hdsp_put_ad_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
22491da177e4SLinus Torvalds {
225055e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
22511da177e4SLinus Torvalds 	int change;
22521da177e4SLinus Torvalds 	int val;
22531da177e4SLinus Torvalds 
22541da177e4SLinus Torvalds 	if (!snd_hdsp_use_is_exclusive(hdsp))
22551da177e4SLinus Torvalds 		return -EBUSY;
22561da177e4SLinus Torvalds 	val = ucontrol->value.enumerated.item[0];
22571da177e4SLinus Torvalds 	if (val < 0) val = 0;
22581da177e4SLinus Torvalds 	if (val > 2) val = 2;
22591da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
2260b0b98119STakashi Iwai 	if (val != hdsp_ad_gain(hdsp))
22611da177e4SLinus Torvalds 		change = (hdsp_set_ad_gain(hdsp, val) == 0) ? 1 : 0;
2262b0b98119STakashi Iwai 	else
22631da177e4SLinus Torvalds 		change = 0;
22641da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
22651da177e4SLinus Torvalds 	return change;
22661da177e4SLinus Torvalds }
22671da177e4SLinus Torvalds 
22681da177e4SLinus Torvalds #define HDSP_PHONE_GAIN(xname, xindex) \
226967ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
22701da177e4SLinus Torvalds   .name = xname, \
22711da177e4SLinus Torvalds   .index = xindex, \
22721da177e4SLinus Torvalds   .info = snd_hdsp_info_phone_gain, \
22731da177e4SLinus Torvalds   .get = snd_hdsp_get_phone_gain, \
22741da177e4SLinus Torvalds   .put = snd_hdsp_put_phone_gain \
22751da177e4SLinus Torvalds }
22761da177e4SLinus Torvalds 
227755e957d8STakashi Iwai static int hdsp_phone_gain(struct hdsp *hdsp)
22781da177e4SLinus Torvalds {
22791da177e4SLinus Torvalds 	switch (hdsp->control_register & HDSP_PhoneGainMask) {
22801da177e4SLinus Torvalds 	case HDSP_PhoneGain0dB:
22811da177e4SLinus Torvalds 		return 0;
22821da177e4SLinus Torvalds 	case HDSP_PhoneGainMinus6dB:
22831da177e4SLinus Torvalds 		return 1;
22841da177e4SLinus Torvalds 	case HDSP_PhoneGainMinus12dB:
22851da177e4SLinus Torvalds 		return 2;
22861da177e4SLinus Torvalds 	default:
22871da177e4SLinus Torvalds 		return 0;
22881da177e4SLinus Torvalds 	}
22891da177e4SLinus Torvalds }
22901da177e4SLinus Torvalds 
229155e957d8STakashi Iwai static int hdsp_set_phone_gain(struct hdsp *hdsp, int mode)
22921da177e4SLinus Torvalds {
22931da177e4SLinus Torvalds 	hdsp->control_register &= ~HDSP_PhoneGainMask;
22941da177e4SLinus Torvalds 	switch (mode) {
22951da177e4SLinus Torvalds 	case 0:
22961da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_PhoneGain0dB;
22971da177e4SLinus Torvalds 		break;
22981da177e4SLinus Torvalds 	case 1:
22991da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_PhoneGainMinus6dB;
23001da177e4SLinus Torvalds 		break;
23011da177e4SLinus Torvalds 	case 2:
23021da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_PhoneGainMinus12dB;
23031da177e4SLinus Torvalds 		break;
23041da177e4SLinus Torvalds 	default:
23051da177e4SLinus Torvalds 		return -1;
23061da177e4SLinus Torvalds 
23071da177e4SLinus Torvalds 	}
23081da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
23091da177e4SLinus Torvalds 	return 0;
23101da177e4SLinus Torvalds }
23111da177e4SLinus Torvalds 
231255e957d8STakashi Iwai static int snd_hdsp_info_phone_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
23131da177e4SLinus Torvalds {
23141da177e4SLinus Torvalds 	static char *texts[] = {"0 dB", "-6 dB", "-12 dB"};
23151da177e4SLinus Torvalds 
23161da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
23171da177e4SLinus Torvalds 	uinfo->count = 1;
23181da177e4SLinus Torvalds 	uinfo->value.enumerated.items = 3;
23191da177e4SLinus Torvalds 	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
23201da177e4SLinus Torvalds 		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
23211da177e4SLinus Torvalds 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
23221da177e4SLinus Torvalds 	return 0;
23231da177e4SLinus Torvalds }
23241da177e4SLinus Torvalds 
232555e957d8STakashi Iwai static int snd_hdsp_get_phone_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
23261da177e4SLinus Torvalds {
232755e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
23281da177e4SLinus Torvalds 
23291da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp_phone_gain(hdsp);
23301da177e4SLinus Torvalds 	return 0;
23311da177e4SLinus Torvalds }
23321da177e4SLinus Torvalds 
233355e957d8STakashi Iwai static int snd_hdsp_put_phone_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
23341da177e4SLinus Torvalds {
233555e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
23361da177e4SLinus Torvalds 	int change;
23371da177e4SLinus Torvalds 	int val;
23381da177e4SLinus Torvalds 
23391da177e4SLinus Torvalds 	if (!snd_hdsp_use_is_exclusive(hdsp))
23401da177e4SLinus Torvalds 		return -EBUSY;
23411da177e4SLinus Torvalds 	val = ucontrol->value.enumerated.item[0];
23421da177e4SLinus Torvalds 	if (val < 0) val = 0;
23431da177e4SLinus Torvalds 	if (val > 2) val = 2;
23441da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
2345b0b98119STakashi Iwai 	if (val != hdsp_phone_gain(hdsp))
23461da177e4SLinus Torvalds 		change = (hdsp_set_phone_gain(hdsp, val) == 0) ? 1 : 0;
2347b0b98119STakashi Iwai 	else
23481da177e4SLinus Torvalds 		change = 0;
23491da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
23501da177e4SLinus Torvalds 	return change;
23511da177e4SLinus Torvalds }
23521da177e4SLinus Torvalds 
23531da177e4SLinus Torvalds #define HDSP_XLR_BREAKOUT_CABLE(xname, xindex) \
235467ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
23551da177e4SLinus Torvalds   .name = xname, \
23561da177e4SLinus Torvalds   .index = xindex, \
23571da177e4SLinus Torvalds   .info = snd_hdsp_info_xlr_breakout_cable, \
23581da177e4SLinus Torvalds   .get = snd_hdsp_get_xlr_breakout_cable, \
23591da177e4SLinus Torvalds   .put = snd_hdsp_put_xlr_breakout_cable \
23601da177e4SLinus Torvalds }
23611da177e4SLinus Torvalds 
236255e957d8STakashi Iwai static int hdsp_xlr_breakout_cable(struct hdsp *hdsp)
23631da177e4SLinus Torvalds {
2364b0b98119STakashi Iwai 	if (hdsp->control_register & HDSP_XLRBreakoutCable)
23651da177e4SLinus Torvalds 		return 1;
23661da177e4SLinus Torvalds 	return 0;
23671da177e4SLinus Torvalds }
23681da177e4SLinus Torvalds 
236955e957d8STakashi Iwai static int hdsp_set_xlr_breakout_cable(struct hdsp *hdsp, int mode)
23701da177e4SLinus Torvalds {
2371b0b98119STakashi Iwai 	if (mode)
23721da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_XLRBreakoutCable;
2373b0b98119STakashi Iwai 	else
23741da177e4SLinus Torvalds 		hdsp->control_register &= ~HDSP_XLRBreakoutCable;
23751da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
23761da177e4SLinus Torvalds 	return 0;
23771da177e4SLinus Torvalds }
23781da177e4SLinus Torvalds 
237955e957d8STakashi Iwai static int snd_hdsp_info_xlr_breakout_cable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
23801da177e4SLinus Torvalds {
23811da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
23821da177e4SLinus Torvalds 	uinfo->count = 1;
23831da177e4SLinus Torvalds 	uinfo->value.integer.min = 0;
23841da177e4SLinus Torvalds 	uinfo->value.integer.max = 1;
23851da177e4SLinus Torvalds 	return 0;
23861da177e4SLinus Torvalds }
23871da177e4SLinus Torvalds 
238855e957d8STakashi Iwai static int snd_hdsp_get_xlr_breakout_cable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
23891da177e4SLinus Torvalds {
239055e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
23911da177e4SLinus Torvalds 
23921da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp_xlr_breakout_cable(hdsp);
23931da177e4SLinus Torvalds 	return 0;
23941da177e4SLinus Torvalds }
23951da177e4SLinus Torvalds 
239655e957d8STakashi Iwai static int snd_hdsp_put_xlr_breakout_cable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
23971da177e4SLinus Torvalds {
239855e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
23991da177e4SLinus Torvalds 	int change;
24001da177e4SLinus Torvalds 	int val;
24011da177e4SLinus Torvalds 
24021da177e4SLinus Torvalds 	if (!snd_hdsp_use_is_exclusive(hdsp))
24031da177e4SLinus Torvalds 		return -EBUSY;
24041da177e4SLinus Torvalds 	val = ucontrol->value.integer.value[0] & 1;
24051da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
24061da177e4SLinus Torvalds 	change = (int)val != hdsp_xlr_breakout_cable(hdsp);
24071da177e4SLinus Torvalds 	hdsp_set_xlr_breakout_cable(hdsp, val);
24081da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
24091da177e4SLinus Torvalds 	return change;
24101da177e4SLinus Torvalds }
24111da177e4SLinus Torvalds 
24121da177e4SLinus Torvalds /* (De)activates old RME Analog Extension Board
24131da177e4SLinus Torvalds    These are connected to the internal ADAT connector
24141da177e4SLinus Torvalds    Switching this on desactivates external ADAT
24151da177e4SLinus Torvalds */
24161da177e4SLinus Torvalds #define HDSP_AEB(xname, xindex) \
241767ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
24181da177e4SLinus Torvalds   .name = xname, \
24191da177e4SLinus Torvalds   .index = xindex, \
24201da177e4SLinus Torvalds   .info = snd_hdsp_info_aeb, \
24211da177e4SLinus Torvalds   .get = snd_hdsp_get_aeb, \
24221da177e4SLinus Torvalds   .put = snd_hdsp_put_aeb \
24231da177e4SLinus Torvalds }
24241da177e4SLinus Torvalds 
242555e957d8STakashi Iwai static int hdsp_aeb(struct hdsp *hdsp)
24261da177e4SLinus Torvalds {
2427b0b98119STakashi Iwai 	if (hdsp->control_register & HDSP_AnalogExtensionBoard)
24281da177e4SLinus Torvalds 		return 1;
24291da177e4SLinus Torvalds 	return 0;
24301da177e4SLinus Torvalds }
24311da177e4SLinus Torvalds 
243255e957d8STakashi Iwai static int hdsp_set_aeb(struct hdsp *hdsp, int mode)
24331da177e4SLinus Torvalds {
2434b0b98119STakashi Iwai 	if (mode)
24351da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_AnalogExtensionBoard;
2436b0b98119STakashi Iwai 	else
24371da177e4SLinus Torvalds 		hdsp->control_register &= ~HDSP_AnalogExtensionBoard;
24381da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
24391da177e4SLinus Torvalds 	return 0;
24401da177e4SLinus Torvalds }
24411da177e4SLinus Torvalds 
244255e957d8STakashi Iwai static int snd_hdsp_info_aeb(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
24431da177e4SLinus Torvalds {
24441da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
24451da177e4SLinus Torvalds 	uinfo->count = 1;
24461da177e4SLinus Torvalds 	uinfo->value.integer.min = 0;
24471da177e4SLinus Torvalds 	uinfo->value.integer.max = 1;
24481da177e4SLinus Torvalds 	return 0;
24491da177e4SLinus Torvalds }
24501da177e4SLinus Torvalds 
245155e957d8STakashi Iwai static int snd_hdsp_get_aeb(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
24521da177e4SLinus Torvalds {
245355e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
24541da177e4SLinus Torvalds 
24551da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp_aeb(hdsp);
24561da177e4SLinus Torvalds 	return 0;
24571da177e4SLinus Torvalds }
24581da177e4SLinus Torvalds 
245955e957d8STakashi Iwai static int snd_hdsp_put_aeb(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
24601da177e4SLinus Torvalds {
246155e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
24621da177e4SLinus Torvalds 	int change;
24631da177e4SLinus Torvalds 	int val;
24641da177e4SLinus Torvalds 
24651da177e4SLinus Torvalds 	if (!snd_hdsp_use_is_exclusive(hdsp))
24661da177e4SLinus Torvalds 		return -EBUSY;
24671da177e4SLinus Torvalds 	val = ucontrol->value.integer.value[0] & 1;
24681da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
24691da177e4SLinus Torvalds 	change = (int)val != hdsp_aeb(hdsp);
24701da177e4SLinus Torvalds 	hdsp_set_aeb(hdsp, val);
24711da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
24721da177e4SLinus Torvalds 	return change;
24731da177e4SLinus Torvalds }
24741da177e4SLinus Torvalds 
24751da177e4SLinus Torvalds #define HDSP_PREF_SYNC_REF(xname, xindex) \
247667ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
24771da177e4SLinus Torvalds   .name = xname, \
24781da177e4SLinus Torvalds   .index = xindex, \
24791da177e4SLinus Torvalds   .info = snd_hdsp_info_pref_sync_ref, \
24801da177e4SLinus Torvalds   .get = snd_hdsp_get_pref_sync_ref, \
24811da177e4SLinus Torvalds   .put = snd_hdsp_put_pref_sync_ref \
24821da177e4SLinus Torvalds }
24831da177e4SLinus Torvalds 
248455e957d8STakashi Iwai static int hdsp_pref_sync_ref(struct hdsp *hdsp)
24851da177e4SLinus Torvalds {
24861da177e4SLinus Torvalds 	/* Notice that this looks at the requested sync source,
24871da177e4SLinus Torvalds 	   not the one actually in use.
24881da177e4SLinus Torvalds 	*/
24891da177e4SLinus Torvalds 
24901da177e4SLinus Torvalds 	switch (hdsp->control_register & HDSP_SyncRefMask) {
24911da177e4SLinus Torvalds 	case HDSP_SyncRef_ADAT1:
24921da177e4SLinus Torvalds 		return HDSP_SYNC_FROM_ADAT1;
24931da177e4SLinus Torvalds 	case HDSP_SyncRef_ADAT2:
24941da177e4SLinus Torvalds 		return HDSP_SYNC_FROM_ADAT2;
24951da177e4SLinus Torvalds 	case HDSP_SyncRef_ADAT3:
24961da177e4SLinus Torvalds 		return HDSP_SYNC_FROM_ADAT3;
24971da177e4SLinus Torvalds 	case HDSP_SyncRef_SPDIF:
24981da177e4SLinus Torvalds 		return HDSP_SYNC_FROM_SPDIF;
24991da177e4SLinus Torvalds 	case HDSP_SyncRef_WORD:
25001da177e4SLinus Torvalds 		return HDSP_SYNC_FROM_WORD;
25011da177e4SLinus Torvalds 	case HDSP_SyncRef_ADAT_SYNC:
25021da177e4SLinus Torvalds 		return HDSP_SYNC_FROM_ADAT_SYNC;
25031da177e4SLinus Torvalds 	default:
25041da177e4SLinus Torvalds 		return HDSP_SYNC_FROM_WORD;
25051da177e4SLinus Torvalds 	}
25061da177e4SLinus Torvalds 	return 0;
25071da177e4SLinus Torvalds }
25081da177e4SLinus Torvalds 
250955e957d8STakashi Iwai static int hdsp_set_pref_sync_ref(struct hdsp *hdsp, int pref)
25101da177e4SLinus Torvalds {
25111da177e4SLinus Torvalds 	hdsp->control_register &= ~HDSP_SyncRefMask;
25121da177e4SLinus Torvalds 	switch (pref) {
25131da177e4SLinus Torvalds 	case HDSP_SYNC_FROM_ADAT1:
25141da177e4SLinus Torvalds 		hdsp->control_register &= ~HDSP_SyncRefMask; /* clear SyncRef bits */
25151da177e4SLinus Torvalds 		break;
25161da177e4SLinus Torvalds 	case HDSP_SYNC_FROM_ADAT2:
25171da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_SyncRef_ADAT2;
25181da177e4SLinus Torvalds 		break;
25191da177e4SLinus Torvalds 	case HDSP_SYNC_FROM_ADAT3:
25201da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_SyncRef_ADAT3;
25211da177e4SLinus Torvalds 		break;
25221da177e4SLinus Torvalds 	case HDSP_SYNC_FROM_SPDIF:
25231da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_SyncRef_SPDIF;
25241da177e4SLinus Torvalds 		break;
25251da177e4SLinus Torvalds 	case HDSP_SYNC_FROM_WORD:
25261da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_SyncRef_WORD;
25271da177e4SLinus Torvalds 		break;
25281da177e4SLinus Torvalds 	case HDSP_SYNC_FROM_ADAT_SYNC:
25291da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_SyncRef_ADAT_SYNC;
25301da177e4SLinus Torvalds 		break;
25311da177e4SLinus Torvalds 	default:
25321da177e4SLinus Torvalds 		return -1;
25331da177e4SLinus Torvalds 	}
25341da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
25351da177e4SLinus Torvalds 	return 0;
25361da177e4SLinus Torvalds }
25371da177e4SLinus Torvalds 
253855e957d8STakashi Iwai static int snd_hdsp_info_pref_sync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
25391da177e4SLinus Torvalds {
25401da177e4SLinus Torvalds 	static char *texts[] = {"Word", "IEC958", "ADAT1", "ADAT Sync", "ADAT2", "ADAT3" };
254155e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
25421da177e4SLinus Torvalds 
25431da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
25441da177e4SLinus Torvalds 	uinfo->count = 1;
25451da177e4SLinus Torvalds 
25461da177e4SLinus Torvalds 	switch (hdsp->io_type) {
25471da177e4SLinus Torvalds 	case Digiface:
25481da177e4SLinus Torvalds 	case H9652:
25491da177e4SLinus Torvalds 		uinfo->value.enumerated.items = 6;
25501da177e4SLinus Torvalds 		break;
25511da177e4SLinus Torvalds 	case Multiface:
25521da177e4SLinus Torvalds 		uinfo->value.enumerated.items = 4;
25531da177e4SLinus Torvalds 		break;
25541da177e4SLinus Torvalds 	case H9632:
25551da177e4SLinus Torvalds 		uinfo->value.enumerated.items = 3;
25561da177e4SLinus Torvalds 		break;
25571da177e4SLinus Torvalds 	default:
25581da177e4SLinus Torvalds 		uinfo->value.enumerated.items = 0;
25591da177e4SLinus Torvalds 		break;
25601da177e4SLinus Torvalds 	}
25611da177e4SLinus Torvalds 
25621da177e4SLinus Torvalds 	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
25631da177e4SLinus Torvalds 		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
25641da177e4SLinus Torvalds 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
25651da177e4SLinus Torvalds 	return 0;
25661da177e4SLinus Torvalds }
25671da177e4SLinus Torvalds 
256855e957d8STakashi Iwai static int snd_hdsp_get_pref_sync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
25691da177e4SLinus Torvalds {
257055e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
25711da177e4SLinus Torvalds 
25721da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp_pref_sync_ref(hdsp);
25731da177e4SLinus Torvalds 	return 0;
25741da177e4SLinus Torvalds }
25751da177e4SLinus Torvalds 
257655e957d8STakashi Iwai static int snd_hdsp_put_pref_sync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
25771da177e4SLinus Torvalds {
257855e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
25791da177e4SLinus Torvalds 	int change, max;
25801da177e4SLinus Torvalds 	unsigned int val;
25811da177e4SLinus Torvalds 
25821da177e4SLinus Torvalds 	if (!snd_hdsp_use_is_exclusive(hdsp))
25831da177e4SLinus Torvalds 		return -EBUSY;
25841da177e4SLinus Torvalds 
25851da177e4SLinus Torvalds 	switch (hdsp->io_type) {
25861da177e4SLinus Torvalds 	case Digiface:
25871da177e4SLinus Torvalds 	case H9652:
25881da177e4SLinus Torvalds 		max = 6;
25891da177e4SLinus Torvalds 		break;
25901da177e4SLinus Torvalds 	case Multiface:
25911da177e4SLinus Torvalds 		max = 4;
25921da177e4SLinus Torvalds 		break;
25931da177e4SLinus Torvalds 	case H9632:
25941da177e4SLinus Torvalds 		max = 3;
25951da177e4SLinus Torvalds 		break;
25961da177e4SLinus Torvalds 	default:
25971da177e4SLinus Torvalds 		return -EIO;
25981da177e4SLinus Torvalds 	}
25991da177e4SLinus Torvalds 
26001da177e4SLinus Torvalds 	val = ucontrol->value.enumerated.item[0] % max;
26011da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
26021da177e4SLinus Torvalds 	change = (int)val != hdsp_pref_sync_ref(hdsp);
26031da177e4SLinus Torvalds 	hdsp_set_pref_sync_ref(hdsp, val);
26041da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
26051da177e4SLinus Torvalds 	return change;
26061da177e4SLinus Torvalds }
26071da177e4SLinus Torvalds 
26081da177e4SLinus Torvalds #define HDSP_AUTOSYNC_REF(xname, xindex) \
260967ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
26101da177e4SLinus Torvalds   .name = xname, \
26111da177e4SLinus Torvalds   .index = xindex, \
26121da177e4SLinus Torvalds   .access = SNDRV_CTL_ELEM_ACCESS_READ, \
26131da177e4SLinus Torvalds   .info = snd_hdsp_info_autosync_ref, \
26141da177e4SLinus Torvalds   .get = snd_hdsp_get_autosync_ref, \
26151da177e4SLinus Torvalds }
26161da177e4SLinus Torvalds 
261755e957d8STakashi Iwai static int hdsp_autosync_ref(struct hdsp *hdsp)
26181da177e4SLinus Torvalds {
26191da177e4SLinus Torvalds 	/* This looks at the autosync selected sync reference */
26201da177e4SLinus Torvalds 	unsigned int status2 = hdsp_read(hdsp, HDSP_status2Register);
26211da177e4SLinus Torvalds 
26221da177e4SLinus Torvalds 	switch (status2 & HDSP_SelSyncRefMask) {
26231da177e4SLinus Torvalds 	case HDSP_SelSyncRef_WORD:
26241da177e4SLinus Torvalds 		return HDSP_AUTOSYNC_FROM_WORD;
26251da177e4SLinus Torvalds 	case HDSP_SelSyncRef_ADAT_SYNC:
26261da177e4SLinus Torvalds 		return HDSP_AUTOSYNC_FROM_ADAT_SYNC;
26271da177e4SLinus Torvalds 	case HDSP_SelSyncRef_SPDIF:
26281da177e4SLinus Torvalds 		return HDSP_AUTOSYNC_FROM_SPDIF;
26291da177e4SLinus Torvalds 	case HDSP_SelSyncRefMask:
26301da177e4SLinus Torvalds 		return HDSP_AUTOSYNC_FROM_NONE;
26311da177e4SLinus Torvalds 	case HDSP_SelSyncRef_ADAT1:
26321da177e4SLinus Torvalds 		return HDSP_AUTOSYNC_FROM_ADAT1;
26331da177e4SLinus Torvalds 	case HDSP_SelSyncRef_ADAT2:
26341da177e4SLinus Torvalds 		return HDSP_AUTOSYNC_FROM_ADAT2;
26351da177e4SLinus Torvalds 	case HDSP_SelSyncRef_ADAT3:
26361da177e4SLinus Torvalds 		return HDSP_AUTOSYNC_FROM_ADAT3;
26371da177e4SLinus Torvalds 	default:
26381da177e4SLinus Torvalds 		return HDSP_AUTOSYNC_FROM_WORD;
26391da177e4SLinus Torvalds 	}
26401da177e4SLinus Torvalds 	return 0;
26411da177e4SLinus Torvalds }
26421da177e4SLinus Torvalds 
264355e957d8STakashi Iwai static int snd_hdsp_info_autosync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
26441da177e4SLinus Torvalds {
26451da177e4SLinus Torvalds 	static char *texts[] = {"Word", "ADAT Sync", "IEC958", "None", "ADAT1", "ADAT2", "ADAT3" };
26461da177e4SLinus Torvalds 
26471da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
26481da177e4SLinus Torvalds 	uinfo->count = 1;
26491da177e4SLinus Torvalds 	uinfo->value.enumerated.items = 7;
26501da177e4SLinus Torvalds 	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
26511da177e4SLinus Torvalds 		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
26521da177e4SLinus Torvalds 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
26531da177e4SLinus Torvalds 	return 0;
26541da177e4SLinus Torvalds }
26551da177e4SLinus Torvalds 
265655e957d8STakashi Iwai static int snd_hdsp_get_autosync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
26571da177e4SLinus Torvalds {
265855e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
26591da177e4SLinus Torvalds 
26601da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp_autosync_ref(hdsp);
26611da177e4SLinus Torvalds 	return 0;
26621da177e4SLinus Torvalds }
26631da177e4SLinus Torvalds 
26641da177e4SLinus Torvalds #define HDSP_LINE_OUT(xname, xindex) \
266567ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
26661da177e4SLinus Torvalds   .name = xname, \
26671da177e4SLinus Torvalds   .index = xindex, \
26681da177e4SLinus Torvalds   .info = snd_hdsp_info_line_out, \
26691da177e4SLinus Torvalds   .get = snd_hdsp_get_line_out, \
26701da177e4SLinus Torvalds   .put = snd_hdsp_put_line_out \
26711da177e4SLinus Torvalds }
26721da177e4SLinus Torvalds 
267355e957d8STakashi Iwai static int hdsp_line_out(struct hdsp *hdsp)
26741da177e4SLinus Torvalds {
26751da177e4SLinus Torvalds 	return (hdsp->control_register & HDSP_LineOut) ? 1 : 0;
26761da177e4SLinus Torvalds }
26771da177e4SLinus Torvalds 
267855e957d8STakashi Iwai static int hdsp_set_line_output(struct hdsp *hdsp, int out)
26791da177e4SLinus Torvalds {
2680b0b98119STakashi Iwai 	if (out)
26811da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_LineOut;
2682b0b98119STakashi Iwai 	else
26831da177e4SLinus Torvalds 		hdsp->control_register &= ~HDSP_LineOut;
26841da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
26851da177e4SLinus Torvalds 	return 0;
26861da177e4SLinus Torvalds }
26871da177e4SLinus Torvalds 
268855e957d8STakashi Iwai static int snd_hdsp_info_line_out(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
26891da177e4SLinus Torvalds {
26901da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
26911da177e4SLinus Torvalds 	uinfo->count = 1;
26921da177e4SLinus Torvalds 	uinfo->value.integer.min = 0;
26931da177e4SLinus Torvalds 	uinfo->value.integer.max = 1;
26941da177e4SLinus Torvalds 	return 0;
26951da177e4SLinus Torvalds }
26961da177e4SLinus Torvalds 
269755e957d8STakashi Iwai static int snd_hdsp_get_line_out(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
26981da177e4SLinus Torvalds {
269955e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
27001da177e4SLinus Torvalds 
27011da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
27021da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = hdsp_line_out(hdsp);
27031da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
27041da177e4SLinus Torvalds 	return 0;
27051da177e4SLinus Torvalds }
27061da177e4SLinus Torvalds 
270755e957d8STakashi Iwai static int snd_hdsp_put_line_out(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
27081da177e4SLinus Torvalds {
270955e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
27101da177e4SLinus Torvalds 	int change;
27111da177e4SLinus Torvalds 	unsigned int val;
27121da177e4SLinus Torvalds 
27131da177e4SLinus Torvalds 	if (!snd_hdsp_use_is_exclusive(hdsp))
27141da177e4SLinus Torvalds 		return -EBUSY;
27151da177e4SLinus Torvalds 	val = ucontrol->value.integer.value[0] & 1;
27161da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
27171da177e4SLinus Torvalds 	change = (int)val != hdsp_line_out(hdsp);
27181da177e4SLinus Torvalds 	hdsp_set_line_output(hdsp, val);
27191da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
27201da177e4SLinus Torvalds 	return change;
27211da177e4SLinus Torvalds }
27221da177e4SLinus Torvalds 
27231da177e4SLinus Torvalds #define HDSP_PRECISE_POINTER(xname, xindex) \
272467ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_CARD, \
27251da177e4SLinus Torvalds   .name = xname, \
27261da177e4SLinus Torvalds   .index = xindex, \
27271da177e4SLinus Torvalds   .info = snd_hdsp_info_precise_pointer, \
27281da177e4SLinus Torvalds   .get = snd_hdsp_get_precise_pointer, \
27291da177e4SLinus Torvalds   .put = snd_hdsp_put_precise_pointer \
27301da177e4SLinus Torvalds }
27311da177e4SLinus Torvalds 
273255e957d8STakashi Iwai static int hdsp_set_precise_pointer(struct hdsp *hdsp, int precise)
27331da177e4SLinus Torvalds {
2734b0b98119STakashi Iwai 	if (precise)
27351da177e4SLinus Torvalds 		hdsp->precise_ptr = 1;
2736b0b98119STakashi Iwai 	else
27371da177e4SLinus Torvalds 		hdsp->precise_ptr = 0;
27381da177e4SLinus Torvalds 	return 0;
27391da177e4SLinus Torvalds }
27401da177e4SLinus Torvalds 
274155e957d8STakashi Iwai static int snd_hdsp_info_precise_pointer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
27421da177e4SLinus Torvalds {
27431da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
27441da177e4SLinus Torvalds 	uinfo->count = 1;
27451da177e4SLinus Torvalds 	uinfo->value.integer.min = 0;
27461da177e4SLinus Torvalds 	uinfo->value.integer.max = 1;
27471da177e4SLinus Torvalds 	return 0;
27481da177e4SLinus Torvalds }
27491da177e4SLinus Torvalds 
275055e957d8STakashi Iwai static int snd_hdsp_get_precise_pointer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
27511da177e4SLinus Torvalds {
275255e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
27531da177e4SLinus Torvalds 
27541da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
27551da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = hdsp->precise_ptr;
27561da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
27571da177e4SLinus Torvalds 	return 0;
27581da177e4SLinus Torvalds }
27591da177e4SLinus Torvalds 
276055e957d8STakashi Iwai static int snd_hdsp_put_precise_pointer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
27611da177e4SLinus Torvalds {
276255e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
27631da177e4SLinus Torvalds 	int change;
27641da177e4SLinus Torvalds 	unsigned int val;
27651da177e4SLinus Torvalds 
27661da177e4SLinus Torvalds 	if (!snd_hdsp_use_is_exclusive(hdsp))
27671da177e4SLinus Torvalds 		return -EBUSY;
27681da177e4SLinus Torvalds 	val = ucontrol->value.integer.value[0] & 1;
27691da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
27701da177e4SLinus Torvalds 	change = (int)val != hdsp->precise_ptr;
27711da177e4SLinus Torvalds 	hdsp_set_precise_pointer(hdsp, val);
27721da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
27731da177e4SLinus Torvalds 	return change;
27741da177e4SLinus Torvalds }
27751da177e4SLinus Torvalds 
27761da177e4SLinus Torvalds #define HDSP_USE_MIDI_TASKLET(xname, xindex) \
277767ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_CARD, \
27781da177e4SLinus Torvalds   .name = xname, \
27791da177e4SLinus Torvalds   .index = xindex, \
27801da177e4SLinus Torvalds   .info = snd_hdsp_info_use_midi_tasklet, \
27811da177e4SLinus Torvalds   .get = snd_hdsp_get_use_midi_tasklet, \
27821da177e4SLinus Torvalds   .put = snd_hdsp_put_use_midi_tasklet \
27831da177e4SLinus Torvalds }
27841da177e4SLinus Torvalds 
278555e957d8STakashi Iwai static int hdsp_set_use_midi_tasklet(struct hdsp *hdsp, int use_tasklet)
27861da177e4SLinus Torvalds {
2787b0b98119STakashi Iwai 	if (use_tasklet)
27881da177e4SLinus Torvalds 		hdsp->use_midi_tasklet = 1;
2789b0b98119STakashi Iwai 	else
27901da177e4SLinus Torvalds 		hdsp->use_midi_tasklet = 0;
27911da177e4SLinus Torvalds 	return 0;
27921da177e4SLinus Torvalds }
27931da177e4SLinus Torvalds 
279455e957d8STakashi Iwai static int snd_hdsp_info_use_midi_tasklet(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
27951da177e4SLinus Torvalds {
27961da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
27971da177e4SLinus Torvalds 	uinfo->count = 1;
27981da177e4SLinus Torvalds 	uinfo->value.integer.min = 0;
27991da177e4SLinus Torvalds 	uinfo->value.integer.max = 1;
28001da177e4SLinus Torvalds 	return 0;
28011da177e4SLinus Torvalds }
28021da177e4SLinus Torvalds 
280355e957d8STakashi Iwai static int snd_hdsp_get_use_midi_tasklet(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
28041da177e4SLinus Torvalds {
280555e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
28061da177e4SLinus Torvalds 
28071da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
28081da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = hdsp->use_midi_tasklet;
28091da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
28101da177e4SLinus Torvalds 	return 0;
28111da177e4SLinus Torvalds }
28121da177e4SLinus Torvalds 
281355e957d8STakashi Iwai static int snd_hdsp_put_use_midi_tasklet(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
28141da177e4SLinus Torvalds {
281555e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
28161da177e4SLinus Torvalds 	int change;
28171da177e4SLinus Torvalds 	unsigned int val;
28181da177e4SLinus Torvalds 
28191da177e4SLinus Torvalds 	if (!snd_hdsp_use_is_exclusive(hdsp))
28201da177e4SLinus Torvalds 		return -EBUSY;
28211da177e4SLinus Torvalds 	val = ucontrol->value.integer.value[0] & 1;
28221da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
28231da177e4SLinus Torvalds 	change = (int)val != hdsp->use_midi_tasklet;
28241da177e4SLinus Torvalds 	hdsp_set_use_midi_tasklet(hdsp, val);
28251da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
28261da177e4SLinus Torvalds 	return change;
28271da177e4SLinus Torvalds }
28281da177e4SLinus Torvalds 
28291da177e4SLinus Torvalds #define HDSP_MIXER(xname, xindex) \
28301da177e4SLinus Torvalds { .iface = SNDRV_CTL_ELEM_IFACE_HWDEP, \
28311da177e4SLinus Torvalds   .name = xname, \
28321da177e4SLinus Torvalds   .index = xindex, \
283367ed4161SClemens Ladisch   .device = 0, \
28341da177e4SLinus Torvalds   .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
28351da177e4SLinus Torvalds 		 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
28361da177e4SLinus Torvalds   .info = snd_hdsp_info_mixer, \
28371da177e4SLinus Torvalds   .get = snd_hdsp_get_mixer, \
28381da177e4SLinus Torvalds   .put = snd_hdsp_put_mixer \
28391da177e4SLinus Torvalds }
28401da177e4SLinus Torvalds 
284155e957d8STakashi Iwai static int snd_hdsp_info_mixer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
28421da177e4SLinus Torvalds {
28431da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
28441da177e4SLinus Torvalds 	uinfo->count = 3;
28451da177e4SLinus Torvalds 	uinfo->value.integer.min = 0;
28461da177e4SLinus Torvalds 	uinfo->value.integer.max = 65536;
28471da177e4SLinus Torvalds 	uinfo->value.integer.step = 1;
28481da177e4SLinus Torvalds 	return 0;
28491da177e4SLinus Torvalds }
28501da177e4SLinus Torvalds 
285155e957d8STakashi Iwai static int snd_hdsp_get_mixer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
28521da177e4SLinus Torvalds {
285355e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
28541da177e4SLinus Torvalds 	int source;
28551da177e4SLinus Torvalds 	int destination;
28561da177e4SLinus Torvalds 	int addr;
28571da177e4SLinus Torvalds 
28581da177e4SLinus Torvalds 	source = ucontrol->value.integer.value[0];
28591da177e4SLinus Torvalds 	destination = ucontrol->value.integer.value[1];
28601da177e4SLinus Torvalds 
2861b0b98119STakashi Iwai 	if (source >= hdsp->max_channels)
28621da177e4SLinus Torvalds 		addr = hdsp_playback_to_output_key(hdsp,source-hdsp->max_channels,destination);
2863b0b98119STakashi Iwai 	else
28641da177e4SLinus Torvalds 		addr = hdsp_input_to_output_key(hdsp,source, destination);
28651da177e4SLinus Torvalds 
28661da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
28671da177e4SLinus Torvalds 	ucontrol->value.integer.value[2] = hdsp_read_gain (hdsp, addr);
28681da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
28691da177e4SLinus Torvalds 	return 0;
28701da177e4SLinus Torvalds }
28711da177e4SLinus Torvalds 
287255e957d8STakashi Iwai static int snd_hdsp_put_mixer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
28731da177e4SLinus Torvalds {
287455e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
28751da177e4SLinus Torvalds 	int change;
28761da177e4SLinus Torvalds 	int source;
28771da177e4SLinus Torvalds 	int destination;
28781da177e4SLinus Torvalds 	int gain;
28791da177e4SLinus Torvalds 	int addr;
28801da177e4SLinus Torvalds 
28811da177e4SLinus Torvalds 	if (!snd_hdsp_use_is_exclusive(hdsp))
28821da177e4SLinus Torvalds 		return -EBUSY;
28831da177e4SLinus Torvalds 
28841da177e4SLinus Torvalds 	source = ucontrol->value.integer.value[0];
28851da177e4SLinus Torvalds 	destination = ucontrol->value.integer.value[1];
28861da177e4SLinus Torvalds 
2887b0b98119STakashi Iwai 	if (source >= hdsp->max_channels)
28881da177e4SLinus Torvalds 		addr = hdsp_playback_to_output_key(hdsp,source-hdsp->max_channels, destination);
2889b0b98119STakashi Iwai 	else
28901da177e4SLinus Torvalds 		addr = hdsp_input_to_output_key(hdsp,source, destination);
28911da177e4SLinus Torvalds 
28921da177e4SLinus Torvalds 	gain = ucontrol->value.integer.value[2];
28931da177e4SLinus Torvalds 
28941da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
28951da177e4SLinus Torvalds 	change = gain != hdsp_read_gain(hdsp, addr);
28961da177e4SLinus Torvalds 	if (change)
28971da177e4SLinus Torvalds 		hdsp_write_gain(hdsp, addr, gain);
28981da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
28991da177e4SLinus Torvalds 	return change;
29001da177e4SLinus Torvalds }
29011da177e4SLinus Torvalds 
29021da177e4SLinus Torvalds #define HDSP_WC_SYNC_CHECK(xname, xindex) \
290367ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
29041da177e4SLinus Torvalds   .name = xname, \
29051da177e4SLinus Torvalds   .index = xindex, \
29061da177e4SLinus Torvalds   .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
29071da177e4SLinus Torvalds   .info = snd_hdsp_info_sync_check, \
29081da177e4SLinus Torvalds   .get = snd_hdsp_get_wc_sync_check \
29091da177e4SLinus Torvalds }
29101da177e4SLinus Torvalds 
291155e957d8STakashi Iwai static int snd_hdsp_info_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
29121da177e4SLinus Torvalds {
29131da177e4SLinus Torvalds 	static char *texts[] = {"No Lock", "Lock", "Sync" };
29141da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
29151da177e4SLinus Torvalds 	uinfo->count = 1;
29161da177e4SLinus Torvalds 	uinfo->value.enumerated.items = 3;
29171da177e4SLinus Torvalds 	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
29181da177e4SLinus Torvalds 		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
29191da177e4SLinus Torvalds 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
29201da177e4SLinus Torvalds 	return 0;
29211da177e4SLinus Torvalds }
29221da177e4SLinus Torvalds 
292355e957d8STakashi Iwai static int hdsp_wc_sync_check(struct hdsp *hdsp)
29241da177e4SLinus Torvalds {
29251da177e4SLinus Torvalds 	int status2 = hdsp_read(hdsp, HDSP_status2Register);
29261da177e4SLinus Torvalds 	if (status2 & HDSP_wc_lock) {
2927b0b98119STakashi Iwai 		if (status2 & HDSP_wc_sync)
29281da177e4SLinus Torvalds 			return 2;
2929b0b98119STakashi Iwai 		else
29301da177e4SLinus Torvalds 			 return 1;
2931b0b98119STakashi Iwai 	} else
29321da177e4SLinus Torvalds 		return 0;
29331da177e4SLinus Torvalds 	return 0;
29341da177e4SLinus Torvalds }
29351da177e4SLinus Torvalds 
293655e957d8STakashi Iwai static int snd_hdsp_get_wc_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
29371da177e4SLinus Torvalds {
293855e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
29391da177e4SLinus Torvalds 
29401da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp_wc_sync_check(hdsp);
29411da177e4SLinus Torvalds 	return 0;
29421da177e4SLinus Torvalds }
29431da177e4SLinus Torvalds 
29441da177e4SLinus Torvalds #define HDSP_SPDIF_SYNC_CHECK(xname, xindex) \
294567ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
29461da177e4SLinus Torvalds   .name = xname, \
29471da177e4SLinus Torvalds   .index = xindex, \
29481da177e4SLinus Torvalds   .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
29491da177e4SLinus Torvalds   .info = snd_hdsp_info_sync_check, \
29501da177e4SLinus Torvalds   .get = snd_hdsp_get_spdif_sync_check \
29511da177e4SLinus Torvalds }
29521da177e4SLinus Torvalds 
295355e957d8STakashi Iwai static int hdsp_spdif_sync_check(struct hdsp *hdsp)
29541da177e4SLinus Torvalds {
29551da177e4SLinus Torvalds 	int status = hdsp_read(hdsp, HDSP_statusRegister);
2956b0b98119STakashi Iwai 	if (status & HDSP_SPDIFErrorFlag)
29571da177e4SLinus Torvalds 		return 0;
2958b0b98119STakashi Iwai 	else {
2959b0b98119STakashi Iwai 		if (status & HDSP_SPDIFSync)
29601da177e4SLinus Torvalds 			return 2;
2961b0b98119STakashi Iwai 		else
29621da177e4SLinus Torvalds 			return 1;
29631da177e4SLinus Torvalds 	}
29641da177e4SLinus Torvalds 	return 0;
29651da177e4SLinus Torvalds }
29661da177e4SLinus Torvalds 
296755e957d8STakashi Iwai static int snd_hdsp_get_spdif_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
29681da177e4SLinus Torvalds {
296955e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
29701da177e4SLinus Torvalds 
29711da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp_spdif_sync_check(hdsp);
29721da177e4SLinus Torvalds 	return 0;
29731da177e4SLinus Torvalds }
29741da177e4SLinus Torvalds 
29751da177e4SLinus Torvalds #define HDSP_ADATSYNC_SYNC_CHECK(xname, xindex) \
297667ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
29771da177e4SLinus Torvalds   .name = xname, \
29781da177e4SLinus Torvalds   .index = xindex, \
29791da177e4SLinus Torvalds   .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
29801da177e4SLinus Torvalds   .info = snd_hdsp_info_sync_check, \
29811da177e4SLinus Torvalds   .get = snd_hdsp_get_adatsync_sync_check \
29821da177e4SLinus Torvalds }
29831da177e4SLinus Torvalds 
298455e957d8STakashi Iwai static int hdsp_adatsync_sync_check(struct hdsp *hdsp)
29851da177e4SLinus Torvalds {
29861da177e4SLinus Torvalds 	int status = hdsp_read(hdsp, HDSP_statusRegister);
29871da177e4SLinus Torvalds 	if (status & HDSP_TimecodeLock) {
2988b0b98119STakashi Iwai 		if (status & HDSP_TimecodeSync)
29891da177e4SLinus Torvalds 			return 2;
2990b0b98119STakashi Iwai 		else
29911da177e4SLinus Torvalds 			return 1;
2992b0b98119STakashi Iwai 	} else
29931da177e4SLinus Torvalds 		return 0;
29941da177e4SLinus Torvalds }
29951da177e4SLinus Torvalds 
299655e957d8STakashi Iwai static int snd_hdsp_get_adatsync_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
29971da177e4SLinus Torvalds {
299855e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
29991da177e4SLinus Torvalds 
30001da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp_adatsync_sync_check(hdsp);
30011da177e4SLinus Torvalds 	return 0;
30021da177e4SLinus Torvalds }
30031da177e4SLinus Torvalds 
30041da177e4SLinus Torvalds #define HDSP_ADAT_SYNC_CHECK \
300567ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
30061da177e4SLinus Torvalds   .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
30071da177e4SLinus Torvalds   .info = snd_hdsp_info_sync_check, \
30081da177e4SLinus Torvalds   .get = snd_hdsp_get_adat_sync_check \
30091da177e4SLinus Torvalds }
30101da177e4SLinus Torvalds 
301155e957d8STakashi Iwai static int hdsp_adat_sync_check(struct hdsp *hdsp, int idx)
30121da177e4SLinus Torvalds {
30131da177e4SLinus Torvalds 	int status = hdsp_read(hdsp, HDSP_statusRegister);
30141da177e4SLinus Torvalds 
30151da177e4SLinus Torvalds 	if (status & (HDSP_Lock0>>idx)) {
3016b0b98119STakashi Iwai 		if (status & (HDSP_Sync0>>idx))
30171da177e4SLinus Torvalds 			return 2;
3018b0b98119STakashi Iwai 		else
30191da177e4SLinus Torvalds 			return 1;
3020b0b98119STakashi Iwai 	} else
30211da177e4SLinus Torvalds 		return 0;
30221da177e4SLinus Torvalds }
30231da177e4SLinus Torvalds 
302455e957d8STakashi Iwai static int snd_hdsp_get_adat_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
30251da177e4SLinus Torvalds {
30261da177e4SLinus Torvalds 	int offset;
302755e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
30281da177e4SLinus Torvalds 
30291da177e4SLinus Torvalds 	offset = ucontrol->id.index - 1;
30301da177e4SLinus Torvalds 	snd_assert(offset >= 0);
30311da177e4SLinus Torvalds 
30321da177e4SLinus Torvalds 	switch (hdsp->io_type) {
30331da177e4SLinus Torvalds 	case Digiface:
30341da177e4SLinus Torvalds 	case H9652:
30351da177e4SLinus Torvalds 		if (offset >= 3)
30361da177e4SLinus Torvalds 			return -EINVAL;
30371da177e4SLinus Torvalds 		break;
30381da177e4SLinus Torvalds 	case Multiface:
30391da177e4SLinus Torvalds 	case H9632:
30401da177e4SLinus Torvalds 		if (offset >= 1)
30411da177e4SLinus Torvalds 			return -EINVAL;
30421da177e4SLinus Torvalds 		break;
30431da177e4SLinus Torvalds 	default:
30441da177e4SLinus Torvalds 		return -EIO;
30451da177e4SLinus Torvalds 	}
30461da177e4SLinus Torvalds 
30471da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp_adat_sync_check(hdsp, offset);
30481da177e4SLinus Torvalds 	return 0;
30491da177e4SLinus Torvalds }
30501da177e4SLinus Torvalds 
305155e957d8STakashi Iwai static struct snd_kcontrol_new snd_hdsp_9632_controls[] = {
30521da177e4SLinus Torvalds HDSP_DA_GAIN("DA Gain", 0),
30531da177e4SLinus Torvalds HDSP_AD_GAIN("AD Gain", 0),
30541da177e4SLinus Torvalds HDSP_PHONE_GAIN("Phones Gain", 0),
30551da177e4SLinus Torvalds HDSP_XLR_BREAKOUT_CABLE("XLR Breakout Cable", 0)
30561da177e4SLinus Torvalds };
30571da177e4SLinus Torvalds 
305855e957d8STakashi Iwai static struct snd_kcontrol_new snd_hdsp_controls[] = {
30591da177e4SLinus Torvalds {
30605549d549SClemens Ladisch 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
30611da177e4SLinus Torvalds 	.name =		SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
30621da177e4SLinus Torvalds 	.info =		snd_hdsp_control_spdif_info,
30631da177e4SLinus Torvalds 	.get =		snd_hdsp_control_spdif_get,
30641da177e4SLinus Torvalds 	.put =		snd_hdsp_control_spdif_put,
30651da177e4SLinus Torvalds },
30661da177e4SLinus Torvalds {
30671da177e4SLinus Torvalds 	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
30685549d549SClemens Ladisch 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
30691da177e4SLinus Torvalds 	.name =		SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
30701da177e4SLinus Torvalds 	.info =		snd_hdsp_control_spdif_stream_info,
30711da177e4SLinus Torvalds 	.get =		snd_hdsp_control_spdif_stream_get,
30721da177e4SLinus Torvalds 	.put =		snd_hdsp_control_spdif_stream_put,
30731da177e4SLinus Torvalds },
30741da177e4SLinus Torvalds {
30751da177e4SLinus Torvalds 	.access =	SNDRV_CTL_ELEM_ACCESS_READ,
30765549d549SClemens Ladisch 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
30771da177e4SLinus Torvalds 	.name =		SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
30781da177e4SLinus Torvalds 	.info =		snd_hdsp_control_spdif_mask_info,
30791da177e4SLinus Torvalds 	.get =		snd_hdsp_control_spdif_mask_get,
30801da177e4SLinus Torvalds 	.private_value = IEC958_AES0_NONAUDIO |
30811da177e4SLinus Torvalds   			 IEC958_AES0_PROFESSIONAL |
30821da177e4SLinus Torvalds 			 IEC958_AES0_CON_EMPHASIS,
30831da177e4SLinus Torvalds },
30841da177e4SLinus Torvalds {
30851da177e4SLinus Torvalds 	.access =	SNDRV_CTL_ELEM_ACCESS_READ,
30865549d549SClemens Ladisch 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
30871da177e4SLinus Torvalds 	.name =		SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
30881da177e4SLinus Torvalds 	.info =		snd_hdsp_control_spdif_mask_info,
30891da177e4SLinus Torvalds 	.get =		snd_hdsp_control_spdif_mask_get,
30901da177e4SLinus Torvalds 	.private_value = IEC958_AES0_NONAUDIO |
30911da177e4SLinus Torvalds 			 IEC958_AES0_PROFESSIONAL |
30921da177e4SLinus Torvalds 			 IEC958_AES0_PRO_EMPHASIS,
30931da177e4SLinus Torvalds },
30941da177e4SLinus Torvalds HDSP_MIXER("Mixer", 0),
30951da177e4SLinus Torvalds HDSP_SPDIF_IN("IEC958 Input Connector", 0),
30961da177e4SLinus Torvalds HDSP_SPDIF_OUT("IEC958 Output also on ADAT1", 0),
30971da177e4SLinus Torvalds HDSP_SPDIF_PROFESSIONAL("IEC958 Professional Bit", 0),
30981da177e4SLinus Torvalds HDSP_SPDIF_EMPHASIS("IEC958 Emphasis Bit", 0),
30991da177e4SLinus Torvalds HDSP_SPDIF_NON_AUDIO("IEC958 Non-audio Bit", 0),
31001da177e4SLinus Torvalds /* 'Sample Clock Source' complies with the alsa control naming scheme */
31011da177e4SLinus Torvalds HDSP_CLOCK_SOURCE("Sample Clock Source", 0),
3102e3ea4d89STakashi Iwai {
3103e3ea4d89STakashi Iwai 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3104e3ea4d89STakashi Iwai 	.name = "Sample Clock Source Locking",
3105e3ea4d89STakashi Iwai 	.info = snd_hdsp_info_clock_source_lock,
3106e3ea4d89STakashi Iwai 	.get = snd_hdsp_get_clock_source_lock,
3107e3ea4d89STakashi Iwai 	.put = snd_hdsp_put_clock_source_lock,
3108e3ea4d89STakashi Iwai },
31091da177e4SLinus Torvalds HDSP_SYSTEM_CLOCK_MODE("System Clock Mode", 0),
31101da177e4SLinus Torvalds HDSP_PREF_SYNC_REF("Preferred Sync Reference", 0),
31111da177e4SLinus Torvalds HDSP_AUTOSYNC_REF("AutoSync Reference", 0),
31121da177e4SLinus Torvalds HDSP_SPDIF_SAMPLE_RATE("SPDIF Sample Rate", 0),
31131da177e4SLinus Torvalds HDSP_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
31141da177e4SLinus Torvalds /* 'External Rate' complies with the alsa control naming scheme */
31151da177e4SLinus Torvalds HDSP_AUTOSYNC_SAMPLE_RATE("External Rate", 0),
31161da177e4SLinus Torvalds HDSP_WC_SYNC_CHECK("Word Clock Lock Status", 0),
31171da177e4SLinus Torvalds HDSP_SPDIF_SYNC_CHECK("SPDIF Lock Status", 0),
31181da177e4SLinus Torvalds HDSP_ADATSYNC_SYNC_CHECK("ADAT Sync Lock Status", 0),
31191da177e4SLinus Torvalds HDSP_LINE_OUT("Line Out", 0),
31201da177e4SLinus Torvalds HDSP_PRECISE_POINTER("Precise Pointer", 0),
31211da177e4SLinus Torvalds HDSP_USE_MIDI_TASKLET("Use Midi Tasklet", 0),
31221da177e4SLinus Torvalds };
31231da177e4SLinus Torvalds 
312455e957d8STakashi Iwai static struct snd_kcontrol_new snd_hdsp_96xx_aeb = HDSP_AEB("Analog Extension Board", 0);
312555e957d8STakashi Iwai static struct snd_kcontrol_new snd_hdsp_adat_sync_check = HDSP_ADAT_SYNC_CHECK;
31261da177e4SLinus Torvalds 
312755e957d8STakashi Iwai static int snd_hdsp_create_controls(struct snd_card *card, struct hdsp *hdsp)
31281da177e4SLinus Torvalds {
31291da177e4SLinus Torvalds 	unsigned int idx;
31301da177e4SLinus Torvalds 	int err;
313155e957d8STakashi Iwai 	struct snd_kcontrol *kctl;
31321da177e4SLinus Torvalds 
31331da177e4SLinus Torvalds 	for (idx = 0; idx < ARRAY_SIZE(snd_hdsp_controls); idx++) {
3134b0b98119STakashi Iwai 		if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_controls[idx], hdsp))) < 0)
31351da177e4SLinus Torvalds 			return err;
31361da177e4SLinus Torvalds 		if (idx == 1)	/* IEC958 (S/PDIF) Stream */
31371da177e4SLinus Torvalds 			hdsp->spdif_ctl = kctl;
31381da177e4SLinus Torvalds 	}
31391da177e4SLinus Torvalds 
31401da177e4SLinus Torvalds 	/* ADAT SyncCheck status */
31411da177e4SLinus Torvalds 	snd_hdsp_adat_sync_check.name = "ADAT Lock Status";
31421da177e4SLinus Torvalds 	snd_hdsp_adat_sync_check.index = 1;
3143b0b98119STakashi Iwai 	if ((err = snd_ctl_add (card, kctl = snd_ctl_new1(&snd_hdsp_adat_sync_check, hdsp))))
31441da177e4SLinus Torvalds 		return err;
31451da177e4SLinus Torvalds 	if (hdsp->io_type == Digiface || hdsp->io_type == H9652) {
31461da177e4SLinus Torvalds 		for (idx = 1; idx < 3; ++idx) {
31471da177e4SLinus Torvalds 			snd_hdsp_adat_sync_check.index = idx+1;
3148b0b98119STakashi Iwai 			if ((err = snd_ctl_add (card, kctl = snd_ctl_new1(&snd_hdsp_adat_sync_check, hdsp))))
31491da177e4SLinus Torvalds 				return err;
31501da177e4SLinus Torvalds 		}
31511da177e4SLinus Torvalds 	}
31521da177e4SLinus Torvalds 
31531da177e4SLinus Torvalds 	/* DA, AD and Phone gain and XLR breakout cable controls for H9632 cards */
31541da177e4SLinus Torvalds 	if (hdsp->io_type == H9632) {
31551da177e4SLinus Torvalds 		for (idx = 0; idx < ARRAY_SIZE(snd_hdsp_9632_controls); idx++) {
3156b0b98119STakashi Iwai 			if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_9632_controls[idx], hdsp))) < 0)
31571da177e4SLinus Torvalds 				return err;
31581da177e4SLinus Torvalds 		}
31591da177e4SLinus Torvalds 	}
31601da177e4SLinus Torvalds 
31611da177e4SLinus Torvalds 	/* AEB control for H96xx card */
31621da177e4SLinus Torvalds 	if (hdsp->io_type == H9632 || hdsp->io_type == H9652) {
3163b0b98119STakashi Iwai 		if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_96xx_aeb, hdsp))) < 0)
31641da177e4SLinus Torvalds 				return err;
31651da177e4SLinus Torvalds 	}
31661da177e4SLinus Torvalds 
31671da177e4SLinus Torvalds 	return 0;
31681da177e4SLinus Torvalds }
31691da177e4SLinus Torvalds 
31701da177e4SLinus Torvalds /*------------------------------------------------------------
31711da177e4SLinus Torvalds    /proc interface
31721da177e4SLinus Torvalds  ------------------------------------------------------------*/
31731da177e4SLinus Torvalds 
31741da177e4SLinus Torvalds static void
317555e957d8STakashi Iwai snd_hdsp_proc_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
31761da177e4SLinus Torvalds {
317755e957d8STakashi Iwai 	struct hdsp *hdsp = (struct hdsp *) entry->private_data;
31781da177e4SLinus Torvalds 	unsigned int status;
31791da177e4SLinus Torvalds 	unsigned int status2;
31801da177e4SLinus Torvalds 	char *pref_sync_ref;
31811da177e4SLinus Torvalds 	char *autosync_ref;
31821da177e4SLinus Torvalds 	char *system_clock_mode;
31831da177e4SLinus Torvalds 	char *clock_source;
31841da177e4SLinus Torvalds 	int x;
31851da177e4SLinus Torvalds 
3186ecb594e6SRemy Bruno 	if (hdsp_check_for_iobox (hdsp)) {
31871da177e4SLinus Torvalds 		snd_iprintf(buffer, "No I/O box connected.\nPlease connect one and upload firmware.\n");
31881da177e4SLinus Torvalds 		return;
3189ecb594e6SRemy Bruno         }
31901da177e4SLinus Torvalds 
3191b0b98119STakashi Iwai 	if (hdsp_check_for_firmware(hdsp, 0)) {
31921da177e4SLinus Torvalds 		if (hdsp->state & HDSP_FirmwareCached) {
31931da177e4SLinus Torvalds 			if (snd_hdsp_load_firmware_from_cache(hdsp) != 0) {
31941da177e4SLinus Torvalds 				snd_iprintf(buffer, "Firmware loading from cache failed, please upload manually.\n");
31951da177e4SLinus Torvalds 				return;
31961da177e4SLinus Torvalds 			}
31971da177e4SLinus Torvalds 		} else {
3198311e70a4STakashi Iwai 			int err = -EINVAL;
3199311e70a4STakashi Iwai #ifdef HDSP_FW_LOADER
3200311e70a4STakashi Iwai 			err = hdsp_request_fw_loader(hdsp);
3201311e70a4STakashi Iwai #endif
3202311e70a4STakashi Iwai 			if (err < 0) {
3203311e70a4STakashi Iwai 				snd_iprintf(buffer,
3204311e70a4STakashi Iwai 					    "No firmware loaded nor cached, "
3205311e70a4STakashi Iwai 					    "please upload firmware.\n");
32061da177e4SLinus Torvalds 				return;
32071da177e4SLinus Torvalds 			}
32081da177e4SLinus Torvalds 		}
3209311e70a4STakashi Iwai 	}
32101da177e4SLinus Torvalds 
32111da177e4SLinus Torvalds 	status = hdsp_read(hdsp, HDSP_statusRegister);
32121da177e4SLinus Torvalds 	status2 = hdsp_read(hdsp, HDSP_status2Register);
32131da177e4SLinus Torvalds 
32141da177e4SLinus Torvalds 	snd_iprintf(buffer, "%s (Card #%d)\n", hdsp->card_name, hdsp->card->number + 1);
32151da177e4SLinus Torvalds 	snd_iprintf(buffer, "Buffers: capture %p playback %p\n",
32161da177e4SLinus Torvalds 		    hdsp->capture_buffer, hdsp->playback_buffer);
32171da177e4SLinus Torvalds 	snd_iprintf(buffer, "IRQ: %d Registers bus: 0x%lx VM: 0x%lx\n",
32181da177e4SLinus Torvalds 		    hdsp->irq, hdsp->port, (unsigned long)hdsp->iobase);
32191da177e4SLinus Torvalds 	snd_iprintf(buffer, "Control register: 0x%x\n", hdsp->control_register);
32201da177e4SLinus Torvalds 	snd_iprintf(buffer, "Control2 register: 0x%x\n", hdsp->control2_register);
32211da177e4SLinus Torvalds 	snd_iprintf(buffer, "Status register: 0x%x\n", status);
32221da177e4SLinus Torvalds 	snd_iprintf(buffer, "Status2 register: 0x%x\n", status2);
32231da177e4SLinus Torvalds 	snd_iprintf(buffer, "FIFO status: %d\n", hdsp_read(hdsp, HDSP_fifoStatus) & 0xff);
32241da177e4SLinus Torvalds 	snd_iprintf(buffer, "MIDI1 Output status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusOut0));
32251da177e4SLinus Torvalds 	snd_iprintf(buffer, "MIDI1 Input status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusIn0));
32261da177e4SLinus Torvalds 	snd_iprintf(buffer, "MIDI2 Output status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusOut1));
32271da177e4SLinus Torvalds 	snd_iprintf(buffer, "MIDI2 Input status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusIn1));
32281da177e4SLinus Torvalds 	snd_iprintf(buffer, "Use Midi Tasklet: %s\n", hdsp->use_midi_tasklet ? "on" : "off");
32291da177e4SLinus Torvalds 
32301da177e4SLinus Torvalds 	snd_iprintf(buffer, "\n");
32311da177e4SLinus Torvalds 
32321da177e4SLinus Torvalds 	x = 1 << (6 + hdsp_decode_latency(hdsp->control_register & HDSP_LatencyMask));
32331da177e4SLinus Torvalds 
32341da177e4SLinus Torvalds 	snd_iprintf(buffer, "Buffer Size (Latency): %d samples (2 periods of %lu bytes)\n", x, (unsigned long) hdsp->period_bytes);
32351da177e4SLinus Torvalds 	snd_iprintf(buffer, "Hardware pointer (frames): %ld\n", hdsp_hw_pointer(hdsp));
32361da177e4SLinus Torvalds 	snd_iprintf(buffer, "Precise pointer: %s\n", hdsp->precise_ptr ? "on" : "off");
32371da177e4SLinus Torvalds 	snd_iprintf(buffer, "Line out: %s\n", (hdsp->control_register & HDSP_LineOut) ? "on" : "off");
32381da177e4SLinus Torvalds 
32391da177e4SLinus Torvalds 	snd_iprintf(buffer, "Firmware version: %d\n", (status2&HDSP_version0)|(status2&HDSP_version1)<<1|(status2&HDSP_version2)<<2);
32401da177e4SLinus Torvalds 
32411da177e4SLinus Torvalds 	snd_iprintf(buffer, "\n");
32421da177e4SLinus Torvalds 
32431da177e4SLinus Torvalds 
32441da177e4SLinus Torvalds 	switch (hdsp_clock_source(hdsp)) {
32451da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_AUTOSYNC:
32461da177e4SLinus Torvalds 		clock_source = "AutoSync";
32471da177e4SLinus Torvalds 		break;
32481da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_32KHZ:
32491da177e4SLinus Torvalds 		clock_source = "Internal 32 kHz";
32501da177e4SLinus Torvalds 		break;
32511da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_44_1KHZ:
32521da177e4SLinus Torvalds 		clock_source = "Internal 44.1 kHz";
32531da177e4SLinus Torvalds 		break;
32541da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_48KHZ:
32551da177e4SLinus Torvalds 		clock_source = "Internal 48 kHz";
32561da177e4SLinus Torvalds 		break;
32571da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_64KHZ:
32581da177e4SLinus Torvalds 		clock_source = "Internal 64 kHz";
32591da177e4SLinus Torvalds 		break;
32601da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_88_2KHZ:
32611da177e4SLinus Torvalds 		clock_source = "Internal 88.2 kHz";
32621da177e4SLinus Torvalds 		break;
32631da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_96KHZ:
32641da177e4SLinus Torvalds 		clock_source = "Internal 96 kHz";
32651da177e4SLinus Torvalds 		break;
32661da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_128KHZ:
32671da177e4SLinus Torvalds 		clock_source = "Internal 128 kHz";
32681da177e4SLinus Torvalds 		break;
32691da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_176_4KHZ:
32701da177e4SLinus Torvalds 		clock_source = "Internal 176.4 kHz";
32711da177e4SLinus Torvalds 		break;
32721da177e4SLinus Torvalds 		case HDSP_CLOCK_SOURCE_INTERNAL_192KHZ:
32731da177e4SLinus Torvalds 		clock_source = "Internal 192 kHz";
32741da177e4SLinus Torvalds 		break;
32751da177e4SLinus Torvalds 	default:
32761da177e4SLinus Torvalds 		clock_source = "Error";
32771da177e4SLinus Torvalds 	}
32781da177e4SLinus Torvalds 	snd_iprintf (buffer, "Sample Clock Source: %s\n", clock_source);
32791da177e4SLinus Torvalds 
3280b0b98119STakashi Iwai 	if (hdsp_system_clock_mode(hdsp))
32811da177e4SLinus Torvalds 		system_clock_mode = "Slave";
3282b0b98119STakashi Iwai 	else
32831da177e4SLinus Torvalds 		system_clock_mode = "Master";
32841da177e4SLinus Torvalds 
32851da177e4SLinus Torvalds 	switch (hdsp_pref_sync_ref (hdsp)) {
32861da177e4SLinus Torvalds 	case HDSP_SYNC_FROM_WORD:
32871da177e4SLinus Torvalds 		pref_sync_ref = "Word Clock";
32881da177e4SLinus Torvalds 		break;
32891da177e4SLinus Torvalds 	case HDSP_SYNC_FROM_ADAT_SYNC:
32901da177e4SLinus Torvalds 		pref_sync_ref = "ADAT Sync";
32911da177e4SLinus Torvalds 		break;
32921da177e4SLinus Torvalds 	case HDSP_SYNC_FROM_SPDIF:
32931da177e4SLinus Torvalds 		pref_sync_ref = "SPDIF";
32941da177e4SLinus Torvalds 		break;
32951da177e4SLinus Torvalds 	case HDSP_SYNC_FROM_ADAT1:
32961da177e4SLinus Torvalds 		pref_sync_ref = "ADAT1";
32971da177e4SLinus Torvalds 		break;
32981da177e4SLinus Torvalds 	case HDSP_SYNC_FROM_ADAT2:
32991da177e4SLinus Torvalds 		pref_sync_ref = "ADAT2";
33001da177e4SLinus Torvalds 		break;
33011da177e4SLinus Torvalds 	case HDSP_SYNC_FROM_ADAT3:
33021da177e4SLinus Torvalds 		pref_sync_ref = "ADAT3";
33031da177e4SLinus Torvalds 		break;
33041da177e4SLinus Torvalds 	default:
33051da177e4SLinus Torvalds 		pref_sync_ref = "Word Clock";
33061da177e4SLinus Torvalds 		break;
33071da177e4SLinus Torvalds 	}
33081da177e4SLinus Torvalds 	snd_iprintf (buffer, "Preferred Sync Reference: %s\n", pref_sync_ref);
33091da177e4SLinus Torvalds 
33101da177e4SLinus Torvalds 	switch (hdsp_autosync_ref (hdsp)) {
33111da177e4SLinus Torvalds 	case HDSP_AUTOSYNC_FROM_WORD:
33121da177e4SLinus Torvalds 		autosync_ref = "Word Clock";
33131da177e4SLinus Torvalds 		break;
33141da177e4SLinus Torvalds 	case HDSP_AUTOSYNC_FROM_ADAT_SYNC:
33151da177e4SLinus Torvalds 		autosync_ref = "ADAT Sync";
33161da177e4SLinus Torvalds 		break;
33171da177e4SLinus Torvalds 	case HDSP_AUTOSYNC_FROM_SPDIF:
33181da177e4SLinus Torvalds 		autosync_ref = "SPDIF";
33191da177e4SLinus Torvalds 		break;
33201da177e4SLinus Torvalds 	case HDSP_AUTOSYNC_FROM_NONE:
33211da177e4SLinus Torvalds 		autosync_ref = "None";
33221da177e4SLinus Torvalds 		break;
33231da177e4SLinus Torvalds 	case HDSP_AUTOSYNC_FROM_ADAT1:
33241da177e4SLinus Torvalds 		autosync_ref = "ADAT1";
33251da177e4SLinus Torvalds 		break;
33261da177e4SLinus Torvalds 	case HDSP_AUTOSYNC_FROM_ADAT2:
33271da177e4SLinus Torvalds 		autosync_ref = "ADAT2";
33281da177e4SLinus Torvalds 		break;
33291da177e4SLinus Torvalds 	case HDSP_AUTOSYNC_FROM_ADAT3:
33301da177e4SLinus Torvalds 		autosync_ref = "ADAT3";
33311da177e4SLinus Torvalds 		break;
33321da177e4SLinus Torvalds 	default:
33331da177e4SLinus Torvalds 		autosync_ref = "---";
33341da177e4SLinus Torvalds 		break;
33351da177e4SLinus Torvalds 	}
33361da177e4SLinus Torvalds 	snd_iprintf (buffer, "AutoSync Reference: %s\n", autosync_ref);
33371da177e4SLinus Torvalds 
33381da177e4SLinus Torvalds 	snd_iprintf (buffer, "AutoSync Frequency: %d\n", hdsp_external_sample_rate(hdsp));
33391da177e4SLinus Torvalds 
33401da177e4SLinus Torvalds 	snd_iprintf (buffer, "System Clock Mode: %s\n", system_clock_mode);
33411da177e4SLinus Torvalds 
33421da177e4SLinus Torvalds 	snd_iprintf (buffer, "System Clock Frequency: %d\n", hdsp->system_sample_rate);
3343e3ea4d89STakashi Iwai 	snd_iprintf (buffer, "System Clock Locked: %s\n", hdsp->clock_source_locked ? "Yes" : "No");
33441da177e4SLinus Torvalds 
33451da177e4SLinus Torvalds 	snd_iprintf(buffer, "\n");
33461da177e4SLinus Torvalds 
33471da177e4SLinus Torvalds 	switch (hdsp_spdif_in(hdsp)) {
33481da177e4SLinus Torvalds 	case HDSP_SPDIFIN_OPTICAL:
33491da177e4SLinus Torvalds 		snd_iprintf(buffer, "IEC958 input: Optical\n");
33501da177e4SLinus Torvalds 		break;
33511da177e4SLinus Torvalds 	case HDSP_SPDIFIN_COAXIAL:
33521da177e4SLinus Torvalds 		snd_iprintf(buffer, "IEC958 input: Coaxial\n");
33531da177e4SLinus Torvalds 		break;
33541da177e4SLinus Torvalds 	case HDSP_SPDIFIN_INTERNAL:
33551da177e4SLinus Torvalds 		snd_iprintf(buffer, "IEC958 input: Internal\n");
33561da177e4SLinus Torvalds 		break;
33571da177e4SLinus Torvalds 	case HDSP_SPDIFIN_AES:
33581da177e4SLinus Torvalds 		snd_iprintf(buffer, "IEC958 input: AES\n");
33591da177e4SLinus Torvalds 		break;
33601da177e4SLinus Torvalds 	default:
33611da177e4SLinus Torvalds 		snd_iprintf(buffer, "IEC958 input: ???\n");
33621da177e4SLinus Torvalds 		break;
33631da177e4SLinus Torvalds 	}
33641da177e4SLinus Torvalds 
3365b0b98119STakashi Iwai 	if (hdsp->control_register & HDSP_SPDIFOpticalOut)
33661da177e4SLinus Torvalds 		snd_iprintf(buffer, "IEC958 output: Coaxial & ADAT1\n");
3367b0b98119STakashi Iwai 	else
33681da177e4SLinus Torvalds 		snd_iprintf(buffer, "IEC958 output: Coaxial only\n");
33691da177e4SLinus Torvalds 
3370b0b98119STakashi Iwai 	if (hdsp->control_register & HDSP_SPDIFProfessional)
33711da177e4SLinus Torvalds 		snd_iprintf(buffer, "IEC958 quality: Professional\n");
3372b0b98119STakashi Iwai 	else
33731da177e4SLinus Torvalds 		snd_iprintf(buffer, "IEC958 quality: Consumer\n");
33741da177e4SLinus Torvalds 
3375b0b98119STakashi Iwai 	if (hdsp->control_register & HDSP_SPDIFEmphasis)
33761da177e4SLinus Torvalds 		snd_iprintf(buffer, "IEC958 emphasis: on\n");
3377b0b98119STakashi Iwai 	else
33781da177e4SLinus Torvalds 		snd_iprintf(buffer, "IEC958 emphasis: off\n");
33791da177e4SLinus Torvalds 
3380b0b98119STakashi Iwai 	if (hdsp->control_register & HDSP_SPDIFNonAudio)
33811da177e4SLinus Torvalds 		snd_iprintf(buffer, "IEC958 NonAudio: on\n");
3382b0b98119STakashi Iwai 	else
33831da177e4SLinus Torvalds 		snd_iprintf(buffer, "IEC958 NonAudio: off\n");
3384b0b98119STakashi Iwai 	if ((x = hdsp_spdif_sample_rate (hdsp)) != 0)
33851da177e4SLinus Torvalds 		snd_iprintf (buffer, "IEC958 sample rate: %d\n", x);
3386b0b98119STakashi Iwai 	else
33871da177e4SLinus Torvalds 		snd_iprintf (buffer, "IEC958 sample rate: Error flag set\n");
33881da177e4SLinus Torvalds 
33891da177e4SLinus Torvalds 	snd_iprintf(buffer, "\n");
33901da177e4SLinus Torvalds 
33911da177e4SLinus Torvalds 	/* Sync Check */
33921da177e4SLinus Torvalds 	x = status & HDSP_Sync0;
3393b0b98119STakashi Iwai 	if (status & HDSP_Lock0)
33941da177e4SLinus Torvalds 		snd_iprintf(buffer, "ADAT1: %s\n", x ? "Sync" : "Lock");
3395b0b98119STakashi Iwai 	else
33961da177e4SLinus Torvalds 		snd_iprintf(buffer, "ADAT1: No Lock\n");
33971da177e4SLinus Torvalds 
33981da177e4SLinus Torvalds 	switch (hdsp->io_type) {
33991da177e4SLinus Torvalds 	case Digiface:
34001da177e4SLinus Torvalds 	case H9652:
34011da177e4SLinus Torvalds 		x = status & HDSP_Sync1;
3402b0b98119STakashi Iwai 		if (status & HDSP_Lock1)
34031da177e4SLinus Torvalds 			snd_iprintf(buffer, "ADAT2: %s\n", x ? "Sync" : "Lock");
3404b0b98119STakashi Iwai 		else
34051da177e4SLinus Torvalds 			snd_iprintf(buffer, "ADAT2: No Lock\n");
34061da177e4SLinus Torvalds 		x = status & HDSP_Sync2;
3407b0b98119STakashi Iwai 		if (status & HDSP_Lock2)
34081da177e4SLinus Torvalds 			snd_iprintf(buffer, "ADAT3: %s\n", x ? "Sync" : "Lock");
3409b0b98119STakashi Iwai 		else
34101da177e4SLinus Torvalds 			snd_iprintf(buffer, "ADAT3: No Lock\n");
3411b0b98119STakashi Iwai 		break;
34121da177e4SLinus Torvalds 	default:
34131da177e4SLinus Torvalds 		/* relax */
34141da177e4SLinus Torvalds 		break;
34151da177e4SLinus Torvalds 	}
34161da177e4SLinus Torvalds 
34171da177e4SLinus Torvalds 	x = status & HDSP_SPDIFSync;
3418b0b98119STakashi Iwai 	if (status & HDSP_SPDIFErrorFlag)
34191da177e4SLinus Torvalds 		snd_iprintf (buffer, "SPDIF: No Lock\n");
3420b0b98119STakashi Iwai 	else
34211da177e4SLinus Torvalds 		snd_iprintf (buffer, "SPDIF: %s\n", x ? "Sync" : "Lock");
34221da177e4SLinus Torvalds 
34231da177e4SLinus Torvalds 	x = status2 & HDSP_wc_sync;
3424b0b98119STakashi Iwai 	if (status2 & HDSP_wc_lock)
34251da177e4SLinus Torvalds 		snd_iprintf (buffer, "Word Clock: %s\n", x ? "Sync" : "Lock");
3426b0b98119STakashi Iwai 	else
34271da177e4SLinus Torvalds 		snd_iprintf (buffer, "Word Clock: No Lock\n");
34281da177e4SLinus Torvalds 
34291da177e4SLinus Torvalds 	x = status & HDSP_TimecodeSync;
3430b0b98119STakashi Iwai 	if (status & HDSP_TimecodeLock)
34311da177e4SLinus Torvalds 		snd_iprintf(buffer, "ADAT Sync: %s\n", x ? "Sync" : "Lock");
3432b0b98119STakashi Iwai 	else
34331da177e4SLinus Torvalds 		snd_iprintf(buffer, "ADAT Sync: No Lock\n");
34341da177e4SLinus Torvalds 
34351da177e4SLinus Torvalds 	snd_iprintf(buffer, "\n");
34361da177e4SLinus Torvalds 
34371da177e4SLinus Torvalds 	/* Informations about H9632 specific controls */
34381da177e4SLinus Torvalds 	if (hdsp->io_type == H9632) {
34391da177e4SLinus Torvalds 		char *tmp;
34401da177e4SLinus Torvalds 
34411da177e4SLinus Torvalds 		switch (hdsp_ad_gain(hdsp)) {
34421da177e4SLinus Torvalds 		case 0:
34431da177e4SLinus Torvalds 			tmp = "-10 dBV";
34441da177e4SLinus Torvalds 			break;
34451da177e4SLinus Torvalds 		case 1:
34461da177e4SLinus Torvalds 			tmp = "+4 dBu";
34471da177e4SLinus Torvalds 			break;
34481da177e4SLinus Torvalds 		default:
34491da177e4SLinus Torvalds 			tmp = "Lo Gain";
34501da177e4SLinus Torvalds 			break;
34511da177e4SLinus Torvalds 		}
34521da177e4SLinus Torvalds 		snd_iprintf(buffer, "AD Gain : %s\n", tmp);
34531da177e4SLinus Torvalds 
34541da177e4SLinus Torvalds 		switch (hdsp_da_gain(hdsp)) {
34551da177e4SLinus Torvalds 		case 0:
34561da177e4SLinus Torvalds 			tmp = "Hi Gain";
34571da177e4SLinus Torvalds 			break;
34581da177e4SLinus Torvalds 		case 1:
34591da177e4SLinus Torvalds 			tmp = "+4 dBu";
34601da177e4SLinus Torvalds 			break;
34611da177e4SLinus Torvalds 		default:
34621da177e4SLinus Torvalds 			tmp = "-10 dBV";
34631da177e4SLinus Torvalds 			break;
34641da177e4SLinus Torvalds 		}
34651da177e4SLinus Torvalds 		snd_iprintf(buffer, "DA Gain : %s\n", tmp);
34661da177e4SLinus Torvalds 
34671da177e4SLinus Torvalds 		switch (hdsp_phone_gain(hdsp)) {
34681da177e4SLinus Torvalds 		case 0:
34691da177e4SLinus Torvalds 			tmp = "0 dB";
34701da177e4SLinus Torvalds 			break;
34711da177e4SLinus Torvalds 		case 1:
34721da177e4SLinus Torvalds 			tmp = "-6 dB";
34731da177e4SLinus Torvalds 			break;
34741da177e4SLinus Torvalds 		default:
34751da177e4SLinus Torvalds 			tmp = "-12 dB";
34761da177e4SLinus Torvalds 			break;
34771da177e4SLinus Torvalds 		}
34781da177e4SLinus Torvalds 		snd_iprintf(buffer, "Phones Gain : %s\n", tmp);
34791da177e4SLinus Torvalds 
34801da177e4SLinus Torvalds 		snd_iprintf(buffer, "XLR Breakout Cable : %s\n", hdsp_xlr_breakout_cable(hdsp) ? "yes" : "no");
34811da177e4SLinus Torvalds 
3482b0b98119STakashi Iwai 		if (hdsp->control_register & HDSP_AnalogExtensionBoard)
34831da177e4SLinus Torvalds 			snd_iprintf(buffer, "AEB : on (ADAT1 internal)\n");
3484b0b98119STakashi Iwai 		else
34851da177e4SLinus Torvalds 			snd_iprintf(buffer, "AEB : off (ADAT1 external)\n");
34861da177e4SLinus Torvalds 		snd_iprintf(buffer, "\n");
34871da177e4SLinus Torvalds 	}
34881da177e4SLinus Torvalds 
34891da177e4SLinus Torvalds }
34901da177e4SLinus Torvalds 
349155e957d8STakashi Iwai static void __devinit snd_hdsp_proc_init(struct hdsp *hdsp)
34921da177e4SLinus Torvalds {
349355e957d8STakashi Iwai 	struct snd_info_entry *entry;
34941da177e4SLinus Torvalds 
34951da177e4SLinus Torvalds 	if (! snd_card_proc_new(hdsp->card, "hdsp", &entry))
3496bf850204STakashi Iwai 		snd_info_set_text_ops(entry, hdsp, snd_hdsp_proc_read);
34971da177e4SLinus Torvalds }
34981da177e4SLinus Torvalds 
349955e957d8STakashi Iwai static void snd_hdsp_free_buffers(struct hdsp *hdsp)
35001da177e4SLinus Torvalds {
35011da177e4SLinus Torvalds 	snd_hammerfall_free_buffer(&hdsp->capture_dma_buf, hdsp->pci);
35021da177e4SLinus Torvalds 	snd_hammerfall_free_buffer(&hdsp->playback_dma_buf, hdsp->pci);
35031da177e4SLinus Torvalds }
35041da177e4SLinus Torvalds 
350555e957d8STakashi Iwai static int __devinit snd_hdsp_initialize_memory(struct hdsp *hdsp)
35061da177e4SLinus Torvalds {
35071da177e4SLinus Torvalds 	unsigned long pb_bus, cb_bus;
35081da177e4SLinus Torvalds 
35091da177e4SLinus Torvalds 	if (snd_hammerfall_get_buffer(hdsp->pci, &hdsp->capture_dma_buf, HDSP_DMA_AREA_BYTES) < 0 ||
35101da177e4SLinus Torvalds 	    snd_hammerfall_get_buffer(hdsp->pci, &hdsp->playback_dma_buf, HDSP_DMA_AREA_BYTES) < 0) {
35111da177e4SLinus Torvalds 		if (hdsp->capture_dma_buf.area)
35121da177e4SLinus Torvalds 			snd_dma_free_pages(&hdsp->capture_dma_buf);
35131da177e4SLinus Torvalds 		printk(KERN_ERR "%s: no buffers available\n", hdsp->card_name);
35141da177e4SLinus Torvalds 		return -ENOMEM;
35151da177e4SLinus Torvalds 	}
35161da177e4SLinus Torvalds 
35171da177e4SLinus Torvalds 	/* Align to bus-space 64K boundary */
35181da177e4SLinus Torvalds 
35197ab39926SClemens Ladisch 	cb_bus = ALIGN(hdsp->capture_dma_buf.addr, 0x10000ul);
35207ab39926SClemens Ladisch 	pb_bus = ALIGN(hdsp->playback_dma_buf.addr, 0x10000ul);
35211da177e4SLinus Torvalds 
35221da177e4SLinus Torvalds 	/* Tell the card where it is */
35231da177e4SLinus Torvalds 
35241da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_inputBufferAddress, cb_bus);
35251da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_outputBufferAddress, pb_bus);
35261da177e4SLinus Torvalds 
35271da177e4SLinus Torvalds 	hdsp->capture_buffer = hdsp->capture_dma_buf.area + (cb_bus - hdsp->capture_dma_buf.addr);
35281da177e4SLinus Torvalds 	hdsp->playback_buffer = hdsp->playback_dma_buf.area + (pb_bus - hdsp->playback_dma_buf.addr);
35291da177e4SLinus Torvalds 
35301da177e4SLinus Torvalds 	return 0;
35311da177e4SLinus Torvalds }
35321da177e4SLinus Torvalds 
353355e957d8STakashi Iwai static int snd_hdsp_set_defaults(struct hdsp *hdsp)
35341da177e4SLinus Torvalds {
35351da177e4SLinus Torvalds 	unsigned int i;
35361da177e4SLinus Torvalds 
35371da177e4SLinus Torvalds 	/* ASSUMPTION: hdsp->lock is either held, or
35381da177e4SLinus Torvalds 	   there is no need to hold it (e.g. during module
35391da177e4SLinus Torvalds 	   initalization).
35401da177e4SLinus Torvalds 	 */
35411da177e4SLinus Torvalds 
35421da177e4SLinus Torvalds 	/* set defaults:
35431da177e4SLinus Torvalds 
35441da177e4SLinus Torvalds 	   SPDIF Input via Coax
35451da177e4SLinus Torvalds 	   Master clock mode
35461da177e4SLinus Torvalds 	   maximum latency (7 => 2^7 = 8192 samples, 64Kbyte buffer,
35471da177e4SLinus Torvalds 	                    which implies 2 4096 sample, 32Kbyte periods).
35481da177e4SLinus Torvalds            Enable line out.
35491da177e4SLinus Torvalds 	 */
35501da177e4SLinus Torvalds 
35511da177e4SLinus Torvalds 	hdsp->control_register = HDSP_ClockModeMaster |
35521da177e4SLinus Torvalds 		                 HDSP_SPDIFInputCoaxial |
35531da177e4SLinus Torvalds 		                 hdsp_encode_latency(7) |
35541da177e4SLinus Torvalds 		                 HDSP_LineOut;
35551da177e4SLinus Torvalds 
35561da177e4SLinus Torvalds 
35571da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
35581da177e4SLinus Torvalds 
35591da177e4SLinus Torvalds #ifdef SNDRV_BIG_ENDIAN
35601da177e4SLinus Torvalds 	hdsp->control2_register = HDSP_BIGENDIAN_MODE;
35611da177e4SLinus Torvalds #else
35621da177e4SLinus Torvalds 	hdsp->control2_register = 0;
35631da177e4SLinus Torvalds #endif
3564b0b98119STakashi Iwai 	if (hdsp->io_type == H9652)
35651da177e4SLinus Torvalds 	        snd_hdsp_9652_enable_mixer (hdsp);
3566b0b98119STakashi Iwai 	else
35671da177e4SLinus Torvalds 		hdsp_write (hdsp, HDSP_control2Reg, hdsp->control2_register);
35681da177e4SLinus Torvalds 
35691da177e4SLinus Torvalds 	hdsp_reset_hw_pointer(hdsp);
35701da177e4SLinus Torvalds 	hdsp_compute_period_size(hdsp);
35711da177e4SLinus Torvalds 
35721da177e4SLinus Torvalds 	/* silence everything */
35731da177e4SLinus Torvalds 
3574b0b98119STakashi Iwai 	for (i = 0; i < HDSP_MATRIX_MIXER_SIZE; ++i)
35751da177e4SLinus Torvalds 		hdsp->mixer_matrix[i] = MINUS_INFINITY_GAIN;
35761da177e4SLinus Torvalds 
35771da177e4SLinus Torvalds 	for (i = 0; i < ((hdsp->io_type == H9652 || hdsp->io_type == H9632) ? 1352 : HDSP_MATRIX_MIXER_SIZE); ++i) {
3578b0b98119STakashi Iwai 		if (hdsp_write_gain (hdsp, i, MINUS_INFINITY_GAIN))
35791da177e4SLinus Torvalds 			return -EIO;
35801da177e4SLinus Torvalds 	}
35811da177e4SLinus Torvalds 
35821da177e4SLinus Torvalds 	/* H9632 specific defaults */
35831da177e4SLinus Torvalds 	if (hdsp->io_type == H9632) {
35841da177e4SLinus Torvalds 		hdsp->control_register |= (HDSP_DAGainPlus4dBu | HDSP_ADGainPlus4dBu | HDSP_PhoneGain0dB);
35851da177e4SLinus Torvalds 		hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
35861da177e4SLinus Torvalds 	}
35871da177e4SLinus Torvalds 
35881da177e4SLinus Torvalds 	/* set a default rate so that the channel map is set up.
35891da177e4SLinus Torvalds 	 */
35901da177e4SLinus Torvalds 
35911da177e4SLinus Torvalds 	hdsp_set_rate(hdsp, 48000, 1);
35921da177e4SLinus Torvalds 
35931da177e4SLinus Torvalds 	return 0;
35941da177e4SLinus Torvalds }
35951da177e4SLinus Torvalds 
35961da177e4SLinus Torvalds static void hdsp_midi_tasklet(unsigned long arg)
35971da177e4SLinus Torvalds {
359855e957d8STakashi Iwai 	struct hdsp *hdsp = (struct hdsp *)arg;
35991da177e4SLinus Torvalds 
3600b0b98119STakashi Iwai 	if (hdsp->midi[0].pending)
36011da177e4SLinus Torvalds 		snd_hdsp_midi_input_read (&hdsp->midi[0]);
3602b0b98119STakashi Iwai 	if (hdsp->midi[1].pending)
36031da177e4SLinus Torvalds 		snd_hdsp_midi_input_read (&hdsp->midi[1]);
36041da177e4SLinus Torvalds }
36051da177e4SLinus Torvalds 
36067d12e780SDavid Howells static irqreturn_t snd_hdsp_interrupt(int irq, void *dev_id)
36071da177e4SLinus Torvalds {
360855e957d8STakashi Iwai 	struct hdsp *hdsp = (struct hdsp *) dev_id;
36091da177e4SLinus Torvalds 	unsigned int status;
36101da177e4SLinus Torvalds 	int audio;
36111da177e4SLinus Torvalds 	int midi0;
36121da177e4SLinus Torvalds 	int midi1;
36131da177e4SLinus Torvalds 	unsigned int midi0status;
36141da177e4SLinus Torvalds 	unsigned int midi1status;
36151da177e4SLinus Torvalds 	int schedule = 0;
36161da177e4SLinus Torvalds 
36171da177e4SLinus Torvalds 	status = hdsp_read(hdsp, HDSP_statusRegister);
36181da177e4SLinus Torvalds 
36191da177e4SLinus Torvalds 	audio = status & HDSP_audioIRQPending;
36201da177e4SLinus Torvalds 	midi0 = status & HDSP_midi0IRQPending;
36211da177e4SLinus Torvalds 	midi1 = status & HDSP_midi1IRQPending;
36221da177e4SLinus Torvalds 
3623b0b98119STakashi Iwai 	if (!audio && !midi0 && !midi1)
36241da177e4SLinus Torvalds 		return IRQ_NONE;
36251da177e4SLinus Torvalds 
36261da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_interruptConfirmation, 0);
36271da177e4SLinus Torvalds 
36281da177e4SLinus Torvalds 	midi0status = hdsp_read (hdsp, HDSP_midiStatusIn0) & 0xff;
36291da177e4SLinus Torvalds 	midi1status = hdsp_read (hdsp, HDSP_midiStatusIn1) & 0xff;
36301da177e4SLinus Torvalds 
36311da177e4SLinus Torvalds 	if (audio) {
3632b0b98119STakashi Iwai 		if (hdsp->capture_substream)
36331da177e4SLinus Torvalds 			snd_pcm_period_elapsed(hdsp->pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream);
36341da177e4SLinus Torvalds 
3635b0b98119STakashi Iwai 		if (hdsp->playback_substream)
36361da177e4SLinus Torvalds 			snd_pcm_period_elapsed(hdsp->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream);
36371da177e4SLinus Torvalds 	}
36381da177e4SLinus Torvalds 
36391da177e4SLinus Torvalds 	if (midi0 && midi0status) {
36401da177e4SLinus Torvalds 		if (hdsp->use_midi_tasklet) {
36411da177e4SLinus Torvalds 			/* we disable interrupts for this input until processing is done */
36421da177e4SLinus Torvalds 			hdsp->control_register &= ~HDSP_Midi0InterruptEnable;
36431da177e4SLinus Torvalds 			hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
36441da177e4SLinus Torvalds 			hdsp->midi[0].pending = 1;
36451da177e4SLinus Torvalds 			schedule = 1;
36461da177e4SLinus Torvalds 		} else {
36471da177e4SLinus Torvalds 			snd_hdsp_midi_input_read (&hdsp->midi[0]);
36481da177e4SLinus Torvalds 		}
36491da177e4SLinus Torvalds 	}
36501da177e4SLinus Torvalds 	if (hdsp->io_type != Multiface && hdsp->io_type != H9632 && midi1 && midi1status) {
36511da177e4SLinus Torvalds 		if (hdsp->use_midi_tasklet) {
36521da177e4SLinus Torvalds 			/* we disable interrupts for this input until processing is done */
36531da177e4SLinus Torvalds 			hdsp->control_register &= ~HDSP_Midi1InterruptEnable;
36541da177e4SLinus Torvalds 			hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
36551da177e4SLinus Torvalds 			hdsp->midi[1].pending = 1;
36561da177e4SLinus Torvalds 			schedule = 1;
36571da177e4SLinus Torvalds 		} else {
36581da177e4SLinus Torvalds 			snd_hdsp_midi_input_read (&hdsp->midi[1]);
36591da177e4SLinus Torvalds 		}
36601da177e4SLinus Torvalds 	}
36611da177e4SLinus Torvalds 	if (hdsp->use_midi_tasklet && schedule)
36621da177e4SLinus Torvalds 		tasklet_hi_schedule(&hdsp->midi_tasklet);
36631da177e4SLinus Torvalds 	return IRQ_HANDLED;
36641da177e4SLinus Torvalds }
36651da177e4SLinus Torvalds 
366655e957d8STakashi Iwai static snd_pcm_uframes_t snd_hdsp_hw_pointer(struct snd_pcm_substream *substream)
36671da177e4SLinus Torvalds {
366855e957d8STakashi Iwai 	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
36691da177e4SLinus Torvalds 	return hdsp_hw_pointer(hdsp);
36701da177e4SLinus Torvalds }
36711da177e4SLinus Torvalds 
367255e957d8STakashi Iwai static char *hdsp_channel_buffer_location(struct hdsp *hdsp,
36731da177e4SLinus Torvalds 					     int stream,
36741da177e4SLinus Torvalds 					     int channel)
36751da177e4SLinus Torvalds 
36761da177e4SLinus Torvalds {
36771da177e4SLinus Torvalds 	int mapped_channel;
36781da177e4SLinus Torvalds 
36791da177e4SLinus Torvalds         snd_assert(channel >= 0 && channel < hdsp->max_channels, return NULL);
36801da177e4SLinus Torvalds 
3681b0b98119STakashi Iwai 	if ((mapped_channel = hdsp->channel_map[channel]) < 0)
36821da177e4SLinus Torvalds 		return NULL;
36831da177e4SLinus Torvalds 
3684b0b98119STakashi Iwai 	if (stream == SNDRV_PCM_STREAM_CAPTURE)
36851da177e4SLinus Torvalds 		return hdsp->capture_buffer + (mapped_channel * HDSP_CHANNEL_BUFFER_BYTES);
3686b0b98119STakashi Iwai 	else
36871da177e4SLinus Torvalds 		return hdsp->playback_buffer + (mapped_channel * HDSP_CHANNEL_BUFFER_BYTES);
36881da177e4SLinus Torvalds }
36891da177e4SLinus Torvalds 
369055e957d8STakashi Iwai static int snd_hdsp_playback_copy(struct snd_pcm_substream *substream, int channel,
36911da177e4SLinus Torvalds 				  snd_pcm_uframes_t pos, void __user *src, snd_pcm_uframes_t count)
36921da177e4SLinus Torvalds {
369355e957d8STakashi Iwai 	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
36941da177e4SLinus Torvalds 	char *channel_buf;
36951da177e4SLinus Torvalds 
36961da177e4SLinus Torvalds 	snd_assert(pos + count <= HDSP_CHANNEL_BUFFER_BYTES / 4, return -EINVAL);
36971da177e4SLinus Torvalds 
36981da177e4SLinus Torvalds 	channel_buf = hdsp_channel_buffer_location (hdsp, substream->pstr->stream, channel);
36991da177e4SLinus Torvalds 	snd_assert(channel_buf != NULL, return -EIO);
37001da177e4SLinus Torvalds 	if (copy_from_user(channel_buf + pos * 4, src, count * 4))
37011da177e4SLinus Torvalds 		return -EFAULT;
37021da177e4SLinus Torvalds 	return count;
37031da177e4SLinus Torvalds }
37041da177e4SLinus Torvalds 
370555e957d8STakashi Iwai static int snd_hdsp_capture_copy(struct snd_pcm_substream *substream, int channel,
37061da177e4SLinus Torvalds 				 snd_pcm_uframes_t pos, void __user *dst, snd_pcm_uframes_t count)
37071da177e4SLinus Torvalds {
370855e957d8STakashi Iwai 	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
37091da177e4SLinus Torvalds 	char *channel_buf;
37101da177e4SLinus Torvalds 
37111da177e4SLinus Torvalds 	snd_assert(pos + count <= HDSP_CHANNEL_BUFFER_BYTES / 4, return -EINVAL);
37121da177e4SLinus Torvalds 
37131da177e4SLinus Torvalds 	channel_buf = hdsp_channel_buffer_location (hdsp, substream->pstr->stream, channel);
37141da177e4SLinus Torvalds 	snd_assert(channel_buf != NULL, return -EIO);
37151da177e4SLinus Torvalds 	if (copy_to_user(dst, channel_buf + pos * 4, count * 4))
37161da177e4SLinus Torvalds 		return -EFAULT;
37171da177e4SLinus Torvalds 	return count;
37181da177e4SLinus Torvalds }
37191da177e4SLinus Torvalds 
372055e957d8STakashi Iwai static int snd_hdsp_hw_silence(struct snd_pcm_substream *substream, int channel,
37211da177e4SLinus Torvalds 				  snd_pcm_uframes_t pos, snd_pcm_uframes_t count)
37221da177e4SLinus Torvalds {
372355e957d8STakashi Iwai 	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
37241da177e4SLinus Torvalds 	char *channel_buf;
37251da177e4SLinus Torvalds 
37261da177e4SLinus Torvalds 	channel_buf = hdsp_channel_buffer_location (hdsp, substream->pstr->stream, channel);
37271da177e4SLinus Torvalds 	snd_assert(channel_buf != NULL, return -EIO);
37281da177e4SLinus Torvalds 	memset(channel_buf + pos * 4, 0, count * 4);
37291da177e4SLinus Torvalds 	return count;
37301da177e4SLinus Torvalds }
37311da177e4SLinus Torvalds 
373255e957d8STakashi Iwai static int snd_hdsp_reset(struct snd_pcm_substream *substream)
37331da177e4SLinus Torvalds {
373455e957d8STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
373555e957d8STakashi Iwai 	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
373655e957d8STakashi Iwai 	struct snd_pcm_substream *other;
37371da177e4SLinus Torvalds 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
37381da177e4SLinus Torvalds 		other = hdsp->capture_substream;
37391da177e4SLinus Torvalds 	else
37401da177e4SLinus Torvalds 		other = hdsp->playback_substream;
37411da177e4SLinus Torvalds 	if (hdsp->running)
37421da177e4SLinus Torvalds 		runtime->status->hw_ptr = hdsp_hw_pointer(hdsp);
37431da177e4SLinus Torvalds 	else
37441da177e4SLinus Torvalds 		runtime->status->hw_ptr = 0;
37451da177e4SLinus Torvalds 	if (other) {
37461da177e4SLinus Torvalds 		struct list_head *pos;
374755e957d8STakashi Iwai 		struct snd_pcm_substream *s;
374855e957d8STakashi Iwai 		struct snd_pcm_runtime *oruntime = other->runtime;
37491da177e4SLinus Torvalds 		snd_pcm_group_for_each(pos, substream) {
37501da177e4SLinus Torvalds 			s = snd_pcm_group_substream_entry(pos);
37511da177e4SLinus Torvalds 			if (s == other) {
37521da177e4SLinus Torvalds 				oruntime->status->hw_ptr = runtime->status->hw_ptr;
37531da177e4SLinus Torvalds 				break;
37541da177e4SLinus Torvalds 			}
37551da177e4SLinus Torvalds 		}
37561da177e4SLinus Torvalds 	}
37571da177e4SLinus Torvalds 	return 0;
37581da177e4SLinus Torvalds }
37591da177e4SLinus Torvalds 
376055e957d8STakashi Iwai static int snd_hdsp_hw_params(struct snd_pcm_substream *substream,
376155e957d8STakashi Iwai 				 struct snd_pcm_hw_params *params)
37621da177e4SLinus Torvalds {
376355e957d8STakashi Iwai 	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
37641da177e4SLinus Torvalds 	int err;
37651da177e4SLinus Torvalds 	pid_t this_pid;
37661da177e4SLinus Torvalds 	pid_t other_pid;
37671da177e4SLinus Torvalds 
3768b0b98119STakashi Iwai 	if (hdsp_check_for_iobox (hdsp))
37691da177e4SLinus Torvalds 		return -EIO;
37701da177e4SLinus Torvalds 
3771b0b98119STakashi Iwai 	if (hdsp_check_for_firmware(hdsp, 1))
37721da177e4SLinus Torvalds 		return -EIO;
37731da177e4SLinus Torvalds 
37741da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
37751da177e4SLinus Torvalds 
37761da177e4SLinus Torvalds 	if (substream->pstr->stream == SNDRV_PCM_STREAM_PLAYBACK) {
37771da177e4SLinus Torvalds 		hdsp->control_register &= ~(HDSP_SPDIFProfessional | HDSP_SPDIFNonAudio | HDSP_SPDIFEmphasis);
37781da177e4SLinus Torvalds 		hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register |= hdsp->creg_spdif_stream);
37791da177e4SLinus Torvalds 		this_pid = hdsp->playback_pid;
37801da177e4SLinus Torvalds 		other_pid = hdsp->capture_pid;
37811da177e4SLinus Torvalds 	} else {
37821da177e4SLinus Torvalds 		this_pid = hdsp->capture_pid;
37831da177e4SLinus Torvalds 		other_pid = hdsp->playback_pid;
37841da177e4SLinus Torvalds 	}
37851da177e4SLinus Torvalds 
37861da177e4SLinus Torvalds 	if ((other_pid > 0) && (this_pid != other_pid)) {
37871da177e4SLinus Torvalds 
37881da177e4SLinus Torvalds 		/* The other stream is open, and not by the same
37891da177e4SLinus Torvalds 		   task as this one. Make sure that the parameters
37901da177e4SLinus Torvalds 		   that matter are the same.
37911da177e4SLinus Torvalds 		 */
37921da177e4SLinus Torvalds 
37931da177e4SLinus Torvalds 		if (params_rate(params) != hdsp->system_sample_rate) {
37941da177e4SLinus Torvalds 			spin_unlock_irq(&hdsp->lock);
37951da177e4SLinus Torvalds 			_snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_RATE);
37961da177e4SLinus Torvalds 			return -EBUSY;
37971da177e4SLinus Torvalds 		}
37981da177e4SLinus Torvalds 
37991da177e4SLinus Torvalds 		if (params_period_size(params) != hdsp->period_bytes / 4) {
38001da177e4SLinus Torvalds 			spin_unlock_irq(&hdsp->lock);
38011da177e4SLinus Torvalds 			_snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
38021da177e4SLinus Torvalds 			return -EBUSY;
38031da177e4SLinus Torvalds 		}
38041da177e4SLinus Torvalds 
38051da177e4SLinus Torvalds 		/* We're fine. */
38061da177e4SLinus Torvalds 
38071da177e4SLinus Torvalds 		spin_unlock_irq(&hdsp->lock);
38081da177e4SLinus Torvalds  		return 0;
38091da177e4SLinus Torvalds 
38101da177e4SLinus Torvalds 	} else {
38111da177e4SLinus Torvalds 		spin_unlock_irq(&hdsp->lock);
38121da177e4SLinus Torvalds 	}
38131da177e4SLinus Torvalds 
38141da177e4SLinus Torvalds 	/* how to make sure that the rate matches an externally-set one ?
38151da177e4SLinus Torvalds 	 */
38161da177e4SLinus Torvalds 
38171da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
3818e3ea4d89STakashi Iwai 	if (! hdsp->clock_source_locked) {
38191da177e4SLinus Torvalds 		if ((err = hdsp_set_rate(hdsp, params_rate(params), 0)) < 0) {
38201da177e4SLinus Torvalds 			spin_unlock_irq(&hdsp->lock);
38211da177e4SLinus Torvalds 			_snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_RATE);
38221da177e4SLinus Torvalds 			return err;
38231da177e4SLinus Torvalds 		}
3824e3ea4d89STakashi Iwai 	}
3825e3ea4d89STakashi Iwai 	spin_unlock_irq(&hdsp->lock);
38261da177e4SLinus Torvalds 
38271da177e4SLinus Torvalds 	if ((err = hdsp_set_interrupt_interval(hdsp, params_period_size(params))) < 0) {
38281da177e4SLinus Torvalds 		_snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
38291da177e4SLinus Torvalds 		return err;
38301da177e4SLinus Torvalds 	}
38311da177e4SLinus Torvalds 
38321da177e4SLinus Torvalds 	return 0;
38331da177e4SLinus Torvalds }
38341da177e4SLinus Torvalds 
383555e957d8STakashi Iwai static int snd_hdsp_channel_info(struct snd_pcm_substream *substream,
383655e957d8STakashi Iwai 				    struct snd_pcm_channel_info *info)
38371da177e4SLinus Torvalds {
383855e957d8STakashi Iwai 	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
38391da177e4SLinus Torvalds 	int mapped_channel;
38401da177e4SLinus Torvalds 
38411da177e4SLinus Torvalds 	snd_assert(info->channel < hdsp->max_channels, return -EINVAL);
38421da177e4SLinus Torvalds 
3843b0b98119STakashi Iwai 	if ((mapped_channel = hdsp->channel_map[info->channel]) < 0)
38441da177e4SLinus Torvalds 		return -EINVAL;
38451da177e4SLinus Torvalds 
38461da177e4SLinus Torvalds 	info->offset = mapped_channel * HDSP_CHANNEL_BUFFER_BYTES;
38471da177e4SLinus Torvalds 	info->first = 0;
38481da177e4SLinus Torvalds 	info->step = 32;
38491da177e4SLinus Torvalds 	return 0;
38501da177e4SLinus Torvalds }
38511da177e4SLinus Torvalds 
385255e957d8STakashi Iwai static int snd_hdsp_ioctl(struct snd_pcm_substream *substream,
38531da177e4SLinus Torvalds 			     unsigned int cmd, void *arg)
38541da177e4SLinus Torvalds {
38551da177e4SLinus Torvalds 	switch (cmd) {
38561da177e4SLinus Torvalds 	case SNDRV_PCM_IOCTL1_RESET:
38571da177e4SLinus Torvalds 		return snd_hdsp_reset(substream);
38581da177e4SLinus Torvalds 	case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
3859b0b98119STakashi Iwai 		return snd_hdsp_channel_info(substream, arg);
38601da177e4SLinus Torvalds 	default:
38611da177e4SLinus Torvalds 		break;
38621da177e4SLinus Torvalds 	}
38631da177e4SLinus Torvalds 
38641da177e4SLinus Torvalds 	return snd_pcm_lib_ioctl(substream, cmd, arg);
38651da177e4SLinus Torvalds }
38661da177e4SLinus Torvalds 
386755e957d8STakashi Iwai static int snd_hdsp_trigger(struct snd_pcm_substream *substream, int cmd)
38681da177e4SLinus Torvalds {
386955e957d8STakashi Iwai 	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
387055e957d8STakashi Iwai 	struct snd_pcm_substream *other;
38711da177e4SLinus Torvalds 	int running;
38721da177e4SLinus Torvalds 
3873b0b98119STakashi Iwai 	if (hdsp_check_for_iobox (hdsp))
38741da177e4SLinus Torvalds 		return -EIO;
38751da177e4SLinus Torvalds 
3876311e70a4STakashi Iwai 	if (hdsp_check_for_firmware(hdsp, 0)) /* no auto-loading in trigger */
38771da177e4SLinus Torvalds 		return -EIO;
38781da177e4SLinus Torvalds 
38791da177e4SLinus Torvalds 	spin_lock(&hdsp->lock);
38801da177e4SLinus Torvalds 	running = hdsp->running;
38811da177e4SLinus Torvalds 	switch (cmd) {
38821da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_START:
38831da177e4SLinus Torvalds 		running |= 1 << substream->stream;
38841da177e4SLinus Torvalds 		break;
38851da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_STOP:
38861da177e4SLinus Torvalds 		running &= ~(1 << substream->stream);
38871da177e4SLinus Torvalds 		break;
38881da177e4SLinus Torvalds 	default:
38891da177e4SLinus Torvalds 		snd_BUG();
38901da177e4SLinus Torvalds 		spin_unlock(&hdsp->lock);
38911da177e4SLinus Torvalds 		return -EINVAL;
38921da177e4SLinus Torvalds 	}
38931da177e4SLinus Torvalds 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
38941da177e4SLinus Torvalds 		other = hdsp->capture_substream;
38951da177e4SLinus Torvalds 	else
38961da177e4SLinus Torvalds 		other = hdsp->playback_substream;
38971da177e4SLinus Torvalds 
38981da177e4SLinus Torvalds 	if (other) {
38991da177e4SLinus Torvalds 		struct list_head *pos;
390055e957d8STakashi Iwai 		struct snd_pcm_substream *s;
39011da177e4SLinus Torvalds 		snd_pcm_group_for_each(pos, substream) {
39021da177e4SLinus Torvalds 			s = snd_pcm_group_substream_entry(pos);
39031da177e4SLinus Torvalds 			if (s == other) {
39041da177e4SLinus Torvalds 				snd_pcm_trigger_done(s, substream);
39051da177e4SLinus Torvalds 				if (cmd == SNDRV_PCM_TRIGGER_START)
39061da177e4SLinus Torvalds 					running |= 1 << s->stream;
39071da177e4SLinus Torvalds 				else
39081da177e4SLinus Torvalds 					running &= ~(1 << s->stream);
39091da177e4SLinus Torvalds 				goto _ok;
39101da177e4SLinus Torvalds 			}
39111da177e4SLinus Torvalds 		}
39121da177e4SLinus Torvalds 		if (cmd == SNDRV_PCM_TRIGGER_START) {
39131da177e4SLinus Torvalds 			if (!(running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) &&
39141da177e4SLinus Torvalds 			    substream->stream == SNDRV_PCM_STREAM_CAPTURE)
39151da177e4SLinus Torvalds 				hdsp_silence_playback(hdsp);
39161da177e4SLinus Torvalds 		} else {
39171da177e4SLinus Torvalds 			if (running &&
39181da177e4SLinus Torvalds 			    substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
39191da177e4SLinus Torvalds 				hdsp_silence_playback(hdsp);
39201da177e4SLinus Torvalds 		}
39211da177e4SLinus Torvalds 	} else {
39221da177e4SLinus Torvalds 		if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
39231da177e4SLinus Torvalds 				hdsp_silence_playback(hdsp);
39241da177e4SLinus Torvalds 	}
39251da177e4SLinus Torvalds  _ok:
39261da177e4SLinus Torvalds 	snd_pcm_trigger_done(substream, substream);
39271da177e4SLinus Torvalds 	if (!hdsp->running && running)
39281da177e4SLinus Torvalds 		hdsp_start_audio(hdsp);
39291da177e4SLinus Torvalds 	else if (hdsp->running && !running)
39301da177e4SLinus Torvalds 		hdsp_stop_audio(hdsp);
39311da177e4SLinus Torvalds 	hdsp->running = running;
39321da177e4SLinus Torvalds 	spin_unlock(&hdsp->lock);
39331da177e4SLinus Torvalds 
39341da177e4SLinus Torvalds 	return 0;
39351da177e4SLinus Torvalds }
39361da177e4SLinus Torvalds 
393755e957d8STakashi Iwai static int snd_hdsp_prepare(struct snd_pcm_substream *substream)
39381da177e4SLinus Torvalds {
393955e957d8STakashi Iwai 	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
39401da177e4SLinus Torvalds 	int result = 0;
39411da177e4SLinus Torvalds 
3942b0b98119STakashi Iwai 	if (hdsp_check_for_iobox (hdsp))
39431da177e4SLinus Torvalds 		return -EIO;
39441da177e4SLinus Torvalds 
3945b0b98119STakashi Iwai 	if (hdsp_check_for_firmware(hdsp, 1))
39461da177e4SLinus Torvalds 		return -EIO;
39471da177e4SLinus Torvalds 
39481da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
39491da177e4SLinus Torvalds 	if (!hdsp->running)
39501da177e4SLinus Torvalds 		hdsp_reset_hw_pointer(hdsp);
39511da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
39521da177e4SLinus Torvalds 	return result;
39531da177e4SLinus Torvalds }
39541da177e4SLinus Torvalds 
395555e957d8STakashi Iwai static struct snd_pcm_hardware snd_hdsp_playback_subinfo =
39561da177e4SLinus Torvalds {
39571da177e4SLinus Torvalds 	.info =			(SNDRV_PCM_INFO_MMAP |
39581da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_MMAP_VALID |
39591da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_NONINTERLEAVED |
39601da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_SYNC_START |
39611da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_DOUBLE),
39621da177e4SLinus Torvalds #ifdef SNDRV_BIG_ENDIAN
39631da177e4SLinus Torvalds 	.formats =		SNDRV_PCM_FMTBIT_S32_BE,
39641da177e4SLinus Torvalds #else
39651da177e4SLinus Torvalds 	.formats =		SNDRV_PCM_FMTBIT_S32_LE,
39661da177e4SLinus Torvalds #endif
39671da177e4SLinus Torvalds 	.rates =		(SNDRV_PCM_RATE_32000 |
39681da177e4SLinus Torvalds 				 SNDRV_PCM_RATE_44100 |
39691da177e4SLinus Torvalds 				 SNDRV_PCM_RATE_48000 |
39701da177e4SLinus Torvalds 				 SNDRV_PCM_RATE_64000 |
39711da177e4SLinus Torvalds 				 SNDRV_PCM_RATE_88200 |
39721da177e4SLinus Torvalds 				 SNDRV_PCM_RATE_96000),
39731da177e4SLinus Torvalds 	.rate_min =		32000,
39741da177e4SLinus Torvalds 	.rate_max =		96000,
39751da177e4SLinus Torvalds 	.channels_min =		14,
39761da177e4SLinus Torvalds 	.channels_max =		HDSP_MAX_CHANNELS,
39771da177e4SLinus Torvalds 	.buffer_bytes_max =	HDSP_CHANNEL_BUFFER_BYTES * HDSP_MAX_CHANNELS,
39781da177e4SLinus Torvalds 	.period_bytes_min =	(64 * 4) * 10,
39791da177e4SLinus Torvalds 	.period_bytes_max =	(8192 * 4) * HDSP_MAX_CHANNELS,
39801da177e4SLinus Torvalds 	.periods_min =		2,
39811da177e4SLinus Torvalds 	.periods_max =		2,
39821da177e4SLinus Torvalds 	.fifo_size =		0
39831da177e4SLinus Torvalds };
39841da177e4SLinus Torvalds 
398555e957d8STakashi Iwai static struct snd_pcm_hardware snd_hdsp_capture_subinfo =
39861da177e4SLinus Torvalds {
39871da177e4SLinus Torvalds 	.info =			(SNDRV_PCM_INFO_MMAP |
39881da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_MMAP_VALID |
39891da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_NONINTERLEAVED |
39901da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_SYNC_START),
39911da177e4SLinus Torvalds #ifdef SNDRV_BIG_ENDIAN
39921da177e4SLinus Torvalds 	.formats =		SNDRV_PCM_FMTBIT_S32_BE,
39931da177e4SLinus Torvalds #else
39941da177e4SLinus Torvalds 	.formats =		SNDRV_PCM_FMTBIT_S32_LE,
39951da177e4SLinus Torvalds #endif
39961da177e4SLinus Torvalds 	.rates =		(SNDRV_PCM_RATE_32000 |
39971da177e4SLinus Torvalds 				 SNDRV_PCM_RATE_44100 |
39981da177e4SLinus Torvalds 				 SNDRV_PCM_RATE_48000 |
39991da177e4SLinus Torvalds 				 SNDRV_PCM_RATE_64000 |
40001da177e4SLinus Torvalds 				 SNDRV_PCM_RATE_88200 |
40011da177e4SLinus Torvalds 				 SNDRV_PCM_RATE_96000),
40021da177e4SLinus Torvalds 	.rate_min =		32000,
40031da177e4SLinus Torvalds 	.rate_max =		96000,
40041da177e4SLinus Torvalds 	.channels_min =		14,
40051da177e4SLinus Torvalds 	.channels_max =		HDSP_MAX_CHANNELS,
40061da177e4SLinus Torvalds 	.buffer_bytes_max =	HDSP_CHANNEL_BUFFER_BYTES * HDSP_MAX_CHANNELS,
40071da177e4SLinus Torvalds 	.period_bytes_min =	(64 * 4) * 10,
40081da177e4SLinus Torvalds 	.period_bytes_max =	(8192 * 4) * HDSP_MAX_CHANNELS,
40091da177e4SLinus Torvalds 	.periods_min =		2,
40101da177e4SLinus Torvalds 	.periods_max =		2,
40111da177e4SLinus Torvalds 	.fifo_size =		0
40121da177e4SLinus Torvalds };
40131da177e4SLinus Torvalds 
40141da177e4SLinus Torvalds static unsigned int hdsp_period_sizes[] = { 64, 128, 256, 512, 1024, 2048, 4096, 8192 };
40151da177e4SLinus Torvalds 
401655e957d8STakashi Iwai static struct snd_pcm_hw_constraint_list hdsp_hw_constraints_period_sizes = {
40171da177e4SLinus Torvalds 	.count = ARRAY_SIZE(hdsp_period_sizes),
40181da177e4SLinus Torvalds 	.list = hdsp_period_sizes,
40191da177e4SLinus Torvalds 	.mask = 0
40201da177e4SLinus Torvalds };
40211da177e4SLinus Torvalds 
40221da177e4SLinus Torvalds static unsigned int hdsp_9632_sample_rates[] = { 32000, 44100, 48000, 64000, 88200, 96000, 128000, 176400, 192000 };
40231da177e4SLinus Torvalds 
402455e957d8STakashi Iwai static struct snd_pcm_hw_constraint_list hdsp_hw_constraints_9632_sample_rates = {
40251da177e4SLinus Torvalds 	.count = ARRAY_SIZE(hdsp_9632_sample_rates),
40261da177e4SLinus Torvalds 	.list = hdsp_9632_sample_rates,
40271da177e4SLinus Torvalds 	.mask = 0
40281da177e4SLinus Torvalds };
40291da177e4SLinus Torvalds 
403055e957d8STakashi Iwai static int snd_hdsp_hw_rule_in_channels(struct snd_pcm_hw_params *params,
403155e957d8STakashi Iwai 					struct snd_pcm_hw_rule *rule)
40321da177e4SLinus Torvalds {
403355e957d8STakashi Iwai 	struct hdsp *hdsp = rule->private;
403455e957d8STakashi Iwai 	struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
40351da177e4SLinus Torvalds 	if (hdsp->io_type == H9632) {
40361da177e4SLinus Torvalds 		unsigned int list[3];
40371da177e4SLinus Torvalds 		list[0] = hdsp->qs_in_channels;
40381da177e4SLinus Torvalds 		list[1] = hdsp->ds_in_channels;
40391da177e4SLinus Torvalds 		list[2] = hdsp->ss_in_channels;
40401da177e4SLinus Torvalds 		return snd_interval_list(c, 3, list, 0);
40411da177e4SLinus Torvalds 	} else {
40421da177e4SLinus Torvalds 		unsigned int list[2];
40431da177e4SLinus Torvalds 		list[0] = hdsp->ds_in_channels;
40441da177e4SLinus Torvalds 		list[1] = hdsp->ss_in_channels;
40451da177e4SLinus Torvalds 		return snd_interval_list(c, 2, list, 0);
40461da177e4SLinus Torvalds 	}
40471da177e4SLinus Torvalds }
40481da177e4SLinus Torvalds 
404955e957d8STakashi Iwai static int snd_hdsp_hw_rule_out_channels(struct snd_pcm_hw_params *params,
405055e957d8STakashi Iwai 					struct snd_pcm_hw_rule *rule)
40511da177e4SLinus Torvalds {
40521da177e4SLinus Torvalds 	unsigned int list[3];
405355e957d8STakashi Iwai 	struct hdsp *hdsp = rule->private;
405455e957d8STakashi Iwai 	struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
40551da177e4SLinus Torvalds 	if (hdsp->io_type == H9632) {
40561da177e4SLinus Torvalds 		list[0] = hdsp->qs_out_channels;
40571da177e4SLinus Torvalds 		list[1] = hdsp->ds_out_channels;
40581da177e4SLinus Torvalds 		list[2] = hdsp->ss_out_channels;
40591da177e4SLinus Torvalds 		return snd_interval_list(c, 3, list, 0);
40601da177e4SLinus Torvalds 	} else {
40611da177e4SLinus Torvalds 		list[0] = hdsp->ds_out_channels;
40621da177e4SLinus Torvalds 		list[1] = hdsp->ss_out_channels;
40631da177e4SLinus Torvalds 	}
40641da177e4SLinus Torvalds 	return snd_interval_list(c, 2, list, 0);
40651da177e4SLinus Torvalds }
40661da177e4SLinus Torvalds 
406755e957d8STakashi Iwai static int snd_hdsp_hw_rule_in_channels_rate(struct snd_pcm_hw_params *params,
406855e957d8STakashi Iwai 					     struct snd_pcm_hw_rule *rule)
40691da177e4SLinus Torvalds {
407055e957d8STakashi Iwai 	struct hdsp *hdsp = rule->private;
407155e957d8STakashi Iwai 	struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
407255e957d8STakashi Iwai 	struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
40731da177e4SLinus Torvalds 	if (r->min > 96000 && hdsp->io_type == H9632) {
407455e957d8STakashi Iwai 		struct snd_interval t = {
40751da177e4SLinus Torvalds 			.min = hdsp->qs_in_channels,
40761da177e4SLinus Torvalds 			.max = hdsp->qs_in_channels,
40771da177e4SLinus Torvalds 			.integer = 1,
40781da177e4SLinus Torvalds 		};
40791da177e4SLinus Torvalds 		return snd_interval_refine(c, &t);
40801da177e4SLinus Torvalds 	} else if (r->min > 48000 && r->max <= 96000) {
408155e957d8STakashi Iwai 		struct snd_interval t = {
40821da177e4SLinus Torvalds 			.min = hdsp->ds_in_channels,
40831da177e4SLinus Torvalds 			.max = hdsp->ds_in_channels,
40841da177e4SLinus Torvalds 			.integer = 1,
40851da177e4SLinus Torvalds 		};
40861da177e4SLinus Torvalds 		return snd_interval_refine(c, &t);
40871da177e4SLinus Torvalds 	} else if (r->max < 64000) {
408855e957d8STakashi Iwai 		struct snd_interval t = {
40891da177e4SLinus Torvalds 			.min = hdsp->ss_in_channels,
40901da177e4SLinus Torvalds 			.max = hdsp->ss_in_channels,
40911da177e4SLinus Torvalds 			.integer = 1,
40921da177e4SLinus Torvalds 		};
40931da177e4SLinus Torvalds 		return snd_interval_refine(c, &t);
40941da177e4SLinus Torvalds 	}
40951da177e4SLinus Torvalds 	return 0;
40961da177e4SLinus Torvalds }
40971da177e4SLinus Torvalds 
409855e957d8STakashi Iwai static int snd_hdsp_hw_rule_out_channels_rate(struct snd_pcm_hw_params *params,
409955e957d8STakashi Iwai 					     struct snd_pcm_hw_rule *rule)
41001da177e4SLinus Torvalds {
410155e957d8STakashi Iwai 	struct hdsp *hdsp = rule->private;
410255e957d8STakashi Iwai 	struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
410355e957d8STakashi Iwai 	struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
41041da177e4SLinus Torvalds 	if (r->min > 96000 && hdsp->io_type == H9632) {
410555e957d8STakashi Iwai 		struct snd_interval t = {
41061da177e4SLinus Torvalds 			.min = hdsp->qs_out_channels,
41071da177e4SLinus Torvalds 			.max = hdsp->qs_out_channels,
41081da177e4SLinus Torvalds 			.integer = 1,
41091da177e4SLinus Torvalds 		};
41101da177e4SLinus Torvalds 		return snd_interval_refine(c, &t);
41111da177e4SLinus Torvalds 	} else if (r->min > 48000 && r->max <= 96000) {
411255e957d8STakashi Iwai 		struct snd_interval t = {
41131da177e4SLinus Torvalds 			.min = hdsp->ds_out_channels,
41141da177e4SLinus Torvalds 			.max = hdsp->ds_out_channels,
41151da177e4SLinus Torvalds 			.integer = 1,
41161da177e4SLinus Torvalds 		};
41171da177e4SLinus Torvalds 		return snd_interval_refine(c, &t);
41181da177e4SLinus Torvalds 	} else if (r->max < 64000) {
411955e957d8STakashi Iwai 		struct snd_interval t = {
41201da177e4SLinus Torvalds 			.min = hdsp->ss_out_channels,
41211da177e4SLinus Torvalds 			.max = hdsp->ss_out_channels,
41221da177e4SLinus Torvalds 			.integer = 1,
41231da177e4SLinus Torvalds 		};
41241da177e4SLinus Torvalds 		return snd_interval_refine(c, &t);
41251da177e4SLinus Torvalds 	}
41261da177e4SLinus Torvalds 	return 0;
41271da177e4SLinus Torvalds }
41281da177e4SLinus Torvalds 
412955e957d8STakashi Iwai static int snd_hdsp_hw_rule_rate_out_channels(struct snd_pcm_hw_params *params,
413055e957d8STakashi Iwai 					     struct snd_pcm_hw_rule *rule)
41311da177e4SLinus Torvalds {
413255e957d8STakashi Iwai 	struct hdsp *hdsp = rule->private;
413355e957d8STakashi Iwai 	struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
413455e957d8STakashi Iwai 	struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
41351da177e4SLinus Torvalds 	if (c->min >= hdsp->ss_out_channels) {
413655e957d8STakashi Iwai 		struct snd_interval t = {
41371da177e4SLinus Torvalds 			.min = 32000,
41381da177e4SLinus Torvalds 			.max = 48000,
41391da177e4SLinus Torvalds 			.integer = 1,
41401da177e4SLinus Torvalds 		};
41411da177e4SLinus Torvalds 		return snd_interval_refine(r, &t);
41421da177e4SLinus Torvalds 	} else if (c->max <= hdsp->qs_out_channels && hdsp->io_type == H9632) {
414355e957d8STakashi Iwai 		struct snd_interval t = {
41441da177e4SLinus Torvalds 			.min = 128000,
41451da177e4SLinus Torvalds 			.max = 192000,
41461da177e4SLinus Torvalds 			.integer = 1,
41471da177e4SLinus Torvalds 		};
41481da177e4SLinus Torvalds 		return snd_interval_refine(r, &t);
41491da177e4SLinus Torvalds 	} else if (c->max <= hdsp->ds_out_channels) {
415055e957d8STakashi Iwai 		struct snd_interval t = {
41511da177e4SLinus Torvalds 			.min = 64000,
41521da177e4SLinus Torvalds 			.max = 96000,
41531da177e4SLinus Torvalds 			.integer = 1,
41541da177e4SLinus Torvalds 		};
41551da177e4SLinus Torvalds 		return snd_interval_refine(r, &t);
41561da177e4SLinus Torvalds 	}
41571da177e4SLinus Torvalds 	return 0;
41581da177e4SLinus Torvalds }
41591da177e4SLinus Torvalds 
416055e957d8STakashi Iwai static int snd_hdsp_hw_rule_rate_in_channels(struct snd_pcm_hw_params *params,
416155e957d8STakashi Iwai 					     struct snd_pcm_hw_rule *rule)
41621da177e4SLinus Torvalds {
416355e957d8STakashi Iwai 	struct hdsp *hdsp = rule->private;
416455e957d8STakashi Iwai 	struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
416555e957d8STakashi Iwai 	struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
41661da177e4SLinus Torvalds 	if (c->min >= hdsp->ss_in_channels) {
416755e957d8STakashi Iwai 		struct snd_interval t = {
41681da177e4SLinus Torvalds 			.min = 32000,
41691da177e4SLinus Torvalds 			.max = 48000,
41701da177e4SLinus Torvalds 			.integer = 1,
41711da177e4SLinus Torvalds 		};
41721da177e4SLinus Torvalds 		return snd_interval_refine(r, &t);
41731da177e4SLinus Torvalds 	} else if (c->max <= hdsp->qs_in_channels && hdsp->io_type == H9632) {
417455e957d8STakashi Iwai 		struct snd_interval t = {
41751da177e4SLinus Torvalds 			.min = 128000,
41761da177e4SLinus Torvalds 			.max = 192000,
41771da177e4SLinus Torvalds 			.integer = 1,
41781da177e4SLinus Torvalds 		};
41791da177e4SLinus Torvalds 		return snd_interval_refine(r, &t);
41801da177e4SLinus Torvalds 	} else if (c->max <= hdsp->ds_in_channels) {
418155e957d8STakashi Iwai 		struct snd_interval t = {
41821da177e4SLinus Torvalds 			.min = 64000,
41831da177e4SLinus Torvalds 			.max = 96000,
41841da177e4SLinus Torvalds 			.integer = 1,
41851da177e4SLinus Torvalds 		};
41861da177e4SLinus Torvalds 		return snd_interval_refine(r, &t);
41871da177e4SLinus Torvalds 	}
41881da177e4SLinus Torvalds 	return 0;
41891da177e4SLinus Torvalds }
41901da177e4SLinus Torvalds 
419155e957d8STakashi Iwai static int snd_hdsp_playback_open(struct snd_pcm_substream *substream)
41921da177e4SLinus Torvalds {
419355e957d8STakashi Iwai 	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
419455e957d8STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
41951da177e4SLinus Torvalds 
4196b0b98119STakashi Iwai 	if (hdsp_check_for_iobox (hdsp))
41971da177e4SLinus Torvalds 		return -EIO;
41981da177e4SLinus Torvalds 
4199b0b98119STakashi Iwai 	if (hdsp_check_for_firmware(hdsp, 1))
42001da177e4SLinus Torvalds 		return -EIO;
42011da177e4SLinus Torvalds 
42021da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
42031da177e4SLinus Torvalds 
42041da177e4SLinus Torvalds 	snd_pcm_set_sync(substream);
42051da177e4SLinus Torvalds 
42061da177e4SLinus Torvalds         runtime->hw = snd_hdsp_playback_subinfo;
42071da177e4SLinus Torvalds 	runtime->dma_area = hdsp->playback_buffer;
42081da177e4SLinus Torvalds 	runtime->dma_bytes = HDSP_DMA_AREA_BYTES;
42091da177e4SLinus Torvalds 
42101da177e4SLinus Torvalds 	hdsp->playback_pid = current->pid;
42111da177e4SLinus Torvalds 	hdsp->playback_substream = substream;
42121da177e4SLinus Torvalds 
42131da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
42141da177e4SLinus Torvalds 
42151da177e4SLinus Torvalds 	snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
42161da177e4SLinus Torvalds 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, &hdsp_hw_constraints_period_sizes);
4217e3ea4d89STakashi Iwai 	if (hdsp->clock_source_locked) {
4218e3ea4d89STakashi Iwai 		runtime->hw.rate_min = runtime->hw.rate_max = hdsp->system_sample_rate;
4219e3ea4d89STakashi Iwai 	} else if (hdsp->io_type == H9632) {
42201da177e4SLinus Torvalds 		runtime->hw.rate_max = 192000;
42211da177e4SLinus Torvalds 		runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
42221da177e4SLinus Torvalds 		snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hdsp_hw_constraints_9632_sample_rates);
42231da177e4SLinus Torvalds 	}
4224e3ea4d89STakashi Iwai 	if (hdsp->io_type == H9632) {
4225e3ea4d89STakashi Iwai 		runtime->hw.channels_min = hdsp->qs_out_channels;
4226e3ea4d89STakashi Iwai 		runtime->hw.channels_max = hdsp->ss_out_channels;
4227e3ea4d89STakashi Iwai 	}
42281da177e4SLinus Torvalds 
42291da177e4SLinus Torvalds 	snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
42301da177e4SLinus Torvalds 			     snd_hdsp_hw_rule_out_channels, hdsp,
42311da177e4SLinus Torvalds 			     SNDRV_PCM_HW_PARAM_CHANNELS, -1);
42321da177e4SLinus Torvalds 	snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
42331da177e4SLinus Torvalds 			     snd_hdsp_hw_rule_out_channels_rate, hdsp,
42341da177e4SLinus Torvalds 			     SNDRV_PCM_HW_PARAM_RATE, -1);
42351da177e4SLinus Torvalds 	snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
42361da177e4SLinus Torvalds 			     snd_hdsp_hw_rule_rate_out_channels, hdsp,
42371da177e4SLinus Torvalds 			     SNDRV_PCM_HW_PARAM_CHANNELS, -1);
42381da177e4SLinus Torvalds 
42391da177e4SLinus Torvalds 	hdsp->creg_spdif_stream = hdsp->creg_spdif;
42401da177e4SLinus Torvalds 	hdsp->spdif_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
42411da177e4SLinus Torvalds 	snd_ctl_notify(hdsp->card, SNDRV_CTL_EVENT_MASK_VALUE |
42421da177e4SLinus Torvalds 		       SNDRV_CTL_EVENT_MASK_INFO, &hdsp->spdif_ctl->id);
42431da177e4SLinus Torvalds 	return 0;
42441da177e4SLinus Torvalds }
42451da177e4SLinus Torvalds 
424655e957d8STakashi Iwai static int snd_hdsp_playback_release(struct snd_pcm_substream *substream)
42471da177e4SLinus Torvalds {
424855e957d8STakashi Iwai 	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
42491da177e4SLinus Torvalds 
42501da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
42511da177e4SLinus Torvalds 
42521da177e4SLinus Torvalds 	hdsp->playback_pid = -1;
42531da177e4SLinus Torvalds 	hdsp->playback_substream = NULL;
42541da177e4SLinus Torvalds 
42551da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
42561da177e4SLinus Torvalds 
42571da177e4SLinus Torvalds 	hdsp->spdif_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
42581da177e4SLinus Torvalds 	snd_ctl_notify(hdsp->card, SNDRV_CTL_EVENT_MASK_VALUE |
42591da177e4SLinus Torvalds 		       SNDRV_CTL_EVENT_MASK_INFO, &hdsp->spdif_ctl->id);
42601da177e4SLinus Torvalds 	return 0;
42611da177e4SLinus Torvalds }
42621da177e4SLinus Torvalds 
42631da177e4SLinus Torvalds 
426455e957d8STakashi Iwai static int snd_hdsp_capture_open(struct snd_pcm_substream *substream)
42651da177e4SLinus Torvalds {
426655e957d8STakashi Iwai 	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
426755e957d8STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
42681da177e4SLinus Torvalds 
4269b0b98119STakashi Iwai 	if (hdsp_check_for_iobox (hdsp))
42701da177e4SLinus Torvalds 		return -EIO;
42711da177e4SLinus Torvalds 
4272b0b98119STakashi Iwai 	if (hdsp_check_for_firmware(hdsp, 1))
42731da177e4SLinus Torvalds 		return -EIO;
42741da177e4SLinus Torvalds 
42751da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
42761da177e4SLinus Torvalds 
42771da177e4SLinus Torvalds 	snd_pcm_set_sync(substream);
42781da177e4SLinus Torvalds 
42791da177e4SLinus Torvalds 	runtime->hw = snd_hdsp_capture_subinfo;
42801da177e4SLinus Torvalds 	runtime->dma_area = hdsp->capture_buffer;
42811da177e4SLinus Torvalds 	runtime->dma_bytes = HDSP_DMA_AREA_BYTES;
42821da177e4SLinus Torvalds 
42831da177e4SLinus Torvalds 	hdsp->capture_pid = current->pid;
42841da177e4SLinus Torvalds 	hdsp->capture_substream = substream;
42851da177e4SLinus Torvalds 
42861da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
42871da177e4SLinus Torvalds 
42881da177e4SLinus Torvalds 	snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
42891da177e4SLinus Torvalds 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, &hdsp_hw_constraints_period_sizes);
42901da177e4SLinus Torvalds 	if (hdsp->io_type == H9632) {
42911da177e4SLinus Torvalds 		runtime->hw.channels_min = hdsp->qs_in_channels;
42921da177e4SLinus Torvalds 		runtime->hw.channels_max = hdsp->ss_in_channels;
42931da177e4SLinus Torvalds 		runtime->hw.rate_max = 192000;
42941da177e4SLinus Torvalds 		runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
42951da177e4SLinus Torvalds 		snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hdsp_hw_constraints_9632_sample_rates);
42961da177e4SLinus Torvalds 	}
42971da177e4SLinus Torvalds 	snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
42981da177e4SLinus Torvalds 			     snd_hdsp_hw_rule_in_channels, hdsp,
42991da177e4SLinus Torvalds 			     SNDRV_PCM_HW_PARAM_CHANNELS, -1);
43001da177e4SLinus Torvalds 	snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
43011da177e4SLinus Torvalds 			     snd_hdsp_hw_rule_in_channels_rate, hdsp,
43021da177e4SLinus Torvalds 			     SNDRV_PCM_HW_PARAM_RATE, -1);
43031da177e4SLinus Torvalds 	snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
43041da177e4SLinus Torvalds 			     snd_hdsp_hw_rule_rate_in_channels, hdsp,
43051da177e4SLinus Torvalds 			     SNDRV_PCM_HW_PARAM_CHANNELS, -1);
43061da177e4SLinus Torvalds 	return 0;
43071da177e4SLinus Torvalds }
43081da177e4SLinus Torvalds 
430955e957d8STakashi Iwai static int snd_hdsp_capture_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->capture_pid = -1;
43161da177e4SLinus Torvalds 	hdsp->capture_substream = NULL;
43171da177e4SLinus Torvalds 
43181da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
43191da177e4SLinus Torvalds 	return 0;
43201da177e4SLinus Torvalds }
43211da177e4SLinus Torvalds 
432255e957d8STakashi Iwai static int snd_hdsp_hwdep_dummy_op(struct snd_hwdep *hw, struct file *file)
43231da177e4SLinus Torvalds {
43241da177e4SLinus Torvalds 	/* we have nothing to initialize but the call is required */
43251da177e4SLinus Torvalds 	return 0;
43261da177e4SLinus Torvalds }
43271da177e4SLinus Torvalds 
43281da177e4SLinus Torvalds 
43291da177e4SLinus Torvalds /* helper functions for copying meter values */
43301da177e4SLinus Torvalds static inline int copy_u32_le(void __user *dest, void __iomem *src)
43311da177e4SLinus Torvalds {
43321da177e4SLinus Torvalds 	u32 val = readl(src);
43331da177e4SLinus Torvalds 	return copy_to_user(dest, &val, 4);
43341da177e4SLinus Torvalds }
43351da177e4SLinus Torvalds 
43361da177e4SLinus Torvalds static inline int copy_u64_le(void __user *dest, void __iomem *src_low, void __iomem *src_high)
43371da177e4SLinus Torvalds {
43381da177e4SLinus Torvalds 	u32 rms_low, rms_high;
43391da177e4SLinus Torvalds 	u64 rms;
43401da177e4SLinus Torvalds 	rms_low = readl(src_low);
43411da177e4SLinus Torvalds 	rms_high = readl(src_high);
43421da177e4SLinus Torvalds 	rms = ((u64)rms_high << 32) | rms_low;
43431da177e4SLinus Torvalds 	return copy_to_user(dest, &rms, 8);
43441da177e4SLinus Torvalds }
43451da177e4SLinus Torvalds 
43461da177e4SLinus Torvalds static inline int copy_u48_le(void __user *dest, void __iomem *src_low, void __iomem *src_high)
43471da177e4SLinus Torvalds {
43481da177e4SLinus Torvalds 	u32 rms_low, rms_high;
43491da177e4SLinus Torvalds 	u64 rms;
43501da177e4SLinus Torvalds 	rms_low = readl(src_low) & 0xffffff00;
43511da177e4SLinus Torvalds 	rms_high = readl(src_high) & 0xffffff00;
43521da177e4SLinus Torvalds 	rms = ((u64)rms_high << 32) | rms_low;
43531da177e4SLinus Torvalds 	return copy_to_user(dest, &rms, 8);
43541da177e4SLinus Torvalds }
43551da177e4SLinus Torvalds 
435655e957d8STakashi Iwai static int hdsp_9652_get_peak(struct hdsp *hdsp, struct hdsp_peak_rms __user *peak_rms)
43571da177e4SLinus Torvalds {
43581da177e4SLinus Torvalds 	int doublespeed = 0;
43591da177e4SLinus Torvalds 	int i, j, channels, ofs;
43601da177e4SLinus Torvalds 
43611da177e4SLinus Torvalds 	if (hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DoubleSpeedStatus)
43621da177e4SLinus Torvalds 		doublespeed = 1;
43631da177e4SLinus Torvalds 	channels = doublespeed ? 14 : 26;
43641da177e4SLinus Torvalds 	for (i = 0, j = 0; i < 26; ++i) {
43651da177e4SLinus Torvalds 		if (doublespeed && (i & 4))
43661da177e4SLinus Torvalds 			continue;
43671da177e4SLinus Torvalds 		ofs = HDSP_9652_peakBase - j * 4;
43681da177e4SLinus Torvalds 		if (copy_u32_le(&peak_rms->input_peaks[i], hdsp->iobase + ofs))
43691da177e4SLinus Torvalds 			return -EFAULT;
43701da177e4SLinus Torvalds 		ofs -= channels * 4;
43711da177e4SLinus Torvalds 		if (copy_u32_le(&peak_rms->playback_peaks[i], hdsp->iobase + ofs))
43721da177e4SLinus Torvalds 			return -EFAULT;
43731da177e4SLinus Torvalds 		ofs -= channels * 4;
43741da177e4SLinus Torvalds 		if (copy_u32_le(&peak_rms->output_peaks[i], hdsp->iobase + ofs))
43751da177e4SLinus Torvalds 			return -EFAULT;
43761da177e4SLinus Torvalds 		ofs = HDSP_9652_rmsBase + j * 8;
43771da177e4SLinus Torvalds 		if (copy_u48_le(&peak_rms->input_rms[i], hdsp->iobase + ofs,
43781da177e4SLinus Torvalds 				hdsp->iobase + ofs + 4))
43791da177e4SLinus Torvalds 			return -EFAULT;
43801da177e4SLinus Torvalds 		ofs += channels * 8;
43811da177e4SLinus Torvalds 		if (copy_u48_le(&peak_rms->playback_rms[i], hdsp->iobase + ofs,
43821da177e4SLinus Torvalds 				hdsp->iobase + ofs + 4))
43831da177e4SLinus Torvalds 			return -EFAULT;
43841da177e4SLinus Torvalds 		ofs += channels * 8;
43851da177e4SLinus Torvalds 		if (copy_u48_le(&peak_rms->output_rms[i], hdsp->iobase + ofs,
43861da177e4SLinus Torvalds 				hdsp->iobase + ofs + 4))
43871da177e4SLinus Torvalds 			return -EFAULT;
43881da177e4SLinus Torvalds 		j++;
43891da177e4SLinus Torvalds 	}
43901da177e4SLinus Torvalds 	return 0;
43911da177e4SLinus Torvalds }
43921da177e4SLinus Torvalds 
439355e957d8STakashi Iwai static int hdsp_9632_get_peak(struct hdsp *hdsp, struct hdsp_peak_rms __user *peak_rms)
43941da177e4SLinus Torvalds {
43951da177e4SLinus Torvalds 	int i, j;
439655e957d8STakashi Iwai 	struct hdsp_9632_meters __iomem *m;
43971da177e4SLinus Torvalds 	int doublespeed = 0;
43981da177e4SLinus Torvalds 
43991da177e4SLinus Torvalds 	if (hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DoubleSpeedStatus)
44001da177e4SLinus Torvalds 		doublespeed = 1;
440155e957d8STakashi Iwai 	m = (struct hdsp_9632_meters __iomem *)(hdsp->iobase+HDSP_9632_metersBase);
44021da177e4SLinus Torvalds 	for (i = 0, j = 0; i < 16; ++i, ++j) {
44031da177e4SLinus Torvalds 		if (copy_u32_le(&peak_rms->input_peaks[i], &m->input_peak[j]))
44041da177e4SLinus Torvalds 			return -EFAULT;
44051da177e4SLinus Torvalds 		if (copy_u32_le(&peak_rms->playback_peaks[i], &m->playback_peak[j]))
44061da177e4SLinus Torvalds 			return -EFAULT;
44071da177e4SLinus Torvalds 		if (copy_u32_le(&peak_rms->output_peaks[i], &m->output_peak[j]))
44081da177e4SLinus Torvalds 			return -EFAULT;
44091da177e4SLinus Torvalds 		if (copy_u64_le(&peak_rms->input_rms[i], &m->input_rms_low[j],
44101da177e4SLinus Torvalds 				&m->input_rms_high[j]))
44111da177e4SLinus Torvalds 			return -EFAULT;
44121da177e4SLinus Torvalds 		if (copy_u64_le(&peak_rms->playback_rms[i], &m->playback_rms_low[j],
44131da177e4SLinus Torvalds 				&m->playback_rms_high[j]))
44141da177e4SLinus Torvalds 			return -EFAULT;
44151da177e4SLinus Torvalds 		if (copy_u64_le(&peak_rms->output_rms[i], &m->output_rms_low[j],
44161da177e4SLinus Torvalds 				&m->output_rms_high[j]))
44171da177e4SLinus Torvalds 			return -EFAULT;
44181da177e4SLinus Torvalds 		if (doublespeed && i == 3) i += 4;
44191da177e4SLinus Torvalds 	}
44201da177e4SLinus Torvalds 	return 0;
44211da177e4SLinus Torvalds }
44221da177e4SLinus Torvalds 
442355e957d8STakashi Iwai static int hdsp_get_peak(struct hdsp *hdsp, struct hdsp_peak_rms __user *peak_rms)
44241da177e4SLinus Torvalds {
44251da177e4SLinus Torvalds 	int i;
44261da177e4SLinus Torvalds 
44271da177e4SLinus Torvalds 	for (i = 0; i < 26; i++) {
44281da177e4SLinus Torvalds 		if (copy_u32_le(&peak_rms->playback_peaks[i],
44291da177e4SLinus Torvalds 				hdsp->iobase + HDSP_playbackPeakLevel + i * 4))
44301da177e4SLinus Torvalds 			return -EFAULT;
44311da177e4SLinus Torvalds 		if (copy_u32_le(&peak_rms->input_peaks[i],
44321da177e4SLinus Torvalds 				hdsp->iobase + HDSP_inputPeakLevel + i * 4))
44331da177e4SLinus Torvalds 			return -EFAULT;
44341da177e4SLinus Torvalds 	}
44351da177e4SLinus Torvalds 	for (i = 0; i < 28; i++) {
44361da177e4SLinus Torvalds 		if (copy_u32_le(&peak_rms->output_peaks[i],
44371da177e4SLinus Torvalds 				hdsp->iobase + HDSP_outputPeakLevel + i * 4))
44381da177e4SLinus Torvalds 			return -EFAULT;
44391da177e4SLinus Torvalds 	}
44401da177e4SLinus Torvalds 	for (i = 0; i < 26; ++i) {
44411da177e4SLinus Torvalds 		if (copy_u64_le(&peak_rms->playback_rms[i],
44421da177e4SLinus Torvalds 				hdsp->iobase + HDSP_playbackRmsLevel + i * 8 + 4,
44431da177e4SLinus Torvalds 				hdsp->iobase + HDSP_playbackRmsLevel + i * 8))
44441da177e4SLinus Torvalds 			return -EFAULT;
44451da177e4SLinus Torvalds 		if (copy_u64_le(&peak_rms->input_rms[i],
44461da177e4SLinus Torvalds 				hdsp->iobase + HDSP_inputRmsLevel + i * 8 + 4,
44471da177e4SLinus Torvalds 				hdsp->iobase + HDSP_inputRmsLevel + i * 8))
44481da177e4SLinus Torvalds 			return -EFAULT;
44491da177e4SLinus Torvalds 	}
44501da177e4SLinus Torvalds 	return 0;
44511da177e4SLinus Torvalds }
44521da177e4SLinus Torvalds 
445355e957d8STakashi Iwai static int snd_hdsp_hwdep_ioctl(struct snd_hwdep *hw, struct file *file, unsigned int cmd, unsigned long arg)
44541da177e4SLinus Torvalds {
445555e957d8STakashi Iwai 	struct hdsp *hdsp = (struct hdsp *)hw->private_data;
44561da177e4SLinus Torvalds 	void __user *argp = (void __user *)arg;
44571da177e4SLinus Torvalds 
44581da177e4SLinus Torvalds 	switch (cmd) {
44591da177e4SLinus Torvalds 	case SNDRV_HDSP_IOCTL_GET_PEAK_RMS: {
446055e957d8STakashi Iwai 		struct hdsp_peak_rms __user *peak_rms = (struct hdsp_peak_rms __user *)arg;
44611da177e4SLinus Torvalds 
44621da177e4SLinus Torvalds 		if (!(hdsp->state & HDSP_FirmwareLoaded)) {
44631da177e4SLinus Torvalds 			snd_printk(KERN_ERR "Hammerfall-DSP: firmware needs to be uploaded to the card.\n");
44641da177e4SLinus Torvalds 			return -EINVAL;
44651da177e4SLinus Torvalds 		}
44661da177e4SLinus Torvalds 
44671da177e4SLinus Torvalds 		switch (hdsp->io_type) {
44681da177e4SLinus Torvalds 		case H9652:
44691da177e4SLinus Torvalds 			return hdsp_9652_get_peak(hdsp, peak_rms);
44701da177e4SLinus Torvalds 		case H9632:
44711da177e4SLinus Torvalds 			return hdsp_9632_get_peak(hdsp, peak_rms);
44721da177e4SLinus Torvalds 		default:
44731da177e4SLinus Torvalds 			return hdsp_get_peak(hdsp, peak_rms);
44741da177e4SLinus Torvalds 		}
44751da177e4SLinus Torvalds 	}
44761da177e4SLinus Torvalds 	case SNDRV_HDSP_IOCTL_GET_CONFIG_INFO: {
447755e957d8STakashi Iwai 		struct hdsp_config_info info;
44781da177e4SLinus Torvalds 		unsigned long flags;
44791da177e4SLinus Torvalds 		int i;
44801da177e4SLinus Torvalds 
44811da177e4SLinus Torvalds 		if (!(hdsp->state & HDSP_FirmwareLoaded)) {
4482b0b98119STakashi Iwai 			snd_printk(KERN_ERR "Hammerfall-DSP: Firmware needs to be uploaded to the card.\n");
44831da177e4SLinus Torvalds 			return -EINVAL;
44841da177e4SLinus Torvalds 		}
44851da177e4SLinus Torvalds 		spin_lock_irqsave(&hdsp->lock, flags);
44861da177e4SLinus Torvalds 		info.pref_sync_ref = (unsigned char)hdsp_pref_sync_ref(hdsp);
44871da177e4SLinus Torvalds 		info.wordclock_sync_check = (unsigned char)hdsp_wc_sync_check(hdsp);
4488b0b98119STakashi Iwai 		if (hdsp->io_type != H9632)
44891da177e4SLinus Torvalds 		    info.adatsync_sync_check = (unsigned char)hdsp_adatsync_sync_check(hdsp);
44901da177e4SLinus Torvalds 		info.spdif_sync_check = (unsigned char)hdsp_spdif_sync_check(hdsp);
4491b0b98119STakashi Iwai 		for (i = 0; i < ((hdsp->io_type != Multiface && hdsp->io_type != H9632) ? 3 : 1); ++i)
44921da177e4SLinus Torvalds 			info.adat_sync_check[i] = (unsigned char)hdsp_adat_sync_check(hdsp, i);
44931da177e4SLinus Torvalds 		info.spdif_in = (unsigned char)hdsp_spdif_in(hdsp);
44941da177e4SLinus Torvalds 		info.spdif_out = (unsigned char)hdsp_spdif_out(hdsp);
44951da177e4SLinus Torvalds 		info.spdif_professional = (unsigned char)hdsp_spdif_professional(hdsp);
44961da177e4SLinus Torvalds 		info.spdif_emphasis = (unsigned char)hdsp_spdif_emphasis(hdsp);
44971da177e4SLinus Torvalds 		info.spdif_nonaudio = (unsigned char)hdsp_spdif_nonaudio(hdsp);
44981da177e4SLinus Torvalds 		info.spdif_sample_rate = hdsp_spdif_sample_rate(hdsp);
44991da177e4SLinus Torvalds 		info.system_sample_rate = hdsp->system_sample_rate;
45001da177e4SLinus Torvalds 		info.autosync_sample_rate = hdsp_external_sample_rate(hdsp);
45011da177e4SLinus Torvalds 		info.system_clock_mode = (unsigned char)hdsp_system_clock_mode(hdsp);
45021da177e4SLinus Torvalds 		info.clock_source = (unsigned char)hdsp_clock_source(hdsp);
45031da177e4SLinus Torvalds 		info.autosync_ref = (unsigned char)hdsp_autosync_ref(hdsp);
45041da177e4SLinus Torvalds 		info.line_out = (unsigned char)hdsp_line_out(hdsp);
45051da177e4SLinus Torvalds 		if (hdsp->io_type == H9632) {
45061da177e4SLinus Torvalds 			info.da_gain = (unsigned char)hdsp_da_gain(hdsp);
45071da177e4SLinus Torvalds 			info.ad_gain = (unsigned char)hdsp_ad_gain(hdsp);
45081da177e4SLinus Torvalds 			info.phone_gain = (unsigned char)hdsp_phone_gain(hdsp);
45091da177e4SLinus Torvalds 			info.xlr_breakout_cable = (unsigned char)hdsp_xlr_breakout_cable(hdsp);
45101da177e4SLinus Torvalds 
45111da177e4SLinus Torvalds 		}
4512b0b98119STakashi Iwai 		if (hdsp->io_type == H9632 || hdsp->io_type == H9652)
45131da177e4SLinus Torvalds 			info.analog_extension_board = (unsigned char)hdsp_aeb(hdsp);
45141da177e4SLinus Torvalds 		spin_unlock_irqrestore(&hdsp->lock, flags);
45151da177e4SLinus Torvalds 		if (copy_to_user(argp, &info, sizeof(info)))
45161da177e4SLinus Torvalds 			return -EFAULT;
45171da177e4SLinus Torvalds 		break;
45181da177e4SLinus Torvalds 	}
45191da177e4SLinus Torvalds 	case SNDRV_HDSP_IOCTL_GET_9632_AEB: {
452055e957d8STakashi Iwai 		struct hdsp_9632_aeb h9632_aeb;
45211da177e4SLinus Torvalds 
45221da177e4SLinus Torvalds 		if (hdsp->io_type != H9632) return -EINVAL;
45231da177e4SLinus Torvalds 		h9632_aeb.aebi = hdsp->ss_in_channels - H9632_SS_CHANNELS;
45241da177e4SLinus Torvalds 		h9632_aeb.aebo = hdsp->ss_out_channels - H9632_SS_CHANNELS;
45251da177e4SLinus Torvalds 		if (copy_to_user(argp, &h9632_aeb, sizeof(h9632_aeb)))
45261da177e4SLinus Torvalds 			return -EFAULT;
45271da177e4SLinus Torvalds 		break;
45281da177e4SLinus Torvalds 	}
45291da177e4SLinus Torvalds 	case SNDRV_HDSP_IOCTL_GET_VERSION: {
453055e957d8STakashi Iwai 		struct hdsp_version hdsp_version;
45311da177e4SLinus Torvalds 		int err;
45321da177e4SLinus Torvalds 
45331da177e4SLinus Torvalds 		if (hdsp->io_type == H9652 || hdsp->io_type == H9632) return -EINVAL;
45341da177e4SLinus Torvalds 		if (hdsp->io_type == Undefined) {
4535b0b98119STakashi Iwai 			if ((err = hdsp_get_iobox_version(hdsp)) < 0)
45361da177e4SLinus Torvalds 				return err;
45371da177e4SLinus Torvalds 		}
45381da177e4SLinus Torvalds 		hdsp_version.io_type = hdsp->io_type;
45391da177e4SLinus Torvalds 		hdsp_version.firmware_rev = hdsp->firmware_rev;
4540b0b98119STakashi Iwai 		if ((err = copy_to_user(argp, &hdsp_version, sizeof(hdsp_version))))
45411da177e4SLinus Torvalds 		    	return -EFAULT;
45421da177e4SLinus Torvalds 		break;
45431da177e4SLinus Torvalds 	}
45441da177e4SLinus Torvalds 	case SNDRV_HDSP_IOCTL_UPLOAD_FIRMWARE: {
454555e957d8STakashi Iwai 		struct hdsp_firmware __user *firmware;
45461da177e4SLinus Torvalds 		u32 __user *firmware_data;
45471da177e4SLinus Torvalds 		int err;
45481da177e4SLinus Torvalds 
45491da177e4SLinus Torvalds 		if (hdsp->io_type == H9652 || hdsp->io_type == H9632) return -EINVAL;
45501da177e4SLinus Torvalds 		/* SNDRV_HDSP_IOCTL_GET_VERSION must have been called */
45511da177e4SLinus Torvalds 		if (hdsp->io_type == Undefined) return -EINVAL;
45521da177e4SLinus Torvalds 
45531da177e4SLinus Torvalds 		if (hdsp->state & (HDSP_FirmwareCached | HDSP_FirmwareLoaded))
45541da177e4SLinus Torvalds 			return -EBUSY;
45551da177e4SLinus Torvalds 
4556b0b98119STakashi Iwai 		snd_printk(KERN_INFO "Hammerfall-DSP: initializing firmware upload\n");
455755e957d8STakashi Iwai 		firmware = (struct hdsp_firmware __user *)argp;
45581da177e4SLinus Torvalds 
4559b0b98119STakashi Iwai 		if (get_user(firmware_data, &firmware->firmware_data))
45601da177e4SLinus Torvalds 			return -EFAULT;
45611da177e4SLinus Torvalds 
4562b0b98119STakashi Iwai 		if (hdsp_check_for_iobox (hdsp))
45631da177e4SLinus Torvalds 			return -EIO;
45641da177e4SLinus Torvalds 
4565b0b98119STakashi Iwai 		if (copy_from_user(hdsp->firmware_cache, firmware_data, sizeof(hdsp->firmware_cache)) != 0)
45661da177e4SLinus Torvalds 			return -EFAULT;
45671da177e4SLinus Torvalds 
45681da177e4SLinus Torvalds 		hdsp->state |= HDSP_FirmwareCached;
45691da177e4SLinus Torvalds 
4570b0b98119STakashi Iwai 		if ((err = snd_hdsp_load_firmware_from_cache(hdsp)) < 0)
45711da177e4SLinus Torvalds 			return err;
45721da177e4SLinus Torvalds 
45731da177e4SLinus Torvalds 		if (!(hdsp->state & HDSP_InitializationComplete)) {
4574b0b98119STakashi Iwai 			if ((err = snd_hdsp_enable_io(hdsp)) < 0)
45751da177e4SLinus Torvalds 				return err;
45761da177e4SLinus Torvalds 
45771da177e4SLinus Torvalds 			snd_hdsp_initialize_channels(hdsp);
45781da177e4SLinus Torvalds 			snd_hdsp_initialize_midi_flush(hdsp);
45791da177e4SLinus Torvalds 
45801da177e4SLinus Torvalds 			if ((err = snd_hdsp_create_alsa_devices(hdsp->card, hdsp)) < 0) {
4581b0b98119STakashi Iwai 				snd_printk(KERN_ERR "Hammerfall-DSP: error creating alsa devices\n");
45821da177e4SLinus Torvalds 				return err;
45831da177e4SLinus Torvalds 			}
45841da177e4SLinus Torvalds 		}
45851da177e4SLinus Torvalds 		break;
45861da177e4SLinus Torvalds 	}
45871da177e4SLinus Torvalds 	case SNDRV_HDSP_IOCTL_GET_MIXER: {
458855e957d8STakashi Iwai 		struct hdsp_mixer __user *mixer = (struct hdsp_mixer __user *)argp;
45891da177e4SLinus Torvalds 		if (copy_to_user(mixer->matrix, hdsp->mixer_matrix, sizeof(unsigned short)*HDSP_MATRIX_MIXER_SIZE))
45901da177e4SLinus Torvalds 			return -EFAULT;
45911da177e4SLinus Torvalds 		break;
45921da177e4SLinus Torvalds 	}
45931da177e4SLinus Torvalds 	default:
45941da177e4SLinus Torvalds 		return -EINVAL;
45951da177e4SLinus Torvalds 	}
45961da177e4SLinus Torvalds 	return 0;
45971da177e4SLinus Torvalds }
45981da177e4SLinus Torvalds 
459955e957d8STakashi Iwai static struct snd_pcm_ops snd_hdsp_playback_ops = {
46001da177e4SLinus Torvalds 	.open =		snd_hdsp_playback_open,
46011da177e4SLinus Torvalds 	.close =	snd_hdsp_playback_release,
46021da177e4SLinus Torvalds 	.ioctl =	snd_hdsp_ioctl,
46031da177e4SLinus Torvalds 	.hw_params =	snd_hdsp_hw_params,
46041da177e4SLinus Torvalds 	.prepare =	snd_hdsp_prepare,
46051da177e4SLinus Torvalds 	.trigger =	snd_hdsp_trigger,
46061da177e4SLinus Torvalds 	.pointer =	snd_hdsp_hw_pointer,
46071da177e4SLinus Torvalds 	.copy =		snd_hdsp_playback_copy,
46081da177e4SLinus Torvalds 	.silence =	snd_hdsp_hw_silence,
46091da177e4SLinus Torvalds };
46101da177e4SLinus Torvalds 
461155e957d8STakashi Iwai static struct snd_pcm_ops snd_hdsp_capture_ops = {
46121da177e4SLinus Torvalds 	.open =		snd_hdsp_capture_open,
46131da177e4SLinus Torvalds 	.close =	snd_hdsp_capture_release,
46141da177e4SLinus Torvalds 	.ioctl =	snd_hdsp_ioctl,
46151da177e4SLinus Torvalds 	.hw_params =	snd_hdsp_hw_params,
46161da177e4SLinus Torvalds 	.prepare =	snd_hdsp_prepare,
46171da177e4SLinus Torvalds 	.trigger =	snd_hdsp_trigger,
46181da177e4SLinus Torvalds 	.pointer =	snd_hdsp_hw_pointer,
46191da177e4SLinus Torvalds 	.copy =		snd_hdsp_capture_copy,
46201da177e4SLinus Torvalds };
46211da177e4SLinus Torvalds 
462255e957d8STakashi Iwai static int __devinit snd_hdsp_create_hwdep(struct snd_card *card,
462355e957d8STakashi Iwai 					   struct hdsp *hdsp)
46241da177e4SLinus Torvalds {
462555e957d8STakashi Iwai 	struct snd_hwdep *hw;
46261da177e4SLinus Torvalds 	int err;
46271da177e4SLinus Torvalds 
46281da177e4SLinus Torvalds 	if ((err = snd_hwdep_new(card, "HDSP hwdep", 0, &hw)) < 0)
46291da177e4SLinus Torvalds 		return err;
46301da177e4SLinus Torvalds 
46311da177e4SLinus Torvalds 	hdsp->hwdep = hw;
46321da177e4SLinus Torvalds 	hw->private_data = hdsp;
46331da177e4SLinus Torvalds 	strcpy(hw->name, "HDSP hwdep interface");
46341da177e4SLinus Torvalds 
46351da177e4SLinus Torvalds 	hw->ops.open = snd_hdsp_hwdep_dummy_op;
46361da177e4SLinus Torvalds 	hw->ops.ioctl = snd_hdsp_hwdep_ioctl;
46371da177e4SLinus Torvalds 	hw->ops.release = snd_hdsp_hwdep_dummy_op;
46381da177e4SLinus Torvalds 
46391da177e4SLinus Torvalds 	return 0;
46401da177e4SLinus Torvalds }
46411da177e4SLinus Torvalds 
464255e957d8STakashi Iwai static int snd_hdsp_create_pcm(struct snd_card *card, struct hdsp *hdsp)
46431da177e4SLinus Torvalds {
464455e957d8STakashi Iwai 	struct snd_pcm *pcm;
46451da177e4SLinus Torvalds 	int err;
46461da177e4SLinus Torvalds 
46471da177e4SLinus Torvalds 	if ((err = snd_pcm_new(card, hdsp->card_name, 0, 1, 1, &pcm)) < 0)
46481da177e4SLinus Torvalds 		return err;
46491da177e4SLinus Torvalds 
46501da177e4SLinus Torvalds 	hdsp->pcm = pcm;
46511da177e4SLinus Torvalds 	pcm->private_data = hdsp;
46521da177e4SLinus Torvalds 	strcpy(pcm->name, hdsp->card_name);
46531da177e4SLinus Torvalds 
46541da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_hdsp_playback_ops);
46551da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_hdsp_capture_ops);
46561da177e4SLinus Torvalds 
46571da177e4SLinus Torvalds 	pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
46581da177e4SLinus Torvalds 
46591da177e4SLinus Torvalds 	return 0;
46601da177e4SLinus Torvalds }
46611da177e4SLinus Torvalds 
466255e957d8STakashi Iwai static void snd_hdsp_9652_enable_mixer (struct hdsp *hdsp)
46631da177e4SLinus Torvalds {
46641da177e4SLinus Torvalds         hdsp->control2_register |= HDSP_9652_ENABLE_MIXER;
46651da177e4SLinus Torvalds 	hdsp_write (hdsp, HDSP_control2Reg, hdsp->control2_register);
46661da177e4SLinus Torvalds }
46671da177e4SLinus Torvalds 
466855e957d8STakashi Iwai static int snd_hdsp_enable_io (struct hdsp *hdsp)
46691da177e4SLinus Torvalds {
46701da177e4SLinus Torvalds 	int i;
46711da177e4SLinus Torvalds 
46721da177e4SLinus Torvalds 	if (hdsp_fifo_wait (hdsp, 0, 100)) {
4673b0b98119STakashi Iwai 		snd_printk(KERN_ERR "Hammerfall-DSP: enable_io fifo_wait failed\n");
46741da177e4SLinus Torvalds 		return -EIO;
46751da177e4SLinus Torvalds 	}
46761da177e4SLinus Torvalds 
46771da177e4SLinus Torvalds 	for (i = 0; i < hdsp->max_channels; ++i) {
46781da177e4SLinus Torvalds 		hdsp_write (hdsp, HDSP_inputEnable + (4 * i), 1);
46791da177e4SLinus Torvalds 		hdsp_write (hdsp, HDSP_outputEnable + (4 * i), 1);
46801da177e4SLinus Torvalds 	}
46811da177e4SLinus Torvalds 
46821da177e4SLinus Torvalds 	return 0;
46831da177e4SLinus Torvalds }
46841da177e4SLinus Torvalds 
468555e957d8STakashi Iwai static void snd_hdsp_initialize_channels(struct hdsp *hdsp)
46861da177e4SLinus Torvalds {
46871da177e4SLinus Torvalds 	int status, aebi_channels, aebo_channels;
46881da177e4SLinus Torvalds 
46891da177e4SLinus Torvalds 	switch (hdsp->io_type) {
46901da177e4SLinus Torvalds 	case Digiface:
46911da177e4SLinus Torvalds 		hdsp->card_name = "RME Hammerfall DSP + Digiface";
46921da177e4SLinus Torvalds 		hdsp->ss_in_channels = hdsp->ss_out_channels = DIGIFACE_SS_CHANNELS;
46931da177e4SLinus Torvalds 		hdsp->ds_in_channels = hdsp->ds_out_channels = DIGIFACE_DS_CHANNELS;
46941da177e4SLinus Torvalds 		break;
46951da177e4SLinus Torvalds 
46961da177e4SLinus Torvalds 	case H9652:
46971da177e4SLinus Torvalds 		hdsp->card_name = "RME Hammerfall HDSP 9652";
46981da177e4SLinus Torvalds 		hdsp->ss_in_channels = hdsp->ss_out_channels = H9652_SS_CHANNELS;
46991da177e4SLinus Torvalds 		hdsp->ds_in_channels = hdsp->ds_out_channels = H9652_DS_CHANNELS;
47001da177e4SLinus Torvalds 		break;
47011da177e4SLinus Torvalds 
47021da177e4SLinus Torvalds 	case H9632:
47031da177e4SLinus Torvalds 		status = hdsp_read(hdsp, HDSP_statusRegister);
47041da177e4SLinus Torvalds 		/* HDSP_AEBx bits are low when AEB are connected */
47051da177e4SLinus Torvalds 		aebi_channels = (status & HDSP_AEBI) ? 0 : 4;
47061da177e4SLinus Torvalds 		aebo_channels = (status & HDSP_AEBO) ? 0 : 4;
47071da177e4SLinus Torvalds 		hdsp->card_name = "RME Hammerfall HDSP 9632";
47081da177e4SLinus Torvalds 		hdsp->ss_in_channels = H9632_SS_CHANNELS+aebi_channels;
47091da177e4SLinus Torvalds 		hdsp->ds_in_channels = H9632_DS_CHANNELS+aebi_channels;
47101da177e4SLinus Torvalds 		hdsp->qs_in_channels = H9632_QS_CHANNELS+aebi_channels;
47111da177e4SLinus Torvalds 		hdsp->ss_out_channels = H9632_SS_CHANNELS+aebo_channels;
47121da177e4SLinus Torvalds 		hdsp->ds_out_channels = H9632_DS_CHANNELS+aebo_channels;
47131da177e4SLinus Torvalds 		hdsp->qs_out_channels = H9632_QS_CHANNELS+aebo_channels;
47141da177e4SLinus Torvalds 		break;
47151da177e4SLinus Torvalds 
47161da177e4SLinus Torvalds 	case Multiface:
47171da177e4SLinus Torvalds 		hdsp->card_name = "RME Hammerfall DSP + Multiface";
47181da177e4SLinus Torvalds 		hdsp->ss_in_channels = hdsp->ss_out_channels = MULTIFACE_SS_CHANNELS;
47191da177e4SLinus Torvalds 		hdsp->ds_in_channels = hdsp->ds_out_channels = MULTIFACE_DS_CHANNELS;
47201da177e4SLinus Torvalds 		break;
47211da177e4SLinus Torvalds 
47221da177e4SLinus Torvalds 	default:
47231da177e4SLinus Torvalds  		/* should never get here */
47241da177e4SLinus Torvalds 		break;
47251da177e4SLinus Torvalds 	}
47261da177e4SLinus Torvalds }
47271da177e4SLinus Torvalds 
472855e957d8STakashi Iwai static void snd_hdsp_initialize_midi_flush (struct hdsp *hdsp)
47291da177e4SLinus Torvalds {
47301da177e4SLinus Torvalds 	snd_hdsp_flush_midi_input (hdsp, 0);
47311da177e4SLinus Torvalds 	snd_hdsp_flush_midi_input (hdsp, 1);
47321da177e4SLinus Torvalds }
47331da177e4SLinus Torvalds 
473455e957d8STakashi Iwai static int snd_hdsp_create_alsa_devices(struct snd_card *card, struct hdsp *hdsp)
47351da177e4SLinus Torvalds {
47361da177e4SLinus Torvalds 	int err;
47371da177e4SLinus Torvalds 
47381da177e4SLinus Torvalds 	if ((err = snd_hdsp_create_pcm(card, hdsp)) < 0) {
4739b0b98119STakashi Iwai 		snd_printk(KERN_ERR "Hammerfall-DSP: Error creating pcm interface\n");
47401da177e4SLinus Torvalds 		return err;
47411da177e4SLinus Torvalds 	}
47421da177e4SLinus Torvalds 
47431da177e4SLinus Torvalds 
47441da177e4SLinus Torvalds 	if ((err = snd_hdsp_create_midi(card, hdsp, 0)) < 0) {
4745b0b98119STakashi Iwai 		snd_printk(KERN_ERR "Hammerfall-DSP: Error creating first midi interface\n");
47461da177e4SLinus Torvalds 		return err;
47471da177e4SLinus Torvalds 	}
47481da177e4SLinus Torvalds 
47491da177e4SLinus Torvalds 	if (hdsp->io_type == Digiface || hdsp->io_type == H9652) {
47501da177e4SLinus Torvalds 		if ((err = snd_hdsp_create_midi(card, hdsp, 1)) < 0) {
4751b0b98119STakashi Iwai 			snd_printk(KERN_ERR "Hammerfall-DSP: Error creating second midi interface\n");
47521da177e4SLinus Torvalds 			return err;
47531da177e4SLinus Torvalds 		}
47541da177e4SLinus Torvalds 	}
47551da177e4SLinus Torvalds 
47561da177e4SLinus Torvalds 	if ((err = snd_hdsp_create_controls(card, hdsp)) < 0) {
4757b0b98119STakashi Iwai 		snd_printk(KERN_ERR "Hammerfall-DSP: Error creating ctl interface\n");
47581da177e4SLinus Torvalds 		return err;
47591da177e4SLinus Torvalds 	}
47601da177e4SLinus Torvalds 
47611da177e4SLinus Torvalds 	snd_hdsp_proc_init(hdsp);
47621da177e4SLinus Torvalds 
47631da177e4SLinus Torvalds 	hdsp->system_sample_rate = -1;
47641da177e4SLinus Torvalds 	hdsp->playback_pid = -1;
47651da177e4SLinus Torvalds 	hdsp->capture_pid = -1;
47661da177e4SLinus Torvalds 	hdsp->capture_substream = NULL;
47671da177e4SLinus Torvalds 	hdsp->playback_substream = NULL;
47681da177e4SLinus Torvalds 
47691da177e4SLinus Torvalds 	if ((err = snd_hdsp_set_defaults(hdsp)) < 0) {
4770b0b98119STakashi Iwai 		snd_printk(KERN_ERR "Hammerfall-DSP: Error setting default values\n");
47711da177e4SLinus Torvalds 		return err;
47721da177e4SLinus Torvalds 	}
47731da177e4SLinus Torvalds 
47741da177e4SLinus Torvalds 	if (!(hdsp->state & HDSP_InitializationComplete)) {
4775b73c1c12SClemens Ladisch 		strcpy(card->shortname, "Hammerfall DSP");
47761da177e4SLinus Torvalds 		sprintf(card->longname, "%s at 0x%lx, irq %d", hdsp->card_name,
47771da177e4SLinus Torvalds 			hdsp->port, hdsp->irq);
47781da177e4SLinus Torvalds 
47791da177e4SLinus Torvalds 		if ((err = snd_card_register(card)) < 0) {
4780b0b98119STakashi Iwai 			snd_printk(KERN_ERR "Hammerfall-DSP: error registering card\n");
47811da177e4SLinus Torvalds 			return err;
47821da177e4SLinus Torvalds 		}
47831da177e4SLinus Torvalds 		hdsp->state |= HDSP_InitializationComplete;
47841da177e4SLinus Torvalds 	}
47851da177e4SLinus Torvalds 
47861da177e4SLinus Torvalds 	return 0;
47871da177e4SLinus Torvalds }
47881da177e4SLinus Torvalds 
47891da177e4SLinus Torvalds #ifdef HDSP_FW_LOADER
47901da177e4SLinus Torvalds /* load firmware via hotplug fw loader */
479155e957d8STakashi Iwai static int __devinit hdsp_request_fw_loader(struct hdsp *hdsp)
47921da177e4SLinus Torvalds {
47931da177e4SLinus Torvalds 	const char *fwfile;
47941da177e4SLinus Torvalds 	const struct firmware *fw;
47951da177e4SLinus Torvalds 	int err;
47961da177e4SLinus Torvalds 
47971da177e4SLinus Torvalds 	if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
47981da177e4SLinus Torvalds 		return 0;
47991da177e4SLinus Torvalds 	if (hdsp->io_type == Undefined) {
48001da177e4SLinus Torvalds 		if ((err = hdsp_get_iobox_version(hdsp)) < 0)
48011da177e4SLinus Torvalds 			return err;
48021da177e4SLinus Torvalds 		if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
48031da177e4SLinus Torvalds 			return 0;
48041da177e4SLinus Torvalds 	}
48051da177e4SLinus Torvalds 
48061da177e4SLinus Torvalds 	/* caution: max length of firmware filename is 30! */
48071da177e4SLinus Torvalds 	switch (hdsp->io_type) {
48081da177e4SLinus Torvalds 	case Multiface:
48091da177e4SLinus Torvalds 		if (hdsp->firmware_rev == 0xa)
48101da177e4SLinus Torvalds 			fwfile = "multiface_firmware.bin";
48111da177e4SLinus Torvalds 		else
48121da177e4SLinus Torvalds 			fwfile = "multiface_firmware_rev11.bin";
48131da177e4SLinus Torvalds 		break;
48141da177e4SLinus Torvalds 	case Digiface:
48151da177e4SLinus Torvalds 		if (hdsp->firmware_rev == 0xa)
48161da177e4SLinus Torvalds 			fwfile = "digiface_firmware.bin";
48171da177e4SLinus Torvalds 		else
48181da177e4SLinus Torvalds 			fwfile = "digiface_firmware_rev11.bin";
48191da177e4SLinus Torvalds 		break;
48201da177e4SLinus Torvalds 	default:
48211da177e4SLinus Torvalds 		snd_printk(KERN_ERR "Hammerfall-DSP: invalid io_type %d\n", hdsp->io_type);
48221da177e4SLinus Torvalds 		return -EINVAL;
48231da177e4SLinus Torvalds 	}
48241da177e4SLinus Torvalds 
48251da177e4SLinus Torvalds 	if (request_firmware(&fw, fwfile, &hdsp->pci->dev)) {
48261da177e4SLinus Torvalds 		snd_printk(KERN_ERR "Hammerfall-DSP: cannot load firmware %s\n", fwfile);
48271da177e4SLinus Torvalds 		return -ENOENT;
48281da177e4SLinus Torvalds 	}
48291da177e4SLinus Torvalds 	if (fw->size < sizeof(hdsp->firmware_cache)) {
48301da177e4SLinus Torvalds 		snd_printk(KERN_ERR "Hammerfall-DSP: too short firmware size %d (expected %d)\n",
48311da177e4SLinus Torvalds 			   (int)fw->size, (int)sizeof(hdsp->firmware_cache));
48321da177e4SLinus Torvalds 		release_firmware(fw);
48331da177e4SLinus Torvalds 		return -EINVAL;
48341da177e4SLinus Torvalds 	}
48357679a030SThomas Charbonnel 
48361da177e4SLinus Torvalds 	memcpy(hdsp->firmware_cache, fw->data, sizeof(hdsp->firmware_cache));
48377679a030SThomas Charbonnel 
48381da177e4SLinus Torvalds 	release_firmware(fw);
48391da177e4SLinus Torvalds 
48401da177e4SLinus Torvalds 	hdsp->state |= HDSP_FirmwareCached;
48411da177e4SLinus Torvalds 
48421da177e4SLinus Torvalds 	if ((err = snd_hdsp_load_firmware_from_cache(hdsp)) < 0)
48431da177e4SLinus Torvalds 		return err;
48441da177e4SLinus Torvalds 
48451da177e4SLinus Torvalds 	if (!(hdsp->state & HDSP_InitializationComplete)) {
4846b0b98119STakashi Iwai 		if ((err = snd_hdsp_enable_io(hdsp)) < 0)
48471da177e4SLinus Torvalds 			return err;
48481da177e4SLinus Torvalds 
48491da177e4SLinus Torvalds 		if ((err = snd_hdsp_create_hwdep(hdsp->card, hdsp)) < 0) {
4850b0b98119STakashi Iwai 			snd_printk(KERN_ERR "Hammerfall-DSP: error creating hwdep device\n");
48511da177e4SLinus Torvalds 			return err;
48521da177e4SLinus Torvalds 		}
48531da177e4SLinus Torvalds 		snd_hdsp_initialize_channels(hdsp);
48541da177e4SLinus Torvalds 		snd_hdsp_initialize_midi_flush(hdsp);
48551da177e4SLinus Torvalds 		if ((err = snd_hdsp_create_alsa_devices(hdsp->card, hdsp)) < 0) {
4856b0b98119STakashi Iwai 			snd_printk(KERN_ERR "Hammerfall-DSP: error creating alsa devices\n");
48571da177e4SLinus Torvalds 			return err;
48581da177e4SLinus Torvalds 		}
48591da177e4SLinus Torvalds 	}
48601da177e4SLinus Torvalds 	return 0;
48611da177e4SLinus Torvalds }
48621da177e4SLinus Torvalds #endif
48631da177e4SLinus Torvalds 
486455e957d8STakashi Iwai static int __devinit snd_hdsp_create(struct snd_card *card,
486555e957d8STakashi Iwai 				     struct hdsp *hdsp)
48661da177e4SLinus Torvalds {
48671da177e4SLinus Torvalds 	struct pci_dev *pci = hdsp->pci;
48681da177e4SLinus Torvalds 	int err;
48691da177e4SLinus Torvalds 	int is_9652 = 0;
48701da177e4SLinus Torvalds 	int is_9632 = 0;
48711da177e4SLinus Torvalds 
48721da177e4SLinus Torvalds 	hdsp->irq = -1;
48731da177e4SLinus Torvalds 	hdsp->state = 0;
48741da177e4SLinus Torvalds 	hdsp->midi[0].rmidi = NULL;
48751da177e4SLinus Torvalds 	hdsp->midi[1].rmidi = NULL;
48761da177e4SLinus Torvalds 	hdsp->midi[0].input = NULL;
48771da177e4SLinus Torvalds 	hdsp->midi[1].input = NULL;
48781da177e4SLinus Torvalds 	hdsp->midi[0].output = NULL;
48791da177e4SLinus Torvalds 	hdsp->midi[1].output = NULL;
48801da177e4SLinus Torvalds 	hdsp->midi[0].pending = 0;
48811da177e4SLinus Torvalds 	hdsp->midi[1].pending = 0;
48821da177e4SLinus Torvalds 	spin_lock_init(&hdsp->midi[0].lock);
48831da177e4SLinus Torvalds 	spin_lock_init(&hdsp->midi[1].lock);
48841da177e4SLinus Torvalds 	hdsp->iobase = NULL;
48851da177e4SLinus Torvalds 	hdsp->control_register = 0;
48861da177e4SLinus Torvalds 	hdsp->control2_register = 0;
48871da177e4SLinus Torvalds 	hdsp->io_type = Undefined;
48881da177e4SLinus Torvalds 	hdsp->max_channels = 26;
48891da177e4SLinus Torvalds 
48901da177e4SLinus Torvalds 	hdsp->card = card;
48911da177e4SLinus Torvalds 
48921da177e4SLinus Torvalds 	spin_lock_init(&hdsp->lock);
48931da177e4SLinus Torvalds 
48941da177e4SLinus Torvalds 	tasklet_init(&hdsp->midi_tasklet, hdsp_midi_tasklet, (unsigned long)hdsp);
48951da177e4SLinus Torvalds 
48961da177e4SLinus Torvalds 	pci_read_config_word(hdsp->pci, PCI_CLASS_REVISION, &hdsp->firmware_rev);
48971da177e4SLinus Torvalds 	hdsp->firmware_rev &= 0xff;
48981da177e4SLinus Torvalds 
48991da177e4SLinus Torvalds 	/* From Martin Bjoernsen :
49001da177e4SLinus Torvalds 	    "It is important that the card's latency timer register in
49011da177e4SLinus Torvalds 	    the PCI configuration space is set to a value much larger
49021da177e4SLinus Torvalds 	    than 0 by the computer's BIOS or the driver.
49031da177e4SLinus Torvalds 	    The windows driver always sets this 8 bit register [...]
49041da177e4SLinus Torvalds 	    to its maximum 255 to avoid problems with some computers."
49051da177e4SLinus Torvalds 	*/
49061da177e4SLinus Torvalds 	pci_write_config_byte(hdsp->pci, PCI_LATENCY_TIMER, 0xFF);
49071da177e4SLinus Torvalds 
49081da177e4SLinus Torvalds 	strcpy(card->driver, "H-DSP");
49091da177e4SLinus Torvalds 	strcpy(card->mixername, "Xilinx FPGA");
49101da177e4SLinus Torvalds 
4911b0b98119STakashi Iwai 	if (hdsp->firmware_rev < 0xa)
49121da177e4SLinus Torvalds 		return -ENODEV;
4913b0b98119STakashi Iwai 	else if (hdsp->firmware_rev < 0x64)
49141da177e4SLinus Torvalds 		hdsp->card_name = "RME Hammerfall DSP";
4915b0b98119STakashi Iwai 	else if (hdsp->firmware_rev < 0x96) {
49161da177e4SLinus Torvalds 		hdsp->card_name = "RME HDSP 9652";
49171da177e4SLinus Torvalds 		is_9652 = 1;
49181da177e4SLinus Torvalds 	} else {
49191da177e4SLinus Torvalds 		hdsp->card_name = "RME HDSP 9632";
49201da177e4SLinus Torvalds 		hdsp->max_channels = 16;
49211da177e4SLinus Torvalds 		is_9632 = 1;
49221da177e4SLinus Torvalds 	}
49231da177e4SLinus Torvalds 
4924b0b98119STakashi Iwai 	if ((err = pci_enable_device(pci)) < 0)
49251da177e4SLinus Torvalds 		return err;
49261da177e4SLinus Torvalds 
49271da177e4SLinus Torvalds 	pci_set_master(hdsp->pci);
49281da177e4SLinus Torvalds 
49291da177e4SLinus Torvalds 	if ((err = pci_request_regions(pci, "hdsp")) < 0)
49301da177e4SLinus Torvalds 		return err;
49311da177e4SLinus Torvalds 	hdsp->port = pci_resource_start(pci, 0);
49321da177e4SLinus Torvalds 	if ((hdsp->iobase = ioremap_nocache(hdsp->port, HDSP_IO_EXTENT)) == NULL) {
4933b0b98119STakashi Iwai 		snd_printk(KERN_ERR "Hammerfall-DSP: unable to remap region 0x%lx-0x%lx\n", hdsp->port, hdsp->port + HDSP_IO_EXTENT - 1);
49341da177e4SLinus Torvalds 		return -EBUSY;
49351da177e4SLinus Torvalds 	}
49361da177e4SLinus Torvalds 
4937*437a5a46STakashi Iwai 	if (request_irq(pci->irq, snd_hdsp_interrupt, IRQF_SHARED,
4938*437a5a46STakashi Iwai 			"hdsp", hdsp)) {
4939b0b98119STakashi Iwai 		snd_printk(KERN_ERR "Hammerfall-DSP: unable to use IRQ %d\n", pci->irq);
49401da177e4SLinus Torvalds 		return -EBUSY;
49411da177e4SLinus Torvalds 	}
49421da177e4SLinus Torvalds 
49431da177e4SLinus Torvalds 	hdsp->irq = pci->irq;
4944176546abSRemy Bruno 	hdsp->precise_ptr = 0;
49451da177e4SLinus Torvalds 	hdsp->use_midi_tasklet = 1;
49461da177e4SLinus Torvalds 
4947b0b98119STakashi Iwai 	if ((err = snd_hdsp_initialize_memory(hdsp)) < 0)
49481da177e4SLinus Torvalds 		return err;
49491da177e4SLinus Torvalds 
49501da177e4SLinus Torvalds 	if (!is_9652 && !is_9632) {
49511da177e4SLinus Torvalds 		/* we wait 2 seconds to let freshly inserted cardbus cards do their hardware init */
4952ef21ca24SNishanth Aravamudan 		ssleep(2);
49531da177e4SLinus Torvalds 
49541da177e4SLinus Torvalds 		if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
49551da177e4SLinus Torvalds #ifdef HDSP_FW_LOADER
4956b0b98119STakashi Iwai 			if ((err = hdsp_request_fw_loader(hdsp)) < 0)
49571da177e4SLinus Torvalds 				/* we don't fail as this can happen
49581da177e4SLinus Torvalds 				   if userspace is not ready for
49591da177e4SLinus Torvalds 				   firmware upload
49601da177e4SLinus Torvalds 				*/
4961b0b98119STakashi Iwai 				snd_printk(KERN_ERR "Hammerfall-DSP: couldn't get firmware from userspace. try using hdsploader\n");
4962b0b98119STakashi Iwai 			else
49631da177e4SLinus Torvalds 				/* init is complete, we return */
49641da177e4SLinus Torvalds 				return 0;
49651da177e4SLinus Torvalds #endif
49661da177e4SLinus Torvalds 			/* no iobox connected, we defer initialization */
4967b0b98119STakashi Iwai 			snd_printk(KERN_INFO "Hammerfall-DSP: card initialization pending : waiting for firmware\n");
4968b0b98119STakashi Iwai 			if ((err = snd_hdsp_create_hwdep(card, hdsp)) < 0)
49691da177e4SLinus Torvalds 				return err;
49701da177e4SLinus Torvalds 			return 0;
49711da177e4SLinus Torvalds 		} else {
4972b0b98119STakashi Iwai 			snd_printk(KERN_INFO "Hammerfall-DSP: Firmware already present, initializing card.\n");
4973b0b98119STakashi Iwai 			if (hdsp_read(hdsp, HDSP_status2Register) & HDSP_version1)
49741da177e4SLinus Torvalds 				hdsp->io_type = Multiface;
4975b0b98119STakashi Iwai 			else
49761da177e4SLinus Torvalds 				hdsp->io_type = Digiface;
49771da177e4SLinus Torvalds 		}
49781da177e4SLinus Torvalds 	}
49791da177e4SLinus Torvalds 
4980b0b98119STakashi Iwai 	if ((err = snd_hdsp_enable_io(hdsp)) != 0)
49811da177e4SLinus Torvalds 		return err;
49821da177e4SLinus Torvalds 
4983b0b98119STakashi Iwai 	if (is_9652)
49841da177e4SLinus Torvalds 	        hdsp->io_type = H9652;
49851da177e4SLinus Torvalds 
4986b0b98119STakashi Iwai 	if (is_9632)
49871da177e4SLinus Torvalds 		hdsp->io_type = H9632;
49881da177e4SLinus Torvalds 
4989b0b98119STakashi Iwai 	if ((err = snd_hdsp_create_hwdep(card, hdsp)) < 0)
49901da177e4SLinus Torvalds 		return err;
49911da177e4SLinus Torvalds 
49921da177e4SLinus Torvalds 	snd_hdsp_initialize_channels(hdsp);
49931da177e4SLinus Torvalds 	snd_hdsp_initialize_midi_flush(hdsp);
49941da177e4SLinus Torvalds 
49951da177e4SLinus Torvalds 	hdsp->state |= HDSP_FirmwareLoaded;
49961da177e4SLinus Torvalds 
4997b0b98119STakashi Iwai 	if ((err = snd_hdsp_create_alsa_devices(card, hdsp)) < 0)
49981da177e4SLinus Torvalds 		return err;
49991da177e4SLinus Torvalds 
50001da177e4SLinus Torvalds 	return 0;
50011da177e4SLinus Torvalds }
50021da177e4SLinus Torvalds 
500355e957d8STakashi Iwai static int snd_hdsp_free(struct hdsp *hdsp)
50041da177e4SLinus Torvalds {
50051da177e4SLinus Torvalds 	if (hdsp->port) {
50061da177e4SLinus Torvalds 		/* stop the audio, and cancel all interrupts */
50071da177e4SLinus Torvalds 		tasklet_kill(&hdsp->midi_tasklet);
50081da177e4SLinus Torvalds 		hdsp->control_register &= ~(HDSP_Start|HDSP_AudioInterruptEnable|HDSP_Midi0InterruptEnable|HDSP_Midi1InterruptEnable);
50091da177e4SLinus Torvalds 		hdsp_write (hdsp, HDSP_controlRegister, hdsp->control_register);
50101da177e4SLinus Torvalds 	}
50111da177e4SLinus Torvalds 
50121da177e4SLinus Torvalds 	if (hdsp->irq >= 0)
50131da177e4SLinus Torvalds 		free_irq(hdsp->irq, (void *)hdsp);
50141da177e4SLinus Torvalds 
50151da177e4SLinus Torvalds 	snd_hdsp_free_buffers(hdsp);
50161da177e4SLinus Torvalds 
50171da177e4SLinus Torvalds 	if (hdsp->iobase)
50181da177e4SLinus Torvalds 		iounmap(hdsp->iobase);
50191da177e4SLinus Torvalds 
50201da177e4SLinus Torvalds 	if (hdsp->port)
50211da177e4SLinus Torvalds 		pci_release_regions(hdsp->pci);
50221da177e4SLinus Torvalds 
50231da177e4SLinus Torvalds 	pci_disable_device(hdsp->pci);
50241da177e4SLinus Torvalds 	return 0;
50251da177e4SLinus Torvalds }
50261da177e4SLinus Torvalds 
502755e957d8STakashi Iwai static void snd_hdsp_card_free(struct snd_card *card)
50281da177e4SLinus Torvalds {
502955e957d8STakashi Iwai 	struct hdsp *hdsp = (struct hdsp *) card->private_data;
50301da177e4SLinus Torvalds 
50311da177e4SLinus Torvalds 	if (hdsp)
50321da177e4SLinus Torvalds 		snd_hdsp_free(hdsp);
50331da177e4SLinus Torvalds }
50341da177e4SLinus Torvalds 
50351da177e4SLinus Torvalds static int __devinit snd_hdsp_probe(struct pci_dev *pci,
50361da177e4SLinus Torvalds 				    const struct pci_device_id *pci_id)
50371da177e4SLinus Torvalds {
50381da177e4SLinus Torvalds 	static int dev;
503955e957d8STakashi Iwai 	struct hdsp *hdsp;
504055e957d8STakashi Iwai 	struct snd_card *card;
50411da177e4SLinus Torvalds 	int err;
50421da177e4SLinus Torvalds 
50431da177e4SLinus Torvalds 	if (dev >= SNDRV_CARDS)
50441da177e4SLinus Torvalds 		return -ENODEV;
50451da177e4SLinus Torvalds 	if (!enable[dev]) {
50461da177e4SLinus Torvalds 		dev++;
50471da177e4SLinus Torvalds 		return -ENOENT;
50481da177e4SLinus Torvalds 	}
50491da177e4SLinus Torvalds 
505055e957d8STakashi Iwai 	if (!(card = snd_card_new(index[dev], id[dev], THIS_MODULE, sizeof(struct hdsp))))
50511da177e4SLinus Torvalds 		return -ENOMEM;
50521da177e4SLinus Torvalds 
505355e957d8STakashi Iwai 	hdsp = (struct hdsp *) card->private_data;
50541da177e4SLinus Torvalds 	card->private_free = snd_hdsp_card_free;
50551da177e4SLinus Torvalds 	hdsp->dev = dev;
50561da177e4SLinus Torvalds 	hdsp->pci = pci;
50571da177e4SLinus Torvalds 	snd_card_set_dev(card, &pci->dev);
50581da177e4SLinus Torvalds 
50591da177e4SLinus Torvalds 	if ((err = snd_hdsp_create(card, hdsp)) < 0) {
50601da177e4SLinus Torvalds 		snd_card_free(card);
50611da177e4SLinus Torvalds 		return err;
50621da177e4SLinus Torvalds 	}
50631da177e4SLinus Torvalds 
50641da177e4SLinus Torvalds 	strcpy(card->shortname, "Hammerfall DSP");
50651da177e4SLinus Torvalds 	sprintf(card->longname, "%s at 0x%lx, irq %d", hdsp->card_name,
50661da177e4SLinus Torvalds 		hdsp->port, hdsp->irq);
50671da177e4SLinus Torvalds 
50681da177e4SLinus Torvalds 	if ((err = snd_card_register(card)) < 0) {
50691da177e4SLinus Torvalds 		snd_card_free(card);
50701da177e4SLinus Torvalds 		return err;
50711da177e4SLinus Torvalds 	}
50721da177e4SLinus Torvalds 	pci_set_drvdata(pci, card);
50731da177e4SLinus Torvalds 	dev++;
50741da177e4SLinus Torvalds 	return 0;
50751da177e4SLinus Torvalds }
50761da177e4SLinus Torvalds 
50771da177e4SLinus Torvalds static void __devexit snd_hdsp_remove(struct pci_dev *pci)
50781da177e4SLinus Torvalds {
50791da177e4SLinus Torvalds 	snd_card_free(pci_get_drvdata(pci));
50801da177e4SLinus Torvalds 	pci_set_drvdata(pci, NULL);
50811da177e4SLinus Torvalds }
50821da177e4SLinus Torvalds 
50831da177e4SLinus Torvalds static struct pci_driver driver = {
50841da177e4SLinus Torvalds 	.name =     "RME Hammerfall DSP",
50851da177e4SLinus Torvalds 	.id_table = snd_hdsp_ids,
50861da177e4SLinus Torvalds 	.probe =    snd_hdsp_probe,
50871da177e4SLinus Torvalds 	.remove = __devexit_p(snd_hdsp_remove),
50881da177e4SLinus Torvalds };
50891da177e4SLinus Torvalds 
50901da177e4SLinus Torvalds static int __init alsa_card_hdsp_init(void)
50911da177e4SLinus Torvalds {
509201d25d46STakashi Iwai 	return pci_register_driver(&driver);
50931da177e4SLinus Torvalds }
50941da177e4SLinus Torvalds 
50951da177e4SLinus Torvalds static void __exit alsa_card_hdsp_exit(void)
50961da177e4SLinus Torvalds {
50971da177e4SLinus Torvalds 	pci_unregister_driver(&driver);
50981da177e4SLinus Torvalds }
50991da177e4SLinus Torvalds 
51001da177e4SLinus Torvalds module_init(alsa_card_hdsp_init)
51011da177e4SLinus Torvalds module_exit(alsa_card_hdsp_exit)
5102