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