PageRenderTime 177ms CodeModel.GetById 6ms app.highlight 158ms RepoModel.GetById 1ms app.codeStats 0ms

/arch/arm/mach-msm/qdsp5/audio_amrwb.c

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