Merge tag 'locks-v3.16-2' of git://git.samba.org/jlayton/linux
[linux/fpc-iii.git] / sound / firewire / bebob / bebob_pcm.c
blob4a55561ed4ec028084a6d2f5bbd43f33c74f38e5
1 /*
2 * bebob_pcm.c - a part of driver for BeBoB based devices
4 * Copyright (c) 2013-2014 Takashi Sakamoto
6 * Licensed under the terms of the GNU General Public License, version 2.
7 */
9 #include "./bebob.h"
11 static int
12 hw_rule_rate(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
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
22 unsigned int i;
24 for (i = 0; i < SND_BEBOB_STRM_FMT_ENTRIES; i++) {
25 /* entry is invalid */
26 if (formations[i].pcm == 0)
27 continue;
29 if (!snd_interval_test(c, formations[i].pcm))
30 continue;
32 t.min = min(t.min, snd_bebob_rate_table[i]);
33 t.max = max(t.max, snd_bebob_rate_table[i]);
36 return snd_interval_refine(r, &t);
39 static int
40 hw_rule_channels(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
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
51 unsigned int i;
53 for (i = 0; i < SND_BEBOB_STRM_FMT_ENTRIES; i++) {
54 /* entry is invalid */
55 if (formations[i].pcm == 0)
56 continue;
58 if (!snd_interval_test(r, snd_bebob_rate_table[i]))
59 continue;
61 t.min = min(t.min, formations[i].pcm);
62 t.max = max(t.max, formations[i].pcm);
65 return snd_interval_refine(c, &t);
68 static void
69 limit_channels_and_rates(struct snd_pcm_hardware *hw,
70 struct snd_bebob_stream_formation *formations)
72 unsigned int i;
74 hw->channels_min = UINT_MAX;
75 hw->channels_max = 0;
77 hw->rate_min = UINT_MAX;
78 hw->rate_max = 0;
79 hw->rates = 0;
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;
86 hw->channels_min = min(hw->channels_min, formations[i].pcm);
87 hw->channels_max = max(hw->channels_max, formations[i].pcm);
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]);
95 static void
96 limit_period_and_buffer(struct snd_pcm_hardware *hw)
98 hw->periods_min = 2; /* SNDRV_PCM_INFO_BATCH */
99 hw->periods_max = UINT_MAX;
101 hw->period_bytes_min = 4 * hw->channels_max; /* bytes for a frame */
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;
108 static int
109 pcm_init_hw_params(struct snd_bebob *bebob,
110 struct snd_pcm_substream *substream)
112 struct snd_pcm_runtime *runtime = substream->runtime;
113 struct amdtp_stream *s;
114 struct snd_bebob_stream_formation *formations;
115 int err;
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;
124 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
125 runtime->hw.formats = AMDTP_IN_PCM_FORMAT_BITS;
126 s = &bebob->tx_stream;
127 formations = bebob->tx_stream_formations;
128 } else {
129 runtime->hw.formats = AMDTP_OUT_PCM_FORMAT_BITS;
130 s = &bebob->rx_stream;
131 formations = bebob->rx_stream_formations;
134 limit_channels_and_rates(&runtime->hw, formations);
135 limit_period_and_buffer(&runtime->hw);
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;
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;
149 err = amdtp_stream_add_pcm_hw_constraints(s, runtime);
150 end:
151 return err;
154 static int
155 pcm_open(struct snd_pcm_substream *substream)
157 struct snd_bebob *bebob = substream->private_data;
158 struct snd_bebob_rate_spec *spec = bebob->spec->rate;
159 unsigned int sampling_rate;
160 bool internal;
161 int err;
163 err = snd_bebob_stream_lock_try(bebob);
164 if (err < 0)
165 goto end;
167 err = pcm_init_hw_params(bebob, substream);
168 if (err < 0)
169 goto err_locked;
171 err = snd_bebob_stream_check_internal_clock(bebob, &internal);
172 if (err < 0)
173 goto err_locked;
176 * When source of clock is internal or any PCM stream are running,
177 * the available sampling rate is limited at current sampling rate.
179 if (!internal ||
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;
189 substream->runtime->hw.rate_min = sampling_rate;
190 substream->runtime->hw.rate_max = sampling_rate;
193 snd_pcm_set_sync(substream);
194 end:
195 return err;
196 err_locked:
197 snd_bebob_stream_lock_release(bebob);
198 return err;
201 static int
202 pcm_close(struct snd_pcm_substream *substream)
204 struct snd_bebob *bebob = substream->private_data;
205 snd_bebob_stream_lock_release(bebob);
206 return 0;
209 static int
210 pcm_capture_hw_params(struct snd_pcm_substream *substream,
211 struct snd_pcm_hw_params *hw_params)
213 struct snd_bebob *bebob = substream->private_data;
215 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN)
216 atomic_inc(&bebob->capture_substreams);
217 amdtp_stream_set_pcm_format(&bebob->tx_stream,
218 params_format(hw_params));
219 return snd_pcm_lib_alloc_vmalloc_buffer(substream,
220 params_buffer_bytes(hw_params));
222 static int
223 pcm_playback_hw_params(struct snd_pcm_substream *substream,
224 struct snd_pcm_hw_params *hw_params)
226 struct snd_bebob *bebob = substream->private_data;
228 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN)
229 atomic_inc(&bebob->playback_substreams);
230 amdtp_stream_set_pcm_format(&bebob->rx_stream,
231 params_format(hw_params));
232 return snd_pcm_lib_alloc_vmalloc_buffer(substream,
233 params_buffer_bytes(hw_params));
236 static int
237 pcm_capture_hw_free(struct snd_pcm_substream *substream)
239 struct snd_bebob *bebob = substream->private_data;
241 if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
242 atomic_dec(&bebob->capture_substreams);
244 snd_bebob_stream_stop_duplex(bebob);
246 return snd_pcm_lib_free_vmalloc_buffer(substream);
248 static int
249 pcm_playback_hw_free(struct snd_pcm_substream *substream)
251 struct snd_bebob *bebob = substream->private_data;
253 if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
254 atomic_dec(&bebob->playback_substreams);
256 snd_bebob_stream_stop_duplex(bebob);
258 return snd_pcm_lib_free_vmalloc_buffer(substream);
261 static int
262 pcm_capture_prepare(struct snd_pcm_substream *substream)
264 struct snd_bebob *bebob = substream->private_data;
265 struct snd_pcm_runtime *runtime = substream->runtime;
266 int err;
268 err = snd_bebob_stream_start_duplex(bebob, runtime->rate);
269 if (err >= 0)
270 amdtp_stream_pcm_prepare(&bebob->tx_stream);
272 return err;
274 static int
275 pcm_playback_prepare(struct snd_pcm_substream *substream)
277 struct snd_bebob *bebob = substream->private_data;
278 struct snd_pcm_runtime *runtime = substream->runtime;
279 int err;
281 err = snd_bebob_stream_start_duplex(bebob, runtime->rate);
282 if (err >= 0)
283 amdtp_stream_pcm_prepare(&bebob->rx_stream);
285 return err;
288 static int
289 pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
291 struct snd_bebob *bebob = substream->private_data;
293 switch (cmd) {
294 case SNDRV_PCM_TRIGGER_START:
295 amdtp_stream_pcm_trigger(&bebob->tx_stream, substream);
296 break;
297 case SNDRV_PCM_TRIGGER_STOP:
298 amdtp_stream_pcm_trigger(&bebob->tx_stream, NULL);
299 break;
300 default:
301 return -EINVAL;
304 return 0;
306 static int
307 pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
309 struct snd_bebob *bebob = substream->private_data;
311 switch (cmd) {
312 case SNDRV_PCM_TRIGGER_START:
313 amdtp_stream_pcm_trigger(&bebob->rx_stream, substream);
314 break;
315 case SNDRV_PCM_TRIGGER_STOP:
316 amdtp_stream_pcm_trigger(&bebob->rx_stream, NULL);
317 break;
318 default:
319 return -EINVAL;
322 return 0;
325 static snd_pcm_uframes_t
326 pcm_capture_pointer(struct snd_pcm_substream *sbstrm)
328 struct snd_bebob *bebob = sbstrm->private_data;
329 return amdtp_stream_pcm_pointer(&bebob->tx_stream);
331 static snd_pcm_uframes_t
332 pcm_playback_pointer(struct snd_pcm_substream *sbstrm)
334 struct snd_bebob *bebob = sbstrm->private_data;
335 return amdtp_stream_pcm_pointer(&bebob->rx_stream);
338 static const struct snd_pcm_ops pcm_capture_ops = {
339 .open = pcm_open,
340 .close = pcm_close,
341 .ioctl = snd_pcm_lib_ioctl,
342 .hw_params = pcm_capture_hw_params,
343 .hw_free = pcm_capture_hw_free,
344 .prepare = pcm_capture_prepare,
345 .trigger = pcm_capture_trigger,
346 .pointer = pcm_capture_pointer,
347 .page = snd_pcm_lib_get_vmalloc_page,
349 static const struct snd_pcm_ops pcm_playback_ops = {
350 .open = pcm_open,
351 .close = pcm_close,
352 .ioctl = snd_pcm_lib_ioctl,
353 .hw_params = pcm_playback_hw_params,
354 .hw_free = pcm_playback_hw_free,
355 .prepare = pcm_playback_prepare,
356 .trigger = pcm_playback_trigger,
357 .pointer = pcm_playback_pointer,
358 .page = snd_pcm_lib_get_vmalloc_page,
359 .mmap = snd_pcm_lib_mmap_vmalloc,
362 int snd_bebob_create_pcm_devices(struct snd_bebob *bebob)
364 struct snd_pcm *pcm;
365 int err;
367 err = snd_pcm_new(bebob->card, bebob->card->driver, 0, 1, 1, &pcm);
368 if (err < 0)
369 goto end;
371 pcm->private_data = bebob;
372 snprintf(pcm->name, sizeof(pcm->name),
373 "%s PCM", bebob->card->shortname);
374 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &pcm_playback_ops);
375 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &pcm_capture_ops);
376 end:
377 return err;