]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Digital Audio (PCM) abstract layer | |
c1017a4c | 3 | * Copyright (c) by Jaroslav Kysela <[email protected]> |
1da177e4 LT |
4 | * |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
19 | * | |
20 | */ | |
21 | ||
1da177e4 | 22 | #include <linux/mm.h> |
da155d5b | 23 | #include <linux/module.h> |
1da177e4 LT |
24 | #include <linux/file.h> |
25 | #include <linux/slab.h> | |
174cd4b1 | 26 | #include <linux/sched/signal.h> |
1da177e4 | 27 | #include <linux/time.h> |
e8db0be1 | 28 | #include <linux/pm_qos.h> |
6cbbfe1c | 29 | #include <linux/io.h> |
657b1989 | 30 | #include <linux/dma-mapping.h> |
1da177e4 LT |
31 | #include <sound/core.h> |
32 | #include <sound/control.h> | |
33 | #include <sound/info.h> | |
34 | #include <sound/pcm.h> | |
35 | #include <sound/pcm_params.h> | |
36 | #include <sound/timer.h> | |
37 | #include <sound/minors.h> | |
e2e40f2c | 38 | #include <linux/uio.h> |
b888a5f7 | 39 | #include <linux/delay.h> |
1da177e4 | 40 | |
2c4842d3 TS |
41 | #include "pcm_local.h" |
42 | ||
37567c55 | 43 | #ifdef CONFIG_SND_DEBUG |
be4e31da TS |
44 | #define CREATE_TRACE_POINTS |
45 | #include "pcm_param_trace.h" | |
37567c55 TS |
46 | #else |
47 | #define trace_hw_mask_param_enabled() 0 | |
48 | #define trace_hw_interval_param_enabled() 0 | |
49 | #define trace_hw_mask_param(substream, type, index, prev, curr) | |
50 | #define trace_hw_interval_param(substream, type, index, prev, curr) | |
51 | #endif | |
be4e31da | 52 | |
1da177e4 LT |
53 | /* |
54 | * Compatibility | |
55 | */ | |
56 | ||
877211f5 | 57 | struct snd_pcm_hw_params_old { |
1da177e4 LT |
58 | unsigned int flags; |
59 | unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT - | |
60 | SNDRV_PCM_HW_PARAM_ACCESS + 1]; | |
877211f5 | 61 | struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME - |
1da177e4 LT |
62 | SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1]; |
63 | unsigned int rmask; | |
64 | unsigned int cmask; | |
65 | unsigned int info; | |
66 | unsigned int msbits; | |
67 | unsigned int rate_num; | |
68 | unsigned int rate_den; | |
877211f5 | 69 | snd_pcm_uframes_t fifo_size; |
1da177e4 LT |
70 | unsigned char reserved[64]; |
71 | }; | |
72 | ||
59d48582 | 73 | #ifdef CONFIG_SND_SUPPORT_OLD_API |
877211f5 TI |
74 | #define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old) |
75 | #define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old) | |
1da177e4 | 76 | |
877211f5 TI |
77 | static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream, |
78 | struct snd_pcm_hw_params_old __user * _oparams); | |
79 | static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream, | |
80 | struct snd_pcm_hw_params_old __user * _oparams); | |
59d48582 | 81 | #endif |
f87135f5 | 82 | static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream); |
1da177e4 LT |
83 | |
84 | /* | |
85 | * | |
86 | */ | |
87 | ||
7af142f7 TI |
88 | static DEFINE_RWLOCK(snd_pcm_link_rwlock); |
89 | static DECLARE_RWSEM(snd_pcm_link_rwsem); | |
1da177e4 | 90 | |
67ec1072 TI |
91 | /* Writer in rwsem may block readers even during its waiting in queue, |
92 | * and this may lead to a deadlock when the code path takes read sem | |
93 | * twice (e.g. one in snd_pcm_action_nonatomic() and another in | |
94 | * snd_pcm_stream_lock()). As a (suboptimal) workaround, let writer to | |
b888a5f7 | 95 | * sleep until all the readers are completed without blocking by writer. |
67ec1072 | 96 | */ |
b888a5f7 | 97 | static inline void down_write_nonfifo(struct rw_semaphore *lock) |
67ec1072 TI |
98 | { |
99 | while (!down_write_trylock(lock)) | |
b888a5f7 | 100 | msleep(1); |
67ec1072 TI |
101 | } |
102 | ||
73365cb1 TI |
103 | void snd_pcm_group_init(struct snd_pcm_group *group) |
104 | { | |
105 | spin_lock_init(&group->lock); | |
106 | mutex_init(&group->mutex); | |
107 | INIT_LIST_HEAD(&group->substreams); | |
108 | } | |
109 | ||
10aa7cad AMG |
110 | #define PCM_LOCK_DEFAULT 0 |
111 | #define PCM_LOCK_IRQ 1 | |
112 | #define PCM_LOCK_IRQSAVE 2 | |
113 | ||
114 | static unsigned long __snd_pcm_stream_lock_mode(struct snd_pcm_substream *substream, | |
115 | unsigned int mode) | |
116 | { | |
117 | unsigned long flags = 0; | |
118 | if (substream->pcm->nonatomic) { | |
119 | down_read_nested(&snd_pcm_link_rwsem, SINGLE_DEPTH_NESTING); | |
120 | mutex_lock(&substream->self_group.mutex); | |
121 | } else { | |
122 | switch (mode) { | |
123 | case PCM_LOCK_DEFAULT: | |
124 | read_lock(&snd_pcm_link_rwlock); | |
125 | break; | |
126 | case PCM_LOCK_IRQ: | |
127 | read_lock_irq(&snd_pcm_link_rwlock); | |
128 | break; | |
129 | case PCM_LOCK_IRQSAVE: | |
130 | read_lock_irqsave(&snd_pcm_link_rwlock, flags); | |
131 | break; | |
132 | } | |
133 | spin_lock(&substream->self_group.lock); | |
134 | } | |
135 | return flags; | |
136 | } | |
137 | ||
138 | static void __snd_pcm_stream_unlock_mode(struct snd_pcm_substream *substream, | |
139 | unsigned int mode, unsigned long flags) | |
140 | { | |
141 | if (substream->pcm->nonatomic) { | |
142 | mutex_unlock(&substream->self_group.mutex); | |
143 | up_read(&snd_pcm_link_rwsem); | |
144 | } else { | |
145 | spin_unlock(&substream->self_group.lock); | |
146 | ||
147 | switch (mode) { | |
148 | case PCM_LOCK_DEFAULT: | |
149 | read_unlock(&snd_pcm_link_rwlock); | |
150 | break; | |
151 | case PCM_LOCK_IRQ: | |
152 | read_unlock_irq(&snd_pcm_link_rwlock); | |
153 | break; | |
154 | case PCM_LOCK_IRQSAVE: | |
155 | read_unlock_irqrestore(&snd_pcm_link_rwlock, flags); | |
156 | break; | |
157 | } | |
158 | } | |
159 | } | |
160 | ||
30b771cf TI |
161 | /** |
162 | * snd_pcm_stream_lock - Lock the PCM stream | |
163 | * @substream: PCM substream | |
164 | * | |
165 | * This locks the PCM stream's spinlock or mutex depending on the nonatomic | |
166 | * flag of the given substream. This also takes the global link rw lock | |
167 | * (or rw sem), too, for avoiding the race with linked streams. | |
168 | */ | |
7af142f7 TI |
169 | void snd_pcm_stream_lock(struct snd_pcm_substream *substream) |
170 | { | |
10aa7cad | 171 | __snd_pcm_stream_lock_mode(substream, PCM_LOCK_DEFAULT); |
7af142f7 TI |
172 | } |
173 | EXPORT_SYMBOL_GPL(snd_pcm_stream_lock); | |
174 | ||
30b771cf TI |
175 | /** |
176 | * snd_pcm_stream_lock - Unlock the PCM stream | |
177 | * @substream: PCM substream | |
178 | * | |
179 | * This unlocks the PCM stream that has been locked via snd_pcm_stream_lock(). | |
180 | */ | |
7af142f7 TI |
181 | void snd_pcm_stream_unlock(struct snd_pcm_substream *substream) |
182 | { | |
10aa7cad | 183 | __snd_pcm_stream_unlock_mode(substream, PCM_LOCK_DEFAULT, 0); |
7af142f7 TI |
184 | } |
185 | EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock); | |
186 | ||
30b771cf TI |
187 | /** |
188 | * snd_pcm_stream_lock_irq - Lock the PCM stream | |
189 | * @substream: PCM substream | |
190 | * | |
191 | * This locks the PCM stream like snd_pcm_stream_lock() and disables the local | |
192 | * IRQ (only when nonatomic is false). In nonatomic case, this is identical | |
193 | * as snd_pcm_stream_lock(). | |
194 | */ | |
7af142f7 TI |
195 | void snd_pcm_stream_lock_irq(struct snd_pcm_substream *substream) |
196 | { | |
10aa7cad | 197 | __snd_pcm_stream_lock_mode(substream, PCM_LOCK_IRQ); |
7af142f7 TI |
198 | } |
199 | EXPORT_SYMBOL_GPL(snd_pcm_stream_lock_irq); | |
200 | ||
30b771cf TI |
201 | /** |
202 | * snd_pcm_stream_unlock_irq - Unlock the PCM stream | |
203 | * @substream: PCM substream | |
204 | * | |
205 | * This is a counter-part of snd_pcm_stream_lock_irq(). | |
206 | */ | |
7af142f7 TI |
207 | void snd_pcm_stream_unlock_irq(struct snd_pcm_substream *substream) |
208 | { | |
10aa7cad | 209 | __snd_pcm_stream_unlock_mode(substream, PCM_LOCK_IRQ, 0); |
7af142f7 TI |
210 | } |
211 | EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irq); | |
212 | ||
213 | unsigned long _snd_pcm_stream_lock_irqsave(struct snd_pcm_substream *substream) | |
214 | { | |
10aa7cad | 215 | return __snd_pcm_stream_lock_mode(substream, PCM_LOCK_IRQSAVE); |
7af142f7 TI |
216 | } |
217 | EXPORT_SYMBOL_GPL(_snd_pcm_stream_lock_irqsave); | |
218 | ||
30b771cf TI |
219 | /** |
220 | * snd_pcm_stream_unlock_irqrestore - Unlock the PCM stream | |
221 | * @substream: PCM substream | |
222 | * @flags: irq flags | |
223 | * | |
224 | * This is a counter-part of snd_pcm_stream_lock_irqsave(). | |
225 | */ | |
7af142f7 TI |
226 | void snd_pcm_stream_unlock_irqrestore(struct snd_pcm_substream *substream, |
227 | unsigned long flags) | |
228 | { | |
10aa7cad | 229 | __snd_pcm_stream_unlock_mode(substream, PCM_LOCK_IRQSAVE, flags); |
7af142f7 TI |
230 | } |
231 | EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irqrestore); | |
1da177e4 | 232 | |
877211f5 | 233 | int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info) |
1da177e4 | 234 | { |
877211f5 TI |
235 | struct snd_pcm *pcm = substream->pcm; |
236 | struct snd_pcm_str *pstr = substream->pstr; | |
1da177e4 | 237 | |
1da177e4 LT |
238 | memset(info, 0, sizeof(*info)); |
239 | info->card = pcm->card->number; | |
240 | info->device = pcm->device; | |
241 | info->stream = substream->stream; | |
242 | info->subdevice = substream->number; | |
243 | strlcpy(info->id, pcm->id, sizeof(info->id)); | |
244 | strlcpy(info->name, pcm->name, sizeof(info->name)); | |
245 | info->dev_class = pcm->dev_class; | |
246 | info->dev_subclass = pcm->dev_subclass; | |
247 | info->subdevices_count = pstr->substream_count; | |
248 | info->subdevices_avail = pstr->substream_count - pstr->substream_opened; | |
249 | strlcpy(info->subname, substream->name, sizeof(info->subname)); | |
e11f0f90 | 250 | |
1da177e4 LT |
251 | return 0; |
252 | } | |
253 | ||
877211f5 TI |
254 | int snd_pcm_info_user(struct snd_pcm_substream *substream, |
255 | struct snd_pcm_info __user * _info) | |
1da177e4 | 256 | { |
877211f5 | 257 | struct snd_pcm_info *info; |
1da177e4 LT |
258 | int err; |
259 | ||
260 | info = kmalloc(sizeof(*info), GFP_KERNEL); | |
261 | if (! info) | |
262 | return -ENOMEM; | |
263 | err = snd_pcm_info(substream, info); | |
264 | if (err >= 0) { | |
265 | if (copy_to_user(_info, info, sizeof(*info))) | |
266 | err = -EFAULT; | |
267 | } | |
268 | kfree(info); | |
269 | return err; | |
270 | } | |
271 | ||
63825f3a TI |
272 | static bool hw_support_mmap(struct snd_pcm_substream *substream) |
273 | { | |
274 | if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_MMAP)) | |
275 | return false; | |
85dc0f85 TI |
276 | /* architecture supports dma_mmap_coherent()? */ |
277 | #if defined(CONFIG_ARCH_NO_COHERENT_DMA_MMAP) || !defined(CONFIG_HAS_DMA) | |
63825f3a TI |
278 | if (!substream->ops->mmap && |
279 | substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV) | |
280 | return false; | |
281 | #endif | |
282 | return true; | |
283 | } | |
284 | ||
561e1cad TS |
285 | static int constrain_mask_params(struct snd_pcm_substream *substream, |
286 | struct snd_pcm_hw_params *params) | |
1da177e4 | 287 | { |
561e1cad TS |
288 | struct snd_pcm_hw_constraints *constrs = |
289 | &substream->runtime->hw_constraints; | |
290 | struct snd_mask *m; | |
1da177e4 | 291 | unsigned int k; |
561e1cad TS |
292 | struct snd_mask old_mask; |
293 | int changed; | |
1da177e4 LT |
294 | |
295 | for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) { | |
296 | m = hw_param_mask(params, k); | |
297 | if (snd_mask_empty(m)) | |
298 | return -EINVAL; | |
d81052f9 TS |
299 | |
300 | /* This parameter is not requested to change by a caller. */ | |
1da177e4 LT |
301 | if (!(params->rmask & (1 << k))) |
302 | continue; | |
561e1cad TS |
303 | |
304 | if (trace_hw_mask_param_enabled()) | |
305 | old_mask = *m; | |
306 | ||
1da177e4 | 307 | changed = snd_mask_refine(m, constrs_mask(constrs, k)); |
1da177e4 LT |
308 | if (changed < 0) |
309 | return changed; | |
82e7d501 TS |
310 | if (changed == 0) |
311 | continue; | |
561e1cad | 312 | |
d81052f9 | 313 | /* Set corresponding flag so that the caller gets it. */ |
82e7d501 TS |
314 | trace_hw_mask_param(substream, k, 0, &old_mask, m); |
315 | params->cmask |= 1 << k; | |
1da177e4 LT |
316 | } |
317 | ||
561e1cad TS |
318 | return 0; |
319 | } | |
320 | ||
3432fa04 TS |
321 | static int constrain_interval_params(struct snd_pcm_substream *substream, |
322 | struct snd_pcm_hw_params *params) | |
323 | { | |
324 | struct snd_pcm_hw_constraints *constrs = | |
325 | &substream->runtime->hw_constraints; | |
326 | struct snd_interval *i; | |
327 | unsigned int k; | |
328 | struct snd_interval old_interval; | |
329 | int changed; | |
330 | ||
1da177e4 LT |
331 | for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) { |
332 | i = hw_param_interval(params, k); | |
333 | if (snd_interval_empty(i)) | |
334 | return -EINVAL; | |
d81052f9 TS |
335 | |
336 | /* This parameter is not requested to change by a caller. */ | |
1da177e4 LT |
337 | if (!(params->rmask & (1 << k))) |
338 | continue; | |
3432fa04 TS |
339 | |
340 | if (trace_hw_interval_param_enabled()) | |
341 | old_interval = *i; | |
342 | ||
1da177e4 | 343 | changed = snd_interval_refine(i, constrs_interval(constrs, k)); |
1da177e4 LT |
344 | if (changed < 0) |
345 | return changed; | |
82e7d501 TS |
346 | if (changed == 0) |
347 | continue; | |
3432fa04 | 348 | |
d81052f9 | 349 | /* Set corresponding flag so that the caller gets it. */ |
82e7d501 TS |
350 | trace_hw_interval_param(substream, k, 0, &old_interval, i); |
351 | params->cmask |= 1 << k; | |
1da177e4 LT |
352 | } |
353 | ||
3432fa04 TS |
354 | return 0; |
355 | } | |
356 | ||
9cc07f55 TS |
357 | static int constrain_params_by_rules(struct snd_pcm_substream *substream, |
358 | struct snd_pcm_hw_params *params) | |
1da177e4 | 359 | { |
9cc07f55 TS |
360 | struct snd_pcm_hw_constraints *constrs = |
361 | &substream->runtime->hw_constraints; | |
1da177e4 | 362 | unsigned int k; |
5730f9f7 | 363 | unsigned int *rstamps; |
1da177e4 | 364 | unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1]; |
d81052f9 | 365 | unsigned int stamp; |
a1c06e39 TS |
366 | struct snd_pcm_hw_rule *r; |
367 | unsigned int d; | |
9cc07f55 TS |
368 | struct snd_mask old_mask; |
369 | struct snd_interval old_interval; | |
a1c06e39 | 370 | bool again; |
5730f9f7 | 371 | int changed, err = 0; |
1da177e4 | 372 | |
d81052f9 TS |
373 | /* |
374 | * Each application of rule has own sequence number. | |
375 | * | |
376 | * Each member of 'rstamps' array represents the sequence number of | |
377 | * recent application of corresponding rule. | |
378 | */ | |
5730f9f7 TI |
379 | rstamps = kcalloc(constrs->rules_num, sizeof(unsigned int), GFP_KERNEL); |
380 | if (!rstamps) | |
381 | return -ENOMEM; | |
d81052f9 TS |
382 | |
383 | /* | |
384 | * Each member of 'vstamps' array represents the sequence number of | |
385 | * recent application of rule in which corresponding parameters were | |
386 | * changed. | |
387 | * | |
388 | * In initial state, elements corresponding to parameters requested by | |
389 | * a caller is 1. For unrequested parameters, corresponding members | |
390 | * have 0 so that the parameters are never changed anymore. | |
391 | */ | |
9cc07f55 | 392 | for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) |
1da177e4 | 393 | vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0; |
d81052f9 TS |
394 | |
395 | /* Due to the above design, actual sequence number starts at 2. */ | |
396 | stamp = 2; | |
0d4e3999 | 397 | retry: |
d81052f9 | 398 | /* Apply all rules in order. */ |
a1c06e39 | 399 | again = false; |
0d4e3999 | 400 | for (k = 0; k < constrs->rules_num; k++) { |
a1c06e39 | 401 | r = &constrs->rules[k]; |
d81052f9 TS |
402 | |
403 | /* | |
404 | * Check condition bits of this rule. When the rule has | |
405 | * some condition bits, parameter without the bits is | |
406 | * never processed. SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP | |
407 | * is an example of the condition bits. | |
408 | */ | |
0d4e3999 TS |
409 | if (r->cond && !(r->cond & params->flags)) |
410 | continue; | |
d81052f9 TS |
411 | |
412 | /* | |
413 | * The 'deps' array includes maximum three dependencies | |
414 | * to SNDRV_PCM_HW_PARAM_XXXs for this rule. The fourth | |
415 | * member of this array is a sentinel and should be | |
416 | * negative value. | |
417 | * | |
418 | * This rule should be processed in this time when dependent | |
419 | * parameters were changed at former applications of the other | |
420 | * rules. | |
421 | */ | |
0d4e3999 | 422 | for (d = 0; r->deps[d] >= 0; d++) { |
d656b4a6 | 423 | if (vstamps[r->deps[d]] > rstamps[k]) |
0d4e3999 | 424 | break; |
0d4e3999 | 425 | } |
d656b4a6 | 426 | if (r->deps[d] < 0) |
0d4e3999 | 427 | continue; |
be4e31da | 428 | |
0d4e3999 TS |
429 | if (trace_hw_mask_param_enabled()) { |
430 | if (hw_is_mask(r->var)) | |
431 | old_mask = *hw_param_mask(params, r->var); | |
432 | } | |
433 | if (trace_hw_interval_param_enabled()) { | |
434 | if (hw_is_interval(r->var)) | |
435 | old_interval = *hw_param_interval(params, r->var); | |
436 | } | |
c6706de0 | 437 | |
0d4e3999 | 438 | changed = r->func(params, r); |
5730f9f7 TI |
439 | if (changed < 0) { |
440 | err = changed; | |
441 | goto out; | |
442 | } | |
c6706de0 | 443 | |
d81052f9 | 444 | /* |
82e7d501 | 445 | * When the parameter is changed, notify it to the caller |
d81052f9 TS |
446 | * by corresponding returned bit, then preparing for next |
447 | * iteration. | |
448 | */ | |
0d4e3999 | 449 | if (changed && r->var >= 0) { |
82e7d501 TS |
450 | if (hw_is_mask(r->var)) { |
451 | trace_hw_mask_param(substream, r->var, | |
452 | k + 1, &old_mask, | |
453 | hw_param_mask(params, r->var)); | |
1da177e4 | 454 | } |
82e7d501 TS |
455 | if (hw_is_interval(r->var)) { |
456 | trace_hw_interval_param(substream, r->var, | |
457 | k + 1, &old_interval, | |
458 | hw_param_interval(params, r->var)); | |
1da177e4 | 459 | } |
82e7d501 | 460 | |
0d4e3999 TS |
461 | params->cmask |= (1 << r->var); |
462 | vstamps[r->var] = stamp; | |
a1c06e39 | 463 | again = true; |
1da177e4 | 464 | } |
f74ae15f | 465 | |
82e7d501 | 466 | rstamps[k] = stamp++; |
0d4e3999 TS |
467 | } |
468 | ||
d81052f9 | 469 | /* Iterate to evaluate all rules till no parameters are changed. */ |
0d4e3999 TS |
470 | if (again) |
471 | goto retry; | |
9cc07f55 | 472 | |
5730f9f7 TI |
473 | out: |
474 | kfree(rstamps); | |
475 | return err; | |
9cc07f55 TS |
476 | } |
477 | ||
f9a076bf TS |
478 | static int fixup_unreferenced_params(struct snd_pcm_substream *substream, |
479 | struct snd_pcm_hw_params *params) | |
480 | { | |
481 | const struct snd_interval *i; | |
482 | const struct snd_mask *m; | |
483 | int err; | |
484 | ||
1da177e4 | 485 | if (!params->msbits) { |
f9a076bf | 486 | i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS); |
1da177e4 LT |
487 | if (snd_interval_single(i)) |
488 | params->msbits = snd_interval_value(i); | |
489 | } | |
490 | ||
491 | if (!params->rate_den) { | |
f9a076bf | 492 | i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE); |
1da177e4 LT |
493 | if (snd_interval_single(i)) { |
494 | params->rate_num = snd_interval_value(i); | |
495 | params->rate_den = 1; | |
496 | } | |
497 | } | |
498 | ||
f9a076bf TS |
499 | if (!params->fifo_size) { |
500 | m = hw_param_mask_c(params, SNDRV_PCM_HW_PARAM_FORMAT); | |
501 | i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS); | |
502 | if (snd_mask_single(m) && snd_interval_single(i)) { | |
503 | err = substream->ops->ioctl(substream, | |
504 | SNDRV_PCM_IOCTL1_FIFO_SIZE, params); | |
505 | if (err < 0) | |
506 | return err; | |
507 | } | |
508 | } | |
509 | ||
63825f3a | 510 | if (!params->info) { |
7802fb52 TS |
511 | params->info = substream->runtime->hw.info; |
512 | params->info &= ~(SNDRV_PCM_INFO_FIFO_IN_FRAMES | | |
513 | SNDRV_PCM_INFO_DRAIN_TRIGGER); | |
63825f3a TI |
514 | if (!hw_support_mmap(substream)) |
515 | params->info &= ~(SNDRV_PCM_INFO_MMAP | | |
516 | SNDRV_PCM_INFO_MMAP_VALID); | |
517 | } | |
7802fb52 | 518 | |
f9a076bf TS |
519 | return 0; |
520 | } | |
521 | ||
9cc07f55 TS |
522 | int snd_pcm_hw_refine(struct snd_pcm_substream *substream, |
523 | struct snd_pcm_hw_params *params) | |
524 | { | |
9cc07f55 TS |
525 | int err; |
526 | ||
527 | params->info = 0; | |
528 | params->fifo_size = 0; | |
529 | if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS)) | |
530 | params->msbits = 0; | |
531 | if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) { | |
532 | params->rate_num = 0; | |
533 | params->rate_den = 0; | |
8bea869c | 534 | } |
9cc07f55 TS |
535 | |
536 | err = constrain_mask_params(substream, params); | |
537 | if (err < 0) | |
538 | return err; | |
539 | ||
540 | err = constrain_interval_params(substream, params); | |
541 | if (err < 0) | |
542 | return err; | |
543 | ||
544 | err = constrain_params_by_rules(substream, params); | |
545 | if (err < 0) | |
546 | return err; | |
547 | ||
1da177e4 | 548 | params->rmask = 0; |
7802fb52 | 549 | |
1da177e4 LT |
550 | return 0; |
551 | } | |
e88e8ae6 TI |
552 | EXPORT_SYMBOL(snd_pcm_hw_refine); |
553 | ||
877211f5 TI |
554 | static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream, |
555 | struct snd_pcm_hw_params __user * _params) | |
1da177e4 | 556 | { |
877211f5 | 557 | struct snd_pcm_hw_params *params; |
1da177e4 LT |
558 | int err; |
559 | ||
ef44a1ec LZ |
560 | params = memdup_user(_params, sizeof(*params)); |
561 | if (IS_ERR(params)) | |
562 | return PTR_ERR(params); | |
563 | ||
1da177e4 | 564 | err = snd_pcm_hw_refine(substream, params); |
f74ae15f TS |
565 | if (err < 0) |
566 | goto end; | |
ef44a1ec | 567 | |
f74ae15f TS |
568 | err = fixup_unreferenced_params(substream, params); |
569 | if (err < 0) | |
570 | goto end; | |
ef44a1ec | 571 | |
f74ae15f TS |
572 | if (copy_to_user(_params, params, sizeof(*params))) |
573 | err = -EFAULT; | |
574 | end: | |
1da177e4 LT |
575 | kfree(params); |
576 | return err; | |
577 | } | |
578 | ||
9442e691 TI |
579 | static int period_to_usecs(struct snd_pcm_runtime *runtime) |
580 | { | |
581 | int usecs; | |
582 | ||
583 | if (! runtime->rate) | |
584 | return -1; /* invalid */ | |
585 | ||
586 | /* take 75% of period time as the deadline */ | |
587 | usecs = (750000 / runtime->rate) * runtime->period_size; | |
588 | usecs += ((750000 % runtime->rate) * runtime->period_size) / | |
589 | runtime->rate; | |
590 | ||
591 | return usecs; | |
592 | } | |
593 | ||
9b0573c0 TI |
594 | static void snd_pcm_set_state(struct snd_pcm_substream *substream, int state) |
595 | { | |
596 | snd_pcm_stream_lock_irq(substream); | |
597 | if (substream->runtime->status->state != SNDRV_PCM_STATE_DISCONNECTED) | |
598 | substream->runtime->status->state = state; | |
599 | snd_pcm_stream_unlock_irq(substream); | |
600 | } | |
601 | ||
90bbaf66 JY |
602 | static inline void snd_pcm_timer_notify(struct snd_pcm_substream *substream, |
603 | int event) | |
604 | { | |
605 | #ifdef CONFIG_SND_PCM_TIMER | |
606 | if (substream->timer) | |
607 | snd_timer_notify(substream->timer, event, | |
608 | &substream->runtime->trigger_tstamp); | |
609 | #endif | |
610 | } | |
611 | ||
60f96aae TS |
612 | /** |
613 | * snd_pcm_hw_param_choose - choose a configuration defined by @params | |
614 | * @pcm: PCM instance | |
615 | * @params: the hw_params instance | |
616 | * | |
617 | * Choose one configuration from configuration space defined by @params. | |
618 | * The configuration chosen is that obtained fixing in this order: | |
619 | * first access, first format, first subformat, min channels, | |
620 | * min rate, min period time, max buffer size, min tick time | |
621 | * | |
622 | * Return: Zero if successful, or a negative error code on failure. | |
623 | */ | |
624 | static int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm, | |
625 | struct snd_pcm_hw_params *params) | |
626 | { | |
627 | static const int vars[] = { | |
628 | SNDRV_PCM_HW_PARAM_ACCESS, | |
629 | SNDRV_PCM_HW_PARAM_FORMAT, | |
630 | SNDRV_PCM_HW_PARAM_SUBFORMAT, | |
631 | SNDRV_PCM_HW_PARAM_CHANNELS, | |
632 | SNDRV_PCM_HW_PARAM_RATE, | |
633 | SNDRV_PCM_HW_PARAM_PERIOD_TIME, | |
634 | SNDRV_PCM_HW_PARAM_BUFFER_SIZE, | |
635 | SNDRV_PCM_HW_PARAM_TICK_TIME, | |
636 | -1 | |
637 | }; | |
638 | const int *v; | |
7b8a54af TS |
639 | struct snd_mask old_mask; |
640 | struct snd_interval old_interval; | |
82e7d501 | 641 | int changed; |
60f96aae TS |
642 | |
643 | for (v = vars; *v != -1; v++) { | |
7b8a54af TS |
644 | /* Keep old parameter to trace. */ |
645 | if (trace_hw_mask_param_enabled()) { | |
646 | if (hw_is_mask(*v)) | |
647 | old_mask = *hw_param_mask(params, *v); | |
648 | } | |
649 | if (trace_hw_interval_param_enabled()) { | |
650 | if (hw_is_interval(*v)) | |
651 | old_interval = *hw_param_interval(params, *v); | |
652 | } | |
60f96aae | 653 | if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE) |
82e7d501 | 654 | changed = snd_pcm_hw_param_first(pcm, params, *v, NULL); |
60f96aae | 655 | else |
82e7d501 | 656 | changed = snd_pcm_hw_param_last(pcm, params, *v, NULL); |
e1a3a981 | 657 | if (changed < 0) |
82e7d501 TS |
658 | return changed; |
659 | if (changed == 0) | |
660 | continue; | |
7b8a54af | 661 | |
82e7d501 | 662 | /* Trace the changed parameter. */ |
7b8a54af TS |
663 | if (hw_is_mask(*v)) { |
664 | trace_hw_mask_param(pcm, *v, 0, &old_mask, | |
665 | hw_param_mask(params, *v)); | |
666 | } | |
667 | if (hw_is_interval(*v)) { | |
668 | trace_hw_interval_param(pcm, *v, 0, &old_interval, | |
669 | hw_param_interval(params, *v)); | |
670 | } | |
60f96aae | 671 | } |
7b8a54af | 672 | |
60f96aae TS |
673 | return 0; |
674 | } | |
675 | ||
877211f5 TI |
676 | static int snd_pcm_hw_params(struct snd_pcm_substream *substream, |
677 | struct snd_pcm_hw_params *params) | |
1da177e4 | 678 | { |
877211f5 | 679 | struct snd_pcm_runtime *runtime; |
9442e691 | 680 | int err, usecs; |
1da177e4 LT |
681 | unsigned int bits; |
682 | snd_pcm_uframes_t frames; | |
683 | ||
7eaa943c TI |
684 | if (PCM_RUNTIME_CHECK(substream)) |
685 | return -ENXIO; | |
1da177e4 | 686 | runtime = substream->runtime; |
1da177e4 LT |
687 | snd_pcm_stream_lock_irq(substream); |
688 | switch (runtime->status->state) { | |
689 | case SNDRV_PCM_STATE_OPEN: | |
690 | case SNDRV_PCM_STATE_SETUP: | |
691 | case SNDRV_PCM_STATE_PREPARED: | |
692 | break; | |
693 | default: | |
694 | snd_pcm_stream_unlock_irq(substream); | |
695 | return -EBADFD; | |
696 | } | |
697 | snd_pcm_stream_unlock_irq(substream); | |
8eeaa2f9 | 698 | #if IS_ENABLED(CONFIG_SND_PCM_OSS) |
1da177e4 LT |
699 | if (!substream->oss.oss) |
700 | #endif | |
9c323fcb | 701 | if (atomic_read(&substream->mmap_count)) |
1da177e4 LT |
702 | return -EBADFD; |
703 | ||
704 | params->rmask = ~0U; | |
705 | err = snd_pcm_hw_refine(substream, params); | |
706 | if (err < 0) | |
707 | goto _error; | |
708 | ||
709 | err = snd_pcm_hw_params_choose(substream, params); | |
710 | if (err < 0) | |
711 | goto _error; | |
712 | ||
f9a076bf TS |
713 | err = fixup_unreferenced_params(substream, params); |
714 | if (err < 0) | |
715 | goto _error; | |
716 | ||
1da177e4 LT |
717 | if (substream->ops->hw_params != NULL) { |
718 | err = substream->ops->hw_params(substream, params); | |
719 | if (err < 0) | |
720 | goto _error; | |
721 | } | |
722 | ||
723 | runtime->access = params_access(params); | |
724 | runtime->format = params_format(params); | |
725 | runtime->subformat = params_subformat(params); | |
726 | runtime->channels = params_channels(params); | |
727 | runtime->rate = params_rate(params); | |
728 | runtime->period_size = params_period_size(params); | |
729 | runtime->periods = params_periods(params); | |
730 | runtime->buffer_size = params_buffer_size(params); | |
1da177e4 LT |
731 | runtime->info = params->info; |
732 | runtime->rate_num = params->rate_num; | |
733 | runtime->rate_den = params->rate_den; | |
ab69a490 CL |
734 | runtime->no_period_wakeup = |
735 | (params->info & SNDRV_PCM_INFO_NO_PERIOD_WAKEUP) && | |
736 | (params->flags & SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP); | |
1da177e4 LT |
737 | |
738 | bits = snd_pcm_format_physical_width(runtime->format); | |
739 | runtime->sample_bits = bits; | |
740 | bits *= runtime->channels; | |
741 | runtime->frame_bits = bits; | |
742 | frames = 1; | |
743 | while (bits % 8 != 0) { | |
744 | bits *= 2; | |
745 | frames *= 2; | |
746 | } | |
747 | runtime->byte_align = bits / 8; | |
748 | runtime->min_align = frames; | |
749 | ||
750 | /* Default sw params */ | |
751 | runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE; | |
752 | runtime->period_step = 1; | |
1da177e4 | 753 | runtime->control->avail_min = runtime->period_size; |
1da177e4 LT |
754 | runtime->start_threshold = 1; |
755 | runtime->stop_threshold = runtime->buffer_size; | |
756 | runtime->silence_threshold = 0; | |
757 | runtime->silence_size = 0; | |
ead4046b CL |
758 | runtime->boundary = runtime->buffer_size; |
759 | while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size) | |
760 | runtime->boundary *= 2; | |
1da177e4 LT |
761 | |
762 | snd_pcm_timer_resolution_change(substream); | |
9b0573c0 | 763 | snd_pcm_set_state(substream, SNDRV_PCM_STATE_SETUP); |
9442e691 | 764 | |
82f68251 JB |
765 | if (pm_qos_request_active(&substream->latency_pm_qos_req)) |
766 | pm_qos_remove_request(&substream->latency_pm_qos_req); | |
9442e691 | 767 | if ((usecs = period_to_usecs(runtime)) >= 0) |
82f68251 JB |
768 | pm_qos_add_request(&substream->latency_pm_qos_req, |
769 | PM_QOS_CPU_DMA_LATENCY, usecs); | |
1da177e4 LT |
770 | return 0; |
771 | _error: | |
25985edc | 772 | /* hardware might be unusable from this time, |
1da177e4 LT |
773 | so we force application to retry to set |
774 | the correct hardware parameter settings */ | |
9b0573c0 | 775 | snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN); |
1da177e4 LT |
776 | if (substream->ops->hw_free != NULL) |
777 | substream->ops->hw_free(substream); | |
778 | return err; | |
779 | } | |
780 | ||
877211f5 TI |
781 | static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream, |
782 | struct snd_pcm_hw_params __user * _params) | |
1da177e4 | 783 | { |
877211f5 | 784 | struct snd_pcm_hw_params *params; |
1da177e4 LT |
785 | int err; |
786 | ||
ef44a1ec LZ |
787 | params = memdup_user(_params, sizeof(*params)); |
788 | if (IS_ERR(params)) | |
789 | return PTR_ERR(params); | |
790 | ||
1da177e4 | 791 | err = snd_pcm_hw_params(substream, params); |
f74ae15f TS |
792 | if (err < 0) |
793 | goto end; | |
ef44a1ec | 794 | |
f74ae15f TS |
795 | if (copy_to_user(_params, params, sizeof(*params))) |
796 | err = -EFAULT; | |
797 | end: | |
1da177e4 LT |
798 | kfree(params); |
799 | return err; | |
800 | } | |
801 | ||
877211f5 | 802 | static int snd_pcm_hw_free(struct snd_pcm_substream *substream) |
1da177e4 | 803 | { |
877211f5 | 804 | struct snd_pcm_runtime *runtime; |
1da177e4 LT |
805 | int result = 0; |
806 | ||
7eaa943c TI |
807 | if (PCM_RUNTIME_CHECK(substream)) |
808 | return -ENXIO; | |
1da177e4 | 809 | runtime = substream->runtime; |
1da177e4 LT |
810 | snd_pcm_stream_lock_irq(substream); |
811 | switch (runtime->status->state) { | |
812 | case SNDRV_PCM_STATE_SETUP: | |
813 | case SNDRV_PCM_STATE_PREPARED: | |
814 | break; | |
815 | default: | |
816 | snd_pcm_stream_unlock_irq(substream); | |
817 | return -EBADFD; | |
818 | } | |
819 | snd_pcm_stream_unlock_irq(substream); | |
9c323fcb | 820 | if (atomic_read(&substream->mmap_count)) |
1da177e4 LT |
821 | return -EBADFD; |
822 | if (substream->ops->hw_free) | |
823 | result = substream->ops->hw_free(substream); | |
9b0573c0 | 824 | snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN); |
82f68251 | 825 | pm_qos_remove_request(&substream->latency_pm_qos_req); |
1da177e4 LT |
826 | return result; |
827 | } | |
828 | ||
877211f5 TI |
829 | static int snd_pcm_sw_params(struct snd_pcm_substream *substream, |
830 | struct snd_pcm_sw_params *params) | |
1da177e4 | 831 | { |
877211f5 | 832 | struct snd_pcm_runtime *runtime; |
1250932e | 833 | int err; |
1da177e4 | 834 | |
7eaa943c TI |
835 | if (PCM_RUNTIME_CHECK(substream)) |
836 | return -ENXIO; | |
1da177e4 | 837 | runtime = substream->runtime; |
1da177e4 LT |
838 | snd_pcm_stream_lock_irq(substream); |
839 | if (runtime->status->state == SNDRV_PCM_STATE_OPEN) { | |
840 | snd_pcm_stream_unlock_irq(substream); | |
841 | return -EBADFD; | |
842 | } | |
843 | snd_pcm_stream_unlock_irq(substream); | |
844 | ||
145d92e7 DC |
845 | if (params->tstamp_mode < 0 || |
846 | params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST) | |
1da177e4 | 847 | return -EINVAL; |
58900810 TI |
848 | if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12) && |
849 | params->tstamp_type > SNDRV_PCM_TSTAMP_TYPE_LAST) | |
5646eda5 | 850 | return -EINVAL; |
1da177e4 LT |
851 | if (params->avail_min == 0) |
852 | return -EINVAL; | |
1da177e4 LT |
853 | if (params->silence_size >= runtime->boundary) { |
854 | if (params->silence_threshold != 0) | |
855 | return -EINVAL; | |
856 | } else { | |
857 | if (params->silence_size > params->silence_threshold) | |
858 | return -EINVAL; | |
859 | if (params->silence_threshold > runtime->buffer_size) | |
860 | return -EINVAL; | |
861 | } | |
1250932e | 862 | err = 0; |
1da177e4 LT |
863 | snd_pcm_stream_lock_irq(substream); |
864 | runtime->tstamp_mode = params->tstamp_mode; | |
58900810 TI |
865 | if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12)) |
866 | runtime->tstamp_type = params->tstamp_type; | |
1da177e4 LT |
867 | runtime->period_step = params->period_step; |
868 | runtime->control->avail_min = params->avail_min; | |
869 | runtime->start_threshold = params->start_threshold; | |
870 | runtime->stop_threshold = params->stop_threshold; | |
871 | runtime->silence_threshold = params->silence_threshold; | |
872 | runtime->silence_size = params->silence_size; | |
1da177e4 LT |
873 | params->boundary = runtime->boundary; |
874 | if (snd_pcm_running(substream)) { | |
1da177e4 LT |
875 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && |
876 | runtime->silence_size > 0) | |
877 | snd_pcm_playback_silence(substream, ULONG_MAX); | |
1250932e | 878 | err = snd_pcm_update_state(substream, runtime); |
1da177e4 LT |
879 | } |
880 | snd_pcm_stream_unlock_irq(substream); | |
1250932e | 881 | return err; |
1da177e4 LT |
882 | } |
883 | ||
877211f5 TI |
884 | static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream, |
885 | struct snd_pcm_sw_params __user * _params) | |
1da177e4 | 886 | { |
877211f5 | 887 | struct snd_pcm_sw_params params; |
1da177e4 LT |
888 | int err; |
889 | if (copy_from_user(¶ms, _params, sizeof(params))) | |
890 | return -EFAULT; | |
891 | err = snd_pcm_sw_params(substream, ¶ms); | |
892 | if (copy_to_user(_params, ¶ms, sizeof(params))) | |
893 | return -EFAULT; | |
894 | return err; | |
895 | } | |
896 | ||
c99c5a3b TI |
897 | static inline snd_pcm_uframes_t |
898 | snd_pcm_calc_delay(struct snd_pcm_substream *substream) | |
899 | { | |
900 | snd_pcm_uframes_t delay; | |
901 | ||
902 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) | |
903 | delay = snd_pcm_playback_hw_avail(substream->runtime); | |
904 | else | |
905 | delay = snd_pcm_capture_avail(substream->runtime); | |
906 | return delay + substream->runtime->delay; | |
907 | } | |
908 | ||
877211f5 TI |
909 | int snd_pcm_status(struct snd_pcm_substream *substream, |
910 | struct snd_pcm_status *status) | |
1da177e4 | 911 | { |
877211f5 | 912 | struct snd_pcm_runtime *runtime = substream->runtime; |
1da177e4 LT |
913 | |
914 | snd_pcm_stream_lock_irq(substream); | |
3179f620 PLB |
915 | |
916 | snd_pcm_unpack_audio_tstamp_config(status->audio_tstamp_data, | |
917 | &runtime->audio_tstamp_config); | |
918 | ||
919 | /* backwards compatible behavior */ | |
920 | if (runtime->audio_tstamp_config.type_requested == | |
921 | SNDRV_PCM_AUDIO_TSTAMP_TYPE_COMPAT) { | |
922 | if (runtime->hw.info & SNDRV_PCM_INFO_HAS_WALL_CLOCK) | |
923 | runtime->audio_tstamp_config.type_requested = | |
924 | SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK; | |
925 | else | |
926 | runtime->audio_tstamp_config.type_requested = | |
927 | SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT; | |
928 | runtime->audio_tstamp_report.valid = 0; | |
929 | } else | |
930 | runtime->audio_tstamp_report.valid = 1; | |
931 | ||
1da177e4 LT |
932 | status->state = runtime->status->state; |
933 | status->suspended_state = runtime->status->suspended_state; | |
934 | if (status->state == SNDRV_PCM_STATE_OPEN) | |
935 | goto _end; | |
936 | status->trigger_tstamp = runtime->trigger_tstamp; | |
8c121586 | 937 | if (snd_pcm_running(substream)) { |
1da177e4 | 938 | snd_pcm_update_hw_ptr(substream); |
8c121586 JK |
939 | if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) { |
940 | status->tstamp = runtime->status->tstamp; | |
3179f620 | 941 | status->driver_tstamp = runtime->driver_tstamp; |
4eeaaeae PLB |
942 | status->audio_tstamp = |
943 | runtime->status->audio_tstamp; | |
3179f620 PLB |
944 | if (runtime->audio_tstamp_report.valid == 1) |
945 | /* backwards compatibility, no report provided in COMPAT mode */ | |
946 | snd_pcm_pack_audio_tstamp_report(&status->audio_tstamp_data, | |
947 | &status->audio_tstamp_accuracy, | |
948 | &runtime->audio_tstamp_report); | |
949 | ||
8c121586 JK |
950 | goto _tstamp_end; |
951 | } | |
0d59b814 PLB |
952 | } else { |
953 | /* get tstamp only in fallback mode and only if enabled */ | |
954 | if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) | |
955 | snd_pcm_gettime(runtime, &status->tstamp); | |
8c121586 | 956 | } |
8c121586 | 957 | _tstamp_end: |
1da177e4 LT |
958 | status->appl_ptr = runtime->control->appl_ptr; |
959 | status->hw_ptr = runtime->status->hw_ptr; | |
763e5067 | 960 | status->avail = snd_pcm_avail(substream); |
c99c5a3b TI |
961 | status->delay = snd_pcm_running(substream) ? |
962 | snd_pcm_calc_delay(substream) : 0; | |
1da177e4 LT |
963 | status->avail_max = runtime->avail_max; |
964 | status->overrange = runtime->overrange; | |
965 | runtime->avail_max = 0; | |
966 | runtime->overrange = 0; | |
967 | _end: | |
968 | snd_pcm_stream_unlock_irq(substream); | |
969 | return 0; | |
970 | } | |
971 | ||
877211f5 | 972 | static int snd_pcm_status_user(struct snd_pcm_substream *substream, |
38ca2a4d PLB |
973 | struct snd_pcm_status __user * _status, |
974 | bool ext) | |
1da177e4 | 975 | { |
877211f5 | 976 | struct snd_pcm_status status; |
1da177e4 | 977 | int res; |
38ca2a4d | 978 | |
1da177e4 | 979 | memset(&status, 0, sizeof(status)); |
38ca2a4d PLB |
980 | /* |
981 | * with extension, parameters are read/write, | |
982 | * get audio_tstamp_data from user, | |
983 | * ignore rest of status structure | |
984 | */ | |
985 | if (ext && get_user(status.audio_tstamp_data, | |
986 | (u32 __user *)(&_status->audio_tstamp_data))) | |
987 | return -EFAULT; | |
1da177e4 LT |
988 | res = snd_pcm_status(substream, &status); |
989 | if (res < 0) | |
990 | return res; | |
991 | if (copy_to_user(_status, &status, sizeof(status))) | |
992 | return -EFAULT; | |
993 | return 0; | |
994 | } | |
995 | ||
877211f5 TI |
996 | static int snd_pcm_channel_info(struct snd_pcm_substream *substream, |
997 | struct snd_pcm_channel_info * info) | |
1da177e4 | 998 | { |
877211f5 | 999 | struct snd_pcm_runtime *runtime; |
1da177e4 LT |
1000 | unsigned int channel; |
1001 | ||
1da177e4 LT |
1002 | channel = info->channel; |
1003 | runtime = substream->runtime; | |
1004 | snd_pcm_stream_lock_irq(substream); | |
1005 | if (runtime->status->state == SNDRV_PCM_STATE_OPEN) { | |
1006 | snd_pcm_stream_unlock_irq(substream); | |
1007 | return -EBADFD; | |
1008 | } | |
1009 | snd_pcm_stream_unlock_irq(substream); | |
1010 | if (channel >= runtime->channels) | |
1011 | return -EINVAL; | |
1012 | memset(info, 0, sizeof(*info)); | |
1013 | info->channel = channel; | |
1014 | return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info); | |
1015 | } | |
1016 | ||
877211f5 TI |
1017 | static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream, |
1018 | struct snd_pcm_channel_info __user * _info) | |
1da177e4 | 1019 | { |
877211f5 | 1020 | struct snd_pcm_channel_info info; |
1da177e4 LT |
1021 | int res; |
1022 | ||
1023 | if (copy_from_user(&info, _info, sizeof(info))) | |
1024 | return -EFAULT; | |
1025 | res = snd_pcm_channel_info(substream, &info); | |
1026 | if (res < 0) | |
1027 | return res; | |
1028 | if (copy_to_user(_info, &info, sizeof(info))) | |
1029 | return -EFAULT; | |
1030 | return 0; | |
1031 | } | |
1032 | ||
877211f5 | 1033 | static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream) |
1da177e4 | 1034 | { |
877211f5 | 1035 | struct snd_pcm_runtime *runtime = substream->runtime; |
1da177e4 LT |
1036 | if (runtime->trigger_master == NULL) |
1037 | return; | |
1038 | if (runtime->trigger_master == substream) { | |
2b79d7a6 PLB |
1039 | if (!runtime->trigger_tstamp_latched) |
1040 | snd_pcm_gettime(runtime, &runtime->trigger_tstamp); | |
1da177e4 LT |
1041 | } else { |
1042 | snd_pcm_trigger_tstamp(runtime->trigger_master); | |
1043 | runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp; | |
1044 | } | |
1045 | runtime->trigger_master = NULL; | |
1046 | } | |
1047 | ||
1048 | struct action_ops { | |
877211f5 TI |
1049 | int (*pre_action)(struct snd_pcm_substream *substream, int state); |
1050 | int (*do_action)(struct snd_pcm_substream *substream, int state); | |
1051 | void (*undo_action)(struct snd_pcm_substream *substream, int state); | |
1052 | void (*post_action)(struct snd_pcm_substream *substream, int state); | |
1da177e4 LT |
1053 | }; |
1054 | ||
1055 | /* | |
1056 | * this functions is core for handling of linked stream | |
1057 | * Note: the stream state might be changed also on failure | |
1058 | * Note2: call with calling stream lock + link lock | |
1059 | */ | |
b17154cf | 1060 | static int snd_pcm_action_group(const struct action_ops *ops, |
877211f5 | 1061 | struct snd_pcm_substream *substream, |
1da177e4 LT |
1062 | int state, int do_lock) |
1063 | { | |
877211f5 TI |
1064 | struct snd_pcm_substream *s = NULL; |
1065 | struct snd_pcm_substream *s1; | |
dde1c652 | 1066 | int res = 0, depth = 1; |
1da177e4 | 1067 | |
ef991b95 | 1068 | snd_pcm_group_for_each_entry(s, substream) { |
257f8cce TI |
1069 | if (do_lock && s != substream) { |
1070 | if (s->pcm->nonatomic) | |
dde1c652 | 1071 | mutex_lock_nested(&s->self_group.mutex, depth); |
257f8cce | 1072 | else |
dde1c652 TI |
1073 | spin_lock_nested(&s->self_group.lock, depth); |
1074 | depth++; | |
257f8cce | 1075 | } |
1da177e4 LT |
1076 | res = ops->pre_action(s, state); |
1077 | if (res < 0) | |
1078 | goto _unlock; | |
1079 | } | |
ef991b95 | 1080 | snd_pcm_group_for_each_entry(s, substream) { |
1da177e4 LT |
1081 | res = ops->do_action(s, state); |
1082 | if (res < 0) { | |
1083 | if (ops->undo_action) { | |
ef991b95 | 1084 | snd_pcm_group_for_each_entry(s1, substream) { |
1da177e4 LT |
1085 | if (s1 == s) /* failed stream */ |
1086 | break; | |
1087 | ops->undo_action(s1, state); | |
1088 | } | |
1089 | } | |
1090 | s = NULL; /* unlock all */ | |
1091 | goto _unlock; | |
1092 | } | |
1093 | } | |
ef991b95 | 1094 | snd_pcm_group_for_each_entry(s, substream) { |
1da177e4 LT |
1095 | ops->post_action(s, state); |
1096 | } | |
1097 | _unlock: | |
1098 | if (do_lock) { | |
1099 | /* unlock streams */ | |
ef991b95 | 1100 | snd_pcm_group_for_each_entry(s1, substream) { |
257f8cce | 1101 | if (s1 != substream) { |
811deede | 1102 | if (s1->pcm->nonatomic) |
257f8cce TI |
1103 | mutex_unlock(&s1->self_group.mutex); |
1104 | else | |
1105 | spin_unlock(&s1->self_group.lock); | |
1106 | } | |
1da177e4 LT |
1107 | if (s1 == s) /* end */ |
1108 | break; | |
1109 | } | |
1110 | } | |
1111 | return res; | |
1112 | } | |
1113 | ||
1114 | /* | |
1115 | * Note: call with stream lock | |
1116 | */ | |
b17154cf | 1117 | static int snd_pcm_action_single(const struct action_ops *ops, |
877211f5 | 1118 | struct snd_pcm_substream *substream, |
1da177e4 LT |
1119 | int state) |
1120 | { | |
1121 | int res; | |
1122 | ||
1123 | res = ops->pre_action(substream, state); | |
1124 | if (res < 0) | |
1125 | return res; | |
1126 | res = ops->do_action(substream, state); | |
1127 | if (res == 0) | |
1128 | ops->post_action(substream, state); | |
1129 | else if (ops->undo_action) | |
1130 | ops->undo_action(substream, state); | |
1131 | return res; | |
1132 | } | |
1133 | ||
a41c4cb9 TI |
1134 | static void snd_pcm_group_assign(struct snd_pcm_substream *substream, |
1135 | struct snd_pcm_group *new_group) | |
1136 | { | |
1137 | substream->group = new_group; | |
1138 | list_move(&substream->link_list, &new_group->substreams); | |
1139 | } | |
1140 | ||
aa8edd8c TI |
1141 | /* |
1142 | * Note: call with stream lock | |
1143 | */ | |
b17154cf | 1144 | static int snd_pcm_action(const struct action_ops *ops, |
aa8edd8c TI |
1145 | struct snd_pcm_substream *substream, |
1146 | int state) | |
257f8cce TI |
1147 | { |
1148 | int res; | |
1149 | ||
aa8edd8c TI |
1150 | if (!snd_pcm_stream_linked(substream)) |
1151 | return snd_pcm_action_single(ops, substream, state); | |
1152 | ||
1153 | if (substream->pcm->nonatomic) { | |
257f8cce TI |
1154 | if (!mutex_trylock(&substream->group->mutex)) { |
1155 | mutex_unlock(&substream->self_group.mutex); | |
1156 | mutex_lock(&substream->group->mutex); | |
1157 | mutex_lock(&substream->self_group.mutex); | |
1158 | } | |
1159 | res = snd_pcm_action_group(ops, substream, state, 1); | |
1160 | mutex_unlock(&substream->group->mutex); | |
1161 | } else { | |
1da177e4 LT |
1162 | if (!spin_trylock(&substream->group->lock)) { |
1163 | spin_unlock(&substream->self_group.lock); | |
1164 | spin_lock(&substream->group->lock); | |
1165 | spin_lock(&substream->self_group.lock); | |
1166 | } | |
1167 | res = snd_pcm_action_group(ops, substream, state, 1); | |
1168 | spin_unlock(&substream->group->lock); | |
1da177e4 LT |
1169 | } |
1170 | return res; | |
1171 | } | |
1172 | ||
1173 | /* | |
1174 | * Note: don't use any locks before | |
1175 | */ | |
b17154cf | 1176 | static int snd_pcm_action_lock_irq(const struct action_ops *ops, |
877211f5 | 1177 | struct snd_pcm_substream *substream, |
1da177e4 LT |
1178 | int state) |
1179 | { | |
1180 | int res; | |
1181 | ||
e3a4bd5e TI |
1182 | snd_pcm_stream_lock_irq(substream); |
1183 | res = snd_pcm_action(ops, substream, state); | |
1184 | snd_pcm_stream_unlock_irq(substream); | |
1da177e4 LT |
1185 | return res; |
1186 | } | |
1187 | ||
1188 | /* | |
1189 | */ | |
b17154cf | 1190 | static int snd_pcm_action_nonatomic(const struct action_ops *ops, |
877211f5 | 1191 | struct snd_pcm_substream *substream, |
1da177e4 LT |
1192 | int state) |
1193 | { | |
1194 | int res; | |
1195 | ||
1196 | down_read(&snd_pcm_link_rwsem); | |
1197 | if (snd_pcm_stream_linked(substream)) | |
1198 | res = snd_pcm_action_group(ops, substream, state, 0); | |
1199 | else | |
1200 | res = snd_pcm_action_single(ops, substream, state); | |
1201 | up_read(&snd_pcm_link_rwsem); | |
1202 | return res; | |
1203 | } | |
1204 | ||
1205 | /* | |
1206 | * start callbacks | |
1207 | */ | |
877211f5 | 1208 | static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state) |
1da177e4 | 1209 | { |
877211f5 | 1210 | struct snd_pcm_runtime *runtime = substream->runtime; |
1da177e4 LT |
1211 | if (runtime->status->state != SNDRV_PCM_STATE_PREPARED) |
1212 | return -EBADFD; | |
1213 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && | |
1214 | !snd_pcm_playback_data(substream)) | |
1215 | return -EPIPE; | |
2b79d7a6 | 1216 | runtime->trigger_tstamp_latched = false; |
1da177e4 LT |
1217 | runtime->trigger_master = substream; |
1218 | return 0; | |
1219 | } | |
1220 | ||
877211f5 | 1221 | static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state) |
1da177e4 LT |
1222 | { |
1223 | if (substream->runtime->trigger_master != substream) | |
1224 | return 0; | |
1225 | return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START); | |
1226 | } | |
1227 | ||
877211f5 | 1228 | static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state) |
1da177e4 LT |
1229 | { |
1230 | if (substream->runtime->trigger_master == substream) | |
1231 | substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP); | |
1232 | } | |
1233 | ||
877211f5 | 1234 | static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state) |
1da177e4 | 1235 | { |
877211f5 | 1236 | struct snd_pcm_runtime *runtime = substream->runtime; |
1da177e4 | 1237 | snd_pcm_trigger_tstamp(substream); |
6af3fb72 | 1238 | runtime->hw_ptr_jiffies = jiffies; |
bd76af0f JK |
1239 | runtime->hw_ptr_buffer_jiffies = (runtime->buffer_size * HZ) / |
1240 | runtime->rate; | |
1da177e4 LT |
1241 | runtime->status->state = state; |
1242 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && | |
1243 | runtime->silence_size > 0) | |
1244 | snd_pcm_playback_silence(substream, ULONG_MAX); | |
90bbaf66 | 1245 | snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTART); |
1da177e4 LT |
1246 | } |
1247 | ||
b17154cf | 1248 | static const struct action_ops snd_pcm_action_start = { |
1da177e4 LT |
1249 | .pre_action = snd_pcm_pre_start, |
1250 | .do_action = snd_pcm_do_start, | |
1251 | .undo_action = snd_pcm_undo_start, | |
1252 | .post_action = snd_pcm_post_start | |
1253 | }; | |
1254 | ||
1255 | /** | |
1c85cc64 | 1256 | * snd_pcm_start - start all linked streams |
df8db936 | 1257 | * @substream: the PCM substream instance |
eb7c06e8 YB |
1258 | * |
1259 | * Return: Zero if successful, or a negative error code. | |
c2c86a97 | 1260 | * The stream lock must be acquired before calling this function. |
1da177e4 | 1261 | */ |
877211f5 | 1262 | int snd_pcm_start(struct snd_pcm_substream *substream) |
1da177e4 | 1263 | { |
877211f5 TI |
1264 | return snd_pcm_action(&snd_pcm_action_start, substream, |
1265 | SNDRV_PCM_STATE_RUNNING); | |
1da177e4 LT |
1266 | } |
1267 | ||
c2c86a97 TI |
1268 | /* take the stream lock and start the streams */ |
1269 | static int snd_pcm_start_lock_irq(struct snd_pcm_substream *substream) | |
1270 | { | |
1271 | return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, | |
1272 | SNDRV_PCM_STATE_RUNNING); | |
1273 | } | |
1274 | ||
1da177e4 LT |
1275 | /* |
1276 | * stop callbacks | |
1277 | */ | |
877211f5 | 1278 | static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state) |
1da177e4 | 1279 | { |
877211f5 | 1280 | struct snd_pcm_runtime *runtime = substream->runtime; |
1da177e4 LT |
1281 | if (runtime->status->state == SNDRV_PCM_STATE_OPEN) |
1282 | return -EBADFD; | |
1283 | runtime->trigger_master = substream; | |
1284 | return 0; | |
1285 | } | |
1286 | ||
877211f5 | 1287 | static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state) |
1da177e4 LT |
1288 | { |
1289 | if (substream->runtime->trigger_master == substream && | |
1290 | snd_pcm_running(substream)) | |
1291 | substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP); | |
1292 | return 0; /* unconditonally stop all substreams */ | |
1293 | } | |
1294 | ||
877211f5 | 1295 | static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state) |
1da177e4 | 1296 | { |
877211f5 | 1297 | struct snd_pcm_runtime *runtime = substream->runtime; |
1da177e4 LT |
1298 | if (runtime->status->state != state) { |
1299 | snd_pcm_trigger_tstamp(substream); | |
9bc889b4 | 1300 | runtime->status->state = state; |
90bbaf66 | 1301 | snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTOP); |
1da177e4 LT |
1302 | } |
1303 | wake_up(&runtime->sleep); | |
c91a988d | 1304 | wake_up(&runtime->tsleep); |
1da177e4 LT |
1305 | } |
1306 | ||
b17154cf | 1307 | static const struct action_ops snd_pcm_action_stop = { |
1da177e4 LT |
1308 | .pre_action = snd_pcm_pre_stop, |
1309 | .do_action = snd_pcm_do_stop, | |
1310 | .post_action = snd_pcm_post_stop | |
1311 | }; | |
1312 | ||
1313 | /** | |
1c85cc64 | 1314 | * snd_pcm_stop - try to stop all running streams in the substream group |
df8db936 TI |
1315 | * @substream: the PCM substream instance |
1316 | * @state: PCM state after stopping the stream | |
1da177e4 | 1317 | * |
1c85cc64 | 1318 | * The state of each stream is then changed to the given state unconditionally. |
eb7c06e8 | 1319 | * |
0a11458c | 1320 | * Return: Zero if successful, or a negative error code. |
1da177e4 | 1321 | */ |
fea952e5 | 1322 | int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t state) |
1da177e4 LT |
1323 | { |
1324 | return snd_pcm_action(&snd_pcm_action_stop, substream, state); | |
1325 | } | |
e88e8ae6 TI |
1326 | EXPORT_SYMBOL(snd_pcm_stop); |
1327 | ||
1da177e4 | 1328 | /** |
1c85cc64 | 1329 | * snd_pcm_drain_done - stop the DMA only when the given stream is playback |
df8db936 | 1330 | * @substream: the PCM substream |
1da177e4 | 1331 | * |
1c85cc64 | 1332 | * After stopping, the state is changed to SETUP. |
1da177e4 | 1333 | * Unlike snd_pcm_stop(), this affects only the given stream. |
eb7c06e8 YB |
1334 | * |
1335 | * Return: Zero if succesful, or a negative error code. | |
1da177e4 | 1336 | */ |
877211f5 | 1337 | int snd_pcm_drain_done(struct snd_pcm_substream *substream) |
1da177e4 | 1338 | { |
877211f5 TI |
1339 | return snd_pcm_action_single(&snd_pcm_action_stop, substream, |
1340 | SNDRV_PCM_STATE_SETUP); | |
1da177e4 LT |
1341 | } |
1342 | ||
1fb8510c TI |
1343 | /** |
1344 | * snd_pcm_stop_xrun - stop the running streams as XRUN | |
1345 | * @substream: the PCM substream instance | |
1fb8510c TI |
1346 | * |
1347 | * This stops the given running substream (and all linked substreams) as XRUN. | |
1348 | * Unlike snd_pcm_stop(), this function takes the substream lock by itself. | |
1349 | * | |
1350 | * Return: Zero if successful, or a negative error code. | |
1351 | */ | |
1352 | int snd_pcm_stop_xrun(struct snd_pcm_substream *substream) | |
1353 | { | |
1354 | unsigned long flags; | |
1fb8510c TI |
1355 | |
1356 | snd_pcm_stream_lock_irqsave(substream, flags); | |
e647f5a5 | 1357 | if (substream->runtime && snd_pcm_running(substream)) |
9cd641ed | 1358 | __snd_pcm_xrun(substream); |
1fb8510c | 1359 | snd_pcm_stream_unlock_irqrestore(substream, flags); |
9cd641ed | 1360 | return 0; |
1fb8510c TI |
1361 | } |
1362 | EXPORT_SYMBOL_GPL(snd_pcm_stop_xrun); | |
1363 | ||
1da177e4 LT |
1364 | /* |
1365 | * pause callbacks | |
1366 | */ | |
877211f5 | 1367 | static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push) |
1da177e4 | 1368 | { |
877211f5 | 1369 | struct snd_pcm_runtime *runtime = substream->runtime; |
1da177e4 LT |
1370 | if (!(runtime->info & SNDRV_PCM_INFO_PAUSE)) |
1371 | return -ENOSYS; | |
1372 | if (push) { | |
1373 | if (runtime->status->state != SNDRV_PCM_STATE_RUNNING) | |
1374 | return -EBADFD; | |
1375 | } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED) | |
1376 | return -EBADFD; | |
1377 | runtime->trigger_master = substream; | |
1378 | return 0; | |
1379 | } | |
1380 | ||
877211f5 | 1381 | static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push) |
1da177e4 LT |
1382 | { |
1383 | if (substream->runtime->trigger_master != substream) | |
1384 | return 0; | |
56385a12 JK |
1385 | /* some drivers might use hw_ptr to recover from the pause - |
1386 | update the hw_ptr now */ | |
1387 | if (push) | |
1388 | snd_pcm_update_hw_ptr(substream); | |
6af3fb72 | 1389 | /* The jiffies check in snd_pcm_update_hw_ptr*() is done by |
b595076a | 1390 | * a delta between the current jiffies, this gives a large enough |
6af3fb72 TI |
1391 | * delta, effectively to skip the check once. |
1392 | */ | |
1393 | substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000; | |
1da177e4 LT |
1394 | return substream->ops->trigger(substream, |
1395 | push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH : | |
1396 | SNDRV_PCM_TRIGGER_PAUSE_RELEASE); | |
1397 | } | |
1398 | ||
877211f5 | 1399 | static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push) |
1da177e4 LT |
1400 | { |
1401 | if (substream->runtime->trigger_master == substream) | |
1402 | substream->ops->trigger(substream, | |
1403 | push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE : | |
1404 | SNDRV_PCM_TRIGGER_PAUSE_PUSH); | |
1405 | } | |
1406 | ||
877211f5 | 1407 | static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push) |
1da177e4 | 1408 | { |
877211f5 | 1409 | struct snd_pcm_runtime *runtime = substream->runtime; |
1da177e4 LT |
1410 | snd_pcm_trigger_tstamp(substream); |
1411 | if (push) { | |
1412 | runtime->status->state = SNDRV_PCM_STATE_PAUSED; | |
90bbaf66 | 1413 | snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MPAUSE); |
1da177e4 | 1414 | wake_up(&runtime->sleep); |
c91a988d | 1415 | wake_up(&runtime->tsleep); |
1da177e4 LT |
1416 | } else { |
1417 | runtime->status->state = SNDRV_PCM_STATE_RUNNING; | |
90bbaf66 | 1418 | snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MCONTINUE); |
1da177e4 LT |
1419 | } |
1420 | } | |
1421 | ||
b17154cf | 1422 | static const struct action_ops snd_pcm_action_pause = { |
1da177e4 LT |
1423 | .pre_action = snd_pcm_pre_pause, |
1424 | .do_action = snd_pcm_do_pause, | |
1425 | .undo_action = snd_pcm_undo_pause, | |
1426 | .post_action = snd_pcm_post_pause | |
1427 | }; | |
1428 | ||
1429 | /* | |
1430 | * Push/release the pause for all linked streams. | |
1431 | */ | |
877211f5 | 1432 | static int snd_pcm_pause(struct snd_pcm_substream *substream, int push) |
1da177e4 LT |
1433 | { |
1434 | return snd_pcm_action(&snd_pcm_action_pause, substream, push); | |
1435 | } | |
1436 | ||
1437 | #ifdef CONFIG_PM | |
1438 | /* suspend */ | |
1439 | ||
877211f5 | 1440 | static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state) |
1da177e4 | 1441 | { |
877211f5 | 1442 | struct snd_pcm_runtime *runtime = substream->runtime; |
1da177e4 LT |
1443 | if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) |
1444 | return -EBUSY; | |
1445 | runtime->trigger_master = substream; | |
1446 | return 0; | |
1447 | } | |
1448 | ||
877211f5 | 1449 | static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state) |
1da177e4 | 1450 | { |
877211f5 | 1451 | struct snd_pcm_runtime *runtime = substream->runtime; |
1da177e4 LT |
1452 | if (runtime->trigger_master != substream) |
1453 | return 0; | |
1454 | if (! snd_pcm_running(substream)) | |
1455 | return 0; | |
1456 | substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND); | |
1457 | return 0; /* suspend unconditionally */ | |
1458 | } | |
1459 | ||
877211f5 | 1460 | static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state) |
1da177e4 | 1461 | { |
877211f5 | 1462 | struct snd_pcm_runtime *runtime = substream->runtime; |
1da177e4 | 1463 | snd_pcm_trigger_tstamp(substream); |
9bc889b4 TI |
1464 | runtime->status->suspended_state = runtime->status->state; |
1465 | runtime->status->state = SNDRV_PCM_STATE_SUSPENDED; | |
90bbaf66 | 1466 | snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSUSPEND); |
1da177e4 | 1467 | wake_up(&runtime->sleep); |
c91a988d | 1468 | wake_up(&runtime->tsleep); |
1da177e4 LT |
1469 | } |
1470 | ||
b17154cf | 1471 | static const struct action_ops snd_pcm_action_suspend = { |
1da177e4 LT |
1472 | .pre_action = snd_pcm_pre_suspend, |
1473 | .do_action = snd_pcm_do_suspend, | |
1474 | .post_action = snd_pcm_post_suspend | |
1475 | }; | |
1476 | ||
1477 | /** | |
1c85cc64 | 1478 | * snd_pcm_suspend - trigger SUSPEND to all linked streams |
df8db936 | 1479 | * @substream: the PCM substream |
1da177e4 | 1480 | * |
1da177e4 | 1481 | * After this call, all streams are changed to SUSPENDED state. |
eb7c06e8 YB |
1482 | * |
1483 | * Return: Zero if successful (or @substream is %NULL), or a negative error | |
1484 | * code. | |
1da177e4 | 1485 | */ |
877211f5 | 1486 | int snd_pcm_suspend(struct snd_pcm_substream *substream) |
1da177e4 LT |
1487 | { |
1488 | int err; | |
1489 | unsigned long flags; | |
1490 | ||
603bf524 TI |
1491 | if (! substream) |
1492 | return 0; | |
1493 | ||
1da177e4 LT |
1494 | snd_pcm_stream_lock_irqsave(substream, flags); |
1495 | err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0); | |
1496 | snd_pcm_stream_unlock_irqrestore(substream, flags); | |
1497 | return err; | |
1498 | } | |
e88e8ae6 TI |
1499 | EXPORT_SYMBOL(snd_pcm_suspend); |
1500 | ||
1da177e4 | 1501 | /** |
1c85cc64 | 1502 | * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm |
df8db936 | 1503 | * @pcm: the PCM instance |
1da177e4 | 1504 | * |
1da177e4 | 1505 | * After this call, all streams are changed to SUSPENDED state. |
eb7c06e8 YB |
1506 | * |
1507 | * Return: Zero if successful (or @pcm is %NULL), or a negative error code. | |
1da177e4 | 1508 | */ |
877211f5 | 1509 | int snd_pcm_suspend_all(struct snd_pcm *pcm) |
1da177e4 | 1510 | { |
877211f5 | 1511 | struct snd_pcm_substream *substream; |
1da177e4 LT |
1512 | int stream, err = 0; |
1513 | ||
603bf524 TI |
1514 | if (! pcm) |
1515 | return 0; | |
1516 | ||
1da177e4 | 1517 | for (stream = 0; stream < 2; stream++) { |
877211f5 TI |
1518 | for (substream = pcm->streams[stream].substream; |
1519 | substream; substream = substream->next) { | |
1da177e4 LT |
1520 | /* FIXME: the open/close code should lock this as well */ |
1521 | if (substream->runtime == NULL) | |
1522 | continue; | |
1523 | err = snd_pcm_suspend(substream); | |
1524 | if (err < 0 && err != -EBUSY) | |
1525 | return err; | |
1526 | } | |
1527 | } | |
1528 | return 0; | |
1529 | } | |
e88e8ae6 TI |
1530 | EXPORT_SYMBOL(snd_pcm_suspend_all); |
1531 | ||
1da177e4 LT |
1532 | /* resume */ |
1533 | ||
877211f5 | 1534 | static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state) |
1da177e4 | 1535 | { |
877211f5 | 1536 | struct snd_pcm_runtime *runtime = substream->runtime; |
1da177e4 LT |
1537 | if (!(runtime->info & SNDRV_PCM_INFO_RESUME)) |
1538 | return -ENOSYS; | |
1539 | runtime->trigger_master = substream; | |
1540 | return 0; | |
1541 | } | |
1542 | ||
877211f5 | 1543 | static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state) |
1da177e4 | 1544 | { |
877211f5 | 1545 | struct snd_pcm_runtime *runtime = substream->runtime; |
1da177e4 LT |
1546 | if (runtime->trigger_master != substream) |
1547 | return 0; | |
1548 | /* DMA not running previously? */ | |
1549 | if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING && | |
1550 | (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING || | |
1551 | substream->stream != SNDRV_PCM_STREAM_PLAYBACK)) | |
1552 | return 0; | |
1553 | return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME); | |
1554 | } | |
1555 | ||
877211f5 | 1556 | static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state) |
1da177e4 LT |
1557 | { |
1558 | if (substream->runtime->trigger_master == substream && | |
1559 | snd_pcm_running(substream)) | |
1560 | substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND); | |
1561 | } | |
1562 | ||
877211f5 | 1563 | static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state) |
1da177e4 | 1564 | { |
877211f5 | 1565 | struct snd_pcm_runtime *runtime = substream->runtime; |
1da177e4 | 1566 | snd_pcm_trigger_tstamp(substream); |
9bc889b4 | 1567 | runtime->status->state = runtime->status->suspended_state; |
90bbaf66 | 1568 | snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MRESUME); |
1da177e4 LT |
1569 | } |
1570 | ||
b17154cf | 1571 | static const struct action_ops snd_pcm_action_resume = { |
1da177e4 LT |
1572 | .pre_action = snd_pcm_pre_resume, |
1573 | .do_action = snd_pcm_do_resume, | |
1574 | .undo_action = snd_pcm_undo_resume, | |
1575 | .post_action = snd_pcm_post_resume | |
1576 | }; | |
1577 | ||
877211f5 | 1578 | static int snd_pcm_resume(struct snd_pcm_substream *substream) |
1da177e4 | 1579 | { |
68b4acd3 | 1580 | return snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0); |
1da177e4 LT |
1581 | } |
1582 | ||
1583 | #else | |
1584 | ||
877211f5 | 1585 | static int snd_pcm_resume(struct snd_pcm_substream *substream) |
1da177e4 LT |
1586 | { |
1587 | return -ENOSYS; | |
1588 | } | |
1589 | ||
1590 | #endif /* CONFIG_PM */ | |
1591 | ||
1592 | /* | |
1593 | * xrun ioctl | |
1594 | * | |
1595 | * Change the RUNNING stream(s) to XRUN state. | |
1596 | */ | |
877211f5 | 1597 | static int snd_pcm_xrun(struct snd_pcm_substream *substream) |
1da177e4 | 1598 | { |
877211f5 | 1599 | struct snd_pcm_runtime *runtime = substream->runtime; |
1da177e4 LT |
1600 | int result; |
1601 | ||
1da177e4 LT |
1602 | snd_pcm_stream_lock_irq(substream); |
1603 | switch (runtime->status->state) { | |
1604 | case SNDRV_PCM_STATE_XRUN: | |
1605 | result = 0; /* already there */ | |
1606 | break; | |
1607 | case SNDRV_PCM_STATE_RUNNING: | |
9cd641ed TI |
1608 | __snd_pcm_xrun(substream); |
1609 | result = 0; | |
1da177e4 LT |
1610 | break; |
1611 | default: | |
1612 | result = -EBADFD; | |
1613 | } | |
1614 | snd_pcm_stream_unlock_irq(substream); | |
1da177e4 LT |
1615 | return result; |
1616 | } | |
1617 | ||
1618 | /* | |
1619 | * reset ioctl | |
1620 | */ | |
877211f5 | 1621 | static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state) |
1da177e4 | 1622 | { |
877211f5 | 1623 | struct snd_pcm_runtime *runtime = substream->runtime; |
1da177e4 LT |
1624 | switch (runtime->status->state) { |
1625 | case SNDRV_PCM_STATE_RUNNING: | |
1626 | case SNDRV_PCM_STATE_PREPARED: | |
1627 | case SNDRV_PCM_STATE_PAUSED: | |
1628 | case SNDRV_PCM_STATE_SUSPENDED: | |
1629 | return 0; | |
1630 | default: | |
1631 | return -EBADFD; | |
1632 | } | |
1633 | } | |
1634 | ||
877211f5 | 1635 | static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state) |
1da177e4 | 1636 | { |
877211f5 | 1637 | struct snd_pcm_runtime *runtime = substream->runtime; |
1da177e4 LT |
1638 | int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL); |
1639 | if (err < 0) | |
1640 | return err; | |
1da177e4 | 1641 | runtime->hw_ptr_base = 0; |
e7636925 JK |
1642 | runtime->hw_ptr_interrupt = runtime->status->hw_ptr - |
1643 | runtime->status->hw_ptr % runtime->period_size; | |
1da177e4 LT |
1644 | runtime->silence_start = runtime->status->hw_ptr; |
1645 | runtime->silence_filled = 0; | |
1646 | return 0; | |
1647 | } | |
1648 | ||
877211f5 | 1649 | static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state) |
1da177e4 | 1650 | { |
877211f5 | 1651 | struct snd_pcm_runtime *runtime = substream->runtime; |
1da177e4 LT |
1652 | runtime->control->appl_ptr = runtime->status->hw_ptr; |
1653 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && | |
1654 | runtime->silence_size > 0) | |
1655 | snd_pcm_playback_silence(substream, ULONG_MAX); | |
1656 | } | |
1657 | ||
b17154cf | 1658 | static const struct action_ops snd_pcm_action_reset = { |
1da177e4 LT |
1659 | .pre_action = snd_pcm_pre_reset, |
1660 | .do_action = snd_pcm_do_reset, | |
1661 | .post_action = snd_pcm_post_reset | |
1662 | }; | |
1663 | ||
877211f5 | 1664 | static int snd_pcm_reset(struct snd_pcm_substream *substream) |
1da177e4 LT |
1665 | { |
1666 | return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0); | |
1667 | } | |
1668 | ||
1669 | /* | |
1670 | * prepare ioctl | |
1671 | */ | |
0df63e44 TI |
1672 | /* we use the second argument for updating f_flags */ |
1673 | static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream, | |
1674 | int f_flags) | |
1da177e4 | 1675 | { |
877211f5 | 1676 | struct snd_pcm_runtime *runtime = substream->runtime; |
de1b8b93 TI |
1677 | if (runtime->status->state == SNDRV_PCM_STATE_OPEN || |
1678 | runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED) | |
1da177e4 LT |
1679 | return -EBADFD; |
1680 | if (snd_pcm_running(substream)) | |
1681 | return -EBUSY; | |
0df63e44 | 1682 | substream->f_flags = f_flags; |
1da177e4 LT |
1683 | return 0; |
1684 | } | |
1685 | ||
877211f5 | 1686 | static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state) |
1da177e4 LT |
1687 | { |
1688 | int err; | |
1689 | err = substream->ops->prepare(substream); | |
1690 | if (err < 0) | |
1691 | return err; | |
1692 | return snd_pcm_do_reset(substream, 0); | |
1693 | } | |
1694 | ||
877211f5 | 1695 | static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state) |
1da177e4 | 1696 | { |
877211f5 | 1697 | struct snd_pcm_runtime *runtime = substream->runtime; |
1da177e4 | 1698 | runtime->control->appl_ptr = runtime->status->hw_ptr; |
9b0573c0 | 1699 | snd_pcm_set_state(substream, SNDRV_PCM_STATE_PREPARED); |
1da177e4 LT |
1700 | } |
1701 | ||
b17154cf | 1702 | static const struct action_ops snd_pcm_action_prepare = { |
1da177e4 LT |
1703 | .pre_action = snd_pcm_pre_prepare, |
1704 | .do_action = snd_pcm_do_prepare, | |
1705 | .post_action = snd_pcm_post_prepare | |
1706 | }; | |
1707 | ||
1708 | /** | |
1c85cc64 | 1709 | * snd_pcm_prepare - prepare the PCM substream to be triggerable |
df8db936 | 1710 | * @substream: the PCM substream instance |
0df63e44 | 1711 | * @file: file to refer f_flags |
eb7c06e8 YB |
1712 | * |
1713 | * Return: Zero if successful, or a negative error code. | |
1da177e4 | 1714 | */ |
0df63e44 TI |
1715 | static int snd_pcm_prepare(struct snd_pcm_substream *substream, |
1716 | struct file *file) | |
1da177e4 | 1717 | { |
0df63e44 TI |
1718 | int f_flags; |
1719 | ||
1720 | if (file) | |
1721 | f_flags = file->f_flags; | |
1722 | else | |
1723 | f_flags = substream->f_flags; | |
1da177e4 | 1724 | |
1b745cd9 TI |
1725 | snd_pcm_stream_lock_irq(substream); |
1726 | switch (substream->runtime->status->state) { | |
1727 | case SNDRV_PCM_STATE_PAUSED: | |
1728 | snd_pcm_pause(substream, 0); | |
1729 | /* fallthru */ | |
1730 | case SNDRV_PCM_STATE_SUSPENDED: | |
1731 | snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP); | |
1732 | break; | |
1733 | } | |
1734 | snd_pcm_stream_unlock_irq(substream); | |
1735 | ||
68b4acd3 TI |
1736 | return snd_pcm_action_nonatomic(&snd_pcm_action_prepare, |
1737 | substream, f_flags); | |
1da177e4 LT |
1738 | } |
1739 | ||
1740 | /* | |
1741 | * drain ioctl | |
1742 | */ | |
1743 | ||
877211f5 | 1744 | static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state) |
1da177e4 | 1745 | { |
4f7c39dc TI |
1746 | struct snd_pcm_runtime *runtime = substream->runtime; |
1747 | switch (runtime->status->state) { | |
1748 | case SNDRV_PCM_STATE_OPEN: | |
1749 | case SNDRV_PCM_STATE_DISCONNECTED: | |
1750 | case SNDRV_PCM_STATE_SUSPENDED: | |
1751 | return -EBADFD; | |
1752 | } | |
1753 | runtime->trigger_master = substream; | |
1da177e4 LT |
1754 | return 0; |
1755 | } | |
1756 | ||
877211f5 | 1757 | static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state) |
1da177e4 | 1758 | { |
877211f5 | 1759 | struct snd_pcm_runtime *runtime = substream->runtime; |
1da177e4 LT |
1760 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
1761 | switch (runtime->status->state) { | |
1762 | case SNDRV_PCM_STATE_PREPARED: | |
1763 | /* start playback stream if possible */ | |
1764 | if (! snd_pcm_playback_empty(substream)) { | |
1765 | snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING); | |
1766 | snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING); | |
70372a75 TI |
1767 | } else { |
1768 | runtime->status->state = SNDRV_PCM_STATE_SETUP; | |
1da177e4 LT |
1769 | } |
1770 | break; | |
1771 | case SNDRV_PCM_STATE_RUNNING: | |
1772 | runtime->status->state = SNDRV_PCM_STATE_DRAINING; | |
1773 | break; | |
4f7c39dc TI |
1774 | case SNDRV_PCM_STATE_XRUN: |
1775 | runtime->status->state = SNDRV_PCM_STATE_SETUP; | |
1776 | break; | |
1da177e4 LT |
1777 | default: |
1778 | break; | |
1779 | } | |
1780 | } else { | |
1781 | /* stop running stream */ | |
1782 | if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) { | |
b05e5787 | 1783 | int new_state = snd_pcm_capture_avail(runtime) > 0 ? |
1da177e4 | 1784 | SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP; |
b05e5787 MŚ |
1785 | snd_pcm_do_stop(substream, new_state); |
1786 | snd_pcm_post_stop(substream, new_state); | |
1da177e4 LT |
1787 | } |
1788 | } | |
48d88297 LY |
1789 | |
1790 | if (runtime->status->state == SNDRV_PCM_STATE_DRAINING && | |
1791 | runtime->trigger_master == substream && | |
1792 | (runtime->hw.info & SNDRV_PCM_INFO_DRAIN_TRIGGER)) | |
1793 | return substream->ops->trigger(substream, | |
1794 | SNDRV_PCM_TRIGGER_DRAIN); | |
1795 | ||
1da177e4 LT |
1796 | return 0; |
1797 | } | |
1798 | ||
877211f5 | 1799 | static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state) |
1da177e4 LT |
1800 | { |
1801 | } | |
1802 | ||
b17154cf | 1803 | static const struct action_ops snd_pcm_action_drain_init = { |
1da177e4 LT |
1804 | .pre_action = snd_pcm_pre_drain_init, |
1805 | .do_action = snd_pcm_do_drain_init, | |
1806 | .post_action = snd_pcm_post_drain_init | |
1807 | }; | |
1808 | ||
877211f5 | 1809 | static int snd_pcm_drop(struct snd_pcm_substream *substream); |
1da177e4 LT |
1810 | |
1811 | /* | |
1812 | * Drain the stream(s). | |
1813 | * When the substream is linked, sync until the draining of all playback streams | |
1814 | * is finished. | |
1815 | * After this call, all streams are supposed to be either SETUP or DRAINING | |
1816 | * (capture only) state. | |
1817 | */ | |
4cdc115f TI |
1818 | static int snd_pcm_drain(struct snd_pcm_substream *substream, |
1819 | struct file *file) | |
1da177e4 | 1820 | { |
877211f5 TI |
1821 | struct snd_card *card; |
1822 | struct snd_pcm_runtime *runtime; | |
ef991b95 | 1823 | struct snd_pcm_substream *s; |
ac6424b9 | 1824 | wait_queue_entry_t wait; |
1da177e4 | 1825 | int result = 0; |
4cdc115f | 1826 | int nonblock = 0; |
1da177e4 | 1827 | |
1da177e4 LT |
1828 | card = substream->pcm->card; |
1829 | runtime = substream->runtime; | |
1830 | ||
1831 | if (runtime->status->state == SNDRV_PCM_STATE_OPEN) | |
1832 | return -EBADFD; | |
1833 | ||
4cdc115f TI |
1834 | if (file) { |
1835 | if (file->f_flags & O_NONBLOCK) | |
1836 | nonblock = 1; | |
1837 | } else if (substream->f_flags & O_NONBLOCK) | |
1838 | nonblock = 1; | |
1839 | ||
21cb2a2e | 1840 | down_read(&snd_pcm_link_rwsem); |
21cb2a2e TI |
1841 | snd_pcm_stream_lock_irq(substream); |
1842 | /* resume pause */ | |
d3a7dcfe | 1843 | if (runtime->status->state == SNDRV_PCM_STATE_PAUSED) |
21cb2a2e TI |
1844 | snd_pcm_pause(substream, 0); |
1845 | ||
1846 | /* pre-start/stop - all running streams are changed to DRAINING state */ | |
1847 | result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0); | |
4cdc115f TI |
1848 | if (result < 0) |
1849 | goto unlock; | |
1850 | /* in non-blocking, we don't wait in ioctl but let caller poll */ | |
1851 | if (nonblock) { | |
1852 | result = -EAGAIN; | |
1853 | goto unlock; | |
21cb2a2e | 1854 | } |
1da177e4 LT |
1855 | |
1856 | for (;;) { | |
1857 | long tout; | |
d3a7dcfe | 1858 | struct snd_pcm_runtime *to_check; |
1da177e4 LT |
1859 | if (signal_pending(current)) { |
1860 | result = -ERESTARTSYS; | |
1861 | break; | |
1862 | } | |
d3a7dcfe TI |
1863 | /* find a substream to drain */ |
1864 | to_check = NULL; | |
1865 | snd_pcm_group_for_each_entry(s, substream) { | |
1866 | if (s->stream != SNDRV_PCM_STREAM_PLAYBACK) | |
1867 | continue; | |
1868 | runtime = s->runtime; | |
1869 | if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) { | |
1870 | to_check = runtime; | |
21cb2a2e | 1871 | break; |
d3a7dcfe | 1872 | } |
21cb2a2e | 1873 | } |
d3a7dcfe TI |
1874 | if (!to_check) |
1875 | break; /* all drained */ | |
1876 | init_waitqueue_entry(&wait, current); | |
1877 | add_wait_queue(&to_check->sleep, &wait); | |
1da177e4 | 1878 | snd_pcm_stream_unlock_irq(substream); |
d3a7dcfe | 1879 | up_read(&snd_pcm_link_rwsem); |
f2b3614c TI |
1880 | if (runtime->no_period_wakeup) |
1881 | tout = MAX_SCHEDULE_TIMEOUT; | |
1882 | else { | |
1883 | tout = 10; | |
1884 | if (runtime->rate) { | |
1885 | long t = runtime->period_size * 2 / runtime->rate; | |
1886 | tout = max(t, tout); | |
1887 | } | |
1888 | tout = msecs_to_jiffies(tout * 1000); | |
1889 | } | |
1890 | tout = schedule_timeout_interruptible(tout); | |
d3a7dcfe | 1891 | down_read(&snd_pcm_link_rwsem); |
1da177e4 | 1892 | snd_pcm_stream_lock_irq(substream); |
d3a7dcfe | 1893 | remove_wait_queue(&to_check->sleep, &wait); |
0914f796 TI |
1894 | if (card->shutdown) { |
1895 | result = -ENODEV; | |
1896 | break; | |
1897 | } | |
1da177e4 LT |
1898 | if (tout == 0) { |
1899 | if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) | |
1900 | result = -ESTRPIPE; | |
1901 | else { | |
09e56df8 TI |
1902 | dev_dbg(substream->pcm->card->dev, |
1903 | "playback drain error (DMA or IRQ trouble?)\n"); | |
1da177e4 LT |
1904 | snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP); |
1905 | result = -EIO; | |
1906 | } | |
1907 | break; | |
1908 | } | |
1da177e4 | 1909 | } |
21cb2a2e | 1910 | |
4cdc115f | 1911 | unlock: |
21cb2a2e | 1912 | snd_pcm_stream_unlock_irq(substream); |
d3a7dcfe | 1913 | up_read(&snd_pcm_link_rwsem); |
1da177e4 LT |
1914 | |
1915 | return result; | |
1916 | } | |
1917 | ||
1918 | /* | |
1919 | * drop ioctl | |
1920 | * | |
1921 | * Immediately put all linked substreams into SETUP state. | |
1922 | */ | |
877211f5 | 1923 | static int snd_pcm_drop(struct snd_pcm_substream *substream) |
1da177e4 | 1924 | { |
877211f5 | 1925 | struct snd_pcm_runtime *runtime; |
1da177e4 LT |
1926 | int result = 0; |
1927 | ||
7eaa943c TI |
1928 | if (PCM_RUNTIME_CHECK(substream)) |
1929 | return -ENXIO; | |
1da177e4 | 1930 | runtime = substream->runtime; |
1da177e4 | 1931 | |
de1b8b93 | 1932 | if (runtime->status->state == SNDRV_PCM_STATE_OPEN || |
4b95ff78 | 1933 | runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED) |
1da177e4 LT |
1934 | return -EBADFD; |
1935 | ||
1da177e4 LT |
1936 | snd_pcm_stream_lock_irq(substream); |
1937 | /* resume pause */ | |
1938 | if (runtime->status->state == SNDRV_PCM_STATE_PAUSED) | |
1939 | snd_pcm_pause(substream, 0); | |
1940 | ||
1941 | snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP); | |
1942 | /* runtime->control->appl_ptr = runtime->status->hw_ptr; */ | |
1943 | snd_pcm_stream_unlock_irq(substream); | |
24e8fc49 | 1944 | |
1da177e4 LT |
1945 | return result; |
1946 | } | |
1947 | ||
1948 | ||
0888c321 | 1949 | static bool is_pcm_file(struct file *file) |
1da177e4 | 1950 | { |
0888c321 | 1951 | struct inode *inode = file_inode(file); |
d819fb21 | 1952 | struct snd_pcm *pcm; |
f87135f5 CL |
1953 | unsigned int minor; |
1954 | ||
0888c321 AV |
1955 | if (!S_ISCHR(inode->i_mode) || imajor(inode) != snd_major) |
1956 | return false; | |
1da177e4 | 1957 | minor = iminor(inode); |
d819fb21 TI |
1958 | pcm = snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK); |
1959 | if (!pcm) | |
1960 | pcm = snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE); | |
1961 | if (!pcm) | |
1962 | return false; | |
1963 | snd_card_unref(pcm->card); | |
1964 | return true; | |
1da177e4 LT |
1965 | } |
1966 | ||
1967 | /* | |
1968 | * PCM link handling | |
1969 | */ | |
877211f5 | 1970 | static int snd_pcm_link(struct snd_pcm_substream *substream, int fd) |
1da177e4 LT |
1971 | { |
1972 | int res = 0; | |
877211f5 TI |
1973 | struct snd_pcm_file *pcm_file; |
1974 | struct snd_pcm_substream *substream1; | |
1662591b | 1975 | struct snd_pcm_group *group; |
0888c321 | 1976 | struct fd f = fdget(fd); |
1da177e4 | 1977 | |
0888c321 | 1978 | if (!f.file) |
1da177e4 | 1979 | return -EBADFD; |
0888c321 AV |
1980 | if (!is_pcm_file(f.file)) { |
1981 | res = -EBADFD; | |
1982 | goto _badf; | |
1983 | } | |
1984 | pcm_file = f.file->private_data; | |
1da177e4 | 1985 | substream1 = pcm_file->substream; |
73365cb1 | 1986 | group = kzalloc(sizeof(*group), GFP_KERNEL); |
1662591b TI |
1987 | if (!group) { |
1988 | res = -ENOMEM; | |
1989 | goto _nolock; | |
1990 | } | |
73365cb1 | 1991 | snd_pcm_group_init(group); |
b888a5f7 | 1992 | down_write_nonfifo(&snd_pcm_link_rwsem); |
1da177e4 LT |
1993 | write_lock_irq(&snd_pcm_link_rwlock); |
1994 | if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN || | |
257f8cce TI |
1995 | substream->runtime->status->state != substream1->runtime->status->state || |
1996 | substream->pcm->nonatomic != substream1->pcm->nonatomic) { | |
1da177e4 LT |
1997 | res = -EBADFD; |
1998 | goto _end; | |
1999 | } | |
2000 | if (snd_pcm_stream_linked(substream1)) { | |
2001 | res = -EALREADY; | |
2002 | goto _end; | |
2003 | } | |
2004 | if (!snd_pcm_stream_linked(substream)) { | |
a41c4cb9 | 2005 | snd_pcm_group_assign(substream, group); |
dd6c5cd8 | 2006 | group = NULL; |
1da177e4 | 2007 | } |
a41c4cb9 | 2008 | snd_pcm_group_assign(substream1, substream->group); |
1da177e4 LT |
2009 | _end: |
2010 | write_unlock_irq(&snd_pcm_link_rwlock); | |
2011 | up_write(&snd_pcm_link_rwsem); | |
1662591b | 2012 | _nolock: |
dd6c5cd8 | 2013 | kfree(group); |
0888c321 AV |
2014 | _badf: |
2015 | fdput(f); | |
1da177e4 LT |
2016 | return res; |
2017 | } | |
2018 | ||
877211f5 | 2019 | static void relink_to_local(struct snd_pcm_substream *substream) |
1da177e4 | 2020 | { |
a41c4cb9 | 2021 | snd_pcm_group_assign(substream, &substream->self_group); |
1da177e4 LT |
2022 | } |
2023 | ||
877211f5 | 2024 | static int snd_pcm_unlink(struct snd_pcm_substream *substream) |
1da177e4 | 2025 | { |
a41c4cb9 | 2026 | struct snd_pcm_group *group; |
1da177e4 LT |
2027 | int res = 0; |
2028 | ||
b888a5f7 | 2029 | down_write_nonfifo(&snd_pcm_link_rwsem); |
1da177e4 LT |
2030 | write_lock_irq(&snd_pcm_link_rwlock); |
2031 | if (!snd_pcm_stream_linked(substream)) { | |
2032 | res = -EALREADY; | |
2033 | goto _end; | |
2034 | } | |
a41c4cb9 TI |
2035 | |
2036 | group = substream->group; | |
2037 | ||
2038 | relink_to_local(substream); | |
2039 | ||
2040 | /* detach the last stream, too */ | |
2041 | if (list_is_singular(&group->substreams)) { | |
7df5a5f6 TI |
2042 | relink_to_local(list_first_entry(&group->substreams, |
2043 | struct snd_pcm_substream, | |
2044 | link_list)); | |
a41c4cb9 | 2045 | kfree(group); |
1da177e4 | 2046 | } |
a41c4cb9 | 2047 | |
1da177e4 LT |
2048 | _end: |
2049 | write_unlock_irq(&snd_pcm_link_rwlock); | |
2050 | up_write(&snd_pcm_link_rwsem); | |
2051 | return res; | |
2052 | } | |
2053 | ||
2054 | /* | |
2055 | * hw configurator | |
2056 | */ | |
877211f5 TI |
2057 | static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params, |
2058 | struct snd_pcm_hw_rule *rule) | |
1da177e4 | 2059 | { |
877211f5 | 2060 | struct snd_interval t; |
1da177e4 LT |
2061 | snd_interval_mul(hw_param_interval_c(params, rule->deps[0]), |
2062 | hw_param_interval_c(params, rule->deps[1]), &t); | |
2063 | return snd_interval_refine(hw_param_interval(params, rule->var), &t); | |
2064 | } | |
2065 | ||
877211f5 TI |
2066 | static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params, |
2067 | struct snd_pcm_hw_rule *rule) | |
1da177e4 | 2068 | { |
877211f5 | 2069 | struct snd_interval t; |
1da177e4 LT |
2070 | snd_interval_div(hw_param_interval_c(params, rule->deps[0]), |
2071 | hw_param_interval_c(params, rule->deps[1]), &t); | |
2072 | return snd_interval_refine(hw_param_interval(params, rule->var), &t); | |
2073 | } | |
2074 | ||
877211f5 TI |
2075 | static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params, |
2076 | struct snd_pcm_hw_rule *rule) | |
1da177e4 | 2077 | { |
877211f5 | 2078 | struct snd_interval t; |
1da177e4 LT |
2079 | snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]), |
2080 | hw_param_interval_c(params, rule->deps[1]), | |
2081 | (unsigned long) rule->private, &t); | |
2082 | return snd_interval_refine(hw_param_interval(params, rule->var), &t); | |
2083 | } | |
2084 | ||
877211f5 TI |
2085 | static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params, |
2086 | struct snd_pcm_hw_rule *rule) | |
1da177e4 | 2087 | { |
877211f5 | 2088 | struct snd_interval t; |
1da177e4 LT |
2089 | snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]), |
2090 | (unsigned long) rule->private, | |
2091 | hw_param_interval_c(params, rule->deps[1]), &t); | |
2092 | return snd_interval_refine(hw_param_interval(params, rule->var), &t); | |
2093 | } | |
2094 | ||
877211f5 TI |
2095 | static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params, |
2096 | struct snd_pcm_hw_rule *rule) | |
1da177e4 LT |
2097 | { |
2098 | unsigned int k; | |
b55f9fdc TS |
2099 | const struct snd_interval *i = |
2100 | hw_param_interval_c(params, rule->deps[0]); | |
877211f5 TI |
2101 | struct snd_mask m; |
2102 | struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); | |
1da177e4 LT |
2103 | snd_mask_any(&m); |
2104 | for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) { | |
2105 | int bits; | |
2106 | if (! snd_mask_test(mask, k)) | |
2107 | continue; | |
2108 | bits = snd_pcm_format_physical_width(k); | |
2109 | if (bits <= 0) | |
2110 | continue; /* ignore invalid formats */ | |
2111 | if ((unsigned)bits < i->min || (unsigned)bits > i->max) | |
2112 | snd_mask_reset(&m, k); | |
2113 | } | |
2114 | return snd_mask_refine(mask, &m); | |
2115 | } | |
2116 | ||
877211f5 TI |
2117 | static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params, |
2118 | struct snd_pcm_hw_rule *rule) | |
1da177e4 | 2119 | { |
877211f5 | 2120 | struct snd_interval t; |
1da177e4 LT |
2121 | unsigned int k; |
2122 | t.min = UINT_MAX; | |
2123 | t.max = 0; | |
2124 | t.openmin = 0; | |
2125 | t.openmax = 0; | |
2126 | for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) { | |
2127 | int bits; | |
2128 | if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k)) | |
2129 | continue; | |
2130 | bits = snd_pcm_format_physical_width(k); | |
2131 | if (bits <= 0) | |
2132 | continue; /* ignore invalid formats */ | |
2133 | if (t.min > (unsigned)bits) | |
2134 | t.min = bits; | |
2135 | if (t.max < (unsigned)bits) | |
2136 | t.max = bits; | |
2137 | } | |
2138 | t.integer = 1; | |
2139 | return snd_interval_refine(hw_param_interval(params, rule->var), &t); | |
2140 | } | |
2141 | ||
2142 | #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12 | |
2143 | #error "Change this table" | |
2144 | #endif | |
2145 | ||
8b674308 TS |
2146 | static const unsigned int rates[] = { |
2147 | 5512, 8000, 11025, 16000, 22050, 32000, 44100, | |
2148 | 48000, 64000, 88200, 96000, 176400, 192000 | |
2149 | }; | |
1da177e4 | 2150 | |
7653d557 CL |
2151 | const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = { |
2152 | .count = ARRAY_SIZE(rates), | |
2153 | .list = rates, | |
2154 | }; | |
2155 | ||
877211f5 TI |
2156 | static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params, |
2157 | struct snd_pcm_hw_rule *rule) | |
1da177e4 | 2158 | { |
877211f5 | 2159 | struct snd_pcm_hardware *hw = rule->private; |
1da177e4 | 2160 | return snd_interval_list(hw_param_interval(params, rule->var), |
7653d557 CL |
2161 | snd_pcm_known_rates.count, |
2162 | snd_pcm_known_rates.list, hw->rates); | |
1da177e4 LT |
2163 | } |
2164 | ||
877211f5 TI |
2165 | static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params, |
2166 | struct snd_pcm_hw_rule *rule) | |
1da177e4 | 2167 | { |
877211f5 TI |
2168 | struct snd_interval t; |
2169 | struct snd_pcm_substream *substream = rule->private; | |
1da177e4 LT |
2170 | t.min = 0; |
2171 | t.max = substream->buffer_bytes_max; | |
2172 | t.openmin = 0; | |
2173 | t.openmax = 0; | |
2174 | t.integer = 1; | |
2175 | return snd_interval_refine(hw_param_interval(params, rule->var), &t); | |
2176 | } | |
2177 | ||
877211f5 | 2178 | int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream) |
1da177e4 | 2179 | { |
877211f5 TI |
2180 | struct snd_pcm_runtime *runtime = substream->runtime; |
2181 | struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints; | |
1da177e4 LT |
2182 | int k, err; |
2183 | ||
2184 | for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) { | |
2185 | snd_mask_any(constrs_mask(constrs, k)); | |
2186 | } | |
2187 | ||
2188 | for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) { | |
2189 | snd_interval_any(constrs_interval(constrs, k)); | |
2190 | } | |
2191 | ||
2192 | snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS)); | |
2193 | snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE)); | |
2194 | snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES)); | |
2195 | snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS)); | |
2196 | snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS)); | |
2197 | ||
2198 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT, | |
2199 | snd_pcm_hw_rule_format, NULL, | |
2200 | SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1); | |
2201 | if (err < 0) | |
2202 | return err; | |
2203 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, | |
2204 | snd_pcm_hw_rule_sample_bits, NULL, | |
2205 | SNDRV_PCM_HW_PARAM_FORMAT, | |
2206 | SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1); | |
2207 | if (err < 0) | |
2208 | return err; | |
2209 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, | |
2210 | snd_pcm_hw_rule_div, NULL, | |
2211 | SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1); | |
2212 | if (err < 0) | |
2213 | return err; | |
2214 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS, | |
2215 | snd_pcm_hw_rule_mul, NULL, | |
2216 | SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1); | |
2217 | if (err < 0) | |
2218 | return err; | |
2219 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS, | |
2220 | snd_pcm_hw_rule_mulkdiv, (void*) 8, | |
2221 | SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1); | |
2222 | if (err < 0) | |
2223 | return err; | |
2224 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS, | |
2225 | snd_pcm_hw_rule_mulkdiv, (void*) 8, | |
2226 | SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1); | |
2227 | if (err < 0) | |
2228 | return err; | |
2229 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, | |
2230 | snd_pcm_hw_rule_div, NULL, | |
2231 | SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1); | |
2232 | if (err < 0) | |
2233 | return err; | |
2234 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, | |
2235 | snd_pcm_hw_rule_mulkdiv, (void*) 1000000, | |
2236 | SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1); | |
2237 | if (err < 0) | |
2238 | return err; | |
2239 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, | |
2240 | snd_pcm_hw_rule_mulkdiv, (void*) 1000000, | |
2241 | SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1); | |
2242 | if (err < 0) | |
2243 | return err; | |
2244 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS, | |
2245 | snd_pcm_hw_rule_div, NULL, | |
2246 | SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1); | |
2247 | if (err < 0) | |
2248 | return err; | |
2249 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, | |
2250 | snd_pcm_hw_rule_div, NULL, | |
2251 | SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1); | |
2252 | if (err < 0) | |
2253 | return err; | |
2254 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, | |
2255 | snd_pcm_hw_rule_mulkdiv, (void*) 8, | |
2256 | SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1); | |
2257 | if (err < 0) | |
2258 | return err; | |
2259 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, | |
2260 | snd_pcm_hw_rule_muldivk, (void*) 1000000, | |
2261 | SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1); | |
2262 | if (err < 0) | |
2263 | return err; | |
2264 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, | |
2265 | snd_pcm_hw_rule_mul, NULL, | |
2266 | SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1); | |
2267 | if (err < 0) | |
2268 | return err; | |
2269 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, | |
2270 | snd_pcm_hw_rule_mulkdiv, (void*) 8, | |
2271 | SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1); | |
2272 | if (err < 0) | |
2273 | return err; | |
2274 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, | |
2275 | snd_pcm_hw_rule_muldivk, (void*) 1000000, | |
2276 | SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1); | |
2277 | if (err < 0) | |
2278 | return err; | |
2279 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, | |
2280 | snd_pcm_hw_rule_muldivk, (void*) 8, | |
2281 | SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1); | |
2282 | if (err < 0) | |
2283 | return err; | |
2284 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, | |
2285 | snd_pcm_hw_rule_muldivk, (void*) 8, | |
2286 | SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1); | |
2287 | if (err < 0) | |
2288 | return err; | |
2289 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME, | |
2290 | snd_pcm_hw_rule_mulkdiv, (void*) 1000000, | |
2291 | SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1); | |
2292 | if (err < 0) | |
2293 | return err; | |
2294 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME, | |
2295 | snd_pcm_hw_rule_mulkdiv, (void*) 1000000, | |
2296 | SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1); | |
2297 | if (err < 0) | |
2298 | return err; | |
2299 | return 0; | |
2300 | } | |
2301 | ||
877211f5 | 2302 | int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream) |
1da177e4 | 2303 | { |
877211f5 TI |
2304 | struct snd_pcm_runtime *runtime = substream->runtime; |
2305 | struct snd_pcm_hardware *hw = &runtime->hw; | |
1da177e4 LT |
2306 | int err; |
2307 | unsigned int mask = 0; | |
2308 | ||
2309 | if (hw->info & SNDRV_PCM_INFO_INTERLEAVED) | |
2310 | mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED; | |
2311 | if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED) | |
2312 | mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED; | |
63825f3a | 2313 | if (hw_support_mmap(substream)) { |
1da177e4 LT |
2314 | if (hw->info & SNDRV_PCM_INFO_INTERLEAVED) |
2315 | mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED; | |
2316 | if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED) | |
2317 | mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED; | |
2318 | if (hw->info & SNDRV_PCM_INFO_COMPLEX) | |
2319 | mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX; | |
2320 | } | |
2321 | err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask); | |
7eaa943c TI |
2322 | if (err < 0) |
2323 | return err; | |
1da177e4 LT |
2324 | |
2325 | err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats); | |
7eaa943c TI |
2326 | if (err < 0) |
2327 | return err; | |
1da177e4 LT |
2328 | |
2329 | err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD); | |
7eaa943c TI |
2330 | if (err < 0) |
2331 | return err; | |
1da177e4 LT |
2332 | |
2333 | err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS, | |
2334 | hw->channels_min, hw->channels_max); | |
7eaa943c TI |
2335 | if (err < 0) |
2336 | return err; | |
1da177e4 LT |
2337 | |
2338 | err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE, | |
2339 | hw->rate_min, hw->rate_max); | |
8b90ca08 GL |
2340 | if (err < 0) |
2341 | return err; | |
1da177e4 LT |
2342 | |
2343 | err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, | |
2344 | hw->period_bytes_min, hw->period_bytes_max); | |
8b90ca08 GL |
2345 | if (err < 0) |
2346 | return err; | |
1da177e4 LT |
2347 | |
2348 | err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS, | |
2349 | hw->periods_min, hw->periods_max); | |
7eaa943c TI |
2350 | if (err < 0) |
2351 | return err; | |
1da177e4 LT |
2352 | |
2353 | err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, | |
2354 | hw->period_bytes_min, hw->buffer_bytes_max); | |
7eaa943c TI |
2355 | if (err < 0) |
2356 | return err; | |
1da177e4 LT |
2357 | |
2358 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, | |
2359 | snd_pcm_hw_rule_buffer_bytes_max, substream, | |
2360 | SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1); | |
2361 | if (err < 0) | |
2362 | return err; | |
2363 | ||
2364 | /* FIXME: remove */ | |
2365 | if (runtime->dma_bytes) { | |
2366 | err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes); | |
7eaa943c | 2367 | if (err < 0) |
6cf95152 | 2368 | return err; |
1da177e4 LT |
2369 | } |
2370 | ||
2371 | if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) { | |
2372 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, | |
2373 | snd_pcm_hw_rule_rate, hw, | |
2374 | SNDRV_PCM_HW_PARAM_RATE, -1); | |
2375 | if (err < 0) | |
2376 | return err; | |
2377 | } | |
2378 | ||
2379 | /* FIXME: this belong to lowlevel */ | |
1da177e4 LT |
2380 | snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE); |
2381 | ||
2382 | return 0; | |
2383 | } | |
2384 | ||
3bf75f9b | 2385 | static void pcm_release_private(struct snd_pcm_substream *substream) |
1da177e4 | 2386 | { |
b51abed8 TI |
2387 | if (snd_pcm_stream_linked(substream)) |
2388 | snd_pcm_unlink(substream); | |
3bf75f9b TI |
2389 | } |
2390 | ||
2391 | void snd_pcm_release_substream(struct snd_pcm_substream *substream) | |
2392 | { | |
0df63e44 TI |
2393 | substream->ref_count--; |
2394 | if (substream->ref_count > 0) | |
2395 | return; | |
2396 | ||
3bf75f9b | 2397 | snd_pcm_drop(substream); |
3bf75f9b | 2398 | if (substream->hw_opened) { |
094435d4 TI |
2399 | if (substream->ops->hw_free && |
2400 | substream->runtime->status->state != SNDRV_PCM_STATE_OPEN) | |
1da177e4 LT |
2401 | substream->ops->hw_free(substream); |
2402 | substream->ops->close(substream); | |
3bf75f9b | 2403 | substream->hw_opened = 0; |
1da177e4 | 2404 | } |
8699a0b6 TI |
2405 | if (pm_qos_request_active(&substream->latency_pm_qos_req)) |
2406 | pm_qos_remove_request(&substream->latency_pm_qos_req); | |
1576274d TI |
2407 | if (substream->pcm_release) { |
2408 | substream->pcm_release(substream); | |
2409 | substream->pcm_release = NULL; | |
2410 | } | |
3bf75f9b TI |
2411 | snd_pcm_detach_substream(substream); |
2412 | } | |
e88e8ae6 TI |
2413 | EXPORT_SYMBOL(snd_pcm_release_substream); |
2414 | ||
3bf75f9b TI |
2415 | int snd_pcm_open_substream(struct snd_pcm *pcm, int stream, |
2416 | struct file *file, | |
2417 | struct snd_pcm_substream **rsubstream) | |
2418 | { | |
2419 | struct snd_pcm_substream *substream; | |
2420 | int err; | |
2421 | ||
2422 | err = snd_pcm_attach_substream(pcm, stream, file, &substream); | |
2423 | if (err < 0) | |
2424 | return err; | |
0df63e44 TI |
2425 | if (substream->ref_count > 1) { |
2426 | *rsubstream = substream; | |
2427 | return 0; | |
2428 | } | |
2429 | ||
3bf75f9b TI |
2430 | err = snd_pcm_hw_constraints_init(substream); |
2431 | if (err < 0) { | |
09e56df8 | 2432 | pcm_dbg(pcm, "snd_pcm_hw_constraints_init failed\n"); |
3bf75f9b TI |
2433 | goto error; |
2434 | } | |
2435 | ||
2436 | if ((err = substream->ops->open(substream)) < 0) | |
2437 | goto error; | |
2438 | ||
2439 | substream->hw_opened = 1; | |
2440 | ||
2441 | err = snd_pcm_hw_constraints_complete(substream); | |
2442 | if (err < 0) { | |
09e56df8 | 2443 | pcm_dbg(pcm, "snd_pcm_hw_constraints_complete failed\n"); |
3bf75f9b TI |
2444 | goto error; |
2445 | } | |
2446 | ||
2447 | *rsubstream = substream; | |
1da177e4 | 2448 | return 0; |
3bf75f9b TI |
2449 | |
2450 | error: | |
2451 | snd_pcm_release_substream(substream); | |
2452 | return err; | |
1da177e4 | 2453 | } |
e88e8ae6 TI |
2454 | EXPORT_SYMBOL(snd_pcm_open_substream); |
2455 | ||
1da177e4 | 2456 | static int snd_pcm_open_file(struct file *file, |
877211f5 | 2457 | struct snd_pcm *pcm, |
ffd3d5c6 | 2458 | int stream) |
1da177e4 | 2459 | { |
877211f5 TI |
2460 | struct snd_pcm_file *pcm_file; |
2461 | struct snd_pcm_substream *substream; | |
3bf75f9b | 2462 | int err; |
1da177e4 | 2463 | |
3bf75f9b TI |
2464 | err = snd_pcm_open_substream(pcm, stream, file, &substream); |
2465 | if (err < 0) | |
2466 | return err; | |
2467 | ||
548a648b TI |
2468 | pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL); |
2469 | if (pcm_file == NULL) { | |
2470 | snd_pcm_release_substream(substream); | |
2471 | return -ENOMEM; | |
2472 | } | |
2473 | pcm_file->substream = substream; | |
2474 | if (substream->ref_count == 1) { | |
0df63e44 TI |
2475 | substream->file = pcm_file; |
2476 | substream->pcm_release = pcm_release_private; | |
1da177e4 | 2477 | } |
1da177e4 | 2478 | file->private_data = pcm_file; |
ffd3d5c6 | 2479 | |
1da177e4 LT |
2480 | return 0; |
2481 | } | |
2482 | ||
f87135f5 CL |
2483 | static int snd_pcm_playback_open(struct inode *inode, struct file *file) |
2484 | { | |
2485 | struct snd_pcm *pcm; | |
02f4865f TI |
2486 | int err = nonseekable_open(inode, file); |
2487 | if (err < 0) | |
2488 | return err; | |
f87135f5 CL |
2489 | pcm = snd_lookup_minor_data(iminor(inode), |
2490 | SNDRV_DEVICE_TYPE_PCM_PLAYBACK); | |
a0830dbd | 2491 | err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK); |
8bb4d9ce TI |
2492 | if (pcm) |
2493 | snd_card_unref(pcm->card); | |
a0830dbd | 2494 | return err; |
f87135f5 CL |
2495 | } |
2496 | ||
2497 | static int snd_pcm_capture_open(struct inode *inode, struct file *file) | |
1da177e4 | 2498 | { |
877211f5 | 2499 | struct snd_pcm *pcm; |
02f4865f TI |
2500 | int err = nonseekable_open(inode, file); |
2501 | if (err < 0) | |
2502 | return err; | |
f87135f5 CL |
2503 | pcm = snd_lookup_minor_data(iminor(inode), |
2504 | SNDRV_DEVICE_TYPE_PCM_CAPTURE); | |
a0830dbd | 2505 | err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE); |
8bb4d9ce TI |
2506 | if (pcm) |
2507 | snd_card_unref(pcm->card); | |
a0830dbd | 2508 | return err; |
f87135f5 CL |
2509 | } |
2510 | ||
2511 | static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream) | |
2512 | { | |
2513 | int err; | |
ac6424b9 | 2514 | wait_queue_entry_t wait; |
1da177e4 | 2515 | |
1da177e4 LT |
2516 | if (pcm == NULL) { |
2517 | err = -ENODEV; | |
2518 | goto __error1; | |
2519 | } | |
2520 | err = snd_card_file_add(pcm->card, file); | |
2521 | if (err < 0) | |
2522 | goto __error1; | |
2523 | if (!try_module_get(pcm->card->module)) { | |
2524 | err = -EFAULT; | |
2525 | goto __error2; | |
2526 | } | |
2527 | init_waitqueue_entry(&wait, current); | |
2528 | add_wait_queue(&pcm->open_wait, &wait); | |
1a60d4c5 | 2529 | mutex_lock(&pcm->open_mutex); |
1da177e4 | 2530 | while (1) { |
ffd3d5c6 | 2531 | err = snd_pcm_open_file(file, pcm, stream); |
1da177e4 LT |
2532 | if (err >= 0) |
2533 | break; | |
2534 | if (err == -EAGAIN) { | |
2535 | if (file->f_flags & O_NONBLOCK) { | |
2536 | err = -EBUSY; | |
2537 | break; | |
2538 | } | |
2539 | } else | |
2540 | break; | |
2541 | set_current_state(TASK_INTERRUPTIBLE); | |
1a60d4c5 | 2542 | mutex_unlock(&pcm->open_mutex); |
1da177e4 | 2543 | schedule(); |
1a60d4c5 | 2544 | mutex_lock(&pcm->open_mutex); |
0914f796 TI |
2545 | if (pcm->card->shutdown) { |
2546 | err = -ENODEV; | |
2547 | break; | |
2548 | } | |
1da177e4 LT |
2549 | if (signal_pending(current)) { |
2550 | err = -ERESTARTSYS; | |
2551 | break; | |
2552 | } | |
2553 | } | |
2554 | remove_wait_queue(&pcm->open_wait, &wait); | |
1a60d4c5 | 2555 | mutex_unlock(&pcm->open_mutex); |
1da177e4 LT |
2556 | if (err < 0) |
2557 | goto __error; | |
2558 | return err; | |
2559 | ||
2560 | __error: | |
2561 | module_put(pcm->card->module); | |
2562 | __error2: | |
2563 | snd_card_file_remove(pcm->card, file); | |
2564 | __error1: | |
2565 | return err; | |
2566 | } | |
2567 | ||
2568 | static int snd_pcm_release(struct inode *inode, struct file *file) | |
2569 | { | |
877211f5 TI |
2570 | struct snd_pcm *pcm; |
2571 | struct snd_pcm_substream *substream; | |
2572 | struct snd_pcm_file *pcm_file; | |
1da177e4 LT |
2573 | |
2574 | pcm_file = file->private_data; | |
2575 | substream = pcm_file->substream; | |
7eaa943c TI |
2576 | if (snd_BUG_ON(!substream)) |
2577 | return -ENXIO; | |
1da177e4 | 2578 | pcm = substream->pcm; |
1a60d4c5 | 2579 | mutex_lock(&pcm->open_mutex); |
3bf75f9b | 2580 | snd_pcm_release_substream(substream); |
548a648b | 2581 | kfree(pcm_file); |
1a60d4c5 | 2582 | mutex_unlock(&pcm->open_mutex); |
1da177e4 LT |
2583 | wake_up(&pcm->open_wait); |
2584 | module_put(pcm->card->module); | |
2585 | snd_card_file_remove(pcm->card, file); | |
2586 | return 0; | |
2587 | } | |
2588 | ||
f839cc1c TI |
2589 | /* check and update PCM state; return 0 or a negative error |
2590 | * call this inside PCM lock | |
2591 | */ | |
2592 | static int do_pcm_hwsync(struct snd_pcm_substream *substream) | |
1da177e4 | 2593 | { |
f839cc1c | 2594 | switch (substream->runtime->status->state) { |
1da177e4 | 2595 | case SNDRV_PCM_STATE_DRAINING: |
f839cc1c TI |
2596 | if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) |
2597 | return -EBADFD; | |
1da177e4 | 2598 | /* Fall through */ |
f839cc1c TI |
2599 | case SNDRV_PCM_STATE_RUNNING: |
2600 | return snd_pcm_update_hw_ptr(substream); | |
2601 | case SNDRV_PCM_STATE_PREPARED: | |
2602 | case SNDRV_PCM_STATE_PAUSED: | |
2603 | return 0; | |
51840409 | 2604 | case SNDRV_PCM_STATE_SUSPENDED: |
f839cc1c TI |
2605 | return -ESTRPIPE; |
2606 | case SNDRV_PCM_STATE_XRUN: | |
2607 | return -EPIPE; | |
1da177e4 | 2608 | default: |
f839cc1c | 2609 | return -EBADFD; |
1da177e4 | 2610 | } |
f839cc1c | 2611 | } |
1da177e4 | 2612 | |
9027c463 | 2613 | /* increase the appl_ptr; returns the processed frames or a negative error */ |
e0327a0f TI |
2614 | static snd_pcm_sframes_t forward_appl_ptr(struct snd_pcm_substream *substream, |
2615 | snd_pcm_uframes_t frames, | |
2616 | snd_pcm_sframes_t avail) | |
2617 | { | |
2618 | struct snd_pcm_runtime *runtime = substream->runtime; | |
2619 | snd_pcm_sframes_t appl_ptr; | |
9027c463 | 2620 | int ret; |
e0327a0f TI |
2621 | |
2622 | if (avail <= 0) | |
2623 | return 0; | |
2624 | if (frames > (snd_pcm_uframes_t)avail) | |
2625 | frames = avail; | |
2626 | appl_ptr = runtime->control->appl_ptr + frames; | |
2627 | if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary) | |
2628 | appl_ptr -= runtime->boundary; | |
66e01a5c | 2629 | ret = pcm_lib_apply_appl_ptr(substream, appl_ptr); |
9027c463 | 2630 | return ret < 0 ? ret : frames; |
e0327a0f TI |
2631 | } |
2632 | ||
fb51f1cd | 2633 | /* decrease the appl_ptr; returns the processed frames or zero for error */ |
e0327a0f TI |
2634 | static snd_pcm_sframes_t rewind_appl_ptr(struct snd_pcm_substream *substream, |
2635 | snd_pcm_uframes_t frames, | |
2636 | snd_pcm_sframes_t avail) | |
2637 | { | |
2638 | struct snd_pcm_runtime *runtime = substream->runtime; | |
2639 | snd_pcm_sframes_t appl_ptr; | |
9027c463 | 2640 | int ret; |
e0327a0f TI |
2641 | |
2642 | if (avail <= 0) | |
2643 | return 0; | |
2644 | if (frames > (snd_pcm_uframes_t)avail) | |
2645 | frames = avail; | |
1da177e4 LT |
2646 | appl_ptr = runtime->control->appl_ptr - frames; |
2647 | if (appl_ptr < 0) | |
2648 | appl_ptr += runtime->boundary; | |
66e01a5c | 2649 | ret = pcm_lib_apply_appl_ptr(substream, appl_ptr); |
fb51f1cd TI |
2650 | /* NOTE: we return zero for errors because PulseAudio gets depressed |
2651 | * upon receiving an error from rewind ioctl and stops processing | |
2652 | * any longer. Returning zero means that no rewind is done, so | |
2653 | * it's not absolutely wrong to answer like that. | |
2654 | */ | |
2655 | return ret < 0 ? 0 : frames; | |
e0327a0f TI |
2656 | } |
2657 | ||
763e5067 TI |
2658 | static snd_pcm_sframes_t snd_pcm_rewind(struct snd_pcm_substream *substream, |
2659 | snd_pcm_uframes_t frames) | |
1da177e4 | 2660 | { |
1da177e4 | 2661 | snd_pcm_sframes_t ret; |
1da177e4 LT |
2662 | |
2663 | if (frames == 0) | |
2664 | return 0; | |
2665 | ||
2666 | snd_pcm_stream_lock_irq(substream); | |
f839cc1c | 2667 | ret = do_pcm_hwsync(substream); |
e0327a0f TI |
2668 | if (!ret) |
2669 | ret = rewind_appl_ptr(substream, frames, | |
763e5067 | 2670 | snd_pcm_hw_avail(substream)); |
1da177e4 LT |
2671 | snd_pcm_stream_unlock_irq(substream); |
2672 | return ret; | |
2673 | } | |
2674 | ||
763e5067 TI |
2675 | static snd_pcm_sframes_t snd_pcm_forward(struct snd_pcm_substream *substream, |
2676 | snd_pcm_uframes_t frames) | |
1da177e4 | 2677 | { |
1da177e4 | 2678 | snd_pcm_sframes_t ret; |
1da177e4 LT |
2679 | |
2680 | if (frames == 0) | |
2681 | return 0; | |
2682 | ||
2683 | snd_pcm_stream_lock_irq(substream); | |
f839cc1c | 2684 | ret = do_pcm_hwsync(substream); |
e0327a0f TI |
2685 | if (!ret) |
2686 | ret = forward_appl_ptr(substream, frames, | |
763e5067 | 2687 | snd_pcm_avail(substream)); |
1da177e4 LT |
2688 | snd_pcm_stream_unlock_irq(substream); |
2689 | return ret; | |
2690 | } | |
2691 | ||
877211f5 | 2692 | static int snd_pcm_hwsync(struct snd_pcm_substream *substream) |
1da177e4 | 2693 | { |
1da177e4 LT |
2694 | int err; |
2695 | ||
2696 | snd_pcm_stream_lock_irq(substream); | |
f839cc1c | 2697 | err = do_pcm_hwsync(substream); |
1da177e4 LT |
2698 | snd_pcm_stream_unlock_irq(substream); |
2699 | return err; | |
2700 | } | |
2701 | ||
912e4c33 JM |
2702 | static int snd_pcm_delay(struct snd_pcm_substream *substream, |
2703 | snd_pcm_sframes_t *delay) | |
1da177e4 | 2704 | { |
1da177e4 LT |
2705 | int err; |
2706 | snd_pcm_sframes_t n = 0; | |
2707 | ||
2708 | snd_pcm_stream_lock_irq(substream); | |
f839cc1c | 2709 | err = do_pcm_hwsync(substream); |
c99c5a3b TI |
2710 | if (!err) |
2711 | n = snd_pcm_calc_delay(substream); | |
1da177e4 | 2712 | snd_pcm_stream_unlock_irq(substream); |
912e4c33 JM |
2713 | if (!err) |
2714 | *delay = n; | |
2715 | return err; | |
1da177e4 LT |
2716 | } |
2717 | ||
877211f5 TI |
2718 | static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream, |
2719 | struct snd_pcm_sync_ptr __user *_sync_ptr) | |
1da177e4 | 2720 | { |
877211f5 TI |
2721 | struct snd_pcm_runtime *runtime = substream->runtime; |
2722 | struct snd_pcm_sync_ptr sync_ptr; | |
2723 | volatile struct snd_pcm_mmap_status *status; | |
2724 | volatile struct snd_pcm_mmap_control *control; | |
1da177e4 LT |
2725 | int err; |
2726 | ||
2727 | memset(&sync_ptr, 0, sizeof(sync_ptr)); | |
2728 | if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags))) | |
2729 | return -EFAULT; | |
877211f5 | 2730 | if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control))) |
1da177e4 LT |
2731 | return -EFAULT; |
2732 | status = runtime->status; | |
2733 | control = runtime->control; | |
2734 | if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) { | |
2735 | err = snd_pcm_hwsync(substream); | |
2736 | if (err < 0) | |
2737 | return err; | |
2738 | } | |
2739 | snd_pcm_stream_lock_irq(substream); | |
9027c463 | 2740 | if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL)) { |
66e01a5c TS |
2741 | err = pcm_lib_apply_appl_ptr(substream, |
2742 | sync_ptr.c.control.appl_ptr); | |
9027c463 TI |
2743 | if (err < 0) { |
2744 | snd_pcm_stream_unlock_irq(substream); | |
2745 | return err; | |
2746 | } | |
2747 | } else { | |
1da177e4 | 2748 | sync_ptr.c.control.appl_ptr = control->appl_ptr; |
9027c463 | 2749 | } |
1da177e4 LT |
2750 | if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN)) |
2751 | control->avail_min = sync_ptr.c.control.avail_min; | |
2752 | else | |
2753 | sync_ptr.c.control.avail_min = control->avail_min; | |
2754 | sync_ptr.s.status.state = status->state; | |
2755 | sync_ptr.s.status.hw_ptr = status->hw_ptr; | |
2756 | sync_ptr.s.status.tstamp = status->tstamp; | |
2757 | sync_ptr.s.status.suspended_state = status->suspended_state; | |
f853dcaa | 2758 | sync_ptr.s.status.audio_tstamp = status->audio_tstamp; |
1da177e4 LT |
2759 | snd_pcm_stream_unlock_irq(substream); |
2760 | if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr))) | |
2761 | return -EFAULT; | |
2762 | return 0; | |
2763 | } | |
b751eef1 JK |
2764 | |
2765 | static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg) | |
2766 | { | |
2767 | struct snd_pcm_runtime *runtime = substream->runtime; | |
2768 | int arg; | |
2769 | ||
2770 | if (get_user(arg, _arg)) | |
2771 | return -EFAULT; | |
2772 | if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST) | |
2773 | return -EINVAL; | |
2408c219 | 2774 | runtime->tstamp_type = arg; |
b751eef1 JK |
2775 | return 0; |
2776 | } | |
67616fed TI |
2777 | |
2778 | static int snd_pcm_xferi_frames_ioctl(struct snd_pcm_substream *substream, | |
2779 | struct snd_xferi __user *_xferi) | |
2780 | { | |
2781 | struct snd_xferi xferi; | |
2782 | struct snd_pcm_runtime *runtime = substream->runtime; | |
2783 | snd_pcm_sframes_t result; | |
2784 | ||
2785 | if (runtime->status->state == SNDRV_PCM_STATE_OPEN) | |
2786 | return -EBADFD; | |
2787 | if (put_user(0, &_xferi->result)) | |
2788 | return -EFAULT; | |
2789 | if (copy_from_user(&xferi, _xferi, sizeof(xferi))) | |
2790 | return -EFAULT; | |
2791 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) | |
2792 | result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames); | |
2793 | else | |
2794 | result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames); | |
2795 | __put_user(result, &_xferi->result); | |
2796 | return result < 0 ? result : 0; | |
2797 | } | |
2798 | ||
2799 | static int snd_pcm_xfern_frames_ioctl(struct snd_pcm_substream *substream, | |
2800 | struct snd_xfern __user *_xfern) | |
2801 | { | |
2802 | struct snd_xfern xfern; | |
2803 | struct snd_pcm_runtime *runtime = substream->runtime; | |
2804 | void *bufs; | |
2805 | snd_pcm_sframes_t result; | |
2806 | ||
2807 | if (runtime->status->state == SNDRV_PCM_STATE_OPEN) | |
2808 | return -EBADFD; | |
2809 | if (runtime->channels > 128) | |
2810 | return -EINVAL; | |
2811 | if (put_user(0, &_xfern->result)) | |
2812 | return -EFAULT; | |
2813 | if (copy_from_user(&xfern, _xfern, sizeof(xfern))) | |
2814 | return -EFAULT; | |
2815 | ||
2816 | bufs = memdup_user(xfern.bufs, sizeof(void *) * runtime->channels); | |
2817 | if (IS_ERR(bufs)) | |
2818 | return PTR_ERR(bufs); | |
2819 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) | |
2820 | result = snd_pcm_lib_writev(substream, bufs, xfern.frames); | |
2821 | else | |
2822 | result = snd_pcm_lib_readv(substream, bufs, xfern.frames); | |
2823 | kfree(bufs); | |
2824 | __put_user(result, &_xfern->result); | |
2825 | return result < 0 ? result : 0; | |
2826 | } | |
2827 | ||
2828 | static int snd_pcm_rewind_ioctl(struct snd_pcm_substream *substream, | |
2829 | snd_pcm_uframes_t __user *_frames) | |
2830 | { | |
2831 | snd_pcm_uframes_t frames; | |
2832 | snd_pcm_sframes_t result; | |
2833 | ||
2834 | if (get_user(frames, _frames)) | |
2835 | return -EFAULT; | |
2836 | if (put_user(0, _frames)) | |
2837 | return -EFAULT; | |
763e5067 | 2838 | result = snd_pcm_rewind(substream, frames); |
67616fed TI |
2839 | __put_user(result, _frames); |
2840 | return result < 0 ? result : 0; | |
2841 | } | |
2842 | ||
2843 | static int snd_pcm_forward_ioctl(struct snd_pcm_substream *substream, | |
2844 | snd_pcm_uframes_t __user *_frames) | |
2845 | { | |
2846 | snd_pcm_uframes_t frames; | |
2847 | snd_pcm_sframes_t result; | |
2848 | ||
2849 | if (get_user(frames, _frames)) | |
2850 | return -EFAULT; | |
2851 | if (put_user(0, _frames)) | |
2852 | return -EFAULT; | |
763e5067 | 2853 | result = snd_pcm_forward(substream, frames); |
67616fed TI |
2854 | __put_user(result, _frames); |
2855 | return result < 0 ? result : 0; | |
2856 | } | |
2857 | ||
2858 | static int snd_pcm_common_ioctl(struct file *file, | |
0df63e44 | 2859 | struct snd_pcm_substream *substream, |
1da177e4 LT |
2860 | unsigned int cmd, void __user *arg) |
2861 | { | |
4b671f57 | 2862 | struct snd_pcm_file *pcm_file = file->private_data; |
7d8e8292 TI |
2863 | int res; |
2864 | ||
67616fed TI |
2865 | if (PCM_RUNTIME_CHECK(substream)) |
2866 | return -ENXIO; | |
2867 | ||
7d8e8292 TI |
2868 | res = snd_power_wait(substream->pcm->card, SNDRV_CTL_POWER_D0); |
2869 | if (res < 0) | |
2870 | return res; | |
4b671f57 | 2871 | |
1da177e4 LT |
2872 | switch (cmd) { |
2873 | case SNDRV_PCM_IOCTL_PVERSION: | |
2874 | return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0; | |
2875 | case SNDRV_PCM_IOCTL_INFO: | |
2876 | return snd_pcm_info_user(substream, arg); | |
28e9e473 JK |
2877 | case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */ |
2878 | return 0; | |
b751eef1 JK |
2879 | case SNDRV_PCM_IOCTL_TTSTAMP: |
2880 | return snd_pcm_tstamp(substream, arg); | |
4b671f57 TI |
2881 | case SNDRV_PCM_IOCTL_USER_PVERSION: |
2882 | if (get_user(pcm_file->user_pversion, | |
2883 | (unsigned int __user *)arg)) | |
2884 | return -EFAULT; | |
2885 | return 0; | |
1da177e4 LT |
2886 | case SNDRV_PCM_IOCTL_HW_REFINE: |
2887 | return snd_pcm_hw_refine_user(substream, arg); | |
2888 | case SNDRV_PCM_IOCTL_HW_PARAMS: | |
2889 | return snd_pcm_hw_params_user(substream, arg); | |
2890 | case SNDRV_PCM_IOCTL_HW_FREE: | |
2891 | return snd_pcm_hw_free(substream); | |
2892 | case SNDRV_PCM_IOCTL_SW_PARAMS: | |
2893 | return snd_pcm_sw_params_user(substream, arg); | |
2894 | case SNDRV_PCM_IOCTL_STATUS: | |
38ca2a4d PLB |
2895 | return snd_pcm_status_user(substream, arg, false); |
2896 | case SNDRV_PCM_IOCTL_STATUS_EXT: | |
2897 | return snd_pcm_status_user(substream, arg, true); | |
1da177e4 LT |
2898 | case SNDRV_PCM_IOCTL_CHANNEL_INFO: |
2899 | return snd_pcm_channel_info_user(substream, arg); | |
2900 | case SNDRV_PCM_IOCTL_PREPARE: | |
0df63e44 | 2901 | return snd_pcm_prepare(substream, file); |
1da177e4 LT |
2902 | case SNDRV_PCM_IOCTL_RESET: |
2903 | return snd_pcm_reset(substream); | |
2904 | case SNDRV_PCM_IOCTL_START: | |
c2c86a97 | 2905 | return snd_pcm_start_lock_irq(substream); |
1da177e4 LT |
2906 | case SNDRV_PCM_IOCTL_LINK: |
2907 | return snd_pcm_link(substream, (int)(unsigned long) arg); | |
2908 | case SNDRV_PCM_IOCTL_UNLINK: | |
2909 | return snd_pcm_unlink(substream); | |
2910 | case SNDRV_PCM_IOCTL_RESUME: | |
2911 | return snd_pcm_resume(substream); | |
2912 | case SNDRV_PCM_IOCTL_XRUN: | |
2913 | return snd_pcm_xrun(substream); | |
2914 | case SNDRV_PCM_IOCTL_HWSYNC: | |
2915 | return snd_pcm_hwsync(substream); | |
2916 | case SNDRV_PCM_IOCTL_DELAY: | |
c2c86a97 | 2917 | { |
912e4c33 | 2918 | snd_pcm_sframes_t delay; |
c2c86a97 | 2919 | snd_pcm_sframes_t __user *res = arg; |
912e4c33 | 2920 | int err; |
c2c86a97 | 2921 | |
912e4c33 JM |
2922 | err = snd_pcm_delay(substream, &delay); |
2923 | if (err) | |
2924 | return err; | |
c2c86a97 TI |
2925 | if (put_user(delay, res)) |
2926 | return -EFAULT; | |
2927 | return 0; | |
2928 | } | |
1da177e4 LT |
2929 | case SNDRV_PCM_IOCTL_SYNC_PTR: |
2930 | return snd_pcm_sync_ptr(substream, arg); | |
59d48582 | 2931 | #ifdef CONFIG_SND_SUPPORT_OLD_API |
1da177e4 LT |
2932 | case SNDRV_PCM_IOCTL_HW_REFINE_OLD: |
2933 | return snd_pcm_hw_refine_old_user(substream, arg); | |
2934 | case SNDRV_PCM_IOCTL_HW_PARAMS_OLD: | |
2935 | return snd_pcm_hw_params_old_user(substream, arg); | |
59d48582 | 2936 | #endif |
1da177e4 | 2937 | case SNDRV_PCM_IOCTL_DRAIN: |
4cdc115f | 2938 | return snd_pcm_drain(substream, file); |
1da177e4 LT |
2939 | case SNDRV_PCM_IOCTL_DROP: |
2940 | return snd_pcm_drop(substream); | |
e661d0dd | 2941 | case SNDRV_PCM_IOCTL_PAUSE: |
34bcc44a TI |
2942 | return snd_pcm_action_lock_irq(&snd_pcm_action_pause, |
2943 | substream, | |
2944 | (int)(unsigned long)arg); | |
1da177e4 | 2945 | case SNDRV_PCM_IOCTL_WRITEI_FRAMES: |
1da177e4 | 2946 | case SNDRV_PCM_IOCTL_READI_FRAMES: |
67616fed TI |
2947 | return snd_pcm_xferi_frames_ioctl(substream, arg); |
2948 | case SNDRV_PCM_IOCTL_WRITEN_FRAMES: | |
1da177e4 | 2949 | case SNDRV_PCM_IOCTL_READN_FRAMES: |
67616fed | 2950 | return snd_pcm_xfern_frames_ioctl(substream, arg); |
1da177e4 | 2951 | case SNDRV_PCM_IOCTL_REWIND: |
67616fed | 2952 | return snd_pcm_rewind_ioctl(substream, arg); |
1da177e4 | 2953 | case SNDRV_PCM_IOCTL_FORWARD: |
67616fed | 2954 | return snd_pcm_forward_ioctl(substream, arg); |
1da177e4 | 2955 | } |
67616fed TI |
2956 | pcm_dbg(substream->pcm, "unknown ioctl = 0x%x\n", cmd); |
2957 | return -ENOTTY; | |
1da177e4 LT |
2958 | } |
2959 | ||
67616fed TI |
2960 | static long snd_pcm_ioctl(struct file *file, unsigned int cmd, |
2961 | unsigned long arg) | |
1da177e4 | 2962 | { |
877211f5 | 2963 | struct snd_pcm_file *pcm_file; |
1da177e4 LT |
2964 | |
2965 | pcm_file = file->private_data; | |
2966 | ||
2967 | if (((cmd >> 8) & 0xff) != 'A') | |
2968 | return -ENOTTY; | |
2969 | ||
67616fed TI |
2970 | return snd_pcm_common_ioctl(file, pcm_file->substream, cmd, |
2971 | (void __user *)arg); | |
1da177e4 LT |
2972 | } |
2973 | ||
c2c86a97 TI |
2974 | /** |
2975 | * snd_pcm_kernel_ioctl - Execute PCM ioctl in the kernel-space | |
2976 | * @substream: PCM substream | |
2977 | * @cmd: IOCTL cmd | |
2978 | * @arg: IOCTL argument | |
2979 | * | |
2980 | * The function is provided primarily for OSS layer and USB gadget drivers, | |
2981 | * and it allows only the limited set of ioctls (hw_params, sw_params, | |
2982 | * prepare, start, drain, drop, forward). | |
2983 | */ | |
bf1bbb5a TI |
2984 | int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream, |
2985 | unsigned int cmd, void *arg) | |
1da177e4 | 2986 | { |
c2c86a97 TI |
2987 | snd_pcm_uframes_t *frames = arg; |
2988 | snd_pcm_sframes_t result; | |
1da177e4 | 2989 | |
c2c86a97 TI |
2990 | switch (cmd) { |
2991 | case SNDRV_PCM_IOCTL_FORWARD: | |
2992 | { | |
2993 | /* provided only for OSS; capture-only and no value returned */ | |
2994 | if (substream->stream != SNDRV_PCM_STREAM_CAPTURE) | |
2995 | return -EINVAL; | |
763e5067 | 2996 | result = snd_pcm_forward(substream, *frames); |
c2c86a97 TI |
2997 | return result < 0 ? result : 0; |
2998 | } | |
2999 | case SNDRV_PCM_IOCTL_HW_PARAMS: | |
3000 | return snd_pcm_hw_params(substream, arg); | |
3001 | case SNDRV_PCM_IOCTL_SW_PARAMS: | |
3002 | return snd_pcm_sw_params(substream, arg); | |
3003 | case SNDRV_PCM_IOCTL_PREPARE: | |
3004 | return snd_pcm_prepare(substream, NULL); | |
3005 | case SNDRV_PCM_IOCTL_START: | |
3006 | return snd_pcm_start_lock_irq(substream); | |
3007 | case SNDRV_PCM_IOCTL_DRAIN: | |
7d8e8292 | 3008 | return snd_pcm_drain(substream, NULL); |
c2c86a97 TI |
3009 | case SNDRV_PCM_IOCTL_DROP: |
3010 | return snd_pcm_drop(substream); | |
3011 | case SNDRV_PCM_IOCTL_DELAY: | |
912e4c33 | 3012 | return snd_pcm_delay(substream, frames); |
1da177e4 | 3013 | default: |
c2c86a97 | 3014 | return -EINVAL; |
1da177e4 LT |
3015 | } |
3016 | } | |
e88e8ae6 TI |
3017 | EXPORT_SYMBOL(snd_pcm_kernel_ioctl); |
3018 | ||
877211f5 TI |
3019 | static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count, |
3020 | loff_t * offset) | |
1da177e4 | 3021 | { |
877211f5 TI |
3022 | struct snd_pcm_file *pcm_file; |
3023 | struct snd_pcm_substream *substream; | |
3024 | struct snd_pcm_runtime *runtime; | |
1da177e4 LT |
3025 | snd_pcm_sframes_t result; |
3026 | ||
3027 | pcm_file = file->private_data; | |
3028 | substream = pcm_file->substream; | |
7eaa943c TI |
3029 | if (PCM_RUNTIME_CHECK(substream)) |
3030 | return -ENXIO; | |
1da177e4 LT |
3031 | runtime = substream->runtime; |
3032 | if (runtime->status->state == SNDRV_PCM_STATE_OPEN) | |
3033 | return -EBADFD; | |
3034 | if (!frame_aligned(runtime, count)) | |
3035 | return -EINVAL; | |
3036 | count = bytes_to_frames(runtime, count); | |
3037 | result = snd_pcm_lib_read(substream, buf, count); | |
3038 | if (result > 0) | |
3039 | result = frames_to_bytes(runtime, result); | |
3040 | return result; | |
3041 | } | |
3042 | ||
877211f5 TI |
3043 | static ssize_t snd_pcm_write(struct file *file, const char __user *buf, |
3044 | size_t count, loff_t * offset) | |
1da177e4 | 3045 | { |
877211f5 TI |
3046 | struct snd_pcm_file *pcm_file; |
3047 | struct snd_pcm_substream *substream; | |
3048 | struct snd_pcm_runtime *runtime; | |
1da177e4 LT |
3049 | snd_pcm_sframes_t result; |
3050 | ||
3051 | pcm_file = file->private_data; | |
3052 | substream = pcm_file->substream; | |
7eaa943c TI |
3053 | if (PCM_RUNTIME_CHECK(substream)) |
3054 | return -ENXIO; | |
1da177e4 | 3055 | runtime = substream->runtime; |
7eaa943c TI |
3056 | if (runtime->status->state == SNDRV_PCM_STATE_OPEN) |
3057 | return -EBADFD; | |
3058 | if (!frame_aligned(runtime, count)) | |
3059 | return -EINVAL; | |
1da177e4 LT |
3060 | count = bytes_to_frames(runtime, count); |
3061 | result = snd_pcm_lib_write(substream, buf, count); | |
3062 | if (result > 0) | |
3063 | result = frames_to_bytes(runtime, result); | |
1da177e4 LT |
3064 | return result; |
3065 | } | |
3066 | ||
1c65d986 | 3067 | static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to) |
1da177e4 | 3068 | { |
877211f5 TI |
3069 | struct snd_pcm_file *pcm_file; |
3070 | struct snd_pcm_substream *substream; | |
3071 | struct snd_pcm_runtime *runtime; | |
1da177e4 LT |
3072 | snd_pcm_sframes_t result; |
3073 | unsigned long i; | |
3074 | void __user **bufs; | |
3075 | snd_pcm_uframes_t frames; | |
3076 | ||
ee0b3e67 | 3077 | pcm_file = iocb->ki_filp->private_data; |
1da177e4 | 3078 | substream = pcm_file->substream; |
7eaa943c TI |
3079 | if (PCM_RUNTIME_CHECK(substream)) |
3080 | return -ENXIO; | |
1da177e4 LT |
3081 | runtime = substream->runtime; |
3082 | if (runtime->status->state == SNDRV_PCM_STATE_OPEN) | |
3083 | return -EBADFD; | |
1c65d986 AV |
3084 | if (!iter_is_iovec(to)) |
3085 | return -EINVAL; | |
3086 | if (to->nr_segs > 1024 || to->nr_segs != runtime->channels) | |
1da177e4 | 3087 | return -EINVAL; |
1c65d986 | 3088 | if (!frame_aligned(runtime, to->iov->iov_len)) |
1da177e4 | 3089 | return -EINVAL; |
1c65d986 | 3090 | frames = bytes_to_samples(runtime, to->iov->iov_len); |
6da2ec56 | 3091 | bufs = kmalloc_array(to->nr_segs, sizeof(void *), GFP_KERNEL); |
1da177e4 LT |
3092 | if (bufs == NULL) |
3093 | return -ENOMEM; | |
1c65d986 AV |
3094 | for (i = 0; i < to->nr_segs; ++i) |
3095 | bufs[i] = to->iov[i].iov_base; | |
1da177e4 LT |
3096 | result = snd_pcm_lib_readv(substream, bufs, frames); |
3097 | if (result > 0) | |
3098 | result = frames_to_bytes(runtime, result); | |
3099 | kfree(bufs); | |
3100 | return result; | |
3101 | } | |
3102 | ||
1c65d986 | 3103 | static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from) |
1da177e4 | 3104 | { |
877211f5 TI |
3105 | struct snd_pcm_file *pcm_file; |
3106 | struct snd_pcm_substream *substream; | |
3107 | struct snd_pcm_runtime *runtime; | |
1da177e4 LT |
3108 | snd_pcm_sframes_t result; |
3109 | unsigned long i; | |
3110 | void __user **bufs; | |
3111 | snd_pcm_uframes_t frames; | |
3112 | ||
ee0b3e67 | 3113 | pcm_file = iocb->ki_filp->private_data; |
1da177e4 | 3114 | substream = pcm_file->substream; |
7eaa943c TI |
3115 | if (PCM_RUNTIME_CHECK(substream)) |
3116 | return -ENXIO; | |
1da177e4 | 3117 | runtime = substream->runtime; |
7eaa943c TI |
3118 | if (runtime->status->state == SNDRV_PCM_STATE_OPEN) |
3119 | return -EBADFD; | |
1c65d986 AV |
3120 | if (!iter_is_iovec(from)) |
3121 | return -EINVAL; | |
3122 | if (from->nr_segs > 128 || from->nr_segs != runtime->channels || | |
3123 | !frame_aligned(runtime, from->iov->iov_len)) | |
7eaa943c | 3124 | return -EINVAL; |
1c65d986 | 3125 | frames = bytes_to_samples(runtime, from->iov->iov_len); |
6da2ec56 | 3126 | bufs = kmalloc_array(from->nr_segs, sizeof(void *), GFP_KERNEL); |
1da177e4 LT |
3127 | if (bufs == NULL) |
3128 | return -ENOMEM; | |
1c65d986 AV |
3129 | for (i = 0; i < from->nr_segs; ++i) |
3130 | bufs[i] = from->iov[i].iov_base; | |
1da177e4 LT |
3131 | result = snd_pcm_lib_writev(substream, bufs, frames); |
3132 | if (result > 0) | |
3133 | result = frames_to_bytes(runtime, result); | |
3134 | kfree(bufs); | |
1da177e4 LT |
3135 | return result; |
3136 | } | |
3137 | ||
6448fcba | 3138 | static __poll_t snd_pcm_poll(struct file *file, poll_table *wait) |
1da177e4 | 3139 | { |
877211f5 TI |
3140 | struct snd_pcm_file *pcm_file; |
3141 | struct snd_pcm_substream *substream; | |
3142 | struct snd_pcm_runtime *runtime; | |
6448fcba | 3143 | __poll_t mask, ok; |
1da177e4 LT |
3144 | snd_pcm_uframes_t avail; |
3145 | ||
3146 | pcm_file = file->private_data; | |
3147 | ||
3148 | substream = pcm_file->substream; | |
6448fcba TI |
3149 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
3150 | ok = EPOLLOUT | EPOLLWRNORM; | |
3151 | else | |
3152 | ok = EPOLLIN | EPOLLRDNORM; | |
7eaa943c | 3153 | if (PCM_RUNTIME_CHECK(substream)) |
6448fcba | 3154 | return ok | EPOLLERR; |
1da177e4 | 3155 | |
1da177e4 | 3156 | runtime = substream->runtime; |
1da177e4 LT |
3157 | poll_wait(file, &runtime->sleep, wait); |
3158 | ||
6448fcba | 3159 | mask = 0; |
1da177e4 | 3160 | snd_pcm_stream_lock_irq(substream); |
6448fcba | 3161 | avail = snd_pcm_avail(substream); |
1da177e4 LT |
3162 | switch (runtime->status->state) { |
3163 | case SNDRV_PCM_STATE_RUNNING: | |
3164 | case SNDRV_PCM_STATE_PREPARED: | |
3165 | case SNDRV_PCM_STATE_PAUSED: | |
6448fcba TI |
3166 | if (avail >= runtime->control->avail_min) |
3167 | mask = ok; | |
1da177e4 LT |
3168 | break; |
3169 | case SNDRV_PCM_STATE_DRAINING: | |
6448fcba TI |
3170 | if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) { |
3171 | mask = ok; | |
3172 | if (!avail) | |
3173 | mask |= EPOLLERR; | |
1da177e4 | 3174 | } |
6448fcba | 3175 | break; |
1da177e4 | 3176 | default: |
6448fcba | 3177 | mask = ok | EPOLLERR; |
1da177e4 LT |
3178 | break; |
3179 | } | |
3180 | snd_pcm_stream_unlock_irq(substream); | |
3181 | return mask; | |
3182 | } | |
3183 | ||
3184 | /* | |
3185 | * mmap support | |
3186 | */ | |
3187 | ||
3188 | /* | |
3189 | * Only on coherent architectures, we can mmap the status and the control records | |
3190 | * for effcient data transfer. On others, we have to use HWSYNC ioctl... | |
3191 | */ | |
3192 | #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA) | |
3193 | /* | |
3194 | * mmap status record | |
3195 | */ | |
41412fe9 | 3196 | static vm_fault_t snd_pcm_mmap_status_fault(struct vm_fault *vmf) |
1da177e4 | 3197 | { |
11bac800 | 3198 | struct snd_pcm_substream *substream = vmf->vma->vm_private_data; |
877211f5 | 3199 | struct snd_pcm_runtime *runtime; |
1da177e4 LT |
3200 | |
3201 | if (substream == NULL) | |
3ad5afcd | 3202 | return VM_FAULT_SIGBUS; |
1da177e4 | 3203 | runtime = substream->runtime; |
3ad5afcd NP |
3204 | vmf->page = virt_to_page(runtime->status); |
3205 | get_page(vmf->page); | |
3206 | return 0; | |
1da177e4 LT |
3207 | } |
3208 | ||
f0f37e2f | 3209 | static const struct vm_operations_struct snd_pcm_vm_ops_status = |
1da177e4 | 3210 | { |
3ad5afcd | 3211 | .fault = snd_pcm_mmap_status_fault, |
1da177e4 LT |
3212 | }; |
3213 | ||
877211f5 | 3214 | static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file, |
1da177e4 LT |
3215 | struct vm_area_struct *area) |
3216 | { | |
1da177e4 LT |
3217 | long size; |
3218 | if (!(area->vm_flags & VM_READ)) | |
3219 | return -EINVAL; | |
1da177e4 | 3220 | size = area->vm_end - area->vm_start; |
877211f5 | 3221 | if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status))) |
1da177e4 LT |
3222 | return -EINVAL; |
3223 | area->vm_ops = &snd_pcm_vm_ops_status; | |
3224 | area->vm_private_data = substream; | |
314e51b9 | 3225 | area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP; |
1da177e4 LT |
3226 | return 0; |
3227 | } | |
3228 | ||
3229 | /* | |
3230 | * mmap control record | |
3231 | */ | |
41412fe9 | 3232 | static vm_fault_t snd_pcm_mmap_control_fault(struct vm_fault *vmf) |
1da177e4 | 3233 | { |
11bac800 | 3234 | struct snd_pcm_substream *substream = vmf->vma->vm_private_data; |
877211f5 | 3235 | struct snd_pcm_runtime *runtime; |
1da177e4 LT |
3236 | |
3237 | if (substream == NULL) | |
3ad5afcd | 3238 | return VM_FAULT_SIGBUS; |
1da177e4 | 3239 | runtime = substream->runtime; |
3ad5afcd NP |
3240 | vmf->page = virt_to_page(runtime->control); |
3241 | get_page(vmf->page); | |
3242 | return 0; | |
1da177e4 LT |
3243 | } |
3244 | ||
f0f37e2f | 3245 | static const struct vm_operations_struct snd_pcm_vm_ops_control = |
1da177e4 | 3246 | { |
3ad5afcd | 3247 | .fault = snd_pcm_mmap_control_fault, |
1da177e4 LT |
3248 | }; |
3249 | ||
877211f5 | 3250 | static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file, |
1da177e4 LT |
3251 | struct vm_area_struct *area) |
3252 | { | |
1da177e4 LT |
3253 | long size; |
3254 | if (!(area->vm_flags & VM_READ)) | |
3255 | return -EINVAL; | |
1da177e4 | 3256 | size = area->vm_end - area->vm_start; |
877211f5 | 3257 | if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control))) |
1da177e4 LT |
3258 | return -EINVAL; |
3259 | area->vm_ops = &snd_pcm_vm_ops_control; | |
3260 | area->vm_private_data = substream; | |
314e51b9 | 3261 | area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP; |
1da177e4 LT |
3262 | return 0; |
3263 | } | |
42f94597 TI |
3264 | |
3265 | static bool pcm_status_mmap_allowed(struct snd_pcm_file *pcm_file) | |
3266 | { | |
3267 | if (pcm_file->no_compat_mmap) | |
3268 | return false; | |
b602aa8e TI |
3269 | /* See pcm_control_mmap_allowed() below. |
3270 | * Since older alsa-lib requires both status and control mmaps to be | |
3271 | * coupled, we have to disable the status mmap for old alsa-lib, too. | |
3272 | */ | |
3273 | if (pcm_file->user_pversion < SNDRV_PROTOCOL_VERSION(2, 0, 14) && | |
3274 | (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_SYNC_APPLPTR)) | |
3275 | return false; | |
3276 | return true; | |
3277 | } | |
3278 | ||
3279 | static bool pcm_control_mmap_allowed(struct snd_pcm_file *pcm_file) | |
3280 | { | |
3281 | if (pcm_file->no_compat_mmap) | |
3282 | return false; | |
3283 | /* Disallow the control mmap when SYNC_APPLPTR flag is set; | |
42f94597 TI |
3284 | * it enforces the user-space to fall back to snd_pcm_sync_ptr(), |
3285 | * thus it effectively assures the manual update of appl_ptr. | |
42f94597 TI |
3286 | */ |
3287 | if (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_SYNC_APPLPTR) | |
3288 | return false; | |
3289 | return true; | |
3290 | } | |
3291 | ||
1da177e4 LT |
3292 | #else /* ! coherent mmap */ |
3293 | /* | |
3294 | * don't support mmap for status and control records. | |
3295 | */ | |
42f94597 | 3296 | #define pcm_status_mmap_allowed(pcm_file) false |
b602aa8e | 3297 | #define pcm_control_mmap_allowed(pcm_file) false |
42f94597 | 3298 | |
877211f5 | 3299 | static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file, |
1da177e4 LT |
3300 | struct vm_area_struct *area) |
3301 | { | |
3302 | return -ENXIO; | |
3303 | } | |
877211f5 | 3304 | static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file, |
1da177e4 LT |
3305 | struct vm_area_struct *area) |
3306 | { | |
3307 | return -ENXIO; | |
3308 | } | |
3309 | #endif /* coherent mmap */ | |
3310 | ||
9eb4a067 TI |
3311 | static inline struct page * |
3312 | snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs) | |
3313 | { | |
3314 | void *vaddr = substream->runtime->dma_area + ofs; | |
3315 | return virt_to_page(vaddr); | |
3316 | } | |
3317 | ||
1da177e4 | 3318 | /* |
3ad5afcd | 3319 | * fault callback for mmapping a RAM page |
1da177e4 | 3320 | */ |
41412fe9 | 3321 | static vm_fault_t snd_pcm_mmap_data_fault(struct vm_fault *vmf) |
1da177e4 | 3322 | { |
11bac800 | 3323 | struct snd_pcm_substream *substream = vmf->vma->vm_private_data; |
877211f5 | 3324 | struct snd_pcm_runtime *runtime; |
1da177e4 LT |
3325 | unsigned long offset; |
3326 | struct page * page; | |
1da177e4 LT |
3327 | size_t dma_bytes; |
3328 | ||
3329 | if (substream == NULL) | |
3ad5afcd | 3330 | return VM_FAULT_SIGBUS; |
1da177e4 | 3331 | runtime = substream->runtime; |
3ad5afcd | 3332 | offset = vmf->pgoff << PAGE_SHIFT; |
1da177e4 LT |
3333 | dma_bytes = PAGE_ALIGN(runtime->dma_bytes); |
3334 | if (offset > dma_bytes - PAGE_SIZE) | |
3ad5afcd | 3335 | return VM_FAULT_SIGBUS; |
9eb4a067 | 3336 | if (substream->ops->page) |
1da177e4 | 3337 | page = substream->ops->page(substream, offset); |
9eb4a067 TI |
3338 | else |
3339 | page = snd_pcm_default_page_ops(substream, offset); | |
3340 | if (!page) | |
3341 | return VM_FAULT_SIGBUS; | |
b5810039 | 3342 | get_page(page); |
3ad5afcd NP |
3343 | vmf->page = page; |
3344 | return 0; | |
1da177e4 LT |
3345 | } |
3346 | ||
657b1989 TI |
3347 | static const struct vm_operations_struct snd_pcm_vm_ops_data = { |
3348 | .open = snd_pcm_mmap_data_open, | |
3349 | .close = snd_pcm_mmap_data_close, | |
3350 | }; | |
3351 | ||
3352 | static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = { | |
1da177e4 LT |
3353 | .open = snd_pcm_mmap_data_open, |
3354 | .close = snd_pcm_mmap_data_close, | |
3ad5afcd | 3355 | .fault = snd_pcm_mmap_data_fault, |
1da177e4 LT |
3356 | }; |
3357 | ||
3358 | /* | |
3359 | * mmap the DMA buffer on RAM | |
3360 | */ | |
30b771cf TI |
3361 | |
3362 | /** | |
3363 | * snd_pcm_lib_default_mmap - Default PCM data mmap function | |
3364 | * @substream: PCM substream | |
3365 | * @area: VMA | |
3366 | * | |
3367 | * This is the default mmap handler for PCM data. When mmap pcm_ops is NULL, | |
3368 | * this function is invoked implicitly. | |
3369 | */ | |
18a2b962 TI |
3370 | int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream, |
3371 | struct vm_area_struct *area) | |
1da177e4 | 3372 | { |
314e51b9 | 3373 | area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP; |
a5606f85 | 3374 | #ifdef CONFIG_GENERIC_ALLOCATOR |
05503214 NC |
3375 | if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_IRAM) { |
3376 | area->vm_page_prot = pgprot_writecombine(area->vm_page_prot); | |
3377 | return remap_pfn_range(area, area->vm_start, | |
3378 | substream->dma_buffer.addr >> PAGE_SHIFT, | |
3379 | area->vm_end - area->vm_start, area->vm_page_prot); | |
3380 | } | |
a5606f85 | 3381 | #endif /* CONFIG_GENERIC_ALLOCATOR */ |
49d776ff | 3382 | #ifndef CONFIG_X86 /* for avoiding warnings arch/x86/mm/pat.c */ |
abe594c2 | 3383 | if (IS_ENABLED(CONFIG_HAS_DMA) && !substream->ops->page && |
657b1989 TI |
3384 | substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV) |
3385 | return dma_mmap_coherent(substream->dma_buffer.dev.dev, | |
3386 | area, | |
3387 | substream->runtime->dma_area, | |
3388 | substream->runtime->dma_addr, | |
9066ae7f | 3389 | substream->runtime->dma_bytes); |
49d776ff | 3390 | #endif /* CONFIG_X86 */ |
657b1989 TI |
3391 | /* mmap with fault handler */ |
3392 | area->vm_ops = &snd_pcm_vm_ops_data_fault; | |
1da177e4 LT |
3393 | return 0; |
3394 | } | |
18a2b962 | 3395 | EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap); |
1da177e4 LT |
3396 | |
3397 | /* | |
3398 | * mmap the DMA buffer on I/O memory area | |
3399 | */ | |
3400 | #if SNDRV_PCM_INFO_MMAP_IOMEM | |
30b771cf TI |
3401 | /** |
3402 | * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem | |
3403 | * @substream: PCM substream | |
3404 | * @area: VMA | |
3405 | * | |
3406 | * When your hardware uses the iomapped pages as the hardware buffer and | |
3407 | * wants to mmap it, pass this function as mmap pcm_ops. Note that this | |
3408 | * is supposed to work only on limited architectures. | |
3409 | */ | |
877211f5 TI |
3410 | int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream, |
3411 | struct vm_area_struct *area) | |
1da177e4 | 3412 | { |
3c7f6919 | 3413 | struct snd_pcm_runtime *runtime = substream->runtime; |
1da177e4 | 3414 | |
1da177e4 | 3415 | area->vm_page_prot = pgprot_noncached(area->vm_page_prot); |
0fe09a45 | 3416 | return vm_iomap_memory(area, runtime->dma_addr, runtime->dma_bytes); |
1da177e4 | 3417 | } |
e88e8ae6 | 3418 | EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem); |
1da177e4 LT |
3419 | #endif /* SNDRV_PCM_INFO_MMAP */ |
3420 | ||
3421 | /* | |
3422 | * mmap DMA buffer | |
3423 | */ | |
877211f5 | 3424 | int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file, |
1da177e4 LT |
3425 | struct vm_area_struct *area) |
3426 | { | |
877211f5 | 3427 | struct snd_pcm_runtime *runtime; |
1da177e4 LT |
3428 | long size; |
3429 | unsigned long offset; | |
3430 | size_t dma_bytes; | |
657b1989 | 3431 | int err; |
1da177e4 LT |
3432 | |
3433 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { | |
3434 | if (!(area->vm_flags & (VM_WRITE|VM_READ))) | |
3435 | return -EINVAL; | |
3436 | } else { | |
3437 | if (!(area->vm_flags & VM_READ)) | |
3438 | return -EINVAL; | |
3439 | } | |
3440 | runtime = substream->runtime; | |
1da177e4 LT |
3441 | if (runtime->status->state == SNDRV_PCM_STATE_OPEN) |
3442 | return -EBADFD; | |
3443 | if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) | |
3444 | return -ENXIO; | |
3445 | if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED || | |
3446 | runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) | |
3447 | return -EINVAL; | |
3448 | size = area->vm_end - area->vm_start; | |
3449 | offset = area->vm_pgoff << PAGE_SHIFT; | |
3450 | dma_bytes = PAGE_ALIGN(runtime->dma_bytes); | |
3451 | if ((size_t)size > dma_bytes) | |
3452 | return -EINVAL; | |
3453 | if (offset > dma_bytes - size) | |
3454 | return -EINVAL; | |
3455 | ||
657b1989 TI |
3456 | area->vm_ops = &snd_pcm_vm_ops_data; |
3457 | area->vm_private_data = substream; | |
1da177e4 | 3458 | if (substream->ops->mmap) |
657b1989 | 3459 | err = substream->ops->mmap(substream, area); |
1da177e4 | 3460 | else |
18a2b962 | 3461 | err = snd_pcm_lib_default_mmap(substream, area); |
657b1989 TI |
3462 | if (!err) |
3463 | atomic_inc(&substream->mmap_count); | |
3464 | return err; | |
1da177e4 | 3465 | } |
e88e8ae6 TI |
3466 | EXPORT_SYMBOL(snd_pcm_mmap_data); |
3467 | ||
1da177e4 LT |
3468 | static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area) |
3469 | { | |
877211f5 TI |
3470 | struct snd_pcm_file * pcm_file; |
3471 | struct snd_pcm_substream *substream; | |
1da177e4 LT |
3472 | unsigned long offset; |
3473 | ||
3474 | pcm_file = file->private_data; | |
3475 | substream = pcm_file->substream; | |
7eaa943c TI |
3476 | if (PCM_RUNTIME_CHECK(substream)) |
3477 | return -ENXIO; | |
1da177e4 LT |
3478 | |
3479 | offset = area->vm_pgoff << PAGE_SHIFT; | |
3480 | switch (offset) { | |
3481 | case SNDRV_PCM_MMAP_OFFSET_STATUS: | |
42f94597 | 3482 | if (!pcm_status_mmap_allowed(pcm_file)) |
1da177e4 LT |
3483 | return -ENXIO; |
3484 | return snd_pcm_mmap_status(substream, file, area); | |
3485 | case SNDRV_PCM_MMAP_OFFSET_CONTROL: | |
b602aa8e | 3486 | if (!pcm_control_mmap_allowed(pcm_file)) |
1da177e4 LT |
3487 | return -ENXIO; |
3488 | return snd_pcm_mmap_control(substream, file, area); | |
3489 | default: | |
3490 | return snd_pcm_mmap_data(substream, file, area); | |
3491 | } | |
3492 | return 0; | |
3493 | } | |
3494 | ||
3495 | static int snd_pcm_fasync(int fd, struct file * file, int on) | |
3496 | { | |
877211f5 TI |
3497 | struct snd_pcm_file * pcm_file; |
3498 | struct snd_pcm_substream *substream; | |
3499 | struct snd_pcm_runtime *runtime; | |
1da177e4 LT |
3500 | |
3501 | pcm_file = file->private_data; | |
3502 | substream = pcm_file->substream; | |
7eaa943c | 3503 | if (PCM_RUNTIME_CHECK(substream)) |
d05468b7 | 3504 | return -ENXIO; |
1da177e4 | 3505 | runtime = substream->runtime; |
d05468b7 | 3506 | return fasync_helper(fd, file, on, &runtime->fasync); |
1da177e4 LT |
3507 | } |
3508 | ||
3509 | /* | |
3510 | * ioctl32 compat | |
3511 | */ | |
3512 | #ifdef CONFIG_COMPAT | |
3513 | #include "pcm_compat.c" | |
3514 | #else | |
3515 | #define snd_pcm_ioctl_compat NULL | |
3516 | #endif | |
3517 | ||
3518 | /* | |
3519 | * To be removed helpers to keep binary compatibility | |
3520 | */ | |
3521 | ||
59d48582 | 3522 | #ifdef CONFIG_SND_SUPPORT_OLD_API |
1da177e4 LT |
3523 | #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5)) |
3524 | #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5)) | |
3525 | ||
877211f5 TI |
3526 | static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params, |
3527 | struct snd_pcm_hw_params_old *oparams) | |
1da177e4 LT |
3528 | { |
3529 | unsigned int i; | |
3530 | ||
3531 | memset(params, 0, sizeof(*params)); | |
3532 | params->flags = oparams->flags; | |
3533 | for (i = 0; i < ARRAY_SIZE(oparams->masks); i++) | |
3534 | params->masks[i].bits[0] = oparams->masks[i]; | |
3535 | memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals)); | |
3536 | params->rmask = __OLD_TO_NEW_MASK(oparams->rmask); | |
3537 | params->cmask = __OLD_TO_NEW_MASK(oparams->cmask); | |
3538 | params->info = oparams->info; | |
3539 | params->msbits = oparams->msbits; | |
3540 | params->rate_num = oparams->rate_num; | |
3541 | params->rate_den = oparams->rate_den; | |
3542 | params->fifo_size = oparams->fifo_size; | |
3543 | } | |
3544 | ||
877211f5 TI |
3545 | static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams, |
3546 | struct snd_pcm_hw_params *params) | |
1da177e4 LT |
3547 | { |
3548 | unsigned int i; | |
3549 | ||
3550 | memset(oparams, 0, sizeof(*oparams)); | |
3551 | oparams->flags = params->flags; | |
3552 | for (i = 0; i < ARRAY_SIZE(oparams->masks); i++) | |
3553 | oparams->masks[i] = params->masks[i].bits[0]; | |
3554 | memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals)); | |
3555 | oparams->rmask = __NEW_TO_OLD_MASK(params->rmask); | |
3556 | oparams->cmask = __NEW_TO_OLD_MASK(params->cmask); | |
3557 | oparams->info = params->info; | |
3558 | oparams->msbits = params->msbits; | |
3559 | oparams->rate_num = params->rate_num; | |
3560 | oparams->rate_den = params->rate_den; | |
3561 | oparams->fifo_size = params->fifo_size; | |
3562 | } | |
3563 | ||
877211f5 TI |
3564 | static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream, |
3565 | struct snd_pcm_hw_params_old __user * _oparams) | |
1da177e4 | 3566 | { |
877211f5 TI |
3567 | struct snd_pcm_hw_params *params; |
3568 | struct snd_pcm_hw_params_old *oparams = NULL; | |
1da177e4 LT |
3569 | int err; |
3570 | ||
3571 | params = kmalloc(sizeof(*params), GFP_KERNEL); | |
ef44a1ec LZ |
3572 | if (!params) |
3573 | return -ENOMEM; | |
1da177e4 | 3574 | |
ef44a1ec LZ |
3575 | oparams = memdup_user(_oparams, sizeof(*oparams)); |
3576 | if (IS_ERR(oparams)) { | |
3577 | err = PTR_ERR(oparams); | |
1da177e4 LT |
3578 | goto out; |
3579 | } | |
3580 | snd_pcm_hw_convert_from_old_params(params, oparams); | |
3581 | err = snd_pcm_hw_refine(substream, params); | |
f74ae15f TS |
3582 | if (err < 0) |
3583 | goto out_old; | |
ef44a1ec | 3584 | |
f74ae15f TS |
3585 | err = fixup_unreferenced_params(substream, params); |
3586 | if (err < 0) | |
3587 | goto out_old; | |
ef44a1ec | 3588 | |
f74ae15f TS |
3589 | snd_pcm_hw_convert_to_old_params(oparams, params); |
3590 | if (copy_to_user(_oparams, oparams, sizeof(*oparams))) | |
3591 | err = -EFAULT; | |
3592 | out_old: | |
ef44a1ec | 3593 | kfree(oparams); |
1da177e4 LT |
3594 | out: |
3595 | kfree(params); | |
1da177e4 LT |
3596 | return err; |
3597 | } | |
3598 | ||
877211f5 TI |
3599 | static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream, |
3600 | struct snd_pcm_hw_params_old __user * _oparams) | |
1da177e4 | 3601 | { |
877211f5 TI |
3602 | struct snd_pcm_hw_params *params; |
3603 | struct snd_pcm_hw_params_old *oparams = NULL; | |
1da177e4 LT |
3604 | int err; |
3605 | ||
3606 | params = kmalloc(sizeof(*params), GFP_KERNEL); | |
ef44a1ec LZ |
3607 | if (!params) |
3608 | return -ENOMEM; | |
3609 | ||
3610 | oparams = memdup_user(_oparams, sizeof(*oparams)); | |
3611 | if (IS_ERR(oparams)) { | |
3612 | err = PTR_ERR(oparams); | |
1da177e4 LT |
3613 | goto out; |
3614 | } | |
f74ae15f | 3615 | |
1da177e4 LT |
3616 | snd_pcm_hw_convert_from_old_params(params, oparams); |
3617 | err = snd_pcm_hw_params(substream, params); | |
f74ae15f TS |
3618 | if (err < 0) |
3619 | goto out_old; | |
ef44a1ec | 3620 | |
f74ae15f TS |
3621 | snd_pcm_hw_convert_to_old_params(oparams, params); |
3622 | if (copy_to_user(_oparams, oparams, sizeof(*oparams))) | |
3623 | err = -EFAULT; | |
3624 | out_old: | |
ef44a1ec | 3625 | kfree(oparams); |
1da177e4 LT |
3626 | out: |
3627 | kfree(params); | |
1da177e4 LT |
3628 | return err; |
3629 | } | |
59d48582 | 3630 | #endif /* CONFIG_SND_SUPPORT_OLD_API */ |
1da177e4 | 3631 | |
7003609b | 3632 | #ifndef CONFIG_MMU |
55c63bd2 DG |
3633 | static unsigned long snd_pcm_get_unmapped_area(struct file *file, |
3634 | unsigned long addr, | |
3635 | unsigned long len, | |
3636 | unsigned long pgoff, | |
3637 | unsigned long flags) | |
3638 | { | |
3639 | struct snd_pcm_file *pcm_file = file->private_data; | |
3640 | struct snd_pcm_substream *substream = pcm_file->substream; | |
3641 | struct snd_pcm_runtime *runtime = substream->runtime; | |
3642 | unsigned long offset = pgoff << PAGE_SHIFT; | |
3643 | ||
3644 | switch (offset) { | |
3645 | case SNDRV_PCM_MMAP_OFFSET_STATUS: | |
3646 | return (unsigned long)runtime->status; | |
3647 | case SNDRV_PCM_MMAP_OFFSET_CONTROL: | |
3648 | return (unsigned long)runtime->control; | |
3649 | default: | |
3650 | return (unsigned long)runtime->dma_area + offset; | |
3651 | } | |
7003609b CC |
3652 | } |
3653 | #else | |
55c63bd2 | 3654 | # define snd_pcm_get_unmapped_area NULL |
7003609b CC |
3655 | #endif |
3656 | ||
1da177e4 LT |
3657 | /* |
3658 | * Register section | |
3659 | */ | |
3660 | ||
9c2e08c5 | 3661 | const struct file_operations snd_pcm_f_ops[2] = { |
1da177e4 | 3662 | { |
2af677fc CL |
3663 | .owner = THIS_MODULE, |
3664 | .write = snd_pcm_write, | |
1c65d986 | 3665 | .write_iter = snd_pcm_writev, |
f87135f5 | 3666 | .open = snd_pcm_playback_open, |
2af677fc | 3667 | .release = snd_pcm_release, |
02f4865f | 3668 | .llseek = no_llseek, |
6448fcba | 3669 | .poll = snd_pcm_poll, |
67616fed | 3670 | .unlocked_ioctl = snd_pcm_ioctl, |
2af677fc CL |
3671 | .compat_ioctl = snd_pcm_ioctl_compat, |
3672 | .mmap = snd_pcm_mmap, | |
3673 | .fasync = snd_pcm_fasync, | |
55c63bd2 | 3674 | .get_unmapped_area = snd_pcm_get_unmapped_area, |
1da177e4 LT |
3675 | }, |
3676 | { | |
2af677fc CL |
3677 | .owner = THIS_MODULE, |
3678 | .read = snd_pcm_read, | |
1c65d986 | 3679 | .read_iter = snd_pcm_readv, |
f87135f5 | 3680 | .open = snd_pcm_capture_open, |
2af677fc | 3681 | .release = snd_pcm_release, |
02f4865f | 3682 | .llseek = no_llseek, |
6448fcba | 3683 | .poll = snd_pcm_poll, |
67616fed | 3684 | .unlocked_ioctl = snd_pcm_ioctl, |
2af677fc CL |
3685 | .compat_ioctl = snd_pcm_ioctl_compat, |
3686 | .mmap = snd_pcm_mmap, | |
3687 | .fasync = snd_pcm_fasync, | |
55c63bd2 | 3688 | .get_unmapped_area = snd_pcm_get_unmapped_area, |
1da177e4 LT |
3689 | } |
3690 | }; |