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