xref: /linux/sound/firewire/bebob/bebob_pcm.c (revision 1fc9522a084f36d042298ef15893cf07d66a54bc)
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) {
125fbbebd2cSTakashi Sakamoto 		runtime->hw.formats = AMDTP_IN_PCM_FORMAT_BITS;
126fbbebd2cSTakashi Sakamoto 		s = &bebob->tx_stream;
127fbbebd2cSTakashi Sakamoto 		formations = bebob->tx_stream_formations;
128fbbebd2cSTakashi Sakamoto 	} else {
129fbbebd2cSTakashi Sakamoto 		runtime->hw.formats = AMDTP_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 
149fbbebd2cSTakashi Sakamoto 	err = amdtp_stream_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;
158*1fc9522aSTakashi Sakamoto 	struct snd_bebob_rate_spec *spec = bebob->spec->rate;
159fbbebd2cSTakashi Sakamoto 	unsigned int sampling_rate;
160fbbebd2cSTakashi Sakamoto 	bool internal;
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 
171fbbebd2cSTakashi Sakamoto 	err = snd_bebob_stream_check_internal_clock(bebob, &internal);
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 	 */
179fbbebd2cSTakashi Sakamoto 	if (!internal ||
180fbbebd2cSTakashi Sakamoto 	    amdtp_stream_pcm_running(&bebob->tx_stream) ||
181fbbebd2cSTakashi Sakamoto 	    amdtp_stream_pcm_running(&bebob->rx_stream)) {
182*1fc9522aSTakashi 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;
214fbbebd2cSTakashi Sakamoto 
215fbbebd2cSTakashi Sakamoto 	if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN)
216fbbebd2cSTakashi Sakamoto 		atomic_inc(&bebob->capture_substreams);
217fbbebd2cSTakashi Sakamoto 	amdtp_stream_set_pcm_format(&bebob->tx_stream,
218fbbebd2cSTakashi Sakamoto 				    params_format(hw_params));
219fbbebd2cSTakashi Sakamoto 	return snd_pcm_lib_alloc_vmalloc_buffer(substream,
220fbbebd2cSTakashi Sakamoto 						params_buffer_bytes(hw_params));
221fbbebd2cSTakashi Sakamoto }
222fbbebd2cSTakashi Sakamoto static int
223fbbebd2cSTakashi Sakamoto pcm_playback_hw_params(struct snd_pcm_substream *substream,
224fbbebd2cSTakashi Sakamoto 		       struct snd_pcm_hw_params *hw_params)
225fbbebd2cSTakashi Sakamoto {
226fbbebd2cSTakashi Sakamoto 	struct snd_bebob *bebob = substream->private_data;
227fbbebd2cSTakashi Sakamoto 
228fbbebd2cSTakashi Sakamoto 	if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN)
229fbbebd2cSTakashi Sakamoto 		atomic_inc(&bebob->playback_substreams);
230fbbebd2cSTakashi Sakamoto 	amdtp_stream_set_pcm_format(&bebob->rx_stream,
231fbbebd2cSTakashi Sakamoto 				    params_format(hw_params));
232fbbebd2cSTakashi Sakamoto 	return snd_pcm_lib_alloc_vmalloc_buffer(substream,
233fbbebd2cSTakashi Sakamoto 						params_buffer_bytes(hw_params));
234fbbebd2cSTakashi Sakamoto }
235fbbebd2cSTakashi Sakamoto 
236fbbebd2cSTakashi Sakamoto static int
237fbbebd2cSTakashi Sakamoto pcm_capture_hw_free(struct snd_pcm_substream *substream)
238fbbebd2cSTakashi Sakamoto {
239fbbebd2cSTakashi Sakamoto 	struct snd_bebob *bebob = substream->private_data;
240fbbebd2cSTakashi Sakamoto 
241fbbebd2cSTakashi Sakamoto 	if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
242fbbebd2cSTakashi Sakamoto 		atomic_dec(&bebob->capture_substreams);
243fbbebd2cSTakashi Sakamoto 
244fbbebd2cSTakashi Sakamoto 	snd_bebob_stream_stop_duplex(bebob);
245fbbebd2cSTakashi Sakamoto 
246fbbebd2cSTakashi Sakamoto 	return snd_pcm_lib_free_vmalloc_buffer(substream);
247fbbebd2cSTakashi Sakamoto }
248fbbebd2cSTakashi Sakamoto static int
249fbbebd2cSTakashi Sakamoto pcm_playback_hw_free(struct snd_pcm_substream *substream)
250fbbebd2cSTakashi Sakamoto {
251fbbebd2cSTakashi Sakamoto 	struct snd_bebob *bebob = substream->private_data;
252fbbebd2cSTakashi Sakamoto 
253fbbebd2cSTakashi Sakamoto 	if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
254fbbebd2cSTakashi Sakamoto 		atomic_dec(&bebob->playback_substreams);
255fbbebd2cSTakashi Sakamoto 
256fbbebd2cSTakashi Sakamoto 	snd_bebob_stream_stop_duplex(bebob);
257fbbebd2cSTakashi Sakamoto 
258fbbebd2cSTakashi Sakamoto 	return snd_pcm_lib_free_vmalloc_buffer(substream);
259fbbebd2cSTakashi Sakamoto }
260fbbebd2cSTakashi Sakamoto 
261fbbebd2cSTakashi Sakamoto static int
262fbbebd2cSTakashi Sakamoto pcm_capture_prepare(struct snd_pcm_substream *substream)
263fbbebd2cSTakashi Sakamoto {
264fbbebd2cSTakashi Sakamoto 	struct snd_bebob *bebob = substream->private_data;
265fbbebd2cSTakashi Sakamoto 	struct snd_pcm_runtime *runtime = substream->runtime;
266fbbebd2cSTakashi Sakamoto 	int err;
267fbbebd2cSTakashi Sakamoto 
268fbbebd2cSTakashi Sakamoto 	err = snd_bebob_stream_start_duplex(bebob, runtime->rate);
269fbbebd2cSTakashi Sakamoto 	if (err >= 0)
270fbbebd2cSTakashi Sakamoto 		amdtp_stream_pcm_prepare(&bebob->tx_stream);
271fbbebd2cSTakashi Sakamoto 
272fbbebd2cSTakashi Sakamoto 	return err;
273fbbebd2cSTakashi Sakamoto }
274fbbebd2cSTakashi Sakamoto static int
275fbbebd2cSTakashi Sakamoto pcm_playback_prepare(struct snd_pcm_substream *substream)
276fbbebd2cSTakashi Sakamoto {
277fbbebd2cSTakashi Sakamoto 	struct snd_bebob *bebob = substream->private_data;
278fbbebd2cSTakashi Sakamoto 	struct snd_pcm_runtime *runtime = substream->runtime;
279fbbebd2cSTakashi Sakamoto 	int err;
280fbbebd2cSTakashi Sakamoto 
281fbbebd2cSTakashi Sakamoto 	err = snd_bebob_stream_start_duplex(bebob, runtime->rate);
282fbbebd2cSTakashi Sakamoto 	if (err >= 0)
283fbbebd2cSTakashi Sakamoto 		amdtp_stream_pcm_prepare(&bebob->rx_stream);
284fbbebd2cSTakashi Sakamoto 
285fbbebd2cSTakashi Sakamoto 	return err;
286fbbebd2cSTakashi Sakamoto }
287fbbebd2cSTakashi Sakamoto 
288fbbebd2cSTakashi Sakamoto static int
289fbbebd2cSTakashi Sakamoto pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
290fbbebd2cSTakashi Sakamoto {
291fbbebd2cSTakashi Sakamoto 	struct snd_bebob *bebob = substream->private_data;
292fbbebd2cSTakashi Sakamoto 
293fbbebd2cSTakashi Sakamoto 	switch (cmd) {
294fbbebd2cSTakashi Sakamoto 	case SNDRV_PCM_TRIGGER_START:
295fbbebd2cSTakashi Sakamoto 		amdtp_stream_pcm_trigger(&bebob->tx_stream, substream);
296fbbebd2cSTakashi Sakamoto 		break;
297fbbebd2cSTakashi Sakamoto 	case SNDRV_PCM_TRIGGER_STOP:
298fbbebd2cSTakashi Sakamoto 		amdtp_stream_pcm_trigger(&bebob->tx_stream, NULL);
299fbbebd2cSTakashi Sakamoto 		break;
300fbbebd2cSTakashi Sakamoto 	default:
301fbbebd2cSTakashi Sakamoto 		return -EINVAL;
302fbbebd2cSTakashi Sakamoto 	}
303fbbebd2cSTakashi Sakamoto 
304fbbebd2cSTakashi Sakamoto 	return 0;
305fbbebd2cSTakashi Sakamoto }
306fbbebd2cSTakashi Sakamoto static int
307fbbebd2cSTakashi Sakamoto pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
308fbbebd2cSTakashi Sakamoto {
309fbbebd2cSTakashi Sakamoto 	struct snd_bebob *bebob = substream->private_data;
310fbbebd2cSTakashi Sakamoto 
311fbbebd2cSTakashi Sakamoto 	switch (cmd) {
312fbbebd2cSTakashi Sakamoto 	case SNDRV_PCM_TRIGGER_START:
313fbbebd2cSTakashi Sakamoto 		amdtp_stream_pcm_trigger(&bebob->rx_stream, substream);
314fbbebd2cSTakashi Sakamoto 		break;
315fbbebd2cSTakashi Sakamoto 	case SNDRV_PCM_TRIGGER_STOP:
316fbbebd2cSTakashi Sakamoto 		amdtp_stream_pcm_trigger(&bebob->rx_stream, NULL);
317fbbebd2cSTakashi Sakamoto 		break;
318fbbebd2cSTakashi Sakamoto 	default:
319fbbebd2cSTakashi Sakamoto 		return -EINVAL;
320fbbebd2cSTakashi Sakamoto 	}
321fbbebd2cSTakashi Sakamoto 
322fbbebd2cSTakashi Sakamoto 	return 0;
323fbbebd2cSTakashi Sakamoto }
324fbbebd2cSTakashi Sakamoto 
325fbbebd2cSTakashi Sakamoto static snd_pcm_uframes_t
326fbbebd2cSTakashi Sakamoto pcm_capture_pointer(struct snd_pcm_substream *sbstrm)
327fbbebd2cSTakashi Sakamoto {
328fbbebd2cSTakashi Sakamoto 	struct snd_bebob *bebob = sbstrm->private_data;
329fbbebd2cSTakashi Sakamoto 	return amdtp_stream_pcm_pointer(&bebob->tx_stream);
330fbbebd2cSTakashi Sakamoto }
331fbbebd2cSTakashi Sakamoto static snd_pcm_uframes_t
332fbbebd2cSTakashi Sakamoto pcm_playback_pointer(struct snd_pcm_substream *sbstrm)
333fbbebd2cSTakashi Sakamoto {
334fbbebd2cSTakashi Sakamoto 	struct snd_bebob *bebob = sbstrm->private_data;
335fbbebd2cSTakashi Sakamoto 	return amdtp_stream_pcm_pointer(&bebob->rx_stream);
336fbbebd2cSTakashi Sakamoto }
337fbbebd2cSTakashi Sakamoto 
338fbbebd2cSTakashi Sakamoto static const struct snd_pcm_ops pcm_capture_ops = {
339fbbebd2cSTakashi Sakamoto 	.open		= pcm_open,
340fbbebd2cSTakashi Sakamoto 	.close		= pcm_close,
341fbbebd2cSTakashi Sakamoto 	.ioctl		= snd_pcm_lib_ioctl,
342fbbebd2cSTakashi Sakamoto 	.hw_params	= pcm_capture_hw_params,
343fbbebd2cSTakashi Sakamoto 	.hw_free	= pcm_capture_hw_free,
344fbbebd2cSTakashi Sakamoto 	.prepare	= pcm_capture_prepare,
345fbbebd2cSTakashi Sakamoto 	.trigger	= pcm_capture_trigger,
346fbbebd2cSTakashi Sakamoto 	.pointer	= pcm_capture_pointer,
347fbbebd2cSTakashi Sakamoto 	.page		= snd_pcm_lib_get_vmalloc_page,
348fbbebd2cSTakashi Sakamoto };
349fbbebd2cSTakashi Sakamoto static const struct snd_pcm_ops pcm_playback_ops = {
350fbbebd2cSTakashi Sakamoto 	.open		= pcm_open,
351fbbebd2cSTakashi Sakamoto 	.close		= pcm_close,
352fbbebd2cSTakashi Sakamoto 	.ioctl		= snd_pcm_lib_ioctl,
353fbbebd2cSTakashi Sakamoto 	.hw_params	= pcm_playback_hw_params,
354fbbebd2cSTakashi Sakamoto 	.hw_free	= pcm_playback_hw_free,
355fbbebd2cSTakashi Sakamoto 	.prepare	= pcm_playback_prepare,
356fbbebd2cSTakashi Sakamoto 	.trigger	= pcm_playback_trigger,
357fbbebd2cSTakashi Sakamoto 	.pointer	= pcm_playback_pointer,
358fbbebd2cSTakashi Sakamoto 	.page		= snd_pcm_lib_get_vmalloc_page,
359fbbebd2cSTakashi Sakamoto 	.mmap		= snd_pcm_lib_mmap_vmalloc,
360fbbebd2cSTakashi Sakamoto };
361fbbebd2cSTakashi Sakamoto 
362fbbebd2cSTakashi Sakamoto int snd_bebob_create_pcm_devices(struct snd_bebob *bebob)
363fbbebd2cSTakashi Sakamoto {
364fbbebd2cSTakashi Sakamoto 	struct snd_pcm *pcm;
365fbbebd2cSTakashi Sakamoto 	int err;
366fbbebd2cSTakashi Sakamoto 
367fbbebd2cSTakashi Sakamoto 	err = snd_pcm_new(bebob->card, bebob->card->driver, 0, 1, 1, &pcm);
368fbbebd2cSTakashi Sakamoto 	if (err < 0)
369fbbebd2cSTakashi Sakamoto 		goto end;
370fbbebd2cSTakashi Sakamoto 
371fbbebd2cSTakashi Sakamoto 	pcm->private_data = bebob;
372fbbebd2cSTakashi Sakamoto 	snprintf(pcm->name, sizeof(pcm->name),
373fbbebd2cSTakashi Sakamoto 		 "%s PCM", bebob->card->shortname);
374fbbebd2cSTakashi Sakamoto 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &pcm_playback_ops);
375fbbebd2cSTakashi Sakamoto 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &pcm_capture_ops);
376fbbebd2cSTakashi Sakamoto end:
377fbbebd2cSTakashi Sakamoto 	return err;
378fbbebd2cSTakashi Sakamoto }
379