2 * amdtp-motu.c - a part of driver for MOTU FireWire series
4 * Copyright (c) 2015-2017 Takashi Sakamoto <o-takashi@sakamocchi.jp>
6 * Licensed under the terms of the GNU General Public License, version 2.
9 #include <linux/slab.h>
10 #include <sound/pcm.h>
13 #define CREATE_TRACE_POINTS
14 #include "amdtp-motu-trace.h"
16 #define CIP_FMT_MOTU 0x02
17 #define CIP_FMT_MOTU_TX_V3 0x22
18 #define MOTU_FDF_AM824 0x22
21 * Nominally 3125 bytes/second, but the MIDI port's clock might be
22 * 1% too slow, and the bus clock 100 ppm too fast.
24 #define MIDI_BYTES_PER_SECOND 3093
27 /* For timestamp processing. */
28 unsigned int quotient_ticks_per_event
;
29 unsigned int remainder_ticks_per_event
;
30 unsigned int next_ticks
;
31 unsigned int next_accumulated
;
32 unsigned int next_cycles
;
33 unsigned int next_seconds
;
35 unsigned int pcm_chunks
;
36 unsigned int pcm_byte_offset
;
38 struct snd_rawmidi_substream
*midi
;
39 unsigned int midi_ports
;
40 unsigned int midi_flag_offset
;
41 unsigned int midi_byte_offset
;
44 unsigned int midi_db_interval
;
47 int amdtp_motu_set_parameters(struct amdtp_stream
*s
, unsigned int rate
,
48 unsigned int midi_ports
,
49 struct snd_motu_packet_format
*formats
)
52 unsigned int quotient_ticks_per_event
;
53 unsigned int remainder_ticks_per_event
;
55 [CIP_SFC_44100
] = { 557, 123 },
56 [CIP_SFC_48000
] = { 512, 0 },
57 [CIP_SFC_88200
] = { 278, 282 },
58 [CIP_SFC_96000
] = { 256, 0 },
59 [CIP_SFC_176400
] = { 139, 141 },
60 [CIP_SFC_192000
] = { 128, 0 },
62 struct amdtp_motu
*p
= s
->protocol
;
63 unsigned int pcm_chunks
, data_chunks
, data_block_quadlets
;
68 if (amdtp_stream_running(s
))
71 for (i
= 0; i
< ARRAY_SIZE(snd_motu_clock_rates
); ++i
) {
72 if (snd_motu_clock_rates
[i
] == rate
) {
77 if (i
== ARRAY_SIZE(snd_motu_clock_rates
))
80 pcm_chunks
= formats
->fixed_part_pcm_chunks
[mode
] +
81 formats
->differed_part_pcm_chunks
[mode
];
82 data_chunks
= formats
->msg_chunks
+ pcm_chunks
;
85 * Each data block includes SPH in its head. Data chunks follow with
86 * 3 byte alignment. Padding follows with zero to conform to quadlet
89 data_block_quadlets
= 1 + DIV_ROUND_UP(data_chunks
* 3, 4);
91 err
= amdtp_stream_set_parameters(s
, rate
, data_block_quadlets
);
95 p
->pcm_chunks
= pcm_chunks
;
96 p
->pcm_byte_offset
= formats
->pcm_byte_offset
;
98 p
->midi_ports
= midi_ports
;
99 p
->midi_flag_offset
= formats
->midi_flag_offset
;
100 p
->midi_byte_offset
= formats
->midi_byte_offset
;
102 p
->midi_db_count
= 0;
103 p
->midi_db_interval
= rate
/ MIDI_BYTES_PER_SECOND
;
105 /* IEEE 1394 bus requires. */
108 /* For no-data or empty packets to adjust PCM sampling frequency. */
109 delay
+= 8000 * 3072 * s
->syt_interval
/ rate
;
112 p
->next_cycles
= delay
/ 3072;
113 p
->quotient_ticks_per_event
= params
[s
->sfc
].quotient_ticks_per_event
;
114 p
->remainder_ticks_per_event
= params
[s
->sfc
].remainder_ticks_per_event
;
115 p
->next_ticks
= delay
% 3072;
116 p
->next_accumulated
= 0;
121 static void read_pcm_s32(struct amdtp_stream
*s
,
122 struct snd_pcm_runtime
*runtime
,
123 __be32
*buffer
, unsigned int data_blocks
)
125 struct amdtp_motu
*p
= s
->protocol
;
126 unsigned int channels
, remaining_frames
, i
, c
;
130 channels
= p
->pcm_chunks
;
131 dst
= (void *)runtime
->dma_area
+
132 frames_to_bytes(runtime
, s
->pcm_buffer_pointer
);
133 remaining_frames
= runtime
->buffer_size
- s
->pcm_buffer_pointer
;
135 for (i
= 0; i
< data_blocks
; ++i
) {
136 byte
= (u8
*)buffer
+ p
->pcm_byte_offset
;
138 for (c
= 0; c
< channels
; ++c
) {
139 *dst
= (byte
[0] << 24) | (byte
[1] << 16) | byte
[2];
143 buffer
+= s
->data_block_quadlets
;
144 if (--remaining_frames
== 0)
145 dst
= (void *)runtime
->dma_area
;
149 static void write_pcm_s32(struct amdtp_stream
*s
,
150 struct snd_pcm_runtime
*runtime
,
151 __be32
*buffer
, unsigned int data_blocks
)
153 struct amdtp_motu
*p
= s
->protocol
;
154 unsigned int channels
, remaining_frames
, i
, c
;
158 channels
= p
->pcm_chunks
;
159 src
= (void *)runtime
->dma_area
+
160 frames_to_bytes(runtime
, s
->pcm_buffer_pointer
);
161 remaining_frames
= runtime
->buffer_size
- s
->pcm_buffer_pointer
;
163 for (i
= 0; i
< data_blocks
; ++i
) {
164 byte
= (u8
*)buffer
+ p
->pcm_byte_offset
;
166 for (c
= 0; c
< channels
; ++c
) {
167 byte
[0] = (*src
>> 24) & 0xff;
168 byte
[1] = (*src
>> 16) & 0xff;
169 byte
[2] = (*src
>> 8) & 0xff;
174 buffer
+= s
->data_block_quadlets
;
175 if (--remaining_frames
== 0)
176 src
= (void *)runtime
->dma_area
;
180 static void write_pcm_silence(struct amdtp_stream
*s
, __be32
*buffer
,
181 unsigned int data_blocks
)
183 struct amdtp_motu
*p
= s
->protocol
;
184 unsigned int channels
, i
, c
;
187 channels
= p
->pcm_chunks
;
189 for (i
= 0; i
< data_blocks
; ++i
) {
190 byte
= (u8
*)buffer
+ p
->pcm_byte_offset
;
192 for (c
= 0; c
< channels
; ++c
) {
199 buffer
+= s
->data_block_quadlets
;
203 int amdtp_motu_add_pcm_hw_constraints(struct amdtp_stream
*s
,
204 struct snd_pcm_runtime
*runtime
)
208 /* TODO: how to set an constraint for exactly 24bit PCM sample? */
209 err
= snd_pcm_hw_constraint_msbits(runtime
, 0, 32, 24);
213 return amdtp_stream_add_pcm_hw_constraints(s
, runtime
);
216 void amdtp_motu_midi_trigger(struct amdtp_stream
*s
, unsigned int port
,
217 struct snd_rawmidi_substream
*midi
)
219 struct amdtp_motu
*p
= s
->protocol
;
221 if (port
< p
->midi_ports
)
222 WRITE_ONCE(p
->midi
, midi
);
225 static void write_midi_messages(struct amdtp_stream
*s
, __be32
*buffer
,
226 unsigned int data_blocks
)
228 struct amdtp_motu
*p
= s
->protocol
;
229 struct snd_rawmidi_substream
*midi
= READ_ONCE(p
->midi
);
233 for (i
= 0; i
< data_blocks
; i
++) {
236 if (midi
&& p
->midi_db_count
== 0 &&
237 snd_rawmidi_transmit(midi
, b
+ p
->midi_byte_offset
, 1) == 1) {
238 b
[p
->midi_flag_offset
] = 0x01;
240 b
[p
->midi_byte_offset
] = 0x00;
241 b
[p
->midi_flag_offset
] = 0x00;
244 buffer
+= s
->data_block_quadlets
;
246 if (--p
->midi_db_count
< 0)
247 p
->midi_db_count
= p
->midi_db_interval
;
251 static void read_midi_messages(struct amdtp_stream
*s
, __be32
*buffer
,
252 unsigned int data_blocks
)
254 struct amdtp_motu
*p
= s
->protocol
;
255 struct snd_rawmidi_substream
*midi
;
259 for (i
= 0; i
< data_blocks
; i
++) {
261 midi
= READ_ONCE(p
->midi
);
263 if (midi
&& (b
[p
->midi_flag_offset
] & 0x01))
264 snd_rawmidi_receive(midi
, b
+ p
->midi_byte_offset
, 1);
266 buffer
+= s
->data_block_quadlets
;
270 /* For tracepoints. */
271 static void __maybe_unused
copy_sph(u32
*frames
, __be32
*buffer
,
272 unsigned int data_blocks
,
273 unsigned int data_block_quadlets
)
277 for (i
= 0; i
< data_blocks
; ++i
) {
278 *frames
= be32_to_cpu(*buffer
);
279 buffer
+= data_block_quadlets
;
284 /* For tracepoints. */
285 static void __maybe_unused
copy_message(u64
*frames
, __be32
*buffer
,
286 unsigned int data_blocks
,
287 unsigned int data_block_quadlets
)
291 /* This is just for v2/v3 protocol. */
292 for (i
= 0; i
< data_blocks
; ++i
) {
293 *frames
= (be32_to_cpu(buffer
[1]) << 16) |
294 (be32_to_cpu(buffer
[2]) >> 16);
295 buffer
+= data_block_quadlets
;
300 static unsigned int process_tx_data_blocks(struct amdtp_stream
*s
,
301 __be32
*buffer
, unsigned int data_blocks
,
304 struct amdtp_motu
*p
= s
->protocol
;
305 struct snd_pcm_substream
*pcm
;
307 trace_in_data_block_sph(s
, data_blocks
, buffer
);
308 trace_in_data_block_message(s
, data_blocks
, buffer
);
311 read_midi_messages(s
, buffer
, data_blocks
);
313 pcm
= READ_ONCE(s
->pcm
);
314 if (data_blocks
> 0 && pcm
)
315 read_pcm_s32(s
, pcm
->runtime
, buffer
, data_blocks
);
320 static inline void compute_next_elapse_from_start(struct amdtp_motu
*p
)
322 p
->next_accumulated
+= p
->remainder_ticks_per_event
;
323 if (p
->next_accumulated
>= 441) {
324 p
->next_accumulated
-= 441;
328 p
->next_ticks
+= p
->quotient_ticks_per_event
;
329 if (p
->next_ticks
>= 3072) {
330 p
->next_ticks
-= 3072;
334 if (p
->next_cycles
>= 8000) {
335 p
->next_cycles
-= 8000;
339 if (p
->next_seconds
>= 128)
340 p
->next_seconds
-= 128;
343 static void write_sph(struct amdtp_stream
*s
, __be32
*buffer
,
344 unsigned int data_blocks
)
346 struct amdtp_motu
*p
= s
->protocol
;
347 unsigned int next_cycles
;
351 for (i
= 0; i
< data_blocks
; i
++) {
352 next_cycles
= (s
->start_cycle
+ p
->next_cycles
) % 8000;
353 sph
= ((next_cycles
<< 12) | p
->next_ticks
) & 0x01ffffff;
354 *buffer
= cpu_to_be32(sph
);
356 compute_next_elapse_from_start(p
);
358 buffer
+= s
->data_block_quadlets
;
362 static unsigned int process_rx_data_blocks(struct amdtp_stream
*s
,
363 __be32
*buffer
, unsigned int data_blocks
,
366 struct amdtp_motu
*p
= (struct amdtp_motu
*)s
->protocol
;
367 struct snd_pcm_substream
*pcm
;
372 /* TODO: how to interact control messages between userspace? */
375 write_midi_messages(s
, buffer
, data_blocks
);
377 pcm
= READ_ONCE(s
->pcm
);
379 write_pcm_s32(s
, pcm
->runtime
, buffer
, data_blocks
);
381 write_pcm_silence(s
, buffer
, data_blocks
);
383 write_sph(s
, buffer
, data_blocks
);
385 trace_out_data_block_sph(s
, data_blocks
, buffer
);
386 trace_out_data_block_message(s
, data_blocks
, buffer
);
391 int amdtp_motu_init(struct amdtp_stream
*s
, struct fw_unit
*unit
,
392 enum amdtp_stream_direction dir
,
393 const struct snd_motu_protocol
*const protocol
)
395 amdtp_stream_process_data_blocks_t process_data_blocks
;
396 int fmt
= CIP_FMT_MOTU
;
397 int flags
= CIP_BLOCKING
;
400 if (dir
== AMDTP_IN_STREAM
) {
401 process_data_blocks
= process_tx_data_blocks
;
404 * Units of version 3 transmits packets with invalid CIP header
405 * against IEC 61883-1.
407 if (protocol
== &snd_motu_protocol_v3
) {
408 flags
|= CIP_WRONG_DBS
|
409 CIP_SKIP_DBC_ZERO_CHECK
|
410 CIP_HEADER_WITHOUT_EOH
;
411 fmt
= CIP_FMT_MOTU_TX_V3
;
414 process_data_blocks
= process_rx_data_blocks
;
415 flags
|= CIP_DBC_IS_END_EVENT
;
418 err
= amdtp_stream_init(s
, unit
, dir
, flags
, fmt
, process_data_blocks
,
419 sizeof(struct amdtp_motu
));
424 s
->fdf
= MOTU_FDF_AM824
;