2019-05-30 07:57:59 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2017-03-31 21:06:09 +08:00
|
|
|
/*
|
|
|
|
* ff-stream.c - a part of driver for RME Fireface series
|
|
|
|
*
|
|
|
|
* Copyright (c) 2015-2017 Takashi Sakamoto
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ff.h"
|
|
|
|
|
2021-05-20 12:01:54 +08:00
|
|
|
#define READY_TIMEOUT_MS 200
|
2017-03-31 21:06:09 +08:00
|
|
|
|
2018-12-16 16:32:30 +08:00
|
|
|
int snd_ff_stream_get_multiplier_mode(enum cip_sfc sfc,
|
|
|
|
enum snd_ff_stream_mode *mode)
|
2017-03-31 21:06:09 +08:00
|
|
|
{
|
2018-12-16 16:32:30 +08:00
|
|
|
static const enum snd_ff_stream_mode modes[] = {
|
|
|
|
[CIP_SFC_32000] = SND_FF_STREAM_MODE_LOW,
|
|
|
|
[CIP_SFC_44100] = SND_FF_STREAM_MODE_LOW,
|
|
|
|
[CIP_SFC_48000] = SND_FF_STREAM_MODE_LOW,
|
|
|
|
[CIP_SFC_88200] = SND_FF_STREAM_MODE_MID,
|
|
|
|
[CIP_SFC_96000] = SND_FF_STREAM_MODE_MID,
|
|
|
|
[CIP_SFC_176400] = SND_FF_STREAM_MODE_HIGH,
|
|
|
|
[CIP_SFC_192000] = SND_FF_STREAM_MODE_HIGH,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (sfc >= CIP_SFC_COUNT)
|
2017-03-31 21:06:09 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
2018-12-16 16:32:30 +08:00
|
|
|
*mode = modes[sfc];
|
2017-03-31 21:06:09 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void finish_session(struct snd_ff *ff)
|
|
|
|
{
|
|
|
|
ff->spec->protocol->finish_session(ff);
|
2019-01-20 16:25:49 +08:00
|
|
|
ff->spec->protocol->switch_fetching_mode(ff, false);
|
2017-03-31 21:06:09 +08:00
|
|
|
}
|
|
|
|
|
2019-08-04 14:21:29 +08:00
|
|
|
static int init_stream(struct snd_ff *ff, struct amdtp_stream *s)
|
2017-03-31 21:06:09 +08:00
|
|
|
{
|
|
|
|
struct fw_iso_resources *resources;
|
2019-08-04 14:21:29 +08:00
|
|
|
enum amdtp_stream_direction dir;
|
|
|
|
int err;
|
2017-03-31 21:06:09 +08:00
|
|
|
|
2019-08-04 14:21:29 +08:00
|
|
|
if (s == &ff->tx_stream) {
|
2017-03-31 21:06:09 +08:00
|
|
|
resources = &ff->tx_resources;
|
2019-08-04 14:21:29 +08:00
|
|
|
dir = AMDTP_IN_STREAM;
|
2017-03-31 21:06:09 +08:00
|
|
|
} else {
|
|
|
|
resources = &ff->rx_resources;
|
2019-08-04 14:21:29 +08:00
|
|
|
dir = AMDTP_OUT_STREAM;
|
2017-03-31 21:06:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
err = fw_iso_resources_init(resources, ff->unit);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
|
2019-08-04 14:21:29 +08:00
|
|
|
err = amdtp_ff_init(s, ff->unit, dir);
|
2017-03-31 21:06:09 +08:00
|
|
|
if (err < 0)
|
|
|
|
fw_iso_resources_destroy(resources);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2019-08-04 14:21:29 +08:00
|
|
|
static void destroy_stream(struct snd_ff *ff, struct amdtp_stream *s)
|
2017-03-31 21:06:09 +08:00
|
|
|
{
|
2019-08-04 14:21:29 +08:00
|
|
|
amdtp_stream_destroy(s);
|
|
|
|
|
|
|
|
if (s == &ff->tx_stream)
|
2017-03-31 21:06:09 +08:00
|
|
|
fw_iso_resources_destroy(&ff->tx_resources);
|
2019-08-04 14:21:29 +08:00
|
|
|
else
|
2017-03-31 21:06:09 +08:00
|
|
|
fw_iso_resources_destroy(&ff->rx_resources);
|
|
|
|
}
|
|
|
|
|
|
|
|
int snd_ff_stream_init_duplex(struct snd_ff *ff)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
2019-08-04 14:21:29 +08:00
|
|
|
err = init_stream(ff, &ff->rx_stream);
|
2017-03-31 21:06:09 +08:00
|
|
|
if (err < 0)
|
2019-08-04 14:21:29 +08:00
|
|
|
return err;
|
2017-03-31 21:06:09 +08:00
|
|
|
|
2019-08-04 14:21:29 +08:00
|
|
|
err = init_stream(ff, &ff->tx_stream);
|
2019-08-04 14:21:37 +08:00
|
|
|
if (err < 0) {
|
2019-08-04 14:21:29 +08:00
|
|
|
destroy_stream(ff, &ff->rx_stream);
|
2019-08-04 14:21:37 +08:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = amdtp_domain_init(&ff->domain);
|
|
|
|
if (err < 0) {
|
|
|
|
destroy_stream(ff, &ff->rx_stream);
|
|
|
|
destroy_stream(ff, &ff->tx_stream);
|
|
|
|
}
|
2019-08-04 14:21:29 +08:00
|
|
|
|
2017-03-31 21:06:09 +08:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function should be called before starting streams or after stopping
|
|
|
|
* streams.
|
|
|
|
*/
|
|
|
|
void snd_ff_stream_destroy_duplex(struct snd_ff *ff)
|
|
|
|
{
|
2019-08-04 14:21:37 +08:00
|
|
|
amdtp_domain_destroy(&ff->domain);
|
|
|
|
|
2019-08-04 14:21:29 +08:00
|
|
|
destroy_stream(ff, &ff->rx_stream);
|
|
|
|
destroy_stream(ff, &ff->tx_stream);
|
2017-03-31 21:06:09 +08:00
|
|
|
}
|
|
|
|
|
2019-10-07 19:05:24 +08:00
|
|
|
int snd_ff_stream_reserve_duplex(struct snd_ff *ff, unsigned int rate,
|
2019-10-17 23:54:21 +08:00
|
|
|
unsigned int frames_per_period,
|
|
|
|
unsigned int frames_per_buffer)
|
2017-03-31 21:06:09 +08:00
|
|
|
{
|
|
|
|
unsigned int curr_rate;
|
|
|
|
enum snd_ff_clock_src src;
|
|
|
|
int err;
|
|
|
|
|
2019-01-20 16:25:50 +08:00
|
|
|
err = ff->spec->protocol->get_clock(ff, &curr_rate, &src);
|
2017-03-31 21:06:09 +08:00
|
|
|
if (err < 0)
|
|
|
|
return err;
|
2019-06-02 15:12:56 +08:00
|
|
|
|
|
|
|
if (ff->substreams_counter == 0 || curr_rate != rate) {
|
|
|
|
enum snd_ff_stream_mode mode;
|
|
|
|
int i;
|
2017-03-31 21:06:09 +08:00
|
|
|
|
2019-08-04 14:21:37 +08:00
|
|
|
amdtp_domain_stop(&ff->domain);
|
2019-06-02 15:12:56 +08:00
|
|
|
finish_session(ff);
|
2017-03-31 21:06:09 +08:00
|
|
|
|
2019-06-02 15:12:56 +08:00
|
|
|
fw_iso_resources_free(&ff->tx_resources);
|
|
|
|
fw_iso_resources_free(&ff->rx_resources);
|
2018-12-16 16:32:31 +08:00
|
|
|
|
|
|
|
for (i = 0; i < CIP_SFC_COUNT; ++i) {
|
|
|
|
if (amdtp_rate_table[i] == rate)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i >= CIP_SFC_COUNT)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
err = snd_ff_stream_get_multiplier_mode(i, &mode);
|
2017-03-31 21:06:09 +08:00
|
|
|
if (err < 0)
|
2018-12-16 16:32:31 +08:00
|
|
|
return err;
|
|
|
|
|
|
|
|
err = amdtp_ff_set_parameters(&ff->tx_stream, rate,
|
|
|
|
ff->spec->pcm_capture_channels[mode]);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
err = amdtp_ff_set_parameters(&ff->rx_stream, rate,
|
|
|
|
ff->spec->pcm_playback_channels[mode]);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
2017-03-31 21:06:09 +08:00
|
|
|
|
2019-06-02 15:12:52 +08:00
|
|
|
err = ff->spec->protocol->allocate_resources(ff, rate);
|
|
|
|
if (err < 0)
|
2019-06-02 15:12:56 +08:00
|
|
|
return err;
|
2019-10-07 19:05:24 +08:00
|
|
|
|
|
|
|
err = amdtp_domain_set_events_per_period(&ff->domain,
|
2019-10-17 23:54:21 +08:00
|
|
|
frames_per_period, frames_per_buffer);
|
2019-10-07 19:05:24 +08:00
|
|
|
if (err < 0) {
|
|
|
|
fw_iso_resources_free(&ff->tx_resources);
|
|
|
|
fw_iso_resources_free(&ff->rx_resources);
|
|
|
|
return err;
|
|
|
|
}
|
2019-06-02 15:12:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int snd_ff_stream_start_duplex(struct snd_ff *ff, unsigned int rate)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (ff->substreams_counter == 0)
|
|
|
|
return 0;
|
2019-06-02 15:12:52 +08:00
|
|
|
|
2019-06-02 15:12:56 +08:00
|
|
|
if (amdtp_streaming_error(&ff->tx_stream) ||
|
2019-08-04 14:21:37 +08:00
|
|
|
amdtp_streaming_error(&ff->rx_stream)) {
|
|
|
|
amdtp_domain_stop(&ff->domain);
|
2019-06-02 15:12:56 +08:00
|
|
|
finish_session(ff);
|
2019-08-04 14:21:37 +08:00
|
|
|
}
|
2019-06-02 15:12:56 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Regardless of current source of clock signal, drivers transfer some
|
|
|
|
* packets. Then, the device transfers packets.
|
|
|
|
*/
|
|
|
|
if (!amdtp_stream_running(&ff->rx_stream)) {
|
2019-08-04 14:21:37 +08:00
|
|
|
int spd = fw_parent_device(ff->unit)->max_speed;
|
|
|
|
|
2017-03-31 21:06:09 +08:00
|
|
|
err = ff->spec->protocol->begin_session(ff, rate);
|
|
|
|
if (err < 0)
|
|
|
|
goto error;
|
|
|
|
|
2019-08-04 14:21:37 +08:00
|
|
|
err = amdtp_domain_add_stream(&ff->domain, &ff->rx_stream,
|
|
|
|
ff->rx_resources.channel, spd);
|
2017-03-31 21:06:09 +08:00
|
|
|
if (err < 0)
|
|
|
|
goto error;
|
|
|
|
|
2019-08-04 14:21:37 +08:00
|
|
|
err = amdtp_domain_add_stream(&ff->domain, &ff->tx_stream,
|
|
|
|
ff->tx_resources.channel, spd);
|
2017-03-31 21:06:09 +08:00
|
|
|
if (err < 0)
|
|
|
|
goto error;
|
|
|
|
|
2021-05-31 10:51:03 +08:00
|
|
|
// NOTE: The device doesn't transfer packets unless receiving any packet. The
|
|
|
|
// sequence of tx packets includes cycle skip corresponding to empty packet or
|
|
|
|
// NODATA packet in IEC 61883-1/6. The sequence of the number of data blocks per
|
|
|
|
// packet is important for media clock recovery.
|
|
|
|
err = amdtp_domain_start(&ff->domain, 0, true, true);
|
2017-03-31 21:06:09 +08:00
|
|
|
if (err < 0)
|
|
|
|
goto error;
|
|
|
|
|
2021-05-20 12:01:54 +08:00
|
|
|
if (!amdtp_domain_wait_ready(&ff->domain, READY_TIMEOUT_MS)) {
|
2017-03-31 21:06:09 +08:00
|
|
|
err = -ETIMEDOUT;
|
|
|
|
goto error;
|
|
|
|
}
|
2019-08-04 14:21:37 +08:00
|
|
|
|
|
|
|
err = ff->spec->protocol->switch_fetching_mode(ff, true);
|
|
|
|
if (err < 0)
|
|
|
|
goto error;
|
2017-03-31 21:06:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
error:
|
2019-08-04 14:21:37 +08:00
|
|
|
amdtp_domain_stop(&ff->domain);
|
2017-03-31 21:06:09 +08:00
|
|
|
finish_session(ff);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
void snd_ff_stream_stop_duplex(struct snd_ff *ff)
|
|
|
|
{
|
2019-06-18 21:26:16 +08:00
|
|
|
if (ff->substreams_counter == 0) {
|
2019-08-04 14:21:37 +08:00
|
|
|
amdtp_domain_stop(&ff->domain);
|
2019-06-02 15:12:58 +08:00
|
|
|
finish_session(ff);
|
2019-06-18 21:26:16 +08:00
|
|
|
|
|
|
|
fw_iso_resources_free(&ff->tx_resources);
|
|
|
|
fw_iso_resources_free(&ff->rx_resources);
|
|
|
|
}
|
2017-03-31 21:06:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void snd_ff_stream_update_duplex(struct snd_ff *ff)
|
|
|
|
{
|
2019-08-04 14:21:37 +08:00
|
|
|
amdtp_domain_stop(&ff->domain);
|
|
|
|
|
2019-06-02 15:12:57 +08:00
|
|
|
// The device discontinue to transfer packets.
|
2017-03-31 21:06:09 +08:00
|
|
|
amdtp_stream_pcm_abort(&ff->tx_stream);
|
|
|
|
amdtp_stream_pcm_abort(&ff->rx_stream);
|
|
|
|
}
|
2017-03-31 21:06:11 +08:00
|
|
|
|
|
|
|
void snd_ff_stream_lock_changed(struct snd_ff *ff)
|
|
|
|
{
|
|
|
|
ff->dev_lock_changed = true;
|
|
|
|
wake_up(&ff->hwdep_wait);
|
|
|
|
}
|
|
|
|
|
|
|
|
int snd_ff_stream_lock_try(struct snd_ff *ff)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
spin_lock_irq(&ff->lock);
|
|
|
|
|
|
|
|
/* user land lock this */
|
|
|
|
if (ff->dev_lock_count < 0) {
|
|
|
|
err = -EBUSY;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* this is the first time */
|
|
|
|
if (ff->dev_lock_count++ == 0)
|
|
|
|
snd_ff_stream_lock_changed(ff);
|
|
|
|
err = 0;
|
|
|
|
end:
|
|
|
|
spin_unlock_irq(&ff->lock);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
void snd_ff_stream_lock_release(struct snd_ff *ff)
|
|
|
|
{
|
|
|
|
spin_lock_irq(&ff->lock);
|
|
|
|
|
|
|
|
if (WARN_ON(ff->dev_lock_count <= 0))
|
|
|
|
goto end;
|
|
|
|
if (--ff->dev_lock_count == 0)
|
|
|
|
snd_ff_stream_lock_changed(ff);
|
|
|
|
end:
|
|
|
|
spin_unlock_irq(&ff->lock);
|
|
|
|
}
|