blob: 427dc8f5e0c4bfcb705e9015c2a5db330d18f4cd [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* arch/arm/mach-msm/qdsp5/audio_mp3.c
2 *
3 * mp3 audio output device
4 *
5 * Copyright (C) 2008 Google, Inc.
6 * Copyright (C) 2008 HTC Corporation
Manish Dewangana4f1df02012-02-08 17:06:54 +05307 * Copyright (c) 2009-2012, Code Aurora Forum. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07008 *
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. See the
16 * GNU General Public License for more details.
17 *
18 */
19
Santosh Mardi0be3b8e2011-07-06 10:00:21 +053020#include <asm/atomic.h>
21#include <asm/ioctls.h>
22
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070023#include <linux/module.h>
24#include <linux/fs.h>
25#include <linux/miscdevice.h>
26#include <linux/uaccess.h>
27#include <linux/kthread.h>
28#include <linux/wait.h>
29#include <linux/dma-mapping.h>
30#include <linux/debugfs.h>
31#include <linux/delay.h>
32#include <linux/earlysuspend.h>
33#include <linux/list.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070034#include <linux/slab.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070035#include <linux/msm_audio.h>
Santosh Mardi0be3b8e2011-07-06 10:00:21 +053036#include <linux/memory_alloc.h>
Sidipotu Ashok172c98b2012-06-26 17:58:29 +053037#include <linux/ion.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070038
Santosh Mardi0be3b8e2011-07-06 10:00:21 +053039#include <mach/msm_adsp.h>
40#include <mach/iommu.h>
41#include <mach/iommu_domains.h>
Santosh Mardi0be3b8e2011-07-06 10:00:21 +053042#include <mach/msm_memtypes.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070043#include <mach/qdsp5/qdsp5audppcmdi.h>
44#include <mach/qdsp5/qdsp5audppmsg.h>
45#include <mach/qdsp5/qdsp5audplaycmdi.h>
46#include <mach/qdsp5/qdsp5audplaymsg.h>
47#include <mach/qdsp5/qdsp5rmtcmdi.h>
48#include <mach/debug_mm.h>
49
Santosh Mardi0be3b8e2011-07-06 10:00:21 +053050#include "audmgr.h"
51
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070052#define ADRV_STATUS_AIO_INTF 0x00000001
53#define ADRV_STATUS_OBUF_GIVEN 0x00000002
54#define ADRV_STATUS_IBUF_GIVEN 0x00000004
55#define ADRV_STATUS_FSYNC 0x00000008
56
57/* Size must be power of 2 */
58#define BUFSZ_MAX 32768
59#define BUFSZ_MIN 4096
60#define DMASZ_MAX (BUFSZ_MAX * 2)
61#define DMASZ_MIN (BUFSZ_MIN * 2)
62
63#define AUDPLAY_INVALID_READ_PTR_OFFSET 0xFFFF
64#define AUDDEC_DEC_MP3 2
65
66#define PCM_BUFSZ_MIN 4800 /* Hold one stereo MP3 frame */
67#define PCM_BUF_MAX_COUNT 5 /* DSP only accepts 5 buffers at most
68 but support 2 buffers currently */
69#define ROUTING_MODE_FTRT 1
70#define ROUTING_MODE_RT 2
71/* Decoder status received from AUDPPTASK */
72#define AUDPP_DEC_STATUS_SLEEP 0
73#define AUDPP_DEC_STATUS_INIT 1
74#define AUDPP_DEC_STATUS_CFG 2
75#define AUDPP_DEC_STATUS_PLAY 3
76
77#define AUDMP3_METAFIELD_MASK 0xFFFF0000
78#define AUDMP3_EOS_FLG_OFFSET 0x0A /* Offset from beginning of buffer */
79#define AUDMP3_EOS_FLG_MASK 0x01
80#define AUDMP3_EOS_NONE 0x0 /* No EOS detected */
81#define AUDMP3_EOS_SET 0x1 /* EOS set in meta field */
82
83#define AUDMP3_EVENT_NUM 10 /* Default number of pre-allocated event packets */
84
85#define __CONTAINS(r, v, l) ({ \
86 typeof(r) __r = r; \
87 typeof(v) __v = v; \
88 typeof(v) __e = __v + l; \
89 int res = ((__v >= __r->vaddr) && \
90 (__e <= __r->vaddr + __r->len)); \
91 res; \
92})
93
94#define CONTAINS(r1, r2) ({ \
95 typeof(r2) __r2 = r2; \
96 __CONTAINS(r1, __r2->vaddr, __r2->len); \
97})
98
99#define IN_RANGE(r, v) ({ \
100 typeof(r) __r = r; \
101 typeof(v) __vv = v; \
102 int res = ((__vv >= __r->vaddr) && \
103 (__vv < (__r->vaddr + __r->len))); \
104 res; \
105})
106
107#define OVERLAPS(r1, r2) ({ \
108 typeof(r1) __r1 = r1; \
109 typeof(r2) __r2 = r2; \
110 typeof(__r2->vaddr) __v = __r2->vaddr; \
111 typeof(__v) __e = __v + __r2->len - 1; \
112 int res = (IN_RANGE(__r1, __v) || IN_RANGE(__r1, __e)); \
113 res; \
114})
115
116struct audio;
117
118struct buffer {
119 void *data;
120 unsigned size;
121 unsigned used; /* Input usage actual DSP produced PCM size */
122 unsigned addr;
123 unsigned short mfield_sz; /*only useful for data has meta field */
124};
125
126#ifdef CONFIG_HAS_EARLYSUSPEND
127struct audmp3_suspend_ctl {
128 struct early_suspend node;
129 struct audio *audio;
130};
131#endif
132
133struct audmp3_event {
134 struct list_head list;
135 int event_type;
136 union msm_audio_event_payload payload;
137};
138
Sidipotu Ashok172c98b2012-06-26 17:58:29 +0530139struct audmp3_ion_region {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700140 struct list_head list;
Sidipotu Ashok172c98b2012-06-26 17:58:29 +0530141 struct ion_handle *handle;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700142 int fd;
143 void *vaddr;
144 unsigned long paddr;
145 unsigned long kvaddr;
146 unsigned long len;
147 unsigned ref_cnt;
148};
149
150struct audmp3_buffer_node {
151 struct list_head list;
152 struct msm_audio_aio_buf buf;
153 unsigned long paddr;
154};
155
156struct audmp3_drv_operations {
157 void (*pcm_buf_update)(struct audio *, uint32_t *);
158 void (*buffer_refresh)(struct audio *);
159 void (*send_data)(struct audio *, unsigned);
160 void (*out_flush)(struct audio *);
161 void (*in_flush)(struct audio *);
162 int (*fsync)(struct audio *);
163};
164
165struct audio {
166 struct buffer out[2];
167
168 spinlock_t dsp_lock;
169
170 uint8_t out_head;
171 uint8_t out_tail;
172 uint8_t out_needed; /* number of buffers the dsp is waiting for */
173 unsigned out_dma_sz;
174 struct list_head out_queue; /* queue to retain output buffers */
175 atomic_t out_bytes;
176
177 struct mutex lock;
178 struct mutex write_lock;
179 wait_queue_head_t write_wait;
180
181 /* Host PCM section */
182 struct buffer in[PCM_BUF_MAX_COUNT];
183 struct mutex read_lock;
184 wait_queue_head_t read_wait; /* Wait queue for read */
185 char *read_data; /* pointer to reader buffer */
186 int32_t read_phys; /* physical address of reader buffer */
187 uint8_t read_next; /* index to input buffers to be read next */
188 uint8_t fill_next; /* index to buffer that DSP should be filling */
189 uint8_t pcm_buf_count; /* number of pcm buffer allocated */
190 struct list_head in_queue; /* queue to retain input buffers */
191 /* ---- End of Host PCM section */
192
193 struct msm_adsp_module *audplay;
194
195 /* configuration to use on next enable */
196 uint32_t out_sample_rate;
197 uint32_t out_channel_mode;
198
199 struct audmgr audmgr;
200
201 /* data allocated for various buffers */
202 char *data;
203 int32_t phys; /* physical address of write buffer */
Laura Abbott35111d32012-04-27 18:41:48 -0700204 void *map_v_read;
205 void *map_v_write;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700206
207 uint32_t drv_status;
208 int mfield; /* meta field embedded in data */
209 int rflush; /* Read flush */
210 int wflush; /* Write flush */
211 int opened;
212 int enabled;
213 int running;
214 int stopped; /* set when stopped, cleared on flush */
215 int pcm_feedback;
216 int buf_refresh;
217 int rmt_resource_released;
218 int teos; /* valid only if tunnel mode & no data left for decoder */
219 enum msm_aud_decoder_state dec_state; /* Represents decoder state */
220 int reserved; /* A byte is being reserved */
221 char rsv_byte; /* Handle odd length user data */
222
223 const char *module_name;
224 unsigned queue_id;
225 uint16_t dec_id;
226 uint32_t read_ptr_offset;
227
228#ifdef CONFIG_HAS_EARLYSUSPEND
229 struct audmp3_suspend_ctl suspend_ctl;
230#endif
231
232#ifdef CONFIG_DEBUG_FS
233 struct dentry *dentry;
234#endif
235
236 wait_queue_head_t wait;
237 struct list_head free_event_queue;
238 struct list_head event_queue;
239 wait_queue_head_t event_wait;
240 spinlock_t event_queue_lock;
241 struct mutex get_event_lock;
242 int event_abort;
243
Sidipotu Ashok172c98b2012-06-26 17:58:29 +0530244 struct list_head ion_region_queue; /* protected by lock */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700245 struct audmp3_drv_operations drv_ops;
246
247 int eq_enable;
248 int eq_needs_commit;
249 audpp_cmd_cfg_object_params_eqalizer eq;
250 audpp_cmd_cfg_object_params_volume vol_pan;
Sidipotu Ashok172c98b2012-06-26 17:58:29 +0530251 struct ion_client *client;
252 struct ion_handle *input_buff_handle;
253 struct ion_handle *output_buff_handle;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700254};
255
256static int auddec_dsp_config(struct audio *audio, int enable);
257static void audpp_cmd_cfg_adec_params(struct audio *audio);
258static void audpp_cmd_cfg_routing_mode(struct audio *audio);
259static void audplay_send_data(struct audio *audio, unsigned needed);
260static void audplay_config_hostpcm(struct audio *audio);
261static void audplay_buffer_refresh(struct audio *audio);
262static void audio_dsp_event(void *private, unsigned id, uint16_t *msg);
263static void audmp3_post_event(struct audio *audio, int type,
264 union msm_audio_event_payload payload);
Sidipotu Ashok172c98b2012-06-26 17:58:29 +0530265static unsigned long audmp3_ion_fixup(struct audio *audio, void *addr,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700266 unsigned long len, int ref_up);
267
268static int rmt_put_resource(struct audio *audio)
269{
270 struct aud_codec_config_cmd cmd;
271 unsigned short client_idx;
272
273 cmd.cmd_id = RM_CMD_AUD_CODEC_CFG;
274 cmd.client_id = RM_AUD_CLIENT_ID;
275 cmd.task_id = audio->dec_id;
276 cmd.enable = RMT_DISABLE;
277 cmd.dec_type = AUDDEC_DEC_MP3;
278 client_idx = ((cmd.client_id << 8) | cmd.task_id);
279
280 return put_adsp_resource(client_idx, &cmd, sizeof(cmd));
281}
282
283static int rmt_get_resource(struct audio *audio)
284{
285 struct aud_codec_config_cmd cmd;
286 unsigned short client_idx;
287
288 cmd.cmd_id = RM_CMD_AUD_CODEC_CFG;
289 cmd.client_id = RM_AUD_CLIENT_ID;
290 cmd.task_id = audio->dec_id;
291 cmd.enable = RMT_ENABLE;
292 cmd.dec_type = AUDDEC_DEC_MP3;
293 client_idx = ((cmd.client_id << 8) | cmd.task_id);
294
295 return get_adsp_resource(client_idx, &cmd, sizeof(cmd));
296}
297
298/* must be called with audio->lock held */
299static int audio_enable(struct audio *audio)
300{
301 struct audmgr_config cfg;
302 int rc;
303
304 MM_DBG("\n"); /* Macro prints the file name and function */
305
306 if (audio->enabled)
307 return 0;
308
309 if (audio->rmt_resource_released == 1) {
310 audio->rmt_resource_released = 0;
311 rc = rmt_get_resource(audio);
312 if (rc) {
313 MM_ERR("ADSP resources are not available for MP3 \
314 session 0x%08x on decoder: %d\n Ignoring \
315 error and going ahead with the playback\n",
316 (int)audio, audio->dec_id);
317 }
318 }
319
320 audio->dec_state = MSM_AUD_DECODER_STATE_NONE;
321 audio->out_tail = 0;
322 audio->out_needed = 0;
323
324 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK) {
325 cfg.tx_rate = RPC_AUD_DEF_SAMPLE_RATE_NONE;
326 cfg.rx_rate = RPC_AUD_DEF_SAMPLE_RATE_48000;
327 cfg.def_method = RPC_AUD_DEF_METHOD_PLAYBACK;
328 cfg.codec = RPC_AUD_DEF_CODEC_MP3;
329 cfg.snd_method = RPC_SND_METHOD_MIDI;
330
331 rc = audmgr_enable(&audio->audmgr, &cfg);
332 if (rc < 0)
333 return rc;
334 }
335
336 if (msm_adsp_enable(audio->audplay)) {
337 MM_ERR("msm_adsp_enable(audplay) failed\n");
338 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK)
339 audmgr_disable(&audio->audmgr);
340 return -ENODEV;
341 }
342
343 if (audpp_enable(audio->dec_id, audio_dsp_event, audio)) {
344 MM_ERR("audpp_enable() failed\n");
345 msm_adsp_disable(audio->audplay);
346 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK)
347 audmgr_disable(&audio->audmgr);
348 return -ENODEV;
349 }
350
351 audio->enabled = 1;
352 return 0;
353}
354
355/* must be called with audio->lock held */
356static int audio_disable(struct audio *audio)
357{
358 int rc = 0;
359 MM_DBG("\n"); /* Macro prints the file name and function */
360 if (audio->enabled) {
361 audio->enabled = 0;
362 audio->dec_state = MSM_AUD_DECODER_STATE_NONE;
363 auddec_dsp_config(audio, 0);
364 rc = wait_event_interruptible_timeout(audio->wait,
365 audio->dec_state != MSM_AUD_DECODER_STATE_NONE,
366 msecs_to_jiffies(MSM_AUD_DECODER_WAIT_MS));
367 if (rc == 0)
368 rc = -ETIMEDOUT;
369 else if (audio->dec_state != MSM_AUD_DECODER_STATE_CLOSE)
370 rc = -EFAULT;
371 else
372 rc = 0;
Manish Dewangan89a9f232012-02-09 17:14:40 +0530373 audio->stopped = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700374 wake_up(&audio->write_wait);
375 wake_up(&audio->read_wait);
376 msm_adsp_disable(audio->audplay);
377 audpp_disable(audio->dec_id, audio);
378 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK)
379 audmgr_disable(&audio->audmgr);
380 audio->out_needed = 0;
381 rmt_put_resource(audio);
382 audio->rmt_resource_released = 1;
383 }
384 return rc;
385}
386
387/* ------------------- dsp --------------------- */
388static void audmp3_async_pcm_buf_update(struct audio *audio, uint32_t *payload)
389{
390 unsigned long flags;
391 union msm_audio_event_payload event_payload;
392 struct audmp3_buffer_node *filled_buf;
393 uint8_t index;
394
395 if (audio->rflush)
396 return;
397
398 spin_lock_irqsave(&audio->dsp_lock, flags);
399 for (index = 0; index < payload[1]; index++) {
400 BUG_ON(list_empty(&audio->in_queue));
401 filled_buf = list_first_entry(&audio->in_queue,
402 struct audmp3_buffer_node, list);
403 if (filled_buf->paddr == payload[2 + index * 2]) {
404 list_del(&filled_buf->list);
405 event_payload.aio_buf = filled_buf->buf;
406 event_payload.aio_buf.data_len =
407 payload[3 + index * 2];
408 MM_DBG("pcm buf %p data_len %d\n", filled_buf,
409 event_payload.aio_buf.data_len);
410 audmp3_post_event(audio, AUDIO_EVENT_READ_DONE,
411 event_payload);
412 kfree(filled_buf);
413 } else {
414 MM_ERR("expected=%lx ret=%x\n", filled_buf->paddr,
415 payload[2 + index * 2]);
416 break;
417 }
418 }
419
420 audio->drv_status &= ~ADRV_STATUS_IBUF_GIVEN;
421 audio->drv_ops.buffer_refresh(audio);
422 spin_unlock_irqrestore(&audio->dsp_lock, flags);
423
424}
425
426static void audio_update_pcm_buf_entry(struct audio *audio, uint32_t *payload)
427{
428 uint8_t index;
429 unsigned long flags;
430
431 if (audio->rflush)
432 return;
433
434 spin_lock_irqsave(&audio->dsp_lock, flags);
435 for (index = 0; index < payload[1]; index++) {
436 if (audio->in[audio->fill_next].addr ==
437 payload[2 + index * 2]) {
438 MM_DBG("in[%d] ready\n", audio->fill_next);
439 audio->in[audio->fill_next].used =
440 payload[3 + index * 2];
441 if ((++audio->fill_next) == audio->pcm_buf_count)
442 audio->fill_next = 0;
443
444 } else {
445 MM_ERR("expected=%x ret=%x\n",
446 audio->in[audio->fill_next].addr,
447 payload[2 + index * 2]);
448 break;
449 }
450 }
451 if (audio->in[audio->fill_next].used == 0) {
452 audio->drv_ops.buffer_refresh(audio);
453 } else {
454 MM_DBG("read cannot keep up\n");
455 audio->buf_refresh = 1;
456 }
457 wake_up(&audio->read_wait);
458 spin_unlock_irqrestore(&audio->dsp_lock, flags);
459
460}
461
462static void audplay_dsp_event(void *data, unsigned id, size_t len,
463 void (*getevent) (void *ptr, size_t len))
464{
465 struct audio *audio = data;
466 uint32_t msg[28];
467 getevent(msg, sizeof(msg));
468
469 MM_DBG("msg_id=%x\n", id);
470
471 switch (id) {
472 case AUDPLAY_MSG_DEC_NEEDS_DATA:
473 audio->drv_ops.send_data(audio, 1);
474 break;
475
476 case AUDPLAY_MSG_BUFFER_UPDATE:
477 audio->drv_ops.pcm_buf_update(audio, msg);
478 break;
479
480 case ADSP_MESSAGE_ID:
481 MM_DBG("Received ADSP event: module enable(audplaytask)\n");
482 break;
483
484 default:
485 MM_ERR("unexpected message from decoder \n");
486 break;
487 }
488}
489
490static void audio_dsp_event(void *private, unsigned id, uint16_t *msg)
491{
492 struct audio *audio = private;
493
494 switch (id) {
495 case AUDPP_MSG_STATUS_MSG:{
496 unsigned status = msg[1];
497
498 switch (status) {
499 case AUDPP_DEC_STATUS_SLEEP: {
500 uint16_t reason = msg[2];
501 MM_DBG("decoder status: sleep reason=0x%04x\n",
502 reason);
503 if ((reason == AUDPP_MSG_REASON_MEM)
504 || (reason ==
505 AUDPP_MSG_REASON_NODECODER)) {
506 audio->dec_state =
507 MSM_AUD_DECODER_STATE_FAILURE;
508 wake_up(&audio->wait);
509 } else if (reason == AUDPP_MSG_REASON_NONE) {
510 /* decoder is in disable state */
511 audio->dec_state =
512 MSM_AUD_DECODER_STATE_CLOSE;
513 wake_up(&audio->wait);
514 }
515 break;
516 }
517 case AUDPP_DEC_STATUS_INIT:
518 MM_DBG("decoder status: init \n");
519 if (audio->pcm_feedback)
520 audpp_cmd_cfg_routing_mode(audio);
521 else
522 audpp_cmd_cfg_adec_params(audio);
523 break;
524
525 case AUDPP_DEC_STATUS_CFG:
526 MM_DBG("decoder status: cfg \n");
527 break;
528 case AUDPP_DEC_STATUS_PLAY:
529 MM_DBG("decoder status: play \n");
530 if (audio->pcm_feedback) {
531 audplay_config_hostpcm(audio);
532 audio->drv_ops.buffer_refresh(audio);
533 }
534 audio->dec_state =
535 MSM_AUD_DECODER_STATE_SUCCESS;
536 wake_up(&audio->wait);
537 break;
538 default:
539 MM_ERR("unknown decoder status \n");
540 break;
541 }
542 break;
543 }
544 case AUDPP_MSG_CFG_MSG:
545 if (msg[0] == AUDPP_MSG_ENA_ENA) {
546 MM_DBG("CFG_MSG ENABLE\n");
547 auddec_dsp_config(audio, 1);
548 audio->out_needed = 0;
549 audio->running = 1;
550 audpp_dsp_set_vol_pan(audio->dec_id, &audio->vol_pan);
551 audpp_dsp_set_eq(audio->dec_id, audio->eq_enable,
552 &audio->eq);
553 audpp_avsync(audio->dec_id, 22050);
554 } else if (msg[0] == AUDPP_MSG_ENA_DIS) {
555 MM_DBG("CFG_MSG DISABLE\n");
556 audpp_avsync(audio->dec_id, 0);
557 audio->running = 0;
558 } else {
559 MM_DBG("CFG_MSG %d?\n", msg[0]);
560 }
561 break;
562 case AUDPP_MSG_ROUTING_ACK:
563 MM_DBG("ROUTING_ACK mode=%d\n", msg[1]);
564 audpp_cmd_cfg_adec_params(audio);
565 break;
566
567 case AUDPP_MSG_FLUSH_ACK:
568 MM_DBG("FLUSH_ACK\n");
569 audio->wflush = 0;
570 audio->rflush = 0;
571 wake_up(&audio->write_wait);
572 if (audio->pcm_feedback)
573 audio->drv_ops.buffer_refresh(audio);
574 break;
575
576 case AUDPP_MSG_PCMDMAMISSED:
577 MM_DBG("PCMDMAMISSED\n");
578 audio->teos = 1;
579 wake_up(&audio->write_wait);
580 break;
581
582 default:
583 MM_ERR("UNKNOWN (%d)\n", id);
584 }
585
586}
587
588
589struct msm_adsp_ops audplay_adsp_ops = {
590 .event = audplay_dsp_event,
591};
592
593
594#define audplay_send_queue0(audio, cmd, len) \
595 msm_adsp_write(audio->audplay, audio->queue_id, \
596 cmd, len)
597
598static int auddec_dsp_config(struct audio *audio, int enable)
599{
600 u16 cfg_dec_cmd[AUDPP_CMD_CFG_DEC_TYPE_LEN / sizeof(unsigned short)];
601
602 memset(cfg_dec_cmd, 0, sizeof(cfg_dec_cmd));
603
604 cfg_dec_cmd[0] = AUDPP_CMD_CFG_DEC_TYPE;
605 if (enable)
606 cfg_dec_cmd[1 + audio->dec_id] = AUDPP_CMD_UPDATDE_CFG_DEC |
607 AUDPP_CMD_ENA_DEC_V | AUDDEC_DEC_MP3;
608 else
609 cfg_dec_cmd[1 + audio->dec_id] = AUDPP_CMD_UPDATDE_CFG_DEC |
610 AUDPP_CMD_DIS_DEC_V;
611
612 return audpp_send_queue1(&cfg_dec_cmd, sizeof(cfg_dec_cmd));
613}
614
615static void audpp_cmd_cfg_adec_params(struct audio *audio)
616{
617 audpp_cmd_cfg_adec_params_mp3 cmd;
618
619 memset(&cmd, 0, sizeof(cmd));
620 cmd.common.cmd_id = AUDPP_CMD_CFG_ADEC_PARAMS;
621 cmd.common.length = AUDPP_CMD_CFG_ADEC_PARAMS_MP3_LEN;
622 cmd.common.dec_id = audio->dec_id;
623 cmd.common.input_sampling_frequency = audio->out_sample_rate;
624
625 audpp_send_queue2(&cmd, sizeof(cmd));
626}
627
628static void audpp_cmd_cfg_routing_mode(struct audio *audio)
629{
630 struct audpp_cmd_routing_mode cmd;
631 MM_DBG("\n"); /* Macro prints the file name and function */
632 memset(&cmd, 0, sizeof(cmd));
633 cmd.cmd_id = AUDPP_CMD_ROUTING_MODE;
634 cmd.object_number = audio->dec_id;
635 if (audio->pcm_feedback)
636 cmd.routing_mode = ROUTING_MODE_FTRT;
637 else
638 cmd.routing_mode = ROUTING_MODE_RT;
639
640 audpp_send_queue1(&cmd, sizeof(cmd));
641}
642
643static int audplay_dsp_send_data_avail(struct audio *audio,
644 unsigned idx, unsigned len)
645{
646 struct audplay_cmd_bitstream_data_avail_nt2 cmd;
647
648 cmd.cmd_id = AUDPLAY_CMD_BITSTREAM_DATA_AVAIL_NT2;
649 if (audio->mfield)
650 cmd.decoder_id = AUDMP3_METAFIELD_MASK |
651 (audio->out[idx].mfield_sz >> 1);
652 else
653 cmd.decoder_id = audio->dec_id;
654 cmd.buf_ptr = audio->out[idx].addr;
655 cmd.buf_size = len/2;
656 cmd.partition_number = 0;
657 /* complete all the writes to the input buffer */
658 wmb();
659 return audplay_send_queue0(audio, &cmd, sizeof(cmd));
660}
661
662/* Caller holds irq_lock */
663static void audmp3_async_buffer_refresh(struct audio *audio)
664{
665 struct audplay_cmd_buffer_refresh refresh_cmd;
666 struct audmp3_buffer_node *next_buf;
667
668 if (!audio->running ||
669 audio->drv_status & ADRV_STATUS_IBUF_GIVEN)
670 return;
671
672 if (!list_empty(&audio->in_queue)) {
673 next_buf = list_first_entry(&audio->in_queue,
674 struct audmp3_buffer_node, list);
675 if (!next_buf)
676 return;
677 MM_DBG("next buf %p phy %lx len %d\n", next_buf,
678 next_buf->paddr, next_buf->buf.buf_len);
679 refresh_cmd.cmd_id = AUDPLAY_CMD_BUFFER_REFRESH;
680 refresh_cmd.num_buffers = 1;
681 refresh_cmd.buf0_address = next_buf->paddr;
682 refresh_cmd.buf0_length = next_buf->buf.buf_len -
683 (next_buf->buf.buf_len % 576) +
684 (audio->mfield ? 24 : 0); /* Mp3 frame size */
685 refresh_cmd.buf_read_count = 0;
686 audio->drv_status |= ADRV_STATUS_IBUF_GIVEN;
687 (void) audplay_send_queue0(audio, &refresh_cmd,
688 sizeof(refresh_cmd));
689 }
690
691}
692
693static void audplay_buffer_refresh(struct audio *audio)
694{
695 struct audplay_cmd_buffer_refresh refresh_cmd;
696
697 refresh_cmd.cmd_id = AUDPLAY_CMD_BUFFER_REFRESH;
698 refresh_cmd.num_buffers = 1;
699 refresh_cmd.buf0_address = audio->in[audio->fill_next].addr;
700 refresh_cmd.buf0_length = audio->in[audio->fill_next].size -
701 (audio->in[audio->fill_next].size % 576) +
702 (audio->mfield ? 24 : 0); /* Mp3 frame size */
703 refresh_cmd.buf_read_count = 0;
704 MM_DBG("buf0_addr=%x buf0_len=%d\n", refresh_cmd.buf0_address,
705 refresh_cmd.buf0_length);
706 (void)audplay_send_queue0(audio, &refresh_cmd, sizeof(refresh_cmd));
707}
708
709static void audplay_config_hostpcm(struct audio *audio)
710{
711 struct audplay_cmd_hpcm_buf_cfg cfg_cmd;
712
713 MM_DBG("\n"); /* Macro prints the file name and function */
714 cfg_cmd.cmd_id = AUDPLAY_CMD_HPCM_BUF_CFG;
715 cfg_cmd.max_buffers = 1;
716 cfg_cmd.byte_swap = 0;
717 cfg_cmd.hostpcm_config = (0x8000) | (0x4000);
718 cfg_cmd.feedback_frequency = 1;
719 cfg_cmd.partition_number = 0;
720 (void)audplay_send_queue0(audio, &cfg_cmd, sizeof(cfg_cmd));
721
722}
723
724static void audmp3_async_send_data(struct audio *audio, unsigned needed)
725{
726 unsigned long flags;
727
728 spin_lock_irqsave(&audio->dsp_lock, flags);
729 if (!audio->running)
730 goto done;
731
732 if (needed && !audio->wflush) {
733 audio->out_needed = 1;
734 if (audio->drv_status & ADRV_STATUS_OBUF_GIVEN) {
735 /* pop one node out of queue */
736 union msm_audio_event_payload payload;
737 struct audmp3_buffer_node *used_buf;
738
739 MM_DBG("consumed\n");
740 BUG_ON(list_empty(&audio->out_queue));
741 used_buf = list_first_entry(&audio->out_queue,
742 struct audmp3_buffer_node, list);
743 list_del(&used_buf->list);
744 payload.aio_buf = used_buf->buf;
745 audmp3_post_event(audio, AUDIO_EVENT_WRITE_DONE,
746 payload);
747 kfree(used_buf);
748 audio->drv_status &= ~ADRV_STATUS_OBUF_GIVEN;
749 }
750
751 }
752
753 if (audio->out_needed) {
754 struct audmp3_buffer_node *next_buf;
755 struct audplay_cmd_bitstream_data_avail_nt2 cmd;
756 if (!list_empty(&audio->out_queue)) {
757 next_buf = list_first_entry(&audio->out_queue,
758 struct audmp3_buffer_node, list);
759 MM_DBG("next_buf %p\n", next_buf);
760 if (next_buf) {
761 MM_DBG("next buf phy %lx len %d\n",
762 next_buf->paddr,
763 next_buf->buf.data_len);
764
765 cmd.cmd_id =
766 AUDPLAY_CMD_BITSTREAM_DATA_AVAIL_NT2;
767 if (audio->mfield)
768 cmd.decoder_id = AUDMP3_METAFIELD_MASK |
769 (next_buf->buf.mfield_sz >> 1);
770 else
771 cmd.decoder_id = audio->dec_id;
772 cmd.buf_ptr = (unsigned) next_buf->paddr;
773 cmd.buf_size = next_buf->buf.data_len >> 1;
774 cmd.partition_number = 0;
775 /* complete the writes to the input buffer */
776 wmb();
777 audplay_send_queue0(audio, &cmd, sizeof(cmd));
778 audio->out_needed = 0;
779 audio->drv_status |= ADRV_STATUS_OBUF_GIVEN;
780 }
781 }
782 }
783
784done:
785 spin_unlock_irqrestore(&audio->dsp_lock, flags);
786}
787
788static void audplay_send_data(struct audio *audio, unsigned needed)
789{
790 struct buffer *frame;
791 unsigned long flags;
792
793 spin_lock_irqsave(&audio->dsp_lock, flags);
794 if (!audio->running)
795 goto done;
796
797 if (needed && !audio->wflush) {
798 /* We were called from the callback because the DSP
799 * requested more data. Note that the DSP does want
800 * more data, and if a buffer was in-flight, mark it
801 * as available (since the DSP must now be done with
802 * it).
803 */
804 audio->out_needed = 1;
805 frame = audio->out + audio->out_tail;
806 if (frame->used == 0xffffffff) {
807 MM_DBG("frame %d free\n", audio->out_tail);
808 frame->used = 0;
809 audio->out_tail ^= 1;
810 wake_up(&audio->write_wait);
811 }
812 }
813
814 if (audio->out_needed) {
815 /* If the DSP currently wants data and we have a
816 * buffer available, we will send it and reset
817 * the needed flag. We'll mark the buffer as in-flight
818 * so that it won't be recycled until the next buffer
819 * is requested
820 */
821
822 frame = audio->out + audio->out_tail;
823 if (frame->used) {
824 BUG_ON(frame->used == 0xffffffff);
825 MM_DBG("frame %d busy\n", audio->out_tail);
826 audplay_dsp_send_data_avail(audio, audio->out_tail,
827 frame->used);
828 frame->used = 0xffffffff;
829 audio->out_needed = 0;
830 }
831 }
832done:
833 spin_unlock_irqrestore(&audio->dsp_lock, flags);
834}
835
836/* ------------------- device --------------------- */
837static void audmp3_async_flush(struct audio *audio)
838{
839 struct audmp3_buffer_node *buf_node;
840 struct list_head *ptr, *next;
841 union msm_audio_event_payload payload;
Chaithanya Krishna Bacharaju221f7992012-05-16 09:32:54 +0530842 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700843
Chaithanya Krishna Bacharaju221f7992012-05-16 09:32:54 +0530844 spin_lock_irqsave(&audio->dsp_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700845 MM_DBG("\n"); /* Macro prints the file name and function */
846 list_for_each_safe(ptr, next, &audio->out_queue) {
847 buf_node = list_entry(ptr, struct audmp3_buffer_node, list);
848 list_del(&buf_node->list);
849 payload.aio_buf = buf_node->buf;
850 audmp3_post_event(audio, AUDIO_EVENT_WRITE_DONE,
851 payload);
852 kfree(buf_node);
853 }
854 audio->drv_status &= ~ADRV_STATUS_OBUF_GIVEN;
855 audio->out_needed = 0;
856 atomic_set(&audio->out_bytes, 0);
Chaithanya Krishna Bacharaju221f7992012-05-16 09:32:54 +0530857 spin_unlock_irqrestore(&audio->dsp_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700858}
859
860static void audio_flush(struct audio *audio)
861{
Manish Dewangana4f1df02012-02-08 17:06:54 +0530862 unsigned long flags;
863
864 spin_lock_irqsave(&audio->dsp_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700865 audio->out[0].used = 0;
866 audio->out[1].used = 0;
867 audio->out_head = 0;
868 audio->out_tail = 0;
869 audio->reserved = 0;
870 audio->out_needed = 0;
Manish Dewangana4f1df02012-02-08 17:06:54 +0530871 spin_unlock_irqrestore(&audio->dsp_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700872 atomic_set(&audio->out_bytes, 0);
873}
874
875static void audmp3_async_flush_pcm_buf(struct audio *audio)
876{
877 struct audmp3_buffer_node *buf_node;
878 struct list_head *ptr, *next;
879 union msm_audio_event_payload payload;
880
881 MM_DBG("\n"); /* Macro prints the file name and function */
882 list_for_each_safe(ptr, next, &audio->in_queue) {
883 buf_node = list_entry(ptr, struct audmp3_buffer_node, list);
884 list_del(&buf_node->list);
885 payload.aio_buf = buf_node->buf;
886 payload.aio_buf.data_len = 0;
887 audmp3_post_event(audio, AUDIO_EVENT_READ_DONE,
888 payload);
889 kfree(buf_node);
890 }
891 audio->drv_status &= ~ADRV_STATUS_IBUF_GIVEN;
892
893}
894
895static void audio_flush_pcm_buf(struct audio *audio)
896{
897 uint8_t index;
Manish Dewangana4f1df02012-02-08 17:06:54 +0530898 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700899
Manish Dewangana4f1df02012-02-08 17:06:54 +0530900 spin_lock_irqsave(&audio->dsp_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700901 for (index = 0; index < PCM_BUF_MAX_COUNT; index++)
902 audio->in[index].used = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700903 audio->buf_refresh = 0;
904 audio->read_next = 0;
905 audio->fill_next = 0;
Manish Dewangana4f1df02012-02-08 17:06:54 +0530906 spin_unlock_irqrestore(&audio->dsp_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700907}
908
909static void audio_ioport_reset(struct audio *audio)
910{
911 if (audio->drv_status & ADRV_STATUS_AIO_INTF) {
912 /* If fsync is in progress, make sure
913 * return value of fsync indicates
914 * abort due to flush
915 */
916 if (audio->drv_status & ADRV_STATUS_FSYNC) {
917 MM_DBG("fsync in progress\n");
918 wake_up(&audio->write_wait);
919 mutex_lock(&audio->write_lock);
920 audio->drv_ops.out_flush(audio);
921 mutex_unlock(&audio->write_lock);
922 } else
923 audio->drv_ops.out_flush(audio);
924 audio->drv_ops.in_flush(audio);
925 } else {
926 /* Make sure read/write thread are free from
927 * sleep and knowing that system is not able
928 * to process io request at the moment
929 */
930 wake_up(&audio->write_wait);
931 mutex_lock(&audio->write_lock);
932 audio->drv_ops.out_flush(audio);
933 mutex_unlock(&audio->write_lock);
934 wake_up(&audio->read_wait);
935 mutex_lock(&audio->read_lock);
936 audio->drv_ops.in_flush(audio);
937 mutex_unlock(&audio->read_lock);
938 }
939}
940
941static int audmp3_events_pending(struct audio *audio)
942{
943 unsigned long flags;
944 int empty;
945
946 spin_lock_irqsave(&audio->event_queue_lock, flags);
947 empty = !list_empty(&audio->event_queue);
948 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
949 return empty || audio->event_abort;
950}
951
952static void audmp3_reset_event_queue(struct audio *audio)
953{
954 unsigned long flags;
955 struct audmp3_event *drv_evt;
956 struct list_head *ptr, *next;
957
958 spin_lock_irqsave(&audio->event_queue_lock, flags);
959 list_for_each_safe(ptr, next, &audio->event_queue) {
960 drv_evt = list_first_entry(&audio->event_queue,
961 struct audmp3_event, list);
962 list_del(&drv_evt->list);
963 kfree(drv_evt);
964 }
965 list_for_each_safe(ptr, next, &audio->free_event_queue) {
966 drv_evt = list_first_entry(&audio->free_event_queue,
967 struct audmp3_event, list);
968 list_del(&drv_evt->list);
969 kfree(drv_evt);
970 }
971 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
972
973 return;
974}
975
976static long audmp3_process_event_req(struct audio *audio, void __user *arg)
977{
978 long rc;
979 struct msm_audio_event usr_evt;
980 struct audmp3_event *drv_evt = NULL;
981 int timeout;
982 unsigned long flags;
983
984 if (copy_from_user(&usr_evt, arg, sizeof(struct msm_audio_event)))
985 return -EFAULT;
986
987 timeout = (int) usr_evt.timeout_ms;
988
989 if (timeout > 0) {
990 rc = wait_event_interruptible_timeout(
991 audio->event_wait, audmp3_events_pending(audio),
992 msecs_to_jiffies(timeout));
993 if (rc == 0)
994 return -ETIMEDOUT;
995 } else {
996 rc = wait_event_interruptible(
997 audio->event_wait, audmp3_events_pending(audio));
998 }
999
1000 if (rc < 0)
1001 return rc;
1002
1003 if (audio->event_abort) {
1004 audio->event_abort = 0;
1005 return -ENODEV;
1006 }
1007
1008 rc = 0;
1009
1010 spin_lock_irqsave(&audio->event_queue_lock, flags);
1011 if (!list_empty(&audio->event_queue)) {
1012 drv_evt = list_first_entry(&audio->event_queue,
1013 struct audmp3_event, list);
1014 list_del(&drv_evt->list);
1015 }
1016 if (drv_evt) {
1017 usr_evt.event_type = drv_evt->event_type;
1018 usr_evt.event_payload = drv_evt->payload;
1019 list_add_tail(&drv_evt->list, &audio->free_event_queue);
1020 } else
1021 rc = -1;
1022 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
1023
1024 if (drv_evt->event_type == AUDIO_EVENT_WRITE_DONE ||
1025 drv_evt->event_type == AUDIO_EVENT_READ_DONE) {
1026 mutex_lock(&audio->lock);
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301027 audmp3_ion_fixup(audio, drv_evt->payload.aio_buf.buf_addr,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001028 drv_evt->payload.aio_buf.buf_len, 0);
1029 mutex_unlock(&audio->lock);
1030 }
1031
1032 /* order reads from the output buffer */
1033 if (drv_evt->event_type == AUDIO_EVENT_READ_DONE)
1034 rmb();
1035
1036 if (!rc && copy_to_user(arg, &usr_evt, sizeof(usr_evt)))
1037 rc = -EFAULT;
1038
1039 return rc;
1040}
1041
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301042static int audmp3_ion_check(struct audio *audio,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001043 void *vaddr, unsigned long len)
1044{
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301045 struct audmp3_ion_region *region_elt;
1046 struct audmp3_ion_region t = { .vaddr = vaddr, .len = len };
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001047
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301048 list_for_each_entry(region_elt, &audio->ion_region_queue, list) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001049 if (CONTAINS(region_elt, &t) || CONTAINS(&t, region_elt) ||
1050 OVERLAPS(region_elt, &t)) {
1051 MM_ERR("region (vaddr %p len %ld)"
1052 " clashes with registered region"
1053 " (vaddr %p paddr %p len %ld)\n",
1054 vaddr, len,
1055 region_elt->vaddr,
1056 (void *)region_elt->paddr,
1057 region_elt->len);
1058 return -EINVAL;
1059 }
1060 }
1061
1062 return 0;
1063}
1064
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301065static int audmp3_ion_add(struct audio *audio,
1066 struct msm_audio_ion_info *info)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001067{
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301068 ion_phys_addr_t paddr;
1069 size_t len;
1070 unsigned long kvaddr;
1071 struct audmp3_ion_region *region;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001072 int rc = -EINVAL;
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301073 struct ion_handle *handle;
1074 unsigned long ionflag;
1075 void *temp_ptr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001076
1077 MM_DBG("\n"); /* Macro prints the file name and function */
1078 region = kmalloc(sizeof(*region), GFP_KERNEL);
1079
1080 if (!region) {
1081 rc = -ENOMEM;
1082 goto end;
1083 }
1084
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301085 handle = ion_import_dma_buf(audio->client, info->fd);
1086 if (IS_ERR_OR_NULL(handle)) {
1087 pr_err("%s: could not get handle of the given fd\n", __func__);
1088 goto import_error;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001089 }
1090
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301091 rc = ion_handle_get_flags(audio->client, handle, &ionflag);
1092 if (rc) {
1093 pr_err("%s: could not get flags for the handle\n", __func__);
1094 goto flag_error;
1095 }
1096
1097 temp_ptr = ion_map_kernel(audio->client, handle, ionflag);
1098 if (IS_ERR_OR_NULL(temp_ptr)) {
1099 pr_err("%s: could not get virtual address\n", __func__);
1100 goto map_error;
1101 }
1102 kvaddr = (unsigned long) temp_ptr;
1103
1104 rc = ion_phys(audio->client, handle, &paddr, &len);
1105 if (rc) {
1106 pr_err("%s: could not get physical address\n", __func__);
1107 goto ion_error;
1108 }
1109
1110 rc = audmp3_ion_check(audio, info->vaddr, len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001111 if (rc < 0) {
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301112 MM_ERR("audpcm_ion_check failed\n");
1113 goto ion_error;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001114 }
1115
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301116 region->handle = handle;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001117 region->vaddr = info->vaddr;
1118 region->fd = info->fd;
1119 region->paddr = paddr;
1120 region->kvaddr = kvaddr;
1121 region->len = len;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001122 region->ref_cnt = 0;
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301123 MM_DBG("[%p]:add region paddr %lx vaddr %p, len %lu kvaddr %lx\n",
1124 audio, region->paddr, region->vaddr,
1125 region->len, region->kvaddr);
1126 list_add_tail(&region->list, &audio->ion_region_queue);
1127 return rc;
1128
1129ion_error:
1130 ion_unmap_kernel(audio->client, handle);
1131map_error:
1132flag_error:
1133 ion_free(audio->client, handle);
1134import_error:
1135 kfree(region);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001136end:
1137 return rc;
1138}
1139
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301140static int audmp3_ion_remove(struct audio *audio,
1141 struct msm_audio_ion_info *info)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001142{
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301143 struct audmp3_ion_region *region;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001144 struct list_head *ptr, *next;
1145 int rc = -EINVAL;
1146
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301147 list_for_each_safe(ptr, next, &audio->ion_region_queue) {
1148 region = list_entry(ptr, struct audmp3_ion_region, list);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001149
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301150 if (region != NULL && (region->fd == info->fd) &&
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001151 (region->vaddr == info->vaddr)) {
1152 if (region->ref_cnt) {
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301153 MM_DBG("%s[%p]:region %p in use ref_cnt %d\n",
1154 __func__, audio, region,
1155 region->ref_cnt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001156 break;
1157 }
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301158 MM_DBG("remove region fd %d vaddr %p\n",
1159 info->fd, info->vaddr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001160 list_del(&region->list);
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301161 ion_unmap_kernel(audio->client, region->handle);
1162 ion_free(audio->client, region->handle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001163 kfree(region);
1164 rc = 0;
1165 break;
1166 }
1167 }
1168
1169 return rc;
1170}
1171
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301172static int audmp3_ion_lookup_vaddr(struct audio *audio, void *addr,
1173 unsigned long len, struct audmp3_ion_region **region)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001174{
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301175 struct audmp3_ion_region *region_elt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001176 int match_count = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001177 *region = NULL;
1178
1179 /* returns physical address or zero */
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301180 list_for_each_entry(region_elt, &audio->ion_region_queue, list) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001181 if (addr >= region_elt->vaddr &&
1182 addr < region_elt->vaddr + region_elt->len &&
1183 addr + len <= region_elt->vaddr + region_elt->len) {
1184 /* offset since we could pass vaddr inside a registerd
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301185 * ion buffer
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001186 */
1187
1188 match_count++;
1189 if (!*region)
1190 *region = region_elt;
1191 }
1192 }
1193
1194 if (match_count > 1) {
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301195 MM_ERR("%s[%p]:multiple hits for vaddr %p, len %ld\n",
1196 __func__, audio, addr, len);
1197 list_for_each_entry(region_elt, &audio->ion_region_queue,
1198 list) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001199 if (addr >= region_elt->vaddr &&
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301200 addr < region_elt->vaddr + region_elt->len &&
1201 addr + len <= region_elt->vaddr + region_elt->len)
1202 MM_ERR("\t%s[%p]:%p, %ld --> %p\n",
1203 __func__, audio,
1204 region_elt->vaddr,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001205 region_elt->len,
1206 (void *)region_elt->paddr);
1207 }
1208 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001209 return *region ? 0 : -1;
1210}
1211
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301212unsigned long audmp3_ion_fixup(struct audio *audio, void *addr,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001213 unsigned long len, int ref_up)
1214{
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301215 struct audmp3_ion_region *region;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001216 unsigned long paddr;
1217 int ret;
1218
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301219 ret = audmp3_ion_lookup_vaddr(audio, addr, len, &region);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001220 if (ret) {
1221 MM_ERR("lookup (%p, %ld) failed\n", addr, len);
1222 return 0;
1223 }
1224 if (ref_up)
1225 region->ref_cnt++;
1226 else
1227 region->ref_cnt--;
1228 MM_DBG("found region %p ref_cnt %d\n", region, region->ref_cnt);
1229 paddr = region->paddr + (addr - region->vaddr);
1230 return paddr;
1231}
1232
1233/* audio -> lock must be held at this point */
1234static int audmp3_aio_buf_add(struct audio *audio, unsigned dir,
1235 void __user *arg)
1236{
1237 unsigned long flags;
1238 struct audmp3_buffer_node *buf_node;
1239
1240 buf_node = kmalloc(sizeof(*buf_node), GFP_KERNEL);
1241
1242 if (!buf_node)
1243 return -ENOMEM;
1244
1245 if (copy_from_user(&buf_node->buf, arg, sizeof(buf_node->buf))) {
1246 kfree(buf_node);
1247 return -EFAULT;
1248 }
1249
1250 MM_DBG("node %p dir %x buf_addr %p buf_len %d data_len \
1251 %d\n", buf_node, dir,
1252 buf_node->buf.buf_addr, buf_node->buf.buf_len,
1253 buf_node->buf.data_len);
1254
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301255 buf_node->paddr = audmp3_ion_fixup(
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001256 audio, buf_node->buf.buf_addr,
1257 buf_node->buf.buf_len, 1);
1258
1259 if (dir) {
1260 /* write */
1261 if (!buf_node->paddr ||
1262 (buf_node->paddr & 0x1) ||
1263 (buf_node->buf.data_len & 0x1) ||
1264 (!audio->pcm_feedback &&
1265 !buf_node->buf.data_len)) {
1266 kfree(buf_node);
1267 return -EINVAL;
1268 }
1269 spin_lock_irqsave(&audio->dsp_lock, flags);
1270 list_add_tail(&buf_node->list, &audio->out_queue);
1271 spin_unlock_irqrestore(&audio->dsp_lock, flags);
1272 audio->drv_ops.send_data(audio, 0);
1273 } else {
1274 /* read */
1275 if (!buf_node->paddr ||
1276 (buf_node->paddr & 0x1) ||
1277 (buf_node->buf.buf_len < PCM_BUFSZ_MIN)) {
1278 kfree(buf_node);
1279 return -EINVAL;
1280 }
1281 spin_lock_irqsave(&audio->dsp_lock, flags);
1282 list_add_tail(&buf_node->list, &audio->in_queue);
1283 audio->drv_ops.buffer_refresh(audio);
1284 spin_unlock_irqrestore(&audio->dsp_lock, flags);
1285 }
1286
1287 MM_DBG("Add buf_node %p paddr %lx\n", buf_node, buf_node->paddr);
1288
1289 return 0;
1290}
1291
1292static int audio_enable_eq(struct audio *audio, int enable)
1293{
1294 if (audio->eq_enable == enable && !audio->eq_needs_commit)
1295 return 0;
1296
1297 audio->eq_enable = enable;
1298
1299 if (audio->running) {
1300 audpp_dsp_set_eq(audio->dec_id, enable, &audio->eq);
1301 audio->eq_needs_commit = 0;
1302 }
1303 return 0;
1304}
1305
1306static long audio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1307{
1308 struct audio *audio = file->private_data;
1309 int rc = -EINVAL;
1310 unsigned long flags = 0;
1311 uint16_t enable_mask;
1312 int enable;
1313 int prev_state;
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301314 unsigned long ionflag = 0;
1315 ion_phys_addr_t addr = 0;
1316 struct ion_handle *handle = NULL;
1317 int len = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001318
1319 MM_DBG("cmd = %d\n", cmd);
1320
1321 if (cmd == AUDIO_GET_STATS) {
1322 struct msm_audio_stats stats;
1323 stats.byte_count = audpp_avsync_byte_count(audio->dec_id);
1324 stats.sample_count = audpp_avsync_sample_count(audio->dec_id);
1325 if (copy_to_user((void *) arg, &stats, sizeof(stats)))
1326 return -EFAULT;
1327 return 0;
1328 }
1329
1330 switch (cmd) {
1331 case AUDIO_ENABLE_AUDPP:
1332 if (copy_from_user(&enable_mask, (void *) arg,
1333 sizeof(enable_mask))) {
1334 rc = -EFAULT;
1335 break;
1336 }
1337
1338 spin_lock_irqsave(&audio->dsp_lock, flags);
1339 enable = (enable_mask & EQ_ENABLE) ? 1 : 0;
1340 audio_enable_eq(audio, enable);
1341 spin_unlock_irqrestore(&audio->dsp_lock, flags);
1342 rc = 0;
1343 break;
1344 case AUDIO_SET_VOLUME:
1345 spin_lock_irqsave(&audio->dsp_lock, flags);
1346 audio->vol_pan.volume = arg;
1347 if (audio->running)
1348 audpp_dsp_set_vol_pan(audio->dec_id, &audio->vol_pan);
1349 spin_unlock_irqrestore(&audio->dsp_lock, flags);
1350 rc = 0;
1351 break;
1352
1353 case AUDIO_SET_PAN:
1354 spin_lock_irqsave(&audio->dsp_lock, flags);
1355 audio->vol_pan.pan = arg;
1356 if (audio->running)
1357 audpp_dsp_set_vol_pan(audio->dec_id, &audio->vol_pan);
1358 spin_unlock_irqrestore(&audio->dsp_lock, flags);
1359 rc = 0;
1360 break;
1361
1362 case AUDIO_SET_EQ:
1363 prev_state = audio->eq_enable;
1364 audio->eq_enable = 0;
1365 if (copy_from_user(&audio->eq.num_bands, (void *) arg,
1366 sizeof(audio->eq) -
1367 (AUDPP_CMD_CFG_OBJECT_PARAMS_COMMON_LEN + 2))) {
1368 rc = -EFAULT;
1369 break;
1370 }
1371 audio->eq_enable = prev_state;
1372 audio->eq_needs_commit = 1;
1373 rc = 0;
1374 break;
1375 }
1376
1377 if (-EINVAL != rc)
1378 return rc;
1379
1380 if (cmd == AUDIO_GET_EVENT) {
1381 MM_DBG(" AUDIO_GET_EVENT\n");
1382 if (mutex_trylock(&audio->get_event_lock)) {
1383 rc = audmp3_process_event_req(audio,
1384 (void __user *) arg);
1385 mutex_unlock(&audio->get_event_lock);
1386 } else
1387 rc = -EBUSY;
1388 return rc;
1389 }
1390
1391 if (cmd == AUDIO_ABORT_GET_EVENT) {
1392 audio->event_abort = 1;
1393 wake_up(&audio->event_wait);
1394 return 0;
1395 }
1396
1397 mutex_lock(&audio->lock);
1398 switch (cmd) {
1399 case AUDIO_START:
1400 MM_DBG("AUDIO_START\n");
1401 rc = audio_enable(audio);
1402 if (!rc) {
1403 rc = wait_event_interruptible_timeout(audio->wait,
1404 audio->dec_state != MSM_AUD_DECODER_STATE_NONE,
1405 msecs_to_jiffies(MSM_AUD_DECODER_WAIT_MS));
1406 MM_INFO("dec_state %d rc = %d\n", audio->dec_state, rc);
1407
1408 if (audio->dec_state != MSM_AUD_DECODER_STATE_SUCCESS)
1409 rc = -ENODEV;
1410 else
1411 rc = 0;
1412 }
1413 break;
1414 case AUDIO_STOP:
1415 MM_DBG("AUDIO_STOP\n");
1416 rc = audio_disable(audio);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001417 audio_ioport_reset(audio);
1418 audio->stopped = 0;
1419 break;
1420 case AUDIO_FLUSH:
1421 MM_DBG("AUDIO_FLUSH\n");
1422 audio->rflush = 1;
1423 audio->wflush = 1;
1424 audio_ioport_reset(audio);
1425 if (audio->running) {
1426 audpp_flush(audio->dec_id);
1427 rc = wait_event_interruptible(audio->write_wait,
1428 !audio->wflush);
1429 if (rc < 0) {
1430 MM_ERR("AUDIO_FLUSH interrupted\n");
1431 rc = -EINTR;
1432 }
1433 } else {
1434 audio->rflush = 0;
1435 audio->wflush = 0;
1436 }
1437 break;
1438
1439 case AUDIO_SET_CONFIG: {
1440 struct msm_audio_config config;
1441 if (copy_from_user(&config, (void *) arg, sizeof(config))) {
1442 rc = -EFAULT;
1443 break;
1444 }
1445 if (config.channel_count == 1) {
1446 config.channel_count = AUDPP_CMD_PCM_INTF_MONO_V;
1447 } else if (config.channel_count == 2) {
1448 config.channel_count = AUDPP_CMD_PCM_INTF_STEREO_V;
1449 } else {
1450 rc = -EINVAL;
1451 break;
1452 }
1453 audio->mfield = config.meta_field;
1454 audio->out_sample_rate = config.sample_rate;
1455 audio->out_channel_mode = config.channel_count;
1456 rc = 0;
1457 break;
1458 }
1459 case AUDIO_GET_CONFIG: {
1460 struct msm_audio_config config;
1461 config.buffer_size = (audio->out_dma_sz >> 1);
1462 config.buffer_count = 2;
1463 config.sample_rate = audio->out_sample_rate;
1464 if (audio->out_channel_mode == AUDPP_CMD_PCM_INTF_MONO_V) {
1465 config.channel_count = 1;
1466 } else {
1467 config.channel_count = 2;
1468 }
1469 config.meta_field = 0;
1470 config.unused[0] = 0;
1471 config.unused[1] = 0;
1472 config.unused[2] = 0;
1473 if (copy_to_user((void *) arg, &config, sizeof(config))) {
1474 rc = -EFAULT;
1475 } else {
1476 rc = 0;
1477 }
1478 break;
1479 }
1480 case AUDIO_GET_PCM_CONFIG:{
1481 struct msm_audio_pcm_config config;
1482 config.pcm_feedback = audio->pcm_feedback;
1483 config.buffer_count = PCM_BUF_MAX_COUNT;
1484 config.buffer_size = PCM_BUFSZ_MIN;
1485 if (copy_to_user((void *)arg, &config,
1486 sizeof(config)))
1487 rc = -EFAULT;
1488 else
1489 rc = 0;
1490 break;
1491 }
1492 case AUDIO_SET_PCM_CONFIG:{
1493 struct msm_audio_pcm_config config;
1494 if (copy_from_user
1495 (&config, (void *)arg, sizeof(config))) {
1496 rc = -EFAULT;
1497 break;
1498 }
1499
1500 if (config.pcm_feedback != audio->pcm_feedback) {
1501 MM_ERR("Not sufficient permission to"
1502 "change the playback mode\n");
1503 rc = -EACCES;
1504 break;
1505 }
1506 if (audio->drv_status & ADRV_STATUS_AIO_INTF) {
1507 rc = 0;
1508 break;
1509 }
1510
1511 if ((config.buffer_count > PCM_BUF_MAX_COUNT) ||
1512 (config.buffer_count == 1))
1513 config.buffer_count = PCM_BUF_MAX_COUNT;
1514
1515 if (config.buffer_size < PCM_BUFSZ_MIN)
1516 config.buffer_size = PCM_BUFSZ_MIN;
1517
1518 /* Check if pcm feedback is required */
1519 if ((config.pcm_feedback) && (!audio->read_data)) {
1520 MM_DBG("allocate PCM buffer %d\n",
1521 config.buffer_count *
1522 config.buffer_size);
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301523
1524 handle = ion_alloc(audio->client,
1525 (config.buffer_size *
1526 config.buffer_count),
1527 SZ_4K, ION_HEAP(ION_AUDIO_HEAP_ID));
1528 if (IS_ERR_OR_NULL(handle)) {
1529 MM_ERR("Unable to alloc I/P buffs\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001530 rc = -ENOMEM;
1531 break;
1532 }
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301533
1534 audio->input_buff_handle = handle;
1535
1536 rc = ion_phys(audio->client ,
1537 handle, &addr, &len);
1538 if (rc) {
1539 MM_ERR("Invalid phy: %x sz: %x\n",
1540 (unsigned int) addr,
1541 (unsigned int) len);
1542 rc = -ENOMEM;
1543 break;
1544 } else {
1545 MM_INFO("Got valid phy: %x sz: %x\n",
1546 (unsigned int) audio->read_phys,
1547 (unsigned int) len);
1548 }
1549 audio->read_phys = (int32_t)addr;
1550
1551 rc = ion_handle_get_flags(audio->client,
1552 handle, &ionflag);
1553 if (rc) {
1554 MM_ERR("could not get flags\n");
1555 rc = -ENOMEM;
1556 break;
1557 }
1558
1559 audio->map_v_read = ion_map_kernel(
1560 audio->client,
1561 handle, ionflag);
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05301562
1563 if (IS_ERR(audio->map_v_read)) {
1564 MM_ERR("map of read buf failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001565 rc = -ENOMEM;
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301566 ion_free(audio->client, handle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001567 } else {
1568 uint8_t index;
1569 uint32_t offset = 0;
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05301570 audio->read_data =
Laura Abbott35111d32012-04-27 18:41:48 -07001571 audio->map_v_read;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001572 audio->buf_refresh = 0;
1573 audio->pcm_buf_count =
1574 config.buffer_count;
1575 audio->read_next = 0;
1576 audio->fill_next = 0;
1577
1578 for (index = 0;
1579 index < config.buffer_count;
1580 index++) {
1581 audio->in[index].data =
1582 audio->read_data + offset;
1583 audio->in[index].addr =
1584 audio->read_phys + offset;
1585 audio->in[index].size =
1586 config.buffer_size;
1587 audio->in[index].used = 0;
1588 offset += config.buffer_size;
1589 }
1590 rc = 0;
1591 MM_DBG("read buf: phy addr \
1592 0x%08x kernel addr 0x%08x\n",
1593 audio->read_phys,
1594 (int)audio->read_data);
1595 }
1596 } else {
1597 rc = 0;
1598 }
1599 break;
1600 }
1601 case AUDIO_PAUSE:
1602 MM_DBG("AUDIO_PAUSE %ld\n", arg);
1603 rc = audpp_pause(audio->dec_id, (int) arg);
1604 break;
1605
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301606 case AUDIO_REGISTER_ION: {
1607 struct msm_audio_ion_info info;
1608 MM_DBG("AUDIO_REGISTER_ION\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001609 if (copy_from_user(&info, (void *) arg, sizeof(info)))
1610 rc = -EFAULT;
1611 else
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301612 rc = audmp3_ion_add(audio, &info);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001613 break;
1614 }
1615
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301616 case AUDIO_DEREGISTER_ION: {
1617 struct msm_audio_ion_info info;
1618 MM_DBG("AUDIO_DEREGISTER_ION\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001619 if (copy_from_user(&info, (void *) arg, sizeof(info)))
1620 rc = -EFAULT;
1621 else
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301622 rc = audmp3_ion_remove(audio, &info);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001623 break;
1624 }
1625 case AUDIO_ASYNC_WRITE:
1626 if (audio->drv_status & ADRV_STATUS_FSYNC)
1627 rc = -EBUSY;
1628 else
1629 rc = audmp3_aio_buf_add(audio, 1, (void __user *) arg);
1630 break;
1631
1632 case AUDIO_ASYNC_READ:
1633 if (audio->pcm_feedback)
1634 rc = audmp3_aio_buf_add(audio, 0, (void __user *) arg);
1635 else
1636 rc = -EPERM;
1637 break;
1638
1639 default:
1640 rc = -EINVAL;
1641 }
1642 mutex_unlock(&audio->lock);
1643 return rc;
1644}
1645
1646/* Only useful in tunnel-mode */
1647int audmp3_async_fsync(struct audio *audio)
1648{
1649 int rc = 0;
1650
1651 MM_DBG("\n"); /* Macro prints the file name and function */
1652
1653 /* Blocking client sends more data */
1654 mutex_lock(&audio->lock);
1655 audio->drv_status |= ADRV_STATUS_FSYNC;
1656 mutex_unlock(&audio->lock);
1657
1658 mutex_lock(&audio->write_lock);
1659 /* pcm dmamiss message is sent continously
1660 * when decoder is starved so no race
1661 * condition concern
1662 */
1663 audio->teos = 0;
1664
1665 rc = wait_event_interruptible(audio->write_wait,
1666 (audio->teos && audio->out_needed &&
1667 list_empty(&audio->out_queue))
1668 || audio->wflush || audio->stopped);
1669
1670 if (audio->stopped || audio->wflush)
1671 rc = -EBUSY;
1672
1673 mutex_unlock(&audio->write_lock);
1674 mutex_lock(&audio->lock);
1675 audio->drv_status &= ~ADRV_STATUS_FSYNC;
1676 mutex_unlock(&audio->lock);
1677
1678 return rc;
1679}
1680
1681int audmp3_sync_fsync(struct audio *audio)
1682{
1683 struct buffer *frame;
1684 int rc = 0;
1685
1686 MM_DBG("\n"); /* Macro prints the file name and function */
1687
1688 mutex_lock(&audio->write_lock);
1689
1690 rc = wait_event_interruptible(audio->write_wait,
1691 (!audio->out[0].used &&
1692 !audio->out[1].used &&
1693 audio->out_needed) || audio->wflush);
1694
1695 if (rc < 0)
1696 goto done;
1697 else if (audio->wflush) {
1698 rc = -EBUSY;
1699 goto done;
1700 }
1701
1702 if (audio->reserved) {
1703 MM_DBG("send reserved byte\n");
1704 frame = audio->out + audio->out_tail;
1705 ((char *) frame->data)[0] = audio->rsv_byte;
1706 ((char *) frame->data)[1] = 0;
1707 frame->used = 2;
1708 audio->drv_ops.send_data(audio, 0);
1709
1710 rc = wait_event_interruptible(audio->write_wait,
1711 (!audio->out[0].used &&
1712 !audio->out[1].used &&
1713 audio->out_needed) || audio->wflush);
1714
1715 if (rc < 0)
1716 goto done;
1717 else if (audio->wflush) {
1718 rc = -EBUSY;
1719 goto done;
1720 }
1721 }
1722
1723 /* pcm dmamiss message is sent continously
1724 * when decoder is starved so no race
1725 * condition concern
1726 */
1727 audio->teos = 0;
1728
1729 rc = wait_event_interruptible(audio->write_wait,
1730 audio->teos || audio->wflush);
1731
1732 if (audio->wflush)
1733 rc = -EBUSY;
1734
1735done:
1736 mutex_unlock(&audio->write_lock);
1737 return rc;
1738}
1739
Steve Mucklef132c6c2012-06-06 18:30:57 -07001740int audmp3_fsync(struct file *file, loff_t a, loff_t b, int datasync)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001741{
1742 struct audio *audio = file->private_data;
1743
1744 if (!audio->running || audio->pcm_feedback)
1745 return -EINVAL;
1746
1747 return audio->drv_ops.fsync(audio);
1748}
1749
1750static ssize_t audio_read(struct file *file, char __user *buf, size_t count,
1751 loff_t *pos)
1752{
1753 struct audio *audio = file->private_data;
1754 const char __user *start = buf;
1755 int rc = 0;
1756
1757 if (audio->drv_status & ADRV_STATUS_AIO_INTF)
1758 return -EPERM;
1759 else if (!audio->pcm_feedback)
1760 return 0; /* PCM feedback disabled. Nothing to read */
1761
1762 mutex_lock(&audio->read_lock);
1763 MM_DBG("%d \n", count);
1764 while (count > 0) {
1765 rc = wait_event_interruptible(
1766 audio->read_wait,
1767 (audio->in[audio->read_next].
1768 used > 0) || (audio->stopped)
1769 || (audio->rflush));
1770
1771 if (rc < 0)
1772 break;
1773
1774 if (audio->stopped || audio->rflush) {
1775 rc = -EBUSY;
1776 break;
1777 }
1778
1779 if (count < audio->in[audio->read_next].used) {
1780 /* Read must happen in frame boundary. Since
1781 * driver does not know frame size, read count
1782 * must be greater or equal
1783 * to size of PCM samples
1784 */
1785 MM_DBG("no partial frame done reading\n");
1786 break;
1787 } else {
1788 MM_DBG("read from in[%d]\n", audio->read_next);
1789 /* order reads from the output buffer */
1790 rmb();
1791 if (copy_to_user
1792 (buf, audio->in[audio->read_next].data,
1793 audio->in[audio->read_next].used)) {
1794 MM_ERR("invalid addr %x \n", (unsigned int)buf);
1795 rc = -EFAULT;
1796 break;
1797 }
1798 count -= audio->in[audio->read_next].used;
1799 buf += audio->in[audio->read_next].used;
1800 audio->in[audio->read_next].used = 0;
1801 if ((++audio->read_next) == audio->pcm_buf_count)
1802 audio->read_next = 0;
1803 break; /* Force to exit while loop
1804 * to prevent output thread
1805 * sleep too long if data is
1806 * not ready at this moment.
1807 */
1808 }
1809 }
1810
1811 /* don't feed output buffer to HW decoder during flushing
1812 * buffer refresh command will be sent once flush completes
1813 * send buf refresh command here can confuse HW decoder
1814 */
1815 if (audio->buf_refresh && !audio->rflush) {
1816 audio->buf_refresh = 0;
1817 MM_DBG("kick start pcm feedback again\n");
1818 audio->drv_ops.buffer_refresh(audio);
1819 }
1820
1821 mutex_unlock(&audio->read_lock);
1822
1823 if (buf > start)
1824 rc = buf - start;
1825
1826 MM_DBG("read %d bytes\n", rc);
1827 return rc;
1828}
1829
1830static int audmp3_process_eos(struct audio *audio,
1831 const char __user *buf_start, unsigned short mfield_size)
1832{
1833 int rc = 0;
1834 struct buffer *frame;
1835 char *buf_ptr;
1836
1837 if (audio->reserved) {
1838 MM_DBG("flush reserve byte\n");
1839 frame = audio->out + audio->out_head;
1840 buf_ptr = frame->data;
1841 rc = wait_event_interruptible(audio->write_wait,
1842 (frame->used == 0)
1843 || (audio->stopped)
1844 || (audio->wflush));
1845 if (rc < 0)
1846 goto done;
1847 if (audio->stopped || audio->wflush) {
1848 rc = -EBUSY;
1849 goto done;
1850 }
1851
1852 buf_ptr[0] = audio->rsv_byte;
1853 buf_ptr[1] = 0;
1854 audio->out_head ^= 1;
1855 frame->mfield_sz = 0;
1856 frame->used = 2;
1857 audio->reserved = 0;
1858 audio->drv_ops.send_data(audio, 0);
1859 }
1860
1861 frame = audio->out + audio->out_head;
1862
1863 rc = wait_event_interruptible(audio->write_wait,
1864 (audio->out_needed &&
1865 audio->out[0].used == 0 &&
1866 audio->out[1].used == 0)
1867 || (audio->stopped)
1868 || (audio->wflush));
1869
1870 if (rc < 0)
1871 goto done;
1872 if (audio->stopped || audio->wflush) {
1873 rc = -EBUSY;
1874 goto done;
1875 }
1876
1877 if (copy_from_user(frame->data, buf_start, mfield_size)) {
1878 rc = -EFAULT;
1879 goto done;
1880 }
1881
1882 frame->mfield_sz = mfield_size;
1883 audio->out_head ^= 1;
1884 frame->used = mfield_size;
1885 audio->drv_ops.send_data(audio, 0);
1886done:
1887 return rc;
1888}
1889
1890static ssize_t audio_write(struct file *file, const char __user *buf,
1891 size_t count, loff_t *pos)
1892{
1893 struct audio *audio = file->private_data;
1894 const char __user *start = buf;
1895 struct buffer *frame;
1896 size_t xfer;
1897 char *cpy_ptr;
1898 int rc = 0, eos_condition = AUDMP3_EOS_NONE;
1899 unsigned dsize;
1900 unsigned short mfield_size = 0;
1901
1902 if (audio->drv_status & ADRV_STATUS_AIO_INTF)
1903 return -EPERM;
1904
1905 MM_DBG("cnt=%d\n", count);
1906
1907 mutex_lock(&audio->write_lock);
1908 while (count > 0) {
1909 frame = audio->out + audio->out_head;
1910 cpy_ptr = frame->data;
1911 dsize = 0;
1912 rc = wait_event_interruptible(audio->write_wait,
1913 (frame->used == 0)
1914 || (audio->stopped)
1915 || (audio->wflush));
1916 if (rc < 0)
1917 break;
1918 if (audio->stopped || audio->wflush) {
1919 rc = -EBUSY;
1920 break;
1921 }
1922 if (audio->mfield) {
1923 if (buf == start) {
1924 /* Processing beginning of user buffer */
1925 if (__get_user(mfield_size,
1926 (unsigned short __user *) buf)) {
1927 rc = -EFAULT;
1928 break;
1929 } else if (mfield_size > count) {
1930 rc = -EINVAL;
1931 break;
1932 }
1933 MM_DBG("mf offset_val %x\n", mfield_size);
1934 if (copy_from_user(cpy_ptr, buf, mfield_size)) {
1935 rc = -EFAULT;
1936 break;
1937 }
1938 /* Check if EOS flag is set and buffer has
1939 * contains just meta field
1940 */
1941 if (cpy_ptr[AUDMP3_EOS_FLG_OFFSET] &
1942 AUDMP3_EOS_FLG_MASK) {
1943 MM_DBG("EOS SET\n");
1944 eos_condition = AUDMP3_EOS_SET;
1945 if (mfield_size == count) {
1946 buf += mfield_size;
1947 break;
1948 } else
1949 cpy_ptr[AUDMP3_EOS_FLG_OFFSET]
1950 &= ~AUDMP3_EOS_FLG_MASK;
1951 }
1952 cpy_ptr += mfield_size;
1953 count -= mfield_size;
1954 dsize += mfield_size;
1955 buf += mfield_size;
1956 } else {
1957 mfield_size = 0;
1958 MM_DBG("continuous buffer\n");
1959 }
1960 frame->mfield_sz = mfield_size;
1961 }
1962
1963 if (audio->reserved) {
1964 MM_DBG("append reserved byte %x\n", audio->rsv_byte);
1965 *cpy_ptr = audio->rsv_byte;
1966 xfer = (count > ((frame->size - mfield_size) - 1)) ?
1967 (frame->size - mfield_size) - 1 : count;
1968 cpy_ptr++;
1969 dsize += 1;
1970 audio->reserved = 0;
1971 } else
1972 xfer = (count > (frame->size - mfield_size)) ?
1973 (frame->size - mfield_size) : count;
1974
1975 if (copy_from_user(cpy_ptr, buf, xfer)) {
1976 rc = -EFAULT;
1977 break;
1978 }
1979
1980 dsize += xfer;
1981 if (dsize & 1) {
1982 audio->rsv_byte = ((char *) frame->data)[dsize - 1];
1983 MM_DBG("odd length buf reserve last byte %x\n",
1984 audio->rsv_byte);
1985 audio->reserved = 1;
1986 dsize--;
1987 }
1988 count -= xfer;
1989 buf += xfer;
1990
1991 if (dsize > 0) {
1992 audio->out_head ^= 1;
1993 frame->used = dsize;
1994 audio->drv_ops.send_data(audio, 0);
1995 }
1996 }
1997 if (eos_condition == AUDMP3_EOS_SET)
1998 rc = audmp3_process_eos(audio, start, mfield_size);
1999 mutex_unlock(&audio->write_lock);
2000 if (!rc) {
2001 if (buf > start)
2002 return buf - start;
2003 }
2004 return rc;
2005}
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05302006static void audmp3_reset_ion_region(struct audio *audio)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002007{
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05302008 struct audmp3_ion_region *region;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002009 struct list_head *ptr, *next;
2010
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05302011 list_for_each_safe(ptr, next, &audio->ion_region_queue) {
2012 region = list_entry(ptr, struct audmp3_ion_region, list);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002013 list_del(&region->list);
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05302014 ion_unmap_kernel(audio->client, region->handle);
2015 ion_free(audio->client, region->handle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002016 kfree(region);
2017 }
2018
2019 return;
2020}
2021
2022static int audio_release(struct inode *inode, struct file *file)
2023{
2024 struct audio *audio = file->private_data;
2025
2026 MM_INFO("audio instance 0x%08x freeing\n", (int)audio);
2027 mutex_lock(&audio->lock);
2028 audio_disable(audio);
2029 if (audio->rmt_resource_released == 0)
2030 rmt_put_resource(audio);
2031 audio->drv_ops.out_flush(audio);
2032 audio->drv_ops.in_flush(audio);
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05302033 audmp3_reset_ion_region(audio);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002034
2035 msm_adsp_put(audio->audplay);
2036 audpp_adec_free(audio->dec_id);
2037#ifdef CONFIG_HAS_EARLYSUSPEND
2038 unregister_early_suspend(&audio->suspend_ctl.node);
2039#endif
2040 audio->opened = 0;
2041 audio->event_abort = 1;
2042 wake_up(&audio->event_wait);
2043 audmp3_reset_event_queue(audio);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002044 mutex_unlock(&audio->lock);
2045#ifdef CONFIG_DEBUG_FS
2046 if (audio->dentry)
2047 debugfs_remove(audio->dentry);
2048#endif
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05302049 if (!(audio->drv_status & ADRV_STATUS_AIO_INTF)) {
2050 ion_unmap_kernel(audio->client, audio->output_buff_handle);
2051 ion_free(audio->client, audio->output_buff_handle);
2052 ion_unmap_kernel(audio->client, audio->input_buff_handle);
2053 ion_free(audio->client, audio->input_buff_handle);
2054 }
2055 ion_client_destroy(audio->client);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002056 kfree(audio);
2057 return 0;
2058}
2059
2060static void audmp3_post_event(struct audio *audio, int type,
2061 union msm_audio_event_payload payload)
2062{
2063 struct audmp3_event *e_node = NULL;
2064 unsigned long flags;
2065
2066 spin_lock_irqsave(&audio->event_queue_lock, flags);
2067
2068 if (!list_empty(&audio->free_event_queue)) {
2069 e_node = list_first_entry(&audio->free_event_queue,
2070 struct audmp3_event, list);
2071 list_del(&e_node->list);
2072 } else {
2073 e_node = kmalloc(sizeof(struct audmp3_event), GFP_ATOMIC);
2074 if (!e_node) {
2075 MM_ERR("No mem to post event %d\n", type);
2076 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
2077 return;
2078 }
2079 }
2080
2081 e_node->event_type = type;
2082 e_node->payload = payload;
2083
2084 list_add_tail(&e_node->list, &audio->event_queue);
2085 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
2086 wake_up(&audio->event_wait);
2087}
2088
2089#ifdef CONFIG_HAS_EARLYSUSPEND
2090static void audmp3_suspend(struct early_suspend *h)
2091{
2092 struct audmp3_suspend_ctl *ctl =
2093 container_of(h, struct audmp3_suspend_ctl, node);
2094 union msm_audio_event_payload payload;
2095
2096 MM_DBG("\n"); /* Macro prints the file name and function */
2097 audmp3_post_event(ctl->audio, AUDIO_EVENT_SUSPEND, payload);
2098}
2099
2100static void audmp3_resume(struct early_suspend *h)
2101{
2102 struct audmp3_suspend_ctl *ctl =
2103 container_of(h, struct audmp3_suspend_ctl, node);
2104 union msm_audio_event_payload payload;
2105
2106 MM_DBG("\n"); /* Macro prints the file name and function */
2107 audmp3_post_event(ctl->audio, AUDIO_EVENT_RESUME, payload);
2108}
2109#endif
2110
2111#ifdef CONFIG_DEBUG_FS
2112static ssize_t audmp3_debug_open(struct inode *inode, struct file *file)
2113{
2114 file->private_data = inode->i_private;
2115 return 0;
2116}
2117
2118static ssize_t audmp3_debug_read(struct file *file, char __user *buf,
2119 size_t count, loff_t *ppos)
2120{
2121 const int debug_bufmax = 4096;
2122 static char buffer[4096];
2123 int n = 0, i;
2124 struct audio *audio = file->private_data;
2125
2126 mutex_lock(&audio->lock);
2127 n = scnprintf(buffer, debug_bufmax, "opened %d\n", audio->opened);
2128 n += scnprintf(buffer + n, debug_bufmax - n,
2129 "enabled %d\n", audio->enabled);
2130 n += scnprintf(buffer + n, debug_bufmax - n,
2131 "stopped %d\n", audio->stopped);
2132 n += scnprintf(buffer + n, debug_bufmax - n,
2133 "pcm_feedback %d\n", audio->pcm_feedback);
2134 n += scnprintf(buffer + n, debug_bufmax - n,
2135 "out_buf_sz %d\n", audio->out[0].size);
2136 n += scnprintf(buffer + n, debug_bufmax - n,
2137 "pcm_buf_count %d \n", audio->pcm_buf_count);
2138 n += scnprintf(buffer + n, debug_bufmax - n,
2139 "pcm_buf_sz %d \n", audio->in[0].size);
2140 n += scnprintf(buffer + n, debug_bufmax - n,
2141 "volume %x \n", audio->vol_pan.volume);
2142 n += scnprintf(buffer + n, debug_bufmax - n,
2143 "sample rate %d \n", audio->out_sample_rate);
2144 n += scnprintf(buffer + n, debug_bufmax - n,
2145 "channel mode %d \n", audio->out_channel_mode);
2146 mutex_unlock(&audio->lock);
2147 /* Following variables are only useful for debugging when
2148 * when playback halts unexpectedly. Thus, no mutual exclusion
2149 * enforced
2150 */
2151 n += scnprintf(buffer + n, debug_bufmax - n,
2152 "wflush %d\n", audio->wflush);
2153 n += scnprintf(buffer + n, debug_bufmax - n,
2154 "rflush %d\n", audio->rflush);
2155 n += scnprintf(buffer + n, debug_bufmax - n,
2156 "running %d \n", audio->running);
2157 n += scnprintf(buffer + n, debug_bufmax - n,
2158 "dec state %d \n", audio->dec_state);
2159 n += scnprintf(buffer + n, debug_bufmax - n,
2160 "out_needed %d \n", audio->out_needed);
2161 n += scnprintf(buffer + n, debug_bufmax - n,
2162 "out_head %d \n", audio->out_head);
2163 n += scnprintf(buffer + n, debug_bufmax - n,
2164 "out_tail %d \n", audio->out_tail);
2165 n += scnprintf(buffer + n, debug_bufmax - n,
2166 "out[0].used %d \n", audio->out[0].used);
2167 n += scnprintf(buffer + n, debug_bufmax - n,
2168 "out[1].used %d \n", audio->out[1].used);
2169 n += scnprintf(buffer + n, debug_bufmax - n,
2170 "buffer_refresh %d \n", audio->buf_refresh);
2171 n += scnprintf(buffer + n, debug_bufmax - n,
2172 "read_next %d \n", audio->read_next);
2173 n += scnprintf(buffer + n, debug_bufmax - n,
2174 "fill_next %d \n", audio->fill_next);
2175 for (i = 0; i < audio->pcm_buf_count; i++)
2176 n += scnprintf(buffer + n, debug_bufmax - n,
2177 "in[%d].size %d \n", i, audio->in[i].used);
2178 buffer[n] = 0;
2179 return simple_read_from_buffer(buf, count, ppos, buffer, n);
2180}
2181
2182static const struct file_operations audmp3_debug_fops = {
2183 .read = audmp3_debug_read,
2184 .open = audmp3_debug_open,
2185};
2186#endif
2187
2188static int audio_open(struct inode *inode, struct file *file)
2189{
2190
2191 struct audio *audio = NULL;
2192 int rc, i, dec_attrb, decid;
2193 struct audmp3_event *e_node = NULL;
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05302194 unsigned mem_sz = DMASZ_MAX;
2195 unsigned long ionflag = 0;
2196 ion_phys_addr_t addr = 0;
2197 struct ion_handle *handle = NULL;
2198 struct ion_client *client = NULL;
2199 int len = 0;
2200
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002201#ifdef CONFIG_DEBUG_FS
2202 /* 4 bytes represents decoder number, 1 byte for terminate string */
2203 char name[sizeof "msm_mp3_" + 5];
2204#endif
2205
2206 /* Allocate audio instance, set to zero */
2207 audio = kzalloc(sizeof(struct audio), GFP_KERNEL);
2208 if (!audio) {
2209 MM_ERR("no memory to allocate audio instance \n");
2210 rc = -ENOMEM;
2211 goto done;
2212 }
2213 MM_INFO("audio instance 0x%08x created\n", (int)audio);
2214
2215 /* Allocate the decoder */
2216 dec_attrb = AUDDEC_DEC_MP3;
2217 if ((file->f_mode & FMODE_WRITE) &&
2218 (file->f_mode & FMODE_READ)) {
2219 dec_attrb |= MSM_AUD_MODE_NONTUNNEL;
2220 audio->pcm_feedback = NON_TUNNEL_MODE_PLAYBACK;
2221 } else if ((file->f_mode & FMODE_WRITE) &&
2222 !(file->f_mode & FMODE_READ)) {
2223 dec_attrb |= MSM_AUD_MODE_TUNNEL;
2224 audio->pcm_feedback = TUNNEL_MODE_PLAYBACK;
2225 } else {
2226 kfree(audio);
2227 rc = -EACCES;
2228 goto done;
2229 }
2230
2231 decid = audpp_adec_alloc(dec_attrb, &audio->module_name,
2232 &audio->queue_id);
2233 if (decid < 0) {
2234 MM_ERR("No free decoder available, freeing instance 0x%08x\n",
2235 (int)audio);
2236 rc = -ENODEV;
2237 kfree(audio);
2238 goto done;
2239 }
2240 audio->dec_id = decid & MSM_AUD_DECODER_MASK;
2241
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05302242 client = msm_ion_client_create(UINT_MAX, "Audio_MP3_Client");
2243 if (IS_ERR_OR_NULL(client)) {
2244 pr_err("Unable to create ION client\n");
2245 rc = -ENOMEM;
2246 goto client_create_error;
2247 }
2248 audio->client = client;
2249
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002250 /* Non AIO interface */
2251 if (!(file->f_flags & O_NONBLOCK)) {
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05302252
2253 MM_DBG("memsz = %d\n", mem_sz);
2254
2255 handle = ion_alloc(client, mem_sz, SZ_4K,
2256 ION_HEAP(ION_AUDIO_HEAP_ID));
2257 if (IS_ERR_OR_NULL(handle)) {
2258 MM_ERR("Unable to create allocate O/P buffers\n");
2259 rc = -ENOMEM;
2260 goto output_buff_alloc_error;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002261 }
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05302262 audio->output_buff_handle = handle;
2263
2264 rc = ion_phys(client , handle, &addr, &len);
2265 if (rc) {
2266 MM_ERR("O/P buffers:Invalid phy: %x sz: %x\n",
2267 (unsigned int) addr, (unsigned int) len);
2268 goto output_buff_get_phys_error;
2269 } else {
2270 MM_INFO("O/P buffers:valid phy: %x sz: %x\n",
2271 (unsigned int) addr, (unsigned int) len);
2272 }
2273 audio->phys = (int32_t)addr;
2274
2275
2276 rc = ion_handle_get_flags(client, handle, &ionflag);
2277 if (rc) {
2278 MM_ERR("could not get flags for the handle\n");
2279 goto output_buff_get_flags_error;
2280 }
2281
2282 audio->map_v_write = ion_map_kernel(client, handle, ionflag);
2283 if (IS_ERR(audio->map_v_write)) {
2284 MM_ERR("could not map write buffers\n");
2285 rc = -ENOMEM;
2286 goto output_buff_map_error;
2287 }
2288 audio->data = audio->map_v_write;
2289 MM_DBG("write buf: phy addr 0x%08x kernel addr 0x%08x\n",
2290 audio->phys, (int)audio->data);
2291
2292 audio->out_dma_sz = mem_sz;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002293 }
2294
2295 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK) {
2296 rc = audmgr_open(&audio->audmgr);
2297 if (rc) {
2298 MM_ERR("audmgr open failed, freeing instance \
2299 0x%08x\n", (int)audio);
2300 goto err;
2301 }
2302 }
2303
2304 rc = msm_adsp_get(audio->module_name, &audio->audplay,
2305 &audplay_adsp_ops, audio);
2306
2307 if (rc) {
2308 MM_ERR("failed to get %s module, freeing instance 0x%08x\n",
2309 audio->module_name, (int)audio);
2310 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK)
2311 audmgr_close(&audio->audmgr);
2312 goto err;
2313 }
2314
2315 rc = rmt_get_resource(audio);
2316 if (rc) {
2317 MM_ERR("ADSP resources are not available for MP3 session \
2318 0x%08x on decoder: %d\n", (int)audio, audio->dec_id);
2319 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK)
2320 audmgr_close(&audio->audmgr);
2321 msm_adsp_put(audio->audplay);
2322 goto err;
2323 }
2324
2325 if (file->f_flags & O_NONBLOCK) {
2326 MM_DBG("set to aio interface\n");
2327 audio->drv_status |= ADRV_STATUS_AIO_INTF;
2328 audio->drv_ops.pcm_buf_update = audmp3_async_pcm_buf_update;
2329 audio->drv_ops.buffer_refresh = audmp3_async_buffer_refresh;
2330 audio->drv_ops.send_data = audmp3_async_send_data;
2331 audio->drv_ops.out_flush = audmp3_async_flush;
2332 audio->drv_ops.in_flush = audmp3_async_flush_pcm_buf;
2333 audio->drv_ops.fsync = audmp3_async_fsync;
2334 } else {
2335 MM_DBG("set to std io interface\n");
2336 audio->drv_ops.pcm_buf_update = audio_update_pcm_buf_entry;
2337 audio->drv_ops.buffer_refresh = audplay_buffer_refresh;
2338 audio->drv_ops.send_data = audplay_send_data;
2339 audio->drv_ops.out_flush = audio_flush;
2340 audio->drv_ops.in_flush = audio_flush_pcm_buf;
2341 audio->drv_ops.fsync = audmp3_sync_fsync;
2342 audio->out[0].data = audio->data + 0;
2343 audio->out[0].addr = audio->phys + 0;
2344 audio->out[0].size = (audio->out_dma_sz >> 1);
2345
2346 audio->out[1].data = audio->data + audio->out[0].size;
2347 audio->out[1].addr = audio->phys + audio->out[0].size;
2348 audio->out[1].size = audio->out[0].size;
2349 }
2350
2351 /* Initialize all locks of audio instance */
2352 mutex_init(&audio->lock);
2353 mutex_init(&audio->write_lock);
2354 mutex_init(&audio->read_lock);
2355 mutex_init(&audio->get_event_lock);
2356 spin_lock_init(&audio->dsp_lock);
2357 init_waitqueue_head(&audio->write_wait);
2358 init_waitqueue_head(&audio->read_wait);
2359 INIT_LIST_HEAD(&audio->out_queue);
2360 INIT_LIST_HEAD(&audio->in_queue);
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05302361 INIT_LIST_HEAD(&audio->ion_region_queue);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002362 INIT_LIST_HEAD(&audio->free_event_queue);
2363 INIT_LIST_HEAD(&audio->event_queue);
2364 init_waitqueue_head(&audio->wait);
2365 init_waitqueue_head(&audio->event_wait);
2366 spin_lock_init(&audio->event_queue_lock);
2367
2368 audio->out_sample_rate = 44100;
2369 audio->out_channel_mode = AUDPP_CMD_PCM_INTF_STEREO_V;
2370 audio->vol_pan.volume = 0x2000;
2371
2372 audio->drv_ops.out_flush(audio);
2373
2374 file->private_data = audio;
2375 audio->opened = 1;
2376#ifdef CONFIG_DEBUG_FS
2377 snprintf(name, sizeof name, "msm_mp3_%04x", audio->dec_id);
2378 audio->dentry = debugfs_create_file(name, S_IFREG | S_IRUGO,
2379 NULL, (void *) audio, &audmp3_debug_fops);
2380
2381 if (IS_ERR(audio->dentry))
2382 MM_DBG("debugfs_create_file failed\n");
2383#endif
2384#ifdef CONFIG_HAS_EARLYSUSPEND
2385 audio->suspend_ctl.node.level = EARLY_SUSPEND_LEVEL_DISABLE_FB;
2386 audio->suspend_ctl.node.resume = audmp3_resume;
2387 audio->suspend_ctl.node.suspend = audmp3_suspend;
2388 audio->suspend_ctl.audio = audio;
2389 register_early_suspend(&audio->suspend_ctl.node);
2390#endif
2391 for (i = 0; i < AUDMP3_EVENT_NUM; i++) {
2392 e_node = kmalloc(sizeof(struct audmp3_event), GFP_KERNEL);
2393 if (e_node)
2394 list_add_tail(&e_node->list, &audio->free_event_queue);
2395 else {
2396 MM_ERR("event pkt alloc failed\n");
2397 break;
2398 }
2399 }
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05302400
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002401done:
2402 return rc;
2403err:
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05302404 ion_unmap_kernel(client, audio->output_buff_handle);
2405output_buff_map_error:
2406output_buff_get_flags_error:
2407output_buff_get_phys_error:
2408 ion_free(client, audio->output_buff_handle);
2409output_buff_alloc_error:
2410 ion_client_destroy(client);
2411client_create_error:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002412 audpp_adec_free(audio->dec_id);
2413 kfree(audio);
2414 return rc;
2415}
2416
2417static const struct file_operations audio_mp3_fops = {
2418 .owner = THIS_MODULE,
2419 .open = audio_open,
2420 .release = audio_release,
2421 .read = audio_read,
2422 .write = audio_write,
2423 .unlocked_ioctl = audio_ioctl,
2424 .fsync = audmp3_fsync,
2425};
2426
2427struct miscdevice audio_mp3_misc = {
2428 .minor = MISC_DYNAMIC_MINOR,
2429 .name = "msm_mp3",
2430 .fops = &audio_mp3_fops,
2431};
2432
2433static int __init audio_init(void)
2434{
2435 return misc_register(&audio_mp3_misc);
2436}
2437
2438static void __exit audio_exit(void)
2439{
2440 misc_deregister(&audio_mp3_misc);
2441}
2442
2443module_init(audio_init);
2444module_exit(audio_exit);
2445
2446MODULE_DESCRIPTION("MSM MP3 driver");
2447MODULE_LICENSE("GPL v2");