Linux-libre 5.3.12-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         amdtp_stream_stop(&motu->tx_stream);
96         amdtp_stream_stop(&motu->rx_stream);
97
98         err = snd_motu_transaction_read(motu, ISOC_COMM_CONTROL_OFFSET, &reg,
99                                         sizeof(reg));
100         if (err < 0)
101                 return;
102         data = be32_to_cpu(reg);
103
104         data &= ~(RX_ISOC_COMM_IS_ACTIVATED | TX_ISOC_COMM_IS_ACTIVATED);
105         data |= CHANGE_RX_ISOC_COMM_STATE | CHANGE_TX_ISOC_COMM_STATE;
106
107         reg = cpu_to_be32(data);
108         snd_motu_transaction_write(motu, ISOC_COMM_CONTROL_OFFSET, &reg,
109                                    sizeof(reg));
110 }
111
112 static int start_isoc_ctx(struct snd_motu *motu, struct amdtp_stream *stream)
113 {
114         struct fw_iso_resources *resources;
115         int err;
116
117         if (stream == &motu->rx_stream)
118                 resources = &motu->rx_resources;
119         else
120                 resources = &motu->tx_resources;
121
122         err = amdtp_stream_start(stream, resources->channel,
123                                  fw_parent_device(motu->unit)->max_speed);
124         if (err < 0)
125                 return err;
126
127         if (!amdtp_stream_wait_callback(stream, CALLBACK_TIMEOUT))
128                 return -ETIMEDOUT;
129
130         return 0;
131 }
132
133 int snd_motu_stream_cache_packet_formats(struct snd_motu *motu)
134 {
135         int err;
136
137         err = motu->spec->protocol->cache_packet_formats(motu);
138         if (err < 0)
139                 return err;
140
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;
147         }
148
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;
155         }
156
157         return 0;
158 }
159
160 int snd_motu_stream_reserve_duplex(struct snd_motu *motu, unsigned int rate)
161 {
162         unsigned int curr_rate;
163         int err;
164
165         err = motu->spec->protocol->get_clock_rate(motu, &curr_rate);
166         if (err < 0)
167                 return err;
168         if (rate == 0)
169                 rate = curr_rate;
170
171         if (motu->substreams_counter == 0 || curr_rate != rate) {
172                 finish_session(motu);
173
174                 fw_iso_resources_free(&motu->tx_resources);
175                 fw_iso_resources_free(&motu->rx_resources);
176
177                 err = motu->spec->protocol->set_clock_rate(motu, rate);
178                 if (err < 0) {
179                         dev_err(&motu->unit->device,
180                                 "fail to set sampling rate: %d\n", err);
181                         return err;
182                 }
183
184                 err = snd_motu_stream_cache_packet_formats(motu);
185                 if (err < 0)
186                         return err;
187
188                 err = keep_resources(motu, rate, &motu->tx_stream);
189                 if (err < 0)
190                         return err;
191
192                 err = keep_resources(motu, rate, &motu->rx_stream);
193                 if (err < 0) {
194                         fw_iso_resources_free(&motu->tx_resources);
195                         return err;
196                 }
197         }
198
199         return 0;
200 }
201
202 static int ensure_packet_formats(struct snd_motu *motu)
203 {
204         __be32 reg;
205         u32 data;
206         int err;
207
208         err = snd_motu_transaction_read(motu, PACKET_FORMAT_OFFSET, &reg,
209                                         sizeof(reg));
210         if (err < 0)
211                 return err;
212         data = be32_to_cpu(reg);
213
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;
222
223         reg = cpu_to_be32(data);
224         return snd_motu_transaction_write(motu, PACKET_FORMAT_OFFSET, &reg,
225                                           sizeof(reg));
226 }
227
228 int snd_motu_stream_start_duplex(struct snd_motu *motu)
229 {
230         unsigned int generation = motu->rx_resources.generation;
231         int err = 0;
232
233         if (motu->substreams_counter == 0)
234                 return 0;
235
236         if (amdtp_streaming_error(&motu->rx_stream) ||
237             amdtp_streaming_error(&motu->tx_stream))
238                 finish_session(motu);
239
240         if (generation != fw_parent_device(motu->unit)->card->generation) {
241                 err = fw_iso_resources_update(&motu->rx_resources);
242                 if (err < 0)
243                         return err;
244
245                 err = fw_iso_resources_update(&motu->tx_resources);
246                 if (err < 0)
247                         return err;
248         }
249
250         if (!amdtp_stream_running(&motu->rx_stream)) {
251                 err = ensure_packet_formats(motu);
252                 if (err < 0)
253                         return err;
254
255                 err = begin_session(motu);
256                 if (err < 0) {
257                         dev_err(&motu->unit->device,
258                                 "fail to start isochronous comm: %d\n", err);
259                         goto stop_streams;
260                 }
261
262                 err = start_isoc_ctx(motu, &motu->rx_stream);
263                 if (err < 0) {
264                         dev_err(&motu->unit->device,
265                                 "fail to start IT context: %d\n", err);
266                         goto stop_streams;
267                 }
268
269                 err = motu->spec->protocol->switch_fetching_mode(motu, true);
270                 if (err < 0) {
271                         dev_err(&motu->unit->device,
272                                 "fail to enable frame fetching: %d\n", err);
273                         goto stop_streams;
274                 }
275         }
276
277         if (!amdtp_stream_running(&motu->tx_stream)) {
278                 err = start_isoc_ctx(motu, &motu->tx_stream);
279                 if (err < 0) {
280                         dev_err(&motu->unit->device,
281                                 "fail to start IR context: %d", err);
282                         goto stop_streams;
283                 }
284         }
285
286         return 0;
287
288 stop_streams:
289         finish_session(motu);
290         return err;
291 }
292
293 void snd_motu_stream_stop_duplex(struct snd_motu *motu)
294 {
295         if (motu->substreams_counter == 0) {
296                 finish_session(motu);
297
298                 fw_iso_resources_free(&motu->tx_resources);
299                 fw_iso_resources_free(&motu->rx_resources);
300         }
301 }
302
303 static int init_stream(struct snd_motu *motu, enum amdtp_stream_direction dir)
304 {
305         int err;
306         struct amdtp_stream *stream;
307         struct fw_iso_resources *resources;
308
309         if (dir == AMDTP_IN_STREAM) {
310                 stream = &motu->tx_stream;
311                 resources = &motu->tx_resources;
312         } else {
313                 stream = &motu->rx_stream;
314                 resources = &motu->rx_resources;
315         }
316
317         err = fw_iso_resources_init(resources, motu->unit);
318         if (err < 0)
319                 return err;
320
321         err = amdtp_motu_init(stream, motu->unit, dir, motu->spec->protocol);
322         if (err < 0) {
323                 amdtp_stream_destroy(stream);
324                 fw_iso_resources_destroy(resources);
325         }
326
327         return err;
328 }
329
330 static void destroy_stream(struct snd_motu *motu,
331                            enum amdtp_stream_direction dir)
332 {
333         struct amdtp_stream *stream;
334         struct fw_iso_resources *resources;
335
336         if (dir == AMDTP_IN_STREAM) {
337                 stream = &motu->tx_stream;
338                 resources = &motu->tx_resources;
339         } else {
340                 stream = &motu->rx_stream;
341                 resources = &motu->rx_resources;
342         }
343
344         amdtp_stream_destroy(stream);
345         fw_iso_resources_destroy(resources);
346 }
347
348 int snd_motu_stream_init_duplex(struct snd_motu *motu)
349 {
350         int err;
351
352         err = init_stream(motu, AMDTP_IN_STREAM);
353         if (err < 0)
354                 return err;
355
356         err = init_stream(motu, AMDTP_OUT_STREAM);
357         if (err < 0)
358                 destroy_stream(motu, AMDTP_IN_STREAM);
359
360         return err;
361 }
362
363 /*
364  * This function should be called before starting streams or after stopping
365  * streams.
366  */
367 void snd_motu_stream_destroy_duplex(struct snd_motu *motu)
368 {
369         destroy_stream(motu, AMDTP_IN_STREAM);
370         destroy_stream(motu, AMDTP_OUT_STREAM);
371
372         motu->substreams_counter = 0;
373 }
374
375 static void motu_lock_changed(struct snd_motu *motu)
376 {
377         motu->dev_lock_changed = true;
378         wake_up(&motu->hwdep_wait);
379 }
380
381 int snd_motu_stream_lock_try(struct snd_motu *motu)
382 {
383         int err;
384
385         spin_lock_irq(&motu->lock);
386
387         if (motu->dev_lock_count < 0) {
388                 err = -EBUSY;
389                 goto out;
390         }
391
392         if (motu->dev_lock_count++ == 0)
393                 motu_lock_changed(motu);
394         err = 0;
395 out:
396         spin_unlock_irq(&motu->lock);
397         return err;
398 }
399
400 void snd_motu_stream_lock_release(struct snd_motu *motu)
401 {
402         spin_lock_irq(&motu->lock);
403
404         if (WARN_ON(motu->dev_lock_count <= 0))
405                 goto out;
406
407         if (--motu->dev_lock_count == 0)
408                 motu_lock_changed(motu);
409 out:
410         spin_unlock_irq(&motu->lock);
411 }