// SPDX-License-Identifier: GPL-2.0-or-later /* * ALSA driver for RME Hammerfall DSP MADI audio interface(s) * * Copyright (c) 2003 Winfried Ritsch (IEM) * code based on hdsp.c Paul Davis * Marcus Andersson * Thomas Charbonnel * Modified 2006-06-01 for AES32 support by Remy Bruno * <remy.bruno@trinnov.com> * * Modified 2009-04-13 for proper metering by Florian Faber * <faber@faberman.de> * * Modified 2009-04-14 for native float support by Florian Faber * <faber@faberman.de> * * Modified 2009-04-26 fixed bug in rms metering by Florian Faber * <faber@faberman.de> * * Modified 2009-04-30 added hw serial number support by Florian Faber * * Modified 2011-01-14 added S/PDIF input on RayDATs by Adrian Knoth * * Modified 2011-01-25 variable period sizes on RayDAT/AIO by Adrian Knoth * * Modified 2019-05-23 fix AIO single speed ADAT capture and playback * by Philippe.Bekaert@uhasselt.be
*/
/* --- Write registers. ---
These are defined as byte-offsets from the iobase value. */
#define HDSPM_WR_SETTINGS 0 #define HDSPM_outputBufferAddress 32 #define HDSPM_inputBufferAddress 36 #define HDSPM_controlRegister 64 #define HDSPM_interruptConfirmation 96 #define HDSPM_control2Reg 256 /* not in specs ???????? */ #define HDSPM_freqReg 256 /* for setting arbitrary clock values (DDS feature) */ #define HDSPM_midiDataOut0 352 /* just believe in old code */ #define HDSPM_midiDataOut1 356 #define HDSPM_eeprom_wr 384 /* for AES32 */
/* DMA enable for 64 channels, only Bit 0 is relevant */ #define HDSPM_outputEnableBase 512 /* 512-767 input DMA */ #define HDSPM_inputEnableBase 768 /* 768-1023 output DMA */
/* 16 page addresses for each of the 64 channels DMA buffer in and out
(each 64k=16*4k) Buffer must be 4k aligned (which is default i386 ????) */ #define HDSPM_pageAddressBufferOut 8192 #define HDSPM_pageAddressBufferIn (HDSPM_pageAddressBufferOut+64*16*4)
#define HDSPM_MADI_mixerBase 32768 /* 32768-65535 for 2x64x64 Fader */
/* --- Read registers. ---
These are defined as byte-offsets from the iobase value */ #define HDSPM_statusRegister 0 /*#define HDSPM_statusRegister2 96 */ /* after RME Windows driver sources, status2 is 4-byte word # 48 = word at * offset 192, for AES32 *and* MADI
* => need to check that offset 192 is working on MADI */ #define HDSPM_statusRegister2 192 #define HDSPM_timecodeRegister 128
/* 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 HDSPM_Latency0 (1<<1) /* buffer size = 2^n */ #define HDSPM_Latency1 (1<<2) /* where n is defined */ #define HDSPM_Latency2 (1<<3) /* by Latency{2,1,0} */
#define HDSPM_ClockModeMaster (1<<4) /* 1=Master, 0=Autosync */ #define HDSPM_c0Master 0x1 /* Master clock bit in settings
register [RayDAT, AIO] */
#define HDSPM_AudioInterruptEnable (1<<5) /* what do you think ? */
/* --- Status Register bits --- */ /* MADI ONLY */ /* Bits defined here and
that donot conflict with specific bits for AES32 seem to be valid also for the AES32
*/ #define HDSPM_audioIRQPending (1<<0) /* IRQ is high and pending */ #define HDSPM_RX_64ch (1<<1) /* Input 64chan. MODE=1, 56chn MODE=0 */ #define HDSPM_AB_int (1<<2) /* InputChannel Opt=0, Coax=1 * (like inp0)
*/
#define HDSPM_madiLock (1<<3) /* MADI Locked =1, no=0 */ #define HDSPM_madiSync (1<<18) /* MADI is in sync */
#define HDSPM_tcoLockMadi 0x00000020 /* Optional TCO locked status for HDSPe MADI*/ #define HDSPM_tcoSync 0x10000000 /* Optional TCO sync status for HDSPe MADI and AES32!*/
#define HDSPM_syncInLock 0x00010000 /* Sync In lock status for HDSPe MADI! */ #define HDSPM_syncInSync 0x00020000 /* Sync In sync status for HDSPe MADI! */
#define HDSPM_BufferPositionMask 0x000FFC0 /* Bit 6..15 : h/w buffer pointer */ /* since 64byte accurate, last 6 bits are not used */
#define HDSPM_DoubleSpeedStatus (1<<19) /* (input) card in double speed */
#define HDSPM_version0 (1<<0) /* not really defined but I guess */ #define HDSPM_version1 (1<<1) /* in former cards it was ??? */ #define HDSPM_version2 (1<<2)
#define HDSPM_wcLock (1<<3) /* Wordclock is detected and locked */ #define HDSPM_wcSync (1<<4) /* Wordclock is in sync with systemclock */
/* the size of a substream (1 mono data stream) */ #define HDSPM_CHANNEL_BUFFER_SAMPLES (16*1024) #define HDSPM_CHANNEL_BUFFER_BYTES (4*HDSPM_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, and also the latency to use. for one direction !!!
*/ #define HDSPM_DMA_AREA_BYTES (HDSPM_MAX_CHANNELS * HDSPM_CHANNEL_BUFFER_BYTES) #define HDSPM_DMA_AREA_KILOBYTES (HDSPM_DMA_AREA_BYTES/1024)
/* 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 refers 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.
*/
struct hdspm_midi { struct hdspm *hdspm; int id; struct snd_rawmidi *rmidi; struct snd_rawmidi_substream *input; struct snd_rawmidi_substream *output; char istimer; /* timer in use */ struct timer_list timer;
spinlock_t lock; int pending; int dataIn; int statusIn; int dataOut; int statusOut; int ie; int irq;
};
struct hdspm_tco { int input; /* 0: LTC, 1:Video, 2: WC*/ int framerate; /* 0=24, 1=25, 2=29.97, 3=29.97d, 4=30, 5=30d */ int wordclock; /* 0=1:1, 1=44.1->48, 2=48->44.1 */ int samplerate; /* 0=44.1, 1=48, 2= freq from app */ int pull; /* 0=0, 1=+0.1%, 2=-0.1%, 3=+4%, 4=-4%*/ int term; /* 0 = off, 1 = on */
};
struct hdspm {
spinlock_t lock; /* only one playback and/or capture stream */ struct snd_pcm_substream *capture_substream; struct snd_pcm_substream *playback_substream;
char *card_name; /* for procinfo */ unsignedshort firmware_rev; /* dont know if relevant (yes if AES32)*/
uint8_t io_type;
int monitor_outs; /* set up monitoring outs init flag */
u32 control_register; /* cached value */
u32 control2_register; /* cached value */
u32 settings_register; /* cached value for AIO / RayDat (sync reference, master/slave) */
pid_t capture_pid; /* process id which uses capture */
pid_t playback_pid; /* process id which uses capture */ int running; /* running status */
int last_external_sample_rate; /* samplerate mystic ... */ int last_internal_sample_rate; int system_sample_rate;
int dev; /* Hardware vars... */ int irq; unsignedlong port; void __iomem *iobase;
int irq_count; /* for debug */ int midiPorts;
struct snd_card *card; /* one card */ struct snd_pcm *pcm; /* has one pcm */ struct snd_hwdep *hwdep; /* and a hwdep for additional ioctl */ struct pci_dev *pci; /* and an pci info */
/* Mixer vars */ /* fast alsa mixer */ struct snd_kcontrol *playback_mixer_ctls[HDSPM_MAX_CHANNELS]; /* but input to much, so not used */ struct snd_kcontrol *input_mixer_ctls[HDSPM_MAX_CHANNELS]; /* full mixer accessible over mixer ioctl or hwdep-device */ struct hdspm_mixer *mixer;
struct hdspm_tco *tco; /* NULL if no TCO detected */
constchar *const *texts_autosync; int texts_autosync_items;
/* for each output channel (chan) I have an Input (in) and Playback (pb) Fader mixer is write only on hardware so we have to cache him for read
each fader is a u32, but uses only the first 16 bit */
staticinlineint hdspm_read_in_gain(struct hdspm * hdspm, unsignedint chan, unsignedint in)
{ if (chan >= HDSPM_MIXER_CHANNELS || in >= HDSPM_MIXER_CHANNELS) return 0;
/* enable DMA for specific channels, now available for DSP-MADI */ staticinlinevoid snd_hdspm_enable_in(struct hdspm * hdspm, int i, int v)
{
hdspm_write(hdspm, HDSPM_inputEnableBase + (4 * i), v);
}
staticinlinevoid snd_hdspm_enable_out(struct hdspm * hdspm, int i, int v)
{
hdspm_write(hdspm, HDSPM_outputEnableBase + (4 * i), v);
}
/* check if same process is writing and reading */ staticint snd_hdspm_use_is_exclusive(struct hdspm *hdspm)
{ unsignedlong flags; int ret = 1;
/* round arbitrary sample rates to commonly known rates */ staticint hdspm_round_frequency(int rate)
{ if (rate < 38050) return 32000; if (rate < 46008) return 44100; else return 48000;
}
/* QS and DS rates normally can not be detected * automatically by the card. Only exception is MADI * in 96k frame mode. * * So if we read SS values (32 .. 48k), check for * user-provided DS/QS bits in the control register * and multiply the base frequency accordingly.
*/ staticint hdspm_rate_multiplier(struct hdspm *hdspm, int rate)
{ if (rate <= 48000) { if (hdspm->control_register & HDSPM_QuadSpeed) return rate * 4; elseif (hdspm->control_register &
HDSPM_DoubleSpeed) return rate * 2;
} return rate;
}
/* check for external sample rate, returns the sample rate in Hz*/ staticint hdspm_external_sample_rate(struct hdspm *hdspm)
{ unsignedint status, status2; int syncref, rate = 0, rate_bits;
switch (hdspm->io_type) { case AES32:
status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
status = hdspm_read(hdspm, HDSPM_statusRegister);
syncref = hdspm_autosync_ref(hdspm); switch (syncref) { case HDSPM_AES32_AUTOSYNC_FROM_WORD: /* Check WC sync and get sample rate */ if (hdspm_wc_sync_check(hdspm)) return HDSPM_bit2freq(hdspm_get_wc_sample_rate(hdspm)); break;
case HDSPM_AES32_AUTOSYNC_FROM_AES1: case HDSPM_AES32_AUTOSYNC_FROM_AES2: case HDSPM_AES32_AUTOSYNC_FROM_AES3: case HDSPM_AES32_AUTOSYNC_FROM_AES4: case HDSPM_AES32_AUTOSYNC_FROM_AES5: case HDSPM_AES32_AUTOSYNC_FROM_AES6: case HDSPM_AES32_AUTOSYNC_FROM_AES7: case HDSPM_AES32_AUTOSYNC_FROM_AES8: /* Check AES sync and get sample rate */ if (hdspm_aes_sync_check(hdspm, syncref - HDSPM_AES32_AUTOSYNC_FROM_AES1)) return HDSPM_bit2freq(hdspm_get_aes_sample_rate(hdspm,
syncref - HDSPM_AES32_AUTOSYNC_FROM_AES1)); break;
case HDSPM_AES32_AUTOSYNC_FROM_TCO: /* Check TCO sync and get sample rate */ if (hdspm_tco_sync_check(hdspm)) return HDSPM_bit2freq(hdspm_get_tco_sample_rate(hdspm)); break; default: return 0;
} /* end switch(syncref) */ break;
case MADIface:
status = hdspm_read(hdspm, HDSPM_statusRegister);
if (!(status & HDSPM_madiLock)) {
rate = 0; /* no lock */
} else { switch (status & (HDSPM_status1_freqMask)) { case HDSPM_status1_F_0*1:
rate = 32000; break; case HDSPM_status1_F_0*2:
rate = 44100; break; case HDSPM_status1_F_0*3:
rate = 48000; break; case HDSPM_status1_F_0*4:
rate = 64000; break; case HDSPM_status1_F_0*5:
rate = 88200; break; case HDSPM_status1_F_0*6:
rate = 96000; break; case HDSPM_status1_F_0*7:
rate = 128000; break; case HDSPM_status1_F_0*8:
rate = 176400; break; case HDSPM_status1_F_0*9:
rate = 192000; break; default:
rate = 0; break;
}
}
break;
case MADI: case AIO: case RayDAT:
status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
status = hdspm_read(hdspm, HDSPM_statusRegister);
rate = 0;
/* if wordclock has synced freq and wordclock is valid */ if ((status2 & HDSPM_wcLock) != 0 &&
(status2 & HDSPM_SelSyncRef0) == 0) {
rate_bits = status2 & HDSPM_wcFreqMask;
switch (rate_bits) { case HDSPM_wcFreq32:
rate = 32000; break; case HDSPM_wcFreq44_1:
rate = 44100; break; case HDSPM_wcFreq48:
rate = 48000; break; case HDSPM_wcFreq64:
rate = 64000; break; case HDSPM_wcFreq88_2:
rate = 88200; break; case HDSPM_wcFreq96:
rate = 96000; break; case HDSPM_wcFreq128:
rate = 128000; break; case HDSPM_wcFreq176_4:
rate = 176400; break; case HDSPM_wcFreq192:
rate = 192000; break; default:
rate = 0; break;
}
}
/* if rate detected and Syncref is Word than have it, * word has priority to MADI
*/ if (rate != 0 &&
(status2 & HDSPM_SelSyncRefMask) == HDSPM_SelSyncRef_WORD) return hdspm_rate_multiplier(hdspm, rate);
/* maybe a madi input (which is taken if sel sync is madi) */ if (status & HDSPM_madiLock) {
rate_bits = status & HDSPM_madiFreqMask;
switch (rate_bits) { case HDSPM_madiFreq32:
rate = 32000; break; case HDSPM_madiFreq44_1:
rate = 44100; break; case HDSPM_madiFreq48:
rate = 48000; break; case HDSPM_madiFreq64:
rate = 64000; break; case HDSPM_madiFreq88_2:
rate = 88200; break; case HDSPM_madiFreq96:
rate = 96000; break; case HDSPM_madiFreq128:
rate = 128000; break; case HDSPM_madiFreq176_4:
rate = 176400; break; case HDSPM_madiFreq192:
rate = 192000; break; default:
rate = 0; break;
}
} /* endif HDSPM_madiLock */
/* check sample rate from TCO or SYNC_IN */
{ bool is_valid_input = 0; bool has_sync = 0;
/* return latency in samples per period */ staticint hdspm_get_latency(struct hdspm *hdspm)
{ int n;
n = hdspm_decode_latency(hdspm->control_register);
/* Special case for new RME cards with 32 samples period size. * The three latency bits in the control register * (HDSP_LatencyMask) encode latency values of 64 samples as * 0, 128 samples as 1 ... 4096 samples as 6. For old cards, 7 * denotes 8192 samples, but on new cards like RayDAT or AIO, * it corresponds to 32 samples.
*/ if ((7 == n) && (RayDAT == hdspm->io_type || AIO == hdspm->io_type))
n = -1;
/* should I silence all or only opened ones ? doit all for first even is 4MB*/ staticvoid hdspm_silence_playback(struct hdspm *hdspm)
{ int i; int n = hdspm->period_bytes; void *buf = hdspm->playback_buffer;
if (!buf) return;
for (i = 0; i < HDSPM_MAX_CHANNELS; i++) {
memset(buf, 0, n);
buf += HDSPM_CHANNEL_BUFFER_BYTES;
}
}
staticint hdspm_set_interrupt_interval(struct hdspm *s, unsignedint frames)
{ int n;
spin_lock_irq(&s->lock);
if (32 == frames) { /* Special case for new RME cards like RayDAT/AIO which * support period sizes of 32 samples. Since latency is * encoded in the three bits of HDSP_LatencyMask, we can only * have values from 0 .. 7. While 0 still means 64 samples and * 6 represents 4096 samples on all cards, 7 represents 8192 * on older cards and 32 samples on new cards. * * In other words, period size in samples is calculated by * 2^(n+6) with n ranging from 0 .. 7.
*/
n = 7;
} else {
frames >>= 7;
n = 0; while (frames) {
n++;
frames >>= 1;
}
}
switch (hdspm->io_type) { case MADIface:
n = 131072000000000ULL; /* 125 MHz */ break; case MADI: case AES32:
n = 110069313433624ULL; /* 105 MHz */ break; case RayDAT: case AIO:
n = 104857600000000ULL; /* 100 MHz */ break; default:
snd_BUG(); return;
}
n = div_u64(n, rate); /* n should be less than 2^32 for being written to FREQ register */
snd_BUG_ON(n >> 32);
hdspm_write(hdspm, HDSPM_freqReg, (u32)n);
}
/* dummy set rate lets see what happens */ staticint hdspm_set_rate(struct hdspm * hdspm, int rate, int called_internally)
{ int current_rate; int rate_bits; int not_set = 0; int current_speed, target_speed;
/* ASSUMPTION: hdspm->lock is either set, or there is no need for it (e.g. during module initialization).
*/
if (!(hdspm->control_register & HDSPM_ClockModeMaster)) {
/* SLAVE --- */ if (called_internally) {
/* request from ctl or card initialization just make a warning an remember setting
for future master mode switching */
dev_warn(hdspm->card->dev, "Warning: device is not running as a clock master.\n");
not_set = 1;
} else {
/* hw_param request while in AutoSync mode */ int external_freq =
hdspm_external_sample_rate(hdspm);
if (hdspm_autosync_ref(hdspm) ==
HDSPM_AUTOSYNC_FROM_NONE) {
dev_warn(hdspm->card->dev, "Detected no External Sync\n");
not_set = 1;
} elseif (rate != external_freq) {
dev_warn(hdspm->card->dev, "Warning: No AutoSync source for requested rate\n");
not_set = 1;
}
}
}
current_rate = hdspm->system_sample_rate;
/* Changing between Singe, Double and Quad speed 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.
*/
/* mainly for init to 0 on load */ staticvoid all_in_all_mixer(struct hdspm * hdspm, int sgain)
{ int i, j; unsignedint gain;
if (sgain > UNITY_GAIN)
gain = UNITY_GAIN; elseif (sgain < 0)
gain = 0; else
gain = sgain;
for (i = 0; i < HDSPM_MIXER_CHANNELS; i++) for (j = 0; j < HDSPM_MIXER_CHANNELS; j++) {
hdspm_write_in_gain(hdspm, i, j, gain);
hdspm_write_pb_gain(hdspm, i, j, gain);
}
}
staticinlineunsignedchar snd_hdspm_midi_read_byte (struct hdspm *hdspm, int id)
{ /* the hardware already does the relevant bit-mask with 0xff */ return hdspm_read(hdspm, hdspm->midi[id].dataIn);
}
staticinlinevoid snd_hdspm_midi_write_byte (struct hdspm *hdspm, int id, int val)
{ /* the hardware already does the relevant bit-mask with 0xff */ return hdspm_write(hdspm, hdspm->midi[id].dataOut, val);
}
staticvoid snd_hdspm_flush_midi_input(struct hdspm *hdspm, int id)
{ int count = 256;
while (snd_hdspm_midi_input_available(hdspm, id) && --count)
snd_hdspm_midi_read_byte(hdspm, id);
}
staticint snd_hdspm_midi_output_write (struct hdspm_midi *hmidi)
{ unsignedlong flags; int n_pending; int to_write; int i; unsignedchar buf[128];
/* Output is not interrupt driven */
spin_lock_irqsave (&hmidi->lock, flags); if (hmidi->output &&
!snd_rawmidi_transmit_empty (hmidi->output)) {
n_pending = snd_hdspm_midi_output_possible (hmidi->hdspm,
hmidi->id); if (n_pending > 0) { if (n_pending > (int)sizeof (buf))
n_pending = sizeof (buf);
to_write = snd_rawmidi_transmit (hmidi->output, buf,
n_pending); if (to_write > 0) { for (i = 0; i < to_write; ++i)
snd_hdspm_midi_write_byte (hmidi->hdspm,
hmidi->id,
buf[i]);
}
}
}
spin_unlock_irqrestore (&hmidi->lock, flags); return 0;
}
staticint snd_hdspm_midi_input_read (struct hdspm_midi *hmidi)
{ unsignedchar buf[128]; /* this buffer is designed to match the MIDI * input FIFO size
*/ unsignedlong flags; int n_pending; int i;
spin_lock_irqsave (&hmidi->lock, flags);
n_pending = snd_hdspm_midi_input_available (hmidi->hdspm, hmidi->id); if (n_pending > 0) { if (hmidi->input) { if (n_pending > (int)sizeof (buf))
n_pending = sizeof (buf); for (i = 0; i < n_pending; ++i)
buf[i] = snd_hdspm_midi_read_byte (hmidi->hdspm,
hmidi->id); if (n_pending)
snd_rawmidi_receive (hmidi->input, buf,
n_pending);
} else { /* flush the MIDI input FIFO */ while (n_pending--)
snd_hdspm_midi_read_byte (hmidi->hdspm,
hmidi->id);
}
}
hmidi->pending = 0;
spin_unlock_irqrestore(&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)
mod_timer(&hmidi->timer, 1 + jiffies);
¤ Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.0.31Bemerkung:
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.