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