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