Duy Truong | 790f06d | 2013-02-13 16:38:12 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2010-2012, The Linux Foundation. All rights reserved. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/fs.h> |
| 16 | #include <linux/miscdevice.h> |
| 17 | #include <linux/uaccess.h> |
| 18 | #include <linux/sched.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/wait.h> |
| 21 | #include <linux/dma-mapping.h> |
| 22 | #include <linux/msm_audio_amrnb.h> |
| 23 | #include <asm/atomic.h> |
| 24 | #include <asm/ioctls.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 25 | #include "audio_utils.h" |
| 26 | |
| 27 | /* Buffer with meta*/ |
| 28 | #define PCM_BUF_SIZE (4096 + sizeof(struct meta_in)) |
| 29 | |
| 30 | /* Maximum 10 frames in buffer with meta */ |
| 31 | #define FRAME_SIZE (1 + ((32+sizeof(struct meta_out_dsp)) * 10)) |
| 32 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 33 | /* ------------------- device --------------------- */ |
| 34 | static long amrnb_in_ioctl(struct file *file, |
| 35 | unsigned int cmd, unsigned long arg) |
| 36 | { |
| 37 | struct q6audio_in *audio = file->private_data; |
| 38 | int rc = 0; |
| 39 | int cnt = 0; |
| 40 | |
| 41 | switch (cmd) { |
| 42 | case AUDIO_START: { |
| 43 | struct msm_audio_amrnb_enc_config_v2 *enc_cfg; |
| 44 | enc_cfg = audio->enc_cfg; |
| 45 | pr_debug("%s:session id %d: default buf alloc[%d]\n", __func__, |
| 46 | audio->ac->session, audio->buf_alloc); |
| 47 | if (audio->enabled == 1) { |
| 48 | pr_info("%s:AUDIO_START already over\n", __func__); |
| 49 | rc = 0; |
| 50 | break; |
| 51 | } |
| 52 | rc = audio_in_buf_alloc(audio); |
| 53 | if (rc < 0) { |
| 54 | pr_err("%s:session id %d: buffer allocation failed\n", |
| 55 | __func__, audio->ac->session); |
| 56 | break; |
| 57 | } |
| 58 | |
| 59 | rc = q6asm_enc_cfg_blk_amrnb(audio->ac, |
| 60 | audio->buf_cfg.frames_per_buf, |
| 61 | enc_cfg->band_mode, |
| 62 | enc_cfg->dtx_enable); |
| 63 | |
| 64 | if (rc < 0) { |
Harmandeep Singh | c35fa07d | 2012-05-31 07:08:59 -0700 | [diff] [blame] | 65 | pr_err("%s:session id %d: cmd amrnb media format block" |
| 66 | "failed\n", __func__, audio->ac->session); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 67 | break; |
| 68 | } |
| 69 | if (audio->feedback == NON_TUNNEL_MODE) { |
| 70 | rc = q6asm_media_format_block_pcm(audio->ac, |
| 71 | audio->pcm_cfg.sample_rate, |
| 72 | audio->pcm_cfg.channel_count); |
| 73 | |
| 74 | if (rc < 0) { |
Harmandeep Singh | c35fa07d | 2012-05-31 07:08:59 -0700 | [diff] [blame] | 75 | pr_err("%s:session id %d: media format block" |
| 76 | "failed\n", __func__, audio->ac->session); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 77 | break; |
| 78 | } |
| 79 | } |
| 80 | pr_debug("%s:session id %d: AUDIO_START enable[%d]\n", |
| 81 | __func__, audio->ac->session, |
| 82 | audio->enabled); |
| 83 | rc = audio_in_enable(audio); |
| 84 | if (!rc) { |
| 85 | audio->enabled = 1; |
| 86 | } else { |
| 87 | audio->enabled = 0; |
Harmandeep Singh | c35fa07d | 2012-05-31 07:08:59 -0700 | [diff] [blame] | 88 | pr_err("%s:session id %d: Audio Start procedure failed" |
| 89 | "rc=%d\n", __func__, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 90 | audio->ac->session, rc); |
| 91 | break; |
| 92 | } |
| 93 | while (cnt++ < audio->str_cfg.buffer_count) |
| 94 | q6asm_read(audio->ac); /* Push buffer to DSP */ |
| 95 | rc = 0; |
| 96 | pr_debug("%s:session id %d: AUDIO_START success enable[%d]\n", |
| 97 | __func__, audio->ac->session, audio->enabled); |
| 98 | break; |
| 99 | } |
| 100 | case AUDIO_STOP: { |
| 101 | pr_debug("%s:AUDIO_STOP\n", __func__); |
| 102 | rc = audio_in_disable(audio); |
| 103 | if (rc < 0) { |
Harmandeep Singh | c35fa07d | 2012-05-31 07:08:59 -0700 | [diff] [blame] | 104 | pr_err("%s:session id %d: Audio Stop procedure failed" |
| 105 | "rc=%d\n", __func__, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 106 | audio->ac->session, rc); |
| 107 | break; |
| 108 | } |
| 109 | break; |
| 110 | } |
| 111 | case AUDIO_GET_AMRNB_ENC_CONFIG_V2: { |
| 112 | if (copy_to_user((void *)arg, audio->enc_cfg, |
| 113 | sizeof(struct msm_audio_amrnb_enc_config_v2))) |
| 114 | rc = -EFAULT; |
| 115 | break; |
| 116 | } |
| 117 | case AUDIO_SET_AMRNB_ENC_CONFIG_V2: { |
| 118 | struct msm_audio_amrnb_enc_config_v2 cfg; |
| 119 | struct msm_audio_amrnb_enc_config_v2 *enc_cfg; |
| 120 | enc_cfg = audio->enc_cfg; |
| 121 | if (copy_from_user(&cfg, (void *) arg, |
| 122 | sizeof(struct msm_audio_amrnb_enc_config_v2))) { |
| 123 | rc = -EFAULT; |
| 124 | break; |
| 125 | } |
| 126 | if (cfg.band_mode > 8 || |
| 127 | cfg.band_mode < 1) { |
| 128 | pr_err("%s:session id %d: invalid band mode\n", |
| 129 | __func__, audio->ac->session); |
| 130 | rc = -EINVAL; |
| 131 | break; |
| 132 | } |
| 133 | /* AMR NB encoder accepts values between 0-7 |
| 134 | while openmax provides value between 1-8 |
| 135 | as per spec */ |
| 136 | enc_cfg->band_mode = (cfg.band_mode - 1); |
| 137 | enc_cfg->dtx_enable = (cfg.dtx_enable ? 1 : 0); |
| 138 | enc_cfg->frame_format = 0; |
| 139 | pr_debug("%s:session id %d: band_mode = 0x%x dtx_enable=0x%x\n", |
| 140 | __func__, audio->ac->session, |
| 141 | enc_cfg->band_mode, enc_cfg->dtx_enable); |
| 142 | break; |
| 143 | } |
| 144 | default: |
| 145 | rc = -EINVAL; |
| 146 | } |
| 147 | return rc; |
| 148 | } |
| 149 | |
| 150 | static int amrnb_in_open(struct inode *inode, struct file *file) |
| 151 | { |
| 152 | struct q6audio_in *audio = NULL; |
| 153 | struct msm_audio_amrnb_enc_config_v2 *enc_cfg; |
| 154 | int rc = 0; |
| 155 | |
| 156 | audio = kzalloc(sizeof(struct q6audio_in), GFP_KERNEL); |
| 157 | |
| 158 | if (audio == NULL) { |
Harmandeep Singh | c35fa07d | 2012-05-31 07:08:59 -0700 | [diff] [blame] | 159 | pr_err("%s Could not allocate memory for amrnb" |
| 160 | "driver\n", __func__); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 161 | return -ENOMEM; |
| 162 | } |
| 163 | /* Allocate memory for encoder config param */ |
| 164 | audio->enc_cfg = kzalloc(sizeof(struct msm_audio_amrnb_enc_config_v2), |
| 165 | GFP_KERNEL); |
| 166 | if (audio->enc_cfg == NULL) { |
Harmandeep Singh | c35fa07d | 2012-05-31 07:08:59 -0700 | [diff] [blame] | 167 | pr_err("%s:session id %d: Could not allocate memory for aac" |
| 168 | "config param\n", __func__, audio->ac->session); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 169 | kfree(audio); |
| 170 | return -ENOMEM; |
| 171 | } |
| 172 | enc_cfg = audio->enc_cfg; |
| 173 | |
| 174 | mutex_init(&audio->lock); |
| 175 | mutex_init(&audio->read_lock); |
| 176 | mutex_init(&audio->write_lock); |
| 177 | spin_lock_init(&audio->dsp_lock); |
| 178 | init_waitqueue_head(&audio->read_wait); |
| 179 | init_waitqueue_head(&audio->write_wait); |
| 180 | |
| 181 | /* Settings will be re-config at AUDIO_SET_CONFIG, |
| 182 | * but at least we need to have initial config |
| 183 | */ |
| 184 | audio->str_cfg.buffer_size = FRAME_SIZE; |
| 185 | audio->str_cfg.buffer_count = FRAME_NUM; |
| 186 | audio->min_frame_size = 32; |
| 187 | audio->max_frames_per_buf = 10; |
| 188 | audio->pcm_cfg.buffer_size = PCM_BUF_SIZE; |
| 189 | audio->pcm_cfg.buffer_count = PCM_BUF_COUNT; |
| 190 | enc_cfg->band_mode = 7; |
| 191 | enc_cfg->dtx_enable = 0; |
| 192 | audio->pcm_cfg.channel_count = 1; |
| 193 | audio->pcm_cfg.sample_rate = 8000; |
| 194 | audio->buf_cfg.meta_info_enable = 0x01; |
| 195 | audio->buf_cfg.frames_per_buf = 0x01; |
| 196 | |
Harmandeep Singh | c35fa07d | 2012-05-31 07:08:59 -0700 | [diff] [blame] | 197 | audio->ac = q6asm_audio_client_alloc((app_cb)q6asm_in_cb, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 198 | (void *)audio); |
| 199 | |
| 200 | if (!audio->ac) { |
Harmandeep Singh | c35fa07d | 2012-05-31 07:08:59 -0700 | [diff] [blame] | 201 | pr_err("%s: Could not allocate memory for audio" |
| 202 | "client\n", __func__); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 203 | kfree(audio->enc_cfg); |
| 204 | kfree(audio); |
| 205 | return -ENOMEM; |
| 206 | } |
| 207 | |
| 208 | /* open amrnb encoder in T/NT mode */ |
| 209 | if ((file->f_mode & FMODE_WRITE) && |
| 210 | (file->f_mode & FMODE_READ)) { |
| 211 | audio->feedback = NON_TUNNEL_MODE; |
| 212 | rc = q6asm_open_read_write(audio->ac, FORMAT_AMRNB, |
| 213 | FORMAT_LINEAR_PCM); |
| 214 | if (rc < 0) { |
| 215 | pr_err("%s:session id %d: NT mode Open failed rc=%d\n", |
| 216 | __func__, audio->ac->session, rc); |
| 217 | rc = -ENODEV; |
| 218 | goto fail; |
| 219 | } |
| 220 | pr_info("%s:session id %d: NT mode encoder success\n", |
| 221 | __func__, audio->ac->session); |
| 222 | } else if (!(file->f_mode & FMODE_WRITE) && |
| 223 | (file->f_mode & FMODE_READ)) { |
| 224 | audio->feedback = TUNNEL_MODE; |
| 225 | rc = q6asm_open_read(audio->ac, FORMAT_AMRNB); |
| 226 | if (rc < 0) { |
| 227 | pr_err("%s:session id %d: T mode Open failed rc=%d\n", |
| 228 | __func__, audio->ac->session, rc); |
| 229 | rc = -ENODEV; |
| 230 | goto fail; |
| 231 | } |
| 232 | /* register for tx overflow (valid for tunnel mode only) */ |
| 233 | rc = q6asm_reg_tx_overflow(audio->ac, 0x01); |
| 234 | if (rc < 0) { |
Harmandeep Singh | c35fa07d | 2012-05-31 07:08:59 -0700 | [diff] [blame] | 235 | pr_err("%s:session id %d: TX Overflow registration" |
| 236 | "failed rc=%d\n", __func__, audio->ac->session, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 237 | rc); |
| 238 | rc = -ENODEV; |
| 239 | goto fail; |
| 240 | } |
| 241 | pr_info("%s:session id %d: T mode encoder success\n", |
| 242 | __func__, audio->ac->session); |
| 243 | } else { |
| 244 | pr_err("%s:session id %d: Unexpected mode\n", __func__, |
| 245 | audio->ac->session); |
| 246 | rc = -EACCES; |
| 247 | goto fail; |
| 248 | } |
| 249 | |
| 250 | audio->opened = 1; |
| 251 | atomic_set(&audio->in_count, PCM_BUF_COUNT); |
| 252 | atomic_set(&audio->out_count, 0x00); |
| 253 | audio->enc_ioctl = amrnb_in_ioctl; |
| 254 | file->private_data = audio; |
| 255 | |
| 256 | pr_info("%s:session id %d: success\n", __func__, audio->ac->session); |
| 257 | return 0; |
| 258 | fail: |
| 259 | q6asm_audio_client_free(audio->ac); |
| 260 | kfree(audio->enc_cfg); |
| 261 | kfree(audio); |
| 262 | return rc; |
| 263 | } |
| 264 | |
| 265 | static const struct file_operations audio_in_fops = { |
| 266 | .owner = THIS_MODULE, |
| 267 | .open = amrnb_in_open, |
| 268 | .release = audio_in_release, |
| 269 | .read = audio_in_read, |
| 270 | .write = audio_in_write, |
| 271 | .unlocked_ioctl = audio_in_ioctl, |
| 272 | }; |
| 273 | |
| 274 | struct miscdevice audio_amrnb_in_misc = { |
| 275 | .minor = MISC_DYNAMIC_MINOR, |
| 276 | .name = "msm_amrnb_in", |
| 277 | .fops = &audio_in_fops, |
| 278 | }; |
| 279 | |
| 280 | static int __init amrnb_in_init(void) |
| 281 | { |
| 282 | return misc_register(&audio_amrnb_in_misc); |
| 283 | } |
| 284 | |
| 285 | device_initcall(amrnb_in_init); |