Phani Kumar Uppalapati | 4e3c5db | 2013-01-08 10:54:14 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2010-2013, The Linux Foundation. All rights reserved. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [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/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> |
Phani Kumar Uppalapati | 4e3c5db | 2013-01-08 10:54:14 -0800 | [diff] [blame] | 22 | #include <linux/atomic.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 23 | #include <asm/ioctls.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 24 | #include "audio_utils.h" |
| 25 | |
| 26 | static int audio_in_pause(struct q6audio_in *audio) |
| 27 | { |
| 28 | int rc; |
| 29 | |
| 30 | rc = q6asm_cmd(audio->ac, CMD_PAUSE); |
| 31 | if (rc < 0) |
| 32 | pr_err("%s:session id %d: pause cmd failed rc=%d\n", __func__, |
| 33 | audio->ac->session, rc); |
| 34 | |
| 35 | return rc; |
| 36 | } |
| 37 | |
| 38 | static int audio_in_flush(struct q6audio_in *audio) |
| 39 | { |
| 40 | int rc; |
| 41 | |
| 42 | pr_debug("%s:session id %d: flush\n", __func__, audio->ac->session); |
Rajesha Kini | 72a8b61 | 2011-08-10 11:19:55 +0530 | [diff] [blame] | 43 | /* Flush if session running */ |
| 44 | if (audio->enabled) { |
| 45 | /* Implicitly issue a pause to the encoder before flushing */ |
| 46 | rc = audio_in_pause(audio); |
| 47 | if (rc < 0) { |
| 48 | pr_err("%s:session id %d: pause cmd failed rc=%d\n", |
| 49 | __func__, audio->ac->session, rc); |
| 50 | return rc; |
| 51 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 52 | |
Rajesha Kini | 72a8b61 | 2011-08-10 11:19:55 +0530 | [diff] [blame] | 53 | rc = q6asm_cmd(audio->ac, CMD_FLUSH); |
| 54 | if (rc < 0) { |
| 55 | pr_err("%s:session id %d: flush cmd failed rc=%d\n", |
| 56 | __func__, audio->ac->session, rc); |
| 57 | return rc; |
| 58 | } |
| 59 | /* 2nd arg: 0 -> run immediately |
| 60 | 3rd arg: 0 -> msw_ts, 4th arg: 0 ->lsw_ts */ |
| 61 | q6asm_run(audio->ac, 0x00, 0x00, 0x00); |
| 62 | pr_debug("Rerun the session\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 63 | } |
| 64 | audio->rflush = 1; |
| 65 | audio->wflush = 1; |
| 66 | memset(audio->out_frame_info, 0, sizeof(audio->out_frame_info)); |
| 67 | wake_up(&audio->read_wait); |
| 68 | /* get read_lock to ensure no more waiting read thread */ |
| 69 | mutex_lock(&audio->read_lock); |
| 70 | audio->rflush = 0; |
| 71 | mutex_unlock(&audio->read_lock); |
| 72 | wake_up(&audio->write_wait); |
| 73 | /* get write_lock to ensure no more waiting write thread */ |
| 74 | mutex_lock(&audio->write_lock); |
| 75 | audio->wflush = 0; |
| 76 | mutex_unlock(&audio->write_lock); |
| 77 | pr_debug("%s:session id %d: in_bytes %d\n", __func__, |
| 78 | audio->ac->session, atomic_read(&audio->in_bytes)); |
| 79 | pr_debug("%s:session id %d: in_samples %d\n", __func__, |
| 80 | audio->ac->session, atomic_read(&audio->in_samples)); |
| 81 | atomic_set(&audio->in_bytes, 0); |
| 82 | atomic_set(&audio->in_samples, 0); |
Rajesha Kini | 72a8b61 | 2011-08-10 11:19:55 +0530 | [diff] [blame] | 83 | atomic_set(&audio->out_count, 0); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 84 | return 0; |
| 85 | } |
| 86 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 87 | /* must be called with audio->lock held */ |
| 88 | int audio_in_enable(struct q6audio_in *audio) |
| 89 | { |
| 90 | if (audio->enabled) |
| 91 | return 0; |
| 92 | |
| 93 | /* 2nd arg: 0 -> run immediately |
| 94 | 3rd arg: 0 -> msw_ts, 4th arg: 0 ->lsw_ts */ |
| 95 | return q6asm_run(audio->ac, 0x00, 0x00, 0x00); |
| 96 | } |
| 97 | |
| 98 | /* must be called with audio->lock held */ |
| 99 | int audio_in_disable(struct q6audio_in *audio) |
| 100 | { |
| 101 | int rc = 0; |
| 102 | if (audio->opened) { |
| 103 | audio->enabled = 0; |
| 104 | audio->opened = 0; |
| 105 | pr_debug("%s:session id %d: inbytes[%d] insamples[%d]\n", |
| 106 | __func__, audio->ac->session, |
| 107 | atomic_read(&audio->in_bytes), |
| 108 | atomic_read(&audio->in_samples)); |
| 109 | |
| 110 | rc = q6asm_cmd(audio->ac, CMD_CLOSE); |
| 111 | if (rc < 0) |
Phani Kumar Uppalapati | 4e3c5db | 2013-01-08 10:54:14 -0800 | [diff] [blame] | 112 | pr_err("%s:session id %d: Failed to close the session rc=%d\n", |
| 113 | __func__, audio->ac->session, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 114 | rc); |
| 115 | audio->stopped = 1; |
| 116 | memset(audio->out_frame_info, 0, |
| 117 | sizeof(audio->out_frame_info)); |
| 118 | wake_up(&audio->read_wait); |
| 119 | wake_up(&audio->write_wait); |
| 120 | } |
| 121 | pr_debug("%s:session id %d: enabled[%d]\n", __func__, |
| 122 | audio->ac->session, audio->enabled); |
| 123 | return rc; |
| 124 | } |
| 125 | |
| 126 | int audio_in_buf_alloc(struct q6audio_in *audio) |
| 127 | { |
| 128 | int rc = 0; |
| 129 | |
| 130 | switch (audio->buf_alloc) { |
| 131 | case NO_BUF_ALLOC: |
| 132 | if (audio->feedback == NON_TUNNEL_MODE) { |
| 133 | rc = q6asm_audio_client_buf_alloc(IN, |
| 134 | audio->ac, |
| 135 | ALIGN_BUF_SIZE(audio->pcm_cfg.buffer_size), |
| 136 | audio->pcm_cfg.buffer_count); |
| 137 | if (rc < 0) { |
Phani Kumar Uppalapati | 4e3c5db | 2013-01-08 10:54:14 -0800 | [diff] [blame] | 138 | pr_err("%s:session id %d: Buffer Alloc failed\n", |
| 139 | __func__, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 140 | audio->ac->session); |
| 141 | rc = -ENOMEM; |
| 142 | break; |
| 143 | } |
| 144 | audio->buf_alloc |= BUF_ALLOC_IN; |
| 145 | } |
| 146 | rc = q6asm_audio_client_buf_alloc(OUT, audio->ac, |
| 147 | ALIGN_BUF_SIZE(audio->str_cfg.buffer_size), |
| 148 | audio->str_cfg.buffer_count); |
| 149 | if (rc < 0) { |
| 150 | pr_err("%s:session id %d: Buffer Alloc failed rc=%d\n", |
| 151 | __func__, audio->ac->session, rc); |
| 152 | rc = -ENOMEM; |
| 153 | break; |
| 154 | } |
| 155 | audio->buf_alloc |= BUF_ALLOC_OUT; |
| 156 | break; |
| 157 | case BUF_ALLOC_IN: |
| 158 | rc = q6asm_audio_client_buf_alloc(OUT, audio->ac, |
| 159 | ALIGN_BUF_SIZE(audio->str_cfg.buffer_size), |
| 160 | audio->str_cfg.buffer_count); |
| 161 | if (rc < 0) { |
| 162 | pr_err("%s:session id %d: Buffer Alloc failed rc=%d\n", |
| 163 | __func__, audio->ac->session, rc); |
| 164 | rc = -ENOMEM; |
| 165 | break; |
| 166 | } |
| 167 | audio->buf_alloc |= BUF_ALLOC_OUT; |
| 168 | break; |
| 169 | case BUF_ALLOC_OUT: |
| 170 | if (audio->feedback == NON_TUNNEL_MODE) { |
| 171 | rc = q6asm_audio_client_buf_alloc(IN, audio->ac, |
| 172 | ALIGN_BUF_SIZE(audio->pcm_cfg.buffer_size), |
| 173 | audio->pcm_cfg.buffer_count); |
| 174 | if (rc < 0) { |
Phani Kumar Uppalapati | 4e3c5db | 2013-01-08 10:54:14 -0800 | [diff] [blame] | 175 | pr_err("%s:session id %d: Buffer Alloc failed\n", |
| 176 | __func__, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 177 | audio->ac->session); |
| 178 | rc = -ENOMEM; |
| 179 | break; |
| 180 | } |
| 181 | audio->buf_alloc |= BUF_ALLOC_IN; |
| 182 | } |
| 183 | break; |
| 184 | default: |
| 185 | pr_debug("%s:session id %d: buf[%d]\n", __func__, |
| 186 | audio->ac->session, audio->buf_alloc); |
| 187 | } |
| 188 | |
| 189 | return rc; |
| 190 | } |
| 191 | /* ------------------- device --------------------- */ |
| 192 | long audio_in_ioctl(struct file *file, |
| 193 | unsigned int cmd, unsigned long arg) |
| 194 | { |
| 195 | struct q6audio_in *audio = file->private_data; |
| 196 | int rc = 0; |
| 197 | |
| 198 | if (cmd == AUDIO_GET_STATS) { |
| 199 | struct msm_audio_stats stats; |
| 200 | stats.byte_count = atomic_read(&audio->in_bytes); |
| 201 | stats.sample_count = atomic_read(&audio->in_samples); |
| 202 | if (copy_to_user((void *) arg, &stats, sizeof(stats))) |
| 203 | return -EFAULT; |
| 204 | return rc; |
| 205 | } |
| 206 | |
| 207 | mutex_lock(&audio->lock); |
| 208 | switch (cmd) { |
| 209 | case AUDIO_FLUSH: { |
| 210 | /* Make sure we're stopped and we wake any threads |
| 211 | * that might be blocked holding the read_lock. |
| 212 | * While audio->stopped read threads will always |
| 213 | * exit immediately. |
| 214 | */ |
| 215 | rc = audio_in_flush(audio); |
| 216 | if (rc < 0) |
| 217 | pr_err("%s:session id %d: Flush Fail rc=%d\n", |
| 218 | __func__, audio->ac->session, rc); |
Rajesha Kini | 72a8b61 | 2011-08-10 11:19:55 +0530 | [diff] [blame] | 219 | else { /* Register back the flushed read buffer with DSP */ |
| 220 | int cnt = 0; |
| 221 | while (cnt++ < audio->str_cfg.buffer_count) |
| 222 | q6asm_read(audio->ac); /* Push buffer to DSP */ |
| 223 | pr_debug("register the read buffer\n"); |
| 224 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 225 | break; |
| 226 | } |
| 227 | case AUDIO_PAUSE: { |
| 228 | pr_debug("%s:session id %d: AUDIO_PAUSE\n", __func__, |
| 229 | audio->ac->session); |
| 230 | if (audio->enabled) |
| 231 | audio_in_pause(audio); |
| 232 | break; |
| 233 | } |
| 234 | case AUDIO_GET_STREAM_CONFIG: { |
| 235 | struct msm_audio_stream_config cfg; |
| 236 | memset(&cfg, 0, sizeof(cfg)); |
| 237 | cfg.buffer_size = audio->str_cfg.buffer_size; |
| 238 | cfg.buffer_count = audio->str_cfg.buffer_count; |
| 239 | if (copy_to_user((void *)arg, &cfg, sizeof(cfg))) |
| 240 | rc = -EFAULT; |
| 241 | pr_debug("%s:session id %d: AUDIO_GET_STREAM_CONFIG %d %d\n", |
| 242 | __func__, audio->ac->session, cfg.buffer_size, |
| 243 | cfg.buffer_count); |
| 244 | break; |
| 245 | } |
| 246 | case AUDIO_SET_STREAM_CONFIG: { |
| 247 | struct msm_audio_stream_config cfg; |
| 248 | if (copy_from_user(&cfg, (void *)arg, sizeof(cfg))) { |
| 249 | rc = -EFAULT; |
| 250 | break; |
| 251 | } |
| 252 | /* Minimum single frame size, |
| 253 | but with in maximum frames number */ |
| 254 | if ((cfg.buffer_size < (audio->min_frame_size+ \ |
| 255 | sizeof(struct meta_out_dsp))) || |
| 256 | (cfg.buffer_count < FRAME_NUM)) { |
| 257 | rc = -EINVAL; |
| 258 | break; |
| 259 | } |
| 260 | audio->str_cfg.buffer_size = cfg.buffer_size; |
| 261 | audio->str_cfg.buffer_count = cfg.buffer_count; |
| 262 | rc = q6asm_audio_client_buf_alloc(OUT, audio->ac, |
| 263 | ALIGN_BUF_SIZE(audio->str_cfg.buffer_size), |
| 264 | audio->str_cfg.buffer_count); |
| 265 | if (rc < 0) { |
| 266 | pr_err("%s: session id %d: Buffer Alloc failed rc=%d\n", |
| 267 | __func__, audio->ac->session, rc); |
| 268 | rc = -ENOMEM; |
| 269 | break; |
| 270 | } |
| 271 | audio->buf_alloc |= BUF_ALLOC_OUT; |
| 272 | rc = 0; |
| 273 | pr_debug("%s:session id %d: AUDIO_SET_STREAM_CONFIG %d %d\n", |
| 274 | __func__, audio->ac->session, |
| 275 | audio->str_cfg.buffer_size, |
| 276 | audio->str_cfg.buffer_count); |
| 277 | break; |
| 278 | } |
| 279 | case AUDIO_GET_SESSION_ID: { |
| 280 | if (copy_to_user((void *) arg, &audio->ac->session, |
| 281 | sizeof(unsigned short))) { |
| 282 | rc = -EFAULT; |
| 283 | } |
| 284 | break; |
| 285 | } |
| 286 | case AUDIO_SET_BUF_CFG: { |
| 287 | struct msm_audio_buf_cfg cfg; |
| 288 | if (copy_from_user(&cfg, (void *)arg, sizeof(cfg))) { |
| 289 | rc = -EFAULT; |
| 290 | break; |
| 291 | } |
| 292 | if ((audio->feedback == NON_TUNNEL_MODE) && |
| 293 | !cfg.meta_info_enable) { |
| 294 | rc = -EFAULT; |
| 295 | break; |
| 296 | } |
| 297 | |
| 298 | /* Restrict the num of frames per buf to coincide with |
| 299 | * default buf size */ |
| 300 | if (cfg.frames_per_buf > audio->max_frames_per_buf) { |
| 301 | rc = -EFAULT; |
| 302 | break; |
| 303 | } |
| 304 | audio->buf_cfg.meta_info_enable = cfg.meta_info_enable; |
| 305 | audio->buf_cfg.frames_per_buf = cfg.frames_per_buf; |
Phani Kumar Uppalapati | 4e3c5db | 2013-01-08 10:54:14 -0800 | [diff] [blame] | 306 | pr_debug("%s:session id %d: Set-buf-cfg: meta[%d] framesperbuf[%d]\n", |
| 307 | __func__, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 308 | audio->ac->session, cfg.meta_info_enable, |
| 309 | cfg.frames_per_buf); |
| 310 | break; |
| 311 | } |
| 312 | case AUDIO_GET_BUF_CFG: { |
Phani Kumar Uppalapati | 4e3c5db | 2013-01-08 10:54:14 -0800 | [diff] [blame] | 313 | pr_debug("%s:session id %d: Get-buf-cfg: meta[%d] framesperbuf[%d]\n", |
| 314 | __func__, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 315 | audio->ac->session, audio->buf_cfg.meta_info_enable, |
| 316 | audio->buf_cfg.frames_per_buf); |
| 317 | |
| 318 | if (copy_to_user((void *)arg, &audio->buf_cfg, |
| 319 | sizeof(struct msm_audio_buf_cfg))) |
| 320 | rc = -EFAULT; |
| 321 | break; |
| 322 | } |
| 323 | case AUDIO_GET_CONFIG: { |
| 324 | if (copy_to_user((void *)arg, &audio->pcm_cfg, |
| 325 | sizeof(struct msm_audio_config))) |
| 326 | rc = -EFAULT; |
| 327 | break; |
| 328 | |
| 329 | } |
| 330 | case AUDIO_SET_CONFIG: { |
| 331 | struct msm_audio_config cfg; |
| 332 | if (copy_from_user(&cfg, (void *)arg, sizeof(cfg))) { |
| 333 | rc = -EFAULT; |
| 334 | break; |
| 335 | } |
| 336 | if (audio->feedback != NON_TUNNEL_MODE) { |
Phani Kumar Uppalapati | 4e3c5db | 2013-01-08 10:54:14 -0800 | [diff] [blame] | 337 | pr_err("%s:session id %d: Not sufficient permission to change the record mode\n", |
| 338 | __func__, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 339 | audio->ac->session); |
| 340 | rc = -EACCES; |
| 341 | break; |
| 342 | } |
| 343 | if ((cfg.buffer_count > PCM_BUF_COUNT) || |
| 344 | (cfg.buffer_count == 1)) |
| 345 | cfg.buffer_count = PCM_BUF_COUNT; |
| 346 | |
| 347 | audio->pcm_cfg.buffer_count = cfg.buffer_count; |
| 348 | audio->pcm_cfg.buffer_size = cfg.buffer_size; |
| 349 | audio->pcm_cfg.channel_count = cfg.channel_count; |
| 350 | audio->pcm_cfg.sample_rate = cfg.sample_rate; |
| 351 | rc = q6asm_audio_client_buf_alloc(IN, audio->ac, |
| 352 | ALIGN_BUF_SIZE(audio->pcm_cfg.buffer_size), |
| 353 | audio->pcm_cfg.buffer_count); |
| 354 | if (rc < 0) { |
| 355 | pr_err("%s:session id %d: Buffer Alloc failed\n", |
| 356 | __func__, audio->ac->session); |
| 357 | rc = -ENOMEM; |
| 358 | break; |
| 359 | } |
| 360 | audio->buf_alloc |= BUF_ALLOC_IN; |
| 361 | rc = 0; |
| 362 | pr_debug("%s:session id %d: AUDIO_SET_CONFIG %d %d\n", __func__, |
| 363 | audio->ac->session, audio->pcm_cfg.buffer_count, |
| 364 | audio->pcm_cfg.buffer_size); |
| 365 | break; |
| 366 | } |
| 367 | default: |
| 368 | /* call codec specific ioctl */ |
| 369 | rc = audio->enc_ioctl(file, cmd, arg); |
| 370 | } |
| 371 | mutex_unlock(&audio->lock); |
| 372 | return rc; |
| 373 | } |
| 374 | |
| 375 | ssize_t audio_in_read(struct file *file, |
| 376 | char __user *buf, |
| 377 | size_t count, loff_t *pos) |
| 378 | { |
| 379 | struct q6audio_in *audio = file->private_data; |
| 380 | const char __user *start = buf; |
| 381 | unsigned char *data; |
| 382 | uint32_t offset = 0; |
| 383 | uint32_t size = 0; |
| 384 | int rc = 0; |
| 385 | uint32_t idx; |
| 386 | struct meta_out_dsp meta; |
| 387 | uint32_t bytes_to_copy = 0; |
| 388 | uint32_t mfield_size = (audio->buf_cfg.meta_info_enable == 0) ? 0 : |
| 389 | (sizeof(unsigned char) + |
| 390 | (sizeof(struct meta_out_dsp)*(audio->buf_cfg.frames_per_buf))); |
| 391 | |
| 392 | pr_debug("%s:session id %d: read - %d\n", __func__, audio->ac->session, |
| 393 | count); |
| 394 | if (!audio->enabled) |
| 395 | return -EFAULT; |
| 396 | mutex_lock(&audio->read_lock); |
| 397 | while (count > 0) { |
| 398 | rc = wait_event_interruptible( |
| 399 | audio->read_wait, |
| 400 | ((atomic_read(&audio->out_count) > 0) || |
| 401 | (audio->stopped) || |
Phani Kumar Uppalapati | 4e3c5db | 2013-01-08 10:54:14 -0800 | [diff] [blame] | 402 | audio->rflush || audio->eos_rsp || |
| 403 | audio->event_abort)); |
| 404 | |
| 405 | if (audio->event_abort) { |
| 406 | rc = -EIO; |
| 407 | break; |
| 408 | } |
| 409 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 410 | |
| 411 | if (rc < 0) |
| 412 | break; |
| 413 | |
| 414 | if ((audio->stopped && !(atomic_read(&audio->out_count))) || |
| 415 | audio->rflush) { |
Phani Kumar Uppalapati | 4e3c5db | 2013-01-08 10:54:14 -0800 | [diff] [blame] | 416 | pr_debug("%s:session id %d: driver in stop state or flush,No more buf to read", |
| 417 | __func__, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 418 | audio->ac->session); |
| 419 | rc = 0;/* End of File */ |
| 420 | break; |
| 421 | } |
| 422 | if (!(atomic_read(&audio->out_count)) && |
| 423 | (audio->eos_rsp == 1) && |
| 424 | (count >= (sizeof(unsigned char) + |
| 425 | sizeof(struct meta_out_dsp)))) { |
| 426 | unsigned char num_of_frames; |
| 427 | pr_info("%s:session id %d: eos %d at output\n", |
| 428 | __func__, audio->ac->session, audio->eos_rsp); |
| 429 | if (buf != start) |
| 430 | break; |
| 431 | num_of_frames = 0xFF; |
| 432 | if (copy_to_user(buf, &num_of_frames, |
| 433 | sizeof(unsigned char))) { |
| 434 | rc = -EFAULT; |
| 435 | break; |
| 436 | } |
| 437 | buf += sizeof(unsigned char); |
| 438 | meta.frame_size = 0xFFFF; |
| 439 | meta.encoded_pcm_samples = 0xFFFF; |
| 440 | meta.msw_ts = 0x00; |
| 441 | meta.lsw_ts = 0x00; |
| 442 | meta.nflags = AUD_EOS_SET; |
| 443 | audio->eos_rsp = 0; |
| 444 | if (copy_to_user(buf, &meta, sizeof(meta))) { |
| 445 | rc = -EFAULT; |
| 446 | break; |
| 447 | } |
| 448 | buf += sizeof(meta); |
| 449 | break; |
| 450 | } |
| 451 | data = (unsigned char *)q6asm_is_cpu_buf_avail(OUT, audio->ac, |
| 452 | &size, &idx); |
| 453 | if ((count >= (size + mfield_size)) && data) { |
| 454 | if (audio->buf_cfg.meta_info_enable) { |
| 455 | if (copy_to_user(buf, |
| 456 | &audio->out_frame_info[idx][0], |
| 457 | sizeof(unsigned char))) { |
| 458 | rc = -EFAULT; |
| 459 | break; |
| 460 | } |
| 461 | bytes_to_copy = |
| 462 | (size + audio->out_frame_info[idx][1]); |
| 463 | /* Number of frames information copied */ |
| 464 | buf += sizeof(unsigned char); |
| 465 | count -= sizeof(unsigned char); |
| 466 | } else { |
| 467 | offset = audio->out_frame_info[idx][1]; |
| 468 | bytes_to_copy = size; |
| 469 | } |
| 470 | |
| 471 | pr_debug("%s:session id %d: offset=%d nr of frames= %d\n", |
| 472 | __func__, audio->ac->session, |
| 473 | audio->out_frame_info[idx][1], |
| 474 | audio->out_frame_info[idx][0]); |
| 475 | |
| 476 | if (copy_to_user(buf, &data[offset], bytes_to_copy)) { |
| 477 | rc = -EFAULT; |
| 478 | break; |
| 479 | } |
| 480 | count -= bytes_to_copy; |
| 481 | buf += bytes_to_copy; |
| 482 | } else { |
Phani Kumar Uppalapati | 4e3c5db | 2013-01-08 10:54:14 -0800 | [diff] [blame] | 483 | pr_err("%s:session id %d: short read data[%p] bytesavail[%d]bytesrequest[%d]\n", |
| 484 | __func__, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 485 | audio->ac->session, |
| 486 | data, size, count); |
| 487 | } |
| 488 | atomic_dec(&audio->out_count); |
| 489 | q6asm_read(audio->ac); |
| 490 | break; |
| 491 | } |
| 492 | mutex_unlock(&audio->read_lock); |
| 493 | |
| 494 | pr_debug("%s:session id %d: read: %d bytes\n", __func__, |
| 495 | audio->ac->session, (buf-start)); |
| 496 | if (buf > start) |
| 497 | return buf - start; |
| 498 | return rc; |
| 499 | } |
| 500 | |
| 501 | static int extract_meta_info(char *buf, unsigned long *msw_ts, |
| 502 | unsigned long *lsw_ts, unsigned int *flags) |
| 503 | { |
| 504 | struct meta_in *meta = (struct meta_in *)buf; |
| 505 | *msw_ts = meta->ntimestamp.highpart; |
| 506 | *lsw_ts = meta->ntimestamp.lowpart; |
| 507 | *flags = meta->nflags; |
| 508 | return 0; |
| 509 | } |
| 510 | |
| 511 | ssize_t audio_in_write(struct file *file, |
| 512 | const char __user *buf, |
| 513 | size_t count, loff_t *pos) |
| 514 | { |
| 515 | struct q6audio_in *audio = file->private_data; |
| 516 | const char __user *start = buf; |
| 517 | size_t xfer = 0; |
| 518 | char *cpy_ptr; |
| 519 | int rc = 0; |
| 520 | unsigned char *data; |
| 521 | uint32_t size = 0; |
| 522 | uint32_t idx = 0; |
| 523 | uint32_t nflags = 0; |
| 524 | unsigned long msw_ts = 0; |
| 525 | unsigned long lsw_ts = 0; |
| 526 | uint32_t mfield_size = (audio->buf_cfg.meta_info_enable == 0) ? 0 : |
| 527 | sizeof(struct meta_in); |
| 528 | |
| 529 | pr_debug("%s:session id %d: to write[%d]\n", __func__, |
| 530 | audio->ac->session, count); |
| 531 | if (!audio->enabled) |
| 532 | return -EFAULT; |
| 533 | mutex_lock(&audio->write_lock); |
| 534 | |
| 535 | while (count > 0) { |
| 536 | rc = wait_event_interruptible(audio->write_wait, |
| 537 | ((atomic_read(&audio->in_count) > 0) || |
| 538 | (audio->stopped) || |
Phani Kumar Uppalapati | 4e3c5db | 2013-01-08 10:54:14 -0800 | [diff] [blame] | 539 | (audio->wflush) || (audio->event_abort))); |
| 540 | |
| 541 | if (audio->event_abort) { |
| 542 | rc = -EIO; |
| 543 | break; |
| 544 | } |
| 545 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 546 | if (rc < 0) |
| 547 | break; |
| 548 | if (audio->stopped || audio->wflush) { |
| 549 | pr_debug("%s: session id %d: stop or flush\n", __func__, |
| 550 | audio->ac->session); |
| 551 | rc = -EBUSY; |
| 552 | break; |
| 553 | } |
Rajesha Kini | 72a8b61 | 2011-08-10 11:19:55 +0530 | [diff] [blame] | 554 | /* if no PCM data, might have only eos buffer |
| 555 | such case do not hold cpu buffer */ |
| 556 | if ((buf == start) && (count == mfield_size)) { |
| 557 | char eos_buf[sizeof(struct meta_in)]; |
| 558 | /* Processing begining of user buffer */ |
| 559 | if (copy_from_user(eos_buf, buf, mfield_size)) { |
| 560 | rc = -EFAULT; |
| 561 | break; |
| 562 | } |
| 563 | /* Check if EOS flag is set and buffer has |
| 564 | * contains just meta field |
| 565 | */ |
| 566 | extract_meta_info(eos_buf, &msw_ts, &lsw_ts, |
| 567 | &nflags); |
| 568 | buf += mfield_size; |
| 569 | /* send the EOS and return */ |
Phani Kumar Uppalapati | 4e3c5db | 2013-01-08 10:54:14 -0800 | [diff] [blame] | 570 | pr_debug("%s:session id %d: send EOS 0x%8x\n", |
| 571 | __func__, |
Rajesha Kini | 72a8b61 | 2011-08-10 11:19:55 +0530 | [diff] [blame] | 572 | audio->ac->session, nflags); |
| 573 | break; |
| 574 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 575 | data = (unsigned char *)q6asm_is_cpu_buf_avail(IN, audio->ac, |
| 576 | &size, &idx); |
| 577 | if (!data) { |
| 578 | pr_debug("%s:session id %d: No buf available\n", |
| 579 | __func__, audio->ac->session); |
| 580 | continue; |
| 581 | } |
| 582 | cpy_ptr = data; |
| 583 | if (audio->buf_cfg.meta_info_enable) { |
| 584 | if (buf == start) { |
| 585 | /* Processing beginning of user buffer */ |
| 586 | if (copy_from_user(cpy_ptr, buf, mfield_size)) { |
| 587 | rc = -EFAULT; |
| 588 | break; |
| 589 | } |
| 590 | /* Check if EOS flag is set and buffer has |
| 591 | * contains just meta field |
| 592 | */ |
| 593 | extract_meta_info(cpy_ptr, &msw_ts, &lsw_ts, |
| 594 | &nflags); |
| 595 | buf += mfield_size; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 596 | count -= mfield_size; |
| 597 | } else { |
Phani Kumar Uppalapati | 4e3c5db | 2013-01-08 10:54:14 -0800 | [diff] [blame] | 598 | pr_debug("%s:session id %d: continuous buffer\n", |
| 599 | __func__, audio->ac->session); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 600 | } |
| 601 | } |
| 602 | xfer = (count > (audio->pcm_cfg.buffer_size)) ? |
| 603 | (audio->pcm_cfg.buffer_size) : count; |
| 604 | |
| 605 | if (copy_from_user(cpy_ptr, buf, xfer)) { |
| 606 | rc = -EFAULT; |
| 607 | break; |
| 608 | } |
| 609 | rc = q6asm_write(audio->ac, xfer, msw_ts, lsw_ts, 0x00); |
| 610 | if (rc < 0) { |
| 611 | rc = -EFAULT; |
| 612 | break; |
| 613 | } |
| 614 | atomic_dec(&audio->in_count); |
| 615 | count -= xfer; |
| 616 | buf += xfer; |
| 617 | } |
| 618 | mutex_unlock(&audio->write_lock); |
Phani Kumar Uppalapati | 4e3c5db | 2013-01-08 10:54:14 -0800 | [diff] [blame] | 619 | pr_debug("%s:session id %d: eos_condition 0x%8x buf[0x%x] start[0x%x]\n", |
| 620 | __func__, audio->ac->session, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 621 | nflags, (int) buf, (int) start); |
| 622 | if (nflags & AUD_EOS_SET) { |
| 623 | rc = q6asm_cmd(audio->ac, CMD_EOS); |
| 624 | pr_info("%s:session id %d: eos %d at input\n", __func__, |
| 625 | audio->ac->session, audio->eos_rsp); |
| 626 | } |
| 627 | pr_debug("%s:session id %d: Written %d Avail Buf[%d]", __func__, |
| 628 | audio->ac->session, (buf - start - mfield_size), |
| 629 | atomic_read(&audio->in_count)); |
| 630 | if (!rc) { |
| 631 | if (buf > start) |
| 632 | return buf - start; |
| 633 | } |
| 634 | return rc; |
| 635 | } |
| 636 | |
| 637 | int audio_in_release(struct inode *inode, struct file *file) |
| 638 | { |
| 639 | struct q6audio_in *audio = file->private_data; |
| 640 | pr_info("%s: session id %d\n", __func__, audio->ac->session); |
| 641 | mutex_lock(&audio->lock); |
| 642 | audio_in_disable(audio); |
| 643 | q6asm_audio_client_free(audio->ac); |
| 644 | mutex_unlock(&audio->lock); |
| 645 | kfree(audio->enc_cfg); |
Harmandeep Singh | d7d785c | 2011-11-15 11:46:26 -0800 | [diff] [blame] | 646 | kfree(audio->codec_cfg); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 647 | kfree(audio); |
| 648 | return 0; |
| 649 | } |
| 650 | |