2 * intel_sst_interface.c - Intel SST Driver for audio engine
4 * Copyright (C) 2008-10 Intel Corp
5 * Authors: Vinod Koul <vinod.koul@intel.com>
6 * Harsha Priya <priya.harsha@intel.com>
7 * Dharageswari R <dharageswari.r@intel.com>
8 * Jeeja KP <jeeja.kp@intel.com>
9 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2 of the License.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
24 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25 * This driver exposes the audio engine functionalities to the ALSA
27 * Upper layer interfaces (MAD driver, MMF) to SST driver
30 #include <linux/pci.h>
32 #include <linux/uio.h>
33 #include <linux/aio.h>
34 #include <linux/uaccess.h>
35 #include <linux/firmware.h>
36 #include <linux/ioctl.h>
37 #include <linux/smp_lock.h>
38 #ifdef CONFIG_MRST_RAR_HANDLER
39 #include <linux/rar_register.h>
40 #include "../../../drivers/staging/memrar/memrar.h"
42 #include "intel_sst.h"
43 #include "intel_sst_ioctl.h"
44 #include "intel_sst_fw_ipc.h"
45 #include "intel_sst_common.h"
48 #define STREAM_MODULE 0
52 * intel_sst_check_device - checks SST device
54 * This utility function checks the state of SST device and downlaods FW if
55 * not done, or resumes the device if suspended
58 static int intel_sst_check_device(void)
61 if (sst_drv_ctx->pmic_state != SND_MAD_INIT_DONE) {
62 pr_warn("sst: Sound card not availble\n ");
65 if (sst_drv_ctx->sst_state == SST_SUSPENDED) {
66 pr_debug("sst: Resuming from Suspended state\n");
67 retval = intel_sst_resume(sst_drv_ctx->pci);
69 pr_debug("sst: Resume Failed= %#x,abort\n", retval);
74 if (sst_drv_ctx->sst_state == SST_UN_INIT) {
75 /* FW is not downloaded */
76 retval = sst_download_fw();
79 if (sst_drv_ctx->pci_id == SST_MRST_PCI_ID) {
80 retval = sst_drv_ctx->rx_time_slot_status;
81 if (retval != RX_TIMESLOT_UNINIT
82 && sst_drv_ctx->pmic_vendor != SND_NC)
83 sst_enable_rx_timeslot(retval);
90 * intel_sst_open - opens a handle to driver
92 * @i_node: inode structure
93 * @file_ptr:pointer to file
95 * This function is called by OS when a user space component
96 * tries to get a driver handle. Only one handle at a time
99 int intel_sst_open(struct inode *i_node, struct file *file_ptr)
101 int retval = intel_sst_check_device();
105 mutex_lock(&sst_drv_ctx->stream_lock);
106 if (sst_drv_ctx->encoded_cnt < MAX_ENC_STREAM) {
107 struct ioctl_pvt_data *data =
108 kzalloc(sizeof(struct ioctl_pvt_data), GFP_KERNEL);
110 mutex_unlock(&sst_drv_ctx->stream_lock);
114 sst_drv_ctx->encoded_cnt++;
115 mutex_unlock(&sst_drv_ctx->stream_lock);
116 data->pvt_id = sst_assign_pvt_id(sst_drv_ctx);
118 file_ptr->private_data = (void *)data;
119 pr_debug("sst: pvt_id handle = %d!\n", data->pvt_id);
122 mutex_unlock(&sst_drv_ctx->stream_lock);
128 * intel_sst_open_cntrl - opens a handle to driver
130 * @i_node: inode structure
131 * @file_ptr:pointer to file
133 * This function is called by OS when a user space component
134 * tries to get a driver handle to /dev/intel_sst_control.
135 * Only one handle at a time will be allowed
136 * This is for control operations only
138 int intel_sst_open_cntrl(struct inode *i_node, struct file *file_ptr)
140 int retval = intel_sst_check_device();
144 /* audio manager open */
145 mutex_lock(&sst_drv_ctx->stream_lock);
146 if (sst_drv_ctx->am_cnt < MAX_AM_HANDLES) {
147 sst_drv_ctx->am_cnt++;
148 pr_debug("sst: AM handle opened...\n");
149 file_ptr->private_data = NULL;
153 mutex_unlock(&sst_drv_ctx->stream_lock);
158 * intel_sst_release - releases a handle to driver
160 * @i_node: inode structure
161 * @file_ptr: pointer to file
163 * This function is called by OS when a user space component
164 * tries to release a driver handle.
166 int intel_sst_release(struct inode *i_node, struct file *file_ptr)
168 struct ioctl_pvt_data *data = file_ptr->private_data;
170 pr_debug("sst: Release called, closing app handle\n");
171 mutex_lock(&sst_drv_ctx->stream_lock);
172 sst_drv_ctx->encoded_cnt--;
173 sst_drv_ctx->stream_cnt--;
174 mutex_unlock(&sst_drv_ctx->stream_lock);
175 free_stream_context(data->str_id);
180 int intel_sst_release_cntrl(struct inode *i_node, struct file *file_ptr)
182 /* audio manager close */
183 mutex_lock(&sst_drv_ctx->stream_lock);
184 sst_drv_ctx->am_cnt--;
185 mutex_unlock(&sst_drv_ctx->stream_lock);
186 pr_debug("sst: AM handle closed\n");
191 * intel_sst_mmap - mmaps a kernel buffer to user space for copying data
193 * @vma: vm area structure instance
194 * @file_ptr: pointer to file
196 * This function is called by OS when a user space component
197 * tries to get mmap memory from driver
199 int intel_sst_mmap(struct file *file_ptr, struct vm_area_struct *vma)
202 struct ioctl_pvt_data *data =
203 (struct ioctl_pvt_data *)file_ptr->private_data;
204 int str_id = data->str_id;
207 retval = sst_validate_strid(str_id);
211 length = vma->vm_end - vma->vm_start;
212 pr_debug("sst: called for stream %d length 0x%x\n", str_id, length);
214 if (length > sst_drv_ctx->mmap_len)
216 if (!sst_drv_ctx->mmap_mem)
219 /* round it up to the page bondary */
220 /*mem_area = (void *)((((unsigned long)sst_drv_ctx->mmap_mem)
221 + PAGE_SIZE - 1) & PAGE_MASK);*/
222 mem_area = (void *) PAGE_ALIGN((unsigned int) sst_drv_ctx->mmap_mem);
224 /* map the whole physically contiguous area in one piece */
225 retval = remap_pfn_range(vma,
227 virt_to_phys((void *)mem_area) >> PAGE_SHIFT,
231 sst_drv_ctx->streams[str_id].mmapped = false;
233 sst_drv_ctx->streams[str_id].mmapped = true;
235 pr_debug("sst: mmap ret 0x%x\n", retval);
239 /* sets mmap data buffers to play/capture*/
240 static int intel_sst_mmap_play_capture(u32 str_id,
241 struct snd_sst_mmap_buffs *mmap_buf)
243 struct sst_stream_bufs *bufs;
245 struct stream_info *stream;
246 struct snd_sst_mmap_buff_entry *buf_entry;
247 struct snd_sst_mmap_buff_entry *tmp_buf;
249 pr_debug("sst:called for str_id %d\n", str_id);
250 retval = sst_validate_strid(str_id);
254 stream = &sst_drv_ctx->streams[str_id];
255 if (stream->mmapped != true)
258 if (stream->status == STREAM_UN_INIT ||
259 stream->status == STREAM_DECODE) {
262 stream->curr_bytes = 0;
263 stream->cumm_bytes = 0;
265 tmp_buf = kcalloc(mmap_buf->entries, sizeof(*tmp_buf), GFP_KERNEL);
268 if (copy_from_user(tmp_buf, (void __user *)mmap_buf->buff,
269 mmap_buf->entries * sizeof(*tmp_buf))) {
274 pr_debug("sst:new buffers count %d status %d\n",
275 mmap_buf->entries, stream->status);
277 for (i = 0; i < mmap_buf->entries; i++) {
278 bufs = kzalloc(sizeof(*bufs), GFP_KERNEL);
283 bufs->size = buf_entry->size;
284 bufs->offset = buf_entry->offset;
285 bufs->addr = sst_drv_ctx->mmap_mem;
286 bufs->in_use = false;
289 mutex_lock(&stream->lock);
290 list_add_tail(&bufs->node, &stream->bufs);
291 mutex_unlock(&stream->lock);
294 mutex_lock(&stream->lock);
295 stream->data_blk.condition = false;
296 stream->data_blk.ret_code = 0;
297 if (stream->status == STREAM_INIT &&
298 stream->prev != STREAM_UN_INIT &&
299 stream->need_draining != true) {
300 stream->prev = stream->status;
301 stream->status = STREAM_RUNNING;
302 if (stream->ops == STREAM_OPS_PLAYBACK) {
303 if (sst_play_frame(str_id) < 0) {
304 pr_warn("sst: play frames fail\n");
305 mutex_unlock(&stream->lock);
309 } else if (stream->ops == STREAM_OPS_CAPTURE) {
310 if (sst_capture_frame(str_id) < 0) {
311 pr_warn("sst: capture frame fail\n");
312 mutex_unlock(&stream->lock);
318 mutex_unlock(&stream->lock);
319 /* Block the call for reply */
320 if (!list_empty(&stream->bufs)) {
321 stream->data_blk.on = true;
322 retval = sst_wait_interruptible(sst_drv_ctx,
327 retval = stream->cumm_bytes;
328 pr_debug("sst:end of play/rec ioctl bytes = %d!!\n", retval);
335 /*sets user data buffers to play/capture*/
336 static int intel_sst_play_capture(struct stream_info *stream, int str_id)
340 stream->data_blk.ret_code = 0;
341 stream->data_blk.on = true;
342 stream->data_blk.condition = false;
344 mutex_lock(&stream->lock);
345 if (stream->status == STREAM_INIT && stream->prev != STREAM_UN_INIT) {
346 /* stream is started */
347 stream->prev = stream->status;
348 stream->status = STREAM_RUNNING;
351 if (stream->status == STREAM_INIT && stream->prev == STREAM_UN_INIT) {
352 /* stream is not started yet */
353 pr_debug("sst: Stream isn't in started state %d, prev %d\n",
354 stream->status, stream->prev);
355 } else if ((stream->status == STREAM_RUNNING ||
356 stream->status == STREAM_PAUSED) &&
357 stream->need_draining != true) {
358 /* stream is started */
359 if (stream->ops == STREAM_OPS_PLAYBACK ||
360 stream->ops == STREAM_OPS_PLAYBACK_DRM) {
361 if (sst_play_frame(str_id) < 0) {
362 pr_warn("sst: play frames failed\n");
363 mutex_unlock(&stream->lock);
366 } else if (stream->ops == STREAM_OPS_CAPTURE) {
367 if (sst_capture_frame(str_id) < 0) {
368 pr_warn("sst: capture frames failed\n ");
369 mutex_unlock(&stream->lock);
374 mutex_unlock(&stream->lock);
377 mutex_unlock(&stream->lock);
378 /* Block the call for reply */
380 retval = sst_wait_interruptible(sst_drv_ctx, &stream->data_blk);
382 stream->status = STREAM_INIT;
383 pr_debug("sst: wait returned error...\n");
388 /* fills kernel list with buffer addresses for SST DSP driver to process*/
389 static int snd_sst_fill_kernel_list(struct stream_info *stream,
390 const struct iovec *iovec, unsigned long nr_segs,
391 struct list_head *copy_to_list)
393 struct sst_stream_bufs *stream_bufs;
394 unsigned long index, mmap_len;
396 unsigned long size, copied_size;
397 int retval = 0, add_to_list = 0;
398 static int sent_offset;
399 static unsigned long sent_index;
401 stream_bufs = kzalloc(sizeof(*stream_bufs), GFP_KERNEL);
404 stream_bufs->addr = sst_drv_ctx->mmap_mem;
405 #ifdef CONFIG_MRST_RAR_HANDLER
406 if (stream->ops == STREAM_OPS_PLAYBACK_DRM) {
407 for (index = stream->sg_index; index < nr_segs; index++) {
409 struct sst_stream_bufs *stream_bufs =
410 kzalloc(sizeof(*stream_bufs), GFP_KERNEL);
412 stream->sg_index = index;
415 if (copy_from_user((void *) &rar_handle,
416 iovec[index].iov_base,
419 stream_bufs->addr = (char *)rar_handle;
420 stream_bufs->in_use = false;
421 stream_bufs->size = iovec[0].iov_len;
423 mutex_lock(&stream->lock);
424 list_add_tail(&stream_bufs->node, &stream->bufs);
425 mutex_unlock(&stream->lock);
427 stream->sg_index = index;
431 mmap_len = sst_drv_ctx->mmap_len;
432 stream_bufs->addr = sst_drv_ctx->mmap_mem;
433 bufp = stream->cur_ptr;
437 if (!stream->sg_index)
438 sent_index = sent_offset = 0;
440 for (index = stream->sg_index; index < nr_segs; index++) {
441 stream->sg_index = index;
442 if (!stream->cur_ptr)
443 bufp = iovec[index].iov_base;
445 size = ((unsigned long)iovec[index].iov_base
446 + iovec[index].iov_len) - (unsigned long) bufp;
448 if ((copied_size + size) > mmap_len)
449 size = mmap_len - copied_size;
452 if (stream->ops == STREAM_OPS_PLAYBACK) {
453 if (copy_from_user((void *)
454 (stream_bufs->addr + copied_size),
456 /* Clean up the list and return error code */
460 } else if (stream->ops == STREAM_OPS_CAPTURE) {
461 struct snd_sst_user_cap_list *entry =
462 kzalloc(sizeof(*entry), GFP_KERNEL);
468 entry->iov_index = index;
469 entry->iov_offset = (unsigned long) bufp -
470 (unsigned long)iovec[index].iov_base;
471 entry->offset = copied_size;
473 list_add_tail(&entry->node, copy_to_list);
476 stream->cur_ptr = bufp + size;
478 if (((unsigned long)iovec[index].iov_base
479 + iovec[index].iov_len) <
480 ((unsigned long)iovec[index].iov_base)) {
481 pr_debug("sst: Buffer overflows");
486 if (((unsigned long)iovec[index].iov_base
487 + iovec[index].iov_len) ==
488 (unsigned long)stream->cur_ptr) {
489 stream->cur_ptr = NULL;
494 pr_debug("sst: copied_size - %lx\n", copied_size);
495 if ((copied_size >= mmap_len) ||
496 (stream->sg_index == nr_segs)) {
501 stream_bufs->in_use = false;
502 stream_bufs->size = copied_size;
504 mutex_lock(&stream->lock);
505 list_add_tail(&stream_bufs->node, &stream->bufs);
506 mutex_unlock(&stream->lock);
513 /* This function copies the captured data returned from SST DSP engine
514 * to the user buffers*/
515 static int snd_sst_copy_userbuf_capture(struct stream_info *stream,
516 const struct iovec *iovec,
517 struct list_head *copy_to_list)
519 struct snd_sst_user_cap_list *entry, *_entry;
520 struct sst_stream_bufs *kbufs = NULL, *_kbufs;
523 /* copy sent buffers */
524 pr_debug("sst: capture stream copying to user now...\n");
525 list_for_each_entry_safe(kbufs, _kbufs, &stream->bufs, node) {
526 if (kbufs->in_use == true) {
528 list_for_each_entry_safe(entry, _entry,
529 copy_to_list, node) {
530 if (copy_to_user((void *)
531 iovec[entry->iov_index].iov_base +
533 kbufs->addr + entry->offset,
535 /* Clean up the list and return error */
539 list_del(&entry->node);
544 pr_debug("sst: end of cap copy\n");
549 * snd_sst_userbufs_play_cap - constructs the list from user buffers
551 * @iovec:pointer to iovec structure
552 * @nr_segs:number entries in the iovec structure
554 * @stream:pointer to stream_info structure
556 * This function will traverse the user list and copy the data to the kernel
559 static int snd_sst_userbufs_play_cap(const struct iovec *iovec,
560 unsigned long nr_segs, unsigned int str_id,
561 struct stream_info *stream)
564 LIST_HEAD(copy_to_list);
567 retval = snd_sst_fill_kernel_list(stream, iovec, nr_segs,
570 retval = intel_sst_play_capture(stream, str_id);
574 if (stream->ops == STREAM_OPS_CAPTURE) {
575 retval = snd_sst_copy_userbuf_capture(stream, iovec,
581 /* This function is common function across read/write
582 for user buffers called from system calls*/
583 static int intel_sst_read_write(unsigned int str_id, char __user *buf,
587 struct stream_info *stream;
589 unsigned long nr_segs;
591 retval = sst_validate_strid(str_id);
594 stream = &sst_drv_ctx->streams[str_id];
595 if (stream->mmapped == true) {
596 pr_warn("sst: user write and stream is mapped");
601 stream->curr_bytes = 0;
602 stream->cumm_bytes = 0;
603 /* copy user buf details */
604 pr_debug("sst: new buffers %p, copy size %d, status %d\n" ,
605 buf, (int) count, (int) stream->status);
607 stream->buf_type = SST_BUF_USER_STATIC;
608 iovec.iov_base = (void *)buf;
609 iovec.iov_len = count;
613 retval = snd_sst_userbufs_play_cap(
614 &iovec, nr_segs, str_id, stream);
618 } while (stream->sg_index < nr_segs);
620 stream->sg_index = 0;
621 stream->cur_ptr = NULL;
623 retval = stream->cumm_bytes;
624 pr_debug("sst: end of play/rec bytes = %d!!\n", retval);
629 * intel_sst_write - This function is called when user tries to play out data
631 * @file_ptr:pointer to file
632 * @buf:user buffer to be played out
633 * @count:size of tthe buffer
634 * @offset:offset to start from
636 * writes the encoded data into DSP
638 int intel_sst_write(struct file *file_ptr, const char __user *buf,
639 size_t count, loff_t *offset)
641 struct ioctl_pvt_data *data = file_ptr->private_data;
642 int str_id = data->str_id;
643 struct stream_info *stream = &sst_drv_ctx->streams[str_id];
645 pr_debug("sst: called for %d\n", str_id);
646 if (stream->status == STREAM_UN_INIT ||
647 stream->status == STREAM_DECODE) {
650 return intel_sst_read_write(str_id, (char __user *)buf, count);
654 * intel_sst_aio_write - write buffers
656 * @kiocb:pointer to a structure containing file pointer
657 * @iov:list of user buffer to be played out
658 * @nr_segs:number of entries
659 * @offset:offset to start from
661 * This function is called when user tries to play out multiple data buffers
663 ssize_t intel_sst_aio_write(struct kiocb *kiocb, const struct iovec *iov,
664 unsigned long nr_segs, loff_t offset)
667 struct ioctl_pvt_data *data = kiocb->ki_filp->private_data;
668 int str_id = data->str_id;
669 struct stream_info *stream;
671 pr_debug("sst: entry - %ld\n", nr_segs);
673 if (is_sync_kiocb(kiocb) == false)
676 pr_debug("sst: called for str_id %d\n", str_id);
677 retval = sst_validate_strid(str_id);
680 stream = &sst_drv_ctx->streams[str_id];
681 if (stream->mmapped == true)
683 if (stream->status == STREAM_UN_INIT ||
684 stream->status == STREAM_DECODE) {
687 stream->curr_bytes = 0;
688 stream->cumm_bytes = 0;
689 pr_debug("sst: new segs %ld, offset %d, status %d\n" ,
690 nr_segs, (int) offset, (int) stream->status);
691 stream->buf_type = SST_BUF_USER_STATIC;
693 retval = snd_sst_userbufs_play_cap(iov, nr_segs,
698 } while (stream->sg_index < nr_segs);
700 stream->sg_index = 0;
701 stream->cur_ptr = NULL;
703 retval = stream->cumm_bytes;
704 pr_debug("sst: end of play/rec bytes = %d!!\n", retval);
709 * intel_sst_read - read the encoded data
711 * @file_ptr: pointer to file
712 * @buf: user buffer to be filled with captured data
713 * @count: size of tthe buffer
714 * @offset: offset to start from
716 * This function is called when user tries to capture data
718 int intel_sst_read(struct file *file_ptr, char __user *buf,
719 size_t count, loff_t *offset)
721 struct ioctl_pvt_data *data = file_ptr->private_data;
722 int str_id = data->str_id;
723 struct stream_info *stream = &sst_drv_ctx->streams[str_id];
725 pr_debug("sst: called for %d\n", str_id);
726 if (stream->status == STREAM_UN_INIT ||
727 stream->status == STREAM_DECODE)
729 return intel_sst_read_write(str_id, buf, count);
733 * intel_sst_aio_read - aio read
735 * @kiocb: pointer to a structure containing file pointer
736 * @iov: list of user buffer to be filled with captured
737 * @nr_segs: number of entries
738 * @offset: offset to start from
740 * This function is called when user tries to capture out multiple data buffers
742 ssize_t intel_sst_aio_read(struct kiocb *kiocb, const struct iovec *iov,
743 unsigned long nr_segs, loff_t offset)
746 struct ioctl_pvt_data *data = kiocb->ki_filp->private_data;
747 int str_id = data->str_id;
748 struct stream_info *stream;
750 pr_debug("sst: entry - %ld\n", nr_segs);
752 if (is_sync_kiocb(kiocb) == false) {
753 pr_debug("sst: aio_read from user space is not allowed\n");
757 pr_debug("sst: called for str_id %d\n", str_id);
758 retval = sst_validate_strid(str_id);
761 stream = &sst_drv_ctx->streams[str_id];
762 if (stream->mmapped == true)
764 if (stream->status == STREAM_UN_INIT ||
765 stream->status == STREAM_DECODE)
767 stream->curr_bytes = 0;
768 stream->cumm_bytes = 0;
770 pr_debug("sst: new segs %ld, offset %d, status %d\n" ,
771 nr_segs, (int) offset, (int) stream->status);
772 stream->buf_type = SST_BUF_USER_STATIC;
774 retval = snd_sst_userbufs_play_cap(iov, nr_segs,
779 } while (stream->sg_index < nr_segs);
781 stream->sg_index = 0;
782 stream->cur_ptr = NULL;
784 retval = stream->cumm_bytes;
785 pr_debug("sst: end of play/rec bytes = %d!!\n", retval);
789 /* sst_print_stream_params - prints the stream parameters (debug fn)*/
790 static void sst_print_stream_params(struct snd_sst_get_stream_params *get_prm)
792 pr_debug("sst: codec params:result =%d\n",
793 get_prm->codec_params.result);
794 pr_debug("sst: codec params:stream = %d\n",
795 get_prm->codec_params.stream_id);
796 pr_debug("sst: codec params:codec = %d\n",
797 get_prm->codec_params.codec);
798 pr_debug("sst: codec params:ops = %d\n",
799 get_prm->codec_params.ops);
800 pr_debug("sst: codec params:stream_type= %d\n",
801 get_prm->codec_params.stream_type);
802 pr_debug("sst: pcmparams:sfreq= %d\n",
803 get_prm->pcm_params.sfreq);
804 pr_debug("sst: pcmparams:num_chan= %d\n",
805 get_prm->pcm_params.num_chan);
806 pr_debug("sst: pcmparams:pcm_wd_sz= %d\n",
807 get_prm->pcm_params.pcm_wd_sz);
812 * intel_sst_ioctl - recieves the device ioctl's
813 * @file_ptr:pointer to file
817 * This function is called by OS when a user space component
818 * sends an Ioctl to SST driver
820 long intel_sst_ioctl(struct file *file_ptr, unsigned int cmd, unsigned long arg)
823 struct ioctl_pvt_data *data = NULL;
824 int str_id = 0, minor = 0;
826 data = file_ptr->private_data;
829 str_id = data->str_id;
833 if (sst_drv_ctx->sst_state != SST_FW_RUNNING)
836 switch (_IOC_NR(cmd)) {
837 case _IOC_NR(SNDRV_SST_STREAM_PAUSE):
838 pr_debug("sst: IOCTL_PAUSE recieved for %d!\n", str_id);
839 if (minor != STREAM_MODULE) {
843 retval = sst_pause_stream(str_id);
846 case _IOC_NR(SNDRV_SST_STREAM_RESUME):
847 pr_debug("sst: SNDRV_SST_IOCTL_RESUME recieved!\n");
848 if (minor != STREAM_MODULE) {
852 retval = sst_resume_stream(str_id);
855 case _IOC_NR(SNDRV_SST_STREAM_SET_PARAMS): {
856 struct snd_sst_params str_param;
858 pr_debug("sst: IOCTL_SET_PARAMS recieved!\n");
859 if (minor != STREAM_MODULE) {
864 if (copy_from_user(&str_param, (void __user *)arg,
865 sizeof(str_param))) {
872 retval = sst_get_stream(&str_param);
874 struct stream_info *str_info;
877 sst_drv_ctx->stream_cnt++;
878 data->str_id = retval;
879 str_info = &sst_drv_ctx->streams[retval];
880 str_info->src = SST_DRV;
881 dest = (char *)arg + offsetof(struct snd_sst_params, stream_id);
882 retval = copy_to_user(dest, &retval, sizeof(__u32));
886 if (retval == -SST_ERR_INVALID_PARAMS)
890 pr_debug("sst: SET_STREAM_PARAMS recieved!\n");
891 /* allocated set params only */
892 retval = sst_set_stream_param(str_id, &str_param);
893 /* Block the call for reply */
895 int sfreq = 0, word_size = 0, num_channel = 0;
896 sfreq = str_param.sparams.uc.pcm_params.sfreq;
897 word_size = str_param.sparams.uc.pcm_params.pcm_wd_sz;
898 num_channel = str_param.sparams.uc.pcm_params.num_chan;
899 if (str_param.ops == STREAM_OPS_CAPTURE) {
900 sst_drv_ctx->scard_ops->\
901 set_pcm_audio_params(sfreq,
902 word_size, num_channel);
908 case _IOC_NR(SNDRV_SST_SET_VOL): {
909 struct snd_sst_vol set_vol;
911 if (copy_from_user(&set_vol, (void __user *)arg,
913 pr_debug("sst: copy failed\n");
917 pr_debug("sst: SET_VOLUME recieved for %d!\n",
919 if (minor == STREAM_MODULE && set_vol.stream_id == 0) {
920 pr_debug("sst: invalid operation!\n");
924 retval = sst_set_vol(&set_vol);
927 case _IOC_NR(SNDRV_SST_GET_VOL): {
928 struct snd_sst_vol get_vol;
930 if (copy_from_user(&get_vol, (void __user *)arg,
935 pr_debug("sst: IOCTL_GET_VOLUME recieved for stream = %d!\n",
937 if (minor == STREAM_MODULE && get_vol.stream_id == 0) {
938 pr_debug("sst: invalid operation!\n");
942 retval = sst_get_vol(&get_vol);
947 pr_debug("sst: id:%d\n, vol:%d, ramp_dur:%d, ramp_type:%d\n",
948 get_vol.stream_id, get_vol.volume,
949 get_vol.ramp_duration, get_vol.ramp_type);
950 if (copy_to_user((struct snd_sst_vol *)arg,
951 &get_vol, sizeof(get_vol))) {
955 /*sst_print_get_vol_info(str_id, &get_vol);*/
959 case _IOC_NR(SNDRV_SST_MUTE): {
960 struct snd_sst_mute set_mute;
962 if (copy_from_user(&set_mute, (void __user *)arg,
967 pr_debug("sst: SNDRV_SST_SET_VOLUME recieved for %d!\n",
969 if (minor == STREAM_MODULE && set_mute.stream_id == 0) {
973 retval = sst_set_mute(&set_mute);
976 case _IOC_NR(SNDRV_SST_STREAM_GET_PARAMS): {
977 struct snd_sst_get_stream_params get_params;
979 pr_debug("sst: IOCTL_GET_PARAMS recieved!\n");
985 retval = sst_get_stream_params(str_id, &get_params);
990 if (copy_to_user((struct snd_sst_get_stream_params *)arg,
991 &get_params, sizeof(get_params))) {
995 sst_print_stream_params(&get_params);
999 case _IOC_NR(SNDRV_SST_MMAP_PLAY):
1000 case _IOC_NR(SNDRV_SST_MMAP_CAPTURE): {
1001 struct snd_sst_mmap_buffs mmap_buf;
1003 pr_debug("sst: SNDRV_SST_MMAP_PLAY/CAPTURE recieved!\n");
1004 if (minor != STREAM_MODULE) {
1008 if (copy_from_user(&mmap_buf, (void __user *)arg,
1009 sizeof(mmap_buf))) {
1013 retval = intel_sst_mmap_play_capture(str_id, &mmap_buf);
1016 case _IOC_NR(SNDRV_SST_STREAM_DROP):
1017 pr_debug("sst: SNDRV_SST_IOCTL_DROP recieved!\n");
1018 if (minor != STREAM_MODULE) {
1022 retval = sst_drop_stream(str_id);
1025 case _IOC_NR(SNDRV_SST_STREAM_GET_TSTAMP): {
1026 struct snd_sst_tstamp tstamp = {0};
1027 unsigned long long time, freq, mod;
1029 pr_debug("sst: SNDRV_SST_STREAM_GET_TSTAMP recieved!\n");
1030 if (minor != STREAM_MODULE) {
1034 memcpy_fromio(&tstamp,
1035 ((void *)(sst_drv_ctx->mailbox + SST_TIME_STAMP)
1036 +(str_id * sizeof(tstamp))),
1038 time = tstamp.samples_rendered;
1039 freq = (unsigned long long) tstamp.sampling_frequency;
1040 time = time * 1000; /* converting it to ms */
1041 mod = do_div(time, freq);
1042 if (copy_to_user((void __user *)arg, &time,
1043 sizeof(unsigned long long)))
1048 case _IOC_NR(SNDRV_SST_STREAM_START):{
1049 struct stream_info *stream;
1051 pr_debug("sst: SNDRV_SST_STREAM_START recieved!\n");
1052 if (minor != STREAM_MODULE) {
1056 retval = sst_validate_strid(str_id);
1059 stream = &sst_drv_ctx->streams[str_id];
1060 mutex_lock(&stream->lock);
1061 if (stream->status == STREAM_INIT &&
1062 stream->need_draining != true) {
1063 stream->prev = stream->status;
1064 stream->status = STREAM_RUNNING;
1065 if (stream->ops == STREAM_OPS_PLAYBACK ||
1066 stream->ops == STREAM_OPS_PLAYBACK_DRM) {
1067 retval = sst_play_frame(str_id);
1068 } else if (stream->ops == STREAM_OPS_CAPTURE)
1069 retval = sst_capture_frame(str_id);
1072 mutex_unlock(&stream->lock);
1076 stream->status = STREAM_INIT;
1077 mutex_unlock(&stream->lock);
1083 mutex_unlock(&stream->lock);
1087 case _IOC_NR(SNDRV_SST_SET_TARGET_DEVICE): {
1088 struct snd_sst_target_device target_device;
1090 pr_debug("sst: SET_TARGET_DEVICE recieved!\n");
1091 if (copy_from_user(&target_device, (void __user *)arg,
1092 sizeof(target_device))) {
1096 if (minor != AM_MODULE) {
1100 retval = sst_target_device_select(&target_device);
1104 case _IOC_NR(SNDRV_SST_DRIVER_INFO): {
1105 struct snd_sst_driver_info info;
1107 pr_debug("sst: SNDRV_SST_DRIVER_INFO recived\n");
1108 info.version = SST_VERSION_NUM;
1109 /* hard coding, shud get sumhow later */
1110 info.active_pcm_streams = sst_drv_ctx->stream_cnt -
1111 sst_drv_ctx->encoded_cnt;
1112 info.active_enc_streams = sst_drv_ctx->encoded_cnt;
1113 info.max_pcm_streams = MAX_ACTIVE_STREAM - MAX_ENC_STREAM;
1114 info.max_enc_streams = MAX_ENC_STREAM;
1115 info.buf_per_stream = sst_drv_ctx->mmap_len;
1116 if (copy_to_user((void __user *)arg, &info,
1122 case _IOC_NR(SNDRV_SST_STREAM_DECODE): {
1123 struct snd_sst_dbufs param;
1124 struct snd_sst_dbufs dbufs_local;
1125 struct snd_sst_buffs ibufs, obufs;
1126 struct snd_sst_buff_entry *ibuf_tmp, *obuf_tmp;
1129 pr_debug("sst: SNDRV_SST_STREAM_DECODE recived\n");
1130 if (minor != STREAM_MODULE) {
1134 if (copy_from_user(¶m, (void __user *)arg,
1140 dbufs_local.input_bytes_consumed = param.input_bytes_consumed;
1141 dbufs_local.output_bytes_produced =
1142 param.output_bytes_produced;
1144 if (copy_from_user(&ibufs, param.ibufs, sizeof(ibufs))) {
1148 if (copy_from_user(&obufs, param.obufs, sizeof(obufs))) {
1153 ibuf_tmp = kcalloc(ibufs.entries, sizeof(*ibuf_tmp), GFP_KERNEL);
1154 obuf_tmp = kcalloc(obufs.entries, sizeof(*obuf_tmp), GFP_KERNEL);
1155 if (!ibuf_tmp || !obuf_tmp) {
1160 if (copy_from_user(ibuf_tmp, ibufs.buff_entry,
1161 ibufs.entries * sizeof(*ibuf_tmp))) {
1165 ibufs.buff_entry = ibuf_tmp;
1166 dbufs_local.ibufs = &ibufs;
1168 if (copy_from_user(obuf_tmp, obufs.buff_entry,
1169 obufs.entries * sizeof(*obuf_tmp))) {
1173 obufs.buff_entry = obuf_tmp;
1174 dbufs_local.obufs = &obufs;
1176 retval = sst_decode(str_id, &dbufs_local);
1182 dest = (char *)arg + offsetof(struct snd_sst_dbufs, input_bytes_consumed);
1183 if (copy_to_user(dest,
1184 &dbufs_local.input_bytes_consumed,
1185 sizeof(unsigned long long))) {
1190 dest = (char *)arg + offsetof(struct snd_sst_dbufs, input_bytes_consumed);
1191 if (copy_to_user(dest,
1192 &dbufs_local.output_bytes_produced,
1193 sizeof(unsigned long long))) {
1203 case _IOC_NR(SNDRV_SST_STREAM_DRAIN):
1204 pr_debug("sst: SNDRV_SST_STREAM_DRAIN recived\n");
1205 if (minor != STREAM_MODULE) {
1209 retval = sst_drain_stream(str_id);
1212 case _IOC_NR(SNDRV_SST_STREAM_BYTES_DECODED): {
1213 unsigned long long *bytes = (unsigned long long *)arg;
1214 struct snd_sst_tstamp tstamp = {0};
1216 pr_debug("sst: STREAM_BYTES_DECODED recieved!\n");
1217 if (minor != STREAM_MODULE) {
1221 memcpy_fromio(&tstamp,
1222 ((void *)(sst_drv_ctx->mailbox + SST_TIME_STAMP)
1223 +(str_id * sizeof(tstamp))),
1225 if (copy_to_user(bytes, &tstamp.bytes_processed,
1230 case _IOC_NR(SNDRV_SST_FW_INFO): {
1231 struct snd_sst_fw_info *fw_info;
1233 pr_debug("sst: SNDRV_SST_FW_INFO recived\n");
1235 fw_info = kzalloc(sizeof(*fw_info), GFP_ATOMIC);
1240 retval = sst_get_fw_info(fw_info);
1246 if (copy_to_user((struct snd_sst_dbufs *)arg,
1247 fw_info, sizeof(*fw_info))) {
1252 /*sst_print_fw_info(fw_info);*/
1259 pr_debug("sst: intel_sst_ioctl:complete ret code = %d\n", retval);