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