Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 1 | /* Copyright (c) 2010-2017, The Linux Foundation. All rights reserved. |
| 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/wait.h> |
| 20 | #include <linux/dma-mapping.h> |
| 21 | #include <linux/slab.h> |
| 22 | #include <linux/msm_audio_qcp.h> |
| 23 | #include <linux/atomic.h> |
| 24 | #include <linux/compat.h> |
| 25 | #include <asm/ioctls.h> |
| 26 | #include "audio_utils.h" |
| 27 | |
| 28 | /* Buffer with meta*/ |
| 29 | #define PCM_BUF_SIZE (4096 + sizeof(struct meta_in)) |
| 30 | |
| 31 | /* Maximum 10 frames in buffer with meta */ |
| 32 | #define FRAME_SIZE (1 + ((35+sizeof(struct meta_out_dsp)) * 10)) |
| 33 | |
| 34 | static long qcelp_in_ioctl_shared(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_qcelp_enc_config *enc_cfg; |
| 44 | |
| 45 | enc_cfg = audio->enc_cfg; |
| 46 | pr_debug("%s:session id %d: default buf alloc[%d]\n", __func__, |
| 47 | audio->ac->session, audio->buf_alloc); |
| 48 | if (audio->enabled == 1) { |
| 49 | pr_info("%s:AUDIO_START already over\n", __func__); |
| 50 | rc = 0; |
| 51 | break; |
| 52 | } |
| 53 | rc = audio_in_buf_alloc(audio); |
| 54 | if (rc < 0) { |
| 55 | pr_err("%s:session id %d: buffer allocation failed\n", |
| 56 | __func__, audio->ac->session); |
| 57 | break; |
| 58 | } |
| 59 | |
| 60 | /* reduced_rate_level, rate_modulation_cmd set to zero |
| 61 | * currently not configurable from user space |
| 62 | */ |
| 63 | rc = q6asm_enc_cfg_blk_qcelp(audio->ac, |
| 64 | audio->buf_cfg.frames_per_buf, |
| 65 | enc_cfg->min_bit_rate, |
| 66 | enc_cfg->max_bit_rate, 0, 0); |
| 67 | |
| 68 | if (rc < 0) { |
| 69 | pr_err("%s:session id %d: cmd qcelp media format block failed\n", |
| 70 | __func__, audio->ac->session); |
| 71 | break; |
| 72 | } |
| 73 | if (audio->feedback == NON_TUNNEL_MODE) { |
| 74 | rc = q6asm_media_format_block_pcm(audio->ac, |
| 75 | audio->pcm_cfg.sample_rate, |
| 76 | audio->pcm_cfg.channel_count); |
| 77 | |
| 78 | if (rc < 0) { |
| 79 | pr_err("%s:session id %d: media format block failed\n", |
| 80 | __func__, audio->ac->session); |
| 81 | break; |
| 82 | } |
| 83 | } |
| 84 | pr_debug("%s:session id %d: AUDIO_START enable[%d]\n", __func__, |
| 85 | audio->ac->session, audio->enabled); |
| 86 | rc = audio_in_enable(audio); |
| 87 | if (!rc) { |
| 88 | audio->enabled = 1; |
| 89 | } else { |
| 90 | audio->enabled = 0; |
| 91 | pr_err("%s:session id %d: Audio Start procedure failed rc=%d\n", |
| 92 | __func__, audio->ac->session, rc); |
| 93 | break; |
| 94 | } |
| 95 | while (cnt++ < audio->str_cfg.buffer_count) |
| 96 | q6asm_read(audio->ac); /* Push buffer to DSP */ |
| 97 | rc = 0; |
| 98 | pr_debug("%s:session id %d: AUDIO_START success enable[%d]\n", |
| 99 | __func__, audio->ac->session, audio->enabled); |
| 100 | break; |
| 101 | } |
| 102 | case AUDIO_STOP: { |
| 103 | pr_debug("%s:session id %d: AUDIO_STOP\n", __func__, |
| 104 | audio->ac->session); |
| 105 | rc = audio_in_disable(audio); |
| 106 | if (rc < 0) { |
| 107 | pr_err("%s:session id %d: Audio Stop procedure failed rc=%d\n", |
| 108 | __func__, audio->ac->session, |
| 109 | rc); |
| 110 | break; |
| 111 | } |
| 112 | break; |
| 113 | } |
| 114 | case AUDIO_SET_QCELP_ENC_CONFIG: { |
| 115 | struct msm_audio_qcelp_enc_config *cfg; |
| 116 | struct msm_audio_qcelp_enc_config *enc_cfg; |
| 117 | |
| 118 | enc_cfg = audio->enc_cfg; |
| 119 | cfg = (struct msm_audio_qcelp_enc_config *)arg; |
| 120 | if (cfg == NULL) { |
| 121 | pr_err("%s: NULL config pointer\n", __func__); |
| 122 | rc = -EINVAL; |
| 123 | break; |
| 124 | } |
| 125 | if (cfg->min_bit_rate > 4 || |
| 126 | cfg->min_bit_rate < 1) { |
| 127 | pr_err("%s:session id %d: invalid min bitrate\n", |
| 128 | __func__, audio->ac->session); |
| 129 | rc = -EINVAL; |
| 130 | break; |
| 131 | } |
| 132 | if (cfg->max_bit_rate > 4 || |
| 133 | cfg->max_bit_rate < 1) { |
| 134 | pr_err("%s:session id %d: invalid max bitrate\n", |
| 135 | __func__, audio->ac->session); |
| 136 | rc = -EINVAL; |
| 137 | break; |
| 138 | } |
| 139 | enc_cfg->cdma_rate = cfg->cdma_rate; |
| 140 | enc_cfg->min_bit_rate = cfg->min_bit_rate; |
| 141 | enc_cfg->max_bit_rate = cfg->max_bit_rate; |
| 142 | pr_debug("%s:session id %d: min_bit_rate= 0x%x max_bit_rate=0x%x\n", |
| 143 | __func__, |
| 144 | audio->ac->session, enc_cfg->min_bit_rate, |
| 145 | enc_cfg->max_bit_rate); |
| 146 | break; |
| 147 | } |
| 148 | default: |
| 149 | pr_err("%s: Unknown ioctl cmd = %d", __func__, cmd); |
| 150 | rc = -EINVAL; |
| 151 | } |
| 152 | return rc; |
| 153 | } |
| 154 | |
| 155 | static long qcelp_in_ioctl(struct file *file, |
| 156 | unsigned int cmd, unsigned long arg) |
| 157 | { |
| 158 | struct q6audio_in *audio = file->private_data; |
| 159 | int rc = 0; |
| 160 | |
| 161 | switch (cmd) { |
| 162 | case AUDIO_START: |
| 163 | case AUDIO_STOP: { |
| 164 | rc = qcelp_in_ioctl_shared(file, cmd, arg); |
| 165 | break; |
| 166 | } |
| 167 | case AUDIO_GET_QCELP_ENC_CONFIG: { |
| 168 | if (copy_to_user((void *)arg, audio->enc_cfg, |
| 169 | sizeof(struct msm_audio_qcelp_enc_config))) { |
| 170 | pr_err( |
| 171 | "%s: copy_to_user for AUDIO_GET_QCELP_ENC_CONFIG failed", |
| 172 | __func__); |
| 173 | rc = -EFAULT; |
| 174 | } |
| 175 | break; |
| 176 | } |
| 177 | case AUDIO_SET_QCELP_ENC_CONFIG: { |
| 178 | struct msm_audio_qcelp_enc_config cfg; |
| 179 | |
| 180 | if (copy_from_user(&cfg, (void *) arg, |
| 181 | sizeof(cfg))) { |
| 182 | pr_err( |
| 183 | "%s: copy_from_user for AUDIO_SET_QCELP_ENC_CONFIG failed", |
| 184 | __func__); |
| 185 | rc = -EFAULT; |
| 186 | break; |
| 187 | } |
| 188 | rc = qcelp_in_ioctl_shared(file, cmd, (unsigned long)&cfg); |
| 189 | if (rc) |
| 190 | pr_err("%s:AUDIO_SET_QCELP_ENC_CONFIG failed. Rc= %d\n", |
| 191 | __func__, rc); |
| 192 | break; |
| 193 | } |
| 194 | default: |
| 195 | pr_err("%s: Unknown ioctl cmd = %d", __func__, cmd); |
| 196 | rc = -EINVAL; |
| 197 | } |
| 198 | return rc; |
| 199 | } |
| 200 | |
| 201 | #ifdef CONFIG_COMPAT |
| 202 | struct msm_audio_qcelp_enc_config32 { |
| 203 | u32 cdma_rate; |
| 204 | u32 min_bit_rate; |
| 205 | u32 max_bit_rate; |
| 206 | }; |
| 207 | |
| 208 | enum { |
| 209 | AUDIO_SET_QCELP_ENC_CONFIG_32 = _IOW(AUDIO_IOCTL_MAGIC, |
| 210 | 0, struct msm_audio_qcelp_enc_config32), |
| 211 | AUDIO_GET_QCELP_ENC_CONFIG_32 = _IOR(AUDIO_IOCTL_MAGIC, |
| 212 | 1, struct msm_audio_qcelp_enc_config32) |
| 213 | }; |
| 214 | |
| 215 | static long qcelp_in_compat_ioctl(struct file *file, |
| 216 | unsigned int cmd, unsigned long arg) |
| 217 | { |
| 218 | struct q6audio_in *audio = file->private_data; |
| 219 | int rc = 0; |
| 220 | |
| 221 | switch (cmd) { |
| 222 | case AUDIO_START: |
| 223 | case AUDIO_STOP: { |
| 224 | rc = qcelp_in_ioctl_shared(file, cmd, arg); |
| 225 | break; |
| 226 | } |
| 227 | case AUDIO_GET_QCELP_ENC_CONFIG_32: { |
| 228 | struct msm_audio_qcelp_enc_config32 cfg_32; |
| 229 | struct msm_audio_qcelp_enc_config *enc_cfg; |
| 230 | |
| 231 | memset(&cfg_32, 0, sizeof(cfg_32)); |
| 232 | |
| 233 | enc_cfg = (struct msm_audio_qcelp_enc_config *)audio->enc_cfg; |
| 234 | cfg_32.cdma_rate = enc_cfg->cdma_rate; |
| 235 | cfg_32.min_bit_rate = enc_cfg->min_bit_rate; |
| 236 | cfg_32.max_bit_rate = enc_cfg->max_bit_rate; |
| 237 | if (copy_to_user((void *)arg, &cfg_32, |
| 238 | sizeof(cfg_32))) { |
| 239 | pr_err("%s: copy_to_user for AUDIO_GET_QCELP_ENC_CONFIG_32 failed\n", |
| 240 | __func__); |
| 241 | rc = -EFAULT; |
| 242 | } |
| 243 | break; |
| 244 | } |
| 245 | case AUDIO_SET_QCELP_ENC_CONFIG_32: { |
| 246 | struct msm_audio_qcelp_enc_config32 cfg_32; |
| 247 | struct msm_audio_qcelp_enc_config cfg; |
| 248 | |
| 249 | if (copy_from_user(&cfg_32, (void *) arg, |
| 250 | sizeof(cfg_32))) { |
| 251 | pr_err("%s: copy_from_user for AUDIO_SET_QCELP_ENC_CONFIG_32 failed\n", |
| 252 | __func__); |
| 253 | rc = -EFAULT; |
| 254 | break; |
| 255 | } |
| 256 | cfg.cdma_rate = cfg_32.cdma_rate; |
| 257 | cfg.min_bit_rate = cfg_32.min_bit_rate; |
| 258 | cfg.max_bit_rate = cfg_32.max_bit_rate; |
| 259 | cmd = AUDIO_SET_QCELP_ENC_CONFIG; |
| 260 | rc = qcelp_in_ioctl_shared(file, cmd, (unsigned long)&cfg); |
| 261 | if (rc) |
| 262 | pr_err("%s:AUDIO_SET_QCELP_ENC_CONFIG failed. rc= %d\n", |
| 263 | __func__, rc); |
| 264 | break; |
| 265 | } |
| 266 | default: |
| 267 | pr_err("%s: Unknown ioctl cmd = %d", __func__, cmd); |
| 268 | rc = -EINVAL; |
| 269 | } |
| 270 | return rc; |
| 271 | } |
| 272 | #else |
| 273 | #define qcelp_in_compat_ioctl NULL |
| 274 | #endif |
| 275 | |
| 276 | static int qcelp_in_open(struct inode *inode, struct file *file) |
| 277 | { |
| 278 | struct q6audio_in *audio = NULL; |
| 279 | struct msm_audio_qcelp_enc_config *enc_cfg; |
| 280 | int rc = 0; |
| 281 | |
| 282 | audio = kzalloc(sizeof(struct q6audio_in), GFP_KERNEL); |
| 283 | |
| 284 | if (audio == NULL) |
| 285 | return -ENOMEM; |
| 286 | |
| 287 | /* Allocate memory for encoder config param */ |
| 288 | audio->enc_cfg = kzalloc(sizeof(struct msm_audio_qcelp_enc_config), |
| 289 | GFP_KERNEL); |
| 290 | if (audio->enc_cfg == NULL) { |
| 291 | kfree(audio); |
| 292 | return -ENOMEM; |
| 293 | } |
| 294 | enc_cfg = audio->enc_cfg; |
| 295 | |
| 296 | mutex_init(&audio->lock); |
| 297 | mutex_init(&audio->read_lock); |
| 298 | mutex_init(&audio->write_lock); |
| 299 | spin_lock_init(&audio->dsp_lock); |
| 300 | init_waitqueue_head(&audio->read_wait); |
| 301 | init_waitqueue_head(&audio->write_wait); |
| 302 | |
| 303 | /* Settings will be re-config at AUDIO_SET_CONFIG, |
| 304 | * but at least we need to have initial config |
| 305 | */ |
| 306 | audio->str_cfg.buffer_size = FRAME_SIZE; |
| 307 | audio->str_cfg.buffer_count = FRAME_NUM; |
| 308 | audio->min_frame_size = 35; |
| 309 | audio->max_frames_per_buf = 10; |
| 310 | audio->pcm_cfg.buffer_size = PCM_BUF_SIZE; |
| 311 | audio->pcm_cfg.buffer_count = PCM_BUF_COUNT; |
| 312 | enc_cfg->min_bit_rate = 4; |
| 313 | enc_cfg->max_bit_rate = 4; |
| 314 | audio->pcm_cfg.channel_count = 1; |
| 315 | audio->pcm_cfg.sample_rate = 8000; |
| 316 | audio->buf_cfg.meta_info_enable = 0x01; |
| 317 | audio->buf_cfg.frames_per_buf = 0x01; |
| 318 | audio->event_abort = 0; |
| 319 | |
| 320 | audio->ac = q6asm_audio_client_alloc((app_cb)q6asm_in_cb, |
| 321 | (void *)audio); |
| 322 | |
| 323 | if (!audio->ac) { |
| 324 | pr_err("%s: Could not allocate memory for audio client\n", |
| 325 | __func__); |
| 326 | kfree(audio->enc_cfg); |
| 327 | kfree(audio); |
| 328 | return -ENOMEM; |
| 329 | } |
| 330 | |
| 331 | /* open qcelp encoder in T/NT mode */ |
| 332 | if ((file->f_mode & FMODE_WRITE) && |
| 333 | (file->f_mode & FMODE_READ)) { |
| 334 | audio->feedback = NON_TUNNEL_MODE; |
| 335 | rc = q6asm_open_read_write(audio->ac, FORMAT_V13K, |
| 336 | FORMAT_LINEAR_PCM); |
| 337 | if (rc < 0) { |
| 338 | pr_err("%s:session id %d: NT mode Open failed rc=%d\n", |
| 339 | __func__, audio->ac->session, rc); |
| 340 | rc = -ENODEV; |
| 341 | goto fail; |
| 342 | } |
| 343 | pr_info("%s:session id %d: NT mode encoder success\n", __func__, |
| 344 | audio->ac->session); |
| 345 | } else if (!(file->f_mode & FMODE_WRITE) && |
| 346 | (file->f_mode & FMODE_READ)) { |
| 347 | audio->feedback = TUNNEL_MODE; |
| 348 | rc = q6asm_open_read(audio->ac, FORMAT_V13K); |
| 349 | if (rc < 0) { |
| 350 | pr_err("%s:session id %d: T mode Open failed rc=%d\n", |
| 351 | __func__, audio->ac->session, rc); |
| 352 | rc = -ENODEV; |
| 353 | goto fail; |
| 354 | } |
| 355 | /* register for tx overflow (valid for tunnel mode only) */ |
| 356 | rc = q6asm_reg_tx_overflow(audio->ac, 0x01); |
| 357 | if (rc < 0) { |
| 358 | pr_err("%s:session id %d: TX Overflow registration failed rc=%d\n", |
| 359 | __func__, audio->ac->session, rc); |
| 360 | rc = -ENODEV; |
| 361 | goto fail; |
| 362 | } |
| 363 | pr_info("%s:session id %d: T mode encoder success\n", __func__, |
| 364 | audio->ac->session); |
| 365 | } else { |
| 366 | pr_err("%s:session id %d: Unexpected mode\n", __func__, |
| 367 | audio->ac->session); |
| 368 | rc = -EACCES; |
| 369 | goto fail; |
| 370 | } |
| 371 | |
| 372 | audio->opened = 1; |
| 373 | audio->reset_event = false; |
| 374 | atomic_set(&audio->in_count, PCM_BUF_COUNT); |
| 375 | atomic_set(&audio->out_count, 0x00); |
| 376 | audio->enc_compat_ioctl = qcelp_in_compat_ioctl; |
| 377 | audio->enc_ioctl = qcelp_in_ioctl; |
| 378 | file->private_data = audio; |
| 379 | |
| 380 | pr_info("%s:session id %d: success\n", __func__, audio->ac->session); |
| 381 | return 0; |
| 382 | fail: |
| 383 | q6asm_audio_client_free(audio->ac); |
| 384 | kfree(audio->enc_cfg); |
| 385 | kfree(audio); |
| 386 | return rc; |
| 387 | } |
| 388 | |
| 389 | static const struct file_operations audio_in_fops = { |
| 390 | .owner = THIS_MODULE, |
| 391 | .open = qcelp_in_open, |
| 392 | .release = audio_in_release, |
| 393 | .read = audio_in_read, |
| 394 | .write = audio_in_write, |
| 395 | .unlocked_ioctl = audio_in_ioctl, |
| 396 | .compat_ioctl = audio_in_compat_ioctl |
| 397 | }; |
| 398 | |
| 399 | struct miscdevice audio_qcelp_in_misc = { |
| 400 | .minor = MISC_DYNAMIC_MINOR, |
| 401 | .name = "msm_qcelp_in", |
| 402 | .fops = &audio_in_fops, |
| 403 | }; |
| 404 | |
Laxminath Kasam | 8b1366a | 2017-10-05 01:44:16 +0530 | [diff] [blame] | 405 | int __init qcelp_in_init(void) |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 406 | { |
| 407 | return misc_register(&audio_qcelp_in_misc); |
| 408 | } |
| 409 | |
Asish Bhattacharya | 5faacb3 | 2017-12-04 17:23:15 +0530 | [diff] [blame] | 410 | void qcelp_in_exit(void) |
Laxminath Kasam | 8b1366a | 2017-10-05 01:44:16 +0530 | [diff] [blame] | 411 | { |
| 412 | misc_deregister(&audio_qcelp_in_misc); |
| 413 | } |