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

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

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

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