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