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