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