blob: bda2e4db9424424caafc5bb43bc1ee03343cf7c6 [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* mp3 audio output device
2 *
3 * Copyright (C) 2008 Google, Inc.
4 * Copyright (C) 2008 HTC Corporation
Duy Truong790f06d2013-02-13 16:38:12 -08005 * Copyright (c) 2009-2012, The Linux Foundation. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07006 *
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
Santosh Mardifdc227a2011-07-11 17:20:34 +053018#include <asm/atomic.h>
19#include <asm/ioctls.h>
20
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070021#include <linux/module.h>
22#include <linux/fs.h>
23#include <linux/miscdevice.h>
24#include <linux/uaccess.h>
25#include <linux/kthread.h>
26#include <linux/wait.h>
27#include <linux/dma-mapping.h>
28#include <linux/debugfs.h>
29#include <linux/delay.h>
30#include <linux/earlysuspend.h>
31#include <linux/list.h>
Santosh Mardifdc227a2011-07-11 17:20:34 +053032#include <linux/slab.h>
33#include <linux/memory_alloc.h>
34#include <linux/msm_audio.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070035#include <mach/msm_adsp.h>
36
Santosh Mardifdc227a2011-07-11 17:20:34 +053037#include <mach/iommu.h>
38#include <mach/iommu_domains.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070039#include <mach/qdsp5v2/audio_dev_ctl.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070040#include <mach/qdsp5v2/qdsp5audppmsg.h>
41#include <mach/qdsp5v2/qdsp5audplaycmdi.h>
42#include <mach/qdsp5v2/qdsp5audplaymsg.h>
43#include <mach/qdsp5v2/audio_dev_ctl.h>
44#include <mach/qdsp5v2/audpp.h>
45#include <mach/debug_mm.h>
Santosh Mardifdc227a2011-07-11 17:20:34 +053046#include <mach/msm_memtypes.h>
47
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070048#define ADRV_STATUS_AIO_INTF 0x00000001
49#define ADRV_STATUS_OBUF_GIVEN 0x00000002
50#define ADRV_STATUS_IBUF_GIVEN 0x00000004
51#define ADRV_STATUS_FSYNC 0x00000008
52
53/* Size must be power of 2 */
54#define BUFSZ_MAX 32768
55#define BUFSZ_MIN 4096
56#define DMASZ_MAX (BUFSZ_MAX * 2)
57#define DMASZ_MIN (BUFSZ_MIN * 2)
58
59#define AUDPLAY_INVALID_READ_PTR_OFFSET 0xFFFF
60#define AUDDEC_DEC_MP3 2
61
62#define PCM_BUFSZ_MIN 4800 /* Hold one stereo MP3 frame */
63#define PCM_BUF_MAX_COUNT 5 /* DSP only accepts 5 buffers at most
64 but support 2 buffers currently */
65#define ROUTING_MODE_FTRT 1
66#define ROUTING_MODE_RT 2
67/* Decoder status received from AUDPPTASK */
68#define AUDPP_DEC_STATUS_SLEEP 0
69#define AUDPP_DEC_STATUS_INIT 1
70#define AUDPP_DEC_STATUS_CFG 2
71#define AUDPP_DEC_STATUS_PLAY 3
72
73#define AUDMP3_METAFIELD_MASK 0xFFFF0000
74#define AUDMP3_EOS_FLG_OFFSET 0x0A /* Offset from beginning of buffer */
75#define AUDMP3_EOS_FLG_MASK 0x01
76#define AUDMP3_EOS_NONE 0x0 /* No EOS detected */
77#define AUDMP3_EOS_SET 0x1 /* EOS set in meta field */
78
79#define AUDMP3_EVENT_NUM 10 /* Default number of pre-allocated event packets */
80
81#define BITSTREAM_ERROR_THRESHOLD_VALUE 0x1 /* DEFAULT THRESHOLD VALUE */
82
83#define __CONTAINS(r, v, l) ({ \
84 typeof(r) __r = r; \
85 typeof(v) __v = v; \
86 typeof(v) __e = __v + l; \
87 int res = ((__v >= __r->vaddr) && \
88 (__e <= __r->vaddr + __r->len)); \
89 res; \
90})
91
92#define CONTAINS(r1, r2) ({ \
93 typeof(r2) __r2 = r2; \
94 __CONTAINS(r1, __r2->vaddr, __r2->len); \
95})
96
97#define IN_RANGE(r, v) ({ \
98 typeof(r) __r = r; \
99 typeof(v) __vv = v; \
100 int res = ((__vv >= __r->vaddr) && \
101 (__vv < (__r->vaddr + __r->len))); \
102 res; \
103})
104
105#define OVERLAPS(r1, r2) ({ \
106 typeof(r1) __r1 = r1; \
107 typeof(r2) __r2 = r2; \
108 typeof(__r2->vaddr) __v = __r2->vaddr; \
109 typeof(__v) __e = __v + __r2->len - 1; \
110 int res = (IN_RANGE(__r1, __v) || IN_RANGE(__r1, __e)); \
111 res; \
112})
Vinay Vaka051db722012-01-24 19:48:32 +0530113struct audio;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700114
115struct buffer {
116 void *data;
117 unsigned size;
118 unsigned used; /* Input usage actual DSP produced PCM size */
119 unsigned addr;
120 unsigned short mfield_sz; /*only useful for data has meta field */
121};
122
123#ifdef CONFIG_HAS_EARLYSUSPEND
124struct audmp3_suspend_ctl {
125 struct early_suspend node;
126 struct audio *audio;
127};
128#endif
129
130struct audmp3_event {
131 struct list_head list;
132 int event_type;
133 union msm_audio_event_payload payload;
134};
135
136struct audmp3_pmem_region {
137 struct list_head list;
138 struct file *file;
139 int fd;
140 void *vaddr;
141 unsigned long paddr;
142 unsigned long kvaddr;
143 unsigned long len;
144 unsigned ref_cnt;
145};
146
147struct audmp3_buffer_node {
148 struct list_head list;
149 struct msm_audio_aio_buf buf;
150 unsigned long paddr;
151};
152
153struct audmp3_drv_operations {
154 void (*pcm_buf_update)(struct audio *, uint32_t *);
155 void (*buffer_refresh)(struct audio *);
156 void (*send_data)(struct audio *, unsigned);
157 void (*out_flush)(struct audio *);
158 void (*in_flush)(struct audio *);
159 int (*fsync)(struct audio *);
160};
161
162struct audio {
163 struct buffer out[2];
164
165 spinlock_t dsp_lock;
166
167 uint8_t out_head;
168 uint8_t out_tail;
169 uint8_t out_needed; /* number of buffers the dsp is waiting for */
170 unsigned out_dma_sz;
171 struct list_head out_queue; /* queue to retain output buffers */
172 atomic_t out_bytes;
173
174 struct mutex lock;
175 struct mutex write_lock;
176 wait_queue_head_t write_wait;
177
178 /* Host PCM section */
179 struct buffer in[PCM_BUF_MAX_COUNT];
180 struct mutex read_lock;
181 wait_queue_head_t read_wait; /* Wait queue for read */
182 char *read_data; /* pointer to reader buffer */
183 int32_t read_phys; /* physical address of reader buffer */
184 uint8_t read_next; /* index to input buffers to be read next */
185 uint8_t fill_next; /* index to buffer that DSP should be filling */
186 uint8_t pcm_buf_count; /* number of pcm buffer allocated */
187 struct list_head in_queue; /* queue to retain input buffers */
188 /* ---- End of Host PCM section */
189
190 struct msm_adsp_module *audplay;
191
192 /* configuration to use on next enable */
193 uint32_t out_sample_rate;
194 uint32_t out_channel_mode;
195
196 /* data allocated for various buffers */
197 char *data;
198 int32_t phys; /* physical address of write buffer */
Laura Abbott61399692012-04-30 14:25:46 -0700199 void *map_v_read;
200 void *map_v_write;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700201
202 uint32_t drv_status;
203 int mfield; /* meta field embedded in data */
204 int rflush; /* Read flush */
205 int wflush; /* Write flush */
206 int opened;
207 int enabled;
208 int running;
209 int stopped; /* set when stopped, cleared on flush */
210 int pcm_feedback;
211 int buf_refresh;
212 int teos; /* valid only if tunnel mode & no data left for decoder */
213 enum msm_aud_decoder_state dec_state; /* Represents decoder state */
214 int reserved; /* A byte is being reserved */
215 char rsv_byte; /* Handle odd length user data */
216
217 const char *module_name;
218 unsigned queue_id;
219 uint16_t dec_id;
220 uint32_t read_ptr_offset;
221 int16_t source;
222
223#ifdef CONFIG_HAS_EARLYSUSPEND
224 struct audmp3_suspend_ctl suspend_ctl;
225#endif
226
227#ifdef CONFIG_DEBUG_FS
228 struct dentry *dentry;
229#endif
230
231 wait_queue_head_t wait;
232 struct list_head free_event_queue;
233 struct list_head event_queue;
234 wait_queue_head_t event_wait;
235 spinlock_t event_queue_lock;
236 struct mutex get_event_lock;
237 int event_abort;
238 /* AV sync Info */
239 int avsync_flag; /* Flag to indicate feedback from DSP */
240 wait_queue_head_t avsync_wait;/* Wait queue for AV Sync Message */
241 /* flags, 48 bits sample/bytes counter per channel */
242 uint16_t avsync[AUDPP_AVSYNC_CH_COUNT * AUDPP_AVSYNC_NUM_WORDS + 1];
243
244 uint32_t device_events;
245
246 struct list_head pmem_region_queue; /* protected by lock */
247 struct audmp3_drv_operations drv_ops;
248
249 struct msm_audio_bitstream_info stream_info;
250 struct msm_audio_bitstream_error_info bitstream_error_info;
251 uint32_t bitstream_error_threshold_value;
252
253 int eq_enable;
254 int eq_needs_commit;
255 struct audpp_cmd_cfg_object_params_eqalizer eq;
256 struct audpp_cmd_cfg_object_params_volume vol_pan;
257};
258
259static int auddec_dsp_config(struct audio *audio, int enable);
260static void audpp_cmd_cfg_adec_params(struct audio *audio);
261static void audpp_cmd_cfg_routing_mode(struct audio *audio);
262static void audplay_send_data(struct audio *audio, unsigned needed);
263static void audplay_error_threshold_config(struct audio *audio);
264static void audplay_config_hostpcm(struct audio *audio);
265static void audplay_buffer_refresh(struct audio *audio);
266static void audio_dsp_event(void *private, unsigned id, uint16_t *msg);
267static void audmp3_post_event(struct audio *audio, int type,
268 union msm_audio_event_payload payload);
269static unsigned long audmp3_pmem_fixup(struct audio *audio, void *addr,
270 unsigned long len, int ref_up);
271
272static void mp3_listner(u32 evt_id, union auddev_evt_data *evt_payload,
273 void *private_data)
274{
275 struct audio *audio = (struct audio *) private_data;
276 switch (evt_id) {
277 case AUDDEV_EVT_DEV_RDY:
278 MM_DBG(":AUDDEV_EVT_DEV_RDY\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
283 break;
284 case AUDDEV_EVT_DEV_RLS:
285 MM_DBG(":AUDDEV_EVT_DEV_RLS\n");
286 audio->source &= ~(0x1 << evt_payload->routing_id);
287 if (audio->running == 1 && audio->enabled == 1)
288 audpp_route_stream(audio->dec_id, audio->source);
289 break;
290 case AUDDEV_EVT_STREAM_VOL_CHG:
291 audio->vol_pan.volume = evt_payload->session_vol;
292 MM_DBG(":AUDDEV_EVT_STREAM_VOL_CHG, stream vol %d\n",
293 audio->vol_pan.volume);
294 if (audio->running)
295 audpp_dsp_set_vol_pan(audio->dec_id, &audio->vol_pan,
296 POPP);
297 break;
298 default:
299 MM_ERR(":ERROR:wrong event\n");
300 break;
301 }
302}
303/* must be called with audio->lock held */
304static int audio_enable(struct audio *audio)
305{
306 MM_DBG("\n"); /* Macro prints the file name and function */
307
308 if (audio->enabled)
309 return 0;
310
311 audio->dec_state = MSM_AUD_DECODER_STATE_NONE;
312 audio->out_tail = 0;
313 audio->out_needed = 0;
314
315 if (msm_adsp_enable(audio->audplay)) {
316 MM_ERR("msm_adsp_enable(audplay) failed\n");
317 return -ENODEV;
318 }
319
320 if (audpp_enable(audio->dec_id, audio_dsp_event, audio)) {
321 MM_ERR("audpp_enable() failed\n");
322 msm_adsp_disable(audio->audplay);
323 return -ENODEV;
324 }
325
326 audio->enabled = 1;
327 return 0;
328}
329
330/* must be called with audio->lock held */
331static int audio_disable(struct audio *audio)
332{
333 int rc = 0;
334 MM_DBG("\n"); /* Macro prints the file name and function */
335 if (audio->enabled) {
336 audio->enabled = 0;
337 audio->dec_state = MSM_AUD_DECODER_STATE_NONE;
338 auddec_dsp_config(audio, 0);
339 rc = wait_event_interruptible_timeout(audio->wait,
340 audio->dec_state != MSM_AUD_DECODER_STATE_NONE,
341 msecs_to_jiffies(MSM_AUD_DECODER_WAIT_MS));
342 if (rc == 0)
343 rc = -ETIMEDOUT;
344 else if (audio->dec_state != MSM_AUD_DECODER_STATE_CLOSE)
345 rc = -EFAULT;
346 else
347 rc = 0;
348 wake_up(&audio->write_wait);
349 wake_up(&audio->read_wait);
350 msm_adsp_disable(audio->audplay);
351 audpp_disable(audio->dec_id, audio);
352 audio->out_needed = 0;
353 }
354 return rc;
355}
356
357/* ------------------- dsp --------------------- */
358static void audmp3_async_pcm_buf_update(struct audio *audio, uint32_t *payload)
359{
360 unsigned long flags;
361 union msm_audio_event_payload event_payload;
362 struct audmp3_buffer_node *filled_buf;
363 uint8_t index;
364
365 if (audio->rflush)
366 return;
367
368 spin_lock_irqsave(&audio->dsp_lock, flags);
369 for (index = 0; index < payload[1]; index++) {
370 BUG_ON(list_empty(&audio->in_queue));
371 filled_buf = list_first_entry(&audio->in_queue,
372 struct audmp3_buffer_node, list);
373 if (filled_buf->paddr == payload[2 + index * 2]) {
374 list_del(&filled_buf->list);
375 event_payload.aio_buf = filled_buf->buf;
376 event_payload.aio_buf.data_len =
377 payload[3 + index * 2];
378 MM_DBG("pcm buf %p data_len %d\n", filled_buf,
379 event_payload.aio_buf.data_len);
380 audmp3_post_event(audio, AUDIO_EVENT_READ_DONE,
381 event_payload);
382 kfree(filled_buf);
383 } else {
384 MM_ERR("expected=%lx ret=%x\n", filled_buf->paddr,
385 payload[2 + index * 2]);
386 break;
387 }
388 }
389
390 audio->drv_status &= ~ADRV_STATUS_IBUF_GIVEN;
391 audio->drv_ops.buffer_refresh(audio);
392 spin_unlock_irqrestore(&audio->dsp_lock, flags);
393
394}
395
396static void audio_update_pcm_buf_entry(struct audio *audio, uint32_t *payload)
397{
398 uint8_t index;
399 unsigned long flags;
400
401 if (audio->rflush)
402 return;
403
404 spin_lock_irqsave(&audio->dsp_lock, flags);
405 for (index = 0; index < payload[1]; index++) {
406 if (audio->in[audio->fill_next].addr ==
407 payload[2 + index * 2]) {
408 MM_DBG("in[%d] ready\n", audio->fill_next);
409 audio->in[audio->fill_next].used =
410 payload[3 + index * 2];
411 if ((++audio->fill_next) == audio->pcm_buf_count)
412 audio->fill_next = 0;
413
414 } else {
415 MM_ERR("expected=%x ret=%x\n",
416 audio->in[audio->fill_next].addr,
417 payload[2 + index * 2]);
418 break;
419 }
420 }
421 if (audio->in[audio->fill_next].used == 0) {
422 audio->drv_ops.buffer_refresh(audio);
423 } else {
424 MM_DBG("read cannot keep up\n");
425 audio->buf_refresh = 1;
426 }
427 wake_up(&audio->read_wait);
428 spin_unlock_irqrestore(&audio->dsp_lock, flags);
429
430}
431
432static void audmp3_bitstream_error_info(struct audio *audio, uint32_t *payload)
433{
434 unsigned long flags;
435 union msm_audio_event_payload e_payload;
436
437 if (payload[0] != AUDDEC_DEC_MP3) {
438 MM_ERR("Unexpected bitstream error info from DSP:\
439 Invalid decoder\n");
440 return;
441 }
442
443 /* get stream info from DSP msg */
444 spin_lock_irqsave(&audio->dsp_lock, flags);
445
446 audio->bitstream_error_info.dec_id = payload[0];
447 audio->bitstream_error_info.err_msg_indicator = payload[1];
448 audio->bitstream_error_info.err_type = payload[2];
449
450 spin_unlock_irqrestore(&audio->dsp_lock, flags);
451 MM_ERR("bit_stream_error_type=%d error_count=%d\n",
452 audio->bitstream_error_info.err_type, (0x0000FFFF &
453 audio->bitstream_error_info.err_msg_indicator));
454
455 /* send event to ARM to notify error info coming */
456 e_payload.error_info = audio->bitstream_error_info;
457 audmp3_post_event(audio, AUDIO_EVENT_BITSTREAM_ERROR_INFO, e_payload);
458}
459
460static void audmp3_update_stream_info(struct audio *audio, uint32_t *payload)
461{
462 unsigned long flags;
463 union msm_audio_event_payload e_payload;
464
465 /* get stream info from DSP msg */
466 spin_lock_irqsave(&audio->dsp_lock, flags);
467
468 audio->stream_info.codec_type = AUDIO_CODEC_TYPE_MP3;
469 audio->stream_info.chan_info = (0x0000FFFF & payload[1]);
470 audio->stream_info.sample_rate = (0x0000FFFF & payload[2]);
471 audio->stream_info.bit_stream_info = (0x0000FFFF & payload[3]);
472 audio->stream_info.bit_rate = payload[4];
473
474 spin_unlock_irqrestore(&audio->dsp_lock, flags);
475 MM_DBG("chan_info=%d, sample_rate=%d, bit_stream_info=%d\n",
476 audio->stream_info.chan_info,
477 audio->stream_info.sample_rate,
478 audio->stream_info.bit_stream_info);
479
480 /* send event to ARM to notify steam info coming */
481 e_payload.stream_info = audio->stream_info;
482 audmp3_post_event(audio, AUDIO_EVENT_STREAM_INFO, e_payload);
483}
484
485static void audplay_dsp_event(void *data, unsigned id, size_t len,
486 void (*getevent) (void *ptr, size_t len))
487{
488 struct audio *audio = data;
489 uint32_t msg[28];
490 getevent(msg, sizeof(msg));
491
492 MM_DBG("msg_id=%x\n", id);
493
494 switch (id) {
495 case AUDPLAY_MSG_DEC_NEEDS_DATA:
496 audio->drv_ops.send_data(audio, 1);
497 break;
498
499 case AUDPLAY_MSG_BUFFER_UPDATE:
500 audio->drv_ops.pcm_buf_update(audio, msg);
501 break;
502
503 case AUDPLAY_UP_STREAM_INFO:
504 if ((msg[1] & AUDPLAY_STREAM_INFO_MSG_MASK) ==
505 AUDPLAY_STREAM_INFO_MSG_MASK) {
506 audmp3_bitstream_error_info(audio, msg);
507 } else {
508 audmp3_update_stream_info(audio, msg);
509 }
510 break;
511
512 case AUDPLAY_UP_OUTPORT_FLUSH_ACK:
513 MM_DBG("OUTPORT_FLUSH_ACK\n");
514 audio->rflush = 0;
515 wake_up(&audio->read_wait);
516 if (audio->pcm_feedback)
517 audio->drv_ops.buffer_refresh(audio);
518 break;
519
520 case ADSP_MESSAGE_ID:
521 MM_DBG("Received ADSP event: module enable(audplaytask)\n");
522 break;
523
524 default:
525 MM_ERR("unexpected message from decoder \n");
526 break;
527 }
528}
529
530static void audio_dsp_event(void *private, unsigned id, uint16_t *msg)
531{
532 struct audio *audio = private;
533
534 switch (id) {
535 case AUDPP_MSG_STATUS_MSG:{
536 unsigned status = msg[1];
537
538 switch (status) {
539 case AUDPP_DEC_STATUS_SLEEP: {
540 uint16_t reason = msg[2];
541 MM_DBG("decoder status: sleep reason=0x%04x\n",
542 reason);
543 if ((reason == AUDPP_MSG_REASON_MEM)
544 || (reason ==
545 AUDPP_MSG_REASON_NODECODER)) {
546 audio->dec_state =
547 MSM_AUD_DECODER_STATE_FAILURE;
548 wake_up(&audio->wait);
549 } else if (reason == AUDPP_MSG_REASON_NONE) {
550 /* decoder is in disable state */
551 audio->dec_state =
552 MSM_AUD_DECODER_STATE_CLOSE;
553 wake_up(&audio->wait);
554 }
555 break;
556 }
557 case AUDPP_DEC_STATUS_INIT:
558 MM_DBG("decoder status: init \n");
559 if (audio->pcm_feedback)
560 audpp_cmd_cfg_routing_mode(audio);
561 else
562 audpp_cmd_cfg_adec_params(audio);
563 break;
564
565 case AUDPP_DEC_STATUS_CFG:
566 MM_DBG("decoder status: cfg \n");
567 break;
568 case AUDPP_DEC_STATUS_PLAY:
569 MM_DBG("decoder status: play \n");
570 /* send mixer command */
571 audpp_route_stream(audio->dec_id,
572 audio->source);
573 if (audio->pcm_feedback) {
574 audplay_error_threshold_config(audio);
575 audplay_config_hostpcm(audio);
576 audio->drv_ops.buffer_refresh(audio);
577 }
578 audio->dec_state =
579 MSM_AUD_DECODER_STATE_SUCCESS;
580 wake_up(&audio->wait);
581 break;
582 default:
583 MM_ERR("unknown decoder status \n");
584 break;
585 }
586 break;
587 }
588 case AUDPP_MSG_CFG_MSG:
589 if (msg[0] == AUDPP_MSG_ENA_ENA) {
590 MM_DBG("CFG_MSG ENABLE\n");
591 auddec_dsp_config(audio, 1);
592 audio->out_needed = 0;
593 audio->running = 1;
594 audpp_dsp_set_vol_pan(audio->dec_id, &audio->vol_pan,
595 POPP);
596 audpp_dsp_set_eq(audio->dec_id, audio->eq_enable,
597 &audio->eq, POPP);
598 } else if (msg[0] == AUDPP_MSG_ENA_DIS) {
599 MM_DBG("CFG_MSG DISABLE\n");
600 audio->running = 0;
601 } else {
602 MM_DBG("CFG_MSG %d?\n", msg[0]);
603 }
604 break;
605 case AUDPP_MSG_ROUTING_ACK:
606 MM_DBG("ROUTING_ACK mode=%d\n", msg[1]);
607 audpp_cmd_cfg_adec_params(audio);
608 break;
609
610 case AUDPP_MSG_FLUSH_ACK:
611 MM_DBG("FLUSH_ACK\n");
612 audio->wflush = 0;
613 audio->rflush = 0;
614 wake_up(&audio->write_wait);
615 if (audio->pcm_feedback)
616 audio->drv_ops.buffer_refresh(audio);
617 break;
618
619 case AUDPP_MSG_PCMDMAMISSED:
620 MM_DBG("PCMDMAMISSED\n");
621 audio->teos = 1;
622 wake_up(&audio->write_wait);
623 break;
624
625 case AUDPP_MSG_AVSYNC_MSG:
626 MM_DBG("AUDPP_MSG_AVSYNC_MSG\n");
627 memcpy(&audio->avsync[0], msg, sizeof(audio->avsync));
628 audio->avsync_flag = 1;
629 wake_up(&audio->avsync_wait);
630 break;
631
632 default:
633 MM_ERR("UNKNOWN (%d)\n", id);
634 }
635
636}
637
638
639struct msm_adsp_ops audplay_adsp_ops = {
640 .event = audplay_dsp_event,
641};
642
643
644#define audplay_send_queue0(audio, cmd, len) \
645 msm_adsp_write(audio->audplay, audio->queue_id, \
646 cmd, len)
647
648static int auddec_dsp_config(struct audio *audio, int enable)
649{
650 struct audpp_cmd_cfg_dec_type cfg_dec_cmd;
651
652 memset(&cfg_dec_cmd, 0, sizeof(cfg_dec_cmd));
653
654 cfg_dec_cmd.cmd_id = AUDPP_CMD_CFG_DEC_TYPE;
655 if (enable)
656 cfg_dec_cmd.dec_cfg = AUDPP_CMD_UPDATDE_CFG_DEC |
657 AUDPP_CMD_ENA_DEC_V | AUDDEC_DEC_MP3;
658 else
659 cfg_dec_cmd.dec_cfg = AUDPP_CMD_UPDATDE_CFG_DEC |
660 AUDPP_CMD_DIS_DEC_V;
661 cfg_dec_cmd.dm_mode = 0x0;
662 cfg_dec_cmd.stream_id = audio->dec_id;
663 return audpp_send_queue1(&cfg_dec_cmd, sizeof(cfg_dec_cmd));
664}
665
666static void audpp_cmd_cfg_adec_params(struct audio *audio)
667{
668 struct audpp_cmd_cfg_adec_params_mp3 cmd;
669
670 memset(&cmd, 0, sizeof(cmd));
671 cmd.common.cmd_id = AUDPP_CMD_CFG_ADEC_PARAMS;
672 cmd.common.length = AUDPP_CMD_CFG_ADEC_PARAMS_MP3_LEN;
673 cmd.common.dec_id = audio->dec_id;
674 cmd.common.input_sampling_frequency = audio->out_sample_rate;
675
676 audpp_send_queue2(&cmd, sizeof(cmd));
677}
678
679static void audpp_cmd_cfg_routing_mode(struct audio *audio)
680{
681 struct audpp_cmd_routing_mode cmd;
682 MM_DBG("\n"); /* Macro prints the file name and function */
683 memset(&cmd, 0, sizeof(cmd));
684 cmd.cmd_id = AUDPP_CMD_ROUTING_MODE;
685 cmd.object_number = audio->dec_id;
686 if (audio->pcm_feedback)
687 cmd.routing_mode = ROUTING_MODE_FTRT;
688 else
689 cmd.routing_mode = ROUTING_MODE_RT;
690
691 audpp_send_queue1(&cmd, sizeof(cmd));
692}
693
694static int audplay_dsp_send_data_avail(struct audio *audio,
695 unsigned idx, unsigned len)
696{
697 struct audplay_cmd_bitstream_data_avail_nt2 cmd;
698
699 cmd.cmd_id = AUDPLAY_CMD_BITSTREAM_DATA_AVAIL_NT2;
700 if (audio->mfield)
701 cmd.decoder_id = AUDMP3_METAFIELD_MASK |
702 (audio->out[idx].mfield_sz >> 1);
703 else
704 cmd.decoder_id = audio->dec_id;
705 cmd.buf_ptr = audio->out[idx].addr;
706 cmd.buf_size = len/2;
707 cmd.partition_number = 0;
708 return audplay_send_queue0(audio, &cmd, sizeof(cmd));
709}
710/* Caller holds irq_lock */
711static void audmp3_async_buffer_refresh(struct audio *audio)
712{
713 struct audplay_cmd_buffer_refresh refresh_cmd;
714 struct audmp3_buffer_node *next_buf;
715
716 if (!audio->running ||
717 audio->drv_status & ADRV_STATUS_IBUF_GIVEN)
718 return;
719
720 if (!list_empty(&audio->in_queue)) {
721 next_buf = list_first_entry(&audio->in_queue,
722 struct audmp3_buffer_node, list);
723 if (!next_buf)
724 return;
725 MM_DBG("next buf %p phy %lx len %d\n", next_buf,
726 next_buf->paddr, next_buf->buf.buf_len);
727 refresh_cmd.cmd_id = AUDPLAY_CMD_BUFFER_REFRESH;
728 refresh_cmd.num_buffers = 1;
729 refresh_cmd.buf0_address = next_buf->paddr;
730 refresh_cmd.buf0_length = next_buf->buf.buf_len -
731 (next_buf->buf.buf_len % 576) +
732 (audio->mfield ? 24 : 0); /* Mp3 frame size */
733 refresh_cmd.buf_read_count = 0;
734 audio->drv_status |= ADRV_STATUS_IBUF_GIVEN;
735 (void) audplay_send_queue0(audio, &refresh_cmd,
736 sizeof(refresh_cmd));
737 }
738
739}
740
741static void audplay_buffer_refresh(struct audio *audio)
742{
743 struct audplay_cmd_buffer_refresh refresh_cmd;
744
745 refresh_cmd.cmd_id = AUDPLAY_CMD_BUFFER_REFRESH;
746 refresh_cmd.num_buffers = 1;
747 refresh_cmd.buf0_address = audio->in[audio->fill_next].addr;
748 refresh_cmd.buf0_length = audio->in[audio->fill_next].size -
749 (audio->in[audio->fill_next].size % 576) +
750 (audio->mfield ? 24 : 0); /* Mp3 frame size */
751 refresh_cmd.buf_read_count = 0;
752 MM_DBG("buf0_addr=%x buf0_len=%d\n", refresh_cmd.buf0_address,
753 refresh_cmd.buf0_length);
754 (void)audplay_send_queue0(audio, &refresh_cmd, sizeof(refresh_cmd));
755}
756
757static void audplay_error_threshold_config(struct audio *audio)
758{
759 union audplay_cmd_channel_info ch_cfg_cmd;
760
761 MM_DBG("\n"); /* Macro prints the file name and function */
762 ch_cfg_cmd.thr_update.cmd_id = AUDPLAY_CMD_CHANNEL_INFO;
763 ch_cfg_cmd.thr_update.threshold_update = AUDPLAY_ERROR_THRESHOLD_ENABLE;
764 ch_cfg_cmd.thr_update.threshold_value =
765 audio->bitstream_error_threshold_value;
766 (void)audplay_send_queue0(audio, &ch_cfg_cmd, sizeof(ch_cfg_cmd));
767}
768
769static void audplay_config_hostpcm(struct audio *audio)
770{
771 struct audplay_cmd_hpcm_buf_cfg cfg_cmd;
772
773 MM_DBG("\n"); /* Macro prints the file name and function */
774 cfg_cmd.cmd_id = AUDPLAY_CMD_HPCM_BUF_CFG;
775 cfg_cmd.max_buffers = 1;
776 cfg_cmd.byte_swap = 0;
777 cfg_cmd.hostpcm_config = (0x8000) | (0x4000);
778 cfg_cmd.feedback_frequency = 1;
779 cfg_cmd.partition_number = 0;
780 (void)audplay_send_queue0(audio, &cfg_cmd, sizeof(cfg_cmd));
781
782}
783
784static void audplay_outport_flush(struct audio *audio)
785{
786 struct audplay_cmd_outport_flush op_flush_cmd;
787
788 MM_DBG("\n"); /* Macro prints the file name and function */
789 op_flush_cmd.cmd_id = AUDPLAY_CMD_OUTPORT_FLUSH;
790 (void)audplay_send_queue0(audio, &op_flush_cmd, sizeof(op_flush_cmd));
791}
792
793static void audmp3_async_send_data(struct audio *audio, unsigned needed)
794{
795 unsigned long flags;
796
797 spin_lock_irqsave(&audio->dsp_lock, flags);
798 if (!audio->running)
799 goto done;
800
801 if (needed && !audio->wflush) {
802 audio->out_needed = 1;
803 if (audio->drv_status & ADRV_STATUS_OBUF_GIVEN) {
804 /* pop one node out of queue */
805 union msm_audio_event_payload payload;
806 struct audmp3_buffer_node *used_buf;
807
808 MM_DBG("consumed\n");
809 BUG_ON(list_empty(&audio->out_queue));
810 used_buf = list_first_entry(&audio->out_queue,
811 struct audmp3_buffer_node, list);
812 list_del(&used_buf->list);
813 payload.aio_buf = used_buf->buf;
814 audmp3_post_event(audio, AUDIO_EVENT_WRITE_DONE,
815 payload);
816 kfree(used_buf);
817 audio->drv_status &= ~ADRV_STATUS_OBUF_GIVEN;
818 }
819
820 }
821
822 if (audio->out_needed) {
823 struct audmp3_buffer_node *next_buf;
824 struct audplay_cmd_bitstream_data_avail_nt2 cmd;
825 if (!list_empty(&audio->out_queue)) {
826 next_buf = list_first_entry(&audio->out_queue,
827 struct audmp3_buffer_node, list);
828 MM_DBG("next_buf %p\n", next_buf);
829 if (next_buf) {
830 MM_DBG("next buf phy %lx len %d\n",
831 next_buf->paddr,
832 next_buf->buf.data_len);
833
834 cmd.cmd_id =
835 AUDPLAY_CMD_BITSTREAM_DATA_AVAIL_NT2;
836 if (audio->mfield)
837 cmd.decoder_id = AUDMP3_METAFIELD_MASK |
838 (next_buf->buf.mfield_sz >> 1);
839 else
840 cmd.decoder_id = audio->dec_id;
841 cmd.buf_ptr = (unsigned) next_buf->paddr;
842 cmd.buf_size = next_buf->buf.data_len >> 1;
843 cmd.partition_number = 0;
844 audplay_send_queue0(audio, &cmd, sizeof(cmd));
845 audio->out_needed = 0;
846 audio->drv_status |= ADRV_STATUS_OBUF_GIVEN;
847 }
848 }
849 }
850
851done:
852 spin_unlock_irqrestore(&audio->dsp_lock, flags);
853}
854
855static void audplay_send_data(struct audio *audio, unsigned needed)
856{
857 struct buffer *frame;
858 unsigned long flags;
859
860 spin_lock_irqsave(&audio->dsp_lock, flags);
861 if (!audio->running)
862 goto done;
863
864 if (needed && !audio->wflush) {
865 /* We were called from the callback because the DSP
866 * requested more data. Note that the DSP does want
867 * more data, and if a buffer was in-flight, mark it
868 * as available (since the DSP must now be done with
869 * it).
870 */
871 audio->out_needed = 1;
872 frame = audio->out + audio->out_tail;
873 if (frame->used == 0xffffffff) {
874 MM_DBG("frame %d free\n", audio->out_tail);
875 frame->used = 0;
876 audio->out_tail ^= 1;
877 wake_up(&audio->write_wait);
878 }
879 }
880
881 if (audio->out_needed) {
882 /* If the DSP currently wants data and we have a
883 * buffer available, we will send it and reset
884 * the needed flag. We'll mark the buffer as in-flight
885 * so that it won't be recycled until the next buffer
886 * is requested
887 */
888
889 frame = audio->out + audio->out_tail;
890 if (frame->used) {
891 BUG_ON(frame->used == 0xffffffff);
892 MM_DBG("frame %d busy\n", audio->out_tail);
893 audplay_dsp_send_data_avail(audio, audio->out_tail,
894 frame->used);
895 frame->used = 0xffffffff;
896 audio->out_needed = 0;
897 }
898 }
899done:
900 spin_unlock_irqrestore(&audio->dsp_lock, flags);
901}
902
903/* ------------------- device --------------------- */
904static void audmp3_async_flush(struct audio *audio)
905{
906 struct audmp3_buffer_node *buf_node;
907 struct list_head *ptr, *next;
908 union msm_audio_event_payload payload;
909
910 MM_DBG("\n"); /* Macro prints the file name and function */
911 list_for_each_safe(ptr, next, &audio->out_queue) {
912 buf_node = list_entry(ptr, struct audmp3_buffer_node, list);
913 list_del(&buf_node->list);
914 payload.aio_buf = buf_node->buf;
915 audmp3_post_event(audio, AUDIO_EVENT_WRITE_DONE,
916 payload);
917 kfree(buf_node);
918 }
919 audio->drv_status &= ~ADRV_STATUS_OBUF_GIVEN;
920 audio->out_needed = 0;
921 atomic_set(&audio->out_bytes, 0);
922}
923
924static void audio_flush(struct audio *audio)
925{
926 audio->out[0].used = 0;
927 audio->out[1].used = 0;
928 audio->out_head = 0;
929 audio->out_tail = 0;
930 audio->reserved = 0;
931 audio->out_needed = 0;
932 atomic_set(&audio->out_bytes, 0);
933}
934
935static void audmp3_async_flush_pcm_buf(struct audio *audio)
936{
937 struct audmp3_buffer_node *buf_node;
938 struct list_head *ptr, *next;
939 union msm_audio_event_payload payload;
940
941 MM_DBG("\n"); /* Macro prints the file name and function */
942 list_for_each_safe(ptr, next, &audio->in_queue) {
943 buf_node = list_entry(ptr, struct audmp3_buffer_node, list);
944 list_del(&buf_node->list);
945 payload.aio_buf = buf_node->buf;
946 payload.aio_buf.data_len = 0;
947 audmp3_post_event(audio, AUDIO_EVENT_READ_DONE,
948 payload);
949 kfree(buf_node);
950 }
951 audio->drv_status &= ~ADRV_STATUS_IBUF_GIVEN;
952
953}
954
955static void audio_flush_pcm_buf(struct audio *audio)
956{
957 uint8_t index;
958
959 for (index = 0; index < PCM_BUF_MAX_COUNT; index++)
960 audio->in[index].used = 0;
961
962 audio->buf_refresh = 0;
963 audio->read_next = 0;
964 audio->fill_next = 0;
965}
966
967static void audio_ioport_reset(struct audio *audio)
968{
969 if (audio->drv_status & ADRV_STATUS_AIO_INTF) {
970 /* If fsync is in progress, make sure
971 * return value of fsync indicates
972 * abort due to flush
973 */
974 if (audio->drv_status & ADRV_STATUS_FSYNC) {
975 MM_DBG("fsync in progress\n");
976 wake_up(&audio->write_wait);
977 mutex_lock(&audio->write_lock);
978 audio->drv_ops.out_flush(audio);
979 mutex_unlock(&audio->write_lock);
980 } else
981 audio->drv_ops.out_flush(audio);
982 audio->drv_ops.in_flush(audio);
983 } else {
984 /* Make sure read/write thread are free from
985 * sleep and knowing that system is not able
986 * to process io request at the moment
987 */
988 wake_up(&audio->write_wait);
989 mutex_lock(&audio->write_lock);
990 audio->drv_ops.out_flush(audio);
991 mutex_unlock(&audio->write_lock);
992 wake_up(&audio->read_wait);
993 mutex_lock(&audio->read_lock);
994 audio->drv_ops.in_flush(audio);
995 mutex_unlock(&audio->read_lock);
996 }
997 audio->avsync_flag = 1;
998 wake_up(&audio->avsync_wait);
999}
1000
1001static int audmp3_events_pending(struct audio *audio)
1002{
1003 unsigned long flags;
1004 int empty;
1005
1006 spin_lock_irqsave(&audio->event_queue_lock, flags);
1007 empty = !list_empty(&audio->event_queue);
1008 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
1009 return empty || audio->event_abort;
1010}
1011
1012static void audmp3_reset_event_queue(struct audio *audio)
1013{
1014 unsigned long flags;
1015 struct audmp3_event *drv_evt;
1016 struct list_head *ptr, *next;
1017
1018 spin_lock_irqsave(&audio->event_queue_lock, flags);
1019 list_for_each_safe(ptr, next, &audio->event_queue) {
1020 drv_evt = list_first_entry(&audio->event_queue,
1021 struct audmp3_event, list);
1022 list_del(&drv_evt->list);
1023 kfree(drv_evt);
1024 }
1025 list_for_each_safe(ptr, next, &audio->free_event_queue) {
1026 drv_evt = list_first_entry(&audio->free_event_queue,
1027 struct audmp3_event, list);
1028 list_del(&drv_evt->list);
1029 kfree(drv_evt);
1030 }
1031 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
1032
1033 return;
1034}
1035
1036static long audmp3_process_event_req(struct audio *audio, void __user *arg)
1037{
1038 long rc;
1039 struct msm_audio_event usr_evt;
1040 struct audmp3_event *drv_evt = NULL;
1041 int timeout;
1042 unsigned long flags;
1043
1044 if (copy_from_user(&usr_evt, arg, sizeof(struct msm_audio_event)))
1045 return -EFAULT;
1046
1047 timeout = (int) usr_evt.timeout_ms;
1048
1049 if (timeout > 0) {
1050 rc = wait_event_interruptible_timeout(
1051 audio->event_wait, audmp3_events_pending(audio),
1052 msecs_to_jiffies(timeout));
1053 if (rc == 0)
1054 return -ETIMEDOUT;
1055 } else {
1056 rc = wait_event_interruptible(
1057 audio->event_wait, audmp3_events_pending(audio));
1058 }
1059
1060 if (rc < 0)
1061 return rc;
1062
1063 if (audio->event_abort) {
1064 audio->event_abort = 0;
1065 return -ENODEV;
1066 }
1067
1068 rc = 0;
1069
1070 spin_lock_irqsave(&audio->event_queue_lock, flags);
1071 if (!list_empty(&audio->event_queue)) {
1072 drv_evt = list_first_entry(&audio->event_queue,
1073 struct audmp3_event, list);
1074 list_del(&drv_evt->list);
1075 }
1076 if (drv_evt) {
1077 usr_evt.event_type = drv_evt->event_type;
1078 usr_evt.event_payload = drv_evt->payload;
1079 list_add_tail(&drv_evt->list, &audio->free_event_queue);
1080 } else
1081 rc = -1;
1082 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
1083
1084 if (drv_evt->event_type == AUDIO_EVENT_WRITE_DONE ||
1085 drv_evt->event_type == AUDIO_EVENT_READ_DONE) {
1086 mutex_lock(&audio->lock);
1087 audmp3_pmem_fixup(audio, drv_evt->payload.aio_buf.buf_addr,
1088 drv_evt->payload.aio_buf.buf_len, 0);
1089 mutex_unlock(&audio->lock);
1090 }
1091 if (!rc && copy_to_user(arg, &usr_evt, sizeof(usr_evt)))
1092 rc = -EFAULT;
1093
1094 return rc;
1095}
1096
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001097static int audmp3_pmem_lookup_vaddr(struct audio *audio, void *addr,
1098 unsigned long len, struct audmp3_pmem_region **region)
1099{
1100 struct audmp3_pmem_region *region_elt;
1101
1102 int match_count = 0;
1103
1104 *region = NULL;
1105
1106 /* returns physical address or zero */
1107 list_for_each_entry(region_elt, &audio->pmem_region_queue,
1108 list) {
1109 if (addr >= region_elt->vaddr &&
1110 addr < region_elt->vaddr + region_elt->len &&
1111 addr + len <= region_elt->vaddr + region_elt->len) {
1112 /* offset since we could pass vaddr inside a registerd
1113 * pmem buffer
1114 */
1115
1116 match_count++;
1117 if (!*region)
1118 *region = region_elt;
1119 }
1120 }
1121
1122 if (match_count > 1) {
1123 MM_ERR("multiple hits for vaddr %p, len %ld\n", addr, len);
1124 list_for_each_entry(region_elt,
1125 &audio->pmem_region_queue, list) {
1126 if (addr >= region_elt->vaddr &&
1127 addr < region_elt->vaddr + region_elt->len &&
1128 addr + len <= region_elt->vaddr + region_elt->len)
1129 MM_ERR("\t%p, %ld --> %p\n", region_elt->vaddr,
1130 region_elt->len,
1131 (void *)region_elt->paddr);
1132 }
1133 }
1134
1135 return *region ? 0 : -1;
1136}
1137
1138unsigned long audmp3_pmem_fixup(struct audio *audio, void *addr,
1139 unsigned long len, int ref_up)
1140{
1141 struct audmp3_pmem_region *region;
1142 unsigned long paddr;
1143 int ret;
1144
1145 ret = audmp3_pmem_lookup_vaddr(audio, addr, len, &region);
1146 if (ret) {
1147 MM_ERR("lookup (%p, %ld) failed\n", addr, len);
1148 return 0;
1149 }
1150 if (ref_up)
1151 region->ref_cnt++;
1152 else
1153 region->ref_cnt--;
1154 MM_DBG("found region %p ref_cnt %d\n", region, region->ref_cnt);
1155 paddr = region->paddr + (addr - region->vaddr);
1156 return paddr;
1157}
1158
1159/* audio -> lock must be held at this point */
1160static int audmp3_aio_buf_add(struct audio *audio, unsigned dir,
1161 void __user *arg)
1162{
1163 unsigned long flags;
1164 struct audmp3_buffer_node *buf_node;
1165
1166 buf_node = kmalloc(sizeof(*buf_node), GFP_KERNEL);
1167
1168 if (!buf_node)
1169 return -ENOMEM;
1170
1171 if (copy_from_user(&buf_node->buf, arg, sizeof(buf_node->buf))) {
1172 kfree(buf_node);
1173 return -EFAULT;
1174 }
1175
1176 MM_DBG("node %p dir %x buf_addr %p buf_len %d data_len \
1177 %d\n", buf_node, dir,
1178 buf_node->buf.buf_addr, buf_node->buf.buf_len,
1179 buf_node->buf.data_len);
1180
1181 buf_node->paddr = audmp3_pmem_fixup(
1182 audio, buf_node->buf.buf_addr,
1183 buf_node->buf.buf_len, 1);
1184
1185 if (dir) {
1186 /* write */
1187 if (!buf_node->paddr ||
1188 (buf_node->paddr & 0x1) ||
1189 (buf_node->buf.data_len & 0x1) ||
1190 (!audio->pcm_feedback &&
1191 !buf_node->buf.data_len)) {
1192 kfree(buf_node);
1193 return -EINVAL;
1194 }
1195 spin_lock_irqsave(&audio->dsp_lock, flags);
1196 list_add_tail(&buf_node->list, &audio->out_queue);
1197 spin_unlock_irqrestore(&audio->dsp_lock, flags);
1198 audio->drv_ops.send_data(audio, 0);
1199 } else {
1200 /* read */
1201 if (!buf_node->paddr ||
1202 (buf_node->paddr & 0x1) ||
1203 (buf_node->buf.buf_len < PCM_BUFSZ_MIN)) {
1204 kfree(buf_node);
1205 return -EINVAL;
1206 }
1207 spin_lock_irqsave(&audio->dsp_lock, flags);
1208 list_add_tail(&buf_node->list, &audio->in_queue);
1209 audio->drv_ops.buffer_refresh(audio);
1210 spin_unlock_irqrestore(&audio->dsp_lock, flags);
1211 }
1212
1213 MM_DBG("Add buf_node %p paddr %lx\n", buf_node, buf_node->paddr);
1214
1215 return 0;
1216}
1217
1218static int audio_enable_eq(struct audio *audio, int enable)
1219{
1220 if (audio->eq_enable == enable && !audio->eq_needs_commit)
1221 return 0;
1222
1223 audio->eq_enable = enable;
1224
1225 if (audio->running) {
1226 audpp_dsp_set_eq(audio->dec_id, enable, &audio->eq, POPP);
1227 audio->eq_needs_commit = 0;
1228 }
1229 return 0;
1230}
1231
1232static int audio_get_avsync_data(struct audio *audio,
1233 struct msm_audio_stats *stats)
1234{
1235 int rc = -EINVAL;
1236 unsigned long flags;
1237
1238 local_irq_save(flags);
1239 if (audio->dec_id == audio->avsync[0] && audio->avsync_flag) {
1240 /* av_sync sample count */
1241 stats->sample_count = (audio->avsync[2] << 16) |
1242 (audio->avsync[3]);
1243
1244 /* av_sync byte_count */
1245 stats->byte_count = (audio->avsync[5] << 16) |
1246 (audio->avsync[6]);
1247
1248 audio->avsync_flag = 0;
1249 rc = 0;
1250 }
1251 local_irq_restore(flags);
1252 return rc;
1253
1254}
1255
1256static long audio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1257{
1258 struct audio *audio = file->private_data;
1259 int rc = -EINVAL;
1260 unsigned long flags = 0;
1261 uint16_t enable_mask;
1262 int enable;
1263 int prev_state;
1264
1265 MM_DBG("cmd = %d\n", cmd);
1266
1267 if (cmd == AUDIO_GET_STATS) {
1268 struct msm_audio_stats stats;
1269
1270 audio->avsync_flag = 0;
1271 memset(&stats, 0, sizeof(stats));
1272 if (audpp_query_avsync(audio->dec_id) < 0)
1273 return rc;
1274
1275 rc = wait_event_interruptible_timeout(audio->avsync_wait,
1276 (audio->avsync_flag == 1),
1277 msecs_to_jiffies(AUDPP_AVSYNC_EVENT_TIMEOUT));
1278
1279 if (rc < 0)
1280 return rc;
1281 else if ((rc > 0) || ((rc == 0) && (audio->avsync_flag == 1))) {
1282 if (audio_get_avsync_data(audio, &stats) < 0)
1283 return rc;
1284
1285 if (copy_to_user((void *)arg, &stats, sizeof(stats)))
1286 return -EFAULT;
1287 return 0;
1288 } else
1289 return -EAGAIN;
1290 }
1291
1292 switch (cmd) {
1293 case AUDIO_ENABLE_AUDPP:
1294 if (copy_from_user(&enable_mask, (void *) arg,
1295 sizeof(enable_mask))) {
1296 rc = -EFAULT;
1297 break;
1298 }
1299
1300 spin_lock_irqsave(&audio->dsp_lock, flags);
1301 enable = (enable_mask & EQ_ENABLE) ? 1 : 0;
1302 audio_enable_eq(audio, enable);
1303 spin_unlock_irqrestore(&audio->dsp_lock, flags);
1304 rc = 0;
1305 break;
1306 case AUDIO_SET_VOLUME:
1307 spin_lock_irqsave(&audio->dsp_lock, flags);
1308 audio->vol_pan.volume = arg;
1309 if (audio->running)
1310 audpp_dsp_set_vol_pan(audio->dec_id, &audio->vol_pan,
1311 POPP);
1312 spin_unlock_irqrestore(&audio->dsp_lock, flags);
1313 rc = 0;
1314 break;
1315
1316 case AUDIO_SET_PAN:
1317 spin_lock_irqsave(&audio->dsp_lock, flags);
1318 audio->vol_pan.pan = arg;
1319 if (audio->running)
1320 audpp_dsp_set_vol_pan(audio->dec_id, &audio->vol_pan,
1321 POPP);
1322 spin_unlock_irqrestore(&audio->dsp_lock, flags);
1323 rc = 0;
1324 break;
1325
1326 case AUDIO_SET_EQ:
1327 prev_state = audio->eq_enable;
1328 audio->eq_enable = 0;
1329 if (copy_from_user(&audio->eq.num_bands, (void *) arg,
1330 sizeof(audio->eq) -
1331 (AUDPP_CMD_CFG_OBJECT_PARAMS_COMMON_LEN + 2))) {
1332 rc = -EFAULT;
1333 break;
1334 }
1335 audio->eq_enable = prev_state;
1336 audio->eq_needs_commit = 1;
1337 rc = 0;
1338 break;
1339 }
1340
1341 if (-EINVAL != rc)
1342 return rc;
1343
1344 if (cmd == AUDIO_GET_EVENT) {
1345 MM_DBG(" AUDIO_GET_EVENT\n");
1346 if (mutex_trylock(&audio->get_event_lock)) {
1347 rc = audmp3_process_event_req(audio,
1348 (void __user *) arg);
1349 mutex_unlock(&audio->get_event_lock);
1350 } else
1351 rc = -EBUSY;
1352 return rc;
1353 }
1354
1355 if (cmd == AUDIO_ABORT_GET_EVENT) {
1356 audio->event_abort = 1;
1357 wake_up(&audio->event_wait);
1358 return 0;
1359 }
1360
1361 mutex_lock(&audio->lock);
1362 switch (cmd) {
1363 case AUDIO_START:
1364 MM_DBG("AUDIO_START\n");
1365 rc = audio_enable(audio);
1366 if (!rc) {
1367 rc = wait_event_interruptible_timeout(audio->wait,
1368 audio->dec_state != MSM_AUD_DECODER_STATE_NONE,
1369 msecs_to_jiffies(MSM_AUD_DECODER_WAIT_MS));
1370 MM_INFO("dec_state %d rc = %d\n", audio->dec_state, rc);
1371
1372 if (audio->dec_state != MSM_AUD_DECODER_STATE_SUCCESS)
1373 rc = -ENODEV;
1374 else
1375 rc = 0;
1376 }
1377 break;
1378 case AUDIO_STOP:
1379 MM_DBG("AUDIO_STOP\n");
1380 rc = audio_disable(audio);
1381 audio->stopped = 1;
1382 audio_ioport_reset(audio);
1383 audio->stopped = 0;
1384 break;
1385 case AUDIO_FLUSH:
1386 MM_DBG("AUDIO_FLUSH\n");
1387 audio->rflush = 1;
1388 audio->wflush = 1;
1389 audio_ioport_reset(audio);
1390 if (audio->running) {
1391 audpp_flush(audio->dec_id);
1392 rc = wait_event_interruptible(audio->write_wait,
1393 !audio->wflush);
1394 if (rc < 0) {
1395 MM_ERR("AUDIO_FLUSH interrupted\n");
1396 rc = -EINTR;
1397 }
1398 } else {
1399 audio->rflush = 0;
1400 audio->wflush = 0;
1401 }
1402 break;
1403 case AUDIO_OUTPORT_FLUSH:
1404 MM_DBG("AUDIO_OUTPORT_FLUSH\n");
1405 audio->rflush = 1;
1406 if (audio->drv_status & ADRV_STATUS_AIO_INTF) {
1407 audio->drv_ops.in_flush(audio);
1408 } else {
1409 wake_up(&audio->read_wait);
1410 mutex_lock(&audio->read_lock);
1411 audio->drv_ops.in_flush(audio);
1412 mutex_unlock(&audio->read_lock);
1413 }
1414 audplay_outport_flush(audio);
1415 rc = wait_event_interruptible(audio->read_wait,
1416 !audio->rflush);
1417 if (rc < 0) {
1418 MM_ERR("AUDPLAY_OUTPORT_FLUSH interrupted\n");
1419 rc = -EINTR;
1420 }
1421 break;
1422 case AUDIO_SET_CONFIG: {
1423 struct msm_audio_config config;
1424 if (copy_from_user(&config, (void *) arg, sizeof(config))) {
1425 rc = -EFAULT;
1426 break;
1427 }
1428 if (config.channel_count == 1) {
1429 config.channel_count = AUDPP_CMD_PCM_INTF_MONO_V;
1430 } else if (config.channel_count == 2) {
1431 config.channel_count = AUDPP_CMD_PCM_INTF_STEREO_V;
1432 } else {
1433 rc = -EINVAL;
1434 break;
1435 }
1436 audio->mfield = config.meta_field;
1437 audio->out_sample_rate = config.sample_rate;
1438 audio->out_channel_mode = config.channel_count;
1439 rc = 0;
1440 break;
1441 }
1442 case AUDIO_GET_CONFIG: {
1443 struct msm_audio_config config;
1444 config.buffer_size = (audio->out_dma_sz >> 1);
1445 config.buffer_count = 2;
1446 config.sample_rate = audio->out_sample_rate;
1447 if (audio->out_channel_mode == AUDPP_CMD_PCM_INTF_MONO_V)
1448 config.channel_count = 1;
1449 else
1450 config.channel_count = 2;
1451 config.meta_field = 0;
1452 config.unused[0] = 0;
1453 config.unused[1] = 0;
1454 config.unused[2] = 0;
1455 if (copy_to_user((void *) arg, &config, sizeof(config)))
1456 rc = -EFAULT;
1457 else
1458 rc = 0;
1459 break;
1460 }
1461 case AUDIO_GET_PCM_CONFIG:{
1462 struct msm_audio_pcm_config config;
1463 config.pcm_feedback = audio->pcm_feedback;
1464 config.buffer_count = PCM_BUF_MAX_COUNT;
1465 config.buffer_size = PCM_BUFSZ_MIN;
1466 if (copy_to_user((void *)arg, &config,
1467 sizeof(config)))
1468 rc = -EFAULT;
1469 else
1470 rc = 0;
1471 break;
1472 }
1473 case AUDIO_SET_PCM_CONFIG:{
1474 struct msm_audio_pcm_config config;
1475 if (copy_from_user
1476 (&config, (void *)arg, sizeof(config))) {
1477 rc = -EFAULT;
1478 break;
1479 }
1480
1481 if (config.pcm_feedback != audio->pcm_feedback) {
1482 MM_ERR("Not sufficient permission to"
1483 "change the playback mode\n");
1484 rc = -EACCES;
1485 break;
1486 }
1487 if (audio->drv_status & ADRV_STATUS_AIO_INTF) {
1488 rc = 0;
1489 break;
1490 }
1491
1492 if ((config.buffer_count > PCM_BUF_MAX_COUNT) ||
1493 (config.buffer_count == 1))
1494 config.buffer_count = PCM_BUF_MAX_COUNT;
1495
1496 if (config.buffer_size < PCM_BUFSZ_MIN)
1497 config.buffer_size = PCM_BUFSZ_MIN;
1498
1499 /* Check if pcm feedback is required */
1500 if ((config.pcm_feedback) && (!audio->read_data)) {
1501 MM_DBG("allocate PCM buffer %d\n",
1502 config.buffer_count *
1503 config.buffer_size);
Santosh Mardifdc227a2011-07-11 17:20:34 +05301504 audio->read_phys =
1505 allocate_contiguous_ebi_nomap(
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001506 config.buffer_size *
1507 config.buffer_count,
Santosh Mardifdc227a2011-07-11 17:20:34 +05301508 SZ_4K);
1509 if (!audio->read_phys) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001510 rc = -ENOMEM;
1511 break;
1512 }
Laura Abbott61399692012-04-30 14:25:46 -07001513 audio->map_v_read = ioremap(
Santosh Mardifdc227a2011-07-11 17:20:34 +05301514 audio->read_phys,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001515 config.buffer_size *
Laura Abbott61399692012-04-30 14:25:46 -07001516 config.buffer_count);
Santosh Mardifdc227a2011-07-11 17:20:34 +05301517 if (IS_ERR(audio->map_v_read)) {
1518 MM_ERR("failed to map read buffer"
1519 " physical address\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001520 rc = -ENOMEM;
Santosh Mardifdc227a2011-07-11 17:20:34 +05301521 free_contiguous_memory_by_paddr(
1522 audio->read_phys);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001523 } else {
1524 uint8_t index;
1525 uint32_t offset = 0;
Santosh Mardifdc227a2011-07-11 17:20:34 +05301526 audio->read_data =
Laura Abbott61399692012-04-30 14:25:46 -07001527 audio->map_v_read;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001528 audio->buf_refresh = 0;
1529 audio->pcm_buf_count =
1530 config.buffer_count;
1531 audio->read_next = 0;
1532 audio->fill_next = 0;
1533
1534 for (index = 0;
1535 index < config.buffer_count;
1536 index++) {
1537 audio->in[index].data =
1538 audio->read_data + offset;
1539 audio->in[index].addr =
1540 audio->read_phys + offset;
1541 audio->in[index].size =
1542 config.buffer_size;
1543 audio->in[index].used = 0;
1544 offset += config.buffer_size;
1545 }
1546 rc = 0;
1547 MM_DBG("read buf: phy addr \
1548 0x%08x kernel addr 0x%08x\n",
1549 audio->read_phys,
1550 (int)audio->read_data);
1551 }
1552 } else {
1553 rc = 0;
1554 }
1555 break;
1556 }
1557 case AUDIO_PAUSE:
1558 MM_DBG("AUDIO_PAUSE %ld\n", arg);
1559 rc = audpp_pause(audio->dec_id, (int) arg);
1560 break;
1561
1562 case AUDIO_GET_STREAM_INFO:{
1563 if (audio->stream_info.sample_rate == 0) {
1564 /* haven't received DSP stream event,
1565 the stream info is not updated */
1566 rc = -EPERM;
1567 break;
1568 }
1569 if (copy_to_user((void *)arg, &audio->stream_info,
1570 sizeof(struct msm_audio_bitstream_info)))
1571 rc = -EFAULT;
1572 else
1573 rc = 0;
1574 break;
1575 }
1576 case AUDIO_GET_BITSTREAM_ERROR_INFO:{
1577 if ((audio->bitstream_error_info.err_msg_indicator &
1578 AUDPLAY_STREAM_INFO_MSG_MASK) ==
1579 AUDPLAY_STREAM_INFO_MSG_MASK) {
1580 /* haven't received bitstream error info event,
1581 the bitstream error info is not updated */
1582 rc = -EPERM;
1583 break;
1584 }
1585 if (copy_to_user((void *)arg, &audio->bitstream_error_info,
1586 sizeof(struct msm_audio_bitstream_error_info)))
1587 rc = -EFAULT;
1588 else
1589 rc = 0;
1590 break;
1591 }
1592
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001593 case AUDIO_ASYNC_WRITE:
1594 if (audio->drv_status & ADRV_STATUS_FSYNC)
1595 rc = -EBUSY;
1596 else
1597 rc = audmp3_aio_buf_add(audio, 1, (void __user *) arg);
1598 break;
1599
1600 case AUDIO_ASYNC_READ:
1601 if (audio->pcm_feedback)
1602 rc = audmp3_aio_buf_add(audio, 0, (void __user *) arg);
1603 else
1604 rc = -EPERM;
1605 break;
1606 case AUDIO_GET_SESSION_ID:
1607 if (copy_to_user((void *) arg, &audio->dec_id,
1608 sizeof(unsigned short)))
1609 rc = -EFAULT;
1610 else
1611 rc = 0;
1612 break;
1613 case AUDIO_SET_ERR_THRESHOLD_VALUE:
1614 if (copy_from_user(&audio->bitstream_error_threshold_value,
1615 (void *)arg, sizeof(uint32_t)))
1616 rc = -EFAULT;
1617 else
1618 rc = 0;
1619 break;
1620 default:
1621 rc = -EINVAL;
1622 }
1623 mutex_unlock(&audio->lock);
1624 return rc;
1625}
1626
1627/* Only useful in tunnel-mode */
1628int audmp3_async_fsync(struct audio *audio)
1629{
1630 int rc = 0;
1631
1632 MM_DBG("\n"); /* Macro prints the file name and function */
1633
1634 /* Blocking client sends more data */
1635 mutex_lock(&audio->lock);
1636 audio->drv_status |= ADRV_STATUS_FSYNC;
1637 mutex_unlock(&audio->lock);
1638
1639 mutex_lock(&audio->write_lock);
1640 /* pcm dmamiss message is sent continously
1641 * when decoder is starved so no race
1642 * condition concern
1643 */
1644 audio->teos = 0;
1645
1646 rc = wait_event_interruptible(audio->write_wait,
1647 (audio->teos && audio->out_needed &&
1648 list_empty(&audio->out_queue))
1649 || audio->wflush || audio->stopped);
1650
1651 if (audio->stopped || audio->wflush)
1652 rc = -EBUSY;
1653
1654 mutex_unlock(&audio->write_lock);
1655 mutex_lock(&audio->lock);
1656 audio->drv_status &= ~ADRV_STATUS_FSYNC;
1657 mutex_unlock(&audio->lock);
1658
1659 return rc;
1660}
1661
1662int audmp3_sync_fsync(struct audio *audio)
1663{
1664 struct buffer *frame;
1665 int rc = 0;
1666
1667 MM_DBG("\n"); /* Macro prints the file name and function */
1668
1669 mutex_lock(&audio->write_lock);
1670
1671 rc = wait_event_interruptible(audio->write_wait,
1672 (!audio->out[0].used &&
1673 !audio->out[1].used &&
1674 audio->out_needed) || audio->wflush);
1675
1676 if (rc < 0)
1677 goto done;
1678 else if (audio->wflush) {
1679 rc = -EBUSY;
1680 goto done;
1681 }
1682
1683 if (audio->reserved) {
1684 MM_DBG("send reserved byte\n");
1685 frame = audio->out + audio->out_tail;
1686 ((char *) frame->data)[0] = audio->rsv_byte;
1687 ((char *) frame->data)[1] = 0;
1688 frame->used = 2;
1689 audio->drv_ops.send_data(audio, 0);
1690
1691 rc = wait_event_interruptible(audio->write_wait,
1692 (!audio->out[0].used &&
1693 !audio->out[1].used &&
1694 audio->out_needed) || audio->wflush);
1695
1696 if (rc < 0)
1697 goto done;
1698 else if (audio->wflush) {
1699 rc = -EBUSY;
1700 goto done;
1701 }
1702 }
1703
1704 /* pcm dmamiss message is sent continously
1705 * when decoder is starved so no race
1706 * condition concern
1707 */
1708 audio->teos = 0;
1709
1710 rc = wait_event_interruptible(audio->write_wait,
1711 audio->teos || audio->wflush);
1712
1713 if (audio->wflush)
1714 rc = -EBUSY;
1715
1716done:
1717 mutex_unlock(&audio->write_lock);
1718 return rc;
1719}
1720
Steve Mucklef132c6c2012-06-06 18:30:57 -07001721int audmp3_fsync(struct file *file, loff_t ppos1, loff_t ppos2, int datasync)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001722{
1723 struct audio *audio = file->private_data;
1724
1725 if (!audio->running || audio->pcm_feedback)
1726 return -EINVAL;
1727
1728 return audio->drv_ops.fsync(audio);
1729}
1730
1731static ssize_t audio_read(struct file *file, char __user *buf, size_t count,
1732 loff_t *pos)
1733{
1734 struct audio *audio = file->private_data;
1735 const char __user *start = buf;
1736 int rc = 0;
1737
1738 if (audio->drv_status & ADRV_STATUS_AIO_INTF)
1739 return -EPERM;
1740 else if (!audio->pcm_feedback)
1741 return 0; /* PCM feedback disabled. Nothing to read */
1742
1743 mutex_lock(&audio->read_lock);
1744 MM_DBG("%d \n", count);
1745 while (count > 0) {
1746 rc = wait_event_interruptible_timeout(
1747 audio->read_wait,
1748 (audio->in[audio->read_next].
1749 used > 0) || (audio->stopped)
1750 || (audio->rflush),
1751 msecs_to_jiffies(MSM_AUD_BUFFER_UPDATE_WAIT_MS));
1752
1753 if (rc == 0) {
1754 rc = -ETIMEDOUT;
1755 break;
1756 } else if (rc < 0)
1757 break;
1758
1759 if (audio->stopped || audio->rflush) {
1760 rc = -EBUSY;
1761 break;
1762 }
1763
1764 if (count < audio->in[audio->read_next].used) {
1765 /* Read must happen in frame boundary. Since
1766 * driver does not know frame size, read count
1767 * must be greater or equal
1768 * to size of PCM samples
1769 */
1770 MM_DBG("no partial frame done reading\n");
1771 break;
1772 } else {
1773 MM_DBG("read from in[%d]\n", audio->read_next);
1774
1775 if (copy_to_user
1776 (buf, audio->in[audio->read_next].data,
1777 audio->in[audio->read_next].used)) {
1778 MM_ERR("invalid addr %x \n", (unsigned int)buf);
1779 rc = -EFAULT;
1780 break;
1781 }
1782 count -= audio->in[audio->read_next].used;
1783 buf += audio->in[audio->read_next].used;
1784 audio->in[audio->read_next].used = 0;
1785 if ((++audio->read_next) == audio->pcm_buf_count)
1786 audio->read_next = 0;
1787 break; /* Force to exit while loop
1788 * to prevent output thread
1789 * sleep too long if data is
1790 * not ready at this moment.
1791 */
1792 }
1793 }
1794
1795 /* don't feed output buffer to HW decoder during flushing
1796 * buffer refresh command will be sent once flush completes
1797 * send buf refresh command here can confuse HW decoder
1798 */
1799 if (audio->buf_refresh && !audio->rflush) {
1800 audio->buf_refresh = 0;
1801 MM_DBG("kick start pcm feedback again\n");
1802 audio->drv_ops.buffer_refresh(audio);
1803 }
1804
1805 mutex_unlock(&audio->read_lock);
1806
1807 if (buf > start)
1808 rc = buf - start;
1809
1810 MM_DBG("read %d bytes\n", rc);
1811 return rc;
1812}
1813
1814static int audmp3_process_eos(struct audio *audio,
1815 const char __user *buf_start, unsigned short mfield_size)
1816{
1817 int rc = 0;
1818 struct buffer *frame;
1819 char *buf_ptr;
1820
1821 if (audio->reserved) {
1822 MM_DBG("flush reserve byte\n");
1823 frame = audio->out + audio->out_head;
1824 buf_ptr = frame->data;
1825 rc = wait_event_interruptible(audio->write_wait,
1826 (frame->used == 0)
1827 || (audio->stopped)
1828 || (audio->wflush));
1829 if (rc < 0)
1830 goto done;
1831 if (audio->stopped || audio->wflush) {
1832 rc = -EBUSY;
1833 goto done;
1834 }
1835
1836 buf_ptr[0] = audio->rsv_byte;
1837 buf_ptr[1] = 0;
1838 audio->out_head ^= 1;
1839 frame->mfield_sz = 0;
1840 frame->used = 2;
1841 audio->reserved = 0;
1842 audio->drv_ops.send_data(audio, 0);
1843 }
1844
1845 frame = audio->out + audio->out_head;
1846
1847 rc = wait_event_interruptible(audio->write_wait,
1848 (audio->out_needed &&
1849 audio->out[0].used == 0 &&
1850 audio->out[1].used == 0)
1851 || (audio->stopped)
1852 || (audio->wflush));
1853
1854 if (rc < 0)
1855 goto done;
1856 if (audio->stopped || audio->wflush) {
1857 rc = -EBUSY;
1858 goto done;
1859 }
1860
1861 if (copy_from_user(frame->data, buf_start, mfield_size)) {
1862 rc = -EFAULT;
1863 goto done;
1864 }
1865
1866 frame->mfield_sz = mfield_size;
1867 audio->out_head ^= 1;
1868 frame->used = mfield_size;
1869 audio->drv_ops.send_data(audio, 0);
1870done:
1871 return rc;
1872}
1873
1874static ssize_t audio_write(struct file *file, const char __user *buf,
1875 size_t count, loff_t *pos)
1876{
1877 struct audio *audio = file->private_data;
1878 const char __user *start = buf;
1879 struct buffer *frame;
1880 size_t xfer;
1881 char *cpy_ptr;
1882 int rc = 0, eos_condition = AUDMP3_EOS_NONE;
1883 unsigned dsize;
1884 unsigned short mfield_size = 0;
1885
1886 if (audio->drv_status & ADRV_STATUS_AIO_INTF)
1887 return -EPERM;
1888
1889 MM_DBG("cnt=%d\n", count);
1890
1891 mutex_lock(&audio->write_lock);
1892 while (count > 0) {
1893 frame = audio->out + audio->out_head;
1894 cpy_ptr = frame->data;
1895 dsize = 0;
1896 rc = wait_event_interruptible(audio->write_wait,
1897 (frame->used == 0)
1898 || (audio->stopped)
1899 || (audio->wflush));
1900 if (rc < 0)
1901 break;
1902 if (audio->stopped || audio->wflush) {
1903 rc = -EBUSY;
1904 break;
1905 }
1906 if (audio->mfield) {
1907 if (buf == start) {
1908 /* Processing beginning of user buffer */
1909 if (__get_user(mfield_size,
1910 (unsigned short __user *) buf)) {
1911 rc = -EFAULT;
1912 break;
1913 } else if (mfield_size > count) {
1914 rc = -EINVAL;
1915 break;
1916 }
1917 MM_DBG("mf offset_val %x\n", mfield_size);
1918 if (copy_from_user(cpy_ptr, buf, mfield_size)) {
1919 rc = -EFAULT;
1920 break;
1921 }
1922 /* Check if EOS flag is set and buffer has
1923 * contains just meta field
1924 */
1925 if (cpy_ptr[AUDMP3_EOS_FLG_OFFSET] &
1926 AUDMP3_EOS_FLG_MASK) {
1927 MM_DBG("EOS SET\n");
1928 eos_condition = AUDMP3_EOS_SET;
1929 if (mfield_size == count) {
1930 buf += mfield_size;
1931 break;
1932 } else
1933 cpy_ptr[AUDMP3_EOS_FLG_OFFSET]
1934 &= ~AUDMP3_EOS_FLG_MASK;
1935 }
1936 cpy_ptr += mfield_size;
1937 count -= mfield_size;
1938 dsize += mfield_size;
1939 buf += mfield_size;
1940 } else {
1941 mfield_size = 0;
1942 MM_DBG("continuous buffer\n");
1943 }
1944 frame->mfield_sz = mfield_size;
1945 }
1946
1947 if (audio->reserved) {
1948 MM_DBG("append reserved byte %x\n", audio->rsv_byte);
1949 *cpy_ptr = audio->rsv_byte;
1950 xfer = (count > ((frame->size - mfield_size) - 1)) ?
1951 (frame->size - mfield_size) - 1 : count;
1952 cpy_ptr++;
1953 dsize += 1;
1954 audio->reserved = 0;
1955 } else
1956 xfer = (count > (frame->size - mfield_size)) ?
1957 (frame->size - mfield_size) : count;
1958
1959 if (copy_from_user(cpy_ptr, buf, xfer)) {
1960 rc = -EFAULT;
1961 break;
1962 }
1963
1964 dsize += xfer;
1965 if (dsize & 1) {
1966 audio->rsv_byte = ((char *) frame->data)[dsize - 1];
1967 MM_DBG("odd length buf reserve last byte %x\n",
1968 audio->rsv_byte);
1969 audio->reserved = 1;
1970 dsize--;
1971 }
1972 count -= xfer;
1973 buf += xfer;
1974
1975 if (dsize > 0) {
1976 audio->out_head ^= 1;
1977 frame->used = dsize;
1978 audio->drv_ops.send_data(audio, 0);
1979 }
1980 }
1981 if (eos_condition == AUDMP3_EOS_SET)
1982 rc = audmp3_process_eos(audio, start, mfield_size);
1983 mutex_unlock(&audio->write_lock);
1984 if (!rc) {
1985 if (buf > start)
1986 return buf - start;
1987 }
1988 return rc;
1989}
1990
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001991static int audio_release(struct inode *inode, struct file *file)
1992{
1993 struct audio *audio = file->private_data;
1994
1995 MM_INFO("audio instance 0x%08x freeing\n", (int)audio);
1996 mutex_lock(&audio->lock);
1997 auddev_unregister_evt_listner(AUDDEV_CLNT_DEC, audio->dec_id);
1998 audio_disable(audio);
1999 audio->drv_ops.out_flush(audio);
2000 audio->drv_ops.in_flush(audio);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002001
2002 msm_adsp_put(audio->audplay);
2003 audpp_adec_free(audio->dec_id);
2004#ifdef CONFIG_HAS_EARLYSUSPEND
2005 unregister_early_suspend(&audio->suspend_ctl.node);
2006#endif
2007 audio->opened = 0;
2008 audio->event_abort = 1;
2009 wake_up(&audio->event_wait);
2010 audmp3_reset_event_queue(audio);
2011 if (audio->data) {
Laura Abbott61399692012-04-30 14:25:46 -07002012 iounmap(audio->map_v_write);
Santosh Mardifdc227a2011-07-11 17:20:34 +05302013 free_contiguous_memory_by_paddr(audio->phys);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002014 }
2015 if (audio->read_data) {
Laura Abbott61399692012-04-30 14:25:46 -07002016 iounmap(audio->map_v_read);
Santosh Mardifdc227a2011-07-11 17:20:34 +05302017 free_contiguous_memory_by_paddr(audio->read_phys);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002018 }
2019 mutex_unlock(&audio->lock);
2020#ifdef CONFIG_DEBUG_FS
2021 if (audio->dentry)
2022 debugfs_remove(audio->dentry);
2023#endif
2024 kfree(audio);
2025 return 0;
2026}
2027
2028static void audmp3_post_event(struct audio *audio, int type,
2029 union msm_audio_event_payload payload)
2030{
2031 struct audmp3_event *e_node = NULL;
2032 unsigned long flags;
2033
2034 spin_lock_irqsave(&audio->event_queue_lock, flags);
2035
2036 if (!list_empty(&audio->free_event_queue)) {
2037 e_node = list_first_entry(&audio->free_event_queue,
2038 struct audmp3_event, list);
2039 list_del(&e_node->list);
2040 } else {
2041 e_node = kmalloc(sizeof(struct audmp3_event), GFP_ATOMIC);
2042 if (!e_node) {
2043 MM_ERR("No mem to post event %d\n", type);
2044 return;
2045 }
2046 }
2047
2048 e_node->event_type = type;
2049 e_node->payload = payload;
2050
2051 list_add_tail(&e_node->list, &audio->event_queue);
2052 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
2053 wake_up(&audio->event_wait);
2054}
2055
2056#ifdef CONFIG_HAS_EARLYSUSPEND
2057static void audmp3_suspend(struct early_suspend *h)
2058{
2059 struct audmp3_suspend_ctl *ctl =
2060 container_of(h, struct audmp3_suspend_ctl, node);
2061 union msm_audio_event_payload payload;
2062
2063 MM_DBG("\n"); /* Macro prints the file name and function */
2064 audmp3_post_event(ctl->audio, AUDIO_EVENT_SUSPEND, payload);
2065}
2066
2067static void audmp3_resume(struct early_suspend *h)
2068{
2069 struct audmp3_suspend_ctl *ctl =
2070 container_of(h, struct audmp3_suspend_ctl, node);
2071 union msm_audio_event_payload payload;
2072
2073 MM_DBG("\n"); /* Macro prints the file name and function */
2074 audmp3_post_event(ctl->audio, AUDIO_EVENT_RESUME, payload);
2075}
2076#endif
2077
2078#ifdef CONFIG_DEBUG_FS
2079static ssize_t audmp3_debug_open(struct inode *inode, struct file *file)
2080{
2081 file->private_data = inode->i_private;
2082 return 0;
2083}
2084
2085static ssize_t audmp3_debug_read(struct file *file, char __user *buf,
2086 size_t count, loff_t *ppos)
2087{
2088 const int debug_bufmax = 4096;
2089 static char buffer[4096];
2090 int n = 0, i;
2091 struct audio *audio = file->private_data;
2092
2093 mutex_lock(&audio->lock);
2094 n = scnprintf(buffer, debug_bufmax, "opened %d\n", audio->opened);
2095 n += scnprintf(buffer + n, debug_bufmax - n,
2096 "enabled %d\n", audio->enabled);
2097 n += scnprintf(buffer + n, debug_bufmax - n,
2098 "stopped %d\n", audio->stopped);
2099 n += scnprintf(buffer + n, debug_bufmax - n,
2100 "pcm_feedback %d\n", audio->pcm_feedback);
2101 n += scnprintf(buffer + n, debug_bufmax - n,
2102 "out_buf_sz %d\n", audio->out[0].size);
2103 n += scnprintf(buffer + n, debug_bufmax - n,
2104 "pcm_buf_count %d \n", audio->pcm_buf_count);
2105 n += scnprintf(buffer + n, debug_bufmax - n,
2106 "pcm_buf_sz %d \n", audio->in[0].size);
2107 n += scnprintf(buffer + n, debug_bufmax - n,
2108 "volume %x \n", audio->vol_pan.volume);
2109 n += scnprintf(buffer + n, debug_bufmax - n,
2110 "sample rate %d \n", audio->out_sample_rate);
2111 n += scnprintf(buffer + n, debug_bufmax - n,
2112 "channel mode %d \n", audio->out_channel_mode);
2113 mutex_unlock(&audio->lock);
2114 /* Following variables are only useful for debugging when
2115 * when playback halts unexpectedly. Thus, no mutual exclusion
2116 * enforced
2117 */
2118 n += scnprintf(buffer + n, debug_bufmax - n,
2119 "wflush %d\n", audio->wflush);
2120 n += scnprintf(buffer + n, debug_bufmax - n,
2121 "rflush %d\n", audio->rflush);
2122 n += scnprintf(buffer + n, debug_bufmax - n,
2123 "running %d \n", audio->running);
2124 n += scnprintf(buffer + n, debug_bufmax - n,
2125 "dec state %d \n", audio->dec_state);
2126 n += scnprintf(buffer + n, debug_bufmax - n,
2127 "out_needed %d \n", audio->out_needed);
2128 n += scnprintf(buffer + n, debug_bufmax - n,
2129 "out_head %d \n", audio->out_head);
2130 n += scnprintf(buffer + n, debug_bufmax - n,
2131 "out_tail %d \n", audio->out_tail);
2132 n += scnprintf(buffer + n, debug_bufmax - n,
2133 "out[0].used %d \n", audio->out[0].used);
2134 n += scnprintf(buffer + n, debug_bufmax - n,
2135 "out[1].used %d \n", audio->out[1].used);
2136 n += scnprintf(buffer + n, debug_bufmax - n,
2137 "buffer_refresh %d \n", audio->buf_refresh);
2138 n += scnprintf(buffer + n, debug_bufmax - n,
2139 "read_next %d \n", audio->read_next);
2140 n += scnprintf(buffer + n, debug_bufmax - n,
2141 "fill_next %d \n", audio->fill_next);
2142 for (i = 0; i < audio->pcm_buf_count; i++)
2143 n += scnprintf(buffer + n, debug_bufmax - n,
2144 "in[%d].size %d \n", i, audio->in[i].used);
2145 buffer[n] = 0;
2146 return simple_read_from_buffer(buf, count, ppos, buffer, n);
2147}
2148
2149static const struct file_operations audmp3_debug_fops = {
2150 .read = audmp3_debug_read,
2151 .open = audmp3_debug_open,
2152};
2153#endif
2154
2155static int audio_open(struct inode *inode, struct file *file)
2156{
2157
2158 struct audio *audio = NULL;
2159 int rc, i, dec_attrb, decid;
2160 struct audmp3_event *e_node = NULL;
2161 unsigned pmem_sz = DMASZ_MAX;
2162#ifdef CONFIG_DEBUG_FS
2163 /* 4 bytes represents decoder number, 1 byte for terminate string */
2164 char name[sizeof "msm_mp3_" + 5];
2165#endif
2166
2167 /* Allocate audio instance, set to zero */
2168 audio = kzalloc(sizeof(struct audio), GFP_KERNEL);
2169 if (!audio) {
2170 MM_ERR("no memory to allocate audio instance \n");
2171 rc = -ENOMEM;
2172 goto done;
2173 }
2174 MM_INFO("audio instance 0x%08x created\n", (int)audio);
2175
2176 /* Allocate the decoder */
2177 dec_attrb = AUDDEC_DEC_MP3;
2178 if ((file->f_mode & FMODE_WRITE) &&
2179 (file->f_mode & FMODE_READ)) {
2180 dec_attrb |= MSM_AUD_MODE_NONTUNNEL;
2181 audio->pcm_feedback = NON_TUNNEL_MODE_PLAYBACK;
2182 } else if ((file->f_mode & FMODE_WRITE) &&
2183 !(file->f_mode & FMODE_READ)) {
2184 dec_attrb |= MSM_AUD_MODE_TUNNEL;
2185 audio->pcm_feedback = TUNNEL_MODE_PLAYBACK;
2186 } else {
2187 kfree(audio);
2188 rc = -EACCES;
2189 goto done;
2190 }
2191
2192 decid = audpp_adec_alloc(dec_attrb, &audio->module_name,
2193 &audio->queue_id);
2194 if (decid < 0) {
2195 MM_ERR("No free decoder available, freeing instance 0x%08x\n",
2196 (int)audio);
2197 rc = -ENODEV;
2198 kfree(audio);
2199 goto done;
2200 }
2201 audio->dec_id = decid & MSM_AUD_DECODER_MASK;
2202
2203 /* AIO interface */
2204 if (file->f_flags & O_NONBLOCK) {
Santosh Mardifdc227a2011-07-11 17:20:34 +05302205 MM_DBG("set to aio interface\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002206 audio->drv_status |= ADRV_STATUS_AIO_INTF;
2207 audio->drv_ops.pcm_buf_update = audmp3_async_pcm_buf_update;
2208 audio->drv_ops.buffer_refresh = audmp3_async_buffer_refresh;
2209 audio->drv_ops.send_data = audmp3_async_send_data;
2210 audio->drv_ops.out_flush = audmp3_async_flush;
2211 audio->drv_ops.in_flush = audmp3_async_flush_pcm_buf;
2212 audio->drv_ops.fsync = audmp3_async_fsync;
2213 } else {
Santosh Mardifdc227a2011-07-11 17:20:34 +05302214 MM_DBG("set to std io interface\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002215 while (pmem_sz >= DMASZ_MIN) {
Santosh Mardifdc227a2011-07-11 17:20:34 +05302216 MM_DBG("pmemsz = %d\n", pmem_sz);
2217 audio->phys = allocate_contiguous_ebi_nomap(pmem_sz,
2218 SZ_4K);
2219 if (audio->phys) {
Laura Abbott61399692012-04-30 14:25:46 -07002220 audio->map_v_write = ioremap(
2221 audio->phys, pmem_sz);
Santosh Mardifdc227a2011-07-11 17:20:34 +05302222 if (IS_ERR(audio->map_v_write)) {
2223 MM_ERR("failed to map write physical"
2224 " address , freeing instance"
2225 "0x%08x\n", (int)audio);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002226 rc = -ENOMEM;
Santosh Mardifdc227a2011-07-11 17:20:34 +05302227 free_contiguous_memory_by_paddr(
2228 audio->phys);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002229 audpp_adec_free(audio->dec_id);
2230 kfree(audio);
2231 goto done;
2232 }
Laura Abbott61399692012-04-30 14:25:46 -07002233 audio->data = audio->map_v_write;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002234 MM_DBG("write buf: phy addr 0x%08x kernel addr\
2235 0x%08x\n", audio->phys,\
2236 (int)audio->data);
2237 break;
2238 } else if (pmem_sz == DMASZ_MIN) {
2239 MM_ERR("could not allocate write buffers, \
2240 freeing instance 0x%08x\n",
2241 (int)audio);
2242 rc = -ENOMEM;
2243 audpp_adec_free(audio->dec_id);
2244 kfree(audio);
2245 goto done;
2246 } else
2247 pmem_sz >>= 1;
2248 }
2249 audio->out_dma_sz = pmem_sz;
2250 audio->drv_ops.pcm_buf_update = audio_update_pcm_buf_entry;
2251 audio->drv_ops.buffer_refresh = audplay_buffer_refresh;
2252 audio->drv_ops.send_data = audplay_send_data;
2253 audio->drv_ops.out_flush = audio_flush;
2254 audio->drv_ops.in_flush = audio_flush_pcm_buf;
2255 audio->drv_ops.fsync = audmp3_sync_fsync;
2256 audio->out[0].data = audio->data + 0;
2257 audio->out[0].addr = audio->phys + 0;
2258 audio->out[0].size = (audio->out_dma_sz >> 1);
2259
2260 audio->out[1].data = audio->data + audio->out[0].size;
2261 audio->out[1].addr = audio->phys + audio->out[0].size;
2262 audio->out[1].size = audio->out[0].size;
2263 }
2264
2265 rc = msm_adsp_get(audio->module_name, &audio->audplay,
2266 &audplay_adsp_ops, audio);
2267
2268 if (rc) {
2269 MM_ERR("failed to get %s module freeing instance 0x%08x\n",
2270 audio->module_name, (int)audio);
2271 goto err;
2272 }
2273
2274 /* Initialize all locks of audio instance */
2275 mutex_init(&audio->lock);
2276 mutex_init(&audio->write_lock);
2277 mutex_init(&audio->read_lock);
2278 mutex_init(&audio->get_event_lock);
2279 spin_lock_init(&audio->dsp_lock);
2280 init_waitqueue_head(&audio->write_wait);
2281 init_waitqueue_head(&audio->read_wait);
2282 INIT_LIST_HEAD(&audio->out_queue);
2283 INIT_LIST_HEAD(&audio->in_queue);
2284 INIT_LIST_HEAD(&audio->pmem_region_queue);
2285 INIT_LIST_HEAD(&audio->free_event_queue);
2286 INIT_LIST_HEAD(&audio->event_queue);
2287 init_waitqueue_head(&audio->wait);
2288 init_waitqueue_head(&audio->event_wait);
2289 spin_lock_init(&audio->event_queue_lock);
2290 init_waitqueue_head(&audio->avsync_wait);
2291
2292 audio->out_sample_rate = 44100;
2293 audio->out_channel_mode = AUDPP_CMD_PCM_INTF_STEREO_V;
2294 audio->vol_pan.volume = 0x2000;
2295 audio->bitstream_error_threshold_value =
2296 BITSTREAM_ERROR_THRESHOLD_VALUE;
2297
2298 audio->drv_ops.out_flush(audio);
2299
2300 file->private_data = audio;
2301 audio->opened = 1;
2302
2303 audio->device_events = AUDDEV_EVT_DEV_RDY
2304 |AUDDEV_EVT_DEV_RLS |
2305 AUDDEV_EVT_STREAM_VOL_CHG;
2306
2307 rc = auddev_register_evt_listner(audio->device_events,
2308 AUDDEV_CLNT_DEC,
2309 audio->dec_id,
2310 mp3_listner,
2311 (void *)audio);
2312 if (rc) {
2313 MM_ERR("%s: failed to register listner\n", __func__);
2314 goto event_err;
2315 }
2316
2317#ifdef CONFIG_DEBUG_FS
2318 snprintf(name, sizeof name, "msm_mp3_%04x", audio->dec_id);
2319 audio->dentry = debugfs_create_file(name, S_IFREG | S_IRUGO,
2320 NULL, (void *) audio, &audmp3_debug_fops);
2321
2322 if (IS_ERR(audio->dentry))
2323 MM_DBG("debugfs_create_file failed\n");
2324#endif
2325#ifdef CONFIG_HAS_EARLYSUSPEND
2326 audio->suspend_ctl.node.level = EARLY_SUSPEND_LEVEL_DISABLE_FB;
2327 audio->suspend_ctl.node.resume = audmp3_resume;
2328 audio->suspend_ctl.node.suspend = audmp3_suspend;
2329 audio->suspend_ctl.audio = audio;
2330 register_early_suspend(&audio->suspend_ctl.node);
2331#endif
2332 for (i = 0; i < AUDMP3_EVENT_NUM; i++) {
2333 e_node = kmalloc(sizeof(struct audmp3_event), GFP_KERNEL);
2334 if (e_node)
2335 list_add_tail(&e_node->list, &audio->free_event_queue);
2336 else {
2337 MM_ERR("event pkt alloc failed\n");
2338 break;
2339 }
2340 }
2341 memset(&audio->stream_info, 0, sizeof(struct msm_audio_bitstream_info));
2342 memset(&audio->bitstream_error_info, 0,
2343 sizeof(struct msm_audio_bitstream_info));
2344done:
2345 return rc;
2346event_err:
2347 msm_adsp_put(audio->audplay);
2348err:
2349 if (audio->data) {
Laura Abbott61399692012-04-30 14:25:46 -07002350 iounmap(audio->map_v_write);
Santosh Mardifdc227a2011-07-11 17:20:34 +05302351 free_contiguous_memory_by_paddr(audio->phys);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002352 }
2353 audpp_adec_free(audio->dec_id);
2354 kfree(audio);
2355 return rc;
2356}
2357
2358static const struct file_operations audio_mp3_fops = {
2359 .owner = THIS_MODULE,
2360 .open = audio_open,
2361 .release = audio_release,
2362 .read = audio_read,
2363 .write = audio_write,
2364 .unlocked_ioctl = audio_ioctl,
2365 .fsync = audmp3_fsync,
2366};
2367
2368struct miscdevice audio_mp3_misc = {
2369 .minor = MISC_DYNAMIC_MINOR,
2370 .name = "msm_mp3",
2371 .fops = &audio_mp3_fops,
2372};
2373
2374static int __init audio_init(void)
2375{
2376 return misc_register(&audio_mp3_misc);
2377}
2378
2379static void __exit audio_exit(void)
2380{
2381 misc_deregister(&audio_mp3_misc);
2382}
2383
2384module_init(audio_init);
2385module_exit(audio_exit);
2386
2387MODULE_DESCRIPTION("MSM MP3 driver");
2388MODULE_LICENSE("GPL v2");