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