PageRenderTime 79ms CodeModel.GetById 16ms app.highlight 50ms RepoModel.GetById 1ms app.codeStats 0ms

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

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