blob: b566c60ddb1edc2d366b9fb95230059ddfae222b [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>
41#include <linux/android_pmem.h>
42#include <linux/slab.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070043#include <linux/msm_audio.h>
Santosh Mardi0be3b8e2011-07-06 10:00:21 +053044#include <linux/memory_alloc.h>
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>
49#include <mach/msm_subsystem_map.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070050#include <mach/qdsp5/qdsp5audppcmdi.h>
51#include <mach/qdsp5/qdsp5audppmsg.h>
52#include <mach/qdsp5/qdsp5audplaycmdi.h>
53#include <mach/qdsp5/qdsp5audplaymsg.h>
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 */
Santosh Mardi0be3b8e2011-07-06 10:00:21 +0530142 struct msm_mapped_buffer *map_v_read;
143 struct msm_mapped_buffer *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;
184};
185
186static int auddec_dsp_config(struct audio *audio, int enable);
187static void audpp_cmd_cfg_adec_params(struct audio *audio);
188static void audpp_cmd_cfg_routing_mode(struct audio *audio);
189static void audamrwb_send_data(struct audio *audio, unsigned needed);
190static void audamrwb_config_hostpcm(struct audio *audio);
191static void audamrwb_buffer_refresh(struct audio *audio);
192static void audamrwb_dsp_event(void *private, unsigned id, uint16_t *msg);
193#ifdef CONFIG_HAS_EARLYSUSPEND
194static void audamrwb_post_event(struct audio *audio, int type,
195 union msm_audio_event_payload payload);
196#endif
197
198static int rmt_put_resource(struct audio *audio)
199{
200 struct aud_codec_config_cmd cmd;
201 unsigned short client_idx;
202
203 cmd.cmd_id = RM_CMD_AUD_CODEC_CFG;
204 cmd.client_id = RM_AUD_CLIENT_ID;
205 cmd.task_id = audio->dec_id;
206 cmd.enable = RMT_DISABLE;
207 cmd.dec_type = AUDDEC_DEC_AMRWB;
208 client_idx = ((cmd.client_id << 8) | cmd.task_id);
209
210 return put_adsp_resource(client_idx, &cmd, sizeof(cmd));
211}
212
213static int rmt_get_resource(struct audio *audio)
214{
215 struct aud_codec_config_cmd cmd;
216 unsigned short client_idx;
217
218 cmd.cmd_id = RM_CMD_AUD_CODEC_CFG;
219 cmd.client_id = RM_AUD_CLIENT_ID;
220 cmd.task_id = audio->dec_id;
221 cmd.enable = RMT_ENABLE;
222 cmd.dec_type = AUDDEC_DEC_AMRWB;
223 client_idx = ((cmd.client_id << 8) | cmd.task_id);
224
225 return get_adsp_resource(client_idx, &cmd, sizeof(cmd));
226}
227
228/* must be called with audio->lock held */
229static int audamrwb_enable(struct audio *audio)
230{
231 struct audmgr_config cfg;
232 int rc;
233
234 MM_DBG("\n"); /* Macro prints the file name and function */
235
236 if (audio->enabled)
237 return 0;
238
239 if (audio->rmt_resource_released == 1) {
240 audio->rmt_resource_released = 0;
241 rc = rmt_get_resource(audio);
242 if (rc) {
243 MM_ERR("ADSP resources are not available for AMRWB \
244 session 0x%08x on decoder: %d\n Ignoring \
245 error and going ahead with the playback\n",
246 (int)audio, audio->dec_id);
247 }
248 }
249
250 audio->dec_state = MSM_AUD_DECODER_STATE_NONE;
251 audio->out_tail = 0;
252 audio->out_needed = 0;
253
254 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK) {
255 cfg.tx_rate = RPC_AUD_DEF_SAMPLE_RATE_NONE;
256 cfg.rx_rate = RPC_AUD_DEF_SAMPLE_RATE_48000;
257 cfg.def_method = RPC_AUD_DEF_METHOD_PLAYBACK;
258 cfg.codec = RPC_AUD_DEF_CODEC_AMR_WB;
259 cfg.snd_method = RPC_SND_METHOD_MIDI;
260
261 rc = audmgr_enable(&audio->audmgr, &cfg);
262 if (rc < 0)
263 return rc;
264 }
265
266 if (msm_adsp_enable(audio->audplay)) {
267 MM_ERR("msm_adsp_enable(audplay) failed\n");
268 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK)
269 audmgr_disable(&audio->audmgr);
270 return -ENODEV;
271 }
272
273 if (audpp_enable(audio->dec_id, audamrwb_dsp_event, audio)) {
274 MM_ERR("audpp_enable() failed\n");
275 msm_adsp_disable(audio->audplay);
276 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK)
277 audmgr_disable(&audio->audmgr);
278 return -ENODEV;
279 }
280 audio->enabled = 1;
281 return 0;
282}
283
284/* must be called with audio->lock held */
285static int audamrwb_disable(struct audio *audio)
286{
287 int rc = 0;
288 MM_DBG("\n"); /* Macro prints the file name and function */
289 if (audio->enabled) {
290 audio->enabled = 0;
291 audio->dec_state = MSM_AUD_DECODER_STATE_NONE;
292 auddec_dsp_config(audio, 0);
293 rc = wait_event_interruptible_timeout(audio->wait,
294 audio->dec_state != MSM_AUD_DECODER_STATE_NONE,
295 msecs_to_jiffies(MSM_AUD_DECODER_WAIT_MS));
296 if (rc == 0)
297 rc = -ETIMEDOUT;
298 else if (audio->dec_state != MSM_AUD_DECODER_STATE_CLOSE)
299 rc = -EFAULT;
300 else
301 rc = 0;
Manish Dewangan89a9f232012-02-09 17:14:40 +0530302 audio->stopped = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700303 wake_up(&audio->write_wait);
304 wake_up(&audio->read_wait);
305 msm_adsp_disable(audio->audplay);
306 audpp_disable(audio->dec_id, audio);
307 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK)
308 audmgr_disable(&audio->audmgr);
309 audio->out_needed = 0;
310 rmt_put_resource(audio);
311 audio->rmt_resource_released = 1;
312 }
313 return rc;
314}
315
316/* ------------------- dsp --------------------- */
317static void audamrwb_update_pcm_buf_entry(struct audio *audio,
318 uint32_t *payload)
319{
320 uint8_t index;
321 unsigned long flags;
322
323 if (audio->rflush)
324 return;
325
326 spin_lock_irqsave(&audio->dsp_lock, flags);
327 for (index = 0; index < payload[1]; index++) {
328 if (audio->in[audio->fill_next].addr ==
329 payload[2 + index * 2]) {
330 MM_DBG("in[%d] ready\n", audio->fill_next);
331 audio->in[audio->fill_next].used =
332 payload[3 + index * 2];
333 if ((++audio->fill_next) == audio->pcm_buf_count)
334 audio->fill_next = 0;
335
336 } else {
337 MM_ERR("expected=%x ret=%x\n",
338 audio->in[audio->fill_next].addr,
339 payload[1 + index * 2]);
340 break;
341 }
342 }
343 if (audio->in[audio->fill_next].used == 0) {
344 audamrwb_buffer_refresh(audio);
345 } else {
346 MM_DBG("read cannot keep up\n");
347 audio->buf_refresh = 1;
348 }
349 wake_up(&audio->read_wait);
350 spin_unlock_irqrestore(&audio->dsp_lock, flags);
351}
352
353static void audplay_dsp_event(void *data, unsigned id, size_t len,
354 void (*getevent) (void *ptr, size_t len))
355{
356 struct audio *audio = data;
357 uint32_t msg[28];
358 getevent(msg, sizeof(msg));
359
360 MM_DBG("msg_id=%x\n", id);
361
362 switch (id) {
363 case AUDPLAY_MSG_DEC_NEEDS_DATA:
364 audamrwb_send_data(audio, 1);
365 break;
366
367 case AUDPLAY_MSG_BUFFER_UPDATE:
368 audamrwb_update_pcm_buf_entry(audio, msg);
369 break;
370
371 default:
372 MM_ERR("unexpected message from decoder\n");
373 }
374}
375
376static void audamrwb_dsp_event(void *private, unsigned id, uint16_t *msg)
377{
378 struct audio *audio = private;
379
380 switch (id) {
381 case AUDPP_MSG_STATUS_MSG:{
382 unsigned status = msg[1];
383
384 switch (status) {
385 case AUDPP_DEC_STATUS_SLEEP: {
386 uint16_t reason = msg[2];
387 MM_DBG("decoder status:sleep reason=0x%04x\n",
388 reason);
389 if ((reason == AUDPP_MSG_REASON_MEM)
390 || (reason ==
391 AUDPP_MSG_REASON_NODECODER)) {
392 audio->dec_state =
393 MSM_AUD_DECODER_STATE_FAILURE;
394 wake_up(&audio->wait);
395 } else if (reason == AUDPP_MSG_REASON_NONE) {
396 /* decoder is in disable state */
397 audio->dec_state =
398 MSM_AUD_DECODER_STATE_CLOSE;
399 wake_up(&audio->wait);
400 }
401 break;
402 }
403 case AUDPP_DEC_STATUS_INIT:
404 MM_DBG("decoder status: init\n");
405 if (audio->pcm_feedback)
406 audpp_cmd_cfg_routing_mode(audio);
407 else
408 audpp_cmd_cfg_adec_params(audio);
409 break;
410
411 case AUDPP_DEC_STATUS_CFG:
412 MM_DBG("decoder status: cfg\n");
413 break;
414 case AUDPP_DEC_STATUS_PLAY:
415 MM_DBG("decoder status: play\n");
416 if (audio->pcm_feedback) {
417 audamrwb_config_hostpcm(audio);
418 audamrwb_buffer_refresh(audio);
419 }
420 audio->dec_state =
421 MSM_AUD_DECODER_STATE_SUCCESS;
422 wake_up(&audio->wait);
423 break;
424 default:
425 MM_DBG("unknown decoder status\n");
426 break;
427 }
428 break;
429 }
430 case AUDPP_MSG_CFG_MSG:
431 if (msg[0] == AUDPP_MSG_ENA_ENA) {
432 MM_DBG("CFG_MSG ENABLE\n");
433 auddec_dsp_config(audio, 1);
434 audio->out_needed = 0;
435 audio->running = 1;
436 audpp_dsp_set_vol_pan(audio->dec_id, &audio->vol_pan);
437 audpp_dsp_set_eq(audio->dec_id, audio->eq_enable,
438 &audio->eq);
439 audpp_avsync(audio->dec_id, 22050);
440 } else if (msg[0] == AUDPP_MSG_ENA_DIS) {
441 MM_DBG("CFG_MSG DISABLE\n");
442 audpp_avsync(audio->dec_id, 0);
443 audio->running = 0;
444 } else {
445 MM_DBG("CFG_MSG %d?\n", msg[0]);
446 }
447 break;
448 case AUDPP_MSG_ROUTING_ACK:
449 MM_DBG("ROUTING_ACK mode=%d\n", msg[1]);
450 audpp_cmd_cfg_adec_params(audio);
451 break;
452 case AUDPP_MSG_FLUSH_ACK:
453 MM_DBG("FLUSH_ACK\n");
454 audio->wflush = 0;
455 audio->rflush = 0;
456 wake_up(&audio->write_wait);
457 if (audio->pcm_feedback)
458 audamrwb_buffer_refresh(audio);
459 break;
460 case AUDPP_MSG_PCMDMAMISSED:
461 MM_DBG("PCMDMAMISSED\n");
462 audio->teos = 1;
463 wake_up(&audio->write_wait);
464 break;
465 default:
466 MM_DBG("UNKNOWN (%d)\n", id);
467 }
468
469}
470
471struct msm_adsp_ops audplay_adsp_ops_amrwb = {
472 .event = audplay_dsp_event,
473};
474
475#define audplay_send_queue0(audio, cmd, len) \
476 msm_adsp_write(audio->audplay, audio->queue_id, \
477 cmd, len)
478
479static int auddec_dsp_config(struct audio *audio, int enable)
480{
481 u16 cfg_dec_cmd[AUDPP_CMD_CFG_DEC_TYPE_LEN / sizeof(unsigned short)];
482
483 memset(cfg_dec_cmd, 0, sizeof(cfg_dec_cmd));
484 cfg_dec_cmd[0] = AUDPP_CMD_CFG_DEC_TYPE;
485 if (enable)
486 cfg_dec_cmd[1 + audio->dec_id] = AUDPP_CMD_UPDATDE_CFG_DEC |
487 AUDPP_CMD_ENA_DEC_V | AUDDEC_DEC_AMRWB;
488 else
489 cfg_dec_cmd[1 + audio->dec_id] = AUDPP_CMD_UPDATDE_CFG_DEC |
490 AUDPP_CMD_DIS_DEC_V;
491
492 return audpp_send_queue1(&cfg_dec_cmd, sizeof(cfg_dec_cmd));
493}
494
495static void audpp_cmd_cfg_adec_params(struct audio *audio)
496{
497 struct audpp_cmd_cfg_adec_params_amrwb cmd;
498
499 memset(&cmd, 0, sizeof(cmd));
500 cmd.common.cmd_id = AUDPP_CMD_CFG_ADEC_PARAMS;
501 cmd.common.length = AUDPP_CMD_CFG_ADEC_PARAMS_AMRWB_LEN;
502 cmd.common.dec_id = audio->dec_id;
503 cmd.common.input_sampling_frequency = audio->out_sample_rate;
504 cmd.stereo_cfg = audio->out_channel_mode;
505 audpp_send_queue2(&cmd, sizeof(cmd));
506}
507
508static void audpp_cmd_cfg_routing_mode(struct audio *audio)
509{
510 struct audpp_cmd_routing_mode cmd;
511 MM_DBG("\n"); /* Macro prints the file name and function */
512 memset(&cmd, 0, sizeof(cmd));
513 cmd.cmd_id = AUDPP_CMD_ROUTING_MODE;
514 cmd.object_number = audio->dec_id;
515 if (audio->pcm_feedback)
516 cmd.routing_mode = ROUTING_MODE_FTRT;
517 else
518 cmd.routing_mode = ROUTING_MODE_RT;
519
520 audpp_send_queue1(&cmd, sizeof(cmd));
521}
522
523static int audplay_dsp_send_data_avail(struct audio *audio,
524 unsigned idx, unsigned len)
525{
526 struct audplay_cmd_bitstream_data_avail_nt2 cmd;
527
528 cmd.cmd_id = AUDPLAY_CMD_BITSTREAM_DATA_AVAIL_NT2;
529 if (audio->mfield)
530 cmd.decoder_id = AUDAMRWB_METAFIELD_MASK |
531 (audio->out[idx].mfield_sz >> 1);
532 else
533 cmd.decoder_id = audio->dec_id;
534 cmd.buf_ptr = audio->out[idx].addr;
535 cmd.buf_size = len / 2;
536 cmd.partition_number = 0;
537 return audplay_send_queue0(audio, &cmd, sizeof(cmd));
538}
539
540static void audamrwb_buffer_refresh(struct audio *audio)
541{
542 struct audplay_cmd_buffer_refresh refresh_cmd;
543
544 refresh_cmd.cmd_id = AUDPLAY_CMD_BUFFER_REFRESH;
545 refresh_cmd.num_buffers = 1;
546 refresh_cmd.buf0_address = audio->in[audio->fill_next].addr;
547 refresh_cmd.buf0_length = audio->in[audio->fill_next].size;
548 refresh_cmd.buf_read_count = 0;
549 MM_DBG("buf0_addr=%x buf0_len=%d\n", refresh_cmd.buf0_address,
550 refresh_cmd.buf0_length);
551 (void)audplay_send_queue0(audio, &refresh_cmd, sizeof(refresh_cmd));
552}
553
554static void audamrwb_config_hostpcm(struct audio *audio)
555{
556 struct audplay_cmd_hpcm_buf_cfg cfg_cmd;
557
558 MM_DBG("\n"); /* Macro prints the file name and function */
559 cfg_cmd.cmd_id = AUDPLAY_CMD_HPCM_BUF_CFG;
560 cfg_cmd.max_buffers = audio->pcm_buf_count;
561 cfg_cmd.byte_swap = 0;
562 cfg_cmd.hostpcm_config = (0x8000) | (0x4000);
563 cfg_cmd.feedback_frequency = 1;
564 cfg_cmd.partition_number = 0;
565 (void)audplay_send_queue0(audio, &cfg_cmd, sizeof(cfg_cmd));
566
567}
568
569static void audamrwb_send_data(struct audio *audio, unsigned needed)
570{
571 struct buffer *frame;
572 unsigned long flags;
573
574 spin_lock_irqsave(&audio->dsp_lock, flags);
575 if (!audio->running)
576 goto done;
577
578 if (needed && !audio->wflush) {
579 /* We were called from the callback because the DSP
580 * requested more data. Note that the DSP does want
581 * more data, and if a buffer was in-flight, mark it
582 * as available (since the DSP must now be done with
583 * it).
584 */
585 audio->out_needed = 1;
586 frame = audio->out + audio->out_tail;
587 if (frame->used == 0xffffffff) {
588 frame->used = 0;
589 audio->out_tail ^= 1;
590 wake_up(&audio->write_wait);
591 }
592 }
593
594 if (audio->out_needed) {
595 /* If the DSP currently wants data and we have a
596 * buffer available, we will send it and reset
597 * the needed flag. We'll mark the buffer as in-flight
598 * so that it won't be recycled until the next buffer
599 * is requested
600 */
601
602 frame = audio->out + audio->out_tail;
603 if (frame->used) {
604 BUG_ON(frame->used == 0xffffffff);
605 MM_DBG("frame %d busy\n", audio->out_tail);
606 audplay_dsp_send_data_avail(audio, audio->out_tail,
607 frame->used);
608 frame->used = 0xffffffff;
609 audio->out_needed = 0;
610 }
611 }
612 done:
613 spin_unlock_irqrestore(&audio->dsp_lock, flags);
614}
615
616/* ------------------- device --------------------- */
617
618static void audamrwb_flush(struct audio *audio)
619{
Manish Dewangana4f1df02012-02-08 17:06:54 +0530620 unsigned long flags;
621
622 spin_lock_irqsave(&audio->dsp_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700623 audio->out[0].used = 0;
624 audio->out[1].used = 0;
625 audio->out_head = 0;
626 audio->out_tail = 0;
627 audio->reserved = 0;
628 audio->out_needed = 0;
Manish Dewangana4f1df02012-02-08 17:06:54 +0530629 spin_unlock_irqrestore(&audio->dsp_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700630 atomic_set(&audio->out_bytes, 0);
631}
632
633static void audamrwb_flush_pcm_buf(struct audio *audio)
634{
635 uint8_t index;
Manish Dewangana4f1df02012-02-08 17:06:54 +0530636 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700637
Manish Dewangana4f1df02012-02-08 17:06:54 +0530638 spin_lock_irqsave(&audio->dsp_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700639 for (index = 0; index < PCM_BUF_MAX_COUNT; index++)
640 audio->in[index].used = 0;
641
642 audio->buf_refresh = 0;
643 audio->read_next = 0;
644 audio->fill_next = 0;
Manish Dewangana4f1df02012-02-08 17:06:54 +0530645 spin_unlock_irqrestore(&audio->dsp_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700646}
647
648static void audamrwb_ioport_reset(struct audio *audio)
649{
650 /* Make sure read/write thread are free from
651 * sleep and knowing that system is not able
652 * to process io request at the moment
653 */
654 wake_up(&audio->write_wait);
655 mutex_lock(&audio->write_lock);
656 audamrwb_flush(audio);
657 mutex_unlock(&audio->write_lock);
658 wake_up(&audio->read_wait);
659 mutex_lock(&audio->read_lock);
660 audamrwb_flush_pcm_buf(audio);
661 mutex_unlock(&audio->read_lock);
662}
663
664static int audamrwb_events_pending(struct audio *audio)
665{
666 unsigned long flags;
667 int empty;
668
669 spin_lock_irqsave(&audio->event_queue_lock, flags);
670 empty = !list_empty(&audio->event_queue);
671 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
672 return empty || audio->event_abort;
673}
674
675static void audamrwb_reset_event_queue(struct audio *audio)
676{
677 unsigned long flags;
678 struct audamrwb_event *drv_evt;
679 struct list_head *ptr, *next;
680
681 spin_lock_irqsave(&audio->event_queue_lock, flags);
682 list_for_each_safe(ptr, next, &audio->event_queue) {
683 drv_evt = list_first_entry(&audio->event_queue,
684 struct audamrwb_event, list);
685 list_del(&drv_evt->list);
686 kfree(drv_evt);
687 }
688 list_for_each_safe(ptr, next, &audio->free_event_queue) {
689 drv_evt = list_first_entry(&audio->free_event_queue,
690 struct audamrwb_event, list);
691 list_del(&drv_evt->list);
692 kfree(drv_evt);
693 }
694 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
695
696 return;
697}
698
699static long audamrwb_process_event_req(struct audio *audio, void __user *arg)
700{
701 long rc;
702 struct msm_audio_event usr_evt;
703 struct audamrwb_event *drv_evt = NULL;
704 int timeout;
705 unsigned long flags;
706
707 if (copy_from_user(&usr_evt, arg, sizeof(struct msm_audio_event)))
708 return -EFAULT;
709
710 timeout = (int) usr_evt.timeout_ms;
711
712 if (timeout > 0) {
713 rc = wait_event_interruptible_timeout(
714 audio->event_wait, audamrwb_events_pending(audio),
715 msecs_to_jiffies(timeout));
716 if (rc == 0)
717 return -ETIMEDOUT;
718 } else {
719 rc = wait_event_interruptible(
720 audio->event_wait, audamrwb_events_pending(audio));
721 }
722
723 if (rc < 0)
724 return rc;
725
726 if (audio->event_abort) {
727 audio->event_abort = 0;
728 return -ENODEV;
729 }
730
731 rc = 0;
732
733 spin_lock_irqsave(&audio->event_queue_lock, flags);
734 if (!list_empty(&audio->event_queue)) {
735 drv_evt = list_first_entry(&audio->event_queue,
736 struct audamrwb_event, list);
737 list_del(&drv_evt->list);
738 }
739
740 if (drv_evt) {
741 usr_evt.event_type = drv_evt->event_type;
742 usr_evt.event_payload = drv_evt->payload;
743 list_add_tail(&drv_evt->list, &audio->free_event_queue);
744 } else
745 rc = -1;
746 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
747
748 if (!rc && copy_to_user(arg, &usr_evt, sizeof(usr_evt)))
749 rc = -EFAULT;
750
751 return rc;
752}
753
754static int audio_enable_eq(struct audio *audio, int enable)
755{
756 if (audio->eq_enable == enable && !audio->eq_needs_commit)
757 return 0;
758
759 audio->eq_enable = enable;
760
761 if (audio->running) {
762 audpp_dsp_set_eq(audio->dec_id, enable, &audio->eq);
763 audio->eq_needs_commit = 0;
764 }
765 return 0;
766}
767
768static long audamrwb_ioctl(struct file *file, unsigned int cmd,
769 unsigned long arg)
770{
771 struct audio *audio = file->private_data;
772 int rc = -EINVAL;
773 unsigned long flags = 0;
774 uint16_t enable_mask;
775 int enable;
776 int prev_state;
777
778 MM_DBG("cmd = %d\n", cmd);
779
780 if (cmd == AUDIO_GET_STATS) {
781 struct msm_audio_stats stats;
782 stats.byte_count = audpp_avsync_byte_count(audio->dec_id);
783 stats.sample_count = audpp_avsync_sample_count(audio->dec_id);
784 if (copy_to_user((void *)arg, &stats, sizeof(stats)))
785 return -EFAULT;
786 return 0;
787 }
788
789 switch (cmd) {
790 case AUDIO_ENABLE_AUDPP:
791 if (copy_from_user(&enable_mask, (void *) arg,
792 sizeof(enable_mask))) {
793 rc = -EFAULT;
794 break;
795 }
796
797 spin_lock_irqsave(&audio->dsp_lock, flags);
798 enable = (enable_mask & EQ_ENABLE) ? 1 : 0;
799 audio_enable_eq(audio, enable);
800 spin_unlock_irqrestore(&audio->dsp_lock, flags);
801 rc = 0;
802 break;
803 case AUDIO_SET_VOLUME:
804 spin_lock_irqsave(&audio->dsp_lock, flags);
805 audio->vol_pan.volume = arg;
806 if (audio->running)
807 audpp_dsp_set_vol_pan(audio->dec_id, &audio->vol_pan);
808 spin_unlock_irqrestore(&audio->dsp_lock, flags);
809 rc = 0;
810 break;
811
812 case AUDIO_SET_PAN:
813 spin_lock_irqsave(&audio->dsp_lock, flags);
814 audio->vol_pan.pan = arg;
815 if (audio->running)
816 audpp_dsp_set_vol_pan(audio->dec_id, &audio->vol_pan);
817 spin_unlock_irqrestore(&audio->dsp_lock, flags);
818 rc = 0;
819 break;
820
821 case AUDIO_SET_EQ:
822 prev_state = audio->eq_enable;
823 audio->eq_enable = 0;
824 if (copy_from_user(&audio->eq.num_bands, (void *) arg,
825 sizeof(audio->eq) -
826 (AUDPP_CMD_CFG_OBJECT_PARAMS_COMMON_LEN + 2))) {
827 rc = -EFAULT;
828 break;
829 }
830 audio->eq_enable = prev_state;
831 audio->eq_needs_commit = 1;
832 rc = 0;
833 break;
834 }
835
836 if (-EINVAL != rc)
837 return rc;
838
839 if (cmd == AUDIO_GET_EVENT) {
840 MM_DBG("AUDIO_GET_EVENT\n");
841 if (mutex_trylock(&audio->get_event_lock)) {
842 rc = audamrwb_process_event_req(audio,
843 (void __user *) arg);
844 mutex_unlock(&audio->get_event_lock);
845 } else
846 rc = -EBUSY;
847 return rc;
848 }
849
850 if (cmd == AUDIO_ABORT_GET_EVENT) {
851 audio->event_abort = 1;
852 wake_up(&audio->event_wait);
853 return 0;
854 }
855
856 mutex_lock(&audio->lock);
857 switch (cmd) {
858 case AUDIO_START:
859 MM_DBG("AUDIO_START\n");
860 rc = audamrwb_enable(audio);
861 if (!rc) {
862 rc = wait_event_interruptible_timeout(audio->wait,
863 audio->dec_state != MSM_AUD_DECODER_STATE_NONE,
864 msecs_to_jiffies(MSM_AUD_DECODER_WAIT_MS));
865 MM_INFO("dec_state %d rc = %d\n", audio->dec_state, rc);
866
867 if (audio->dec_state != MSM_AUD_DECODER_STATE_SUCCESS)
868 rc = -ENODEV;
869 else
870 rc = 0;
871 }
872 break;
873 case AUDIO_STOP:
874 MM_DBG("AUDIO_STOP\n");
875 rc = audamrwb_disable(audio);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700876 audamrwb_ioport_reset(audio);
877 audio->stopped = 0;
878 break;
879 case AUDIO_FLUSH:
880 MM_DBG(" AUDIO_FLUSH\n");
881 audio->rflush = 1;
882 audio->wflush = 1;
883 audamrwb_ioport_reset(audio);
884 if (audio->running) {
885 audpp_flush(audio->dec_id);
886 rc = wait_event_interruptible(audio->write_wait,
887 !audio->wflush);
888 if (rc < 0) {
889 MM_ERR("AUDIO_FLUSH interrupted\n");
890 rc = -EINTR;
891 }
892 } else {
893 audio->rflush = 0;
894 audio->wflush = 0;
895 }
896 break;
897 case AUDIO_SET_CONFIG:{
898 struct msm_audio_config config;
899 if (copy_from_user
900 (&config, (void *)arg, sizeof(config))) {
901 rc = -EFAULT;
902 break;
903 }
904 if (config.channel_count == 1)
905 config.channel_count =
906 AUDPP_CMD_PCM_INTF_MONO_V;
907 else if (config.channel_count == 2)
908 config.channel_count =
909 AUDPP_CMD_PCM_INTF_STEREO_V;
910 else
911 rc = -EINVAL;
912 audio->out_channel_mode = config.channel_count;
913 audio->out_sample_rate = config.sample_rate;
914 audio->mfield = config.meta_field;
915 rc = 0;
916 break;
917 }
918 case AUDIO_GET_CONFIG:{
919 struct msm_audio_config config;
920 config.buffer_size = BUFSZ;
921 config.buffer_count = 2;
922 config.sample_rate = audio->out_sample_rate;
923 if (audio->out_channel_mode ==
924 AUDPP_CMD_PCM_INTF_MONO_V)
925 config.channel_count = 1;
926 else
927 config.channel_count = 2;
928 config.meta_field = 0;
929 config.unused[0] = 0;
930 config.unused[1] = 0;
931 config.unused[2] = 0;
932 if (copy_to_user((void *)arg, &config,
933 sizeof(config)))
934 rc = -EFAULT;
935 else
936 rc = 0;
937
938 break;
939 }
940 case AUDIO_GET_PCM_CONFIG:{
941 struct msm_audio_pcm_config config;
942 config.pcm_feedback = 0;
943 config.buffer_count = PCM_BUF_MAX_COUNT;
944 config.buffer_size = PCM_BUFSZ_MIN;
945 if (copy_to_user((void *)arg, &config,
946 sizeof(config)))
947 rc = -EFAULT;
948 else
949 rc = 0;
950 break;
951 }
952 case AUDIO_SET_PCM_CONFIG:{
953 struct msm_audio_pcm_config config;
954 if (copy_from_user
955 (&config, (void *)arg, sizeof(config))) {
956 rc = -EFAULT;
957 break;
958 }
959 if ((config.buffer_count > PCM_BUF_MAX_COUNT) ||
960 (config.buffer_count == 1))
961 config.buffer_count = PCM_BUF_MAX_COUNT;
962
963 if (config.buffer_size < PCM_BUFSZ_MIN)
964 config.buffer_size = PCM_BUFSZ_MIN;
965
966 /* Check if pcm feedback is required */
967 if ((config.pcm_feedback) && (!audio->read_data)) {
968 MM_DBG("allocate PCM buf %d\n", config.buffer_count *
969 config.buffer_size);
Santosh Mardi0be3b8e2011-07-06 10:00:21 +0530970 audio->read_phys = allocate_contiguous_ebi_nomap(
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700971 config.buffer_size *
972 config.buffer_count,
Santosh Mardi0be3b8e2011-07-06 10:00:21 +0530973 SZ_4K);
974 if (!audio->read_phys) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700975 rc = -ENOMEM;
976 break;
977 }
Santosh Mardi0be3b8e2011-07-06 10:00:21 +0530978 audio->map_v_read = msm_subsystem_map_buffer(
979 audio->read_phys,
980 config.buffer_size *
981 config.buffer_count,
982 MSM_SUBSYSTEM_MAP_KADDR,
983 NULL, 0);
984 if (IS_ERR(audio->map_v_read)) {
985 MM_ERR("failed to map mem for read buf\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700986 rc = -ENOMEM;
Santosh Mardi0be3b8e2011-07-06 10:00:21 +0530987 free_contiguous_memory_by_paddr(
988 audio->read_phys);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700989 } else {
990 uint8_t index;
991 uint32_t offset = 0;
Santosh Mardi0be3b8e2011-07-06 10:00:21 +0530992 audio->read_data = audio->map_v_read->vaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700993 audio->pcm_feedback = 1;
994 audio->buf_refresh = 0;
995 audio->pcm_buf_count =
996 config.buffer_count;
997 audio->read_next = 0;
998 audio->fill_next = 0;
999
1000 for (index = 0;
1001 index < config.buffer_count; index++) {
1002 audio->in[index].data =
1003 audio->read_data + offset;
1004 audio->in[index].addr =
1005 audio->read_phys + offset;
1006 audio->in[index].size =
1007 config.buffer_size;
1008 audio->in[index].used = 0;
1009 offset += config.buffer_size;
1010 }
1011 MM_DBG("read buf: phy addr 0x%08x \
1012 kernel addr 0x%08x\n",
1013 audio->read_phys,
1014 (int)audio->read_data);
1015 rc = 0;
1016 }
1017 } else {
1018 rc = 0;
1019 }
1020 break;
1021 }
1022 default:
1023 rc = -EINVAL;
1024 }
1025 mutex_unlock(&audio->lock);
1026 return rc;
1027}
1028
1029/* Only useful in tunnel-mode */
1030static int audamrwb_fsync(struct file *file, int datasync)
1031{
1032 struct audio *audio = file->private_data;
1033 struct buffer *frame;
1034 int rc = 0;
1035
1036 MM_DBG("\n"); /* Macro prints the file name and function */
1037
1038 if (!audio->running || audio->pcm_feedback) {
1039 rc = -EINVAL;
1040 goto done_nolock;
1041 }
1042
1043 mutex_lock(&audio->write_lock);
1044
1045 rc = wait_event_interruptible(audio->write_wait,
1046 (!audio->out[0].used &&
1047 !audio->out[1].used &&
1048 audio->out_needed) || audio->wflush);
1049
1050 if (rc < 0)
1051 goto done;
1052 else if (audio->wflush) {
1053 rc = -EBUSY;
1054 goto done;
1055 }
1056
1057 if (audio->reserved) {
1058 MM_DBG("send reserved byte\n");
1059 frame = audio->out + audio->out_tail;
1060 ((char *) frame->data)[0] = audio->rsv_byte;
1061 ((char *) frame->data)[1] = 0;
1062 frame->used = 2;
1063 audamrwb_send_data(audio, 0);
1064
1065 rc = wait_event_interruptible(audio->write_wait,
1066 (!audio->out[0].used &&
1067 !audio->out[1].used &&
1068 audio->out_needed) || audio->wflush);
1069
1070 if (rc < 0)
1071 goto done;
1072 else if (audio->wflush) {
1073 rc = -EBUSY;
1074 goto done;
1075 }
1076 }
1077
1078 /* pcm dmamiss message is sent continously
1079 * when decoder is starved so no race
1080 * condition concern
1081 */
1082 audio->teos = 0;
1083
1084 rc = wait_event_interruptible(audio->write_wait,
1085 audio->teos || audio->wflush);
1086
1087 if (audio->wflush)
1088 rc = -EBUSY;
1089
1090done:
1091 mutex_unlock(&audio->write_lock);
1092done_nolock:
1093 return rc;
1094}
1095
1096static ssize_t audamrwb_read(struct file *file, char __user *buf, size_t count,
1097 loff_t *pos)
1098{
1099 struct audio *audio = file->private_data;
1100 const char __user *start = buf;
1101 int rc = 0;
1102
1103 if (!audio->pcm_feedback)
1104 return 0; /* PCM feedback is not enabled. Nothing to read */
1105
1106 mutex_lock(&audio->read_lock);
1107 MM_DBG("count %d\n", count);
1108 while (count > 0) {
1109 rc = wait_event_interruptible(audio->read_wait,
1110 (audio->in[audio->read_next].used > 0) ||
1111 (audio->stopped) || (audio->rflush));
1112
1113 if (rc < 0)
1114 break;
1115
1116 if (audio->stopped || audio->rflush) {
1117 rc = -EBUSY;
1118 break;
1119 }
1120
1121 if (count < audio->in[audio->read_next].used) {
1122 /* Read must happen in frame boundary. Since driver does
1123 * not know frame size, read count must be greater or
1124 * equal to size of PCM samples
1125 */
1126 MM_DBG("read stop - partial frame\n");
1127 break;
1128 } else {
1129 MM_DBG("read from in[%d]\n",
1130 audio->read_next);
1131
1132 if (copy_to_user
1133 (buf, audio->in[audio->read_next].data,
1134 audio->in[audio->read_next].used)) {
1135 MM_ERR("invalid addr %x \n", (unsigned int)buf);
1136 rc = -EFAULT;
1137 break;
1138 }
1139 count -= audio->in[audio->read_next].used;
1140 buf += audio->in[audio->read_next].used;
1141 audio->in[audio->read_next].used = 0;
1142 if ((++audio->read_next) == audio->pcm_buf_count)
1143 audio->read_next = 0;
1144 break;
1145 }
1146 }
1147
1148 /* don't feed output buffer to HW decoder during flushing
1149 * buffer refresh command will be sent once flush completes
1150 * send buf refresh command here can confuse HW decoder
1151 */
1152 if (audio->buf_refresh && !audio->rflush) {
1153 audio->buf_refresh = 0;
1154 MM_ERR("kick start pcm feedback again\n");
1155 audamrwb_buffer_refresh(audio);
1156 }
1157
1158 mutex_unlock(&audio->read_lock);
1159
1160 if (buf > start)
1161 rc = buf - start;
1162
1163 MM_DBG("read %d bytes\n", rc);
1164 return rc;
1165}
1166
1167static int audamrwb_process_eos(struct audio *audio,
1168 const char __user *buf_start, unsigned short mfield_size)
1169{
1170 struct buffer *frame;
1171 char *buf_ptr;
1172 int rc = 0;
1173
1174 MM_DBG("signal input EOS reserved=%d\n", audio->reserved);
1175 if (audio->reserved) {
1176 MM_DBG("Pass reserve byte\n");
1177 frame = audio->out + audio->out_head;
1178 buf_ptr = frame->data;
1179 rc = wait_event_interruptible(audio->write_wait,
1180 (frame->used == 0)
1181 || (audio->stopped)
1182 || (audio->wflush));
1183 if (rc < 0)
1184 goto done;
1185 if (audio->stopped || audio->wflush) {
1186 rc = -EBUSY;
1187 goto done;
1188 }
1189 buf_ptr[0] = audio->rsv_byte;
1190 buf_ptr[1] = 0;
1191 audio->out_head ^= 1;
1192 frame->mfield_sz = 0;
1193 audio->reserved = 0;
1194 frame->used = 2;
1195 audamrwb_send_data(audio, 0);
1196 }
1197
1198 MM_DBG("Now signal input EOS after reserved bytes %d %d %d\n",
1199 audio->out[0].used, audio->out[1].used, audio->out_needed);
1200
1201 frame = audio->out + audio->out_head;
1202
1203 rc = wait_event_interruptible(audio->write_wait,
1204 (audio->out_needed &&
1205 audio->out[0].used == 0 &&
1206 audio->out[1].used == 0)
1207 || (audio->stopped)
1208 || (audio->wflush));
1209
1210 if (rc < 0)
1211 goto done;
1212 if (audio->stopped || audio->wflush) {
1213 rc = -EBUSY;
1214 goto done;
1215 }
1216
1217 if (copy_from_user(frame->data, buf_start, mfield_size)) {
1218 rc = -EFAULT;
1219 goto done;
1220 }
1221
1222 frame->mfield_sz = mfield_size;
1223 audio->out_head ^= 1;
1224 frame->used = mfield_size;
1225 audamrwb_send_data(audio, 0);
1226
1227done:
1228 return rc;
1229}
1230
1231static ssize_t audamrwb_write(struct file *file, const char __user *buf,
1232 size_t count, loff_t *pos)
1233{
1234 struct audio *audio = file->private_data;
1235 const char __user *start = buf;
1236 struct buffer *frame;
1237 size_t xfer;
1238 char *cpy_ptr;
1239 int rc = 0, eos_condition = AUDAMRWB_EOS_NONE;
1240 unsigned short mfield_size = 0;
1241 unsigned dsize;
1242
1243 MM_DBG("cnt=%d\n", count);
1244
1245 mutex_lock(&audio->write_lock);
1246 while (count > 0) {
1247 frame = audio->out + audio->out_head;
1248 cpy_ptr = frame->data;
1249 dsize = 0;
1250 rc = wait_event_interruptible(audio->write_wait,
1251 (frame->used == 0)
1252 || (audio->stopped)
1253 || (audio->wflush));
1254
1255 MM_DBG("buffer available\n");
1256 if (rc < 0)
1257 break;
1258 if (audio->stopped || audio->wflush) {
1259 rc = -EBUSY;
1260 break;
1261 }
1262
1263 if (audio->mfield) {
1264 if (buf == start) {
1265 /* Processing beginning of user buffer */
1266 if (__get_user(mfield_size,
1267 (unsigned short __user *) buf)) {
1268 rc = -EFAULT;
1269 break;
1270 } else if (mfield_size > count) {
1271 rc = -EINVAL;
1272 break;
1273 }
1274 MM_DBG("mf offset_val %x\n", mfield_size);
1275 if (copy_from_user(cpy_ptr, buf, mfield_size)) {
1276 rc = -EFAULT;
1277 break;
1278 }
1279 /* Check if EOS flag is set and buffer
1280 * contains just meta field
1281 */
1282 if (cpy_ptr[AUDAMRWB_EOS_FLG_OFFSET] &
1283 AUDAMRWB_EOS_FLG_MASK) {
1284 MM_DBG("eos set\n");
1285 eos_condition = AUDAMRWB_EOS_SET;
1286 if (mfield_size == count) {
1287 buf += mfield_size;
1288 break;
1289 } else
1290 cpy_ptr[AUDAMRWB_EOS_FLG_OFFSET] &=
1291 ~AUDAMRWB_EOS_FLG_MASK;
1292 }
1293 cpy_ptr += mfield_size;
1294 count -= mfield_size;
1295 dsize += mfield_size;
1296 buf += mfield_size;
1297 } else {
1298 mfield_size = 0;
1299 MM_DBG("continuous buffer\n");
1300 }
1301 frame->mfield_sz = mfield_size;
1302 }
1303
1304 if (audio->reserved) {
1305 MM_DBG("append reserved byte %x\n", audio->rsv_byte);
1306 *cpy_ptr = audio->rsv_byte;
1307 xfer = (count > ((frame->size - mfield_size) - 1)) ?
1308 ((frame->size - mfield_size) - 1) : count;
1309 cpy_ptr++;
1310 dsize += 1;
1311 audio->reserved = 0;
1312 } else
1313 xfer = (count > (frame->size - mfield_size)) ?
1314 (frame->size - mfield_size) : count;
1315
1316 if (copy_from_user(cpy_ptr, buf, xfer)) {
1317 rc = -EFAULT;
1318 break;
1319 }
1320
1321 dsize += xfer;
1322 if (dsize & 1) {
1323 audio->rsv_byte = ((char *) frame->data)[dsize - 1];
1324 MM_DBG("odd length buf reserve last byte %x\n",
1325 audio->rsv_byte);
1326 audio->reserved = 1;
1327 dsize--;
1328 }
1329 count -= xfer;
1330 buf += xfer;
1331
1332 if (dsize > 0) {
1333 audio->out_head ^= 1;
1334 frame->used = dsize;
1335 audamrwb_send_data(audio, 0);
1336 }
1337 }
1338 MM_DBG("eos_condition %x buf[0x%x] start[0x%x]\n", eos_condition,
1339 (int) buf, (int) start);
1340 if (eos_condition == AUDAMRWB_EOS_SET)
1341 rc = audamrwb_process_eos(audio, start, mfield_size);
1342 mutex_unlock(&audio->write_lock);
1343 if (!rc) {
1344 if (buf > start)
1345 return buf - start;
1346 }
1347 return rc;
1348}
1349
1350static int audamrwb_release(struct inode *inode, struct file *file)
1351{
1352 struct audio *audio = file->private_data;
1353
1354 MM_INFO("audio instance 0x%08x freeing\n", (int)audio);
1355 mutex_lock(&audio->lock);
1356 audamrwb_disable(audio);
1357 if (audio->rmt_resource_released == 0)
1358 rmt_put_resource(audio);
1359 audamrwb_flush(audio);
1360 audamrwb_flush_pcm_buf(audio);
1361 msm_adsp_put(audio->audplay);
1362 audpp_adec_free(audio->dec_id);
1363#ifdef CONFIG_HAS_EARLYSUSPEND
1364 unregister_early_suspend(&audio->suspend_ctl.node);
1365#endif
1366 audio->event_abort = 1;
1367 wake_up(&audio->event_wait);
1368 audamrwb_reset_event_queue(audio);
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05301369 msm_subsystem_unmap_buffer(audio->map_v_write);
1370 free_contiguous_memory_by_paddr(audio->phys);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001371 if (audio->read_data) {
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05301372 msm_subsystem_unmap_buffer(audio->map_v_read);
1373 free_contiguous_memory_by_paddr(audio->read_phys);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001374 }
1375 mutex_unlock(&audio->lock);
1376#ifdef CONFIG_DEBUG_FS
1377 if (audio->dentry)
1378 debugfs_remove(audio->dentry);
1379#endif
1380 kfree(audio);
1381 return 0;
1382}
1383
1384#ifdef CONFIG_HAS_EARLYSUSPEND
1385static void audamrwb_post_event(struct audio *audio, int type,
1386 union msm_audio_event_payload payload)
1387{
1388 struct audamrwb_event *e_node = NULL;
1389 unsigned long flags;
1390
1391 spin_lock_irqsave(&audio->event_queue_lock, flags);
1392
1393 if (!list_empty(&audio->free_event_queue)) {
1394 e_node = list_first_entry(&audio->free_event_queue,
1395 struct audamrwb_event, list);
1396 list_del(&e_node->list);
1397 } else {
1398 e_node = kmalloc(sizeof(struct audamrwb_event), GFP_ATOMIC);
1399 if (!e_node) {
1400 MM_ERR("No mem to post event %d\n", type);
1401 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
1402 return;
1403 }
1404 }
1405
1406 e_node->event_type = type;
1407 e_node->payload = payload;
1408
1409 list_add_tail(&e_node->list, &audio->event_queue);
1410 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
1411 wake_up(&audio->event_wait);
1412}
1413
1414static void audamrwb_suspend(struct early_suspend *h)
1415{
1416 struct audamrwb_suspend_ctl *ctl =
1417 container_of(h, struct audamrwb_suspend_ctl, node);
1418 union msm_audio_event_payload payload;
1419
1420 MM_DBG("\n"); /* Macro prints the file name and function */
1421 audamrwb_post_event(ctl->audio, AUDIO_EVENT_SUSPEND, payload);
1422}
1423
1424static void audamrwb_resume(struct early_suspend *h)
1425{
1426 struct audamrwb_suspend_ctl *ctl =
1427 container_of(h, struct audamrwb_suspend_ctl, node);
1428 union msm_audio_event_payload payload;
1429
1430 MM_DBG("\n"); /* Macro prints the file name and function */
1431 audamrwb_post_event(ctl->audio, AUDIO_EVENT_RESUME, payload);
1432}
1433#endif
1434
1435#ifdef CONFIG_DEBUG_FS
1436static ssize_t audamrwb_debug_open(struct inode *inode, struct file *file)
1437{
1438 file->private_data = inode->i_private;
1439 return 0;
1440}
1441
1442static ssize_t audamrwb_debug_read(struct file *file, char __user *buf,
1443 size_t count, loff_t *ppos)
1444{
1445 const int debug_bufmax = 1024;
1446 static char buffer[1024];
1447 int n = 0, i;
1448 struct audio *audio = file->private_data;
1449
1450 mutex_lock(&audio->lock);
1451 n = scnprintf(buffer, debug_bufmax, "opened %d\n", audio->opened);
1452 n += scnprintf(buffer + n, debug_bufmax - n,
1453 "enabled %d\n", audio->enabled);
1454 n += scnprintf(buffer + n, debug_bufmax - n,
1455 "stopped %d\n", audio->stopped);
1456 n += scnprintf(buffer + n, debug_bufmax - n,
1457 "pcm_feedback %d\n", audio->pcm_feedback);
1458 n += scnprintf(buffer + n, debug_bufmax - n,
1459 "out_buf_sz %d\n", audio->out[0].size);
1460 n += scnprintf(buffer + n, debug_bufmax - n,
1461 "pcm_buf_count %d \n", audio->pcm_buf_count);
1462 n += scnprintf(buffer + n, debug_bufmax - n,
1463 "pcm_buf_sz %d \n", audio->in[0].size);
1464 n += scnprintf(buffer + n, debug_bufmax - n,
1465 "volume %x \n", audio->vol_pan.volume);
1466 n += scnprintf(buffer + n, debug_bufmax - n,
1467 "sample rate %d \n", audio->out_sample_rate);
1468 n += scnprintf(buffer + n, debug_bufmax - n,
1469 "channel mode %d \n", audio->out_channel_mode);
1470 mutex_unlock(&audio->lock);
1471 /* Following variables are only useful for debugging when
1472 * when playback halts unexpectedly. Thus, no mutual exclusion
1473 * enforced
1474 */
1475 n += scnprintf(buffer + n, debug_bufmax - n,
1476 "wflush %d\n", audio->wflush);
1477 n += scnprintf(buffer + n, debug_bufmax - n,
1478 "rflush %d\n", audio->rflush);
1479 n += scnprintf(buffer + n, debug_bufmax - n,
1480 "running %d \n", audio->running);
1481 n += scnprintf(buffer + n, debug_bufmax - n,
1482 "dec state %d \n", audio->dec_state);
1483 n += scnprintf(buffer + n, debug_bufmax - n,
1484 "out_needed %d \n", audio->out_needed);
1485 n += scnprintf(buffer + n, debug_bufmax - n,
1486 "out_head %d \n", audio->out_head);
1487 n += scnprintf(buffer + n, debug_bufmax - n,
1488 "out_tail %d \n", audio->out_tail);
1489 n += scnprintf(buffer + n, debug_bufmax - n,
1490 "out[0].used %d \n", audio->out[0].used);
1491 n += scnprintf(buffer + n, debug_bufmax - n,
1492 "out[1].used %d \n", audio->out[1].used);
1493 n += scnprintf(buffer + n, debug_bufmax - n,
1494 "buffer_refresh %d \n", audio->buf_refresh);
1495 n += scnprintf(buffer + n, debug_bufmax - n,
1496 "read_next %d \n", audio->read_next);
1497 n += scnprintf(buffer + n, debug_bufmax - n,
1498 "fill_next %d \n", audio->fill_next);
1499 for (i = 0; i < audio->pcm_buf_count; i++)
1500 n += scnprintf(buffer + n, debug_bufmax - n,
1501 "in[%d].used %d \n", i, audio->in[i].used);
1502 buffer[n] = 0;
1503 return simple_read_from_buffer(buf, count, ppos, buffer, n);
1504}
1505
1506static const struct file_operations audamrwb_debug_fops = {
1507 .read = audamrwb_debug_read,
1508 .open = audamrwb_debug_open,
1509};
1510#endif
1511
1512static int audamrwb_open(struct inode *inode, struct file *file)
1513{
1514 struct audio *audio = NULL;
1515 int rc, dec_attrb, decid, i;
1516 struct audamrwb_event *e_node = NULL;
1517#ifdef CONFIG_DEBUG_FS
1518 /* 4 bytes represents decoder number, 1 byte for terminate string */
1519 char name[sizeof "msm_amrwb_" + 5];
1520#endif
1521
1522 /* Allocate Mem for audio instance */
1523 audio = kzalloc(sizeof(struct audio), GFP_KERNEL);
1524 if (!audio) {
1525 MM_ERR("No memory to allocate audio instance\n");
1526 rc = -ENOMEM;
1527 goto done;
1528 }
1529 MM_INFO("audio instance 0x%08x created\n", (int)audio);
1530
1531 /* Allocate the decoder */
1532 dec_attrb = AUDDEC_DEC_AMRWB;
1533 if (file->f_mode & FMODE_READ)
1534 dec_attrb |= MSM_AUD_MODE_NONTUNNEL;
1535 else
1536 dec_attrb |= MSM_AUD_MODE_TUNNEL;
1537
1538 decid = audpp_adec_alloc(dec_attrb, &audio->module_name,
1539 &audio->queue_id);
1540
1541 if (decid < 0) {
1542 MM_ERR("No free decoder available, freeing instance 0x%08x\n",
1543 (int)audio);
1544 rc = -ENODEV;
1545 kfree(audio);
1546 goto done;
1547 }
1548
1549 audio->dec_id = decid & MSM_AUD_DECODER_MASK;
1550
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05301551 audio->phys = allocate_contiguous_ebi_nomap(DMASZ, SZ_4K);
1552 if (!audio->phys) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001553 MM_ERR("could not allocate write buffers, freeing instance \
1554 0x%08x\n", (int)audio);
1555 rc = -ENOMEM;
1556 audpp_adec_free(audio->dec_id);
1557 kfree(audio);
1558 goto done;
1559 } else {
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05301560 audio->map_v_write = msm_subsystem_map_buffer(
1561 audio->phys, DMASZ,
1562 MSM_SUBSYSTEM_MAP_KADDR,
1563 NULL, 0);
1564
1565 if (IS_ERR(audio->map_v_write)) {
1566 MM_ERR("could not map write buffers, freeing \
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001567 instance 0x%08x\n", (int)audio);
1568 rc = -ENOMEM;
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05301569 free_contiguous_memory_by_paddr(audio->phys);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001570 audpp_adec_free(audio->dec_id);
1571 kfree(audio);
1572 goto done;
1573 }
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05301574 audio->data = audio->map_v_write->vaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001575 MM_DBG("write buf: phy addr 0x%08x kernel addr 0x%08x\n",
1576 audio->phys, (int)audio->data);
1577 }
1578
1579 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK) {
1580 rc = audmgr_open(&audio->audmgr);
1581 if (rc) {
1582 MM_ERR("audmgr open failed, freeing instance \
1583 0x%08x\n", (int)audio);
1584 goto err;
1585 }
1586 }
1587
1588 rc = msm_adsp_get(audio->module_name, &audio->audplay,
1589 &audplay_adsp_ops_amrwb, audio);
1590 if (rc) {
1591 MM_ERR("failed to get %s module, freeing instance 0x%08x\n",
1592 audio->module_name, (int)audio);
1593 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK)
1594 audmgr_close(&audio->audmgr);
1595 goto err;
1596 }
1597
1598 rc = rmt_get_resource(audio);
1599 if (rc) {
1600 MM_ERR("ADSP resources are not available for AMRWB session \
1601 0x%08x on decoder: %d\n", (int)audio, audio->dec_id);
1602 if (audio->pcm_feedback == TUNNEL_MODE_PLAYBACK)
1603 audmgr_close(&audio->audmgr);
1604 msm_adsp_put(audio->audplay);
1605 goto err;
1606 }
1607
1608 mutex_init(&audio->lock);
1609 mutex_init(&audio->write_lock);
1610 mutex_init(&audio->read_lock);
1611 mutex_init(&audio->get_event_lock);
1612 spin_lock_init(&audio->dsp_lock);
1613 spin_lock_init(&audio->event_queue_lock);
1614 INIT_LIST_HEAD(&audio->free_event_queue);
1615 INIT_LIST_HEAD(&audio->event_queue);
1616 init_waitqueue_head(&audio->write_wait);
1617 init_waitqueue_head(&audio->read_wait);
1618 init_waitqueue_head(&audio->wait);
1619 init_waitqueue_head(&audio->event_wait);
1620
1621 audio->out[0].data = audio->data + 0;
1622 audio->out[0].addr = audio->phys + 0;
1623 audio->out[0].size = BUFSZ;
1624
1625 audio->out[1].data = audio->data + BUFSZ;
1626 audio->out[1].addr = audio->phys + BUFSZ;
1627 audio->out[1].size = BUFSZ;
1628
1629 audio->vol_pan.volume = 0x2000;
1630 audio->vol_pan.pan = 0x0;
1631 audio->eq_enable = 0;
1632 audio->out_sample_rate = 44100;
1633 audio->out_channel_mode = AUDPP_CMD_PCM_INTF_STEREO_V;
1634
1635 audamrwb_flush(audio);
1636
1637 file->private_data = audio;
1638 audio->opened = 1;
1639 audio->event_abort = 0;
1640#ifdef CONFIG_DEBUG_FS
1641 snprintf(name, sizeof name, "msm_amrwb_%04x", audio->dec_id);
1642 audio->dentry = debugfs_create_file(name, S_IFREG | S_IRUGO,
1643 NULL, (void *) audio, &audamrwb_debug_fops);
1644
1645 if (IS_ERR(audio->dentry))
1646 MM_DBG("debugfs_create_file failed\n");
1647#endif
1648#ifdef CONFIG_HAS_EARLYSUSPEND
1649 audio->suspend_ctl.node.level = EARLY_SUSPEND_LEVEL_DISABLE_FB;
1650 audio->suspend_ctl.node.resume = audamrwb_resume;
1651 audio->suspend_ctl.node.suspend = audamrwb_suspend;
1652 audio->suspend_ctl.audio = audio;
1653 register_early_suspend(&audio->suspend_ctl.node);
1654#endif
1655 for (i = 0; i < AUDAMRWB_EVENT_NUM; i++) {
1656 e_node = kmalloc(sizeof(struct audamrwb_event), GFP_KERNEL);
1657 if (e_node)
1658 list_add_tail(&e_node->list, &audio->free_event_queue);
1659 else {
1660 MM_ERR("event pkt alloc failed\n");
1661 break;
1662 }
1663 }
1664done:
1665 return rc;
1666err:
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05301667 msm_subsystem_unmap_buffer(audio->map_v_write);
1668 free_contiguous_memory_by_paddr(audio->phys);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001669 audpp_adec_free(audio->dec_id);
1670 kfree(audio);
1671 return rc;
1672}
1673
1674static const struct file_operations audio_amrwb_fops = {
1675 .owner = THIS_MODULE,
1676 .open = audamrwb_open,
1677 .release = audamrwb_release,
1678 .read = audamrwb_read,
1679 .write = audamrwb_write,
1680 .unlocked_ioctl = audamrwb_ioctl,
1681 .fsync = audamrwb_fsync,
1682};
1683
1684struct miscdevice audio_amrwb_misc = {
1685 .minor = MISC_DYNAMIC_MINOR,
1686 .name = "msm_amrwb",
1687 .fops = &audio_amrwb_fops,
1688};
1689
1690static int __init audamrwb_init(void)
1691{
1692 return misc_register(&audio_amrwb_misc);
1693}
1694
1695static void __exit audamrwb_exit(void)
1696{
1697 misc_deregister(&audio_amrwb_misc);
1698}
1699
1700module_init(audamrwb_init);
1701module_exit(audamrwb_exit);
1702
1703MODULE_DESCRIPTION("MSM AMR-WB driver");
1704MODULE_LICENSE("GPL v2");