1 // SPDX-License-Identifier: GPL-2.0-only
3 * motu-stream.c - a part of driver for MOTU FireWire series
5 * Copyright (c) 2015-2017 Takashi Sakamoto <o-takashi@sakamocchi.jp>
10 #define CALLBACK_TIMEOUT 200
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
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
28 static int keep_resources(struct snd_motu *motu, unsigned int rate,
29 struct amdtp_stream *stream)
31 struct fw_iso_resources *resources;
32 struct snd_motu_packet_format *packet_format;
33 unsigned int midi_ports = 0;
36 if (stream == &motu->rx_stream) {
37 resources = &motu->rx_resources;
38 packet_format = &motu->rx_packet_formats;
40 if ((motu->spec->flags & SND_MOTU_SPEC_RX_MIDI_2ND_Q) ||
41 (motu->spec->flags & SND_MOTU_SPEC_RX_MIDI_3RD_Q))
44 resources = &motu->tx_resources;
45 packet_format = &motu->tx_packet_formats;
47 if ((motu->spec->flags & SND_MOTU_SPEC_TX_MIDI_2ND_Q) ||
48 (motu->spec->flags & SND_MOTU_SPEC_TX_MIDI_3RD_Q))
52 err = amdtp_motu_set_parameters(stream, rate, midi_ports,
57 return fw_iso_resources_allocate(resources,
58 amdtp_stream_get_max_payload(stream),
59 fw_parent_device(motu->unit)->max_speed);
62 static int begin_session(struct snd_motu *motu)
68 // Configure the unit to start isochronous communication.
69 err = snd_motu_transaction_read(motu, ISOC_COMM_CONTROL_OFFSET, ®,
73 data = be32_to_cpu(reg) & ~ISOC_COMM_CONTROL_MASK;
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);
80 reg = cpu_to_be32(data);
81 return snd_motu_transaction_write(motu, ISOC_COMM_CONTROL_OFFSET, ®,
85 static void finish_session(struct snd_motu *motu)
91 err = motu->spec->protocol->switch_fetching_mode(motu, false);
95 amdtp_stream_stop(&motu->tx_stream);
96 amdtp_stream_stop(&motu->rx_stream);
98 err = snd_motu_transaction_read(motu, ISOC_COMM_CONTROL_OFFSET, ®,
102 data = be32_to_cpu(reg);
104 data &= ~(RX_ISOC_COMM_IS_ACTIVATED | TX_ISOC_COMM_IS_ACTIVATED);
105 data |= CHANGE_RX_ISOC_COMM_STATE | CHANGE_TX_ISOC_COMM_STATE;
107 reg = cpu_to_be32(data);
108 snd_motu_transaction_write(motu, ISOC_COMM_CONTROL_OFFSET, ®,
112 static int start_isoc_ctx(struct snd_motu *motu, struct amdtp_stream *stream)
114 struct fw_iso_resources *resources;
117 if (stream == &motu->rx_stream)
118 resources = &motu->rx_resources;
120 resources = &motu->tx_resources;
122 err = amdtp_stream_start(stream, resources->channel,
123 fw_parent_device(motu->unit)->max_speed);
127 if (!amdtp_stream_wait_callback(stream, CALLBACK_TIMEOUT))
133 int snd_motu_stream_cache_packet_formats(struct snd_motu *motu)
137 err = motu->spec->protocol->cache_packet_formats(motu);
141 if (motu->spec->flags & SND_MOTU_SPEC_TX_MIDI_2ND_Q) {
142 motu->tx_packet_formats.midi_flag_offset = 4;
143 motu->tx_packet_formats.midi_byte_offset = 6;
144 } else if (motu->spec->flags & SND_MOTU_SPEC_TX_MIDI_3RD_Q) {
145 motu->tx_packet_formats.midi_flag_offset = 8;
146 motu->tx_packet_formats.midi_byte_offset = 7;
149 if (motu->spec->flags & SND_MOTU_SPEC_RX_MIDI_2ND_Q) {
150 motu->rx_packet_formats.midi_flag_offset = 4;
151 motu->rx_packet_formats.midi_byte_offset = 6;
152 } else if (motu->spec->flags & SND_MOTU_SPEC_RX_MIDI_3RD_Q) {
153 motu->rx_packet_formats.midi_flag_offset = 8;
154 motu->rx_packet_formats.midi_byte_offset = 7;
160 int snd_motu_stream_reserve_duplex(struct snd_motu *motu, unsigned int rate)
162 unsigned int curr_rate;
165 err = motu->spec->protocol->get_clock_rate(motu, &curr_rate);
171 if (motu->substreams_counter == 0 || curr_rate != rate) {
172 finish_session(motu);
174 fw_iso_resources_free(&motu->tx_resources);
175 fw_iso_resources_free(&motu->rx_resources);
177 err = motu->spec->protocol->set_clock_rate(motu, rate);
179 dev_err(&motu->unit->device,
180 "fail to set sampling rate: %d\n", err);
184 err = snd_motu_stream_cache_packet_formats(motu);
188 err = keep_resources(motu, rate, &motu->tx_stream);
192 err = keep_resources(motu, rate, &motu->rx_stream);
194 fw_iso_resources_free(&motu->tx_resources);
202 static int ensure_packet_formats(struct snd_motu *motu)
208 err = snd_motu_transaction_read(motu, PACKET_FORMAT_OFFSET, ®,
212 data = be32_to_cpu(reg);
214 data &= ~(TX_PACKET_EXCLUDE_DIFFERED_DATA_CHUNKS |
215 RX_PACKET_EXCLUDE_DIFFERED_DATA_CHUNKS|
216 TX_PACKET_TRANSMISSION_SPEED_MASK);
217 if (motu->tx_packet_formats.differed_part_pcm_chunks[0] == 0)
218 data |= TX_PACKET_EXCLUDE_DIFFERED_DATA_CHUNKS;
219 if (motu->rx_packet_formats.differed_part_pcm_chunks[0] == 0)
220 data |= RX_PACKET_EXCLUDE_DIFFERED_DATA_CHUNKS;
221 data |= fw_parent_device(motu->unit)->max_speed;
223 reg = cpu_to_be32(data);
224 return snd_motu_transaction_write(motu, PACKET_FORMAT_OFFSET, ®,
228 int snd_motu_stream_start_duplex(struct snd_motu *motu)
230 unsigned int generation = motu->rx_resources.generation;
233 if (motu->substreams_counter == 0)
236 if (amdtp_streaming_error(&motu->rx_stream) ||
237 amdtp_streaming_error(&motu->tx_stream))
238 finish_session(motu);
240 if (generation != fw_parent_device(motu->unit)->card->generation) {
241 err = fw_iso_resources_update(&motu->rx_resources);
245 err = fw_iso_resources_update(&motu->tx_resources);
250 if (!amdtp_stream_running(&motu->rx_stream)) {
251 err = ensure_packet_formats(motu);
255 err = begin_session(motu);
257 dev_err(&motu->unit->device,
258 "fail to start isochronous comm: %d\n", err);
262 err = start_isoc_ctx(motu, &motu->rx_stream);
264 dev_err(&motu->unit->device,
265 "fail to start IT context: %d\n", err);
269 err = motu->spec->protocol->switch_fetching_mode(motu, true);
271 dev_err(&motu->unit->device,
272 "fail to enable frame fetching: %d\n", err);
277 if (!amdtp_stream_running(&motu->tx_stream)) {
278 err = start_isoc_ctx(motu, &motu->tx_stream);
280 dev_err(&motu->unit->device,
281 "fail to start IR context: %d", err);
289 finish_session(motu);
293 void snd_motu_stream_stop_duplex(struct snd_motu *motu)
295 if (motu->substreams_counter == 0) {
296 finish_session(motu);
298 fw_iso_resources_free(&motu->tx_resources);
299 fw_iso_resources_free(&motu->rx_resources);
303 static int init_stream(struct snd_motu *motu, enum amdtp_stream_direction dir)
306 struct amdtp_stream *stream;
307 struct fw_iso_resources *resources;
309 if (dir == AMDTP_IN_STREAM) {
310 stream = &motu->tx_stream;
311 resources = &motu->tx_resources;
313 stream = &motu->rx_stream;
314 resources = &motu->rx_resources;
317 err = fw_iso_resources_init(resources, motu->unit);
321 err = amdtp_motu_init(stream, motu->unit, dir, motu->spec->protocol);
323 amdtp_stream_destroy(stream);
324 fw_iso_resources_destroy(resources);
330 static void destroy_stream(struct snd_motu *motu,
331 enum amdtp_stream_direction dir)
333 struct amdtp_stream *stream;
334 struct fw_iso_resources *resources;
336 if (dir == AMDTP_IN_STREAM) {
337 stream = &motu->tx_stream;
338 resources = &motu->tx_resources;
340 stream = &motu->rx_stream;
341 resources = &motu->rx_resources;
344 amdtp_stream_destroy(stream);
345 fw_iso_resources_destroy(resources);
348 int snd_motu_stream_init_duplex(struct snd_motu *motu)
352 err = init_stream(motu, AMDTP_IN_STREAM);
356 err = init_stream(motu, AMDTP_OUT_STREAM);
358 destroy_stream(motu, AMDTP_IN_STREAM);
364 * This function should be called before starting streams or after stopping
367 void snd_motu_stream_destroy_duplex(struct snd_motu *motu)
369 destroy_stream(motu, AMDTP_IN_STREAM);
370 destroy_stream(motu, AMDTP_OUT_STREAM);
372 motu->substreams_counter = 0;
375 static void motu_lock_changed(struct snd_motu *motu)
377 motu->dev_lock_changed = true;
378 wake_up(&motu->hwdep_wait);
381 int snd_motu_stream_lock_try(struct snd_motu *motu)
385 spin_lock_irq(&motu->lock);
387 if (motu->dev_lock_count < 0) {
392 if (motu->dev_lock_count++ == 0)
393 motu_lock_changed(motu);
396 spin_unlock_irq(&motu->lock);
400 void snd_motu_stream_lock_release(struct snd_motu *motu)
402 spin_lock_irq(&motu->lock);
404 if (WARN_ON(motu->dev_lock_count <= 0))
407 if (--motu->dev_lock_count == 0)
408 motu_lock_changed(motu);
410 spin_unlock_irq(&motu->lock);