ALSA: oxfw: add support for capturing PCM samples
[cascardo/linux.git] / sound / firewire / oxfw / oxfw-pcm.c
1 /*
2  * oxfw_pcm.c - a part of driver for OXFW970/971 based devices
3  *
4  * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
5  * Licensed under the terms of the GNU General Public License, version 2.
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         unsigned 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         unsigned 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         unsigned 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 void limit_period_and_buffer(struct snd_pcm_hardware *hw)
110 {
111         hw->periods_min = 2;            /* SNDRV_PCM_INFO_BATCH */
112         hw->periods_max = UINT_MAX;
113
114         hw->period_bytes_min = 4 * hw->channels_max;    /* bytes for a frame */
115
116         /* Just to prevent from allocating much pages. */
117         hw->period_bytes_max = hw->period_bytes_min * 2048;
118         hw->buffer_bytes_max = hw->period_bytes_max * hw->periods_min;
119 }
120
121 static int init_hw_params(struct snd_oxfw *oxfw,
122                           struct snd_pcm_substream *substream)
123 {
124         struct snd_pcm_runtime *runtime = substream->runtime;
125         u8 **formats;
126         struct amdtp_stream *stream;
127         int err;
128
129         runtime->hw.info = SNDRV_PCM_INFO_BATCH |
130                            SNDRV_PCM_INFO_BLOCK_TRANSFER |
131                            SNDRV_PCM_INFO_INTERLEAVED |
132                            SNDRV_PCM_INFO_JOINT_DUPLEX |
133                            SNDRV_PCM_INFO_MMAP |
134                            SNDRV_PCM_INFO_MMAP_VALID;
135
136         if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
137                 runtime->hw.formats = AMDTP_IN_PCM_FORMAT_BITS;
138                 stream = &oxfw->tx_stream;
139                 formats = oxfw->tx_stream_formats;
140         } else {
141                 runtime->hw.formats = AMDTP_OUT_PCM_FORMAT_BITS;
142                 stream = &oxfw->rx_stream;
143                 formats = oxfw->rx_stream_formats;
144         }
145
146         limit_channels_and_rates(&runtime->hw, formats);
147         limit_period_and_buffer(&runtime->hw);
148
149         err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
150                                   hw_rule_channels, formats,
151                                   SNDRV_PCM_HW_PARAM_RATE, -1);
152         if (err < 0)
153                 goto end;
154
155         err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
156                                   hw_rule_rate, formats,
157                                   SNDRV_PCM_HW_PARAM_CHANNELS, -1);
158         if (err < 0)
159                 goto end;
160
161         err = amdtp_stream_add_pcm_hw_constraints(stream, runtime);
162 end:
163         return err;
164 }
165
166 static int limit_to_current_params(struct snd_pcm_substream *substream)
167 {
168         struct snd_oxfw *oxfw = substream->private_data;
169         struct snd_oxfw_stream_formation formation;
170         enum avc_general_plug_dir dir;
171         int err;
172
173         if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
174                 dir = AVC_GENERAL_PLUG_DIR_OUT;
175         else
176                 dir = AVC_GENERAL_PLUG_DIR_IN;
177
178         err = snd_oxfw_stream_get_current_formation(oxfw, dir, &formation);
179         if (err < 0)
180                 goto end;
181
182         substream->runtime->hw.channels_min = formation.pcm;
183         substream->runtime->hw.channels_max = formation.pcm;
184         substream->runtime->hw.rate_min = formation.rate;
185         substream->runtime->hw.rate_max = formation.rate;
186 end:
187         return err;
188 }
189
190 static int pcm_open(struct snd_pcm_substream *substream)
191 {
192         struct snd_oxfw *oxfw = substream->private_data;
193         int err;
194
195         err = init_hw_params(oxfw, substream);
196         if (err < 0)
197                 goto end;
198
199         /*
200          * When any PCM streams are already running, the available sampling
201          * rate is limited at current value.
202          */
203         if (amdtp_stream_pcm_running(&oxfw->tx_stream) ||
204             amdtp_stream_pcm_running(&oxfw->rx_stream)) {
205                 err = limit_to_current_params(substream);
206                 if (err < 0)
207                         goto end;
208         }
209
210         snd_pcm_set_sync(substream);
211 end:
212         return err;
213 }
214
215 static int pcm_close(struct snd_pcm_substream *substream)
216 {
217         return 0;
218 }
219
220 static int pcm_capture_hw_params(struct snd_pcm_substream *substream,
221                                  struct snd_pcm_hw_params *hw_params)
222 {
223         struct snd_oxfw *oxfw = substream->private_data;
224
225
226         if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
227                 mutex_lock(&oxfw->mutex);
228                 oxfw->capture_substreams++;
229                 mutex_unlock(&oxfw->mutex);
230         }
231
232         amdtp_stream_set_pcm_format(&oxfw->tx_stream, params_format(hw_params));
233
234         return snd_pcm_lib_alloc_vmalloc_buffer(substream,
235                                                 params_buffer_bytes(hw_params));
236 }
237 static int pcm_playback_hw_params(struct snd_pcm_substream *substream,
238                                   struct snd_pcm_hw_params *hw_params)
239 {
240         struct snd_oxfw *oxfw = substream->private_data;
241
242         if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
243                 mutex_lock(&oxfw->mutex);
244                 oxfw->playback_substreams++;
245                 mutex_unlock(&oxfw->mutex);
246         }
247
248         amdtp_stream_set_pcm_format(&oxfw->rx_stream, params_format(hw_params));
249
250         return snd_pcm_lib_alloc_vmalloc_buffer(substream,
251                                                 params_buffer_bytes(hw_params));
252 }
253
254 static int pcm_capture_hw_free(struct snd_pcm_substream *substream)
255 {
256         struct snd_oxfw *oxfw = substream->private_data;
257
258         mutex_lock(&oxfw->mutex);
259
260         if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
261                 oxfw->capture_substreams--;
262
263         snd_oxfw_stream_stop_simplex(oxfw, &oxfw->tx_stream);
264
265         mutex_unlock(&oxfw->mutex);
266
267         return snd_pcm_lib_free_vmalloc_buffer(substream);
268 }
269 static int pcm_playback_hw_free(struct snd_pcm_substream *substream)
270 {
271         struct snd_oxfw *oxfw = substream->private_data;
272
273         mutex_lock(&oxfw->mutex);
274
275         if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
276                 oxfw->playback_substreams--;
277
278         snd_oxfw_stream_stop_simplex(oxfw, &oxfw->rx_stream);
279
280         mutex_unlock(&oxfw->mutex);
281
282         return snd_pcm_lib_free_vmalloc_buffer(substream);
283 }
284
285 static int pcm_capture_prepare(struct snd_pcm_substream *substream)
286 {
287         struct snd_oxfw *oxfw = substream->private_data;
288         struct snd_pcm_runtime *runtime = substream->runtime;
289         int err;
290
291         mutex_lock(&oxfw->mutex);
292         err = snd_oxfw_stream_start_simplex(oxfw, &oxfw->tx_stream,
293                                             runtime->rate, runtime->channels);
294         mutex_unlock(&oxfw->mutex);
295         if (err < 0)
296                 goto end;
297
298         amdtp_stream_pcm_prepare(&oxfw->tx_stream);
299 end:
300         return err;
301 }
302 static int pcm_playback_prepare(struct snd_pcm_substream *substream)
303 {
304         struct snd_oxfw *oxfw = substream->private_data;
305         struct snd_pcm_runtime *runtime = substream->runtime;
306         int err;
307
308         mutex_lock(&oxfw->mutex);
309         err = snd_oxfw_stream_start_simplex(oxfw, &oxfw->rx_stream,
310                                             runtime->rate, runtime->channels);
311         mutex_unlock(&oxfw->mutex);
312         if (err < 0)
313                 goto end;
314
315         amdtp_stream_pcm_prepare(&oxfw->rx_stream);
316 end:
317         return err;
318 }
319
320 static int pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
321 {
322         struct snd_oxfw *oxfw = substream->private_data;
323         struct snd_pcm_substream *pcm;
324
325         switch (cmd) {
326         case SNDRV_PCM_TRIGGER_START:
327                 pcm = substream;
328                 break;
329         case SNDRV_PCM_TRIGGER_STOP:
330                 pcm = NULL;
331                 break;
332         default:
333                 return -EINVAL;
334         }
335         amdtp_stream_pcm_trigger(&oxfw->tx_stream, pcm);
336         return 0;
337 }
338 static int pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
339 {
340         struct snd_oxfw *oxfw = substream->private_data;
341         struct snd_pcm_substream *pcm;
342
343         switch (cmd) {
344         case SNDRV_PCM_TRIGGER_START:
345                 pcm = substream;
346                 break;
347         case SNDRV_PCM_TRIGGER_STOP:
348                 pcm = NULL;
349                 break;
350         default:
351                 return -EINVAL;
352         }
353         amdtp_stream_pcm_trigger(&oxfw->rx_stream, pcm);
354         return 0;
355 }
356
357 static snd_pcm_uframes_t pcm_capture_pointer(struct snd_pcm_substream *sbstm)
358 {
359         struct snd_oxfw *oxfw = sbstm->private_data;
360
361         return amdtp_stream_pcm_pointer(&oxfw->tx_stream);
362 }
363 static snd_pcm_uframes_t pcm_playback_pointer(struct snd_pcm_substream *sbstm)
364 {
365         struct snd_oxfw *oxfw = sbstm->private_data;
366
367         return amdtp_stream_pcm_pointer(&oxfw->rx_stream);
368 }
369
370 int snd_oxfw_create_pcm(struct snd_oxfw *oxfw)
371 {
372         static struct snd_pcm_ops capture_ops = {
373                 .open      = pcm_open,
374                 .close     = pcm_close,
375                 .ioctl     = snd_pcm_lib_ioctl,
376                 .hw_params = pcm_capture_hw_params,
377                 .hw_free   = pcm_capture_hw_free,
378                 .prepare   = pcm_capture_prepare,
379                 .trigger   = pcm_capture_trigger,
380                 .pointer   = pcm_capture_pointer,
381                 .page      = snd_pcm_lib_get_vmalloc_page,
382                 .mmap      = snd_pcm_lib_mmap_vmalloc,
383         };
384         static struct snd_pcm_ops playback_ops = {
385                 .open      = pcm_open,
386                 .close     = pcm_close,
387                 .ioctl     = snd_pcm_lib_ioctl,
388                 .hw_params = pcm_playback_hw_params,
389                 .hw_free   = pcm_playback_hw_free,
390                 .prepare   = pcm_playback_prepare,
391                 .trigger   = pcm_playback_trigger,
392                 .pointer   = pcm_playback_pointer,
393                 .page      = snd_pcm_lib_get_vmalloc_page,
394                 .mmap      = snd_pcm_lib_mmap_vmalloc,
395         };
396         struct snd_pcm *pcm;
397         unsigned int cap = 0;
398         int err;
399
400         if (oxfw->has_output)
401                 cap = 1;
402
403         err = snd_pcm_new(oxfw->card, oxfw->card->driver, 0, 1, cap, &pcm);
404         if (err < 0)
405                 return err;
406
407         pcm->private_data = oxfw;
408         strcpy(pcm->name, oxfw->card->shortname);
409         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &playback_ops);
410         if (cap > 0)
411                 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &capture_ops);
412
413         return 0;
414 }