ALSA: firewire-motu: add wrapper functions for protocol-dependent operations
[linux-2.6-microblaze.git] / sound / firewire / motu / motu-pcm.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * motu-pcm.c - a part of driver for MOTU FireWire series
4  *
5  * Copyright (c) 2015-2017 Takashi Sakamoto <o-takashi@sakamocchi.jp>
6  */
7
8 #include <sound/pcm_params.h>
9 #include "motu.h"
10
11 static int motu_rate_constraint(struct snd_pcm_hw_params *params,
12                                 struct snd_pcm_hw_rule *rule)
13 {
14         struct snd_motu_packet_format *formats = rule->private;
15
16         const struct snd_interval *c =
17                 hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS);
18         struct snd_interval *r =
19                 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
20         struct snd_interval rates = {
21                 .min = UINT_MAX, .max = 0, .integer = 1
22         };
23         unsigned int i, pcm_channels, rate, mode;
24
25         for (i = 0; i < ARRAY_SIZE(snd_motu_clock_rates); ++i) {
26                 rate = snd_motu_clock_rates[i];
27                 mode = i / 2;
28
29                 pcm_channels = formats->fixed_part_pcm_chunks[mode] +
30                                formats->differed_part_pcm_chunks[mode];
31                 if (!snd_interval_test(c, pcm_channels))
32                         continue;
33
34                 rates.min = min(rates.min, rate);
35                 rates.max = max(rates.max, rate);
36         }
37
38         return snd_interval_refine(r, &rates);
39 }
40
41 static int motu_channels_constraint(struct snd_pcm_hw_params *params,
42                                     struct snd_pcm_hw_rule *rule)
43 {
44         struct snd_motu_packet_format *formats = rule->private;
45
46         const struct snd_interval *r =
47                 hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
48         struct snd_interval *c =
49                 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
50         struct snd_interval channels = {
51                 .min = UINT_MAX, .max = 0, .integer = 1
52         };
53         unsigned int i, pcm_channels, rate, mode;
54
55         for (i = 0; i < ARRAY_SIZE(snd_motu_clock_rates); ++i) {
56                 rate = snd_motu_clock_rates[i];
57                 mode = i / 2;
58
59                 if (!snd_interval_test(r, rate))
60                         continue;
61
62                 pcm_channels = formats->fixed_part_pcm_chunks[mode] +
63                                formats->differed_part_pcm_chunks[mode];
64                 channels.min = min(channels.min, pcm_channels);
65                 channels.max = max(channels.max, pcm_channels);
66         }
67
68         return snd_interval_refine(c, &channels);
69 }
70
71 static void limit_channels_and_rates(struct snd_motu *motu,
72                                      struct snd_pcm_runtime *runtime,
73                                      struct snd_motu_packet_format *formats)
74 {
75         struct snd_pcm_hardware *hw = &runtime->hw;
76         unsigned int i, pcm_channels, rate, mode;
77
78         hw->channels_min = UINT_MAX;
79         hw->channels_max = 0;
80
81         for (i = 0; i < ARRAY_SIZE(snd_motu_clock_rates); ++i) {
82                 rate = snd_motu_clock_rates[i];
83                 mode = i / 2;
84
85                 pcm_channels = formats->fixed_part_pcm_chunks[mode] +
86                                formats->differed_part_pcm_chunks[mode];
87                 if (pcm_channels == 0)
88                         continue;
89
90                 hw->rates |= snd_pcm_rate_to_rate_bit(rate);
91                 hw->channels_min = min(hw->channels_min, pcm_channels);
92                 hw->channels_max = max(hw->channels_max, pcm_channels);
93         }
94
95         snd_pcm_limit_hw_rates(runtime);
96 }
97
98 static int init_hw_info(struct snd_motu *motu,
99                         struct snd_pcm_substream *substream)
100 {
101         struct snd_pcm_runtime *runtime = substream->runtime;
102         struct snd_pcm_hardware *hw = &runtime->hw;
103         struct amdtp_stream *stream;
104         struct snd_motu_packet_format *formats;
105         int err;
106
107         if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
108                 hw->formats = SNDRV_PCM_FMTBIT_S32;
109                 stream = &motu->tx_stream;
110                 formats = &motu->tx_packet_formats;
111         } else {
112                 hw->formats = SNDRV_PCM_FMTBIT_S32;
113                 stream = &motu->rx_stream;
114                 formats = &motu->rx_packet_formats;
115         }
116
117         limit_channels_and_rates(motu, runtime, formats);
118
119         err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
120                                   motu_rate_constraint, formats,
121                                   SNDRV_PCM_HW_PARAM_CHANNELS, -1);
122         if (err < 0)
123                 return err;
124         err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
125                                   motu_channels_constraint, formats,
126                                   SNDRV_PCM_HW_PARAM_RATE, -1);
127         if (err < 0)
128                 return err;
129
130         return amdtp_motu_add_pcm_hw_constraints(stream, runtime);
131 }
132
133 static int pcm_open(struct snd_pcm_substream *substream)
134 {
135         struct snd_motu *motu = substream->private_data;
136         struct amdtp_domain *d = &motu->domain;
137         enum snd_motu_clock_source src;
138         int err;
139
140         err = snd_motu_stream_lock_try(motu);
141         if (err < 0)
142                 return err;
143
144         mutex_lock(&motu->mutex);
145
146         err = snd_motu_stream_cache_packet_formats(motu);
147         if (err < 0)
148                 goto err_locked;
149
150         err = init_hw_info(motu, substream);
151         if (err < 0)
152                 goto err_locked;
153
154         err = snd_motu_protocol_get_clock_source(motu, &src);
155         if (err < 0)
156                 goto err_locked;
157
158         // When source of clock is not internal or any stream is reserved for
159         // transmission of PCM frames, the available sampling rate is limited
160         // at current one.
161         if ((src != SND_MOTU_CLOCK_SOURCE_INTERNAL &&
162              src != SND_MOTU_CLOCK_SOURCE_SPH) ||
163             (motu->substreams_counter > 0 && d->events_per_period > 0)) {
164                 unsigned int frames_per_period = d->events_per_period;
165                 unsigned int frames_per_buffer = d->events_per_buffer;
166                 unsigned int rate;
167
168                 err = snd_motu_protocol_get_clock_rate(motu, &rate);
169                 if (err < 0)
170                         goto err_locked;
171
172                 substream->runtime->hw.rate_min = rate;
173                 substream->runtime->hw.rate_max = rate;
174
175                 if (frames_per_period > 0) {
176                         err = snd_pcm_hw_constraint_minmax(substream->runtime,
177                                         SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
178                                         frames_per_period, frames_per_period);
179                         if (err < 0)
180                                 goto err_locked;
181
182                         err = snd_pcm_hw_constraint_minmax(substream->runtime,
183                                         SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
184                                         frames_per_buffer, frames_per_buffer);
185                         if (err < 0)
186                                 goto err_locked;
187                 }
188         }
189
190         snd_pcm_set_sync(substream);
191
192         mutex_unlock(&motu->mutex);
193
194         return 0;
195 err_locked:
196         mutex_unlock(&motu->mutex);
197         snd_motu_stream_lock_release(motu);
198         return err;
199 }
200
201 static int pcm_close(struct snd_pcm_substream *substream)
202 {
203         struct snd_motu *motu = substream->private_data;
204
205         snd_motu_stream_lock_release(motu);
206
207         return 0;
208 }
209
210 static int pcm_hw_params(struct snd_pcm_substream *substream,
211                          struct snd_pcm_hw_params *hw_params)
212 {
213         struct snd_motu *motu = substream->private_data;
214         int err = 0;
215
216         if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
217                 unsigned int rate = params_rate(hw_params);
218                 unsigned int frames_per_period = params_period_size(hw_params);
219                 unsigned int frames_per_buffer = params_buffer_size(hw_params);
220
221                 mutex_lock(&motu->mutex);
222                 err = snd_motu_stream_reserve_duplex(motu, rate,
223                                         frames_per_period, frames_per_buffer);
224                 if (err >= 0)
225                         ++motu->substreams_counter;
226                 mutex_unlock(&motu->mutex);
227         }
228
229         return err;
230 }
231
232 static int pcm_hw_free(struct snd_pcm_substream *substream)
233 {
234         struct snd_motu *motu = substream->private_data;
235
236         mutex_lock(&motu->mutex);
237
238         if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
239                 --motu->substreams_counter;
240
241         snd_motu_stream_stop_duplex(motu);
242
243         mutex_unlock(&motu->mutex);
244
245         return 0;
246 }
247
248 static int capture_prepare(struct snd_pcm_substream *substream)
249 {
250         struct snd_motu *motu = substream->private_data;
251         int err;
252
253         mutex_lock(&motu->mutex);
254         err = snd_motu_stream_start_duplex(motu);
255         mutex_unlock(&motu->mutex);
256         if (err >= 0)
257                 amdtp_stream_pcm_prepare(&motu->tx_stream);
258
259         return 0;
260 }
261 static int playback_prepare(struct snd_pcm_substream *substream)
262 {
263         struct snd_motu *motu = substream->private_data;
264         int err;
265
266         mutex_lock(&motu->mutex);
267         err = snd_motu_stream_start_duplex(motu);
268         mutex_unlock(&motu->mutex);
269         if (err >= 0)
270                 amdtp_stream_pcm_prepare(&motu->rx_stream);
271
272         return err;
273 }
274
275 static int capture_trigger(struct snd_pcm_substream *substream, int cmd)
276 {
277         struct snd_motu *motu = substream->private_data;
278
279         switch (cmd) {
280         case SNDRV_PCM_TRIGGER_START:
281                 amdtp_stream_pcm_trigger(&motu->tx_stream, substream);
282                 break;
283         case SNDRV_PCM_TRIGGER_STOP:
284                 amdtp_stream_pcm_trigger(&motu->tx_stream, NULL);
285                 break;
286         default:
287                 return -EINVAL;
288         }
289
290         return 0;
291 }
292 static int playback_trigger(struct snd_pcm_substream *substream, int cmd)
293 {
294         struct snd_motu *motu = substream->private_data;
295
296         switch (cmd) {
297         case SNDRV_PCM_TRIGGER_START:
298                 amdtp_stream_pcm_trigger(&motu->rx_stream, substream);
299                 break;
300         case SNDRV_PCM_TRIGGER_STOP:
301                 amdtp_stream_pcm_trigger(&motu->rx_stream, NULL);
302                 break;
303         default:
304                 return -EINVAL;
305         }
306
307         return 0;
308 }
309
310 static snd_pcm_uframes_t capture_pointer(struct snd_pcm_substream *substream)
311 {
312         struct snd_motu *motu = substream->private_data;
313
314         return amdtp_domain_stream_pcm_pointer(&motu->domain, &motu->tx_stream);
315 }
316 static snd_pcm_uframes_t playback_pointer(struct snd_pcm_substream *substream)
317 {
318         struct snd_motu *motu = substream->private_data;
319
320         return amdtp_domain_stream_pcm_pointer(&motu->domain, &motu->rx_stream);
321 }
322
323 static int capture_ack(struct snd_pcm_substream *substream)
324 {
325         struct snd_motu *motu = substream->private_data;
326
327         return amdtp_domain_stream_pcm_ack(&motu->domain, &motu->tx_stream);
328 }
329
330 static int playback_ack(struct snd_pcm_substream *substream)
331 {
332         struct snd_motu *motu = substream->private_data;
333
334         return amdtp_domain_stream_pcm_ack(&motu->domain, &motu->rx_stream);
335 }
336
337 int snd_motu_create_pcm_devices(struct snd_motu *motu)
338 {
339         static const struct snd_pcm_ops capture_ops = {
340                 .open      = pcm_open,
341                 .close     = pcm_close,
342                 .hw_params = pcm_hw_params,
343                 .hw_free   = pcm_hw_free,
344                 .prepare   = capture_prepare,
345                 .trigger   = capture_trigger,
346                 .pointer   = capture_pointer,
347                 .ack       = capture_ack,
348         };
349         static const struct snd_pcm_ops playback_ops = {
350                 .open      = pcm_open,
351                 .close     = pcm_close,
352                 .hw_params = pcm_hw_params,
353                 .hw_free   = pcm_hw_free,
354                 .prepare   = playback_prepare,
355                 .trigger   = playback_trigger,
356                 .pointer   = playback_pointer,
357                 .ack       = playback_ack,
358         };
359         struct snd_pcm *pcm;
360         int err;
361
362         err = snd_pcm_new(motu->card, motu->card->driver, 0, 1, 1, &pcm);
363         if (err < 0)
364                 return err;
365         pcm->private_data = motu;
366         strcpy(pcm->name, motu->card->shortname);
367
368         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &capture_ops);
369         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &playback_ops);
370         snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC, NULL, 0, 0);
371
372         return 0;
373 }