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