]> git.karo-electronics.de Git - karo-tx-linux.git/blob - sound/firewire/bebob/bebob_pcm.c
e2f023f3cd9f10b2562595adf7b9b2b99a8909e2
[karo-tx-linux.git] / sound / firewire / bebob / bebob_pcm.c
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 void
96 limit_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
108 static int
109 pcm_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) {
125                 runtime->hw.formats = AM824_IN_PCM_FORMAT_BITS;
126                 s = &bebob->tx_stream;
127                 formations = bebob->tx_stream_formations;
128         } else {
129                 runtime->hw.formats = AM824_OUT_PCM_FORMAT_BITS;
130                 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
149         err = amdtp_am824_add_pcm_hw_constraints(s, runtime);
150 end:
151         return err;
152 }
153
154 static int
155 pcm_open(struct snd_pcm_substream *substream)
156 {
157         struct snd_bebob *bebob = substream->private_data;
158         const struct snd_bebob_rate_spec *spec = bebob->spec->rate;
159         unsigned int sampling_rate;
160         enum snd_bebob_clock_type src;
161         int err;
162
163         err = snd_bebob_stream_lock_try(bebob);
164         if (err < 0)
165                 goto end;
166
167         err = pcm_init_hw_params(bebob, substream);
168         if (err < 0)
169                 goto err_locked;
170
171         err = snd_bebob_stream_get_clock_src(bebob, &src);
172         if (err < 0)
173                 goto err_locked;
174
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          */
179         if (src == SND_BEBOB_CLOCK_TYPE_EXTERNAL ||
180             amdtp_stream_pcm_running(&bebob->tx_stream) ||
181             amdtp_stream_pcm_running(&bebob->rx_stream)) {
182                 err = spec->get(bebob, &sampling_rate);
183                 if (err < 0) {
184                         dev_err(&bebob->unit->device,
185                                 "fail to get sampling rate: %d\n", err);
186                         goto err_locked;
187                 }
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);
194 end:
195         return err;
196 err_locked:
197         snd_bebob_stream_lock_release(bebob);
198         return err;
199 }
200
201 static int
202 pcm_close(struct snd_pcm_substream *substream)
203 {
204         struct snd_bebob *bebob = substream->private_data;
205         snd_bebob_stream_lock_release(bebob);
206         return 0;
207 }
208
209 static int
210 pcm_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;
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                 mutex_lock(&bebob->mutex);
223                 bebob->substreams_counter++;
224                 mutex_unlock(&bebob->mutex);
225         }
226
227         return 0;
228 }
229 static int
230 pcm_playback_hw_params(struct snd_pcm_substream *substream,
231                        struct snd_pcm_hw_params *hw_params)
232 {
233         struct snd_bebob *bebob = substream->private_data;
234         int err;
235
236         err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
237                                                params_buffer_bytes(hw_params));
238         if (err < 0)
239                 return err;
240
241         if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
242                 mutex_lock(&bebob->mutex);
243                 bebob->substreams_counter++;
244                 mutex_unlock(&bebob->mutex);
245         }
246
247         return 0;
248 }
249
250 static int
251 pcm_capture_hw_free(struct snd_pcm_substream *substream)
252 {
253         struct snd_bebob *bebob = substream->private_data;
254
255         if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN) {
256                 mutex_lock(&bebob->mutex);
257                 bebob->substreams_counter--;
258                 mutex_unlock(&bebob->mutex);
259         }
260
261         snd_bebob_stream_stop_duplex(bebob);
262
263         return snd_pcm_lib_free_vmalloc_buffer(substream);
264 }
265 static int
266 pcm_playback_hw_free(struct snd_pcm_substream *substream)
267 {
268         struct snd_bebob *bebob = substream->private_data;
269
270         if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN) {
271                 mutex_lock(&bebob->mutex);
272                 bebob->substreams_counter--;
273                 mutex_unlock(&bebob->mutex);
274         }
275
276         snd_bebob_stream_stop_duplex(bebob);
277
278         return snd_pcm_lib_free_vmalloc_buffer(substream);
279 }
280
281 static int
282 pcm_capture_prepare(struct snd_pcm_substream *substream)
283 {
284         struct snd_bebob *bebob = substream->private_data;
285         struct snd_pcm_runtime *runtime = substream->runtime;
286         int err;
287
288         err = snd_bebob_stream_start_duplex(bebob, runtime->rate);
289         if (err >= 0)
290                 amdtp_stream_pcm_prepare(&bebob->tx_stream);
291
292         return err;
293 }
294 static int
295 pcm_playback_prepare(struct snd_pcm_substream *substream)
296 {
297         struct snd_bebob *bebob = substream->private_data;
298         struct snd_pcm_runtime *runtime = substream->runtime;
299         int err;
300
301         err = snd_bebob_stream_start_duplex(bebob, runtime->rate);
302         if (err >= 0)
303                 amdtp_stream_pcm_prepare(&bebob->rx_stream);
304
305         return err;
306 }
307
308 static int
309 pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
310 {
311         struct snd_bebob *bebob = substream->private_data;
312
313         switch (cmd) {
314         case SNDRV_PCM_TRIGGER_START:
315                 amdtp_stream_pcm_trigger(&bebob->tx_stream, substream);
316                 break;
317         case SNDRV_PCM_TRIGGER_STOP:
318                 amdtp_stream_pcm_trigger(&bebob->tx_stream, NULL);
319                 break;
320         default:
321                 return -EINVAL;
322         }
323
324         return 0;
325 }
326 static int
327 pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
328 {
329         struct snd_bebob *bebob = substream->private_data;
330
331         switch (cmd) {
332         case SNDRV_PCM_TRIGGER_START:
333                 amdtp_stream_pcm_trigger(&bebob->rx_stream, substream);
334                 break;
335         case SNDRV_PCM_TRIGGER_STOP:
336                 amdtp_stream_pcm_trigger(&bebob->rx_stream, NULL);
337                 break;
338         default:
339                 return -EINVAL;
340         }
341
342         return 0;
343 }
344
345 static snd_pcm_uframes_t
346 pcm_capture_pointer(struct snd_pcm_substream *sbstrm)
347 {
348         struct snd_bebob *bebob = sbstrm->private_data;
349         return amdtp_stream_pcm_pointer(&bebob->tx_stream);
350 }
351 static snd_pcm_uframes_t
352 pcm_playback_pointer(struct snd_pcm_substream *sbstrm)
353 {
354         struct snd_bebob *bebob = sbstrm->private_data;
355         return amdtp_stream_pcm_pointer(&bebob->rx_stream);
356 }
357
358 int snd_bebob_create_pcm_devices(struct snd_bebob *bebob)
359 {
360         static const struct snd_pcm_ops capture_ops = {
361                 .open           = pcm_open,
362                 .close          = pcm_close,
363                 .ioctl          = snd_pcm_lib_ioctl,
364                 .hw_params      = pcm_capture_hw_params,
365                 .hw_free        = pcm_capture_hw_free,
366                 .prepare        = pcm_capture_prepare,
367                 .trigger        = pcm_capture_trigger,
368                 .pointer        = pcm_capture_pointer,
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                 .page           = snd_pcm_lib_get_vmalloc_page,
381                 .mmap           = snd_pcm_lib_mmap_vmalloc,
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 }