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 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 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 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 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 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); 194e50dfac8STakashi Sakamoto return err; 195e50dfac8STakashi Sakamoto } 1968edc56ecSTakashi Sakamoto } 1978edc56ecSTakashi Sakamoto 1988edc56ecSTakashi Sakamoto return 0; 1998edc56ecSTakashi Sakamoto } 2008edc56ecSTakashi Sakamoto 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 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 2588edc56ecSTakashi Sakamoto err = begin_session(motu); 2599b2bb4f2STakashi Sakamoto if (err < 0) { 2609b2bb4f2STakashi Sakamoto dev_err(&motu->unit->device, 2619b2bb4f2STakashi Sakamoto "fail to start isochronous comm: %d\n", err); 262f16e666bSMarkus Elfring goto stop_streams; 2639b2bb4f2STakashi Sakamoto } 2649b2bb4f2STakashi Sakamoto 265ccc6c1b0STakashi Sakamoto err = amdtp_domain_add_stream(&motu->domain, &motu->tx_stream, 266ccc6c1b0STakashi Sakamoto motu->tx_resources.channel, spd); 267ccc6c1b0STakashi Sakamoto if (err < 0) 268ccc6c1b0STakashi Sakamoto goto stop_streams; 269ccc6c1b0STakashi Sakamoto 270ccc6c1b0STakashi Sakamoto err = amdtp_domain_add_stream(&motu->domain, &motu->rx_stream, 271ccc6c1b0STakashi Sakamoto motu->rx_resources.channel, spd); 272ccc6c1b0STakashi Sakamoto if (err < 0) 273ccc6c1b0STakashi Sakamoto goto stop_streams; 274ccc6c1b0STakashi Sakamoto 275e50dfac8STakashi Sakamoto motu->cache.tail = 0; 276e50dfac8STakashi Sakamoto motu->cache.tx_cycle_count = UINT_MAX; 277*f2ac3b83STakashi Sakamoto motu->cache.head = 0; 278*f2ac3b83STakashi Sakamoto motu->cache.rx_cycle_count = UINT_MAX; 279e50dfac8STakashi Sakamoto 280*f2ac3b83STakashi Sakamoto // NOTE: The device requires both of replay; the sequence of the number of data 281*f2ac3b83STakashi Sakamoto // blocks per packet, and the sequence of source packet header per data block as 282*f2ac3b83STakashi Sakamoto // presentation time. 283*f2ac3b83STakashi Sakamoto err = amdtp_domain_start(&motu->domain, 0, true, false); 284ccc6c1b0STakashi Sakamoto if (err < 0) 285ccc6c1b0STakashi Sakamoto goto stop_streams; 286ccc6c1b0STakashi Sakamoto 287bdaedca7STakashi Sakamoto if (!amdtp_domain_wait_ready(&motu->domain, READY_TIMEOUT_MS)) { 288ccc6c1b0STakashi Sakamoto err = -ETIMEDOUT; 289f16e666bSMarkus Elfring goto stop_streams; 2909b2bb4f2STakashi Sakamoto } 2919b2bb4f2STakashi Sakamoto 292ff222b7eSTakashi Sakamoto err = snd_motu_protocol_switch_fetching_mode(motu, true); 2939b2bb4f2STakashi Sakamoto if (err < 0) { 2949b2bb4f2STakashi Sakamoto dev_err(&motu->unit->device, 2959b2bb4f2STakashi Sakamoto "fail to enable frame fetching: %d\n", err); 296f16e666bSMarkus Elfring goto stop_streams; 2979b2bb4f2STakashi Sakamoto } 2989b2bb4f2STakashi Sakamoto } 2999b2bb4f2STakashi Sakamoto 3009b2bb4f2STakashi Sakamoto return 0; 301f16e666bSMarkus Elfring 302f16e666bSMarkus Elfring stop_streams: 303ccc6c1b0STakashi Sakamoto amdtp_domain_stop(&motu->domain); 304b66ab142STakashi Sakamoto finish_session(motu); 305f16e666bSMarkus Elfring return err; 3069b2bb4f2STakashi Sakamoto } 3079b2bb4f2STakashi Sakamoto 3089b2bb4f2STakashi Sakamoto void snd_motu_stream_stop_duplex(struct snd_motu *motu) 3099b2bb4f2STakashi Sakamoto { 310eccd895cSTakashi Sakamoto if (motu->substreams_counter == 0) { 311ccc6c1b0STakashi Sakamoto amdtp_domain_stop(&motu->domain); 312ec694fbaSTakashi Sakamoto finish_session(motu); 313eccd895cSTakashi Sakamoto 314eccd895cSTakashi Sakamoto fw_iso_resources_free(&motu->tx_resources); 315eccd895cSTakashi Sakamoto fw_iso_resources_free(&motu->rx_resources); 316e50dfac8STakashi Sakamoto 317e50dfac8STakashi Sakamoto kfree(motu->cache.event_offsets); 318e50dfac8STakashi Sakamoto motu->cache.event_offsets = NULL; 319eccd895cSTakashi Sakamoto } 3209b2bb4f2STakashi Sakamoto } 3219b2bb4f2STakashi Sakamoto 32239e522a5STakashi Sakamoto static int init_stream(struct snd_motu *motu, struct amdtp_stream *s) 3239b2bb4f2STakashi Sakamoto { 3249b2bb4f2STakashi Sakamoto struct fw_iso_resources *resources; 32539e522a5STakashi Sakamoto enum amdtp_stream_direction dir; 32639e522a5STakashi Sakamoto int err; 3279b2bb4f2STakashi Sakamoto 32839e522a5STakashi Sakamoto if (s == &motu->tx_stream) { 3299b2bb4f2STakashi Sakamoto resources = &motu->tx_resources; 33039e522a5STakashi Sakamoto dir = AMDTP_IN_STREAM; 3319b2bb4f2STakashi Sakamoto } else { 3329b2bb4f2STakashi Sakamoto resources = &motu->rx_resources; 33339e522a5STakashi Sakamoto dir = AMDTP_OUT_STREAM; 3349b2bb4f2STakashi Sakamoto } 3359b2bb4f2STakashi Sakamoto 3369b2bb4f2STakashi Sakamoto err = fw_iso_resources_init(resources, motu->unit); 3379b2bb4f2STakashi Sakamoto if (err < 0) 3389b2bb4f2STakashi Sakamoto return err; 3399b2bb4f2STakashi Sakamoto 340e50dfac8STakashi Sakamoto err = amdtp_motu_init(s, motu->unit, dir, motu->spec, &motu->cache); 34139e522a5STakashi Sakamoto if (err < 0) 3429b2bb4f2STakashi Sakamoto fw_iso_resources_destroy(resources); 3439b2bb4f2STakashi Sakamoto 3449b2bb4f2STakashi Sakamoto return err; 3459b2bb4f2STakashi Sakamoto } 3469b2bb4f2STakashi Sakamoto 34739e522a5STakashi Sakamoto static void destroy_stream(struct snd_motu *motu, struct amdtp_stream *s) 3489b2bb4f2STakashi Sakamoto { 34939e522a5STakashi Sakamoto amdtp_stream_destroy(s); 3509b2bb4f2STakashi Sakamoto 35139e522a5STakashi Sakamoto if (s == &motu->tx_stream) 35239e522a5STakashi Sakamoto fw_iso_resources_destroy(&motu->tx_resources); 35339e522a5STakashi Sakamoto else 35439e522a5STakashi Sakamoto fw_iso_resources_destroy(&motu->rx_resources); 3559b2bb4f2STakashi Sakamoto } 3569b2bb4f2STakashi Sakamoto 3579b2bb4f2STakashi Sakamoto int snd_motu_stream_init_duplex(struct snd_motu *motu) 3589b2bb4f2STakashi Sakamoto { 3599b2bb4f2STakashi Sakamoto int err; 3609b2bb4f2STakashi Sakamoto 36139e522a5STakashi Sakamoto err = init_stream(motu, &motu->tx_stream); 3629b2bb4f2STakashi Sakamoto if (err < 0) 3639b2bb4f2STakashi Sakamoto return err; 3649b2bb4f2STakashi Sakamoto 36539e522a5STakashi Sakamoto err = init_stream(motu, &motu->rx_stream); 366ccc6c1b0STakashi Sakamoto if (err < 0) { 36739e522a5STakashi Sakamoto destroy_stream(motu, &motu->tx_stream); 368ccc6c1b0STakashi Sakamoto return err; 369ccc6c1b0STakashi Sakamoto } 370ccc6c1b0STakashi Sakamoto 371ccc6c1b0STakashi Sakamoto err = amdtp_domain_init(&motu->domain); 372ccc6c1b0STakashi Sakamoto if (err < 0) { 373ccc6c1b0STakashi Sakamoto destroy_stream(motu, &motu->tx_stream); 374ccc6c1b0STakashi Sakamoto destroy_stream(motu, &motu->rx_stream); 375ccc6c1b0STakashi Sakamoto } 3769b2bb4f2STakashi Sakamoto 3779b2bb4f2STakashi Sakamoto return err; 3789b2bb4f2STakashi Sakamoto } 3799b2bb4f2STakashi Sakamoto 380ccc6c1b0STakashi Sakamoto // This function should be called before starting streams or after stopping 381ccc6c1b0STakashi Sakamoto // streams. 3829b2bb4f2STakashi Sakamoto void snd_motu_stream_destroy_duplex(struct snd_motu *motu) 3839b2bb4f2STakashi Sakamoto { 384ccc6c1b0STakashi Sakamoto amdtp_domain_destroy(&motu->domain); 385ccc6c1b0STakashi Sakamoto 38639e522a5STakashi Sakamoto destroy_stream(motu, &motu->rx_stream); 38739e522a5STakashi Sakamoto destroy_stream(motu, &motu->tx_stream); 3889b2bb4f2STakashi Sakamoto 38918f26034STakashi Sakamoto motu->substreams_counter = 0; 3909b2bb4f2STakashi Sakamoto } 39171c37977STakashi Sakamoto 39271c37977STakashi Sakamoto static void motu_lock_changed(struct snd_motu *motu) 39371c37977STakashi Sakamoto { 39471c37977STakashi Sakamoto motu->dev_lock_changed = true; 39571c37977STakashi Sakamoto wake_up(&motu->hwdep_wait); 39671c37977STakashi Sakamoto } 39771c37977STakashi Sakamoto 39871c37977STakashi Sakamoto int snd_motu_stream_lock_try(struct snd_motu *motu) 39971c37977STakashi Sakamoto { 40071c37977STakashi Sakamoto int err; 40171c37977STakashi Sakamoto 40271c37977STakashi Sakamoto spin_lock_irq(&motu->lock); 40371c37977STakashi Sakamoto 40471c37977STakashi Sakamoto if (motu->dev_lock_count < 0) { 40571c37977STakashi Sakamoto err = -EBUSY; 40671c37977STakashi Sakamoto goto out; 40771c37977STakashi Sakamoto } 40871c37977STakashi Sakamoto 40971c37977STakashi Sakamoto if (motu->dev_lock_count++ == 0) 41071c37977STakashi Sakamoto motu_lock_changed(motu); 41171c37977STakashi Sakamoto err = 0; 41271c37977STakashi Sakamoto out: 41371c37977STakashi Sakamoto spin_unlock_irq(&motu->lock); 41471c37977STakashi Sakamoto return err; 41571c37977STakashi Sakamoto } 41671c37977STakashi Sakamoto 41771c37977STakashi Sakamoto void snd_motu_stream_lock_release(struct snd_motu *motu) 41871c37977STakashi Sakamoto { 41971c37977STakashi Sakamoto spin_lock_irq(&motu->lock); 42071c37977STakashi Sakamoto 42171c37977STakashi Sakamoto if (WARN_ON(motu->dev_lock_count <= 0)) 42271c37977STakashi Sakamoto goto out; 42371c37977STakashi Sakamoto 42471c37977STakashi Sakamoto if (--motu->dev_lock_count == 0) 42571c37977STakashi Sakamoto motu_lock_changed(motu); 42671c37977STakashi Sakamoto out: 42771c37977STakashi Sakamoto spin_unlock_irq(&motu->lock); 42871c37977STakashi Sakamoto } 429