ALSA: oxfw: Add proc interface for debugging purpose
[deliverable/linux.git] / sound / firewire / oxfw / oxfw-pcm.c
CommitLineData
3713d93a
TS
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
5cd1d3f4
TS
10static int hw_rule_rate(struct snd_pcm_hw_params *params,
11 struct snd_pcm_hw_rule *rule)
3713d93a 12{
5cd1d3f4
TS
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);
3713d93a
TS
39}
40
5cd1d3f4
TS
41static int hw_rule_channels(struct snd_pcm_hw_params *params,
42 struct snd_pcm_hw_rule *rule)
3713d93a 43{
5cd1d3f4
TS
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);
3713d93a
TS
78}
79
5cd1d3f4 80static void limit_channels_and_rates(struct snd_pcm_hardware *hw, u8 **formats)
3713d93a 81{
5cd1d3f4
TS
82 struct snd_oxfw_stream_formation formation;
83 unsigned int i, err;
3713d93a 84
5cd1d3f4
TS
85 hw->channels_min = UINT_MAX;
86 hw->channels_max = 0;
3713d93a 87
5cd1d3f4
TS
88 hw->rate_min = UINT_MAX;
89 hw->rate_max = 0;
90 hw->rates = 0;
3713d93a 91
5cd1d3f4
TS
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 }
3713d93a
TS
107}
108
5cd1d3f4 109static void limit_period_and_buffer(struct snd_pcm_hardware *hw)
3713d93a 110{
5cd1d3f4
TS
111 hw->periods_min = 2; /* SNDRV_PCM_INFO_BATCH */
112 hw->periods_max = UINT_MAX;
3713d93a 113
5cd1d3f4
TS
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;
3713d93a
TS
119}
120
121static int pcm_open(struct snd_pcm_substream *substream)
122{
3713d93a
TS
123 struct snd_oxfw *oxfw = substream->private_data;
124 struct snd_pcm_runtime *runtime = substream->runtime;
5cd1d3f4 125 u8 **formats;
3713d93a
TS
126 int err;
127
5cd1d3f4
TS
128 formats = oxfw->rx_stream_formats;
129
130 runtime->hw.info = SNDRV_PCM_INFO_BATCH |
131 SNDRV_PCM_INFO_BLOCK_TRANSFER |
132 SNDRV_PCM_INFO_INTERLEAVED |
133 SNDRV_PCM_INFO_MMAP |
134 SNDRV_PCM_INFO_MMAP_VALID;
3713d93a 135
5cd1d3f4
TS
136 limit_channels_and_rates(&runtime->hw, formats);
137 limit_period_and_buffer(&runtime->hw);
3713d93a 138
5cd1d3f4
TS
139 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
140 hw_rule_channels, formats,
141 SNDRV_PCM_HW_PARAM_RATE, -1);
3713d93a
TS
142 if (err < 0)
143 goto end;
5cd1d3f4
TS
144
145 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
146 hw_rule_rate, formats,
147 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
3713d93a
TS
148 if (err < 0)
149 goto end;
150
151 err = amdtp_stream_add_pcm_hw_constraints(&oxfw->rx_stream, runtime);
5cd1d3f4
TS
152 if (err < 0)
153 goto end;
154
155 snd_pcm_set_sync(substream);
3713d93a
TS
156end:
157 return err;
158}
159
160static int pcm_close(struct snd_pcm_substream *substream)
161{
162 return 0;
163}
164
165static int pcm_hw_params(struct snd_pcm_substream *substream,
166 struct snd_pcm_hw_params *hw_params)
167{
168 struct snd_oxfw *oxfw = substream->private_data;
169 int err;
170
171 mutex_lock(&oxfw->mutex);
172
173 snd_oxfw_stream_stop_simplex(oxfw);
174
175 err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
176 params_buffer_bytes(hw_params));
177 if (err < 0)
178 goto error;
179
180 amdtp_stream_set_parameters(&oxfw->rx_stream,
181 params_rate(hw_params),
182 params_channels(hw_params),
183 0);
184
185 amdtp_stream_set_pcm_format(&oxfw->rx_stream,
186 params_format(hw_params));
187
188 err = avc_general_set_sig_fmt(oxfw->unit, params_rate(hw_params),
189 AVC_GENERAL_PLUG_DIR_IN, 0);
190 if (err < 0) {
191 dev_err(&oxfw->unit->device, "failed to set sample rate\n");
192 goto err_buffer;
193 }
194
195 return 0;
196
197err_buffer:
198 snd_pcm_lib_free_vmalloc_buffer(substream);
199error:
200 mutex_unlock(&oxfw->mutex);
201 return err;
202}
203
204static int pcm_hw_free(struct snd_pcm_substream *substream)
205{
206 struct snd_oxfw *oxfw = substream->private_data;
207
208 mutex_lock(&oxfw->mutex);
209 snd_oxfw_stream_stop_simplex(oxfw);
210 mutex_unlock(&oxfw->mutex);
211
212 return snd_pcm_lib_free_vmalloc_buffer(substream);
213}
214
215static int pcm_prepare(struct snd_pcm_substream *substream)
216{
217 struct snd_oxfw *oxfw = substream->private_data;
218 int err;
219
220 mutex_lock(&oxfw->mutex);
221
222 snd_oxfw_stream_stop_simplex(oxfw);
223
224 err = snd_oxfw_stream_start_simplex(oxfw);
225 if (err < 0)
226 goto end;
227
228 amdtp_stream_pcm_prepare(&oxfw->rx_stream);
229end:
230 mutex_unlock(&oxfw->mutex);
231 return err;
232}
233
234static int pcm_trigger(struct snd_pcm_substream *substream, int cmd)
235{
236 struct snd_oxfw *oxfw = substream->private_data;
237 struct snd_pcm_substream *pcm;
238
239 switch (cmd) {
240 case SNDRV_PCM_TRIGGER_START:
241 pcm = substream;
242 break;
243 case SNDRV_PCM_TRIGGER_STOP:
244 pcm = NULL;
245 break;
246 default:
247 return -EINVAL;
248 }
249 amdtp_stream_pcm_trigger(&oxfw->rx_stream, pcm);
250 return 0;
251}
252
253static snd_pcm_uframes_t pcm_pointer(struct snd_pcm_substream *substream)
254{
255 struct snd_oxfw *oxfw = substream->private_data;
256
257 return amdtp_stream_pcm_pointer(&oxfw->rx_stream);
258}
259
260int snd_oxfw_create_pcm(struct snd_oxfw *oxfw)
261{
262 static struct snd_pcm_ops ops = {
263 .open = pcm_open,
264 .close = pcm_close,
265 .ioctl = snd_pcm_lib_ioctl,
266 .hw_params = pcm_hw_params,
267 .hw_free = pcm_hw_free,
268 .prepare = pcm_prepare,
269 .trigger = pcm_trigger,
270 .pointer = pcm_pointer,
271 .page = snd_pcm_lib_get_vmalloc_page,
272 .mmap = snd_pcm_lib_mmap_vmalloc,
273 };
274 struct snd_pcm *pcm;
275 int err;
276
277 err = snd_pcm_new(oxfw->card, oxfw->card->driver, 0, 1, 0, &pcm);
278 if (err < 0)
279 return err;
280 pcm->private_data = oxfw;
281 strcpy(pcm->name, oxfw->card->shortname);
282 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &ops);
283 return 0;
284}
This page took 0.037448 seconds and 5 git commands to generate.