blob: ea9b8645058072c03e6f8f654088282146f9e893 [file] [log] [blame]
Takashi Sakamotofbbebd22014-04-25 22:45:19 +09001/*
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
11static int
12hw_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
39static int
40hw_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
68static void
69limit_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
Takashi Sakamotofbbebd22014-04-25 22:45:19 +090095static int
96pcm_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
Takashi Sakamotofbbebd22014-04-25 22:45:19 +0900104 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
Takashi Sakamoto49c7b3f2015-09-19 11:22:01 +0900105 runtime->hw.formats = AM824_IN_PCM_FORMAT_BITS;
Takashi Sakamotofbbebd22014-04-25 22:45:19 +0900106 s = &bebob->tx_stream;
107 formations = bebob->tx_stream_formations;
108 } else {
Takashi Sakamoto49c7b3f2015-09-19 11:22:01 +0900109 runtime->hw.formats = AM824_OUT_PCM_FORMAT_BITS;
Takashi Sakamotofbbebd22014-04-25 22:45:19 +0900110 s = &bebob->rx_stream;
111 formations = bebob->rx_stream_formations;
112 }
113
114 limit_channels_and_rates(&runtime->hw, formations);
Takashi Sakamotofbbebd22014-04-25 22:45:19 +0900115
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
Takashi Sakamotobc8500d2015-09-19 11:21:57 +0900128 err = amdtp_am824_add_pcm_hw_constraints(s, runtime);
Takashi Sakamotofbbebd22014-04-25 22:45:19 +0900129end:
130 return err;
131}
132
133static int
134pcm_open(struct snd_pcm_substream *substream)
135{
136 struct snd_bebob *bebob = substream->private_data;
Julia Lawall6b9866c2015-10-11 08:10:55 +0200137 const struct snd_bebob_rate_spec *spec = bebob->spec->rate;
Takashi Sakamotofbbebd22014-04-25 22:45:19 +0900138 unsigned int sampling_rate;
Takashi Sakamoto3e254b12015-06-14 12:49:30 +0900139 enum snd_bebob_clock_type src;
Takashi Sakamotofbbebd22014-04-25 22:45:19 +0900140 int err;
141
Takashi Sakamoto618eabe2014-04-25 22:45:20 +0900142 err = snd_bebob_stream_lock_try(bebob);
Takashi Sakamotofbbebd22014-04-25 22:45:19 +0900143 if (err < 0)
144 goto end;
145
Takashi Sakamoto618eabe2014-04-25 22:45:20 +0900146 err = pcm_init_hw_params(bebob, substream);
147 if (err < 0)
148 goto err_locked;
149
Takashi Sakamoto3e254b12015-06-14 12:49:30 +0900150 err = snd_bebob_stream_get_clock_src(bebob, &src);
Takashi Sakamotofbbebd22014-04-25 22:45:19 +0900151 if (err < 0)
Takashi Sakamoto618eabe2014-04-25 22:45:20 +0900152 goto err_locked;
Takashi Sakamotofbbebd22014-04-25 22:45:19 +0900153
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 */
Takashi Sakamoto3e254b12015-06-14 12:49:30 +0900158 if (src == SND_BEBOB_CLOCK_TYPE_EXTERNAL ||
Takashi Sakamotofbbebd22014-04-25 22:45:19 +0900159 amdtp_stream_pcm_running(&bebob->tx_stream) ||
160 amdtp_stream_pcm_running(&bebob->rx_stream)) {
Takashi Sakamoto1fc95222014-04-25 22:45:21 +0900161 err = spec->get(bebob, &sampling_rate);
Takashi Sakamotofbbebd22014-04-25 22:45:19 +0900162 if (err < 0) {
163 dev_err(&bebob->unit->device,
164 "fail to get sampling rate: %d\n", err);
Takashi Sakamoto618eabe2014-04-25 22:45:20 +0900165 goto err_locked;
Takashi Sakamotofbbebd22014-04-25 22:45:19 +0900166 }
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);
Takashi Sakamotofbbebd22014-04-25 22:45:19 +0900173end:
174 return err;
Takashi Sakamoto618eabe2014-04-25 22:45:20 +0900175err_locked:
176 snd_bebob_stream_lock_release(bebob);
177 return err;
Takashi Sakamotofbbebd22014-04-25 22:45:19 +0900178}
179
180static int
181pcm_close(struct snd_pcm_substream *substream)
182{
Takashi Sakamoto618eabe2014-04-25 22:45:20 +0900183 struct snd_bebob *bebob = substream->private_data;
184 snd_bebob_stream_lock_release(bebob);
Takashi Sakamotofbbebd22014-04-25 22:45:19 +0900185 return 0;
186}
187
188static int
189pcm_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;
Takashi Sakamoto22c103c2015-08-29 10:38:46 +0900193 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;
Takashi Sakamotofbbebd22014-04-25 22:45:19 +0900199
Takashi Sakamoto2a71e702016-02-20 16:18:57 +0900200 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
201 mutex_lock(&bebob->mutex);
Takashi Sakamoto4fd6c6c2016-02-20 16:18:58 +0900202 bebob->substreams_counter++;
Takashi Sakamoto2a71e702016-02-20 16:18:57 +0900203 mutex_unlock(&bebob->mutex);
204 }
Takashi Sakamoto85130cb2015-09-19 11:22:00 +0900205
Takashi Sakamoto22c103c2015-08-29 10:38:46 +0900206 return 0;
Takashi Sakamotofbbebd22014-04-25 22:45:19 +0900207}
208static int
209pcm_playback_hw_params(struct snd_pcm_substream *substream,
210 struct snd_pcm_hw_params *hw_params)
211{
212 struct snd_bebob *bebob = substream->private_data;
Takashi Sakamoto22c103c2015-08-29 10:38:46 +0900213 int err;
214
215 err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
216 params_buffer_bytes(hw_params));
217 if (err < 0)
218 return err;
Takashi Sakamotofbbebd22014-04-25 22:45:19 +0900219
Takashi Sakamoto2a71e702016-02-20 16:18:57 +0900220 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
221 mutex_lock(&bebob->mutex);
Takashi Sakamoto4fd6c6c2016-02-20 16:18:58 +0900222 bebob->substreams_counter++;
Takashi Sakamoto2a71e702016-02-20 16:18:57 +0900223 mutex_unlock(&bebob->mutex);
224 }
Takashi Sakamoto85130cb2015-09-19 11:22:00 +0900225
Takashi Sakamoto22c103c2015-08-29 10:38:46 +0900226 return 0;
Takashi Sakamotofbbebd22014-04-25 22:45:19 +0900227}
228
229static int
230pcm_capture_hw_free(struct snd_pcm_substream *substream)
231{
232 struct snd_bebob *bebob = substream->private_data;
233
Takashi Sakamoto2a71e702016-02-20 16:18:57 +0900234 if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN) {
235 mutex_lock(&bebob->mutex);
Takashi Sakamoto4fd6c6c2016-02-20 16:18:58 +0900236 bebob->substreams_counter--;
Takashi Sakamoto2a71e702016-02-20 16:18:57 +0900237 mutex_unlock(&bebob->mutex);
238 }
Takashi Sakamotofbbebd22014-04-25 22:45:19 +0900239
240 snd_bebob_stream_stop_duplex(bebob);
241
242 return snd_pcm_lib_free_vmalloc_buffer(substream);
243}
244static int
245pcm_playback_hw_free(struct snd_pcm_substream *substream)
246{
247 struct snd_bebob *bebob = substream->private_data;
248
Takashi Sakamoto2a71e702016-02-20 16:18:57 +0900249 if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN) {
250 mutex_lock(&bebob->mutex);
Takashi Sakamoto4fd6c6c2016-02-20 16:18:58 +0900251 bebob->substreams_counter--;
Takashi Sakamoto2a71e702016-02-20 16:18:57 +0900252 mutex_unlock(&bebob->mutex);
253 }
Takashi Sakamotofbbebd22014-04-25 22:45:19 +0900254
255 snd_bebob_stream_stop_duplex(bebob);
256
257 return snd_pcm_lib_free_vmalloc_buffer(substream);
258}
259
260static int
261pcm_capture_prepare(struct snd_pcm_substream *substream)
262{
263 struct snd_bebob *bebob = substream->private_data;
264 struct snd_pcm_runtime *runtime = substream->runtime;
265 int err;
266
267 err = snd_bebob_stream_start_duplex(bebob, runtime->rate);
268 if (err >= 0)
269 amdtp_stream_pcm_prepare(&bebob->tx_stream);
270
271 return err;
272}
273static int
274pcm_playback_prepare(struct snd_pcm_substream *substream)
275{
276 struct snd_bebob *bebob = substream->private_data;
277 struct snd_pcm_runtime *runtime = substream->runtime;
278 int err;
279
280 err = snd_bebob_stream_start_duplex(bebob, runtime->rate);
281 if (err >= 0)
282 amdtp_stream_pcm_prepare(&bebob->rx_stream);
283
284 return err;
285}
286
287static int
288pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
289{
290 struct snd_bebob *bebob = substream->private_data;
291
292 switch (cmd) {
293 case SNDRV_PCM_TRIGGER_START:
294 amdtp_stream_pcm_trigger(&bebob->tx_stream, substream);
295 break;
296 case SNDRV_PCM_TRIGGER_STOP:
297 amdtp_stream_pcm_trigger(&bebob->tx_stream, NULL);
298 break;
299 default:
300 return -EINVAL;
301 }
302
303 return 0;
304}
305static int
306pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
307{
308 struct snd_bebob *bebob = substream->private_data;
309
310 switch (cmd) {
311 case SNDRV_PCM_TRIGGER_START:
312 amdtp_stream_pcm_trigger(&bebob->rx_stream, substream);
313 break;
314 case SNDRV_PCM_TRIGGER_STOP:
315 amdtp_stream_pcm_trigger(&bebob->rx_stream, NULL);
316 break;
317 default:
318 return -EINVAL;
319 }
320
321 return 0;
322}
323
324static snd_pcm_uframes_t
325pcm_capture_pointer(struct snd_pcm_substream *sbstrm)
326{
327 struct snd_bebob *bebob = sbstrm->private_data;
328 return amdtp_stream_pcm_pointer(&bebob->tx_stream);
329}
330static snd_pcm_uframes_t
331pcm_playback_pointer(struct snd_pcm_substream *sbstrm)
332{
333 struct snd_bebob *bebob = sbstrm->private_data;
334 return amdtp_stream_pcm_pointer(&bebob->rx_stream);
335}
336
Takashi Sakamoto875becf2017-06-07 09:38:05 +0900337static int pcm_capture_ack(struct snd_pcm_substream *substream)
338{
339 struct snd_bebob *bebob = substream->private_data;
340
341 return amdtp_stream_pcm_ack(&bebob->tx_stream);
342}
343
344static int pcm_playback_ack(struct snd_pcm_substream *substream)
345{
346 struct snd_bebob *bebob = substream->private_data;
347
348 return amdtp_stream_pcm_ack(&bebob->rx_stream);
349}
350
Takashi Sakamotofbbebd22014-04-25 22:45:19 +0900351int snd_bebob_create_pcm_devices(struct snd_bebob *bebob)
352{
Takashi Sakamoto4780f772017-01-05 21:48:07 +0900353 static const struct snd_pcm_ops capture_ops = {
354 .open = pcm_open,
355 .close = pcm_close,
356 .ioctl = snd_pcm_lib_ioctl,
357 .hw_params = pcm_capture_hw_params,
358 .hw_free = pcm_capture_hw_free,
359 .prepare = pcm_capture_prepare,
360 .trigger = pcm_capture_trigger,
361 .pointer = pcm_capture_pointer,
Takashi Sakamoto875becf2017-06-07 09:38:05 +0900362 .ack = pcm_capture_ack,
Takashi Sakamoto4780f772017-01-05 21:48:07 +0900363 .page = snd_pcm_lib_get_vmalloc_page,
364 };
365 static const struct snd_pcm_ops playback_ops = {
366 .open = pcm_open,
367 .close = pcm_close,
368 .ioctl = snd_pcm_lib_ioctl,
369 .hw_params = pcm_playback_hw_params,
370 .hw_free = pcm_playback_hw_free,
371 .prepare = pcm_playback_prepare,
372 .trigger = pcm_playback_trigger,
373 .pointer = pcm_playback_pointer,
Takashi Sakamoto875becf2017-06-07 09:38:05 +0900374 .ack = pcm_playback_ack,
Takashi Sakamoto4780f772017-01-05 21:48:07 +0900375 .page = snd_pcm_lib_get_vmalloc_page,
Takashi Sakamoto4780f772017-01-05 21:48:07 +0900376 };
Takashi Sakamotofbbebd22014-04-25 22:45:19 +0900377 struct snd_pcm *pcm;
378 int err;
379
380 err = snd_pcm_new(bebob->card, bebob->card->driver, 0, 1, 1, &pcm);
381 if (err < 0)
382 goto end;
383
384 pcm->private_data = bebob;
385 snprintf(pcm->name, sizeof(pcm->name),
386 "%s PCM", bebob->card->shortname);
Takashi Sakamoto4780f772017-01-05 21:48:07 +0900387 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &playback_ops);
388 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &capture_ops);
Takashi Sakamotofbbebd22014-04-25 22:45:19 +0900389end:
390 return err;
391}