xref: /openbmc/linux/sound/firewire/bebob/bebob_pcm.c (revision ac2888b9)
1 /*
2  * bebob_pcm.c - a part of driver for BeBoB based devices
3  *
4  * Copyright (c) 2013-2014 Takashi Sakamoto
5  *
6  * Licensed under the terms of the GNU General Public License, version 2.
7  */
8 
9 #include "./bebob.h"
10 
11 static int
12 hw_rule_rate(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
13 {
14 	struct snd_bebob_stream_formation *formations = rule->private;
15 	struct snd_interval *r =
16 		hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
17 	const struct snd_interval *c =
18 		hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS);
19 	struct snd_interval t = {
20 		.min = UINT_MAX, .max = 0, .integer = 1
21 	};
22 	unsigned int i;
23 
24 	for (i = 0; i < SND_BEBOB_STRM_FMT_ENTRIES; i++) {
25 		/* entry is invalid */
26 		if (formations[i].pcm == 0)
27 			continue;
28 
29 		if (!snd_interval_test(c, formations[i].pcm))
30 			continue;
31 
32 		t.min = min(t.min, snd_bebob_rate_table[i]);
33 		t.max = max(t.max, snd_bebob_rate_table[i]);
34 
35 	}
36 	return snd_interval_refine(r, &t);
37 }
38 
39 static int
40 hw_rule_channels(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
41 {
42 	struct snd_bebob_stream_formation *formations = rule->private;
43 	struct snd_interval *c =
44 		hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
45 	const struct snd_interval *r =
46 		hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
47 	struct snd_interval t = {
48 		.min = UINT_MAX, .max = 0, .integer = 1
49 	};
50 
51 	unsigned int i;
52 
53 	for (i = 0; i < SND_BEBOB_STRM_FMT_ENTRIES; i++) {
54 		/* entry is invalid */
55 		if (formations[i].pcm == 0)
56 			continue;
57 
58 		if (!snd_interval_test(r, snd_bebob_rate_table[i]))
59 			continue;
60 
61 		t.min = min(t.min, formations[i].pcm);
62 		t.max = max(t.max, formations[i].pcm);
63 	}
64 
65 	return snd_interval_refine(c, &t);
66 }
67 
68 static void
69 limit_channels_and_rates(struct snd_pcm_hardware *hw,
70 			 struct snd_bebob_stream_formation *formations)
71 {
72 	unsigned int i;
73 
74 	hw->channels_min = UINT_MAX;
75 	hw->channels_max = 0;
76 
77 	hw->rate_min = UINT_MAX;
78 	hw->rate_max = 0;
79 	hw->rates = 0;
80 
81 	for (i = 0; i < SND_BEBOB_STRM_FMT_ENTRIES; i++) {
82 		/* entry has no PCM channels */
83 		if (formations[i].pcm == 0)
84 			continue;
85 
86 		hw->channels_min = min(hw->channels_min, formations[i].pcm);
87 		hw->channels_max = max(hw->channels_max, formations[i].pcm);
88 
89 		hw->rate_min = min(hw->rate_min, snd_bebob_rate_table[i]);
90 		hw->rate_max = max(hw->rate_max, snd_bebob_rate_table[i]);
91 		hw->rates |= snd_pcm_rate_to_rate_bit(snd_bebob_rate_table[i]);
92 	}
93 }
94 
95 static int
96 pcm_init_hw_params(struct snd_bebob *bebob,
97 		   struct snd_pcm_substream *substream)
98 {
99 	struct snd_pcm_runtime *runtime = substream->runtime;
100 	struct amdtp_stream *s;
101 	struct snd_bebob_stream_formation *formations;
102 	int err;
103 
104 	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
105 		runtime->hw.formats = AM824_IN_PCM_FORMAT_BITS;
106 		s = &bebob->tx_stream;
107 		formations = bebob->tx_stream_formations;
108 	} else {
109 		runtime->hw.formats = AM824_OUT_PCM_FORMAT_BITS;
110 		s = &bebob->rx_stream;
111 		formations = bebob->rx_stream_formations;
112 	}
113 
114 	limit_channels_and_rates(&runtime->hw, formations);
115 
116 	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
117 				  hw_rule_channels, formations,
118 				  SNDRV_PCM_HW_PARAM_RATE, -1);
119 	if (err < 0)
120 		goto end;
121 
122 	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
123 				  hw_rule_rate, formations,
124 				  SNDRV_PCM_HW_PARAM_CHANNELS, -1);
125 	if (err < 0)
126 		goto end;
127 
128 	err = amdtp_am824_add_pcm_hw_constraints(s, runtime);
129 end:
130 	return err;
131 }
132 
133 static int
134 pcm_open(struct snd_pcm_substream *substream)
135 {
136 	struct snd_bebob *bebob = substream->private_data;
137 	const struct snd_bebob_rate_spec *spec = bebob->spec->rate;
138 	unsigned int sampling_rate;
139 	enum snd_bebob_clock_type src;
140 	int err;
141 
142 	err = snd_bebob_stream_lock_try(bebob);
143 	if (err < 0)
144 		goto end;
145 
146 	err = pcm_init_hw_params(bebob, substream);
147 	if (err < 0)
148 		goto err_locked;
149 
150 	err = snd_bebob_stream_get_clock_src(bebob, &src);
151 	if (err < 0)
152 		goto err_locked;
153 
154 	/*
155 	 * When source of clock is internal or any PCM stream are running,
156 	 * the available sampling rate is limited at current sampling rate.
157 	 */
158 	if (src == SND_BEBOB_CLOCK_TYPE_EXTERNAL ||
159 	    amdtp_stream_pcm_running(&bebob->tx_stream) ||
160 	    amdtp_stream_pcm_running(&bebob->rx_stream)) {
161 		err = spec->get(bebob, &sampling_rate);
162 		if (err < 0) {
163 			dev_err(&bebob->unit->device,
164 				"fail to get sampling rate: %d\n", err);
165 			goto err_locked;
166 		}
167 
168 		substream->runtime->hw.rate_min = sampling_rate;
169 		substream->runtime->hw.rate_max = sampling_rate;
170 	}
171 
172 	snd_pcm_set_sync(substream);
173 end:
174 	return err;
175 err_locked:
176 	snd_bebob_stream_lock_release(bebob);
177 	return err;
178 }
179 
180 static int
181 pcm_close(struct snd_pcm_substream *substream)
182 {
183 	struct snd_bebob *bebob = substream->private_data;
184 	snd_bebob_stream_lock_release(bebob);
185 	return 0;
186 }
187 
188 static int
189 pcm_capture_hw_params(struct snd_pcm_substream *substream,
190 		      struct snd_pcm_hw_params *hw_params)
191 {
192 	struct snd_bebob *bebob = substream->private_data;
193 	int err;
194 
195 	err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
196 					       params_buffer_bytes(hw_params));
197 	if (err < 0)
198 		return err;
199 
200 	if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
201 		unsigned int rate = params_rate(hw_params);
202 
203 		mutex_lock(&bebob->mutex);
204 		err = snd_bebob_stream_reserve_duplex(bebob, rate);
205 		if (err >= 0)
206 			++bebob->substreams_counter;
207 		mutex_unlock(&bebob->mutex);
208 	}
209 
210 	return err;
211 }
212 static int
213 pcm_playback_hw_params(struct snd_pcm_substream *substream,
214 		       struct snd_pcm_hw_params *hw_params)
215 {
216 	struct snd_bebob *bebob = substream->private_data;
217 	int err;
218 
219 	err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
220 					       params_buffer_bytes(hw_params));
221 	if (err < 0)
222 		return err;
223 
224 	if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
225 		unsigned int rate = params_rate(hw_params);
226 
227 		mutex_lock(&bebob->mutex);
228 		err = snd_bebob_stream_reserve_duplex(bebob, rate);
229 		if (err >= 0)
230 			++bebob->substreams_counter;
231 		mutex_unlock(&bebob->mutex);
232 	}
233 
234 	return err;
235 }
236 
237 static int
238 pcm_capture_hw_free(struct snd_pcm_substream *substream)
239 {
240 	struct snd_bebob *bebob = substream->private_data;
241 
242 	if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN) {
243 		mutex_lock(&bebob->mutex);
244 		bebob->substreams_counter--;
245 		mutex_unlock(&bebob->mutex);
246 	}
247 
248 	snd_bebob_stream_stop_duplex(bebob);
249 
250 	return snd_pcm_lib_free_vmalloc_buffer(substream);
251 }
252 static int
253 pcm_playback_hw_free(struct snd_pcm_substream *substream)
254 {
255 	struct snd_bebob *bebob = substream->private_data;
256 
257 	if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN) {
258 		mutex_lock(&bebob->mutex);
259 		bebob->substreams_counter--;
260 		mutex_unlock(&bebob->mutex);
261 	}
262 
263 	snd_bebob_stream_stop_duplex(bebob);
264 
265 	return snd_pcm_lib_free_vmalloc_buffer(substream);
266 }
267 
268 static int
269 pcm_capture_prepare(struct snd_pcm_substream *substream)
270 {
271 	struct snd_bebob *bebob = substream->private_data;
272 	int err;
273 
274 	err = snd_bebob_stream_start_duplex(bebob);
275 	if (err >= 0)
276 		amdtp_stream_pcm_prepare(&bebob->tx_stream);
277 
278 	return err;
279 }
280 static int
281 pcm_playback_prepare(struct snd_pcm_substream *substream)
282 {
283 	struct snd_bebob *bebob = substream->private_data;
284 	int err;
285 
286 	err = snd_bebob_stream_start_duplex(bebob);
287 	if (err >= 0)
288 		amdtp_stream_pcm_prepare(&bebob->rx_stream);
289 
290 	return err;
291 }
292 
293 static int
294 pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
295 {
296 	struct snd_bebob *bebob = substream->private_data;
297 
298 	switch (cmd) {
299 	case SNDRV_PCM_TRIGGER_START:
300 		amdtp_stream_pcm_trigger(&bebob->tx_stream, substream);
301 		break;
302 	case SNDRV_PCM_TRIGGER_STOP:
303 		amdtp_stream_pcm_trigger(&bebob->tx_stream, NULL);
304 		break;
305 	default:
306 		return -EINVAL;
307 	}
308 
309 	return 0;
310 }
311 static int
312 pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
313 {
314 	struct snd_bebob *bebob = substream->private_data;
315 
316 	switch (cmd) {
317 	case SNDRV_PCM_TRIGGER_START:
318 		amdtp_stream_pcm_trigger(&bebob->rx_stream, substream);
319 		break;
320 	case SNDRV_PCM_TRIGGER_STOP:
321 		amdtp_stream_pcm_trigger(&bebob->rx_stream, NULL);
322 		break;
323 	default:
324 		return -EINVAL;
325 	}
326 
327 	return 0;
328 }
329 
330 static snd_pcm_uframes_t
331 pcm_capture_pointer(struct snd_pcm_substream *sbstrm)
332 {
333 	struct snd_bebob *bebob = sbstrm->private_data;
334 	return amdtp_stream_pcm_pointer(&bebob->tx_stream);
335 }
336 static snd_pcm_uframes_t
337 pcm_playback_pointer(struct snd_pcm_substream *sbstrm)
338 {
339 	struct snd_bebob *bebob = sbstrm->private_data;
340 	return amdtp_stream_pcm_pointer(&bebob->rx_stream);
341 }
342 
343 static int pcm_capture_ack(struct snd_pcm_substream *substream)
344 {
345 	struct snd_bebob *bebob = substream->private_data;
346 
347 	return amdtp_stream_pcm_ack(&bebob->tx_stream);
348 }
349 
350 static int pcm_playback_ack(struct snd_pcm_substream *substream)
351 {
352 	struct snd_bebob *bebob = substream->private_data;
353 
354 	return amdtp_stream_pcm_ack(&bebob->rx_stream);
355 }
356 
357 int snd_bebob_create_pcm_devices(struct snd_bebob *bebob)
358 {
359 	static const struct snd_pcm_ops capture_ops = {
360 		.open		= pcm_open,
361 		.close		= pcm_close,
362 		.ioctl		= snd_pcm_lib_ioctl,
363 		.hw_params	= pcm_capture_hw_params,
364 		.hw_free	= pcm_capture_hw_free,
365 		.prepare	= pcm_capture_prepare,
366 		.trigger	= pcm_capture_trigger,
367 		.pointer	= pcm_capture_pointer,
368 		.ack		= pcm_capture_ack,
369 		.page		= snd_pcm_lib_get_vmalloc_page,
370 	};
371 	static const struct snd_pcm_ops playback_ops = {
372 		.open		= pcm_open,
373 		.close		= pcm_close,
374 		.ioctl		= snd_pcm_lib_ioctl,
375 		.hw_params	= pcm_playback_hw_params,
376 		.hw_free	= pcm_playback_hw_free,
377 		.prepare	= pcm_playback_prepare,
378 		.trigger	= pcm_playback_trigger,
379 		.pointer	= pcm_playback_pointer,
380 		.ack		= pcm_playback_ack,
381 		.page		= snd_pcm_lib_get_vmalloc_page,
382 	};
383 	struct snd_pcm *pcm;
384 	int err;
385 
386 	err = snd_pcm_new(bebob->card, bebob->card->driver, 0, 1, 1, &pcm);
387 	if (err < 0)
388 		goto end;
389 
390 	pcm->private_data = bebob;
391 	snprintf(pcm->name, sizeof(pcm->name),
392 		 "%s PCM", bebob->card->shortname);
393 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &playback_ops);
394 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &capture_ops);
395 end:
396 	return err;
397 }
398