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