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