1 /*
2  * AM824 format in Audio and Music Data Transmission Protocol (IEC 61883-6)
3  *
4  * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
5  * Copyright (c) 2015 Takashi Sakamoto <o-takashi@sakamocchi.jp>
6  *
7  * Licensed under the terms of the GNU General Public License, version 2.
8  */
9 
10 #include <linux/slab.h>
11 
12 #include "amdtp-am824.h"
13 
14 #define CIP_FMT_AM		0x10
15 
16 /* "Clock-based rate control mode" is just supported. */
17 #define AMDTP_FDF_AM824		0x00
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 /*
26  * Several devices look only at the first eight data blocks.
27  * In any case, this is more than enough for the MIDI data rate.
28  */
29 #define MAX_MIDI_RX_BLOCKS	8
30 
31 struct amdtp_am824 {
32 	struct snd_rawmidi_substream *midi[AM824_MAX_CHANNELS_FOR_MIDI * 8];
33 	int midi_fifo_limit;
34 	int midi_fifo_used[AM824_MAX_CHANNELS_FOR_MIDI * 8];
35 	unsigned int pcm_channels;
36 	unsigned int midi_ports;
37 
38 	u8 pcm_positions[AM824_MAX_CHANNELS_FOR_PCM];
39 	u8 midi_position;
40 
41 	unsigned int frame_multiplier;
42 };
43 
44 /**
45  * amdtp_am824_set_parameters - set stream parameters
46  * @s: the AMDTP stream to configure
47  * @rate: the sample rate
48  * @pcm_channels: the number of PCM samples in each data block, to be encoded
49  *                as AM824 multi-bit linear audio
50  * @midi_ports: the number of MIDI ports (i.e., MPX-MIDI Data Channels)
51  * @double_pcm_frames: one data block transfers two PCM frames
52  *
53  * The parameters must be set before the stream is started, and must not be
54  * changed while the stream is running.
55  */
amdtp_am824_set_parameters(struct amdtp_stream * s,unsigned int rate,unsigned int pcm_channels,unsigned int midi_ports,bool double_pcm_frames)56 int amdtp_am824_set_parameters(struct amdtp_stream *s, unsigned int rate,
57 			       unsigned int pcm_channels,
58 			       unsigned int midi_ports,
59 			       bool double_pcm_frames)
60 {
61 	struct amdtp_am824 *p = s->protocol;
62 	unsigned int midi_channels;
63 	unsigned int i;
64 	int err;
65 
66 	if (amdtp_stream_running(s))
67 		return -EINVAL;
68 
69 	if (pcm_channels > AM824_MAX_CHANNELS_FOR_PCM)
70 		return -EINVAL;
71 
72 	midi_channels = DIV_ROUND_UP(midi_ports, 8);
73 	if (midi_channels > AM824_MAX_CHANNELS_FOR_MIDI)
74 		return -EINVAL;
75 
76 	if (WARN_ON(amdtp_stream_running(s)) ||
77 	    WARN_ON(pcm_channels > AM824_MAX_CHANNELS_FOR_PCM) ||
78 	    WARN_ON(midi_channels > AM824_MAX_CHANNELS_FOR_MIDI))
79 		return -EINVAL;
80 
81 	err = amdtp_stream_set_parameters(s, rate,
82 					  pcm_channels + midi_channels);
83 	if (err < 0)
84 		return err;
85 
86 	s->fdf = AMDTP_FDF_AM824 | s->sfc;
87 
88 	p->pcm_channels = pcm_channels;
89 	p->midi_ports = midi_ports;
90 
91 	/*
92 	 * In IEC 61883-6, one data block represents one event. In ALSA, one
93 	 * event equals to one PCM frame. But Dice has a quirk at higher
94 	 * sampling rate to transfer two PCM frames in one data block.
95 	 */
96 	if (double_pcm_frames)
97 		p->frame_multiplier = 2;
98 	else
99 		p->frame_multiplier = 1;
100 
101 	/* init the position map for PCM and MIDI channels */
102 	for (i = 0; i < pcm_channels; i++)
103 		p->pcm_positions[i] = i;
104 	p->midi_position = p->pcm_channels;
105 
106 	/*
107 	 * We do not know the actual MIDI FIFO size of most devices.  Just
108 	 * assume two bytes, i.e., one byte can be received over the bus while
109 	 * the previous one is transmitted over MIDI.
110 	 * (The value here is adjusted for midi_ratelimit_per_packet().)
111 	 */
112 	p->midi_fifo_limit = rate - MIDI_BYTES_PER_SECOND * s->syt_interval + 1;
113 
114 	return 0;
115 }
116 EXPORT_SYMBOL_GPL(amdtp_am824_set_parameters);
117 
118 /**
119  * amdtp_am824_set_pcm_position - set an index of data channel for a channel
120  *				  of PCM frame
121  * @s: the AMDTP stream
122  * @index: the index of data channel in an data block
123  * @position: the channel of PCM frame
124  */
amdtp_am824_set_pcm_position(struct amdtp_stream * s,unsigned int index,unsigned int position)125 void amdtp_am824_set_pcm_position(struct amdtp_stream *s, unsigned int index,
126 				 unsigned int position)
127 {
128 	struct amdtp_am824 *p = s->protocol;
129 
130 	if (index < p->pcm_channels)
131 		p->pcm_positions[index] = position;
132 }
133 EXPORT_SYMBOL_GPL(amdtp_am824_set_pcm_position);
134 
135 /**
136  * amdtp_am824_set_midi_position - set a index of data channel for MIDI
137  *				   conformant data channel
138  * @s: the AMDTP stream
139  * @position: the index of data channel in an data block
140  */
amdtp_am824_set_midi_position(struct amdtp_stream * s,unsigned int position)141 void amdtp_am824_set_midi_position(struct amdtp_stream *s,
142 				   unsigned int position)
143 {
144 	struct amdtp_am824 *p = s->protocol;
145 
146 	p->midi_position = position;
147 }
148 EXPORT_SYMBOL_GPL(amdtp_am824_set_midi_position);
149 
write_pcm_s32(struct amdtp_stream * s,struct snd_pcm_substream * pcm,__be32 * buffer,unsigned int frames)150 static void write_pcm_s32(struct amdtp_stream *s,
151 			  struct snd_pcm_substream *pcm,
152 			  __be32 *buffer, unsigned int frames)
153 {
154 	struct amdtp_am824 *p = s->protocol;
155 	struct snd_pcm_runtime *runtime = pcm->runtime;
156 	unsigned int channels, remaining_frames, i, c;
157 	const u32 *src;
158 
159 	channels = p->pcm_channels;
160 	src = (void *)runtime->dma_area +
161 			frames_to_bytes(runtime, s->pcm_buffer_pointer);
162 	remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
163 
164 	for (i = 0; i < frames; ++i) {
165 		for (c = 0; c < channels; ++c) {
166 			buffer[p->pcm_positions[c]] =
167 					cpu_to_be32((*src >> 8) | 0x40000000);
168 			src++;
169 		}
170 		buffer += s->data_block_quadlets;
171 		if (--remaining_frames == 0)
172 			src = (void *)runtime->dma_area;
173 	}
174 }
175 
read_pcm_s32(struct amdtp_stream * s,struct snd_pcm_substream * pcm,__be32 * buffer,unsigned int frames)176 static void read_pcm_s32(struct amdtp_stream *s,
177 			 struct snd_pcm_substream *pcm,
178 			 __be32 *buffer, unsigned int frames)
179 {
180 	struct amdtp_am824 *p = s->protocol;
181 	struct snd_pcm_runtime *runtime = pcm->runtime;
182 	unsigned int channels, remaining_frames, i, c;
183 	u32 *dst;
184 
185 	channels = p->pcm_channels;
186 	dst  = (void *)runtime->dma_area +
187 			frames_to_bytes(runtime, s->pcm_buffer_pointer);
188 	remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
189 
190 	for (i = 0; i < frames; ++i) {
191 		for (c = 0; c < channels; ++c) {
192 			*dst = be32_to_cpu(buffer[p->pcm_positions[c]]) << 8;
193 			dst++;
194 		}
195 		buffer += s->data_block_quadlets;
196 		if (--remaining_frames == 0)
197 			dst = (void *)runtime->dma_area;
198 	}
199 }
200 
write_pcm_silence(struct amdtp_stream * s,__be32 * buffer,unsigned int frames)201 static void write_pcm_silence(struct amdtp_stream *s,
202 			      __be32 *buffer, unsigned int frames)
203 {
204 	struct amdtp_am824 *p = s->protocol;
205 	unsigned int i, c, channels = p->pcm_channels;
206 
207 	for (i = 0; i < frames; ++i) {
208 		for (c = 0; c < channels; ++c)
209 			buffer[p->pcm_positions[c]] = cpu_to_be32(0x40000000);
210 		buffer += s->data_block_quadlets;
211 	}
212 }
213 
214 /**
215  * amdtp_am824_add_pcm_hw_constraints - add hw constraints for PCM substream
216  * @s:		the AMDTP stream for AM824 data block, must be initialized.
217  * @runtime:	the PCM substream runtime
218  *
219  */
amdtp_am824_add_pcm_hw_constraints(struct amdtp_stream * s,struct snd_pcm_runtime * runtime)220 int amdtp_am824_add_pcm_hw_constraints(struct amdtp_stream *s,
221 				       struct snd_pcm_runtime *runtime)
222 {
223 	int err;
224 
225 	err = amdtp_stream_add_pcm_hw_constraints(s, runtime);
226 	if (err < 0)
227 		return err;
228 
229 	/* AM824 in IEC 61883-6 can deliver 24bit data. */
230 	return snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
231 }
232 EXPORT_SYMBOL_GPL(amdtp_am824_add_pcm_hw_constraints);
233 
234 /**
235  * amdtp_am824_midi_trigger - start/stop playback/capture with a MIDI device
236  * @s: the AMDTP stream
237  * @port: index of MIDI port
238  * @midi: the MIDI device to be started, or %NULL to stop the current device
239  *
240  * Call this function on a running isochronous stream to enable the actual
241  * transmission of MIDI data.  This function should be called from the MIDI
242  * device's .trigger callback.
243  */
amdtp_am824_midi_trigger(struct amdtp_stream * s,unsigned int port,struct snd_rawmidi_substream * midi)244 void amdtp_am824_midi_trigger(struct amdtp_stream *s, unsigned int port,
245 			      struct snd_rawmidi_substream *midi)
246 {
247 	struct amdtp_am824 *p = s->protocol;
248 
249 	if (port < p->midi_ports)
250 		WRITE_ONCE(p->midi[port], midi);
251 }
252 EXPORT_SYMBOL_GPL(amdtp_am824_midi_trigger);
253 
254 /*
255  * To avoid sending MIDI bytes at too high a rate, assume that the receiving
256  * device has a FIFO, and track how much it is filled.  This values increases
257  * by one whenever we send one byte in a packet, but the FIFO empties at
258  * a constant rate independent of our packet rate.  One packet has syt_interval
259  * samples, so the number of bytes that empty out of the FIFO, per packet(!),
260  * is MIDI_BYTES_PER_SECOND * syt_interval / sample_rate.  To avoid storing
261  * fractional values, the values in midi_fifo_used[] are measured in bytes
262  * multiplied by the sample rate.
263  */
midi_ratelimit_per_packet(struct amdtp_stream * s,unsigned int port)264 static bool midi_ratelimit_per_packet(struct amdtp_stream *s, unsigned int port)
265 {
266 	struct amdtp_am824 *p = s->protocol;
267 	int used;
268 
269 	used = p->midi_fifo_used[port];
270 	if (used == 0) /* common shortcut */
271 		return true;
272 
273 	used -= MIDI_BYTES_PER_SECOND * s->syt_interval;
274 	used = max(used, 0);
275 	p->midi_fifo_used[port] = used;
276 
277 	return used < p->midi_fifo_limit;
278 }
279 
midi_rate_use_one_byte(struct amdtp_stream * s,unsigned int port)280 static void midi_rate_use_one_byte(struct amdtp_stream *s, unsigned int port)
281 {
282 	struct amdtp_am824 *p = s->protocol;
283 
284 	p->midi_fifo_used[port] += amdtp_rate_table[s->sfc];
285 }
286 
write_midi_messages(struct amdtp_stream * s,__be32 * buffer,unsigned int frames)287 static void write_midi_messages(struct amdtp_stream *s, __be32 *buffer,
288 				unsigned int frames)
289 {
290 	struct amdtp_am824 *p = s->protocol;
291 	unsigned int f, port;
292 	u8 *b;
293 
294 	for (f = 0; f < frames; f++) {
295 		b = (u8 *)&buffer[p->midi_position];
296 
297 		port = (s->data_block_counter + f) % 8;
298 		if (f < MAX_MIDI_RX_BLOCKS &&
299 		    midi_ratelimit_per_packet(s, port) &&
300 		    p->midi[port] != NULL &&
301 		    snd_rawmidi_transmit(p->midi[port], &b[1], 1) == 1) {
302 			midi_rate_use_one_byte(s, port);
303 			b[0] = 0x81;
304 		} else {
305 			b[0] = 0x80;
306 			b[1] = 0;
307 		}
308 		b[2] = 0;
309 		b[3] = 0;
310 
311 		buffer += s->data_block_quadlets;
312 	}
313 }
314 
read_midi_messages(struct amdtp_stream * s,__be32 * buffer,unsigned int frames)315 static void read_midi_messages(struct amdtp_stream *s,
316 			       __be32 *buffer, unsigned int frames)
317 {
318 	struct amdtp_am824 *p = s->protocol;
319 	unsigned int f, port;
320 	int len;
321 	u8 *b;
322 
323 	for (f = 0; f < frames; f++) {
324 		port = (s->data_block_counter + f) % 8;
325 		b = (u8 *)&buffer[p->midi_position];
326 
327 		len = b[0] - 0x80;
328 		if ((1 <= len) &&  (len <= 3) && (p->midi[port]))
329 			snd_rawmidi_receive(p->midi[port], b + 1, len);
330 
331 		buffer += s->data_block_quadlets;
332 	}
333 }
334 
process_rx_data_blocks(struct amdtp_stream * s,__be32 * buffer,unsigned int data_blocks,unsigned int * syt)335 static unsigned int process_rx_data_blocks(struct amdtp_stream *s, __be32 *buffer,
336 					   unsigned int data_blocks, unsigned int *syt)
337 {
338 	struct amdtp_am824 *p = s->protocol;
339 	struct snd_pcm_substream *pcm = READ_ONCE(s->pcm);
340 	unsigned int pcm_frames;
341 
342 	if (pcm) {
343 		write_pcm_s32(s, pcm, buffer, data_blocks);
344 		pcm_frames = data_blocks * p->frame_multiplier;
345 	} else {
346 		write_pcm_silence(s, buffer, data_blocks);
347 		pcm_frames = 0;
348 	}
349 
350 	if (p->midi_ports)
351 		write_midi_messages(s, buffer, data_blocks);
352 
353 	return pcm_frames;
354 }
355 
process_tx_data_blocks(struct amdtp_stream * s,__be32 * buffer,unsigned int data_blocks,unsigned int * syt)356 static unsigned int process_tx_data_blocks(struct amdtp_stream *s, __be32 *buffer,
357 					   unsigned int data_blocks, unsigned int *syt)
358 {
359 	struct amdtp_am824 *p = s->protocol;
360 	struct snd_pcm_substream *pcm = READ_ONCE(s->pcm);
361 	unsigned int pcm_frames;
362 
363 	if (pcm) {
364 		read_pcm_s32(s, pcm, buffer, data_blocks);
365 		pcm_frames = data_blocks * p->frame_multiplier;
366 	} else {
367 		pcm_frames = 0;
368 	}
369 
370 	if (p->midi_ports)
371 		read_midi_messages(s, buffer, data_blocks);
372 
373 	return pcm_frames;
374 }
375 
376 /**
377  * amdtp_am824_init - initialize an AMDTP stream structure to handle AM824
378  *		      data block
379  * @s: the AMDTP stream to initialize
380  * @unit: the target of the stream
381  * @dir: the direction of stream
382  * @flags: the packet transmission method to use
383  */
amdtp_am824_init(struct amdtp_stream * s,struct fw_unit * unit,enum amdtp_stream_direction dir,enum cip_flags flags)384 int amdtp_am824_init(struct amdtp_stream *s, struct fw_unit *unit,
385 		     enum amdtp_stream_direction dir, enum cip_flags flags)
386 {
387 	amdtp_stream_process_data_blocks_t process_data_blocks;
388 
389 	if (dir == AMDTP_IN_STREAM)
390 		process_data_blocks = process_tx_data_blocks;
391 	else
392 		process_data_blocks = process_rx_data_blocks;
393 
394 	return amdtp_stream_init(s, unit, dir, flags, CIP_FMT_AM,
395 				 process_data_blocks,
396 				 sizeof(struct amdtp_am824));
397 }
398 EXPORT_SYMBOL_GPL(amdtp_am824_init);
399