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