]>
Commit | Line | Data |
---|---|---|
1a59d1b8 | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
1da177e4 LT |
2 | /* |
3 | * Digital Audio (PCM) abstract layer | |
c1017a4c | 4 | * Copyright (c) by Jaroslav Kysela <[email protected]> |
1da177e4 | 5 | * Abramo Bagnara <[email protected]> |
1da177e4 LT |
6 | */ |
7 | ||
1da177e4 | 8 | #include <linux/slab.h> |
174cd4b1 | 9 | #include <linux/sched/signal.h> |
1da177e4 | 10 | #include <linux/time.h> |
3f7440a6 | 11 | #include <linux/math64.h> |
d81a6d71 | 12 | #include <linux/export.h> |
1da177e4 LT |
13 | #include <sound/core.h> |
14 | #include <sound/control.h> | |
2d3391ec | 15 | #include <sound/tlv.h> |
1da177e4 LT |
16 | #include <sound/info.h> |
17 | #include <sound/pcm.h> | |
18 | #include <sound/pcm_params.h> | |
19 | #include <sound/timer.h> | |
20 | ||
2c4842d3 TS |
21 | #include "pcm_local.h" |
22 | ||
f5914908 TI |
23 | #ifdef CONFIG_SND_PCM_XRUN_DEBUG |
24 | #define CREATE_TRACE_POINTS | |
25 | #include "pcm_trace.h" | |
26 | #else | |
27 | #define trace_hwptr(substream, pos, in_interrupt) | |
28 | #define trace_xrun(substream) | |
29 | #define trace_hw_ptr_error(substream, reason) | |
fccf5388 | 30 | #define trace_applptr(substream, prev, curr) |
f5914908 TI |
31 | #endif |
32 | ||
a9cd29e7 TI |
33 | static int fill_silence_frames(struct snd_pcm_substream *substream, |
34 | snd_pcm_uframes_t off, snd_pcm_uframes_t frames); | |
35 | ||
1da177e4 LT |
36 | /* |
37 | * fill ring buffer with silence | |
38 | * runtime->silence_start: starting pointer to silence area | |
39 | * runtime->silence_filled: size filled with silence | |
40 | * runtime->silence_threshold: threshold from application | |
41 | * runtime->silence_size: maximal size from application | |
42 | * | |
43 | * when runtime->silence_size >= runtime->boundary - fill processed area with silence immediately | |
44 | */ | |
877211f5 | 45 | void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_uframes_t new_hw_ptr) |
1da177e4 | 46 | { |
877211f5 | 47 | struct snd_pcm_runtime *runtime = substream->runtime; |
1da177e4 | 48 | snd_pcm_uframes_t frames, ofs, transfer; |
29d1a873 | 49 | int err; |
1da177e4 LT |
50 | |
51 | if (runtime->silence_size < runtime->boundary) { | |
52 | snd_pcm_sframes_t noise_dist, n; | |
aa30db06 TI |
53 | snd_pcm_uframes_t appl_ptr = READ_ONCE(runtime->control->appl_ptr); |
54 | if (runtime->silence_start != appl_ptr) { | |
55 | n = appl_ptr - runtime->silence_start; | |
1da177e4 LT |
56 | if (n < 0) |
57 | n += runtime->boundary; | |
58 | if ((snd_pcm_uframes_t)n < runtime->silence_filled) | |
59 | runtime->silence_filled -= n; | |
60 | else | |
61 | runtime->silence_filled = 0; | |
aa30db06 | 62 | runtime->silence_start = appl_ptr; |
1da177e4 | 63 | } |
235475cb | 64 | if (runtime->silence_filled >= runtime->buffer_size) |
1da177e4 | 65 | return; |
1da177e4 LT |
66 | noise_dist = snd_pcm_playback_hw_avail(runtime) + runtime->silence_filled; |
67 | if (noise_dist >= (snd_pcm_sframes_t) runtime->silence_threshold) | |
68 | return; | |
69 | frames = runtime->silence_threshold - noise_dist; | |
70 | if (frames > runtime->silence_size) | |
71 | frames = runtime->silence_size; | |
72 | } else { | |
73 | if (new_hw_ptr == ULONG_MAX) { /* initialization */ | |
74 | snd_pcm_sframes_t avail = snd_pcm_playback_hw_avail(runtime); | |
9e216e8a JK |
75 | if (avail > runtime->buffer_size) |
76 | avail = runtime->buffer_size; | |
1da177e4 LT |
77 | runtime->silence_filled = avail > 0 ? avail : 0; |
78 | runtime->silence_start = (runtime->status->hw_ptr + | |
79 | runtime->silence_filled) % | |
80 | runtime->boundary; | |
81 | } else { | |
82 | ofs = runtime->status->hw_ptr; | |
83 | frames = new_hw_ptr - ofs; | |
84 | if ((snd_pcm_sframes_t)frames < 0) | |
85 | frames += runtime->boundary; | |
86 | runtime->silence_filled -= frames; | |
87 | if ((snd_pcm_sframes_t)runtime->silence_filled < 0) { | |
88 | runtime->silence_filled = 0; | |
9a826ddb | 89 | runtime->silence_start = new_hw_ptr; |
1da177e4 | 90 | } else { |
9a826ddb | 91 | runtime->silence_start = ofs; |
1da177e4 | 92 | } |
1da177e4 LT |
93 | } |
94 | frames = runtime->buffer_size - runtime->silence_filled; | |
95 | } | |
7eaa943c TI |
96 | if (snd_BUG_ON(frames > runtime->buffer_size)) |
97 | return; | |
1da177e4 LT |
98 | if (frames == 0) |
99 | return; | |
9a826ddb | 100 | ofs = runtime->silence_start % runtime->buffer_size; |
1da177e4 LT |
101 | while (frames > 0) { |
102 | transfer = ofs + frames > runtime->buffer_size ? runtime->buffer_size - ofs : frames; | |
a9cd29e7 TI |
103 | err = fill_silence_frames(substream, ofs, transfer); |
104 | snd_BUG_ON(err < 0); | |
1da177e4 LT |
105 | runtime->silence_filled += transfer; |
106 | frames -= transfer; | |
107 | ofs = 0; | |
108 | } | |
109 | } | |
110 | ||
acb03d44 EB |
111 | #ifdef CONFIG_SND_DEBUG |
112 | void snd_pcm_debug_name(struct snd_pcm_substream *substream, | |
c0070110 TI |
113 | char *name, size_t len) |
114 | { | |
115 | snprintf(name, len, "pcmC%dD%d%c:%d", | |
116 | substream->pcm->card->number, | |
117 | substream->pcm->device, | |
118 | substream->stream ? 'c' : 'p', | |
119 | substream->number); | |
120 | } | |
acb03d44 EB |
121 | EXPORT_SYMBOL(snd_pcm_debug_name); |
122 | #endif | |
c0070110 | 123 | |
741b20cf JK |
124 | #define XRUN_DEBUG_BASIC (1<<0) |
125 | #define XRUN_DEBUG_STACK (1<<1) /* dump also stack */ | |
126 | #define XRUN_DEBUG_JIFFIESCHECK (1<<2) /* do jiffies check */ | |
741b20cf | 127 | |
ed3da3d9 | 128 | #ifdef CONFIG_SND_PCM_XRUN_DEBUG |
4d96eb25 | 129 | |
741b20cf JK |
130 | #define xrun_debug(substream, mask) \ |
131 | ((substream)->pstr->xrun_debug & (mask)) | |
0f17014b JN |
132 | #else |
133 | #define xrun_debug(substream, mask) 0 | |
134 | #endif | |
ed3da3d9 | 135 | |
741b20cf JK |
136 | #define dump_stack_on_xrun(substream) do { \ |
137 | if (xrun_debug(substream, XRUN_DEBUG_STACK)) \ | |
138 | dump_stack(); \ | |
ed3da3d9 TI |
139 | } while (0) |
140 | ||
9cd641ed TI |
141 | /* call with stream lock held */ |
142 | void __snd_pcm_xrun(struct snd_pcm_substream *substream) | |
1da177e4 | 143 | { |
13f040f9 JK |
144 | struct snd_pcm_runtime *runtime = substream->runtime; |
145 | ||
f5914908 | 146 | trace_xrun(substream); |
fcae40c9 BW |
147 | if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) { |
148 | struct timespec64 tstamp; | |
149 | ||
150 | snd_pcm_gettime(runtime, &tstamp); | |
80fe7430 AB |
151 | runtime->status->tstamp.tv_sec = tstamp.tv_sec; |
152 | runtime->status->tstamp.tv_nsec = tstamp.tv_nsec; | |
fcae40c9 | 153 | } |
1da177e4 | 154 | snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN); |
741b20cf | 155 | if (xrun_debug(substream, XRUN_DEBUG_BASIC)) { |
c0070110 | 156 | char name[16]; |
acb03d44 | 157 | snd_pcm_debug_name(substream, name, sizeof(name)); |
09e56df8 | 158 | pcm_warn(substream->pcm, "XRUN: %s\n", name); |
ed3da3d9 | 159 | dump_stack_on_xrun(substream); |
1da177e4 | 160 | } |
1da177e4 LT |
161 | } |
162 | ||
0f17014b | 163 | #ifdef CONFIG_SND_PCM_XRUN_DEBUG |
f5914908 | 164 | #define hw_ptr_error(substream, in_interrupt, reason, fmt, args...) \ |
4d96eb25 | 165 | do { \ |
f5914908 | 166 | trace_hw_ptr_error(substream, reason); \ |
4d96eb25 | 167 | if (xrun_debug(substream, XRUN_DEBUG_BASIC)) { \ |
f5914908 TI |
168 | pr_err_ratelimited("ALSA: PCM: [%c] " reason ": " fmt, \ |
169 | (in_interrupt) ? 'Q' : 'P', ##args); \ | |
4d96eb25 JK |
170 | dump_stack_on_xrun(substream); \ |
171 | } \ | |
172 | } while (0) | |
173 | ||
4d96eb25 JK |
174 | #else /* ! CONFIG_SND_PCM_XRUN_DEBUG */ |
175 | ||
4d96eb25 | 176 | #define hw_ptr_error(substream, fmt, args...) do { } while (0) |
4d96eb25 JK |
177 | |
178 | #endif | |
179 | ||
1250932e JK |
180 | int snd_pcm_update_state(struct snd_pcm_substream *substream, |
181 | struct snd_pcm_runtime *runtime) | |
1da177e4 LT |
182 | { |
183 | snd_pcm_uframes_t avail; | |
184 | ||
763e5067 | 185 | avail = snd_pcm_avail(substream); |
1da177e4 LT |
186 | if (avail > runtime->avail_max) |
187 | runtime->avail_max = avail; | |
4cdc115f TI |
188 | if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) { |
189 | if (avail >= runtime->buffer_size) { | |
1da177e4 | 190 | snd_pcm_drain_done(substream); |
4cdc115f TI |
191 | return -EPIPE; |
192 | } | |
193 | } else { | |
194 | if (avail >= runtime->stop_threshold) { | |
9cd641ed | 195 | __snd_pcm_xrun(substream); |
4cdc115f TI |
196 | return -EPIPE; |
197 | } | |
1da177e4 | 198 | } |
5daeba34 DD |
199 | if (runtime->twake) { |
200 | if (avail >= runtime->twake) | |
201 | wake_up(&runtime->tsleep); | |
202 | } else if (avail >= runtime->control->avail_min) | |
203 | wake_up(&runtime->sleep); | |
1da177e4 LT |
204 | return 0; |
205 | } | |
206 | ||
3179f620 | 207 | static void update_audio_tstamp(struct snd_pcm_substream *substream, |
fcae40c9 BW |
208 | struct timespec64 *curr_tstamp, |
209 | struct timespec64 *audio_tstamp) | |
3179f620 PLB |
210 | { |
211 | struct snd_pcm_runtime *runtime = substream->runtime; | |
212 | u64 audio_frames, audio_nsecs; | |
fcae40c9 | 213 | struct timespec64 driver_tstamp; |
3179f620 PLB |
214 | |
215 | if (runtime->tstamp_mode != SNDRV_PCM_TSTAMP_ENABLE) | |
216 | return; | |
217 | ||
218 | if (!(substream->ops->get_time_info) || | |
219 | (runtime->audio_tstamp_report.actual_type == | |
220 | SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT)) { | |
221 | ||
222 | /* | |
223 | * provide audio timestamp derived from pointer position | |
224 | * add delay only if requested | |
225 | */ | |
226 | ||
227 | audio_frames = runtime->hw_ptr_wrap + runtime->status->hw_ptr; | |
228 | ||
229 | if (runtime->audio_tstamp_config.report_delay) { | |
230 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) | |
231 | audio_frames -= runtime->delay; | |
232 | else | |
233 | audio_frames += runtime->delay; | |
234 | } | |
235 | audio_nsecs = div_u64(audio_frames * 1000000000LL, | |
236 | runtime->rate); | |
fcae40c9 | 237 | *audio_tstamp = ns_to_timespec64(audio_nsecs); |
3179f620 | 238 | } |
fcae40c9 BW |
239 | |
240 | if (runtime->status->audio_tstamp.tv_sec != audio_tstamp->tv_sec || | |
241 | runtime->status->audio_tstamp.tv_nsec != audio_tstamp->tv_nsec) { | |
80fe7430 AB |
242 | runtime->status->audio_tstamp.tv_sec = audio_tstamp->tv_sec; |
243 | runtime->status->audio_tstamp.tv_nsec = audio_tstamp->tv_nsec; | |
244 | runtime->status->tstamp.tv_sec = curr_tstamp->tv_sec; | |
245 | runtime->status->tstamp.tv_nsec = curr_tstamp->tv_nsec; | |
20e3f985 | 246 | } |
3179f620 | 247 | |
fcae40c9 | 248 | |
3179f620 PLB |
249 | /* |
250 | * re-take a driver timestamp to let apps detect if the reference tstamp | |
251 | * read by low-level hardware was provided with a delay | |
252 | */ | |
fcae40c9 | 253 | snd_pcm_gettime(substream->runtime, &driver_tstamp); |
3179f620 PLB |
254 | runtime->driver_tstamp = driver_tstamp; |
255 | } | |
256 | ||
f240406b JK |
257 | static int snd_pcm_update_hw_ptr0(struct snd_pcm_substream *substream, |
258 | unsigned int in_interrupt) | |
1da177e4 | 259 | { |
877211f5 | 260 | struct snd_pcm_runtime *runtime = substream->runtime; |
1da177e4 | 261 | snd_pcm_uframes_t pos; |
f240406b | 262 | snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base; |
bbf6ad13 JK |
263 | snd_pcm_sframes_t hdelta, delta; |
264 | unsigned long jdelta; | |
3509a03f | 265 | unsigned long curr_jiffies; |
fcae40c9 BW |
266 | struct timespec64 curr_tstamp; |
267 | struct timespec64 audio_tstamp; | |
0e8014d7 | 268 | int crossed_boundary = 0; |
1da177e4 | 269 | |
bbf6ad13 | 270 | old_hw_ptr = runtime->status->hw_ptr; |
3509a03f PLB |
271 | |
272 | /* | |
273 | * group pointer, time and jiffies reads to allow for more | |
274 | * accurate correlations/corrections. | |
275 | * The values are stored at the end of this routine after | |
276 | * corrections for hw_ptr position | |
277 | */ | |
f240406b | 278 | pos = substream->ops->pointer(substream); |
3509a03f | 279 | curr_jiffies = jiffies; |
4eeaaeae | 280 | if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) { |
3179f620 PLB |
281 | if ((substream->ops->get_time_info) && |
282 | (runtime->audio_tstamp_config.type_requested != SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT)) { | |
283 | substream->ops->get_time_info(substream, &curr_tstamp, | |
284 | &audio_tstamp, | |
285 | &runtime->audio_tstamp_config, | |
286 | &runtime->audio_tstamp_report); | |
287 | ||
288 | /* re-test in case tstamp type is not supported in hardware and was demoted to DEFAULT */ | |
289 | if (runtime->audio_tstamp_report.actual_type == SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT) | |
fcae40c9 | 290 | snd_pcm_gettime(runtime, &curr_tstamp); |
3179f620 | 291 | } else |
fcae40c9 | 292 | snd_pcm_gettime(runtime, &curr_tstamp); |
4eeaaeae PLB |
293 | } |
294 | ||
1da177e4 | 295 | if (pos == SNDRV_PCM_POS_XRUN) { |
9cd641ed | 296 | __snd_pcm_xrun(substream); |
1da177e4 LT |
297 | return -EPIPE; |
298 | } | |
f240406b | 299 | if (pos >= runtime->buffer_size) { |
09e56df8 | 300 | if (printk_ratelimit()) { |
f240406b | 301 | char name[16]; |
acb03d44 | 302 | snd_pcm_debug_name(substream, name, sizeof(name)); |
09e56df8 | 303 | pcm_err(substream->pcm, |
0ab1ace8 | 304 | "invalid position: %s, pos = %ld, buffer size = %ld, period size = %ld\n", |
09e56df8 TI |
305 | name, pos, runtime->buffer_size, |
306 | runtime->period_size); | |
f240406b JK |
307 | } |
308 | pos = 0; | |
cedb8118 | 309 | } |
f240406b | 310 | pos -= pos % runtime->min_align; |
f5914908 | 311 | trace_hwptr(substream, pos, in_interrupt); |
ed3da3d9 TI |
312 | hw_base = runtime->hw_ptr_base; |
313 | new_hw_ptr = hw_base + pos; | |
f240406b JK |
314 | if (in_interrupt) { |
315 | /* we know that one period was processed */ | |
316 | /* delta = "expected next hw_ptr" for in_interrupt != 0 */ | |
e7636925 | 317 | delta = runtime->hw_ptr_interrupt + runtime->period_size; |
f240406b | 318 | if (delta > new_hw_ptr) { |
bd76af0f | 319 | /* check for double acknowledged interrupts */ |
3509a03f | 320 | hdelta = curr_jiffies - runtime->hw_ptr_jiffies; |
13a98839 | 321 | if (hdelta > runtime->hw_ptr_buffer_jiffies/2 + 1) { |
bd76af0f | 322 | hw_base += runtime->buffer_size; |
0e8014d7 | 323 | if (hw_base >= runtime->boundary) { |
bd76af0f | 324 | hw_base = 0; |
0e8014d7 PLB |
325 | crossed_boundary++; |
326 | } | |
bd76af0f JK |
327 | new_hw_ptr = hw_base + pos; |
328 | goto __delta; | |
329 | } | |
1da177e4 | 330 | } |
1da177e4 | 331 | } |
f240406b JK |
332 | /* new_hw_ptr might be lower than old_hw_ptr in case when */ |
333 | /* pointer crosses the end of the ring buffer */ | |
334 | if (new_hw_ptr < old_hw_ptr) { | |
335 | hw_base += runtime->buffer_size; | |
0e8014d7 | 336 | if (hw_base >= runtime->boundary) { |
f240406b | 337 | hw_base = 0; |
0e8014d7 PLB |
338 | crossed_boundary++; |
339 | } | |
f240406b JK |
340 | new_hw_ptr = hw_base + pos; |
341 | } | |
342 | __delta: | |
b406e610 CL |
343 | delta = new_hw_ptr - old_hw_ptr; |
344 | if (delta < 0) | |
345 | delta += runtime->boundary; | |
ab69a490 | 346 | |
59ff878f | 347 | if (runtime->no_period_wakeup) { |
12ff414e | 348 | snd_pcm_sframes_t xrun_threshold; |
59ff878f CL |
349 | /* |
350 | * Without regular period interrupts, we have to check | |
351 | * the elapsed time to detect xruns. | |
352 | */ | |
3509a03f | 353 | jdelta = curr_jiffies - runtime->hw_ptr_jiffies; |
47228e48 CL |
354 | if (jdelta < runtime->hw_ptr_buffer_jiffies / 2) |
355 | goto no_delta_check; | |
59ff878f | 356 | hdelta = jdelta - delta * HZ / runtime->rate; |
12ff414e KA |
357 | xrun_threshold = runtime->hw_ptr_buffer_jiffies / 2 + 1; |
358 | while (hdelta > xrun_threshold) { | |
59ff878f CL |
359 | delta += runtime->buffer_size; |
360 | hw_base += runtime->buffer_size; | |
0e8014d7 | 361 | if (hw_base >= runtime->boundary) { |
59ff878f | 362 | hw_base = 0; |
0e8014d7 PLB |
363 | crossed_boundary++; |
364 | } | |
59ff878f CL |
365 | new_hw_ptr = hw_base + pos; |
366 | hdelta -= runtime->hw_ptr_buffer_jiffies; | |
367 | } | |
ab69a490 | 368 | goto no_delta_check; |
59ff878f | 369 | } |
ab69a490 | 370 | |
f240406b | 371 | /* something must be really wrong */ |
7b3a177b | 372 | if (delta >= runtime->buffer_size + runtime->period_size) { |
f5914908 TI |
373 | hw_ptr_error(substream, in_interrupt, "Unexpected hw_ptr", |
374 | "(stream=%i, pos=%ld, new_hw_ptr=%ld, old_hw_ptr=%ld)\n", | |
375 | substream->stream, (long)pos, | |
376 | (long)new_hw_ptr, (long)old_hw_ptr); | |
f240406b JK |
377 | return 0; |
378 | } | |
c87d9732 TI |
379 | |
380 | /* Do jiffies check only in xrun_debug mode */ | |
741b20cf | 381 | if (!xrun_debug(substream, XRUN_DEBUG_JIFFIESCHECK)) |
c87d9732 TI |
382 | goto no_jiffies_check; |
383 | ||
3e5b5016 TI |
384 | /* Skip the jiffies check for hardwares with BATCH flag. |
385 | * Such hardware usually just increases the position at each IRQ, | |
386 | * thus it can't give any strange position. | |
387 | */ | |
388 | if (runtime->hw.info & SNDRV_PCM_INFO_BATCH) | |
389 | goto no_jiffies_check; | |
f240406b | 390 | hdelta = delta; |
a4444da3 JK |
391 | if (hdelta < runtime->delay) |
392 | goto no_jiffies_check; | |
393 | hdelta -= runtime->delay; | |
3509a03f | 394 | jdelta = curr_jiffies - runtime->hw_ptr_jiffies; |
bbf6ad13 JK |
395 | if (((hdelta * HZ) / runtime->rate) > jdelta + HZ/100) { |
396 | delta = jdelta / | |
397 | (((runtime->period_size * HZ) / runtime->rate) | |
398 | + HZ/100); | |
f240406b JK |
399 | /* move new_hw_ptr according jiffies not pos variable */ |
400 | new_hw_ptr = old_hw_ptr; | |
ed69c6a8 | 401 | hw_base = delta; |
f240406b JK |
402 | /* use loop to avoid checks for delta overflows */ |
403 | /* the delta value is small or zero in most cases */ | |
404 | while (delta > 0) { | |
405 | new_hw_ptr += runtime->period_size; | |
0e8014d7 | 406 | if (new_hw_ptr >= runtime->boundary) { |
f240406b | 407 | new_hw_ptr -= runtime->boundary; |
0e8014d7 PLB |
408 | crossed_boundary--; |
409 | } | |
f240406b JK |
410 | delta--; |
411 | } | |
412 | /* align hw_base to buffer_size */ | |
f5914908 TI |
413 | hw_ptr_error(substream, in_interrupt, "hw_ptr skipping", |
414 | "(pos=%ld, delta=%ld, period=%ld, jdelta=%lu/%lu/%lu, hw_ptr=%ld/%ld)\n", | |
bbf6ad13 JK |
415 | (long)pos, (long)hdelta, |
416 | (long)runtime->period_size, jdelta, | |
ed69c6a8 | 417 | ((hdelta * HZ) / runtime->rate), hw_base, |
f240406b JK |
418 | (unsigned long)old_hw_ptr, |
419 | (unsigned long)new_hw_ptr); | |
ed69c6a8 JK |
420 | /* reset values to proper state */ |
421 | delta = 0; | |
422 | hw_base = new_hw_ptr - (new_hw_ptr % runtime->buffer_size); | |
bbf6ad13 | 423 | } |
3e5b5016 | 424 | no_jiffies_check: |
bbf6ad13 | 425 | if (delta > runtime->period_size + runtime->period_size / 2) { |
f5914908 TI |
426 | hw_ptr_error(substream, in_interrupt, |
427 | "Lost interrupts?", | |
428 | "(stream=%i, delta=%ld, new_hw_ptr=%ld, old_hw_ptr=%ld)\n", | |
ed3da3d9 | 429 | substream->stream, (long)delta, |
f240406b JK |
430 | (long)new_hw_ptr, |
431 | (long)old_hw_ptr); | |
ed3da3d9 | 432 | } |
f240406b | 433 | |
ab69a490 | 434 | no_delta_check: |
3179f620 PLB |
435 | if (runtime->status->hw_ptr == new_hw_ptr) { |
436 | update_audio_tstamp(substream, &curr_tstamp, &audio_tstamp); | |
f240406b | 437 | return 0; |
3179f620 | 438 | } |
ab1863fc | 439 | |
1da177e4 LT |
440 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && |
441 | runtime->silence_size > 0) | |
442 | snd_pcm_playback_silence(substream, new_hw_ptr); | |
443 | ||
e7636925 | 444 | if (in_interrupt) { |
ead4046b CL |
445 | delta = new_hw_ptr - runtime->hw_ptr_interrupt; |
446 | if (delta < 0) | |
447 | delta += runtime->boundary; | |
448 | delta -= (snd_pcm_uframes_t)delta % runtime->period_size; | |
449 | runtime->hw_ptr_interrupt += delta; | |
450 | if (runtime->hw_ptr_interrupt >= runtime->boundary) | |
451 | runtime->hw_ptr_interrupt -= runtime->boundary; | |
e7636925 | 452 | } |
ed3da3d9 | 453 | runtime->hw_ptr_base = hw_base; |
1da177e4 | 454 | runtime->status->hw_ptr = new_hw_ptr; |
3509a03f | 455 | runtime->hw_ptr_jiffies = curr_jiffies; |
0e8014d7 PLB |
456 | if (crossed_boundary) { |
457 | snd_BUG_ON(crossed_boundary != 1); | |
458 | runtime->hw_ptr_wrap += runtime->boundary; | |
459 | } | |
4eeaaeae | 460 | |
3179f620 | 461 | update_audio_tstamp(substream, &curr_tstamp, &audio_tstamp); |
4eeaaeae | 462 | |
1250932e | 463 | return snd_pcm_update_state(substream, runtime); |
1da177e4 LT |
464 | } |
465 | ||
466 | /* CAUTION: call it with irq disabled */ | |
877211f5 | 467 | int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream) |
1da177e4 | 468 | { |
f240406b | 469 | return snd_pcm_update_hw_ptr0(substream, 0); |
1da177e4 LT |
470 | } |
471 | ||
472 | /** | |
473 | * snd_pcm_set_ops - set the PCM operators | |
474 | * @pcm: the pcm instance | |
475 | * @direction: stream direction, SNDRV_PCM_STREAM_XXX | |
476 | * @ops: the operator table | |
477 | * | |
478 | * Sets the given PCM operators to the pcm instance. | |
479 | */ | |
e6c2e7eb LPC |
480 | void snd_pcm_set_ops(struct snd_pcm *pcm, int direction, |
481 | const struct snd_pcm_ops *ops) | |
1da177e4 | 482 | { |
877211f5 TI |
483 | struct snd_pcm_str *stream = &pcm->streams[direction]; |
484 | struct snd_pcm_substream *substream; | |
1da177e4 LT |
485 | |
486 | for (substream = stream->substream; substream != NULL; substream = substream->next) | |
487 | substream->ops = ops; | |
488 | } | |
e88e8ae6 | 489 | EXPORT_SYMBOL(snd_pcm_set_ops); |
1da177e4 LT |
490 | |
491 | /** | |
492 | * snd_pcm_sync - set the PCM sync id | |
493 | * @substream: the pcm substream | |
494 | * | |
495 | * Sets the PCM sync identifier for the card. | |
496 | */ | |
877211f5 | 497 | void snd_pcm_set_sync(struct snd_pcm_substream *substream) |
1da177e4 | 498 | { |
877211f5 | 499 | struct snd_pcm_runtime *runtime = substream->runtime; |
1da177e4 LT |
500 | |
501 | runtime->sync.id32[0] = substream->pcm->card->number; | |
502 | runtime->sync.id32[1] = -1; | |
503 | runtime->sync.id32[2] = -1; | |
504 | runtime->sync.id32[3] = -1; | |
505 | } | |
e88e8ae6 TI |
506 | EXPORT_SYMBOL(snd_pcm_set_sync); |
507 | ||
1da177e4 LT |
508 | /* |
509 | * Standard ioctl routine | |
510 | */ | |
511 | ||
1da177e4 LT |
512 | static inline unsigned int div32(unsigned int a, unsigned int b, |
513 | unsigned int *r) | |
514 | { | |
515 | if (b == 0) { | |
516 | *r = 0; | |
517 | return UINT_MAX; | |
518 | } | |
519 | *r = a % b; | |
520 | return a / b; | |
521 | } | |
522 | ||
523 | static inline unsigned int div_down(unsigned int a, unsigned int b) | |
524 | { | |
525 | if (b == 0) | |
526 | return UINT_MAX; | |
527 | return a / b; | |
528 | } | |
529 | ||
530 | static inline unsigned int div_up(unsigned int a, unsigned int b) | |
531 | { | |
532 | unsigned int r; | |
533 | unsigned int q; | |
534 | if (b == 0) | |
535 | return UINT_MAX; | |
536 | q = div32(a, b, &r); | |
537 | if (r) | |
538 | ++q; | |
539 | return q; | |
540 | } | |
541 | ||
542 | static inline unsigned int mul(unsigned int a, unsigned int b) | |
543 | { | |
544 | if (a == 0) | |
545 | return 0; | |
546 | if (div_down(UINT_MAX, a) < b) | |
547 | return UINT_MAX; | |
548 | return a * b; | |
549 | } | |
550 | ||
551 | static inline unsigned int muldiv32(unsigned int a, unsigned int b, | |
552 | unsigned int c, unsigned int *r) | |
553 | { | |
554 | u_int64_t n = (u_int64_t) a * b; | |
555 | if (c == 0) { | |
1da177e4 LT |
556 | *r = 0; |
557 | return UINT_MAX; | |
558 | } | |
3f7440a6 | 559 | n = div_u64_rem(n, c, r); |
1da177e4 LT |
560 | if (n >= UINT_MAX) { |
561 | *r = 0; | |
562 | return UINT_MAX; | |
563 | } | |
564 | return n; | |
565 | } | |
566 | ||
1da177e4 LT |
567 | /** |
568 | * snd_interval_refine - refine the interval value of configurator | |
569 | * @i: the interval value to refine | |
570 | * @v: the interval value to refer to | |
571 | * | |
572 | * Refines the interval value with the reference value. | |
573 | * The interval is changed to the range satisfying both intervals. | |
574 | * The interval status (min, max, integer, etc.) are evaluated. | |
575 | * | |
eb7c06e8 YB |
576 | * Return: Positive if the value is changed, zero if it's not changed, or a |
577 | * negative error code. | |
1da177e4 | 578 | */ |
877211f5 | 579 | int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v) |
1da177e4 LT |
580 | { |
581 | int changed = 0; | |
7eaa943c TI |
582 | if (snd_BUG_ON(snd_interval_empty(i))) |
583 | return -EINVAL; | |
1da177e4 LT |
584 | if (i->min < v->min) { |
585 | i->min = v->min; | |
586 | i->openmin = v->openmin; | |
587 | changed = 1; | |
588 | } else if (i->min == v->min && !i->openmin && v->openmin) { | |
589 | i->openmin = 1; | |
590 | changed = 1; | |
591 | } | |
592 | if (i->max > v->max) { | |
593 | i->max = v->max; | |
594 | i->openmax = v->openmax; | |
595 | changed = 1; | |
596 | } else if (i->max == v->max && !i->openmax && v->openmax) { | |
597 | i->openmax = 1; | |
598 | changed = 1; | |
599 | } | |
600 | if (!i->integer && v->integer) { | |
601 | i->integer = 1; | |
602 | changed = 1; | |
603 | } | |
604 | if (i->integer) { | |
605 | if (i->openmin) { | |
606 | i->min++; | |
607 | i->openmin = 0; | |
608 | } | |
609 | if (i->openmax) { | |
610 | i->max--; | |
611 | i->openmax = 0; | |
612 | } | |
613 | } else if (!i->openmin && !i->openmax && i->min == i->max) | |
614 | i->integer = 1; | |
615 | if (snd_interval_checkempty(i)) { | |
616 | snd_interval_none(i); | |
617 | return -EINVAL; | |
618 | } | |
619 | return changed; | |
620 | } | |
e88e8ae6 TI |
621 | EXPORT_SYMBOL(snd_interval_refine); |
622 | ||
877211f5 | 623 | static int snd_interval_refine_first(struct snd_interval *i) |
1da177e4 | 624 | { |
ff2d6acd TW |
625 | const unsigned int last_max = i->max; |
626 | ||
7eaa943c TI |
627 | if (snd_BUG_ON(snd_interval_empty(i))) |
628 | return -EINVAL; | |
1da177e4 LT |
629 | if (snd_interval_single(i)) |
630 | return 0; | |
631 | i->max = i->min; | |
ff2d6acd | 632 | if (i->openmin) |
1da177e4 | 633 | i->max++; |
ff2d6acd TW |
634 | /* only exclude max value if also excluded before refine */ |
635 | i->openmax = (i->openmax && i->max >= last_max); | |
1da177e4 LT |
636 | return 1; |
637 | } | |
638 | ||
877211f5 | 639 | static int snd_interval_refine_last(struct snd_interval *i) |
1da177e4 | 640 | { |
ff2d6acd TW |
641 | const unsigned int last_min = i->min; |
642 | ||
7eaa943c TI |
643 | if (snd_BUG_ON(snd_interval_empty(i))) |
644 | return -EINVAL; | |
1da177e4 LT |
645 | if (snd_interval_single(i)) |
646 | return 0; | |
647 | i->min = i->max; | |
ff2d6acd | 648 | if (i->openmax) |
1da177e4 | 649 | i->min--; |
ff2d6acd TW |
650 | /* only exclude min value if also excluded before refine */ |
651 | i->openmin = (i->openmin && i->min <= last_min); | |
1da177e4 LT |
652 | return 1; |
653 | } | |
654 | ||
877211f5 | 655 | void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c) |
1da177e4 LT |
656 | { |
657 | if (a->empty || b->empty) { | |
658 | snd_interval_none(c); | |
659 | return; | |
660 | } | |
661 | c->empty = 0; | |
662 | c->min = mul(a->min, b->min); | |
663 | c->openmin = (a->openmin || b->openmin); | |
664 | c->max = mul(a->max, b->max); | |
665 | c->openmax = (a->openmax || b->openmax); | |
666 | c->integer = (a->integer && b->integer); | |
667 | } | |
668 | ||
669 | /** | |
670 | * snd_interval_div - refine the interval value with division | |
df8db936 TI |
671 | * @a: dividend |
672 | * @b: divisor | |
673 | * @c: quotient | |
1da177e4 LT |
674 | * |
675 | * c = a / b | |
676 | * | |
677 | * Returns non-zero if the value is changed, zero if not changed. | |
678 | */ | |
877211f5 | 679 | void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c) |
1da177e4 LT |
680 | { |
681 | unsigned int r; | |
682 | if (a->empty || b->empty) { | |
683 | snd_interval_none(c); | |
684 | return; | |
685 | } | |
686 | c->empty = 0; | |
687 | c->min = div32(a->min, b->max, &r); | |
688 | c->openmin = (r || a->openmin || b->openmax); | |
689 | if (b->min > 0) { | |
690 | c->max = div32(a->max, b->min, &r); | |
691 | if (r) { | |
692 | c->max++; | |
693 | c->openmax = 1; | |
694 | } else | |
695 | c->openmax = (a->openmax || b->openmin); | |
696 | } else { | |
697 | c->max = UINT_MAX; | |
698 | c->openmax = 0; | |
699 | } | |
700 | c->integer = 0; | |
701 | } | |
702 | ||
703 | /** | |
704 | * snd_interval_muldivk - refine the interval value | |
df8db936 TI |
705 | * @a: dividend 1 |
706 | * @b: dividend 2 | |
707 | * @k: divisor (as integer) | |
708 | * @c: result | |
709 | * | |
1da177e4 LT |
710 | * c = a * b / k |
711 | * | |
712 | * Returns non-zero if the value is changed, zero if not changed. | |
713 | */ | |
877211f5 TI |
714 | void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b, |
715 | unsigned int k, struct snd_interval *c) | |
1da177e4 LT |
716 | { |
717 | unsigned int r; | |
718 | if (a->empty || b->empty) { | |
719 | snd_interval_none(c); | |
720 | return; | |
721 | } | |
722 | c->empty = 0; | |
723 | c->min = muldiv32(a->min, b->min, k, &r); | |
724 | c->openmin = (r || a->openmin || b->openmin); | |
725 | c->max = muldiv32(a->max, b->max, k, &r); | |
726 | if (r) { | |
727 | c->max++; | |
728 | c->openmax = 1; | |
729 | } else | |
730 | c->openmax = (a->openmax || b->openmax); | |
731 | c->integer = 0; | |
732 | } | |
733 | ||
734 | /** | |
735 | * snd_interval_mulkdiv - refine the interval value | |
df8db936 TI |
736 | * @a: dividend 1 |
737 | * @k: dividend 2 (as integer) | |
738 | * @b: divisor | |
739 | * @c: result | |
1da177e4 LT |
740 | * |
741 | * c = a * k / b | |
742 | * | |
743 | * Returns non-zero if the value is changed, zero if not changed. | |
744 | */ | |
877211f5 TI |
745 | void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k, |
746 | const struct snd_interval *b, struct snd_interval *c) | |
1da177e4 LT |
747 | { |
748 | unsigned int r; | |
749 | if (a->empty || b->empty) { | |
750 | snd_interval_none(c); | |
751 | return; | |
752 | } | |
753 | c->empty = 0; | |
754 | c->min = muldiv32(a->min, k, b->max, &r); | |
755 | c->openmin = (r || a->openmin || b->openmax); | |
756 | if (b->min > 0) { | |
757 | c->max = muldiv32(a->max, k, b->min, &r); | |
758 | if (r) { | |
759 | c->max++; | |
760 | c->openmax = 1; | |
761 | } else | |
762 | c->openmax = (a->openmax || b->openmin); | |
763 | } else { | |
764 | c->max = UINT_MAX; | |
765 | c->openmax = 0; | |
766 | } | |
767 | c->integer = 0; | |
768 | } | |
769 | ||
1da177e4 LT |
770 | /* ---- */ |
771 | ||
772 | ||
773 | /** | |
774 | * snd_interval_ratnum - refine the interval value | |
df8db936 TI |
775 | * @i: interval to refine |
776 | * @rats_count: number of ratnum_t | |
777 | * @rats: ratnum_t array | |
778 | * @nump: pointer to store the resultant numerator | |
779 | * @denp: pointer to store the resultant denominator | |
1da177e4 | 780 | * |
eb7c06e8 YB |
781 | * Return: Positive if the value is changed, zero if it's not changed, or a |
782 | * negative error code. | |
1da177e4 | 783 | */ |
877211f5 | 784 | int snd_interval_ratnum(struct snd_interval *i, |
e5e113cf | 785 | unsigned int rats_count, const struct snd_ratnum *rats, |
877211f5 | 786 | unsigned int *nump, unsigned int *denp) |
1da177e4 | 787 | { |
8374e24c KH |
788 | unsigned int best_num, best_den; |
789 | int best_diff; | |
1da177e4 | 790 | unsigned int k; |
877211f5 | 791 | struct snd_interval t; |
1da177e4 | 792 | int err; |
8374e24c KH |
793 | unsigned int result_num, result_den; |
794 | int result_diff; | |
1da177e4 LT |
795 | |
796 | best_num = best_den = best_diff = 0; | |
797 | for (k = 0; k < rats_count; ++k) { | |
798 | unsigned int num = rats[k].num; | |
799 | unsigned int den; | |
800 | unsigned int q = i->min; | |
801 | int diff; | |
802 | if (q == 0) | |
803 | q = 1; | |
40962d7c | 804 | den = div_up(num, q); |
1da177e4 LT |
805 | if (den < rats[k].den_min) |
806 | continue; | |
807 | if (den > rats[k].den_max) | |
808 | den = rats[k].den_max; | |
809 | else { | |
810 | unsigned int r; | |
811 | r = (den - rats[k].den_min) % rats[k].den_step; | |
812 | if (r != 0) | |
813 | den -= r; | |
814 | } | |
815 | diff = num - q * den; | |
8374e24c KH |
816 | if (diff < 0) |
817 | diff = -diff; | |
1da177e4 LT |
818 | if (best_num == 0 || |
819 | diff * best_den < best_diff * den) { | |
820 | best_diff = diff; | |
821 | best_den = den; | |
822 | best_num = num; | |
823 | } | |
824 | } | |
825 | if (best_den == 0) { | |
826 | i->empty = 1; | |
827 | return -EINVAL; | |
828 | } | |
829 | t.min = div_down(best_num, best_den); | |
830 | t.openmin = !!(best_num % best_den); | |
831 | ||
8374e24c KH |
832 | result_num = best_num; |
833 | result_diff = best_diff; | |
834 | result_den = best_den; | |
1da177e4 LT |
835 | best_num = best_den = best_diff = 0; |
836 | for (k = 0; k < rats_count; ++k) { | |
837 | unsigned int num = rats[k].num; | |
838 | unsigned int den; | |
839 | unsigned int q = i->max; | |
840 | int diff; | |
841 | if (q == 0) { | |
842 | i->empty = 1; | |
843 | return -EINVAL; | |
844 | } | |
40962d7c | 845 | den = div_down(num, q); |
1da177e4 LT |
846 | if (den > rats[k].den_max) |
847 | continue; | |
848 | if (den < rats[k].den_min) | |
849 | den = rats[k].den_min; | |
850 | else { | |
851 | unsigned int r; | |
852 | r = (den - rats[k].den_min) % rats[k].den_step; | |
853 | if (r != 0) | |
854 | den += rats[k].den_step - r; | |
855 | } | |
856 | diff = q * den - num; | |
8374e24c KH |
857 | if (diff < 0) |
858 | diff = -diff; | |
1da177e4 LT |
859 | if (best_num == 0 || |
860 | diff * best_den < best_diff * den) { | |
861 | best_diff = diff; | |
862 | best_den = den; | |
863 | best_num = num; | |
864 | } | |
865 | } | |
866 | if (best_den == 0) { | |
867 | i->empty = 1; | |
868 | return -EINVAL; | |
869 | } | |
870 | t.max = div_up(best_num, best_den); | |
871 | t.openmax = !!(best_num % best_den); | |
872 | t.integer = 0; | |
873 | err = snd_interval_refine(i, &t); | |
874 | if (err < 0) | |
875 | return err; | |
876 | ||
877 | if (snd_interval_single(i)) { | |
8374e24c KH |
878 | if (best_diff * result_den < result_diff * best_den) { |
879 | result_num = best_num; | |
880 | result_den = best_den; | |
881 | } | |
1da177e4 | 882 | if (nump) |
8374e24c | 883 | *nump = result_num; |
1da177e4 | 884 | if (denp) |
8374e24c | 885 | *denp = result_den; |
1da177e4 LT |
886 | } |
887 | return err; | |
888 | } | |
e88e8ae6 TI |
889 | EXPORT_SYMBOL(snd_interval_ratnum); |
890 | ||
1da177e4 LT |
891 | /** |
892 | * snd_interval_ratden - refine the interval value | |
df8db936 | 893 | * @i: interval to refine |
877211f5 TI |
894 | * @rats_count: number of struct ratden |
895 | * @rats: struct ratden array | |
df8db936 TI |
896 | * @nump: pointer to store the resultant numerator |
897 | * @denp: pointer to store the resultant denominator | |
1da177e4 | 898 | * |
eb7c06e8 YB |
899 | * Return: Positive if the value is changed, zero if it's not changed, or a |
900 | * negative error code. | |
1da177e4 | 901 | */ |
877211f5 | 902 | static int snd_interval_ratden(struct snd_interval *i, |
e5e113cf LPC |
903 | unsigned int rats_count, |
904 | const struct snd_ratden *rats, | |
1da177e4 LT |
905 | unsigned int *nump, unsigned int *denp) |
906 | { | |
907 | unsigned int best_num, best_diff, best_den; | |
908 | unsigned int k; | |
877211f5 | 909 | struct snd_interval t; |
1da177e4 LT |
910 | int err; |
911 | ||
912 | best_num = best_den = best_diff = 0; | |
913 | for (k = 0; k < rats_count; ++k) { | |
914 | unsigned int num; | |
915 | unsigned int den = rats[k].den; | |
916 | unsigned int q = i->min; | |
917 | int diff; | |
918 | num = mul(q, den); | |
919 | if (num > rats[k].num_max) | |
920 | continue; | |
921 | if (num < rats[k].num_min) | |
922 | num = rats[k].num_max; | |
923 | else { | |
924 | unsigned int r; | |
925 | r = (num - rats[k].num_min) % rats[k].num_step; | |
926 | if (r != 0) | |
927 | num += rats[k].num_step - r; | |
928 | } | |
929 | diff = num - q * den; | |
930 | if (best_num == 0 || | |
931 | diff * best_den < best_diff * den) { | |
932 | best_diff = diff; | |
933 | best_den = den; | |
934 | best_num = num; | |
935 | } | |
936 | } | |
937 | if (best_den == 0) { | |
938 | i->empty = 1; | |
939 | return -EINVAL; | |
940 | } | |
941 | t.min = div_down(best_num, best_den); | |
942 | t.openmin = !!(best_num % best_den); | |
943 | ||
944 | best_num = best_den = best_diff = 0; | |
945 | for (k = 0; k < rats_count; ++k) { | |
946 | unsigned int num; | |
947 | unsigned int den = rats[k].den; | |
948 | unsigned int q = i->max; | |
949 | int diff; | |
950 | num = mul(q, den); | |
951 | if (num < rats[k].num_min) | |
952 | continue; | |
953 | if (num > rats[k].num_max) | |
954 | num = rats[k].num_max; | |
955 | else { | |
956 | unsigned int r; | |
957 | r = (num - rats[k].num_min) % rats[k].num_step; | |
958 | if (r != 0) | |
959 | num -= r; | |
960 | } | |
961 | diff = q * den - num; | |
962 | if (best_num == 0 || | |
963 | diff * best_den < best_diff * den) { | |
964 | best_diff = diff; | |
965 | best_den = den; | |
966 | best_num = num; | |
967 | } | |
968 | } | |
969 | if (best_den == 0) { | |
970 | i->empty = 1; | |
971 | return -EINVAL; | |
972 | } | |
973 | t.max = div_up(best_num, best_den); | |
974 | t.openmax = !!(best_num % best_den); | |
975 | t.integer = 0; | |
976 | err = snd_interval_refine(i, &t); | |
977 | if (err < 0) | |
978 | return err; | |
979 | ||
980 | if (snd_interval_single(i)) { | |
981 | if (nump) | |
982 | *nump = best_num; | |
983 | if (denp) | |
984 | *denp = best_den; | |
985 | } | |
986 | return err; | |
987 | } | |
988 | ||
989 | /** | |
990 | * snd_interval_list - refine the interval value from the list | |
991 | * @i: the interval value to refine | |
992 | * @count: the number of elements in the list | |
993 | * @list: the value list | |
994 | * @mask: the bit-mask to evaluate | |
995 | * | |
996 | * Refines the interval value from the list. | |
997 | * When mask is non-zero, only the elements corresponding to bit 1 are | |
998 | * evaluated. | |
999 | * | |
eb7c06e8 YB |
1000 | * Return: Positive if the value is changed, zero if it's not changed, or a |
1001 | * negative error code. | |
1da177e4 | 1002 | */ |
4af87a93 MB |
1003 | int snd_interval_list(struct snd_interval *i, unsigned int count, |
1004 | const unsigned int *list, unsigned int mask) | |
1da177e4 LT |
1005 | { |
1006 | unsigned int k; | |
b1ddaf68 | 1007 | struct snd_interval list_range; |
0981a260 TI |
1008 | |
1009 | if (!count) { | |
1010 | i->empty = 1; | |
1011 | return -EINVAL; | |
1012 | } | |
b1ddaf68 CL |
1013 | snd_interval_any(&list_range); |
1014 | list_range.min = UINT_MAX; | |
1015 | list_range.max = 0; | |
1da177e4 LT |
1016 | for (k = 0; k < count; k++) { |
1017 | if (mask && !(mask & (1 << k))) | |
1018 | continue; | |
b1ddaf68 | 1019 | if (!snd_interval_test(i, list[k])) |
1da177e4 | 1020 | continue; |
b1ddaf68 CL |
1021 | list_range.min = min(list_range.min, list[k]); |
1022 | list_range.max = max(list_range.max, list[k]); | |
1da177e4 | 1023 | } |
b1ddaf68 | 1024 | return snd_interval_refine(i, &list_range); |
1da177e4 | 1025 | } |
e88e8ae6 TI |
1026 | EXPORT_SYMBOL(snd_interval_list); |
1027 | ||
f66f898e PR |
1028 | /** |
1029 | * snd_interval_ranges - refine the interval value from the list of ranges | |
1030 | * @i: the interval value to refine | |
1031 | * @count: the number of elements in the list of ranges | |
1032 | * @ranges: the ranges list | |
1033 | * @mask: the bit-mask to evaluate | |
1034 | * | |
1035 | * Refines the interval value from the list of ranges. | |
1036 | * When mask is non-zero, only the elements corresponding to bit 1 are | |
1037 | * evaluated. | |
1038 | * | |
1039 | * Return: Positive if the value is changed, zero if it's not changed, or a | |
1040 | * negative error code. | |
1041 | */ | |
1042 | int snd_interval_ranges(struct snd_interval *i, unsigned int count, | |
1043 | const struct snd_interval *ranges, unsigned int mask) | |
1044 | { | |
1045 | unsigned int k; | |
1046 | struct snd_interval range_union; | |
1047 | struct snd_interval range; | |
1048 | ||
1049 | if (!count) { | |
1050 | snd_interval_none(i); | |
1051 | return -EINVAL; | |
1052 | } | |
1053 | snd_interval_any(&range_union); | |
1054 | range_union.min = UINT_MAX; | |
1055 | range_union.max = 0; | |
1056 | for (k = 0; k < count; k++) { | |
1057 | if (mask && !(mask & (1 << k))) | |
1058 | continue; | |
1059 | snd_interval_copy(&range, &ranges[k]); | |
1060 | if (snd_interval_refine(&range, i) < 0) | |
1061 | continue; | |
1062 | if (snd_interval_empty(&range)) | |
1063 | continue; | |
1064 | ||
1065 | if (range.min < range_union.min) { | |
1066 | range_union.min = range.min; | |
1067 | range_union.openmin = 1; | |
1068 | } | |
1069 | if (range.min == range_union.min && !range.openmin) | |
1070 | range_union.openmin = 0; | |
1071 | if (range.max > range_union.max) { | |
1072 | range_union.max = range.max; | |
1073 | range_union.openmax = 1; | |
1074 | } | |
1075 | if (range.max == range_union.max && !range.openmax) | |
1076 | range_union.openmax = 0; | |
1077 | } | |
1078 | return snd_interval_refine(i, &range_union); | |
1079 | } | |
1080 | EXPORT_SYMBOL(snd_interval_ranges); | |
1081 | ||
0f519b62 | 1082 | static int snd_interval_step(struct snd_interval *i, unsigned int step) |
1da177e4 LT |
1083 | { |
1084 | unsigned int n; | |
1085 | int changed = 0; | |
0f519b62 | 1086 | n = i->min % step; |
1da177e4 LT |
1087 | if (n != 0 || i->openmin) { |
1088 | i->min += step - n; | |
df1e4719 | 1089 | i->openmin = 0; |
1da177e4 LT |
1090 | changed = 1; |
1091 | } | |
0f519b62 | 1092 | n = i->max % step; |
1da177e4 LT |
1093 | if (n != 0 || i->openmax) { |
1094 | i->max -= n; | |
df1e4719 | 1095 | i->openmax = 0; |
1da177e4 LT |
1096 | changed = 1; |
1097 | } | |
1098 | if (snd_interval_checkempty(i)) { | |
1099 | i->empty = 1; | |
1100 | return -EINVAL; | |
1101 | } | |
1102 | return changed; | |
1103 | } | |
1104 | ||
1105 | /* Info constraints helpers */ | |
1106 | ||
1107 | /** | |
1108 | * snd_pcm_hw_rule_add - add the hw-constraint rule | |
1109 | * @runtime: the pcm runtime instance | |
1110 | * @cond: condition bits | |
1111 | * @var: the variable to evaluate | |
1112 | * @func: the evaluation function | |
1113 | * @private: the private data pointer passed to function | |
1114 | * @dep: the dependent variables | |
1115 | * | |
eb7c06e8 | 1116 | * Return: Zero if successful, or a negative error code on failure. |
1da177e4 | 1117 | */ |
877211f5 | 1118 | int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond, |
1da177e4 LT |
1119 | int var, |
1120 | snd_pcm_hw_rule_func_t func, void *private, | |
1121 | int dep, ...) | |
1122 | { | |
877211f5 TI |
1123 | struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints; |
1124 | struct snd_pcm_hw_rule *c; | |
1da177e4 LT |
1125 | unsigned int k; |
1126 | va_list args; | |
1127 | va_start(args, dep); | |
1128 | if (constrs->rules_num >= constrs->rules_all) { | |
877211f5 | 1129 | struct snd_pcm_hw_rule *new; |
1da177e4 | 1130 | unsigned int new_rules = constrs->rules_all + 16; |
09b9ddfa TI |
1131 | new = krealloc(constrs->rules, new_rules * sizeof(*c), |
1132 | GFP_KERNEL); | |
87a1c8aa JJ |
1133 | if (!new) { |
1134 | va_end(args); | |
1da177e4 | 1135 | return -ENOMEM; |
87a1c8aa | 1136 | } |
1da177e4 LT |
1137 | constrs->rules = new; |
1138 | constrs->rules_all = new_rules; | |
1139 | } | |
1140 | c = &constrs->rules[constrs->rules_num]; | |
1141 | c->cond = cond; | |
1142 | c->func = func; | |
1143 | c->var = var; | |
1144 | c->private = private; | |
1145 | k = 0; | |
1146 | while (1) { | |
87a1c8aa JJ |
1147 | if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps))) { |
1148 | va_end(args); | |
7eaa943c | 1149 | return -EINVAL; |
87a1c8aa | 1150 | } |
1da177e4 LT |
1151 | c->deps[k++] = dep; |
1152 | if (dep < 0) | |
1153 | break; | |
1154 | dep = va_arg(args, int); | |
1155 | } | |
1156 | constrs->rules_num++; | |
1157 | va_end(args); | |
1158 | return 0; | |
87a1c8aa | 1159 | } |
e88e8ae6 TI |
1160 | EXPORT_SYMBOL(snd_pcm_hw_rule_add); |
1161 | ||
1da177e4 | 1162 | /** |
1c85cc64 | 1163 | * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint |
df8db936 TI |
1164 | * @runtime: PCM runtime instance |
1165 | * @var: hw_params variable to apply the mask | |
1166 | * @mask: the bitmap mask | |
1167 | * | |
1c85cc64 | 1168 | * Apply the constraint of the given bitmap mask to a 32-bit mask parameter. |
eb7c06e8 YB |
1169 | * |
1170 | * Return: Zero if successful, or a negative error code on failure. | |
1da177e4 | 1171 | */ |
877211f5 | 1172 | int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var, |
1da177e4 LT |
1173 | u_int32_t mask) |
1174 | { | |
877211f5 TI |
1175 | struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints; |
1176 | struct snd_mask *maskp = constrs_mask(constrs, var); | |
1da177e4 LT |
1177 | *maskp->bits &= mask; |
1178 | memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */ | |
1179 | if (*maskp->bits == 0) | |
1180 | return -EINVAL; | |
1181 | return 0; | |
1182 | } | |
1183 | ||
1184 | /** | |
1c85cc64 | 1185 | * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint |
df8db936 TI |
1186 | * @runtime: PCM runtime instance |
1187 | * @var: hw_params variable to apply the mask | |
1188 | * @mask: the 64bit bitmap mask | |
1189 | * | |
1c85cc64 | 1190 | * Apply the constraint of the given bitmap mask to a 64-bit mask parameter. |
eb7c06e8 YB |
1191 | * |
1192 | * Return: Zero if successful, or a negative error code on failure. | |
1da177e4 | 1193 | */ |
877211f5 | 1194 | int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var, |
1da177e4 LT |
1195 | u_int64_t mask) |
1196 | { | |
877211f5 TI |
1197 | struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints; |
1198 | struct snd_mask *maskp = constrs_mask(constrs, var); | |
1da177e4 LT |
1199 | maskp->bits[0] &= (u_int32_t)mask; |
1200 | maskp->bits[1] &= (u_int32_t)(mask >> 32); | |
1201 | memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */ | |
1202 | if (! maskp->bits[0] && ! maskp->bits[1]) | |
1203 | return -EINVAL; | |
1204 | return 0; | |
1205 | } | |
63a5d4c6 | 1206 | EXPORT_SYMBOL(snd_pcm_hw_constraint_mask64); |
1da177e4 LT |
1207 | |
1208 | /** | |
1c85cc64 | 1209 | * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval |
df8db936 TI |
1210 | * @runtime: PCM runtime instance |
1211 | * @var: hw_params variable to apply the integer constraint | |
1212 | * | |
1213 | * Apply the constraint of integer to an interval parameter. | |
eb7c06e8 YB |
1214 | * |
1215 | * Return: Positive if the value is changed, zero if it's not changed, or a | |
1216 | * negative error code. | |
1da177e4 | 1217 | */ |
877211f5 | 1218 | int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var) |
1da177e4 | 1219 | { |
877211f5 | 1220 | struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints; |
1da177e4 LT |
1221 | return snd_interval_setinteger(constrs_interval(constrs, var)); |
1222 | } | |
e88e8ae6 TI |
1223 | EXPORT_SYMBOL(snd_pcm_hw_constraint_integer); |
1224 | ||
1da177e4 | 1225 | /** |
1c85cc64 | 1226 | * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval |
df8db936 TI |
1227 | * @runtime: PCM runtime instance |
1228 | * @var: hw_params variable to apply the range | |
1229 | * @min: the minimal value | |
1230 | * @max: the maximal value | |
1231 | * | |
1232 | * Apply the min/max range constraint to an interval parameter. | |
eb7c06e8 YB |
1233 | * |
1234 | * Return: Positive if the value is changed, zero if it's not changed, or a | |
1235 | * negative error code. | |
1da177e4 | 1236 | */ |
877211f5 | 1237 | int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var, |
1da177e4 LT |
1238 | unsigned int min, unsigned int max) |
1239 | { | |
877211f5 TI |
1240 | struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints; |
1241 | struct snd_interval t; | |
1da177e4 LT |
1242 | t.min = min; |
1243 | t.max = max; | |
1244 | t.openmin = t.openmax = 0; | |
1245 | t.integer = 0; | |
1246 | return snd_interval_refine(constrs_interval(constrs, var), &t); | |
1247 | } | |
e88e8ae6 TI |
1248 | EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax); |
1249 | ||
877211f5 TI |
1250 | static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params, |
1251 | struct snd_pcm_hw_rule *rule) | |
1da177e4 | 1252 | { |
877211f5 | 1253 | struct snd_pcm_hw_constraint_list *list = rule->private; |
1da177e4 LT |
1254 | return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask); |
1255 | } | |
1256 | ||
1257 | ||
1258 | /** | |
1c85cc64 | 1259 | * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter |
df8db936 TI |
1260 | * @runtime: PCM runtime instance |
1261 | * @cond: condition bits | |
1262 | * @var: hw_params variable to apply the list constraint | |
1263 | * @l: list | |
1264 | * | |
1265 | * Apply the list of constraints to an interval parameter. | |
eb7c06e8 YB |
1266 | * |
1267 | * Return: Zero if successful, or a negative error code on failure. | |
1da177e4 | 1268 | */ |
877211f5 | 1269 | int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime, |
1da177e4 LT |
1270 | unsigned int cond, |
1271 | snd_pcm_hw_param_t var, | |
1464189f | 1272 | const struct snd_pcm_hw_constraint_list *l) |
1da177e4 LT |
1273 | { |
1274 | return snd_pcm_hw_rule_add(runtime, cond, var, | |
1464189f | 1275 | snd_pcm_hw_rule_list, (void *)l, |
1da177e4 LT |
1276 | var, -1); |
1277 | } | |
e88e8ae6 TI |
1278 | EXPORT_SYMBOL(snd_pcm_hw_constraint_list); |
1279 | ||
f66f898e PR |
1280 | static int snd_pcm_hw_rule_ranges(struct snd_pcm_hw_params *params, |
1281 | struct snd_pcm_hw_rule *rule) | |
1282 | { | |
1283 | struct snd_pcm_hw_constraint_ranges *r = rule->private; | |
1284 | return snd_interval_ranges(hw_param_interval(params, rule->var), | |
1285 | r->count, r->ranges, r->mask); | |
1286 | } | |
1287 | ||
1288 | ||
1289 | /** | |
1290 | * snd_pcm_hw_constraint_ranges - apply list of range constraints to a parameter | |
1291 | * @runtime: PCM runtime instance | |
1292 | * @cond: condition bits | |
1293 | * @var: hw_params variable to apply the list of range constraints | |
1294 | * @r: ranges | |
1295 | * | |
1296 | * Apply the list of range constraints to an interval parameter. | |
1297 | * | |
1298 | * Return: Zero if successful, or a negative error code on failure. | |
1299 | */ | |
1300 | int snd_pcm_hw_constraint_ranges(struct snd_pcm_runtime *runtime, | |
1301 | unsigned int cond, | |
1302 | snd_pcm_hw_param_t var, | |
1303 | const struct snd_pcm_hw_constraint_ranges *r) | |
1304 | { | |
1305 | return snd_pcm_hw_rule_add(runtime, cond, var, | |
1306 | snd_pcm_hw_rule_ranges, (void *)r, | |
1307 | var, -1); | |
1308 | } | |
1309 | EXPORT_SYMBOL(snd_pcm_hw_constraint_ranges); | |
1310 | ||
877211f5 TI |
1311 | static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params, |
1312 | struct snd_pcm_hw_rule *rule) | |
1da177e4 | 1313 | { |
e5e113cf | 1314 | const struct snd_pcm_hw_constraint_ratnums *r = rule->private; |
1da177e4 LT |
1315 | unsigned int num = 0, den = 0; |
1316 | int err; | |
1317 | err = snd_interval_ratnum(hw_param_interval(params, rule->var), | |
1318 | r->nrats, r->rats, &num, &den); | |
1319 | if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) { | |
1320 | params->rate_num = num; | |
1321 | params->rate_den = den; | |
1322 | } | |
1323 | return err; | |
1324 | } | |
1325 | ||
1326 | /** | |
1c85cc64 | 1327 | * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter |
df8db936 TI |
1328 | * @runtime: PCM runtime instance |
1329 | * @cond: condition bits | |
1330 | * @var: hw_params variable to apply the ratnums constraint | |
877211f5 | 1331 | * @r: struct snd_ratnums constriants |
eb7c06e8 YB |
1332 | * |
1333 | * Return: Zero if successful, or a negative error code on failure. | |
1da177e4 | 1334 | */ |
877211f5 | 1335 | int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime, |
1da177e4 LT |
1336 | unsigned int cond, |
1337 | snd_pcm_hw_param_t var, | |
e5e113cf | 1338 | const struct snd_pcm_hw_constraint_ratnums *r) |
1da177e4 LT |
1339 | { |
1340 | return snd_pcm_hw_rule_add(runtime, cond, var, | |
e5e113cf | 1341 | snd_pcm_hw_rule_ratnums, (void *)r, |
1da177e4 LT |
1342 | var, -1); |
1343 | } | |
e88e8ae6 TI |
1344 | EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums); |
1345 | ||
877211f5 TI |
1346 | static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params, |
1347 | struct snd_pcm_hw_rule *rule) | |
1da177e4 | 1348 | { |
e5e113cf | 1349 | const struct snd_pcm_hw_constraint_ratdens *r = rule->private; |
1da177e4 LT |
1350 | unsigned int num = 0, den = 0; |
1351 | int err = snd_interval_ratden(hw_param_interval(params, rule->var), | |
1352 | r->nrats, r->rats, &num, &den); | |
1353 | if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) { | |
1354 | params->rate_num = num; | |
1355 | params->rate_den = den; | |
1356 | } | |
1357 | return err; | |
1358 | } | |
1359 | ||
1360 | /** | |
1c85cc64 | 1361 | * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter |
df8db936 TI |
1362 | * @runtime: PCM runtime instance |
1363 | * @cond: condition bits | |
1364 | * @var: hw_params variable to apply the ratdens constraint | |
877211f5 | 1365 | * @r: struct snd_ratdens constriants |
eb7c06e8 YB |
1366 | * |
1367 | * Return: Zero if successful, or a negative error code on failure. | |
1da177e4 | 1368 | */ |
877211f5 | 1369 | int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime, |
1da177e4 LT |
1370 | unsigned int cond, |
1371 | snd_pcm_hw_param_t var, | |
e5e113cf | 1372 | const struct snd_pcm_hw_constraint_ratdens *r) |
1da177e4 LT |
1373 | { |
1374 | return snd_pcm_hw_rule_add(runtime, cond, var, | |
e5e113cf | 1375 | snd_pcm_hw_rule_ratdens, (void *)r, |
1da177e4 LT |
1376 | var, -1); |
1377 | } | |
e88e8ae6 TI |
1378 | EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens); |
1379 | ||
877211f5 TI |
1380 | static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params, |
1381 | struct snd_pcm_hw_rule *rule) | |
1da177e4 LT |
1382 | { |
1383 | unsigned int l = (unsigned long) rule->private; | |
1384 | int width = l & 0xffff; | |
1385 | unsigned int msbits = l >> 16; | |
b55f9fdc TS |
1386 | const struct snd_interval *i = |
1387 | hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS); | |
8ef9df55 LPC |
1388 | |
1389 | if (!snd_interval_single(i)) | |
1390 | return 0; | |
1391 | ||
1392 | if ((snd_interval_value(i) == width) || | |
1393 | (width == 0 && snd_interval_value(i) > msbits)) | |
19f52fae | 1394 | params->msbits = min_not_zero(params->msbits, msbits); |
8ef9df55 | 1395 | |
1da177e4 LT |
1396 | return 0; |
1397 | } | |
1398 | ||
1399 | /** | |
1c85cc64 | 1400 | * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule |
df8db936 TI |
1401 | * @runtime: PCM runtime instance |
1402 | * @cond: condition bits | |
1403 | * @width: sample bits width | |
1404 | * @msbits: msbits width | |
eb7c06e8 | 1405 | * |
8ef9df55 LPC |
1406 | * This constraint will set the number of most significant bits (msbits) if a |
1407 | * sample format with the specified width has been select. If width is set to 0 | |
1408 | * the msbits will be set for any sample format with a width larger than the | |
1409 | * specified msbits. | |
1410 | * | |
eb7c06e8 | 1411 | * Return: Zero if successful, or a negative error code on failure. |
1da177e4 | 1412 | */ |
877211f5 | 1413 | int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime, |
1da177e4 LT |
1414 | unsigned int cond, |
1415 | unsigned int width, | |
1416 | unsigned int msbits) | |
1417 | { | |
1418 | unsigned long l = (msbits << 16) | width; | |
1419 | return snd_pcm_hw_rule_add(runtime, cond, -1, | |
1420 | snd_pcm_hw_rule_msbits, | |
1421 | (void*) l, | |
1422 | SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1); | |
1423 | } | |
e88e8ae6 TI |
1424 | EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits); |
1425 | ||
877211f5 TI |
1426 | static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params, |
1427 | struct snd_pcm_hw_rule *rule) | |
1da177e4 LT |
1428 | { |
1429 | unsigned long step = (unsigned long) rule->private; | |
0f519b62 | 1430 | return snd_interval_step(hw_param_interval(params, rule->var), step); |
1da177e4 LT |
1431 | } |
1432 | ||
1433 | /** | |
1c85cc64 | 1434 | * snd_pcm_hw_constraint_step - add a hw constraint step rule |
df8db936 TI |
1435 | * @runtime: PCM runtime instance |
1436 | * @cond: condition bits | |
1437 | * @var: hw_params variable to apply the step constraint | |
1438 | * @step: step size | |
eb7c06e8 YB |
1439 | * |
1440 | * Return: Zero if successful, or a negative error code on failure. | |
1da177e4 | 1441 | */ |
877211f5 | 1442 | int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime, |
1da177e4 LT |
1443 | unsigned int cond, |
1444 | snd_pcm_hw_param_t var, | |
1445 | unsigned long step) | |
1446 | { | |
1447 | return snd_pcm_hw_rule_add(runtime, cond, var, | |
1448 | snd_pcm_hw_rule_step, (void *) step, | |
1449 | var, -1); | |
1450 | } | |
e88e8ae6 TI |
1451 | EXPORT_SYMBOL(snd_pcm_hw_constraint_step); |
1452 | ||
877211f5 | 1453 | static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule) |
1da177e4 | 1454 | { |
d03af9b8 | 1455 | static const unsigned int pow2_sizes[] = { |
1da177e4 LT |
1456 | 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7, |
1457 | 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15, | |
1458 | 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23, | |
1459 | 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30 | |
1460 | }; | |
1461 | return snd_interval_list(hw_param_interval(params, rule->var), | |
1462 | ARRAY_SIZE(pow2_sizes), pow2_sizes, 0); | |
1463 | } | |
1464 | ||
1465 | /** | |
1c85cc64 | 1466 | * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule |
df8db936 TI |
1467 | * @runtime: PCM runtime instance |
1468 | * @cond: condition bits | |
1469 | * @var: hw_params variable to apply the power-of-2 constraint | |
eb7c06e8 YB |
1470 | * |
1471 | * Return: Zero if successful, or a negative error code on failure. | |
1da177e4 | 1472 | */ |
877211f5 | 1473 | int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime, |
1da177e4 LT |
1474 | unsigned int cond, |
1475 | snd_pcm_hw_param_t var) | |
1476 | { | |
1477 | return snd_pcm_hw_rule_add(runtime, cond, var, | |
1478 | snd_pcm_hw_rule_pow2, NULL, | |
1479 | var, -1); | |
1480 | } | |
e88e8ae6 TI |
1481 | EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2); |
1482 | ||
d5b702a6 CL |
1483 | static int snd_pcm_hw_rule_noresample_func(struct snd_pcm_hw_params *params, |
1484 | struct snd_pcm_hw_rule *rule) | |
1485 | { | |
1486 | unsigned int base_rate = (unsigned int)(uintptr_t)rule->private; | |
1487 | struct snd_interval *rate; | |
1488 | ||
1489 | rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE); | |
1490 | return snd_interval_list(rate, 1, &base_rate, 0); | |
1491 | } | |
1492 | ||
1493 | /** | |
1494 | * snd_pcm_hw_rule_noresample - add a rule to allow disabling hw resampling | |
1495 | * @runtime: PCM runtime instance | |
1496 | * @base_rate: the rate at which the hardware does not resample | |
eb7c06e8 YB |
1497 | * |
1498 | * Return: Zero if successful, or a negative error code on failure. | |
d5b702a6 CL |
1499 | */ |
1500 | int snd_pcm_hw_rule_noresample(struct snd_pcm_runtime *runtime, | |
1501 | unsigned int base_rate) | |
1502 | { | |
1503 | return snd_pcm_hw_rule_add(runtime, SNDRV_PCM_HW_PARAMS_NORESAMPLE, | |
1504 | SNDRV_PCM_HW_PARAM_RATE, | |
1505 | snd_pcm_hw_rule_noresample_func, | |
1506 | (void *)(uintptr_t)base_rate, | |
1507 | SNDRV_PCM_HW_PARAM_RATE, -1); | |
1508 | } | |
1509 | EXPORT_SYMBOL(snd_pcm_hw_rule_noresample); | |
1510 | ||
877211f5 | 1511 | static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params, |
123992f7 | 1512 | snd_pcm_hw_param_t var) |
1da177e4 LT |
1513 | { |
1514 | if (hw_is_mask(var)) { | |
1515 | snd_mask_any(hw_param_mask(params, var)); | |
1516 | params->cmask |= 1 << var; | |
1517 | params->rmask |= 1 << var; | |
1518 | return; | |
1519 | } | |
1520 | if (hw_is_interval(var)) { | |
1521 | snd_interval_any(hw_param_interval(params, var)); | |
1522 | params->cmask |= 1 << var; | |
1523 | params->rmask |= 1 << var; | |
1524 | return; | |
1525 | } | |
1526 | snd_BUG(); | |
1527 | } | |
1528 | ||
877211f5 | 1529 | void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params) |
1da177e4 LT |
1530 | { |
1531 | unsigned int k; | |
1532 | memset(params, 0, sizeof(*params)); | |
1533 | for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) | |
1534 | _snd_pcm_hw_param_any(params, k); | |
1535 | for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) | |
1536 | _snd_pcm_hw_param_any(params, k); | |
1537 | params->info = ~0U; | |
1538 | } | |
e88e8ae6 | 1539 | EXPORT_SYMBOL(_snd_pcm_hw_params_any); |
1da177e4 LT |
1540 | |
1541 | /** | |
1c85cc64 | 1542 | * snd_pcm_hw_param_value - return @params field @var value |
df8db936 TI |
1543 | * @params: the hw_params instance |
1544 | * @var: parameter to retrieve | |
1c85cc64 | 1545 | * @dir: pointer to the direction (-1,0,1) or %NULL |
1da177e4 | 1546 | * |
eb7c06e8 YB |
1547 | * Return: The value for field @var if it's fixed in configuration space |
1548 | * defined by @params. -%EINVAL otherwise. | |
1da177e4 | 1549 | */ |
e88e8ae6 TI |
1550 | int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params, |
1551 | snd_pcm_hw_param_t var, int *dir) | |
1da177e4 LT |
1552 | { |
1553 | if (hw_is_mask(var)) { | |
877211f5 | 1554 | const struct snd_mask *mask = hw_param_mask_c(params, var); |
1da177e4 LT |
1555 | if (!snd_mask_single(mask)) |
1556 | return -EINVAL; | |
1557 | if (dir) | |
1558 | *dir = 0; | |
1559 | return snd_mask_value(mask); | |
1560 | } | |
1561 | if (hw_is_interval(var)) { | |
877211f5 | 1562 | const struct snd_interval *i = hw_param_interval_c(params, var); |
1da177e4 LT |
1563 | if (!snd_interval_single(i)) |
1564 | return -EINVAL; | |
1565 | if (dir) | |
1566 | *dir = i->openmin; | |
1567 | return snd_interval_value(i); | |
1568 | } | |
1da177e4 LT |
1569 | return -EINVAL; |
1570 | } | |
e88e8ae6 | 1571 | EXPORT_SYMBOL(snd_pcm_hw_param_value); |
1da177e4 | 1572 | |
877211f5 | 1573 | void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params, |
1da177e4 LT |
1574 | snd_pcm_hw_param_t var) |
1575 | { | |
1576 | if (hw_is_mask(var)) { | |
1577 | snd_mask_none(hw_param_mask(params, var)); | |
1578 | params->cmask |= 1 << var; | |
1579 | params->rmask |= 1 << var; | |
1580 | } else if (hw_is_interval(var)) { | |
1581 | snd_interval_none(hw_param_interval(params, var)); | |
1582 | params->cmask |= 1 << var; | |
1583 | params->rmask |= 1 << var; | |
1584 | } else { | |
1585 | snd_BUG(); | |
1586 | } | |
1587 | } | |
e88e8ae6 | 1588 | EXPORT_SYMBOL(_snd_pcm_hw_param_setempty); |
1da177e4 | 1589 | |
877211f5 | 1590 | static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params, |
123992f7 | 1591 | snd_pcm_hw_param_t var) |
1da177e4 LT |
1592 | { |
1593 | int changed; | |
1594 | if (hw_is_mask(var)) | |
1595 | changed = snd_mask_refine_first(hw_param_mask(params, var)); | |
1596 | else if (hw_is_interval(var)) | |
1597 | changed = snd_interval_refine_first(hw_param_interval(params, var)); | |
2f4ca8e5 | 1598 | else |
1da177e4 | 1599 | return -EINVAL; |
7a0a8716 | 1600 | if (changed > 0) { |
1da177e4 LT |
1601 | params->cmask |= 1 << var; |
1602 | params->rmask |= 1 << var; | |
1603 | } | |
1604 | return changed; | |
1605 | } | |
1606 | ||
1607 | ||
1608 | /** | |
1c85cc64 | 1609 | * snd_pcm_hw_param_first - refine config space and return minimum value |
df8db936 TI |
1610 | * @pcm: PCM instance |
1611 | * @params: the hw_params instance | |
1612 | * @var: parameter to retrieve | |
1c85cc64 | 1613 | * @dir: pointer to the direction (-1,0,1) or %NULL |
1da177e4 | 1614 | * |
1c85cc64 | 1615 | * Inside configuration space defined by @params remove from @var all |
1da177e4 | 1616 | * values > minimum. Reduce configuration space accordingly. |
eb7c06e8 YB |
1617 | * |
1618 | * Return: The minimum, or a negative error code on failure. | |
1da177e4 | 1619 | */ |
e88e8ae6 TI |
1620 | int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm, |
1621 | struct snd_pcm_hw_params *params, | |
1622 | snd_pcm_hw_param_t var, int *dir) | |
1da177e4 LT |
1623 | { |
1624 | int changed = _snd_pcm_hw_param_first(params, var); | |
1625 | if (changed < 0) | |
1626 | return changed; | |
1627 | if (params->rmask) { | |
1628 | int err = snd_pcm_hw_refine(pcm, params); | |
fe08f34d | 1629 | if (err < 0) |
7eaa943c | 1630 | return err; |
1da177e4 LT |
1631 | } |
1632 | return snd_pcm_hw_param_value(params, var, dir); | |
1633 | } | |
e88e8ae6 TI |
1634 | EXPORT_SYMBOL(snd_pcm_hw_param_first); |
1635 | ||
877211f5 | 1636 | static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params, |
123992f7 | 1637 | snd_pcm_hw_param_t var) |
1da177e4 LT |
1638 | { |
1639 | int changed; | |
1640 | if (hw_is_mask(var)) | |
1641 | changed = snd_mask_refine_last(hw_param_mask(params, var)); | |
1642 | else if (hw_is_interval(var)) | |
1643 | changed = snd_interval_refine_last(hw_param_interval(params, var)); | |
2f4ca8e5 | 1644 | else |
1da177e4 | 1645 | return -EINVAL; |
7a0a8716 | 1646 | if (changed > 0) { |
1da177e4 LT |
1647 | params->cmask |= 1 << var; |
1648 | params->rmask |= 1 << var; | |
1649 | } | |
1650 | return changed; | |
1651 | } | |
1652 | ||
1653 | ||
1654 | /** | |
1c85cc64 | 1655 | * snd_pcm_hw_param_last - refine config space and return maximum value |
df8db936 TI |
1656 | * @pcm: PCM instance |
1657 | * @params: the hw_params instance | |
1658 | * @var: parameter to retrieve | |
1c85cc64 | 1659 | * @dir: pointer to the direction (-1,0,1) or %NULL |
1da177e4 | 1660 | * |
1c85cc64 | 1661 | * Inside configuration space defined by @params remove from @var all |
1da177e4 | 1662 | * values < maximum. Reduce configuration space accordingly. |
eb7c06e8 YB |
1663 | * |
1664 | * Return: The maximum, or a negative error code on failure. | |
1da177e4 | 1665 | */ |
e88e8ae6 TI |
1666 | int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm, |
1667 | struct snd_pcm_hw_params *params, | |
1668 | snd_pcm_hw_param_t var, int *dir) | |
1da177e4 LT |
1669 | { |
1670 | int changed = _snd_pcm_hw_param_last(params, var); | |
1671 | if (changed < 0) | |
1672 | return changed; | |
1673 | if (params->rmask) { | |
1674 | int err = snd_pcm_hw_refine(pcm, params); | |
fe08f34d | 1675 | if (err < 0) |
7eaa943c | 1676 | return err; |
1da177e4 LT |
1677 | } |
1678 | return snd_pcm_hw_param_value(params, var, dir); | |
1679 | } | |
e88e8ae6 | 1680 | EXPORT_SYMBOL(snd_pcm_hw_param_last); |
1da177e4 | 1681 | |
877211f5 | 1682 | static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream, |
1da177e4 LT |
1683 | void *arg) |
1684 | { | |
877211f5 | 1685 | struct snd_pcm_runtime *runtime = substream->runtime; |
1da177e4 LT |
1686 | unsigned long flags; |
1687 | snd_pcm_stream_lock_irqsave(substream, flags); | |
1688 | if (snd_pcm_running(substream) && | |
1689 | snd_pcm_update_hw_ptr(substream) >= 0) | |
1690 | runtime->status->hw_ptr %= runtime->buffer_size; | |
0e8014d7 | 1691 | else { |
1da177e4 | 1692 | runtime->status->hw_ptr = 0; |
0e8014d7 PLB |
1693 | runtime->hw_ptr_wrap = 0; |
1694 | } | |
1da177e4 LT |
1695 | snd_pcm_stream_unlock_irqrestore(substream, flags); |
1696 | return 0; | |
1697 | } | |
1698 | ||
877211f5 | 1699 | static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream, |
1da177e4 LT |
1700 | void *arg) |
1701 | { | |
877211f5 TI |
1702 | struct snd_pcm_channel_info *info = arg; |
1703 | struct snd_pcm_runtime *runtime = substream->runtime; | |
1da177e4 LT |
1704 | int width; |
1705 | if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) { | |
1706 | info->offset = -1; | |
1707 | return 0; | |
1708 | } | |
1709 | width = snd_pcm_format_physical_width(runtime->format); | |
1710 | if (width < 0) | |
1711 | return width; | |
1712 | info->offset = 0; | |
1713 | switch (runtime->access) { | |
1714 | case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED: | |
1715 | case SNDRV_PCM_ACCESS_RW_INTERLEAVED: | |
1716 | info->first = info->channel * width; | |
1717 | info->step = runtime->channels * width; | |
1718 | break; | |
1719 | case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED: | |
1720 | case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED: | |
1721 | { | |
1722 | size_t size = runtime->dma_bytes / runtime->channels; | |
1723 | info->first = info->channel * size * 8; | |
1724 | info->step = width; | |
1725 | break; | |
1726 | } | |
1727 | default: | |
1728 | snd_BUG(); | |
1729 | break; | |
1730 | } | |
1731 | return 0; | |
1732 | } | |
1733 | ||
8bea869c JK |
1734 | static int snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream *substream, |
1735 | void *arg) | |
1736 | { | |
1737 | struct snd_pcm_hw_params *params = arg; | |
1738 | snd_pcm_format_t format; | |
a9960e6a CL |
1739 | int channels; |
1740 | ssize_t frame_size; | |
8bea869c JK |
1741 | |
1742 | params->fifo_size = substream->runtime->hw.fifo_size; | |
1743 | if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_FIFO_IN_FRAMES)) { | |
1744 | format = params_format(params); | |
1745 | channels = params_channels(params); | |
a9960e6a CL |
1746 | frame_size = snd_pcm_format_size(format, channels); |
1747 | if (frame_size > 0) | |
1748 | params->fifo_size /= (unsigned)frame_size; | |
8bea869c JK |
1749 | } |
1750 | return 0; | |
1751 | } | |
1752 | ||
1da177e4 LT |
1753 | /** |
1754 | * snd_pcm_lib_ioctl - a generic PCM ioctl callback | |
1755 | * @substream: the pcm substream instance | |
1756 | * @cmd: ioctl command | |
1757 | * @arg: ioctl argument | |
1758 | * | |
1759 | * Processes the generic ioctl commands for PCM. | |
1760 | * Can be passed as the ioctl callback for PCM ops. | |
1761 | * | |
eb7c06e8 | 1762 | * Return: Zero if successful, or a negative error code on failure. |
1da177e4 | 1763 | */ |
877211f5 | 1764 | int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream, |
1da177e4 LT |
1765 | unsigned int cmd, void *arg) |
1766 | { | |
1767 | switch (cmd) { | |
1da177e4 LT |
1768 | case SNDRV_PCM_IOCTL1_RESET: |
1769 | return snd_pcm_lib_ioctl_reset(substream, arg); | |
1770 | case SNDRV_PCM_IOCTL1_CHANNEL_INFO: | |
1771 | return snd_pcm_lib_ioctl_channel_info(substream, arg); | |
8bea869c JK |
1772 | case SNDRV_PCM_IOCTL1_FIFO_SIZE: |
1773 | return snd_pcm_lib_ioctl_fifo_size(substream, arg); | |
1da177e4 LT |
1774 | } |
1775 | return -ENXIO; | |
1776 | } | |
e88e8ae6 TI |
1777 | EXPORT_SYMBOL(snd_pcm_lib_ioctl); |
1778 | ||
1da177e4 LT |
1779 | /** |
1780 | * snd_pcm_period_elapsed - update the pcm status for the next period | |
1781 | * @substream: the pcm substream instance | |
1782 | * | |
1783 | * This function is called from the interrupt handler when the | |
1784 | * PCM has processed the period size. It will update the current | |
31e8960b | 1785 | * pointer, wake up sleepers, etc. |
1da177e4 LT |
1786 | * |
1787 | * Even if more than one periods have elapsed since the last call, you | |
1788 | * have to call this only once. | |
1789 | */ | |
877211f5 | 1790 | void snd_pcm_period_elapsed(struct snd_pcm_substream *substream) |
1da177e4 | 1791 | { |
877211f5 | 1792 | struct snd_pcm_runtime *runtime; |
1da177e4 LT |
1793 | unsigned long flags; |
1794 | ||
f5cdc9d4 | 1795 | if (snd_BUG_ON(!substream)) |
7eaa943c | 1796 | return; |
1da177e4 | 1797 | |
1da177e4 | 1798 | snd_pcm_stream_lock_irqsave(substream, flags); |
f5cdc9d4 | 1799 | if (PCM_RUNTIME_CHECK(substream)) |
1800 | goto _unlock; | |
1801 | runtime = substream->runtime; | |
1802 | ||
1da177e4 | 1803 | if (!snd_pcm_running(substream) || |
f240406b | 1804 | snd_pcm_update_hw_ptr0(substream, 1) < 0) |
1da177e4 LT |
1805 | goto _end; |
1806 | ||
90bbaf66 | 1807 | #ifdef CONFIG_SND_PCM_TIMER |
1da177e4 LT |
1808 | if (substream->timer_running) |
1809 | snd_timer_interrupt(substream->timer, 1); | |
90bbaf66 | 1810 | #endif |
1da177e4 | 1811 | _end: |
1da177e4 | 1812 | kill_fasync(&runtime->fasync, SIGIO, POLL_IN); |
f5cdc9d4 | 1813 | _unlock: |
3aa02cb6 | 1814 | snd_pcm_stream_unlock_irqrestore(substream, flags); |
1da177e4 | 1815 | } |
e88e8ae6 TI |
1816 | EXPORT_SYMBOL(snd_pcm_period_elapsed); |
1817 | ||
13075510 TI |
1818 | /* |
1819 | * Wait until avail_min data becomes available | |
1820 | * Returns a negative error code if any error occurs during operation. | |
1821 | * The available space is stored on availp. When err = 0 and avail = 0 | |
1822 | * on the capture stream, it indicates the stream is in DRAINING state. | |
1823 | */ | |
5daeba34 | 1824 | static int wait_for_avail(struct snd_pcm_substream *substream, |
13075510 TI |
1825 | snd_pcm_uframes_t *availp) |
1826 | { | |
1827 | struct snd_pcm_runtime *runtime = substream->runtime; | |
1828 | int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; | |
ac6424b9 | 1829 | wait_queue_entry_t wait; |
13075510 TI |
1830 | int err = 0; |
1831 | snd_pcm_uframes_t avail = 0; | |
f2b3614c TI |
1832 | long wait_time, tout; |
1833 | ||
763437a9 AV |
1834 | init_waitqueue_entry(&wait, current); |
1835 | set_current_state(TASK_INTERRUPTIBLE); | |
1836 | add_wait_queue(&runtime->tsleep, &wait); | |
1837 | ||
f2b3614c TI |
1838 | if (runtime->no_period_wakeup) |
1839 | wait_time = MAX_SCHEDULE_TIMEOUT; | |
1840 | else { | |
d64c5cf8 LG |
1841 | /* use wait time from substream if available */ |
1842 | if (substream->wait_time) { | |
1843 | wait_time = substream->wait_time; | |
1844 | } else { | |
1845 | wait_time = 10; | |
1846 | ||
1847 | if (runtime->rate) { | |
1848 | long t = runtime->period_size * 2 / | |
1849 | runtime->rate; | |
1850 | wait_time = max(t, wait_time); | |
1851 | } | |
1852 | wait_time = msecs_to_jiffies(wait_time * 1000); | |
f2b3614c | 1853 | } |
f2b3614c | 1854 | } |
763437a9 | 1855 | |
13075510 TI |
1856 | for (;;) { |
1857 | if (signal_pending(current)) { | |
1858 | err = -ERESTARTSYS; | |
1859 | break; | |
1860 | } | |
763437a9 AV |
1861 | |
1862 | /* | |
1863 | * We need to check if space became available already | |
1864 | * (and thus the wakeup happened already) first to close | |
1865 | * the race of space already having become available. | |
1866 | * This check must happen after been added to the waitqueue | |
1867 | * and having current state be INTERRUPTIBLE. | |
1868 | */ | |
763e5067 | 1869 | avail = snd_pcm_avail(substream); |
763437a9 AV |
1870 | if (avail >= runtime->twake) |
1871 | break; | |
13075510 | 1872 | snd_pcm_stream_unlock_irq(substream); |
763437a9 AV |
1873 | |
1874 | tout = schedule_timeout(wait_time); | |
1875 | ||
13075510 | 1876 | snd_pcm_stream_lock_irq(substream); |
763437a9 | 1877 | set_current_state(TASK_INTERRUPTIBLE); |
13075510 TI |
1878 | switch (runtime->status->state) { |
1879 | case SNDRV_PCM_STATE_SUSPENDED: | |
1880 | err = -ESTRPIPE; | |
1881 | goto _endloop; | |
1882 | case SNDRV_PCM_STATE_XRUN: | |
1883 | err = -EPIPE; | |
1884 | goto _endloop; | |
1885 | case SNDRV_PCM_STATE_DRAINING: | |
1886 | if (is_playback) | |
1887 | err = -EPIPE; | |
1888 | else | |
1889 | avail = 0; /* indicate draining */ | |
1890 | goto _endloop; | |
1891 | case SNDRV_PCM_STATE_OPEN: | |
1892 | case SNDRV_PCM_STATE_SETUP: | |
1893 | case SNDRV_PCM_STATE_DISCONNECTED: | |
1894 | err = -EBADFD; | |
1895 | goto _endloop; | |
ed697e1a JK |
1896 | case SNDRV_PCM_STATE_PAUSED: |
1897 | continue; | |
13075510 TI |
1898 | } |
1899 | if (!tout) { | |
09e56df8 TI |
1900 | pcm_dbg(substream->pcm, |
1901 | "%s write error (DMA or IRQ trouble?)\n", | |
1902 | is_playback ? "playback" : "capture"); | |
13075510 TI |
1903 | err = -EIO; |
1904 | break; | |
1905 | } | |
13075510 TI |
1906 | } |
1907 | _endloop: | |
763437a9 | 1908 | set_current_state(TASK_RUNNING); |
c91a988d | 1909 | remove_wait_queue(&runtime->tsleep, &wait); |
13075510 TI |
1910 | *availp = avail; |
1911 | return err; | |
1912 | } | |
1913 | ||
9f600630 TI |
1914 | typedef int (*pcm_transfer_f)(struct snd_pcm_substream *substream, |
1915 | int channel, unsigned long hwoff, | |
1916 | void *buf, unsigned long bytes); | |
bdc4acf7 | 1917 | |
9f600630 TI |
1918 | typedef int (*pcm_copy_f)(struct snd_pcm_substream *, snd_pcm_uframes_t, void *, |
1919 | snd_pcm_uframes_t, snd_pcm_uframes_t, pcm_transfer_f); | |
1920 | ||
1921 | /* calculate the target DMA-buffer position to be written/read */ | |
1922 | static void *get_dma_ptr(struct snd_pcm_runtime *runtime, | |
1923 | int channel, unsigned long hwoff) | |
1da177e4 | 1924 | { |
9f600630 TI |
1925 | return runtime->dma_area + hwoff + |
1926 | channel * (runtime->dma_bytes / runtime->channels); | |
1927 | } | |
1928 | ||
5c7264cf TI |
1929 | /* default copy_user ops for write; used for both interleaved and non- modes */ |
1930 | static int default_write_copy(struct snd_pcm_substream *substream, | |
1931 | int channel, unsigned long hwoff, | |
1932 | void *buf, unsigned long bytes) | |
9f600630 TI |
1933 | { |
1934 | if (copy_from_user(get_dma_ptr(substream->runtime, channel, hwoff), | |
5c7264cf | 1935 | (void __user *)buf, bytes)) |
9f600630 TI |
1936 | return -EFAULT; |
1937 | return 0; | |
1938 | } | |
1939 | ||
68541213 TI |
1940 | /* default copy_kernel ops for write */ |
1941 | static int default_write_copy_kernel(struct snd_pcm_substream *substream, | |
1942 | int channel, unsigned long hwoff, | |
1943 | void *buf, unsigned long bytes) | |
1944 | { | |
1945 | memcpy(get_dma_ptr(substream->runtime, channel, hwoff), buf, bytes); | |
1da177e4 LT |
1946 | return 0; |
1947 | } | |
1da177e4 | 1948 | |
9f600630 TI |
1949 | /* fill silence instead of copy data; called as a transfer helper |
1950 | * from __snd_pcm_lib_write() or directly from noninterleaved_copy() when | |
1951 | * a NULL buffer is passed | |
1952 | */ | |
1953 | static int fill_silence(struct snd_pcm_substream *substream, int channel, | |
1954 | unsigned long hwoff, void *buf, unsigned long bytes) | |
1da177e4 | 1955 | { |
877211f5 | 1956 | struct snd_pcm_runtime *runtime = substream->runtime; |
1da177e4 | 1957 | |
9f600630 | 1958 | if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK) |
1da177e4 | 1959 | return 0; |
9f600630 TI |
1960 | if (substream->ops->fill_silence) |
1961 | return substream->ops->fill_silence(substream, channel, | |
1962 | hwoff, bytes); | |
1da177e4 | 1963 | |
9f600630 TI |
1964 | snd_pcm_format_set_silence(runtime->format, |
1965 | get_dma_ptr(runtime, channel, hwoff), | |
1966 | bytes_to_samples(runtime, bytes)); | |
1da177e4 LT |
1967 | return 0; |
1968 | } | |
1da177e4 | 1969 | |
5c7264cf TI |
1970 | /* default copy_user ops for read; used for both interleaved and non- modes */ |
1971 | static int default_read_copy(struct snd_pcm_substream *substream, | |
1972 | int channel, unsigned long hwoff, | |
1973 | void *buf, unsigned long bytes) | |
1974 | { | |
1975 | if (copy_to_user((void __user *)buf, | |
1976 | get_dma_ptr(substream->runtime, channel, hwoff), | |
1977 | bytes)) | |
1978 | return -EFAULT; | |
1979 | return 0; | |
1980 | } | |
1da177e4 | 1981 | |
68541213 TI |
1982 | /* default copy_kernel ops for read */ |
1983 | static int default_read_copy_kernel(struct snd_pcm_substream *substream, | |
1984 | int channel, unsigned long hwoff, | |
1985 | void *buf, unsigned long bytes) | |
1986 | { | |
1987 | memcpy(buf, get_dma_ptr(substream->runtime, channel, hwoff), bytes); | |
1988 | return 0; | |
1989 | } | |
1990 | ||
9f600630 TI |
1991 | /* call transfer function with the converted pointers and sizes; |
1992 | * for interleaved mode, it's one shot for all samples | |
1993 | */ | |
1994 | static int interleaved_copy(struct snd_pcm_substream *substream, | |
1995 | snd_pcm_uframes_t hwoff, void *data, | |
1996 | snd_pcm_uframes_t off, | |
1997 | snd_pcm_uframes_t frames, | |
1998 | pcm_transfer_f transfer) | |
1999 | { | |
2000 | struct snd_pcm_runtime *runtime = substream->runtime; | |
2001 | ||
2002 | /* convert to bytes */ | |
2003 | hwoff = frames_to_bytes(runtime, hwoff); | |
2004 | off = frames_to_bytes(runtime, off); | |
2005 | frames = frames_to_bytes(runtime, frames); | |
2006 | return transfer(substream, 0, hwoff, data + off, frames); | |
2007 | } | |
2008 | ||
2009 | /* call transfer function with the converted pointers and sizes for each | |
2010 | * non-interleaved channel; when buffer is NULL, silencing instead of copying | |
2011 | */ | |
2012 | static int noninterleaved_copy(struct snd_pcm_substream *substream, | |
2013 | snd_pcm_uframes_t hwoff, void *data, | |
2014 | snd_pcm_uframes_t off, | |
2015 | snd_pcm_uframes_t frames, | |
2016 | pcm_transfer_f transfer) | |
bdc4acf7 TI |
2017 | { |
2018 | struct snd_pcm_runtime *runtime = substream->runtime; | |
bdc4acf7 | 2019 | int channels = runtime->channels; |
9f600630 TI |
2020 | void **bufs = data; |
2021 | int c, err; | |
2022 | ||
2023 | /* convert to bytes; note that it's not frames_to_bytes() here. | |
2024 | * in non-interleaved mode, we copy for each channel, thus | |
2025 | * each copy is n_samples bytes x channels = whole frames. | |
2026 | */ | |
2027 | off = samples_to_bytes(runtime, off); | |
2028 | frames = samples_to_bytes(runtime, frames); | |
2029 | hwoff = samples_to_bytes(runtime, hwoff); | |
2030 | for (c = 0; c < channels; ++c, ++bufs) { | |
2031 | if (!data || !*bufs) | |
2032 | err = fill_silence(substream, c, hwoff, NULL, frames); | |
2033 | else | |
2034 | err = transfer(substream, c, hwoff, *bufs + off, | |
2035 | frames); | |
2036 | if (err < 0) | |
2037 | return err; | |
1da177e4 | 2038 | } |
bdc4acf7 TI |
2039 | return 0; |
2040 | } | |
2041 | ||
a9cd29e7 TI |
2042 | /* fill silence on the given buffer position; |
2043 | * called from snd_pcm_playback_silence() | |
2044 | */ | |
2045 | static int fill_silence_frames(struct snd_pcm_substream *substream, | |
2046 | snd_pcm_uframes_t off, snd_pcm_uframes_t frames) | |
2047 | { | |
2048 | if (substream->runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED || | |
2049 | substream->runtime->access == SNDRV_PCM_ACCESS_MMAP_INTERLEAVED) | |
2050 | return interleaved_copy(substream, off, NULL, 0, frames, | |
2051 | fill_silence); | |
2052 | else | |
2053 | return noninterleaved_copy(substream, off, NULL, 0, frames, | |
2054 | fill_silence); | |
1da177e4 LT |
2055 | } |
2056 | ||
7eaa943c TI |
2057 | /* sanity-check for read/write methods */ |
2058 | static int pcm_sanity_check(struct snd_pcm_substream *substream) | |
1da177e4 | 2059 | { |
877211f5 | 2060 | struct snd_pcm_runtime *runtime; |
7eaa943c TI |
2061 | if (PCM_RUNTIME_CHECK(substream)) |
2062 | return -ENXIO; | |
1da177e4 | 2063 | runtime = substream->runtime; |
bdc4acf7 | 2064 | if (snd_BUG_ON(!substream->ops->copy_user && !runtime->dma_area)) |
7eaa943c | 2065 | return -EINVAL; |
1da177e4 LT |
2066 | if (runtime->status->state == SNDRV_PCM_STATE_OPEN) |
2067 | return -EBADFD; | |
7eaa943c TI |
2068 | return 0; |
2069 | } | |
2070 | ||
6ba63929 | 2071 | static int pcm_accessible_state(struct snd_pcm_runtime *runtime) |
7eaa943c | 2072 | { |
6ba63929 TI |
2073 | switch (runtime->status->state) { |
2074 | case SNDRV_PCM_STATE_PREPARED: | |
2075 | case SNDRV_PCM_STATE_RUNNING: | |
2076 | case SNDRV_PCM_STATE_PAUSED: | |
2077 | return 0; | |
2078 | case SNDRV_PCM_STATE_XRUN: | |
2079 | return -EPIPE; | |
2080 | case SNDRV_PCM_STATE_SUSPENDED: | |
2081 | return -ESTRPIPE; | |
2082 | default: | |
2083 | return -EBADFD; | |
2084 | } | |
1da177e4 LT |
2085 | } |
2086 | ||
66e01a5c TS |
2087 | /* update to the given appl_ptr and call ack callback if needed; |
2088 | * when an error is returned, take back to the original value | |
2089 | */ | |
2090 | int pcm_lib_apply_appl_ptr(struct snd_pcm_substream *substream, | |
2091 | snd_pcm_uframes_t appl_ptr) | |
1da177e4 | 2092 | { |
877211f5 | 2093 | struct snd_pcm_runtime *runtime = substream->runtime; |
66e01a5c TS |
2094 | snd_pcm_uframes_t old_appl_ptr = runtime->control->appl_ptr; |
2095 | int ret; | |
2096 | ||
f8ff2f28 TI |
2097 | if (old_appl_ptr == appl_ptr) |
2098 | return 0; | |
2099 | ||
66e01a5c TS |
2100 | runtime->control->appl_ptr = appl_ptr; |
2101 | if (substream->ops->ack) { | |
2102 | ret = substream->ops->ack(substream); | |
2103 | if (ret < 0) { | |
2104 | runtime->control->appl_ptr = old_appl_ptr; | |
2105 | return ret; | |
1da177e4 LT |
2106 | } |
2107 | } | |
fccf5388 TS |
2108 | |
2109 | trace_applptr(substream, old_appl_ptr, appl_ptr); | |
2110 | ||
1da177e4 LT |
2111 | return 0; |
2112 | } | |
66e01a5c | 2113 | |
5c7264cf TI |
2114 | /* the common loop for read/write data */ |
2115 | snd_pcm_sframes_t __snd_pcm_lib_xfer(struct snd_pcm_substream *substream, | |
2116 | void *data, bool interleaved, | |
68541213 | 2117 | snd_pcm_uframes_t size, bool in_kernel) |
1da177e4 | 2118 | { |
877211f5 | 2119 | struct snd_pcm_runtime *runtime = substream->runtime; |
1da177e4 LT |
2120 | snd_pcm_uframes_t xfer = 0; |
2121 | snd_pcm_uframes_t offset = 0; | |
0910c216 | 2122 | snd_pcm_uframes_t avail; |
9f600630 TI |
2123 | pcm_copy_f writer; |
2124 | pcm_transfer_f transfer; | |
c48f12ee | 2125 | bool nonblock; |
5c7264cf | 2126 | bool is_playback; |
7eaa943c | 2127 | int err; |
1da177e4 | 2128 | |
7eaa943c TI |
2129 | err = pcm_sanity_check(substream); |
2130 | if (err < 0) | |
2131 | return err; | |
e88e8ae6 | 2132 | |
5c7264cf | 2133 | is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; |
c48f12ee TI |
2134 | if (interleaved) { |
2135 | if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED && | |
2136 | runtime->channels > 1) | |
2137 | return -EINVAL; | |
9f600630 | 2138 | writer = interleaved_copy; |
1da177e4 | 2139 | } else { |
c48f12ee TI |
2140 | if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) |
2141 | return -EINVAL; | |
9f600630 | 2142 | writer = noninterleaved_copy; |
1da177e4 | 2143 | } |
1da177e4 | 2144 | |
9f600630 | 2145 | if (!data) { |
5c7264cf TI |
2146 | if (is_playback) |
2147 | transfer = fill_silence; | |
2148 | else | |
2149 | return -EINVAL; | |
68541213 TI |
2150 | } else if (in_kernel) { |
2151 | if (substream->ops->copy_kernel) | |
2152 | transfer = substream->ops->copy_kernel; | |
2153 | else | |
2154 | transfer = is_playback ? | |
2155 | default_write_copy_kernel : default_read_copy_kernel; | |
9f600630 TI |
2156 | } else { |
2157 | if (substream->ops->copy_user) | |
2158 | transfer = (pcm_transfer_f)substream->ops->copy_user; | |
2159 | else | |
5c7264cf TI |
2160 | transfer = is_playback ? |
2161 | default_write_copy : default_read_copy; | |
c48f12ee | 2162 | } |
1da177e4 LT |
2163 | |
2164 | if (size == 0) | |
2165 | return 0; | |
1da177e4 | 2166 | |
c48f12ee TI |
2167 | nonblock = !!(substream->f_flags & O_NONBLOCK); |
2168 | ||
1da177e4 | 2169 | snd_pcm_stream_lock_irq(substream); |
6ba63929 TI |
2170 | err = pcm_accessible_state(runtime); |
2171 | if (err < 0) | |
1da177e4 | 2172 | goto _end_unlock; |
1da177e4 | 2173 | |
64b6acf6 RBP |
2174 | runtime->twake = runtime->control->avail_min ? : 1; |
2175 | if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) | |
2176 | snd_pcm_update_hw_ptr(substream); | |
2177 | ||
932a8151 RBP |
2178 | /* |
2179 | * If size < start_threshold, wait indefinitely. Another | |
2180 | * thread may start capture | |
2181 | */ | |
5c7264cf | 2182 | if (!is_playback && |
00a399ca TI |
2183 | runtime->status->state == SNDRV_PCM_STATE_PREPARED && |
2184 | size >= runtime->start_threshold) { | |
2185 | err = snd_pcm_start(substream); | |
2186 | if (err < 0) | |
6ba63929 | 2187 | goto _end_unlock; |
1da177e4 LT |
2188 | } |
2189 | ||
763e5067 | 2190 | avail = snd_pcm_avail(substream); |
64b6acf6 | 2191 | |
1da177e4 LT |
2192 | while (size > 0) { |
2193 | snd_pcm_uframes_t frames, appl_ptr, appl_ofs; | |
1da177e4 | 2194 | snd_pcm_uframes_t cont; |
13075510 | 2195 | if (!avail) { |
5c7264cf TI |
2196 | if (!is_playback && |
2197 | runtime->status->state == SNDRV_PCM_STATE_DRAINING) { | |
13075510 | 2198 | snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP); |
1da177e4 LT |
2199 | goto _end_unlock; |
2200 | } | |
1da177e4 LT |
2201 | if (nonblock) { |
2202 | err = -EAGAIN; | |
2203 | goto _end_unlock; | |
2204 | } | |
5daeba34 DD |
2205 | runtime->twake = min_t(snd_pcm_uframes_t, size, |
2206 | runtime->control->avail_min ? : 1); | |
2207 | err = wait_for_avail(substream, &avail); | |
13075510 | 2208 | if (err < 0) |
443feb88 | 2209 | goto _end_unlock; |
13075510 TI |
2210 | if (!avail) |
2211 | continue; /* draining */ | |
1da177e4 | 2212 | } |
1da177e4 | 2213 | frames = size > avail ? avail : size; |
aa30db06 TI |
2214 | appl_ptr = READ_ONCE(runtime->control->appl_ptr); |
2215 | appl_ofs = appl_ptr % runtime->buffer_size; | |
2216 | cont = runtime->buffer_size - appl_ofs; | |
1da177e4 LT |
2217 | if (frames > cont) |
2218 | frames = cont; | |
7eaa943c | 2219 | if (snd_BUG_ON(!frames)) { |
315d9f1b TI |
2220 | err = -EINVAL; |
2221 | goto _end_unlock; | |
7eaa943c | 2222 | } |
1da177e4 | 2223 | snd_pcm_stream_unlock_irq(substream); |
5c7264cf | 2224 | err = writer(substream, appl_ofs, data, offset, frames, |
9f600630 | 2225 | transfer); |
1da177e4 | 2226 | snd_pcm_stream_lock_irq(substream); |
1250932e JK |
2227 | if (err < 0) |
2228 | goto _end_unlock; | |
6ba63929 TI |
2229 | err = pcm_accessible_state(runtime); |
2230 | if (err < 0) | |
1da177e4 | 2231 | goto _end_unlock; |
1da177e4 LT |
2232 | appl_ptr += frames; |
2233 | if (appl_ptr >= runtime->boundary) | |
2234 | appl_ptr -= runtime->boundary; | |
66e01a5c TS |
2235 | err = pcm_lib_apply_appl_ptr(substream, appl_ptr); |
2236 | if (err < 0) | |
2237 | goto _end_unlock; | |
1da177e4 LT |
2238 | |
2239 | offset += frames; | |
2240 | size -= frames; | |
2241 | xfer += frames; | |
0910c216 | 2242 | avail -= frames; |
5c7264cf TI |
2243 | if (is_playback && |
2244 | runtime->status->state == SNDRV_PCM_STATE_PREPARED && | |
2245 | snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) { | |
2246 | err = snd_pcm_start(substream); | |
2247 | if (err < 0) | |
2248 | goto _end_unlock; | |
2249 | } | |
1da177e4 LT |
2250 | } |
2251 | _end_unlock: | |
c91a988d | 2252 | runtime->twake = 0; |
1250932e JK |
2253 | if (xfer > 0 && err >= 0) |
2254 | snd_pcm_update_state(substream, runtime); | |
1da177e4 | 2255 | snd_pcm_stream_unlock_irq(substream); |
1da177e4 LT |
2256 | return xfer > 0 ? (snd_pcm_sframes_t)xfer : err; |
2257 | } | |
5c7264cf | 2258 | EXPORT_SYMBOL(__snd_pcm_lib_xfer); |
2d3391ec TI |
2259 | |
2260 | /* | |
2261 | * standard channel mapping helpers | |
2262 | */ | |
2263 | ||
2264 | /* default channel maps for multi-channel playbacks, up to 8 channels */ | |
2265 | const struct snd_pcm_chmap_elem snd_pcm_std_chmaps[] = { | |
2266 | { .channels = 1, | |
5efbc261 | 2267 | .map = { SNDRV_CHMAP_MONO } }, |
2d3391ec TI |
2268 | { .channels = 2, |
2269 | .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } }, | |
2270 | { .channels = 4, | |
2271 | .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, | |
2272 | SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } }, | |
2273 | { .channels = 6, | |
2274 | .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, | |
2275 | SNDRV_CHMAP_RL, SNDRV_CHMAP_RR, | |
2276 | SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE } }, | |
2277 | { .channels = 8, | |
2278 | .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, | |
2279 | SNDRV_CHMAP_RL, SNDRV_CHMAP_RR, | |
2280 | SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE, | |
2281 | SNDRV_CHMAP_SL, SNDRV_CHMAP_SR } }, | |
2282 | { } | |
2283 | }; | |
2284 | EXPORT_SYMBOL_GPL(snd_pcm_std_chmaps); | |
2285 | ||
2286 | /* alternative channel maps with CLFE <-> surround swapped for 6/8 channels */ | |
2287 | const struct snd_pcm_chmap_elem snd_pcm_alt_chmaps[] = { | |
2288 | { .channels = 1, | |
5efbc261 | 2289 | .map = { SNDRV_CHMAP_MONO } }, |
2d3391ec TI |
2290 | { .channels = 2, |
2291 | .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } }, | |
2292 | { .channels = 4, | |
2293 | .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, | |
2294 | SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } }, | |
2295 | { .channels = 6, | |
2296 | .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, | |
2297 | SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE, | |
2298 | SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } }, | |
2299 | { .channels = 8, | |
2300 | .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, | |
2301 | SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE, | |
2302 | SNDRV_CHMAP_RL, SNDRV_CHMAP_RR, | |
2303 | SNDRV_CHMAP_SL, SNDRV_CHMAP_SR } }, | |
2304 | { } | |
2305 | }; | |
2306 | EXPORT_SYMBOL_GPL(snd_pcm_alt_chmaps); | |
2307 | ||
2308 | static bool valid_chmap_channels(const struct snd_pcm_chmap *info, int ch) | |
2309 | { | |
2310 | if (ch > info->max_channels) | |
2311 | return false; | |
2312 | return !info->channel_mask || (info->channel_mask & (1U << ch)); | |
2313 | } | |
2314 | ||
2315 | static int pcm_chmap_ctl_info(struct snd_kcontrol *kcontrol, | |
2316 | struct snd_ctl_elem_info *uinfo) | |
2317 | { | |
2318 | struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol); | |
2319 | ||
2320 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; | |
2d3391ec TI |
2321 | uinfo->count = info->max_channels; |
2322 | uinfo->value.integer.min = 0; | |
2323 | uinfo->value.integer.max = SNDRV_CHMAP_LAST; | |
2324 | return 0; | |
2325 | } | |
2326 | ||
2327 | /* get callback for channel map ctl element | |
2328 | * stores the channel position firstly matching with the current channels | |
2329 | */ | |
2330 | static int pcm_chmap_ctl_get(struct snd_kcontrol *kcontrol, | |
2331 | struct snd_ctl_elem_value *ucontrol) | |
2332 | { | |
2333 | struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol); | |
2334 | unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id); | |
2335 | struct snd_pcm_substream *substream; | |
2336 | const struct snd_pcm_chmap_elem *map; | |
2337 | ||
2deaeaf1 | 2338 | if (!info->chmap) |
2d3391ec TI |
2339 | return -EINVAL; |
2340 | substream = snd_pcm_chmap_substream(info, idx); | |
2341 | if (!substream) | |
2342 | return -ENODEV; | |
2343 | memset(ucontrol->value.integer.value, 0, | |
fbd3eb7f | 2344 | sizeof(long) * info->max_channels); |
2d3391ec TI |
2345 | if (!substream->runtime) |
2346 | return 0; /* no channels set */ | |
2347 | for (map = info->chmap; map->channels; map++) { | |
2348 | int i; | |
2349 | if (map->channels == substream->runtime->channels && | |
2350 | valid_chmap_channels(info, map->channels)) { | |
2351 | for (i = 0; i < map->channels; i++) | |
2352 | ucontrol->value.integer.value[i] = map->map[i]; | |
2353 | return 0; | |
2354 | } | |
2355 | } | |
2356 | return -EINVAL; | |
2357 | } | |
2358 | ||
2359 | /* tlv callback for channel map ctl element | |
2360 | * expands the pre-defined channel maps in a form of TLV | |
2361 | */ | |
2362 | static int pcm_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag, | |
2363 | unsigned int size, unsigned int __user *tlv) | |
2364 | { | |
2365 | struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol); | |
2366 | const struct snd_pcm_chmap_elem *map; | |
2367 | unsigned int __user *dst; | |
2368 | int c, count = 0; | |
2369 | ||
2deaeaf1 | 2370 | if (!info->chmap) |
2d3391ec TI |
2371 | return -EINVAL; |
2372 | if (size < 8) | |
2373 | return -ENOMEM; | |
2374 | if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv)) | |
2375 | return -EFAULT; | |
2376 | size -= 8; | |
2377 | dst = tlv + 2; | |
2378 | for (map = info->chmap; map->channels; map++) { | |
2379 | int chs_bytes = map->channels * 4; | |
2380 | if (!valid_chmap_channels(info, map->channels)) | |
2381 | continue; | |
2382 | if (size < 8) | |
2383 | return -ENOMEM; | |
2384 | if (put_user(SNDRV_CTL_TLVT_CHMAP_FIXED, dst) || | |
2385 | put_user(chs_bytes, dst + 1)) | |
2386 | return -EFAULT; | |
2387 | dst += 2; | |
2388 | size -= 8; | |
2389 | count += 8; | |
2390 | if (size < chs_bytes) | |
2391 | return -ENOMEM; | |
2392 | size -= chs_bytes; | |
2393 | count += chs_bytes; | |
2394 | for (c = 0; c < map->channels; c++) { | |
2395 | if (put_user(map->map[c], dst)) | |
2396 | return -EFAULT; | |
2397 | dst++; | |
2398 | } | |
2399 | } | |
2400 | if (put_user(count, tlv + 1)) | |
2401 | return -EFAULT; | |
2402 | return 0; | |
2403 | } | |
2404 | ||
2405 | static void pcm_chmap_ctl_private_free(struct snd_kcontrol *kcontrol) | |
2406 | { | |
2407 | struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol); | |
2408 | info->pcm->streams[info->stream].chmap_kctl = NULL; | |
2409 | kfree(info); | |
2410 | } | |
2411 | ||
2412 | /** | |
2413 | * snd_pcm_add_chmap_ctls - create channel-mapping control elements | |
2414 | * @pcm: the assigned PCM instance | |
2415 | * @stream: stream direction | |
2416 | * @chmap: channel map elements (for query) | |
2417 | * @max_channels: the max number of channels for the stream | |
2418 | * @private_value: the value passed to each kcontrol's private_value field | |
2419 | * @info_ret: store struct snd_pcm_chmap instance if non-NULL | |
2420 | * | |
2421 | * Create channel-mapping control elements assigned to the given PCM stream(s). | |
eb7c06e8 | 2422 | * Return: Zero if successful, or a negative error value. |
2d3391ec TI |
2423 | */ |
2424 | int snd_pcm_add_chmap_ctls(struct snd_pcm *pcm, int stream, | |
2425 | const struct snd_pcm_chmap_elem *chmap, | |
2426 | int max_channels, | |
2427 | unsigned long private_value, | |
2428 | struct snd_pcm_chmap **info_ret) | |
2429 | { | |
2430 | struct snd_pcm_chmap *info; | |
2431 | struct snd_kcontrol_new knew = { | |
2432 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, | |
2433 | .access = SNDRV_CTL_ELEM_ACCESS_READ | | |
2d3391ec TI |
2434 | SNDRV_CTL_ELEM_ACCESS_TLV_READ | |
2435 | SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK, | |
2436 | .info = pcm_chmap_ctl_info, | |
2437 | .get = pcm_chmap_ctl_get, | |
2438 | .tlv.c = pcm_chmap_ctl_tlv, | |
2439 | }; | |
2440 | int err; | |
2441 | ||
8d879be8 TI |
2442 | if (WARN_ON(pcm->streams[stream].chmap_kctl)) |
2443 | return -EBUSY; | |
2d3391ec TI |
2444 | info = kzalloc(sizeof(*info), GFP_KERNEL); |
2445 | if (!info) | |
2446 | return -ENOMEM; | |
2447 | info->pcm = pcm; | |
2448 | info->stream = stream; | |
2449 | info->chmap = chmap; | |
2450 | info->max_channels = max_channels; | |
2451 | if (stream == SNDRV_PCM_STREAM_PLAYBACK) | |
2452 | knew.name = "Playback Channel Map"; | |
2453 | else | |
2454 | knew.name = "Capture Channel Map"; | |
2455 | knew.device = pcm->device; | |
2456 | knew.count = pcm->streams[stream].substream_count; | |
2457 | knew.private_value = private_value; | |
2458 | info->kctl = snd_ctl_new1(&knew, info); | |
2459 | if (!info->kctl) { | |
2460 | kfree(info); | |
2461 | return -ENOMEM; | |
2462 | } | |
2463 | info->kctl->private_free = pcm_chmap_ctl_private_free; | |
2464 | err = snd_ctl_add(pcm->card, info->kctl); | |
2465 | if (err < 0) | |
2466 | return err; | |
2467 | pcm->streams[stream].chmap_kctl = info->kctl; | |
2468 | if (info_ret) | |
2469 | *info_ret = info; | |
2470 | return 0; | |
2471 | } | |
2472 | EXPORT_SYMBOL_GPL(snd_pcm_add_chmap_ctls); |