blob: 5cd95f6d45b853e7ce62892431d242ebe2946290 [file] [log] [blame]
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05301
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002/* audio_pcm.c - pcm audio decoder driver
3 *
Manish Dewangana4f1df02012-02-08 17:06:54 +05304 * Copyright (c) 2009-2012, Code Aurora Forum. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07005 *
6 * Based on the mp3 decoder driver in arch/arm/mach-msm/qdsp5/audio_mp3.c
7 *
8 * Copyright (C) 2008 Google, Inc.
9 * Copyright (C) 2008 HTC Corporation
10 *
11 * All source code in this file is licensed under the following license except
12 * where indicated.
13 *
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License version 2 as published
16 * by the Free Software Foundation.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
21 *
22 * See the GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, you can find it at http://www.fsf.org
25 */
26
Santosh Mardi0be3b8e2011-07-06 10:00:21 +053027#include <asm/atomic.h>
28#include <asm/ioctls.h>
29
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070030#include <linux/module.h>
31#include <linux/fs.h>
32#include <linux/miscdevice.h>
33#include <linux/uaccess.h>
34#include <linux/kthread.h>
35#include <linux/wait.h>
36#include <linux/dma-mapping.h>
37#include <linux/debugfs.h>
38#include <linux/delay.h>
39#include <linux/earlysuspend.h>
40#include <linux/list.h>
41#include <linux/android_pmem.h>
42#include <linux/slab.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070043#include <linux/msm_audio.h>
44
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070045
Santosh Mardi0be3b8e2011-07-06 10:00:21 +053046#include <mach/msm_adsp.h>
47#include <mach/iommu.h>
48#include <mach/iommu_domains.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070049#include <mach/qdsp5/qdsp5audppcmdi.h>
50#include <mach/qdsp5/qdsp5audppmsg.h>
51#include <mach/qdsp5/qdsp5audplaycmdi.h>
52#include <mach/qdsp5/qdsp5audplaymsg.h>
53#include <mach/qdsp5/qdsp5rmtcmdi.h>
54#include <mach/debug_mm.h>
Santosh Mardi0be3b8e2011-07-06 10:00:21 +053055#include <linux/memory_alloc.h>
56#include <mach/msm_memtypes.h>
57
58#include "audmgr.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070059
60/* for queue ids - should be relative to module number*/
61#include "adsp.h"
62
63#define ADRV_STATUS_AIO_INTF 0x00000001
64#define ADRV_STATUS_OBUF_GIVEN 0x00000002
65#define ADRV_STATUS_IBUF_GIVEN 0x00000004
66#define ADRV_STATUS_FSYNC 0x00000008
67
68/* Size must be power of 2 */
69#define BUFSZ_MAX 32768
70#define BUFSZ_MIN 4096
71#define DMASZ_MAX (BUFSZ_MAX * 2)
72#define DMASZ_MIN (BUFSZ_MIN * 2)
73
74#define AUDDEC_DEC_PCM 0
75
76/* Decoder status received from AUDPPTASK */
77#define AUDPP_DEC_STATUS_SLEEP 0
78#define AUDPP_DEC_STATUS_INIT 1
79#define AUDPP_DEC_STATUS_CFG 2
80#define AUDPP_DEC_STATUS_PLAY 3
81
82#define AUDPCM_EVENT_NUM 10 /* Default number of pre-allocated event packets */
83
84#define __CONTAINS(r, v, l) ({ \
85 typeof(r) __r = r; \
86 typeof(v) __v = v; \
87 typeof(v) __e = __v + l; \
88 int res = ((__v >= __r->vaddr) && \
89 (__e <= __r->vaddr + __r->len)); \
90 res; \
91})
92
93#define CONTAINS(r1, r2) ({ \
94 typeof(r2) __r2 = r2; \
95 __CONTAINS(r1, __r2->vaddr, __r2->len); \
96})
97
98#define IN_RANGE(r, v) ({ \
99 typeof(r) __r = r; \
100 typeof(v) __vv = v; \
101 int res = ((__vv >= __r->vaddr) && \
102 (__vv < (__r->vaddr + __r->len))); \
103 res; \
104})
105
106#define OVERLAPS(r1, r2) ({ \
107 typeof(r1) __r1 = r1; \
108 typeof(r2) __r2 = r2; \
109 typeof(__r2->vaddr) __v = __r2->vaddr; \
110 typeof(__v) __e = __v + __r2->len - 1; \
111 int res = (IN_RANGE(__r1, __v) || IN_RANGE(__r1, __e)); \
112 res; \
113})
114
115struct audio;
116
117struct buffer {
118 void *data;
119 unsigned size;
120 unsigned used; /* Input usage actual DSP produced PCM size */
121 unsigned addr;
122};
123
124#ifdef CONFIG_HAS_EARLYSUSPEND
125struct audpcm_suspend_ctl {
126 struct early_suspend node;
127 struct audio *audio;
128};
129#endif
130
131struct audpcm_event {
132 struct list_head list;
133 int event_type;
134 union msm_audio_event_payload payload;
135};
136
137struct audpcm_pmem_region {
138 struct list_head list;
139 struct file *file;
140 int fd;
141 void *vaddr_ref;
142 void *vaddr;
143 unsigned long paddr;
144 unsigned long kvaddr;
145 unsigned long len;
146 unsigned ref_cnt;
147};
148
149struct audpcm_buffer_node {
150 struct list_head list;
151 struct msm_audio_aio_buf buf;
152 unsigned long paddr;
153};
154
155struct audpcm_drv_operations {
156 void (*send_data)(struct audio *, unsigned);
157 void (*out_flush)(struct audio *);
158 int (*fsync)(struct audio *);
159};
160
161struct audio {
162 struct buffer out[2];
163
164 spinlock_t dsp_lock;
165
166 uint8_t out_head;
167 uint8_t out_tail;
168 uint8_t out_needed; /* number of buffers the dsp is waiting for */
169 unsigned out_dma_sz;
170 struct list_head out_queue; /* queue to retain output buffers */
171 atomic_t out_bytes;
172
173 struct mutex lock;
174 struct mutex write_lock;
175 wait_queue_head_t write_wait;
176
177 struct msm_adsp_module *audplay;
178
179 /* configuration to use on next enable */
180 uint32_t out_sample_rate;
181 uint32_t out_channel_mode;
182 uint32_t out_bits; /* bits per sample */
183
184 struct audmgr audmgr;
185
186 /* data allocated for various buffers */
187 char *data;
188 int32_t phys;
Laura Abbott35111d32012-04-27 18:41:48 -0700189 void *map_v_write;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700190
191 uint32_t drv_status;
192 int wflush; /* Write flush */
193 int opened;
194 int enabled;
195 int running;
196 int stopped; /* set when stopped, cleared on flush */
197 int teos; /* valid only if tunnel mode & no data left for decoder */
198 int rmt_resource_released;
199 enum msm_aud_decoder_state dec_state; /* Represents decoder state */
200 int reserved; /* A byte is being reserved */
201 char rsv_byte; /* Handle odd length user data */
202
203 const char *module_name;
204 unsigned queue_id;
205
206 unsigned volume;
207
208 uint16_t dec_id;
209
210#ifdef CONFIG_HAS_EARLYSUSPEND
211 struct audpcm_suspend_ctl suspend_ctl;
212#endif
213
214#ifdef CONFIG_DEBUG_FS
215 struct dentry *dentry;
216#endif
217 wait_queue_head_t wait;
218 struct list_head free_event_queue;
219 struct list_head event_queue;
220 wait_queue_head_t event_wait;
221 spinlock_t event_queue_lock;
222 struct mutex get_event_lock;
223 int event_abort;
224
225 struct list_head pmem_region_queue;
226 struct audpcm_drv_operations drv_ops;
227};
228
229static int auddec_dsp_config(struct audio *audio, int enable);
230static void audpp_cmd_cfg_adec_params(struct audio *audio);
231static void audplay_send_data(struct audio *audio, unsigned needed);
232static void audio_dsp_event(void *private, unsigned id, uint16_t *msg);
233static void audpcm_post_event(struct audio *audio, int type,
234 union msm_audio_event_payload payload);
235static unsigned long audpcm_pmem_fixup(struct audio *audio, void *addr,
236 unsigned long len, int ref_up);
237
238static int rmt_put_resource(struct audio *audio)
239{
240 struct aud_codec_config_cmd cmd;
241 unsigned short client_idx;
242
243 cmd.cmd_id = RM_CMD_AUD_CODEC_CFG;
244 cmd.client_id = RM_AUD_CLIENT_ID;
245 cmd.task_id = audio->dec_id;
246 cmd.enable = RMT_DISABLE;
247 cmd.dec_type = AUDDEC_DEC_PCM;
248 client_idx = ((cmd.client_id << 8) | cmd.task_id);
249
250 return put_adsp_resource(client_idx, &cmd, sizeof(cmd));
251}
252
253static int rmt_get_resource(struct audio *audio)
254{
255 struct aud_codec_config_cmd cmd;
256 unsigned short client_idx;
257
258 cmd.cmd_id = RM_CMD_AUD_CODEC_CFG;
259 cmd.client_id = RM_AUD_CLIENT_ID;
260 cmd.task_id = audio->dec_id;
261 cmd.enable = RMT_ENABLE;
262 cmd.dec_type = AUDDEC_DEC_PCM;
263 client_idx = ((cmd.client_id << 8) | cmd.task_id);
264
265 return get_adsp_resource(client_idx, &cmd, sizeof(cmd));
266}
267
268/* must be called with audio->lock held */
269static int audio_enable(struct audio *audio)
270{
271 struct audmgr_config cfg;
272 int rc;
273
274 MM_DBG("\n"); /* Macro prints the file name and function */
275 if (audio->enabled)
276 return 0;
277
278 if (audio->rmt_resource_released == 1) {
279 audio->rmt_resource_released = 0;
280 rc = rmt_get_resource(audio);
281 if (rc) {
282 MM_ERR("ADSP resources are not available for PCM \
283 session 0x%08x on decoder: %d\n Ignoring \
284 error and going ahead with the playback\n",
285 (int)audio, audio->dec_id);
286 }
287 }
288
289 audio->dec_state = MSM_AUD_DECODER_STATE_NONE;
290 audio->out_tail = 0;
291 audio->out_needed = 0;
292
293 cfg.tx_rate = RPC_AUD_DEF_SAMPLE_RATE_NONE;
294 cfg.rx_rate = RPC_AUD_DEF_SAMPLE_RATE_48000;
295 cfg.def_method = RPC_AUD_DEF_METHOD_PLAYBACK;
296 cfg.codec = RPC_AUD_DEF_CODEC_PCM;
297 cfg.snd_method = RPC_SND_METHOD_MIDI;
298
299 rc = audmgr_enable(&audio->audmgr, &cfg);
300 if (rc < 0)
301 return rc;
302
303 if (msm_adsp_enable(audio->audplay)) {
304 MM_ERR("msm_adsp_enable(audplay) failed\n");
305 audmgr_disable(&audio->audmgr);
306 return -ENODEV;
307 }
308
309 if (audpp_enable(audio->dec_id, audio_dsp_event, audio)) {
310 MM_ERR("audpp_enable() failed\n");
311 msm_adsp_disable(audio->audplay);
312 audmgr_disable(&audio->audmgr);
313 return -ENODEV;
314 }
315
316 audio->enabled = 1;
317 return 0;
318}
319
320/* must be called with audio->lock held */
321static int audio_disable(struct audio *audio)
322{
323 int rc = 0;
324 MM_DBG("\n"); /* Macro prints the file name and function */
325 if (audio->enabled) {
326 audio->enabled = 0;
327 audio->dec_state = MSM_AUD_DECODER_STATE_NONE;
328 auddec_dsp_config(audio, 0);
329 rc = wait_event_interruptible_timeout(audio->wait,
330 audio->dec_state != MSM_AUD_DECODER_STATE_NONE,
331 msecs_to_jiffies(MSM_AUD_DECODER_WAIT_MS));
332 if (rc == 0)
333 rc = -ETIMEDOUT;
334 else if (audio->dec_state != MSM_AUD_DECODER_STATE_CLOSE)
335 rc = -EFAULT;
336 else
337 rc = 0;
Manish Dewangan89a9f232012-02-09 17:14:40 +0530338 audio->stopped = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700339 wake_up(&audio->write_wait);
340 msm_adsp_disable(audio->audplay);
341 audpp_disable(audio->dec_id, audio);
342 audmgr_disable(&audio->audmgr);
343 audio->out_needed = 0;
344 rmt_put_resource(audio);
345 audio->rmt_resource_released = 1;
346 }
347 return rc;
348}
349
350/* ------------------- dsp --------------------- */
351static void audplay_dsp_event(void *data, unsigned id, size_t len,
352 void (*getevent) (void *ptr, size_t len))
353{
354 struct audio *audio = data;
355 uint32_t msg[28];
356 getevent(msg, sizeof(msg));
357
358 MM_DBG("msg_id=%x\n", id);
359
360 switch (id) {
361 case AUDPLAY_MSG_DEC_NEEDS_DATA:
362 audio->drv_ops.send_data(audio, 1);
363 break;
364 case ADSP_MESSAGE_ID:
365 MM_DBG("Received ADSP event: module enable(audplaytask)\n");
366 break;
367 default:
368 MM_ERR("unexpected message from decoder \n");
369 break;
370 }
371}
372
373static void audio_dsp_event(void *private, unsigned id, uint16_t *msg)
374{
375 struct audio *audio = private;
376
377 switch (id) {
378 case AUDPP_MSG_STATUS_MSG:{
379 unsigned status = msg[1];
380
381 switch (status) {
382 case AUDPP_DEC_STATUS_SLEEP: {
383 uint16_t reason = msg[2];
384 MM_DBG("decoder status: sleep reason = \
385 0x%04x\n", reason);
386 if ((reason == AUDPP_MSG_REASON_MEM)
387 || (reason ==
388 AUDPP_MSG_REASON_NODECODER)) {
389 audio->dec_state =
390 MSM_AUD_DECODER_STATE_FAILURE;
391 wake_up(&audio->wait);
392 } else if (reason == AUDPP_MSG_REASON_NONE) {
393 /* decoder is in disable state */
394 audio->dec_state =
395 MSM_AUD_DECODER_STATE_CLOSE;
396 wake_up(&audio->wait);
397 }
398 break;
399 }
400 case AUDPP_DEC_STATUS_INIT:
401 MM_DBG("decoder status: init\n");
402 audpp_cmd_cfg_adec_params(audio);
403 break;
404
405 case AUDPP_DEC_STATUS_CFG:
406 MM_DBG("decoder status: cfg \n");
407 break;
408 case AUDPP_DEC_STATUS_PLAY:
409 MM_DBG("decoder status: play \n");
410 audio->dec_state =
411 MSM_AUD_DECODER_STATE_SUCCESS;
412 wake_up(&audio->wait);
413 break;
414 default:
415 MM_ERR("unknown decoder status \n");
416 break;
417 }
418 break;
419 }
420 case AUDPP_MSG_CFG_MSG:
421 if (msg[0] == AUDPP_MSG_ENA_ENA) {
422 MM_DBG("CFG_MSG ENABLE\n");
423 auddec_dsp_config(audio, 1);
424 audio->out_needed = 0;
425 audio->running = 1;
426 audpp_set_volume_and_pan(audio->dec_id, audio->volume,
427 0);
428 } else if (msg[0] == AUDPP_MSG_ENA_DIS) {
429 MM_DBG("CFG_MSG DISABLE\n");
430 audio->running = 0;
431 } else {
432 MM_ERR("CFG_MSG %d?\n", msg[0]);
433 }
434 break;
435 case AUDPP_MSG_FLUSH_ACK:
436 MM_DBG("FLUSH_ACK\n");
437 audio->wflush = 0;
438 wake_up(&audio->write_wait);
439 break;
440
441 case AUDPP_MSG_PCMDMAMISSED:
442 MM_DBG("PCMDMAMISSED\n");
443 audio->teos = 1;
444 wake_up(&audio->write_wait);
445 break;
446
447 default:
448 MM_ERR("UNKNOWN (%d)\n", id);
449 }
450
451}
452
453
454struct msm_adsp_ops audpcmdec_adsp_ops = {
455 .event = audplay_dsp_event,
456};
457
458
459#define audplay_send_queue0(audio, cmd, len) \
460 msm_adsp_write(audio->audplay, audio->queue_id, \
461 cmd, len)
462
463static int auddec_dsp_config(struct audio *audio, int enable)
464{
465 u16 cfg_dec_cmd[AUDPP_CMD_CFG_DEC_TYPE_LEN / sizeof(unsigned short)];
466
467 memset(cfg_dec_cmd, 0, sizeof(cfg_dec_cmd));
468
469 cfg_dec_cmd[0] = AUDPP_CMD_CFG_DEC_TYPE;
470 if (enable)
471 cfg_dec_cmd[1 + audio->dec_id] = AUDPP_CMD_UPDATDE_CFG_DEC |
472 AUDPP_CMD_ENA_DEC_V | AUDDEC_DEC_PCM;
473 else
474 cfg_dec_cmd[1 + audio->dec_id] = AUDPP_CMD_UPDATDE_CFG_DEC |
475 AUDPP_CMD_DIS_DEC_V;
476
477 return audpp_send_queue1(&cfg_dec_cmd, sizeof(cfg_dec_cmd));
478}
479
480static void audpp_cmd_cfg_adec_params(struct audio *audio)
481{
482 audpp_cmd_cfg_adec_params_wav cmd;
483
484 memset(&cmd, 0, sizeof(cmd));
485 cmd.common.cmd_id = AUDPP_CMD_CFG_ADEC_PARAMS;
486 cmd.common.length = AUDPP_CMD_CFG_ADEC_PARAMS_WAV_LEN;
487 cmd.common.dec_id = audio->dec_id;
488 cmd.common.input_sampling_frequency = audio->out_sample_rate;
489 cmd.stereo_cfg = audio->out_channel_mode;
490 cmd.pcm_width = audio->out_bits;
491 cmd.sign = 0;
492 audpp_send_queue2(&cmd, sizeof(cmd));
493}
494
495static int audplay_dsp_send_data_avail(struct audio *audio,
496 unsigned idx, unsigned len)
497{
498 audplay_cmd_bitstream_data_avail cmd;
499
500 cmd.cmd_id = AUDPLAY_CMD_BITSTREAM_DATA_AVAIL;
501 cmd.decoder_id = audio->dec_id;
502 cmd.buf_ptr = audio->out[idx].addr;
503 cmd.buf_size = len/2;
504 cmd.partition_number = 0;
505 /* complete writes to the input buffer */
506 wmb();
507 return audplay_send_queue0(audio, &cmd, sizeof(cmd));
508}
509
510static void audpcm_async_send_data(struct audio *audio, unsigned needed)
511{
512 unsigned long flags;
513
514 if (!audio->running)
515 return;
516
517 spin_lock_irqsave(&audio->dsp_lock, flags);
518
519 if (needed && !audio->wflush) {
520 audio->out_needed = 1;
521 if (audio->drv_status & ADRV_STATUS_OBUF_GIVEN) {
522 /* pop one node out of queue */
523 union msm_audio_event_payload payload;
524 struct audpcm_buffer_node *used_buf;
525
526 MM_DBG("consumed\n");
527
528 BUG_ON(list_empty(&audio->out_queue));
529 used_buf = list_first_entry(&audio->out_queue,
530 struct audpcm_buffer_node, list);
531 list_del(&used_buf->list);
532 payload.aio_buf = used_buf->buf;
533 audpcm_post_event(audio, AUDIO_EVENT_WRITE_DONE,
534 payload);
535 kfree(used_buf);
536 audio->drv_status &= ~ADRV_STATUS_OBUF_GIVEN;
537 }
538 }
539 if (audio->out_needed) {
540 struct audpcm_buffer_node *next_buf;
541 audplay_cmd_bitstream_data_avail cmd;
542 if (!list_empty(&audio->out_queue)) {
543 next_buf = list_first_entry(&audio->out_queue,
544 struct audpcm_buffer_node, list);
545 MM_DBG("next_buf %p\n", next_buf);
546 if (next_buf) {
547 MM_DBG("next buf phy %lx len %d\n",
548 next_buf->paddr, next_buf->buf.data_len);
549
550 cmd.cmd_id = AUDPLAY_CMD_BITSTREAM_DATA_AVAIL;
551 if (next_buf->buf.data_len)
552 cmd.decoder_id = audio->dec_id;
553 else {
554 cmd.decoder_id = -1;
555 MM_DBG("input EOS signaled\n");
556 }
557 cmd.buf_ptr = (unsigned) next_buf->paddr;
558 cmd.buf_size = next_buf->buf.data_len >> 1;
559 cmd.partition_number = 0;
560 /* complete writes to the input buffer */
561 wmb();
562 audplay_send_queue0(audio, &cmd, sizeof(cmd));
563 audio->out_needed = 0;
564 audio->drv_status |= ADRV_STATUS_OBUF_GIVEN;
565 }
566 }
567 }
568 spin_unlock_irqrestore(&audio->dsp_lock, flags);
569}
570
571static void audplay_send_data(struct audio *audio, unsigned needed)
572{
573 struct buffer *frame;
574 unsigned long flags;
575
576 if (!audio->running)
577 return;
578
579 spin_lock_irqsave(&audio->dsp_lock, flags);
580
581 if (needed && !audio->wflush) {
582 /* We were called from the callback because the DSP
583 * requested more data. Note that the DSP does want
584 * more data, and if a buffer was in-flight, mark it
585 * as available (since the DSP must now be done with
586 * it).
587 */
588 audio->out_needed = 1;
589 frame = audio->out + audio->out_tail;
590 if (frame->used == 0xffffffff) {
591 MM_DBG("frame %d free\n", audio->out_tail);
592 frame->used = 0;
593 audio->out_tail ^= 1;
594 wake_up(&audio->write_wait);
595 }
596 }
597
598 if (audio->out_needed) {
599 /* If the DSP currently wants data and we have a
600 * buffer available, we will send it and reset
601 * the needed flag. We'll mark the buffer as in-flight
602 * so that it won't be recycled until the next buffer
603 * is requested
604 */
605
606 frame = audio->out + audio->out_tail;
607 if (frame->used) {
608 BUG_ON(frame->used == 0xffffffff);
609 MM_DBG("frame %d busy\n", audio->out_tail);
610 audplay_dsp_send_data_avail(audio, audio->out_tail,
611 frame->used);
612 frame->used = 0xffffffff;
613 audio->out_needed = 0;
614 }
615 }
616 spin_unlock_irqrestore(&audio->dsp_lock, flags);
617}
618
619/* ------------------- device --------------------- */
620static void audpcm_async_flush(struct audio *audio)
621{
622 struct audpcm_buffer_node *buf_node;
623 struct list_head *ptr, *next;
624 union msm_audio_event_payload payload;
625
626 MM_DBG("\n"); /* Macro prints the file name and function */
627 list_for_each_safe(ptr, next, &audio->out_queue) {
628 buf_node = list_entry(ptr, struct audpcm_buffer_node, list);
629 list_del(&buf_node->list);
630 payload.aio_buf = buf_node->buf;
631 audpcm_post_event(audio, AUDIO_EVENT_WRITE_DONE,
632 payload);
633 kfree(buf_node);
634 }
635 audio->drv_status &= ~ADRV_STATUS_OBUF_GIVEN;
636 audio->out_needed = 0;
637 atomic_set(&audio->out_bytes, 0);
638}
639
640static void audio_flush(struct audio *audio)
641{
Manish Dewangana4f1df02012-02-08 17:06:54 +0530642 unsigned long flags;
643
644 spin_lock_irqsave(&audio->dsp_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700645 audio->out[0].used = 0;
646 audio->out[1].used = 0;
647 audio->out_head = 0;
648 audio->out_tail = 0;
649 audio->reserved = 0;
650 audio->out_needed = 0;
Manish Dewangana4f1df02012-02-08 17:06:54 +0530651 spin_unlock_irqrestore(&audio->dsp_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700652 atomic_set(&audio->out_bytes, 0);
653}
654
655static void audio_ioport_reset(struct audio *audio)
656{
657 if (audio->drv_status & ADRV_STATUS_AIO_INTF) {
658 /* If fsync is in progress, make sure
659 * return value of fsync indicates
660 * abort due to flush
661 */
662 if (audio->drv_status & ADRV_STATUS_FSYNC) {
663 MM_DBG("fsync in progress\n");
664 wake_up(&audio->write_wait);
665 mutex_lock(&audio->write_lock);
666 audio->drv_ops.out_flush(audio);
667 mutex_unlock(&audio->write_lock);
668 } else
669 audio->drv_ops.out_flush(audio);
670 } else {
671 /* Make sure read/write thread are free from
672 * sleep and knowing that system is not able
673 * to process io request at the moment
674 */
675 wake_up(&audio->write_wait);
676 mutex_lock(&audio->write_lock);
677 audio->drv_ops.out_flush(audio);
678 mutex_unlock(&audio->write_lock);
679 }
680}
681
682static int audpcm_events_pending(struct audio *audio)
683{
684 unsigned long flags;
685 int empty;
686
687 spin_lock_irqsave(&audio->event_queue_lock, flags);
688 empty = !list_empty(&audio->event_queue);
689 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
690 return empty || audio->event_abort;
691}
692
693static void audpcm_reset_event_queue(struct audio *audio)
694{
695 unsigned long flags;
696 struct audpcm_event *drv_evt;
697 struct list_head *ptr, *next;
698
699 spin_lock_irqsave(&audio->event_queue_lock, flags);
700 list_for_each_safe(ptr, next, &audio->event_queue) {
701 drv_evt = list_first_entry(&audio->event_queue,
702 struct audpcm_event, list);
703 list_del(&drv_evt->list);
704 kfree(drv_evt);
705 }
706 list_for_each_safe(ptr, next, &audio->free_event_queue) {
707 drv_evt = list_first_entry(&audio->free_event_queue,
708 struct audpcm_event, list);
709 list_del(&drv_evt->list);
710 kfree(drv_evt);
711 }
712 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
713
714 return;
715}
716
717static long audpcm_process_event_req(struct audio *audio, void __user *arg)
718{
719 long rc;
720 struct msm_audio_event usr_evt;
721 struct audpcm_event *drv_evt = NULL;
722 int timeout;
723 unsigned long flags;
724
725 if (copy_from_user(&usr_evt, arg, sizeof(struct msm_audio_event)))
726 return -EFAULT;
727
728 timeout = (int) usr_evt.timeout_ms;
729
730 if (timeout > 0) {
731 rc = wait_event_interruptible_timeout(
732 audio->event_wait, audpcm_events_pending(audio),
733 msecs_to_jiffies(timeout));
734 if (rc == 0)
735 return -ETIMEDOUT;
736 } else {
737 rc = wait_event_interruptible(
738 audio->event_wait, audpcm_events_pending(audio));
739 }
740
741 if (rc < 0)
742 return rc;
743
744 if (audio->event_abort) {
745 audio->event_abort = 0;
746 return -ENODEV;
747 }
748
749 spin_lock_irqsave(&audio->event_queue_lock, flags);
750 if (!list_empty(&audio->event_queue)) {
751 drv_evt = list_first_entry(&audio->event_queue,
752 struct audpcm_event, list);
753 list_del(&drv_evt->list);
754 }
755 if (drv_evt) {
756 usr_evt.event_type = drv_evt->event_type;
757 usr_evt.event_payload = drv_evt->payload;
758 list_add_tail(&drv_evt->list, &audio->free_event_queue);
759 } else
760 rc = -1;
761 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
762
763 if (drv_evt && drv_evt->event_type == AUDIO_EVENT_WRITE_DONE) {
764 mutex_lock(&audio->lock);
765 audpcm_pmem_fixup(audio, drv_evt->payload.aio_buf.buf_addr,
766 drv_evt->payload.aio_buf.buf_len, 0);
767 mutex_unlock(&audio->lock);
768 }
769 if (!rc && copy_to_user(arg, &usr_evt, sizeof(usr_evt)))
770 rc = -EFAULT;
771
772 return rc;
773}
774
775static int audpcm_pmem_check(struct audio *audio,
776 void *vaddr, unsigned long len)
777{
778 struct audpcm_pmem_region *region_elt;
779 struct audpcm_pmem_region t = { .vaddr = vaddr, .len = len };
780
781 list_for_each_entry(region_elt, &audio->pmem_region_queue, list) {
782 if (CONTAINS(region_elt, &t) || CONTAINS(&t, region_elt) ||
783 OVERLAPS(region_elt, &t)) {
784 MM_ERR("region (vaddr %p len %ld)"
785 " clashes with registered region"
786 " (vaddr %p paddr %p len %ld)\n",
787 vaddr, len,
788 region_elt->vaddr,
789 (void *)region_elt->paddr,
790 region_elt->len);
791 return -EINVAL;
792 }
793 }
794
795 return 0;
796}
797
798static int audpcm_pmem_add(struct audio *audio,
799 struct msm_audio_pmem_info *info)
800{
801 unsigned long paddr, kvaddr, len;
802 struct file *file;
803 struct audpcm_pmem_region *region;
804 struct vm_area_struct *vma;
805 int rc = -EINVAL;
806
807 MM_DBG("\n"); /* Macro prints the file name and function */
808 region = kmalloc(sizeof(*region), GFP_KERNEL);
809 if (!region)
810 return -ENOMEM;
811
812 if (get_pmem_file(info->fd, &paddr, &kvaddr, &len, &file)) {
813 kfree(region);
814 return -EINVAL;
815 }
816
817 vma = find_vma_intersection(current->active_mm,
818 (unsigned long) info->vaddr, (unsigned long) info->vaddr+1);
819
820 if (vma && ((vma->vm_end - vma->vm_start) == len)) {
821 rc = audpcm_pmem_check(audio, (void *) vma->vm_start, len);
822 if (rc < 0) {
823 put_pmem_file(file);
824 kfree(region);
825 return rc;
826 }
827 region->vaddr = (void *) vma->vm_start;
828 region->vaddr_ref = info->vaddr;
829 MM_DBG("Valid VMA region vma->vm_start = 0x%8x \
830 vma->vm_end = 0x%8x\n", (int) vma->vm_start,
831 (int) vma->vm_end);
832 } else {
833 MM_ERR("No valid VMA region found\n");
834 put_pmem_file(file);
835 kfree(region);
836 return rc;
837 }
838 region->fd = info->fd;
839 region->paddr = paddr;
840 region->kvaddr = kvaddr;
841 region->len = len;
842 region->file = file;
843 region->ref_cnt = 0;
844 MM_DBG("add region paddr %lx vaddr %p, len %lu\n", region->paddr,
845 region->vaddr, region->len);
846 list_add_tail(&region->list, &audio->pmem_region_queue);
847 return rc;
848}
849
850static int audpcm_pmem_remove(struct audio *audio,
851 struct msm_audio_pmem_info *info)
852{
853 struct audpcm_pmem_region *region;
854 struct list_head *ptr, *next;
855 int rc = -EINVAL;
856
857 MM_DBG("info fd %d vaddr %p\n", info->fd, info->vaddr);
858
859 list_for_each_safe(ptr, next, &audio->pmem_region_queue) {
860 region = list_entry(ptr, struct audpcm_pmem_region, list);
861
862 if ((region->fd == info->fd) &&
863 (region->vaddr_ref == info->vaddr)) {
864 if (region->ref_cnt) {
865 MM_DBG("region %p in use ref_cnt %d\n",
866 region, region->ref_cnt);
867 break;
868 }
869 MM_DBG("remove region fd %d vaddr %p \n", info->fd,
870 info->vaddr);
871 list_del(&region->list);
872 put_pmem_file(region->file);
873 kfree(region);
874 rc = 0;
875 break;
876 }
877 }
878
879 return rc;
880}
881
882static int audpcm_pmem_lookup_vaddr(struct audio *audio, void *addr,
883 unsigned long len, struct audpcm_pmem_region **region)
884{
885 struct audpcm_pmem_region *region_elt;
886
887 int match_count = 0;
888
889 *region = NULL;
890
891 /* returns physical address or zero */
892 list_for_each_entry(region_elt, &audio->pmem_region_queue,
893 list) {
894 if (addr >= region_elt->vaddr &&
895 addr < region_elt->vaddr + region_elt->len &&
896 addr + len <= region_elt->vaddr + region_elt->len) {
897 /* offset since we could pass vaddr inside a registerd
898 * pmem buffer
899 */
900 match_count++;
901 if (!*region)
902 *region = region_elt;
903 }
904 }
905
906 if (match_count > 1) {
907 MM_ERR("multiple hits for vaddr %p, len %ld\n", addr, len);
908 list_for_each_entry(region_elt,
909 &audio->pmem_region_queue, list) {
910 if (addr >= region_elt->vaddr &&
911 addr < region_elt->vaddr + region_elt->len &&
912 addr + len <= region_elt->vaddr + region_elt->len)
913 MM_ERR("\t%p, %ld --> %p\n", region_elt->vaddr,
914 region_elt->len,
915 (void *)region_elt->paddr);
916 }
917 }
918
919 return *region ? 0 : -1;
920}
921
922static unsigned long audpcm_pmem_fixup(struct audio *audio, void *addr,
923 unsigned long len, int ref_up)
924{
925 struct audpcm_pmem_region *region;
926 unsigned long paddr;
927 int ret;
928
929 ret = audpcm_pmem_lookup_vaddr(audio, addr, len, &region);
930 if (ret) {
931 MM_ERR("lookup (%p, %ld) failed\n", addr, len);
932 return 0;
933 }
934 if (ref_up)
935 region->ref_cnt++;
936 else
937 region->ref_cnt--;
938 MM_DBG("found region %p ref_cnt %d\n", region, region->ref_cnt);
939 paddr = region->paddr + (addr - region->vaddr);
940 return paddr;
941}
942
943/* audio -> lock must be held at this point */
944static int audpcm_aio_buf_add(struct audio *audio, unsigned dir,
945 void __user *arg)
946{
947 unsigned long flags;
948 struct audpcm_buffer_node *buf_node;
949
950 buf_node = kmalloc(sizeof(*buf_node), GFP_KERNEL);
951
952 if (!buf_node)
953 return -ENOMEM;
954
955 if (copy_from_user(&buf_node->buf, arg, sizeof(buf_node->buf))) {
956 kfree(buf_node);
957 return -EFAULT;
958 }
959
960 MM_DBG("node %p dir %x buf_addr %p buf_len %d data_len %d\n",
961 buf_node, dir, buf_node->buf.buf_addr,
962 buf_node->buf.buf_len, buf_node->buf.data_len);
963
964 buf_node->paddr = audpcm_pmem_fixup(
965 audio, buf_node->buf.buf_addr,
966 buf_node->buf.buf_len, 1);
967 if (dir) {
968 /* write */
969 if (!buf_node->paddr ||
970 (buf_node->paddr & 0x1) ||
971 (buf_node->buf.data_len & 0x1) ||
972 (!buf_node->buf.data_len)) {
973 kfree(buf_node);
974 return -EINVAL;
975 }
976 spin_lock_irqsave(&audio->dsp_lock, flags);
977 list_add_tail(&buf_node->list, &audio->out_queue);
978 spin_unlock_irqrestore(&audio->dsp_lock, flags);
979 audio->drv_ops.send_data(audio, 0);
980 }
981
982 MM_DBG("Add buf_node %p paddr %lx\n", buf_node, buf_node->paddr);
983
984 return 0;
985}
986
987static long audio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
988{
989 struct audio *audio = file->private_data;
990 int rc = 0;
991
992 MM_DBG("cmd = %d\n", cmd);
993
994 if (cmd == AUDIO_GET_STATS) {
995 struct msm_audio_stats stats;
996 stats.byte_count = audpp_avsync_byte_count(audio->dec_id);
997 stats.sample_count = audpp_avsync_sample_count(audio->dec_id);
998 if (copy_to_user((void *) arg, &stats, sizeof(stats)))
999 return -EFAULT;
1000 return 0;
1001 }
1002 if (cmd == AUDIO_SET_VOLUME) {
1003 unsigned long flags;
1004 spin_lock_irqsave(&audio->dsp_lock, flags);
1005 audio->volume = arg;
1006 if (audio->running)
1007 audpp_set_volume_and_pan(audio->dec_id, arg, 0);
1008 spin_unlock_irqrestore(&audio->dsp_lock, flags);
1009 return 0;
1010 }
1011 if (cmd == AUDIO_GET_EVENT) {
1012 MM_DBG("AUDIO_GET_EVENT\n");
1013 if (mutex_trylock(&audio->get_event_lock)) {
1014 rc = audpcm_process_event_req(audio,
1015 (void __user *) arg);
1016 mutex_unlock(&audio->get_event_lock);
1017 } else
1018 rc = -EBUSY;
1019 return rc;
1020 }
1021
1022 if (cmd == AUDIO_ABORT_GET_EVENT) {
1023 audio->event_abort = 1;
1024 wake_up(&audio->event_wait);
1025 return 0;
1026 }
1027
1028 mutex_lock(&audio->lock);
1029 switch (cmd) {
1030 case AUDIO_START:
1031 MM_DBG("AUDIO_START\n");
1032 rc = audio_enable(audio);
1033 if (!rc) {
1034 rc = wait_event_interruptible_timeout(audio->wait,
1035 audio->dec_state != MSM_AUD_DECODER_STATE_NONE,
1036 msecs_to_jiffies(MSM_AUD_DECODER_WAIT_MS));
1037 MM_INFO("dec_state %d rc = %d\n", audio->dec_state, rc);
1038
1039 if (audio->dec_state != MSM_AUD_DECODER_STATE_SUCCESS)
1040 rc = -ENODEV;
1041 else
1042 rc = 0;
1043 }
1044 break;
1045 case AUDIO_STOP:
1046 MM_DBG("AUDIO_STOP\n");
1047 rc = audio_disable(audio);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001048 audio_ioport_reset(audio);
1049 audio->stopped = 0;
1050 break;
1051 case AUDIO_FLUSH:
1052 MM_DBG("AUDIO_FLUSH\n");
1053 audio->wflush = 1;
1054 audio_ioport_reset(audio);
1055 if (audio->running) {
1056 audpp_flush(audio->dec_id);
1057 rc = wait_event_interruptible(audio->write_wait,
1058 !audio->wflush);
1059 if (rc < 0) {
1060 MM_ERR("AUDIO_FLUSH interrupted\n");
1061 rc = -EINTR;
1062 }
1063 } else {
1064 audio->wflush = 0;
1065 }
1066 break;
1067
1068 case AUDIO_SET_CONFIG: {
1069 struct msm_audio_config config;
1070 if (copy_from_user(&config, (void *) arg, sizeof(config))) {
1071 rc = -EFAULT;
1072 break;
1073 }
1074 if (config.channel_count == 1) {
1075 config.channel_count = AUDPP_CMD_PCM_INTF_MONO_V;
1076 } else if (config.channel_count == 2) {
1077 config.channel_count = AUDPP_CMD_PCM_INTF_STEREO_V;
1078 } else {
1079 rc = -EINVAL;
1080 break;
1081 }
1082 if (config.bits == 8)
1083 config.bits = AUDPP_CMD_WAV_PCM_WIDTH_8;
1084 else if (config.bits == 16)
1085 config.bits = AUDPP_CMD_WAV_PCM_WIDTH_16;
1086 else if (config.bits == 24)
1087 config.bits = AUDPP_CMD_WAV_PCM_WIDTH_24;
1088 else {
1089 rc = -EINVAL;
1090 break;
1091 }
1092 audio->out_sample_rate = config.sample_rate;
1093 audio->out_channel_mode = config.channel_count;
1094 audio->out_bits = config.bits;
1095 break;
1096 }
1097 case AUDIO_GET_CONFIG: {
1098 struct msm_audio_config config;
1099 config.buffer_size = (audio->out_dma_sz >> 1);
1100 config.buffer_count = 2;
1101 config.sample_rate = audio->out_sample_rate;
1102 if (audio->out_channel_mode == AUDPP_CMD_PCM_INTF_MONO_V)
1103 config.channel_count = 1;
1104 else
1105 config.channel_count = 2;
1106 if (audio->out_bits == AUDPP_CMD_WAV_PCM_WIDTH_8)
1107 config.bits = 8;
1108 else if (audio->out_bits == AUDPP_CMD_WAV_PCM_WIDTH_24)
1109 config.bits = 24;
1110 else
1111 config.bits = 16;
1112 config.unused[0] = 0;
1113 config.unused[1] = 0;
1114
1115 if (copy_to_user((void *) arg, &config, sizeof(config)))
1116 rc = -EFAULT;
1117 else
1118 rc = 0;
1119 break;
1120 }
1121
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05301122
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001123 case AUDIO_PAUSE:
1124 MM_DBG("AUDIO_PAUSE %ld\n", arg);
1125 rc = audpp_pause(audio->dec_id, (int) arg);
1126 break;
1127
1128 case AUDIO_REGISTER_PMEM: {
1129 struct msm_audio_pmem_info info;
1130 MM_DBG("AUDIO_REGISTER_PMEM\n");
1131 if (copy_from_user(&info, (void *) arg, sizeof(info)))
1132 rc = -EFAULT;
1133 else
1134 rc = audpcm_pmem_add(audio, &info);
1135 break;
1136 }
1137
1138 case AUDIO_DEREGISTER_PMEM: {
1139 struct msm_audio_pmem_info info;
1140 MM_DBG("AUDIO_DEREGISTER_PMEM\n");
1141 if (copy_from_user(&info, (void *) arg, sizeof(info)))
1142 rc = -EFAULT;
1143 else
1144 rc = audpcm_pmem_remove(audio, &info);
1145 break;
1146 }
1147
1148 case AUDIO_ASYNC_WRITE:
1149 if (audio->drv_status & ADRV_STATUS_FSYNC)
1150 rc = -EBUSY;
1151 else
1152 rc = audpcm_aio_buf_add(audio, 1, (void __user *) arg);
1153 break;
1154
1155 case AUDIO_ASYNC_READ:
1156 MM_ERR("AUDIO_ASYNC_READ not supported\n");
1157 rc = -EPERM;
1158 break;
1159
1160 default:
1161 rc = -EINVAL;
1162 }
1163 mutex_unlock(&audio->lock);
1164 return rc;
1165}
1166
1167/* Only useful in tunnel-mode */
1168int audpcm_async_fsync(struct audio *audio)
1169{
1170 int rc = 0;
1171
1172 MM_DBG("\n"); /* Macro prints the file name and function */
1173
1174 /* Blocking client sends more data */
1175 mutex_lock(&audio->lock);
1176 audio->drv_status |= ADRV_STATUS_FSYNC;
1177 mutex_unlock(&audio->lock);
1178
1179 mutex_lock(&audio->write_lock);
1180 /* pcm dmamiss message is sent continously
1181 * when decoder is starved so no race
1182 * condition concern
1183 */
1184 audio->teos = 0;
1185
1186 rc = wait_event_interruptible(audio->write_wait,
1187 (audio->teos && audio->out_needed &&
1188 list_empty(&audio->out_queue))
1189 || audio->wflush || audio->stopped);
1190
1191 if (audio->stopped || audio->wflush)
1192 rc = -EBUSY;
1193
1194 mutex_unlock(&audio->write_lock);
1195 mutex_lock(&audio->lock);
1196 audio->drv_status &= ~ADRV_STATUS_FSYNC;
1197 mutex_unlock(&audio->lock);
1198
1199 return rc;
1200}
1201
1202int audpcm_sync_fsync(struct audio *audio)
1203{
1204 struct buffer *frame;
1205 int rc = 0;
1206
1207 MM_DBG("\n"); /* Macro prints the file name and function */
1208
1209 mutex_lock(&audio->write_lock);
1210
1211 rc = wait_event_interruptible(audio->write_wait,
1212 (!audio->out[0].used &&
1213 !audio->out[1].used &&
1214 audio->out_needed) || audio->wflush);
1215
1216 if (rc < 0)
1217 goto done;
1218 else if (audio->wflush) {
1219 rc = -EBUSY;
1220 goto done;
1221 }
1222
1223 if (audio->reserved) {
1224 MM_DBG("send reserved byte\n");
1225 frame = audio->out + audio->out_tail;
1226 ((char *) frame->data)[0] = audio->rsv_byte;
1227 ((char *) frame->data)[1] = 0;
1228 frame->used = 2;
1229 audio->drv_ops.send_data(audio, 0);
1230
1231 rc = wait_event_interruptible(audio->write_wait,
1232 (!audio->out[0].used &&
1233 !audio->out[1].used &&
1234 audio->out_needed) || audio->wflush);
1235
1236 if (rc < 0)
1237 goto done;
1238 else if (audio->wflush) {
1239 rc = -EBUSY;
1240 goto done;
1241 }
1242 }
1243
1244 /* pcm dmamiss message is sent continously
1245 * when decoder is starved so no race
1246 * condition concern
1247 */
1248 audio->teos = 0;
1249
1250 rc = wait_event_interruptible(audio->write_wait,
1251 audio->teos || audio->wflush);
1252
1253 if (audio->wflush)
1254 rc = -EBUSY;
1255
1256done:
1257 mutex_unlock(&audio->write_lock);
1258 return rc;
1259}
1260
1261int audpcm_fsync(struct file *file, int datasync)
1262{
1263 struct audio *audio = file->private_data;
1264
1265 if (!audio->running)
1266 return -EINVAL;
1267
1268 return audio->drv_ops.fsync(audio);
1269}
1270
1271static ssize_t audio_write(struct file *file, const char __user *buf,
1272 size_t count, loff_t *pos)
1273{
1274 struct audio *audio = file->private_data;
1275 const char __user *start = buf;
1276 struct buffer *frame;
1277 size_t xfer;
1278 char *cpy_ptr;
1279 int rc = 0;
1280 unsigned dsize;
1281
1282 if (audio->drv_status & ADRV_STATUS_AIO_INTF)
1283 return -EPERM;
1284
1285 MM_DBG("cnt=%d\n", count);
1286
1287 mutex_lock(&audio->write_lock);
1288 while (count > 0) {
1289 frame = audio->out + audio->out_head;
1290 cpy_ptr = frame->data;
1291 dsize = 0;
1292 rc = wait_event_interruptible(audio->write_wait,
1293 (frame->used == 0)
1294 || (audio->stopped)
1295 || (audio->wflush));
1296 if (rc < 0)
1297 break;
1298 if (audio->stopped || audio->wflush) {
1299 rc = -EBUSY;
1300 break;
1301 }
1302
1303 if (audio->reserved) {
1304 MM_DBG("append reserved byte %x\n", audio->rsv_byte);
1305 *cpy_ptr = audio->rsv_byte;
1306 xfer = (count > (frame->size - 1)) ?
1307 frame->size - 1 : count;
1308 cpy_ptr++;
1309 dsize = 1;
1310 audio->reserved = 0;
1311 } else
1312 xfer = (count > frame->size) ? frame->size : count;
1313
1314 if (copy_from_user(cpy_ptr, buf, xfer)) {
1315 rc = -EFAULT;
1316 break;
1317 }
1318
1319 dsize += xfer;
1320 if (dsize & 1) {
1321 audio->rsv_byte = ((char *) frame->data)[dsize - 1];
1322 MM_DBG("odd length buf reserve last byte %x\n",
1323 audio->rsv_byte);
1324 audio->reserved = 1;
1325 dsize--;
1326 }
1327 count -= xfer;
1328 buf += xfer;
1329
1330 if (dsize > 0) {
1331 audio->out_head ^= 1;
1332 frame->used = dsize;
1333 audio->drv_ops.send_data(audio, 0);
1334 }
1335 }
1336 mutex_unlock(&audio->write_lock);
1337 if (buf > start)
1338 return buf - start;
1339
1340 return rc;
1341}
1342
1343static void audpcm_reset_pmem_region(struct audio *audio)
1344{
1345 struct audpcm_pmem_region *region;
1346 struct list_head *ptr, *next;
1347
1348 list_for_each_safe(ptr, next, &audio->pmem_region_queue) {
1349 region = list_entry(ptr, struct audpcm_pmem_region, list);
1350 list_del(&region->list);
1351 put_pmem_file(region->file);
1352 kfree(region);
1353 }
1354
1355 return;
1356}
1357
1358static int audio_release(struct inode *inode, struct file *file)
1359{
1360 struct audio *audio = file->private_data;
1361
1362 MM_DBG("audio instance 0x%08x freeing\n", (int)audio);
1363 mutex_lock(&audio->lock);
1364 audio_disable(audio);
1365 if (audio->rmt_resource_released == 0)
1366 rmt_put_resource(audio);
1367 audio->drv_ops.out_flush(audio);
1368 audpcm_reset_pmem_region(audio);
1369
1370 msm_adsp_put(audio->audplay);
1371 audpp_adec_free(audio->dec_id);
1372#ifdef CONFIG_HAS_EARLYSUSPEND
1373 unregister_early_suspend(&audio->suspend_ctl.node);
1374#endif
1375 audio->opened = 0;
1376 audio->event_abort = 1;
1377 wake_up(&audio->event_wait);
1378 audpcm_reset_event_queue(audio);
1379 MM_DBG("pmem area = 0x%8x\n", (unsigned int)audio->data);
1380 if (audio->data) {
Laura Abbott35111d32012-04-27 18:41:48 -07001381 iounmap(audio->map_v_write);
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05301382 free_contiguous_memory_by_paddr(audio->phys);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001383 }
1384 mutex_unlock(&audio->lock);
1385#ifdef CONFIG_DEBUG_FS
1386 if (audio->dentry)
1387 debugfs_remove(audio->dentry);
1388#endif
1389 kfree(audio);
1390 return 0;
1391}
1392
1393static void audpcm_post_event(struct audio *audio, int type,
1394 union msm_audio_event_payload payload)
1395{
1396 struct audpcm_event *e_node = NULL;
1397 unsigned long flags;
1398
1399 spin_lock_irqsave(&audio->event_queue_lock, flags);
1400
1401 if (!list_empty(&audio->free_event_queue)) {
1402 e_node = list_first_entry(&audio->free_event_queue,
1403 struct audpcm_event, list);
1404 list_del(&e_node->list);
1405 } else {
1406 e_node = kmalloc(sizeof(struct audpcm_event), GFP_ATOMIC);
1407 if (!e_node) {
1408 MM_ERR("No mem to post event %d\n", type);
1409 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
1410 return;
1411 }
1412 }
1413
1414 e_node->event_type = type;
1415 e_node->payload = payload;
1416
1417 list_add_tail(&e_node->list, &audio->event_queue);
1418 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
1419 wake_up(&audio->event_wait);
1420}
1421
1422#ifdef CONFIG_HAS_EARLYSUSPEND
1423static void audpcm_suspend(struct early_suspend *h)
1424{
1425 struct audpcm_suspend_ctl *ctl =
1426 container_of(h, struct audpcm_suspend_ctl, node);
1427 union msm_audio_event_payload payload;
1428
1429 MM_DBG("\n"); /* Macro prints the file name and function */
1430 audpcm_post_event(ctl->audio, AUDIO_EVENT_SUSPEND, payload);
1431}
1432
1433static void audpcm_resume(struct early_suspend *h)
1434{
1435 struct audpcm_suspend_ctl *ctl =
1436 container_of(h, struct audpcm_suspend_ctl, node);
1437 union msm_audio_event_payload payload;
1438
1439 MM_DBG("\n"); /* Macro prints the file name and function */
1440 audpcm_post_event(ctl->audio, AUDIO_EVENT_RESUME, payload);
1441}
1442#endif
1443
1444#ifdef CONFIG_DEBUG_FS
1445static ssize_t audpcm_debug_open(struct inode *inode, struct file *file)
1446{
1447 file->private_data = inode->i_private;
1448 return 0;
1449}
1450
1451static ssize_t audpcm_debug_read(struct file *file, char __user *buf,
1452 size_t count, loff_t *ppos)
1453{
1454 const int debug_bufmax = 4096;
1455 static char buffer[4096];
1456 int n = 0;
1457 struct audio *audio = file->private_data;
1458
1459 mutex_lock(&audio->lock);
1460 n = scnprintf(buffer, debug_bufmax, "opened %d\n", audio->opened);
1461 n += scnprintf(buffer + n, debug_bufmax - n,
1462 "enabled %d\n", audio->enabled);
1463 n += scnprintf(buffer + n, debug_bufmax - n,
1464 "stopped %d\n", audio->stopped);
1465 n += scnprintf(buffer + n, debug_bufmax - n,
1466 "out_buf_sz %d\n", audio->out[0].size);
1467 n += scnprintf(buffer + n, debug_bufmax - n,
1468 "volume %x \n", audio->volume);
1469 n += scnprintf(buffer + n, debug_bufmax - n,
1470 "sample rate %d \n", audio->out_sample_rate);
1471 n += scnprintf(buffer + n, debug_bufmax - n,
1472 "channel mode %d \n", audio->out_channel_mode);
1473 mutex_unlock(&audio->lock);
1474 /* Following variables are only useful for debugging when
1475 * when playback halts unexpectedly. Thus, no mutual exclusion
1476 * enforced
1477 */
1478 n += scnprintf(buffer + n, debug_bufmax - n,
1479 "wflush %d\n", audio->wflush);
1480 n += scnprintf(buffer + n, debug_bufmax - n,
1481 "running %d \n", audio->running);
1482 n += scnprintf(buffer + n, debug_bufmax - n,
1483 "dec state %d \n", audio->dec_state);
1484 n += scnprintf(buffer + n, debug_bufmax - n,
1485 "out_needed %d \n", audio->out_needed);
1486 n += scnprintf(buffer + n, debug_bufmax - n,
1487 "out_head %d \n", audio->out_head);
1488 n += scnprintf(buffer + n, debug_bufmax - n,
1489 "out_tail %d \n", audio->out_tail);
1490 n += scnprintf(buffer + n, debug_bufmax - n,
1491 "out[0].used %d \n", audio->out[0].used);
1492 n += scnprintf(buffer + n, debug_bufmax - n,
1493 "out[1].used %d \n", audio->out[1].used);
1494 buffer[n] = 0;
1495 return simple_read_from_buffer(buf, count, ppos, buffer, n);
1496}
1497
1498static const struct file_operations audpcm_debug_fops = {
1499 .read = audpcm_debug_read,
1500 .open = audpcm_debug_open,
1501};
1502#endif
1503
1504static int audio_open(struct inode *inode, struct file *file)
1505{
1506 struct audio *audio = NULL;
1507 int rc, i, dec_attrb, decid;
1508 struct audpcm_event *e_node = NULL;
1509 unsigned pmem_sz = DMASZ_MAX;
1510
1511#ifdef CONFIG_DEBUG_FS
1512 /* 4 bytes represents decoder number, 1 byte for terminate string */
1513 char name[sizeof "msm_pcm_dec_" + 5];
1514#endif
1515
1516 /* Allocate audio instance, set to zero */
1517 audio = kzalloc(sizeof(struct audio), GFP_KERNEL);
1518 if (!audio) {
1519 MM_ERR("no memory to allocate audio instance \n");
1520 rc = -ENOMEM;
1521 goto done;
1522 }
1523 MM_DBG("audio instance 0x%08x created\n", (int)audio);
1524
1525 /* Allocate the decoder */
1526 dec_attrb = AUDDEC_DEC_PCM;
1527 if (file->f_mode & FMODE_READ) {
1528 MM_ERR("Non-Tunneled mode not supported\n");
1529 rc = -EPERM;
1530 kfree(audio);
1531 goto done;
1532 } else
1533 dec_attrb |= MSM_AUD_MODE_TUNNEL;
1534
1535 decid = audpp_adec_alloc(dec_attrb, &audio->module_name,
1536 &audio->queue_id);
1537 if (decid < 0) {
1538 MM_ERR("No free decoder available\n");
1539 rc = -ENODEV;
1540 MM_DBG("audio instance 0x%08x freeing\n", (int)audio);
1541 kfree(audio);
1542 goto done;
1543 }
1544 audio->dec_id = decid & MSM_AUD_DECODER_MASK;
1545
1546 /* Non AIO interface */
1547 if (!(file->f_flags & O_NONBLOCK)) {
1548 while (pmem_sz >= DMASZ_MIN) {
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05301549 MM_DBG("pmemsz = %d\n", pmem_sz);
1550 audio->phys = allocate_contiguous_ebi_nomap(pmem_sz,
1551 SZ_4K);
1552 if (audio->phys) {
Laura Abbott35111d32012-04-27 18:41:48 -07001553 audio->map_v_write = ioremap(
1554 audio->phys, pmem_sz);
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05301555 if (IS_ERR(audio->map_v_write)) {
1556 MM_ERR("could not map write\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001557 buffers\n");
1558 rc = -ENOMEM;
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05301559 free_contiguous_memory_by_paddr(
1560 audio->phys);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001561 audpp_adec_free(audio->dec_id);
1562 MM_DBG("audio instance 0x%08x\
1563 freeing\n", (int)audio);
1564 kfree(audio);
1565 goto done;
1566 }
Laura Abbott35111d32012-04-27 18:41:48 -07001567 audio->data = audio->map_v_write;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001568 MM_DBG("write buf: phy addr 0x%08x kernel addr\
1569 0x%08x\n", audio->phys,\
1570 (int)audio->data);
1571 break;
1572 } else if (pmem_sz == DMASZ_MIN) {
1573 MM_ERR("could not allocate write buffers\n");
1574 rc = -ENOMEM;
1575 audpp_adec_free(audio->dec_id);
1576 MM_DBG("audio instance 0x%08x freeing\n",\
1577 (int)audio);
1578 kfree(audio);
1579 goto done;
1580 } else
1581 pmem_sz >>= 1;
1582 }
1583 audio->out_dma_sz = pmem_sz;
1584 }
1585
1586 rc = audmgr_open(&audio->audmgr);
1587 if (rc)
1588 goto err;
1589
1590 rc = msm_adsp_get(audio->module_name, &audio->audplay,
1591 &audpcmdec_adsp_ops, audio);
1592 if (rc) {
1593 MM_ERR("failed to get %s module\n", audio->module_name);
1594 audmgr_close(&audio->audmgr);
1595 goto err;
1596 }
1597
1598 rc = rmt_get_resource(audio);
1599 if (rc) {
1600 MM_ERR("ADSP resources are not available for PCM session \
1601 0x%08x on decoder: %d\n", (int)audio, audio->dec_id);
1602 audmgr_close(&audio->audmgr);
1603 msm_adsp_put(audio->audplay);
1604 goto err;
1605 }
1606
1607 if (file->f_flags & O_NONBLOCK) {
1608 MM_DBG("set to aio interface\n");
1609 audio->drv_status |= ADRV_STATUS_AIO_INTF;
1610 audio->drv_ops.send_data = audpcm_async_send_data;
1611 audio->drv_ops.out_flush = audpcm_async_flush;
1612 audio->drv_ops.fsync = audpcm_async_fsync;
1613 } else {
1614 MM_DBG("set to std io interface\n");
1615 audio->drv_ops.send_data = audplay_send_data;
1616 audio->drv_ops.out_flush = audio_flush;
1617 audio->drv_ops.fsync = audpcm_sync_fsync;
1618 audio->out[0].data = audio->data + 0;
1619 audio->out[0].addr = audio->phys + 0;
1620 audio->out[0].size = (audio->out_dma_sz >> 1);
1621
1622 audio->out[1].data = audio->data + audio->out[0].size;
1623 audio->out[1].addr = audio->phys + audio->out[0].size;
1624 audio->out[1].size = audio->out[0].size;
1625 }
1626
1627 /* Initialize all locks of audio instance */
1628 mutex_init(&audio->lock);
1629 mutex_init(&audio->write_lock);
1630 mutex_init(&audio->get_event_lock);
1631 spin_lock_init(&audio->dsp_lock);
1632 init_waitqueue_head(&audio->write_wait);
1633 INIT_LIST_HEAD(&audio->out_queue);
1634 INIT_LIST_HEAD(&audio->pmem_region_queue);
1635 INIT_LIST_HEAD(&audio->free_event_queue);
1636 INIT_LIST_HEAD(&audio->event_queue);
1637 init_waitqueue_head(&audio->wait);
1638 init_waitqueue_head(&audio->event_wait);
1639 spin_lock_init(&audio->event_queue_lock);
1640
1641 audio->out_sample_rate = 44100;
1642 audio->out_channel_mode = AUDPP_CMD_PCM_INTF_STEREO_V;
1643 audio->out_bits = AUDPP_CMD_WAV_PCM_WIDTH_16;
1644 audio->volume = 0x2000;
1645 audio->drv_ops.out_flush(audio);
1646
1647 file->private_data = audio;
1648 audio->opened = 1;
1649
1650#ifdef CONFIG_DEBUG_FS
1651 snprintf(name, sizeof name, "msm_pcm_dec_%04x", audio->dec_id);
1652 audio->dentry = debugfs_create_file(name, S_IFREG | S_IRUGO,
1653 NULL, (void *) audio, &audpcm_debug_fops);
1654
1655 if (IS_ERR(audio->dentry))
1656 MM_DBG("debugfs_create_file failed\n");
1657#endif
1658#ifdef CONFIG_HAS_EARLYSUSPEND
1659 audio->suspend_ctl.node.level = EARLY_SUSPEND_LEVEL_DISABLE_FB;
1660 audio->suspend_ctl.node.resume = audpcm_resume;
1661 audio->suspend_ctl.node.suspend = audpcm_suspend;
1662 audio->suspend_ctl.audio = audio;
1663 register_early_suspend(&audio->suspend_ctl.node);
1664#endif
1665 for (i = 0; i < AUDPCM_EVENT_NUM; i++) {
1666 e_node = kmalloc(sizeof(struct audpcm_event), GFP_KERNEL);
1667 if (e_node)
1668 list_add_tail(&e_node->list, &audio->free_event_queue);
1669 else {
1670 MM_ERR("event pkt alloc failed\n");
1671 break;
1672 }
1673 }
1674done:
1675 return rc;
1676err:
1677 if (audio->data) {
Laura Abbott35111d32012-04-27 18:41:48 -07001678 iounmap(audio->map_v_write);
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05301679 free_contiguous_memory_by_paddr(audio->phys);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001680 }
1681 audpp_adec_free(audio->dec_id);
1682 MM_DBG("audio instance 0x%08x freeing\n", (int)audio);
1683 kfree(audio);
1684 return rc;
1685}
1686
1687static const struct file_operations audio_pcm_fops = {
1688 .owner = THIS_MODULE,
1689 .open = audio_open,
1690 .release = audio_release,
1691 .write = audio_write,
1692 .unlocked_ioctl = audio_ioctl,
1693 .fsync = audpcm_fsync,
1694};
1695
1696struct miscdevice audio_pcm_misc = {
1697 .minor = MISC_DYNAMIC_MINOR,
1698 .name = "msm_pcm_dec",
1699 .fops = &audio_pcm_fops,
1700};
1701
1702static int __init audio_init(void)
1703{
1704 return misc_register(&audio_pcm_misc);
1705}
1706
1707device_initcall(audio_init);