Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 1 | /* Copyright (c) 2016-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/types.h> |
| 15 | #include <linux/msm_audio_g711_dec.h> |
| 16 | #include <linux/compat.h> |
| 17 | #include "audio_utils_aio.h" |
| 18 | |
| 19 | static struct miscdevice audio_g711alaw_misc; |
| 20 | static struct ws_mgr audio_g711_ws_mgr; |
| 21 | |
| 22 | static const struct file_operations audio_g711_debug_fops = { |
| 23 | .read = audio_aio_debug_read, |
| 24 | .open = audio_aio_debug_open, |
| 25 | }; |
| 26 | |
| 27 | static struct dentry *config_debugfs_create_file(const char *name, void *data) |
| 28 | { |
| 29 | return debugfs_create_file(name, S_IFREG | 0444, |
| 30 | NULL, (void *)data, &audio_g711_debug_fops); |
| 31 | } |
| 32 | |
| 33 | static int g711_channel_map(u8 *channel_mapping, uint32_t channels); |
| 34 | |
| 35 | static long audio_ioctl_shared(struct file *file, unsigned int cmd, |
| 36 | void *arg) |
| 37 | { |
| 38 | struct q6audio_aio *audio = file->private_data; |
| 39 | int rc = 0; |
| 40 | |
| 41 | switch (cmd) { |
| 42 | case AUDIO_START: { |
| 43 | struct asm_g711_dec_cfg g711_dec_cfg; |
| 44 | struct msm_audio_g711_dec_config *g711_dec_config; |
| 45 | u8 channel_mapping[PCM_FORMAT_MAX_NUM_CHANNEL]; |
| 46 | |
| 47 | memset(channel_mapping, 0, PCM_FORMAT_MAX_NUM_CHANNEL); |
| 48 | memset(&g711_dec_cfg, 0, sizeof(g711_dec_cfg)); |
| 49 | |
| 50 | if (g711_channel_map(channel_mapping, |
| 51 | audio->pcm_cfg.channel_count)) { |
| 52 | pr_err("%s: setting channel map failed %d\n", |
| 53 | __func__, audio->pcm_cfg.channel_count); |
| 54 | } |
| 55 | |
| 56 | pr_debug("%s[%pK]: AUDIO_START session_id[%d]\n", __func__, |
| 57 | audio, audio->ac->session); |
| 58 | if (audio->feedback == NON_TUNNEL_MODE) { |
| 59 | /* Configure PCM output block */ |
| 60 | rc = q6asm_enc_cfg_blk_pcm_v2(audio->ac, |
| 61 | audio->pcm_cfg.sample_rate, |
| 62 | audio->pcm_cfg.channel_count, |
| 63 | 16, /*bits per sample*/ |
| 64 | false, false, channel_mapping); |
| 65 | if (rc < 0) { |
| 66 | pr_err("%s: pcm output block config failed rc=%d\n", |
| 67 | __func__, rc); |
| 68 | break; |
| 69 | } |
| 70 | } |
| 71 | g711_dec_config = |
| 72 | (struct msm_audio_g711_dec_config *)audio->codec_cfg; |
| 73 | g711_dec_cfg.sample_rate = g711_dec_config->sample_rate; |
| 74 | /* Configure Media format block */ |
| 75 | rc = q6asm_media_format_block_g711(audio->ac, &g711_dec_cfg, |
| 76 | audio->ac->stream_id); |
| 77 | if (rc < 0) { |
| 78 | pr_err("%s: cmd media format block failed rc=%d\n", |
| 79 | __func__, rc); |
| 80 | break; |
| 81 | } |
| 82 | rc = audio_aio_enable(audio); |
| 83 | audio->eos_rsp = 0; |
| 84 | audio->eos_flag = 0; |
| 85 | if (!rc) { |
| 86 | audio->enabled = 1; |
| 87 | } else { |
| 88 | audio->enabled = 0; |
| 89 | pr_err("%s: Audio Start procedure failed rc=%d\n", |
| 90 | __func__, rc); |
| 91 | break; |
| 92 | } |
| 93 | pr_debug("%s: AUDIO_START success enable[%d]\n", |
| 94 | __func__, audio->enabled); |
| 95 | if (audio->stopped == 1) |
| 96 | audio->stopped = 0; |
| 97 | break; |
| 98 | } |
| 99 | default: |
| 100 | pr_debug("%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_G711_DEC_CONFIG: { |
| 117 | if (copy_to_user((void *)arg, audio->codec_cfg, |
| 118 | sizeof(struct msm_audio_g711_dec_config))) { |
| 119 | pr_err("%s: copy_to_user for AUDIO_GET_G711_DEC_CONFIG failed\n", |
| 120 | __func__); |
| 121 | rc = -EFAULT; |
| 122 | } |
| 123 | break; |
| 124 | } |
| 125 | case AUDIO_SET_G711_DEC_CONFIG: { |
| 126 | if (copy_from_user(audio->codec_cfg, (void *)arg, |
| 127 | sizeof(struct msm_audio_g711_dec_config))) { |
| 128 | pr_err("%s: copy_from_user for AUDIO_SET_G711_DEC_CONFIG failed\n", |
| 129 | __func__); |
| 130 | rc = -EFAULT; |
| 131 | } |
| 132 | break; |
| 133 | } |
| 134 | default: { |
| 135 | rc = audio->codec_ioctl(file, cmd, arg); |
| 136 | if (rc) |
| 137 | pr_err("%s: Failed in audio_aio_ioctl: %d cmd=%d\n", |
| 138 | __func__, rc, cmd); |
| 139 | break; |
| 140 | } |
| 141 | } |
| 142 | return rc; |
| 143 | } |
| 144 | |
| 145 | #ifdef CONFIG_COMPAT |
| 146 | struct msm_audio_g711_dec_config_32 { |
| 147 | u32 sample_rate; |
| 148 | }; |
| 149 | |
| 150 | enum { |
| 151 | AUDIO_SET_G711_DEC_CONFIG_32 = _IOW(AUDIO_IOCTL_MAGIC, |
| 152 | (AUDIO_MAX_COMMON_IOCTL_NUM+0), struct msm_audio_g711_dec_config_32), |
| 153 | AUDIO_GET_G711_DEC_CONFIG_32 = _IOR(AUDIO_IOCTL_MAGIC, |
| 154 | (AUDIO_MAX_COMMON_IOCTL_NUM+1), struct msm_audio_g711_dec_config_32) |
| 155 | }; |
| 156 | |
| 157 | static long audio_compat_ioctl(struct file *file, unsigned int cmd, |
| 158 | unsigned long arg) |
| 159 | { |
| 160 | struct q6audio_aio *audio = file->private_data; |
| 161 | int rc = 0; |
| 162 | |
| 163 | switch (cmd) { |
| 164 | case AUDIO_START: { |
| 165 | rc = audio_ioctl_shared(file, cmd, (void *)arg); |
| 166 | break; |
| 167 | } |
| 168 | case AUDIO_GET_G711_DEC_CONFIG_32: { |
| 169 | struct msm_audio_g711_dec_config *g711_dec_config; |
| 170 | struct msm_audio_g711_dec_config_32 g711_dec_config_32; |
| 171 | |
| 172 | memset(&g711_dec_config_32, 0, sizeof(g711_dec_config_32)); |
| 173 | |
| 174 | g711_dec_config = |
| 175 | (struct msm_audio_g711_dec_config *)audio->codec_cfg; |
| 176 | g711_dec_config_32.sample_rate = g711_dec_config->sample_rate; |
| 177 | |
| 178 | if (copy_to_user((void *)arg, &g711_dec_config_32, |
| 179 | sizeof(g711_dec_config_32))) { |
| 180 | pr_err("%s: copy_to_user for AUDIO_GET_G711_DEC_CONFIG_32 failed\n", |
| 181 | __func__); |
| 182 | rc = -EFAULT; |
| 183 | } |
| 184 | break; |
| 185 | } |
| 186 | case AUDIO_SET_G711_DEC_CONFIG_32: { |
| 187 | struct msm_audio_g711_dec_config *g711_dec_config; |
| 188 | struct msm_audio_g711_dec_config_32 g711_dec_config_32; |
| 189 | |
| 190 | memset(&g711_dec_config_32, 0, sizeof(g711_dec_config_32)); |
| 191 | |
| 192 | if (copy_from_user(&g711_dec_config_32, (void *)arg, |
| 193 | sizeof(g711_dec_config_32))) { |
| 194 | pr_err("%s: copy_from_user for AUDIO_SET_G711_DEC_CONFIG_32 failed\n", |
| 195 | __func__); |
| 196 | rc = -EFAULT; |
| 197 | break; |
| 198 | } |
| 199 | |
| 200 | g711_dec_config = |
| 201 | (struct msm_audio_g711_dec_config *)audio->codec_cfg; |
| 202 | g711_dec_config->sample_rate = g711_dec_config_32.sample_rate; |
| 203 | |
| 204 | break; |
| 205 | } |
| 206 | default: { |
| 207 | rc = audio->codec_compat_ioctl(file, cmd, arg); |
| 208 | if (rc) |
| 209 | pr_err("%s: Failed in audio_aio_compat_ioctl: %d cmd=%d\n", |
| 210 | __func__, rc, cmd); |
| 211 | break; |
| 212 | } |
| 213 | } |
| 214 | return rc; |
| 215 | } |
| 216 | #else |
| 217 | #define audio_compat_ioctl NULL |
| 218 | #endif |
| 219 | |
| 220 | static int audio_open(struct inode *inode, struct file *file) |
| 221 | { |
| 222 | struct q6audio_aio *audio = NULL; |
| 223 | int rc = 0; |
| 224 | /* 4 bytes represents decoder number, 1 byte for terminate string */ |
| 225 | char name[sizeof "msm_g711_" + 5]; |
| 226 | |
| 227 | audio = kzalloc(sizeof(struct q6audio_aio), GFP_KERNEL); |
| 228 | |
| 229 | if (!audio) |
| 230 | return -ENOMEM; |
| 231 | audio->codec_cfg = kzalloc(sizeof(struct msm_audio_g711_dec_config), |
| 232 | GFP_KERNEL); |
| 233 | if (!audio->codec_cfg) { |
| 234 | kfree(audio); |
| 235 | return -ENOMEM; |
| 236 | } |
| 237 | |
| 238 | audio->pcm_cfg.buffer_size = PCM_BUFSZ_MIN; |
| 239 | audio->miscdevice = &audio_g711alaw_misc; |
| 240 | audio->wakelock_voted = false; |
| 241 | audio->audio_ws_mgr = &audio_g711_ws_mgr; |
| 242 | |
| 243 | init_waitqueue_head(&audio->event_wait); |
| 244 | |
| 245 | audio->ac = q6asm_audio_client_alloc((app_cb) q6_audio_cb, |
| 246 | (void *)audio); |
| 247 | |
| 248 | if (!audio->ac) { |
| 249 | pr_err("%s: Could not allocate memory for audio client\n", |
| 250 | __func__); |
| 251 | kfree(audio->codec_cfg); |
| 252 | kfree(audio); |
| 253 | return -ENOMEM; |
| 254 | } |
| 255 | rc = audio_aio_open(audio, file); |
| 256 | if (rc < 0) { |
| 257 | pr_err("%s: audio_aio_open rc=%d\n", |
| 258 | __func__, rc); |
| 259 | goto fail; |
| 260 | } |
| 261 | /* open in T/NT mode */ /*foramt:G711_ALAW*/ |
| 262 | if ((file->f_mode & FMODE_WRITE) && (file->f_mode & FMODE_READ)) { |
| 263 | rc = q6asm_open_read_write(audio->ac, FORMAT_LINEAR_PCM, |
| 264 | FORMAT_G711_ALAW_FS); |
| 265 | if (rc < 0) { |
| 266 | pr_err("%s: NT mode Open failed rc=%d\n", __func__, rc); |
| 267 | goto fail; |
| 268 | } |
| 269 | audio->feedback = NON_TUNNEL_MODE; |
| 270 | /* open G711 decoder, expected frames is always 1*/ |
| 271 | audio->buf_cfg.frames_per_buf = 0x01; |
| 272 | audio->buf_cfg.meta_info_enable = 0x01; |
| 273 | } else if ((file->f_mode & FMODE_WRITE) && |
| 274 | !(file->f_mode & FMODE_READ)) { |
| 275 | rc = q6asm_open_write(audio->ac, FORMAT_G711_ALAW_FS); |
| 276 | if (rc < 0) { |
| 277 | pr_err("%s: T mode Open failed rc=%d\n", __func__, rc); |
| 278 | goto fail; |
| 279 | } |
| 280 | audio->feedback = TUNNEL_MODE; |
| 281 | audio->buf_cfg.meta_info_enable = 0x00; |
| 282 | } else { |
| 283 | pr_err("%s: %d mode is not supported mode\n", |
| 284 | __func__, file->f_mode); |
| 285 | rc = -EACCES; |
| 286 | goto fail; |
| 287 | } |
| 288 | |
| 289 | snprintf(name, sizeof(name), "msm_g711_%04x", audio->ac->session); |
| 290 | audio->dentry = config_debugfs_create_file(name, (void *)audio); |
| 291 | |
| 292 | if (IS_ERR_OR_NULL(audio->dentry)) |
| 293 | pr_debug("%s: debugfs_create_file failed\n", __func__); |
| 294 | pr_debug("%s: g711dec success mode[%d]session[%d]\n", __func__, |
| 295 | audio->feedback, |
| 296 | audio->ac->session); |
| 297 | return rc; |
| 298 | fail: |
| 299 | q6asm_audio_client_free(audio->ac); |
| 300 | kfree(audio->codec_cfg); |
| 301 | kfree(audio); |
| 302 | return rc; |
| 303 | } |
| 304 | |
| 305 | static int g711_channel_map(u8 *channel_mapping, uint32_t channels) |
| 306 | { |
| 307 | u8 *lchannel_mapping; |
| 308 | |
| 309 | lchannel_mapping = channel_mapping; |
| 310 | pr_debug("%s: channels passed: %d\n", __func__, channels); |
| 311 | if (channels == 1) { |
| 312 | lchannel_mapping[0] = PCM_CHANNEL_FC; |
| 313 | } else if (channels == 2) { |
| 314 | lchannel_mapping[0] = PCM_CHANNEL_FL; |
| 315 | lchannel_mapping[1] = PCM_CHANNEL_FR; |
| 316 | } else if (channels == 3) { |
| 317 | lchannel_mapping[0] = PCM_CHANNEL_FC; |
| 318 | lchannel_mapping[1] = PCM_CHANNEL_FL; |
| 319 | lchannel_mapping[2] = PCM_CHANNEL_FR; |
| 320 | } else if (channels == 4) { |
| 321 | lchannel_mapping[0] = PCM_CHANNEL_FC; |
| 322 | lchannel_mapping[1] = PCM_CHANNEL_FL; |
| 323 | lchannel_mapping[2] = PCM_CHANNEL_FR; |
| 324 | lchannel_mapping[3] = PCM_CHANNEL_CS; |
| 325 | } else if (channels == 5) { |
| 326 | lchannel_mapping[0] = PCM_CHANNEL_FC; |
| 327 | lchannel_mapping[1] = PCM_CHANNEL_FL; |
| 328 | lchannel_mapping[2] = PCM_CHANNEL_FR; |
| 329 | lchannel_mapping[3] = PCM_CHANNEL_LS; |
| 330 | lchannel_mapping[4] = PCM_CHANNEL_RS; |
| 331 | } else if (channels == 6) { |
| 332 | lchannel_mapping[0] = PCM_CHANNEL_FC; |
| 333 | lchannel_mapping[1] = PCM_CHANNEL_FL; |
| 334 | lchannel_mapping[2] = PCM_CHANNEL_FR; |
| 335 | lchannel_mapping[3] = PCM_CHANNEL_LS; |
| 336 | lchannel_mapping[4] = PCM_CHANNEL_RS; |
| 337 | lchannel_mapping[5] = PCM_CHANNEL_LFE; |
| 338 | } else if (channels == 7) { |
| 339 | lchannel_mapping[0] = PCM_CHANNEL_FC; |
| 340 | lchannel_mapping[1] = PCM_CHANNEL_FL; |
| 341 | lchannel_mapping[2] = PCM_CHANNEL_FR; |
| 342 | lchannel_mapping[3] = PCM_CHANNEL_LS; |
| 343 | lchannel_mapping[4] = PCM_CHANNEL_RS; |
| 344 | lchannel_mapping[5] = PCM_CHANNEL_CS; |
| 345 | lchannel_mapping[6] = PCM_CHANNEL_LFE; |
| 346 | } else if (channels == 8) { |
| 347 | lchannel_mapping[0] = PCM_CHANNEL_FC; |
| 348 | lchannel_mapping[1] = PCM_CHANNEL_FLC; |
| 349 | lchannel_mapping[2] = PCM_CHANNEL_FRC; |
| 350 | lchannel_mapping[3] = PCM_CHANNEL_FL; |
| 351 | lchannel_mapping[4] = PCM_CHANNEL_FR; |
| 352 | lchannel_mapping[5] = PCM_CHANNEL_LS; |
| 353 | lchannel_mapping[6] = PCM_CHANNEL_RS; |
| 354 | lchannel_mapping[7] = PCM_CHANNEL_LFE; |
| 355 | } else { |
| 356 | pr_err("%s: ERROR.unsupported num_ch = %u\n", |
| 357 | __func__, channels); |
| 358 | return -EINVAL; |
| 359 | } |
| 360 | return 0; |
| 361 | } |
| 362 | |
| 363 | static const struct file_operations audio_g711_fops = { |
| 364 | .owner = THIS_MODULE, |
| 365 | .open = audio_open, |
| 366 | .release = audio_aio_release, |
| 367 | .unlocked_ioctl = audio_ioctl, |
| 368 | .compat_ioctl = audio_compat_ioctl, |
| 369 | .fsync = audio_aio_fsync, |
| 370 | }; |
| 371 | |
| 372 | static struct miscdevice audio_g711alaw_misc = { |
| 373 | .minor = MISC_DYNAMIC_MINOR, |
| 374 | .name = "msm_g711alaw", |
| 375 | .fops = &audio_g711_fops, |
| 376 | }; |
| 377 | |
Laxminath Kasam | 8b1366a | 2017-10-05 01:44:16 +0530 | [diff] [blame] | 378 | int __init audio_g711alaw_init(void) |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 379 | { |
| 380 | int ret = misc_register(&audio_g711alaw_misc); |
| 381 | |
| 382 | if (ret == 0) |
| 383 | device_init_wakeup(audio_g711alaw_misc.this_device, true); |
| 384 | audio_g711_ws_mgr.ref_cnt = 0; |
| 385 | mutex_init(&audio_g711_ws_mgr.ws_lock); |
| 386 | |
| 387 | return ret; |
| 388 | } |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 389 | |
Asish Bhattacharya | 5faacb3 | 2017-12-04 17:23:15 +0530 | [diff] [blame] | 390 | void audio_g711alaw_exit(void) |
Laxminath Kasam | 8b1366a | 2017-10-05 01:44:16 +0530 | [diff] [blame] | 391 | { |
| 392 | mutex_destroy(&audio_g711_ws_mgr.ws_lock); |
| 393 | misc_deregister(&audio_g711alaw_misc); |
| 394 | } |