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