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