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