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