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