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