1 // SPDX-License-Identifier: GPL-2.0-only
3 * amdtp-motu.c - a part of driver for MOTU FireWire series
5 * Copyright (c) 2015-2017 Takashi Sakamoto <o-takashi@sakamocchi.jp>
8 #include <linux/slab.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
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;
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)
51 unsigned int quotient_ticks_per_event;
52 unsigned int remainder_ticks_per_event;
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;
67 if (amdtp_stream_running(s))
70 for (i = 0; i < ARRAY_SIZE(snd_motu_clock_rates); ++i) {
71 if (snd_motu_clock_rates[i] == rate) {
76 if (i == ARRAY_SIZE(snd_motu_clock_rates))
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
88 data_block_quadlets = 1 + DIV_ROUND_UP(data_chunks * 3, 4);
90 err = amdtp_stream_set_parameters(s, rate, data_block_quadlets);
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. */
107 /* For no-data or empty packets to adjust PCM sampling frequency. */
108 delay += 8000 * 3072 * s->syt_interval / rate;
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;
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;
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) |
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;
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;
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;
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) {
212 buffer += s->data_block_quadlets;
216 int amdtp_motu_add_pcm_hw_constraints(struct amdtp_stream *s,
217 struct snd_pcm_runtime *runtime)
221 /* TODO: how to set an constraint for exactly 24bit PCM sample? */
222 err = snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
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);
246 for (i = 0; i < data_blocks; i++) {
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;
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;
272 for (i = 0; i < data_blocks; i++) {
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)
290 for (i = 0; i < data_blocks; ++i) {
291 *frames = be32_to_cpu(*buffer);
292 buffer += data_block_quadlets;
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)
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;
313 static unsigned int process_tx_data_blocks(struct amdtp_stream *s,
314 const struct pkt_desc *desc,
315 struct snd_pcm_substream *pcm)
317 struct amdtp_motu *p = s->protocol;
318 unsigned int pcm_frames = 0;
320 trace_data_block_sph(s, desc->data_blocks, desc->ctx_payload);
321 trace_data_block_message(s, desc->data_blocks, desc->ctx_payload);
324 read_midi_messages(s, desc->ctx_payload, desc->data_blocks);
327 read_pcm_s32(s, pcm, desc->ctx_payload, desc->data_blocks,
329 pcm_frames = desc->data_blocks;
335 static inline void compute_next_elapse_from_start(struct amdtp_motu *p)
337 p->next_accumulated += p->remainder_ticks_per_event;
338 if (p->next_accumulated >= 441) {
339 p->next_accumulated -= 441;
343 p->next_ticks += p->quotient_ticks_per_event;
344 if (p->next_ticks >= 3072) {
345 p->next_ticks -= 3072;
349 if (p->next_cycles >= 8000) {
350 p->next_cycles -= 8000;
354 if (p->next_seconds >= 128)
355 p->next_seconds -= 128;
358 static void write_sph(struct amdtp_stream *s, __be32 *buffer,
359 unsigned int data_blocks)
361 struct amdtp_motu *p = s->protocol;
362 unsigned int next_cycles;
366 for (i = 0; i < data_blocks; i++) {
367 next_cycles = (s->start_cycle + p->next_cycles) % 8000;
368 sph = ((next_cycles << 12) | p->next_ticks) & 0x01ffffff;
369 *buffer = cpu_to_be32(sph);
371 compute_next_elapse_from_start(p);
373 buffer += s->data_block_quadlets;
377 static unsigned int process_rx_data_blocks(struct amdtp_stream *s,
378 const struct pkt_desc *desc,
379 struct snd_pcm_substream *pcm)
381 struct amdtp_motu *p = s->protocol;
382 unsigned int pcm_frames = 0;
384 /* TODO: how to interact control messages between userspace? */
387 write_midi_messages(s, desc->ctx_payload, desc->data_blocks);
390 write_pcm_s32(s, pcm, desc->ctx_payload, desc->data_blocks,
392 pcm_frames = desc->data_blocks;
394 write_pcm_silence(s, desc->ctx_payload, desc->data_blocks);
397 write_sph(s, desc->ctx_payload, desc->data_blocks);
399 trace_data_block_sph(s, desc->data_blocks, desc->ctx_payload);
400 trace_data_block_message(s, desc->data_blocks, desc->ctx_payload);
405 int amdtp_motu_init(struct amdtp_stream *s, struct fw_unit *unit,
406 enum amdtp_stream_direction dir,
407 const struct snd_motu_protocol *const protocol)
409 amdtp_stream_process_data_blocks_t process_data_blocks;
410 int fmt = CIP_FMT_MOTU;
411 int flags = CIP_BLOCKING;
414 if (dir == AMDTP_IN_STREAM) {
415 process_data_blocks = process_tx_data_blocks;
418 * Units of version 3 transmits packets with invalid CIP header
419 * against IEC 61883-1.
421 if (protocol == &snd_motu_protocol_v3) {
422 flags |= CIP_WRONG_DBS |
423 CIP_SKIP_DBC_ZERO_CHECK |
424 CIP_HEADER_WITHOUT_EOH;
425 fmt = CIP_FMT_MOTU_TX_V3;
428 if (protocol == &snd_motu_protocol_v2) {
429 // 8pre has some quirks.
430 flags |= CIP_WRONG_DBS |
431 CIP_SKIP_DBC_ZERO_CHECK;
434 process_data_blocks = process_rx_data_blocks;
435 flags |= CIP_DBC_IS_END_EVENT;
438 err = amdtp_stream_init(s, unit, dir, flags, fmt, process_data_blocks,
439 sizeof(struct amdtp_motu));
445 if (dir == AMDTP_OUT_STREAM) {
446 // Use fixed value for FDF field.
447 s->ctx_data.rx.fdf = MOTU_FDF_AM824;
449 s->ctx_data.rx.syt_override = 0xffff;