xref: /linux/sound/firewire/bebob/bebob_pcm.c (revision 875becf8412c60ffae93c5f69e95a4d023f0e8ee)
1fbbebd2cSTakashi Sakamoto /*
2fbbebd2cSTakashi Sakamoto  * bebob_pcm.c - a part of driver for BeBoB based devices
3fbbebd2cSTakashi Sakamoto  *
4fbbebd2cSTakashi Sakamoto  * Copyright (c) 2013-2014 Takashi Sakamoto
5fbbebd2cSTakashi Sakamoto  *
6fbbebd2cSTakashi Sakamoto  * Licensed under the terms of the GNU General Public License, version 2.
7fbbebd2cSTakashi Sakamoto  */
8fbbebd2cSTakashi Sakamoto 
9fbbebd2cSTakashi Sakamoto #include "./bebob.h"
10fbbebd2cSTakashi Sakamoto 
11fbbebd2cSTakashi Sakamoto static int
12fbbebd2cSTakashi Sakamoto hw_rule_rate(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
13fbbebd2cSTakashi Sakamoto {
14fbbebd2cSTakashi Sakamoto 	struct snd_bebob_stream_formation *formations = rule->private;
15fbbebd2cSTakashi Sakamoto 	struct snd_interval *r =
16fbbebd2cSTakashi Sakamoto 		hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
17fbbebd2cSTakashi Sakamoto 	const struct snd_interval *c =
18fbbebd2cSTakashi Sakamoto 		hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS);
19fbbebd2cSTakashi Sakamoto 	struct snd_interval t = {
20fbbebd2cSTakashi Sakamoto 		.min = UINT_MAX, .max = 0, .integer = 1
21fbbebd2cSTakashi Sakamoto 	};
22fbbebd2cSTakashi Sakamoto 	unsigned int i;
23fbbebd2cSTakashi Sakamoto 
24fbbebd2cSTakashi Sakamoto 	for (i = 0; i < SND_BEBOB_STRM_FMT_ENTRIES; i++) {
25fbbebd2cSTakashi Sakamoto 		/* entry is invalid */
26fbbebd2cSTakashi Sakamoto 		if (formations[i].pcm == 0)
27fbbebd2cSTakashi Sakamoto 			continue;
28fbbebd2cSTakashi Sakamoto 
29fbbebd2cSTakashi Sakamoto 		if (!snd_interval_test(c, formations[i].pcm))
30fbbebd2cSTakashi Sakamoto 			continue;
31fbbebd2cSTakashi Sakamoto 
32fbbebd2cSTakashi Sakamoto 		t.min = min(t.min, snd_bebob_rate_table[i]);
33fbbebd2cSTakashi Sakamoto 		t.max = max(t.max, snd_bebob_rate_table[i]);
34fbbebd2cSTakashi Sakamoto 
35fbbebd2cSTakashi Sakamoto 	}
36fbbebd2cSTakashi Sakamoto 	return snd_interval_refine(r, &t);
37fbbebd2cSTakashi Sakamoto }
38fbbebd2cSTakashi Sakamoto 
39fbbebd2cSTakashi Sakamoto static int
40fbbebd2cSTakashi Sakamoto hw_rule_channels(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
41fbbebd2cSTakashi Sakamoto {
42fbbebd2cSTakashi Sakamoto 	struct snd_bebob_stream_formation *formations = rule->private;
43fbbebd2cSTakashi Sakamoto 	struct snd_interval *c =
44fbbebd2cSTakashi Sakamoto 		hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
45fbbebd2cSTakashi Sakamoto 	const struct snd_interval *r =
46fbbebd2cSTakashi Sakamoto 		hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
47fbbebd2cSTakashi Sakamoto 	struct snd_interval t = {
48fbbebd2cSTakashi Sakamoto 		.min = UINT_MAX, .max = 0, .integer = 1
49fbbebd2cSTakashi Sakamoto 	};
50fbbebd2cSTakashi Sakamoto 
51fbbebd2cSTakashi Sakamoto 	unsigned int i;
52fbbebd2cSTakashi Sakamoto 
53fbbebd2cSTakashi Sakamoto 	for (i = 0; i < SND_BEBOB_STRM_FMT_ENTRIES; i++) {
54fbbebd2cSTakashi Sakamoto 		/* entry is invalid */
55fbbebd2cSTakashi Sakamoto 		if (formations[i].pcm == 0)
56fbbebd2cSTakashi Sakamoto 			continue;
57fbbebd2cSTakashi Sakamoto 
58fbbebd2cSTakashi Sakamoto 		if (!snd_interval_test(r, snd_bebob_rate_table[i]))
59fbbebd2cSTakashi Sakamoto 			continue;
60fbbebd2cSTakashi Sakamoto 
61fbbebd2cSTakashi Sakamoto 		t.min = min(t.min, formations[i].pcm);
62fbbebd2cSTakashi Sakamoto 		t.max = max(t.max, formations[i].pcm);
63fbbebd2cSTakashi Sakamoto 	}
64fbbebd2cSTakashi Sakamoto 
65fbbebd2cSTakashi Sakamoto 	return snd_interval_refine(c, &t);
66fbbebd2cSTakashi Sakamoto }
67fbbebd2cSTakashi Sakamoto 
68fbbebd2cSTakashi Sakamoto static void
69fbbebd2cSTakashi Sakamoto limit_channels_and_rates(struct snd_pcm_hardware *hw,
70fbbebd2cSTakashi Sakamoto 			 struct snd_bebob_stream_formation *formations)
71fbbebd2cSTakashi Sakamoto {
72fbbebd2cSTakashi Sakamoto 	unsigned int i;
73fbbebd2cSTakashi Sakamoto 
74fbbebd2cSTakashi Sakamoto 	hw->channels_min = UINT_MAX;
75fbbebd2cSTakashi Sakamoto 	hw->channels_max = 0;
76fbbebd2cSTakashi Sakamoto 
77fbbebd2cSTakashi Sakamoto 	hw->rate_min = UINT_MAX;
78fbbebd2cSTakashi Sakamoto 	hw->rate_max = 0;
79fbbebd2cSTakashi Sakamoto 	hw->rates = 0;
80fbbebd2cSTakashi Sakamoto 
81fbbebd2cSTakashi Sakamoto 	for (i = 0; i < SND_BEBOB_STRM_FMT_ENTRIES; i++) {
82fbbebd2cSTakashi Sakamoto 		/* entry has no PCM channels */
83fbbebd2cSTakashi Sakamoto 		if (formations[i].pcm == 0)
84fbbebd2cSTakashi Sakamoto 			continue;
85fbbebd2cSTakashi Sakamoto 
86fbbebd2cSTakashi Sakamoto 		hw->channels_min = min(hw->channels_min, formations[i].pcm);
87fbbebd2cSTakashi Sakamoto 		hw->channels_max = max(hw->channels_max, formations[i].pcm);
88fbbebd2cSTakashi Sakamoto 
89fbbebd2cSTakashi Sakamoto 		hw->rate_min = min(hw->rate_min, snd_bebob_rate_table[i]);
90fbbebd2cSTakashi Sakamoto 		hw->rate_max = max(hw->rate_max, snd_bebob_rate_table[i]);
91fbbebd2cSTakashi Sakamoto 		hw->rates |= snd_pcm_rate_to_rate_bit(snd_bebob_rate_table[i]);
92fbbebd2cSTakashi Sakamoto 	}
93fbbebd2cSTakashi Sakamoto }
94fbbebd2cSTakashi Sakamoto 
95fbbebd2cSTakashi Sakamoto static void
96fbbebd2cSTakashi Sakamoto limit_period_and_buffer(struct snd_pcm_hardware *hw)
97fbbebd2cSTakashi Sakamoto {
98fbbebd2cSTakashi Sakamoto 	hw->periods_min = 2;		/* SNDRV_PCM_INFO_BATCH */
99fbbebd2cSTakashi Sakamoto 	hw->periods_max = UINT_MAX;
100fbbebd2cSTakashi Sakamoto 
101fbbebd2cSTakashi Sakamoto 	hw->period_bytes_min = 4 * hw->channels_max;	/* bytes for a frame */
102fbbebd2cSTakashi Sakamoto 
103fbbebd2cSTakashi Sakamoto 	/* Just to prevent from allocating much pages. */
104fbbebd2cSTakashi Sakamoto 	hw->period_bytes_max = hw->period_bytes_min * 2048;
105fbbebd2cSTakashi Sakamoto 	hw->buffer_bytes_max = hw->period_bytes_max * hw->periods_min;
106fbbebd2cSTakashi Sakamoto }
107fbbebd2cSTakashi Sakamoto 
108fbbebd2cSTakashi Sakamoto static int
109fbbebd2cSTakashi Sakamoto pcm_init_hw_params(struct snd_bebob *bebob,
110fbbebd2cSTakashi Sakamoto 		   struct snd_pcm_substream *substream)
111fbbebd2cSTakashi Sakamoto {
112fbbebd2cSTakashi Sakamoto 	struct snd_pcm_runtime *runtime = substream->runtime;
113fbbebd2cSTakashi Sakamoto 	struct amdtp_stream *s;
114fbbebd2cSTakashi Sakamoto 	struct snd_bebob_stream_formation *formations;
115fbbebd2cSTakashi Sakamoto 	int err;
116fbbebd2cSTakashi Sakamoto 
117fbbebd2cSTakashi Sakamoto 	runtime->hw.info = SNDRV_PCM_INFO_BATCH |
118fbbebd2cSTakashi Sakamoto 			   SNDRV_PCM_INFO_BLOCK_TRANSFER |
119fbbebd2cSTakashi Sakamoto 			   SNDRV_PCM_INFO_INTERLEAVED |
120fbbebd2cSTakashi Sakamoto 			   SNDRV_PCM_INFO_JOINT_DUPLEX |
121fbbebd2cSTakashi Sakamoto 			   SNDRV_PCM_INFO_MMAP |
122fbbebd2cSTakashi Sakamoto 			   SNDRV_PCM_INFO_MMAP_VALID;
123fbbebd2cSTakashi Sakamoto 
124fbbebd2cSTakashi Sakamoto 	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
12549c7b3fcSTakashi Sakamoto 		runtime->hw.formats = AM824_IN_PCM_FORMAT_BITS;
126fbbebd2cSTakashi Sakamoto 		s = &bebob->tx_stream;
127fbbebd2cSTakashi Sakamoto 		formations = bebob->tx_stream_formations;
128fbbebd2cSTakashi Sakamoto 	} else {
12949c7b3fcSTakashi Sakamoto 		runtime->hw.formats = AM824_OUT_PCM_FORMAT_BITS;
130fbbebd2cSTakashi Sakamoto 		s = &bebob->rx_stream;
131fbbebd2cSTakashi Sakamoto 		formations = bebob->rx_stream_formations;
132fbbebd2cSTakashi Sakamoto 	}
133fbbebd2cSTakashi Sakamoto 
134fbbebd2cSTakashi Sakamoto 	limit_channels_and_rates(&runtime->hw, formations);
135fbbebd2cSTakashi Sakamoto 	limit_period_and_buffer(&runtime->hw);
136fbbebd2cSTakashi Sakamoto 
137fbbebd2cSTakashi Sakamoto 	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
138fbbebd2cSTakashi Sakamoto 				  hw_rule_channels, formations,
139fbbebd2cSTakashi Sakamoto 				  SNDRV_PCM_HW_PARAM_RATE, -1);
140fbbebd2cSTakashi Sakamoto 	if (err < 0)
141fbbebd2cSTakashi Sakamoto 		goto end;
142fbbebd2cSTakashi Sakamoto 
143fbbebd2cSTakashi Sakamoto 	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
144fbbebd2cSTakashi Sakamoto 				  hw_rule_rate, formations,
145fbbebd2cSTakashi Sakamoto 				  SNDRV_PCM_HW_PARAM_CHANNELS, -1);
146fbbebd2cSTakashi Sakamoto 	if (err < 0)
147fbbebd2cSTakashi Sakamoto 		goto end;
148fbbebd2cSTakashi Sakamoto 
149bc8500daSTakashi Sakamoto 	err = amdtp_am824_add_pcm_hw_constraints(s, runtime);
150fbbebd2cSTakashi Sakamoto end:
151fbbebd2cSTakashi Sakamoto 	return err;
152fbbebd2cSTakashi Sakamoto }
153fbbebd2cSTakashi Sakamoto 
154fbbebd2cSTakashi Sakamoto static int
155fbbebd2cSTakashi Sakamoto pcm_open(struct snd_pcm_substream *substream)
156fbbebd2cSTakashi Sakamoto {
157fbbebd2cSTakashi Sakamoto 	struct snd_bebob *bebob = substream->private_data;
1586b9866c8SJulia Lawall 	const struct snd_bebob_rate_spec *spec = bebob->spec->rate;
159fbbebd2cSTakashi Sakamoto 	unsigned int sampling_rate;
1603e254b16STakashi Sakamoto 	enum snd_bebob_clock_type src;
161fbbebd2cSTakashi Sakamoto 	int err;
162fbbebd2cSTakashi Sakamoto 
163618eabeaSTakashi Sakamoto 	err = snd_bebob_stream_lock_try(bebob);
164fbbebd2cSTakashi Sakamoto 	if (err < 0)
165fbbebd2cSTakashi Sakamoto 		goto end;
166fbbebd2cSTakashi Sakamoto 
167618eabeaSTakashi Sakamoto 	err = pcm_init_hw_params(bebob, substream);
168618eabeaSTakashi Sakamoto 	if (err < 0)
169618eabeaSTakashi Sakamoto 		goto err_locked;
170618eabeaSTakashi Sakamoto 
1713e254b16STakashi Sakamoto 	err = snd_bebob_stream_get_clock_src(bebob, &src);
172fbbebd2cSTakashi Sakamoto 	if (err < 0)
173618eabeaSTakashi Sakamoto 		goto err_locked;
174fbbebd2cSTakashi Sakamoto 
175fbbebd2cSTakashi Sakamoto 	/*
176fbbebd2cSTakashi Sakamoto 	 * When source of clock is internal or any PCM stream are running,
177fbbebd2cSTakashi Sakamoto 	 * the available sampling rate is limited at current sampling rate.
178fbbebd2cSTakashi Sakamoto 	 */
1793e254b16STakashi Sakamoto 	if (src == SND_BEBOB_CLOCK_TYPE_EXTERNAL ||
180fbbebd2cSTakashi Sakamoto 	    amdtp_stream_pcm_running(&bebob->tx_stream) ||
181fbbebd2cSTakashi Sakamoto 	    amdtp_stream_pcm_running(&bebob->rx_stream)) {
1821fc9522aSTakashi Sakamoto 		err = spec->get(bebob, &sampling_rate);
183fbbebd2cSTakashi Sakamoto 		if (err < 0) {
184fbbebd2cSTakashi Sakamoto 			dev_err(&bebob->unit->device,
185fbbebd2cSTakashi Sakamoto 				"fail to get sampling rate: %d\n", err);
186618eabeaSTakashi Sakamoto 			goto err_locked;
187fbbebd2cSTakashi Sakamoto 		}
188fbbebd2cSTakashi Sakamoto 
189fbbebd2cSTakashi Sakamoto 		substream->runtime->hw.rate_min = sampling_rate;
190fbbebd2cSTakashi Sakamoto 		substream->runtime->hw.rate_max = sampling_rate;
191fbbebd2cSTakashi Sakamoto 	}
192fbbebd2cSTakashi Sakamoto 
193fbbebd2cSTakashi Sakamoto 	snd_pcm_set_sync(substream);
194fbbebd2cSTakashi Sakamoto end:
195fbbebd2cSTakashi Sakamoto 	return err;
196618eabeaSTakashi Sakamoto err_locked:
197618eabeaSTakashi Sakamoto 	snd_bebob_stream_lock_release(bebob);
198618eabeaSTakashi Sakamoto 	return err;
199fbbebd2cSTakashi Sakamoto }
200fbbebd2cSTakashi Sakamoto 
201fbbebd2cSTakashi Sakamoto static int
202fbbebd2cSTakashi Sakamoto pcm_close(struct snd_pcm_substream *substream)
203fbbebd2cSTakashi Sakamoto {
204618eabeaSTakashi Sakamoto 	struct snd_bebob *bebob = substream->private_data;
205618eabeaSTakashi Sakamoto 	snd_bebob_stream_lock_release(bebob);
206fbbebd2cSTakashi Sakamoto 	return 0;
207fbbebd2cSTakashi Sakamoto }
208fbbebd2cSTakashi Sakamoto 
209fbbebd2cSTakashi Sakamoto static int
210fbbebd2cSTakashi Sakamoto pcm_capture_hw_params(struct snd_pcm_substream *substream,
211fbbebd2cSTakashi Sakamoto 		      struct snd_pcm_hw_params *hw_params)
212fbbebd2cSTakashi Sakamoto {
213fbbebd2cSTakashi Sakamoto 	struct snd_bebob *bebob = substream->private_data;
21422c103cdSTakashi Sakamoto 	int err;
21522c103cdSTakashi Sakamoto 
21622c103cdSTakashi Sakamoto 	err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
21722c103cdSTakashi Sakamoto 					       params_buffer_bytes(hw_params));
21822c103cdSTakashi Sakamoto 	if (err < 0)
21922c103cdSTakashi Sakamoto 		return err;
220fbbebd2cSTakashi Sakamoto 
2212a71e701STakashi Sakamoto 	if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
2222a71e701STakashi Sakamoto 		mutex_lock(&bebob->mutex);
2234fd6c6c7STakashi Sakamoto 		bebob->substreams_counter++;
2242a71e701STakashi Sakamoto 		mutex_unlock(&bebob->mutex);
2252a71e701STakashi Sakamoto 	}
22685130cb4STakashi Sakamoto 
22722c103cdSTakashi Sakamoto 	return 0;
228fbbebd2cSTakashi Sakamoto }
229fbbebd2cSTakashi Sakamoto static int
230fbbebd2cSTakashi Sakamoto pcm_playback_hw_params(struct snd_pcm_substream *substream,
231fbbebd2cSTakashi Sakamoto 		       struct snd_pcm_hw_params *hw_params)
232fbbebd2cSTakashi Sakamoto {
233fbbebd2cSTakashi Sakamoto 	struct snd_bebob *bebob = substream->private_data;
23422c103cdSTakashi Sakamoto 	int err;
23522c103cdSTakashi Sakamoto 
23622c103cdSTakashi Sakamoto 	err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
23722c103cdSTakashi Sakamoto 					       params_buffer_bytes(hw_params));
23822c103cdSTakashi Sakamoto 	if (err < 0)
23922c103cdSTakashi Sakamoto 		return err;
240fbbebd2cSTakashi Sakamoto 
2412a71e701STakashi Sakamoto 	if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
2422a71e701STakashi Sakamoto 		mutex_lock(&bebob->mutex);
2434fd6c6c7STakashi Sakamoto 		bebob->substreams_counter++;
2442a71e701STakashi Sakamoto 		mutex_unlock(&bebob->mutex);
2452a71e701STakashi Sakamoto 	}
24685130cb4STakashi Sakamoto 
24722c103cdSTakashi Sakamoto 	return 0;
248fbbebd2cSTakashi Sakamoto }
249fbbebd2cSTakashi Sakamoto 
250fbbebd2cSTakashi Sakamoto static int
251fbbebd2cSTakashi Sakamoto pcm_capture_hw_free(struct snd_pcm_substream *substream)
252fbbebd2cSTakashi Sakamoto {
253fbbebd2cSTakashi Sakamoto 	struct snd_bebob *bebob = substream->private_data;
254fbbebd2cSTakashi Sakamoto 
2552a71e701STakashi Sakamoto 	if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN) {
2562a71e701STakashi Sakamoto 		mutex_lock(&bebob->mutex);
2574fd6c6c7STakashi Sakamoto 		bebob->substreams_counter--;
2582a71e701STakashi Sakamoto 		mutex_unlock(&bebob->mutex);
2592a71e701STakashi Sakamoto 	}
260fbbebd2cSTakashi Sakamoto 
261fbbebd2cSTakashi Sakamoto 	snd_bebob_stream_stop_duplex(bebob);
262fbbebd2cSTakashi Sakamoto 
263fbbebd2cSTakashi Sakamoto 	return snd_pcm_lib_free_vmalloc_buffer(substream);
264fbbebd2cSTakashi Sakamoto }
265fbbebd2cSTakashi Sakamoto static int
266fbbebd2cSTakashi Sakamoto pcm_playback_hw_free(struct snd_pcm_substream *substream)
267fbbebd2cSTakashi Sakamoto {
268fbbebd2cSTakashi Sakamoto 	struct snd_bebob *bebob = substream->private_data;
269fbbebd2cSTakashi Sakamoto 
2702a71e701STakashi Sakamoto 	if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN) {
2712a71e701STakashi Sakamoto 		mutex_lock(&bebob->mutex);
2724fd6c6c7STakashi Sakamoto 		bebob->substreams_counter--;
2732a71e701STakashi Sakamoto 		mutex_unlock(&bebob->mutex);
2742a71e701STakashi Sakamoto 	}
275fbbebd2cSTakashi Sakamoto 
276fbbebd2cSTakashi Sakamoto 	snd_bebob_stream_stop_duplex(bebob);
277fbbebd2cSTakashi Sakamoto 
278fbbebd2cSTakashi Sakamoto 	return snd_pcm_lib_free_vmalloc_buffer(substream);
279fbbebd2cSTakashi Sakamoto }
280fbbebd2cSTakashi Sakamoto 
281fbbebd2cSTakashi Sakamoto static int
282fbbebd2cSTakashi Sakamoto pcm_capture_prepare(struct snd_pcm_substream *substream)
283fbbebd2cSTakashi Sakamoto {
284fbbebd2cSTakashi Sakamoto 	struct snd_bebob *bebob = substream->private_data;
285fbbebd2cSTakashi Sakamoto 	struct snd_pcm_runtime *runtime = substream->runtime;
286fbbebd2cSTakashi Sakamoto 	int err;
287fbbebd2cSTakashi Sakamoto 
288fbbebd2cSTakashi Sakamoto 	err = snd_bebob_stream_start_duplex(bebob, runtime->rate);
289fbbebd2cSTakashi Sakamoto 	if (err >= 0)
290fbbebd2cSTakashi Sakamoto 		amdtp_stream_pcm_prepare(&bebob->tx_stream);
291fbbebd2cSTakashi Sakamoto 
292fbbebd2cSTakashi Sakamoto 	return err;
293fbbebd2cSTakashi Sakamoto }
294fbbebd2cSTakashi Sakamoto static int
295fbbebd2cSTakashi Sakamoto pcm_playback_prepare(struct snd_pcm_substream *substream)
296fbbebd2cSTakashi Sakamoto {
297fbbebd2cSTakashi Sakamoto 	struct snd_bebob *bebob = substream->private_data;
298fbbebd2cSTakashi Sakamoto 	struct snd_pcm_runtime *runtime = substream->runtime;
299fbbebd2cSTakashi Sakamoto 	int err;
300fbbebd2cSTakashi Sakamoto 
301fbbebd2cSTakashi Sakamoto 	err = snd_bebob_stream_start_duplex(bebob, runtime->rate);
302fbbebd2cSTakashi Sakamoto 	if (err >= 0)
303fbbebd2cSTakashi Sakamoto 		amdtp_stream_pcm_prepare(&bebob->rx_stream);
304fbbebd2cSTakashi Sakamoto 
305fbbebd2cSTakashi Sakamoto 	return err;
306fbbebd2cSTakashi Sakamoto }
307fbbebd2cSTakashi Sakamoto 
308fbbebd2cSTakashi Sakamoto static int
309fbbebd2cSTakashi Sakamoto pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
310fbbebd2cSTakashi Sakamoto {
311fbbebd2cSTakashi Sakamoto 	struct snd_bebob *bebob = substream->private_data;
312fbbebd2cSTakashi Sakamoto 
313fbbebd2cSTakashi Sakamoto 	switch (cmd) {
314fbbebd2cSTakashi Sakamoto 	case SNDRV_PCM_TRIGGER_START:
315fbbebd2cSTakashi Sakamoto 		amdtp_stream_pcm_trigger(&bebob->tx_stream, substream);
316fbbebd2cSTakashi Sakamoto 		break;
317fbbebd2cSTakashi Sakamoto 	case SNDRV_PCM_TRIGGER_STOP:
318fbbebd2cSTakashi Sakamoto 		amdtp_stream_pcm_trigger(&bebob->tx_stream, NULL);
319fbbebd2cSTakashi Sakamoto 		break;
320fbbebd2cSTakashi Sakamoto 	default:
321fbbebd2cSTakashi Sakamoto 		return -EINVAL;
322fbbebd2cSTakashi Sakamoto 	}
323fbbebd2cSTakashi Sakamoto 
324fbbebd2cSTakashi Sakamoto 	return 0;
325fbbebd2cSTakashi Sakamoto }
326fbbebd2cSTakashi Sakamoto static int
327fbbebd2cSTakashi Sakamoto pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
328fbbebd2cSTakashi Sakamoto {
329fbbebd2cSTakashi Sakamoto 	struct snd_bebob *bebob = substream->private_data;
330fbbebd2cSTakashi Sakamoto 
331fbbebd2cSTakashi Sakamoto 	switch (cmd) {
332fbbebd2cSTakashi Sakamoto 	case SNDRV_PCM_TRIGGER_START:
333fbbebd2cSTakashi Sakamoto 		amdtp_stream_pcm_trigger(&bebob->rx_stream, substream);
334fbbebd2cSTakashi Sakamoto 		break;
335fbbebd2cSTakashi Sakamoto 	case SNDRV_PCM_TRIGGER_STOP:
336fbbebd2cSTakashi Sakamoto 		amdtp_stream_pcm_trigger(&bebob->rx_stream, NULL);
337fbbebd2cSTakashi Sakamoto 		break;
338fbbebd2cSTakashi Sakamoto 	default:
339fbbebd2cSTakashi Sakamoto 		return -EINVAL;
340fbbebd2cSTakashi Sakamoto 	}
341fbbebd2cSTakashi Sakamoto 
342fbbebd2cSTakashi Sakamoto 	return 0;
343fbbebd2cSTakashi Sakamoto }
344fbbebd2cSTakashi Sakamoto 
345fbbebd2cSTakashi Sakamoto static snd_pcm_uframes_t
346fbbebd2cSTakashi Sakamoto pcm_capture_pointer(struct snd_pcm_substream *sbstrm)
347fbbebd2cSTakashi Sakamoto {
348fbbebd2cSTakashi Sakamoto 	struct snd_bebob *bebob = sbstrm->private_data;
349fbbebd2cSTakashi Sakamoto 	return amdtp_stream_pcm_pointer(&bebob->tx_stream);
350fbbebd2cSTakashi Sakamoto }
351fbbebd2cSTakashi Sakamoto static snd_pcm_uframes_t
352fbbebd2cSTakashi Sakamoto pcm_playback_pointer(struct snd_pcm_substream *sbstrm)
353fbbebd2cSTakashi Sakamoto {
354fbbebd2cSTakashi Sakamoto 	struct snd_bebob *bebob = sbstrm->private_data;
355fbbebd2cSTakashi Sakamoto 	return amdtp_stream_pcm_pointer(&bebob->rx_stream);
356fbbebd2cSTakashi Sakamoto }
357fbbebd2cSTakashi Sakamoto 
358*875becf8STakashi Sakamoto static int pcm_capture_ack(struct snd_pcm_substream *substream)
359*875becf8STakashi Sakamoto {
360*875becf8STakashi Sakamoto 	struct snd_bebob *bebob = substream->private_data;
361*875becf8STakashi Sakamoto 
362*875becf8STakashi Sakamoto 	return amdtp_stream_pcm_ack(&bebob->tx_stream);
363*875becf8STakashi Sakamoto }
364*875becf8STakashi Sakamoto 
365*875becf8STakashi Sakamoto static int pcm_playback_ack(struct snd_pcm_substream *substream)
366*875becf8STakashi Sakamoto {
367*875becf8STakashi Sakamoto 	struct snd_bebob *bebob = substream->private_data;
368*875becf8STakashi Sakamoto 
369*875becf8STakashi Sakamoto 	return amdtp_stream_pcm_ack(&bebob->rx_stream);
370*875becf8STakashi Sakamoto }
371*875becf8STakashi Sakamoto 
3724780f774STakashi Sakamoto int snd_bebob_create_pcm_devices(struct snd_bebob *bebob)
3734780f774STakashi Sakamoto {
3744780f774STakashi Sakamoto 	static const struct snd_pcm_ops capture_ops = {
375fbbebd2cSTakashi Sakamoto 		.open		= pcm_open,
376fbbebd2cSTakashi Sakamoto 		.close		= pcm_close,
377fbbebd2cSTakashi Sakamoto 		.ioctl		= snd_pcm_lib_ioctl,
378fbbebd2cSTakashi Sakamoto 		.hw_params	= pcm_capture_hw_params,
379fbbebd2cSTakashi Sakamoto 		.hw_free	= pcm_capture_hw_free,
380fbbebd2cSTakashi Sakamoto 		.prepare	= pcm_capture_prepare,
381fbbebd2cSTakashi Sakamoto 		.trigger	= pcm_capture_trigger,
382fbbebd2cSTakashi Sakamoto 		.pointer	= pcm_capture_pointer,
383*875becf8STakashi Sakamoto 		.ack		= pcm_capture_ack,
384fbbebd2cSTakashi Sakamoto 		.page		= snd_pcm_lib_get_vmalloc_page,
385fbbebd2cSTakashi Sakamoto 	};
3864780f774STakashi Sakamoto 	static const struct snd_pcm_ops playback_ops = {
387fbbebd2cSTakashi Sakamoto 		.open		= pcm_open,
388fbbebd2cSTakashi Sakamoto 		.close		= pcm_close,
389fbbebd2cSTakashi Sakamoto 		.ioctl		= snd_pcm_lib_ioctl,
390fbbebd2cSTakashi Sakamoto 		.hw_params	= pcm_playback_hw_params,
391fbbebd2cSTakashi Sakamoto 		.hw_free	= pcm_playback_hw_free,
392fbbebd2cSTakashi Sakamoto 		.prepare	= pcm_playback_prepare,
393fbbebd2cSTakashi Sakamoto 		.trigger	= pcm_playback_trigger,
394fbbebd2cSTakashi Sakamoto 		.pointer	= pcm_playback_pointer,
395*875becf8STakashi Sakamoto 		.ack		= pcm_playback_ack,
396fbbebd2cSTakashi Sakamoto 		.page		= snd_pcm_lib_get_vmalloc_page,
397fbbebd2cSTakashi Sakamoto 		.mmap		= snd_pcm_lib_mmap_vmalloc,
398fbbebd2cSTakashi Sakamoto 	};
399fbbebd2cSTakashi Sakamoto 	struct snd_pcm *pcm;
400fbbebd2cSTakashi Sakamoto 	int err;
401fbbebd2cSTakashi Sakamoto 
402fbbebd2cSTakashi Sakamoto 	err = snd_pcm_new(bebob->card, bebob->card->driver, 0, 1, 1, &pcm);
403fbbebd2cSTakashi Sakamoto 	if (err < 0)
404fbbebd2cSTakashi Sakamoto 		goto end;
405fbbebd2cSTakashi Sakamoto 
406fbbebd2cSTakashi Sakamoto 	pcm->private_data = bebob;
407fbbebd2cSTakashi Sakamoto 	snprintf(pcm->name, sizeof(pcm->name),
408fbbebd2cSTakashi Sakamoto 		 "%s PCM", bebob->card->shortname);
4094780f774STakashi Sakamoto 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &playback_ops);
4104780f774STakashi Sakamoto 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &capture_ops);
411fbbebd2cSTakashi Sakamoto end:
412fbbebd2cSTakashi Sakamoto 	return err;
413fbbebd2cSTakashi Sakamoto }
414