blob: 6d520b4dc00acff4c82ba8dcf9f4c281397447a0 [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* audio_wma.c - wma audio decoder driver
2 *
Manish Dewangana4f1df02012-02-08 17:06:54 +05303 * Copyright (c) 2009, 2011-2012, Code Aurora Forum. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004 *
5 * Based on the mp3 native driver in arch/arm/mach-msm/qdsp5/audio_mp3.c
6 *
7 * Copyright (C) 2008 Google, Inc.
8 * Copyright (C) 2008 HTC Corporation
9 *
10 * All source code in this file is licensed under the following license except
11 * where indicated.
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License version 2 as published
15 * by the Free Software Foundation.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20 *
21 * See the GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, you can find it at http://www.fsf.org
24 */
25
Santosh Mardi0be3b8e2011-07-06 10:00:21 +053026#include <asm/atomic.h>
27#include <asm/ioctls.h>
28
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070029#include <linux/module.h>
30#include <linux/fs.h>
31#include <linux/miscdevice.h>
32#include <linux/uaccess.h>
33#include <linux/kthread.h>
34#include <linux/wait.h>
35#include <linux/dma-mapping.h>
36#include <linux/debugfs.h>
37#include <linux/delay.h>
38#include <linux/list.h>
39#include <linux/earlysuspend.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070040#include <linux/slab.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070041#include <linux/msm_audio.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070042#include <linux/msm_audio_wma.h>
Santosh Mardi0be3b8e2011-07-06 10:00:21 +053043#include <linux/memory_alloc.h>
Mitchel Humpherys1da6ebe2012-09-06 10:15:56 -070044#include <linux/msm_ion.h>
Santosh Mardi0be3b8e2011-07-06 10:00:21 +053045
46#include <mach/msm_adsp.h>
47#include <mach/iommu.h>
48#include <mach/iommu_domains.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070049#include <mach/qdsp5/qdsp5audppcmdi.h>
50#include <mach/qdsp5/qdsp5audppmsg.h>
Manish Dewanganfa8a6b62012-07-09 16:23:27 +053051#include <mach/qdsp5/qdsp5audpp.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070052#include <mach/qdsp5/qdsp5audplaycmdi.h>
53#include <mach/qdsp5/qdsp5audplaymsg.h>
54#include <mach/qdsp5/qdsp5rmtcmdi.h>
55#include <mach/debug_mm.h>
Santosh Mardi0be3b8e2011-07-06 10:00:21 +053056#include <mach/msm_memtypes.h>
57
58#include "audmgr.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070059
60/* Size must be power of 2 */
61#define BUFSZ_MAX 2062 /* Includes meta in size */
62#define BUFSZ_MIN 1038 /* Includes meta in size */
63#define DMASZ_MAX (BUFSZ_MAX * 2)
64#define DMASZ_MIN (BUFSZ_MIN * 2)
65
66#define AUDPLAY_INVALID_READ_PTR_OFFSET 0xFFFF
67#define AUDDEC_DEC_WMA 4
68
69#define PCM_BUFSZ_MIN 8216 /* Hold one stereo WMA frame and meta out*/
70#define PCM_BUF_MAX_COUNT 5 /* DSP only accepts 5 buffers at most
71 but support 2 buffers currently */
72#define ROUTING_MODE_FTRT 1
73#define ROUTING_MODE_RT 2
74/* Decoder status received from AUDPPTASK */
75#define AUDPP_DEC_STATUS_SLEEP 0
76#define AUDPP_DEC_STATUS_INIT 1
77#define AUDPP_DEC_STATUS_CFG 2
78#define AUDPP_DEC_STATUS_PLAY 3
79
80#define AUDWMA_METAFIELD_MASK 0xFFFF0000
81#define AUDWMA_EOS_FLG_OFFSET 0x0A /* Offset from beginning of buffer */
82#define AUDWMA_EOS_FLG_MASK 0x01
83#define AUDWMA_EOS_NONE 0x0 /* No EOS detected */
84#define AUDWMA_EOS_SET 0x1 /* EOS set in meta field */
85
86#define AUDWMA_EVENT_NUM 10 /* Default number of pre-allocated event packets */
87
88struct buffer {
89 void *data;
90 unsigned size;
91 unsigned used; /* Input usage actual DSP produced PCM size */
92 unsigned addr;
93 unsigned short mfield_sz; /*only useful for data has meta field */
94};
95
96#ifdef CONFIG_HAS_EARLYSUSPEND
97struct audwma_suspend_ctl {
98 struct early_suspend node;
99 struct audio *audio;
100};
101#endif
102
103struct audwma_event{
104 struct list_head list;
105 int event_type;
106 union msm_audio_event_payload payload;
107};
108
109struct audio {
110 struct buffer out[2];
111
112 spinlock_t dsp_lock;
113
114 uint8_t out_head;
115 uint8_t out_tail;
116 uint8_t out_needed; /* number of buffers the dsp is waiting for */
117 unsigned out_dma_sz;
118
119 atomic_t out_bytes;
120
121 struct mutex lock;
122 struct mutex write_lock;
123 wait_queue_head_t write_wait;
124
125 /* Host PCM section */
126 struct buffer in[PCM_BUF_MAX_COUNT];
127 struct mutex read_lock;
128 wait_queue_head_t read_wait; /* Wait queue for read */
129 char *read_data; /* pointer to reader buffer */
130 int32_t read_phys; /* physical address of reader buffer */
131 uint8_t read_next; /* index to input buffers to be read next */
132 uint8_t fill_next; /* index to buffer that DSP should be filling */
133 uint8_t pcm_buf_count; /* number of pcm buffer allocated */
134 /* ---- End of Host PCM section */
135
136 struct msm_adsp_module *audplay;
137
138 /* configuration to use on next enable */
139 uint32_t out_sample_rate;
140 uint32_t out_channel_mode;
141
142 struct msm_audio_wma_config wma_config;
143 struct audmgr audmgr;
144
145 /* data allocated for various buffers */
146 char *data;
147 int32_t phys; /* physical address of write buffer */
Laura Abbott35111d32012-04-27 18:41:48 -0700148 void *map_v_read;
149 void *map_v_write;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700150
151 int mfield; /* meta field embedded in data */
152 int rflush; /* Read flush */
153 int wflush; /* Write flush */
154 int opened;
155 int enabled;
156 int running;
157 int stopped; /* set when stopped, cleared on flush */
158 int pcm_feedback;
159 int buf_refresh;
160 int rmt_resource_released;
161 int teos; /* valid only if tunnel mode & no data left for decoder */
162 enum msm_aud_decoder_state dec_state; /* Represents decoder state */
163 int reserved; /* A byte is being reserved */
164 char rsv_byte; /* Handle odd length user data */
165
166 const char *module_name;
167 unsigned queue_id;
168 uint16_t dec_id;
169 uint32_t read_ptr_offset;
170
171#ifdef CONFIG_HAS_EARLYSUSPEND
172 struct audwma_suspend_ctl suspend_ctl;
173#endif
174
175#ifdef CONFIG_DEBUG_FS
176 struct dentry *dentry;
177#endif
178
179 wait_queue_head_t wait;
180 struct list_head free_event_queue;
181 struct list_head event_queue;
182 wait_queue_head_t event_wait;
183 spinlock_t event_queue_lock;
184 struct mutex get_event_lock;
185 int event_abort;
186
187 int eq_enable;
188 int eq_needs_commit;
189 audpp_cmd_cfg_object_params_eqalizer eq;
190 audpp_cmd_cfg_object_params_volume vol_pan;
Sidipotu Ashok172c98b2012-06-26 17:58:29 +0530191 struct ion_client *client;
192 struct ion_handle *input_buff_handle;
193 struct ion_handle *output_buff_handle;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700194};
195
196static int auddec_dsp_config(struct audio *audio, int enable);
197static void audpp_cmd_cfg_adec_params(struct audio *audio);
198static void audpp_cmd_cfg_routing_mode(struct audio *audio);
199static void audplay_send_data(struct audio *audio, unsigned needed);
200static void audplay_config_hostpcm(struct audio *audio);
201static void audplay_buffer_refresh(struct audio *audio);
202static void audio_dsp_event(void *private, unsigned id, uint16_t *msg);
203#ifdef CONFIG_HAS_EARLYSUSPEND
204static void audwma_post_event(struct audio *audio, int type,
205 union msm_audio_event_payload payload);
206#endif
207
208static int rmt_put_resource(struct audio *audio)
209{
210 struct aud_codec_config_cmd cmd;
211 unsigned short client_idx;
212
213 cmd.cmd_id = RM_CMD_AUD_CODEC_CFG;
214 cmd.client_id = RM_AUD_CLIENT_ID;
215 cmd.task_id = audio->dec_id;
216 cmd.enable = RMT_DISABLE;
217 cmd.dec_type = AUDDEC_DEC_WMA;
218 client_idx = ((cmd.client_id << 8) | cmd.task_id);
219
220 return put_adsp_resource(client_idx, &cmd, sizeof(cmd));
221}
222
223static int rmt_get_resource(struct audio *audio)
224{
225 struct aud_codec_config_cmd cmd;
226 unsigned short client_idx;
227
228 cmd.cmd_id = RM_CMD_AUD_CODEC_CFG;
229 cmd.client_id = RM_AUD_CLIENT_ID;
230 cmd.task_id = audio->dec_id;
231 cmd.enable = RMT_ENABLE;
232 cmd.dec_type = AUDDEC_DEC_WMA;
233 client_idx = ((cmd.client_id << 8) | cmd.task_id);
234
235 return get_adsp_resource(client_idx, &cmd, sizeof(cmd));
236}
237
238/* must be called with audio->lock held */
239static int audio_enable(struct audio *audio)
240{
241 struct audmgr_config cfg;
242 int rc;
243
244 MM_DBG("\n"); /* Macro prints the file name and function */
245 if (audio->enabled)
246 return 0;
247
248 if (audio->rmt_resource_released == 1) {
249 audio->rmt_resource_released = 0;
250 rc = rmt_get_resource(audio);
251 if (rc) {
252 MM_ERR("ADSP resources are not available for WMA \
253 session 0x%08x on decoder: %d\n Ignoring \
254 error and going ahead with the playback\n",
255 (int)audio, audio->dec_id);
256 }
257 }
258
259 audio->dec_state = MSM_AUD_DECODER_STATE_NONE;
260 audio->out_tail = 0;
261 audio->out_needed = 0;
262
263 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK) {
264 cfg.tx_rate = RPC_AUD_DEF_SAMPLE_RATE_NONE;
265 cfg.rx_rate = RPC_AUD_DEF_SAMPLE_RATE_48000;
266 cfg.def_method = RPC_AUD_DEF_METHOD_PLAYBACK;
267 cfg.codec = RPC_AUD_DEF_CODEC_WMA;
268 cfg.snd_method = RPC_SND_METHOD_MIDI;
269
270 rc = audmgr_enable(&audio->audmgr, &cfg);
271 if (rc < 0)
272 return rc;
273 }
274
275 if (msm_adsp_enable(audio->audplay)) {
276 MM_ERR("msm_adsp_enable(audplay) failed\n");
277 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK)
278 audmgr_disable(&audio->audmgr);
279 return -ENODEV;
280 }
281
282 if (audpp_enable(audio->dec_id, audio_dsp_event, audio)) {
283 MM_ERR("audpp_enable() failed\n");
284 msm_adsp_disable(audio->audplay);
285 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK)
286 audmgr_disable(&audio->audmgr);
287 return -ENODEV;
288 }
289
290 audio->enabled = 1;
291 return 0;
292}
293
294/* must be called with audio->lock held */
295static int audio_disable(struct audio *audio)
296{
297 int rc = 0;
298 MM_DBG("\n"); /* Macro prints the file name and function */
299 if (audio->enabled) {
300 audio->enabled = 0;
301 audio->dec_state = MSM_AUD_DECODER_STATE_NONE;
302 auddec_dsp_config(audio, 0);
303 rc = wait_event_interruptible_timeout(audio->wait,
304 audio->dec_state != MSM_AUD_DECODER_STATE_NONE,
305 msecs_to_jiffies(MSM_AUD_DECODER_WAIT_MS));
306 if (rc == 0)
307 rc = -ETIMEDOUT;
308 else if (audio->dec_state != MSM_AUD_DECODER_STATE_CLOSE)
309 rc = -EFAULT;
310 else
311 rc = 0;
Manish Dewangan89a9f232012-02-09 17:14:40 +0530312 audio->stopped = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700313 wake_up(&audio->write_wait);
314 wake_up(&audio->read_wait);
315 msm_adsp_disable(audio->audplay);
316 audpp_disable(audio->dec_id, audio);
317 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK)
318 audmgr_disable(&audio->audmgr);
319 audio->out_needed = 0;
320 rmt_put_resource(audio);
321 audio->rmt_resource_released = 1;
322 }
323 return rc;
324}
325
326/* ------------------- dsp --------------------- */
327static void audio_update_pcm_buf_entry(struct audio *audio,
328 uint32_t *payload)
329{
330 uint8_t index;
331 unsigned long flags;
332
333 if (audio->rflush)
334 return;
335
336 spin_lock_irqsave(&audio->dsp_lock, flags);
337 for (index = 0; index < payload[1]; index++) {
338 if (audio->in[audio->fill_next].addr ==
339 payload[2 + index * 2]) {
340 MM_DBG("audio_update_pcm_buf_entry: \
341 in[%d] ready\n", audio->fill_next);
342 audio->in[audio->fill_next].used =
343 payload[3 + index * 2];
344 if ((++audio->fill_next) == audio->pcm_buf_count)
345 audio->fill_next = 0;
346 } else {
347 MM_ERR("audio_update_pcm_buf_entry: \
348 expected=%x ret=%x\n",
349 audio->in[audio->fill_next].addr,
350 payload[1 + index * 2]);
351 break;
352 }
353 }
354 if (audio->in[audio->fill_next].used == 0) {
355 audplay_buffer_refresh(audio);
356 } else {
357 MM_DBG("read cannot keep up\n");
358 audio->buf_refresh = 1;
359 }
360 wake_up(&audio->read_wait);
361 spin_unlock_irqrestore(&audio->dsp_lock, flags);
362}
363
364static void audplay_dsp_event(void *data, unsigned id, size_t len,
365 void (*getevent) (void *ptr, size_t len))
366{
367 struct audio *audio = data;
368 uint32_t msg[28];
369
370 getevent(msg, sizeof(msg));
371
372 MM_DBG("msg_id=%x\n", id);
373
374 switch (id) {
375 case AUDPLAY_MSG_DEC_NEEDS_DATA:
376 audplay_send_data(audio, 1);
377 break;
378
379 case AUDPLAY_MSG_BUFFER_UPDATE:
380 audio_update_pcm_buf_entry(audio, msg);
381 break;
382
383 case ADSP_MESSAGE_ID:
384 MM_DBG("Received ADSP event: module enable(audplaytask)\n");
385 break;
386
387 default:
388 MM_ERR("unexpected message from decoder \n");
389 break;
390 }
391}
392
393static void audio_dsp_event(void *private, unsigned id, uint16_t *msg)
394{
395 struct audio *audio = private;
396
397 switch (id) {
398 case AUDPP_MSG_STATUS_MSG:{
399 unsigned status = msg[1];
400
401 switch (status) {
402 case AUDPP_DEC_STATUS_SLEEP: {
403 uint16_t reason = msg[2];
404 MM_DBG("decoder status:sleep reason = \
405 0x%04x\n", reason);
406 if ((reason == AUDPP_MSG_REASON_MEM)
407 || (reason ==
408 AUDPP_MSG_REASON_NODECODER)) {
409 audio->dec_state =
410 MSM_AUD_DECODER_STATE_FAILURE;
411 wake_up(&audio->wait);
412 } else if (reason == AUDPP_MSG_REASON_NONE) {
413 /* decoder is in disable state */
414 audio->dec_state =
415 MSM_AUD_DECODER_STATE_CLOSE;
416 wake_up(&audio->wait);
417 }
418 break;
419 }
420 case AUDPP_DEC_STATUS_INIT:
421 MM_DBG("decoder status: init\n");
422 if (audio->pcm_feedback)
423 audpp_cmd_cfg_routing_mode(audio);
424 else
425 audpp_cmd_cfg_adec_params(audio);
426 break;
427
428 case AUDPP_DEC_STATUS_CFG:
429 MM_DBG("decoder status: cfg\n");
430 break;
431 case AUDPP_DEC_STATUS_PLAY:
432 MM_DBG("decoder status: play\n");
433 if (audio->pcm_feedback) {
434 audplay_config_hostpcm(audio);
435 audplay_buffer_refresh(audio);
436 }
437 audio->dec_state =
438 MSM_AUD_DECODER_STATE_SUCCESS;
439 wake_up(&audio->wait);
440 break;
441 default:
442 MM_ERR("unknown decoder status\n");
443 }
444 break;
445 }
446 case AUDPP_MSG_CFG_MSG:
447 if (msg[0] == AUDPP_MSG_ENA_ENA) {
448 MM_DBG("CFG_MSG ENABLE\n");
449 auddec_dsp_config(audio, 1);
450 audio->out_needed = 0;
451 audio->running = 1;
452 audpp_dsp_set_vol_pan(audio->dec_id, &audio->vol_pan);
453 audpp_dsp_set_eq(audio->dec_id, audio->eq_enable,
454 &audio->eq);
455 audpp_avsync(audio->dec_id, 22050);
456 } else if (msg[0] == AUDPP_MSG_ENA_DIS) {
457 MM_DBG("CFG_MSG DISABLE\n");
458 audpp_avsync(audio->dec_id, 0);
459 audio->running = 0;
460 } else {
461 MM_DBG("CFG_MSG %d?\n", msg[0]);
462 }
463 break;
464 case AUDPP_MSG_ROUTING_ACK:
465 MM_DBG("ROUTING_ACK mode=%d\n", msg[1]);
466 audpp_cmd_cfg_adec_params(audio);
467 break;
468
469 case AUDPP_MSG_FLUSH_ACK:
470 MM_DBG("FLUSH_ACK\n");
471 audio->wflush = 0;
472 audio->rflush = 0;
473 wake_up(&audio->write_wait);
474 if (audio->pcm_feedback)
475 audplay_buffer_refresh(audio);
Phani Kumar Alladaee940eb2012-05-10 11:13:04 +0530476 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700477 case AUDPP_MSG_PCMDMAMISSED:
478 MM_DBG("PCMDMAMISSED\n");
479 audio->teos = 1;
480 wake_up(&audio->write_wait);
481 break;
482
483 default:
484 MM_ERR("UNKNOWN (%d)\n", id);
485 }
486
487}
488
489static struct msm_adsp_ops audplay_adsp_ops_wma = {
490 .event = audplay_dsp_event,
491};
492
493#define audplay_send_queue0(audio, cmd, len) \
494 msm_adsp_write(audio->audplay, audio->queue_id, \
495 cmd, len)
496
497static int auddec_dsp_config(struct audio *audio, int enable)
498{
499 u16 cfg_dec_cmd[AUDPP_CMD_CFG_DEC_TYPE_LEN / sizeof(unsigned short)];
500
501 memset(cfg_dec_cmd, 0, sizeof(cfg_dec_cmd));
502 cfg_dec_cmd[0] = AUDPP_CMD_CFG_DEC_TYPE;
503 if (enable)
504 cfg_dec_cmd[1 + audio->dec_id] = AUDPP_CMD_UPDATDE_CFG_DEC |
505 AUDPP_CMD_ENA_DEC_V | AUDDEC_DEC_WMA;
506 else
507 cfg_dec_cmd[1 + audio->dec_id] = AUDPP_CMD_UPDATDE_CFG_DEC |
508 AUDPP_CMD_DIS_DEC_V;
509
510 return audpp_send_queue1(&cfg_dec_cmd, sizeof(cfg_dec_cmd));
511}
512
513static void audpp_cmd_cfg_adec_params(struct audio *audio)
514{
515 struct audpp_cmd_cfg_adec_params_wma cmd;
516
517 memset(&cmd, 0, sizeof(cmd));
518 cmd.common.cmd_id = AUDPP_CMD_CFG_ADEC_PARAMS;
519 cmd.common.length = AUDPP_CMD_CFG_ADEC_PARAMS_WMA_LEN;
520 cmd.common.dec_id = audio->dec_id;
521 cmd.common.input_sampling_frequency = audio->out_sample_rate;
522
523 /*
524 * Test done for sample with the following configuration
525 * armdatareqthr = 1262
526 * channelsdecoded = 1(MONO)/2(STEREO)
527 * wmabytespersec = Tested with 6003 Bytes per sec
528 * wmasamplingfreq = 44100
529 * wmaencoderopts = 31
530 */
531
532 cmd.armdatareqthr = audio->wma_config.armdatareqthr;
533 cmd.channelsdecoded = audio->wma_config.channelsdecoded;
534 cmd.wmabytespersec = audio->wma_config.wmabytespersec;
535 cmd.wmasamplingfreq = audio->wma_config.wmasamplingfreq;
536 cmd.wmaencoderopts = audio->wma_config.wmaencoderopts;
537
538 audpp_send_queue2(&cmd, sizeof(cmd));
539}
540
541static void audpp_cmd_cfg_routing_mode(struct audio *audio)
542{
543 struct audpp_cmd_routing_mode cmd;
544
545 MM_DBG("\n"); /* Macro prints the file name and function */
546 memset(&cmd, 0, sizeof(cmd));
547 cmd.cmd_id = AUDPP_CMD_ROUTING_MODE;
548 cmd.object_number = audio->dec_id;
549 if (audio->pcm_feedback)
550 cmd.routing_mode = ROUTING_MODE_FTRT;
551 else
552 cmd.routing_mode = ROUTING_MODE_RT;
553
554 audpp_send_queue1(&cmd, sizeof(cmd));
555}
556
557static void audplay_buffer_refresh(struct audio *audio)
558{
559 struct audplay_cmd_buffer_refresh refresh_cmd;
560
561 refresh_cmd.cmd_id = AUDPLAY_CMD_BUFFER_REFRESH;
562 refresh_cmd.num_buffers = 1;
563 refresh_cmd.buf0_address = audio->in[audio->fill_next].addr;
564 refresh_cmd.buf0_length = audio->in[audio->fill_next].size;
565 refresh_cmd.buf_read_count = 0;
566
567 MM_DBG("buf0_addr=%x buf0_len=%d\n",
568 refresh_cmd.buf0_address,
569 refresh_cmd.buf0_length);
570
571 (void)audplay_send_queue0(audio, &refresh_cmd, sizeof(refresh_cmd));
572}
573
574static void audplay_config_hostpcm(struct audio *audio)
575{
576 struct audplay_cmd_hpcm_buf_cfg cfg_cmd;
577
578 MM_DBG("\n"); /* Macro prints the file name and function */
579 cfg_cmd.cmd_id = AUDPLAY_CMD_HPCM_BUF_CFG;
580 cfg_cmd.max_buffers = audio->pcm_buf_count;
581 cfg_cmd.byte_swap = 0;
582 cfg_cmd.hostpcm_config = (0x8000) | (0x4000);
583 cfg_cmd.feedback_frequency = 1;
584 cfg_cmd.partition_number = 0;
585
586 (void)audplay_send_queue0(audio, &cfg_cmd, sizeof(cfg_cmd));
587}
588
589
590static int audplay_dsp_send_data_avail(struct audio *audio,
591 unsigned idx, unsigned len)
592{
593 struct audplay_cmd_bitstream_data_avail_nt2 cmd;
594
595 cmd.cmd_id = AUDPLAY_CMD_BITSTREAM_DATA_AVAIL_NT2;
596 if (audio->mfield)
597 cmd.decoder_id = AUDWMA_METAFIELD_MASK |
598 (audio->out[idx].mfield_sz >> 1);
599 else
600 cmd.decoder_id = audio->dec_id;
601 cmd.buf_ptr = audio->out[idx].addr;
602 cmd.buf_size = len/2;
603 cmd.partition_number = 0;
604 /* complete writes to the input buffer */
605 wmb();
606 return audplay_send_queue0(audio, &cmd, sizeof(cmd));
607}
608
609static void audplay_send_data(struct audio *audio, unsigned needed)
610{
611 struct buffer *frame;
612 unsigned long flags;
613
614 spin_lock_irqsave(&audio->dsp_lock, flags);
615 if (!audio->running)
616 goto done;
617
618 if (audio->wflush) {
619 audio->out_needed = 1;
620 goto done;
621 }
622
623 if (needed && !audio->wflush) {
624 /* We were called from the callback because the DSP
625 * requested more data. Note that the DSP does want
626 * more data, and if a buffer was in-flight, mark it
627 * as available (since the DSP must now be done with
628 * it).
629 */
630 audio->out_needed = 1;
631 frame = audio->out + audio->out_tail;
632 if (frame->used == 0xffffffff) {
633 MM_DBG("frame %d free\n", audio->out_tail);
634 frame->used = 0;
635 audio->out_tail ^= 1;
636 wake_up(&audio->write_wait);
637 }
638 }
639
640 if (audio->out_needed) {
641 /* If the DSP currently wants data and we have a
642 * buffer available, we will send it and reset
643 * the needed flag. We'll mark the buffer as in-flight
644 * so that it won't be recycled until the next buffer
645 * is requested
646 */
647
648 MM_DBG("\n"); /* Macro prints the file name and function */
649 frame = audio->out + audio->out_tail;
650 if (frame->used) {
651 BUG_ON(frame->used == 0xffffffff);
652 MM_DBG("frame %d busy\n", audio->out_tail);
653 audplay_dsp_send_data_avail(audio, audio->out_tail,
654 frame->used);
655 frame->used = 0xffffffff;
656 audio->out_needed = 0;
657 }
658 }
659done:
660 spin_unlock_irqrestore(&audio->dsp_lock, flags);
661}
662
663/* ------------------- device --------------------- */
664
665static void audio_flush(struct audio *audio)
666{
Manish Dewangana4f1df02012-02-08 17:06:54 +0530667 unsigned long flags;
668
669 spin_lock_irqsave(&audio->dsp_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700670 audio->out[0].used = 0;
671 audio->out[1].used = 0;
672 audio->out_head = 0;
673 audio->out_tail = 0;
674 audio->reserved = 0;
Manish Dewangana4f1df02012-02-08 17:06:54 +0530675 spin_unlock_irqrestore(&audio->dsp_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700676 atomic_set(&audio->out_bytes, 0);
677}
678
679static void audio_flush_pcm_buf(struct audio *audio)
680{
681 uint8_t index;
682
Manish Dewangana4f1df02012-02-08 17:06:54 +0530683 unsigned long flags;
684
685 spin_lock_irqsave(&audio->dsp_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700686 for (index = 0; index < PCM_BUF_MAX_COUNT; index++)
687 audio->in[index].used = 0;
688 audio->buf_refresh = 0;
689 audio->read_next = 0;
690 audio->fill_next = 0;
Manish Dewangana4f1df02012-02-08 17:06:54 +0530691 spin_unlock_irqrestore(&audio->dsp_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700692}
693
694static void audio_ioport_reset(struct audio *audio)
695{
696 /* Make sure read/write thread are free from
697 * sleep and knowing that system is not able
698 * to process io request at the moment
699 */
700 wake_up(&audio->write_wait);
701 mutex_lock(&audio->write_lock);
702 audio_flush(audio);
703 mutex_unlock(&audio->write_lock);
704 wake_up(&audio->read_wait);
705 mutex_lock(&audio->read_lock);
706 audio_flush_pcm_buf(audio);
707 mutex_unlock(&audio->read_lock);
708}
709
710static int audwma_events_pending(struct audio *audio)
711{
712 unsigned long flags;
713 int empty;
714
715 spin_lock_irqsave(&audio->event_queue_lock, flags);
716 empty = !list_empty(&audio->event_queue);
717 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
718 return empty || audio->event_abort;
719}
720
721static void audwma_reset_event_queue(struct audio *audio)
722{
723 unsigned long flags;
724 struct audwma_event *drv_evt;
725 struct list_head *ptr, *next;
726
727 spin_lock_irqsave(&audio->event_queue_lock, flags);
728 list_for_each_safe(ptr, next, &audio->event_queue) {
729 drv_evt = list_first_entry(&audio->event_queue,
730 struct audwma_event, list);
731 list_del(&drv_evt->list);
732 kfree(drv_evt);
733 }
734 list_for_each_safe(ptr, next, &audio->free_event_queue) {
735 drv_evt = list_first_entry(&audio->free_event_queue,
736 struct audwma_event, list);
737 list_del(&drv_evt->list);
738 kfree(drv_evt);
739 }
740 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
741
742 return;
743}
744
745static long audwma_process_event_req(struct audio *audio, void __user *arg)
746{
747 long rc;
748 struct msm_audio_event usr_evt;
749 struct audwma_event *drv_evt = NULL;
750 int timeout;
751 unsigned long flags;
752
753 if (copy_from_user(&usr_evt, arg, sizeof(struct msm_audio_event)))
754 return -EFAULT;
755
756 timeout = (int) usr_evt.timeout_ms;
757
758 if (timeout > 0) {
759 rc = wait_event_interruptible_timeout(
760 audio->event_wait, audwma_events_pending(audio),
761 msecs_to_jiffies(timeout));
762 if (rc == 0)
763 return -ETIMEDOUT;
764 } else {
765 rc = wait_event_interruptible(
766 audio->event_wait, audwma_events_pending(audio));
767 }
768
769 if (rc < 0)
770 return rc;
771
772 if (audio->event_abort) {
773 audio->event_abort = 0;
774 return -ENODEV;
775 }
776
777 rc = 0;
778
779 spin_lock_irqsave(&audio->event_queue_lock, flags);
780 if (!list_empty(&audio->event_queue)) {
781 drv_evt = list_first_entry(&audio->event_queue,
782 struct audwma_event, list);
783 list_del(&drv_evt->list);
784 }
785
786 if (drv_evt) {
787 usr_evt.event_type = drv_evt->event_type;
788 usr_evt.event_payload = drv_evt->payload;
789 list_add_tail(&drv_evt->list, &audio->free_event_queue);
790 } else
791 rc = -1;
792 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
793
794 if (!rc && copy_to_user(arg, &usr_evt, sizeof(usr_evt)))
795 rc = -EFAULT;
796
797 return rc;
798}
799
800static int audio_enable_eq(struct audio *audio, int enable)
801{
802 if (audio->eq_enable == enable && !audio->eq_needs_commit)
803 return 0;
804
805 audio->eq_enable = enable;
806
807 if (audio->running) {
808 audpp_dsp_set_eq(audio->dec_id, enable, &audio->eq);
809 audio->eq_needs_commit = 0;
810 }
811 return 0;
812}
813
814static long audio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
815{
816 struct audio *audio = file->private_data;
817 int rc = -EINVAL;
818 unsigned long flags = 0;
819 uint16_t enable_mask;
820 int enable;
821 int prev_state;
Sidipotu Ashok172c98b2012-06-26 17:58:29 +0530822 unsigned long ionflag = 0;
823 ion_phys_addr_t addr = 0;
824 struct ion_handle *handle = NULL;
825 int len = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700826
827 MM_DBG("cmd = %d\n", cmd);
828
829 if (cmd == AUDIO_GET_STATS) {
830 struct msm_audio_stats stats;
831 stats.byte_count = audpp_avsync_byte_count(audio->dec_id);
832 stats.sample_count = audpp_avsync_sample_count(audio->dec_id);
833 if (copy_to_user((void *)arg, &stats, sizeof(stats)))
834 return -EFAULT;
835 return 0;
836 }
837
838 switch (cmd) {
839 case AUDIO_ENABLE_AUDPP:
840 if (copy_from_user(&enable_mask, (void *) arg,
841 sizeof(enable_mask))) {
842 rc = -EFAULT;
843 break;
844 }
845
846 spin_lock_irqsave(&audio->dsp_lock, flags);
847 enable = (enable_mask & EQ_ENABLE) ? 1 : 0;
848 audio_enable_eq(audio, enable);
849 spin_unlock_irqrestore(&audio->dsp_lock, flags);
850 rc = 0;
851 break;
852 case AUDIO_SET_VOLUME:
853 spin_lock_irqsave(&audio->dsp_lock, flags);
854 audio->vol_pan.volume = arg;
855 if (audio->running)
856 audpp_dsp_set_vol_pan(audio->dec_id, &audio->vol_pan);
857 spin_unlock_irqrestore(&audio->dsp_lock, flags);
858 rc = 0;
859 break;
860
861 case AUDIO_SET_PAN:
862 spin_lock_irqsave(&audio->dsp_lock, flags);
863 audio->vol_pan.pan = arg;
864 if (audio->running)
865 audpp_dsp_set_vol_pan(audio->dec_id, &audio->vol_pan);
866 spin_unlock_irqrestore(&audio->dsp_lock, flags);
867 rc = 0;
868 break;
869
870 case AUDIO_SET_EQ:
871 prev_state = audio->eq_enable;
872 audio->eq_enable = 0;
873 if (copy_from_user(&audio->eq.num_bands, (void *) arg,
874 sizeof(audio->eq) -
875 (AUDPP_CMD_CFG_OBJECT_PARAMS_COMMON_LEN + 2))) {
876 rc = -EFAULT;
877 break;
878 }
879 audio->eq_enable = prev_state;
880 audio->eq_needs_commit = 1;
881 rc = 0;
882 break;
883 }
884
885 if (-EINVAL != rc)
886 return rc;
887
888 if (cmd == AUDIO_GET_EVENT) {
889 MM_DBG("AUDIO_GET_EVENT\n");
890 if (mutex_trylock(&audio->get_event_lock)) {
891 rc = audwma_process_event_req(audio,
892 (void __user *) arg);
893 mutex_unlock(&audio->get_event_lock);
894 } else
895 rc = -EBUSY;
896 return rc;
897 }
898
899 if (cmd == AUDIO_ABORT_GET_EVENT) {
900 audio->event_abort = 1;
901 wake_up(&audio->event_wait);
902 return 0;
903 }
904
905 mutex_lock(&audio->lock);
906 switch (cmd) {
907 case AUDIO_START:
908 MM_DBG("AUDIO_START\n");
909 rc = audio_enable(audio);
910 if (!rc) {
911 rc = wait_event_interruptible_timeout(audio->wait,
912 audio->dec_state != MSM_AUD_DECODER_STATE_NONE,
913 msecs_to_jiffies(MSM_AUD_DECODER_WAIT_MS));
914 MM_INFO("dec_state %d rc = %d\n", audio->dec_state, rc);
915
916 if (audio->dec_state != MSM_AUD_DECODER_STATE_SUCCESS)
917 rc = -ENODEV;
918 else
919 rc = 0;
920 }
921 break;
922 case AUDIO_STOP:
923 MM_DBG("AUDIO_STOP\n");
924 rc = audio_disable(audio);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700925 audio_ioport_reset(audio);
926 audio->stopped = 0;
927 break;
928 case AUDIO_FLUSH:
929 MM_DBG("AUDIO_FLUSH\n");
930 audio->rflush = 1;
931 audio->wflush = 1;
932 audio_ioport_reset(audio);
933 if (audio->running) {
934 audpp_flush(audio->dec_id);
935 rc = wait_event_interruptible(audio->write_wait,
936 !audio->wflush);
937 if (rc < 0) {
938 MM_ERR("AUDIO_FLUSH interrupted\n");
939 rc = -EINTR;
940 }
941 } else {
942 audio->rflush = 0;
943 audio->wflush = 0;
944 }
945 break;
946 case AUDIO_SET_CONFIG: {
947 struct msm_audio_config config;
948 if (copy_from_user(&config, (void *) arg, sizeof(config))) {
949 rc = -EFAULT;
950 break;
951 }
952 if (config.channel_count == 1) {
953 config.channel_count = AUDPP_CMD_PCM_INTF_MONO_V;
954 } else if (config.channel_count == 2) {
955 config.channel_count = AUDPP_CMD_PCM_INTF_STEREO_V;
956 } else {
957 rc = -EINVAL;
958 break;
959 }
960 audio->mfield = config.meta_field;
961 audio->out_sample_rate = config.sample_rate;
962 audio->out_channel_mode = config.channel_count;
963 rc = 0;
964 break;
965 }
966 case AUDIO_GET_CONFIG: {
967 struct msm_audio_config config;
968 config.buffer_size = (audio->out_dma_sz >> 1);
969 config.buffer_count = 2;
970 config.sample_rate = audio->out_sample_rate;
971 if (audio->out_channel_mode == AUDPP_CMD_PCM_INTF_MONO_V)
972 config.channel_count = 1;
973 else
974 config.channel_count = 2;
975 config.meta_field = 0;
976 config.unused[0] = 0;
977 config.unused[1] = 0;
978 config.unused[2] = 0;
979 if (copy_to_user((void *) arg, &config, sizeof(config)))
980 rc = -EFAULT;
981 else
982 rc = 0;
983
984 break;
985 }
986 case AUDIO_GET_WMA_CONFIG:{
987 if (copy_to_user((void *)arg, &audio->wma_config,
988 sizeof(audio->wma_config)))
989 rc = -EFAULT;
990 else
991 rc = 0;
992 break;
993 }
994 case AUDIO_SET_WMA_CONFIG:{
995 struct msm_audio_wma_config usr_config;
996
997 if (copy_from_user
998 (&usr_config, (void *)arg,
999 sizeof(usr_config))) {
1000 rc = -EFAULT;
1001 break;
1002 }
1003
1004 audio->wma_config = usr_config;
1005 rc = 0;
1006 break;
1007 }
1008 case AUDIO_GET_PCM_CONFIG:{
1009 struct msm_audio_pcm_config config;
1010 config.pcm_feedback = audio->pcm_feedback;
1011 config.buffer_count = PCM_BUF_MAX_COUNT;
1012 config.buffer_size = PCM_BUFSZ_MIN;
1013 if (copy_to_user((void *)arg, &config,
1014 sizeof(config)))
1015 rc = -EFAULT;
1016 else
1017 rc = 0;
1018 break;
1019 }
1020 case AUDIO_SET_PCM_CONFIG:{
1021 struct msm_audio_pcm_config config;
1022 if (copy_from_user
1023 (&config, (void *)arg, sizeof(config))) {
1024 rc = -EFAULT;
1025 break;
1026 }
1027 if (config.pcm_feedback != audio->pcm_feedback) {
1028 MM_ERR("Not sufficient permission to"
1029 "change the playback mode\n");
1030 rc = -EACCES;
1031 break;
1032 }
1033 if ((config.buffer_count > PCM_BUF_MAX_COUNT) ||
1034 (config.buffer_count == 1))
1035 config.buffer_count = PCM_BUF_MAX_COUNT;
1036
1037 if (config.buffer_size < PCM_BUFSZ_MIN)
1038 config.buffer_size = PCM_BUFSZ_MIN;
1039
1040 /* Check if pcm feedback is required */
1041 if ((config.pcm_feedback) && (!audio->read_data)) {
1042 MM_DBG("allocate PCM buffer %d\n",
1043 config.buffer_count *
1044 config.buffer_size);
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301045 handle = ion_alloc(audio->client,
1046 (config.buffer_size *
1047 config.buffer_count),
Hanumant Singh7d72bad2012-08-29 18:39:44 -07001048 SZ_4K, ION_HEAP(ION_AUDIO_HEAP_ID), 0);
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301049 if (IS_ERR_OR_NULL(handle)) {
1050 MM_ERR("Unable to alloc I/P buffs\n");
1051 audio->input_buff_handle = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001052 rc = -ENOMEM;
1053 break;
1054 }
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301055
1056 audio->input_buff_handle = handle;
1057
1058 rc = ion_phys(audio->client ,
1059 handle, &addr, &len);
1060 if (rc) {
1061 MM_ERR("Invalid phy: %x sz: %x\n",
1062 (unsigned int) addr,
1063 (unsigned int) len);
1064 ion_free(audio->client, handle);
1065 audio->input_buff_handle = NULL;
1066 rc = -ENOMEM;
1067 break;
1068 } else {
1069 MM_INFO("Got valid phy: %x sz: %x\n",
1070 (unsigned int) audio->read_phys,
1071 (unsigned int) len);
1072 }
1073 audio->read_phys = (int32_t)addr;
1074
1075 rc = ion_handle_get_flags(audio->client,
1076 handle, &ionflag);
1077 if (rc) {
1078 MM_ERR("could not get flags\n");
1079 ion_free(audio->client, handle);
1080 audio->input_buff_handle = NULL;
1081 rc = -ENOMEM;
1082 break;
1083 }
1084
1085 audio->map_v_read = ion_map_kernel(
Mitchel Humpherys911b4b72012-09-12 14:42:50 -07001086 audio->client, handle);
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05301087 if (IS_ERR(audio->map_v_read)) {
1088 MM_ERR("map of read buf failed\n");
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301089 ion_free(audio->client, handle);
1090 audio->input_buff_handle = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001091 rc = -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001092 } else {
1093 uint8_t index;
1094 uint32_t offset = 0;
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05301095 audio->read_data =
Laura Abbott35111d32012-04-27 18:41:48 -07001096 audio->map_v_read;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001097 audio->buf_refresh = 0;
1098 audio->pcm_buf_count =
1099 config.buffer_count;
1100 audio->read_next = 0;
1101 audio->fill_next = 0;
1102
1103 for (index = 0;
1104 index < config.buffer_count;
1105 index++) {
1106 audio->in[index].data =
1107 audio->read_data + offset;
1108 audio->in[index].addr =
1109 audio->read_phys + offset;
1110 audio->in[index].size =
1111 config.buffer_size;
1112 audio->in[index].used = 0;
1113 offset += config.buffer_size;
1114 }
1115 MM_DBG("read buf: phy addr \
1116 0x%08x kernel addr 0x%08x\n",
1117 audio->read_phys,
1118 (int)audio->read_data);
1119 rc = 0;
1120 }
1121 } else {
1122 rc = 0;
1123 }
1124 break;
1125 }
1126 case AUDIO_PAUSE:
1127 MM_DBG("AUDIO_PAUSE %ld\n", arg);
1128 rc = audpp_pause(audio->dec_id, (int) arg);
1129 break;
1130 default:
1131 rc = -EINVAL;
1132 }
1133 mutex_unlock(&audio->lock);
1134 return rc;
1135}
1136
1137/* Only useful in tunnel-mode */
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301138static int audio_fsync(struct file *file, loff_t a, loff_t b,
1139 int datasync)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001140{
1141 struct audio *audio = file->private_data;
1142 struct buffer *frame;
1143 int rc = 0;
1144
1145 MM_DBG("\n"); /* Macro prints the file name and function */
1146
1147 if (!audio->running || audio->pcm_feedback) {
1148 rc = -EINVAL;
1149 goto done_nolock;
1150 }
1151
1152 mutex_lock(&audio->write_lock);
1153
1154 rc = wait_event_interruptible(audio->write_wait,
1155 (!audio->out[0].used &&
1156 !audio->out[1].used &&
1157 audio->out_needed) || audio->wflush);
1158
1159 if (rc < 0)
1160 goto done;
1161 else if (audio->wflush) {
1162 rc = -EBUSY;
1163 goto done;
1164 }
1165
1166 if (audio->reserved) {
1167 MM_DBG("send reserved byte\n");
1168 frame = audio->out + audio->out_tail;
1169 ((char *) frame->data)[0] = audio->rsv_byte;
1170 ((char *) frame->data)[1] = 0;
1171 frame->used = 2;
1172 audplay_send_data(audio, 0);
1173
1174 rc = wait_event_interruptible(audio->write_wait,
1175 (!audio->out[0].used &&
1176 !audio->out[1].used &&
1177 audio->out_needed) || audio->wflush);
1178
1179 if (rc < 0)
1180 goto done;
1181 else if (audio->wflush) {
1182 rc = -EBUSY;
1183 goto done;
1184 }
1185 }
1186
1187 /* pcm dmamiss message is sent continously
1188 * when decoder is starved so no race
1189 * condition concern
1190 */
1191 audio->teos = 0;
1192
1193 rc = wait_event_interruptible(audio->write_wait,
1194 audio->teos || audio->wflush);
1195
1196 if (audio->wflush)
1197 rc = -EBUSY;
1198
1199done:
1200 mutex_unlock(&audio->write_lock);
1201done_nolock:
1202 return rc;
1203}
1204
1205static ssize_t audio_read(struct file *file, char __user *buf, size_t count,
1206 loff_t *pos)
1207{
1208 struct audio *audio = file->private_data;
1209 const char __user *start = buf;
1210 int rc = 0;
1211
1212 if (!audio->pcm_feedback)
1213 return 0; /* PCM feedback is not enabled. Nothing to read */
1214
1215 mutex_lock(&audio->read_lock);
1216 MM_DBG("%d \n", count);
1217 while (count > 0) {
1218 rc = wait_event_interruptible(audio->read_wait,
1219 (audio->in[audio->read_next].used > 0) ||
1220 (audio->stopped) || (audio->rflush));
1221
1222 if (rc < 0)
1223 break;
1224
1225 if (audio->stopped || audio->rflush) {
1226 rc = -EBUSY;
1227 break;
1228 }
1229
1230 if (count < audio->in[audio->read_next].used) {
1231 /* Read must happen in frame boundary. Since driver
1232 does not know frame size, read count must be greater
1233 or equal to size of PCM samples */
1234 MM_DBG("audio_read: no partial frame done reading\n");
1235 break;
1236 } else {
1237 MM_DBG("audio_read: read from in[%d]\n",
1238 audio->read_next);
1239 /* order reads from the output buffer */
1240 rmb();
1241 if (copy_to_user
1242 (buf, audio->in[audio->read_next].data,
1243 audio->in[audio->read_next].used)) {
1244 MM_ERR("invalid addr %x \n", (unsigned int)buf);
1245 rc = -EFAULT;
1246 break;
1247 }
1248 count -= audio->in[audio->read_next].used;
1249 buf += audio->in[audio->read_next].used;
1250 audio->in[audio->read_next].used = 0;
1251 if ((++audio->read_next) == audio->pcm_buf_count)
1252 audio->read_next = 0;
1253 break; /* Force to exit while loop
1254 * to prevent output thread
1255 * sleep too long if data is
1256 * not ready at this moment.
1257 */
1258 }
1259 }
1260
1261 /* don't feed output buffer to HW decoder during flushing
1262 * buffer refresh command will be sent once flush completes
1263 * send buf refresh command here can confuse HW decoder
1264 */
1265 if (audio->buf_refresh && !audio->rflush) {
1266 audio->buf_refresh = 0;
1267 MM_DBG("kick start pcm feedback again\n");
1268 audplay_buffer_refresh(audio);
1269 }
1270
1271 mutex_unlock(&audio->read_lock);
1272
1273 if (buf > start)
1274 rc = buf - start;
1275
1276 MM_DBG("read %d bytes\n", rc);
1277 return rc;
1278}
1279
1280static int audwma_process_eos(struct audio *audio,
1281 const char __user *buf_start, unsigned short mfield_size)
1282{
1283 int rc = 0;
1284 struct buffer *frame;
1285 char *buf_ptr;
1286
1287 if (audio->reserved) {
1288 MM_DBG("flush reserve byte\n");
1289 frame = audio->out + audio->out_head;
1290 buf_ptr = frame->data;
1291 rc = wait_event_interruptible(audio->write_wait,
1292 (frame->used == 0)
1293 || (audio->stopped)
1294 || (audio->wflush));
1295 if (rc < 0)
1296 goto done;
1297 if (audio->stopped || audio->wflush) {
1298 rc = -EBUSY;
1299 goto done;
1300 }
1301
1302 buf_ptr[0] = audio->rsv_byte;
1303 buf_ptr[1] = 0;
1304 audio->out_head ^= 1;
1305 frame->mfield_sz = 0;
1306 frame->used = 2;
1307 audio->reserved = 0;
1308 audplay_send_data(audio, 0);
1309 }
1310
1311 frame = audio->out + audio->out_head;
1312
1313 rc = wait_event_interruptible(audio->write_wait,
1314 (audio->out_needed &&
1315 audio->out[0].used == 0 &&
1316 audio->out[1].used == 0)
1317 || (audio->stopped)
1318 || (audio->wflush));
1319
1320 if (rc < 0)
1321 goto done;
1322 if (audio->stopped || audio->wflush) {
1323 rc = -EBUSY;
1324 goto done;
1325 }
1326
1327 if (copy_from_user(frame->data, buf_start, mfield_size)) {
1328 rc = -EFAULT;
1329 goto done;
1330 }
1331
1332 frame->mfield_sz = mfield_size;
1333 audio->out_head ^= 1;
1334 frame->used = mfield_size;
1335 audplay_send_data(audio, 0);
1336done:
1337 return rc;
1338}
1339
1340static ssize_t audio_write(struct file *file, const char __user *buf,
1341 size_t count, loff_t *pos)
1342{
1343 struct audio *audio = file->private_data;
1344 const char __user *start = buf;
1345 struct buffer *frame;
1346 size_t xfer;
1347 char *cpy_ptr;
1348 int rc = 0, eos_condition = AUDWMA_EOS_NONE;
1349 unsigned dsize;
1350 unsigned short mfield_size = 0;
1351
1352 MM_DBG("cnt=%d\n", count);
1353
1354 mutex_lock(&audio->write_lock);
1355 while (count > 0) {
1356 frame = audio->out + audio->out_head;
1357 cpy_ptr = frame->data;
1358 dsize = 0;
1359 rc = wait_event_interruptible(audio->write_wait,
1360 (frame->used == 0)
1361 || (audio->stopped)
1362 || (audio->wflush));
1363 if (rc < 0)
1364 break;
1365 if (audio->stopped || audio->wflush) {
1366 rc = -EBUSY;
1367 break;
1368 }
1369 if (audio->mfield) {
1370 if (buf == start) {
1371 /* Processing beginning of user buffer */
1372 if (__get_user(mfield_size,
1373 (unsigned short __user *) buf)) {
1374 rc = -EFAULT;
1375 break;
1376 } else if (mfield_size > count) {
1377 rc = -EINVAL;
1378 break;
1379 }
1380 MM_DBG("audio_write: mf offset_val %x\n",
1381 mfield_size);
1382 if (copy_from_user(cpy_ptr, buf, mfield_size)) {
1383 rc = -EFAULT;
1384 break;
1385 }
1386 /* Check if EOS flag is set and buffer has
1387 * contains just meta field
1388 */
1389 if (cpy_ptr[AUDWMA_EOS_FLG_OFFSET] &
1390 AUDWMA_EOS_FLG_MASK) {
1391 MM_DBG("audio_write: EOS SET\n");
1392 eos_condition = AUDWMA_EOS_SET;
1393 if (mfield_size == count) {
1394 buf += mfield_size;
1395 break;
1396 } else
1397 cpy_ptr[AUDWMA_EOS_FLG_OFFSET]
1398 &= ~AUDWMA_EOS_FLG_MASK;
1399 }
1400 cpy_ptr += mfield_size;
1401 count -= mfield_size;
1402 dsize += mfield_size;
1403 buf += mfield_size;
1404 } else {
1405 mfield_size = 0;
1406 MM_DBG("audio_write: continuous buffer\n");
1407 }
1408 frame->mfield_sz = mfield_size;
1409 }
1410
1411 if (audio->reserved) {
1412 MM_DBG("append reserved byte %x\n", audio->rsv_byte);
1413 *cpy_ptr = audio->rsv_byte;
1414 xfer = (count > ((frame->size - mfield_size) - 1)) ?
1415 (frame->size - mfield_size) - 1 : count;
1416 cpy_ptr++;
1417 dsize += 1;
1418 audio->reserved = 0;
1419 } else
1420 xfer = (count > (frame->size - mfield_size)) ?
1421 (frame->size - mfield_size) : count;
1422
1423 if (copy_from_user(cpy_ptr, buf, xfer)) {
1424 rc = -EFAULT;
1425 break;
1426 }
1427
1428 dsize += xfer;
1429 if (dsize & 1) {
1430 audio->rsv_byte = ((char *) frame->data)[dsize - 1];
1431 MM_DBG("odd length buf reserve last byte %x\n",
1432 audio->rsv_byte);
1433 audio->reserved = 1;
1434 dsize--;
1435 }
1436 count -= xfer;
1437 buf += xfer;
1438
1439 if (dsize > 0) {
1440 audio->out_head ^= 1;
1441 frame->used = dsize;
1442 audplay_send_data(audio, 0);
1443 }
1444 }
1445 if (eos_condition == AUDWMA_EOS_SET)
1446 rc = audwma_process_eos(audio, start, mfield_size);
1447 mutex_unlock(&audio->write_lock);
1448 if (!rc) {
1449 if (buf > start)
1450 return buf - start;
1451 }
1452 return rc;
1453}
1454
1455static int audio_release(struct inode *inode, struct file *file)
1456{
1457 struct audio *audio = file->private_data;
1458
1459 MM_INFO("audio instance 0x%08x freeing\n", (int)audio);
1460 mutex_lock(&audio->lock);
1461 audio_disable(audio);
1462 if (audio->rmt_resource_released == 0)
1463 rmt_put_resource(audio);
1464 audio_flush(audio);
1465 audio_flush_pcm_buf(audio);
1466 msm_adsp_put(audio->audplay);
1467 audpp_adec_free(audio->dec_id);
1468#ifdef CONFIG_HAS_EARLYSUSPEND
1469 unregister_early_suspend(&audio->suspend_ctl.node);
1470#endif
1471 audio->event_abort = 1;
1472 wake_up(&audio->event_wait);
1473 audwma_reset_event_queue(audio);
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301474 ion_unmap_kernel(audio->client, audio->output_buff_handle);
1475 ion_free(audio->client, audio->output_buff_handle);
1476 if (audio->input_buff_handle != NULL) {
1477 ion_unmap_kernel(audio->client, audio->input_buff_handle);
1478 ion_free(audio->client, audio->input_buff_handle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001479 }
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301480 ion_client_destroy(audio->client);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001481 mutex_unlock(&audio->lock);
1482#ifdef CONFIG_DEBUG_FS
1483 if (audio->dentry)
1484 debugfs_remove(audio->dentry);
1485#endif
1486 kfree(audio);
1487 return 0;
1488}
1489
1490#ifdef CONFIG_HAS_EARLYSUSPEND
1491static void audwma_post_event(struct audio *audio, int type,
1492 union msm_audio_event_payload payload)
1493{
1494 struct audwma_event *e_node = NULL;
1495 unsigned long flags;
1496
1497 spin_lock_irqsave(&audio->event_queue_lock, flags);
1498
1499 if (!list_empty(&audio->free_event_queue)) {
1500 e_node = list_first_entry(&audio->free_event_queue,
1501 struct audwma_event, list);
1502 list_del(&e_node->list);
1503 } else {
1504 e_node = kmalloc(sizeof(struct audwma_event), GFP_ATOMIC);
1505 if (!e_node) {
1506 MM_ERR("No mem to post event %d\n", type);
1507 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
1508 return;
1509 }
1510 }
1511
1512 e_node->event_type = type;
1513 e_node->payload = payload;
1514
1515 list_add_tail(&e_node->list, &audio->event_queue);
1516 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
1517 wake_up(&audio->event_wait);
1518}
1519
1520static void audwma_suspend(struct early_suspend *h)
1521{
1522 struct audwma_suspend_ctl *ctl =
1523 container_of(h, struct audwma_suspend_ctl, node);
1524 union msm_audio_event_payload payload;
1525
1526 MM_DBG("\n"); /* Macro prints the file name and function */
1527 audwma_post_event(ctl->audio, AUDIO_EVENT_SUSPEND, payload);
1528}
1529
1530static void audwma_resume(struct early_suspend *h)
1531{
1532 struct audwma_suspend_ctl *ctl =
1533 container_of(h, struct audwma_suspend_ctl, node);
1534 union msm_audio_event_payload payload;
1535
1536 MM_DBG("\n"); /* Macro prints the file name and function */
1537 audwma_post_event(ctl->audio, AUDIO_EVENT_RESUME, payload);
1538}
1539#endif
1540
1541#ifdef CONFIG_DEBUG_FS
1542static ssize_t audwma_debug_open(struct inode *inode, struct file *file)
1543{
1544 file->private_data = inode->i_private;
1545 return 0;
1546}
1547
1548static ssize_t audwma_debug_read(struct file *file, char __user *buf,
1549 size_t count, loff_t *ppos)
1550{
1551 const int debug_bufmax = 4096;
1552 static char buffer[4096];
1553 int n = 0, i;
1554 struct audio *audio = file->private_data;
1555
1556 mutex_lock(&audio->lock);
1557 n = scnprintf(buffer, debug_bufmax, "opened %d\n", audio->opened);
1558 n += scnprintf(buffer + n, debug_bufmax - n,
1559 "enabled %d\n", audio->enabled);
1560 n += scnprintf(buffer + n, debug_bufmax - n,
1561 "stopped %d\n", audio->stopped);
1562 n += scnprintf(buffer + n, debug_bufmax - n,
1563 "pcm_feedback %d\n", audio->pcm_feedback);
1564 n += scnprintf(buffer + n, debug_bufmax - n,
1565 "out_buf_sz %d\n", audio->out[0].size);
1566 n += scnprintf(buffer + n, debug_bufmax - n,
1567 "pcm_buf_count %d \n", audio->pcm_buf_count);
1568 n += scnprintf(buffer + n, debug_bufmax - n,
1569 "pcm_buf_sz %d \n", audio->in[0].size);
1570 n += scnprintf(buffer + n, debug_bufmax - n,
1571 "volume %x \n", audio->vol_pan.volume);
1572 n += scnprintf(buffer + n, debug_bufmax - n,
1573 "sample rate %d \n", audio->out_sample_rate);
1574 n += scnprintf(buffer + n, debug_bufmax - n,
1575 "channel mode %d \n", audio->out_channel_mode);
1576 mutex_unlock(&audio->lock);
1577 /* Following variables are only useful for debugging when
1578 * when playback halts unexpectedly. Thus, no mutual exclusion
1579 * enforced
1580 */
1581 n += scnprintf(buffer + n, debug_bufmax - n,
1582 "wflush %d\n", audio->wflush);
1583 n += scnprintf(buffer + n, debug_bufmax - n,
1584 "rflush %d\n", audio->rflush);
1585 n += scnprintf(buffer + n, debug_bufmax - n,
1586 "running %d \n", audio->running);
1587 n += scnprintf(buffer + n, debug_bufmax - n,
1588 "dec state %d \n", audio->dec_state);
1589 n += scnprintf(buffer + n, debug_bufmax - n,
1590 "out_needed %d \n", audio->out_needed);
1591 n += scnprintf(buffer + n, debug_bufmax - n,
1592 "out_head %d \n", audio->out_head);
1593 n += scnprintf(buffer + n, debug_bufmax - n,
1594 "out_tail %d \n", audio->out_tail);
1595 n += scnprintf(buffer + n, debug_bufmax - n,
1596 "out[0].used %d \n", audio->out[0].used);
1597 n += scnprintf(buffer + n, debug_bufmax - n,
1598 "out[1].used %d \n", audio->out[1].used);
1599 n += scnprintf(buffer + n, debug_bufmax - n,
1600 "buffer_refresh %d \n", audio->buf_refresh);
1601 n += scnprintf(buffer + n, debug_bufmax - n,
1602 "read_next %d \n", audio->read_next);
1603 n += scnprintf(buffer + n, debug_bufmax - n,
1604 "fill_next %d \n", audio->fill_next);
1605 for (i = 0; i < audio->pcm_buf_count; i++)
1606 n += scnprintf(buffer + n, debug_bufmax - n,
1607 "in[%d].size %d \n", i, audio->in[i].used);
1608 buffer[n] = 0;
1609 return simple_read_from_buffer(buf, count, ppos, buffer, n);
1610}
1611
1612static const struct file_operations audwma_debug_fops = {
1613 .read = audwma_debug_read,
1614 .open = audwma_debug_open,
1615};
1616#endif
1617
1618static int audio_open(struct inode *inode, struct file *file)
1619{
1620 struct audio *audio = NULL;
1621 int rc, dec_attrb, decid, i;
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301622 unsigned mem_sz = DMASZ_MAX;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001623 struct audwma_event *e_node = NULL;
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301624 unsigned long ionflag = 0;
1625 ion_phys_addr_t addr = 0;
1626 struct ion_handle *handle = NULL;
1627 struct ion_client *client = NULL;
1628 int len = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001629#ifdef CONFIG_DEBUG_FS
1630 /* 4 bytes represents decoder number, 1 byte for terminate string */
1631 char name[sizeof "msm_wma_" + 5];
1632#endif
1633
1634 /* Allocate Mem for audio instance */
1635 audio = kzalloc(sizeof(struct audio), GFP_KERNEL);
1636 if (!audio) {
1637 MM_ERR("no memory to allocate audio instance \n");
1638 rc = -ENOMEM;
1639 goto done;
1640 }
1641 MM_INFO("audio instance 0x%08x created\n", (int)audio);
1642
1643 /* Allocate the decoder */
1644 dec_attrb = AUDDEC_DEC_WMA;
1645 if ((file->f_mode & FMODE_WRITE) &&
1646 (file->f_mode & FMODE_READ)) {
1647 dec_attrb |= MSM_AUD_MODE_NONTUNNEL;
1648 audio->pcm_feedback = NON_TUNNEL_MODE_PLAYBACK;
1649 } else if ((file->f_mode & FMODE_WRITE) &&
1650 !(file->f_mode & FMODE_READ)) {
1651 dec_attrb |= MSM_AUD_MODE_TUNNEL;
1652 audio->pcm_feedback = TUNNEL_MODE_PLAYBACK;
1653 } else {
1654 kfree(audio);
1655 rc = -EACCES;
1656 goto done;
1657 }
1658
1659 decid = audpp_adec_alloc(dec_attrb, &audio->module_name,
1660 &audio->queue_id);
1661
1662 if (decid < 0) {
1663 MM_ERR("No free decoder available, freeing instance 0x%08x\n",
1664 (int)audio);
1665 rc = -ENODEV;
1666 kfree(audio);
1667 goto done;
1668 }
1669 audio->dec_id = decid & MSM_AUD_DECODER_MASK;
1670
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301671 client = msm_ion_client_create(UINT_MAX, "Audio_WMA_Client");
1672 if (IS_ERR_OR_NULL(client)) {
1673 pr_err("Unable to create ION client\n");
1674 rc = -ENOMEM;
1675 goto client_create_error;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001676 }
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301677 audio->client = client;
1678
1679 handle = ion_alloc(client, mem_sz, SZ_4K,
Hanumant Singh7d72bad2012-08-29 18:39:44 -07001680 ION_HEAP(ION_AUDIO_HEAP_ID), 0);
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301681 if (IS_ERR_OR_NULL(handle)) {
1682 MM_ERR("Unable to create allocate O/P buffers\n");
1683 rc = -ENOMEM;
1684 goto output_buff_alloc_error;
1685 }
1686 audio->output_buff_handle = handle;
1687
1688 rc = ion_phys(client, handle, &addr, &len);
1689 if (rc) {
1690 MM_ERR("O/P buffers:Invalid phy: %x sz: %x\n",
1691 (unsigned int) addr, (unsigned int) len);
1692 goto output_buff_get_phys_error;
1693 } else {
1694 MM_INFO("O/P buffers:valid phy: %x sz: %x\n",
1695 (unsigned int) addr, (unsigned int) len);
1696 }
1697 audio->phys = (int32_t)addr;
1698
1699
1700 rc = ion_handle_get_flags(client, handle, &ionflag);
1701 if (rc) {
1702 MM_ERR("could not get flags for the handle\n");
1703 goto output_buff_get_flags_error;
1704 }
1705
Mitchel Humpherys911b4b72012-09-12 14:42:50 -07001706 audio->map_v_write = ion_map_kernel(client, handle);
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301707 if (IS_ERR(audio->map_v_write)) {
1708 MM_ERR("could not map write buffers\n");
1709 rc = -ENOMEM;
1710 goto output_buff_map_error;
1711 }
1712 audio->data = audio->map_v_write;
1713 MM_DBG("write buf: phy addr 0x%08x kernel addr 0x%08x\n",
1714 audio->phys, (int)audio->data);
1715
1716 audio->out_dma_sz = mem_sz;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001717
1718 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK) {
1719 rc = audmgr_open(&audio->audmgr);
1720 if (rc) {
1721 MM_ERR("audmgr open failed, freeing instance \
1722 0x%08x\n", (int)audio);
1723 goto err;
1724 }
1725 }
1726
1727 rc = msm_adsp_get(audio->module_name, &audio->audplay,
1728 &audplay_adsp_ops_wma, audio);
1729 if (rc) {
1730 MM_ERR("failed to get %s module, freeing instance 0x%08x\n",
1731 audio->module_name, (int)audio);
1732 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK)
1733 audmgr_close(&audio->audmgr);
1734 goto err;
1735 }
1736
1737 rc = rmt_get_resource(audio);
1738 if (rc) {
1739 MM_ERR("ADSP resources are not available for WMA session \
1740 0x%08x on decoder: %d\n", (int)audio, audio->dec_id);
1741 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK)
1742 audmgr_close(&audio->audmgr);
1743 msm_adsp_put(audio->audplay);
1744 goto err;
1745 }
1746
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301747 audio->input_buff_handle = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001748 mutex_init(&audio->lock);
1749 mutex_init(&audio->write_lock);
1750 mutex_init(&audio->read_lock);
1751 mutex_init(&audio->get_event_lock);
1752 spin_lock_init(&audio->dsp_lock);
1753 init_waitqueue_head(&audio->write_wait);
1754 init_waitqueue_head(&audio->read_wait);
1755 INIT_LIST_HEAD(&audio->free_event_queue);
1756 INIT_LIST_HEAD(&audio->event_queue);
1757 init_waitqueue_head(&audio->wait);
1758 init_waitqueue_head(&audio->event_wait);
1759 spin_lock_init(&audio->event_queue_lock);
1760
1761 audio->out[0].data = audio->data + 0;
1762 audio->out[0].addr = audio->phys + 0;
1763 audio->out[0].size = audio->out_dma_sz >> 1;
1764
1765 audio->out[1].data = audio->data + audio->out[0].size;
1766 audio->out[1].addr = audio->phys + audio->out[0].size;
1767 audio->out[1].size = audio->out[0].size;
1768
1769 audio->wma_config.armdatareqthr = 1262;
1770 audio->wma_config.channelsdecoded = 2;
1771 audio->wma_config.wmabytespersec = 6003;
1772 audio->wma_config.wmasamplingfreq = 44100;
1773 audio->wma_config.wmaencoderopts = 31;
1774
1775 audio->out_sample_rate = 44100;
1776 audio->out_channel_mode = AUDPP_CMD_PCM_INTF_STEREO_V;
1777
1778 audio->vol_pan.volume = 0x2000;
1779
1780 audio_flush(audio);
1781
1782 file->private_data = audio;
1783 audio->opened = 1;
1784#ifdef CONFIG_DEBUG_FS
1785 snprintf(name, sizeof name, "msm_wma_%04x", audio->dec_id);
1786 audio->dentry = debugfs_create_file(name, S_IFREG | S_IRUGO,
1787 NULL, (void *) audio,
1788 &audwma_debug_fops);
1789
1790 if (IS_ERR(audio->dentry))
1791 MM_DBG("debugfs_create_file failed\n");
1792#endif
1793#ifdef CONFIG_HAS_EARLYSUSPEND
1794 audio->suspend_ctl.node.level = EARLY_SUSPEND_LEVEL_DISABLE_FB;
1795 audio->suspend_ctl.node.resume = audwma_resume;
1796 audio->suspend_ctl.node.suspend = audwma_suspend;
1797 audio->suspend_ctl.audio = audio;
1798 register_early_suspend(&audio->suspend_ctl.node);
1799#endif
1800 for (i = 0; i < AUDWMA_EVENT_NUM; i++) {
1801 e_node = kmalloc(sizeof(struct audwma_event), GFP_KERNEL);
1802 if (e_node)
1803 list_add_tail(&e_node->list, &audio->free_event_queue);
1804 else {
1805 MM_ERR("event pkt alloc failed\n");
1806 break;
1807 }
1808 }
1809done:
1810 return rc;
1811err:
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301812 ion_unmap_kernel(client, audio->output_buff_handle);
1813output_buff_map_error:
1814output_buff_get_phys_error:
1815output_buff_get_flags_error:
1816 ion_free(client, audio->output_buff_handle);
1817output_buff_alloc_error:
1818 ion_client_destroy(client);
1819client_create_error:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001820 audpp_adec_free(audio->dec_id);
1821 kfree(audio);
1822 return rc;
1823}
1824
1825static const struct file_operations audio_wma_fops = {
1826 .owner = THIS_MODULE,
1827 .open = audio_open,
1828 .release = audio_release,
1829 .read = audio_read,
1830 .write = audio_write,
1831 .unlocked_ioctl = audio_ioctl,
1832 .fsync = audio_fsync,
1833};
1834
1835struct miscdevice audio_wma_misc = {
1836 .minor = MISC_DYNAMIC_MINOR,
1837 .name = "msm_wma",
1838 .fops = &audio_wma_fops,
1839};
1840
1841static int __init audio_init(void)
1842{
1843 return misc_register(&audio_wma_misc);
1844}
1845
1846device_initcall(audio_init);