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