blob: 155b0e14a80c6b91c8e0ce377448fd661716290e [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* arch/arm/mach-msm/audio_evrc.c
2 *
Manish Dewangana4f1df02012-02-08 17:06:54 +05303 * Copyright (c) 2008-2009, 2011-2012 Code Aurora Forum. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004 *
5 * This code also borrows from audio_aac.c, which is
6 * Copyright (C) 2008 Google, Inc.
7 * Copyright (C) 2008 HTC Corporation
8 *
9 * This software is licensed under the terms of the GNU General Public
10 * License version 2, as published by the Free Software Foundation, and
11 * may be copied, distributed, and modified under those terms.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 *
17 * See the GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, you can find it at http://www.fsf.org.
20 */
21
Santosh Mardi0be3b8e2011-07-06 10:00:21 +053022#include <asm/atomic.h>
23#include <asm/ioctls.h>
24
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070025#include <linux/module.h>
26#include <linux/fs.h>
27#include <linux/miscdevice.h>
28#include <linux/uaccess.h>
29#include <linux/kthread.h>
30#include <linux/wait.h>
31#include <linux/dma-mapping.h>
32#include <linux/debugfs.h>
33#include <linux/delay.h>
34#include <linux/list.h>
35#include <linux/earlysuspend.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070036#include <linux/slab.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070037#include <linux/msm_audio.h>
Santosh Mardi0be3b8e2011-07-06 10:00:21 +053038#include <linux/memory_alloc.h>
Mitchel Humpherys1da6ebe2012-09-06 10:15:56 -070039#include <linux/msm_ion.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070040
Santosh Mardi0be3b8e2011-07-06 10:00:21 +053041#include <mach/msm_adsp.h>
42#include <mach/iommu.h>
43#include <mach/iommu_domains.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070044#include <mach/qdsp5/qdsp5audppcmdi.h>
45#include <mach/qdsp5/qdsp5audppmsg.h>
Manish Dewanganfa8a6b62012-07-09 16:23:27 +053046#include <mach/qdsp5/qdsp5audpp.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070047#include <mach/qdsp5/qdsp5audplaycmdi.h>
48#include <mach/qdsp5/qdsp5audplaymsg.h>
49#include <mach/qdsp5/qdsp5rmtcmdi.h>
50#include <mach/debug_mm.h>
Santosh Mardi0be3b8e2011-07-06 10:00:21 +053051#include <mach/msm_memtypes.h>
52
53#include "audmgr.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070054
55/* Hold 30 packets of 24 bytes each and 14 bytes of meta in */
56#define BUFSZ 734
57#define DMASZ (BUFSZ * 2)
58
59#define AUDDEC_DEC_EVRC 12
60
61#define PCM_BUFSZ_MIN 1624 /* 100ms worth of data and
62 and 24 bytes of meta out */
63#define PCM_BUF_MAX_COUNT 5
64/* DSP only accepts 5 buffers at most
65 * but support 2 buffers currently
66 */
67#define EVRC_DECODED_FRSZ 320 /* EVRC 20ms 8KHz mono PCM size */
68
69#define ROUTING_MODE_FTRT 1
70#define ROUTING_MODE_RT 2
71/* Decoder status received from AUDPPTASK */
72#define AUDPP_DEC_STATUS_SLEEP 0
73#define AUDPP_DEC_STATUS_INIT 1
74#define AUDPP_DEC_STATUS_CFG 2
75#define AUDPP_DEC_STATUS_PLAY 3
76
77#define AUDEVRC_METAFIELD_MASK 0xFFFF0000
78#define AUDEVRC_EOS_FLG_OFFSET 0x0A /* Offset from beginning of buffer */
79#define AUDEVRC_EOS_FLG_MASK 0x01
80#define AUDEVRC_EOS_NONE 0x0 /* No EOS detected */
81#define AUDEVRC_EOS_SET 0x1 /* EOS set in meta field */
82
83#define AUDEVRC_EVENT_NUM 10 /* Default number of pre-allocated event packets */
84
85struct buffer {
86 void *data;
87 unsigned size;
88 unsigned used; /* Input usage actual DSP produced PCM size */
89 unsigned addr;
90 unsigned short mfield_sz; /*only useful for data has meta field */
91};
92
93#ifdef CONFIG_HAS_EARLYSUSPEND
94struct audevrc_suspend_ctl {
95 struct early_suspend node;
96 struct audio *audio;
97};
98#endif
99
100struct audevrc_event{
101 struct list_head list;
102 int event_type;
103 union msm_audio_event_payload payload;
104};
105
106struct audio {
107 struct buffer out[2];
108
109 spinlock_t dsp_lock;
110
111 uint8_t out_head;
112 uint8_t out_tail;
113 uint8_t out_needed; /* number of buffers the dsp is waiting for */
114
115 atomic_t out_bytes;
116
117 struct mutex lock;
118 struct mutex write_lock;
119 wait_queue_head_t write_wait;
120
121 /* Host PCM section */
122 struct buffer in[PCM_BUF_MAX_COUNT];
123 struct mutex read_lock;
124 wait_queue_head_t read_wait; /* Wait queue for read */
125 char *read_data; /* pointer to reader buffer */
126 int32_t read_phys; /* physical address of reader buffer */
127 uint8_t read_next; /* index to input buffers to be read next */
128 uint8_t fill_next; /* index to buffer that DSP should be filling */
129 uint8_t pcm_buf_count; /* number of pcm buffer allocated */
130 /* ---- End of Host PCM section */
131
132 struct msm_adsp_module *audplay;
133 struct audmgr audmgr;
134
135 /* data allocated for various buffers */
136 char *data;
137 int32_t phys; /* physical address of write buffer */
Laura Abbott35111d32012-04-27 18:41:48 -0700138 void *map_v_read;
139 void *map_v_write;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700140
141 int mfield; /* meta field embedded in data */
142 int rflush; /* Read flush */
143 int wflush; /* Write flush */
144 uint8_t opened:1;
145 uint8_t enabled:1;
146 uint8_t running:1;
147 uint8_t stopped:1; /* set when stopped, cleared on flush */
148 uint8_t pcm_feedback:1;
149 uint8_t buf_refresh:1;
150 int teos; /* valid only if tunnel mode & no data left for decoder */
151 enum msm_aud_decoder_state dec_state; /* Represents decoder state */
152 int rmt_resource_released;
153
154 const char *module_name;
155 unsigned queue_id;
156 uint16_t dec_id;
157 uint32_t read_ptr_offset;
158
159#ifdef CONFIG_HAS_EARLYSUSPEND
160 struct audevrc_suspend_ctl suspend_ctl;
161#endif
162
163#ifdef CONFIG_DEBUG_FS
164 struct dentry *dentry;
165#endif
166
167 wait_queue_head_t wait;
168 struct list_head free_event_queue;
169 struct list_head event_queue;
170 wait_queue_head_t event_wait;
171 spinlock_t event_queue_lock;
172 struct mutex get_event_lock;
173 int event_abort;
174
175 int eq_enable;
176 int eq_needs_commit;
177 audpp_cmd_cfg_object_params_eqalizer eq;
178 audpp_cmd_cfg_object_params_volume vol_pan;
Sidipotu Ashok172c98b2012-06-26 17:58:29 +0530179 struct ion_client *client;
180 struct ion_handle *input_buff_handle;
181 struct ion_handle *output_buff_handle;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700182};
183
184static int auddec_dsp_config(struct audio *audio, int enable);
185static void audpp_cmd_cfg_adec_params(struct audio *audio);
186static void audpp_cmd_cfg_routing_mode(struct audio *audio);
187static void audevrc_send_data(struct audio *audio, unsigned needed);
188static void audevrc_dsp_event(void *private, unsigned id, uint16_t *msg);
189static void audevrc_config_hostpcm(struct audio *audio);
190static void audevrc_buffer_refresh(struct audio *audio);
191#ifdef CONFIG_HAS_EARLYSUSPEND
192static void audevrc_post_event(struct audio *audio, int type,
193 union msm_audio_event_payload payload);
194#endif
195
196static int rmt_put_resource(struct audio *audio)
197{
198 struct aud_codec_config_cmd cmd;
199 unsigned short client_idx;
200
201 cmd.cmd_id = RM_CMD_AUD_CODEC_CFG;
202 cmd.client_id = RM_AUD_CLIENT_ID;
203 cmd.task_id = audio->dec_id;
204 cmd.enable = RMT_DISABLE;
205 cmd.dec_type = AUDDEC_DEC_EVRC;
206 client_idx = ((cmd.client_id << 8) | cmd.task_id);
207
208 return put_adsp_resource(client_idx, &cmd, sizeof(cmd));
209}
210
211static int rmt_get_resource(struct audio *audio)
212{
213 struct aud_codec_config_cmd cmd;
214 unsigned short client_idx;
215
216 cmd.cmd_id = RM_CMD_AUD_CODEC_CFG;
217 cmd.client_id = RM_AUD_CLIENT_ID;
218 cmd.task_id = audio->dec_id;
219 cmd.enable = RMT_ENABLE;
220 cmd.dec_type = AUDDEC_DEC_EVRC;
221 client_idx = ((cmd.client_id << 8) | cmd.task_id);
222
223 return get_adsp_resource(client_idx, &cmd, sizeof(cmd));
224}
225
226/* must be called with audio->lock held */
227static int audevrc_enable(struct audio *audio)
228{
229 struct audmgr_config cfg;
230 int rc;
231
232 if (audio->enabled)
233 return 0;
234
235 if (audio->rmt_resource_released == 1) {
236 audio->rmt_resource_released = 0;
237 rc = rmt_get_resource(audio);
238 if (rc) {
239 MM_ERR("ADSP resources are not available for EVRC \
240 session 0x%08x on decoder: %d\n Ignoring \
241 error and going ahead with the playback\n",
242 (int)audio, audio->dec_id);
243 }
244 }
245
246 audio->dec_state = MSM_AUD_DECODER_STATE_NONE;
247 audio->out_tail = 0;
248 audio->out_needed = 0;
249
250 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK) {
251 cfg.tx_rate = RPC_AUD_DEF_SAMPLE_RATE_NONE;
252 cfg.rx_rate = RPC_AUD_DEF_SAMPLE_RATE_48000;
253 cfg.def_method = RPC_AUD_DEF_METHOD_PLAYBACK;
254 cfg.codec = RPC_AUD_DEF_CODEC_EVRC;
255 cfg.snd_method = RPC_SND_METHOD_MIDI;
256
257 rc = audmgr_enable(&audio->audmgr, &cfg);
258 if (rc < 0)
259 return rc;
260 }
261
262 if (msm_adsp_enable(audio->audplay)) {
263 MM_ERR("msm_adsp_enable(audplay) failed\n");
264 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK)
265 audmgr_disable(&audio->audmgr);
266 return -ENODEV;
267 }
268
269 if (audpp_enable(audio->dec_id, audevrc_dsp_event, audio)) {
270 MM_ERR("audpp_enable() failed\n");
271 msm_adsp_disable(audio->audplay);
272 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK)
273 audmgr_disable(&audio->audmgr);
274 return -ENODEV;
275 }
276 audio->enabled = 1;
277 return 0;
278}
279
280/* must be called with audio->lock held */
281static int audevrc_disable(struct audio *audio)
282{
283 int rc = 0;
284 if (audio->enabled) {
285 audio->enabled = 0;
286 audio->dec_state = MSM_AUD_DECODER_STATE_NONE;
287 auddec_dsp_config(audio, 0);
288 rc = wait_event_interruptible_timeout(audio->wait,
289 audio->dec_state != MSM_AUD_DECODER_STATE_NONE,
290 msecs_to_jiffies(MSM_AUD_DECODER_WAIT_MS));
291 if (rc == 0)
292 rc = -ETIMEDOUT;
293 else if (audio->dec_state != MSM_AUD_DECODER_STATE_CLOSE)
294 rc = -EFAULT;
295 else
296 rc = 0;
Manish Dewangan89a9f232012-02-09 17:14:40 +0530297 audio->stopped = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700298 wake_up(&audio->write_wait);
299 wake_up(&audio->read_wait);
300 msm_adsp_disable(audio->audplay);
301 audpp_disable(audio->dec_id, audio);
302 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK)
303 audmgr_disable(&audio->audmgr);
304 audio->out_needed = 0;
305 rmt_put_resource(audio);
306 audio->rmt_resource_released = 1;
307 }
308 return rc;
309}
310
311/* ------------------- dsp --------------------- */
312
313static void audevrc_update_pcm_buf_entry(struct audio *audio,
314 uint32_t *payload)
315{
316 uint8_t index;
317 unsigned long flags;
318
319 if (audio->rflush)
320 return;
321
322 spin_lock_irqsave(&audio->dsp_lock, flags);
323 for (index = 0; index < payload[1]; index++) {
324 if (audio->in[audio->fill_next].addr
325 == payload[2 + index * 2]) {
326 MM_DBG("in[%d] ready\n", audio->fill_next);
327 audio->in[audio->fill_next].used =
328 payload[3 + index * 2];
329 if ((++audio->fill_next) == audio->pcm_buf_count)
330 audio->fill_next = 0;
331
332 } else {
333 MM_ERR("expected=%x ret=%x\n",
334 audio->in[audio->fill_next].addr,
335 payload[1 + index * 2]);
336 break;
337 }
338 }
339 if (audio->in[audio->fill_next].used == 0) {
340 audevrc_buffer_refresh(audio);
341 } else {
342 MM_DBG("read cannot keep up\n");
343 audio->buf_refresh = 1;
344 }
345 wake_up(&audio->read_wait);
346 spin_unlock_irqrestore(&audio->dsp_lock, flags);
347}
348
349static void audplay_dsp_event(void *data, unsigned id, size_t len,
350 void (*getevent) (void *ptr, size_t len))
351{
352 struct audio *audio = data;
353 uint32_t msg[28];
354 getevent(msg, sizeof(msg));
355
356 MM_DBG("msg_id=%x\n", id);
357 switch (id) {
358 case AUDPLAY_MSG_DEC_NEEDS_DATA:
359 audevrc_send_data(audio, 1);
360 break;
361 case AUDPLAY_MSG_BUFFER_UPDATE:
362 MM_DBG("\n"); /* Macro prints the file name and function */
363 audevrc_update_pcm_buf_entry(audio, msg);
364 break;
365 case ADSP_MESSAGE_ID:
366 MM_DBG("Received ADSP event: module enable(audplaytask)\n");
367 break;
368 default:
369 MM_ERR("unexpected message from decoder \n");
370 }
371}
372
373static void audevrc_dsp_event(void *private, unsigned id, uint16_t *msg)
374{
375 struct audio *audio = private;
376
377 switch (id) {
378 case AUDPP_MSG_STATUS_MSG:{
379 unsigned status = msg[1];
380
381 switch (status) {
382 case AUDPP_DEC_STATUS_SLEEP: {
383 uint16_t reason = msg[2];
384 MM_DBG("decoder status:sleep reason = \
385 0x%04x\n", reason);
386 if ((reason == AUDPP_MSG_REASON_MEM)
387 || (reason ==
388 AUDPP_MSG_REASON_NODECODER)) {
389 audio->dec_state =
390 MSM_AUD_DECODER_STATE_FAILURE;
391 wake_up(&audio->wait);
392 } else if (reason == AUDPP_MSG_REASON_NONE) {
393 /* decoder is in disable state */
394 audio->dec_state =
395 MSM_AUD_DECODER_STATE_CLOSE;
396 wake_up(&audio->wait);
397 }
398 break;
399 }
400 case AUDPP_DEC_STATUS_INIT:
401 MM_DBG("decoder status: init \n");
402 if (audio->pcm_feedback)
403 audpp_cmd_cfg_routing_mode(audio);
404 else
405 audpp_cmd_cfg_adec_params(audio);
406 break;
407
408 case AUDPP_DEC_STATUS_CFG:
409 MM_DBG("decoder status: cfg \n");
410 break;
411 case AUDPP_DEC_STATUS_PLAY:
412 MM_DBG("decoder status: play \n");
413 if (audio->pcm_feedback) {
414 audevrc_config_hostpcm(audio);
415 audevrc_buffer_refresh(audio);
416 }
417 audio->dec_state =
418 MSM_AUD_DECODER_STATE_SUCCESS;
419 wake_up(&audio->wait);
420 break;
421 default:
422 MM_ERR("unknown decoder status \n");
423 }
424 break;
425 }
426 case AUDPP_MSG_CFG_MSG:
427 if (msg[0] == AUDPP_MSG_ENA_ENA) {
428 MM_DBG("CFG_MSG ENABLE\n");
429 auddec_dsp_config(audio, 1);
430 audio->out_needed = 0;
431 audio->running = 1;
432 audpp_dsp_set_vol_pan(audio->dec_id, &audio->vol_pan);
433 audpp_dsp_set_eq(audio->dec_id, audio->eq_enable,
434 &audio->eq);
435 audpp_avsync(audio->dec_id, 22050);
436 } else if (msg[0] == AUDPP_MSG_ENA_DIS) {
437 MM_DBG("CFG_MSG DISABLE\n");
438 audpp_avsync(audio->dec_id, 0);
439 audio->running = 0;
440 } else {
441 MM_DBG("CFG_MSG %d?\n", msg[0]);
442 }
443 break;
444 case AUDPP_MSG_ROUTING_ACK:
445 MM_DBG("ROUTING_ACK\n");
446 audpp_cmd_cfg_adec_params(audio);
447 break;
448 case AUDPP_MSG_FLUSH_ACK:
449 MM_DBG("FLUSH_ACK\n");
450 audio->wflush = 0;
451 audio->rflush = 0;
452 wake_up(&audio->write_wait);
453 if (audio->pcm_feedback)
454 audevrc_buffer_refresh(audio);
455 break;
456 case AUDPP_MSG_PCMDMAMISSED:
457 MM_DBG("PCMDMAMISSED\n");
458 audio->teos = 1;
459 wake_up(&audio->write_wait);
460 break;
461 default:
462 MM_ERR("UNKNOWN (%d)\n", id);
463 }
464
465}
466
467struct msm_adsp_ops audplay_adsp_ops_evrc = {
468 .event = audplay_dsp_event,
469};
470
471#define audplay_send_queue0(audio, cmd, len) \
472 msm_adsp_write(audio->audplay, audio->queue_id, \
473 cmd, len)
474
475static int auddec_dsp_config(struct audio *audio, int enable)
476{
477 u16 cfg_dec_cmd[AUDPP_CMD_CFG_DEC_TYPE_LEN / sizeof(unsigned short)];
478
479 memset(cfg_dec_cmd, 0, sizeof(cfg_dec_cmd));
480
481 cfg_dec_cmd[0] = AUDPP_CMD_CFG_DEC_TYPE;
482 if (enable)
483 cfg_dec_cmd[1 + audio->dec_id] = AUDPP_CMD_UPDATDE_CFG_DEC |
484 AUDPP_CMD_ENA_DEC_V | AUDDEC_DEC_EVRC;
485 else
486 cfg_dec_cmd[1 + audio->dec_id] = AUDPP_CMD_UPDATDE_CFG_DEC |
487 AUDPP_CMD_DIS_DEC_V;
488
489 return audpp_send_queue1(&cfg_dec_cmd, sizeof(cfg_dec_cmd));
490}
491
492static void audpp_cmd_cfg_adec_params(struct audio *audio)
493{
494 struct audpp_cmd_cfg_adec_params_evrc cmd;
495
496 memset(&cmd, 0, sizeof(cmd));
497 cmd.common.cmd_id = AUDPP_CMD_CFG_ADEC_PARAMS;
498 cmd.common.length = sizeof(cmd);
499 cmd.common.dec_id = audio->dec_id;
500 cmd.common.input_sampling_frequency = 8000;
501 cmd.stereo_cfg = AUDPP_CMD_PCM_INTF_MONO_V;
502
503 audpp_send_queue2(&cmd, sizeof(cmd));
504}
505
506static void audpp_cmd_cfg_routing_mode(struct audio *audio)
507{
508 struct audpp_cmd_routing_mode cmd;
509 MM_DBG("\n"); /* Macro prints the file name and function */
510 memset(&cmd, 0, sizeof(cmd));
511 cmd.cmd_id = AUDPP_CMD_ROUTING_MODE;
512 cmd.object_number = audio->dec_id;
513 if (audio->pcm_feedback)
514 cmd.routing_mode = ROUTING_MODE_FTRT;
515 else
516 cmd.routing_mode = ROUTING_MODE_RT;
517
518 audpp_send_queue1(&cmd, sizeof(cmd));
519}
520
521static int audplay_dsp_send_data_avail(struct audio *audio,
522 unsigned idx, unsigned len)
523{
524 struct audplay_cmd_bitstream_data_avail_nt2 cmd;
525
526 cmd.cmd_id = AUDPLAY_CMD_BITSTREAM_DATA_AVAIL_NT2;
527 if (audio->mfield)
528 cmd.decoder_id = AUDEVRC_METAFIELD_MASK |
529 (audio->out[idx].mfield_sz >> 1);
530 else
531 cmd.decoder_id = audio->dec_id;
532 cmd.buf_ptr = audio->out[idx].addr;
533 cmd.buf_size = len / 2;
534 cmd.partition_number = 0;
535 /* complete writes to the input buffer */
536 wmb();
537 return audplay_send_queue0(audio, &cmd, sizeof(cmd));
538}
539
540static void audevrc_buffer_refresh(struct audio *audio)
541{
542 struct audplay_cmd_buffer_refresh refresh_cmd;
543
544 refresh_cmd.cmd_id = AUDPLAY_CMD_BUFFER_REFRESH;
545 refresh_cmd.num_buffers = 1;
546 refresh_cmd.buf0_address = audio->in[audio->fill_next].addr;
547 refresh_cmd.buf0_length = audio->in[audio->fill_next].size;
548
549 refresh_cmd.buf_read_count = 0;
550 MM_DBG("buf0_addr=%x buf0_len=%d\n", refresh_cmd.buf0_address,
551 refresh_cmd.buf0_length);
552 audplay_send_queue0(audio, &refresh_cmd, sizeof(refresh_cmd));
553}
554
555static void audevrc_config_hostpcm(struct audio *audio)
556{
557 struct audplay_cmd_hpcm_buf_cfg cfg_cmd;
558
559 MM_DBG("\n"); /* Macro prints the file name and function */
560 cfg_cmd.cmd_id = AUDPLAY_CMD_HPCM_BUF_CFG;
561 cfg_cmd.max_buffers = 1;
562 cfg_cmd.byte_swap = 0;
563 cfg_cmd.hostpcm_config = (0x8000) | (0x4000);
564 cfg_cmd.feedback_frequency = 1;
565 cfg_cmd.partition_number = 0;
566 audplay_send_queue0(audio, &cfg_cmd, sizeof(cfg_cmd));
567
568}
569
570static void audevrc_send_data(struct audio *audio, unsigned needed)
571{
572 struct buffer *frame;
573 unsigned long flags;
574
575 spin_lock_irqsave(&audio->dsp_lock, flags);
576 if (!audio->running)
577 goto done;
578
579 if (needed && !audio->wflush) {
580 /* We were called from the callback because the DSP
581 * requested more data. Note that the DSP does want
582 * more data, and if a buffer was in-flight, mark it
583 * as available (since the DSP must now be done with
584 * it).
585 */
586 audio->out_needed = 1;
587 frame = audio->out + audio->out_tail;
588 if (frame->used == 0xffffffff) {
589 MM_DBG("frame %d free\n", audio->out_tail);
590 frame->used = 0;
591 audio->out_tail ^= 1;
592 wake_up(&audio->write_wait);
593 }
594 }
595
596 if (audio->out_needed) {
597 /* If the DSP currently wants data and we have a
598 * buffer available, we will send it and reset
599 * the needed flag. We'll mark the buffer as in-flight
600 * so that it won't be recycled until the next buffer
601 * is requested
602 */
603
604 frame = audio->out + audio->out_tail;
605 if (frame->used) {
606 BUG_ON(frame->used == 0xffffffff);
607 MM_DBG("frame %d busy\n", audio->out_tail);
608 audplay_dsp_send_data_avail(audio, audio->out_tail,
609 frame->used);
610 frame->used = 0xffffffff;
611 audio->out_needed = 0;
612 }
613 }
614done:
615 spin_unlock_irqrestore(&audio->dsp_lock, flags);
616}
617
618/* ------------------- device --------------------- */
619
620static void audevrc_flush(struct audio *audio)
621{
Manish Dewangana4f1df02012-02-08 17:06:54 +0530622 unsigned long flags;
623
624 spin_lock_irqsave(&audio->dsp_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700625 audio->out[0].used = 0;
626 audio->out[1].used = 0;
627 audio->out_head = 0;
628 audio->out_tail = 0;
629 audio->out_needed = 0;
Manish Dewangana4f1df02012-02-08 17:06:54 +0530630 spin_unlock_irqrestore(&audio->dsp_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700631 atomic_set(&audio->out_bytes, 0);
632}
633
634static void audevrc_flush_pcm_buf(struct audio *audio)
635{
636 uint8_t index;
Manish Dewangana4f1df02012-02-08 17:06:54 +0530637 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700638
Manish Dewangana4f1df02012-02-08 17:06:54 +0530639 spin_lock_irqsave(&audio->dsp_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700640 for (index = 0; index < PCM_BUF_MAX_COUNT; index++)
641 audio->in[index].used = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700642 audio->buf_refresh = 0;
643 audio->read_next = 0;
644 audio->fill_next = 0;
Manish Dewangana4f1df02012-02-08 17:06:54 +0530645 spin_unlock_irqrestore(&audio->dsp_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700646}
647
648static void audevrc_ioport_reset(struct audio *audio)
649{
650 /* Make sure read/write thread are free from
651 * sleep and knowing that system is not able
652 * to process io request at the moment
653 */
654 wake_up(&audio->write_wait);
655 mutex_lock(&audio->write_lock);
656 audevrc_flush(audio);
657 mutex_unlock(&audio->write_lock);
658 wake_up(&audio->read_wait);
659 mutex_lock(&audio->read_lock);
660 audevrc_flush_pcm_buf(audio);
661 mutex_unlock(&audio->read_lock);
662}
663
664static int audevrc_events_pending(struct audio *audio)
665{
666 unsigned long flags;
667 int empty;
668
669 spin_lock_irqsave(&audio->event_queue_lock, flags);
670 empty = !list_empty(&audio->event_queue);
671 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
672 return empty || audio->event_abort;
673}
674
675static void audevrc_reset_event_queue(struct audio *audio)
676{
677 unsigned long flags;
678 struct audevrc_event *drv_evt;
679 struct list_head *ptr, *next;
680
681 spin_lock_irqsave(&audio->event_queue_lock, flags);
682 list_for_each_safe(ptr, next, &audio->event_queue) {
683 drv_evt = list_first_entry(&audio->event_queue,
684 struct audevrc_event, list);
685 list_del(&drv_evt->list);
686 kfree(drv_evt);
687 }
688 list_for_each_safe(ptr, next, &audio->free_event_queue) {
689 drv_evt = list_first_entry(&audio->free_event_queue,
690 struct audevrc_event, list);
691 list_del(&drv_evt->list);
692 kfree(drv_evt);
693 }
694 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
695
696 return;
697}
698
699
700static long audevrc_process_event_req(struct audio *audio, void __user *arg)
701{
702 long rc;
703 struct msm_audio_event usr_evt;
704 struct audevrc_event *drv_evt = NULL;
705 int timeout;
706 unsigned long flags;
707
708 if (copy_from_user(&usr_evt, arg, sizeof(struct msm_audio_event)))
709 return -EFAULT;
710
711 timeout = (int) usr_evt.timeout_ms;
712
713 if (timeout > 0) {
714 rc = wait_event_interruptible_timeout(
715 audio->event_wait, audevrc_events_pending(audio),
716 msecs_to_jiffies(timeout));
717 if (rc == 0)
718 return -ETIMEDOUT;
719 } else {
720 rc = wait_event_interruptible(
721 audio->event_wait, audevrc_events_pending(audio));
722 }
723
724 if (rc < 0)
725 return rc;
726
727 if (audio->event_abort) {
728 audio->event_abort = 0;
729 return -ENODEV;
730 }
731
732 rc = 0;
733
734 spin_lock_irqsave(&audio->event_queue_lock, flags);
735 if (!list_empty(&audio->event_queue)) {
736 drv_evt = list_first_entry(&audio->event_queue,
737 struct audevrc_event, list);
738 list_del(&drv_evt->list);
739 }
740 if (drv_evt) {
741 usr_evt.event_type = drv_evt->event_type;
742 usr_evt.event_payload = drv_evt->payload;
743 list_add_tail(&drv_evt->list, &audio->free_event_queue);
744 } else
745 rc = -1;
746 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
747
748 if (!rc && copy_to_user(arg, &usr_evt, sizeof(usr_evt)))
749 rc = -EFAULT;
750
751 return rc;
752}
753
754static int audio_enable_eq(struct audio *audio, int enable)
755{
756 if (audio->eq_enable == enable && !audio->eq_needs_commit)
757 return 0;
758
759 audio->eq_enable = enable;
760
761 if (audio->running) {
762 audpp_dsp_set_eq(audio->dec_id, enable, &audio->eq);
763 audio->eq_needs_commit = 0;
764 }
765 return 0;
766}
767
768static long audevrc_ioctl(struct file *file, unsigned int cmd,
769 unsigned long arg)
770{
771 struct audio *audio = file->private_data;
772 int rc = -EINVAL;
773 unsigned long flags = 0;
774 uint16_t enable_mask;
775 int enable;
776 int prev_state;
Sidipotu Ashok172c98b2012-06-26 17:58:29 +0530777 unsigned long ionflag = 0;
778 ion_phys_addr_t addr = 0;
779 struct ion_handle *handle = NULL;
780 int len = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700781
782 MM_DBG("cmd = %d\n", cmd);
783
784 if (cmd == AUDIO_GET_STATS) {
785 struct msm_audio_stats stats;
786 stats.byte_count = audpp_avsync_byte_count(audio->dec_id);
787 stats.sample_count = audpp_avsync_sample_count(audio->dec_id);
788 if (copy_to_user((void *)arg, &stats, sizeof(stats)))
789 return -EFAULT;
790 return 0;
791 }
792
793 switch (cmd) {
794 case AUDIO_ENABLE_AUDPP:
795 if (copy_from_user(&enable_mask, (void *) arg,
796 sizeof(enable_mask))) {
797 rc = -EFAULT;
798 break;
799 }
800
801 spin_lock_irqsave(&audio->dsp_lock, flags);
802 enable = (enable_mask & EQ_ENABLE) ? 1 : 0;
803 audio_enable_eq(audio, enable);
804 spin_unlock_irqrestore(&audio->dsp_lock, flags);
805 rc = 0;
806 break;
807 case AUDIO_SET_VOLUME:
808 spin_lock_irqsave(&audio->dsp_lock, flags);
809 audio->vol_pan.volume = arg;
810 if (audio->running)
811 audpp_dsp_set_vol_pan(audio->dec_id, &audio->vol_pan);
812 spin_unlock_irqrestore(&audio->dsp_lock, flags);
813 rc = 0;
814 break;
815
816 case AUDIO_SET_PAN:
817 spin_lock_irqsave(&audio->dsp_lock, flags);
818 audio->vol_pan.pan = arg;
819 if (audio->running)
820 audpp_dsp_set_vol_pan(audio->dec_id, &audio->vol_pan);
821 spin_unlock_irqrestore(&audio->dsp_lock, flags);
822 rc = 0;
823 break;
824
825 case AUDIO_SET_EQ:
826 prev_state = audio->eq_enable;
827 audio->eq_enable = 0;
828 if (copy_from_user(&audio->eq.num_bands, (void *) arg,
829 sizeof(audio->eq) -
830 (AUDPP_CMD_CFG_OBJECT_PARAMS_COMMON_LEN + 2))) {
831 rc = -EFAULT;
832 break;
833 }
834 audio->eq_enable = prev_state;
835 audio->eq_needs_commit = 1;
836 rc = 0;
837 break;
838 }
839
840 if (-EINVAL != rc)
841 return rc;
842
843 if (cmd == AUDIO_GET_EVENT) {
844 MM_DBG("AUDIO_GET_EVENT\n");
845 if (mutex_trylock(&audio->get_event_lock)) {
846 rc = audevrc_process_event_req(audio,
847 (void __user *) arg);
848 mutex_unlock(&audio->get_event_lock);
849 } else
850 rc = -EBUSY;
851 return rc;
852 }
853
854 if (cmd == AUDIO_ABORT_GET_EVENT) {
855 audio->event_abort = 1;
856 wake_up(&audio->event_wait);
857 return 0;
858 }
859
860 mutex_lock(&audio->lock);
861 switch (cmd) {
862 case AUDIO_START:
863 MM_DBG("AUDIO_START\n");
864 rc = audevrc_enable(audio);
865 if (!rc) {
866 rc = wait_event_interruptible_timeout(audio->wait,
867 audio->dec_state != MSM_AUD_DECODER_STATE_NONE,
868 msecs_to_jiffies(MSM_AUD_DECODER_WAIT_MS));
869 MM_INFO("dec_state %d rc = %d\n", audio->dec_state, rc);
870
871 if (audio->dec_state != MSM_AUD_DECODER_STATE_SUCCESS)
872 rc = -ENODEV;
873 else
874 rc = 0;
875 }
876 break;
877 case AUDIO_STOP:
878 MM_DBG("AUDIO_STOP\n");
879 rc = audevrc_disable(audio);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700880 audevrc_ioport_reset(audio);
881 audio->stopped = 0;
882 break;
883 case AUDIO_FLUSH:
884 MM_DBG("AUDIO_FLUSH\n");
885 audio->rflush = 1;
886 audio->wflush = 1;
887 audevrc_ioport_reset(audio);
888 if (audio->running) {
889 audpp_flush(audio->dec_id);
890 rc = wait_event_interruptible(audio->write_wait,
891 !audio->wflush);
892 if (rc < 0) {
893 MM_ERR("AUDIO_FLUSH interrupted\n");
894 rc = -EINTR;
895 }
896 } else {
897 audio->rflush = 0;
898 audio->wflush = 0;
899 }
900 break;
901 case AUDIO_SET_CONFIG:{
902 struct msm_audio_config config;
903 if (copy_from_user
904 (&config, (void *)arg, sizeof(config))) {
905 rc = -EFAULT;
906 break;
907 }
908 audio->mfield = config.meta_field;
909 rc = 0;
910 MM_DBG("AUDIO_SET_CONFIG applicable only \
911 for meta field configuration\n");
912 break;
913 }
914 case AUDIO_GET_CONFIG:{
915 struct msm_audio_config config;
916 config.buffer_size = BUFSZ;
917 config.buffer_count = 2;
918 config.sample_rate = 8000;
919 config.channel_count = 1;
920 config.meta_field = 0;
921 config.unused[0] = 0;
922 config.unused[1] = 0;
923 config.unused[2] = 0;
924 if (copy_to_user((void *)arg, &config, sizeof(config)))
925 rc = -EFAULT;
926 else
927 rc = 0;
928 break;
929 }
930 case AUDIO_GET_PCM_CONFIG:{
931 struct msm_audio_pcm_config config;
932 config.pcm_feedback = audio->pcm_feedback;
933 config.buffer_count = PCM_BUF_MAX_COUNT;
934 config.buffer_size = PCM_BUFSZ_MIN;
935 if (copy_to_user((void *)arg, &config, sizeof(config)))
936 rc = -EFAULT;
937 else
938 rc = 0;
939 break;
940 }
941 case AUDIO_SET_PCM_CONFIG:{
942 struct msm_audio_pcm_config config;
943 if (copy_from_user
944 (&config, (void *)arg, sizeof(config))) {
945 rc = -EFAULT;
946 break;
947 }
948 if (config.pcm_feedback != audio->pcm_feedback) {
949 MM_ERR("Not sufficient permission to"
950 "change the playback mode\n");
951 rc = -EACCES;
952 break;
953 }
954 if ((config.buffer_count > PCM_BUF_MAX_COUNT) ||
955 (config.buffer_count == 1))
956 config.buffer_count = PCM_BUF_MAX_COUNT;
957
958 if (config.buffer_size < PCM_BUFSZ_MIN)
959 config.buffer_size = PCM_BUFSZ_MIN;
960
961 /* Check if pcm feedback is required */
962 if ((config.pcm_feedback) && (!audio->read_data)) {
963 MM_DBG("allocate PCM buf %d\n",
964 config.buffer_count *
965 config.buffer_size);
Sidipotu Ashok172c98b2012-06-26 17:58:29 +0530966 handle = ion_alloc(audio->client,
967 (config.buffer_size *
968 config.buffer_count),
Hanumant Singh7d72bad2012-08-29 18:39:44 -0700969 SZ_4K, ION_HEAP(ION_AUDIO_HEAP_ID), 0);
Sidipotu Ashok172c98b2012-06-26 17:58:29 +0530970 if (IS_ERR_OR_NULL(handle)) {
971 MM_ERR("Unable to alloc I/P buffs\n");
972 audio->input_buff_handle = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700973 rc = -ENOMEM;
974 break;
975 }
Sidipotu Ashok172c98b2012-06-26 17:58:29 +0530976
977 audio->input_buff_handle = handle;
978
979 rc = ion_phys(audio->client ,
980 handle, &addr, &len);
981 if (rc) {
982 MM_ERR("Invalid phy: %x sz: %x\n",
983 (unsigned int) addr,
984 (unsigned int) len);
985 ion_free(audio->client, handle);
986 audio->input_buff_handle = NULL;
987 rc = -ENOMEM;
988 break;
989 } else {
990 MM_INFO("Got valid phy: %x sz: %x\n",
991 (unsigned int) audio->read_phys,
992 (unsigned int) len);
993 }
994 audio->read_phys = (int32_t)addr;
995
996 rc = ion_handle_get_flags(audio->client,
997 handle, &ionflag);
998 if (rc) {
999 MM_ERR("could not get flags\n");
1000 ion_free(audio->client, handle);
1001 audio->input_buff_handle = NULL;
1002 rc = -ENOMEM;
1003 break;
1004 }
1005 audio->map_v_read = ion_map_kernel(
Mitchel Humpherys911b4b72012-09-12 14:42:50 -07001006 audio->client, handle);
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05301007 if (IS_ERR(audio->map_v_read)) {
1008 MM_ERR("failed to map mem"
1009 " for read buf\n");
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301010 ion_free(audio->client, handle);
1011 audio->input_buff_handle = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001012 rc = -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001013 } else {
1014 uint8_t index;
1015 uint32_t offset = 0;
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05301016 audio->read_data =
Laura Abbott35111d32012-04-27 18:41:48 -07001017 audio->map_v_read;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001018 audio->buf_refresh = 0;
1019 audio->pcm_buf_count =
1020 config.buffer_count;
1021 audio->read_next = 0;
1022 audio->fill_next = 0;
1023
1024 for (index = 0;
1025 index < config.buffer_count;
1026 index++) {
1027 audio->in[index].data =
1028 audio->read_data + offset;
1029 audio->in[index].addr =
1030 audio->read_phys + offset;
1031 audio->in[index].size =
1032 config.buffer_size;
1033 audio->in[index].used = 0;
1034 offset += config.buffer_size;
1035 }
1036 MM_DBG("read buf: phy addr \
1037 0x%08x kernel addr 0x%08x\n",
1038 audio->read_phys,
1039 (int)audio->read_data);
1040 rc = 0;
1041 }
1042 } else {
1043 rc = 0;
1044 }
1045 break;
1046 }
1047 case AUDIO_PAUSE:
1048 MM_DBG("AUDIO_PAUSE %ld\n", arg);
1049 rc = audpp_pause(audio->dec_id, (int) arg);
1050 break;
1051 default:
1052 rc = -EINVAL;
1053 }
1054 mutex_unlock(&audio->lock);
1055 return rc;
1056}
1057
1058/* Only useful in tunnel-mode */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001059static int audevrc_fsync(struct file *file, loff_t a, loff_t b, int datasync)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001060{
1061 struct audio *audio = file->private_data;
1062 int rc = 0;
1063
1064 MM_DBG("\n"); /* Macro prints the file name and function */
1065 if (!audio->running || audio->pcm_feedback) {
1066 rc = -EINVAL;
1067 goto done_nolock;
1068 }
1069
1070 mutex_lock(&audio->write_lock);
1071
1072 rc = wait_event_interruptible(audio->write_wait,
1073 (!audio->out[0].used &&
1074 !audio->out[1].used &&
1075 audio->out_needed) || audio->wflush);
1076
1077 if (rc < 0)
1078 goto done;
1079 else if (audio->wflush) {
1080 rc = -EBUSY;
1081 goto done;
1082 }
1083
1084 /* pcm dmamiss message is sent continously
1085 * when decoder is starved so no race
1086 * condition concern
1087 */
1088 audio->teos = 0;
1089
1090 rc = wait_event_interruptible(audio->write_wait,
1091 audio->teos || audio->wflush);
1092
1093 if (audio->wflush)
1094 rc = -EBUSY;
1095
1096done:
1097 mutex_unlock(&audio->write_lock);
1098done_nolock:
1099 return rc;
1100}
1101
1102static ssize_t audevrc_read(struct file *file, char __user *buf, size_t count,
1103 loff_t *pos)
1104{
1105 struct audio *audio = file->private_data;
1106 const char __user *start = buf;
1107 int rc = 0;
1108 if (!audio->pcm_feedback) {
1109 return 0;
1110 /* PCM feedback is not enabled. Nothing to read */
1111 }
1112 mutex_lock(&audio->read_lock);
1113 MM_DBG("\n"); /* Macro prints the file name and function */
1114 while (count > 0) {
1115 rc = wait_event_interruptible(audio->read_wait,
1116 (audio->in[audio->read_next].used > 0) ||
1117 (audio->stopped) || (audio->rflush));
1118
1119 MM_DBG("wait terminated \n");
1120 if (rc < 0)
1121 break;
1122 if (audio->stopped || audio->rflush) {
1123 rc = -EBUSY;
1124 break;
1125 }
1126 if (count < audio->in[audio->read_next].used) {
1127 /* Read must happen in frame boundary. Since driver does
1128 * not know frame size, read count must be greater or
1129 * equal to size of PCM samples
1130 */
1131 MM_DBG("read stop - partial frame\n");
1132 break;
1133 } else {
1134 MM_DBG("read from in[%d]\n", audio->read_next);
1135 /* order reads from the output buffer */
1136 rmb();
1137 if (copy_to_user
1138 (buf, audio->in[audio->read_next].data,
1139 audio->in[audio->read_next].used)) {
1140 MM_ERR("invalid addr %x \n",
1141 (unsigned int)buf);
1142 rc = -EFAULT;
1143 break;
1144 }
1145 count -= audio->in[audio->read_next].used;
1146 buf += audio->in[audio->read_next].used;
1147 audio->in[audio->read_next].used = 0;
1148 if ((++audio->read_next) == audio->pcm_buf_count)
1149 audio->read_next = 0;
1150 break;
1151 /* Force to exit while loop
1152 * to prevent output thread
1153 * sleep too long if data is
1154 * not ready at this moment
1155 */
1156
1157 }
1158 }
1159 /* don't feed output buffer to HW decoder during flushing
1160 * buffer refresh command will be sent once flush completes
1161 * send buf refresh command here can confuse HW decoder
1162 */
1163 if (audio->buf_refresh && !audio->rflush) {
1164 audio->buf_refresh = 0;
1165 MM_DBG("kick start pcm feedback again\n");
1166 audevrc_buffer_refresh(audio);
1167 }
1168 mutex_unlock(&audio->read_lock);
1169 if (buf > start)
1170 rc = buf - start;
1171 MM_DBG("read %d bytes\n", rc);
1172 return rc;
1173}
1174
1175static int audevrc_process_eos(struct audio *audio,
1176 const char __user *buf_start, unsigned short mfield_size)
1177{
1178 int rc = 0;
1179 struct buffer *frame;
1180
1181 frame = audio->out + audio->out_head;
1182
1183 rc = wait_event_interruptible(audio->write_wait,
1184 (audio->out_needed &&
1185 audio->out[0].used == 0 &&
1186 audio->out[1].used == 0)
1187 || (audio->stopped)
1188 || (audio->wflush));
1189
1190 if (rc < 0)
1191 goto done;
1192 if (audio->stopped || audio->wflush) {
1193 rc = -EBUSY;
1194 goto done;
1195 }
1196
1197 if (copy_from_user(frame->data, buf_start, mfield_size)) {
1198 rc = -EFAULT;
1199 goto done;
1200 }
1201
1202 frame->mfield_sz = mfield_size;
1203 audio->out_head ^= 1;
1204 frame->used = mfield_size;
1205 audevrc_send_data(audio, 0);
1206
1207done:
1208 return rc;
1209}
1210
1211static ssize_t audevrc_write(struct file *file, const char __user *buf,
1212 size_t count, loff_t *pos)
1213{
1214 struct audio *audio = file->private_data;
1215 const char __user *start = buf;
1216 struct buffer *frame;
1217 size_t xfer;
1218 char *cpy_ptr;
1219 unsigned short mfield_size = 0;
1220 int rc = 0, eos_condition = AUDEVRC_EOS_NONE;
1221
1222 MM_DBG("cnt=%d\n", count);
1223
1224 if (count & 1)
1225 return -EINVAL;
1226
1227 mutex_lock(&audio->write_lock);
1228 while (count > 0) {
1229 frame = audio->out + audio->out_head;
1230 cpy_ptr = frame->data;
1231 rc = wait_event_interruptible(audio->write_wait,
1232 (frame->used == 0)
1233 || (audio->stopped)
1234 || (audio->wflush));
1235 if (rc < 0)
1236 break;
1237 if (audio->stopped || audio->wflush) {
1238 rc = -EBUSY;
1239 break;
1240 }
1241
1242 if (audio->mfield) {
1243 if (buf == start) {
1244 /* Processing beginning of user buffer */
1245 if (__get_user(mfield_size,
1246 (unsigned short __user *) buf)) {
1247 rc = -EFAULT;
1248 break;
1249 } else if (mfield_size > count) {
1250 rc = -EINVAL;
1251 break;
1252 }
1253 MM_DBG("mf offset_val %x\n", mfield_size);
1254 if (copy_from_user(cpy_ptr, buf,
1255 mfield_size)) {
1256 rc = -EFAULT;
1257 break;
1258 }
1259 /* Check if EOS flag is set and buffer has
1260 * contains just meta field
1261 */
1262 if (cpy_ptr[AUDEVRC_EOS_FLG_OFFSET] &
1263 AUDEVRC_EOS_FLG_MASK) {
1264 MM_DBG("eos set\n");
1265 eos_condition = AUDEVRC_EOS_SET;
1266 if (mfield_size == count) {
1267 buf += mfield_size;
1268 break;
1269 } else
1270 cpy_ptr[AUDEVRC_EOS_FLG_OFFSET] &=
1271 ~AUDEVRC_EOS_FLG_MASK;
1272 }
1273 /* Check EOS to see if */
1274 cpy_ptr += mfield_size;
1275 count -= mfield_size;
1276 buf += mfield_size;
1277 } else {
1278 mfield_size = 0;
1279 MM_DBG("continuous buffer\n");
1280 }
1281 frame->mfield_sz = mfield_size;
1282 }
1283
1284 xfer = (count > (frame->size - mfield_size)) ?
1285 (frame->size - mfield_size) : count;
1286 if (copy_from_user(cpy_ptr, buf, xfer)) {
1287 rc = -EFAULT;
1288 break;
1289 }
1290
1291 frame->used = xfer + mfield_size;
1292 audio->out_head ^= 1;
1293 count -= xfer;
1294 buf += xfer;
1295 audevrc_send_data(audio, 0);
1296 }
1297 if (eos_condition == AUDEVRC_EOS_SET)
1298 rc = audevrc_process_eos(audio, start, mfield_size);
1299 mutex_unlock(&audio->write_lock);
1300 if (!rc) {
1301 if (buf > start)
1302 return buf - start;
1303 }
1304 return rc;
1305}
1306
1307static int audevrc_release(struct inode *inode, struct file *file)
1308{
1309 struct audio *audio = file->private_data;
1310
1311 MM_INFO("audio instance 0x%08x freeing\n", (int)audio);
1312 mutex_lock(&audio->lock);
1313 audevrc_disable(audio);
1314 if (audio->rmt_resource_released == 0)
1315 rmt_put_resource(audio);
1316 audevrc_flush(audio);
1317 audevrc_flush_pcm_buf(audio);
1318 msm_adsp_put(audio->audplay);
1319 audpp_adec_free(audio->dec_id);
1320#ifdef CONFIG_HAS_EARLYSUSPEND
1321 unregister_early_suspend(&audio->suspend_ctl.node);
1322#endif
1323 audio->event_abort = 1;
1324 wake_up(&audio->event_wait);
1325 audevrc_reset_event_queue(audio);
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301326 ion_unmap_kernel(audio->client, audio->output_buff_handle);
1327 ion_free(audio->client, audio->output_buff_handle);
1328 if (audio->input_buff_handle != NULL) {
1329 ion_unmap_kernel(audio->client, audio->input_buff_handle);
1330 ion_free(audio->client, audio->input_buff_handle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001331 }
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301332 ion_client_destroy(audio->client);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001333 mutex_unlock(&audio->lock);
1334#ifdef CONFIG_DEBUG_FS
1335 if (audio->dentry)
1336 debugfs_remove(audio->dentry);
1337#endif
1338 kfree(audio);
1339 return 0;
1340}
1341
1342#ifdef CONFIG_HAS_EARLYSUSPEND
1343static void audevrc_post_event(struct audio *audio, int type,
1344 union msm_audio_event_payload payload)
1345{
1346 struct audevrc_event *e_node = NULL;
1347 unsigned long flags;
1348
1349 spin_lock_irqsave(&audio->event_queue_lock, flags);
1350
1351 if (!list_empty(&audio->free_event_queue)) {
1352 e_node = list_first_entry(&audio->free_event_queue,
1353 struct audevrc_event, list);
1354 list_del(&e_node->list);
1355 } else {
1356 e_node = kmalloc(sizeof(struct audevrc_event), GFP_ATOMIC);
1357 if (!e_node) {
1358 MM_ERR("No mem to post event %d\n", type);
1359 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
1360 return;
1361 }
1362 }
1363
1364 e_node->event_type = type;
1365 e_node->payload = payload;
1366
1367 list_add_tail(&e_node->list, &audio->event_queue);
1368 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
1369 wake_up(&audio->event_wait);
1370}
1371
1372static void audevrc_suspend(struct early_suspend *h)
1373{
1374 struct audevrc_suspend_ctl *ctl =
1375 container_of(h, struct audevrc_suspend_ctl, node);
1376 union msm_audio_event_payload payload;
1377
1378 MM_DBG("\n"); /* Macro prints the file name and function */
1379 audevrc_post_event(ctl->audio, AUDIO_EVENT_SUSPEND, payload);
1380}
1381
1382static void audevrc_resume(struct early_suspend *h)
1383{
1384 struct audevrc_suspend_ctl *ctl =
1385 container_of(h, struct audevrc_suspend_ctl, node);
1386 union msm_audio_event_payload payload;
1387
1388 MM_DBG("\n"); /* Macro prints the file name and function */
1389 audevrc_post_event(ctl->audio, AUDIO_EVENT_RESUME, payload);
1390}
1391#endif
1392
1393#ifdef CONFIG_DEBUG_FS
1394static ssize_t audevrc_debug_open(struct inode *inode, struct file *file)
1395{
1396 file->private_data = inode->i_private;
1397 return 0;
1398}
1399
1400static ssize_t audevrc_debug_read(struct file *file, char __user *buf,
1401 size_t count, loff_t *ppos)
1402{
1403 const int debug_bufmax = 1024;
1404 static char buffer[1024];
1405 int n = 0, i;
1406 struct audio *audio = file->private_data;
1407
1408 mutex_lock(&audio->lock);
1409 n = scnprintf(buffer, debug_bufmax, "opened %d\n", audio->opened);
1410 n += scnprintf(buffer + n, debug_bufmax - n,
1411 "enabled %d\n", audio->enabled);
1412 n += scnprintf(buffer + n, debug_bufmax - n,
1413 "stopped %d\n", audio->stopped);
1414 n += scnprintf(buffer + n, debug_bufmax - n,
1415 "pcm_feedback %d\n", audio->pcm_feedback);
1416 n += scnprintf(buffer + n, debug_bufmax - n,
1417 "out_buf_sz %d\n", audio->out[0].size);
1418 n += scnprintf(buffer + n, debug_bufmax - n,
1419 "pcm_buf_count %d \n", audio->pcm_buf_count);
1420 n += scnprintf(buffer + n, debug_bufmax - n,
1421 "pcm_buf_sz %d \n", audio->in[0].size);
1422 n += scnprintf(buffer + n, debug_bufmax - n,
1423 "volume %x \n", audio->vol_pan.volume);
1424 mutex_unlock(&audio->lock);
1425 /* Following variables are only useful for debugging when
1426 * when playback halts unexpectedly. Thus, no mutual exclusion
1427 * enforced
1428 */
1429 n += scnprintf(buffer + n, debug_bufmax - n,
1430 "wflush %d\n", audio->wflush);
1431 n += scnprintf(buffer + n, debug_bufmax - n,
1432 "rflush %d\n", audio->rflush);
1433 n += scnprintf(buffer + n, debug_bufmax - n,
1434 "running %d \n", audio->running);
1435 n += scnprintf(buffer + n, debug_bufmax - n,
1436 "dec state %d \n", audio->dec_state);
1437 n += scnprintf(buffer + n, debug_bufmax - n,
1438 "out_needed %d \n", audio->out_needed);
1439 n += scnprintf(buffer + n, debug_bufmax - n,
1440 "out_head %d \n", audio->out_head);
1441 n += scnprintf(buffer + n, debug_bufmax - n,
1442 "out_tail %d \n", audio->out_tail);
1443 n += scnprintf(buffer + n, debug_bufmax - n,
1444 "out[0].used %d \n", audio->out[0].used);
1445 n += scnprintf(buffer + n, debug_bufmax - n,
1446 "out[1].used %d \n", audio->out[1].used);
1447 n += scnprintf(buffer + n, debug_bufmax - n,
1448 "buffer_refresh %d \n", audio->buf_refresh);
1449 n += scnprintf(buffer + n, debug_bufmax - n,
1450 "read_next %d \n", audio->read_next);
1451 n += scnprintf(buffer + n, debug_bufmax - n,
1452 "fill_next %d \n", audio->fill_next);
1453 for (i = 0; i < audio->pcm_buf_count; i++)
1454 n += scnprintf(buffer + n, debug_bufmax - n,
1455 "in[%d].size %d \n", i, audio->in[i].used);
1456 buffer[n] = 0;
1457 return simple_read_from_buffer(buf, count, ppos, buffer, n);
1458}
1459
1460static const struct file_operations audevrc_debug_fops = {
1461 .read = audevrc_debug_read,
1462 .open = audevrc_debug_open,
1463};
1464#endif
1465
1466static int audevrc_open(struct inode *inode, struct file *file)
1467{
1468 struct audio *audio = NULL;
1469 int rc, dec_attrb, decid, i;
1470 struct audevrc_event *e_node = NULL;
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301471 unsigned mem_sz = DMASZ;
1472 unsigned long ionflag = 0;
1473 ion_phys_addr_t addr = 0;
1474 struct ion_handle *handle = NULL;
1475 struct ion_client *client = NULL;
1476 int len = 0;
1477
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001478#ifdef CONFIG_DEBUG_FS
1479 /* 4 bytes represents decoder number, 1 byte for terminate string */
1480 char name[sizeof "msm_evrc_" + 5];
1481#endif
1482
1483 /* Allocate audio instance, set to zero */
1484 audio = kzalloc(sizeof(struct audio), GFP_KERNEL);
1485 if (!audio) {
1486 MM_ERR("no memory to allocate audio instance\n");
1487 rc = -ENOMEM;
1488 goto done;
1489 }
1490 MM_INFO("audio instance 0x%08x created\n", (int)audio);
1491
1492 /* Allocate the decoder */
1493 dec_attrb = AUDDEC_DEC_EVRC;
1494 if ((file->f_mode & FMODE_WRITE) &&
1495 (file->f_mode & FMODE_READ)) {
1496 dec_attrb |= MSM_AUD_MODE_NONTUNNEL;
1497 audio->pcm_feedback = NON_TUNNEL_MODE_PLAYBACK;
1498 } else if ((file->f_mode & FMODE_WRITE) &&
1499 !(file->f_mode & FMODE_READ)) {
1500 dec_attrb |= MSM_AUD_MODE_TUNNEL;
1501 audio->pcm_feedback = TUNNEL_MODE_PLAYBACK;
1502 } else {
1503 kfree(audio);
1504 rc = -EACCES;
1505 goto done;
1506 }
1507 decid = audpp_adec_alloc(dec_attrb, &audio->module_name,
1508 &audio->queue_id);
1509
1510 if (decid < 0) {
1511 MM_ERR("No free decoder available, freeing instance 0x%08x\n",
1512 (int)audio);
1513 rc = -ENODEV;
1514 kfree(audio);
1515 goto done;
1516 }
1517
1518 audio->dec_id = decid & MSM_AUD_DECODER_MASK;
1519
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301520 client = msm_ion_client_create(UINT_MAX, "Audio_EVRC_Client");
1521 if (IS_ERR_OR_NULL(client)) {
1522 pr_err("Unable to create ION client\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001523 rc = -ENOMEM;
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301524 goto client_create_error;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001525 }
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301526 audio->client = client;
1527
1528 handle = ion_alloc(client, mem_sz, SZ_4K,
Hanumant Singh7d72bad2012-08-29 18:39:44 -07001529 ION_HEAP(ION_AUDIO_HEAP_ID), 0);
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301530 if (IS_ERR_OR_NULL(handle)) {
1531 MM_ERR("Unable to create allocate O/P buffers\n");
1532 rc = -ENOMEM;
1533 goto output_buff_alloc_error;
1534 }
1535 audio->output_buff_handle = handle;
1536
1537 rc = ion_phys(client, handle, &addr, &len);
1538 if (rc) {
1539 MM_ERR("O/P buffers:Invalid phy: %x sz: %x\n",
1540 (unsigned int) addr, (unsigned int) len);
1541 goto output_buff_get_phys_error;
1542 } else {
1543 MM_INFO("O/P buffers:valid phy: %x sz: %x\n",
1544 (unsigned int) addr, (unsigned int) len);
1545 }
1546 audio->phys = (int32_t)addr;
1547
1548
1549 rc = ion_handle_get_flags(client, handle, &ionflag);
1550 if (rc) {
1551 MM_ERR("could not get flags for the handle\n");
1552 goto output_buff_get_flags_error;
1553 }
1554
Mitchel Humpherys911b4b72012-09-12 14:42:50 -07001555 audio->map_v_write = ion_map_kernel(client, handle);
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301556 if (IS_ERR(audio->map_v_write)) {
1557 MM_ERR("could not map write buffers\n");
1558 rc = -ENOMEM;
1559 goto output_buff_map_error;
1560 }
1561 audio->data = audio->map_v_write;
1562 MM_DBG("write buf: phy addr 0x%08x kernel addr 0x%08x\n",
1563 audio->phys, (int)audio->data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001564
1565 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK) {
1566 rc = audmgr_open(&audio->audmgr);
1567 if (rc) {
1568 MM_ERR("audmgr open failed, freeing instance \
1569 0x%08x\n", (int)audio);
1570 goto err;
1571 }
1572 }
1573
1574 rc = msm_adsp_get(audio->module_name, &audio->audplay,
1575 &audplay_adsp_ops_evrc, audio);
1576
1577 if (rc) {
1578 MM_ERR("failed to get %s module, freeing instance 0x%08x\n",
1579 audio->module_name, (int)audio);
1580 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK)
1581 audmgr_close(&audio->audmgr);
1582 goto err;
1583 }
1584
1585 rc = rmt_get_resource(audio);
1586 if (rc) {
1587 MM_ERR("ADSP resources are not available for EVRC session \
1588 0x%08x on decoder: %d\n", (int)audio, audio->dec_id);
1589 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK)
1590 audmgr_close(&audio->audmgr);
1591 msm_adsp_put(audio->audplay);
1592 goto err;
1593 }
1594
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301595 audio->input_buff_handle = NULL;
1596
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001597 /* Initialize all locks of audio instance */
1598 mutex_init(&audio->lock);
1599 mutex_init(&audio->write_lock);
1600 mutex_init(&audio->read_lock);
1601 mutex_init(&audio->get_event_lock);
1602 spin_lock_init(&audio->dsp_lock);
1603 init_waitqueue_head(&audio->write_wait);
1604 init_waitqueue_head(&audio->read_wait);
1605 INIT_LIST_HEAD(&audio->free_event_queue);
1606 INIT_LIST_HEAD(&audio->event_queue);
1607 init_waitqueue_head(&audio->wait);
1608 init_waitqueue_head(&audio->event_wait);
1609 spin_lock_init(&audio->event_queue_lock);
1610
1611 audio->out[0].data = audio->data + 0;
1612 audio->out[0].addr = audio->phys + 0;
1613 audio->out[0].size = BUFSZ;
1614
1615 audio->out[1].data = audio->data + BUFSZ;
1616 audio->out[1].addr = audio->phys + BUFSZ;
1617 audio->out[1].size = BUFSZ;
1618
1619 audio->vol_pan.volume = 0x3FFF;
1620
1621 audevrc_flush(audio);
1622
1623 file->private_data = audio;
1624 audio->opened = 1;
1625#ifdef CONFIG_DEBUG_FS
1626 snprintf(name, sizeof name, "msm_evrc_%04x", audio->dec_id);
1627 audio->dentry = debugfs_create_file(name, S_IFREG | S_IRUGO,
1628 NULL, (void *) audio, &audevrc_debug_fops);
1629
1630 if (IS_ERR(audio->dentry))
1631 MM_DBG("debugfs_create_file failed\n");
1632#endif
1633#ifdef CONFIG_HAS_EARLYSUSPEND
1634 audio->suspend_ctl.node.level = EARLY_SUSPEND_LEVEL_DISABLE_FB;
1635 audio->suspend_ctl.node.resume = audevrc_resume;
1636 audio->suspend_ctl.node.suspend = audevrc_suspend;
1637 audio->suspend_ctl.audio = audio;
1638 register_early_suspend(&audio->suspend_ctl.node);
1639#endif
1640 for (i = 0; i < AUDEVRC_EVENT_NUM; i++) {
1641 e_node = kmalloc(sizeof(struct audevrc_event), GFP_KERNEL);
1642 if (e_node)
1643 list_add_tail(&e_node->list, &audio->free_event_queue);
1644 else {
1645 MM_ERR("event pkt alloc failed\n");
1646 break;
1647 }
1648 }
1649done:
1650 return rc;
1651err:
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301652 ion_unmap_kernel(client, audio->output_buff_handle);
1653output_buff_map_error:
1654output_buff_get_phys_error:
1655output_buff_get_flags_error:
1656 ion_free(client, audio->output_buff_handle);
1657output_buff_alloc_error:
1658 ion_client_destroy(client);
1659client_create_error:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001660 audpp_adec_free(audio->dec_id);
1661 kfree(audio);
1662 return rc;
1663}
1664
1665static const struct file_operations audio_evrc_fops = {
1666 .owner = THIS_MODULE,
1667 .open = audevrc_open,
1668 .release = audevrc_release,
1669 .read = audevrc_read,
1670 .write = audevrc_write,
1671 .unlocked_ioctl = audevrc_ioctl,
1672 .fsync = audevrc_fsync,
1673};
1674
1675struct miscdevice audio_evrc_misc = {
1676 .minor = MISC_DYNAMIC_MINOR,
1677 .name = "msm_evrc",
1678 .fops = &audio_evrc_fops,
1679};
1680
1681static int __init audevrc_init(void)
1682{
1683 return misc_register(&audio_evrc_misc);
1684
1685}
1686
1687static void __exit audevrc_exit(void)
1688{
1689 misc_deregister(&audio_evrc_misc);
1690}
1691
1692module_init(audevrc_init);
1693module_exit(audevrc_exit);
1694
1695MODULE_DESCRIPTION("MSM EVRC driver");
1696MODULE_LICENSE("GPL v2");