blob: 7f72e250140543135f3b2df66635eb8ce36700c0 [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>
34#include <linux/android_pmem.h>
35#include <linux/slab.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070036#include <linux/msm_audio.h>
Santosh Mardi0be3b8e2011-07-06 10:00:21 +053037#include <linux/memory_alloc.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
139struct audmp3_pmem_region {
140 struct list_head list;
141 struct file *file;
142 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
244 struct list_head pmem_region_queue; /* protected by lock */
245 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;
251};
252
253static int auddec_dsp_config(struct audio *audio, int enable);
254static void audpp_cmd_cfg_adec_params(struct audio *audio);
255static void audpp_cmd_cfg_routing_mode(struct audio *audio);
256static void audplay_send_data(struct audio *audio, unsigned needed);
257static void audplay_config_hostpcm(struct audio *audio);
258static void audplay_buffer_refresh(struct audio *audio);
259static void audio_dsp_event(void *private, unsigned id, uint16_t *msg);
260static void audmp3_post_event(struct audio *audio, int type,
261 union msm_audio_event_payload payload);
262static unsigned long audmp3_pmem_fixup(struct audio *audio, void *addr,
263 unsigned long len, int ref_up);
264
265static int rmt_put_resource(struct audio *audio)
266{
267 struct aud_codec_config_cmd cmd;
268 unsigned short client_idx;
269
270 cmd.cmd_id = RM_CMD_AUD_CODEC_CFG;
271 cmd.client_id = RM_AUD_CLIENT_ID;
272 cmd.task_id = audio->dec_id;
273 cmd.enable = RMT_DISABLE;
274 cmd.dec_type = AUDDEC_DEC_MP3;
275 client_idx = ((cmd.client_id << 8) | cmd.task_id);
276
277 return put_adsp_resource(client_idx, &cmd, sizeof(cmd));
278}
279
280static int rmt_get_resource(struct audio *audio)
281{
282 struct aud_codec_config_cmd cmd;
283 unsigned short client_idx;
284
285 cmd.cmd_id = RM_CMD_AUD_CODEC_CFG;
286 cmd.client_id = RM_AUD_CLIENT_ID;
287 cmd.task_id = audio->dec_id;
288 cmd.enable = RMT_ENABLE;
289 cmd.dec_type = AUDDEC_DEC_MP3;
290 client_idx = ((cmd.client_id << 8) | cmd.task_id);
291
292 return get_adsp_resource(client_idx, &cmd, sizeof(cmd));
293}
294
295/* must be called with audio->lock held */
296static int audio_enable(struct audio *audio)
297{
298 struct audmgr_config cfg;
299 int rc;
300
301 MM_DBG("\n"); /* Macro prints the file name and function */
302
303 if (audio->enabled)
304 return 0;
305
306 if (audio->rmt_resource_released == 1) {
307 audio->rmt_resource_released = 0;
308 rc = rmt_get_resource(audio);
309 if (rc) {
310 MM_ERR("ADSP resources are not available for MP3 \
311 session 0x%08x on decoder: %d\n Ignoring \
312 error and going ahead with the playback\n",
313 (int)audio, audio->dec_id);
314 }
315 }
316
317 audio->dec_state = MSM_AUD_DECODER_STATE_NONE;
318 audio->out_tail = 0;
319 audio->out_needed = 0;
320
321 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK) {
322 cfg.tx_rate = RPC_AUD_DEF_SAMPLE_RATE_NONE;
323 cfg.rx_rate = RPC_AUD_DEF_SAMPLE_RATE_48000;
324 cfg.def_method = RPC_AUD_DEF_METHOD_PLAYBACK;
325 cfg.codec = RPC_AUD_DEF_CODEC_MP3;
326 cfg.snd_method = RPC_SND_METHOD_MIDI;
327
328 rc = audmgr_enable(&audio->audmgr, &cfg);
329 if (rc < 0)
330 return rc;
331 }
332
333 if (msm_adsp_enable(audio->audplay)) {
334 MM_ERR("msm_adsp_enable(audplay) failed\n");
335 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK)
336 audmgr_disable(&audio->audmgr);
337 return -ENODEV;
338 }
339
340 if (audpp_enable(audio->dec_id, audio_dsp_event, audio)) {
341 MM_ERR("audpp_enable() failed\n");
342 msm_adsp_disable(audio->audplay);
343 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK)
344 audmgr_disable(&audio->audmgr);
345 return -ENODEV;
346 }
347
348 audio->enabled = 1;
349 return 0;
350}
351
352/* must be called with audio->lock held */
353static int audio_disable(struct audio *audio)
354{
355 int rc = 0;
356 MM_DBG("\n"); /* Macro prints the file name and function */
357 if (audio->enabled) {
358 audio->enabled = 0;
359 audio->dec_state = MSM_AUD_DECODER_STATE_NONE;
360 auddec_dsp_config(audio, 0);
361 rc = wait_event_interruptible_timeout(audio->wait,
362 audio->dec_state != MSM_AUD_DECODER_STATE_NONE,
363 msecs_to_jiffies(MSM_AUD_DECODER_WAIT_MS));
364 if (rc == 0)
365 rc = -ETIMEDOUT;
366 else if (audio->dec_state != MSM_AUD_DECODER_STATE_CLOSE)
367 rc = -EFAULT;
368 else
369 rc = 0;
Manish Dewangan89a9f232012-02-09 17:14:40 +0530370 audio->stopped = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700371 wake_up(&audio->write_wait);
372 wake_up(&audio->read_wait);
373 msm_adsp_disable(audio->audplay);
374 audpp_disable(audio->dec_id, audio);
375 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK)
376 audmgr_disable(&audio->audmgr);
377 audio->out_needed = 0;
378 rmt_put_resource(audio);
379 audio->rmt_resource_released = 1;
380 }
381 return rc;
382}
383
384/* ------------------- dsp --------------------- */
385static void audmp3_async_pcm_buf_update(struct audio *audio, uint32_t *payload)
386{
387 unsigned long flags;
388 union msm_audio_event_payload event_payload;
389 struct audmp3_buffer_node *filled_buf;
390 uint8_t index;
391
392 if (audio->rflush)
393 return;
394
395 spin_lock_irqsave(&audio->dsp_lock, flags);
396 for (index = 0; index < payload[1]; index++) {
397 BUG_ON(list_empty(&audio->in_queue));
398 filled_buf = list_first_entry(&audio->in_queue,
399 struct audmp3_buffer_node, list);
400 if (filled_buf->paddr == payload[2 + index * 2]) {
401 list_del(&filled_buf->list);
402 event_payload.aio_buf = filled_buf->buf;
403 event_payload.aio_buf.data_len =
404 payload[3 + index * 2];
405 MM_DBG("pcm buf %p data_len %d\n", filled_buf,
406 event_payload.aio_buf.data_len);
407 audmp3_post_event(audio, AUDIO_EVENT_READ_DONE,
408 event_payload);
409 kfree(filled_buf);
410 } else {
411 MM_ERR("expected=%lx ret=%x\n", filled_buf->paddr,
412 payload[2 + index * 2]);
413 break;
414 }
415 }
416
417 audio->drv_status &= ~ADRV_STATUS_IBUF_GIVEN;
418 audio->drv_ops.buffer_refresh(audio);
419 spin_unlock_irqrestore(&audio->dsp_lock, flags);
420
421}
422
423static void audio_update_pcm_buf_entry(struct audio *audio, uint32_t *payload)
424{
425 uint8_t index;
426 unsigned long flags;
427
428 if (audio->rflush)
429 return;
430
431 spin_lock_irqsave(&audio->dsp_lock, flags);
432 for (index = 0; index < payload[1]; index++) {
433 if (audio->in[audio->fill_next].addr ==
434 payload[2 + index * 2]) {
435 MM_DBG("in[%d] ready\n", audio->fill_next);
436 audio->in[audio->fill_next].used =
437 payload[3 + index * 2];
438 if ((++audio->fill_next) == audio->pcm_buf_count)
439 audio->fill_next = 0;
440
441 } else {
442 MM_ERR("expected=%x ret=%x\n",
443 audio->in[audio->fill_next].addr,
444 payload[2 + index * 2]);
445 break;
446 }
447 }
448 if (audio->in[audio->fill_next].used == 0) {
449 audio->drv_ops.buffer_refresh(audio);
450 } else {
451 MM_DBG("read cannot keep up\n");
452 audio->buf_refresh = 1;
453 }
454 wake_up(&audio->read_wait);
455 spin_unlock_irqrestore(&audio->dsp_lock, flags);
456
457}
458
459static void audplay_dsp_event(void *data, unsigned id, size_t len,
460 void (*getevent) (void *ptr, size_t len))
461{
462 struct audio *audio = data;
463 uint32_t msg[28];
464 getevent(msg, sizeof(msg));
465
466 MM_DBG("msg_id=%x\n", id);
467
468 switch (id) {
469 case AUDPLAY_MSG_DEC_NEEDS_DATA:
470 audio->drv_ops.send_data(audio, 1);
471 break;
472
473 case AUDPLAY_MSG_BUFFER_UPDATE:
474 audio->drv_ops.pcm_buf_update(audio, msg);
475 break;
476
477 case ADSP_MESSAGE_ID:
478 MM_DBG("Received ADSP event: module enable(audplaytask)\n");
479 break;
480
481 default:
482 MM_ERR("unexpected message from decoder \n");
483 break;
484 }
485}
486
487static void audio_dsp_event(void *private, unsigned id, uint16_t *msg)
488{
489 struct audio *audio = private;
490
491 switch (id) {
492 case AUDPP_MSG_STATUS_MSG:{
493 unsigned status = msg[1];
494
495 switch (status) {
496 case AUDPP_DEC_STATUS_SLEEP: {
497 uint16_t reason = msg[2];
498 MM_DBG("decoder status: sleep reason=0x%04x\n",
499 reason);
500 if ((reason == AUDPP_MSG_REASON_MEM)
501 || (reason ==
502 AUDPP_MSG_REASON_NODECODER)) {
503 audio->dec_state =
504 MSM_AUD_DECODER_STATE_FAILURE;
505 wake_up(&audio->wait);
506 } else if (reason == AUDPP_MSG_REASON_NONE) {
507 /* decoder is in disable state */
508 audio->dec_state =
509 MSM_AUD_DECODER_STATE_CLOSE;
510 wake_up(&audio->wait);
511 }
512 break;
513 }
514 case AUDPP_DEC_STATUS_INIT:
515 MM_DBG("decoder status: init \n");
516 if (audio->pcm_feedback)
517 audpp_cmd_cfg_routing_mode(audio);
518 else
519 audpp_cmd_cfg_adec_params(audio);
520 break;
521
522 case AUDPP_DEC_STATUS_CFG:
523 MM_DBG("decoder status: cfg \n");
524 break;
525 case AUDPP_DEC_STATUS_PLAY:
526 MM_DBG("decoder status: play \n");
527 if (audio->pcm_feedback) {
528 audplay_config_hostpcm(audio);
529 audio->drv_ops.buffer_refresh(audio);
530 }
531 audio->dec_state =
532 MSM_AUD_DECODER_STATE_SUCCESS;
533 wake_up(&audio->wait);
534 break;
535 default:
536 MM_ERR("unknown decoder status \n");
537 break;
538 }
539 break;
540 }
541 case AUDPP_MSG_CFG_MSG:
542 if (msg[0] == AUDPP_MSG_ENA_ENA) {
543 MM_DBG("CFG_MSG ENABLE\n");
544 auddec_dsp_config(audio, 1);
545 audio->out_needed = 0;
546 audio->running = 1;
547 audpp_dsp_set_vol_pan(audio->dec_id, &audio->vol_pan);
548 audpp_dsp_set_eq(audio->dec_id, audio->eq_enable,
549 &audio->eq);
550 audpp_avsync(audio->dec_id, 22050);
551 } else if (msg[0] == AUDPP_MSG_ENA_DIS) {
552 MM_DBG("CFG_MSG DISABLE\n");
553 audpp_avsync(audio->dec_id, 0);
554 audio->running = 0;
555 } else {
556 MM_DBG("CFG_MSG %d?\n", msg[0]);
557 }
558 break;
559 case AUDPP_MSG_ROUTING_ACK:
560 MM_DBG("ROUTING_ACK mode=%d\n", msg[1]);
561 audpp_cmd_cfg_adec_params(audio);
562 break;
563
564 case AUDPP_MSG_FLUSH_ACK:
565 MM_DBG("FLUSH_ACK\n");
566 audio->wflush = 0;
567 audio->rflush = 0;
568 wake_up(&audio->write_wait);
569 if (audio->pcm_feedback)
570 audio->drv_ops.buffer_refresh(audio);
571 break;
572
573 case AUDPP_MSG_PCMDMAMISSED:
574 MM_DBG("PCMDMAMISSED\n");
575 audio->teos = 1;
576 wake_up(&audio->write_wait);
577 break;
578
579 default:
580 MM_ERR("UNKNOWN (%d)\n", id);
581 }
582
583}
584
585
586struct msm_adsp_ops audplay_adsp_ops = {
587 .event = audplay_dsp_event,
588};
589
590
591#define audplay_send_queue0(audio, cmd, len) \
592 msm_adsp_write(audio->audplay, audio->queue_id, \
593 cmd, len)
594
595static int auddec_dsp_config(struct audio *audio, int enable)
596{
597 u16 cfg_dec_cmd[AUDPP_CMD_CFG_DEC_TYPE_LEN / sizeof(unsigned short)];
598
599 memset(cfg_dec_cmd, 0, sizeof(cfg_dec_cmd));
600
601 cfg_dec_cmd[0] = AUDPP_CMD_CFG_DEC_TYPE;
602 if (enable)
603 cfg_dec_cmd[1 + audio->dec_id] = AUDPP_CMD_UPDATDE_CFG_DEC |
604 AUDPP_CMD_ENA_DEC_V | AUDDEC_DEC_MP3;
605 else
606 cfg_dec_cmd[1 + audio->dec_id] = AUDPP_CMD_UPDATDE_CFG_DEC |
607 AUDPP_CMD_DIS_DEC_V;
608
609 return audpp_send_queue1(&cfg_dec_cmd, sizeof(cfg_dec_cmd));
610}
611
612static void audpp_cmd_cfg_adec_params(struct audio *audio)
613{
614 audpp_cmd_cfg_adec_params_mp3 cmd;
615
616 memset(&cmd, 0, sizeof(cmd));
617 cmd.common.cmd_id = AUDPP_CMD_CFG_ADEC_PARAMS;
618 cmd.common.length = AUDPP_CMD_CFG_ADEC_PARAMS_MP3_LEN;
619 cmd.common.dec_id = audio->dec_id;
620 cmd.common.input_sampling_frequency = audio->out_sample_rate;
621
622 audpp_send_queue2(&cmd, sizeof(cmd));
623}
624
625static void audpp_cmd_cfg_routing_mode(struct audio *audio)
626{
627 struct audpp_cmd_routing_mode cmd;
628 MM_DBG("\n"); /* Macro prints the file name and function */
629 memset(&cmd, 0, sizeof(cmd));
630 cmd.cmd_id = AUDPP_CMD_ROUTING_MODE;
631 cmd.object_number = audio->dec_id;
632 if (audio->pcm_feedback)
633 cmd.routing_mode = ROUTING_MODE_FTRT;
634 else
635 cmd.routing_mode = ROUTING_MODE_RT;
636
637 audpp_send_queue1(&cmd, sizeof(cmd));
638}
639
640static int audplay_dsp_send_data_avail(struct audio *audio,
641 unsigned idx, unsigned len)
642{
643 struct audplay_cmd_bitstream_data_avail_nt2 cmd;
644
645 cmd.cmd_id = AUDPLAY_CMD_BITSTREAM_DATA_AVAIL_NT2;
646 if (audio->mfield)
647 cmd.decoder_id = AUDMP3_METAFIELD_MASK |
648 (audio->out[idx].mfield_sz >> 1);
649 else
650 cmd.decoder_id = audio->dec_id;
651 cmd.buf_ptr = audio->out[idx].addr;
652 cmd.buf_size = len/2;
653 cmd.partition_number = 0;
654 /* complete all the writes to the input buffer */
655 wmb();
656 return audplay_send_queue0(audio, &cmd, sizeof(cmd));
657}
658
659/* Caller holds irq_lock */
660static void audmp3_async_buffer_refresh(struct audio *audio)
661{
662 struct audplay_cmd_buffer_refresh refresh_cmd;
663 struct audmp3_buffer_node *next_buf;
664
665 if (!audio->running ||
666 audio->drv_status & ADRV_STATUS_IBUF_GIVEN)
667 return;
668
669 if (!list_empty(&audio->in_queue)) {
670 next_buf = list_first_entry(&audio->in_queue,
671 struct audmp3_buffer_node, list);
672 if (!next_buf)
673 return;
674 MM_DBG("next buf %p phy %lx len %d\n", next_buf,
675 next_buf->paddr, next_buf->buf.buf_len);
676 refresh_cmd.cmd_id = AUDPLAY_CMD_BUFFER_REFRESH;
677 refresh_cmd.num_buffers = 1;
678 refresh_cmd.buf0_address = next_buf->paddr;
679 refresh_cmd.buf0_length = next_buf->buf.buf_len -
680 (next_buf->buf.buf_len % 576) +
681 (audio->mfield ? 24 : 0); /* Mp3 frame size */
682 refresh_cmd.buf_read_count = 0;
683 audio->drv_status |= ADRV_STATUS_IBUF_GIVEN;
684 (void) audplay_send_queue0(audio, &refresh_cmd,
685 sizeof(refresh_cmd));
686 }
687
688}
689
690static void audplay_buffer_refresh(struct audio *audio)
691{
692 struct audplay_cmd_buffer_refresh refresh_cmd;
693
694 refresh_cmd.cmd_id = AUDPLAY_CMD_BUFFER_REFRESH;
695 refresh_cmd.num_buffers = 1;
696 refresh_cmd.buf0_address = audio->in[audio->fill_next].addr;
697 refresh_cmd.buf0_length = audio->in[audio->fill_next].size -
698 (audio->in[audio->fill_next].size % 576) +
699 (audio->mfield ? 24 : 0); /* Mp3 frame size */
700 refresh_cmd.buf_read_count = 0;
701 MM_DBG("buf0_addr=%x buf0_len=%d\n", refresh_cmd.buf0_address,
702 refresh_cmd.buf0_length);
703 (void)audplay_send_queue0(audio, &refresh_cmd, sizeof(refresh_cmd));
704}
705
706static void audplay_config_hostpcm(struct audio *audio)
707{
708 struct audplay_cmd_hpcm_buf_cfg cfg_cmd;
709
710 MM_DBG("\n"); /* Macro prints the file name and function */
711 cfg_cmd.cmd_id = AUDPLAY_CMD_HPCM_BUF_CFG;
712 cfg_cmd.max_buffers = 1;
713 cfg_cmd.byte_swap = 0;
714 cfg_cmd.hostpcm_config = (0x8000) | (0x4000);
715 cfg_cmd.feedback_frequency = 1;
716 cfg_cmd.partition_number = 0;
717 (void)audplay_send_queue0(audio, &cfg_cmd, sizeof(cfg_cmd));
718
719}
720
721static void audmp3_async_send_data(struct audio *audio, unsigned needed)
722{
723 unsigned long flags;
724
725 spin_lock_irqsave(&audio->dsp_lock, flags);
726 if (!audio->running)
727 goto done;
728
729 if (needed && !audio->wflush) {
730 audio->out_needed = 1;
731 if (audio->drv_status & ADRV_STATUS_OBUF_GIVEN) {
732 /* pop one node out of queue */
733 union msm_audio_event_payload payload;
734 struct audmp3_buffer_node *used_buf;
735
736 MM_DBG("consumed\n");
737 BUG_ON(list_empty(&audio->out_queue));
738 used_buf = list_first_entry(&audio->out_queue,
739 struct audmp3_buffer_node, list);
740 list_del(&used_buf->list);
741 payload.aio_buf = used_buf->buf;
742 audmp3_post_event(audio, AUDIO_EVENT_WRITE_DONE,
743 payload);
744 kfree(used_buf);
745 audio->drv_status &= ~ADRV_STATUS_OBUF_GIVEN;
746 }
747
748 }
749
750 if (audio->out_needed) {
751 struct audmp3_buffer_node *next_buf;
752 struct audplay_cmd_bitstream_data_avail_nt2 cmd;
753 if (!list_empty(&audio->out_queue)) {
754 next_buf = list_first_entry(&audio->out_queue,
755 struct audmp3_buffer_node, list);
756 MM_DBG("next_buf %p\n", next_buf);
757 if (next_buf) {
758 MM_DBG("next buf phy %lx len %d\n",
759 next_buf->paddr,
760 next_buf->buf.data_len);
761
762 cmd.cmd_id =
763 AUDPLAY_CMD_BITSTREAM_DATA_AVAIL_NT2;
764 if (audio->mfield)
765 cmd.decoder_id = AUDMP3_METAFIELD_MASK |
766 (next_buf->buf.mfield_sz >> 1);
767 else
768 cmd.decoder_id = audio->dec_id;
769 cmd.buf_ptr = (unsigned) next_buf->paddr;
770 cmd.buf_size = next_buf->buf.data_len >> 1;
771 cmd.partition_number = 0;
772 /* complete the writes to the input buffer */
773 wmb();
774 audplay_send_queue0(audio, &cmd, sizeof(cmd));
775 audio->out_needed = 0;
776 audio->drv_status |= ADRV_STATUS_OBUF_GIVEN;
777 }
778 }
779 }
780
781done:
782 spin_unlock_irqrestore(&audio->dsp_lock, flags);
783}
784
785static void audplay_send_data(struct audio *audio, unsigned needed)
786{
787 struct buffer *frame;
788 unsigned long flags;
789
790 spin_lock_irqsave(&audio->dsp_lock, flags);
791 if (!audio->running)
792 goto done;
793
794 if (needed && !audio->wflush) {
795 /* We were called from the callback because the DSP
796 * requested more data. Note that the DSP does want
797 * more data, and if a buffer was in-flight, mark it
798 * as available (since the DSP must now be done with
799 * it).
800 */
801 audio->out_needed = 1;
802 frame = audio->out + audio->out_tail;
803 if (frame->used == 0xffffffff) {
804 MM_DBG("frame %d free\n", audio->out_tail);
805 frame->used = 0;
806 audio->out_tail ^= 1;
807 wake_up(&audio->write_wait);
808 }
809 }
810
811 if (audio->out_needed) {
812 /* If the DSP currently wants data and we have a
813 * buffer available, we will send it and reset
814 * the needed flag. We'll mark the buffer as in-flight
815 * so that it won't be recycled until the next buffer
816 * is requested
817 */
818
819 frame = audio->out + audio->out_tail;
820 if (frame->used) {
821 BUG_ON(frame->used == 0xffffffff);
822 MM_DBG("frame %d busy\n", audio->out_tail);
823 audplay_dsp_send_data_avail(audio, audio->out_tail,
824 frame->used);
825 frame->used = 0xffffffff;
826 audio->out_needed = 0;
827 }
828 }
829done:
830 spin_unlock_irqrestore(&audio->dsp_lock, flags);
831}
832
833/* ------------------- device --------------------- */
834static void audmp3_async_flush(struct audio *audio)
835{
836 struct audmp3_buffer_node *buf_node;
837 struct list_head *ptr, *next;
838 union msm_audio_event_payload payload;
Chaithanya Krishna Bacharaju221f7992012-05-16 09:32:54 +0530839 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700840
Chaithanya Krishna Bacharaju221f7992012-05-16 09:32:54 +0530841 spin_lock_irqsave(&audio->dsp_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700842 MM_DBG("\n"); /* Macro prints the file name and function */
843 list_for_each_safe(ptr, next, &audio->out_queue) {
844 buf_node = list_entry(ptr, struct audmp3_buffer_node, list);
845 list_del(&buf_node->list);
846 payload.aio_buf = buf_node->buf;
847 audmp3_post_event(audio, AUDIO_EVENT_WRITE_DONE,
848 payload);
849 kfree(buf_node);
850 }
851 audio->drv_status &= ~ADRV_STATUS_OBUF_GIVEN;
852 audio->out_needed = 0;
853 atomic_set(&audio->out_bytes, 0);
Chaithanya Krishna Bacharaju221f7992012-05-16 09:32:54 +0530854 spin_unlock_irqrestore(&audio->dsp_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700855}
856
857static void audio_flush(struct audio *audio)
858{
Manish Dewangana4f1df02012-02-08 17:06:54 +0530859 unsigned long flags;
860
861 spin_lock_irqsave(&audio->dsp_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700862 audio->out[0].used = 0;
863 audio->out[1].used = 0;
864 audio->out_head = 0;
865 audio->out_tail = 0;
866 audio->reserved = 0;
867 audio->out_needed = 0;
Manish Dewangana4f1df02012-02-08 17:06:54 +0530868 spin_unlock_irqrestore(&audio->dsp_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700869 atomic_set(&audio->out_bytes, 0);
870}
871
872static void audmp3_async_flush_pcm_buf(struct audio *audio)
873{
874 struct audmp3_buffer_node *buf_node;
875 struct list_head *ptr, *next;
876 union msm_audio_event_payload payload;
877
878 MM_DBG("\n"); /* Macro prints the file name and function */
879 list_for_each_safe(ptr, next, &audio->in_queue) {
880 buf_node = list_entry(ptr, struct audmp3_buffer_node, list);
881 list_del(&buf_node->list);
882 payload.aio_buf = buf_node->buf;
883 payload.aio_buf.data_len = 0;
884 audmp3_post_event(audio, AUDIO_EVENT_READ_DONE,
885 payload);
886 kfree(buf_node);
887 }
888 audio->drv_status &= ~ADRV_STATUS_IBUF_GIVEN;
889
890}
891
892static void audio_flush_pcm_buf(struct audio *audio)
893{
894 uint8_t index;
Manish Dewangana4f1df02012-02-08 17:06:54 +0530895 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700896
Manish Dewangana4f1df02012-02-08 17:06:54 +0530897 spin_lock_irqsave(&audio->dsp_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700898 for (index = 0; index < PCM_BUF_MAX_COUNT; index++)
899 audio->in[index].used = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700900 audio->buf_refresh = 0;
901 audio->read_next = 0;
902 audio->fill_next = 0;
Manish Dewangana4f1df02012-02-08 17:06:54 +0530903 spin_unlock_irqrestore(&audio->dsp_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700904}
905
906static void audio_ioport_reset(struct audio *audio)
907{
908 if (audio->drv_status & ADRV_STATUS_AIO_INTF) {
909 /* If fsync is in progress, make sure
910 * return value of fsync indicates
911 * abort due to flush
912 */
913 if (audio->drv_status & ADRV_STATUS_FSYNC) {
914 MM_DBG("fsync in progress\n");
915 wake_up(&audio->write_wait);
916 mutex_lock(&audio->write_lock);
917 audio->drv_ops.out_flush(audio);
918 mutex_unlock(&audio->write_lock);
919 } else
920 audio->drv_ops.out_flush(audio);
921 audio->drv_ops.in_flush(audio);
922 } else {
923 /* Make sure read/write thread are free from
924 * sleep and knowing that system is not able
925 * to process io request at the moment
926 */
927 wake_up(&audio->write_wait);
928 mutex_lock(&audio->write_lock);
929 audio->drv_ops.out_flush(audio);
930 mutex_unlock(&audio->write_lock);
931 wake_up(&audio->read_wait);
932 mutex_lock(&audio->read_lock);
933 audio->drv_ops.in_flush(audio);
934 mutex_unlock(&audio->read_lock);
935 }
936}
937
938static int audmp3_events_pending(struct audio *audio)
939{
940 unsigned long flags;
941 int empty;
942
943 spin_lock_irqsave(&audio->event_queue_lock, flags);
944 empty = !list_empty(&audio->event_queue);
945 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
946 return empty || audio->event_abort;
947}
948
949static void audmp3_reset_event_queue(struct audio *audio)
950{
951 unsigned long flags;
952 struct audmp3_event *drv_evt;
953 struct list_head *ptr, *next;
954
955 spin_lock_irqsave(&audio->event_queue_lock, flags);
956 list_for_each_safe(ptr, next, &audio->event_queue) {
957 drv_evt = list_first_entry(&audio->event_queue,
958 struct audmp3_event, list);
959 list_del(&drv_evt->list);
960 kfree(drv_evt);
961 }
962 list_for_each_safe(ptr, next, &audio->free_event_queue) {
963 drv_evt = list_first_entry(&audio->free_event_queue,
964 struct audmp3_event, list);
965 list_del(&drv_evt->list);
966 kfree(drv_evt);
967 }
968 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
969
970 return;
971}
972
973static long audmp3_process_event_req(struct audio *audio, void __user *arg)
974{
975 long rc;
976 struct msm_audio_event usr_evt;
977 struct audmp3_event *drv_evt = NULL;
978 int timeout;
979 unsigned long flags;
980
981 if (copy_from_user(&usr_evt, arg, sizeof(struct msm_audio_event)))
982 return -EFAULT;
983
984 timeout = (int) usr_evt.timeout_ms;
985
986 if (timeout > 0) {
987 rc = wait_event_interruptible_timeout(
988 audio->event_wait, audmp3_events_pending(audio),
989 msecs_to_jiffies(timeout));
990 if (rc == 0)
991 return -ETIMEDOUT;
992 } else {
993 rc = wait_event_interruptible(
994 audio->event_wait, audmp3_events_pending(audio));
995 }
996
997 if (rc < 0)
998 return rc;
999
1000 if (audio->event_abort) {
1001 audio->event_abort = 0;
1002 return -ENODEV;
1003 }
1004
1005 rc = 0;
1006
1007 spin_lock_irqsave(&audio->event_queue_lock, flags);
1008 if (!list_empty(&audio->event_queue)) {
1009 drv_evt = list_first_entry(&audio->event_queue,
1010 struct audmp3_event, list);
1011 list_del(&drv_evt->list);
1012 }
1013 if (drv_evt) {
1014 usr_evt.event_type = drv_evt->event_type;
1015 usr_evt.event_payload = drv_evt->payload;
1016 list_add_tail(&drv_evt->list, &audio->free_event_queue);
1017 } else
1018 rc = -1;
1019 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
1020
1021 if (drv_evt->event_type == AUDIO_EVENT_WRITE_DONE ||
1022 drv_evt->event_type == AUDIO_EVENT_READ_DONE) {
1023 mutex_lock(&audio->lock);
1024 audmp3_pmem_fixup(audio, drv_evt->payload.aio_buf.buf_addr,
1025 drv_evt->payload.aio_buf.buf_len, 0);
1026 mutex_unlock(&audio->lock);
1027 }
1028
1029 /* order reads from the output buffer */
1030 if (drv_evt->event_type == AUDIO_EVENT_READ_DONE)
1031 rmb();
1032
1033 if (!rc && copy_to_user(arg, &usr_evt, sizeof(usr_evt)))
1034 rc = -EFAULT;
1035
1036 return rc;
1037}
1038
1039static int audmp3_pmem_check(struct audio *audio,
1040 void *vaddr, unsigned long len)
1041{
1042 struct audmp3_pmem_region *region_elt;
1043 struct audmp3_pmem_region t = { .vaddr = vaddr, .len = len };
1044
1045 list_for_each_entry(region_elt, &audio->pmem_region_queue, list) {
1046 if (CONTAINS(region_elt, &t) || CONTAINS(&t, region_elt) ||
1047 OVERLAPS(region_elt, &t)) {
1048 MM_ERR("region (vaddr %p len %ld)"
1049 " clashes with registered region"
1050 " (vaddr %p paddr %p len %ld)\n",
1051 vaddr, len,
1052 region_elt->vaddr,
1053 (void *)region_elt->paddr,
1054 region_elt->len);
1055 return -EINVAL;
1056 }
1057 }
1058
1059 return 0;
1060}
1061
1062static int audmp3_pmem_add(struct audio *audio,
1063 struct msm_audio_pmem_info *info)
1064{
1065 unsigned long paddr, kvaddr, len;
1066 struct file *file;
1067 struct audmp3_pmem_region *region;
1068 int rc = -EINVAL;
1069
1070 MM_DBG("\n"); /* Macro prints the file name and function */
1071 region = kmalloc(sizeof(*region), GFP_KERNEL);
1072
1073 if (!region) {
1074 rc = -ENOMEM;
1075 goto end;
1076 }
1077
1078 if (get_pmem_file(info->fd, &paddr, &kvaddr, &len, &file)) {
1079 kfree(region);
1080 goto end;
1081 }
1082
1083 rc = audmp3_pmem_check(audio, info->vaddr, len);
1084 if (rc < 0) {
1085 put_pmem_file(file);
1086 kfree(region);
1087 goto end;
1088 }
1089
1090 region->vaddr = info->vaddr;
1091 region->fd = info->fd;
1092 region->paddr = paddr;
1093 region->kvaddr = kvaddr;
1094 region->len = len;
1095 region->file = file;
1096 region->ref_cnt = 0;
1097 MM_DBG("add region paddr %lx vaddr %p, len %lu\n", region->paddr,
1098 region->vaddr, region->len);
1099 list_add_tail(&region->list, &audio->pmem_region_queue);
1100end:
1101 return rc;
1102}
1103
1104static int audmp3_pmem_remove(struct audio *audio,
1105 struct msm_audio_pmem_info *info)
1106{
1107 struct audmp3_pmem_region *region;
1108 struct list_head *ptr, *next;
1109 int rc = -EINVAL;
1110
1111 MM_DBG("info fd %d vaddr %p\n", info->fd, info->vaddr);
1112
1113 list_for_each_safe(ptr, next, &audio->pmem_region_queue) {
1114 region = list_entry(ptr, struct audmp3_pmem_region, list);
1115
1116 if ((region->fd == info->fd) &&
1117 (region->vaddr == info->vaddr)) {
1118 if (region->ref_cnt) {
1119 MM_DBG("region %p in use ref_cnt %d\n",
1120 region, region->ref_cnt);
1121 break;
1122 }
1123 MM_DBG("remove region fd %d vaddr %p \n",
1124 info->fd, info->vaddr);
1125 list_del(&region->list);
1126 put_pmem_file(region->file);
1127 kfree(region);
1128 rc = 0;
1129 break;
1130 }
1131 }
1132
1133 return rc;
1134}
1135
1136static int audmp3_pmem_lookup_vaddr(struct audio *audio, void *addr,
1137 unsigned long len, struct audmp3_pmem_region **region)
1138{
1139 struct audmp3_pmem_region *region_elt;
1140
1141 int match_count = 0;
1142
1143 *region = NULL;
1144
1145 /* returns physical address or zero */
1146 list_for_each_entry(region_elt, &audio->pmem_region_queue,
1147 list) {
1148 if (addr >= region_elt->vaddr &&
1149 addr < region_elt->vaddr + region_elt->len &&
1150 addr + len <= region_elt->vaddr + region_elt->len) {
1151 /* offset since we could pass vaddr inside a registerd
1152 * pmem buffer
1153 */
1154
1155 match_count++;
1156 if (!*region)
1157 *region = region_elt;
1158 }
1159 }
1160
1161 if (match_count > 1) {
1162 MM_ERR("multiple hits for vaddr %p, len %ld\n", addr, len);
1163 list_for_each_entry(region_elt,
1164 &audio->pmem_region_queue, list) {
1165 if (addr >= region_elt->vaddr &&
1166 addr < region_elt->vaddr + region_elt->len &&
1167 addr + len <= region_elt->vaddr + region_elt->len)
1168 MM_ERR("\t%p, %ld --> %p\n", region_elt->vaddr,
1169 region_elt->len,
1170 (void *)region_elt->paddr);
1171 }
1172 }
1173
1174 return *region ? 0 : -1;
1175}
1176
1177unsigned long audmp3_pmem_fixup(struct audio *audio, void *addr,
1178 unsigned long len, int ref_up)
1179{
1180 struct audmp3_pmem_region *region;
1181 unsigned long paddr;
1182 int ret;
1183
1184 ret = audmp3_pmem_lookup_vaddr(audio, addr, len, &region);
1185 if (ret) {
1186 MM_ERR("lookup (%p, %ld) failed\n", addr, len);
1187 return 0;
1188 }
1189 if (ref_up)
1190 region->ref_cnt++;
1191 else
1192 region->ref_cnt--;
1193 MM_DBG("found region %p ref_cnt %d\n", region, region->ref_cnt);
1194 paddr = region->paddr + (addr - region->vaddr);
1195 return paddr;
1196}
1197
1198/* audio -> lock must be held at this point */
1199static int audmp3_aio_buf_add(struct audio *audio, unsigned dir,
1200 void __user *arg)
1201{
1202 unsigned long flags;
1203 struct audmp3_buffer_node *buf_node;
1204
1205 buf_node = kmalloc(sizeof(*buf_node), GFP_KERNEL);
1206
1207 if (!buf_node)
1208 return -ENOMEM;
1209
1210 if (copy_from_user(&buf_node->buf, arg, sizeof(buf_node->buf))) {
1211 kfree(buf_node);
1212 return -EFAULT;
1213 }
1214
1215 MM_DBG("node %p dir %x buf_addr %p buf_len %d data_len \
1216 %d\n", buf_node, dir,
1217 buf_node->buf.buf_addr, buf_node->buf.buf_len,
1218 buf_node->buf.data_len);
1219
1220 buf_node->paddr = audmp3_pmem_fixup(
1221 audio, buf_node->buf.buf_addr,
1222 buf_node->buf.buf_len, 1);
1223
1224 if (dir) {
1225 /* write */
1226 if (!buf_node->paddr ||
1227 (buf_node->paddr & 0x1) ||
1228 (buf_node->buf.data_len & 0x1) ||
1229 (!audio->pcm_feedback &&
1230 !buf_node->buf.data_len)) {
1231 kfree(buf_node);
1232 return -EINVAL;
1233 }
1234 spin_lock_irqsave(&audio->dsp_lock, flags);
1235 list_add_tail(&buf_node->list, &audio->out_queue);
1236 spin_unlock_irqrestore(&audio->dsp_lock, flags);
1237 audio->drv_ops.send_data(audio, 0);
1238 } else {
1239 /* read */
1240 if (!buf_node->paddr ||
1241 (buf_node->paddr & 0x1) ||
1242 (buf_node->buf.buf_len < PCM_BUFSZ_MIN)) {
1243 kfree(buf_node);
1244 return -EINVAL;
1245 }
1246 spin_lock_irqsave(&audio->dsp_lock, flags);
1247 list_add_tail(&buf_node->list, &audio->in_queue);
1248 audio->drv_ops.buffer_refresh(audio);
1249 spin_unlock_irqrestore(&audio->dsp_lock, flags);
1250 }
1251
1252 MM_DBG("Add buf_node %p paddr %lx\n", buf_node, buf_node->paddr);
1253
1254 return 0;
1255}
1256
1257static int audio_enable_eq(struct audio *audio, int enable)
1258{
1259 if (audio->eq_enable == enable && !audio->eq_needs_commit)
1260 return 0;
1261
1262 audio->eq_enable = enable;
1263
1264 if (audio->running) {
1265 audpp_dsp_set_eq(audio->dec_id, enable, &audio->eq);
1266 audio->eq_needs_commit = 0;
1267 }
1268 return 0;
1269}
1270
1271static long audio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1272{
1273 struct audio *audio = file->private_data;
1274 int rc = -EINVAL;
1275 unsigned long flags = 0;
1276 uint16_t enable_mask;
1277 int enable;
1278 int prev_state;
1279
1280 MM_DBG("cmd = %d\n", cmd);
1281
1282 if (cmd == AUDIO_GET_STATS) {
1283 struct msm_audio_stats stats;
1284 stats.byte_count = audpp_avsync_byte_count(audio->dec_id);
1285 stats.sample_count = audpp_avsync_sample_count(audio->dec_id);
1286 if (copy_to_user((void *) arg, &stats, sizeof(stats)))
1287 return -EFAULT;
1288 return 0;
1289 }
1290
1291 switch (cmd) {
1292 case AUDIO_ENABLE_AUDPP:
1293 if (copy_from_user(&enable_mask, (void *) arg,
1294 sizeof(enable_mask))) {
1295 rc = -EFAULT;
1296 break;
1297 }
1298
1299 spin_lock_irqsave(&audio->dsp_lock, flags);
1300 enable = (enable_mask & EQ_ENABLE) ? 1 : 0;
1301 audio_enable_eq(audio, enable);
1302 spin_unlock_irqrestore(&audio->dsp_lock, flags);
1303 rc = 0;
1304 break;
1305 case AUDIO_SET_VOLUME:
1306 spin_lock_irqsave(&audio->dsp_lock, flags);
1307 audio->vol_pan.volume = arg;
1308 if (audio->running)
1309 audpp_dsp_set_vol_pan(audio->dec_id, &audio->vol_pan);
1310 spin_unlock_irqrestore(&audio->dsp_lock, flags);
1311 rc = 0;
1312 break;
1313
1314 case AUDIO_SET_PAN:
1315 spin_lock_irqsave(&audio->dsp_lock, flags);
1316 audio->vol_pan.pan = arg;
1317 if (audio->running)
1318 audpp_dsp_set_vol_pan(audio->dec_id, &audio->vol_pan);
1319 spin_unlock_irqrestore(&audio->dsp_lock, flags);
1320 rc = 0;
1321 break;
1322
1323 case AUDIO_SET_EQ:
1324 prev_state = audio->eq_enable;
1325 audio->eq_enable = 0;
1326 if (copy_from_user(&audio->eq.num_bands, (void *) arg,
1327 sizeof(audio->eq) -
1328 (AUDPP_CMD_CFG_OBJECT_PARAMS_COMMON_LEN + 2))) {
1329 rc = -EFAULT;
1330 break;
1331 }
1332 audio->eq_enable = prev_state;
1333 audio->eq_needs_commit = 1;
1334 rc = 0;
1335 break;
1336 }
1337
1338 if (-EINVAL != rc)
1339 return rc;
1340
1341 if (cmd == AUDIO_GET_EVENT) {
1342 MM_DBG(" AUDIO_GET_EVENT\n");
1343 if (mutex_trylock(&audio->get_event_lock)) {
1344 rc = audmp3_process_event_req(audio,
1345 (void __user *) arg);
1346 mutex_unlock(&audio->get_event_lock);
1347 } else
1348 rc = -EBUSY;
1349 return rc;
1350 }
1351
1352 if (cmd == AUDIO_ABORT_GET_EVENT) {
1353 audio->event_abort = 1;
1354 wake_up(&audio->event_wait);
1355 return 0;
1356 }
1357
1358 mutex_lock(&audio->lock);
1359 switch (cmd) {
1360 case AUDIO_START:
1361 MM_DBG("AUDIO_START\n");
1362 rc = audio_enable(audio);
1363 if (!rc) {
1364 rc = wait_event_interruptible_timeout(audio->wait,
1365 audio->dec_state != MSM_AUD_DECODER_STATE_NONE,
1366 msecs_to_jiffies(MSM_AUD_DECODER_WAIT_MS));
1367 MM_INFO("dec_state %d rc = %d\n", audio->dec_state, rc);
1368
1369 if (audio->dec_state != MSM_AUD_DECODER_STATE_SUCCESS)
1370 rc = -ENODEV;
1371 else
1372 rc = 0;
1373 }
1374 break;
1375 case AUDIO_STOP:
1376 MM_DBG("AUDIO_STOP\n");
1377 rc = audio_disable(audio);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001378 audio_ioport_reset(audio);
1379 audio->stopped = 0;
1380 break;
1381 case AUDIO_FLUSH:
1382 MM_DBG("AUDIO_FLUSH\n");
1383 audio->rflush = 1;
1384 audio->wflush = 1;
1385 audio_ioport_reset(audio);
1386 if (audio->running) {
1387 audpp_flush(audio->dec_id);
1388 rc = wait_event_interruptible(audio->write_wait,
1389 !audio->wflush);
1390 if (rc < 0) {
1391 MM_ERR("AUDIO_FLUSH interrupted\n");
1392 rc = -EINTR;
1393 }
1394 } else {
1395 audio->rflush = 0;
1396 audio->wflush = 0;
1397 }
1398 break;
1399
1400 case AUDIO_SET_CONFIG: {
1401 struct msm_audio_config config;
1402 if (copy_from_user(&config, (void *) arg, sizeof(config))) {
1403 rc = -EFAULT;
1404 break;
1405 }
1406 if (config.channel_count == 1) {
1407 config.channel_count = AUDPP_CMD_PCM_INTF_MONO_V;
1408 } else if (config.channel_count == 2) {
1409 config.channel_count = AUDPP_CMD_PCM_INTF_STEREO_V;
1410 } else {
1411 rc = -EINVAL;
1412 break;
1413 }
1414 audio->mfield = config.meta_field;
1415 audio->out_sample_rate = config.sample_rate;
1416 audio->out_channel_mode = config.channel_count;
1417 rc = 0;
1418 break;
1419 }
1420 case AUDIO_GET_CONFIG: {
1421 struct msm_audio_config config;
1422 config.buffer_size = (audio->out_dma_sz >> 1);
1423 config.buffer_count = 2;
1424 config.sample_rate = audio->out_sample_rate;
1425 if (audio->out_channel_mode == AUDPP_CMD_PCM_INTF_MONO_V) {
1426 config.channel_count = 1;
1427 } else {
1428 config.channel_count = 2;
1429 }
1430 config.meta_field = 0;
1431 config.unused[0] = 0;
1432 config.unused[1] = 0;
1433 config.unused[2] = 0;
1434 if (copy_to_user((void *) arg, &config, sizeof(config))) {
1435 rc = -EFAULT;
1436 } else {
1437 rc = 0;
1438 }
1439 break;
1440 }
1441 case AUDIO_GET_PCM_CONFIG:{
1442 struct msm_audio_pcm_config config;
1443 config.pcm_feedback = audio->pcm_feedback;
1444 config.buffer_count = PCM_BUF_MAX_COUNT;
1445 config.buffer_size = PCM_BUFSZ_MIN;
1446 if (copy_to_user((void *)arg, &config,
1447 sizeof(config)))
1448 rc = -EFAULT;
1449 else
1450 rc = 0;
1451 break;
1452 }
1453 case AUDIO_SET_PCM_CONFIG:{
1454 struct msm_audio_pcm_config config;
1455 if (copy_from_user
1456 (&config, (void *)arg, sizeof(config))) {
1457 rc = -EFAULT;
1458 break;
1459 }
1460
1461 if (config.pcm_feedback != audio->pcm_feedback) {
1462 MM_ERR("Not sufficient permission to"
1463 "change the playback mode\n");
1464 rc = -EACCES;
1465 break;
1466 }
1467 if (audio->drv_status & ADRV_STATUS_AIO_INTF) {
1468 rc = 0;
1469 break;
1470 }
1471
1472 if ((config.buffer_count > PCM_BUF_MAX_COUNT) ||
1473 (config.buffer_count == 1))
1474 config.buffer_count = PCM_BUF_MAX_COUNT;
1475
1476 if (config.buffer_size < PCM_BUFSZ_MIN)
1477 config.buffer_size = PCM_BUFSZ_MIN;
1478
1479 /* Check if pcm feedback is required */
1480 if ((config.pcm_feedback) && (!audio->read_data)) {
1481 MM_DBG("allocate PCM buffer %d\n",
1482 config.buffer_count *
1483 config.buffer_size);
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05301484 audio->read_phys =
1485 allocate_contiguous_ebi_nomap(
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001486 config.buffer_size *
1487 config.buffer_count,
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05301488 SZ_4K);
1489 if (!audio->read_phys) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001490 rc = -ENOMEM;
1491 break;
1492 }
Laura Abbott35111d32012-04-27 18:41:48 -07001493 audio->map_v_read = ioremap(
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05301494 audio->read_phys,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001495 config.buffer_size *
Laura Abbott35111d32012-04-27 18:41:48 -07001496 config.buffer_count);
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05301497
1498 if (IS_ERR(audio->map_v_read)) {
1499 MM_ERR("map of read buf failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001500 rc = -ENOMEM;
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05301501 free_contiguous_memory_by_paddr(
1502 audio->read_phys);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001503 } else {
1504 uint8_t index;
1505 uint32_t offset = 0;
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05301506 audio->read_data =
Laura Abbott35111d32012-04-27 18:41:48 -07001507 audio->map_v_read;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001508 audio->buf_refresh = 0;
1509 audio->pcm_buf_count =
1510 config.buffer_count;
1511 audio->read_next = 0;
1512 audio->fill_next = 0;
1513
1514 for (index = 0;
1515 index < config.buffer_count;
1516 index++) {
1517 audio->in[index].data =
1518 audio->read_data + offset;
1519 audio->in[index].addr =
1520 audio->read_phys + offset;
1521 audio->in[index].size =
1522 config.buffer_size;
1523 audio->in[index].used = 0;
1524 offset += config.buffer_size;
1525 }
1526 rc = 0;
1527 MM_DBG("read buf: phy addr \
1528 0x%08x kernel addr 0x%08x\n",
1529 audio->read_phys,
1530 (int)audio->read_data);
1531 }
1532 } else {
1533 rc = 0;
1534 }
1535 break;
1536 }
1537 case AUDIO_PAUSE:
1538 MM_DBG("AUDIO_PAUSE %ld\n", arg);
1539 rc = audpp_pause(audio->dec_id, (int) arg);
1540 break;
1541
1542 case AUDIO_REGISTER_PMEM: {
1543 struct msm_audio_pmem_info info;
1544 MM_DBG("AUDIO_REGISTER_PMEM\n");
1545 if (copy_from_user(&info, (void *) arg, sizeof(info)))
1546 rc = -EFAULT;
1547 else
1548 rc = audmp3_pmem_add(audio, &info);
1549 break;
1550 }
1551
1552 case AUDIO_DEREGISTER_PMEM: {
1553 struct msm_audio_pmem_info info;
1554 MM_DBG("AUDIO_DEREGISTER_PMEM\n");
1555 if (copy_from_user(&info, (void *) arg, sizeof(info)))
1556 rc = -EFAULT;
1557 else
1558 rc = audmp3_pmem_remove(audio, &info);
1559 break;
1560 }
1561 case AUDIO_ASYNC_WRITE:
1562 if (audio->drv_status & ADRV_STATUS_FSYNC)
1563 rc = -EBUSY;
1564 else
1565 rc = audmp3_aio_buf_add(audio, 1, (void __user *) arg);
1566 break;
1567
1568 case AUDIO_ASYNC_READ:
1569 if (audio->pcm_feedback)
1570 rc = audmp3_aio_buf_add(audio, 0, (void __user *) arg);
1571 else
1572 rc = -EPERM;
1573 break;
1574
1575 default:
1576 rc = -EINVAL;
1577 }
1578 mutex_unlock(&audio->lock);
1579 return rc;
1580}
1581
1582/* Only useful in tunnel-mode */
1583int audmp3_async_fsync(struct audio *audio)
1584{
1585 int rc = 0;
1586
1587 MM_DBG("\n"); /* Macro prints the file name and function */
1588
1589 /* Blocking client sends more data */
1590 mutex_lock(&audio->lock);
1591 audio->drv_status |= ADRV_STATUS_FSYNC;
1592 mutex_unlock(&audio->lock);
1593
1594 mutex_lock(&audio->write_lock);
1595 /* pcm dmamiss message is sent continously
1596 * when decoder is starved so no race
1597 * condition concern
1598 */
1599 audio->teos = 0;
1600
1601 rc = wait_event_interruptible(audio->write_wait,
1602 (audio->teos && audio->out_needed &&
1603 list_empty(&audio->out_queue))
1604 || audio->wflush || audio->stopped);
1605
1606 if (audio->stopped || audio->wflush)
1607 rc = -EBUSY;
1608
1609 mutex_unlock(&audio->write_lock);
1610 mutex_lock(&audio->lock);
1611 audio->drv_status &= ~ADRV_STATUS_FSYNC;
1612 mutex_unlock(&audio->lock);
1613
1614 return rc;
1615}
1616
1617int audmp3_sync_fsync(struct audio *audio)
1618{
1619 struct buffer *frame;
1620 int rc = 0;
1621
1622 MM_DBG("\n"); /* Macro prints the file name and function */
1623
1624 mutex_lock(&audio->write_lock);
1625
1626 rc = wait_event_interruptible(audio->write_wait,
1627 (!audio->out[0].used &&
1628 !audio->out[1].used &&
1629 audio->out_needed) || audio->wflush);
1630
1631 if (rc < 0)
1632 goto done;
1633 else if (audio->wflush) {
1634 rc = -EBUSY;
1635 goto done;
1636 }
1637
1638 if (audio->reserved) {
1639 MM_DBG("send reserved byte\n");
1640 frame = audio->out + audio->out_tail;
1641 ((char *) frame->data)[0] = audio->rsv_byte;
1642 ((char *) frame->data)[1] = 0;
1643 frame->used = 2;
1644 audio->drv_ops.send_data(audio, 0);
1645
1646 rc = wait_event_interruptible(audio->write_wait,
1647 (!audio->out[0].used &&
1648 !audio->out[1].used &&
1649 audio->out_needed) || audio->wflush);
1650
1651 if (rc < 0)
1652 goto done;
1653 else if (audio->wflush) {
1654 rc = -EBUSY;
1655 goto done;
1656 }
1657 }
1658
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->wflush);
1667
1668 if (audio->wflush)
1669 rc = -EBUSY;
1670
1671done:
1672 mutex_unlock(&audio->write_lock);
1673 return rc;
1674}
1675
1676int audmp3_fsync(struct file *file, int datasync)
1677{
1678 struct audio *audio = file->private_data;
1679
1680 if (!audio->running || audio->pcm_feedback)
1681 return -EINVAL;
1682
1683 return audio->drv_ops.fsync(audio);
1684}
1685
1686static ssize_t audio_read(struct file *file, char __user *buf, size_t count,
1687 loff_t *pos)
1688{
1689 struct audio *audio = file->private_data;
1690 const char __user *start = buf;
1691 int rc = 0;
1692
1693 if (audio->drv_status & ADRV_STATUS_AIO_INTF)
1694 return -EPERM;
1695 else if (!audio->pcm_feedback)
1696 return 0; /* PCM feedback disabled. Nothing to read */
1697
1698 mutex_lock(&audio->read_lock);
1699 MM_DBG("%d \n", count);
1700 while (count > 0) {
1701 rc = wait_event_interruptible(
1702 audio->read_wait,
1703 (audio->in[audio->read_next].
1704 used > 0) || (audio->stopped)
1705 || (audio->rflush));
1706
1707 if (rc < 0)
1708 break;
1709
1710 if (audio->stopped || audio->rflush) {
1711 rc = -EBUSY;
1712 break;
1713 }
1714
1715 if (count < audio->in[audio->read_next].used) {
1716 /* Read must happen in frame boundary. Since
1717 * driver does not know frame size, read count
1718 * must be greater or equal
1719 * to size of PCM samples
1720 */
1721 MM_DBG("no partial frame done reading\n");
1722 break;
1723 } else {
1724 MM_DBG("read from in[%d]\n", audio->read_next);
1725 /* order reads from the output buffer */
1726 rmb();
1727 if (copy_to_user
1728 (buf, audio->in[audio->read_next].data,
1729 audio->in[audio->read_next].used)) {
1730 MM_ERR("invalid addr %x \n", (unsigned int)buf);
1731 rc = -EFAULT;
1732 break;
1733 }
1734 count -= audio->in[audio->read_next].used;
1735 buf += audio->in[audio->read_next].used;
1736 audio->in[audio->read_next].used = 0;
1737 if ((++audio->read_next) == audio->pcm_buf_count)
1738 audio->read_next = 0;
1739 break; /* Force to exit while loop
1740 * to prevent output thread
1741 * sleep too long if data is
1742 * not ready at this moment.
1743 */
1744 }
1745 }
1746
1747 /* don't feed output buffer to HW decoder during flushing
1748 * buffer refresh command will be sent once flush completes
1749 * send buf refresh command here can confuse HW decoder
1750 */
1751 if (audio->buf_refresh && !audio->rflush) {
1752 audio->buf_refresh = 0;
1753 MM_DBG("kick start pcm feedback again\n");
1754 audio->drv_ops.buffer_refresh(audio);
1755 }
1756
1757 mutex_unlock(&audio->read_lock);
1758
1759 if (buf > start)
1760 rc = buf - start;
1761
1762 MM_DBG("read %d bytes\n", rc);
1763 return rc;
1764}
1765
1766static int audmp3_process_eos(struct audio *audio,
1767 const char __user *buf_start, unsigned short mfield_size)
1768{
1769 int rc = 0;
1770 struct buffer *frame;
1771 char *buf_ptr;
1772
1773 if (audio->reserved) {
1774 MM_DBG("flush reserve byte\n");
1775 frame = audio->out + audio->out_head;
1776 buf_ptr = frame->data;
1777 rc = wait_event_interruptible(audio->write_wait,
1778 (frame->used == 0)
1779 || (audio->stopped)
1780 || (audio->wflush));
1781 if (rc < 0)
1782 goto done;
1783 if (audio->stopped || audio->wflush) {
1784 rc = -EBUSY;
1785 goto done;
1786 }
1787
1788 buf_ptr[0] = audio->rsv_byte;
1789 buf_ptr[1] = 0;
1790 audio->out_head ^= 1;
1791 frame->mfield_sz = 0;
1792 frame->used = 2;
1793 audio->reserved = 0;
1794 audio->drv_ops.send_data(audio, 0);
1795 }
1796
1797 frame = audio->out + audio->out_head;
1798
1799 rc = wait_event_interruptible(audio->write_wait,
1800 (audio->out_needed &&
1801 audio->out[0].used == 0 &&
1802 audio->out[1].used == 0)
1803 || (audio->stopped)
1804 || (audio->wflush));
1805
1806 if (rc < 0)
1807 goto done;
1808 if (audio->stopped || audio->wflush) {
1809 rc = -EBUSY;
1810 goto done;
1811 }
1812
1813 if (copy_from_user(frame->data, buf_start, mfield_size)) {
1814 rc = -EFAULT;
1815 goto done;
1816 }
1817
1818 frame->mfield_sz = mfield_size;
1819 audio->out_head ^= 1;
1820 frame->used = mfield_size;
1821 audio->drv_ops.send_data(audio, 0);
1822done:
1823 return rc;
1824}
1825
1826static ssize_t audio_write(struct file *file, const char __user *buf,
1827 size_t count, loff_t *pos)
1828{
1829 struct audio *audio = file->private_data;
1830 const char __user *start = buf;
1831 struct buffer *frame;
1832 size_t xfer;
1833 char *cpy_ptr;
1834 int rc = 0, eos_condition = AUDMP3_EOS_NONE;
1835 unsigned dsize;
1836 unsigned short mfield_size = 0;
1837
1838 if (audio->drv_status & ADRV_STATUS_AIO_INTF)
1839 return -EPERM;
1840
1841 MM_DBG("cnt=%d\n", count);
1842
1843 mutex_lock(&audio->write_lock);
1844 while (count > 0) {
1845 frame = audio->out + audio->out_head;
1846 cpy_ptr = frame->data;
1847 dsize = 0;
1848 rc = wait_event_interruptible(audio->write_wait,
1849 (frame->used == 0)
1850 || (audio->stopped)
1851 || (audio->wflush));
1852 if (rc < 0)
1853 break;
1854 if (audio->stopped || audio->wflush) {
1855 rc = -EBUSY;
1856 break;
1857 }
1858 if (audio->mfield) {
1859 if (buf == start) {
1860 /* Processing beginning of user buffer */
1861 if (__get_user(mfield_size,
1862 (unsigned short __user *) buf)) {
1863 rc = -EFAULT;
1864 break;
1865 } else if (mfield_size > count) {
1866 rc = -EINVAL;
1867 break;
1868 }
1869 MM_DBG("mf offset_val %x\n", mfield_size);
1870 if (copy_from_user(cpy_ptr, buf, mfield_size)) {
1871 rc = -EFAULT;
1872 break;
1873 }
1874 /* Check if EOS flag is set and buffer has
1875 * contains just meta field
1876 */
1877 if (cpy_ptr[AUDMP3_EOS_FLG_OFFSET] &
1878 AUDMP3_EOS_FLG_MASK) {
1879 MM_DBG("EOS SET\n");
1880 eos_condition = AUDMP3_EOS_SET;
1881 if (mfield_size == count) {
1882 buf += mfield_size;
1883 break;
1884 } else
1885 cpy_ptr[AUDMP3_EOS_FLG_OFFSET]
1886 &= ~AUDMP3_EOS_FLG_MASK;
1887 }
1888 cpy_ptr += mfield_size;
1889 count -= mfield_size;
1890 dsize += mfield_size;
1891 buf += mfield_size;
1892 } else {
1893 mfield_size = 0;
1894 MM_DBG("continuous buffer\n");
1895 }
1896 frame->mfield_sz = mfield_size;
1897 }
1898
1899 if (audio->reserved) {
1900 MM_DBG("append reserved byte %x\n", audio->rsv_byte);
1901 *cpy_ptr = audio->rsv_byte;
1902 xfer = (count > ((frame->size - mfield_size) - 1)) ?
1903 (frame->size - mfield_size) - 1 : count;
1904 cpy_ptr++;
1905 dsize += 1;
1906 audio->reserved = 0;
1907 } else
1908 xfer = (count > (frame->size - mfield_size)) ?
1909 (frame->size - mfield_size) : count;
1910
1911 if (copy_from_user(cpy_ptr, buf, xfer)) {
1912 rc = -EFAULT;
1913 break;
1914 }
1915
1916 dsize += xfer;
1917 if (dsize & 1) {
1918 audio->rsv_byte = ((char *) frame->data)[dsize - 1];
1919 MM_DBG("odd length buf reserve last byte %x\n",
1920 audio->rsv_byte);
1921 audio->reserved = 1;
1922 dsize--;
1923 }
1924 count -= xfer;
1925 buf += xfer;
1926
1927 if (dsize > 0) {
1928 audio->out_head ^= 1;
1929 frame->used = dsize;
1930 audio->drv_ops.send_data(audio, 0);
1931 }
1932 }
1933 if (eos_condition == AUDMP3_EOS_SET)
1934 rc = audmp3_process_eos(audio, start, mfield_size);
1935 mutex_unlock(&audio->write_lock);
1936 if (!rc) {
1937 if (buf > start)
1938 return buf - start;
1939 }
1940 return rc;
1941}
1942
1943static void audmp3_reset_pmem_region(struct audio *audio)
1944{
1945 struct audmp3_pmem_region *region;
1946 struct list_head *ptr, *next;
1947
1948 list_for_each_safe(ptr, next, &audio->pmem_region_queue) {
1949 region = list_entry(ptr, struct audmp3_pmem_region, list);
1950 list_del(&region->list);
1951 put_pmem_file(region->file);
1952 kfree(region);
1953 }
1954
1955 return;
1956}
1957
1958static int audio_release(struct inode *inode, struct file *file)
1959{
1960 struct audio *audio = file->private_data;
1961
1962 MM_INFO("audio instance 0x%08x freeing\n", (int)audio);
1963 mutex_lock(&audio->lock);
1964 audio_disable(audio);
1965 if (audio->rmt_resource_released == 0)
1966 rmt_put_resource(audio);
1967 audio->drv_ops.out_flush(audio);
1968 audio->drv_ops.in_flush(audio);
1969 audmp3_reset_pmem_region(audio);
1970
1971 msm_adsp_put(audio->audplay);
1972 audpp_adec_free(audio->dec_id);
1973#ifdef CONFIG_HAS_EARLYSUSPEND
1974 unregister_early_suspend(&audio->suspend_ctl.node);
1975#endif
1976 audio->opened = 0;
1977 audio->event_abort = 1;
1978 wake_up(&audio->event_wait);
1979 audmp3_reset_event_queue(audio);
1980 MM_DBG("pmem area = 0x%8x\n", (unsigned int)audio->data);
1981 if (audio->data) {
Laura Abbott35111d32012-04-27 18:41:48 -07001982 iounmap(audio->map_v_write);
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05301983 free_contiguous_memory_by_paddr(audio->phys);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001984 }
1985 if (audio->read_data) {
Laura Abbott35111d32012-04-27 18:41:48 -07001986 iounmap(audio->map_v_read);
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05301987 free_contiguous_memory_by_paddr(audio->read_phys);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001988 }
1989 mutex_unlock(&audio->lock);
1990#ifdef CONFIG_DEBUG_FS
1991 if (audio->dentry)
1992 debugfs_remove(audio->dentry);
1993#endif
1994 kfree(audio);
1995 return 0;
1996}
1997
1998static void audmp3_post_event(struct audio *audio, int type,
1999 union msm_audio_event_payload payload)
2000{
2001 struct audmp3_event *e_node = NULL;
2002 unsigned long flags;
2003
2004 spin_lock_irqsave(&audio->event_queue_lock, flags);
2005
2006 if (!list_empty(&audio->free_event_queue)) {
2007 e_node = list_first_entry(&audio->free_event_queue,
2008 struct audmp3_event, list);
2009 list_del(&e_node->list);
2010 } else {
2011 e_node = kmalloc(sizeof(struct audmp3_event), GFP_ATOMIC);
2012 if (!e_node) {
2013 MM_ERR("No mem to post event %d\n", type);
2014 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
2015 return;
2016 }
2017 }
2018
2019 e_node->event_type = type;
2020 e_node->payload = payload;
2021
2022 list_add_tail(&e_node->list, &audio->event_queue);
2023 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
2024 wake_up(&audio->event_wait);
2025}
2026
2027#ifdef CONFIG_HAS_EARLYSUSPEND
2028static void audmp3_suspend(struct early_suspend *h)
2029{
2030 struct audmp3_suspend_ctl *ctl =
2031 container_of(h, struct audmp3_suspend_ctl, node);
2032 union msm_audio_event_payload payload;
2033
2034 MM_DBG("\n"); /* Macro prints the file name and function */
2035 audmp3_post_event(ctl->audio, AUDIO_EVENT_SUSPEND, payload);
2036}
2037
2038static void audmp3_resume(struct early_suspend *h)
2039{
2040 struct audmp3_suspend_ctl *ctl =
2041 container_of(h, struct audmp3_suspend_ctl, node);
2042 union msm_audio_event_payload payload;
2043
2044 MM_DBG("\n"); /* Macro prints the file name and function */
2045 audmp3_post_event(ctl->audio, AUDIO_EVENT_RESUME, payload);
2046}
2047#endif
2048
2049#ifdef CONFIG_DEBUG_FS
2050static ssize_t audmp3_debug_open(struct inode *inode, struct file *file)
2051{
2052 file->private_data = inode->i_private;
2053 return 0;
2054}
2055
2056static ssize_t audmp3_debug_read(struct file *file, char __user *buf,
2057 size_t count, loff_t *ppos)
2058{
2059 const int debug_bufmax = 4096;
2060 static char buffer[4096];
2061 int n = 0, i;
2062 struct audio *audio = file->private_data;
2063
2064 mutex_lock(&audio->lock);
2065 n = scnprintf(buffer, debug_bufmax, "opened %d\n", audio->opened);
2066 n += scnprintf(buffer + n, debug_bufmax - n,
2067 "enabled %d\n", audio->enabled);
2068 n += scnprintf(buffer + n, debug_bufmax - n,
2069 "stopped %d\n", audio->stopped);
2070 n += scnprintf(buffer + n, debug_bufmax - n,
2071 "pcm_feedback %d\n", audio->pcm_feedback);
2072 n += scnprintf(buffer + n, debug_bufmax - n,
2073 "out_buf_sz %d\n", audio->out[0].size);
2074 n += scnprintf(buffer + n, debug_bufmax - n,
2075 "pcm_buf_count %d \n", audio->pcm_buf_count);
2076 n += scnprintf(buffer + n, debug_bufmax - n,
2077 "pcm_buf_sz %d \n", audio->in[0].size);
2078 n += scnprintf(buffer + n, debug_bufmax - n,
2079 "volume %x \n", audio->vol_pan.volume);
2080 n += scnprintf(buffer + n, debug_bufmax - n,
2081 "sample rate %d \n", audio->out_sample_rate);
2082 n += scnprintf(buffer + n, debug_bufmax - n,
2083 "channel mode %d \n", audio->out_channel_mode);
2084 mutex_unlock(&audio->lock);
2085 /* Following variables are only useful for debugging when
2086 * when playback halts unexpectedly. Thus, no mutual exclusion
2087 * enforced
2088 */
2089 n += scnprintf(buffer + n, debug_bufmax - n,
2090 "wflush %d\n", audio->wflush);
2091 n += scnprintf(buffer + n, debug_bufmax - n,
2092 "rflush %d\n", audio->rflush);
2093 n += scnprintf(buffer + n, debug_bufmax - n,
2094 "running %d \n", audio->running);
2095 n += scnprintf(buffer + n, debug_bufmax - n,
2096 "dec state %d \n", audio->dec_state);
2097 n += scnprintf(buffer + n, debug_bufmax - n,
2098 "out_needed %d \n", audio->out_needed);
2099 n += scnprintf(buffer + n, debug_bufmax - n,
2100 "out_head %d \n", audio->out_head);
2101 n += scnprintf(buffer + n, debug_bufmax - n,
2102 "out_tail %d \n", audio->out_tail);
2103 n += scnprintf(buffer + n, debug_bufmax - n,
2104 "out[0].used %d \n", audio->out[0].used);
2105 n += scnprintf(buffer + n, debug_bufmax - n,
2106 "out[1].used %d \n", audio->out[1].used);
2107 n += scnprintf(buffer + n, debug_bufmax - n,
2108 "buffer_refresh %d \n", audio->buf_refresh);
2109 n += scnprintf(buffer + n, debug_bufmax - n,
2110 "read_next %d \n", audio->read_next);
2111 n += scnprintf(buffer + n, debug_bufmax - n,
2112 "fill_next %d \n", audio->fill_next);
2113 for (i = 0; i < audio->pcm_buf_count; i++)
2114 n += scnprintf(buffer + n, debug_bufmax - n,
2115 "in[%d].size %d \n", i, audio->in[i].used);
2116 buffer[n] = 0;
2117 return simple_read_from_buffer(buf, count, ppos, buffer, n);
2118}
2119
2120static const struct file_operations audmp3_debug_fops = {
2121 .read = audmp3_debug_read,
2122 .open = audmp3_debug_open,
2123};
2124#endif
2125
2126static int audio_open(struct inode *inode, struct file *file)
2127{
2128
2129 struct audio *audio = NULL;
2130 int rc, i, dec_attrb, decid;
2131 struct audmp3_event *e_node = NULL;
2132 unsigned pmem_sz = DMASZ_MAX;
2133#ifdef CONFIG_DEBUG_FS
2134 /* 4 bytes represents decoder number, 1 byte for terminate string */
2135 char name[sizeof "msm_mp3_" + 5];
2136#endif
2137
2138 /* Allocate audio instance, set to zero */
2139 audio = kzalloc(sizeof(struct audio), GFP_KERNEL);
2140 if (!audio) {
2141 MM_ERR("no memory to allocate audio instance \n");
2142 rc = -ENOMEM;
2143 goto done;
2144 }
2145 MM_INFO("audio instance 0x%08x created\n", (int)audio);
2146
2147 /* Allocate the decoder */
2148 dec_attrb = AUDDEC_DEC_MP3;
2149 if ((file->f_mode & FMODE_WRITE) &&
2150 (file->f_mode & FMODE_READ)) {
2151 dec_attrb |= MSM_AUD_MODE_NONTUNNEL;
2152 audio->pcm_feedback = NON_TUNNEL_MODE_PLAYBACK;
2153 } else if ((file->f_mode & FMODE_WRITE) &&
2154 !(file->f_mode & FMODE_READ)) {
2155 dec_attrb |= MSM_AUD_MODE_TUNNEL;
2156 audio->pcm_feedback = TUNNEL_MODE_PLAYBACK;
2157 } else {
2158 kfree(audio);
2159 rc = -EACCES;
2160 goto done;
2161 }
2162
2163 decid = audpp_adec_alloc(dec_attrb, &audio->module_name,
2164 &audio->queue_id);
2165 if (decid < 0) {
2166 MM_ERR("No free decoder available, freeing instance 0x%08x\n",
2167 (int)audio);
2168 rc = -ENODEV;
2169 kfree(audio);
2170 goto done;
2171 }
2172 audio->dec_id = decid & MSM_AUD_DECODER_MASK;
2173
2174 /* Non AIO interface */
2175 if (!(file->f_flags & O_NONBLOCK)) {
2176 while (pmem_sz >= DMASZ_MIN) {
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05302177 MM_DBG("pmemsz = %d\n", pmem_sz);
2178 audio->phys = allocate_contiguous_ebi_nomap(pmem_sz,
2179 SZ_4K);
2180 if (audio->phys) {
Laura Abbott35111d32012-04-27 18:41:48 -07002181 audio->map_v_write = ioremap(
2182 audio->phys, pmem_sz);
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05302183 if (IS_ERR(audio->map_v_write)) {
2184 MM_ERR("could not map write \
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002185 buffers, freeing instance \
2186 0x%08x\n", (int)audio);
2187 rc = -ENOMEM;
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05302188 free_contiguous_memory_by_paddr(
2189 audio->phys);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002190 audpp_adec_free(audio->dec_id);
2191 kfree(audio);
2192 goto done;
2193 }
Laura Abbott35111d32012-04-27 18:41:48 -07002194 audio->data = audio->map_v_write;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002195 MM_DBG("write buf: phy addr 0x%08x kernel addr\
2196 0x%08x\n", audio->phys,\
2197 (int)audio->data);
2198 break;
2199 } else if (pmem_sz == DMASZ_MIN) {
2200 MM_ERR("could not allocate write buffers, \
2201 freeing instance 0x%08x\n",
2202 (int)audio);
2203 rc = -ENOMEM;
2204 audpp_adec_free(audio->dec_id);
2205 kfree(audio);
2206 goto done;
2207 } else
2208 pmem_sz >>= 1;
2209 }
2210 audio->out_dma_sz = pmem_sz;
2211 }
2212
2213 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK) {
2214 rc = audmgr_open(&audio->audmgr);
2215 if (rc) {
2216 MM_ERR("audmgr open failed, freeing instance \
2217 0x%08x\n", (int)audio);
2218 goto err;
2219 }
2220 }
2221
2222 rc = msm_adsp_get(audio->module_name, &audio->audplay,
2223 &audplay_adsp_ops, audio);
2224
2225 if (rc) {
2226 MM_ERR("failed to get %s module, freeing instance 0x%08x\n",
2227 audio->module_name, (int)audio);
2228 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK)
2229 audmgr_close(&audio->audmgr);
2230 goto err;
2231 }
2232
2233 rc = rmt_get_resource(audio);
2234 if (rc) {
2235 MM_ERR("ADSP resources are not available for MP3 session \
2236 0x%08x on decoder: %d\n", (int)audio, audio->dec_id);
2237 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK)
2238 audmgr_close(&audio->audmgr);
2239 msm_adsp_put(audio->audplay);
2240 goto err;
2241 }
2242
2243 if (file->f_flags & O_NONBLOCK) {
2244 MM_DBG("set to aio interface\n");
2245 audio->drv_status |= ADRV_STATUS_AIO_INTF;
2246 audio->drv_ops.pcm_buf_update = audmp3_async_pcm_buf_update;
2247 audio->drv_ops.buffer_refresh = audmp3_async_buffer_refresh;
2248 audio->drv_ops.send_data = audmp3_async_send_data;
2249 audio->drv_ops.out_flush = audmp3_async_flush;
2250 audio->drv_ops.in_flush = audmp3_async_flush_pcm_buf;
2251 audio->drv_ops.fsync = audmp3_async_fsync;
2252 } else {
2253 MM_DBG("set to std io interface\n");
2254 audio->drv_ops.pcm_buf_update = audio_update_pcm_buf_entry;
2255 audio->drv_ops.buffer_refresh = audplay_buffer_refresh;
2256 audio->drv_ops.send_data = audplay_send_data;
2257 audio->drv_ops.out_flush = audio_flush;
2258 audio->drv_ops.in_flush = audio_flush_pcm_buf;
2259 audio->drv_ops.fsync = audmp3_sync_fsync;
2260 audio->out[0].data = audio->data + 0;
2261 audio->out[0].addr = audio->phys + 0;
2262 audio->out[0].size = (audio->out_dma_sz >> 1);
2263
2264 audio->out[1].data = audio->data + audio->out[0].size;
2265 audio->out[1].addr = audio->phys + audio->out[0].size;
2266 audio->out[1].size = audio->out[0].size;
2267 }
2268
2269 /* Initialize all locks of audio instance */
2270 mutex_init(&audio->lock);
2271 mutex_init(&audio->write_lock);
2272 mutex_init(&audio->read_lock);
2273 mutex_init(&audio->get_event_lock);
2274 spin_lock_init(&audio->dsp_lock);
2275 init_waitqueue_head(&audio->write_wait);
2276 init_waitqueue_head(&audio->read_wait);
2277 INIT_LIST_HEAD(&audio->out_queue);
2278 INIT_LIST_HEAD(&audio->in_queue);
2279 INIT_LIST_HEAD(&audio->pmem_region_queue);
2280 INIT_LIST_HEAD(&audio->free_event_queue);
2281 INIT_LIST_HEAD(&audio->event_queue);
2282 init_waitqueue_head(&audio->wait);
2283 init_waitqueue_head(&audio->event_wait);
2284 spin_lock_init(&audio->event_queue_lock);
2285
2286 audio->out_sample_rate = 44100;
2287 audio->out_channel_mode = AUDPP_CMD_PCM_INTF_STEREO_V;
2288 audio->vol_pan.volume = 0x2000;
2289
2290 audio->drv_ops.out_flush(audio);
2291
2292 file->private_data = audio;
2293 audio->opened = 1;
2294#ifdef CONFIG_DEBUG_FS
2295 snprintf(name, sizeof name, "msm_mp3_%04x", audio->dec_id);
2296 audio->dentry = debugfs_create_file(name, S_IFREG | S_IRUGO,
2297 NULL, (void *) audio, &audmp3_debug_fops);
2298
2299 if (IS_ERR(audio->dentry))
2300 MM_DBG("debugfs_create_file failed\n");
2301#endif
2302#ifdef CONFIG_HAS_EARLYSUSPEND
2303 audio->suspend_ctl.node.level = EARLY_SUSPEND_LEVEL_DISABLE_FB;
2304 audio->suspend_ctl.node.resume = audmp3_resume;
2305 audio->suspend_ctl.node.suspend = audmp3_suspend;
2306 audio->suspend_ctl.audio = audio;
2307 register_early_suspend(&audio->suspend_ctl.node);
2308#endif
2309 for (i = 0; i < AUDMP3_EVENT_NUM; i++) {
2310 e_node = kmalloc(sizeof(struct audmp3_event), GFP_KERNEL);
2311 if (e_node)
2312 list_add_tail(&e_node->list, &audio->free_event_queue);
2313 else {
2314 MM_ERR("event pkt alloc failed\n");
2315 break;
2316 }
2317 }
2318done:
2319 return rc;
2320err:
2321 if (audio->data) {
Laura Abbott35111d32012-04-27 18:41:48 -07002322 iounmap(audio->map_v_write);
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05302323 free_contiguous_memory_by_paddr(audio->phys);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002324 }
2325 audpp_adec_free(audio->dec_id);
2326 kfree(audio);
2327 return rc;
2328}
2329
2330static const struct file_operations audio_mp3_fops = {
2331 .owner = THIS_MODULE,
2332 .open = audio_open,
2333 .release = audio_release,
2334 .read = audio_read,
2335 .write = audio_write,
2336 .unlocked_ioctl = audio_ioctl,
2337 .fsync = audmp3_fsync,
2338};
2339
2340struct miscdevice audio_mp3_misc = {
2341 .minor = MISC_DYNAMIC_MINOR,
2342 .name = "msm_mp3",
2343 .fops = &audio_mp3_fops,
2344};
2345
2346static int __init audio_init(void)
2347{
2348 return misc_register(&audio_mp3_misc);
2349}
2350
2351static void __exit audio_exit(void)
2352{
2353 misc_deregister(&audio_mp3_misc);
2354}
2355
2356module_init(audio_init);
2357module_exit(audio_exit);
2358
2359MODULE_DESCRIPTION("MSM MP3 driver");
2360MODULE_LICENSE("GPL v2");