blob: dfd85288290f230719e318c17b6fab435376b758 [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>
42#include <mach/msm_subsystem_map.h>
43#include <mach/msm_memtypes.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070044#include <mach/qdsp5/qdsp5audppcmdi.h>
45#include <mach/qdsp5/qdsp5audppmsg.h>
46#include <mach/qdsp5/qdsp5audplaycmdi.h>
47#include <mach/qdsp5/qdsp5audplaymsg.h>
48#include <mach/qdsp5/qdsp5rmtcmdi.h>
49#include <mach/debug_mm.h>
50
Santosh Mardi0be3b8e2011-07-06 10:00:21 +053051#include "audmgr.h"
52
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070053#define ADRV_STATUS_AIO_INTF 0x00000001
54#define ADRV_STATUS_OBUF_GIVEN 0x00000002
55#define ADRV_STATUS_IBUF_GIVEN 0x00000004
56#define ADRV_STATUS_FSYNC 0x00000008
57
58/* Size must be power of 2 */
59#define BUFSZ_MAX 32768
60#define BUFSZ_MIN 4096
61#define DMASZ_MAX (BUFSZ_MAX * 2)
62#define DMASZ_MIN (BUFSZ_MIN * 2)
63
64#define AUDPLAY_INVALID_READ_PTR_OFFSET 0xFFFF
65#define AUDDEC_DEC_MP3 2
66
67#define PCM_BUFSZ_MIN 4800 /* Hold one stereo MP3 frame */
68#define PCM_BUF_MAX_COUNT 5 /* DSP only accepts 5 buffers at most
69 but support 2 buffers currently */
70#define ROUTING_MODE_FTRT 1
71#define ROUTING_MODE_RT 2
72/* Decoder status received from AUDPPTASK */
73#define AUDPP_DEC_STATUS_SLEEP 0
74#define AUDPP_DEC_STATUS_INIT 1
75#define AUDPP_DEC_STATUS_CFG 2
76#define AUDPP_DEC_STATUS_PLAY 3
77
78#define AUDMP3_METAFIELD_MASK 0xFFFF0000
79#define AUDMP3_EOS_FLG_OFFSET 0x0A /* Offset from beginning of buffer */
80#define AUDMP3_EOS_FLG_MASK 0x01
81#define AUDMP3_EOS_NONE 0x0 /* No EOS detected */
82#define AUDMP3_EOS_SET 0x1 /* EOS set in meta field */
83
84#define AUDMP3_EVENT_NUM 10 /* Default number of pre-allocated event packets */
85
86#define __CONTAINS(r, v, l) ({ \
87 typeof(r) __r = r; \
88 typeof(v) __v = v; \
89 typeof(v) __e = __v + l; \
90 int res = ((__v >= __r->vaddr) && \
91 (__e <= __r->vaddr + __r->len)); \
92 res; \
93})
94
95#define CONTAINS(r1, r2) ({ \
96 typeof(r2) __r2 = r2; \
97 __CONTAINS(r1, __r2->vaddr, __r2->len); \
98})
99
100#define IN_RANGE(r, v) ({ \
101 typeof(r) __r = r; \
102 typeof(v) __vv = v; \
103 int res = ((__vv >= __r->vaddr) && \
104 (__vv < (__r->vaddr + __r->len))); \
105 res; \
106})
107
108#define OVERLAPS(r1, r2) ({ \
109 typeof(r1) __r1 = r1; \
110 typeof(r2) __r2 = r2; \
111 typeof(__r2->vaddr) __v = __r2->vaddr; \
112 typeof(__v) __e = __v + __r2->len - 1; \
113 int res = (IN_RANGE(__r1, __v) || IN_RANGE(__r1, __e)); \
114 res; \
115})
116
117struct audio;
118
119struct buffer {
120 void *data;
121 unsigned size;
122 unsigned used; /* Input usage actual DSP produced PCM size */
123 unsigned addr;
124 unsigned short mfield_sz; /*only useful for data has meta field */
125};
126
127#ifdef CONFIG_HAS_EARLYSUSPEND
128struct audmp3_suspend_ctl {
129 struct early_suspend node;
130 struct audio *audio;
131};
132#endif
133
134struct audmp3_event {
135 struct list_head list;
136 int event_type;
137 union msm_audio_event_payload payload;
138};
139
140struct audmp3_pmem_region {
141 struct list_head list;
142 struct file *file;
143 int fd;
144 void *vaddr;
145 unsigned long paddr;
146 unsigned long kvaddr;
147 unsigned long len;
148 unsigned ref_cnt;
149};
150
151struct audmp3_buffer_node {
152 struct list_head list;
153 struct msm_audio_aio_buf buf;
154 unsigned long paddr;
155};
156
157struct audmp3_drv_operations {
158 void (*pcm_buf_update)(struct audio *, uint32_t *);
159 void (*buffer_refresh)(struct audio *);
160 void (*send_data)(struct audio *, unsigned);
161 void (*out_flush)(struct audio *);
162 void (*in_flush)(struct audio *);
163 int (*fsync)(struct audio *);
164};
165
166struct audio {
167 struct buffer out[2];
168
169 spinlock_t dsp_lock;
170
171 uint8_t out_head;
172 uint8_t out_tail;
173 uint8_t out_needed; /* number of buffers the dsp is waiting for */
174 unsigned out_dma_sz;
175 struct list_head out_queue; /* queue to retain output buffers */
176 atomic_t out_bytes;
177
178 struct mutex lock;
179 struct mutex write_lock;
180 wait_queue_head_t write_wait;
181
182 /* Host PCM section */
183 struct buffer in[PCM_BUF_MAX_COUNT];
184 struct mutex read_lock;
185 wait_queue_head_t read_wait; /* Wait queue for read */
186 char *read_data; /* pointer to reader buffer */
187 int32_t read_phys; /* physical address of reader buffer */
188 uint8_t read_next; /* index to input buffers to be read next */
189 uint8_t fill_next; /* index to buffer that DSP should be filling */
190 uint8_t pcm_buf_count; /* number of pcm buffer allocated */
191 struct list_head in_queue; /* queue to retain input buffers */
192 /* ---- End of Host PCM section */
193
194 struct msm_adsp_module *audplay;
195
196 /* configuration to use on next enable */
197 uint32_t out_sample_rate;
198 uint32_t out_channel_mode;
199
200 struct audmgr audmgr;
201
202 /* data allocated for various buffers */
203 char *data;
204 int32_t phys; /* physical address of write buffer */
Santosh Mardi0be3b8e2011-07-06 10:00:21 +0530205 struct msm_mapped_buffer *map_v_read;
206 struct msm_mapped_buffer *map_v_write;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700207
208 uint32_t drv_status;
209 int mfield; /* meta field embedded in data */
210 int rflush; /* Read flush */
211 int wflush; /* Write flush */
212 int opened;
213 int enabled;
214 int running;
215 int stopped; /* set when stopped, cleared on flush */
216 int pcm_feedback;
217 int buf_refresh;
218 int rmt_resource_released;
219 int teos; /* valid only if tunnel mode & no data left for decoder */
220 enum msm_aud_decoder_state dec_state; /* Represents decoder state */
221 int reserved; /* A byte is being reserved */
222 char rsv_byte; /* Handle odd length user data */
223
224 const char *module_name;
225 unsigned queue_id;
226 uint16_t dec_id;
227 uint32_t read_ptr_offset;
228
229#ifdef CONFIG_HAS_EARLYSUSPEND
230 struct audmp3_suspend_ctl suspend_ctl;
231#endif
232
233#ifdef CONFIG_DEBUG_FS
234 struct dentry *dentry;
235#endif
236
237 wait_queue_head_t wait;
238 struct list_head free_event_queue;
239 struct list_head event_queue;
240 wait_queue_head_t event_wait;
241 spinlock_t event_queue_lock;
242 struct mutex get_event_lock;
243 int event_abort;
244
245 struct list_head pmem_region_queue; /* protected by lock */
246 struct audmp3_drv_operations drv_ops;
247
248 int eq_enable;
249 int eq_needs_commit;
250 audpp_cmd_cfg_object_params_eqalizer eq;
251 audpp_cmd_cfg_object_params_volume vol_pan;
252};
253
254static int auddec_dsp_config(struct audio *audio, int enable);
255static void audpp_cmd_cfg_adec_params(struct audio *audio);
256static void audpp_cmd_cfg_routing_mode(struct audio *audio);
257static void audplay_send_data(struct audio *audio, unsigned needed);
258static void audplay_config_hostpcm(struct audio *audio);
259static void audplay_buffer_refresh(struct audio *audio);
260static void audio_dsp_event(void *private, unsigned id, uint16_t *msg);
261static void audmp3_post_event(struct audio *audio, int type,
262 union msm_audio_event_payload payload);
263static unsigned long audmp3_pmem_fixup(struct audio *audio, void *addr,
264 unsigned long len, int ref_up);
265
266static int rmt_put_resource(struct audio *audio)
267{
268 struct aud_codec_config_cmd cmd;
269 unsigned short client_idx;
270
271 cmd.cmd_id = RM_CMD_AUD_CODEC_CFG;
272 cmd.client_id = RM_AUD_CLIENT_ID;
273 cmd.task_id = audio->dec_id;
274 cmd.enable = RMT_DISABLE;
275 cmd.dec_type = AUDDEC_DEC_MP3;
276 client_idx = ((cmd.client_id << 8) | cmd.task_id);
277
278 return put_adsp_resource(client_idx, &cmd, sizeof(cmd));
279}
280
281static int rmt_get_resource(struct audio *audio)
282{
283 struct aud_codec_config_cmd cmd;
284 unsigned short client_idx;
285
286 cmd.cmd_id = RM_CMD_AUD_CODEC_CFG;
287 cmd.client_id = RM_AUD_CLIENT_ID;
288 cmd.task_id = audio->dec_id;
289 cmd.enable = RMT_ENABLE;
290 cmd.dec_type = AUDDEC_DEC_MP3;
291 client_idx = ((cmd.client_id << 8) | cmd.task_id);
292
293 return get_adsp_resource(client_idx, &cmd, sizeof(cmd));
294}
295
296/* must be called with audio->lock held */
297static int audio_enable(struct audio *audio)
298{
299 struct audmgr_config cfg;
300 int rc;
301
302 MM_DBG("\n"); /* Macro prints the file name and function */
303
304 if (audio->enabled)
305 return 0;
306
307 if (audio->rmt_resource_released == 1) {
308 audio->rmt_resource_released = 0;
309 rc = rmt_get_resource(audio);
310 if (rc) {
311 MM_ERR("ADSP resources are not available for MP3 \
312 session 0x%08x on decoder: %d\n Ignoring \
313 error and going ahead with the playback\n",
314 (int)audio, audio->dec_id);
315 }
316 }
317
318 audio->dec_state = MSM_AUD_DECODER_STATE_NONE;
319 audio->out_tail = 0;
320 audio->out_needed = 0;
321
322 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK) {
323 cfg.tx_rate = RPC_AUD_DEF_SAMPLE_RATE_NONE;
324 cfg.rx_rate = RPC_AUD_DEF_SAMPLE_RATE_48000;
325 cfg.def_method = RPC_AUD_DEF_METHOD_PLAYBACK;
326 cfg.codec = RPC_AUD_DEF_CODEC_MP3;
327 cfg.snd_method = RPC_SND_METHOD_MIDI;
328
329 rc = audmgr_enable(&audio->audmgr, &cfg);
330 if (rc < 0)
331 return rc;
332 }
333
334 if (msm_adsp_enable(audio->audplay)) {
335 MM_ERR("msm_adsp_enable(audplay) failed\n");
336 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK)
337 audmgr_disable(&audio->audmgr);
338 return -ENODEV;
339 }
340
341 if (audpp_enable(audio->dec_id, audio_dsp_event, audio)) {
342 MM_ERR("audpp_enable() failed\n");
343 msm_adsp_disable(audio->audplay);
344 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK)
345 audmgr_disable(&audio->audmgr);
346 return -ENODEV;
347 }
348
349 audio->enabled = 1;
350 return 0;
351}
352
353/* must be called with audio->lock held */
354static int audio_disable(struct audio *audio)
355{
356 int rc = 0;
357 MM_DBG("\n"); /* Macro prints the file name and function */
358 if (audio->enabled) {
359 audio->enabled = 0;
360 audio->dec_state = MSM_AUD_DECODER_STATE_NONE;
361 auddec_dsp_config(audio, 0);
362 rc = wait_event_interruptible_timeout(audio->wait,
363 audio->dec_state != MSM_AUD_DECODER_STATE_NONE,
364 msecs_to_jiffies(MSM_AUD_DECODER_WAIT_MS));
365 if (rc == 0)
366 rc = -ETIMEDOUT;
367 else if (audio->dec_state != MSM_AUD_DECODER_STATE_CLOSE)
368 rc = -EFAULT;
369 else
370 rc = 0;
371 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);
1375 audio->stopped = 1;
1376 audio_ioport_reset(audio);
1377 audio->stopped = 0;
1378 break;
1379 case AUDIO_FLUSH:
1380 MM_DBG("AUDIO_FLUSH\n");
1381 audio->rflush = 1;
1382 audio->wflush = 1;
1383 audio_ioport_reset(audio);
1384 if (audio->running) {
1385 audpp_flush(audio->dec_id);
1386 rc = wait_event_interruptible(audio->write_wait,
1387 !audio->wflush);
1388 if (rc < 0) {
1389 MM_ERR("AUDIO_FLUSH interrupted\n");
1390 rc = -EINTR;
1391 }
1392 } else {
1393 audio->rflush = 0;
1394 audio->wflush = 0;
1395 }
1396 break;
1397
1398 case AUDIO_SET_CONFIG: {
1399 struct msm_audio_config config;
1400 if (copy_from_user(&config, (void *) arg, sizeof(config))) {
1401 rc = -EFAULT;
1402 break;
1403 }
1404 if (config.channel_count == 1) {
1405 config.channel_count = AUDPP_CMD_PCM_INTF_MONO_V;
1406 } else if (config.channel_count == 2) {
1407 config.channel_count = AUDPP_CMD_PCM_INTF_STEREO_V;
1408 } else {
1409 rc = -EINVAL;
1410 break;
1411 }
1412 audio->mfield = config.meta_field;
1413 audio->out_sample_rate = config.sample_rate;
1414 audio->out_channel_mode = config.channel_count;
1415 rc = 0;
1416 break;
1417 }
1418 case AUDIO_GET_CONFIG: {
1419 struct msm_audio_config config;
1420 config.buffer_size = (audio->out_dma_sz >> 1);
1421 config.buffer_count = 2;
1422 config.sample_rate = audio->out_sample_rate;
1423 if (audio->out_channel_mode == AUDPP_CMD_PCM_INTF_MONO_V) {
1424 config.channel_count = 1;
1425 } else {
1426 config.channel_count = 2;
1427 }
1428 config.meta_field = 0;
1429 config.unused[0] = 0;
1430 config.unused[1] = 0;
1431 config.unused[2] = 0;
1432 if (copy_to_user((void *) arg, &config, sizeof(config))) {
1433 rc = -EFAULT;
1434 } else {
1435 rc = 0;
1436 }
1437 break;
1438 }
1439 case AUDIO_GET_PCM_CONFIG:{
1440 struct msm_audio_pcm_config config;
1441 config.pcm_feedback = audio->pcm_feedback;
1442 config.buffer_count = PCM_BUF_MAX_COUNT;
1443 config.buffer_size = PCM_BUFSZ_MIN;
1444 if (copy_to_user((void *)arg, &config,
1445 sizeof(config)))
1446 rc = -EFAULT;
1447 else
1448 rc = 0;
1449 break;
1450 }
1451 case AUDIO_SET_PCM_CONFIG:{
1452 struct msm_audio_pcm_config config;
1453 if (copy_from_user
1454 (&config, (void *)arg, sizeof(config))) {
1455 rc = -EFAULT;
1456 break;
1457 }
1458
1459 if (config.pcm_feedback != audio->pcm_feedback) {
1460 MM_ERR("Not sufficient permission to"
1461 "change the playback mode\n");
1462 rc = -EACCES;
1463 break;
1464 }
1465 if (audio->drv_status & ADRV_STATUS_AIO_INTF) {
1466 rc = 0;
1467 break;
1468 }
1469
1470 if ((config.buffer_count > PCM_BUF_MAX_COUNT) ||
1471 (config.buffer_count == 1))
1472 config.buffer_count = PCM_BUF_MAX_COUNT;
1473
1474 if (config.buffer_size < PCM_BUFSZ_MIN)
1475 config.buffer_size = PCM_BUFSZ_MIN;
1476
1477 /* Check if pcm feedback is required */
1478 if ((config.pcm_feedback) && (!audio->read_data)) {
1479 MM_DBG("allocate PCM buffer %d\n",
1480 config.buffer_count *
1481 config.buffer_size);
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05301482 audio->read_phys =
1483 allocate_contiguous_ebi_nomap(
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001484 config.buffer_size *
1485 config.buffer_count,
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05301486 SZ_4K);
1487 if (!audio->read_phys) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001488 rc = -ENOMEM;
1489 break;
1490 }
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05301491 audio->map_v_read = msm_subsystem_map_buffer(
1492 audio->read_phys,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001493 config.buffer_size *
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05301494 config.buffer_count,
1495 MSM_SUBSYSTEM_MAP_KADDR,
1496 NULL, 0);
1497
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 =
1507 audio->map_v_read->vaddr;
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) {
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05301982 msm_subsystem_unmap_buffer(audio->map_v_write);
1983 free_contiguous_memory_by_paddr(audio->phys);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001984 }
1985 if (audio->read_data) {
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05301986 msm_subsystem_unmap_buffer(audio->map_v_read);
1987 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) {
2181 audio->map_v_write = msm_subsystem_map_buffer(
2182 audio->phys, pmem_sz,
2183 MSM_SUBSYSTEM_MAP_KADDR,
2184 NULL, 0);
2185 if (IS_ERR(audio->map_v_write)) {
2186 MM_ERR("could not map write \
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002187 buffers, freeing instance \
2188 0x%08x\n", (int)audio);
2189 rc = -ENOMEM;
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05302190 free_contiguous_memory_by_paddr(
2191 audio->phys);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002192 audpp_adec_free(audio->dec_id);
2193 kfree(audio);
2194 goto done;
2195 }
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05302196 audio->data = audio->map_v_write->vaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002197 MM_DBG("write buf: phy addr 0x%08x kernel addr\
2198 0x%08x\n", audio->phys,\
2199 (int)audio->data);
2200 break;
2201 } else if (pmem_sz == DMASZ_MIN) {
2202 MM_ERR("could not allocate write buffers, \
2203 freeing instance 0x%08x\n",
2204 (int)audio);
2205 rc = -ENOMEM;
2206 audpp_adec_free(audio->dec_id);
2207 kfree(audio);
2208 goto done;
2209 } else
2210 pmem_sz >>= 1;
2211 }
2212 audio->out_dma_sz = pmem_sz;
2213 }
2214
2215 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK) {
2216 rc = audmgr_open(&audio->audmgr);
2217 if (rc) {
2218 MM_ERR("audmgr open failed, freeing instance \
2219 0x%08x\n", (int)audio);
2220 goto err;
2221 }
2222 }
2223
2224 rc = msm_adsp_get(audio->module_name, &audio->audplay,
2225 &audplay_adsp_ops, audio);
2226
2227 if (rc) {
2228 MM_ERR("failed to get %s module, freeing instance 0x%08x\n",
2229 audio->module_name, (int)audio);
2230 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK)
2231 audmgr_close(&audio->audmgr);
2232 goto err;
2233 }
2234
2235 rc = rmt_get_resource(audio);
2236 if (rc) {
2237 MM_ERR("ADSP resources are not available for MP3 session \
2238 0x%08x on decoder: %d\n", (int)audio, audio->dec_id);
2239 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK)
2240 audmgr_close(&audio->audmgr);
2241 msm_adsp_put(audio->audplay);
2242 goto err;
2243 }
2244
2245 if (file->f_flags & O_NONBLOCK) {
2246 MM_DBG("set to aio interface\n");
2247 audio->drv_status |= ADRV_STATUS_AIO_INTF;
2248 audio->drv_ops.pcm_buf_update = audmp3_async_pcm_buf_update;
2249 audio->drv_ops.buffer_refresh = audmp3_async_buffer_refresh;
2250 audio->drv_ops.send_data = audmp3_async_send_data;
2251 audio->drv_ops.out_flush = audmp3_async_flush;
2252 audio->drv_ops.in_flush = audmp3_async_flush_pcm_buf;
2253 audio->drv_ops.fsync = audmp3_async_fsync;
2254 } else {
2255 MM_DBG("set to std io interface\n");
2256 audio->drv_ops.pcm_buf_update = audio_update_pcm_buf_entry;
2257 audio->drv_ops.buffer_refresh = audplay_buffer_refresh;
2258 audio->drv_ops.send_data = audplay_send_data;
2259 audio->drv_ops.out_flush = audio_flush;
2260 audio->drv_ops.in_flush = audio_flush_pcm_buf;
2261 audio->drv_ops.fsync = audmp3_sync_fsync;
2262 audio->out[0].data = audio->data + 0;
2263 audio->out[0].addr = audio->phys + 0;
2264 audio->out[0].size = (audio->out_dma_sz >> 1);
2265
2266 audio->out[1].data = audio->data + audio->out[0].size;
2267 audio->out[1].addr = audio->phys + audio->out[0].size;
2268 audio->out[1].size = audio->out[0].size;
2269 }
2270
2271 /* Initialize all locks of audio instance */
2272 mutex_init(&audio->lock);
2273 mutex_init(&audio->write_lock);
2274 mutex_init(&audio->read_lock);
2275 mutex_init(&audio->get_event_lock);
2276 spin_lock_init(&audio->dsp_lock);
2277 init_waitqueue_head(&audio->write_wait);
2278 init_waitqueue_head(&audio->read_wait);
2279 INIT_LIST_HEAD(&audio->out_queue);
2280 INIT_LIST_HEAD(&audio->in_queue);
2281 INIT_LIST_HEAD(&audio->pmem_region_queue);
2282 INIT_LIST_HEAD(&audio->free_event_queue);
2283 INIT_LIST_HEAD(&audio->event_queue);
2284 init_waitqueue_head(&audio->wait);
2285 init_waitqueue_head(&audio->event_wait);
2286 spin_lock_init(&audio->event_queue_lock);
2287
2288 audio->out_sample_rate = 44100;
2289 audio->out_channel_mode = AUDPP_CMD_PCM_INTF_STEREO_V;
2290 audio->vol_pan.volume = 0x2000;
2291
2292 audio->drv_ops.out_flush(audio);
2293
2294 file->private_data = audio;
2295 audio->opened = 1;
2296#ifdef CONFIG_DEBUG_FS
2297 snprintf(name, sizeof name, "msm_mp3_%04x", audio->dec_id);
2298 audio->dentry = debugfs_create_file(name, S_IFREG | S_IRUGO,
2299 NULL, (void *) audio, &audmp3_debug_fops);
2300
2301 if (IS_ERR(audio->dentry))
2302 MM_DBG("debugfs_create_file failed\n");
2303#endif
2304#ifdef CONFIG_HAS_EARLYSUSPEND
2305 audio->suspend_ctl.node.level = EARLY_SUSPEND_LEVEL_DISABLE_FB;
2306 audio->suspend_ctl.node.resume = audmp3_resume;
2307 audio->suspend_ctl.node.suspend = audmp3_suspend;
2308 audio->suspend_ctl.audio = audio;
2309 register_early_suspend(&audio->suspend_ctl.node);
2310#endif
2311 for (i = 0; i < AUDMP3_EVENT_NUM; i++) {
2312 e_node = kmalloc(sizeof(struct audmp3_event), GFP_KERNEL);
2313 if (e_node)
2314 list_add_tail(&e_node->list, &audio->free_event_queue);
2315 else {
2316 MM_ERR("event pkt alloc failed\n");
2317 break;
2318 }
2319 }
2320done:
2321 return rc;
2322err:
2323 if (audio->data) {
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05302324 msm_subsystem_unmap_buffer(audio->map_v_write);
2325 free_contiguous_memory_by_paddr(audio->phys);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002326 }
2327 audpp_adec_free(audio->dec_id);
2328 kfree(audio);
2329 return rc;
2330}
2331
2332static const struct file_operations audio_mp3_fops = {
2333 .owner = THIS_MODULE,
2334 .open = audio_open,
2335 .release = audio_release,
2336 .read = audio_read,
2337 .write = audio_write,
2338 .unlocked_ioctl = audio_ioctl,
2339 .fsync = audmp3_fsync,
2340};
2341
2342struct miscdevice audio_mp3_misc = {
2343 .minor = MISC_DYNAMIC_MINOR,
2344 .name = "msm_mp3",
2345 .fops = &audio_mp3_fops,
2346};
2347
2348static int __init audio_init(void)
2349{
2350 return misc_register(&audio_mp3_misc);
2351}
2352
2353static void __exit audio_exit(void)
2354{
2355 misc_deregister(&audio_mp3_misc);
2356}
2357
2358module_init(audio_init);
2359module_exit(audio_exit);
2360
2361MODULE_DESCRIPTION("MSM MP3 driver");
2362MODULE_LICENSE("GPL v2");