xref: /openbmc/linux/sound/firewire/oxfw/oxfw-pcm.c (revision 23cb0767)
1da607e19SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
23713d93aSTakashi Sakamoto /*
33713d93aSTakashi Sakamoto  * oxfw_pcm.c - a part of driver for OXFW970/971 based devices
43713d93aSTakashi Sakamoto  *
53713d93aSTakashi Sakamoto  * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
63713d93aSTakashi Sakamoto  */
73713d93aSTakashi Sakamoto 
83713d93aSTakashi Sakamoto #include "oxfw.h"
93713d93aSTakashi Sakamoto 
hw_rule_rate(struct snd_pcm_hw_params * params,struct snd_pcm_hw_rule * rule)105cd1d3f4STakashi Sakamoto static int hw_rule_rate(struct snd_pcm_hw_params *params,
113713d93aSTakashi Sakamoto 			struct snd_pcm_hw_rule *rule)
123713d93aSTakashi Sakamoto {
135cd1d3f4STakashi Sakamoto 	u8 **formats = rule->private;
145cd1d3f4STakashi Sakamoto 	struct snd_interval *r =
153713d93aSTakashi Sakamoto 		hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
165cd1d3f4STakashi Sakamoto 	const struct snd_interval *c =
175cd1d3f4STakashi Sakamoto 		hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS);
185cd1d3f4STakashi Sakamoto 	struct snd_interval t = {
195cd1d3f4STakashi Sakamoto 		.min = UINT_MAX, .max = 0, .integer = 1
203713d93aSTakashi Sakamoto 	};
215cd1d3f4STakashi Sakamoto 	struct snd_oxfw_stream_formation formation;
225580ba7bSDan Carpenter 	int i, err;
233713d93aSTakashi Sakamoto 
245cd1d3f4STakashi Sakamoto 	for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
255cd1d3f4STakashi Sakamoto 		if (formats[i] == NULL)
265cd1d3f4STakashi Sakamoto 			continue;
273713d93aSTakashi Sakamoto 
285cd1d3f4STakashi Sakamoto 		err = snd_oxfw_stream_parse_format(formats[i], &formation);
293713d93aSTakashi Sakamoto 		if (err < 0)
305cd1d3f4STakashi Sakamoto 			continue;
315cd1d3f4STakashi Sakamoto 		if (!snd_interval_test(c, formation.pcm))
325cd1d3f4STakashi Sakamoto 			continue;
333713d93aSTakashi Sakamoto 
345cd1d3f4STakashi Sakamoto 		t.min = min(t.min, formation.rate);
355cd1d3f4STakashi Sakamoto 		t.max = max(t.max, formation.rate);
365cd1d3f4STakashi Sakamoto 
375cd1d3f4STakashi Sakamoto 	}
385cd1d3f4STakashi Sakamoto 	return snd_interval_refine(r, &t);
393713d93aSTakashi Sakamoto }
403713d93aSTakashi Sakamoto 
hw_rule_channels(struct snd_pcm_hw_params * params,struct snd_pcm_hw_rule * rule)415cd1d3f4STakashi Sakamoto static int hw_rule_channels(struct snd_pcm_hw_params *params,
425cd1d3f4STakashi Sakamoto 			    struct snd_pcm_hw_rule *rule)
433713d93aSTakashi Sakamoto {
445cd1d3f4STakashi Sakamoto 	u8 **formats = rule->private;
455cd1d3f4STakashi Sakamoto 	struct snd_interval *c =
465cd1d3f4STakashi Sakamoto 		hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
475cd1d3f4STakashi Sakamoto 	const struct snd_interval *r =
485cd1d3f4STakashi Sakamoto 		hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
495cd1d3f4STakashi Sakamoto 	struct snd_oxfw_stream_formation formation;
505580ba7bSDan Carpenter 	int i, j, err;
515cd1d3f4STakashi Sakamoto 	unsigned int count, list[SND_OXFW_STREAM_FORMAT_ENTRIES] = {0};
523713d93aSTakashi Sakamoto 
535cd1d3f4STakashi Sakamoto 	count = 0;
545cd1d3f4STakashi Sakamoto 	for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
555cd1d3f4STakashi Sakamoto 		if (formats[i] == NULL)
565cd1d3f4STakashi Sakamoto 			break;
575cd1d3f4STakashi Sakamoto 
585cd1d3f4STakashi Sakamoto 		err = snd_oxfw_stream_parse_format(formats[i], &formation);
595cd1d3f4STakashi Sakamoto 		if (err < 0)
605cd1d3f4STakashi Sakamoto 			continue;
615cd1d3f4STakashi Sakamoto 		if (!snd_interval_test(r, formation.rate))
625cd1d3f4STakashi Sakamoto 			continue;
635cd1d3f4STakashi Sakamoto 		if (list[count] == formation.pcm)
645cd1d3f4STakashi Sakamoto 			continue;
655cd1d3f4STakashi Sakamoto 
665cd1d3f4STakashi Sakamoto 		for (j = 0; j < ARRAY_SIZE(list); j++) {
675cd1d3f4STakashi Sakamoto 			if (list[j] == formation.pcm)
685cd1d3f4STakashi Sakamoto 				break;
695cd1d3f4STakashi Sakamoto 		}
705cd1d3f4STakashi Sakamoto 		if (j == ARRAY_SIZE(list)) {
715cd1d3f4STakashi Sakamoto 			list[count] = formation.pcm;
725cd1d3f4STakashi Sakamoto 			if (++count == ARRAY_SIZE(list))
735cd1d3f4STakashi Sakamoto 				break;
745cd1d3f4STakashi Sakamoto 		}
755cd1d3f4STakashi Sakamoto 	}
765cd1d3f4STakashi Sakamoto 
775cd1d3f4STakashi Sakamoto 	return snd_interval_list(c, count, list, 0);
785cd1d3f4STakashi Sakamoto }
795cd1d3f4STakashi Sakamoto 
limit_channels_and_rates(struct snd_pcm_hardware * hw,u8 ** formats)805cd1d3f4STakashi Sakamoto static void limit_channels_and_rates(struct snd_pcm_hardware *hw, u8 **formats)
815cd1d3f4STakashi Sakamoto {
825cd1d3f4STakashi Sakamoto 	struct snd_oxfw_stream_formation formation;
835580ba7bSDan Carpenter 	int i, err;
845cd1d3f4STakashi Sakamoto 
855cd1d3f4STakashi Sakamoto 	hw->channels_min = UINT_MAX;
865cd1d3f4STakashi Sakamoto 	hw->channels_max = 0;
875cd1d3f4STakashi Sakamoto 
885cd1d3f4STakashi Sakamoto 	hw->rate_min = UINT_MAX;
895cd1d3f4STakashi Sakamoto 	hw->rate_max = 0;
905cd1d3f4STakashi Sakamoto 	hw->rates = 0;
915cd1d3f4STakashi Sakamoto 
925cd1d3f4STakashi Sakamoto 	for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
935cd1d3f4STakashi Sakamoto 		if (formats[i] == NULL)
945cd1d3f4STakashi Sakamoto 			break;
955cd1d3f4STakashi Sakamoto 
965cd1d3f4STakashi Sakamoto 		err = snd_oxfw_stream_parse_format(formats[i], &formation);
975cd1d3f4STakashi Sakamoto 		if (err < 0)
985cd1d3f4STakashi Sakamoto 			continue;
995cd1d3f4STakashi Sakamoto 
1005cd1d3f4STakashi Sakamoto 		hw->channels_min = min(hw->channels_min, formation.pcm);
1015cd1d3f4STakashi Sakamoto 		hw->channels_max = max(hw->channels_max, formation.pcm);
1025cd1d3f4STakashi Sakamoto 
1035cd1d3f4STakashi Sakamoto 		hw->rate_min = min(hw->rate_min, formation.rate);
1045cd1d3f4STakashi Sakamoto 		hw->rate_max = max(hw->rate_max, formation.rate);
1055cd1d3f4STakashi Sakamoto 		hw->rates |= snd_pcm_rate_to_rate_bit(formation.rate);
1065cd1d3f4STakashi Sakamoto 	}
1075cd1d3f4STakashi Sakamoto }
1085cd1d3f4STakashi Sakamoto 
init_hw_params(struct snd_oxfw * oxfw,struct snd_pcm_substream * substream)109216e256fSTakashi Sakamoto static int init_hw_params(struct snd_oxfw *oxfw,
110216e256fSTakashi Sakamoto 			  struct snd_pcm_substream *substream)
1113713d93aSTakashi Sakamoto {
1123713d93aSTakashi Sakamoto 	struct snd_pcm_runtime *runtime = substream->runtime;
1135cd1d3f4STakashi Sakamoto 	u8 **formats;
114216e256fSTakashi Sakamoto 	struct amdtp_stream *stream;
1153713d93aSTakashi Sakamoto 	int err;
1163713d93aSTakashi Sakamoto 
117216e256fSTakashi Sakamoto 	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
11849c7b3fcSTakashi Sakamoto 		runtime->hw.formats = AM824_IN_PCM_FORMAT_BITS;
119216e256fSTakashi Sakamoto 		stream = &oxfw->tx_stream;
120216e256fSTakashi Sakamoto 		formats = oxfw->tx_stream_formats;
121216e256fSTakashi Sakamoto 	} else {
12249c7b3fcSTakashi Sakamoto 		runtime->hw.formats = AM824_OUT_PCM_FORMAT_BITS;
123216e256fSTakashi Sakamoto 		stream = &oxfw->rx_stream;
124216e256fSTakashi Sakamoto 		formats = oxfw->rx_stream_formats;
125216e256fSTakashi Sakamoto 	}
126216e256fSTakashi Sakamoto 
1275cd1d3f4STakashi Sakamoto 	limit_channels_and_rates(&runtime->hw, formats);
1285cd1d3f4STakashi Sakamoto 
1295cd1d3f4STakashi Sakamoto 	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1305cd1d3f4STakashi Sakamoto 				  hw_rule_channels, formats,
1315cd1d3f4STakashi Sakamoto 				  SNDRV_PCM_HW_PARAM_RATE, -1);
1323713d93aSTakashi Sakamoto 	if (err < 0)
1333713d93aSTakashi Sakamoto 		goto end;
1345cd1d3f4STakashi Sakamoto 
1355cd1d3f4STakashi Sakamoto 	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1365cd1d3f4STakashi Sakamoto 				  hw_rule_rate, formats,
1375cd1d3f4STakashi Sakamoto 				  SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1383713d93aSTakashi Sakamoto 	if (err < 0)
1393713d93aSTakashi Sakamoto 		goto end;
1403713d93aSTakashi Sakamoto 
141bc8500daSTakashi Sakamoto 	err = amdtp_am824_add_pcm_hw_constraints(stream, runtime);
142216e256fSTakashi Sakamoto end:
143216e256fSTakashi Sakamoto 	return err;
144216e256fSTakashi Sakamoto }
145216e256fSTakashi Sakamoto 
limit_to_current_params(struct snd_pcm_substream * substream)146216e256fSTakashi Sakamoto static int limit_to_current_params(struct snd_pcm_substream *substream)
147216e256fSTakashi Sakamoto {
148216e256fSTakashi Sakamoto 	struct snd_oxfw *oxfw = substream->private_data;
149216e256fSTakashi Sakamoto 	struct snd_oxfw_stream_formation formation;
150216e256fSTakashi Sakamoto 	enum avc_general_plug_dir dir;
151216e256fSTakashi Sakamoto 	int err;
152216e256fSTakashi Sakamoto 
153216e256fSTakashi Sakamoto 	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
154216e256fSTakashi Sakamoto 		dir = AVC_GENERAL_PLUG_DIR_OUT;
155216e256fSTakashi Sakamoto 	else
156216e256fSTakashi Sakamoto 		dir = AVC_GENERAL_PLUG_DIR_IN;
157216e256fSTakashi Sakamoto 
158216e256fSTakashi Sakamoto 	err = snd_oxfw_stream_get_current_formation(oxfw, dir, &formation);
1595cd1d3f4STakashi Sakamoto 	if (err < 0)
1605cd1d3f4STakashi Sakamoto 		goto end;
1615cd1d3f4STakashi Sakamoto 
162216e256fSTakashi Sakamoto 	substream->runtime->hw.channels_min = formation.pcm;
163216e256fSTakashi Sakamoto 	substream->runtime->hw.channels_max = formation.pcm;
164216e256fSTakashi Sakamoto 	substream->runtime->hw.rate_min = formation.rate;
165216e256fSTakashi Sakamoto 	substream->runtime->hw.rate_max = formation.rate;
166216e256fSTakashi Sakamoto end:
167216e256fSTakashi Sakamoto 	return err;
168216e256fSTakashi Sakamoto }
169216e256fSTakashi Sakamoto 
pcm_open(struct snd_pcm_substream * substream)170216e256fSTakashi Sakamoto static int pcm_open(struct snd_pcm_substream *substream)
171216e256fSTakashi Sakamoto {
172216e256fSTakashi Sakamoto 	struct snd_oxfw *oxfw = substream->private_data;
173c0ede398STakashi Sakamoto 	struct amdtp_domain *d = &oxfw->domain;
174216e256fSTakashi Sakamoto 	int err;
175216e256fSTakashi Sakamoto 
1768985f4acSTakashi Sakamoto 	err = snd_oxfw_stream_lock_try(oxfw);
177216e256fSTakashi Sakamoto 	if (err < 0)
178c0ede398STakashi Sakamoto 		return err;
179216e256fSTakashi Sakamoto 
1808985f4acSTakashi Sakamoto 	err = init_hw_params(oxfw, substream);
1818985f4acSTakashi Sakamoto 	if (err < 0)
1828985f4acSTakashi Sakamoto 		goto err_locked;
1838985f4acSTakashi Sakamoto 
184c0ede398STakashi Sakamoto 	mutex_lock(&oxfw->mutex);
185c0ede398STakashi Sakamoto 
186c0ede398STakashi Sakamoto 	// When source of clock is not internal or any stream is reserved for
187c0ede398STakashi Sakamoto 	// transmission of PCM frames, the available sampling rate is limited
188c0ede398STakashi Sakamoto 	// at current one.
189c0ede398STakashi Sakamoto 	if (oxfw->substreams_count > 0 && d->events_per_period > 0) {
190c0ede398STakashi Sakamoto 		unsigned int frames_per_period = d->events_per_period;
1913299d2a0STakashi Sakamoto 		unsigned int frames_per_buffer = d->events_per_buffer;
192c0ede398STakashi Sakamoto 
193216e256fSTakashi Sakamoto 		err = limit_to_current_params(substream);
194c0ede398STakashi Sakamoto 		if (err < 0) {
195c0ede398STakashi Sakamoto 			mutex_unlock(&oxfw->mutex);
196c0ede398STakashi Sakamoto 			goto err_locked;
197216e256fSTakashi Sakamoto 		}
198216e256fSTakashi Sakamoto 
199c0ede398STakashi Sakamoto 		if (frames_per_period > 0) {
200c0ede398STakashi Sakamoto 			err = snd_pcm_hw_constraint_minmax(substream->runtime,
201c0ede398STakashi Sakamoto 					SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
202c0ede398STakashi Sakamoto 					frames_per_period, frames_per_period);
203c0ede398STakashi Sakamoto 			if (err < 0) {
204c0ede398STakashi Sakamoto 				mutex_unlock(&oxfw->mutex);
205c0ede398STakashi Sakamoto 				goto err_locked;
206c0ede398STakashi Sakamoto 			}
2073299d2a0STakashi Sakamoto 
2083299d2a0STakashi Sakamoto 			err = snd_pcm_hw_constraint_minmax(substream->runtime,
2093299d2a0STakashi Sakamoto 					SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2103299d2a0STakashi Sakamoto 					frames_per_buffer, frames_per_buffer);
2113299d2a0STakashi Sakamoto 			if (err < 0) {
2123299d2a0STakashi Sakamoto 				mutex_unlock(&oxfw->mutex);
2133299d2a0STakashi Sakamoto 				goto err_locked;
2143299d2a0STakashi Sakamoto 			}
215c0ede398STakashi Sakamoto 		}
216c0ede398STakashi Sakamoto 	}
217c0ede398STakashi Sakamoto 
218c0ede398STakashi Sakamoto 	mutex_unlock(&oxfw->mutex);
219c0ede398STakashi Sakamoto 
2205cd1d3f4STakashi Sakamoto 	snd_pcm_set_sync(substream);
221c0ede398STakashi Sakamoto 
222c0ede398STakashi Sakamoto 	return 0;
2238985f4acSTakashi Sakamoto err_locked:
2248985f4acSTakashi Sakamoto 	snd_oxfw_stream_lock_release(oxfw);
2258985f4acSTakashi Sakamoto 	return err;
2263713d93aSTakashi Sakamoto }
2273713d93aSTakashi Sakamoto 
pcm_close(struct snd_pcm_substream * substream)2283713d93aSTakashi Sakamoto static int pcm_close(struct snd_pcm_substream *substream)
2293713d93aSTakashi Sakamoto {
2308985f4acSTakashi Sakamoto 	struct snd_oxfw *oxfw = substream->private_data;
2318985f4acSTakashi Sakamoto 
2328985f4acSTakashi Sakamoto 	snd_oxfw_stream_lock_release(oxfw);
2333713d93aSTakashi Sakamoto 	return 0;
2343713d93aSTakashi Sakamoto }
2353713d93aSTakashi Sakamoto 
pcm_capture_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * hw_params)236216e256fSTakashi Sakamoto static int pcm_capture_hw_params(struct snd_pcm_substream *substream,
2373713d93aSTakashi Sakamoto 				 struct snd_pcm_hw_params *hw_params)
2383713d93aSTakashi Sakamoto {
2393713d93aSTakashi Sakamoto 	struct snd_oxfw *oxfw = substream->private_data;
2407641d549STakashi Iwai 	int err = 0;
241216e256fSTakashi Sakamoto 
242*23cb0767STakashi Iwai 	if (substream->runtime->state == SNDRV_PCM_STATE_OPEN) {
2434f380d00STakashi Sakamoto 		unsigned int rate = params_rate(hw_params);
2444f380d00STakashi Sakamoto 		unsigned int channels = params_channels(hw_params);
2451d6a722cSTakashi Sakamoto 		unsigned int frames_per_period = params_period_size(hw_params);
2463299d2a0STakashi Sakamoto 		unsigned int frames_per_buffer = params_buffer_size(hw_params);
2474f380d00STakashi Sakamoto 
248216e256fSTakashi Sakamoto 		mutex_lock(&oxfw->mutex);
2494f380d00STakashi Sakamoto 		err = snd_oxfw_stream_reserve_duplex(oxfw, &oxfw->tx_stream,
2503299d2a0STakashi Sakamoto 					rate, channels, frames_per_period,
2513299d2a0STakashi Sakamoto 					frames_per_buffer);
2524f380d00STakashi Sakamoto 		if (err >= 0)
2534a0a0472STakashi Sakamoto 			++oxfw->substreams_count;
254216e256fSTakashi Sakamoto 		mutex_unlock(&oxfw->mutex);
255216e256fSTakashi Sakamoto 	}
256216e256fSTakashi Sakamoto 
2574f380d00STakashi Sakamoto 	return err;
258216e256fSTakashi Sakamoto }
pcm_playback_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * hw_params)259216e256fSTakashi Sakamoto static int pcm_playback_hw_params(struct snd_pcm_substream *substream,
260216e256fSTakashi Sakamoto 				  struct snd_pcm_hw_params *hw_params)
261216e256fSTakashi Sakamoto {
262216e256fSTakashi Sakamoto 	struct snd_oxfw *oxfw = substream->private_data;
2637641d549STakashi Iwai 	int err = 0;
264216e256fSTakashi Sakamoto 
265*23cb0767STakashi Iwai 	if (substream->runtime->state == SNDRV_PCM_STATE_OPEN) {
2664f380d00STakashi Sakamoto 		unsigned int rate = params_rate(hw_params);
2674f380d00STakashi Sakamoto 		unsigned int channels = params_channels(hw_params);
2681d6a722cSTakashi Sakamoto 		unsigned int frames_per_period = params_period_size(hw_params);
2693299d2a0STakashi Sakamoto 		unsigned int frames_per_buffer = params_buffer_size(hw_params);
2704f380d00STakashi Sakamoto 
271216e256fSTakashi Sakamoto 		mutex_lock(&oxfw->mutex);
2722fd23293STakashi Sakamoto 		err = snd_oxfw_stream_reserve_duplex(oxfw, &oxfw->rx_stream,
2733299d2a0STakashi Sakamoto 					rate, channels, frames_per_period,
2743299d2a0STakashi Sakamoto 					frames_per_buffer);
2754f380d00STakashi Sakamoto 		if (err >= 0)
2764a0a0472STakashi Sakamoto 			++oxfw->substreams_count;
277216e256fSTakashi Sakamoto 		mutex_unlock(&oxfw->mutex);
278216e256fSTakashi Sakamoto 	}
279216e256fSTakashi Sakamoto 
28059a126aaSTakashi Sakamoto 	return err;
2813713d93aSTakashi Sakamoto }
2823713d93aSTakashi Sakamoto 
pcm_capture_hw_free(struct snd_pcm_substream * substream)283216e256fSTakashi Sakamoto static int pcm_capture_hw_free(struct snd_pcm_substream *substream)
2843713d93aSTakashi Sakamoto {
2853713d93aSTakashi Sakamoto 	struct snd_oxfw *oxfw = substream->private_data;
2863713d93aSTakashi Sakamoto 
2873713d93aSTakashi Sakamoto 	mutex_lock(&oxfw->mutex);
288216e256fSTakashi Sakamoto 
289*23cb0767STakashi Iwai 	if (substream->runtime->state != SNDRV_PCM_STATE_OPEN)
2904a0a0472STakashi Sakamoto 		--oxfw->substreams_count;
291216e256fSTakashi Sakamoto 
292779f0dbaSTakashi Sakamoto 	snd_oxfw_stream_stop_duplex(oxfw);
293216e256fSTakashi Sakamoto 
294216e256fSTakashi Sakamoto 	mutex_unlock(&oxfw->mutex);
295216e256fSTakashi Sakamoto 
2967641d549STakashi Iwai 	return 0;
297216e256fSTakashi Sakamoto }
pcm_playback_hw_free(struct snd_pcm_substream * substream)298216e256fSTakashi Sakamoto static int pcm_playback_hw_free(struct snd_pcm_substream *substream)
299216e256fSTakashi Sakamoto {
300216e256fSTakashi Sakamoto 	struct snd_oxfw *oxfw = substream->private_data;
301216e256fSTakashi Sakamoto 
302216e256fSTakashi Sakamoto 	mutex_lock(&oxfw->mutex);
303216e256fSTakashi Sakamoto 
304*23cb0767STakashi Iwai 	if (substream->runtime->state != SNDRV_PCM_STATE_OPEN)
3054a0a0472STakashi Sakamoto 		--oxfw->substreams_count;
306216e256fSTakashi Sakamoto 
307779f0dbaSTakashi Sakamoto 	snd_oxfw_stream_stop_duplex(oxfw);
308216e256fSTakashi Sakamoto 
3093713d93aSTakashi Sakamoto 	mutex_unlock(&oxfw->mutex);
3103713d93aSTakashi Sakamoto 
3117641d549STakashi Iwai 	return 0;
3123713d93aSTakashi Sakamoto }
3133713d93aSTakashi Sakamoto 
pcm_capture_prepare(struct snd_pcm_substream * substream)314216e256fSTakashi Sakamoto static int pcm_capture_prepare(struct snd_pcm_substream *substream)
315216e256fSTakashi Sakamoto {
316216e256fSTakashi Sakamoto 	struct snd_oxfw *oxfw = substream->private_data;
317216e256fSTakashi Sakamoto 	int err;
318216e256fSTakashi Sakamoto 
319216e256fSTakashi Sakamoto 	mutex_lock(&oxfw->mutex);
3204f380d00STakashi Sakamoto 	err = snd_oxfw_stream_start_duplex(oxfw);
321216e256fSTakashi Sakamoto 	mutex_unlock(&oxfw->mutex);
322216e256fSTakashi Sakamoto 	if (err < 0)
323216e256fSTakashi Sakamoto 		goto end;
324216e256fSTakashi Sakamoto 
325216e256fSTakashi Sakamoto 	amdtp_stream_pcm_prepare(&oxfw->tx_stream);
326216e256fSTakashi Sakamoto end:
327216e256fSTakashi Sakamoto 	return err;
328216e256fSTakashi Sakamoto }
pcm_playback_prepare(struct snd_pcm_substream * substream)329216e256fSTakashi Sakamoto static int pcm_playback_prepare(struct snd_pcm_substream *substream)
3303713d93aSTakashi Sakamoto {
3313713d93aSTakashi Sakamoto 	struct snd_oxfw *oxfw = substream->private_data;
3323713d93aSTakashi Sakamoto 	int err;
3333713d93aSTakashi Sakamoto 
3343713d93aSTakashi Sakamoto 	mutex_lock(&oxfw->mutex);
3354f380d00STakashi Sakamoto 	err = snd_oxfw_stream_start_duplex(oxfw);
336f3699e2cSTakashi Sakamoto 	mutex_unlock(&oxfw->mutex);
3373713d93aSTakashi Sakamoto 	if (err < 0)
3383713d93aSTakashi Sakamoto 		goto end;
3393713d93aSTakashi Sakamoto 
3403713d93aSTakashi Sakamoto 	amdtp_stream_pcm_prepare(&oxfw->rx_stream);
3413713d93aSTakashi Sakamoto end:
3423713d93aSTakashi Sakamoto 	return err;
3433713d93aSTakashi Sakamoto }
3443713d93aSTakashi Sakamoto 
pcm_capture_trigger(struct snd_pcm_substream * substream,int cmd)345216e256fSTakashi Sakamoto static int pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
346216e256fSTakashi Sakamoto {
347216e256fSTakashi Sakamoto 	struct snd_oxfw *oxfw = substream->private_data;
348216e256fSTakashi Sakamoto 	struct snd_pcm_substream *pcm;
349216e256fSTakashi Sakamoto 
350216e256fSTakashi Sakamoto 	switch (cmd) {
351216e256fSTakashi Sakamoto 	case SNDRV_PCM_TRIGGER_START:
352216e256fSTakashi Sakamoto 		pcm = substream;
353216e256fSTakashi Sakamoto 		break;
354216e256fSTakashi Sakamoto 	case SNDRV_PCM_TRIGGER_STOP:
355216e256fSTakashi Sakamoto 		pcm = NULL;
356216e256fSTakashi Sakamoto 		break;
357216e256fSTakashi Sakamoto 	default:
358216e256fSTakashi Sakamoto 		return -EINVAL;
359216e256fSTakashi Sakamoto 	}
360216e256fSTakashi Sakamoto 	amdtp_stream_pcm_trigger(&oxfw->tx_stream, pcm);
361216e256fSTakashi Sakamoto 	return 0;
362216e256fSTakashi Sakamoto }
pcm_playback_trigger(struct snd_pcm_substream * substream,int cmd)363216e256fSTakashi Sakamoto static int pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
3643713d93aSTakashi Sakamoto {
3653713d93aSTakashi Sakamoto 	struct snd_oxfw *oxfw = substream->private_data;
3663713d93aSTakashi Sakamoto 	struct snd_pcm_substream *pcm;
3673713d93aSTakashi Sakamoto 
3683713d93aSTakashi Sakamoto 	switch (cmd) {
3693713d93aSTakashi Sakamoto 	case SNDRV_PCM_TRIGGER_START:
3703713d93aSTakashi Sakamoto 		pcm = substream;
3713713d93aSTakashi Sakamoto 		break;
3723713d93aSTakashi Sakamoto 	case SNDRV_PCM_TRIGGER_STOP:
3733713d93aSTakashi Sakamoto 		pcm = NULL;
3743713d93aSTakashi Sakamoto 		break;
3753713d93aSTakashi Sakamoto 	default:
3763713d93aSTakashi Sakamoto 		return -EINVAL;
3773713d93aSTakashi Sakamoto 	}
3783713d93aSTakashi Sakamoto 	amdtp_stream_pcm_trigger(&oxfw->rx_stream, pcm);
3793713d93aSTakashi Sakamoto 	return 0;
3803713d93aSTakashi Sakamoto }
3813713d93aSTakashi Sakamoto 
pcm_capture_pointer(struct snd_pcm_substream * sbstm)382216e256fSTakashi Sakamoto static snd_pcm_uframes_t pcm_capture_pointer(struct snd_pcm_substream *sbstm)
3833713d93aSTakashi Sakamoto {
384216e256fSTakashi Sakamoto 	struct snd_oxfw *oxfw = sbstm->private_data;
385216e256fSTakashi Sakamoto 
386f890f9a0STakashi Sakamoto 	return amdtp_domain_stream_pcm_pointer(&oxfw->domain, &oxfw->tx_stream);
387216e256fSTakashi Sakamoto }
pcm_playback_pointer(struct snd_pcm_substream * sbstm)388216e256fSTakashi Sakamoto static snd_pcm_uframes_t pcm_playback_pointer(struct snd_pcm_substream *sbstm)
389216e256fSTakashi Sakamoto {
390216e256fSTakashi Sakamoto 	struct snd_oxfw *oxfw = sbstm->private_data;
3913713d93aSTakashi Sakamoto 
392f890f9a0STakashi Sakamoto 	return amdtp_domain_stream_pcm_pointer(&oxfw->domain, &oxfw->rx_stream);
3933713d93aSTakashi Sakamoto }
3943713d93aSTakashi Sakamoto 
pcm_capture_ack(struct snd_pcm_substream * substream)395875becf8STakashi Sakamoto static int pcm_capture_ack(struct snd_pcm_substream *substream)
396875becf8STakashi Sakamoto {
397875becf8STakashi Sakamoto 	struct snd_oxfw *oxfw = substream->private_data;
398875becf8STakashi Sakamoto 
399e6dcc92fSTakashi Sakamoto 	return amdtp_domain_stream_pcm_ack(&oxfw->domain, &oxfw->tx_stream);
400875becf8STakashi Sakamoto }
401875becf8STakashi Sakamoto 
pcm_playback_ack(struct snd_pcm_substream * substream)402875becf8STakashi Sakamoto static int pcm_playback_ack(struct snd_pcm_substream *substream)
403875becf8STakashi Sakamoto {
404875becf8STakashi Sakamoto 	struct snd_oxfw *oxfw = substream->private_data;
405875becf8STakashi Sakamoto 
406e6dcc92fSTakashi Sakamoto 	return amdtp_domain_stream_pcm_ack(&oxfw->domain, &oxfw->rx_stream);
407875becf8STakashi Sakamoto }
408875becf8STakashi Sakamoto 
snd_oxfw_create_pcm(struct snd_oxfw * oxfw)4093713d93aSTakashi Sakamoto int snd_oxfw_create_pcm(struct snd_oxfw *oxfw)
4103713d93aSTakashi Sakamoto {
4115116ffc3SJulia Lawall 	static const struct snd_pcm_ops capture_ops = {
4123713d93aSTakashi Sakamoto 		.open      = pcm_open,
4133713d93aSTakashi Sakamoto 		.close     = pcm_close,
414216e256fSTakashi Sakamoto 		.hw_params = pcm_capture_hw_params,
415216e256fSTakashi Sakamoto 		.hw_free   = pcm_capture_hw_free,
416216e256fSTakashi Sakamoto 		.prepare   = pcm_capture_prepare,
417216e256fSTakashi Sakamoto 		.trigger   = pcm_capture_trigger,
418216e256fSTakashi Sakamoto 		.pointer   = pcm_capture_pointer,
419875becf8STakashi Sakamoto 		.ack       = pcm_capture_ack,
420216e256fSTakashi Sakamoto 	};
4215116ffc3SJulia Lawall 	static const struct snd_pcm_ops playback_ops = {
422216e256fSTakashi Sakamoto 		.open      = pcm_open,
423216e256fSTakashi Sakamoto 		.close     = pcm_close,
424216e256fSTakashi Sakamoto 		.hw_params = pcm_playback_hw_params,
425216e256fSTakashi Sakamoto 		.hw_free   = pcm_playback_hw_free,
426216e256fSTakashi Sakamoto 		.prepare   = pcm_playback_prepare,
427216e256fSTakashi Sakamoto 		.trigger   = pcm_playback_trigger,
428216e256fSTakashi Sakamoto 		.pointer   = pcm_playback_pointer,
429875becf8STakashi Sakamoto 		.ack       = pcm_playback_ack,
4303713d93aSTakashi Sakamoto 	};
4313713d93aSTakashi Sakamoto 	struct snd_pcm *pcm;
432216e256fSTakashi Sakamoto 	unsigned int cap = 0;
4333713d93aSTakashi Sakamoto 	int err;
4343713d93aSTakashi Sakamoto 
435216e256fSTakashi Sakamoto 	if (oxfw->has_output)
436216e256fSTakashi Sakamoto 		cap = 1;
437216e256fSTakashi Sakamoto 
438216e256fSTakashi Sakamoto 	err = snd_pcm_new(oxfw->card, oxfw->card->driver, 0, 1, cap, &pcm);
4393713d93aSTakashi Sakamoto 	if (err < 0)
4403713d93aSTakashi Sakamoto 		return err;
441216e256fSTakashi Sakamoto 
4423713d93aSTakashi Sakamoto 	pcm->private_data = oxfw;
4433713d93aSTakashi Sakamoto 	strcpy(pcm->name, oxfw->card->shortname);
444216e256fSTakashi Sakamoto 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &playback_ops);
445216e256fSTakashi Sakamoto 	if (cap > 0)
446216e256fSTakashi Sakamoto 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &capture_ops);
4477641d549STakashi Iwai 	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC, NULL, 0, 0);
448216e256fSTakashi Sakamoto 
4493713d93aSTakashi Sakamoto 	return 0;
4503713d93aSTakashi Sakamoto }
451