aboutsummaryrefslogtreecommitdiffstats
path: root/sound
diff options
context:
space:
mode:
authorTakashi Sakamoto <o-takashi@sakamocchi.jp>2014-04-25 09:45:19 -0400
committerTakashi Iwai <tiwai@suse.de>2014-05-26 08:30:46 -0400
commitfbbebd2c40795e87f1280ca4d963f7cbe1c83168 (patch)
tree349d71f02954a977d33e595c835045bad2722438 /sound
parent248b78027d26735eb22f5ee91a400e5eabfea77d (diff)
ALSA: bebob: Add PCM interface
This commit adds a functionality to capture/playback PCM samples. When AMDTP stream is already running for PCM or the source of clock is not internal, available sampling rate is limited at current one. Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp> Signed-off-by: Takashi Iwai <tiwai@suse.de>
Diffstat (limited to 'sound')
-rw-r--r--sound/firewire/Kconfig1
-rw-r--r--sound/firewire/bebob/Makefile1
-rw-r--r--sound/firewire/bebob/bebob.c4
-rw-r--r--sound/firewire/bebob/bebob.h4
-rw-r--r--sound/firewire/bebob/bebob_pcm.c369
5 files changed, 379 insertions, 0 deletions
diff --git a/sound/firewire/Kconfig b/sound/firewire/Kconfig
index 708d3c81ef87..d55fd5fd16ca 100644
--- a/sound/firewire/Kconfig
+++ b/sound/firewire/Kconfig
@@ -83,6 +83,7 @@ config SND_BEBOB
83 tristate "BridgeCo DM1000/DM1100/DM1500 with BeBoB firmware" 83 tristate "BridgeCo DM1000/DM1100/DM1500 with BeBoB firmware"
84 select SND_FIREWIRE_LIB 84 select SND_FIREWIRE_LIB
85 select SND_RAWMIDI 85 select SND_RAWMIDI
86 select SND_PCM
86 help 87 help
87 Say Y here to include support for FireWire devices based 88 Say Y here to include support for FireWire devices based
88 on BridgeCo DM1000/DM1100/DM1500 with BeBoB firmware: 89 on BridgeCo DM1000/DM1100/DM1500 with BeBoB firmware:
diff --git a/sound/firewire/bebob/Makefile b/sound/firewire/bebob/Makefile
index 1e39e591540b..533718a5c1e5 100644
--- a/sound/firewire/bebob/Makefile
+++ b/sound/firewire/bebob/Makefile
@@ -1,3 +1,4 @@
1snd-bebob-objs := bebob_command.o bebob_stream.o bebob_proc.o bebob_midi.o \ 1snd-bebob-objs := bebob_command.o bebob_stream.o bebob_proc.o bebob_midi.o \
2 bebob_pcm.o \
2 bebob.o 3 bebob.o
3obj-m += snd-bebob.o 4obj-m += snd-bebob.o
diff --git a/sound/firewire/bebob/bebob.c b/sound/firewire/bebob/bebob.c
index 383df9a22004..dbd12c360046 100644
--- a/sound/firewire/bebob/bebob.c
+++ b/sound/firewire/bebob/bebob.c
@@ -171,6 +171,10 @@ bebob_probe(struct fw_unit *unit,
171 goto error; 171 goto error;
172 } 172 }
173 173
174 err = snd_bebob_create_pcm_devices(bebob);
175 if (err < 0)
176 goto error;
177
174 err = snd_bebob_stream_init_duplex(bebob); 178 err = snd_bebob_stream_init_duplex(bebob);
175 if (err < 0) 179 if (err < 0)
176 goto error; 180 goto error;
diff --git a/sound/firewire/bebob/bebob.h b/sound/firewire/bebob/bebob.h
index 0d53cff8a68d..b41bb913bac5 100644
--- a/sound/firewire/bebob/bebob.h
+++ b/sound/firewire/bebob/bebob.h
@@ -22,6 +22,8 @@
22#include <sound/initval.h> 22#include <sound/initval.h>
23#include <sound/info.h> 23#include <sound/info.h>
24#include <sound/rawmidi.h> 24#include <sound/rawmidi.h>
25#include <sound/pcm.h>
26#include <sound/pcm_params.h>
25 27
26#include "../lib.h" 28#include "../lib.h"
27#include "../fcp.h" 29#include "../fcp.h"
@@ -177,6 +179,8 @@ void snd_bebob_proc_init(struct snd_bebob *bebob);
177 179
178int snd_bebob_create_midi_devices(struct snd_bebob *bebob); 180int snd_bebob_create_midi_devices(struct snd_bebob *bebob);
179 181
182int snd_bebob_create_pcm_devices(struct snd_bebob *bebob);
183
180#define SND_BEBOB_DEV_ENTRY(vendor, model) \ 184#define SND_BEBOB_DEV_ENTRY(vendor, model) \
181{ \ 185{ \
182 .match_flags = IEEE1394_MATCH_VENDOR_ID | \ 186 .match_flags = IEEE1394_MATCH_VENDOR_ID | \
diff --git a/sound/firewire/bebob/bebob_pcm.c b/sound/firewire/bebob/bebob_pcm.c
new file mode 100644
index 000000000000..7474309dad87
--- /dev/null
+++ b/sound/firewire/bebob/bebob_pcm.c
@@ -0,0 +1,369 @@
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
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) {
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;
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_stream_add_pcm_hw_constraints(s, runtime);
150end:
151 return err;
152}
153
154static int
155pcm_open(struct snd_pcm_substream *substream)
156{
157 struct snd_bebob *bebob = substream->private_data;
158 unsigned int sampling_rate;
159 bool internal;
160 int err;
161
162 err = pcm_init_hw_params(bebob, substream);
163 if (err < 0)
164 goto end;
165
166 err = snd_bebob_stream_check_internal_clock(bebob, &internal);
167 if (err < 0)
168 goto end;
169
170 /*
171 * When source of clock is internal or any PCM stream are running,
172 * the available sampling rate is limited at current sampling rate.
173 */
174 if (!internal ||
175 amdtp_stream_pcm_running(&bebob->tx_stream) ||
176 amdtp_stream_pcm_running(&bebob->rx_stream)) {
177 err = snd_bebob_stream_get_rate(bebob, &sampling_rate);
178 if (err < 0) {
179 dev_err(&bebob->unit->device,
180 "fail to get sampling rate: %d\n", err);
181 goto end;
182 }
183
184 substream->runtime->hw.rate_min = sampling_rate;
185 substream->runtime->hw.rate_max = sampling_rate;
186 }
187
188 snd_pcm_set_sync(substream);
189
190end:
191 return err;
192}
193
194static int
195pcm_close(struct snd_pcm_substream *substream)
196{
197 return 0;
198}
199
200static int
201pcm_capture_hw_params(struct snd_pcm_substream *substream,
202 struct snd_pcm_hw_params *hw_params)
203{
204 struct snd_bebob *bebob = substream->private_data;
205
206 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN)
207 atomic_inc(&bebob->capture_substreams);
208 amdtp_stream_set_pcm_format(&bebob->tx_stream,
209 params_format(hw_params));
210 return snd_pcm_lib_alloc_vmalloc_buffer(substream,
211 params_buffer_bytes(hw_params));
212}
213static int
214pcm_playback_hw_params(struct snd_pcm_substream *substream,
215 struct snd_pcm_hw_params *hw_params)
216{
217 struct snd_bebob *bebob = substream->private_data;
218
219 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN)
220 atomic_inc(&bebob->playback_substreams);
221 amdtp_stream_set_pcm_format(&bebob->rx_stream,
222 params_format(hw_params));
223 return snd_pcm_lib_alloc_vmalloc_buffer(substream,
224 params_buffer_bytes(hw_params));
225}
226
227static int
228pcm_capture_hw_free(struct snd_pcm_substream *substream)
229{
230 struct snd_bebob *bebob = substream->private_data;
231
232 if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
233 atomic_dec(&bebob->capture_substreams);
234
235 snd_bebob_stream_stop_duplex(bebob);
236
237 return snd_pcm_lib_free_vmalloc_buffer(substream);
238}
239static int
240pcm_playback_hw_free(struct snd_pcm_substream *substream)
241{
242 struct snd_bebob *bebob = substream->private_data;
243
244 if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
245 atomic_dec(&bebob->playback_substreams);
246
247 snd_bebob_stream_stop_duplex(bebob);
248
249 return snd_pcm_lib_free_vmalloc_buffer(substream);
250}
251
252static int
253pcm_capture_prepare(struct snd_pcm_substream *substream)
254{
255 struct snd_bebob *bebob = substream->private_data;
256 struct snd_pcm_runtime *runtime = substream->runtime;
257 int err;
258
259 err = snd_bebob_stream_start_duplex(bebob, runtime->rate);
260 if (err >= 0)
261 amdtp_stream_pcm_prepare(&bebob->tx_stream);
262
263 return err;
264}
265static int
266pcm_playback_prepare(struct snd_pcm_substream *substream)
267{
268 struct snd_bebob *bebob = substream->private_data;
269 struct snd_pcm_runtime *runtime = substream->runtime;
270 int err;
271
272 err = snd_bebob_stream_start_duplex(bebob, runtime->rate);
273 if (err >= 0)
274 amdtp_stream_pcm_prepare(&bebob->rx_stream);
275
276 return err;
277}
278
279static int
280pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
281{
282 struct snd_bebob *bebob = substream->private_data;
283
284 switch (cmd) {
285 case SNDRV_PCM_TRIGGER_START:
286 amdtp_stream_pcm_trigger(&bebob->tx_stream, substream);
287 break;
288 case SNDRV_PCM_TRIGGER_STOP:
289 amdtp_stream_pcm_trigger(&bebob->tx_stream, NULL);
290 break;
291 default:
292 return -EINVAL;
293 }
294
295 return 0;
296}
297static int
298pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
299{
300 struct snd_bebob *bebob = substream->private_data;
301
302 switch (cmd) {
303 case SNDRV_PCM_TRIGGER_START:
304 amdtp_stream_pcm_trigger(&bebob->rx_stream, substream);
305 break;
306 case SNDRV_PCM_TRIGGER_STOP:
307 amdtp_stream_pcm_trigger(&bebob->rx_stream, NULL);
308 break;
309 default:
310 return -EINVAL;
311 }
312
313 return 0;
314}
315
316static snd_pcm_uframes_t
317pcm_capture_pointer(struct snd_pcm_substream *sbstrm)
318{
319 struct snd_bebob *bebob = sbstrm->private_data;
320 return amdtp_stream_pcm_pointer(&bebob->tx_stream);
321}
322static snd_pcm_uframes_t
323pcm_playback_pointer(struct snd_pcm_substream *sbstrm)
324{
325 struct snd_bebob *bebob = sbstrm->private_data;
326 return amdtp_stream_pcm_pointer(&bebob->rx_stream);
327}
328
329static const struct snd_pcm_ops pcm_capture_ops = {
330 .open = pcm_open,
331 .close = pcm_close,
332 .ioctl = snd_pcm_lib_ioctl,
333 .hw_params = pcm_capture_hw_params,
334 .hw_free = pcm_capture_hw_free,
335 .prepare = pcm_capture_prepare,
336 .trigger = pcm_capture_trigger,
337 .pointer = pcm_capture_pointer,
338 .page = snd_pcm_lib_get_vmalloc_page,
339};
340static const struct snd_pcm_ops pcm_playback_ops = {
341 .open = pcm_open,
342 .close = pcm_close,
343 .ioctl = snd_pcm_lib_ioctl,
344 .hw_params = pcm_playback_hw_params,
345 .hw_free = pcm_playback_hw_free,
346 .prepare = pcm_playback_prepare,
347 .trigger = pcm_playback_trigger,
348 .pointer = pcm_playback_pointer,
349 .page = snd_pcm_lib_get_vmalloc_page,
350 .mmap = snd_pcm_lib_mmap_vmalloc,
351};
352
353int snd_bebob_create_pcm_devices(struct snd_bebob *bebob)
354{
355 struct snd_pcm *pcm;
356 int err;
357
358 err = snd_pcm_new(bebob->card, bebob->card->driver, 0, 1, 1, &pcm);
359 if (err < 0)
360 goto end;
361
362 pcm->private_data = bebob;
363 snprintf(pcm->name, sizeof(pcm->name),
364 "%s PCM", bebob->card->shortname);
365 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &pcm_playback_ops);
366 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &pcm_capture_ops);
367end:
368 return err;
369}