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