Thomas Gleixner | da607e1 | 2019-05-29 16:57:59 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Takashi Sakamoto | 9b2bb4f | 2017-03-22 21:30:20 +0900 | [diff] [blame] | 2 | /* |
| 3 | * motu-stream.c - a part of driver for MOTU FireWire series |
| 4 | * |
| 5 | * Copyright (c) 2015-2017 Takashi Sakamoto <o-takashi@sakamocchi.jp> |
Takashi Sakamoto | 9b2bb4f | 2017-03-22 21:30:20 +0900 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include "motu.h" |
| 9 | |
| 10 | #define CALLBACK_TIMEOUT 200 |
| 11 | |
| 12 | #define ISOC_COMM_CONTROL_OFFSET 0x0b00 |
| 13 | #define ISOC_COMM_CONTROL_MASK 0xffff0000 |
| 14 | #define CHANGE_RX_ISOC_COMM_STATE 0x80000000 |
| 15 | #define RX_ISOC_COMM_IS_ACTIVATED 0x40000000 |
| 16 | #define RX_ISOC_COMM_CHANNEL_MASK 0x3f000000 |
| 17 | #define RX_ISOC_COMM_CHANNEL_SHIFT 24 |
| 18 | #define CHANGE_TX_ISOC_COMM_STATE 0x00800000 |
| 19 | #define TX_ISOC_COMM_IS_ACTIVATED 0x00400000 |
| 20 | #define TX_ISOC_COMM_CHANNEL_MASK 0x003f0000 |
| 21 | #define TX_ISOC_COMM_CHANNEL_SHIFT 16 |
| 22 | |
| 23 | #define PACKET_FORMAT_OFFSET 0x0b10 |
| 24 | #define TX_PACKET_EXCLUDE_DIFFERED_DATA_CHUNKS 0x00000080 |
| 25 | #define RX_PACKET_EXCLUDE_DIFFERED_DATA_CHUNKS 0x00000040 |
| 26 | #define TX_PACKET_TRANSMISSION_SPEED_MASK 0x0000000f |
| 27 | |
Takashi Sakamoto | 8350132 | 2019-06-17 17:15:03 +0900 | [diff] [blame] | 28 | static int keep_resources(struct snd_motu *motu, unsigned int rate, |
| 29 | struct amdtp_stream *stream) |
| 30 | { |
| 31 | struct fw_iso_resources *resources; |
| 32 | struct snd_motu_packet_format *packet_format; |
| 33 | unsigned int midi_ports = 0; |
| 34 | int err; |
| 35 | |
| 36 | if (stream == &motu->rx_stream) { |
| 37 | resources = &motu->rx_resources; |
| 38 | packet_format = &motu->rx_packet_formats; |
| 39 | |
| 40 | if ((motu->spec->flags & SND_MOTU_SPEC_RX_MIDI_2ND_Q) || |
| 41 | (motu->spec->flags & SND_MOTU_SPEC_RX_MIDI_3RD_Q)) |
| 42 | midi_ports = 1; |
| 43 | } else { |
| 44 | resources = &motu->tx_resources; |
| 45 | packet_format = &motu->tx_packet_formats; |
| 46 | |
| 47 | if ((motu->spec->flags & SND_MOTU_SPEC_TX_MIDI_2ND_Q) || |
| 48 | (motu->spec->flags & SND_MOTU_SPEC_TX_MIDI_3RD_Q)) |
| 49 | midi_ports = 1; |
| 50 | } |
| 51 | |
| 52 | err = amdtp_motu_set_parameters(stream, rate, midi_ports, |
| 53 | packet_format); |
| 54 | if (err < 0) |
| 55 | return err; |
| 56 | |
| 57 | return fw_iso_resources_allocate(resources, |
| 58 | amdtp_stream_get_max_payload(stream), |
| 59 | fw_parent_device(motu->unit)->max_speed); |
| 60 | } |
| 61 | |
Takashi Sakamoto | 8edc56e | 2019-06-17 17:15:08 +0900 | [diff] [blame] | 62 | static int begin_session(struct snd_motu *motu) |
Takashi Sakamoto | 9b2bb4f | 2017-03-22 21:30:20 +0900 | [diff] [blame] | 63 | { |
| 64 | __be32 reg; |
| 65 | u32 data; |
| 66 | int err; |
| 67 | |
Takashi Sakamoto | 8350132 | 2019-06-17 17:15:03 +0900 | [diff] [blame] | 68 | // Configure the unit to start isochronous communication. |
Takashi Sakamoto | 9b2bb4f | 2017-03-22 21:30:20 +0900 | [diff] [blame] | 69 | err = snd_motu_transaction_read(motu, ISOC_COMM_CONTROL_OFFSET, ®, |
| 70 | sizeof(reg)); |
| 71 | if (err < 0) |
| 72 | return err; |
| 73 | data = be32_to_cpu(reg) & ~ISOC_COMM_CONTROL_MASK; |
| 74 | |
| 75 | data |= CHANGE_RX_ISOC_COMM_STATE | RX_ISOC_COMM_IS_ACTIVATED | |
| 76 | (motu->rx_resources.channel << RX_ISOC_COMM_CHANNEL_SHIFT) | |
| 77 | CHANGE_TX_ISOC_COMM_STATE | TX_ISOC_COMM_IS_ACTIVATED | |
| 78 | (motu->tx_resources.channel << TX_ISOC_COMM_CHANNEL_SHIFT); |
| 79 | |
| 80 | reg = cpu_to_be32(data); |
| 81 | return snd_motu_transaction_write(motu, ISOC_COMM_CONTROL_OFFSET, ®, |
| 82 | sizeof(reg)); |
| 83 | } |
| 84 | |
Takashi Sakamoto | b66ab14 | 2019-06-17 17:15:05 +0900 | [diff] [blame] | 85 | static void finish_session(struct snd_motu *motu) |
Takashi Sakamoto | 9b2bb4f | 2017-03-22 21:30:20 +0900 | [diff] [blame] | 86 | { |
| 87 | __be32 reg; |
| 88 | u32 data; |
| 89 | int err; |
| 90 | |
| 91 | err = motu->spec->protocol->switch_fetching_mode(motu, false); |
| 92 | if (err < 0) |
| 93 | return; |
| 94 | |
| 95 | err = snd_motu_transaction_read(motu, ISOC_COMM_CONTROL_OFFSET, ®, |
| 96 | sizeof(reg)); |
| 97 | if (err < 0) |
| 98 | return; |
| 99 | data = be32_to_cpu(reg); |
| 100 | |
| 101 | data &= ~(RX_ISOC_COMM_IS_ACTIVATED | TX_ISOC_COMM_IS_ACTIVATED); |
| 102 | data |= CHANGE_RX_ISOC_COMM_STATE | CHANGE_TX_ISOC_COMM_STATE; |
| 103 | |
| 104 | reg = cpu_to_be32(data); |
| 105 | snd_motu_transaction_write(motu, ISOC_COMM_CONTROL_OFFSET, ®, |
| 106 | sizeof(reg)); |
Takashi Sakamoto | 9b2bb4f | 2017-03-22 21:30:20 +0900 | [diff] [blame] | 107 | } |
| 108 | |
Takashi Sakamoto | 8b460c7 | 2017-08-20 21:25:03 +0900 | [diff] [blame] | 109 | int snd_motu_stream_cache_packet_formats(struct snd_motu *motu) |
| 110 | { |
| 111 | int err; |
| 112 | |
| 113 | err = motu->spec->protocol->cache_packet_formats(motu); |
| 114 | if (err < 0) |
| 115 | return err; |
| 116 | |
| 117 | if (motu->spec->flags & SND_MOTU_SPEC_TX_MIDI_2ND_Q) { |
| 118 | motu->tx_packet_formats.midi_flag_offset = 4; |
| 119 | motu->tx_packet_formats.midi_byte_offset = 6; |
| 120 | } else if (motu->spec->flags & SND_MOTU_SPEC_TX_MIDI_3RD_Q) { |
| 121 | motu->tx_packet_formats.midi_flag_offset = 8; |
| 122 | motu->tx_packet_formats.midi_byte_offset = 7; |
| 123 | } |
| 124 | |
| 125 | if (motu->spec->flags & SND_MOTU_SPEC_RX_MIDI_2ND_Q) { |
| 126 | motu->rx_packet_formats.midi_flag_offset = 4; |
| 127 | motu->rx_packet_formats.midi_byte_offset = 6; |
| 128 | } else if (motu->spec->flags & SND_MOTU_SPEC_RX_MIDI_3RD_Q) { |
| 129 | motu->rx_packet_formats.midi_flag_offset = 8; |
| 130 | motu->rx_packet_formats.midi_byte_offset = 7; |
| 131 | } |
| 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
Takashi Sakamoto | 0d39cd0 | 2019-10-07 20:05:23 +0900 | [diff] [blame] | 136 | int snd_motu_stream_reserve_duplex(struct snd_motu *motu, unsigned int rate, |
Takashi Sakamoto | 0f5482e | 2019-10-18 00:54:20 +0900 | [diff] [blame] | 137 | unsigned int frames_per_period, |
| 138 | unsigned int frames_per_buffer) |
Takashi Sakamoto | 8edc56e | 2019-06-17 17:15:08 +0900 | [diff] [blame] | 139 | { |
| 140 | unsigned int curr_rate; |
| 141 | int err; |
| 142 | |
| 143 | err = motu->spec->protocol->get_clock_rate(motu, &curr_rate); |
| 144 | if (err < 0) |
| 145 | return err; |
| 146 | if (rate == 0) |
| 147 | rate = curr_rate; |
| 148 | |
| 149 | if (motu->substreams_counter == 0 || curr_rate != rate) { |
Takashi Sakamoto | ccc6c1b | 2019-08-04 15:21:36 +0900 | [diff] [blame] | 150 | amdtp_domain_stop(&motu->domain); |
Takashi Sakamoto | 8edc56e | 2019-06-17 17:15:08 +0900 | [diff] [blame] | 151 | finish_session(motu); |
| 152 | |
| 153 | fw_iso_resources_free(&motu->tx_resources); |
| 154 | fw_iso_resources_free(&motu->rx_resources); |
| 155 | |
| 156 | err = motu->spec->protocol->set_clock_rate(motu, rate); |
| 157 | if (err < 0) { |
| 158 | dev_err(&motu->unit->device, |
| 159 | "fail to set sampling rate: %d\n", err); |
| 160 | return err; |
| 161 | } |
| 162 | |
| 163 | err = snd_motu_stream_cache_packet_formats(motu); |
| 164 | if (err < 0) |
| 165 | return err; |
| 166 | |
| 167 | err = keep_resources(motu, rate, &motu->tx_stream); |
| 168 | if (err < 0) |
| 169 | return err; |
| 170 | |
| 171 | err = keep_resources(motu, rate, &motu->rx_stream); |
| 172 | if (err < 0) { |
| 173 | fw_iso_resources_free(&motu->tx_resources); |
| 174 | return err; |
| 175 | } |
Takashi Sakamoto | 0d39cd0 | 2019-10-07 20:05:23 +0900 | [diff] [blame] | 176 | |
| 177 | err = amdtp_domain_set_events_per_period(&motu->domain, |
Takashi Sakamoto | 0f5482e | 2019-10-18 00:54:20 +0900 | [diff] [blame] | 178 | frames_per_period, frames_per_buffer); |
Takashi Sakamoto | 0d39cd0 | 2019-10-07 20:05:23 +0900 | [diff] [blame] | 179 | if (err < 0) { |
| 180 | fw_iso_resources_free(&motu->tx_resources); |
| 181 | fw_iso_resources_free(&motu->rx_resources); |
| 182 | return err; |
| 183 | } |
Takashi Sakamoto | 8edc56e | 2019-06-17 17:15:08 +0900 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | return 0; |
| 187 | } |
| 188 | |
Takashi Sakamoto | 9b2bb4f | 2017-03-22 21:30:20 +0900 | [diff] [blame] | 189 | static int ensure_packet_formats(struct snd_motu *motu) |
| 190 | { |
| 191 | __be32 reg; |
| 192 | u32 data; |
| 193 | int err; |
| 194 | |
| 195 | err = snd_motu_transaction_read(motu, PACKET_FORMAT_OFFSET, ®, |
| 196 | sizeof(reg)); |
| 197 | if (err < 0) |
| 198 | return err; |
| 199 | data = be32_to_cpu(reg); |
| 200 | |
| 201 | data &= ~(TX_PACKET_EXCLUDE_DIFFERED_DATA_CHUNKS | |
| 202 | RX_PACKET_EXCLUDE_DIFFERED_DATA_CHUNKS| |
| 203 | TX_PACKET_TRANSMISSION_SPEED_MASK); |
| 204 | if (motu->tx_packet_formats.differed_part_pcm_chunks[0] == 0) |
| 205 | data |= TX_PACKET_EXCLUDE_DIFFERED_DATA_CHUNKS; |
| 206 | if (motu->rx_packet_formats.differed_part_pcm_chunks[0] == 0) |
| 207 | data |= RX_PACKET_EXCLUDE_DIFFERED_DATA_CHUNKS; |
| 208 | data |= fw_parent_device(motu->unit)->max_speed; |
| 209 | |
| 210 | reg = cpu_to_be32(data); |
| 211 | return snd_motu_transaction_write(motu, PACKET_FORMAT_OFFSET, ®, |
| 212 | sizeof(reg)); |
| 213 | } |
| 214 | |
Takashi Sakamoto | 8edc56e | 2019-06-17 17:15:08 +0900 | [diff] [blame] | 215 | int snd_motu_stream_start_duplex(struct snd_motu *motu) |
Takashi Sakamoto | 9b2bb4f | 2017-03-22 21:30:20 +0900 | [diff] [blame] | 216 | { |
Takashi Sakamoto | 2d10342 | 2019-06-17 17:15:09 +0900 | [diff] [blame] | 217 | unsigned int generation = motu->rx_resources.generation; |
Takashi Sakamoto | 9b2bb4f | 2017-03-22 21:30:20 +0900 | [diff] [blame] | 218 | int err = 0; |
| 219 | |
Takashi Sakamoto | 18f2603 | 2019-06-17 17:15:01 +0900 | [diff] [blame] | 220 | if (motu->substreams_counter == 0) |
Takashi Sakamoto | 9b2bb4f | 2017-03-22 21:30:20 +0900 | [diff] [blame] | 221 | return 0; |
| 222 | |
Takashi Sakamoto | 8edc56e | 2019-06-17 17:15:08 +0900 | [diff] [blame] | 223 | if (amdtp_streaming_error(&motu->rx_stream) || |
Takashi Sakamoto | ccc6c1b | 2019-08-04 15:21:36 +0900 | [diff] [blame] | 224 | amdtp_streaming_error(&motu->tx_stream)) { |
| 225 | amdtp_domain_stop(&motu->domain); |
Takashi Sakamoto | b66ab14 | 2019-06-17 17:15:05 +0900 | [diff] [blame] | 226 | finish_session(motu); |
Takashi Sakamoto | ccc6c1b | 2019-08-04 15:21:36 +0900 | [diff] [blame] | 227 | } |
Takashi Sakamoto | 9b2bb4f | 2017-03-22 21:30:20 +0900 | [diff] [blame] | 228 | |
Takashi Sakamoto | 2d10342 | 2019-06-17 17:15:09 +0900 | [diff] [blame] | 229 | if (generation != fw_parent_device(motu->unit)->card->generation) { |
| 230 | err = fw_iso_resources_update(&motu->rx_resources); |
| 231 | if (err < 0) |
| 232 | return err; |
| 233 | |
| 234 | err = fw_iso_resources_update(&motu->tx_resources); |
| 235 | if (err < 0) |
| 236 | return err; |
| 237 | } |
| 238 | |
Takashi Sakamoto | 9b2bb4f | 2017-03-22 21:30:20 +0900 | [diff] [blame] | 239 | if (!amdtp_stream_running(&motu->rx_stream)) { |
Takashi Sakamoto | ccc6c1b | 2019-08-04 15:21:36 +0900 | [diff] [blame] | 240 | int spd = fw_parent_device(motu->unit)->max_speed; |
| 241 | |
Takashi Sakamoto | 9b2bb4f | 2017-03-22 21:30:20 +0900 | [diff] [blame] | 242 | err = ensure_packet_formats(motu); |
| 243 | if (err < 0) |
| 244 | return err; |
| 245 | |
Takashi Sakamoto | 8edc56e | 2019-06-17 17:15:08 +0900 | [diff] [blame] | 246 | err = begin_session(motu); |
Takashi Sakamoto | 9b2bb4f | 2017-03-22 21:30:20 +0900 | [diff] [blame] | 247 | if (err < 0) { |
| 248 | dev_err(&motu->unit->device, |
| 249 | "fail to start isochronous comm: %d\n", err); |
Markus Elfring | f16e666 | 2017-09-06 13:30:14 +0200 | [diff] [blame] | 250 | goto stop_streams; |
Takashi Sakamoto | 9b2bb4f | 2017-03-22 21:30:20 +0900 | [diff] [blame] | 251 | } |
| 252 | |
Takashi Sakamoto | ccc6c1b | 2019-08-04 15:21:36 +0900 | [diff] [blame] | 253 | err = amdtp_domain_add_stream(&motu->domain, &motu->tx_stream, |
| 254 | motu->tx_resources.channel, spd); |
| 255 | if (err < 0) |
| 256 | goto stop_streams; |
| 257 | |
| 258 | err = amdtp_domain_add_stream(&motu->domain, &motu->rx_stream, |
| 259 | motu->rx_resources.channel, spd); |
| 260 | if (err < 0) |
| 261 | goto stop_streams; |
| 262 | |
Takashi Sakamoto | acfedcb | 2019-10-18 15:19:11 +0900 | [diff] [blame] | 263 | err = amdtp_domain_start(&motu->domain, 0); |
Takashi Sakamoto | ccc6c1b | 2019-08-04 15:21:36 +0900 | [diff] [blame] | 264 | if (err < 0) |
| 265 | goto stop_streams; |
| 266 | |
| 267 | if (!amdtp_stream_wait_callback(&motu->tx_stream, |
| 268 | CALLBACK_TIMEOUT) || |
| 269 | !amdtp_stream_wait_callback(&motu->rx_stream, |
| 270 | CALLBACK_TIMEOUT)) { |
| 271 | err = -ETIMEDOUT; |
Markus Elfring | f16e666 | 2017-09-06 13:30:14 +0200 | [diff] [blame] | 272 | goto stop_streams; |
Takashi Sakamoto | 9b2bb4f | 2017-03-22 21:30:20 +0900 | [diff] [blame] | 273 | } |
| 274 | |
Takashi Sakamoto | 8edc56e | 2019-06-17 17:15:08 +0900 | [diff] [blame] | 275 | err = motu->spec->protocol->switch_fetching_mode(motu, true); |
Takashi Sakamoto | 9b2bb4f | 2017-03-22 21:30:20 +0900 | [diff] [blame] | 276 | if (err < 0) { |
| 277 | dev_err(&motu->unit->device, |
| 278 | "fail to enable frame fetching: %d\n", err); |
Markus Elfring | f16e666 | 2017-09-06 13:30:14 +0200 | [diff] [blame] | 279 | goto stop_streams; |
Takashi Sakamoto | 9b2bb4f | 2017-03-22 21:30:20 +0900 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | |
Takashi Sakamoto | 9b2bb4f | 2017-03-22 21:30:20 +0900 | [diff] [blame] | 283 | return 0; |
Markus Elfring | f16e666 | 2017-09-06 13:30:14 +0200 | [diff] [blame] | 284 | |
| 285 | stop_streams: |
Takashi Sakamoto | ccc6c1b | 2019-08-04 15:21:36 +0900 | [diff] [blame] | 286 | amdtp_domain_stop(&motu->domain); |
Takashi Sakamoto | b66ab14 | 2019-06-17 17:15:05 +0900 | [diff] [blame] | 287 | finish_session(motu); |
Markus Elfring | f16e666 | 2017-09-06 13:30:14 +0200 | [diff] [blame] | 288 | return err; |
Takashi Sakamoto | 9b2bb4f | 2017-03-22 21:30:20 +0900 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | void snd_motu_stream_stop_duplex(struct snd_motu *motu) |
| 292 | { |
Takashi Sakamoto | eccd895 | 2019-06-18 22:26:17 +0900 | [diff] [blame] | 293 | if (motu->substreams_counter == 0) { |
Takashi Sakamoto | ccc6c1b | 2019-08-04 15:21:36 +0900 | [diff] [blame] | 294 | amdtp_domain_stop(&motu->domain); |
Takashi Sakamoto | ec694fb | 2019-06-17 17:15:07 +0900 | [diff] [blame] | 295 | finish_session(motu); |
Takashi Sakamoto | eccd895 | 2019-06-18 22:26:17 +0900 | [diff] [blame] | 296 | |
| 297 | fw_iso_resources_free(&motu->tx_resources); |
| 298 | fw_iso_resources_free(&motu->rx_resources); |
| 299 | } |
Takashi Sakamoto | 9b2bb4f | 2017-03-22 21:30:20 +0900 | [diff] [blame] | 300 | } |
| 301 | |
Takashi Sakamoto | 39e522a | 2019-08-04 15:21:28 +0900 | [diff] [blame] | 302 | static int init_stream(struct snd_motu *motu, struct amdtp_stream *s) |
Takashi Sakamoto | 9b2bb4f | 2017-03-22 21:30:20 +0900 | [diff] [blame] | 303 | { |
Takashi Sakamoto | 9b2bb4f | 2017-03-22 21:30:20 +0900 | [diff] [blame] | 304 | struct fw_iso_resources *resources; |
Takashi Sakamoto | 39e522a | 2019-08-04 15:21:28 +0900 | [diff] [blame] | 305 | enum amdtp_stream_direction dir; |
| 306 | int err; |
Takashi Sakamoto | 9b2bb4f | 2017-03-22 21:30:20 +0900 | [diff] [blame] | 307 | |
Takashi Sakamoto | 39e522a | 2019-08-04 15:21:28 +0900 | [diff] [blame] | 308 | if (s == &motu->tx_stream) { |
Takashi Sakamoto | 9b2bb4f | 2017-03-22 21:30:20 +0900 | [diff] [blame] | 309 | resources = &motu->tx_resources; |
Takashi Sakamoto | 39e522a | 2019-08-04 15:21:28 +0900 | [diff] [blame] | 310 | dir = AMDTP_IN_STREAM; |
Takashi Sakamoto | 9b2bb4f | 2017-03-22 21:30:20 +0900 | [diff] [blame] | 311 | } else { |
Takashi Sakamoto | 9b2bb4f | 2017-03-22 21:30:20 +0900 | [diff] [blame] | 312 | resources = &motu->rx_resources; |
Takashi Sakamoto | 39e522a | 2019-08-04 15:21:28 +0900 | [diff] [blame] | 313 | dir = AMDTP_OUT_STREAM; |
Takashi Sakamoto | 9b2bb4f | 2017-03-22 21:30:20 +0900 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | err = fw_iso_resources_init(resources, motu->unit); |
| 317 | if (err < 0) |
| 318 | return err; |
| 319 | |
Takashi Sakamoto | 61d79c7 | 2020-05-19 20:16:30 +0900 | [diff] [blame^] | 320 | err = amdtp_motu_init(s, motu->unit, dir, motu->spec); |
Takashi Sakamoto | 39e522a | 2019-08-04 15:21:28 +0900 | [diff] [blame] | 321 | if (err < 0) |
Takashi Sakamoto | 9b2bb4f | 2017-03-22 21:30:20 +0900 | [diff] [blame] | 322 | fw_iso_resources_destroy(resources); |
Takashi Sakamoto | 9b2bb4f | 2017-03-22 21:30:20 +0900 | [diff] [blame] | 323 | |
| 324 | return err; |
| 325 | } |
| 326 | |
Takashi Sakamoto | 39e522a | 2019-08-04 15:21:28 +0900 | [diff] [blame] | 327 | static void destroy_stream(struct snd_motu *motu, struct amdtp_stream *s) |
Takashi Sakamoto | 9b2bb4f | 2017-03-22 21:30:20 +0900 | [diff] [blame] | 328 | { |
Takashi Sakamoto | 39e522a | 2019-08-04 15:21:28 +0900 | [diff] [blame] | 329 | amdtp_stream_destroy(s); |
Takashi Sakamoto | 9b2bb4f | 2017-03-22 21:30:20 +0900 | [diff] [blame] | 330 | |
Takashi Sakamoto | 39e522a | 2019-08-04 15:21:28 +0900 | [diff] [blame] | 331 | if (s == &motu->tx_stream) |
| 332 | fw_iso_resources_destroy(&motu->tx_resources); |
| 333 | else |
| 334 | fw_iso_resources_destroy(&motu->rx_resources); |
Takashi Sakamoto | 9b2bb4f | 2017-03-22 21:30:20 +0900 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | int snd_motu_stream_init_duplex(struct snd_motu *motu) |
| 338 | { |
| 339 | int err; |
| 340 | |
Takashi Sakamoto | 39e522a | 2019-08-04 15:21:28 +0900 | [diff] [blame] | 341 | err = init_stream(motu, &motu->tx_stream); |
Takashi Sakamoto | 9b2bb4f | 2017-03-22 21:30:20 +0900 | [diff] [blame] | 342 | if (err < 0) |
| 343 | return err; |
| 344 | |
Takashi Sakamoto | 39e522a | 2019-08-04 15:21:28 +0900 | [diff] [blame] | 345 | err = init_stream(motu, &motu->rx_stream); |
Takashi Sakamoto | ccc6c1b | 2019-08-04 15:21:36 +0900 | [diff] [blame] | 346 | if (err < 0) { |
Takashi Sakamoto | 39e522a | 2019-08-04 15:21:28 +0900 | [diff] [blame] | 347 | destroy_stream(motu, &motu->tx_stream); |
Takashi Sakamoto | ccc6c1b | 2019-08-04 15:21:36 +0900 | [diff] [blame] | 348 | return err; |
| 349 | } |
| 350 | |
| 351 | err = amdtp_domain_init(&motu->domain); |
| 352 | if (err < 0) { |
| 353 | destroy_stream(motu, &motu->tx_stream); |
| 354 | destroy_stream(motu, &motu->rx_stream); |
| 355 | } |
Takashi Sakamoto | 9b2bb4f | 2017-03-22 21:30:20 +0900 | [diff] [blame] | 356 | |
| 357 | return err; |
| 358 | } |
| 359 | |
Takashi Sakamoto | ccc6c1b | 2019-08-04 15:21:36 +0900 | [diff] [blame] | 360 | // This function should be called before starting streams or after stopping |
| 361 | // streams. |
Takashi Sakamoto | 9b2bb4f | 2017-03-22 21:30:20 +0900 | [diff] [blame] | 362 | void snd_motu_stream_destroy_duplex(struct snd_motu *motu) |
| 363 | { |
Takashi Sakamoto | ccc6c1b | 2019-08-04 15:21:36 +0900 | [diff] [blame] | 364 | amdtp_domain_destroy(&motu->domain); |
| 365 | |
Takashi Sakamoto | 39e522a | 2019-08-04 15:21:28 +0900 | [diff] [blame] | 366 | destroy_stream(motu, &motu->rx_stream); |
| 367 | destroy_stream(motu, &motu->tx_stream); |
Takashi Sakamoto | 9b2bb4f | 2017-03-22 21:30:20 +0900 | [diff] [blame] | 368 | |
Takashi Sakamoto | 18f2603 | 2019-06-17 17:15:01 +0900 | [diff] [blame] | 369 | motu->substreams_counter = 0; |
Takashi Sakamoto | 9b2bb4f | 2017-03-22 21:30:20 +0900 | [diff] [blame] | 370 | } |
Takashi Sakamoto | 71c3797 | 2017-03-22 21:30:24 +0900 | [diff] [blame] | 371 | |
| 372 | static void motu_lock_changed(struct snd_motu *motu) |
| 373 | { |
| 374 | motu->dev_lock_changed = true; |
| 375 | wake_up(&motu->hwdep_wait); |
| 376 | } |
| 377 | |
| 378 | int snd_motu_stream_lock_try(struct snd_motu *motu) |
| 379 | { |
| 380 | int err; |
| 381 | |
| 382 | spin_lock_irq(&motu->lock); |
| 383 | |
| 384 | if (motu->dev_lock_count < 0) { |
| 385 | err = -EBUSY; |
| 386 | goto out; |
| 387 | } |
| 388 | |
| 389 | if (motu->dev_lock_count++ == 0) |
| 390 | motu_lock_changed(motu); |
| 391 | err = 0; |
| 392 | out: |
| 393 | spin_unlock_irq(&motu->lock); |
| 394 | return err; |
| 395 | } |
| 396 | |
| 397 | void snd_motu_stream_lock_release(struct snd_motu *motu) |
| 398 | { |
| 399 | spin_lock_irq(&motu->lock); |
| 400 | |
| 401 | if (WARN_ON(motu->dev_lock_count <= 0)) |
| 402 | goto out; |
| 403 | |
| 404 | if (--motu->dev_lock_count == 0) |
| 405 | motu_lock_changed(motu); |
| 406 | out: |
| 407 | spin_unlock_irq(&motu->lock); |
| 408 | } |