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