~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/sound/core/pcm_native.c

Version: ~ [ linux-5.1-rc1 ] ~ [ linux-5.0.3 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.30 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.107 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.164 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.176 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.136 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.63 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.39.4 ] ~ [ linux-2.6.38.8 ] ~ [ linux-2.6.37.6 ] ~ [ linux-2.6.36.4 ] ~ [ linux-2.6.35.14 ] ~ [ linux-2.6.34.15 ] ~ [ linux-2.6.33.20 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  *  Digital Audio (PCM) abstract layer
  3  *  Copyright (c) by Jaroslav Kysela <perex@suse.cz>
  4  *
  5  *
  6  *   This program is free software; you can redistribute it and/or modify
  7  *   it under the terms of the GNU General Public License as published by
  8  *   the Free Software Foundation; either version 2 of the License, or
  9  *   (at your option) any later version.
 10  *
 11  *   This program is distributed in the hope that it will be useful,
 12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 14  *   GNU General Public License for more details.
 15  *
 16  *   You should have received a copy of the GNU General Public License
 17  *   along with this program; if not, write to the Free Software
 18  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 19  *
 20  */
 21 
 22 #include <sound/driver.h>
 23 #include <linux/mm.h>
 24 #include <linux/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>
 35 
 36 /*
 37  *  Compatibility
 38  */
 39 
 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];
 54 };
 55 
 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)
 58 
 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);
 61 
 62 /*
 63  *
 64  */
 65 
 66 rwlock_t snd_pcm_link_rwlock = RW_LOCK_UNLOCKED;
 67 
 68 
 69 static inline mm_segment_t snd_enter_user(void)
 70 {
 71         mm_segment_t fs = get_fs();
 72         set_fs(get_ds());
 73         return fs;
 74 }
 75 
 76 static inline void snd_leave_user(mm_segment_t fs)
 77 {
 78         set_fs(fs);
 79 }
 80 
 81 
 82 
 83 int snd_pcm_info(snd_pcm_substream_t * substream, snd_pcm_info_t *info)
 84 {
 85         snd_pcm_runtime_t * runtime;
 86         snd_pcm_t *pcm = substream->pcm;
 87         snd_pcm_str_t *pstr = substream->pstr;
 88 
 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);
107         }
108         return 0;
109 }
110 
111 int snd_pcm_info_user(snd_pcm_substream_t * substream, snd_pcm_info_t * _info)
112 {
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;
118 }
119 
120 #undef RULES_DEBUG
121 
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),
140 };
141 #endif
142 
143 int snd_pcm_hw_refine(snd_pcm_substream_t *substream, 
144                       snd_pcm_hw_params_t *params)
145 {
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;
155 
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;
163         }
164 
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;
183         }
184 
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;
214         }
215 
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;
232                                 }
233                         }
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 ? ')' : ']');
251                                 }
252                         }
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 ? ')' : ']');
267                                 }
268                         }
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;
276                         }
277                         if (changed < 0)
278                                 return changed;
279                         stamp++;
280                 }
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);
286         }
287 
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;
293                 }
294         }
295 
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;
303 }
304 
305 static int snd_pcm_hw_refine_user(snd_pcm_substream_t * substream, snd_pcm_hw_params_t * _params)
306 {
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;
315 }
316 
317 static int snd_pcm_hw_params(snd_pcm_substream_t *substream,
318                              snd_pcm_hw_params_t *params)
319 {
320         snd_pcm_runtime_t *runtime;
321         int err;
322         unsigned int bits;
323         snd_pcm_uframes_t frames;
324 
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;
335         }
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;
341 
342         params->rmask = ~0U;
343         err = snd_pcm_hw_refine(substream, params);
344         if (err < 0)
345                 goto _error;
346 
347         err = snd_pcm_hw_params_choose(substream, params);
348         if (err < 0)
349                 goto _error;
350 
351         if (substream->ops->hw_params != NULL) {
352                 err = substream->ops->hw_params(substream, params);
353                 if (err < 0)
354                         goto _error;
355         }
356 
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;
369 
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;
378         }
379         runtime->byte_align = bits / 8;
380         runtime->min_align = frames;
381 
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;
395 
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;
407 }
408 
409 static int snd_pcm_hw_params_user(snd_pcm_substream_t * substream, snd_pcm_hw_params_t * _params)
410 {
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;
419 }
420 
421 static int snd_pcm_hw_free(snd_pcm_substream_t * substream)
422 {
423         snd_pcm_runtime_t *runtime;
424         int result;
425 
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;
435         }
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;
441         }
442         result = substream->ops->hw_free(substream);
443         runtime->status->state = SNDRV_PCM_STATE_OPEN;
444         return result;
445 }
446 
447 static int snd_pcm_sw_params(snd_pcm_substream_t * substream, snd_pcm_sw_params_t *params)
448 {
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;
455 
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;
471         }
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);
492         }
493         snd_pcm_stream_unlock_irq(substream);
494         return 0;
495 }
496 
497 static int snd_pcm_sw_params_user(snd_pcm_substream_t * substream, snd_pcm_sw_params_t * _params)
498 {
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;
507 }
508 
509 int snd_pcm_status(snd_pcm_substream_t *substream,
510                    snd_pcm_status_t *status)
511 {
512         snd_pcm_runtime_t *runtime = substream->runtime;
513 
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;
543         }
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;
551 }
552 
553 static int snd_pcm_status_user(snd_pcm_substream_t * substream, snd_pcm_status_t * _status)
554 {
555         snd_pcm_status_t status;
556         snd_pcm_runtime_t *runtime;
557         int res;
558         
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;
568 }
569 
570 static int snd_pcm_channel_info(snd_pcm_substream_t * substream, snd_pcm_channel_info_t * _info)
571 {
572         snd_pcm_channel_info_t info;
573         snd_pcm_runtime_t *runtime;
574         int res;
575         unsigned int channel;
576         
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;
594 }
595 
596 static void snd_pcm_trigger_tstamp(snd_pcm_substream_t *substream)
597 {
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;
606         }
607         runtime->trigger_master = NULL;
608 }
609 
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);
614 };
615 
616 /*
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
620  */
621 static int snd_pcm_action_group(struct action_ops *ops,
622                                 snd_pcm_substream_t *substream,
623                                 int state)
624 {
625         struct list_head *pos;
626         snd_pcm_substream_t *s = NULL;
627         int err, res = 0;
628 
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;
636         }
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);
646                         }
647                         if (s != substream)
648                                 spin_unlock(&s->self_group.lock);
649                 }
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;
659                 }
660         }
661         return res;
662 }
663 
664 /*
665  *  Note: call with stream lock
666  */
667 static int snd_pcm_action_single(struct action_ops *ops,
668                                  snd_pcm_substream_t *substream,
669                                  int state)
670 {
671         int res;
672         
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);
679         }
680         return res;
681 }
682 
683 /*
684  *  Note: call with stream lock
685  */
686 static int snd_pcm_action(struct action_ops *ops,
687                           snd_pcm_substream_t *substream,
688                           int state)
689 {
690         int res;
691 
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);
697                 }
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);
702         }
703         return res;
704 }
705 
706 /*
707  *  Note: don't use any locks before
708  */
709 static int snd_pcm_action_lock_irq(struct action_ops *ops,
710                                    snd_pcm_substream_t *substream,
711                                    int state)
712 {
713         int res;
714 
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);
726         }
727         read_unlock_irq(&snd_pcm_link_rwlock);
728         return res;
729 }
730 
731 static int snd_pcm_pre_start(snd_pcm_substream_t *substream, int state)
732 {
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;
741 }
742 
743 static int snd_pcm_do_start(snd_pcm_substream_t *substream, int state)
744 {
745         if (substream->runtime->trigger_master != substream)
746                 return 0;
747         return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
748 }
749 
750 static void snd_pcm_post_start(snd_pcm_substream_t *substream, int state)
751 {
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);
762 }
763 
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
768 };
769 
770 /**
771  * snd_pcm_start
772  */
773 int snd_pcm_start(snd_pcm_substream_t *substream)
774 {
775         return snd_pcm_action(&snd_pcm_action_start, substream, 0);
776 }
777 
778 static int snd_pcm_pre_stop(snd_pcm_substream_t *substream, int state)
779 {
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;
786 }
787 
788 static int snd_pcm_do_stop(snd_pcm_substream_t *substream, int state)
789 {
790         if (substream->runtime->trigger_master != substream)
791                 return 0;
792         return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
793 }
794 
795 static void snd_pcm_post_stop(snd_pcm_substream_t *substream, int state)
796 {
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);
804 }
805 
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
810 };
811 
812 /**
813  * snd_pcm_stop
814  */
815 int snd_pcm_stop(snd_pcm_substream_t *substream, int state)
816 {
817         return snd_pcm_action(&snd_pcm_action_stop, substream, state);
818 }
819 
820 static int snd_pcm_pre_pause(snd_pcm_substream_t *substream, int push)
821 {
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;
832 }
833 
834 static int snd_pcm_do_pause(snd_pcm_substream_t *substream, int push)
835 {
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);
841 }
842 
843 static void snd_pcm_post_pause(snd_pcm_substream_t *substream, int push)
844 {
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);
859         }
860 }
861 
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
866 };
867 
868 static int snd_pcm_pause(snd_pcm_substream_t *substream, int push)
869 {
870         return snd_pcm_action(&snd_pcm_action_pause, substream, push);
871 }
872 
873 #ifdef CONFIG_PM
874 /* suspend */
875 
876 static int snd_pcm_pre_suspend(snd_pcm_substream_t *substream, int state)
877 {
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;
884 }
885 
886 static int snd_pcm_do_suspend(snd_pcm_substream_t *substream, int state)
887 {
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);
894 }
895 
896 static void snd_pcm_post_suspend(snd_pcm_substream_t *substream, int state)
897 {
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);
905 }
906 
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
911 };
912 
913 /**
914  * snd_pcm_suspend
915  */
916 int snd_pcm_suspend(snd_pcm_substream_t *substream)
917 {
918         return snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
919 }
920 
921 /**
922  * snd_pcm_suspend_all
923  */
924 int snd_pcm_suspend_all(snd_pcm_t *pcm)
925 {
926         snd_pcm_substream_t *substream;
927         int stream, err;
928 
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;
938                         }
939                         if ((err = snd_pcm_suspend(substream)) < 0) {
940                                 snd_pcm_stream_unlock(substream);
941                                 return err;
942                         }
943                         snd_pcm_stream_unlock(substream);
944                 }
945         }
946         return 0;
947 }
948 
949 /* resume */
950 
951 static int snd_pcm_pre_resume(snd_pcm_substream_t *substream, int state)
952 {
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;
958 }
959 
960 static int snd_pcm_do_resume(snd_pcm_substream_t *substream, int state)
961 {
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);
968 }
969 
970 static void snd_pcm_post_resume(snd_pcm_substream_t *substream, int state)
971 {
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);
979 }
980 
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
985 };
986 
987 static int snd_pcm_resume(snd_pcm_substream_t *substream)
988 {
989         snd_card_t *card = substream->pcm->card;
990         int res;
991 
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;
997 }
998 
999 #else
1000 
1001 static int snd_pcm_resume(snd_pcm_substream_t *substream)
1002 {
1003         return -ENOSYS;
1004 }
1005 
1006 #endif /* CONFIG_PM */
1007 
1008 static int snd_pcm_xrun(snd_pcm_substream_t *substream)
1009 {
1010         snd_card_t *card = substream->pcm->card;
1011         snd_pcm_runtime_t *runtime = substream->runtime;
1012         int result;
1013 
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;
1033         }
1034         snd_pcm_stream_unlock_irq(substream);
1035         snd_power_unlock(card);
1036         return result;
1037 }
1038 
1039 static int snd_pcm_pre_reset(snd_pcm_substream_t * substream, int state)
1040 {
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;
1050         }
1051 }
1052 
1053 static int snd_pcm_do_reset(snd_pcm_substream_t * substream, int state)
1054 {
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;
1065 }
1066 
1067 static void snd_pcm_post_reset(snd_pcm_substream_t * substream, int state)
1068 {
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);
1074 }
1075 
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
1080 };
1081 
1082 static int snd_pcm_reset(snd_pcm_substream_t *substream)
1083 {
1084         return snd_pcm_action_lock_irq(&snd_pcm_action_reset, substream, 0);
1085 }
1086 
1087 static int snd_pcm_pre_prepare(snd_pcm_substream_t * substream, int state)
1088 {
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;
1097         }
1098 }
1099 
1100 static int snd_pcm_do_prepare(snd_pcm_substream_t * substream, int state)
1101 {
1102         int err;
1103         err = substream->ops->prepare(substream);
1104         if (err < 0)
1105                 return err;
1106         return snd_pcm_do_reset(substream, 0);
1107 }
1108 
1109 static void snd_pcm_post_prepare(snd_pcm_substream_t * substream, int state)
1110 {
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;
1114 }
1115 
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
1120 };
1121 
1122 /**
1123  * snd_pcm_prepare
1124  */
1125 int snd_pcm_prepare(snd_pcm_substream_t *substream)
1126 {
1127         int res;
1128         snd_card_t *card = substream->pcm->card;
1129 
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;
1135 }
1136 
1137 static void snd_pcm_change_state(snd_pcm_substream_t *substream, int state)
1138 {
1139         struct list_head *pos;
1140         snd_pcm_substream_t *s;
1141 
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);
1147                 }
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);
1155                 }
1156                 spin_unlock(&substream->group->lock);
1157         } else {
1158                 substream->runtime->status->state = state;
1159         }
1160 }
1161 
1162 static int snd_pcm_playback_drop(snd_pcm_substream_t *substream);
1163 
1164 static int snd_pcm_playback_drain(snd_pcm_substream_t * substream)
1165 {
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;
1172 
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;
1177 
1178         snd_power_lock(card);
1179         snd_pcm_stream_lock_irq(substream);
1180 
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;
1186 
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;
1210                         }
1211                         break;
1212                 }
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; 
1221         }
1222 
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;
1227                 }
1228                 snd_pcm_change_state(substream, SNDRV_PCM_STATE_DRAINING);
1229         }
1230 
1231         if (substream->ffile->f_flags & O_NONBLOCK) {
1232                 result = -EAGAIN;
1233                 goto _end;
1234         }
1235 
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;
1243                 }
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;
1251                 }
1252                 if (runtime->status->state != SNDRV_PCM_STATE_DRAINING) {
1253                         state = READY;
1254                         break;
1255                 }
1256         }
1257         remove_wait_queue(&runtime->sleep, &wait);
1258 
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;
1272         }
1273 
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);
1280 
1281         return result;
1282 }
1283 
1284 static int snd_pcm_playback_drop(snd_pcm_substream_t *substream)
1285 {
1286         snd_pcm_runtime_t *runtime = substream->runtime;
1287         snd_card_t *card = substream->pcm->card;
1288         int res = 0;
1289         
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;
1306                 }
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; 
1321         }
1322         runtime->control->appl_ptr = runtime->status->hw_ptr;
1323         snd_pcm_stream_unlock_irq(substream);
1324         snd_power_unlock(card);
1325         return res;
1326 }
1327 
1328 static int snd_pcm_capture_drain(snd_pcm_substream_t * substream)
1329 {
1330         snd_pcm_runtime_t *runtime = substream->runtime;
1331         snd_card_t *card = substream->pcm->card;
1332         int res = 0;
1333 
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;
1355                 }
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; 
1372         }
1373         snd_pcm_stream_unlock_irq(substream);
1374         snd_power_unlock(card);
1375         return res;
1376 }
1377 
1378 static int snd_pcm_capture_drop(snd_pcm_substream_t * substream)
1379 {
1380         snd_pcm_runtime_t *runtime = substream->runtime;
1381         snd_card_t *card = substream->pcm->card;
1382         int res = 0;
1383 
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; 
1412         }
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;
1418 }
1419 
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)
1423 {
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;
1435         }
1436         minor = iminor(inode);
1437         if (minor >= 256 || 
1438             minor % SNDRV_MINOR_DEVICES < SNDRV_MINOR_PCM_PLAYBACK) {
1439                 fput(file);
1440                 return 0;
1441         }
1442         return file;
1443 }
1444 
1445 static int snd_pcm_link(snd_pcm_substream_t *substream, int fd)
1446 {
1447         int res = 0;
1448         struct file *file;
1449         snd_pcm_file_t *pcm_file;
1450         snd_pcm_substream_t *substream1;
1451 
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;
1463         }
1464         if (snd_pcm_stream_linked(substream1)) {
1465                 res = -EALREADY;
1466                 goto _end;
1467         }
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;
1473                 }
1474                 spin_lock_init(&substream->group->lock);
1475                 INIT_LIST_HEAD(&substream->group->substreams);
1476                 list_add_tail(&substream->link_list, &substream->group->substreams);
1477         }
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;
1484 }
1485 
1486 static void relink_to_local(snd_pcm_substream_t *substream)
1487 {
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);
1491 }
1492 
1493 static int snd_pcm_unlink(snd_pcm_substream_t *substream)
1494 {
1495         struct list_head *pos;
1496         int res = 0, count = 0;
1497 
1498         write_lock_irq(&snd_pcm_link_rwlock);
1499         if (!snd_pcm_stream_linked(substream)) {
1500                 res = -EINVAL;
1501                 goto _end;
1502         }
1503         list_del(&substream->link_list);
1504         snd_pcm_group_for_each(pos, substream) {
1505                 if (++count > 1)
1506                         break;
1507         }
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;
1512                 }
1513                 kfree(substream->group);
1514         }
1515         relink_to_local(substream);
1516        _end:
1517         write_unlock_irq(&snd_pcm_link_rwlock);
1518         return res;
1519 }
1520 
1521 static int snd_pcm_hw_rule_mul(snd_pcm_hw_params_t *params,
1522                                snd_pcm_hw_rule_t *rule)
1523 {
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);
1528 }
1529 
1530 static int snd_pcm_hw_rule_div(snd_pcm_hw_params_t *params,
1531                                snd_pcm_hw_rule_t *rule)
1532 {
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);
1537 }
1538 
1539 static int snd_pcm_hw_rule_muldivk(snd_pcm_hw_params_t *params,
1540                                    snd_pcm_hw_rule_t *rule)
1541 {
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);
1547 }
1548 
1549 static int snd_pcm_hw_rule_mulkdiv(snd_pcm_hw_params_t *params,
1550                                    snd_pcm_hw_rule_t *rule)
1551 {
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);
1557 }
1558 
1559 static int snd_pcm_hw_rule_format(snd_pcm_hw_params_t *params,
1560                                   snd_pcm_hw_rule_t *rule)
1561 {
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);
1576         }
1577         return snd_mask_refine(mask, &m);
1578 }
1579 
1580 static int snd_pcm_hw_rule_sample_bits(snd_pcm_hw_params_t *params,
1581                                        snd_pcm_hw_rule_t *rule)
1582 {
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;
1600         }
1601         t.integer = 1;
1602         return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1603 }
1604 
1605 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1606 #error "Change this table"
1607 #endif
1608 
1609 static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1610                                  48000, 64000, 88200, 96000, 176400, 192000 };
1611 
1612 #define RATES (sizeof(rates) / sizeof(rates[0]))
1613 
1614 static int snd_pcm_hw_rule_rate(snd_pcm_hw_params_t *params,
1615                                 snd_pcm_hw_rule_t *rule)
1616 {
1617         snd_pcm_hardware_t *hw = rule->private;
1618         return snd_interval_list(hw_param_interval(params, rule->var), RATES, rates, hw->rates);
1619 }               
1620 
1621 static int snd_pcm_hw_rule_buffer_bytes_max(snd_pcm_hw_params_t *params,
1622                                             snd_pcm_hw_rule_t *rule)
1623 {
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);
1632 }               
1633 
1634 int snd_pcm_hw_constraints_init(snd_pcm_substream_t *substream)
1635 {
1636         snd_pcm_runtime_t *runtime = substream->runtime;
1637         snd_pcm_hw_constraints_t *constrs = &runtime->hw_constraints;
1638         int k, err;
1639 
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));
1642         }
1643 
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));
1646         }
1647 
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));
1653 
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;
1756 }
1757 
1758 int snd_pcm_hw_constraints_complete(snd_pcm_substream_t *substream)
1759 {
1760         snd_pcm_runtime_t *runtime = substream->runtime;
1761         snd_pcm_hardware_t *hw = &runtime->hw;
1762         int err;
1763         unsigned int mask = 0;
1764 
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;
1776         }
1777         err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
1778         snd_assert(err >= 0, return -EINVAL);
1779 
1780         err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
1781         snd_assert(err >= 0, return -EINVAL);
1782 
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);
1785 
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);
1789 
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);
1793 
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);
1797 
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);
1801 
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);
1805 
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;
1811 
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);
1816         }
1817 
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;
1824         }
1825 
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);
1830 
1831         return 0;
1832 }
1833 
1834 static void snd_pcm_add_file(snd_pcm_str_t *str,
1835                              snd_pcm_file_t *pcm_file)
1836 {
1837         pcm_file->next = str->files;
1838         str->files = pcm_file;
1839 }
1840 
1841 static void snd_pcm_remove_file(snd_pcm_str_t *str,
1842                                 snd_pcm_file_t *pcm_file)
1843 {
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;
1853         }
1854 }
1855 
1856 static int snd_pcm_release_file(snd_pcm_file_t * pcm_file)
1857 {
1858         snd_pcm_substream_t *substream;
1859         snd_pcm_runtime_t *runtime;
1860         snd_pcm_str_t * str;
1861 
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;
1873         }
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;
1879 }
1880 
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)
1885 {
1886         int err = 0;
1887         snd_pcm_file_t *pcm_file;
1888         snd_pcm_substream_t *substream;
1889         snd_pcm_str_t *str;
1890 
1891         snd_assert(rpcm_file != NULL, return -EINVAL);
1892         *rpcm_file = NULL;
1893 
1894         pcm_file = snd_magic_kcalloc(snd_pcm_file_t, 0, GFP_KERNEL);
1895         if (pcm_file == NULL) {
1896                 return -ENOMEM;
1897         }
1898 
1899         if ((err = snd_pcm_open_substream(pcm, stream, &substream)) < 0) {
1900                 snd_magic_kfree(pcm_file);
1901                 return err;
1902         }
1903 
1904         str = substream->pstr;
1905         substream->file = pcm_file;
1906 
1907         pcm_file->substream = substream;
1908 
1909         snd_pcm_add_file(str, pcm_file);
1910 
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;
1916         }
1917 
1918         if ((err = substream->ops->open(substream)) < 0) {
1919                 snd_pcm_release_file(pcm_file);
1920                 return err;
1921         }
1922         substream->open_flag = 1;
1923 
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;
1930         }
1931 
1932         substream->ffile = file;
1933 
1934         file->private_data = pcm_file;
1935         *rpcm_file = pcm_file;
1936         return 0;
1937 }
1938 
1939 int snd_pcm_open(struct inode *inode, struct file *file)
1940 {
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;
1947 
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;
1953         }
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;
1960         }
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;
1972                         }
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;
1982                 }
1983         }
1984         remove_wait_queue(&pcm->open_wait, &wait);
1985         up(&pcm->open_mutex);
1986         if (err < 0)
1987                 goto __error;
1988         return err;
1989 
1990       __error:
1991         module_put(pcm->card->module);
1992       __error2:
1993         snd_card_file_remove(pcm->card, file);
1994       __error1:
1995         return err;
1996 }
1997 
1998 int snd_pcm_release(struct inode *inode, struct file *file)
1999 {
2000         snd_pcm_t *pcm;
2001         snd_pcm_substream_t *substream;
2002         snd_pcm_file_t *pcm_file;
2003 
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;
2021 }
2022 
2023 snd_pcm_sframes_t snd_pcm_playback_rewind(snd_pcm_substream_t *substream, snd_pcm_uframes_t frames)
2024 {
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;
2029 
2030         if (frames == 0)
2031                 return 0;
2032 
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;
2048         }
2049 
2050         hw_avail = snd_pcm_playback_hw_avail(runtime);
2051         if (hw_avail <= 0) {
2052                 ret = 0;
2053                 goto __end;
2054         }
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;
2070 }
2071 
2072 snd_pcm_sframes_t snd_pcm_capture_rewind(snd_pcm_substream_t *substream, snd_pcm_uframes_t frames)
2073 {
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;
2078 
2079         if (frames == 0)
2080                 return 0;
2081 
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;
2097         }
2098 
2099         hw_avail = snd_pcm_capture_hw_avail(runtime);
2100         if (hw_avail <= 0) {
2101                 ret = 0;
2102                 goto __end;
2103         }
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;
2119 }
2120 
2121 snd_pcm_sframes_t snd_pcm_playback_forward(snd_pcm_substream_t *substream, snd_pcm_uframes_t frames)
2122 {
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;
2127 
2128         if (frames == 0)
2129                 return 0;
2130 
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;
2147         }
2148 
2149         avail = snd_pcm_playback_avail(runtime);
2150         if (avail <= 0) {
2151                 ret = 0;
2152                 goto __end;
2153         }
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;
2169 }
2170 
2171 snd_pcm_sframes_t snd_pcm_capture_forward(snd_pcm_substream_t *substream, snd_pcm_uframes_t frames)
2172 {
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;
2177 
2178         if (frames == 0)
2179                 return 0;
2180 
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;
2197         }
2198 
2199         avail = snd_pcm_capture_avail(runtime);
2200         if (avail <= 0) {
2201                 ret = 0;
2202                 goto __end;
2203         }
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;
2219 }
2220 
2221 static int snd_pcm_hwsync(snd_pcm_substream_t *substream)
2222 {
2223         snd_pcm_runtime_t *runtime = substream->runtime;
2224         int err;
2225 
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;
2246         }
2247         snd_pcm_stream_unlock_irq(substream);
2248         return err;
2249 }
2250                 
2251 static int snd_pcm_delay(snd_pcm_substream_t *substream, snd_pcm_sframes_t *res)
2252 {
2253         snd_pcm_runtime_t *runtime = substream->runtime;
2254         int err;
2255         snd_pcm_sframes_t n = 0;
2256 
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;
2281         }
2282         snd_pcm_stream_unlock_irq(substream);
2283         if (!err)
2284                 if (put_user(n, res))
2285                         err = -EFAULT;
2286         return err;
2287 }
2288                 
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);
2293 
2294 static int snd_pcm_common_ioctl1(snd_pcm_substream_t *substream,
2295                                  unsigned int cmd, void *arg)
2296 {
2297         snd_assert(substream != NULL, return -ENXIO);
2298 
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:
2305         {
2306                 int xarg;
2307                 if (get_user(xarg, (int *) arg))
2308                         return -EFAULT;
2309                 substream->runtime->tstamp_timespec = xarg ? 1 : 0;
2310                 return 0;
2311         }
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);
2346         }
2347         snd_printd("unknown ioctl = 0x%x\n", cmd);
2348         return -ENOTTY;
2349 }
2350 
2351 static int snd_pcm_playback_ioctl1(snd_pcm_substream_t *substream,
2352                                    unsigned int cmd, void *arg)
2353 {
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:
2358         {
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;
2371         }
2372         case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2373         {
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;
2392                 }
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;
2397         }
2398         case SNDRV_PCM_IOCTL_REWIND:
2399         {
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;
2409         }
2410         case SNDRV_PCM_IOCTL_FORWARD:
2411         {
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;
2421         }
2422         case SNDRV_PCM_IOCTL_PAUSE:
2423         {
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;
2429         }
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);
2434         }
2435         return snd_pcm_common_ioctl1(substream, cmd, arg);
2436 }
2437 
2438 static int snd_pcm_capture_ioctl1(snd_pcm_substream_t *substream,
2439                                   unsigned int cmd, void *arg)
2440 {
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:
2445         {
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;
2458         }
2459         case SNDRV_PCM_IOCTL_READN_FRAMES:
2460         {
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;
2479                 }
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;
2484         }
2485         case SNDRV_PCM_IOCTL_REWIND:
2486         {
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;
2496         }
2497         case SNDRV_PCM_IOCTL_FORWARD:
2498         {
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;
2508         }
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);
2513         }
2514         return snd_pcm_common_ioctl1(substream, cmd, arg);
2515 }
2516 
2517 static int snd_pcm_playback_ioctl(struct inode *inode, struct file *file,
2518                                   unsigned int cmd, unsigned long arg)
2519 {
2520         snd_pcm_file_t *pcm_file;
2521 
2522         pcm_file = snd_magic_cast(snd_pcm_file_t, file->private_data, return -ENXIO);
2523 
2524         if (((cmd >> 8) & 0xff) != 'A')
2525                 return -ENOTTY;
2526 
2527         return snd_pcm_playback_ioctl1(pcm_file->substream, cmd, (void *) arg);
2528 }
2529 
2530 static int snd_pcm_capture_ioctl(struct inode *inode, struct file *file,
2531                                  unsigned int cmd, unsigned long arg)
2532 {
2533         snd_pcm_file_t *pcm_file;
2534 
2535         pcm_file = snd_magic_cast(snd_pcm_file_t, file->private_data, return -ENXIO);
2536 
2537         if (((cmd >> 8) & 0xff) != 'A')
2538                 return -ENOTTY;
2539 
2540         return snd_pcm_capture_ioctl1(pcm_file->substream, cmd, (void *) arg);
2541 }
2542 
2543 int snd_pcm_kernel_playback_ioctl(snd_pcm_substream_t *substream,
2544                                   unsigned int cmd, void *arg)
2545 {
2546         mm_segment_t fs;
2547         int result;
2548         
2549         fs = snd_enter_user();
2550         result = snd_pcm_playback_ioctl1(substream, cmd, arg);
2551         snd_leave_user(fs);
2552         return result;
2553 }
2554 
2555 int snd_pcm_kernel_capture_ioctl(snd_pcm_substream_t *substream,
2556                                  unsigned int cmd, void *arg)
2557 {
2558         mm_segment_t fs;
2559         int result;
2560         
2561         fs = snd_enter_user();
2562         result = snd_pcm_capture_ioctl1(substream, cmd, arg);
2563         snd_leave_user(fs);
2564         return result;
2565 }
2566 
2567 int snd_pcm_kernel_ioctl(snd_pcm_substream_t *substream,
2568                          unsigned int cmd, void *arg)
2569 {
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;
2577         }
2578 }
2579 
2580 static ssize_t snd_pcm_read(struct file *file, char *buf, size_t count, loff_t * offset)
2581 {
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;
2586 
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;
2600 }
2601 
2602 static ssize_t snd_pcm_write(struct file *file, const char *buf, size_t count, loff_t * offset)
2603 {
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;
2608 
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;
2616         }
2617         if (!frame_aligned(runtime, count)) {
2618                 result = -EINVAL;
2619                 goto end;
2620         }
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;
2627 }
2628 
2629 static ssize_t snd_pcm_readv(struct file *file, const struct iovec *_vector,
2630                              unsigned long count, loff_t * offset)
2631 
2632 {
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;
2640 
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;
2662 }
2663 
2664 static ssize_t snd_pcm_writev(struct file *file, const struct iovec *_vector,
2665                               unsigned long count, loff_t * offset)
2666 {
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;
2674 
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;
2682         }
2683         if (count > 128 || count != runtime->channels ||
2684             !frame_aligned(runtime, _vector->iov_len)) {
2685                 result = -EINVAL;
2686                 goto end;
2687         }
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;
2700 }
2701 
2702 unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
2703 {
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;
2709 
2710         pcm_file = snd_magic_cast(snd_pcm_file_t, file->private_data, return 0);
2711 
2712         substream = pcm_file->substream;
2713         snd_assert(substream != NULL, return -ENXIO);
2714         runtime = substream->runtime;
2715 
2716         poll_wait(file, &runtime->sleep, wait);
2717 
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;
2727                 }
2728                 /* Fall through */
2729         case SNDRV_PCM_STATE_DRAINING:
2730                 mask = 0;
2731                 break;
2732         default:
2733                 mask = POLLOUT | POLLWRNORM | POLLERR;
2734                 break;
2735         }
2736         snd_pcm_stream_unlock_irq(substream);
2737         return mask;
2738 }
2739 
2740 unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
2741 {
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;
2747 
2748         pcm_file = snd_magic_cast(snd_pcm_file_t, file->private_data, return 0);
2749 
2750         substream = pcm_file->substream;
2751         snd_assert(substream != NULL, return -ENXIO);
2752         runtime = substream->runtime;
2753 
2754         poll_wait(file, &runtime->sleep, wait);
2755 
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;
2765                 }
2766                 mask = 0;
2767                 break;
2768         case SNDRV_PCM_STATE_DRAINING:
2769                 if (avail > 0) {
2770                         mask = POLLIN | POLLRDNORM;
2771                         break;
2772                 }
2773                 /* Fall through */
2774         default:
2775                 mask = POLLIN | POLLRDNORM | POLLERR;
2776                 break;
2777         }
2778         snd_pcm_stream_unlock_irq(substream);
2779         return mask;
2780 }
2781 
2782 static struct page * snd_pcm_mmap_status_nopage(struct vm_area_struct *area, unsigned long address, int no_share)
2783 {
2784         snd_pcm_substream_t *substream = (snd_pcm_substream_t *)area->vm_private_data;
2785         snd_pcm_runtime_t *runtime;
2786         struct page * page;
2787         
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         return page;
2795 }
2796 
2797 static struct vm_operations_struct snd_pcm_vm_ops_status =
2798 {
2799         .nopage =       snd_pcm_mmap_status_nopage,
2800 };
2801 
2802 int snd_pcm_mmap_status(snd_pcm_substream_t *substream, struct file *file,
2803                         struct vm_area_struct *area)
2804 {
2805         snd_pcm_runtime_t *runtime;
2806         long size;
2807         if (!(area->vm_flags & VM_READ))
2808                 return -EINVAL;
2809         runtime = substream->runtime;
2810         snd_assert(runtime != NULL, return -EAGAIN);
2811         size = area->vm_end - area->vm_start;
2812         if (size != PAGE_ALIGN(sizeof(snd_pcm_mmap_status_t)))
2813                 return -EINVAL;
2814         area->vm_ops = &snd_pcm_vm_ops_status;
2815         area->vm_private_data = substream;
2816         area->vm_flags |= VM_RESERVED;
2817         return 0;
2818 }
2819 
2820 static struct page * snd_pcm_mmap_control_nopage(struct vm_area_struct *area, unsigned long address, int no_share)
2821 {
2822         snd_pcm_substream_t *substream = (snd_pcm_substream_t *)area->vm_private_data;
2823         snd_pcm_runtime_t *runtime;
2824         struct page * page;
2825         
2826         if (substream == NULL)
2827                 return NOPAGE_OOM;
2828         runtime = substream->runtime;
2829         page = virt_to_page(runtime->control);
2830         if (!PageReserved(page))
2831                 get_page(page);
2832         return page;
2833 }
2834 
2835 static struct vm_operations_struct snd_pcm_vm_ops_control =
2836 {
2837         .nopage =       snd_pcm_mmap_control_nopage,
2838 };
2839 
2840 static int snd_pcm_mmap_control(snd_pcm_substream_t *substream, struct file *file,
2841                                 struct vm_area_struct *area)
2842 {
2843         snd_pcm_runtime_t *runtime;
2844         long size;
2845         if (!(area->vm_flags & VM_READ))
2846                 return -EINVAL;
2847         runtime = substream->runtime;
2848         snd_assert(runtime != NULL, return -EAGAIN);
2849         size = area->vm_end - area->vm_start;
2850         if (size != PAGE_ALIGN(sizeof(snd_pcm_mmap_control_t)))
2851                 return -EINVAL;
2852         area->vm_ops = &snd_pcm_vm_ops_control;
2853         area->vm_private_data = substream;
2854         area->vm_flags |= VM_RESERVED;
2855         return 0;
2856 }
2857 
2858 static void snd_pcm_mmap_data_open(struct vm_area_struct *area)
2859 {
2860         snd_pcm_substream_t *substream = (snd_pcm_substream_t *)area->vm_private_data;
2861         atomic_inc(&substream->runtime->mmap_count);
2862 }
2863 
2864 static void snd_pcm_mmap_data_close(struct vm_area_struct *area)
2865 {
2866         snd_pcm_substream_t *substream = (snd_pcm_substream_t *)area->vm_private_data;
2867         atomic_dec(&substream->runtime->mmap_count);
2868 }
2869 
2870 static struct page * snd_pcm_mmap_data_nopage(struct vm_area_struct *area, unsigned long address, int no_share)
2871 {
2872         snd_pcm_substream_t *substream = (snd_pcm_substream_t *)area->vm_private_data;
2873         snd_pcm_runtime_t *runtime;
2874         unsigned long offset;
2875         struct page * page;
2876         void *vaddr;
2877         size_t dma_bytes;
2878         
2879         if (substream == NULL)
2880                 return NOPAGE_OOM;
2881         runtime = substream->runtime;
2882         offset = area->vm_pgoff << PAGE_SHIFT;
2883         offset += address - area->vm_start;
2884         snd_assert((offset % PAGE_SIZE) == 0, return NOPAGE_OOM);
2885         dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
2886         if (offset > dma_bytes - PAGE_SIZE)
2887                 return NOPAGE_SIGBUS;
2888         if (substream->ops->page) {
2889                 page = substream->ops->page(substream, offset);
2890                 if (! page)
2891                         return NOPAGE_OOM;
2892         } else {
2893                 vaddr = runtime->dma_area + offset;
2894                 page = virt_to_page(vaddr);
2895         }
2896         if (!PageReserved(page))
2897                 get_page(page);
2898         return page;
2899 }
2900 
2901 static struct vm_operations_struct snd_pcm_vm_ops_data =
2902 {
2903         .open =         snd_pcm_mmap_data_open,
2904         .close =        snd_pcm_mmap_data_close,
2905         .nopage =       snd_pcm_mmap_data_nopage,
2906 };
2907 
2908 int snd_pcm_mmap_data(snd_pcm_substream_t *substream, struct file *file,
2909                       struct vm_area_struct *area)
2910 {
2911         snd_pcm_runtime_t *runtime;
2912         long size;
2913         unsigned long offset;
2914         size_t dma_bytes;
2915 
2916         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
2917                 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
2918                         return -EINVAL;
2919         } else {
2920                 if (!(area->vm_flags & VM_READ))
2921                         return -EINVAL;
2922         }
2923         runtime = substream->runtime;
2924         snd_assert(runtime != NULL, return -EAGAIN);
2925         if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2926                 return -EBADFD;
2927         if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
2928                 return -ENXIO;
2929         if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
2930             runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2931                 return -EINVAL;
2932         size = area->vm_end - area->vm_start;
2933         offset = area->vm_pgoff << PAGE_SHIFT;
2934         dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
2935         if ((size_t)size > dma_bytes)
2936                 return -EINVAL;
2937         if (offset > dma_bytes - size)
2938                 return -EINVAL;
2939 
2940         area->vm_ops = &snd_pcm_vm_ops_data;
2941         area->vm_private_data = substream;
2942         area->vm_flags |= VM_RESERVED;
2943         atomic_inc(&runtime->mmap_count);
2944         return 0;
2945 }
2946 
2947 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
2948 {
2949         snd_pcm_file_t * pcm_file;
2950         snd_pcm_substream_t *substream; 
2951         unsigned long offset;
2952         
2953         pcm_file = snd_magic_cast(snd_pcm_file_t, file->private_data, return -ENXIO);
2954         substream = pcm_file->substream;
2955         snd_assert(substream != NULL, return -ENXIO);
2956 
2957         offset = area->vm_pgoff << PAGE_SHIFT;
2958         switch (offset) {
2959         case SNDRV_PCM_MMAP_OFFSET_STATUS:
2960                 return snd_pcm_mmap_status(substream, file, area);
2961         case SNDRV_PCM_MMAP_OFFSET_CONTROL:
2962                 return snd_pcm_mmap_control(substream, file, area);
2963         default:
2964                 return snd_pcm_mmap_data(substream, file, area);
2965         }
2966         return 0;
2967 }
2968 
2969 static int snd_pcm_fasync(int fd, struct file * file, int on)
2970 {
2971         snd_pcm_file_t * pcm_file;
2972         snd_pcm_substream_t *substream;
2973         snd_pcm_runtime_t *runtime;
2974         int err;
2975 
2976         pcm_file = snd_magic_cast(snd_pcm_file_t, file->private_data, return -ENXIO);
2977         substream = pcm_file->substream;
2978         snd_assert(substream != NULL, return -ENXIO);
2979         runtime = substream->runtime;
2980 
2981         err = fasync_helper(fd, file, on, &runtime->fasync);
2982         if (err < 0)
2983                 return err;
2984         return 0;
2985 }
2986 
2987 /*
2988  *  To be removed helpers to keep binary compatibility
2989  */
2990 
2991 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
2992 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
2993 
2994 static void snd_pcm_hw_convert_from_old_params(snd_pcm_hw_params_t *params, struct sndrv_pcm_hw_params_old *oparams)
2995 {
2996         unsigned int i;
2997 
2998         memset(params, 0, sizeof(*params));
2999         params->flags = oparams->flags;
3000         for (i = 0; i < sizeof(oparams->masks) / sizeof(unsigned int); i++)
3001                 params->masks[i].bits[0] = oparams->masks[i];
3002         memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3003         params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3004         params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3005         params->info = oparams->info;
3006         params->msbits = oparams->msbits;
3007         params->rate_num = oparams->rate_num;
3008         params->rate_den = oparams->rate_den;
3009         params->fifo_size = oparams->fifo_size;
3010 }
3011 
3012 static void snd_pcm_hw_convert_to_old_params(struct sndrv_pcm_hw_params_old *oparams, snd_pcm_hw_params_t *params)
3013 {
3014         unsigned int i;
3015 
3016         memset(oparams, 0, sizeof(*oparams));
3017         oparams->flags = params->flags;
3018         for (i = 0; i < sizeof(oparams->masks) / sizeof(unsigned int); i++)
3019                 oparams->masks[i] = params->masks[i].bits[0];
3020         memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3021         oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3022         oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3023         oparams->info = params->info;
3024         oparams->msbits = params->msbits;
3025         oparams->rate_num = params->rate_num;
3026         oparams->rate_den = params->rate_den;
3027         oparams->fifo_size = params->fifo_size;
3028 }
3029 
3030 static int snd_pcm_hw_refine_old_user(snd_pcm_substream_t * substream, struct sndrv_pcm_hw_params_old * _oparams)
3031 {
3032         snd_pcm_hw_params_t params;
3033         struct sndrv_pcm_hw_params_old oparams;
3034         int err;
3035         if (copy_from_user(&oparams, _oparams, sizeof(oparams)))
3036                 return -EFAULT;
3037         snd_pcm_hw_convert_from_old_params(&params, &oparams);
3038         err = snd_pcm_hw_refine(substream, &params);
3039         snd_pcm_hw_convert_to_old_params(&oparams, &params);
3040         if (copy_to_user(_oparams, &oparams, sizeof(oparams)))
3041                 return -EFAULT;
3042         return err;
3043 }
3044 
3045 static int snd_pcm_hw_params_old_user(snd_pcm_substream_t * substream, struct sndrv_pcm_hw_params_old * _oparams)
3046 {
3047         snd_pcm_hw_params_t params;
3048         struct sndrv_pcm_hw_params_old oparams;
3049         int err;
3050         if (copy_from_user(&oparams, _oparams, sizeof(oparams)))
3051                 return -EFAULT;
3052         snd_pcm_hw_convert_from_old_params(&params, &oparams);
3053         err = snd_pcm_hw_params(substream, &params);
3054         snd_pcm_hw_convert_to_old_params(&oparams, &params);
3055         if (copy_to_user(_oparams, &oparams, sizeof(oparams)))
3056                 return -EFAULT;
3057         return err;
3058 }
3059 
3060 /*
3061  *  Register section
3062  */
3063 
3064 static struct file_operations snd_pcm_f_ops_playback = {
3065         .owner =        THIS_MODULE,
3066         .write =        snd_pcm_write,
3067         .writev =       snd_pcm_writev,
3068         .open =         snd_pcm_open,
3069         .release =      snd_pcm_release,
3070         .poll =         snd_pcm_playback_poll,
3071         .ioctl =        snd_pcm_playback_ioctl,
3072         .mmap =         snd_pcm_mmap,
3073         .fasync =       snd_pcm_fasync,
3074 };
3075 
3076 static struct file_operations snd_pcm_f_ops_capture = {
3077         .owner =        THIS_MODULE,
3078         .read =         snd_pcm_read,
3079         .readv =        snd_pcm_readv,
3080         .open =         snd_pcm_open,
3081         .release =      snd_pcm_release,
3082         .poll =         snd_pcm_capture_poll,
3083         .ioctl =        snd_pcm_capture_ioctl,
3084         .mmap =         snd_pcm_mmap,
3085         .fasync =       snd_pcm_fasync,
3086 };
3087 
3088 snd_minor_t snd_pcm_reg[2] =
3089 {
3090         {
3091                 .comment =      "digital audio playback",
3092                 .f_ops =        &snd_pcm_f_ops_playback,
3093         },
3094         {
3095                 .comment =      "digital audio capture",
3096                 .f_ops =        &snd_pcm_f_ops_capture,
3097         }
3098 };
3099 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp