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