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