forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-10-22 8ac6c7a54ed1b98d142dce24b11c6de6a1e239a5
kernel/sound/firewire/motu/amdtp-motu.c
....@@ -1,9 +1,8 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * amdtp-motu.c - a part of driver for MOTU FireWire series
34 *
45 * Copyright (c) 2015-2017 Takashi Sakamoto <o-takashi@sakamocchi.jp>
5
- *
6
- * Licensed under the terms of the GNU General Public License, version 2.
76 */
87
98 #include <linux/slab.h>
....@@ -77,15 +76,11 @@
7776 if (i == ARRAY_SIZE(snd_motu_clock_rates))
7877 return -EINVAL;
7978
80
- pcm_chunks = formats->fixed_part_pcm_chunks[mode] +
81
- formats->differed_part_pcm_chunks[mode];
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];
8283 data_chunks = formats->msg_chunks + pcm_chunks;
83
-
84
- /*
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
87
- * alignment.
88
- */
8984 data_block_quadlets = 1 + DIV_ROUND_UP(data_chunks * 3, 4);
9085
9186 err = amdtp_stream_set_parameters(s, rate, data_block_quadlets);
....@@ -118,19 +113,25 @@
118113 return 0;
119114 }
120115
121
-static void read_pcm_s32(struct amdtp_stream *s,
122
- struct snd_pcm_runtime *runtime,
123
- __be32 *buffer, unsigned int data_blocks)
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)
124119 {
125120 struct amdtp_motu *p = s->protocol;
126
- unsigned int channels, remaining_frames, i, c;
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;
127125 u8 *byte;
128126 u32 *dst;
127
+ int i, c;
129128
130
- channels = p->pcm_chunks;
129
+ pcm_buffer_pointer = s->pcm_buffer_pointer + pcm_frames;
130
+ pcm_buffer_pointer %= runtime->buffer_size;
131
+
131132 dst = (void *)runtime->dma_area +
132
- frames_to_bytes(runtime, s->pcm_buffer_pointer);
133
- remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
133
+ frames_to_bytes(runtime, pcm_buffer_pointer);
134
+ remaining_frames = runtime->buffer_size - pcm_buffer_pointer;
134135
135136 for (i = 0; i < data_blocks; ++i) {
136137 byte = (u8 *)buffer + p->pcm_byte_offset;
....@@ -148,19 +149,25 @@
148149 }
149150 }
150151
151
-static void write_pcm_s32(struct amdtp_stream *s,
152
- struct snd_pcm_runtime *runtime,
153
- __be32 *buffer, unsigned int data_blocks)
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)
154155 {
155156 struct amdtp_motu *p = s->protocol;
156
- unsigned int channels, remaining_frames, i, c;
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;
157161 u8 *byte;
158162 const u32 *src;
163
+ int i, c;
159164
160
- channels = p->pcm_chunks;
165
+ pcm_buffer_pointer = s->pcm_buffer_pointer + pcm_frames;
166
+ pcm_buffer_pointer %= runtime->buffer_size;
167
+
161168 src = (void *)runtime->dma_area +
162
- frames_to_bytes(runtime, s->pcm_buffer_pointer);
163
- remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
169
+ frames_to_bytes(runtime, pcm_buffer_pointer);
170
+ remaining_frames = runtime->buffer_size - pcm_buffer_pointer;
164171
165172 for (i = 0; i < data_blocks; ++i) {
166173 byte = (u8 *)buffer + p->pcm_byte_offset;
....@@ -299,24 +306,52 @@
299306 }
300307 }
301308
302
-static unsigned int process_tx_data_blocks(struct amdtp_stream *s,
303
- __be32 *buffer, unsigned int data_blocks,
304
- unsigned int *syt)
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)
305329 {
306330 struct amdtp_motu *p = s->protocol;
307
- struct snd_pcm_substream *pcm;
331
+ unsigned int pcm_frames = 0;
332
+ int i;
308333
309
- trace_in_data_block_sph(s, data_blocks, buffer);
310
- trace_in_data_block_message(s, data_blocks, buffer);
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;
311339
312
- if (p->midi_ports)
313
- read_midi_messages(s, buffer, data_blocks);
340
+ if (pcm) {
341
+ read_pcm_s32(s, pcm, buf, data_blocks, pcm_frames);
342
+ pcm_frames += data_blocks;
343
+ }
314344
315
- pcm = READ_ONCE(s->pcm);
316
- if (data_blocks > 0 && pcm)
317
- read_pcm_s32(s, pcm->runtime, buffer, data_blocks);
345
+ if (p->midi_ports)
346
+ read_midi_messages(s, buf, data_blocks);
347
+ }
318348
319
- return data_blocks;
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;
320355 }
321356
322357 static inline void compute_next_elapse_from_start(struct amdtp_motu *p)
....@@ -361,69 +396,91 @@
361396 }
362397 }
363398
364
-static unsigned int process_rx_data_blocks(struct amdtp_stream *s,
365
- __be32 *buffer, unsigned int data_blocks,
366
- unsigned int *syt)
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)
367403 {
368
- struct amdtp_motu *p = (struct amdtp_motu *)s->protocol;
369
- struct snd_pcm_substream *pcm;
404
+ struct amdtp_motu *p = s->protocol;
405
+ unsigned int pcm_frames = 0;
406
+ int i;
370407
371
- /* Not used. */
372
- *syt = 0xffff;
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;
373413
374
- /* TODO: how to interact control messages between userspace? */
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
+ }
375420
376
- if (p->midi_ports)
377
- write_midi_messages(s, buffer, data_blocks);
421
+ if (p->midi_ports)
422
+ write_midi_messages(s, buf, data_blocks);
378423
379
- pcm = READ_ONCE(s->pcm);
380
- if (pcm)
381
- write_pcm_s32(s, pcm->runtime, buffer, data_blocks);
382
- else
383
- write_pcm_silence(s, buffer, data_blocks);
424
+ // TODO: how to interact control messages between userspace?
384425
385
- write_sph(s, buffer, data_blocks);
426
+ write_sph(s, buf, data_blocks);
427
+ }
386428
387
- trace_out_data_block_sph(s, data_blocks, buffer);
388
- trace_out_data_block_message(s, data_blocks, buffer);
429
+ // For tracepoints.
430
+ if (trace_data_block_sph_enabled() ||
431
+ trace_data_block_message_enabled())
432
+ probe_tracepoints_events(s, descs, packets);
389433
390
- return data_blocks;
434
+ return pcm_frames;
391435 }
392436
393437 int amdtp_motu_init(struct amdtp_stream *s, struct fw_unit *unit,
394438 enum amdtp_stream_direction dir,
395
- const struct snd_motu_protocol *const protocol)
439
+ const struct snd_motu_spec *spec)
396440 {
397
- amdtp_stream_process_data_blocks_t process_data_blocks;
441
+ amdtp_stream_process_ctx_payloads_t process_ctx_payloads;
398442 int fmt = CIP_FMT_MOTU;
399443 int flags = CIP_BLOCKING;
400444 int err;
401445
402446 if (dir == AMDTP_IN_STREAM) {
403
- process_data_blocks = process_tx_data_blocks;
447
+ process_ctx_payloads = process_ir_ctx_payloads;
404448
405449 /*
406450 * Units of version 3 transmits packets with invalid CIP header
407451 * against IEC 61883-1.
408452 */
409
- if (protocol == &snd_motu_protocol_v3) {
453
+ if (spec->protocol_version == SND_MOTU_PROTOCOL_V3) {
410454 flags |= CIP_WRONG_DBS |
411455 CIP_SKIP_DBC_ZERO_CHECK |
412456 CIP_HEADER_WITHOUT_EOH;
413457 fmt = CIP_FMT_MOTU_TX_V3;
414458 }
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
+ }
415466 } else {
416
- process_data_blocks = process_rx_data_blocks;
467
+ process_ctx_payloads = process_it_ctx_payloads;
417468 flags |= CIP_DBC_IS_END_EVENT;
418469 }
419470
420
- err = amdtp_stream_init(s, unit, dir, flags, fmt, process_data_blocks,
471
+ err = amdtp_stream_init(s, unit, dir, flags, fmt, process_ctx_payloads,
421472 sizeof(struct amdtp_motu));
422473 if (err < 0)
423474 return err;
424475
425476 s->sph = 1;
426
- s->fdf = MOTU_FDF_AM824;
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
+ }
427484
428485 return 0;
429486 }