hdsp.c 146.8 KB
Newer Older
L
Linus Torvalds 已提交
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 26 27 28 29
/*
 *   ALSA driver for RME Hammerfall DSP audio interface(s)
 *
 *      Copyright (c) 2002  Paul Davis
 *                          Marcus Andersson
 *                          Thomas Charbonnel
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 */

#include <linux/init.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
#include <linux/pci.h>
#include <linux/firmware.h>
#include <linux/moduleparam.h>
30
#include <linux/math64.h>
L
Linus Torvalds 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

#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>
#include <asm/io.h>

static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-MAX */
static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;	/* Enable this card */

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}}");
62 63 64 65 66 67
#ifdef HDSP_FW_LOADER
MODULE_FIRMWARE("multiface_firmware.bin");
MODULE_FIRMWARE("multiface_firmware_rev11.bin");
MODULE_FIRMWARE("digiface_firmware.bin");
MODULE_FIRMWARE("digiface_firmware_rev11.bin");
#endif
L
Linus Torvalds 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

#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

/* Write registers. These are defined as byte-offsets from the iobase value.
 */
#define HDSP_resetPointer               0
88
#define HDSP_freqReg			0
L
Linus Torvalds 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
#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
T
Tim Blechmann 已提交
116
   when read; the least-significant 4 bits are full-scale counters;
L
Linus Torvalds 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
   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
*/
T
Tim Blechmann 已提交
134
#define HDSP_9652_peakBase	7164
L
Linus Torvalds 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
#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
#define HDSP_VERSION_BIT	0x100
#define HDSP_BIGENDIAN_MODE     0x200
#define HDSP_RD_MULTIPLE        0x400
#define HDSP_9652_ENABLE_MIXER  0x800
#define HDSP_TDO                0x10000000

#define HDSP_S_PROGRAM     	(HDSP_PROGRAM|HDSP_CONFIG_MODE_0)
#define HDSP_S_LOAD		(HDSP_PROGRAM|HDSP_CONFIG_MODE_1)

/* 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 */
T
Tim Blechmann 已提交
176 177 178 179
#define HDSP_SyncRef2             (1<<13)
#define HDSP_SPDIFInputSelect0    (1<<14)
#define HDSP_SPDIFInputSelect1    (1<<15)
#define HDSP_SyncRef0             (1<<16)
L
Linus Torvalds 已提交
180
#define HDSP_SyncRef1             (1<<17)
T
Tim Blechmann 已提交
181
#define HDSP_AnalogExtensionBoard (1<<18) /* For H9632 cards */
L
Linus Torvalds 已提交
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 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
#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)

#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)
281 282 283 284 285
/* 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 */
L
Linus Torvalds 已提交
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316

#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 */
T
Tim Blechmann 已提交
317
#define HDSP_midi0IRQPending    (1<<30)
L
Linus Torvalds 已提交
318 319 320
#define HDSP_midi1IRQPending    (1<<31)

#define HDSP_spdifFrequencyMask    (HDSP_spdifFrequency0|HDSP_spdifFrequency1|HDSP_spdifFrequency2)
321 322 323 324
#define HDSP_spdifFrequencyMask_9632 (HDSP_spdifFrequency0|\
				      HDSP_spdifFrequency1|\
				      HDSP_spdifFrequency2|\
				      HDSP_spdifFrequency3)
L
Linus Torvalds 已提交
325 326 327 328 329 330 331 332 333 334

#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 */
335 336 337
#define HDSP_spdifFrequency128KHz   (HDSP_spdifFrequency0|\
				     HDSP_spdifFrequency1|\
				     HDSP_spdifFrequency2)
L
Linus Torvalds 已提交
338 339 340 341 342 343 344 345 346 347 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
#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
T
Tim Blechmann 已提交
394
   size is the same regardless of the number of channels - the
L
Linus Torvalds 已提交
395 396 397 398 399 400 401 402 403 404
   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)

405
/* use hotplug firmware loader? */
L
Linus Torvalds 已提交
406
#if defined(CONFIG_FW_LOADER) || defined(CONFIG_FW_LOADER_MODULE)
407
#if !defined(HDSP_USE_HWDEP_LOADER)
L
Linus Torvalds 已提交
408 409 410 411
#define HDSP_FW_LOADER
#endif
#endif

412
struct hdsp_9632_meters {
L
Linus Torvalds 已提交
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
    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];
};

428 429
struct hdsp_midi {
    struct hdsp             *hdsp;
L
Linus Torvalds 已提交
430
    int                      id;
431 432 433
    struct snd_rawmidi           *rmidi;
    struct snd_rawmidi_substream *input;
    struct snd_rawmidi_substream *output;
L
Linus Torvalds 已提交
434 435 436 437 438 439
    char                     istimer; /* timer in use */
    struct timer_list	     timer;
    spinlock_t               lock;
    int			     pending;
};

440
struct hdsp {
L
Linus Torvalds 已提交
441
	spinlock_t            lock;
442 443 444
	struct snd_pcm_substream *capture_substream;
	struct snd_pcm_substream *playback_substream;
        struct hdsp_midi      midi[2];
L
Linus Torvalds 已提交
445 446 447 448 449 450 451
	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;
452
	int                   clock_source_locked;
L
Linus Torvalds 已提交
453
	char                 *card_name;	     /* digiface/multiface */
454
	enum HDSP_IO_Type     io_type;               /* ditto, but for code use */
L
Linus Torvalds 已提交
455 456 457 458 459 460 461 462
        unsigned short        firmware_rev;
	unsigned short	      state;		     /* stores state bits */
	u32		      firmware_cache[24413]; /* this helps recover from accidental iobox power failure */
	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 */
T
Tim Blechmann 已提交
463
	unsigned char	      qs_out_channels;
L
Linus Torvalds 已提交
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
	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;
481 482 483
	struct snd_card *card;
	struct snd_pcm *pcm;
	struct snd_hwdep          *hwdep;
L
Linus Torvalds 已提交
484
	struct pci_dev       *pci;
485
	struct snd_kcontrol *spdif_ctl;
L
Linus Torvalds 已提交
486
        unsigned short        mixer_matrix[HDSP_MATRIX_MIXER_SIZE];
487
	unsigned int          dds_value; /* last value written to freq register */
L
Linus Torvalds 已提交
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
};

/* 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 */
T
Tim Blechmann 已提交
505
	0, 1, 2, 3, 4, 5, 6, 7,
L
Linus Torvalds 已提交
506
	/* ADAT 2 */
T
Tim Blechmann 已提交
507
	16, 17, 18, 19, 20, 21, 22, 23,
L
Linus Torvalds 已提交
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
	/* 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 */
T
Tim Blechmann 已提交
528
	10, 11,
L
Linus Torvalds 已提交
529 530 531
	/* AO4S-192 and AI4S-192 extension boards */
	12, 13, 14, 15,
	/* others don't exist */
T
Tim Blechmann 已提交
532
	-1, -1, -1, -1, -1, -1, -1, -1,
L
Linus Torvalds 已提交
533 534 535 536 537 538 539 540 541
	-1, -1
};

static char channel_map_H9632_ds[HDSP_MAX_CHANNELS] = {
	/* ADAT */
	1, 3, 5, 7,
	/* SPDIF */
	8, 9,
	/* Analog */
T
Tim Blechmann 已提交
542
	10, 11,
L
Linus Torvalds 已提交
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
	/* 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)
{
	dmab->dev.type = SNDRV_DMA_TYPE_DEV;
	dmab->dev.dev = snd_dma_pci_data(pci);
568 569 570
	if (snd_dma_get_reserved_buf(dmab, snd_dma_pci_buf_id(pci))) {
		if (dmab->bytes >= size)
			return 0;
L
Linus Torvalds 已提交
571
	}
572 573 574
	if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
				size, dmab) < 0)
		return -ENOMEM;
L
Linus Torvalds 已提交
575 576 577 578 579
	return 0;
}

static void snd_hammerfall_free_buffer(struct snd_dma_buffer *dmab, struct pci_dev *pci)
{
580 581
	if (dmab->area) {
		dmab->dev.dev = NULL; /* make it anonymous */
L
Linus Torvalds 已提交
582
		snd_dma_reserve_buf(dmab, snd_dma_pci_buf_id(pci));
583
	}
L
Linus Torvalds 已提交
584 585 586
}


587
static DEFINE_PCI_DEVICE_TABLE(snd_hdsp_ids) = {
L
Linus Torvalds 已提交
588 589
	{
		.vendor = PCI_VENDOR_ID_XILINX,
T
Tim Blechmann 已提交
590
		.device = PCI_DEVICE_ID_XILINX_HAMMERFALL_DSP,
L
Linus Torvalds 已提交
591 592 593 594 595 596 597 598 599
		.subvendor = PCI_ANY_ID,
		.subdevice = PCI_ANY_ID,
	}, /* RME Hammerfall-DSP */
	{ 0, },
};

MODULE_DEVICE_TABLE(pci, snd_hdsp_ids);

/* prototypes */
600 601 602 603 604 605 606 607 608 609 610
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)
L
Linus Torvalds 已提交
611
{
612 613 614 615
	switch (hdsp->io_type) {
	case Multiface:
	case Digiface:
	default:
616 617 618 619
		if (hdsp->firmware_rev == 0xa)
			return (64 * out) + (32 + (in));
		else
			return (52 * out) + (26 + (in));
620
	case H9632:
L
Linus Torvalds 已提交
621
		return (32 * out) + (16 + (in));
622
	case H9652:
L
Linus Torvalds 已提交
623 624 625 626
		return (52 * out) + (26 + (in));
	}
}

627
static int hdsp_input_to_output_key (struct hdsp *hdsp, int in, int out)
L
Linus Torvalds 已提交
628
{
629 630 631 632
	switch (hdsp->io_type) {
	case Multiface:
	case Digiface:
	default:
633 634 635 636
		if (hdsp->firmware_rev == 0xa)
			return (64 * out) + in;
		else
			return (52 * out) + in;
637
	case H9632:
L
Linus Torvalds 已提交
638
		return (32 * out) + in;
639
	case H9652:
L
Linus Torvalds 已提交
640 641 642 643
		return (52 * out) + in;
	}
}

644
static void hdsp_write(struct hdsp *hdsp, int reg, int val)
L
Linus Torvalds 已提交
645 646 647 648
{
	writel(val, hdsp->iobase + reg);
}

649
static unsigned int hdsp_read(struct hdsp *hdsp, int reg)
L
Linus Torvalds 已提交
650 651 652 653
{
	return readl (hdsp->iobase + reg);
}

654
static int hdsp_check_for_iobox (struct hdsp *hdsp)
L
Linus Torvalds 已提交
655 656 657 658 659 660 661 662
{
	if (hdsp->io_type == H9652 || hdsp->io_type == H9632) return 0;
	if (hdsp_read (hdsp, HDSP_statusRegister) & HDSP_ConfigError) {
		snd_printk ("Hammerfall-DSP: no Digiface or Multiface connected!\n");
		hdsp->state &= ~HDSP_FirmwareLoaded;
		return -EIO;
	}
	return 0;
T
Tim Blechmann 已提交
663
}
L
Linus Torvalds 已提交
664

T
Tim Blechmann 已提交
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
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 {
			snd_printd("Hammerfall-DSP: iobox found after %ums!\n",
				   i * delay);
			return 0;
		}
	}

	snd_printk("Hammerfall-DSP: no Digiface or Multiface connected!\n");
	hdsp->state &= ~HDSP_FirmwareLoaded;
	return -EIO;
L
Linus Torvalds 已提交
686 687
}

688
static int snd_hdsp_load_firmware_from_cache(struct hdsp *hdsp) {
L
Linus Torvalds 已提交
689 690 691 692 693

	int i;
	unsigned long flags;

	if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
T
Tim Blechmann 已提交
694

L
Linus Torvalds 已提交
695 696 697 698
		snd_printk ("Hammerfall-DSP: loading firmware\n");

		hdsp_write (hdsp, HDSP_control2Reg, HDSP_S_PROGRAM);
		hdsp_write (hdsp, HDSP_fifoData, 0);
T
Tim Blechmann 已提交
699

L
Linus Torvalds 已提交
700 701 702 703
		if (hdsp_fifo_wait (hdsp, 0, HDSP_LONG_WAIT)) {
			snd_printk ("Hammerfall-DSP: timeout waiting for download preparation\n");
			return -EIO;
		}
T
Tim Blechmann 已提交
704

L
Linus Torvalds 已提交
705
		hdsp_write (hdsp, HDSP_control2Reg, HDSP_S_LOAD);
T
Tim Blechmann 已提交
706

L
Linus Torvalds 已提交
707 708 709 710 711 712 713 714
		for (i = 0; i < 24413; ++i) {
			hdsp_write(hdsp, HDSP_fifoData, hdsp->firmware_cache[i]);
			if (hdsp_fifo_wait (hdsp, 127, HDSP_LONG_WAIT)) {
				snd_printk ("Hammerfall-DSP: timeout during firmware loading\n");
				return -EIO;
			}
		}

T
Takashi Iwai 已提交
715
		ssleep(3);
T
Tim Blechmann 已提交
716

L
Linus Torvalds 已提交
717 718 719 720 721 722 723 724 725 726 727 728
		if (hdsp_fifo_wait (hdsp, 0, HDSP_LONG_WAIT)) {
			snd_printk ("Hammerfall-DSP: timeout at end of firmware loading\n");
		    	return -EIO;
		}

#ifdef SNDRV_BIG_ENDIAN
		hdsp->control2_register = HDSP_BIGENDIAN_MODE;
#else
		hdsp->control2_register = 0;
#endif
		hdsp_write (hdsp, HDSP_control2Reg, hdsp->control2_register);
		snd_printk ("Hammerfall-DSP: finished firmware loading\n");
T
Tim Blechmann 已提交
729

L
Linus Torvalds 已提交
730 731
	}
	if (hdsp->state & HDSP_InitializationComplete) {
T
Takashi Iwai 已提交
732
		snd_printk(KERN_INFO "Hammerfall-DSP: firmware loaded from cache, restoring defaults\n");
L
Linus Torvalds 已提交
733 734
		spin_lock_irqsave(&hdsp->lock, flags);
		snd_hdsp_set_defaults(hdsp);
T
Tim Blechmann 已提交
735
		spin_unlock_irqrestore(&hdsp->lock, flags);
L
Linus Torvalds 已提交
736
	}
T
Tim Blechmann 已提交
737

L
Linus Torvalds 已提交
738 739 740 741 742
	hdsp->state |= HDSP_FirmwareLoaded;

	return 0;
}

743
static int hdsp_get_iobox_version (struct hdsp *hdsp)
L
Linus Torvalds 已提交
744 745
{
	if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
T
Tim Blechmann 已提交
746

L
Linus Torvalds 已提交
747 748
		hdsp_write (hdsp, HDSP_control2Reg, HDSP_PROGRAM);
		hdsp_write (hdsp, HDSP_fifoData, 0);
T
Takashi Iwai 已提交
749
		if (hdsp_fifo_wait (hdsp, 0, HDSP_SHORT_WAIT) < 0)
L
Linus Torvalds 已提交
750 751 752 753 754 755 756 757 758 759 760 761
			return -EIO;

		hdsp_write (hdsp, HDSP_control2Reg, HDSP_S_LOAD);
		hdsp_write (hdsp, HDSP_fifoData, 0);

		if (hdsp_fifo_wait (hdsp, 0, HDSP_SHORT_WAIT)) {
			hdsp->io_type = Multiface;
			hdsp_write (hdsp, HDSP_control2Reg, HDSP_VERSION_BIT);
			hdsp_write (hdsp, HDSP_control2Reg, HDSP_S_LOAD);
			hdsp_fifo_wait (hdsp, 0, HDSP_SHORT_WAIT);
		} else {
			hdsp->io_type = Digiface;
T
Tim Blechmann 已提交
762
		}
L
Linus Torvalds 已提交
763 764
	} else {
		/* firmware was already loaded, get iobox type */
T
Takashi Iwai 已提交
765
		if (hdsp_read(hdsp, HDSP_status2Register) & HDSP_version1)
L
Linus Torvalds 已提交
766
			hdsp->io_type = Multiface;
T
Takashi Iwai 已提交
767
		else
L
Linus Torvalds 已提交
768 769 770 771 772 773
			hdsp->io_type = Digiface;
	}
	return 0;
}


774
#ifdef HDSP_FW_LOADER
775
static int hdsp_request_fw_loader(struct hdsp *hdsp);
776 777 778
#endif

static int hdsp_check_for_firmware (struct hdsp *hdsp, int load_on_demand)
L
Linus Torvalds 已提交
779
{
780 781
	if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
		return 0;
L
Linus Torvalds 已提交
782 783
	if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
		hdsp->state &= ~HDSP_FirmwareLoaded;
784
		if (! load_on_demand)
T
Takashi Iwai 已提交
785
			return -EIO;
786
		snd_printk(KERN_ERR "Hammerfall-DSP: firmware not present.\n");
T
Takashi Iwai 已提交
787
		/* try to load firmware */
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
		if (! (hdsp->state & HDSP_FirmwareCached)) {
#ifdef HDSP_FW_LOADER
			if (! hdsp_request_fw_loader(hdsp))
				return 0;
#endif
			snd_printk(KERN_ERR
				   "Hammerfall-DSP: No firmware loaded nor "
				   "cached, please upload firmware.\n");
			return -EIO;
		}
		if (snd_hdsp_load_firmware_from_cache(hdsp) != 0) {
			snd_printk(KERN_ERR
				   "Hammerfall-DSP: Firmware loading from "
				   "cache failed, please upload manually.\n");
			return -EIO;
T
Takashi Iwai 已提交
803
		}
L
Linus Torvalds 已提交
804 805 806 807 808
	}
	return 0;
}


809
static int hdsp_fifo_wait(struct hdsp *hdsp, int count, int timeout)
T
Tim Blechmann 已提交
810
{
L
Linus Torvalds 已提交
811 812 813 814 815
	int i;

	/* the fifoStatus registers reports on how many words
	   are available in the command FIFO.
	*/
T
Tim Blechmann 已提交
816

L
Linus Torvalds 已提交
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833
	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);
	}

	snd_printk ("Hammerfall-DSP: wait for FIFO status <= %d failed after %d iterations\n",
		    count, timeout);
	return -1;
}

834
static int hdsp_read_gain (struct hdsp *hdsp, unsigned int addr)
L
Linus Torvalds 已提交
835
{
T
Takashi Iwai 已提交
836
	if (addr >= HDSP_MATRIX_MIXER_SIZE)
L
Linus Torvalds 已提交
837
		return 0;
T
Takashi Iwai 已提交
838

L
Linus Torvalds 已提交
839 840 841
	return hdsp->mixer_matrix[addr];
}

842
static int hdsp_write_gain(struct hdsp *hdsp, unsigned int addr, unsigned short data)
L
Linus Torvalds 已提交
843 844 845 846 847
{
	unsigned int ad;

	if (addr >= HDSP_MATRIX_MIXER_SIZE)
		return -1;
T
Tim Blechmann 已提交
848

L
Linus Torvalds 已提交
849 850 851
	if (hdsp->io_type == H9652 || hdsp->io_type == H9632) {

		/* from martin bjornsen:
T
Tim Blechmann 已提交
852

L
Linus Torvalds 已提交
853 854 855 856 857 858 859 860 861 862
		   "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."
		*/

T
Takashi Iwai 已提交
863
		if (hdsp->io_type == H9632 && addr >= 512)
L
Linus Torvalds 已提交
864 865
			return 0;

T
Takashi Iwai 已提交
866
		if (hdsp->io_type == H9652 && addr >= 1352)
L
Linus Torvalds 已提交
867 868 869 870
			return 0;

		hdsp->mixer_matrix[addr] = data;

T
Tim Blechmann 已提交
871

L
Linus Torvalds 已提交
872 873 874 875 876 877 878 879
		/* `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;
T
Tim Blechmann 已提交
880 881 882

		hdsp_write (hdsp, 4096 + (ad*4),
			    (hdsp->mixer_matrix[(addr&0x7fe)+1] << 16) +
L
Linus Torvalds 已提交
883
			    hdsp->mixer_matrix[addr&0x7fe]);
T
Tim Blechmann 已提交
884

L
Linus Torvalds 已提交
885 886 887 888 889
		return 0;

	} else {

		ad = (addr << 16) + data;
T
Tim Blechmann 已提交
890

T
Takashi Iwai 已提交
891
		if (hdsp_fifo_wait(hdsp, 127, HDSP_LONG_WAIT))
L
Linus Torvalds 已提交
892 893 894 895 896 897 898 899 900 901
			return -1;

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

	}

	return 0;
}

902
static int snd_hdsp_use_is_exclusive(struct hdsp *hdsp)
L
Linus Torvalds 已提交
903 904 905 906 907 908
{
	unsigned long flags;
	int ret = 1;

	spin_lock_irqsave(&hdsp->lock, flags);
	if ((hdsp->playback_pid != hdsp->capture_pid) &&
T
Takashi Iwai 已提交
909
	    (hdsp->playback_pid >= 0) && (hdsp->capture_pid >= 0))
L
Linus Torvalds 已提交
910 911 912 913 914
		ret = 0;
	spin_unlock_irqrestore(&hdsp->lock, flags);
	return ret;
}

915
static int hdsp_spdif_sample_rate(struct hdsp *hdsp)
L
Linus Torvalds 已提交
916 917 918 919
{
	unsigned int status = hdsp_read(hdsp, HDSP_statusRegister);
	unsigned int rate_bits = (status & HDSP_spdifFrequencyMask);

920 921 922 923
	/* For the 9632, the mask is different */
	if (hdsp->io_type == H9632)
		 rate_bits = (status & HDSP_spdifFrequencyMask_9632);

T
Takashi Iwai 已提交
924
	if (status & HDSP_SPDIFErrorFlag)
L
Linus Torvalds 已提交
925
		return 0;
T
Tim Blechmann 已提交
926

L
Linus Torvalds 已提交
927 928 929 930 931 932 933
	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;
T
Tim Blechmann 已提交
934
	case HDSP_spdifFrequency128KHz:
L
Linus Torvalds 已提交
935 936
		if (hdsp->io_type == H9632) return 128000;
		break;
T
Tim Blechmann 已提交
937
	case HDSP_spdifFrequency176_4KHz:
L
Linus Torvalds 已提交
938 939
		if (hdsp->io_type == H9632) return 176400;
		break;
T
Tim Blechmann 已提交
940
	case HDSP_spdifFrequency192KHz:
L
Linus Torvalds 已提交
941 942 943 944 945 946 947 948 949
		if (hdsp->io_type == H9632) return 192000;
		break;
	default:
		break;
	}
	snd_printk ("Hammerfall-DSP: unknown spdif frequency status; bits = 0x%x, status = 0x%x\n", rate_bits, status);
	return 0;
}

950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974
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;
	}
}

975
static void hdsp_compute_period_size(struct hdsp *hdsp)
L
Linus Torvalds 已提交
976 977 978 979
{
	hdsp->period_bytes = 1 << ((hdsp_decode_latency(hdsp->control_register) + 8));
}

980
static snd_pcm_uframes_t hdsp_hw_pointer(struct hdsp *hdsp)
L
Linus Torvalds 已提交
981 982 983 984 985
{
	int position;

	position = hdsp_read(hdsp, HDSP_statusRegister);

T
Takashi Iwai 已提交
986
	if (!hdsp->precise_ptr)
L
Linus Torvalds 已提交
987 988 989 990 991 992 993 994
		return (position & HDSP_BufferID) ? (hdsp->period_bytes / 4) : 0;

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

995
static void hdsp_reset_hw_pointer(struct hdsp *hdsp)
L
Linus Torvalds 已提交
996 997
{
	hdsp_write (hdsp, HDSP_resetPointer, 0);
998 999 1000 1001 1002
	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);
L
Linus Torvalds 已提交
1003 1004
}

1005
static void hdsp_start_audio(struct hdsp *s)
L
Linus Torvalds 已提交
1006 1007 1008 1009 1010
{
	s->control_register |= (HDSP_AudioInterruptEnable | HDSP_Start);
	hdsp_write(s, HDSP_controlRegister, s->control_register);
}

1011
static void hdsp_stop_audio(struct hdsp *s)
L
Linus Torvalds 已提交
1012 1013 1014 1015 1016
{
	s->control_register &= ~(HDSP_Start | HDSP_AudioInterruptEnable);
	hdsp_write(s, HDSP_controlRegister, s->control_register);
}

1017
static void hdsp_silence_playback(struct hdsp *hdsp)
L
Linus Torvalds 已提交
1018 1019 1020 1021
{
	memset(hdsp->playback_buffer, 0, HDSP_DMA_AREA_BYTES);
}

1022
static int hdsp_set_interrupt_interval(struct hdsp *s, unsigned int frames)
L
Linus Torvalds 已提交
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
{
	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;
}

1047 1048 1049
static void hdsp_set_dds_value(struct hdsp *hdsp, int rate)
{
	u64 n;
T
Tim Blechmann 已提交
1050

1051 1052 1053 1054 1055
	if (rate >= 112000)
		rate /= 4;
	else if (rate >= 56000)
		rate /= 2;

1056
	n = DDS_NUMERATOR;
1057
	n = div_u64(n, rate);
1058
	/* n should be less than 2^32 for being written to FREQ register */
1059
	snd_BUG_ON(n >> 32);
1060 1061 1062 1063 1064 1065
	/* 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);
}

1066
static int hdsp_set_rate(struct hdsp *hdsp, int rate, int called_internally)
L
Linus Torvalds 已提交
1067 1068 1069 1070 1071 1072 1073 1074 1075
{
	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).
	*/
T
Tim Blechmann 已提交
1076 1077

	if (!(hdsp->control_register & HDSP_ClockModeMaster)) {
L
Linus Torvalds 已提交
1078 1079
		if (called_internally) {
			/* request from ctl or card initialization */
T
Takashi Iwai 已提交
1080
			snd_printk(KERN_ERR "Hammerfall-DSP: device is not running as a clock master: cannot set sample rate.\n");
L
Linus Torvalds 已提交
1081
			return -1;
T
Tim Blechmann 已提交
1082
		} else {
L
Linus Torvalds 已提交
1083 1084 1085
			/* hw_param request while in AutoSync mode */
			int external_freq = hdsp_external_sample_rate(hdsp);
			int spdif_freq = hdsp_spdif_sample_rate(hdsp);
T
Tim Blechmann 已提交
1086

T
Takashi Iwai 已提交
1087 1088 1089
			if ((spdif_freq == external_freq*2) && (hdsp_autosync_ref(hdsp) >= HDSP_AUTOSYNC_FROM_ADAT1))
				snd_printk(KERN_INFO "Hammerfall-DSP: Detected ADAT in double speed mode\n");
			else if (hdsp->io_type == H9632 && (spdif_freq == external_freq*4) && (hdsp_autosync_ref(hdsp) >= HDSP_AUTOSYNC_FROM_ADAT1))
T
Tim Blechmann 已提交
1090
				snd_printk(KERN_INFO "Hammerfall-DSP: Detected ADAT in quad speed mode\n");
T
Takashi Iwai 已提交
1091 1092
			else if (rate != external_freq) {
				snd_printk(KERN_INFO "Hammerfall-DSP: No AutoSync source for requested rate\n");
L
Linus Torvalds 已提交
1093
				return -1;
T
Tim Blechmann 已提交
1094 1095
			}
		}
L
Linus Torvalds 已提交
1096 1097 1098 1099 1100 1101
	}

	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
T
Tim Blechmann 已提交
1102
	   such a change causes a shift in the location of
L
Linus Torvalds 已提交
1103
	   the DMA buffers and a reduction in the number of available
T
Tim Blechmann 已提交
1104
	   buffers.
L
Linus Torvalds 已提交
1105 1106 1107 1108 1109

	   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.  */

T
Takashi Iwai 已提交
1110
	if (rate > 96000 && hdsp->io_type != H9632)
L
Linus Torvalds 已提交
1111
		return -EINVAL;
T
Tim Blechmann 已提交
1112

L
Linus Torvalds 已提交
1113 1114
	switch (rate) {
	case 32000:
T
Takashi Iwai 已提交
1115
		if (current_rate > 48000)
L
Linus Torvalds 已提交
1116 1117 1118 1119
			reject_if_open = 1;
		rate_bits = HDSP_Frequency32KHz;
		break;
	case 44100:
T
Takashi Iwai 已提交
1120
		if (current_rate > 48000)
L
Linus Torvalds 已提交
1121 1122 1123 1124
			reject_if_open = 1;
		rate_bits = HDSP_Frequency44_1KHz;
		break;
	case 48000:
T
Takashi Iwai 已提交
1125
		if (current_rate > 48000)
L
Linus Torvalds 已提交
1126 1127 1128 1129
			reject_if_open = 1;
		rate_bits = HDSP_Frequency48KHz;
		break;
	case 64000:
T
Takashi Iwai 已提交
1130
		if (current_rate <= 48000 || current_rate > 96000)
L
Linus Torvalds 已提交
1131 1132 1133 1134
			reject_if_open = 1;
		rate_bits = HDSP_Frequency64KHz;
		break;
	case 88200:
T
Takashi Iwai 已提交
1135
		if (current_rate <= 48000 || current_rate > 96000)
L
Linus Torvalds 已提交
1136 1137 1138 1139
			reject_if_open = 1;
		rate_bits = HDSP_Frequency88_2KHz;
		break;
	case 96000:
T
Takashi Iwai 已提交
1140
		if (current_rate <= 48000 || current_rate > 96000)
L
Linus Torvalds 已提交
1141 1142 1143 1144
			reject_if_open = 1;
		rate_bits = HDSP_Frequency96KHz;
		break;
	case 128000:
T
Takashi Iwai 已提交
1145
		if (current_rate < 128000)
L
Linus Torvalds 已提交
1146 1147 1148 1149
			reject_if_open = 1;
		rate_bits = HDSP_Frequency128KHz;
		break;
	case 176400:
T
Takashi Iwai 已提交
1150
		if (current_rate < 128000)
L
Linus Torvalds 已提交
1151 1152 1153 1154
			reject_if_open = 1;
		rate_bits = HDSP_Frequency176_4KHz;
		break;
	case 192000:
T
Takashi Iwai 已提交
1155
		if (current_rate < 128000)
L
Linus Torvalds 已提交
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
			reject_if_open = 1;
		rate_bits = HDSP_Frequency192KHz;
		break;
	default:
		return -EINVAL;
	}

	if (reject_if_open && (hdsp->capture_pid >= 0 || hdsp->playback_pid >= 0)) {
		snd_printk ("Hammerfall-DSP: cannot change speed mode (capture PID = %d, playback PID = %d)\n",
			    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);

1174 1175 1176 1177
	/* 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);

L
Linus Torvalds 已提交
1178 1179 1180
	if (rate >= 128000) {
		hdsp->channel_map = channel_map_H9632_qs;
	} else if (rate > 48000) {
T
Takashi Iwai 已提交
1181
		if (hdsp->io_type == H9632)
L
Linus Torvalds 已提交
1182
			hdsp->channel_map = channel_map_H9632_ds;
T
Takashi Iwai 已提交
1183
		else
L
Linus Torvalds 已提交
1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
			hdsp->channel_map = channel_map_ds;
	} else {
		switch (hdsp->io_type) {
		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;
		}
	}
T
Tim Blechmann 已提交
1202

L
Linus Torvalds 已提交
1203 1204 1205 1206 1207 1208 1209 1210 1211
	hdsp->system_sample_rate = rate;

	return 0;
}

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

1212
static unsigned char snd_hdsp_midi_read_byte (struct hdsp *hdsp, int id)
L
Linus Torvalds 已提交
1213 1214
{
	/* the hardware already does the relevant bit-mask with 0xff */
T
Takashi Iwai 已提交
1215
	if (id)
L
Linus Torvalds 已提交
1216
		return hdsp_read(hdsp, HDSP_midiDataIn1);
T
Takashi Iwai 已提交
1217
	else
L
Linus Torvalds 已提交
1218 1219 1220
		return hdsp_read(hdsp, HDSP_midiDataIn0);
}

1221
static void snd_hdsp_midi_write_byte (struct hdsp *hdsp, int id, int val)
L
Linus Torvalds 已提交
1222 1223
{
	/* the hardware already does the relevant bit-mask with 0xff */
T
Takashi Iwai 已提交
1224
	if (id)
L
Linus Torvalds 已提交
1225
		hdsp_write(hdsp, HDSP_midiDataOut1, val);
T
Takashi Iwai 已提交
1226
	else
L
Linus Torvalds 已提交
1227 1228 1229
		hdsp_write(hdsp, HDSP_midiDataOut0, val);
}

1230
static int snd_hdsp_midi_input_available (struct hdsp *hdsp, int id)
L
Linus Torvalds 已提交
1231
{
T
Takashi Iwai 已提交
1232
	if (id)
L
Linus Torvalds 已提交
1233
		return (hdsp_read(hdsp, HDSP_midiStatusIn1) & 0xff);
T
Takashi Iwai 已提交
1234
	else
L
Linus Torvalds 已提交
1235 1236 1237
		return (hdsp_read(hdsp, HDSP_midiStatusIn0) & 0xff);
}

1238
static int snd_hdsp_midi_output_possible (struct hdsp *hdsp, int id)
L
Linus Torvalds 已提交
1239 1240 1241
{
	int fifo_bytes_used;

T
Takashi Iwai 已提交
1242
	if (id)
L
Linus Torvalds 已提交
1243
		fifo_bytes_used = hdsp_read(hdsp, HDSP_midiStatusOut1) & 0xff;
T
Takashi Iwai 已提交
1244
	else
L
Linus Torvalds 已提交
1245 1246
		fifo_bytes_used = hdsp_read(hdsp, HDSP_midiStatusOut0) & 0xff;

T
Takashi Iwai 已提交
1247
	if (fifo_bytes_used < 128)
L
Linus Torvalds 已提交
1248
		return  128 - fifo_bytes_used;
T
Takashi Iwai 已提交
1249
	else
L
Linus Torvalds 已提交
1250 1251 1252
		return 0;
}

1253
static void snd_hdsp_flush_midi_input (struct hdsp *hdsp, int id)
L
Linus Torvalds 已提交
1254
{
T
Takashi Iwai 已提交
1255
	while (snd_hdsp_midi_input_available (hdsp, id))
L
Linus Torvalds 已提交
1256 1257 1258
		snd_hdsp_midi_read_byte (hdsp, id);
}

1259
static int snd_hdsp_midi_output_write (struct hdsp_midi *hmidi)
L
Linus Torvalds 已提交
1260 1261 1262 1263 1264 1265 1266 1267
{
	unsigned long flags;
	int n_pending;
	int to_write;
	int i;
	unsigned char buf[128];

	/* Output is not interrupt driven */
T
Tim Blechmann 已提交
1268

L
Linus Torvalds 已提交
1269 1270 1271 1272 1273 1274
	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);
T
Tim Blechmann 已提交
1275

L
Linus Torvalds 已提交
1276
				if ((to_write = snd_rawmidi_transmit (hmidi->output, buf, n_pending)) > 0) {
T
Tim Blechmann 已提交
1277
					for (i = 0; i < to_write; ++i)
L
Linus Torvalds 已提交
1278 1279 1280 1281 1282 1283 1284 1285 1286
						snd_hdsp_midi_write_byte (hmidi->hdsp, hmidi->id, buf[i]);
				}
			}
		}
	}
	spin_unlock_irqrestore (&hmidi->lock, flags);
	return 0;
}

1287
static int snd_hdsp_midi_input_read (struct hdsp_midi *hmidi)
L
Linus Torvalds 已提交
1288 1289 1290 1291 1292 1293 1294 1295 1296
{
	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) {
T
Takashi Iwai 已提交
1297
			if (n_pending > (int)sizeof (buf))
L
Linus Torvalds 已提交
1298
				n_pending = sizeof (buf);
T
Takashi Iwai 已提交
1299
			for (i = 0; i < n_pending; ++i)
L
Linus Torvalds 已提交
1300
				buf[i] = snd_hdsp_midi_read_byte (hmidi->hdsp, hmidi->id);
T
Takashi Iwai 已提交
1301
			if (n_pending)
L
Linus Torvalds 已提交
1302 1303 1304
				snd_rawmidi_receive (hmidi->input, buf, n_pending);
		} else {
			/* flush the MIDI input FIFO */
T
Takashi Iwai 已提交
1305
			while (--n_pending)
L
Linus Torvalds 已提交
1306 1307 1308 1309
				snd_hdsp_midi_read_byte (hmidi->hdsp, hmidi->id);
		}
	}
	hmidi->pending = 0;
T
Takashi Iwai 已提交
1310
	if (hmidi->id)
L
Linus Torvalds 已提交
1311
		hmidi->hdsp->control_register |= HDSP_Midi1InterruptEnable;
T
Takashi Iwai 已提交
1312
	else
L
Linus Torvalds 已提交
1313 1314 1315 1316 1317 1318
		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);
}

1319
static void snd_hdsp_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
L
Linus Torvalds 已提交
1320
{
1321 1322
	struct hdsp *hdsp;
	struct hdsp_midi *hmidi;
L
Linus Torvalds 已提交
1323 1324 1325
	unsigned long flags;
	u32 ie;

1326
	hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
L
Linus Torvalds 已提交
1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345
	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);
}

static void snd_hdsp_midi_output_timer(unsigned long data)
{
1346
	struct hdsp_midi *hmidi = (struct hdsp_midi *) data;
L
Linus Torvalds 已提交
1347
	unsigned long flags;
T
Tim Blechmann 已提交
1348

L
Linus Torvalds 已提交
1349 1350 1351 1352 1353 1354
	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
T
Tim Blechmann 已提交
1355
	   leaving istimer wherever it was set before.
L
Linus Torvalds 已提交
1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
	*/

	if (hmidi->istimer) {
		hmidi->timer.expires = 1 + jiffies;
		add_timer(&hmidi->timer);
	}

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

1366
static void snd_hdsp_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
L
Linus Torvalds 已提交
1367
{
1368
	struct hdsp_midi *hmidi;
L
Linus Torvalds 已提交
1369 1370
	unsigned long flags;

1371
	hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
L
Linus Torvalds 已提交
1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382
	spin_lock_irqsave (&hmidi->lock, flags);
	if (up) {
		if (!hmidi->istimer) {
			init_timer(&hmidi->timer);
			hmidi->timer.function = snd_hdsp_midi_output_timer;
			hmidi->timer.data = (unsigned long) hmidi;
			hmidi->timer.expires = 1 + jiffies;
			add_timer(&hmidi->timer);
			hmidi->istimer++;
		}
	} else {
T
Takashi Iwai 已提交
1383
		if (hmidi->istimer && --hmidi->istimer <= 0)
L
Linus Torvalds 已提交
1384 1385 1386 1387 1388 1389 1390
			del_timer (&hmidi->timer);
	}
	spin_unlock_irqrestore (&hmidi->lock, flags);
	if (up)
		snd_hdsp_midi_output_write(hmidi);
}

1391
static int snd_hdsp_midi_input_open(struct snd_rawmidi_substream *substream)
L
Linus Torvalds 已提交
1392
{
1393
	struct hdsp_midi *hmidi;
L
Linus Torvalds 已提交
1394

1395
	hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
L
Linus Torvalds 已提交
1396 1397 1398 1399 1400 1401 1402 1403
	spin_lock_irq (&hmidi->lock);
	snd_hdsp_flush_midi_input (hmidi->hdsp, hmidi->id);
	hmidi->input = substream;
	spin_unlock_irq (&hmidi->lock);

	return 0;
}

1404
static int snd_hdsp_midi_output_open(struct snd_rawmidi_substream *substream)
L
Linus Torvalds 已提交
1405
{
1406
	struct hdsp_midi *hmidi;
L
Linus Torvalds 已提交
1407

1408
	hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
L
Linus Torvalds 已提交
1409 1410 1411 1412 1413 1414 1415
	spin_lock_irq (&hmidi->lock);
	hmidi->output = substream;
	spin_unlock_irq (&hmidi->lock);

	return 0;
}

1416
static int snd_hdsp_midi_input_close(struct snd_rawmidi_substream *substream)
L
Linus Torvalds 已提交
1417
{
1418
	struct hdsp_midi *hmidi;
L
Linus Torvalds 已提交
1419 1420 1421

	snd_hdsp_midi_input_trigger (substream, 0);

1422
	hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
L
Linus Torvalds 已提交
1423 1424 1425 1426 1427 1428 1429
	spin_lock_irq (&hmidi->lock);
	hmidi->input = NULL;
	spin_unlock_irq (&hmidi->lock);

	return 0;
}

1430
static int snd_hdsp_midi_output_close(struct snd_rawmidi_substream *substream)
L
Linus Torvalds 已提交
1431
{
1432
	struct hdsp_midi *hmidi;
L
Linus Torvalds 已提交
1433 1434 1435

	snd_hdsp_midi_output_trigger (substream, 0);

1436
	hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
L
Linus Torvalds 已提交
1437 1438 1439 1440 1441 1442 1443
	spin_lock_irq (&hmidi->lock);
	hmidi->output = NULL;
	spin_unlock_irq (&hmidi->lock);

	return 0;
}

1444
static struct snd_rawmidi_ops snd_hdsp_midi_output =
L
Linus Torvalds 已提交
1445 1446 1447 1448 1449 1450
{
	.open =		snd_hdsp_midi_output_open,
	.close =	snd_hdsp_midi_output_close,
	.trigger =	snd_hdsp_midi_output_trigger,
};

1451
static struct snd_rawmidi_ops snd_hdsp_midi_input =
L
Linus Torvalds 已提交
1452 1453 1454 1455 1456 1457
{
	.open =		snd_hdsp_midi_input_open,
	.close =	snd_hdsp_midi_input_close,
	.trigger =	snd_hdsp_midi_input_trigger,
};

1458
static int snd_hdsp_create_midi (struct snd_card *card, struct hdsp *hdsp, int id)
L
Linus Torvalds 已提交
1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471
{
	char buf[32];

	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);

	sprintf (buf, "%s MIDI %d", card->shortname, id+1);
T
Takashi Iwai 已提交
1472
	if (snd_rawmidi_new (card, buf, id, 1, 1, &hdsp->midi[id].rmidi) < 0)
L
Linus Torvalds 已提交
1473 1474
		return -1;

1475
	sprintf(hdsp->midi[id].rmidi->name, "HDSP MIDI %d", id+1);
L
Linus Torvalds 已提交
1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491
	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
  ----------------------------------------------------------------------------*/

1492
static u32 snd_hdsp_convert_from_aes(struct snd_aes_iec958 *aes)
L
Linus Torvalds 已提交
1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503
{
	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;
}

1504
static void snd_hdsp_convert_to_aes(struct snd_aes_iec958 *aes, u32 val)
L
Linus Torvalds 已提交
1505 1506 1507 1508 1509 1510 1511 1512 1513
{
	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;
}

1514
static int snd_hdsp_control_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
L
Linus Torvalds 已提交
1515 1516 1517 1518 1519 1520
{
	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
	uinfo->count = 1;
	return 0;
}

1521
static int snd_hdsp_control_spdif_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
1522
{
1523
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
T
Tim Blechmann 已提交
1524

L
Linus Torvalds 已提交
1525 1526 1527 1528
	snd_hdsp_convert_to_aes(&ucontrol->value.iec958, hdsp->creg_spdif);
	return 0;
}

1529
static int snd_hdsp_control_spdif_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
1530
{
1531
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
1532 1533
	int change;
	u32 val;
T
Tim Blechmann 已提交
1534

L
Linus Torvalds 已提交
1535 1536 1537 1538 1539 1540 1541 1542
	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;
}

1543
static int snd_hdsp_control_spdif_stream_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
L
Linus Torvalds 已提交
1544 1545 1546 1547 1548 1549
{
	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
	uinfo->count = 1;
	return 0;
}

1550
static int snd_hdsp_control_spdif_stream_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
1551
{
1552
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
T
Tim Blechmann 已提交
1553

L
Linus Torvalds 已提交
1554 1555 1556 1557
	snd_hdsp_convert_to_aes(&ucontrol->value.iec958, hdsp->creg_spdif_stream);
	return 0;
}

1558
static int snd_hdsp_control_spdif_stream_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
1559
{
1560
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
1561 1562
	int change;
	u32 val;
T
Tim Blechmann 已提交
1563

L
Linus Torvalds 已提交
1564 1565 1566 1567 1568 1569 1570 1571 1572 1573
	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;
}

1574
static int snd_hdsp_control_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
L
Linus Torvalds 已提交
1575 1576 1577 1578 1579 1580
{
	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
	uinfo->count = 1;
	return 0;
}

1581
static int snd_hdsp_control_spdif_mask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
1582 1583 1584 1585 1586 1587
{
	ucontrol->value.iec958.status[0] = kcontrol->private_value;
	return 0;
}

#define HDSP_SPDIF_IN(xname, xindex) \
1588
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
L
Linus Torvalds 已提交
1589 1590 1591 1592 1593 1594
  .name = xname, \
  .index = xindex, \
  .info = snd_hdsp_info_spdif_in, \
  .get = snd_hdsp_get_spdif_in, \
  .put = snd_hdsp_put_spdif_in }

1595
static unsigned int hdsp_spdif_in(struct hdsp *hdsp)
L
Linus Torvalds 已提交
1596 1597 1598 1599
{
	return hdsp_decode_spdif_in(hdsp->control_register & HDSP_SPDIFInputMask);
}

1600
static int hdsp_set_spdif_input(struct hdsp *hdsp, int in)
L
Linus Torvalds 已提交
1601 1602 1603 1604 1605 1606 1607
{
	hdsp->control_register &= ~HDSP_SPDIFInputMask;
	hdsp->control_register |= hdsp_encode_spdif_in(in);
	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
	return 0;
}

1608
static int snd_hdsp_info_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
L
Linus Torvalds 已提交
1609 1610
{
	static char *texts[4] = {"Optical", "Coaxial", "Internal", "AES"};
1611
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
1612 1613 1614 1615 1616 1617 1618 1619 1620 1621

	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
	uinfo->count = 1;
	uinfo->value.enumerated.items = ((hdsp->io_type == H9632) ? 4 : 3);
	if (uinfo->value.enumerated.item > ((hdsp->io_type == H9632) ? 3 : 2))
		uinfo->value.enumerated.item = ((hdsp->io_type == H9632) ? 3 : 2);
	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
	return 0;
}

1622
static int snd_hdsp_get_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
1623
{
1624
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
T
Tim Blechmann 已提交
1625

L
Linus Torvalds 已提交
1626 1627 1628 1629
	ucontrol->value.enumerated.item[0] = hdsp_spdif_in(hdsp);
	return 0;
}

1630
static int snd_hdsp_put_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
1631
{
1632
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
1633 1634
	int change;
	unsigned int val;
T
Tim Blechmann 已提交
1635

L
Linus Torvalds 已提交
1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647
	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;
}

#define HDSP_SPDIF_OUT(xname, xindex) \
1648
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
L
Linus Torvalds 已提交
1649 1650 1651
  .info = snd_hdsp_info_spdif_bits, \
  .get = snd_hdsp_get_spdif_out, .put = snd_hdsp_put_spdif_out }

1652
static int hdsp_spdif_out(struct hdsp *hdsp)
L
Linus Torvalds 已提交
1653 1654 1655 1656
{
	return (hdsp->control_register & HDSP_SPDIFOpticalOut) ? 1 : 0;
}

1657
static int hdsp_set_spdif_output(struct hdsp *hdsp, int out)
L
Linus Torvalds 已提交
1658
{
T
Takashi Iwai 已提交
1659
	if (out)
L
Linus Torvalds 已提交
1660
		hdsp->control_register |= HDSP_SPDIFOpticalOut;
T
Takashi Iwai 已提交
1661
	else
L
Linus Torvalds 已提交
1662 1663 1664 1665 1666
		hdsp->control_register &= ~HDSP_SPDIFOpticalOut;
	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
	return 0;
}

1667
#define snd_hdsp_info_spdif_bits	snd_ctl_boolean_mono_info
L
Linus Torvalds 已提交
1668

1669
static int snd_hdsp_get_spdif_out(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
1670
{
1671
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
T
Tim Blechmann 已提交
1672

L
Linus Torvalds 已提交
1673 1674 1675 1676
	ucontrol->value.integer.value[0] = hdsp_spdif_out(hdsp);
	return 0;
}

1677
static int snd_hdsp_put_spdif_out(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
1678
{
1679
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
1680 1681
	int change;
	unsigned int val;
T
Tim Blechmann 已提交
1682

L
Linus Torvalds 已提交
1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693
	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_spdif_out(hdsp);
	hdsp_set_spdif_output(hdsp, val);
	spin_unlock_irq(&hdsp->lock);
	return change;
}

#define HDSP_SPDIF_PROFESSIONAL(xname, xindex) \
1694
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
L
Linus Torvalds 已提交
1695 1696 1697
  .info = snd_hdsp_info_spdif_bits, \
  .get = snd_hdsp_get_spdif_professional, .put = snd_hdsp_put_spdif_professional }

1698
static int hdsp_spdif_professional(struct hdsp *hdsp)
L
Linus Torvalds 已提交
1699 1700 1701 1702
{
	return (hdsp->control_register & HDSP_SPDIFProfessional) ? 1 : 0;
}

1703
static int hdsp_set_spdif_professional(struct hdsp *hdsp, int val)
L
Linus Torvalds 已提交
1704
{
T
Takashi Iwai 已提交
1705
	if (val)
L
Linus Torvalds 已提交
1706
		hdsp->control_register |= HDSP_SPDIFProfessional;
T
Takashi Iwai 已提交
1707
	else
L
Linus Torvalds 已提交
1708 1709 1710 1711 1712
		hdsp->control_register &= ~HDSP_SPDIFProfessional;
	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
	return 0;
}

1713
static int snd_hdsp_get_spdif_professional(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
1714
{
1715
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
T
Tim Blechmann 已提交
1716

L
Linus Torvalds 已提交
1717 1718 1719 1720
	ucontrol->value.integer.value[0] = hdsp_spdif_professional(hdsp);
	return 0;
}

1721
static int snd_hdsp_put_spdif_professional(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
1722
{
1723
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
1724 1725
	int change;
	unsigned int val;
T
Tim Blechmann 已提交
1726

L
Linus Torvalds 已提交
1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737
	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_spdif_professional(hdsp);
	hdsp_set_spdif_professional(hdsp, val);
	spin_unlock_irq(&hdsp->lock);
	return change;
}

#define HDSP_SPDIF_EMPHASIS(xname, xindex) \
1738
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
L
Linus Torvalds 已提交
1739 1740 1741
  .info = snd_hdsp_info_spdif_bits, \
  .get = snd_hdsp_get_spdif_emphasis, .put = snd_hdsp_put_spdif_emphasis }

1742
static int hdsp_spdif_emphasis(struct hdsp *hdsp)
L
Linus Torvalds 已提交
1743 1744 1745 1746
{
	return (hdsp->control_register & HDSP_SPDIFEmphasis) ? 1 : 0;
}

1747
static int hdsp_set_spdif_emphasis(struct hdsp *hdsp, int val)
L
Linus Torvalds 已提交
1748
{
T
Takashi Iwai 已提交
1749
	if (val)
L
Linus Torvalds 已提交
1750
		hdsp->control_register |= HDSP_SPDIFEmphasis;
T
Takashi Iwai 已提交
1751
	else
L
Linus Torvalds 已提交
1752 1753 1754 1755 1756
		hdsp->control_register &= ~HDSP_SPDIFEmphasis;
	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
	return 0;
}

1757
static int snd_hdsp_get_spdif_emphasis(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
1758
{
1759
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
T
Tim Blechmann 已提交
1760

L
Linus Torvalds 已提交
1761 1762 1763 1764
	ucontrol->value.integer.value[0] = hdsp_spdif_emphasis(hdsp);
	return 0;
}

1765
static int snd_hdsp_put_spdif_emphasis(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
1766
{
1767
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
1768 1769
	int change;
	unsigned int val;
T
Tim Blechmann 已提交
1770

L
Linus Torvalds 已提交
1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781
	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_spdif_emphasis(hdsp);
	hdsp_set_spdif_emphasis(hdsp, val);
	spin_unlock_irq(&hdsp->lock);
	return change;
}

#define HDSP_SPDIF_NON_AUDIO(xname, xindex) \
1782
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
L
Linus Torvalds 已提交
1783 1784 1785
  .info = snd_hdsp_info_spdif_bits, \
  .get = snd_hdsp_get_spdif_nonaudio, .put = snd_hdsp_put_spdif_nonaudio }

1786
static int hdsp_spdif_nonaudio(struct hdsp *hdsp)
L
Linus Torvalds 已提交
1787 1788 1789 1790
{
	return (hdsp->control_register & HDSP_SPDIFNonAudio) ? 1 : 0;
}

1791
static int hdsp_set_spdif_nonaudio(struct hdsp *hdsp, int val)
L
Linus Torvalds 已提交
1792
{
T
Takashi Iwai 已提交
1793
	if (val)
L
Linus Torvalds 已提交
1794
		hdsp->control_register |= HDSP_SPDIFNonAudio;
T
Takashi Iwai 已提交
1795
	else
L
Linus Torvalds 已提交
1796 1797 1798 1799 1800
		hdsp->control_register &= ~HDSP_SPDIFNonAudio;
	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
	return 0;
}

1801
static int snd_hdsp_get_spdif_nonaudio(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
1802
{
1803
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
T
Tim Blechmann 已提交
1804

L
Linus Torvalds 已提交
1805 1806 1807 1808
	ucontrol->value.integer.value[0] = hdsp_spdif_nonaudio(hdsp);
	return 0;
}

1809
static int snd_hdsp_put_spdif_nonaudio(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
1810
{
1811
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
1812 1813
	int change;
	unsigned int val;
T
Tim Blechmann 已提交
1814

L
Linus Torvalds 已提交
1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825
	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_spdif_nonaudio(hdsp);
	hdsp_set_spdif_nonaudio(hdsp, val);
	spin_unlock_irq(&hdsp->lock);
	return change;
}

#define HDSP_SPDIF_SAMPLE_RATE(xname, xindex) \
1826
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
L
Linus Torvalds 已提交
1827 1828 1829 1830 1831 1832 1833
  .name = xname, \
  .index = xindex, \
  .access = SNDRV_CTL_ELEM_ACCESS_READ, \
  .info = snd_hdsp_info_spdif_sample_rate, \
  .get = snd_hdsp_get_spdif_sample_rate \
}

1834
static int snd_hdsp_info_spdif_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
L
Linus Torvalds 已提交
1835 1836
{
	static char *texts[] = {"32000", "44100", "48000", "64000", "88200", "96000", "None", "128000", "176400", "192000"};
1837
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
1838 1839 1840 1841 1842 1843 1844 1845 1846 1847

	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
	uinfo->count = 1;
	uinfo->value.enumerated.items = (hdsp->io_type == H9632) ? 10 : 7;
	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
	return 0;
}

1848
static int snd_hdsp_get_spdif_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
1849
{
1850
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
T
Tim Blechmann 已提交
1851

L
Linus Torvalds 已提交
1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880
	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:
T
Tim Blechmann 已提交
1881
		ucontrol->value.enumerated.item[0] = 6;
L
Linus Torvalds 已提交
1882 1883 1884 1885 1886
	}
	return 0;
}

#define HDSP_SYSTEM_SAMPLE_RATE(xname, xindex) \
1887
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
L
Linus Torvalds 已提交
1888 1889 1890 1891 1892 1893 1894
  .name = xname, \
  .index = xindex, \
  .access = SNDRV_CTL_ELEM_ACCESS_READ, \
  .info = snd_hdsp_info_system_sample_rate, \
  .get = snd_hdsp_get_system_sample_rate \
}

1895
static int snd_hdsp_info_system_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
L
Linus Torvalds 已提交
1896 1897 1898 1899 1900 1901
{
	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
	uinfo->count = 1;
	return 0;
}

1902
static int snd_hdsp_get_system_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
1903
{
1904
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
T
Tim Blechmann 已提交
1905

L
Linus Torvalds 已提交
1906 1907 1908 1909 1910
	ucontrol->value.enumerated.item[0] = hdsp->system_sample_rate;
	return 0;
}

#define HDSP_AUTOSYNC_SAMPLE_RATE(xname, xindex) \
1911
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
L
Linus Torvalds 已提交
1912 1913 1914 1915 1916 1917 1918
  .name = xname, \
  .index = xindex, \
  .access = SNDRV_CTL_ELEM_ACCESS_READ, \
  .info = snd_hdsp_info_autosync_sample_rate, \
  .get = snd_hdsp_get_autosync_sample_rate \
}

1919
static int snd_hdsp_info_autosync_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
L
Linus Torvalds 已提交
1920
{
1921
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
T
Tim Blechmann 已提交
1922
	static char *texts[] = {"32000", "44100", "48000", "64000", "88200", "96000", "None", "128000", "176400", "192000"};
L
Linus Torvalds 已提交
1923 1924 1925 1926 1927 1928 1929 1930 1931
	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
	uinfo->count = 1;
	uinfo->value.enumerated.items = (hdsp->io_type == H9632) ? 10 : 7 ;
	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
	return 0;
}

1932
static int snd_hdsp_get_autosync_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
1933
{
1934
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
T
Tim Blechmann 已提交
1935

L
Linus Torvalds 已提交
1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962
	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;
T
Tim Blechmann 已提交
1963
		break;
L
Linus Torvalds 已提交
1964
	default:
T
Tim Blechmann 已提交
1965
		ucontrol->value.enumerated.item[0] = 6;
L
Linus Torvalds 已提交
1966 1967 1968 1969 1970
	}
	return 0;
}

#define HDSP_SYSTEM_CLOCK_MODE(xname, xindex) \
1971
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
L
Linus Torvalds 已提交
1972 1973 1974 1975 1976 1977 1978
  .name = xname, \
  .index = xindex, \
  .access = SNDRV_CTL_ELEM_ACCESS_READ, \
  .info = snd_hdsp_info_system_clock_mode, \
  .get = snd_hdsp_get_system_clock_mode \
}

1979
static int hdsp_system_clock_mode(struct hdsp *hdsp)
L
Linus Torvalds 已提交
1980
{
T
Takashi Iwai 已提交
1981
	if (hdsp->control_register & HDSP_ClockModeMaster)
L
Linus Torvalds 已提交
1982
		return 0;
T
Takashi Iwai 已提交
1983
	else if (hdsp_external_sample_rate(hdsp) != hdsp->system_sample_rate)
L
Linus Torvalds 已提交
1984 1985 1986 1987
			return 0;
	return 1;
}

1988
static int snd_hdsp_info_system_clock_mode(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
L
Linus Torvalds 已提交
1989 1990
{
	static char *texts[] = {"Master", "Slave" };
T
Tim Blechmann 已提交
1991

L
Linus Torvalds 已提交
1992 1993 1994 1995 1996 1997 1998 1999 2000
	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
	uinfo->count = 1;
	uinfo->value.enumerated.items = 2;
	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
	return 0;
}

2001
static int snd_hdsp_get_system_clock_mode(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
2002
{
2003
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
T
Tim Blechmann 已提交
2004

L
Linus Torvalds 已提交
2005 2006 2007 2008 2009
	ucontrol->value.enumerated.item[0] = hdsp_system_clock_mode(hdsp);
	return 0;
}

#define HDSP_CLOCK_SOURCE(xname, xindex) \
2010
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
L
Linus Torvalds 已提交
2011 2012 2013 2014 2015 2016 2017
  .name = xname, \
  .index = xindex, \
  .info = snd_hdsp_info_clock_source, \
  .get = snd_hdsp_get_clock_source, \
  .put = snd_hdsp_put_clock_source \
}

2018
static int hdsp_clock_source(struct hdsp *hdsp)
L
Linus Torvalds 已提交
2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040
{
	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:
T
Tim Blechmann 已提交
2041
			return 3;
L
Linus Torvalds 已提交
2042 2043 2044 2045 2046 2047
		}
	} else {
		return 0;
	}
}

2048
static int hdsp_set_clock_source(struct hdsp *hdsp, int mode)
L
Linus Torvalds 已提交
2049 2050 2051 2052 2053 2054
{
	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)) {
T
Tim Blechmann 已提交
2055
			hdsp->control_register &= ~HDSP_ClockModeMaster;
L
Linus Torvalds 已提交
2056 2057 2058 2059 2060 2061 2062 2063 2064 2065
			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;
T
Tim Blechmann 已提交
2066
		break;
L
Linus Torvalds 已提交
2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096
	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;
}

2097
static int snd_hdsp_info_clock_source(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
L
Linus Torvalds 已提交
2098 2099
{
	static char *texts[] = {"AutoSync", "Internal 32.0 kHz", "Internal 44.1 kHz", "Internal 48.0 kHz", "Internal 64.0 kHz", "Internal 88.2 kHz", "Internal 96.0 kHz", "Internal 128 kHz", "Internal 176.4 kHz", "Internal 192.0 KHz" };
2100
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
T
Tim Blechmann 已提交
2101

L
Linus Torvalds 已提交
2102 2103 2104 2105 2106
	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
	uinfo->count = 1;
	if (hdsp->io_type == H9632)
	    uinfo->value.enumerated.items = 10;
	else
T
Tim Blechmann 已提交
2107
	    uinfo->value.enumerated.items = 7;
L
Linus Torvalds 已提交
2108 2109 2110 2111 2112 2113
	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
	return 0;
}

2114
static int snd_hdsp_get_clock_source(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
2115
{
2116
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
T
Tim Blechmann 已提交
2117

L
Linus Torvalds 已提交
2118 2119 2120 2121
	ucontrol->value.enumerated.item[0] = hdsp_clock_source(hdsp);
	return 0;
}

2122
static int snd_hdsp_put_clock_source(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
2123
{
2124
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
2125 2126
	int change;
	int val;
T
Tim Blechmann 已提交
2127

L
Linus Torvalds 已提交
2128 2129 2130 2131 2132
	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) {
T
Takashi Iwai 已提交
2133 2134
		if (val > 9)
			val = 9;
L
Linus Torvalds 已提交
2135
	} else {
T
Takashi Iwai 已提交
2136 2137
		if (val > 6)
			val = 6;
L
Linus Torvalds 已提交
2138 2139
	}
	spin_lock_irq(&hdsp->lock);
T
Takashi Iwai 已提交
2140
	if (val != hdsp_clock_source(hdsp))
L
Linus Torvalds 已提交
2141
		change = (hdsp_set_clock_source(hdsp, val) == 0) ? 1 : 0;
T
Takashi Iwai 已提交
2142
	else
L
Linus Torvalds 已提交
2143 2144 2145 2146 2147
		change = 0;
	spin_unlock_irq(&hdsp->lock);
	return change;
}

2148
#define snd_hdsp_info_clock_source_lock		snd_ctl_boolean_mono_info
2149

2150
static int snd_hdsp_get_clock_source_lock(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2151
{
2152
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
T
Tim Blechmann 已提交
2153

2154 2155 2156 2157
	ucontrol->value.integer.value[0] = hdsp->clock_source_locked;
	return 0;
}

2158
static int snd_hdsp_put_clock_source_lock(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2159
{
2160
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2161 2162 2163 2164
	int change;

	change = (int)ucontrol->value.integer.value[0] != hdsp->clock_source_locked;
	if (change)
2165
		hdsp->clock_source_locked = !!ucontrol->value.integer.value[0];
2166 2167 2168
	return change;
}

L
Linus Torvalds 已提交
2169
#define HDSP_DA_GAIN(xname, xindex) \
2170
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
L
Linus Torvalds 已提交
2171 2172 2173 2174 2175 2176 2177
  .name = xname, \
  .index = xindex, \
  .info = snd_hdsp_info_da_gain, \
  .get = snd_hdsp_get_da_gain, \
  .put = snd_hdsp_put_da_gain \
}

2178
static int hdsp_da_gain(struct hdsp *hdsp)
L
Linus Torvalds 已提交
2179 2180 2181 2182 2183 2184 2185 2186 2187
{
	switch (hdsp->control_register & HDSP_DAGainMask) {
	case HDSP_DAGainHighGain:
		return 0;
	case HDSP_DAGainPlus4dBu:
		return 1;
	case HDSP_DAGainMinus10dBV:
		return 2;
	default:
T
Tim Blechmann 已提交
2188
		return 1;
L
Linus Torvalds 已提交
2189 2190 2191
	}
}

2192
static int hdsp_set_da_gain(struct hdsp *hdsp, int mode)
L
Linus Torvalds 已提交
2193 2194 2195 2196 2197 2198 2199 2200 2201 2202
{
	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:
T
Tim Blechmann 已提交
2203 2204
		hdsp->control_register |= HDSP_DAGainMinus10dBV;
		break;
L
Linus Torvalds 已提交
2205 2206 2207 2208 2209 2210 2211 2212
	default:
		return -1;

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

2213
static int snd_hdsp_info_da_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
L
Linus Torvalds 已提交
2214 2215
{
	static char *texts[] = {"Hi Gain", "+4 dBu", "-10 dbV"};
T
Tim Blechmann 已提交
2216

L
Linus Torvalds 已提交
2217 2218 2219 2220 2221 2222 2223 2224 2225
	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
	uinfo->count = 1;
	uinfo->value.enumerated.items = 3;
	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
	return 0;
}

2226
static int snd_hdsp_get_da_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
2227
{
2228
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
T
Tim Blechmann 已提交
2229

L
Linus Torvalds 已提交
2230 2231 2232 2233
	ucontrol->value.enumerated.item[0] = hdsp_da_gain(hdsp);
	return 0;
}

2234
static int snd_hdsp_put_da_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
2235
{
2236
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
2237 2238
	int change;
	int val;
T
Tim Blechmann 已提交
2239

L
Linus Torvalds 已提交
2240 2241 2242 2243 2244 2245
	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);
T
Takashi Iwai 已提交
2246
	if (val != hdsp_da_gain(hdsp))
L
Linus Torvalds 已提交
2247
		change = (hdsp_set_da_gain(hdsp, val) == 0) ? 1 : 0;
T
Takashi Iwai 已提交
2248
	else
L
Linus Torvalds 已提交
2249 2250 2251 2252 2253 2254
		change = 0;
	spin_unlock_irq(&hdsp->lock);
	return change;
}

#define HDSP_AD_GAIN(xname, xindex) \
2255
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
L
Linus Torvalds 已提交
2256 2257 2258 2259 2260 2261 2262
  .name = xname, \
  .index = xindex, \
  .info = snd_hdsp_info_ad_gain, \
  .get = snd_hdsp_get_ad_gain, \
  .put = snd_hdsp_put_ad_gain \
}

2263
static int hdsp_ad_gain(struct hdsp *hdsp)
L
Linus Torvalds 已提交
2264 2265 2266 2267 2268 2269 2270 2271 2272
{
	switch (hdsp->control_register & HDSP_ADGainMask) {
	case HDSP_ADGainMinus10dBV:
		return 0;
	case HDSP_ADGainPlus4dBu:
		return 1;
	case HDSP_ADGainLowGain:
		return 2;
	default:
T
Tim Blechmann 已提交
2273
		return 1;
L
Linus Torvalds 已提交
2274 2275 2276
	}
}

2277
static int hdsp_set_ad_gain(struct hdsp *hdsp, int mode)
L
Linus Torvalds 已提交
2278 2279 2280 2281 2282 2283 2284
{
	hdsp->control_register &= ~HDSP_ADGainMask;
	switch (mode) {
	case 0:
		hdsp->control_register |= HDSP_ADGainMinus10dBV;
		break;
	case 1:
T
Tim Blechmann 已提交
2285
		hdsp->control_register |= HDSP_ADGainPlus4dBu;
L
Linus Torvalds 已提交
2286 2287
		break;
	case 2:
T
Tim Blechmann 已提交
2288 2289
		hdsp->control_register |= HDSP_ADGainLowGain;
		break;
L
Linus Torvalds 已提交
2290 2291 2292 2293 2294 2295 2296 2297
	default:
		return -1;

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

2298
static int snd_hdsp_info_ad_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
L
Linus Torvalds 已提交
2299 2300
{
	static char *texts[] = {"-10 dBV", "+4 dBu", "Lo Gain"};
T
Tim Blechmann 已提交
2301

L
Linus Torvalds 已提交
2302 2303 2304 2305 2306 2307 2308 2309 2310
	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
	uinfo->count = 1;
	uinfo->value.enumerated.items = 3;
	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
	return 0;
}

2311
static int snd_hdsp_get_ad_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
2312
{
2313
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
T
Tim Blechmann 已提交
2314

L
Linus Torvalds 已提交
2315 2316 2317 2318
	ucontrol->value.enumerated.item[0] = hdsp_ad_gain(hdsp);
	return 0;
}

2319
static int snd_hdsp_put_ad_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
2320
{
2321
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
2322 2323
	int change;
	int val;
T
Tim Blechmann 已提交
2324

L
Linus Torvalds 已提交
2325 2326 2327 2328 2329 2330
	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);
T
Takashi Iwai 已提交
2331
	if (val != hdsp_ad_gain(hdsp))
L
Linus Torvalds 已提交
2332
		change = (hdsp_set_ad_gain(hdsp, val) == 0) ? 1 : 0;
T
Takashi Iwai 已提交
2333
	else
L
Linus Torvalds 已提交
2334 2335 2336 2337 2338 2339
		change = 0;
	spin_unlock_irq(&hdsp->lock);
	return change;
}

#define HDSP_PHONE_GAIN(xname, xindex) \
2340
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
L
Linus Torvalds 已提交
2341 2342 2343 2344 2345 2346 2347
  .name = xname, \
  .index = xindex, \
  .info = snd_hdsp_info_phone_gain, \
  .get = snd_hdsp_get_phone_gain, \
  .put = snd_hdsp_put_phone_gain \
}

2348
static int hdsp_phone_gain(struct hdsp *hdsp)
L
Linus Torvalds 已提交
2349 2350 2351 2352 2353 2354 2355 2356 2357
{
	switch (hdsp->control_register & HDSP_PhoneGainMask) {
	case HDSP_PhoneGain0dB:
		return 0;
	case HDSP_PhoneGainMinus6dB:
		return 1;
	case HDSP_PhoneGainMinus12dB:
		return 2;
	default:
T
Tim Blechmann 已提交
2358
		return 0;
L
Linus Torvalds 已提交
2359 2360 2361
	}
}

2362
static int hdsp_set_phone_gain(struct hdsp *hdsp, int mode)
L
Linus Torvalds 已提交
2363 2364 2365 2366 2367 2368 2369
{
	hdsp->control_register &= ~HDSP_PhoneGainMask;
	switch (mode) {
	case 0:
		hdsp->control_register |= HDSP_PhoneGain0dB;
		break;
	case 1:
T
Tim Blechmann 已提交
2370
		hdsp->control_register |= HDSP_PhoneGainMinus6dB;
L
Linus Torvalds 已提交
2371 2372
		break;
	case 2:
T
Tim Blechmann 已提交
2373 2374
		hdsp->control_register |= HDSP_PhoneGainMinus12dB;
		break;
L
Linus Torvalds 已提交
2375 2376 2377 2378 2379 2380 2381 2382
	default:
		return -1;

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

2383
static int snd_hdsp_info_phone_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
L
Linus Torvalds 已提交
2384 2385
{
	static char *texts[] = {"0 dB", "-6 dB", "-12 dB"};
T
Tim Blechmann 已提交
2386

L
Linus Torvalds 已提交
2387 2388 2389 2390 2391 2392 2393 2394 2395
	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
	uinfo->count = 1;
	uinfo->value.enumerated.items = 3;
	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
	return 0;
}

2396
static int snd_hdsp_get_phone_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
2397
{
2398
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
T
Tim Blechmann 已提交
2399

L
Linus Torvalds 已提交
2400 2401 2402 2403
	ucontrol->value.enumerated.item[0] = hdsp_phone_gain(hdsp);
	return 0;
}

2404
static int snd_hdsp_put_phone_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
2405
{
2406
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
2407 2408
	int change;
	int val;
T
Tim Blechmann 已提交
2409

L
Linus Torvalds 已提交
2410 2411 2412 2413 2414 2415
	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);
T
Takashi Iwai 已提交
2416
	if (val != hdsp_phone_gain(hdsp))
L
Linus Torvalds 已提交
2417
		change = (hdsp_set_phone_gain(hdsp, val) == 0) ? 1 : 0;
T
Takashi Iwai 已提交
2418
	else
L
Linus Torvalds 已提交
2419 2420 2421 2422 2423 2424
		change = 0;
	spin_unlock_irq(&hdsp->lock);
	return change;
}

#define HDSP_XLR_BREAKOUT_CABLE(xname, xindex) \
2425
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
L
Linus Torvalds 已提交
2426 2427 2428 2429 2430 2431 2432
  .name = xname, \
  .index = xindex, \
  .info = snd_hdsp_info_xlr_breakout_cable, \
  .get = snd_hdsp_get_xlr_breakout_cable, \
  .put = snd_hdsp_put_xlr_breakout_cable \
}

2433
static int hdsp_xlr_breakout_cable(struct hdsp *hdsp)
L
Linus Torvalds 已提交
2434
{
T
Takashi Iwai 已提交
2435
	if (hdsp->control_register & HDSP_XLRBreakoutCable)
L
Linus Torvalds 已提交
2436 2437 2438 2439
		return 1;
	return 0;
}

2440
static int hdsp_set_xlr_breakout_cable(struct hdsp *hdsp, int mode)
L
Linus Torvalds 已提交
2441
{
T
Takashi Iwai 已提交
2442
	if (mode)
L
Linus Torvalds 已提交
2443
		hdsp->control_register |= HDSP_XLRBreakoutCable;
T
Takashi Iwai 已提交
2444
	else
L
Linus Torvalds 已提交
2445 2446 2447 2448 2449
		hdsp->control_register &= ~HDSP_XLRBreakoutCable;
	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
	return 0;
}

2450
#define snd_hdsp_info_xlr_breakout_cable	snd_ctl_boolean_mono_info
L
Linus Torvalds 已提交
2451

2452
static int snd_hdsp_get_xlr_breakout_cable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
2453
{
2454
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
T
Tim Blechmann 已提交
2455

L
Linus Torvalds 已提交
2456 2457 2458 2459
	ucontrol->value.enumerated.item[0] = hdsp_xlr_breakout_cable(hdsp);
	return 0;
}

2460
static int snd_hdsp_put_xlr_breakout_cable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
2461
{
2462
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
2463 2464
	int change;
	int val;
T
Tim Blechmann 已提交
2465

L
Linus Torvalds 已提交
2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480
	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_xlr_breakout_cable(hdsp);
	hdsp_set_xlr_breakout_cable(hdsp, val);
	spin_unlock_irq(&hdsp->lock);
	return change;
}

/* (De)activates old RME Analog Extension Board
   These are connected to the internal ADAT connector
   Switching this on desactivates external ADAT
*/
#define HDSP_AEB(xname, xindex) \
2481
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
L
Linus Torvalds 已提交
2482 2483 2484 2485 2486 2487 2488
  .name = xname, \
  .index = xindex, \
  .info = snd_hdsp_info_aeb, \
  .get = snd_hdsp_get_aeb, \
  .put = snd_hdsp_put_aeb \
}

2489
static int hdsp_aeb(struct hdsp *hdsp)
L
Linus Torvalds 已提交
2490
{
T
Takashi Iwai 已提交
2491
	if (hdsp->control_register & HDSP_AnalogExtensionBoard)
L
Linus Torvalds 已提交
2492 2493 2494 2495
		return 1;
	return 0;
}

2496
static int hdsp_set_aeb(struct hdsp *hdsp, int mode)
L
Linus Torvalds 已提交
2497
{
T
Takashi Iwai 已提交
2498
	if (mode)
L
Linus Torvalds 已提交
2499
		hdsp->control_register |= HDSP_AnalogExtensionBoard;
T
Takashi Iwai 已提交
2500
	else
L
Linus Torvalds 已提交
2501 2502 2503 2504 2505
		hdsp->control_register &= ~HDSP_AnalogExtensionBoard;
	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
	return 0;
}

2506
#define snd_hdsp_info_aeb		snd_ctl_boolean_mono_info
L
Linus Torvalds 已提交
2507

2508
static int snd_hdsp_get_aeb(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
2509
{
2510
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
T
Tim Blechmann 已提交
2511

L
Linus Torvalds 已提交
2512 2513 2514 2515
	ucontrol->value.enumerated.item[0] = hdsp_aeb(hdsp);
	return 0;
}

2516
static int snd_hdsp_put_aeb(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
2517
{
2518
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
2519 2520
	int change;
	int val;
T
Tim Blechmann 已提交
2521

L
Linus Torvalds 已提交
2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532
	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_aeb(hdsp);
	hdsp_set_aeb(hdsp, val);
	spin_unlock_irq(&hdsp->lock);
	return change;
}

#define HDSP_PREF_SYNC_REF(xname, xindex) \
2533
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
L
Linus Torvalds 已提交
2534 2535 2536 2537 2538 2539 2540
  .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 \
}

2541
static int hdsp_pref_sync_ref(struct hdsp *hdsp)
L
Linus Torvalds 已提交
2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565
{
	/* 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;
}

2566
static int hdsp_set_pref_sync_ref(struct hdsp *hdsp, int pref)
L
Linus Torvalds 已提交
2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594
{
	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;
}

2595
static int snd_hdsp_info_pref_sync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
L
Linus Torvalds 已提交
2596 2597
{
	static char *texts[] = {"Word", "IEC958", "ADAT1", "ADAT Sync", "ADAT2", "ADAT3" };
2598
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
T
Tim Blechmann 已提交
2599

L
Linus Torvalds 已提交
2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617
	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
	uinfo->count = 1;

	switch (hdsp->io_type) {
	case Digiface:
	case H9652:
		uinfo->value.enumerated.items = 6;
		break;
	case Multiface:
		uinfo->value.enumerated.items = 4;
		break;
	case H9632:
		uinfo->value.enumerated.items = 3;
		break;
	default:
		uinfo->value.enumerated.items = 0;
		break;
	}
T
Tim Blechmann 已提交
2618

L
Linus Torvalds 已提交
2619 2620 2621 2622 2623 2624
	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
	return 0;
}

2625
static int snd_hdsp_get_pref_sync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
2626
{
2627
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
T
Tim Blechmann 已提交
2628

L
Linus Torvalds 已提交
2629 2630 2631 2632
	ucontrol->value.enumerated.item[0] = hdsp_pref_sync_ref(hdsp);
	return 0;
}

2633
static int snd_hdsp_put_pref_sync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
2634
{
2635
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
2636 2637
	int change, max;
	unsigned int val;
T
Tim Blechmann 已提交
2638

L
Linus Torvalds 已提交
2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665
	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) \
2666
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
L
Linus Torvalds 已提交
2667 2668 2669 2670 2671 2672 2673
  .name = xname, \
  .index = xindex, \
  .access = SNDRV_CTL_ELEM_ACCESS_READ, \
  .info = snd_hdsp_info_autosync_ref, \
  .get = snd_hdsp_get_autosync_ref, \
}

2674
static int hdsp_autosync_ref(struct hdsp *hdsp)
L
Linus Torvalds 已提交
2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686
{
	/* 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:
T
Tim Blechmann 已提交
2687
		return HDSP_AUTOSYNC_FROM_NONE;
L
Linus Torvalds 已提交
2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699
	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;
}

2700
static int snd_hdsp_info_autosync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
L
Linus Torvalds 已提交
2701 2702
{
	static char *texts[] = {"Word", "ADAT Sync", "IEC958", "None", "ADAT1", "ADAT2", "ADAT3" };
T
Tim Blechmann 已提交
2703

L
Linus Torvalds 已提交
2704 2705 2706 2707 2708 2709 2710 2711 2712
	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
	uinfo->count = 1;
	uinfo->value.enumerated.items = 7;
	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
	return 0;
}

2713
static int snd_hdsp_get_autosync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
2714
{
2715
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
T
Tim Blechmann 已提交
2716

L
Linus Torvalds 已提交
2717 2718 2719 2720 2721
	ucontrol->value.enumerated.item[0] = hdsp_autosync_ref(hdsp);
	return 0;
}

#define HDSP_LINE_OUT(xname, xindex) \
2722
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
L
Linus Torvalds 已提交
2723 2724 2725 2726 2727 2728 2729
  .name = xname, \
  .index = xindex, \
  .info = snd_hdsp_info_line_out, \
  .get = snd_hdsp_get_line_out, \
  .put = snd_hdsp_put_line_out \
}

2730
static int hdsp_line_out(struct hdsp *hdsp)
L
Linus Torvalds 已提交
2731 2732 2733 2734
{
	return (hdsp->control_register & HDSP_LineOut) ? 1 : 0;
}

2735
static int hdsp_set_line_output(struct hdsp *hdsp, int out)
L
Linus Torvalds 已提交
2736
{
T
Takashi Iwai 已提交
2737
	if (out)
L
Linus Torvalds 已提交
2738
		hdsp->control_register |= HDSP_LineOut;
T
Takashi Iwai 已提交
2739
	else
L
Linus Torvalds 已提交
2740 2741 2742 2743 2744
		hdsp->control_register &= ~HDSP_LineOut;
	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
	return 0;
}

2745
#define snd_hdsp_info_line_out		snd_ctl_boolean_mono_info
L
Linus Torvalds 已提交
2746

2747
static int snd_hdsp_get_line_out(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
2748
{
2749
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
T
Tim Blechmann 已提交
2750

L
Linus Torvalds 已提交
2751 2752 2753 2754 2755 2756
	spin_lock_irq(&hdsp->lock);
	ucontrol->value.integer.value[0] = hdsp_line_out(hdsp);
	spin_unlock_irq(&hdsp->lock);
	return 0;
}

2757
static int snd_hdsp_put_line_out(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
2758
{
2759
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
2760 2761
	int change;
	unsigned int val;
T
Tim Blechmann 已提交
2762

L
Linus Torvalds 已提交
2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773
	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_line_out(hdsp);
	hdsp_set_line_output(hdsp, val);
	spin_unlock_irq(&hdsp->lock);
	return change;
}

#define HDSP_PRECISE_POINTER(xname, xindex) \
2774
{ .iface = SNDRV_CTL_ELEM_IFACE_CARD, \
L
Linus Torvalds 已提交
2775 2776 2777 2778 2779 2780 2781
  .name = xname, \
  .index = xindex, \
  .info = snd_hdsp_info_precise_pointer, \
  .get = snd_hdsp_get_precise_pointer, \
  .put = snd_hdsp_put_precise_pointer \
}

2782
static int hdsp_set_precise_pointer(struct hdsp *hdsp, int precise)
L
Linus Torvalds 已提交
2783
{
T
Takashi Iwai 已提交
2784
	if (precise)
L
Linus Torvalds 已提交
2785
		hdsp->precise_ptr = 1;
T
Takashi Iwai 已提交
2786
	else
L
Linus Torvalds 已提交
2787 2788 2789 2790
		hdsp->precise_ptr = 0;
	return 0;
}

2791
#define snd_hdsp_info_precise_pointer		snd_ctl_boolean_mono_info
L
Linus Torvalds 已提交
2792

2793
static int snd_hdsp_get_precise_pointer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
2794
{
2795
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
T
Tim Blechmann 已提交
2796

L
Linus Torvalds 已提交
2797 2798 2799 2800 2801 2802
	spin_lock_irq(&hdsp->lock);
	ucontrol->value.integer.value[0] = hdsp->precise_ptr;
	spin_unlock_irq(&hdsp->lock);
	return 0;
}

2803
static int snd_hdsp_put_precise_pointer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
2804
{
2805
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
2806 2807
	int change;
	unsigned int val;
T
Tim Blechmann 已提交
2808

L
Linus Torvalds 已提交
2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819
	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) \
2820
{ .iface = SNDRV_CTL_ELEM_IFACE_CARD, \
L
Linus Torvalds 已提交
2821 2822 2823 2824 2825 2826 2827
  .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 \
}

2828
static int hdsp_set_use_midi_tasklet(struct hdsp *hdsp, int use_tasklet)
L
Linus Torvalds 已提交
2829
{
T
Takashi Iwai 已提交
2830
	if (use_tasklet)
L
Linus Torvalds 已提交
2831
		hdsp->use_midi_tasklet = 1;
T
Takashi Iwai 已提交
2832
	else
L
Linus Torvalds 已提交
2833 2834 2835 2836
		hdsp->use_midi_tasklet = 0;
	return 0;
}

2837
#define snd_hdsp_info_use_midi_tasklet		snd_ctl_boolean_mono_info
L
Linus Torvalds 已提交
2838

2839
static int snd_hdsp_get_use_midi_tasklet(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
2840
{
2841
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
T
Tim Blechmann 已提交
2842

L
Linus Torvalds 已提交
2843 2844 2845 2846 2847 2848
	spin_lock_irq(&hdsp->lock);
	ucontrol->value.integer.value[0] = hdsp->use_midi_tasklet;
	spin_unlock_irq(&hdsp->lock);
	return 0;
}

2849
static int snd_hdsp_put_use_midi_tasklet(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
2850
{
2851
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
2852 2853
	int change;
	unsigned int val;
T
Tim Blechmann 已提交
2854

L
Linus Torvalds 已提交
2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868
	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, \
2869
  .device = 0, \
L
Linus Torvalds 已提交
2870 2871 2872 2873 2874 2875 2876
  .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 \
}

2877
static int snd_hdsp_info_mixer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
L
Linus Torvalds 已提交
2878 2879 2880 2881 2882 2883 2884 2885 2886
{
	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;
}

2887
static int snd_hdsp_get_mixer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
2888
{
2889
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
2890 2891 2892 2893 2894 2895
	int source;
	int destination;
	int addr;

	source = ucontrol->value.integer.value[0];
	destination = ucontrol->value.integer.value[1];
T
Tim Blechmann 已提交
2896

T
Takashi Iwai 已提交
2897
	if (source >= hdsp->max_channels)
L
Linus Torvalds 已提交
2898
		addr = hdsp_playback_to_output_key(hdsp,source-hdsp->max_channels,destination);
T
Takashi Iwai 已提交
2899
	else
L
Linus Torvalds 已提交
2900
		addr = hdsp_input_to_output_key(hdsp,source, destination);
T
Tim Blechmann 已提交
2901

L
Linus Torvalds 已提交
2902 2903 2904 2905 2906 2907
	spin_lock_irq(&hdsp->lock);
	ucontrol->value.integer.value[2] = hdsp_read_gain (hdsp, addr);
	spin_unlock_irq(&hdsp->lock);
	return 0;
}

2908
static int snd_hdsp_put_mixer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
2909
{
2910
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922
	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];

T
Takashi Iwai 已提交
2923
	if (source >= hdsp->max_channels)
L
Linus Torvalds 已提交
2924
		addr = hdsp_playback_to_output_key(hdsp,source-hdsp->max_channels, destination);
T
Takashi Iwai 已提交
2925
	else
L
Linus Torvalds 已提交
2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938
		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) \
2939
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
L
Linus Torvalds 已提交
2940 2941 2942 2943 2944 2945 2946
  .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 \
}

2947
static int snd_hdsp_info_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
L
Linus Torvalds 已提交
2948
{
T
Tim Blechmann 已提交
2949
	static char *texts[] = {"No Lock", "Lock", "Sync" };
L
Linus Torvalds 已提交
2950 2951 2952 2953 2954 2955 2956 2957 2958
	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
	uinfo->count = 1;
	uinfo->value.enumerated.items = 3;
	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
	return 0;
}

2959
static int hdsp_wc_sync_check(struct hdsp *hdsp)
L
Linus Torvalds 已提交
2960 2961 2962
{
	int status2 = hdsp_read(hdsp, HDSP_status2Register);
	if (status2 & HDSP_wc_lock) {
T
Takashi Iwai 已提交
2963
		if (status2 & HDSP_wc_sync)
L
Linus Torvalds 已提交
2964
			return 2;
T
Takashi Iwai 已提交
2965
		else
L
Linus Torvalds 已提交
2966
			 return 1;
T
Takashi Iwai 已提交
2967
	} else
L
Linus Torvalds 已提交
2968 2969 2970 2971
		return 0;
	return 0;
}

2972
static int snd_hdsp_get_wc_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
2973
{
2974
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
2975 2976 2977 2978 2979 2980

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

#define HDSP_SPDIF_SYNC_CHECK(xname, xindex) \
2981
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
L
Linus Torvalds 已提交
2982 2983 2984 2985 2986 2987 2988
  .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 \
}

2989
static int hdsp_spdif_sync_check(struct hdsp *hdsp)
L
Linus Torvalds 已提交
2990 2991
{
	int status = hdsp_read(hdsp, HDSP_statusRegister);
T
Takashi Iwai 已提交
2992
	if (status & HDSP_SPDIFErrorFlag)
L
Linus Torvalds 已提交
2993
		return 0;
T
Tim Blechmann 已提交
2994
	else {
T
Takashi Iwai 已提交
2995
		if (status & HDSP_SPDIFSync)
L
Linus Torvalds 已提交
2996
			return 2;
T
Takashi Iwai 已提交
2997
		else
L
Linus Torvalds 已提交
2998 2999 3000 3001 3002
			return 1;
	}
	return 0;
}

3003
static int snd_hdsp_get_spdif_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
3004
{
3005
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
3006 3007 3008 3009 3010 3011

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

#define HDSP_ADATSYNC_SYNC_CHECK(xname, xindex) \
3012
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
L
Linus Torvalds 已提交
3013 3014 3015 3016 3017 3018 3019
  .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 \
}

3020
static int hdsp_adatsync_sync_check(struct hdsp *hdsp)
L
Linus Torvalds 已提交
3021 3022 3023
{
	int status = hdsp_read(hdsp, HDSP_statusRegister);
	if (status & HDSP_TimecodeLock) {
T
Takashi Iwai 已提交
3024
		if (status & HDSP_TimecodeSync)
L
Linus Torvalds 已提交
3025
			return 2;
T
Takashi Iwai 已提交
3026
		else
L
Linus Torvalds 已提交
3027
			return 1;
T
Takashi Iwai 已提交
3028
	} else
L
Linus Torvalds 已提交
3029
		return 0;
T
Tim Blechmann 已提交
3030
}
L
Linus Torvalds 已提交
3031

3032
static int snd_hdsp_get_adatsync_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
3033
{
3034
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
3035 3036 3037 3038 3039 3040

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

#define HDSP_ADAT_SYNC_CHECK \
3041
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
L
Linus Torvalds 已提交
3042 3043 3044 3045 3046
  .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
  .info = snd_hdsp_info_sync_check, \
  .get = snd_hdsp_get_adat_sync_check \
}

3047
static int hdsp_adat_sync_check(struct hdsp *hdsp, int idx)
T
Tim Blechmann 已提交
3048
{
L
Linus Torvalds 已提交
3049
	int status = hdsp_read(hdsp, HDSP_statusRegister);
T
Tim Blechmann 已提交
3050

L
Linus Torvalds 已提交
3051
	if (status & (HDSP_Lock0>>idx)) {
T
Takashi Iwai 已提交
3052
		if (status & (HDSP_Sync0>>idx))
L
Linus Torvalds 已提交
3053
			return 2;
T
Takashi Iwai 已提交
3054
		else
T
Tim Blechmann 已提交
3055
			return 1;
T
Takashi Iwai 已提交
3056
	} else
L
Linus Torvalds 已提交
3057
		return 0;
T
Tim Blechmann 已提交
3058
}
L
Linus Torvalds 已提交
3059

3060
static int snd_hdsp_get_adat_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
3061 3062
{
	int offset;
3063
	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
3064 3065

	offset = ucontrol->id.index - 1;
3066
	snd_BUG_ON(offset < 0);
L
Linus Torvalds 已提交
3067 3068 3069 3070 3071 3072 3073 3074 3075

	switch (hdsp->io_type) {
	case Digiface:
	case H9652:
		if (offset >= 3)
			return -EINVAL;
		break;
	case Multiface:
	case H9632:
T
Tim Blechmann 已提交
3076
		if (offset >= 1)
L
Linus Torvalds 已提交
3077 3078 3079 3080 3081 3082 3083 3084 3085 3086
			return -EINVAL;
		break;
	default:
		return -EIO;
	}

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

3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101
#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;

T
Takashi Iwai 已提交
3102 3103 3104
	if (!dds_value)
		return 0;

3105 3106 3107 3108 3109
	n = DDS_NUMERATOR;
	/*
	 * dds_value = n / rate
	 * rate = n / dds_value
	 */
3110
	n = div_u64(n, dds_value);
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
	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);
T
Tim Blechmann 已提交
3137

3138 3139 3140 3141 3142 3143 3144 3145 3146
	ucontrol->value.enumerated.item[0] = hdsp_dds_offset(hdsp);
	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;
T
Tim Blechmann 已提交
3147

3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159
	if (!snd_hdsp_use_is_exclusive(hdsp))
		return -EBUSY;
	val = ucontrol->value.enumerated.item[0];
	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;
}

3160
static struct snd_kcontrol_new snd_hdsp_9632_controls[] = {
L
Linus Torvalds 已提交
3161 3162 3163
HDSP_DA_GAIN("DA Gain", 0),
HDSP_AD_GAIN("AD Gain", 0),
HDSP_PHONE_GAIN("Phones Gain", 0),
3164 3165
HDSP_XLR_BREAKOUT_CABLE("XLR Breakout Cable", 0),
HDSP_DDS_OFFSET("DDS Sample Rate Offset", 0)
L
Linus Torvalds 已提交
3166 3167
};

3168
static struct snd_kcontrol_new snd_hdsp_controls[] = {
L
Linus Torvalds 已提交
3169
{
3170
	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
L
Linus Torvalds 已提交
3171 3172 3173 3174 3175 3176 3177
	.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,
3178
	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
L
Linus Torvalds 已提交
3179 3180 3181 3182 3183 3184 3185
	.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,
3186
	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
L
Linus Torvalds 已提交
3187 3188 3189 3190 3191
	.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 |
T
Tim Blechmann 已提交
3192
			 IEC958_AES0_CON_EMPHASIS,
L
Linus Torvalds 已提交
3193 3194 3195
},
{
	.access =	SNDRV_CTL_ELEM_ACCESS_READ,
3196
	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
L
Linus Torvalds 已提交
3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209
	.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),
HDSP_SPDIF_OUT("IEC958 Output also on ADAT1", 0),
HDSP_SPDIF_PROFESSIONAL("IEC958 Professional Bit", 0),
HDSP_SPDIF_EMPHASIS("IEC958 Emphasis Bit", 0),
HDSP_SPDIF_NON_AUDIO("IEC958 Non-audio Bit", 0),
T
Tim Blechmann 已提交
3210
/* 'Sample Clock Source' complies with the alsa control naming scheme */
L
Linus Torvalds 已提交
3211
HDSP_CLOCK_SOURCE("Sample Clock Source", 0),
3212 3213 3214 3215 3216 3217 3218
{
	.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,
},
L
Linus Torvalds 已提交
3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233
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),
HDSP_LINE_OUT("Line Out", 0),
HDSP_PRECISE_POINTER("Precise Pointer", 0),
HDSP_USE_MIDI_TASKLET("Use Midi Tasklet", 0),
};

3234 3235
static struct snd_kcontrol_new snd_hdsp_96xx_aeb = HDSP_AEB("Analog Extension Board", 0);
static struct snd_kcontrol_new snd_hdsp_adat_sync_check = HDSP_ADAT_SYNC_CHECK;
L
Linus Torvalds 已提交
3236

3237
static int snd_hdsp_create_controls(struct snd_card *card, struct hdsp *hdsp)
L
Linus Torvalds 已提交
3238 3239 3240
{
	unsigned int idx;
	int err;
3241
	struct snd_kcontrol *kctl;
L
Linus Torvalds 已提交
3242 3243

	for (idx = 0; idx < ARRAY_SIZE(snd_hdsp_controls); idx++) {
T
Takashi Iwai 已提交
3244
		if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_controls[idx], hdsp))) < 0)
L
Linus Torvalds 已提交
3245 3246 3247 3248 3249 3250 3251 3252
			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;
T
Takashi Iwai 已提交
3253
	if ((err = snd_ctl_add (card, kctl = snd_ctl_new1(&snd_hdsp_adat_sync_check, hdsp))))
L
Linus Torvalds 已提交
3254 3255 3256 3257
		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;
T
Takashi Iwai 已提交
3258
			if ((err = snd_ctl_add (card, kctl = snd_ctl_new1(&snd_hdsp_adat_sync_check, hdsp))))
L
Linus Torvalds 已提交
3259 3260 3261
				return err;
		}
	}
T
Tim Blechmann 已提交
3262

L
Linus Torvalds 已提交
3263 3264 3265
	/* 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++) {
T
Takashi Iwai 已提交
3266
			if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_9632_controls[idx], hdsp))) < 0)
L
Linus Torvalds 已提交
3267 3268 3269 3270 3271 3272
				return err;
		}
	}

	/* AEB control for H96xx card */
	if (hdsp->io_type == H9632 || hdsp->io_type == H9652) {
T
Takashi Iwai 已提交
3273
		if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_96xx_aeb, hdsp))) < 0)
L
Linus Torvalds 已提交
3274 3275 3276 3277 3278 3279 3280
				return err;
	}

	return 0;
}

/*------------------------------------------------------------
T
Tim Blechmann 已提交
3281
   /proc interface
L
Linus Torvalds 已提交
3282 3283 3284
 ------------------------------------------------------------*/

static void
3285
snd_hdsp_proc_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
L
Linus Torvalds 已提交
3286
{
3287
	struct hdsp *hdsp = (struct hdsp *) entry->private_data;
L
Linus Torvalds 已提交
3288 3289 3290 3291 3292 3293 3294 3295
	unsigned int status;
	unsigned int status2;
	char *pref_sync_ref;
	char *autosync_ref;
	char *system_clock_mode;
	char *clock_source;
	int x;

3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313
	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");
L
Linus Torvalds 已提交
3314
		return;
3315
	}
L
Linus Torvalds 已提交
3316

T
Takashi Iwai 已提交
3317
	if (hdsp_check_for_firmware(hdsp, 0)) {
L
Linus Torvalds 已提交
3318 3319
		if (hdsp->state & HDSP_FirmwareCached) {
			if (snd_hdsp_load_firmware_from_cache(hdsp) != 0) {
3320 3321 3322
				snd_iprintf(buffer, "Firmware loading from "
					    "cache failed, "
					    "please upload manually.\n");
L
Linus Torvalds 已提交
3323 3324 3325
				return;
			}
		} else {
3326 3327 3328 3329 3330 3331 3332 3333 3334 3335
			int err = -EINVAL;
#ifdef HDSP_FW_LOADER
			err = hdsp_request_fw_loader(hdsp);
#endif
			if (err < 0) {
				snd_iprintf(buffer,
					    "No firmware loaded nor cached, "
					    "please upload firmware.\n");
				return;
			}
L
Linus Torvalds 已提交
3336 3337
		}
	}
T
Tim Blechmann 已提交
3338

L
Linus Torvalds 已提交
3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388
	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";
T
Tim Blechmann 已提交
3389
		break;
L
Linus Torvalds 已提交
3390
	default:
T
Tim Blechmann 已提交
3391
		clock_source = "Error";
L
Linus Torvalds 已提交
3392 3393
	}
	snd_iprintf (buffer, "Sample Clock Source: %s\n", clock_source);
T
Tim Blechmann 已提交
3394

T
Takashi Iwai 已提交
3395
	if (hdsp_system_clock_mode(hdsp))
L
Linus Torvalds 已提交
3396
		system_clock_mode = "Slave";
T
Takashi Iwai 已提交
3397
	else
L
Linus Torvalds 已提交
3398
		system_clock_mode = "Master";
T
Tim Blechmann 已提交
3399

L
Linus Torvalds 已提交
3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423
	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);
T
Tim Blechmann 已提交
3424

L
Linus Torvalds 已提交
3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436
	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";
T
Tim Blechmann 已提交
3437
		break;
L
Linus Torvalds 已提交
3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451
	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);
T
Tim Blechmann 已提交
3452

L
Linus Torvalds 已提交
3453
	snd_iprintf (buffer, "AutoSync Frequency: %d\n", hdsp_external_sample_rate(hdsp));
T
Tim Blechmann 已提交
3454

L
Linus Torvalds 已提交
3455 3456 3457
	snd_iprintf (buffer, "System Clock Mode: %s\n", system_clock_mode);

	snd_iprintf (buffer, "System Clock Frequency: %d\n", hdsp->system_sample_rate);
3458
	snd_iprintf (buffer, "System Clock Locked: %s\n", hdsp->clock_source_locked ? "Yes" : "No");
T
Tim Blechmann 已提交
3459

L
Linus Torvalds 已提交
3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478
	snd_iprintf(buffer, "\n");

	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;
	}
T
Tim Blechmann 已提交
3479

T
Takashi Iwai 已提交
3480
	if (hdsp->control_register & HDSP_SPDIFOpticalOut)
L
Linus Torvalds 已提交
3481
		snd_iprintf(buffer, "IEC958 output: Coaxial & ADAT1\n");
T
Takashi Iwai 已提交
3482
	else
L
Linus Torvalds 已提交
3483 3484
		snd_iprintf(buffer, "IEC958 output: Coaxial only\n");

T
Takashi Iwai 已提交
3485
	if (hdsp->control_register & HDSP_SPDIFProfessional)
L
Linus Torvalds 已提交
3486
		snd_iprintf(buffer, "IEC958 quality: Professional\n");
T
Takashi Iwai 已提交
3487
	else
L
Linus Torvalds 已提交
3488 3489
		snd_iprintf(buffer, "IEC958 quality: Consumer\n");

T
Takashi Iwai 已提交
3490
	if (hdsp->control_register & HDSP_SPDIFEmphasis)
L
Linus Torvalds 已提交
3491
		snd_iprintf(buffer, "IEC958 emphasis: on\n");
T
Takashi Iwai 已提交
3492
	else
L
Linus Torvalds 已提交
3493 3494
		snd_iprintf(buffer, "IEC958 emphasis: off\n");

T
Takashi Iwai 已提交
3495
	if (hdsp->control_register & HDSP_SPDIFNonAudio)
L
Linus Torvalds 已提交
3496
		snd_iprintf(buffer, "IEC958 NonAudio: on\n");
T
Takashi Iwai 已提交
3497
	else
L
Linus Torvalds 已提交
3498
		snd_iprintf(buffer, "IEC958 NonAudio: off\n");
T
Takashi Iwai 已提交
3499
	if ((x = hdsp_spdif_sample_rate (hdsp)) != 0)
L
Linus Torvalds 已提交
3500
		snd_iprintf (buffer, "IEC958 sample rate: %d\n", x);
T
Takashi Iwai 已提交
3501
	else
L
Linus Torvalds 已提交
3502 3503 3504 3505 3506 3507
		snd_iprintf (buffer, "IEC958 sample rate: Error flag set\n");

	snd_iprintf(buffer, "\n");

	/* Sync Check */
	x = status & HDSP_Sync0;
T
Takashi Iwai 已提交
3508
	if (status & HDSP_Lock0)
L
Linus Torvalds 已提交
3509
		snd_iprintf(buffer, "ADAT1: %s\n", x ? "Sync" : "Lock");
T
Takashi Iwai 已提交
3510
	else
L
Linus Torvalds 已提交
3511 3512 3513 3514 3515 3516
		snd_iprintf(buffer, "ADAT1: No Lock\n");

	switch (hdsp->io_type) {
	case Digiface:
	case H9652:
		x = status & HDSP_Sync1;
T
Takashi Iwai 已提交
3517
		if (status & HDSP_Lock1)
L
Linus Torvalds 已提交
3518
			snd_iprintf(buffer, "ADAT2: %s\n", x ? "Sync" : "Lock");
T
Takashi Iwai 已提交
3519
		else
L
Linus Torvalds 已提交
3520 3521
			snd_iprintf(buffer, "ADAT2: No Lock\n");
		x = status & HDSP_Sync2;
T
Takashi Iwai 已提交
3522
		if (status & HDSP_Lock2)
L
Linus Torvalds 已提交
3523
			snd_iprintf(buffer, "ADAT3: %s\n", x ? "Sync" : "Lock");
T
Takashi Iwai 已提交
3524
		else
L
Linus Torvalds 已提交
3525
			snd_iprintf(buffer, "ADAT3: No Lock\n");
T
Takashi Iwai 已提交
3526
		break;
L
Linus Torvalds 已提交
3527 3528 3529 3530 3531 3532
	default:
		/* relax */
		break;
	}

	x = status & HDSP_SPDIFSync;
T
Takashi Iwai 已提交
3533
	if (status & HDSP_SPDIFErrorFlag)
L
Linus Torvalds 已提交
3534
		snd_iprintf (buffer, "SPDIF: No Lock\n");
T
Takashi Iwai 已提交
3535
	else
L
Linus Torvalds 已提交
3536
		snd_iprintf (buffer, "SPDIF: %s\n", x ? "Sync" : "Lock");
T
Tim Blechmann 已提交
3537

L
Linus Torvalds 已提交
3538
	x = status2 & HDSP_wc_sync;
T
Takashi Iwai 已提交
3539
	if (status2 & HDSP_wc_lock)
L
Linus Torvalds 已提交
3540
		snd_iprintf (buffer, "Word Clock: %s\n", x ? "Sync" : "Lock");
T
Takashi Iwai 已提交
3541
	else
L
Linus Torvalds 已提交
3542
		snd_iprintf (buffer, "Word Clock: No Lock\n");
T
Tim Blechmann 已提交
3543

L
Linus Torvalds 已提交
3544
	x = status & HDSP_TimecodeSync;
T
Takashi Iwai 已提交
3545
	if (status & HDSP_TimecodeLock)
L
Linus Torvalds 已提交
3546
		snd_iprintf(buffer, "ADAT Sync: %s\n", x ? "Sync" : "Lock");
T
Takashi Iwai 已提交
3547
	else
L
Linus Torvalds 已提交
3548 3549 3550
		snd_iprintf(buffer, "ADAT Sync: No Lock\n");

	snd_iprintf(buffer, "\n");
T
Tim Blechmann 已提交
3551

L
Linus Torvalds 已提交
3552 3553 3554
	/* Informations about H9632 specific controls */
	if (hdsp->io_type == H9632) {
		char *tmp;
T
Tim Blechmann 已提交
3555

L
Linus Torvalds 已提交
3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580
		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);
T
Tim Blechmann 已提交
3581

L
Linus Torvalds 已提交
3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594
		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);

T
Tim Blechmann 已提交
3595 3596
		snd_iprintf(buffer, "XLR Breakout Cable : %s\n", hdsp_xlr_breakout_cable(hdsp) ? "yes" : "no");

T
Takashi Iwai 已提交
3597
		if (hdsp->control_register & HDSP_AnalogExtensionBoard)
L
Linus Torvalds 已提交
3598
			snd_iprintf(buffer, "AEB : on (ADAT1 internal)\n");
T
Takashi Iwai 已提交
3599
		else
L
Linus Torvalds 已提交
3600 3601 3602 3603 3604 3605
			snd_iprintf(buffer, "AEB : off (ADAT1 external)\n");
		snd_iprintf(buffer, "\n");
	}

}

3606
static void snd_hdsp_proc_init(struct hdsp *hdsp)
L
Linus Torvalds 已提交
3607
{
3608
	struct snd_info_entry *entry;
L
Linus Torvalds 已提交
3609 3610

	if (! snd_card_proc_new(hdsp->card, "hdsp", &entry))
3611
		snd_info_set_text_ops(entry, hdsp, snd_hdsp_proc_read);
L
Linus Torvalds 已提交
3612 3613
}

3614
static void snd_hdsp_free_buffers(struct hdsp *hdsp)
L
Linus Torvalds 已提交
3615 3616 3617 3618 3619
{
	snd_hammerfall_free_buffer(&hdsp->capture_dma_buf, hdsp->pci);
	snd_hammerfall_free_buffer(&hdsp->playback_dma_buf, hdsp->pci);
}

3620
static int __devinit snd_hdsp_initialize_memory(struct hdsp *hdsp)
L
Linus Torvalds 已提交
3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633
{
	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);
		printk(KERN_ERR "%s: no buffers available\n", hdsp->card_name);
		return -ENOMEM;
	}

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

C
Clemens Ladisch 已提交
3634 3635
	cb_bus = ALIGN(hdsp->capture_dma_buf.addr, 0x10000ul);
	pb_bus = ALIGN(hdsp->playback_dma_buf.addr, 0x10000ul);
L
Linus Torvalds 已提交
3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647

	/* 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;
}

3648
static int snd_hdsp_set_defaults(struct hdsp *hdsp)
L
Linus Torvalds 已提交
3649 3650 3651 3652 3653
{
	unsigned int i;

	/* ASSUMPTION: hdsp->lock is either held, or
	   there is no need to hold it (e.g. during module
J
Joe Perches 已提交
3654
	   initialization).
L
Linus Torvalds 已提交
3655 3656 3657 3658
	 */

	/* set defaults:

T
Tim Blechmann 已提交
3659
	   SPDIF Input via Coax
L
Linus Torvalds 已提交
3660 3661 3662
	   Master clock mode
	   maximum latency (7 => 2^7 = 8192 samples, 64Kbyte buffer,
	                    which implies 2 4096 sample, 32Kbyte periods).
T
Tim Blechmann 已提交
3663
           Enable line out.
L
Linus Torvalds 已提交
3664 3665
	 */

T
Tim Blechmann 已提交
3666 3667 3668
	hdsp->control_register = HDSP_ClockModeMaster |
		                 HDSP_SPDIFInputCoaxial |
		                 hdsp_encode_latency(7) |
L
Linus Torvalds 已提交
3669
		                 HDSP_LineOut;
T
Tim Blechmann 已提交
3670

L
Linus Torvalds 已提交
3671 3672 3673 3674 3675 3676 3677 3678

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

#ifdef SNDRV_BIG_ENDIAN
	hdsp->control2_register = HDSP_BIGENDIAN_MODE;
#else
	hdsp->control2_register = 0;
#endif
T
Takashi Iwai 已提交
3679
	if (hdsp->io_type == H9652)
L
Linus Torvalds 已提交
3680
	        snd_hdsp_9652_enable_mixer (hdsp);
T
Takashi Iwai 已提交
3681 3682
	else
		hdsp_write (hdsp, HDSP_control2Reg, hdsp->control2_register);
L
Linus Torvalds 已提交
3683 3684 3685 3686 3687

	hdsp_reset_hw_pointer(hdsp);
	hdsp_compute_period_size(hdsp);

	/* silence everything */
T
Tim Blechmann 已提交
3688

T
Takashi Iwai 已提交
3689
	for (i = 0; i < HDSP_MATRIX_MIXER_SIZE; ++i)
L
Linus Torvalds 已提交
3690 3691 3692
		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) {
T
Takashi Iwai 已提交
3693
		if (hdsp_write_gain (hdsp, i, MINUS_INFINITY_GAIN))
L
Linus Torvalds 已提交
3694 3695
			return -EIO;
	}
T
Tim Blechmann 已提交
3696

L
Linus Torvalds 已提交
3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712
	/* 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)
{
3713
	struct hdsp *hdsp = (struct hdsp *)arg;
T
Tim Blechmann 已提交
3714

T
Takashi Iwai 已提交
3715
	if (hdsp->midi[0].pending)
L
Linus Torvalds 已提交
3716
		snd_hdsp_midi_input_read (&hdsp->midi[0]);
T
Takashi Iwai 已提交
3717
	if (hdsp->midi[1].pending)
L
Linus Torvalds 已提交
3718
		snd_hdsp_midi_input_read (&hdsp->midi[1]);
T
Tim Blechmann 已提交
3719
}
L
Linus Torvalds 已提交
3720

3721
static irqreturn_t snd_hdsp_interrupt(int irq, void *dev_id)
L
Linus Torvalds 已提交
3722
{
3723
	struct hdsp *hdsp = (struct hdsp *) dev_id;
L
Linus Torvalds 已提交
3724 3725 3726 3727 3728 3729 3730
	unsigned int status;
	int audio;
	int midi0;
	int midi1;
	unsigned int midi0status;
	unsigned int midi1status;
	int schedule = 0;
T
Tim Blechmann 已提交
3731

L
Linus Torvalds 已提交
3732 3733 3734 3735 3736 3737
	status = hdsp_read(hdsp, HDSP_statusRegister);

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

T
Takashi Iwai 已提交
3738
	if (!audio && !midi0 && !midi1)
L
Linus Torvalds 已提交
3739 3740 3741 3742 3743 3744
		return IRQ_NONE;

	hdsp_write(hdsp, HDSP_interruptConfirmation, 0);

	midi0status = hdsp_read (hdsp, HDSP_midiStatusIn0) & 0xff;
	midi1status = hdsp_read (hdsp, HDSP_midiStatusIn1) & 0xff;
T
Tim Blechmann 已提交
3745

3746 3747 3748
	if (!(hdsp->state & HDSP_InitializationComplete))
		return IRQ_HANDLED;

L
Linus Torvalds 已提交
3749
	if (audio) {
T
Takashi Iwai 已提交
3750
		if (hdsp->capture_substream)
L
Linus Torvalds 已提交
3751
			snd_pcm_period_elapsed(hdsp->pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream);
T
Tim Blechmann 已提交
3752

T
Takashi Iwai 已提交
3753
		if (hdsp->playback_substream)
L
Linus Torvalds 已提交
3754 3755
			snd_pcm_period_elapsed(hdsp->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream);
	}
T
Tim Blechmann 已提交
3756

L
Linus Torvalds 已提交
3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779
	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]);
		}
	}
	if (hdsp->io_type != Multiface && hdsp->io_type != H9632 && midi1 && midi1status) {
		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)
3780
		tasklet_schedule(&hdsp->midi_tasklet);
L
Linus Torvalds 已提交
3781 3782 3783
	return IRQ_HANDLED;
}

3784
static snd_pcm_uframes_t snd_hdsp_hw_pointer(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
3785
{
3786
	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
L
Linus Torvalds 已提交
3787 3788 3789
	return hdsp_hw_pointer(hdsp);
}

3790
static char *hdsp_channel_buffer_location(struct hdsp *hdsp,
L
Linus Torvalds 已提交
3791 3792 3793 3794 3795 3796
					     int stream,
					     int channel)

{
	int mapped_channel;

3797 3798
        if (snd_BUG_ON(channel < 0 || channel >= hdsp->max_channels))
		return NULL;
T
Tim Blechmann 已提交
3799

T
Takashi Iwai 已提交
3800
	if ((mapped_channel = hdsp->channel_map[channel]) < 0)
L
Linus Torvalds 已提交
3801
		return NULL;
T
Tim Blechmann 已提交
3802

T
Takashi Iwai 已提交
3803
	if (stream == SNDRV_PCM_STREAM_CAPTURE)
L
Linus Torvalds 已提交
3804
		return hdsp->capture_buffer + (mapped_channel * HDSP_CHANNEL_BUFFER_BYTES);
T
Takashi Iwai 已提交
3805
	else
L
Linus Torvalds 已提交
3806 3807 3808
		return hdsp->playback_buffer + (mapped_channel * HDSP_CHANNEL_BUFFER_BYTES);
}

3809
static int snd_hdsp_playback_copy(struct snd_pcm_substream *substream, int channel,
L
Linus Torvalds 已提交
3810 3811
				  snd_pcm_uframes_t pos, void __user *src, snd_pcm_uframes_t count)
{
3812
	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
L
Linus Torvalds 已提交
3813 3814
	char *channel_buf;

3815 3816
	if (snd_BUG_ON(pos + count > HDSP_CHANNEL_BUFFER_BYTES / 4))
		return -EINVAL;
L
Linus Torvalds 已提交
3817 3818

	channel_buf = hdsp_channel_buffer_location (hdsp, substream->pstr->stream, channel);
3819 3820
	if (snd_BUG_ON(!channel_buf))
		return -EIO;
L
Linus Torvalds 已提交
3821 3822 3823 3824 3825
	if (copy_from_user(channel_buf + pos * 4, src, count * 4))
		return -EFAULT;
	return count;
}

3826
static int snd_hdsp_capture_copy(struct snd_pcm_substream *substream, int channel,
L
Linus Torvalds 已提交
3827 3828
				 snd_pcm_uframes_t pos, void __user *dst, snd_pcm_uframes_t count)
{
3829
	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
L
Linus Torvalds 已提交
3830 3831
	char *channel_buf;

3832 3833
	if (snd_BUG_ON(pos + count > HDSP_CHANNEL_BUFFER_BYTES / 4))
		return -EINVAL;
L
Linus Torvalds 已提交
3834 3835

	channel_buf = hdsp_channel_buffer_location (hdsp, substream->pstr->stream, channel);
3836 3837
	if (snd_BUG_ON(!channel_buf))
		return -EIO;
L
Linus Torvalds 已提交
3838 3839 3840 3841 3842
	if (copy_to_user(dst, channel_buf + pos * 4, count * 4))
		return -EFAULT;
	return count;
}

3843
static int snd_hdsp_hw_silence(struct snd_pcm_substream *substream, int channel,
L
Linus Torvalds 已提交
3844 3845
				  snd_pcm_uframes_t pos, snd_pcm_uframes_t count)
{
3846
	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
L
Linus Torvalds 已提交
3847 3848 3849
	char *channel_buf;

	channel_buf = hdsp_channel_buffer_location (hdsp, substream->pstr->stream, channel);
3850 3851
	if (snd_BUG_ON(!channel_buf))
		return -EIO;
L
Linus Torvalds 已提交
3852 3853 3854 3855
	memset(channel_buf + pos * 4, 0, count * 4);
	return count;
}

3856
static int snd_hdsp_reset(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
3857
{
3858 3859 3860
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
	struct snd_pcm_substream *other;
L
Linus Torvalds 已提交
3861 3862 3863 3864 3865 3866 3867 3868 3869
	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) {
3870 3871
		struct snd_pcm_substream *s;
		struct snd_pcm_runtime *oruntime = other->runtime;
3872
		snd_pcm_group_for_each_entry(s, substream) {
L
Linus Torvalds 已提交
3873 3874 3875 3876 3877 3878 3879 3880 3881
			if (s == other) {
				oruntime->status->hw_ptr = runtime->status->hw_ptr;
				break;
			}
		}
	}
	return 0;
}

3882 3883
static int snd_hdsp_hw_params(struct snd_pcm_substream *substream,
				 struct snd_pcm_hw_params *params)
L
Linus Torvalds 已提交
3884
{
3885
	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
L
Linus Torvalds 已提交
3886 3887 3888 3889
	int err;
	pid_t this_pid;
	pid_t other_pid;

T
Takashi Iwai 已提交
3890
	if (hdsp_check_for_iobox (hdsp))
L
Linus Torvalds 已提交
3891 3892
		return -EIO;

T
Takashi Iwai 已提交
3893
	if (hdsp_check_for_firmware(hdsp, 1))
L
Linus Torvalds 已提交
3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939
		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);
3940 3941 3942 3943 3944 3945
	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;
		}
L
Linus Torvalds 已提交
3946
	}
3947
	spin_unlock_irq(&hdsp->lock);
L
Linus Torvalds 已提交
3948 3949 3950 3951 3952 3953 3954 3955 3956

	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;
}

3957 3958
static int snd_hdsp_channel_info(struct snd_pcm_substream *substream,
				    struct snd_pcm_channel_info *info)
L
Linus Torvalds 已提交
3959
{
3960
	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
L
Linus Torvalds 已提交
3961 3962
	int mapped_channel;

3963 3964
	if (snd_BUG_ON(info->channel >= hdsp->max_channels))
		return -EINVAL;
L
Linus Torvalds 已提交
3965

T
Takashi Iwai 已提交
3966
	if ((mapped_channel = hdsp->channel_map[info->channel]) < 0)
L
Linus Torvalds 已提交
3967 3968 3969 3970 3971 3972 3973 3974
		return -EINVAL;

	info->offset = mapped_channel * HDSP_CHANNEL_BUFFER_BYTES;
	info->first = 0;
	info->step = 32;
	return 0;
}

3975
static int snd_hdsp_ioctl(struct snd_pcm_substream *substream,
L
Linus Torvalds 已提交
3976 3977 3978 3979 3980 3981
			     unsigned int cmd, void *arg)
{
	switch (cmd) {
	case SNDRV_PCM_IOCTL1_RESET:
		return snd_hdsp_reset(substream);
	case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
T
Takashi Iwai 已提交
3982
		return snd_hdsp_channel_info(substream, arg);
L
Linus Torvalds 已提交
3983 3984 3985 3986 3987 3988 3989
	default:
		break;
	}

	return snd_pcm_lib_ioctl(substream, cmd, arg);
}

3990
static int snd_hdsp_trigger(struct snd_pcm_substream *substream, int cmd)
L
Linus Torvalds 已提交
3991
{
3992 3993
	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
	struct snd_pcm_substream *other;
L
Linus Torvalds 已提交
3994
	int running;
T
Tim Blechmann 已提交
3995

T
Takashi Iwai 已提交
3996
	if (hdsp_check_for_iobox (hdsp))
L
Linus Torvalds 已提交
3997 3998
		return -EIO;

3999
	if (hdsp_check_for_firmware(hdsp, 0)) /* no auto-loading in trigger */
L
Linus Torvalds 已提交
4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021
		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) {
4022
		struct snd_pcm_substream *s;
4023
		snd_pcm_group_for_each_entry(s, substream) {
L
Linus Torvalds 已提交
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 4053 4054 4055 4056 4057
			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;
}

4058
static int snd_hdsp_prepare(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
4059
{
4060
	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
L
Linus Torvalds 已提交
4061 4062
	int result = 0;

T
Takashi Iwai 已提交
4063
	if (hdsp_check_for_iobox (hdsp))
L
Linus Torvalds 已提交
4064 4065
		return -EIO;

T
Takashi Iwai 已提交
4066
	if (hdsp_check_for_firmware(hdsp, 1))
L
Linus Torvalds 已提交
4067 4068 4069 4070 4071 4072 4073 4074 4075
		return -EIO;

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

4076
static struct snd_pcm_hardware snd_hdsp_playback_subinfo =
L
Linus Torvalds 已提交
4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088
{
	.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 |
T
Tim Blechmann 已提交
4089 4090 4091 4092
				 SNDRV_PCM_RATE_44100 |
				 SNDRV_PCM_RATE_48000 |
				 SNDRV_PCM_RATE_64000 |
				 SNDRV_PCM_RATE_88200 |
L
Linus Torvalds 已提交
4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105
				 SNDRV_PCM_RATE_96000),
	.rate_min =		32000,
	.rate_max =		96000,
	.channels_min =		14,
	.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
};

4106
static struct snd_pcm_hardware snd_hdsp_capture_subinfo =
L
Linus Torvalds 已提交
4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117
{
	.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 |
T
Tim Blechmann 已提交
4118 4119 4120 4121
				 SNDRV_PCM_RATE_44100 |
				 SNDRV_PCM_RATE_48000 |
				 SNDRV_PCM_RATE_64000 |
				 SNDRV_PCM_RATE_88200 |
L
Linus Torvalds 已提交
4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136
				 SNDRV_PCM_RATE_96000),
	.rate_min =		32000,
	.rate_max =		96000,
	.channels_min =		14,
	.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
};

static unsigned int hdsp_period_sizes[] = { 64, 128, 256, 512, 1024, 2048, 4096, 8192 };

4137
static struct snd_pcm_hw_constraint_list hdsp_hw_constraints_period_sizes = {
L
Linus Torvalds 已提交
4138 4139 4140 4141 4142 4143 4144
	.count = ARRAY_SIZE(hdsp_period_sizes),
	.list = hdsp_period_sizes,
	.mask = 0
};

static unsigned int hdsp_9632_sample_rates[] = { 32000, 44100, 48000, 64000, 88200, 96000, 128000, 176400, 192000 };

4145
static struct snd_pcm_hw_constraint_list hdsp_hw_constraints_9632_sample_rates = {
L
Linus Torvalds 已提交
4146 4147 4148 4149 4150
	.count = ARRAY_SIZE(hdsp_9632_sample_rates),
	.list = hdsp_9632_sample_rates,
	.mask = 0
};

4151 4152
static int snd_hdsp_hw_rule_in_channels(struct snd_pcm_hw_params *params,
					struct snd_pcm_hw_rule *rule)
L
Linus Torvalds 已提交
4153
{
4154 4155
	struct hdsp *hdsp = rule->private;
	struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
L
Linus Torvalds 已提交
4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169
	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);
	}
}

4170 4171
static int snd_hdsp_hw_rule_out_channels(struct snd_pcm_hw_params *params,
					struct snd_pcm_hw_rule *rule)
L
Linus Torvalds 已提交
4172 4173
{
	unsigned int list[3];
4174 4175
	struct hdsp *hdsp = rule->private;
	struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
L
Linus Torvalds 已提交
4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187
	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);
}

4188 4189
static int snd_hdsp_hw_rule_in_channels_rate(struct snd_pcm_hw_params *params,
					     struct snd_pcm_hw_rule *rule)
L
Linus Torvalds 已提交
4190
{
4191 4192 4193
	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);
L
Linus Torvalds 已提交
4194
	if (r->min > 96000 && hdsp->io_type == H9632) {
4195
		struct snd_interval t = {
L
Linus Torvalds 已提交
4196 4197 4198 4199
			.min = hdsp->qs_in_channels,
			.max = hdsp->qs_in_channels,
			.integer = 1,
		};
T
Tim Blechmann 已提交
4200
		return snd_interval_refine(c, &t);
L
Linus Torvalds 已提交
4201
	} else if (r->min > 48000 && r->max <= 96000) {
4202
		struct snd_interval t = {
L
Linus Torvalds 已提交
4203 4204 4205 4206 4207 4208
			.min = hdsp->ds_in_channels,
			.max = hdsp->ds_in_channels,
			.integer = 1,
		};
		return snd_interval_refine(c, &t);
	} else if (r->max < 64000) {
4209
		struct snd_interval t = {
L
Linus Torvalds 已提交
4210 4211 4212 4213 4214 4215 4216 4217 4218
			.min = hdsp->ss_in_channels,
			.max = hdsp->ss_in_channels,
			.integer = 1,
		};
		return snd_interval_refine(c, &t);
	}
	return 0;
}

4219 4220
static int snd_hdsp_hw_rule_out_channels_rate(struct snd_pcm_hw_params *params,
					     struct snd_pcm_hw_rule *rule)
L
Linus Torvalds 已提交
4221
{
4222 4223 4224
	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);
L
Linus Torvalds 已提交
4225
	if (r->min > 96000 && hdsp->io_type == H9632) {
4226
		struct snd_interval t = {
L
Linus Torvalds 已提交
4227 4228 4229 4230
			.min = hdsp->qs_out_channels,
			.max = hdsp->qs_out_channels,
			.integer = 1,
		};
T
Tim Blechmann 已提交
4231
		return snd_interval_refine(c, &t);
L
Linus Torvalds 已提交
4232
	} else if (r->min > 48000 && r->max <= 96000) {
4233
		struct snd_interval t = {
L
Linus Torvalds 已提交
4234 4235 4236 4237 4238 4239
			.min = hdsp->ds_out_channels,
			.max = hdsp->ds_out_channels,
			.integer = 1,
		};
		return snd_interval_refine(c, &t);
	} else if (r->max < 64000) {
4240
		struct snd_interval t = {
L
Linus Torvalds 已提交
4241 4242 4243 4244 4245 4246 4247 4248 4249
			.min = hdsp->ss_out_channels,
			.max = hdsp->ss_out_channels,
			.integer = 1,
		};
		return snd_interval_refine(c, &t);
	}
	return 0;
}

4250 4251
static int snd_hdsp_hw_rule_rate_out_channels(struct snd_pcm_hw_params *params,
					     struct snd_pcm_hw_rule *rule)
L
Linus Torvalds 已提交
4252
{
4253 4254 4255
	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);
L
Linus Torvalds 已提交
4256
	if (c->min >= hdsp->ss_out_channels) {
4257
		struct snd_interval t = {
L
Linus Torvalds 已提交
4258 4259 4260 4261 4262 4263
			.min = 32000,
			.max = 48000,
			.integer = 1,
		};
		return snd_interval_refine(r, &t);
	} else if (c->max <= hdsp->qs_out_channels && hdsp->io_type == H9632) {
4264
		struct snd_interval t = {
L
Linus Torvalds 已提交
4265 4266 4267 4268 4269 4270
			.min = 128000,
			.max = 192000,
			.integer = 1,
		};
		return snd_interval_refine(r, &t);
	} else if (c->max <= hdsp->ds_out_channels) {
4271
		struct snd_interval t = {
L
Linus Torvalds 已提交
4272 4273 4274 4275 4276 4277 4278 4279 4280
			.min = 64000,
			.max = 96000,
			.integer = 1,
		};
		return snd_interval_refine(r, &t);
	}
	return 0;
}

4281 4282
static int snd_hdsp_hw_rule_rate_in_channels(struct snd_pcm_hw_params *params,
					     struct snd_pcm_hw_rule *rule)
L
Linus Torvalds 已提交
4283
{
4284 4285 4286
	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);
L
Linus Torvalds 已提交
4287
	if (c->min >= hdsp->ss_in_channels) {
4288
		struct snd_interval t = {
L
Linus Torvalds 已提交
4289 4290 4291 4292 4293 4294
			.min = 32000,
			.max = 48000,
			.integer = 1,
		};
		return snd_interval_refine(r, &t);
	} else if (c->max <= hdsp->qs_in_channels && hdsp->io_type == H9632) {
4295
		struct snd_interval t = {
L
Linus Torvalds 已提交
4296 4297 4298 4299 4300 4301
			.min = 128000,
			.max = 192000,
			.integer = 1,
		};
		return snd_interval_refine(r, &t);
	} else if (c->max <= hdsp->ds_in_channels) {
4302
		struct snd_interval t = {
L
Linus Torvalds 已提交
4303 4304 4305 4306 4307 4308 4309 4310 4311
			.min = 64000,
			.max = 96000,
			.integer = 1,
		};
		return snd_interval_refine(r, &t);
	}
	return 0;
}

4312
static int snd_hdsp_playback_open(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
4313
{
4314 4315
	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
L
Linus Torvalds 已提交
4316

T
Takashi Iwai 已提交
4317
	if (hdsp_check_for_iobox (hdsp))
L
Linus Torvalds 已提交
4318 4319
		return -EIO;

T
Takashi Iwai 已提交
4320
	if (hdsp_check_for_firmware(hdsp, 1))
L
Linus Torvalds 已提交
4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337
		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);
4338 4339 4340
	if (hdsp->clock_source_locked) {
		runtime->hw.rate_min = runtime->hw.rate_max = hdsp->system_sample_rate;
	} else if (hdsp->io_type == H9632) {
L
Linus Torvalds 已提交
4341 4342 4343 4344
		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);
	}
4345 4346 4347
	if (hdsp->io_type == H9632) {
		runtime->hw.channels_min = hdsp->qs_out_channels;
		runtime->hw.channels_max = hdsp->ss_out_channels;
T
Tim Blechmann 已提交
4348 4349
	}

L
Linus Torvalds 已提交
4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366
	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);

	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);
	return 0;
}

4367
static int snd_hdsp_playback_release(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
4368
{
4369
	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
L
Linus Torvalds 已提交
4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384

	spin_lock_irq(&hdsp->lock);

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

	spin_unlock_irq(&hdsp->lock);

	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);
	return 0;
}


4385
static int snd_hdsp_capture_open(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
4386
{
4387 4388
	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
L
Linus Torvalds 已提交
4389

T
Takashi Iwai 已提交
4390
	if (hdsp_check_for_iobox (hdsp))
L
Linus Torvalds 已提交
4391 4392
		return -EIO;

T
Takashi Iwai 已提交
4393
	if (hdsp_check_for_firmware(hdsp, 1))
L
Linus Torvalds 已提交
4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429
		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;
}

4430
static int snd_hdsp_capture_release(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
4431
{
4432
	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
L
Linus Torvalds 已提交
4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469

	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);
}

4470
static int hdsp_9652_get_peak(struct hdsp *hdsp, struct hdsp_peak_rms __user *peak_rms)
L
Linus Torvalds 已提交
4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506
{
	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;
}

4507
static int hdsp_9632_get_peak(struct hdsp *hdsp, struct hdsp_peak_rms __user *peak_rms)
L
Linus Torvalds 已提交
4508 4509
{
	int i, j;
4510
	struct hdsp_9632_meters __iomem *m;
L
Linus Torvalds 已提交
4511 4512 4513 4514
	int doublespeed = 0;

	if (hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DoubleSpeedStatus)
		doublespeed = 1;
4515
	m = (struct hdsp_9632_meters __iomem *)(hdsp->iobase+HDSP_9632_metersBase);
L
Linus Torvalds 已提交
4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536
	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;
}

4537
static int hdsp_get_peak(struct hdsp *hdsp, struct hdsp_peak_rms __user *peak_rms)
L
Linus Torvalds 已提交
4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558
{
	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;
T
Tim Blechmann 已提交
4559
		if (copy_u64_le(&peak_rms->input_rms[i],
L
Linus Torvalds 已提交
4560 4561 4562 4563 4564 4565 4566
				hdsp->iobase + HDSP_inputRmsLevel + i * 8 + 4,
				hdsp->iobase + HDSP_inputRmsLevel + i * 8))
			return -EFAULT;
	}
	return 0;
}

4567
static int snd_hdsp_hwdep_ioctl(struct snd_hwdep *hw, struct file *file, unsigned int cmd, unsigned long arg)
L
Linus Torvalds 已提交
4568
{
T
Tim Blechmann 已提交
4569
	struct hdsp *hdsp = (struct hdsp *)hw->private_data;
L
Linus Torvalds 已提交
4570
	void __user *argp = (void __user *)arg;
4571
	int err;
L
Linus Torvalds 已提交
4572 4573 4574

	switch (cmd) {
	case SNDRV_HDSP_IOCTL_GET_PEAK_RMS: {
4575
		struct hdsp_peak_rms __user *peak_rms = (struct hdsp_peak_rms __user *)arg;
L
Linus Torvalds 已提交
4576

4577 4578 4579 4580 4581 4582 4583 4584
		err = hdsp_check_for_iobox(hdsp);
		if (err < 0)
			return err;

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

L
Linus Torvalds 已提交
4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599
		if (!(hdsp->state & HDSP_FirmwareLoaded)) {
			snd_printk(KERN_ERR "Hammerfall-DSP: firmware needs to be uploaded to the card.\n");
			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: {
4600
		struct hdsp_config_info info;
L
Linus Torvalds 已提交
4601 4602
		unsigned long flags;
		int i;
T
Tim Blechmann 已提交
4603

4604 4605 4606 4607 4608 4609 4610 4611
		err = hdsp_check_for_iobox(hdsp);
		if (err < 0)
			return err;

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

4612
		memset(&info, 0, sizeof(info));
L
Linus Torvalds 已提交
4613 4614 4615
		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);
T
Takashi Iwai 已提交
4616
		if (hdsp->io_type != H9632)
L
Linus Torvalds 已提交
4617 4618
		    info.adatsync_sync_check = (unsigned char)hdsp_adatsync_sync_check(hdsp);
		info.spdif_sync_check = (unsigned char)hdsp_spdif_sync_check(hdsp);
T
Takashi Iwai 已提交
4619
		for (i = 0; i < ((hdsp->io_type != Multiface && hdsp->io_type != H9632) ? 3 : 1); ++i)
L
Linus Torvalds 已提交
4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637
			info.adat_sync_check[i] = (unsigned char)hdsp_adat_sync_check(hdsp, i);
		info.spdif_in = (unsigned char)hdsp_spdif_in(hdsp);
		info.spdif_out = (unsigned char)hdsp_spdif_out(hdsp);
		info.spdif_professional = (unsigned char)hdsp_spdif_professional(hdsp);
		info.spdif_emphasis = (unsigned char)hdsp_spdif_emphasis(hdsp);
		info.spdif_nonaudio = (unsigned char)hdsp_spdif_nonaudio(hdsp);
		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);
		info.line_out = (unsigned char)hdsp_line_out(hdsp);
		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);
			info.xlr_breakout_cable = (unsigned char)hdsp_xlr_breakout_cable(hdsp);
T
Tim Blechmann 已提交
4638

L
Linus Torvalds 已提交
4639
		}
T
Takashi Iwai 已提交
4640
		if (hdsp->io_type == H9632 || hdsp->io_type == H9652)
L
Linus Torvalds 已提交
4641 4642 4643 4644 4645 4646 4647
			info.analog_extension_board = (unsigned char)hdsp_aeb(hdsp);
		spin_unlock_irqrestore(&hdsp->lock, flags);
		if (copy_to_user(argp, &info, sizeof(info)))
			return -EFAULT;
		break;
	}
	case SNDRV_HDSP_IOCTL_GET_9632_AEB: {
4648
		struct hdsp_9632_aeb h9632_aeb;
T
Tim Blechmann 已提交
4649

L
Linus Torvalds 已提交
4650 4651 4652 4653 4654 4655 4656 4657
		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: {
4658
		struct hdsp_version hdsp_version;
L
Linus Torvalds 已提交
4659
		int err;
T
Tim Blechmann 已提交
4660

L
Linus Torvalds 已提交
4661 4662
		if (hdsp->io_type == H9652 || hdsp->io_type == H9632) return -EINVAL;
		if (hdsp->io_type == Undefined) {
T
Takashi Iwai 已提交
4663
			if ((err = hdsp_get_iobox_version(hdsp)) < 0)
L
Linus Torvalds 已提交
4664 4665 4666 4667
				return err;
		}
		hdsp_version.io_type = hdsp->io_type;
		hdsp_version.firmware_rev = hdsp->firmware_rev;
T
Takashi Iwai 已提交
4668
		if ((err = copy_to_user(argp, &hdsp_version, sizeof(hdsp_version))))
L
Linus Torvalds 已提交
4669 4670 4671 4672
		    	return -EFAULT;
		break;
	}
	case SNDRV_HDSP_IOCTL_UPLOAD_FIRMWARE: {
4673
		struct hdsp_firmware __user *firmware;
L
Linus Torvalds 已提交
4674 4675
		u32 __user *firmware_data;
		int err;
T
Tim Blechmann 已提交
4676

L
Linus Torvalds 已提交
4677 4678 4679 4680 4681 4682 4683
		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;

T
Takashi Iwai 已提交
4684
		snd_printk(KERN_INFO "Hammerfall-DSP: initializing firmware upload\n");
4685
		firmware = (struct hdsp_firmware __user *)argp;
L
Linus Torvalds 已提交
4686

T
Takashi Iwai 已提交
4687
		if (get_user(firmware_data, &firmware->firmware_data))
L
Linus Torvalds 已提交
4688
			return -EFAULT;
T
Tim Blechmann 已提交
4689

T
Takashi Iwai 已提交
4690
		if (hdsp_check_for_iobox (hdsp))
L
Linus Torvalds 已提交
4691 4692
			return -EIO;

T
Takashi Iwai 已提交
4693
		if (copy_from_user(hdsp->firmware_cache, firmware_data, sizeof(hdsp->firmware_cache)) != 0)
L
Linus Torvalds 已提交
4694
			return -EFAULT;
T
Tim Blechmann 已提交
4695

L
Linus Torvalds 已提交
4696 4697
		hdsp->state |= HDSP_FirmwareCached;

T
Takashi Iwai 已提交
4698
		if ((err = snd_hdsp_load_firmware_from_cache(hdsp)) < 0)
L
Linus Torvalds 已提交
4699
			return err;
T
Tim Blechmann 已提交
4700

L
Linus Torvalds 已提交
4701
		if (!(hdsp->state & HDSP_InitializationComplete)) {
T
Takashi Iwai 已提交
4702
			if ((err = snd_hdsp_enable_io(hdsp)) < 0)
L
Linus Torvalds 已提交
4703
				return err;
T
Tim Blechmann 已提交
4704 4705

			snd_hdsp_initialize_channels(hdsp);
L
Linus Torvalds 已提交
4706
			snd_hdsp_initialize_midi_flush(hdsp);
T
Tim Blechmann 已提交
4707

L
Linus Torvalds 已提交
4708
			if ((err = snd_hdsp_create_alsa_devices(hdsp->card, hdsp)) < 0) {
T
Takashi Iwai 已提交
4709 4710
				snd_printk(KERN_ERR "Hammerfall-DSP: error creating alsa devices\n");
				return err;
L
Linus Torvalds 已提交
4711 4712 4713 4714 4715
			}
		}
		break;
	}
	case SNDRV_HDSP_IOCTL_GET_MIXER: {
4716
		struct hdsp_mixer __user *mixer = (struct hdsp_mixer __user *)argp;
L
Linus Torvalds 已提交
4717 4718 4719 4720 4721 4722 4723 4724 4725 4726
		if (copy_to_user(mixer->matrix, hdsp->mixer_matrix, sizeof(unsigned short)*HDSP_MATRIX_MIXER_SIZE))
			return -EFAULT;
		break;
	}
	default:
		return -EINVAL;
	}
	return 0;
}

4727
static struct snd_pcm_ops snd_hdsp_playback_ops = {
L
Linus Torvalds 已提交
4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738
	.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,
	.copy =		snd_hdsp_playback_copy,
	.silence =	snd_hdsp_hw_silence,
};

4739
static struct snd_pcm_ops snd_hdsp_capture_ops = {
L
Linus Torvalds 已提交
4740 4741 4742 4743 4744 4745 4746 4747 4748 4749
	.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,
	.copy =		snd_hdsp_capture_copy,
};

4750
static int snd_hdsp_create_hwdep(struct snd_card *card, struct hdsp *hdsp)
L
Linus Torvalds 已提交
4751
{
4752
	struct snd_hwdep *hw;
L
Linus Torvalds 已提交
4753
	int err;
T
Tim Blechmann 已提交
4754

L
Linus Torvalds 已提交
4755 4756
	if ((err = snd_hwdep_new(card, "HDSP hwdep", 0, &hw)) < 0)
		return err;
T
Tim Blechmann 已提交
4757

L
Linus Torvalds 已提交
4758 4759 4760 4761 4762
	hdsp->hwdep = hw;
	hw->private_data = hdsp;
	strcpy(hw->name, "HDSP hwdep interface");

	hw->ops.ioctl = snd_hdsp_hwdep_ioctl;
T
Tim Blechmann 已提交
4763

L
Linus Torvalds 已提交
4764 4765 4766
	return 0;
}

4767
static int snd_hdsp_create_pcm(struct snd_card *card, struct hdsp *hdsp)
L
Linus Torvalds 已提交
4768
{
4769
	struct snd_pcm *pcm;
L
Linus Torvalds 已提交
4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786
	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;
}

4787
static void snd_hdsp_9652_enable_mixer (struct hdsp *hdsp)
L
Linus Torvalds 已提交
4788 4789 4790 4791 4792
{
        hdsp->control2_register |= HDSP_9652_ENABLE_MIXER;
	hdsp_write (hdsp, HDSP_control2Reg, hdsp->control2_register);
}

4793
static int snd_hdsp_enable_io (struct hdsp *hdsp)
L
Linus Torvalds 已提交
4794 4795
{
	int i;
T
Tim Blechmann 已提交
4796

L
Linus Torvalds 已提交
4797
	if (hdsp_fifo_wait (hdsp, 0, 100)) {
T
Takashi Iwai 已提交
4798
		snd_printk(KERN_ERR "Hammerfall-DSP: enable_io fifo_wait failed\n");
L
Linus Torvalds 已提交
4799 4800
		return -EIO;
	}
T
Tim Blechmann 已提交
4801

L
Linus Torvalds 已提交
4802 4803 4804 4805
	for (i = 0; i < hdsp->max_channels; ++i) {
		hdsp_write (hdsp, HDSP_inputEnable + (4 * i), 1);
		hdsp_write (hdsp, HDSP_outputEnable + (4 * i), 1);
	}
T
Tim Blechmann 已提交
4806

L
Linus Torvalds 已提交
4807 4808 4809
	return 0;
}

4810
static void snd_hdsp_initialize_channels(struct hdsp *hdsp)
L
Linus Torvalds 已提交
4811 4812
{
	int status, aebi_channels, aebo_channels;
T
Tim Blechmann 已提交
4813

L
Linus Torvalds 已提交
4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825
	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;
T
Tim Blechmann 已提交
4826

L
Linus Torvalds 已提交
4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845
	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;
T
Tim Blechmann 已提交
4846

L
Linus Torvalds 已提交
4847 4848 4849 4850 4851 4852
	default:
 		/* should never get here */
		break;
	}
}

4853
static void snd_hdsp_initialize_midi_flush (struct hdsp *hdsp)
L
Linus Torvalds 已提交
4854 4855 4856 4857 4858
{
	snd_hdsp_flush_midi_input (hdsp, 0);
	snd_hdsp_flush_midi_input (hdsp, 1);
}

4859
static int snd_hdsp_create_alsa_devices(struct snd_card *card, struct hdsp *hdsp)
L
Linus Torvalds 已提交
4860 4861
{
	int err;
T
Tim Blechmann 已提交
4862

L
Linus Torvalds 已提交
4863
	if ((err = snd_hdsp_create_pcm(card, hdsp)) < 0) {
T
Takashi Iwai 已提交
4864
		snd_printk(KERN_ERR "Hammerfall-DSP: Error creating pcm interface\n");
L
Linus Torvalds 已提交
4865 4866
		return err;
	}
T
Tim Blechmann 已提交
4867

L
Linus Torvalds 已提交
4868 4869

	if ((err = snd_hdsp_create_midi(card, hdsp, 0)) < 0) {
T
Takashi Iwai 已提交
4870
		snd_printk(KERN_ERR "Hammerfall-DSP: Error creating first midi interface\n");
L
Linus Torvalds 已提交
4871 4872 4873 4874 4875
		return err;
	}

	if (hdsp->io_type == Digiface || hdsp->io_type == H9652) {
		if ((err = snd_hdsp_create_midi(card, hdsp, 1)) < 0) {
T
Takashi Iwai 已提交
4876
			snd_printk(KERN_ERR "Hammerfall-DSP: Error creating second midi interface\n");
L
Linus Torvalds 已提交
4877 4878 4879 4880 4881
			return err;
		}
	}

	if ((err = snd_hdsp_create_controls(card, hdsp)) < 0) {
T
Takashi Iwai 已提交
4882
		snd_printk(KERN_ERR "Hammerfall-DSP: Error creating ctl interface\n");
L
Linus Torvalds 已提交
4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894
		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) {
T
Takashi Iwai 已提交
4895
		snd_printk(KERN_ERR "Hammerfall-DSP: Error setting default values\n");
L
Linus Torvalds 已提交
4896 4897
		return err;
	}
T
Tim Blechmann 已提交
4898

L
Linus Torvalds 已提交
4899
	if (!(hdsp->state & HDSP_InitializationComplete)) {
4900
		strcpy(card->shortname, "Hammerfall DSP");
T
Tim Blechmann 已提交
4901
		sprintf(card->longname, "%s at 0x%lx, irq %d", hdsp->card_name,
L
Linus Torvalds 已提交
4902
			hdsp->port, hdsp->irq);
T
Tim Blechmann 已提交
4903

L
Linus Torvalds 已提交
4904
		if ((err = snd_card_register(card)) < 0) {
T
Takashi Iwai 已提交
4905
			snd_printk(KERN_ERR "Hammerfall-DSP: error registering card\n");
L
Linus Torvalds 已提交
4906 4907 4908 4909
			return err;
		}
		hdsp->state |= HDSP_InitializationComplete;
	}
T
Tim Blechmann 已提交
4910

L
Linus Torvalds 已提交
4911 4912 4913 4914 4915
	return 0;
}

#ifdef HDSP_FW_LOADER
/* load firmware via hotplug fw loader */
4916
static int hdsp_request_fw_loader(struct hdsp *hdsp)
L
Linus Torvalds 已提交
4917 4918 4919 4920
{
	const char *fwfile;
	const struct firmware *fw;
	int err;
T
Tim Blechmann 已提交
4921

L
Linus Torvalds 已提交
4922 4923 4924 4925 4926 4927 4928 4929
	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;
	}
T
Tim Blechmann 已提交
4930

L
Linus Torvalds 已提交
4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959
	/* caution: max length of firmware filename is 30! */
	switch (hdsp->io_type) {
	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:
		snd_printk(KERN_ERR "Hammerfall-DSP: invalid io_type %d\n", hdsp->io_type);
		return -EINVAL;
	}

	if (request_firmware(&fw, fwfile, &hdsp->pci->dev)) {
		snd_printk(KERN_ERR "Hammerfall-DSP: cannot load firmware %s\n", fwfile);
		return -ENOENT;
	}
	if (fw->size < sizeof(hdsp->firmware_cache)) {
		snd_printk(KERN_ERR "Hammerfall-DSP: too short firmware size %d (expected %d)\n",
			   (int)fw->size, (int)sizeof(hdsp->firmware_cache));
		release_firmware(fw);
		return -EINVAL;
	}
4960

L
Linus Torvalds 已提交
4961
	memcpy(hdsp->firmware_cache, fw->data, sizeof(hdsp->firmware_cache));
4962

L
Linus Torvalds 已提交
4963
	release_firmware(fw);
T
Tim Blechmann 已提交
4964

L
Linus Torvalds 已提交
4965 4966 4967 4968
	hdsp->state |= HDSP_FirmwareCached;

	if ((err = snd_hdsp_load_firmware_from_cache(hdsp)) < 0)
		return err;
T
Tim Blechmann 已提交
4969

L
Linus Torvalds 已提交
4970
	if (!(hdsp->state & HDSP_InitializationComplete)) {
T
Takashi Iwai 已提交
4971
		if ((err = snd_hdsp_enable_io(hdsp)) < 0)
L
Linus Torvalds 已提交
4972 4973 4974
			return err;

		if ((err = snd_hdsp_create_hwdep(hdsp->card, hdsp)) < 0) {
T
Takashi Iwai 已提交
4975
			snd_printk(KERN_ERR "Hammerfall-DSP: error creating hwdep device\n");
L
Linus Torvalds 已提交
4976 4977 4978 4979 4980
			return err;
		}
		snd_hdsp_initialize_channels(hdsp);
		snd_hdsp_initialize_midi_flush(hdsp);
		if ((err = snd_hdsp_create_alsa_devices(hdsp->card, hdsp)) < 0) {
T
Takashi Iwai 已提交
4981
			snd_printk(KERN_ERR "Hammerfall-DSP: error creating alsa devices\n");
L
Linus Torvalds 已提交
4982 4983 4984 4985 4986 4987 4988
			return err;
		}
	}
	return 0;
}
#endif

4989 4990
static int __devinit snd_hdsp_create(struct snd_card *card,
				     struct hdsp *hdsp)
L
Linus Torvalds 已提交
4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015
{
	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;
T
Tim Blechmann 已提交
5016

L
Linus Torvalds 已提交
5017 5018 5019
	spin_lock_init(&hdsp->lock);

	tasklet_init(&hdsp->midi_tasklet, hdsp_midi_tasklet, (unsigned long)hdsp);
T
Tim Blechmann 已提交
5020

L
Linus Torvalds 已提交
5021 5022
	pci_read_config_word(hdsp->pci, PCI_CLASS_REVISION, &hdsp->firmware_rev);
	hdsp->firmware_rev &= 0xff;
T
Tim Blechmann 已提交
5023

L
Linus Torvalds 已提交
5024 5025 5026 5027 5028 5029 5030 5031
	/* 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);
T
Tim Blechmann 已提交
5032

L
Linus Torvalds 已提交
5033 5034 5035
	strcpy(card->driver, "H-DSP");
	strcpy(card->mixername, "Xilinx FPGA");

T
Takashi Iwai 已提交
5036
	if (hdsp->firmware_rev < 0xa)
L
Linus Torvalds 已提交
5037
		return -ENODEV;
T
Takashi Iwai 已提交
5038
	else if (hdsp->firmware_rev < 0x64)
L
Linus Torvalds 已提交
5039
		hdsp->card_name = "RME Hammerfall DSP";
T
Takashi Iwai 已提交
5040
	else if (hdsp->firmware_rev < 0x96) {
L
Linus Torvalds 已提交
5041 5042 5043 5044 5045
		hdsp->card_name = "RME HDSP 9652";
		is_9652 = 1;
	} else {
		hdsp->card_name = "RME HDSP 9632";
		hdsp->max_channels = 16;
T
Tim Blechmann 已提交
5046
		is_9632 = 1;
L
Linus Torvalds 已提交
5047 5048
	}

T
Takashi Iwai 已提交
5049
	if ((err = pci_enable_device(pci)) < 0)
L
Linus Torvalds 已提交
5050 5051 5052 5053 5054 5055 5056 5057
		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) {
T
Takashi Iwai 已提交
5058
		snd_printk(KERN_ERR "Hammerfall-DSP: unable to remap region 0x%lx-0x%lx\n", hdsp->port, hdsp->port + HDSP_IO_EXTENT - 1);
L
Linus Torvalds 已提交
5059 5060 5061
		return -EBUSY;
	}

5062 5063
	if (request_irq(pci->irq, snd_hdsp_interrupt, IRQF_SHARED,
			"hdsp", hdsp)) {
T
Takashi Iwai 已提交
5064
		snd_printk(KERN_ERR "Hammerfall-DSP: unable to use IRQ %d\n", pci->irq);
L
Linus Torvalds 已提交
5065 5066 5067 5068
		return -EBUSY;
	}

	hdsp->irq = pci->irq;
5069
	hdsp->precise_ptr = 0;
L
Linus Torvalds 已提交
5070
	hdsp->use_midi_tasklet = 1;
5071
	hdsp->dds_value = 0;
L
Linus Torvalds 已提交
5072

T
Takashi Iwai 已提交
5073
	if ((err = snd_hdsp_initialize_memory(hdsp)) < 0)
L
Linus Torvalds 已提交
5074
		return err;
T
Tim Blechmann 已提交
5075

L
Linus Torvalds 已提交
5076
	if (!is_9652 && !is_9632) {
T
Tim Blechmann 已提交
5077 5078 5079
		/* 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);
L
Linus Torvalds 已提交
5080

5081 5082 5083
		if (err < 0)
			return err;

L
Linus Torvalds 已提交
5084 5085
		if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
#ifdef HDSP_FW_LOADER
T
Takashi Iwai 已提交
5086
			if ((err = hdsp_request_fw_loader(hdsp)) < 0)
L
Linus Torvalds 已提交
5087 5088 5089 5090
				/* we don't fail as this can happen
				   if userspace is not ready for
				   firmware upload
				*/
T
Takashi Iwai 已提交
5091 5092
				snd_printk(KERN_ERR "Hammerfall-DSP: couldn't get firmware from userspace. try using hdsploader\n");
			else
L
Linus Torvalds 已提交
5093 5094 5095
				/* init is complete, we return */
				return 0;
#endif
5096
			/* we defer initialization */
T
Takashi Iwai 已提交
5097 5098
			snd_printk(KERN_INFO "Hammerfall-DSP: card initialization pending : waiting for firmware\n");
			if ((err = snd_hdsp_create_hwdep(card, hdsp)) < 0)
L
Linus Torvalds 已提交
5099 5100 5101
				return err;
			return 0;
		} else {
T
Tim Blechmann 已提交
5102
			snd_printk(KERN_INFO "Hammerfall-DSP: Firmware already present, initializing card.\n");
T
Takashi Iwai 已提交
5103
			if (hdsp_read(hdsp, HDSP_status2Register) & HDSP_version1)
L
Linus Torvalds 已提交
5104
				hdsp->io_type = Multiface;
T
Tim Blechmann 已提交
5105
			else
L
Linus Torvalds 已提交
5106 5107 5108
				hdsp->io_type = Digiface;
		}
	}
T
Tim Blechmann 已提交
5109

T
Takashi Iwai 已提交
5110
	if ((err = snd_hdsp_enable_io(hdsp)) != 0)
L
Linus Torvalds 已提交
5111
		return err;
T
Tim Blechmann 已提交
5112

T
Takashi Iwai 已提交
5113
	if (is_9652)
L
Linus Torvalds 已提交
5114
	        hdsp->io_type = H9652;
T
Tim Blechmann 已提交
5115

T
Takashi Iwai 已提交
5116
	if (is_9632)
L
Linus Torvalds 已提交
5117 5118
		hdsp->io_type = H9632;

T
Takashi Iwai 已提交
5119
	if ((err = snd_hdsp_create_hwdep(card, hdsp)) < 0)
L
Linus Torvalds 已提交
5120
		return err;
T
Tim Blechmann 已提交
5121

L
Linus Torvalds 已提交
5122 5123 5124
	snd_hdsp_initialize_channels(hdsp);
	snd_hdsp_initialize_midi_flush(hdsp);

T
Tim Blechmann 已提交
5125
	hdsp->state |= HDSP_FirmwareLoaded;
L
Linus Torvalds 已提交
5126

T
Takashi Iwai 已提交
5127
	if ((err = snd_hdsp_create_alsa_devices(card, hdsp)) < 0)
L
Linus Torvalds 已提交
5128 5129
		return err;

T
Tim Blechmann 已提交
5130
	return 0;
L
Linus Torvalds 已提交
5131 5132
}

5133
static int snd_hdsp_free(struct hdsp *hdsp)
L
Linus Torvalds 已提交
5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145
{
	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);
T
Tim Blechmann 已提交
5146

L
Linus Torvalds 已提交
5147 5148 5149 5150 5151
	if (hdsp->iobase)
		iounmap(hdsp->iobase);

	if (hdsp->port)
		pci_release_regions(hdsp->pci);
T
Tim Blechmann 已提交
5152

L
Linus Torvalds 已提交
5153 5154 5155 5156
	pci_disable_device(hdsp->pci);
	return 0;
}

5157
static void snd_hdsp_card_free(struct snd_card *card)
L
Linus Torvalds 已提交
5158
{
5159
	struct hdsp *hdsp = (struct hdsp *) card->private_data;
L
Linus Torvalds 已提交
5160 5161 5162 5163 5164 5165 5166 5167 5168

	if (hdsp)
		snd_hdsp_free(hdsp);
}

static int __devinit snd_hdsp_probe(struct pci_dev *pci,
				    const struct pci_device_id *pci_id)
{
	static int dev;
5169 5170
	struct hdsp *hdsp;
	struct snd_card *card;
L
Linus Torvalds 已提交
5171 5172 5173 5174 5175 5176 5177 5178 5179
	int err;

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

5180 5181 5182 5183
	err = snd_card_create(index[dev], id[dev], THIS_MODULE,
			      sizeof(struct hdsp), &card);
	if (err < 0)
		return err;
L
Linus Torvalds 已提交
5184

5185
	hdsp = (struct hdsp *) card->private_data;
L
Linus Torvalds 已提交
5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196
	card->private_free = snd_hdsp_card_free;
	hdsp->dev = dev;
	hdsp->pci = pci;
	snd_card_set_dev(card, &pci->dev);

	if ((err = snd_hdsp_create(card, hdsp)) < 0) {
		snd_card_free(card);
		return err;
	}

	strcpy(card->shortname, "Hammerfall DSP");
T
Tim Blechmann 已提交
5197
	sprintf(card->longname, "%s at 0x%lx, irq %d", hdsp->card_name,
L
Linus Torvalds 已提交
5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223
		hdsp->port, hdsp->irq);

	if ((err = snd_card_register(card)) < 0) {
		snd_card_free(card);
		return err;
	}
	pci_set_drvdata(pci, card);
	dev++;
	return 0;
}

static void __devexit snd_hdsp_remove(struct pci_dev *pci)
{
	snd_card_free(pci_get_drvdata(pci));
	pci_set_drvdata(pci, NULL);
}

static struct pci_driver driver = {
	.name =     "RME Hammerfall DSP",
	.id_table = snd_hdsp_ids,
	.probe =    snd_hdsp_probe,
	.remove = __devexit_p(snd_hdsp_remove),
};

static int __init alsa_card_hdsp_init(void)
{
5224
	return pci_register_driver(&driver);
L
Linus Torvalds 已提交
5225 5226 5227 5228 5229 5230 5231 5232 5233
}

static void __exit alsa_card_hdsp_exit(void)
{
	pci_unregister_driver(&driver);
}

module_init(alsa_card_hdsp_init)
module_exit(alsa_card_hdsp_exit)