ALSA: fireface: use the same size of period for PCM substreams in AMDTP streams
[linux-2.6-block.git] / sound / firewire / fireface / ff-pcm.c
CommitLineData
da607e19 1// SPDX-License-Identifier: GPL-2.0-only
4b316436
TS
2/*
3 * ff-pcm.c - a part of driver for RME Fireface series
4 *
5 * Copyright (c) 2015-2017 Takashi Sakamoto
4b316436
TS
6 */
7
8#include "ff.h"
9
4b316436
TS
10static int hw_rule_rate(struct snd_pcm_hw_params *params,
11 struct snd_pcm_hw_rule *rule)
12{
13 const unsigned int *pcm_channels = rule->private;
14 struct snd_interval *r =
15 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
16 const struct snd_interval *c =
17 hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS);
18 struct snd_interval t = {
19 .min = UINT_MAX, .max = 0, .integer = 1
20 };
76ea4688 21 unsigned int i;
4b316436
TS
22
23 for (i = 0; i < ARRAY_SIZE(amdtp_rate_table); i++) {
76ea4688
TS
24 enum snd_ff_stream_mode mode;
25 int err;
26
27 err = snd_ff_stream_get_multiplier_mode(i, &mode);
28 if (err < 0)
29 continue;
30
4b316436
TS
31 if (!snd_interval_test(c, pcm_channels[mode]))
32 continue;
33
34 t.min = min(t.min, amdtp_rate_table[i]);
35 t.max = max(t.max, amdtp_rate_table[i]);
36 }
37
38 return snd_interval_refine(r, &t);
39}
40
41static int hw_rule_channels(struct snd_pcm_hw_params *params,
42 struct snd_pcm_hw_rule *rule)
43{
44 const unsigned int *pcm_channels = rule->private;
45 struct snd_interval *c =
46 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
47 const struct snd_interval *r =
48 hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
49 struct snd_interval t = {
50 .min = UINT_MAX, .max = 0, .integer = 1
51 };
76ea4688 52 unsigned int i;
4b316436
TS
53
54 for (i = 0; i < ARRAY_SIZE(amdtp_rate_table); i++) {
76ea4688
TS
55 enum snd_ff_stream_mode mode;
56 int err;
57
58 err = snd_ff_stream_get_multiplier_mode(i, &mode);
59 if (err < 0)
60 continue;
61
4b316436
TS
62 if (!snd_interval_test(r, amdtp_rate_table[i]))
63 continue;
64
65 t.min = min(t.min, pcm_channels[mode]);
66 t.max = max(t.max, pcm_channels[mode]);
67 }
68
69 return snd_interval_refine(c, &t);
70}
71
72static void limit_channels_and_rates(struct snd_pcm_hardware *hw,
73 const unsigned int *pcm_channels)
74{
4b316436
TS
75 unsigned int rate, channels;
76 int i;
77
78 hw->channels_min = UINT_MAX;
79 hw->channels_max = 0;
80 hw->rate_min = UINT_MAX;
81 hw->rate_max = 0;
82
83 for (i = 0; i < ARRAY_SIZE(amdtp_rate_table); i++) {
76ea4688
TS
84 enum snd_ff_stream_mode mode;
85 int err;
86
87 err = snd_ff_stream_get_multiplier_mode(i, &mode);
88 if (err < 0)
89 continue;
4b316436
TS
90
91 channels = pcm_channels[mode];
92 if (pcm_channels[mode] == 0)
93 continue;
94 hw->channels_min = min(hw->channels_min, channels);
95 hw->channels_max = max(hw->channels_max, channels);
96
97 rate = amdtp_rate_table[i];
98 hw->rates |= snd_pcm_rate_to_rate_bit(rate);
99 hw->rate_min = min(hw->rate_min, rate);
100 hw->rate_max = max(hw->rate_max, rate);
101 }
102}
103
4b316436
TS
104static int pcm_init_hw_params(struct snd_ff *ff,
105 struct snd_pcm_substream *substream)
106{
107 struct snd_pcm_runtime *runtime = substream->runtime;
108 struct amdtp_stream *s;
109 const unsigned int *pcm_channels;
110 int err;
111
4b316436
TS
112 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
113 runtime->hw.formats = SNDRV_PCM_FMTBIT_S32;
114 s = &ff->tx_stream;
115 pcm_channels = ff->spec->pcm_capture_channels;
116 } else {
117 runtime->hw.formats = SNDRV_PCM_FMTBIT_S32;
118 s = &ff->rx_stream;
119 pcm_channels = ff->spec->pcm_playback_channels;
120 }
121
4b316436 122 limit_channels_and_rates(&runtime->hw, pcm_channels);
4b316436
TS
123
124 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
125 hw_rule_channels, (void *)pcm_channels,
126 SNDRV_PCM_HW_PARAM_RATE, -1);
127 if (err < 0)
128 return err;
129
130 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
131 hw_rule_rate, (void *)pcm_channels,
132 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
133 if (err < 0)
134 return err;
135
136 return amdtp_ff_add_pcm_hw_constraints(s, runtime);
137}
138
139static int pcm_open(struct snd_pcm_substream *substream)
140{
141 struct snd_ff *ff = substream->private_data;
3aac3263 142 struct amdtp_domain *d = &ff->domain;
4b316436
TS
143 unsigned int rate;
144 enum snd_ff_clock_src src;
145 int i, err;
146
f656edd5 147 err = snd_ff_stream_lock_try(ff);
4b316436
TS
148 if (err < 0)
149 return err;
150
f656edd5 151 err = pcm_init_hw_params(ff, substream);
af43173c
ME
152 if (err < 0)
153 goto release_lock;
f656edd5 154
b1d0cb0a 155 err = ff->spec->protocol->get_clock(ff, &rate, &src);
af43173c
ME
156 if (err < 0)
157 goto release_lock;
4b316436 158
3aac3263
TS
159 mutex_lock(&ff->mutex);
160
161 // When source of clock is not internal or any stream is reserved for
162 // transmission of PCM frames, the available sampling rate is limited
163 // at current one.
4b316436
TS
164 if (src != SND_FF_CLOCK_SRC_INTERNAL) {
165 for (i = 0; i < CIP_SFC_COUNT; ++i) {
166 if (amdtp_rate_table[i] == rate)
167 break;
168 }
3aac3263
TS
169
170 // The unit is configured at sampling frequency which packet
171 // streaming engine can't support.
f656edd5 172 if (i >= CIP_SFC_COUNT) {
3aac3263 173 mutex_unlock(&ff->mutex);
af43173c
ME
174 err = -EIO;
175 goto release_lock;
f656edd5 176 }
4b316436
TS
177
178 substream->runtime->hw.rate_min = rate;
179 substream->runtime->hw.rate_max = rate;
180 } else {
3aac3263
TS
181 if (ff->substreams_counter > 0) {
182 unsigned int frames_per_period = d->events_per_period;
183
4b316436
TS
184 rate = amdtp_rate_table[ff->rx_stream.sfc];
185 substream->runtime->hw.rate_min = rate;
186 substream->runtime->hw.rate_max = rate;
3aac3263
TS
187
188 err = snd_pcm_hw_constraint_minmax(substream->runtime,
189 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
190 frames_per_period, frames_per_period);
191 if (err < 0) {
192 mutex_unlock(&ff->mutex);
193 goto release_lock;
194 }
4b316436
TS
195 }
196 }
197
3aac3263
TS
198 mutex_unlock(&ff->mutex);
199
4b316436
TS
200 snd_pcm_set_sync(substream);
201
202 return 0;
af43173c
ME
203
204release_lock:
205 snd_ff_stream_lock_release(ff);
206 return err;
4b316436
TS
207}
208
209static int pcm_close(struct snd_pcm_substream *substream)
210{
f656edd5
TS
211 struct snd_ff *ff = substream->private_data;
212
213 snd_ff_stream_lock_release(ff);
214
4b316436
TS
215 return 0;
216}
217
49f621fe
TS
218static int pcm_hw_params(struct snd_pcm_substream *substream,
219 struct snd_pcm_hw_params *hw_params)
4b316436
TS
220{
221 struct snd_ff *ff = substream->private_data;
222 int err;
223
224 err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
225 params_buffer_bytes(hw_params));
226 if (err < 0)
227 return err;
228
229 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
55162d2b 230 unsigned int rate = params_rate(hw_params);
9d9ff58c 231 unsigned int frames_per_period = params_period_size(hw_params);
55162d2b 232
4b316436 233 mutex_lock(&ff->mutex);
9d9ff58c 234 err = snd_ff_stream_reserve_duplex(ff, rate, frames_per_period);
55162d2b
TS
235 if (err >= 0)
236 ++ff->substreams_counter;
4b316436
TS
237 mutex_unlock(&ff->mutex);
238 }
239
240 return 0;
241}
242
49f621fe 243static int pcm_hw_free(struct snd_pcm_substream *substream)
4b316436
TS
244{
245 struct snd_ff *ff = substream->private_data;
246
247 mutex_lock(&ff->mutex);
248
249 if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
55162d2b 250 --ff->substreams_counter;
4b316436
TS
251
252 snd_ff_stream_stop_duplex(ff);
253
254 mutex_unlock(&ff->mutex);
255
256 return snd_pcm_lib_free_vmalloc_buffer(substream);
257}
258
259static int pcm_capture_prepare(struct snd_pcm_substream *substream)
260{
261 struct snd_ff *ff = substream->private_data;
262 struct snd_pcm_runtime *runtime = substream->runtime;
263 int err;
264
265 mutex_lock(&ff->mutex);
266
267 err = snd_ff_stream_start_duplex(ff, runtime->rate);
268 if (err >= 0)
269 amdtp_stream_pcm_prepare(&ff->tx_stream);
270
271 mutex_unlock(&ff->mutex);
272
273 return err;
274}
275
276static int pcm_playback_prepare(struct snd_pcm_substream *substream)
277{
278 struct snd_ff *ff = substream->private_data;
279 struct snd_pcm_runtime *runtime = substream->runtime;
280 int err;
281
282 mutex_lock(&ff->mutex);
283
284 err = snd_ff_stream_start_duplex(ff, runtime->rate);
285 if (err >= 0)
286 amdtp_stream_pcm_prepare(&ff->rx_stream);
287
288 mutex_unlock(&ff->mutex);
289
290 return err;
291}
292
293static int pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
294{
295 struct snd_ff *ff = substream->private_data;
296
297 switch (cmd) {
298 case SNDRV_PCM_TRIGGER_START:
299 amdtp_stream_pcm_trigger(&ff->tx_stream, substream);
300 break;
301 case SNDRV_PCM_TRIGGER_STOP:
302 amdtp_stream_pcm_trigger(&ff->tx_stream, NULL);
303 break;
304 default:
305 return -EINVAL;
306 }
307
308 return 0;
309}
310
311static int pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
312{
313 struct snd_ff *ff = substream->private_data;
314
315 switch (cmd) {
316 case SNDRV_PCM_TRIGGER_START:
317 amdtp_stream_pcm_trigger(&ff->rx_stream, substream);
318 break;
319 case SNDRV_PCM_TRIGGER_STOP:
320 amdtp_stream_pcm_trigger(&ff->rx_stream, NULL);
321 break;
322 default:
323 return -EINVAL;
324 }
325
326 return 0;
327}
328
329static snd_pcm_uframes_t pcm_capture_pointer(struct snd_pcm_substream *sbstrm)
330{
331 struct snd_ff *ff = sbstrm->private_data;
332
333 return amdtp_stream_pcm_pointer(&ff->tx_stream);
334}
335
336static snd_pcm_uframes_t pcm_playback_pointer(struct snd_pcm_substream *sbstrm)
337{
338 struct snd_ff *ff = sbstrm->private_data;
339
340 return amdtp_stream_pcm_pointer(&ff->rx_stream);
341}
342
875becf8
TS
343static int pcm_capture_ack(struct snd_pcm_substream *substream)
344{
345 struct snd_ff *ff = substream->private_data;
346
347 return amdtp_stream_pcm_ack(&ff->tx_stream);
348}
349
350static int pcm_playback_ack(struct snd_pcm_substream *substream)
351{
352 struct snd_ff *ff = substream->private_data;
353
354 return amdtp_stream_pcm_ack(&ff->rx_stream);
355}
356
4b316436
TS
357int snd_ff_create_pcm_devices(struct snd_ff *ff)
358{
d2dc2a96
TS
359 static const struct snd_pcm_ops pcm_capture_ops = {
360 .open = pcm_open,
361 .close = pcm_close,
362 .ioctl = snd_pcm_lib_ioctl,
49f621fe
TS
363 .hw_params = pcm_hw_params,
364 .hw_free = pcm_hw_free,
d2dc2a96
TS
365 .prepare = pcm_capture_prepare,
366 .trigger = pcm_capture_trigger,
367 .pointer = pcm_capture_pointer,
368 .ack = pcm_capture_ack,
369 .page = snd_pcm_lib_get_vmalloc_page,
370 };
371 static const struct snd_pcm_ops pcm_playback_ops = {
372 .open = pcm_open,
373 .close = pcm_close,
374 .ioctl = snd_pcm_lib_ioctl,
49f621fe
TS
375 .hw_params = pcm_hw_params,
376 .hw_free = pcm_hw_free,
d2dc2a96
TS
377 .prepare = pcm_playback_prepare,
378 .trigger = pcm_playback_trigger,
379 .pointer = pcm_playback_pointer,
380 .ack = pcm_playback_ack,
381 .page = snd_pcm_lib_get_vmalloc_page,
d2dc2a96 382 };
4b316436
TS
383 struct snd_pcm *pcm;
384 int err;
385
386 err = snd_pcm_new(ff->card, ff->card->driver, 0, 1, 1, &pcm);
387 if (err < 0)
388 return err;
389
390 pcm->private_data = ff;
391 snprintf(pcm->name, sizeof(pcm->name),
392 "%s PCM", ff->card->shortname);
393 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &pcm_playback_ops);
394 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &pcm_capture_ops);
395
396 return 0;
397}