Commit | Line | Data |
---|---|---|
da607e19 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
6fb7db90 TS |
2 | /* |
3 | * amdtp-ff.c - a part of driver for RME Fireface series | |
4 | * | |
5 | * Copyright (c) 2015-2017 Takashi Sakamoto | |
6fb7db90 TS |
6 | */ |
7 | ||
8 | #include <sound/pcm.h> | |
9 | #include "ff.h" | |
10 | ||
11 | struct amdtp_ff { | |
12 | unsigned int pcm_channels; | |
13 | }; | |
14 | ||
15 | int amdtp_ff_set_parameters(struct amdtp_stream *s, unsigned int rate, | |
16 | unsigned int pcm_channels) | |
17 | { | |
18 | struct amdtp_ff *p = s->protocol; | |
19 | unsigned int data_channels; | |
20 | ||
21 | if (amdtp_stream_running(s)) | |
22 | return -EBUSY; | |
23 | ||
24 | p->pcm_channels = pcm_channels; | |
25 | data_channels = pcm_channels; | |
26 | ||
a36183f6 | 27 | return amdtp_stream_set_parameters(s, rate, data_channels, 1); |
6fb7db90 TS |
28 | } |
29 | ||
4731c672 TS |
30 | static void write_pcm_s32(struct amdtp_stream *s, struct snd_pcm_substream *pcm, |
31 | __le32 *buffer, unsigned int frames, | |
32 | unsigned int pcm_frames) | |
6fb7db90 TS |
33 | { |
34 | struct amdtp_ff *p = s->protocol; | |
4731c672 | 35 | unsigned int channels = p->pcm_channels; |
6fb7db90 | 36 | struct snd_pcm_runtime *runtime = pcm->runtime; |
4731c672 TS |
37 | unsigned int pcm_buffer_pointer; |
38 | int remaining_frames; | |
6fb7db90 | 39 | const u32 *src; |
4731c672 TS |
40 | int i, c; |
41 | ||
42 | pcm_buffer_pointer = s->pcm_buffer_pointer + pcm_frames; | |
43 | pcm_buffer_pointer %= runtime->buffer_size; | |
6fb7db90 | 44 | |
6fb7db90 | 45 | src = (void *)runtime->dma_area + |
4731c672 TS |
46 | frames_to_bytes(runtime, pcm_buffer_pointer); |
47 | remaining_frames = runtime->buffer_size - pcm_buffer_pointer; | |
6fb7db90 TS |
48 | |
49 | for (i = 0; i < frames; ++i) { | |
50 | for (c = 0; c < channels; ++c) { | |
51 | buffer[c] = cpu_to_le32(*src); | |
52 | src++; | |
53 | } | |
54 | buffer += s->data_block_quadlets; | |
55 | if (--remaining_frames == 0) | |
56 | src = (void *)runtime->dma_area; | |
57 | } | |
58 | } | |
59 | ||
4731c672 TS |
60 | static void read_pcm_s32(struct amdtp_stream *s, struct snd_pcm_substream *pcm, |
61 | __le32 *buffer, unsigned int frames, | |
62 | unsigned int pcm_frames) | |
6fb7db90 TS |
63 | { |
64 | struct amdtp_ff *p = s->protocol; | |
4731c672 | 65 | unsigned int channels = p->pcm_channels; |
6fb7db90 | 66 | struct snd_pcm_runtime *runtime = pcm->runtime; |
4731c672 TS |
67 | unsigned int pcm_buffer_pointer; |
68 | int remaining_frames; | |
6fb7db90 | 69 | u32 *dst; |
4731c672 TS |
70 | int i, c; |
71 | ||
72 | pcm_buffer_pointer = s->pcm_buffer_pointer + pcm_frames; | |
73 | pcm_buffer_pointer %= runtime->buffer_size; | |
6fb7db90 | 74 | |
6fb7db90 | 75 | dst = (void *)runtime->dma_area + |
4731c672 TS |
76 | frames_to_bytes(runtime, pcm_buffer_pointer); |
77 | remaining_frames = runtime->buffer_size - pcm_buffer_pointer; | |
6fb7db90 TS |
78 | |
79 | for (i = 0; i < frames; ++i) { | |
80 | for (c = 0; c < channels; ++c) { | |
81 | *dst = le32_to_cpu(buffer[c]) & 0xffffff00; | |
82 | dst++; | |
83 | } | |
84 | buffer += s->data_block_quadlets; | |
85 | if (--remaining_frames == 0) | |
86 | dst = (void *)runtime->dma_area; | |
87 | } | |
88 | } | |
89 | ||
90 | static void write_pcm_silence(struct amdtp_stream *s, | |
91 | __le32 *buffer, unsigned int frames) | |
92 | { | |
93 | struct amdtp_ff *p = s->protocol; | |
94 | unsigned int i, c, channels = p->pcm_channels; | |
95 | ||
96 | for (i = 0; i < frames; ++i) { | |
97 | for (c = 0; c < channels; ++c) | |
98 | buffer[c] = cpu_to_le32(0x00000000); | |
99 | buffer += s->data_block_quadlets; | |
100 | } | |
101 | } | |
102 | ||
103 | int amdtp_ff_add_pcm_hw_constraints(struct amdtp_stream *s, | |
104 | struct snd_pcm_runtime *runtime) | |
105 | { | |
106 | int err; | |
107 | ||
108 | err = snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24); | |
109 | if (err < 0) | |
110 | return err; | |
111 | ||
112 | return amdtp_stream_add_pcm_hw_constraints(s, runtime); | |
113 | } | |
114 | ||
7fc693e4 TS |
115 | static void process_it_ctx_payloads(struct amdtp_stream *s, const struct pkt_desc *desc, |
116 | unsigned int count, struct snd_pcm_substream *pcm) | |
6fb7db90 | 117 | { |
4731c672 | 118 | unsigned int pcm_frames = 0; |
9a738ad1 TS |
119 | int i; |
120 | ||
0cac60c7 | 121 | for (i = 0; i < count; ++i) { |
9a738ad1 TS |
122 | __le32 *buf = (__le32 *)desc->ctx_payload; |
123 | unsigned int data_blocks = desc->data_blocks; | |
124 | ||
125 | if (pcm) { | |
126 | write_pcm_s32(s, pcm, buf, data_blocks, pcm_frames); | |
127 | pcm_frames += data_blocks; | |
128 | } else { | |
129 | write_pcm_silence(s, buf, data_blocks); | |
130 | } | |
0cac60c7 TS |
131 | |
132 | desc = amdtp_stream_next_packet_desc(s, desc); | |
6fb7db90 | 133 | } |
6fb7db90 TS |
134 | } |
135 | ||
7fc693e4 TS |
136 | static void process_ir_ctx_payloads(struct amdtp_stream *s, const struct pkt_desc *desc, |
137 | unsigned int count, struct snd_pcm_substream *pcm) | |
6fb7db90 | 138 | { |
4731c672 | 139 | unsigned int pcm_frames = 0; |
9a738ad1 | 140 | int i; |
6fb7db90 | 141 | |
0cac60c7 | 142 | for (i = 0; i < count; ++i) { |
9a738ad1 TS |
143 | __le32 *buf = (__le32 *)desc->ctx_payload; |
144 | unsigned int data_blocks = desc->data_blocks; | |
145 | ||
146 | if (pcm) { | |
147 | read_pcm_s32(s, pcm, buf, data_blocks, pcm_frames); | |
148 | pcm_frames += data_blocks; | |
149 | } | |
0cac60c7 TS |
150 | |
151 | desc = amdtp_stream_next_packet_desc(s, desc); | |
6fb7db90 | 152 | } |
6fb7db90 TS |
153 | } |
154 | ||
155 | int amdtp_ff_init(struct amdtp_stream *s, struct fw_unit *unit, | |
156 | enum amdtp_stream_direction dir) | |
157 | { | |
9a738ad1 | 158 | amdtp_stream_process_ctx_payloads_t process_ctx_payloads; |
6fb7db90 TS |
159 | |
160 | if (dir == AMDTP_IN_STREAM) | |
9a738ad1 | 161 | process_ctx_payloads = process_ir_ctx_payloads; |
6fb7db90 | 162 | else |
9a738ad1 | 163 | process_ctx_payloads = process_it_ctx_payloads; |
6fb7db90 | 164 | |
dfacca39 | 165 | return amdtp_stream_init(s, unit, dir, CIP_BLOCKING | CIP_UNAWARE_SYT | CIP_NO_HEADER, 0, |
9a738ad1 | 166 | process_ctx_payloads, sizeof(struct amdtp_ff)); |
6fb7db90 | 167 | } |