/arch/arm/mach-msm/qdsp5v2/audio_mp3.c
C | 2505 lines | 2094 code | 304 blank | 107 comment | 308 complexity | ff24e02e18d75190a0e4efeff7c4ea42 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
Large files files are truncated, but you can click here to view the full file
1/* mp3 audio output device
2 *
3 * Copyright (C) 2008 Google, Inc.
4 * Copyright (C) 2008 HTC Corporation
5 * Copyright (c) 2009-2010, Code Aurora Forum. All rights reserved.
6 *
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 */
17
18#include <linux/module.h>
19#include <linux/fs.h>
20#include <linux/miscdevice.h>
21#include <linux/uaccess.h>
22#include <linux/kthread.h>
23#include <linux/wait.h>
24#include <linux/dma-mapping.h>
25#include <linux/debugfs.h>
26#include <linux/delay.h>
27#include <linux/earlysuspend.h>
28#include <linux/list.h>
29#include <linux/android_pmem.h>
30#include <asm/atomic.h>
31#include <asm/ioctls.h>
32#include <mach/msm_adsp.h>
33
34#include <linux/msm_audio.h>
35#include <mach/qdsp5v2/audio_dev_ctl.h>
36
37#include <mach/qdsp5v2/qdsp5audppmsg.h>
38#include <mach/qdsp5v2/qdsp5audplaycmdi.h>
39#include <mach/qdsp5v2/qdsp5audplaymsg.h>
40#include <mach/qdsp5v2/audio_dev_ctl.h>
41#include <mach/qdsp5v2/audpp.h>
42#include <mach/debug_mm.h>
43#include <linux/slab.h>
44#define ADRV_STATUS_AIO_INTF 0x00000001
45#define ADRV_STATUS_OBUF_GIVEN 0x00000002
46#define ADRV_STATUS_IBUF_GIVEN 0x00000004
47#define ADRV_STATUS_FSYNC 0x00000008
48
49/* Size must be power of 2 */
50#define BUFSZ_MAX 32768
51#define BUFSZ_MIN 4096
52#define DMASZ_MAX (BUFSZ_MAX * 2)
53#define DMASZ_MIN (BUFSZ_MIN * 2)
54
55#define AUDPLAY_INVALID_READ_PTR_OFFSET 0xFFFF
56#define AUDDEC_DEC_MP3 2
57
58#define PCM_BUFSZ_MIN 4800 /* Hold one stereo MP3 frame */
59#define PCM_BUF_MAX_COUNT 5 /* DSP only accepts 5 buffers at most
60 but support 2 buffers currently */
61#define ROUTING_MODE_FTRT 1
62#define ROUTING_MODE_RT 2
63/* Decoder status received from AUDPPTASK */
64#define AUDPP_DEC_STATUS_SLEEP 0
65#define AUDPP_DEC_STATUS_INIT 1
66#define AUDPP_DEC_STATUS_CFG 2
67#define AUDPP_DEC_STATUS_PLAY 3
68
69#define AUDMP3_METAFIELD_MASK 0xFFFF0000
70#define AUDMP3_EOS_FLG_OFFSET 0x0A /* Offset from beginning of buffer */
71#define AUDMP3_EOS_FLG_MASK 0x01
72#define AUDMP3_EOS_NONE 0x0 /* No EOS detected */
73#define AUDMP3_EOS_SET 0x1 /* EOS set in meta field */
74
75#define AUDMP3_EVENT_NUM 10 /* Default number of pre-allocated event packets */
76
77#define BITSTREAM_ERROR_THRESHOLD_VALUE 0x1 /* DEFAULT THRESHOLD VALUE */
78
79#define __CONTAINS(r, v, l) ({ \
80 typeof(r) __r = r; \
81 typeof(v) __v = v; \
82 typeof(v) __e = __v + l; \
83 int res = ((__v >= __r->vaddr) && \
84 (__e <= __r->vaddr + __r->len)); \
85 res; \
86})
87
88#define CONTAINS(r1, r2) ({ \
89 typeof(r2) __r2 = r2; \
90 __CONTAINS(r1, __r2->vaddr, __r2->len); \
91})
92
93#define IN_RANGE(r, v) ({ \
94 typeof(r) __r = r; \
95 typeof(v) __vv = v; \
96 int res = ((__vv >= __r->vaddr) && \
97 (__vv < (__r->vaddr + __r->len))); \
98 res; \
99})
100
101#define OVERLAPS(r1, r2) ({ \
102 typeof(r1) __r1 = r1; \
103 typeof(r2) __r2 = r2; \
104 typeof(__r2->vaddr) __v = __r2->vaddr; \
105 typeof(__v) __e = __v + __r2->len - 1; \
106 int res = (IN_RANGE(__r1, __v) || IN_RANGE(__r1, __e)); \
107 res; \
108})
109
110struct buffer {
111 void *data;
112 unsigned size;
113 unsigned used; /* Input usage actual DSP produced PCM size */
114 unsigned addr;
115 unsigned short mfield_sz; /*only useful for data has meta field */
116};
117
118#ifdef CONFIG_HAS_EARLYSUSPEND
119struct audmp3_suspend_ctl {
120 struct early_suspend node;
121 struct audio *audio;
122};
123#endif
124
125struct audmp3_event {
126 struct list_head list;
127 int event_type;
128 union msm_audio_event_payload payload;
129};
130
131struct audmp3_pmem_region {
132 struct list_head list;
133 struct file *file;
134 int fd;
135 void *vaddr;
136 unsigned long paddr;
137 unsigned long kvaddr;
138 unsigned long len;
139 unsigned ref_cnt;
140};
141
142struct audmp3_buffer_node {
143 struct list_head list;
144 struct msm_audio_aio_buf buf;
145 unsigned long paddr;
146};
147
148struct audmp3_drv_operations {
149 void (*pcm_buf_update)(struct audio *, uint32_t *);
150 void (*buffer_refresh)(struct audio *);
151 void (*send_data)(struct audio *, unsigned);
152 void (*out_flush)(struct audio *);
153 void (*in_flush)(struct audio *);
154 int (*fsync)(struct audio *);
155};
156
157struct audio {
158 struct buffer out[2];
159
160 spinlock_t dsp_lock;
161
162 uint8_t out_head;
163 uint8_t out_tail;
164 uint8_t out_needed; /* number of buffers the dsp is waiting for */
165 unsigned out_dma_sz;
166 struct list_head out_queue; /* queue to retain output buffers */
167 atomic_t out_bytes;
168
169 struct mutex lock;
170 struct mutex write_lock;
171 wait_queue_head_t write_wait;
172
173 /* Host PCM section */
174 struct buffer in[PCM_BUF_MAX_COUNT];
175 struct mutex read_lock;
176 wait_queue_head_t read_wait; /* Wait queue for read */
177 char *read_data; /* pointer to reader buffer */
178 int32_t read_phys; /* physical address of reader buffer */
179 uint8_t read_next; /* index to input buffers to be read next */
180 uint8_t fill_next; /* index to buffer that DSP should be filling */
181 uint8_t pcm_buf_count; /* number of pcm buffer allocated */
182 struct list_head in_queue; /* queue to retain input buffers */
183 /* ---- End of Host PCM section */
184
185 struct msm_adsp_module *audplay;
186
187 /* configuration to use on next enable */
188 uint32_t out_sample_rate;
189 uint32_t out_channel_mode;
190
191 /* data allocated for various buffers */
192 char *data;
193 int32_t phys; /* physical address of write buffer */
194
195 uint32_t drv_status;
196 int mfield; /* meta field embedded in data */
197 int rflush; /* Read flush */
198 int wflush; /* Write flush */
199 int opened;
200 int enabled;
201 int running;
202 int stopped; /* set when stopped, cleared on flush */
203 int pcm_feedback;
204 int buf_refresh;
205 int teos; /* valid only if tunnel mode & no data left for decoder */
206 enum msm_aud_decoder_state dec_state; /* Represents decoder state */
207 int reserved; /* A byte is being reserved */
208 char rsv_byte; /* Handle odd length user data */
209
210 const char *module_name;
211 unsigned queue_id;
212 uint16_t dec_id;
213 uint32_t read_ptr_offset;
214 int16_t source;
215
216#ifdef CONFIG_HAS_EARLYSUSPEND
217 struct audmp3_suspend_ctl suspend_ctl;
218#endif
219
220#ifdef CONFIG_DEBUG_FS
221 struct dentry *dentry;
222#endif
223
224 wait_queue_head_t wait;
225 struct list_head free_event_queue;
226 struct list_head event_queue;
227 wait_queue_head_t event_wait;
228 spinlock_t event_queue_lock;
229 struct mutex get_event_lock;
230 int event_abort;
231 /* AV sync Info */
232 int avsync_flag; /* Flag to indicate feedback from DSP */
233 wait_queue_head_t avsync_wait;/* Wait queue for AV Sync Message */
234 /* flags, 48 bits sample/bytes counter per channel */
235 uint16_t avsync[AUDPP_AVSYNC_CH_COUNT * AUDPP_AVSYNC_NUM_WORDS + 1];
236
237 uint32_t device_events;
238
239 struct list_head pmem_region_queue; /* protected by lock */
240 struct audmp3_drv_operations drv_ops;
241
242 struct msm_audio_bitstream_info stream_info;
243 struct msm_audio_bitstream_error_info bitstream_error_info;
244 uint32_t bitstream_error_threshold_value;
245
246 int eq_enable;
247 int eq_needs_commit;
248 struct audpp_cmd_cfg_object_params_eqalizer eq;
249 struct audpp_cmd_cfg_object_params_volume vol_pan;
250};
251
252static int auddec_dsp_config(struct audio *audio, int enable);
253static void audpp_cmd_cfg_adec_params(struct audio *audio);
254static void audpp_cmd_cfg_routing_mode(struct audio *audio);
255static void audplay_send_data(struct audio *audio, unsigned needed);
256static void audplay_error_threshold_config(struct audio *audio);
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 void mp3_listner(u32 evt_id, union auddev_evt_data *evt_payload,
266 void *private_data)
267{
268 struct audio *audio = (struct audio *) private_data;
269 switch (evt_id) {
270 case AUDDEV_EVT_DEV_RDY:
271 MM_DBG(":AUDDEV_EVT_DEV_RDY\n");
272 audio->source |= (0x1 << evt_payload->routing_id);
273 if (audio->running == 1 && audio->enabled == 1)
274 audpp_route_stream(audio->dec_id, audio->source);
275
276 break;
277 case AUDDEV_EVT_DEV_RLS:
278 MM_DBG(":AUDDEV_EVT_DEV_RLS\n");
279 audio->source &= ~(0x1 << evt_payload->routing_id);
280 if (audio->running == 1 && audio->enabled == 1)
281 audpp_route_stream(audio->dec_id, audio->source);
282 break;
283 case AUDDEV_EVT_STREAM_VOL_CHG:
284 audio->vol_pan.volume = evt_payload->session_vol;
285 MM_DBG(":AUDDEV_EVT_STREAM_VOL_CHG, stream vol %d\n",
286 audio->vol_pan.volume);
287 if (audio->running)
288 audpp_dsp_set_vol_pan(audio->dec_id, &audio->vol_pan,
289 POPP);
290 break;
291 default:
292 MM_ERR(":ERROR:wrong event\n");
293 break;
294 }
295}
296/* must be called with audio->lock held */
297static int audio_enable(struct audio *audio)
298{
299 MM_DBG("\n"); /* Macro prints the file name and function */
300
301 if (audio->enabled)
302 return 0;
303
304 audio->dec_state = MSM_AUD_DECODER_STATE_NONE;
305 audio->out_tail = 0;
306 audio->out_needed = 0;
307
308 if (msm_adsp_enable(audio->audplay)) {
309 MM_ERR("msm_adsp_enable(audplay) failed\n");
310 return -ENODEV;
311 }
312
313 if (audpp_enable(audio->dec_id, audio_dsp_event, audio)) {
314 MM_ERR("audpp_enable() failed\n");
315 msm_adsp_disable(audio->audplay);
316 return -ENODEV;
317 }
318
319 audio->enabled = 1;
320 return 0;
321}
322
323/* must be called with audio->lock held */
324static int audio_disable(struct audio *audio)
325{
326 int rc = 0;
327 MM_DBG("\n"); /* Macro prints the file name and function */
328 if (audio->enabled) {
329 audio->enabled = 0;
330 audio->dec_state = MSM_AUD_DECODER_STATE_NONE;
331 auddec_dsp_config(audio, 0);
332 rc = wait_event_interruptible_timeout(audio->wait,
333 audio->dec_state != MSM_AUD_DECODER_STATE_NONE,
334 msecs_to_jiffies(MSM_AUD_DECODER_WAIT_MS));
335 if (rc == 0)
336 rc = -ETIMEDOUT;
337 else if (audio->dec_state != MSM_AUD_DECODER_STATE_CLOSE)
338 rc = -EFAULT;
339 else
340 rc = 0;
341 wake_up(&audio->write_wait);
342 wake_up(&audio->read_wait);
343 msm_adsp_disable(audio->audplay);
344 audpp_disable(audio->dec_id, audio);
345 audio->out_needed = 0;
346 }
347 return rc;
348}
349
350/* ------------------- dsp --------------------- */
351static void audmp3_async_pcm_buf_update(struct audio *audio, uint32_t *payload)
352{
353 unsigned long flags;
354 union msm_audio_event_payload event_payload;
355 struct audmp3_buffer_node *filled_buf;
356 uint8_t index;
357
358 if (audio->rflush)
359 return;
360
361 spin_lock_irqsave(&audio->dsp_lock, flags);
362 for (index = 0; index < payload[1]; index++) {
363 BUG_ON(list_empty(&audio->in_queue));
364 filled_buf = list_first_entry(&audio->in_queue,
365 struct audmp3_buffer_node, list);
366 if (filled_buf->paddr == payload[2 + index * 2]) {
367 list_del(&filled_buf->list);
368 event_payload.aio_buf = filled_buf->buf;
369 event_payload.aio_buf.data_len =
370 payload[3 + index * 2];
371 MM_DBG("pcm buf %p data_len %d\n", filled_buf,
372 event_payload.aio_buf.data_len);
373 audmp3_post_event(audio, AUDIO_EVENT_READ_DONE,
374 event_payload);
375 kfree(filled_buf);
376 } else {
377 MM_ERR("expected=%lx ret=%x\n", filled_buf->paddr,
378 payload[2 + index * 2]);
379 break;
380 }
381 }
382
383 audio->drv_status &= ~ADRV_STATUS_IBUF_GIVEN;
384 audio->drv_ops.buffer_refresh(audio);
385 spin_unlock_irqrestore(&audio->dsp_lock, flags);
386
387}
388
389static void audio_update_pcm_buf_entry(struct audio *audio, uint32_t *payload)
390{
391 uint8_t index;
392 unsigned long flags;
393
394 if (audio->rflush)
395 return;
396
397 spin_lock_irqsave(&audio->dsp_lock, flags);
398 for (index = 0; index < payload[1]; index++) {
399 if (audio->in[audio->fill_next].addr ==
400 payload[2 + index * 2]) {
401 MM_DBG("in[%d] ready\n", audio->fill_next);
402 audio->in[audio->fill_next].used =
403 payload[3 + index * 2];
404 if ((++audio->fill_next) == audio->pcm_buf_count)
405 audio->fill_next = 0;
406
407 } else {
408 MM_ERR("expected=%x ret=%x\n",
409 audio->in[audio->fill_next].addr,
410 payload[2 + index * 2]);
411 break;
412 }
413 }
414 if (audio->in[audio->fill_next].used == 0) {
415 audio->drv_ops.buffer_refresh(audio);
416 } else {
417 MM_DBG("read cannot keep up\n");
418 audio->buf_refresh = 1;
419 }
420 wake_up(&audio->read_wait);
421 spin_unlock_irqrestore(&audio->dsp_lock, flags);
422
423}
424
425static void audmp3_bitstream_error_info(struct audio *audio, uint32_t *payload)
426{
427 unsigned long flags;
428 union msm_audio_event_payload e_payload;
429
430 if (payload[0] != AUDDEC_DEC_MP3) {
431 MM_ERR("Unexpected bitstream error info from DSP:\
432 Invalid decoder\n");
433 return;
434 }
435
436 /* get stream info from DSP msg */
437 spin_lock_irqsave(&audio->dsp_lock, flags);
438
439 audio->bitstream_error_info.dec_id = payload[0];
440 audio->bitstream_error_info.err_msg_indicator = payload[1];
441 audio->bitstream_error_info.err_type = payload[2];
442
443 spin_unlock_irqrestore(&audio->dsp_lock, flags);
444 MM_ERR("bit_stream_error_type=%d error_count=%d\n",
445 audio->bitstream_error_info.err_type, (0x0000FFFF &
446 audio->bitstream_error_info.err_msg_indicator));
447
448 /* send event to ARM to notify error info coming */
449 e_payload.error_info = audio->bitstream_error_info;
450 audmp3_post_event(audio, AUDIO_EVENT_BITSTREAM_ERROR_INFO, e_payload);
451}
452
453static void audmp3_update_stream_info(struct audio *audio, uint32_t *payload)
454{
455 unsigned long flags;
456 union msm_audio_event_payload e_payload;
457
458 /* get stream info from DSP msg */
459 spin_lock_irqsave(&audio->dsp_lock, flags);
460
461 audio->stream_info.codec_type = AUDIO_CODEC_TYPE_MP3;
462 audio->stream_info.chan_info = (0x0000FFFF & payload[1]);
463 audio->stream_info.sample_rate = (0x0000FFFF & payload[2]);
464 audio->stream_info.bit_stream_info = (0x0000FFFF & payload[3]);
465 audio->stream_info.bit_rate = payload[4];
466
467 spin_unlock_irqrestore(&audio->dsp_lock, flags);
468 MM_DBG("chan_info=%d, sample_rate=%d, bit_stream_info=%d\n",
469 audio->stream_info.chan_info,
470 audio->stream_info.sample_rate,
471 audio->stream_info.bit_stream_info);
472
473 /* send event to ARM to notify steam info coming */
474 e_payload.stream_info = audio->stream_info;
475 audmp3_post_event(audio, AUDIO_EVENT_STREAM_INFO, e_payload);
476}
477
478static void audplay_dsp_event(void *data, unsigned id, size_t len,
479 void (*getevent) (void *ptr, size_t len))
480{
481 struct audio *audio = data;
482 uint32_t msg[28];
483 getevent(msg, sizeof(msg));
484
485 MM_DBG("msg_id=%x\n", id);
486
487 switch (id) {
488 case AUDPLAY_MSG_DEC_NEEDS_DATA:
489 audio->drv_ops.send_data(audio, 1);
490 break;
491
492 case AUDPLAY_MSG_BUFFER_UPDATE:
493 audio->drv_ops.pcm_buf_update(audio, msg);
494 break;
495
496 case AUDPLAY_UP_STREAM_INFO:
497 if ((msg[1] & AUDPLAY_STREAM_INFO_MSG_MASK) ==
498 AUDPLAY_STREAM_INFO_MSG_MASK) {
499 audmp3_bitstream_error_info(audio, msg);
500 } else {
501 audmp3_update_stream_info(audio, msg);
502 }
503 break;
504
505 case AUDPLAY_UP_OUTPORT_FLUSH_ACK:
506 MM_DBG("OUTPORT_FLUSH_ACK\n");
507 audio->rflush = 0;
508 wake_up(&audio->read_wait);
509 if (audio->pcm_feedback)
510 audio->drv_ops.buffer_refresh(audio);
511 break;
512
513 case ADSP_MESSAGE_ID:
514 MM_DBG("Received ADSP event: module enable(audplaytask)\n");
515 break;
516
517 default:
518 MM_ERR("unexpected message from decoder \n");
519 break;
520 }
521}
522
523static void audio_dsp_event(void *private, unsigned id, uint16_t *msg)
524{
525 struct audio *audio = private;
526
527 switch (id) {
528 case AUDPP_MSG_STATUS_MSG:{
529 unsigned status = msg[1];
530
531 switch (status) {
532 case AUDPP_DEC_STATUS_SLEEP: {
533 uint16_t reason = msg[2];
534 MM_DBG("decoder status: sleep reason=0x%04x\n",
535 reason);
536 if ((reason == AUDPP_MSG_REASON_MEM)
537 || (reason ==
538 AUDPP_MSG_REASON_NODECODER)) {
539 audio->dec_state =
540 MSM_AUD_DECODER_STATE_FAILURE;
541 wake_up(&audio->wait);
542 } else if (reason == AUDPP_MSG_REASON_NONE) {
543 /* decoder is in disable state */
544 audio->dec_state =
545 MSM_AUD_DECODER_STATE_CLOSE;
546 wake_up(&audio->wait);
547 }
548 break;
549 }
550 case AUDPP_DEC_STATUS_INIT:
551 MM_DBG("decoder status: init \n");
552 if (audio->pcm_feedback)
553 audpp_cmd_cfg_routing_mode(audio);
554 else
555 audpp_cmd_cfg_adec_params(audio);
556 break;
557
558 case AUDPP_DEC_STATUS_CFG:
559 MM_DBG("decoder status: cfg \n");
560 break;
561 case AUDPP_DEC_STATUS_PLAY:
562 MM_DBG("decoder status: play \n");
563 /* send mixer command */
564 audpp_route_stream(audio->dec_id,
565 audio->source);
566 if (audio->pcm_feedback) {
567 audplay_error_threshold_config(audio);
568 audplay_config_hostpcm(audio);
569 audio->drv_ops.buffer_refresh(audio);
570 }
571 audio->dec_state =
572 MSM_AUD_DECODER_STATE_SUCCESS;
573 wake_up(&audio->wait);
574 break;
575 default:
576 MM_ERR("unknown decoder status \n");
577 break;
578 }
579 break;
580 }
581 case AUDPP_MSG_CFG_MSG:
582 if (msg[0] == AUDPP_MSG_ENA_ENA) {
583 MM_DBG("CFG_MSG ENABLE\n");
584 auddec_dsp_config(audio, 1);
585 audio->out_needed = 0;
586 audio->running = 1;
587 audpp_dsp_set_vol_pan(audio->dec_id, &audio->vol_pan,
588 POPP);
589 audpp_dsp_set_eq(audio->dec_id, audio->eq_enable,
590 &audio->eq, POPP);
591 } else if (msg[0] == AUDPP_MSG_ENA_DIS) {
592 MM_DBG("CFG_MSG DISABLE\n");
593 audio->running = 0;
594 } else {
595 MM_DBG("CFG_MSG %d?\n", msg[0]);
596 }
597 break;
598 case AUDPP_MSG_ROUTING_ACK:
599 MM_DBG("ROUTING_ACK mode=%d\n", msg[1]);
600 audpp_cmd_cfg_adec_params(audio);
601 break;
602
603 case AUDPP_MSG_FLUSH_ACK:
604 MM_DBG("FLUSH_ACK\n");
605 audio->wflush = 0;
606 audio->rflush = 0;
607 wake_up(&audio->write_wait);
608 if (audio->pcm_feedback)
609 audio->drv_ops.buffer_refresh(audio);
610 break;
611
612 case AUDPP_MSG_PCMDMAMISSED:
613 MM_DBG("PCMDMAMISSED\n");
614 audio->teos = 1;
615 wake_up(&audio->write_wait);
616 break;
617
618 case AUDPP_MSG_AVSYNC_MSG:
619 MM_DBG("AUDPP_MSG_AVSYNC_MSG\n");
620 memcpy(&audio->avsync[0], msg, sizeof(audio->avsync));
621 audio->avsync_flag = 1;
622 wake_up(&audio->avsync_wait);
623 break;
624
625 default:
626 MM_ERR("UNKNOWN (%d)\n", id);
627 }
628
629}
630
631
632struct msm_adsp_ops audplay_adsp_ops = {
633 .event = audplay_dsp_event,
634};
635
636
637#define audplay_send_queue0(audio, cmd, len) \
638 msm_adsp_write(audio->audplay, audio->queue_id, \
639 cmd, len)
640
641static int auddec_dsp_config(struct audio *audio, int enable)
642{
643 struct audpp_cmd_cfg_dec_type cfg_dec_cmd;
644
645 memset(&cfg_dec_cmd, 0, sizeof(cfg_dec_cmd));
646
647 cfg_dec_cmd.cmd_id = AUDPP_CMD_CFG_DEC_TYPE;
648 if (enable)
649 cfg_dec_cmd.dec_cfg = AUDPP_CMD_UPDATDE_CFG_DEC |
650 AUDPP_CMD_ENA_DEC_V | AUDDEC_DEC_MP3;
651 else
652 cfg_dec_cmd.dec_cfg = AUDPP_CMD_UPDATDE_CFG_DEC |
653 AUDPP_CMD_DIS_DEC_V;
654 cfg_dec_cmd.dm_mode = 0x0;
655 cfg_dec_cmd.stream_id = audio->dec_id;
656 return audpp_send_queue1(&cfg_dec_cmd, sizeof(cfg_dec_cmd));
657}
658
659static void audpp_cmd_cfg_adec_params(struct audio *audio)
660{
661 struct audpp_cmd_cfg_adec_params_mp3 cmd;
662
663 memset(&cmd, 0, sizeof(cmd));
664 cmd.common.cmd_id = AUDPP_CMD_CFG_ADEC_PARAMS;
665 cmd.common.length = AUDPP_CMD_CFG_ADEC_PARAMS_MP3_LEN;
666 cmd.common.dec_id = audio->dec_id;
667 cmd.common.input_sampling_frequency = audio->out_sample_rate;
668
669 audpp_send_queue2(&cmd, sizeof(cmd));
670}
671
672static void audpp_cmd_cfg_routing_mode(struct audio *audio)
673{
674 struct audpp_cmd_routing_mode cmd;
675 MM_DBG("\n"); /* Macro prints the file name and function */
676 memset(&cmd, 0, sizeof(cmd));
677 cmd.cmd_id = AUDPP_CMD_ROUTING_MODE;
678 cmd.object_number = audio->dec_id;
679 if (audio->pcm_feedback)
680 cmd.routing_mode = ROUTING_MODE_FTRT;
681 else
682 cmd.routing_mode = ROUTING_MODE_RT;
683
684 audpp_send_queue1(&cmd, sizeof(cmd));
685}
686
687static int audplay_dsp_send_data_avail(struct audio *audio,
688 unsigned idx, unsigned len)
689{
690 struct audplay_cmd_bitstream_data_avail_nt2 cmd;
691
692 cmd.cmd_id = AUDPLAY_CMD_BITSTREAM_DATA_AVAIL_NT2;
693 if (audio->mfield)
694 cmd.decoder_id = AUDMP3_METAFIELD_MASK |
695 (audio->out[idx].mfield_sz >> 1);
696 else
697 cmd.decoder_id = audio->dec_id;
698 cmd.buf_ptr = audio->out[idx].addr;
699 cmd.buf_size = len/2;
700 cmd.partition_number = 0;
701 return audplay_send_queue0(audio, &cmd, sizeof(cmd));
702}
703/* Caller holds irq_lock */
704static void audmp3_async_buffer_refresh(struct audio *audio)
705{
706 struct audplay_cmd_buffer_refresh refresh_cmd;
707 struct audmp3_buffer_node *next_buf;
708
709 if (!audio->running ||
710 audio->drv_status & ADRV_STATUS_IBUF_GIVEN)
711 return;
712
713 if (!list_empty(&audio->in_queue)) {
714 next_buf = list_first_entry(&audio->in_queue,
715 struct audmp3_buffer_node, list);
716 if (!next_buf)
717 return;
718 MM_DBG("next buf %p phy %lx len %d\n", next_buf,
719 next_buf->paddr, next_buf->buf.buf_len);
720 refresh_cmd.cmd_id = AUDPLAY_CMD_BUFFER_REFRESH;
721 refresh_cmd.num_buffers = 1;
722 refresh_cmd.buf0_address = next_buf->paddr;
723 refresh_cmd.buf0_length = next_buf->buf.buf_len -
724 (next_buf->buf.buf_len % 576) +
725 (audio->mfield ? 24 : 0); /* Mp3 frame size */
726 refresh_cmd.buf_read_count = 0;
727 audio->drv_status |= ADRV_STATUS_IBUF_GIVEN;
728 (void) audplay_send_queue0(audio, &refresh_cmd,
729 sizeof(refresh_cmd));
730 }
731
732}
733
734static void audplay_buffer_refresh(struct audio *audio)
735{
736 struct audplay_cmd_buffer_refresh refresh_cmd;
737
738 refresh_cmd.cmd_id = AUDPLAY_CMD_BUFFER_REFRESH;
739 refresh_cmd.num_buffers = 1;
740 refresh_cmd.buf0_address = audio->in[audio->fill_next].addr;
741 refresh_cmd.buf0_length = audio->in[audio->fill_next].size -
742 (audio->in[audio->fill_next].size % 576) +
743 (audio->mfield ? 24 : 0); /* Mp3 frame size */
744 refresh_cmd.buf_read_count = 0;
745 MM_DBG("buf0_addr=%x buf0_len=%d\n", refresh_cmd.buf0_address,
746 refresh_cmd.buf0_length);
747 (void)audplay_send_queue0(audio, &refresh_cmd, sizeof(refresh_cmd));
748}
749
750static void audplay_error_threshold_config(struct audio *audio)
751{
752 union audplay_cmd_channel_info ch_cfg_cmd;
753
754 MM_DBG("\n"); /* Macro prints the file name and function */
755 ch_cfg_cmd.thr_update.cmd_id = AUDPLAY_CMD_CHANNEL_INFO;
756 ch_cfg_cmd.thr_update.threshold_update = AUDPLAY_ERROR_THRESHOLD_ENABLE;
757 ch_cfg_cmd.thr_update.threshold_value =
758 audio->bitstream_error_threshold_value;
759 (void)audplay_send_queue0(audio, &ch_cfg_cmd, sizeof(ch_cfg_cmd));
760}
761
762static void audplay_config_hostpcm(struct audio *audio)
763{
764 struct audplay_cmd_hpcm_buf_cfg cfg_cmd;
765
766 MM_DBG("\n"); /* Macro prints the file name and function */
767 cfg_cmd.cmd_id = AUDPLAY_CMD_HPCM_BUF_CFG;
768 cfg_cmd.max_buffers = 1;
769 cfg_cmd.byte_swap = 0;
770 cfg_cmd.hostpcm_config = (0x8000) | (0x4000);
771 cfg_cmd.feedback_frequency = 1;
772 cfg_cmd.partition_number = 0;
773 (void)audplay_send_queue0(audio, &cfg_cmd, sizeof(cfg_cmd));
774
775}
776
777static void audplay_outport_flush(struct audio *audio)
778{
779 struct audplay_cmd_outport_flush op_flush_cmd;
780
781 MM_DBG("\n"); /* Macro prints the file name and function */
782 op_flush_cmd.cmd_id = AUDPLAY_CMD_OUTPORT_FLUSH;
783 (void)audplay_send_queue0(audio, &op_flush_cmd, sizeof(op_flush_cmd));
784}
785
786static void audmp3_async_send_data(struct audio *audio, unsigned needed)
787{
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 audio->out_needed = 1;
796 if (audio->drv_status & ADRV_STATUS_OBUF_GIVEN) {
797 /* pop one node out of queue */
798 union msm_audio_event_payload payload;
799 struct audmp3_buffer_node *used_buf;
800
801 MM_DBG("consumed\n");
802 BUG_ON(list_empty(&audio->out_queue));
803 used_buf = list_first_entry(&audio->out_queue,
804 struct audmp3_buffer_node, list);
805 list_del(&used_buf->list);
806 payload.aio_buf = used_buf->buf;
807 audmp3_post_event(audio, AUDIO_EVENT_WRITE_DONE,
808 payload);
809 kfree(used_buf);
810 audio->drv_status &= ~ADRV_STATUS_OBUF_GIVEN;
811 }
812
813 }
814
815 if (audio->out_needed) {
816 struct audmp3_buffer_node *next_buf;
817 struct audplay_cmd_bitstream_data_avail_nt2 cmd;
818 if (!list_empty(&audio->out_queue)) {
819 next_buf = list_first_entry(&audio->out_queue,
820 struct audmp3_buffer_node, list);
821 MM_DBG("next_buf %p\n", next_buf);
822 if (next_buf) {
823 MM_DBG("next buf phy %lx len %d\n",
824 next_buf->paddr,
825 next_buf->buf.data_len);
826
827 cmd.cmd_id =
828 AUDPLAY_CMD_BITSTREAM_DATA_AVAIL_NT2;
829 if (audio->mfield)
830 cmd.decoder_id = AUDMP3_METAFIELD_MASK |
831 (next_buf->buf.mfield_sz >> 1);
832 else
833 cmd.decoder_id = audio->dec_id;
834 cmd.buf_ptr = (unsigned) next_buf->paddr;
835 cmd.buf_size = next_buf->buf.data_len >> 1;
836 cmd.partition_number = 0;
837 audplay_send_queue0(audio, &cmd, sizeof(cmd));
838 audio->out_needed = 0;
839 audio->drv_status |= ADRV_STATUS_OBUF_GIVEN;
840 }
841 }
842 }
843
844done:
845 spin_unlock_irqrestore(&audio->dsp_lock, flags);
846}
847
848static void audplay_send_data(struct audio *audio, unsigned needed)
849{
850 struct buffer *frame;
851 unsigned long flags;
852
853 spin_lock_irqsave(&audio->dsp_lock, flags);
854 if (!audio->running)
855 goto done;
856
857 if (needed && !audio->wflush) {
858 /* We were called from the callback because the DSP
859 * requested more data. Note that the DSP does want
860 * more data, and if a buffer was in-flight, mark it
861 * as available (since the DSP must now be done with
862 * it).
863 */
864 audio->out_needed = 1;
865 frame = audio->out + audio->out_tail;
866 if (frame->used == 0xffffffff) {
867 MM_DBG("frame %d free\n", audio->out_tail);
868 frame->used = 0;
869 audio->out_tail ^= 1;
870 wake_up(&audio->write_wait);
871 }
872 }
873
874 if (audio->out_needed) {
875 /* If the DSP currently wants data and we have a
876 * buffer available, we will send it and reset
877 * the needed flag. We'll mark the buffer as in-flight
878 * so that it won't be recycled until the next buffer
879 * is requested
880 */
881
882 frame = audio->out + audio->out_tail;
883 if (frame->used) {
884 BUG_ON(frame->used == 0xffffffff);
885 MM_DBG("frame %d busy\n", audio->out_tail);
886 audplay_dsp_send_data_avail(audio, audio->out_tail,
887 frame->used);
888 frame->used = 0xffffffff;
889 audio->out_needed = 0;
890 }
891 }
892done:
893 spin_unlock_irqrestore(&audio->dsp_lock, flags);
894}
895
896/* ------------------- device --------------------- */
897static void audmp3_async_flush(struct audio *audio)
898{
899 struct audmp3_buffer_node *buf_node;
900 struct list_head *ptr, *next;
901 union msm_audio_event_payload payload;
902
903 MM_DBG("\n"); /* Macro prints the file name and function */
904 list_for_each_safe(ptr, next, &audio->out_queue) {
905 buf_node = list_entry(ptr, struct audmp3_buffer_node, list);
906 list_del(&buf_node->list);
907 payload.aio_buf = buf_node->buf;
908 audmp3_post_event(audio, AUDIO_EVENT_WRITE_DONE,
909 payload);
910 kfree(buf_node);
911 }
912 audio->drv_status &= ~ADRV_STATUS_OBUF_GIVEN;
913 audio->out_needed = 0;
914 atomic_set(&audio->out_bytes, 0);
915}
916
917static void audio_flush(struct audio *audio)
918{
919 audio->out[0].used = 0;
920 audio->out[1].used = 0;
921 audio->out_head = 0;
922 audio->out_tail = 0;
923 audio->reserved = 0;
924 audio->out_needed = 0;
925 atomic_set(&audio->out_bytes, 0);
926}
927
928static void audmp3_async_flush_pcm_buf(struct audio *audio)
929{
930 struct audmp3_buffer_node *buf_node;
931 struct list_head *ptr, *next;
932 union msm_audio_event_payload payload;
933
934 MM_DBG("\n"); /* Macro prints the file name and function */
935 list_for_each_safe(ptr, next, &audio->in_queue) {
936 buf_node = list_entry(ptr, struct audmp3_buffer_node, list);
937 list_del(&buf_node->list);
938 payload.aio_buf = buf_node->buf;
939 payload.aio_buf.data_len = 0;
940 audmp3_post_event(audio, AUDIO_EVENT_READ_DONE,
941 payload);
942 kfree(buf_node);
943 }
944 audio->drv_status &= ~ADRV_STATUS_IBUF_GIVEN;
945
946}
947
948static void audio_flush_pcm_buf(struct audio *audio)
949{
950 uint8_t index;
951
952 for (index = 0; index < PCM_BUF_MAX_COUNT; index++)
953 audio->in[index].used = 0;
954
955 audio->buf_refresh = 0;
956 audio->read_next = 0;
957 audio->fill_next = 0;
958}
959
960static void audio_ioport_reset(struct audio *audio)
961{
962 if (audio->drv_status & ADRV_STATUS_AIO_INTF) {
963 /* If fsync is in progress, make sure
964 * return value of fsync indicates
965 * abort due to flush
966 */
967 if (audio->drv_status & ADRV_STATUS_FSYNC) {
968 MM_DBG("fsync in progress\n");
969 wake_up(&audio->write_wait);
970 mutex_lock(&audio->write_lock);
971 audio->drv_ops.out_flush(audio);
972 mutex_unlock(&audio->write_lock);
973 } else
974 audio->drv_ops.out_flush(audio);
975 audio->drv_ops.in_flush(audio);
976 } else {
977 /* Make sure read/write thread are free from
978 * sleep and knowing that system is not able
979 * to process io request at the moment
980 */
981 wake_up(&audio->write_wait);
982 mutex_lock(&audio->write_lock);
983 audio->drv_ops.out_flush(audio);
984 mutex_unlock(&audio->write_lock);
985 wake_up(&audio->read_wait);
986 mutex_lock(&audio->read_lock);
987 audio->drv_ops.in_flush(audio);
988 mutex_unlock(&audio->read_lock);
989 }
990 audio->avsync_flag = 1;
991 wake_up(&audio->avsync_wait);
992}
993
994static int audmp3_events_pending(struct audio *audio)
995{
996 unsigned long flags;
997 int empty;
998
999 spin_lock_irqsave(&audio->event_queue_lock, flags);
1000 empty = !list_empty(&audio->event_queue);
1001 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
1002 return empty || audio->event_abort;
1003}
1004
1005static void audmp3_reset_event_queue(struct audio *audio)
1006{
1007 unsigned long flags;
1008 struct audmp3_event *drv_evt;
1009 struct list_head *ptr, *next;
1010
1011 spin_lock_irqsave(&audio->event_queue_lock, flags);
1012 list_for_each_safe(ptr, next, &audio->event_queue) {
1013 drv_evt = list_first_entry(&audio->event_queue,
1014 struct audmp3_event, list);
1015 list_del(&drv_evt->list);
1016 kfree(drv_evt);
1017 }
1018 list_for_each_safe(ptr, next, &audio->free_event_queue) {
1019 drv_evt = list_first_entry(&audio->free_event_queue,
1020 struct audmp3_event, list);
1021 list_del(&drv_evt->list);
1022 kfree(drv_evt);
1023 }
1024 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
1025
1026 return;
1027}
1028
1029static long audmp3_process_event_req(struct audio *audio, void __user *arg)
1030{
1031 long rc;
1032 struct msm_audio_event usr_evt;
1033 struct audmp3_event *drv_evt = NULL;
1034 int timeout;
1035 unsigned long flags;
1036
1037 if (copy_from_user(&usr_evt, arg, sizeof(struct msm_audio_event)))
1038 return -EFAULT;
1039
1040 timeout = (int) usr_evt.timeout_ms;
1041
1042 if (timeout > 0) {
1043 rc = wait_event_interruptible_timeout(
1044 audio->event_wait, audmp3_events_pending(audio),
1045 msecs_to_jiffies(timeout));
1046 if (rc == 0)
1047 return -ETIMEDOUT;
1048 } else {
1049 rc = wait_event_interruptible(
1050 audio->event_wait, audmp3_events_pending(audio));
1051 }
1052
1053 if (rc < 0)
1054 return rc;
1055
1056 if (audio->event_abort) {
1057 audio->event_abort = 0;
1058 return -ENODEV;
1059 }
1060
1061 rc = 0;
1062
1063 spin_lock_irqsave(&audio->event_queue_lock, flags);
1064 if (!list_empty(&audio->event_queue)) {
1065 drv_evt = list_first_entry(&audio->event_queue,
1066 struct audmp3_event, list);
1067 list_del(&drv_evt->list);
1068 }
1069 if (drv_evt) {
1070 usr_evt.event_type = drv_evt->event_type;
1071 usr_evt.event_payload = drv_evt->payload;
1072 list_add_tail(&drv_evt->list, &audio->free_event_queue);
1073 } else
1074 rc = -1;
1075 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
1076
1077 if (drv_evt->event_type == AUDIO_EVENT_WRITE_DONE ||
1078 drv_evt->event_type == AUDIO_EVENT_READ_DONE) {
1079 mutex_lock(&audio->lock);
1080 audmp3_pmem_fixup(audio, drv_evt->payload.aio_buf.buf_addr,
1081 drv_evt->payload.aio_buf.buf_len, 0);
1082 mutex_unlock(&audio->lock);
1083 }
1084 if (!rc && copy_to_user(arg, &usr_evt, sizeof(usr_evt)))
1085 rc = -EFAULT;
1086
1087 return rc;
1088}
1089
1090static int audmp3_pmem_check(struct audio *audio,
1091 void *vaddr, unsigned long len)
1092{
1093 struct audmp3_pmem_region *region_elt;
1094 struct audmp3_pmem_region t = { .vaddr = vaddr, .len = len };
1095
1096 list_for_each_entry(region_elt, &audio->pmem_region_queue, list) {
1097 if (CONTAINS(region_elt, &t) || CONTAINS(&t, region_elt) ||
1098 OVERLAPS(region_elt, &t)) {
1099 MM_ERR("region (vaddr %p len %ld)"
1100 " clashes with registered region"
1101 " (vaddr %p paddr %p len %ld)\n",
1102 vaddr, len,
1103 region_elt->vaddr,
1104 (void *)region_elt->paddr,
1105 region_elt->len);
1106 return -EINVAL;
1107 }
1108 }
1109
1110 return 0;
1111}
1112
1113static int audmp3_pmem_add(struct audio *audio,
1114 struct msm_audio_pmem_info *info)
1115{
1116 unsigned long paddr, kvaddr, len;
1117 struct file *file;
1118 struct audmp3_pmem_region *region;
1119 int rc = -EINVAL;
1120
1121 MM_DBG("\n"); /* Macro prints the file name and function */
1122 region = kmalloc(sizeof(*region), GFP_KERNEL);
1123
1124 if (!region) {
1125 rc = -ENOMEM;
1126 goto end;
1127 }
1128
1129 if (get_pmem_file(info->fd, &paddr, &kvaddr, &len, &file)) {
1130 kfree(region);
1131 goto end;
1132 }
1133
1134 rc = audmp3_pmem_check(audio, info->vaddr, len);
1135 if (rc < 0) {
1136 put_pmem_file(file);
1137 kfree(region);
1138 goto end;
1139 }
1140
1141 region->vaddr = info->vaddr;
1142 region->fd = info->fd;
1143 region->paddr = paddr;
1144 region->kvaddr = kvaddr;
1145 region->len = len;
1146 region->file = file;
1147 region->ref_cnt = 0;
1148 MM_DBG("add region paddr %lx vaddr %p, len %lu\n", region->paddr,
1149 region->vaddr, region->len);
1150 list_add_tail(®ion->list, &audio->pmem_region_queue);
1151end:
1152 return rc;
1153}
1154
1155static int audmp3_pmem_remove(struct audio *audio,
1156 struct msm_audio_pmem_info *info)
1157{
1158 struct audmp3_pmem_region *region;
1159 struct list_head *ptr, *next;
1160 int rc = -EINVAL;
1161
1162 MM_DBG("info fd %d vaddr %p\n", info->fd, info->vaddr);
1163
1164 list_for_each_safe(ptr, next, &audio->pmem_region_queue) {
1165 region = list_entry(ptr, struct audmp3_pmem_region, list);
1166
1167 if ((region->fd == info->fd) &&
1168 (region->vaddr == info->vaddr)) {
1169 if (region->ref_cnt) {
1170 MM_DBG("region %p in use ref_cnt %d\n",
1171 region, region->ref_cnt);
1172 break;
1173 }
1174 MM_DBG("remove region fd %d vaddr %p \n",
1175 info->fd, info->vaddr);
1176 list_del(®ion->list);
1177 put_pmem_file(region->file);
1178 kfree(region);
1179 rc = 0;
1180 break;
1181 }
1182 }
1183
1184 return rc;
1185}
1186
1187static int audmp3_pmem_lookup_vaddr(struct audio *audio, void *addr,
1188 unsigned long len, struct audmp3_pmem_region **region)
1189{
1190 struct audmp3_pmem_region *region_elt;
1191
1192 int match_count = 0;
1193
1194 *region = NULL;
1195
1196 /* returns physical address or zero */
1197 list_for_each_entry(region_elt, &audio->pmem_region_queue,
1198 list) {
1199 if (addr >= region_elt->vaddr &&
1200 addr < region_elt->vaddr + region_elt->len &&
1201 addr + len <= region_elt->vaddr + region_elt->len) {
1202 /* offset since we could pass vaddr inside a registerd
1203 * pmem buffer
1204 */
1205
1206 match_count++;
1207 if (!*region)
1208 *region = region_elt;
1209 }
1210 }
1211
1212 if (match_count > 1) {
1213 MM_ERR("multiple hits for vaddr %p, len %ld\n", addr, len);
1214 list_for_each_entry(region_elt,
1215 &audio->pmem_region_queue, list) {
1216 if (addr >= region_elt->vaddr &&
1217 addr < region_elt->vaddr + region_elt->len &&
1218 addr + len <= region_elt->vaddr + region_elt->len)
1219 MM_ERR("\t%p, %ld --> %p\n", region_elt->vaddr,
1220 region_elt->len,
1221 (void *)region_elt->paddr);
1222 }
1223 }
1224
1225 return *region ? 0 : -1;
1226}
1227
1228unsigned long audmp3_pmem_fixup(struct audio *audio, void *addr,
1229 unsigned long len, int ref_up)
1230{
1231 struct audmp3_pmem_region *region;
1232 unsigned long paddr;
1233 int ret;
1234
1235 ret = audmp3_pmem_lookup_vaddr(audio, addr, len, ®ion);
1236 if (ret) {
1237 MM_ERR("lookup (%p, %ld) failed\n", addr, len);
1238 return 0;
1239 }
1240 if (ref_up)
1241 region->ref_cnt++;
1242 else
1243 region->ref_cnt--;
1244 MM_DBG("found region %p ref_cnt %d\n", region, region->ref_cnt);
1245 paddr = region->paddr + (addr - region->vaddr);
1246 return paddr;
1247}
1248
1249/* audio -> lock must be held at this point */
1250static int audmp3_aio_buf_add(struct audio *audio, unsigned dir,
1251 void __user *arg)
1252{
1253 unsigned long flags;
1254 struct audmp3_buffer_node *buf_node;
1255
1256 buf_node = kmalloc(sizeof(*buf_node), GFP_KERNEL);
1257
1258 if (!buf_node)
1259 return -ENOMEM;
1260
1261 if (copy_from_user(&buf_node->buf, arg, sizeof(buf_node->buf))) {
1262 kfree(buf_node);
1263 return -EFAULT;
1264 }
1265
1266 MM_DBG("node %p dir %x buf_addr %p buf_len %d data_len \
1267 %d\n", buf_node, dir,
1268 buf_node->buf.buf_addr, buf_node->buf.buf_len,
1269 buf_node->buf.data_len);
1270
1271 buf_node->paddr = audmp3_pmem_fixup(
1272 audio, buf_node->buf.buf_addr,
1273 buf_node->buf.buf_len, 1);
1274
1275 if (dir) {
1276 /* write */
1277 if (!buf_node->paddr ||
1278 (buf_node->paddr & 0x1) ||
1279 (buf_node->buf.data_len & 0x1) ||
1280 (!audio->pcm_feedback &&
1281 !buf_node->buf.data_len)) {
1282 kfree(buf_node);
1283 return -EINVAL;
1284 }
1285 spin_lock_irqsave(&audio->dsp_lock, flags);
1286 list_add_tail(&buf_node->list, &audio->out_queue);
1287 spin_unlock_irqrestore(&audio->dsp_lock, flags);
1288 audio->drv_ops.send_data(audio, 0);
1289 } else {
1290 /* read */
1291 if (!buf_node->paddr ||
1292 (buf_node->paddr & 0x1) ||
1293 (buf_node->buf.buf_len < PCM_BUFSZ_MIN)) {
1294 kfree(buf_node);
1295 return -EINVAL;
1296 }
1297 spin_lock_irqsave(&audio->dsp_lock, flags);
1298 list_add_tail(&buf_node->list, &audio->in_queue);
1299 audio->drv_ops.buffer_refresh(audio);
1300 spin_unlock_irqrestore(&audio->dsp_lock, flags);
1301 }
1302
1303 MM_DBG("Add buf_node %p paddr %lx\n", buf_node, buf_node->paddr);
1304
1305 return 0;
1306}
1307
1308static int audio_enable_eq(struct audio *audio, int enable)
1309{
1310 if (audio->eq_enable == enable && !audio->eq_needs_commit)
1311 return 0;
1312
1313 audio->eq_enable = enable;
1314
1315 if (audio->running) {
1316 audpp_dsp_set_eq(audio->dec_id, enable, &audio->eq, POPP);
1317 audio->eq_needs_commit = 0;
1318 }
1319 return 0;
1320}
1321
1322static int audio_get_avsync_data(struct audio *audio,
1323 struct msm_audio_stats *stats)
1324{
1325 int rc = -EINVAL;
1326 unsigned long flags;
1327
1328 local_irq_save(flags);
1329 if (audio->dec_id == audio->avsync[0] && audio->avsync_flag) {
1330 /* av_sync sample count */
1331 stats->sample_count = (audio->avsync[2] << 16) |
1332 (audio->avsync[3]);
1333
1334 /* av_sync byte_count */
1335 stats->byte_count = (audio->avsync[5] << 16) |
1336 (audio->avsync[6]);
1337
1338 audio->avsync_flag = 0;
1339 rc = 0;
1340 }
1341 local_irq_restore(flags);
1342 return rc;
1343
1344}
1345
1346static long audio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1347{
1348 struct audio *audio = file->private_data;
1349 int rc = -EINVAL;
1350 unsigned long flags = 0;
1351 uint16_t enable_mask;
1352 int enable;
1353 int prev_state;
1354
1355 MM_DBG("cmd = %d\n", cmd);
1356
1357 if (cmd == AUDIO_GET_STATS) {
1358 struct msm_audio_stats stats;
1359
1360 audio->avsync_flag = 0;
1361 memset(&stats, 0, sizeof(stats));
1362 if (audpp_query_avsync(audio->dec_id) < 0)
1363 return rc;
1364
1365 rc = wait_event_interruptible_timeout(audio->avsync_wait,
1366 (audio->avsync_flag == 1),
1367 msecs_to_jiffies(AUDPP_AVSYNC_EVENT_TIMEOUT));
1368
1369 if (rc < 0)
1370 return rc;
1371 else if ((rc > 0) || ((rc == 0) && (audio->avsync_flag == 1))) {
1372 if (audio_get_avsync_data(audio, &stats) < 0)
1373 return rc;
1374
1375 if (copy_to_user((void *)arg, &stats, sizeof(stats)))
1376 return -EFAULT;
1377 return 0;
1378 } else
1379 return -EAGAIN;
1380 }
1381
1382 switch (cmd) {
1383 case AUDIO_ENABLE_AUDPP:
1384 if (copy_from_user(&enable_mask, (void *) arg,
1385 sizeof(enable_mask))) {
1386 rc = -EFAULT;
1387 break;
1388 }
1389
1390 spin_lock_irqsave(&audio->dsp_lock, flags);
1391 enable = (enable_mask & EQ_ENABLE) ? 1 : 0;
1392 audio_enable_eq(audio, enable);
1393 spin_unlock_irqrestore(&audio->dsp_lock, flags);
1394 rc = 0;
1395 break;
1396 case AUDIO_SET_VOLUME:
1397 spin_lock_irqsave(&audio->dsp_lock, flags);
1398 audio->vol_pan.volume = arg;
1399 if (audio->running)
1400 audpp_dsp_set_vol_pan(audio->dec_id, &audio->vol_pan,
1401 POPP);
1402 spin_unlock_irqrestore(&audio->dsp_lock, flags);
1403 rc = 0;
1404 break;
1405
1406 case AUDIO_SET_PAN:
1407 spin_lock_irqsave(&audio->dsp_lock, flags);
1408 audio->vol_pan.pan = arg;
1409 if (audio->running)
1410 audpp_dsp_set_vol_pan(audio->dec_id, &audio->vol_pan,
1411 POPP);
1412 spin_unlock_irqrestore(&audio->dsp_lock, flags);
1413 rc = 0;
1414 break;
1415
1416 case AUDIO_SET_EQ:
1417 prev_state = audio->eq_enable;
1418 audio->eq_enable = 0;
1419 if (copy_from_user(&audio->eq.num_bands, (void *) arg,
1420 sizeof(audio->eq) -
1421 (AUDPP_CMD_CFG_OBJECT_PARAMS_COMMON_LEN + 2))) {
1422 rc = -EFAULT;
1423 break;
1424 }
1425 audio->eq_enable = prev_state;
1426 audio->eq_needs_commit = 1;
1427 rc = 0;
1428 break;
1429 }
1430
1431 if (-EINVAL != rc)
1432 return rc;
1433
1434 if (cmd == AUDIO_GET_EVENT) {
1435 MM_DBG(" AUDIO_GET_EVENT\n");
1436 if (mutex_trylock(&audio->get_event_lock)) {
1437 rc = audmp3_process_event_req(audio,
1438 (void __user *) arg);
1439 mutex_unlock(&audio->get_event_lock);
1440 } else
1441 rc = -EBUSY;
1442 return rc;
1443 }
1444
1445 if (cmd == AUDIO_ABORT_GET_EVENT) {
1446 audio->event_abort = 1;
1447 wake_up(&audio->event_wait);
1448 return 0;
1449 }
1450
1451 mutex_lock(&audio->lock);
1452 switch (cmd) {
1453 case AUDIO_START:
1454 MM_DBG("AUDIO_START\n");
1455 rc = audio_enable(audio);
1456 if (!rc) {
1457 rc = wait_event_interruptible_timeout(audio->wait,
1458 audio->dec_state != MSM_AUD_DECODER_STATE_NONE,
1459 msecs_to_jiffies(MSM_AUD_DECODER_WAIT_MS));
1460 MM_INFO("dec_state %d rc = %d\n", audio->dec_state, rc);
1461
1462 if (audio->dec_state != MSM_AUD_DECODER_STATE_SUCCESS)
1463 rc = -ENODEV;
1464 else
1465 rc = 0;
1466 }
1467 break;
1468 case AUDIO_STOP:
1469 MM_DBG("AUDIO_STOP\n");
1470 rc = audio_disable(audio);
1471 audio->stopped = 1;
1472 audio_ioport_reset(audio);
1473 audio->stopped = 0;
1474 break;
1475 case AUDIO_FLUSH:
1476 MM_DBG("AUDIO_FLUSH\n");
1477 audio->rflush = 1;
1478 audio->wflush = 1;
1479 audio_ioport_reset(audio);
1480 if (audio->running) {
1481 audpp_flush(audio->dec_id);
1482 rc = wait_event_interruptible(audio->write_wait,
1483 !audio->wflush);
1484 if (rc < 0) {
1485 MM_ERR("AUDIO_FLUSH interrupted\n");
1486 rc = -EINTR;
1487 }
1488 } else {
1489 audio->rflush = 0;
1490 audio->wflush = 0;
1491 }
1492 break;
1493 case AUDIO_OUTPORT_FLUSH:
1494 MM_DBG("AUDIO_OUTPORT_FLUSH\n");
1495 audio->rflush = 1;
1496 if (audio->drv_status & ADRV_STATUS_AIO_INTF) {
1497 audio->drv_ops.in_flush(audio);
1498 } else {
1499 wake_up(&audio->read_wait);
1500 mutex_lock(&audio->read_lock);
1501 audio->drv_ops.in_flush(audio);
1502 mutex_unlock(&audio->read_lock);
1503 }
1504 audplay_outport_flush(audio);
1505 rc = wait_event_interruptible(audio->read_wait,
1506 !audio->rflush);
1507 if (rc < 0) {
1508 MM_ERR("AUDPLAY_OUTPORT_FLUSH interrupted\n");
1509 rc = -EINTR;
1510 }
1511 break;
1512 case AUDIO_SET_CONFIG: {
1513 struct msm_audio_config config;
1514 if (copy_from_user(&config, (void *) arg, sizeof(config))) {
1515 rc = -EFAULT;
1516 break;
1517 }
1518 if (config.channel_count == 1) {
1519 config.channel_count = AUDPP_CMD_PCM_INTF_MONO_V;
1520 } else if (config.channel_count == 2) {
1521 config.channel_count = AUDPP_CMD_PCM_INTF_STEREO_V;
1522 } else {
1523 rc = -EINVAL;
1524 break;
1525 }
1526 audio->mfield = config.meta_field;
1527 audio->out_sample_rate = config.sample_rate;
1528 audio->out_channel_mode = config.channel_count;
1529 rc = 0;
1530 break;
1531 }
1532 case AUDIO_GET_CONFIG: {
1533 struct msm_audio_config config;
1534 config.buffer_size = (audio->out_dma_sz >> 1);
1535 config.buffer_count = 2;
1536 config.sample_rate = audio->out_sample_rate;
1537 if (audio->out_channel_mode == AUDPP_CMD_PCM_INTF_MONO_V)
1538 config.channel_count = 1;
1539 else
1540 config.channel_count = 2;
1541 config.meta_field = 0;
1542 config.unused[0] = 0;
1543 config.unused[1] = 0;
1544 config.unused[2] = 0;
1545 if (copy_to_user((void *) arg, &config, sizeof(config)))
1546 rc = -EFAULT;
1547 else
1548 rc = 0;
1549 break;
1550 }
1551 case AUDIO_GET_PCM_CONFIG:{
1552 struct msm_audio_pcm_config config;
1553 config.pcm_feedback = audio->pcm_feedback;
1554 config.buffer_count = PCM_BUF_MAX_COUNT;
1555 config.buffer_size = PCM_BUFSZ_MIN;
1556 if (copy_to_user((void *)arg, &config,
1557 sizeof(config)))
1558 rc = -EFAULT;
1559 else
1560 rc = 0;
1561 break;
1562 }
1563 case AUDIO_SET_PCM_CONFIG:{
1564 struct msm_audio_pcm_config config;
1565 if (copy_from_user
1566 (&config, (void *)arg, sizeof(config))) {
1567 rc = -EFAULT;
1568 break;
1569 }
1570
1571 if (config.pcm_feedback != audio->pcm_feedback) {
1572 MM_ERR("Not sufficient permission to"
1573 "change the playback mode\n");
1574 rc = -EACCES;
1575 break;
1576 }
1577 if (audio->drv_status & ADRV_STATUS_AIO_INTF) {
1578 rc = 0;
1579 break;
1580 }
1581
1582 if ((config.buffer_count > PCM_BUF_MAX_COUNT) ||
1583 (config.buffer_count == 1))
1584 config.buffer_count = PCM_BUF_MAX_COUNT;
1585
1586 if (config.buffer_size < PCM_BUFSZ_MIN)
1587 config.buffer_size = PCM_BUFSZ_MIN;
1588
1589 /* Check if pcm feedback is required */
1590 if ((config.pcm_feedback) && (!audio->read_data)) {
1591 MM_DBG("allocate PCM buffer %d\n",
1592 config.buffer_count *
1593 config.buffer_size);
1594 audio->read_phys = pmem_kalloc(
1595 config.buffer_size *
1596 config.buffer_count,
1597 PMEM_MEMTYPE_EBI1|
1598 PMEM_ALIGNMENT_4K);
1599 if (IS_ERR((void *)audio->read_phys)) {
1600 rc = -ENOMEM;
1601 break;
1602 }
1603 audio->read_data = ioremap(audio->read_phys,
1604 config.buffer_size *
1605 config.buffer_count);
1606 if (!audio->read_data) {
1607 MM_ERR("malloc read buf failed\n");
1608 rc = -ENOMEM;
1609 pmem_kfree(audio->read_phys);
1610 } else {
1611 uint8_t index;
1612 uint32_t offset = 0;
1613 audio->buf_refresh = 0;
1614 audio->pcm_buf_count =
1615 config.buffer_count;
1616 audio->read_next = 0;
1617 audio->fill_next = 0;
1618
1619 for (index = 0;
1620 index < config.buffer_count;
1621 index++) {
1622 audio->in[index].data =
1623 audio->read_data + offset;
1624 audio->in[index].addr =
1625 audio->read_phys + offset;
1626 audio->in[index].size =
1627 config.buffer_size;
1628 audio->in[index].used = 0;
1629 offset += config.buffer_size;
1630 }
1631 rc = 0;
1632 MM_DBG("read buf: phy addr \
1633 0x%08x kernel addr 0x%08x\n",
1634 audio->read_phys,
1635 (int)audio->read_data);
1636 }
1637 } else {
1638 rc = 0;
1639 }
1640 break;
1641 }
1642 case AUDIO_PAUSE:
1643 MM_DBG("AUDIO_PAUSE %ld\n", arg);
1644 rc = audpp_pause(audio->dec_id, (int) arg);
1645 break;
1646
1647 case AUDIO_GET_STREAM_INFO:{
1648 if (audio->stream_info.sample_rate == 0) {
1649 /* haven't received DSP stream event,
1650 the stream info is not updated */
1651 rc = -EPERM;
1652 break;
1653 }
1654 if (copy_to_user((void *)arg, &audio->stream_info,
1655 sizeof(struct msm_audio_bitstream_info)))
1656 rc = -EFAULT;
1657 else
1658 rc = 0;
1659 break;
1660 }
1661 case AUDIO_GET_BITSTREAM_ERROR_INFO:{
1662 if ((audio->bitstream_error_info.err_msg_indicator &
1663 AUDPLAY_STREAM_INFO_MSG_MASK) ==
1664 AUDPLAY_STREAM_INFO_MSG_MASK) {
1665 /* haven't received bitstream error info event,
1666 the bitstream error info is not updated */
1667 rc = -EPERM;
1668 break;
1669 }
1670 if (copy_to_user((void *)arg, &audio->bitstream_error_info,
1671 sizeof(struct msm_audio_bitstream_error_info)))
1672 rc = -EFAULT;
1673 else
1674 rc = 0;
1675 break;
1676 }
1677
1678 case AUDIO_REGISTER_PMEM: {
1679 struct msm_audio_pmem_info info;
1680 MM_DBG("AUDIO_REGISTER_PMEM\n");
1681 if (copy_from_user(&info, (void *) arg, sizeof(info)))
1682 rc = -EFAULT;
1683 else
1684 rc = audmp3_pmem_add(audio, &info);
1685 break;
1686 }
1687
1688 case AUDIO_DEREGISTER_PMEM: {
1689 struct msm_audio_pmem_info info;
1690 MM_DBG("AUDIO_DEREGISTER_PMEM\n");
1691 if (copy_from_user(&info, (void *) arg, sizeof(info)))
1692 rc = -EFAULT;
1693 else
1694 rc = audmp3_pmem_remove(audio, &info);
1695 break;
1696 }
1697 case AUDIO_ASYNC_WRITE:
1698 if (audio->drv_status & ADRV_STATUS_FSYNC)
1699 rc = -EBUSY;
1700 else
1701 rc = audmp3_aio_buf_add(audio, 1, (void __user *) arg);
1702 break;
1703
1704 case AUDIO_ASYNC_READ:
1705 if (audio->pcm_feedback)
1706 rc = audmp3_aio_buf_add(audio, 0, (void __user *) arg);
1707 else
1708 rc = -EPERM;
1709 break;
1710 case AUDIO_GET_SESSION_ID:
1711 if (copy_to_user((void *) arg, &audio->dec_id,
1712 sizeof(unsigned short)))
1713 rc = -EFAULT;
1714 else
1715 rc = 0;
1716 break;
1717 case AUDIO_SET_ERR_THRESHOLD_VALUE:
1718 if (copy_from_user(&audio->bitstream_error_threshold_value,
1719 (void *)arg, sizeof(uint32_t)))
1720 rc = -EFAULT;
1721 else
1722 rc = 0;
1723 break;
1724 default:
1725 rc = -EINVAL;
1726 }
1727 mutex_unlock(&audio->lock);
1728 return rc;
1729}
1730
1731/* Only useful in tunnel-mode */
1732int audmp3_async_fsync(struct audio *audio)
1733{
1734 int rc = 0;
1735
1736 MM_DBG("\n"); /* Macro prints the file name and function */
1737
1738 /* Blocking client sends more data */
1739 mutex_lock(&audio->lock);
1740 audio->drv_status |= ADRV_STATUS_FSYNC;
1741 mutex_unlock(&audio->lock);
1742
1743 mutex_lock(&audio->write_lock);
1744 /* pcm dmamiss message is sent continously
1745 * when decoder is starved so no race
1746 * condition concern
1747 */
1748 audio->teos = 0;
1749
1750 rc = wait_event_interruptible(audio->write_wait,
1751 (audio->teos && audio->out_needed &&
1752 list_empty(&audio->out_queue))
1753 || audio->wflush || audio->stopped);
1754
1755 if (audio->stopped || audio->wflush)
1756 rc = -EBUSY;
1757
1758 mutex_unlock(&audio->write_lock);
1759 mutex_lock(&audio->lock);
1760 audio->drv_status &= ~ADRV_STATUS_FSYNC;
1761 mutex_unlock(&audio->lock);
1762
1763 return rc;
1764}
1765
1766int audmp3_sync_fsync(struct audio *audio)
1767{
1768 struct buffer *frame;
1769 int rc = 0;
1770
1771 MM_DBG("\n"); /* Macro prints the file name and function */
1772
1773 mutex_lock(&audio->write_lock);
1774
1775 rc = wait_event_interruptible(audio->write_wait,
1776 (!audio->out[0].used &&
1777 !audio->out[1].used &&
1778 audio->out_needed) || audio->wflush);
1779
1780 if (rc < 0)
1781 goto done;
1782 else if (audio->wflush) {
1783 rc = -EBUSY;
1784 goto done;
1785 }
1786
1787 if (audio->reserved) {
1788 MM_DBG("send reserved byte\n");
1789 frame = audio->out + audio->out_tail;
1790 ((char *) frame->data)[0] = audio->rsv_byte;
1791 ((char *) frame->data)[1] = 0;
1792 frame->used = 2;
1793 audio->drv_ops.send_data(audio, 0);
1794
1795 rc = wait_event_interruptible(audio->write_wait,
1796 (!audio->out[0].used &&
1797 !audio->out[1].used &&
1798 audio->out_needed) || audio->wflush);
1799
1800 if (rc < 0)
1801 goto done;
1802 else if (audio->wflush) {
1803 rc = -EBUSY;
1804 goto done;
1805 }
1806 }
1807
1808 /* pcm dmamiss message is sent continously
1809 * when decoder is starved so no race
1810 * condition concern
1811 */
1812 audio->teos = 0;
1813
1814 rc = wait_event_interruptible(audio->write_wait,
1815 audio->teos || audio->wflush);
1816
1817 if (audio->wflush)
1818 rc = -EBUSY;
1819
1820done:
1821 mutex_unlock(&audio->write_lock);
1822 return rc;
1823}
1824
1825int audmp3_fsync(struct file *file, int datasync)
1826{
1827 struct audio *audio = file->private_data;
1828
1829 if (!audio->running || audio->pcm_feedback)
1830 return -EINVAL;
1831
1832 return audio->drv_ops.fsync(audio);
1833}
1834
1835static ssize_t audio_read(struct file *file, char __user *buf, size_t count,
1836 loff_t *pos)
1837{
1838 struct audio *audio = file->private_data;
1839 const char __user *start = buf;
1840 int rc = 0;
1841
1842 if (audio->drv_status & ADRV_STATUS_AIO_INTF)
1843 return -EPERM;
1844 else if (!audio->pcm_feedback)
1845 return 0; /* PCM feedback disabled. Nothing to read */
1846
1847 mutex_lock(&audio->read_lock);
1848 MM_DBG("%d \n", count);
1849 while (count > 0) {
1850 rc = wait_event_interruptible_timeout(
1851 audio->read_wait,
1852 (audio->in[audio->read_next].
1853 used > 0) || (audio->stopped)
1854 || (audio->rflush),
1855 msecs_to_jiffies(MSM_AUD_BUFFER_UPDATE_WAIT_MS));
1856
1857 if (rc == 0) {
1858 rc = -ETIMEDOUT;
1859 break;
1860 } else if (rc < 0)
1861 break;
1862
1863 if (audio->stopped || audio->rflush) {
1864 rc = -EBUSY;
1865 break;
1866 }
1867
1868 if (count < audio->in[audio->read_next].used) {
1869 /* Read must happen in frame boundary. Since
1870 * driver does not know frame size, read count
1871 * must be greater or equal
1872 * to size of PCM samples
1873 */
1874 MM_DBG("no partial frame done reading\n");
1875 break;
1876 } else {
1877 MM_DBG("read from in[%d]\n", audio->read_next);
1878
1879 if (copy_to_user
1880 (buf, audio->in[audio->read_next].data,
1881 audio->in[audio->read_next].used)) {
1882 MM_ERR("invalid addr %x \n", (unsigned int)buf);
1883 rc = -EFAULT;
1884 break;
1885 }
1886 count -= audio->in[audio->read_next].used;
1887 buf += audio->in[audio->read_next].used;
1888 audio->in[audio->read_next].used = 0;
1889 if ((++audio->read_next) == audio->pcm_buf_count)
1890 audio->read_next = 0;
1891 break; /* Force to exit while loop
1892 * to prevent output thread
1893 * sleep too long if data is
1894 * not ready at this moment.
1895 */
1896 }
1897 }
1898
1899 /* don't feed output buffer to …
Large files files are truncated, but you can click here to view the full file