1b21c60a4SVinod Koul /* 2b21c60a4SVinod Koul * compress_core.c - compress offload core 3b21c60a4SVinod Koul * 4b21c60a4SVinod Koul * Copyright (C) 2011 Intel Corporation 5b21c60a4SVinod Koul * Authors: Vinod Koul <vinod.koul@linux.intel.com> 6b21c60a4SVinod Koul * Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com> 7b21c60a4SVinod Koul * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 8b21c60a4SVinod Koul * 9b21c60a4SVinod Koul * This program is free software; you can redistribute it and/or modify 10b21c60a4SVinod Koul * it under the terms of the GNU General Public License as published by 11b21c60a4SVinod Koul * the Free Software Foundation; version 2 of the License. 12b21c60a4SVinod Koul * 13b21c60a4SVinod Koul * This program is distributed in the hope that it will be useful, but 14b21c60a4SVinod Koul * WITHOUT ANY WARRANTY; without even the implied warranty of 15b21c60a4SVinod Koul * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16b21c60a4SVinod Koul * General Public License for more details. 17b21c60a4SVinod Koul * 18b21c60a4SVinod Koul * You should have received a copy of the GNU General Public License along 19b21c60a4SVinod Koul * with this program; if not, write to the Free Software Foundation, Inc., 20b21c60a4SVinod Koul * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. 21b21c60a4SVinod Koul * 22b21c60a4SVinod Koul * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 23b21c60a4SVinod Koul * 24b21c60a4SVinod Koul */ 25b21c60a4SVinod Koul #define FORMAT(fmt) "%s: %d: " fmt, __func__, __LINE__ 26b21c60a4SVinod Koul #define pr_fmt(fmt) KBUILD_MODNAME ": " FORMAT(fmt) 27b21c60a4SVinod Koul 28b21c60a4SVinod Koul #include <linux/file.h> 29b21c60a4SVinod Koul #include <linux/fs.h> 30b21c60a4SVinod Koul #include <linux/list.h> 31f0283b58SCharles Keepax #include <linux/math64.h> 32b21c60a4SVinod Koul #include <linux/mm.h> 33b21c60a4SVinod Koul #include <linux/mutex.h> 34b21c60a4SVinod Koul #include <linux/poll.h> 35b21c60a4SVinod Koul #include <linux/slab.h> 36b21c60a4SVinod Koul #include <linux/sched.h> 37f0283b58SCharles Keepax #include <linux/types.h> 38b21c60a4SVinod Koul #include <linux/uio.h> 39b21c60a4SVinod Koul #include <linux/uaccess.h> 40b21c60a4SVinod Koul #include <linux/module.h> 41b21c60a4SVinod Koul #include <sound/core.h> 42b21c60a4SVinod Koul #include <sound/initval.h> 43b21c60a4SVinod Koul #include <sound/compress_params.h> 44b21c60a4SVinod Koul #include <sound/compress_offload.h> 45b21c60a4SVinod Koul #include <sound/compress_driver.h> 46b21c60a4SVinod Koul 47b21c60a4SVinod Koul /* TODO: 48b21c60a4SVinod Koul * - add substream support for multiple devices in case of 49b21c60a4SVinod Koul * SND_DYNAMIC_MINORS is not used 50b21c60a4SVinod Koul * - Multiple node representation 51b21c60a4SVinod Koul * driver should be able to register multiple nodes 52b21c60a4SVinod Koul */ 53b21c60a4SVinod Koul 54b21c60a4SVinod Koul static DEFINE_MUTEX(device_mutex); 55b21c60a4SVinod Koul 56b21c60a4SVinod Koul struct snd_compr_file { 57b21c60a4SVinod Koul unsigned long caps; 58b21c60a4SVinod Koul struct snd_compr_stream stream; 59b21c60a4SVinod Koul }; 60b21c60a4SVinod Koul 61b21c60a4SVinod Koul /* 62b21c60a4SVinod Koul * a note on stream states used: 63b21c60a4SVinod Koul * we use follwing states in the compressed core 64b21c60a4SVinod Koul * SNDRV_PCM_STATE_OPEN: When stream has been opened. 65b21c60a4SVinod Koul * SNDRV_PCM_STATE_SETUP: When stream has been initialized. This is done by 66b21c60a4SVinod Koul * calling SNDRV_COMPRESS_SET_PARAMS. running streams will come to this 67b21c60a4SVinod Koul * state at stop by calling SNDRV_COMPRESS_STOP, or at end of drain. 68b21c60a4SVinod Koul * SNDRV_PCM_STATE_RUNNING: When stream has been started and is 69b21c60a4SVinod Koul * decoding/encoding and rendering/capturing data. 70b21c60a4SVinod Koul * SNDRV_PCM_STATE_DRAINING: When stream is draining current data. This is done 71b21c60a4SVinod Koul * by calling SNDRV_COMPRESS_DRAIN. 72b21c60a4SVinod Koul * SNDRV_PCM_STATE_PAUSED: When stream is paused. This is done by calling 73b21c60a4SVinod Koul * SNDRV_COMPRESS_PAUSE. It can be stopped or resumed by calling 74b21c60a4SVinod Koul * SNDRV_COMPRESS_STOP or SNDRV_COMPRESS_RESUME respectively. 75b21c60a4SVinod Koul */ 76b21c60a4SVinod Koul static int snd_compr_open(struct inode *inode, struct file *f) 77b21c60a4SVinod Koul { 78b21c60a4SVinod Koul struct snd_compr *compr; 79b21c60a4SVinod Koul struct snd_compr_file *data; 80b21c60a4SVinod Koul struct snd_compr_runtime *runtime; 81b21c60a4SVinod Koul enum snd_compr_direction dirn; 82b21c60a4SVinod Koul int maj = imajor(inode); 83b21c60a4SVinod Koul int ret; 84b21c60a4SVinod Koul 8581cb3246SDan Carpenter if ((f->f_flags & O_ACCMODE) == O_WRONLY) 86b21c60a4SVinod Koul dirn = SND_COMPRESS_PLAYBACK; 8781cb3246SDan Carpenter else if ((f->f_flags & O_ACCMODE) == O_RDONLY) 88b21c60a4SVinod Koul dirn = SND_COMPRESS_CAPTURE; 8981cb3246SDan Carpenter else 90b21c60a4SVinod Koul return -EINVAL; 91b21c60a4SVinod Koul 92b21c60a4SVinod Koul if (maj == snd_major) 93b21c60a4SVinod Koul compr = snd_lookup_minor_data(iminor(inode), 94b21c60a4SVinod Koul SNDRV_DEVICE_TYPE_COMPRESS); 95b21c60a4SVinod Koul else 96b21c60a4SVinod Koul return -EBADFD; 97b21c60a4SVinod Koul 98b21c60a4SVinod Koul if (compr == NULL) { 99b21c60a4SVinod Koul pr_err("no device data!!!\n"); 100b21c60a4SVinod Koul return -ENODEV; 101b21c60a4SVinod Koul } 102b21c60a4SVinod Koul 103b21c60a4SVinod Koul if (dirn != compr->direction) { 104b21c60a4SVinod Koul pr_err("this device doesn't support this direction\n"); 105a0830dbdSTakashi Iwai snd_card_unref(compr->card); 106b21c60a4SVinod Koul return -EINVAL; 107b21c60a4SVinod Koul } 108b21c60a4SVinod Koul 109b21c60a4SVinod Koul data = kzalloc(sizeof(*data), GFP_KERNEL); 110a0830dbdSTakashi Iwai if (!data) { 111a0830dbdSTakashi Iwai snd_card_unref(compr->card); 112b21c60a4SVinod Koul return -ENOMEM; 113a0830dbdSTakashi Iwai } 114b21c60a4SVinod Koul data->stream.ops = compr->ops; 115b21c60a4SVinod Koul data->stream.direction = dirn; 116b21c60a4SVinod Koul data->stream.private_data = compr->private_data; 117b21c60a4SVinod Koul data->stream.device = compr; 118b21c60a4SVinod Koul runtime = kzalloc(sizeof(*runtime), GFP_KERNEL); 119b21c60a4SVinod Koul if (!runtime) { 120b21c60a4SVinod Koul kfree(data); 121a0830dbdSTakashi Iwai snd_card_unref(compr->card); 122b21c60a4SVinod Koul return -ENOMEM; 123b21c60a4SVinod Koul } 124b21c60a4SVinod Koul runtime->state = SNDRV_PCM_STATE_OPEN; 125b21c60a4SVinod Koul init_waitqueue_head(&runtime->sleep); 126b21c60a4SVinod Koul data->stream.runtime = runtime; 127b21c60a4SVinod Koul f->private_data = (void *)data; 128b21c60a4SVinod Koul mutex_lock(&compr->lock); 129b21c60a4SVinod Koul ret = compr->ops->open(&data->stream); 130b21c60a4SVinod Koul mutex_unlock(&compr->lock); 131b21c60a4SVinod Koul if (ret) { 132b21c60a4SVinod Koul kfree(runtime); 133b21c60a4SVinod Koul kfree(data); 134b21c60a4SVinod Koul } 135a0830dbdSTakashi Iwai snd_card_unref(compr->card); 136a0830dbdSTakashi Iwai return 0; 137b21c60a4SVinod Koul } 138b21c60a4SVinod Koul 139b21c60a4SVinod Koul static int snd_compr_free(struct inode *inode, struct file *f) 140b21c60a4SVinod Koul { 141b21c60a4SVinod Koul struct snd_compr_file *data = f->private_data; 142b21c60a4SVinod Koul data->stream.ops->free(&data->stream); 143b21c60a4SVinod Koul kfree(data->stream.runtime->buffer); 144b21c60a4SVinod Koul kfree(data->stream.runtime); 145b21c60a4SVinod Koul kfree(data); 146b21c60a4SVinod Koul return 0; 147b21c60a4SVinod Koul } 148b21c60a4SVinod Koul 14917ac8e5cSRichard Fitzgerald static int snd_compr_update_tstamp(struct snd_compr_stream *stream, 150b21c60a4SVinod Koul struct snd_compr_tstamp *tstamp) 151b21c60a4SVinod Koul { 152b21c60a4SVinod Koul if (!stream->ops->pointer) 15317ac8e5cSRichard Fitzgerald return -ENOTSUPP; 154b21c60a4SVinod Koul stream->ops->pointer(stream, tstamp); 155b21c60a4SVinod Koul pr_debug("dsp consumed till %d total %d bytes\n", 156b21c60a4SVinod Koul tstamp->byte_offset, tstamp->copied_total); 1575b1f79f7SCharles Keepax if (stream->direction == SND_COMPRESS_PLAYBACK) 158b21c60a4SVinod Koul stream->runtime->total_bytes_transferred = tstamp->copied_total; 1595b1f79f7SCharles Keepax else 1605b1f79f7SCharles Keepax stream->runtime->total_bytes_available = tstamp->copied_total; 16117ac8e5cSRichard Fitzgerald return 0; 162b21c60a4SVinod Koul } 163b21c60a4SVinod Koul 164b21c60a4SVinod Koul static size_t snd_compr_calc_avail(struct snd_compr_stream *stream, 165b21c60a4SVinod Koul struct snd_compr_avail *avail) 166b21c60a4SVinod Koul { 16717ac8e5cSRichard Fitzgerald memset(avail, 0, sizeof(*avail)); 168b21c60a4SVinod Koul snd_compr_update_tstamp(stream, &avail->tstamp); 16917ac8e5cSRichard Fitzgerald /* Still need to return avail even if tstamp can't be filled in */ 170b21c60a4SVinod Koul 171b21c60a4SVinod Koul if (stream->runtime->total_bytes_available == 0 && 1725b1f79f7SCharles Keepax stream->runtime->state == SNDRV_PCM_STATE_SETUP && 1735b1f79f7SCharles Keepax stream->direction == SND_COMPRESS_PLAYBACK) { 174b21c60a4SVinod Koul pr_debug("detected init and someone forgot to do a write\n"); 175b21c60a4SVinod Koul return stream->runtime->buffer_size; 176b21c60a4SVinod Koul } 177b21c60a4SVinod Koul pr_debug("app wrote %lld, DSP consumed %lld\n", 178b21c60a4SVinod Koul stream->runtime->total_bytes_available, 179b21c60a4SVinod Koul stream->runtime->total_bytes_transferred); 180b21c60a4SVinod Koul if (stream->runtime->total_bytes_available == 181b21c60a4SVinod Koul stream->runtime->total_bytes_transferred) { 1825b1f79f7SCharles Keepax if (stream->direction == SND_COMPRESS_PLAYBACK) { 183b21c60a4SVinod Koul pr_debug("both pointers are same, returning full avail\n"); 184b21c60a4SVinod Koul return stream->runtime->buffer_size; 1855b1f79f7SCharles Keepax } else { 1865b1f79f7SCharles Keepax pr_debug("both pointers are same, returning no avail\n"); 1875b1f79f7SCharles Keepax return 0; 1885b1f79f7SCharles Keepax } 189b21c60a4SVinod Koul } 190b21c60a4SVinod Koul 1915b1f79f7SCharles Keepax avail->avail = stream->runtime->total_bytes_available - 1925b1f79f7SCharles Keepax stream->runtime->total_bytes_transferred; 1935b1f79f7SCharles Keepax if (stream->direction == SND_COMPRESS_PLAYBACK) 1945b1f79f7SCharles Keepax avail->avail = stream->runtime->buffer_size - avail->avail; 1955b1f79f7SCharles Keepax 1964c28e32dSCharles Keepax pr_debug("ret avail as %lld\n", avail->avail); 1974c28e32dSCharles Keepax return avail->avail; 198b21c60a4SVinod Koul } 199b21c60a4SVinod Koul 200b21c60a4SVinod Koul static inline size_t snd_compr_get_avail(struct snd_compr_stream *stream) 201b21c60a4SVinod Koul { 202b21c60a4SVinod Koul struct snd_compr_avail avail; 203b21c60a4SVinod Koul 204b21c60a4SVinod Koul return snd_compr_calc_avail(stream, &avail); 205b21c60a4SVinod Koul } 206b21c60a4SVinod Koul 207b21c60a4SVinod Koul static int 208b21c60a4SVinod Koul snd_compr_ioctl_avail(struct snd_compr_stream *stream, unsigned long arg) 209b21c60a4SVinod Koul { 210b21c60a4SVinod Koul struct snd_compr_avail ioctl_avail; 211b21c60a4SVinod Koul size_t avail; 212b21c60a4SVinod Koul 213b21c60a4SVinod Koul avail = snd_compr_calc_avail(stream, &ioctl_avail); 214b21c60a4SVinod Koul ioctl_avail.avail = avail; 215b21c60a4SVinod Koul 216b21c60a4SVinod Koul if (copy_to_user((__u64 __user *)arg, 217b21c60a4SVinod Koul &ioctl_avail, sizeof(ioctl_avail))) 218b21c60a4SVinod Koul return -EFAULT; 219b21c60a4SVinod Koul return 0; 220b21c60a4SVinod Koul } 221b21c60a4SVinod Koul 222b21c60a4SVinod Koul static int snd_compr_write_data(struct snd_compr_stream *stream, 223b21c60a4SVinod Koul const char __user *buf, size_t count) 224b21c60a4SVinod Koul { 225b21c60a4SVinod Koul void *dstn; 226b21c60a4SVinod Koul size_t copy; 227b21c60a4SVinod Koul struct snd_compr_runtime *runtime = stream->runtime; 228f0283b58SCharles Keepax /* 64-bit Modulus */ 229f0283b58SCharles Keepax u64 app_pointer = div64_u64(runtime->total_bytes_available, 230f0283b58SCharles Keepax runtime->buffer_size); 231f0283b58SCharles Keepax app_pointer = runtime->total_bytes_available - 232f0283b58SCharles Keepax (app_pointer * runtime->buffer_size); 233b21c60a4SVinod Koul 234f0283b58SCharles Keepax dstn = runtime->buffer + app_pointer; 235b21c60a4SVinod Koul pr_debug("copying %ld at %lld\n", 236f0283b58SCharles Keepax (unsigned long)count, app_pointer); 237f0283b58SCharles Keepax if (count < runtime->buffer_size - app_pointer) { 238b21c60a4SVinod Koul if (copy_from_user(dstn, buf, count)) 239b21c60a4SVinod Koul return -EFAULT; 240b21c60a4SVinod Koul } else { 241f0283b58SCharles Keepax copy = runtime->buffer_size - app_pointer; 242b21c60a4SVinod Koul if (copy_from_user(dstn, buf, copy)) 243b21c60a4SVinod Koul return -EFAULT; 244b21c60a4SVinod Koul if (copy_from_user(runtime->buffer, buf + copy, count - copy)) 245b21c60a4SVinod Koul return -EFAULT; 246b21c60a4SVinod Koul } 247b21c60a4SVinod Koul /* if DSP cares, let it know data has been written */ 248b21c60a4SVinod Koul if (stream->ops->ack) 249b21c60a4SVinod Koul stream->ops->ack(stream, count); 250b21c60a4SVinod Koul return count; 251b21c60a4SVinod Koul } 252b21c60a4SVinod Koul 253b21c60a4SVinod Koul static ssize_t snd_compr_write(struct file *f, const char __user *buf, 254b21c60a4SVinod Koul size_t count, loff_t *offset) 255b21c60a4SVinod Koul { 256b21c60a4SVinod Koul struct snd_compr_file *data = f->private_data; 257b21c60a4SVinod Koul struct snd_compr_stream *stream; 258b21c60a4SVinod Koul size_t avail; 259b21c60a4SVinod Koul int retval; 260b21c60a4SVinod Koul 261b21c60a4SVinod Koul if (snd_BUG_ON(!data)) 262b21c60a4SVinod Koul return -EFAULT; 263b21c60a4SVinod Koul 264b21c60a4SVinod Koul stream = &data->stream; 265b21c60a4SVinod Koul mutex_lock(&stream->device->lock); 266b21c60a4SVinod Koul /* write is allowed when stream is running or has been steup */ 267b21c60a4SVinod Koul if (stream->runtime->state != SNDRV_PCM_STATE_SETUP && 268b21c60a4SVinod Koul stream->runtime->state != SNDRV_PCM_STATE_RUNNING) { 269b21c60a4SVinod Koul mutex_unlock(&stream->device->lock); 270b21c60a4SVinod Koul return -EBADFD; 271b21c60a4SVinod Koul } 272b21c60a4SVinod Koul 273b21c60a4SVinod Koul avail = snd_compr_get_avail(stream); 274b21c60a4SVinod Koul pr_debug("avail returned %ld\n", (unsigned long)avail); 275b21c60a4SVinod Koul /* calculate how much we can write to buffer */ 276b21c60a4SVinod Koul if (avail > count) 277b21c60a4SVinod Koul avail = count; 278b21c60a4SVinod Koul 2794daf891cSCharles Keepax if (stream->ops->copy) { 2804daf891cSCharles Keepax char __user* cbuf = (char __user*)buf; 2814daf891cSCharles Keepax retval = stream->ops->copy(stream, cbuf, avail); 2824daf891cSCharles Keepax } else { 283b21c60a4SVinod Koul retval = snd_compr_write_data(stream, buf, avail); 2844daf891cSCharles Keepax } 285b21c60a4SVinod Koul if (retval > 0) 286b21c60a4SVinod Koul stream->runtime->total_bytes_available += retval; 287b21c60a4SVinod Koul 288b21c60a4SVinod Koul /* while initiating the stream, write should be called before START 289b21c60a4SVinod Koul * call, so in setup move state */ 290b21c60a4SVinod Koul if (stream->runtime->state == SNDRV_PCM_STATE_SETUP) { 291b21c60a4SVinod Koul stream->runtime->state = SNDRV_PCM_STATE_PREPARED; 292b21c60a4SVinod Koul pr_debug("stream prepared, Houston we are good to go\n"); 293b21c60a4SVinod Koul } 294b21c60a4SVinod Koul 295b21c60a4SVinod Koul mutex_unlock(&stream->device->lock); 296b21c60a4SVinod Koul return retval; 297b21c60a4SVinod Koul } 298b21c60a4SVinod Koul 299b21c60a4SVinod Koul 300b21c60a4SVinod Koul static ssize_t snd_compr_read(struct file *f, char __user *buf, 301b21c60a4SVinod Koul size_t count, loff_t *offset) 302b21c60a4SVinod Koul { 30349bb6402SCharles Keepax struct snd_compr_file *data = f->private_data; 30449bb6402SCharles Keepax struct snd_compr_stream *stream; 30549bb6402SCharles Keepax size_t avail; 30649bb6402SCharles Keepax int retval; 30749bb6402SCharles Keepax 30849bb6402SCharles Keepax if (snd_BUG_ON(!data)) 30949bb6402SCharles Keepax return -EFAULT; 31049bb6402SCharles Keepax 31149bb6402SCharles Keepax stream = &data->stream; 31249bb6402SCharles Keepax mutex_lock(&stream->device->lock); 31349bb6402SCharles Keepax 31475481347SVinod Koul /* read is allowed when stream is running, paused, draining and setup 31575481347SVinod Koul * (yes setup is state which we transition to after stop, so if user 31675481347SVinod Koul * wants to read data after stop we allow that) 31775481347SVinod Koul */ 31875481347SVinod Koul switch (stream->runtime->state) { 31975481347SVinod Koul case SNDRV_PCM_STATE_OPEN: 32075481347SVinod Koul case SNDRV_PCM_STATE_PREPARED: 32175481347SVinod Koul case SNDRV_PCM_STATE_XRUN: 32275481347SVinod Koul case SNDRV_PCM_STATE_SUSPENDED: 32375481347SVinod Koul case SNDRV_PCM_STATE_DISCONNECTED: 32449bb6402SCharles Keepax retval = -EBADFD; 32549bb6402SCharles Keepax goto out; 32649bb6402SCharles Keepax } 32749bb6402SCharles Keepax 32849bb6402SCharles Keepax avail = snd_compr_get_avail(stream); 32949bb6402SCharles Keepax pr_debug("avail returned %ld\n", (unsigned long)avail); 33049bb6402SCharles Keepax /* calculate how much we can read from buffer */ 33149bb6402SCharles Keepax if (avail > count) 33249bb6402SCharles Keepax avail = count; 33349bb6402SCharles Keepax 33449bb6402SCharles Keepax if (stream->ops->copy) { 33549bb6402SCharles Keepax retval = stream->ops->copy(stream, buf, avail); 33649bb6402SCharles Keepax } else { 33749bb6402SCharles Keepax retval = -ENXIO; 33849bb6402SCharles Keepax goto out; 33949bb6402SCharles Keepax } 34049bb6402SCharles Keepax if (retval > 0) 34149bb6402SCharles Keepax stream->runtime->total_bytes_transferred += retval; 34249bb6402SCharles Keepax 34349bb6402SCharles Keepax out: 34449bb6402SCharles Keepax mutex_unlock(&stream->device->lock); 34549bb6402SCharles Keepax return retval; 346b21c60a4SVinod Koul } 347b21c60a4SVinod Koul 348b21c60a4SVinod Koul static int snd_compr_mmap(struct file *f, struct vm_area_struct *vma) 349b21c60a4SVinod Koul { 350b21c60a4SVinod Koul return -ENXIO; 351b21c60a4SVinod Koul } 352b21c60a4SVinod Koul 353b21c60a4SVinod Koul static inline int snd_compr_get_poll(struct snd_compr_stream *stream) 354b21c60a4SVinod Koul { 355b21c60a4SVinod Koul if (stream->direction == SND_COMPRESS_PLAYBACK) 356b21c60a4SVinod Koul return POLLOUT | POLLWRNORM; 357b21c60a4SVinod Koul else 358b21c60a4SVinod Koul return POLLIN | POLLRDNORM; 359b21c60a4SVinod Koul } 360b21c60a4SVinod Koul 361b21c60a4SVinod Koul static unsigned int snd_compr_poll(struct file *f, poll_table *wait) 362b21c60a4SVinod Koul { 363b21c60a4SVinod Koul struct snd_compr_file *data = f->private_data; 364b21c60a4SVinod Koul struct snd_compr_stream *stream; 365b21c60a4SVinod Koul size_t avail; 366b21c60a4SVinod Koul int retval = 0; 367b21c60a4SVinod Koul 368b21c60a4SVinod Koul if (snd_BUG_ON(!data)) 369b21c60a4SVinod Koul return -EFAULT; 370b21c60a4SVinod Koul stream = &data->stream; 371b21c60a4SVinod Koul if (snd_BUG_ON(!stream)) 372b21c60a4SVinod Koul return -EFAULT; 373b21c60a4SVinod Koul 374b21c60a4SVinod Koul mutex_lock(&stream->device->lock); 375b21c60a4SVinod Koul if (stream->runtime->state == SNDRV_PCM_STATE_PAUSED || 376b21c60a4SVinod Koul stream->runtime->state == SNDRV_PCM_STATE_OPEN) { 377b21c60a4SVinod Koul retval = -EBADFD; 378b21c60a4SVinod Koul goto out; 379b21c60a4SVinod Koul } 380b21c60a4SVinod Koul poll_wait(f, &stream->runtime->sleep, wait); 381b21c60a4SVinod Koul 382b21c60a4SVinod Koul avail = snd_compr_get_avail(stream); 383b21c60a4SVinod Koul pr_debug("avail is %ld\n", (unsigned long)avail); 384b21c60a4SVinod Koul /* check if we have at least one fragment to fill */ 385b21c60a4SVinod Koul switch (stream->runtime->state) { 386b21c60a4SVinod Koul case SNDRV_PCM_STATE_DRAINING: 387b21c60a4SVinod Koul /* stream has been woken up after drain is complete 388b21c60a4SVinod Koul * draining done so set stream state to stopped 389b21c60a4SVinod Koul */ 390b21c60a4SVinod Koul retval = snd_compr_get_poll(stream); 391b21c60a4SVinod Koul stream->runtime->state = SNDRV_PCM_STATE_SETUP; 392b21c60a4SVinod Koul break; 393b21c60a4SVinod Koul case SNDRV_PCM_STATE_RUNNING: 394b21c60a4SVinod Koul case SNDRV_PCM_STATE_PREPARED: 395b21c60a4SVinod Koul case SNDRV_PCM_STATE_PAUSED: 396b21c60a4SVinod Koul if (avail >= stream->runtime->fragment_size) 397b21c60a4SVinod Koul retval = snd_compr_get_poll(stream); 398b21c60a4SVinod Koul break; 399b21c60a4SVinod Koul default: 400b21c60a4SVinod Koul if (stream->direction == SND_COMPRESS_PLAYBACK) 401b21c60a4SVinod Koul retval = POLLOUT | POLLWRNORM | POLLERR; 402b21c60a4SVinod Koul else 403b21c60a4SVinod Koul retval = POLLIN | POLLRDNORM | POLLERR; 404b21c60a4SVinod Koul break; 405b21c60a4SVinod Koul } 406b21c60a4SVinod Koul out: 407b21c60a4SVinod Koul mutex_unlock(&stream->device->lock); 408b21c60a4SVinod Koul return retval; 409b21c60a4SVinod Koul } 410b21c60a4SVinod Koul 411b21c60a4SVinod Koul static int 412b21c60a4SVinod Koul snd_compr_get_caps(struct snd_compr_stream *stream, unsigned long arg) 413b21c60a4SVinod Koul { 414b21c60a4SVinod Koul int retval; 415b21c60a4SVinod Koul struct snd_compr_caps caps; 416b21c60a4SVinod Koul 417b21c60a4SVinod Koul if (!stream->ops->get_caps) 418b21c60a4SVinod Koul return -ENXIO; 419b21c60a4SVinod Koul 4201c62e9f2SDan Carpenter memset(&caps, 0, sizeof(caps)); 421b21c60a4SVinod Koul retval = stream->ops->get_caps(stream, &caps); 422b21c60a4SVinod Koul if (retval) 423b21c60a4SVinod Koul goto out; 424b21c60a4SVinod Koul if (copy_to_user((void __user *)arg, &caps, sizeof(caps))) 425b21c60a4SVinod Koul retval = -EFAULT; 426b21c60a4SVinod Koul out: 427b21c60a4SVinod Koul return retval; 428b21c60a4SVinod Koul } 429b21c60a4SVinod Koul 430b21c60a4SVinod Koul static int 431b21c60a4SVinod Koul snd_compr_get_codec_caps(struct snd_compr_stream *stream, unsigned long arg) 432b21c60a4SVinod Koul { 433b21c60a4SVinod Koul int retval; 434b21c60a4SVinod Koul struct snd_compr_codec_caps *caps; 435b21c60a4SVinod Koul 436b21c60a4SVinod Koul if (!stream->ops->get_codec_caps) 437b21c60a4SVinod Koul return -ENXIO; 438b21c60a4SVinod Koul 43947966e97STakashi Iwai caps = kzalloc(sizeof(*caps), GFP_KERNEL); 440b21c60a4SVinod Koul if (!caps) 441b21c60a4SVinod Koul return -ENOMEM; 442b21c60a4SVinod Koul 443b21c60a4SVinod Koul retval = stream->ops->get_codec_caps(stream, caps); 444b21c60a4SVinod Koul if (retval) 445b21c60a4SVinod Koul goto out; 446b21c60a4SVinod Koul if (copy_to_user((void __user *)arg, caps, sizeof(*caps))) 447b21c60a4SVinod Koul retval = -EFAULT; 448b21c60a4SVinod Koul 449b21c60a4SVinod Koul out: 450b21c60a4SVinod Koul kfree(caps); 451b21c60a4SVinod Koul return retval; 452b21c60a4SVinod Koul } 453b21c60a4SVinod Koul 454b21c60a4SVinod Koul /* revisit this with snd_pcm_preallocate_xxx */ 455b21c60a4SVinod Koul static int snd_compr_allocate_buffer(struct snd_compr_stream *stream, 456b21c60a4SVinod Koul struct snd_compr_params *params) 457b21c60a4SVinod Koul { 458b21c60a4SVinod Koul unsigned int buffer_size; 459b21c60a4SVinod Koul void *buffer; 460b21c60a4SVinod Koul 461b21c60a4SVinod Koul buffer_size = params->buffer.fragment_size * params->buffer.fragments; 462b21c60a4SVinod Koul if (stream->ops->copy) { 463b21c60a4SVinod Koul buffer = NULL; 464b21c60a4SVinod Koul /* if copy is defined the driver will be required to copy 465b21c60a4SVinod Koul * the data from core 466b21c60a4SVinod Koul */ 467b21c60a4SVinod Koul } else { 468b21c60a4SVinod Koul buffer = kmalloc(buffer_size, GFP_KERNEL); 469b21c60a4SVinod Koul if (!buffer) 470b21c60a4SVinod Koul return -ENOMEM; 471b21c60a4SVinod Koul } 472b21c60a4SVinod Koul stream->runtime->fragment_size = params->buffer.fragment_size; 473b21c60a4SVinod Koul stream->runtime->fragments = params->buffer.fragments; 474b21c60a4SVinod Koul stream->runtime->buffer = buffer; 475b21c60a4SVinod Koul stream->runtime->buffer_size = buffer_size; 476b21c60a4SVinod Koul return 0; 477b21c60a4SVinod Koul } 478b21c60a4SVinod Koul 4794dc040a0SVinod Koul static int snd_compress_check_input(struct snd_compr_params *params) 4804dc040a0SVinod Koul { 4814dc040a0SVinod Koul /* first let's check the buffer parameter's */ 4824dc040a0SVinod Koul if (params->buffer.fragment_size == 0 || 4834dc040a0SVinod Koul params->buffer.fragments > SIZE_MAX / params->buffer.fragment_size) 4844dc040a0SVinod Koul return -EINVAL; 4854dc040a0SVinod Koul 486fb4a9779SVinod Koul /* now codec parameters */ 487fb4a9779SVinod Koul if (params->codec.id == 0 || params->codec.id > SND_AUDIOCODEC_MAX) 488fb4a9779SVinod Koul return -EINVAL; 489fb4a9779SVinod Koul 490fb4a9779SVinod Koul if (params->codec.ch_in == 0 || params->codec.ch_out == 0) 491fb4a9779SVinod Koul return -EINVAL; 492fb4a9779SVinod Koul 493fb4a9779SVinod Koul if (!(params->codec.sample_rate & SNDRV_PCM_RATE_8000_192000)) 494fb4a9779SVinod Koul return -EINVAL; 495fb4a9779SVinod Koul 4964dc040a0SVinod Koul return 0; 4974dc040a0SVinod Koul } 4984dc040a0SVinod Koul 499b21c60a4SVinod Koul static int 500b21c60a4SVinod Koul snd_compr_set_params(struct snd_compr_stream *stream, unsigned long arg) 501b21c60a4SVinod Koul { 502b21c60a4SVinod Koul struct snd_compr_params *params; 503b21c60a4SVinod Koul int retval; 504b21c60a4SVinod Koul 505b21c60a4SVinod Koul if (stream->runtime->state == SNDRV_PCM_STATE_OPEN) { 506b21c60a4SVinod Koul /* 507b21c60a4SVinod Koul * we should allow parameter change only when stream has been 508b21c60a4SVinod Koul * opened not in other cases 509b21c60a4SVinod Koul */ 510b21c60a4SVinod Koul params = kmalloc(sizeof(*params), GFP_KERNEL); 511b21c60a4SVinod Koul if (!params) 512b21c60a4SVinod Koul return -ENOMEM; 513769fab2aSJesper Juhl if (copy_from_user(params, (void __user *)arg, sizeof(*params))) { 514769fab2aSJesper Juhl retval = -EFAULT; 515769fab2aSJesper Juhl goto out; 516769fab2aSJesper Juhl } 5174dc040a0SVinod Koul 5184dc040a0SVinod Koul retval = snd_compress_check_input(params); 5194dc040a0SVinod Koul if (retval) 5204dc040a0SVinod Koul goto out; 5214dc040a0SVinod Koul 522b21c60a4SVinod Koul retval = snd_compr_allocate_buffer(stream, params); 523b21c60a4SVinod Koul if (retval) { 524769fab2aSJesper Juhl retval = -ENOMEM; 525769fab2aSJesper Juhl goto out; 526b21c60a4SVinod Koul } 5274dc040a0SVinod Koul 528b21c60a4SVinod Koul retval = stream->ops->set_params(stream, params); 529b21c60a4SVinod Koul if (retval) 530b21c60a4SVinod Koul goto out; 53149bb6402SCharles Keepax 5329727b490SJeeja KP stream->metadata_set = false; 5339727b490SJeeja KP stream->next_track = false; 53449bb6402SCharles Keepax 53549bb6402SCharles Keepax if (stream->direction == SND_COMPRESS_PLAYBACK) 53649bb6402SCharles Keepax stream->runtime->state = SNDRV_PCM_STATE_SETUP; 53749bb6402SCharles Keepax else 53849bb6402SCharles Keepax stream->runtime->state = SNDRV_PCM_STATE_PREPARED; 539769fab2aSJesper Juhl } else { 540b21c60a4SVinod Koul return -EPERM; 541769fab2aSJesper Juhl } 542b21c60a4SVinod Koul out: 543b21c60a4SVinod Koul kfree(params); 544b21c60a4SVinod Koul return retval; 545b21c60a4SVinod Koul } 546b21c60a4SVinod Koul 547b21c60a4SVinod Koul static int 548b21c60a4SVinod Koul snd_compr_get_params(struct snd_compr_stream *stream, unsigned long arg) 549b21c60a4SVinod Koul { 550b21c60a4SVinod Koul struct snd_codec *params; 551b21c60a4SVinod Koul int retval; 552b21c60a4SVinod Koul 553b21c60a4SVinod Koul if (!stream->ops->get_params) 554b21c60a4SVinod Koul return -EBADFD; 555b21c60a4SVinod Koul 55647966e97STakashi Iwai params = kzalloc(sizeof(*params), GFP_KERNEL); 557b21c60a4SVinod Koul if (!params) 558b21c60a4SVinod Koul return -ENOMEM; 559b21c60a4SVinod Koul retval = stream->ops->get_params(stream, params); 560b21c60a4SVinod Koul if (retval) 561b21c60a4SVinod Koul goto out; 562b21c60a4SVinod Koul if (copy_to_user((char __user *)arg, params, sizeof(*params))) 563b21c60a4SVinod Koul retval = -EFAULT; 564b21c60a4SVinod Koul 565b21c60a4SVinod Koul out: 566b21c60a4SVinod Koul kfree(params); 567b21c60a4SVinod Koul return retval; 568b21c60a4SVinod Koul } 569b21c60a4SVinod Koul 5709727b490SJeeja KP static int 5719727b490SJeeja KP snd_compr_get_metadata(struct snd_compr_stream *stream, unsigned long arg) 5729727b490SJeeja KP { 5739727b490SJeeja KP struct snd_compr_metadata metadata; 5749727b490SJeeja KP int retval; 5759727b490SJeeja KP 5769727b490SJeeja KP if (!stream->ops->get_metadata) 5779727b490SJeeja KP return -ENXIO; 5789727b490SJeeja KP 5799727b490SJeeja KP if (copy_from_user(&metadata, (void __user *)arg, sizeof(metadata))) 5809727b490SJeeja KP return -EFAULT; 5819727b490SJeeja KP 5829727b490SJeeja KP retval = stream->ops->get_metadata(stream, &metadata); 5839727b490SJeeja KP if (retval != 0) 5849727b490SJeeja KP return retval; 5859727b490SJeeja KP 5869727b490SJeeja KP if (copy_to_user((void __user *)arg, &metadata, sizeof(metadata))) 5879727b490SJeeja KP return -EFAULT; 5889727b490SJeeja KP 5899727b490SJeeja KP return 0; 5909727b490SJeeja KP } 5919727b490SJeeja KP 5929727b490SJeeja KP static int 5939727b490SJeeja KP snd_compr_set_metadata(struct snd_compr_stream *stream, unsigned long arg) 5949727b490SJeeja KP { 5959727b490SJeeja KP struct snd_compr_metadata metadata; 5969727b490SJeeja KP int retval; 5979727b490SJeeja KP 5989727b490SJeeja KP if (!stream->ops->set_metadata) 5999727b490SJeeja KP return -ENXIO; 6009727b490SJeeja KP /* 6019727b490SJeeja KP * we should allow parameter change only when stream has been 6029727b490SJeeja KP * opened not in other cases 6039727b490SJeeja KP */ 6049727b490SJeeja KP if (copy_from_user(&metadata, (void __user *)arg, sizeof(metadata))) 6059727b490SJeeja KP return -EFAULT; 6069727b490SJeeja KP 6079727b490SJeeja KP retval = stream->ops->set_metadata(stream, &metadata); 6089727b490SJeeja KP stream->metadata_set = true; 6099727b490SJeeja KP 6109727b490SJeeja KP return retval; 6119727b490SJeeja KP } 6129727b490SJeeja KP 613b21c60a4SVinod Koul static inline int 614b21c60a4SVinod Koul snd_compr_tstamp(struct snd_compr_stream *stream, unsigned long arg) 615b21c60a4SVinod Koul { 61617ac8e5cSRichard Fitzgerald struct snd_compr_tstamp tstamp = {0}; 61717ac8e5cSRichard Fitzgerald int ret; 618b21c60a4SVinod Koul 61917ac8e5cSRichard Fitzgerald ret = snd_compr_update_tstamp(stream, &tstamp); 62017ac8e5cSRichard Fitzgerald if (ret == 0) 62117ac8e5cSRichard Fitzgerald ret = copy_to_user((struct snd_compr_tstamp __user *)arg, 622b21c60a4SVinod Koul &tstamp, sizeof(tstamp)) ? -EFAULT : 0; 62317ac8e5cSRichard Fitzgerald return ret; 624b21c60a4SVinod Koul } 625b21c60a4SVinod Koul 626b21c60a4SVinod Koul static int snd_compr_pause(struct snd_compr_stream *stream) 627b21c60a4SVinod Koul { 628b21c60a4SVinod Koul int retval; 629b21c60a4SVinod Koul 630b21c60a4SVinod Koul if (stream->runtime->state != SNDRV_PCM_STATE_RUNNING) 631b21c60a4SVinod Koul return -EPERM; 632b21c60a4SVinod Koul retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_PUSH); 6336b18f793SVinod Koul if (!retval) 634b21c60a4SVinod Koul stream->runtime->state = SNDRV_PCM_STATE_PAUSED; 635b21c60a4SVinod Koul return retval; 636b21c60a4SVinod Koul } 637b21c60a4SVinod Koul 638b21c60a4SVinod Koul static int snd_compr_resume(struct snd_compr_stream *stream) 639b21c60a4SVinod Koul { 640b21c60a4SVinod Koul int retval; 641b21c60a4SVinod Koul 642b21c60a4SVinod Koul if (stream->runtime->state != SNDRV_PCM_STATE_PAUSED) 643b21c60a4SVinod Koul return -EPERM; 644b21c60a4SVinod Koul retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_RELEASE); 645b21c60a4SVinod Koul if (!retval) 646b21c60a4SVinod Koul stream->runtime->state = SNDRV_PCM_STATE_RUNNING; 647b21c60a4SVinod Koul return retval; 648b21c60a4SVinod Koul } 649b21c60a4SVinod Koul 650b21c60a4SVinod Koul static int snd_compr_start(struct snd_compr_stream *stream) 651b21c60a4SVinod Koul { 652b21c60a4SVinod Koul int retval; 653b21c60a4SVinod Koul 654b21c60a4SVinod Koul if (stream->runtime->state != SNDRV_PCM_STATE_PREPARED) 655b21c60a4SVinod Koul return -EPERM; 656b21c60a4SVinod Koul retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_START); 657b21c60a4SVinod Koul if (!retval) 658b21c60a4SVinod Koul stream->runtime->state = SNDRV_PCM_STATE_RUNNING; 659b21c60a4SVinod Koul return retval; 660b21c60a4SVinod Koul } 661b21c60a4SVinod Koul 662b21c60a4SVinod Koul static int snd_compr_stop(struct snd_compr_stream *stream) 663b21c60a4SVinod Koul { 664b21c60a4SVinod Koul int retval; 665b21c60a4SVinod Koul 666b21c60a4SVinod Koul if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED || 667b21c60a4SVinod Koul stream->runtime->state == SNDRV_PCM_STATE_SETUP) 668b21c60a4SVinod Koul return -EPERM; 669b21c60a4SVinod Koul retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_STOP); 670b21c60a4SVinod Koul if (!retval) { 671b21c60a4SVinod Koul stream->runtime->state = SNDRV_PCM_STATE_SETUP; 672b21c60a4SVinod Koul wake_up(&stream->runtime->sleep); 6738b21460aSVinod Koul stream->runtime->total_bytes_available = 0; 6748b21460aSVinod Koul stream->runtime->total_bytes_transferred = 0; 675b21c60a4SVinod Koul } 676b21c60a4SVinod Koul return retval; 677b21c60a4SVinod Koul } 678b21c60a4SVinod Koul 679b21c60a4SVinod Koul static int snd_compr_drain(struct snd_compr_stream *stream) 680b21c60a4SVinod Koul { 681b21c60a4SVinod Koul int retval; 682b21c60a4SVinod Koul 683b21c60a4SVinod Koul if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED || 684b21c60a4SVinod Koul stream->runtime->state == SNDRV_PCM_STATE_SETUP) 685b21c60a4SVinod Koul return -EPERM; 686b21c60a4SVinod Koul retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_DRAIN); 687b21c60a4SVinod Koul if (!retval) { 688b21c60a4SVinod Koul stream->runtime->state = SNDRV_PCM_STATE_DRAINING; 689b21c60a4SVinod Koul wake_up(&stream->runtime->sleep); 690b21c60a4SVinod Koul } 691b21c60a4SVinod Koul return retval; 692b21c60a4SVinod Koul } 693b21c60a4SVinod Koul 6949727b490SJeeja KP static int snd_compr_next_track(struct snd_compr_stream *stream) 6959727b490SJeeja KP { 6969727b490SJeeja KP int retval; 6979727b490SJeeja KP 6989727b490SJeeja KP /* only a running stream can transition to next track */ 6999727b490SJeeja KP if (stream->runtime->state != SNDRV_PCM_STATE_RUNNING) 7009727b490SJeeja KP return -EPERM; 7019727b490SJeeja KP 7029727b490SJeeja KP /* you can signal next track isf this is intended to be a gapless stream 7039727b490SJeeja KP * and current track metadata is set 7049727b490SJeeja KP */ 7059727b490SJeeja KP if (stream->metadata_set == false) 7069727b490SJeeja KP return -EPERM; 7079727b490SJeeja KP 7089727b490SJeeja KP retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_NEXT_TRACK); 7099727b490SJeeja KP if (retval != 0) 7109727b490SJeeja KP return retval; 7119727b490SJeeja KP stream->metadata_set = false; 7129727b490SJeeja KP stream->next_track = true; 7139727b490SJeeja KP return 0; 7149727b490SJeeja KP } 7159727b490SJeeja KP 7169727b490SJeeja KP static int snd_compr_partial_drain(struct snd_compr_stream *stream) 7179727b490SJeeja KP { 7189727b490SJeeja KP int retval; 7199727b490SJeeja KP if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED || 7209727b490SJeeja KP stream->runtime->state == SNDRV_PCM_STATE_SETUP) 7219727b490SJeeja KP return -EPERM; 7229727b490SJeeja KP /* stream can be drained only when next track has been signalled */ 7239727b490SJeeja KP if (stream->next_track == false) 7249727b490SJeeja KP return -EPERM; 7259727b490SJeeja KP 7269727b490SJeeja KP retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_PARTIAL_DRAIN); 7279727b490SJeeja KP 7289727b490SJeeja KP stream->next_track = false; 7299727b490SJeeja KP return retval; 7309727b490SJeeja KP } 7319727b490SJeeja KP 732b21c60a4SVinod Koul static long snd_compr_ioctl(struct file *f, unsigned int cmd, unsigned long arg) 733b21c60a4SVinod Koul { 734b21c60a4SVinod Koul struct snd_compr_file *data = f->private_data; 735b21c60a4SVinod Koul struct snd_compr_stream *stream; 736b21c60a4SVinod Koul int retval = -ENOTTY; 737b21c60a4SVinod Koul 738b21c60a4SVinod Koul if (snd_BUG_ON(!data)) 739b21c60a4SVinod Koul return -EFAULT; 740b21c60a4SVinod Koul stream = &data->stream; 741b21c60a4SVinod Koul if (snd_BUG_ON(!stream)) 742b21c60a4SVinod Koul return -EFAULT; 743b21c60a4SVinod Koul mutex_lock(&stream->device->lock); 744b21c60a4SVinod Koul switch (_IOC_NR(cmd)) { 745b21c60a4SVinod Koul case _IOC_NR(SNDRV_COMPRESS_IOCTL_VERSION): 746a8d30608SVinod Koul retval = put_user(SNDRV_COMPRESS_VERSION, 747b21c60a4SVinod Koul (int __user *)arg) ? -EFAULT : 0; 748b21c60a4SVinod Koul break; 749b21c60a4SVinod Koul case _IOC_NR(SNDRV_COMPRESS_GET_CAPS): 750b21c60a4SVinod Koul retval = snd_compr_get_caps(stream, arg); 751b21c60a4SVinod Koul break; 752b21c60a4SVinod Koul case _IOC_NR(SNDRV_COMPRESS_GET_CODEC_CAPS): 753b21c60a4SVinod Koul retval = snd_compr_get_codec_caps(stream, arg); 754b21c60a4SVinod Koul break; 755b21c60a4SVinod Koul case _IOC_NR(SNDRV_COMPRESS_SET_PARAMS): 756b21c60a4SVinod Koul retval = snd_compr_set_params(stream, arg); 757b21c60a4SVinod Koul break; 758b21c60a4SVinod Koul case _IOC_NR(SNDRV_COMPRESS_GET_PARAMS): 759b21c60a4SVinod Koul retval = snd_compr_get_params(stream, arg); 760b21c60a4SVinod Koul break; 7619727b490SJeeja KP case _IOC_NR(SNDRV_COMPRESS_SET_METADATA): 7629727b490SJeeja KP retval = snd_compr_set_metadata(stream, arg); 7639727b490SJeeja KP break; 7649727b490SJeeja KP case _IOC_NR(SNDRV_COMPRESS_GET_METADATA): 7659727b490SJeeja KP retval = snd_compr_get_metadata(stream, arg); 7669727b490SJeeja KP break; 767b21c60a4SVinod Koul case _IOC_NR(SNDRV_COMPRESS_TSTAMP): 768b21c60a4SVinod Koul retval = snd_compr_tstamp(stream, arg); 769b21c60a4SVinod Koul break; 770b21c60a4SVinod Koul case _IOC_NR(SNDRV_COMPRESS_AVAIL): 771b21c60a4SVinod Koul retval = snd_compr_ioctl_avail(stream, arg); 772b21c60a4SVinod Koul break; 773b21c60a4SVinod Koul case _IOC_NR(SNDRV_COMPRESS_PAUSE): 774b21c60a4SVinod Koul retval = snd_compr_pause(stream); 775b21c60a4SVinod Koul break; 776b21c60a4SVinod Koul case _IOC_NR(SNDRV_COMPRESS_RESUME): 777b21c60a4SVinod Koul retval = snd_compr_resume(stream); 778b21c60a4SVinod Koul break; 779b21c60a4SVinod Koul case _IOC_NR(SNDRV_COMPRESS_START): 780b21c60a4SVinod Koul retval = snd_compr_start(stream); 781b21c60a4SVinod Koul break; 782b21c60a4SVinod Koul case _IOC_NR(SNDRV_COMPRESS_STOP): 783b21c60a4SVinod Koul retval = snd_compr_stop(stream); 784b21c60a4SVinod Koul break; 785b21c60a4SVinod Koul case _IOC_NR(SNDRV_COMPRESS_DRAIN): 786b21c60a4SVinod Koul retval = snd_compr_drain(stream); 787b21c60a4SVinod Koul break; 7889727b490SJeeja KP case _IOC_NR(SNDRV_COMPRESS_PARTIAL_DRAIN): 7899727b490SJeeja KP retval = snd_compr_partial_drain(stream); 7909727b490SJeeja KP break; 7919727b490SJeeja KP case _IOC_NR(SNDRV_COMPRESS_NEXT_TRACK): 7929727b490SJeeja KP retval = snd_compr_next_track(stream); 7939727b490SJeeja KP break; 7949727b490SJeeja KP 795b21c60a4SVinod Koul } 796b21c60a4SVinod Koul mutex_unlock(&stream->device->lock); 797b21c60a4SVinod Koul return retval; 798b21c60a4SVinod Koul } 799b21c60a4SVinod Koul 800b21c60a4SVinod Koul static const struct file_operations snd_compr_file_ops = { 801b21c60a4SVinod Koul .owner = THIS_MODULE, 802b21c60a4SVinod Koul .open = snd_compr_open, 803b21c60a4SVinod Koul .release = snd_compr_free, 804b21c60a4SVinod Koul .write = snd_compr_write, 805b21c60a4SVinod Koul .read = snd_compr_read, 806b21c60a4SVinod Koul .unlocked_ioctl = snd_compr_ioctl, 807b21c60a4SVinod Koul .mmap = snd_compr_mmap, 808b21c60a4SVinod Koul .poll = snd_compr_poll, 809b21c60a4SVinod Koul }; 810b21c60a4SVinod Koul 811b21c60a4SVinod Koul static int snd_compress_dev_register(struct snd_device *device) 812b21c60a4SVinod Koul { 813b21c60a4SVinod Koul int ret = -EINVAL; 814b21c60a4SVinod Koul char str[16]; 815b21c60a4SVinod Koul struct snd_compr *compr; 816b21c60a4SVinod Koul 817b21c60a4SVinod Koul if (snd_BUG_ON(!device || !device->device_data)) 818b21c60a4SVinod Koul return -EBADFD; 819b21c60a4SVinod Koul compr = device->device_data; 820b21c60a4SVinod Koul 821b21c60a4SVinod Koul sprintf(str, "comprC%iD%i", compr->card->number, compr->device); 822b21c60a4SVinod Koul pr_debug("reg %s for device %s, direction %d\n", str, compr->name, 823b21c60a4SVinod Koul compr->direction); 824b21c60a4SVinod Koul /* register compressed device */ 825b21c60a4SVinod Koul ret = snd_register_device(SNDRV_DEVICE_TYPE_COMPRESS, compr->card, 826b21c60a4SVinod Koul compr->device, &snd_compr_file_ops, compr, str); 827b21c60a4SVinod Koul if (ret < 0) { 828b21c60a4SVinod Koul pr_err("snd_register_device failed\n %d", ret); 829b21c60a4SVinod Koul return ret; 830b21c60a4SVinod Koul } 831b21c60a4SVinod Koul return ret; 832b21c60a4SVinod Koul 833b21c60a4SVinod Koul } 834b21c60a4SVinod Koul 835b21c60a4SVinod Koul static int snd_compress_dev_disconnect(struct snd_device *device) 836b21c60a4SVinod Koul { 837b21c60a4SVinod Koul struct snd_compr *compr; 838b21c60a4SVinod Koul 839b21c60a4SVinod Koul compr = device->device_data; 840b21c60a4SVinod Koul snd_unregister_device(compr->direction, compr->card, compr->device); 841b21c60a4SVinod Koul return 0; 842b21c60a4SVinod Koul } 843b21c60a4SVinod Koul 844b21c60a4SVinod Koul /* 845b21c60a4SVinod Koul * snd_compress_new: create new compress device 846b21c60a4SVinod Koul * @card: sound card pointer 847b21c60a4SVinod Koul * @device: device number 848b21c60a4SVinod Koul * @dirn: device direction, should be of type enum snd_compr_direction 849b21c60a4SVinod Koul * @compr: compress device pointer 850b21c60a4SVinod Koul */ 851b21c60a4SVinod Koul int snd_compress_new(struct snd_card *card, int device, 852b21c60a4SVinod Koul int dirn, struct snd_compr *compr) 853b21c60a4SVinod Koul { 854b21c60a4SVinod Koul static struct snd_device_ops ops = { 855b21c60a4SVinod Koul .dev_free = NULL, 856b21c60a4SVinod Koul .dev_register = snd_compress_dev_register, 857b21c60a4SVinod Koul .dev_disconnect = snd_compress_dev_disconnect, 858b21c60a4SVinod Koul }; 859b21c60a4SVinod Koul 860b21c60a4SVinod Koul compr->card = card; 861b21c60a4SVinod Koul compr->device = device; 862b21c60a4SVinod Koul compr->direction = dirn; 863b21c60a4SVinod Koul return snd_device_new(card, SNDRV_DEV_COMPRESS, compr, &ops); 864b21c60a4SVinod Koul } 865b21c60a4SVinod Koul EXPORT_SYMBOL_GPL(snd_compress_new); 866b21c60a4SVinod Koul 867b21c60a4SVinod Koul static int snd_compress_add_device(struct snd_compr *device) 868b21c60a4SVinod Koul { 869b21c60a4SVinod Koul int ret; 870b21c60a4SVinod Koul 871b21c60a4SVinod Koul if (!device->card) 872b21c60a4SVinod Koul return -EINVAL; 873b21c60a4SVinod Koul 874b21c60a4SVinod Koul /* register the card */ 875b21c60a4SVinod Koul ret = snd_card_register(device->card); 876b21c60a4SVinod Koul if (ret) 877b21c60a4SVinod Koul goto out; 878b21c60a4SVinod Koul return 0; 879b21c60a4SVinod Koul 880b21c60a4SVinod Koul out: 881b21c60a4SVinod Koul pr_err("failed with %d\n", ret); 882b21c60a4SVinod Koul return ret; 883b21c60a4SVinod Koul 884b21c60a4SVinod Koul } 885b21c60a4SVinod Koul 886b21c60a4SVinod Koul static int snd_compress_remove_device(struct snd_compr *device) 887b21c60a4SVinod Koul { 888b21c60a4SVinod Koul return snd_card_free(device->card); 889b21c60a4SVinod Koul } 890b21c60a4SVinod Koul 891b21c60a4SVinod Koul /** 892b21c60a4SVinod Koul * snd_compress_register - register compressed device 893b21c60a4SVinod Koul * 894b21c60a4SVinod Koul * @device: compressed device to register 895b21c60a4SVinod Koul */ 896b21c60a4SVinod Koul int snd_compress_register(struct snd_compr *device) 897b21c60a4SVinod Koul { 898b21c60a4SVinod Koul int retval; 899b21c60a4SVinod Koul 900b21c60a4SVinod Koul if (device->name == NULL || device->dev == NULL || device->ops == NULL) 901b21c60a4SVinod Koul return -EINVAL; 902b21c60a4SVinod Koul 903b21c60a4SVinod Koul pr_debug("Registering compressed device %s\n", device->name); 904b21c60a4SVinod Koul if (snd_BUG_ON(!device->ops->open)) 905b21c60a4SVinod Koul return -EINVAL; 906b21c60a4SVinod Koul if (snd_BUG_ON(!device->ops->free)) 907b21c60a4SVinod Koul return -EINVAL; 908b21c60a4SVinod Koul if (snd_BUG_ON(!device->ops->set_params)) 909b21c60a4SVinod Koul return -EINVAL; 910b21c60a4SVinod Koul if (snd_BUG_ON(!device->ops->trigger)) 911b21c60a4SVinod Koul return -EINVAL; 912b21c60a4SVinod Koul 913b21c60a4SVinod Koul mutex_init(&device->lock); 914b21c60a4SVinod Koul 915b21c60a4SVinod Koul /* register a compressed card */ 916b21c60a4SVinod Koul mutex_lock(&device_mutex); 917b21c60a4SVinod Koul retval = snd_compress_add_device(device); 918b21c60a4SVinod Koul mutex_unlock(&device_mutex); 919b21c60a4SVinod Koul return retval; 920b21c60a4SVinod Koul } 921b21c60a4SVinod Koul EXPORT_SYMBOL_GPL(snd_compress_register); 922b21c60a4SVinod Koul 923b21c60a4SVinod Koul int snd_compress_deregister(struct snd_compr *device) 924b21c60a4SVinod Koul { 925b21c60a4SVinod Koul pr_debug("Removing compressed device %s\n", device->name); 926b21c60a4SVinod Koul mutex_lock(&device_mutex); 927b21c60a4SVinod Koul snd_compress_remove_device(device); 928b21c60a4SVinod Koul mutex_unlock(&device_mutex); 929b21c60a4SVinod Koul return 0; 930b21c60a4SVinod Koul } 931b21c60a4SVinod Koul EXPORT_SYMBOL_GPL(snd_compress_deregister); 932b21c60a4SVinod Koul 933b21c60a4SVinod Koul static int __init snd_compress_init(void) 934b21c60a4SVinod Koul { 935b21c60a4SVinod Koul return 0; 936b21c60a4SVinod Koul } 937b21c60a4SVinod Koul 938b21c60a4SVinod Koul static void __exit snd_compress_exit(void) 939b21c60a4SVinod Koul { 940b21c60a4SVinod Koul } 941b21c60a4SVinod Koul 942b21c60a4SVinod Koul module_init(snd_compress_init); 943b21c60a4SVinod Koul module_exit(snd_compress_exit); 944b21c60a4SVinod Koul 945b21c60a4SVinod Koul MODULE_DESCRIPTION("ALSA Compressed offload framework"); 946b21c60a4SVinod Koul MODULE_AUTHOR("Vinod Koul <vinod.koul@linux.intel.com>"); 947b21c60a4SVinod Koul MODULE_LICENSE("GPL v2"); 948