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