[PATCH] DVB: Add static firmware compilation again
[linux-2.6/history.git] / sound / core / pcm_native.c
blob053d7b38a56c1b4bd0f368481485748cf8ef3c08
1 /*
2 * Digital Audio (PCM) abstract layer
3 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
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.
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.
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
22 #include <sound/driver.h>
23 #include <linux/mm.h>
24 #include <linux/file.h>
25 #include <linux/slab.h>
26 #include <linux/time.h>
27 #include <linux/uio.h>
28 #include <sound/core.h>
29 #include <sound/control.h>
30 #include <sound/info.h>
31 #include <sound/pcm.h>
32 #include <sound/pcm_params.h>
33 #include <sound/timer.h>
34 #include <sound/minors.h>
37 * Compatibility
40 struct sndrv_pcm_hw_params_old {
41 unsigned int flags;
42 unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT -
43 SNDRV_PCM_HW_PARAM_ACCESS + 1];
44 struct sndrv_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME -
45 SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1];
46 unsigned int rmask;
47 unsigned int cmask;
48 unsigned int info;
49 unsigned int msbits;
50 unsigned int rate_num;
51 unsigned int rate_den;
52 sndrv_pcm_uframes_t fifo_size;
53 unsigned char reserved[64];
56 #define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct sndrv_pcm_hw_params_old)
57 #define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct sndrv_pcm_hw_params_old)
59 static int snd_pcm_hw_refine_old_user(snd_pcm_substream_t * substream, struct sndrv_pcm_hw_params_old * _oparams);
60 static int snd_pcm_hw_params_old_user(snd_pcm_substream_t * substream, struct sndrv_pcm_hw_params_old * _oparams);
66 rwlock_t snd_pcm_link_rwlock = RW_LOCK_UNLOCKED;
69 static inline mm_segment_t snd_enter_user(void)
71 mm_segment_t fs = get_fs();
72 set_fs(get_ds());
73 return fs;
76 static inline void snd_leave_user(mm_segment_t fs)
78 set_fs(fs);
83 int snd_pcm_info(snd_pcm_substream_t * substream, snd_pcm_info_t *info)
85 snd_pcm_runtime_t * runtime;
86 snd_pcm_t *pcm = substream->pcm;
87 snd_pcm_str_t *pstr = substream->pstr;
89 snd_assert(substream != NULL, return -ENXIO);
90 memset(info, 0, sizeof(*info));
91 info->card = pcm->card->number;
92 info->device = pcm->device;
93 info->stream = substream->stream;
94 info->subdevice = substream->number;
95 strlcpy(info->id, pcm->id, sizeof(info->id));
96 strlcpy(info->name, pcm->name, sizeof(info->name));
97 info->dev_class = pcm->dev_class;
98 info->dev_subclass = pcm->dev_subclass;
99 info->subdevices_count = pstr->substream_count;
100 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
101 strlcpy(info->subname, substream->name, sizeof(info->subname));
102 runtime = substream->runtime;
103 /* AB: FIXME!!! This is definitely nonsense */
104 if (runtime) {
105 info->sync = runtime->sync;
106 substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_INFO, info);
108 return 0;
111 int snd_pcm_info_user(snd_pcm_substream_t * substream, snd_pcm_info_t * _info)
113 snd_pcm_info_t info;
114 int err = snd_pcm_info(substream, &info);
115 if (copy_to_user(_info, &info, sizeof(info)))
116 return -EFAULT;
117 return err;
120 #undef RULES_DEBUG
122 #ifdef RULES_DEBUG
123 #define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v
124 char *snd_pcm_hw_param_names[] = {
125 HW_PARAM(ACCESS),
126 HW_PARAM(FORMAT),
127 HW_PARAM(SUBFORMAT),
128 HW_PARAM(SAMPLE_BITS),
129 HW_PARAM(FRAME_BITS),
130 HW_PARAM(CHANNELS),
131 HW_PARAM(RATE),
132 HW_PARAM(PERIOD_TIME),
133 HW_PARAM(PERIOD_SIZE),
134 HW_PARAM(PERIOD_BYTES),
135 HW_PARAM(PERIODS),
136 HW_PARAM(BUFFER_TIME),
137 HW_PARAM(BUFFER_SIZE),
138 HW_PARAM(BUFFER_BYTES),
139 HW_PARAM(TICK_TIME),
141 #endif
143 int snd_pcm_hw_refine(snd_pcm_substream_t *substream,
144 snd_pcm_hw_params_t *params)
146 unsigned int k;
147 snd_pcm_hardware_t *hw;
148 snd_interval_t *i = NULL;
149 snd_mask_t *m = NULL;
150 snd_pcm_hw_constraints_t *constrs = &substream->runtime->hw_constraints;
151 unsigned int rstamps[constrs->rules_num];
152 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
153 unsigned int stamp = 2;
154 int changed, again;
156 params->info = 0;
157 params->fifo_size = 0;
158 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
159 params->msbits = 0;
160 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
161 params->rate_num = 0;
162 params->rate_den = 0;
165 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
166 m = hw_param_mask(params, k);
167 if (snd_mask_empty(m))
168 return -EINVAL;
169 if (!(params->rmask & (1 << k)))
170 continue;
171 #ifdef RULES_DEBUG
172 printk("%s = ", snd_pcm_hw_param_names[k]);
173 printk("%04x%04x%04x%04x -> ", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
174 #endif
175 changed = snd_mask_refine(m, constrs_mask(constrs, k));
176 #ifdef RULES_DEBUG
177 printk("%04x%04x%04x%04x\n", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
178 #endif
179 if (changed)
180 params->cmask |= 1 << k;
181 if (changed < 0)
182 return changed;
185 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
186 i = hw_param_interval(params, k);
187 if (snd_interval_empty(i))
188 return -EINVAL;
189 if (!(params->rmask & (1 << k)))
190 continue;
191 #ifdef RULES_DEBUG
192 printk("%s = ", snd_pcm_hw_param_names[k]);
193 if (i->empty)
194 printk("empty");
195 else
196 printk("%c%u %u%c",
197 i->openmin ? '(' : '[', i->min,
198 i->max, i->openmax ? ')' : ']');
199 printk(" -> ");
200 #endif
201 changed = snd_interval_refine(i, constrs_interval(constrs, k));
202 #ifdef RULES_DEBUG
203 if (i->empty)
204 printk("empty\n");
205 else
206 printk("%c%u %u%c\n",
207 i->openmin ? '(' : '[', i->min,
208 i->max, i->openmax ? ')' : ']');
209 #endif
210 if (changed)
211 params->cmask |= 1 << k;
212 if (changed < 0)
213 return changed;
216 for (k = 0; k < constrs->rules_num; k++)
217 rstamps[k] = 0;
218 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
219 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
220 do {
221 again = 0;
222 for (k = 0; k < constrs->rules_num; k++) {
223 snd_pcm_hw_rule_t *r = &constrs->rules[k];
224 unsigned int d;
225 int doit = 0;
226 if (r->cond && !(r->cond & params->flags))
227 continue;
228 for (d = 0; r->deps[d] >= 0; d++) {
229 if (vstamps[r->deps[d]] > rstamps[k]) {
230 doit = 1;
231 break;
234 if (!doit)
235 continue;
236 #ifdef RULES_DEBUG
237 printk("Rule %d [%p]: ", k, r->func);
238 if (r->var >= 0) {
239 printk("%s = ", snd_pcm_hw_param_names[r->var]);
240 if (hw_is_mask(r->var)) {
241 m = hw_param_mask(params, r->var);
242 printk("%x", *m->bits);
243 } else {
244 i = hw_param_interval(params, r->var);
245 if (i->empty)
246 printk("empty");
247 else
248 printk("%c%u %u%c",
249 i->openmin ? '(' : '[', i->min,
250 i->max, i->openmax ? ')' : ']');
253 #endif
254 changed = r->func(params, r);
255 #ifdef RULES_DEBUG
256 if (r->var >= 0) {
257 printk(" -> ");
258 if (hw_is_mask(r->var))
259 printk("%x", *m->bits);
260 else {
261 if (i->empty)
262 printk("empty");
263 else
264 printk("%c%u %u%c",
265 i->openmin ? '(' : '[', i->min,
266 i->max, i->openmax ? ')' : ']');
269 printk("\n");
270 #endif
271 rstamps[k] = stamp;
272 if (changed && r->var >= 0) {
273 params->cmask |= (1 << r->var);
274 vstamps[r->var] = stamp;
275 again = 1;
277 if (changed < 0)
278 return changed;
279 stamp++;
281 } while (again);
282 if (!params->msbits) {
283 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
284 if (snd_interval_single(i))
285 params->msbits = snd_interval_value(i);
288 if (!params->rate_den) {
289 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
290 if (snd_interval_single(i)) {
291 params->rate_num = snd_interval_value(i);
292 params->rate_den = 1;
296 hw = &substream->runtime->hw;
297 if (!params->info)
298 params->info = hw->info;
299 if (!params->fifo_size)
300 params->fifo_size = hw->fifo_size;
301 params->rmask = 0;
302 return 0;
305 static int snd_pcm_hw_refine_user(snd_pcm_substream_t * substream, snd_pcm_hw_params_t * _params)
307 snd_pcm_hw_params_t params;
308 int err;
309 if (copy_from_user(&params, _params, sizeof(params)))
310 return -EFAULT;
311 err = snd_pcm_hw_refine(substream, &params);
312 if (copy_to_user(_params, &params, sizeof(params)))
313 return -EFAULT;
314 return err;
317 static int snd_pcm_hw_params(snd_pcm_substream_t *substream,
318 snd_pcm_hw_params_t *params)
320 snd_pcm_runtime_t *runtime;
321 int err;
322 unsigned int bits;
323 snd_pcm_uframes_t frames;
325 snd_assert(substream != NULL, return -ENXIO);
326 runtime = substream->runtime;
327 snd_assert(runtime != NULL, return -ENXIO);
328 switch (runtime->status->state) {
329 case SNDRV_PCM_STATE_OPEN:
330 case SNDRV_PCM_STATE_SETUP:
331 case SNDRV_PCM_STATE_PREPARED:
332 break;
333 default:
334 return -EBADFD;
336 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
337 if (!substream->oss.oss)
338 #endif
339 if (atomic_read(&runtime->mmap_count))
340 return -EBADFD;
342 params->rmask = ~0U;
343 err = snd_pcm_hw_refine(substream, params);
344 if (err < 0)
345 goto _error;
347 err = snd_pcm_hw_params_choose(substream, params);
348 if (err < 0)
349 goto _error;
351 if (substream->ops->hw_params != NULL) {
352 err = substream->ops->hw_params(substream, params);
353 if (err < 0)
354 goto _error;
357 runtime->access = params_access(params);
358 runtime->format = params_format(params);
359 runtime->subformat = params_subformat(params);
360 runtime->channels = params_channels(params);
361 runtime->rate = params_rate(params);
362 runtime->period_size = params_period_size(params);
363 runtime->periods = params_periods(params);
364 runtime->buffer_size = params_buffer_size(params);
365 runtime->tick_time = params_tick_time(params);
366 runtime->info = params->info;
367 runtime->rate_num = params->rate_num;
368 runtime->rate_den = params->rate_den;
370 bits = snd_pcm_format_physical_width(runtime->format);
371 runtime->sample_bits = bits;
372 bits *= runtime->channels;
373 runtime->frame_bits = bits;
374 frames = 1;
375 while (bits % 8 != 0) {
376 bits *= 2;
377 frames *= 2;
379 runtime->byte_align = bits / 8;
380 runtime->min_align = frames;
382 /* Default sw params */
383 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
384 runtime->period_step = 1;
385 runtime->sleep_min = 0;
386 runtime->control->avail_min = runtime->period_size;
387 runtime->xfer_align = runtime->period_size;
388 runtime->start_threshold = 1;
389 runtime->stop_threshold = runtime->buffer_size;
390 runtime->silence_threshold = 0;
391 runtime->silence_size = 0;
392 runtime->boundary = runtime->buffer_size;
393 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
394 runtime->boundary *= 2;
396 snd_pcm_timer_resolution_change(substream);
397 runtime->status->state = SNDRV_PCM_STATE_SETUP;
398 return 0;
399 _error:
400 /* hardware might be unuseable from this time,
401 so we force application to retry to set
402 the correct hardware parameter settings */
403 runtime->status->state = SNDRV_PCM_STATE_OPEN;
404 if (substream->ops->hw_free != NULL)
405 substream->ops->hw_free(substream);
406 return err;
409 static int snd_pcm_hw_params_user(snd_pcm_substream_t * substream, snd_pcm_hw_params_t * _params)
411 snd_pcm_hw_params_t params;
412 int err;
413 if (copy_from_user(&params, _params, sizeof(params)))
414 return -EFAULT;
415 err = snd_pcm_hw_params(substream, &params);
416 if (copy_to_user(_params, &params, sizeof(params)))
417 return -EFAULT;
418 return err;
421 static int snd_pcm_hw_free(snd_pcm_substream_t * substream)
423 snd_pcm_runtime_t *runtime;
424 int result;
426 snd_assert(substream != NULL, return -ENXIO);
427 runtime = substream->runtime;
428 snd_assert(runtime != NULL, return -ENXIO);
429 switch (runtime->status->state) {
430 case SNDRV_PCM_STATE_SETUP:
431 case SNDRV_PCM_STATE_PREPARED:
432 break;
433 default:
434 return -EBADFD;
436 if (atomic_read(&runtime->mmap_count))
437 return -EBADFD;
438 if (substream->ops->hw_free == NULL) {
439 runtime->status->state = SNDRV_PCM_STATE_OPEN;
440 return 0;
442 result = substream->ops->hw_free(substream);
443 runtime->status->state = SNDRV_PCM_STATE_OPEN;
444 return result;
447 static int snd_pcm_sw_params(snd_pcm_substream_t * substream, snd_pcm_sw_params_t *params)
449 snd_pcm_runtime_t *runtime;
450 snd_assert(substream != NULL, return -ENXIO);
451 runtime = substream->runtime;
452 snd_assert(runtime != NULL, return -ENXIO);
453 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
454 return -EBADFD;
456 if (params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
457 return -EINVAL;
458 if (params->avail_min == 0)
459 return -EINVAL;
460 if (params->xfer_align == 0 ||
461 params->xfer_align % runtime->min_align != 0)
462 return -EINVAL;
463 if (params->silence_size >= runtime->boundary) {
464 if (params->silence_threshold != 0)
465 return -EINVAL;
466 } else {
467 if (params->silence_size > params->silence_threshold)
468 return -EINVAL;
469 if (params->silence_threshold > runtime->buffer_size)
470 return -EINVAL;
472 snd_pcm_stream_lock_irq(substream);
473 runtime->tstamp_mode = params->tstamp_mode;
474 runtime->sleep_min = params->sleep_min;
475 runtime->period_step = params->period_step;
476 runtime->control->avail_min = params->avail_min;
477 runtime->start_threshold = params->start_threshold;
478 runtime->stop_threshold = params->stop_threshold;
479 runtime->silence_threshold = params->silence_threshold;
480 runtime->silence_size = params->silence_size;
481 runtime->xfer_align = params->xfer_align;
482 params->boundary = runtime->boundary;
483 if (snd_pcm_running(substream)) {
484 if (runtime->sleep_min)
485 snd_pcm_tick_prepare(substream);
486 else
487 snd_pcm_tick_set(substream, 0);
488 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
489 runtime->silence_size > 0)
490 snd_pcm_playback_silence(substream, ULONG_MAX);
491 wake_up(&runtime->sleep);
493 snd_pcm_stream_unlock_irq(substream);
494 return 0;
497 static int snd_pcm_sw_params_user(snd_pcm_substream_t * substream, snd_pcm_sw_params_t * _params)
499 snd_pcm_sw_params_t params;
500 int err;
501 if (copy_from_user(&params, _params, sizeof(params)))
502 return -EFAULT;
503 err = snd_pcm_sw_params(substream, &params);
504 if (copy_to_user(_params, &params, sizeof(params)))
505 return -EFAULT;
506 return err;
509 int snd_pcm_status(snd_pcm_substream_t *substream,
510 snd_pcm_status_t *status)
512 snd_pcm_runtime_t *runtime = substream->runtime;
514 snd_pcm_stream_lock_irq(substream);
515 status->state = runtime->status->state;
516 status->suspended_state = runtime->status->suspended_state;
517 if (status->state == SNDRV_PCM_STATE_OPEN)
518 goto _end;
519 status->trigger_tstamp = runtime->trigger_tstamp;
520 if (snd_pcm_running(substream)) {
521 snd_pcm_update_hw_ptr(substream);
522 if (runtime->tstamp_mode & SNDRV_PCM_TSTAMP_MMAP)
523 status->tstamp = runtime->status->tstamp;
524 else
525 snd_timestamp_now(&status->tstamp, runtime->tstamp_timespec);
526 } else
527 snd_timestamp_now(&status->tstamp, runtime->tstamp_timespec);
528 status->appl_ptr = runtime->control->appl_ptr;
529 status->hw_ptr = runtime->status->hw_ptr;
530 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
531 status->avail = snd_pcm_playback_avail(runtime);
532 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
533 runtime->status->state == SNDRV_PCM_STATE_DRAINING)
534 status->delay = runtime->buffer_size - status->avail;
535 else
536 status->delay = 0;
537 } else {
538 status->avail = snd_pcm_capture_avail(runtime);
539 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
540 status->delay = status->avail;
541 else
542 status->delay = 0;
544 status->avail_max = runtime->avail_max;
545 status->overrange = runtime->overrange;
546 runtime->avail_max = 0;
547 runtime->overrange = 0;
548 _end:
549 snd_pcm_stream_unlock_irq(substream);
550 return 0;
553 static int snd_pcm_status_user(snd_pcm_substream_t * substream, snd_pcm_status_t * _status)
555 snd_pcm_status_t status;
556 snd_pcm_runtime_t *runtime;
557 int res;
559 snd_assert(substream != NULL, return -ENXIO);
560 runtime = substream->runtime;
561 memset(&status, 0, sizeof(status));
562 res = snd_pcm_status(substream, &status);
563 if (res < 0)
564 return res;
565 if (copy_to_user(_status, &status, sizeof(status)))
566 return -EFAULT;
567 return 0;
570 static int snd_pcm_channel_info(snd_pcm_substream_t * substream, snd_pcm_channel_info_t * _info)
572 snd_pcm_channel_info_t info;
573 snd_pcm_runtime_t *runtime;
574 int res;
575 unsigned int channel;
577 snd_assert(substream != NULL, return -ENXIO);
578 if (copy_from_user(&info, _info, sizeof(info)))
579 return -EFAULT;
580 channel = info.channel;
581 runtime = substream->runtime;
582 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
583 return -EBADFD;
584 if (channel >= runtime->channels)
585 return -EINVAL;
586 memset(&info, 0, sizeof(info));
587 info.channel = channel;
588 res = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, &info);
589 if (res < 0)
590 return res;
591 if (copy_to_user(_info, &info, sizeof(info)))
592 return -EFAULT;
593 return 0;
596 static void snd_pcm_trigger_tstamp(snd_pcm_substream_t *substream)
598 snd_pcm_runtime_t *runtime = substream->runtime;
599 if (runtime->trigger_master == NULL)
600 return;
601 if (runtime->trigger_master == substream) {
602 snd_timestamp_now(&runtime->trigger_tstamp, runtime->tstamp_timespec);
603 } else {
604 snd_pcm_trigger_tstamp(runtime->trigger_master);
605 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
607 runtime->trigger_master = NULL;
610 struct action_ops {
611 int (*pre_action)(snd_pcm_substream_t *substream, int state);
612 int (*do_action)(snd_pcm_substream_t *substream, int state);
613 void (*post_action)(snd_pcm_substream_t *substream, int state);
617 * this functions is core for handling of linked stream
618 * Note: the stream state might be changed also on failure
619 * Note2: call with calling stream lock + link lock
621 static int snd_pcm_action_group(struct action_ops *ops,
622 snd_pcm_substream_t *substream,
623 int state)
625 struct list_head *pos;
626 snd_pcm_substream_t *s = NULL;
627 int err, res = 0;
629 snd_pcm_group_for_each(pos, substream) {
630 s = snd_pcm_group_substream_entry(pos);
631 if (s != substream)
632 spin_lock(&s->self_group.lock);
633 res = ops->pre_action(s, state);
634 if (res < 0)
635 break;
637 if (res >= 0) {
638 snd_pcm_group_for_each(pos, substream) {
639 s = snd_pcm_group_substream_entry(pos);
640 err = ops->do_action(s, state);
641 if (err < 0) {
642 if (res == 0)
643 res = err;
644 } else {
645 ops->post_action(s, state);
647 if (s != substream)
648 spin_unlock(&s->self_group.lock);
650 } else {
651 snd_pcm_substream_t *s1;
652 /* unlock all streams */
653 snd_pcm_group_for_each(pos, substream) {
654 s1 = snd_pcm_group_substream_entry(pos);
655 if (s1 != substream)
656 spin_unlock(&s1->self_group.lock);
657 if (s1 == s) /* end */
658 break;
661 return res;
665 * Note: call with stream lock
667 static int snd_pcm_action_single(struct action_ops *ops,
668 snd_pcm_substream_t *substream,
669 int state)
671 int res;
673 res = ops->pre_action(substream, state);
674 if (res < 0)
675 return res;
676 res = ops->do_action(substream, state);
677 if (res == 0) {
678 ops->post_action(substream, state);
680 return res;
684 * Note: call with stream lock
686 static int snd_pcm_action(struct action_ops *ops,
687 snd_pcm_substream_t *substream,
688 int state)
690 int res;
692 if (snd_pcm_stream_linked(substream)) {
693 if (!spin_trylock(&substream->group->lock)) {
694 spin_unlock(&substream->self_group.lock);
695 spin_lock(&substream->group->lock);
696 spin_lock(&substream->self_group.lock);
698 res = snd_pcm_action_group(ops, substream, state);
699 spin_unlock(&substream->group->lock);
700 } else {
701 res = snd_pcm_action_single(ops, substream, state);
703 return res;
707 * Note: don't use any locks before
709 static int snd_pcm_action_lock_irq(struct action_ops *ops,
710 snd_pcm_substream_t *substream,
711 int state)
713 int res;
715 read_lock_irq(&snd_pcm_link_rwlock);
716 if (snd_pcm_stream_linked(substream)) {
717 spin_lock(&substream->group->lock);
718 spin_lock(&substream->self_group.lock);
719 res = snd_pcm_action_group(ops, substream, state);
720 spin_unlock(&substream->self_group.lock);
721 spin_unlock(&substream->group->lock);
722 } else {
723 spin_lock(&substream->self_group.lock);
724 res = snd_pcm_action_single(ops, substream, state);
725 spin_unlock(&substream->self_group.lock);
727 read_unlock_irq(&snd_pcm_link_rwlock);
728 return res;
731 static int snd_pcm_pre_start(snd_pcm_substream_t *substream, int state)
733 snd_pcm_runtime_t *runtime = substream->runtime;
734 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
735 return -EBADFD;
736 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
737 !snd_pcm_playback_data(substream))
738 return -EPIPE;
739 runtime->trigger_master = substream;
740 return 0;
743 static int snd_pcm_do_start(snd_pcm_substream_t *substream, int state)
745 if (substream->runtime->trigger_master != substream)
746 return 0;
747 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
750 static void snd_pcm_post_start(snd_pcm_substream_t *substream, int state)
752 snd_pcm_runtime_t *runtime = substream->runtime;
753 snd_pcm_trigger_tstamp(substream);
754 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
755 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
756 runtime->silence_size > 0)
757 snd_pcm_playback_silence(substream, ULONG_MAX);
758 if (runtime->sleep_min)
759 snd_pcm_tick_prepare(substream);
760 if (substream->timer)
761 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTART, &runtime->trigger_tstamp);
764 static struct action_ops snd_pcm_action_start = {
765 .pre_action = snd_pcm_pre_start,
766 .do_action = snd_pcm_do_start,
767 .post_action = snd_pcm_post_start
771 * snd_pcm_start
773 int snd_pcm_start(snd_pcm_substream_t *substream)
775 return snd_pcm_action(&snd_pcm_action_start, substream, 0);
778 static int snd_pcm_pre_stop(snd_pcm_substream_t *substream, int state)
780 snd_pcm_runtime_t *runtime = substream->runtime;
781 if (substream->runtime->status->state != SNDRV_PCM_STATE_RUNNING &&
782 substream->runtime->status->state != SNDRV_PCM_STATE_DRAINING)
783 return -EBADFD;
784 runtime->trigger_master = substream;
785 return 0;
788 static int snd_pcm_do_stop(snd_pcm_substream_t *substream, int state)
790 if (substream->runtime->trigger_master != substream)
791 return 0;
792 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
795 static void snd_pcm_post_stop(snd_pcm_substream_t *substream, int state)
797 snd_pcm_runtime_t *runtime = substream->runtime;
798 snd_pcm_trigger_tstamp(substream);
799 if (substream->timer)
800 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTOP, &runtime->trigger_tstamp);
801 runtime->status->state = state;
802 snd_pcm_tick_set(substream, 0);
803 wake_up(&runtime->sleep);
806 static struct action_ops snd_pcm_action_stop = {
807 .pre_action = snd_pcm_pre_stop,
808 .do_action = snd_pcm_do_stop,
809 .post_action = snd_pcm_post_stop
813 * snd_pcm_stop
815 int snd_pcm_stop(snd_pcm_substream_t *substream, int state)
817 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
820 static int snd_pcm_pre_pause(snd_pcm_substream_t *substream, int push)
822 snd_pcm_runtime_t *runtime = substream->runtime;
823 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
824 return -ENOSYS;
825 if (push) {
826 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
827 return -EBADFD;
828 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
829 return -EBADFD;
830 runtime->trigger_master = substream;
831 return 0;
834 static int snd_pcm_do_pause(snd_pcm_substream_t *substream, int push)
836 if (substream->runtime->trigger_master != substream)
837 return 0;
838 return substream->ops->trigger(substream,
839 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
840 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
843 static void snd_pcm_post_pause(snd_pcm_substream_t *substream, int push)
845 snd_pcm_runtime_t *runtime = substream->runtime;
846 snd_pcm_trigger_tstamp(substream);
847 if (push) {
848 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
849 if (substream->timer)
850 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MPAUSE, &runtime->trigger_tstamp);
851 snd_pcm_tick_set(substream, 0);
852 wake_up(&runtime->sleep);
853 } else {
854 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
855 if (runtime->sleep_min)
856 snd_pcm_tick_prepare(substream);
857 if (substream->timer)
858 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MCONTINUE, &runtime->trigger_tstamp);
862 static struct action_ops snd_pcm_action_pause = {
863 .pre_action = snd_pcm_pre_pause,
864 .do_action = snd_pcm_do_pause,
865 .post_action = snd_pcm_post_pause
868 static int snd_pcm_pause(snd_pcm_substream_t *substream, int push)
870 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
873 #ifdef CONFIG_PM
874 /* suspend */
876 static int snd_pcm_pre_suspend(snd_pcm_substream_t *substream, int state)
878 snd_pcm_runtime_t *runtime = substream->runtime;
879 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
880 return -EBUSY;
881 runtime->status->suspended_state = runtime->status->state;
882 runtime->trigger_master = substream;
883 return 0;
886 static int snd_pcm_do_suspend(snd_pcm_substream_t *substream, int state)
888 snd_pcm_runtime_t *runtime = substream->runtime;
889 if (runtime->trigger_master != substream)
890 return 0;
891 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING)
892 return 0;
893 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
896 static void snd_pcm_post_suspend(snd_pcm_substream_t *substream, int state)
898 snd_pcm_runtime_t *runtime = substream->runtime;
899 snd_pcm_trigger_tstamp(substream);
900 if (substream->timer)
901 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MPAUSE, &runtime->trigger_tstamp);
902 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
903 snd_pcm_tick_set(substream, 0);
904 wake_up(&runtime->sleep);
907 static struct action_ops snd_pcm_action_suspend = {
908 .pre_action = snd_pcm_pre_suspend,
909 .do_action = snd_pcm_do_suspend,
910 .post_action = snd_pcm_post_suspend
914 * snd_pcm_suspend
916 int snd_pcm_suspend(snd_pcm_substream_t *substream)
918 return snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
922 * snd_pcm_suspend_all
924 int snd_pcm_suspend_all(snd_pcm_t *pcm)
926 snd_pcm_substream_t *substream;
927 int stream, err;
929 for (stream = 0; stream < 2; stream++) {
930 for (substream = pcm->streams[stream].substream; substream; substream = substream->next) {
931 /* FIXME: the open/close code should lock this as well */
932 if (substream->runtime == NULL)
933 continue;
934 snd_pcm_stream_lock(substream);
935 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
936 snd_pcm_stream_unlock(substream);
937 continue;
939 if ((err = snd_pcm_suspend(substream)) < 0) {
940 snd_pcm_stream_unlock(substream);
941 return err;
943 snd_pcm_stream_unlock(substream);
946 return 0;
949 /* resume */
951 static int snd_pcm_pre_resume(snd_pcm_substream_t *substream, int state)
953 snd_pcm_runtime_t *runtime = substream->runtime;
954 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
955 return -ENOSYS;
956 runtime->trigger_master = substream;
957 return 0;
960 static int snd_pcm_do_resume(snd_pcm_substream_t *substream, int state)
962 snd_pcm_runtime_t *runtime = substream->runtime;
963 if (runtime->trigger_master != substream)
964 return 0;
965 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING)
966 return 0;
967 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
970 static void snd_pcm_post_resume(snd_pcm_substream_t *substream, int state)
972 snd_pcm_runtime_t *runtime = substream->runtime;
973 snd_pcm_trigger_tstamp(substream);
974 if (substream->timer)
975 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MCONTINUE, &runtime->trigger_tstamp);
976 runtime->status->state = runtime->status->suspended_state;
977 if (runtime->sleep_min)
978 snd_pcm_tick_prepare(substream);
981 static struct action_ops snd_pcm_action_resume = {
982 .pre_action = snd_pcm_pre_resume,
983 .do_action = snd_pcm_do_resume,
984 .post_action = snd_pcm_post_resume
987 static int snd_pcm_resume(snd_pcm_substream_t *substream)
989 snd_card_t *card = substream->pcm->card;
990 int res;
992 snd_power_lock(card);
993 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile)) >= 0)
994 return snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
995 snd_power_unlock(card);
996 return res;
999 #else
1001 static int snd_pcm_resume(snd_pcm_substream_t *substream)
1003 return -ENOSYS;
1006 #endif /* CONFIG_PM */
1008 static int snd_pcm_xrun(snd_pcm_substream_t *substream)
1010 snd_card_t *card = substream->pcm->card;
1011 snd_pcm_runtime_t *runtime = substream->runtime;
1012 int result;
1014 snd_power_lock(card);
1015 snd_pcm_stream_lock_irq(substream);
1016 _xrun_recovery:
1017 switch (runtime->status->state) {
1018 case SNDRV_PCM_STATE_XRUN:
1019 result = 0; /* already there */
1020 break;
1021 case SNDRV_PCM_STATE_RUNNING:
1022 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1023 break;
1024 case SNDRV_PCM_STATE_SUSPENDED:
1025 snd_pcm_stream_unlock_irq(substream);
1026 result = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile);
1027 snd_pcm_stream_lock_irq(substream);
1028 if (result >= 0)
1029 goto _xrun_recovery;
1030 break;
1031 default:
1032 result = -EBADFD;
1034 snd_pcm_stream_unlock_irq(substream);
1035 snd_power_unlock(card);
1036 return result;
1039 static int snd_pcm_pre_reset(snd_pcm_substream_t * substream, int state)
1041 snd_pcm_runtime_t *runtime = substream->runtime;
1042 switch (runtime->status->state) {
1043 case SNDRV_PCM_STATE_RUNNING:
1044 case SNDRV_PCM_STATE_PREPARED:
1045 case SNDRV_PCM_STATE_PAUSED:
1046 case SNDRV_PCM_STATE_SUSPENDED:
1047 return 0;
1048 default:
1049 return -EBADFD;
1053 static int snd_pcm_do_reset(snd_pcm_substream_t * substream, int state)
1055 snd_pcm_runtime_t *runtime = substream->runtime;
1056 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, 0);
1057 if (err < 0)
1058 return err;
1059 // snd_assert(runtime->status->hw_ptr < runtime->buffer_size, );
1060 runtime->hw_ptr_base = 0;
1061 runtime->hw_ptr_interrupt = runtime->status->hw_ptr - runtime->status->hw_ptr % runtime->period_size;
1062 runtime->silence_start = runtime->status->hw_ptr;
1063 runtime->silence_filled = 0;
1064 return 0;
1067 static void snd_pcm_post_reset(snd_pcm_substream_t * substream, int state)
1069 snd_pcm_runtime_t *runtime = substream->runtime;
1070 runtime->control->appl_ptr = runtime->status->hw_ptr;
1071 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1072 runtime->silence_size > 0)
1073 snd_pcm_playback_silence(substream, ULONG_MAX);
1076 static struct action_ops snd_pcm_action_reset = {
1077 .pre_action = snd_pcm_pre_reset,
1078 .do_action = snd_pcm_do_reset,
1079 .post_action = snd_pcm_post_reset
1082 static int snd_pcm_reset(snd_pcm_substream_t *substream)
1084 return snd_pcm_action_lock_irq(&snd_pcm_action_reset, substream, 0);
1087 static int snd_pcm_pre_prepare(snd_pcm_substream_t * substream, int state)
1089 snd_pcm_runtime_t *runtime = substream->runtime;
1090 switch (runtime->status->state) {
1091 case SNDRV_PCM_STATE_OPEN:
1092 return -EBADFD;
1093 case SNDRV_PCM_STATE_RUNNING:
1094 return -EBUSY;
1095 default:
1096 return 0;
1100 static int snd_pcm_do_prepare(snd_pcm_substream_t * substream, int state)
1102 int err;
1103 err = substream->ops->prepare(substream);
1104 if (err < 0)
1105 return err;
1106 return snd_pcm_do_reset(substream, 0);
1109 static void snd_pcm_post_prepare(snd_pcm_substream_t * substream, int state)
1111 snd_pcm_runtime_t *runtime = substream->runtime;
1112 runtime->control->appl_ptr = runtime->status->hw_ptr;
1113 runtime->status->state = SNDRV_PCM_STATE_PREPARED;
1116 static struct action_ops snd_pcm_action_prepare = {
1117 .pre_action = snd_pcm_pre_prepare,
1118 .do_action = snd_pcm_do_prepare,
1119 .post_action = snd_pcm_post_prepare
1123 * snd_pcm_prepare
1125 int snd_pcm_prepare(snd_pcm_substream_t *substream)
1127 int res;
1128 snd_card_t *card = substream->pcm->card;
1130 snd_power_lock(card);
1131 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile)) >= 0)
1132 res = snd_pcm_action_lock_irq(&snd_pcm_action_prepare, substream, 0);
1133 snd_power_unlock(card);
1134 return res;
1137 static void snd_pcm_change_state(snd_pcm_substream_t *substream, int state)
1139 struct list_head *pos;
1140 snd_pcm_substream_t *s;
1142 if (snd_pcm_stream_linked(substream)) {
1143 if (!spin_trylock(&substream->group->lock)) {
1144 spin_unlock(&substream->self_group.lock);
1145 spin_lock(&substream->group->lock);
1146 spin_lock(&substream->self_group.lock);
1148 snd_pcm_group_for_each(pos, substream) {
1149 s = snd_pcm_group_substream_entry(pos);
1150 if (s != substream)
1151 spin_lock(&s->self_group.lock);
1152 s->runtime->status->state = state;
1153 if (s != substream)
1154 spin_unlock(&s->self_group.lock);
1156 spin_unlock(&substream->group->lock);
1157 } else {
1158 substream->runtime->status->state = state;
1162 static int snd_pcm_playback_drop(snd_pcm_substream_t *substream);
1164 static int snd_pcm_playback_drain(snd_pcm_substream_t * substream)
1166 snd_card_t *card;
1167 snd_pcm_runtime_t *runtime;
1168 int err, result = 0;
1169 wait_queue_t wait;
1170 enum { READY, EXPIRED, SUSPENDED, SIGNALED } state = READY;
1171 snd_pcm_uframes_t stop_threshold;
1173 snd_assert(substream != NULL, return -ENXIO);
1174 snd_assert(substream->stream == SNDRV_PCM_STREAM_PLAYBACK, return -EINVAL);
1175 runtime = substream->runtime;
1176 card = substream->pcm->card;
1178 snd_power_lock(card);
1179 snd_pcm_stream_lock_irq(substream);
1181 /* stop_threshold fixup to avoid endless loop when */
1182 /* stop_threshold > buffer_size */
1183 stop_threshold = runtime->stop_threshold;
1184 if (runtime->stop_threshold > runtime->buffer_size)
1185 runtime->stop_threshold = runtime->buffer_size;
1187 switch (runtime->status->state) {
1188 case SNDRV_PCM_STATE_PAUSED:
1189 snd_pcm_pause(substream, 0);
1190 /* Fall through */
1191 case SNDRV_PCM_STATE_RUNNING:
1192 case SNDRV_PCM_STATE_DRAINING:
1193 break;
1194 case SNDRV_PCM_STATE_SUSPENDED:
1195 snd_pcm_stream_unlock_irq(substream);
1196 result = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile);
1197 snd_pcm_stream_lock_irq(substream);
1198 if (result >= 0)
1199 snd_pcm_change_state(substream, SNDRV_PCM_STATE_SETUP);
1200 goto _end;
1201 case SNDRV_PCM_STATE_OPEN:
1202 result = -EBADFD;
1203 goto _end;
1204 case SNDRV_PCM_STATE_PREPARED:
1205 if (!snd_pcm_playback_empty(substream)) {
1206 err = snd_pcm_start(substream);
1207 if (err < 0) {
1208 result = err;
1209 goto _end;
1211 break;
1213 /* Fall through */
1214 case SNDRV_PCM_STATE_XRUN:
1215 snd_pcm_change_state(substream, SNDRV_PCM_STATE_SETUP);
1216 /* Fall through */
1217 case SNDRV_PCM_STATE_SETUP:
1218 goto _end;
1219 default:
1220 break;
1223 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1224 if (snd_pcm_playback_empty(substream)) {
1225 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1226 goto _end;
1228 snd_pcm_change_state(substream, SNDRV_PCM_STATE_DRAINING);
1231 if (substream->ffile->f_flags & O_NONBLOCK) {
1232 result = -EAGAIN;
1233 goto _end;
1236 init_waitqueue_entry(&wait, current);
1237 add_wait_queue(&runtime->sleep, &wait);
1238 while (1) {
1239 long tout;
1240 if (signal_pending(current)) {
1241 state = SIGNALED;
1242 break;
1244 set_current_state(TASK_INTERRUPTIBLE);
1245 snd_pcm_stream_unlock_irq(substream);
1246 tout = schedule_timeout(10 * HZ);
1247 snd_pcm_stream_lock_irq(substream);
1248 if (tout == 0) {
1249 state = runtime->status->state == SNDRV_PCM_STATE_SUSPENDED ? SUSPENDED : EXPIRED;
1250 break;
1252 if (runtime->status->state != SNDRV_PCM_STATE_DRAINING) {
1253 state = READY;
1254 break;
1257 remove_wait_queue(&runtime->sleep, &wait);
1259 switch (state) {
1260 case SIGNALED:
1261 result = -ERESTARTSYS;
1262 goto _end;
1263 case SUSPENDED:
1264 result = -ESTRPIPE;
1265 goto _end;
1266 case EXPIRED:
1267 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1268 result = -EIO;
1269 goto _end;
1270 default:
1271 break;
1274 _end:
1275 runtime->stop_threshold = stop_threshold;
1276 snd_pcm_stream_unlock_irq(substream);
1277 snd_power_unlock(card);
1278 if (state == EXPIRED)
1279 snd_pcm_playback_drop(substream);
1281 return result;
1284 static int snd_pcm_playback_drop(snd_pcm_substream_t *substream)
1286 snd_pcm_runtime_t *runtime = substream->runtime;
1287 snd_card_t *card = substream->pcm->card;
1288 int res = 0;
1290 snd_power_lock(card);
1291 snd_pcm_stream_lock_irq(substream);
1292 switch (runtime->status->state) {
1293 case SNDRV_PCM_STATE_OPEN:
1294 res = -EBADFD;
1295 break;
1296 case SNDRV_PCM_STATE_SETUP:
1297 break;
1298 case SNDRV_PCM_STATE_PAUSED:
1299 snd_pcm_pause(substream, 0);
1300 /* Fall through */
1301 case SNDRV_PCM_STATE_RUNNING:
1302 case SNDRV_PCM_STATE_DRAINING:
1303 if (snd_pcm_update_hw_ptr(substream) >= 0) {
1304 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1305 break;
1307 /* Fall through */
1308 case SNDRV_PCM_STATE_PREPARED:
1309 case SNDRV_PCM_STATE_XRUN:
1310 snd_pcm_change_state(substream, SNDRV_PCM_STATE_SETUP);
1311 break;
1312 case SNDRV_PCM_STATE_SUSPENDED:
1313 snd_pcm_stream_unlock_irq(substream);
1314 res = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile);
1315 snd_pcm_stream_lock_irq(substream);
1316 if (res >= 0)
1317 snd_pcm_change_state(substream, SNDRV_PCM_STATE_SETUP);
1318 break;
1319 default:
1320 break;
1322 runtime->control->appl_ptr = runtime->status->hw_ptr;
1323 snd_pcm_stream_unlock_irq(substream);
1324 snd_power_unlock(card);
1325 return res;
1328 static int snd_pcm_capture_drain(snd_pcm_substream_t * substream)
1330 snd_pcm_runtime_t *runtime = substream->runtime;
1331 snd_card_t *card = substream->pcm->card;
1332 int res = 0;
1334 snd_power_lock(card);
1335 snd_pcm_stream_lock_irq(substream);
1336 switch (runtime->status->state) {
1337 case SNDRV_PCM_STATE_OPEN:
1338 res = -EBADFD;
1339 break;
1340 case SNDRV_PCM_STATE_PREPARED:
1341 snd_pcm_change_state(substream, SNDRV_PCM_STATE_SETUP);
1342 break;
1343 case SNDRV_PCM_STATE_SETUP:
1344 case SNDRV_PCM_STATE_DRAINING:
1345 break;
1346 case SNDRV_PCM_STATE_PAUSED:
1347 snd_pcm_pause(substream, 0);
1348 /* Fall through */
1349 case SNDRV_PCM_STATE_RUNNING:
1350 if (snd_pcm_update_hw_ptr(substream) >= 0) {
1351 snd_pcm_stop(substream,
1352 snd_pcm_capture_avail(runtime) > 0 ?
1353 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP);
1354 break;
1356 /* Fall through */
1357 case SNDRV_PCM_STATE_XRUN:
1358 _xrun_recovery:
1359 snd_pcm_change_state(substream,
1360 snd_pcm_capture_avail(runtime) > 0 ?
1361 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP);
1362 break;
1363 case SNDRV_PCM_STATE_SUSPENDED:
1364 snd_pcm_stream_unlock_irq(substream);
1365 res = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile);
1366 snd_pcm_stream_lock_irq(substream);
1367 if (res >= 0)
1368 goto _xrun_recovery;
1369 break;
1370 default:
1371 break;
1373 snd_pcm_stream_unlock_irq(substream);
1374 snd_power_unlock(card);
1375 return res;
1378 static int snd_pcm_capture_drop(snd_pcm_substream_t * substream)
1380 snd_pcm_runtime_t *runtime = substream->runtime;
1381 snd_card_t *card = substream->pcm->card;
1382 int res = 0;
1384 snd_power_lock(card);
1385 snd_pcm_stream_lock_irq(substream);
1386 switch (runtime->status->state) {
1387 case SNDRV_PCM_STATE_OPEN:
1388 res = -EBADFD;
1389 break;
1390 case SNDRV_PCM_STATE_SETUP:
1391 break;
1392 case SNDRV_PCM_STATE_PAUSED:
1393 snd_pcm_pause(substream, 0);
1394 /* Fall through */
1395 case SNDRV_PCM_STATE_RUNNING:
1396 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1397 break;
1398 case SNDRV_PCM_STATE_SUSPENDED:
1399 snd_pcm_stream_unlock_irq(substream);
1400 res = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile);
1401 snd_pcm_stream_lock_irq(substream);
1402 if (res < 0)
1403 goto _end;
1404 /* Fall through */
1405 case SNDRV_PCM_STATE_PREPARED:
1406 case SNDRV_PCM_STATE_DRAINING:
1407 case SNDRV_PCM_STATE_XRUN:
1408 snd_pcm_change_state(substream, SNDRV_PCM_STATE_SETUP);
1409 break;
1410 default:
1411 break;
1413 runtime->control->appl_ptr = runtime->status->hw_ptr;
1414 _end:
1415 snd_pcm_stream_unlock_irq(substream);
1416 snd_power_unlock(card);
1417 return res;
1420 /* WARNING: Don't forget to fput back the file */
1421 extern int snd_major;
1422 static struct file *snd_pcm_file_fd(int fd)
1424 struct file *file;
1425 struct inode *inode;
1426 unsigned short minor;
1427 file = fget(fd);
1428 if (!file)
1429 return 0;
1430 inode = file->f_dentry->d_inode;
1431 if (!S_ISCHR(inode->i_mode) ||
1432 imajor(inode) != snd_major) {
1433 fput(file);
1434 return 0;
1436 minor = iminor(inode);
1437 if (minor >= 256 ||
1438 minor % SNDRV_MINOR_DEVICES < SNDRV_MINOR_PCM_PLAYBACK) {
1439 fput(file);
1440 return 0;
1442 return file;
1445 static int snd_pcm_link(snd_pcm_substream_t *substream, int fd)
1447 int res = 0;
1448 struct file *file;
1449 snd_pcm_file_t *pcm_file;
1450 snd_pcm_substream_t *substream1;
1452 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN)
1453 return -EBADFD;
1454 file = snd_pcm_file_fd(fd);
1455 if (!file)
1456 return -EBADFD;
1457 pcm_file = snd_magic_cast(snd_pcm_file_t, file->private_data, return -ENXIO);
1458 substream1 = pcm_file->substream;
1459 write_lock_irq(&snd_pcm_link_rwlock);
1460 if (substream->runtime->status->state != substream1->runtime->status->state) {
1461 res = -EBADFD;
1462 goto _end;
1464 if (snd_pcm_stream_linked(substream1)) {
1465 res = -EALREADY;
1466 goto _end;
1468 if (!snd_pcm_stream_linked(substream)) {
1469 substream->group = kmalloc(sizeof(snd_pcm_group_t), GFP_ATOMIC);
1470 if (substream->group == NULL) {
1471 res = -ENOMEM;
1472 goto _end;
1474 spin_lock_init(&substream->group->lock);
1475 INIT_LIST_HEAD(&substream->group->substreams);
1476 list_add_tail(&substream->link_list, &substream->group->substreams);
1478 list_add_tail(&substream1->link_list, &substream->group->substreams);
1479 substream1->group = substream->group;
1480 _end:
1481 write_unlock_irq(&snd_pcm_link_rwlock);
1482 fput(file);
1483 return res;
1486 static void relink_to_local(snd_pcm_substream_t *substream)
1488 substream->group = &substream->self_group;
1489 INIT_LIST_HEAD(&substream->self_group.substreams);
1490 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1493 static int snd_pcm_unlink(snd_pcm_substream_t *substream)
1495 struct list_head *pos;
1496 int res = 0, count = 0;
1498 write_lock_irq(&snd_pcm_link_rwlock);
1499 if (!snd_pcm_stream_linked(substream)) {
1500 res = -EINVAL;
1501 goto _end;
1503 list_del(&substream->link_list);
1504 snd_pcm_group_for_each(pos, substream) {
1505 if (++count > 1)
1506 break;
1508 if (count == 1) { /* detach the last stream, too */
1509 snd_pcm_group_for_each(pos, substream) {
1510 relink_to_local(snd_pcm_group_substream_entry(pos));
1511 break;
1513 kfree(substream->group);
1515 relink_to_local(substream);
1516 _end:
1517 write_unlock_irq(&snd_pcm_link_rwlock);
1518 return res;
1521 static int snd_pcm_hw_rule_mul(snd_pcm_hw_params_t *params,
1522 snd_pcm_hw_rule_t *rule)
1524 snd_interval_t t;
1525 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1526 hw_param_interval_c(params, rule->deps[1]), &t);
1527 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1530 static int snd_pcm_hw_rule_div(snd_pcm_hw_params_t *params,
1531 snd_pcm_hw_rule_t *rule)
1533 snd_interval_t t;
1534 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1535 hw_param_interval_c(params, rule->deps[1]), &t);
1536 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1539 static int snd_pcm_hw_rule_muldivk(snd_pcm_hw_params_t *params,
1540 snd_pcm_hw_rule_t *rule)
1542 snd_interval_t t;
1543 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1544 hw_param_interval_c(params, rule->deps[1]),
1545 (unsigned long) rule->private, &t);
1546 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1549 static int snd_pcm_hw_rule_mulkdiv(snd_pcm_hw_params_t *params,
1550 snd_pcm_hw_rule_t *rule)
1552 snd_interval_t t;
1553 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1554 (unsigned long) rule->private,
1555 hw_param_interval_c(params, rule->deps[1]), &t);
1556 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1559 static int snd_pcm_hw_rule_format(snd_pcm_hw_params_t *params,
1560 snd_pcm_hw_rule_t *rule)
1562 unsigned int k;
1563 snd_interval_t *i = hw_param_interval(params, rule->deps[0]);
1564 snd_mask_t m;
1565 snd_mask_t *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1566 snd_mask_any(&m);
1567 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1568 int bits;
1569 if (! snd_mask_test(mask, k))
1570 continue;
1571 bits = snd_pcm_format_physical_width(k);
1572 if (bits <= 0)
1573 continue; /* ignore invalid formats */
1574 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1575 snd_mask_reset(&m, k);
1577 return snd_mask_refine(mask, &m);
1580 static int snd_pcm_hw_rule_sample_bits(snd_pcm_hw_params_t *params,
1581 snd_pcm_hw_rule_t *rule)
1583 snd_interval_t t;
1584 unsigned int k;
1585 t.min = UINT_MAX;
1586 t.max = 0;
1587 t.openmin = 0;
1588 t.openmax = 0;
1589 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1590 int bits;
1591 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1592 continue;
1593 bits = snd_pcm_format_physical_width(k);
1594 if (bits <= 0)
1595 continue; /* ignore invalid formats */
1596 if (t.min > (unsigned)bits)
1597 t.min = bits;
1598 if (t.max < (unsigned)bits)
1599 t.max = bits;
1601 t.integer = 1;
1602 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1605 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1606 #error "Change this table"
1607 #endif
1609 static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1610 48000, 64000, 88200, 96000, 176400, 192000 };
1612 #define RATES (sizeof(rates) / sizeof(rates[0]))
1614 static int snd_pcm_hw_rule_rate(snd_pcm_hw_params_t *params,
1615 snd_pcm_hw_rule_t *rule)
1617 snd_pcm_hardware_t *hw = rule->private;
1618 return snd_interval_list(hw_param_interval(params, rule->var), RATES, rates, hw->rates);
1621 static int snd_pcm_hw_rule_buffer_bytes_max(snd_pcm_hw_params_t *params,
1622 snd_pcm_hw_rule_t *rule)
1624 snd_interval_t t;
1625 snd_pcm_substream_t *substream = rule->private;
1626 t.min = 0;
1627 t.max = substream->buffer_bytes_max;
1628 t.openmin = 0;
1629 t.openmax = 0;
1630 t.integer = 1;
1631 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1634 int snd_pcm_hw_constraints_init(snd_pcm_substream_t *substream)
1636 snd_pcm_runtime_t *runtime = substream->runtime;
1637 snd_pcm_hw_constraints_t *constrs = &runtime->hw_constraints;
1638 int k, err;
1640 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
1641 snd_mask_any(constrs_mask(constrs, k));
1644 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
1645 snd_interval_any(constrs_interval(constrs, k));
1648 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
1649 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
1650 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
1651 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
1652 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
1654 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1655 snd_pcm_hw_rule_format, 0,
1656 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1657 if (err < 0)
1658 return err;
1659 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1660 snd_pcm_hw_rule_sample_bits, 0,
1661 SNDRV_PCM_HW_PARAM_FORMAT,
1662 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1663 if (err < 0)
1664 return err;
1665 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1666 snd_pcm_hw_rule_div, 0,
1667 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1668 if (err < 0)
1669 return err;
1670 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1671 snd_pcm_hw_rule_mul, 0,
1672 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1673 if (err < 0)
1674 return err;
1675 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1676 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1677 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1678 if (err < 0)
1679 return err;
1680 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1681 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1682 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
1683 if (err < 0)
1684 return err;
1685 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1686 snd_pcm_hw_rule_div, 0,
1687 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1688 if (err < 0)
1689 return err;
1690 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1691 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1692 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
1693 if (err < 0)
1694 return err;
1695 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1696 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1697 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
1698 if (err < 0)
1699 return err;
1700 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
1701 snd_pcm_hw_rule_div, 0,
1702 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1703 if (err < 0)
1704 return err;
1705 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1706 snd_pcm_hw_rule_div, 0,
1707 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1708 if (err < 0)
1709 return err;
1710 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1711 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1712 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1713 if (err < 0)
1714 return err;
1715 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1716 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1717 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1718 if (err < 0)
1719 return err;
1720 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1721 snd_pcm_hw_rule_mul, 0,
1722 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1723 if (err < 0)
1724 return err;
1725 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1726 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1727 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1728 if (err < 0)
1729 return err;
1730 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1731 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1732 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1733 if (err < 0)
1734 return err;
1735 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1736 snd_pcm_hw_rule_muldivk, (void*) 8,
1737 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1738 if (err < 0)
1739 return err;
1740 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1741 snd_pcm_hw_rule_muldivk, (void*) 8,
1742 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1743 if (err < 0)
1744 return err;
1745 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1746 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1747 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1748 if (err < 0)
1749 return err;
1750 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
1751 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1752 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1753 if (err < 0)
1754 return err;
1755 return 0;
1758 int snd_pcm_hw_constraints_complete(snd_pcm_substream_t *substream)
1760 snd_pcm_runtime_t *runtime = substream->runtime;
1761 snd_pcm_hardware_t *hw = &runtime->hw;
1762 int err;
1763 unsigned int mask = 0;
1765 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1766 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
1767 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1768 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
1769 if (hw->info & SNDRV_PCM_INFO_MMAP) {
1770 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1771 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
1772 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1773 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
1774 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
1775 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
1777 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
1778 snd_assert(err >= 0, return -EINVAL);
1780 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
1781 snd_assert(err >= 0, return -EINVAL);
1783 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
1784 snd_assert(err >= 0, return -EINVAL);
1786 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
1787 hw->channels_min, hw->channels_max);
1788 snd_assert(err >= 0, return -EINVAL);
1790 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
1791 hw->rate_min, hw->rate_max);
1792 snd_assert(err >= 0, return -EINVAL);
1794 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1795 hw->period_bytes_min, hw->period_bytes_max);
1796 snd_assert(err >= 0, return -EINVAL);
1798 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
1799 hw->periods_min, hw->periods_max);
1800 snd_assert(err >= 0, return -EINVAL);
1802 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1803 hw->period_bytes_min, hw->buffer_bytes_max);
1804 snd_assert(err >= 0, return -EINVAL);
1806 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1807 snd_pcm_hw_rule_buffer_bytes_max, substream,
1808 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
1809 if (err < 0)
1810 return err;
1812 /* FIXME: remove */
1813 if (runtime->dma_bytes) {
1814 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
1815 snd_assert(err >= 0, return -EINVAL);
1818 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
1819 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1820 snd_pcm_hw_rule_rate, hw,
1821 SNDRV_PCM_HW_PARAM_RATE, -1);
1822 if (err < 0)
1823 return err;
1826 /* FIXME: this belong to lowlevel */
1827 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_TICK_TIME,
1828 1000000 / HZ, 1000000 / HZ);
1829 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
1831 return 0;
1834 static void snd_pcm_add_file(snd_pcm_str_t *str,
1835 snd_pcm_file_t *pcm_file)
1837 pcm_file->next = str->files;
1838 str->files = pcm_file;
1841 static void snd_pcm_remove_file(snd_pcm_str_t *str,
1842 snd_pcm_file_t *pcm_file)
1844 snd_pcm_file_t * pcm_file1;
1845 if (str->files == pcm_file) {
1846 str->files = pcm_file->next;
1847 } else {
1848 pcm_file1 = str->files;
1849 while (pcm_file1 && pcm_file1->next != pcm_file)
1850 pcm_file1 = pcm_file1->next;
1851 if (pcm_file1 != NULL)
1852 pcm_file1->next = pcm_file->next;
1856 static int snd_pcm_release_file(snd_pcm_file_t * pcm_file)
1858 snd_pcm_substream_t *substream;
1859 snd_pcm_runtime_t *runtime;
1860 snd_pcm_str_t * str;
1862 snd_assert(pcm_file != NULL, return -ENXIO);
1863 substream = pcm_file->substream;
1864 snd_assert(substream != NULL, return -ENXIO);
1865 runtime = substream->runtime;
1866 str = substream->pstr;
1867 snd_pcm_unlink(substream);
1868 if (substream->open_flag) {
1869 if (substream->ops->hw_free != NULL)
1870 substream->ops->hw_free(substream);
1871 substream->ops->close(substream);
1872 substream->open_flag = 0;
1874 substream->ffile = NULL;
1875 snd_pcm_remove_file(str, pcm_file);
1876 snd_pcm_release_substream(substream);
1877 snd_magic_kfree(pcm_file);
1878 return 0;
1881 static int snd_pcm_open_file(struct file *file,
1882 snd_pcm_t *pcm,
1883 int stream,
1884 snd_pcm_file_t **rpcm_file)
1886 int err = 0;
1887 snd_pcm_file_t *pcm_file;
1888 snd_pcm_substream_t *substream;
1889 snd_pcm_str_t *str;
1891 snd_assert(rpcm_file != NULL, return -EINVAL);
1892 *rpcm_file = NULL;
1894 pcm_file = snd_magic_kcalloc(snd_pcm_file_t, 0, GFP_KERNEL);
1895 if (pcm_file == NULL) {
1896 return -ENOMEM;
1899 if ((err = snd_pcm_open_substream(pcm, stream, &substream)) < 0) {
1900 snd_magic_kfree(pcm_file);
1901 return err;
1904 str = substream->pstr;
1905 substream->file = pcm_file;
1907 pcm_file->substream = substream;
1909 snd_pcm_add_file(str, pcm_file);
1911 err = snd_pcm_hw_constraints_init(substream);
1912 if (err < 0) {
1913 snd_printd("snd_pcm_hw_constraints_init failed\n");
1914 snd_pcm_release_file(pcm_file);
1915 return err;
1918 if ((err = substream->ops->open(substream)) < 0) {
1919 snd_pcm_release_file(pcm_file);
1920 return err;
1922 substream->open_flag = 1;
1924 err = snd_pcm_hw_constraints_complete(substream);
1925 if (err < 0) {
1926 snd_printd("snd_pcm_hw_constraints_complete failed\n");
1927 substream->ops->close(substream);
1928 snd_pcm_release_file(pcm_file);
1929 return err;
1932 substream->ffile = file;
1934 file->private_data = pcm_file;
1935 *rpcm_file = pcm_file;
1936 return 0;
1939 int snd_pcm_open(struct inode *inode, struct file *file)
1941 int cardnum = SNDRV_MINOR_CARD(iminor(inode));
1942 int device = SNDRV_MINOR_DEVICE(iminor(inode));
1943 int err;
1944 snd_pcm_t *pcm;
1945 snd_pcm_file_t *pcm_file;
1946 wait_queue_t wait;
1948 snd_runtime_check(device >= SNDRV_MINOR_PCM_PLAYBACK && device < SNDRV_MINOR_DEVICES, return -ENXIO);
1949 pcm = snd_pcm_devices[(cardnum * SNDRV_PCM_DEVICES) + (device % SNDRV_MINOR_PCMS)];
1950 if (pcm == NULL) {
1951 err = -ENODEV;
1952 goto __error1;
1954 err = snd_card_file_add(pcm->card, file);
1955 if (err < 0)
1956 goto __error1;
1957 if (!try_module_get(pcm->card->module)) {
1958 err = -EFAULT;
1959 goto __error2;
1961 init_waitqueue_entry(&wait, current);
1962 add_wait_queue(&pcm->open_wait, &wait);
1963 down(&pcm->open_mutex);
1964 while (1) {
1965 err = snd_pcm_open_file(file, pcm, device >= SNDRV_MINOR_PCM_CAPTURE ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK, &pcm_file);
1966 if (err >= 0)
1967 break;
1968 if (err == -EAGAIN) {
1969 if (file->f_flags & O_NONBLOCK) {
1970 err = -EBUSY;
1971 break;
1973 } else
1974 break;
1975 set_current_state(TASK_INTERRUPTIBLE);
1976 up(&pcm->open_mutex);
1977 schedule();
1978 down(&pcm->open_mutex);
1979 if (signal_pending(current)) {
1980 err = -ERESTARTSYS;
1981 break;
1984 remove_wait_queue(&pcm->open_wait, &wait);
1985 up(&pcm->open_mutex);
1986 if (err < 0)
1987 goto __error;
1988 return err;
1990 __error:
1991 module_put(pcm->card->module);
1992 __error2:
1993 snd_card_file_remove(pcm->card, file);
1994 __error1:
1995 return err;
1998 int snd_pcm_release(struct inode *inode, struct file *file)
2000 snd_pcm_t *pcm;
2001 snd_pcm_substream_t *substream;
2002 snd_pcm_file_t *pcm_file;
2004 pcm_file = snd_magic_cast(snd_pcm_file_t, file->private_data, return -ENXIO);
2005 substream = pcm_file->substream;
2006 snd_assert(substream != NULL, return -ENXIO);
2007 snd_assert(!atomic_read(&substream->runtime->mmap_count), );
2008 pcm = substream->pcm;
2009 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2010 snd_pcm_playback_drop(substream);
2011 else
2012 snd_pcm_capture_drop(substream);
2013 fasync_helper(-1, file, 0, &substream->runtime->fasync);
2014 down(&pcm->open_mutex);
2015 snd_pcm_release_file(pcm_file);
2016 up(&pcm->open_mutex);
2017 wake_up(&pcm->open_wait);
2018 module_put(pcm->card->module);
2019 snd_card_file_remove(pcm->card, file);
2020 return 0;
2023 snd_pcm_sframes_t snd_pcm_playback_rewind(snd_pcm_substream_t *substream, snd_pcm_uframes_t frames)
2025 snd_pcm_runtime_t *runtime = substream->runtime;
2026 snd_pcm_sframes_t appl_ptr;
2027 snd_pcm_sframes_t ret;
2028 snd_pcm_sframes_t hw_avail;
2030 if (frames == 0)
2031 return 0;
2033 snd_pcm_stream_lock_irq(substream);
2034 switch (runtime->status->state) {
2035 case SNDRV_PCM_STATE_PREPARED:
2036 break;
2037 case SNDRV_PCM_STATE_DRAINING:
2038 case SNDRV_PCM_STATE_RUNNING:
2039 if (snd_pcm_update_hw_ptr(substream) >= 0)
2040 break;
2041 /* Fall through */
2042 case SNDRV_PCM_STATE_XRUN:
2043 ret = -EPIPE;
2044 goto __end;
2045 default:
2046 ret = -EBADFD;
2047 goto __end;
2050 hw_avail = snd_pcm_playback_hw_avail(runtime);
2051 if (hw_avail <= 0) {
2052 ret = 0;
2053 goto __end;
2055 if (frames > (snd_pcm_uframes_t)hw_avail)
2056 frames = hw_avail;
2057 else
2058 frames -= frames % runtime->xfer_align;
2059 appl_ptr = runtime->control->appl_ptr - frames;
2060 if (appl_ptr < 0)
2061 appl_ptr += runtime->boundary;
2062 runtime->control->appl_ptr = appl_ptr;
2063 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2064 runtime->sleep_min)
2065 snd_pcm_tick_prepare(substream);
2066 ret = frames;
2067 __end:
2068 snd_pcm_stream_unlock_irq(substream);
2069 return ret;
2072 snd_pcm_sframes_t snd_pcm_capture_rewind(snd_pcm_substream_t *substream, snd_pcm_uframes_t frames)
2074 snd_pcm_runtime_t *runtime = substream->runtime;
2075 snd_pcm_sframes_t appl_ptr;
2076 snd_pcm_sframes_t ret;
2077 snd_pcm_sframes_t hw_avail;
2079 if (frames == 0)
2080 return 0;
2082 snd_pcm_stream_lock_irq(substream);
2083 switch (runtime->status->state) {
2084 case SNDRV_PCM_STATE_PREPARED:
2085 case SNDRV_PCM_STATE_DRAINING:
2086 break;
2087 case SNDRV_PCM_STATE_RUNNING:
2088 if (snd_pcm_update_hw_ptr(substream) >= 0)
2089 break;
2090 /* Fall through */
2091 case SNDRV_PCM_STATE_XRUN:
2092 ret = -EPIPE;
2093 goto __end;
2094 default:
2095 ret = -EBADFD;
2096 goto __end;
2099 hw_avail = snd_pcm_capture_hw_avail(runtime);
2100 if (hw_avail <= 0) {
2101 ret = 0;
2102 goto __end;
2104 if (frames > (snd_pcm_uframes_t)hw_avail)
2105 frames = hw_avail;
2106 else
2107 frames -= frames % runtime->xfer_align;
2108 appl_ptr = runtime->control->appl_ptr - frames;
2109 if (appl_ptr < 0)
2110 appl_ptr += runtime->boundary;
2111 runtime->control->appl_ptr = appl_ptr;
2112 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2113 runtime->sleep_min)
2114 snd_pcm_tick_prepare(substream);
2115 ret = frames;
2116 __end:
2117 snd_pcm_stream_unlock_irq(substream);
2118 return ret;
2121 snd_pcm_sframes_t snd_pcm_playback_forward(snd_pcm_substream_t *substream, snd_pcm_uframes_t frames)
2123 snd_pcm_runtime_t *runtime = substream->runtime;
2124 snd_pcm_sframes_t appl_ptr;
2125 snd_pcm_sframes_t ret;
2126 snd_pcm_sframes_t avail;
2128 if (frames == 0)
2129 return 0;
2131 snd_pcm_stream_lock_irq(substream);
2132 switch (runtime->status->state) {
2133 case SNDRV_PCM_STATE_PREPARED:
2134 case SNDRV_PCM_STATE_PAUSED:
2135 break;
2136 case SNDRV_PCM_STATE_DRAINING:
2137 case SNDRV_PCM_STATE_RUNNING:
2138 if (snd_pcm_update_hw_ptr(substream) >= 0)
2139 break;
2140 /* Fall through */
2141 case SNDRV_PCM_STATE_XRUN:
2142 ret = -EPIPE;
2143 goto __end;
2144 default:
2145 ret = -EBADFD;
2146 goto __end;
2149 avail = snd_pcm_playback_avail(runtime);
2150 if (avail <= 0) {
2151 ret = 0;
2152 goto __end;
2154 if (frames > (snd_pcm_uframes_t)avail)
2155 frames = avail;
2156 else
2157 frames -= frames % runtime->xfer_align;
2158 appl_ptr = runtime->control->appl_ptr + frames;
2159 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2160 appl_ptr -= runtime->boundary;
2161 runtime->control->appl_ptr = appl_ptr;
2162 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2163 runtime->sleep_min)
2164 snd_pcm_tick_prepare(substream);
2165 ret = frames;
2166 __end:
2167 snd_pcm_stream_unlock_irq(substream);
2168 return ret;
2171 snd_pcm_sframes_t snd_pcm_capture_forward(snd_pcm_substream_t *substream, snd_pcm_uframes_t frames)
2173 snd_pcm_runtime_t *runtime = substream->runtime;
2174 snd_pcm_sframes_t appl_ptr;
2175 snd_pcm_sframes_t ret;
2176 snd_pcm_sframes_t avail;
2178 if (frames == 0)
2179 return 0;
2181 snd_pcm_stream_lock_irq(substream);
2182 switch (runtime->status->state) {
2183 case SNDRV_PCM_STATE_PREPARED:
2184 case SNDRV_PCM_STATE_DRAINING:
2185 case SNDRV_PCM_STATE_PAUSED:
2186 break;
2187 case SNDRV_PCM_STATE_RUNNING:
2188 if (snd_pcm_update_hw_ptr(substream) >= 0)
2189 break;
2190 /* Fall through */
2191 case SNDRV_PCM_STATE_XRUN:
2192 ret = -EPIPE;
2193 goto __end;
2194 default:
2195 ret = -EBADFD;
2196 goto __end;
2199 avail = snd_pcm_capture_avail(runtime);
2200 if (avail <= 0) {
2201 ret = 0;
2202 goto __end;
2204 if (frames > (snd_pcm_uframes_t)avail)
2205 frames = avail;
2206 else
2207 frames -= frames % runtime->xfer_align;
2208 appl_ptr = runtime->control->appl_ptr + frames;
2209 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2210 appl_ptr -= runtime->boundary;
2211 runtime->control->appl_ptr = appl_ptr;
2212 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2213 runtime->sleep_min)
2214 snd_pcm_tick_prepare(substream);
2215 ret = frames;
2216 __end:
2217 snd_pcm_stream_unlock_irq(substream);
2218 return ret;
2221 static int snd_pcm_hwsync(snd_pcm_substream_t *substream)
2223 snd_pcm_runtime_t *runtime = substream->runtime;
2224 int err;
2226 snd_pcm_stream_lock_irq(substream);
2227 switch (runtime->status->state) {
2228 case SNDRV_PCM_STATE_DRAINING:
2229 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2230 goto __badfd;
2231 case SNDRV_PCM_STATE_RUNNING:
2232 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2233 break;
2234 /* Fall through */
2235 case SNDRV_PCM_STATE_PREPARED:
2236 case SNDRV_PCM_STATE_SUSPENDED:
2237 err = 0;
2238 break;
2239 case SNDRV_PCM_STATE_XRUN:
2240 err = -EPIPE;
2241 break;
2242 default:
2243 __badfd:
2244 err = -EBADFD;
2245 break;
2247 snd_pcm_stream_unlock_irq(substream);
2248 return err;
2251 static int snd_pcm_delay(snd_pcm_substream_t *substream, snd_pcm_sframes_t *res)
2253 snd_pcm_runtime_t *runtime = substream->runtime;
2254 int err;
2255 snd_pcm_sframes_t n = 0;
2257 snd_pcm_stream_lock_irq(substream);
2258 switch (runtime->status->state) {
2259 case SNDRV_PCM_STATE_DRAINING:
2260 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2261 goto __badfd;
2262 case SNDRV_PCM_STATE_RUNNING:
2263 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2264 break;
2265 /* Fall through */
2266 case SNDRV_PCM_STATE_PREPARED:
2267 case SNDRV_PCM_STATE_SUSPENDED:
2268 err = 0;
2269 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2270 n = snd_pcm_playback_hw_avail(runtime);
2271 else
2272 n = snd_pcm_capture_avail(runtime);
2273 break;
2274 case SNDRV_PCM_STATE_XRUN:
2275 err = -EPIPE;
2276 break;
2277 default:
2278 __badfd:
2279 err = -EBADFD;
2280 break;
2282 snd_pcm_stream_unlock_irq(substream);
2283 if (!err)
2284 if (put_user(n, res))
2285 err = -EFAULT;
2286 return err;
2289 static int snd_pcm_playback_ioctl1(snd_pcm_substream_t *substream,
2290 unsigned int cmd, void *arg);
2291 static int snd_pcm_capture_ioctl1(snd_pcm_substream_t *substream,
2292 unsigned int cmd, void *arg);
2294 static int snd_pcm_common_ioctl1(snd_pcm_substream_t *substream,
2295 unsigned int cmd, void *arg)
2297 snd_assert(substream != NULL, return -ENXIO);
2299 switch (cmd) {
2300 case SNDRV_PCM_IOCTL_PVERSION:
2301 return put_user(SNDRV_PCM_VERSION, (int *)arg) ? -EFAULT : 0;
2302 case SNDRV_PCM_IOCTL_INFO:
2303 return snd_pcm_info_user(substream, (snd_pcm_info_t *) arg);
2304 case SNDRV_PCM_IOCTL_TSTAMP:
2306 int xarg;
2307 if (get_user(xarg, (int *) arg))
2308 return -EFAULT;
2309 substream->runtime->tstamp_timespec = xarg ? 1 : 0;
2310 return 0;
2312 case SNDRV_PCM_IOCTL_HW_REFINE:
2313 return snd_pcm_hw_refine_user(substream, (snd_pcm_hw_params_t *) arg);
2314 case SNDRV_PCM_IOCTL_HW_PARAMS:
2315 return snd_pcm_hw_params_user(substream, (snd_pcm_hw_params_t *) arg);
2316 case SNDRV_PCM_IOCTL_HW_FREE:
2317 return snd_pcm_hw_free(substream);
2318 case SNDRV_PCM_IOCTL_SW_PARAMS:
2319 return snd_pcm_sw_params_user(substream, (snd_pcm_sw_params_t *) arg);
2320 case SNDRV_PCM_IOCTL_STATUS:
2321 return snd_pcm_status_user(substream, (snd_pcm_status_t *) arg);
2322 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2323 return snd_pcm_channel_info(substream, (snd_pcm_channel_info_t *) arg);
2324 case SNDRV_PCM_IOCTL_PREPARE:
2325 return snd_pcm_prepare(substream);
2326 case SNDRV_PCM_IOCTL_RESET:
2327 return snd_pcm_reset(substream);
2328 case SNDRV_PCM_IOCTL_START:
2329 return snd_pcm_action(&snd_pcm_action_start, substream, 0);
2330 case SNDRV_PCM_IOCTL_LINK:
2331 return snd_pcm_link(substream, (long) arg);
2332 case SNDRV_PCM_IOCTL_UNLINK:
2333 return snd_pcm_unlink(substream);
2334 case SNDRV_PCM_IOCTL_RESUME:
2335 return snd_pcm_resume(substream);
2336 case SNDRV_PCM_IOCTL_XRUN:
2337 return snd_pcm_xrun(substream);
2338 case SNDRV_PCM_IOCTL_HWSYNC:
2339 return snd_pcm_hwsync(substream);
2340 case SNDRV_PCM_IOCTL_DELAY:
2341 return snd_pcm_delay(substream, (snd_pcm_sframes_t *) arg);
2342 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2343 return snd_pcm_hw_refine_old_user(substream, (struct sndrv_pcm_hw_params_old *) arg);
2344 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2345 return snd_pcm_hw_params_old_user(substream, (struct sndrv_pcm_hw_params_old *) arg);
2347 snd_printd("unknown ioctl = 0x%x\n", cmd);
2348 return -ENOTTY;
2351 static int snd_pcm_playback_ioctl1(snd_pcm_substream_t *substream,
2352 unsigned int cmd, void *arg)
2354 snd_assert(substream != NULL, return -ENXIO);
2355 snd_assert(substream->stream == SNDRV_PCM_STREAM_PLAYBACK, return -EINVAL);
2356 switch (cmd) {
2357 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2359 snd_xferi_t xferi, *_xferi = arg;
2360 snd_pcm_runtime_t *runtime = substream->runtime;
2361 snd_pcm_sframes_t result;
2362 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2363 return -EBADFD;
2364 if (put_user(0, &_xferi->result))
2365 return -EFAULT;
2366 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2367 return -EFAULT;
2368 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2369 __put_user(result, &_xferi->result);
2370 return result < 0 ? result : 0;
2372 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2374 snd_xfern_t xfern, *_xfern = arg;
2375 snd_pcm_runtime_t *runtime = substream->runtime;
2376 void *bufs;
2377 snd_pcm_sframes_t result;
2378 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2379 return -EBADFD;
2380 if (runtime->channels > 128)
2381 return -EINVAL;
2382 if (put_user(0, &_xfern->result))
2383 return -EFAULT;
2384 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2385 return -EFAULT;
2386 bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL);
2387 if (bufs == NULL)
2388 return -ENOMEM;
2389 if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) {
2390 kfree(bufs);
2391 return -EFAULT;
2393 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2394 kfree(bufs);
2395 __put_user(result, &_xfern->result);
2396 return result < 0 ? result : 0;
2398 case SNDRV_PCM_IOCTL_REWIND:
2400 snd_pcm_uframes_t frames, *_frames = arg;
2401 snd_pcm_sframes_t result;
2402 if (get_user(frames, _frames))
2403 return -EFAULT;
2404 if (put_user(0, _frames))
2405 return -EFAULT;
2406 result = snd_pcm_playback_rewind(substream, frames);
2407 __put_user(result, _frames);
2408 return result < 0 ? result : 0;
2410 case SNDRV_PCM_IOCTL_FORWARD:
2412 snd_pcm_uframes_t frames, *_frames = arg;
2413 snd_pcm_sframes_t result;
2414 if (get_user(frames, _frames))
2415 return -EFAULT;
2416 if (put_user(0, _frames))
2417 return -EFAULT;
2418 result = snd_pcm_playback_forward(substream, frames);
2419 __put_user(result, _frames);
2420 return result < 0 ? result : 0;
2422 case SNDRV_PCM_IOCTL_PAUSE:
2424 int res;
2425 snd_pcm_stream_lock_irq(substream);
2426 res = snd_pcm_pause(substream, (long) arg);
2427 snd_pcm_stream_unlock_irq(substream);
2428 return res;
2430 case SNDRV_PCM_IOCTL_DRAIN:
2431 return snd_pcm_playback_drain(substream);
2432 case SNDRV_PCM_IOCTL_DROP:
2433 return snd_pcm_playback_drop(substream);
2435 return snd_pcm_common_ioctl1(substream, cmd, arg);
2438 static int snd_pcm_capture_ioctl1(snd_pcm_substream_t *substream,
2439 unsigned int cmd, void *arg)
2441 snd_assert(substream != NULL, return -ENXIO);
2442 snd_assert(substream->stream == SNDRV_PCM_STREAM_CAPTURE, return -EINVAL);
2443 switch (cmd) {
2444 case SNDRV_PCM_IOCTL_READI_FRAMES:
2446 snd_xferi_t xferi, *_xferi = arg;
2447 snd_pcm_runtime_t *runtime = substream->runtime;
2448 snd_pcm_sframes_t result;
2449 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2450 return -EBADFD;
2451 if (put_user(0, &_xferi->result))
2452 return -EFAULT;
2453 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2454 return -EFAULT;
2455 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2456 __put_user(result, &_xferi->result);
2457 return result < 0 ? result : 0;
2459 case SNDRV_PCM_IOCTL_READN_FRAMES:
2461 snd_xfern_t xfern, *_xfern = arg;
2462 snd_pcm_runtime_t *runtime = substream->runtime;
2463 void *bufs;
2464 snd_pcm_sframes_t result;
2465 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2466 return -EBADFD;
2467 if (runtime->channels > 128)
2468 return -EINVAL;
2469 if (put_user(0, &_xfern->result))
2470 return -EFAULT;
2471 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2472 return -EFAULT;
2473 bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL);
2474 if (bufs == NULL)
2475 return -ENOMEM;
2476 if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) {
2477 kfree(bufs);
2478 return -EFAULT;
2480 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2481 kfree(bufs);
2482 __put_user(result, &_xfern->result);
2483 return result < 0 ? result : 0;
2485 case SNDRV_PCM_IOCTL_REWIND:
2487 snd_pcm_uframes_t frames, *_frames = arg;
2488 snd_pcm_sframes_t result;
2489 if (get_user(frames, _frames))
2490 return -EFAULT;
2491 if (put_user(0, _frames))
2492 return -EFAULT;
2493 result = snd_pcm_capture_rewind(substream, frames);
2494 __put_user(result, _frames);
2495 return result < 0 ? result : 0;
2497 case SNDRV_PCM_IOCTL_FORWARD:
2499 snd_pcm_uframes_t frames, *_frames = arg;
2500 snd_pcm_sframes_t result;
2501 if (get_user(frames, _frames))
2502 return -EFAULT;
2503 if (put_user(0, _frames))
2504 return -EFAULT;
2505 result = snd_pcm_capture_forward(substream, frames);
2506 __put_user(result, _frames);
2507 return result < 0 ? result : 0;
2509 case SNDRV_PCM_IOCTL_DRAIN:
2510 return snd_pcm_capture_drain(substream);
2511 case SNDRV_PCM_IOCTL_DROP:
2512 return snd_pcm_capture_drop(substream);
2514 return snd_pcm_common_ioctl1(substream, cmd, arg);
2517 static int snd_pcm_playback_ioctl(struct inode *inode, struct file *file,
2518 unsigned int cmd, unsigned long arg)
2520 snd_pcm_file_t *pcm_file;
2522 pcm_file = snd_magic_cast(snd_pcm_file_t, file->private_data, return -ENXIO);
2524 if (((cmd >> 8) & 0xff) != 'A')
2525 return -ENOTTY;
2527 return snd_pcm_playback_ioctl1(pcm_file->substream, cmd, (void *) arg);
2530 static int snd_pcm_capture_ioctl(struct inode *inode, struct file *file,
2531 unsigned int cmd, unsigned long arg)
2533 snd_pcm_file_t *pcm_file;
2535 pcm_file = snd_magic_cast(snd_pcm_file_t, file->private_data, return -ENXIO);
2537 if (((cmd >> 8) & 0xff) != 'A')
2538 return -ENOTTY;
2540 return snd_pcm_capture_ioctl1(pcm_file->substream, cmd, (void *) arg);
2543 int snd_pcm_kernel_playback_ioctl(snd_pcm_substream_t *substream,
2544 unsigned int cmd, void *arg)
2546 mm_segment_t fs;
2547 int result;
2549 fs = snd_enter_user();
2550 result = snd_pcm_playback_ioctl1(substream, cmd, arg);
2551 snd_leave_user(fs);
2552 return result;
2555 int snd_pcm_kernel_capture_ioctl(snd_pcm_substream_t *substream,
2556 unsigned int cmd, void *arg)
2558 mm_segment_t fs;
2559 int result;
2561 fs = snd_enter_user();
2562 result = snd_pcm_capture_ioctl1(substream, cmd, arg);
2563 snd_leave_user(fs);
2564 return result;
2567 int snd_pcm_kernel_ioctl(snd_pcm_substream_t *substream,
2568 unsigned int cmd, void *arg)
2570 switch (substream->stream) {
2571 case SNDRV_PCM_STREAM_PLAYBACK:
2572 return snd_pcm_kernel_playback_ioctl(substream, cmd, arg);
2573 case SNDRV_PCM_STREAM_CAPTURE:
2574 return snd_pcm_kernel_capture_ioctl(substream, cmd, arg);
2575 default:
2576 return -EINVAL;
2580 static ssize_t snd_pcm_read(struct file *file, char *buf, size_t count, loff_t * offset)
2582 snd_pcm_file_t *pcm_file;
2583 snd_pcm_substream_t *substream;
2584 snd_pcm_runtime_t *runtime;
2585 snd_pcm_sframes_t result;
2587 pcm_file = snd_magic_cast(snd_pcm_file_t, file->private_data, return -ENXIO);
2588 substream = pcm_file->substream;
2589 snd_assert(substream != NULL, return -ENXIO);
2590 runtime = substream->runtime;
2591 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2592 return -EBADFD;
2593 if (!frame_aligned(runtime, count))
2594 return -EINVAL;
2595 count = bytes_to_frames(runtime, count);
2596 result = snd_pcm_lib_read(substream, buf, count);
2597 if (result > 0)
2598 result = frames_to_bytes(runtime, result);
2599 return result;
2602 static ssize_t snd_pcm_write(struct file *file, const char *buf, size_t count, loff_t * offset)
2604 snd_pcm_file_t *pcm_file;
2605 snd_pcm_substream_t *substream;
2606 snd_pcm_runtime_t *runtime;
2607 snd_pcm_sframes_t result;
2609 pcm_file = snd_magic_cast(snd_pcm_file_t, file->private_data, result = -ENXIO; goto end);
2610 substream = pcm_file->substream;
2611 snd_assert(substream != NULL, result = -ENXIO; goto end);
2612 runtime = substream->runtime;
2613 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
2614 result = -EBADFD;
2615 goto end;
2617 if (!frame_aligned(runtime, count)) {
2618 result = -EINVAL;
2619 goto end;
2621 count = bytes_to_frames(runtime, count);
2622 result = snd_pcm_lib_write(substream, buf, count);
2623 if (result > 0)
2624 result = frames_to_bytes(runtime, result);
2625 end:
2626 return result;
2629 static ssize_t snd_pcm_readv(struct file *file, const struct iovec *_vector,
2630 unsigned long count, loff_t * offset)
2633 snd_pcm_file_t *pcm_file;
2634 snd_pcm_substream_t *substream;
2635 snd_pcm_runtime_t *runtime;
2636 snd_pcm_sframes_t result;
2637 unsigned long i;
2638 void **bufs;
2639 snd_pcm_uframes_t frames;
2641 pcm_file = snd_magic_cast(snd_pcm_file_t, file->private_data, return -ENXIO);
2642 substream = pcm_file->substream;
2643 snd_assert(substream != NULL, return -ENXIO);
2644 runtime = substream->runtime;
2645 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2646 return -EBADFD;
2647 if (count > 1024 || count != runtime->channels)
2648 return -EINVAL;
2649 if (!frame_aligned(runtime, _vector->iov_len))
2650 return -EINVAL;
2651 frames = bytes_to_samples(runtime, _vector->iov_len);
2652 bufs = kmalloc(sizeof(void *) * count, GFP_KERNEL);
2653 if (bufs == NULL)
2654 return -ENOMEM;
2655 for (i = 0; i < count; ++i)
2656 bufs[i] = _vector[i].iov_base;
2657 result = snd_pcm_lib_readv(substream, bufs, frames);
2658 if (result > 0)
2659 result = frames_to_bytes(runtime, result);
2660 kfree(bufs);
2661 return result;
2664 static ssize_t snd_pcm_writev(struct file *file, const struct iovec *_vector,
2665 unsigned long count, loff_t * offset)
2667 snd_pcm_file_t *pcm_file;
2668 snd_pcm_substream_t *substream;
2669 snd_pcm_runtime_t *runtime;
2670 snd_pcm_sframes_t result;
2671 unsigned long i;
2672 void **bufs;
2673 snd_pcm_uframes_t frames;
2675 pcm_file = snd_magic_cast(snd_pcm_file_t, file->private_data, result = -ENXIO; goto end);
2676 substream = pcm_file->substream;
2677 snd_assert(substream != NULL, result = -ENXIO; goto end);
2678 runtime = substream->runtime;
2679 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
2680 result = -EBADFD;
2681 goto end;
2683 if (count > 128 || count != runtime->channels ||
2684 !frame_aligned(runtime, _vector->iov_len)) {
2685 result = -EINVAL;
2686 goto end;
2688 frames = bytes_to_samples(runtime, _vector->iov_len);
2689 bufs = kmalloc(sizeof(void *) * count, GFP_KERNEL);
2690 if (bufs == NULL)
2691 return -ENOMEM;
2692 for (i = 0; i < count; ++i)
2693 bufs[i] = _vector[i].iov_base;
2694 result = snd_pcm_lib_writev(substream, bufs, frames);
2695 if (result > 0)
2696 result = frames_to_bytes(runtime, result);
2697 kfree(bufs);
2698 end:
2699 return result;
2702 unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
2704 snd_pcm_file_t *pcm_file;
2705 snd_pcm_substream_t *substream;
2706 snd_pcm_runtime_t *runtime;
2707 unsigned int mask;
2708 snd_pcm_uframes_t avail;
2710 pcm_file = snd_magic_cast(snd_pcm_file_t, file->private_data, return 0);
2712 substream = pcm_file->substream;
2713 snd_assert(substream != NULL, return -ENXIO);
2714 runtime = substream->runtime;
2716 poll_wait(file, &runtime->sleep, wait);
2718 snd_pcm_stream_lock_irq(substream);
2719 avail = snd_pcm_playback_avail(runtime);
2720 switch (runtime->status->state) {
2721 case SNDRV_PCM_STATE_RUNNING:
2722 case SNDRV_PCM_STATE_PREPARED:
2723 case SNDRV_PCM_STATE_PAUSED:
2724 if (avail >= runtime->control->avail_min) {
2725 mask = POLLOUT | POLLWRNORM;
2726 break;
2728 /* Fall through */
2729 case SNDRV_PCM_STATE_DRAINING:
2730 mask = 0;
2731 break;
2732 default:
2733 mask = POLLOUT | POLLWRNORM | POLLERR;
2734 break;
2736 snd_pcm_stream_unlock_irq(substream);
2737 return mask;
2740 unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
2742 snd_pcm_file_t *pcm_file;
2743 snd_pcm_substream_t *substream;
2744 snd_pcm_runtime_t *runtime;
2745 unsigned int mask;
2746 snd_pcm_uframes_t avail;
2748 pcm_file = snd_magic_cast(snd_pcm_file_t, file->private_data, return 0);
2750 substream = pcm_file->substream;
2751 snd_assert(substream != NULL, return -ENXIO);
2752 runtime = substream->runtime;
2754 poll_wait(file, &runtime->sleep, wait);
2756 snd_pcm_stream_lock_irq(substream);
2757 avail = snd_pcm_capture_avail(runtime);
2758 switch (runtime->status->state) {
2759 case SNDRV_PCM_STATE_RUNNING:
2760 case SNDRV_PCM_STATE_PREPARED:
2761 case SNDRV_PCM_STATE_PAUSED:
2762 if (avail >= runtime->control->avail_min) {
2763 mask = POLLIN | POLLRDNORM;
2764 break;
2766 mask = 0;
2767 break;
2768 case SNDRV_PCM_STATE_DRAINING:
2769 if (avail > 0) {
2770 mask = POLLIN | POLLRDNORM;
2771 break;
2773 /* Fall through */
2774 default:
2775 mask = POLLIN | POLLRDNORM | POLLERR;
2776 break;
2778 snd_pcm_stream_unlock_irq(substream);
2779 return mask;
2782 static struct page * snd_pcm_mmap_status_nopage(struct vm_area_struct *area, unsigned long address, int *type)
2784 snd_pcm_substream_t *substream = (snd_pcm_substream_t *)area->vm_private_data;
2785 snd_pcm_runtime_t *runtime;
2786 struct page * page;
2788 if (substream == NULL)
2789 return NOPAGE_OOM;
2790 runtime = substream->runtime;
2791 page = virt_to_page(runtime->status);
2792 if (!PageReserved(page))
2793 get_page(page);
2794 if (type)
2795 *type = VM_FAULT_MINOR;
2796 return page;
2799 static struct vm_operations_struct snd_pcm_vm_ops_status =
2801 .nopage = snd_pcm_mmap_status_nopage,
2804 int snd_pcm_mmap_status(snd_pcm_substream_t *substream, struct file *file,
2805 struct vm_area_struct *area)
2807 snd_pcm_runtime_t *runtime;
2808 long size;
2809 if (!(area->vm_flags & VM_READ))
2810 return -EINVAL;
2811 runtime = substream->runtime;
2812 snd_assert(runtime != NULL, return -EAGAIN);
2813 size = area->vm_end - area->vm_start;
2814 if (size != PAGE_ALIGN(sizeof(snd_pcm_mmap_status_t)))
2815 return -EINVAL;
2816 area->vm_ops = &snd_pcm_vm_ops_status;
2817 area->vm_private_data = substream;
2818 area->vm_flags |= VM_RESERVED;
2819 return 0;
2822 static struct page * snd_pcm_mmap_control_nopage(struct vm_area_struct *area, unsigned long address, int *type)
2824 snd_pcm_substream_t *substream = (snd_pcm_substream_t *)area->vm_private_data;
2825 snd_pcm_runtime_t *runtime;
2826 struct page * page;
2828 if (substream == NULL)
2829 return NOPAGE_OOM;
2830 runtime = substream->runtime;
2831 page = virt_to_page(runtime->control);
2832 if (!PageReserved(page))
2833 get_page(page);
2834 if (type)
2835 *type = VM_FAULT_MINOR;
2836 return page;
2839 static struct vm_operations_struct snd_pcm_vm_ops_control =
2841 .nopage = snd_pcm_mmap_control_nopage,
2844 static int snd_pcm_mmap_control(snd_pcm_substream_t *substream, struct file *file,
2845 struct vm_area_struct *area)
2847 snd_pcm_runtime_t *runtime;
2848 long size;
2849 if (!(area->vm_flags & VM_READ))
2850 return -EINVAL;
2851 runtime = substream->runtime;
2852 snd_assert(runtime != NULL, return -EAGAIN);
2853 size = area->vm_end - area->vm_start;
2854 if (size != PAGE_ALIGN(sizeof(snd_pcm_mmap_control_t)))
2855 return -EINVAL;
2856 area->vm_ops = &snd_pcm_vm_ops_control;
2857 area->vm_private_data = substream;
2858 area->vm_flags |= VM_RESERVED;
2859 return 0;
2862 static void snd_pcm_mmap_data_open(struct vm_area_struct *area)
2864 snd_pcm_substream_t *substream = (snd_pcm_substream_t *)area->vm_private_data;
2865 atomic_inc(&substream->runtime->mmap_count);
2868 static void snd_pcm_mmap_data_close(struct vm_area_struct *area)
2870 snd_pcm_substream_t *substream = (snd_pcm_substream_t *)area->vm_private_data;
2871 atomic_dec(&substream->runtime->mmap_count);
2874 static struct page * snd_pcm_mmap_data_nopage(struct vm_area_struct *area, unsigned long address, int *type)
2876 snd_pcm_substream_t *substream = (snd_pcm_substream_t *)area->vm_private_data;
2877 snd_pcm_runtime_t *runtime;
2878 unsigned long offset;
2879 struct page * page;
2880 void *vaddr;
2881 size_t dma_bytes;
2883 if (substream == NULL)
2884 return NOPAGE_OOM;
2885 runtime = substream->runtime;
2886 offset = area->vm_pgoff << PAGE_SHIFT;
2887 offset += address - area->vm_start;
2888 snd_assert((offset % PAGE_SIZE) == 0, return NOPAGE_OOM);
2889 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
2890 if (offset > dma_bytes - PAGE_SIZE)
2891 return NOPAGE_SIGBUS;
2892 if (substream->ops->page) {
2893 page = substream->ops->page(substream, offset);
2894 if (! page)
2895 return NOPAGE_OOM;
2896 } else {
2897 vaddr = runtime->dma_area + offset;
2898 page = virt_to_page(vaddr);
2900 if (!PageReserved(page))
2901 get_page(page);
2902 if (type)
2903 *type = VM_FAULT_MINOR;
2904 return page;
2907 static struct vm_operations_struct snd_pcm_vm_ops_data =
2909 .open = snd_pcm_mmap_data_open,
2910 .close = snd_pcm_mmap_data_close,
2911 .nopage = snd_pcm_mmap_data_nopage,
2914 int snd_pcm_mmap_data(snd_pcm_substream_t *substream, struct file *file,
2915 struct vm_area_struct *area)
2917 snd_pcm_runtime_t *runtime;
2918 long size;
2919 unsigned long offset;
2920 size_t dma_bytes;
2922 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
2923 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
2924 return -EINVAL;
2925 } else {
2926 if (!(area->vm_flags & VM_READ))
2927 return -EINVAL;
2929 runtime = substream->runtime;
2930 snd_assert(runtime != NULL, return -EAGAIN);
2931 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2932 return -EBADFD;
2933 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
2934 return -ENXIO;
2935 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
2936 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2937 return -EINVAL;
2938 size = area->vm_end - area->vm_start;
2939 offset = area->vm_pgoff << PAGE_SHIFT;
2940 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
2941 if ((size_t)size > dma_bytes)
2942 return -EINVAL;
2943 if (offset > dma_bytes - size)
2944 return -EINVAL;
2946 area->vm_ops = &snd_pcm_vm_ops_data;
2947 area->vm_private_data = substream;
2948 area->vm_flags |= VM_RESERVED;
2949 atomic_inc(&runtime->mmap_count);
2950 return 0;
2953 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
2955 snd_pcm_file_t * pcm_file;
2956 snd_pcm_substream_t *substream;
2957 unsigned long offset;
2959 pcm_file = snd_magic_cast(snd_pcm_file_t, file->private_data, return -ENXIO);
2960 substream = pcm_file->substream;
2961 snd_assert(substream != NULL, return -ENXIO);
2963 offset = area->vm_pgoff << PAGE_SHIFT;
2964 switch (offset) {
2965 case SNDRV_PCM_MMAP_OFFSET_STATUS:
2966 return snd_pcm_mmap_status(substream, file, area);
2967 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
2968 return snd_pcm_mmap_control(substream, file, area);
2969 default:
2970 return snd_pcm_mmap_data(substream, file, area);
2972 return 0;
2975 static int snd_pcm_fasync(int fd, struct file * file, int on)
2977 snd_pcm_file_t * pcm_file;
2978 snd_pcm_substream_t *substream;
2979 snd_pcm_runtime_t *runtime;
2980 int err;
2982 pcm_file = snd_magic_cast(snd_pcm_file_t, file->private_data, return -ENXIO);
2983 substream = pcm_file->substream;
2984 snd_assert(substream != NULL, return -ENXIO);
2985 runtime = substream->runtime;
2987 err = fasync_helper(fd, file, on, &runtime->fasync);
2988 if (err < 0)
2989 return err;
2990 return 0;
2994 * To be removed helpers to keep binary compatibility
2997 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
2998 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3000 static void snd_pcm_hw_convert_from_old_params(snd_pcm_hw_params_t *params, struct sndrv_pcm_hw_params_old *oparams)
3002 unsigned int i;
3004 memset(params, 0, sizeof(*params));
3005 params->flags = oparams->flags;
3006 for (i = 0; i < sizeof(oparams->masks) / sizeof(unsigned int); i++)
3007 params->masks[i].bits[0] = oparams->masks[i];
3008 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3009 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3010 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3011 params->info = oparams->info;
3012 params->msbits = oparams->msbits;
3013 params->rate_num = oparams->rate_num;
3014 params->rate_den = oparams->rate_den;
3015 params->fifo_size = oparams->fifo_size;
3018 static void snd_pcm_hw_convert_to_old_params(struct sndrv_pcm_hw_params_old *oparams, snd_pcm_hw_params_t *params)
3020 unsigned int i;
3022 memset(oparams, 0, sizeof(*oparams));
3023 oparams->flags = params->flags;
3024 for (i = 0; i < sizeof(oparams->masks) / sizeof(unsigned int); i++)
3025 oparams->masks[i] = params->masks[i].bits[0];
3026 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3027 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3028 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3029 oparams->info = params->info;
3030 oparams->msbits = params->msbits;
3031 oparams->rate_num = params->rate_num;
3032 oparams->rate_den = params->rate_den;
3033 oparams->fifo_size = params->fifo_size;
3036 static int snd_pcm_hw_refine_old_user(snd_pcm_substream_t * substream, struct sndrv_pcm_hw_params_old * _oparams)
3038 snd_pcm_hw_params_t params;
3039 struct sndrv_pcm_hw_params_old oparams;
3040 int err;
3041 if (copy_from_user(&oparams, _oparams, sizeof(oparams)))
3042 return -EFAULT;
3043 snd_pcm_hw_convert_from_old_params(&params, &oparams);
3044 err = snd_pcm_hw_refine(substream, &params);
3045 snd_pcm_hw_convert_to_old_params(&oparams, &params);
3046 if (copy_to_user(_oparams, &oparams, sizeof(oparams)))
3047 return -EFAULT;
3048 return err;
3051 static int snd_pcm_hw_params_old_user(snd_pcm_substream_t * substream, struct sndrv_pcm_hw_params_old * _oparams)
3053 snd_pcm_hw_params_t params;
3054 struct sndrv_pcm_hw_params_old oparams;
3055 int err;
3056 if (copy_from_user(&oparams, _oparams, sizeof(oparams)))
3057 return -EFAULT;
3058 snd_pcm_hw_convert_from_old_params(&params, &oparams);
3059 err = snd_pcm_hw_params(substream, &params);
3060 snd_pcm_hw_convert_to_old_params(&oparams, &params);
3061 if (copy_to_user(_oparams, &oparams, sizeof(oparams)))
3062 return -EFAULT;
3063 return err;
3067 * Register section
3070 static struct file_operations snd_pcm_f_ops_playback = {
3071 .owner = THIS_MODULE,
3072 .write = snd_pcm_write,
3073 .writev = snd_pcm_writev,
3074 .open = snd_pcm_open,
3075 .release = snd_pcm_release,
3076 .poll = snd_pcm_playback_poll,
3077 .ioctl = snd_pcm_playback_ioctl,
3078 .mmap = snd_pcm_mmap,
3079 .fasync = snd_pcm_fasync,
3082 static struct file_operations snd_pcm_f_ops_capture = {
3083 .owner = THIS_MODULE,
3084 .read = snd_pcm_read,
3085 .readv = snd_pcm_readv,
3086 .open = snd_pcm_open,
3087 .release = snd_pcm_release,
3088 .poll = snd_pcm_capture_poll,
3089 .ioctl = snd_pcm_capture_ioctl,
3090 .mmap = snd_pcm_mmap,
3091 .fasync = snd_pcm_fasync,
3094 snd_minor_t snd_pcm_reg[2] =
3097 .comment = "digital audio playback",
3098 .f_ops = &snd_pcm_f_ops_playback,
3101 .comment = "digital audio capture",
3102 .f_ops = &snd_pcm_f_ops_capture,