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 void probe_tracepoints_events(struct amdtp_stream
*s
,
314 const struct pkt_desc
*descs
,
315 unsigned int packets
)
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;
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
;
345 read_pcm_s32(s
, pcm
, buf
, data_blocks
, pcm_frames
);
346 pcm_frames
+= data_blocks
;
350 read_midi_messages(s
, buf
, data_blocks
);
354 if (trace_data_block_sph_enabled() ||
355 trace_data_block_message_enabled())
356 probe_tracepoints_events(s
, descs
, packets
);
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;
369 p
->next_ticks
+= p
->quotient_ticks_per_event
;
370 if (p
->next_ticks
>= 3072) {
371 p
->next_ticks
-= 3072;
375 if (p
->next_cycles
>= 8000) {
376 p
->next_cycles
-= 8000;
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
;
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;
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
;
419 write_pcm_s32(s
, pcm
, buf
, data_blocks
, pcm_frames
);
420 pcm_frames
+= data_blocks
;
422 write_pcm_silence(s
, buf
, data_blocks
);
426 write_midi_messages(s
, buf
, data_blocks
);
428 // TODO: how to interact control messages between userspace?
430 write_sph(s
, buf
, data_blocks
);
434 if (trace_data_block_sph_enabled() ||
435 trace_data_block_message_enabled())
436 probe_tracepoints_events(s
, descs
, packets
);
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
;
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
;
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
));
481 if (dir
== AMDTP_OUT_STREAM
) {
482 // Use fixed value for FDF field.
483 s
->ctx_data
.rx
.fdf
= MOTU_FDF_AM824
;
485 s
->ctx_data
.rx
.syt_override
= 0xffff;