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