Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2010-2017, The Linux Foundation. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 and |
| 6 | * only version 2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 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_aac.h> |
| 23 | #include <linux/compat.h> |
| 24 | #include <linux/atomic.h> |
| 25 | #include <asm/ioctls.h> |
| 26 | #include "audio_utils.h" |
| 27 | |
| 28 | |
| 29 | /* Buffer with meta*/ |
| 30 | #define PCM_BUF_SIZE (4096 + sizeof(struct meta_in)) |
| 31 | |
| 32 | /* Maximum 5 frames in buffer with meta */ |
| 33 | #define FRAME_SIZE (1 + ((1536+sizeof(struct meta_out_dsp)) * 5)) |
| 34 | |
| 35 | #define AAC_FORMAT_ADTS 65535 |
| 36 | |
| 37 | #define MAX_SAMPLE_RATE_384K 384000 |
| 38 | |
| 39 | static long aac_in_ioctl_shared(struct file *file, unsigned int cmd, void *arg) |
| 40 | { |
| 41 | struct q6audio_in *audio = file->private_data; |
| 42 | int rc = 0; |
| 43 | int cnt = 0; |
| 44 | |
| 45 | switch (cmd) { |
| 46 | case AUDIO_START: { |
| 47 | struct msm_audio_aac_enc_config *enc_cfg; |
| 48 | struct msm_audio_aac_config *aac_config; |
| 49 | uint32_t aac_mode = AAC_ENC_MODE_AAC_LC; |
| 50 | |
| 51 | enc_cfg = audio->enc_cfg; |
| 52 | aac_config = audio->codec_cfg; |
| 53 | /* ENCODE CFG (after new set of API's are published )bharath*/ |
| 54 | pr_debug("%s:session id %d: default buf alloc[%d]\n", __func__, |
| 55 | audio->ac->session, audio->buf_alloc); |
| 56 | if (audio->enabled == 1) { |
| 57 | pr_info("%s:AUDIO_START already over\n", __func__); |
| 58 | rc = 0; |
| 59 | break; |
| 60 | } |
| 61 | |
| 62 | if (audio->opened) { |
| 63 | rc = audio_in_buf_alloc(audio); |
| 64 | if (rc < 0) { |
| 65 | pr_err("%s:session id %d: buffer allocation failed\n", |
| 66 | __func__, audio->ac->session); |
| 67 | break; |
| 68 | } |
| 69 | } else { |
| 70 | if (audio->feedback == NON_TUNNEL_MODE) { |
| 71 | pr_debug("%s: starting in non_tunnel mode", |
| 72 | __func__); |
| 73 | rc = q6asm_open_read_write(audio->ac, |
| 74 | FORMAT_MPEG4_AAC, FORMAT_LINEAR_PCM); |
| 75 | if (rc < 0) { |
| 76 | pr_err("%s:open read write failed\n", |
| 77 | __func__); |
| 78 | break; |
| 79 | } |
| 80 | } |
| 81 | if (audio->feedback == TUNNEL_MODE) { |
| 82 | pr_debug("%s: starting in tunnel mode", |
| 83 | __func__); |
| 84 | rc = q6asm_open_read(audio->ac, |
| 85 | FORMAT_MPEG4_AAC); |
| 86 | |
| 87 | if (rc < 0) { |
| 88 | pr_err("%s:open read failed\n", |
| 89 | __func__); |
| 90 | break; |
| 91 | } |
| 92 | } |
| 93 | audio->stopped = 0; |
| 94 | } |
| 95 | |
| 96 | pr_debug("%s:sbr_ps_flag = %d, sbr_flag = %d\n", __func__, |
| 97 | aac_config->sbr_ps_on_flag, aac_config->sbr_on_flag); |
| 98 | if (aac_config->sbr_ps_on_flag) |
| 99 | aac_mode = AAC_ENC_MODE_EAAC_P; |
| 100 | else if (aac_config->sbr_on_flag) |
| 101 | aac_mode = AAC_ENC_MODE_AAC_P; |
| 102 | else |
| 103 | aac_mode = AAC_ENC_MODE_AAC_LC; |
| 104 | |
| 105 | rc = q6asm_enc_cfg_blk_aac(audio->ac, |
| 106 | audio->buf_cfg.frames_per_buf, |
| 107 | enc_cfg->sample_rate, |
| 108 | enc_cfg->channels, |
| 109 | enc_cfg->bit_rate, |
| 110 | aac_mode, |
| 111 | enc_cfg->stream_format); |
| 112 | if (rc < 0) { |
| 113 | pr_err("%s:session id %d: cmd media format block failed\n", |
| 114 | __func__, audio->ac->session); |
| 115 | break; |
| 116 | } |
| 117 | if (audio->feedback == NON_TUNNEL_MODE) { |
| 118 | rc = q6asm_media_format_block_pcm(audio->ac, |
| 119 | audio->pcm_cfg.sample_rate, |
| 120 | audio->pcm_cfg.channel_count); |
| 121 | if (rc < 0) { |
| 122 | pr_err("%s:session id %d: media format block failed\n", |
| 123 | __func__, audio->ac->session); |
| 124 | break; |
| 125 | } |
| 126 | } |
| 127 | rc = audio_in_enable(audio); |
| 128 | if (!rc) { |
| 129 | audio->enabled = 1; |
| 130 | } else { |
| 131 | audio->enabled = 0; |
| 132 | pr_err("%s:session id %d: Audio Start procedure failed rc=%d\n", |
| 133 | __func__, audio->ac->session, rc); |
| 134 | break; |
| 135 | } |
| 136 | while (cnt++ < audio->str_cfg.buffer_count) |
| 137 | q6asm_read(audio->ac); |
| 138 | pr_debug("%s:session id %d: AUDIO_START success enable[%d]\n", |
| 139 | __func__, audio->ac->session, audio->enabled); |
| 140 | break; |
| 141 | } |
| 142 | case AUDIO_STOP: { |
| 143 | pr_debug("%s:session id %d: Rxed AUDIO_STOP\n", __func__, |
| 144 | audio->ac->session); |
| 145 | rc = audio_in_disable(audio); |
| 146 | if (rc < 0) { |
| 147 | pr_err("%s:session id %d: Audio Stop procedure failed rc=%d\n", |
| 148 | __func__, audio->ac->session, rc); |
| 149 | break; |
| 150 | } |
| 151 | break; |
| 152 | } |
| 153 | case AUDIO_GET_AAC_ENC_CONFIG: { |
| 154 | struct msm_audio_aac_enc_config *cfg; |
| 155 | struct msm_audio_aac_enc_config *enc_cfg; |
| 156 | |
| 157 | cfg = (struct msm_audio_aac_enc_config *)arg; |
| 158 | if (cfg == NULL) { |
| 159 | pr_err("%s: NULL config pointer for %s\n", |
| 160 | __func__, "AUDIO_GET_AAC_CONFIG"); |
| 161 | rc = -EINVAL; |
| 162 | break; |
| 163 | } |
| 164 | memset(cfg, 0, sizeof(*cfg)); |
| 165 | enc_cfg = audio->enc_cfg; |
| 166 | if (enc_cfg->channels == CH_MODE_MONO) |
| 167 | cfg->channels = 1; |
| 168 | else |
| 169 | cfg->channels = 2; |
| 170 | |
| 171 | cfg->sample_rate = enc_cfg->sample_rate; |
| 172 | cfg->bit_rate = enc_cfg->bit_rate; |
| 173 | switch (enc_cfg->stream_format) { |
| 174 | case 0x00: |
| 175 | cfg->stream_format = AUDIO_AAC_FORMAT_ADTS; |
| 176 | break; |
| 177 | case 0x01: |
| 178 | cfg->stream_format = AUDIO_AAC_FORMAT_LOAS; |
| 179 | break; |
| 180 | case 0x02: |
| 181 | cfg->stream_format = AUDIO_AAC_FORMAT_ADIF; |
| 182 | break; |
| 183 | default: |
| 184 | case 0x03: |
| 185 | cfg->stream_format = AUDIO_AAC_FORMAT_RAW; |
| 186 | } |
| 187 | pr_debug("%s:session id %d: Get-aac-cfg: format=%d sr=%d bitrate=%d\n", |
| 188 | __func__, audio->ac->session, |
| 189 | cfg->stream_format, cfg->sample_rate, cfg->bit_rate); |
| 190 | break; |
| 191 | } |
| 192 | case AUDIO_SET_AAC_ENC_CONFIG: { |
| 193 | struct msm_audio_aac_enc_config *cfg; |
| 194 | struct msm_audio_aac_enc_config *enc_cfg; |
| 195 | uint32_t min_bitrate, max_bitrate; |
| 196 | |
| 197 | cfg = (struct msm_audio_aac_enc_config *)arg; |
| 198 | if (cfg == NULL) { |
| 199 | pr_err("%s: NULL config pointer for %s\n", |
| 200 | "AUDIO_SET_AAC_ENC_CONFIG", __func__); |
| 201 | rc = -EINVAL; |
| 202 | break; |
| 203 | } |
| 204 | enc_cfg = audio->enc_cfg; |
| 205 | pr_debug("%s:session id %d: Set-aac-cfg: stream=%d\n", __func__, |
| 206 | audio->ac->session, cfg->stream_format); |
| 207 | |
| 208 | switch (cfg->stream_format) { |
| 209 | case AUDIO_AAC_FORMAT_ADTS: |
| 210 | enc_cfg->stream_format = 0x00; |
| 211 | break; |
| 212 | case AUDIO_AAC_FORMAT_LOAS: |
| 213 | enc_cfg->stream_format = 0x01; |
| 214 | break; |
| 215 | case AUDIO_AAC_FORMAT_ADIF: |
| 216 | enc_cfg->stream_format = 0x02; |
| 217 | break; |
| 218 | case AUDIO_AAC_FORMAT_RAW: |
| 219 | enc_cfg->stream_format = 0x03; |
| 220 | break; |
| 221 | default: |
| 222 | pr_err("%s:session id %d: unsupported AAC format %d\n", |
| 223 | __func__, audio->ac->session, |
| 224 | cfg->stream_format); |
| 225 | rc = -EINVAL; |
| 226 | break; |
| 227 | } |
| 228 | |
| 229 | if (cfg->channels == 1) { |
| 230 | cfg->channels = CH_MODE_MONO; |
| 231 | } else if (cfg->channels == 2) { |
| 232 | cfg->channels = CH_MODE_STEREO; |
| 233 | } else { |
| 234 | rc = -EINVAL; |
| 235 | break; |
| 236 | } |
| 237 | |
| 238 | if (cfg->sample_rate > MAX_SAMPLE_RATE_384K) { |
| 239 | pr_err("%s: ERROR: invalid sample rate = %u", |
| 240 | __func__, cfg->sample_rate); |
| 241 | rc = -EINVAL; |
| 242 | break; |
| 243 | } |
| 244 | |
| 245 | min_bitrate = ((cfg->sample_rate)*(cfg->channels))/2; |
| 246 | /* This calculation should be based on AAC mode. But we cannot |
| 247 | * get AAC mode in this setconfig. min_bitrate's logical max |
| 248 | * value is 24000. So if min_bitrate is higher than 24000, |
| 249 | * choose 24000. |
| 250 | */ |
| 251 | if (min_bitrate > 24000) |
| 252 | min_bitrate = 24000; |
| 253 | max_bitrate = 6*(cfg->sample_rate)*(cfg->channels); |
| 254 | if (max_bitrate > 192000) |
| 255 | max_bitrate = 192000; |
| 256 | if ((cfg->bit_rate < min_bitrate) || |
| 257 | (cfg->bit_rate > max_bitrate)) { |
| 258 | pr_err("%s: bitrate permissible: max=%d, min=%d\n", |
| 259 | __func__, max_bitrate, min_bitrate); |
| 260 | pr_err("%s: ERROR in setting bitrate = %d\n", |
| 261 | __func__, cfg->bit_rate); |
| 262 | rc = -EINVAL; |
| 263 | break; |
| 264 | } |
| 265 | enc_cfg->sample_rate = cfg->sample_rate; |
| 266 | enc_cfg->channels = cfg->channels; |
| 267 | enc_cfg->bit_rate = cfg->bit_rate; |
| 268 | pr_debug("%s:session id %d: Set-aac-cfg:SR= 0x%x ch=0x%x bitrate=0x%x, format(adts/raw) = %d\n", |
| 269 | __func__, audio->ac->session, enc_cfg->sample_rate, |
| 270 | enc_cfg->channels, enc_cfg->bit_rate, |
| 271 | enc_cfg->stream_format); |
| 272 | break; |
| 273 | } |
| 274 | case AUDIO_SET_AAC_CONFIG: { |
| 275 | struct msm_audio_aac_config *aac_cfg; |
| 276 | struct msm_audio_aac_config *audio_aac_cfg; |
| 277 | struct msm_audio_aac_enc_config *enc_cfg; |
| 278 | |
| 279 | enc_cfg = audio->enc_cfg; |
| 280 | audio_aac_cfg = audio->codec_cfg; |
| 281 | aac_cfg = (struct msm_audio_aac_config *)arg; |
| 282 | |
| 283 | if (aac_cfg == NULL) { |
| 284 | pr_err("%s: NULL config pointer %s\n", |
| 285 | __func__, "AUDIO_SET_AAC_CONFIG"); |
| 286 | rc = -EINVAL; |
| 287 | break; |
| 288 | } |
| 289 | pr_debug("%s:session id %d: AUDIO_SET_AAC_CONFIG: sbr_flag = %d sbr_ps_flag = %d\n", |
| 290 | __func__, audio->ac->session, aac_cfg->sbr_on_flag, |
| 291 | aac_cfg->sbr_ps_on_flag); |
| 292 | audio_aac_cfg->sbr_on_flag = aac_cfg->sbr_on_flag; |
| 293 | audio_aac_cfg->sbr_ps_on_flag = aac_cfg->sbr_ps_on_flag; |
| 294 | if ((audio_aac_cfg->sbr_on_flag == 1) || |
| 295 | (audio_aac_cfg->sbr_ps_on_flag == 1)) { |
| 296 | if (enc_cfg->sample_rate < 24000) { |
| 297 | pr_err("%s: ERROR in setting samplerate = %d\n", |
| 298 | __func__, enc_cfg->sample_rate); |
| 299 | rc = -EINVAL; |
| 300 | break; |
| 301 | } |
| 302 | } |
| 303 | break; |
| 304 | } |
| 305 | default: |
| 306 | pr_err("%s: Unknown ioctl cmd = %d", __func__, cmd); |
| 307 | rc = -EINVAL; |
| 308 | } |
| 309 | return rc; |
| 310 | } |
| 311 | |
| 312 | static long aac_in_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 313 | { |
| 314 | struct q6audio_in *audio = file->private_data; |
| 315 | int rc = 0; |
| 316 | |
| 317 | switch (cmd) { |
| 318 | case AUDIO_START: |
| 319 | case AUDIO_STOP: { |
| 320 | rc = aac_in_ioctl_shared(file, cmd, NULL); |
| 321 | break; |
| 322 | } |
| 323 | case AUDIO_GET_AAC_ENC_CONFIG: { |
| 324 | struct msm_audio_aac_enc_config cfg; |
| 325 | |
| 326 | rc = aac_in_ioctl_shared(file, cmd, &cfg); |
| 327 | if (rc) { |
| 328 | pr_err("%s:AUDIO_GET_AAC_ENC_CONFIG failed. rc=%d\n", |
| 329 | __func__, rc); |
| 330 | break; |
| 331 | } |
| 332 | if (copy_to_user((void *)arg, &cfg, sizeof(cfg))) { |
| 333 | pr_err("%s: copy_to_user for AUDIO_GET_AAC_ENC_CONFIG failed\n", |
| 334 | __func__); |
| 335 | rc = -EFAULT; |
| 336 | } |
| 337 | break; |
| 338 | } |
| 339 | case AUDIO_SET_AAC_ENC_CONFIG: { |
| 340 | struct msm_audio_aac_enc_config cfg; |
| 341 | |
| 342 | if (copy_from_user(&cfg, (void *)arg, sizeof(cfg))) { |
| 343 | pr_err("%s: copy_from_user for AUDIO_SET_AAC_ENC_CONFIG failed\n", |
| 344 | __func__); |
| 345 | rc = -EFAULT; |
| 346 | break; |
| 347 | } |
| 348 | rc = aac_in_ioctl_shared(file, cmd, &cfg); |
| 349 | if (rc) |
| 350 | pr_err("%s:AUDIO_SET_AAC_ENC_CONFIG failed. rc=%d\n", |
| 351 | __func__, rc); |
| 352 | break; |
| 353 | } |
| 354 | case AUDIO_GET_AAC_CONFIG: { |
| 355 | if (copy_to_user((void *)arg, &audio->codec_cfg, |
| 356 | sizeof(struct msm_audio_aac_config))) { |
| 357 | pr_err("%s: copy_to_user for AUDIO_GET_AAC_CONFIG failed\n", |
| 358 | __func__); |
| 359 | rc = -EFAULT; |
| 360 | break; |
| 361 | } |
| 362 | break; |
| 363 | } |
| 364 | case AUDIO_SET_AAC_CONFIG: { |
| 365 | struct msm_audio_aac_config aac_cfg; |
| 366 | |
| 367 | if (copy_from_user(&aac_cfg, (void *)arg, |
| 368 | sizeof(struct msm_audio_aac_config))) { |
| 369 | pr_err("%s: copy_to_user for AUDIO_SET_CONFIG failed\n", |
| 370 | __func__); |
| 371 | rc = -EFAULT; |
| 372 | break; |
| 373 | } |
| 374 | rc = aac_in_ioctl_shared(file, cmd, &aac_cfg); |
| 375 | if (rc) |
| 376 | pr_err("%s:AUDIO_SET_AAC_CONFIG failed. rc=%d\n", |
| 377 | __func__, rc); |
| 378 | break; |
| 379 | } |
| 380 | default: |
| 381 | pr_err("%s: Unknown ioctl cmd=%d\n", __func__, cmd); |
| 382 | rc = -EINVAL; |
| 383 | } |
| 384 | return rc; |
| 385 | } |
| 386 | |
| 387 | #ifdef CONFIG_COMPAT |
| 388 | struct msm_audio_aac_enc_config32 { |
| 389 | u32 channels; |
| 390 | u32 sample_rate; |
| 391 | u32 bit_rate; |
| 392 | u32 stream_format; |
| 393 | }; |
| 394 | |
| 395 | struct msm_audio_aac_config32 { |
| 396 | s16 format; |
| 397 | u16 audio_object; |
| 398 | u16 ep_config; /* 0 ~ 3 useful only obj = ERLC */ |
| 399 | u16 aac_section_data_resilience_flag; |
| 400 | u16 aac_scalefactor_data_resilience_flag; |
| 401 | u16 aac_spectral_data_resilience_flag; |
| 402 | u16 sbr_on_flag; |
| 403 | u16 sbr_ps_on_flag; |
| 404 | u16 dual_mono_mode; |
| 405 | u16 channel_configuration; |
| 406 | u16 sample_rate; |
| 407 | }; |
| 408 | |
| 409 | enum { |
| 410 | AUDIO_SET_AAC_CONFIG_32 = _IOW(AUDIO_IOCTL_MAGIC, |
| 411 | (AUDIO_MAX_COMMON_IOCTL_NUM+0), struct msm_audio_aac_config32), |
| 412 | AUDIO_GET_AAC_CONFIG_32 = _IOR(AUDIO_IOCTL_MAGIC, |
| 413 | (AUDIO_MAX_COMMON_IOCTL_NUM+1), struct msm_audio_aac_config32), |
| 414 | AUDIO_SET_AAC_ENC_CONFIG_32 = _IOW(AUDIO_IOCTL_MAGIC, |
| 415 | (AUDIO_MAX_COMMON_IOCTL_NUM+3), struct msm_audio_aac_enc_config32), |
| 416 | AUDIO_GET_AAC_ENC_CONFIG_32 = _IOR(AUDIO_IOCTL_MAGIC, |
| 417 | (AUDIO_MAX_COMMON_IOCTL_NUM+4), struct msm_audio_aac_enc_config32) |
| 418 | }; |
| 419 | |
| 420 | static long aac_in_compat_ioctl(struct file *file, unsigned int cmd, |
| 421 | unsigned long arg) |
| 422 | { |
| 423 | struct q6audio_in *audio = file->private_data; |
| 424 | int rc = 0; |
| 425 | |
| 426 | switch (cmd) { |
| 427 | case AUDIO_START: |
| 428 | case AUDIO_STOP: { |
| 429 | rc = aac_in_ioctl_shared(file, cmd, NULL); |
| 430 | break; |
| 431 | } |
| 432 | case AUDIO_GET_AAC_ENC_CONFIG_32: { |
| 433 | struct msm_audio_aac_enc_config cfg; |
| 434 | struct msm_audio_aac_enc_config32 cfg_32; |
| 435 | |
| 436 | memset(&cfg_32, 0, sizeof(cfg_32)); |
| 437 | |
| 438 | cmd = AUDIO_GET_AAC_ENC_CONFIG; |
| 439 | rc = aac_in_ioctl_shared(file, cmd, &cfg); |
| 440 | if (rc) { |
| 441 | pr_err("%s:AUDIO_GET_AAC_ENC_CONFIG_32 failed. Rc= %d\n", |
| 442 | __func__, rc); |
| 443 | break; |
| 444 | } |
| 445 | cfg_32.channels = cfg.channels; |
| 446 | cfg_32.sample_rate = cfg.sample_rate; |
| 447 | cfg_32.bit_rate = cfg.bit_rate; |
| 448 | cfg_32.stream_format = cfg.stream_format; |
| 449 | if (copy_to_user((void *)arg, &cfg_32, sizeof(cfg_32))) { |
| 450 | pr_err("%s: copy_to_user for AUDIO_GET_AAC_ENC_CONFIG_32 failed\n", |
| 451 | __func__); |
| 452 | rc = -EFAULT; |
| 453 | } |
| 454 | break; |
| 455 | } |
| 456 | case AUDIO_SET_AAC_ENC_CONFIG_32: { |
| 457 | struct msm_audio_aac_enc_config cfg; |
| 458 | struct msm_audio_aac_enc_config32 cfg_32; |
| 459 | |
| 460 | if (copy_from_user(&cfg_32, (void *)arg, sizeof(cfg_32))) { |
| 461 | pr_err("%s: copy_from_user for AUDIO_GET_AAC_ENC_CONFIG_32 failed\n", |
| 462 | __func__); |
| 463 | rc = -EFAULT; |
| 464 | break; |
| 465 | } |
| 466 | cfg.channels = cfg_32.channels; |
| 467 | cfg.sample_rate = cfg_32.sample_rate; |
| 468 | cfg.bit_rate = cfg_32.bit_rate; |
| 469 | cfg.stream_format = cfg_32.stream_format; |
| 470 | /* The command should be converted from 32 bit to normal |
| 471 | * before the shared ioctl is called as shared ioctl |
| 472 | * can process only normal commands |
| 473 | */ |
| 474 | cmd = AUDIO_SET_AAC_ENC_CONFIG; |
| 475 | rc = aac_in_ioctl_shared(file, cmd, &cfg); |
| 476 | if (rc) |
| 477 | pr_err("%s:AUDIO_SET_AAC_ENC_CONFIG_32 failed. rc=%d\n", |
| 478 | __func__, rc); |
| 479 | break; |
| 480 | } |
| 481 | case AUDIO_GET_AAC_CONFIG_32: { |
| 482 | struct msm_audio_aac_config *aac_config; |
| 483 | struct msm_audio_aac_config32 aac_config_32; |
| 484 | |
| 485 | aac_config = (struct msm_audio_aac_config *)audio->codec_cfg; |
| 486 | aac_config_32.format = aac_config->format; |
| 487 | aac_config_32.audio_object = aac_config->audio_object; |
| 488 | aac_config_32.ep_config = aac_config->ep_config; |
| 489 | aac_config_32.aac_section_data_resilience_flag = |
| 490 | aac_config->aac_section_data_resilience_flag; |
| 491 | aac_config_32.aac_scalefactor_data_resilience_flag = |
| 492 | aac_config->aac_scalefactor_data_resilience_flag; |
| 493 | aac_config_32.aac_spectral_data_resilience_flag = |
| 494 | aac_config->aac_spectral_data_resilience_flag; |
| 495 | aac_config_32.sbr_on_flag = aac_config->sbr_on_flag; |
| 496 | aac_config_32.sbr_ps_on_flag = aac_config->sbr_ps_on_flag; |
| 497 | aac_config_32.dual_mono_mode = aac_config->dual_mono_mode; |
| 498 | aac_config_32.channel_configuration = |
| 499 | aac_config->channel_configuration; |
| 500 | aac_config_32.sample_rate = aac_config->sample_rate; |
| 501 | |
| 502 | if (copy_to_user((void *)arg, &aac_config_32, |
| 503 | sizeof(aac_config_32))) { |
| 504 | pr_err("%s: copy_to_user for AUDIO_GET_AAC_CONFIG_32 failed\n", |
| 505 | __func__); |
| 506 | rc = -EFAULT; |
| 507 | break; |
| 508 | } |
| 509 | break; |
| 510 | } |
| 511 | case AUDIO_SET_AAC_CONFIG_32: { |
| 512 | struct msm_audio_aac_config aac_cfg; |
| 513 | struct msm_audio_aac_config32 aac_cfg_32; |
| 514 | |
| 515 | if (copy_from_user(&aac_cfg_32, (void *)arg, |
| 516 | sizeof(aac_cfg_32))) { |
| 517 | pr_err("%s: copy_from_user for AUDIO_SET_AAC_CONFIG_32 failed\n", |
| 518 | __func__); |
| 519 | rc = -EFAULT; |
| 520 | break; |
| 521 | } |
| 522 | aac_cfg.format = aac_cfg_32.format; |
| 523 | aac_cfg.audio_object = aac_cfg_32.audio_object; |
| 524 | aac_cfg.ep_config = aac_cfg_32.ep_config; |
| 525 | aac_cfg.aac_section_data_resilience_flag = |
| 526 | aac_cfg_32.aac_section_data_resilience_flag; |
| 527 | aac_cfg.aac_scalefactor_data_resilience_flag = |
| 528 | aac_cfg_32.aac_scalefactor_data_resilience_flag; |
| 529 | aac_cfg.aac_spectral_data_resilience_flag = |
| 530 | aac_cfg_32.aac_spectral_data_resilience_flag; |
| 531 | aac_cfg.sbr_on_flag = aac_cfg_32.sbr_on_flag; |
| 532 | aac_cfg.sbr_ps_on_flag = aac_cfg_32.sbr_ps_on_flag; |
| 533 | aac_cfg.dual_mono_mode = aac_cfg_32.dual_mono_mode; |
| 534 | aac_cfg.channel_configuration = |
| 535 | aac_cfg_32.channel_configuration; |
| 536 | aac_cfg.sample_rate = aac_cfg_32.sample_rate; |
| 537 | |
| 538 | cmd = AUDIO_SET_AAC_CONFIG; |
| 539 | rc = aac_in_ioctl_shared(file, cmd, &aac_cfg); |
| 540 | if (rc) |
| 541 | pr_err("%s:AUDIO_SET_AAC_CONFIG failed. Rc= %d\n", |
| 542 | __func__, rc); |
| 543 | break; |
| 544 | } |
| 545 | default: |
| 546 | pr_err("%s: Unknown ioctl cmd = %d\n", __func__, cmd); |
| 547 | rc = -EINVAL; |
| 548 | } |
| 549 | return rc; |
| 550 | } |
| 551 | #else |
| 552 | #define aac_in_compat_ioctl NULL |
| 553 | #endif |
| 554 | |
| 555 | static int aac_in_open(struct inode *inode, struct file *file) |
| 556 | { |
| 557 | struct q6audio_in *audio = NULL; |
| 558 | struct msm_audio_aac_enc_config *enc_cfg; |
| 559 | struct msm_audio_aac_config *aac_config; |
| 560 | int rc = 0; |
| 561 | |
| 562 | audio = kzalloc(sizeof(struct q6audio_in), GFP_KERNEL); |
| 563 | |
| 564 | if (audio == NULL) |
| 565 | return -ENOMEM; |
| 566 | |
| 567 | /* Allocate memory for encoder config param */ |
| 568 | audio->enc_cfg = kzalloc(sizeof(struct msm_audio_aac_enc_config), |
| 569 | GFP_KERNEL); |
| 570 | if (audio->enc_cfg == NULL) { |
| 571 | kfree(audio); |
| 572 | return -ENOMEM; |
| 573 | } |
| 574 | enc_cfg = audio->enc_cfg; |
| 575 | |
| 576 | audio->codec_cfg = kzalloc(sizeof(struct msm_audio_aac_config), |
| 577 | GFP_KERNEL); |
| 578 | if (audio->codec_cfg == NULL) { |
| 579 | kfree(audio->enc_cfg); |
| 580 | kfree(audio); |
| 581 | return -ENOMEM; |
| 582 | } |
| 583 | aac_config = audio->codec_cfg; |
| 584 | |
| 585 | mutex_init(&audio->lock); |
| 586 | mutex_init(&audio->read_lock); |
| 587 | mutex_init(&audio->write_lock); |
| 588 | spin_lock_init(&audio->dsp_lock); |
| 589 | init_waitqueue_head(&audio->read_wait); |
| 590 | init_waitqueue_head(&audio->write_wait); |
| 591 | |
| 592 | /* Settings will be re-config at AUDIO_SET_CONFIG, |
| 593 | * but at least we need to have initial config |
| 594 | */ |
| 595 | audio->str_cfg.buffer_size = FRAME_SIZE; |
| 596 | audio->str_cfg.buffer_count = FRAME_NUM; |
| 597 | audio->min_frame_size = 1536; |
| 598 | audio->max_frames_per_buf = 5; |
| 599 | enc_cfg->sample_rate = 8000; |
| 600 | enc_cfg->channels = 1; |
| 601 | enc_cfg->bit_rate = 16000; |
| 602 | enc_cfg->stream_format = 0x00;/* 0:ADTS, 3:RAW */ |
| 603 | audio->buf_cfg.meta_info_enable = 0x01; |
| 604 | audio->buf_cfg.frames_per_buf = 0x01; |
| 605 | audio->pcm_cfg.buffer_count = PCM_BUF_COUNT; |
| 606 | audio->pcm_cfg.buffer_size = PCM_BUF_SIZE; |
| 607 | aac_config->format = AUDIO_AAC_FORMAT_ADTS; |
| 608 | aac_config->audio_object = AUDIO_AAC_OBJECT_LC; |
| 609 | aac_config->sbr_on_flag = 0; |
| 610 | aac_config->sbr_ps_on_flag = 0; |
| 611 | aac_config->channel_configuration = 1; |
| 612 | |
| 613 | audio->ac = q6asm_audio_client_alloc((app_cb)q6asm_in_cb, |
| 614 | (void *)audio); |
| 615 | |
| 616 | if (!audio->ac) { |
| 617 | pr_err("%s: Could not allocate memory for audio client\n", |
| 618 | __func__); |
| 619 | kfree(audio->enc_cfg); |
| 620 | kfree(audio->codec_cfg); |
| 621 | kfree(audio); |
| 622 | return -ENOMEM; |
| 623 | } |
| 624 | /* open aac encoder in tunnel mode */ |
| 625 | audio->buf_cfg.frames_per_buf = 0x01; |
| 626 | |
| 627 | if ((file->f_mode & FMODE_WRITE) && |
| 628 | (file->f_mode & FMODE_READ)) { |
| 629 | audio->feedback = NON_TUNNEL_MODE; |
| 630 | rc = q6asm_open_read_write(audio->ac, FORMAT_MPEG4_AAC, |
| 631 | FORMAT_LINEAR_PCM); |
| 632 | |
| 633 | if (rc < 0) { |
| 634 | pr_err("%s:session id %d: NT Open failed rc=%d\n", |
| 635 | __func__, audio->ac->session, rc); |
| 636 | rc = -ENODEV; |
| 637 | goto fail; |
| 638 | } |
| 639 | audio->buf_cfg.meta_info_enable = 0x01; |
| 640 | pr_info("%s:session id %d: NT mode encoder success\n", __func__, |
| 641 | audio->ac->session); |
| 642 | } else if (!(file->f_mode & FMODE_WRITE) && |
| 643 | (file->f_mode & FMODE_READ)) { |
| 644 | audio->feedback = TUNNEL_MODE; |
| 645 | rc = q6asm_open_read(audio->ac, FORMAT_MPEG4_AAC); |
| 646 | |
| 647 | if (rc < 0) { |
| 648 | pr_err("%s:session id %d: Tunnel Open failed rc=%d\n", |
| 649 | __func__, audio->ac->session, rc); |
| 650 | rc = -ENODEV; |
| 651 | goto fail; |
| 652 | } |
| 653 | /* register for tx overflow (valid for tunnel mode only) */ |
| 654 | rc = q6asm_reg_tx_overflow(audio->ac, 0x01); |
| 655 | if (rc < 0) { |
| 656 | pr_err("%s:session id %d: TX Overflow registration failed rc=%d\n", |
| 657 | __func__, |
| 658 | audio->ac->session, rc); |
| 659 | rc = -ENODEV; |
| 660 | goto fail; |
| 661 | } |
| 662 | audio->buf_cfg.meta_info_enable = 0x00; |
| 663 | pr_info("%s:session id %d: T mode encoder success\n", __func__, |
| 664 | audio->ac->session); |
| 665 | } else { |
| 666 | pr_err("%s:session id %d: Unexpected mode\n", __func__, |
| 667 | audio->ac->session); |
| 668 | rc = -EACCES; |
| 669 | goto fail; |
| 670 | } |
| 671 | audio->opened = 1; |
| 672 | audio->reset_event = false; |
| 673 | atomic_set(&audio->in_count, PCM_BUF_COUNT); |
| 674 | atomic_set(&audio->out_count, 0x00); |
| 675 | audio->enc_compat_ioctl = aac_in_compat_ioctl; |
| 676 | audio->enc_ioctl = aac_in_ioctl; |
| 677 | file->private_data = audio; |
| 678 | |
| 679 | pr_info("%s:session id %d: success\n", __func__, audio->ac->session); |
| 680 | return 0; |
| 681 | fail: |
| 682 | q6asm_audio_client_free(audio->ac); |
| 683 | kfree(audio->enc_cfg); |
| 684 | kfree(audio->codec_cfg); |
| 685 | kfree(audio); |
| 686 | return rc; |
| 687 | } |
| 688 | |
| 689 | static const struct file_operations audio_in_fops = { |
| 690 | .owner = THIS_MODULE, |
| 691 | .open = aac_in_open, |
| 692 | .release = audio_in_release, |
| 693 | .read = audio_in_read, |
| 694 | .write = audio_in_write, |
| 695 | .unlocked_ioctl = audio_in_ioctl, |
| 696 | .compat_ioctl = audio_in_compat_ioctl |
| 697 | }; |
| 698 | |
| 699 | struct miscdevice audio_aac_in_misc = { |
| 700 | .minor = MISC_DYNAMIC_MINOR, |
| 701 | .name = "msm_aac_in", |
| 702 | .fops = &audio_in_fops, |
| 703 | }; |
| 704 | |
Laxminath Kasam | 8b1366a | 2017-10-05 01:44:16 +0530 | [diff] [blame] | 705 | int __init aac_in_init(void) |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 706 | { |
| 707 | return misc_register(&audio_aac_in_misc); |
| 708 | } |
Laxminath Kasam | 8b1366a | 2017-10-05 01:44:16 +0530 | [diff] [blame] | 709 | |
Asish Bhattacharya | 5faacb3 | 2017-12-04 17:23:15 +0530 | [diff] [blame] | 710 | void aac_in_exit(void) |
Laxminath Kasam | 8b1366a | 2017-10-05 01:44:16 +0530 | [diff] [blame] | 711 | { |
| 712 | misc_deregister(&audio_aac_in_misc); |
| 713 | } |