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