blob: 5d7b9343fa85683268b6f6d6bfdfdeabe58e39cf [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
95static void
96limit_period_and_buffer(struct snd_pcm_hardware *hw)
97{
98 hw->periods_min = 2; /* SNDRV_PCM_INFO_BATCH */
99 hw->periods_max = UINT_MAX;
100
101 hw->period_bytes_min = 4 * hw->channels_max; /* bytes for a frame */
102
103 /* Just to prevent from allocating much pages. */
104 hw->period_bytes_max = hw->period_bytes_min * 2048;
105 hw->buffer_bytes_max = hw->period_bytes_max * hw->periods_min;
106}
107
108static int
109pcm_init_hw_params(struct snd_bebob *bebob,
110 struct snd_pcm_substream *substream)
111{
112 struct snd_pcm_runtime *runtime = substream->runtime;
113 struct amdtp_stream *s;
114 struct snd_bebob_stream_formation *formations;
115 int err;
116
117 runtime->hw.info = SNDRV_PCM_INFO_BATCH |
118 SNDRV_PCM_INFO_BLOCK_TRANSFER |
119 SNDRV_PCM_INFO_INTERLEAVED |
120 SNDRV_PCM_INFO_JOINT_DUPLEX |
121 SNDRV_PCM_INFO_MMAP |
122 SNDRV_PCM_INFO_MMAP_VALID;
123
124 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
Takashi Sakamoto49c7b3f2015-09-19 11:22:01 +0900125 runtime->hw.formats = AM824_IN_PCM_FORMAT_BITS;
Takashi Sakamotofbbebd22014-04-25 22:45:19 +0900126 s = &bebob->tx_stream;
127 formations = bebob->tx_stream_formations;
128 } else {
Takashi Sakamoto49c7b3f2015-09-19 11:22:01 +0900129 runtime->hw.formats = AM824_OUT_PCM_FORMAT_BITS;
Takashi Sakamotofbbebd22014-04-25 22:45:19 +0900130 s = &bebob->rx_stream;
131 formations = bebob->rx_stream_formations;
132 }
133
134 limit_channels_and_rates(&runtime->hw, formations);
135 limit_period_and_buffer(&runtime->hw);
136
137 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
138 hw_rule_channels, formations,
139 SNDRV_PCM_HW_PARAM_RATE, -1);
140 if (err < 0)
141 goto end;
142
143 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
144 hw_rule_rate, formations,
145 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
146 if (err < 0)
147 goto end;
148
Takashi Sakamotobc8500d2015-09-19 11:21:57 +0900149 err = amdtp_am824_add_pcm_hw_constraints(s, runtime);
Takashi Sakamotofbbebd22014-04-25 22:45:19 +0900150end:
151 return err;
152}
153
154static int
155pcm_open(struct snd_pcm_substream *substream)
156{
157 struct snd_bebob *bebob = substream->private_data;
Julia Lawall6b9866c2015-10-11 08:10:55 +0200158 const struct snd_bebob_rate_spec *spec = bebob->spec->rate;
Takashi Sakamotofbbebd22014-04-25 22:45:19 +0900159 unsigned int sampling_rate;
Takashi Sakamoto3e254b12015-06-14 12:49:30 +0900160 enum snd_bebob_clock_type src;
Takashi Sakamotofbbebd22014-04-25 22:45:19 +0900161 int err;
162
Takashi Sakamoto618eabe2014-04-25 22:45:20 +0900163 err = snd_bebob_stream_lock_try(bebob);
Takashi Sakamotofbbebd22014-04-25 22:45:19 +0900164 if (err < 0)
165 goto end;
166
Takashi Sakamoto618eabe2014-04-25 22:45:20 +0900167 err = pcm_init_hw_params(bebob, substream);
168 if (err < 0)
169 goto err_locked;
170
Takashi Sakamoto3e254b12015-06-14 12:49:30 +0900171 err = snd_bebob_stream_get_clock_src(bebob, &src);
Takashi Sakamotofbbebd22014-04-25 22:45:19 +0900172 if (err < 0)
Takashi Sakamoto618eabe2014-04-25 22:45:20 +0900173 goto err_locked;
Takashi Sakamotofbbebd22014-04-25 22:45:19 +0900174
175 /*
176 * When source of clock is internal or any PCM stream are running,
177 * the available sampling rate is limited at current sampling rate.
178 */
Takashi Sakamoto3e254b12015-06-14 12:49:30 +0900179 if (src == SND_BEBOB_CLOCK_TYPE_EXTERNAL ||
Takashi Sakamotofbbebd22014-04-25 22:45:19 +0900180 amdtp_stream_pcm_running(&bebob->tx_stream) ||
181 amdtp_stream_pcm_running(&bebob->rx_stream)) {
Takashi Sakamoto1fc95222014-04-25 22:45:21 +0900182 err = spec->get(bebob, &sampling_rate);
Takashi Sakamotofbbebd22014-04-25 22:45:19 +0900183 if (err < 0) {
184 dev_err(&bebob->unit->device,
185 "fail to get sampling rate: %d\n", err);
Takashi Sakamoto618eabe2014-04-25 22:45:20 +0900186 goto err_locked;
Takashi Sakamotofbbebd22014-04-25 22:45:19 +0900187 }
188
189 substream->runtime->hw.rate_min = sampling_rate;
190 substream->runtime->hw.rate_max = sampling_rate;
191 }
192
193 snd_pcm_set_sync(substream);
Takashi Sakamotofbbebd22014-04-25 22:45:19 +0900194end:
195 return err;
Takashi Sakamoto618eabe2014-04-25 22:45:20 +0900196err_locked:
197 snd_bebob_stream_lock_release(bebob);
198 return err;
Takashi Sakamotofbbebd22014-04-25 22:45:19 +0900199}
200
201static int
202pcm_close(struct snd_pcm_substream *substream)
203{
Takashi Sakamoto618eabe2014-04-25 22:45:20 +0900204 struct snd_bebob *bebob = substream->private_data;
205 snd_bebob_stream_lock_release(bebob);
Takashi Sakamotofbbebd22014-04-25 22:45:19 +0900206 return 0;
207}
208
209static int
210pcm_capture_hw_params(struct snd_pcm_substream *substream,
211 struct snd_pcm_hw_params *hw_params)
212{
213 struct snd_bebob *bebob = substream->private_data;
Takashi Sakamoto22c103c2015-08-29 10:38:46 +0900214 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;
Takashi Sakamotofbbebd22014-04-25 22:45:19 +0900220
Takashi Sakamoto2a71e702016-02-20 16:18:57 +0900221 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
222 mutex_lock(&bebob->mutex);
Takashi Sakamoto4fd6c6c2016-02-20 16:18:58 +0900223 bebob->substreams_counter++;
Takashi Sakamoto2a71e702016-02-20 16:18:57 +0900224 mutex_unlock(&bebob->mutex);
225 }
Takashi Sakamoto85130cb2015-09-19 11:22:00 +0900226
227 amdtp_am824_set_pcm_format(&bebob->tx_stream, params_format(hw_params));
Takashi Sakamoto22c103c2015-08-29 10:38:46 +0900228
229 return 0;
Takashi Sakamotofbbebd22014-04-25 22:45:19 +0900230}
231static int
232pcm_playback_hw_params(struct snd_pcm_substream *substream,
233 struct snd_pcm_hw_params *hw_params)
234{
235 struct snd_bebob *bebob = substream->private_data;
Takashi Sakamoto22c103c2015-08-29 10:38:46 +0900236 int err;
237
238 err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
239 params_buffer_bytes(hw_params));
240 if (err < 0)
241 return err;
Takashi Sakamotofbbebd22014-04-25 22:45:19 +0900242
Takashi Sakamoto2a71e702016-02-20 16:18:57 +0900243 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
244 mutex_lock(&bebob->mutex);
Takashi Sakamoto4fd6c6c2016-02-20 16:18:58 +0900245 bebob->substreams_counter++;
Takashi Sakamoto2a71e702016-02-20 16:18:57 +0900246 mutex_unlock(&bebob->mutex);
247 }
Takashi Sakamoto85130cb2015-09-19 11:22:00 +0900248
249 amdtp_am824_set_pcm_format(&bebob->rx_stream, params_format(hw_params));
Takashi Sakamoto22c103c2015-08-29 10:38:46 +0900250
251 return 0;
Takashi Sakamotofbbebd22014-04-25 22:45:19 +0900252}
253
254static int
255pcm_capture_hw_free(struct snd_pcm_substream *substream)
256{
257 struct snd_bebob *bebob = substream->private_data;
258
Takashi Sakamoto2a71e702016-02-20 16:18:57 +0900259 if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN) {
260 mutex_lock(&bebob->mutex);
Takashi Sakamoto4fd6c6c2016-02-20 16:18:58 +0900261 bebob->substreams_counter--;
Takashi Sakamoto2a71e702016-02-20 16:18:57 +0900262 mutex_unlock(&bebob->mutex);
263 }
Takashi Sakamotofbbebd22014-04-25 22:45:19 +0900264
265 snd_bebob_stream_stop_duplex(bebob);
266
267 return snd_pcm_lib_free_vmalloc_buffer(substream);
268}
269static int
270pcm_playback_hw_free(struct snd_pcm_substream *substream)
271{
272 struct snd_bebob *bebob = substream->private_data;
273
Takashi Sakamoto2a71e702016-02-20 16:18:57 +0900274 if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN) {
275 mutex_lock(&bebob->mutex);
Takashi Sakamoto4fd6c6c2016-02-20 16:18:58 +0900276 bebob->substreams_counter--;
Takashi Sakamoto2a71e702016-02-20 16:18:57 +0900277 mutex_unlock(&bebob->mutex);
278 }
Takashi Sakamotofbbebd22014-04-25 22:45:19 +0900279
280 snd_bebob_stream_stop_duplex(bebob);
281
282 return snd_pcm_lib_free_vmalloc_buffer(substream);
283}
284
285static int
286pcm_capture_prepare(struct snd_pcm_substream *substream)
287{
288 struct snd_bebob *bebob = substream->private_data;
289 struct snd_pcm_runtime *runtime = substream->runtime;
290 int err;
291
292 err = snd_bebob_stream_start_duplex(bebob, runtime->rate);
293 if (err >= 0)
294 amdtp_stream_pcm_prepare(&bebob->tx_stream);
295
296 return err;
297}
298static int
299pcm_playback_prepare(struct snd_pcm_substream *substream)
300{
301 struct snd_bebob *bebob = substream->private_data;
302 struct snd_pcm_runtime *runtime = substream->runtime;
303 int err;
304
305 err = snd_bebob_stream_start_duplex(bebob, runtime->rate);
306 if (err >= 0)
307 amdtp_stream_pcm_prepare(&bebob->rx_stream);
308
309 return err;
310}
311
312static int
313pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
314{
315 struct snd_bebob *bebob = substream->private_data;
316
317 switch (cmd) {
318 case SNDRV_PCM_TRIGGER_START:
319 amdtp_stream_pcm_trigger(&bebob->tx_stream, substream);
320 break;
321 case SNDRV_PCM_TRIGGER_STOP:
322 amdtp_stream_pcm_trigger(&bebob->tx_stream, NULL);
323 break;
324 default:
325 return -EINVAL;
326 }
327
328 return 0;
329}
330static int
331pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
332{
333 struct snd_bebob *bebob = substream->private_data;
334
335 switch (cmd) {
336 case SNDRV_PCM_TRIGGER_START:
337 amdtp_stream_pcm_trigger(&bebob->rx_stream, substream);
338 break;
339 case SNDRV_PCM_TRIGGER_STOP:
340 amdtp_stream_pcm_trigger(&bebob->rx_stream, NULL);
341 break;
342 default:
343 return -EINVAL;
344 }
345
346 return 0;
347}
348
349static snd_pcm_uframes_t
350pcm_capture_pointer(struct snd_pcm_substream *sbstrm)
351{
352 struct snd_bebob *bebob = sbstrm->private_data;
353 return amdtp_stream_pcm_pointer(&bebob->tx_stream);
354}
355static snd_pcm_uframes_t
356pcm_playback_pointer(struct snd_pcm_substream *sbstrm)
357{
358 struct snd_bebob *bebob = sbstrm->private_data;
359 return amdtp_stream_pcm_pointer(&bebob->rx_stream);
360}
361
362static const struct snd_pcm_ops pcm_capture_ops = {
363 .open = pcm_open,
364 .close = pcm_close,
365 .ioctl = snd_pcm_lib_ioctl,
366 .hw_params = pcm_capture_hw_params,
367 .hw_free = pcm_capture_hw_free,
368 .prepare = pcm_capture_prepare,
369 .trigger = pcm_capture_trigger,
370 .pointer = pcm_capture_pointer,
371 .page = snd_pcm_lib_get_vmalloc_page,
372};
373static const struct snd_pcm_ops pcm_playback_ops = {
374 .open = pcm_open,
375 .close = pcm_close,
376 .ioctl = snd_pcm_lib_ioctl,
377 .hw_params = pcm_playback_hw_params,
378 .hw_free = pcm_playback_hw_free,
379 .prepare = pcm_playback_prepare,
380 .trigger = pcm_playback_trigger,
381 .pointer = pcm_playback_pointer,
382 .page = snd_pcm_lib_get_vmalloc_page,
383 .mmap = snd_pcm_lib_mmap_vmalloc,
384};
385
386int snd_bebob_create_pcm_devices(struct snd_bebob *bebob)
387{
388 struct snd_pcm *pcm;
389 int err;
390
391 err = snd_pcm_new(bebob->card, bebob->card->driver, 0, 1, 1, &pcm);
392 if (err < 0)
393 goto end;
394
395 pcm->private_data = bebob;
396 snprintf(pcm->name, sizeof(pcm->name),
397 "%s PCM", bebob->card->shortname);
398 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &pcm_playback_ops);
399 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &pcm_capture_ops);
400end:
401 return err;
402}