Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 1 | /* wmapro audio output device |
| 2 | * |
| 3 | * Copyright (C) 2008 Google, Inc. |
| 4 | * Copyright (C) 2008 HTC Corporation |
Xiaojun Sang | c327749 | 2018-09-06 14:34:03 +0800 | [diff] [blame] | 5 | * Copyright (c) 2009-2018, The Linux Foundation. All rights reserved. |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 6 | * |
| 7 | * This software is licensed under the terms of the GNU General Public |
| 8 | * License version 2, as published by the Free Software Foundation, and |
| 9 | * may be copied, distributed, and modified under those terms. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | */ |
| 17 | |
| 18 | #include <linux/types.h> |
| 19 | #include <linux/msm_audio_wmapro.h> |
| 20 | #include <linux/compat.h> |
| 21 | #include "audio_utils_aio.h" |
| 22 | |
| 23 | static struct miscdevice audio_wmapro_misc; |
| 24 | static struct ws_mgr audio_wmapro_ws_mgr; |
| 25 | |
| 26 | #ifdef CONFIG_DEBUG_FS |
| 27 | static const struct file_operations audio_wmapro_debug_fops = { |
| 28 | .read = audio_aio_debug_read, |
| 29 | .open = audio_aio_debug_open, |
| 30 | }; |
| 31 | #endif |
| 32 | |
| 33 | static long audio_ioctl_shared(struct file *file, unsigned int cmd, |
| 34 | void *arg) |
| 35 | { |
| 36 | struct q6audio_aio *audio = file->private_data; |
| 37 | int rc = 0; |
| 38 | |
| 39 | switch (cmd) { |
| 40 | case AUDIO_START: { |
| 41 | struct asm_wmapro_cfg wmapro_cfg; |
| 42 | struct msm_audio_wmapro_config *wmapro_config; |
| 43 | |
| 44 | pr_debug("%s: AUDIO_START session_id[%d]\n", __func__, |
| 45 | audio->ac->session); |
| 46 | if (audio->feedback == NON_TUNNEL_MODE) { |
| 47 | /* Configure PCM output block */ |
| 48 | rc = q6asm_enc_cfg_blk_pcm_v2(audio->ac, |
| 49 | audio->pcm_cfg.sample_rate, |
| 50 | audio->pcm_cfg.channel_count, |
| 51 | 16, /* bits per sample */ |
| 52 | true, /* use default channel map */ |
| 53 | true, /* use back channel map flavor */ |
| 54 | NULL); |
| 55 | if (rc < 0) { |
| 56 | pr_err("pcm output block config failed\n"); |
| 57 | break; |
| 58 | } |
| 59 | } |
| 60 | wmapro_config = (struct msm_audio_wmapro_config *) |
| 61 | audio->codec_cfg; |
| 62 | if ((wmapro_config->formattag == 0x162) || |
| 63 | (wmapro_config->formattag == 0x163) || |
| 64 | (wmapro_config->formattag == 0x166) || |
| 65 | (wmapro_config->formattag == 0x167)) { |
| 66 | wmapro_cfg.format_tag = wmapro_config->formattag; |
| 67 | } else { |
| 68 | pr_err("%s:AUDIO_START failed: formattag = %d\n", |
| 69 | __func__, wmapro_config->formattag); |
| 70 | rc = -EINVAL; |
| 71 | break; |
| 72 | } |
| 73 | if (wmapro_config->numchannels > 0) { |
| 74 | wmapro_cfg.ch_cfg = wmapro_config->numchannels; |
| 75 | } else { |
| 76 | pr_err("%s:AUDIO_START failed: channels = %d\n", |
| 77 | __func__, wmapro_config->numchannels); |
| 78 | rc = -EINVAL; |
| 79 | break; |
| 80 | } |
| 81 | if (wmapro_config->samplingrate > 0) { |
| 82 | wmapro_cfg.sample_rate = wmapro_config->samplingrate; |
| 83 | } else { |
| 84 | pr_err("%s:AUDIO_START failed: sample_rate = %d\n", |
| 85 | __func__, wmapro_config->samplingrate); |
| 86 | rc = -EINVAL; |
| 87 | break; |
| 88 | } |
| 89 | wmapro_cfg.avg_bytes_per_sec = |
| 90 | wmapro_config->avgbytespersecond; |
| 91 | if ((wmapro_config->asfpacketlength <= 13376) || |
| 92 | (wmapro_config->asfpacketlength > 0)) { |
| 93 | wmapro_cfg.block_align = |
| 94 | wmapro_config->asfpacketlength; |
| 95 | } else { |
| 96 | pr_err("%s:AUDIO_START failed: block_align = %d\n", |
| 97 | __func__, wmapro_config->asfpacketlength); |
| 98 | rc = -EINVAL; |
| 99 | break; |
| 100 | } |
| 101 | if ((wmapro_config->validbitspersample == 16) || |
| 102 | (wmapro_config->validbitspersample == 24)) { |
| 103 | wmapro_cfg.valid_bits_per_sample = |
| 104 | wmapro_config->validbitspersample; |
| 105 | } else { |
| 106 | pr_err("%s:AUDIO_START failed: bitspersample = %d\n", |
| 107 | __func__, wmapro_config->validbitspersample); |
| 108 | rc = -EINVAL; |
| 109 | break; |
| 110 | } |
| 111 | wmapro_cfg.ch_mask = wmapro_config->channelmask; |
| 112 | wmapro_cfg.encode_opt = wmapro_config->encodeopt; |
| 113 | wmapro_cfg.adv_encode_opt = |
| 114 | wmapro_config->advancedencodeopt; |
| 115 | wmapro_cfg.adv_encode_opt2 = |
| 116 | wmapro_config->advancedencodeopt2; |
| 117 | /* Configure Media format block */ |
| 118 | rc = q6asm_media_format_block_wmapro(audio->ac, &wmapro_cfg, |
| 119 | audio->ac->stream_id); |
| 120 | if (rc < 0) { |
| 121 | pr_err("cmd media format block failed\n"); |
| 122 | break; |
| 123 | } |
| 124 | rc = audio_aio_enable(audio); |
| 125 | audio->eos_rsp = 0; |
| 126 | audio->eos_flag = 0; |
| 127 | if (!rc) { |
| 128 | audio->enabled = 1; |
| 129 | } else { |
| 130 | audio->enabled = 0; |
| 131 | pr_err("Audio Start procedure failed rc=%d\n", rc); |
| 132 | break; |
| 133 | } |
| 134 | pr_debug("AUDIO_START success enable[%d]\n", audio->enabled); |
| 135 | if (audio->stopped == 1) |
| 136 | audio->stopped = 0; |
| 137 | break; |
| 138 | } |
| 139 | default: |
| 140 | pr_err("%s: Unknown ioctl cmd %d\n", __func__, cmd); |
| 141 | rc = -EINVAL; |
| 142 | break; |
| 143 | } |
| 144 | return rc; |
| 145 | } |
| 146 | |
| 147 | static long audio_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 148 | { |
| 149 | struct q6audio_aio *audio = file->private_data; |
| 150 | int rc = 0; |
| 151 | |
| 152 | switch (cmd) { |
| 153 | case AUDIO_GET_WMAPRO_CONFIG: { |
| 154 | if (copy_to_user((void *)arg, audio->codec_cfg, |
| 155 | sizeof(struct msm_audio_wmapro_config))) { |
| 156 | pr_err("%s: copy_to_user for AUDIO_GET_WMAPRO_CONFIG failed\n", |
| 157 | __func__); |
| 158 | rc = -EFAULT; |
| 159 | } |
| 160 | break; |
| 161 | } |
| 162 | case AUDIO_SET_WMAPRO_CONFIG: { |
| 163 | if (copy_from_user(audio->codec_cfg, (void *)arg, |
| 164 | sizeof(struct msm_audio_wmapro_config))) { |
| 165 | pr_err("%s: copy_from_user for AUDIO_SET_WMAPRO_CONFIG_V2 failed\n", |
| 166 | __func__); |
| 167 | rc = -EFAULT; |
| 168 | break; |
| 169 | } |
| 170 | break; |
| 171 | } |
| 172 | case AUDIO_START: { |
| 173 | rc = audio_ioctl_shared(file, cmd, (void *)arg); |
| 174 | break; |
| 175 | } |
| 176 | default: { |
| 177 | pr_debug("%s[%pK]: Calling utils ioctl\n", __func__, audio); |
| 178 | rc = audio->codec_ioctl(file, cmd, arg); |
| 179 | if (rc) |
Xiaojun Sang | c327749 | 2018-09-06 14:34:03 +0800 | [diff] [blame] | 180 | pr_err_ratelimited("Failed in utils_ioctl: %d\n", rc); |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 181 | break; |
| 182 | } |
| 183 | } |
| 184 | return rc; |
| 185 | } |
| 186 | |
| 187 | #ifdef CONFIG_COMPAT |
| 188 | |
| 189 | struct msm_audio_wmapro_config32 { |
| 190 | u16 armdatareqthr; |
| 191 | u8 validbitspersample; |
| 192 | u8 numchannels; |
| 193 | u16 formattag; |
| 194 | u32 samplingrate; |
| 195 | u32 avgbytespersecond; |
| 196 | u16 asfpacketlength; |
| 197 | u32 channelmask; |
| 198 | u16 encodeopt; |
| 199 | u16 advancedencodeopt; |
| 200 | u32 advancedencodeopt2; |
| 201 | }; |
| 202 | |
| 203 | enum { |
| 204 | AUDIO_GET_WMAPRO_CONFIG_32 = _IOR(AUDIO_IOCTL_MAGIC, |
| 205 | (AUDIO_MAX_COMMON_IOCTL_NUM+0), struct msm_audio_wmapro_config32), |
| 206 | AUDIO_SET_WMAPRO_CONFIG_32 = _IOW(AUDIO_IOCTL_MAGIC, |
| 207 | (AUDIO_MAX_COMMON_IOCTL_NUM+1), struct msm_audio_wmapro_config32) |
| 208 | }; |
| 209 | |
| 210 | static long audio_compat_ioctl(struct file *file, unsigned int cmd, |
| 211 | unsigned long arg) |
| 212 | { |
| 213 | struct q6audio_aio *audio = file->private_data; |
| 214 | int rc = 0; |
| 215 | |
| 216 | switch (cmd) { |
| 217 | case AUDIO_GET_WMAPRO_CONFIG_32: { |
| 218 | struct msm_audio_wmapro_config *wmapro_config; |
| 219 | struct msm_audio_wmapro_config32 wmapro_config_32; |
| 220 | |
| 221 | memset(&wmapro_config_32, 0, sizeof(wmapro_config_32)); |
| 222 | |
| 223 | wmapro_config = |
| 224 | (struct msm_audio_wmapro_config *)audio->codec_cfg; |
| 225 | wmapro_config_32.armdatareqthr = wmapro_config->armdatareqthr; |
| 226 | wmapro_config_32.validbitspersample = |
| 227 | wmapro_config->validbitspersample; |
| 228 | wmapro_config_32.numchannels = wmapro_config->numchannels; |
| 229 | wmapro_config_32.formattag = wmapro_config->formattag; |
| 230 | wmapro_config_32.samplingrate = wmapro_config->samplingrate; |
| 231 | wmapro_config_32.avgbytespersecond = |
| 232 | wmapro_config->avgbytespersecond; |
| 233 | wmapro_config_32.asfpacketlength = |
| 234 | wmapro_config->asfpacketlength; |
| 235 | wmapro_config_32.channelmask = wmapro_config->channelmask; |
| 236 | wmapro_config_32.encodeopt = wmapro_config->encodeopt; |
| 237 | wmapro_config_32.advancedencodeopt = |
| 238 | wmapro_config->advancedencodeopt; |
| 239 | wmapro_config_32.advancedencodeopt2 = |
| 240 | wmapro_config->advancedencodeopt2; |
| 241 | |
| 242 | if (copy_to_user((void *)arg, &wmapro_config_32, |
| 243 | sizeof(struct msm_audio_wmapro_config32))) { |
| 244 | pr_err("%s: copy_to_user for AUDIO_GET_WMAPRO_CONFIG_V2_32 failed\n", |
| 245 | __func__); |
| 246 | rc = -EFAULT; |
| 247 | } |
| 248 | break; |
| 249 | } |
| 250 | case AUDIO_SET_WMAPRO_CONFIG_32: { |
| 251 | struct msm_audio_wmapro_config *wmapro_config; |
| 252 | struct msm_audio_wmapro_config32 wmapro_config_32; |
| 253 | |
| 254 | if (copy_from_user(&wmapro_config_32, (void *)arg, |
| 255 | sizeof(struct msm_audio_wmapro_config32))) { |
| 256 | pr_err( |
| 257 | "%s: copy_from_user for AUDIO_SET_WMAPRO_CONFG_V2_32 failed\n", |
| 258 | __func__); |
| 259 | rc = -EFAULT; |
| 260 | break; |
| 261 | } |
| 262 | wmapro_config = |
| 263 | (struct msm_audio_wmapro_config *)audio->codec_cfg; |
| 264 | wmapro_config->armdatareqthr = wmapro_config_32.armdatareqthr; |
| 265 | wmapro_config->validbitspersample = |
| 266 | wmapro_config_32.validbitspersample; |
| 267 | wmapro_config->numchannels = wmapro_config_32.numchannels; |
| 268 | wmapro_config->formattag = wmapro_config_32.formattag; |
| 269 | wmapro_config->samplingrate = wmapro_config_32.samplingrate; |
| 270 | wmapro_config->avgbytespersecond = |
| 271 | wmapro_config_32.avgbytespersecond; |
| 272 | wmapro_config->asfpacketlength = |
| 273 | wmapro_config_32.asfpacketlength; |
| 274 | wmapro_config->channelmask = wmapro_config_32.channelmask; |
| 275 | wmapro_config->encodeopt = wmapro_config_32.encodeopt; |
| 276 | wmapro_config->advancedencodeopt = |
| 277 | wmapro_config_32.advancedencodeopt; |
| 278 | wmapro_config->advancedencodeopt2 = |
| 279 | wmapro_config_32.advancedencodeopt2; |
| 280 | break; |
| 281 | } |
| 282 | case AUDIO_START: { |
| 283 | rc = audio_ioctl_shared(file, cmd, (void *)arg); |
| 284 | break; |
| 285 | } |
| 286 | default: { |
| 287 | pr_debug("%s[%pK]: Calling utils ioctl\n", __func__, audio); |
| 288 | rc = audio->codec_compat_ioctl(file, cmd, arg); |
| 289 | if (rc) |
Xiaojun Sang | c327749 | 2018-09-06 14:34:03 +0800 | [diff] [blame] | 290 | pr_err_ratelimited("Failed in utils_ioctl: %d\n", rc); |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 291 | break; |
| 292 | } |
| 293 | } |
| 294 | return rc; |
| 295 | } |
| 296 | #else |
| 297 | #define audio_compat_ioctl NULL |
| 298 | #endif |
| 299 | |
| 300 | static int audio_open(struct inode *inode, struct file *file) |
| 301 | { |
| 302 | struct q6audio_aio *audio = NULL; |
| 303 | int rc = 0; |
| 304 | |
| 305 | #ifdef CONFIG_DEBUG_FS |
| 306 | /* 4 bytes represents decoder number, 1 byte for terminate string */ |
| 307 | char name[sizeof "msm_wmapro_" + 5]; |
| 308 | #endif |
| 309 | audio = kzalloc(sizeof(struct q6audio_aio), GFP_KERNEL); |
| 310 | |
| 311 | if (audio == NULL) |
| 312 | return -ENOMEM; |
| 313 | |
| 314 | audio->codec_cfg = kzalloc(sizeof(struct msm_audio_wmapro_config), |
| 315 | GFP_KERNEL); |
| 316 | if (audio->codec_cfg == NULL) { |
| 317 | kfree(audio); |
| 318 | return -ENOMEM; |
| 319 | } |
| 320 | |
| 321 | |
| 322 | audio->pcm_cfg.buffer_size = PCM_BUFSZ_MIN; |
| 323 | audio->miscdevice = &audio_wmapro_misc; |
| 324 | audio->wakelock_voted = false; |
| 325 | audio->audio_ws_mgr = &audio_wmapro_ws_mgr; |
| 326 | |
| 327 | audio->ac = q6asm_audio_client_alloc((app_cb) q6_audio_cb, |
| 328 | (void *)audio); |
| 329 | |
| 330 | if (!audio->ac) { |
| 331 | pr_err("Could not allocate memory for audio client\n"); |
| 332 | kfree(audio->codec_cfg); |
| 333 | kfree(audio); |
| 334 | return -ENOMEM; |
| 335 | } |
| 336 | |
| 337 | rc = audio_aio_open(audio, file); |
| 338 | if (rc < 0) { |
| 339 | pr_err("%s: audio_aio_open rc=%d\n", |
| 340 | __func__, rc); |
| 341 | goto fail; |
| 342 | } |
| 343 | /* open in T/NT mode */ |
| 344 | if ((file->f_mode & FMODE_WRITE) && (file->f_mode & FMODE_READ)) { |
| 345 | rc = q6asm_open_read_write(audio->ac, FORMAT_LINEAR_PCM, |
| 346 | FORMAT_WMA_V10PRO); |
| 347 | if (rc < 0) { |
| 348 | pr_err("NT mode Open failed rc=%d\n", rc); |
| 349 | rc = -ENODEV; |
| 350 | goto fail; |
| 351 | } |
| 352 | audio->feedback = NON_TUNNEL_MODE; |
| 353 | /* open WMA decoder, expected frames is always 1*/ |
| 354 | audio->buf_cfg.frames_per_buf = 0x01; |
| 355 | audio->buf_cfg.meta_info_enable = 0x01; |
| 356 | } else if ((file->f_mode & FMODE_WRITE) && |
| 357 | !(file->f_mode & FMODE_READ)) { |
| 358 | rc = q6asm_open_write(audio->ac, FORMAT_WMA_V10PRO); |
| 359 | if (rc < 0) { |
| 360 | pr_err("T mode Open failed rc=%d\n", rc); |
| 361 | rc = -ENODEV; |
| 362 | goto fail; |
| 363 | } |
| 364 | audio->feedback = TUNNEL_MODE; |
| 365 | audio->buf_cfg.meta_info_enable = 0x00; |
| 366 | } else { |
| 367 | pr_err("Not supported mode\n"); |
| 368 | rc = -EACCES; |
| 369 | goto fail; |
| 370 | } |
| 371 | |
| 372 | #ifdef CONFIG_DEBUG_FS |
| 373 | snprintf(name, sizeof(name), "msm_wmapro_%04x", audio->ac->session); |
| 374 | audio->dentry = debugfs_create_file(name, S_IFREG | 0444, |
| 375 | NULL, (void *)audio, |
| 376 | &audio_wmapro_debug_fops); |
| 377 | |
| 378 | if (IS_ERR(audio->dentry)) |
| 379 | pr_debug("debugfs_create_file failed\n"); |
| 380 | #endif |
| 381 | pr_info("%s:wmapro decoder open success, session_id = %d\n", __func__, |
| 382 | audio->ac->session); |
| 383 | return rc; |
| 384 | fail: |
| 385 | q6asm_audio_client_free(audio->ac); |
| 386 | kfree(audio->codec_cfg); |
| 387 | kfree(audio); |
| 388 | return rc; |
| 389 | } |
| 390 | |
| 391 | static const struct file_operations audio_wmapro_fops = { |
| 392 | .owner = THIS_MODULE, |
| 393 | .open = audio_open, |
| 394 | .release = audio_aio_release, |
| 395 | .unlocked_ioctl = audio_ioctl, |
| 396 | .fsync = audio_aio_fsync, |
| 397 | .compat_ioctl = audio_compat_ioctl |
| 398 | }; |
| 399 | |
| 400 | static struct miscdevice audio_wmapro_misc = { |
| 401 | .minor = MISC_DYNAMIC_MINOR, |
| 402 | .name = "msm_wmapro", |
| 403 | .fops = &audio_wmapro_fops, |
| 404 | }; |
| 405 | |
Laxminath Kasam | 8b1366a | 2017-10-05 01:44:16 +0530 | [diff] [blame] | 406 | int __init audio_wmapro_init(void) |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 407 | { |
| 408 | int ret = misc_register(&audio_wmapro_misc); |
| 409 | |
| 410 | if (ret == 0) |
| 411 | device_init_wakeup(audio_wmapro_misc.this_device, true); |
| 412 | audio_wmapro_ws_mgr.ref_cnt = 0; |
| 413 | mutex_init(&audio_wmapro_ws_mgr.ws_lock); |
| 414 | |
| 415 | return ret; |
| 416 | } |
| 417 | |
Asish Bhattacharya | 5faacb3 | 2017-12-04 17:23:15 +0530 | [diff] [blame] | 418 | void audio_wmapro_exit(void) |
Laxminath Kasam | 8b1366a | 2017-10-05 01:44:16 +0530 | [diff] [blame] | 419 | { |
| 420 | mutex_destroy(&audio_wmapro_ws_mgr.ws_lock); |
| 421 | misc_deregister(&audio_wmapro_misc); |
| 422 | } |