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