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