blob: 67ade0775a5b21b45329ef54db02a5a812b966ae [file] [log] [blame]
Takashi Sakamoto3713d932014-11-29 00:59:28 +09001/*
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
Takashi Sakamoto5cd1d3f2014-12-09 00:10:42 +090010static int hw_rule_rate(struct snd_pcm_hw_params *params,
11 struct snd_pcm_hw_rule *rule)
Takashi Sakamoto3713d932014-11-29 00:59:28 +090012{
Takashi Sakamoto5cd1d3f2014-12-09 00:10:42 +090013 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
Takashi Sakamoto3713d932014-11-29 00:59:28 +090020 };
Takashi Sakamoto5cd1d3f2014-12-09 00:10:42 +090021 struct snd_oxfw_stream_formation formation;
Dan Carpenter5580ba72014-12-12 22:27:03 +030022 int i, err;
Takashi Sakamoto3713d932014-11-29 00:59:28 +090023
Takashi Sakamoto5cd1d3f2014-12-09 00:10:42 +090024 for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
25 if (formats[i] == NULL)
26 continue;
Takashi Sakamoto3713d932014-11-29 00:59:28 +090027
Takashi Sakamoto5cd1d3f2014-12-09 00:10:42 +090028 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;
Takashi Sakamoto3713d932014-11-29 00:59:28 +090033
Takashi Sakamoto5cd1d3f2014-12-09 00:10:42 +090034 t.min = min(t.min, formation.rate);
35 t.max = max(t.max, formation.rate);
36
37 }
38 return snd_interval_refine(r, &t);
Takashi Sakamoto3713d932014-11-29 00:59:28 +090039}
40
Takashi Sakamoto5cd1d3f2014-12-09 00:10:42 +090041static int hw_rule_channels(struct snd_pcm_hw_params *params,
42 struct snd_pcm_hw_rule *rule)
Takashi Sakamoto3713d932014-11-29 00:59:28 +090043{
Takashi Sakamoto5cd1d3f2014-12-09 00:10:42 +090044 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;
Dan Carpenter5580ba72014-12-12 22:27:03 +030050 int i, j, err;
Takashi Sakamoto5cd1d3f2014-12-09 00:10:42 +090051 unsigned int count, list[SND_OXFW_STREAM_FORMAT_ENTRIES] = {0};
Takashi Sakamoto3713d932014-11-29 00:59:28 +090052
Takashi Sakamoto5cd1d3f2014-12-09 00:10:42 +090053 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
80static void limit_channels_and_rates(struct snd_pcm_hardware *hw, u8 **formats)
81{
82 struct snd_oxfw_stream_formation formation;
Dan Carpenter5580ba72014-12-12 22:27:03 +030083 int i, err;
Takashi Sakamoto5cd1d3f2014-12-09 00:10:42 +090084
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
109static 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;
Takashi Sakamoto3713d932014-11-29 00:59:28 +0900119}
120
Takashi Sakamoto216e2562014-12-09 00:10:47 +0900121static int init_hw_params(struct snd_oxfw *oxfw,
122 struct snd_pcm_substream *substream)
Takashi Sakamoto3713d932014-11-29 00:59:28 +0900123{
Takashi Sakamoto3713d932014-11-29 00:59:28 +0900124 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Sakamoto5cd1d3f2014-12-09 00:10:42 +0900125 u8 **formats;
Takashi Sakamoto216e2562014-12-09 00:10:47 +0900126 struct amdtp_stream *stream;
Takashi Sakamoto3713d932014-11-29 00:59:28 +0900127 int err;
128
Takashi Sakamoto5cd1d3f2014-12-09 00:10:42 +0900129 runtime->hw.info = SNDRV_PCM_INFO_BATCH |
130 SNDRV_PCM_INFO_BLOCK_TRANSFER |
131 SNDRV_PCM_INFO_INTERLEAVED |
Takashi Sakamoto216e2562014-12-09 00:10:47 +0900132 SNDRV_PCM_INFO_JOINT_DUPLEX |
Takashi Sakamoto5cd1d3f2014-12-09 00:10:42 +0900133 SNDRV_PCM_INFO_MMAP |
134 SNDRV_PCM_INFO_MMAP_VALID;
Takashi Sakamoto3713d932014-11-29 00:59:28 +0900135
Takashi Sakamoto216e2562014-12-09 00:10:47 +0900136 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
Takashi Sakamoto5cd1d3f2014-12-09 00:10:42 +0900146 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);
Takashi Sakamoto3713d932014-11-29 00:59:28 +0900152 if (err < 0)
153 goto end;
Takashi Sakamoto5cd1d3f2014-12-09 00:10:42 +0900154
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);
Takashi Sakamoto3713d932014-11-29 00:59:28 +0900158 if (err < 0)
159 goto end;
160
Takashi Sakamoto216e2562014-12-09 00:10:47 +0900161 err = amdtp_stream_add_pcm_hw_constraints(stream, runtime);
162end:
163 return err;
164}
165
166static 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);
Takashi Sakamoto5cd1d3f2014-12-09 00:10:42 +0900179 if (err < 0)
180 goto end;
181
Takashi Sakamoto216e2562014-12-09 00:10:47 +0900182 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;
186end:
187 return err;
188}
189
190static int pcm_open(struct snd_pcm_substream *substream)
191{
192 struct snd_oxfw *oxfw = substream->private_data;
193 int err;
194
Takashi Sakamoto8985f4a2014-12-09 00:10:49 +0900195 err = snd_oxfw_stream_lock_try(oxfw);
Takashi Sakamoto216e2562014-12-09 00:10:47 +0900196 if (err < 0)
197 goto end;
198
Takashi Sakamoto8985f4a2014-12-09 00:10:49 +0900199 err = init_hw_params(oxfw, substream);
200 if (err < 0)
201 goto err_locked;
202
Takashi Sakamoto216e2562014-12-09 00:10:47 +0900203 /*
204 * When any PCM streams are already running, the available sampling
205 * rate is limited at current value.
206 */
207 if (amdtp_stream_pcm_running(&oxfw->tx_stream) ||
208 amdtp_stream_pcm_running(&oxfw->rx_stream)) {
209 err = limit_to_current_params(substream);
210 if (err < 0)
211 goto end;
212 }
213
Takashi Sakamoto5cd1d3f2014-12-09 00:10:42 +0900214 snd_pcm_set_sync(substream);
Takashi Sakamoto3713d932014-11-29 00:59:28 +0900215end:
216 return err;
Takashi Sakamoto8985f4a2014-12-09 00:10:49 +0900217err_locked:
218 snd_oxfw_stream_lock_release(oxfw);
219 return err;
Takashi Sakamoto3713d932014-11-29 00:59:28 +0900220}
221
222static int pcm_close(struct snd_pcm_substream *substream)
223{
Takashi Sakamoto8985f4a2014-12-09 00:10:49 +0900224 struct snd_oxfw *oxfw = substream->private_data;
225
226 snd_oxfw_stream_lock_release(oxfw);
Takashi Sakamoto3713d932014-11-29 00:59:28 +0900227 return 0;
228}
229
Takashi Sakamoto216e2562014-12-09 00:10:47 +0900230static int pcm_capture_hw_params(struct snd_pcm_substream *substream,
231 struct snd_pcm_hw_params *hw_params)
Takashi Sakamoto3713d932014-11-29 00:59:28 +0900232{
233 struct snd_oxfw *oxfw = substream->private_data;
Takashi Sakamoto3713d932014-11-29 00:59:28 +0900234
Takashi Sakamoto216e2562014-12-09 00:10:47 +0900235
236 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
237 mutex_lock(&oxfw->mutex);
238 oxfw->capture_substreams++;
239 mutex_unlock(&oxfw->mutex);
240 }
241
242 amdtp_stream_set_pcm_format(&oxfw->tx_stream, params_format(hw_params));
243
244 return snd_pcm_lib_alloc_vmalloc_buffer(substream,
245 params_buffer_bytes(hw_params));
246}
247static int pcm_playback_hw_params(struct snd_pcm_substream *substream,
248 struct snd_pcm_hw_params *hw_params)
249{
250 struct snd_oxfw *oxfw = substream->private_data;
251
252 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
253 mutex_lock(&oxfw->mutex);
254 oxfw->playback_substreams++;
255 mutex_unlock(&oxfw->mutex);
256 }
257
Takashi Sakamotof3699e22014-12-09 00:10:44 +0900258 amdtp_stream_set_pcm_format(&oxfw->rx_stream, params_format(hw_params));
Takashi Sakamoto216e2562014-12-09 00:10:47 +0900259
Takashi Sakamotof3699e22014-12-09 00:10:44 +0900260 return snd_pcm_lib_alloc_vmalloc_buffer(substream,
261 params_buffer_bytes(hw_params));
Takashi Sakamoto3713d932014-11-29 00:59:28 +0900262}
263
Takashi Sakamoto216e2562014-12-09 00:10:47 +0900264static int pcm_capture_hw_free(struct snd_pcm_substream *substream)
Takashi Sakamoto3713d932014-11-29 00:59:28 +0900265{
266 struct snd_oxfw *oxfw = substream->private_data;
267
268 mutex_lock(&oxfw->mutex);
Takashi Sakamoto216e2562014-12-09 00:10:47 +0900269
270 if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
271 oxfw->capture_substreams--;
272
273 snd_oxfw_stream_stop_simplex(oxfw, &oxfw->tx_stream);
274
275 mutex_unlock(&oxfw->mutex);
276
277 return snd_pcm_lib_free_vmalloc_buffer(substream);
278}
279static int pcm_playback_hw_free(struct snd_pcm_substream *substream)
280{
281 struct snd_oxfw *oxfw = substream->private_data;
282
283 mutex_lock(&oxfw->mutex);
284
285 if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
286 oxfw->playback_substreams--;
287
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900288 snd_oxfw_stream_stop_simplex(oxfw, &oxfw->rx_stream);
Takashi Sakamoto216e2562014-12-09 00:10:47 +0900289
Takashi Sakamoto3713d932014-11-29 00:59:28 +0900290 mutex_unlock(&oxfw->mutex);
291
292 return snd_pcm_lib_free_vmalloc_buffer(substream);
293}
294
Takashi Sakamoto216e2562014-12-09 00:10:47 +0900295static int pcm_capture_prepare(struct snd_pcm_substream *substream)
296{
297 struct snd_oxfw *oxfw = substream->private_data;
298 struct snd_pcm_runtime *runtime = substream->runtime;
299 int err;
300
301 mutex_lock(&oxfw->mutex);
302 err = snd_oxfw_stream_start_simplex(oxfw, &oxfw->tx_stream,
303 runtime->rate, runtime->channels);
304 mutex_unlock(&oxfw->mutex);
305 if (err < 0)
306 goto end;
307
308 amdtp_stream_pcm_prepare(&oxfw->tx_stream);
309end:
310 return err;
311}
312static int pcm_playback_prepare(struct snd_pcm_substream *substream)
Takashi Sakamoto3713d932014-11-29 00:59:28 +0900313{
314 struct snd_oxfw *oxfw = substream->private_data;
Takashi Sakamotof3699e22014-12-09 00:10:44 +0900315 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Sakamoto3713d932014-11-29 00:59:28 +0900316 int err;
317
318 mutex_lock(&oxfw->mutex);
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900319 err = snd_oxfw_stream_start_simplex(oxfw, &oxfw->rx_stream,
320 runtime->rate, runtime->channels);
Takashi Sakamotof3699e22014-12-09 00:10:44 +0900321 mutex_unlock(&oxfw->mutex);
Takashi Sakamoto3713d932014-11-29 00:59:28 +0900322 if (err < 0)
323 goto end;
324
325 amdtp_stream_pcm_prepare(&oxfw->rx_stream);
326end:
Takashi Sakamoto3713d932014-11-29 00:59:28 +0900327 return err;
328}
329
Takashi Sakamoto216e2562014-12-09 00:10:47 +0900330static int pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
331{
332 struct snd_oxfw *oxfw = substream->private_data;
333 struct snd_pcm_substream *pcm;
334
335 switch (cmd) {
336 case SNDRV_PCM_TRIGGER_START:
337 pcm = substream;
338 break;
339 case SNDRV_PCM_TRIGGER_STOP:
340 pcm = NULL;
341 break;
342 default:
343 return -EINVAL;
344 }
345 amdtp_stream_pcm_trigger(&oxfw->tx_stream, pcm);
346 return 0;
347}
348static int pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
Takashi Sakamoto3713d932014-11-29 00:59:28 +0900349{
350 struct snd_oxfw *oxfw = substream->private_data;
351 struct snd_pcm_substream *pcm;
352
353 switch (cmd) {
354 case SNDRV_PCM_TRIGGER_START:
355 pcm = substream;
356 break;
357 case SNDRV_PCM_TRIGGER_STOP:
358 pcm = NULL;
359 break;
360 default:
361 return -EINVAL;
362 }
363 amdtp_stream_pcm_trigger(&oxfw->rx_stream, pcm);
364 return 0;
365}
366
Takashi Sakamoto216e2562014-12-09 00:10:47 +0900367static snd_pcm_uframes_t pcm_capture_pointer(struct snd_pcm_substream *sbstm)
Takashi Sakamoto3713d932014-11-29 00:59:28 +0900368{
Takashi Sakamoto216e2562014-12-09 00:10:47 +0900369 struct snd_oxfw *oxfw = sbstm->private_data;
370
371 return amdtp_stream_pcm_pointer(&oxfw->tx_stream);
372}
373static snd_pcm_uframes_t pcm_playback_pointer(struct snd_pcm_substream *sbstm)
374{
375 struct snd_oxfw *oxfw = sbstm->private_data;
Takashi Sakamoto3713d932014-11-29 00:59:28 +0900376
377 return amdtp_stream_pcm_pointer(&oxfw->rx_stream);
378}
379
380int snd_oxfw_create_pcm(struct snd_oxfw *oxfw)
381{
Takashi Sakamoto216e2562014-12-09 00:10:47 +0900382 static struct snd_pcm_ops capture_ops = {
Takashi Sakamoto3713d932014-11-29 00:59:28 +0900383 .open = pcm_open,
384 .close = pcm_close,
385 .ioctl = snd_pcm_lib_ioctl,
Takashi Sakamoto216e2562014-12-09 00:10:47 +0900386 .hw_params = pcm_capture_hw_params,
387 .hw_free = pcm_capture_hw_free,
388 .prepare = pcm_capture_prepare,
389 .trigger = pcm_capture_trigger,
390 .pointer = pcm_capture_pointer,
391 .page = snd_pcm_lib_get_vmalloc_page,
392 .mmap = snd_pcm_lib_mmap_vmalloc,
393 };
394 static struct snd_pcm_ops playback_ops = {
395 .open = pcm_open,
396 .close = pcm_close,
397 .ioctl = snd_pcm_lib_ioctl,
398 .hw_params = pcm_playback_hw_params,
399 .hw_free = pcm_playback_hw_free,
400 .prepare = pcm_playback_prepare,
401 .trigger = pcm_playback_trigger,
402 .pointer = pcm_playback_pointer,
Takashi Sakamoto3713d932014-11-29 00:59:28 +0900403 .page = snd_pcm_lib_get_vmalloc_page,
404 .mmap = snd_pcm_lib_mmap_vmalloc,
405 };
406 struct snd_pcm *pcm;
Takashi Sakamoto216e2562014-12-09 00:10:47 +0900407 unsigned int cap = 0;
Takashi Sakamoto3713d932014-11-29 00:59:28 +0900408 int err;
409
Takashi Sakamoto216e2562014-12-09 00:10:47 +0900410 if (oxfw->has_output)
411 cap = 1;
412
413 err = snd_pcm_new(oxfw->card, oxfw->card->driver, 0, 1, cap, &pcm);
Takashi Sakamoto3713d932014-11-29 00:59:28 +0900414 if (err < 0)
415 return err;
Takashi Sakamoto216e2562014-12-09 00:10:47 +0900416
Takashi Sakamoto3713d932014-11-29 00:59:28 +0900417 pcm->private_data = oxfw;
418 strcpy(pcm->name, oxfw->card->shortname);
Takashi Sakamoto216e2562014-12-09 00:10:47 +0900419 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &playback_ops);
420 if (cap > 0)
421 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &capture_ops);
422
Takashi Sakamoto3713d932014-11-29 00:59:28 +0900423 return 0;
424}