aboutsummaryrefslogtreecommitdiffstats
path: root/sound
diff options
context:
space:
mode:
authorTakashi Sakamoto <o-takashi@sakamocchi.jp>2014-04-25 09:45:11 -0400
committerTakashi Iwai <tiwai@suse.de>2014-05-26 08:28:27 -0400
commitaa02bb6e60783938d61eefe38346781a646800a6 (patch)
tree9eda5e0ae49c61c799b39682e943768ba4e51e94 /sound
parent53111cdc53205fd35aac392c1d33893766be514e (diff)
ALSA: fireworks: 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/fireworks/Makefile2
-rw-r--r--sound/firewire/fireworks/fireworks.c4
-rw-r--r--sound/firewire/fireworks/fireworks.h4
-rw-r--r--sound/firewire/fireworks/fireworks_pcm.c394
-rw-r--r--sound/firewire/fireworks/fireworks_stream.c33
6 files changed, 404 insertions, 34 deletions
diff --git a/sound/firewire/Kconfig b/sound/firewire/Kconfig
index 36a12127c53f..fb39f9cfdfbf 100644
--- a/sound/firewire/Kconfig
+++ b/sound/firewire/Kconfig
@@ -65,6 +65,7 @@ config SND_FIREWORKS
65 tristate "Echo Fireworks board module support" 65 tristate "Echo Fireworks board module support"
66 select SND_FIREWIRE_LIB 66 select SND_FIREWIRE_LIB
67 select SND_RAWMIDI 67 select SND_RAWMIDI
68 select SND_PCM
68 help 69 help
69 Say Y here to include support for FireWire devices based 70 Say Y here to include support for FireWire devices based
70 on Echo Digital Audio Fireworks board: 71 on Echo Digital Audio Fireworks board:
diff --git a/sound/firewire/fireworks/Makefile b/sound/firewire/fireworks/Makefile
index a2cecc6d8a4a..d7ebf834fece 100644
--- a/sound/firewire/fireworks/Makefile
+++ b/sound/firewire/fireworks/Makefile
@@ -1,4 +1,4 @@
1snd-fireworks-objs := fireworks_transaction.o fireworks_command.o \ 1snd-fireworks-objs := fireworks_transaction.o fireworks_command.o \
2 fireworks_stream.o fireworks_proc.o fireworks_midi.o \ 2 fireworks_stream.o fireworks_proc.o fireworks_midi.o \
3 fireworks.o 3 fireworks_pcm.o fireworks.o
4obj-m += snd-fireworks.o 4obj-m += snd-fireworks.o
diff --git a/sound/firewire/fireworks/fireworks.c b/sound/firewire/fireworks/fireworks.c
index 9204c7c56513..d7877c79e32f 100644
--- a/sound/firewire/fireworks/fireworks.c
+++ b/sound/firewire/fireworks/fireworks.c
@@ -232,6 +232,10 @@ efw_probe(struct fw_unit *unit,
232 goto error; 232 goto error;
233 } 233 }
234 234
235 err = snd_efw_create_pcm_devices(efw);
236 if (err < 0)
237 goto error;
238
235 err = snd_efw_stream_init_duplex(efw); 239 err = snd_efw_stream_init_duplex(efw);
236 if (err < 0) 240 if (err < 0)
237 goto error; 241 goto error;
diff --git a/sound/firewire/fireworks/fireworks.h b/sound/firewire/fireworks/fireworks.h
index d1819696754d..36419ca12d6f 100644
--- a/sound/firewire/fireworks/fireworks.h
+++ b/sound/firewire/fireworks/fireworks.h
@@ -23,6 +23,7 @@
23#include <sound/pcm.h> 23#include <sound/pcm.h>
24#include <sound/info.h> 24#include <sound/info.h>
25#include <sound/rawmidi.h> 25#include <sound/rawmidi.h>
26#include <sound/pcm_params.h>
26 27
27#include "../packets-buffer.h" 28#include "../packets-buffer.h"
28#include "../iso-resources.h" 29#include "../iso-resources.h"
@@ -201,6 +202,9 @@ void snd_efw_proc_init(struct snd_efw *efw);
201 202
202int snd_efw_create_midi_devices(struct snd_efw *efw); 203int snd_efw_create_midi_devices(struct snd_efw *efw);
203 204
205int snd_efw_create_pcm_devices(struct snd_efw *efw);
206int snd_efw_get_multiplier_mode(unsigned int sampling_rate, unsigned int *mode);
207
204#define SND_EFW_DEV_ENTRY(vendor, model) \ 208#define SND_EFW_DEV_ENTRY(vendor, model) \
205{ \ 209{ \
206 .match_flags = IEEE1394_MATCH_VENDOR_ID | \ 210 .match_flags = IEEE1394_MATCH_VENDOR_ID | \
diff --git a/sound/firewire/fireworks/fireworks_pcm.c b/sound/firewire/fireworks/fireworks_pcm.c
new file mode 100644
index 000000000000..15ec4e02b84f
--- /dev/null
+++ b/sound/firewire/fireworks/fireworks_pcm.c
@@ -0,0 +1,394 @@
1/*
2 * fireworks_pcm.c - a part of driver for Fireworks based devices
3 *
4 * Copyright (c) 2009-2010 Clemens Ladisch
5 * Copyright (c) 2013-2014 Takashi Sakamoto
6 *
7 * Licensed under the terms of the GNU General Public License, version 2.
8 */
9#include "./fireworks.h"
10
11/*
12 * NOTE:
13 * Fireworks changes its AMDTP channels for PCM data according to its sampling
14 * rate. There are three modes. Here _XX is either _rx or _tx.
15 * 0: 32.0- 48.0 kHz then snd_efw_hwinfo.amdtp_XX_pcm_channels applied
16 * 1: 88.2- 96.0 kHz then snd_efw_hwinfo.amdtp_XX_pcm_channels_2x applied
17 * 2: 176.4-192.0 kHz then snd_efw_hwinfo.amdtp_XX_pcm_channels_4x applied
18 *
19 * The number of PCM channels for analog input and output are always fixed but
20 * the number of PCM channels for digital input and output are differed.
21 *
22 * Additionally, according to "AudioFire Owner's Manual Version 2.2", in some
23 * model, the number of PCM channels for digital input has more restriction
24 * depending on which digital interface is selected.
25 * - S/PDIF coaxial and optical : use input 1-2
26 * - ADAT optical at 32.0-48.0 kHz : use input 1-8
27 * - ADAT optical at 88.2-96.0 kHz : use input 1-4 (S/MUX format)
28 *
29 * The data in AMDTP channels for blank PCM channels are zero.
30 */
31static const unsigned int freq_table[] = {
32 /* multiplier mode 0 */
33 [0] = 32000,
34 [1] = 44100,
35 [2] = 48000,
36 /* multiplier mode 1 */
37 [3] = 88200,
38 [4] = 96000,
39 /* multiplier mode 2 */
40 [5] = 176400,
41 [6] = 192000,
42};
43
44static inline unsigned int
45get_multiplier_mode_with_index(unsigned int index)
46{
47 return ((int)index - 1) / 2;
48}
49
50int snd_efw_get_multiplier_mode(unsigned int sampling_rate, unsigned int *mode)
51{
52 unsigned int i;
53
54 for (i = 0; i < ARRAY_SIZE(freq_table); i++) {
55 if (freq_table[i] == sampling_rate) {
56 *mode = get_multiplier_mode_with_index(i);
57 return 0;
58 }
59 }
60
61 return -EINVAL;
62}
63
64static int
65hw_rule_rate(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
66{
67 unsigned int *pcm_channels = rule->private;
68 struct snd_interval *r =
69 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
70 const struct snd_interval *c =
71 hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS);
72 struct snd_interval t = {
73 .min = UINT_MAX, .max = 0, .integer = 1
74 };
75 unsigned int i, mode;
76
77 for (i = 0; i < ARRAY_SIZE(freq_table); i++) {
78 mode = get_multiplier_mode_with_index(i);
79 if (!snd_interval_test(c, pcm_channels[mode]))
80 continue;
81
82 t.min = min(t.min, freq_table[i]);
83 t.max = max(t.max, freq_table[i]);
84 }
85
86 return snd_interval_refine(r, &t);
87}
88
89static int
90hw_rule_channels(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
91{
92 unsigned int *pcm_channels = rule->private;
93 struct snd_interval *c =
94 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
95 const struct snd_interval *r =
96 hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
97 struct snd_interval t = {
98 .min = UINT_MAX, .max = 0, .integer = 1
99 };
100 unsigned int i, mode;
101
102 for (i = 0; i < ARRAY_SIZE(freq_table); i++) {
103 mode = get_multiplier_mode_with_index(i);
104 if (!snd_interval_test(r, freq_table[i]))
105 continue;
106
107 t.min = min(t.min, pcm_channels[mode]);
108 t.max = max(t.max, pcm_channels[mode]);
109 }
110
111 return snd_interval_refine(c, &t);
112}
113
114static void
115limit_channels(struct snd_pcm_hardware *hw, unsigned int *pcm_channels)
116{
117 unsigned int i, mode;
118
119 hw->channels_min = UINT_MAX;
120 hw->channels_max = 0;
121
122 for (i = 0; i < ARRAY_SIZE(freq_table); i++) {
123 mode = get_multiplier_mode_with_index(i);
124 if (pcm_channels[mode] == 0)
125 continue;
126
127 hw->channels_min = min(hw->channels_min, pcm_channels[mode]);
128 hw->channels_max = max(hw->channels_max, pcm_channels[mode]);
129 }
130}
131
132static void
133limit_period_and_buffer(struct snd_pcm_hardware *hw)
134{
135 hw->periods_min = 2; /* SNDRV_PCM_INFO_BATCH */
136 hw->periods_max = UINT_MAX;
137
138 hw->period_bytes_min = 4 * hw->channels_max; /* bytes for a frame */
139
140 /* Just to prevent from allocating much pages. */
141 hw->period_bytes_max = hw->period_bytes_min * 2048;
142 hw->buffer_bytes_max = hw->period_bytes_max * hw->periods_min;
143}
144
145static int
146pcm_init_hw_params(struct snd_efw *efw,
147 struct snd_pcm_substream *substream)
148{
149 struct snd_pcm_runtime *runtime = substream->runtime;
150 struct amdtp_stream *s;
151 unsigned int *pcm_channels;
152 int err;
153
154 runtime->hw.info = SNDRV_PCM_INFO_BATCH |
155 SNDRV_PCM_INFO_BLOCK_TRANSFER |
156 SNDRV_PCM_INFO_INTERLEAVED |
157 SNDRV_PCM_INFO_JOINT_DUPLEX |
158 SNDRV_PCM_INFO_MMAP |
159 SNDRV_PCM_INFO_MMAP_VALID;
160
161 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
162 runtime->hw.formats = AMDTP_IN_PCM_FORMAT_BITS;
163 s = &efw->tx_stream;
164 pcm_channels = efw->pcm_capture_channels;
165 } else {
166 runtime->hw.formats = AMDTP_OUT_PCM_FORMAT_BITS;
167 s = &efw->rx_stream;
168 pcm_channels = efw->pcm_playback_channels;
169 }
170
171 /* limit rates */
172 runtime->hw.rates = efw->supported_sampling_rate,
173 snd_pcm_limit_hw_rates(runtime);
174
175 limit_channels(&runtime->hw, pcm_channels);
176 limit_period_and_buffer(&runtime->hw);
177
178 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
179 hw_rule_channels, pcm_channels,
180 SNDRV_PCM_HW_PARAM_RATE, -1);
181 if (err < 0)
182 goto end;
183
184 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
185 hw_rule_rate, pcm_channels,
186 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
187 if (err < 0)
188 goto end;
189
190 err = amdtp_stream_add_pcm_hw_constraints(s, runtime);
191end:
192 return err;
193}
194
195static int pcm_open(struct snd_pcm_substream *substream)
196{
197 struct snd_efw *efw = substream->private_data;
198 int sampling_rate;
199 unsigned int clock_source;
200 int err;
201
202 err = pcm_init_hw_params(efw, substream);
203 if (err < 0)
204 goto end;
205
206 err = snd_efw_command_get_clock_source(efw, &clock_source);
207 if (err < 0)
208 goto end;
209
210 /*
211 * When source of clock is not internal or any PCM streams are running,
212 * available sampling rate is limited at current sampling rate.
213 */
214 if ((clock_source != SND_EFW_CLOCK_SOURCE_INTERNAL) ||
215 amdtp_stream_pcm_running(&efw->tx_stream) ||
216 amdtp_stream_pcm_running(&efw->rx_stream)) {
217 err = snd_efw_command_get_sampling_rate(efw, &sampling_rate);
218 if (err < 0)
219 goto end;
220 substream->runtime->hw.rate_min = sampling_rate;
221 substream->runtime->hw.rate_max = sampling_rate;
222 }
223
224 snd_pcm_set_sync(substream);
225end:
226 return err;
227}
228
229static int pcm_close(struct snd_pcm_substream *substream)
230{
231 return 0;
232}
233
234static int pcm_capture_hw_params(struct snd_pcm_substream *substream,
235 struct snd_pcm_hw_params *hw_params)
236{
237 struct snd_efw *efw = substream->private_data;
238
239 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN)
240 atomic_inc(&efw->capture_substreams);
241 amdtp_stream_set_pcm_format(&efw->tx_stream, params_format(hw_params));
242
243 return snd_pcm_lib_alloc_vmalloc_buffer(substream,
244 params_buffer_bytes(hw_params));
245}
246static int pcm_playback_hw_params(struct snd_pcm_substream *substream,
247 struct snd_pcm_hw_params *hw_params)
248{
249 struct snd_efw *efw = substream->private_data;
250
251 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN)
252 atomic_inc(&efw->playback_substreams);
253 amdtp_stream_set_pcm_format(&efw->rx_stream, params_format(hw_params));
254
255 return snd_pcm_lib_alloc_vmalloc_buffer(substream,
256 params_buffer_bytes(hw_params));
257}
258
259static int pcm_capture_hw_free(struct snd_pcm_substream *substream)
260{
261 struct snd_efw *efw = substream->private_data;
262
263 if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
264 atomic_dec(&efw->capture_substreams);
265
266 snd_efw_stream_stop_duplex(efw);
267
268 return snd_pcm_lib_free_vmalloc_buffer(substream);
269}
270static int pcm_playback_hw_free(struct snd_pcm_substream *substream)
271{
272 struct snd_efw *efw = substream->private_data;
273
274 if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
275 atomic_dec(&efw->playback_substreams);
276
277 snd_efw_stream_stop_duplex(efw);
278
279 return snd_pcm_lib_free_vmalloc_buffer(substream);
280}
281
282static int pcm_capture_prepare(struct snd_pcm_substream *substream)
283{
284 struct snd_efw *efw = substream->private_data;
285 struct snd_pcm_runtime *runtime = substream->runtime;
286 int err;
287
288 err = snd_efw_stream_start_duplex(efw, runtime->rate);
289 if (err >= 0)
290 amdtp_stream_pcm_prepare(&efw->tx_stream);
291
292 return err;
293}
294static int pcm_playback_prepare(struct snd_pcm_substream *substream)
295{
296 struct snd_efw *efw = substream->private_data;
297 struct snd_pcm_runtime *runtime = substream->runtime;
298 int err;
299
300 err = snd_efw_stream_start_duplex(efw, runtime->rate);
301 if (err >= 0)
302 amdtp_stream_pcm_prepare(&efw->rx_stream);
303
304 return err;
305}
306
307static int pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
308{
309 struct snd_efw *efw = substream->private_data;
310
311 switch (cmd) {
312 case SNDRV_PCM_TRIGGER_START:
313 amdtp_stream_pcm_trigger(&efw->tx_stream, substream);
314 break;
315 case SNDRV_PCM_TRIGGER_STOP:
316 amdtp_stream_pcm_trigger(&efw->tx_stream, NULL);
317 break;
318 default:
319 return -EINVAL;
320 }
321
322 return 0;
323}
324static int pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
325{
326 struct snd_efw *efw = substream->private_data;
327
328 switch (cmd) {
329 case SNDRV_PCM_TRIGGER_START:
330 amdtp_stream_pcm_trigger(&efw->rx_stream, substream);
331 break;
332 case SNDRV_PCM_TRIGGER_STOP:
333 amdtp_stream_pcm_trigger(&efw->rx_stream, NULL);
334 break;
335 default:
336 return -EINVAL;
337 }
338
339 return 0;
340}
341
342static snd_pcm_uframes_t pcm_capture_pointer(struct snd_pcm_substream *sbstrm)
343{
344 struct snd_efw *efw = sbstrm->private_data;
345 return amdtp_stream_pcm_pointer(&efw->tx_stream);
346}
347static snd_pcm_uframes_t pcm_playback_pointer(struct snd_pcm_substream *sbstrm)
348{
349 struct snd_efw *efw = sbstrm->private_data;
350 return amdtp_stream_pcm_pointer(&efw->rx_stream);
351}
352
353static const struct snd_pcm_ops pcm_capture_ops = {
354 .open = pcm_open,
355 .close = pcm_close,
356 .ioctl = snd_pcm_lib_ioctl,
357 .hw_params = pcm_capture_hw_params,
358 .hw_free = pcm_capture_hw_free,
359 .prepare = pcm_capture_prepare,
360 .trigger = pcm_capture_trigger,
361 .pointer = pcm_capture_pointer,
362 .page = snd_pcm_lib_get_vmalloc_page,
363};
364
365static const struct snd_pcm_ops pcm_playback_ops = {
366 .open = pcm_open,
367 .close = pcm_close,
368 .ioctl = snd_pcm_lib_ioctl,
369 .hw_params = pcm_playback_hw_params,
370 .hw_free = pcm_playback_hw_free,
371 .prepare = pcm_playback_prepare,
372 .trigger = pcm_playback_trigger,
373 .pointer = pcm_playback_pointer,
374 .page = snd_pcm_lib_get_vmalloc_page,
375 .mmap = snd_pcm_lib_mmap_vmalloc,
376};
377
378int snd_efw_create_pcm_devices(struct snd_efw *efw)
379{
380 struct snd_pcm *pcm;
381 int err;
382
383 err = snd_pcm_new(efw->card, efw->card->driver, 0, 1, 1, &pcm);
384 if (err < 0)
385 goto end;
386
387 pcm->private_data = efw;
388 snprintf(pcm->name, sizeof(pcm->name), "%s PCM", efw->card->shortname);
389 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &pcm_playback_ops);
390 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &pcm_capture_ops);
391end:
392 return err;
393}
394
diff --git a/sound/firewire/fireworks/fireworks_stream.c b/sound/firewire/fireworks/fireworks_stream.c
index f9d836fc5039..1860914a3e0f 100644
--- a/sound/firewire/fireworks/fireworks_stream.c
+++ b/sound/firewire/fireworks/fireworks_stream.c
@@ -9,39 +9,6 @@
9 9
10#define CALLBACK_TIMEOUT 100 10#define CALLBACK_TIMEOUT 100
11 11
12static unsigned int freq_table[] = {
13 /* multiplier mode 0 */
14 [0] = 32000,
15 [1] = 44100,
16 [2] = 48000,
17 /* multiplier mode 1 */
18 [3] = 88200,
19 [4] = 96000,
20 /* multiplier mode 2 */
21 [5] = 176400,
22 [6] = 192000,
23};
24
25static inline unsigned int
26get_multiplier_mode_with_index(unsigned int index)
27{
28 return ((int)index - 1) / 2;
29}
30
31int snd_efw_get_multiplier_mode(unsigned int sampling_rate, unsigned int *mode)
32{
33 unsigned int i;
34
35 for (i = 0; i < ARRAY_SIZE(freq_table); i++) {
36 if (freq_table[i] == sampling_rate) {
37 *mode = get_multiplier_mode_with_index(i);
38 return 0;
39 }
40 }
41
42 return -EINVAL;
43}
44
45static int 12static int
46init_stream(struct snd_efw *efw, struct amdtp_stream *stream) 13init_stream(struct snd_efw *efw, struct amdtp_stream *stream)
47{ 14{