forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-11 1f93a7dfd1f8d5ff7a5c53246c7534fe2332d6f4
kernel/sound/firewire/amdtp-am824.c
....@@ -1,10 +1,9 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * AM824 format in Audio and Music Data Transmission Protocol (IEC 61883-6)
34 *
45 * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
56 * Copyright (c) 2015 Takashi Sakamoto <o-takashi@sakamocchi.jp>
6
- *
7
- * Licensed under the terms of the GNU General Public License, version 2.
87 */
98
109 #include <linux/slab.h>
....@@ -83,7 +82,8 @@
8382 if (err < 0)
8483 return err;
8584
86
- s->fdf = AMDTP_FDF_AM824 | s->sfc;
85
+ if (s->direction == AMDTP_OUT_STREAM)
86
+ s->ctx_data.rx.fdf = AMDTP_FDF_AM824 | s->sfc;
8787
8888 p->pcm_channels = pcm_channels;
8989 p->midi_ports = midi_ports;
....@@ -147,19 +147,24 @@
147147 }
148148 EXPORT_SYMBOL_GPL(amdtp_am824_set_midi_position);
149149
150
-static void write_pcm_s32(struct amdtp_stream *s,
151
- struct snd_pcm_substream *pcm,
152
- __be32 *buffer, unsigned int frames)
150
+static void write_pcm_s32(struct amdtp_stream *s, struct snd_pcm_substream *pcm,
151
+ __be32 *buffer, unsigned int frames,
152
+ unsigned int pcm_frames)
153153 {
154154 struct amdtp_am824 *p = s->protocol;
155
+ unsigned int channels = p->pcm_channels;
155156 struct snd_pcm_runtime *runtime = pcm->runtime;
156
- unsigned int channels, remaining_frames, i, c;
157
+ unsigned int pcm_buffer_pointer;
158
+ int remaining_frames;
157159 const u32 *src;
160
+ int i, c;
158161
159
- channels = p->pcm_channels;
162
+ pcm_buffer_pointer = s->pcm_buffer_pointer + pcm_frames;
163
+ pcm_buffer_pointer %= runtime->buffer_size;
164
+
160165 src = (void *)runtime->dma_area +
161
- frames_to_bytes(runtime, s->pcm_buffer_pointer);
162
- remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
166
+ frames_to_bytes(runtime, pcm_buffer_pointer);
167
+ remaining_frames = runtime->buffer_size - pcm_buffer_pointer;
163168
164169 for (i = 0; i < frames; ++i) {
165170 for (c = 0; c < channels; ++c) {
....@@ -173,19 +178,24 @@
173178 }
174179 }
175180
176
-static void read_pcm_s32(struct amdtp_stream *s,
177
- struct snd_pcm_substream *pcm,
178
- __be32 *buffer, unsigned int frames)
181
+static void read_pcm_s32(struct amdtp_stream *s, struct snd_pcm_substream *pcm,
182
+ __be32 *buffer, unsigned int frames,
183
+ unsigned int pcm_frames)
179184 {
180185 struct amdtp_am824 *p = s->protocol;
186
+ unsigned int channels = p->pcm_channels;
181187 struct snd_pcm_runtime *runtime = pcm->runtime;
182
- unsigned int channels, remaining_frames, i, c;
188
+ unsigned int pcm_buffer_pointer;
189
+ int remaining_frames;
183190 u32 *dst;
191
+ int i, c;
184192
185
- channels = p->pcm_channels;
193
+ pcm_buffer_pointer = s->pcm_buffer_pointer + pcm_frames;
194
+ pcm_buffer_pointer %= runtime->buffer_size;
195
+
186196 dst = (void *)runtime->dma_area +
187
- frames_to_bytes(runtime, s->pcm_buffer_pointer);
188
- remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
197
+ frames_to_bytes(runtime, pcm_buffer_pointer);
198
+ remaining_frames = runtime->buffer_size - pcm_buffer_pointer;
189199
190200 for (i = 0; i < frames; ++i) {
191201 for (c = 0; c < channels; ++c) {
....@@ -285,7 +295,7 @@
285295 }
286296
287297 static void write_midi_messages(struct amdtp_stream *s, __be32 *buffer,
288
- unsigned int frames)
298
+ unsigned int frames, unsigned int data_block_counter)
289299 {
290300 struct amdtp_am824 *p = s->protocol;
291301 unsigned int f, port;
....@@ -294,7 +304,7 @@
294304 for (f = 0; f < frames; f++) {
295305 b = (u8 *)&buffer[p->midi_position];
296306
297
- port = (s->data_block_counter + f) % 8;
307
+ port = (data_block_counter + f) % 8;
298308 if (f < MAX_MIDI_RX_BLOCKS &&
299309 midi_ratelimit_per_packet(s, port) &&
300310 p->midi[port] != NULL &&
....@@ -312,16 +322,20 @@
312322 }
313323 }
314324
315
-static void read_midi_messages(struct amdtp_stream *s,
316
- __be32 *buffer, unsigned int frames)
325
+static void read_midi_messages(struct amdtp_stream *s, __be32 *buffer,
326
+ unsigned int frames, unsigned int data_block_counter)
317327 {
318328 struct amdtp_am824 *p = s->protocol;
319
- unsigned int f, port;
320329 int len;
321330 u8 *b;
331
+ int f;
322332
323333 for (f = 0; f < frames; f++) {
324
- port = (8 - s->tx_first_dbc + s->data_block_counter + f) % 8;
334
+ unsigned int port = f;
335
+
336
+ if (!(s->flags & CIP_UNALIGHED_DBC))
337
+ port += data_block_counter;
338
+ port %= 8;
325339 b = (u8 *)&buffer[p->midi_position];
326340
327341 len = b[0] - 0x80;
....@@ -332,43 +346,60 @@
332346 }
333347 }
334348
335
-static unsigned int process_rx_data_blocks(struct amdtp_stream *s, __be32 *buffer,
336
- unsigned int data_blocks, unsigned int *syt)
349
+static unsigned int process_it_ctx_payloads(struct amdtp_stream *s,
350
+ const struct pkt_desc *descs,
351
+ unsigned int packets,
352
+ struct snd_pcm_substream *pcm)
337353 {
338354 struct amdtp_am824 *p = s->protocol;
339
- struct snd_pcm_substream *pcm = READ_ONCE(s->pcm);
340
- unsigned int pcm_frames;
355
+ unsigned int pcm_frames = 0;
356
+ int i;
341357
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;
358
+ for (i = 0; i < packets; ++i) {
359
+ const struct pkt_desc *desc = descs + i;
360
+ __be32 *buf = desc->ctx_payload;
361
+ unsigned int data_blocks = desc->data_blocks;
362
+
363
+ if (pcm) {
364
+ write_pcm_s32(s, pcm, buf, data_blocks, pcm_frames);
365
+ pcm_frames += data_blocks * p->frame_multiplier;
366
+ } else {
367
+ write_pcm_silence(s, buf, data_blocks);
368
+ }
369
+
370
+ if (p->midi_ports) {
371
+ write_midi_messages(s, buf, data_blocks,
372
+ desc->data_block_counter);
373
+ }
348374 }
349
-
350
- if (p->midi_ports)
351
- write_midi_messages(s, buffer, data_blocks);
352375
353376 return pcm_frames;
354377 }
355378
356
-static unsigned int process_tx_data_blocks(struct amdtp_stream *s, __be32 *buffer,
357
- unsigned int data_blocks, unsigned int *syt)
379
+static unsigned int process_ir_ctx_payloads(struct amdtp_stream *s,
380
+ const struct pkt_desc *descs,
381
+ unsigned int packets,
382
+ struct snd_pcm_substream *pcm)
358383 {
359384 struct amdtp_am824 *p = s->protocol;
360
- struct snd_pcm_substream *pcm = READ_ONCE(s->pcm);
361
- unsigned int pcm_frames;
385
+ unsigned int pcm_frames = 0;
386
+ int i;
362387
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;
388
+ for (i = 0; i < packets; ++i) {
389
+ const struct pkt_desc *desc = descs + i;
390
+ __be32 *buf = desc->ctx_payload;
391
+ unsigned int data_blocks = desc->data_blocks;
392
+
393
+ if (pcm) {
394
+ read_pcm_s32(s, pcm, buf, data_blocks, pcm_frames);
395
+ pcm_frames += data_blocks * p->frame_multiplier;
396
+ }
397
+
398
+ if (p->midi_ports) {
399
+ read_midi_messages(s, buf, data_blocks,
400
+ desc->data_block_counter);
401
+ }
368402 }
369
-
370
- if (p->midi_ports)
371
- read_midi_messages(s, buffer, data_blocks);
372403
373404 return pcm_frames;
374405 }
....@@ -384,15 +415,14 @@
384415 int amdtp_am824_init(struct amdtp_stream *s, struct fw_unit *unit,
385416 enum amdtp_stream_direction dir, enum cip_flags flags)
386417 {
387
- amdtp_stream_process_data_blocks_t process_data_blocks;
418
+ amdtp_stream_process_ctx_payloads_t process_ctx_payloads;
388419
389420 if (dir == AMDTP_IN_STREAM)
390
- process_data_blocks = process_tx_data_blocks;
421
+ process_ctx_payloads = process_ir_ctx_payloads;
391422 else
392
- process_data_blocks = process_rx_data_blocks;
423
+ process_ctx_payloads = process_it_ctx_payloads;
393424
394425 return amdtp_stream_init(s, unit, dir, flags, CIP_FMT_AM,
395
- process_data_blocks,
396
- sizeof(struct amdtp_am824));
426
+ process_ctx_payloads, sizeof(struct amdtp_am824));
397427 }
398428 EXPORT_SYMBOL_GPL(amdtp_am824_init);