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