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