interconnect: Add a common standard aggregate function
[linux/fpc-iii.git] / sound / firewire / motu / amdtp-motu.c
blob0fd36e469ad076c4a170c7adb30ad3e4869fec86
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * amdtp-motu.c - a part of driver for MOTU FireWire series
5 * Copyright (c) 2015-2017 Takashi Sakamoto <o-takashi@sakamocchi.jp>
6 */
8 #include <linux/slab.h>
9 #include <sound/pcm.h>
10 #include "motu.h"
12 #define CREATE_TRACE_POINTS
13 #include "amdtp-motu-trace.h"
15 #define CIP_FMT_MOTU 0x02
16 #define CIP_FMT_MOTU_TX_V3 0x22
17 #define MOTU_FDF_AM824 0x22
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.
23 #define MIDI_BYTES_PER_SECOND 3093
25 struct 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;
34 unsigned int pcm_chunks;
35 unsigned int pcm_byte_offset;
37 struct snd_rawmidi_substream *midi;
38 unsigned int midi_ports;
39 unsigned int midi_flag_offset;
40 unsigned int midi_byte_offset;
42 int midi_db_count;
43 unsigned int midi_db_interval;
46 int amdtp_motu_set_parameters(struct amdtp_stream *s, unsigned int rate,
47 unsigned int midi_ports,
48 struct snd_motu_packet_format *formats)
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 },
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;
67 if (amdtp_stream_running(s))
68 return -EBUSY;
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;
76 if (i == ARRAY_SIZE(snd_motu_clock_rates))
77 return -EINVAL;
79 pcm_chunks = formats->fixed_part_pcm_chunks[mode] +
80 formats->differed_part_pcm_chunks[mode];
81 data_chunks = formats->msg_chunks + pcm_chunks;
84 * Each data block includes SPH in its head. Data chunks follow with
85 * 3 byte alignment. Padding follows with zero to conform to quadlet
86 * alignment.
88 data_block_quadlets = 1 + DIV_ROUND_UP(data_chunks * 3, 4);
90 err = amdtp_stream_set_parameters(s, rate, data_block_quadlets);
91 if (err < 0)
92 return err;
94 p->pcm_chunks = pcm_chunks;
95 p->pcm_byte_offset = formats->pcm_byte_offset;
97 p->midi_ports = midi_ports;
98 p->midi_flag_offset = formats->midi_flag_offset;
99 p->midi_byte_offset = formats->midi_byte_offset;
101 p->midi_db_count = 0;
102 p->midi_db_interval = rate / MIDI_BYTES_PER_SECOND;
104 /* IEEE 1394 bus requires. */
105 delay = 0x2e00;
107 /* For no-data or empty packets to adjust PCM sampling frequency. */
108 delay += 8000 * 3072 * s->syt_interval / rate;
110 p->next_seconds = 0;
111 p->next_cycles = delay / 3072;
112 p->quotient_ticks_per_event = params[s->sfc].quotient_ticks_per_event;
113 p->remainder_ticks_per_event = params[s->sfc].remainder_ticks_per_event;
114 p->next_ticks = delay % 3072;
115 p->next_accumulated = 0;
117 return 0;
120 static void read_pcm_s32(struct amdtp_stream *s, struct snd_pcm_substream *pcm,
121 __be32 *buffer, unsigned int data_blocks,
122 unsigned int pcm_frames)
124 struct amdtp_motu *p = s->protocol;
125 unsigned int channels = p->pcm_chunks;
126 struct snd_pcm_runtime *runtime = pcm->runtime;
127 unsigned int pcm_buffer_pointer;
128 int remaining_frames;
129 u8 *byte;
130 u32 *dst;
131 int i, c;
133 pcm_buffer_pointer = s->pcm_buffer_pointer + pcm_frames;
134 pcm_buffer_pointer %= runtime->buffer_size;
136 dst = (void *)runtime->dma_area +
137 frames_to_bytes(runtime, pcm_buffer_pointer);
138 remaining_frames = runtime->buffer_size - pcm_buffer_pointer;
140 for (i = 0; i < data_blocks; ++i) {
141 byte = (u8 *)buffer + p->pcm_byte_offset;
143 for (c = 0; c < channels; ++c) {
144 *dst = (byte[0] << 24) |
145 (byte[1] << 16) |
146 (byte[2] << 8);
147 byte += 3;
148 dst++;
150 buffer += s->data_block_quadlets;
151 if (--remaining_frames == 0)
152 dst = (void *)runtime->dma_area;
156 static void write_pcm_s32(struct amdtp_stream *s, struct snd_pcm_substream *pcm,
157 __be32 *buffer, unsigned int data_blocks,
158 unsigned int pcm_frames)
160 struct amdtp_motu *p = s->protocol;
161 unsigned int channels = p->pcm_chunks;
162 struct snd_pcm_runtime *runtime = pcm->runtime;
163 unsigned int pcm_buffer_pointer;
164 int remaining_frames;
165 u8 *byte;
166 const u32 *src;
167 int i, c;
169 pcm_buffer_pointer = s->pcm_buffer_pointer + pcm_frames;
170 pcm_buffer_pointer %= runtime->buffer_size;
172 src = (void *)runtime->dma_area +
173 frames_to_bytes(runtime, pcm_buffer_pointer);
174 remaining_frames = runtime->buffer_size - pcm_buffer_pointer;
176 for (i = 0; i < data_blocks; ++i) {
177 byte = (u8 *)buffer + p->pcm_byte_offset;
179 for (c = 0; c < channels; ++c) {
180 byte[0] = (*src >> 24) & 0xff;
181 byte[1] = (*src >> 16) & 0xff;
182 byte[2] = (*src >> 8) & 0xff;
183 byte += 3;
184 src++;
187 buffer += s->data_block_quadlets;
188 if (--remaining_frames == 0)
189 src = (void *)runtime->dma_area;
193 static void write_pcm_silence(struct amdtp_stream *s, __be32 *buffer,
194 unsigned int data_blocks)
196 struct amdtp_motu *p = s->protocol;
197 unsigned int channels, i, c;
198 u8 *byte;
200 channels = p->pcm_chunks;
202 for (i = 0; i < data_blocks; ++i) {
203 byte = (u8 *)buffer + p->pcm_byte_offset;
205 for (c = 0; c < channels; ++c) {
206 byte[0] = 0;
207 byte[1] = 0;
208 byte[2] = 0;
209 byte += 3;
212 buffer += s->data_block_quadlets;
216 int amdtp_motu_add_pcm_hw_constraints(struct amdtp_stream *s,
217 struct snd_pcm_runtime *runtime)
219 int err;
221 /* TODO: how to set an constraint for exactly 24bit PCM sample? */
222 err = snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
223 if (err < 0)
224 return err;
226 return amdtp_stream_add_pcm_hw_constraints(s, runtime);
229 void amdtp_motu_midi_trigger(struct amdtp_stream *s, unsigned int port,
230 struct snd_rawmidi_substream *midi)
232 struct amdtp_motu *p = s->protocol;
234 if (port < p->midi_ports)
235 WRITE_ONCE(p->midi, midi);
238 static void write_midi_messages(struct amdtp_stream *s, __be32 *buffer,
239 unsigned int data_blocks)
241 struct amdtp_motu *p = s->protocol;
242 struct snd_rawmidi_substream *midi = READ_ONCE(p->midi);
243 u8 *b;
244 int i;
246 for (i = 0; i < data_blocks; i++) {
247 b = (u8 *)buffer;
249 if (midi && p->midi_db_count == 0 &&
250 snd_rawmidi_transmit(midi, b + p->midi_byte_offset, 1) == 1) {
251 b[p->midi_flag_offset] = 0x01;
252 } else {
253 b[p->midi_byte_offset] = 0x00;
254 b[p->midi_flag_offset] = 0x00;
257 buffer += s->data_block_quadlets;
259 if (--p->midi_db_count < 0)
260 p->midi_db_count = p->midi_db_interval;
264 static void read_midi_messages(struct amdtp_stream *s, __be32 *buffer,
265 unsigned int data_blocks)
267 struct amdtp_motu *p = s->protocol;
268 struct snd_rawmidi_substream *midi;
269 u8 *b;
270 int i;
272 for (i = 0; i < data_blocks; i++) {
273 b = (u8 *)buffer;
274 midi = READ_ONCE(p->midi);
276 if (midi && (b[p->midi_flag_offset] & 0x01))
277 snd_rawmidi_receive(midi, b + p->midi_byte_offset, 1);
279 buffer += s->data_block_quadlets;
283 /* For tracepoints. */
284 static void __maybe_unused copy_sph(u32 *frames, __be32 *buffer,
285 unsigned int data_blocks,
286 unsigned int data_block_quadlets)
288 unsigned int i;
290 for (i = 0; i < data_blocks; ++i) {
291 *frames = be32_to_cpu(*buffer);
292 buffer += data_block_quadlets;
293 frames++;
297 /* For tracepoints. */
298 static void __maybe_unused copy_message(u64 *frames, __be32 *buffer,
299 unsigned int data_blocks,
300 unsigned int data_block_quadlets)
302 unsigned int i;
304 /* This is just for v2/v3 protocol. */
305 for (i = 0; i < data_blocks; ++i) {
306 *frames = (be32_to_cpu(buffer[1]) << 16) |
307 (be32_to_cpu(buffer[2]) >> 16);
308 buffer += data_block_quadlets;
309 frames++;
313 static void probe_tracepoints_events(struct amdtp_stream *s,
314 const struct pkt_desc *descs,
315 unsigned int packets)
317 int i;
319 for (i = 0; i < packets; ++i) {
320 const struct pkt_desc *desc = descs + i;
321 __be32 *buf = desc->ctx_payload;
322 unsigned int data_blocks = desc->data_blocks;
324 trace_data_block_sph(s, data_blocks, buf);
325 trace_data_block_message(s, data_blocks, buf);
329 static unsigned int process_ir_ctx_payloads(struct amdtp_stream *s,
330 const struct pkt_desc *descs,
331 unsigned int packets,
332 struct snd_pcm_substream *pcm)
334 struct amdtp_motu *p = s->protocol;
335 unsigned int pcm_frames = 0;
336 int i;
338 // For data block processing.
339 for (i = 0; i < packets; ++i) {
340 const struct pkt_desc *desc = descs + i;
341 __be32 *buf = desc->ctx_payload;
342 unsigned int data_blocks = desc->data_blocks;
344 if (pcm) {
345 read_pcm_s32(s, pcm, buf, data_blocks, pcm_frames);
346 pcm_frames += data_blocks;
349 if (p->midi_ports)
350 read_midi_messages(s, buf, data_blocks);
353 // For tracepoints.
354 if (trace_data_block_sph_enabled() ||
355 trace_data_block_message_enabled())
356 probe_tracepoints_events(s, descs, packets);
358 return pcm_frames;
361 static inline void compute_next_elapse_from_start(struct amdtp_motu *p)
363 p->next_accumulated += p->remainder_ticks_per_event;
364 if (p->next_accumulated >= 441) {
365 p->next_accumulated -= 441;
366 p->next_ticks++;
369 p->next_ticks += p->quotient_ticks_per_event;
370 if (p->next_ticks >= 3072) {
371 p->next_ticks -= 3072;
372 p->next_cycles++;
375 if (p->next_cycles >= 8000) {
376 p->next_cycles -= 8000;
377 p->next_seconds++;
380 if (p->next_seconds >= 128)
381 p->next_seconds -= 128;
384 static void write_sph(struct amdtp_stream *s, __be32 *buffer,
385 unsigned int data_blocks)
387 struct amdtp_motu *p = s->protocol;
388 unsigned int next_cycles;
389 unsigned int i;
390 u32 sph;
392 for (i = 0; i < data_blocks; i++) {
393 next_cycles = (s->start_cycle + p->next_cycles) % 8000;
394 sph = ((next_cycles << 12) | p->next_ticks) & 0x01ffffff;
395 *buffer = cpu_to_be32(sph);
397 compute_next_elapse_from_start(p);
399 buffer += s->data_block_quadlets;
403 static unsigned int process_it_ctx_payloads(struct amdtp_stream *s,
404 const struct pkt_desc *descs,
405 unsigned int packets,
406 struct snd_pcm_substream *pcm)
408 struct amdtp_motu *p = s->protocol;
409 unsigned int pcm_frames = 0;
410 int i;
412 // For data block processing.
413 for (i = 0; i < packets; ++i) {
414 const struct pkt_desc *desc = descs + i;
415 __be32 *buf = desc->ctx_payload;
416 unsigned int data_blocks = desc->data_blocks;
418 if (pcm) {
419 write_pcm_s32(s, pcm, buf, data_blocks, pcm_frames);
420 pcm_frames += data_blocks;
421 } else {
422 write_pcm_silence(s, buf, data_blocks);
425 if (p->midi_ports)
426 write_midi_messages(s, buf, data_blocks);
428 // TODO: how to interact control messages between userspace?
430 write_sph(s, buf, data_blocks);
433 // For tracepoints.
434 if (trace_data_block_sph_enabled() ||
435 trace_data_block_message_enabled())
436 probe_tracepoints_events(s, descs, packets);
438 return pcm_frames;
441 int amdtp_motu_init(struct amdtp_stream *s, struct fw_unit *unit,
442 enum amdtp_stream_direction dir,
443 const struct snd_motu_protocol *const protocol)
445 amdtp_stream_process_ctx_payloads_t process_ctx_payloads;
446 int fmt = CIP_FMT_MOTU;
447 int flags = CIP_BLOCKING;
448 int err;
450 if (dir == AMDTP_IN_STREAM) {
451 process_ctx_payloads = process_ir_ctx_payloads;
454 * Units of version 3 transmits packets with invalid CIP header
455 * against IEC 61883-1.
457 if (protocol == &snd_motu_protocol_v3) {
458 flags |= CIP_WRONG_DBS |
459 CIP_SKIP_DBC_ZERO_CHECK |
460 CIP_HEADER_WITHOUT_EOH;
461 fmt = CIP_FMT_MOTU_TX_V3;
464 if (protocol == &snd_motu_protocol_v2) {
465 // 8pre has some quirks.
466 flags |= CIP_WRONG_DBS |
467 CIP_SKIP_DBC_ZERO_CHECK;
469 } else {
470 process_ctx_payloads = process_it_ctx_payloads;
471 flags |= CIP_DBC_IS_END_EVENT;
474 err = amdtp_stream_init(s, unit, dir, flags, fmt, process_ctx_payloads,
475 sizeof(struct amdtp_motu));
476 if (err < 0)
477 return err;
479 s->sph = 1;
481 if (dir == AMDTP_OUT_STREAM) {
482 // Use fixed value for FDF field.
483 s->ctx_data.rx.fdf = MOTU_FDF_AM824;
484 // Not used.
485 s->ctx_data.rx.syt_override = 0xffff;
488 return 0;