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 63b66ab142STakashi Sakamoto static int begin_session(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, ®, 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, ®, 919b2bb4f2STakashi Sakamoto sizeof(reg)); 929b2bb4f2STakashi Sakamoto } 939b2bb4f2STakashi Sakamoto 94b66ab142STakashi Sakamoto static void finish_session(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 104*ec694fbaSTakashi Sakamoto amdtp_stream_stop(&motu->tx_stream); 105*ec694fbaSTakashi Sakamoto amdtp_stream_stop(&motu->rx_stream); 106*ec694fbaSTakashi Sakamoto 1079b2bb4f2STakashi Sakamoto err = snd_motu_transaction_read(motu, ISOC_COMM_CONTROL_OFFSET, ®, 1089b2bb4f2STakashi Sakamoto sizeof(reg)); 1099b2bb4f2STakashi Sakamoto if (err < 0) 1109b2bb4f2STakashi Sakamoto return; 1119b2bb4f2STakashi Sakamoto data = be32_to_cpu(reg); 1129b2bb4f2STakashi Sakamoto 1139b2bb4f2STakashi Sakamoto data &= ~(RX_ISOC_COMM_IS_ACTIVATED | TX_ISOC_COMM_IS_ACTIVATED); 1149b2bb4f2STakashi Sakamoto data |= CHANGE_RX_ISOC_COMM_STATE | CHANGE_TX_ISOC_COMM_STATE; 1159b2bb4f2STakashi Sakamoto 1169b2bb4f2STakashi Sakamoto reg = cpu_to_be32(data); 1179b2bb4f2STakashi Sakamoto snd_motu_transaction_write(motu, ISOC_COMM_CONTROL_OFFSET, ®, 1189b2bb4f2STakashi Sakamoto sizeof(reg)); 1199b2bb4f2STakashi Sakamoto 1209b2bb4f2STakashi Sakamoto fw_iso_resources_free(&motu->tx_resources); 1219b2bb4f2STakashi Sakamoto fw_iso_resources_free(&motu->rx_resources); 1229b2bb4f2STakashi Sakamoto } 1239b2bb4f2STakashi Sakamoto 1249b2bb4f2STakashi Sakamoto static int start_isoc_ctx(struct snd_motu *motu, struct amdtp_stream *stream) 1259b2bb4f2STakashi Sakamoto { 1269b2bb4f2STakashi Sakamoto struct fw_iso_resources *resources; 1279b2bb4f2STakashi Sakamoto int err; 1289b2bb4f2STakashi Sakamoto 1299b2bb4f2STakashi Sakamoto if (stream == &motu->rx_stream) 1309b2bb4f2STakashi Sakamoto resources = &motu->rx_resources; 1319b2bb4f2STakashi Sakamoto else 1329b2bb4f2STakashi Sakamoto resources = &motu->tx_resources; 1339b2bb4f2STakashi Sakamoto 1349b2bb4f2STakashi Sakamoto err = amdtp_stream_start(stream, resources->channel, 1359b2bb4f2STakashi Sakamoto fw_parent_device(motu->unit)->max_speed); 1369b2bb4f2STakashi Sakamoto if (err < 0) 1379b2bb4f2STakashi Sakamoto return err; 1389b2bb4f2STakashi Sakamoto 1399b2bb4f2STakashi Sakamoto if (!amdtp_stream_wait_callback(stream, CALLBACK_TIMEOUT)) { 1409b2bb4f2STakashi Sakamoto amdtp_stream_stop(stream); 1419b2bb4f2STakashi Sakamoto fw_iso_resources_free(resources); 1429b2bb4f2STakashi Sakamoto return -ETIMEDOUT; 1439b2bb4f2STakashi Sakamoto } 1449b2bb4f2STakashi Sakamoto 1459b2bb4f2STakashi Sakamoto return 0; 1469b2bb4f2STakashi Sakamoto } 1479b2bb4f2STakashi Sakamoto 1488b460c76STakashi Sakamoto int snd_motu_stream_cache_packet_formats(struct snd_motu *motu) 1498b460c76STakashi Sakamoto { 1508b460c76STakashi Sakamoto int err; 1518b460c76STakashi Sakamoto 1528b460c76STakashi Sakamoto err = motu->spec->protocol->cache_packet_formats(motu); 1538b460c76STakashi Sakamoto if (err < 0) 1548b460c76STakashi Sakamoto return err; 1558b460c76STakashi Sakamoto 1568b460c76STakashi Sakamoto if (motu->spec->flags & SND_MOTU_SPEC_TX_MIDI_2ND_Q) { 1578b460c76STakashi Sakamoto motu->tx_packet_formats.midi_flag_offset = 4; 1588b460c76STakashi Sakamoto motu->tx_packet_formats.midi_byte_offset = 6; 1598b460c76STakashi Sakamoto } else if (motu->spec->flags & SND_MOTU_SPEC_TX_MIDI_3RD_Q) { 1608b460c76STakashi Sakamoto motu->tx_packet_formats.midi_flag_offset = 8; 1618b460c76STakashi Sakamoto motu->tx_packet_formats.midi_byte_offset = 7; 1628b460c76STakashi Sakamoto } 1638b460c76STakashi Sakamoto 1648b460c76STakashi Sakamoto if (motu->spec->flags & SND_MOTU_SPEC_RX_MIDI_2ND_Q) { 1658b460c76STakashi Sakamoto motu->rx_packet_formats.midi_flag_offset = 4; 1668b460c76STakashi Sakamoto motu->rx_packet_formats.midi_byte_offset = 6; 1678b460c76STakashi Sakamoto } else if (motu->spec->flags & SND_MOTU_SPEC_RX_MIDI_3RD_Q) { 1688b460c76STakashi Sakamoto motu->rx_packet_formats.midi_flag_offset = 8; 1698b460c76STakashi Sakamoto motu->rx_packet_formats.midi_byte_offset = 7; 1708b460c76STakashi Sakamoto } 1718b460c76STakashi Sakamoto 1728b460c76STakashi Sakamoto return 0; 1738b460c76STakashi Sakamoto } 1748b460c76STakashi Sakamoto 1759b2bb4f2STakashi Sakamoto static int ensure_packet_formats(struct snd_motu *motu) 1769b2bb4f2STakashi Sakamoto { 1779b2bb4f2STakashi Sakamoto __be32 reg; 1789b2bb4f2STakashi Sakamoto u32 data; 1799b2bb4f2STakashi Sakamoto int err; 1809b2bb4f2STakashi Sakamoto 1819b2bb4f2STakashi Sakamoto err = snd_motu_transaction_read(motu, PACKET_FORMAT_OFFSET, ®, 1829b2bb4f2STakashi Sakamoto sizeof(reg)); 1839b2bb4f2STakashi Sakamoto if (err < 0) 1849b2bb4f2STakashi Sakamoto return err; 1859b2bb4f2STakashi Sakamoto data = be32_to_cpu(reg); 1869b2bb4f2STakashi Sakamoto 1879b2bb4f2STakashi Sakamoto data &= ~(TX_PACKET_EXCLUDE_DIFFERED_DATA_CHUNKS | 1889b2bb4f2STakashi Sakamoto RX_PACKET_EXCLUDE_DIFFERED_DATA_CHUNKS| 1899b2bb4f2STakashi Sakamoto TX_PACKET_TRANSMISSION_SPEED_MASK); 1909b2bb4f2STakashi Sakamoto if (motu->tx_packet_formats.differed_part_pcm_chunks[0] == 0) 1919b2bb4f2STakashi Sakamoto data |= TX_PACKET_EXCLUDE_DIFFERED_DATA_CHUNKS; 1929b2bb4f2STakashi Sakamoto if (motu->rx_packet_formats.differed_part_pcm_chunks[0] == 0) 1939b2bb4f2STakashi Sakamoto data |= RX_PACKET_EXCLUDE_DIFFERED_DATA_CHUNKS; 1949b2bb4f2STakashi Sakamoto data |= fw_parent_device(motu->unit)->max_speed; 1959b2bb4f2STakashi Sakamoto 1969b2bb4f2STakashi Sakamoto reg = cpu_to_be32(data); 1979b2bb4f2STakashi Sakamoto return snd_motu_transaction_write(motu, PACKET_FORMAT_OFFSET, ®, 1989b2bb4f2STakashi Sakamoto sizeof(reg)); 1999b2bb4f2STakashi Sakamoto } 2009b2bb4f2STakashi Sakamoto 2019b2bb4f2STakashi Sakamoto int snd_motu_stream_start_duplex(struct snd_motu *motu, unsigned int rate) 2029b2bb4f2STakashi Sakamoto { 2039b2bb4f2STakashi Sakamoto const struct snd_motu_protocol *protocol = motu->spec->protocol; 2049b2bb4f2STakashi Sakamoto unsigned int curr_rate; 2059b2bb4f2STakashi Sakamoto int err = 0; 2069b2bb4f2STakashi Sakamoto 20718f26034STakashi Sakamoto if (motu->substreams_counter == 0) 2089b2bb4f2STakashi Sakamoto return 0; 2099b2bb4f2STakashi Sakamoto 2108b460c76STakashi Sakamoto err = snd_motu_stream_cache_packet_formats(motu); 2119b2bb4f2STakashi Sakamoto if (err < 0) 2129b2bb4f2STakashi Sakamoto return err; 2139b2bb4f2STakashi Sakamoto 214fc49769aSTakashi Sakamoto // Stop stream if rate is different. 2159b2bb4f2STakashi Sakamoto err = protocol->get_clock_rate(motu, &curr_rate); 2169b2bb4f2STakashi Sakamoto if (err < 0) { 2179b2bb4f2STakashi Sakamoto dev_err(&motu->unit->device, 2189b2bb4f2STakashi Sakamoto "fail to get sampling rate: %d\n", err); 2199b2bb4f2STakashi Sakamoto return err; 2209b2bb4f2STakashi Sakamoto } 2219b2bb4f2STakashi Sakamoto if (rate == 0) 2229b2bb4f2STakashi Sakamoto rate = curr_rate; 223fc49769aSTakashi Sakamoto if (rate != curr_rate || 224fc49769aSTakashi Sakamoto amdtp_streaming_error(&motu->rx_stream) || 225*ec694fbaSTakashi Sakamoto amdtp_streaming_error(&motu->tx_stream)) 226b66ab142STakashi Sakamoto finish_session(motu); 2279b2bb4f2STakashi Sakamoto 2289b2bb4f2STakashi Sakamoto if (!amdtp_stream_running(&motu->rx_stream)) { 2299b2bb4f2STakashi Sakamoto err = protocol->set_clock_rate(motu, rate); 2309b2bb4f2STakashi Sakamoto if (err < 0) { 2319b2bb4f2STakashi Sakamoto dev_err(&motu->unit->device, 2329b2bb4f2STakashi Sakamoto "fail to set sampling rate: %d\n", err); 2339b2bb4f2STakashi Sakamoto return err; 2349b2bb4f2STakashi Sakamoto } 2359b2bb4f2STakashi Sakamoto 2369b2bb4f2STakashi Sakamoto err = ensure_packet_formats(motu); 2379b2bb4f2STakashi Sakamoto if (err < 0) 2389b2bb4f2STakashi Sakamoto return err; 2399b2bb4f2STakashi Sakamoto 240b66ab142STakashi Sakamoto err = begin_session(motu, rate); 2419b2bb4f2STakashi Sakamoto if (err < 0) { 2429b2bb4f2STakashi Sakamoto dev_err(&motu->unit->device, 2439b2bb4f2STakashi Sakamoto "fail to start isochronous comm: %d\n", err); 244f16e666bSMarkus Elfring goto stop_streams; 2459b2bb4f2STakashi Sakamoto } 2469b2bb4f2STakashi Sakamoto 2479b2bb4f2STakashi Sakamoto err = start_isoc_ctx(motu, &motu->rx_stream); 2489b2bb4f2STakashi Sakamoto if (err < 0) { 2499b2bb4f2STakashi Sakamoto dev_err(&motu->unit->device, 2509b2bb4f2STakashi Sakamoto "fail to start IT context: %d\n", err); 251f16e666bSMarkus Elfring goto stop_streams; 2529b2bb4f2STakashi Sakamoto } 2539b2bb4f2STakashi Sakamoto 2549b2bb4f2STakashi Sakamoto err = protocol->switch_fetching_mode(motu, true); 2559b2bb4f2STakashi Sakamoto if (err < 0) { 2569b2bb4f2STakashi Sakamoto dev_err(&motu->unit->device, 2579b2bb4f2STakashi Sakamoto "fail to enable frame fetching: %d\n", err); 258f16e666bSMarkus Elfring goto stop_streams; 2599b2bb4f2STakashi Sakamoto } 2609b2bb4f2STakashi Sakamoto } 2619b2bb4f2STakashi Sakamoto 26218f26034STakashi Sakamoto if (!amdtp_stream_running(&motu->tx_stream)) { 2639b2bb4f2STakashi Sakamoto err = start_isoc_ctx(motu, &motu->tx_stream); 2649b2bb4f2STakashi Sakamoto if (err < 0) { 2659b2bb4f2STakashi Sakamoto dev_err(&motu->unit->device, 2669b2bb4f2STakashi Sakamoto "fail to start IR context: %d", err); 2679b2bb4f2STakashi Sakamoto amdtp_stream_stop(&motu->rx_stream); 268f16e666bSMarkus Elfring goto stop_streams; 2699b2bb4f2STakashi Sakamoto } 2709b2bb4f2STakashi Sakamoto } 2719b2bb4f2STakashi Sakamoto 2729b2bb4f2STakashi Sakamoto return 0; 273f16e666bSMarkus Elfring 274f16e666bSMarkus Elfring stop_streams: 275b66ab142STakashi Sakamoto finish_session(motu); 276f16e666bSMarkus Elfring return err; 2779b2bb4f2STakashi Sakamoto } 2789b2bb4f2STakashi Sakamoto 2799b2bb4f2STakashi Sakamoto void snd_motu_stream_stop_duplex(struct snd_motu *motu) 2809b2bb4f2STakashi Sakamoto { 281*ec694fbaSTakashi Sakamoto if (motu->substreams_counter == 0) 282*ec694fbaSTakashi Sakamoto finish_session(motu); 2839b2bb4f2STakashi Sakamoto } 2849b2bb4f2STakashi Sakamoto 2859b2bb4f2STakashi Sakamoto static int init_stream(struct snd_motu *motu, enum amdtp_stream_direction dir) 2869b2bb4f2STakashi Sakamoto { 2879b2bb4f2STakashi Sakamoto int err; 2889b2bb4f2STakashi Sakamoto struct amdtp_stream *stream; 2899b2bb4f2STakashi Sakamoto struct fw_iso_resources *resources; 2909b2bb4f2STakashi Sakamoto 2919b2bb4f2STakashi Sakamoto if (dir == AMDTP_IN_STREAM) { 2929b2bb4f2STakashi Sakamoto stream = &motu->tx_stream; 2939b2bb4f2STakashi Sakamoto resources = &motu->tx_resources; 2949b2bb4f2STakashi Sakamoto } else { 2959b2bb4f2STakashi Sakamoto stream = &motu->rx_stream; 2969b2bb4f2STakashi Sakamoto resources = &motu->rx_resources; 2979b2bb4f2STakashi Sakamoto } 2989b2bb4f2STakashi Sakamoto 2999b2bb4f2STakashi Sakamoto err = fw_iso_resources_init(resources, motu->unit); 3009b2bb4f2STakashi Sakamoto if (err < 0) 3019b2bb4f2STakashi Sakamoto return err; 3029b2bb4f2STakashi Sakamoto 3039b2bb4f2STakashi Sakamoto err = amdtp_motu_init(stream, motu->unit, dir, motu->spec->protocol); 3049b2bb4f2STakashi Sakamoto if (err < 0) { 3059b2bb4f2STakashi Sakamoto amdtp_stream_destroy(stream); 3069b2bb4f2STakashi Sakamoto fw_iso_resources_destroy(resources); 3079b2bb4f2STakashi Sakamoto } 3089b2bb4f2STakashi Sakamoto 3099b2bb4f2STakashi Sakamoto return err; 3109b2bb4f2STakashi Sakamoto } 3119b2bb4f2STakashi Sakamoto 3129b2bb4f2STakashi Sakamoto static void destroy_stream(struct snd_motu *motu, 3139b2bb4f2STakashi Sakamoto enum amdtp_stream_direction dir) 3149b2bb4f2STakashi Sakamoto { 3159b2bb4f2STakashi Sakamoto struct amdtp_stream *stream; 3169b2bb4f2STakashi Sakamoto struct fw_iso_resources *resources; 3179b2bb4f2STakashi Sakamoto 3189b2bb4f2STakashi Sakamoto if (dir == AMDTP_IN_STREAM) { 3199b2bb4f2STakashi Sakamoto stream = &motu->tx_stream; 3209b2bb4f2STakashi Sakamoto resources = &motu->tx_resources; 3219b2bb4f2STakashi Sakamoto } else { 3229b2bb4f2STakashi Sakamoto stream = &motu->rx_stream; 3239b2bb4f2STakashi Sakamoto resources = &motu->rx_resources; 3249b2bb4f2STakashi Sakamoto } 3259b2bb4f2STakashi Sakamoto 3269b2bb4f2STakashi Sakamoto amdtp_stream_destroy(stream); 3279b2bb4f2STakashi Sakamoto fw_iso_resources_free(resources); 3289b2bb4f2STakashi Sakamoto } 3299b2bb4f2STakashi Sakamoto 3309b2bb4f2STakashi Sakamoto int snd_motu_stream_init_duplex(struct snd_motu *motu) 3319b2bb4f2STakashi Sakamoto { 3329b2bb4f2STakashi Sakamoto int err; 3339b2bb4f2STakashi Sakamoto 3349b2bb4f2STakashi Sakamoto err = init_stream(motu, AMDTP_IN_STREAM); 3359b2bb4f2STakashi Sakamoto if (err < 0) 3369b2bb4f2STakashi Sakamoto return err; 3379b2bb4f2STakashi Sakamoto 3389b2bb4f2STakashi Sakamoto err = init_stream(motu, AMDTP_OUT_STREAM); 3399b2bb4f2STakashi Sakamoto if (err < 0) 3409b2bb4f2STakashi Sakamoto destroy_stream(motu, AMDTP_IN_STREAM); 3419b2bb4f2STakashi Sakamoto 3429b2bb4f2STakashi Sakamoto return err; 3439b2bb4f2STakashi Sakamoto } 3449b2bb4f2STakashi Sakamoto 3459b2bb4f2STakashi Sakamoto /* 3469b2bb4f2STakashi Sakamoto * This function should be called before starting streams or after stopping 3479b2bb4f2STakashi Sakamoto * streams. 3489b2bb4f2STakashi Sakamoto */ 3499b2bb4f2STakashi Sakamoto void snd_motu_stream_destroy_duplex(struct snd_motu *motu) 3509b2bb4f2STakashi Sakamoto { 3519b2bb4f2STakashi Sakamoto destroy_stream(motu, AMDTP_IN_STREAM); 3529b2bb4f2STakashi Sakamoto destroy_stream(motu, AMDTP_OUT_STREAM); 3539b2bb4f2STakashi Sakamoto 35418f26034STakashi Sakamoto motu->substreams_counter = 0; 3559b2bb4f2STakashi Sakamoto } 35671c37977STakashi Sakamoto 35771c37977STakashi Sakamoto static void motu_lock_changed(struct snd_motu *motu) 35871c37977STakashi Sakamoto { 35971c37977STakashi Sakamoto motu->dev_lock_changed = true; 36071c37977STakashi Sakamoto wake_up(&motu->hwdep_wait); 36171c37977STakashi Sakamoto } 36271c37977STakashi Sakamoto 36371c37977STakashi Sakamoto int snd_motu_stream_lock_try(struct snd_motu *motu) 36471c37977STakashi Sakamoto { 36571c37977STakashi Sakamoto int err; 36671c37977STakashi Sakamoto 36771c37977STakashi Sakamoto spin_lock_irq(&motu->lock); 36871c37977STakashi Sakamoto 36971c37977STakashi Sakamoto if (motu->dev_lock_count < 0) { 37071c37977STakashi Sakamoto err = -EBUSY; 37171c37977STakashi Sakamoto goto out; 37271c37977STakashi Sakamoto } 37371c37977STakashi Sakamoto 37471c37977STakashi Sakamoto if (motu->dev_lock_count++ == 0) 37571c37977STakashi Sakamoto motu_lock_changed(motu); 37671c37977STakashi Sakamoto err = 0; 37771c37977STakashi Sakamoto out: 37871c37977STakashi Sakamoto spin_unlock_irq(&motu->lock); 37971c37977STakashi Sakamoto return err; 38071c37977STakashi Sakamoto } 38171c37977STakashi Sakamoto 38271c37977STakashi Sakamoto void snd_motu_stream_lock_release(struct snd_motu *motu) 38371c37977STakashi Sakamoto { 38471c37977STakashi Sakamoto spin_lock_irq(&motu->lock); 38571c37977STakashi Sakamoto 38671c37977STakashi Sakamoto if (WARN_ON(motu->dev_lock_count <= 0)) 38771c37977STakashi Sakamoto goto out; 38871c37977STakashi Sakamoto 38971c37977STakashi Sakamoto if (--motu->dev_lock_count == 0) 39071c37977STakashi Sakamoto motu_lock_changed(motu); 39171c37977STakashi Sakamoto out: 39271c37977STakashi Sakamoto spin_unlock_irq(&motu->lock); 39371c37977STakashi Sakamoto } 394