2 * motu-stream.c - a part of driver for MOTU FireWire series
4 * Copyright (c) 2015-2017 Takashi Sakamoto <o-takashi@sakamocchi.jp>
6 * Licensed under the terms of the GNU General Public License, version 2.
11 #define CALLBACK_TIMEOUT 200
13 #define ISOC_COMM_CONTROL_OFFSET 0x0b00
14 #define ISOC_COMM_CONTROL_MASK 0xffff0000
15 #define CHANGE_RX_ISOC_COMM_STATE 0x80000000
16 #define RX_ISOC_COMM_IS_ACTIVATED 0x40000000
17 #define RX_ISOC_COMM_CHANNEL_MASK 0x3f000000
18 #define RX_ISOC_COMM_CHANNEL_SHIFT 24
19 #define CHANGE_TX_ISOC_COMM_STATE 0x00800000
20 #define TX_ISOC_COMM_IS_ACTIVATED 0x00400000
21 #define TX_ISOC_COMM_CHANNEL_MASK 0x003f0000
22 #define TX_ISOC_COMM_CHANNEL_SHIFT 16
24 #define PACKET_FORMAT_OFFSET 0x0b10
25 #define TX_PACKET_EXCLUDE_DIFFERED_DATA_CHUNKS 0x00000080
26 #define RX_PACKET_EXCLUDE_DIFFERED_DATA_CHUNKS 0x00000040
27 #define TX_PACKET_TRANSMISSION_SPEED_MASK 0x0000000f
29 static int keep_resources(struct snd_motu *motu, unsigned int rate,
30 struct amdtp_stream *stream)
32 struct fw_iso_resources *resources;
33 struct snd_motu_packet_format *packet_format;
34 unsigned int midi_ports = 0;
37 if (stream == &motu->rx_stream) {
38 resources = &motu->rx_resources;
39 packet_format = &motu->rx_packet_formats;
41 if ((motu->spec->flags & SND_MOTU_SPEC_RX_MIDI_2ND_Q) ||
42 (motu->spec->flags & SND_MOTU_SPEC_RX_MIDI_3RD_Q))
45 resources = &motu->tx_resources;
46 packet_format = &motu->tx_packet_formats;
48 if ((motu->spec->flags & SND_MOTU_SPEC_TX_MIDI_2ND_Q) ||
49 (motu->spec->flags & SND_MOTU_SPEC_TX_MIDI_3RD_Q))
53 err = amdtp_motu_set_parameters(stream, rate, midi_ports,
58 return fw_iso_resources_allocate(resources,
59 amdtp_stream_get_max_payload(stream),
60 fw_parent_device(motu->unit)->max_speed);
63 static int begin_session(struct snd_motu *motu)
69 // Configure the unit to start isochronous communication.
70 err = snd_motu_transaction_read(motu, ISOC_COMM_CONTROL_OFFSET, ®,
74 data = be32_to_cpu(reg) & ~ISOC_COMM_CONTROL_MASK;
76 data |= CHANGE_RX_ISOC_COMM_STATE | RX_ISOC_COMM_IS_ACTIVATED |
77 (motu->rx_resources.channel << RX_ISOC_COMM_CHANNEL_SHIFT) |
78 CHANGE_TX_ISOC_COMM_STATE | TX_ISOC_COMM_IS_ACTIVATED |
79 (motu->tx_resources.channel << TX_ISOC_COMM_CHANNEL_SHIFT);
81 reg = cpu_to_be32(data);
82 return snd_motu_transaction_write(motu, ISOC_COMM_CONTROL_OFFSET, ®,
86 static void finish_session(struct snd_motu *motu)
92 err = motu->spec->protocol->switch_fetching_mode(motu, false);
96 amdtp_stream_stop(&motu->tx_stream);
97 amdtp_stream_stop(&motu->rx_stream);
99 err = snd_motu_transaction_read(motu, ISOC_COMM_CONTROL_OFFSET, ®,
103 data = be32_to_cpu(reg);
105 data &= ~(RX_ISOC_COMM_IS_ACTIVATED | TX_ISOC_COMM_IS_ACTIVATED);
106 data |= CHANGE_RX_ISOC_COMM_STATE | CHANGE_TX_ISOC_COMM_STATE;
108 reg = cpu_to_be32(data);
109 snd_motu_transaction_write(motu, ISOC_COMM_CONTROL_OFFSET, ®,
113 static int start_isoc_ctx(struct snd_motu *motu, struct amdtp_stream *stream)
115 struct fw_iso_resources *resources;
118 if (stream == &motu->rx_stream)
119 resources = &motu->rx_resources;
121 resources = &motu->tx_resources;
123 err = amdtp_stream_start(stream, resources->channel,
124 fw_parent_device(motu->unit)->max_speed);
128 if (!amdtp_stream_wait_callback(stream, CALLBACK_TIMEOUT))
134 int snd_motu_stream_cache_packet_formats(struct snd_motu *motu)
138 err = motu->spec->protocol->cache_packet_formats(motu);
142 if (motu->spec->flags & SND_MOTU_SPEC_TX_MIDI_2ND_Q) {
143 motu->tx_packet_formats.midi_flag_offset = 4;
144 motu->tx_packet_formats.midi_byte_offset = 6;
145 } else if (motu->spec->flags & SND_MOTU_SPEC_TX_MIDI_3RD_Q) {
146 motu->tx_packet_formats.midi_flag_offset = 8;
147 motu->tx_packet_formats.midi_byte_offset = 7;
150 if (motu->spec->flags & SND_MOTU_SPEC_RX_MIDI_2ND_Q) {
151 motu->rx_packet_formats.midi_flag_offset = 4;
152 motu->rx_packet_formats.midi_byte_offset = 6;
153 } else if (motu->spec->flags & SND_MOTU_SPEC_RX_MIDI_3RD_Q) {
154 motu->rx_packet_formats.midi_flag_offset = 8;
155 motu->rx_packet_formats.midi_byte_offset = 7;
161 int snd_motu_stream_reserve_duplex(struct snd_motu *motu, unsigned int rate)
163 unsigned int curr_rate;
166 err = motu->spec->protocol->get_clock_rate(motu, &curr_rate);
172 if (motu->substreams_counter == 0 || curr_rate != rate) {
173 finish_session(motu);
175 fw_iso_resources_free(&motu->tx_resources);
176 fw_iso_resources_free(&motu->rx_resources);
178 err = motu->spec->protocol->set_clock_rate(motu, rate);
180 dev_err(&motu->unit->device,
181 "fail to set sampling rate: %d\n", err);
185 err = snd_motu_stream_cache_packet_formats(motu);
189 err = keep_resources(motu, rate, &motu->tx_stream);
193 err = keep_resources(motu, rate, &motu->rx_stream);
195 fw_iso_resources_free(&motu->tx_resources);
203 static int ensure_packet_formats(struct snd_motu *motu)
209 err = snd_motu_transaction_read(motu, PACKET_FORMAT_OFFSET, ®,
213 data = be32_to_cpu(reg);
215 data &= ~(TX_PACKET_EXCLUDE_DIFFERED_DATA_CHUNKS |
216 RX_PACKET_EXCLUDE_DIFFERED_DATA_CHUNKS|
217 TX_PACKET_TRANSMISSION_SPEED_MASK);
218 if (motu->tx_packet_formats.differed_part_pcm_chunks[0] == 0)
219 data |= TX_PACKET_EXCLUDE_DIFFERED_DATA_CHUNKS;
220 if (motu->rx_packet_formats.differed_part_pcm_chunks[0] == 0)
221 data |= RX_PACKET_EXCLUDE_DIFFERED_DATA_CHUNKS;
222 data |= fw_parent_device(motu->unit)->max_speed;
224 reg = cpu_to_be32(data);
225 return snd_motu_transaction_write(motu, PACKET_FORMAT_OFFSET, ®,
229 int snd_motu_stream_start_duplex(struct snd_motu *motu)
231 unsigned int generation = motu->rx_resources.generation;
234 if (motu->substreams_counter == 0)
237 if (amdtp_streaming_error(&motu->rx_stream) ||
238 amdtp_streaming_error(&motu->tx_stream))
239 finish_session(motu);
241 if (generation != fw_parent_device(motu->unit)->card->generation) {
242 err = fw_iso_resources_update(&motu->rx_resources);
246 err = fw_iso_resources_update(&motu->tx_resources);
251 if (!amdtp_stream_running(&motu->rx_stream)) {
252 err = ensure_packet_formats(motu);
256 err = begin_session(motu);
258 dev_err(&motu->unit->device,
259 "fail to start isochronous comm: %d\n", err);
263 err = start_isoc_ctx(motu, &motu->rx_stream);
265 dev_err(&motu->unit->device,
266 "fail to start IT context: %d\n", err);
270 err = motu->spec->protocol->switch_fetching_mode(motu, true);
272 dev_err(&motu->unit->device,
273 "fail to enable frame fetching: %d\n", err);
278 if (!amdtp_stream_running(&motu->tx_stream)) {
279 err = start_isoc_ctx(motu, &motu->tx_stream);
281 dev_err(&motu->unit->device,
282 "fail to start IR context: %d", err);
290 finish_session(motu);
294 void snd_motu_stream_stop_duplex(struct snd_motu *motu)
296 if (motu->substreams_counter == 0) {
297 finish_session(motu);
299 fw_iso_resources_free(&motu->tx_resources);
300 fw_iso_resources_free(&motu->rx_resources);
304 static int init_stream(struct snd_motu *motu, enum amdtp_stream_direction dir)
307 struct amdtp_stream *stream;
308 struct fw_iso_resources *resources;
310 if (dir == AMDTP_IN_STREAM) {
311 stream = &motu->tx_stream;
312 resources = &motu->tx_resources;
314 stream = &motu->rx_stream;
315 resources = &motu->rx_resources;
318 err = fw_iso_resources_init(resources, motu->unit);
322 err = amdtp_motu_init(stream, motu->unit, dir, motu->spec->protocol);
324 amdtp_stream_destroy(stream);
325 fw_iso_resources_destroy(resources);
331 static void destroy_stream(struct snd_motu *motu,
332 enum amdtp_stream_direction dir)
334 struct amdtp_stream *stream;
335 struct fw_iso_resources *resources;
337 if (dir == AMDTP_IN_STREAM) {
338 stream = &motu->tx_stream;
339 resources = &motu->tx_resources;
341 stream = &motu->rx_stream;
342 resources = &motu->rx_resources;
345 amdtp_stream_destroy(stream);
346 fw_iso_resources_destroy(resources);
349 int snd_motu_stream_init_duplex(struct snd_motu *motu)
353 err = init_stream(motu, AMDTP_IN_STREAM);
357 err = init_stream(motu, AMDTP_OUT_STREAM);
359 destroy_stream(motu, AMDTP_IN_STREAM);
365 * This function should be called before starting streams or after stopping
368 void snd_motu_stream_destroy_duplex(struct snd_motu *motu)
370 destroy_stream(motu, AMDTP_IN_STREAM);
371 destroy_stream(motu, AMDTP_OUT_STREAM);
373 motu->substreams_counter = 0;
376 static void motu_lock_changed(struct snd_motu *motu)
378 motu->dev_lock_changed = true;
379 wake_up(&motu->hwdep_wait);
382 int snd_motu_stream_lock_try(struct snd_motu *motu)
386 spin_lock_irq(&motu->lock);
388 if (motu->dev_lock_count < 0) {
393 if (motu->dev_lock_count++ == 0)
394 motu_lock_changed(motu);
397 spin_unlock_irq(&motu->lock);
401 void snd_motu_stream_lock_release(struct snd_motu *motu)
403 spin_lock_irq(&motu->lock);
405 if (WARN_ON(motu->dev_lock_count <= 0))
408 if (--motu->dev_lock_count == 0)
409 motu_lock_changed(motu);
411 spin_unlock_irq(&motu->lock);