blob: 7d37ceaa4f4e01c85826ded7352a09b2dde0c5d0 [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(
Mitchel Humpherys911b4b72012-09-12 14:42:50 -07001017 audio->client, handle);
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05301018 if (IS_ERR(audio->map_v_read)) {
1019 MM_ERR("failed to map mem for read buf\n");
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301020 ion_free(audio->client, handle);
1021 audio->input_buff_handle = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001022 rc = -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001023 } else {
1024 uint8_t index;
1025 uint32_t offset = 0;
Laura Abbott35111d32012-04-27 18:41:48 -07001026 audio->read_data = audio->map_v_read;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001027 audio->pcm_feedback = 1;
1028 audio->buf_refresh = 0;
1029 audio->pcm_buf_count =
1030 config.buffer_count;
1031 audio->read_next = 0;
1032 audio->fill_next = 0;
1033
1034 for (index = 0;
1035 index < config.buffer_count; index++) {
1036 audio->in[index].data =
1037 audio->read_data + offset;
1038 audio->in[index].addr =
1039 audio->read_phys + offset;
1040 audio->in[index].size =
1041 config.buffer_size;
1042 audio->in[index].used = 0;
1043 offset += config.buffer_size;
1044 }
1045 MM_DBG("read buf: phy addr 0x%08x \
1046 kernel addr 0x%08x\n",
1047 audio->read_phys,
1048 (int)audio->read_data);
1049 rc = 0;
1050 }
1051 } else {
1052 rc = 0;
1053 }
1054 break;
1055 }
1056 default:
1057 rc = -EINVAL;
1058 }
1059 mutex_unlock(&audio->lock);
1060 return rc;
1061}
1062
1063/* Only useful in tunnel-mode */
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301064static int audamrwb_fsync(struct file *file, loff_t a, loff_t b,
1065 int datasync)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001066{
1067 struct audio *audio = file->private_data;
1068 struct buffer *frame;
1069 int rc = 0;
1070
1071 MM_DBG("\n"); /* Macro prints the file name and function */
1072
1073 if (!audio->running || audio->pcm_feedback) {
1074 rc = -EINVAL;
1075 goto done_nolock;
1076 }
1077
1078 mutex_lock(&audio->write_lock);
1079
1080 rc = wait_event_interruptible(audio->write_wait,
1081 (!audio->out[0].used &&
1082 !audio->out[1].used &&
1083 audio->out_needed) || audio->wflush);
1084
1085 if (rc < 0)
1086 goto done;
1087 else if (audio->wflush) {
1088 rc = -EBUSY;
1089 goto done;
1090 }
1091
1092 if (audio->reserved) {
1093 MM_DBG("send reserved byte\n");
1094 frame = audio->out + audio->out_tail;
1095 ((char *) frame->data)[0] = audio->rsv_byte;
1096 ((char *) frame->data)[1] = 0;
1097 frame->used = 2;
1098 audamrwb_send_data(audio, 0);
1099
1100 rc = wait_event_interruptible(audio->write_wait,
1101 (!audio->out[0].used &&
1102 !audio->out[1].used &&
1103 audio->out_needed) || audio->wflush);
1104
1105 if (rc < 0)
1106 goto done;
1107 else if (audio->wflush) {
1108 rc = -EBUSY;
1109 goto done;
1110 }
1111 }
1112
1113 /* pcm dmamiss message is sent continously
1114 * when decoder is starved so no race
1115 * condition concern
1116 */
1117 audio->teos = 0;
1118
1119 rc = wait_event_interruptible(audio->write_wait,
1120 audio->teos || audio->wflush);
1121
1122 if (audio->wflush)
1123 rc = -EBUSY;
1124
1125done:
1126 mutex_unlock(&audio->write_lock);
1127done_nolock:
1128 return rc;
1129}
1130
1131static ssize_t audamrwb_read(struct file *file, char __user *buf, size_t count,
1132 loff_t *pos)
1133{
1134 struct audio *audio = file->private_data;
1135 const char __user *start = buf;
1136 int rc = 0;
1137
1138 if (!audio->pcm_feedback)
1139 return 0; /* PCM feedback is not enabled. Nothing to read */
1140
1141 mutex_lock(&audio->read_lock);
1142 MM_DBG("count %d\n", count);
1143 while (count > 0) {
1144 rc = wait_event_interruptible(audio->read_wait,
1145 (audio->in[audio->read_next].used > 0) ||
1146 (audio->stopped) || (audio->rflush));
1147
1148 if (rc < 0)
1149 break;
1150
1151 if (audio->stopped || audio->rflush) {
1152 rc = -EBUSY;
1153 break;
1154 }
1155
1156 if (count < audio->in[audio->read_next].used) {
1157 /* Read must happen in frame boundary. Since driver does
1158 * not know frame size, read count must be greater or
1159 * equal to size of PCM samples
1160 */
1161 MM_DBG("read stop - partial frame\n");
1162 break;
1163 } else {
1164 MM_DBG("read from in[%d]\n",
1165 audio->read_next);
1166
1167 if (copy_to_user
1168 (buf, audio->in[audio->read_next].data,
1169 audio->in[audio->read_next].used)) {
1170 MM_ERR("invalid addr %x \n", (unsigned int)buf);
1171 rc = -EFAULT;
1172 break;
1173 }
1174 count -= audio->in[audio->read_next].used;
1175 buf += audio->in[audio->read_next].used;
1176 audio->in[audio->read_next].used = 0;
1177 if ((++audio->read_next) == audio->pcm_buf_count)
1178 audio->read_next = 0;
1179 break;
1180 }
1181 }
1182
1183 /* don't feed output buffer to HW decoder during flushing
1184 * buffer refresh command will be sent once flush completes
1185 * send buf refresh command here can confuse HW decoder
1186 */
1187 if (audio->buf_refresh && !audio->rflush) {
1188 audio->buf_refresh = 0;
1189 MM_ERR("kick start pcm feedback again\n");
1190 audamrwb_buffer_refresh(audio);
1191 }
1192
1193 mutex_unlock(&audio->read_lock);
1194
1195 if (buf > start)
1196 rc = buf - start;
1197
1198 MM_DBG("read %d bytes\n", rc);
1199 return rc;
1200}
1201
1202static int audamrwb_process_eos(struct audio *audio,
1203 const char __user *buf_start, unsigned short mfield_size)
1204{
1205 struct buffer *frame;
1206 char *buf_ptr;
1207 int rc = 0;
1208
1209 MM_DBG("signal input EOS reserved=%d\n", audio->reserved);
1210 if (audio->reserved) {
1211 MM_DBG("Pass reserve byte\n");
1212 frame = audio->out + audio->out_head;
1213 buf_ptr = frame->data;
1214 rc = wait_event_interruptible(audio->write_wait,
1215 (frame->used == 0)
1216 || (audio->stopped)
1217 || (audio->wflush));
1218 if (rc < 0)
1219 goto done;
1220 if (audio->stopped || audio->wflush) {
1221 rc = -EBUSY;
1222 goto done;
1223 }
1224 buf_ptr[0] = audio->rsv_byte;
1225 buf_ptr[1] = 0;
1226 audio->out_head ^= 1;
1227 frame->mfield_sz = 0;
1228 audio->reserved = 0;
1229 frame->used = 2;
1230 audamrwb_send_data(audio, 0);
1231 }
1232
1233 MM_DBG("Now signal input EOS after reserved bytes %d %d %d\n",
1234 audio->out[0].used, audio->out[1].used, audio->out_needed);
1235
1236 frame = audio->out + audio->out_head;
1237
1238 rc = wait_event_interruptible(audio->write_wait,
1239 (audio->out_needed &&
1240 audio->out[0].used == 0 &&
1241 audio->out[1].used == 0)
1242 || (audio->stopped)
1243 || (audio->wflush));
1244
1245 if (rc < 0)
1246 goto done;
1247 if (audio->stopped || audio->wflush) {
1248 rc = -EBUSY;
1249 goto done;
1250 }
1251
1252 if (copy_from_user(frame->data, buf_start, mfield_size)) {
1253 rc = -EFAULT;
1254 goto done;
1255 }
1256
1257 frame->mfield_sz = mfield_size;
1258 audio->out_head ^= 1;
1259 frame->used = mfield_size;
1260 audamrwb_send_data(audio, 0);
1261
1262done:
1263 return rc;
1264}
1265
1266static ssize_t audamrwb_write(struct file *file, const char __user *buf,
1267 size_t count, loff_t *pos)
1268{
1269 struct audio *audio = file->private_data;
1270 const char __user *start = buf;
1271 struct buffer *frame;
1272 size_t xfer;
1273 char *cpy_ptr;
1274 int rc = 0, eos_condition = AUDAMRWB_EOS_NONE;
1275 unsigned short mfield_size = 0;
1276 unsigned dsize;
1277
1278 MM_DBG("cnt=%d\n", count);
1279
1280 mutex_lock(&audio->write_lock);
1281 while (count > 0) {
1282 frame = audio->out + audio->out_head;
1283 cpy_ptr = frame->data;
1284 dsize = 0;
1285 rc = wait_event_interruptible(audio->write_wait,
1286 (frame->used == 0)
1287 || (audio->stopped)
1288 || (audio->wflush));
1289
1290 MM_DBG("buffer available\n");
1291 if (rc < 0)
1292 break;
1293 if (audio->stopped || audio->wflush) {
1294 rc = -EBUSY;
1295 break;
1296 }
1297
1298 if (audio->mfield) {
1299 if (buf == start) {
1300 /* Processing beginning of user buffer */
1301 if (__get_user(mfield_size,
1302 (unsigned short __user *) buf)) {
1303 rc = -EFAULT;
1304 break;
1305 } else if (mfield_size > count) {
1306 rc = -EINVAL;
1307 break;
1308 }
1309 MM_DBG("mf offset_val %x\n", mfield_size);
1310 if (copy_from_user(cpy_ptr, buf, mfield_size)) {
1311 rc = -EFAULT;
1312 break;
1313 }
1314 /* Check if EOS flag is set and buffer
1315 * contains just meta field
1316 */
1317 if (cpy_ptr[AUDAMRWB_EOS_FLG_OFFSET] &
1318 AUDAMRWB_EOS_FLG_MASK) {
1319 MM_DBG("eos set\n");
1320 eos_condition = AUDAMRWB_EOS_SET;
1321 if (mfield_size == count) {
1322 buf += mfield_size;
1323 break;
1324 } else
1325 cpy_ptr[AUDAMRWB_EOS_FLG_OFFSET] &=
1326 ~AUDAMRWB_EOS_FLG_MASK;
1327 }
1328 cpy_ptr += mfield_size;
1329 count -= mfield_size;
1330 dsize += mfield_size;
1331 buf += mfield_size;
1332 } else {
1333 mfield_size = 0;
1334 MM_DBG("continuous buffer\n");
1335 }
1336 frame->mfield_sz = mfield_size;
1337 }
1338
1339 if (audio->reserved) {
1340 MM_DBG("append reserved byte %x\n", audio->rsv_byte);
1341 *cpy_ptr = audio->rsv_byte;
1342 xfer = (count > ((frame->size - mfield_size) - 1)) ?
1343 ((frame->size - mfield_size) - 1) : count;
1344 cpy_ptr++;
1345 dsize += 1;
1346 audio->reserved = 0;
1347 } else
1348 xfer = (count > (frame->size - mfield_size)) ?
1349 (frame->size - mfield_size) : count;
1350
1351 if (copy_from_user(cpy_ptr, buf, xfer)) {
1352 rc = -EFAULT;
1353 break;
1354 }
1355
1356 dsize += xfer;
1357 if (dsize & 1) {
1358 audio->rsv_byte = ((char *) frame->data)[dsize - 1];
1359 MM_DBG("odd length buf reserve last byte %x\n",
1360 audio->rsv_byte);
1361 audio->reserved = 1;
1362 dsize--;
1363 }
1364 count -= xfer;
1365 buf += xfer;
1366
1367 if (dsize > 0) {
1368 audio->out_head ^= 1;
1369 frame->used = dsize;
1370 audamrwb_send_data(audio, 0);
1371 }
1372 }
1373 MM_DBG("eos_condition %x buf[0x%x] start[0x%x]\n", eos_condition,
1374 (int) buf, (int) start);
1375 if (eos_condition == AUDAMRWB_EOS_SET)
1376 rc = audamrwb_process_eos(audio, start, mfield_size);
1377 mutex_unlock(&audio->write_lock);
1378 if (!rc) {
1379 if (buf > start)
1380 return buf - start;
1381 }
1382 return rc;
1383}
1384
1385static int audamrwb_release(struct inode *inode, struct file *file)
1386{
1387 struct audio *audio = file->private_data;
1388
1389 MM_INFO("audio instance 0x%08x freeing\n", (int)audio);
1390 mutex_lock(&audio->lock);
1391 audamrwb_disable(audio);
1392 if (audio->rmt_resource_released == 0)
1393 rmt_put_resource(audio);
1394 audamrwb_flush(audio);
1395 audamrwb_flush_pcm_buf(audio);
1396 msm_adsp_put(audio->audplay);
1397 audpp_adec_free(audio->dec_id);
1398#ifdef CONFIG_HAS_EARLYSUSPEND
1399 unregister_early_suspend(&audio->suspend_ctl.node);
1400#endif
1401 audio->event_abort = 1;
1402 wake_up(&audio->event_wait);
1403 audamrwb_reset_event_queue(audio);
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301404 ion_unmap_kernel(audio->client, audio->output_buff_handle);
1405 ion_free(audio->client, audio->output_buff_handle);
1406 if (audio->input_buff_handle != NULL) {
1407 ion_unmap_kernel(audio->client, audio->input_buff_handle);
1408 ion_free(audio->client, audio->input_buff_handle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001409 }
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301410 ion_client_destroy(audio->client);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001411 mutex_unlock(&audio->lock);
1412#ifdef CONFIG_DEBUG_FS
1413 if (audio->dentry)
1414 debugfs_remove(audio->dentry);
1415#endif
1416 kfree(audio);
1417 return 0;
1418}
1419
1420#ifdef CONFIG_HAS_EARLYSUSPEND
1421static void audamrwb_post_event(struct audio *audio, int type,
1422 union msm_audio_event_payload payload)
1423{
1424 struct audamrwb_event *e_node = NULL;
1425 unsigned long flags;
1426
1427 spin_lock_irqsave(&audio->event_queue_lock, flags);
1428
1429 if (!list_empty(&audio->free_event_queue)) {
1430 e_node = list_first_entry(&audio->free_event_queue,
1431 struct audamrwb_event, list);
1432 list_del(&e_node->list);
1433 } else {
1434 e_node = kmalloc(sizeof(struct audamrwb_event), GFP_ATOMIC);
1435 if (!e_node) {
1436 MM_ERR("No mem to post event %d\n", type);
1437 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
1438 return;
1439 }
1440 }
1441
1442 e_node->event_type = type;
1443 e_node->payload = payload;
1444
1445 list_add_tail(&e_node->list, &audio->event_queue);
1446 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
1447 wake_up(&audio->event_wait);
1448}
1449
1450static void audamrwb_suspend(struct early_suspend *h)
1451{
1452 struct audamrwb_suspend_ctl *ctl =
1453 container_of(h, struct audamrwb_suspend_ctl, node);
1454 union msm_audio_event_payload payload;
1455
1456 MM_DBG("\n"); /* Macro prints the file name and function */
1457 audamrwb_post_event(ctl->audio, AUDIO_EVENT_SUSPEND, payload);
1458}
1459
1460static void audamrwb_resume(struct early_suspend *h)
1461{
1462 struct audamrwb_suspend_ctl *ctl =
1463 container_of(h, struct audamrwb_suspend_ctl, node);
1464 union msm_audio_event_payload payload;
1465
1466 MM_DBG("\n"); /* Macro prints the file name and function */
1467 audamrwb_post_event(ctl->audio, AUDIO_EVENT_RESUME, payload);
1468}
1469#endif
1470
1471#ifdef CONFIG_DEBUG_FS
1472static ssize_t audamrwb_debug_open(struct inode *inode, struct file *file)
1473{
1474 file->private_data = inode->i_private;
1475 return 0;
1476}
1477
1478static ssize_t audamrwb_debug_read(struct file *file, char __user *buf,
1479 size_t count, loff_t *ppos)
1480{
1481 const int debug_bufmax = 1024;
1482 static char buffer[1024];
1483 int n = 0, i;
1484 struct audio *audio = file->private_data;
1485
1486 mutex_lock(&audio->lock);
1487 n = scnprintf(buffer, debug_bufmax, "opened %d\n", audio->opened);
1488 n += scnprintf(buffer + n, debug_bufmax - n,
1489 "enabled %d\n", audio->enabled);
1490 n += scnprintf(buffer + n, debug_bufmax - n,
1491 "stopped %d\n", audio->stopped);
1492 n += scnprintf(buffer + n, debug_bufmax - n,
1493 "pcm_feedback %d\n", audio->pcm_feedback);
1494 n += scnprintf(buffer + n, debug_bufmax - n,
1495 "out_buf_sz %d\n", audio->out[0].size);
1496 n += scnprintf(buffer + n, debug_bufmax - n,
1497 "pcm_buf_count %d \n", audio->pcm_buf_count);
1498 n += scnprintf(buffer + n, debug_bufmax - n,
1499 "pcm_buf_sz %d \n", audio->in[0].size);
1500 n += scnprintf(buffer + n, debug_bufmax - n,
1501 "volume %x \n", audio->vol_pan.volume);
1502 n += scnprintf(buffer + n, debug_bufmax - n,
1503 "sample rate %d \n", audio->out_sample_rate);
1504 n += scnprintf(buffer + n, debug_bufmax - n,
1505 "channel mode %d \n", audio->out_channel_mode);
1506 mutex_unlock(&audio->lock);
1507 /* Following variables are only useful for debugging when
1508 * when playback halts unexpectedly. Thus, no mutual exclusion
1509 * enforced
1510 */
1511 n += scnprintf(buffer + n, debug_bufmax - n,
1512 "wflush %d\n", audio->wflush);
1513 n += scnprintf(buffer + n, debug_bufmax - n,
1514 "rflush %d\n", audio->rflush);
1515 n += scnprintf(buffer + n, debug_bufmax - n,
1516 "running %d \n", audio->running);
1517 n += scnprintf(buffer + n, debug_bufmax - n,
1518 "dec state %d \n", audio->dec_state);
1519 n += scnprintf(buffer + n, debug_bufmax - n,
1520 "out_needed %d \n", audio->out_needed);
1521 n += scnprintf(buffer + n, debug_bufmax - n,
1522 "out_head %d \n", audio->out_head);
1523 n += scnprintf(buffer + n, debug_bufmax - n,
1524 "out_tail %d \n", audio->out_tail);
1525 n += scnprintf(buffer + n, debug_bufmax - n,
1526 "out[0].used %d \n", audio->out[0].used);
1527 n += scnprintf(buffer + n, debug_bufmax - n,
1528 "out[1].used %d \n", audio->out[1].used);
1529 n += scnprintf(buffer + n, debug_bufmax - n,
1530 "buffer_refresh %d \n", audio->buf_refresh);
1531 n += scnprintf(buffer + n, debug_bufmax - n,
1532 "read_next %d \n", audio->read_next);
1533 n += scnprintf(buffer + n, debug_bufmax - n,
1534 "fill_next %d \n", audio->fill_next);
1535 for (i = 0; i < audio->pcm_buf_count; i++)
1536 n += scnprintf(buffer + n, debug_bufmax - n,
1537 "in[%d].used %d \n", i, audio->in[i].used);
1538 buffer[n] = 0;
1539 return simple_read_from_buffer(buf, count, ppos, buffer, n);
1540}
1541
1542static const struct file_operations audamrwb_debug_fops = {
1543 .read = audamrwb_debug_read,
1544 .open = audamrwb_debug_open,
1545};
1546#endif
1547
1548static int audamrwb_open(struct inode *inode, struct file *file)
1549{
1550 struct audio *audio = NULL;
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301551 int rc = 0, dec_attrb, decid, i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001552 struct audamrwb_event *e_node = NULL;
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301553 unsigned mem_sz = DMASZ;
1554 unsigned long ionflag = 0;
1555 ion_phys_addr_t addr = 0;
1556 struct ion_handle *handle = NULL;
1557 struct ion_client *client = NULL;
1558 int len = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001559#ifdef CONFIG_DEBUG_FS
1560 /* 4 bytes represents decoder number, 1 byte for terminate string */
1561 char name[sizeof "msm_amrwb_" + 5];
1562#endif
1563
1564 /* Allocate Mem for audio instance */
1565 audio = kzalloc(sizeof(struct audio), GFP_KERNEL);
1566 if (!audio) {
1567 MM_ERR("No memory to allocate audio instance\n");
1568 rc = -ENOMEM;
1569 goto done;
1570 }
1571 MM_INFO("audio instance 0x%08x created\n", (int)audio);
1572
1573 /* Allocate the decoder */
1574 dec_attrb = AUDDEC_DEC_AMRWB;
1575 if (file->f_mode & FMODE_READ)
1576 dec_attrb |= MSM_AUD_MODE_NONTUNNEL;
1577 else
1578 dec_attrb |= MSM_AUD_MODE_TUNNEL;
1579
1580 decid = audpp_adec_alloc(dec_attrb, &audio->module_name,
1581 &audio->queue_id);
1582
1583 if (decid < 0) {
1584 MM_ERR("No free decoder available, freeing instance 0x%08x\n",
1585 (int)audio);
1586 rc = -ENODEV;
1587 kfree(audio);
1588 goto done;
1589 }
1590
1591 audio->dec_id = decid & MSM_AUD_DECODER_MASK;
1592
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301593 client = msm_ion_client_create(UINT_MAX, "Audio_AMR_WB_Client");
1594 if (IS_ERR_OR_NULL(client)) {
1595 pr_err("Unable to create ION client\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001596 rc = -ENOMEM;
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301597 goto client_create_error;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001598 }
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301599 audio->client = client;
1600
1601 handle = ion_alloc(client, mem_sz, SZ_4K,
Hanumant Singh7d72bad2012-08-29 18:39:44 -07001602 ION_HEAP(ION_AUDIO_HEAP_ID), 0);
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301603 if (IS_ERR_OR_NULL(handle)) {
1604 MM_ERR("Unable to create allocate O/P buffers\n");
1605 goto output_buff_alloc_error;
1606 }
1607 audio->output_buff_handle = handle;
1608
1609 rc = ion_phys(client, handle, &addr, &len);
1610 if (rc) {
1611 MM_ERR("O/P buffers:Invalid phy: %x sz: %x\n",
1612 (unsigned int) addr, (unsigned int) len);
1613 goto output_buff_get_phys_error;
1614 } else {
1615 MM_INFO("O/P buffers:valid phy: %x sz: %x\n",
1616 (unsigned int) addr, (unsigned int) len);
1617 }
1618 audio->phys = (int32_t)addr;
1619
1620
1621 rc = ion_handle_get_flags(client, handle, &ionflag);
1622 if (rc) {
1623 MM_ERR("could not get flags for the handle\n");
1624 goto output_buff_get_flags_error;
1625 }
1626
Mitchel Humpherys911b4b72012-09-12 14:42:50 -07001627 audio->map_v_write = ion_map_kernel(client, handle);
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301628 if (IS_ERR(audio->map_v_write)) {
1629 MM_ERR("could not map write buffers\n");
1630 rc = -ENOMEM;
1631 goto output_buff_map_error;
1632 }
1633 audio->data = audio->map_v_write;
1634 MM_DBG("write buf: phy addr 0x%08x kernel addr 0x%08x\n",
1635 audio->phys, (int)audio->data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001636
1637 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK) {
1638 rc = audmgr_open(&audio->audmgr);
1639 if (rc) {
1640 MM_ERR("audmgr open failed, freeing instance \
1641 0x%08x\n", (int)audio);
1642 goto err;
1643 }
1644 }
1645
1646 rc = msm_adsp_get(audio->module_name, &audio->audplay,
1647 &audplay_adsp_ops_amrwb, audio);
1648 if (rc) {
1649 MM_ERR("failed to get %s module, freeing instance 0x%08x\n",
1650 audio->module_name, (int)audio);
1651 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK)
1652 audmgr_close(&audio->audmgr);
1653 goto err;
1654 }
1655
1656 rc = rmt_get_resource(audio);
1657 if (rc) {
1658 MM_ERR("ADSP resources are not available for AMRWB session \
1659 0x%08x on decoder: %d\n", (int)audio, audio->dec_id);
1660 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK)
1661 audmgr_close(&audio->audmgr);
1662 msm_adsp_put(audio->audplay);
1663 goto err;
1664 }
1665
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301666 audio->input_buff_handle = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001667 mutex_init(&audio->lock);
1668 mutex_init(&audio->write_lock);
1669 mutex_init(&audio->read_lock);
1670 mutex_init(&audio->get_event_lock);
1671 spin_lock_init(&audio->dsp_lock);
1672 spin_lock_init(&audio->event_queue_lock);
1673 INIT_LIST_HEAD(&audio->free_event_queue);
1674 INIT_LIST_HEAD(&audio->event_queue);
1675 init_waitqueue_head(&audio->write_wait);
1676 init_waitqueue_head(&audio->read_wait);
1677 init_waitqueue_head(&audio->wait);
1678 init_waitqueue_head(&audio->event_wait);
1679
1680 audio->out[0].data = audio->data + 0;
1681 audio->out[0].addr = audio->phys + 0;
1682 audio->out[0].size = BUFSZ;
1683
1684 audio->out[1].data = audio->data + BUFSZ;
1685 audio->out[1].addr = audio->phys + BUFSZ;
1686 audio->out[1].size = BUFSZ;
1687
1688 audio->vol_pan.volume = 0x2000;
1689 audio->vol_pan.pan = 0x0;
1690 audio->eq_enable = 0;
1691 audio->out_sample_rate = 44100;
1692 audio->out_channel_mode = AUDPP_CMD_PCM_INTF_STEREO_V;
1693
1694 audamrwb_flush(audio);
1695
1696 file->private_data = audio;
1697 audio->opened = 1;
1698 audio->event_abort = 0;
1699#ifdef CONFIG_DEBUG_FS
1700 snprintf(name, sizeof name, "msm_amrwb_%04x", audio->dec_id);
1701 audio->dentry = debugfs_create_file(name, S_IFREG | S_IRUGO,
1702 NULL, (void *) audio, &audamrwb_debug_fops);
1703
1704 if (IS_ERR(audio->dentry))
1705 MM_DBG("debugfs_create_file failed\n");
1706#endif
1707#ifdef CONFIG_HAS_EARLYSUSPEND
1708 audio->suspend_ctl.node.level = EARLY_SUSPEND_LEVEL_DISABLE_FB;
1709 audio->suspend_ctl.node.resume = audamrwb_resume;
1710 audio->suspend_ctl.node.suspend = audamrwb_suspend;
1711 audio->suspend_ctl.audio = audio;
1712 register_early_suspend(&audio->suspend_ctl.node);
1713#endif
1714 for (i = 0; i < AUDAMRWB_EVENT_NUM; i++) {
1715 e_node = kmalloc(sizeof(struct audamrwb_event), GFP_KERNEL);
1716 if (e_node)
1717 list_add_tail(&e_node->list, &audio->free_event_queue);
1718 else {
1719 MM_ERR("event pkt alloc failed\n");
1720 break;
1721 }
1722 }
1723done:
1724 return rc;
1725err:
Sidipotu Ashok172c98b2012-06-26 17:58:29 +05301726 ion_unmap_kernel(client, audio->output_buff_handle);
1727output_buff_map_error:
1728output_buff_get_phys_error:
1729output_buff_get_flags_error:
1730 ion_free(client, audio->output_buff_handle);
1731output_buff_alloc_error:
1732 ion_client_destroy(client);
1733client_create_error:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001734 audpp_adec_free(audio->dec_id);
1735 kfree(audio);
1736 return rc;
1737}
1738
1739static const struct file_operations audio_amrwb_fops = {
1740 .owner = THIS_MODULE,
1741 .open = audamrwb_open,
1742 .release = audamrwb_release,
1743 .read = audamrwb_read,
1744 .write = audamrwb_write,
1745 .unlocked_ioctl = audamrwb_ioctl,
1746 .fsync = audamrwb_fsync,
1747};
1748
1749struct miscdevice audio_amrwb_misc = {
1750 .minor = MISC_DYNAMIC_MINOR,
1751 .name = "msm_amrwb",
1752 .fops = &audio_amrwb_fops,
1753};
1754
1755static int __init audamrwb_init(void)
1756{
1757 return misc_register(&audio_amrwb_misc);
1758}
1759
1760static void __exit audamrwb_exit(void)
1761{
1762 misc_deregister(&audio_amrwb_misc);
1763}
1764
1765module_init(audamrwb_init);
1766module_exit(audamrwb_exit);
1767
1768MODULE_DESCRIPTION("MSM AMR-WB driver");
1769MODULE_LICENSE("GPL v2");