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