hdsp.c 147 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds's avatar
Linus Torvalds committed
2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 *   ALSA driver for RME Hammerfall DSP audio interface(s)
 *
 *      Copyright (c) 2002  Paul Davis
 *                          Marcus Andersson
 *                          Thomas Charbonnel
 */

#include <linux/init.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
#include <linux/pci.h>
#include <linux/firmware.h>
15
#include <linux/module.h>
16
#include <linux/math64.h>
17
#include <linux/vmalloc.h>
18
#include <linux/io.h>
19
#include <linux/nospec.h>
Linus Torvalds's avatar
Linus Torvalds committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

#include <sound/core.h>
#include <sound/control.h>
#include <sound/pcm.h>
#include <sound/info.h>
#include <sound/asoundef.h>
#include <sound/rawmidi.h>
#include <sound/hwdep.h>
#include <sound/initval.h>
#include <sound/hdsp.h>

#include <asm/byteorder.h>
#include <asm/current.h>

static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-MAX */
static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
36
static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;	/* Enable this card */
Linus Torvalds's avatar
Linus Torvalds committed
37 38 39 40 41 42 43 44 45 46 47 48 49

module_param_array(index, int, NULL, 0444);
MODULE_PARM_DESC(index, "Index value for RME Hammerfall DSP interface.");
module_param_array(id, charp, NULL, 0444);
MODULE_PARM_DESC(id, "ID string for RME Hammerfall DSP interface.");
module_param_array(enable, bool, NULL, 0444);
MODULE_PARM_DESC(enable, "Enable/disable specific Hammerfall DSP soundcards.");
MODULE_AUTHOR("Paul Davis <paul@linuxaudiosystems.com>, Marcus Andersson, Thomas Charbonnel <thomas@undata.org>");
MODULE_DESCRIPTION("RME Hammerfall DSP");
MODULE_LICENSE("GPL");
MODULE_SUPPORTED_DEVICE("{{RME Hammerfall-DSP},"
	        "{RME HDSP-9652},"
		"{RME HDSP-9632}}");
50
MODULE_FIRMWARE("rpm_firmware.bin");
51 52 53 54
MODULE_FIRMWARE("multiface_firmware.bin");
MODULE_FIRMWARE("multiface_firmware_rev11.bin");
MODULE_FIRMWARE("digiface_firmware.bin");
MODULE_FIRMWARE("digiface_firmware_rev11.bin");
Linus Torvalds's avatar
Linus Torvalds committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

#define HDSP_MAX_CHANNELS        26
#define HDSP_MAX_DS_CHANNELS     14
#define HDSP_MAX_QS_CHANNELS     8
#define DIGIFACE_SS_CHANNELS     26
#define DIGIFACE_DS_CHANNELS     14
#define MULTIFACE_SS_CHANNELS    18
#define MULTIFACE_DS_CHANNELS    14
#define H9652_SS_CHANNELS        26
#define H9652_DS_CHANNELS        14
/* This does not include possible Analog Extension Boards
   AEBs are detected at card initialization
*/
#define H9632_SS_CHANNELS	 12
#define H9632_DS_CHANNELS	 8
#define H9632_QS_CHANNELS	 4
71
#define RPM_CHANNELS             6
Linus Torvalds's avatar
Linus Torvalds committed
72 73 74 75

/* Write registers. These are defined as byte-offsets from the iobase value.
 */
#define HDSP_resetPointer               0
76
#define HDSP_freqReg			0
Linus Torvalds's avatar
Linus Torvalds committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
#define HDSP_outputBufferAddress	32
#define HDSP_inputBufferAddress		36
#define HDSP_controlRegister		64
#define HDSP_interruptConfirmation	96
#define HDSP_outputEnable	  	128
#define HDSP_control2Reg		256
#define HDSP_midiDataOut0  		352
#define HDSP_midiDataOut1  		356
#define HDSP_fifoData  			368
#define HDSP_inputEnable	 	384

/* Read registers. These are defined as byte-offsets from the iobase value
 */

#define HDSP_statusRegister    0
#define HDSP_timecode        128
#define HDSP_status2Register 192
#define HDSP_midiDataIn0     360
#define HDSP_midiDataIn1     364
#define HDSP_midiStatusOut0  384
#define HDSP_midiStatusOut1  388
#define HDSP_midiStatusIn0   392
#define HDSP_midiStatusIn1   396
#define HDSP_fifoStatus      400

/* the meters are regular i/o-mapped registers, but offset
   considerably from the rest. the peak registers are reset
104
   when read; the least-significant 4 bits are full-scale counters;
Linus Torvalds's avatar
Linus Torvalds committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
   the actual peak value is in the most-significant 24 bits.
*/

#define HDSP_playbackPeakLevel  4096  /* 26 * 32 bit values */
#define HDSP_inputPeakLevel     4224  /* 26 * 32 bit values */
#define HDSP_outputPeakLevel    4352  /* (26+2) * 32 bit values */
#define HDSP_playbackRmsLevel   4612  /* 26 * 64 bit values */
#define HDSP_inputRmsLevel      4868  /* 26 * 64 bit values */


/* This is for H9652 cards
   Peak values are read downward from the base
   Rms values are read upward
   There are rms values for the outputs too
   26*3 values are read in ss mode
   14*3 in ds mode, with no gap between values
*/
122
#define HDSP_9652_peakBase	7164
Linus Torvalds's avatar
Linus Torvalds committed
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
#define HDSP_9652_rmsBase	4096

/* c.f. the hdsp_9632_meters_t struct */
#define HDSP_9632_metersBase	4096

#define HDSP_IO_EXTENT     7168

/* control2 register bits */

#define HDSP_TMS                0x01
#define HDSP_TCK                0x02
#define HDSP_TDI                0x04
#define HDSP_JTAG               0x08
#define HDSP_PWDN               0x10
#define HDSP_PROGRAM	        0x020
#define HDSP_CONFIG_MODE_0	0x040
#define HDSP_CONFIG_MODE_1	0x080
140
#define HDSP_VERSION_BIT	(0x100 | HDSP_S_LOAD)
Linus Torvalds's avatar
Linus Torvalds committed
141 142 143
#define HDSP_BIGENDIAN_MODE     0x200
#define HDSP_RD_MULTIPLE        0x400
#define HDSP_9652_ENABLE_MIXER  0x800
144 145 146
#define HDSP_S200		0x800
#define HDSP_S300		(0x100 | HDSP_S200) /* dummy, purpose of 0x100 unknown */
#define HDSP_CYCLIC_MODE	0x1000
Linus Torvalds's avatar
Linus Torvalds committed
147 148
#define HDSP_TDO                0x10000000

149 150
#define HDSP_S_PROGRAM	    (HDSP_CYCLIC_MODE|HDSP_PROGRAM|HDSP_CONFIG_MODE_0)
#define HDSP_S_LOAD	    (HDSP_CYCLIC_MODE|HDSP_PROGRAM|HDSP_CONFIG_MODE_1)
Linus Torvalds's avatar
Linus Torvalds committed
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166

/* Control Register bits */

#define HDSP_Start                (1<<0)  /* start engine */
#define HDSP_Latency0             (1<<1)  /* buffer size = 2^n where n is defined by Latency{2,1,0} */
#define HDSP_Latency1             (1<<2)  /* [ see above ] */
#define HDSP_Latency2             (1<<3)  /* [ see above ] */
#define HDSP_ClockModeMaster      (1<<4)  /* 1=Master, 0=Slave/Autosync */
#define HDSP_AudioInterruptEnable (1<<5)  /* what do you think ? */
#define HDSP_Frequency0           (1<<6)  /* 0=44.1kHz/88.2kHz/176.4kHz 1=48kHz/96kHz/192kHz */
#define HDSP_Frequency1           (1<<7)  /* 0=32kHz/64kHz/128kHz */
#define HDSP_DoubleSpeed          (1<<8)  /* 0=normal speed, 1=double speed */
#define HDSP_SPDIFProfessional    (1<<9)  /* 0=consumer, 1=professional */
#define HDSP_SPDIFEmphasis        (1<<10) /* 0=none, 1=on */
#define HDSP_SPDIFNonAudio        (1<<11) /* 0=off, 1=on */
#define HDSP_SPDIFOpticalOut      (1<<12) /* 1=use 1st ADAT connector for SPDIF, 0=do not */
167 168 169 170
#define HDSP_SyncRef2             (1<<13)
#define HDSP_SPDIFInputSelect0    (1<<14)
#define HDSP_SPDIFInputSelect1    (1<<15)
#define HDSP_SyncRef0             (1<<16)
Linus Torvalds's avatar
Linus Torvalds committed
171
#define HDSP_SyncRef1             (1<<17)
172
#define HDSP_AnalogExtensionBoard (1<<18) /* For H9632 cards */
Linus Torvalds's avatar
Linus Torvalds committed
173 174 175 176 177 178 179 180 181 182 183 184
#define HDSP_XLRBreakoutCable     (1<<20) /* For H9632 cards */
#define HDSP_Midi0InterruptEnable (1<<22)
#define HDSP_Midi1InterruptEnable (1<<23)
#define HDSP_LineOut              (1<<24)
#define HDSP_ADGain0		  (1<<25) /* From here : H9632 specific */
#define HDSP_ADGain1		  (1<<26)
#define HDSP_DAGain0		  (1<<27)
#define HDSP_DAGain1		  (1<<28)
#define HDSP_PhoneGain0		  (1<<29)
#define HDSP_PhoneGain1		  (1<<30)
#define HDSP_QuadSpeed	  	  (1<<31)

185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
/* RPM uses some of the registers for special purposes */
#define HDSP_RPM_Inp12            0x04A00
#define HDSP_RPM_Inp12_Phon_6dB   0x00800  /* Dolby */
#define HDSP_RPM_Inp12_Phon_0dB   0x00000  /* .. */
#define HDSP_RPM_Inp12_Phon_n6dB  0x04000  /* inp_0 */
#define HDSP_RPM_Inp12_Line_0dB   0x04200  /* Dolby+PRO */
#define HDSP_RPM_Inp12_Line_n6dB  0x00200  /* PRO */

#define HDSP_RPM_Inp34            0x32000
#define HDSP_RPM_Inp34_Phon_6dB   0x20000  /* SyncRef1 */
#define HDSP_RPM_Inp34_Phon_0dB   0x00000  /* .. */
#define HDSP_RPM_Inp34_Phon_n6dB  0x02000  /* SyncRef2 */
#define HDSP_RPM_Inp34_Line_0dB   0x30000  /* SyncRef1+SyncRef0 */
#define HDSP_RPM_Inp34_Line_n6dB  0x10000  /* SyncRef0 */

#define HDSP_RPM_Bypass           0x01000

#define HDSP_RPM_Disconnect       0x00001

Linus Torvalds's avatar
Linus Torvalds committed
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
#define HDSP_ADGainMask       (HDSP_ADGain0|HDSP_ADGain1)
#define HDSP_ADGainMinus10dBV  HDSP_ADGainMask
#define HDSP_ADGainPlus4dBu   (HDSP_ADGain0)
#define HDSP_ADGainLowGain     0

#define HDSP_DAGainMask         (HDSP_DAGain0|HDSP_DAGain1)
#define HDSP_DAGainHighGain      HDSP_DAGainMask
#define HDSP_DAGainPlus4dBu     (HDSP_DAGain0)
#define HDSP_DAGainMinus10dBV    0

#define HDSP_PhoneGainMask      (HDSP_PhoneGain0|HDSP_PhoneGain1)
#define HDSP_PhoneGain0dB        HDSP_PhoneGainMask
#define HDSP_PhoneGainMinus6dB  (HDSP_PhoneGain0)
#define HDSP_PhoneGainMinus12dB  0

#define HDSP_LatencyMask    (HDSP_Latency0|HDSP_Latency1|HDSP_Latency2)
#define HDSP_FrequencyMask  (HDSP_Frequency0|HDSP_Frequency1|HDSP_DoubleSpeed|HDSP_QuadSpeed)

#define HDSP_SPDIFInputMask    (HDSP_SPDIFInputSelect0|HDSP_SPDIFInputSelect1)
#define HDSP_SPDIFInputADAT1    0
#define HDSP_SPDIFInputCoaxial (HDSP_SPDIFInputSelect0)
#define HDSP_SPDIFInputCdrom   (HDSP_SPDIFInputSelect1)
#define HDSP_SPDIFInputAES     (HDSP_SPDIFInputSelect0|HDSP_SPDIFInputSelect1)

#define HDSP_SyncRefMask        (HDSP_SyncRef0|HDSP_SyncRef1|HDSP_SyncRef2)
#define HDSP_SyncRef_ADAT1       0
#define HDSP_SyncRef_ADAT2      (HDSP_SyncRef0)
#define HDSP_SyncRef_ADAT3      (HDSP_SyncRef1)
#define HDSP_SyncRef_SPDIF      (HDSP_SyncRef0|HDSP_SyncRef1)
#define HDSP_SyncRef_WORD       (HDSP_SyncRef2)
#define HDSP_SyncRef_ADAT_SYNC  (HDSP_SyncRef0|HDSP_SyncRef2)

/* Sample Clock Sources */

#define HDSP_CLOCK_SOURCE_AUTOSYNC           0
#define HDSP_CLOCK_SOURCE_INTERNAL_32KHZ     1
#define HDSP_CLOCK_SOURCE_INTERNAL_44_1KHZ   2
#define HDSP_CLOCK_SOURCE_INTERNAL_48KHZ     3
#define HDSP_CLOCK_SOURCE_INTERNAL_64KHZ     4
#define HDSP_CLOCK_SOURCE_INTERNAL_88_2KHZ   5
#define HDSP_CLOCK_SOURCE_INTERNAL_96KHZ     6
#define HDSP_CLOCK_SOURCE_INTERNAL_128KHZ    7
#define HDSP_CLOCK_SOURCE_INTERNAL_176_4KHZ  8
#define HDSP_CLOCK_SOURCE_INTERNAL_192KHZ    9

/* Preferred sync reference choices - used by "pref_sync_ref" control switch */

#define HDSP_SYNC_FROM_WORD      0
#define HDSP_SYNC_FROM_SPDIF     1
#define HDSP_SYNC_FROM_ADAT1     2
#define HDSP_SYNC_FROM_ADAT_SYNC 3
#define HDSP_SYNC_FROM_ADAT2     4
#define HDSP_SYNC_FROM_ADAT3     5

/* SyncCheck status */

#define HDSP_SYNC_CHECK_NO_LOCK 0
#define HDSP_SYNC_CHECK_LOCK    1
#define HDSP_SYNC_CHECK_SYNC	2

/* AutoSync references - used by "autosync_ref" control switch */

#define HDSP_AUTOSYNC_FROM_WORD      0
#define HDSP_AUTOSYNC_FROM_ADAT_SYNC 1
#define HDSP_AUTOSYNC_FROM_SPDIF     2
#define HDSP_AUTOSYNC_FROM_NONE	     3
#define HDSP_AUTOSYNC_FROM_ADAT1     4
#define HDSP_AUTOSYNC_FROM_ADAT2     5
#define HDSP_AUTOSYNC_FROM_ADAT3     6

/* Possible sources of S/PDIF input */

#define HDSP_SPDIFIN_OPTICAL  0	/* optical  (ADAT1) */
#define HDSP_SPDIFIN_COAXIAL  1	/* coaxial (RCA) */
#define HDSP_SPDIFIN_INTERNAL 2	/* internal (CDROM) */
#define HDSP_SPDIFIN_AES      3 /* xlr for H9632 (AES)*/

#define HDSP_Frequency32KHz    HDSP_Frequency0
#define HDSP_Frequency44_1KHz  HDSP_Frequency1
#define HDSP_Frequency48KHz    (HDSP_Frequency1|HDSP_Frequency0)
#define HDSP_Frequency64KHz    (HDSP_DoubleSpeed|HDSP_Frequency0)
#define HDSP_Frequency88_2KHz  (HDSP_DoubleSpeed|HDSP_Frequency1)
#define HDSP_Frequency96KHz    (HDSP_DoubleSpeed|HDSP_Frequency1|HDSP_Frequency0)
/* For H9632 cards */
#define HDSP_Frequency128KHz   (HDSP_QuadSpeed|HDSP_DoubleSpeed|HDSP_Frequency0)
#define HDSP_Frequency176_4KHz (HDSP_QuadSpeed|HDSP_DoubleSpeed|HDSP_Frequency1)
#define HDSP_Frequency192KHz   (HDSP_QuadSpeed|HDSP_DoubleSpeed|HDSP_Frequency1|HDSP_Frequency0)
291 292 293 294 295
/* RME says n = 104857600000000, but in the windows MADI driver, I see:
	return 104857600000000 / rate; // 100 MHz
	return 110100480000000 / rate; // 105 MHz
*/
#define DDS_NUMERATOR 104857600000000ULL;  /*  =  2^20 * 10^8 */
Linus Torvalds's avatar
Linus Torvalds committed
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326

#define hdsp_encode_latency(x)       (((x)<<1) & HDSP_LatencyMask)
#define hdsp_decode_latency(x)       (((x) & HDSP_LatencyMask)>>1)

#define hdsp_encode_spdif_in(x) (((x)&0x3)<<14)
#define hdsp_decode_spdif_in(x) (((x)>>14)&0x3)

/* Status Register bits */

#define HDSP_audioIRQPending    (1<<0)
#define HDSP_Lock2              (1<<1)     /* this is for Digiface and H9652 */
#define HDSP_spdifFrequency3	HDSP_Lock2 /* this is for H9632 only */
#define HDSP_Lock1              (1<<2)
#define HDSP_Lock0              (1<<3)
#define HDSP_SPDIFSync          (1<<4)
#define HDSP_TimecodeLock       (1<<5)
#define HDSP_BufferPositionMask 0x000FFC0 /* Bit 6..15 : h/w buffer pointer */
#define HDSP_Sync2              (1<<16)
#define HDSP_Sync1              (1<<17)
#define HDSP_Sync0              (1<<18)
#define HDSP_DoubleSpeedStatus  (1<<19)
#define HDSP_ConfigError        (1<<20)
#define HDSP_DllError           (1<<21)
#define HDSP_spdifFrequency0    (1<<22)
#define HDSP_spdifFrequency1    (1<<23)
#define HDSP_spdifFrequency2    (1<<24)
#define HDSP_SPDIFErrorFlag     (1<<25)
#define HDSP_BufferID           (1<<26)
#define HDSP_TimecodeSync       (1<<27)
#define HDSP_AEBO          	(1<<28) /* H9632 specific Analog Extension Boards */
#define HDSP_AEBI		(1<<29) /* 0 = present, 1 = absent */
327
#define HDSP_midi0IRQPending    (1<<30)
Linus Torvalds's avatar
Linus Torvalds committed
328 329 330
#define HDSP_midi1IRQPending    (1<<31)

#define HDSP_spdifFrequencyMask    (HDSP_spdifFrequency0|HDSP_spdifFrequency1|HDSP_spdifFrequency2)
331 332 333 334
#define HDSP_spdifFrequencyMask_9632 (HDSP_spdifFrequency0|\
				      HDSP_spdifFrequency1|\
				      HDSP_spdifFrequency2|\
				      HDSP_spdifFrequency3)
Linus Torvalds's avatar
Linus Torvalds committed
335 336 337 338 339 340 341 342 343 344

#define HDSP_spdifFrequency32KHz   (HDSP_spdifFrequency0)
#define HDSP_spdifFrequency44_1KHz (HDSP_spdifFrequency1)
#define HDSP_spdifFrequency48KHz   (HDSP_spdifFrequency0|HDSP_spdifFrequency1)

#define HDSP_spdifFrequency64KHz   (HDSP_spdifFrequency2)
#define HDSP_spdifFrequency88_2KHz (HDSP_spdifFrequency0|HDSP_spdifFrequency2)
#define HDSP_spdifFrequency96KHz   (HDSP_spdifFrequency2|HDSP_spdifFrequency1)

/* This is for H9632 cards */
345 346 347
#define HDSP_spdifFrequency128KHz   (HDSP_spdifFrequency0|\
				     HDSP_spdifFrequency1|\
				     HDSP_spdifFrequency2)
Linus Torvalds's avatar
Linus Torvalds committed
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
#define HDSP_spdifFrequency176_4KHz HDSP_spdifFrequency3
#define HDSP_spdifFrequency192KHz   (HDSP_spdifFrequency3|HDSP_spdifFrequency0)

/* Status2 Register bits */

#define HDSP_version0     (1<<0)
#define HDSP_version1     (1<<1)
#define HDSP_version2     (1<<2)
#define HDSP_wc_lock      (1<<3)
#define HDSP_wc_sync      (1<<4)
#define HDSP_inp_freq0    (1<<5)
#define HDSP_inp_freq1    (1<<6)
#define HDSP_inp_freq2    (1<<7)
#define HDSP_SelSyncRef0  (1<<8)
#define HDSP_SelSyncRef1  (1<<9)
#define HDSP_SelSyncRef2  (1<<10)

#define HDSP_wc_valid (HDSP_wc_lock|HDSP_wc_sync)

#define HDSP_systemFrequencyMask (HDSP_inp_freq0|HDSP_inp_freq1|HDSP_inp_freq2)
#define HDSP_systemFrequency32   (HDSP_inp_freq0)
#define HDSP_systemFrequency44_1 (HDSP_inp_freq1)
#define HDSP_systemFrequency48   (HDSP_inp_freq0|HDSP_inp_freq1)
#define HDSP_systemFrequency64   (HDSP_inp_freq2)
#define HDSP_systemFrequency88_2 (HDSP_inp_freq0|HDSP_inp_freq2)
#define HDSP_systemFrequency96   (HDSP_inp_freq1|HDSP_inp_freq2)
/* FIXME : more values for 9632 cards ? */

#define HDSP_SelSyncRefMask        (HDSP_SelSyncRef0|HDSP_SelSyncRef1|HDSP_SelSyncRef2)
#define HDSP_SelSyncRef_ADAT1      0
#define HDSP_SelSyncRef_ADAT2      (HDSP_SelSyncRef0)
#define HDSP_SelSyncRef_ADAT3      (HDSP_SelSyncRef1)
#define HDSP_SelSyncRef_SPDIF      (HDSP_SelSyncRef0|HDSP_SelSyncRef1)
#define HDSP_SelSyncRef_WORD       (HDSP_SelSyncRef2)
#define HDSP_SelSyncRef_ADAT_SYNC  (HDSP_SelSyncRef0|HDSP_SelSyncRef2)

/* Card state flags */

#define HDSP_InitializationComplete  (1<<0)
#define HDSP_FirmwareLoaded	     (1<<1)
#define HDSP_FirmwareCached	     (1<<2)

/* FIFO wait times, defined in terms of 1/10ths of msecs */

#define HDSP_LONG_WAIT	 5000
#define HDSP_SHORT_WAIT  30

#define UNITY_GAIN                       32768
#define MINUS_INFINITY_GAIN              0

/* the size of a substream (1 mono data stream) */

#define HDSP_CHANNEL_BUFFER_SAMPLES  (16*1024)
#define HDSP_CHANNEL_BUFFER_BYTES    (4*HDSP_CHANNEL_BUFFER_SAMPLES)

/* the size of the area we need to allocate for DMA transfers. the
404
   size is the same regardless of the number of channels - the
Linus Torvalds's avatar
Linus Torvalds committed
405 406 407 408 409 410 411 412 413 414
   Multiface still uses the same memory area.

   Note that we allocate 1 more channel than is apparently needed
   because the h/w seems to write 1 byte beyond the end of the last
   page. Sigh.
*/

#define HDSP_DMA_AREA_BYTES ((HDSP_MAX_CHANNELS+1) * HDSP_CHANNEL_BUFFER_BYTES)
#define HDSP_DMA_AREA_KILOBYTES (HDSP_DMA_AREA_BYTES/1024)

415
#define HDSP_FIRMWARE_SIZE	(24413 * 4)
Linus Torvalds's avatar
Linus Torvalds committed
416

417
struct hdsp_9632_meters {
Linus Torvalds's avatar
Linus Torvalds committed
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
    u32 input_peak[16];
    u32 playback_peak[16];
    u32 output_peak[16];
    u32 xxx_peak[16];
    u32 padding[64];
    u32 input_rms_low[16];
    u32 playback_rms_low[16];
    u32 output_rms_low[16];
    u32 xxx_rms_low[16];
    u32 input_rms_high[16];
    u32 playback_rms_high[16];
    u32 output_rms_high[16];
    u32 xxx_rms_high[16];
};

433 434
struct hdsp_midi {
    struct hdsp             *hdsp;
Linus Torvalds's avatar
Linus Torvalds committed
435
    int                      id;
436 437 438
    struct snd_rawmidi           *rmidi;
    struct snd_rawmidi_substream *input;
    struct snd_rawmidi_substream *output;
Linus Torvalds's avatar
Linus Torvalds committed
439 440 441 442 443 444
    char                     istimer; /* timer in use */
    struct timer_list	     timer;
    spinlock_t               lock;
    int			     pending;
};

445
struct hdsp {
Linus Torvalds's avatar
Linus Torvalds committed
446
	spinlock_t            lock;
447 448 449
	struct snd_pcm_substream *capture_substream;
	struct snd_pcm_substream *playback_substream;
        struct hdsp_midi      midi[2];
Linus Torvalds's avatar
Linus Torvalds committed
450 451 452 453 454 455 456
	struct tasklet_struct midi_tasklet;
	int		      use_midi_tasklet;
	int                   precise_ptr;
	u32                   control_register;	     /* cached value */
	u32                   control2_register;     /* cached value */
	u32                   creg_spdif;
	u32                   creg_spdif_stream;
457
	int                   clock_source_locked;
458
	char                 *card_name;	 /* digiface/multiface/rpm */
459
	enum HDSP_IO_Type     io_type;               /* ditto, but for code use */
Linus Torvalds's avatar
Linus Torvalds committed
460 461
        unsigned short        firmware_rev;
	unsigned short	      state;		     /* stores state bits */
462 463
	const struct firmware *firmware;
	u32                  *fw_uploaded;
Linus Torvalds's avatar
Linus Torvalds committed
464 465 466 467 468
	size_t                period_bytes; 	     /* guess what this is */
	unsigned char	      max_channels;
	unsigned char	      qs_in_channels;	     /* quad speed mode for H9632 */
	unsigned char         ds_in_channels;
	unsigned char         ss_in_channels;	    /* different for multiface/digiface */
469
	unsigned char	      qs_out_channels;
Linus Torvalds's avatar
Linus Torvalds committed
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
	unsigned char         ds_out_channels;
	unsigned char         ss_out_channels;

	struct snd_dma_buffer capture_dma_buf;
	struct snd_dma_buffer playback_dma_buf;
	unsigned char        *capture_buffer;	    /* suitably aligned address */
	unsigned char        *playback_buffer;	    /* suitably aligned address */

	pid_t                 capture_pid;
	pid_t                 playback_pid;
	int                   running;
	int                   system_sample_rate;
	char                 *channel_map;
	int                   dev;
	int                   irq;
	unsigned long         port;
        void __iomem         *iobase;
487 488 489
	struct snd_card *card;
	struct snd_pcm *pcm;
	struct snd_hwdep          *hwdep;
Linus Torvalds's avatar
Linus Torvalds committed
490
	struct pci_dev       *pci;
491
	struct snd_kcontrol *spdif_ctl;
Linus Torvalds's avatar
Linus Torvalds committed
492
        unsigned short        mixer_matrix[HDSP_MATRIX_MIXER_SIZE];
493
	unsigned int          dds_value; /* last value written to freq register */
Linus Torvalds's avatar
Linus Torvalds committed
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
};

/* These tables map the ALSA channels 1..N to the channels that we
   need to use in order to find the relevant channel buffer. RME
   refer to this kind of mapping as between "the ADAT channel and
   the DMA channel." We index it using the logical audio channel,
   and the value is the DMA channel (i.e. channel buffer number)
   where the data for that channel can be read/written from/to.
*/

static char channel_map_df_ss[HDSP_MAX_CHANNELS] = {
	0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
	18, 19, 20, 21, 22, 23, 24, 25
};

static char channel_map_mf_ss[HDSP_MAX_CHANNELS] = { /* Multiface */
	/* Analog */
511
	0, 1, 2, 3, 4, 5, 6, 7,
Linus Torvalds's avatar
Linus Torvalds committed
512
	/* ADAT 2 */
513
	16, 17, 18, 19, 20, 21, 22, 23,
Linus Torvalds's avatar
Linus Torvalds committed
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
	/* SPDIF */
	24, 25,
	-1, -1, -1, -1, -1, -1, -1, -1
};

static char channel_map_ds[HDSP_MAX_CHANNELS] = {
	/* ADAT channels are remapped */
	1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23,
	/* channels 12 and 13 are S/PDIF */
	24, 25,
	/* others don't exist */
	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
};

static char channel_map_H9632_ss[HDSP_MAX_CHANNELS] = {
	/* ADAT channels */
	0, 1, 2, 3, 4, 5, 6, 7,
	/* SPDIF */
	8, 9,
	/* Analog */
534
	10, 11,
Linus Torvalds's avatar
Linus Torvalds committed
535 536 537
	/* AO4S-192 and AI4S-192 extension boards */
	12, 13, 14, 15,
	/* others don't exist */
538
	-1, -1, -1, -1, -1, -1, -1, -1,
Linus Torvalds's avatar
Linus Torvalds committed
539 540 541 542 543 544 545 546 547
	-1, -1
};

static char channel_map_H9632_ds[HDSP_MAX_CHANNELS] = {
	/* ADAT */
	1, 3, 5, 7,
	/* SPDIF */
	8, 9,
	/* Analog */
548
	10, 11,
Linus Torvalds's avatar
Linus Torvalds committed
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
	/* AO4S-192 and AI4S-192 extension boards */
	12, 13, 14, 15,
	/* others don't exist */
	-1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1
};

static char channel_map_H9632_qs[HDSP_MAX_CHANNELS] = {
	/* ADAT is disabled in this mode */
	/* SPDIF */
	8, 9,
	/* Analog */
	10, 11,
	/* AO4S-192 and AI4S-192 extension boards */
	12, 13, 14, 15,
	/* others don't exist */
	-1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1
};

static int snd_hammerfall_get_buffer(struct pci_dev *pci, struct snd_dma_buffer *dmab, size_t size)
{
572
	return snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, &pci->dev, size, dmab);
Linus Torvalds's avatar
Linus Torvalds committed
573 574 575 576
}

static void snd_hammerfall_free_buffer(struct snd_dma_buffer *dmab, struct pci_dev *pci)
{
577 578
	if (dmab->area)
		snd_dma_free_pages(dmab);
Linus Torvalds's avatar
Linus Torvalds committed
579 580 581
}


582
static const struct pci_device_id snd_hdsp_ids[] = {
Linus Torvalds's avatar
Linus Torvalds committed
583 584
	{
		.vendor = PCI_VENDOR_ID_XILINX,
585
		.device = PCI_DEVICE_ID_XILINX_HAMMERFALL_DSP,
Linus Torvalds's avatar
Linus Torvalds committed
586 587 588 589 590 591 592 593 594
		.subvendor = PCI_ANY_ID,
		.subdevice = PCI_ANY_ID,
	}, /* RME Hammerfall-DSP */
	{ 0, },
};

MODULE_DEVICE_TABLE(pci, snd_hdsp_ids);

/* prototypes */
595 596 597 598 599 600 601 602 603 604 605
static int snd_hdsp_create_alsa_devices(struct snd_card *card, struct hdsp *hdsp);
static int snd_hdsp_create_pcm(struct snd_card *card, struct hdsp *hdsp);
static int snd_hdsp_enable_io (struct hdsp *hdsp);
static void snd_hdsp_initialize_midi_flush (struct hdsp *hdsp);
static void snd_hdsp_initialize_channels (struct hdsp *hdsp);
static int hdsp_fifo_wait(struct hdsp *hdsp, int count, int timeout);
static int hdsp_autosync_ref(struct hdsp *hdsp);
static int snd_hdsp_set_defaults(struct hdsp *hdsp);
static void snd_hdsp_9652_enable_mixer (struct hdsp *hdsp);

static int hdsp_playback_to_output_key (struct hdsp *hdsp, int in, int out)
Linus Torvalds's avatar
Linus Torvalds committed
606
{
607 608 609
	switch (hdsp->io_type) {
	case Multiface:
	case Digiface:
610
	case RPM:
611
	default:
612 613 614 615
		if (hdsp->firmware_rev == 0xa)
			return (64 * out) + (32 + (in));
		else
			return (52 * out) + (26 + (in));
616
	case H9632:
Linus Torvalds's avatar
Linus Torvalds committed
617
		return (32 * out) + (16 + (in));
618
	case H9652:
Linus Torvalds's avatar
Linus Torvalds committed
619 620 621 622
		return (52 * out) + (26 + (in));
	}
}

623
static int hdsp_input_to_output_key (struct hdsp *hdsp, int in, int out)
Linus Torvalds's avatar
Linus Torvalds committed
624
{
625 626 627
	switch (hdsp->io_type) {
	case Multiface:
	case Digiface:
628
	case RPM:
629
	default:
630 631 632 633
		if (hdsp->firmware_rev == 0xa)
			return (64 * out) + in;
		else
			return (52 * out) + in;
634
	case H9632:
Linus Torvalds's avatar
Linus Torvalds committed
635
		return (32 * out) + in;
636
	case H9652:
Linus Torvalds's avatar
Linus Torvalds committed
637 638 639 640
		return (52 * out) + in;
	}
}

641
static void hdsp_write(struct hdsp *hdsp, int reg, int val)
Linus Torvalds's avatar
Linus Torvalds committed
642 643 644 645
{
	writel(val, hdsp->iobase + reg);
}

646
static unsigned int hdsp_read(struct hdsp *hdsp, int reg)
Linus Torvalds's avatar
Linus Torvalds committed
647 648 649 650
{
	return readl (hdsp->iobase + reg);
}

651
static int hdsp_check_for_iobox (struct hdsp *hdsp)
Linus Torvalds's avatar
Linus Torvalds committed
652
{
653 654
	int i;

Linus Torvalds's avatar
Linus Torvalds committed
655
	if (hdsp->io_type == H9652 || hdsp->io_type == H9632) return 0;
656 657 658 659
	for (i = 0; i < 500; i++) {
		if (0 == (hdsp_read(hdsp, HDSP_statusRegister) &
					HDSP_ConfigError)) {
			if (i) {
660 661
				dev_dbg(hdsp->card->dev,
					"IO box found after %d ms\n",
662 663 664 665 666
						(20 * i));
			}
			return 0;
		}
		msleep(20);
Linus Torvalds's avatar
Linus Torvalds committed
667
	}
668
	dev_err(hdsp->card->dev, "no IO box connected!\n");
669 670
	hdsp->state &= ~HDSP_FirmwareLoaded;
	return -EIO;
Tim Blechmann's avatar
Tim Blechmann committed
671
}
Linus Torvalds's avatar
Linus Torvalds committed
672

Tim Blechmann's avatar
Tim Blechmann committed
673 674 675 676 677 678 679 680 681 682 683 684
static int hdsp_wait_for_iobox(struct hdsp *hdsp, unsigned int loops,
			       unsigned int delay)
{
	unsigned int i;

	if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
		return 0;

	for (i = 0; i != loops; ++i) {
		if (hdsp_read(hdsp, HDSP_statusRegister) & HDSP_ConfigError)
			msleep(delay);
		else {
685
			dev_dbg(hdsp->card->dev, "iobox found after %ums!\n",
Tim Blechmann's avatar
Tim Blechmann committed
686 687 688 689 690
				   i * delay);
			return 0;
		}
	}

691
	dev_info(hdsp->card->dev, "no IO box connected!\n");
Tim Blechmann's avatar
Tim Blechmann committed
692 693
	hdsp->state &= ~HDSP_FirmwareLoaded;
	return -EIO;
Linus Torvalds's avatar
Linus Torvalds committed
694 695
}

696
static int snd_hdsp_load_firmware_from_cache(struct hdsp *hdsp) {
Linus Torvalds's avatar
Linus Torvalds committed
697 698 699

	int i;
	unsigned long flags;
700 701 702 703 704 705 706 707 708 709 710
	const u32 *cache;

	if (hdsp->fw_uploaded)
		cache = hdsp->fw_uploaded;
	else {
		if (!hdsp->firmware)
			return -ENODEV;
		cache = (u32 *)hdsp->firmware->data;
		if (!cache)
			return -ENODEV;
	}
Linus Torvalds's avatar
Linus Torvalds committed
711 712

	if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
713

714
		dev_info(hdsp->card->dev, "loading firmware\n");
Linus Torvalds's avatar
Linus Torvalds committed
715 716 717

		hdsp_write (hdsp, HDSP_control2Reg, HDSP_S_PROGRAM);
		hdsp_write (hdsp, HDSP_fifoData, 0);
718

Linus Torvalds's avatar
Linus Torvalds committed
719
		if (hdsp_fifo_wait (hdsp, 0, HDSP_LONG_WAIT)) {
720 721
			dev_info(hdsp->card->dev,
				 "timeout waiting for download preparation\n");
722
			hdsp_write(hdsp, HDSP_control2Reg, HDSP_S200);
Linus Torvalds's avatar
Linus Torvalds committed
723 724
			return -EIO;
		}
725

Linus Torvalds's avatar
Linus Torvalds committed
726
		hdsp_write (hdsp, HDSP_control2Reg, HDSP_S_LOAD);
727

728 729
		for (i = 0; i < HDSP_FIRMWARE_SIZE / 4; ++i) {
			hdsp_write(hdsp, HDSP_fifoData, cache[i]);
Linus Torvalds's avatar
Linus Torvalds committed
730
			if (hdsp_fifo_wait (hdsp, 127, HDSP_LONG_WAIT)) {
731 732
				dev_info(hdsp->card->dev,
					 "timeout during firmware loading\n");
733
				hdsp_write(hdsp, HDSP_control2Reg, HDSP_S200);
Linus Torvalds's avatar
Linus Torvalds committed
734 735 736 737
				return -EIO;
			}
		}

738 739
		hdsp_fifo_wait(hdsp, 3, HDSP_LONG_WAIT);
		hdsp_write(hdsp, HDSP_control2Reg, HDSP_S200);
Linus Torvalds's avatar
Linus Torvalds committed
740

741
		ssleep(3);
Linus Torvalds's avatar
Linus Torvalds committed
742 743 744 745 746 747
#ifdef SNDRV_BIG_ENDIAN
		hdsp->control2_register = HDSP_BIGENDIAN_MODE;
#else
		hdsp->control2_register = 0;
#endif
		hdsp_write (hdsp, HDSP_control2Reg, hdsp->control2_register);
748
		dev_info(hdsp->card->dev, "finished firmware loading\n");
749

Linus Torvalds's avatar
Linus Torvalds committed
750 751
	}
	if (hdsp->state & HDSP_InitializationComplete) {
752 753
		dev_info(hdsp->card->dev,
			 "firmware loaded from cache, restoring defaults\n");
Linus Torvalds's avatar
Linus Torvalds committed
754 755
		spin_lock_irqsave(&hdsp->lock, flags);
		snd_hdsp_set_defaults(hdsp);
756
		spin_unlock_irqrestore(&hdsp->lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
757
	}
758

Linus Torvalds's avatar
Linus Torvalds committed
759 760 761 762 763
	hdsp->state |= HDSP_FirmwareLoaded;

	return 0;
}

764
static int hdsp_get_iobox_version (struct hdsp *hdsp)
Linus Torvalds's avatar
Linus Torvalds committed
765 766
{
	if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
767

768 769
		hdsp_write(hdsp, HDSP_control2Reg, HDSP_S_LOAD);
		hdsp_write(hdsp, HDSP_fifoData, 0);
Linus Torvalds's avatar
Linus Torvalds committed
770

771 772 773 774 775 776
		if (hdsp_fifo_wait(hdsp, 0, HDSP_SHORT_WAIT) < 0) {
			hdsp_write(hdsp, HDSP_control2Reg, HDSP_S300);
			hdsp_write(hdsp, HDSP_control2Reg, HDSP_S_LOAD);
		}

		hdsp_write(hdsp, HDSP_control2Reg, HDSP_S200 | HDSP_PROGRAM);
Linus Torvalds's avatar
Linus Torvalds committed
777
		hdsp_write (hdsp, HDSP_fifoData, 0);
778 779
		if (hdsp_fifo_wait(hdsp, 0, HDSP_SHORT_WAIT) < 0)
			goto set_multi;
Linus Torvalds's avatar
Linus Torvalds committed
780

781 782 783
		hdsp_write(hdsp, HDSP_control2Reg, HDSP_S_LOAD);
		hdsp_write(hdsp, HDSP_fifoData, 0);
		if (hdsp_fifo_wait(hdsp, 0, HDSP_SHORT_WAIT) == 0) {
Linus Torvalds's avatar
Linus Torvalds committed
784
			hdsp->io_type = Digiface;
785
			dev_info(hdsp->card->dev, "Digiface found\n");
786
			return 0;
787
		}
788 789 790 791

		hdsp_write(hdsp, HDSP_control2Reg, HDSP_S300);
		hdsp_write(hdsp, HDSP_control2Reg, HDSP_S_LOAD);
		hdsp_write(hdsp, HDSP_fifoData, 0);
792 793
		if (hdsp_fifo_wait(hdsp, 0, HDSP_SHORT_WAIT) == 0)
			goto set_multi;
794 795 796 797

		hdsp_write(hdsp, HDSP_control2Reg, HDSP_S300);
		hdsp_write(hdsp, HDSP_control2Reg, HDSP_S_LOAD);
		hdsp_write(hdsp, HDSP_fifoData, 0);
798 799
		if (hdsp_fifo_wait(hdsp, 0, HDSP_SHORT_WAIT) < 0)
			goto set_multi;
800 801

		hdsp->io_type = RPM;
802
		dev_info(hdsp->card->dev, "RPM found\n");
803
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
804 805
	} else {
		/* firmware was already loaded, get iobox type */
806 807 808
		if (hdsp_read(hdsp, HDSP_status2Register) & HDSP_version2)
			hdsp->io_type = RPM;
		else if (hdsp_read(hdsp, HDSP_status2Register) & HDSP_version1)
Linus Torvalds's avatar
Linus Torvalds committed
809
			hdsp->io_type = Multiface;
Takashi Iwai's avatar
Takashi Iwai committed
810
		else
Linus Torvalds's avatar
Linus Torvalds committed
811 812 813
			hdsp->io_type = Digiface;
	}
	return 0;
814 815 816 817 818

set_multi:
	hdsp->io_type = Multiface;
	dev_info(hdsp->card->dev, "Multiface found\n");
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
819 820 821
}


822
static int hdsp_request_fw_loader(struct hdsp *hdsp);
823 824

static int hdsp_check_for_firmware (struct hdsp *hdsp, int load_on_demand)
Linus Torvalds's avatar
Linus Torvalds committed
825
{
826 827
	if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
828 829
	if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
		hdsp->state &= ~HDSP_FirmwareLoaded;
830
		if (! load_on_demand)
Takashi Iwai's avatar
Takashi Iwai committed
831
			return -EIO;
832
		dev_err(hdsp->card->dev, "firmware not present.\n");
Takashi Iwai's avatar
Takashi Iwai committed
833
		/* try to load firmware */
834 835 836
		if (! (hdsp->state & HDSP_FirmwareCached)) {
			if (! hdsp_request_fw_loader(hdsp))
				return 0;
837 838
			dev_err(hdsp->card->dev,
				   "No firmware loaded nor cached, please upload firmware.\n");
839 840 841
			return -EIO;
		}
		if (snd_hdsp_load_firmware_from_cache(hdsp) != 0) {
842 843
			dev_err(hdsp->card->dev,
				   "Firmware loading from cache failed, please upload manually.\n");
844
			return -EIO;
Takashi Iwai's avatar
Takashi Iwai committed
845
		}
Linus Torvalds's avatar
Linus Torvalds committed
846 847 848 849 850
	}
	return 0;
}


851
static int hdsp_fifo_wait(struct hdsp *hdsp, int count, int timeout)
852
{
Linus Torvalds's avatar
Linus Torvalds committed
853 854 855 856 857
	int i;

	/* the fifoStatus registers reports on how many words
	   are available in the command FIFO.
	*/
858

Linus Torvalds's avatar
Linus Torvalds committed
859 860 861 862 863 864 865 866 867 868 869 870
	for (i = 0; i < timeout; i++) {

		if ((int)(hdsp_read (hdsp, HDSP_fifoStatus) & 0xff) <= count)
			return 0;

		/* not very friendly, but we only do this during a firmware
		   load and changing the mixer, so we just put up with it.
		*/

		udelay (100);
	}

871 872
	dev_warn(hdsp->card->dev,
		 "wait for FIFO status <= %d failed after %d iterations\n",
Linus Torvalds's avatar
Linus Torvalds committed
873 874 875 876
		    count, timeout);
	return -1;
}

877
static int hdsp_read_gain (struct hdsp *hdsp, unsigned int addr)
Linus Torvalds's avatar
Linus Torvalds committed
878
{
Takashi Iwai's avatar
Takashi Iwai committed
879
	if (addr >= HDSP_MATRIX_MIXER_SIZE)
Linus Torvalds's avatar
Linus Torvalds committed
880
		return 0;
Takashi Iwai's avatar
Takashi Iwai committed
881

Linus Torvalds's avatar
Linus Torvalds committed
882 883 884
	return hdsp->mixer_matrix[addr];
}

885
static int hdsp_write_gain(struct hdsp *hdsp, unsigned int addr, unsigned short data)
Linus Torvalds's avatar
Linus Torvalds committed
886 887 888 889 890
{
	unsigned int ad;

	if (addr >= HDSP_MATRIX_MIXER_SIZE)
		return -1;
891

Linus Torvalds's avatar
Linus Torvalds committed
892 893 894
	if (hdsp->io_type == H9652 || hdsp->io_type == H9632) {

		/* from martin bjornsen:
895

Linus Torvalds's avatar
Linus Torvalds committed
896 897 898 899 900 901 902 903 904 905
		   "You can only write dwords to the
		   mixer memory which contain two
		   mixer values in the low and high
		   word. So if you want to change
		   value 0 you have to read value 1
		   from the cache and write both to
		   the first dword in the mixer
		   memory."
		*/

Takashi Iwai's avatar
Takashi Iwai committed
906
		if (hdsp->io_type == H9632 && addr >= 512)
Linus Torvalds's avatar
Linus Torvalds committed
907 908
			return 0;

Takashi Iwai's avatar
Takashi Iwai committed
909
		if (hdsp->io_type == H9652 && addr >= 1352)
Linus Torvalds's avatar
Linus Torvalds committed
910 911 912 913
			return 0;

		hdsp->mixer_matrix[addr] = data;

914

Linus Torvalds's avatar
Linus Torvalds committed
915 916 917 918 919 920 921 922
		/* `addr' addresses a 16-bit wide address, but
		   the address space accessed via hdsp_write
		   uses byte offsets. put another way, addr
		   varies from 0 to 1351, but to access the
		   corresponding memory location, we need
		   to access 0 to 2703 ...
		*/
		ad = addr/2;
923 924 925

		hdsp_write (hdsp, 4096 + (ad*4),
			    (hdsp->mixer_matrix[(addr&0x7fe)+1] << 16) +
Linus Torvalds's avatar
Linus Torvalds committed
926
			    hdsp->mixer_matrix[addr&0x7fe]);
927

Linus Torvalds's avatar
Linus Torvalds committed
928 929 930 931 932
		return 0;

	} else {

		ad = (addr << 16) + data;
933

Takashi Iwai's avatar
Takashi Iwai committed
934
		if (hdsp_fifo_wait(hdsp, 127, HDSP_LONG_WAIT))
Linus Torvalds's avatar
Linus Torvalds committed
935 936 937 938 939 940 941 942 943 944
			return -1;

		hdsp_write (hdsp, HDSP_fifoData, ad);
		hdsp->mixer_matrix[addr] = data;

	}

	return 0;
}

945
static int snd_hdsp_use_is_exclusive(struct hdsp *hdsp)
Linus Torvalds's avatar
Linus Torvalds committed
946 947 948 949 950 951
{
	unsigned long flags;
	int ret = 1;

	spin_lock_irqsave(&hdsp->lock, flags);
	if ((hdsp->playback_pid != hdsp->capture_pid) &&
Takashi Iwai's avatar
Takashi Iwai committed
952
	    (hdsp->playback_pid >= 0) && (hdsp->capture_pid >= 0))
Linus Torvalds's avatar
Linus Torvalds committed
953 954 955 956 957
		ret = 0;
	spin_unlock_irqrestore(&hdsp->lock, flags);
	return ret;
}

958
static int hdsp_spdif_sample_rate(struct hdsp *hdsp)
Linus Torvalds's avatar
Linus Torvalds committed
959 960 961 962
{
	unsigned int status = hdsp_read(hdsp, HDSP_statusRegister);
	unsigned int rate_bits = (status & HDSP_spdifFrequencyMask);

963 964 965 966
	/* For the 9632, the mask is different */
	if (hdsp->io_type == H9632)
		 rate_bits = (status & HDSP_spdifFrequencyMask_9632);

Takashi Iwai's avatar
Takashi Iwai committed
967
	if (status & HDSP_SPDIFErrorFlag)
Linus Torvalds's avatar
Linus Torvalds committed
968
		return 0;
969

Linus Torvalds's avatar
Linus Torvalds committed
970 971 972 973 974 975 976
	switch (rate_bits) {
	case HDSP_spdifFrequency32KHz: return 32000;
	case HDSP_spdifFrequency44_1KHz: return 44100;
	case HDSP_spdifFrequency48KHz: return 48000;
	case HDSP_spdifFrequency64KHz: return 64000;
	case HDSP_spdifFrequency88_2KHz: return 88200;
	case HDSP_spdifFrequency96KHz: return 96000;
977
	case HDSP_spdifFrequency128KHz:
Linus Torvalds's avatar
Linus Torvalds committed
978 979
		if (hdsp->io_type == H9632) return 128000;
		break;
980
	case HDSP_spdifFrequency176_4KHz:
Linus Torvalds's avatar
Linus Torvalds committed
981 982
		if (hdsp->io_type == H9632) return 176400;
		break;
983
	case HDSP_spdifFrequency192KHz:
Linus Torvalds's avatar
Linus Torvalds committed
984 985 986 987 988
		if (hdsp->io_type == H9632) return 192000;
		break;
	default:
		break;
	}
989 990 991
	dev_warn(hdsp->card->dev,
		 "unknown spdif frequency status; bits = 0x%x, status = 0x%x\n",
		 rate_bits, status);
Linus Torvalds's avatar
Linus Torvalds committed
992 993 994
	return 0;
}

995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
static int hdsp_external_sample_rate(struct hdsp *hdsp)
{
	unsigned int status2 = hdsp_read(hdsp, HDSP_status2Register);
	unsigned int rate_bits = status2 & HDSP_systemFrequencyMask;

	/* For the 9632 card, there seems to be no bit for indicating external
	 * sample rate greater than 96kHz. The card reports the corresponding
	 * single speed. So the best means seems to get spdif rate when
	 * autosync reference is spdif */
	if (hdsp->io_type == H9632 &&
	    hdsp_autosync_ref(hdsp) == HDSP_AUTOSYNC_FROM_SPDIF)
		 return hdsp_spdif_sample_rate(hdsp);

	switch (rate_bits) {
	case HDSP_systemFrequency32:   return 32000;
	case HDSP_systemFrequency44_1: return 44100;
	case HDSP_systemFrequency48:   return 48000;
	case HDSP_systemFrequency64:   return 64000;
	case HDSP_systemFrequency88_2: return 88200;
	case HDSP_systemFrequency96:   return 96000;
	default:
		return 0;
	}
}

1020
static void hdsp_compute_period_size(struct hdsp *hdsp)
Linus Torvalds's avatar
Linus Torvalds committed
1021 1022 1023 1024
{
	hdsp->period_bytes = 1 << ((hdsp_decode_latency(hdsp->control_register) + 8));
}

1025
static snd_pcm_uframes_t hdsp_hw_pointer(struct hdsp *hdsp)
Linus Torvalds's avatar
Linus Torvalds committed
1026 1027 1028 1029 1030
{
	int position;

	position = hdsp_read(hdsp, HDSP_statusRegister);

Takashi Iwai's avatar
Takashi Iwai committed
1031
	if (!hdsp->precise_ptr)
Linus Torvalds's avatar
Linus Torvalds committed
1032 1033 1034 1035 1036 1037 1038 1039
		return (position & HDSP_BufferID) ? (hdsp->period_bytes / 4) : 0;

	position &= HDSP_BufferPositionMask;
	position /= 4;
	position &= (hdsp->period_bytes/2) - 1;
	return position;
}

1040
static void hdsp_reset_hw_pointer(struct hdsp *hdsp)
Linus Torvalds's avatar
Linus Torvalds committed
1041 1042
{
	hdsp_write (hdsp, HDSP_resetPointer, 0);
1043 1044 1045 1046 1047
	if (hdsp->io_type == H9632 && hdsp->firmware_rev >= 152)
		/* HDSP_resetPointer = HDSP_freqReg, which is strange and
		 * requires (?) to write again DDS value after a reset pointer
		 * (at least, it works like this) */
		hdsp_write (hdsp, HDSP_freqReg, hdsp->dds_value);
Linus Torvalds's avatar
Linus Torvalds committed
1048 1049
}

1050
static void hdsp_start_audio(struct hdsp *s)
Linus Torvalds's avatar
Linus Torvalds committed
1051 1052 1053 1054 1055
{
	s->control_register |= (HDSP_AudioInterruptEnable | HDSP_Start);
	hdsp_write(s, HDSP_controlRegister, s->control_register);
}

1056
static void hdsp_stop_audio(struct hdsp *s)
Linus Torvalds's avatar
Linus Torvalds committed
1057 1058 1059 1060 1061
{
	s->control_register &= ~(HDSP_Start | HDSP_AudioInterruptEnable);
	hdsp_write(s, HDSP_controlRegister, s->control_register);
}

1062
static void hdsp_silence_playback(struct hdsp *hdsp)
Linus Torvalds's avatar
Linus Torvalds committed
1063 1064 1065 1066
{
	memset(hdsp->playback_buffer, 0, HDSP_DMA_AREA_BYTES);
}

1067
static int hdsp_set_interrupt_interval(struct hdsp *s, unsigned int frames)
Linus Torvalds's avatar
Linus Torvalds committed
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
{
	int n;

	spin_lock_irq(&s->lock);

	frames >>= 7;
	n = 0;
	while (frames) {
		n++;
		frames >>= 1;
	}

	s->control_register &= ~HDSP_LatencyMask;
	s->control_register |= hdsp_encode_latency(n);

	hdsp_write(s, HDSP_controlRegister, s->control_register);

	hdsp_compute_period_size(s);

	spin_unlock_irq(&s->lock);

	return 0;
}

1092 1093 1094
static void hdsp_set_dds_value(struct hdsp *hdsp, int rate)
{
	u64 n;
1095

1096 1097 1098 1099 1100
	if (rate >= 112000)
		rate /= 4;
	else if (rate >= 56000)
		rate /= 2;

1101
	n = DDS_NUMERATOR;
1102
	n = div_u64(n, rate);
1103
	/* n should be less than 2^32 for being written to FREQ register */
1104
	snd_BUG_ON(n >> 32);
1105 1106 1107 1108 1109 1110
	/* HDSP_freqReg and HDSP_resetPointer are the same, so keep the DDS
	   value to write it after a reset */
	hdsp->dds_value = n;
	hdsp_write(hdsp, HDSP_freqReg, hdsp->dds_value);
}

1111
static int hdsp_set_rate(struct hdsp *hdsp, int rate, int called_internally)
Linus Torvalds's avatar
Linus Torvalds committed
1112 1113 1114 1115 1116 1117 1118 1119 1120
{
	int reject_if_open = 0;
	int current_rate;
	int rate_bits;

	/* ASSUMPTION: hdsp->lock is either held, or
	   there is no need for it (e.g. during module
	   initialization).
	*/
1121 1122

	if (!(hdsp->control_register & HDSP_ClockModeMaster)) {
Linus Torvalds's avatar
Linus Torvalds committed
1123 1124
		if (called_internally) {
			/* request from ctl or card initialization */
1125 1126
			dev_err(hdsp->card->dev,
				"device is not running as a clock master: cannot set sample rate.\n");
Linus Torvalds's avatar
Linus Torvalds committed
1127
			return -1;
1128
		} else {
Linus Torvalds's avatar
Linus Torvalds committed
1129 1130 1131
			/* hw_param request while in AutoSync mode */
			int external_freq = hdsp_external_sample_rate(hdsp);
			int spdif_freq = hdsp_spdif_sample_rate(hdsp);
1132

Takashi Iwai's avatar
Takashi Iwai committed
1133
			if ((spdif_freq == external_freq*2) && (hdsp_autosync_ref(hdsp) >= HDSP_AUTOSYNC_FROM_ADAT1))
1134 1135
				dev_info(hdsp->card->dev,
					 "Detected ADAT in double speed mode\n");
Takashi Iwai's avatar
Takashi Iwai committed
1136
			else if (hdsp->io_type == H9632 && (spdif_freq == external_freq*4) && (hdsp_autosync_ref(hdsp) >= HDSP_AUTOSYNC_FROM_ADAT1))
1137 1138
				dev_info(hdsp->card->dev,
					 "Detected ADAT in quad speed mode\n");
Takashi Iwai's avatar
Takashi Iwai committed
1139
			else if (rate != external_freq) {
1140 1141
				dev_info(hdsp->card->dev,
					 "No AutoSync source for requested rate\n");
Linus Torvalds's avatar
Linus Torvalds committed
1142
				return -1;
1143 1144
			}
		}
Linus Torvalds's avatar
Linus Torvalds committed
1145 1146 1147 1148 1149 1150
	}

	current_rate = hdsp->system_sample_rate;

	/* Changing from a "single speed" to a "double speed" rate is
	   not allowed if any substreams are open. This is because
1151
	   such a change causes a shift in the location of
Linus Torvalds's avatar
Linus Torvalds committed
1152
	   the DMA buffers and a reduction in the number of available
1153
	   buffers.
Linus Torvalds's avatar
Linus Torvalds committed
1154 1155 1156 1157 1158

	   Note that a similar but essentially insoluble problem
	   exists for externally-driven rate changes. All we can do
	   is to flag rate changes in the read/write routines.  */

Takashi Iwai's avatar
Takashi Iwai committed
1159
	if (rate > 96000 && hdsp->io_type != H9632)
Linus Torvalds's avatar
Linus Torvalds committed
1160
		return -EINVAL;
1161

Linus Torvalds's avatar
Linus Torvalds committed
1162 1163
	switch (rate) {
	case 32000:
Takashi Iwai's avatar
Takashi Iwai committed
1164
		if (current_rate > 48000)
Linus Torvalds's avatar
Linus Torvalds committed
1165 1166 1167 1168
			reject_if_open = 1;
		rate_bits = HDSP_Frequency32KHz;
		break;
	case 44100:
Takashi Iwai's avatar
Takashi Iwai committed
1169
		if (current_rate > 48000)
Linus Torvalds's avatar
Linus Torvalds committed
1170 1171 1172 1173
			reject_if_open = 1;
		rate_bits = HDSP_Frequency44_1KHz;
		break;
	case 48000:
Takashi Iwai's avatar
Takashi Iwai committed
1174
		if (current_rate > 48000)
Linus Torvalds's avatar
Linus Torvalds committed
1175 1176 1177 1178
			reject_if_open = 1;
		rate_bits = HDSP_Frequency48KHz;
		break;
	case 64000:
Takashi Iwai's avatar
Takashi Iwai committed
1179
		if (current_rate <= 48000 || current_rate > 96000)
Linus Torvalds's avatar
Linus Torvalds committed
1180 1181 1182 1183
			reject_if_open = 1;
		rate_bits = HDSP_Frequency64KHz;
		break;
	case 88200:
Takashi Iwai's avatar
Takashi Iwai committed
1184
		if (current_rate <= 48000 || current_rate > 96000)
Linus Torvalds's avatar
Linus Torvalds committed
1185 1186 1187 1188
			reject_if_open = 1;
		rate_bits = HDSP_Frequency88_2KHz;
		break;
	case 96000:
Takashi Iwai's avatar
Takashi Iwai committed
1189
		if (current_rate <= 48000 || current_rate > 96000)
Linus Torvalds's avatar
Linus Torvalds committed
1190 1191 1192 1193
			reject_if_open = 1;
		rate_bits = HDSP_Frequency96KHz;
		break;
	case 128000:
Takashi Iwai's avatar
Takashi Iwai committed
1194
		if (current_rate < 128000)
Linus Torvalds's avatar
Linus Torvalds committed
1195 1196 1197 1198
			reject_if_open = 1;
		rate_bits = HDSP_Frequency128KHz;
		break;
	case 176400:
Takashi Iwai's avatar
Takashi Iwai committed
1199
		if (current_rate < 128000)
Linus Torvalds's avatar
Linus Torvalds committed
1200 1201 1202 1203
			reject_if_open = 1;
		rate_bits = HDSP_Frequency176_4KHz;
		break;
	case 192000:
Takashi Iwai's avatar
Takashi Iwai committed
1204
		if (current_rate < 128000)
Linus Torvalds's avatar
Linus Torvalds committed
1205 1206 1207 1208 1209 1210 1211 1212
			reject_if_open = 1;
		rate_bits = HDSP_Frequency192KHz;
		break;
	default:
		return -EINVAL;
	}

	if (reject_if_open && (hdsp->capture_pid >= 0 || hdsp->playback_pid >= 0)) {
1213 1214
		dev_warn(hdsp->card->dev,
			 "cannot change speed mode (capture PID = %d, playback PID = %d)\n",
Linus Torvalds's avatar
Linus Torvalds committed
1215 1216 1217 1218 1219 1220 1221 1222 1223
			    hdsp->capture_pid,
			    hdsp->playback_pid);
		return -EBUSY;
	}

	hdsp->control_register &= ~HDSP_FrequencyMask;
	hdsp->control_register |= rate_bits;
	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);

1224 1225 1226 1227
	/* For HDSP9632 rev 152, need to set DDS value in FREQ register */
	if (hdsp->io_type == H9632 && hdsp->firmware_rev >= 152)
		hdsp_set_dds_value(hdsp, rate);

Linus Torvalds's avatar
Linus Torvalds committed
1228 1229 1230
	if (rate >= 128000) {
		hdsp->channel_map = channel_map_H9632_qs;
	} else if (rate > 48000) {
Takashi Iwai's avatar
Takashi Iwai committed
1231
		if (hdsp->io_type == H9632)
Linus Torvalds's avatar
Linus Torvalds committed
1232
			hdsp->channel_map = channel_map_H9632_ds;
Takashi Iwai's avatar
Takashi Iwai committed
1233
		else
Linus Torvalds's avatar
Linus Torvalds committed
1234 1235 1236
			hdsp->channel_map = channel_map_ds;
	} else {
		switch (hdsp->io_type) {
1237
		case RPM:
Linus Torvalds's avatar
Linus Torvalds committed
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
		case Multiface:
			hdsp->channel_map = channel_map_mf_ss;
			break;
		case Digiface:
		case H9652:
			hdsp->channel_map = channel_map_df_ss;
			break;
		case H9632:
			hdsp->channel_map = channel_map_H9632_ss;
			break;
		default:
			/* should never happen */
			break;
		}
	}
1253

Linus Torvalds's avatar
Linus Torvalds committed
1254 1255 1256 1257 1258 1259 1260 1261 1262
	hdsp->system_sample_rate = rate;

	return 0;
}

/*----------------------------------------------------------------------------
   MIDI
  ----------------------------------------------------------------------------*/

1263
static unsigned char snd_hdsp_midi_read_byte (struct hdsp *hdsp, int id)
Linus Torvalds's avatar
Linus Torvalds committed
1264 1265
{
	/* the hardware already does the relevant bit-mask with 0xff */
Takashi Iwai's avatar
Takashi Iwai committed
1266
	if (id)
Linus Torvalds's avatar
Linus Torvalds committed
1267
		return hdsp_read(hdsp, HDSP_midiDataIn1);
Takashi Iwai's avatar
Takashi Iwai committed
1268
	else
Linus Torvalds's avatar
Linus Torvalds committed
1269 1270 1271
		return hdsp_read(hdsp, HDSP_midiDataIn0);
}

1272
static void snd_hdsp_midi_write_byte (struct hdsp *hdsp, int id, int val)
Linus Torvalds's avatar
Linus Torvalds committed
1273 1274
{
	/* the hardware already does the relevant bit-mask with 0xff */
Takashi Iwai's avatar
Takashi Iwai committed
1275
	if (id)
Linus Torvalds's avatar
Linus Torvalds committed
1276
		hdsp_write(hdsp, HDSP_midiDataOut1, val);
Takashi Iwai's avatar
Takashi Iwai committed
1277
	else
Linus Torvalds's avatar
Linus Torvalds committed
1278 1279 1280
		hdsp_write(hdsp, HDSP_midiDataOut0, val);
}

1281
static int snd_hdsp_midi_input_available (struct hdsp *hdsp, int id)
Linus Torvalds's avatar
Linus Torvalds committed
1282
{
Takashi Iwai's avatar
Takashi Iwai committed
1283
	if (id)
Linus Torvalds's avatar
Linus Torvalds committed
1284
		return (hdsp_read(hdsp, HDSP_midiStatusIn1) & 0xff);
Takashi Iwai's avatar
Takashi Iwai committed
1285
	else
Linus Torvalds's avatar
Linus Torvalds committed
1286 1287 1288
		return (hdsp_read(hdsp, HDSP_midiStatusIn0) & 0xff);
}

1289
static int snd_hdsp_midi_output_possible (struct hdsp *hdsp, int id)
Linus Torvalds's avatar
Linus Torvalds committed
1290 1291 1292
{
	int fifo_bytes_used;

Takashi Iwai's avatar
Takashi Iwai committed
1293
	if (id)
Linus Torvalds's avatar
Linus Torvalds committed
1294
		fifo_bytes_used = hdsp_read(hdsp, HDSP_midiStatusOut1) & 0xff;
Takashi Iwai's avatar
Takashi Iwai committed
1295
	else
Linus Torvalds's avatar
Linus Torvalds committed
1296 1297
		fifo_bytes_used = hdsp_read(hdsp, HDSP_midiStatusOut0) & 0xff;

Takashi Iwai's avatar
Takashi Iwai committed
1298
	if (fifo_bytes_used < 128)
Linus Torvalds's avatar
Linus Torvalds committed
1299
		return  128 - fifo_bytes_used;
Takashi Iwai's avatar
Takashi Iwai committed
1300
	else
Linus Torvalds's avatar
Linus Torvalds committed
1301 1302 1303
		return 0;
}

1304
static void snd_hdsp_flush_midi_input (struct hdsp *hdsp, int id)
Linus Torvalds's avatar
Linus Torvalds committed
1305
{
Takashi Iwai's avatar
Takashi Iwai committed
1306
	while (snd_hdsp_midi_input_available (hdsp, id))
Linus Torvalds's avatar
Linus Torvalds committed
1307 1308 1309
		snd_hdsp_midi_read_byte (hdsp, id);
}

1310
static int snd_hdsp_midi_output_write (struct hdsp_midi *hmidi)
Linus Torvalds's avatar
Linus Torvalds committed
1311 1312 1313 1314 1315 1316 1317 1318
{
	unsigned long flags;
	int n_pending;
	int to_write;
	int i;
	unsigned char buf[128];

	/* Output is not interrupt driven */
1319

Linus Torvalds's avatar
Linus Torvalds committed
1320 1321 1322 1323 1324 1325
	spin_lock_irqsave (&hmidi->lock, flags);
	if (hmidi->output) {
		if (!snd_rawmidi_transmit_empty (hmidi->output)) {
			if ((n_pending = snd_hdsp_midi_output_possible (hmidi->hdsp, hmidi->id)) > 0) {
				if (n_pending > (int)sizeof (buf))
					n_pending = sizeof (buf);
1326

Linus Torvalds's avatar
Linus Torvalds committed
1327
				if ((to_write = snd_rawmidi_transmit (hmidi->output, buf, n_pending)) > 0) {
1328
					for (i = 0; i < to_write; ++i)
Linus Torvalds's avatar
Linus Torvalds committed
1329 1330 1331 1332 1333 1334 1335 1336 1337
						snd_hdsp_midi_write_byte (hmidi->hdsp, hmidi->id, buf[i]);
				}
			}
		}
	}
	spin_unlock_irqrestore (&hmidi->lock, flags);
	return 0;
}

1338
static int snd_hdsp_midi_input_read (struct hdsp_midi *hmidi)
Linus Torvalds's avatar
Linus Torvalds committed
1339 1340 1341 1342 1343 1344 1345 1346 1347
{
	unsigned char buf[128]; /* this buffer is designed to match the MIDI input FIFO size */
	unsigned long flags;
	int n_pending;
	int i;

	spin_lock_irqsave (&hmidi->lock, flags);
	if ((n_pending = snd_hdsp_midi_input_available (hmidi->hdsp, hmidi->id)) > 0) {
		if (hmidi->input) {
Takashi Iwai's avatar
Takashi Iwai committed
1348
			if (n_pending > (int)sizeof (buf))
Linus Torvalds's avatar
Linus Torvalds committed
1349
				n_pending = sizeof (buf);
Takashi Iwai's avatar
Takashi Iwai committed
1350
			for (i = 0; i < n_pending; ++i)
Linus Torvalds's avatar
Linus Torvalds committed
1351
				buf[i] = snd_hdsp_midi_read_byte (hmidi->hdsp, hmidi->id);
Takashi Iwai's avatar
Takashi Iwai committed
1352
			if (n_pending)
Linus Torvalds's avatar
Linus Torvalds committed
1353 1354 1355
				snd_rawmidi_receive (hmidi->input, buf, n_pending);
		} else {
			/* flush the MIDI input FIFO */
Takashi Iwai's avatar
Takashi Iwai committed
1356
			while (--n_pending)
Linus Torvalds's avatar
Linus Torvalds committed
1357 1358 1359 1360
				snd_hdsp_midi_read_byte (hmidi->hdsp, hmidi->id);
		}
	}
	hmidi->pending = 0;
Takashi Iwai's avatar
Takashi Iwai committed
1361
	if (hmidi->id)
Linus Torvalds's avatar
Linus Torvalds committed
1362
		hmidi->hdsp->control_register |= HDSP_Midi1InterruptEnable;
Takashi Iwai's avatar
Takashi Iwai committed
1363
	else
Linus Torvalds's avatar
Linus Torvalds committed
1364 1365 1366 1367 1368 1369
		hmidi->hdsp->control_register |= HDSP_Midi0InterruptEnable;
	hdsp_write(hmidi->hdsp, HDSP_controlRegister, hmidi->hdsp->control_register);
	spin_unlock_irqrestore (&hmidi->lock, flags);
	return snd_hdsp_midi_output_write (hmidi);
}

1370
static void snd_hdsp_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds's avatar
Linus Torvalds committed
1371
{
1372 1373
	struct hdsp *hdsp;
	struct hdsp_midi *hmidi;
Linus Torvalds's avatar
Linus Torvalds committed
1374 1375 1376
	unsigned long flags;
	u32 ie;

1377
	hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
Linus Torvalds's avatar
Linus Torvalds committed
1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394
	hdsp = hmidi->hdsp;
	ie = hmidi->id ? HDSP_Midi1InterruptEnable : HDSP_Midi0InterruptEnable;
	spin_lock_irqsave (&hdsp->lock, flags);
	if (up) {
		if (!(hdsp->control_register & ie)) {
			snd_hdsp_flush_midi_input (hdsp, hmidi->id);
			hdsp->control_register |= ie;
		}
	} else {
		hdsp->control_register &= ~ie;
		tasklet_kill(&hdsp->midi_tasklet);
	}

	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
	spin_unlock_irqrestore (&hdsp->lock, flags);
}

1395
static void snd_hdsp_midi_output_timer(struct timer_list *t)
Linus Torvalds's avatar
Linus Torvalds committed
1396
{
1397
	struct hdsp_midi *hmidi = from_timer(hmidi, t, timer);
Linus Torvalds's avatar
Linus Torvalds committed
1398
	unsigned long flags;
1399

Linus Torvalds's avatar
Linus Torvalds committed
1400 1401 1402 1403 1404 1405
	snd_hdsp_midi_output_write(hmidi);
	spin_lock_irqsave (&hmidi->lock, flags);

	/* this does not bump hmidi->istimer, because the
	   kernel automatically removed the timer when it
	   expired, and we are now adding it back, thus
1406
	   leaving istimer wherever it was set before.
Linus Torvalds's avatar
Linus Torvalds committed
1407 1408
	*/

1409 1410
	if (hmidi->istimer)
		mod_timer(&hmidi->timer, 1 + jiffies);
Linus Torvalds's avatar
Linus Torvalds committed
1411 1412 1413 1414

	spin_unlock_irqrestore (&hmidi->lock, flags);
}

1415
static void snd_hdsp_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds's avatar
Linus Torvalds committed
1416
{
1417
	struct hdsp_midi *hmidi;
Linus Torvalds's avatar
Linus Torvalds committed
1418 1419
	unsigned long flags;

1420
	hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
Linus Torvalds's avatar
Linus Torvalds committed
1421 1422 1423
	spin_lock_irqsave (&hmidi->lock, flags);
	if (up) {
		if (!hmidi->istimer) {
1424 1425
			timer_setup(&hmidi->timer, snd_hdsp_midi_output_timer,
				    0);
1426
			mod_timer(&hmidi->timer, 1 + jiffies);
Linus Torvalds's avatar
Linus Torvalds committed
1427 1428 1429
			hmidi->istimer++;
		}
	} else {
Takashi Iwai's avatar
Takashi Iwai committed
1430
		if (hmidi->istimer && --hmidi->istimer <= 0)
Linus Torvalds's avatar
Linus Torvalds committed
1431 1432 1433 1434 1435 1436 1437
			del_timer (&hmidi->timer);
	}
	spin_unlock_irqrestore (&hmidi->lock, flags);
	if (up)
		snd_hdsp_midi_output_write(hmidi);
}

1438
static int snd_hdsp_midi_input_open(struct snd_rawmidi_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
1439
{
1440
	struct hdsp_midi *hmidi;
Linus Torvalds's avatar
Linus Torvalds committed
1441

1442
	hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
Linus Torvalds's avatar
Linus Torvalds committed
1443 1444 1445 1446 1447 1448 1449 1450
	spin_lock_irq (&hmidi->lock);
	snd_hdsp_flush_midi_input (hmidi->hdsp, hmidi->id);
	hmidi->input = substream;
	spin_unlock_irq (&hmidi->lock);

	return 0;
}

1451
static int snd_hdsp_midi_output_open(struct snd_rawmidi_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
1452
{
1453
	struct hdsp_midi *hmidi;
Linus Torvalds's avatar
Linus Torvalds committed
1454

1455
	hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
Linus Torvalds's avatar
Linus Torvalds committed
1456 1457 1458 1459 1460 1461 1462
	spin_lock_irq (&hmidi->lock);
	hmidi->output = substream;
	spin_unlock_irq (&hmidi->lock);

	return 0;
}

1463
static int snd_hdsp_midi_input_close(struct snd_rawmidi_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
1464
{
1465
	struct hdsp_midi *hmidi;
Linus Torvalds's avatar
Linus Torvalds committed
1466 1467 1468

	snd_hdsp_midi_input_trigger (substream, 0);

1469
	hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
Linus Torvalds's avatar
Linus Torvalds committed
1470 1471 1472 1473 1474 1475 1476
	spin_lock_irq (&hmidi->lock);
	hmidi->input = NULL;
	spin_unlock_irq (&hmidi->lock);

	return 0;
}

1477
static int snd_hdsp_midi_output_close(struct snd_rawmidi_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
1478
{
1479
	struct hdsp_midi *hmidi;
Linus Torvalds's avatar
Linus Torvalds committed
1480 1481 1482

	snd_hdsp_midi_output_trigger (substream, 0);

1483
	hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
Linus Torvalds's avatar
Linus Torvalds committed
1484 1485 1486 1487 1488 1489 1490
	spin_lock_irq (&hmidi->lock);
	hmidi->output = NULL;
	spin_unlock_irq (&hmidi->lock);

	return 0;
}

1491
static const struct snd_rawmidi_ops snd_hdsp_midi_output =
Linus Torvalds's avatar
Linus Torvalds committed
1492 1493 1494 1495 1496 1497
{
	.open =		snd_hdsp_midi_output_open,
	.close =	snd_hdsp_midi_output_close,
	.trigger =	snd_hdsp_midi_output_trigger,
};

1498
static const struct snd_rawmidi_ops snd_hdsp_midi_input =
Linus Torvalds's avatar
Linus Torvalds committed
1499 1500 1501 1502 1503 1504
{
	.open =		snd_hdsp_midi_input_open,
	.close =	snd_hdsp_midi_input_close,
	.trigger =	snd_hdsp_midi_input_trigger,
};

1505
static int snd_hdsp_create_midi (struct snd_card *card, struct hdsp *hdsp, int id)
Linus Torvalds's avatar
Linus Torvalds committed
1506
{
1507
	char buf[40];
Linus Torvalds's avatar
Linus Torvalds committed
1508 1509 1510 1511 1512 1513 1514 1515 1516 1517

	hdsp->midi[id].id = id;
	hdsp->midi[id].rmidi = NULL;
	hdsp->midi[id].input = NULL;
	hdsp->midi[id].output = NULL;
	hdsp->midi[id].hdsp = hdsp;
	hdsp->midi[id].istimer = 0;
	hdsp->midi[id].pending = 0;
	spin_lock_init (&hdsp->midi[id].lock);

1518
	snprintf(buf, sizeof(buf), "%s MIDI %d", card->shortname, id + 1);
Takashi Iwai's avatar
Takashi Iwai committed
1519
	if (snd_rawmidi_new (card, buf, id, 1, 1, &hdsp->midi[id].rmidi) < 0)
Linus Torvalds's avatar
Linus Torvalds committed
1520 1521
		return -1;

1522
	sprintf(hdsp->midi[id].rmidi->name, "HDSP MIDI %d", id+1);
Linus Torvalds's avatar
Linus Torvalds committed
1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538
	hdsp->midi[id].rmidi->private_data = &hdsp->midi[id];

	snd_rawmidi_set_ops (hdsp->midi[id].rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_hdsp_midi_output);
	snd_rawmidi_set_ops (hdsp->midi[id].rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_hdsp_midi_input);

	hdsp->midi[id].rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT |
		SNDRV_RAWMIDI_INFO_INPUT |
		SNDRV_RAWMIDI_INFO_DUPLEX;

	return 0;
}

/*-----------------------------------------------------------------------------
  Control Interface
  ----------------------------------------------------------------------------*/

1539
static u32 snd_hdsp_convert_from_aes(struct snd_aes_iec958 *aes)
Linus Torvalds's avatar
Linus Torvalds committed
1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550
{
	u32 val = 0;
	val |= (aes->status[0] & IEC958_AES0_PROFESSIONAL) ? HDSP_SPDIFProfessional : 0;
	val |= (aes->status[0] & IEC958_AES0_NONAUDIO) ? HDSP_SPDIFNonAudio : 0;
	if (val & HDSP_SPDIFProfessional)
		val |= (aes->status[0] & IEC958_AES0_PRO_EMPHASIS_5015) ? HDSP_SPDIFEmphasis : 0;
	else
		val |= (aes->status[0] & IEC958_AES0_CON_EMPHASIS_5015) ? HDSP_SPDIFEmphasis : 0;
	return val;
}

1551
static void snd_hdsp_convert_to_aes(struct snd_aes_iec958 *aes, u32 val)
Linus Torvalds's avatar
Linus Torvalds committed
1552 1553 1554 1555 1556 1557 1558 1559 1560
{
	aes->status[0] = ((val & HDSP_SPDIFProfessional) ? IEC958_AES0_PROFESSIONAL : 0) |
			 ((val & HDSP_SPDIFNonAudio) ? IEC958_AES0_NONAUDIO : 0);
	if (val & HDSP_SPDIFProfessional)
		aes->status[0] |= (val & HDSP_SPDIFEmphasis) ? IEC958_AES0_PRO_EMPHASIS_5015 : 0;
	else
		aes->status[0] |= (val & HDSP_SPDIFEmphasis) ? IEC958_AES0_CON_EMPHASIS_5015 : 0;
}

1561
static int snd_hdsp_control_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds's avatar
Linus Torvalds committed
1562 1563 1564 1565 1566 1567
{
	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
	uinfo->count = 1;
	return 0;
}

1568
static int snd_hdsp_control_spdif_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
1569
{
1570
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
1571

Linus Torvalds's avatar
Linus Torvalds committed
1572 1573 1574 1575
	snd_hdsp_convert_to_aes(&ucontrol->value.iec958, hdsp->creg_spdif);
	return 0;
}

1576
static int snd_hdsp_control_spdif_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
1577
{
1578
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
1579 1580
	int change;
	u32 val;
1581

Linus Torvalds's avatar
Linus Torvalds committed
1582 1583 1584 1585 1586 1587 1588 1589
	val = snd_hdsp_convert_from_aes(&ucontrol->value.iec958);
	spin_lock_irq(&hdsp->lock);
	change = val != hdsp->creg_spdif;
	hdsp->creg_spdif = val;
	spin_unlock_irq(&hdsp->lock);
	return change;
}

1590
static int snd_hdsp_control_spdif_stream_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds's avatar
Linus Torvalds committed
1591 1592 1593 1594 1595 1596
{
	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
	uinfo->count = 1;
	return 0;
}

1597
static int snd_hdsp_control_spdif_stream_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
1598
{
1599
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
1600

Linus Torvalds's avatar
Linus Torvalds committed
1601 1602 1603 1604
	snd_hdsp_convert_to_aes(&ucontrol->value.iec958, hdsp->creg_spdif_stream);
	return 0;
}

1605
static int snd_hdsp_control_spdif_stream_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
1606
{
1607
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
1608 1609
	int change;
	u32 val;
1610

Linus Torvalds's avatar
Linus Torvalds committed
1611 1612 1613 1614 1615 1616 1617 1618 1619 1620
	val = snd_hdsp_convert_from_aes(&ucontrol->value.iec958);
	spin_lock_irq(&hdsp->lock);
	change = val != hdsp->creg_spdif_stream;
	hdsp->creg_spdif_stream = val;
	hdsp->control_register &= ~(HDSP_SPDIFProfessional | HDSP_SPDIFNonAudio | HDSP_SPDIFEmphasis);
	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register |= val);
	spin_unlock_irq(&hdsp->lock);
	return change;
}

1621
static int snd_hdsp_control_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds's avatar
Linus Torvalds committed
1622 1623 1624 1625 1626 1627
{
	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
	uinfo->count = 1;
	return 0;
}

1628
static int snd_hdsp_control_spdif_mask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
1629 1630 1631 1632 1633 1634
{
	ucontrol->value.iec958.status[0] = kcontrol->private_value;
	return 0;
}

#define HDSP_SPDIF_IN(xname, xindex) \
1635
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds's avatar
Linus Torvalds committed
1636 1637 1638 1639 1640 1641
  .name = xname, \
  .index = xindex, \
  .info = snd_hdsp_info_spdif_in, \
  .get = snd_hdsp_get_spdif_in, \
  .put = snd_hdsp_put_spdif_in }

1642
static unsigned int hdsp_spdif_in(struct hdsp *hdsp)
Linus Torvalds's avatar
Linus Torvalds committed
1643 1644 1645 1646
{
	return hdsp_decode_spdif_in(hdsp->control_register & HDSP_SPDIFInputMask);
}

1647
static int hdsp_set_spdif_input(struct hdsp *hdsp, int in)
Linus Torvalds's avatar
Linus Torvalds committed
1648 1649 1650 1651 1652 1653 1654
{
	hdsp->control_register &= ~HDSP_SPDIFInputMask;
	hdsp->control_register |= hdsp_encode_spdif_in(in);
	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
	return 0;
}

1655
static int snd_hdsp_info_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds's avatar
Linus Torvalds committed
1656
{
1657 1658 1659
	static const char * const texts[4] = {
		"Optical", "Coaxial", "Internal", "AES"
	};
1660
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
1661

1662 1663
	return snd_ctl_enum_info(uinfo, 1, (hdsp->io_type == H9632) ? 4 : 3,
				 texts);
Linus Torvalds's avatar
Linus Torvalds committed
1664 1665
}

1666
static int snd_hdsp_get_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
1667
{
1668
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
1669

Linus Torvalds's avatar
Linus Torvalds committed
1670 1671 1672 1673
	ucontrol->value.enumerated.item[0] = hdsp_spdif_in(hdsp);
	return 0;
}

1674
static int snd_hdsp_put_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
1675
{
1676
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
1677 1678
	int change;
	unsigned int val;
1679

Linus Torvalds's avatar
Linus Torvalds committed
1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690
	if (!snd_hdsp_use_is_exclusive(hdsp))
		return -EBUSY;
	val = ucontrol->value.enumerated.item[0] % ((hdsp->io_type == H9632) ? 4 : 3);
	spin_lock_irq(&hdsp->lock);
	change = val != hdsp_spdif_in(hdsp);
	if (change)
		hdsp_set_spdif_input(hdsp, val);
	spin_unlock_irq(&hdsp->lock);
	return change;
}

1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748
#define HDSP_TOGGLE_SETTING(xname, xindex) \
{   .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
	.name = xname, \
	.private_value = xindex, \
	.info = snd_hdsp_info_toggle_setting, \
	.get = snd_hdsp_get_toggle_setting, \
	.put = snd_hdsp_put_toggle_setting \
}

static int hdsp_toggle_setting(struct hdsp *hdsp, u32 regmask)
{
	return (hdsp->control_register & regmask) ? 1 : 0;
}

static int hdsp_set_toggle_setting(struct hdsp *hdsp, u32 regmask, int out)
{
	if (out)
		hdsp->control_register |= regmask;
	else
		hdsp->control_register &= ~regmask;
	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);

	return 0;
}

#define snd_hdsp_info_toggle_setting		   snd_ctl_boolean_mono_info

static int snd_hdsp_get_toggle_setting(struct snd_kcontrol *kcontrol,
		struct snd_ctl_elem_value *ucontrol)
{
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
	u32 regmask = kcontrol->private_value;

	spin_lock_irq(&hdsp->lock);
	ucontrol->value.integer.value[0] = hdsp_toggle_setting(hdsp, regmask);
	spin_unlock_irq(&hdsp->lock);
	return 0;
}

static int snd_hdsp_put_toggle_setting(struct snd_kcontrol *kcontrol,
		struct snd_ctl_elem_value *ucontrol)
{
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
	u32 regmask = kcontrol->private_value;
	int change;
	unsigned int val;

	if (!snd_hdsp_use_is_exclusive(hdsp))
		return -EBUSY;
	val = ucontrol->value.integer.value[0] & 1;
	spin_lock_irq(&hdsp->lock);
	change = (int) val != hdsp_toggle_setting(hdsp, regmask);
	if (change)
		hdsp_set_toggle_setting(hdsp, regmask, val);
	spin_unlock_irq(&hdsp->lock);
	return change;
}

Linus Torvalds's avatar
Linus Torvalds committed
1749
#define HDSP_SPDIF_SAMPLE_RATE(xname, xindex) \
1750
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds's avatar
Linus Torvalds committed
1751 1752 1753 1754 1755 1756 1757
  .name = xname, \
  .index = xindex, \
  .access = SNDRV_CTL_ELEM_ACCESS_READ, \
  .info = snd_hdsp_info_spdif_sample_rate, \
  .get = snd_hdsp_get_spdif_sample_rate \
}

1758
static int snd_hdsp_info_spdif_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds's avatar
Linus Torvalds committed
1759
{
1760 1761 1762 1763
	static const char * const texts[] = {
		"32000", "44100", "48000", "64000", "88200", "96000",
		"None", "128000", "176400", "192000"
	};
1764
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
1765

1766 1767
	return snd_ctl_enum_info(uinfo, 1, (hdsp->io_type == H9632) ? 10 : 7,
				 texts);
Linus Torvalds's avatar
Linus Torvalds committed
1768 1769
}

1770
static int snd_hdsp_get_spdif_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
1771
{
1772
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
1773

Linus Torvalds's avatar
Linus Torvalds committed
1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802
	switch (hdsp_spdif_sample_rate(hdsp)) {
	case 32000:
		ucontrol->value.enumerated.item[0] = 0;
		break;
	case 44100:
		ucontrol->value.enumerated.item[0] = 1;
		break;
	case 48000:
		ucontrol->value.enumerated.item[0] = 2;
		break;
	case 64000:
		ucontrol->value.enumerated.item[0] = 3;
		break;
	case 88200:
		ucontrol->value.enumerated.item[0] = 4;
		break;
	case 96000:
		ucontrol->value.enumerated.item[0] = 5;
		break;
	case 128000:
		ucontrol->value.enumerated.item[0] = 7;
		break;
	case 176400:
		ucontrol->value.enumerated.item[0] = 8;
		break;
	case 192000:
		ucontrol->value.enumerated.item[0] = 9;
		break;
	default:
1803
		ucontrol->value.enumerated.item[0] = 6;
Linus Torvalds's avatar
Linus Torvalds committed
1804 1805 1806 1807 1808
	}
	return 0;
}

#define HDSP_SYSTEM_SAMPLE_RATE(xname, xindex) \
1809
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds's avatar
Linus Torvalds committed
1810 1811 1812 1813 1814 1815 1816
  .name = xname, \
  .index = xindex, \
  .access = SNDRV_CTL_ELEM_ACCESS_READ, \
  .info = snd_hdsp_info_system_sample_rate, \
  .get = snd_hdsp_get_system_sample_rate \
}

1817
static int snd_hdsp_info_system_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds's avatar
Linus Torvalds committed
1818 1819 1820 1821 1822 1823
{
	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
	uinfo->count = 1;
	return 0;
}

1824
static int snd_hdsp_get_system_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
1825
{
1826
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
1827

Linus Torvalds's avatar
Linus Torvalds committed
1828 1829 1830 1831 1832
	ucontrol->value.enumerated.item[0] = hdsp->system_sample_rate;
	return 0;
}

#define HDSP_AUTOSYNC_SAMPLE_RATE(xname, xindex) \
1833
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds's avatar
Linus Torvalds committed
1834 1835 1836 1837 1838 1839 1840
  .name = xname, \
  .index = xindex, \
  .access = SNDRV_CTL_ELEM_ACCESS_READ, \
  .info = snd_hdsp_info_autosync_sample_rate, \
  .get = snd_hdsp_get_autosync_sample_rate \
}

1841
static int snd_hdsp_info_autosync_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds's avatar
Linus Torvalds committed
1842
{
1843
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
1844 1845 1846 1847 1848 1849 1850
	static const char * const texts[] = {
		"32000", "44100", "48000", "64000", "88200", "96000",
		"None", "128000", "176400", "192000"
	};

	return snd_ctl_enum_info(uinfo, 1, (hdsp->io_type == H9632) ? 10 : 7,
				 texts);
Linus Torvalds's avatar
Linus Torvalds committed
1851 1852
}

1853
static int snd_hdsp_get_autosync_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
1854
{
1855
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
1856

Linus Torvalds's avatar
Linus Torvalds committed
1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883
	switch (hdsp_external_sample_rate(hdsp)) {
	case 32000:
		ucontrol->value.enumerated.item[0] = 0;
		break;
	case 44100:
		ucontrol->value.enumerated.item[0] = 1;
		break;
	case 48000:
		ucontrol->value.enumerated.item[0] = 2;
		break;
	case 64000:
		ucontrol->value.enumerated.item[0] = 3;
		break;
	case 88200:
		ucontrol->value.enumerated.item[0] = 4;
		break;
	case 96000:
		ucontrol->value.enumerated.item[0] = 5;
		break;
	case 128000:
		ucontrol->value.enumerated.item[0] = 7;
		break;
	case 176400:
		ucontrol->value.enumerated.item[0] = 8;
		break;
	case 192000:
		ucontrol->value.enumerated.item[0] = 9;
1884
		break;
Linus Torvalds's avatar
Linus Torvalds committed
1885
	default:
1886
		ucontrol->value.enumerated.item[0] = 6;
Linus Torvalds's avatar
Linus Torvalds committed
1887 1888 1889 1890 1891
	}
	return 0;
}

#define HDSP_SYSTEM_CLOCK_MODE(xname, xindex) \
1892
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds's avatar
Linus Torvalds committed
1893 1894 1895 1896 1897 1898 1899
  .name = xname, \
  .index = xindex, \
  .access = SNDRV_CTL_ELEM_ACCESS_READ, \
  .info = snd_hdsp_info_system_clock_mode, \
  .get = snd_hdsp_get_system_clock_mode \
}

1900
static int hdsp_system_clock_mode(struct hdsp *hdsp)
Linus Torvalds's avatar
Linus Torvalds committed
1901
{
Takashi Iwai's avatar
Takashi Iwai committed
1902
	if (hdsp->control_register & HDSP_ClockModeMaster)
Linus Torvalds's avatar
Linus Torvalds committed
1903
		return 0;
Takashi Iwai's avatar
Takashi Iwai committed
1904
	else if (hdsp_external_sample_rate(hdsp) != hdsp->system_sample_rate)
Linus Torvalds's avatar
Linus Torvalds committed
1905 1906 1907 1908
			return 0;
	return 1;
}

1909
static int snd_hdsp_info_system_clock_mode(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds's avatar
Linus Torvalds committed
1910
{
1911
	static const char * const texts[] = {"Master", "Slave" };
1912

1913
	return snd_ctl_enum_info(uinfo, 1, 2, texts);
Linus Torvalds's avatar
Linus Torvalds committed
1914 1915
}

1916
static int snd_hdsp_get_system_clock_mode(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
1917
{
1918
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
1919

Linus Torvalds's avatar
Linus Torvalds committed
1920 1921 1922 1923 1924
	ucontrol->value.enumerated.item[0] = hdsp_system_clock_mode(hdsp);
	return 0;
}

#define HDSP_CLOCK_SOURCE(xname, xindex) \
1925
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds's avatar
Linus Torvalds committed
1926 1927 1928 1929 1930 1931 1932
  .name = xname, \
  .index = xindex, \
  .info = snd_hdsp_info_clock_source, \
  .get = snd_hdsp_get_clock_source, \
  .put = snd_hdsp_put_clock_source \
}

1933
static int hdsp_clock_source(struct hdsp *hdsp)
Linus Torvalds's avatar
Linus Torvalds committed
1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955
{
	if (hdsp->control_register & HDSP_ClockModeMaster) {
		switch (hdsp->system_sample_rate) {
		case 32000:
			return 1;
		case 44100:
			return 2;
		case 48000:
			return 3;
		case 64000:
			return 4;
		case 88200:
			return 5;
		case 96000:
			return 6;
		case 128000:
			return 7;
		case 176400:
			return 8;
		case 192000:
			return 9;
		default:
1956
			return 3;
Linus Torvalds's avatar
Linus Torvalds committed
1957 1958 1959 1960 1961 1962
		}
	} else {
		return 0;
	}
}

1963
static int hdsp_set_clock_source(struct hdsp *hdsp, int mode)
Linus Torvalds's avatar
Linus Torvalds committed
1964 1965 1966 1967 1968 1969
{
	int rate;
	switch (mode) {
	case HDSP_CLOCK_SOURCE_AUTOSYNC:
		if (hdsp_external_sample_rate(hdsp) != 0) {
		    if (!hdsp_set_rate(hdsp, hdsp_external_sample_rate(hdsp), 1)) {
1970
			hdsp->control_register &= ~HDSP_ClockModeMaster;
Linus Torvalds's avatar
Linus Torvalds committed
1971 1972 1973 1974 1975 1976 1977 1978 1979 1980
			hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
			return 0;
		    }
		}
		return -1;
	case HDSP_CLOCK_SOURCE_INTERNAL_32KHZ:
		rate = 32000;
		break;
	case HDSP_CLOCK_SOURCE_INTERNAL_44_1KHZ:
		rate = 44100;
1981
		break;
Linus Torvalds's avatar
Linus Torvalds committed
1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011
	case HDSP_CLOCK_SOURCE_INTERNAL_48KHZ:
		rate = 48000;
		break;
	case HDSP_CLOCK_SOURCE_INTERNAL_64KHZ:
		rate = 64000;
		break;
	case HDSP_CLOCK_SOURCE_INTERNAL_88_2KHZ:
		rate = 88200;
		break;
	case HDSP_CLOCK_SOURCE_INTERNAL_96KHZ:
		rate = 96000;
		break;
	case HDSP_CLOCK_SOURCE_INTERNAL_128KHZ:
		rate = 128000;
		break;
	case HDSP_CLOCK_SOURCE_INTERNAL_176_4KHZ:
		rate = 176400;
		break;
	case HDSP_CLOCK_SOURCE_INTERNAL_192KHZ:
		rate = 192000;
		break;
	default:
		rate = 48000;
	}
	hdsp->control_register |= HDSP_ClockModeMaster;
	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
	hdsp_set_rate(hdsp, rate, 1);
	return 0;
}

2012
static int snd_hdsp_info_clock_source(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds's avatar
Linus Torvalds committed
2013
{
2014 2015 2016 2017 2018 2019
	static const char * const 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"
	};
2020
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2021

2022 2023
	return snd_ctl_enum_info(uinfo, 1, (hdsp->io_type == H9632) ? 10 : 7,
				 texts);
Linus Torvalds's avatar
Linus Torvalds committed
2024 2025
}

2026
static int snd_hdsp_get_clock_source(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
2027
{
2028
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2029

Linus Torvalds's avatar
Linus Torvalds committed
2030 2031 2032 2033
	ucontrol->value.enumerated.item[0] = hdsp_clock_source(hdsp);
	return 0;
}

2034
static int snd_hdsp_put_clock_source(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
2035
{
2036
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
2037 2038
	int change;
	int val;
2039

Linus Torvalds's avatar
Linus Torvalds committed
2040 2041 2042 2043 2044
	if (!snd_hdsp_use_is_exclusive(hdsp))
		return -EBUSY;
	val = ucontrol->value.enumerated.item[0];
	if (val < 0) val = 0;
	if (hdsp->io_type == H9632) {
Takashi Iwai's avatar
Takashi Iwai committed
2045 2046
		if (val > 9)
			val = 9;
Linus Torvalds's avatar
Linus Torvalds committed
2047
	} else {
Takashi Iwai's avatar
Takashi Iwai committed
2048 2049
		if (val > 6)
			val = 6;
Linus Torvalds's avatar
Linus Torvalds committed
2050 2051
	}
	spin_lock_irq(&hdsp->lock);
Takashi Iwai's avatar
Takashi Iwai committed
2052
	if (val != hdsp_clock_source(hdsp))
Linus Torvalds's avatar
Linus Torvalds committed
2053
		change = (hdsp_set_clock_source(hdsp, val) == 0) ? 1 : 0;
Takashi Iwai's avatar
Takashi Iwai committed
2054
	else
Linus Torvalds's avatar
Linus Torvalds committed
2055 2056 2057 2058 2059
		change = 0;
	spin_unlock_irq(&hdsp->lock);
	return change;
}

2060
#define snd_hdsp_info_clock_source_lock		snd_ctl_boolean_mono_info
2061

2062
static int snd_hdsp_get_clock_source_lock(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2063
{
2064
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2065

2066 2067 2068 2069
	ucontrol->value.integer.value[0] = hdsp->clock_source_locked;
	return 0;
}

2070
static int snd_hdsp_put_clock_source_lock(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2071
{
2072
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2073 2074 2075 2076
	int change;

	change = (int)ucontrol->value.integer.value[0] != hdsp->clock_source_locked;
	if (change)
2077
		hdsp->clock_source_locked = !!ucontrol->value.integer.value[0];
2078 2079 2080
	return change;
}

Linus Torvalds's avatar
Linus Torvalds committed
2081
#define HDSP_DA_GAIN(xname, xindex) \
2082
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds's avatar
Linus Torvalds committed
2083 2084 2085 2086 2087 2088 2089
  .name = xname, \
  .index = xindex, \
  .info = snd_hdsp_info_da_gain, \
  .get = snd_hdsp_get_da_gain, \
  .put = snd_hdsp_put_da_gain \
}

2090
static int hdsp_da_gain(struct hdsp *hdsp)
Linus Torvalds's avatar
Linus Torvalds committed
2091 2092 2093 2094 2095 2096 2097 2098 2099
{
	switch (hdsp->control_register & HDSP_DAGainMask) {
	case HDSP_DAGainHighGain:
		return 0;
	case HDSP_DAGainPlus4dBu:
		return 1;
	case HDSP_DAGainMinus10dBV:
		return 2;
	default:
2100
		return 1;
Linus Torvalds's avatar
Linus Torvalds committed
2101 2102 2103
	}
}

2104
static int hdsp_set_da_gain(struct hdsp *hdsp, int mode)
Linus Torvalds's avatar
Linus Torvalds committed
2105 2106 2107 2108 2109 2110 2111 2112 2113 2114
{
	hdsp->control_register &= ~HDSP_DAGainMask;
	switch (mode) {
	case 0:
		hdsp->control_register |= HDSP_DAGainHighGain;
		break;
	case 1:
		hdsp->control_register |= HDSP_DAGainPlus4dBu;
		break;
	case 2:
2115 2116
		hdsp->control_register |= HDSP_DAGainMinus10dBV;
		break;
Linus Torvalds's avatar
Linus Torvalds committed
2117 2118 2119 2120 2121 2122 2123 2124
	default:
		return -1;

	}
	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
	return 0;
}

2125
static int snd_hdsp_info_da_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds's avatar
Linus Torvalds committed
2126
{
2127
	static const char * const texts[] = {"Hi Gain", "+4 dBu", "-10 dbV"};
2128

2129
	return snd_ctl_enum_info(uinfo, 1, 3, texts);
Linus Torvalds's avatar
Linus Torvalds committed
2130 2131
}

2132
static int snd_hdsp_get_da_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
2133
{
2134
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2135

Linus Torvalds's avatar
Linus Torvalds committed
2136 2137 2138 2139
	ucontrol->value.enumerated.item[0] = hdsp_da_gain(hdsp);
	return 0;
}

2140
static int snd_hdsp_put_da_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
2141
{
2142
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
2143 2144
	int change;
	int val;
2145

Linus Torvalds's avatar
Linus Torvalds committed
2146 2147 2148 2149 2150 2151
	if (!snd_hdsp_use_is_exclusive(hdsp))
		return -EBUSY;
	val = ucontrol->value.enumerated.item[0];
	if (val < 0) val = 0;
	if (val > 2) val = 2;
	spin_lock_irq(&hdsp->lock);
Takashi Iwai's avatar
Takashi Iwai committed
2152
	if (val != hdsp_da_gain(hdsp))
Linus Torvalds's avatar
Linus Torvalds committed
2153
		change = (hdsp_set_da_gain(hdsp, val) == 0) ? 1 : 0;
Takashi Iwai's avatar
Takashi Iwai committed
2154
	else
Linus Torvalds's avatar
Linus Torvalds committed
2155 2156 2157 2158 2159 2160
		change = 0;
	spin_unlock_irq(&hdsp->lock);
	return change;
}

#define HDSP_AD_GAIN(xname, xindex) \
2161
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds's avatar
Linus Torvalds committed
2162 2163 2164 2165 2166 2167 2168
  .name = xname, \
  .index = xindex, \
  .info = snd_hdsp_info_ad_gain, \
  .get = snd_hdsp_get_ad_gain, \
  .put = snd_hdsp_put_ad_gain \
}

2169
static int hdsp_ad_gain(struct hdsp *hdsp)
Linus Torvalds's avatar
Linus Torvalds committed
2170 2171 2172 2173 2174 2175 2176 2177 2178
{
	switch (hdsp->control_register & HDSP_ADGainMask) {
	case HDSP_ADGainMinus10dBV:
		return 0;
	case HDSP_ADGainPlus4dBu:
		return 1;
	case HDSP_ADGainLowGain:
		return 2;
	default:
2179
		return 1;
Linus Torvalds's avatar
Linus Torvalds committed
2180 2181 2182
	}
}

2183
static int hdsp_set_ad_gain(struct hdsp *hdsp, int mode)
Linus Torvalds's avatar
Linus Torvalds committed
2184 2185 2186 2187 2188 2189 2190
{
	hdsp->control_register &= ~HDSP_ADGainMask;
	switch (mode) {
	case 0:
		hdsp->control_register |= HDSP_ADGainMinus10dBV;
		break;
	case 1:
2191
		hdsp->control_register |= HDSP_ADGainPlus4dBu;
Linus Torvalds's avatar
Linus Torvalds committed
2192 2193
		break;
	case 2:
2194 2195
		hdsp->control_register |= HDSP_ADGainLowGain;
		break;
Linus Torvalds's avatar
Linus Torvalds committed
2196 2197 2198 2199 2200 2201 2202 2203
	default:
		return -1;

	}
	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
	return 0;
}

2204
static int snd_hdsp_info_ad_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds's avatar
Linus Torvalds committed
2205
{
2206
	static const char * const texts[] = {"-10 dBV", "+4 dBu", "Lo Gain"};
2207

2208
	return snd_ctl_enum_info(uinfo, 1, 3, texts);
Linus Torvalds's avatar
Linus Torvalds committed
2209 2210
}

2211
static int snd_hdsp_get_ad_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
2212
{
2213
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2214

Linus Torvalds's avatar
Linus Torvalds committed
2215 2216 2217 2218
	ucontrol->value.enumerated.item[0] = hdsp_ad_gain(hdsp);
	return 0;
}

2219
static int snd_hdsp_put_ad_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
2220
{
2221
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
2222 2223
	int change;
	int val;
2224

Linus Torvalds's avatar
Linus Torvalds committed
2225 2226 2227 2228 2229 2230
	if (!snd_hdsp_use_is_exclusive(hdsp))
		return -EBUSY;
	val = ucontrol->value.enumerated.item[0];
	if (val < 0) val = 0;
	if (val > 2) val = 2;
	spin_lock_irq(&hdsp->lock);
Takashi Iwai's avatar
Takashi Iwai committed
2231
	if (val != hdsp_ad_gain(hdsp))
Linus Torvalds's avatar
Linus Torvalds committed
2232
		change = (hdsp_set_ad_gain(hdsp, val) == 0) ? 1 : 0;
Takashi Iwai's avatar
Takashi Iwai committed
2233
	else
Linus Torvalds's avatar
Linus Torvalds committed
2234 2235 2236 2237 2238 2239
		change = 0;
	spin_unlock_irq(&hdsp->lock);
	return change;
}

#define HDSP_PHONE_GAIN(xname, xindex) \
2240
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds's avatar
Linus Torvalds committed
2241 2242 2243 2244 2245 2246 2247
  .name = xname, \
  .index = xindex, \
  .info = snd_hdsp_info_phone_gain, \
  .get = snd_hdsp_get_phone_gain, \
  .put = snd_hdsp_put_phone_gain \
}

2248
static int hdsp_phone_gain(struct hdsp *hdsp)
Linus Torvalds's avatar
Linus Torvalds committed
2249 2250 2251 2252 2253 2254 2255 2256 2257
{
	switch (hdsp->control_register & HDSP_PhoneGainMask) {
	case HDSP_PhoneGain0dB:
		return 0;
	case HDSP_PhoneGainMinus6dB:
		return 1;
	case HDSP_PhoneGainMinus12dB:
		return 2;
	default:
2258
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
2259 2260 2261
	}
}

2262
static int hdsp_set_phone_gain(struct hdsp *hdsp, int mode)
Linus Torvalds's avatar
Linus Torvalds committed
2263 2264 2265 2266 2267 2268 2269
{
	hdsp->control_register &= ~HDSP_PhoneGainMask;
	switch (mode) {
	case 0:
		hdsp->control_register |= HDSP_PhoneGain0dB;
		break;
	case 1:
2270
		hdsp->control_register |= HDSP_PhoneGainMinus6dB;
Linus Torvalds's avatar
Linus Torvalds committed
2271 2272
		break;
	case 2:
2273 2274
		hdsp->control_register |= HDSP_PhoneGainMinus12dB;
		break;
Linus Torvalds's avatar
Linus Torvalds committed
2275 2276 2277 2278 2279 2280 2281 2282
	default:
		return -1;

	}
	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
	return 0;
}

2283
static int snd_hdsp_info_phone_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds's avatar
Linus Torvalds committed
2284
{
2285
	static const char * const texts[] = {"0 dB", "-6 dB", "-12 dB"};
2286

2287
	return snd_ctl_enum_info(uinfo, 1, 3, texts);
Linus Torvalds's avatar
Linus Torvalds committed
2288 2289
}

2290
static int snd_hdsp_get_phone_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
2291
{
2292
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2293

Linus Torvalds's avatar
Linus Torvalds committed
2294 2295 2296 2297
	ucontrol->value.enumerated.item[0] = hdsp_phone_gain(hdsp);
	return 0;
}

2298
static int snd_hdsp_put_phone_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
2299
{
2300
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
2301 2302
	int change;
	int val;
2303

Linus Torvalds's avatar
Linus Torvalds committed
2304 2305 2306 2307 2308 2309
	if (!snd_hdsp_use_is_exclusive(hdsp))
		return -EBUSY;
	val = ucontrol->value.enumerated.item[0];
	if (val < 0) val = 0;
	if (val > 2) val = 2;
	spin_lock_irq(&hdsp->lock);
Takashi Iwai's avatar
Takashi Iwai committed
2310
	if (val != hdsp_phone_gain(hdsp))
Linus Torvalds's avatar
Linus Torvalds committed
2311
		change = (hdsp_set_phone_gain(hdsp, val) == 0) ? 1 : 0;
Takashi Iwai's avatar
Takashi Iwai committed
2312
	else
Linus Torvalds's avatar
Linus Torvalds committed
2313 2314 2315 2316 2317 2318
		change = 0;
	spin_unlock_irq(&hdsp->lock);
	return change;
}

#define HDSP_PREF_SYNC_REF(xname, xindex) \
2319
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds's avatar
Linus Torvalds committed
2320 2321 2322 2323 2324 2325 2326
  .name = xname, \
  .index = xindex, \
  .info = snd_hdsp_info_pref_sync_ref, \
  .get = snd_hdsp_get_pref_sync_ref, \
  .put = snd_hdsp_put_pref_sync_ref \
}

2327
static int hdsp_pref_sync_ref(struct hdsp *hdsp)
Linus Torvalds's avatar
Linus Torvalds committed
2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351
{
	/* Notice that this looks at the requested sync source,
	   not the one actually in use.
	*/

	switch (hdsp->control_register & HDSP_SyncRefMask) {
	case HDSP_SyncRef_ADAT1:
		return HDSP_SYNC_FROM_ADAT1;
	case HDSP_SyncRef_ADAT2:
		return HDSP_SYNC_FROM_ADAT2;
	case HDSP_SyncRef_ADAT3:
		return HDSP_SYNC_FROM_ADAT3;
	case HDSP_SyncRef_SPDIF:
		return HDSP_SYNC_FROM_SPDIF;
	case HDSP_SyncRef_WORD:
		return HDSP_SYNC_FROM_WORD;
	case HDSP_SyncRef_ADAT_SYNC:
		return HDSP_SYNC_FROM_ADAT_SYNC;
	default:
		return HDSP_SYNC_FROM_WORD;
	}
	return 0;
}

2352
static int hdsp_set_pref_sync_ref(struct hdsp *hdsp, int pref)
Linus Torvalds's avatar
Linus Torvalds committed
2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380
{
	hdsp->control_register &= ~HDSP_SyncRefMask;
	switch (pref) {
	case HDSP_SYNC_FROM_ADAT1:
		hdsp->control_register &= ~HDSP_SyncRefMask; /* clear SyncRef bits */
		break;
	case HDSP_SYNC_FROM_ADAT2:
		hdsp->control_register |= HDSP_SyncRef_ADAT2;
		break;
	case HDSP_SYNC_FROM_ADAT3:
		hdsp->control_register |= HDSP_SyncRef_ADAT3;
		break;
	case HDSP_SYNC_FROM_SPDIF:
		hdsp->control_register |= HDSP_SyncRef_SPDIF;
		break;
	case HDSP_SYNC_FROM_WORD:
		hdsp->control_register |= HDSP_SyncRef_WORD;
		break;
	case HDSP_SYNC_FROM_ADAT_SYNC:
		hdsp->control_register |= HDSP_SyncRef_ADAT_SYNC;
		break;
	default:
		return -1;
	}
	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
	return 0;
}

2381
static int snd_hdsp_info_pref_sync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds's avatar
Linus Torvalds committed
2382
{
2383 2384 2385
	static const char * const texts[] = {
		"Word", "IEC958", "ADAT1", "ADAT Sync", "ADAT2", "ADAT3"
	};
2386
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2387
	int num_items;
Linus Torvalds's avatar
Linus Torvalds committed
2388 2389 2390 2391

	switch (hdsp->io_type) {
	case Digiface:
	case H9652:
2392
		num_items = 6;
Linus Torvalds's avatar
Linus Torvalds committed
2393 2394
		break;
	case Multiface:
2395
		num_items = 4;
Linus Torvalds's avatar
Linus Torvalds committed
2396 2397
		break;
	case H9632:
2398
		num_items = 3;
Linus Torvalds's avatar
Linus Torvalds committed
2399 2400
		break;
	default:
2401
		return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
2402
	}
2403

2404
	return snd_ctl_enum_info(uinfo, 1, num_items, texts);
Linus Torvalds's avatar
Linus Torvalds committed
2405 2406
}

2407
static int snd_hdsp_get_pref_sync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
2408
{
2409
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2410

Linus Torvalds's avatar
Linus Torvalds committed
2411 2412 2413 2414
	ucontrol->value.enumerated.item[0] = hdsp_pref_sync_ref(hdsp);
	return 0;
}

2415
static int snd_hdsp_put_pref_sync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
2416
{
2417
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
2418 2419
	int change, max;
	unsigned int val;
2420

Linus Torvalds's avatar
Linus Torvalds committed
2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447
	if (!snd_hdsp_use_is_exclusive(hdsp))
		return -EBUSY;

	switch (hdsp->io_type) {
	case Digiface:
	case H9652:
		max = 6;
		break;
	case Multiface:
		max = 4;
		break;
	case H9632:
		max = 3;
		break;
	default:
		return -EIO;
	}

	val = ucontrol->value.enumerated.item[0] % max;
	spin_lock_irq(&hdsp->lock);
	change = (int)val != hdsp_pref_sync_ref(hdsp);
	hdsp_set_pref_sync_ref(hdsp, val);
	spin_unlock_irq(&hdsp->lock);
	return change;
}

#define HDSP_AUTOSYNC_REF(xname, xindex) \
2448
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds's avatar
Linus Torvalds committed
2449 2450 2451 2452 2453 2454 2455
  .name = xname, \
  .index = xindex, \
  .access = SNDRV_CTL_ELEM_ACCESS_READ, \
  .info = snd_hdsp_info_autosync_ref, \
  .get = snd_hdsp_get_autosync_ref, \
}

2456
static int hdsp_autosync_ref(struct hdsp *hdsp)
Linus Torvalds's avatar
Linus Torvalds committed
2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468
{
	/* This looks at the autosync selected sync reference */
	unsigned int status2 = hdsp_read(hdsp, HDSP_status2Register);

	switch (status2 & HDSP_SelSyncRefMask) {
	case HDSP_SelSyncRef_WORD:
		return HDSP_AUTOSYNC_FROM_WORD;
	case HDSP_SelSyncRef_ADAT_SYNC:
		return HDSP_AUTOSYNC_FROM_ADAT_SYNC;
	case HDSP_SelSyncRef_SPDIF:
		return HDSP_AUTOSYNC_FROM_SPDIF;
	case HDSP_SelSyncRefMask:
2469
		return HDSP_AUTOSYNC_FROM_NONE;
Linus Torvalds's avatar
Linus Torvalds committed
2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481
	case HDSP_SelSyncRef_ADAT1:
		return HDSP_AUTOSYNC_FROM_ADAT1;
	case HDSP_SelSyncRef_ADAT2:
		return HDSP_AUTOSYNC_FROM_ADAT2;
	case HDSP_SelSyncRef_ADAT3:
		return HDSP_AUTOSYNC_FROM_ADAT3;
	default:
		return HDSP_AUTOSYNC_FROM_WORD;
	}
	return 0;
}

2482
static int snd_hdsp_info_autosync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds's avatar
Linus Torvalds committed
2483
{
2484 2485 2486
	static const char * const texts[] = {
		"Word", "ADAT Sync", "IEC958", "None", "ADAT1", "ADAT2", "ADAT3"
	};
2487

2488
	return snd_ctl_enum_info(uinfo, 1, 7, texts);
Linus Torvalds's avatar
Linus Torvalds committed
2489 2490
}

2491
static int snd_hdsp_get_autosync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
2492
{
2493
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2494

Linus Torvalds's avatar
Linus Torvalds committed
2495 2496 2497 2498 2499
	ucontrol->value.enumerated.item[0] = hdsp_autosync_ref(hdsp);
	return 0;
}

#define HDSP_PRECISE_POINTER(xname, xindex) \
2500
{ .iface = SNDRV_CTL_ELEM_IFACE_CARD, \
Linus Torvalds's avatar
Linus Torvalds committed
2501 2502 2503 2504 2505 2506 2507
  .name = xname, \
  .index = xindex, \
  .info = snd_hdsp_info_precise_pointer, \
  .get = snd_hdsp_get_precise_pointer, \
  .put = snd_hdsp_put_precise_pointer \
}

2508
static int hdsp_set_precise_pointer(struct hdsp *hdsp, int precise)
Linus Torvalds's avatar
Linus Torvalds committed
2509
{
Takashi Iwai's avatar
Takashi Iwai committed
2510
	if (precise)
Linus Torvalds's avatar
Linus Torvalds committed
2511
		hdsp->precise_ptr = 1;
Takashi Iwai's avatar
Takashi Iwai committed
2512
	else
Linus Torvalds's avatar
Linus Torvalds committed
2513 2514 2515 2516
		hdsp->precise_ptr = 0;
	return 0;
}

2517
#define snd_hdsp_info_precise_pointer		snd_ctl_boolean_mono_info
Linus Torvalds's avatar
Linus Torvalds committed
2518

2519
static int snd_hdsp_get_precise_pointer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
2520
{
2521
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2522

Linus Torvalds's avatar
Linus Torvalds committed
2523 2524 2525 2526 2527 2528
	spin_lock_irq(&hdsp->lock);
	ucontrol->value.integer.value[0] = hdsp->precise_ptr;
	spin_unlock_irq(&hdsp->lock);
	return 0;
}

2529
static int snd_hdsp_put_precise_pointer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
2530
{
2531
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
2532 2533
	int change;
	unsigned int val;
2534

Linus Torvalds's avatar
Linus Torvalds committed
2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545
	if (!snd_hdsp_use_is_exclusive(hdsp))
		return -EBUSY;
	val = ucontrol->value.integer.value[0] & 1;
	spin_lock_irq(&hdsp->lock);
	change = (int)val != hdsp->precise_ptr;
	hdsp_set_precise_pointer(hdsp, val);
	spin_unlock_irq(&hdsp->lock);
	return change;
}

#define HDSP_USE_MIDI_TASKLET(xname, xindex) \
2546
{ .iface = SNDRV_CTL_ELEM_IFACE_CARD, \
Linus Torvalds's avatar
Linus Torvalds committed
2547 2548 2549 2550 2551 2552 2553
  .name = xname, \
  .index = xindex, \
  .info = snd_hdsp_info_use_midi_tasklet, \
  .get = snd_hdsp_get_use_midi_tasklet, \
  .put = snd_hdsp_put_use_midi_tasklet \
}

2554
static int hdsp_set_use_midi_tasklet(struct hdsp *hdsp, int use_tasklet)
Linus Torvalds's avatar
Linus Torvalds committed
2555
{
Takashi Iwai's avatar
Takashi Iwai committed
2556
	if (use_tasklet)
Linus Torvalds's avatar
Linus Torvalds committed
2557
		hdsp->use_midi_tasklet = 1;
Takashi Iwai's avatar
Takashi Iwai committed
2558
	else
Linus Torvalds's avatar
Linus Torvalds committed
2559 2560 2561 2562
		hdsp->use_midi_tasklet = 0;
	return 0;
}

2563
#define snd_hdsp_info_use_midi_tasklet		snd_ctl_boolean_mono_info
Linus Torvalds's avatar
Linus Torvalds committed
2564

2565
static int snd_hdsp_get_use_midi_tasklet(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
2566
{
2567
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2568

Linus Torvalds's avatar
Linus Torvalds committed
2569 2570 2571 2572 2573 2574
	spin_lock_irq(&hdsp->lock);
	ucontrol->value.integer.value[0] = hdsp->use_midi_tasklet;
	spin_unlock_irq(&hdsp->lock);
	return 0;
}

2575
static int snd_hdsp_put_use_midi_tasklet(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
2576
{
2577
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
2578 2579
	int change;
	unsigned int val;
2580

Linus Torvalds's avatar
Linus Torvalds committed
2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594
	if (!snd_hdsp_use_is_exclusive(hdsp))
		return -EBUSY;
	val = ucontrol->value.integer.value[0] & 1;
	spin_lock_irq(&hdsp->lock);
	change = (int)val != hdsp->use_midi_tasklet;
	hdsp_set_use_midi_tasklet(hdsp, val);
	spin_unlock_irq(&hdsp->lock);
	return change;
}

#define HDSP_MIXER(xname, xindex) \
{ .iface = SNDRV_CTL_ELEM_IFACE_HWDEP, \
  .name = xname, \
  .index = xindex, \
2595
  .device = 0, \
Linus Torvalds's avatar
Linus Torvalds committed
2596 2597 2598 2599 2600 2601 2602
  .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
		 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
  .info = snd_hdsp_info_mixer, \
  .get = snd_hdsp_get_mixer, \
  .put = snd_hdsp_put_mixer \
}

2603
static int snd_hdsp_info_mixer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds's avatar
Linus Torvalds committed
2604 2605 2606 2607 2608 2609 2610 2611 2612
{
	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
	uinfo->count = 3;
	uinfo->value.integer.min = 0;
	uinfo->value.integer.max = 65536;
	uinfo->value.integer.step = 1;
	return 0;
}

2613
static int snd_hdsp_get_mixer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
2614
{
2615
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
2616 2617 2618 2619 2620 2621
	int source;
	int destination;
	int addr;

	source = ucontrol->value.integer.value[0];
	destination = ucontrol->value.integer.value[1];
2622

Takashi Iwai's avatar
Takashi Iwai committed
2623
	if (source >= hdsp->max_channels)
Linus Torvalds's avatar
Linus Torvalds committed
2624
		addr = hdsp_playback_to_output_key(hdsp,source-hdsp->max_channels,destination);
Takashi Iwai's avatar
Takashi Iwai committed
2625
	else
Linus Torvalds's avatar
Linus Torvalds committed
2626
		addr = hdsp_input_to_output_key(hdsp,source, destination);
2627

Linus Torvalds's avatar
Linus Torvalds committed
2628 2629 2630 2631 2632 2633
	spin_lock_irq(&hdsp->lock);
	ucontrol->value.integer.value[2] = hdsp_read_gain (hdsp, addr);
	spin_unlock_irq(&hdsp->lock);
	return 0;
}

2634
static int snd_hdsp_put_mixer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
2635
{
2636
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648
	int change;
	int source;
	int destination;
	int gain;
	int addr;

	if (!snd_hdsp_use_is_exclusive(hdsp))
		return -EBUSY;

	source = ucontrol->value.integer.value[0];
	destination = ucontrol->value.integer.value[1];

Takashi Iwai's avatar
Takashi Iwai committed
2649
	if (source >= hdsp->max_channels)
Linus Torvalds's avatar
Linus Torvalds committed
2650
		addr = hdsp_playback_to_output_key(hdsp,source-hdsp->max_channels, destination);
Takashi Iwai's avatar
Takashi Iwai committed
2651
	else
Linus Torvalds's avatar
Linus Torvalds committed
2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664
		addr = hdsp_input_to_output_key(hdsp,source, destination);

	gain = ucontrol->value.integer.value[2];

	spin_lock_irq(&hdsp->lock);
	change = gain != hdsp_read_gain(hdsp, addr);
	if (change)
		hdsp_write_gain(hdsp, addr, gain);
	spin_unlock_irq(&hdsp->lock);
	return change;
}

#define HDSP_WC_SYNC_CHECK(xname, xindex) \
2665
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds's avatar
Linus Torvalds committed
2666 2667 2668 2669 2670 2671 2672
  .name = xname, \
  .index = xindex, \
  .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
  .info = snd_hdsp_info_sync_check, \
  .get = snd_hdsp_get_wc_sync_check \
}

2673
static int snd_hdsp_info_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds's avatar
Linus Torvalds committed
2674
{
2675 2676 2677
	static const char * const texts[] = {"No Lock", "Lock", "Sync" };

	return snd_ctl_enum_info(uinfo, 1, 3, texts);
Linus Torvalds's avatar
Linus Torvalds committed
2678 2679
}

2680
static int hdsp_wc_sync_check(struct hdsp *hdsp)
Linus Torvalds's avatar
Linus Torvalds committed
2681 2682 2683
{
	int status2 = hdsp_read(hdsp, HDSP_status2Register);
	if (status2 & HDSP_wc_lock) {
Takashi Iwai's avatar
Takashi Iwai committed
2684
		if (status2 & HDSP_wc_sync)
Linus Torvalds's avatar
Linus Torvalds committed
2685
			return 2;
Takashi Iwai's avatar
Takashi Iwai committed
2686
		else
Linus Torvalds's avatar
Linus Torvalds committed
2687
			 return 1;
Takashi Iwai's avatar
Takashi Iwai committed
2688
	} else
Linus Torvalds's avatar
Linus Torvalds committed
2689 2690 2691 2692
		return 0;
	return 0;
}

2693
static int snd_hdsp_get_wc_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
2694
{
2695
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
2696 2697 2698 2699 2700 2701

	ucontrol->value.enumerated.item[0] = hdsp_wc_sync_check(hdsp);
	return 0;
}

#define HDSP_SPDIF_SYNC_CHECK(xname, xindex) \
2702
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds's avatar
Linus Torvalds committed
2703 2704 2705 2706 2707 2708 2709
  .name = xname, \
  .index = xindex, \
  .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
  .info = snd_hdsp_info_sync_check, \
  .get = snd_hdsp_get_spdif_sync_check \
}

2710
static int hdsp_spdif_sync_check(struct hdsp *hdsp)
Linus Torvalds's avatar
Linus Torvalds committed
2711 2712
{
	int status = hdsp_read(hdsp, HDSP_statusRegister);
Takashi Iwai's avatar
Takashi Iwai committed
2713
	if (status & HDSP_SPDIFErrorFlag)
Linus Torvalds's avatar
Linus Torvalds committed
2714
		return 0;
2715
	else {
Takashi Iwai's avatar
Takashi Iwai committed
2716
		if (status & HDSP_SPDIFSync)
Linus Torvalds's avatar
Linus Torvalds committed
2717
			return 2;
Takashi Iwai's avatar
Takashi Iwai committed
2718
		else
Linus Torvalds's avatar
Linus Torvalds committed
2719 2720 2721 2722 2723
			return 1;
	}
	return 0;
}

2724
static int snd_hdsp_get_spdif_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
2725
{
2726
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
2727 2728 2729 2730 2731 2732

	ucontrol->value.enumerated.item[0] = hdsp_spdif_sync_check(hdsp);
	return 0;
}

#define HDSP_ADATSYNC_SYNC_CHECK(xname, xindex) \
2733
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds's avatar
Linus Torvalds committed
2734 2735 2736 2737 2738 2739 2740
  .name = xname, \
  .index = xindex, \
  .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
  .info = snd_hdsp_info_sync_check, \
  .get = snd_hdsp_get_adatsync_sync_check \
}

2741
static int hdsp_adatsync_sync_check(struct hdsp *hdsp)
Linus Torvalds's avatar
Linus Torvalds committed
2742 2743 2744
{
	int status = hdsp_read(hdsp, HDSP_statusRegister);
	if (status & HDSP_TimecodeLock) {
Takashi Iwai's avatar
Takashi Iwai committed
2745
		if (status & HDSP_TimecodeSync)
Linus Torvalds's avatar
Linus Torvalds committed
2746
			return 2;
Takashi Iwai's avatar
Takashi Iwai committed
2747
		else
Linus Torvalds's avatar
Linus Torvalds committed
2748
			return 1;
Takashi Iwai's avatar
Takashi Iwai committed
2749
	} else
Linus Torvalds's avatar
Linus Torvalds committed
2750
		return 0;
2751
}
Linus Torvalds's avatar
Linus Torvalds committed
2752

2753
static int snd_hdsp_get_adatsync_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
2754
{
2755
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
2756 2757 2758 2759 2760 2761

	ucontrol->value.enumerated.item[0] = hdsp_adatsync_sync_check(hdsp);
	return 0;
}

#define HDSP_ADAT_SYNC_CHECK \
2762
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds's avatar
Linus Torvalds committed
2763 2764 2765 2766 2767
  .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
  .info = snd_hdsp_info_sync_check, \
  .get = snd_hdsp_get_adat_sync_check \
}

2768
static int hdsp_adat_sync_check(struct hdsp *hdsp, int idx)
2769
{
Linus Torvalds's avatar
Linus Torvalds committed
2770
	int status = hdsp_read(hdsp, HDSP_statusRegister);
2771

Linus Torvalds's avatar
Linus Torvalds committed
2772
	if (status & (HDSP_Lock0>>idx)) {
Takashi Iwai's avatar
Takashi Iwai committed
2773
		if (status & (HDSP_Sync0>>idx))
Linus Torvalds's avatar
Linus Torvalds committed
2774
			return 2;
Takashi Iwai's avatar
Takashi Iwai committed
2775
		else
2776
			return 1;
Takashi Iwai's avatar
Takashi Iwai committed
2777
	} else
Linus Torvalds's avatar
Linus Torvalds committed
2778
		return 0;
2779
}
Linus Torvalds's avatar
Linus Torvalds committed
2780

2781
static int snd_hdsp_get_adat_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
2782 2783
{
	int offset;
2784
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
2785 2786

	offset = ucontrol->id.index - 1;
2787 2788
	if (snd_BUG_ON(offset < 0))
		return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
2789 2790 2791 2792 2793 2794 2795 2796 2797

	switch (hdsp->io_type) {
	case Digiface:
	case H9652:
		if (offset >= 3)
			return -EINVAL;
		break;
	case Multiface:
	case H9632:
2798
		if (offset >= 1)
Linus Torvalds's avatar
Linus Torvalds committed
2799 2800 2801 2802 2803 2804 2805 2806 2807 2808
			return -EINVAL;
		break;
	default:
		return -EIO;
	}

	ucontrol->value.enumerated.item[0] = hdsp_adat_sync_check(hdsp, offset);
	return 0;
}

2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823
#define HDSP_DDS_OFFSET(xname, xindex) \
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  .name = xname, \
  .index = xindex, \
  .info = snd_hdsp_info_dds_offset, \
  .get = snd_hdsp_get_dds_offset, \
  .put = snd_hdsp_put_dds_offset \
}

static int hdsp_dds_offset(struct hdsp *hdsp)
{
	u64 n;
	unsigned int dds_value = hdsp->dds_value;
	int system_sample_rate = hdsp->system_sample_rate;

2824 2825 2826
	if (!dds_value)
		return 0;

2827 2828 2829 2830 2831
	n = DDS_NUMERATOR;
	/*
	 * dds_value = n / rate
	 * rate = n / dds_value
	 */
2832
	n = div_u64(n, dds_value);
2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858
	if (system_sample_rate >= 112000)
		n *= 4;
	else if (system_sample_rate >= 56000)
		n *= 2;
	return ((int)n) - system_sample_rate;
}

static int hdsp_set_dds_offset(struct hdsp *hdsp, int offset_hz)
{
	int rate = hdsp->system_sample_rate + offset_hz;
	hdsp_set_dds_value(hdsp, rate);
	return 0;
}

static int snd_hdsp_info_dds_offset(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
{
	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
	uinfo->count = 1;
	uinfo->value.integer.min = -5000;
	uinfo->value.integer.max = 5000;
	return 0;
}

static int snd_hdsp_get_dds_offset(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
{
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2859

2860
	ucontrol->value.integer.value[0] = hdsp_dds_offset(hdsp);
2861 2862 2863 2864 2865 2866 2867 2868
	return 0;
}

static int snd_hdsp_put_dds_offset(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
{
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
	int change;
	int val;
2869

2870 2871
	if (!snd_hdsp_use_is_exclusive(hdsp))
		return -EBUSY;
2872
	val = ucontrol->value.integer.value[0];
2873 2874 2875 2876 2877 2878 2879 2880 2881
	spin_lock_irq(&hdsp->lock);
	if (val != hdsp_dds_offset(hdsp))
		change = (hdsp_set_dds_offset(hdsp, val) == 0) ? 1 : 0;
	else
		change = 0;
	spin_unlock_irq(&hdsp->lock);
	return change;
}

2882
static struct snd_kcontrol_new snd_hdsp_9632_controls[] = {
Linus Torvalds's avatar
Linus Torvalds committed
2883 2884 2885
HDSP_DA_GAIN("DA Gain", 0),
HDSP_AD_GAIN("AD Gain", 0),
HDSP_PHONE_GAIN("Phones Gain", 0),
2886
HDSP_TOGGLE_SETTING("XLR Breakout Cable", HDSP_XLRBreakoutCable),
2887
HDSP_DDS_OFFSET("DDS Sample Rate Offset", 0)
Linus Torvalds's avatar
Linus Torvalds committed
2888 2889
};

2890
static struct snd_kcontrol_new snd_hdsp_controls[] = {
Linus Torvalds's avatar
Linus Torvalds committed
2891
{
2892
	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds's avatar
Linus Torvalds committed
2893 2894 2895 2896 2897 2898 2899
	.name =		SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
	.info =		snd_hdsp_control_spdif_info,
	.get =		snd_hdsp_control_spdif_get,
	.put =		snd_hdsp_control_spdif_put,
},
{
	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
2900
	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds's avatar
Linus Torvalds committed
2901 2902 2903 2904 2905 2906 2907
	.name =		SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
	.info =		snd_hdsp_control_spdif_stream_info,
	.get =		snd_hdsp_control_spdif_stream_get,
	.put =		snd_hdsp_control_spdif_stream_put,
},
{
	.access =	SNDRV_CTL_ELEM_ACCESS_READ,
2908
	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds's avatar
Linus Torvalds committed
2909 2910 2911 2912 2913
	.name =		SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
	.info =		snd_hdsp_control_spdif_mask_info,
	.get =		snd_hdsp_control_spdif_mask_get,
	.private_value = IEC958_AES0_NONAUDIO |
  			 IEC958_AES0_PROFESSIONAL |
2914
			 IEC958_AES0_CON_EMPHASIS,
Linus Torvalds's avatar
Linus Torvalds committed
2915 2916 2917
},
{
	.access =	SNDRV_CTL_ELEM_ACCESS_READ,
2918
	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds's avatar
Linus Torvalds committed
2919 2920 2921 2922 2923 2924 2925 2926 2927
	.name =		SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
	.info =		snd_hdsp_control_spdif_mask_info,
	.get =		snd_hdsp_control_spdif_mask_get,
	.private_value = IEC958_AES0_NONAUDIO |
			 IEC958_AES0_PROFESSIONAL |
			 IEC958_AES0_PRO_EMPHASIS,
},
HDSP_MIXER("Mixer", 0),
HDSP_SPDIF_IN("IEC958 Input Connector", 0),
2928 2929 2930 2931
HDSP_TOGGLE_SETTING("IEC958 Output also on ADAT1", HDSP_SPDIFOpticalOut),
HDSP_TOGGLE_SETTING("IEC958 Professional Bit", HDSP_SPDIFProfessional),
HDSP_TOGGLE_SETTING("IEC958 Emphasis Bit", HDSP_SPDIFEmphasis),
HDSP_TOGGLE_SETTING("IEC958 Non-audio Bit", HDSP_SPDIFNonAudio),
2932
/* 'Sample Clock Source' complies with the alsa control naming scheme */
Linus Torvalds's avatar
Linus Torvalds committed
2933
HDSP_CLOCK_SOURCE("Sample Clock Source", 0),
2934 2935 2936 2937 2938 2939 2940
{
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.name = "Sample Clock Source Locking",
	.info = snd_hdsp_info_clock_source_lock,
	.get = snd_hdsp_get_clock_source_lock,
	.put = snd_hdsp_put_clock_source_lock,
},
Linus Torvalds's avatar
Linus Torvalds committed
2941 2942 2943 2944 2945 2946 2947 2948 2949 2950
HDSP_SYSTEM_CLOCK_MODE("System Clock Mode", 0),
HDSP_PREF_SYNC_REF("Preferred Sync Reference", 0),
HDSP_AUTOSYNC_REF("AutoSync Reference", 0),
HDSP_SPDIF_SAMPLE_RATE("SPDIF Sample Rate", 0),
HDSP_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
/* 'External Rate' complies with the alsa control naming scheme */
HDSP_AUTOSYNC_SAMPLE_RATE("External Rate", 0),
HDSP_WC_SYNC_CHECK("Word Clock Lock Status", 0),
HDSP_SPDIF_SYNC_CHECK("SPDIF Lock Status", 0),
HDSP_ADATSYNC_SYNC_CHECK("ADAT Sync Lock Status", 0),
2951
HDSP_TOGGLE_SETTING("Line Out", HDSP_LineOut),
Linus Torvalds's avatar
Linus Torvalds committed
2952 2953 2954 2955
HDSP_PRECISE_POINTER("Precise Pointer", 0),
HDSP_USE_MIDI_TASKLET("Use Midi Tasklet", 0),
};

2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033

static int hdsp_rpm_input12(struct hdsp *hdsp)
{
	switch (hdsp->control_register & HDSP_RPM_Inp12) {
	case HDSP_RPM_Inp12_Phon_6dB:
		return 0;
	case HDSP_RPM_Inp12_Phon_n6dB:
		return 2;
	case HDSP_RPM_Inp12_Line_0dB:
		return 3;
	case HDSP_RPM_Inp12_Line_n6dB:
		return 4;
	}
	return 1;
}


static int snd_hdsp_get_rpm_input12(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
{
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);

	ucontrol->value.enumerated.item[0] = hdsp_rpm_input12(hdsp);
	return 0;
}


static int hdsp_set_rpm_input12(struct hdsp *hdsp, int mode)
{
	hdsp->control_register &= ~HDSP_RPM_Inp12;
	switch (mode) {
	case 0:
		hdsp->control_register |= HDSP_RPM_Inp12_Phon_6dB;
		break;
	case 1:
		break;
	case 2:
		hdsp->control_register |= HDSP_RPM_Inp12_Phon_n6dB;
		break;
	case 3:
		hdsp->control_register |= HDSP_RPM_Inp12_Line_0dB;
		break;
	case 4:
		hdsp->control_register |= HDSP_RPM_Inp12_Line_n6dB;
		break;
	default:
		return -1;
	}

	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
	return 0;
}


static int snd_hdsp_put_rpm_input12(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
{
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
	int change;
	int val;

	if (!snd_hdsp_use_is_exclusive(hdsp))
		return -EBUSY;
	val = ucontrol->value.enumerated.item[0];
	if (val < 0)
		val = 0;
	if (val > 4)
		val = 4;
	spin_lock_irq(&hdsp->lock);
	if (val != hdsp_rpm_input12(hdsp))
		change = (hdsp_set_rpm_input12(hdsp, val) == 0) ? 1 : 0;
	else
		change = 0;
	spin_unlock_irq(&hdsp->lock);
	return change;
}


static int snd_hdsp_info_rpm_input(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
{
3034 3035 3036
	static const char * const texts[] = {
		"Phono +6dB", "Phono 0dB", "Phono -6dB", "Line 0dB", "Line -6dB"
	};
3037

3038
	return snd_ctl_enum_info(uinfo, 1, 5, texts);
3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162
}


static int hdsp_rpm_input34(struct hdsp *hdsp)
{
	switch (hdsp->control_register & HDSP_RPM_Inp34) {
	case HDSP_RPM_Inp34_Phon_6dB:
		return 0;
	case HDSP_RPM_Inp34_Phon_n6dB:
		return 2;
	case HDSP_RPM_Inp34_Line_0dB:
		return 3;
	case HDSP_RPM_Inp34_Line_n6dB:
		return 4;
	}
	return 1;
}


static int snd_hdsp_get_rpm_input34(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
{
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);

	ucontrol->value.enumerated.item[0] = hdsp_rpm_input34(hdsp);
	return 0;
}


static int hdsp_set_rpm_input34(struct hdsp *hdsp, int mode)
{
	hdsp->control_register &= ~HDSP_RPM_Inp34;
	switch (mode) {
	case 0:
		hdsp->control_register |= HDSP_RPM_Inp34_Phon_6dB;
		break;
	case 1:
		break;
	case 2:
		hdsp->control_register |= HDSP_RPM_Inp34_Phon_n6dB;
		break;
	case 3:
		hdsp->control_register |= HDSP_RPM_Inp34_Line_0dB;
		break;
	case 4:
		hdsp->control_register |= HDSP_RPM_Inp34_Line_n6dB;
		break;
	default:
		return -1;
	}

	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
	return 0;
}


static int snd_hdsp_put_rpm_input34(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
{
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
	int change;
	int val;

	if (!snd_hdsp_use_is_exclusive(hdsp))
		return -EBUSY;
	val = ucontrol->value.enumerated.item[0];
	if (val < 0)
		val = 0;
	if (val > 4)
		val = 4;
	spin_lock_irq(&hdsp->lock);
	if (val != hdsp_rpm_input34(hdsp))
		change = (hdsp_set_rpm_input34(hdsp, val) == 0) ? 1 : 0;
	else
		change = 0;
	spin_unlock_irq(&hdsp->lock);
	return change;
}


/* RPM Bypass switch */
static int hdsp_rpm_bypass(struct hdsp *hdsp)
{
	return (hdsp->control_register & HDSP_RPM_Bypass) ? 1 : 0;
}


static int snd_hdsp_get_rpm_bypass(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
{
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);

	ucontrol->value.integer.value[0] = hdsp_rpm_bypass(hdsp);
	return 0;
}


static int hdsp_set_rpm_bypass(struct hdsp *hdsp, int on)
{
	if (on)
		hdsp->control_register |= HDSP_RPM_Bypass;
	else
		hdsp->control_register &= ~HDSP_RPM_Bypass;
	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
	return 0;
}


static int snd_hdsp_put_rpm_bypass(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
{
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
	int change;
	unsigned int val;

	if (!snd_hdsp_use_is_exclusive(hdsp))
		return -EBUSY;
	val = ucontrol->value.integer.value[0] & 1;
	spin_lock_irq(&hdsp->lock);
	change = (int)val != hdsp_rpm_bypass(hdsp);
	hdsp_set_rpm_bypass(hdsp, val);
	spin_unlock_irq(&hdsp->lock);
	return change;
}


static int snd_hdsp_info_rpm_bypass(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
{
3163
	static const char * const texts[] = {"On", "Off"};
3164

3165
	return snd_ctl_enum_info(uinfo, 1, 2, texts);
3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213
}


/* RPM Disconnect switch */
static int hdsp_rpm_disconnect(struct hdsp *hdsp)
{
	return (hdsp->control_register & HDSP_RPM_Disconnect) ? 1 : 0;
}


static int snd_hdsp_get_rpm_disconnect(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
{
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);

	ucontrol->value.integer.value[0] = hdsp_rpm_disconnect(hdsp);
	return 0;
}


static int hdsp_set_rpm_disconnect(struct hdsp *hdsp, int on)
{
	if (on)
		hdsp->control_register |= HDSP_RPM_Disconnect;
	else
		hdsp->control_register &= ~HDSP_RPM_Disconnect;
	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
	return 0;
}


static int snd_hdsp_put_rpm_disconnect(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
{
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
	int change;
	unsigned int val;

	if (!snd_hdsp_use_is_exclusive(hdsp))
		return -EBUSY;
	val = ucontrol->value.integer.value[0] & 1;
	spin_lock_irq(&hdsp->lock);
	change = (int)val != hdsp_rpm_disconnect(hdsp);
	hdsp_set_rpm_disconnect(hdsp, val);
	spin_unlock_irq(&hdsp->lock);
	return change;
}

static int snd_hdsp_info_rpm_disconnect(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
{
3214
	static const char * const texts[] = {"On", "Off"};
3215

3216
	return snd_ctl_enum_info(uinfo, 1, 2, texts);
3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251
}

static struct snd_kcontrol_new snd_hdsp_rpm_controls[] = {
	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		.name = "RPM Bypass",
		.get = snd_hdsp_get_rpm_bypass,
		.put = snd_hdsp_put_rpm_bypass,
		.info = snd_hdsp_info_rpm_bypass
	},
	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		.name = "RPM Disconnect",
		.get = snd_hdsp_get_rpm_disconnect,
		.put = snd_hdsp_put_rpm_disconnect,
		.info = snd_hdsp_info_rpm_disconnect
	},
	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		.name = "Input 1/2",
		.get = snd_hdsp_get_rpm_input12,
		.put = snd_hdsp_put_rpm_input12,
		.info = snd_hdsp_info_rpm_input
	},
	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		.name = "Input 3/4",
		.get = snd_hdsp_get_rpm_input34,
		.put = snd_hdsp_put_rpm_input34,
		.info = snd_hdsp_info_rpm_input
	},
	HDSP_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
	HDSP_MIXER("Mixer", 0)
};

3252 3253 3254
static struct snd_kcontrol_new snd_hdsp_96xx_aeb =
	HDSP_TOGGLE_SETTING("Analog Extension Board",
			HDSP_AnalogExtensionBoard);
3255
static struct snd_kcontrol_new snd_hdsp_adat_sync_check = HDSP_ADAT_SYNC_CHECK;
Linus Torvalds's avatar
Linus Torvalds committed
3256

3257
static int snd_hdsp_create_controls(struct snd_card *card, struct hdsp *hdsp)
Linus Torvalds's avatar
Linus Torvalds committed
3258 3259 3260
{
	unsigned int idx;
	int err;
3261
	struct snd_kcontrol *kctl;
Linus Torvalds's avatar
Linus Torvalds committed
3262

3263 3264 3265 3266 3267 3268 3269 3270 3271 3272
	if (hdsp->io_type == RPM) {
		/* RPM Bypass, Disconnect and Input switches */
		for (idx = 0; idx < ARRAY_SIZE(snd_hdsp_rpm_controls); idx++) {
			err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_rpm_controls[idx], hdsp));
			if (err < 0)
				return err;
		}
		return 0;
	}

Linus Torvalds's avatar
Linus Torvalds committed
3273
	for (idx = 0; idx < ARRAY_SIZE(snd_hdsp_controls); idx++) {
Takashi Iwai's avatar
Takashi Iwai committed
3274
		if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_controls[idx], hdsp))) < 0)
Linus Torvalds's avatar
Linus Torvalds committed
3275 3276 3277 3278 3279 3280 3281 3282
			return err;
		if (idx == 1)	/* IEC958 (S/PDIF) Stream */
			hdsp->spdif_ctl = kctl;
	}

	/* ADAT SyncCheck status */
	snd_hdsp_adat_sync_check.name = "ADAT Lock Status";
	snd_hdsp_adat_sync_check.index = 1;
Takashi Iwai's avatar
Takashi Iwai committed
3283
	if ((err = snd_ctl_add (card, kctl = snd_ctl_new1(&snd_hdsp_adat_sync_check, hdsp))))
Linus Torvalds's avatar
Linus Torvalds committed
3284 3285 3286 3287
		return err;
	if (hdsp->io_type == Digiface || hdsp->io_type == H9652) {
		for (idx = 1; idx < 3; ++idx) {
			snd_hdsp_adat_sync_check.index = idx+1;
Takashi Iwai's avatar
Takashi Iwai committed
3288
			if ((err = snd_ctl_add (card, kctl = snd_ctl_new1(&snd_hdsp_adat_sync_check, hdsp))))
Linus Torvalds's avatar
Linus Torvalds committed
3289 3290 3291
				return err;
		}
	}
3292

Linus Torvalds's avatar
Linus Torvalds committed
3293 3294 3295
	/* DA, AD and Phone gain and XLR breakout cable controls for H9632 cards */
	if (hdsp->io_type == H9632) {
		for (idx = 0; idx < ARRAY_SIZE(snd_hdsp_9632_controls); idx++) {
Takashi Iwai's avatar
Takashi Iwai committed
3296
			if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_9632_controls[idx], hdsp))) < 0)
Linus Torvalds's avatar
Linus Torvalds committed
3297 3298 3299 3300 3301 3302
				return err;
		}
	}

	/* AEB control for H96xx card */
	if (hdsp->io_type == H9632 || hdsp->io_type == H9652) {
Takashi Iwai's avatar
Takashi Iwai committed
3303
		if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_96xx_aeb, hdsp))) < 0)
Linus Torvalds's avatar
Linus Torvalds committed
3304 3305 3306 3307 3308 3309 3310
				return err;
	}

	return 0;
}

/*------------------------------------------------------------
3311
   /proc interface
Linus Torvalds's avatar
Linus Torvalds committed
3312 3313 3314
 ------------------------------------------------------------*/

static void
3315
snd_hdsp_proc_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
Linus Torvalds's avatar
Linus Torvalds committed
3316
{
3317
	struct hdsp *hdsp = entry->private_data;
Linus Torvalds's avatar
Linus Torvalds committed
3318 3319 3320 3321 3322 3323 3324 3325
	unsigned int status;
	unsigned int status2;
	char *pref_sync_ref;
	char *autosync_ref;
	char *system_clock_mode;
	char *clock_source;
	int x;

3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343
	status = hdsp_read(hdsp, HDSP_statusRegister);
	status2 = hdsp_read(hdsp, HDSP_status2Register);

	snd_iprintf(buffer, "%s (Card #%d)\n", hdsp->card_name,
		    hdsp->card->number + 1);
	snd_iprintf(buffer, "Buffers: capture %p playback %p\n",
		    hdsp->capture_buffer, hdsp->playback_buffer);
	snd_iprintf(buffer, "IRQ: %d Registers bus: 0x%lx VM: 0x%lx\n",
		    hdsp->irq, hdsp->port, (unsigned long)hdsp->iobase);
	snd_iprintf(buffer, "Control register: 0x%x\n", hdsp->control_register);
	snd_iprintf(buffer, "Control2 register: 0x%x\n",
		    hdsp->control2_register);
	snd_iprintf(buffer, "Status register: 0x%x\n", status);
	snd_iprintf(buffer, "Status2 register: 0x%x\n", status2);

	if (hdsp_check_for_iobox(hdsp)) {
		snd_iprintf(buffer, "No I/O box connected.\n"
			    "Please connect one and upload firmware.\n");
Linus Torvalds's avatar
Linus Torvalds committed
3344
		return;
3345
	}
Linus Torvalds's avatar
Linus Torvalds committed
3346

Takashi Iwai's avatar
Takashi Iwai committed
3347
	if (hdsp_check_for_firmware(hdsp, 0)) {
Linus Torvalds's avatar
Linus Torvalds committed
3348 3349
		if (hdsp->state & HDSP_FirmwareCached) {
			if (snd_hdsp_load_firmware_from_cache(hdsp) != 0) {
3350 3351 3352
				snd_iprintf(buffer, "Firmware loading from "
					    "cache failed, "
					    "please upload manually.\n");
Linus Torvalds's avatar
Linus Torvalds committed
3353 3354 3355
				return;
			}
		} else {
3356 3357 3358 3359 3360 3361 3362 3363
			int err = -EINVAL;
			err = hdsp_request_fw_loader(hdsp);
			if (err < 0) {
				snd_iprintf(buffer,
					    "No firmware loaded nor cached, "
					    "please upload firmware.\n");
				return;
			}
Linus Torvalds's avatar
Linus Torvalds committed
3364 3365
		}
	}
3366

Linus Torvalds's avatar
Linus Torvalds committed
3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416
	snd_iprintf(buffer, "FIFO status: %d\n", hdsp_read(hdsp, HDSP_fifoStatus) & 0xff);
	snd_iprintf(buffer, "MIDI1 Output status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusOut0));
	snd_iprintf(buffer, "MIDI1 Input status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusIn0));
	snd_iprintf(buffer, "MIDI2 Output status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusOut1));
	snd_iprintf(buffer, "MIDI2 Input status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusIn1));
	snd_iprintf(buffer, "Use Midi Tasklet: %s\n", hdsp->use_midi_tasklet ? "on" : "off");

	snd_iprintf(buffer, "\n");

	x = 1 << (6 + hdsp_decode_latency(hdsp->control_register & HDSP_LatencyMask));

	snd_iprintf(buffer, "Buffer Size (Latency): %d samples (2 periods of %lu bytes)\n", x, (unsigned long) hdsp->period_bytes);
	snd_iprintf(buffer, "Hardware pointer (frames): %ld\n", hdsp_hw_pointer(hdsp));
	snd_iprintf(buffer, "Precise pointer: %s\n", hdsp->precise_ptr ? "on" : "off");
	snd_iprintf(buffer, "Line out: %s\n", (hdsp->control_register & HDSP_LineOut) ? "on" : "off");

	snd_iprintf(buffer, "Firmware version: %d\n", (status2&HDSP_version0)|(status2&HDSP_version1)<<1|(status2&HDSP_version2)<<2);

	snd_iprintf(buffer, "\n");

	switch (hdsp_clock_source(hdsp)) {
	case HDSP_CLOCK_SOURCE_AUTOSYNC:
		clock_source = "AutoSync";
		break;
	case HDSP_CLOCK_SOURCE_INTERNAL_32KHZ:
		clock_source = "Internal 32 kHz";
		break;
	case HDSP_CLOCK_SOURCE_INTERNAL_44_1KHZ:
		clock_source = "Internal 44.1 kHz";
		break;
	case HDSP_CLOCK_SOURCE_INTERNAL_48KHZ:
		clock_source = "Internal 48 kHz";
		break;
	case HDSP_CLOCK_SOURCE_INTERNAL_64KHZ:
		clock_source = "Internal 64 kHz";
		break;
	case HDSP_CLOCK_SOURCE_INTERNAL_88_2KHZ:
		clock_source = "Internal 88.2 kHz";
		break;
	case HDSP_CLOCK_SOURCE_INTERNAL_96KHZ:
		clock_source = "Internal 96 kHz";
		break;
	case HDSP_CLOCK_SOURCE_INTERNAL_128KHZ:
		clock_source = "Internal 128 kHz";
		break;
	case HDSP_CLOCK_SOURCE_INTERNAL_176_4KHZ:
		clock_source = "Internal 176.4 kHz";
		break;
		case HDSP_CLOCK_SOURCE_INTERNAL_192KHZ:
		clock_source = "Internal 192 kHz";
3417
		break;
Linus Torvalds's avatar
Linus Torvalds committed
3418
	default:
3419
		clock_source = "Error";
Linus Torvalds's avatar
Linus Torvalds committed
3420 3421
	}
	snd_iprintf (buffer, "Sample Clock Source: %s\n", clock_source);
3422

Takashi Iwai's avatar
Takashi Iwai committed
3423
	if (hdsp_system_clock_mode(hdsp))
Linus Torvalds's avatar
Linus Torvalds committed
3424
		system_clock_mode = "Slave";
Takashi Iwai's avatar
Takashi Iwai committed
3425
	else
Linus Torvalds's avatar
Linus Torvalds committed
3426
		system_clock_mode = "Master";
3427

Linus Torvalds's avatar
Linus Torvalds committed
3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451
	switch (hdsp_pref_sync_ref (hdsp)) {
	case HDSP_SYNC_FROM_WORD:
		pref_sync_ref = "Word Clock";
		break;
	case HDSP_SYNC_FROM_ADAT_SYNC:
		pref_sync_ref = "ADAT Sync";
		break;
	case HDSP_SYNC_FROM_SPDIF:
		pref_sync_ref = "SPDIF";
		break;
	case HDSP_SYNC_FROM_ADAT1:
		pref_sync_ref = "ADAT1";
		break;
	case HDSP_SYNC_FROM_ADAT2:
		pref_sync_ref = "ADAT2";
		break;
	case HDSP_SYNC_FROM_ADAT3:
		pref_sync_ref = "ADAT3";
		break;
	default:
		pref_sync_ref = "Word Clock";
		break;
	}
	snd_iprintf (buffer, "Preferred Sync Reference: %s\n", pref_sync_ref);
3452

Linus Torvalds's avatar
Linus Torvalds committed
3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464
	switch (hdsp_autosync_ref (hdsp)) {
	case HDSP_AUTOSYNC_FROM_WORD:
		autosync_ref = "Word Clock";
		break;
	case HDSP_AUTOSYNC_FROM_ADAT_SYNC:
		autosync_ref = "ADAT Sync";
		break;
	case HDSP_AUTOSYNC_FROM_SPDIF:
		autosync_ref = "SPDIF";
		break;
	case HDSP_AUTOSYNC_FROM_NONE:
		autosync_ref = "None";
3465
		break;
Linus Torvalds's avatar
Linus Torvalds committed
3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479
	case HDSP_AUTOSYNC_FROM_ADAT1:
		autosync_ref = "ADAT1";
		break;
	case HDSP_AUTOSYNC_FROM_ADAT2:
		autosync_ref = "ADAT2";
		break;
	case HDSP_AUTOSYNC_FROM_ADAT3:
		autosync_ref = "ADAT3";
		break;
	default:
		autosync_ref = "---";
		break;
	}
	snd_iprintf (buffer, "AutoSync Reference: %s\n", autosync_ref);
3480

Linus Torvalds's avatar
Linus Torvalds committed
3481
	snd_iprintf (buffer, "AutoSync Frequency: %d\n", hdsp_external_sample_rate(hdsp));
3482

Linus Torvalds's avatar
Linus Torvalds committed
3483 3484 3485
	snd_iprintf (buffer, "System Clock Mode: %s\n", system_clock_mode);

	snd_iprintf (buffer, "System Clock Frequency: %d\n", hdsp->system_sample_rate);
3486
	snd_iprintf (buffer, "System Clock Locked: %s\n", hdsp->clock_source_locked ? "Yes" : "No");
3487

Linus Torvalds's avatar
Linus Torvalds committed
3488 3489
	snd_iprintf(buffer, "\n");

3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507
	if (hdsp->io_type != RPM) {
		switch (hdsp_spdif_in(hdsp)) {
		case HDSP_SPDIFIN_OPTICAL:
			snd_iprintf(buffer, "IEC958 input: Optical\n");
			break;
		case HDSP_SPDIFIN_COAXIAL:
			snd_iprintf(buffer, "IEC958 input: Coaxial\n");
			break;
		case HDSP_SPDIFIN_INTERNAL:
			snd_iprintf(buffer, "IEC958 input: Internal\n");
			break;
		case HDSP_SPDIFIN_AES:
			snd_iprintf(buffer, "IEC958 input: AES\n");
			break;
		default:
			snd_iprintf(buffer, "IEC958 input: ???\n");
			break;
		}
Linus Torvalds's avatar
Linus Torvalds committed
3508
	}
3509

3510 3511 3512 3513 3514 3515 3516 3517 3518
	if (RPM == hdsp->io_type) {
		if (hdsp->control_register & HDSP_RPM_Bypass)
			snd_iprintf(buffer, "RPM Bypass: disabled\n");
		else
			snd_iprintf(buffer, "RPM Bypass: enabled\n");
		if (hdsp->control_register & HDSP_RPM_Disconnect)
			snd_iprintf(buffer, "RPM disconnected\n");
		else
			snd_iprintf(buffer, "RPM connected\n");
Linus Torvalds's avatar
Linus Torvalds committed
3519

3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538
		switch (hdsp->control_register & HDSP_RPM_Inp12) {
		case HDSP_RPM_Inp12_Phon_6dB:
			snd_iprintf(buffer, "Input 1/2: Phono, 6dB\n");
			break;
		case HDSP_RPM_Inp12_Phon_0dB:
			snd_iprintf(buffer, "Input 1/2: Phono, 0dB\n");
			break;
		case HDSP_RPM_Inp12_Phon_n6dB:
			snd_iprintf(buffer, "Input 1/2: Phono, -6dB\n");
			break;
		case HDSP_RPM_Inp12_Line_0dB:
			snd_iprintf(buffer, "Input 1/2: Line, 0dB\n");
			break;
		case HDSP_RPM_Inp12_Line_n6dB:
			snd_iprintf(buffer, "Input 1/2: Line, -6dB\n");
			break;
		default:
			snd_iprintf(buffer, "Input 1/2: ???\n");
		}
Linus Torvalds's avatar
Linus Torvalds committed
3539

3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558
		switch (hdsp->control_register & HDSP_RPM_Inp34) {
		case HDSP_RPM_Inp34_Phon_6dB:
			snd_iprintf(buffer, "Input 3/4: Phono, 6dB\n");
			break;
		case HDSP_RPM_Inp34_Phon_0dB:
			snd_iprintf(buffer, "Input 3/4: Phono, 0dB\n");
			break;
		case HDSP_RPM_Inp34_Phon_n6dB:
			snd_iprintf(buffer, "Input 3/4: Phono, -6dB\n");
			break;
		case HDSP_RPM_Inp34_Line_0dB:
			snd_iprintf(buffer, "Input 3/4: Line, 0dB\n");
			break;
		case HDSP_RPM_Inp34_Line_n6dB:
			snd_iprintf(buffer, "Input 3/4: Line, -6dB\n");
			break;
		default:
			snd_iprintf(buffer, "Input 3/4: ???\n");
		}
Linus Torvalds's avatar
Linus Torvalds committed
3559

3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574
	} else {
		if (hdsp->control_register & HDSP_SPDIFOpticalOut)
			snd_iprintf(buffer, "IEC958 output: Coaxial & ADAT1\n");
		else
			snd_iprintf(buffer, "IEC958 output: Coaxial only\n");

		if (hdsp->control_register & HDSP_SPDIFProfessional)
			snd_iprintf(buffer, "IEC958 quality: Professional\n");
		else
			snd_iprintf(buffer, "IEC958 quality: Consumer\n");

		if (hdsp->control_register & HDSP_SPDIFEmphasis)
			snd_iprintf(buffer, "IEC958 emphasis: on\n");
		else
			snd_iprintf(buffer, "IEC958 emphasis: off\n");
Linus Torvalds's avatar
Linus Torvalds committed
3575

3576 3577 3578 3579 3580 3581 3582 3583 3584 3585
		if (hdsp->control_register & HDSP_SPDIFNonAudio)
			snd_iprintf(buffer, "IEC958 NonAudio: on\n");
		else
			snd_iprintf(buffer, "IEC958 NonAudio: off\n");
		x = hdsp_spdif_sample_rate(hdsp);
		if (x != 0)
			snd_iprintf(buffer, "IEC958 sample rate: %d\n", x);
		else
			snd_iprintf(buffer, "IEC958 sample rate: Error flag set\n");
	}
Linus Torvalds's avatar
Linus Torvalds committed
3586 3587 3588 3589
	snd_iprintf(buffer, "\n");

	/* Sync Check */
	x = status & HDSP_Sync0;
Takashi Iwai's avatar
Takashi Iwai committed
3590
	if (status & HDSP_Lock0)
Linus Torvalds's avatar
Linus Torvalds committed
3591
		snd_iprintf(buffer, "ADAT1: %s\n", x ? "Sync" : "Lock");
Takashi Iwai's avatar
Takashi Iwai committed
3592
	else
Linus Torvalds's avatar
Linus Torvalds committed
3593 3594 3595 3596 3597 3598
		snd_iprintf(buffer, "ADAT1: No Lock\n");

	switch (hdsp->io_type) {
	case Digiface:
	case H9652:
		x = status & HDSP_Sync1;
Takashi Iwai's avatar
Takashi Iwai committed
3599
		if (status & HDSP_Lock1)
Linus Torvalds's avatar
Linus Torvalds committed
3600
			snd_iprintf(buffer, "ADAT2: %s\n", x ? "Sync" : "Lock");
Takashi Iwai's avatar
Takashi Iwai committed
3601
		else
Linus Torvalds's avatar
Linus Torvalds committed
3602 3603
			snd_iprintf(buffer, "ADAT2: No Lock\n");
		x = status & HDSP_Sync2;
Takashi Iwai's avatar
Takashi Iwai committed
3604
		if (status & HDSP_Lock2)
Linus Torvalds's avatar
Linus Torvalds committed
3605
			snd_iprintf(buffer, "ADAT3: %s\n", x ? "Sync" : "Lock");
Takashi Iwai's avatar
Takashi Iwai committed
3606
		else
Linus Torvalds's avatar
Linus Torvalds committed
3607
			snd_iprintf(buffer, "ADAT3: No Lock\n");
Takashi Iwai's avatar
Takashi Iwai committed
3608
		break;
Linus Torvalds's avatar
Linus Torvalds committed
3609 3610 3611 3612 3613 3614
	default:
		/* relax */
		break;
	}

	x = status & HDSP_SPDIFSync;
Takashi Iwai's avatar
Takashi Iwai committed
3615
	if (status & HDSP_SPDIFErrorFlag)
Linus Torvalds's avatar
Linus Torvalds committed
3616
		snd_iprintf (buffer, "SPDIF: No Lock\n");
Takashi Iwai's avatar
Takashi Iwai committed
3617
	else
Linus Torvalds's avatar
Linus Torvalds committed
3618
		snd_iprintf (buffer, "SPDIF: %s\n", x ? "Sync" : "Lock");
3619

Linus Torvalds's avatar
Linus Torvalds committed
3620
	x = status2 & HDSP_wc_sync;
Takashi Iwai's avatar
Takashi Iwai committed
3621
	if (status2 & HDSP_wc_lock)
Linus Torvalds's avatar
Linus Torvalds committed
3622
		snd_iprintf (buffer, "Word Clock: %s\n", x ? "Sync" : "Lock");
Takashi Iwai's avatar
Takashi Iwai committed
3623
	else
Linus Torvalds's avatar
Linus Torvalds committed
3624
		snd_iprintf (buffer, "Word Clock: No Lock\n");
3625

Linus Torvalds's avatar
Linus Torvalds committed
3626
	x = status & HDSP_TimecodeSync;
Takashi Iwai's avatar
Takashi Iwai committed
3627
	if (status & HDSP_TimecodeLock)
Linus Torvalds's avatar
Linus Torvalds committed
3628
		snd_iprintf(buffer, "ADAT Sync: %s\n", x ? "Sync" : "Lock");
Takashi Iwai's avatar
Takashi Iwai committed
3629
	else
Linus Torvalds's avatar
Linus Torvalds committed
3630 3631 3632
		snd_iprintf(buffer, "ADAT Sync: No Lock\n");

	snd_iprintf(buffer, "\n");
3633

Linus Torvalds's avatar
Linus Torvalds committed
3634 3635 3636
	/* Informations about H9632 specific controls */
	if (hdsp->io_type == H9632) {
		char *tmp;
3637

Linus Torvalds's avatar
Linus Torvalds committed
3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662
		switch (hdsp_ad_gain(hdsp)) {
		case 0:
			tmp = "-10 dBV";
			break;
		case 1:
			tmp = "+4 dBu";
			break;
		default:
			tmp = "Lo Gain";
			break;
		}
		snd_iprintf(buffer, "AD Gain : %s\n", tmp);

		switch (hdsp_da_gain(hdsp)) {
		case 0:
			tmp = "Hi Gain";
			break;
		case 1:
			tmp = "+4 dBu";
			break;
		default:
			tmp = "-10 dBV";
			break;
		}
		snd_iprintf(buffer, "DA Gain : %s\n", tmp);
3663

Linus Torvalds's avatar
Linus Torvalds committed
3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676
		switch (hdsp_phone_gain(hdsp)) {
		case 0:
			tmp = "0 dB";
			break;
		case 1:
			tmp = "-6 dB";
			break;
		default:
			tmp = "-12 dB";
			break;
		}
		snd_iprintf(buffer, "Phones Gain : %s\n", tmp);

3677 3678 3679
		snd_iprintf(buffer, "XLR Breakout Cable : %s\n",
			hdsp_toggle_setting(hdsp, HDSP_XLRBreakoutCable) ?
			"yes" : "no");
3680

Takashi Iwai's avatar
Takashi Iwai committed
3681
		if (hdsp->control_register & HDSP_AnalogExtensionBoard)
Linus Torvalds's avatar
Linus Torvalds committed
3682
			snd_iprintf(buffer, "AEB : on (ADAT1 internal)\n");
Takashi Iwai's avatar
Takashi Iwai committed
3683
		else
Linus Torvalds's avatar
Linus Torvalds committed
3684 3685 3686 3687 3688 3689
			snd_iprintf(buffer, "AEB : off (ADAT1 external)\n");
		snd_iprintf(buffer, "\n");
	}

}

3690
static void snd_hdsp_proc_init(struct hdsp *hdsp)
Linus Torvalds's avatar
Linus Torvalds committed
3691
{
3692
	snd_card_ro_proc_new(hdsp->card, "hdsp", hdsp, snd_hdsp_proc_read);
Linus Torvalds's avatar
Linus Torvalds committed
3693 3694
}

3695
static void snd_hdsp_free_buffers(struct hdsp *hdsp)
Linus Torvalds's avatar
Linus Torvalds committed
3696 3697 3698 3699 3700
{
	snd_hammerfall_free_buffer(&hdsp->capture_dma_buf, hdsp->pci);
	snd_hammerfall_free_buffer(&hdsp->playback_dma_buf, hdsp->pci);
}

3701
static int snd_hdsp_initialize_memory(struct hdsp *hdsp)
Linus Torvalds's avatar
Linus Torvalds committed
3702 3703 3704 3705 3706 3707 3708
{
	unsigned long pb_bus, cb_bus;

	if (snd_hammerfall_get_buffer(hdsp->pci, &hdsp->capture_dma_buf, HDSP_DMA_AREA_BYTES) < 0 ||
	    snd_hammerfall_get_buffer(hdsp->pci, &hdsp->playback_dma_buf, HDSP_DMA_AREA_BYTES) < 0) {
		if (hdsp->capture_dma_buf.area)
			snd_dma_free_pages(&hdsp->capture_dma_buf);
3709 3710
		dev_err(hdsp->card->dev,
			"%s: no buffers available\n", hdsp->card_name);
Linus Torvalds's avatar
Linus Torvalds committed
3711 3712 3713 3714 3715
		return -ENOMEM;
	}

	/* Align to bus-space 64K boundary */

Clemens Ladisch's avatar
Clemens Ladisch committed
3716 3717
	cb_bus = ALIGN(hdsp->capture_dma_buf.addr, 0x10000ul);
	pb_bus = ALIGN(hdsp->playback_dma_buf.addr, 0x10000ul);
Linus Torvalds's avatar
Linus Torvalds committed
3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729

	/* Tell the card where it is */

	hdsp_write(hdsp, HDSP_inputBufferAddress, cb_bus);
	hdsp_write(hdsp, HDSP_outputBufferAddress, pb_bus);

	hdsp->capture_buffer = hdsp->capture_dma_buf.area + (cb_bus - hdsp->capture_dma_buf.addr);
	hdsp->playback_buffer = hdsp->playback_dma_buf.area + (pb_bus - hdsp->playback_dma_buf.addr);

	return 0;
}

3730
static int snd_hdsp_set_defaults(struct hdsp *hdsp)
Linus Torvalds's avatar
Linus Torvalds committed
3731 3732 3733 3734 3735
{
	unsigned int i;

	/* ASSUMPTION: hdsp->lock is either held, or
	   there is no need to hold it (e.g. during module
Joe Perches's avatar
Joe Perches committed
3736
	   initialization).
Linus Torvalds's avatar
Linus Torvalds committed
3737 3738 3739 3740
	 */

	/* set defaults:

3741
	   SPDIF Input via Coax
Linus Torvalds's avatar
Linus Torvalds committed
3742 3743 3744
	   Master clock mode
	   maximum latency (7 => 2^7 = 8192 samples, 64Kbyte buffer,
	                    which implies 2 4096 sample, 32Kbyte periods).
3745
           Enable line out.
Linus Torvalds's avatar
Linus Torvalds committed
3746 3747
	 */

3748 3749 3750
	hdsp->control_register = HDSP_ClockModeMaster |
		                 HDSP_SPDIFInputCoaxial |
		                 hdsp_encode_latency(7) |
Linus Torvalds's avatar
Linus Torvalds committed
3751
		                 HDSP_LineOut;
3752

Linus Torvalds's avatar
Linus Torvalds committed
3753 3754 3755 3756 3757 3758 3759 3760

	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);

#ifdef SNDRV_BIG_ENDIAN
	hdsp->control2_register = HDSP_BIGENDIAN_MODE;
#else
	hdsp->control2_register = 0;
#endif
Takashi Iwai's avatar
Takashi Iwai committed
3761
	if (hdsp->io_type == H9652)
Linus Torvalds's avatar
Linus Torvalds committed
3762
	        snd_hdsp_9652_enable_mixer (hdsp);
Takashi Iwai's avatar
Takashi Iwai committed
3763 3764
	else
		hdsp_write (hdsp, HDSP_control2Reg, hdsp->control2_register);
Linus Torvalds's avatar
Linus Torvalds committed
3765 3766 3767 3768 3769

	hdsp_reset_hw_pointer(hdsp);
	hdsp_compute_period_size(hdsp);

	/* silence everything */
3770

Takashi Iwai's avatar
Takashi Iwai committed
3771
	for (i = 0; i < HDSP_MATRIX_MIXER_SIZE; ++i)
Linus Torvalds's avatar
Linus Torvalds committed
3772 3773 3774
		hdsp->mixer_matrix[i] = MINUS_INFINITY_GAIN;

	for (i = 0; i < ((hdsp->io_type == H9652 || hdsp->io_type == H9632) ? 1352 : HDSP_MATRIX_MIXER_SIZE); ++i) {
Takashi Iwai's avatar
Takashi Iwai committed
3775
		if (hdsp_write_gain (hdsp, i, MINUS_INFINITY_GAIN))
Linus Torvalds's avatar
Linus Torvalds committed
3776 3777
			return -EIO;
	}
3778

Linus Torvalds's avatar
Linus Torvalds committed
3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794
	/* H9632 specific defaults */
	if (hdsp->io_type == H9632) {
		hdsp->control_register |= (HDSP_DAGainPlus4dBu | HDSP_ADGainPlus4dBu | HDSP_PhoneGain0dB);
		hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
	}

	/* set a default rate so that the channel map is set up.
	 */

	hdsp_set_rate(hdsp, 48000, 1);

	return 0;
}

static void hdsp_midi_tasklet(unsigned long arg)
{
3795
	struct hdsp *hdsp = (struct hdsp *)arg;
3796

Takashi Iwai's avatar
Takashi Iwai committed
3797
	if (hdsp->midi[0].pending)
Linus Torvalds's avatar
Linus Torvalds committed
3798
		snd_hdsp_midi_input_read (&hdsp->midi[0]);
Takashi Iwai's avatar
Takashi Iwai committed
3799
	if (hdsp->midi[1].pending)
Linus Torvalds's avatar
Linus Torvalds committed
3800
		snd_hdsp_midi_input_read (&hdsp->midi[1]);
3801
}
Linus Torvalds's avatar
Linus Torvalds committed
3802

3803
static irqreturn_t snd_hdsp_interrupt(int irq, void *dev_id)
Linus Torvalds's avatar
Linus Torvalds committed
3804
{
3805
	struct hdsp *hdsp = (struct hdsp *) dev_id;
Linus Torvalds's avatar
Linus Torvalds committed
3806 3807 3808 3809 3810 3811 3812
	unsigned int status;
	int audio;
	int midi0;
	int midi1;
	unsigned int midi0status;
	unsigned int midi1status;
	int schedule = 0;
3813

Linus Torvalds's avatar
Linus Torvalds committed
3814 3815 3816 3817 3818 3819
	status = hdsp_read(hdsp, HDSP_statusRegister);

	audio = status & HDSP_audioIRQPending;
	midi0 = status & HDSP_midi0IRQPending;
	midi1 = status & HDSP_midi1IRQPending;

Takashi Iwai's avatar
Takashi Iwai committed
3820
	if (!audio && !midi0 && !midi1)
Linus Torvalds's avatar
Linus Torvalds committed
3821 3822 3823 3824 3825 3826
		return IRQ_NONE;

	hdsp_write(hdsp, HDSP_interruptConfirmation, 0);

	midi0status = hdsp_read (hdsp, HDSP_midiStatusIn0) & 0xff;
	midi1status = hdsp_read (hdsp, HDSP_midiStatusIn1) & 0xff;
3827

3828 3829 3830
	if (!(hdsp->state & HDSP_InitializationComplete))
		return IRQ_HANDLED;

Linus Torvalds's avatar
Linus Torvalds committed
3831
	if (audio) {
Takashi Iwai's avatar
Takashi Iwai committed
3832
		if (hdsp->capture_substream)
Linus Torvalds's avatar
Linus Torvalds committed
3833
			snd_pcm_period_elapsed(hdsp->pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream);
3834

Takashi Iwai's avatar
Takashi Iwai committed
3835
		if (hdsp->playback_substream)
Linus Torvalds's avatar
Linus Torvalds committed
3836 3837
			snd_pcm_period_elapsed(hdsp->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream);
	}
3838

Linus Torvalds's avatar
Linus Torvalds committed
3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849
	if (midi0 && midi0status) {
		if (hdsp->use_midi_tasklet) {
			/* we disable interrupts for this input until processing is done */
			hdsp->control_register &= ~HDSP_Midi0InterruptEnable;
			hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
			hdsp->midi[0].pending = 1;
			schedule = 1;
		} else {
			snd_hdsp_midi_input_read (&hdsp->midi[0]);
		}
	}
3850
	if (hdsp->io_type != Multiface && hdsp->io_type != RPM && hdsp->io_type != H9632 && midi1 && midi1status) {
Linus Torvalds's avatar
Linus Torvalds committed
3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861
		if (hdsp->use_midi_tasklet) {
			/* we disable interrupts for this input until processing is done */
			hdsp->control_register &= ~HDSP_Midi1InterruptEnable;
			hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
			hdsp->midi[1].pending = 1;
			schedule = 1;
		} else {
			snd_hdsp_midi_input_read (&hdsp->midi[1]);
		}
	}
	if (hdsp->use_midi_tasklet && schedule)
3862
		tasklet_schedule(&hdsp->midi_tasklet);
Linus Torvalds's avatar
Linus Torvalds committed
3863 3864 3865
	return IRQ_HANDLED;
}

3866
static snd_pcm_uframes_t snd_hdsp_hw_pointer(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
3867
{
3868
	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds's avatar
Linus Torvalds committed
3869 3870 3871
	return hdsp_hw_pointer(hdsp);
}

3872
static char *hdsp_channel_buffer_location(struct hdsp *hdsp,
Linus Torvalds's avatar
Linus Torvalds committed
3873 3874 3875 3876 3877 3878
					     int stream,
					     int channel)

{
	int mapped_channel;

3879 3880
        if (snd_BUG_ON(channel < 0 || channel >= hdsp->max_channels))
		return NULL;
3881

Takashi Iwai's avatar
Takashi Iwai committed
3882
	if ((mapped_channel = hdsp->channel_map[channel]) < 0)
Linus Torvalds's avatar
Linus Torvalds committed
3883
		return NULL;
3884

Takashi Iwai's avatar
Takashi Iwai committed
3885
	if (stream == SNDRV_PCM_STREAM_CAPTURE)
Linus Torvalds's avatar
Linus Torvalds committed
3886
		return hdsp->capture_buffer + (mapped_channel * HDSP_CHANNEL_BUFFER_BYTES);
Takashi Iwai's avatar
Takashi Iwai committed
3887
	else
Linus Torvalds's avatar
Linus Torvalds committed
3888 3889 3890
		return hdsp->playback_buffer + (mapped_channel * HDSP_CHANNEL_BUFFER_BYTES);
}

3891 3892 3893
static int snd_hdsp_playback_copy(struct snd_pcm_substream *substream,
				  int channel, unsigned long pos,
				  void __user *src, unsigned long count)
Linus Torvalds's avatar
Linus Torvalds committed
3894
{
3895
	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds's avatar
Linus Torvalds committed
3896 3897
	char *channel_buf;

3898
	if (snd_BUG_ON(pos + count > HDSP_CHANNEL_BUFFER_BYTES))
3899
		return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
3900 3901

	channel_buf = hdsp_channel_buffer_location (hdsp, substream->pstr->stream, channel);
3902 3903
	if (snd_BUG_ON(!channel_buf))
		return -EIO;
3904
	if (copy_from_user(channel_buf + pos, src, count))
Linus Torvalds's avatar
Linus Torvalds committed
3905
		return -EFAULT;
3906
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
3907 3908
}

3909 3910 3911
static int snd_hdsp_playback_copy_kernel(struct snd_pcm_substream *substream,
					 int channel, unsigned long pos,
					 void *src, unsigned long count)
Linus Torvalds's avatar
Linus Torvalds committed
3912
{
3913
	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds's avatar
Linus Torvalds committed
3914 3915
	char *channel_buf;

3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930
	channel_buf = hdsp_channel_buffer_location(hdsp, substream->pstr->stream, channel);
	if (snd_BUG_ON(!channel_buf))
		return -EIO;
	memcpy(channel_buf + pos, src, count);
	return 0;
}

static int snd_hdsp_capture_copy(struct snd_pcm_substream *substream,
				 int channel, unsigned long pos,
				 void __user *dst, unsigned long count)
{
	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
	char *channel_buf;

	if (snd_BUG_ON(pos + count > HDSP_CHANNEL_BUFFER_BYTES))
3931
		return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
3932 3933

	channel_buf = hdsp_channel_buffer_location (hdsp, substream->pstr->stream, channel);
3934 3935
	if (snd_BUG_ON(!channel_buf))
		return -EIO;
3936
	if (copy_to_user(dst, channel_buf + pos, count))
Linus Torvalds's avatar
Linus Torvalds committed
3937
		return -EFAULT;
3938
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
3939 3940
}

3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957
static int snd_hdsp_capture_copy_kernel(struct snd_pcm_substream *substream,
					int channel, unsigned long pos,
					void *dst, unsigned long count)
{
	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
	char *channel_buf;

	channel_buf = hdsp_channel_buffer_location(hdsp, substream->pstr->stream, channel);
	if (snd_BUG_ON(!channel_buf))
		return -EIO;
	memcpy(dst, channel_buf + pos, count);
	return 0;
}

static int snd_hdsp_hw_silence(struct snd_pcm_substream *substream,
			       int channel, unsigned long pos,
			       unsigned long count)
Linus Torvalds's avatar
Linus Torvalds committed
3958
{
3959
	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds's avatar
Linus Torvalds committed
3960 3961 3962
	char *channel_buf;

	channel_buf = hdsp_channel_buffer_location (hdsp, substream->pstr->stream, channel);
3963 3964
	if (snd_BUG_ON(!channel_buf))
		return -EIO;
3965 3966
	memset(channel_buf + pos, 0, count);
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
3967 3968
}

3969
static int snd_hdsp_reset(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
3970
{
3971 3972 3973
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
	struct snd_pcm_substream *other;
Linus Torvalds's avatar
Linus Torvalds committed
3974 3975 3976 3977 3978 3979 3980 3981 3982
	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
		other = hdsp->capture_substream;
	else
		other = hdsp->playback_substream;
	if (hdsp->running)
		runtime->status->hw_ptr = hdsp_hw_pointer(hdsp);
	else
		runtime->status->hw_ptr = 0;
	if (other) {
3983 3984
		struct snd_pcm_substream *s;
		struct snd_pcm_runtime *oruntime = other->runtime;
3985
		snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds's avatar
Linus Torvalds committed
3986 3987 3988 3989 3990 3991 3992 3993 3994
			if (s == other) {
				oruntime->status->hw_ptr = runtime->status->hw_ptr;
				break;
			}
		}
	}
	return 0;
}

3995 3996
static int snd_hdsp_hw_params(struct snd_pcm_substream *substream,
				 struct snd_pcm_hw_params *params)
Linus Torvalds's avatar
Linus Torvalds committed
3997
{
3998
	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds's avatar
Linus Torvalds committed
3999 4000 4001 4002
	int err;
	pid_t this_pid;
	pid_t other_pid;

Takashi Iwai's avatar
Takashi Iwai committed
4003
	if (hdsp_check_for_iobox (hdsp))
Linus Torvalds's avatar
Linus Torvalds committed
4004 4005
		return -EIO;

Takashi Iwai's avatar
Takashi Iwai committed
4006
	if (hdsp_check_for_firmware(hdsp, 1))
Linus Torvalds's avatar
Linus Torvalds committed
4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052
		return -EIO;

	spin_lock_irq(&hdsp->lock);

	if (substream->pstr->stream == SNDRV_PCM_STREAM_PLAYBACK) {
		hdsp->control_register &= ~(HDSP_SPDIFProfessional | HDSP_SPDIFNonAudio | HDSP_SPDIFEmphasis);
		hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register |= hdsp->creg_spdif_stream);
		this_pid = hdsp->playback_pid;
		other_pid = hdsp->capture_pid;
	} else {
		this_pid = hdsp->capture_pid;
		other_pid = hdsp->playback_pid;
	}

	if ((other_pid > 0) && (this_pid != other_pid)) {

		/* The other stream is open, and not by the same
		   task as this one. Make sure that the parameters
		   that matter are the same.
		 */

		if (params_rate(params) != hdsp->system_sample_rate) {
			spin_unlock_irq(&hdsp->lock);
			_snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_RATE);
			return -EBUSY;
		}

		if (params_period_size(params) != hdsp->period_bytes / 4) {
			spin_unlock_irq(&hdsp->lock);
			_snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
			return -EBUSY;
		}

		/* We're fine. */

		spin_unlock_irq(&hdsp->lock);
 		return 0;

	} else {
		spin_unlock_irq(&hdsp->lock);
	}

	/* how to make sure that the rate matches an externally-set one ?
	 */

	spin_lock_irq(&hdsp->lock);
4053 4054 4055 4056 4057 4058
	if (! hdsp->clock_source_locked) {
		if ((err = hdsp_set_rate(hdsp, params_rate(params), 0)) < 0) {
			spin_unlock_irq(&hdsp->lock);
			_snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_RATE);
			return err;
		}
Linus Torvalds's avatar
Linus Torvalds committed
4059
	}
4060
	spin_unlock_irq(&hdsp->lock);
Linus Torvalds's avatar
Linus Torvalds committed
4061 4062 4063 4064 4065 4066 4067 4068 4069

	if ((err = hdsp_set_interrupt_interval(hdsp, params_period_size(params))) < 0) {
		_snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
		return err;
	}

	return 0;
}

4070 4071
static int snd_hdsp_channel_info(struct snd_pcm_substream *substream,
				    struct snd_pcm_channel_info *info)
Linus Torvalds's avatar
Linus Torvalds committed
4072
{
4073
	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
4074
	unsigned int channel = info->channel;
Linus Torvalds's avatar
Linus Torvalds committed
4075

4076
	if (snd_BUG_ON(channel >= hdsp->max_channels))
4077
		return -EINVAL;
4078
	channel = array_index_nospec(channel, hdsp->max_channels);
Linus Torvalds's avatar
Linus Torvalds committed
4079

4080
	if (hdsp->channel_map[channel] < 0)
Linus Torvalds's avatar
Linus Torvalds committed
4081 4082
		return -EINVAL;

4083
	info->offset = hdsp->channel_map[channel] * HDSP_CHANNEL_BUFFER_BYTES;
Linus Torvalds's avatar
Linus Torvalds committed
4084 4085 4086 4087 4088
	info->first = 0;
	info->step = 32;
	return 0;
}

4089
static int snd_hdsp_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds's avatar
Linus Torvalds committed
4090 4091 4092 4093 4094 4095
			     unsigned int cmd, void *arg)
{
	switch (cmd) {
	case SNDRV_PCM_IOCTL1_RESET:
		return snd_hdsp_reset(substream);
	case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
Takashi Iwai's avatar
Takashi Iwai committed
4096
		return snd_hdsp_channel_info(substream, arg);
Linus Torvalds's avatar
Linus Torvalds committed
4097 4098 4099 4100 4101 4102 4103
	default:
		break;
	}

	return snd_pcm_lib_ioctl(substream, cmd, arg);
}

4104
static int snd_hdsp_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds's avatar
Linus Torvalds committed
4105
{
4106 4107
	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
	struct snd_pcm_substream *other;
Linus Torvalds's avatar
Linus Torvalds committed
4108
	int running;
4109

Takashi Iwai's avatar
Takashi Iwai committed
4110
	if (hdsp_check_for_iobox (hdsp))
Linus Torvalds's avatar
Linus Torvalds committed
4111 4112
		return -EIO;

4113
	if (hdsp_check_for_firmware(hdsp, 0)) /* no auto-loading in trigger */
Linus Torvalds's avatar
Linus Torvalds committed
4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135
		return -EIO;

	spin_lock(&hdsp->lock);
	running = hdsp->running;
	switch (cmd) {
	case SNDRV_PCM_TRIGGER_START:
		running |= 1 << substream->stream;
		break;
	case SNDRV_PCM_TRIGGER_STOP:
		running &= ~(1 << substream->stream);
		break;
	default:
		snd_BUG();
		spin_unlock(&hdsp->lock);
		return -EINVAL;
	}
	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
		other = hdsp->capture_substream;
	else
		other = hdsp->playback_substream;

	if (other) {
4136
		struct snd_pcm_substream *s;
4137
		snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds's avatar
Linus Torvalds committed
4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171
			if (s == other) {
				snd_pcm_trigger_done(s, substream);
				if (cmd == SNDRV_PCM_TRIGGER_START)
					running |= 1 << s->stream;
				else
					running &= ~(1 << s->stream);
				goto _ok;
			}
		}
		if (cmd == SNDRV_PCM_TRIGGER_START) {
			if (!(running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) &&
			    substream->stream == SNDRV_PCM_STREAM_CAPTURE)
				hdsp_silence_playback(hdsp);
		} else {
			if (running &&
			    substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
				hdsp_silence_playback(hdsp);
		}
	} else {
		if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
				hdsp_silence_playback(hdsp);
	}
 _ok:
	snd_pcm_trigger_done(substream, substream);
	if (!hdsp->running && running)
		hdsp_start_audio(hdsp);
	else if (hdsp->running && !running)
		hdsp_stop_audio(hdsp);
	hdsp->running = running;
	spin_unlock(&hdsp->lock);

	return 0;
}

4172
static int snd_hdsp_prepare(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
4173
{
4174
	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds's avatar
Linus Torvalds committed
4175 4176
	int result = 0;

Takashi Iwai's avatar
Takashi Iwai committed
4177
	if (hdsp_check_for_iobox (hdsp))
Linus Torvalds's avatar
Linus Torvalds committed
4178 4179
		return -EIO;

Takashi Iwai's avatar
Takashi Iwai committed
4180
	if (hdsp_check_for_firmware(hdsp, 1))
Linus Torvalds's avatar
Linus Torvalds committed
4181 4182 4183 4184 4185 4186 4187 4188 4189
		return -EIO;

	spin_lock_irq(&hdsp->lock);
	if (!hdsp->running)
		hdsp_reset_hw_pointer(hdsp);
	spin_unlock_irq(&hdsp->lock);
	return result;
}

4190
static const struct snd_pcm_hardware snd_hdsp_playback_subinfo =
Linus Torvalds's avatar
Linus Torvalds committed
4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202
{
	.info =			(SNDRV_PCM_INFO_MMAP |
				 SNDRV_PCM_INFO_MMAP_VALID |
				 SNDRV_PCM_INFO_NONINTERLEAVED |
				 SNDRV_PCM_INFO_SYNC_START |
				 SNDRV_PCM_INFO_DOUBLE),
#ifdef SNDRV_BIG_ENDIAN
	.formats =		SNDRV_PCM_FMTBIT_S32_BE,
#else
	.formats =		SNDRV_PCM_FMTBIT_S32_LE,
#endif
	.rates =		(SNDRV_PCM_RATE_32000 |
4203 4204 4205 4206
				 SNDRV_PCM_RATE_44100 |
				 SNDRV_PCM_RATE_48000 |
				 SNDRV_PCM_RATE_64000 |
				 SNDRV_PCM_RATE_88200 |
Linus Torvalds's avatar
Linus Torvalds committed
4207 4208 4209
				 SNDRV_PCM_RATE_96000),
	.rate_min =		32000,
	.rate_max =		96000,
4210
	.channels_min =		6,
Linus Torvalds's avatar
Linus Torvalds committed
4211 4212 4213 4214 4215 4216 4217 4218 4219
	.channels_max =		HDSP_MAX_CHANNELS,
	.buffer_bytes_max =	HDSP_CHANNEL_BUFFER_BYTES * HDSP_MAX_CHANNELS,
	.period_bytes_min =	(64 * 4) * 10,
	.period_bytes_max =	(8192 * 4) * HDSP_MAX_CHANNELS,
	.periods_min =		2,
	.periods_max =		2,
	.fifo_size =		0
};

4220
static const struct snd_pcm_hardware snd_hdsp_capture_subinfo =
Linus Torvalds's avatar
Linus Torvalds committed
4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231
{
	.info =			(SNDRV_PCM_INFO_MMAP |
				 SNDRV_PCM_INFO_MMAP_VALID |
				 SNDRV_PCM_INFO_NONINTERLEAVED |
				 SNDRV_PCM_INFO_SYNC_START),
#ifdef SNDRV_BIG_ENDIAN
	.formats =		SNDRV_PCM_FMTBIT_S32_BE,
#else
	.formats =		SNDRV_PCM_FMTBIT_S32_LE,
#endif
	.rates =		(SNDRV_PCM_RATE_32000 |
4232 4233 4234 4235
				 SNDRV_PCM_RATE_44100 |
				 SNDRV_PCM_RATE_48000 |
				 SNDRV_PCM_RATE_64000 |
				 SNDRV_PCM_RATE_88200 |
Linus Torvalds's avatar
Linus Torvalds committed
4236 4237 4238
				 SNDRV_PCM_RATE_96000),
	.rate_min =		32000,
	.rate_max =		96000,
4239
	.channels_min =		5,
Linus Torvalds's avatar
Linus Torvalds committed
4240 4241 4242 4243 4244 4245 4246 4247 4248
	.channels_max =		HDSP_MAX_CHANNELS,
	.buffer_bytes_max =	HDSP_CHANNEL_BUFFER_BYTES * HDSP_MAX_CHANNELS,
	.period_bytes_min =	(64 * 4) * 10,
	.period_bytes_max =	(8192 * 4) * HDSP_MAX_CHANNELS,
	.periods_min =		2,
	.periods_max =		2,
	.fifo_size =		0
};

4249
static const unsigned int hdsp_period_sizes[] = { 64, 128, 256, 512, 1024, 2048, 4096, 8192 };
Linus Torvalds's avatar
Linus Torvalds committed
4250

4251
static const struct snd_pcm_hw_constraint_list hdsp_hw_constraints_period_sizes = {
Linus Torvalds's avatar
Linus Torvalds committed
4252 4253 4254 4255 4256
	.count = ARRAY_SIZE(hdsp_period_sizes),
	.list = hdsp_period_sizes,
	.mask = 0
};

4257
static const unsigned int hdsp_9632_sample_rates[] = { 32000, 44100, 48000, 64000, 88200, 96000, 128000, 176400, 192000 };
Linus Torvalds's avatar
Linus Torvalds committed
4258

4259
static const struct snd_pcm_hw_constraint_list hdsp_hw_constraints_9632_sample_rates = {
Linus Torvalds's avatar
Linus Torvalds committed
4260 4261 4262 4263 4264
	.count = ARRAY_SIZE(hdsp_9632_sample_rates),
	.list = hdsp_9632_sample_rates,
	.mask = 0
};

4265 4266
static int snd_hdsp_hw_rule_in_channels(struct snd_pcm_hw_params *params,
					struct snd_pcm_hw_rule *rule)
Linus Torvalds's avatar
Linus Torvalds committed
4267
{
4268 4269
	struct hdsp *hdsp = rule->private;
	struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
Linus Torvalds's avatar
Linus Torvalds committed
4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283
	if (hdsp->io_type == H9632) {
		unsigned int list[3];
		list[0] = hdsp->qs_in_channels;
		list[1] = hdsp->ds_in_channels;
		list[2] = hdsp->ss_in_channels;
		return snd_interval_list(c, 3, list, 0);
	} else {
		unsigned int list[2];
		list[0] = hdsp->ds_in_channels;
		list[1] = hdsp->ss_in_channels;
		return snd_interval_list(c, 2, list, 0);
	}
}

4284 4285
static int snd_hdsp_hw_rule_out_channels(struct snd_pcm_hw_params *params,
					struct snd_pcm_hw_rule *rule)
Linus Torvalds's avatar
Linus Torvalds committed
4286 4287
{
	unsigned int list[3];
4288 4289
	struct hdsp *hdsp = rule->private;
	struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
Linus Torvalds's avatar
Linus Torvalds committed
4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301
	if (hdsp->io_type == H9632) {
		list[0] = hdsp->qs_out_channels;
		list[1] = hdsp->ds_out_channels;
		list[2] = hdsp->ss_out_channels;
		return snd_interval_list(c, 3, list, 0);
	} else {
		list[0] = hdsp->ds_out_channels;
		list[1] = hdsp->ss_out_channels;
	}
	return snd_interval_list(c, 2, list, 0);
}

4302 4303
static int snd_hdsp_hw_rule_in_channels_rate(struct snd_pcm_hw_params *params,
					     struct snd_pcm_hw_rule *rule)
Linus Torvalds's avatar
Linus Torvalds committed
4304
{
4305 4306 4307
	struct hdsp *hdsp = rule->private;
	struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
	struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
Linus Torvalds's avatar
Linus Torvalds committed
4308
	if (r->min > 96000 && hdsp->io_type == H9632) {
4309
		struct snd_interval t = {
Linus Torvalds's avatar
Linus Torvalds committed
4310 4311 4312 4313
			.min = hdsp->qs_in_channels,
			.max = hdsp->qs_in_channels,
			.integer = 1,
		};
4314
		return snd_interval_refine(c, &t);
Linus Torvalds's avatar
Linus Torvalds committed
4315
	} else if (r->min > 48000 && r->max <= 96000) {
4316
		struct snd_interval t = {
Linus Torvalds's avatar
Linus Torvalds committed
4317 4318 4319 4320 4321 4322
			.min = hdsp->ds_in_channels,
			.max = hdsp->ds_in_channels,
			.integer = 1,
		};
		return snd_interval_refine(c, &t);
	} else if (r->max < 64000) {
4323
		struct snd_interval t = {
Linus Torvalds's avatar
Linus Torvalds committed
4324 4325 4326 4327 4328 4329 4330 4331 4332
			.min = hdsp->ss_in_channels,
			.max = hdsp->ss_in_channels,
			.integer = 1,
		};
		return snd_interval_refine(c, &t);
	}
	return 0;
}

4333 4334
static int snd_hdsp_hw_rule_out_channels_rate(struct snd_pcm_hw_params *params,
					     struct snd_pcm_hw_rule *rule)
Linus Torvalds's avatar
Linus Torvalds committed
4335
{
4336 4337 4338
	struct hdsp *hdsp = rule->private;
	struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
	struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
Linus Torvalds's avatar
Linus Torvalds committed
4339
	if (r->min > 96000 && hdsp->io_type == H9632) {
4340
		struct snd_interval t = {
Linus Torvalds's avatar
Linus Torvalds committed
4341 4342 4343 4344
			.min = hdsp->qs_out_channels,
			.max = hdsp->qs_out_channels,
			.integer = 1,
		};
4345
		return snd_interval_refine(c, &t);
Linus Torvalds's avatar
Linus Torvalds committed
4346
	} else if (r->min > 48000 && r->max <= 96000) {
4347
		struct snd_interval t = {
Linus Torvalds's avatar
Linus Torvalds committed
4348 4349 4350 4351 4352 4353
			.min = hdsp->ds_out_channels,
			.max = hdsp->ds_out_channels,
			.integer = 1,
		};
		return snd_interval_refine(c, &t);
	} else if (r->max < 64000) {
4354
		struct snd_interval t = {
Linus Torvalds's avatar
Linus Torvalds committed
4355 4356 4357 4358 4359 4360 4361 4362 4363
			.min = hdsp->ss_out_channels,
			.max = hdsp->ss_out_channels,
			.integer = 1,
		};
		return snd_interval_refine(c, &t);
	}
	return 0;
}

4364 4365
static int snd_hdsp_hw_rule_rate_out_channels(struct snd_pcm_hw_params *params,
					     struct snd_pcm_hw_rule *rule)
Linus Torvalds's avatar
Linus Torvalds committed
4366
{
4367 4368 4369
	struct hdsp *hdsp = rule->private;
	struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
	struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
Linus Torvalds's avatar
Linus Torvalds committed
4370
	if (c->min >= hdsp->ss_out_channels) {
4371
		struct snd_interval t = {
Linus Torvalds's avatar
Linus Torvalds committed
4372 4373 4374 4375 4376 4377
			.min = 32000,
			.max = 48000,
			.integer = 1,
		};
		return snd_interval_refine(r, &t);
	} else if (c->max <= hdsp->qs_out_channels && hdsp->io_type == H9632) {
4378
		struct snd_interval t = {
Linus Torvalds's avatar
Linus Torvalds committed
4379 4380 4381 4382 4383 4384
			.min = 128000,
			.max = 192000,
			.integer = 1,
		};
		return snd_interval_refine(r, &t);
	} else if (c->max <= hdsp->ds_out_channels) {
4385
		struct snd_interval t = {
Linus Torvalds's avatar
Linus Torvalds committed
4386 4387 4388 4389 4390 4391 4392 4393 4394
			.min = 64000,
			.max = 96000,
			.integer = 1,
		};
		return snd_interval_refine(r, &t);
	}
	return 0;
}

4395 4396
static int snd_hdsp_hw_rule_rate_in_channels(struct snd_pcm_hw_params *params,
					     struct snd_pcm_hw_rule *rule)
Linus Torvalds's avatar
Linus Torvalds committed
4397
{
4398 4399 4400
	struct hdsp *hdsp = rule->private;
	struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
	struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
Linus Torvalds's avatar
Linus Torvalds committed
4401
	if (c->min >= hdsp->ss_in_channels) {
4402
		struct snd_interval t = {
Linus Torvalds's avatar
Linus Torvalds committed
4403 4404 4405 4406 4407 4408
			.min = 32000,
			.max = 48000,
			.integer = 1,
		};
		return snd_interval_refine(r, &t);
	} else if (c->max <= hdsp->qs_in_channels && hdsp->io_type == H9632) {
4409
		struct snd_interval t = {
Linus Torvalds's avatar
Linus Torvalds committed
4410 4411 4412 4413 4414 4415
			.min = 128000,
			.max = 192000,
			.integer = 1,
		};
		return snd_interval_refine(r, &t);
	} else if (c->max <= hdsp->ds_in_channels) {
4416
		struct snd_interval t = {
Linus Torvalds's avatar
Linus Torvalds committed
4417 4418 4419 4420 4421 4422 4423 4424 4425
			.min = 64000,
			.max = 96000,
			.integer = 1,
		};
		return snd_interval_refine(r, &t);
	}
	return 0;
}

4426
static int snd_hdsp_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
4427
{
4428 4429
	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds's avatar
Linus Torvalds committed
4430

Takashi Iwai's avatar
Takashi Iwai committed
4431
	if (hdsp_check_for_iobox (hdsp))
Linus Torvalds's avatar
Linus Torvalds committed
4432 4433
		return -EIO;

Takashi Iwai's avatar
Takashi Iwai committed
4434
	if (hdsp_check_for_firmware(hdsp, 1))
Linus Torvalds's avatar
Linus Torvalds committed
4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451
		return -EIO;

	spin_lock_irq(&hdsp->lock);

	snd_pcm_set_sync(substream);

        runtime->hw = snd_hdsp_playback_subinfo;
	runtime->dma_area = hdsp->playback_buffer;
	runtime->dma_bytes = HDSP_DMA_AREA_BYTES;

	hdsp->playback_pid = current->pid;
	hdsp->playback_substream = substream;

	spin_unlock_irq(&hdsp->lock);

	snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, &hdsp_hw_constraints_period_sizes);
4452 4453 4454
	if (hdsp->clock_source_locked) {
		runtime->hw.rate_min = runtime->hw.rate_max = hdsp->system_sample_rate;
	} else if (hdsp->io_type == H9632) {
Linus Torvalds's avatar
Linus Torvalds committed
4455 4456 4457 4458
		runtime->hw.rate_max = 192000;
		runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
		snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hdsp_hw_constraints_9632_sample_rates);
	}
4459 4460 4461
	if (hdsp->io_type == H9632) {
		runtime->hw.channels_min = hdsp->qs_out_channels;
		runtime->hw.channels_max = hdsp->ss_out_channels;
4462 4463
	}

Linus Torvalds's avatar
Linus Torvalds committed
4464 4465 4466 4467 4468 4469 4470 4471 4472 4473
	snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
			     snd_hdsp_hw_rule_out_channels, hdsp,
			     SNDRV_PCM_HW_PARAM_CHANNELS, -1);
	snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
			     snd_hdsp_hw_rule_out_channels_rate, hdsp,
			     SNDRV_PCM_HW_PARAM_RATE, -1);
	snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
			     snd_hdsp_hw_rule_rate_out_channels, hdsp,
			     SNDRV_PCM_HW_PARAM_CHANNELS, -1);

4474 4475 4476 4477 4478 4479
	if (RPM != hdsp->io_type) {
		hdsp->creg_spdif_stream = hdsp->creg_spdif;
		hdsp->spdif_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
		snd_ctl_notify(hdsp->card, SNDRV_CTL_EVENT_MASK_VALUE |
			SNDRV_CTL_EVENT_MASK_INFO, &hdsp->spdif_ctl->id);
	}
Linus Torvalds's avatar
Linus Torvalds committed
4480 4481 4482
	return 0;
}

4483
static int snd_hdsp_playback_release(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
4484
{
4485
	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds's avatar
Linus Torvalds committed
4486 4487 4488 4489 4490 4491 4492 4493

	spin_lock_irq(&hdsp->lock);

	hdsp->playback_pid = -1;
	hdsp->playback_substream = NULL;

	spin_unlock_irq(&hdsp->lock);

4494 4495 4496 4497 4498
	if (RPM != hdsp->io_type) {
		hdsp->spdif_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
		snd_ctl_notify(hdsp->card, SNDRV_CTL_EVENT_MASK_VALUE |
			SNDRV_CTL_EVENT_MASK_INFO, &hdsp->spdif_ctl->id);
	}
Linus Torvalds's avatar
Linus Torvalds committed
4499 4500 4501 4502
	return 0;
}


4503
static int snd_hdsp_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
4504
{
4505 4506
	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds's avatar
Linus Torvalds committed
4507

Takashi Iwai's avatar
Takashi Iwai committed
4508
	if (hdsp_check_for_iobox (hdsp))
Linus Torvalds's avatar
Linus Torvalds committed
4509 4510
		return -EIO;

Takashi Iwai's avatar
Takashi Iwai committed
4511
	if (hdsp_check_for_firmware(hdsp, 1))
Linus Torvalds's avatar
Linus Torvalds committed
4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547
		return -EIO;

	spin_lock_irq(&hdsp->lock);

	snd_pcm_set_sync(substream);

	runtime->hw = snd_hdsp_capture_subinfo;
	runtime->dma_area = hdsp->capture_buffer;
	runtime->dma_bytes = HDSP_DMA_AREA_BYTES;

	hdsp->capture_pid = current->pid;
	hdsp->capture_substream = substream;

	spin_unlock_irq(&hdsp->lock);

	snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, &hdsp_hw_constraints_period_sizes);
	if (hdsp->io_type == H9632) {
		runtime->hw.channels_min = hdsp->qs_in_channels;
		runtime->hw.channels_max = hdsp->ss_in_channels;
		runtime->hw.rate_max = 192000;
		runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
		snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hdsp_hw_constraints_9632_sample_rates);
	}
	snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
			     snd_hdsp_hw_rule_in_channels, hdsp,
			     SNDRV_PCM_HW_PARAM_CHANNELS, -1);
	snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
			     snd_hdsp_hw_rule_in_channels_rate, hdsp,
			     SNDRV_PCM_HW_PARAM_RATE, -1);
	snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
			     snd_hdsp_hw_rule_rate_in_channels, hdsp,
			     SNDRV_PCM_HW_PARAM_CHANNELS, -1);
	return 0;
}

4548
static int snd_hdsp_capture_release(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
4549
{
4550
	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
Linus Torvalds's avatar
Linus Torvalds committed
4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587

	spin_lock_irq(&hdsp->lock);

	hdsp->capture_pid = -1;
	hdsp->capture_substream = NULL;

	spin_unlock_irq(&hdsp->lock);
	return 0;
}

/* helper functions for copying meter values */
static inline int copy_u32_le(void __user *dest, void __iomem *src)
{
	u32 val = readl(src);
	return copy_to_user(dest, &val, 4);
}

static inline int copy_u64_le(void __user *dest, void __iomem *src_low, void __iomem *src_high)
{
	u32 rms_low, rms_high;
	u64 rms;
	rms_low = readl(src_low);
	rms_high = readl(src_high);
	rms = ((u64)rms_high << 32) | rms_low;
	return copy_to_user(dest, &rms, 8);
}

static inline int copy_u48_le(void __user *dest, void __iomem *src_low, void __iomem *src_high)
{
	u32 rms_low, rms_high;
	u64 rms;
	rms_low = readl(src_low) & 0xffffff00;
	rms_high = readl(src_high) & 0xffffff00;
	rms = ((u64)rms_high << 32) | rms_low;
	return copy_to_user(dest, &rms, 8);
}

4588
static int hdsp_9652_get_peak(struct hdsp *hdsp, struct hdsp_peak_rms __user *peak_rms)
Linus Torvalds's avatar
Linus Torvalds committed
4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624
{
	int doublespeed = 0;
	int i, j, channels, ofs;

	if (hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DoubleSpeedStatus)
		doublespeed = 1;
	channels = doublespeed ? 14 : 26;
	for (i = 0, j = 0; i < 26; ++i) {
		if (doublespeed && (i & 4))
			continue;
		ofs = HDSP_9652_peakBase - j * 4;
		if (copy_u32_le(&peak_rms->input_peaks[i], hdsp->iobase + ofs))
			return -EFAULT;
		ofs -= channels * 4;
		if (copy_u32_le(&peak_rms->playback_peaks[i], hdsp->iobase + ofs))
			return -EFAULT;
		ofs -= channels * 4;
		if (copy_u32_le(&peak_rms->output_peaks[i], hdsp->iobase + ofs))
			return -EFAULT;
		ofs = HDSP_9652_rmsBase + j * 8;
		if (copy_u48_le(&peak_rms->input_rms[i], hdsp->iobase + ofs,
				hdsp->iobase + ofs + 4))
			return -EFAULT;
		ofs += channels * 8;
		if (copy_u48_le(&peak_rms->playback_rms[i], hdsp->iobase + ofs,
				hdsp->iobase + ofs + 4))
			return -EFAULT;
		ofs += channels * 8;
		if (copy_u48_le(&peak_rms->output_rms[i], hdsp->iobase + ofs,
				hdsp->iobase + ofs + 4))
			return -EFAULT;
		j++;
	}
	return 0;
}

4625
static int hdsp_9632_get_peak(struct hdsp *hdsp, struct hdsp_peak_rms __user *peak_rms)
Linus Torvalds's avatar
Linus Torvalds committed
4626 4627
{
	int i, j;
4628
	struct hdsp_9632_meters __iomem *m;
Linus Torvalds's avatar
Linus Torvalds committed
4629 4630 4631 4632
	int doublespeed = 0;

	if (hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DoubleSpeedStatus)
		doublespeed = 1;
4633
	m = (struct hdsp_9632_meters __iomem *)(hdsp->iobase+HDSP_9632_metersBase);
Linus Torvalds's avatar
Linus Torvalds committed
4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654
	for (i = 0, j = 0; i < 16; ++i, ++j) {
		if (copy_u32_le(&peak_rms->input_peaks[i], &m->input_peak[j]))
			return -EFAULT;
		if (copy_u32_le(&peak_rms->playback_peaks[i], &m->playback_peak[j]))
			return -EFAULT;
		if (copy_u32_le(&peak_rms->output_peaks[i], &m->output_peak[j]))
			return -EFAULT;
		if (copy_u64_le(&peak_rms->input_rms[i], &m->input_rms_low[j],
				&m->input_rms_high[j]))
			return -EFAULT;
		if (copy_u64_le(&peak_rms->playback_rms[i], &m->playback_rms_low[j],
				&m->playback_rms_high[j]))
			return -EFAULT;
		if (copy_u64_le(&peak_rms->output_rms[i], &m->output_rms_low[j],
				&m->output_rms_high[j]))
			return -EFAULT;
		if (doublespeed && i == 3) i += 4;
	}
	return 0;
}

4655
static int hdsp_get_peak(struct hdsp *hdsp, struct hdsp_peak_rms __user *peak_rms)
Linus Torvalds's avatar
Linus Torvalds committed
4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676
{
	int i;

	for (i = 0; i < 26; i++) {
		if (copy_u32_le(&peak_rms->playback_peaks[i],
				hdsp->iobase + HDSP_playbackPeakLevel + i * 4))
			return -EFAULT;
		if (copy_u32_le(&peak_rms->input_peaks[i],
				hdsp->iobase + HDSP_inputPeakLevel + i * 4))
			return -EFAULT;
	}
	for (i = 0; i < 28; i++) {
		if (copy_u32_le(&peak_rms->output_peaks[i],
				hdsp->iobase + HDSP_outputPeakLevel + i * 4))
			return -EFAULT;
	}
	for (i = 0; i < 26; ++i) {
		if (copy_u64_le(&peak_rms->playback_rms[i],
				hdsp->iobase + HDSP_playbackRmsLevel + i * 8 + 4,
				hdsp->iobase + HDSP_playbackRmsLevel + i * 8))
			return -EFAULT;
4677
		if (copy_u64_le(&peak_rms->input_rms[i],
Linus Torvalds's avatar
Linus Torvalds committed
4678 4679 4680 4681 4682 4683 4684
				hdsp->iobase + HDSP_inputRmsLevel + i * 8 + 4,
				hdsp->iobase + HDSP_inputRmsLevel + i * 8))
			return -EFAULT;
	}
	return 0;
}

4685
static int snd_hdsp_hwdep_ioctl(struct snd_hwdep *hw, struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds's avatar
Linus Torvalds committed
4686
{
4687
	struct hdsp *hdsp = hw->private_data;
Linus Torvalds's avatar
Linus Torvalds committed
4688
	void __user *argp = (void __user *)arg;
4689
	int err;
Linus Torvalds's avatar
Linus Torvalds committed
4690 4691 4692

	switch (cmd) {
	case SNDRV_HDSP_IOCTL_GET_PEAK_RMS: {
4693
		struct hdsp_peak_rms __user *peak_rms = (struct hdsp_peak_rms __user *)arg;
Linus Torvalds's avatar
Linus Torvalds committed
4694

4695 4696 4697 4698 4699 4700 4701 4702
		err = hdsp_check_for_iobox(hdsp);
		if (err < 0)
			return err;

		err = hdsp_check_for_firmware(hdsp, 1);
		if (err < 0)
			return err;

Linus Torvalds's avatar
Linus Torvalds committed
4703
		if (!(hdsp->state & HDSP_FirmwareLoaded)) {
4704 4705
			dev_err(hdsp->card->dev,
				"firmware needs to be uploaded to the card.\n");
Linus Torvalds's avatar
Linus Torvalds committed
4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718
			return -EINVAL;
		}

		switch (hdsp->io_type) {
		case H9652:
			return hdsp_9652_get_peak(hdsp, peak_rms);
		case H9632:
			return hdsp_9632_get_peak(hdsp, peak_rms);
		default:
			return hdsp_get_peak(hdsp, peak_rms);
		}
	}
	case SNDRV_HDSP_IOCTL_GET_CONFIG_INFO: {
4719
		struct hdsp_config_info info;
Linus Torvalds's avatar
Linus Torvalds committed
4720 4721
		unsigned long flags;
		int i;
4722

4723 4724 4725 4726 4727 4728 4729 4730
		err = hdsp_check_for_iobox(hdsp);
		if (err < 0)
			return err;

		err = hdsp_check_for_firmware(hdsp, 1);
		if (err < 0)
			return err;

4731
		memset(&info, 0, sizeof(info));
Linus Torvalds's avatar
Linus Torvalds committed
4732 4733 4734
		spin_lock_irqsave(&hdsp->lock, flags);
		info.pref_sync_ref = (unsigned char)hdsp_pref_sync_ref(hdsp);
		info.wordclock_sync_check = (unsigned char)hdsp_wc_sync_check(hdsp);
Takashi Iwai's avatar
Takashi Iwai committed
4735
		if (hdsp->io_type != H9632)
Linus Torvalds's avatar
Linus Torvalds committed
4736 4737
		    info.adatsync_sync_check = (unsigned char)hdsp_adatsync_sync_check(hdsp);
		info.spdif_sync_check = (unsigned char)hdsp_spdif_sync_check(hdsp);
4738
		for (i = 0; i < ((hdsp->io_type != Multiface && hdsp->io_type != RPM && hdsp->io_type != H9632) ? 3 : 1); ++i)
Linus Torvalds's avatar
Linus Torvalds committed
4739 4740
			info.adat_sync_check[i] = (unsigned char)hdsp_adat_sync_check(hdsp, i);
		info.spdif_in = (unsigned char)hdsp_spdif_in(hdsp);
4741 4742 4743 4744 4745 4746 4747 4748
		info.spdif_out = (unsigned char)hdsp_toggle_setting(hdsp,
				HDSP_SPDIFOpticalOut);
		info.spdif_professional = (unsigned char)
			hdsp_toggle_setting(hdsp, HDSP_SPDIFProfessional);
		info.spdif_emphasis = (unsigned char)
			hdsp_toggle_setting(hdsp, HDSP_SPDIFEmphasis);
		info.spdif_nonaudio = (unsigned char)
			hdsp_toggle_setting(hdsp, HDSP_SPDIFNonAudio);
Linus Torvalds's avatar
Linus Torvalds committed
4749 4750 4751 4752 4753 4754
		info.spdif_sample_rate = hdsp_spdif_sample_rate(hdsp);
		info.system_sample_rate = hdsp->system_sample_rate;
		info.autosync_sample_rate = hdsp_external_sample_rate(hdsp);
		info.system_clock_mode = (unsigned char)hdsp_system_clock_mode(hdsp);
		info.clock_source = (unsigned char)hdsp_clock_source(hdsp);
		info.autosync_ref = (unsigned char)hdsp_autosync_ref(hdsp);
4755 4756
		info.line_out = (unsigned char)
			hdsp_toggle_setting(hdsp, HDSP_LineOut);
Linus Torvalds's avatar
Linus Torvalds committed
4757 4758 4759 4760
		if (hdsp->io_type == H9632) {
			info.da_gain = (unsigned char)hdsp_da_gain(hdsp);
			info.ad_gain = (unsigned char)hdsp_ad_gain(hdsp);
			info.phone_gain = (unsigned char)hdsp_phone_gain(hdsp);
4761 4762 4763
			info.xlr_breakout_cable =
				(unsigned char)hdsp_toggle_setting(hdsp,
					HDSP_XLRBreakoutCable);
4764

4765 4766 4767
		} else if (hdsp->io_type == RPM) {
			info.da_gain = (unsigned char) hdsp_rpm_input12(hdsp);
			info.ad_gain = (unsigned char) hdsp_rpm_input34(hdsp);
Linus Torvalds's avatar
Linus Torvalds committed
4768
		}
Takashi Iwai's avatar
Takashi Iwai committed
4769
		if (hdsp->io_type == H9632 || hdsp->io_type == H9652)
4770 4771 4772
			info.analog_extension_board =
				(unsigned char)hdsp_toggle_setting(hdsp,
					    HDSP_AnalogExtensionBoard);
Linus Torvalds's avatar
Linus Torvalds committed
4773 4774 4775 4776 4777 4778
		spin_unlock_irqrestore(&hdsp->lock, flags);
		if (copy_to_user(argp, &info, sizeof(info)))
			return -EFAULT;
		break;
	}
	case SNDRV_HDSP_IOCTL_GET_9632_AEB: {
4779
		struct hdsp_9632_aeb h9632_aeb;
4780

Linus Torvalds's avatar
Linus Torvalds committed
4781 4782 4783 4784 4785 4786 4787 4788
		if (hdsp->io_type != H9632) return -EINVAL;
		h9632_aeb.aebi = hdsp->ss_in_channels - H9632_SS_CHANNELS;
		h9632_aeb.aebo = hdsp->ss_out_channels - H9632_SS_CHANNELS;
		if (copy_to_user(argp, &h9632_aeb, sizeof(h9632_aeb)))
			return -EFAULT;
		break;
	}
	case SNDRV_HDSP_IOCTL_GET_VERSION: {
4789
		struct hdsp_version hdsp_version;
Linus Torvalds's avatar
Linus Torvalds committed
4790
		int err;
4791

Linus Torvalds's avatar
Linus Torvalds committed
4792 4793
		if (hdsp->io_type == H9652 || hdsp->io_type == H9632) return -EINVAL;
		if (hdsp->io_type == Undefined) {
Takashi Iwai's avatar
Takashi Iwai committed
4794
			if ((err = hdsp_get_iobox_version(hdsp)) < 0)
Linus Torvalds's avatar
Linus Torvalds committed
4795 4796
				return err;
		}
4797
		memset(&hdsp_version, 0, sizeof(hdsp_version));
Linus Torvalds's avatar
Linus Torvalds committed
4798 4799
		hdsp_version.io_type = hdsp->io_type;
		hdsp_version.firmware_rev = hdsp->firmware_rev;
Takashi Iwai's avatar
Takashi Iwai committed
4800
		if ((err = copy_to_user(argp, &hdsp_version, sizeof(hdsp_version))))
Linus Torvalds's avatar
Linus Torvalds committed
4801 4802 4803 4804
		    	return -EFAULT;
		break;
	}
	case SNDRV_HDSP_IOCTL_UPLOAD_FIRMWARE: {
4805
		struct hdsp_firmware __user *firmware;
Linus Torvalds's avatar
Linus Torvalds committed
4806 4807
		u32 __user *firmware_data;
		int err;
4808

Linus Torvalds's avatar
Linus Torvalds committed
4809 4810 4811 4812 4813 4814 4815
		if (hdsp->io_type == H9652 || hdsp->io_type == H9632) return -EINVAL;
		/* SNDRV_HDSP_IOCTL_GET_VERSION must have been called */
		if (hdsp->io_type == Undefined) return -EINVAL;

		if (hdsp->state & (HDSP_FirmwareCached | HDSP_FirmwareLoaded))
			return -EBUSY;

4816 4817
		dev_info(hdsp->card->dev,
			 "initializing firmware upload\n");
4818
		firmware = (struct hdsp_firmware __user *)argp;
Linus Torvalds's avatar
Linus Torvalds committed
4819

Takashi Iwai's avatar
Takashi Iwai committed
4820
		if (get_user(firmware_data, &firmware->firmware_data))
Linus Torvalds's avatar
Linus Torvalds committed
4821
			return -EFAULT;
4822

Takashi Iwai's avatar
Takashi Iwai committed
4823
		if (hdsp_check_for_iobox (hdsp))
Linus Torvalds's avatar
Linus Torvalds committed
4824 4825
			return -EIO;

4826 4827 4828 4829 4830 4831 4832 4833 4834 4835
		if (!hdsp->fw_uploaded) {
			hdsp->fw_uploaded = vmalloc(HDSP_FIRMWARE_SIZE);
			if (!hdsp->fw_uploaded)
				return -ENOMEM;
		}

		if (copy_from_user(hdsp->fw_uploaded, firmware_data,
				   HDSP_FIRMWARE_SIZE)) {
			vfree(hdsp->fw_uploaded);
			hdsp->fw_uploaded = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
4836
			return -EFAULT;
4837
		}
4838

Linus Torvalds's avatar
Linus Torvalds committed
4839 4840
		hdsp->state |= HDSP_FirmwareCached;

Takashi Iwai's avatar
Takashi Iwai committed
4841
		if ((err = snd_hdsp_load_firmware_from_cache(hdsp)) < 0)
Linus Torvalds's avatar
Linus Torvalds committed
4842
			return err;
4843

Linus Torvalds's avatar
Linus Torvalds committed
4844
		if (!(hdsp->state & HDSP_InitializationComplete)) {
Takashi Iwai's avatar
Takashi Iwai committed
4845
			if ((err = snd_hdsp_enable_io(hdsp)) < 0)
Linus Torvalds's avatar
Linus Torvalds committed
4846
				return err;
4847 4848

			snd_hdsp_initialize_channels(hdsp);
Linus Torvalds's avatar
Linus Torvalds committed
4849
			snd_hdsp_initialize_midi_flush(hdsp);
4850

Linus Torvalds's avatar
Linus Torvalds committed
4851
			if ((err = snd_hdsp_create_alsa_devices(hdsp->card, hdsp)) < 0) {
4852 4853
				dev_err(hdsp->card->dev,
					"error creating alsa devices\n");
Takashi Iwai's avatar
Takashi Iwai committed
4854
				return err;
Linus Torvalds's avatar
Linus Torvalds committed
4855 4856 4857 4858 4859
			}
		}
		break;
	}
	case SNDRV_HDSP_IOCTL_GET_MIXER: {
4860
		struct hdsp_mixer __user *mixer = (struct hdsp_mixer __user *)argp;
Linus Torvalds's avatar
Linus Torvalds committed
4861 4862 4863 4864 4865 4866 4867 4868 4869 4870
		if (copy_to_user(mixer->matrix, hdsp->mixer_matrix, sizeof(unsigned short)*HDSP_MATRIX_MIXER_SIZE))
			return -EFAULT;
		break;
	}
	default:
		return -EINVAL;
	}
	return 0;
}

4871
static const struct snd_pcm_ops snd_hdsp_playback_ops = {
Linus Torvalds's avatar
Linus Torvalds committed
4872 4873 4874 4875 4876 4877 4878
	.open =		snd_hdsp_playback_open,
	.close =	snd_hdsp_playback_release,
	.ioctl =	snd_hdsp_ioctl,
	.hw_params =	snd_hdsp_hw_params,
	.prepare =	snd_hdsp_prepare,
	.trigger =	snd_hdsp_trigger,
	.pointer =	snd_hdsp_hw_pointer,
4879 4880 4881
	.copy_user =	snd_hdsp_playback_copy,
	.copy_kernel =	snd_hdsp_playback_copy_kernel,
	.fill_silence =	snd_hdsp_hw_silence,
Linus Torvalds's avatar
Linus Torvalds committed
4882 4883
};

4884
static const struct snd_pcm_ops snd_hdsp_capture_ops = {
Linus Torvalds's avatar
Linus Torvalds committed
4885 4886 4887 4888 4889 4890 4891
	.open =		snd_hdsp_capture_open,
	.close =	snd_hdsp_capture_release,
	.ioctl =	snd_hdsp_ioctl,
	.hw_params =	snd_hdsp_hw_params,
	.prepare =	snd_hdsp_prepare,
	.trigger =	snd_hdsp_trigger,
	.pointer =	snd_hdsp_hw_pointer,
4892 4893
	.copy_user =	snd_hdsp_capture_copy,
	.copy_kernel =	snd_hdsp_capture_copy_kernel,
Linus Torvalds's avatar
Linus Torvalds committed
4894 4895
};

4896
static int snd_hdsp_create_hwdep(struct snd_card *card, struct hdsp *hdsp)
Linus Torvalds's avatar
Linus Torvalds committed
4897
{
4898
	struct snd_hwdep *hw;
Linus Torvalds's avatar
Linus Torvalds committed
4899
	int err;
4900

Linus Torvalds's avatar
Linus Torvalds committed
4901 4902
	if ((err = snd_hwdep_new(card, "HDSP hwdep", 0, &hw)) < 0)
		return err;
4903

Linus Torvalds's avatar
Linus Torvalds committed
4904 4905 4906 4907 4908
	hdsp->hwdep = hw;
	hw->private_data = hdsp;
	strcpy(hw->name, "HDSP hwdep interface");

	hw->ops.ioctl = snd_hdsp_hwdep_ioctl;
4909
	hw->ops.ioctl_compat = snd_hdsp_hwdep_ioctl;
4910

Linus Torvalds's avatar
Linus Torvalds committed
4911 4912 4913
	return 0;
}

4914
static int snd_hdsp_create_pcm(struct snd_card *card, struct hdsp *hdsp)
Linus Torvalds's avatar
Linus Torvalds committed
4915
{
4916
	struct snd_pcm *pcm;
Linus Torvalds's avatar
Linus Torvalds committed
4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933
	int err;

	if ((err = snd_pcm_new(card, hdsp->card_name, 0, 1, 1, &pcm)) < 0)
		return err;

	hdsp->pcm = pcm;
	pcm->private_data = hdsp;
	strcpy(pcm->name, hdsp->card_name);

	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_hdsp_playback_ops);
	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_hdsp_capture_ops);

	pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;

	return 0;
}

4934
static void snd_hdsp_9652_enable_mixer (struct hdsp *hdsp)
Linus Torvalds's avatar
Linus Torvalds committed
4935 4936 4937 4938 4939
{
        hdsp->control2_register |= HDSP_9652_ENABLE_MIXER;
	hdsp_write (hdsp, HDSP_control2Reg, hdsp->control2_register);
}

4940
static int snd_hdsp_enable_io (struct hdsp *hdsp)
Linus Torvalds's avatar
Linus Torvalds committed
4941 4942
{
	int i;
4943

Linus Torvalds's avatar
Linus Torvalds committed
4944
	if (hdsp_fifo_wait (hdsp, 0, 100)) {
4945 4946
		dev_err(hdsp->card->dev,
			"enable_io fifo_wait failed\n");
Linus Torvalds's avatar
Linus Torvalds committed
4947 4948
		return -EIO;
	}
4949

Linus Torvalds's avatar
Linus Torvalds committed
4950 4951 4952 4953
	for (i = 0; i < hdsp->max_channels; ++i) {
		hdsp_write (hdsp, HDSP_inputEnable + (4 * i), 1);
		hdsp_write (hdsp, HDSP_outputEnable + (4 * i), 1);
	}
4954

Linus Torvalds's avatar
Linus Torvalds committed
4955 4956 4957
	return 0;
}

4958
static void snd_hdsp_initialize_channels(struct hdsp *hdsp)
Linus Torvalds's avatar
Linus Torvalds committed
4959 4960
{
	int status, aebi_channels, aebo_channels;
4961

Linus Torvalds's avatar
Linus Torvalds committed
4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973
	switch (hdsp->io_type) {
	case Digiface:
		hdsp->card_name = "RME Hammerfall DSP + Digiface";
		hdsp->ss_in_channels = hdsp->ss_out_channels = DIGIFACE_SS_CHANNELS;
		hdsp->ds_in_channels = hdsp->ds_out_channels = DIGIFACE_DS_CHANNELS;
		break;

	case H9652:
		hdsp->card_name = "RME Hammerfall HDSP 9652";
		hdsp->ss_in_channels = hdsp->ss_out_channels = H9652_SS_CHANNELS;
		hdsp->ds_in_channels = hdsp->ds_out_channels = H9652_DS_CHANNELS;
		break;
4974

Linus Torvalds's avatar
Linus Torvalds committed
4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993
	case H9632:
		status = hdsp_read(hdsp, HDSP_statusRegister);
		/* HDSP_AEBx bits are low when AEB are connected */
		aebi_channels = (status & HDSP_AEBI) ? 0 : 4;
		aebo_channels = (status & HDSP_AEBO) ? 0 : 4;
		hdsp->card_name = "RME Hammerfall HDSP 9632";
		hdsp->ss_in_channels = H9632_SS_CHANNELS+aebi_channels;
		hdsp->ds_in_channels = H9632_DS_CHANNELS+aebi_channels;
		hdsp->qs_in_channels = H9632_QS_CHANNELS+aebi_channels;
		hdsp->ss_out_channels = H9632_SS_CHANNELS+aebo_channels;
		hdsp->ds_out_channels = H9632_DS_CHANNELS+aebo_channels;
		hdsp->qs_out_channels = H9632_QS_CHANNELS+aebo_channels;
		break;

	case Multiface:
		hdsp->card_name = "RME Hammerfall DSP + Multiface";
		hdsp->ss_in_channels = hdsp->ss_out_channels = MULTIFACE_SS_CHANNELS;
		hdsp->ds_in_channels = hdsp->ds_out_channels = MULTIFACE_DS_CHANNELS;
		break;
4994

4995 4996 4997 4998 4999 5000 5001 5002
	case RPM:
		hdsp->card_name = "RME Hammerfall DSP + RPM";
		hdsp->ss_in_channels = RPM_CHANNELS-1;
		hdsp->ss_out_channels = RPM_CHANNELS;
		hdsp->ds_in_channels = RPM_CHANNELS-1;
		hdsp->ds_out_channels = RPM_CHANNELS;
		break;

Linus Torvalds's avatar
Linus Torvalds committed
5003 5004 5005 5006 5007 5008
	default:
 		/* should never get here */
		break;
	}
}

5009
static void snd_hdsp_initialize_midi_flush (struct hdsp *hdsp)
Linus Torvalds's avatar
Linus Torvalds committed
5010 5011 5012 5013 5014
{
	snd_hdsp_flush_midi_input (hdsp, 0);
	snd_hdsp_flush_midi_input (hdsp, 1);
}

5015
static int snd_hdsp_create_alsa_devices(struct snd_card *card, struct hdsp *hdsp)
Linus Torvalds's avatar
Linus Torvalds committed
5016 5017
{
	int err;
5018

Linus Torvalds's avatar
Linus Torvalds committed
5019
	if ((err = snd_hdsp_create_pcm(card, hdsp)) < 0) {
5020 5021
		dev_err(card->dev,
			"Error creating pcm interface\n");
Linus Torvalds's avatar
Linus Torvalds committed
5022 5023
		return err;
	}
5024

Linus Torvalds's avatar
Linus Torvalds committed
5025 5026

	if ((err = snd_hdsp_create_midi(card, hdsp, 0)) < 0) {
5027 5028
		dev_err(card->dev,
			"Error creating first midi interface\n");
Linus Torvalds's avatar
Linus Torvalds committed
5029 5030 5031 5032 5033
		return err;
	}

	if (hdsp->io_type == Digiface || hdsp->io_type == H9652) {
		if ((err = snd_hdsp_create_midi(card, hdsp, 1)) < 0) {
5034 5035
			dev_err(card->dev,
				"Error creating second midi interface\n");
Linus Torvalds's avatar
Linus Torvalds committed
5036 5037 5038 5039 5040
			return err;
		}
	}

	if ((err = snd_hdsp_create_controls(card, hdsp)) < 0) {
5041 5042
		dev_err(card->dev,
			"Error creating ctl interface\n");
Linus Torvalds's avatar
Linus Torvalds committed
5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054
		return err;
	}

	snd_hdsp_proc_init(hdsp);

	hdsp->system_sample_rate = -1;
	hdsp->playback_pid = -1;
	hdsp->capture_pid = -1;
	hdsp->capture_substream = NULL;
	hdsp->playback_substream = NULL;

	if ((err = snd_hdsp_set_defaults(hdsp)) < 0) {
5055 5056
		dev_err(card->dev,
			"Error setting default values\n");
Linus Torvalds's avatar
Linus Torvalds committed
5057 5058
		return err;
	}
5059

Linus Torvalds's avatar
Linus Torvalds committed
5060
	if (!(hdsp->state & HDSP_InitializationComplete)) {
5061
		strcpy(card->shortname, "Hammerfall DSP");
5062
		sprintf(card->longname, "%s at 0x%lx, irq %d", hdsp->card_name,
Linus Torvalds's avatar
Linus Torvalds committed
5063
			hdsp->port, hdsp->irq);
5064

Linus Torvalds's avatar
Linus Torvalds committed
5065
		if ((err = snd_card_register(card)) < 0) {
5066 5067
			dev_err(card->dev,
				"error registering card\n");
Linus Torvalds's avatar
Linus Torvalds committed
5068 5069 5070 5071
			return err;
		}
		hdsp->state |= HDSP_InitializationComplete;
	}
5072

Linus Torvalds's avatar
Linus Torvalds committed
5073 5074 5075 5076
	return 0;
}

/* load firmware via hotplug fw loader */
5077
static int hdsp_request_fw_loader(struct hdsp *hdsp)
Linus Torvalds's avatar
Linus Torvalds committed
5078 5079 5080 5081
{
	const char *fwfile;
	const struct firmware *fw;
	int err;
5082

Linus Torvalds's avatar
Linus Torvalds committed
5083 5084 5085 5086 5087 5088 5089 5090
	if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
		return 0;
	if (hdsp->io_type == Undefined) {
		if ((err = hdsp_get_iobox_version(hdsp)) < 0)
			return err;
		if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
			return 0;
	}
5091

Linus Torvalds's avatar
Linus Torvalds committed
5092 5093
	/* caution: max length of firmware filename is 30! */
	switch (hdsp->io_type) {
5094 5095 5096
	case RPM:
		fwfile = "rpm_firmware.bin";
		break;
Linus Torvalds's avatar
Linus Torvalds committed
5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109
	case Multiface:
		if (hdsp->firmware_rev == 0xa)
			fwfile = "multiface_firmware.bin";
		else
			fwfile = "multiface_firmware_rev11.bin";
		break;
	case Digiface:
		if (hdsp->firmware_rev == 0xa)
			fwfile = "digiface_firmware.bin";
		else
			fwfile = "digiface_firmware_rev11.bin";
		break;
	default:
5110 5111
		dev_err(hdsp->card->dev,
			"invalid io_type %d\n", hdsp->io_type);
Linus Torvalds's avatar
Linus Torvalds committed
5112 5113 5114 5115
		return -EINVAL;
	}

	if (request_firmware(&fw, fwfile, &hdsp->pci->dev)) {
5116 5117
		dev_err(hdsp->card->dev,
			"cannot load firmware %s\n", fwfile);
Linus Torvalds's avatar
Linus Torvalds committed
5118 5119
		return -ENOENT;
	}
5120
	if (fw->size < HDSP_FIRMWARE_SIZE) {
5121 5122
		dev_err(hdsp->card->dev,
			"too short firmware size %d (expected %d)\n",
5123
			   (int)fw->size, HDSP_FIRMWARE_SIZE);
5124
		release_firmware(fw);
Linus Torvalds's avatar
Linus Torvalds committed
5125 5126
		return -EINVAL;
	}
5127

5128
	hdsp->firmware = fw;
5129

Linus Torvalds's avatar
Linus Torvalds committed
5130 5131 5132 5133
	hdsp->state |= HDSP_FirmwareCached;

	if ((err = snd_hdsp_load_firmware_from_cache(hdsp)) < 0)
		return err;
5134

Linus Torvalds's avatar
Linus Torvalds committed
5135
	if (!(hdsp->state & HDSP_InitializationComplete)) {
Takashi Iwai's avatar
Takashi Iwai committed
5136
		if ((err = snd_hdsp_enable_io(hdsp)) < 0)
Linus Torvalds's avatar
Linus Torvalds committed
5137 5138 5139
			return err;

		if ((err = snd_hdsp_create_hwdep(hdsp->card, hdsp)) < 0) {
5140 5141
			dev_err(hdsp->card->dev,
				"error creating hwdep device\n");
Linus Torvalds's avatar
Linus Torvalds committed
5142 5143 5144 5145 5146
			return err;
		}
		snd_hdsp_initialize_channels(hdsp);
		snd_hdsp_initialize_midi_flush(hdsp);
		if ((err = snd_hdsp_create_alsa_devices(hdsp->card, hdsp)) < 0) {
5147 5148
			dev_err(hdsp->card->dev,
				"error creating alsa devices\n");
Linus Torvalds's avatar
Linus Torvalds committed
5149 5150 5151 5152 5153 5154
			return err;
		}
	}
	return 0;
}

5155 5156
static int snd_hdsp_create(struct snd_card *card,
			   struct hdsp *hdsp)
Linus Torvalds's avatar
Linus Torvalds committed
5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181
{
	struct pci_dev *pci = hdsp->pci;
	int err;
	int is_9652 = 0;
	int is_9632 = 0;

	hdsp->irq = -1;
	hdsp->state = 0;
	hdsp->midi[0].rmidi = NULL;
	hdsp->midi[1].rmidi = NULL;
	hdsp->midi[0].input = NULL;
	hdsp->midi[1].input = NULL;
	hdsp->midi[0].output = NULL;
	hdsp->midi[1].output = NULL;
	hdsp->midi[0].pending = 0;
	hdsp->midi[1].pending = 0;
	spin_lock_init(&hdsp->midi[0].lock);
	spin_lock_init(&hdsp->midi[1].lock);
	hdsp->iobase = NULL;
	hdsp->control_register = 0;
	hdsp->control2_register = 0;
	hdsp->io_type = Undefined;
	hdsp->max_channels = 26;

	hdsp->card = card;
5182

Linus Torvalds's avatar
Linus Torvalds committed
5183 5184 5185
	spin_lock_init(&hdsp->lock);

	tasklet_init(&hdsp->midi_tasklet, hdsp_midi_tasklet, (unsigned long)hdsp);
5186

Linus Torvalds's avatar
Linus Torvalds committed
5187 5188
	pci_read_config_word(hdsp->pci, PCI_CLASS_REVISION, &hdsp->firmware_rev);
	hdsp->firmware_rev &= 0xff;
5189

Linus Torvalds's avatar
Linus Torvalds committed
5190 5191 5192 5193 5194 5195 5196 5197
	/* From Martin Bjoernsen :
	    "It is important that the card's latency timer register in
	    the PCI configuration space is set to a value much larger
	    than 0 by the computer's BIOS or the driver.
	    The windows driver always sets this 8 bit register [...]
	    to its maximum 255 to avoid problems with some computers."
	*/
	pci_write_config_byte(hdsp->pci, PCI_LATENCY_TIMER, 0xFF);
5198

Linus Torvalds's avatar
Linus Torvalds committed
5199 5200 5201
	strcpy(card->driver, "H-DSP");
	strcpy(card->mixername, "Xilinx FPGA");

Takashi Iwai's avatar
Takashi Iwai committed
5202
	if (hdsp->firmware_rev < 0xa)
Linus Torvalds's avatar
Linus Torvalds committed
5203
		return -ENODEV;
Takashi Iwai's avatar
Takashi Iwai committed
5204
	else if (hdsp->firmware_rev < 0x64)
Linus Torvalds's avatar
Linus Torvalds committed
5205
		hdsp->card_name = "RME Hammerfall DSP";
Takashi Iwai's avatar
Takashi Iwai committed
5206
	else if (hdsp->firmware_rev < 0x96) {
Linus Torvalds's avatar
Linus Torvalds committed
5207 5208 5209 5210 5211
		hdsp->card_name = "RME HDSP 9652";
		is_9652 = 1;
	} else {
		hdsp->card_name = "RME HDSP 9632";
		hdsp->max_channels = 16;
5212
		is_9632 = 1;
Linus Torvalds's avatar
Linus Torvalds committed
5213 5214
	}

Takashi Iwai's avatar
Takashi Iwai committed
5215
	if ((err = pci_enable_device(pci)) < 0)
Linus Torvalds's avatar
Linus Torvalds committed
5216 5217 5218 5219 5220 5221 5222 5223
		return err;

	pci_set_master(hdsp->pci);

	if ((err = pci_request_regions(pci, "hdsp")) < 0)
		return err;
	hdsp->port = pci_resource_start(pci, 0);
	if ((hdsp->iobase = ioremap_nocache(hdsp->port, HDSP_IO_EXTENT)) == NULL) {
5224 5225
		dev_err(hdsp->card->dev, "unable to remap region 0x%lx-0x%lx\n",
			hdsp->port, hdsp->port + HDSP_IO_EXTENT - 1);
Linus Torvalds's avatar
Linus Torvalds committed
5226 5227 5228
		return -EBUSY;
	}

5229
	if (request_irq(pci->irq, snd_hdsp_interrupt, IRQF_SHARED,
5230
			KBUILD_MODNAME, hdsp)) {
5231
		dev_err(hdsp->card->dev, "unable to use IRQ %d\n", pci->irq);
Linus Torvalds's avatar
Linus Torvalds committed
5232 5233 5234 5235
		return -EBUSY;
	}

	hdsp->irq = pci->irq;
5236
	card->sync_irq = hdsp->irq;
5237
	hdsp->precise_ptr = 0;
Linus Torvalds's avatar
Linus Torvalds committed
5238
	hdsp->use_midi_tasklet = 1;
5239
	hdsp->dds_value = 0;
Linus Torvalds's avatar
Linus Torvalds committed
5240

Takashi Iwai's avatar
Takashi Iwai committed
5241
	if ((err = snd_hdsp_initialize_memory(hdsp)) < 0)
Linus Torvalds's avatar
Linus Torvalds committed
5242
		return err;
5243

Linus Torvalds's avatar
Linus Torvalds committed
5244
	if (!is_9652 && !is_9632) {
Tim Blechmann's avatar
Tim Blechmann committed
5245 5246 5247
		/* we wait a maximum of 10 seconds to let freshly
		 * inserted cardbus cards do their hardware init */
		err = hdsp_wait_for_iobox(hdsp, 1000, 10);
Linus Torvalds's avatar
Linus Torvalds committed
5248

5249 5250 5251
		if (err < 0)
			return err;

Linus Torvalds's avatar
Linus Torvalds committed
5252
		if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
Takashi Iwai's avatar
Takashi Iwai committed
5253
			if ((err = hdsp_request_fw_loader(hdsp)) < 0)
Linus Torvalds's avatar
Linus Torvalds committed
5254 5255 5256 5257
				/* we don't fail as this can happen
				   if userspace is not ready for
				   firmware upload
				*/
5258 5259
				dev_err(hdsp->card->dev,
					"couldn't get firmware from userspace. try using hdsploader\n");
Takashi Iwai's avatar
Takashi Iwai committed
5260
			else
Linus Torvalds's avatar
Linus Torvalds committed
5261 5262
				/* init is complete, we return */
				return 0;
5263
			/* we defer initialization */
5264 5265
			dev_info(hdsp->card->dev,
				 "card initialization pending : waiting for firmware\n");
Takashi Iwai's avatar
Takashi Iwai committed
5266
			if ((err = snd_hdsp_create_hwdep(card, hdsp)) < 0)
Linus Torvalds's avatar
Linus Torvalds committed
5267 5268 5269
				return err;
			return 0;
		} else {
5270 5271
			dev_info(hdsp->card->dev,
				 "Firmware already present, initializing card.\n");
5272 5273 5274
			if (hdsp_read(hdsp, HDSP_status2Register) & HDSP_version2)
				hdsp->io_type = RPM;
			else if (hdsp_read(hdsp, HDSP_status2Register) & HDSP_version1)
Linus Torvalds's avatar
Linus Torvalds committed
5275
				hdsp->io_type = Multiface;
5276
			else
Linus Torvalds's avatar
Linus Torvalds committed
5277 5278 5279
				hdsp->io_type = Digiface;
		}
	}
5280

Takashi Iwai's avatar
Takashi Iwai committed
5281
	if ((err = snd_hdsp_enable_io(hdsp)) != 0)
Linus Torvalds's avatar
Linus Torvalds committed
5282
		return err;
5283

Takashi Iwai's avatar
Takashi Iwai committed
5284
	if (is_9652)
Linus Torvalds's avatar
Linus Torvalds committed
5285
	        hdsp->io_type = H9652;
5286

Takashi Iwai's avatar
Takashi Iwai committed
5287
	if (is_9632)
Linus Torvalds's avatar
Linus Torvalds committed
5288 5289
		hdsp->io_type = H9632;

Takashi Iwai's avatar
Takashi Iwai committed
5290
	if ((err = snd_hdsp_create_hwdep(card, hdsp)) < 0)
Linus Torvalds's avatar
Linus Torvalds committed
5291
		return err;
5292

Linus Torvalds's avatar
Linus Torvalds committed
5293 5294 5295
	snd_hdsp_initialize_channels(hdsp);
	snd_hdsp_initialize_midi_flush(hdsp);

5296
	hdsp->state |= HDSP_FirmwareLoaded;
Linus Torvalds's avatar
Linus Torvalds committed
5297

Takashi Iwai's avatar
Takashi Iwai committed
5298
	if ((err = snd_hdsp_create_alsa_devices(card, hdsp)) < 0)
Linus Torvalds's avatar
Linus Torvalds committed
5299 5300
		return err;

5301
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
5302 5303
}

5304
static int snd_hdsp_free(struct hdsp *hdsp)
Linus Torvalds's avatar
Linus Torvalds committed
5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316
{
	if (hdsp->port) {
		/* stop the audio, and cancel all interrupts */
		tasklet_kill(&hdsp->midi_tasklet);
		hdsp->control_register &= ~(HDSP_Start|HDSP_AudioInterruptEnable|HDSP_Midi0InterruptEnable|HDSP_Midi1InterruptEnable);
		hdsp_write (hdsp, HDSP_controlRegister, hdsp->control_register);
	}

	if (hdsp->irq >= 0)
		free_irq(hdsp->irq, (void *)hdsp);

	snd_hdsp_free_buffers(hdsp);
5317

5318
	release_firmware(hdsp->firmware);
5319
	vfree(hdsp->fw_uploaded);
5320
	iounmap(hdsp->iobase);
Linus Torvalds's avatar
Linus Torvalds committed
5321 5322 5323

	if (hdsp->port)
		pci_release_regions(hdsp->pci);
5324

Linus Torvalds's avatar
Linus Torvalds committed
5325 5326 5327 5328
	pci_disable_device(hdsp->pci);
	return 0;
}

5329
static void snd_hdsp_card_free(struct snd_card *card)
Linus Torvalds's avatar
Linus Torvalds committed
5330
{
5331
	struct hdsp *hdsp = card->private_data;
Linus Torvalds's avatar
Linus Torvalds committed
5332 5333 5334 5335 5336

	if (hdsp)
		snd_hdsp_free(hdsp);
}

5337 5338
static int snd_hdsp_probe(struct pci_dev *pci,
			  const struct pci_device_id *pci_id)
Linus Torvalds's avatar
Linus Torvalds committed
5339 5340
{
	static int dev;
5341 5342
	struct hdsp *hdsp;
	struct snd_card *card;
Linus Torvalds's avatar
Linus Torvalds committed
5343 5344 5345 5346 5347 5348 5349 5350 5351
	int err;

	if (dev >= SNDRV_CARDS)
		return -ENODEV;
	if (!enable[dev]) {
		dev++;
		return -ENOENT;
	}

5352 5353
	err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
			   sizeof(struct hdsp), &card);
5354 5355
	if (err < 0)
		return err;
Linus Torvalds's avatar
Linus Torvalds committed
5356

5357
	hdsp = card->private_data;
Linus Torvalds's avatar
Linus Torvalds committed
5358 5359 5360
	card->private_free = snd_hdsp_card_free;
	hdsp->dev = dev;
	hdsp->pci = pci;
5361 5362 5363
	err = snd_hdsp_create(card, hdsp);
	if (err)
		goto free_card;
Linus Torvalds's avatar
Linus Torvalds committed
5364 5365

	strcpy(card->shortname, "Hammerfall DSP");
5366
	sprintf(card->longname, "%s at 0x%lx, irq %d", hdsp->card_name,
Linus Torvalds's avatar
Linus Torvalds committed
5367
		hdsp->port, hdsp->irq);
5368 5369 5370
	err = snd_card_register(card);
	if (err) {
free_card:
Linus Torvalds's avatar
Linus Torvalds committed
5371 5372 5373 5374 5375 5376 5377 5378
		snd_card_free(card);
		return err;
	}
	pci_set_drvdata(pci, card);
	dev++;
	return 0;
}

5379
static void snd_hdsp_remove(struct pci_dev *pci)
Linus Torvalds's avatar
Linus Torvalds committed
5380 5381 5382 5383
{
	snd_card_free(pci_get_drvdata(pci));
}

5384
static struct pci_driver hdsp_driver = {
5385
	.name =     KBUILD_MODNAME,
Linus Torvalds's avatar
Linus Torvalds committed
5386 5387
	.id_table = snd_hdsp_ids,
	.probe =    snd_hdsp_probe,
5388
	.remove = snd_hdsp_remove,
Linus Torvalds's avatar
Linus Torvalds committed
5389 5390
};

5391
module_pci_driver(hdsp_driver);