Skip to content

Commit

Permalink
ALSA: firewire-motu: cache event ticks in source packet header per da…
Browse files Browse the repository at this point in the history
…ta block

The devices in MOTU FireWire series put source packet header (SPH) into
each data block of tx packet for presentation time of event. The format
of timestamp is compliant to IEC 61883-1, with cycle and offset fields
without sec field of 32 bit cycle time.

This commit takes ALSA firewire-motu driver to cache the presentation
time as offset from cycle in which the packet is transferred.

Signed-off-by: Takashi Sakamoto <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Takashi Iwai <[email protected]>
  • Loading branch information
takaswie authored and tiwai committed Jun 2, 2021
1 parent 138d1bc commit e50dfac
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
41 changes: 40 additions & 1 deletion sound/firewire/motu/amdtp-motu.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ struct amdtp_motu {

int midi_db_count;
unsigned int midi_db_interval;

struct amdtp_motu_cache *cache;
};

int amdtp_motu_set_parameters(struct amdtp_stream *s, unsigned int rate,
Expand Down Expand Up @@ -333,6 +335,34 @@ static void probe_tracepoints_events(struct amdtp_stream *s,
}
}

static void cache_event_offsets(struct amdtp_motu_cache *cache, const __be32 *buf,
unsigned int data_blocks, unsigned int data_block_quadlets)
{
unsigned int *event_offsets = cache->event_offsets;
const unsigned int cache_size = cache->size;
unsigned int cache_tail = cache->tail;
unsigned int base_tick = cache->tx_cycle_count * TICKS_PER_CYCLE;
int i;

for (i = 0; i < data_blocks; ++i) {
u32 sph = be32_to_cpu(*buf);
unsigned int tick;

tick = ((sph & CIP_SPH_CYCLE_MASK) >> CIP_SPH_CYCLE_SHIFT) * TICKS_PER_CYCLE +
(sph & CIP_SPH_OFFSET_MASK);

if (tick < base_tick)
tick += TICKS_PER_SECOND;
event_offsets[cache_tail] = tick - base_tick;

cache_tail = (cache_tail + 1) % cache_size;
buf += data_block_quadlets;
}

cache->tail = cache_tail;
cache->tx_cycle_count = (cache->tx_cycle_count + 1) % CYCLES_PER_SECOND;
}

static unsigned int process_ir_ctx_payloads(struct amdtp_stream *s,
const struct pkt_desc *descs,
unsigned int packets,
Expand All @@ -342,12 +372,17 @@ static unsigned int process_ir_ctx_payloads(struct amdtp_stream *s,
unsigned int pcm_frames = 0;
int i;

if (p->cache->tx_cycle_count == UINT_MAX)
p->cache->tx_cycle_count = (s->domain->processing_cycle.tx_start % CYCLES_PER_SECOND);

// For data block processing.
for (i = 0; i < packets; ++i) {
const struct pkt_desc *desc = descs + i;
__be32 *buf = desc->ctx_payload;
unsigned int data_blocks = desc->data_blocks;

cache_event_offsets(p->cache, buf, data_blocks, s->data_block_quadlets);

if (pcm) {
read_pcm_s32(s, pcm, buf, data_blocks, pcm_frames);
pcm_frames += data_blocks;
Expand Down Expand Up @@ -449,11 +484,12 @@ static unsigned int process_it_ctx_payloads(struct amdtp_stream *s,

int amdtp_motu_init(struct amdtp_stream *s, struct fw_unit *unit,
enum amdtp_stream_direction dir,
const struct snd_motu_spec *spec)
const struct snd_motu_spec *spec, struct amdtp_motu_cache *cache)
{
amdtp_stream_process_ctx_payloads_t process_ctx_payloads;
int fmt = CIP_FMT_MOTU;
unsigned int flags = CIP_BLOCKING | CIP_UNAWARE_SYT;
struct amdtp_motu *p;
int err;

if (dir == AMDTP_IN_STREAM) {
Expand Down Expand Up @@ -493,5 +529,8 @@ int amdtp_motu_init(struct amdtp_stream *s, struct fw_unit *unit,
s->ctx_data.rx.fdf = MOTU_FDF_AM824;
}

p = s->protocol;
p->cache = cache;

return 0;
}
20 changes: 19 additions & 1 deletion sound/firewire/motu/motu-stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ int snd_motu_stream_reserve_duplex(struct snd_motu *motu, unsigned int rate,
fw_iso_resources_free(&motu->tx_resources);
fw_iso_resources_free(&motu->rx_resources);

kfree(motu->cache.event_offsets);
motu->cache.event_offsets = NULL;

err = snd_motu_protocol_set_clock_rate(motu, rate);
if (err < 0) {
dev_err(&motu->unit->device,
Expand Down Expand Up @@ -181,6 +184,15 @@ int snd_motu_stream_reserve_duplex(struct snd_motu *motu, unsigned int rate,
fw_iso_resources_free(&motu->rx_resources);
return err;
}

motu->cache.size = motu->tx_stream.syt_interval * frames_per_buffer;
motu->cache.event_offsets = kcalloc(motu->cache.size, sizeof(*motu->cache.event_offsets),
GFP_KERNEL);
if (!motu->cache.event_offsets) {
fw_iso_resources_free(&motu->tx_resources);
fw_iso_resources_free(&motu->rx_resources);
return err;
}
}

return 0;
Expand Down Expand Up @@ -260,6 +272,9 @@ int snd_motu_stream_start_duplex(struct snd_motu *motu)
if (err < 0)
goto stop_streams;

motu->cache.tail = 0;
motu->cache.tx_cycle_count = UINT_MAX;

err = amdtp_domain_start(&motu->domain, 0, false, false);
if (err < 0)
goto stop_streams;
Expand Down Expand Up @@ -293,6 +308,9 @@ void snd_motu_stream_stop_duplex(struct snd_motu *motu)

fw_iso_resources_free(&motu->tx_resources);
fw_iso_resources_free(&motu->rx_resources);

kfree(motu->cache.event_offsets);
motu->cache.event_offsets = NULL;
}
}

Expand All @@ -314,7 +332,7 @@ static int init_stream(struct snd_motu *motu, struct amdtp_stream *s)
if (err < 0)
return err;

err = amdtp_motu_init(s, motu->unit, dir, motu->spec);
err = amdtp_motu_init(s, motu->unit, dir, motu->spec, &motu->cache);
if (err < 0)
fw_iso_resources_destroy(resources);

Expand Down
12 changes: 11 additions & 1 deletion sound/firewire/motu/motu.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ struct snd_motu_packet_format {
unsigned char pcm_chunks[3];
};

struct amdtp_motu_cache {
unsigned int *event_offsets;
unsigned int size;
unsigned int tail;
unsigned int tx_cycle_count;
};

struct snd_motu {
struct snd_card *card;
struct fw_unit *unit;
Expand Down Expand Up @@ -70,6 +77,8 @@ struct snd_motu {
wait_queue_head_t hwdep_wait;

struct amdtp_domain domain;

struct amdtp_motu_cache cache;
};

enum snd_motu_spec_flags {
Expand Down Expand Up @@ -125,7 +134,8 @@ extern const struct snd_motu_spec snd_motu_spec_4pre;

int amdtp_motu_init(struct amdtp_stream *s, struct fw_unit *unit,
enum amdtp_stream_direction dir,
const struct snd_motu_spec *spec);
const struct snd_motu_spec *spec,
struct amdtp_motu_cache *cache);
int amdtp_motu_set_parameters(struct amdtp_stream *s, unsigned int rate,
unsigned int midi_ports,
struct snd_motu_packet_format *formats);
Expand Down

0 comments on commit e50dfac

Please sign in to comment.