PageRenderTime 161ms CodeModel.GetById 21ms app.highlight 125ms RepoModel.GetById 1ms app.codeStats 0ms

/arch/arm/mach-fsm/qdsp5v2/audio_amrwb.c

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