2 * ff-pcm.c - a part of driver for RME Fireface series
4 * Copyright (c) 2015-2017 Takashi Sakamoto
6 * Licensed under the terms of the GNU General Public License, version 2.
11 static inline unsigned int get_multiplier_mode_with_index(unsigned int index)
13 return ((int)index - 1) / 2;
16 static int hw_rule_rate(struct snd_pcm_hw_params *params,
17 struct snd_pcm_hw_rule *rule)
19 const unsigned int *pcm_channels = rule->private;
20 struct snd_interval *r =
21 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
22 const struct snd_interval *c =
23 hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS);
24 struct snd_interval t = {
25 .min = UINT_MAX, .max = 0, .integer = 1
29 for (i = 0; i < ARRAY_SIZE(amdtp_rate_table); i++) {
30 mode = get_multiplier_mode_with_index(i);
31 if (!snd_interval_test(c, pcm_channels[mode]))
34 t.min = min(t.min, amdtp_rate_table[i]);
35 t.max = max(t.max, amdtp_rate_table[i]);
38 return snd_interval_refine(r, &t);
41 static int hw_rule_channels(struct snd_pcm_hw_params *params,
42 struct snd_pcm_hw_rule *rule)
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
54 for (i = 0; i < ARRAY_SIZE(amdtp_rate_table); i++) {
55 mode = get_multiplier_mode_with_index(i);
56 if (!snd_interval_test(r, amdtp_rate_table[i]))
59 t.min = min(t.min, pcm_channels[mode]);
60 t.max = max(t.max, pcm_channels[mode]);
63 return snd_interval_refine(c, &t);
66 static void limit_channels_and_rates(struct snd_pcm_hardware *hw,
67 const unsigned int *pcm_channels)
70 unsigned int rate, channels;
73 hw->channels_min = UINT_MAX;
75 hw->rate_min = UINT_MAX;
78 for (i = 0; i < ARRAY_SIZE(amdtp_rate_table); i++) {
79 mode = get_multiplier_mode_with_index(i);
81 channels = pcm_channels[mode];
82 if (pcm_channels[mode] == 0)
84 hw->channels_min = min(hw->channels_min, channels);
85 hw->channels_max = max(hw->channels_max, channels);
87 rate = amdtp_rate_table[i];
88 hw->rates |= snd_pcm_rate_to_rate_bit(rate);
89 hw->rate_min = min(hw->rate_min, rate);
90 hw->rate_max = max(hw->rate_max, rate);
94 static void limit_period_and_buffer(struct snd_pcm_hardware *hw)
96 hw->periods_min = 2; /* SNDRV_PCM_INFO_BATCH */
97 hw->periods_max = UINT_MAX;
99 hw->period_bytes_min = 4 * hw->channels_max; /* bytes for a frame */
101 /* Just to prevent from allocating much pages. */
102 hw->period_bytes_max = hw->period_bytes_min * 2048;
103 hw->buffer_bytes_max = hw->period_bytes_max * hw->periods_min;
106 static int pcm_init_hw_params(struct snd_ff *ff,
107 struct snd_pcm_substream *substream)
109 struct snd_pcm_runtime *runtime = substream->runtime;
110 struct amdtp_stream *s;
111 const unsigned int *pcm_channels;
114 runtime->hw.info = SNDRV_PCM_INFO_BATCH |
115 SNDRV_PCM_INFO_BLOCK_TRANSFER |
116 SNDRV_PCM_INFO_INTERLEAVED |
117 SNDRV_PCM_INFO_JOINT_DUPLEX |
118 SNDRV_PCM_INFO_MMAP |
119 SNDRV_PCM_INFO_MMAP_VALID;
121 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
122 runtime->hw.formats = SNDRV_PCM_FMTBIT_S32;
124 pcm_channels = ff->spec->pcm_capture_channels;
126 runtime->hw.formats = SNDRV_PCM_FMTBIT_S32;
128 pcm_channels = ff->spec->pcm_playback_channels;
132 limit_channels_and_rates(&runtime->hw, pcm_channels);
133 limit_period_and_buffer(&runtime->hw);
135 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
136 hw_rule_channels, (void *)pcm_channels,
137 SNDRV_PCM_HW_PARAM_RATE, -1);
141 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
142 hw_rule_rate, (void *)pcm_channels,
143 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
147 return amdtp_ff_add_pcm_hw_constraints(s, runtime);
150 static int pcm_open(struct snd_pcm_substream *substream)
152 struct snd_ff *ff = substream->private_data;
154 enum snd_ff_clock_src src;
157 err = snd_ff_stream_lock_try(ff);
161 err = pcm_init_hw_params(ff, substream);
163 snd_ff_stream_lock_release(ff);
167 err = ff->spec->protocol->get_clock(ff, &rate, &src);
169 snd_ff_stream_lock_release(ff);
173 if (src != SND_FF_CLOCK_SRC_INTERNAL) {
174 for (i = 0; i < CIP_SFC_COUNT; ++i) {
175 if (amdtp_rate_table[i] == rate)
179 * The unit is configured at sampling frequency which packet
180 * streaming engine can't support.
182 if (i >= CIP_SFC_COUNT) {
183 snd_ff_stream_lock_release(ff);
187 substream->runtime->hw.rate_min = rate;
188 substream->runtime->hw.rate_max = rate;
190 if (amdtp_stream_pcm_running(&ff->rx_stream) ||
191 amdtp_stream_pcm_running(&ff->tx_stream)) {
192 rate = amdtp_rate_table[ff->rx_stream.sfc];
193 substream->runtime->hw.rate_min = rate;
194 substream->runtime->hw.rate_max = rate;
198 snd_pcm_set_sync(substream);
203 static int pcm_close(struct snd_pcm_substream *substream)
205 struct snd_ff *ff = substream->private_data;
207 snd_ff_stream_lock_release(ff);
212 static int pcm_capture_hw_params(struct snd_pcm_substream *substream,
213 struct snd_pcm_hw_params *hw_params)
215 struct snd_ff *ff = substream->private_data;
218 err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
219 params_buffer_bytes(hw_params));
223 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
224 mutex_lock(&ff->mutex);
225 ff->substreams_counter++;
226 mutex_unlock(&ff->mutex);
232 static int pcm_playback_hw_params(struct snd_pcm_substream *substream,
233 struct snd_pcm_hw_params *hw_params)
235 struct snd_ff *ff = substream->private_data;
238 err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
239 params_buffer_bytes(hw_params));
243 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
244 mutex_lock(&ff->mutex);
245 ff->substreams_counter++;
246 mutex_unlock(&ff->mutex);
252 static int pcm_capture_hw_free(struct snd_pcm_substream *substream)
254 struct snd_ff *ff = substream->private_data;
256 mutex_lock(&ff->mutex);
258 if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
259 ff->substreams_counter--;
261 snd_ff_stream_stop_duplex(ff);
263 mutex_unlock(&ff->mutex);
265 return snd_pcm_lib_free_vmalloc_buffer(substream);
268 static int pcm_playback_hw_free(struct snd_pcm_substream *substream)
270 struct snd_ff *ff = substream->private_data;
272 mutex_lock(&ff->mutex);
274 if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
275 ff->substreams_counter--;
277 snd_ff_stream_stop_duplex(ff);
279 mutex_unlock(&ff->mutex);
281 return snd_pcm_lib_free_vmalloc_buffer(substream);
284 static int pcm_capture_prepare(struct snd_pcm_substream *substream)
286 struct snd_ff *ff = substream->private_data;
287 struct snd_pcm_runtime *runtime = substream->runtime;
290 mutex_lock(&ff->mutex);
292 err = snd_ff_stream_start_duplex(ff, runtime->rate);
294 amdtp_stream_pcm_prepare(&ff->tx_stream);
296 mutex_unlock(&ff->mutex);
301 static int pcm_playback_prepare(struct snd_pcm_substream *substream)
303 struct snd_ff *ff = substream->private_data;
304 struct snd_pcm_runtime *runtime = substream->runtime;
307 mutex_lock(&ff->mutex);
309 err = snd_ff_stream_start_duplex(ff, runtime->rate);
311 amdtp_stream_pcm_prepare(&ff->rx_stream);
313 mutex_unlock(&ff->mutex);
318 static int pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
320 struct snd_ff *ff = substream->private_data;
323 case SNDRV_PCM_TRIGGER_START:
324 amdtp_stream_pcm_trigger(&ff->tx_stream, substream);
326 case SNDRV_PCM_TRIGGER_STOP:
327 amdtp_stream_pcm_trigger(&ff->tx_stream, NULL);
336 static int pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
338 struct snd_ff *ff = substream->private_data;
341 case SNDRV_PCM_TRIGGER_START:
342 amdtp_stream_pcm_trigger(&ff->rx_stream, substream);
344 case SNDRV_PCM_TRIGGER_STOP:
345 amdtp_stream_pcm_trigger(&ff->rx_stream, NULL);
354 static snd_pcm_uframes_t pcm_capture_pointer(struct snd_pcm_substream *sbstrm)
356 struct snd_ff *ff = sbstrm->private_data;
358 return amdtp_stream_pcm_pointer(&ff->tx_stream);
361 static snd_pcm_uframes_t pcm_playback_pointer(struct snd_pcm_substream *sbstrm)
363 struct snd_ff *ff = sbstrm->private_data;
365 return amdtp_stream_pcm_pointer(&ff->rx_stream);
368 static struct snd_pcm_ops pcm_capture_ops = {
371 .ioctl = snd_pcm_lib_ioctl,
372 .hw_params = pcm_capture_hw_params,
373 .hw_free = pcm_capture_hw_free,
374 .prepare = pcm_capture_prepare,
375 .trigger = pcm_capture_trigger,
376 .pointer = pcm_capture_pointer,
377 .page = snd_pcm_lib_get_vmalloc_page,
380 static struct snd_pcm_ops pcm_playback_ops = {
383 .ioctl = snd_pcm_lib_ioctl,
384 .hw_params = pcm_playback_hw_params,
385 .hw_free = pcm_playback_hw_free,
386 .prepare = pcm_playback_prepare,
387 .trigger = pcm_playback_trigger,
388 .pointer = pcm_playback_pointer,
389 .page = snd_pcm_lib_get_vmalloc_page,
390 .mmap = snd_pcm_lib_mmap_vmalloc,
393 int snd_ff_create_pcm_devices(struct snd_ff *ff)
398 err = snd_pcm_new(ff->card, ff->card->driver, 0, 1, 1, &pcm);
402 pcm->private_data = ff;
403 snprintf(pcm->name, sizeof(pcm->name),
404 "%s PCM", ff->card->shortname);
405 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &pcm_playback_ops);
406 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &pcm_capture_ops);