blob: ed946f98cad3f5d64a341c4effa73a991e153934 [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/*
Duy Truong790f06d2013-02-13 16:38:12 -08002 * Copyright (c) 2008-2012, The Linux Foundation. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003 *
4 * This code also borrows from audio_aac.c, which is
5 * Copyright (C) 2008 Google, Inc.
6 * Copyright (C) 2008 HTC Corporation
7 *
8 * This software is licensed under the terms of the GNU General Public
9 * License version 2, as published by the Free Software Foundation, and
10 * may be copied, distributed, and modified under those terms.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 *
16 * See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, you can find it at http://www.fsf.org.
19 */
20
Santosh Mardifdc227a2011-07-11 17:20:34 +053021#include <asm/atomic.h>
22#include <asm/ioctls.h>
23
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070024#include <linux/module.h>
25#include <linux/fs.h>
26#include <linux/miscdevice.h>
27#include <linux/uaccess.h>
28#include <linux/kthread.h>
29#include <linux/wait.h>
30#include <linux/dma-mapping.h>
31#include <linux/debugfs.h>
32#include <linux/delay.h>
33#include <linux/list.h>
34#include <linux/earlysuspend.h>
Santosh Mardifdc227a2011-07-11 17:20:34 +053035#include <linux/memory_alloc.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070036#include <linux/msm_audio.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070037#include <linux/slab.h>
Santosh Mardifdc227a2011-07-11 17:20:34 +053038
39#include <mach/msm_adsp.h>
40#include <mach/iommu.h>
41#include <mach/iommu_domains.h>
Santosh Mardifdc227a2011-07-11 17:20:34 +053042#include <mach/qdsp5v2/audio_dev_ctl.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070043#include <mach/qdsp5v2/qdsp5audppmsg.h>
44#include <mach/qdsp5v2/qdsp5audplaycmdi.h>
45#include <mach/qdsp5v2/qdsp5audplaymsg.h>
46#include <mach/qdsp5v2/audio_dev_ctl.h>
47#include <mach/qdsp5v2/audpp.h>
48#include <mach/debug_mm.h>
Santosh Mardifdc227a2011-07-11 17:20:34 +053049#include <mach/msm_memtypes.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070050
51/* Hold 30 packets of 24 bytes each and 14 bytes of meta in */
52#define BUFSZ 734
53#define DMASZ (BUFSZ * 2)
54
55#define AUDDEC_DEC_EVRC 12
56
57#define PCM_BUFSZ_MIN 1624 /* 100ms worth of data and
58 and 24 bytes of meta out */
59#define PCM_BUF_MAX_COUNT 5
60/* DSP only accepts 5 buffers at most
61 * but support 2 buffers currently
62 */
63#define EVRC_DECODED_FRSZ 320 /* EVRC 20ms 8KHz mono PCM size */
64
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 AUDEVRC_METAFIELD_MASK 0xFFFF0000
74#define AUDEVRC_EOS_FLG_OFFSET 0x0A /* Offset from beginning of buffer */
75#define AUDEVRC_EOS_FLG_MASK 0x01
76#define AUDEVRC_EOS_NONE 0x0 /* No EOS detected */
77#define AUDEVRC_EOS_SET 0x1 /* EOS set in meta field */
78
79#define AUDEVRC_EVENT_NUM 10 /* Default number of pre-allocated event packets */
80
81struct buffer {
82 void *data;
83 unsigned size;
84 unsigned used; /* Input usage actual DSP produced PCM size */
85 unsigned addr;
86 unsigned short mfield_sz; /*only useful for data has meta field */
87};
88
89#ifdef CONFIG_HAS_EARLYSUSPEND
90struct audevrc_suspend_ctl {
91 struct early_suspend node;
92 struct audio *audio;
93};
94#endif
95
96struct audevrc_event{
97 struct list_head list;
98 int event_type;
99 union msm_audio_event_payload payload;
100};
101
102struct audio {
103 struct buffer out[2];
104
105 spinlock_t dsp_lock;
106
107 uint8_t out_head;
108 uint8_t out_tail;
109 uint8_t out_needed; /* number of buffers the dsp is waiting for */
110
111 atomic_t out_bytes;
112
113 struct mutex lock;
114 struct mutex write_lock;
115 wait_queue_head_t write_wait;
116
117 /* Host PCM section */
118 struct buffer in[PCM_BUF_MAX_COUNT];
119 struct mutex read_lock;
120 wait_queue_head_t read_wait; /* Wait queue for read */
121 char *read_data; /* pointer to reader buffer */
122 int32_t read_phys; /* physical address of reader buffer */
123 uint8_t read_next; /* index to input buffers to be read next */
124 uint8_t fill_next; /* index to buffer that DSP should be filling */
125 uint8_t pcm_buf_count; /* number of pcm buffer allocated */
126 /* ---- End of Host PCM section */
127
128 struct msm_adsp_module *audplay;
129
130 /* data allocated for various buffers */
131 char *data;
132 int32_t phys; /* physical address of write buffer */
Laura Abbott61399692012-04-30 14:25:46 -0700133 void *map_v_read;
134 void *map_v_write;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700135
136 int mfield; /* meta field embedded in data */
137 int rflush; /* Read flush */
138 int wflush; /* Write flush */
139 uint8_t opened:1;
140 uint8_t enabled:1;
141 uint8_t running:1;
142 uint8_t stopped:1; /* set when stopped, cleared on flush */
143 uint8_t pcm_feedback:1;
144 uint8_t buf_refresh:1;
145 int teos; /* valid only if tunnel mode & no data left for decoder */
146 enum msm_aud_decoder_state dec_state; /* Represents decoder state */
147
148 const char *module_name;
149 unsigned queue_id;
150 uint16_t dec_id;
151 uint32_t read_ptr_offset;
152 int16_t source;
153
154#ifdef CONFIG_HAS_EARLYSUSPEND
155 struct audevrc_suspend_ctl suspend_ctl;
156#endif
157
158#ifdef CONFIG_DEBUG_FS
159 struct dentry *dentry;
160#endif
161
162 wait_queue_head_t wait;
163 struct list_head free_event_queue;
164 struct list_head event_queue;
165 wait_queue_head_t event_wait;
166 spinlock_t event_queue_lock;
167 struct mutex get_event_lock;
168 int event_abort;
169 /* AV sync Info */
170 int avsync_flag; /* Flag to indicate feedback from DSP */
171 wait_queue_head_t avsync_wait;/* Wait queue for AV Sync Message */
172 /* flags, 48 bits sample/bytes counter per channel */
173 uint16_t avsync[AUDPP_AVSYNC_CH_COUNT * AUDPP_AVSYNC_NUM_WORDS + 1];
174
175 uint32_t device_events;
176
177 int eq_enable;
178 int eq_needs_commit;
179 struct audpp_cmd_cfg_object_params_eqalizer eq;
180 struct audpp_cmd_cfg_object_params_volume vol_pan;
181};
182
183static int auddec_dsp_config(struct audio *audio, int enable);
184static void audpp_cmd_cfg_adec_params(struct audio *audio);
185static void audpp_cmd_cfg_routing_mode(struct audio *audio);
186static void audevrc_send_data(struct audio *audio, unsigned needed);
187static void audevrc_dsp_event(void *private, unsigned id, uint16_t *msg);
188static void audevrc_config_hostpcm(struct audio *audio);
189static void audevrc_buffer_refresh(struct audio *audio);
Vinay Vaka051db722012-01-24 19:48:32 +0530190#ifdef CONFIG_HAS_EARLYSUSPEND
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700191static void audevrc_post_event(struct audio *audio, int type,
192 union msm_audio_event_payload payload);
Vinay Vaka051db722012-01-24 19:48:32 +0530193#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700194
195/* must be called with audio->lock held */
196static int audevrc_enable(struct audio *audio)
197{
198 if (audio->enabled)
199 return 0;
200
201 audio->dec_state = MSM_AUD_DECODER_STATE_NONE;
202 audio->out_tail = 0;
203 audio->out_needed = 0;
204
205 if (msm_adsp_enable(audio->audplay)) {
206 MM_ERR("msm_adsp_enable(audplay) failed\n");
207 return -ENODEV;
208 }
209
210 if (audpp_enable(audio->dec_id, audevrc_dsp_event, audio)) {
211 MM_ERR("audpp_enable() failed\n");
212 msm_adsp_disable(audio->audplay);
213 return -ENODEV;
214 }
215 audio->enabled = 1;
216 return 0;
217}
218
219static void evrc_listner(u32 evt_id, union auddev_evt_data *evt_payload,
220 void *private_data)
221{
222 struct audio *audio = (struct audio *) private_data;
223 switch (evt_id) {
224 case AUDDEV_EVT_DEV_RDY:
225 MM_DBG(":AUDDEV_EVT_DEV_RDY\n");
226 audio->source |= (0x1 << evt_payload->routing_id);
227 if (audio->running == 1 && audio->enabled == 1)
228 audpp_route_stream(audio->dec_id, audio->source);
229 break;
230 case AUDDEV_EVT_DEV_RLS:
231 MM_DBG(":AUDDEV_EVT_DEV_RLS\n");
232 audio->source &= ~(0x1 << evt_payload->routing_id);
233 if (audio->running == 1 && audio->enabled == 1)
234 audpp_route_stream(audio->dec_id, audio->source);
235 break;
236 case AUDDEV_EVT_STREAM_VOL_CHG:
237 audio->vol_pan.volume = evt_payload->session_vol;
238 MM_DBG(":AUDDEV_EVT_STREAM_VOL_CHG, stream vol %d\n",
239 audio->vol_pan.volume);
240 if (audio->running)
241 audpp_dsp_set_vol_pan(audio->dec_id, &audio->vol_pan,
242 POPP);
243 break;
244 default:
245 MM_ERR(":ERROR:wrong event\n");
246 break;
247 }
248}
249/* must be called with audio->lock held */
250static int audevrc_disable(struct audio *audio)
251{
252 int rc = 0;
253 if (audio->enabled) {
254 audio->enabled = 0;
255 audio->dec_state = MSM_AUD_DECODER_STATE_NONE;
256 auddec_dsp_config(audio, 0);
257 rc = wait_event_interruptible_timeout(audio->wait,
258 audio->dec_state != MSM_AUD_DECODER_STATE_NONE,
259 msecs_to_jiffies(MSM_AUD_DECODER_WAIT_MS));
260 if (rc == 0)
261 rc = -ETIMEDOUT;
262 else if (audio->dec_state != MSM_AUD_DECODER_STATE_CLOSE)
263 rc = -EFAULT;
264 else
265 rc = 0;
266 wake_up(&audio->write_wait);
267 wake_up(&audio->read_wait);
268 msm_adsp_disable(audio->audplay);
269 audpp_disable(audio->dec_id, audio);
270 audio->out_needed = 0;
271 }
272 return rc;
273}
274
275/* ------------------- dsp --------------------- */
276
277static void audevrc_update_pcm_buf_entry(struct audio *audio,
278 uint32_t *payload)
279{
280 uint8_t index;
281 unsigned long flags;
282
283 if (audio->rflush)
284 return;
285
286 spin_lock_irqsave(&audio->dsp_lock, flags);
287 for (index = 0; index < payload[1]; index++) {
288 if (audio->in[audio->fill_next].addr
289 == payload[2 + index * 2]) {
290 MM_DBG("in[%d] ready\n", audio->fill_next);
291 audio->in[audio->fill_next].used =
292 payload[3 + index * 2];
293 if ((++audio->fill_next) == audio->pcm_buf_count)
294 audio->fill_next = 0;
295
296 } else {
297 MM_ERR("expected=%x ret=%x\n",
298 audio->in[audio->fill_next].addr,
299 payload[1 + index * 2]);
300 break;
301 }
302 }
303 if (audio->in[audio->fill_next].used == 0) {
304 audevrc_buffer_refresh(audio);
305 } else {
306 MM_DBG("read cannot keep up\n");
307 audio->buf_refresh = 1;
308 }
309 wake_up(&audio->read_wait);
310 spin_unlock_irqrestore(&audio->dsp_lock, flags);
311}
312
313static void audplay_dsp_event(void *data, unsigned id, size_t len,
314 void (*getevent) (void *ptr, size_t len))
315{
316 struct audio *audio = data;
317 uint32_t msg[28];
318 getevent(msg, sizeof(msg));
319
320 MM_DBG("msg_id=%x\n", id);
321 switch (id) {
322 case AUDPLAY_MSG_DEC_NEEDS_DATA:
323 audevrc_send_data(audio, 1);
324 break;
325 case AUDPLAY_MSG_BUFFER_UPDATE:
326 MM_DBG("\n"); /* Macro prints the file name and function */
327 audevrc_update_pcm_buf_entry(audio, msg);
328 break;
329 case ADSP_MESSAGE_ID:
330 MM_DBG("Received ADSP event: module enable(audplaytask)\n");
331 break;
332 default:
333 MM_ERR("unexpected message from decoder \n");
334 }
335}
336
337static void audevrc_dsp_event(void *private, unsigned id, uint16_t *msg)
338{
339 struct audio *audio = private;
340
341 switch (id) {
342 case AUDPP_MSG_STATUS_MSG:{
343 unsigned status = msg[1];
344
345 switch (status) {
346 case AUDPP_DEC_STATUS_SLEEP: {
347 uint16_t reason = msg[2];
348 MM_DBG("decoder status:sleep reason = \
349 0x%04x\n", reason);
350 if ((reason == AUDPP_MSG_REASON_MEM)
351 || (reason ==
352 AUDPP_MSG_REASON_NODECODER)) {
353 audio->dec_state =
354 MSM_AUD_DECODER_STATE_FAILURE;
355 wake_up(&audio->wait);
356 } else if (reason == AUDPP_MSG_REASON_NONE) {
357 /* decoder is in disable state */
358 audio->dec_state =
359 MSM_AUD_DECODER_STATE_CLOSE;
360 wake_up(&audio->wait);
361 }
362 break;
363 }
364 case AUDPP_DEC_STATUS_INIT:
365 MM_DBG("decoder status: init \n");
366 if (audio->pcm_feedback)
367 audpp_cmd_cfg_routing_mode(audio);
368 else
369 audpp_cmd_cfg_adec_params(audio);
370 break;
371
372 case AUDPP_DEC_STATUS_CFG:
373 MM_DBG("decoder status: cfg \n");
374 break;
375 case AUDPP_DEC_STATUS_PLAY:
376 MM_DBG("decoder status: play \n");
377 audpp_route_stream(audio->dec_id,
378 audio->source);
379 if (audio->pcm_feedback) {
380 audevrc_config_hostpcm(audio);
381 audevrc_buffer_refresh(audio);
382 }
383 audio->dec_state =
384 MSM_AUD_DECODER_STATE_SUCCESS;
385 wake_up(&audio->wait);
386 break;
387 default:
388 MM_ERR("unknown decoder status \n");
389 }
390 break;
391 }
392 case AUDPP_MSG_CFG_MSG:
393 if (msg[0] == AUDPP_MSG_ENA_ENA) {
394 MM_DBG("CFG_MSG ENABLE\n");
395 auddec_dsp_config(audio, 1);
396 audio->out_needed = 0;
397 audio->running = 1;
398 audpp_dsp_set_vol_pan(audio->dec_id, &audio->vol_pan,
399 POPP);
400 audpp_dsp_set_eq(audio->dec_id, audio->eq_enable,
401 &audio->eq, POPP);
402 } else if (msg[0] == AUDPP_MSG_ENA_DIS) {
403 MM_DBG("CFG_MSG DISABLE\n");
404 audio->running = 0;
405 } else {
406 MM_DBG("CFG_MSG %d?\n", msg[0]);
407 }
408 break;
409 case AUDPP_MSG_ROUTING_ACK:
410 MM_DBG("ROUTING_ACK\n");
411 audpp_cmd_cfg_adec_params(audio);
412 break;
413 case AUDPP_MSG_FLUSH_ACK:
414 MM_DBG("FLUSH_ACK\n");
415 audio->wflush = 0;
416 audio->rflush = 0;
417 wake_up(&audio->write_wait);
418 if (audio->pcm_feedback)
419 audevrc_buffer_refresh(audio);
420 break;
421 case AUDPP_MSG_PCMDMAMISSED:
422 MM_DBG("PCMDMAMISSED\n");
423 audio->teos = 1;
424 wake_up(&audio->write_wait);
425 break;
426
427 case AUDPP_MSG_AVSYNC_MSG:
428 MM_DBG("AUDPP_MSG_AVSYNC_MSG\n");
429 memcpy(&audio->avsync[0], msg, sizeof(audio->avsync));
430 audio->avsync_flag = 1;
431 wake_up(&audio->avsync_wait);
432 break;
433
434 default:
435 MM_ERR("UNKNOWN (%d)\n", id);
436 }
437
438}
439
440struct msm_adsp_ops audplay_adsp_ops_evrc = {
441 .event = audplay_dsp_event,
442};
443
444#define audplay_send_queue0(audio, cmd, len) \
445 msm_adsp_write(audio->audplay, audio->queue_id, \
446 cmd, len)
447
448static int auddec_dsp_config(struct audio *audio, int enable)
449{
450 struct audpp_cmd_cfg_dec_type cfg_dec_cmd;
451
452 memset(&cfg_dec_cmd, 0, sizeof(cfg_dec_cmd));
453
454 cfg_dec_cmd.cmd_id = AUDPP_CMD_CFG_DEC_TYPE;
455 if (enable)
456 cfg_dec_cmd.dec_cfg = AUDPP_CMD_UPDATDE_CFG_DEC |
457 AUDPP_CMD_ENA_DEC_V | AUDDEC_DEC_EVRC;
458 else
459 cfg_dec_cmd.dec_cfg = AUDPP_CMD_UPDATDE_CFG_DEC |
460 AUDPP_CMD_DIS_DEC_V;
461 cfg_dec_cmd.dm_mode = 0x0;
462 cfg_dec_cmd.stream_id = audio->dec_id;
463
464 return audpp_send_queue1(&cfg_dec_cmd, sizeof(cfg_dec_cmd));
465}
466
467static void audpp_cmd_cfg_adec_params(struct audio *audio)
468{
469 struct audpp_cmd_cfg_adec_params_evrc cmd;
470
471 memset(&cmd, 0, sizeof(cmd));
472 cmd.common.cmd_id = AUDPP_CMD_CFG_ADEC_PARAMS;
473 cmd.common.length = sizeof(cmd);
474 cmd.common.dec_id = audio->dec_id;
475 cmd.common.input_sampling_frequency = 8000;
476 cmd.stereo_cfg = AUDPP_CMD_PCM_INTF_MONO_V;
477
478 audpp_send_queue2(&cmd, sizeof(cmd));
479}
480
481static void audpp_cmd_cfg_routing_mode(struct audio *audio)
482{
483 struct audpp_cmd_routing_mode cmd;
484 MM_DBG("\n"); /* Macro prints the file name and function */
485 memset(&cmd, 0, sizeof(cmd));
486 cmd.cmd_id = AUDPP_CMD_ROUTING_MODE;
487 cmd.object_number = audio->dec_id;
488 if (audio->pcm_feedback)
489 cmd.routing_mode = ROUTING_MODE_FTRT;
490 else
491 cmd.routing_mode = ROUTING_MODE_RT;
492
493 audpp_send_queue1(&cmd, sizeof(cmd));
494}
495
496static int audplay_dsp_send_data_avail(struct audio *audio,
497 unsigned idx, unsigned len)
498{
499 struct audplay_cmd_bitstream_data_avail_nt2 cmd;
500
501 cmd.cmd_id = AUDPLAY_CMD_BITSTREAM_DATA_AVAIL_NT2;
502 if (audio->mfield)
503 cmd.decoder_id = AUDEVRC_METAFIELD_MASK |
504 (audio->out[idx].mfield_sz >> 1);
505 else
506 cmd.decoder_id = audio->dec_id;
507 cmd.buf_ptr = audio->out[idx].addr;
508 cmd.buf_size = len / 2;
509 cmd.partition_number = 0;
510 return audplay_send_queue0(audio, &cmd, sizeof(cmd));
511}
512
513static void audevrc_buffer_refresh(struct audio *audio)
514{
515 struct audplay_cmd_buffer_refresh refresh_cmd;
516
517 refresh_cmd.cmd_id = AUDPLAY_CMD_BUFFER_REFRESH;
518 refresh_cmd.num_buffers = 1;
519 refresh_cmd.buf0_address = audio->in[audio->fill_next].addr;
520 refresh_cmd.buf0_length = audio->in[audio->fill_next].size;
521
522 refresh_cmd.buf_read_count = 0;
523 MM_DBG("buf0_addr=%x buf0_len=%d\n", refresh_cmd.buf0_address,
524 refresh_cmd.buf0_length);
525 audplay_send_queue0(audio, &refresh_cmd, sizeof(refresh_cmd));
526}
527
528static void audevrc_config_hostpcm(struct audio *audio)
529{
530 struct audplay_cmd_hpcm_buf_cfg cfg_cmd;
531
532 MM_DBG("\n"); /* Macro prints the file name and function */
533 cfg_cmd.cmd_id = AUDPLAY_CMD_HPCM_BUF_CFG;
534 cfg_cmd.max_buffers = 1;
535 cfg_cmd.byte_swap = 0;
536 cfg_cmd.hostpcm_config = (0x8000) | (0x4000);
537 cfg_cmd.feedback_frequency = 1;
538 cfg_cmd.partition_number = 0;
539 audplay_send_queue0(audio, &cfg_cmd, sizeof(cfg_cmd));
540
541}
542
543static void audevrc_send_data(struct audio *audio, unsigned needed)
544{
545 struct buffer *frame;
546 unsigned long flags;
547
548 spin_lock_irqsave(&audio->dsp_lock, flags);
549 if (!audio->running)
550 goto done;
551
552 if (needed && !audio->wflush) {
553 /* We were called from the callback because the DSP
554 * requested more data. Note that the DSP does want
555 * more data, and if a buffer was in-flight, mark it
556 * as available (since the DSP must now be done with
557 * it).
558 */
559 audio->out_needed = 1;
560 frame = audio->out + audio->out_tail;
561 if (frame->used == 0xffffffff) {
562 MM_DBG("frame %d free\n", audio->out_tail);
563 frame->used = 0;
564 audio->out_tail ^= 1;
565 wake_up(&audio->write_wait);
566 }
567 }
568
569 if (audio->out_needed) {
570 /* If the DSP currently wants data and we have a
571 * buffer available, we will send it and reset
572 * the needed flag. We'll mark the buffer as in-flight
573 * so that it won't be recycled until the next buffer
574 * is requested
575 */
576
577 frame = audio->out + audio->out_tail;
578 if (frame->used) {
579 BUG_ON(frame->used == 0xffffffff);
580 MM_DBG("frame %d busy\n", audio->out_tail);
581 audplay_dsp_send_data_avail(audio, audio->out_tail,
582 frame->used);
583 frame->used = 0xffffffff;
584 audio->out_needed = 0;
585 }
586 }
587done:
588 spin_unlock_irqrestore(&audio->dsp_lock, flags);
589}
590
591/* ------------------- device --------------------- */
592
593static void audevrc_flush(struct audio *audio)
594{
595 audio->out[0].used = 0;
596 audio->out[1].used = 0;
597 audio->out_head = 0;
598 audio->out_tail = 0;
599 audio->out_needed = 0;
600 atomic_set(&audio->out_bytes, 0);
601}
602
603static void audevrc_flush_pcm_buf(struct audio *audio)
604{
605 uint8_t index;
606
607 for (index = 0; index < PCM_BUF_MAX_COUNT; index++)
608 audio->in[index].used = 0;
609
610 audio->buf_refresh = 0;
611 audio->read_next = 0;
612 audio->fill_next = 0;
613}
614
615static void audevrc_ioport_reset(struct audio *audio)
616{
617 /* Make sure read/write thread are free from
618 * sleep and knowing that system is not able
619 * to process io request at the moment
620 */
621 wake_up(&audio->write_wait);
622 mutex_lock(&audio->write_lock);
623 audevrc_flush(audio);
624 mutex_unlock(&audio->write_lock);
625 wake_up(&audio->read_wait);
626 mutex_lock(&audio->read_lock);
627 audevrc_flush_pcm_buf(audio);
628 mutex_unlock(&audio->read_lock);
629 audio->avsync_flag = 1;
630 wake_up(&audio->avsync_wait);
631}
632
633static int audevrc_events_pending(struct audio *audio)
634{
635 unsigned long flags;
636 int empty;
637
638 spin_lock_irqsave(&audio->event_queue_lock, flags);
639 empty = !list_empty(&audio->event_queue);
640 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
641 return empty || audio->event_abort;
642}
643
644static void audevrc_reset_event_queue(struct audio *audio)
645{
646 unsigned long flags;
647 struct audevrc_event *drv_evt;
648 struct list_head *ptr, *next;
649
650 spin_lock_irqsave(&audio->event_queue_lock, flags);
651 list_for_each_safe(ptr, next, &audio->event_queue) {
652 drv_evt = list_first_entry(&audio->event_queue,
653 struct audevrc_event, list);
654 list_del(&drv_evt->list);
655 kfree(drv_evt);
656 }
657 list_for_each_safe(ptr, next, &audio->free_event_queue) {
658 drv_evt = list_first_entry(&audio->free_event_queue,
659 struct audevrc_event, list);
660 list_del(&drv_evt->list);
661 kfree(drv_evt);
662 }
663 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
664
665 return;
666}
667
668
669static long audevrc_process_event_req(struct audio *audio, void __user *arg)
670{
671 long rc;
672 struct msm_audio_event usr_evt;
673 struct audevrc_event *drv_evt = NULL;
674 int timeout;
675 unsigned long flags;
676
677 if (copy_from_user(&usr_evt, arg, sizeof(struct msm_audio_event)))
678 return -EFAULT;
679
680 timeout = (int) usr_evt.timeout_ms;
681
682 if (timeout > 0) {
683 rc = wait_event_interruptible_timeout(
684 audio->event_wait, audevrc_events_pending(audio),
685 msecs_to_jiffies(timeout));
686 if (rc == 0)
687 return -ETIMEDOUT;
688 } else {
689 rc = wait_event_interruptible(
690 audio->event_wait, audevrc_events_pending(audio));
691 }
692
693 if (rc < 0)
694 return rc;
695
696 if (audio->event_abort) {
697 audio->event_abort = 0;
698 return -ENODEV;
699 }
700
701 rc = 0;
702
703 spin_lock_irqsave(&audio->event_queue_lock, flags);
704 if (!list_empty(&audio->event_queue)) {
705 drv_evt = list_first_entry(&audio->event_queue,
706 struct audevrc_event, list);
707 list_del(&drv_evt->list);
708 }
709 if (drv_evt) {
710 usr_evt.event_type = drv_evt->event_type;
711 usr_evt.event_payload = drv_evt->payload;
712 list_add_tail(&drv_evt->list, &audio->free_event_queue);
713 } else
714 rc = -1;
715 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
716
717 if (!rc && copy_to_user(arg, &usr_evt, sizeof(usr_evt)))
718 rc = -EFAULT;
719
720 return rc;
721}
722
723static int audio_enable_eq(struct audio *audio, int enable)
724{
725 if (audio->eq_enable == enable && !audio->eq_needs_commit)
726 return 0;
727
728 audio->eq_enable = enable;
729
730 if (audio->running) {
731 audpp_dsp_set_eq(audio->dec_id, enable, &audio->eq, POPP);
732 audio->eq_needs_commit = 0;
733 }
734 return 0;
735}
736
737static int audio_get_avsync_data(struct audio *audio,
738 struct msm_audio_stats *stats)
739{
740 int rc = -EINVAL;
741 unsigned long flags;
742
743 local_irq_save(flags);
744 if (audio->dec_id == audio->avsync[0] && audio->avsync_flag) {
745 /* av_sync sample count */
746 stats->sample_count = (audio->avsync[2] << 16) |
747 (audio->avsync[3]);
748
749 /* av_sync byte_count */
750 stats->byte_count = (audio->avsync[5] << 16) |
751 (audio->avsync[6]);
752
753 audio->avsync_flag = 0;
754 rc = 0;
755 }
756 local_irq_restore(flags);
757 return rc;
758
759}
760
761static long audevrc_ioctl(struct file *file, unsigned int cmd,
762 unsigned long arg)
763{
764 struct audio *audio = file->private_data;
765 int rc = -EINVAL;
766 unsigned long flags = 0;
767 uint16_t enable_mask;
768 int enable;
769 int prev_state;
770
771 MM_DBG("cmd = %d\n", cmd);
772
773 if (cmd == AUDIO_GET_STATS) {
774 struct msm_audio_stats stats;
775
776 audio->avsync_flag = 0;
777 memset(&stats, 0, sizeof(stats));
778 if (audpp_query_avsync(audio->dec_id) < 0)
779 return rc;
780
781 rc = wait_event_interruptible_timeout(audio->avsync_wait,
782 (audio->avsync_flag == 1),
783 msecs_to_jiffies(AUDPP_AVSYNC_EVENT_TIMEOUT));
784
785 if (rc < 0)
786 return rc;
787 else if ((rc > 0) || ((rc == 0) && (audio->avsync_flag == 1))) {
788 if (audio_get_avsync_data(audio, &stats) < 0)
789 return rc;
790
791 if (copy_to_user((void *)arg, &stats, sizeof(stats)))
792 return -EFAULT;
793 return 0;
794 } else
795 return -EAGAIN;
796 }
797
798 switch (cmd) {
799 case AUDIO_ENABLE_AUDPP:
800 if (copy_from_user(&enable_mask, (void *) arg,
801 sizeof(enable_mask))) {
802 rc = -EFAULT;
803 break;
804 }
805
806 spin_lock_irqsave(&audio->dsp_lock, flags);
807 enable = (enable_mask & EQ_ENABLE) ? 1 : 0;
808 audio_enable_eq(audio, enable);
809 spin_unlock_irqrestore(&audio->dsp_lock, flags);
810 rc = 0;
811 break;
812 case AUDIO_SET_VOLUME:
813 spin_lock_irqsave(&audio->dsp_lock, flags);
814 audio->vol_pan.volume = arg;
815 if (audio->running)
816 audpp_dsp_set_vol_pan(audio->dec_id, &audio->vol_pan,
817 POPP);
818 spin_unlock_irqrestore(&audio->dsp_lock, flags);
819 rc = 0;
820 break;
821
822 case AUDIO_SET_PAN:
823 spin_lock_irqsave(&audio->dsp_lock, flags);
824 audio->vol_pan.pan = arg;
825 if (audio->running)
826 audpp_dsp_set_vol_pan(audio->dec_id, &audio->vol_pan,
827 POPP);
828 spin_unlock_irqrestore(&audio->dsp_lock, flags);
829 rc = 0;
830 break;
831
832 case AUDIO_SET_EQ:
833 prev_state = audio->eq_enable;
834 audio->eq_enable = 0;
835 if (copy_from_user(&audio->eq.num_bands, (void *) arg,
836 sizeof(audio->eq) -
837 (AUDPP_CMD_CFG_OBJECT_PARAMS_COMMON_LEN + 2))) {
838 rc = -EFAULT;
839 break;
840 }
841 audio->eq_enable = prev_state;
842 audio->eq_needs_commit = 1;
843 rc = 0;
844 break;
845 }
846
847 if (-EINVAL != rc)
848 return rc;
849
850 if (cmd == AUDIO_GET_EVENT) {
851 MM_DBG("AUDIO_GET_EVENT\n");
852 if (mutex_trylock(&audio->get_event_lock)) {
853 rc = audevrc_process_event_req(audio,
854 (void __user *) arg);
855 mutex_unlock(&audio->get_event_lock);
856 } else
857 rc = -EBUSY;
858 return rc;
859 }
860
861 if (cmd == AUDIO_ABORT_GET_EVENT) {
862 audio->event_abort = 1;
863 wake_up(&audio->event_wait);
864 return 0;
865 }
866
867 mutex_lock(&audio->lock);
868 switch (cmd) {
869 case AUDIO_START:
870 MM_DBG("AUDIO_START\n");
871 rc = audevrc_enable(audio);
872 if (!rc) {
873 rc = wait_event_interruptible_timeout(audio->wait,
874 audio->dec_state != MSM_AUD_DECODER_STATE_NONE,
875 msecs_to_jiffies(MSM_AUD_DECODER_WAIT_MS));
876 MM_INFO("dec_state %d rc = %d\n", audio->dec_state, rc);
877
878 if (audio->dec_state != MSM_AUD_DECODER_STATE_SUCCESS)
879 rc = -ENODEV;
880 else
881 rc = 0;
882 }
883 break;
884 case AUDIO_STOP:
885 MM_DBG("AUDIO_STOP\n");
886 rc = audevrc_disable(audio);
887 audio->stopped = 1;
888 audevrc_ioport_reset(audio);
889 audio->stopped = 0;
890 break;
891 case AUDIO_FLUSH:
892 MM_DBG("AUDIO_FLUSH\n");
893 audio->rflush = 1;
894 audio->wflush = 1;
895 audevrc_ioport_reset(audio);
896 if (audio->running) {
897 audpp_flush(audio->dec_id);
898 rc = wait_event_interruptible(audio->write_wait,
899 !audio->wflush);
900 if (rc < 0) {
901 MM_ERR("AUDIO_FLUSH interrupted\n");
902 rc = -EINTR;
903 }
904 } else {
905 audio->rflush = 0;
906 audio->wflush = 0;
907 }
908 break;
909 case AUDIO_SET_CONFIG:{
910 struct msm_audio_config config;
911 if (copy_from_user
912 (&config, (void *)arg, sizeof(config))) {
913 rc = -EFAULT;
914 break;
915 }
916 audio->mfield = config.meta_field;
917 rc = 0;
918 MM_DBG("AUDIO_SET_CONFIG applicable only \
919 for meta field configuration\n");
920 break;
921 }
922 case AUDIO_GET_CONFIG:{
923 struct msm_audio_config config;
924 config.buffer_size = BUFSZ;
925 config.buffer_count = 2;
926 config.sample_rate = 8000;
927 config.channel_count = 1;
928 config.meta_field = 0;
929 config.unused[0] = 0;
930 config.unused[1] = 0;
931 config.unused[2] = 0;
932 if (copy_to_user((void *)arg, &config, sizeof(config)))
933 rc = -EFAULT;
934 else
935 rc = 0;
936 break;
937 }
938 case AUDIO_GET_PCM_CONFIG:{
939 struct msm_audio_pcm_config config;
940 config.pcm_feedback = audio->pcm_feedback;
941 config.buffer_count = PCM_BUF_MAX_COUNT;
942 config.buffer_size = PCM_BUFSZ_MIN;
943 if (copy_to_user((void *)arg, &config, sizeof(config)))
944 rc = -EFAULT;
945 else
946 rc = 0;
947 break;
948 }
949 case AUDIO_SET_PCM_CONFIG:{
950 struct msm_audio_pcm_config config;
951 if (copy_from_user
952 (&config, (void *)arg, sizeof(config))) {
953 rc = -EFAULT;
954 break;
955 }
956 if (config.pcm_feedback != audio->pcm_feedback) {
957 MM_ERR("Not sufficient permission to"
958 "change the playback mode\n");
959 rc = -EACCES;
960 break;
961 }
962 if ((config.buffer_count > PCM_BUF_MAX_COUNT) ||
963 (config.buffer_count == 1))
964 config.buffer_count = PCM_BUF_MAX_COUNT;
965
966 if (config.buffer_size < PCM_BUFSZ_MIN)
967 config.buffer_size = PCM_BUFSZ_MIN;
968
969 /* Check if pcm feedback is required */
970 if ((config.pcm_feedback) && (!audio->read_data)) {
971 MM_DBG("allocate PCM buf %d\n",
972 config.buffer_count *
973 config.buffer_size);
Santosh Mardifdc227a2011-07-11 17:20:34 +0530974 audio->read_phys =
975 allocate_contiguous_ebi_nomap(
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700976 config.buffer_size *
977 config.buffer_count,
Santosh Mardifdc227a2011-07-11 17:20:34 +0530978 SZ_4K);
979 if (!audio->read_phys) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700980 rc = -ENOMEM;
981 break;
982 }
Laura Abbott61399692012-04-30 14:25:46 -0700983 audio->map_v_read = ioremap(
Santosh Mardifdc227a2011-07-11 17:20:34 +0530984 audio->read_phys,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700985 config.buffer_size *
Laura Abbott61399692012-04-30 14:25:46 -0700986 config.buffer_count);
Santosh Mardifdc227a2011-07-11 17:20:34 +0530987 if (IS_ERR(audio->map_v_read)) {
988 MM_ERR("failed to map read"
989 " phy address\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700990 rc = -ENOMEM;
Santosh Mardifdc227a2011-07-11 17:20:34 +0530991 free_contiguous_memory_by_paddr(
992 audio->read_phys);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700993 } else {
994 uint8_t index;
995 uint32_t offset = 0;
Santosh Mardifdc227a2011-07-11 17:20:34 +0530996 audio->read_data =
Laura Abbott61399692012-04-30 14:25:46 -0700997 audio->map_v_read;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700998 audio->buf_refresh = 0;
999 audio->pcm_buf_count =
1000 config.buffer_count;
1001 audio->read_next = 0;
1002 audio->fill_next = 0;
1003
1004 for (index = 0;
1005 index < config.buffer_count;
1006 index++) {
1007 audio->in[index].data =
1008 audio->read_data + offset;
1009 audio->in[index].addr =
1010 audio->read_phys + offset;
1011 audio->in[index].size =
1012 config.buffer_size;
1013 audio->in[index].used = 0;
1014 offset += config.buffer_size;
1015 }
1016 MM_DBG("read buf: phy addr \
1017 0x%08x kernel addr 0x%08x\n",
1018 audio->read_phys,
1019 (int)audio->read_data);
1020 rc = 0;
1021 }
1022 } else {
1023 rc = 0;
1024 }
1025 break;
1026 }
1027 case AUDIO_PAUSE:
1028 MM_DBG("AUDIO_PAUSE %ld\n", arg);
1029 rc = audpp_pause(audio->dec_id, (int) arg);
1030 break;
1031 case AUDIO_GET_SESSION_ID:
1032 if (copy_to_user((void *) arg, &audio->dec_id,
1033 sizeof(unsigned short)))
1034 rc = -EFAULT;
1035 else
1036 rc = 0;
1037 break;
1038 default:
1039 rc = -EINVAL;
1040 }
1041 mutex_unlock(&audio->lock);
1042 return rc;
1043}
1044
1045/* Only useful in tunnel-mode */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001046static int audevrc_fsync(struct file *file, loff_t ppos1, loff_t ppos2, int datasync)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001047{
1048 struct audio *audio = file->private_data;
1049 int rc = 0;
1050
1051 MM_DBG("\n"); /* Macro prints the file name and function */
1052 if (!audio->running || audio->pcm_feedback) {
1053 rc = -EINVAL;
1054 goto done_nolock;
1055 }
1056
1057 mutex_lock(&audio->write_lock);
1058
1059 rc = wait_event_interruptible(audio->write_wait,
1060 (!audio->out[0].used &&
1061 !audio->out[1].used &&
1062 audio->out_needed) || audio->wflush);
1063
1064 if (rc < 0)
1065 goto done;
1066 else if (audio->wflush) {
1067 rc = -EBUSY;
1068 goto done;
1069 }
1070
1071 /* pcm dmamiss message is sent continously
1072 * when decoder is starved so no race
1073 * condition concern
1074 */
1075 audio->teos = 0;
1076
1077 rc = wait_event_interruptible(audio->write_wait,
1078 audio->teos || audio->wflush);
1079
1080 if (audio->wflush)
1081 rc = -EBUSY;
1082
1083done:
1084 mutex_unlock(&audio->write_lock);
1085done_nolock:
1086 return rc;
1087}
1088
1089static ssize_t audevrc_read(struct file *file, char __user *buf, size_t count,
1090 loff_t *pos)
1091{
1092 struct audio *audio = file->private_data;
1093 const char __user *start = buf;
1094 int rc = 0;
1095 if (!audio->pcm_feedback) {
1096 return 0;
1097 /* PCM feedback is not enabled. Nothing to read */
1098 }
1099 mutex_lock(&audio->read_lock);
1100 MM_DBG("\n"); /* Macro prints the file name and function */
1101 while (count > 0) {
1102 rc = wait_event_interruptible(audio->read_wait,
1103 (audio->in[audio->read_next].used > 0) ||
1104 (audio->stopped) || (audio->rflush));
1105
1106 MM_DBG("wait terminated \n");
1107 if (rc < 0)
1108 break;
1109 if (audio->stopped || audio->rflush) {
1110 rc = -EBUSY;
1111 break;
1112 }
1113 if (count < audio->in[audio->read_next].used) {
1114 /* Read must happen in frame boundary. Since driver does
1115 * not know frame size, read count must be greater or
1116 * equal to size of PCM samples
1117 */
1118 MM_DBG("read stop - partial frame\n");
1119 break;
1120 } else {
1121 MM_DBG("read from in[%d]\n", audio->read_next);
1122 if (copy_to_user
1123 (buf, audio->in[audio->read_next].data,
1124 audio->in[audio->read_next].used)) {
1125 MM_ERR("invalid addr %x \n",
1126 (unsigned int)buf);
1127 rc = -EFAULT;
1128 break;
1129 }
1130 count -= audio->in[audio->read_next].used;
1131 buf += audio->in[audio->read_next].used;
1132 audio->in[audio->read_next].used = 0;
1133 if ((++audio->read_next) == audio->pcm_buf_count)
1134 audio->read_next = 0;
1135 break;
1136 /* Force to exit while loop
1137 * to prevent output thread
1138 * sleep too long if data is
1139 * not ready at this moment
1140 */
1141
1142 }
1143 }
1144 /* don't feed output buffer to HW decoder during flushing
1145 * buffer refresh command will be sent once flush completes
1146 * send buf refresh command here can confuse HW decoder
1147 */
1148 if (audio->buf_refresh && !audio->rflush) {
1149 audio->buf_refresh = 0;
1150 MM_DBG("kick start pcm feedback again\n");
1151 audevrc_buffer_refresh(audio);
1152 }
1153 mutex_unlock(&audio->read_lock);
1154 if (buf > start)
1155 rc = buf - start;
1156 MM_DBG("read %d bytes\n", rc);
1157 return rc;
1158}
1159
1160static int audevrc_process_eos(struct audio *audio,
1161 const char __user *buf_start, unsigned short mfield_size)
1162{
1163 int rc = 0;
1164 struct buffer *frame;
1165
1166 frame = audio->out + audio->out_head;
1167
1168 rc = wait_event_interruptible(audio->write_wait,
1169 (audio->out_needed &&
1170 audio->out[0].used == 0 &&
1171 audio->out[1].used == 0)
1172 || (audio->stopped)
1173 || (audio->wflush));
1174
1175 if (rc < 0)
1176 goto done;
1177 if (audio->stopped || audio->wflush) {
1178 rc = -EBUSY;
1179 goto done;
1180 }
1181
1182 if (copy_from_user(frame->data, buf_start, mfield_size)) {
1183 rc = -EFAULT;
1184 goto done;
1185 }
1186
1187 frame->mfield_sz = mfield_size;
1188 audio->out_head ^= 1;
1189 frame->used = mfield_size;
1190 audevrc_send_data(audio, 0);
1191
1192done:
1193 return rc;
1194}
1195
1196static ssize_t audevrc_write(struct file *file, const char __user *buf,
1197 size_t count, loff_t *pos)
1198{
1199 struct audio *audio = file->private_data;
1200 const char __user *start = buf;
1201 struct buffer *frame;
1202 size_t xfer;
1203 char *cpy_ptr;
1204 unsigned short mfield_size = 0;
1205 int rc = 0, eos_condition = AUDEVRC_EOS_NONE;
1206
1207 MM_DBG("cnt=%d\n", count);
1208
1209 if (count & 1)
1210 return -EINVAL;
1211
1212 mutex_lock(&audio->write_lock);
1213 while (count > 0) {
1214 frame = audio->out + audio->out_head;
1215 cpy_ptr = frame->data;
1216 rc = wait_event_interruptible(audio->write_wait,
1217 (frame->used == 0)
1218 || (audio->stopped)
1219 || (audio->wflush));
1220 if (rc < 0)
1221 break;
1222 if (audio->stopped || audio->wflush) {
1223 rc = -EBUSY;
1224 break;
1225 }
1226
1227 if (audio->mfield) {
1228 if (buf == start) {
1229 /* Processing beginning of user buffer */
1230 if (__get_user(mfield_size,
1231 (unsigned short __user *) buf)) {
1232 rc = -EFAULT;
1233 break;
1234 } else if (mfield_size > count) {
1235 rc = -EINVAL;
1236 break;
1237 }
1238 MM_DBG("mf offset_val %x\n", mfield_size);
1239 if (copy_from_user(cpy_ptr, buf,
1240 mfield_size)) {
1241 rc = -EFAULT;
1242 break;
1243 }
1244 /* Check if EOS flag is set and buffer has
1245 * contains just meta field
1246 */
1247 if (cpy_ptr[AUDEVRC_EOS_FLG_OFFSET] &
1248 AUDEVRC_EOS_FLG_MASK) {
1249 MM_DBG("eos set\n");
1250 eos_condition = AUDEVRC_EOS_SET;
1251 if (mfield_size == count) {
1252 buf += mfield_size;
1253 break;
1254 } else
1255 cpy_ptr[AUDEVRC_EOS_FLG_OFFSET] &=
1256 ~AUDEVRC_EOS_FLG_MASK;
1257 }
1258 /* Check EOS to see if */
1259 cpy_ptr += mfield_size;
1260 count -= mfield_size;
1261 buf += mfield_size;
1262 } else {
1263 mfield_size = 0;
1264 MM_DBG("continuous buffer\n");
1265 }
1266 frame->mfield_sz = mfield_size;
1267 }
1268
1269 xfer = (count > (frame->size - mfield_size)) ?
1270 (frame->size - mfield_size) : count;
1271 if (copy_from_user(cpy_ptr, buf, xfer)) {
1272 rc = -EFAULT;
1273 break;
1274 }
1275
1276 frame->used = xfer + mfield_size;
1277 audio->out_head ^= 1;
1278 count -= xfer;
1279 buf += xfer;
1280 audevrc_send_data(audio, 0);
1281 }
1282 if (eos_condition == AUDEVRC_EOS_SET)
1283 rc = audevrc_process_eos(audio, start, mfield_size);
1284 mutex_unlock(&audio->write_lock);
1285 if (!rc) {
1286 if (buf > start)
1287 return buf - start;
1288 }
1289 return rc;
1290}
1291
1292static int audevrc_release(struct inode *inode, struct file *file)
1293{
1294 struct audio *audio = file->private_data;
1295
1296 MM_INFO("audio instance 0x%08x freeing\n", (int)audio);
1297 mutex_lock(&audio->lock);
1298 auddev_unregister_evt_listner(AUDDEV_CLNT_DEC, audio->dec_id);
1299 audevrc_disable(audio);
1300 audevrc_flush(audio);
1301 audevrc_flush_pcm_buf(audio);
1302 msm_adsp_put(audio->audplay);
1303 audpp_adec_free(audio->dec_id);
1304#ifdef CONFIG_HAS_EARLYSUSPEND
1305 unregister_early_suspend(&audio->suspend_ctl.node);
1306#endif
1307 audio->event_abort = 1;
1308 wake_up(&audio->event_wait);
1309 audevrc_reset_event_queue(audio);
Laura Abbott61399692012-04-30 14:25:46 -07001310 iounmap(audio->map_v_write);
Santosh Mardifdc227a2011-07-11 17:20:34 +05301311 free_contiguous_memory_by_paddr(audio->phys);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001312 if (audio->read_data) {
Laura Abbott61399692012-04-30 14:25:46 -07001313 iounmap(audio->map_v_read);
Santosh Mardifdc227a2011-07-11 17:20:34 +05301314 free_contiguous_memory_by_paddr(audio->read_phys);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001315 }
1316 mutex_unlock(&audio->lock);
1317#ifdef CONFIG_DEBUG_FS
1318 if (audio->dentry)
1319 debugfs_remove(audio->dentry);
1320#endif
1321 kfree(audio);
1322 return 0;
1323}
1324
Vinay Vaka051db722012-01-24 19:48:32 +05301325#ifdef CONFIG_HAS_EARLYSUSPEND
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001326static void audevrc_post_event(struct audio *audio, int type,
1327 union msm_audio_event_payload payload)
1328{
1329 struct audevrc_event *e_node = NULL;
1330 unsigned long flags;
1331
1332 spin_lock_irqsave(&audio->event_queue_lock, flags);
1333
1334 if (!list_empty(&audio->free_event_queue)) {
1335 e_node = list_first_entry(&audio->free_event_queue,
1336 struct audevrc_event, list);
1337 list_del(&e_node->list);
1338 } else {
1339 e_node = kmalloc(sizeof(struct audevrc_event), GFP_ATOMIC);
1340 if (!e_node) {
1341 MM_ERR("No mem to post event %d\n", type);
1342 return;
1343 }
1344 }
1345
1346 e_node->event_type = type;
1347 e_node->payload = payload;
1348
1349 list_add_tail(&e_node->list, &audio->event_queue);
1350 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
1351 wake_up(&audio->event_wait);
1352}
1353
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001354static void audevrc_suspend(struct early_suspend *h)
1355{
1356 struct audevrc_suspend_ctl *ctl =
1357 container_of(h, struct audevrc_suspend_ctl, node);
1358 union msm_audio_event_payload payload;
1359
1360 MM_DBG("\n"); /* Macro prints the file name and function */
1361 audevrc_post_event(ctl->audio, AUDIO_EVENT_SUSPEND, payload);
1362}
1363
1364static void audevrc_resume(struct early_suspend *h)
1365{
1366 struct audevrc_suspend_ctl *ctl =
1367 container_of(h, struct audevrc_suspend_ctl, node);
1368 union msm_audio_event_payload payload;
1369
1370 MM_DBG("\n"); /* Macro prints the file name and function */
1371 audevrc_post_event(ctl->audio, AUDIO_EVENT_RESUME, payload);
1372}
1373#endif
1374
1375#ifdef CONFIG_DEBUG_FS
1376static ssize_t audevrc_debug_open(struct inode *inode, struct file *file)
1377{
1378 file->private_data = inode->i_private;
1379 return 0;
1380}
1381
1382static ssize_t audevrc_debug_read(struct file *file, char __user *buf,
1383 size_t count, loff_t *ppos)
1384{
1385 const int debug_bufmax = 1024;
1386 static char buffer[1024];
1387 int n = 0, i;
1388 struct audio *audio = file->private_data;
1389
1390 mutex_lock(&audio->lock);
1391 n = scnprintf(buffer, debug_bufmax, "opened %d\n", audio->opened);
1392 n += scnprintf(buffer + n, debug_bufmax - n,
1393 "enabled %d\n", audio->enabled);
1394 n += scnprintf(buffer + n, debug_bufmax - n,
1395 "stopped %d\n", audio->stopped);
1396 n += scnprintf(buffer + n, debug_bufmax - n,
1397 "pcm_feedback %d\n", audio->pcm_feedback);
1398 n += scnprintf(buffer + n, debug_bufmax - n,
1399 "out_buf_sz %d\n", audio->out[0].size);
1400 n += scnprintf(buffer + n, debug_bufmax - n,
1401 "pcm_buf_count %d \n", audio->pcm_buf_count);
1402 n += scnprintf(buffer + n, debug_bufmax - n,
1403 "pcm_buf_sz %d \n", audio->in[0].size);
1404 n += scnprintf(buffer + n, debug_bufmax - n,
1405 "volume %x \n", audio->vol_pan.volume);
1406 mutex_unlock(&audio->lock);
1407 /* Following variables are only useful for debugging when
1408 * when playback halts unexpectedly. Thus, no mutual exclusion
1409 * enforced
1410 */
1411 n += scnprintf(buffer + n, debug_bufmax - n,
1412 "wflush %d\n", audio->wflush);
1413 n += scnprintf(buffer + n, debug_bufmax - n,
1414 "rflush %d\n", audio->rflush);
1415 n += scnprintf(buffer + n, debug_bufmax - n,
1416 "running %d \n", audio->running);
1417 n += scnprintf(buffer + n, debug_bufmax - n,
1418 "dec state %d \n", audio->dec_state);
1419 n += scnprintf(buffer + n, debug_bufmax - n,
1420 "out_needed %d \n", audio->out_needed);
1421 n += scnprintf(buffer + n, debug_bufmax - n,
1422 "out_head %d \n", audio->out_head);
1423 n += scnprintf(buffer + n, debug_bufmax - n,
1424 "out_tail %d \n", audio->out_tail);
1425 n += scnprintf(buffer + n, debug_bufmax - n,
1426 "out[0].used %d \n", audio->out[0].used);
1427 n += scnprintf(buffer + n, debug_bufmax - n,
1428 "out[1].used %d \n", audio->out[1].used);
1429 n += scnprintf(buffer + n, debug_bufmax - n,
1430 "buffer_refresh %d \n", audio->buf_refresh);
1431 n += scnprintf(buffer + n, debug_bufmax - n,
1432 "read_next %d \n", audio->read_next);
1433 n += scnprintf(buffer + n, debug_bufmax - n,
1434 "fill_next %d \n", audio->fill_next);
1435 for (i = 0; i < audio->pcm_buf_count; i++)
1436 n += scnprintf(buffer + n, debug_bufmax - n,
1437 "in[%d].size %d \n", i, audio->in[i].used);
1438 buffer[n] = 0;
1439 return simple_read_from_buffer(buf, count, ppos, buffer, n);
1440}
1441
1442static const struct file_operations audevrc_debug_fops = {
1443 .read = audevrc_debug_read,
1444 .open = audevrc_debug_open,
1445};
1446#endif
1447
1448static int audevrc_open(struct inode *inode, struct file *file)
1449{
1450 struct audio *audio = NULL;
1451 int rc, dec_attrb, decid, i;
1452 struct audevrc_event *e_node = NULL;
1453#ifdef CONFIG_DEBUG_FS
1454 /* 4 bytes represents decoder number, 1 byte for terminate string */
1455 char name[sizeof "msm_evrc_" + 5];
1456#endif
1457
1458 /* Allocate audio instance, set to zero */
1459 audio = kzalloc(sizeof(struct audio), GFP_KERNEL);
1460 if (!audio) {
1461 MM_ERR("no memory to allocate audio instance\n");
1462 rc = -ENOMEM;
1463 goto done;
1464 }
1465 MM_INFO("audio instance 0x%08x created\n", (int)audio);
1466
1467 /* Allocate the decoder */
1468 dec_attrb = AUDDEC_DEC_EVRC;
1469 if ((file->f_mode & FMODE_WRITE) &&
1470 (file->f_mode & FMODE_READ)) {
1471 dec_attrb |= MSM_AUD_MODE_NONTUNNEL;
1472 audio->pcm_feedback = NON_TUNNEL_MODE_PLAYBACK;
1473 } else if ((file->f_mode & FMODE_WRITE) &&
1474 !(file->f_mode & FMODE_READ)) {
1475 dec_attrb |= MSM_AUD_MODE_TUNNEL;
1476 audio->pcm_feedback = TUNNEL_MODE_PLAYBACK;
1477 } else {
1478 kfree(audio);
1479 rc = -EACCES;
1480 goto done;
1481 }
1482 decid = audpp_adec_alloc(dec_attrb, &audio->module_name,
1483 &audio->queue_id);
1484
1485 if (decid < 0) {
1486 MM_ERR("No free decoder available, freeing instance 0x%08x\n",
1487 (int)audio);
1488 rc = -ENODEV;
1489 kfree(audio);
1490 goto done;
1491 }
1492
1493 audio->dec_id = decid & MSM_AUD_DECODER_MASK;
1494
Santosh Mardifdc227a2011-07-11 17:20:34 +05301495 audio->phys = allocate_contiguous_ebi_nomap(DMASZ, SZ_4K);
1496 if (!audio->phys) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001497 MM_ERR("could not allocate write buffers, freeing instance \
1498 0x%08x\n", (int)audio);
1499 rc = -ENOMEM;
1500 audpp_adec_free(audio->dec_id);
1501 kfree(audio);
1502 goto done;
1503 } else {
Laura Abbott61399692012-04-30 14:25:46 -07001504 audio->map_v_write = ioremap(audio->phys, DMASZ);
Santosh Mardifdc227a2011-07-11 17:20:34 +05301505 if (IS_ERR(audio->map_v_write)) {
1506 MM_ERR("failed to map write physical address, freeing \
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001507 instance 0x%08x\n", (int)audio);
1508 rc = -ENOMEM;
Santosh Mardifdc227a2011-07-11 17:20:34 +05301509 free_contiguous_memory_by_paddr(audio->phys);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001510 audpp_adec_free(audio->dec_id);
1511 kfree(audio);
1512 goto done;
1513 }
Laura Abbott61399692012-04-30 14:25:46 -07001514 audio->data = audio->map_v_write;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001515 MM_DBG("write buf: phy addr 0x%08x kernel addr 0x%08x\n",
1516 audio->phys, (int)audio->data);
1517 }
1518
1519 rc = msm_adsp_get(audio->module_name, &audio->audplay,
1520 &audplay_adsp_ops_evrc, audio);
1521
1522 if (rc) {
1523 MM_ERR("failed to get %s module, freeing instance 0x%08x\n",
1524 audio->module_name, (int)audio);
1525 goto err;
1526 }
1527
1528 /* Initialize all locks of audio instance */
1529 mutex_init(&audio->lock);
1530 mutex_init(&audio->write_lock);
1531 mutex_init(&audio->read_lock);
1532 mutex_init(&audio->get_event_lock);
1533 spin_lock_init(&audio->dsp_lock);
1534 init_waitqueue_head(&audio->write_wait);
1535 init_waitqueue_head(&audio->read_wait);
1536 INIT_LIST_HEAD(&audio->free_event_queue);
1537 INIT_LIST_HEAD(&audio->event_queue);
1538 init_waitqueue_head(&audio->wait);
1539 init_waitqueue_head(&audio->event_wait);
1540 spin_lock_init(&audio->event_queue_lock);
1541 init_waitqueue_head(&audio->avsync_wait);
1542
1543 audio->out[0].data = audio->data + 0;
1544 audio->out[0].addr = audio->phys + 0;
1545 audio->out[0].size = BUFSZ;
1546
1547 audio->out[1].data = audio->data + BUFSZ;
1548 audio->out[1].addr = audio->phys + BUFSZ;
1549 audio->out[1].size = BUFSZ;
1550
1551 audio->vol_pan.volume = 0x3FFF;
1552
1553 audevrc_flush(audio);
1554
1555 file->private_data = audio;
1556 audio->opened = 1;
1557
1558 audio->device_events = AUDDEV_EVT_DEV_RDY
1559 |AUDDEV_EVT_DEV_RLS|
1560 AUDDEV_EVT_STREAM_VOL_CHG;
1561
1562 rc = auddev_register_evt_listner(audio->device_events,
1563 AUDDEV_CLNT_DEC,
1564 audio->dec_id,
1565 evrc_listner,
1566 (void *)audio);
1567 if (rc) {
1568 MM_ERR("%s: failed to register listner\n", __func__);
1569 goto event_err;
1570 }
1571
1572#ifdef CONFIG_DEBUG_FS
1573 snprintf(name, sizeof name, "msm_evrc_%04x", audio->dec_id);
1574 audio->dentry = debugfs_create_file(name, S_IFREG | S_IRUGO,
1575 NULL, (void *) audio, &audevrc_debug_fops);
1576
1577 if (IS_ERR(audio->dentry))
1578 MM_DBG("debugfs_create_file failed\n");
1579#endif
1580#ifdef CONFIG_HAS_EARLYSUSPEND
1581 audio->suspend_ctl.node.level = EARLY_SUSPEND_LEVEL_DISABLE_FB;
1582 audio->suspend_ctl.node.resume = audevrc_resume;
1583 audio->suspend_ctl.node.suspend = audevrc_suspend;
1584 audio->suspend_ctl.audio = audio;
1585 register_early_suspend(&audio->suspend_ctl.node);
1586#endif
1587 for (i = 0; i < AUDEVRC_EVENT_NUM; i++) {
1588 e_node = kmalloc(sizeof(struct audevrc_event), GFP_KERNEL);
1589 if (e_node)
1590 list_add_tail(&e_node->list, &audio->free_event_queue);
1591 else {
1592 MM_ERR("event pkt alloc failed\n");
1593 break;
1594 }
1595 }
1596done:
1597 return rc;
1598event_err:
1599 msm_adsp_put(audio->audplay);
1600err:
Laura Abbott61399692012-04-30 14:25:46 -07001601 iounmap(audio->map_v_write);
Santosh Mardifdc227a2011-07-11 17:20:34 +05301602 free_contiguous_memory_by_paddr(audio->phys);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001603 audpp_adec_free(audio->dec_id);
1604 kfree(audio);
1605 return rc;
1606}
1607
1608static const struct file_operations audio_evrc_fops = {
1609 .owner = THIS_MODULE,
1610 .open = audevrc_open,
1611 .release = audevrc_release,
1612 .read = audevrc_read,
1613 .write = audevrc_write,
1614 .unlocked_ioctl = audevrc_ioctl,
1615 .fsync = audevrc_fsync,
1616};
1617
1618struct miscdevice audio_evrc_misc = {
1619 .minor = MISC_DYNAMIC_MINOR,
1620 .name = "msm_evrc",
1621 .fops = &audio_evrc_fops,
1622};
1623
1624static int __init audevrc_init(void)
1625{
1626 return misc_register(&audio_evrc_misc);
1627
1628}
1629
1630static void __exit audevrc_exit(void)
1631{
1632 misc_deregister(&audio_evrc_misc);
1633}
1634
1635module_init(audevrc_init);
1636module_exit(audevrc_exit);
1637
1638MODULE_DESCRIPTION("MSM EVRC driver");
1639MODULE_LICENSE("GPL v2");