xref: /openbmc/linux/sound/firewire/motu/amdtp-motu.c (revision 0bf49ffb)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * amdtp-motu.c - a part of driver for MOTU FireWire series
4  *
5  * Copyright (c) 2015-2017 Takashi Sakamoto <o-takashi@sakamocchi.jp>
6  */
7 
8 #include <linux/slab.h>
9 #include <sound/pcm.h>
10 #include "motu.h"
11 
12 #define CREATE_TRACE_POINTS
13 #include "amdtp-motu-trace.h"
14 
15 #define CIP_FMT_MOTU		0x02
16 #define CIP_FMT_MOTU_TX_V3	0x22
17 #define MOTU_FDF_AM824		0x22
18 
19 /*
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 
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;
33 
34 	unsigned int pcm_chunks;
35 	unsigned int pcm_byte_offset;
36 
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;
44 };
45 
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)
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 
79 	// 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];
83 	data_chunks = formats->msg_chunks + pcm_chunks;
84 	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 
93 	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 
100 	/* 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 
116 static 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)
119 {
120 	struct amdtp_motu *p = s->protocol;
121 	unsigned int channels = p->pcm_chunks;
122 	struct snd_pcm_runtime *runtime = pcm->runtime;
123 	unsigned int pcm_buffer_pointer;
124 	int remaining_frames;
125 	u8 *byte;
126 	u32 *dst;
127 	int i, c;
128 
129 	pcm_buffer_pointer = s->pcm_buffer_pointer + pcm_frames;
130 	pcm_buffer_pointer %= runtime->buffer_size;
131 
132 	dst = (void *)runtime->dma_area +
133 				frames_to_bytes(runtime, pcm_buffer_pointer);
134 	remaining_frames = runtime->buffer_size - pcm_buffer_pointer;
135 
136 	for (i = 0; i < data_blocks; ++i) {
137 		byte = (u8 *)buffer + p->pcm_byte_offset;
138 
139 		for (c = 0; c < channels; ++c) {
140 			*dst = (byte[0] << 24) |
141 			       (byte[1] << 16) |
142 			       (byte[2] << 8);
143 			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 
152 static 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)
155 {
156 	struct amdtp_motu *p = s->protocol;
157 	unsigned int channels = p->pcm_chunks;
158 	struct snd_pcm_runtime *runtime = pcm->runtime;
159 	unsigned int pcm_buffer_pointer;
160 	int remaining_frames;
161 	u8 *byte;
162 	const u32 *src;
163 	int i, c;
164 
165 	pcm_buffer_pointer = s->pcm_buffer_pointer + pcm_frames;
166 	pcm_buffer_pointer %= runtime->buffer_size;
167 
168 	src = (void *)runtime->dma_area +
169 				frames_to_bytes(runtime, pcm_buffer_pointer);
170 	remaining_frames = runtime->buffer_size - pcm_buffer_pointer;
171 
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 
189 static 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 
212 int 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 
225 void 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 
234 static 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 
260 static 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 
279 /* For tracepoints. */
280 static void __maybe_unused copy_sph(u32 *frames, __be32 *buffer,
281 				    unsigned int data_blocks,
282 				    unsigned int data_block_quadlets)
283 {
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 
293 /* For tracepoints. */
294 static void __maybe_unused copy_message(u64 *frames, __be32 *buffer,
295 					unsigned int data_blocks,
296 					unsigned int data_block_quadlets)
297 {
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 
309 static void probe_tracepoints_events(struct amdtp_stream *s,
310 				     const struct pkt_desc *descs,
311 				     unsigned int packets)
312 {
313 	int i;
314 
315 	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;
319 
320 		trace_data_block_sph(s, data_blocks, buf);
321 		trace_data_block_message(s, data_blocks, buf);
322 	}
323 }
324 
325 static 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;
339 
340 		if (pcm) {
341 			read_pcm_s32(s, pcm, buf, data_blocks, pcm_frames);
342 			pcm_frames += data_blocks;
343 		}
344 
345 		if (p->midi_ports)
346 			read_midi_messages(s, buf, data_blocks);
347 	}
348 
349 	// For tracepoints.
350 	if (trace_data_block_sph_enabled() ||
351 	    trace_data_block_message_enabled())
352 		probe_tracepoints_events(s, descs, packets);
353 
354 	return pcm_frames;
355 }
356 
357 static 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 
380 static 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 
399 static 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)
403 {
404 	struct amdtp_motu *p = s->protocol;
405 	unsigned int pcm_frames = 0;
406 	int i;
407 
408 	// For data block processing.
409 	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;
413 
414 		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 
421 		if (p->midi_ports)
422 			write_midi_messages(s, buf, data_blocks);
423 
424 		// TODO: how to interact control messages between userspace?
425 
426 		write_sph(s, buf, data_blocks);
427 	}
428 
429 	// For tracepoints.
430 	if (trace_data_block_sph_enabled() ||
431 	    trace_data_block_message_enabled())
432 		probe_tracepoints_events(s, descs, packets);
433 
434 	return pcm_frames;
435 }
436 
437 int amdtp_motu_init(struct amdtp_stream *s, struct fw_unit *unit,
438 		    enum amdtp_stream_direction dir,
439 		    const struct snd_motu_spec *spec)
440 {
441 	amdtp_stream_process_ctx_payloads_t process_ctx_payloads;
442 	int fmt = CIP_FMT_MOTU;
443 	int flags = CIP_BLOCKING;
444 	int err;
445 
446 	if (dir == AMDTP_IN_STREAM) {
447 		process_ctx_payloads = process_ir_ctx_payloads;
448 
449 		/*
450 		 * Units of version 3 transmits packets with invalid CIP header
451 		 * against IEC 61883-1.
452 		 */
453 		if (spec->protocol_version == SND_MOTU_PROTOCOL_V3) {
454 			flags |= CIP_WRONG_DBS |
455 				 CIP_SKIP_DBC_ZERO_CHECK |
456 				 CIP_HEADER_WITHOUT_EOH;
457 			fmt = CIP_FMT_MOTU_TX_V3;
458 		}
459 
460 		if (spec == &snd_motu_spec_8pre ||
461 		    spec == &snd_motu_spec_ultralite) {
462 			// 8pre has some quirks.
463 			flags |= CIP_WRONG_DBS |
464 				 CIP_SKIP_DBC_ZERO_CHECK;
465 		}
466 	} else {
467 		process_ctx_payloads = process_it_ctx_payloads;
468 		flags |= CIP_DBC_IS_END_EVENT;
469 	}
470 
471 	err = amdtp_stream_init(s, unit, dir, flags, fmt, process_ctx_payloads,
472 				sizeof(struct amdtp_motu));
473 	if (err < 0)
474 		return err;
475 
476 	s->sph = 1;
477 
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 	}
484 
485 	return 0;
486 }
487