Linux-libre 5.4.48-gnu
[librecmc/linux-libre.git] / sound / firewire / motu / motu-stream.c
1 // SPDX-License-Identifier: GPL-2.0-only
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>
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
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
62 static int begin_session(struct snd_motu *motu)
63 {
64         __be32 reg;
65         u32 data;
66         int err;
67
68         // Configure the unit to start isochronous communication.
69         err = snd_motu_transaction_read(motu, ISOC_COMM_CONTROL_OFFSET, &reg,
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, &reg,
82                                           sizeof(reg));
83 }
84
85 static void finish_session(struct snd_motu *motu)
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, &reg,
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, &reg,
106                                    sizeof(reg));
107 }
108
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
136 int snd_motu_stream_reserve_duplex(struct snd_motu *motu, unsigned int rate)
137 {
138         unsigned int curr_rate;
139         int err;
140
141         err = motu->spec->protocol->get_clock_rate(motu, &curr_rate);
142         if (err < 0)
143                 return err;
144         if (rate == 0)
145                 rate = curr_rate;
146
147         if (motu->substreams_counter == 0 || curr_rate != rate) {
148                 amdtp_domain_stop(&motu->domain);
149                 finish_session(motu);
150
151                 fw_iso_resources_free(&motu->tx_resources);
152                 fw_iso_resources_free(&motu->rx_resources);
153
154                 err = motu->spec->protocol->set_clock_rate(motu, rate);
155                 if (err < 0) {
156                         dev_err(&motu->unit->device,
157                                 "fail to set sampling rate: %d\n", err);
158                         return err;
159                 }
160
161                 err = snd_motu_stream_cache_packet_formats(motu);
162                 if (err < 0)
163                         return err;
164
165                 err = keep_resources(motu, rate, &motu->tx_stream);
166                 if (err < 0)
167                         return err;
168
169                 err = keep_resources(motu, rate, &motu->rx_stream);
170                 if (err < 0) {
171                         fw_iso_resources_free(&motu->tx_resources);
172                         return err;
173                 }
174         }
175
176         return 0;
177 }
178
179 static int ensure_packet_formats(struct snd_motu *motu)
180 {
181         __be32 reg;
182         u32 data;
183         int err;
184
185         err = snd_motu_transaction_read(motu, PACKET_FORMAT_OFFSET, &reg,
186                                         sizeof(reg));
187         if (err < 0)
188                 return err;
189         data = be32_to_cpu(reg);
190
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;
199
200         reg = cpu_to_be32(data);
201         return snd_motu_transaction_write(motu, PACKET_FORMAT_OFFSET, &reg,
202                                           sizeof(reg));
203 }
204
205 int snd_motu_stream_start_duplex(struct snd_motu *motu)
206 {
207         unsigned int generation = motu->rx_resources.generation;
208         int err = 0;
209
210         if (motu->substreams_counter == 0)
211                 return 0;
212
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);
217         }
218
219         if (generation != fw_parent_device(motu->unit)->card->generation) {
220                 err = fw_iso_resources_update(&motu->rx_resources);
221                 if (err < 0)
222                         return err;
223
224                 err = fw_iso_resources_update(&motu->tx_resources);
225                 if (err < 0)
226                         return err;
227         }
228
229         if (!amdtp_stream_running(&motu->rx_stream)) {
230                 int spd = fw_parent_device(motu->unit)->max_speed;
231
232                 err = ensure_packet_formats(motu);
233                 if (err < 0)
234                         return err;
235
236                 err = begin_session(motu);
237                 if (err < 0) {
238                         dev_err(&motu->unit->device,
239                                 "fail to start isochronous comm: %d\n", err);
240                         goto stop_streams;
241                 }
242
243                 err = amdtp_domain_add_stream(&motu->domain, &motu->tx_stream,
244                                               motu->tx_resources.channel, spd);
245                 if (err < 0)
246                         goto stop_streams;
247
248                 err = amdtp_domain_add_stream(&motu->domain, &motu->rx_stream,
249                                               motu->rx_resources.channel, spd);
250                 if (err < 0)
251                         goto stop_streams;
252
253                 err = amdtp_domain_start(&motu->domain);
254                 if (err < 0)
255                         goto stop_streams;
256
257                 if (!amdtp_stream_wait_callback(&motu->tx_stream,
258                                                 CALLBACK_TIMEOUT) ||
259                     !amdtp_stream_wait_callback(&motu->rx_stream,
260                                                 CALLBACK_TIMEOUT)) {
261                         err = -ETIMEDOUT;
262                         goto stop_streams;
263                 }
264
265                 err = motu->spec->protocol->switch_fetching_mode(motu, true);
266                 if (err < 0) {
267                         dev_err(&motu->unit->device,
268                                 "fail to enable frame fetching: %d\n", err);
269                         goto stop_streams;
270                 }
271         }
272
273         return 0;
274
275 stop_streams:
276         amdtp_domain_stop(&motu->domain);
277         finish_session(motu);
278         return err;
279 }
280
281 void snd_motu_stream_stop_duplex(struct snd_motu *motu)
282 {
283         if (motu->substreams_counter == 0) {
284                 amdtp_domain_stop(&motu->domain);
285                 finish_session(motu);
286
287                 fw_iso_resources_free(&motu->tx_resources);
288                 fw_iso_resources_free(&motu->rx_resources);
289         }
290 }
291
292 static int init_stream(struct snd_motu *motu, struct amdtp_stream *s)
293 {
294         struct fw_iso_resources *resources;
295         enum amdtp_stream_direction dir;
296         int err;
297
298         if (s == &motu->tx_stream) {
299                 resources = &motu->tx_resources;
300                 dir = AMDTP_IN_STREAM;
301         } else {
302                 resources = &motu->rx_resources;
303                 dir = AMDTP_OUT_STREAM;
304         }
305
306         err = fw_iso_resources_init(resources, motu->unit);
307         if (err < 0)
308                 return err;
309
310         err = amdtp_motu_init(s, motu->unit, dir, motu->spec->protocol);
311         if (err < 0)
312                 fw_iso_resources_destroy(resources);
313
314         return err;
315 }
316
317 static void destroy_stream(struct snd_motu *motu, struct amdtp_stream *s)
318 {
319         amdtp_stream_destroy(s);
320
321         if (s == &motu->tx_stream)
322                 fw_iso_resources_destroy(&motu->tx_resources);
323         else
324                 fw_iso_resources_destroy(&motu->rx_resources);
325 }
326
327 int snd_motu_stream_init_duplex(struct snd_motu *motu)
328 {
329         int err;
330
331         err = init_stream(motu, &motu->tx_stream);
332         if (err < 0)
333                 return err;
334
335         err = init_stream(motu, &motu->rx_stream);
336         if (err < 0) {
337                 destroy_stream(motu, &motu->tx_stream);
338                 return err;
339         }
340
341         err = amdtp_domain_init(&motu->domain);
342         if (err < 0) {
343                 destroy_stream(motu, &motu->tx_stream);
344                 destroy_stream(motu, &motu->rx_stream);
345         }
346
347         return err;
348 }
349
350 // This function should be called before starting streams or after stopping
351 // streams.
352 void snd_motu_stream_destroy_duplex(struct snd_motu *motu)
353 {
354         amdtp_domain_destroy(&motu->domain);
355
356         destroy_stream(motu, &motu->rx_stream);
357         destroy_stream(motu, &motu->tx_stream);
358
359         motu->substreams_counter = 0;
360 }
361
362 static void motu_lock_changed(struct snd_motu *motu)
363 {
364         motu->dev_lock_changed = true;
365         wake_up(&motu->hwdep_wait);
366 }
367
368 int snd_motu_stream_lock_try(struct snd_motu *motu)
369 {
370         int err;
371
372         spin_lock_irq(&motu->lock);
373
374         if (motu->dev_lock_count < 0) {
375                 err = -EBUSY;
376                 goto out;
377         }
378
379         if (motu->dev_lock_count++ == 0)
380                 motu_lock_changed(motu);
381         err = 0;
382 out:
383         spin_unlock_irq(&motu->lock);
384         return err;
385 }
386
387 void snd_motu_stream_lock_release(struct snd_motu *motu)
388 {
389         spin_lock_irq(&motu->lock);
390
391         if (WARN_ON(motu->dev_lock_count <= 0))
392                 goto out;
393
394         if (--motu->dev_lock_count == 0)
395                 motu_lock_changed(motu);
396 out:
397         spin_unlock_irq(&motu->lock);
398 }