1da607e19SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
29b2bb4f2STakashi Sakamoto /*
39b2bb4f2STakashi Sakamoto * motu-stream.c - a part of driver for MOTU FireWire series
49b2bb4f2STakashi Sakamoto *
59b2bb4f2STakashi Sakamoto * Copyright (c) 2015-2017 Takashi Sakamoto <o-takashi@sakamocchi.jp>
69b2bb4f2STakashi Sakamoto */
79b2bb4f2STakashi Sakamoto
89b2bb4f2STakashi Sakamoto #include "motu.h"
99b2bb4f2STakashi Sakamoto
10bdaedca7STakashi Sakamoto #define READY_TIMEOUT_MS 200
119b2bb4f2STakashi Sakamoto
129b2bb4f2STakashi Sakamoto #define ISOC_COMM_CONTROL_OFFSET 0x0b00
139b2bb4f2STakashi Sakamoto #define ISOC_COMM_CONTROL_MASK 0xffff0000
149b2bb4f2STakashi Sakamoto #define CHANGE_RX_ISOC_COMM_STATE 0x80000000
159b2bb4f2STakashi Sakamoto #define RX_ISOC_COMM_IS_ACTIVATED 0x40000000
169b2bb4f2STakashi Sakamoto #define RX_ISOC_COMM_CHANNEL_MASK 0x3f000000
179b2bb4f2STakashi Sakamoto #define RX_ISOC_COMM_CHANNEL_SHIFT 24
189b2bb4f2STakashi Sakamoto #define CHANGE_TX_ISOC_COMM_STATE 0x00800000
199b2bb4f2STakashi Sakamoto #define TX_ISOC_COMM_IS_ACTIVATED 0x00400000
209b2bb4f2STakashi Sakamoto #define TX_ISOC_COMM_CHANNEL_MASK 0x003f0000
219b2bb4f2STakashi Sakamoto #define TX_ISOC_COMM_CHANNEL_SHIFT 16
229b2bb4f2STakashi Sakamoto
239b2bb4f2STakashi Sakamoto #define PACKET_FORMAT_OFFSET 0x0b10
249b2bb4f2STakashi Sakamoto #define TX_PACKET_EXCLUDE_DIFFERED_DATA_CHUNKS 0x00000080
259b2bb4f2STakashi Sakamoto #define RX_PACKET_EXCLUDE_DIFFERED_DATA_CHUNKS 0x00000040
269b2bb4f2STakashi Sakamoto #define TX_PACKET_TRANSMISSION_SPEED_MASK 0x0000000f
279b2bb4f2STakashi Sakamoto
keep_resources(struct snd_motu * motu,unsigned int rate,struct amdtp_stream * stream)288350132eSTakashi Sakamoto static int keep_resources(struct snd_motu *motu, unsigned int rate,
298350132eSTakashi Sakamoto struct amdtp_stream *stream)
309b2bb4f2STakashi Sakamoto {
318350132eSTakashi Sakamoto struct fw_iso_resources *resources;
328350132eSTakashi Sakamoto struct snd_motu_packet_format *packet_format;
339e796e7dSTakashi Sakamoto unsigned int midi_ports = 0;
349b2bb4f2STakashi Sakamoto int err;
359b2bb4f2STakashi Sakamoto
368350132eSTakashi Sakamoto if (stream == &motu->rx_stream) {
378350132eSTakashi Sakamoto resources = &motu->rx_resources;
388350132eSTakashi Sakamoto packet_format = &motu->rx_packet_formats;
398350132eSTakashi Sakamoto
408b460c76STakashi Sakamoto if ((motu->spec->flags & SND_MOTU_SPEC_RX_MIDI_2ND_Q) ||
418b460c76STakashi Sakamoto (motu->spec->flags & SND_MOTU_SPEC_RX_MIDI_3RD_Q))
429e796e7dSTakashi Sakamoto midi_ports = 1;
438350132eSTakashi Sakamoto } else {
448350132eSTakashi Sakamoto resources = &motu->tx_resources;
458350132eSTakashi Sakamoto packet_format = &motu->tx_packet_formats;
469b2bb4f2STakashi Sakamoto
478b460c76STakashi Sakamoto if ((motu->spec->flags & SND_MOTU_SPEC_TX_MIDI_2ND_Q) ||
488b460c76STakashi Sakamoto (motu->spec->flags & SND_MOTU_SPEC_TX_MIDI_3RD_Q))
498b460c76STakashi Sakamoto midi_ports = 1;
508350132eSTakashi Sakamoto }
518b460c76STakashi Sakamoto
528350132eSTakashi Sakamoto err = amdtp_motu_set_parameters(stream, rate, midi_ports,
538350132eSTakashi Sakamoto packet_format);
549b2bb4f2STakashi Sakamoto if (err < 0)
559b2bb4f2STakashi Sakamoto return err;
569b2bb4f2STakashi Sakamoto
578350132eSTakashi Sakamoto return fw_iso_resources_allocate(resources,
588350132eSTakashi Sakamoto amdtp_stream_get_max_payload(stream),
599b2bb4f2STakashi Sakamoto fw_parent_device(motu->unit)->max_speed);
608350132eSTakashi Sakamoto }
618350132eSTakashi Sakamoto
begin_session(struct snd_motu * motu)628edc56ecSTakashi Sakamoto static int begin_session(struct snd_motu *motu)
638350132eSTakashi Sakamoto {
648350132eSTakashi Sakamoto __be32 reg;
658350132eSTakashi Sakamoto u32 data;
668350132eSTakashi Sakamoto int err;
678350132eSTakashi Sakamoto
688350132eSTakashi Sakamoto // Configure the unit to start isochronous communication.
699b2bb4f2STakashi Sakamoto err = snd_motu_transaction_read(motu, ISOC_COMM_CONTROL_OFFSET, ®,
709b2bb4f2STakashi Sakamoto sizeof(reg));
719b2bb4f2STakashi Sakamoto if (err < 0)
729b2bb4f2STakashi Sakamoto return err;
739b2bb4f2STakashi Sakamoto data = be32_to_cpu(reg) & ~ISOC_COMM_CONTROL_MASK;
749b2bb4f2STakashi Sakamoto
759b2bb4f2STakashi Sakamoto data |= CHANGE_RX_ISOC_COMM_STATE | RX_ISOC_COMM_IS_ACTIVATED |
769b2bb4f2STakashi Sakamoto (motu->rx_resources.channel << RX_ISOC_COMM_CHANNEL_SHIFT) |
779b2bb4f2STakashi Sakamoto CHANGE_TX_ISOC_COMM_STATE | TX_ISOC_COMM_IS_ACTIVATED |
789b2bb4f2STakashi Sakamoto (motu->tx_resources.channel << TX_ISOC_COMM_CHANNEL_SHIFT);
799b2bb4f2STakashi Sakamoto
809b2bb4f2STakashi Sakamoto reg = cpu_to_be32(data);
819b2bb4f2STakashi Sakamoto return snd_motu_transaction_write(motu, ISOC_COMM_CONTROL_OFFSET, ®,
829b2bb4f2STakashi Sakamoto sizeof(reg));
839b2bb4f2STakashi Sakamoto }
849b2bb4f2STakashi Sakamoto
finish_session(struct snd_motu * motu)85b66ab142STakashi Sakamoto static void finish_session(struct snd_motu *motu)
869b2bb4f2STakashi Sakamoto {
879b2bb4f2STakashi Sakamoto __be32 reg;
889b2bb4f2STakashi Sakamoto u32 data;
899b2bb4f2STakashi Sakamoto int err;
909b2bb4f2STakashi Sakamoto
91ff222b7eSTakashi Sakamoto err = snd_motu_protocol_switch_fetching_mode(motu, false);
929b2bb4f2STakashi Sakamoto if (err < 0)
939b2bb4f2STakashi Sakamoto return;
949b2bb4f2STakashi Sakamoto
959b2bb4f2STakashi Sakamoto err = snd_motu_transaction_read(motu, ISOC_COMM_CONTROL_OFFSET, ®,
969b2bb4f2STakashi Sakamoto sizeof(reg));
979b2bb4f2STakashi Sakamoto if (err < 0)
989b2bb4f2STakashi Sakamoto return;
999b2bb4f2STakashi Sakamoto data = be32_to_cpu(reg);
1009b2bb4f2STakashi Sakamoto
1019b2bb4f2STakashi Sakamoto data &= ~(RX_ISOC_COMM_IS_ACTIVATED | TX_ISOC_COMM_IS_ACTIVATED);
1029b2bb4f2STakashi Sakamoto data |= CHANGE_RX_ISOC_COMM_STATE | CHANGE_TX_ISOC_COMM_STATE;
1039b2bb4f2STakashi Sakamoto
1049b2bb4f2STakashi Sakamoto reg = cpu_to_be32(data);
1059b2bb4f2STakashi Sakamoto snd_motu_transaction_write(motu, ISOC_COMM_CONTROL_OFFSET, ®,
1069b2bb4f2STakashi Sakamoto sizeof(reg));
1079b2bb4f2STakashi Sakamoto }
1089b2bb4f2STakashi Sakamoto
snd_motu_stream_cache_packet_formats(struct snd_motu * motu)1098b460c76STakashi Sakamoto int snd_motu_stream_cache_packet_formats(struct snd_motu *motu)
1108b460c76STakashi Sakamoto {
1118b460c76STakashi Sakamoto int err;
1128b460c76STakashi Sakamoto
113ff222b7eSTakashi Sakamoto err = snd_motu_protocol_cache_packet_formats(motu);
1148b460c76STakashi Sakamoto if (err < 0)
1158b460c76STakashi Sakamoto return err;
1168b460c76STakashi Sakamoto
1178b460c76STakashi Sakamoto if (motu->spec->flags & SND_MOTU_SPEC_TX_MIDI_2ND_Q) {
1188b460c76STakashi Sakamoto motu->tx_packet_formats.midi_flag_offset = 4;
1198b460c76STakashi Sakamoto motu->tx_packet_formats.midi_byte_offset = 6;
1208b460c76STakashi Sakamoto } else if (motu->spec->flags & SND_MOTU_SPEC_TX_MIDI_3RD_Q) {
1218b460c76STakashi Sakamoto motu->tx_packet_formats.midi_flag_offset = 8;
1228b460c76STakashi Sakamoto motu->tx_packet_formats.midi_byte_offset = 7;
1238b460c76STakashi Sakamoto }
1248b460c76STakashi Sakamoto
1258b460c76STakashi Sakamoto if (motu->spec->flags & SND_MOTU_SPEC_RX_MIDI_2ND_Q) {
1268b460c76STakashi Sakamoto motu->rx_packet_formats.midi_flag_offset = 4;
1278b460c76STakashi Sakamoto motu->rx_packet_formats.midi_byte_offset = 6;
1288b460c76STakashi Sakamoto } else if (motu->spec->flags & SND_MOTU_SPEC_RX_MIDI_3RD_Q) {
1298b460c76STakashi Sakamoto motu->rx_packet_formats.midi_flag_offset = 8;
1308b460c76STakashi Sakamoto motu->rx_packet_formats.midi_byte_offset = 7;
1318b460c76STakashi Sakamoto }
1328b460c76STakashi Sakamoto
1338b460c76STakashi Sakamoto return 0;
1348b460c76STakashi Sakamoto }
1358b460c76STakashi Sakamoto
snd_motu_stream_reserve_duplex(struct snd_motu * motu,unsigned int rate,unsigned int frames_per_period,unsigned int frames_per_buffer)1360d39cd0eSTakashi Sakamoto int snd_motu_stream_reserve_duplex(struct snd_motu *motu, unsigned int rate,
1370f5482e7STakashi Sakamoto unsigned int frames_per_period,
1380f5482e7STakashi Sakamoto unsigned int frames_per_buffer)
1398edc56ecSTakashi Sakamoto {
1408edc56ecSTakashi Sakamoto unsigned int curr_rate;
1418edc56ecSTakashi Sakamoto int err;
1428edc56ecSTakashi Sakamoto
143ff222b7eSTakashi Sakamoto err = snd_motu_protocol_get_clock_rate(motu, &curr_rate);
1448edc56ecSTakashi Sakamoto if (err < 0)
1458edc56ecSTakashi Sakamoto return err;
1468edc56ecSTakashi Sakamoto if (rate == 0)
1478edc56ecSTakashi Sakamoto rate = curr_rate;
1488edc56ecSTakashi Sakamoto
1498edc56ecSTakashi Sakamoto if (motu->substreams_counter == 0 || curr_rate != rate) {
150ccc6c1b0STakashi Sakamoto amdtp_domain_stop(&motu->domain);
1518edc56ecSTakashi Sakamoto finish_session(motu);
1528edc56ecSTakashi Sakamoto
1538edc56ecSTakashi Sakamoto fw_iso_resources_free(&motu->tx_resources);
1548edc56ecSTakashi Sakamoto fw_iso_resources_free(&motu->rx_resources);
1558edc56ecSTakashi Sakamoto
156e50dfac8STakashi Sakamoto kfree(motu->cache.event_offsets);
157e50dfac8STakashi Sakamoto motu->cache.event_offsets = NULL;
158e50dfac8STakashi Sakamoto
159ff222b7eSTakashi Sakamoto err = snd_motu_protocol_set_clock_rate(motu, rate);
1608edc56ecSTakashi Sakamoto if (err < 0) {
1618edc56ecSTakashi Sakamoto dev_err(&motu->unit->device,
1628edc56ecSTakashi Sakamoto "fail to set sampling rate: %d\n", err);
1638edc56ecSTakashi Sakamoto return err;
1648edc56ecSTakashi Sakamoto }
1658edc56ecSTakashi Sakamoto
1668edc56ecSTakashi Sakamoto err = snd_motu_stream_cache_packet_formats(motu);
1678edc56ecSTakashi Sakamoto if (err < 0)
1688edc56ecSTakashi Sakamoto return err;
1698edc56ecSTakashi Sakamoto
1708edc56ecSTakashi Sakamoto err = keep_resources(motu, rate, &motu->tx_stream);
1718edc56ecSTakashi Sakamoto if (err < 0)
1728edc56ecSTakashi Sakamoto return err;
1738edc56ecSTakashi Sakamoto
1748edc56ecSTakashi Sakamoto err = keep_resources(motu, rate, &motu->rx_stream);
1758edc56ecSTakashi Sakamoto if (err < 0) {
1768edc56ecSTakashi Sakamoto fw_iso_resources_free(&motu->tx_resources);
1778edc56ecSTakashi Sakamoto return err;
1788edc56ecSTakashi Sakamoto }
1790d39cd0eSTakashi Sakamoto
1800d39cd0eSTakashi Sakamoto err = amdtp_domain_set_events_per_period(&motu->domain,
1810f5482e7STakashi Sakamoto frames_per_period, frames_per_buffer);
1820d39cd0eSTakashi Sakamoto if (err < 0) {
1830d39cd0eSTakashi Sakamoto fw_iso_resources_free(&motu->tx_resources);
1840d39cd0eSTakashi Sakamoto fw_iso_resources_free(&motu->rx_resources);
1850d39cd0eSTakashi Sakamoto return err;
1860d39cd0eSTakashi Sakamoto }
187e50dfac8STakashi Sakamoto
188e50dfac8STakashi Sakamoto motu->cache.size = motu->tx_stream.syt_interval * frames_per_buffer;
189e50dfac8STakashi Sakamoto motu->cache.event_offsets = kcalloc(motu->cache.size, sizeof(*motu->cache.event_offsets),
190e50dfac8STakashi Sakamoto GFP_KERNEL);
191e50dfac8STakashi Sakamoto if (!motu->cache.event_offsets) {
192e50dfac8STakashi Sakamoto fw_iso_resources_free(&motu->tx_resources);
193e50dfac8STakashi Sakamoto fw_iso_resources_free(&motu->rx_resources);
194ce9f50e7SYang Yingliang return -ENOMEM;
195e50dfac8STakashi Sakamoto }
1968edc56ecSTakashi Sakamoto }
1978edc56ecSTakashi Sakamoto
1988edc56ecSTakashi Sakamoto return 0;
1998edc56ecSTakashi Sakamoto }
2008edc56ecSTakashi Sakamoto
ensure_packet_formats(struct snd_motu * motu)2019b2bb4f2STakashi Sakamoto static int ensure_packet_formats(struct snd_motu *motu)
2029b2bb4f2STakashi Sakamoto {
2039b2bb4f2STakashi Sakamoto __be32 reg;
2049b2bb4f2STakashi Sakamoto u32 data;
2059b2bb4f2STakashi Sakamoto int err;
2069b2bb4f2STakashi Sakamoto
2079b2bb4f2STakashi Sakamoto err = snd_motu_transaction_read(motu, PACKET_FORMAT_OFFSET, ®,
2089b2bb4f2STakashi Sakamoto sizeof(reg));
2099b2bb4f2STakashi Sakamoto if (err < 0)
2109b2bb4f2STakashi Sakamoto return err;
2119b2bb4f2STakashi Sakamoto data = be32_to_cpu(reg);
2129b2bb4f2STakashi Sakamoto
2139b2bb4f2STakashi Sakamoto data &= ~(TX_PACKET_EXCLUDE_DIFFERED_DATA_CHUNKS |
2149b2bb4f2STakashi Sakamoto RX_PACKET_EXCLUDE_DIFFERED_DATA_CHUNKS|
2159b2bb4f2STakashi Sakamoto TX_PACKET_TRANSMISSION_SPEED_MASK);
21688e8f89dSTakashi Sakamoto if (motu->spec->tx_fixed_pcm_chunks[0] == motu->tx_packet_formats.pcm_chunks[0])
2179b2bb4f2STakashi Sakamoto data |= TX_PACKET_EXCLUDE_DIFFERED_DATA_CHUNKS;
21888e8f89dSTakashi Sakamoto if (motu->spec->rx_fixed_pcm_chunks[0] == motu->rx_packet_formats.pcm_chunks[0])
2199b2bb4f2STakashi Sakamoto data |= RX_PACKET_EXCLUDE_DIFFERED_DATA_CHUNKS;
2209b2bb4f2STakashi Sakamoto data |= fw_parent_device(motu->unit)->max_speed;
2219b2bb4f2STakashi Sakamoto
2229b2bb4f2STakashi Sakamoto reg = cpu_to_be32(data);
2239b2bb4f2STakashi Sakamoto return snd_motu_transaction_write(motu, PACKET_FORMAT_OFFSET, ®,
2249b2bb4f2STakashi Sakamoto sizeof(reg));
2259b2bb4f2STakashi Sakamoto }
2269b2bb4f2STakashi Sakamoto
snd_motu_stream_start_duplex(struct snd_motu * motu)2278edc56ecSTakashi Sakamoto int snd_motu_stream_start_duplex(struct snd_motu *motu)
2289b2bb4f2STakashi Sakamoto {
2292d103420STakashi Sakamoto unsigned int generation = motu->rx_resources.generation;
2309b2bb4f2STakashi Sakamoto int err = 0;
2319b2bb4f2STakashi Sakamoto
23218f26034STakashi Sakamoto if (motu->substreams_counter == 0)
2339b2bb4f2STakashi Sakamoto return 0;
2349b2bb4f2STakashi Sakamoto
2358edc56ecSTakashi Sakamoto if (amdtp_streaming_error(&motu->rx_stream) ||
236ccc6c1b0STakashi Sakamoto amdtp_streaming_error(&motu->tx_stream)) {
237ccc6c1b0STakashi Sakamoto amdtp_domain_stop(&motu->domain);
238b66ab142STakashi Sakamoto finish_session(motu);
239ccc6c1b0STakashi Sakamoto }
2409b2bb4f2STakashi Sakamoto
2412d103420STakashi Sakamoto if (generation != fw_parent_device(motu->unit)->card->generation) {
2422d103420STakashi Sakamoto err = fw_iso_resources_update(&motu->rx_resources);
2432d103420STakashi Sakamoto if (err < 0)
2442d103420STakashi Sakamoto return err;
2452d103420STakashi Sakamoto
2462d103420STakashi Sakamoto err = fw_iso_resources_update(&motu->tx_resources);
2472d103420STakashi Sakamoto if (err < 0)
2482d103420STakashi Sakamoto return err;
2492d103420STakashi Sakamoto }
2502d103420STakashi Sakamoto
2519b2bb4f2STakashi Sakamoto if (!amdtp_stream_running(&motu->rx_stream)) {
252ccc6c1b0STakashi Sakamoto int spd = fw_parent_device(motu->unit)->max_speed;
253ccc6c1b0STakashi Sakamoto
2549b2bb4f2STakashi Sakamoto err = ensure_packet_formats(motu);
2559b2bb4f2STakashi Sakamoto if (err < 0)
2569b2bb4f2STakashi Sakamoto return err;
2579b2bb4f2STakashi Sakamoto
258bea36afaSTakashi Sakamoto if (motu->spec->flags & SND_MOTU_SPEC_REGISTER_DSP) {
259bea36afaSTakashi Sakamoto err = snd_motu_register_dsp_message_parser_init(motu);
260bea36afaSTakashi Sakamoto if (err < 0)
261bea36afaSTakashi Sakamoto return err;
262*90b28f3bSTakashi Sakamoto } else if (motu->spec->flags & SND_MOTU_SPEC_COMMAND_DSP) {
263*90b28f3bSTakashi Sakamoto err = snd_motu_command_dsp_message_parser_init(motu, motu->tx_stream.sfc);
264*90b28f3bSTakashi Sakamoto if (err < 0)
265*90b28f3bSTakashi Sakamoto return err;
266bea36afaSTakashi Sakamoto }
267bea36afaSTakashi Sakamoto
2688edc56ecSTakashi Sakamoto err = begin_session(motu);
2699b2bb4f2STakashi Sakamoto if (err < 0) {
2709b2bb4f2STakashi Sakamoto dev_err(&motu->unit->device,
2719b2bb4f2STakashi Sakamoto "fail to start isochronous comm: %d\n", err);
272f16e666bSMarkus Elfring goto stop_streams;
2739b2bb4f2STakashi Sakamoto }
2749b2bb4f2STakashi Sakamoto
275ccc6c1b0STakashi Sakamoto err = amdtp_domain_add_stream(&motu->domain, &motu->tx_stream,
276ccc6c1b0STakashi Sakamoto motu->tx_resources.channel, spd);
277ccc6c1b0STakashi Sakamoto if (err < 0)
278ccc6c1b0STakashi Sakamoto goto stop_streams;
279ccc6c1b0STakashi Sakamoto
280ccc6c1b0STakashi Sakamoto err = amdtp_domain_add_stream(&motu->domain, &motu->rx_stream,
281ccc6c1b0STakashi Sakamoto motu->rx_resources.channel, spd);
282ccc6c1b0STakashi Sakamoto if (err < 0)
283ccc6c1b0STakashi Sakamoto goto stop_streams;
284ccc6c1b0STakashi Sakamoto
285e50dfac8STakashi Sakamoto motu->cache.tail = 0;
286e50dfac8STakashi Sakamoto motu->cache.tx_cycle_count = UINT_MAX;
287f2ac3b83STakashi Sakamoto motu->cache.head = 0;
288f2ac3b83STakashi Sakamoto motu->cache.rx_cycle_count = UINT_MAX;
289e50dfac8STakashi Sakamoto
290f2ac3b83STakashi Sakamoto // NOTE: The device requires both of replay; the sequence of the number of data
291f2ac3b83STakashi Sakamoto // blocks per packet, and the sequence of source packet header per data block as
292f2ac3b83STakashi Sakamoto // presentation time.
293f2ac3b83STakashi Sakamoto err = amdtp_domain_start(&motu->domain, 0, true, false);
294ccc6c1b0STakashi Sakamoto if (err < 0)
295ccc6c1b0STakashi Sakamoto goto stop_streams;
296ccc6c1b0STakashi Sakamoto
297bdaedca7STakashi Sakamoto if (!amdtp_domain_wait_ready(&motu->domain, READY_TIMEOUT_MS)) {
298ccc6c1b0STakashi Sakamoto err = -ETIMEDOUT;
299f16e666bSMarkus Elfring goto stop_streams;
3009b2bb4f2STakashi Sakamoto }
3019b2bb4f2STakashi Sakamoto
302ff222b7eSTakashi Sakamoto err = snd_motu_protocol_switch_fetching_mode(motu, true);
3039b2bb4f2STakashi Sakamoto if (err < 0) {
3049b2bb4f2STakashi Sakamoto dev_err(&motu->unit->device,
3059b2bb4f2STakashi Sakamoto "fail to enable frame fetching: %d\n", err);
306f16e666bSMarkus Elfring goto stop_streams;
3079b2bb4f2STakashi Sakamoto }
3089b2bb4f2STakashi Sakamoto }
3099b2bb4f2STakashi Sakamoto
3109b2bb4f2STakashi Sakamoto return 0;
311f16e666bSMarkus Elfring
312f16e666bSMarkus Elfring stop_streams:
313ccc6c1b0STakashi Sakamoto amdtp_domain_stop(&motu->domain);
314b66ab142STakashi Sakamoto finish_session(motu);
315f16e666bSMarkus Elfring return err;
3169b2bb4f2STakashi Sakamoto }
3179b2bb4f2STakashi Sakamoto
snd_motu_stream_stop_duplex(struct snd_motu * motu)3189b2bb4f2STakashi Sakamoto void snd_motu_stream_stop_duplex(struct snd_motu *motu)
3199b2bb4f2STakashi Sakamoto {
320eccd895cSTakashi Sakamoto if (motu->substreams_counter == 0) {
321ccc6c1b0STakashi Sakamoto amdtp_domain_stop(&motu->domain);
322ec694fbaSTakashi Sakamoto finish_session(motu);
323eccd895cSTakashi Sakamoto
324eccd895cSTakashi Sakamoto fw_iso_resources_free(&motu->tx_resources);
325eccd895cSTakashi Sakamoto fw_iso_resources_free(&motu->rx_resources);
326e50dfac8STakashi Sakamoto
327e50dfac8STakashi Sakamoto kfree(motu->cache.event_offsets);
328e50dfac8STakashi Sakamoto motu->cache.event_offsets = NULL;
329eccd895cSTakashi Sakamoto }
3309b2bb4f2STakashi Sakamoto }
3319b2bb4f2STakashi Sakamoto
init_stream(struct snd_motu * motu,struct amdtp_stream * s)33239e522a5STakashi Sakamoto static int init_stream(struct snd_motu *motu, struct amdtp_stream *s)
3339b2bb4f2STakashi Sakamoto {
3349b2bb4f2STakashi Sakamoto struct fw_iso_resources *resources;
33539e522a5STakashi Sakamoto enum amdtp_stream_direction dir;
33639e522a5STakashi Sakamoto int err;
3379b2bb4f2STakashi Sakamoto
33839e522a5STakashi Sakamoto if (s == &motu->tx_stream) {
3399b2bb4f2STakashi Sakamoto resources = &motu->tx_resources;
34039e522a5STakashi Sakamoto dir = AMDTP_IN_STREAM;
3419b2bb4f2STakashi Sakamoto } else {
3429b2bb4f2STakashi Sakamoto resources = &motu->rx_resources;
34339e522a5STakashi Sakamoto dir = AMDTP_OUT_STREAM;
3449b2bb4f2STakashi Sakamoto }
3459b2bb4f2STakashi Sakamoto
3469b2bb4f2STakashi Sakamoto err = fw_iso_resources_init(resources, motu->unit);
3479b2bb4f2STakashi Sakamoto if (err < 0)
3489b2bb4f2STakashi Sakamoto return err;
3499b2bb4f2STakashi Sakamoto
350e50dfac8STakashi Sakamoto err = amdtp_motu_init(s, motu->unit, dir, motu->spec, &motu->cache);
35139e522a5STakashi Sakamoto if (err < 0)
3529b2bb4f2STakashi Sakamoto fw_iso_resources_destroy(resources);
3539b2bb4f2STakashi Sakamoto
3549b2bb4f2STakashi Sakamoto return err;
3559b2bb4f2STakashi Sakamoto }
3569b2bb4f2STakashi Sakamoto
destroy_stream(struct snd_motu * motu,struct amdtp_stream * s)35739e522a5STakashi Sakamoto static void destroy_stream(struct snd_motu *motu, struct amdtp_stream *s)
3589b2bb4f2STakashi Sakamoto {
35939e522a5STakashi Sakamoto amdtp_stream_destroy(s);
3609b2bb4f2STakashi Sakamoto
36139e522a5STakashi Sakamoto if (s == &motu->tx_stream)
36239e522a5STakashi Sakamoto fw_iso_resources_destroy(&motu->tx_resources);
36339e522a5STakashi Sakamoto else
36439e522a5STakashi Sakamoto fw_iso_resources_destroy(&motu->rx_resources);
3659b2bb4f2STakashi Sakamoto }
3669b2bb4f2STakashi Sakamoto
snd_motu_stream_init_duplex(struct snd_motu * motu)3679b2bb4f2STakashi Sakamoto int snd_motu_stream_init_duplex(struct snd_motu *motu)
3689b2bb4f2STakashi Sakamoto {
3699b2bb4f2STakashi Sakamoto int err;
3709b2bb4f2STakashi Sakamoto
37139e522a5STakashi Sakamoto err = init_stream(motu, &motu->tx_stream);
3729b2bb4f2STakashi Sakamoto if (err < 0)
3739b2bb4f2STakashi Sakamoto return err;
3749b2bb4f2STakashi Sakamoto
37539e522a5STakashi Sakamoto err = init_stream(motu, &motu->rx_stream);
376ccc6c1b0STakashi Sakamoto if (err < 0) {
37739e522a5STakashi Sakamoto destroy_stream(motu, &motu->tx_stream);
378ccc6c1b0STakashi Sakamoto return err;
379ccc6c1b0STakashi Sakamoto }
380ccc6c1b0STakashi Sakamoto
381ccc6c1b0STakashi Sakamoto err = amdtp_domain_init(&motu->domain);
382ccc6c1b0STakashi Sakamoto if (err < 0) {
383ccc6c1b0STakashi Sakamoto destroy_stream(motu, &motu->tx_stream);
384ccc6c1b0STakashi Sakamoto destroy_stream(motu, &motu->rx_stream);
385ccc6c1b0STakashi Sakamoto }
3869b2bb4f2STakashi Sakamoto
3879b2bb4f2STakashi Sakamoto return err;
3889b2bb4f2STakashi Sakamoto }
3899b2bb4f2STakashi Sakamoto
390ccc6c1b0STakashi Sakamoto // This function should be called before starting streams or after stopping
391ccc6c1b0STakashi Sakamoto // streams.
snd_motu_stream_destroy_duplex(struct snd_motu * motu)3929b2bb4f2STakashi Sakamoto void snd_motu_stream_destroy_duplex(struct snd_motu *motu)
3939b2bb4f2STakashi Sakamoto {
394ccc6c1b0STakashi Sakamoto amdtp_domain_destroy(&motu->domain);
395ccc6c1b0STakashi Sakamoto
39639e522a5STakashi Sakamoto destroy_stream(motu, &motu->rx_stream);
39739e522a5STakashi Sakamoto destroy_stream(motu, &motu->tx_stream);
3989b2bb4f2STakashi Sakamoto
39918f26034STakashi Sakamoto motu->substreams_counter = 0;
4009b2bb4f2STakashi Sakamoto }
40171c37977STakashi Sakamoto
motu_lock_changed(struct snd_motu * motu)40271c37977STakashi Sakamoto static void motu_lock_changed(struct snd_motu *motu)
40371c37977STakashi Sakamoto {
40471c37977STakashi Sakamoto motu->dev_lock_changed = true;
40571c37977STakashi Sakamoto wake_up(&motu->hwdep_wait);
40671c37977STakashi Sakamoto }
40771c37977STakashi Sakamoto
snd_motu_stream_lock_try(struct snd_motu * motu)40871c37977STakashi Sakamoto int snd_motu_stream_lock_try(struct snd_motu *motu)
40971c37977STakashi Sakamoto {
41071c37977STakashi Sakamoto int err;
41171c37977STakashi Sakamoto
41271c37977STakashi Sakamoto spin_lock_irq(&motu->lock);
41371c37977STakashi Sakamoto
41471c37977STakashi Sakamoto if (motu->dev_lock_count < 0) {
41571c37977STakashi Sakamoto err = -EBUSY;
41671c37977STakashi Sakamoto goto out;
41771c37977STakashi Sakamoto }
41871c37977STakashi Sakamoto
41971c37977STakashi Sakamoto if (motu->dev_lock_count++ == 0)
42071c37977STakashi Sakamoto motu_lock_changed(motu);
42171c37977STakashi Sakamoto err = 0;
42271c37977STakashi Sakamoto out:
42371c37977STakashi Sakamoto spin_unlock_irq(&motu->lock);
42471c37977STakashi Sakamoto return err;
42571c37977STakashi Sakamoto }
42671c37977STakashi Sakamoto
snd_motu_stream_lock_release(struct snd_motu * motu)42771c37977STakashi Sakamoto void snd_motu_stream_lock_release(struct snd_motu *motu)
42871c37977STakashi Sakamoto {
42971c37977STakashi Sakamoto spin_lock_irq(&motu->lock);
43071c37977STakashi Sakamoto
43171c37977STakashi Sakamoto if (WARN_ON(motu->dev_lock_count <= 0))
43271c37977STakashi Sakamoto goto out;
43371c37977STakashi Sakamoto
43471c37977STakashi Sakamoto if (--motu->dev_lock_count == 0)
43571c37977STakashi Sakamoto motu_lock_changed(motu);
43671c37977STakashi Sakamoto out:
43771c37977STakashi Sakamoto spin_unlock_irq(&motu->lock);
43871c37977STakashi Sakamoto }
439