blob: 5355de17843148073732093db01e0c8cc7595094 [file] [log] [blame]
Phani Kumar Uppalapati4e3c5db2013-01-08 10:54:14 -08001/* Copyright (c) 2010-2013, The Linux Foundation. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
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 Uppalapati4e3c5db2013-01-08 10:54:14 -080022#include <linux/atomic.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070023#include <asm/ioctls.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070024#include "audio_utils.h"
25
26static 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
38static 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 Kini72a8b612011-08-10 11:19:55 +053043 /* 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 Huntsman3f2bc4d2011-08-16 17:27:22 -070052
Rajesha Kini72a8b612011-08-10 11:19:55 +053053 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 Huntsman3f2bc4d2011-08-16 17:27:22 -070063 }
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 Kini72a8b612011-08-10 11:19:55 +053083 atomic_set(&audio->out_count, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070084 return 0;
85}
86
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070087/* must be called with audio->lock held */
88int 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 */
99int 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 Uppalapati4e3c5db2013-01-08 10:54:14 -0800112 pr_err("%s:session id %d: Failed to close the session rc=%d\n",
113 __func__, audio->ac->session,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700114 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
126int 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 Uppalapati4e3c5db2013-01-08 10:54:14 -0800138 pr_err("%s:session id %d: Buffer Alloc failed\n",
139 __func__,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700140 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 Uppalapati4e3c5db2013-01-08 10:54:14 -0800175 pr_err("%s:session id %d: Buffer Alloc failed\n",
176 __func__,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700177 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 --------------------- */
192long 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;
Kiran Kandiccf19062013-10-09 11:27:59 -0700200 memset(&stats, 0, sizeof(stats));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700201 stats.byte_count = atomic_read(&audio->in_bytes);
202 stats.sample_count = atomic_read(&audio->in_samples);
203 if (copy_to_user((void *) arg, &stats, sizeof(stats)))
204 return -EFAULT;
205 return rc;
206 }
207
208 mutex_lock(&audio->lock);
209 switch (cmd) {
210 case AUDIO_FLUSH: {
211 /* Make sure we're stopped and we wake any threads
212 * that might be blocked holding the read_lock.
213 * While audio->stopped read threads will always
214 * exit immediately.
215 */
216 rc = audio_in_flush(audio);
217 if (rc < 0)
218 pr_err("%s:session id %d: Flush Fail rc=%d\n",
219 __func__, audio->ac->session, rc);
Rajesha Kini72a8b612011-08-10 11:19:55 +0530220 else { /* Register back the flushed read buffer with DSP */
221 int cnt = 0;
222 while (cnt++ < audio->str_cfg.buffer_count)
223 q6asm_read(audio->ac); /* Push buffer to DSP */
224 pr_debug("register the read buffer\n");
225 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700226 break;
227 }
228 case AUDIO_PAUSE: {
229 pr_debug("%s:session id %d: AUDIO_PAUSE\n", __func__,
230 audio->ac->session);
231 if (audio->enabled)
232 audio_in_pause(audio);
233 break;
234 }
235 case AUDIO_GET_STREAM_CONFIG: {
236 struct msm_audio_stream_config cfg;
237 memset(&cfg, 0, sizeof(cfg));
238 cfg.buffer_size = audio->str_cfg.buffer_size;
239 cfg.buffer_count = audio->str_cfg.buffer_count;
240 if (copy_to_user((void *)arg, &cfg, sizeof(cfg)))
241 rc = -EFAULT;
242 pr_debug("%s:session id %d: AUDIO_GET_STREAM_CONFIG %d %d\n",
243 __func__, audio->ac->session, cfg.buffer_size,
244 cfg.buffer_count);
245 break;
246 }
247 case AUDIO_SET_STREAM_CONFIG: {
248 struct msm_audio_stream_config cfg;
249 if (copy_from_user(&cfg, (void *)arg, sizeof(cfg))) {
250 rc = -EFAULT;
251 break;
252 }
253 /* Minimum single frame size,
254 but with in maximum frames number */
255 if ((cfg.buffer_size < (audio->min_frame_size+ \
256 sizeof(struct meta_out_dsp))) ||
257 (cfg.buffer_count < FRAME_NUM)) {
258 rc = -EINVAL;
259 break;
260 }
261 audio->str_cfg.buffer_size = cfg.buffer_size;
262 audio->str_cfg.buffer_count = cfg.buffer_count;
263 rc = q6asm_audio_client_buf_alloc(OUT, audio->ac,
264 ALIGN_BUF_SIZE(audio->str_cfg.buffer_size),
265 audio->str_cfg.buffer_count);
266 if (rc < 0) {
267 pr_err("%s: session id %d: Buffer Alloc failed rc=%d\n",
268 __func__, audio->ac->session, rc);
269 rc = -ENOMEM;
270 break;
271 }
272 audio->buf_alloc |= BUF_ALLOC_OUT;
273 rc = 0;
274 pr_debug("%s:session id %d: AUDIO_SET_STREAM_CONFIG %d %d\n",
275 __func__, audio->ac->session,
276 audio->str_cfg.buffer_size,
277 audio->str_cfg.buffer_count);
278 break;
279 }
280 case AUDIO_GET_SESSION_ID: {
281 if (copy_to_user((void *) arg, &audio->ac->session,
282 sizeof(unsigned short))) {
283 rc = -EFAULT;
284 }
285 break;
286 }
287 case AUDIO_SET_BUF_CFG: {
288 struct msm_audio_buf_cfg cfg;
289 if (copy_from_user(&cfg, (void *)arg, sizeof(cfg))) {
290 rc = -EFAULT;
291 break;
292 }
293 if ((audio->feedback == NON_TUNNEL_MODE) &&
294 !cfg.meta_info_enable) {
295 rc = -EFAULT;
296 break;
297 }
298
299 /* Restrict the num of frames per buf to coincide with
300 * default buf size */
301 if (cfg.frames_per_buf > audio->max_frames_per_buf) {
302 rc = -EFAULT;
303 break;
304 }
305 audio->buf_cfg.meta_info_enable = cfg.meta_info_enable;
306 audio->buf_cfg.frames_per_buf = cfg.frames_per_buf;
Phani Kumar Uppalapati4e3c5db2013-01-08 10:54:14 -0800307 pr_debug("%s:session id %d: Set-buf-cfg: meta[%d] framesperbuf[%d]\n",
308 __func__,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700309 audio->ac->session, cfg.meta_info_enable,
310 cfg.frames_per_buf);
311 break;
312 }
313 case AUDIO_GET_BUF_CFG: {
Phani Kumar Uppalapati4e3c5db2013-01-08 10:54:14 -0800314 pr_debug("%s:session id %d: Get-buf-cfg: meta[%d] framesperbuf[%d]\n",
315 __func__,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700316 audio->ac->session, audio->buf_cfg.meta_info_enable,
317 audio->buf_cfg.frames_per_buf);
318
319 if (copy_to_user((void *)arg, &audio->buf_cfg,
320 sizeof(struct msm_audio_buf_cfg)))
321 rc = -EFAULT;
322 break;
323 }
324 case AUDIO_GET_CONFIG: {
325 if (copy_to_user((void *)arg, &audio->pcm_cfg,
326 sizeof(struct msm_audio_config)))
327 rc = -EFAULT;
328 break;
329
330 }
331 case AUDIO_SET_CONFIG: {
332 struct msm_audio_config cfg;
333 if (copy_from_user(&cfg, (void *)arg, sizeof(cfg))) {
334 rc = -EFAULT;
335 break;
336 }
337 if (audio->feedback != NON_TUNNEL_MODE) {
Phani Kumar Uppalapati4e3c5db2013-01-08 10:54:14 -0800338 pr_err("%s:session id %d: Not sufficient permission to change the record mode\n",
339 __func__,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700340 audio->ac->session);
341 rc = -EACCES;
342 break;
343 }
344 if ((cfg.buffer_count > PCM_BUF_COUNT) ||
345 (cfg.buffer_count == 1))
346 cfg.buffer_count = PCM_BUF_COUNT;
347
348 audio->pcm_cfg.buffer_count = cfg.buffer_count;
349 audio->pcm_cfg.buffer_size = cfg.buffer_size;
350 audio->pcm_cfg.channel_count = cfg.channel_count;
351 audio->pcm_cfg.sample_rate = cfg.sample_rate;
352 rc = q6asm_audio_client_buf_alloc(IN, audio->ac,
353 ALIGN_BUF_SIZE(audio->pcm_cfg.buffer_size),
354 audio->pcm_cfg.buffer_count);
355 if (rc < 0) {
356 pr_err("%s:session id %d: Buffer Alloc failed\n",
357 __func__, audio->ac->session);
358 rc = -ENOMEM;
359 break;
360 }
361 audio->buf_alloc |= BUF_ALLOC_IN;
362 rc = 0;
363 pr_debug("%s:session id %d: AUDIO_SET_CONFIG %d %d\n", __func__,
364 audio->ac->session, audio->pcm_cfg.buffer_count,
365 audio->pcm_cfg.buffer_size);
366 break;
367 }
368 default:
369 /* call codec specific ioctl */
370 rc = audio->enc_ioctl(file, cmd, arg);
371 }
372 mutex_unlock(&audio->lock);
373 return rc;
374}
375
376ssize_t audio_in_read(struct file *file,
377 char __user *buf,
378 size_t count, loff_t *pos)
379{
380 struct q6audio_in *audio = file->private_data;
381 const char __user *start = buf;
382 unsigned char *data;
383 uint32_t offset = 0;
384 uint32_t size = 0;
385 int rc = 0;
386 uint32_t idx;
387 struct meta_out_dsp meta;
388 uint32_t bytes_to_copy = 0;
389 uint32_t mfield_size = (audio->buf_cfg.meta_info_enable == 0) ? 0 :
390 (sizeof(unsigned char) +
391 (sizeof(struct meta_out_dsp)*(audio->buf_cfg.frames_per_buf)));
392
393 pr_debug("%s:session id %d: read - %d\n", __func__, audio->ac->session,
394 count);
395 if (!audio->enabled)
396 return -EFAULT;
397 mutex_lock(&audio->read_lock);
398 while (count > 0) {
399 rc = wait_event_interruptible(
400 audio->read_wait,
401 ((atomic_read(&audio->out_count) > 0) ||
402 (audio->stopped) ||
Phani Kumar Uppalapati4e3c5db2013-01-08 10:54:14 -0800403 audio->rflush || audio->eos_rsp ||
404 audio->event_abort));
405
406 if (audio->event_abort) {
407 rc = -EIO;
408 break;
409 }
410
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700411
412 if (rc < 0)
413 break;
414
415 if ((audio->stopped && !(atomic_read(&audio->out_count))) ||
416 audio->rflush) {
Phani Kumar Uppalapati4e3c5db2013-01-08 10:54:14 -0800417 pr_debug("%s:session id %d: driver in stop state or flush,No more buf to read",
418 __func__,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700419 audio->ac->session);
420 rc = 0;/* End of File */
421 break;
422 }
423 if (!(atomic_read(&audio->out_count)) &&
424 (audio->eos_rsp == 1) &&
425 (count >= (sizeof(unsigned char) +
426 sizeof(struct meta_out_dsp)))) {
427 unsigned char num_of_frames;
428 pr_info("%s:session id %d: eos %d at output\n",
429 __func__, audio->ac->session, audio->eos_rsp);
430 if (buf != start)
431 break;
432 num_of_frames = 0xFF;
433 if (copy_to_user(buf, &num_of_frames,
434 sizeof(unsigned char))) {
435 rc = -EFAULT;
436 break;
437 }
438 buf += sizeof(unsigned char);
439 meta.frame_size = 0xFFFF;
440 meta.encoded_pcm_samples = 0xFFFF;
441 meta.msw_ts = 0x00;
442 meta.lsw_ts = 0x00;
443 meta.nflags = AUD_EOS_SET;
444 audio->eos_rsp = 0;
445 if (copy_to_user(buf, &meta, sizeof(meta))) {
446 rc = -EFAULT;
447 break;
448 }
449 buf += sizeof(meta);
450 break;
451 }
452 data = (unsigned char *)q6asm_is_cpu_buf_avail(OUT, audio->ac,
453 &size, &idx);
454 if ((count >= (size + mfield_size)) && data) {
455 if (audio->buf_cfg.meta_info_enable) {
456 if (copy_to_user(buf,
457 &audio->out_frame_info[idx][0],
458 sizeof(unsigned char))) {
459 rc = -EFAULT;
460 break;
461 }
462 bytes_to_copy =
463 (size + audio->out_frame_info[idx][1]);
464 /* Number of frames information copied */
465 buf += sizeof(unsigned char);
466 count -= sizeof(unsigned char);
467 } else {
468 offset = audio->out_frame_info[idx][1];
469 bytes_to_copy = size;
470 }
471
472 pr_debug("%s:session id %d: offset=%d nr of frames= %d\n",
473 __func__, audio->ac->session,
474 audio->out_frame_info[idx][1],
475 audio->out_frame_info[idx][0]);
476
477 if (copy_to_user(buf, &data[offset], bytes_to_copy)) {
478 rc = -EFAULT;
479 break;
480 }
481 count -= bytes_to_copy;
482 buf += bytes_to_copy;
483 } else {
Phani Kumar Uppalapati4e3c5db2013-01-08 10:54:14 -0800484 pr_err("%s:session id %d: short read data[%p] bytesavail[%d]bytesrequest[%d]\n",
485 __func__,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700486 audio->ac->session,
487 data, size, count);
488 }
489 atomic_dec(&audio->out_count);
490 q6asm_read(audio->ac);
491 break;
492 }
493 mutex_unlock(&audio->read_lock);
494
495 pr_debug("%s:session id %d: read: %d bytes\n", __func__,
496 audio->ac->session, (buf-start));
497 if (buf > start)
498 return buf - start;
499 return rc;
500}
501
502static int extract_meta_info(char *buf, unsigned long *msw_ts,
503 unsigned long *lsw_ts, unsigned int *flags)
504{
505 struct meta_in *meta = (struct meta_in *)buf;
506 *msw_ts = meta->ntimestamp.highpart;
507 *lsw_ts = meta->ntimestamp.lowpart;
508 *flags = meta->nflags;
509 return 0;
510}
511
512ssize_t audio_in_write(struct file *file,
513 const char __user *buf,
514 size_t count, loff_t *pos)
515{
516 struct q6audio_in *audio = file->private_data;
517 const char __user *start = buf;
518 size_t xfer = 0;
519 char *cpy_ptr;
520 int rc = 0;
521 unsigned char *data;
522 uint32_t size = 0;
523 uint32_t idx = 0;
524 uint32_t nflags = 0;
525 unsigned long msw_ts = 0;
526 unsigned long lsw_ts = 0;
527 uint32_t mfield_size = (audio->buf_cfg.meta_info_enable == 0) ? 0 :
528 sizeof(struct meta_in);
529
530 pr_debug("%s:session id %d: to write[%d]\n", __func__,
531 audio->ac->session, count);
532 if (!audio->enabled)
533 return -EFAULT;
534 mutex_lock(&audio->write_lock);
535
536 while (count > 0) {
537 rc = wait_event_interruptible(audio->write_wait,
538 ((atomic_read(&audio->in_count) > 0) ||
539 (audio->stopped) ||
Phani Kumar Uppalapati4e3c5db2013-01-08 10:54:14 -0800540 (audio->wflush) || (audio->event_abort)));
541
542 if (audio->event_abort) {
543 rc = -EIO;
544 break;
545 }
546
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700547 if (rc < 0)
548 break;
549 if (audio->stopped || audio->wflush) {
550 pr_debug("%s: session id %d: stop or flush\n", __func__,
551 audio->ac->session);
552 rc = -EBUSY;
553 break;
554 }
Rajesha Kini72a8b612011-08-10 11:19:55 +0530555 /* if no PCM data, might have only eos buffer
556 such case do not hold cpu buffer */
557 if ((buf == start) && (count == mfield_size)) {
558 char eos_buf[sizeof(struct meta_in)];
559 /* Processing begining of user buffer */
560 if (copy_from_user(eos_buf, buf, mfield_size)) {
561 rc = -EFAULT;
562 break;
563 }
564 /* Check if EOS flag is set and buffer has
565 * contains just meta field
566 */
567 extract_meta_info(eos_buf, &msw_ts, &lsw_ts,
568 &nflags);
569 buf += mfield_size;
570 /* send the EOS and return */
Phani Kumar Uppalapati4e3c5db2013-01-08 10:54:14 -0800571 pr_debug("%s:session id %d: send EOS 0x%8x\n",
572 __func__,
Rajesha Kini72a8b612011-08-10 11:19:55 +0530573 audio->ac->session, nflags);
574 break;
575 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700576 data = (unsigned char *)q6asm_is_cpu_buf_avail(IN, audio->ac,
577 &size, &idx);
578 if (!data) {
579 pr_debug("%s:session id %d: No buf available\n",
580 __func__, audio->ac->session);
581 continue;
582 }
583 cpy_ptr = data;
584 if (audio->buf_cfg.meta_info_enable) {
585 if (buf == start) {
586 /* Processing beginning of user buffer */
587 if (copy_from_user(cpy_ptr, buf, mfield_size)) {
588 rc = -EFAULT;
589 break;
590 }
591 /* Check if EOS flag is set and buffer has
592 * contains just meta field
593 */
594 extract_meta_info(cpy_ptr, &msw_ts, &lsw_ts,
595 &nflags);
596 buf += mfield_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700597 count -= mfield_size;
598 } else {
Phani Kumar Uppalapati4e3c5db2013-01-08 10:54:14 -0800599 pr_debug("%s:session id %d: continuous buffer\n",
600 __func__, audio->ac->session);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700601 }
602 }
603 xfer = (count > (audio->pcm_cfg.buffer_size)) ?
604 (audio->pcm_cfg.buffer_size) : count;
605
606 if (copy_from_user(cpy_ptr, buf, xfer)) {
607 rc = -EFAULT;
608 break;
609 }
610 rc = q6asm_write(audio->ac, xfer, msw_ts, lsw_ts, 0x00);
611 if (rc < 0) {
612 rc = -EFAULT;
613 break;
614 }
615 atomic_dec(&audio->in_count);
616 count -= xfer;
617 buf += xfer;
618 }
619 mutex_unlock(&audio->write_lock);
Phani Kumar Uppalapati4e3c5db2013-01-08 10:54:14 -0800620 pr_debug("%s:session id %d: eos_condition 0x%8x buf[0x%x] start[0x%x]\n",
621 __func__, audio->ac->session,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700622 nflags, (int) buf, (int) start);
623 if (nflags & AUD_EOS_SET) {
624 rc = q6asm_cmd(audio->ac, CMD_EOS);
625 pr_info("%s:session id %d: eos %d at input\n", __func__,
626 audio->ac->session, audio->eos_rsp);
627 }
628 pr_debug("%s:session id %d: Written %d Avail Buf[%d]", __func__,
629 audio->ac->session, (buf - start - mfield_size),
630 atomic_read(&audio->in_count));
631 if (!rc) {
632 if (buf > start)
633 return buf - start;
634 }
635 return rc;
636}
637
638int audio_in_release(struct inode *inode, struct file *file)
639{
640 struct q6audio_in *audio = file->private_data;
641 pr_info("%s: session id %d\n", __func__, audio->ac->session);
642 mutex_lock(&audio->lock);
643 audio_in_disable(audio);
644 q6asm_audio_client_free(audio->ac);
645 mutex_unlock(&audio->lock);
646 kfree(audio->enc_cfg);
Harmandeep Singhd7d785c2011-11-15 11:46:26 -0800647 kfree(audio->codec_cfg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700648 kfree(audio);
649 return 0;
650}
651