blob: edb31ac2686826051be5263295b5813969093efa [file] [log] [blame]
Thomas Gleixnerda607e12019-05-29 16:57:59 -07001// SPDX-License-Identifier: GPL-2.0-only
Takashi Sakamoto4641c932017-03-22 21:30:18 +09002/*
3 * amdtp-motu.c - a part of driver for MOTU FireWire series
4 *
5 * Copyright (c) 2015-2017 Takashi Sakamoto <o-takashi@sakamocchi.jp>
Takashi Sakamoto4641c932017-03-22 21:30:18 +09006 */
7
8#include <linux/slab.h>
9#include <sound/pcm.h>
10#include "motu.h"
11
Takashi Sakamoto17909c12017-04-09 21:33:28 +090012#define CREATE_TRACE_POINTS
13#include "amdtp-motu-trace.h"
14
Takashi Sakamoto4641c932017-03-22 21:30:18 +090015#define CIP_FMT_MOTU 0x02
Takashi Sakamoto5992e302017-03-22 21:30:28 +090016#define CIP_FMT_MOTU_TX_V3 0x22
Takashi Sakamoto4641c932017-03-22 21:30:18 +090017#define MOTU_FDF_AM824 0x22
18
Takashi Sakamoto9e796e72017-03-22 21:30:23 +090019/*
20 * Nominally 3125 bytes/second, but the MIDI port's clock might be
21 * 1% too slow, and the bus clock 100 ppm too fast.
22 */
23#define MIDI_BYTES_PER_SECOND 3093
24
Takashi Sakamoto4641c932017-03-22 21:30:18 +090025struct amdtp_motu {
26 /* For timestamp processing. */
27 unsigned int quotient_ticks_per_event;
28 unsigned int remainder_ticks_per_event;
29 unsigned int next_ticks;
30 unsigned int next_accumulated;
31 unsigned int next_cycles;
32 unsigned int next_seconds;
33
34 unsigned int pcm_chunks;
35 unsigned int pcm_byte_offset;
Takashi Sakamoto9e796e72017-03-22 21:30:23 +090036
37 struct snd_rawmidi_substream *midi;
38 unsigned int midi_ports;
39 unsigned int midi_flag_offset;
40 unsigned int midi_byte_offset;
41
42 int midi_db_count;
43 unsigned int midi_db_interval;
Takashi Sakamoto4641c932017-03-22 21:30:18 +090044};
45
46int amdtp_motu_set_parameters(struct amdtp_stream *s, unsigned int rate,
Takashi Sakamoto9e796e72017-03-22 21:30:23 +090047 unsigned int midi_ports,
Takashi Sakamoto4641c932017-03-22 21:30:18 +090048 struct snd_motu_packet_format *formats)
49{
50 static const struct {
51 unsigned int quotient_ticks_per_event;
52 unsigned int remainder_ticks_per_event;
53 } params[] = {
54 [CIP_SFC_44100] = { 557, 123 },
55 [CIP_SFC_48000] = { 512, 0 },
56 [CIP_SFC_88200] = { 278, 282 },
57 [CIP_SFC_96000] = { 256, 0 },
58 [CIP_SFC_176400] = { 139, 141 },
59 [CIP_SFC_192000] = { 128, 0 },
60 };
61 struct amdtp_motu *p = s->protocol;
62 unsigned int pcm_chunks, data_chunks, data_block_quadlets;
63 unsigned int delay;
64 unsigned int mode;
65 int i, err;
66
67 if (amdtp_stream_running(s))
68 return -EBUSY;
69
70 for (i = 0; i < ARRAY_SIZE(snd_motu_clock_rates); ++i) {
71 if (snd_motu_clock_rates[i] == rate) {
72 mode = i >> 1;
73 break;
74 }
75 }
76 if (i == ARRAY_SIZE(snd_motu_clock_rates))
77 return -EINVAL;
78
Takashi Sakamoto88e8f892020-05-19 20:16:37 +090079 // Each data block includes SPH in its head. Data chunks follow with
80 // 3 byte alignment. Padding follows with zero to conform to quadlet
81 // alignment.
82 pcm_chunks = formats->pcm_chunks[mode];
Takashi Sakamoto4641c932017-03-22 21:30:18 +090083 data_chunks = formats->msg_chunks + pcm_chunks;
Takashi Sakamoto4641c932017-03-22 21:30:18 +090084 data_block_quadlets = 1 + DIV_ROUND_UP(data_chunks * 3, 4);
85
86 err = amdtp_stream_set_parameters(s, rate, data_block_quadlets);
87 if (err < 0)
88 return err;
89
90 p->pcm_chunks = pcm_chunks;
91 p->pcm_byte_offset = formats->pcm_byte_offset;
92
Takashi Sakamoto9e796e72017-03-22 21:30:23 +090093 p->midi_ports = midi_ports;
94 p->midi_flag_offset = formats->midi_flag_offset;
95 p->midi_byte_offset = formats->midi_byte_offset;
96
97 p->midi_db_count = 0;
98 p->midi_db_interval = rate / MIDI_BYTES_PER_SECOND;
99
Takashi Sakamoto4641c932017-03-22 21:30:18 +0900100 /* IEEE 1394 bus requires. */
101 delay = 0x2e00;
102
103 /* For no-data or empty packets to adjust PCM sampling frequency. */
104 delay += 8000 * 3072 * s->syt_interval / rate;
105
106 p->next_seconds = 0;
107 p->next_cycles = delay / 3072;
108 p->quotient_ticks_per_event = params[s->sfc].quotient_ticks_per_event;
109 p->remainder_ticks_per_event = params[s->sfc].remainder_ticks_per_event;
110 p->next_ticks = delay % 3072;
111 p->next_accumulated = 0;
112
113 return 0;
114}
115
Takashi Sakamoto00d004d2019-07-22 12:37:07 +0900116static void read_pcm_s32(struct amdtp_stream *s, struct snd_pcm_substream *pcm,
117 __be32 *buffer, unsigned int data_blocks,
118 unsigned int pcm_frames)
Takashi Sakamoto4641c932017-03-22 21:30:18 +0900119{
120 struct amdtp_motu *p = s->protocol;
Takashi Sakamoto00d004d2019-07-22 12:37:07 +0900121 unsigned int channels = p->pcm_chunks;
122 struct snd_pcm_runtime *runtime = pcm->runtime;
123 unsigned int pcm_buffer_pointer;
124 int remaining_frames;
Takashi Sakamoto4641c932017-03-22 21:30:18 +0900125 u8 *byte;
126 u32 *dst;
Takashi Sakamoto00d004d2019-07-22 12:37:07 +0900127 int i, c;
Takashi Sakamoto4641c932017-03-22 21:30:18 +0900128
Takashi Sakamoto00d004d2019-07-22 12:37:07 +0900129 pcm_buffer_pointer = s->pcm_buffer_pointer + pcm_frames;
130 pcm_buffer_pointer %= runtime->buffer_size;
131
Takashi Sakamoto4641c932017-03-22 21:30:18 +0900132 dst = (void *)runtime->dma_area +
Takashi Sakamoto00d004d2019-07-22 12:37:07 +0900133 frames_to_bytes(runtime, pcm_buffer_pointer);
134 remaining_frames = runtime->buffer_size - pcm_buffer_pointer;
Takashi Sakamoto4641c932017-03-22 21:30:18 +0900135
136 for (i = 0; i < data_blocks; ++i) {
137 byte = (u8 *)buffer + p->pcm_byte_offset;
138
139 for (c = 0; c < channels; ++c) {
Takashi Sakamotof97a0942019-02-26 13:38:37 +0900140 *dst = (byte[0] << 24) |
141 (byte[1] << 16) |
142 (byte[2] << 8);
Takashi Sakamoto4641c932017-03-22 21:30:18 +0900143 byte += 3;
144 dst++;
145 }
146 buffer += s->data_block_quadlets;
147 if (--remaining_frames == 0)
148 dst = (void *)runtime->dma_area;
149 }
150}
151
Takashi Sakamoto00d004d2019-07-22 12:37:07 +0900152static void write_pcm_s32(struct amdtp_stream *s, struct snd_pcm_substream *pcm,
153 __be32 *buffer, unsigned int data_blocks,
154 unsigned int pcm_frames)
Takashi Sakamoto4641c932017-03-22 21:30:18 +0900155{
156 struct amdtp_motu *p = s->protocol;
Takashi Sakamoto00d004d2019-07-22 12:37:07 +0900157 unsigned int channels = p->pcm_chunks;
158 struct snd_pcm_runtime *runtime = pcm->runtime;
159 unsigned int pcm_buffer_pointer;
160 int remaining_frames;
Takashi Sakamoto4641c932017-03-22 21:30:18 +0900161 u8 *byte;
162 const u32 *src;
Takashi Sakamoto00d004d2019-07-22 12:37:07 +0900163 int i, c;
Takashi Sakamoto4641c932017-03-22 21:30:18 +0900164
Takashi Sakamoto00d004d2019-07-22 12:37:07 +0900165 pcm_buffer_pointer = s->pcm_buffer_pointer + pcm_frames;
166 pcm_buffer_pointer %= runtime->buffer_size;
167
Takashi Sakamoto4641c932017-03-22 21:30:18 +0900168 src = (void *)runtime->dma_area +
Takashi Sakamoto00d004d2019-07-22 12:37:07 +0900169 frames_to_bytes(runtime, pcm_buffer_pointer);
170 remaining_frames = runtime->buffer_size - pcm_buffer_pointer;
Takashi Sakamoto4641c932017-03-22 21:30:18 +0900171
172 for (i = 0; i < data_blocks; ++i) {
173 byte = (u8 *)buffer + p->pcm_byte_offset;
174
175 for (c = 0; c < channels; ++c) {
176 byte[0] = (*src >> 24) & 0xff;
177 byte[1] = (*src >> 16) & 0xff;
178 byte[2] = (*src >> 8) & 0xff;
179 byte += 3;
180 src++;
181 }
182
183 buffer += s->data_block_quadlets;
184 if (--remaining_frames == 0)
185 src = (void *)runtime->dma_area;
186 }
187}
188
189static void write_pcm_silence(struct amdtp_stream *s, __be32 *buffer,
190 unsigned int data_blocks)
191{
192 struct amdtp_motu *p = s->protocol;
193 unsigned int channels, i, c;
194 u8 *byte;
195
196 channels = p->pcm_chunks;
197
198 for (i = 0; i < data_blocks; ++i) {
199 byte = (u8 *)buffer + p->pcm_byte_offset;
200
201 for (c = 0; c < channels; ++c) {
202 byte[0] = 0;
203 byte[1] = 0;
204 byte[2] = 0;
205 byte += 3;
206 }
207
208 buffer += s->data_block_quadlets;
209 }
210}
211
212int amdtp_motu_add_pcm_hw_constraints(struct amdtp_stream *s,
213 struct snd_pcm_runtime *runtime)
214{
215 int err;
216
217 /* TODO: how to set an constraint for exactly 24bit PCM sample? */
218 err = snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
219 if (err < 0)
220 return err;
221
222 return amdtp_stream_add_pcm_hw_constraints(s, runtime);
223}
224
Takashi Sakamoto9e796e72017-03-22 21:30:23 +0900225void amdtp_motu_midi_trigger(struct amdtp_stream *s, unsigned int port,
226 struct snd_rawmidi_substream *midi)
227{
228 struct amdtp_motu *p = s->protocol;
229
230 if (port < p->midi_ports)
231 WRITE_ONCE(p->midi, midi);
232}
233
234static void write_midi_messages(struct amdtp_stream *s, __be32 *buffer,
235 unsigned int data_blocks)
236{
237 struct amdtp_motu *p = s->protocol;
238 struct snd_rawmidi_substream *midi = READ_ONCE(p->midi);
239 u8 *b;
240 int i;
241
242 for (i = 0; i < data_blocks; i++) {
243 b = (u8 *)buffer;
244
245 if (midi && p->midi_db_count == 0 &&
246 snd_rawmidi_transmit(midi, b + p->midi_byte_offset, 1) == 1) {
247 b[p->midi_flag_offset] = 0x01;
248 } else {
249 b[p->midi_byte_offset] = 0x00;
250 b[p->midi_flag_offset] = 0x00;
251 }
252
253 buffer += s->data_block_quadlets;
254
255 if (--p->midi_db_count < 0)
256 p->midi_db_count = p->midi_db_interval;
257 }
258}
259
260static void read_midi_messages(struct amdtp_stream *s, __be32 *buffer,
261 unsigned int data_blocks)
262{
263 struct amdtp_motu *p = s->protocol;
264 struct snd_rawmidi_substream *midi;
265 u8 *b;
266 int i;
267
268 for (i = 0; i < data_blocks; i++) {
269 b = (u8 *)buffer;
270 midi = READ_ONCE(p->midi);
271
272 if (midi && (b[p->midi_flag_offset] & 0x01))
273 snd_rawmidi_receive(midi, b + p->midi_byte_offset, 1);
274
275 buffer += s->data_block_quadlets;
276 }
277}
278
Takashi Sakamoto17909c12017-04-09 21:33:28 +0900279/* For tracepoints. */
Arnd Bergmannfa8323b2017-04-19 19:51:29 +0200280static void __maybe_unused copy_sph(u32 *frames, __be32 *buffer,
281 unsigned int data_blocks,
282 unsigned int data_block_quadlets)
Takashi Sakamoto17909c12017-04-09 21:33:28 +0900283{
284 unsigned int i;
285
286 for (i = 0; i < data_blocks; ++i) {
287 *frames = be32_to_cpu(*buffer);
288 buffer += data_block_quadlets;
289 frames++;
290 }
291}
292
Takashi Sakamotoc6b0b9e2017-04-09 21:33:29 +0900293/* For tracepoints. */
Arnd Bergmannfa8323b2017-04-19 19:51:29 +0200294static void __maybe_unused copy_message(u64 *frames, __be32 *buffer,
295 unsigned int data_blocks,
296 unsigned int data_block_quadlets)
Takashi Sakamotoc6b0b9e2017-04-09 21:33:29 +0900297{
298 unsigned int i;
299
300 /* This is just for v2/v3 protocol. */
301 for (i = 0; i < data_blocks; ++i) {
302 *frames = (be32_to_cpu(buffer[1]) << 16) |
303 (be32_to_cpu(buffer[2]) >> 16);
304 buffer += data_block_quadlets;
305 frames++;
306 }
307}
308
Takashi Sakamotod2d5a6b2019-07-22 12:37:10 +0900309static void probe_tracepoints_events(struct amdtp_stream *s,
310 const struct pkt_desc *descs,
311 unsigned int packets)
Takashi Sakamoto4641c932017-03-22 21:30:18 +0900312{
Takashi Sakamoto9a738ad2019-07-22 12:37:09 +0900313 int i;
Takashi Sakamoto4641c932017-03-22 21:30:18 +0900314
Takashi Sakamoto9a738ad2019-07-22 12:37:09 +0900315 for (i = 0; i < packets; ++i) {
316 const struct pkt_desc *desc = descs + i;
317 __be32 *buf = desc->ctx_payload;
318 unsigned int data_blocks = desc->data_blocks;
Takashi Sakamoto17909c12017-04-09 21:33:28 +0900319
Takashi Sakamoto9a738ad2019-07-22 12:37:09 +0900320 trace_data_block_sph(s, data_blocks, buf);
321 trace_data_block_message(s, data_blocks, buf);
Takashi Sakamotod2d5a6b2019-07-22 12:37:10 +0900322 }
323}
Takashi Sakamoto9e796e72017-03-22 21:30:23 +0900324
Takashi Sakamotod2d5a6b2019-07-22 12:37:10 +0900325static unsigned int process_ir_ctx_payloads(struct amdtp_stream *s,
326 const struct pkt_desc *descs,
327 unsigned int packets,
328 struct snd_pcm_substream *pcm)
329{
330 struct amdtp_motu *p = s->protocol;
331 unsigned int pcm_frames = 0;
332 int i;
333
334 // For data block processing.
335 for (i = 0; i < packets; ++i) {
336 const struct pkt_desc *desc = descs + i;
337 __be32 *buf = desc->ctx_payload;
338 unsigned int data_blocks = desc->data_blocks;
Takashi Sakamoto9a738ad2019-07-22 12:37:09 +0900339
340 if (pcm) {
341 read_pcm_s32(s, pcm, buf, data_blocks, pcm_frames);
342 pcm_frames += data_blocks;
343 }
Takashi Sakamotod2d5a6b2019-07-22 12:37:10 +0900344
345 if (p->midi_ports)
346 read_midi_messages(s, buf, data_blocks);
Takashi Sakamotod2c104a2019-07-22 12:37:03 +0900347 }
Takashi Sakamoto4641c932017-03-22 21:30:18 +0900348
Takashi Sakamotod2d5a6b2019-07-22 12:37:10 +0900349 // For tracepoints.
350 if (trace_data_block_sph_enabled() ||
351 trace_data_block_message_enabled())
352 probe_tracepoints_events(s, descs, packets);
353
Takashi Sakamotod2c104a2019-07-22 12:37:03 +0900354 return pcm_frames;
Takashi Sakamoto4641c932017-03-22 21:30:18 +0900355}
356
357static inline void compute_next_elapse_from_start(struct amdtp_motu *p)
358{
359 p->next_accumulated += p->remainder_ticks_per_event;
360 if (p->next_accumulated >= 441) {
361 p->next_accumulated -= 441;
362 p->next_ticks++;
363 }
364
365 p->next_ticks += p->quotient_ticks_per_event;
366 if (p->next_ticks >= 3072) {
367 p->next_ticks -= 3072;
368 p->next_cycles++;
369 }
370
371 if (p->next_cycles >= 8000) {
372 p->next_cycles -= 8000;
373 p->next_seconds++;
374 }
375
376 if (p->next_seconds >= 128)
377 p->next_seconds -= 128;
378}
379
380static void write_sph(struct amdtp_stream *s, __be32 *buffer,
381 unsigned int data_blocks)
382{
383 struct amdtp_motu *p = s->protocol;
384 unsigned int next_cycles;
385 unsigned int i;
386 u32 sph;
387
388 for (i = 0; i < data_blocks; i++) {
389 next_cycles = (s->start_cycle + p->next_cycles) % 8000;
390 sph = ((next_cycles << 12) | p->next_ticks) & 0x01ffffff;
391 *buffer = cpu_to_be32(sph);
392
393 compute_next_elapse_from_start(p);
394
395 buffer += s->data_block_quadlets;
396 }
397}
398
Takashi Sakamoto9a738ad2019-07-22 12:37:09 +0900399static unsigned int process_it_ctx_payloads(struct amdtp_stream *s,
400 const struct pkt_desc *descs,
401 unsigned int packets,
402 struct snd_pcm_substream *pcm)
Takashi Sakamoto4641c932017-03-22 21:30:18 +0900403{
Takashi Sakamoto00d004d2019-07-22 12:37:07 +0900404 struct amdtp_motu *p = s->protocol;
405 unsigned int pcm_frames = 0;
Takashi Sakamoto9a738ad2019-07-22 12:37:09 +0900406 int i;
Takashi Sakamoto4641c932017-03-22 21:30:18 +0900407
Takashi Sakamotod2d5a6b2019-07-22 12:37:10 +0900408 // For data block processing.
Takashi Sakamoto9a738ad2019-07-22 12:37:09 +0900409 for (i = 0; i < packets; ++i) {
410 const struct pkt_desc *desc = descs + i;
411 __be32 *buf = desc->ctx_payload;
412 unsigned int data_blocks = desc->data_blocks;
Takashi Sakamoto4641c932017-03-22 21:30:18 +0900413
Takashi Sakamoto9a738ad2019-07-22 12:37:09 +0900414 if (pcm) {
415 write_pcm_s32(s, pcm, buf, data_blocks, pcm_frames);
416 pcm_frames += data_blocks;
417 } else {
418 write_pcm_silence(s, buf, data_blocks);
419 }
420
Takashi Sakamotod2d5a6b2019-07-22 12:37:10 +0900421 if (p->midi_ports)
422 write_midi_messages(s, buf, data_blocks);
Takashi Sakamoto9a738ad2019-07-22 12:37:09 +0900423
Takashi Sakamotod2d5a6b2019-07-22 12:37:10 +0900424 // TODO: how to interact control messages between userspace?
425
426 write_sph(s, buf, data_blocks);
Takashi Sakamotod2c104a2019-07-22 12:37:03 +0900427 }
Takashi Sakamoto4641c932017-03-22 21:30:18 +0900428
Takashi Sakamotod2d5a6b2019-07-22 12:37:10 +0900429 // For tracepoints.
430 if (trace_data_block_sph_enabled() ||
431 trace_data_block_message_enabled())
432 probe_tracepoints_events(s, descs, packets);
433
Takashi Sakamotod2c104a2019-07-22 12:37:03 +0900434 return pcm_frames;
Takashi Sakamoto4641c932017-03-22 21:30:18 +0900435}
436
437int amdtp_motu_init(struct amdtp_stream *s, struct fw_unit *unit,
438 enum amdtp_stream_direction dir,
Takashi Sakamoto61d79c72020-05-19 20:16:30 +0900439 const struct snd_motu_spec *spec)
Takashi Sakamoto4641c932017-03-22 21:30:18 +0900440{
Takashi Sakamoto9a738ad2019-07-22 12:37:09 +0900441 amdtp_stream_process_ctx_payloads_t process_ctx_payloads;
Takashi Sakamoto4641c932017-03-22 21:30:18 +0900442 int fmt = CIP_FMT_MOTU;
443 int flags = CIP_BLOCKING;
444 int err;
445
446 if (dir == AMDTP_IN_STREAM) {
Takashi Sakamoto9a738ad2019-07-22 12:37:09 +0900447 process_ctx_payloads = process_ir_ctx_payloads;
Takashi Sakamoto5992e302017-03-22 21:30:28 +0900448
449 /*
450 * Units of version 3 transmits packets with invalid CIP header
451 * against IEC 61883-1.
452 */
Takashi Sakamoto61d79c72020-05-19 20:16:30 +0900453 if (spec->protocol_version == SND_MOTU_PROTOCOL_V3) {
Takashi Sakamoto5992e302017-03-22 21:30:28 +0900454 flags |= CIP_WRONG_DBS |
455 CIP_SKIP_DBC_ZERO_CHECK |
456 CIP_HEADER_WITHOUT_EOH;
457 fmt = CIP_FMT_MOTU_TX_V3;
458 }
Takashi Sakamoto35033d82019-03-17 16:50:24 +0900459
Takashi Sakamoto61d79c72020-05-19 20:16:30 +0900460 if (spec == &snd_motu_spec_8pre ||
461 spec == &snd_motu_spec_ultralite) {
Takashi Sakamoto35033d82019-03-17 16:50:24 +0900462 // 8pre has some quirks.
463 flags |= CIP_WRONG_DBS |
464 CIP_SKIP_DBC_ZERO_CHECK;
465 }
Takashi Sakamoto4641c932017-03-22 21:30:18 +0900466 } else {
Takashi Sakamoto9a738ad2019-07-22 12:37:09 +0900467 process_ctx_payloads = process_it_ctx_payloads;
Takashi Sakamoto4641c932017-03-22 21:30:18 +0900468 flags |= CIP_DBC_IS_END_EVENT;
469 }
470
Takashi Sakamoto9a738ad2019-07-22 12:37:09 +0900471 err = amdtp_stream_init(s, unit, dir, flags, fmt, process_ctx_payloads,
Takashi Sakamoto4641c932017-03-22 21:30:18 +0900472 sizeof(struct amdtp_motu));
473 if (err < 0)
474 return err;
475
476 s->sph = 1;
Takashi Sakamoto3baf3052019-07-22 12:36:56 +0900477
478 if (dir == AMDTP_OUT_STREAM) {
479 // Use fixed value for FDF field.
480 s->ctx_data.rx.fdf = MOTU_FDF_AM824;
481 // Not used.
482 s->ctx_data.rx.syt_override = 0xffff;
483 }
Takashi Sakamoto4641c932017-03-22 21:30:18 +0900484
485 return 0;
486}