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

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

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

~ [ 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