[alsa-devel] [PATCH 4/8] oxfw: Add connections and streams management

Takashi Sakamoto o-takashi at sakamocchi.jp
Sun Jan 5 12:13:33 CET 2014


This commit adds management functionality for connections and streams.
OXFW970/971 use CMP to manage connections and uses AMDTP for streams.

They transmits AMDTP stream in non-blocking mode. This stream has a quirk for
'presentation timestamp'. The sequence of 'presentation timestamp' is invalid
even if header of packet shows 'CIP header with SYT field'. So this driver
can't reuse it for out-stream.

In this reason, this driver keep to send correct 'presentation timestamp' in
out-stream.

And I note that its fluctuation of 'presentation timestamp' is less when
out-stream exists (they receive AMDTP stream) than when no out-stream exists.

no out-stream exists:
Index	Payload	CIP_Header_0	CIP_Header_1
38	14	00020092	900103D1
39	12	00020098	900102FF
40	12	0002009D	9001027F
41	14	000200A2	90010396
42	14	000200A8	900102E8
43	12	000200AE	90010219
44	14	000200B3	90010331
45	12	000200B9	9001025F
46	14	000200BE	90010376
47	12	000200C4	900102A1
00	12	000200C9	9001023E
01	14	000200CE	90010358
02	12	000200D4	90010289
03	16	000200D9	900103A3
04	12	000200E0	900102DD
05	14	000200E5	900103F1
06	12	000200EB	90010335
07	12	000200F0	90010263
08	14	000200F5	9001037C
09	12	000200FB	900102AE

out-stream exists:
Index	Payload	CIP_Header_0	CIP_Header_1
38	12	000200BD	900104A8
39	14	000200C2	900104A8
40	12	000200C8	900104AC
41	14	000200CD	900104A9
42	12	000200D3	900104B1
43	14	000200D8	900104A8
44	12	000200DE	900104AA
45	14	000200E3	900104A9
46	14	000200E9	900104AE
47	12	000200EF	900104A8
00	14	000200F4	900104AD
01	12	000200FA	900104A7
02	14	000200FF	900104A9
03	12	00020005	900104A9
04	14	0002000A	900104B1
05	12	00020010	900104AA
06	14	00020015	900104AD
07	12	0002001B	900104A7
08	14	00020020	900104AC
09	12	00020026	900104A7

Signed-off-by: Takashi Sakamoto <o-takashi at sakamocchi.jp>
---
 sound/firewire/oxfw/oxfw.c        |  10 ++
 sound/firewire/oxfw/oxfw.h        |  18 ++
 sound/firewire/oxfw/oxfw_stream.c | 357 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 385 insertions(+)

diff --git a/sound/firewire/oxfw/oxfw.c b/sound/firewire/oxfw/oxfw.c
index c030fc0..f8320af 100644
--- a/sound/firewire/oxfw/oxfw.c
+++ b/sound/firewire/oxfw/oxfw.c
@@ -134,6 +134,14 @@ oxfw_probe(struct fw_unit *unit,
 	if (err < 0)
 		goto error;
 
+	err = snd_oxfw_stream_discover(oxfw);
+	if (err < 0)
+		goto error;
+
+	err = snd_oxfw_stream_init_duplex(oxfw);
+	if (err < 0)
+		goto error;
+
 	snd_card_set_dev(card, &unit->device);
 	err = snd_card_register(card);
 	if (err < 0) {
@@ -158,6 +166,7 @@ oxfw_update(struct fw_unit *unit)
 	struct snd_oxfw *oxfw = dev_get_drvdata(&unit->device);
 
 	fcp_bus_reset(oxfw->unit);
+	snd_oxfw_stream_update_duplex(oxfw);
 }
 
 static void
@@ -165,6 +174,7 @@ oxfw_remove(struct fw_unit *unit)
 {
 	struct snd_oxfw *oxfw = dev_get_drvdata(&unit->device);
 
+	snd_oxfw_stream_destroy_duplex(oxfw);
 	snd_card_disconnect(oxfw->card);
 	snd_card_free_when_closed(oxfw->card);
 }
diff --git a/sound/firewire/oxfw/oxfw.h b/sound/firewire/oxfw/oxfw.h
index e82717a..34ea0c8 100644
--- a/sound/firewire/oxfw/oxfw.h
+++ b/sound/firewire/oxfw/oxfw.h
@@ -24,6 +24,9 @@
 #include "../lib.h"
 #include "../fcp.h"
 #include "../amdtp.h"
+#include "../packets-buffer.h"
+#include "../iso-resources.h"
+#include "../cmp.h"
 
 #define	SND_OXFW_RATE_TABLE_ENTRIES	7
 struct snd_oxfw_stream_formation {
@@ -49,6 +52,11 @@ struct snd_oxfw {
 
 	unsigned int midi_input_ports;
 	unsigned int midi_output_ports;
+
+	struct cmp_connection out_conn;
+	struct amdtp_stream tx_stream;
+	struct cmp_connection in_conn;
+	struct amdtp_stream rx_stream;
 };
 
 /* AV/C Stream Format Information Specification 1.1 (Apr 2005, 1394TA) */
@@ -86,6 +94,16 @@ int snd_oxfw_set_rate(struct snd_oxfw *oxfw, unsigned int rate,
 		      enum avc_general_plug_dir dir);
 
 /* for AMDTP streaming */
+int snd_oxfw_stream_get_rate(struct snd_oxfw *oxfw, unsigned int *rate);
+int snd_oxfw_stream_set_rate(struct snd_oxfw *oxfw, unsigned int rate);
+int snd_oxfw_stream_init_duplex(struct snd_oxfw *oxfw);
+int snd_oxfw_stream_start_duplex(struct snd_oxfw *oxfw,
+				  struct amdtp_stream *stream,
+				   unsigned int sampling_rate);
+int snd_oxfw_stream_stop_duplex(struct snd_oxfw *oxfw);
+void snd_oxfw_stream_update_duplex(struct snd_oxfw *oxfw);
+void snd_oxfw_stream_destroy_duplex(struct snd_oxfw *oxfw);
+
 int snd_oxfw_stream_discover(struct snd_oxfw *oxfw);
 
 #define SND_OXFW_DEV_ENTRY(vendor, model) \
diff --git a/sound/firewire/oxfw/oxfw_stream.c b/sound/firewire/oxfw/oxfw_stream.c
index 60ad700..ad2dd86 100644
--- a/sound/firewire/oxfw/oxfw_stream.c
+++ b/sound/firewire/oxfw/oxfw_stream.c
@@ -17,6 +17,15 @@
  * FORMAT INFORMATION' defined in 'AV/C Stream Format Information
  * Specification 1.1 (Apr 2005, 1394TA)'. So this module uses an assumption
  * that OXFW970 doesn't change its formation of channels in AMDTP stream.
+ *
+ * They transmit packet following to 'CIP header with  SYT field' defined in
+ * IEC 61883-1. But the sequence of value in SYT field is not compliant. So
+ * this module doesn't use the value of SYT field in in-packets. Then this
+ * module performs as a 'master of synchronization'. In this way, this module
+ * hopes the device to pick up the value of SYT value in out-packet which
+ * this module transmits. But the device seems not to use it for in-packet
+ * which the device transmits. Concluding, it doesn't matter whether this
+ * module perform as a master or slave.
  */
 const unsigned int snd_oxfw_rate_table[SND_OXFW_RATE_TABLE_ENTRIES] = {
 	[0] = 32000,
@@ -42,6 +51,354 @@ static const unsigned int avc_stream_rate_table[] = {
 	[6] = 0x07,
 };
 
+int snd_oxfw_stream_get_rate(struct snd_oxfw *oxfw, unsigned int *curr_rate)
+{
+	unsigned int tx_rate, rx_rate;
+	int err;
+
+	err = snd_oxfw_get_rate(oxfw, &tx_rate, AVC_GENERAL_PLUG_DIR_OUT);
+	if (err < 0)
+		goto end;
+
+	err = snd_oxfw_get_rate(oxfw, &rx_rate, AVC_GENERAL_PLUG_DIR_IN);
+	if (err < 0)
+		goto end;
+
+	*curr_rate = rx_rate;
+	if (rx_rate == tx_rate)
+		goto end;
+
+	/* synchronize receive stream rate to transmit stream rate */
+	err = snd_oxfw_set_rate(oxfw, rx_rate, AVC_GENERAL_PLUG_DIR_IN);
+end:
+	return err;
+}
+
+int snd_oxfw_stream_set_rate(struct snd_oxfw *oxfw, unsigned int rate)
+{
+	int err;
+
+	err = snd_oxfw_set_rate(oxfw, rate, AVC_GENERAL_PLUG_DIR_OUT);
+	if (err < 0)
+		goto end;
+
+	err = snd_oxfw_set_rate(oxfw, rate, AVC_GENERAL_PLUG_DIR_IN);
+end:
+	return err;
+}
+
+static int
+check_connection_used_by_others(struct snd_oxfw *oxfw,
+				struct amdtp_stream *s, bool *used)
+{
+	struct cmp_connection *conn;
+	int err;
+
+	if (s == &oxfw->tx_stream)
+		conn = &oxfw->out_conn;
+	else
+		conn = &oxfw->in_conn;
+
+	err = cmp_connection_check_used(conn, used);
+	if (err >= 0)
+		*used = (*used && !amdtp_stream_running(s));
+
+	return err;
+}
+
+static int
+init_stream(struct snd_oxfw *oxfw, struct amdtp_stream *stream)
+{
+	struct cmp_connection *conn;
+	enum cmp_direction c_dir;
+	enum amdtp_stream_direction s_dir;
+	int err;
+
+	if (stream == &oxfw->tx_stream) {
+		conn = &oxfw->out_conn;
+		c_dir = CMP_OUTPUT;
+		s_dir = AMDTP_IN_STREAM;
+	} else {
+		conn = &oxfw->in_conn;
+		c_dir = CMP_INPUT;
+		s_dir = AMDTP_OUT_STREAM;
+	}
+
+	err = cmp_connection_init(conn, oxfw->unit, c_dir, 0);
+	if (err < 0)
+		goto end;
+
+	err = amdtp_stream_init(stream, oxfw->unit, s_dir, CIP_NONBLOCKING);
+	if (err < 0)
+		cmp_connection_destroy(conn);
+end:
+	return err;
+}
+
+
+static void
+stop_stream(struct snd_oxfw *oxfw, struct amdtp_stream *stream)
+{
+	if (amdtp_stream_running(stream))
+		amdtp_stream_stop(stream);
+
+	if (stream == &oxfw->tx_stream)
+		cmp_connection_break(&oxfw->out_conn);
+	else
+		cmp_connection_break(&oxfw->in_conn);
+
+	return;
+}
+
+static int
+start_stream(struct snd_oxfw *oxfw, struct amdtp_stream *stream,
+	     unsigned int sampling_rate)
+{
+	struct cmp_connection *conn;
+	unsigned int i, pcm_channels, midi_ports;
+	int err;
+
+	/* already running */
+	if (amdtp_stream_running(stream)) {
+		err = 0;
+		goto end;
+	}
+
+	for (i = 0; i < sizeof(snd_oxfw_rate_table); i++) {
+		if (snd_oxfw_rate_table[i] == sampling_rate)
+			break;
+	}
+	if (i == sizeof(snd_oxfw_rate_table)) {
+		err = -EINVAL;
+		goto end;
+	}
+
+	/* set stream formation */
+	if (stream == &oxfw->tx_stream) {
+		conn = &oxfw->out_conn;
+		pcm_channels = oxfw->tx_stream_formations[i].pcm;
+		midi_ports = oxfw->tx_stream_formations[i].midi * 8;
+	} else {
+		conn = &oxfw->in_conn;
+		pcm_channels = oxfw->rx_stream_formations[i].pcm;
+		midi_ports = oxfw->rx_stream_formations[i].midi * 8;
+	}
+	amdtp_stream_set_parameters(stream, sampling_rate,
+				    pcm_channels, midi_ports);
+
+	/*  establish connection via CMP */
+	err = cmp_connection_establish(conn,
+				amdtp_stream_get_max_payload(stream));
+	if (err < 0)
+		goto end;
+
+	/* start amdtp stream */
+	err = amdtp_stream_start(stream,
+				 conn->resources.channel,
+				 conn->speed);
+	if (err < 0)
+		stop_stream(oxfw, stream);
+
+	/* wait first callback */
+	if (!amdtp_stream_wait_callback(stream)) {
+		stop_stream(oxfw, stream);
+		err = -ETIMEDOUT;
+	}
+end:
+	return err;
+}
+
+static void
+update_stream(struct snd_oxfw *oxfw, struct amdtp_stream *stream)
+{
+	struct cmp_connection *conn;
+
+	if (&oxfw->tx_stream == stream)
+		conn = &oxfw->out_conn;
+	else
+		conn = &oxfw->in_conn;
+
+	if (cmp_connection_update(conn) < 0) {
+		amdtp_stream_pcm_abort(stream);
+		mutex_lock(&oxfw->mutex);
+		stop_stream(oxfw, stream);
+		mutex_unlock(&oxfw->mutex);
+		return;
+	}
+	amdtp_stream_update(stream);
+}
+
+static void
+destroy_stream(struct snd_oxfw *oxfw, struct amdtp_stream *stream)
+{
+	stop_stream(oxfw, stream);
+
+	if (stream == &oxfw->tx_stream)
+		cmp_connection_destroy(&oxfw->out_conn);
+	else
+		cmp_connection_destroy(&oxfw->in_conn);
+}
+
+static int
+get_roles(struct snd_oxfw *oxfw, enum cip_flags *sync_mode,
+	  struct amdtp_stream **master, struct amdtp_stream **slave)
+{
+	/* It doesn't matter. So this module perform as a sync master */
+	*sync_mode = 0x00;
+	*master = &oxfw->rx_stream;
+	*slave = &oxfw->tx_stream;
+
+	return 0;
+}
+
+int snd_oxfw_stream_init_duplex(struct snd_oxfw *oxfw)
+{
+	int err;
+
+	err = init_stream(oxfw, &oxfw->tx_stream);
+	if (err < 0)
+		goto end;
+
+	err = init_stream(oxfw, &oxfw->rx_stream);
+end:
+	return err;
+}
+
+int snd_oxfw_stream_start_duplex(struct snd_oxfw *oxfw,
+				  struct amdtp_stream *request,
+				  unsigned int rate)
+{
+	struct amdtp_stream *master, *slave;
+	enum cip_flags sync_mode;
+	unsigned int curr_rate;
+	bool slave_flag, used;
+	int err;
+
+	mutex_lock(&oxfw->mutex);
+
+	err = get_roles(oxfw, &sync_mode, &master, &slave);
+	if (err < 0)
+		goto end;
+
+	if ((request == slave) || amdtp_stream_running(slave))
+		slave_flag = true;
+	else
+		slave_flag = false;
+
+	/*
+	 * Considering JACK/FFADO streaming:
+	 * TODO: This can be removed hwdep functionality becomes popular.
+	 */
+	err = check_connection_used_by_others(oxfw, master, &used);
+	if (err < 0)
+		goto end;
+	if (used) {
+		dev_err(&oxfw->unit->device,
+			"connections established by others: %d\n",
+			used);
+		err = -EBUSY;
+		goto end;
+	}
+
+	/* get current rate */
+	err = snd_oxfw_stream_get_rate(oxfw, &curr_rate);
+	if (err < 0)
+		goto end;
+	if (rate == 0)
+		rate = curr_rate;
+
+	/* change sampling rate if needed */
+	if (rate != curr_rate) {
+		/* slave is just for MIDI stream */
+		if (amdtp_stream_running(slave) &&
+		    !amdtp_stream_pcm_running(slave))
+			amdtp_stream_stop(slave);
+
+		/* master is just for MIDI stream */
+		if (amdtp_stream_running(master) &&
+		    !amdtp_stream_pcm_running(master))
+			amdtp_stream_stop(master);
+
+		err = snd_oxfw_stream_set_rate(oxfw, rate);
+		if (err < 0)
+			goto end;
+	}
+
+	/* master should be always running */
+	if (!amdtp_stream_running(master)) {
+		err = start_stream(oxfw, master, rate);
+		if (err < 0) {
+			dev_err(&oxfw->unit->device,
+				"fail to run AMDTP master stream:%d\n", err);
+			goto end;
+		}
+	}
+
+	/* start slave if needed */
+	if (slave_flag && !amdtp_stream_running(slave)) {
+		err = start_stream(oxfw, slave, rate);
+		if (err < 0)
+			dev_err(&oxfw->unit->device,
+				"fail to run AMDTP slave stream:%d\n", err);
+	}
+end:
+	mutex_unlock(&oxfw->mutex);
+	return err;
+}
+
+int snd_oxfw_stream_stop_duplex(struct snd_oxfw *oxfw)
+{
+	struct amdtp_stream *master, *slave;
+	enum cip_flags sync_mode;
+	int err;
+
+	mutex_lock(&oxfw->mutex);
+
+	err = get_roles(oxfw, &sync_mode, &master, &slave);
+	if (err < 0)
+		goto end;
+
+	if (amdtp_stream_pcm_running(slave) ||
+	    amdtp_stream_midi_running(slave))
+		goto end;
+
+	stop_stream(oxfw, slave);
+
+	if (amdtp_stream_pcm_running(master) ||
+	    amdtp_stream_midi_running(master))
+		goto end;
+
+	stop_stream(oxfw, master);
+end:
+	mutex_unlock(&oxfw->mutex);
+	return err;
+}
+
+void snd_oxfw_stream_update_duplex(struct snd_oxfw *oxfw)
+{
+	mutex_lock(&oxfw->mutex);
+
+	update_stream(oxfw, &oxfw->rx_stream);
+	update_stream(oxfw, &oxfw->tx_stream);
+
+	mutex_unlock(&oxfw->mutex);
+}
+
+void snd_oxfw_stream_destroy_duplex(struct snd_oxfw *oxfw)
+{
+	mutex_lock(&oxfw->mutex);
+
+	if (amdtp_stream_pcm_running(&oxfw->rx_stream))
+		amdtp_stream_pcm_abort(&oxfw->rx_stream);
+	if (amdtp_stream_pcm_running(&oxfw->tx_stream))
+		amdtp_stream_pcm_abort(&oxfw->tx_stream);
+
+	destroy_stream(oxfw, &oxfw->rx_stream);
+	destroy_stream(oxfw, &oxfw->tx_stream);
+
+	mutex_unlock(&oxfw->mutex);
+}
+
 /*
  * See Table 6.16 - AM824 Stream Format
  *     Figure 6.19 - format_information field for AM824 Compound
-- 
1.8.3.2



More information about the Alsa-devel mailing list