blob: 66b93548e5e62b5a73607580fb963cbe88a08d9d [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>
Sidipotu Ashok172c98b2012-06-26 17:58:29 +053044#include <linux/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>
51#include <mach/qdsp5/qdsp5audplaycmdi.h>
52#include <mach/qdsp5/qdsp5audplaymsg.h>
53#include <mach/qdsp5/qdsp5rmtcmdi.h>
54#include <mach/debug_mm.h>
Santosh Mardi0be3b8e2011-07-06 10:00:21 +053055#include <mach/msm_memtypes.h>
56
57#include "audmgr.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070058
59#define BUFSZ 4110 /* Hold minimum 700ms voice data and 14 bytes of meta in*/
60#define DMASZ (BUFSZ * 2)
61
62#define AUDPLAY_INVALID_READ_PTR_OFFSET 0xFFFF
63#define AUDDEC_DEC_AMRWB 11
64
65#define PCM_BUFSZ_MIN 8216 /* 100ms worth of data and 24 bytes of meta out*/
66#define PCM_BUF_MAX_COUNT 5 /* DSP only accepts 5 buffers at most
67 but support 2 buffers currently */
68#define ROUTING_MODE_FTRT 1
69#define ROUTING_MODE_RT 2
70/* Decoder status received from AUDPPTASK */
71#define AUDPP_DEC_STATUS_SLEEP 0
72#define AUDPP_DEC_STATUS_INIT 1
73#define AUDPP_DEC_STATUS_CFG 2
74#define AUDPP_DEC_STATUS_PLAY 3
75
76#define AUDAMRWB_METAFIELD_MASK 0xFFFF0000
77#define AUDAMRWB_EOS_FLG_OFFSET 0x0A /* Offset from beginning of buffer */
78#define AUDAMRWB_EOS_FLG_MASK 0x01
79#define AUDAMRWB_EOS_NONE 0x0 /* No EOS detected */
80#define AUDAMRWB_EOS_SET 0x1 /* EOS set in meta field */
81
82#define AUDAMRWB_EVENT_NUM 10 /* Default number of pre-allocated event pkts */
83
84struct buffer {
85 void *data;
86 unsigned size;
87 unsigned used; /* Input usage actual DSP produced PCM size */
88 unsigned addr;
89 unsigned short mfield_sz; /*only useful for data has meta field */
90};
91
92#ifdef CONFIG_HAS_EARLYSUSPEND
93struct audamrwb_suspend_ctl {
94 struct early_suspend node;
95 struct audio *audio;
96};
97#endif
98
99struct audamrwb_event{
100 struct list_head list;
101 int event_type;
102 union msm_audio_event_payload payload;
103};
104
105struct audio {
106 struct buffer out[2];
107
108 spinlock_t dsp_lock;
109
110 uint8_t out_head;
111 uint8_t out_tail;
112 uint8_t out_needed; /* number of buffers the dsp is waiting for */
113
114 atomic_t out_bytes;
115
116 struct mutex lock;
117 struct mutex write_lock;
118 wait_queue_head_t write_wait;
119
120 /* Host PCM section */
121 struct buffer in[PCM_BUF_MAX_COUNT];
122 struct mutex read_lock;
123 wait_queue_head_t read_wait; /* Wait queue for read */
124 char *read_data; /* pointer to reader buffer */
125 int32_t read_phys; /* physical address of reader buffer */
126 uint8_t read_next; /* index to input buffers to be read next */
127 uint8_t fill_next; /* index to buffer that DSP should be filling */
128 uint8_t pcm_buf_count; /* number of pcm buffer allocated */
129 /* ---- End of Host PCM section */
130
131 struct msm_adsp_module *audplay;
132 struct audmgr audmgr;
133
134 /* configuration to use on next enable */
135 uint32_t out_sample_rate;
136 uint32_t out_channel_mode;
137
138 /* data allocated for various buffers */
139 char *data;
140 int32_t phys; /* physical address of write buffer */
Laura Abbott35111d32012-04-27 18:41:48 -0700141 void *map_v_read;
142 void *map_v_write;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700143 int mfield; /* meta field embedded in data */
144 int rflush; /* Read flush */
145 int wflush; /* Write flush */
146 int opened;
147 int enabled;
148 int running;
149 int stopped; /* set when stopped, cleared on flush */
150 int pcm_feedback;
151 int buf_refresh;
152 int rmt_resource_released;
153 int teos; /* valid only if tunnel mode & no data left for decoder */
154 enum msm_aud_decoder_state dec_state; /* Represents decoder state */
155 int reserved; /* A byte is being reserved */
156 char rsv_byte; /* Handle odd length user data */
157
158 const char *module_name;
159 unsigned queue_id;
160 uint16_t dec_id;
161 uint32_t read_ptr_offset;
162
163#ifdef CONFIG_HAS_EARLYSUSPEND
164 struct audamrwb_suspend_ctl suspend_ctl;
165#endif
166
167#ifdef CONFIG_DEBUG_FS
168 struct dentry *dentry;
169#endif
170
171 wait_queue_head_t wait;
172 struct list_head free_event_queue;
173 struct list_head event_queue;
174 wait_queue_head_t event_wait;
175 spinlock_t event_queue_lock;
176 struct mutex get_event_lock;
177 int event_abort;
178
179 int eq_enable;
180 int eq_needs_commit;
181 audpp_cmd_cfg_object_params_eqalizer eq;
182 audpp_cmd_cfg_object_params_volume vol_pan;
Sidipotu Ashok172c98b2012-06-26 17:58:29 +0530183 struct ion_client *client;
184 struct ion_handle *input_buff_handle;
185 struct ion_handle *output_buff_handle;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700186};
187
188static int auddec_dsp_config(struct audio *audio, int enable);
189static void audpp_cmd_cfg_adec_params(struct audio *audio);
190static void audpp_cmd_cfg_routing_mode(struct audio *audio);
191static void audamrwb_send_data(struct audio *audio, unsigned needed);
192static void audamrwb_config_hostpcm(struct audio *audio);
193static void audamrwb_buffer_refresh(struct audio *audio);
194static void audamrwb_dsp_event(void *private, unsigned id, uint16_t *msg);
195#ifdef CONFIG_HAS_EARLYSUSPEND
196static void audamrwb_post_event(struct audio *audio, int type,
197 union msm_audio_event_payload payload);
198#endif
199
200static int rmt_put_resource(struct audio *audio)
201{
202 struct aud_codec_config_cmd cmd;
203 unsigned short client_idx;
204
205 cmd.cmd_id = RM_CMD_AUD_CODEC_CFG;
206 cmd.client_id = RM_AUD_CLIENT_ID;
207 cmd.task_id = audio->dec_id;
208 cmd.enable = RMT_DISABLE;
209 cmd.dec_type = AUDDEC_DEC_AMRWB;
210 client_idx = ((cmd.client_id << 8) | cmd.task_id);
211
212 return put_adsp_resource(client_idx, &cmd, sizeof(cmd));
213}
214
215static int rmt_get_resource(struct audio *audio)
216{
217 struct aud_codec_config_cmd cmd;
218 unsigned short client_idx;
219
220 cmd.cmd_id = RM_CMD_AUD_CODEC_CFG;
221 cmd.client_id = RM_AUD_CLIENT_ID;
222 cmd.task_id = audio->dec_id;
223 cmd.enable = RMT_ENABLE;
224 cmd.dec_type = AUDDEC_DEC_AMRWB;
225 client_idx = ((cmd.client_id << 8) | cmd.task_id);
226
227 return get_adsp_resource(client_idx, &cmd, sizeof(cmd));
228}
229
230/* must be called with audio->lock held */
231static int audamrwb_enable(struct audio *audio)
232{
233 struct audmgr_config cfg;
234 int rc;
235
236 MM_DBG("\n"); /* Macro prints the file name and function */
237
238 if (audio->enabled)
239 return 0;
240
241 if (audio->rmt_resource_released == 1) {
242 audio->rmt_resource_released = 0;
243 rc = rmt_get_resource(audio);
244 if (rc) {
245 MM_ERR("ADSP resources are not available for AMRWB \
246 session 0x%08x on decoder: %d\n Ignoring \
247 error and going ahead with the playback\n",
248 (int)audio, audio->dec_id);
249 }
250 }
251
252 audio->dec_state = MSM_AUD_DECODER_STATE_NONE;
253 audio->out_tail = 0;
254 audio->out_needed = 0;
255
256 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK) {
257 cfg.tx_rate = RPC_AUD_DEF_SAMPLE_RATE_NONE;
258 cfg.rx_rate = RPC_AUD_DEF_SAMPLE_RATE_48000;
259 cfg.def_method = RPC_AUD_DEF_METHOD_PLAYBACK;
260 cfg.codec = RPC_AUD_DEF_CODEC_AMR_WB;
261 cfg.snd_method = RPC_SND_METHOD_MIDI;
262
263 rc = audmgr_enable(&audio->audmgr, &cfg);
264 if (rc < 0)
265 return rc;
266 }
267
268 if (msm_adsp_enable(audio->audplay)) {
269 MM_ERR("msm_adsp_enable(audplay) failed\n");
270 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK)
271 audmgr_disable(&audio->audmgr);
272 return -ENODEV;
273 }
274
275 if (audpp_enable(audio->dec_id, audamrwb_dsp_event, audio)) {
276 MM_ERR("audpp_enable() failed\n");
277 msm_adsp_disable(audio->audplay);
278 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK)
279 audmgr_disable(&audio->audmgr);
280 return -ENODEV;
281 }
282 audio->enabled = 1;
283 return 0;
284}
285
286/* must be called with audio->lock held */
287static int audamrwb_disable(struct audio *audio)
288{
289 int rc = 0;
290 MM_DBG("\n"); /* Macro prints the file name and function */
291 if (audio->enabled) {
292 audio->enabled = 0;
293 audio->dec_state = MSM_AUD_DECODER_STATE_NONE;
294 auddec_dsp_config(audio, 0);
295 rc = wait_event_interruptible_timeout(audio->wait,
296 audio->dec_state != MSM_AUD_DECODER_STATE_NONE,
297 msecs_to_jiffies(MSM_AUD_DECODER_WAIT_MS));
298 if (rc == 0)
299 rc = -ETIMEDOUT;
300 else if (audio->dec_state != MSM_AUD_DECODER_STATE_CLOSE)
301 rc = -EFAULT;
302 else
303 rc = 0;
Manish Dewangan89a9f232012-02-09 17:14:40 +0530304 audio->stopped = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700305 wake_up(&audio->write_wait);
306 wake_up(&audio->read_wait);
307 msm_adsp_disable(audio->audplay);
308 audpp_disable(audio->dec_id, audio);
309 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK)
310 audmgr_disable(&audio->audmgr);
311 audio->out_needed = 0;
312 rmt_put_resource(audio);
313 audio->rmt_resource_released = 1;
314 }
315 return rc;
316}
317
318/* ------------------- dsp --------------------- */
319static void audamrwb_update_pcm_buf_entry(struct audio *audio,
320 uint32_t *payload)
321{
322 uint8_t index;
323 unsigned long flags;
324
325 if (audio->rflush)
326 return;
327
328 spin_lock_irqsave(&audio->dsp_lock, flags);
329 for (index = 0; index < payload[1]; index++) {
330 if (audio->in[audio->fill_next].addr ==
331 payload[2 + index * 2]) {
332 MM_DBG("in[%d] ready\n", audio->fill_next);
333 audio->in[audio->fill_next].used =
334 payload[3 + index * 2];
335 if ((++audio->fill_next) == audio->pcm_buf_count)
336 audio->fill_next = 0;
337
338 } else {
339 MM_ERR("expected=%x ret=%x\n",
340 audio->in[audio->fill_next].addr,
341 payload[1 + index * 2]);
342 break;
343 }
344 }
345 if (audio->in[audio->fill_next].used == 0) {
346 audamrwb_buffer_refresh(audio);
347 } else {
348 MM_DBG("read cannot keep up\n");
349 audio->buf_refresh = 1;
350 }
351 wake_up(&audio->read_wait);
352 spin_unlock_irqrestore(&audio->dsp_lock, flags);
353}
354
355static void audplay_dsp_event(void *data, unsigned id, size_t len,
356 void (*getevent) (void *ptr, size_t len))
357{
358 struct audio *audio = data;
359 uint32_t msg[28];
360 getevent(msg, sizeof(msg));
361
362 MM_DBG("msg_id=%x\n", id);
363
364 switch (id) {
365 case AUDPLAY_MSG_DEC_NEEDS_DATA:
366 audamrwb_send_data(audio, 1);
367 break;
368
369 case AUDPLAY_MSG_BUFFER_UPDATE:
370 audamrwb_update_pcm_buf_entry(audio, msg);
371 break;
372
373 default:
374 MM_ERR("unexpected message from decoder\n");
375 }
376}
377
378static void audamrwb_dsp_event(void *private, unsigned id, uint16_t *msg)
379{
380 struct audio *audio = private;
381
382 switch (id) {
383 case AUDPP_MSG_STATUS_MSG:{
384 unsigned status = msg[1];
385
386 switch (status) {
387 case AUDPP_DEC_STATUS_SLEEP: {
388 uint16_t reason = msg[2];
389 MM_DBG("decoder status:sleep reason=0x%04x\n",
390 reason);
391 if ((reason == AUDPP_MSG_REASON_MEM)
392 || (reason ==
393 AUDPP_MSG_REASON_NODECODER)) {
394 audio->dec_state =
395 MSM_AUD_DECODER_STATE_FAILURE;
396 wake_up(&audio->wait);
397 } else if (reason == AUDPP_MSG_REASON_NONE) {
398 /* decoder is in disable state */
399 audio->dec_state =
400 MSM_AUD_DECODER_STATE_CLOSE;
401 wake_up(&audio->wait);
402 }
403 break;
404 }
405 case AUDPP_DEC_STATUS_INIT:
406 MM_DBG("decoder status: init\n");
407 if (audio->pcm_feedback)
408 audpp_cmd_cfg_routing_mode(audio);
409 else
410 audpp_cmd_cfg_adec_params(audio);
411 break;
412
413 case AUDPP_DEC_STATUS_CFG:
414 MM_DBG("decoder status: cfg\n");
415 break;
416 case AUDPP_DEC_STATUS_PLAY:
417 MM_DBG("decoder status: play\n");
418 if (audio->pcm_feedback) {
419 audamrwb_config_hostpcm(audio);
420 audamrwb_buffer_refresh(audio);
421 }
422 audio->dec_state =
423 MSM_AUD_DECODER_STATE_SUCCESS;
424 wake_up(&audio->wait);
425 break;
426 default:
427 MM_DBG("unknown decoder status\n");
428 break;
429 }
430 break;
431 }
432 case AUDPP_MSG_CFG_MSG:
433 if (msg[0] == AUDPP_MSG_ENA_ENA) {
434 MM_DBG("CFG_MSG ENABLE\n");
435 auddec_dsp_config(audio, 1);
436 audio->out_needed = 0;
437 audio->running = 1;
438 audpp_dsp_set_vol_pan(audio->dec_id, &audio->vol_pan);
439 audpp_dsp_set_eq(audio->dec_id, audio->eq_enable,
440 &audio->eq);
441 audpp_avsync(audio->dec_id, 22050);
442 } else if (msg[0] == AUDPP_MSG_ENA_DIS) {
443 MM_DBG("CFG_MSG DISABLE\n");
444 audpp_avsync(audio->dec_id, 0);
445 audio->running = 0;
446 } else {
447 MM_DBG("CFG_MSG %d?\n", msg[0]);
448 }
449 break;
450 case AUDPP_MSG_ROUTING_ACK:
451 MM_DBG("ROUTING_ACK mode=%d\n", msg[1]);
452 audpp_cmd_cfg_adec_params(audio);
453 break;
454 case AUDPP_MSG_FLUSH_ACK:
455 MM_DBG("FLUSH_ACK\n");
456 audio->wflush = 0;
457 audio->rflush = 0;
458 wake_up(&audio->write_wait);
459 if (audio->pcm_feedback)
460 audamrwb_buffer_refresh(audio);
461 break;
462 case AUDPP_MSG_PCMDMAMISSED:
463 MM_DBG("PCMDMAMISSED\n");
464 audio->teos = 1;
465 wake_up(&audio->write_wait);
466 break;
467 default:
468 MM_DBG("UNKNOWN (%d)\n", id);
469 }
470
471}
472
473struct msm_adsp_ops audplay_adsp_ops_amrwb = {
474 .event = audplay_dsp_event,
475};
476
477#define audplay_send_queue0(audio, cmd, len) \
478 msm_adsp_write(audio->audplay, audio->queue_id, \
479 cmd, len)
480
481static int auddec_dsp_config(struct audio *audio, int enable)
482{
483 u16 cfg_dec_cmd[AUDPP_CMD_CFG_DEC_TYPE_LEN / sizeof(unsigned short)];
484
485 memset(cfg_dec_cmd, 0, sizeof(cfg_dec_cmd));
486 cfg_dec_cmd[0] = AUDPP_CMD_CFG_DEC_TYPE;
487 if (enable)
488 cfg_dec_cmd[1 + audio->dec_id] = AUDPP_CMD_UPDATDE_CFG_DEC |
489 AUDPP_CMD_ENA_DEC_V | AUDDEC_DEC_AMRWB;
490 else
491 cfg_dec_cmd[1 + audio->dec_id] = AUDPP_CMD_UPDATDE_CFG_DEC |
492 AUDPP_CMD_DIS_DEC_V;
493
494 return audpp_send_queue1(&cfg_dec_cmd, sizeof(cfg_dec_cmd));
495}
496
497static void audpp_cmd_cfg_adec_params(struct audio *audio)
498{
499 struct audpp_cmd_cfg_adec_params_amrwb cmd;
500
501 memset(&cmd, 0, sizeof(cmd));
502 cmd.common.cmd_id = AUDPP_CMD_CFG_ADEC_PARAMS;
503 cmd.common.length = AUDPP_CMD_CFG_ADEC_PARAMS_AMRWB_LEN;
504 cmd.common.dec_id = audio->dec_id;
505 cmd.common.input_sampling_frequency = audio->out_sample_rate;
506 cmd.stereo_cfg = audio->out_channel_mode;
507 audpp_send_queue2(&cmd, sizeof(cmd));
508}
509
510static void audpp_cmd_cfg_routing_mode(struct audio *audio)
511{
512 struct audpp_cmd_routing_mode cmd;
513 MM_DBG("\n"); /* Macro prints the file name and function */
514 memset(&cmd, 0, sizeof(cmd));
515 cmd.cmd_id = AUDPP_CMD_ROUTING_MODE;
516 cmd.object_number = audio->dec_id;
517 if (audio->pcm_feedback)
518 cmd.routing_mode = ROUTING_MODE_FTRT;
519 else
520 cmd.routing_mode = ROUTING_MODE_RT;
521
522 audpp_send_queue1(&cmd, sizeof(cmd));
523}
524
525static int audplay_dsp_send_data_avail(struct audio *audio,
526 unsigned idx, unsigned len)
527{
528 struct audplay_cmd_bitstream_data_avail_nt2 cmd;
529
530 cmd.cmd_id = AUDPLAY_CMD_BITSTREAM_DATA_AVAIL_NT2;
531 if (audio->mfield)
532 cmd.decoder_id = AUDAMRWB_METAFIELD_MASK |
533 (audio->out[idx].mfield_sz >> 1);
534 else
535 cmd.decoder_id = audio->dec_id;
536 cmd.buf_ptr = audio->out[idx].addr;
537 cmd.buf_size = len / 2;
538 cmd.partition_number = 0;
539 return audplay_send_queue0(audio, &cmd, sizeof(cmd));
540}
541
542static void audamrwb_buffer_refresh(struct audio *audio)
543{
544 struct audplay_cmd_buffer_refresh refresh_cmd;
545
546 refresh_cmd.cmd_id = AUDPLAY_CMD_BUFFER_REFRESH;
547 refresh_cmd.num_buffers = 1;
548 refresh_cmd.buf0_address = audio->in[audio->fill_next].addr;
549 refresh_cmd.buf0_length = audio->in[audio->fill_next].size;
550 refresh_cmd.buf_read_count = 0;
551 MM_DBG("buf0_addr=%x buf0_len=%d\n", refresh_cmd.buf0_address,
552 refresh_cmd.buf0_length);
553 (void)audplay_send_queue0(audio, &refresh_cmd, sizeof(refresh_cmd));
554}
555
556static void audamrwb_config_hostpcm(struct audio *audio)
557{
558 struct audplay_cmd_hpcm_buf_cfg cfg_cmd;
559
560 MM_DBG("\n"); /* Macro prints the file name and function */
561 cfg_cmd.cmd_id = AUDPLAY_CMD_HPCM_BUF_CFG;
562 cfg_cmd.max_buffers = audio->pcm_buf_count;
563 cfg_cmd.byte_swap = 0;
564 cfg_cmd.hostpcm_config = (0x8000) | (0x4000);
565 cfg_cmd.feedback_frequency = 1;
566 cfg_cmd.partition_number = 0;
567 (void)audplay_send_queue0(audio, &cfg_cmd, sizeof(cfg_cmd));
568
569}
570
571static void audamrwb_send_data(struct audio *audio, unsigned needed)
572{
573 struct buffer *frame;
574 unsigned long flags;
575
576 spin_lock_irqsave(&audio->dsp_lock, flags);
577 if (!audio->running)
578 goto done;
579
580 if (needed && !audio->wflush) {
581 /* We were called from the callback because the DSP
582 * requested more data. Note that the DSP does want
583 * more data, and if a buffer was in-flight, mark it
584 * as available (since the DSP must now be done with
585 * it).
586 */
587 audio->out_needed = 1;
588 frame = audio->out + audio->out_tail;
589 if (frame->used == 0xffffffff) {
590 frame->used = 0;
591 audio->out_tail ^= 1;
592 wake_up(&audio->write_wait);
593 }
594 }
595
596 if (audio->out_needed) {
597 /* If the DSP currently wants data and we have a
598 * buffer available, we will send it and reset
599 * the needed flag. We'll mark the buffer as in-flight
600 * so that it won't be recycled until the next buffer
601 * is requested
602 */
603
604 frame = audio->out + audio->out_tail;
605 if (frame->used) {
606 BUG_ON(frame->used == 0xffffffff);
607 MM_DBG("frame %d busy\n", audio->out_tail);
608 audplay_dsp_send_data_avail(audio, audio->out_tail,
609 frame->used);
610 frame->used = 0xffffffff;
611 audio->out_needed = 0;
612 }
613 }
614 done:
615 spin_unlock_irqrestore(&audio->dsp_lock, flags);
616}
617
618/* ------------------- device --------------------- */
619
620static void audamrwb_flush(struct audio *audio)
621{
Manish Dewangana4f1df02012-02-08 17:06:54 +0530622 unsigned long flags;
623
624 spin_lock_irqsave(&audio->dsp_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700625 audio->out[0].used = 0;
626 audio->out[1].used = 0;
627 audio->out_head = 0;
628 audio->out_tail = 0;
629 audio->reserved = 0;
630 audio->out_needed = 0;
Manish Dewangana4f1df02012-02-08 17:06:54 +0530631 spin_unlock_irqrestore(&audio->dsp_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700632 atomic_set(&audio->out_bytes, 0);
633}
634
635static void audamrwb_flush_pcm_buf(struct audio *audio)
636{
637 uint8_t index;
Manish Dewangana4f1df02012-02-08 17:06:54 +0530638 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700639
Manish Dewangana4f1df02012-02-08 17:06:54 +0530640 spin_lock_irqsave(&audio->dsp_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700641 for (index = 0; index < PCM_BUF_MAX_COUNT; index++)
642 audio->in[index].used = 0;
643
644 audio->buf_refresh = 0;
645 audio->read_next = 0;
646 audio->fill_next = 0;
Manish Dewangana4f1df02012-02-08 17:06:54 +0530647 spin_unlock_irqrestore(&audio->dsp_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700648}
649
650static void audamrwb_ioport_reset(struct audio *audio)
651{
652 /* Make sure read/write thread are free from
653 * sleep and knowing that system is not able
654 * to process io request at the moment
655 */
656 wake_up(&audio->write_wait);
657 mutex_lock(&audio->write_lock);
658 audamrwb_flush(audio);
659 mutex_unlock(&audio->write_lock);
660 wake_up(&audio->read_wait);
661 mutex_lock(&audio->read_lock);
662 audamrwb_flush_pcm_buf(audio);
663 mutex_unlock(&audio->read_lock);
664}
665
666static int audamrwb_events_pending(struct audio *audio)
667{
668 unsigned long flags;
669 int empty;
670
671 spin_lock_irqsave(&audio->event_queue_lock, flags);
672 empty = !list_empty(&audio->event_queue);
673 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
674 return empty || audio->event_abort;
675}
676
677static void audamrwb_reset_event_queue(struct audio *audio)
678{
679 unsigned long flags;
680 struct audamrwb_event *drv_evt;
681 struct list_head *ptr, *next;
682
683 spin_lock_irqsave(&audio->event_queue_lock, flags);
684 list_for_each_safe(ptr, next, &audio->event_queue) {
685 drv_evt = list_first_entry(&audio->event_queue,
686 struct audamrwb_event, list);
687 list_del(&drv_evt->list);
688 kfree(drv_evt);
689 }
690 list_for_each_safe(ptr, next, &audio->free_event_queue) {
691 drv_evt = list_first_entry(&audio->free_event_queue,
692 struct audamrwb_event, list);
693 list_del(&drv_evt->list);
694 kfree(drv_evt);
695 }
696 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
697
698 return;
699}
700
701static long audamrwb_process_event_req(struct audio *audio, void __user *arg)
702{
703 long rc;
704 struct msm_audio_event usr_evt;
705 struct audamrwb_event *drv_evt = NULL;
706 int timeout;
707 unsigned long flags;
708
709 if (copy_from_user(&usr_evt, arg, sizeof(struct msm_audio_event)))
710 return -EFAULT;
711
712 timeout = (int) usr_evt.timeout_ms;
713
714 if (timeout > 0) {
715 rc = wait_event_interruptible_timeout(
716 audio->event_wait, audamrwb_events_pending(audio),
717 msecs_to_jiffies(timeout));
718 if (rc == 0)
719 return -ETIMEDOUT;
720 } else {
721 rc = wait_event_interruptible(
722 audio->event_wait, audamrwb_events_pending(audio));
723 }
724
725 if (rc < 0)
726 return rc;
727
728 if (audio->event_abort) {
729 audio->event_abort = 0;
730 return -ENODEV;
731 }
732
733 rc = 0;
734
735 spin_lock_irqsave(&audio->event_queue_lock, flags);
736 if (!list_empty(&audio->event_queue)) {
737 drv_evt = list_first_entry(&audio->event_queue,
738 struct audamrwb_event, list);
739 list_del(&drv_evt->list);
740 }
741
742 if (drv_evt) {
743 usr_evt.event_type = drv_evt->event_type;
744 usr_evt.event_payload = drv_evt->payload;
745 list_add_tail(&drv_evt->list, &audio->free_event_queue);
746 } else
747 rc = -1;
748 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
749
750 if (!rc && copy_to_user(arg, &usr_evt, sizeof(usr_evt)))
751 rc = -EFAULT;
752
753 return rc;
754}
755
756static int audio_enable_eq(struct audio *audio, int enable)
757{
758 if (audio->eq_enable == enable && !audio->eq_needs_commit)
759 return 0;
760
761 audio->eq_enable = enable;
762
763 if (audio->running) {
764 audpp_dsp_set_eq(audio->dec_id, enable, &audio->eq);
765 audio->eq_needs_commit = 0;
766 }
767 return 0;
768}
769
770static long audamrwb_ioctl(struct file *file, unsigned int cmd,
771 unsigned long arg)
772{
773 struct audio *audio = file->private_data;
774 int rc = -EINVAL;
775 unsigned long flags = 0;
776 uint16_t enable_mask;
777 int enable;
778 int prev_state;
Sidipotu Ashok172c98b2012-06-26 17:58:29 +0530779 unsigned long ionflag = 0;
780 ion_phys_addr_t addr = 0;
781 struct ion_handle *handle = NULL;
782 int len = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700783
784 MM_DBG("cmd = %d\n", cmd);
785
786 if (cmd == AUDIO_GET_STATS) {
787 struct msm_audio_stats stats;
788 stats.byte_count = audpp_avsync_byte_count(audio->dec_id);
789 stats.sample_count = audpp_avsync_sample_count(audio->dec_id);
790 if (copy_to_user((void *)arg, &stats, sizeof(stats)))
791 return -EFAULT;
792 return 0;
793 }
794
795 switch (cmd) {
796 case AUDIO_ENABLE_AUDPP:
797 if (copy_from_user(&enable_mask, (void *) arg,
798 sizeof(enable_mask))) {
799 rc = -EFAULT;
800 break;
801 }
802
803 spin_lock_irqsave(&audio->dsp_lock, flags);
804 enable = (enable_mask & EQ_ENABLE) ? 1 : 0;
805 audio_enable_eq(audio, enable);
806 spin_unlock_irqrestore(&audio->dsp_lock, flags);
807 rc = 0;
808 break;
809 case AUDIO_SET_VOLUME:
810 spin_lock_irqsave(&audio->dsp_lock, flags);
811 audio->vol_pan.volume = arg;
812 if (audio->running)
813 audpp_dsp_set_vol_pan(audio->dec_id, &audio->vol_pan);
814 spin_unlock_irqrestore(&audio->dsp_lock, flags);
815 rc = 0;
816 break;
817
818 case AUDIO_SET_PAN:
819 spin_lock_irqsave(&audio->dsp_lock, flags);
820 audio->vol_pan.pan = arg;
821 if (audio->running)
822 audpp_dsp_set_vol_pan(audio->dec_id, &audio->vol_pan);
823 spin_unlock_irqrestore(&audio->dsp_lock, flags);
824 rc = 0;
825 break;
826
827 case AUDIO_SET_EQ:
828 prev_state = audio->eq_enable;
829 audio->eq_enable = 0;
830 if (copy_from_user(&audio->eq.num_bands, (void *) arg,
831 sizeof(audio->eq) -
832 (AUDPP_CMD_CFG_OBJECT_PARAMS_COMMON_LEN + 2))) {
833 rc = -EFAULT;
834 break;
835 }
836 audio->eq_enable = prev_state;
837 audio->eq_needs_commit = 1;
838 rc = 0;
839 break;
840 }
841
842 if (-EINVAL != rc)
843 return rc;
844
845 if (cmd == AUDIO_GET_EVENT) {
846 MM_DBG("AUDIO_GET_EVENT\n");
847 if (mutex_trylock(&audio->get_event_lock)) {
848 rc = audamrwb_process_event_req(audio,
849 (void __user *) arg);
850 mutex_unlock(&audio->get_event_lock);
851 } else
852 rc = -EBUSY;
853 return rc;
854 }
855
856 if (cmd == AUDIO_ABORT_GET_EVENT) {
857 audio->event_abort = 1;
858 wake_up(&audio->event_wait);
859 return 0;
860 }
861
862 mutex_lock(&audio->lock);
863 switch (cmd) {
864 case AUDIO_START:
865 MM_DBG("AUDIO_START\n");
866 rc = audamrwb_enable(audio);
867 if (!rc) {
868 rc = wait_event_interruptible_timeout(audio->wait,
869 audio->dec_state != MSM_AUD_DECODER_STATE_NONE,
870 msecs_to_jiffies(MSM_AUD_DECODER_WAIT_MS));
871 MM_INFO("dec_state %d rc = %d\n", audio->dec_state, rc);
872
873 if (audio->dec_state != MSM_AUD_DECODER_STATE_SUCCESS)
874 rc = -ENODEV;
875 else
876 rc = 0;
877 }
878 break;
879 case AUDIO_STOP:
880 MM_DBG("AUDIO_STOP\n");
881 rc = audamrwb_disable(audio);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700882 audamrwb_ioport_reset(audio);
883 audio->stopped = 0;
884 break;
885 case AUDIO_FLUSH:
886 MM_DBG(" AUDIO_FLUSH\n");
887 audio->rflush = 1;
888 audio->wflush = 1;
889 audamrwb_ioport_reset(audio);
890 if (audio->running) {
891 audpp_flush(audio->dec_id);
892 rc = wait_event_interruptible(audio->write_wait,
893 !audio->wflush);
894 if (rc < 0) {
895 MM_ERR("AUDIO_FLUSH interrupted\n");
896 rc = -EINTR;
897 }
898 } else {
899 audio->rflush = 0;
900 audio->wflush = 0;
901 }
902 break;
903 case AUDIO_SET_CONFIG:{
904 struct msm_audio_config config;
905 if (copy_from_user
906 (&config, (void *)arg, sizeof(config))) {
907 rc = -EFAULT;
908 break;
909 }
910 if (config.channel_count == 1)
911 config.channel_count =
912 AUDPP_CMD_PCM_INTF_MONO_V;
913 else if (config.channel_count == 2)
914 config.channel_count =
915 AUDPP_CMD_PCM_INTF_STEREO_V;
916 else
917 rc = -EINVAL;
918 audio->out_channel_mode = config.channel_count;
919 audio->out_sample_rate = config.sample_rate;
920 audio->mfield = config.meta_field;
921 rc = 0;
922 break;
923 }
924 case AUDIO_GET_CONFIG:{
925 struct msm_audio_config config;
926 config.buffer_size = BUFSZ;
927 config.buffer_count = 2;
928 config.sample_rate = audio->out_sample_rate;
929 if (audio->out_channel_mode ==
930 AUDPP_CMD_PCM_INTF_MONO_V)
931 config.channel_count = 1;
932 else
933 config.channel_count = 2;
934 config.meta_field = 0;
935 config.unused[0] = 0;
936 config.unused[1] = 0;
937 config.unused[2] = 0;
938 if (copy_to_user((void *)arg, &config,
939 sizeof(config)))
940 rc = -EFAULT;
941 else
942 rc = 0;
943
944 break;
945 }
946 case AUDIO_GET_PCM_CONFIG:{
947 struct msm_audio_pcm_config config;
948 config.pcm_feedback = 0;
949 config.buffer_count = PCM_BUF_MAX_COUNT;
950 config.buffer_size = PCM_BUFSZ_MIN;
951 if (copy_to_user((void *)arg, &config,
952 sizeof(config)))
953 rc = -EFAULT;
954 else
955 rc = 0;
956 break;
957 }
958 case AUDIO_SET_PCM_CONFIG:{
959 struct msm_audio_pcm_config config;
960 if (copy_from_user
961 (&config, (void *)arg, sizeof(config))) {
962 rc = -EFAULT;
963 break;
964 }
965 if ((config.buffer_count > PCM_BUF_MAX_COUNT) ||
966 (config.buffer_count == 1))
967 config.buffer_count = PCM_BUF_MAX_COUNT;
968
969 if (config.buffer_size < PCM_BUFSZ_MIN)
970 config.buffer_size = PCM_BUFSZ_MIN;
971
972 /* Check if pcm feedback is required */
973 if ((config.pcm_feedback) && (!audio->read_data)) {
974 MM_DBG("allocate PCM buf %d\n", config.buffer_count *
975 config.buffer_size);
Sidipotu Ashok172c98b2012-06-26 17:58:29 +0530976 handle = ion_alloc(audio->client,
977 (config.buffer_size *
978 config.buffer_count),
979 SZ_4K, ION_HEAP(ION_AUDIO_HEAP_ID));
980 if (IS_ERR_OR_NULL(handle)) {
981 MM_ERR("Unable to alloc I/P buffs\n");
982 audio->input_buff_handle = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700983 rc = -ENOMEM;
984 break;
Sidipotu Ashok172c98b2012-06-26 17:58:29 +0530985 }
986
987 audio->input_buff_handle = handle;
988
989 rc = ion_phys(audio->client ,
990 handle, &addr, &len);
991 if (rc) {
992 MM_ERR("Invalid phy: %x sz: %x\n",
993 (unsigned int) addr,
994 (unsigned int) len);
995 ion_free(audio->client, handle);
996 audio->input_buff_handle = NULL;
997 rc = -ENOMEM;
998 break;
999 } else {
1000 MM_INFO("Got valid phy: %x sz: %x\n",
1001 (unsigned int) audio->read_phys,
1002 (unsigned int) len);
1003 }
1004 audio->read_phys = (int32_t)addr;
1005
1006 rc = ion_handle_get_flags(audio->client,
1007 handle, &ionflag);
1008 if (rc) {
1009 MM_ERR("could not get flags\n");
1010 ion_free(audio->client, handle);
1011 audio->input_buff_handle = NULL;
1012 rc = -ENOMEM;
1013 break;
1014 }
1015 audio->map_v_read = ion_map_kernel(
1016 audio->client,
1017 handle, ionflag);
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,
1602 ION_HEAP(ION_AUDIO_HEAP_ID));
1603 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
1627 audio->map_v_write = ion_map_kernel(client, handle, ionflag);
1628 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");