blob: c0486dbc59d2803abc32919e087fc430909fb41f [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),
969 SZ_4K, ION_HEAP(ION_AUDIO_HEAP_ID));
970 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(
1006 audio->client,
1007 handle, ionflag);
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05301008 if (IS_ERR(audio->map_v_read)) {
1009 MM_ERR("failed to map mem"
1010 " for read buf\n");
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301011 ion_free(audio->client, handle);
1012 audio->input_buff_handle = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001013 rc = -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001014 } else {
1015 uint8_t index;
1016 uint32_t offset = 0;
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05301017 audio->read_data =
Laura Abbott35111d32012-04-27 18:41:48 -07001018 audio->map_v_read;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001019 audio->buf_refresh = 0;
1020 audio->pcm_buf_count =
1021 config.buffer_count;
1022 audio->read_next = 0;
1023 audio->fill_next = 0;
1024
1025 for (index = 0;
1026 index < config.buffer_count;
1027 index++) {
1028 audio->in[index].data =
1029 audio->read_data + offset;
1030 audio->in[index].addr =
1031 audio->read_phys + offset;
1032 audio->in[index].size =
1033 config.buffer_size;
1034 audio->in[index].used = 0;
1035 offset += config.buffer_size;
1036 }
1037 MM_DBG("read buf: phy addr \
1038 0x%08x kernel addr 0x%08x\n",
1039 audio->read_phys,
1040 (int)audio->read_data);
1041 rc = 0;
1042 }
1043 } else {
1044 rc = 0;
1045 }
1046 break;
1047 }
1048 case AUDIO_PAUSE:
1049 MM_DBG("AUDIO_PAUSE %ld\n", arg);
1050 rc = audpp_pause(audio->dec_id, (int) arg);
1051 break;
1052 default:
1053 rc = -EINVAL;
1054 }
1055 mutex_unlock(&audio->lock);
1056 return rc;
1057}
1058
1059/* Only useful in tunnel-mode */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001060static int audevrc_fsync(struct file *file, loff_t a, loff_t b, int datasync)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001061{
1062 struct audio *audio = file->private_data;
1063 int rc = 0;
1064
1065 MM_DBG("\n"); /* Macro prints the file name and function */
1066 if (!audio->running || audio->pcm_feedback) {
1067 rc = -EINVAL;
1068 goto done_nolock;
1069 }
1070
1071 mutex_lock(&audio->write_lock);
1072
1073 rc = wait_event_interruptible(audio->write_wait,
1074 (!audio->out[0].used &&
1075 !audio->out[1].used &&
1076 audio->out_needed) || audio->wflush);
1077
1078 if (rc < 0)
1079 goto done;
1080 else if (audio->wflush) {
1081 rc = -EBUSY;
1082 goto done;
1083 }
1084
1085 /* pcm dmamiss message is sent continously
1086 * when decoder is starved so no race
1087 * condition concern
1088 */
1089 audio->teos = 0;
1090
1091 rc = wait_event_interruptible(audio->write_wait,
1092 audio->teos || audio->wflush);
1093
1094 if (audio->wflush)
1095 rc = -EBUSY;
1096
1097done:
1098 mutex_unlock(&audio->write_lock);
1099done_nolock:
1100 return rc;
1101}
1102
1103static ssize_t audevrc_read(struct file *file, char __user *buf, size_t count,
1104 loff_t *pos)
1105{
1106 struct audio *audio = file->private_data;
1107 const char __user *start = buf;
1108 int rc = 0;
1109 if (!audio->pcm_feedback) {
1110 return 0;
1111 /* PCM feedback is not enabled. Nothing to read */
1112 }
1113 mutex_lock(&audio->read_lock);
1114 MM_DBG("\n"); /* Macro prints the file name and function */
1115 while (count > 0) {
1116 rc = wait_event_interruptible(audio->read_wait,
1117 (audio->in[audio->read_next].used > 0) ||
1118 (audio->stopped) || (audio->rflush));
1119
1120 MM_DBG("wait terminated \n");
1121 if (rc < 0)
1122 break;
1123 if (audio->stopped || audio->rflush) {
1124 rc = -EBUSY;
1125 break;
1126 }
1127 if (count < audio->in[audio->read_next].used) {
1128 /* Read must happen in frame boundary. Since driver does
1129 * not know frame size, read count must be greater or
1130 * equal to size of PCM samples
1131 */
1132 MM_DBG("read stop - partial frame\n");
1133 break;
1134 } else {
1135 MM_DBG("read from in[%d]\n", audio->read_next);
1136 /* order reads from the output buffer */
1137 rmb();
1138 if (copy_to_user
1139 (buf, audio->in[audio->read_next].data,
1140 audio->in[audio->read_next].used)) {
1141 MM_ERR("invalid addr %x \n",
1142 (unsigned int)buf);
1143 rc = -EFAULT;
1144 break;
1145 }
1146 count -= audio->in[audio->read_next].used;
1147 buf += audio->in[audio->read_next].used;
1148 audio->in[audio->read_next].used = 0;
1149 if ((++audio->read_next) == audio->pcm_buf_count)
1150 audio->read_next = 0;
1151 break;
1152 /* Force to exit while loop
1153 * to prevent output thread
1154 * sleep too long if data is
1155 * not ready at this moment
1156 */
1157
1158 }
1159 }
1160 /* don't feed output buffer to HW decoder during flushing
1161 * buffer refresh command will be sent once flush completes
1162 * send buf refresh command here can confuse HW decoder
1163 */
1164 if (audio->buf_refresh && !audio->rflush) {
1165 audio->buf_refresh = 0;
1166 MM_DBG("kick start pcm feedback again\n");
1167 audevrc_buffer_refresh(audio);
1168 }
1169 mutex_unlock(&audio->read_lock);
1170 if (buf > start)
1171 rc = buf - start;
1172 MM_DBG("read %d bytes\n", rc);
1173 return rc;
1174}
1175
1176static int audevrc_process_eos(struct audio *audio,
1177 const char __user *buf_start, unsigned short mfield_size)
1178{
1179 int rc = 0;
1180 struct buffer *frame;
1181
1182 frame = audio->out + audio->out_head;
1183
1184 rc = wait_event_interruptible(audio->write_wait,
1185 (audio->out_needed &&
1186 audio->out[0].used == 0 &&
1187 audio->out[1].used == 0)
1188 || (audio->stopped)
1189 || (audio->wflush));
1190
1191 if (rc < 0)
1192 goto done;
1193 if (audio->stopped || audio->wflush) {
1194 rc = -EBUSY;
1195 goto done;
1196 }
1197
1198 if (copy_from_user(frame->data, buf_start, mfield_size)) {
1199 rc = -EFAULT;
1200 goto done;
1201 }
1202
1203 frame->mfield_sz = mfield_size;
1204 audio->out_head ^= 1;
1205 frame->used = mfield_size;
1206 audevrc_send_data(audio, 0);
1207
1208done:
1209 return rc;
1210}
1211
1212static ssize_t audevrc_write(struct file *file, const char __user *buf,
1213 size_t count, loff_t *pos)
1214{
1215 struct audio *audio = file->private_data;
1216 const char __user *start = buf;
1217 struct buffer *frame;
1218 size_t xfer;
1219 char *cpy_ptr;
1220 unsigned short mfield_size = 0;
1221 int rc = 0, eos_condition = AUDEVRC_EOS_NONE;
1222
1223 MM_DBG("cnt=%d\n", count);
1224
1225 if (count & 1)
1226 return -EINVAL;
1227
1228 mutex_lock(&audio->write_lock);
1229 while (count > 0) {
1230 frame = audio->out + audio->out_head;
1231 cpy_ptr = frame->data;
1232 rc = wait_event_interruptible(audio->write_wait,
1233 (frame->used == 0)
1234 || (audio->stopped)
1235 || (audio->wflush));
1236 if (rc < 0)
1237 break;
1238 if (audio->stopped || audio->wflush) {
1239 rc = -EBUSY;
1240 break;
1241 }
1242
1243 if (audio->mfield) {
1244 if (buf == start) {
1245 /* Processing beginning of user buffer */
1246 if (__get_user(mfield_size,
1247 (unsigned short __user *) buf)) {
1248 rc = -EFAULT;
1249 break;
1250 } else if (mfield_size > count) {
1251 rc = -EINVAL;
1252 break;
1253 }
1254 MM_DBG("mf offset_val %x\n", mfield_size);
1255 if (copy_from_user(cpy_ptr, buf,
1256 mfield_size)) {
1257 rc = -EFAULT;
1258 break;
1259 }
1260 /* Check if EOS flag is set and buffer has
1261 * contains just meta field
1262 */
1263 if (cpy_ptr[AUDEVRC_EOS_FLG_OFFSET] &
1264 AUDEVRC_EOS_FLG_MASK) {
1265 MM_DBG("eos set\n");
1266 eos_condition = AUDEVRC_EOS_SET;
1267 if (mfield_size == count) {
1268 buf += mfield_size;
1269 break;
1270 } else
1271 cpy_ptr[AUDEVRC_EOS_FLG_OFFSET] &=
1272 ~AUDEVRC_EOS_FLG_MASK;
1273 }
1274 /* Check EOS to see if */
1275 cpy_ptr += mfield_size;
1276 count -= mfield_size;
1277 buf += mfield_size;
1278 } else {
1279 mfield_size = 0;
1280 MM_DBG("continuous buffer\n");
1281 }
1282 frame->mfield_sz = mfield_size;
1283 }
1284
1285 xfer = (count > (frame->size - mfield_size)) ?
1286 (frame->size - mfield_size) : count;
1287 if (copy_from_user(cpy_ptr, buf, xfer)) {
1288 rc = -EFAULT;
1289 break;
1290 }
1291
1292 frame->used = xfer + mfield_size;
1293 audio->out_head ^= 1;
1294 count -= xfer;
1295 buf += xfer;
1296 audevrc_send_data(audio, 0);
1297 }
1298 if (eos_condition == AUDEVRC_EOS_SET)
1299 rc = audevrc_process_eos(audio, start, mfield_size);
1300 mutex_unlock(&audio->write_lock);
1301 if (!rc) {
1302 if (buf > start)
1303 return buf - start;
1304 }
1305 return rc;
1306}
1307
1308static int audevrc_release(struct inode *inode, struct file *file)
1309{
1310 struct audio *audio = file->private_data;
1311
1312 MM_INFO("audio instance 0x%08x freeing\n", (int)audio);
1313 mutex_lock(&audio->lock);
1314 audevrc_disable(audio);
1315 if (audio->rmt_resource_released == 0)
1316 rmt_put_resource(audio);
1317 audevrc_flush(audio);
1318 audevrc_flush_pcm_buf(audio);
1319 msm_adsp_put(audio->audplay);
1320 audpp_adec_free(audio->dec_id);
1321#ifdef CONFIG_HAS_EARLYSUSPEND
1322 unregister_early_suspend(&audio->suspend_ctl.node);
1323#endif
1324 audio->event_abort = 1;
1325 wake_up(&audio->event_wait);
1326 audevrc_reset_event_queue(audio);
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301327 ion_unmap_kernel(audio->client, audio->output_buff_handle);
1328 ion_free(audio->client, audio->output_buff_handle);
1329 if (audio->input_buff_handle != NULL) {
1330 ion_unmap_kernel(audio->client, audio->input_buff_handle);
1331 ion_free(audio->client, audio->input_buff_handle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001332 }
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301333 ion_client_destroy(audio->client);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001334 mutex_unlock(&audio->lock);
1335#ifdef CONFIG_DEBUG_FS
1336 if (audio->dentry)
1337 debugfs_remove(audio->dentry);
1338#endif
1339 kfree(audio);
1340 return 0;
1341}
1342
1343#ifdef CONFIG_HAS_EARLYSUSPEND
1344static void audevrc_post_event(struct audio *audio, int type,
1345 union msm_audio_event_payload payload)
1346{
1347 struct audevrc_event *e_node = NULL;
1348 unsigned long flags;
1349
1350 spin_lock_irqsave(&audio->event_queue_lock, flags);
1351
1352 if (!list_empty(&audio->free_event_queue)) {
1353 e_node = list_first_entry(&audio->free_event_queue,
1354 struct audevrc_event, list);
1355 list_del(&e_node->list);
1356 } else {
1357 e_node = kmalloc(sizeof(struct audevrc_event), GFP_ATOMIC);
1358 if (!e_node) {
1359 MM_ERR("No mem to post event %d\n", type);
1360 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
1361 return;
1362 }
1363 }
1364
1365 e_node->event_type = type;
1366 e_node->payload = payload;
1367
1368 list_add_tail(&e_node->list, &audio->event_queue);
1369 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
1370 wake_up(&audio->event_wait);
1371}
1372
1373static void audevrc_suspend(struct early_suspend *h)
1374{
1375 struct audevrc_suspend_ctl *ctl =
1376 container_of(h, struct audevrc_suspend_ctl, node);
1377 union msm_audio_event_payload payload;
1378
1379 MM_DBG("\n"); /* Macro prints the file name and function */
1380 audevrc_post_event(ctl->audio, AUDIO_EVENT_SUSPEND, payload);
1381}
1382
1383static void audevrc_resume(struct early_suspend *h)
1384{
1385 struct audevrc_suspend_ctl *ctl =
1386 container_of(h, struct audevrc_suspend_ctl, node);
1387 union msm_audio_event_payload payload;
1388
1389 MM_DBG("\n"); /* Macro prints the file name and function */
1390 audevrc_post_event(ctl->audio, AUDIO_EVENT_RESUME, payload);
1391}
1392#endif
1393
1394#ifdef CONFIG_DEBUG_FS
1395static ssize_t audevrc_debug_open(struct inode *inode, struct file *file)
1396{
1397 file->private_data = inode->i_private;
1398 return 0;
1399}
1400
1401static ssize_t audevrc_debug_read(struct file *file, char __user *buf,
1402 size_t count, loff_t *ppos)
1403{
1404 const int debug_bufmax = 1024;
1405 static char buffer[1024];
1406 int n = 0, i;
1407 struct audio *audio = file->private_data;
1408
1409 mutex_lock(&audio->lock);
1410 n = scnprintf(buffer, debug_bufmax, "opened %d\n", audio->opened);
1411 n += scnprintf(buffer + n, debug_bufmax - n,
1412 "enabled %d\n", audio->enabled);
1413 n += scnprintf(buffer + n, debug_bufmax - n,
1414 "stopped %d\n", audio->stopped);
1415 n += scnprintf(buffer + n, debug_bufmax - n,
1416 "pcm_feedback %d\n", audio->pcm_feedback);
1417 n += scnprintf(buffer + n, debug_bufmax - n,
1418 "out_buf_sz %d\n", audio->out[0].size);
1419 n += scnprintf(buffer + n, debug_bufmax - n,
1420 "pcm_buf_count %d \n", audio->pcm_buf_count);
1421 n += scnprintf(buffer + n, debug_bufmax - n,
1422 "pcm_buf_sz %d \n", audio->in[0].size);
1423 n += scnprintf(buffer + n, debug_bufmax - n,
1424 "volume %x \n", audio->vol_pan.volume);
1425 mutex_unlock(&audio->lock);
1426 /* Following variables are only useful for debugging when
1427 * when playback halts unexpectedly. Thus, no mutual exclusion
1428 * enforced
1429 */
1430 n += scnprintf(buffer + n, debug_bufmax - n,
1431 "wflush %d\n", audio->wflush);
1432 n += scnprintf(buffer + n, debug_bufmax - n,
1433 "rflush %d\n", audio->rflush);
1434 n += scnprintf(buffer + n, debug_bufmax - n,
1435 "running %d \n", audio->running);
1436 n += scnprintf(buffer + n, debug_bufmax - n,
1437 "dec state %d \n", audio->dec_state);
1438 n += scnprintf(buffer + n, debug_bufmax - n,
1439 "out_needed %d \n", audio->out_needed);
1440 n += scnprintf(buffer + n, debug_bufmax - n,
1441 "out_head %d \n", audio->out_head);
1442 n += scnprintf(buffer + n, debug_bufmax - n,
1443 "out_tail %d \n", audio->out_tail);
1444 n += scnprintf(buffer + n, debug_bufmax - n,
1445 "out[0].used %d \n", audio->out[0].used);
1446 n += scnprintf(buffer + n, debug_bufmax - n,
1447 "out[1].used %d \n", audio->out[1].used);
1448 n += scnprintf(buffer + n, debug_bufmax - n,
1449 "buffer_refresh %d \n", audio->buf_refresh);
1450 n += scnprintf(buffer + n, debug_bufmax - n,
1451 "read_next %d \n", audio->read_next);
1452 n += scnprintf(buffer + n, debug_bufmax - n,
1453 "fill_next %d \n", audio->fill_next);
1454 for (i = 0; i < audio->pcm_buf_count; i++)
1455 n += scnprintf(buffer + n, debug_bufmax - n,
1456 "in[%d].size %d \n", i, audio->in[i].used);
1457 buffer[n] = 0;
1458 return simple_read_from_buffer(buf, count, ppos, buffer, n);
1459}
1460
1461static const struct file_operations audevrc_debug_fops = {
1462 .read = audevrc_debug_read,
1463 .open = audevrc_debug_open,
1464};
1465#endif
1466
1467static int audevrc_open(struct inode *inode, struct file *file)
1468{
1469 struct audio *audio = NULL;
1470 int rc, dec_attrb, decid, i;
1471 struct audevrc_event *e_node = NULL;
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301472 unsigned mem_sz = DMASZ;
1473 unsigned long ionflag = 0;
1474 ion_phys_addr_t addr = 0;
1475 struct ion_handle *handle = NULL;
1476 struct ion_client *client = NULL;
1477 int len = 0;
1478
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001479#ifdef CONFIG_DEBUG_FS
1480 /* 4 bytes represents decoder number, 1 byte for terminate string */
1481 char name[sizeof "msm_evrc_" + 5];
1482#endif
1483
1484 /* Allocate audio instance, set to zero */
1485 audio = kzalloc(sizeof(struct audio), GFP_KERNEL);
1486 if (!audio) {
1487 MM_ERR("no memory to allocate audio instance\n");
1488 rc = -ENOMEM;
1489 goto done;
1490 }
1491 MM_INFO("audio instance 0x%08x created\n", (int)audio);
1492
1493 /* Allocate the decoder */
1494 dec_attrb = AUDDEC_DEC_EVRC;
1495 if ((file->f_mode & FMODE_WRITE) &&
1496 (file->f_mode & FMODE_READ)) {
1497 dec_attrb |= MSM_AUD_MODE_NONTUNNEL;
1498 audio->pcm_feedback = NON_TUNNEL_MODE_PLAYBACK;
1499 } else if ((file->f_mode & FMODE_WRITE) &&
1500 !(file->f_mode & FMODE_READ)) {
1501 dec_attrb |= MSM_AUD_MODE_TUNNEL;
1502 audio->pcm_feedback = TUNNEL_MODE_PLAYBACK;
1503 } else {
1504 kfree(audio);
1505 rc = -EACCES;
1506 goto done;
1507 }
1508 decid = audpp_adec_alloc(dec_attrb, &audio->module_name,
1509 &audio->queue_id);
1510
1511 if (decid < 0) {
1512 MM_ERR("No free decoder available, freeing instance 0x%08x\n",
1513 (int)audio);
1514 rc = -ENODEV;
1515 kfree(audio);
1516 goto done;
1517 }
1518
1519 audio->dec_id = decid & MSM_AUD_DECODER_MASK;
1520
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301521 client = msm_ion_client_create(UINT_MAX, "Audio_EVRC_Client");
1522 if (IS_ERR_OR_NULL(client)) {
1523 pr_err("Unable to create ION client\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001524 rc = -ENOMEM;
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301525 goto client_create_error;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001526 }
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301527 audio->client = client;
1528
1529 handle = ion_alloc(client, mem_sz, SZ_4K,
1530 ION_HEAP(ION_AUDIO_HEAP_ID));
1531 if (IS_ERR_OR_NULL(handle)) {
1532 MM_ERR("Unable to create allocate O/P buffers\n");
1533 rc = -ENOMEM;
1534 goto output_buff_alloc_error;
1535 }
1536 audio->output_buff_handle = handle;
1537
1538 rc = ion_phys(client, handle, &addr, &len);
1539 if (rc) {
1540 MM_ERR("O/P buffers:Invalid phy: %x sz: %x\n",
1541 (unsigned int) addr, (unsigned int) len);
1542 goto output_buff_get_phys_error;
1543 } else {
1544 MM_INFO("O/P buffers:valid phy: %x sz: %x\n",
1545 (unsigned int) addr, (unsigned int) len);
1546 }
1547 audio->phys = (int32_t)addr;
1548
1549
1550 rc = ion_handle_get_flags(client, handle, &ionflag);
1551 if (rc) {
1552 MM_ERR("could not get flags for the handle\n");
1553 goto output_buff_get_flags_error;
1554 }
1555
1556 audio->map_v_write = ion_map_kernel(client, handle, ionflag);
1557 if (IS_ERR(audio->map_v_write)) {
1558 MM_ERR("could not map write buffers\n");
1559 rc = -ENOMEM;
1560 goto output_buff_map_error;
1561 }
1562 audio->data = audio->map_v_write;
1563 MM_DBG("write buf: phy addr 0x%08x kernel addr 0x%08x\n",
1564 audio->phys, (int)audio->data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001565
1566 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK) {
1567 rc = audmgr_open(&audio->audmgr);
1568 if (rc) {
1569 MM_ERR("audmgr open failed, freeing instance \
1570 0x%08x\n", (int)audio);
1571 goto err;
1572 }
1573 }
1574
1575 rc = msm_adsp_get(audio->module_name, &audio->audplay,
1576 &audplay_adsp_ops_evrc, audio);
1577
1578 if (rc) {
1579 MM_ERR("failed to get %s module, freeing instance 0x%08x\n",
1580 audio->module_name, (int)audio);
1581 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK)
1582 audmgr_close(&audio->audmgr);
1583 goto err;
1584 }
1585
1586 rc = rmt_get_resource(audio);
1587 if (rc) {
1588 MM_ERR("ADSP resources are not available for EVRC session \
1589 0x%08x on decoder: %d\n", (int)audio, audio->dec_id);
1590 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK)
1591 audmgr_close(&audio->audmgr);
1592 msm_adsp_put(audio->audplay);
1593 goto err;
1594 }
1595
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301596 audio->input_buff_handle = NULL;
1597
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001598 /* Initialize all locks of audio instance */
1599 mutex_init(&audio->lock);
1600 mutex_init(&audio->write_lock);
1601 mutex_init(&audio->read_lock);
1602 mutex_init(&audio->get_event_lock);
1603 spin_lock_init(&audio->dsp_lock);
1604 init_waitqueue_head(&audio->write_wait);
1605 init_waitqueue_head(&audio->read_wait);
1606 INIT_LIST_HEAD(&audio->free_event_queue);
1607 INIT_LIST_HEAD(&audio->event_queue);
1608 init_waitqueue_head(&audio->wait);
1609 init_waitqueue_head(&audio->event_wait);
1610 spin_lock_init(&audio->event_queue_lock);
1611
1612 audio->out[0].data = audio->data + 0;
1613 audio->out[0].addr = audio->phys + 0;
1614 audio->out[0].size = BUFSZ;
1615
1616 audio->out[1].data = audio->data + BUFSZ;
1617 audio->out[1].addr = audio->phys + BUFSZ;
1618 audio->out[1].size = BUFSZ;
1619
1620 audio->vol_pan.volume = 0x3FFF;
1621
1622 audevrc_flush(audio);
1623
1624 file->private_data = audio;
1625 audio->opened = 1;
1626#ifdef CONFIG_DEBUG_FS
1627 snprintf(name, sizeof name, "msm_evrc_%04x", audio->dec_id);
1628 audio->dentry = debugfs_create_file(name, S_IFREG | S_IRUGO,
1629 NULL, (void *) audio, &audevrc_debug_fops);
1630
1631 if (IS_ERR(audio->dentry))
1632 MM_DBG("debugfs_create_file failed\n");
1633#endif
1634#ifdef CONFIG_HAS_EARLYSUSPEND
1635 audio->suspend_ctl.node.level = EARLY_SUSPEND_LEVEL_DISABLE_FB;
1636 audio->suspend_ctl.node.resume = audevrc_resume;
1637 audio->suspend_ctl.node.suspend = audevrc_suspend;
1638 audio->suspend_ctl.audio = audio;
1639 register_early_suspend(&audio->suspend_ctl.node);
1640#endif
1641 for (i = 0; i < AUDEVRC_EVENT_NUM; i++) {
1642 e_node = kmalloc(sizeof(struct audevrc_event), GFP_KERNEL);
1643 if (e_node)
1644 list_add_tail(&e_node->list, &audio->free_event_queue);
1645 else {
1646 MM_ERR("event pkt alloc failed\n");
1647 break;
1648 }
1649 }
1650done:
1651 return rc;
1652err:
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301653 ion_unmap_kernel(client, audio->output_buff_handle);
1654output_buff_map_error:
1655output_buff_get_phys_error:
1656output_buff_get_flags_error:
1657 ion_free(client, audio->output_buff_handle);
1658output_buff_alloc_error:
1659 ion_client_destroy(client);
1660client_create_error:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001661 audpp_adec_free(audio->dec_id);
1662 kfree(audio);
1663 return rc;
1664}
1665
1666static const struct file_operations audio_evrc_fops = {
1667 .owner = THIS_MODULE,
1668 .open = audevrc_open,
1669 .release = audevrc_release,
1670 .read = audevrc_read,
1671 .write = audevrc_write,
1672 .unlocked_ioctl = audevrc_ioctl,
1673 .fsync = audevrc_fsync,
1674};
1675
1676struct miscdevice audio_evrc_misc = {
1677 .minor = MISC_DYNAMIC_MINOR,
1678 .name = "msm_evrc",
1679 .fops = &audio_evrc_fops,
1680};
1681
1682static int __init audevrc_init(void)
1683{
1684 return misc_register(&audio_evrc_misc);
1685
1686}
1687
1688static void __exit audevrc_exit(void)
1689{
1690 misc_deregister(&audio_evrc_misc);
1691}
1692
1693module_init(audevrc_init);
1694module_exit(audevrc_exit);
1695
1696MODULE_DESCRIPTION("MSM EVRC driver");
1697MODULE_LICENSE("GPL v2");