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