]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - sound/firewire/oxfw/oxfw-pcm.c
ALSA: firewire-lib: replace ack callback to flush isoc contexts in AMDTP domain
[mirror_ubuntu-jammy-kernel.git] / sound / firewire / oxfw / oxfw-pcm.c
CommitLineData
da607e19 1// SPDX-License-Identifier: GPL-2.0-only
3713d93a
TS
2/*
3 * oxfw_pcm.c - a part of driver for OXFW970/971 based devices
4 *
5 * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
3713d93a
TS
6 */
7
8#include "oxfw.h"
9
5cd1d3f4
TS
10static int hw_rule_rate(struct snd_pcm_hw_params *params,
11 struct snd_pcm_hw_rule *rule)
3713d93a 12{
5cd1d3f4
TS
13 u8 **formats = 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 };
21 struct snd_oxfw_stream_formation formation;
5580ba7b 22 int i, err;
5cd1d3f4
TS
23
24 for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
25 if (formats[i] == NULL)
26 continue;
27
28 err = snd_oxfw_stream_parse_format(formats[i], &formation);
29 if (err < 0)
30 continue;
31 if (!snd_interval_test(c, formation.pcm))
32 continue;
33
34 t.min = min(t.min, formation.rate);
35 t.max = max(t.max, formation.rate);
36
37 }
38 return snd_interval_refine(r, &t);
3713d93a
TS
39}
40
5cd1d3f4
TS
41static int hw_rule_channels(struct snd_pcm_hw_params *params,
42 struct snd_pcm_hw_rule *rule)
3713d93a 43{
5cd1d3f4
TS
44 u8 **formats = 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_oxfw_stream_formation formation;
5580ba7b 50 int i, j, err;
5cd1d3f4
TS
51 unsigned int count, list[SND_OXFW_STREAM_FORMAT_ENTRIES] = {0};
52
53 count = 0;
54 for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
55 if (formats[i] == NULL)
56 break;
57
58 err = snd_oxfw_stream_parse_format(formats[i], &formation);
59 if (err < 0)
60 continue;
61 if (!snd_interval_test(r, formation.rate))
62 continue;
63 if (list[count] == formation.pcm)
64 continue;
65
66 for (j = 0; j < ARRAY_SIZE(list); j++) {
67 if (list[j] == formation.pcm)
68 break;
69 }
70 if (j == ARRAY_SIZE(list)) {
71 list[count] = formation.pcm;
72 if (++count == ARRAY_SIZE(list))
73 break;
74 }
75 }
76
77 return snd_interval_list(c, count, list, 0);
3713d93a
TS
78}
79
5cd1d3f4 80static void limit_channels_and_rates(struct snd_pcm_hardware *hw, u8 **formats)
3713d93a 81{
5cd1d3f4 82 struct snd_oxfw_stream_formation formation;
5580ba7b 83 int i, err;
3713d93a 84
5cd1d3f4
TS
85 hw->channels_min = UINT_MAX;
86 hw->channels_max = 0;
3713d93a 87
5cd1d3f4
TS
88 hw->rate_min = UINT_MAX;
89 hw->rate_max = 0;
90 hw->rates = 0;
3713d93a 91
5cd1d3f4
TS
92 for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
93 if (formats[i] == NULL)
94 break;
95
96 err = snd_oxfw_stream_parse_format(formats[i], &formation);
97 if (err < 0)
98 continue;
99
100 hw->channels_min = min(hw->channels_min, formation.pcm);
101 hw->channels_max = max(hw->channels_max, formation.pcm);
102
103 hw->rate_min = min(hw->rate_min, formation.rate);
104 hw->rate_max = max(hw->rate_max, formation.rate);
105 hw->rates |= snd_pcm_rate_to_rate_bit(formation.rate);
106 }
3713d93a
TS
107}
108
216e256f
TS
109static int init_hw_params(struct snd_oxfw *oxfw,
110 struct snd_pcm_substream *substream)
3713d93a 111{
3713d93a 112 struct snd_pcm_runtime *runtime = substream->runtime;
5cd1d3f4 113 u8 **formats;
216e256f 114 struct amdtp_stream *stream;
3713d93a
TS
115 int err;
116
216e256f 117 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
49c7b3fc 118 runtime->hw.formats = AM824_IN_PCM_FORMAT_BITS;
216e256f
TS
119 stream = &oxfw->tx_stream;
120 formats = oxfw->tx_stream_formats;
121 } else {
49c7b3fc 122 runtime->hw.formats = AM824_OUT_PCM_FORMAT_BITS;
216e256f
TS
123 stream = &oxfw->rx_stream;
124 formats = oxfw->rx_stream_formats;
125 }
126
5cd1d3f4 127 limit_channels_and_rates(&runtime->hw, formats);
3713d93a 128
5cd1d3f4
TS
129 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
130 hw_rule_channels, formats,
131 SNDRV_PCM_HW_PARAM_RATE, -1);
3713d93a
TS
132 if (err < 0)
133 goto end;
5cd1d3f4
TS
134
135 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
136 hw_rule_rate, formats,
137 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
3713d93a
TS
138 if (err < 0)
139 goto end;
140
bc8500da 141 err = amdtp_am824_add_pcm_hw_constraints(stream, runtime);
216e256f
TS
142end:
143 return err;
144}
145
146static int limit_to_current_params(struct snd_pcm_substream *substream)
147{
148 struct snd_oxfw *oxfw = substream->private_data;
149 struct snd_oxfw_stream_formation formation;
150 enum avc_general_plug_dir dir;
151 int err;
152
153 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
154 dir = AVC_GENERAL_PLUG_DIR_OUT;
155 else
156 dir = AVC_GENERAL_PLUG_DIR_IN;
157
158 err = snd_oxfw_stream_get_current_formation(oxfw, dir, &formation);
159 if (err < 0)
160 goto end;
161
162 substream->runtime->hw.channels_min = formation.pcm;
163 substream->runtime->hw.channels_max = formation.pcm;
164 substream->runtime->hw.rate_min = formation.rate;
165 substream->runtime->hw.rate_max = formation.rate;
166end:
167 return err;
168}
169
170static int pcm_open(struct snd_pcm_substream *substream)
171{
172 struct snd_oxfw *oxfw = substream->private_data;
c0ede398 173 struct amdtp_domain *d = &oxfw->domain;
216e256f
TS
174 int err;
175
8985f4ac 176 err = snd_oxfw_stream_lock_try(oxfw);
5cd1d3f4 177 if (err < 0)
c0ede398 178 return err;
5cd1d3f4 179
8985f4ac
TS
180 err = init_hw_params(oxfw, substream);
181 if (err < 0)
182 goto err_locked;
183
c0ede398
TS
184 mutex_lock(&oxfw->mutex);
185
186 // When source of clock is not internal or any stream is reserved for
187 // transmission of PCM frames, the available sampling rate is limited
188 // at current one.
189 if (oxfw->substreams_count > 0 && d->events_per_period > 0) {
190 unsigned int frames_per_period = d->events_per_period;
3299d2a0 191 unsigned int frames_per_buffer = d->events_per_buffer;
c0ede398 192
216e256f 193 err = limit_to_current_params(substream);
c0ede398
TS
194 if (err < 0) {
195 mutex_unlock(&oxfw->mutex);
196 goto err_locked;
197 }
198
199 if (frames_per_period > 0) {
200 err = snd_pcm_hw_constraint_minmax(substream->runtime,
201 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
202 frames_per_period, frames_per_period);
203 if (err < 0) {
204 mutex_unlock(&oxfw->mutex);
205 goto err_locked;
206 }
3299d2a0
TS
207
208 err = snd_pcm_hw_constraint_minmax(substream->runtime,
209 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
210 frames_per_buffer, frames_per_buffer);
211 if (err < 0) {
212 mutex_unlock(&oxfw->mutex);
213 goto err_locked;
214 }
c0ede398 215 }
216e256f
TS
216 }
217
c0ede398
TS
218 mutex_unlock(&oxfw->mutex);
219
5cd1d3f4 220 snd_pcm_set_sync(substream);
c0ede398
TS
221
222 return 0;
8985f4ac
TS
223err_locked:
224 snd_oxfw_stream_lock_release(oxfw);
225 return err;
3713d93a
TS
226}
227
228static int pcm_close(struct snd_pcm_substream *substream)
229{
8985f4ac
TS
230 struct snd_oxfw *oxfw = substream->private_data;
231
232 snd_oxfw_stream_lock_release(oxfw);
3713d93a
TS
233 return 0;
234}
235
216e256f
TS
236static int pcm_capture_hw_params(struct snd_pcm_substream *substream,
237 struct snd_pcm_hw_params *hw_params)
3713d93a
TS
238{
239 struct snd_oxfw *oxfw = substream->private_data;
22c103cd 240 int err;
3713d93a 241
22c103cd
TS
242 err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
243 params_buffer_bytes(hw_params));
244 if (err < 0)
245 return err;
216e256f
TS
246
247 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
4f380d00
TS
248 unsigned int rate = params_rate(hw_params);
249 unsigned int channels = params_channels(hw_params);
1d6a722c 250 unsigned int frames_per_period = params_period_size(hw_params);
3299d2a0 251 unsigned int frames_per_buffer = params_buffer_size(hw_params);
4f380d00 252
216e256f 253 mutex_lock(&oxfw->mutex);
4f380d00 254 err = snd_oxfw_stream_reserve_duplex(oxfw, &oxfw->tx_stream,
3299d2a0
TS
255 rate, channels, frames_per_period,
256 frames_per_buffer);
4f380d00
TS
257 if (err >= 0)
258 ++oxfw->substreams_count;
216e256f
TS
259 mutex_unlock(&oxfw->mutex);
260 }
261
4f380d00 262 return err;
216e256f
TS
263}
264static int pcm_playback_hw_params(struct snd_pcm_substream *substream,
265 struct snd_pcm_hw_params *hw_params)
266{
267 struct snd_oxfw *oxfw = substream->private_data;
22c103cd
TS
268 int err;
269
270 err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
271 params_buffer_bytes(hw_params));
272 if (err < 0)
273 return err;
216e256f
TS
274
275 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
4f380d00
TS
276 unsigned int rate = params_rate(hw_params);
277 unsigned int channels = params_channels(hw_params);
1d6a722c 278 unsigned int frames_per_period = params_period_size(hw_params);
3299d2a0 279 unsigned int frames_per_buffer = params_buffer_size(hw_params);
4f380d00 280
216e256f 281 mutex_lock(&oxfw->mutex);
2fd23293 282 err = snd_oxfw_stream_reserve_duplex(oxfw, &oxfw->rx_stream,
3299d2a0
TS
283 rate, channels, frames_per_period,
284 frames_per_buffer);
4f380d00
TS
285 if (err >= 0)
286 ++oxfw->substreams_count;
216e256f
TS
287 mutex_unlock(&oxfw->mutex);
288 }
289
22c103cd 290 return 0;
3713d93a
TS
291}
292
216e256f
TS
293static int pcm_capture_hw_free(struct snd_pcm_substream *substream)
294{
295 struct snd_oxfw *oxfw = substream->private_data;
296
297 mutex_lock(&oxfw->mutex);
298
299 if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
4a0a0472 300 --oxfw->substreams_count;
216e256f 301
779f0dba 302 snd_oxfw_stream_stop_duplex(oxfw);
216e256f
TS
303
304 mutex_unlock(&oxfw->mutex);
305
306 return snd_pcm_lib_free_vmalloc_buffer(substream);
307}
308static int pcm_playback_hw_free(struct snd_pcm_substream *substream)
3713d93a
TS
309{
310 struct snd_oxfw *oxfw = substream->private_data;
311
312 mutex_lock(&oxfw->mutex);
216e256f
TS
313
314 if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
4a0a0472 315 --oxfw->substreams_count;
216e256f 316
779f0dba 317 snd_oxfw_stream_stop_duplex(oxfw);
216e256f 318
3713d93a
TS
319 mutex_unlock(&oxfw->mutex);
320
321 return snd_pcm_lib_free_vmalloc_buffer(substream);
322}
323
216e256f
TS
324static int pcm_capture_prepare(struct snd_pcm_substream *substream)
325{
326 struct snd_oxfw *oxfw = substream->private_data;
216e256f
TS
327 int err;
328
329 mutex_lock(&oxfw->mutex);
4f380d00 330 err = snd_oxfw_stream_start_duplex(oxfw);
216e256f
TS
331 mutex_unlock(&oxfw->mutex);
332 if (err < 0)
333 goto end;
334
335 amdtp_stream_pcm_prepare(&oxfw->tx_stream);
336end:
337 return err;
338}
339static int pcm_playback_prepare(struct snd_pcm_substream *substream)
3713d93a
TS
340{
341 struct snd_oxfw *oxfw = substream->private_data;
342 int err;
343
344 mutex_lock(&oxfw->mutex);
4f380d00 345 err = snd_oxfw_stream_start_duplex(oxfw);
f3699e2c 346 mutex_unlock(&oxfw->mutex);
3713d93a
TS
347 if (err < 0)
348 goto end;
349
350 amdtp_stream_pcm_prepare(&oxfw->rx_stream);
351end:
3713d93a
TS
352 return err;
353}
354
216e256f
TS
355static int pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
356{
357 struct snd_oxfw *oxfw = substream->private_data;
358 struct snd_pcm_substream *pcm;
359
360 switch (cmd) {
361 case SNDRV_PCM_TRIGGER_START:
362 pcm = substream;
363 break;
364 case SNDRV_PCM_TRIGGER_STOP:
365 pcm = NULL;
366 break;
367 default:
368 return -EINVAL;
369 }
370 amdtp_stream_pcm_trigger(&oxfw->tx_stream, pcm);
371 return 0;
372}
373static int pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
3713d93a
TS
374{
375 struct snd_oxfw *oxfw = substream->private_data;
376 struct snd_pcm_substream *pcm;
377
378 switch (cmd) {
379 case SNDRV_PCM_TRIGGER_START:
380 pcm = substream;
381 break;
382 case SNDRV_PCM_TRIGGER_STOP:
383 pcm = NULL;
384 break;
385 default:
386 return -EINVAL;
387 }
388 amdtp_stream_pcm_trigger(&oxfw->rx_stream, pcm);
389 return 0;
390}
391
216e256f 392static snd_pcm_uframes_t pcm_capture_pointer(struct snd_pcm_substream *sbstm)
3713d93a 393{
216e256f
TS
394 struct snd_oxfw *oxfw = sbstm->private_data;
395
f890f9a0 396 return amdtp_domain_stream_pcm_pointer(&oxfw->domain, &oxfw->tx_stream);
216e256f
TS
397}
398static snd_pcm_uframes_t pcm_playback_pointer(struct snd_pcm_substream *sbstm)
399{
400 struct snd_oxfw *oxfw = sbstm->private_data;
3713d93a 401
f890f9a0 402 return amdtp_domain_stream_pcm_pointer(&oxfw->domain, &oxfw->rx_stream);
3713d93a
TS
403}
404
875becf8
TS
405static int pcm_capture_ack(struct snd_pcm_substream *substream)
406{
407 struct snd_oxfw *oxfw = substream->private_data;
408
e6dcc92f 409 return amdtp_domain_stream_pcm_ack(&oxfw->domain, &oxfw->tx_stream);
875becf8
TS
410}
411
412static int pcm_playback_ack(struct snd_pcm_substream *substream)
413{
414 struct snd_oxfw *oxfw = substream->private_data;
415
e6dcc92f 416 return amdtp_domain_stream_pcm_ack(&oxfw->domain, &oxfw->rx_stream);
875becf8
TS
417}
418
3713d93a
TS
419int snd_oxfw_create_pcm(struct snd_oxfw *oxfw)
420{
5116ffc3 421 static const struct snd_pcm_ops capture_ops = {
216e256f
TS
422 .open = pcm_open,
423 .close = pcm_close,
424 .ioctl = snd_pcm_lib_ioctl,
425 .hw_params = pcm_capture_hw_params,
426 .hw_free = pcm_capture_hw_free,
427 .prepare = pcm_capture_prepare,
428 .trigger = pcm_capture_trigger,
429 .pointer = pcm_capture_pointer,
875becf8 430 .ack = pcm_capture_ack,
216e256f 431 .page = snd_pcm_lib_get_vmalloc_page,
216e256f 432 };
5116ffc3 433 static const struct snd_pcm_ops playback_ops = {
3713d93a
TS
434 .open = pcm_open,
435 .close = pcm_close,
436 .ioctl = snd_pcm_lib_ioctl,
216e256f
TS
437 .hw_params = pcm_playback_hw_params,
438 .hw_free = pcm_playback_hw_free,
439 .prepare = pcm_playback_prepare,
440 .trigger = pcm_playback_trigger,
441 .pointer = pcm_playback_pointer,
875becf8 442 .ack = pcm_playback_ack,
3713d93a 443 .page = snd_pcm_lib_get_vmalloc_page,
3713d93a
TS
444 };
445 struct snd_pcm *pcm;
216e256f 446 unsigned int cap = 0;
3713d93a
TS
447 int err;
448
216e256f
TS
449 if (oxfw->has_output)
450 cap = 1;
451
452 err = snd_pcm_new(oxfw->card, oxfw->card->driver, 0, 1, cap, &pcm);
3713d93a
TS
453 if (err < 0)
454 return err;
216e256f 455
3713d93a
TS
456 pcm->private_data = oxfw;
457 strcpy(pcm->name, oxfw->card->shortname);
216e256f
TS
458 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &playback_ops);
459 if (cap > 0)
460 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &capture_ops);
461
3713d93a
TS
462 return 0;
463}