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