blob: d15ffb3e9b0a58195744bf35944b07c27e7cb533 [file] [log] [blame]
/*
* 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 <sound/driver.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
#include <linux/slab.h>
#include <linux/pci.h>
#include <linux/firmware.h>
#include <linux/moduleparam.h>
#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}}");
#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
#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_midiDataOut0 352
#define HDSP_midiDataOut1 356
#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
when read; the least-significant 4 bits are full-scale counters;
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
*/
#define HDSP_9652_peakBase 7164
#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 */
#define HDSP_SyncRef2 (1<<13)
#define HDSP_SPDIFInputSelect0 (1<<14)
#define HDSP_SPDIFInputSelect1 (1<<15)
#define HDSP_SyncRef0 (1<<16)
#define HDSP_SyncRef1 (1<<17)
#define HDSP_AnalogExtensionBoard (1<<18) /* For H9632 cards */
#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)
#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 */
#define HDSP_midi0IRQPending (1<<30)
#define HDSP_midi1IRQPending (1<<31)
#define HDSP_spdifFrequencyMask (HDSP_spdifFrequency0|HDSP_spdifFrequency1|HDSP_spdifFrequency2)
#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 */
#define HDSP_spdifFrequency128KHz HDSP_spdifFrequencyMask
#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
size is the same regardless of the number of channels - the
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)
/* use hotplug firmeare loader? */
#if defined(CONFIG_FW_LOADER) || defined(CONFIG_FW_LOADER_MODULE)
#ifndef HDSP_USE_HWDEP_LOADER
#define HDSP_FW_LOADER
#endif
#endif
typedef struct _hdsp hdsp_t;
typedef struct _hdsp_midi hdsp_midi_t;
typedef struct _hdsp_9632_meters hdsp_9632_meters_t;
struct _hdsp_9632_meters {
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];
};
struct _hdsp_midi {
hdsp_t *hdsp;
int id;
snd_rawmidi_t *rmidi;
snd_rawmidi_substream_t *input;
snd_rawmidi_substream_t *output;
char istimer; /* timer in use */
struct timer_list timer;
spinlock_t lock;
int pending;
};
struct _hdsp {
spinlock_t lock;
snd_pcm_substream_t *capture_substream;
snd_pcm_substream_t *playback_substream;
hdsp_midi_t midi[2];
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;
int clock_source_locked;
char *card_name; /* digiface/multiface */
HDSP_IO_Type io_type; /* ditto, but for code use */
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 */
unsigned char qs_out_channels;
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;
snd_card_t *card;
snd_pcm_t *pcm;
snd_hwdep_t *hwdep;
struct pci_dev *pci;
snd_kcontrol_t *spdif_ctl;
unsigned short mixer_matrix[HDSP_MATRIX_MIXER_SIZE];
};
/* 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 */
0, 1, 2, 3, 4, 5, 6, 7,
/* ADAT 2 */
16, 17, 18, 19, 20, 21, 22, 23,
/* 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 */
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
};
static char channel_map_H9632_ds[HDSP_MAX_CHANNELS] = {
/* ADAT */
1, 3, 5, 7,
/* 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
};
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);
if (snd_dma_get_reserved_buf(dmab, snd_dma_pci_buf_id(pci))) {
if (dmab->bytes >= size)
return 0;
}
if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
size, dmab) < 0)
return -ENOMEM;
return 0;
}
static void snd_hammerfall_free_buffer(struct snd_dma_buffer *dmab, struct pci_dev *pci)
{
if (dmab->area) {
dmab->dev.dev = NULL; /* make it anonymous */
snd_dma_reserve_buf(dmab, snd_dma_pci_buf_id(pci));
}
}
static struct pci_device_id snd_hdsp_ids[] = {
{
.vendor = PCI_VENDOR_ID_XILINX,
.device = PCI_DEVICE_ID_XILINX_HAMMERFALL_DSP,
.subvendor = PCI_ANY_ID,
.subdevice = PCI_ANY_ID,
}, /* RME Hammerfall-DSP */
{ 0, },
};
MODULE_DEVICE_TABLE(pci, snd_hdsp_ids);
/* prototypes */
static int snd_hdsp_create_alsa_devices(snd_card_t *card, hdsp_t *hdsp);
static int snd_hdsp_create_pcm(snd_card_t *card, hdsp_t *hdsp);
static int snd_hdsp_enable_io (hdsp_t *hdsp);
static void snd_hdsp_initialize_midi_flush (hdsp_t *hdsp);
static void snd_hdsp_initialize_channels (hdsp_t *hdsp);
static int hdsp_fifo_wait(hdsp_t *hdsp, int count, int timeout);
static int hdsp_autosync_ref(hdsp_t *hdsp);
static int snd_hdsp_set_defaults(hdsp_t *hdsp);
static void snd_hdsp_9652_enable_mixer (hdsp_t *hdsp);
static int hdsp_playback_to_output_key (hdsp_t *hdsp, int in, int out)
{
switch (hdsp->firmware_rev) {
case 0xa:
return (64 * out) + (32 + (in));
case 0x96:
case 0x97:
return (32 * out) + (16 + (in));
default:
return (52 * out) + (26 + (in));
}
}
static int hdsp_input_to_output_key (hdsp_t *hdsp, int in, int out)
{
switch (hdsp->firmware_rev) {
case 0xa:
return (64 * out) + in;
case 0x96:
case 0x97:
return (32 * out) + in;
default:
return (52 * out) + in;
}
}
static void hdsp_write(hdsp_t *hdsp, int reg, int val)
{
writel(val, hdsp->iobase + reg);
}
static unsigned int hdsp_read(hdsp_t *hdsp, int reg)
{
return readl (hdsp->iobase + reg);
}
static int hdsp_check_for_iobox (hdsp_t *hdsp)
{
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;
}
static int snd_hdsp_load_firmware_from_cache(hdsp_t *hdsp) {
int i;
unsigned long flags;
if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
snd_printk ("Hammerfall-DSP: loading firmware\n");
hdsp_write (hdsp, HDSP_control2Reg, HDSP_S_PROGRAM);
hdsp_write (hdsp, HDSP_fifoData, 0);
if (hdsp_fifo_wait (hdsp, 0, HDSP_LONG_WAIT)) {
snd_printk ("Hammerfall-DSP: timeout waiting for download preparation\n");
return -EIO;
}
hdsp_write (hdsp, HDSP_control2Reg, HDSP_S_LOAD);
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;
}
}
ssleep(3);
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");
}
if (hdsp->state & HDSP_InitializationComplete) {
snd_printk(KERN_INFO "Hammerfall-DSP: firmware loaded from cache, restoring defaults\n");
spin_lock_irqsave(&hdsp->lock, flags);
snd_hdsp_set_defaults(hdsp);
spin_unlock_irqrestore(&hdsp->lock, flags);
}
hdsp->state |= HDSP_FirmwareLoaded;
return 0;
}
static int hdsp_get_iobox_version (hdsp_t *hdsp)
{
if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
hdsp_write (hdsp, HDSP_control2Reg, HDSP_PROGRAM);
hdsp_write (hdsp, HDSP_fifoData, 0);
if (hdsp_fifo_wait (hdsp, 0, HDSP_SHORT_WAIT) < 0)
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;
}
} else {
/* firmware was already loaded, get iobox type */
if (hdsp_read(hdsp, HDSP_status2Register) & HDSP_version1)
hdsp->io_type = Multiface;
else
hdsp->io_type = Digiface;
}
return 0;
}
static int hdsp_check_for_firmware (hdsp_t *hdsp, int show_err)
{
if (hdsp->io_type == H9652 || hdsp->io_type == H9632) return 0;
if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
snd_printk(KERN_ERR "Hammerfall-DSP: firmware not present.\n");
hdsp->state &= ~HDSP_FirmwareLoaded;
if (! show_err)
return -EIO;
/* try to load firmware */
if (hdsp->state & HDSP_FirmwareCached) {
if (snd_hdsp_load_firmware_from_cache(hdsp) != 0)
snd_printk(KERN_ERR "Hammerfall-DSP: Firmware loading from cache failed, please upload manually.\n");
} else {
snd_printk(KERN_ERR "Hammerfall-DSP: No firmware loaded nor cached, please upload firmware.\n");
}
return -EIO;
}
return 0;
}
static int hdsp_fifo_wait(hdsp_t *hdsp, int count, int timeout)
{
int i;
/* the fifoStatus registers reports on how many words
are available in the command FIFO.
*/
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;
}
static int hdsp_read_gain (hdsp_t *hdsp, unsigned int addr)
{
if (addr >= HDSP_MATRIX_MIXER_SIZE)
return 0;
return hdsp->mixer_matrix[addr];
}
static int hdsp_write_gain(hdsp_t *hdsp, unsigned int addr, unsigned short data)
{
unsigned int ad;
if (addr >= HDSP_MATRIX_MIXER_SIZE)
return -1;
if (hdsp->io_type == H9652 || hdsp->io_type == H9632) {
/* from martin bjornsen:
"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."
*/
if (hdsp->io_type == H9632 && addr >= 512)
return 0;
if (hdsp->io_type == H9652 && addr >= 1352)
return 0;
hdsp->mixer_matrix[addr] = data;
/* `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;
hdsp_write (hdsp, 4096 + (ad*4),
(hdsp->mixer_matrix[(addr&0x7fe)+1] << 16) +
hdsp->mixer_matrix[addr&0x7fe]);
return 0;
} else {
ad = (addr << 16) + data;
if (hdsp_fifo_wait(hdsp, 127, HDSP_LONG_WAIT))
return -1;
hdsp_write (hdsp, HDSP_fifoData, ad);
hdsp->mixer_matrix[addr] = data;
}
return 0;
}
static int snd_hdsp_use_is_exclusive(hdsp_t *hdsp)
{
unsigned long flags;
int ret = 1;
spin_lock_irqsave(&hdsp->lock, flags);
if ((hdsp->playback_pid != hdsp->capture_pid) &&
(hdsp->playback_pid >= 0) && (hdsp->capture_pid >= 0))
ret = 0;
spin_unlock_irqrestore(&hdsp->lock, flags);
return ret;
}
static int hdsp_external_sample_rate (hdsp_t *hdsp)
{
unsigned int status2 = hdsp_read(hdsp, HDSP_status2Register);
unsigned int rate_bits = status2 & HDSP_systemFrequencyMask;
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;
}
}
static int hdsp_spdif_sample_rate(hdsp_t *hdsp)
{
unsigned int status = hdsp_read(hdsp, HDSP_statusRegister);
unsigned int rate_bits = (status & HDSP_spdifFrequencyMask);
if (status & HDSP_SPDIFErrorFlag)
return 0;
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;
case HDSP_spdifFrequency128KHz:
if (hdsp->io_type == H9632) return 128000;
break;
case HDSP_spdifFrequency176_4KHz:
if (hdsp->io_type == H9632) return 176400;
break;
case HDSP_spdifFrequency192KHz:
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;
}
static void hdsp_compute_period_size(hdsp_t *hdsp)
{
hdsp->period_bytes = 1 << ((hdsp_decode_latency(hdsp->control_register) + 8));
}
static snd_pcm_uframes_t hdsp_hw_pointer(hdsp_t *hdsp)
{
int position;
position = hdsp_read(hdsp, HDSP_statusRegister);
if (!hdsp->precise_ptr)
return (position & HDSP_BufferID) ? (hdsp->period_bytes / 4) : 0;
position &= HDSP_BufferPositionMask;
position /= 4;
position &= (hdsp->period_bytes/2) - 1;
return position;
}
static void hdsp_reset_hw_pointer(hdsp_t *hdsp)
{
hdsp_write (hdsp, HDSP_resetPointer, 0);
}
static void hdsp_start_audio(hdsp_t *s)
{
s->control_register |= (HDSP_AudioInterruptEnable | HDSP_Start);
hdsp_write(s, HDSP_controlRegister, s->control_register);
}
static void hdsp_stop_audio(hdsp_t *s)
{
s->control_register &= ~(HDSP_Start | HDSP_AudioInterruptEnable);
hdsp_write(s, HDSP_controlRegister, s->control_register);
}
static void hdsp_silence_playback(hdsp_t *hdsp)
{
memset(hdsp->playback_buffer, 0, HDSP_DMA_AREA_BYTES);
}
static int hdsp_set_interrupt_interval(hdsp_t *s, unsigned int frames)
{
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;
}
static int hdsp_set_rate(hdsp_t *hdsp, int rate, int called_internally)
{
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).
*/
if (!(hdsp->control_register & HDSP_ClockModeMaster)) {
if (called_internally) {
/* request from ctl or card initialization */
snd_printk(KERN_ERR "Hammerfall-DSP: device is not running as a clock master: cannot set sample rate.\n");
return -1;
} else {
/* hw_param request while in AutoSync mode */
int external_freq = hdsp_external_sample_rate(hdsp);
int spdif_freq = hdsp_spdif_sample_rate(hdsp);
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))
snd_printk(KERN_INFO "Hammerfall-DSP: Detected ADAT in quad speed mode\n");
else if (rate != external_freq) {
snd_printk(KERN_INFO "Hammerfall-DSP: No AutoSync source for requested rate\n");
return -1;
}
}
}
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
such a change causes a shift in the location of
the DMA buffers and a reduction in the number of available
buffers.
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. */
if (rate > 96000 && hdsp->io_type != H9632)
return -EINVAL;
switch (rate) {
case 32000:
if (current_rate > 48000)
reject_if_open = 1;
rate_bits = HDSP_Frequency32KHz;
break;
case 44100:
if (current_rate > 48000)
reject_if_open = 1;
rate_bits = HDSP_Frequency44_1KHz;
break;
case 48000:
if (current_rate > 48000)
reject_if_open = 1;
rate_bits = HDSP_Frequency48KHz;
break;
case 64000:
if (current_rate <= 48000 || current_rate > 96000)
reject_if_open = 1;
rate_bits = HDSP_Frequency64KHz;
break;
case 88200:
if (current_rate <= 48000 || current_rate > 96000)
reject_if_open = 1;
rate_bits = HDSP_Frequency88_2KHz;
break;
case 96000:
if (current_rate <= 48000 || current_rate > 96000)
reject_if_open = 1;
rate_bits = HDSP_Frequency96KHz;
break;
case 128000:
if (current_rate < 128000)
reject_if_open = 1;
rate_bits = HDSP_Frequency128KHz;
break;
case 176400:
if (current_rate < 128000)
reject_if_open = 1;
rate_bits = HDSP_Frequency176_4KHz;
break;
case 192000:
if (current_rate < 128000)
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);
if (rate >= 128000) {
hdsp->channel_map = channel_map_H9632_qs;
} else if (rate > 48000) {
if (hdsp->io_type == H9632)
hdsp->channel_map = channel_map_H9632_ds;
else
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;
}
}
hdsp->system_sample_rate = rate;
return 0;
}
/*----------------------------------------------------------------------------
MIDI
----------------------------------------------------------------------------*/
static unsigned char snd_hdsp_midi_read_byte (hdsp_t *hdsp, int id)
{
/* the hardware already does the relevant bit-mask with 0xff */
if (id)
return hdsp_read(hdsp, HDSP_midiDataIn1);
else
return hdsp_read(hdsp, HDSP_midiDataIn0);
}
static void snd_hdsp_midi_write_byte (hdsp_t *hdsp, int id, int val)
{
/* the hardware already does the relevant bit-mask with 0xff */
if (id)
hdsp_write(hdsp, HDSP_midiDataOut1, val);
else
hdsp_write(hdsp, HDSP_midiDataOut0, val);
}
static int snd_hdsp_midi_input_available (hdsp_t *hdsp, int id)
{
if (id)
return (hdsp_read(hdsp, HDSP_midiStatusIn1) & 0xff);
else
return (hdsp_read(hdsp, HDSP_midiStatusIn0) & 0xff);
}
static int snd_hdsp_midi_output_possible (hdsp_t *hdsp, int id)
{
int fifo_bytes_used;
if (id)
fifo_bytes_used = hdsp_read(hdsp, HDSP_midiStatusOut1) & 0xff;
else
fifo_bytes_used = hdsp_read(hdsp, HDSP_midiStatusOut0) & 0xff;
if (fifo_bytes_used < 128)
return 128 - fifo_bytes_used;
else
return 0;
}
static void snd_hdsp_flush_midi_input (hdsp_t *hdsp, int id)
{
while (snd_hdsp_midi_input_available (hdsp, id))
snd_hdsp_midi_read_byte (hdsp, id);
}
static int snd_hdsp_midi_output_write (hdsp_midi_t *hmidi)
{
unsigned long flags;
int n_pending;
int to_write;
int i;
unsigned char buf[128];
/* Output is not interrupt driven */
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);
if ((to_write = snd_rawmidi_transmit (hmidi->output, buf, n_pending)) > 0) {
for (i = 0; i < to_write; ++i)
snd_hdsp_midi_write_byte (hmidi->hdsp, hmidi->id, buf[i]);
}
}
}
}
spin_unlock_irqrestore (&hmidi->lock, flags);
return 0;
}
static int snd_hdsp_midi_input_read (hdsp_midi_t *hmidi)
{
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) {
if (n_pending > (int)sizeof (buf))
n_pending = sizeof (buf);
for (i = 0; i < n_pending; ++i)
buf[i] = snd_hdsp_midi_read_byte (hmidi->hdsp, hmidi->id);
if (n_pending)
snd_rawmidi_receive (hmidi->input, buf, n_pending);
} else {
/* flush the MIDI input FIFO */
while (--n_pending)
snd_hdsp_midi_read_byte (hmidi->hdsp, hmidi->id);
}
}
hmidi->pending = 0;
if (hmidi->id)
hmidi->hdsp->control_register |= HDSP_Midi1InterruptEnable;
else
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);
}
static void snd_hdsp_midi_input_trigger(snd_rawmidi_substream_t * substream, int up)
{
hdsp_t *hdsp;
hdsp_midi_t *hmidi;
unsigned long flags;
u32 ie;
hmidi = (hdsp_midi_t *) substream->rmidi->private_data;
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)
{
hdsp_midi_t *hmidi = (hdsp_midi_t *) data;
unsigned long flags;
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
leaving istimer wherever it was set before.
*/
if (hmidi->istimer) {
hmidi->timer.expires = 1 + jiffies;
add_timer(&hmidi->timer);
}
spin_unlock_irqrestore (&hmidi->lock, flags);
}
static void snd_hdsp_midi_output_trigger(snd_rawmidi_substream_t * substream, int up)
{
hdsp_midi_t *hmidi;
unsigned long flags;
hmidi = (hdsp_midi_t *) substream->rmidi->private_data;
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 {
if (hmidi->istimer && --hmidi->istimer <= 0)
del_timer (&hmidi->timer);
}
spin_unlock_irqrestore (&hmidi->lock, flags);
if (up)
snd_hdsp_midi_output_write(hmidi);
}
static int snd_hdsp_midi_input_open(snd_rawmidi_substream_t * substream)
{
hdsp_midi_t *hmidi;
hmidi = (hdsp_midi_t *) substream->rmidi->private_data;
spin_lock_irq (&hmidi->lock);
snd_hdsp_flush_midi_input (hmidi->hdsp, hmidi->id);
hmidi->input = substream;
spin_unlock_irq (&hmidi->lock);
return 0;
}
static int snd_hdsp_midi_output_open(snd_rawmidi_substream_t * substream)
{
hdsp_midi_t *hmidi;
hmidi = (hdsp_midi_t *) substream->rmidi->private_data;
spin_lock_irq (&hmidi->lock);
hmidi->output = substream;
spin_unlock_irq (&hmidi->lock);
return 0;
}
static int snd_hdsp_midi_input_close(snd_rawmidi_substream_t * substream)
{
hdsp_midi_t *hmidi;
snd_hdsp_midi_input_trigger (substream, 0);
hmidi = (hdsp_midi_t *) substream->rmidi->private_data;
spin_lock_irq (&hmidi->lock);
hmidi->input = NULL;
spin_unlock_irq (&hmidi->lock);
return 0;
}
static int snd_hdsp_midi_output_close(snd_rawmidi_substream_t * substream)
{
hdsp_midi_t *hmidi;
snd_hdsp_midi_output_trigger (substream, 0);
hmidi = (hdsp_midi_t *) substream->rmidi->private_data;
spin_lock_irq (&hmidi->lock);
hmidi->output = NULL;
spin_unlock_irq (&hmidi->lock);
return 0;
}
static snd_rawmidi_ops_t snd_hdsp_midi_output =
{
.open = snd_hdsp_midi_output_open,
.close = snd_hdsp_midi_output_close,
.trigger = snd_hdsp_midi_output_trigger,
};
static snd_rawmidi_ops_t snd_hdsp_midi_input =
{
.open = snd_hdsp_midi_input_open,
.close = snd_hdsp_midi_input_close,
.trigger = snd_hdsp_midi_input_trigger,
};
static int __devinit snd_hdsp_create_midi (snd_card_t *card, hdsp_t *hdsp, int id)
{
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);
if (snd_rawmidi_new (card, buf, id, 1, 1, &hdsp->midi[id].rmidi) < 0)
return -1;
sprintf (hdsp->midi[id].rmidi->name, "%s MIDI %d", card->id, id+1);
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
----------------------------------------------------------------------------*/
static u32 snd_hdsp_convert_from_aes(snd_aes_iec958_t *aes)
{
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;
}
static void snd_hdsp_convert_to_aes(snd_aes_iec958_t *aes, u32 val)
{
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;
}
static int snd_hdsp_control_spdif_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
{
uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
uinfo->count = 1;
return 0;
}
static int snd_hdsp_control_spdif_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
{
hdsp_t *hdsp = snd_kcontrol_chip(kcontrol);
snd_hdsp_convert_to_aes(&ucontrol->value.iec958, hdsp->creg_spdif);
return 0;
}
static int snd_hdsp_control_spdif_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
{
hdsp_t *hdsp = snd_kcontrol_chip(kcontrol);
int change;
u32 val;
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;
}
static int snd_hdsp_control_spdif_stream_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
{
uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
uinfo->count = 1;
return 0;
}
static int snd_hdsp_control_spdif_stream_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
{
hdsp_t *hdsp = snd_kcontrol_chip(kcontrol);
snd_hdsp_convert_to_aes(&ucontrol->value.iec958, hdsp->creg_spdif_stream);
return 0;
}
static int snd_hdsp_control_spdif_stream_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
{
hdsp_t *hdsp = snd_kcontrol_chip(kcontrol);
int change;
u32 val;
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;
}
static int snd_hdsp_control_spdif_mask_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
{
uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
uinfo->count = 1;
return 0;
}
static int snd_hdsp_control_spdif_mask_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
{
ucontrol->value.iec958.status[0] = kcontrol->private_value;
return 0;
}
#define HDSP_SPDIF_IN(xname, xindex) \
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
.name = xname, \
.index = xindex, \
.info = snd_hdsp_info_spdif_in, \
.get = snd_hdsp_get_spdif_in, \
.put = snd_hdsp_put_spdif_in }
static unsigned int hdsp_spdif_in(hdsp_t *hdsp)
{
return hdsp_decode_spdif_in(hdsp->control_register & HDSP_SPDIFInputMask);
}
static int hdsp_set_spdif_input(hdsp_t *hdsp, int in)
{
hdsp->control_register &= ~HDSP_SPDIFInputMask;
hdsp->control_register |= hdsp_encode_spdif_in(in);
hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
return 0;
}
static int snd_hdsp_info_spdif_in(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
{
static char *texts[4] = {"Optical", "Coaxial", "Internal", "AES"};
hdsp_t *hdsp = snd_kcontrol_chip(kcontrol);
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;
}
static int snd_hdsp_get_spdif_in(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
{
hdsp_t *hdsp = snd_kcontrol_chip(kcontrol);
ucontrol->value.enumerated.item[0] = hdsp_spdif_in(hdsp);
return 0;
}
static int snd_hdsp_put_spdif_in(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
{
hdsp_t *hdsp = snd_kcontrol_chip(kcontrol);
int change;
unsigned int val;
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) \
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
.info = snd_hdsp_info_spdif_bits, \
.get = snd_hdsp_get_spdif_out, .put = snd_hdsp_put_spdif_out }
static int hdsp_spdif_out(hdsp_t *hdsp)
{
return (hdsp->control_register & HDSP_SPDIFOpticalOut) ? 1 : 0;
}
static int hdsp_set_spdif_output(hdsp_t *hdsp, int out)
{
if (out)
hdsp->control_register |= HDSP_SPDIFOpticalOut;
else
hdsp->control_register &= ~HDSP_SPDIFOpticalOut;
hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
return 0;
}
static int snd_hdsp_info_spdif_bits(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
{
uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
uinfo->count = 1;
uinfo->value.integer.min = 0;
uinfo->value.integer.max = 1;
return 0;
}
static int snd_hdsp_get_spdif_out(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
{
hdsp_t *hdsp = snd_kcontrol_chip(kcontrol);
ucontrol->value.integer.value[0] = hdsp_spdif_out(hdsp);
return 0;
}
static int snd_hdsp_put_spdif_out(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
{
hdsp_t *hdsp = snd_kcontrol_chip(kcontrol);
int change;
unsigned int val;
if (!snd_hdsp_use_is_exclusive(hdsp))
return -EBUSY;
val = ucontrol->value.integer.value[0] & 1;
spin_lock_irq(&hdsp->lock);
change = (int)val != hdsp_spdif_out(hdsp);
hdsp_set_spdif_output(hdsp, val);
spin_unlock_irq(&hdsp->lock);
return change;
}
#define HDSP_SPDIF_PROFESSIONAL(xname, xindex) \
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
.info = snd_hdsp_info_spdif_bits, \
.get = snd_hdsp_get_spdif_professional, .put = snd_hdsp_put_spdif_professional }
static int hdsp_spdif_professional(hdsp_t *hdsp)
{
return (hdsp->control_register & HDSP_SPDIFProfessional) ? 1 : 0;
}
static int hdsp_set_spdif_professional(hdsp_t *hdsp, int val)
{
if (val)
hdsp->control_register |= HDSP_SPDIFProfessional;
else
hdsp->control_register &= ~HDSP_SPDIFProfessional;
hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
return 0;
}
static int snd_hdsp_get_spdif_professional(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
{
hdsp_t *hdsp = snd_kcontrol_chip(kcontrol);
ucontrol->value.integer.value[0] = hdsp_spdif_professional(hdsp);
return 0;
}
static int snd_hdsp_put_spdif_professional(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
{
hdsp_t *hdsp = snd_kcontrol_chip(kcontrol);
int change;
unsigned int val;
if (!snd_hdsp_use_is_exclusive(hdsp))
return -EBUSY;
val = ucontrol->value.integer.value[0] & 1;
spin_lock_irq(&hdsp->lock);
change = (int)val != hdsp_spdif_professional(hdsp);
hdsp_set_spdif_professional(hdsp, val);
spin_unlock_irq(&hdsp->lock);
return change;
}
#define HDSP_SPDIF_EMPHASIS(xname, xindex) \
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
.info = snd_hdsp_info_spdif_bits, \
.get = snd_hdsp_get_spdif_emphasis, .put = snd_hdsp_put_spdif_emphasis }
static int hdsp_spdif_emphasis(hdsp_t *hdsp)
{
return (hdsp->control_register & HDSP_SPDIFEmphasis) ? 1 : 0;
}
static int hdsp_set_spdif_emphasis(hdsp_t *hdsp, int val)
{
if (val)
hdsp->control_register |= HDSP_SPDIFEmphasis;
else
hdsp->control_register &= ~HDSP_SPDIFEmphasis;
hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
return 0;
}
static int snd_hdsp_get_spdif_emphasis(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
{
hdsp_t *hdsp = snd_kcontrol_chip(kcontrol);
ucontrol->value.integer.value[0] = hdsp_spdif_emphasis(hdsp);
return 0;
}
static int snd_hdsp_put_spdif_emphasis(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
{
hdsp_t *hdsp = snd_kcontrol_chip(kcontrol);
int change;
unsigned int val;
if (!snd_hdsp_use_is_exclusive(hdsp))
return -EBUSY;
val = ucontrol->value.integer.value[0] & 1;
spin_lock_irq(&hdsp->lock);
change = (int)val != hdsp_spdif_emphasis(hdsp);
hdsp_set_spdif_emphasis(hdsp, val);
spin_unlock_irq(&hdsp->lock);
return change;
}
#define HDSP_SPDIF_NON_AUDIO(xname, xindex) \
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
.info = snd_hdsp_info_spdif_bits, \
.get = snd_hdsp_get_spdif_nonaudio, .put = snd_hdsp_put_spdif_nonaudio }
static int hdsp_spdif_nonaudio(hdsp_t *hdsp)
{
return (hdsp->control_register & HDSP_SPDIFNonAudio) ? 1 : 0;
}
static int hdsp_set_spdif_nonaudio(hdsp_t *hdsp, int val)
{
if (val)
hdsp->control_register |= HDSP_SPDIFNonAudio;
else
hdsp->control_register &= ~HDSP_SPDIFNonAudio;
hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
return 0;
}
static int snd_hdsp_get_spdif_nonaudio(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
{
hdsp_t *hdsp = snd_kcontrol_chip(kcontrol);
ucontrol->value.integer.value[0] = hdsp_spdif_nonaudio(hdsp);
return 0;
}
static int snd_hdsp_put_spdif_nonaudio(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
{
hdsp_t *hdsp = snd_kcontrol_chip(kcontrol);
int change;
unsigned int val;
if (!snd_hdsp_use_is_exclusive(hdsp))
return -EBUSY;
val = ucontrol->value.integer.value[0] & 1;
spin_lock_irq(&hdsp->lock);
change = (int)val != hdsp_spdif_nonaudio(hdsp);
hdsp_set_spdif_nonaudio(hdsp, val);
spin_unlock_irq(&hdsp->lock);
return change;
}
#define HDSP_SPDIF_SAMPLE_RATE(xname, xindex) \
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
.name = xname, \
.index = xindex, \
.access = SNDRV_CTL_ELEM_ACCESS_READ, \
.info = snd_hdsp_info_spdif_sample_rate, \
.get = snd_hdsp_get_spdif_sample_rate \
}
static int snd_hdsp_info_spdif_sample_rate(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
{
static char *texts[] = {"32000", "44100", "48000", "64000", "88200", "96000", "None", "128000", "176400", "192000"};
hdsp_t *hdsp = snd_kcontrol_chip(kcontrol);
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;
}
static int snd_hdsp_get_spdif_sample_rate(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
{
hdsp_t *hdsp = snd_kcontrol_chip(kcontrol);
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:
ucontrol->value.enumerated.item[0] = 6;
}
return 0;
}
#define HDSP_SYSTEM_SAMPLE_RATE(xname, xindex) \
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
.name = xname, \
.index = xindex, \
.access = SNDRV_CTL_ELEM_ACCESS_READ, \
.info = snd_hdsp_info_system_sample_rate, \
.get = snd_hdsp_get_system_sample_rate \
}
static int snd_hdsp_info_system_sample_rate(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
{
uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
uinfo->count = 1;
return 0;
}
static int snd_hdsp_get_system_sample_rate(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
{
hdsp_t *hdsp = snd_kcontrol_chip(kcontrol);
ucontrol->value.enumerated.item[0] = hdsp->system_sample_rate;
return 0;
}
#define HDSP_AUTOSYNC_SAMPLE_RATE(xname, xindex) \
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
.name = xname, \
.index = xindex, \
.access = SNDRV_CTL_ELEM_ACCESS_READ, \
.info = snd_hdsp_info_autosync_sample_rate, \
.get = snd_hdsp_get_autosync_sample_rate \
}
static int snd_hdsp_info_autosync_sample_rate(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
{
hdsp_t *hdsp = snd_kcontrol_chip(kcontrol);
static char *texts[] = {"32000", "44100", "48000", "64000", "88200", "96000", "None", "128000", "176400", "192000"};
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;
}
static int snd_hdsp_get_autosync_sample_rate(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
{
hdsp_t *hdsp = snd_kcontrol_chip(kcontrol);
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;
break;
default:
ucontrol->value.enumerated.item[0] = 6;
}
return 0;
}
#define HDSP_SYSTEM_CLOCK_MODE(xname, xindex) \
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
.name = xname, \
.index = xindex, \
.access = SNDRV_CTL_ELEM_ACCESS_READ, \
.info = snd_hdsp_info_system_clock_mode, \
.get = snd_hdsp_get_system_clock_mode \
}
static int hdsp_system_clock_mode(hdsp_t *hdsp)
{
if (hdsp->control_register & HDSP_ClockModeMaster)
return 0;
else if (hdsp_external_sample_rate(hdsp) != hdsp->system_sample_rate)
return 0;
return 1;
}
static int snd_hdsp_info_system_clock_mode(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
{
static char *texts[] = {"Master", "Slave" };
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;
}
static int snd_hdsp_get_system_clock_mode(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
{
hdsp_t *hdsp = snd_kcontrol_chip(kcontrol);
ucontrol->value.enumerated.item[0] = hdsp_system_clock_mode(hdsp);
return 0;
}
#define HDSP_CLOCK_SOURCE(xname, xindex) \
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
.name = xname, \
.index = xindex, \
.info = snd_hdsp_info_clock_source, \
.get = snd_hdsp_get_clock_source, \
.put = snd_hdsp_put_clock_source \
}
static int hdsp_clock_source(hdsp_t *hdsp)
{
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:
return 3;
}
} else {
return 0;
}
}
static int hdsp_set_clock_source(hdsp_t *hdsp, int mode)
{
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)) {
hdsp->control_register &= ~HDSP_ClockModeMaster;
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;
break;
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;
}
static int snd_hdsp_info_clock_source(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
{
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" };
hdsp_t *hdsp = snd_kcontrol_chip(kcontrol);
uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
uinfo->count = 1;
if (hdsp->io_type == H9632)
uinfo->value.enumerated.items = 10;
else
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;
}
static int snd_hdsp_get_clock_source(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
{
hdsp_t *hdsp = snd_kcontrol_chip(kcontrol);
ucontrol->value.enumerated.item[0] = hdsp_clock_source(hdsp);
return 0;
}
static int snd_hdsp_put_clock_source(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
{
hdsp_t *hdsp = snd_kcontrol_chip(kcontrol);
int change;
int val;
if (!snd_hdsp_use_is_exclusive(hdsp))
return -EBUSY;
val = ucontrol->value.enumerated.item[0];
if (val < 0) val = 0;
if (hdsp->io_type == H9632) {
if (val > 9)
val = 9;
} else {
if (val > 6)
val = 6;
}
spin_lock_irq(&hdsp->lock);
if (val != hdsp_clock_source(hdsp))
change = (hdsp_set_clock_source(hdsp, val) == 0) ? 1 : 0;
else
change = 0;
spin_unlock_irq(&hdsp->lock);
return change;
}
static int snd_hdsp_info_clock_source_lock(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
{
uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
uinfo->count = 1;
uinfo->value.integer.min = 0;
uinfo->value.integer.max = 1;
return 0;
}
static int snd_hdsp_get_clock_source_lock(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
{
hdsp_t *hdsp = snd_kcontrol_chip(kcontrol);
ucontrol->value.integer.value[0] = hdsp->clock_source_locked;
return 0;
}
static int snd_hdsp_put_clock_source_lock(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
{
hdsp_t *hdsp = snd_kcontrol_chip(kcontrol);
int change;
change = (int)ucontrol->value.integer.value[0] != hdsp->clock_source_locked;
if (change)
hdsp->clock_source_locked = ucontrol->value.integer.value[0];
return change;
}
#define HDSP_DA_GAIN(xname, xindex) \
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
.name = xname, \
.index = xindex, \
.info = snd_hdsp_info_da_gain, \
.get = snd_hdsp_get_da_gain, \
.put = snd_hdsp_put_da_gain \
}
static int hdsp_da_gain(hdsp_t *hdsp)
{
switch (hdsp->control_register & HDSP_DAGainMask) {
case HDSP_DAGainHighGain:
return 0;
case HDSP_DAGainPlus4dBu:
return 1;
case HDSP_DAGainMinus10dBV:
return 2;
default:
return 1;
}
}
static int hdsp_set_da_gain(hdsp_t *hdsp, int mode)
{
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:
hdsp->control_register |= HDSP_DAGainMinus10dBV;
break;
default:
return -1;
}
hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
return 0;
}
static int snd_hdsp_info_da_gain(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
{
static char *texts[] = {"Hi Gain", "+4 dBu", "-10 dbV"};
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;
}
static int snd_hdsp_get_da_gain(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
{
hdsp_t *hdsp = snd_kcontrol_chip(kcontrol);
ucontrol->value.enumerated.item[0] = hdsp_da_gain(hdsp);
return 0;
}
static int snd_hdsp_put_da_gain(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
{
hdsp_t *hdsp = snd_kcontrol_chip(kcontrol);
int change;
int val;
if (!snd_hdsp_use_is_exclusive(hdsp))
return -EBUSY;
val = ucontrol->value.enumerated.item[0];
if (val < 0) val = 0;
if (val > 2) val = 2;
spin_lock_irq(&hdsp->lock);
if (val != hdsp_da_gain(hdsp))
change = (hdsp_set_da_gain(hdsp, val) == 0) ? 1 : 0;
else
change = 0;
spin_unlock_irq(&hdsp->lock);
return change;
}
#define HDSP_AD_GAIN(xname, xindex) \
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
.name = xname, \
.index = xindex, \
.info = snd_hdsp_info_ad_gain, \
.get = snd_hdsp_get_ad_gain, \
.put = snd_hdsp_put_ad_gain \
}
static int hdsp_ad_gain(hdsp_t *hdsp)
{
switch (hdsp->control_register & HDSP_ADGainMask) {
case HDSP_ADGainMinus10dBV:
return 0;
case HDSP_ADGainPlus4dBu:
return 1;
case HDSP_ADGainLowGain:
return 2;
default:
return 1;
}
}
static int hdsp_set_ad_gain(hdsp_t *hdsp, int mode)
{
hdsp->control_register &= ~HDSP_ADGainMask;
switch (mode) {
case 0:
hdsp->control_register |= HDSP_ADGainMinus10dBV;
break;
case 1:
hdsp->control_register |= HDSP_ADGainPlus4dBu;
break;
case 2:
hdsp->control_register |= HDSP_ADGainLowGain;
break;
default:
return -1;
}
hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
return 0;
}
static int snd_hdsp_info_ad_gain(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
{
static char *texts[] = {"-10 dBV", "+4 dBu", "Lo Gain"};
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;
}
static int snd_hdsp_get_ad_gain(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
{
hdsp_t *hdsp = snd_kcontrol_chip(kcontrol);
ucontrol->value.enumerated.item[0] = hdsp_ad_gain(hdsp);
return 0;
}
static int snd_hdsp_put_ad_gain(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
{
hdsp_t *hdsp = snd_kcontrol_chip(kcontrol);
int change;
int val;
if (!snd_hdsp_use_is_exclusive(hdsp))
return -EBUSY;
val = ucontrol->value.enumerated.item[0];
if (val < 0) val = 0;
if (val > 2) val = 2;
spin_lock_irq(&hdsp->lock);
if (val != hdsp_ad_gain(hdsp))
change = (hdsp_set_ad_gain(hdsp, val) == 0) ? 1 : 0;
else
change = 0;
spin_unlock_irq(&hdsp->lock);
return change;
}
#define HDSP_PHONE_GAIN(xname, xindex) \
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
.name = xname, \
.index = xindex, \
.info = snd_hdsp_info_phone_gain, \
.get = snd_hdsp_get_phone_gain, \
.put = snd_hdsp_put_phone_gain \
}
static int hdsp_phone_gain(hdsp_t *hdsp)
{
switch (hdsp->control_register & HDSP_PhoneGainMask) {
case HDSP_PhoneGain0dB:
return 0;
case HDSP_PhoneGainMinus6dB:
return 1;
case HDSP_PhoneGainMinus12dB:
return 2;
default:
return 0;
}
}
static int hdsp_set_phone_gain(hdsp_t *hdsp, int mode)
{
hdsp->control_register &= ~HDSP_PhoneGainMask;
switch (mode) {
case 0:
hdsp->control_register |= HDSP_PhoneGain0dB;
break;
case 1:
hdsp->control_register |= HDSP_PhoneGainMinus6dB;
break;
case 2:
hdsp->control_register |= HDSP_PhoneGainMinus12dB;
break;
default:
return -1;
}
hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
return 0;
}
static int snd_hdsp_info_phone_gain(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
{
static char *texts[] = {"0 dB", "-6 dB", "-12 dB"};
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;
}
static int snd_hdsp_get_phone_gain(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
{
hdsp_t *hdsp = snd_kcontrol_chip(kcontrol);
ucontrol->value.enumerated.item[0] = hdsp_phone_gain(hdsp);
return 0;
}
static int snd_hdsp_put_phone_gain(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
{
hdsp_t *hdsp = snd_kcontrol_chip(kcontrol);
int change;
int val;
if (!snd_hdsp_use_is_exclusive(hdsp))
return -EBUSY;
val = ucontrol->value.enumerated.item[0];
if (val < 0) val = 0;
if (val > 2) val = 2;
spin_lock_irq(&hdsp->lock);
if (val != hdsp_phone_gain(hdsp))
change = (hdsp_set_phone_gain(hdsp, val) == 0) ? 1 : 0;
else
change = 0;
spin_unlock_irq(&hdsp->lock);
return change;
}
#define HDSP_XLR_BREAKOUT_CABLE(xname, xindex) \
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
.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 \
}
static int hdsp_xlr_breakout_cable(hdsp_t *hdsp)
{
if (hdsp->control_register & HDSP_XLRBreakoutCable)
return 1;
return 0;
}
static int hdsp_set_xlr_breakout_cable(hdsp_t *hdsp, int mode)
{
if (mode)
hdsp->control_register |= HDSP_XLRBreakoutCable;
else
hdsp->control_register &= ~HDSP_XLRBreakoutCable;
hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
return 0;
}
static int snd_hdsp_info_xlr_breakout_cable(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
{
uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
uinfo->count = 1;
uinfo->value.integer.min = 0;
uinfo->value.integer.max = 1;
return 0;
}
static int snd_hdsp_get_xlr_breakout_cable(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
{
hdsp_t *hdsp = snd_kcontrol_chip(kcontrol);
ucontrol->value.enumerated.item[0] = hdsp_xlr_breakout_cable(hdsp);
return 0;
}
static int snd_hdsp_put_xlr_breakout_cable(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
{
hdsp_t *hdsp = snd_kcontrol_chip(kcontrol);
int change;
int val;
if (!snd_hdsp_use_is_exclusive(hdsp))
return -EBUSY;
val = ucontrol->value.integer.value[0] & 1;
spin_lock_irq(&hdsp->lock);
change = (int)val != hdsp_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) \
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
.name = xname, \
.index = xindex, \
.info = snd_hdsp_info_aeb, \
.get = snd_hdsp_get_aeb, \
.put = snd_hdsp_put_aeb \
}
static int hdsp_aeb(hdsp_t *hdsp)
{
if (hdsp->control_register & HDSP_AnalogExtensionBoard)
return 1;
return 0;
}
static int hdsp_set_aeb(hdsp_t *hdsp, int mode)
{
if (mode)
hdsp->control_register |= HDSP_AnalogExtensionBoard;
else
hdsp->control_register &= ~HDSP_AnalogExtensionBoard;
hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
return 0;
}
static int snd_hdsp_info_aeb(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
{
uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
uinfo->count = 1;
uinfo->value.integer.min = 0;
uinfo->value.integer.max = 1;
return 0;
}
static int snd_hdsp_get_aeb(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
{
hdsp_t *hdsp = snd_kcontrol_chip(kcontrol);
ucontrol->value.enumerated.item[0] = hdsp_aeb(hdsp);
return 0;
}
static int snd_hdsp_put_aeb(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
{
hdsp_t *hdsp = snd_kcontrol_chip(kcontrol);
int change;
int val;
if (!snd_hdsp_use_is_exclusive(hdsp))
return -EBUSY;
val = ucontrol->value.integer.value[0] & 1;
spin_lock_irq(&hdsp->lock);
change = (int)val != hdsp_aeb(hdsp);
hdsp_set_aeb(hdsp, val);
spin_unlock_irq(&hdsp->lock);
return change;
}
#define HDSP_PREF_SYNC_REF(xname, xindex) \
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
.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 \
}
static int hdsp_pref_sync_ref(hdsp_t *hdsp)
{
/* 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;
}
static int hdsp_set_pref_sync_ref(hdsp_t *hdsp, int pref)
{
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;
}
static int snd_hdsp_info_pref_sync_ref(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
{
static char *texts[] = {"Word", "IEC958", "ADAT1", "ADAT Sync", "ADAT2", "ADAT3" };
hdsp_t *hdsp = snd_kcontrol_chip(kcontrol);
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;
}
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;
}
static int snd_hdsp_get_pref_sync_ref(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
{
hdsp_t *hdsp = snd_kcontrol_chip(kcontrol);
ucontrol->value.enumerated.item[0] = hdsp_pref_sync_ref(hdsp);
return 0;
}
static int snd_hdsp_put_pref_sync_ref(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
{
hdsp_t *hdsp = snd_kcontrol_chip(kcontrol);
int change, max;
unsigned int val;
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) \
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
.name = xname, \
.index = xindex, \
.access = SNDRV_CTL_ELEM_ACCESS_READ, \
.info = snd_hdsp_info_autosync_ref, \
.get = snd_hdsp_get_autosync_ref, \
}
static int hdsp_autosync_ref(hdsp_t *hdsp)
{
/* This looks at the autosync selected sync reference */
unsigned int status2 = hdsp_read(hdsp, HDSP_status2Reg