blob: b8c64beec76e88a45e6417241cf4e6aaa8c8683c [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>
Mitchel Humpherys1da6ebe2012-09-06 10:15:56 -070037#include <linux/msm_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>
Manish Dewanganfa8a6b62012-07-09 16:23:27 +053045#include <mach/qdsp5/qdsp5audpp.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070046#include <mach/qdsp5/qdsp5audplaycmdi.h>
47#include <mach/qdsp5/qdsp5audplaymsg.h>
48#include <mach/qdsp5/qdsp5rmtcmdi.h>
49#include <mach/debug_mm.h>
50
Santosh Mardi0be3b8e2011-07-06 10:00:21 +053051#include "audmgr.h"
52
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070053#define ADRV_STATUS_AIO_INTF 0x00000001
54#define ADRV_STATUS_OBUF_GIVEN 0x00000002
55#define ADRV_STATUS_IBUF_GIVEN 0x00000004
56#define ADRV_STATUS_FSYNC 0x00000008
57
58/* Size must be power of 2 */
59#define BUFSZ_MAX 32768
60#define BUFSZ_MIN 4096
61#define DMASZ_MAX (BUFSZ_MAX * 2)
62#define DMASZ_MIN (BUFSZ_MIN * 2)
63
64#define AUDPLAY_INVALID_READ_PTR_OFFSET 0xFFFF
65#define AUDDEC_DEC_MP3 2
66
67#define PCM_BUFSZ_MIN 4800 /* Hold one stereo MP3 frame */
68#define PCM_BUF_MAX_COUNT 5 /* DSP only accepts 5 buffers at most
69 but support 2 buffers currently */
70#define ROUTING_MODE_FTRT 1
71#define ROUTING_MODE_RT 2
72/* Decoder status received from AUDPPTASK */
73#define AUDPP_DEC_STATUS_SLEEP 0
74#define AUDPP_DEC_STATUS_INIT 1
75#define AUDPP_DEC_STATUS_CFG 2
76#define AUDPP_DEC_STATUS_PLAY 3
77
78#define AUDMP3_METAFIELD_MASK 0xFFFF0000
79#define AUDMP3_EOS_FLG_OFFSET 0x0A /* Offset from beginning of buffer */
80#define AUDMP3_EOS_FLG_MASK 0x01
81#define AUDMP3_EOS_NONE 0x0 /* No EOS detected */
82#define AUDMP3_EOS_SET 0x1 /* EOS set in meta field */
83
84#define AUDMP3_EVENT_NUM 10 /* Default number of pre-allocated event packets */
85
86#define __CONTAINS(r, v, l) ({ \
87 typeof(r) __r = r; \
88 typeof(v) __v = v; \
89 typeof(v) __e = __v + l; \
90 int res = ((__v >= __r->vaddr) && \
91 (__e <= __r->vaddr + __r->len)); \
92 res; \
93})
94
95#define CONTAINS(r1, r2) ({ \
96 typeof(r2) __r2 = r2; \
97 __CONTAINS(r1, __r2->vaddr, __r2->len); \
98})
99
100#define IN_RANGE(r, v) ({ \
101 typeof(r) __r = r; \
102 typeof(v) __vv = v; \
103 int res = ((__vv >= __r->vaddr) && \
104 (__vv < (__r->vaddr + __r->len))); \
105 res; \
106})
107
108#define OVERLAPS(r1, r2) ({ \
109 typeof(r1) __r1 = r1; \
110 typeof(r2) __r2 = r2; \
111 typeof(__r2->vaddr) __v = __r2->vaddr; \
112 typeof(__v) __e = __v + __r2->len - 1; \
113 int res = (IN_RANGE(__r1, __v) || IN_RANGE(__r1, __e)); \
114 res; \
115})
116
117struct audio;
118
119struct buffer {
120 void *data;
121 unsigned size;
122 unsigned used; /* Input usage actual DSP produced PCM size */
123 unsigned addr;
124 unsigned short mfield_sz; /*only useful for data has meta field */
125};
126
127#ifdef CONFIG_HAS_EARLYSUSPEND
128struct audmp3_suspend_ctl {
129 struct early_suspend node;
130 struct audio *audio;
131};
132#endif
133
134struct audmp3_event {
135 struct list_head list;
136 int event_type;
137 union msm_audio_event_payload payload;
138};
139
Sidipotu Ashok172c98b2012-06-26 17:58:29 +0530140struct audmp3_ion_region {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700141 struct list_head list;
Sidipotu Ashok172c98b2012-06-26 17:58:29 +0530142 struct ion_handle *handle;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700143 int fd;
144 void *vaddr;
145 unsigned long paddr;
146 unsigned long kvaddr;
147 unsigned long len;
148 unsigned ref_cnt;
149};
150
151struct audmp3_buffer_node {
152 struct list_head list;
153 struct msm_audio_aio_buf buf;
154 unsigned long paddr;
155};
156
157struct audmp3_drv_operations {
158 void (*pcm_buf_update)(struct audio *, uint32_t *);
159 void (*buffer_refresh)(struct audio *);
160 void (*send_data)(struct audio *, unsigned);
161 void (*out_flush)(struct audio *);
162 void (*in_flush)(struct audio *);
163 int (*fsync)(struct audio *);
164};
165
166struct audio {
167 struct buffer out[2];
168
169 spinlock_t dsp_lock;
170
171 uint8_t out_head;
172 uint8_t out_tail;
173 uint8_t out_needed; /* number of buffers the dsp is waiting for */
174 unsigned out_dma_sz;
175 struct list_head out_queue; /* queue to retain output buffers */
176 atomic_t out_bytes;
177
178 struct mutex lock;
179 struct mutex write_lock;
180 wait_queue_head_t write_wait;
181
182 /* Host PCM section */
183 struct buffer in[PCM_BUF_MAX_COUNT];
184 struct mutex read_lock;
185 wait_queue_head_t read_wait; /* Wait queue for read */
186 char *read_data; /* pointer to reader buffer */
187 int32_t read_phys; /* physical address of reader buffer */
188 uint8_t read_next; /* index to input buffers to be read next */
189 uint8_t fill_next; /* index to buffer that DSP should be filling */
190 uint8_t pcm_buf_count; /* number of pcm buffer allocated */
191 struct list_head in_queue; /* queue to retain input buffers */
192 /* ---- End of Host PCM section */
193
194 struct msm_adsp_module *audplay;
195
196 /* configuration to use on next enable */
197 uint32_t out_sample_rate;
198 uint32_t out_channel_mode;
199
200 struct audmgr audmgr;
201
202 /* data allocated for various buffers */
203 char *data;
204 int32_t phys; /* physical address of write buffer */
Laura Abbott35111d32012-04-27 18:41:48 -0700205 void *map_v_read;
206 void *map_v_write;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700207
208 uint32_t drv_status;
209 int mfield; /* meta field embedded in data */
210 int rflush; /* Read flush */
211 int wflush; /* Write flush */
212 int opened;
213 int enabled;
214 int running;
215 int stopped; /* set when stopped, cleared on flush */
216 int pcm_feedback;
217 int buf_refresh;
218 int rmt_resource_released;
219 int teos; /* valid only if tunnel mode & no data left for decoder */
220 enum msm_aud_decoder_state dec_state; /* Represents decoder state */
221 int reserved; /* A byte is being reserved */
222 char rsv_byte; /* Handle odd length user data */
223
224 const char *module_name;
225 unsigned queue_id;
226 uint16_t dec_id;
227 uint32_t read_ptr_offset;
228
229#ifdef CONFIG_HAS_EARLYSUSPEND
230 struct audmp3_suspend_ctl suspend_ctl;
231#endif
232
233#ifdef CONFIG_DEBUG_FS
234 struct dentry *dentry;
235#endif
236
237 wait_queue_head_t wait;
238 struct list_head free_event_queue;
239 struct list_head event_queue;
240 wait_queue_head_t event_wait;
241 spinlock_t event_queue_lock;
242 struct mutex get_event_lock;
243 int event_abort;
244
Sidipotu Ashok172c98b2012-06-26 17:58:29 +0530245 struct list_head ion_region_queue; /* protected by lock */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700246 struct audmp3_drv_operations drv_ops;
247
248 int eq_enable;
249 int eq_needs_commit;
250 audpp_cmd_cfg_object_params_eqalizer eq;
251 audpp_cmd_cfg_object_params_volume vol_pan;
Sidipotu Ashok172c98b2012-06-26 17:58:29 +0530252 struct ion_client *client;
253 struct ion_handle *input_buff_handle;
254 struct ion_handle *output_buff_handle;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700255};
256
257static int auddec_dsp_config(struct audio *audio, int enable);
258static void audpp_cmd_cfg_adec_params(struct audio *audio);
259static void audpp_cmd_cfg_routing_mode(struct audio *audio);
260static void audplay_send_data(struct audio *audio, unsigned needed);
261static void audplay_config_hostpcm(struct audio *audio);
262static void audplay_buffer_refresh(struct audio *audio);
263static void audio_dsp_event(void *private, unsigned id, uint16_t *msg);
264static void audmp3_post_event(struct audio *audio, int type,
265 union msm_audio_event_payload payload);
Sidipotu Ashok172c98b2012-06-26 17:58:29 +0530266static unsigned long audmp3_ion_fixup(struct audio *audio, void *addr,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700267 unsigned long len, int ref_up);
268
269static int rmt_put_resource(struct audio *audio)
270{
271 struct aud_codec_config_cmd cmd;
272 unsigned short client_idx;
273
274 cmd.cmd_id = RM_CMD_AUD_CODEC_CFG;
275 cmd.client_id = RM_AUD_CLIENT_ID;
276 cmd.task_id = audio->dec_id;
277 cmd.enable = RMT_DISABLE;
278 cmd.dec_type = AUDDEC_DEC_MP3;
279 client_idx = ((cmd.client_id << 8) | cmd.task_id);
280
281 return put_adsp_resource(client_idx, &cmd, sizeof(cmd));
282}
283
284static int rmt_get_resource(struct audio *audio)
285{
286 struct aud_codec_config_cmd cmd;
287 unsigned short client_idx;
288
289 cmd.cmd_id = RM_CMD_AUD_CODEC_CFG;
290 cmd.client_id = RM_AUD_CLIENT_ID;
291 cmd.task_id = audio->dec_id;
292 cmd.enable = RMT_ENABLE;
293 cmd.dec_type = AUDDEC_DEC_MP3;
294 client_idx = ((cmd.client_id << 8) | cmd.task_id);
295
296 return get_adsp_resource(client_idx, &cmd, sizeof(cmd));
297}
298
299/* must be called with audio->lock held */
300static int audio_enable(struct audio *audio)
301{
302 struct audmgr_config cfg;
303 int rc;
304
305 MM_DBG("\n"); /* Macro prints the file name and function */
306
307 if (audio->enabled)
308 return 0;
309
310 if (audio->rmt_resource_released == 1) {
311 audio->rmt_resource_released = 0;
312 rc = rmt_get_resource(audio);
313 if (rc) {
314 MM_ERR("ADSP resources are not available for MP3 \
315 session 0x%08x on decoder: %d\n Ignoring \
316 error and going ahead with the playback\n",
317 (int)audio, audio->dec_id);
318 }
319 }
320
321 audio->dec_state = MSM_AUD_DECODER_STATE_NONE;
322 audio->out_tail = 0;
323 audio->out_needed = 0;
324
325 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK) {
326 cfg.tx_rate = RPC_AUD_DEF_SAMPLE_RATE_NONE;
327 cfg.rx_rate = RPC_AUD_DEF_SAMPLE_RATE_48000;
328 cfg.def_method = RPC_AUD_DEF_METHOD_PLAYBACK;
329 cfg.codec = RPC_AUD_DEF_CODEC_MP3;
330 cfg.snd_method = RPC_SND_METHOD_MIDI;
331
332 rc = audmgr_enable(&audio->audmgr, &cfg);
333 if (rc < 0)
334 return rc;
335 }
336
337 if (msm_adsp_enable(audio->audplay)) {
338 MM_ERR("msm_adsp_enable(audplay) failed\n");
339 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK)
340 audmgr_disable(&audio->audmgr);
341 return -ENODEV;
342 }
343
344 if (audpp_enable(audio->dec_id, audio_dsp_event, audio)) {
345 MM_ERR("audpp_enable() failed\n");
346 msm_adsp_disable(audio->audplay);
347 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK)
348 audmgr_disable(&audio->audmgr);
349 return -ENODEV;
350 }
351
352 audio->enabled = 1;
353 return 0;
354}
355
356/* must be called with audio->lock held */
357static int audio_disable(struct audio *audio)
358{
359 int rc = 0;
360 MM_DBG("\n"); /* Macro prints the file name and function */
361 if (audio->enabled) {
362 audio->enabled = 0;
363 audio->dec_state = MSM_AUD_DECODER_STATE_NONE;
364 auddec_dsp_config(audio, 0);
365 rc = wait_event_interruptible_timeout(audio->wait,
366 audio->dec_state != MSM_AUD_DECODER_STATE_NONE,
367 msecs_to_jiffies(MSM_AUD_DECODER_WAIT_MS));
368 if (rc == 0)
369 rc = -ETIMEDOUT;
370 else if (audio->dec_state != MSM_AUD_DECODER_STATE_CLOSE)
371 rc = -EFAULT;
372 else
373 rc = 0;
Manish Dewangan89a9f232012-02-09 17:14:40 +0530374 audio->stopped = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700375 wake_up(&audio->write_wait);
376 wake_up(&audio->read_wait);
377 msm_adsp_disable(audio->audplay);
378 audpp_disable(audio->dec_id, audio);
379 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK)
380 audmgr_disable(&audio->audmgr);
381 audio->out_needed = 0;
382 rmt_put_resource(audio);
383 audio->rmt_resource_released = 1;
384 }
385 return rc;
386}
387
388/* ------------------- dsp --------------------- */
389static void audmp3_async_pcm_buf_update(struct audio *audio, uint32_t *payload)
390{
391 unsigned long flags;
392 union msm_audio_event_payload event_payload;
393 struct audmp3_buffer_node *filled_buf;
394 uint8_t index;
395
396 if (audio->rflush)
397 return;
398
399 spin_lock_irqsave(&audio->dsp_lock, flags);
400 for (index = 0; index < payload[1]; index++) {
401 BUG_ON(list_empty(&audio->in_queue));
402 filled_buf = list_first_entry(&audio->in_queue,
403 struct audmp3_buffer_node, list);
404 if (filled_buf->paddr == payload[2 + index * 2]) {
405 list_del(&filled_buf->list);
406 event_payload.aio_buf = filled_buf->buf;
407 event_payload.aio_buf.data_len =
408 payload[3 + index * 2];
409 MM_DBG("pcm buf %p data_len %d\n", filled_buf,
410 event_payload.aio_buf.data_len);
411 audmp3_post_event(audio, AUDIO_EVENT_READ_DONE,
412 event_payload);
413 kfree(filled_buf);
414 } else {
415 MM_ERR("expected=%lx ret=%x\n", filled_buf->paddr,
416 payload[2 + index * 2]);
417 break;
418 }
419 }
420
421 audio->drv_status &= ~ADRV_STATUS_IBUF_GIVEN;
422 audio->drv_ops.buffer_refresh(audio);
423 spin_unlock_irqrestore(&audio->dsp_lock, flags);
424
425}
426
427static void audio_update_pcm_buf_entry(struct audio *audio, uint32_t *payload)
428{
429 uint8_t index;
430 unsigned long flags;
431
432 if (audio->rflush)
433 return;
434
435 spin_lock_irqsave(&audio->dsp_lock, flags);
436 for (index = 0; index < payload[1]; index++) {
437 if (audio->in[audio->fill_next].addr ==
438 payload[2 + index * 2]) {
439 MM_DBG("in[%d] ready\n", audio->fill_next);
440 audio->in[audio->fill_next].used =
441 payload[3 + index * 2];
442 if ((++audio->fill_next) == audio->pcm_buf_count)
443 audio->fill_next = 0;
444
445 } else {
446 MM_ERR("expected=%x ret=%x\n",
447 audio->in[audio->fill_next].addr,
448 payload[2 + index * 2]);
449 break;
450 }
451 }
452 if (audio->in[audio->fill_next].used == 0) {
453 audio->drv_ops.buffer_refresh(audio);
454 } else {
455 MM_DBG("read cannot keep up\n");
456 audio->buf_refresh = 1;
457 }
458 wake_up(&audio->read_wait);
459 spin_unlock_irqrestore(&audio->dsp_lock, flags);
460
461}
462
463static void audplay_dsp_event(void *data, unsigned id, size_t len,
464 void (*getevent) (void *ptr, size_t len))
465{
466 struct audio *audio = data;
467 uint32_t msg[28];
468 getevent(msg, sizeof(msg));
469
470 MM_DBG("msg_id=%x\n", id);
471
472 switch (id) {
473 case AUDPLAY_MSG_DEC_NEEDS_DATA:
474 audio->drv_ops.send_data(audio, 1);
475 break;
476
477 case AUDPLAY_MSG_BUFFER_UPDATE:
478 audio->drv_ops.pcm_buf_update(audio, msg);
479 break;
480
481 case ADSP_MESSAGE_ID:
482 MM_DBG("Received ADSP event: module enable(audplaytask)\n");
483 break;
484
485 default:
486 MM_ERR("unexpected message from decoder \n");
487 break;
488 }
489}
490
491static void audio_dsp_event(void *private, unsigned id, uint16_t *msg)
492{
493 struct audio *audio = private;
494
495 switch (id) {
496 case AUDPP_MSG_STATUS_MSG:{
497 unsigned status = msg[1];
498
499 switch (status) {
500 case AUDPP_DEC_STATUS_SLEEP: {
501 uint16_t reason = msg[2];
502 MM_DBG("decoder status: sleep reason=0x%04x\n",
503 reason);
504 if ((reason == AUDPP_MSG_REASON_MEM)
505 || (reason ==
506 AUDPP_MSG_REASON_NODECODER)) {
507 audio->dec_state =
508 MSM_AUD_DECODER_STATE_FAILURE;
509 wake_up(&audio->wait);
510 } else if (reason == AUDPP_MSG_REASON_NONE) {
511 /* decoder is in disable state */
512 audio->dec_state =
513 MSM_AUD_DECODER_STATE_CLOSE;
514 wake_up(&audio->wait);
515 }
516 break;
517 }
518 case AUDPP_DEC_STATUS_INIT:
519 MM_DBG("decoder status: init \n");
520 if (audio->pcm_feedback)
521 audpp_cmd_cfg_routing_mode(audio);
522 else
523 audpp_cmd_cfg_adec_params(audio);
524 break;
525
526 case AUDPP_DEC_STATUS_CFG:
527 MM_DBG("decoder status: cfg \n");
528 break;
529 case AUDPP_DEC_STATUS_PLAY:
530 MM_DBG("decoder status: play \n");
531 if (audio->pcm_feedback) {
532 audplay_config_hostpcm(audio);
533 audio->drv_ops.buffer_refresh(audio);
534 }
535 audio->dec_state =
536 MSM_AUD_DECODER_STATE_SUCCESS;
537 wake_up(&audio->wait);
538 break;
539 default:
540 MM_ERR("unknown decoder status \n");
541 break;
542 }
543 break;
544 }
545 case AUDPP_MSG_CFG_MSG:
546 if (msg[0] == AUDPP_MSG_ENA_ENA) {
547 MM_DBG("CFG_MSG ENABLE\n");
548 auddec_dsp_config(audio, 1);
549 audio->out_needed = 0;
550 audio->running = 1;
551 audpp_dsp_set_vol_pan(audio->dec_id, &audio->vol_pan);
552 audpp_dsp_set_eq(audio->dec_id, audio->eq_enable,
553 &audio->eq);
554 audpp_avsync(audio->dec_id, 22050);
555 } else if (msg[0] == AUDPP_MSG_ENA_DIS) {
556 MM_DBG("CFG_MSG DISABLE\n");
557 audpp_avsync(audio->dec_id, 0);
558 audio->running = 0;
559 } else {
560 MM_DBG("CFG_MSG %d?\n", msg[0]);
561 }
562 break;
563 case AUDPP_MSG_ROUTING_ACK:
564 MM_DBG("ROUTING_ACK mode=%d\n", msg[1]);
565 audpp_cmd_cfg_adec_params(audio);
566 break;
567
568 case AUDPP_MSG_FLUSH_ACK:
569 MM_DBG("FLUSH_ACK\n");
570 audio->wflush = 0;
571 audio->rflush = 0;
572 wake_up(&audio->write_wait);
573 if (audio->pcm_feedback)
574 audio->drv_ops.buffer_refresh(audio);
575 break;
576
577 case AUDPP_MSG_PCMDMAMISSED:
578 MM_DBG("PCMDMAMISSED\n");
579 audio->teos = 1;
580 wake_up(&audio->write_wait);
581 break;
582
583 default:
584 MM_ERR("UNKNOWN (%d)\n", id);
585 }
586
587}
588
589
590struct msm_adsp_ops audplay_adsp_ops = {
591 .event = audplay_dsp_event,
592};
593
594
595#define audplay_send_queue0(audio, cmd, len) \
596 msm_adsp_write(audio->audplay, audio->queue_id, \
597 cmd, len)
598
599static int auddec_dsp_config(struct audio *audio, int enable)
600{
601 u16 cfg_dec_cmd[AUDPP_CMD_CFG_DEC_TYPE_LEN / sizeof(unsigned short)];
602
603 memset(cfg_dec_cmd, 0, sizeof(cfg_dec_cmd));
604
605 cfg_dec_cmd[0] = AUDPP_CMD_CFG_DEC_TYPE;
606 if (enable)
607 cfg_dec_cmd[1 + audio->dec_id] = AUDPP_CMD_UPDATDE_CFG_DEC |
608 AUDPP_CMD_ENA_DEC_V | AUDDEC_DEC_MP3;
609 else
610 cfg_dec_cmd[1 + audio->dec_id] = AUDPP_CMD_UPDATDE_CFG_DEC |
611 AUDPP_CMD_DIS_DEC_V;
612
613 return audpp_send_queue1(&cfg_dec_cmd, sizeof(cfg_dec_cmd));
614}
615
616static void audpp_cmd_cfg_adec_params(struct audio *audio)
617{
618 audpp_cmd_cfg_adec_params_mp3 cmd;
619
620 memset(&cmd, 0, sizeof(cmd));
621 cmd.common.cmd_id = AUDPP_CMD_CFG_ADEC_PARAMS;
622 cmd.common.length = AUDPP_CMD_CFG_ADEC_PARAMS_MP3_LEN;
623 cmd.common.dec_id = audio->dec_id;
624 cmd.common.input_sampling_frequency = audio->out_sample_rate;
625
626 audpp_send_queue2(&cmd, sizeof(cmd));
627}
628
629static void audpp_cmd_cfg_routing_mode(struct audio *audio)
630{
631 struct audpp_cmd_routing_mode cmd;
632 MM_DBG("\n"); /* Macro prints the file name and function */
633 memset(&cmd, 0, sizeof(cmd));
634 cmd.cmd_id = AUDPP_CMD_ROUTING_MODE;
635 cmd.object_number = audio->dec_id;
636 if (audio->pcm_feedback)
637 cmd.routing_mode = ROUTING_MODE_FTRT;
638 else
639 cmd.routing_mode = ROUTING_MODE_RT;
640
641 audpp_send_queue1(&cmd, sizeof(cmd));
642}
643
644static int audplay_dsp_send_data_avail(struct audio *audio,
645 unsigned idx, unsigned len)
646{
647 struct audplay_cmd_bitstream_data_avail_nt2 cmd;
648
649 cmd.cmd_id = AUDPLAY_CMD_BITSTREAM_DATA_AVAIL_NT2;
650 if (audio->mfield)
651 cmd.decoder_id = AUDMP3_METAFIELD_MASK |
652 (audio->out[idx].mfield_sz >> 1);
653 else
654 cmd.decoder_id = audio->dec_id;
655 cmd.buf_ptr = audio->out[idx].addr;
656 cmd.buf_size = len/2;
657 cmd.partition_number = 0;
658 /* complete all the writes to the input buffer */
659 wmb();
660 return audplay_send_queue0(audio, &cmd, sizeof(cmd));
661}
662
663/* Caller holds irq_lock */
664static void audmp3_async_buffer_refresh(struct audio *audio)
665{
666 struct audplay_cmd_buffer_refresh refresh_cmd;
667 struct audmp3_buffer_node *next_buf;
668
669 if (!audio->running ||
670 audio->drv_status & ADRV_STATUS_IBUF_GIVEN)
671 return;
672
673 if (!list_empty(&audio->in_queue)) {
674 next_buf = list_first_entry(&audio->in_queue,
675 struct audmp3_buffer_node, list);
676 if (!next_buf)
677 return;
678 MM_DBG("next buf %p phy %lx len %d\n", next_buf,
679 next_buf->paddr, next_buf->buf.buf_len);
680 refresh_cmd.cmd_id = AUDPLAY_CMD_BUFFER_REFRESH;
681 refresh_cmd.num_buffers = 1;
682 refresh_cmd.buf0_address = next_buf->paddr;
683 refresh_cmd.buf0_length = next_buf->buf.buf_len -
684 (next_buf->buf.buf_len % 576) +
685 (audio->mfield ? 24 : 0); /* Mp3 frame size */
686 refresh_cmd.buf_read_count = 0;
687 audio->drv_status |= ADRV_STATUS_IBUF_GIVEN;
688 (void) audplay_send_queue0(audio, &refresh_cmd,
689 sizeof(refresh_cmd));
690 }
691
692}
693
694static void audplay_buffer_refresh(struct audio *audio)
695{
696 struct audplay_cmd_buffer_refresh refresh_cmd;
697
698 refresh_cmd.cmd_id = AUDPLAY_CMD_BUFFER_REFRESH;
699 refresh_cmd.num_buffers = 1;
700 refresh_cmd.buf0_address = audio->in[audio->fill_next].addr;
701 refresh_cmd.buf0_length = audio->in[audio->fill_next].size -
702 (audio->in[audio->fill_next].size % 576) +
703 (audio->mfield ? 24 : 0); /* Mp3 frame size */
704 refresh_cmd.buf_read_count = 0;
705 MM_DBG("buf0_addr=%x buf0_len=%d\n", refresh_cmd.buf0_address,
706 refresh_cmd.buf0_length);
707 (void)audplay_send_queue0(audio, &refresh_cmd, sizeof(refresh_cmd));
708}
709
710static void audplay_config_hostpcm(struct audio *audio)
711{
712 struct audplay_cmd_hpcm_buf_cfg cfg_cmd;
713
714 MM_DBG("\n"); /* Macro prints the file name and function */
715 cfg_cmd.cmd_id = AUDPLAY_CMD_HPCM_BUF_CFG;
716 cfg_cmd.max_buffers = 1;
717 cfg_cmd.byte_swap = 0;
718 cfg_cmd.hostpcm_config = (0x8000) | (0x4000);
719 cfg_cmd.feedback_frequency = 1;
720 cfg_cmd.partition_number = 0;
721 (void)audplay_send_queue0(audio, &cfg_cmd, sizeof(cfg_cmd));
722
723}
724
725static void audmp3_async_send_data(struct audio *audio, unsigned needed)
726{
727 unsigned long flags;
728
729 spin_lock_irqsave(&audio->dsp_lock, flags);
730 if (!audio->running)
731 goto done;
732
733 if (needed && !audio->wflush) {
734 audio->out_needed = 1;
735 if (audio->drv_status & ADRV_STATUS_OBUF_GIVEN) {
736 /* pop one node out of queue */
737 union msm_audio_event_payload payload;
738 struct audmp3_buffer_node *used_buf;
739
740 MM_DBG("consumed\n");
741 BUG_ON(list_empty(&audio->out_queue));
742 used_buf = list_first_entry(&audio->out_queue,
743 struct audmp3_buffer_node, list);
744 list_del(&used_buf->list);
745 payload.aio_buf = used_buf->buf;
746 audmp3_post_event(audio, AUDIO_EVENT_WRITE_DONE,
747 payload);
748 kfree(used_buf);
749 audio->drv_status &= ~ADRV_STATUS_OBUF_GIVEN;
750 }
751
752 }
753
754 if (audio->out_needed) {
755 struct audmp3_buffer_node *next_buf;
756 struct audplay_cmd_bitstream_data_avail_nt2 cmd;
757 if (!list_empty(&audio->out_queue)) {
758 next_buf = list_first_entry(&audio->out_queue,
759 struct audmp3_buffer_node, list);
760 MM_DBG("next_buf %p\n", next_buf);
761 if (next_buf) {
762 MM_DBG("next buf phy %lx len %d\n",
763 next_buf->paddr,
764 next_buf->buf.data_len);
765
766 cmd.cmd_id =
767 AUDPLAY_CMD_BITSTREAM_DATA_AVAIL_NT2;
768 if (audio->mfield)
769 cmd.decoder_id = AUDMP3_METAFIELD_MASK |
770 (next_buf->buf.mfield_sz >> 1);
771 else
772 cmd.decoder_id = audio->dec_id;
773 cmd.buf_ptr = (unsigned) next_buf->paddr;
774 cmd.buf_size = next_buf->buf.data_len >> 1;
775 cmd.partition_number = 0;
776 /* complete the writes to the input buffer */
777 wmb();
778 audplay_send_queue0(audio, &cmd, sizeof(cmd));
779 audio->out_needed = 0;
780 audio->drv_status |= ADRV_STATUS_OBUF_GIVEN;
781 }
782 }
783 }
784
785done:
786 spin_unlock_irqrestore(&audio->dsp_lock, flags);
787}
788
789static void audplay_send_data(struct audio *audio, unsigned needed)
790{
791 struct buffer *frame;
792 unsigned long flags;
793
794 spin_lock_irqsave(&audio->dsp_lock, flags);
795 if (!audio->running)
796 goto done;
797
798 if (needed && !audio->wflush) {
799 /* We were called from the callback because the DSP
800 * requested more data. Note that the DSP does want
801 * more data, and if a buffer was in-flight, mark it
802 * as available (since the DSP must now be done with
803 * it).
804 */
805 audio->out_needed = 1;
806 frame = audio->out + audio->out_tail;
807 if (frame->used == 0xffffffff) {
808 MM_DBG("frame %d free\n", audio->out_tail);
809 frame->used = 0;
810 audio->out_tail ^= 1;
811 wake_up(&audio->write_wait);
812 }
813 }
814
815 if (audio->out_needed) {
816 /* If the DSP currently wants data and we have a
817 * buffer available, we will send it and reset
818 * the needed flag. We'll mark the buffer as in-flight
819 * so that it won't be recycled until the next buffer
820 * is requested
821 */
822
823 frame = audio->out + audio->out_tail;
824 if (frame->used) {
825 BUG_ON(frame->used == 0xffffffff);
826 MM_DBG("frame %d busy\n", audio->out_tail);
827 audplay_dsp_send_data_avail(audio, audio->out_tail,
828 frame->used);
829 frame->used = 0xffffffff;
830 audio->out_needed = 0;
831 }
832 }
833done:
834 spin_unlock_irqrestore(&audio->dsp_lock, flags);
835}
836
837/* ------------------- device --------------------- */
838static void audmp3_async_flush(struct audio *audio)
839{
840 struct audmp3_buffer_node *buf_node;
841 struct list_head *ptr, *next;
842 union msm_audio_event_payload payload;
Chaithanya Krishna Bacharaju221f7992012-05-16 09:32:54 +0530843 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700844
Chaithanya Krishna Bacharaju221f7992012-05-16 09:32:54 +0530845 spin_lock_irqsave(&audio->dsp_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700846 MM_DBG("\n"); /* Macro prints the file name and function */
847 list_for_each_safe(ptr, next, &audio->out_queue) {
848 buf_node = list_entry(ptr, struct audmp3_buffer_node, list);
849 list_del(&buf_node->list);
850 payload.aio_buf = buf_node->buf;
851 audmp3_post_event(audio, AUDIO_EVENT_WRITE_DONE,
852 payload);
853 kfree(buf_node);
854 }
855 audio->drv_status &= ~ADRV_STATUS_OBUF_GIVEN;
856 audio->out_needed = 0;
857 atomic_set(&audio->out_bytes, 0);
Chaithanya Krishna Bacharaju221f7992012-05-16 09:32:54 +0530858 spin_unlock_irqrestore(&audio->dsp_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700859}
860
861static void audio_flush(struct audio *audio)
862{
Manish Dewangana4f1df02012-02-08 17:06:54 +0530863 unsigned long flags;
864
865 spin_lock_irqsave(&audio->dsp_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700866 audio->out[0].used = 0;
867 audio->out[1].used = 0;
868 audio->out_head = 0;
869 audio->out_tail = 0;
870 audio->reserved = 0;
871 audio->out_needed = 0;
Manish Dewangana4f1df02012-02-08 17:06:54 +0530872 spin_unlock_irqrestore(&audio->dsp_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700873 atomic_set(&audio->out_bytes, 0);
874}
875
876static void audmp3_async_flush_pcm_buf(struct audio *audio)
877{
878 struct audmp3_buffer_node *buf_node;
879 struct list_head *ptr, *next;
880 union msm_audio_event_payload payload;
881
882 MM_DBG("\n"); /* Macro prints the file name and function */
883 list_for_each_safe(ptr, next, &audio->in_queue) {
884 buf_node = list_entry(ptr, struct audmp3_buffer_node, list);
885 list_del(&buf_node->list);
886 payload.aio_buf = buf_node->buf;
887 payload.aio_buf.data_len = 0;
888 audmp3_post_event(audio, AUDIO_EVENT_READ_DONE,
889 payload);
890 kfree(buf_node);
891 }
892 audio->drv_status &= ~ADRV_STATUS_IBUF_GIVEN;
893
894}
895
896static void audio_flush_pcm_buf(struct audio *audio)
897{
898 uint8_t index;
Manish Dewangana4f1df02012-02-08 17:06:54 +0530899 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700900
Manish Dewangana4f1df02012-02-08 17:06:54 +0530901 spin_lock_irqsave(&audio->dsp_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700902 for (index = 0; index < PCM_BUF_MAX_COUNT; index++)
903 audio->in[index].used = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700904 audio->buf_refresh = 0;
905 audio->read_next = 0;
906 audio->fill_next = 0;
Manish Dewangana4f1df02012-02-08 17:06:54 +0530907 spin_unlock_irqrestore(&audio->dsp_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700908}
909
910static void audio_ioport_reset(struct audio *audio)
911{
912 if (audio->drv_status & ADRV_STATUS_AIO_INTF) {
913 /* If fsync is in progress, make sure
914 * return value of fsync indicates
915 * abort due to flush
916 */
917 if (audio->drv_status & ADRV_STATUS_FSYNC) {
918 MM_DBG("fsync in progress\n");
919 wake_up(&audio->write_wait);
920 mutex_lock(&audio->write_lock);
921 audio->drv_ops.out_flush(audio);
922 mutex_unlock(&audio->write_lock);
923 } else
924 audio->drv_ops.out_flush(audio);
925 audio->drv_ops.in_flush(audio);
926 } else {
927 /* Make sure read/write thread are free from
928 * sleep and knowing that system is not able
929 * to process io request at the moment
930 */
931 wake_up(&audio->write_wait);
932 mutex_lock(&audio->write_lock);
933 audio->drv_ops.out_flush(audio);
934 mutex_unlock(&audio->write_lock);
935 wake_up(&audio->read_wait);
936 mutex_lock(&audio->read_lock);
937 audio->drv_ops.in_flush(audio);
938 mutex_unlock(&audio->read_lock);
939 }
940}
941
942static int audmp3_events_pending(struct audio *audio)
943{
944 unsigned long flags;
945 int empty;
946
947 spin_lock_irqsave(&audio->event_queue_lock, flags);
948 empty = !list_empty(&audio->event_queue);
949 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
950 return empty || audio->event_abort;
951}
952
953static void audmp3_reset_event_queue(struct audio *audio)
954{
955 unsigned long flags;
956 struct audmp3_event *drv_evt;
957 struct list_head *ptr, *next;
958
959 spin_lock_irqsave(&audio->event_queue_lock, flags);
960 list_for_each_safe(ptr, next, &audio->event_queue) {
961 drv_evt = list_first_entry(&audio->event_queue,
962 struct audmp3_event, list);
963 list_del(&drv_evt->list);
964 kfree(drv_evt);
965 }
966 list_for_each_safe(ptr, next, &audio->free_event_queue) {
967 drv_evt = list_first_entry(&audio->free_event_queue,
968 struct audmp3_event, list);
969 list_del(&drv_evt->list);
970 kfree(drv_evt);
971 }
972 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
973
974 return;
975}
976
977static long audmp3_process_event_req(struct audio *audio, void __user *arg)
978{
979 long rc;
980 struct msm_audio_event usr_evt;
981 struct audmp3_event *drv_evt = NULL;
982 int timeout;
983 unsigned long flags;
984
985 if (copy_from_user(&usr_evt, arg, sizeof(struct msm_audio_event)))
986 return -EFAULT;
987
988 timeout = (int) usr_evt.timeout_ms;
989
990 if (timeout > 0) {
991 rc = wait_event_interruptible_timeout(
992 audio->event_wait, audmp3_events_pending(audio),
993 msecs_to_jiffies(timeout));
994 if (rc == 0)
995 return -ETIMEDOUT;
996 } else {
997 rc = wait_event_interruptible(
998 audio->event_wait, audmp3_events_pending(audio));
999 }
1000
1001 if (rc < 0)
1002 return rc;
1003
1004 if (audio->event_abort) {
1005 audio->event_abort = 0;
1006 return -ENODEV;
1007 }
1008
1009 rc = 0;
1010
1011 spin_lock_irqsave(&audio->event_queue_lock, flags);
1012 if (!list_empty(&audio->event_queue)) {
1013 drv_evt = list_first_entry(&audio->event_queue,
1014 struct audmp3_event, list);
1015 list_del(&drv_evt->list);
1016 }
1017 if (drv_evt) {
1018 usr_evt.event_type = drv_evt->event_type;
1019 usr_evt.event_payload = drv_evt->payload;
1020 list_add_tail(&drv_evt->list, &audio->free_event_queue);
1021 } else
1022 rc = -1;
1023 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
1024
1025 if (drv_evt->event_type == AUDIO_EVENT_WRITE_DONE ||
1026 drv_evt->event_type == AUDIO_EVENT_READ_DONE) {
1027 mutex_lock(&audio->lock);
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301028 audmp3_ion_fixup(audio, drv_evt->payload.aio_buf.buf_addr,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001029 drv_evt->payload.aio_buf.buf_len, 0);
1030 mutex_unlock(&audio->lock);
1031 }
1032
1033 /* order reads from the output buffer */
1034 if (drv_evt->event_type == AUDIO_EVENT_READ_DONE)
1035 rmb();
1036
1037 if (!rc && copy_to_user(arg, &usr_evt, sizeof(usr_evt)))
1038 rc = -EFAULT;
1039
1040 return rc;
1041}
1042
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301043static int audmp3_ion_check(struct audio *audio,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001044 void *vaddr, unsigned long len)
1045{
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301046 struct audmp3_ion_region *region_elt;
1047 struct audmp3_ion_region t = { .vaddr = vaddr, .len = len };
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001048
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301049 list_for_each_entry(region_elt, &audio->ion_region_queue, list) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001050 if (CONTAINS(region_elt, &t) || CONTAINS(&t, region_elt) ||
1051 OVERLAPS(region_elt, &t)) {
1052 MM_ERR("region (vaddr %p len %ld)"
1053 " clashes with registered region"
1054 " (vaddr %p paddr %p len %ld)\n",
1055 vaddr, len,
1056 region_elt->vaddr,
1057 (void *)region_elt->paddr,
1058 region_elt->len);
1059 return -EINVAL;
1060 }
1061 }
1062
1063 return 0;
1064}
1065
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301066static int audmp3_ion_add(struct audio *audio,
1067 struct msm_audio_ion_info *info)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001068{
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301069 ion_phys_addr_t paddr;
1070 size_t len;
1071 unsigned long kvaddr;
1072 struct audmp3_ion_region *region;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001073 int rc = -EINVAL;
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301074 struct ion_handle *handle;
1075 unsigned long ionflag;
1076 void *temp_ptr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001077
1078 MM_DBG("\n"); /* Macro prints the file name and function */
1079 region = kmalloc(sizeof(*region), GFP_KERNEL);
1080
1081 if (!region) {
1082 rc = -ENOMEM;
1083 goto end;
1084 }
1085
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301086 handle = ion_import_dma_buf(audio->client, info->fd);
1087 if (IS_ERR_OR_NULL(handle)) {
1088 pr_err("%s: could not get handle of the given fd\n", __func__);
1089 goto import_error;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001090 }
1091
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301092 rc = ion_handle_get_flags(audio->client, handle, &ionflag);
1093 if (rc) {
1094 pr_err("%s: could not get flags for the handle\n", __func__);
1095 goto flag_error;
1096 }
1097
Mitchel Humpherys911b4b72012-09-12 14:42:50 -07001098 temp_ptr = ion_map_kernel(audio->client, handle);
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301099 if (IS_ERR_OR_NULL(temp_ptr)) {
1100 pr_err("%s: could not get virtual address\n", __func__);
1101 goto map_error;
1102 }
1103 kvaddr = (unsigned long) temp_ptr;
1104
1105 rc = ion_phys(audio->client, handle, &paddr, &len);
1106 if (rc) {
1107 pr_err("%s: could not get physical address\n", __func__);
1108 goto ion_error;
1109 }
1110
1111 rc = audmp3_ion_check(audio, info->vaddr, len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001112 if (rc < 0) {
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301113 MM_ERR("audpcm_ion_check failed\n");
1114 goto ion_error;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001115 }
1116
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301117 region->handle = handle;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001118 region->vaddr = info->vaddr;
1119 region->fd = info->fd;
1120 region->paddr = paddr;
1121 region->kvaddr = kvaddr;
1122 region->len = len;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001123 region->ref_cnt = 0;
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301124 MM_DBG("[%p]:add region paddr %lx vaddr %p, len %lu kvaddr %lx\n",
1125 audio, region->paddr, region->vaddr,
1126 region->len, region->kvaddr);
1127 list_add_tail(&region->list, &audio->ion_region_queue);
1128 return rc;
1129
1130ion_error:
1131 ion_unmap_kernel(audio->client, handle);
1132map_error:
1133flag_error:
1134 ion_free(audio->client, handle);
1135import_error:
1136 kfree(region);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001137end:
1138 return rc;
1139}
1140
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301141static int audmp3_ion_remove(struct audio *audio,
1142 struct msm_audio_ion_info *info)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001143{
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301144 struct audmp3_ion_region *region;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001145 struct list_head *ptr, *next;
1146 int rc = -EINVAL;
1147
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301148 list_for_each_safe(ptr, next, &audio->ion_region_queue) {
1149 region = list_entry(ptr, struct audmp3_ion_region, list);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001150
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301151 if (region != NULL && (region->fd == info->fd) &&
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001152 (region->vaddr == info->vaddr)) {
1153 if (region->ref_cnt) {
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301154 MM_DBG("%s[%p]:region %p in use ref_cnt %d\n",
1155 __func__, audio, region,
1156 region->ref_cnt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001157 break;
1158 }
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301159 MM_DBG("remove region fd %d vaddr %p\n",
1160 info->fd, info->vaddr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001161 list_del(&region->list);
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301162 ion_unmap_kernel(audio->client, region->handle);
1163 ion_free(audio->client, region->handle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001164 kfree(region);
1165 rc = 0;
1166 break;
1167 }
1168 }
1169
1170 return rc;
1171}
1172
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301173static int audmp3_ion_lookup_vaddr(struct audio *audio, void *addr,
1174 unsigned long len, struct audmp3_ion_region **region)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001175{
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301176 struct audmp3_ion_region *region_elt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001177 int match_count = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001178 *region = NULL;
1179
1180 /* returns physical address or zero */
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301181 list_for_each_entry(region_elt, &audio->ion_region_queue, list) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001182 if (addr >= region_elt->vaddr &&
1183 addr < region_elt->vaddr + region_elt->len &&
1184 addr + len <= region_elt->vaddr + region_elt->len) {
1185 /* offset since we could pass vaddr inside a registerd
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301186 * ion buffer
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001187 */
1188
1189 match_count++;
1190 if (!*region)
1191 *region = region_elt;
1192 }
1193 }
1194
1195 if (match_count > 1) {
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301196 MM_ERR("%s[%p]:multiple hits for vaddr %p, len %ld\n",
1197 __func__, audio, addr, len);
1198 list_for_each_entry(region_elt, &audio->ion_region_queue,
1199 list) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001200 if (addr >= region_elt->vaddr &&
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301201 addr < region_elt->vaddr + region_elt->len &&
1202 addr + len <= region_elt->vaddr + region_elt->len)
1203 MM_ERR("\t%s[%p]:%p, %ld --> %p\n",
1204 __func__, audio,
1205 region_elt->vaddr,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001206 region_elt->len,
1207 (void *)region_elt->paddr);
1208 }
1209 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001210 return *region ? 0 : -1;
1211}
1212
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301213unsigned long audmp3_ion_fixup(struct audio *audio, void *addr,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001214 unsigned long len, int ref_up)
1215{
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301216 struct audmp3_ion_region *region;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001217 unsigned long paddr;
1218 int ret;
1219
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301220 ret = audmp3_ion_lookup_vaddr(audio, addr, len, &region);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001221 if (ret) {
1222 MM_ERR("lookup (%p, %ld) failed\n", addr, len);
1223 return 0;
1224 }
1225 if (ref_up)
1226 region->ref_cnt++;
1227 else
1228 region->ref_cnt--;
1229 MM_DBG("found region %p ref_cnt %d\n", region, region->ref_cnt);
1230 paddr = region->paddr + (addr - region->vaddr);
1231 return paddr;
1232}
1233
1234/* audio -> lock must be held at this point */
1235static int audmp3_aio_buf_add(struct audio *audio, unsigned dir,
1236 void __user *arg)
1237{
1238 unsigned long flags;
1239 struct audmp3_buffer_node *buf_node;
1240
1241 buf_node = kmalloc(sizeof(*buf_node), GFP_KERNEL);
1242
1243 if (!buf_node)
1244 return -ENOMEM;
1245
1246 if (copy_from_user(&buf_node->buf, arg, sizeof(buf_node->buf))) {
1247 kfree(buf_node);
1248 return -EFAULT;
1249 }
1250
1251 MM_DBG("node %p dir %x buf_addr %p buf_len %d data_len \
1252 %d\n", buf_node, dir,
1253 buf_node->buf.buf_addr, buf_node->buf.buf_len,
1254 buf_node->buf.data_len);
1255
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301256 buf_node->paddr = audmp3_ion_fixup(
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001257 audio, buf_node->buf.buf_addr,
1258 buf_node->buf.buf_len, 1);
1259
1260 if (dir) {
1261 /* write */
1262 if (!buf_node->paddr ||
1263 (buf_node->paddr & 0x1) ||
1264 (buf_node->buf.data_len & 0x1) ||
1265 (!audio->pcm_feedback &&
1266 !buf_node->buf.data_len)) {
1267 kfree(buf_node);
1268 return -EINVAL;
1269 }
1270 spin_lock_irqsave(&audio->dsp_lock, flags);
1271 list_add_tail(&buf_node->list, &audio->out_queue);
1272 spin_unlock_irqrestore(&audio->dsp_lock, flags);
1273 audio->drv_ops.send_data(audio, 0);
1274 } else {
1275 /* read */
1276 if (!buf_node->paddr ||
1277 (buf_node->paddr & 0x1) ||
1278 (buf_node->buf.buf_len < PCM_BUFSZ_MIN)) {
1279 kfree(buf_node);
1280 return -EINVAL;
1281 }
1282 spin_lock_irqsave(&audio->dsp_lock, flags);
1283 list_add_tail(&buf_node->list, &audio->in_queue);
1284 audio->drv_ops.buffer_refresh(audio);
1285 spin_unlock_irqrestore(&audio->dsp_lock, flags);
1286 }
1287
1288 MM_DBG("Add buf_node %p paddr %lx\n", buf_node, buf_node->paddr);
1289
1290 return 0;
1291}
1292
1293static int audio_enable_eq(struct audio *audio, int enable)
1294{
1295 if (audio->eq_enable == enable && !audio->eq_needs_commit)
1296 return 0;
1297
1298 audio->eq_enable = enable;
1299
1300 if (audio->running) {
1301 audpp_dsp_set_eq(audio->dec_id, enable, &audio->eq);
1302 audio->eq_needs_commit = 0;
1303 }
1304 return 0;
1305}
1306
1307static long audio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1308{
1309 struct audio *audio = file->private_data;
1310 int rc = -EINVAL;
1311 unsigned long flags = 0;
1312 uint16_t enable_mask;
1313 int enable;
1314 int prev_state;
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301315 unsigned long ionflag = 0;
1316 ion_phys_addr_t addr = 0;
1317 struct ion_handle *handle = NULL;
1318 int len = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001319
1320 MM_DBG("cmd = %d\n", cmd);
1321
1322 if (cmd == AUDIO_GET_STATS) {
1323 struct msm_audio_stats stats;
1324 stats.byte_count = audpp_avsync_byte_count(audio->dec_id);
1325 stats.sample_count = audpp_avsync_sample_count(audio->dec_id);
1326 if (copy_to_user((void *) arg, &stats, sizeof(stats)))
1327 return -EFAULT;
1328 return 0;
1329 }
1330
1331 switch (cmd) {
1332 case AUDIO_ENABLE_AUDPP:
1333 if (copy_from_user(&enable_mask, (void *) arg,
1334 sizeof(enable_mask))) {
1335 rc = -EFAULT;
1336 break;
1337 }
1338
1339 spin_lock_irqsave(&audio->dsp_lock, flags);
1340 enable = (enable_mask & EQ_ENABLE) ? 1 : 0;
1341 audio_enable_eq(audio, enable);
1342 spin_unlock_irqrestore(&audio->dsp_lock, flags);
1343 rc = 0;
1344 break;
1345 case AUDIO_SET_VOLUME:
1346 spin_lock_irqsave(&audio->dsp_lock, flags);
1347 audio->vol_pan.volume = arg;
1348 if (audio->running)
1349 audpp_dsp_set_vol_pan(audio->dec_id, &audio->vol_pan);
1350 spin_unlock_irqrestore(&audio->dsp_lock, flags);
1351 rc = 0;
1352 break;
1353
1354 case AUDIO_SET_PAN:
1355 spin_lock_irqsave(&audio->dsp_lock, flags);
1356 audio->vol_pan.pan = arg;
1357 if (audio->running)
1358 audpp_dsp_set_vol_pan(audio->dec_id, &audio->vol_pan);
1359 spin_unlock_irqrestore(&audio->dsp_lock, flags);
1360 rc = 0;
1361 break;
1362
1363 case AUDIO_SET_EQ:
1364 prev_state = audio->eq_enable;
1365 audio->eq_enable = 0;
1366 if (copy_from_user(&audio->eq.num_bands, (void *) arg,
1367 sizeof(audio->eq) -
1368 (AUDPP_CMD_CFG_OBJECT_PARAMS_COMMON_LEN + 2))) {
1369 rc = -EFAULT;
1370 break;
1371 }
1372 audio->eq_enable = prev_state;
1373 audio->eq_needs_commit = 1;
1374 rc = 0;
1375 break;
1376 }
1377
1378 if (-EINVAL != rc)
1379 return rc;
1380
1381 if (cmd == AUDIO_GET_EVENT) {
1382 MM_DBG(" AUDIO_GET_EVENT\n");
1383 if (mutex_trylock(&audio->get_event_lock)) {
1384 rc = audmp3_process_event_req(audio,
1385 (void __user *) arg);
1386 mutex_unlock(&audio->get_event_lock);
1387 } else
1388 rc = -EBUSY;
1389 return rc;
1390 }
1391
1392 if (cmd == AUDIO_ABORT_GET_EVENT) {
1393 audio->event_abort = 1;
1394 wake_up(&audio->event_wait);
1395 return 0;
1396 }
1397
1398 mutex_lock(&audio->lock);
1399 switch (cmd) {
1400 case AUDIO_START:
1401 MM_DBG("AUDIO_START\n");
1402 rc = audio_enable(audio);
1403 if (!rc) {
1404 rc = wait_event_interruptible_timeout(audio->wait,
1405 audio->dec_state != MSM_AUD_DECODER_STATE_NONE,
1406 msecs_to_jiffies(MSM_AUD_DECODER_WAIT_MS));
1407 MM_INFO("dec_state %d rc = %d\n", audio->dec_state, rc);
1408
1409 if (audio->dec_state != MSM_AUD_DECODER_STATE_SUCCESS)
1410 rc = -ENODEV;
1411 else
1412 rc = 0;
1413 }
1414 break;
1415 case AUDIO_STOP:
1416 MM_DBG("AUDIO_STOP\n");
1417 rc = audio_disable(audio);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001418 audio_ioport_reset(audio);
1419 audio->stopped = 0;
1420 break;
1421 case AUDIO_FLUSH:
1422 MM_DBG("AUDIO_FLUSH\n");
1423 audio->rflush = 1;
1424 audio->wflush = 1;
1425 audio_ioport_reset(audio);
1426 if (audio->running) {
1427 audpp_flush(audio->dec_id);
1428 rc = wait_event_interruptible(audio->write_wait,
1429 !audio->wflush);
1430 if (rc < 0) {
1431 MM_ERR("AUDIO_FLUSH interrupted\n");
1432 rc = -EINTR;
1433 }
1434 } else {
1435 audio->rflush = 0;
1436 audio->wflush = 0;
1437 }
1438 break;
1439
1440 case AUDIO_SET_CONFIG: {
1441 struct msm_audio_config config;
1442 if (copy_from_user(&config, (void *) arg, sizeof(config))) {
1443 rc = -EFAULT;
1444 break;
1445 }
1446 if (config.channel_count == 1) {
1447 config.channel_count = AUDPP_CMD_PCM_INTF_MONO_V;
1448 } else if (config.channel_count == 2) {
1449 config.channel_count = AUDPP_CMD_PCM_INTF_STEREO_V;
1450 } else {
1451 rc = -EINVAL;
1452 break;
1453 }
1454 audio->mfield = config.meta_field;
1455 audio->out_sample_rate = config.sample_rate;
1456 audio->out_channel_mode = config.channel_count;
1457 rc = 0;
1458 break;
1459 }
1460 case AUDIO_GET_CONFIG: {
1461 struct msm_audio_config config;
1462 config.buffer_size = (audio->out_dma_sz >> 1);
1463 config.buffer_count = 2;
1464 config.sample_rate = audio->out_sample_rate;
1465 if (audio->out_channel_mode == AUDPP_CMD_PCM_INTF_MONO_V) {
1466 config.channel_count = 1;
1467 } else {
1468 config.channel_count = 2;
1469 }
1470 config.meta_field = 0;
1471 config.unused[0] = 0;
1472 config.unused[1] = 0;
1473 config.unused[2] = 0;
1474 if (copy_to_user((void *) arg, &config, sizeof(config))) {
1475 rc = -EFAULT;
1476 } else {
1477 rc = 0;
1478 }
1479 break;
1480 }
1481 case AUDIO_GET_PCM_CONFIG:{
1482 struct msm_audio_pcm_config config;
1483 config.pcm_feedback = audio->pcm_feedback;
1484 config.buffer_count = PCM_BUF_MAX_COUNT;
1485 config.buffer_size = PCM_BUFSZ_MIN;
1486 if (copy_to_user((void *)arg, &config,
1487 sizeof(config)))
1488 rc = -EFAULT;
1489 else
1490 rc = 0;
1491 break;
1492 }
1493 case AUDIO_SET_PCM_CONFIG:{
1494 struct msm_audio_pcm_config config;
1495 if (copy_from_user
1496 (&config, (void *)arg, sizeof(config))) {
1497 rc = -EFAULT;
1498 break;
1499 }
1500
1501 if (config.pcm_feedback != audio->pcm_feedback) {
1502 MM_ERR("Not sufficient permission to"
1503 "change the playback mode\n");
1504 rc = -EACCES;
1505 break;
1506 }
1507 if (audio->drv_status & ADRV_STATUS_AIO_INTF) {
1508 rc = 0;
1509 break;
1510 }
1511
1512 if ((config.buffer_count > PCM_BUF_MAX_COUNT) ||
1513 (config.buffer_count == 1))
1514 config.buffer_count = PCM_BUF_MAX_COUNT;
1515
1516 if (config.buffer_size < PCM_BUFSZ_MIN)
1517 config.buffer_size = PCM_BUFSZ_MIN;
1518
1519 /* Check if pcm feedback is required */
1520 if ((config.pcm_feedback) && (!audio->read_data)) {
1521 MM_DBG("allocate PCM buffer %d\n",
1522 config.buffer_count *
1523 config.buffer_size);
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301524
1525 handle = ion_alloc(audio->client,
1526 (config.buffer_size *
1527 config.buffer_count),
Hanumant Singh7d72bad2012-08-29 18:39:44 -07001528 SZ_4K, ION_HEAP(ION_AUDIO_HEAP_ID), 0);
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301529 if (IS_ERR_OR_NULL(handle)) {
1530 MM_ERR("Unable to alloc I/P buffs\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001531 rc = -ENOMEM;
1532 break;
1533 }
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301534
1535 audio->input_buff_handle = handle;
1536
1537 rc = ion_phys(audio->client ,
1538 handle, &addr, &len);
1539 if (rc) {
1540 MM_ERR("Invalid phy: %x sz: %x\n",
1541 (unsigned int) addr,
1542 (unsigned int) len);
1543 rc = -ENOMEM;
1544 break;
1545 } else {
1546 MM_INFO("Got valid phy: %x sz: %x\n",
1547 (unsigned int) audio->read_phys,
1548 (unsigned int) len);
1549 }
1550 audio->read_phys = (int32_t)addr;
1551
1552 rc = ion_handle_get_flags(audio->client,
1553 handle, &ionflag);
1554 if (rc) {
1555 MM_ERR("could not get flags\n");
1556 rc = -ENOMEM;
1557 break;
1558 }
1559
1560 audio->map_v_read = ion_map_kernel(
Mitchel Humpherys911b4b72012-09-12 14:42:50 -07001561 audio->client, handle);
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,
Hanumant Singh7d72bad2012-08-29 18:39:44 -07002256 ION_HEAP(ION_AUDIO_HEAP_ID), 0);
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05302257 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
Mitchel Humpherys911b4b72012-09-12 14:42:50 -07002282 audio->map_v_write = ion_map_kernel(client, handle);
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05302283 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);
Chaithanya Krishna Bacharajud0989382012-09-03 18:34:49 +05302300 if (!(file->f_flags & O_NONBLOCK))
2301 goto err;
2302 else
2303 goto resource_err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002304 }
2305 }
2306
2307 rc = msm_adsp_get(audio->module_name, &audio->audplay,
2308 &audplay_adsp_ops, audio);
2309
2310 if (rc) {
2311 MM_ERR("failed to get %s module, freeing instance 0x%08x\n",
2312 audio->module_name, (int)audio);
2313 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK)
2314 audmgr_close(&audio->audmgr);
Chaithanya Krishna Bacharajud0989382012-09-03 18:34:49 +05302315 if (!(file->f_flags & O_NONBLOCK))
2316 goto err;
2317 else
2318 goto resource_err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002319 }
2320
2321 rc = rmt_get_resource(audio);
2322 if (rc) {
2323 MM_ERR("ADSP resources are not available for MP3 session \
2324 0x%08x on decoder: %d\n", (int)audio, audio->dec_id);
2325 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK)
2326 audmgr_close(&audio->audmgr);
2327 msm_adsp_put(audio->audplay);
Chaithanya Krishna Bacharajud0989382012-09-03 18:34:49 +05302328 if (!(file->f_flags & O_NONBLOCK))
2329 goto err;
2330 else
2331 goto resource_err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002332 }
2333
2334 if (file->f_flags & O_NONBLOCK) {
2335 MM_DBG("set to aio interface\n");
2336 audio->drv_status |= ADRV_STATUS_AIO_INTF;
2337 audio->drv_ops.pcm_buf_update = audmp3_async_pcm_buf_update;
2338 audio->drv_ops.buffer_refresh = audmp3_async_buffer_refresh;
2339 audio->drv_ops.send_data = audmp3_async_send_data;
2340 audio->drv_ops.out_flush = audmp3_async_flush;
2341 audio->drv_ops.in_flush = audmp3_async_flush_pcm_buf;
2342 audio->drv_ops.fsync = audmp3_async_fsync;
2343 } else {
2344 MM_DBG("set to std io interface\n");
2345 audio->drv_ops.pcm_buf_update = audio_update_pcm_buf_entry;
2346 audio->drv_ops.buffer_refresh = audplay_buffer_refresh;
2347 audio->drv_ops.send_data = audplay_send_data;
2348 audio->drv_ops.out_flush = audio_flush;
2349 audio->drv_ops.in_flush = audio_flush_pcm_buf;
2350 audio->drv_ops.fsync = audmp3_sync_fsync;
2351 audio->out[0].data = audio->data + 0;
2352 audio->out[0].addr = audio->phys + 0;
2353 audio->out[0].size = (audio->out_dma_sz >> 1);
2354
2355 audio->out[1].data = audio->data + audio->out[0].size;
2356 audio->out[1].addr = audio->phys + audio->out[0].size;
2357 audio->out[1].size = audio->out[0].size;
2358 }
2359
2360 /* Initialize all locks of audio instance */
2361 mutex_init(&audio->lock);
2362 mutex_init(&audio->write_lock);
2363 mutex_init(&audio->read_lock);
2364 mutex_init(&audio->get_event_lock);
2365 spin_lock_init(&audio->dsp_lock);
2366 init_waitqueue_head(&audio->write_wait);
2367 init_waitqueue_head(&audio->read_wait);
2368 INIT_LIST_HEAD(&audio->out_queue);
2369 INIT_LIST_HEAD(&audio->in_queue);
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05302370 INIT_LIST_HEAD(&audio->ion_region_queue);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002371 INIT_LIST_HEAD(&audio->free_event_queue);
2372 INIT_LIST_HEAD(&audio->event_queue);
2373 init_waitqueue_head(&audio->wait);
2374 init_waitqueue_head(&audio->event_wait);
2375 spin_lock_init(&audio->event_queue_lock);
2376
2377 audio->out_sample_rate = 44100;
2378 audio->out_channel_mode = AUDPP_CMD_PCM_INTF_STEREO_V;
2379 audio->vol_pan.volume = 0x2000;
2380
2381 audio->drv_ops.out_flush(audio);
2382
2383 file->private_data = audio;
2384 audio->opened = 1;
2385#ifdef CONFIG_DEBUG_FS
2386 snprintf(name, sizeof name, "msm_mp3_%04x", audio->dec_id);
2387 audio->dentry = debugfs_create_file(name, S_IFREG | S_IRUGO,
2388 NULL, (void *) audio, &audmp3_debug_fops);
2389
2390 if (IS_ERR(audio->dentry))
2391 MM_DBG("debugfs_create_file failed\n");
2392#endif
2393#ifdef CONFIG_HAS_EARLYSUSPEND
2394 audio->suspend_ctl.node.level = EARLY_SUSPEND_LEVEL_DISABLE_FB;
2395 audio->suspend_ctl.node.resume = audmp3_resume;
2396 audio->suspend_ctl.node.suspend = audmp3_suspend;
2397 audio->suspend_ctl.audio = audio;
2398 register_early_suspend(&audio->suspend_ctl.node);
2399#endif
2400 for (i = 0; i < AUDMP3_EVENT_NUM; i++) {
2401 e_node = kmalloc(sizeof(struct audmp3_event), GFP_KERNEL);
2402 if (e_node)
2403 list_add_tail(&e_node->list, &audio->free_event_queue);
2404 else {
2405 MM_ERR("event pkt alloc failed\n");
2406 break;
2407 }
2408 }
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05302409
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002410done:
2411 return rc;
2412err:
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05302413 ion_unmap_kernel(client, audio->output_buff_handle);
2414output_buff_map_error:
2415output_buff_get_flags_error:
2416output_buff_get_phys_error:
2417 ion_free(client, audio->output_buff_handle);
2418output_buff_alloc_error:
2419 ion_client_destroy(client);
2420client_create_error:
Chaithanya Krishna Bacharajud0989382012-09-03 18:34:49 +05302421resource_err:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002422 audpp_adec_free(audio->dec_id);
2423 kfree(audio);
2424 return rc;
2425}
2426
2427static const struct file_operations audio_mp3_fops = {
2428 .owner = THIS_MODULE,
2429 .open = audio_open,
2430 .release = audio_release,
2431 .read = audio_read,
2432 .write = audio_write,
2433 .unlocked_ioctl = audio_ioctl,
2434 .fsync = audmp3_fsync,
2435};
2436
2437struct miscdevice audio_mp3_misc = {
2438 .minor = MISC_DYNAMIC_MINOR,
2439 .name = "msm_mp3",
2440 .fops = &audio_mp3_fops,
2441};
2442
2443static int __init audio_init(void)
2444{
2445 return misc_register(&audio_mp3_misc);
2446}
2447
2448static void __exit audio_exit(void)
2449{
2450 misc_deregister(&audio_mp3_misc);
2451}
2452
2453module_init(audio_init);
2454module_exit(audio_exit);
2455
2456MODULE_DESCRIPTION("MSM MP3 driver");
2457MODULE_LICENSE("GPL v2");