Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 Google, Inc. |
| 3 | * Copyright (C) 2009 HTC Corporation |
Duy Truong | 790f06d | 2013-02-13 16:38:12 -0800 | [diff] [blame] | 4 | * Copyright (c) 2010, The Linux Foundation. All rights reserved. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 5 | * |
| 6 | * This software is licensed under the terms of the GNU General Public |
| 7 | * License version 2, as published by the Free Software Foundation, and |
| 8 | * may be copied, distributed, and modified under those terms. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | */ |
| 16 | |
| 17 | #include <linux/fs.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/miscdevice.h> |
| 20 | #include <linux/mutex.h> |
| 21 | #include <linux/sched.h> |
| 22 | #include <linux/wait.h> |
| 23 | #include <linux/uaccess.h> |
| 24 | |
| 25 | #include <linux/msm_audio_amrnb.h> |
| 26 | #include <mach/msm_qdsp6_audiov2.h> |
| 27 | #include "dal_audio.h" |
| 28 | #include "dal_audio_format.h" |
| 29 | #include <mach/debug_mm.h> |
| 30 | |
| 31 | |
| 32 | struct amrnb { |
| 33 | struct mutex lock; |
| 34 | struct msm_audio_amrnb_enc_config_v2 cfg; |
| 35 | struct msm_audio_stream_config str_cfg; |
| 36 | struct audio_client *audio_client; |
| 37 | }; |
| 38 | |
| 39 | |
| 40 | static long q6_amrnb_in_ioctl(struct file *file, unsigned int cmd, |
| 41 | unsigned long arg) |
| 42 | { |
| 43 | struct amrnb *amrnb = file->private_data; |
| 44 | struct adsp_open_command rpc; |
| 45 | int rc = 0; |
| 46 | |
| 47 | if (cmd == AUDIO_GET_STATS) { |
| 48 | struct msm_audio_stats stats; |
| 49 | memset(&stats, 0, sizeof(stats)); |
| 50 | if (copy_to_user((void *) arg, &stats, sizeof(stats))) |
| 51 | return -EFAULT; |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | mutex_lock(&amrnb->lock); |
| 56 | switch (cmd) { |
| 57 | case AUDIO_START: |
| 58 | if (amrnb->audio_client) { |
| 59 | rc = -EBUSY; |
| 60 | break; |
| 61 | } else { |
| 62 | amrnb->audio_client = q6audio_open(AUDIO_FLAG_READ, |
| 63 | amrnb->str_cfg.buffer_size); |
| 64 | |
| 65 | if (!amrnb->audio_client) { |
| 66 | kfree(amrnb); |
| 67 | rc = -ENOMEM; |
| 68 | break; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | tx_clk_freq = 8000; |
| 73 | |
| 74 | memset(&rpc, 0, sizeof(rpc)); |
| 75 | |
| 76 | rpc.format_block.standard.format = ADSP_AUDIO_FORMAT_AMRNB_FS; |
| 77 | rpc.format_block.standard.channels = 1; |
| 78 | rpc.format_block.standard.bits_per_sample = 16; |
| 79 | rpc.format_block.standard.sampling_rate = 8000; |
| 80 | rpc.format_block.standard.is_signed = 1; |
| 81 | rpc.format_block.standard.is_interleaved = 0; |
| 82 | |
| 83 | rpc.hdr.opcode = ADSP_AUDIO_IOCTL_CMD_OPEN_READ; |
| 84 | rpc.device = ADSP_AUDIO_DEVICE_ID_DEFAULT; |
| 85 | rpc.stream_context = ADSP_AUDIO_DEVICE_CONTEXT_RECORD; |
| 86 | rpc.buf_max_size = amrnb->str_cfg.buffer_size; |
| 87 | rpc.config.amr.mode = amrnb->cfg.band_mode; |
| 88 | rpc.config.amr.dtx_mode = amrnb->cfg.dtx_enable; |
| 89 | rpc.config.amr.enable = 1; |
| 90 | q6audio_start(amrnb->audio_client, &rpc, sizeof(rpc)); |
| 91 | break; |
| 92 | case AUDIO_STOP: |
| 93 | break; |
| 94 | case AUDIO_FLUSH: |
| 95 | break; |
| 96 | case AUDIO_SET_VOLUME: |
| 97 | break; |
| 98 | case AUDIO_GET_STREAM_CONFIG: |
| 99 | if (copy_to_user((void *)arg, &amrnb->str_cfg, |
| 100 | sizeof(struct msm_audio_stream_config))) |
| 101 | rc = -EFAULT; |
| 102 | break; |
| 103 | case AUDIO_SET_STREAM_CONFIG: |
| 104 | if (copy_from_user(&amrnb->str_cfg, (void *)arg, |
| 105 | sizeof(struct msm_audio_stream_config))) { |
| 106 | rc = -EFAULT; |
| 107 | break; |
| 108 | } |
| 109 | |
| 110 | if (amrnb->str_cfg.buffer_size < 768) { |
| 111 | pr_err("[%s:%s] Buffer size too small\n", __MM_FILE__, |
| 112 | __func__); |
| 113 | rc = -EINVAL; |
| 114 | break; |
| 115 | } |
| 116 | |
| 117 | if (amrnb->str_cfg.buffer_count != 2) |
| 118 | pr_info("[%s:%s] Buffer count set to 2\n", __MM_FILE__, |
| 119 | __func__); |
| 120 | break; |
| 121 | case AUDIO_SET_AMRNB_ENC_CONFIG: |
| 122 | if (copy_from_user(&amrnb->cfg, (void *) arg, |
| 123 | sizeof(struct msm_audio_amrnb_enc_config_v2))) |
| 124 | rc = -EFAULT; |
| 125 | break; |
| 126 | case AUDIO_GET_AMRNB_ENC_CONFIG: |
| 127 | if (copy_to_user((void *) arg, &amrnb->cfg, |
| 128 | sizeof(struct msm_audio_amrnb_enc_config_v2))) |
| 129 | rc = -EFAULT; |
| 130 | break; |
| 131 | |
| 132 | default: |
| 133 | rc = -EINVAL; |
| 134 | } |
| 135 | |
| 136 | mutex_unlock(&amrnb->lock); |
| 137 | return rc; |
| 138 | } |
| 139 | |
| 140 | static int q6_amrnb_in_open(struct inode *inode, struct file *file) |
| 141 | { |
| 142 | struct amrnb *amrnb; |
| 143 | amrnb = kmalloc(sizeof(struct amrnb), GFP_KERNEL); |
| 144 | if (amrnb == NULL) { |
| 145 | pr_err("[%s:%s] Could not allocate memory for amrnb driver\n", |
| 146 | __MM_FILE__, __func__); |
| 147 | return -ENOMEM; |
| 148 | } |
| 149 | |
| 150 | mutex_init(&amrnb->lock); |
| 151 | file->private_data = amrnb; |
| 152 | amrnb->audio_client = NULL; |
| 153 | amrnb->str_cfg.buffer_size = 768; |
| 154 | amrnb->str_cfg.buffer_count = 2; |
| 155 | amrnb->cfg.band_mode = ADSP_AUDIO_AMR_MR475; |
| 156 | amrnb->cfg.dtx_enable = ADSP_AUDIO_AMR_DTX_MODE_ON_AUTO; |
| 157 | amrnb->cfg.frame_format = ADSP_AUDIO_FORMAT_AMRNB_FS; |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | static ssize_t q6_amrnb_in_read(struct file *file, char __user *buf, |
| 162 | size_t count, loff_t *pos) |
| 163 | { |
| 164 | struct audio_client *ac; |
| 165 | struct audio_buffer *ab; |
| 166 | const char __user *start = buf; |
| 167 | struct amrnb *amrnb = file->private_data; |
| 168 | int xfer = 0; |
| 169 | int res; |
| 170 | |
| 171 | mutex_lock(&amrnb->lock); |
| 172 | ac = amrnb->audio_client; |
| 173 | if (!ac) { |
| 174 | res = -ENODEV; |
| 175 | goto fail; |
| 176 | } |
| 177 | while (count > xfer) { |
| 178 | ab = ac->buf + ac->cpu_buf; |
| 179 | |
| 180 | if (ab->used) |
| 181 | wait_event(ac->wait, (ab->used == 0)); |
| 182 | |
| 183 | xfer = ab->actual_size; |
| 184 | |
| 185 | if (copy_to_user(buf, ab->data, xfer)) { |
| 186 | res = -EFAULT; |
| 187 | goto fail; |
| 188 | } |
| 189 | |
| 190 | buf += xfer; |
| 191 | count -= xfer; |
| 192 | |
| 193 | ab->used = 1; |
| 194 | q6audio_read(ac, ab); |
| 195 | ac->cpu_buf ^= 1; |
| 196 | } |
| 197 | |
| 198 | res = buf - start; |
| 199 | fail: |
| 200 | mutex_unlock(&amrnb->lock); |
| 201 | |
| 202 | return res; |
| 203 | } |
| 204 | |
| 205 | static int q6_amrnb_in_release(struct inode *inode, struct file *file) |
| 206 | { |
| 207 | int rc = 0; |
| 208 | struct amrnb *amrnb = file->private_data; |
| 209 | |
| 210 | mutex_lock(&amrnb->lock); |
| 211 | if (amrnb->audio_client) |
| 212 | rc = q6audio_close(amrnb->audio_client); |
| 213 | mutex_unlock(&amrnb->lock); |
| 214 | kfree(amrnb); |
| 215 | return rc; |
| 216 | } |
| 217 | |
| 218 | static const struct file_operations q6_amrnb_in_fops = { |
| 219 | .owner = THIS_MODULE, |
| 220 | .open = q6_amrnb_in_open, |
| 221 | .read = q6_amrnb_in_read, |
| 222 | .release = q6_amrnb_in_release, |
| 223 | .unlocked_ioctl = q6_amrnb_in_ioctl, |
| 224 | }; |
| 225 | |
| 226 | struct miscdevice q6_amrnb_in_misc = { |
| 227 | .minor = MISC_DYNAMIC_MINOR, |
| 228 | .name = "msm_amr_in", |
| 229 | .fops = &q6_amrnb_in_fops, |
| 230 | }; |
| 231 | |
| 232 | static int __init q6_amrnb_in_init(void) |
| 233 | { |
| 234 | return misc_register(&q6_amrnb_in_misc); |
| 235 | } |
| 236 | |
| 237 | device_initcall(q6_amrnb_in_init); |