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