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 err = snd_motu_transaction_read(motu, ISOC_COMM_CONTROL_OFFSET, ®,
99 data = be32_to_cpu(reg);
101 data &= ~(RX_ISOC_COMM_IS_ACTIVATED | TX_ISOC_COMM_IS_ACTIVATED);
102 data |= CHANGE_RX_ISOC_COMM_STATE | CHANGE_TX_ISOC_COMM_STATE;
104 reg = cpu_to_be32(data);
105 snd_motu_transaction_write(motu, ISOC_COMM_CONTROL_OFFSET, ®,
109 int snd_motu_stream_cache_packet_formats(struct snd_motu *motu)
113 err = motu->spec->protocol->cache_packet_formats(motu);
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;
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;
136 int snd_motu_stream_reserve_duplex(struct snd_motu *motu, unsigned int rate)
138 unsigned int curr_rate;
141 err = motu->spec->protocol->get_clock_rate(motu, &curr_rate);
147 if (motu->substreams_counter == 0 || curr_rate != rate) {
148 amdtp_domain_stop(&motu->domain);
149 finish_session(motu);
151 fw_iso_resources_free(&motu->tx_resources);
152 fw_iso_resources_free(&motu->rx_resources);
154 err = motu->spec->protocol->set_clock_rate(motu, rate);
156 dev_err(&motu->unit->device,
157 "fail to set sampling rate: %d\n", err);
161 err = snd_motu_stream_cache_packet_formats(motu);
165 err = keep_resources(motu, rate, &motu->tx_stream);
169 err = keep_resources(motu, rate, &motu->rx_stream);
171 fw_iso_resources_free(&motu->tx_resources);
179 static int ensure_packet_formats(struct snd_motu *motu)
185 err = snd_motu_transaction_read(motu, PACKET_FORMAT_OFFSET, ®,
189 data = be32_to_cpu(reg);
191 data &= ~(TX_PACKET_EXCLUDE_DIFFERED_DATA_CHUNKS |
192 RX_PACKET_EXCLUDE_DIFFERED_DATA_CHUNKS|
193 TX_PACKET_TRANSMISSION_SPEED_MASK);
194 if (motu->tx_packet_formats.differed_part_pcm_chunks[0] == 0)
195 data |= TX_PACKET_EXCLUDE_DIFFERED_DATA_CHUNKS;
196 if (motu->rx_packet_formats.differed_part_pcm_chunks[0] == 0)
197 data |= RX_PACKET_EXCLUDE_DIFFERED_DATA_CHUNKS;
198 data |= fw_parent_device(motu->unit)->max_speed;
200 reg = cpu_to_be32(data);
201 return snd_motu_transaction_write(motu, PACKET_FORMAT_OFFSET, ®,
205 int snd_motu_stream_start_duplex(struct snd_motu *motu)
207 unsigned int generation = motu->rx_resources.generation;
210 if (motu->substreams_counter == 0)
213 if (amdtp_streaming_error(&motu->rx_stream) ||
214 amdtp_streaming_error(&motu->tx_stream)) {
215 amdtp_domain_stop(&motu->domain);
216 finish_session(motu);
219 if (generation != fw_parent_device(motu->unit)->card->generation) {
220 err = fw_iso_resources_update(&motu->rx_resources);
224 err = fw_iso_resources_update(&motu->tx_resources);
229 if (!amdtp_stream_running(&motu->rx_stream)) {
230 int spd = fw_parent_device(motu->unit)->max_speed;
232 err = ensure_packet_formats(motu);
236 err = begin_session(motu);
238 dev_err(&motu->unit->device,
239 "fail to start isochronous comm: %d\n", err);
243 err = amdtp_domain_add_stream(&motu->domain, &motu->tx_stream,
244 motu->tx_resources.channel, spd);
248 err = amdtp_domain_add_stream(&motu->domain, &motu->rx_stream,
249 motu->rx_resources.channel, spd);
253 err = amdtp_domain_start(&motu->domain);
257 if (!amdtp_stream_wait_callback(&motu->tx_stream,
259 !amdtp_stream_wait_callback(&motu->rx_stream,
265 err = motu->spec->protocol->switch_fetching_mode(motu, true);
267 dev_err(&motu->unit->device,
268 "fail to enable frame fetching: %d\n", err);
276 amdtp_domain_stop(&motu->domain);
277 finish_session(motu);
281 void snd_motu_stream_stop_duplex(struct snd_motu *motu)
283 if (motu->substreams_counter == 0) {
284 amdtp_domain_stop(&motu->domain);
285 finish_session(motu);
287 fw_iso_resources_free(&motu->tx_resources);
288 fw_iso_resources_free(&motu->rx_resources);
292 static int init_stream(struct snd_motu *motu, struct amdtp_stream *s)
294 struct fw_iso_resources *resources;
295 enum amdtp_stream_direction dir;
298 if (s == &motu->tx_stream) {
299 resources = &motu->tx_resources;
300 dir = AMDTP_IN_STREAM;
302 resources = &motu->rx_resources;
303 dir = AMDTP_OUT_STREAM;
306 err = fw_iso_resources_init(resources, motu->unit);
310 err = amdtp_motu_init(s, motu->unit, dir, motu->spec->protocol);
312 fw_iso_resources_destroy(resources);
317 static void destroy_stream(struct snd_motu *motu, struct amdtp_stream *s)
319 amdtp_stream_destroy(s);
321 if (s == &motu->tx_stream)
322 fw_iso_resources_destroy(&motu->tx_resources);
324 fw_iso_resources_destroy(&motu->rx_resources);
327 int snd_motu_stream_init_duplex(struct snd_motu *motu)
331 err = init_stream(motu, &motu->tx_stream);
335 err = init_stream(motu, &motu->rx_stream);
337 destroy_stream(motu, &motu->tx_stream);
341 err = amdtp_domain_init(&motu->domain);
343 destroy_stream(motu, &motu->tx_stream);
344 destroy_stream(motu, &motu->rx_stream);
350 // This function should be called before starting streams or after stopping
352 void snd_motu_stream_destroy_duplex(struct snd_motu *motu)
354 amdtp_domain_destroy(&motu->domain);
356 destroy_stream(motu, &motu->rx_stream);
357 destroy_stream(motu, &motu->tx_stream);
359 motu->substreams_counter = 0;
362 static void motu_lock_changed(struct snd_motu *motu)
364 motu->dev_lock_changed = true;
365 wake_up(&motu->hwdep_wait);
368 int snd_motu_stream_lock_try(struct snd_motu *motu)
372 spin_lock_irq(&motu->lock);
374 if (motu->dev_lock_count < 0) {
379 if (motu->dev_lock_count++ == 0)
380 motu_lock_changed(motu);
383 spin_unlock_irq(&motu->lock);
387 void snd_motu_stream_lock_release(struct snd_motu *motu)
389 spin_lock_irq(&motu->lock);
391 if (WARN_ON(motu->dev_lock_count <= 0))
394 if (--motu->dev_lock_count == 0)
395 motu_lock_changed(motu);
397 spin_unlock_irq(&motu->lock);