File Index
Files and directories
Sign in
FILE

digi00x-pcm.c

File preview with syntax highlighting.

linux-6.19.14/sound/firewire/digi00x/digi00x-pcm.c C
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * digi00x-pcm.c - a part of driver for Digidesign Digi 002/003 family
  4 *
  5 * Copyright (c) 2014-2015 Takashi Sakamoto
  6 */
  7
  8#include "digi00x.h"
  9
 10static int hw_rule_rate(struct snd_pcm_hw_params *params,
 11			struct snd_pcm_hw_rule *rule)
 12{
 13	struct snd_interval *r =
 14		hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
 15	const struct snd_interval *c =
 16		hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS);
 17	struct snd_interval t = {
 18		.min = UINT_MAX, .max = 0, .integer = 1,
 19	};
 20	unsigned int i;
 21
 22	for (i = 0; i < SND_DG00X_RATE_COUNT; i++) {
 23		if (!snd_interval_test(c,
 24				       snd_dg00x_stream_pcm_channels[i]))
 25			continue;
 26
 27		t.min = min(t.min, snd_dg00x_stream_rates[i]);
 28		t.max = max(t.max, snd_dg00x_stream_rates[i]);
 29	}
 30
 31	return snd_interval_refine(r, &t);
 32}
 33
 34static int hw_rule_channels(struct snd_pcm_hw_params *params,
 35			    struct snd_pcm_hw_rule *rule)
 36{
 37	struct snd_interval *c =
 38		hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
 39	const struct snd_interval *r =
 40		hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
 41	struct snd_interval t = {
 42		.min = UINT_MAX, .max = 0, .integer = 1,
 43	};
 44	unsigned int i;
 45
 46	for (i = 0; i < SND_DG00X_RATE_COUNT; i++) {
 47		if (!snd_interval_test(r, snd_dg00x_stream_rates[i]))
 48			continue;
 49
 50		t.min = min(t.min, snd_dg00x_stream_pcm_channels[i]);
 51		t.max = max(t.max, snd_dg00x_stream_pcm_channels[i]);
 52	}
 53
 54	return snd_interval_refine(c, &t);
 55}
 56
 57static int pcm_init_hw_params(struct snd_dg00x *dg00x,
 58			      struct snd_pcm_substream *substream)
 59{
 60	struct snd_pcm_runtime *runtime = substream->runtime;
 61	struct snd_pcm_hardware *hw = &runtime->hw;
 62	struct amdtp_stream *s;
 63	int err;
 64
 65
 66	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
 67		substream->runtime->hw.formats = SNDRV_PCM_FMTBIT_S32;
 68		s = &dg00x->tx_stream;
 69	} else {
 70		substream->runtime->hw.formats = SNDRV_PCM_FMTBIT_S32;
 71		s = &dg00x->rx_stream;
 72	}
 73
 74	hw->channels_min = 10;
 75	hw->channels_max = 18;
 76
 77	hw->rates = SNDRV_PCM_RATE_44100 |
 78		    SNDRV_PCM_RATE_48000 |
 79		    SNDRV_PCM_RATE_88200 |
 80		    SNDRV_PCM_RATE_96000;
 81	snd_pcm_limit_hw_rates(runtime);
 82
 83	err = snd_pcm_hw_rule_add(substream->runtime, 0,
 84				  SNDRV_PCM_HW_PARAM_CHANNELS,
 85				  hw_rule_channels, NULL,
 86				  SNDRV_PCM_HW_PARAM_RATE, -1);
 87	if (err < 0)
 88		return err;
 89
 90	err = snd_pcm_hw_rule_add(substream->runtime, 0,
 91				  SNDRV_PCM_HW_PARAM_RATE,
 92				  hw_rule_rate, NULL,
 93				  SNDRV_PCM_HW_PARAM_CHANNELS, -1);
 94	if (err < 0)
 95		return err;
 96
 97	return amdtp_dot_add_pcm_hw_constraints(s, substream->runtime);
 98}
 99
100static int pcm_open(struct snd_pcm_substream *substream)
101{
102	struct snd_dg00x *dg00x = substream->private_data;
103	struct amdtp_domain *d = &dg00x->domain;
104	enum snd_dg00x_clock clock;
105	bool detect;
106	int err;
107
108	err = snd_dg00x_stream_lock_try(dg00x);
109	if (err < 0)
110		return err;
111
112	err = pcm_init_hw_params(dg00x, substream);
113	if (err < 0)
114		goto err_locked;
115
116	/* Check current clock source. */
117	err = snd_dg00x_stream_get_clock(dg00x, &clock);
118	if (err < 0)
119		goto err_locked;
120	if (clock != SND_DG00X_CLOCK_INTERNAL) {
121		err = snd_dg00x_stream_check_external_clock(dg00x, &detect);
122		if (err < 0)
123			goto err_locked;
124		if (!detect) {
125			err = -EBUSY;
126			goto err_locked;
127		}
128	}
129
130	scoped_guard(mutex, &dg00x->mutex) {
131		// When source of clock is not internal or any stream is reserved for
132		// transmission of PCM frames, the available sampling rate is limited
133		// at current one.
134		if ((clock != SND_DG00X_CLOCK_INTERNAL) ||
135		    (dg00x->substreams_counter > 0 && d->events_per_period > 0)) {
136			unsigned int frames_per_period = d->events_per_period;
137			unsigned int frames_per_buffer = d->events_per_buffer;
138			unsigned int rate;
139
140			err = snd_dg00x_stream_get_external_rate(dg00x, &rate);
141			if (err < 0)
142				goto err_locked;
143			substream->runtime->hw.rate_min = rate;
144			substream->runtime->hw.rate_max = rate;
145
146			if (frames_per_period > 0) {
147				err = snd_pcm_hw_constraint_minmax(substream->runtime,
148								   SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
149								   frames_per_period, frames_per_period);
150				if (err < 0)
151					goto err_locked;
152
153				err = snd_pcm_hw_constraint_minmax(substream->runtime,
154								   SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
155								   frames_per_buffer, frames_per_buffer);
156				if (err < 0)
157					goto err_locked;
158			}
159		}
160	}
161
162	snd_pcm_set_sync(substream);
163
164	return 0;
165err_locked:
166	snd_dg00x_stream_lock_release(dg00x);
167	return err;
168}
169
170static int pcm_close(struct snd_pcm_substream *substream)
171{
172	struct snd_dg00x *dg00x = substream->private_data;
173
174	snd_dg00x_stream_lock_release(dg00x);
175
176	return 0;
177}
178
179static int pcm_hw_params(struct snd_pcm_substream *substream,
180			 struct snd_pcm_hw_params *hw_params)
181{
182	struct snd_dg00x *dg00x = substream->private_data;
183	int err = 0;
184
185	if (substream->runtime->state == SNDRV_PCM_STATE_OPEN) {
186		unsigned int rate = params_rate(hw_params);
187		unsigned int frames_per_period = params_period_size(hw_params);
188		unsigned int frames_per_buffer = params_buffer_size(hw_params);
189
190		guard(mutex)(&dg00x->mutex);
191		err = snd_dg00x_stream_reserve_duplex(dg00x, rate,
192					frames_per_period, frames_per_buffer);
193		if (err >= 0)
194			++dg00x->substreams_counter;
195	}
196
197	return err;
198}
199
200static int pcm_hw_free(struct snd_pcm_substream *substream)
201{
202	struct snd_dg00x *dg00x = substream->private_data;
203
204	guard(mutex)(&dg00x->mutex);
205
206	if (substream->runtime->state != SNDRV_PCM_STATE_OPEN)
207		--dg00x->substreams_counter;
208
209	snd_dg00x_stream_stop_duplex(dg00x);
210
211	return 0;
212}
213
214static int pcm_capture_prepare(struct snd_pcm_substream *substream)
215{
216	struct snd_dg00x *dg00x = substream->private_data;
217	int err;
218
219	guard(mutex)(&dg00x->mutex);
220
221	err = snd_dg00x_stream_start_duplex(dg00x);
222	if (err >= 0)
223		amdtp_stream_pcm_prepare(&dg00x->tx_stream);
224
225	return err;
226}
227
228static int pcm_playback_prepare(struct snd_pcm_substream *substream)
229{
230	struct snd_dg00x *dg00x = substream->private_data;
231	int err;
232
233	guard(mutex)(&dg00x->mutex);
234
235	err = snd_dg00x_stream_start_duplex(dg00x);
236	if (err >= 0) {
237		amdtp_stream_pcm_prepare(&dg00x->rx_stream);
238		amdtp_dot_reset(&dg00x->rx_stream);
239	}
240
241	return err;
242}
243
244static int pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
245{
246	struct snd_dg00x *dg00x = substream->private_data;
247
248	switch (cmd) {
249	case SNDRV_PCM_TRIGGER_START:
250		amdtp_stream_pcm_trigger(&dg00x->tx_stream, substream);
251		break;
252	case SNDRV_PCM_TRIGGER_STOP:
253		amdtp_stream_pcm_trigger(&dg00x->tx_stream, NULL);
254		break;
255	default:
256		return -EINVAL;
257	}
258
259	return 0;
260}
261
262static int pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
263{
264	struct snd_dg00x *dg00x = substream->private_data;
265
266	switch (cmd) {
267	case SNDRV_PCM_TRIGGER_START:
268		amdtp_stream_pcm_trigger(&dg00x->rx_stream, substream);
269		break;
270	case SNDRV_PCM_TRIGGER_STOP:
271		amdtp_stream_pcm_trigger(&dg00x->rx_stream, NULL);
272		break;
273	default:
274		return -EINVAL;
275	}
276
277	return 0;
278}
279
280static snd_pcm_uframes_t pcm_capture_pointer(struct snd_pcm_substream *sbstrm)
281{
282	struct snd_dg00x *dg00x = sbstrm->private_data;
283
284	return amdtp_domain_stream_pcm_pointer(&dg00x->domain, &dg00x->tx_stream);
285}
286
287static snd_pcm_uframes_t pcm_playback_pointer(struct snd_pcm_substream *sbstrm)
288{
289	struct snd_dg00x *dg00x = sbstrm->private_data;
290
291	return amdtp_domain_stream_pcm_pointer(&dg00x->domain, &dg00x->rx_stream);
292}
293
294static int pcm_capture_ack(struct snd_pcm_substream *substream)
295{
296	struct snd_dg00x *dg00x = substream->private_data;
297
298	return amdtp_domain_stream_pcm_ack(&dg00x->domain, &dg00x->tx_stream);
299}
300
301static int pcm_playback_ack(struct snd_pcm_substream *substream)
302{
303	struct snd_dg00x *dg00x = substream->private_data;
304
305	return amdtp_domain_stream_pcm_ack(&dg00x->domain, &dg00x->rx_stream);
306}
307
308int snd_dg00x_create_pcm_devices(struct snd_dg00x *dg00x)
309{
310	static const struct snd_pcm_ops capture_ops = {
311		.open		= pcm_open,
312		.close		= pcm_close,
313		.hw_params	= pcm_hw_params,
314		.hw_free	= pcm_hw_free,
315		.prepare	= pcm_capture_prepare,
316		.trigger	= pcm_capture_trigger,
317		.pointer	= pcm_capture_pointer,
318		.ack		= pcm_capture_ack,
319	};
320	static const struct snd_pcm_ops playback_ops = {
321		.open		= pcm_open,
322		.close		= pcm_close,
323		.hw_params	= pcm_hw_params,
324		.hw_free	= pcm_hw_free,
325		.prepare	= pcm_playback_prepare,
326		.trigger	= pcm_playback_trigger,
327		.pointer	= pcm_playback_pointer,
328		.ack		= pcm_playback_ack,
329	};
330	struct snd_pcm *pcm;
331	int err;
332
333	err = snd_pcm_new(dg00x->card, dg00x->card->driver, 0, 1, 1, &pcm);
334	if (err < 0)
335		return err;
336
337	pcm->private_data = dg00x;
338	pcm->nonatomic = true;
339	snprintf(pcm->name, sizeof(pcm->name),
340		 "%s PCM", dg00x->card->shortname);
341	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &playback_ops);
342	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &capture_ops);
343	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC, NULL, 0, 0);
344
345	return 0;
346}
~/ Read-only file preview.