Xiaojun Sang | c327749 | 2018-09-06 14:34:03 +0800 | [diff] [blame] | 1 | /* Copyright (c) 2015-2018, The Linux Foundation. All rights reserved. |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [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/types.h> |
| 15 | #include <linux/msm_audio_ape.h> |
| 16 | #include <linux/compat.h> |
| 17 | #include "audio_utils_aio.h" |
| 18 | |
| 19 | static struct miscdevice audio_ape_misc; |
| 20 | static struct ws_mgr audio_ape_ws_mgr; |
| 21 | |
| 22 | static const struct file_operations audio_ape_debug_fops = { |
| 23 | .read = audio_aio_debug_read, |
| 24 | .open = audio_aio_debug_open, |
| 25 | }; |
| 26 | static struct dentry *config_debugfs_create_file(const char *name, void *data) |
| 27 | { |
| 28 | return debugfs_create_file(name, S_IFREG | 0444, |
| 29 | NULL, (void *)data, &audio_ape_debug_fops); |
| 30 | } |
| 31 | |
| 32 | static long audio_ioctl_shared(struct file *file, unsigned int cmd, |
| 33 | void *arg) |
| 34 | { |
| 35 | struct q6audio_aio *audio = file->private_data; |
| 36 | int rc = 0; |
| 37 | |
| 38 | switch (cmd) { |
| 39 | case AUDIO_START: { |
| 40 | struct asm_ape_cfg ape_cfg; |
| 41 | struct msm_audio_ape_config *ape_config; |
| 42 | |
| 43 | pr_debug("%s[%pK]: AUDIO_START session_id[%d]\n", __func__, |
| 44 | audio, audio->ac->session); |
| 45 | if (audio->feedback == NON_TUNNEL_MODE) { |
| 46 | /* Configure PCM output block */ |
| 47 | rc = q6asm_enc_cfg_blk_pcm(audio->ac, |
| 48 | audio->pcm_cfg.sample_rate, |
| 49 | audio->pcm_cfg.channel_count); |
| 50 | if (rc < 0) { |
| 51 | pr_err("pcm output block config failed\n"); |
| 52 | break; |
| 53 | } |
| 54 | } |
| 55 | ape_config = (struct msm_audio_ape_config *)audio->codec_cfg; |
| 56 | ape_cfg.compatible_version = ape_config->compatibleVersion; |
| 57 | ape_cfg.compression_level = ape_config->compressionLevel; |
| 58 | ape_cfg.format_flags = ape_config->formatFlags; |
| 59 | ape_cfg.blocks_per_frame = ape_config->blocksPerFrame; |
| 60 | ape_cfg.final_frame_blocks = ape_config->finalFrameBlocks; |
| 61 | ape_cfg.total_frames = ape_config->totalFrames; |
| 62 | ape_cfg.bits_per_sample = ape_config->bitsPerSample; |
| 63 | ape_cfg.num_channels = ape_config->numChannels; |
| 64 | ape_cfg.sample_rate = ape_config->sampleRate; |
| 65 | ape_cfg.seek_table_present = ape_config->seekTablePresent; |
| 66 | pr_debug("%s: compatibleVersion %d compressionLevel %d formatFlags %d blocksPerFrame %d finalFrameBlocks %d totalFrames %d bitsPerSample %d numChannels %d sampleRate %d seekTablePresent %d\n", |
| 67 | __func__, ape_config->compatibleVersion, |
| 68 | ape_config->compressionLevel, |
| 69 | ape_config->formatFlags, |
| 70 | ape_config->blocksPerFrame, |
| 71 | ape_config->finalFrameBlocks, |
| 72 | ape_config->totalFrames, |
| 73 | ape_config->bitsPerSample, |
| 74 | ape_config->numChannels, |
| 75 | ape_config->sampleRate, |
| 76 | ape_config->seekTablePresent); |
| 77 | /* Configure Media format block */ |
| 78 | rc = q6asm_media_format_block_ape(audio->ac, &ape_cfg, |
| 79 | audio->ac->stream_id); |
| 80 | if (rc < 0) { |
| 81 | pr_err("cmd media format block failed\n"); |
| 82 | break; |
| 83 | } |
| 84 | rc = audio_aio_enable(audio); |
| 85 | audio->eos_rsp = 0; |
| 86 | audio->eos_flag = 0; |
| 87 | if (!rc) { |
| 88 | audio->enabled = 1; |
| 89 | } else { |
| 90 | audio->enabled = 0; |
| 91 | pr_err("Audio Start procedure failed rc=%d\n", rc); |
| 92 | break; |
| 93 | } |
| 94 | pr_debug("AUDIO_START success enable[%d]\n", audio->enabled); |
| 95 | if (audio->stopped == 1) |
| 96 | audio->stopped = 0; |
| 97 | break; |
| 98 | } |
| 99 | default: |
| 100 | pr_err("%s: Unknown ioctl cmd = %d", __func__, cmd); |
| 101 | break; |
| 102 | } |
| 103 | return rc; |
| 104 | } |
| 105 | |
| 106 | static long audio_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 107 | { |
| 108 | struct q6audio_aio *audio = file->private_data; |
| 109 | int rc = 0; |
| 110 | |
| 111 | switch (cmd) { |
| 112 | case AUDIO_START: { |
| 113 | rc = audio_ioctl_shared(file, cmd, (void *)arg); |
| 114 | break; |
| 115 | } |
| 116 | case AUDIO_GET_APE_CONFIG: { |
| 117 | if (copy_to_user((void *)arg, audio->codec_cfg, |
| 118 | sizeof(struct msm_audio_ape_config))) { |
| 119 | pr_err("%s:copy_to_user for AUDIO_GET_APE_CONFIG failed\n", |
| 120 | __func__); |
| 121 | rc = -EFAULT; |
| 122 | break; |
| 123 | } |
| 124 | break; |
| 125 | } |
| 126 | case AUDIO_SET_APE_CONFIG: { |
| 127 | if (copy_from_user(audio->codec_cfg, (void *)arg, |
| 128 | sizeof(struct msm_audio_ape_config))) { |
| 129 | pr_err("%s:copy_from_user for AUDIO_SET_APE_CONFIG failed\n", |
| 130 | __func__); |
| 131 | rc = -EFAULT; |
| 132 | break; |
| 133 | } |
| 134 | break; |
| 135 | } |
| 136 | default: { |
| 137 | pr_debug("%s[%pK]: Calling utils ioctl\n", __func__, audio); |
| 138 | rc = audio->codec_ioctl(file, cmd, arg); |
| 139 | if (rc) |
Xiaojun Sang | c327749 | 2018-09-06 14:34:03 +0800 | [diff] [blame] | 140 | pr_err_ratelimited("Failed in utils_ioctl: %d\n", rc); |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 141 | break; |
| 142 | } |
| 143 | } |
| 144 | return rc; |
| 145 | } |
| 146 | |
| 147 | #ifdef CONFIG_COMPAT |
| 148 | struct msm_audio_ape_config_32 { |
| 149 | u16 compatibleVersion; |
| 150 | u16 compressionLevel; |
| 151 | u32 formatFlags; |
| 152 | u32 blocksPerFrame; |
| 153 | u32 finalFrameBlocks; |
| 154 | u32 totalFrames; |
| 155 | u16 bitsPerSample; |
| 156 | u16 numChannels; |
| 157 | u32 sampleRate; |
| 158 | u32 seekTablePresent; |
| 159 | |
| 160 | }; |
| 161 | |
| 162 | enum { |
| 163 | AUDIO_GET_APE_CONFIG_32 = _IOR(AUDIO_IOCTL_MAGIC, |
| 164 | (AUDIO_MAX_COMMON_IOCTL_NUM+0), struct msm_audio_ape_config_32), |
| 165 | AUDIO_SET_APE_CONFIG_32 = _IOW(AUDIO_IOCTL_MAGIC, |
| 166 | (AUDIO_MAX_COMMON_IOCTL_NUM+1), struct msm_audio_ape_config_32) |
| 167 | }; |
| 168 | |
| 169 | static long audio_compat_ioctl(struct file *file, unsigned int cmd, |
| 170 | unsigned long arg) |
| 171 | { |
| 172 | struct q6audio_aio *audio = file->private_data; |
| 173 | int rc = 0; |
| 174 | |
| 175 | switch (cmd) { |
| 176 | case AUDIO_START: { |
| 177 | rc = audio_ioctl_shared(file, cmd, (void *)arg); |
| 178 | break; |
| 179 | } |
| 180 | case AUDIO_GET_APE_CONFIG_32: { |
| 181 | struct msm_audio_ape_config *ape_config; |
| 182 | struct msm_audio_ape_config_32 ape_config_32; |
| 183 | |
| 184 | memset(&ape_config_32, 0, sizeof(ape_config_32)); |
| 185 | |
| 186 | ape_config = (struct msm_audio_ape_config *)audio->codec_cfg; |
| 187 | ape_config_32.compatibleVersion = ape_config->compatibleVersion; |
| 188 | ape_config_32.compressionLevel = |
| 189 | ape_config->compressionLevel; |
| 190 | ape_config_32.formatFlags = ape_config->formatFlags; |
| 191 | ape_config_32.blocksPerFrame = ape_config->blocksPerFrame; |
| 192 | ape_config_32.finalFrameBlocks = ape_config->finalFrameBlocks; |
| 193 | ape_config_32.totalFrames = ape_config->totalFrames; |
| 194 | ape_config_32.bitsPerSample = ape_config->bitsPerSample; |
| 195 | ape_config_32.numChannels = ape_config->numChannels; |
| 196 | ape_config_32.sampleRate = ape_config->sampleRate; |
| 197 | ape_config_32.seekTablePresent = ape_config->seekTablePresent; |
| 198 | |
| 199 | if (copy_to_user((void *)arg, &ape_config_32, |
| 200 | sizeof(ape_config_32))) { |
| 201 | pr_err("%s: copy_to_user for GET_APE_CONFIG_32 failed\n", |
| 202 | __func__); |
| 203 | rc = -EFAULT; |
| 204 | break; |
| 205 | } |
| 206 | break; |
| 207 | } |
| 208 | case AUDIO_SET_APE_CONFIG_32: { |
| 209 | struct msm_audio_ape_config *ape_config; |
| 210 | struct msm_audio_ape_config_32 ape_config_32; |
| 211 | |
| 212 | if (copy_from_user(&ape_config_32, (void *)arg, |
| 213 | sizeof(ape_config_32))) { |
| 214 | pr_err("%s: copy_from_user for SET_APE_CONFIG_32 failed\n" |
| 215 | , __func__); |
| 216 | rc = -EFAULT; |
| 217 | break; |
| 218 | } |
| 219 | ape_config = (struct msm_audio_ape_config *)audio->codec_cfg; |
| 220 | ape_config->compatibleVersion = ape_config_32.compatibleVersion; |
| 221 | ape_config->compressionLevel = |
| 222 | ape_config_32.compressionLevel; |
| 223 | ape_config->formatFlags = ape_config_32.formatFlags; |
| 224 | ape_config->blocksPerFrame = ape_config_32.blocksPerFrame; |
| 225 | ape_config->finalFrameBlocks = ape_config_32.finalFrameBlocks; |
| 226 | ape_config->totalFrames = ape_config_32.totalFrames; |
| 227 | ape_config->bitsPerSample = ape_config_32.bitsPerSample; |
| 228 | ape_config->numChannels = ape_config_32.numChannels; |
| 229 | ape_config->sampleRate = ape_config_32.sampleRate; |
| 230 | ape_config->seekTablePresent = ape_config_32.seekTablePresent; |
| 231 | |
| 232 | break; |
| 233 | } |
| 234 | default: { |
| 235 | pr_debug("%s[%pK]: Calling utils ioctl\n", __func__, audio); |
| 236 | rc = audio->codec_compat_ioctl(file, cmd, arg); |
| 237 | if (rc) |
Xiaojun Sang | c327749 | 2018-09-06 14:34:03 +0800 | [diff] [blame] | 238 | pr_err_ratelimited("Failed in utils_ioctl: %d\n", rc); |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 239 | break; |
| 240 | } |
| 241 | } |
| 242 | return rc; |
| 243 | } |
| 244 | #else |
| 245 | #define audio_compat_ioctl NULL |
| 246 | #endif |
| 247 | |
| 248 | static int audio_open(struct inode *inode, struct file *file) |
| 249 | { |
| 250 | struct q6audio_aio *audio = NULL; |
| 251 | int rc = 0; |
| 252 | |
| 253 | /* 4 bytes represents decoder number, 1 byte for terminate string */ |
| 254 | char name[sizeof "msm_ape_" + 5]; |
| 255 | |
| 256 | audio = kzalloc(sizeof(struct q6audio_aio), GFP_KERNEL); |
| 257 | if (!audio) |
| 258 | return -ENOMEM; |
| 259 | |
| 260 | audio->codec_cfg = kzalloc(sizeof(struct msm_audio_ape_config), |
| 261 | GFP_KERNEL); |
| 262 | if (!audio->codec_cfg) { |
| 263 | kfree(audio); |
| 264 | return -ENOMEM; |
| 265 | } |
| 266 | |
| 267 | audio->pcm_cfg.buffer_size = PCM_BUFSZ_MIN; |
| 268 | audio->miscdevice = &audio_ape_misc; |
| 269 | audio->wakelock_voted = false; |
| 270 | audio->audio_ws_mgr = &audio_ape_ws_mgr; |
| 271 | |
| 272 | audio->ac = q6asm_audio_client_alloc((app_cb) q6_audio_cb, |
| 273 | (void *)audio); |
| 274 | |
| 275 | if (!audio->ac) { |
| 276 | pr_err("Could not allocate memory for audio client\n"); |
| 277 | kfree(audio->codec_cfg); |
| 278 | kfree(audio); |
| 279 | return -ENOMEM; |
| 280 | } |
| 281 | rc = audio_aio_open(audio, file); |
| 282 | if (rc < 0) { |
| 283 | pr_err("%s: audio_aio_open rc=%d\n", |
| 284 | __func__, rc); |
| 285 | goto fail; |
| 286 | } |
| 287 | /* open in T/NT mode */ |
| 288 | if ((file->f_mode & FMODE_WRITE) && (file->f_mode & FMODE_READ)) { |
| 289 | rc = q6asm_open_read_write(audio->ac, FORMAT_LINEAR_PCM, |
| 290 | FORMAT_APE); |
| 291 | if (rc < 0) { |
| 292 | pr_err("NT mode Open failed rc=%d\n", rc); |
| 293 | rc = -ENODEV; |
| 294 | goto fail; |
| 295 | } |
| 296 | audio->feedback = NON_TUNNEL_MODE; |
| 297 | /* open APE decoder, expected frames is always 1*/ |
| 298 | audio->buf_cfg.frames_per_buf = 0x01; |
| 299 | audio->buf_cfg.meta_info_enable = 0x01; |
| 300 | } else if ((file->f_mode & FMODE_WRITE) && |
| 301 | !(file->f_mode & FMODE_READ)) { |
| 302 | rc = q6asm_open_write(audio->ac, FORMAT_APE); |
| 303 | if (rc < 0) { |
| 304 | pr_err("T mode Open failed rc=%d\n", rc); |
| 305 | rc = -ENODEV; |
| 306 | goto fail; |
| 307 | } |
| 308 | audio->feedback = TUNNEL_MODE; |
| 309 | audio->buf_cfg.meta_info_enable = 0x00; |
| 310 | } else { |
| 311 | pr_err("Not supported mode\n"); |
| 312 | rc = -EACCES; |
| 313 | goto fail; |
| 314 | } |
| 315 | |
| 316 | snprintf(name, sizeof(name), "msm_ape_%04x", audio->ac->session); |
| 317 | audio->dentry = config_debugfs_create_file(name, (void *)audio); |
| 318 | |
| 319 | if (IS_ERR_OR_NULL(audio->dentry)) |
| 320 | pr_debug("debugfs_create_file failed\n"); |
| 321 | pr_debug("%s:apedec success mode[%d]session[%d]\n", __func__, |
| 322 | audio->feedback, |
| 323 | audio->ac->session); |
| 324 | return rc; |
| 325 | fail: |
| 326 | q6asm_audio_client_free(audio->ac); |
| 327 | kfree(audio->codec_cfg); |
| 328 | kfree(audio); |
| 329 | return rc; |
| 330 | } |
| 331 | |
| 332 | static const struct file_operations audio_ape_fops = { |
| 333 | .owner = THIS_MODULE, |
| 334 | .open = audio_open, |
| 335 | .release = audio_aio_release, |
| 336 | .unlocked_ioctl = audio_ioctl, |
| 337 | .fsync = audio_aio_fsync, |
| 338 | .compat_ioctl = audio_compat_ioctl |
| 339 | }; |
| 340 | |
| 341 | static struct miscdevice audio_ape_misc = { |
| 342 | .minor = MISC_DYNAMIC_MINOR, |
| 343 | .name = "msm_ape", |
| 344 | .fops = &audio_ape_fops, |
| 345 | }; |
| 346 | |
Laxminath Kasam | 8b1366a | 2017-10-05 01:44:16 +0530 | [diff] [blame] | 347 | int __init audio_ape_init(void) |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 348 | { |
| 349 | int ret = misc_register(&audio_ape_misc); |
| 350 | |
| 351 | if (ret == 0) |
| 352 | device_init_wakeup(audio_ape_misc.this_device, true); |
| 353 | audio_ape_ws_mgr.ref_cnt = 0; |
| 354 | mutex_init(&audio_ape_ws_mgr.ws_lock); |
| 355 | |
| 356 | return ret; |
| 357 | } |
| 358 | |
Asish Bhattacharya | 5faacb3 | 2017-12-04 17:23:15 +0530 | [diff] [blame] | 359 | void audio_ape_exit(void) |
Laxminath Kasam | 8b1366a | 2017-10-05 01:44:16 +0530 | [diff] [blame] | 360 | { |
| 361 | mutex_destroy(&audio_ape_ws_mgr.ws_lock); |
| 362 | misc_deregister(&audio_ape_misc); |
| 363 | } |