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