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