blob: 5abdf853b09d7c16d2fc27ffb8161590a26f466f [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;
839
840 MM_DBG("\n"); /* Macro prints the file name and function */
841 list_for_each_safe(ptr, next, &audio->out_queue) {
842 buf_node = list_entry(ptr, struct audmp3_buffer_node, list);
843 list_del(&buf_node->list);
844 payload.aio_buf = buf_node->buf;
845 audmp3_post_event(audio, AUDIO_EVENT_WRITE_DONE,
846 payload);
847 kfree(buf_node);
848 }
849 audio->drv_status &= ~ADRV_STATUS_OBUF_GIVEN;
850 audio->out_needed = 0;
851 atomic_set(&audio->out_bytes, 0);
852}
853
854static void audio_flush(struct audio *audio)
855{
Manish Dewangana4f1df02012-02-08 17:06:54 +0530856 unsigned long flags;
857
858 spin_lock_irqsave(&audio->dsp_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700859 audio->out[0].used = 0;
860 audio->out[1].used = 0;
861 audio->out_head = 0;
862 audio->out_tail = 0;
863 audio->reserved = 0;
864 audio->out_needed = 0;
Manish Dewangana4f1df02012-02-08 17:06:54 +0530865 spin_unlock_irqrestore(&audio->dsp_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700866 atomic_set(&audio->out_bytes, 0);
867}
868
869static void audmp3_async_flush_pcm_buf(struct audio *audio)
870{
871 struct audmp3_buffer_node *buf_node;
872 struct list_head *ptr, *next;
873 union msm_audio_event_payload payload;
874
875 MM_DBG("\n"); /* Macro prints the file name and function */
876 list_for_each_safe(ptr, next, &audio->in_queue) {
877 buf_node = list_entry(ptr, struct audmp3_buffer_node, list);
878 list_del(&buf_node->list);
879 payload.aio_buf = buf_node->buf;
880 payload.aio_buf.data_len = 0;
881 audmp3_post_event(audio, AUDIO_EVENT_READ_DONE,
882 payload);
883 kfree(buf_node);
884 }
885 audio->drv_status &= ~ADRV_STATUS_IBUF_GIVEN;
886
887}
888
889static void audio_flush_pcm_buf(struct audio *audio)
890{
891 uint8_t index;
Manish Dewangana4f1df02012-02-08 17:06:54 +0530892 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700893
Manish Dewangana4f1df02012-02-08 17:06:54 +0530894 spin_lock_irqsave(&audio->dsp_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700895 for (index = 0; index < PCM_BUF_MAX_COUNT; index++)
896 audio->in[index].used = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700897 audio->buf_refresh = 0;
898 audio->read_next = 0;
899 audio->fill_next = 0;
Manish Dewangana4f1df02012-02-08 17:06:54 +0530900 spin_unlock_irqrestore(&audio->dsp_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700901}
902
903static void audio_ioport_reset(struct audio *audio)
904{
905 if (audio->drv_status & ADRV_STATUS_AIO_INTF) {
906 /* If fsync is in progress, make sure
907 * return value of fsync indicates
908 * abort due to flush
909 */
910 if (audio->drv_status & ADRV_STATUS_FSYNC) {
911 MM_DBG("fsync in progress\n");
912 wake_up(&audio->write_wait);
913 mutex_lock(&audio->write_lock);
914 audio->drv_ops.out_flush(audio);
915 mutex_unlock(&audio->write_lock);
916 } else
917 audio->drv_ops.out_flush(audio);
918 audio->drv_ops.in_flush(audio);
919 } else {
920 /* Make sure read/write thread are free from
921 * sleep and knowing that system is not able
922 * to process io request at the moment
923 */
924 wake_up(&audio->write_wait);
925 mutex_lock(&audio->write_lock);
926 audio->drv_ops.out_flush(audio);
927 mutex_unlock(&audio->write_lock);
928 wake_up(&audio->read_wait);
929 mutex_lock(&audio->read_lock);
930 audio->drv_ops.in_flush(audio);
931 mutex_unlock(&audio->read_lock);
932 }
933}
934
935static int audmp3_events_pending(struct audio *audio)
936{
937 unsigned long flags;
938 int empty;
939
940 spin_lock_irqsave(&audio->event_queue_lock, flags);
941 empty = !list_empty(&audio->event_queue);
942 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
943 return empty || audio->event_abort;
944}
945
946static void audmp3_reset_event_queue(struct audio *audio)
947{
948 unsigned long flags;
949 struct audmp3_event *drv_evt;
950 struct list_head *ptr, *next;
951
952 spin_lock_irqsave(&audio->event_queue_lock, flags);
953 list_for_each_safe(ptr, next, &audio->event_queue) {
954 drv_evt = list_first_entry(&audio->event_queue,
955 struct audmp3_event, list);
956 list_del(&drv_evt->list);
957 kfree(drv_evt);
958 }
959 list_for_each_safe(ptr, next, &audio->free_event_queue) {
960 drv_evt = list_first_entry(&audio->free_event_queue,
961 struct audmp3_event, list);
962 list_del(&drv_evt->list);
963 kfree(drv_evt);
964 }
965 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
966
967 return;
968}
969
970static long audmp3_process_event_req(struct audio *audio, void __user *arg)
971{
972 long rc;
973 struct msm_audio_event usr_evt;
974 struct audmp3_event *drv_evt = NULL;
975 int timeout;
976 unsigned long flags;
977
978 if (copy_from_user(&usr_evt, arg, sizeof(struct msm_audio_event)))
979 return -EFAULT;
980
981 timeout = (int) usr_evt.timeout_ms;
982
983 if (timeout > 0) {
984 rc = wait_event_interruptible_timeout(
985 audio->event_wait, audmp3_events_pending(audio),
986 msecs_to_jiffies(timeout));
987 if (rc == 0)
988 return -ETIMEDOUT;
989 } else {
990 rc = wait_event_interruptible(
991 audio->event_wait, audmp3_events_pending(audio));
992 }
993
994 if (rc < 0)
995 return rc;
996
997 if (audio->event_abort) {
998 audio->event_abort = 0;
999 return -ENODEV;
1000 }
1001
1002 rc = 0;
1003
1004 spin_lock_irqsave(&audio->event_queue_lock, flags);
1005 if (!list_empty(&audio->event_queue)) {
1006 drv_evt = list_first_entry(&audio->event_queue,
1007 struct audmp3_event, list);
1008 list_del(&drv_evt->list);
1009 }
1010 if (drv_evt) {
1011 usr_evt.event_type = drv_evt->event_type;
1012 usr_evt.event_payload = drv_evt->payload;
1013 list_add_tail(&drv_evt->list, &audio->free_event_queue);
1014 } else
1015 rc = -1;
1016 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
1017
1018 if (drv_evt->event_type == AUDIO_EVENT_WRITE_DONE ||
1019 drv_evt->event_type == AUDIO_EVENT_READ_DONE) {
1020 mutex_lock(&audio->lock);
1021 audmp3_pmem_fixup(audio, drv_evt->payload.aio_buf.buf_addr,
1022 drv_evt->payload.aio_buf.buf_len, 0);
1023 mutex_unlock(&audio->lock);
1024 }
1025
1026 /* order reads from the output buffer */
1027 if (drv_evt->event_type == AUDIO_EVENT_READ_DONE)
1028 rmb();
1029
1030 if (!rc && copy_to_user(arg, &usr_evt, sizeof(usr_evt)))
1031 rc = -EFAULT;
1032
1033 return rc;
1034}
1035
1036static int audmp3_pmem_check(struct audio *audio,
1037 void *vaddr, unsigned long len)
1038{
1039 struct audmp3_pmem_region *region_elt;
1040 struct audmp3_pmem_region t = { .vaddr = vaddr, .len = len };
1041
1042 list_for_each_entry(region_elt, &audio->pmem_region_queue, list) {
1043 if (CONTAINS(region_elt, &t) || CONTAINS(&t, region_elt) ||
1044 OVERLAPS(region_elt, &t)) {
1045 MM_ERR("region (vaddr %p len %ld)"
1046 " clashes with registered region"
1047 " (vaddr %p paddr %p len %ld)\n",
1048 vaddr, len,
1049 region_elt->vaddr,
1050 (void *)region_elt->paddr,
1051 region_elt->len);
1052 return -EINVAL;
1053 }
1054 }
1055
1056 return 0;
1057}
1058
1059static int audmp3_pmem_add(struct audio *audio,
1060 struct msm_audio_pmem_info *info)
1061{
1062 unsigned long paddr, kvaddr, len;
1063 struct file *file;
1064 struct audmp3_pmem_region *region;
1065 int rc = -EINVAL;
1066
1067 MM_DBG("\n"); /* Macro prints the file name and function */
1068 region = kmalloc(sizeof(*region), GFP_KERNEL);
1069
1070 if (!region) {
1071 rc = -ENOMEM;
1072 goto end;
1073 }
1074
1075 if (get_pmem_file(info->fd, &paddr, &kvaddr, &len, &file)) {
1076 kfree(region);
1077 goto end;
1078 }
1079
1080 rc = audmp3_pmem_check(audio, info->vaddr, len);
1081 if (rc < 0) {
1082 put_pmem_file(file);
1083 kfree(region);
1084 goto end;
1085 }
1086
1087 region->vaddr = info->vaddr;
1088 region->fd = info->fd;
1089 region->paddr = paddr;
1090 region->kvaddr = kvaddr;
1091 region->len = len;
1092 region->file = file;
1093 region->ref_cnt = 0;
1094 MM_DBG("add region paddr %lx vaddr %p, len %lu\n", region->paddr,
1095 region->vaddr, region->len);
1096 list_add_tail(&region->list, &audio->pmem_region_queue);
1097end:
1098 return rc;
1099}
1100
1101static int audmp3_pmem_remove(struct audio *audio,
1102 struct msm_audio_pmem_info *info)
1103{
1104 struct audmp3_pmem_region *region;
1105 struct list_head *ptr, *next;
1106 int rc = -EINVAL;
1107
1108 MM_DBG("info fd %d vaddr %p\n", info->fd, info->vaddr);
1109
1110 list_for_each_safe(ptr, next, &audio->pmem_region_queue) {
1111 region = list_entry(ptr, struct audmp3_pmem_region, list);
1112
1113 if ((region->fd == info->fd) &&
1114 (region->vaddr == info->vaddr)) {
1115 if (region->ref_cnt) {
1116 MM_DBG("region %p in use ref_cnt %d\n",
1117 region, region->ref_cnt);
1118 break;
1119 }
1120 MM_DBG("remove region fd %d vaddr %p \n",
1121 info->fd, info->vaddr);
1122 list_del(&region->list);
1123 put_pmem_file(region->file);
1124 kfree(region);
1125 rc = 0;
1126 break;
1127 }
1128 }
1129
1130 return rc;
1131}
1132
1133static int audmp3_pmem_lookup_vaddr(struct audio *audio, void *addr,
1134 unsigned long len, struct audmp3_pmem_region **region)
1135{
1136 struct audmp3_pmem_region *region_elt;
1137
1138 int match_count = 0;
1139
1140 *region = NULL;
1141
1142 /* returns physical address or zero */
1143 list_for_each_entry(region_elt, &audio->pmem_region_queue,
1144 list) {
1145 if (addr >= region_elt->vaddr &&
1146 addr < region_elt->vaddr + region_elt->len &&
1147 addr + len <= region_elt->vaddr + region_elt->len) {
1148 /* offset since we could pass vaddr inside a registerd
1149 * pmem buffer
1150 */
1151
1152 match_count++;
1153 if (!*region)
1154 *region = region_elt;
1155 }
1156 }
1157
1158 if (match_count > 1) {
1159 MM_ERR("multiple hits for vaddr %p, len %ld\n", addr, len);
1160 list_for_each_entry(region_elt,
1161 &audio->pmem_region_queue, list) {
1162 if (addr >= region_elt->vaddr &&
1163 addr < region_elt->vaddr + region_elt->len &&
1164 addr + len <= region_elt->vaddr + region_elt->len)
1165 MM_ERR("\t%p, %ld --> %p\n", region_elt->vaddr,
1166 region_elt->len,
1167 (void *)region_elt->paddr);
1168 }
1169 }
1170
1171 return *region ? 0 : -1;
1172}
1173
1174unsigned long audmp3_pmem_fixup(struct audio *audio, void *addr,
1175 unsigned long len, int ref_up)
1176{
1177 struct audmp3_pmem_region *region;
1178 unsigned long paddr;
1179 int ret;
1180
1181 ret = audmp3_pmem_lookup_vaddr(audio, addr, len, &region);
1182 if (ret) {
1183 MM_ERR("lookup (%p, %ld) failed\n", addr, len);
1184 return 0;
1185 }
1186 if (ref_up)
1187 region->ref_cnt++;
1188 else
1189 region->ref_cnt--;
1190 MM_DBG("found region %p ref_cnt %d\n", region, region->ref_cnt);
1191 paddr = region->paddr + (addr - region->vaddr);
1192 return paddr;
1193}
1194
1195/* audio -> lock must be held at this point */
1196static int audmp3_aio_buf_add(struct audio *audio, unsigned dir,
1197 void __user *arg)
1198{
1199 unsigned long flags;
1200 struct audmp3_buffer_node *buf_node;
1201
1202 buf_node = kmalloc(sizeof(*buf_node), GFP_KERNEL);
1203
1204 if (!buf_node)
1205 return -ENOMEM;
1206
1207 if (copy_from_user(&buf_node->buf, arg, sizeof(buf_node->buf))) {
1208 kfree(buf_node);
1209 return -EFAULT;
1210 }
1211
1212 MM_DBG("node %p dir %x buf_addr %p buf_len %d data_len \
1213 %d\n", buf_node, dir,
1214 buf_node->buf.buf_addr, buf_node->buf.buf_len,
1215 buf_node->buf.data_len);
1216
1217 buf_node->paddr = audmp3_pmem_fixup(
1218 audio, buf_node->buf.buf_addr,
1219 buf_node->buf.buf_len, 1);
1220
1221 if (dir) {
1222 /* write */
1223 if (!buf_node->paddr ||
1224 (buf_node->paddr & 0x1) ||
1225 (buf_node->buf.data_len & 0x1) ||
1226 (!audio->pcm_feedback &&
1227 !buf_node->buf.data_len)) {
1228 kfree(buf_node);
1229 return -EINVAL;
1230 }
1231 spin_lock_irqsave(&audio->dsp_lock, flags);
1232 list_add_tail(&buf_node->list, &audio->out_queue);
1233 spin_unlock_irqrestore(&audio->dsp_lock, flags);
1234 audio->drv_ops.send_data(audio, 0);
1235 } else {
1236 /* read */
1237 if (!buf_node->paddr ||
1238 (buf_node->paddr & 0x1) ||
1239 (buf_node->buf.buf_len < PCM_BUFSZ_MIN)) {
1240 kfree(buf_node);
1241 return -EINVAL;
1242 }
1243 spin_lock_irqsave(&audio->dsp_lock, flags);
1244 list_add_tail(&buf_node->list, &audio->in_queue);
1245 audio->drv_ops.buffer_refresh(audio);
1246 spin_unlock_irqrestore(&audio->dsp_lock, flags);
1247 }
1248
1249 MM_DBG("Add buf_node %p paddr %lx\n", buf_node, buf_node->paddr);
1250
1251 return 0;
1252}
1253
1254static int audio_enable_eq(struct audio *audio, int enable)
1255{
1256 if (audio->eq_enable == enable && !audio->eq_needs_commit)
1257 return 0;
1258
1259 audio->eq_enable = enable;
1260
1261 if (audio->running) {
1262 audpp_dsp_set_eq(audio->dec_id, enable, &audio->eq);
1263 audio->eq_needs_commit = 0;
1264 }
1265 return 0;
1266}
1267
1268static long audio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1269{
1270 struct audio *audio = file->private_data;
1271 int rc = -EINVAL;
1272 unsigned long flags = 0;
1273 uint16_t enable_mask;
1274 int enable;
1275 int prev_state;
1276
1277 MM_DBG("cmd = %d\n", cmd);
1278
1279 if (cmd == AUDIO_GET_STATS) {
1280 struct msm_audio_stats stats;
1281 stats.byte_count = audpp_avsync_byte_count(audio->dec_id);
1282 stats.sample_count = audpp_avsync_sample_count(audio->dec_id);
1283 if (copy_to_user((void *) arg, &stats, sizeof(stats)))
1284 return -EFAULT;
1285 return 0;
1286 }
1287
1288 switch (cmd) {
1289 case AUDIO_ENABLE_AUDPP:
1290 if (copy_from_user(&enable_mask, (void *) arg,
1291 sizeof(enable_mask))) {
1292 rc = -EFAULT;
1293 break;
1294 }
1295
1296 spin_lock_irqsave(&audio->dsp_lock, flags);
1297 enable = (enable_mask & EQ_ENABLE) ? 1 : 0;
1298 audio_enable_eq(audio, enable);
1299 spin_unlock_irqrestore(&audio->dsp_lock, flags);
1300 rc = 0;
1301 break;
1302 case AUDIO_SET_VOLUME:
1303 spin_lock_irqsave(&audio->dsp_lock, flags);
1304 audio->vol_pan.volume = arg;
1305 if (audio->running)
1306 audpp_dsp_set_vol_pan(audio->dec_id, &audio->vol_pan);
1307 spin_unlock_irqrestore(&audio->dsp_lock, flags);
1308 rc = 0;
1309 break;
1310
1311 case AUDIO_SET_PAN:
1312 spin_lock_irqsave(&audio->dsp_lock, flags);
1313 audio->vol_pan.pan = arg;
1314 if (audio->running)
1315 audpp_dsp_set_vol_pan(audio->dec_id, &audio->vol_pan);
1316 spin_unlock_irqrestore(&audio->dsp_lock, flags);
1317 rc = 0;
1318 break;
1319
1320 case AUDIO_SET_EQ:
1321 prev_state = audio->eq_enable;
1322 audio->eq_enable = 0;
1323 if (copy_from_user(&audio->eq.num_bands, (void *) arg,
1324 sizeof(audio->eq) -
1325 (AUDPP_CMD_CFG_OBJECT_PARAMS_COMMON_LEN + 2))) {
1326 rc = -EFAULT;
1327 break;
1328 }
1329 audio->eq_enable = prev_state;
1330 audio->eq_needs_commit = 1;
1331 rc = 0;
1332 break;
1333 }
1334
1335 if (-EINVAL != rc)
1336 return rc;
1337
1338 if (cmd == AUDIO_GET_EVENT) {
1339 MM_DBG(" AUDIO_GET_EVENT\n");
1340 if (mutex_trylock(&audio->get_event_lock)) {
1341 rc = audmp3_process_event_req(audio,
1342 (void __user *) arg);
1343 mutex_unlock(&audio->get_event_lock);
1344 } else
1345 rc = -EBUSY;
1346 return rc;
1347 }
1348
1349 if (cmd == AUDIO_ABORT_GET_EVENT) {
1350 audio->event_abort = 1;
1351 wake_up(&audio->event_wait);
1352 return 0;
1353 }
1354
1355 mutex_lock(&audio->lock);
1356 switch (cmd) {
1357 case AUDIO_START:
1358 MM_DBG("AUDIO_START\n");
1359 rc = audio_enable(audio);
1360 if (!rc) {
1361 rc = wait_event_interruptible_timeout(audio->wait,
1362 audio->dec_state != MSM_AUD_DECODER_STATE_NONE,
1363 msecs_to_jiffies(MSM_AUD_DECODER_WAIT_MS));
1364 MM_INFO("dec_state %d rc = %d\n", audio->dec_state, rc);
1365
1366 if (audio->dec_state != MSM_AUD_DECODER_STATE_SUCCESS)
1367 rc = -ENODEV;
1368 else
1369 rc = 0;
1370 }
1371 break;
1372 case AUDIO_STOP:
1373 MM_DBG("AUDIO_STOP\n");
1374 rc = audio_disable(audio);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001375 audio_ioport_reset(audio);
1376 audio->stopped = 0;
1377 break;
1378 case AUDIO_FLUSH:
1379 MM_DBG("AUDIO_FLUSH\n");
1380 audio->rflush = 1;
1381 audio->wflush = 1;
1382 audio_ioport_reset(audio);
1383 if (audio->running) {
1384 audpp_flush(audio->dec_id);
1385 rc = wait_event_interruptible(audio->write_wait,
1386 !audio->wflush);
1387 if (rc < 0) {
1388 MM_ERR("AUDIO_FLUSH interrupted\n");
1389 rc = -EINTR;
1390 }
1391 } else {
1392 audio->rflush = 0;
1393 audio->wflush = 0;
1394 }
1395 break;
1396
1397 case AUDIO_SET_CONFIG: {
1398 struct msm_audio_config config;
1399 if (copy_from_user(&config, (void *) arg, sizeof(config))) {
1400 rc = -EFAULT;
1401 break;
1402 }
1403 if (config.channel_count == 1) {
1404 config.channel_count = AUDPP_CMD_PCM_INTF_MONO_V;
1405 } else if (config.channel_count == 2) {
1406 config.channel_count = AUDPP_CMD_PCM_INTF_STEREO_V;
1407 } else {
1408 rc = -EINVAL;
1409 break;
1410 }
1411 audio->mfield = config.meta_field;
1412 audio->out_sample_rate = config.sample_rate;
1413 audio->out_channel_mode = config.channel_count;
1414 rc = 0;
1415 break;
1416 }
1417 case AUDIO_GET_CONFIG: {
1418 struct msm_audio_config config;
1419 config.buffer_size = (audio->out_dma_sz >> 1);
1420 config.buffer_count = 2;
1421 config.sample_rate = audio->out_sample_rate;
1422 if (audio->out_channel_mode == AUDPP_CMD_PCM_INTF_MONO_V) {
1423 config.channel_count = 1;
1424 } else {
1425 config.channel_count = 2;
1426 }
1427 config.meta_field = 0;
1428 config.unused[0] = 0;
1429 config.unused[1] = 0;
1430 config.unused[2] = 0;
1431 if (copy_to_user((void *) arg, &config, sizeof(config))) {
1432 rc = -EFAULT;
1433 } else {
1434 rc = 0;
1435 }
1436 break;
1437 }
1438 case AUDIO_GET_PCM_CONFIG:{
1439 struct msm_audio_pcm_config config;
1440 config.pcm_feedback = audio->pcm_feedback;
1441 config.buffer_count = PCM_BUF_MAX_COUNT;
1442 config.buffer_size = PCM_BUFSZ_MIN;
1443 if (copy_to_user((void *)arg, &config,
1444 sizeof(config)))
1445 rc = -EFAULT;
1446 else
1447 rc = 0;
1448 break;
1449 }
1450 case AUDIO_SET_PCM_CONFIG:{
1451 struct msm_audio_pcm_config config;
1452 if (copy_from_user
1453 (&config, (void *)arg, sizeof(config))) {
1454 rc = -EFAULT;
1455 break;
1456 }
1457
1458 if (config.pcm_feedback != audio->pcm_feedback) {
1459 MM_ERR("Not sufficient permission to"
1460 "change the playback mode\n");
1461 rc = -EACCES;
1462 break;
1463 }
1464 if (audio->drv_status & ADRV_STATUS_AIO_INTF) {
1465 rc = 0;
1466 break;
1467 }
1468
1469 if ((config.buffer_count > PCM_BUF_MAX_COUNT) ||
1470 (config.buffer_count == 1))
1471 config.buffer_count = PCM_BUF_MAX_COUNT;
1472
1473 if (config.buffer_size < PCM_BUFSZ_MIN)
1474 config.buffer_size = PCM_BUFSZ_MIN;
1475
1476 /* Check if pcm feedback is required */
1477 if ((config.pcm_feedback) && (!audio->read_data)) {
1478 MM_DBG("allocate PCM buffer %d\n",
1479 config.buffer_count *
1480 config.buffer_size);
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05301481 audio->read_phys =
1482 allocate_contiguous_ebi_nomap(
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001483 config.buffer_size *
1484 config.buffer_count,
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05301485 SZ_4K);
1486 if (!audio->read_phys) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001487 rc = -ENOMEM;
1488 break;
1489 }
Laura Abbott35111d32012-04-27 18:41:48 -07001490 audio->map_v_read = ioremap(
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05301491 audio->read_phys,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001492 config.buffer_size *
Laura Abbott35111d32012-04-27 18:41:48 -07001493 config.buffer_count);
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05301494
1495 if (IS_ERR(audio->map_v_read)) {
1496 MM_ERR("map of read buf failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001497 rc = -ENOMEM;
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05301498 free_contiguous_memory_by_paddr(
1499 audio->read_phys);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001500 } else {
1501 uint8_t index;
1502 uint32_t offset = 0;
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05301503 audio->read_data =
Laura Abbott35111d32012-04-27 18:41:48 -07001504 audio->map_v_read;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001505 audio->buf_refresh = 0;
1506 audio->pcm_buf_count =
1507 config.buffer_count;
1508 audio->read_next = 0;
1509 audio->fill_next = 0;
1510
1511 for (index = 0;
1512 index < config.buffer_count;
1513 index++) {
1514 audio->in[index].data =
1515 audio->read_data + offset;
1516 audio->in[index].addr =
1517 audio->read_phys + offset;
1518 audio->in[index].size =
1519 config.buffer_size;
1520 audio->in[index].used = 0;
1521 offset += config.buffer_size;
1522 }
1523 rc = 0;
1524 MM_DBG("read buf: phy addr \
1525 0x%08x kernel addr 0x%08x\n",
1526 audio->read_phys,
1527 (int)audio->read_data);
1528 }
1529 } else {
1530 rc = 0;
1531 }
1532 break;
1533 }
1534 case AUDIO_PAUSE:
1535 MM_DBG("AUDIO_PAUSE %ld\n", arg);
1536 rc = audpp_pause(audio->dec_id, (int) arg);
1537 break;
1538
1539 case AUDIO_REGISTER_PMEM: {
1540 struct msm_audio_pmem_info info;
1541 MM_DBG("AUDIO_REGISTER_PMEM\n");
1542 if (copy_from_user(&info, (void *) arg, sizeof(info)))
1543 rc = -EFAULT;
1544 else
1545 rc = audmp3_pmem_add(audio, &info);
1546 break;
1547 }
1548
1549 case AUDIO_DEREGISTER_PMEM: {
1550 struct msm_audio_pmem_info info;
1551 MM_DBG("AUDIO_DEREGISTER_PMEM\n");
1552 if (copy_from_user(&info, (void *) arg, sizeof(info)))
1553 rc = -EFAULT;
1554 else
1555 rc = audmp3_pmem_remove(audio, &info);
1556 break;
1557 }
1558 case AUDIO_ASYNC_WRITE:
1559 if (audio->drv_status & ADRV_STATUS_FSYNC)
1560 rc = -EBUSY;
1561 else
1562 rc = audmp3_aio_buf_add(audio, 1, (void __user *) arg);
1563 break;
1564
1565 case AUDIO_ASYNC_READ:
1566 if (audio->pcm_feedback)
1567 rc = audmp3_aio_buf_add(audio, 0, (void __user *) arg);
1568 else
1569 rc = -EPERM;
1570 break;
1571
1572 default:
1573 rc = -EINVAL;
1574 }
1575 mutex_unlock(&audio->lock);
1576 return rc;
1577}
1578
1579/* Only useful in tunnel-mode */
1580int audmp3_async_fsync(struct audio *audio)
1581{
1582 int rc = 0;
1583
1584 MM_DBG("\n"); /* Macro prints the file name and function */
1585
1586 /* Blocking client sends more data */
1587 mutex_lock(&audio->lock);
1588 audio->drv_status |= ADRV_STATUS_FSYNC;
1589 mutex_unlock(&audio->lock);
1590
1591 mutex_lock(&audio->write_lock);
1592 /* pcm dmamiss message is sent continously
1593 * when decoder is starved so no race
1594 * condition concern
1595 */
1596 audio->teos = 0;
1597
1598 rc = wait_event_interruptible(audio->write_wait,
1599 (audio->teos && audio->out_needed &&
1600 list_empty(&audio->out_queue))
1601 || audio->wflush || audio->stopped);
1602
1603 if (audio->stopped || audio->wflush)
1604 rc = -EBUSY;
1605
1606 mutex_unlock(&audio->write_lock);
1607 mutex_lock(&audio->lock);
1608 audio->drv_status &= ~ADRV_STATUS_FSYNC;
1609 mutex_unlock(&audio->lock);
1610
1611 return rc;
1612}
1613
1614int audmp3_sync_fsync(struct audio *audio)
1615{
1616 struct buffer *frame;
1617 int rc = 0;
1618
1619 MM_DBG("\n"); /* Macro prints the file name and function */
1620
1621 mutex_lock(&audio->write_lock);
1622
1623 rc = wait_event_interruptible(audio->write_wait,
1624 (!audio->out[0].used &&
1625 !audio->out[1].used &&
1626 audio->out_needed) || audio->wflush);
1627
1628 if (rc < 0)
1629 goto done;
1630 else if (audio->wflush) {
1631 rc = -EBUSY;
1632 goto done;
1633 }
1634
1635 if (audio->reserved) {
1636 MM_DBG("send reserved byte\n");
1637 frame = audio->out + audio->out_tail;
1638 ((char *) frame->data)[0] = audio->rsv_byte;
1639 ((char *) frame->data)[1] = 0;
1640 frame->used = 2;
1641 audio->drv_ops.send_data(audio, 0);
1642
1643 rc = wait_event_interruptible(audio->write_wait,
1644 (!audio->out[0].used &&
1645 !audio->out[1].used &&
1646 audio->out_needed) || audio->wflush);
1647
1648 if (rc < 0)
1649 goto done;
1650 else if (audio->wflush) {
1651 rc = -EBUSY;
1652 goto done;
1653 }
1654 }
1655
1656 /* pcm dmamiss message is sent continously
1657 * when decoder is starved so no race
1658 * condition concern
1659 */
1660 audio->teos = 0;
1661
1662 rc = wait_event_interruptible(audio->write_wait,
1663 audio->teos || audio->wflush);
1664
1665 if (audio->wflush)
1666 rc = -EBUSY;
1667
1668done:
1669 mutex_unlock(&audio->write_lock);
1670 return rc;
1671}
1672
1673int audmp3_fsync(struct file *file, int datasync)
1674{
1675 struct audio *audio = file->private_data;
1676
1677 if (!audio->running || audio->pcm_feedback)
1678 return -EINVAL;
1679
1680 return audio->drv_ops.fsync(audio);
1681}
1682
1683static ssize_t audio_read(struct file *file, char __user *buf, size_t count,
1684 loff_t *pos)
1685{
1686 struct audio *audio = file->private_data;
1687 const char __user *start = buf;
1688 int rc = 0;
1689
1690 if (audio->drv_status & ADRV_STATUS_AIO_INTF)
1691 return -EPERM;
1692 else if (!audio->pcm_feedback)
1693 return 0; /* PCM feedback disabled. Nothing to read */
1694
1695 mutex_lock(&audio->read_lock);
1696 MM_DBG("%d \n", count);
1697 while (count > 0) {
1698 rc = wait_event_interruptible(
1699 audio->read_wait,
1700 (audio->in[audio->read_next].
1701 used > 0) || (audio->stopped)
1702 || (audio->rflush));
1703
1704 if (rc < 0)
1705 break;
1706
1707 if (audio->stopped || audio->rflush) {
1708 rc = -EBUSY;
1709 break;
1710 }
1711
1712 if (count < audio->in[audio->read_next].used) {
1713 /* Read must happen in frame boundary. Since
1714 * driver does not know frame size, read count
1715 * must be greater or equal
1716 * to size of PCM samples
1717 */
1718 MM_DBG("no partial frame done reading\n");
1719 break;
1720 } else {
1721 MM_DBG("read from in[%d]\n", audio->read_next);
1722 /* order reads from the output buffer */
1723 rmb();
1724 if (copy_to_user
1725 (buf, audio->in[audio->read_next].data,
1726 audio->in[audio->read_next].used)) {
1727 MM_ERR("invalid addr %x \n", (unsigned int)buf);
1728 rc = -EFAULT;
1729 break;
1730 }
1731 count -= audio->in[audio->read_next].used;
1732 buf += audio->in[audio->read_next].used;
1733 audio->in[audio->read_next].used = 0;
1734 if ((++audio->read_next) == audio->pcm_buf_count)
1735 audio->read_next = 0;
1736 break; /* Force to exit while loop
1737 * to prevent output thread
1738 * sleep too long if data is
1739 * not ready at this moment.
1740 */
1741 }
1742 }
1743
1744 /* don't feed output buffer to HW decoder during flushing
1745 * buffer refresh command will be sent once flush completes
1746 * send buf refresh command here can confuse HW decoder
1747 */
1748 if (audio->buf_refresh && !audio->rflush) {
1749 audio->buf_refresh = 0;
1750 MM_DBG("kick start pcm feedback again\n");
1751 audio->drv_ops.buffer_refresh(audio);
1752 }
1753
1754 mutex_unlock(&audio->read_lock);
1755
1756 if (buf > start)
1757 rc = buf - start;
1758
1759 MM_DBG("read %d bytes\n", rc);
1760 return rc;
1761}
1762
1763static int audmp3_process_eos(struct audio *audio,
1764 const char __user *buf_start, unsigned short mfield_size)
1765{
1766 int rc = 0;
1767 struct buffer *frame;
1768 char *buf_ptr;
1769
1770 if (audio->reserved) {
1771 MM_DBG("flush reserve byte\n");
1772 frame = audio->out + audio->out_head;
1773 buf_ptr = frame->data;
1774 rc = wait_event_interruptible(audio->write_wait,
1775 (frame->used == 0)
1776 || (audio->stopped)
1777 || (audio->wflush));
1778 if (rc < 0)
1779 goto done;
1780 if (audio->stopped || audio->wflush) {
1781 rc = -EBUSY;
1782 goto done;
1783 }
1784
1785 buf_ptr[0] = audio->rsv_byte;
1786 buf_ptr[1] = 0;
1787 audio->out_head ^= 1;
1788 frame->mfield_sz = 0;
1789 frame->used = 2;
1790 audio->reserved = 0;
1791 audio->drv_ops.send_data(audio, 0);
1792 }
1793
1794 frame = audio->out + audio->out_head;
1795
1796 rc = wait_event_interruptible(audio->write_wait,
1797 (audio->out_needed &&
1798 audio->out[0].used == 0 &&
1799 audio->out[1].used == 0)
1800 || (audio->stopped)
1801 || (audio->wflush));
1802
1803 if (rc < 0)
1804 goto done;
1805 if (audio->stopped || audio->wflush) {
1806 rc = -EBUSY;
1807 goto done;
1808 }
1809
1810 if (copy_from_user(frame->data, buf_start, mfield_size)) {
1811 rc = -EFAULT;
1812 goto done;
1813 }
1814
1815 frame->mfield_sz = mfield_size;
1816 audio->out_head ^= 1;
1817 frame->used = mfield_size;
1818 audio->drv_ops.send_data(audio, 0);
1819done:
1820 return rc;
1821}
1822
1823static ssize_t audio_write(struct file *file, const char __user *buf,
1824 size_t count, loff_t *pos)
1825{
1826 struct audio *audio = file->private_data;
1827 const char __user *start = buf;
1828 struct buffer *frame;
1829 size_t xfer;
1830 char *cpy_ptr;
1831 int rc = 0, eos_condition = AUDMP3_EOS_NONE;
1832 unsigned dsize;
1833 unsigned short mfield_size = 0;
1834
1835 if (audio->drv_status & ADRV_STATUS_AIO_INTF)
1836 return -EPERM;
1837
1838 MM_DBG("cnt=%d\n", count);
1839
1840 mutex_lock(&audio->write_lock);
1841 while (count > 0) {
1842 frame = audio->out + audio->out_head;
1843 cpy_ptr = frame->data;
1844 dsize = 0;
1845 rc = wait_event_interruptible(audio->write_wait,
1846 (frame->used == 0)
1847 || (audio->stopped)
1848 || (audio->wflush));
1849 if (rc < 0)
1850 break;
1851 if (audio->stopped || audio->wflush) {
1852 rc = -EBUSY;
1853 break;
1854 }
1855 if (audio->mfield) {
1856 if (buf == start) {
1857 /* Processing beginning of user buffer */
1858 if (__get_user(mfield_size,
1859 (unsigned short __user *) buf)) {
1860 rc = -EFAULT;
1861 break;
1862 } else if (mfield_size > count) {
1863 rc = -EINVAL;
1864 break;
1865 }
1866 MM_DBG("mf offset_val %x\n", mfield_size);
1867 if (copy_from_user(cpy_ptr, buf, mfield_size)) {
1868 rc = -EFAULT;
1869 break;
1870 }
1871 /* Check if EOS flag is set and buffer has
1872 * contains just meta field
1873 */
1874 if (cpy_ptr[AUDMP3_EOS_FLG_OFFSET] &
1875 AUDMP3_EOS_FLG_MASK) {
1876 MM_DBG("EOS SET\n");
1877 eos_condition = AUDMP3_EOS_SET;
1878 if (mfield_size == count) {
1879 buf += mfield_size;
1880 break;
1881 } else
1882 cpy_ptr[AUDMP3_EOS_FLG_OFFSET]
1883 &= ~AUDMP3_EOS_FLG_MASK;
1884 }
1885 cpy_ptr += mfield_size;
1886 count -= mfield_size;
1887 dsize += mfield_size;
1888 buf += mfield_size;
1889 } else {
1890 mfield_size = 0;
1891 MM_DBG("continuous buffer\n");
1892 }
1893 frame->mfield_sz = mfield_size;
1894 }
1895
1896 if (audio->reserved) {
1897 MM_DBG("append reserved byte %x\n", audio->rsv_byte);
1898 *cpy_ptr = audio->rsv_byte;
1899 xfer = (count > ((frame->size - mfield_size) - 1)) ?
1900 (frame->size - mfield_size) - 1 : count;
1901 cpy_ptr++;
1902 dsize += 1;
1903 audio->reserved = 0;
1904 } else
1905 xfer = (count > (frame->size - mfield_size)) ?
1906 (frame->size - mfield_size) : count;
1907
1908 if (copy_from_user(cpy_ptr, buf, xfer)) {
1909 rc = -EFAULT;
1910 break;
1911 }
1912
1913 dsize += xfer;
1914 if (dsize & 1) {
1915 audio->rsv_byte = ((char *) frame->data)[dsize - 1];
1916 MM_DBG("odd length buf reserve last byte %x\n",
1917 audio->rsv_byte);
1918 audio->reserved = 1;
1919 dsize--;
1920 }
1921 count -= xfer;
1922 buf += xfer;
1923
1924 if (dsize > 0) {
1925 audio->out_head ^= 1;
1926 frame->used = dsize;
1927 audio->drv_ops.send_data(audio, 0);
1928 }
1929 }
1930 if (eos_condition == AUDMP3_EOS_SET)
1931 rc = audmp3_process_eos(audio, start, mfield_size);
1932 mutex_unlock(&audio->write_lock);
1933 if (!rc) {
1934 if (buf > start)
1935 return buf - start;
1936 }
1937 return rc;
1938}
1939
1940static void audmp3_reset_pmem_region(struct audio *audio)
1941{
1942 struct audmp3_pmem_region *region;
1943 struct list_head *ptr, *next;
1944
1945 list_for_each_safe(ptr, next, &audio->pmem_region_queue) {
1946 region = list_entry(ptr, struct audmp3_pmem_region, list);
1947 list_del(&region->list);
1948 put_pmem_file(region->file);
1949 kfree(region);
1950 }
1951
1952 return;
1953}
1954
1955static int audio_release(struct inode *inode, struct file *file)
1956{
1957 struct audio *audio = file->private_data;
1958
1959 MM_INFO("audio instance 0x%08x freeing\n", (int)audio);
1960 mutex_lock(&audio->lock);
1961 audio_disable(audio);
1962 if (audio->rmt_resource_released == 0)
1963 rmt_put_resource(audio);
1964 audio->drv_ops.out_flush(audio);
1965 audio->drv_ops.in_flush(audio);
1966 audmp3_reset_pmem_region(audio);
1967
1968 msm_adsp_put(audio->audplay);
1969 audpp_adec_free(audio->dec_id);
1970#ifdef CONFIG_HAS_EARLYSUSPEND
1971 unregister_early_suspend(&audio->suspend_ctl.node);
1972#endif
1973 audio->opened = 0;
1974 audio->event_abort = 1;
1975 wake_up(&audio->event_wait);
1976 audmp3_reset_event_queue(audio);
1977 MM_DBG("pmem area = 0x%8x\n", (unsigned int)audio->data);
1978 if (audio->data) {
Laura Abbott35111d32012-04-27 18:41:48 -07001979 iounmap(audio->map_v_write);
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05301980 free_contiguous_memory_by_paddr(audio->phys);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001981 }
1982 if (audio->read_data) {
Laura Abbott35111d32012-04-27 18:41:48 -07001983 iounmap(audio->map_v_read);
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05301984 free_contiguous_memory_by_paddr(audio->read_phys);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001985 }
1986 mutex_unlock(&audio->lock);
1987#ifdef CONFIG_DEBUG_FS
1988 if (audio->dentry)
1989 debugfs_remove(audio->dentry);
1990#endif
1991 kfree(audio);
1992 return 0;
1993}
1994
1995static void audmp3_post_event(struct audio *audio, int type,
1996 union msm_audio_event_payload payload)
1997{
1998 struct audmp3_event *e_node = NULL;
1999 unsigned long flags;
2000
2001 spin_lock_irqsave(&audio->event_queue_lock, flags);
2002
2003 if (!list_empty(&audio->free_event_queue)) {
2004 e_node = list_first_entry(&audio->free_event_queue,
2005 struct audmp3_event, list);
2006 list_del(&e_node->list);
2007 } else {
2008 e_node = kmalloc(sizeof(struct audmp3_event), GFP_ATOMIC);
2009 if (!e_node) {
2010 MM_ERR("No mem to post event %d\n", type);
2011 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
2012 return;
2013 }
2014 }
2015
2016 e_node->event_type = type;
2017 e_node->payload = payload;
2018
2019 list_add_tail(&e_node->list, &audio->event_queue);
2020 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
2021 wake_up(&audio->event_wait);
2022}
2023
2024#ifdef CONFIG_HAS_EARLYSUSPEND
2025static void audmp3_suspend(struct early_suspend *h)
2026{
2027 struct audmp3_suspend_ctl *ctl =
2028 container_of(h, struct audmp3_suspend_ctl, node);
2029 union msm_audio_event_payload payload;
2030
2031 MM_DBG("\n"); /* Macro prints the file name and function */
2032 audmp3_post_event(ctl->audio, AUDIO_EVENT_SUSPEND, payload);
2033}
2034
2035static void audmp3_resume(struct early_suspend *h)
2036{
2037 struct audmp3_suspend_ctl *ctl =
2038 container_of(h, struct audmp3_suspend_ctl, node);
2039 union msm_audio_event_payload payload;
2040
2041 MM_DBG("\n"); /* Macro prints the file name and function */
2042 audmp3_post_event(ctl->audio, AUDIO_EVENT_RESUME, payload);
2043}
2044#endif
2045
2046#ifdef CONFIG_DEBUG_FS
2047static ssize_t audmp3_debug_open(struct inode *inode, struct file *file)
2048{
2049 file->private_data = inode->i_private;
2050 return 0;
2051}
2052
2053static ssize_t audmp3_debug_read(struct file *file, char __user *buf,
2054 size_t count, loff_t *ppos)
2055{
2056 const int debug_bufmax = 4096;
2057 static char buffer[4096];
2058 int n = 0, i;
2059 struct audio *audio = file->private_data;
2060
2061 mutex_lock(&audio->lock);
2062 n = scnprintf(buffer, debug_bufmax, "opened %d\n", audio->opened);
2063 n += scnprintf(buffer + n, debug_bufmax - n,
2064 "enabled %d\n", audio->enabled);
2065 n += scnprintf(buffer + n, debug_bufmax - n,
2066 "stopped %d\n", audio->stopped);
2067 n += scnprintf(buffer + n, debug_bufmax - n,
2068 "pcm_feedback %d\n", audio->pcm_feedback);
2069 n += scnprintf(buffer + n, debug_bufmax - n,
2070 "out_buf_sz %d\n", audio->out[0].size);
2071 n += scnprintf(buffer + n, debug_bufmax - n,
2072 "pcm_buf_count %d \n", audio->pcm_buf_count);
2073 n += scnprintf(buffer + n, debug_bufmax - n,
2074 "pcm_buf_sz %d \n", audio->in[0].size);
2075 n += scnprintf(buffer + n, debug_bufmax - n,
2076 "volume %x \n", audio->vol_pan.volume);
2077 n += scnprintf(buffer + n, debug_bufmax - n,
2078 "sample rate %d \n", audio->out_sample_rate);
2079 n += scnprintf(buffer + n, debug_bufmax - n,
2080 "channel mode %d \n", audio->out_channel_mode);
2081 mutex_unlock(&audio->lock);
2082 /* Following variables are only useful for debugging when
2083 * when playback halts unexpectedly. Thus, no mutual exclusion
2084 * enforced
2085 */
2086 n += scnprintf(buffer + n, debug_bufmax - n,
2087 "wflush %d\n", audio->wflush);
2088 n += scnprintf(buffer + n, debug_bufmax - n,
2089 "rflush %d\n", audio->rflush);
2090 n += scnprintf(buffer + n, debug_bufmax - n,
2091 "running %d \n", audio->running);
2092 n += scnprintf(buffer + n, debug_bufmax - n,
2093 "dec state %d \n", audio->dec_state);
2094 n += scnprintf(buffer + n, debug_bufmax - n,
2095 "out_needed %d \n", audio->out_needed);
2096 n += scnprintf(buffer + n, debug_bufmax - n,
2097 "out_head %d \n", audio->out_head);
2098 n += scnprintf(buffer + n, debug_bufmax - n,
2099 "out_tail %d \n", audio->out_tail);
2100 n += scnprintf(buffer + n, debug_bufmax - n,
2101 "out[0].used %d \n", audio->out[0].used);
2102 n += scnprintf(buffer + n, debug_bufmax - n,
2103 "out[1].used %d \n", audio->out[1].used);
2104 n += scnprintf(buffer + n, debug_bufmax - n,
2105 "buffer_refresh %d \n", audio->buf_refresh);
2106 n += scnprintf(buffer + n, debug_bufmax - n,
2107 "read_next %d \n", audio->read_next);
2108 n += scnprintf(buffer + n, debug_bufmax - n,
2109 "fill_next %d \n", audio->fill_next);
2110 for (i = 0; i < audio->pcm_buf_count; i++)
2111 n += scnprintf(buffer + n, debug_bufmax - n,
2112 "in[%d].size %d \n", i, audio->in[i].used);
2113 buffer[n] = 0;
2114 return simple_read_from_buffer(buf, count, ppos, buffer, n);
2115}
2116
2117static const struct file_operations audmp3_debug_fops = {
2118 .read = audmp3_debug_read,
2119 .open = audmp3_debug_open,
2120};
2121#endif
2122
2123static int audio_open(struct inode *inode, struct file *file)
2124{
2125
2126 struct audio *audio = NULL;
2127 int rc, i, dec_attrb, decid;
2128 struct audmp3_event *e_node = NULL;
2129 unsigned pmem_sz = DMASZ_MAX;
2130#ifdef CONFIG_DEBUG_FS
2131 /* 4 bytes represents decoder number, 1 byte for terminate string */
2132 char name[sizeof "msm_mp3_" + 5];
2133#endif
2134
2135 /* Allocate audio instance, set to zero */
2136 audio = kzalloc(sizeof(struct audio), GFP_KERNEL);
2137 if (!audio) {
2138 MM_ERR("no memory to allocate audio instance \n");
2139 rc = -ENOMEM;
2140 goto done;
2141 }
2142 MM_INFO("audio instance 0x%08x created\n", (int)audio);
2143
2144 /* Allocate the decoder */
2145 dec_attrb = AUDDEC_DEC_MP3;
2146 if ((file->f_mode & FMODE_WRITE) &&
2147 (file->f_mode & FMODE_READ)) {
2148 dec_attrb |= MSM_AUD_MODE_NONTUNNEL;
2149 audio->pcm_feedback = NON_TUNNEL_MODE_PLAYBACK;
2150 } else if ((file->f_mode & FMODE_WRITE) &&
2151 !(file->f_mode & FMODE_READ)) {
2152 dec_attrb |= MSM_AUD_MODE_TUNNEL;
2153 audio->pcm_feedback = TUNNEL_MODE_PLAYBACK;
2154 } else {
2155 kfree(audio);
2156 rc = -EACCES;
2157 goto done;
2158 }
2159
2160 decid = audpp_adec_alloc(dec_attrb, &audio->module_name,
2161 &audio->queue_id);
2162 if (decid < 0) {
2163 MM_ERR("No free decoder available, freeing instance 0x%08x\n",
2164 (int)audio);
2165 rc = -ENODEV;
2166 kfree(audio);
2167 goto done;
2168 }
2169 audio->dec_id = decid & MSM_AUD_DECODER_MASK;
2170
2171 /* Non AIO interface */
2172 if (!(file->f_flags & O_NONBLOCK)) {
2173 while (pmem_sz >= DMASZ_MIN) {
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05302174 MM_DBG("pmemsz = %d\n", pmem_sz);
2175 audio->phys = allocate_contiguous_ebi_nomap(pmem_sz,
2176 SZ_4K);
2177 if (audio->phys) {
Laura Abbott35111d32012-04-27 18:41:48 -07002178 audio->map_v_write = ioremap(
2179 audio->phys, pmem_sz);
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05302180 if (IS_ERR(audio->map_v_write)) {
2181 MM_ERR("could not map write \
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002182 buffers, freeing instance \
2183 0x%08x\n", (int)audio);
2184 rc = -ENOMEM;
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05302185 free_contiguous_memory_by_paddr(
2186 audio->phys);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002187 audpp_adec_free(audio->dec_id);
2188 kfree(audio);
2189 goto done;
2190 }
Laura Abbott35111d32012-04-27 18:41:48 -07002191 audio->data = audio->map_v_write;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002192 MM_DBG("write buf: phy addr 0x%08x kernel addr\
2193 0x%08x\n", audio->phys,\
2194 (int)audio->data);
2195 break;
2196 } else if (pmem_sz == DMASZ_MIN) {
2197 MM_ERR("could not allocate write buffers, \
2198 freeing instance 0x%08x\n",
2199 (int)audio);
2200 rc = -ENOMEM;
2201 audpp_adec_free(audio->dec_id);
2202 kfree(audio);
2203 goto done;
2204 } else
2205 pmem_sz >>= 1;
2206 }
2207 audio->out_dma_sz = pmem_sz;
2208 }
2209
2210 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK) {
2211 rc = audmgr_open(&audio->audmgr);
2212 if (rc) {
2213 MM_ERR("audmgr open failed, freeing instance \
2214 0x%08x\n", (int)audio);
2215 goto err;
2216 }
2217 }
2218
2219 rc = msm_adsp_get(audio->module_name, &audio->audplay,
2220 &audplay_adsp_ops, audio);
2221
2222 if (rc) {
2223 MM_ERR("failed to get %s module, freeing instance 0x%08x\n",
2224 audio->module_name, (int)audio);
2225 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK)
2226 audmgr_close(&audio->audmgr);
2227 goto err;
2228 }
2229
2230 rc = rmt_get_resource(audio);
2231 if (rc) {
2232 MM_ERR("ADSP resources are not available for MP3 session \
2233 0x%08x on decoder: %d\n", (int)audio, audio->dec_id);
2234 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK)
2235 audmgr_close(&audio->audmgr);
2236 msm_adsp_put(audio->audplay);
2237 goto err;
2238 }
2239
2240 if (file->f_flags & O_NONBLOCK) {
2241 MM_DBG("set to aio interface\n");
2242 audio->drv_status |= ADRV_STATUS_AIO_INTF;
2243 audio->drv_ops.pcm_buf_update = audmp3_async_pcm_buf_update;
2244 audio->drv_ops.buffer_refresh = audmp3_async_buffer_refresh;
2245 audio->drv_ops.send_data = audmp3_async_send_data;
2246 audio->drv_ops.out_flush = audmp3_async_flush;
2247 audio->drv_ops.in_flush = audmp3_async_flush_pcm_buf;
2248 audio->drv_ops.fsync = audmp3_async_fsync;
2249 } else {
2250 MM_DBG("set to std io interface\n");
2251 audio->drv_ops.pcm_buf_update = audio_update_pcm_buf_entry;
2252 audio->drv_ops.buffer_refresh = audplay_buffer_refresh;
2253 audio->drv_ops.send_data = audplay_send_data;
2254 audio->drv_ops.out_flush = audio_flush;
2255 audio->drv_ops.in_flush = audio_flush_pcm_buf;
2256 audio->drv_ops.fsync = audmp3_sync_fsync;
2257 audio->out[0].data = audio->data + 0;
2258 audio->out[0].addr = audio->phys + 0;
2259 audio->out[0].size = (audio->out_dma_sz >> 1);
2260
2261 audio->out[1].data = audio->data + audio->out[0].size;
2262 audio->out[1].addr = audio->phys + audio->out[0].size;
2263 audio->out[1].size = audio->out[0].size;
2264 }
2265
2266 /* Initialize all locks of audio instance */
2267 mutex_init(&audio->lock);
2268 mutex_init(&audio->write_lock);
2269 mutex_init(&audio->read_lock);
2270 mutex_init(&audio->get_event_lock);
2271 spin_lock_init(&audio->dsp_lock);
2272 init_waitqueue_head(&audio->write_wait);
2273 init_waitqueue_head(&audio->read_wait);
2274 INIT_LIST_HEAD(&audio->out_queue);
2275 INIT_LIST_HEAD(&audio->in_queue);
2276 INIT_LIST_HEAD(&audio->pmem_region_queue);
2277 INIT_LIST_HEAD(&audio->free_event_queue);
2278 INIT_LIST_HEAD(&audio->event_queue);
2279 init_waitqueue_head(&audio->wait);
2280 init_waitqueue_head(&audio->event_wait);
2281 spin_lock_init(&audio->event_queue_lock);
2282
2283 audio->out_sample_rate = 44100;
2284 audio->out_channel_mode = AUDPP_CMD_PCM_INTF_STEREO_V;
2285 audio->vol_pan.volume = 0x2000;
2286
2287 audio->drv_ops.out_flush(audio);
2288
2289 file->private_data = audio;
2290 audio->opened = 1;
2291#ifdef CONFIG_DEBUG_FS
2292 snprintf(name, sizeof name, "msm_mp3_%04x", audio->dec_id);
2293 audio->dentry = debugfs_create_file(name, S_IFREG | S_IRUGO,
2294 NULL, (void *) audio, &audmp3_debug_fops);
2295
2296 if (IS_ERR(audio->dentry))
2297 MM_DBG("debugfs_create_file failed\n");
2298#endif
2299#ifdef CONFIG_HAS_EARLYSUSPEND
2300 audio->suspend_ctl.node.level = EARLY_SUSPEND_LEVEL_DISABLE_FB;
2301 audio->suspend_ctl.node.resume = audmp3_resume;
2302 audio->suspend_ctl.node.suspend = audmp3_suspend;
2303 audio->suspend_ctl.audio = audio;
2304 register_early_suspend(&audio->suspend_ctl.node);
2305#endif
2306 for (i = 0; i < AUDMP3_EVENT_NUM; i++) {
2307 e_node = kmalloc(sizeof(struct audmp3_event), GFP_KERNEL);
2308 if (e_node)
2309 list_add_tail(&e_node->list, &audio->free_event_queue);
2310 else {
2311 MM_ERR("event pkt alloc failed\n");
2312 break;
2313 }
2314 }
2315done:
2316 return rc;
2317err:
2318 if (audio->data) {
Laura Abbott35111d32012-04-27 18:41:48 -07002319 iounmap(audio->map_v_write);
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05302320 free_contiguous_memory_by_paddr(audio->phys);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002321 }
2322 audpp_adec_free(audio->dec_id);
2323 kfree(audio);
2324 return rc;
2325}
2326
2327static const struct file_operations audio_mp3_fops = {
2328 .owner = THIS_MODULE,
2329 .open = audio_open,
2330 .release = audio_release,
2331 .read = audio_read,
2332 .write = audio_write,
2333 .unlocked_ioctl = audio_ioctl,
2334 .fsync = audmp3_fsync,
2335};
2336
2337struct miscdevice audio_mp3_misc = {
2338 .minor = MISC_DYNAMIC_MINOR,
2339 .name = "msm_mp3",
2340 .fops = &audio_mp3_fops,
2341};
2342
2343static int __init audio_init(void)
2344{
2345 return misc_register(&audio_mp3_misc);
2346}
2347
2348static void __exit audio_exit(void)
2349{
2350 misc_deregister(&audio_mp3_misc);
2351}
2352
2353module_init(audio_init);
2354module_exit(audio_exit);
2355
2356MODULE_DESCRIPTION("MSM MP3 driver");
2357MODULE_LICENSE("GPL v2");