1 // SPDX-License-Identifier: GPL-2.0-only
3 * oxfw_pcm.c - a part of driver for OXFW970/971 based devices
5 * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
10 static int hw_rule_rate(struct snd_pcm_hw_params *params,
11 struct snd_pcm_hw_rule *rule)
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
21 struct snd_oxfw_stream_formation formation;
24 for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
25 if (formats[i] == NULL)
28 err = snd_oxfw_stream_parse_format(formats[i], &formation);
31 if (!snd_interval_test(c, formation.pcm))
34 t.min = min(t.min, formation.rate);
35 t.max = max(t.max, formation.rate);
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 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;
51 unsigned int count, list[SND_OXFW_STREAM_FORMAT_ENTRIES] = {0};
54 for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
55 if (formats[i] == NULL)
58 err = snd_oxfw_stream_parse_format(formats[i], &formation);
61 if (!snd_interval_test(r, formation.rate))
63 if (list[count] == formation.pcm)
66 for (j = 0; j < ARRAY_SIZE(list); j++) {
67 if (list[j] == formation.pcm)
70 if (j == ARRAY_SIZE(list)) {
71 list[count] = formation.pcm;
72 if (++count == ARRAY_SIZE(list))
77 return snd_interval_list(c, count, list, 0);
80 static void limit_channels_and_rates(struct snd_pcm_hardware *hw, u8 **formats)
82 struct snd_oxfw_stream_formation formation;
85 hw->channels_min = UINT_MAX;
88 hw->rate_min = UINT_MAX;
92 for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
93 if (formats[i] == NULL)
96 err = snd_oxfw_stream_parse_format(formats[i], &formation);
100 hw->channels_min = min(hw->channels_min, formation.pcm);
101 hw->channels_max = max(hw->channels_max, formation.pcm);
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);
109 static int init_hw_params(struct snd_oxfw *oxfw,
110 struct snd_pcm_substream *substream)
112 struct snd_pcm_runtime *runtime = substream->runtime;
114 struct amdtp_stream *stream;
117 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
118 runtime->hw.formats = AM824_IN_PCM_FORMAT_BITS;
119 stream = &oxfw->tx_stream;
120 formats = oxfw->tx_stream_formats;
122 runtime->hw.formats = AM824_OUT_PCM_FORMAT_BITS;
123 stream = &oxfw->rx_stream;
124 formats = oxfw->rx_stream_formats;
127 limit_channels_and_rates(&runtime->hw, formats);
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);
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);
141 err = amdtp_am824_add_pcm_hw_constraints(stream, runtime);
146 static int limit_to_current_params(struct snd_pcm_substream *substream)
148 struct snd_oxfw *oxfw = substream->private_data;
149 struct snd_oxfw_stream_formation formation;
150 enum avc_general_plug_dir dir;
153 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
154 dir = AVC_GENERAL_PLUG_DIR_OUT;
156 dir = AVC_GENERAL_PLUG_DIR_IN;
158 err = snd_oxfw_stream_get_current_formation(oxfw, dir, &formation);
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;
170 static int pcm_open(struct snd_pcm_substream *substream)
172 struct snd_oxfw *oxfw = substream->private_data;
173 struct amdtp_domain *d = &oxfw->domain;
176 err = snd_oxfw_stream_lock_try(oxfw);
180 err = init_hw_params(oxfw, substream);
184 mutex_lock(&oxfw->mutex);
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
189 if (oxfw->substreams_count > 0 && d->events_per_period > 0) {
190 unsigned int frames_per_period = d->events_per_period;
191 unsigned int frames_per_buffer = d->events_per_buffer;
193 err = limit_to_current_params(substream);
195 mutex_unlock(&oxfw->mutex);
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);
204 mutex_unlock(&oxfw->mutex);
208 err = snd_pcm_hw_constraint_minmax(substream->runtime,
209 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
210 frames_per_buffer, frames_per_buffer);
212 mutex_unlock(&oxfw->mutex);
218 mutex_unlock(&oxfw->mutex);
220 snd_pcm_set_sync(substream);
224 snd_oxfw_stream_lock_release(oxfw);
228 static int pcm_close(struct snd_pcm_substream *substream)
230 struct snd_oxfw *oxfw = substream->private_data;
232 snd_oxfw_stream_lock_release(oxfw);
236 static int pcm_capture_hw_params(struct snd_pcm_substream *substream,
237 struct snd_pcm_hw_params *hw_params)
239 struct snd_oxfw *oxfw = substream->private_data;
242 err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
243 params_buffer_bytes(hw_params));
247 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
248 unsigned int rate = params_rate(hw_params);
249 unsigned int channels = params_channels(hw_params);
250 unsigned int frames_per_period = params_period_size(hw_params);
251 unsigned int frames_per_buffer = params_buffer_size(hw_params);
253 mutex_lock(&oxfw->mutex);
254 err = snd_oxfw_stream_reserve_duplex(oxfw, &oxfw->tx_stream,
255 rate, channels, frames_per_period,
258 ++oxfw->substreams_count;
259 mutex_unlock(&oxfw->mutex);
264 static int pcm_playback_hw_params(struct snd_pcm_substream *substream,
265 struct snd_pcm_hw_params *hw_params)
267 struct snd_oxfw *oxfw = substream->private_data;
270 err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
271 params_buffer_bytes(hw_params));
275 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
276 unsigned int rate = params_rate(hw_params);
277 unsigned int channels = params_channels(hw_params);
278 unsigned int frames_per_period = params_period_size(hw_params);
279 unsigned int frames_per_buffer = params_buffer_size(hw_params);
281 mutex_lock(&oxfw->mutex);
282 err = snd_oxfw_stream_reserve_duplex(oxfw, &oxfw->rx_stream,
283 rate, channels, frames_per_period,
286 ++oxfw->substreams_count;
287 mutex_unlock(&oxfw->mutex);
293 static int pcm_capture_hw_free(struct snd_pcm_substream *substream)
295 struct snd_oxfw *oxfw = substream->private_data;
297 mutex_lock(&oxfw->mutex);
299 if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
300 --oxfw->substreams_count;
302 snd_oxfw_stream_stop_duplex(oxfw);
304 mutex_unlock(&oxfw->mutex);
306 return snd_pcm_lib_free_vmalloc_buffer(substream);
308 static int pcm_playback_hw_free(struct snd_pcm_substream *substream)
310 struct snd_oxfw *oxfw = substream->private_data;
312 mutex_lock(&oxfw->mutex);
314 if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
315 --oxfw->substreams_count;
317 snd_oxfw_stream_stop_duplex(oxfw);
319 mutex_unlock(&oxfw->mutex);
321 return snd_pcm_lib_free_vmalloc_buffer(substream);
324 static int pcm_capture_prepare(struct snd_pcm_substream *substream)
326 struct snd_oxfw *oxfw = substream->private_data;
329 mutex_lock(&oxfw->mutex);
330 err = snd_oxfw_stream_start_duplex(oxfw);
331 mutex_unlock(&oxfw->mutex);
335 amdtp_stream_pcm_prepare(&oxfw->tx_stream);
339 static int pcm_playback_prepare(struct snd_pcm_substream *substream)
341 struct snd_oxfw *oxfw = substream->private_data;
344 mutex_lock(&oxfw->mutex);
345 err = snd_oxfw_stream_start_duplex(oxfw);
346 mutex_unlock(&oxfw->mutex);
350 amdtp_stream_pcm_prepare(&oxfw->rx_stream);
355 static int pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
357 struct snd_oxfw *oxfw = substream->private_data;
358 struct snd_pcm_substream *pcm;
361 case SNDRV_PCM_TRIGGER_START:
364 case SNDRV_PCM_TRIGGER_STOP:
370 amdtp_stream_pcm_trigger(&oxfw->tx_stream, pcm);
373 static int pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
375 struct snd_oxfw *oxfw = substream->private_data;
376 struct snd_pcm_substream *pcm;
379 case SNDRV_PCM_TRIGGER_START:
382 case SNDRV_PCM_TRIGGER_STOP:
388 amdtp_stream_pcm_trigger(&oxfw->rx_stream, pcm);
392 static snd_pcm_uframes_t pcm_capture_pointer(struct snd_pcm_substream *sbstm)
394 struct snd_oxfw *oxfw = sbstm->private_data;
396 return amdtp_domain_stream_pcm_pointer(&oxfw->domain, &oxfw->tx_stream);
398 static snd_pcm_uframes_t pcm_playback_pointer(struct snd_pcm_substream *sbstm)
400 struct snd_oxfw *oxfw = sbstm->private_data;
402 return amdtp_domain_stream_pcm_pointer(&oxfw->domain, &oxfw->rx_stream);
405 static int pcm_capture_ack(struct snd_pcm_substream *substream)
407 struct snd_oxfw *oxfw = substream->private_data;
409 return amdtp_stream_pcm_ack(&oxfw->tx_stream);
412 static int pcm_playback_ack(struct snd_pcm_substream *substream)
414 struct snd_oxfw *oxfw = substream->private_data;
416 return amdtp_stream_pcm_ack(&oxfw->rx_stream);
419 int snd_oxfw_create_pcm(struct snd_oxfw *oxfw)
421 static const struct snd_pcm_ops capture_ops = {
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,
430 .ack = pcm_capture_ack,
431 .page = snd_pcm_lib_get_vmalloc_page,
433 static const struct snd_pcm_ops playback_ops = {
436 .ioctl = snd_pcm_lib_ioctl,
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,
442 .ack = pcm_playback_ack,
443 .page = snd_pcm_lib_get_vmalloc_page,
446 unsigned int cap = 0;
449 if (oxfw->has_output)
452 err = snd_pcm_new(oxfw->card, oxfw->card->driver, 0, 1, cap, &pcm);
456 pcm->private_data = oxfw;
457 strcpy(pcm->name, oxfw->card->shortname);
458 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &playback_ops);
460 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &capture_ops);