xref: /openbmc/linux/sound/firewire/motu/motu-stream.c (revision fc49769af4fdba92e8e4f7429e05bb20b0639f90)
19b2bb4f2STakashi Sakamoto /*
29b2bb4f2STakashi Sakamoto  * motu-stream.c - a part of driver for MOTU FireWire series
39b2bb4f2STakashi Sakamoto  *
49b2bb4f2STakashi Sakamoto  * Copyright (c) 2015-2017 Takashi Sakamoto <o-takashi@sakamocchi.jp>
59b2bb4f2STakashi Sakamoto  *
69b2bb4f2STakashi Sakamoto  * Licensed under the terms of the GNU General Public License, version 2.
79b2bb4f2STakashi Sakamoto  */
89b2bb4f2STakashi Sakamoto 
99b2bb4f2STakashi Sakamoto #include "motu.h"
109b2bb4f2STakashi Sakamoto 
119b2bb4f2STakashi Sakamoto #define	CALLBACK_TIMEOUT	200
129b2bb4f2STakashi Sakamoto 
139b2bb4f2STakashi Sakamoto #define ISOC_COMM_CONTROL_OFFSET		0x0b00
149b2bb4f2STakashi Sakamoto #define  ISOC_COMM_CONTROL_MASK			0xffff0000
159b2bb4f2STakashi Sakamoto #define  CHANGE_RX_ISOC_COMM_STATE		0x80000000
169b2bb4f2STakashi Sakamoto #define  RX_ISOC_COMM_IS_ACTIVATED		0x40000000
179b2bb4f2STakashi Sakamoto #define  RX_ISOC_COMM_CHANNEL_MASK		0x3f000000
189b2bb4f2STakashi Sakamoto #define  RX_ISOC_COMM_CHANNEL_SHIFT		24
199b2bb4f2STakashi Sakamoto #define  CHANGE_TX_ISOC_COMM_STATE		0x00800000
209b2bb4f2STakashi Sakamoto #define  TX_ISOC_COMM_IS_ACTIVATED		0x00400000
219b2bb4f2STakashi Sakamoto #define  TX_ISOC_COMM_CHANNEL_MASK		0x003f0000
229b2bb4f2STakashi Sakamoto #define  TX_ISOC_COMM_CHANNEL_SHIFT		16
239b2bb4f2STakashi Sakamoto 
249b2bb4f2STakashi Sakamoto #define PACKET_FORMAT_OFFSET			0x0b10
259b2bb4f2STakashi Sakamoto #define  TX_PACKET_EXCLUDE_DIFFERED_DATA_CHUNKS	0x00000080
269b2bb4f2STakashi Sakamoto #define  RX_PACKET_EXCLUDE_DIFFERED_DATA_CHUNKS	0x00000040
279b2bb4f2STakashi Sakamoto #define  TX_PACKET_TRANSMISSION_SPEED_MASK	0x0000000f
289b2bb4f2STakashi Sakamoto 
298350132eSTakashi Sakamoto static int keep_resources(struct snd_motu *motu, unsigned int rate,
308350132eSTakashi Sakamoto 			  struct amdtp_stream *stream)
319b2bb4f2STakashi Sakamoto {
328350132eSTakashi Sakamoto 	struct fw_iso_resources *resources;
338350132eSTakashi Sakamoto 	struct snd_motu_packet_format *packet_format;
349e796e7dSTakashi Sakamoto 	unsigned int midi_ports = 0;
359b2bb4f2STakashi Sakamoto 	int err;
369b2bb4f2STakashi Sakamoto 
378350132eSTakashi Sakamoto 	if (stream == &motu->rx_stream) {
388350132eSTakashi Sakamoto 		resources = &motu->rx_resources;
398350132eSTakashi Sakamoto 		packet_format = &motu->rx_packet_formats;
408350132eSTakashi Sakamoto 
418b460c76STakashi Sakamoto 		if ((motu->spec->flags & SND_MOTU_SPEC_RX_MIDI_2ND_Q) ||
428b460c76STakashi Sakamoto 		    (motu->spec->flags & SND_MOTU_SPEC_RX_MIDI_3RD_Q))
439e796e7dSTakashi Sakamoto 			midi_ports = 1;
448350132eSTakashi Sakamoto 	} else {
458350132eSTakashi Sakamoto 		resources = &motu->tx_resources;
468350132eSTakashi Sakamoto 		packet_format = &motu->tx_packet_formats;
479b2bb4f2STakashi Sakamoto 
488b460c76STakashi Sakamoto 		if ((motu->spec->flags & SND_MOTU_SPEC_TX_MIDI_2ND_Q) ||
498b460c76STakashi Sakamoto 		    (motu->spec->flags & SND_MOTU_SPEC_TX_MIDI_3RD_Q))
508b460c76STakashi Sakamoto 			midi_ports = 1;
518350132eSTakashi Sakamoto 	}
528b460c76STakashi Sakamoto 
538350132eSTakashi Sakamoto 	err = amdtp_motu_set_parameters(stream, rate, midi_ports,
548350132eSTakashi Sakamoto 					packet_format);
559b2bb4f2STakashi Sakamoto 	if (err < 0)
569b2bb4f2STakashi Sakamoto 		return err;
579b2bb4f2STakashi Sakamoto 
588350132eSTakashi Sakamoto 	return fw_iso_resources_allocate(resources,
598350132eSTakashi Sakamoto 				amdtp_stream_get_max_payload(stream),
609b2bb4f2STakashi Sakamoto 				fw_parent_device(motu->unit)->max_speed);
618350132eSTakashi Sakamoto }
628350132eSTakashi Sakamoto 
638350132eSTakashi Sakamoto static int start_both_streams(struct snd_motu *motu, unsigned int rate)
648350132eSTakashi Sakamoto {
658350132eSTakashi Sakamoto 	__be32 reg;
668350132eSTakashi Sakamoto 	u32 data;
678350132eSTakashi Sakamoto 	int err;
688350132eSTakashi Sakamoto 
698350132eSTakashi Sakamoto 	err = keep_resources(motu, rate, &motu->tx_stream);
709b2bb4f2STakashi Sakamoto 	if (err < 0)
719b2bb4f2STakashi Sakamoto 		return err;
729b2bb4f2STakashi Sakamoto 
738350132eSTakashi Sakamoto 	err = keep_resources(motu, rate, &motu->rx_stream);
749b2bb4f2STakashi Sakamoto 	if (err < 0)
759b2bb4f2STakashi Sakamoto 		return err;
769b2bb4f2STakashi Sakamoto 
778350132eSTakashi Sakamoto 	// Configure the unit to start isochronous communication.
789b2bb4f2STakashi Sakamoto 	err = snd_motu_transaction_read(motu, ISOC_COMM_CONTROL_OFFSET, &reg,
799b2bb4f2STakashi Sakamoto 					sizeof(reg));
809b2bb4f2STakashi Sakamoto 	if (err < 0)
819b2bb4f2STakashi Sakamoto 		return err;
829b2bb4f2STakashi Sakamoto 	data = be32_to_cpu(reg) & ~ISOC_COMM_CONTROL_MASK;
839b2bb4f2STakashi Sakamoto 
849b2bb4f2STakashi Sakamoto 	data |= CHANGE_RX_ISOC_COMM_STATE | RX_ISOC_COMM_IS_ACTIVATED |
859b2bb4f2STakashi Sakamoto 		(motu->rx_resources.channel << RX_ISOC_COMM_CHANNEL_SHIFT) |
869b2bb4f2STakashi Sakamoto 		CHANGE_TX_ISOC_COMM_STATE | TX_ISOC_COMM_IS_ACTIVATED |
879b2bb4f2STakashi Sakamoto 		(motu->tx_resources.channel << TX_ISOC_COMM_CHANNEL_SHIFT);
889b2bb4f2STakashi Sakamoto 
899b2bb4f2STakashi Sakamoto 	reg = cpu_to_be32(data);
909b2bb4f2STakashi Sakamoto 	return snd_motu_transaction_write(motu, ISOC_COMM_CONTROL_OFFSET, &reg,
919b2bb4f2STakashi Sakamoto 					  sizeof(reg));
929b2bb4f2STakashi Sakamoto }
939b2bb4f2STakashi Sakamoto 
949b2bb4f2STakashi Sakamoto static void stop_both_streams(struct snd_motu *motu)
959b2bb4f2STakashi Sakamoto {
969b2bb4f2STakashi Sakamoto 	__be32 reg;
979b2bb4f2STakashi Sakamoto 	u32 data;
989b2bb4f2STakashi Sakamoto 	int err;
999b2bb4f2STakashi Sakamoto 
1009b2bb4f2STakashi Sakamoto 	err = motu->spec->protocol->switch_fetching_mode(motu, false);
1019b2bb4f2STakashi Sakamoto 	if (err < 0)
1029b2bb4f2STakashi Sakamoto 		return;
1039b2bb4f2STakashi Sakamoto 
1049b2bb4f2STakashi Sakamoto 	err = snd_motu_transaction_read(motu, ISOC_COMM_CONTROL_OFFSET, &reg,
1059b2bb4f2STakashi Sakamoto 					sizeof(reg));
1069b2bb4f2STakashi Sakamoto 	if (err < 0)
1079b2bb4f2STakashi Sakamoto 		return;
1089b2bb4f2STakashi Sakamoto 	data = be32_to_cpu(reg);
1099b2bb4f2STakashi Sakamoto 
1109b2bb4f2STakashi Sakamoto 	data &= ~(RX_ISOC_COMM_IS_ACTIVATED | TX_ISOC_COMM_IS_ACTIVATED);
1119b2bb4f2STakashi Sakamoto 	data |= CHANGE_RX_ISOC_COMM_STATE | CHANGE_TX_ISOC_COMM_STATE;
1129b2bb4f2STakashi Sakamoto 
1139b2bb4f2STakashi Sakamoto 	reg = cpu_to_be32(data);
1149b2bb4f2STakashi Sakamoto 	snd_motu_transaction_write(motu, ISOC_COMM_CONTROL_OFFSET, &reg,
1159b2bb4f2STakashi Sakamoto 				   sizeof(reg));
1169b2bb4f2STakashi Sakamoto 
1179b2bb4f2STakashi Sakamoto 	fw_iso_resources_free(&motu->tx_resources);
1189b2bb4f2STakashi Sakamoto 	fw_iso_resources_free(&motu->rx_resources);
1199b2bb4f2STakashi Sakamoto }
1209b2bb4f2STakashi Sakamoto 
1219b2bb4f2STakashi Sakamoto static int start_isoc_ctx(struct snd_motu *motu, struct amdtp_stream *stream)
1229b2bb4f2STakashi Sakamoto {
1239b2bb4f2STakashi Sakamoto 	struct fw_iso_resources *resources;
1249b2bb4f2STakashi Sakamoto 	int err;
1259b2bb4f2STakashi Sakamoto 
1269b2bb4f2STakashi Sakamoto 	if (stream == &motu->rx_stream)
1279b2bb4f2STakashi Sakamoto 		resources = &motu->rx_resources;
1289b2bb4f2STakashi Sakamoto 	else
1299b2bb4f2STakashi Sakamoto 		resources = &motu->tx_resources;
1309b2bb4f2STakashi Sakamoto 
1319b2bb4f2STakashi Sakamoto 	err = amdtp_stream_start(stream, resources->channel,
1329b2bb4f2STakashi Sakamoto 				 fw_parent_device(motu->unit)->max_speed);
1339b2bb4f2STakashi Sakamoto 	if (err < 0)
1349b2bb4f2STakashi Sakamoto 		return err;
1359b2bb4f2STakashi Sakamoto 
1369b2bb4f2STakashi Sakamoto 	if (!amdtp_stream_wait_callback(stream, CALLBACK_TIMEOUT)) {
1379b2bb4f2STakashi Sakamoto 		amdtp_stream_stop(stream);
1389b2bb4f2STakashi Sakamoto 		fw_iso_resources_free(resources);
1399b2bb4f2STakashi Sakamoto 		return -ETIMEDOUT;
1409b2bb4f2STakashi Sakamoto 	}
1419b2bb4f2STakashi Sakamoto 
1429b2bb4f2STakashi Sakamoto 	return 0;
1439b2bb4f2STakashi Sakamoto }
1449b2bb4f2STakashi Sakamoto 
1459b2bb4f2STakashi Sakamoto static void stop_isoc_ctx(struct snd_motu *motu, struct amdtp_stream *stream)
1469b2bb4f2STakashi Sakamoto {
1479b2bb4f2STakashi Sakamoto 	struct fw_iso_resources *resources;
1489b2bb4f2STakashi Sakamoto 
1499b2bb4f2STakashi Sakamoto 	if (stream == &motu->rx_stream)
1509b2bb4f2STakashi Sakamoto 		resources = &motu->rx_resources;
1519b2bb4f2STakashi Sakamoto 	else
1529b2bb4f2STakashi Sakamoto 		resources = &motu->tx_resources;
1539b2bb4f2STakashi Sakamoto 
1549b2bb4f2STakashi Sakamoto 	amdtp_stream_stop(stream);
1559b2bb4f2STakashi Sakamoto 	fw_iso_resources_free(resources);
1569b2bb4f2STakashi Sakamoto }
1579b2bb4f2STakashi Sakamoto 
1588b460c76STakashi Sakamoto int snd_motu_stream_cache_packet_formats(struct snd_motu *motu)
1598b460c76STakashi Sakamoto {
1608b460c76STakashi Sakamoto 	int err;
1618b460c76STakashi Sakamoto 
1628b460c76STakashi Sakamoto 	err = motu->spec->protocol->cache_packet_formats(motu);
1638b460c76STakashi Sakamoto 	if (err < 0)
1648b460c76STakashi Sakamoto 		return err;
1658b460c76STakashi Sakamoto 
1668b460c76STakashi Sakamoto 	if (motu->spec->flags & SND_MOTU_SPEC_TX_MIDI_2ND_Q) {
1678b460c76STakashi Sakamoto 		motu->tx_packet_formats.midi_flag_offset = 4;
1688b460c76STakashi Sakamoto 		motu->tx_packet_formats.midi_byte_offset = 6;
1698b460c76STakashi Sakamoto 	} else if (motu->spec->flags & SND_MOTU_SPEC_TX_MIDI_3RD_Q) {
1708b460c76STakashi Sakamoto 		motu->tx_packet_formats.midi_flag_offset = 8;
1718b460c76STakashi Sakamoto 		motu->tx_packet_formats.midi_byte_offset = 7;
1728b460c76STakashi Sakamoto 	}
1738b460c76STakashi Sakamoto 
1748b460c76STakashi Sakamoto 	if (motu->spec->flags & SND_MOTU_SPEC_RX_MIDI_2ND_Q) {
1758b460c76STakashi Sakamoto 		motu->rx_packet_formats.midi_flag_offset = 4;
1768b460c76STakashi Sakamoto 		motu->rx_packet_formats.midi_byte_offset = 6;
1778b460c76STakashi Sakamoto 	} else if (motu->spec->flags & SND_MOTU_SPEC_RX_MIDI_3RD_Q) {
1788b460c76STakashi Sakamoto 		motu->rx_packet_formats.midi_flag_offset = 8;
1798b460c76STakashi Sakamoto 		motu->rx_packet_formats.midi_byte_offset = 7;
1808b460c76STakashi Sakamoto 	}
1818b460c76STakashi Sakamoto 
1828b460c76STakashi Sakamoto 	return 0;
1838b460c76STakashi Sakamoto }
1848b460c76STakashi Sakamoto 
1859b2bb4f2STakashi Sakamoto static int ensure_packet_formats(struct snd_motu *motu)
1869b2bb4f2STakashi Sakamoto {
1879b2bb4f2STakashi Sakamoto 	__be32 reg;
1889b2bb4f2STakashi Sakamoto 	u32 data;
1899b2bb4f2STakashi Sakamoto 	int err;
1909b2bb4f2STakashi Sakamoto 
1919b2bb4f2STakashi Sakamoto 	err = snd_motu_transaction_read(motu, PACKET_FORMAT_OFFSET, &reg,
1929b2bb4f2STakashi Sakamoto 					sizeof(reg));
1939b2bb4f2STakashi Sakamoto 	if (err < 0)
1949b2bb4f2STakashi Sakamoto 		return err;
1959b2bb4f2STakashi Sakamoto 	data = be32_to_cpu(reg);
1969b2bb4f2STakashi Sakamoto 
1979b2bb4f2STakashi Sakamoto 	data &= ~(TX_PACKET_EXCLUDE_DIFFERED_DATA_CHUNKS |
1989b2bb4f2STakashi Sakamoto 		  RX_PACKET_EXCLUDE_DIFFERED_DATA_CHUNKS|
1999b2bb4f2STakashi Sakamoto 		  TX_PACKET_TRANSMISSION_SPEED_MASK);
2009b2bb4f2STakashi Sakamoto 	if (motu->tx_packet_formats.differed_part_pcm_chunks[0] == 0)
2019b2bb4f2STakashi Sakamoto 		data |= TX_PACKET_EXCLUDE_DIFFERED_DATA_CHUNKS;
2029b2bb4f2STakashi Sakamoto 	if (motu->rx_packet_formats.differed_part_pcm_chunks[0] == 0)
2039b2bb4f2STakashi Sakamoto 		data |= RX_PACKET_EXCLUDE_DIFFERED_DATA_CHUNKS;
2049b2bb4f2STakashi Sakamoto 	data |= fw_parent_device(motu->unit)->max_speed;
2059b2bb4f2STakashi Sakamoto 
2069b2bb4f2STakashi Sakamoto 	reg = cpu_to_be32(data);
2079b2bb4f2STakashi Sakamoto 	return snd_motu_transaction_write(motu, PACKET_FORMAT_OFFSET, &reg,
2089b2bb4f2STakashi Sakamoto 					  sizeof(reg));
2099b2bb4f2STakashi Sakamoto }
2109b2bb4f2STakashi Sakamoto 
2119b2bb4f2STakashi Sakamoto int snd_motu_stream_start_duplex(struct snd_motu *motu, unsigned int rate)
2129b2bb4f2STakashi Sakamoto {
2139b2bb4f2STakashi Sakamoto 	const struct snd_motu_protocol *protocol = motu->spec->protocol;
2149b2bb4f2STakashi Sakamoto 	unsigned int curr_rate;
2159b2bb4f2STakashi Sakamoto 	int err = 0;
2169b2bb4f2STakashi Sakamoto 
21718f26034STakashi Sakamoto 	if (motu->substreams_counter == 0)
2189b2bb4f2STakashi Sakamoto 		return 0;
2199b2bb4f2STakashi Sakamoto 
2208b460c76STakashi Sakamoto 	err = snd_motu_stream_cache_packet_formats(motu);
2219b2bb4f2STakashi Sakamoto 	if (err < 0)
2229b2bb4f2STakashi Sakamoto 		return err;
2239b2bb4f2STakashi Sakamoto 
224*fc49769aSTakashi Sakamoto 	// Stop stream if rate is different.
2259b2bb4f2STakashi Sakamoto 	err = protocol->get_clock_rate(motu, &curr_rate);
2269b2bb4f2STakashi Sakamoto 	if (err < 0) {
2279b2bb4f2STakashi Sakamoto 		dev_err(&motu->unit->device,
2289b2bb4f2STakashi Sakamoto 			"fail to get sampling rate: %d\n", err);
2299b2bb4f2STakashi Sakamoto 		return err;
2309b2bb4f2STakashi Sakamoto 	}
2319b2bb4f2STakashi Sakamoto 	if (rate == 0)
2329b2bb4f2STakashi Sakamoto 		rate = curr_rate;
233*fc49769aSTakashi Sakamoto 	if (rate != curr_rate ||
234*fc49769aSTakashi Sakamoto 	    amdtp_streaming_error(&motu->rx_stream) ||
235*fc49769aSTakashi Sakamoto 	    amdtp_streaming_error(&motu->tx_stream)) {
2369b2bb4f2STakashi Sakamoto 		amdtp_stream_stop(&motu->rx_stream);
2379b2bb4f2STakashi Sakamoto 		amdtp_stream_stop(&motu->tx_stream);
2389b2bb4f2STakashi Sakamoto 		stop_both_streams(motu);
2399b2bb4f2STakashi Sakamoto 	}
2409b2bb4f2STakashi Sakamoto 
2419b2bb4f2STakashi Sakamoto 	if (!amdtp_stream_running(&motu->rx_stream)) {
2429b2bb4f2STakashi Sakamoto 		err = protocol->set_clock_rate(motu, rate);
2439b2bb4f2STakashi Sakamoto 		if (err < 0) {
2449b2bb4f2STakashi Sakamoto 			dev_err(&motu->unit->device,
2459b2bb4f2STakashi Sakamoto 				"fail to set sampling rate: %d\n", err);
2469b2bb4f2STakashi Sakamoto 			return err;
2479b2bb4f2STakashi Sakamoto 		}
2489b2bb4f2STakashi Sakamoto 
2499b2bb4f2STakashi Sakamoto 		err = ensure_packet_formats(motu);
2509b2bb4f2STakashi Sakamoto 		if (err < 0)
2519b2bb4f2STakashi Sakamoto 			return err;
2529b2bb4f2STakashi Sakamoto 
2539b2bb4f2STakashi Sakamoto 		err = start_both_streams(motu, rate);
2549b2bb4f2STakashi Sakamoto 		if (err < 0) {
2559b2bb4f2STakashi Sakamoto 			dev_err(&motu->unit->device,
2569b2bb4f2STakashi Sakamoto 				"fail to start isochronous comm: %d\n", err);
257f16e666bSMarkus Elfring 			goto stop_streams;
2589b2bb4f2STakashi Sakamoto 		}
2599b2bb4f2STakashi Sakamoto 
2609b2bb4f2STakashi Sakamoto 		err = start_isoc_ctx(motu, &motu->rx_stream);
2619b2bb4f2STakashi Sakamoto 		if (err < 0) {
2629b2bb4f2STakashi Sakamoto 			dev_err(&motu->unit->device,
2639b2bb4f2STakashi Sakamoto 				"fail to start IT context: %d\n", err);
264f16e666bSMarkus Elfring 			goto stop_streams;
2659b2bb4f2STakashi Sakamoto 		}
2669b2bb4f2STakashi Sakamoto 
2679b2bb4f2STakashi Sakamoto 		err = protocol->switch_fetching_mode(motu, true);
2689b2bb4f2STakashi Sakamoto 		if (err < 0) {
2699b2bb4f2STakashi Sakamoto 			dev_err(&motu->unit->device,
2709b2bb4f2STakashi Sakamoto 				"fail to enable frame fetching: %d\n", err);
271f16e666bSMarkus Elfring 			goto stop_streams;
2729b2bb4f2STakashi Sakamoto 		}
2739b2bb4f2STakashi Sakamoto 	}
2749b2bb4f2STakashi Sakamoto 
27518f26034STakashi Sakamoto 	if (!amdtp_stream_running(&motu->tx_stream)) {
2769b2bb4f2STakashi Sakamoto 		err = start_isoc_ctx(motu, &motu->tx_stream);
2779b2bb4f2STakashi Sakamoto 		if (err < 0) {
2789b2bb4f2STakashi Sakamoto 			dev_err(&motu->unit->device,
2799b2bb4f2STakashi Sakamoto 				"fail to start IR context: %d", err);
2809b2bb4f2STakashi Sakamoto 			amdtp_stream_stop(&motu->rx_stream);
281f16e666bSMarkus Elfring 			goto stop_streams;
2829b2bb4f2STakashi Sakamoto 		}
2839b2bb4f2STakashi Sakamoto 	}
2849b2bb4f2STakashi Sakamoto 
2859b2bb4f2STakashi Sakamoto 	return 0;
286f16e666bSMarkus Elfring 
287f16e666bSMarkus Elfring stop_streams:
288f16e666bSMarkus Elfring 	stop_both_streams(motu);
289f16e666bSMarkus Elfring 	return err;
2909b2bb4f2STakashi Sakamoto }
2919b2bb4f2STakashi Sakamoto 
2929b2bb4f2STakashi Sakamoto void snd_motu_stream_stop_duplex(struct snd_motu *motu)
2939b2bb4f2STakashi Sakamoto {
29418f26034STakashi Sakamoto 	if (motu->substreams_counter == 0) {
2959b2bb4f2STakashi Sakamoto 		if (amdtp_stream_running(&motu->tx_stream))
2969b2bb4f2STakashi Sakamoto 			stop_isoc_ctx(motu, &motu->tx_stream);
2979b2bb4f2STakashi Sakamoto 
2989b2bb4f2STakashi Sakamoto 		if (amdtp_stream_running(&motu->rx_stream))
2999b2bb4f2STakashi Sakamoto 			stop_isoc_ctx(motu, &motu->rx_stream);
3009b2bb4f2STakashi Sakamoto 	}
3019b2bb4f2STakashi Sakamoto }
3029b2bb4f2STakashi Sakamoto 
3039b2bb4f2STakashi Sakamoto static int init_stream(struct snd_motu *motu, enum amdtp_stream_direction dir)
3049b2bb4f2STakashi Sakamoto {
3059b2bb4f2STakashi Sakamoto 	int err;
3069b2bb4f2STakashi Sakamoto 	struct amdtp_stream *stream;
3079b2bb4f2STakashi Sakamoto 	struct fw_iso_resources *resources;
3089b2bb4f2STakashi Sakamoto 
3099b2bb4f2STakashi Sakamoto 	if (dir == AMDTP_IN_STREAM) {
3109b2bb4f2STakashi Sakamoto 		stream = &motu->tx_stream;
3119b2bb4f2STakashi Sakamoto 		resources = &motu->tx_resources;
3129b2bb4f2STakashi Sakamoto 	} else {
3139b2bb4f2STakashi Sakamoto 		stream = &motu->rx_stream;
3149b2bb4f2STakashi Sakamoto 		resources = &motu->rx_resources;
3159b2bb4f2STakashi Sakamoto 	}
3169b2bb4f2STakashi Sakamoto 
3179b2bb4f2STakashi Sakamoto 	err = fw_iso_resources_init(resources, motu->unit);
3189b2bb4f2STakashi Sakamoto 	if (err < 0)
3199b2bb4f2STakashi Sakamoto 		return err;
3209b2bb4f2STakashi Sakamoto 
3219b2bb4f2STakashi Sakamoto 	err = amdtp_motu_init(stream, motu->unit, dir, motu->spec->protocol);
3229b2bb4f2STakashi Sakamoto 	if (err < 0) {
3239b2bb4f2STakashi Sakamoto 		amdtp_stream_destroy(stream);
3249b2bb4f2STakashi Sakamoto 		fw_iso_resources_destroy(resources);
3259b2bb4f2STakashi Sakamoto 	}
3269b2bb4f2STakashi Sakamoto 
3279b2bb4f2STakashi Sakamoto 	return err;
3289b2bb4f2STakashi Sakamoto }
3299b2bb4f2STakashi Sakamoto 
3309b2bb4f2STakashi Sakamoto static void destroy_stream(struct snd_motu *motu,
3319b2bb4f2STakashi Sakamoto 			   enum amdtp_stream_direction dir)
3329b2bb4f2STakashi Sakamoto {
3339b2bb4f2STakashi Sakamoto 	struct amdtp_stream *stream;
3349b2bb4f2STakashi Sakamoto 	struct fw_iso_resources *resources;
3359b2bb4f2STakashi Sakamoto 
3369b2bb4f2STakashi Sakamoto 	if (dir == AMDTP_IN_STREAM) {
3379b2bb4f2STakashi Sakamoto 		stream = &motu->tx_stream;
3389b2bb4f2STakashi Sakamoto 		resources = &motu->tx_resources;
3399b2bb4f2STakashi Sakamoto 	} else {
3409b2bb4f2STakashi Sakamoto 		stream = &motu->rx_stream;
3419b2bb4f2STakashi Sakamoto 		resources = &motu->rx_resources;
3429b2bb4f2STakashi Sakamoto 	}
3439b2bb4f2STakashi Sakamoto 
3449b2bb4f2STakashi Sakamoto 	amdtp_stream_destroy(stream);
3459b2bb4f2STakashi Sakamoto 	fw_iso_resources_free(resources);
3469b2bb4f2STakashi Sakamoto }
3479b2bb4f2STakashi Sakamoto 
3489b2bb4f2STakashi Sakamoto int snd_motu_stream_init_duplex(struct snd_motu *motu)
3499b2bb4f2STakashi Sakamoto {
3509b2bb4f2STakashi Sakamoto 	int err;
3519b2bb4f2STakashi Sakamoto 
3529b2bb4f2STakashi Sakamoto 	err = init_stream(motu, AMDTP_IN_STREAM);
3539b2bb4f2STakashi Sakamoto 	if (err < 0)
3549b2bb4f2STakashi Sakamoto 		return err;
3559b2bb4f2STakashi Sakamoto 
3569b2bb4f2STakashi Sakamoto 	err = init_stream(motu, AMDTP_OUT_STREAM);
3579b2bb4f2STakashi Sakamoto 	if (err < 0)
3589b2bb4f2STakashi Sakamoto 		destroy_stream(motu, AMDTP_IN_STREAM);
3599b2bb4f2STakashi Sakamoto 
3609b2bb4f2STakashi Sakamoto 	return err;
3619b2bb4f2STakashi Sakamoto }
3629b2bb4f2STakashi Sakamoto 
3639b2bb4f2STakashi Sakamoto /*
3649b2bb4f2STakashi Sakamoto  * This function should be called before starting streams or after stopping
3659b2bb4f2STakashi Sakamoto  * streams.
3669b2bb4f2STakashi Sakamoto  */
3679b2bb4f2STakashi Sakamoto void snd_motu_stream_destroy_duplex(struct snd_motu *motu)
3689b2bb4f2STakashi Sakamoto {
3699b2bb4f2STakashi Sakamoto 	destroy_stream(motu, AMDTP_IN_STREAM);
3709b2bb4f2STakashi Sakamoto 	destroy_stream(motu, AMDTP_OUT_STREAM);
3719b2bb4f2STakashi Sakamoto 
37218f26034STakashi Sakamoto 	motu->substreams_counter = 0;
3739b2bb4f2STakashi Sakamoto }
37471c37977STakashi Sakamoto 
37571c37977STakashi Sakamoto static void motu_lock_changed(struct snd_motu *motu)
37671c37977STakashi Sakamoto {
37771c37977STakashi Sakamoto 	motu->dev_lock_changed = true;
37871c37977STakashi Sakamoto 	wake_up(&motu->hwdep_wait);
37971c37977STakashi Sakamoto }
38071c37977STakashi Sakamoto 
38171c37977STakashi Sakamoto int snd_motu_stream_lock_try(struct snd_motu *motu)
38271c37977STakashi Sakamoto {
38371c37977STakashi Sakamoto 	int err;
38471c37977STakashi Sakamoto 
38571c37977STakashi Sakamoto 	spin_lock_irq(&motu->lock);
38671c37977STakashi Sakamoto 
38771c37977STakashi Sakamoto 	if (motu->dev_lock_count < 0) {
38871c37977STakashi Sakamoto 		err = -EBUSY;
38971c37977STakashi Sakamoto 		goto out;
39071c37977STakashi Sakamoto 	}
39171c37977STakashi Sakamoto 
39271c37977STakashi Sakamoto 	if (motu->dev_lock_count++ == 0)
39371c37977STakashi Sakamoto 		motu_lock_changed(motu);
39471c37977STakashi Sakamoto 	err = 0;
39571c37977STakashi Sakamoto out:
39671c37977STakashi Sakamoto 	spin_unlock_irq(&motu->lock);
39771c37977STakashi Sakamoto 	return err;
39871c37977STakashi Sakamoto }
39971c37977STakashi Sakamoto 
40071c37977STakashi Sakamoto void snd_motu_stream_lock_release(struct snd_motu *motu)
40171c37977STakashi Sakamoto {
40271c37977STakashi Sakamoto 	spin_lock_irq(&motu->lock);
40371c37977STakashi Sakamoto 
40471c37977STakashi Sakamoto 	if (WARN_ON(motu->dev_lock_count <= 0))
40571c37977STakashi Sakamoto 		goto out;
40671c37977STakashi Sakamoto 
40771c37977STakashi Sakamoto 	if (--motu->dev_lock_count == 0)
40871c37977STakashi Sakamoto 		motu_lock_changed(motu);
40971c37977STakashi Sakamoto out:
41071c37977STakashi Sakamoto 	spin_unlock_irq(&motu->lock);
41171c37977STakashi Sakamoto }
412