Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Digital Audio (PCM) abstract layer | |
c1017a4c | 3 | * Copyright (c) by Jaroslav Kysela <perex@perex.cz> |
1da177e4 LT |
4 | * Abramo Bagnara <abramo@alsa-project.org> |
5 | * | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
20 | * | |
21 | */ | |
22 | ||
1da177e4 LT |
23 | #include <linux/slab.h> |
24 | #include <linux/time.h> | |
3f7440a6 | 25 | #include <linux/math64.h> |
1da177e4 LT |
26 | #include <sound/core.h> |
27 | #include <sound/control.h> | |
28 | #include <sound/info.h> | |
29 | #include <sound/pcm.h> | |
30 | #include <sound/pcm_params.h> | |
31 | #include <sound/timer.h> | |
32 | ||
33 | /* | |
34 | * fill ring buffer with silence | |
35 | * runtime->silence_start: starting pointer to silence area | |
36 | * runtime->silence_filled: size filled with silence | |
37 | * runtime->silence_threshold: threshold from application | |
38 | * runtime->silence_size: maximal size from application | |
39 | * | |
40 | * when runtime->silence_size >= runtime->boundary - fill processed area with silence immediately | |
41 | */ | |
877211f5 | 42 | void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_uframes_t new_hw_ptr) |
1da177e4 | 43 | { |
877211f5 | 44 | struct snd_pcm_runtime *runtime = substream->runtime; |
1da177e4 LT |
45 | snd_pcm_uframes_t frames, ofs, transfer; |
46 | ||
47 | if (runtime->silence_size < runtime->boundary) { | |
48 | snd_pcm_sframes_t noise_dist, n; | |
49 | if (runtime->silence_start != runtime->control->appl_ptr) { | |
50 | n = runtime->control->appl_ptr - runtime->silence_start; | |
51 | if (n < 0) | |
52 | n += runtime->boundary; | |
53 | if ((snd_pcm_uframes_t)n < runtime->silence_filled) | |
54 | runtime->silence_filled -= n; | |
55 | else | |
56 | runtime->silence_filled = 0; | |
57 | runtime->silence_start = runtime->control->appl_ptr; | |
58 | } | |
235475cb | 59 | if (runtime->silence_filled >= runtime->buffer_size) |
1da177e4 | 60 | return; |
1da177e4 LT |
61 | noise_dist = snd_pcm_playback_hw_avail(runtime) + runtime->silence_filled; |
62 | if (noise_dist >= (snd_pcm_sframes_t) runtime->silence_threshold) | |
63 | return; | |
64 | frames = runtime->silence_threshold - noise_dist; | |
65 | if (frames > runtime->silence_size) | |
66 | frames = runtime->silence_size; | |
67 | } else { | |
68 | if (new_hw_ptr == ULONG_MAX) { /* initialization */ | |
69 | snd_pcm_sframes_t avail = snd_pcm_playback_hw_avail(runtime); | |
70 | runtime->silence_filled = avail > 0 ? avail : 0; | |
71 | runtime->silence_start = (runtime->status->hw_ptr + | |
72 | runtime->silence_filled) % | |
73 | runtime->boundary; | |
74 | } else { | |
75 | ofs = runtime->status->hw_ptr; | |
76 | frames = new_hw_ptr - ofs; | |
77 | if ((snd_pcm_sframes_t)frames < 0) | |
78 | frames += runtime->boundary; | |
79 | runtime->silence_filled -= frames; | |
80 | if ((snd_pcm_sframes_t)runtime->silence_filled < 0) { | |
81 | runtime->silence_filled = 0; | |
9a826ddb | 82 | runtime->silence_start = new_hw_ptr; |
1da177e4 | 83 | } else { |
9a826ddb | 84 | runtime->silence_start = ofs; |
1da177e4 | 85 | } |
1da177e4 LT |
86 | } |
87 | frames = runtime->buffer_size - runtime->silence_filled; | |
88 | } | |
7eaa943c TI |
89 | if (snd_BUG_ON(frames > runtime->buffer_size)) |
90 | return; | |
1da177e4 LT |
91 | if (frames == 0) |
92 | return; | |
9a826ddb | 93 | ofs = runtime->silence_start % runtime->buffer_size; |
1da177e4 LT |
94 | while (frames > 0) { |
95 | transfer = ofs + frames > runtime->buffer_size ? runtime->buffer_size - ofs : frames; | |
96 | if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED || | |
97 | runtime->access == SNDRV_PCM_ACCESS_MMAP_INTERLEAVED) { | |
98 | if (substream->ops->silence) { | |
99 | int err; | |
100 | err = substream->ops->silence(substream, -1, ofs, transfer); | |
7eaa943c | 101 | snd_BUG_ON(err < 0); |
1da177e4 LT |
102 | } else { |
103 | char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, ofs); | |
104 | snd_pcm_format_set_silence(runtime->format, hwbuf, transfer * runtime->channels); | |
105 | } | |
106 | } else { | |
107 | unsigned int c; | |
108 | unsigned int channels = runtime->channels; | |
109 | if (substream->ops->silence) { | |
110 | for (c = 0; c < channels; ++c) { | |
111 | int err; | |
112 | err = substream->ops->silence(substream, c, ofs, transfer); | |
7eaa943c | 113 | snd_BUG_ON(err < 0); |
1da177e4 LT |
114 | } |
115 | } else { | |
116 | size_t dma_csize = runtime->dma_bytes / channels; | |
117 | for (c = 0; c < channels; ++c) { | |
118 | char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, ofs); | |
119 | snd_pcm_format_set_silence(runtime->format, hwbuf, transfer); | |
120 | } | |
121 | } | |
122 | } | |
123 | runtime->silence_filled += transfer; | |
124 | frames -= transfer; | |
125 | ofs = 0; | |
126 | } | |
127 | } | |
128 | ||
4d96eb25 JK |
129 | static void pcm_debug_name(struct snd_pcm_substream *substream, |
130 | char *name, size_t len) | |
131 | { | |
132 | snprintf(name, len, "pcmC%dD%d%c:%d", | |
133 | substream->pcm->card->number, | |
134 | substream->pcm->device, | |
135 | substream->stream ? 'c' : 'p', | |
136 | substream->number); | |
137 | } | |
138 | ||
741b20cf JK |
139 | #define XRUN_DEBUG_BASIC (1<<0) |
140 | #define XRUN_DEBUG_STACK (1<<1) /* dump also stack */ | |
141 | #define XRUN_DEBUG_JIFFIESCHECK (1<<2) /* do jiffies check */ | |
142 | #define XRUN_DEBUG_PERIODUPDATE (1<<3) /* full period update info */ | |
143 | #define XRUN_DEBUG_HWPTRUPDATE (1<<4) /* full hwptr update info */ | |
4d96eb25 JK |
144 | #define XRUN_DEBUG_LOG (1<<5) /* show last 10 positions on err */ |
145 | #define XRUN_DEBUG_LOGONCE (1<<6) /* do above only once */ | |
741b20cf | 146 | |
ed3da3d9 | 147 | #ifdef CONFIG_SND_PCM_XRUN_DEBUG |
4d96eb25 | 148 | |
741b20cf JK |
149 | #define xrun_debug(substream, mask) \ |
150 | ((substream)->pstr->xrun_debug & (mask)) | |
ed3da3d9 | 151 | |
741b20cf JK |
152 | #define dump_stack_on_xrun(substream) do { \ |
153 | if (xrun_debug(substream, XRUN_DEBUG_STACK)) \ | |
154 | dump_stack(); \ | |
ed3da3d9 TI |
155 | } while (0) |
156 | ||
877211f5 | 157 | static void xrun(struct snd_pcm_substream *substream) |
1da177e4 | 158 | { |
13f040f9 JK |
159 | struct snd_pcm_runtime *runtime = substream->runtime; |
160 | ||
161 | if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) | |
162 | snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp); | |
1da177e4 | 163 | snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN); |
741b20cf | 164 | if (xrun_debug(substream, XRUN_DEBUG_BASIC)) { |
c0070110 TI |
165 | char name[16]; |
166 | pcm_debug_name(substream, name, sizeof(name)); | |
167 | snd_printd(KERN_DEBUG "XRUN: %s\n", name); | |
ed3da3d9 | 168 | dump_stack_on_xrun(substream); |
1da177e4 | 169 | } |
1da177e4 LT |
170 | } |
171 | ||
4d96eb25 JK |
172 | #define hw_ptr_error(substream, fmt, args...) \ |
173 | do { \ | |
174 | if (xrun_debug(substream, XRUN_DEBUG_BASIC)) { \ | |
175 | if (printk_ratelimit()) { \ | |
176 | snd_printd("PCM: " fmt, ##args); \ | |
177 | } \ | |
178 | dump_stack_on_xrun(substream); \ | |
179 | } \ | |
180 | } while (0) | |
181 | ||
182 | #define XRUN_LOG_CNT 10 | |
183 | ||
184 | struct hwptr_log_entry { | |
185 | unsigned long jiffies; | |
186 | snd_pcm_uframes_t pos; | |
187 | snd_pcm_uframes_t period_size; | |
188 | snd_pcm_uframes_t buffer_size; | |
189 | snd_pcm_uframes_t old_hw_ptr; | |
190 | snd_pcm_uframes_t hw_ptr_base; | |
191 | snd_pcm_uframes_t hw_ptr_interrupt; | |
192 | }; | |
193 | ||
194 | struct snd_pcm_hwptr_log { | |
195 | unsigned int idx; | |
196 | unsigned int hit: 1; | |
197 | struct hwptr_log_entry entries[XRUN_LOG_CNT]; | |
198 | }; | |
199 | ||
200 | static void xrun_log(struct snd_pcm_substream *substream, | |
201 | snd_pcm_uframes_t pos) | |
202 | { | |
203 | struct snd_pcm_runtime *runtime = substream->runtime; | |
204 | struct snd_pcm_hwptr_log *log = runtime->hwptr_log; | |
205 | struct hwptr_log_entry *entry; | |
206 | ||
207 | if (log == NULL) { | |
208 | log = kzalloc(sizeof(*log), GFP_ATOMIC); | |
209 | if (log == NULL) | |
210 | return; | |
211 | runtime->hwptr_log = log; | |
212 | } else { | |
213 | if (xrun_debug(substream, XRUN_DEBUG_LOGONCE) && log->hit) | |
214 | return; | |
215 | } | |
216 | entry = &log->entries[log->idx]; | |
217 | entry->jiffies = jiffies; | |
218 | entry->pos = pos; | |
219 | entry->period_size = runtime->period_size; | |
220 | entry->buffer_size = runtime->buffer_size;; | |
221 | entry->old_hw_ptr = runtime->status->hw_ptr; | |
222 | entry->hw_ptr_base = runtime->hw_ptr_base; | |
223 | entry->hw_ptr_interrupt = runtime->hw_ptr_interrupt;; | |
224 | log->idx = (log->idx + 1) % XRUN_LOG_CNT; | |
225 | } | |
226 | ||
227 | static void xrun_log_show(struct snd_pcm_substream *substream) | |
228 | { | |
229 | struct snd_pcm_hwptr_log *log = substream->runtime->hwptr_log; | |
230 | struct hwptr_log_entry *entry; | |
231 | char name[16]; | |
232 | unsigned int idx; | |
233 | int cnt; | |
234 | ||
235 | if (log == NULL) | |
236 | return; | |
237 | if (xrun_debug(substream, XRUN_DEBUG_LOGONCE) && log->hit) | |
238 | return; | |
239 | pcm_debug_name(substream, name, sizeof(name)); | |
240 | for (cnt = 0, idx = log->idx; cnt < XRUN_LOG_CNT; cnt++) { | |
241 | entry = &log->entries[idx]; | |
242 | if (entry->period_size == 0) | |
243 | break; | |
244 | snd_printd("hwptr log: %s: j=%lu, pos=0x%lx/0x%lx/0x%lx, " | |
245 | "hwptr=0x%lx, hw_base=0x%lx, hw_intr=0x%lx\n", | |
246 | name, entry->jiffies, (unsigned long)entry->pos, | |
247 | (unsigned long)entry->period_size, | |
248 | (unsigned long)entry->buffer_size, | |
249 | (unsigned long)entry->old_hw_ptr, | |
250 | (unsigned long)entry->hw_ptr_base, | |
251 | (unsigned long)entry->hw_ptr_interrupt); | |
252 | idx++; | |
253 | idx %= XRUN_LOG_CNT; | |
254 | } | |
255 | log->hit = 1; | |
256 | } | |
257 | ||
258 | #else /* ! CONFIG_SND_PCM_XRUN_DEBUG */ | |
259 | ||
260 | #define xrun_debug(substream, mask) 0 | |
261 | #define xrun(substream) do { } while (0) | |
262 | #define hw_ptr_error(substream, fmt, args...) do { } while (0) | |
263 | #define xrun_log(substream, pos) do { } while (0) | |
264 | #define xrun_log_show(substream) do { } while (0) | |
265 | ||
266 | #endif | |
267 | ||
98204646 TI |
268 | static snd_pcm_uframes_t |
269 | snd_pcm_update_hw_ptr_pos(struct snd_pcm_substream *substream, | |
270 | struct snd_pcm_runtime *runtime) | |
1da177e4 LT |
271 | { |
272 | snd_pcm_uframes_t pos; | |
273 | ||
274 | pos = substream->ops->pointer(substream); | |
275 | if (pos == SNDRV_PCM_POS_XRUN) | |
276 | return pos; /* XRUN */ | |
1da177e4 | 277 | if (pos >= runtime->buffer_size) { |
5f513e11 | 278 | if (printk_ratelimit()) { |
c0070110 TI |
279 | char name[16]; |
280 | pcm_debug_name(substream, name, sizeof(name)); | |
4d96eb25 | 281 | xrun_log_show(substream); |
c0070110 | 282 | snd_printd(KERN_ERR "BUG: %s, pos = 0x%lx, " |
5f513e11 | 283 | "buffer size = 0x%lx, period size = 0x%lx\n", |
c0070110 | 284 | name, pos, runtime->buffer_size, |
5f513e11 TI |
285 | runtime->period_size); |
286 | } | |
287 | pos = 0; | |
7c22f1aa | 288 | } |
1da177e4 | 289 | pos -= pos % runtime->min_align; |
4d96eb25 JK |
290 | if (xrun_debug(substream, XRUN_DEBUG_LOG)) |
291 | xrun_log(substream, pos); | |
1da177e4 LT |
292 | return pos; |
293 | } | |
294 | ||
98204646 TI |
295 | static int snd_pcm_update_hw_ptr_post(struct snd_pcm_substream *substream, |
296 | struct snd_pcm_runtime *runtime) | |
1da177e4 LT |
297 | { |
298 | snd_pcm_uframes_t avail; | |
299 | ||
300 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) | |
301 | avail = snd_pcm_playback_avail(runtime); | |
302 | else | |
303 | avail = snd_pcm_capture_avail(runtime); | |
304 | if (avail > runtime->avail_max) | |
305 | runtime->avail_max = avail; | |
4cdc115f TI |
306 | if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) { |
307 | if (avail >= runtime->buffer_size) { | |
1da177e4 | 308 | snd_pcm_drain_done(substream); |
4cdc115f TI |
309 | return -EPIPE; |
310 | } | |
311 | } else { | |
312 | if (avail >= runtime->stop_threshold) { | |
1da177e4 | 313 | xrun(substream); |
4cdc115f TI |
314 | return -EPIPE; |
315 | } | |
1da177e4 LT |
316 | } |
317 | if (avail >= runtime->control->avail_min) | |
318 | wake_up(&runtime->sleep); | |
319 | return 0; | |
320 | } | |
321 | ||
98204646 | 322 | static int snd_pcm_update_hw_ptr_interrupt(struct snd_pcm_substream *substream) |
1da177e4 | 323 | { |
877211f5 | 324 | struct snd_pcm_runtime *runtime = substream->runtime; |
1da177e4 | 325 | snd_pcm_uframes_t pos; |
bbf6ad13 JK |
326 | snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_ptr_interrupt, hw_base; |
327 | snd_pcm_sframes_t hdelta, delta; | |
328 | unsigned long jdelta; | |
1da177e4 | 329 | |
bbf6ad13 | 330 | old_hw_ptr = runtime->status->hw_ptr; |
1da177e4 LT |
331 | pos = snd_pcm_update_hw_ptr_pos(substream, runtime); |
332 | if (pos == SNDRV_PCM_POS_XRUN) { | |
333 | xrun(substream); | |
334 | return -EPIPE; | |
335 | } | |
741b20cf | 336 | if (xrun_debug(substream, XRUN_DEBUG_PERIODUPDATE)) { |
cedb8118 TI |
337 | char name[16]; |
338 | pcm_debug_name(substream, name, sizeof(name)); | |
339 | snd_printd("period_update: %s: pos=0x%x/0x%x/0x%x, " | |
340 | "hwptr=0x%lx, hw_base=0x%lx, hw_intr=0x%lx\n", | |
89350640 TI |
341 | name, (unsigned int)pos, |
342 | (unsigned int)runtime->period_size, | |
343 | (unsigned int)runtime->buffer_size, | |
344 | (unsigned long)old_hw_ptr, | |
345 | (unsigned long)runtime->hw_ptr_base, | |
346 | (unsigned long)runtime->hw_ptr_interrupt); | |
cedb8118 | 347 | } |
ed3da3d9 TI |
348 | hw_base = runtime->hw_ptr_base; |
349 | new_hw_ptr = hw_base + pos; | |
1da177e4 | 350 | hw_ptr_interrupt = runtime->hw_ptr_interrupt + runtime->period_size; |
ed3da3d9 | 351 | delta = new_hw_ptr - hw_ptr_interrupt; |
ded652f7 | 352 | if (hw_ptr_interrupt >= runtime->boundary) { |
8b22d943 TI |
353 | hw_ptr_interrupt -= runtime->boundary; |
354 | if (hw_base < runtime->boundary / 2) | |
355 | /* hw_base was already lapped; recalc delta */ | |
ded652f7 TI |
356 | delta = new_hw_ptr - hw_ptr_interrupt; |
357 | } | |
ed3da3d9 | 358 | if (delta < 0) { |
947ca210 | 359 | if (runtime->periods == 1 || new_hw_ptr < old_hw_ptr) |
79452f0a | 360 | delta += runtime->buffer_size; |
ed3da3d9 | 361 | if (delta < 0) { |
4d96eb25 | 362 | xrun_log_show(substream); |
ed3da3d9 TI |
363 | hw_ptr_error(substream, |
364 | "Unexpected hw_pointer value " | |
365 | "(stream=%i, pos=%ld, intr_ptr=%ld)\n", | |
366 | substream->stream, (long)pos, | |
367 | (long)hw_ptr_interrupt); | |
79452f0a TI |
368 | #if 1 |
369 | /* simply skipping the hwptr update seems more | |
370 | * robust in some cases, e.g. on VMware with | |
371 | * inaccurate timer source | |
372 | */ | |
373 | return 0; /* skip this update */ | |
374 | #else | |
ed3da3d9 TI |
375 | /* rebase to interrupt position */ |
376 | hw_base = new_hw_ptr = hw_ptr_interrupt; | |
ded652f7 TI |
377 | /* align hw_base to buffer_size */ |
378 | hw_base -= hw_base % runtime->buffer_size; | |
ed3da3d9 | 379 | delta = 0; |
79452f0a | 380 | #endif |
ed3da3d9 TI |
381 | } else { |
382 | hw_base += runtime->buffer_size; | |
8b22d943 | 383 | if (hw_base >= runtime->boundary) |
ed3da3d9 TI |
384 | hw_base = 0; |
385 | new_hw_ptr = hw_base + pos; | |
1da177e4 | 386 | } |
1da177e4 | 387 | } |
c87d9732 TI |
388 | |
389 | /* Do jiffies check only in xrun_debug mode */ | |
741b20cf | 390 | if (!xrun_debug(substream, XRUN_DEBUG_JIFFIESCHECK)) |
c87d9732 TI |
391 | goto no_jiffies_check; |
392 | ||
3e5b5016 TI |
393 | /* Skip the jiffies check for hardwares with BATCH flag. |
394 | * Such hardware usually just increases the position at each IRQ, | |
395 | * thus it can't give any strange position. | |
396 | */ | |
397 | if (runtime->hw.info & SNDRV_PCM_INFO_BATCH) | |
398 | goto no_jiffies_check; | |
bbf6ad13 | 399 | hdelta = new_hw_ptr - old_hw_ptr; |
a4444da3 JK |
400 | if (hdelta < runtime->delay) |
401 | goto no_jiffies_check; | |
402 | hdelta -= runtime->delay; | |
bbf6ad13 JK |
403 | jdelta = jiffies - runtime->hw_ptr_jiffies; |
404 | if (((hdelta * HZ) / runtime->rate) > jdelta + HZ/100) { | |
405 | delta = jdelta / | |
406 | (((runtime->period_size * HZ) / runtime->rate) | |
407 | + HZ/100); | |
4d96eb25 | 408 | xrun_log_show(substream); |
bbf6ad13 JK |
409 | hw_ptr_error(substream, |
410 | "hw_ptr skipping! [Q] " | |
411 | "(pos=%ld, delta=%ld, period=%ld, " | |
412 | "jdelta=%lu/%lu/%lu)\n", | |
413 | (long)pos, (long)hdelta, | |
414 | (long)runtime->period_size, jdelta, | |
415 | ((hdelta * HZ) / runtime->rate), delta); | |
416 | hw_ptr_interrupt = runtime->hw_ptr_interrupt + | |
417 | runtime->period_size * delta; | |
418 | if (hw_ptr_interrupt >= runtime->boundary) | |
419 | hw_ptr_interrupt -= runtime->boundary; | |
420 | /* rebase to interrupt position */ | |
421 | hw_base = new_hw_ptr = hw_ptr_interrupt; | |
422 | /* align hw_base to buffer_size */ | |
423 | hw_base -= hw_base % runtime->buffer_size; | |
424 | delta = 0; | |
425 | } | |
3e5b5016 | 426 | no_jiffies_check: |
bbf6ad13 | 427 | if (delta > runtime->period_size + runtime->period_size / 2) { |
4d96eb25 | 428 | xrun_log_show(substream); |
ed3da3d9 TI |
429 | hw_ptr_error(substream, |
430 | "Lost interrupts? " | |
431 | "(stream=%i, delta=%ld, intr_ptr=%ld)\n", | |
432 | substream->stream, (long)delta, | |
433 | (long)hw_ptr_interrupt); | |
434 | /* rebase hw_ptr_interrupt */ | |
435 | hw_ptr_interrupt = | |
436 | new_hw_ptr - new_hw_ptr % runtime->period_size; | |
437 | } | |
ab1863fc TI |
438 | runtime->hw_ptr_interrupt = hw_ptr_interrupt; |
439 | ||
1da177e4 LT |
440 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && |
441 | runtime->silence_size > 0) | |
442 | snd_pcm_playback_silence(substream, new_hw_ptr); | |
443 | ||
13f040f9 JK |
444 | if (runtime->status->hw_ptr == new_hw_ptr) |
445 | return 0; | |
446 | ||
ed3da3d9 | 447 | runtime->hw_ptr_base = hw_base; |
1da177e4 | 448 | runtime->status->hw_ptr = new_hw_ptr; |
bbf6ad13 | 449 | runtime->hw_ptr_jiffies = jiffies; |
13f040f9 JK |
450 | if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) |
451 | snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp); | |
1da177e4 LT |
452 | |
453 | return snd_pcm_update_hw_ptr_post(substream, runtime); | |
454 | } | |
455 | ||
456 | /* CAUTION: call it with irq disabled */ | |
877211f5 | 457 | int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream) |
1da177e4 | 458 | { |
877211f5 | 459 | struct snd_pcm_runtime *runtime = substream->runtime; |
1da177e4 | 460 | snd_pcm_uframes_t pos; |
ed3da3d9 | 461 | snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base; |
1da177e4 | 462 | snd_pcm_sframes_t delta; |
bbf6ad13 | 463 | unsigned long jdelta; |
1da177e4 LT |
464 | |
465 | old_hw_ptr = runtime->status->hw_ptr; | |
466 | pos = snd_pcm_update_hw_ptr_pos(substream, runtime); | |
467 | if (pos == SNDRV_PCM_POS_XRUN) { | |
468 | xrun(substream); | |
469 | return -EPIPE; | |
470 | } | |
741b20cf | 471 | if (xrun_debug(substream, XRUN_DEBUG_HWPTRUPDATE)) { |
cedb8118 TI |
472 | char name[16]; |
473 | pcm_debug_name(substream, name, sizeof(name)); | |
474 | snd_printd("hw_update: %s: pos=0x%x/0x%x/0x%x, " | |
475 | "hwptr=0x%lx, hw_base=0x%lx, hw_intr=0x%lx\n", | |
89350640 TI |
476 | name, (unsigned int)pos, |
477 | (unsigned int)runtime->period_size, | |
478 | (unsigned int)runtime->buffer_size, | |
479 | (unsigned long)old_hw_ptr, | |
480 | (unsigned long)runtime->hw_ptr_base, | |
481 | (unsigned long)runtime->hw_ptr_interrupt); | |
cedb8118 TI |
482 | } |
483 | ||
ed3da3d9 TI |
484 | hw_base = runtime->hw_ptr_base; |
485 | new_hw_ptr = hw_base + pos; | |
1da177e4 | 486 | |
ed3da3d9 | 487 | delta = new_hw_ptr - old_hw_ptr; |
bbf6ad13 | 488 | jdelta = jiffies - runtime->hw_ptr_jiffies; |
ed3da3d9 TI |
489 | if (delta < 0) { |
490 | delta += runtime->buffer_size; | |
491 | if (delta < 0) { | |
4d96eb25 | 492 | xrun_log_show(substream); |
ed3da3d9 TI |
493 | hw_ptr_error(substream, |
494 | "Unexpected hw_pointer value [2] " | |
bbf6ad13 | 495 | "(stream=%i, pos=%ld, old_ptr=%ld, jdelta=%li)\n", |
ed3da3d9 | 496 | substream->stream, (long)pos, |
bbf6ad13 | 497 | (long)old_hw_ptr, jdelta); |
1da177e4 LT |
498 | return 0; |
499 | } | |
ed3da3d9 | 500 | hw_base += runtime->buffer_size; |
8b22d943 | 501 | if (hw_base >= runtime->boundary) |
ed3da3d9 TI |
502 | hw_base = 0; |
503 | new_hw_ptr = hw_base + pos; | |
504 | } | |
c87d9732 | 505 | /* Do jiffies check only in xrun_debug mode */ |
741b20cf | 506 | if (!xrun_debug(substream, XRUN_DEBUG_JIFFIESCHECK)) |
a4444da3 JK |
507 | goto no_jiffies_check; |
508 | if (delta < runtime->delay) | |
509 | goto no_jiffies_check; | |
510 | delta -= runtime->delay; | |
511 | if (((delta * HZ) / runtime->rate) > jdelta + HZ/100) { | |
4d96eb25 | 512 | xrun_log_show(substream); |
ed3da3d9 TI |
513 | hw_ptr_error(substream, |
514 | "hw_ptr skipping! " | |
bbf6ad13 | 515 | "(pos=%ld, delta=%ld, period=%ld, jdelta=%lu/%lu)\n", |
ed3da3d9 | 516 | (long)pos, (long)delta, |
bbf6ad13 JK |
517 | (long)runtime->period_size, jdelta, |
518 | ((delta * HZ) / runtime->rate)); | |
ed3da3d9 | 519 | return 0; |
1da177e4 | 520 | } |
a4444da3 | 521 | no_jiffies_check: |
1da177e4 LT |
522 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && |
523 | runtime->silence_size > 0) | |
524 | snd_pcm_playback_silence(substream, new_hw_ptr); | |
525 | ||
d86bf923 | 526 | if (runtime->status->hw_ptr == new_hw_ptr) |
13f040f9 JK |
527 | return 0; |
528 | ||
ed3da3d9 | 529 | runtime->hw_ptr_base = hw_base; |
1da177e4 | 530 | runtime->status->hw_ptr = new_hw_ptr; |
bbf6ad13 | 531 | runtime->hw_ptr_jiffies = jiffies; |
13f040f9 JK |
532 | if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) |
533 | snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp); | |
1da177e4 LT |
534 | |
535 | return snd_pcm_update_hw_ptr_post(substream, runtime); | |
536 | } | |
537 | ||
538 | /** | |
539 | * snd_pcm_set_ops - set the PCM operators | |
540 | * @pcm: the pcm instance | |
541 | * @direction: stream direction, SNDRV_PCM_STREAM_XXX | |
542 | * @ops: the operator table | |
543 | * | |
544 | * Sets the given PCM operators to the pcm instance. | |
545 | */ | |
877211f5 | 546 | void snd_pcm_set_ops(struct snd_pcm *pcm, int direction, struct snd_pcm_ops *ops) |
1da177e4 | 547 | { |
877211f5 TI |
548 | struct snd_pcm_str *stream = &pcm->streams[direction]; |
549 | struct snd_pcm_substream *substream; | |
1da177e4 LT |
550 | |
551 | for (substream = stream->substream; substream != NULL; substream = substream->next) | |
552 | substream->ops = ops; | |
553 | } | |
554 | ||
e88e8ae6 | 555 | EXPORT_SYMBOL(snd_pcm_set_ops); |
1da177e4 LT |
556 | |
557 | /** | |
558 | * snd_pcm_sync - set the PCM sync id | |
559 | * @substream: the pcm substream | |
560 | * | |
561 | * Sets the PCM sync identifier for the card. | |
562 | */ | |
877211f5 | 563 | void snd_pcm_set_sync(struct snd_pcm_substream *substream) |
1da177e4 | 564 | { |
877211f5 | 565 | struct snd_pcm_runtime *runtime = substream->runtime; |
1da177e4 LT |
566 | |
567 | runtime->sync.id32[0] = substream->pcm->card->number; | |
568 | runtime->sync.id32[1] = -1; | |
569 | runtime->sync.id32[2] = -1; | |
570 | runtime->sync.id32[3] = -1; | |
571 | } | |
572 | ||
e88e8ae6 TI |
573 | EXPORT_SYMBOL(snd_pcm_set_sync); |
574 | ||
1da177e4 LT |
575 | /* |
576 | * Standard ioctl routine | |
577 | */ | |
578 | ||
1da177e4 LT |
579 | static inline unsigned int div32(unsigned int a, unsigned int b, |
580 | unsigned int *r) | |
581 | { | |
582 | if (b == 0) { | |
583 | *r = 0; | |
584 | return UINT_MAX; | |
585 | } | |
586 | *r = a % b; | |
587 | return a / b; | |
588 | } | |
589 | ||
590 | static inline unsigned int div_down(unsigned int a, unsigned int b) | |
591 | { | |
592 | if (b == 0) | |
593 | return UINT_MAX; | |
594 | return a / b; | |
595 | } | |
596 | ||
597 | static inline unsigned int div_up(unsigned int a, unsigned int b) | |
598 | { | |
599 | unsigned int r; | |
600 | unsigned int q; | |
601 | if (b == 0) | |
602 | return UINT_MAX; | |
603 | q = div32(a, b, &r); | |
604 | if (r) | |
605 | ++q; | |
606 | return q; | |
607 | } | |
608 | ||
609 | static inline unsigned int mul(unsigned int a, unsigned int b) | |
610 | { | |
611 | if (a == 0) | |
612 | return 0; | |
613 | if (div_down(UINT_MAX, a) < b) | |
614 | return UINT_MAX; | |
615 | return a * b; | |
616 | } | |
617 | ||
618 | static inline unsigned int muldiv32(unsigned int a, unsigned int b, | |
619 | unsigned int c, unsigned int *r) | |
620 | { | |
621 | u_int64_t n = (u_int64_t) a * b; | |
622 | if (c == 0) { | |
7eaa943c | 623 | snd_BUG_ON(!n); |
1da177e4 LT |
624 | *r = 0; |
625 | return UINT_MAX; | |
626 | } | |
3f7440a6 | 627 | n = div_u64_rem(n, c, r); |
1da177e4 LT |
628 | if (n >= UINT_MAX) { |
629 | *r = 0; | |
630 | return UINT_MAX; | |
631 | } | |
632 | return n; | |
633 | } | |
634 | ||
1da177e4 LT |
635 | /** |
636 | * snd_interval_refine - refine the interval value of configurator | |
637 | * @i: the interval value to refine | |
638 | * @v: the interval value to refer to | |
639 | * | |
640 | * Refines the interval value with the reference value. | |
641 | * The interval is changed to the range satisfying both intervals. | |
642 | * The interval status (min, max, integer, etc.) are evaluated. | |
643 | * | |
644 | * Returns non-zero if the value is changed, zero if not changed. | |
645 | */ | |
877211f5 | 646 | int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v) |
1da177e4 LT |
647 | { |
648 | int changed = 0; | |
7eaa943c TI |
649 | if (snd_BUG_ON(snd_interval_empty(i))) |
650 | return -EINVAL; | |
1da177e4 LT |
651 | if (i->min < v->min) { |
652 | i->min = v->min; | |
653 | i->openmin = v->openmin; | |
654 | changed = 1; | |
655 | } else if (i->min == v->min && !i->openmin && v->openmin) { | |
656 | i->openmin = 1; | |
657 | changed = 1; | |
658 | } | |
659 | if (i->max > v->max) { | |
660 | i->max = v->max; | |
661 | i->openmax = v->openmax; | |
662 | changed = 1; | |
663 | } else if (i->max == v->max && !i->openmax && v->openmax) { | |
664 | i->openmax = 1; | |
665 | changed = 1; | |
666 | } | |
667 | if (!i->integer && v->integer) { | |
668 | i->integer = 1; | |
669 | changed = 1; | |
670 | } | |
671 | if (i->integer) { | |
672 | if (i->openmin) { | |
673 | i->min++; | |
674 | i->openmin = 0; | |
675 | } | |
676 | if (i->openmax) { | |
677 | i->max--; | |
678 | i->openmax = 0; | |
679 | } | |
680 | } else if (!i->openmin && !i->openmax && i->min == i->max) | |
681 | i->integer = 1; | |
682 | if (snd_interval_checkempty(i)) { | |
683 | snd_interval_none(i); | |
684 | return -EINVAL; | |
685 | } | |
686 | return changed; | |
687 | } | |
688 | ||
e88e8ae6 TI |
689 | EXPORT_SYMBOL(snd_interval_refine); |
690 | ||
877211f5 | 691 | static int snd_interval_refine_first(struct snd_interval *i) |
1da177e4 | 692 | { |
7eaa943c TI |
693 | if (snd_BUG_ON(snd_interval_empty(i))) |
694 | return -EINVAL; | |
1da177e4 LT |
695 | if (snd_interval_single(i)) |
696 | return 0; | |
697 | i->max = i->min; | |
698 | i->openmax = i->openmin; | |
699 | if (i->openmax) | |
700 | i->max++; | |
701 | return 1; | |
702 | } | |
703 | ||
877211f5 | 704 | static int snd_interval_refine_last(struct snd_interval *i) |
1da177e4 | 705 | { |
7eaa943c TI |
706 | if (snd_BUG_ON(snd_interval_empty(i))) |
707 | return -EINVAL; | |
1da177e4 LT |
708 | if (snd_interval_single(i)) |
709 | return 0; | |
710 | i->min = i->max; | |
711 | i->openmin = i->openmax; | |
712 | if (i->openmin) | |
713 | i->min--; | |
714 | return 1; | |
715 | } | |
716 | ||
877211f5 | 717 | void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c) |
1da177e4 LT |
718 | { |
719 | if (a->empty || b->empty) { | |
720 | snd_interval_none(c); | |
721 | return; | |
722 | } | |
723 | c->empty = 0; | |
724 | c->min = mul(a->min, b->min); | |
725 | c->openmin = (a->openmin || b->openmin); | |
726 | c->max = mul(a->max, b->max); | |
727 | c->openmax = (a->openmax || b->openmax); | |
728 | c->integer = (a->integer && b->integer); | |
729 | } | |
730 | ||
731 | /** | |
732 | * snd_interval_div - refine the interval value with division | |
df8db936 TI |
733 | * @a: dividend |
734 | * @b: divisor | |
735 | * @c: quotient | |
1da177e4 LT |
736 | * |
737 | * c = a / b | |
738 | * | |
739 | * Returns non-zero if the value is changed, zero if not changed. | |
740 | */ | |
877211f5 | 741 | void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c) |
1da177e4 LT |
742 | { |
743 | unsigned int r; | |
744 | if (a->empty || b->empty) { | |
745 | snd_interval_none(c); | |
746 | return; | |
747 | } | |
748 | c->empty = 0; | |
749 | c->min = div32(a->min, b->max, &r); | |
750 | c->openmin = (r || a->openmin || b->openmax); | |
751 | if (b->min > 0) { | |
752 | c->max = div32(a->max, b->min, &r); | |
753 | if (r) { | |
754 | c->max++; | |
755 | c->openmax = 1; | |
756 | } else | |
757 | c->openmax = (a->openmax || b->openmin); | |
758 | } else { | |
759 | c->max = UINT_MAX; | |
760 | c->openmax = 0; | |
761 | } | |
762 | c->integer = 0; | |
763 | } | |
764 | ||
765 | /** | |
766 | * snd_interval_muldivk - refine the interval value | |
df8db936 TI |
767 | * @a: dividend 1 |
768 | * @b: dividend 2 | |
769 | * @k: divisor (as integer) | |
770 | * @c: result | |
771 | * | |
1da177e4 LT |
772 | * c = a * b / k |
773 | * | |
774 | * Returns non-zero if the value is changed, zero if not changed. | |
775 | */ | |
877211f5 TI |
776 | void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b, |
777 | unsigned int k, struct snd_interval *c) | |
1da177e4 LT |
778 | { |
779 | unsigned int r; | |
780 | if (a->empty || b->empty) { | |
781 | snd_interval_none(c); | |
782 | return; | |
783 | } | |
784 | c->empty = 0; | |
785 | c->min = muldiv32(a->min, b->min, k, &r); | |
786 | c->openmin = (r || a->openmin || b->openmin); | |
787 | c->max = muldiv32(a->max, b->max, k, &r); | |
788 | if (r) { | |
789 | c->max++; | |
790 | c->openmax = 1; | |
791 | } else | |
792 | c->openmax = (a->openmax || b->openmax); | |
793 | c->integer = 0; | |
794 | } | |
795 | ||
796 | /** | |
797 | * snd_interval_mulkdiv - refine the interval value | |
df8db936 TI |
798 | * @a: dividend 1 |
799 | * @k: dividend 2 (as integer) | |
800 | * @b: divisor | |
801 | * @c: result | |
1da177e4 LT |
802 | * |
803 | * c = a * k / b | |
804 | * | |
805 | * Returns non-zero if the value is changed, zero if not changed. | |
806 | */ | |
877211f5 TI |
807 | void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k, |
808 | const struct snd_interval *b, struct snd_interval *c) | |
1da177e4 LT |
809 | { |
810 | unsigned int r; | |
811 | if (a->empty || b->empty) { | |
812 | snd_interval_none(c); | |
813 | return; | |
814 | } | |
815 | c->empty = 0; | |
816 | c->min = muldiv32(a->min, k, b->max, &r); | |
817 | c->openmin = (r || a->openmin || b->openmax); | |
818 | if (b->min > 0) { | |
819 | c->max = muldiv32(a->max, k, b->min, &r); | |
820 | if (r) { | |
821 | c->max++; | |
822 | c->openmax = 1; | |
823 | } else | |
824 | c->openmax = (a->openmax || b->openmin); | |
825 | } else { | |
826 | c->max = UINT_MAX; | |
827 | c->openmax = 0; | |
828 | } | |
829 | c->integer = 0; | |
830 | } | |
831 | ||
1da177e4 LT |
832 | /* ---- */ |
833 | ||
834 | ||
835 | /** | |
836 | * snd_interval_ratnum - refine the interval value | |
df8db936 TI |
837 | * @i: interval to refine |
838 | * @rats_count: number of ratnum_t | |
839 | * @rats: ratnum_t array | |
840 | * @nump: pointer to store the resultant numerator | |
841 | * @denp: pointer to store the resultant denominator | |
1da177e4 LT |
842 | * |
843 | * Returns non-zero if the value is changed, zero if not changed. | |
844 | */ | |
877211f5 TI |
845 | int snd_interval_ratnum(struct snd_interval *i, |
846 | unsigned int rats_count, struct snd_ratnum *rats, | |
847 | unsigned int *nump, unsigned int *denp) | |
1da177e4 LT |
848 | { |
849 | unsigned int best_num, best_diff, best_den; | |
850 | unsigned int k; | |
877211f5 | 851 | struct snd_interval t; |
1da177e4 LT |
852 | int err; |
853 | ||
854 | best_num = best_den = best_diff = 0; | |
855 | for (k = 0; k < rats_count; ++k) { | |
856 | unsigned int num = rats[k].num; | |
857 | unsigned int den; | |
858 | unsigned int q = i->min; | |
859 | int diff; | |
860 | if (q == 0) | |
861 | q = 1; | |
862 | den = div_down(num, q); | |
863 | if (den < rats[k].den_min) | |
864 | continue; | |
865 | if (den > rats[k].den_max) | |
866 | den = rats[k].den_max; | |
867 | else { | |
868 | unsigned int r; | |
869 | r = (den - rats[k].den_min) % rats[k].den_step; | |
870 | if (r != 0) | |
871 | den -= r; | |
872 | } | |
873 | diff = num - q * den; | |
874 | if (best_num == 0 || | |
875 | diff * best_den < best_diff * den) { | |
876 | best_diff = diff; | |
877 | best_den = den; | |
878 | best_num = num; | |
879 | } | |
880 | } | |
881 | if (best_den == 0) { | |
882 | i->empty = 1; | |
883 | return -EINVAL; | |
884 | } | |
885 | t.min = div_down(best_num, best_den); | |
886 | t.openmin = !!(best_num % best_den); | |
887 | ||
888 | best_num = best_den = best_diff = 0; | |
889 | for (k = 0; k < rats_count; ++k) { | |
890 | unsigned int num = rats[k].num; | |
891 | unsigned int den; | |
892 | unsigned int q = i->max; | |
893 | int diff; | |
894 | if (q == 0) { | |
895 | i->empty = 1; | |
896 | return -EINVAL; | |
897 | } | |
898 | den = div_up(num, q); | |
899 | if (den > rats[k].den_max) | |
900 | continue; | |
901 | if (den < rats[k].den_min) | |
902 | den = rats[k].den_min; | |
903 | else { | |
904 | unsigned int r; | |
905 | r = (den - rats[k].den_min) % rats[k].den_step; | |
906 | if (r != 0) | |
907 | den += rats[k].den_step - r; | |
908 | } | |
909 | diff = q * den - num; | |
910 | if (best_num == 0 || | |
911 | diff * best_den < best_diff * den) { | |
912 | best_diff = diff; | |
913 | best_den = den; | |
914 | best_num = num; | |
915 | } | |
916 | } | |
917 | if (best_den == 0) { | |
918 | i->empty = 1; | |
919 | return -EINVAL; | |
920 | } | |
921 | t.max = div_up(best_num, best_den); | |
922 | t.openmax = !!(best_num % best_den); | |
923 | t.integer = 0; | |
924 | err = snd_interval_refine(i, &t); | |
925 | if (err < 0) | |
926 | return err; | |
927 | ||
928 | if (snd_interval_single(i)) { | |
929 | if (nump) | |
930 | *nump = best_num; | |
931 | if (denp) | |
932 | *denp = best_den; | |
933 | } | |
934 | return err; | |
935 | } | |
936 | ||
e88e8ae6 TI |
937 | EXPORT_SYMBOL(snd_interval_ratnum); |
938 | ||
1da177e4 LT |
939 | /** |
940 | * snd_interval_ratden - refine the interval value | |
df8db936 | 941 | * @i: interval to refine |
877211f5 TI |
942 | * @rats_count: number of struct ratden |
943 | * @rats: struct ratden array | |
df8db936 TI |
944 | * @nump: pointer to store the resultant numerator |
945 | * @denp: pointer to store the resultant denominator | |
1da177e4 LT |
946 | * |
947 | * Returns non-zero if the value is changed, zero if not changed. | |
948 | */ | |
877211f5 TI |
949 | static int snd_interval_ratden(struct snd_interval *i, |
950 | unsigned int rats_count, struct snd_ratden *rats, | |
1da177e4 LT |
951 | unsigned int *nump, unsigned int *denp) |
952 | { | |
953 | unsigned int best_num, best_diff, best_den; | |
954 | unsigned int k; | |
877211f5 | 955 | struct snd_interval t; |
1da177e4 LT |
956 | int err; |
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->min; | |
963 | int diff; | |
964 | num = mul(q, den); | |
965 | if (num > rats[k].num_max) | |
966 | continue; | |
967 | if (num < rats[k].num_min) | |
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 += rats[k].num_step - r; | |
974 | } | |
975 | diff = num - q * den; | |
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.min = div_down(best_num, best_den); | |
988 | t.openmin = !!(best_num % best_den); | |
989 | ||
990 | best_num = best_den = best_diff = 0; | |
991 | for (k = 0; k < rats_count; ++k) { | |
992 | unsigned int num; | |
993 | unsigned int den = rats[k].den; | |
994 | unsigned int q = i->max; | |
995 | int diff; | |
996 | num = mul(q, den); | |
997 | if (num < rats[k].num_min) | |
998 | continue; | |
999 | if (num > rats[k].num_max) | |
1000 | num = rats[k].num_max; | |
1001 | else { | |
1002 | unsigned int r; | |
1003 | r = (num - rats[k].num_min) % rats[k].num_step; | |
1004 | if (r != 0) | |
1005 | num -= r; | |
1006 | } | |
1007 | diff = q * den - num; | |
1008 | if (best_num == 0 || | |
1009 | diff * best_den < best_diff * den) { | |
1010 | best_diff = diff; | |
1011 | best_den = den; | |
1012 | best_num = num; | |
1013 | } | |
1014 | } | |
1015 | if (best_den == 0) { | |
1016 | i->empty = 1; | |
1017 | return -EINVAL; | |
1018 | } | |
1019 | t.max = div_up(best_num, best_den); | |
1020 | t.openmax = !!(best_num % best_den); | |
1021 | t.integer = 0; | |
1022 | err = snd_interval_refine(i, &t); | |
1023 | if (err < 0) | |
1024 | return err; | |
1025 | ||
1026 | if (snd_interval_single(i)) { | |
1027 | if (nump) | |
1028 | *nump = best_num; | |
1029 | if (denp) | |
1030 | *denp = best_den; | |
1031 | } | |
1032 | return err; | |
1033 | } | |
1034 | ||
1035 | /** | |
1036 | * snd_interval_list - refine the interval value from the list | |
1037 | * @i: the interval value to refine | |
1038 | * @count: the number of elements in the list | |
1039 | * @list: the value list | |
1040 | * @mask: the bit-mask to evaluate | |
1041 | * | |
1042 | * Refines the interval value from the list. | |
1043 | * When mask is non-zero, only the elements corresponding to bit 1 are | |
1044 | * evaluated. | |
1045 | * | |
1046 | * Returns non-zero if the value is changed, zero if not changed. | |
1047 | */ | |
877211f5 | 1048 | int snd_interval_list(struct snd_interval *i, unsigned int count, unsigned int *list, unsigned int mask) |
1da177e4 LT |
1049 | { |
1050 | unsigned int k; | |
b1ddaf68 | 1051 | struct snd_interval list_range; |
0981a260 TI |
1052 | |
1053 | if (!count) { | |
1054 | i->empty = 1; | |
1055 | return -EINVAL; | |
1056 | } | |
b1ddaf68 CL |
1057 | snd_interval_any(&list_range); |
1058 | list_range.min = UINT_MAX; | |
1059 | list_range.max = 0; | |
1da177e4 LT |
1060 | for (k = 0; k < count; k++) { |
1061 | if (mask && !(mask & (1 << k))) | |
1062 | continue; | |
b1ddaf68 | 1063 | if (!snd_interval_test(i, list[k])) |
1da177e4 | 1064 | continue; |
b1ddaf68 CL |
1065 | list_range.min = min(list_range.min, list[k]); |
1066 | list_range.max = max(list_range.max, list[k]); | |
1da177e4 | 1067 | } |
b1ddaf68 | 1068 | return snd_interval_refine(i, &list_range); |
1da177e4 LT |
1069 | } |
1070 | ||
e88e8ae6 TI |
1071 | EXPORT_SYMBOL(snd_interval_list); |
1072 | ||
877211f5 | 1073 | static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step) |
1da177e4 LT |
1074 | { |
1075 | unsigned int n; | |
1076 | int changed = 0; | |
1077 | n = (i->min - min) % step; | |
1078 | if (n != 0 || i->openmin) { | |
1079 | i->min += step - n; | |
1080 | changed = 1; | |
1081 | } | |
1082 | n = (i->max - min) % step; | |
1083 | if (n != 0 || i->openmax) { | |
1084 | i->max -= n; | |
1085 | changed = 1; | |
1086 | } | |
1087 | if (snd_interval_checkempty(i)) { | |
1088 | i->empty = 1; | |
1089 | return -EINVAL; | |
1090 | } | |
1091 | return changed; | |
1092 | } | |
1093 | ||
1094 | /* Info constraints helpers */ | |
1095 | ||
1096 | /** | |
1097 | * snd_pcm_hw_rule_add - add the hw-constraint rule | |
1098 | * @runtime: the pcm runtime instance | |
1099 | * @cond: condition bits | |
1100 | * @var: the variable to evaluate | |
1101 | * @func: the evaluation function | |
1102 | * @private: the private data pointer passed to function | |
1103 | * @dep: the dependent variables | |
1104 | * | |
1105 | * Returns zero if successful, or a negative error code on failure. | |
1106 | */ | |
877211f5 | 1107 | int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond, |
1da177e4 LT |
1108 | int var, |
1109 | snd_pcm_hw_rule_func_t func, void *private, | |
1110 | int dep, ...) | |
1111 | { | |
877211f5 TI |
1112 | struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints; |
1113 | struct snd_pcm_hw_rule *c; | |
1da177e4 LT |
1114 | unsigned int k; |
1115 | va_list args; | |
1116 | va_start(args, dep); | |
1117 | if (constrs->rules_num >= constrs->rules_all) { | |
877211f5 | 1118 | struct snd_pcm_hw_rule *new; |
1da177e4 LT |
1119 | unsigned int new_rules = constrs->rules_all + 16; |
1120 | new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL); | |
1121 | if (!new) | |
1122 | return -ENOMEM; | |
1123 | if (constrs->rules) { | |
1124 | memcpy(new, constrs->rules, | |
1125 | constrs->rules_num * sizeof(*c)); | |
1126 | kfree(constrs->rules); | |
1127 | } | |
1128 | constrs->rules = new; | |
1129 | constrs->rules_all = new_rules; | |
1130 | } | |
1131 | c = &constrs->rules[constrs->rules_num]; | |
1132 | c->cond = cond; | |
1133 | c->func = func; | |
1134 | c->var = var; | |
1135 | c->private = private; | |
1136 | k = 0; | |
1137 | while (1) { | |
7eaa943c TI |
1138 | if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps))) |
1139 | return -EINVAL; | |
1da177e4 LT |
1140 | c->deps[k++] = dep; |
1141 | if (dep < 0) | |
1142 | break; | |
1143 | dep = va_arg(args, int); | |
1144 | } | |
1145 | constrs->rules_num++; | |
1146 | va_end(args); | |
1147 | return 0; | |
1148 | } | |
1149 | ||
e88e8ae6 TI |
1150 | EXPORT_SYMBOL(snd_pcm_hw_rule_add); |
1151 | ||
1da177e4 | 1152 | /** |
1c85cc64 | 1153 | * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint |
df8db936 TI |
1154 | * @runtime: PCM runtime instance |
1155 | * @var: hw_params variable to apply the mask | |
1156 | * @mask: the bitmap mask | |
1157 | * | |
1c85cc64 | 1158 | * Apply the constraint of the given bitmap mask to a 32-bit mask parameter. |
1da177e4 | 1159 | */ |
877211f5 | 1160 | int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var, |
1da177e4 LT |
1161 | u_int32_t mask) |
1162 | { | |
877211f5 TI |
1163 | struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints; |
1164 | struct snd_mask *maskp = constrs_mask(constrs, var); | |
1da177e4 LT |
1165 | *maskp->bits &= mask; |
1166 | memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */ | |
1167 | if (*maskp->bits == 0) | |
1168 | return -EINVAL; | |
1169 | return 0; | |
1170 | } | |
1171 | ||
1172 | /** | |
1c85cc64 | 1173 | * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint |
df8db936 TI |
1174 | * @runtime: PCM runtime instance |
1175 | * @var: hw_params variable to apply the mask | |
1176 | * @mask: the 64bit bitmap mask | |
1177 | * | |
1c85cc64 | 1178 | * Apply the constraint of the given bitmap mask to a 64-bit mask parameter. |
1da177e4 | 1179 | */ |
877211f5 | 1180 | int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var, |
1da177e4 LT |
1181 | u_int64_t mask) |
1182 | { | |
877211f5 TI |
1183 | struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints; |
1184 | struct snd_mask *maskp = constrs_mask(constrs, var); | |
1da177e4 LT |
1185 | maskp->bits[0] &= (u_int32_t)mask; |
1186 | maskp->bits[1] &= (u_int32_t)(mask >> 32); | |
1187 | memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */ | |
1188 | if (! maskp->bits[0] && ! maskp->bits[1]) | |
1189 | return -EINVAL; | |
1190 | return 0; | |
1191 | } | |
1192 | ||
1193 | /** | |
1c85cc64 | 1194 | * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval |
df8db936 TI |
1195 | * @runtime: PCM runtime instance |
1196 | * @var: hw_params variable to apply the integer constraint | |
1197 | * | |
1198 | * Apply the constraint of integer to an interval parameter. | |
1da177e4 | 1199 | */ |
877211f5 | 1200 | int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var) |
1da177e4 | 1201 | { |
877211f5 | 1202 | struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints; |
1da177e4 LT |
1203 | return snd_interval_setinteger(constrs_interval(constrs, var)); |
1204 | } | |
1205 | ||
e88e8ae6 TI |
1206 | EXPORT_SYMBOL(snd_pcm_hw_constraint_integer); |
1207 | ||
1da177e4 | 1208 | /** |
1c85cc64 | 1209 | * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval |
df8db936 TI |
1210 | * @runtime: PCM runtime instance |
1211 | * @var: hw_params variable to apply the range | |
1212 | * @min: the minimal value | |
1213 | * @max: the maximal value | |
1214 | * | |
1215 | * Apply the min/max range constraint to an interval parameter. | |
1da177e4 | 1216 | */ |
877211f5 | 1217 | int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var, |
1da177e4 LT |
1218 | unsigned int min, unsigned int max) |
1219 | { | |
877211f5 TI |
1220 | struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints; |
1221 | struct snd_interval t; | |
1da177e4 LT |
1222 | t.min = min; |
1223 | t.max = max; | |
1224 | t.openmin = t.openmax = 0; | |
1225 | t.integer = 0; | |
1226 | return snd_interval_refine(constrs_interval(constrs, var), &t); | |
1227 | } | |
1228 | ||
e88e8ae6 TI |
1229 | EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax); |
1230 | ||
877211f5 TI |
1231 | static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params, |
1232 | struct snd_pcm_hw_rule *rule) | |
1da177e4 | 1233 | { |
877211f5 | 1234 | struct snd_pcm_hw_constraint_list *list = rule->private; |
1da177e4 LT |
1235 | return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask); |
1236 | } | |
1237 | ||
1238 | ||
1239 | /** | |
1c85cc64 | 1240 | * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter |
df8db936 TI |
1241 | * @runtime: PCM runtime instance |
1242 | * @cond: condition bits | |
1243 | * @var: hw_params variable to apply the list constraint | |
1244 | * @l: list | |
1245 | * | |
1246 | * Apply the list of constraints to an interval parameter. | |
1da177e4 | 1247 | */ |
877211f5 | 1248 | int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime, |
1da177e4 LT |
1249 | unsigned int cond, |
1250 | snd_pcm_hw_param_t var, | |
877211f5 | 1251 | struct snd_pcm_hw_constraint_list *l) |
1da177e4 LT |
1252 | { |
1253 | return snd_pcm_hw_rule_add(runtime, cond, var, | |
1254 | snd_pcm_hw_rule_list, l, | |
1255 | var, -1); | |
1256 | } | |
1257 | ||
e88e8ae6 TI |
1258 | EXPORT_SYMBOL(snd_pcm_hw_constraint_list); |
1259 | ||
877211f5 TI |
1260 | static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params, |
1261 | struct snd_pcm_hw_rule *rule) | |
1da177e4 | 1262 | { |
877211f5 | 1263 | struct snd_pcm_hw_constraint_ratnums *r = rule->private; |
1da177e4 LT |
1264 | unsigned int num = 0, den = 0; |
1265 | int err; | |
1266 | err = snd_interval_ratnum(hw_param_interval(params, rule->var), | |
1267 | r->nrats, r->rats, &num, &den); | |
1268 | if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) { | |
1269 | params->rate_num = num; | |
1270 | params->rate_den = den; | |
1271 | } | |
1272 | return err; | |
1273 | } | |
1274 | ||
1275 | /** | |
1c85cc64 | 1276 | * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter |
df8db936 TI |
1277 | * @runtime: PCM runtime instance |
1278 | * @cond: condition bits | |
1279 | * @var: hw_params variable to apply the ratnums constraint | |
877211f5 | 1280 | * @r: struct snd_ratnums constriants |
1da177e4 | 1281 | */ |
877211f5 | 1282 | int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime, |
1da177e4 LT |
1283 | unsigned int cond, |
1284 | snd_pcm_hw_param_t var, | |
877211f5 | 1285 | struct snd_pcm_hw_constraint_ratnums *r) |
1da177e4 LT |
1286 | { |
1287 | return snd_pcm_hw_rule_add(runtime, cond, var, | |
1288 | snd_pcm_hw_rule_ratnums, r, | |
1289 | var, -1); | |
1290 | } | |
1291 | ||
e88e8ae6 TI |
1292 | EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums); |
1293 | ||
877211f5 TI |
1294 | static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params, |
1295 | struct snd_pcm_hw_rule *rule) | |
1da177e4 | 1296 | { |
877211f5 | 1297 | struct snd_pcm_hw_constraint_ratdens *r = rule->private; |
1da177e4 LT |
1298 | unsigned int num = 0, den = 0; |
1299 | int err = snd_interval_ratden(hw_param_interval(params, rule->var), | |
1300 | r->nrats, r->rats, &num, &den); | |
1301 | if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) { | |
1302 | params->rate_num = num; | |
1303 | params->rate_den = den; | |
1304 | } | |
1305 | return err; | |
1306 | } | |
1307 | ||
1308 | /** | |
1c85cc64 | 1309 | * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter |
df8db936 TI |
1310 | * @runtime: PCM runtime instance |
1311 | * @cond: condition bits | |
1312 | * @var: hw_params variable to apply the ratdens constraint | |
877211f5 | 1313 | * @r: struct snd_ratdens constriants |
1da177e4 | 1314 | */ |
877211f5 | 1315 | int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime, |
1da177e4 LT |
1316 | unsigned int cond, |
1317 | snd_pcm_hw_param_t var, | |
877211f5 | 1318 | struct snd_pcm_hw_constraint_ratdens *r) |
1da177e4 LT |
1319 | { |
1320 | return snd_pcm_hw_rule_add(runtime, cond, var, | |
1321 | snd_pcm_hw_rule_ratdens, r, | |
1322 | var, -1); | |
1323 | } | |
1324 | ||
e88e8ae6 TI |
1325 | EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens); |
1326 | ||
877211f5 TI |
1327 | static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params, |
1328 | struct snd_pcm_hw_rule *rule) | |
1da177e4 LT |
1329 | { |
1330 | unsigned int l = (unsigned long) rule->private; | |
1331 | int width = l & 0xffff; | |
1332 | unsigned int msbits = l >> 16; | |
877211f5 | 1333 | struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS); |
1da177e4 LT |
1334 | if (snd_interval_single(i) && snd_interval_value(i) == width) |
1335 | params->msbits = msbits; | |
1336 | return 0; | |
1337 | } | |
1338 | ||
1339 | /** | |
1c85cc64 | 1340 | * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule |
df8db936 TI |
1341 | * @runtime: PCM runtime instance |
1342 | * @cond: condition bits | |
1343 | * @width: sample bits width | |
1344 | * @msbits: msbits width | |
1da177e4 | 1345 | */ |
877211f5 | 1346 | int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime, |
1da177e4 LT |
1347 | unsigned int cond, |
1348 | unsigned int width, | |
1349 | unsigned int msbits) | |
1350 | { | |
1351 | unsigned long l = (msbits << 16) | width; | |
1352 | return snd_pcm_hw_rule_add(runtime, cond, -1, | |
1353 | snd_pcm_hw_rule_msbits, | |
1354 | (void*) l, | |
1355 | SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1); | |
1356 | } | |
1357 | ||
e88e8ae6 TI |
1358 | EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits); |
1359 | ||
877211f5 TI |
1360 | static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params, |
1361 | struct snd_pcm_hw_rule *rule) | |
1da177e4 LT |
1362 | { |
1363 | unsigned long step = (unsigned long) rule->private; | |
1364 | return snd_interval_step(hw_param_interval(params, rule->var), 0, step); | |
1365 | } | |
1366 | ||
1367 | /** | |
1c85cc64 | 1368 | * snd_pcm_hw_constraint_step - add a hw constraint step rule |
df8db936 TI |
1369 | * @runtime: PCM runtime instance |
1370 | * @cond: condition bits | |
1371 | * @var: hw_params variable to apply the step constraint | |
1372 | * @step: step size | |
1da177e4 | 1373 | */ |
877211f5 | 1374 | int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime, |
1da177e4 LT |
1375 | unsigned int cond, |
1376 | snd_pcm_hw_param_t var, | |
1377 | unsigned long step) | |
1378 | { | |
1379 | return snd_pcm_hw_rule_add(runtime, cond, var, | |
1380 | snd_pcm_hw_rule_step, (void *) step, | |
1381 | var, -1); | |
1382 | } | |
1383 | ||
e88e8ae6 TI |
1384 | EXPORT_SYMBOL(snd_pcm_hw_constraint_step); |
1385 | ||
877211f5 | 1386 | static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule) |
1da177e4 | 1387 | { |
67c39317 | 1388 | static unsigned int pow2_sizes[] = { |
1da177e4 LT |
1389 | 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7, |
1390 | 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15, | |
1391 | 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23, | |
1392 | 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30 | |
1393 | }; | |
1394 | return snd_interval_list(hw_param_interval(params, rule->var), | |
1395 | ARRAY_SIZE(pow2_sizes), pow2_sizes, 0); | |
1396 | } | |
1397 | ||
1398 | /** | |
1c85cc64 | 1399 | * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule |
df8db936 TI |
1400 | * @runtime: PCM runtime instance |
1401 | * @cond: condition bits | |
1402 | * @var: hw_params variable to apply the power-of-2 constraint | |
1da177e4 | 1403 | */ |
877211f5 | 1404 | int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime, |
1da177e4 LT |
1405 | unsigned int cond, |
1406 | snd_pcm_hw_param_t var) | |
1407 | { | |
1408 | return snd_pcm_hw_rule_add(runtime, cond, var, | |
1409 | snd_pcm_hw_rule_pow2, NULL, | |
1410 | var, -1); | |
1411 | } | |
1412 | ||
e88e8ae6 TI |
1413 | EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2); |
1414 | ||
877211f5 | 1415 | static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params, |
123992f7 | 1416 | snd_pcm_hw_param_t var) |
1da177e4 LT |
1417 | { |
1418 | if (hw_is_mask(var)) { | |
1419 | snd_mask_any(hw_param_mask(params, var)); | |
1420 | params->cmask |= 1 << var; | |
1421 | params->rmask |= 1 << var; | |
1422 | return; | |
1423 | } | |
1424 | if (hw_is_interval(var)) { | |
1425 | snd_interval_any(hw_param_interval(params, var)); | |
1426 | params->cmask |= 1 << var; | |
1427 | params->rmask |= 1 << var; | |
1428 | return; | |
1429 | } | |
1430 | snd_BUG(); | |
1431 | } | |
1432 | ||
877211f5 | 1433 | void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params) |
1da177e4 LT |
1434 | { |
1435 | unsigned int k; | |
1436 | memset(params, 0, sizeof(*params)); | |
1437 | for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) | |
1438 | _snd_pcm_hw_param_any(params, k); | |
1439 | for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) | |
1440 | _snd_pcm_hw_param_any(params, k); | |
1441 | params->info = ~0U; | |
1442 | } | |
1443 | ||
e88e8ae6 | 1444 | EXPORT_SYMBOL(_snd_pcm_hw_params_any); |
1da177e4 LT |
1445 | |
1446 | /** | |
1c85cc64 | 1447 | * snd_pcm_hw_param_value - return @params field @var value |
df8db936 TI |
1448 | * @params: the hw_params instance |
1449 | * @var: parameter to retrieve | |
1c85cc64 | 1450 | * @dir: pointer to the direction (-1,0,1) or %NULL |
1da177e4 | 1451 | * |
1c85cc64 RD |
1452 | * Return the value for field @var if it's fixed in configuration space |
1453 | * defined by @params. Return -%EINVAL otherwise. | |
1da177e4 | 1454 | */ |
e88e8ae6 TI |
1455 | int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params, |
1456 | snd_pcm_hw_param_t var, int *dir) | |
1da177e4 LT |
1457 | { |
1458 | if (hw_is_mask(var)) { | |
877211f5 | 1459 | const struct snd_mask *mask = hw_param_mask_c(params, var); |
1da177e4 LT |
1460 | if (!snd_mask_single(mask)) |
1461 | return -EINVAL; | |
1462 | if (dir) | |
1463 | *dir = 0; | |
1464 | return snd_mask_value(mask); | |
1465 | } | |
1466 | if (hw_is_interval(var)) { | |
877211f5 | 1467 | const struct snd_interval *i = hw_param_interval_c(params, var); |
1da177e4 LT |
1468 | if (!snd_interval_single(i)) |
1469 | return -EINVAL; | |
1470 | if (dir) | |
1471 | *dir = i->openmin; | |
1472 | return snd_interval_value(i); | |
1473 | } | |
1da177e4 LT |
1474 | return -EINVAL; |
1475 | } | |
1476 | ||
e88e8ae6 | 1477 | EXPORT_SYMBOL(snd_pcm_hw_param_value); |
1da177e4 | 1478 | |
877211f5 | 1479 | void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params, |
1da177e4 LT |
1480 | snd_pcm_hw_param_t var) |
1481 | { | |
1482 | if (hw_is_mask(var)) { | |
1483 | snd_mask_none(hw_param_mask(params, var)); | |
1484 | params->cmask |= 1 << var; | |
1485 | params->rmask |= 1 << var; | |
1486 | } else if (hw_is_interval(var)) { | |
1487 | snd_interval_none(hw_param_interval(params, var)); | |
1488 | params->cmask |= 1 << var; | |
1489 | params->rmask |= 1 << var; | |
1490 | } else { | |
1491 | snd_BUG(); | |
1492 | } | |
1493 | } | |
1494 | ||
e88e8ae6 | 1495 | EXPORT_SYMBOL(_snd_pcm_hw_param_setempty); |
1da177e4 | 1496 | |
877211f5 | 1497 | static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params, |
123992f7 | 1498 | snd_pcm_hw_param_t var) |
1da177e4 LT |
1499 | { |
1500 | int changed; | |
1501 | if (hw_is_mask(var)) | |
1502 | changed = snd_mask_refine_first(hw_param_mask(params, var)); | |
1503 | else if (hw_is_interval(var)) | |
1504 | changed = snd_interval_refine_first(hw_param_interval(params, var)); | |
2f4ca8e5 | 1505 | else |
1da177e4 | 1506 | return -EINVAL; |
1da177e4 LT |
1507 | if (changed) { |
1508 | params->cmask |= 1 << var; | |
1509 | params->rmask |= 1 << var; | |
1510 | } | |
1511 | return changed; | |
1512 | } | |
1513 | ||
1514 | ||
1515 | /** | |
1c85cc64 | 1516 | * snd_pcm_hw_param_first - refine config space and return minimum value |
df8db936 TI |
1517 | * @pcm: PCM instance |
1518 | * @params: the hw_params instance | |
1519 | * @var: parameter to retrieve | |
1c85cc64 | 1520 | * @dir: pointer to the direction (-1,0,1) or %NULL |
1da177e4 | 1521 | * |
1c85cc64 | 1522 | * Inside configuration space defined by @params remove from @var all |
1da177e4 LT |
1523 | * values > minimum. Reduce configuration space accordingly. |
1524 | * Return the minimum. | |
1525 | */ | |
e88e8ae6 TI |
1526 | int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm, |
1527 | struct snd_pcm_hw_params *params, | |
1528 | snd_pcm_hw_param_t var, int *dir) | |
1da177e4 LT |
1529 | { |
1530 | int changed = _snd_pcm_hw_param_first(params, var); | |
1531 | if (changed < 0) | |
1532 | return changed; | |
1533 | if (params->rmask) { | |
1534 | int err = snd_pcm_hw_refine(pcm, params); | |
7eaa943c TI |
1535 | if (snd_BUG_ON(err < 0)) |
1536 | return err; | |
1da177e4 LT |
1537 | } |
1538 | return snd_pcm_hw_param_value(params, var, dir); | |
1539 | } | |
1540 | ||
e88e8ae6 TI |
1541 | EXPORT_SYMBOL(snd_pcm_hw_param_first); |
1542 | ||
877211f5 | 1543 | static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params, |
123992f7 | 1544 | snd_pcm_hw_param_t var) |
1da177e4 LT |
1545 | { |
1546 | int changed; | |
1547 | if (hw_is_mask(var)) | |
1548 | changed = snd_mask_refine_last(hw_param_mask(params, var)); | |
1549 | else if (hw_is_interval(var)) | |
1550 | changed = snd_interval_refine_last(hw_param_interval(params, var)); | |
2f4ca8e5 | 1551 | else |
1da177e4 | 1552 | return -EINVAL; |
1da177e4 LT |
1553 | if (changed) { |
1554 | params->cmask |= 1 << var; | |
1555 | params->rmask |= 1 << var; | |
1556 | } | |
1557 | return changed; | |
1558 | } | |
1559 | ||
1560 | ||
1561 | /** | |
1c85cc64 | 1562 | * snd_pcm_hw_param_last - refine config space and return maximum value |
df8db936 TI |
1563 | * @pcm: PCM instance |
1564 | * @params: the hw_params instance | |
1565 | * @var: parameter to retrieve | |
1c85cc64 | 1566 | * @dir: pointer to the direction (-1,0,1) or %NULL |
1da177e4 | 1567 | * |
1c85cc64 | 1568 | * Inside configuration space defined by @params remove from @var all |
1da177e4 LT |
1569 | * values < maximum. Reduce configuration space accordingly. |
1570 | * Return the maximum. | |
1571 | */ | |
e88e8ae6 TI |
1572 | int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm, |
1573 | struct snd_pcm_hw_params *params, | |
1574 | snd_pcm_hw_param_t var, int *dir) | |
1da177e4 LT |
1575 | { |
1576 | int changed = _snd_pcm_hw_param_last(params, var); | |
1577 | if (changed < 0) | |
1578 | return changed; | |
1579 | if (params->rmask) { | |
1580 | int err = snd_pcm_hw_refine(pcm, params); | |
7eaa943c TI |
1581 | if (snd_BUG_ON(err < 0)) |
1582 | return err; | |
1da177e4 LT |
1583 | } |
1584 | return snd_pcm_hw_param_value(params, var, dir); | |
1585 | } | |
1586 | ||
e88e8ae6 | 1587 | EXPORT_SYMBOL(snd_pcm_hw_param_last); |
1da177e4 LT |
1588 | |
1589 | /** | |
1c85cc64 | 1590 | * snd_pcm_hw_param_choose - choose a configuration defined by @params |
df8db936 TI |
1591 | * @pcm: PCM instance |
1592 | * @params: the hw_params instance | |
1da177e4 | 1593 | * |
1c85cc64 | 1594 | * Choose one configuration from configuration space defined by @params. |
1da177e4 LT |
1595 | * The configuration chosen is that obtained fixing in this order: |
1596 | * first access, first format, first subformat, min channels, | |
1597 | * min rate, min period time, max buffer size, min tick time | |
1598 | */ | |
2f4ca8e5 TI |
1599 | int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm, |
1600 | struct snd_pcm_hw_params *params) | |
1da177e4 | 1601 | { |
2f4ca8e5 TI |
1602 | static int vars[] = { |
1603 | SNDRV_PCM_HW_PARAM_ACCESS, | |
1604 | SNDRV_PCM_HW_PARAM_FORMAT, | |
1605 | SNDRV_PCM_HW_PARAM_SUBFORMAT, | |
1606 | SNDRV_PCM_HW_PARAM_CHANNELS, | |
1607 | SNDRV_PCM_HW_PARAM_RATE, | |
1608 | SNDRV_PCM_HW_PARAM_PERIOD_TIME, | |
1609 | SNDRV_PCM_HW_PARAM_BUFFER_SIZE, | |
1610 | SNDRV_PCM_HW_PARAM_TICK_TIME, | |
1611 | -1 | |
1612 | }; | |
1613 | int err, *v; | |
1da177e4 | 1614 | |
2f4ca8e5 TI |
1615 | for (v = vars; *v != -1; v++) { |
1616 | if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE) | |
1617 | err = snd_pcm_hw_param_first(pcm, params, *v, NULL); | |
1618 | else | |
1619 | err = snd_pcm_hw_param_last(pcm, params, *v, NULL); | |
7eaa943c TI |
1620 | if (snd_BUG_ON(err < 0)) |
1621 | return err; | |
2f4ca8e5 | 1622 | } |
1da177e4 LT |
1623 | return 0; |
1624 | } | |
1625 | ||
877211f5 | 1626 | static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream, |
1da177e4 LT |
1627 | void *arg) |
1628 | { | |
877211f5 | 1629 | struct snd_pcm_runtime *runtime = substream->runtime; |
1da177e4 LT |
1630 | unsigned long flags; |
1631 | snd_pcm_stream_lock_irqsave(substream, flags); | |
1632 | if (snd_pcm_running(substream) && | |
1633 | snd_pcm_update_hw_ptr(substream) >= 0) | |
1634 | runtime->status->hw_ptr %= runtime->buffer_size; | |
1635 | else | |
1636 | runtime->status->hw_ptr = 0; | |
1637 | snd_pcm_stream_unlock_irqrestore(substream, flags); | |
1638 | return 0; | |
1639 | } | |
1640 | ||
877211f5 | 1641 | static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream, |
1da177e4 LT |
1642 | void *arg) |
1643 | { | |
877211f5 TI |
1644 | struct snd_pcm_channel_info *info = arg; |
1645 | struct snd_pcm_runtime *runtime = substream->runtime; | |
1da177e4 LT |
1646 | int width; |
1647 | if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) { | |
1648 | info->offset = -1; | |
1649 | return 0; | |
1650 | } | |
1651 | width = snd_pcm_format_physical_width(runtime->format); | |
1652 | if (width < 0) | |
1653 | return width; | |
1654 | info->offset = 0; | |
1655 | switch (runtime->access) { | |
1656 | case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED: | |
1657 | case SNDRV_PCM_ACCESS_RW_INTERLEAVED: | |
1658 | info->first = info->channel * width; | |
1659 | info->step = runtime->channels * width; | |
1660 | break; | |
1661 | case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED: | |
1662 | case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED: | |
1663 | { | |
1664 | size_t size = runtime->dma_bytes / runtime->channels; | |
1665 | info->first = info->channel * size * 8; | |
1666 | info->step = width; | |
1667 | break; | |
1668 | } | |
1669 | default: | |
1670 | snd_BUG(); | |
1671 | break; | |
1672 | } | |
1673 | return 0; | |
1674 | } | |
1675 | ||
8bea869c JK |
1676 | static int snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream *substream, |
1677 | void *arg) | |
1678 | { | |
1679 | struct snd_pcm_hw_params *params = arg; | |
1680 | snd_pcm_format_t format; | |
1681 | int channels, width; | |
1682 | ||
1683 | params->fifo_size = substream->runtime->hw.fifo_size; | |
1684 | if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_FIFO_IN_FRAMES)) { | |
1685 | format = params_format(params); | |
1686 | channels = params_channels(params); | |
1687 | width = snd_pcm_format_physical_width(format); | |
1688 | params->fifo_size /= width * channels; | |
1689 | } | |
1690 | return 0; | |
1691 | } | |
1692 | ||
1da177e4 LT |
1693 | /** |
1694 | * snd_pcm_lib_ioctl - a generic PCM ioctl callback | |
1695 | * @substream: the pcm substream instance | |
1696 | * @cmd: ioctl command | |
1697 | * @arg: ioctl argument | |
1698 | * | |
1699 | * Processes the generic ioctl commands for PCM. | |
1700 | * Can be passed as the ioctl callback for PCM ops. | |
1701 | * | |
1702 | * Returns zero if successful, or a negative error code on failure. | |
1703 | */ | |
877211f5 | 1704 | int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream, |
1da177e4 LT |
1705 | unsigned int cmd, void *arg) |
1706 | { | |
1707 | switch (cmd) { | |
1708 | case SNDRV_PCM_IOCTL1_INFO: | |
1709 | return 0; | |
1710 | case SNDRV_PCM_IOCTL1_RESET: | |
1711 | return snd_pcm_lib_ioctl_reset(substream, arg); | |
1712 | case SNDRV_PCM_IOCTL1_CHANNEL_INFO: | |
1713 | return snd_pcm_lib_ioctl_channel_info(substream, arg); | |
8bea869c JK |
1714 | case SNDRV_PCM_IOCTL1_FIFO_SIZE: |
1715 | return snd_pcm_lib_ioctl_fifo_size(substream, arg); | |
1da177e4 LT |
1716 | } |
1717 | return -ENXIO; | |
1718 | } | |
1719 | ||
e88e8ae6 TI |
1720 | EXPORT_SYMBOL(snd_pcm_lib_ioctl); |
1721 | ||
1da177e4 LT |
1722 | /** |
1723 | * snd_pcm_period_elapsed - update the pcm status for the next period | |
1724 | * @substream: the pcm substream instance | |
1725 | * | |
1726 | * This function is called from the interrupt handler when the | |
1727 | * PCM has processed the period size. It will update the current | |
31e8960b | 1728 | * pointer, wake up sleepers, etc. |
1da177e4 LT |
1729 | * |
1730 | * Even if more than one periods have elapsed since the last call, you | |
1731 | * have to call this only once. | |
1732 | */ | |
877211f5 | 1733 | void snd_pcm_period_elapsed(struct snd_pcm_substream *substream) |
1da177e4 | 1734 | { |
877211f5 | 1735 | struct snd_pcm_runtime *runtime; |
1da177e4 LT |
1736 | unsigned long flags; |
1737 | ||
7eaa943c TI |
1738 | if (PCM_RUNTIME_CHECK(substream)) |
1739 | return; | |
1da177e4 | 1740 | runtime = substream->runtime; |
1da177e4 LT |
1741 | |
1742 | if (runtime->transfer_ack_begin) | |
1743 | runtime->transfer_ack_begin(substream); | |
1744 | ||
1745 | snd_pcm_stream_lock_irqsave(substream, flags); | |
1746 | if (!snd_pcm_running(substream) || | |
1747 | snd_pcm_update_hw_ptr_interrupt(substream) < 0) | |
1748 | goto _end; | |
1749 | ||
1750 | if (substream->timer_running) | |
1751 | snd_timer_interrupt(substream->timer, 1); | |
1da177e4 LT |
1752 | _end: |
1753 | snd_pcm_stream_unlock_irqrestore(substream, flags); | |
1754 | if (runtime->transfer_ack_end) | |
1755 | runtime->transfer_ack_end(substream); | |
1756 | kill_fasync(&runtime->fasync, SIGIO, POLL_IN); | |
1757 | } | |
1758 | ||
e88e8ae6 TI |
1759 | EXPORT_SYMBOL(snd_pcm_period_elapsed); |
1760 | ||
13075510 TI |
1761 | /* |
1762 | * Wait until avail_min data becomes available | |
1763 | * Returns a negative error code if any error occurs during operation. | |
1764 | * The available space is stored on availp. When err = 0 and avail = 0 | |
1765 | * on the capture stream, it indicates the stream is in DRAINING state. | |
1766 | */ | |
1767 | static int wait_for_avail_min(struct snd_pcm_substream *substream, | |
1768 | snd_pcm_uframes_t *availp) | |
1769 | { | |
1770 | struct snd_pcm_runtime *runtime = substream->runtime; | |
1771 | int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; | |
1772 | wait_queue_t wait; | |
1773 | int err = 0; | |
1774 | snd_pcm_uframes_t avail = 0; | |
1775 | long tout; | |
1776 | ||
1777 | init_waitqueue_entry(&wait, current); | |
1778 | add_wait_queue(&runtime->sleep, &wait); | |
1779 | for (;;) { | |
1780 | if (signal_pending(current)) { | |
1781 | err = -ERESTARTSYS; | |
1782 | break; | |
1783 | } | |
1784 | set_current_state(TASK_INTERRUPTIBLE); | |
1785 | snd_pcm_stream_unlock_irq(substream); | |
1786 | tout = schedule_timeout(msecs_to_jiffies(10000)); | |
1787 | snd_pcm_stream_lock_irq(substream); | |
1788 | switch (runtime->status->state) { | |
1789 | case SNDRV_PCM_STATE_SUSPENDED: | |
1790 | err = -ESTRPIPE; | |
1791 | goto _endloop; | |
1792 | case SNDRV_PCM_STATE_XRUN: | |
1793 | err = -EPIPE; | |
1794 | goto _endloop; | |
1795 | case SNDRV_PCM_STATE_DRAINING: | |
1796 | if (is_playback) | |
1797 | err = -EPIPE; | |
1798 | else | |
1799 | avail = 0; /* indicate draining */ | |
1800 | goto _endloop; | |
1801 | case SNDRV_PCM_STATE_OPEN: | |
1802 | case SNDRV_PCM_STATE_SETUP: | |
1803 | case SNDRV_PCM_STATE_DISCONNECTED: | |
1804 | err = -EBADFD; | |
1805 | goto _endloop; | |
1806 | } | |
1807 | if (!tout) { | |
1808 | snd_printd("%s write error (DMA or IRQ trouble?)\n", | |
1809 | is_playback ? "playback" : "capture"); | |
1810 | err = -EIO; | |
1811 | break; | |
1812 | } | |
1813 | if (is_playback) | |
1814 | avail = snd_pcm_playback_avail(runtime); | |
1815 | else | |
1816 | avail = snd_pcm_capture_avail(runtime); | |
1817 | if (avail >= runtime->control->avail_min) | |
1818 | break; | |
1819 | } | |
1820 | _endloop: | |
1821 | remove_wait_queue(&runtime->sleep, &wait); | |
1822 | *availp = avail; | |
1823 | return err; | |
1824 | } | |
1825 | ||
877211f5 | 1826 | static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream, |
1da177e4 LT |
1827 | unsigned int hwoff, |
1828 | unsigned long data, unsigned int off, | |
1829 | snd_pcm_uframes_t frames) | |
1830 | { | |
877211f5 | 1831 | struct snd_pcm_runtime *runtime = substream->runtime; |
1da177e4 LT |
1832 | int err; |
1833 | char __user *buf = (char __user *) data + frames_to_bytes(runtime, off); | |
1834 | if (substream->ops->copy) { | |
1835 | if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0) | |
1836 | return err; | |
1837 | } else { | |
1838 | char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff); | |
1da177e4 LT |
1839 | if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames))) |
1840 | return -EFAULT; | |
1841 | } | |
1842 | return 0; | |
1843 | } | |
1844 | ||
877211f5 | 1845 | typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff, |
1da177e4 LT |
1846 | unsigned long data, unsigned int off, |
1847 | snd_pcm_uframes_t size); | |
1848 | ||
877211f5 | 1849 | static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream, |
1da177e4 LT |
1850 | unsigned long data, |
1851 | snd_pcm_uframes_t size, | |
1852 | int nonblock, | |
1853 | transfer_f transfer) | |
1854 | { | |
877211f5 | 1855 | struct snd_pcm_runtime *runtime = substream->runtime; |
1da177e4 LT |
1856 | snd_pcm_uframes_t xfer = 0; |
1857 | snd_pcm_uframes_t offset = 0; | |
1858 | int err = 0; | |
1859 | ||
1860 | if (size == 0) | |
1861 | return 0; | |
1da177e4 LT |
1862 | |
1863 | snd_pcm_stream_lock_irq(substream); | |
1864 | switch (runtime->status->state) { | |
1865 | case SNDRV_PCM_STATE_PREPARED: | |
1866 | case SNDRV_PCM_STATE_RUNNING: | |
1867 | case SNDRV_PCM_STATE_PAUSED: | |
1868 | break; | |
1869 | case SNDRV_PCM_STATE_XRUN: | |
1870 | err = -EPIPE; | |
1871 | goto _end_unlock; | |
1872 | case SNDRV_PCM_STATE_SUSPENDED: | |
1873 | err = -ESTRPIPE; | |
1874 | goto _end_unlock; | |
1875 | default: | |
1876 | err = -EBADFD; | |
1877 | goto _end_unlock; | |
1878 | } | |
1879 | ||
1880 | while (size > 0) { | |
1881 | snd_pcm_uframes_t frames, appl_ptr, appl_ofs; | |
1882 | snd_pcm_uframes_t avail; | |
1883 | snd_pcm_uframes_t cont; | |
31e8960b | 1884 | if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) |
1da177e4 LT |
1885 | snd_pcm_update_hw_ptr(substream); |
1886 | avail = snd_pcm_playback_avail(runtime); | |
13075510 | 1887 | if (!avail) { |
1da177e4 LT |
1888 | if (nonblock) { |
1889 | err = -EAGAIN; | |
1890 | goto _end_unlock; | |
1891 | } | |
13075510 TI |
1892 | err = wait_for_avail_min(substream, &avail); |
1893 | if (err < 0) | |
443feb88 | 1894 | goto _end_unlock; |
1da177e4 | 1895 | } |
1da177e4 LT |
1896 | frames = size > avail ? avail : size; |
1897 | cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size; | |
1898 | if (frames > cont) | |
1899 | frames = cont; | |
7eaa943c TI |
1900 | if (snd_BUG_ON(!frames)) { |
1901 | snd_pcm_stream_unlock_irq(substream); | |
1902 | return -EINVAL; | |
1903 | } | |
1da177e4 LT |
1904 | appl_ptr = runtime->control->appl_ptr; |
1905 | appl_ofs = appl_ptr % runtime->buffer_size; | |
1906 | snd_pcm_stream_unlock_irq(substream); | |
1907 | if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0) | |
1908 | goto _end; | |
1909 | snd_pcm_stream_lock_irq(substream); | |
1910 | switch (runtime->status->state) { | |
1911 | case SNDRV_PCM_STATE_XRUN: | |
1912 | err = -EPIPE; | |
1913 | goto _end_unlock; | |
1914 | case SNDRV_PCM_STATE_SUSPENDED: | |
1915 | err = -ESTRPIPE; | |
1916 | goto _end_unlock; | |
1917 | default: | |
1918 | break; | |
1919 | } | |
1920 | appl_ptr += frames; | |
1921 | if (appl_ptr >= runtime->boundary) | |
1922 | appl_ptr -= runtime->boundary; | |
1923 | runtime->control->appl_ptr = appl_ptr; | |
1924 | if (substream->ops->ack) | |
1925 | substream->ops->ack(substream); | |
1926 | ||
1927 | offset += frames; | |
1928 | size -= frames; | |
1929 | xfer += frames; | |
1930 | if (runtime->status->state == SNDRV_PCM_STATE_PREPARED && | |
1931 | snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) { | |
1932 | err = snd_pcm_start(substream); | |
1933 | if (err < 0) | |
1934 | goto _end_unlock; | |
1935 | } | |
1da177e4 LT |
1936 | } |
1937 | _end_unlock: | |
1938 | snd_pcm_stream_unlock_irq(substream); | |
1939 | _end: | |
1940 | return xfer > 0 ? (snd_pcm_sframes_t)xfer : err; | |
1941 | } | |
1942 | ||
7eaa943c TI |
1943 | /* sanity-check for read/write methods */ |
1944 | static int pcm_sanity_check(struct snd_pcm_substream *substream) | |
1da177e4 | 1945 | { |
877211f5 | 1946 | struct snd_pcm_runtime *runtime; |
7eaa943c TI |
1947 | if (PCM_RUNTIME_CHECK(substream)) |
1948 | return -ENXIO; | |
1da177e4 | 1949 | runtime = substream->runtime; |
7eaa943c TI |
1950 | if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area)) |
1951 | return -EINVAL; | |
1da177e4 LT |
1952 | if (runtime->status->state == SNDRV_PCM_STATE_OPEN) |
1953 | return -EBADFD; | |
7eaa943c TI |
1954 | return 0; |
1955 | } | |
1956 | ||
1957 | snd_pcm_sframes_t snd_pcm_lib_write(struct snd_pcm_substream *substream, const void __user *buf, snd_pcm_uframes_t size) | |
1958 | { | |
1959 | struct snd_pcm_runtime *runtime; | |
1960 | int nonblock; | |
1961 | int err; | |
1da177e4 | 1962 | |
7eaa943c TI |
1963 | err = pcm_sanity_check(substream); |
1964 | if (err < 0) | |
1965 | return err; | |
1966 | runtime = substream->runtime; | |
0df63e44 | 1967 | nonblock = !!(substream->f_flags & O_NONBLOCK); |
1da177e4 LT |
1968 | |
1969 | if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED && | |
1970 | runtime->channels > 1) | |
1971 | return -EINVAL; | |
1972 | return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock, | |
1973 | snd_pcm_lib_write_transfer); | |
1974 | } | |
1975 | ||
e88e8ae6 TI |
1976 | EXPORT_SYMBOL(snd_pcm_lib_write); |
1977 | ||
877211f5 | 1978 | static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream, |
1da177e4 LT |
1979 | unsigned int hwoff, |
1980 | unsigned long data, unsigned int off, | |
1981 | snd_pcm_uframes_t frames) | |
1982 | { | |
877211f5 | 1983 | struct snd_pcm_runtime *runtime = substream->runtime; |
1da177e4 LT |
1984 | int err; |
1985 | void __user **bufs = (void __user **)data; | |
1986 | int channels = runtime->channels; | |
1987 | int c; | |
1988 | if (substream->ops->copy) { | |
7eaa943c TI |
1989 | if (snd_BUG_ON(!substream->ops->silence)) |
1990 | return -EINVAL; | |
1da177e4 LT |
1991 | for (c = 0; c < channels; ++c, ++bufs) { |
1992 | if (*bufs == NULL) { | |
1993 | if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0) | |
1994 | return err; | |
1995 | } else { | |
1996 | char __user *buf = *bufs + samples_to_bytes(runtime, off); | |
1997 | if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0) | |
1998 | return err; | |
1999 | } | |
2000 | } | |
2001 | } else { | |
2002 | /* default transfer behaviour */ | |
2003 | size_t dma_csize = runtime->dma_bytes / channels; | |
1da177e4 LT |
2004 | for (c = 0; c < channels; ++c, ++bufs) { |
2005 | char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff); | |
2006 | if (*bufs == NULL) { | |
2007 | snd_pcm_format_set_silence(runtime->format, hwbuf, frames); | |
2008 | } else { | |
2009 | char __user *buf = *bufs + samples_to_bytes(runtime, off); | |
2010 | if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames))) | |
2011 | return -EFAULT; | |
2012 | } | |
2013 | } | |
2014 | } | |
2015 | return 0; | |
2016 | } | |
2017 | ||
877211f5 | 2018 | snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream, |
1da177e4 LT |
2019 | void __user **bufs, |
2020 | snd_pcm_uframes_t frames) | |
2021 | { | |
877211f5 | 2022 | struct snd_pcm_runtime *runtime; |
1da177e4 | 2023 | int nonblock; |
7eaa943c | 2024 | int err; |
1da177e4 | 2025 | |
7eaa943c TI |
2026 | err = pcm_sanity_check(substream); |
2027 | if (err < 0) | |
2028 | return err; | |
1da177e4 | 2029 | runtime = substream->runtime; |
0df63e44 | 2030 | nonblock = !!(substream->f_flags & O_NONBLOCK); |
1da177e4 LT |
2031 | |
2032 | if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) | |
2033 | return -EINVAL; | |
2034 | return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames, | |
2035 | nonblock, snd_pcm_lib_writev_transfer); | |
2036 | } | |
2037 | ||
e88e8ae6 TI |
2038 | EXPORT_SYMBOL(snd_pcm_lib_writev); |
2039 | ||
877211f5 | 2040 | static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream, |
1da177e4 LT |
2041 | unsigned int hwoff, |
2042 | unsigned long data, unsigned int off, | |
2043 | snd_pcm_uframes_t frames) | |
2044 | { | |
877211f5 | 2045 | struct snd_pcm_runtime *runtime = substream->runtime; |
1da177e4 LT |
2046 | int err; |
2047 | char __user *buf = (char __user *) data + frames_to_bytes(runtime, off); | |
2048 | if (substream->ops->copy) { | |
2049 | if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0) | |
2050 | return err; | |
2051 | } else { | |
2052 | char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff); | |
1da177e4 LT |
2053 | if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames))) |
2054 | return -EFAULT; | |
2055 | } | |
2056 | return 0; | |
2057 | } | |
2058 | ||
877211f5 | 2059 | static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream, |
1da177e4 LT |
2060 | unsigned long data, |
2061 | snd_pcm_uframes_t size, | |
2062 | int nonblock, | |
2063 | transfer_f transfer) | |
2064 | { | |
877211f5 | 2065 | struct snd_pcm_runtime *runtime = substream->runtime; |
1da177e4 LT |
2066 | snd_pcm_uframes_t xfer = 0; |
2067 | snd_pcm_uframes_t offset = 0; | |
2068 | int err = 0; | |
2069 | ||
2070 | if (size == 0) | |
2071 | return 0; | |
1da177e4 LT |
2072 | |
2073 | snd_pcm_stream_lock_irq(substream); | |
2074 | switch (runtime->status->state) { | |
2075 | case SNDRV_PCM_STATE_PREPARED: | |
2076 | if (size >= runtime->start_threshold) { | |
2077 | err = snd_pcm_start(substream); | |
2078 | if (err < 0) | |
2079 | goto _end_unlock; | |
2080 | } | |
2081 | break; | |
2082 | case SNDRV_PCM_STATE_DRAINING: | |
2083 | case SNDRV_PCM_STATE_RUNNING: | |
2084 | case SNDRV_PCM_STATE_PAUSED: | |
2085 | break; | |
2086 | case SNDRV_PCM_STATE_XRUN: | |
2087 | err = -EPIPE; | |
2088 | goto _end_unlock; | |
2089 | case SNDRV_PCM_STATE_SUSPENDED: | |
2090 | err = -ESTRPIPE; | |
2091 | goto _end_unlock; | |
2092 | default: | |
2093 | err = -EBADFD; | |
2094 | goto _end_unlock; | |
2095 | } | |
2096 | ||
2097 | while (size > 0) { | |
2098 | snd_pcm_uframes_t frames, appl_ptr, appl_ofs; | |
2099 | snd_pcm_uframes_t avail; | |
2100 | snd_pcm_uframes_t cont; | |
31e8960b | 2101 | if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) |
1da177e4 | 2102 | snd_pcm_update_hw_ptr(substream); |
1da177e4 | 2103 | avail = snd_pcm_capture_avail(runtime); |
13075510 TI |
2104 | if (!avail) { |
2105 | if (runtime->status->state == | |
2106 | SNDRV_PCM_STATE_DRAINING) { | |
2107 | snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP); | |
1da177e4 LT |
2108 | goto _end_unlock; |
2109 | } | |
1da177e4 LT |
2110 | if (nonblock) { |
2111 | err = -EAGAIN; | |
2112 | goto _end_unlock; | |
2113 | } | |
13075510 TI |
2114 | err = wait_for_avail_min(substream, &avail); |
2115 | if (err < 0) | |
443feb88 | 2116 | goto _end_unlock; |
13075510 TI |
2117 | if (!avail) |
2118 | continue; /* draining */ | |
1da177e4 | 2119 | } |
1da177e4 LT |
2120 | frames = size > avail ? avail : size; |
2121 | cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size; | |
2122 | if (frames > cont) | |
2123 | frames = cont; | |
7eaa943c TI |
2124 | if (snd_BUG_ON(!frames)) { |
2125 | snd_pcm_stream_unlock_irq(substream); | |
2126 | return -EINVAL; | |
2127 | } | |
1da177e4 LT |
2128 | appl_ptr = runtime->control->appl_ptr; |
2129 | appl_ofs = appl_ptr % runtime->buffer_size; | |
2130 | snd_pcm_stream_unlock_irq(substream); | |
2131 | if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0) | |
2132 | goto _end; | |
2133 | snd_pcm_stream_lock_irq(substream); | |
2134 | switch (runtime->status->state) { | |
2135 | case SNDRV_PCM_STATE_XRUN: | |
2136 | err = -EPIPE; | |
2137 | goto _end_unlock; | |
2138 | case SNDRV_PCM_STATE_SUSPENDED: | |
2139 | err = -ESTRPIPE; | |
2140 | goto _end_unlock; | |
2141 | default: | |
2142 | break; | |
2143 | } | |
2144 | appl_ptr += frames; | |
2145 | if (appl_ptr >= runtime->boundary) | |
2146 | appl_ptr -= runtime->boundary; | |
2147 | runtime->control->appl_ptr = appl_ptr; | |
2148 | if (substream->ops->ack) | |
2149 | substream->ops->ack(substream); | |
2150 | ||
2151 | offset += frames; | |
2152 | size -= frames; | |
2153 | xfer += frames; | |
1da177e4 LT |
2154 | } |
2155 | _end_unlock: | |
2156 | snd_pcm_stream_unlock_irq(substream); | |
2157 | _end: | |
2158 | return xfer > 0 ? (snd_pcm_sframes_t)xfer : err; | |
2159 | } | |
2160 | ||
877211f5 | 2161 | snd_pcm_sframes_t snd_pcm_lib_read(struct snd_pcm_substream *substream, void __user *buf, snd_pcm_uframes_t size) |
1da177e4 | 2162 | { |
877211f5 | 2163 | struct snd_pcm_runtime *runtime; |
1da177e4 | 2164 | int nonblock; |
7eaa943c | 2165 | int err; |
1da177e4 | 2166 | |
7eaa943c TI |
2167 | err = pcm_sanity_check(substream); |
2168 | if (err < 0) | |
2169 | return err; | |
1da177e4 | 2170 | runtime = substream->runtime; |
0df63e44 | 2171 | nonblock = !!(substream->f_flags & O_NONBLOCK); |
1da177e4 LT |
2172 | if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED) |
2173 | return -EINVAL; | |
2174 | return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer); | |
2175 | } | |
2176 | ||
e88e8ae6 TI |
2177 | EXPORT_SYMBOL(snd_pcm_lib_read); |
2178 | ||
877211f5 | 2179 | static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream, |
1da177e4 LT |
2180 | unsigned int hwoff, |
2181 | unsigned long data, unsigned int off, | |
2182 | snd_pcm_uframes_t frames) | |
2183 | { | |
877211f5 | 2184 | struct snd_pcm_runtime *runtime = substream->runtime; |
1da177e4 LT |
2185 | int err; |
2186 | void __user **bufs = (void __user **)data; | |
2187 | int channels = runtime->channels; | |
2188 | int c; | |
2189 | if (substream->ops->copy) { | |
2190 | for (c = 0; c < channels; ++c, ++bufs) { | |
2191 | char __user *buf; | |
2192 | if (*bufs == NULL) | |
2193 | continue; | |
2194 | buf = *bufs + samples_to_bytes(runtime, off); | |
2195 | if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0) | |
2196 | return err; | |
2197 | } | |
2198 | } else { | |
2199 | snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels; | |
1da177e4 LT |
2200 | for (c = 0; c < channels; ++c, ++bufs) { |
2201 | char *hwbuf; | |
2202 | char __user *buf; | |
2203 | if (*bufs == NULL) | |
2204 | continue; | |
2205 | ||
2206 | hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff); | |
2207 | buf = *bufs + samples_to_bytes(runtime, off); | |
2208 | if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames))) | |
2209 | return -EFAULT; | |
2210 | } | |
2211 | } | |
2212 | return 0; | |
2213 | } | |
2214 | ||
877211f5 | 2215 | snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream, |
1da177e4 LT |
2216 | void __user **bufs, |
2217 | snd_pcm_uframes_t frames) | |
2218 | { | |
877211f5 | 2219 | struct snd_pcm_runtime *runtime; |
1da177e4 | 2220 | int nonblock; |
7eaa943c | 2221 | int err; |
1da177e4 | 2222 | |
7eaa943c TI |
2223 | err = pcm_sanity_check(substream); |
2224 | if (err < 0) | |
2225 | return err; | |
1da177e4 | 2226 | runtime = substream->runtime; |
1da177e4 LT |
2227 | if (runtime->status->state == SNDRV_PCM_STATE_OPEN) |
2228 | return -EBADFD; | |
2229 | ||
0df63e44 | 2230 | nonblock = !!(substream->f_flags & O_NONBLOCK); |
1da177e4 LT |
2231 | if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) |
2232 | return -EINVAL; | |
2233 | return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer); | |
2234 | } | |
2235 | ||
1da177e4 | 2236 | EXPORT_SYMBOL(snd_pcm_lib_readv); |