blob: 33ca7a15397b3e211c0d70e20e828676f3368ead [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/*
2 * qcelp 13k audio decoder device
3 *
Duy Truong790f06d2013-02-13 16:38:12 -08004 * Copyright (c) 2008-2012, The Linux Foundation. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07005 *
6 * This code is based in part on audio_mp3.c, which is
7 * Copyright (C) 2008 Google, Inc.
8 * Copyright (C) 2008 HTC Corporation
9 *
10 * This software is licensed under the terms of the GNU General Public
11 * License version 2, as published by the Free Software Foundation, and
12 * may be copied, distributed, and modified under those terms.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17 *
18 * See the GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, you can find it at http://www.fsf.org.
21 *
22 */
23
Santosh Mardifdc227a2011-07-11 17:20:34 +053024#include <asm/ioctls.h>
25
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070026#include <linux/module.h>
27#include <linux/fs.h>
28#include <linux/miscdevice.h>
29#include <linux/uaccess.h>
30#include <linux/sched.h>
31#include <linux/wait.h>
32#include <linux/dma-mapping.h>
33#include <linux/debugfs.h>
34#include <linux/earlysuspend.h>
35#include <linux/list.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070036#include <linux/slab.h>
Santosh Mardifdc227a2011-07-11 17:20:34 +053037#include <linux/msm_audio.h>
38#include <linux/memory_alloc.h>
39
40#include <mach/msm_adsp.h>
41#include <mach/iommu.h>
42#include <mach/iommu_domains.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070043#include <mach/qdsp5v2/qdsp5audppmsg.h>
44#include <mach/qdsp5v2/qdsp5audplaycmdi.h>
45#include <mach/qdsp5v2/qdsp5audplaymsg.h>
46#include <mach/qdsp5v2/audpp.h>
47#include <mach/qdsp5v2/audio_dev_ctl.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070048#include <mach/qdsp5v2/audio_dev_ctl.h>
49#include <mach/debug_mm.h>
Santosh Mardifdc227a2011-07-11 17:20:34 +053050#include <mach/msm_memtypes.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070051
52#define BUFSZ 1094 /* QCELP 13K Hold 600ms packet data = 36 * 30 and
53 14 bytes of meta in */
54#define BUF_COUNT 2
55#define DMASZ (BUFSZ * BUF_COUNT)
56
57#define PCM_BUFSZ_MIN 1624 /* 100ms worth of data and
58 24 bytes of meta out */
59#define PCM_BUF_MAX_COUNT 5
60
61#define AUDDEC_DEC_QCELP 9
62
63#define ROUTING_MODE_FTRT 1
64#define ROUTING_MODE_RT 2
65/* Decoder status received from AUDPPTASK */
66#define AUDPP_DEC_STATUS_SLEEP 0
67#define AUDPP_DEC_STATUS_INIT 1
68#define AUDPP_DEC_STATUS_CFG 2
69#define AUDPP_DEC_STATUS_PLAY 3
70
71#define AUDQCELP_METAFIELD_MASK 0xFFFF0000
72#define AUDQCELP_EOS_FLG_OFFSET 0x0A /* Offset from beginning of buffer */
73#define AUDQCELP_EOS_FLG_MASK 0x01
74#define AUDQCELP_EOS_NONE 0x0 /* No EOS detected */
75#define AUDQCELP_EOS_SET 0x1 /* EOS set in meta field */
76
77#define AUDQCELP_EVENT_NUM 10 /* Default number of pre-allocated event pkts */
78
79struct buffer {
80 void *data;
81 unsigned size;
82 unsigned used; /* Input usage actual DSP produced PCM size */
83 unsigned addr;
84 unsigned short mfield_sz; /*only useful for data has meta field */
85};
86
87#ifdef CONFIG_HAS_EARLYSUSPEND
88struct audqcelp_suspend_ctl {
89 struct early_suspend node;
90 struct audio *audio;
91};
92#endif
93
94struct audqcelp_event{
95 struct list_head list;
96 int event_type;
97 union msm_audio_event_payload payload;
98};
99
100struct audio {
101 struct buffer out[BUF_COUNT];
102
103 spinlock_t dsp_lock;
104
105 uint8_t out_head;
106 uint8_t out_tail;
107 uint8_t out_needed; /* number of buffers the dsp is waiting for */
108
109 struct mutex lock;
110 struct mutex write_lock;
111 wait_queue_head_t write_wait;
112
113 /* Host PCM section - START */
114 struct buffer in[PCM_BUF_MAX_COUNT];
115 struct mutex read_lock;
116 wait_queue_head_t read_wait; /* Wait queue for read */
117 char *read_data; /* pointer to reader buffer */
118 int32_t read_phys; /* physical address of reader buffer */
119 uint8_t read_next; /* index to input buffers to be read next */
120 uint8_t fill_next; /* index to buffer that DSP should be filling */
121 uint8_t pcm_buf_count; /* number of pcm buffer allocated */
122 /* Host PCM section - END */
123
124 struct msm_adsp_module *audplay;
125
126 /* data allocated for various buffers */
127 char *data;
128 int32_t phys; /* physical address of write buffer */
Laura Abbott61399692012-04-30 14:25:46 -0700129 void *map_v_read;
130 void *map_v_write;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700131
132 int mfield; /* meta field embedded in data */
133 int rflush; /* Read flush */
134 int wflush; /* Write flush */
135 uint8_t opened:1;
136 uint8_t enabled:1;
137 uint8_t running:1;
138 uint8_t stopped:1; /* set when stopped, cleared on flush */
139 uint8_t pcm_feedback:1; /* set when non-tunnel mode */
140 uint8_t buf_refresh:1;
141 int teos; /* valid only if tunnel mode & no data left for decoder */
142 enum msm_aud_decoder_state dec_state; /* Represents decoder state */
143
144 const char *module_name;
145 unsigned queue_id;
146 uint16_t dec_id;
147 int16_t source;
148
149#ifdef CONFIG_HAS_EARLYSUSPEND
150 struct audqcelp_suspend_ctl suspend_ctl;
151#endif
152
153#ifdef CONFIG_DEBUG_FS
154 struct dentry *dentry;
155#endif
156
157 wait_queue_head_t wait;
158 struct list_head free_event_queue;
159 struct list_head event_queue;
160 wait_queue_head_t event_wait;
161 spinlock_t event_queue_lock;
162 struct mutex get_event_lock;
163 int event_abort;
164 /* AV sync Info */
165 int avsync_flag; /* Flag to indicate feedback from DSP */
166 wait_queue_head_t avsync_wait;/* Wait queue for AV Sync Message */
167 /* flags, 48 bits sample/bytes counter per channel */
168 uint16_t avsync[AUDPP_AVSYNC_CH_COUNT * AUDPP_AVSYNC_NUM_WORDS + 1];
169
170 uint32_t device_events;
171
172 int eq_enable;
173 int eq_needs_commit;
174 struct audpp_cmd_cfg_object_params_eqalizer eq;
175 struct audpp_cmd_cfg_object_params_volume vol_pan;
176};
177
178static int auddec_dsp_config(struct audio *audio, int enable);
179static void audpp_cmd_cfg_adec_params(struct audio *audio);
180static void audpp_cmd_cfg_routing_mode(struct audio *audio);
181static void audqcelp_send_data(struct audio *audio, unsigned needed);
182static void audqcelp_config_hostpcm(struct audio *audio);
183static void audqcelp_buffer_refresh(struct audio *audio);
184static void audqcelp_dsp_event(void *private, unsigned id, uint16_t *msg);
Vinay Vaka051db722012-01-24 19:48:32 +0530185#ifdef CONFIG_HAS_EARLYSUSPEND
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700186static void audqcelp_post_event(struct audio *audio, int type,
187 union msm_audio_event_payload payload);
Vinay Vaka051db722012-01-24 19:48:32 +0530188#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700189
190/* must be called with audio->lock held */
191static int audqcelp_enable(struct audio *audio)
192{
193 MM_DBG("\n"); /* Macro prints the file name and function */
194 if (audio->enabled)
195 return 0;
196
197 audio->dec_state = MSM_AUD_DECODER_STATE_NONE;
198 audio->out_tail = 0;
199 audio->out_needed = 0;
200
201 if (msm_adsp_enable(audio->audplay)) {
202 MM_ERR("msm_adsp_enable(audplay) failed\n");
203 return -ENODEV;
204 }
205
206 if (audpp_enable(audio->dec_id, audqcelp_dsp_event, audio)) {
207 MM_ERR("audpp_enable() failed\n");
208 msm_adsp_disable(audio->audplay);
209 return -ENODEV;
210 }
211 audio->enabled = 1;
212 return 0;
213}
214
215static void qcelp_listner(u32 evt_id, union auddev_evt_data *evt_payload,
216 void *private_data)
217{
218 struct audio *audio = (struct audio *) private_data;
219 switch (evt_id) {
220 case AUDDEV_EVT_DEV_RDY:
221 MM_DBG(":AUDDEV_EVT_DEV_RDY\n");
222 audio->source |= (0x1 << evt_payload->routing_id);
223 if (audio->running == 1 && audio->enabled == 1)
224 audpp_route_stream(audio->dec_id, audio->source);
225 break;
226 case AUDDEV_EVT_DEV_RLS:
227 MM_DBG(":AUDDEV_EVT_DEV_RLS\n");
228 audio->source &= ~(0x1 << evt_payload->routing_id);
229 if (audio->running == 1 && audio->enabled == 1)
230 audpp_route_stream(audio->dec_id, audio->source);
231 break;
232 case AUDDEV_EVT_STREAM_VOL_CHG:
233 audio->vol_pan.volume = evt_payload->session_vol;
234 MM_DBG(":AUDDEV_EVT_STREAM_VOL_CHG, stream vol %d\n",
235 audio->vol_pan.volume);
236 if (audio->running)
237 audpp_dsp_set_vol_pan(audio->dec_id, &audio->vol_pan,
238 POPP);
239 break;
240 default:
241 MM_ERR(":ERROR:wrong event\n");
242 break;
243 }
244}
245/* must be called with audio->lock held */
246static int audqcelp_disable(struct audio *audio)
247{
248 int rc = 0;
249 MM_DBG("\n"); /* Macro prints the file name and function */
250 if (audio->enabled) {
251 audio->enabled = 0;
252 audio->dec_state = MSM_AUD_DECODER_STATE_NONE;
253 auddec_dsp_config(audio, 0);
254 rc = wait_event_interruptible_timeout(audio->wait,
255 audio->dec_state != MSM_AUD_DECODER_STATE_NONE,
256 msecs_to_jiffies(MSM_AUD_DECODER_WAIT_MS));
257 if (rc == 0)
258 rc = -ETIMEDOUT;
259 else if (audio->dec_state != MSM_AUD_DECODER_STATE_CLOSE)
260 rc = -EFAULT;
261 else
262 rc = 0;
263 wake_up(&audio->write_wait);
264 wake_up(&audio->read_wait);
265 msm_adsp_disable(audio->audplay);
266 audpp_disable(audio->dec_id, audio);
267 audio->out_needed = 0;
268 }
269 return rc;
270}
271
272/* ------------------- dsp --------------------- */
273static void audqcelp_update_pcm_buf_entry(struct audio *audio,
274 uint32_t *payload)
275{
276 uint8_t index;
277 unsigned long flags;
278
279 if (audio->rflush)
280 return;
281
282 spin_lock_irqsave(&audio->dsp_lock, flags);
283 for (index = 0; index < payload[1]; index++) {
284 if (audio->in[audio->fill_next].addr ==
285 payload[2 + index * 2]) {
286 MM_DBG("in[%d] ready\n", audio->fill_next);
287 audio->in[audio->fill_next].used =
288 payload[3 + index * 2];
289 if ((++audio->fill_next) == audio->pcm_buf_count)
290 audio->fill_next = 0;
291 } else {
292 MM_ERR("expected=%x ret=%x\n",
293 audio->in[audio->fill_next].addr,
294 payload[1 + index * 2]);
295 break;
296 }
297 }
298 if (audio->in[audio->fill_next].used == 0) {
299 audqcelp_buffer_refresh(audio);
300 } else {
301 MM_DBG("read cannot keep up\n");
302 audio->buf_refresh = 1;
303 }
304 wake_up(&audio->read_wait);
305 spin_unlock_irqrestore(&audio->dsp_lock, flags);
306}
307
308static void audplay_dsp_event(void *data, unsigned id, size_t len,
309 void (*getevent) (void *ptr, size_t len))
310{
311 struct audio *audio = data;
312 uint32_t msg[28];
313 getevent(msg, sizeof(msg));
314
315 MM_DBG("msg_id=%x\n", id);
316
317 switch (id) {
318 case AUDPLAY_MSG_DEC_NEEDS_DATA:
319 audqcelp_send_data(audio, 1);
320 break;
321
322 case AUDPLAY_MSG_BUFFER_UPDATE:
323 audqcelp_update_pcm_buf_entry(audio, msg);
324 break;
325
326 case ADSP_MESSAGE_ID:
327 MM_DBG("Received ADSP event: module enable(audplaytask)\n");
328 break;
329
330 default:
331 MM_ERR("unexpected message from decoder \n");
332 }
333}
334
335static void audqcelp_dsp_event(void *private, unsigned id, uint16_t *msg)
336{
337 struct audio *audio = private;
338
339 switch (id) {
340 case AUDPP_MSG_STATUS_MSG:{
341 unsigned status = msg[1];
342
343 switch (status) {
344 case AUDPP_DEC_STATUS_SLEEP: {
345 uint16_t reason = msg[2];
346 MM_DBG("decoder status:sleep reason = \
347 0x%04x\n", reason);
348 if ((reason == AUDPP_MSG_REASON_MEM)
349 || (reason ==
350 AUDPP_MSG_REASON_NODECODER)) {
351 audio->dec_state =
352 MSM_AUD_DECODER_STATE_FAILURE;
353 wake_up(&audio->wait);
354 } else if (reason == AUDPP_MSG_REASON_NONE) {
355 /* decoder is in disable state */
356 audio->dec_state =
357 MSM_AUD_DECODER_STATE_CLOSE;
358 wake_up(&audio->wait);
359 }
360 break;
361 }
362 case AUDPP_DEC_STATUS_INIT:
363 MM_DBG("decoder status: init \n");
364 if (audio->pcm_feedback)
365 audpp_cmd_cfg_routing_mode(audio);
366 else
367 audpp_cmd_cfg_adec_params(audio);
368 break;
369
370 case AUDPP_DEC_STATUS_CFG:
371 MM_DBG("decoder status: cfg \n");
372 break;
373 case AUDPP_DEC_STATUS_PLAY:
374 MM_DBG("decoder status: play \n");
375 /* send mixer command */
376 audpp_route_stream(audio->dec_id,
377 audio->source);
378 if (audio->pcm_feedback) {
379 audqcelp_config_hostpcm(audio);
380 audqcelp_buffer_refresh(audio);
381 }
382 audio->dec_state =
383 MSM_AUD_DECODER_STATE_SUCCESS;
384 wake_up(&audio->wait);
385 break;
386 default:
387 MM_ERR("unknown decoder status\n");
388 }
389 break;
390 }
391 case AUDPP_MSG_CFG_MSG:
392 if (msg[0] == AUDPP_MSG_ENA_ENA) {
393 MM_DBG("CFG_MSG ENABLE\n");
394 auddec_dsp_config(audio, 1);
395 audio->out_needed = 0;
396 audio->running = 1;
397 audpp_dsp_set_vol_pan(audio->dec_id, &audio->vol_pan,
398 POPP);
399 audpp_dsp_set_eq(audio->dec_id, audio->eq_enable,
400 &audio->eq, POPP);
401 } else if (msg[0] == AUDPP_MSG_ENA_DIS) {
402 MM_DBG("CFG_MSG DISABLE\n");
403 audio->running = 0;
404 } else {
405 MM_DBG("CFG_MSG %d?\n", msg[0]);
406 }
407 break;
408 case AUDPP_MSG_ROUTING_ACK:
409 MM_DBG("ROUTING_ACK mode=%d\n", msg[1]);
410 audpp_cmd_cfg_adec_params(audio);
411 break;
412 case AUDPP_MSG_FLUSH_ACK:
413 MM_DBG("FLUSH_ACK\n");
414 audio->wflush = 0;
415 audio->rflush = 0;
416 wake_up(&audio->write_wait);
417 if (audio->pcm_feedback)
418 audqcelp_buffer_refresh(audio);
419 break;
420 case AUDPP_MSG_PCMDMAMISSED:
421 MM_DBG("PCMDMAMISSED\n");
422 audio->teos = 1;
423 wake_up(&audio->write_wait);
424 break;
425
426 case AUDPP_MSG_AVSYNC_MSG:
427 MM_DBG("AUDPP_MSG_AVSYNC_MSG\n");
428 memcpy(&audio->avsync[0], msg, sizeof(audio->avsync));
429 audio->avsync_flag = 1;
430 wake_up(&audio->avsync_wait);
431 break;
432
433 default:
434 MM_ERR("UNKNOWN (%d)\n", id);
435 }
436
437}
438
439struct msm_adsp_ops audplay_adsp_ops_qcelp = {
440 .event = audplay_dsp_event,
441};
442
443#define audplay_send_queue0(audio, cmd, len) \
444 msm_adsp_write(audio->audplay, audio->queue_id, \
445 cmd, len)
446
447static int auddec_dsp_config(struct audio *audio, int enable)
448{
449 struct audpp_cmd_cfg_dec_type cfg_dec_cmd;
450
451 memset(&cfg_dec_cmd, 0, sizeof(cfg_dec_cmd));
452
453 cfg_dec_cmd.cmd_id = AUDPP_CMD_CFG_DEC_TYPE;
454 if (enable)
455 cfg_dec_cmd.dec_cfg = AUDPP_CMD_UPDATDE_CFG_DEC |
456 AUDPP_CMD_ENA_DEC_V | AUDDEC_DEC_QCELP;
457 else
458 cfg_dec_cmd.dec_cfg = AUDPP_CMD_UPDATDE_CFG_DEC |
459 AUDPP_CMD_DIS_DEC_V;
460 cfg_dec_cmd.dm_mode = 0x0;
461 cfg_dec_cmd.stream_id = audio->dec_id;
462
463 return audpp_send_queue1(&cfg_dec_cmd, sizeof(cfg_dec_cmd));
464}
465
466static void audpp_cmd_cfg_adec_params(struct audio *audio)
467{
468 struct audpp_cmd_cfg_adec_params_v13k cmd;
469
470 memset(&cmd, 0, sizeof(cmd));
471 cmd.common.cmd_id = AUDPP_CMD_CFG_ADEC_PARAMS;
472 cmd.common.length = AUDPP_CMD_CFG_ADEC_PARAMS_V13K_LEN;
473 cmd.common.dec_id = audio->dec_id;
474 cmd.common.input_sampling_frequency = 8000;
475 cmd.stereo_cfg = AUDPP_CMD_PCM_INTF_MONO_V;
476
477 audpp_send_queue2(&cmd, sizeof(cmd));
478}
479
480static void audpp_cmd_cfg_routing_mode(struct audio *audio)
481{
482 struct audpp_cmd_routing_mode cmd;
483 MM_DBG("\n"); /* Macro prints the file name and function */
484 memset(&cmd, 0, sizeof(cmd));
485 cmd.cmd_id = AUDPP_CMD_ROUTING_MODE;
486 cmd.object_number = audio->dec_id;
487 if (audio->pcm_feedback)
488 cmd.routing_mode = ROUTING_MODE_FTRT;
489 else
490 cmd.routing_mode = ROUTING_MODE_RT;
491 audpp_send_queue1(&cmd, sizeof(cmd));
492}
493
494static int audplay_dsp_send_data_avail(struct audio *audio,
495 unsigned idx, unsigned len)
496{
497 struct audplay_cmd_bitstream_data_avail_nt2 cmd;
498
499 cmd.cmd_id = AUDPLAY_CMD_BITSTREAM_DATA_AVAIL_NT2;
500 if (audio->mfield)
501 cmd.decoder_id = AUDQCELP_METAFIELD_MASK |
502 (audio->out[idx].mfield_sz >> 1);
503 else
504 cmd.decoder_id = audio->dec_id;
505 cmd.buf_ptr = audio->out[idx].addr;
506 cmd.buf_size = len / 2;
507 cmd.partition_number = 0;
508 return audplay_send_queue0(audio, &cmd, sizeof(cmd));
509}
510
511static void audqcelp_buffer_refresh(struct audio *audio)
512{
513 struct audplay_cmd_buffer_refresh refresh_cmd;
514
515 refresh_cmd.cmd_id = AUDPLAY_CMD_BUFFER_REFRESH;
516 refresh_cmd.num_buffers = 1;
517 refresh_cmd.buf0_address = audio->in[audio->fill_next].addr;
518 refresh_cmd.buf0_length = audio->in[audio->fill_next].size;
519 refresh_cmd.buf_read_count = 0;
520 MM_DBG("buf0_addr=%x buf0_len=%d\n", refresh_cmd.buf0_address,
521 refresh_cmd.buf0_length);
522
523 (void)audplay_send_queue0(audio, &refresh_cmd, sizeof(refresh_cmd));
524}
525
526static void audqcelp_config_hostpcm(struct audio *audio)
527{
528 struct audplay_cmd_hpcm_buf_cfg cfg_cmd;
529
530 MM_DBG("\n"); /* Macro prints the file name and function */
531 cfg_cmd.cmd_id = AUDPLAY_CMD_HPCM_BUF_CFG;
532 cfg_cmd.max_buffers = 1;
533 cfg_cmd.byte_swap = 0;
534 cfg_cmd.hostpcm_config = (0x8000) | (0x4000);
535 cfg_cmd.feedback_frequency = 1;
536 cfg_cmd.partition_number = 0;
537
538 (void)audplay_send_queue0(audio, &cfg_cmd, sizeof(cfg_cmd));
539}
540
541static void audqcelp_send_data(struct audio *audio, unsigned needed)
542{
543 struct buffer *frame;
544 unsigned long flags;
545
546 spin_lock_irqsave(&audio->dsp_lock, flags);
547 if (!audio->running)
548 goto done;
549
550 if (needed && !audio->wflush) {
551 /* We were called from the callback because the DSP
552 * requested more data. Note that the DSP does want
553 * more data, and if a buffer was in-flight, mark it
554 * as available (since the DSP must now be done with
555 * it).
556 */
557 audio->out_needed = 1;
558 frame = audio->out + audio->out_tail;
559 if (frame->used == 0xffffffff) {
560 MM_DBG("frame %d free\n", audio->out_tail);
561 frame->used = 0;
562 audio->out_tail ^= 1;
563 wake_up(&audio->write_wait);
564 }
565 }
566
567 if (audio->out_needed) {
568 /* If the DSP currently wants data and we have a
569 * buffer available, we will send it and reset
570 * the needed flag. We'll mark the buffer as in-flight
571 * so that it won't be recycled until the next buffer
572 * is requested
573 */
574
575 frame = audio->out + audio->out_tail;
576 if (frame->used) {
577 BUG_ON(frame->used == 0xffffffff);
578 MM_DBG("frame %d busy\n", audio->out_tail);
579 audplay_dsp_send_data_avail(audio, audio->out_tail,
580 frame->used);
581 frame->used = 0xffffffff;
582 audio->out_needed = 0;
583 }
584 }
585 done:
586 spin_unlock_irqrestore(&audio->dsp_lock, flags);
587}
588
589/* ------------------- device --------------------- */
590
591static void audqcelp_flush(struct audio *audio)
592{
593 audio->out[0].used = 0;
594 audio->out[1].used = 0;
595 audio->out_head = 0;
596 audio->out_tail = 0;
597 audio->out_needed = 0;
598}
599
600static void audqcelp_flush_pcm_buf(struct audio *audio)
601{
602 uint8_t index;
603
604 for (index = 0; index < PCM_BUF_MAX_COUNT; index++)
605 audio->in[index].used = 0;
606
607 audio->buf_refresh = 0;
608 audio->read_next = 0;
609 audio->fill_next = 0;
610}
611
612static void audqcelp_ioport_reset(struct audio *audio)
613{
614 /* Make sure read/write thread are free from
615 * sleep and knowing that system is not able
616 * to process io request at the moment
617 */
618 wake_up(&audio->write_wait);
619 mutex_lock(&audio->write_lock);
620 audqcelp_flush(audio);
621 mutex_unlock(&audio->write_lock);
622 wake_up(&audio->read_wait);
623 mutex_lock(&audio->read_lock);
624 audqcelp_flush_pcm_buf(audio);
625 mutex_unlock(&audio->read_lock);
626 audio->avsync_flag = 1;
627 wake_up(&audio->avsync_wait);
628}
629
630static int audqcelp_events_pending(struct audio *audio)
631{
632 unsigned long flags;
633 int empty;
634
635 spin_lock_irqsave(&audio->event_queue_lock, flags);
636 empty = !list_empty(&audio->event_queue);
637 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
638 return empty || audio->event_abort;
639}
640
641static void audqcelp_reset_event_queue(struct audio *audio)
642{
643 unsigned long flags;
644 struct audqcelp_event *drv_evt;
645 struct list_head *ptr, *next;
646
647 spin_lock_irqsave(&audio->event_queue_lock, flags);
648 list_for_each_safe(ptr, next, &audio->event_queue) {
649 drv_evt = list_first_entry(&audio->event_queue,
650 struct audqcelp_event, list);
651 list_del(&drv_evt->list);
652 kfree(drv_evt);
653 }
654 list_for_each_safe(ptr, next, &audio->free_event_queue) {
655 drv_evt = list_first_entry(&audio->free_event_queue,
656 struct audqcelp_event, list);
657 list_del(&drv_evt->list);
658 kfree(drv_evt);
659 }
660 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
661
662 return;
663}
664
665
666static long audqcelp_process_event_req(struct audio *audio, void __user *arg)
667{
668 long rc;
669 struct msm_audio_event usr_evt;
670 struct audqcelp_event *drv_evt = NULL;
671 int timeout;
672 unsigned long flags;
673
674 if (copy_from_user(&usr_evt, arg, sizeof(struct msm_audio_event)))
675 return -EFAULT;
676
677 timeout = (int) usr_evt.timeout_ms;
678
679 if (timeout > 0) {
680 rc = wait_event_interruptible_timeout(
681 audio->event_wait, audqcelp_events_pending(audio),
682 msecs_to_jiffies(timeout));
683 if (rc == 0)
684 return -ETIMEDOUT;
685 } else {
686 rc = wait_event_interruptible(
687 audio->event_wait, audqcelp_events_pending(audio));
688 }
689
690 if (rc < 0)
691 return rc;
692
693 if (audio->event_abort) {
694 audio->event_abort = 0;
695 return -ENODEV;
696 }
697
698 rc = 0;
699
700 spin_lock_irqsave(&audio->event_queue_lock, flags);
701 if (!list_empty(&audio->event_queue)) {
702 drv_evt = list_first_entry(&audio->event_queue,
703 struct audqcelp_event, list);
704 list_del(&drv_evt->list);
705 }
706
707 if (drv_evt) {
708 usr_evt.event_type = drv_evt->event_type;
709 usr_evt.event_payload = drv_evt->payload;
710 list_add_tail(&drv_evt->list, &audio->free_event_queue);
711 } else
712 rc = -1;
713 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
714
715 if (!rc && copy_to_user(arg, &usr_evt, sizeof(usr_evt)))
716 rc = -EFAULT;
717
718 return rc;
719}
720
721static int audio_enable_eq(struct audio *audio, int enable)
722{
723 if (audio->eq_enable == enable && !audio->eq_needs_commit)
724 return 0;
725
726 audio->eq_enable = enable;
727
728 if (audio->running) {
729 audpp_dsp_set_eq(audio->dec_id, enable, &audio->eq, POPP);
730 audio->eq_needs_commit = 0;
731 }
732 return 0;
733}
734
735static int audio_get_avsync_data(struct audio *audio,
736 struct msm_audio_stats *stats)
737{
738 int rc = -EINVAL;
739 unsigned long flags;
740
741 local_irq_save(flags);
742 if (audio->dec_id == audio->avsync[0] && audio->avsync_flag) {
743 /* av_sync sample count */
744 stats->sample_count = (audio->avsync[2] << 16) |
745 (audio->avsync[3]);
746
747 /* av_sync byte_count */
748 stats->byte_count = (audio->avsync[5] << 16) |
749 (audio->avsync[6]);
750
751 audio->avsync_flag = 0;
752 rc = 0;
753 }
754 local_irq_restore(flags);
755 return rc;
756
757}
758
759static long audqcelp_ioctl(struct file *file, unsigned int cmd,
760 unsigned long arg)
761{
762 struct audio *audio = file->private_data;
763 int rc = -EINVAL;
764 unsigned long flags = 0;
765 uint16_t enable_mask;
766 int enable;
767 int prev_state;
768
769 MM_DBG("cmd = %d\n", cmd);
770
771 if (cmd == AUDIO_GET_STATS) {
772 struct msm_audio_stats stats;
773
774 audio->avsync_flag = 0;
775 memset(&stats, 0, sizeof(stats));
776 if (audpp_query_avsync(audio->dec_id) < 0)
777 return rc;
778
779 rc = wait_event_interruptible_timeout(audio->avsync_wait,
780 (audio->avsync_flag == 1),
781 msecs_to_jiffies(AUDPP_AVSYNC_EVENT_TIMEOUT));
782
783 if (rc < 0)
784 return rc;
785 else if ((rc > 0) || ((rc == 0) && (audio->avsync_flag == 1))) {
786 if (audio_get_avsync_data(audio, &stats) < 0)
787 return rc;
788
789 if (copy_to_user((void *)arg, &stats, sizeof(stats)))
790 return -EFAULT;
791 return 0;
792 } else
793 return -EAGAIN;
794 }
795
796 switch (cmd) {
797 case AUDIO_ENABLE_AUDPP:
798 if (copy_from_user(&enable_mask, (void *) arg,
799 sizeof(enable_mask))) {
800 rc = -EFAULT;
801 break;
802 }
803
804 spin_lock_irqsave(&audio->dsp_lock, flags);
805 enable = (enable_mask & EQ_ENABLE) ? 1 : 0;
806 audio_enable_eq(audio, enable);
807 spin_unlock_irqrestore(&audio->dsp_lock, flags);
808 rc = 0;
809 break;
810 case AUDIO_SET_VOLUME:
811 spin_lock_irqsave(&audio->dsp_lock, flags);
812 audio->vol_pan.volume = arg;
813 if (audio->running)
814 audpp_dsp_set_vol_pan(audio->dec_id, &audio->vol_pan,
815 POPP);
816 spin_unlock_irqrestore(&audio->dsp_lock, flags);
817 rc = 0;
818 break;
819
820 case AUDIO_SET_PAN:
821 spin_lock_irqsave(&audio->dsp_lock, flags);
822 audio->vol_pan.pan = arg;
823 if (audio->running)
824 audpp_dsp_set_vol_pan(audio->dec_id, &audio->vol_pan,
825 POPP);
826 spin_unlock_irqrestore(&audio->dsp_lock, flags);
827 rc = 0;
828 break;
829
830 case AUDIO_SET_EQ:
831 prev_state = audio->eq_enable;
832 audio->eq_enable = 0;
833 if (copy_from_user(&audio->eq.num_bands, (void *) arg,
834 sizeof(audio->eq) -
835 (AUDPP_CMD_CFG_OBJECT_PARAMS_COMMON_LEN + 2))) {
836 rc = -EFAULT;
837 break;
838 }
839 audio->eq_enable = prev_state;
840 audio->eq_needs_commit = 1;
841 rc = 0;
842 break;
843 }
844
845 if (-EINVAL != rc)
846 return rc;
847
848 if (cmd == AUDIO_GET_EVENT) {
849 MM_DBG("AUDIO_GET_EVENT\n");
850 if (mutex_trylock(&audio->get_event_lock)) {
851 rc = audqcelp_process_event_req(audio,
852 (void __user *) arg);
853 mutex_unlock(&audio->get_event_lock);
854 } else
855 rc = -EBUSY;
856 return rc;
857 }
858
859 if (cmd == AUDIO_ABORT_GET_EVENT) {
860 audio->event_abort = 1;
861 wake_up(&audio->event_wait);
862 return 0;
863 }
864
865 mutex_lock(&audio->lock);
866 switch (cmd) {
867 case AUDIO_START:
868 MM_DBG("AUDIO_START\n");
869 rc = audqcelp_enable(audio);
870 if (!rc) {
871 rc = wait_event_interruptible_timeout(audio->wait,
872 audio->dec_state != MSM_AUD_DECODER_STATE_NONE,
873 msecs_to_jiffies(MSM_AUD_DECODER_WAIT_MS));
874 MM_INFO("dec_state %d rc = %d\n", audio->dec_state, rc);
875
876 if (audio->dec_state != MSM_AUD_DECODER_STATE_SUCCESS)
877 rc = -ENODEV;
878 else
879 rc = 0;
880 }
881 break;
882 case AUDIO_STOP:
883 MM_DBG("AUDIO_STOP\n");
884 rc = audqcelp_disable(audio);
885 audio->stopped = 1;
886 audqcelp_ioport_reset(audio);
887 audio->stopped = 0;
888 break;
889 case AUDIO_FLUSH:
890 MM_DBG("AUDIO_FLUSH\n");
891 audio->rflush = 1;
892 audio->wflush = 1;
893 audqcelp_ioport_reset(audio);
894 if (audio->running) {
895 audpp_flush(audio->dec_id);
896 rc = wait_event_interruptible(audio->write_wait,
897 !audio->wflush);
898 if (rc < 0) {
899 MM_ERR("AUDIO_FLUSH interrupted\n");
900 rc = -EINTR;
901 }
902 } else {
903 audio->rflush = 0;
904 audio->wflush = 0;
905 }
906 break;
907 case AUDIO_SET_CONFIG:{
908 struct msm_audio_config config;
909 if (copy_from_user(&config, (void *)arg,
910 sizeof(config))) {
911 rc = -EFAULT;
912 break;
913 }
914 audio->mfield = config.meta_field;
915 MM_DBG("AUDIO_SET_CONFIG applicable \
916 for metafield configuration\n");
917 rc = 0;
918 break;
919 }
920 case AUDIO_GET_CONFIG:{
921 struct msm_audio_config config;
922 config.buffer_size = BUFSZ;
923 config.buffer_count = BUF_COUNT;
924 config.sample_rate = 8000;
925 config.channel_count = 1;
926 config.meta_field = 0;
927 config.unused[0] = 0;
928 config.unused[1] = 0;
929 config.unused[2] = 0;
930 if (copy_to_user((void *)arg, &config,
931 sizeof(config)))
932 rc = -EFAULT;
933 else
934 rc = 0;
935
936 break;
937 }
938 case AUDIO_GET_PCM_CONFIG:{
939 struct msm_audio_pcm_config config;
940
941 config.pcm_feedback = audio->pcm_feedback;
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
954 if (copy_from_user(&config, (void *)arg,
955 sizeof(config))) {
956 rc = -EFAULT;
957 break;
958 }
959 if (config.pcm_feedback != audio->pcm_feedback) {
960 MM_ERR("Not sufficient permission to"
961 "change the playback mode\n");
962 rc = -EACCES;
963 break;
964 }
965 if ((config.buffer_count > PCM_BUF_MAX_COUNT) ||
966 (config.buffer_count == 1))
967 config.buffer_count = PCM_BUF_MAX_COUNT;
968
969 if (config.buffer_size < PCM_BUFSZ_MIN)
970 config.buffer_size = PCM_BUFSZ_MIN;
971
972 /* Check if pcm feedback is required */
973 if ((config.pcm_feedback) && (!audio->read_data)) {
974 MM_DBG("allocate PCM buf %d\n",
975 config.buffer_count * config.buffer_size);
Santosh Mardifdc227a2011-07-11 17:20:34 +0530976 audio->read_phys =
977 allocate_contiguous_ebi_nomap(
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700978 config.buffer_size *
979 config.buffer_count,
Santosh Mardifdc227a2011-07-11 17:20:34 +0530980 SZ_4K);
981 if (!audio->read_phys) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700982 rc = -ENOMEM;
983 break;
984 }
Laura Abbott61399692012-04-30 14:25:46 -0700985 audio->map_v_read = ioremap(
Santosh Mardifdc227a2011-07-11 17:20:34 +0530986 audio->read_phys,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700987 config.buffer_size *
Laura Abbott61399692012-04-30 14:25:46 -0700988 config.buffer_count);
Santosh Mardifdc227a2011-07-11 17:20:34 +0530989 if (IS_ERR(audio->map_v_read)) {
990 MM_ERR("failed to map read buf\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700991 rc = -ENOMEM;
Santosh Mardifdc227a2011-07-11 17:20:34 +0530992 free_contiguous_memory_by_paddr(
993 audio->read_phys);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700994 } else {
995 uint8_t index;
996 uint32_t offset = 0;
Santosh Mardifdc227a2011-07-11 17:20:34 +0530997 audio->read_data =
Laura Abbott61399692012-04-30 14:25:46 -0700998 audio->map_v_read;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700999 audio->buf_refresh = 0;
1000 audio->pcm_buf_count =
1001 config.buffer_count;
1002 audio->read_next = 0;
1003 audio->fill_next = 0;
1004
1005 for (index = 0;
1006 index < config.buffer_count; index++) {
1007 audio->in[index].data =
1008 audio->read_data + offset;
1009 audio->in[index].addr =
1010 audio->read_phys + offset;
1011 audio->in[index].size =
1012 config.buffer_size;
1013 audio->in[index].used = 0;
1014 offset += config.buffer_size;
1015 }
1016 MM_DBG("read buf: phy addr 0x%08x \
1017 kernel addr 0x%08x\n",
1018 audio->read_phys,
1019 (int)audio->read_data);
1020 rc = 0;
1021 }
1022 } else {
1023 rc = 0;
1024 }
1025 break;
1026 }
1027 case AUDIO_PAUSE:
1028 MM_DBG("AUDIO_PAUSE %ld\n", arg);
1029 rc = audpp_pause(audio->dec_id, (int) arg);
1030 break;
1031 case AUDIO_GET_SESSION_ID:
1032 if (copy_to_user((void *) arg, &audio->dec_id,
1033 sizeof(unsigned short)))
1034 rc = -EFAULT;
1035 else
1036 rc = 0;
1037 break;
1038 default:
1039 rc = -EINVAL;
1040 }
1041 mutex_unlock(&audio->lock);
1042 return rc;
1043}
1044
1045/* Only useful in tunnel-mode */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001046static int audqcelp_fsync(struct file *file, loff_t ppos1, loff_t ppos2, int datasync)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001047{
1048 struct audio *audio = file->private_data;
1049 int rc = 0;
1050
1051 MM_DBG("\n"); /* Macro prints the file name and function */
1052 if (!audio->running || audio->pcm_feedback) {
1053 rc = -EINVAL;
1054 goto done_nolock;
1055 }
1056
1057 mutex_lock(&audio->write_lock);
1058
1059 rc = wait_event_interruptible(audio->write_wait,
1060 (!audio->out[0].used &&
1061 !audio->out[1].used &&
1062 audio->out_needed) || audio->wflush);
1063
1064 if (rc < 0)
1065 goto done;
1066 else if (audio->wflush) {
1067 rc = -EBUSY;
1068 goto done;
1069 }
1070
1071 /* pcm dmamiss message is sent continously
1072 * when decoder is starved so no race
1073 * condition concern
1074 */
1075 audio->teos = 0;
1076
1077 rc = wait_event_interruptible(audio->write_wait,
1078 audio->teos || audio->wflush);
1079
1080 if (audio->wflush)
1081 rc = -EBUSY;
1082
1083done:
1084 mutex_unlock(&audio->write_lock);
1085done_nolock:
1086 return rc;
1087}
1088
1089static ssize_t audqcelp_read(struct file *file, char __user *buf, size_t count,
1090 loff_t *pos)
1091{
1092 struct audio *audio = file->private_data;
1093 const char __user *start = buf;
1094 int rc = 0;
1095
1096 if (!audio->pcm_feedback)
1097 return 0; /* PCM feedback is not enabled. Nothing to read */
1098
1099 mutex_lock(&audio->read_lock);
1100 MM_DBG("%d\n", count);
1101 while (count > 0) {
1102 rc = wait_event_interruptible(audio->read_wait,
1103 (audio->in[audio->read_next].used > 0) ||
1104 (audio->stopped) || (audio->rflush));
1105 if (rc < 0)
1106 break;
1107
1108 if (audio->stopped || audio->rflush) {
1109 rc = -EBUSY;
1110 break;
1111 }
1112
1113 if (count < audio->in[audio->read_next].used) {
1114 /* Read must happen in frame boundary. Since driver does
1115 not know frame size, read count must be greater or equal
1116 to size of PCM samples */
1117 MM_DBG("read stop - partial frame\n");
1118 break;
1119 } else {
1120 MM_DBG("read from in[%d]\n", audio->read_next);
1121
1122 if (copy_to_user(buf,
1123 audio->in[audio->read_next].data,
1124 audio->in[audio->read_next].used)) {
1125 MM_ERR("invalid addr %x\n", (unsigned int)buf);
1126 rc = -EFAULT;
1127 break;
1128 }
1129 count -= audio->in[audio->read_next].used;
1130 buf += audio->in[audio->read_next].used;
1131 audio->in[audio->read_next].used = 0;
1132 if ((++audio->read_next) == audio->pcm_buf_count)
1133 audio->read_next = 0;
1134 break;
1135 /* Force to exit while loop
1136 * to prevent output thread
1137 * sleep too long if data is
1138 * not ready at this moment.
1139 */
1140 }
1141 }
1142
1143 /* don't feed output buffer to HW decoder during flushing
1144 * buffer refresh command will be sent once flush completes
1145 * send buf refresh command here can confuse HW decoder
1146 */
1147 if (audio->buf_refresh && !audio->rflush) {
1148 audio->buf_refresh = 0;
1149 MM_DBG("kick start pcm feedback again\n");
1150 audqcelp_buffer_refresh(audio);
1151 }
1152
1153 mutex_unlock(&audio->read_lock);
1154
1155 if (buf > start)
1156 rc = buf - start;
1157
1158 MM_DBG("read %d bytes\n", rc);
1159 return rc;
1160}
1161
1162static int audqcelp_process_eos(struct audio *audio,
1163 const char __user *buf_start, unsigned short mfield_size)
1164{
1165 struct buffer *frame;
1166 int rc = 0;
1167
1168 frame = audio->out + audio->out_head;
1169
1170 rc = wait_event_interruptible(audio->write_wait,
1171 (audio->out_needed &&
1172 audio->out[0].used == 0 &&
1173 audio->out[1].used == 0)
1174 || (audio->stopped)
1175 || (audio->wflush));
1176
1177 if (rc < 0)
1178 goto done;
1179 if (audio->stopped || audio->wflush) {
1180 rc = -EBUSY;
1181 goto done;
1182 }
1183
1184 if (copy_from_user(frame->data, buf_start, mfield_size)) {
1185 rc = -EFAULT;
1186 goto done;
1187 }
1188
1189 frame->mfield_sz = mfield_size;
1190 audio->out_head ^= 1;
1191 frame->used = mfield_size;
1192 audqcelp_send_data(audio, 0);
1193
1194done:
1195 return rc;
1196}
1197
1198static ssize_t audqcelp_write(struct file *file, const char __user *buf,
1199 size_t count, loff_t *pos)
1200{
1201 struct audio *audio = file->private_data;
1202 const char __user *start = buf;
1203 struct buffer *frame;
1204 size_t xfer;
1205 char *cpy_ptr;
1206 int rc = 0, eos_condition = AUDQCELP_EOS_NONE;
1207 unsigned short mfield_size = 0;
1208
1209 MM_DBG("cnt=%d\n", count);
1210
1211 if (count & 1)
1212 return -EINVAL;
1213
1214 mutex_lock(&audio->write_lock);
1215 while (count > 0) {
1216 frame = audio->out + audio->out_head;
1217 cpy_ptr = frame->data;
1218 rc = wait_event_interruptible(audio->write_wait,
1219 (frame->used == 0)
1220 || (audio->stopped)
1221 || (audio->wflush));
1222 MM_DBG("buffer available\n");
1223 if (rc < 0)
1224 break;
1225 if (audio->stopped || audio->wflush) {
1226 rc = -EBUSY;
1227 break;
1228 }
1229
1230 if (audio->mfield) {
1231 if (buf == start) {
1232 /* Processing beginning of user buffer */
1233 if (__get_user(mfield_size,
1234 (unsigned short __user *) buf)) {
1235 rc = -EFAULT;
1236 break;
1237 } else if (mfield_size > count) {
1238 rc = -EINVAL;
1239 break;
1240 }
1241 MM_DBG("mf offset_val %x\n", mfield_size);
1242 if (copy_from_user(cpy_ptr, buf, mfield_size)) {
1243 rc = -EFAULT;
1244 break;
1245 }
1246 /* Check if EOS flag is set and buffer has
1247 * contains just meta field
1248 */
1249 if (cpy_ptr[AUDQCELP_EOS_FLG_OFFSET] &
1250 AUDQCELP_EOS_FLG_MASK) {
1251 MM_DBG("EOS SET\n");
1252 eos_condition = AUDQCELP_EOS_SET;
1253 if (mfield_size == count) {
1254 buf += mfield_size;
1255 break;
1256 } else
1257 cpy_ptr[AUDQCELP_EOS_FLG_OFFSET] &=
1258 ~AUDQCELP_EOS_FLG_MASK;
1259 }
1260 cpy_ptr += mfield_size;
1261 count -= mfield_size;
1262 buf += mfield_size;
1263 } else {
1264 mfield_size = 0;
1265 MM_DBG("continuous buffer\n");
1266 }
1267 frame->mfield_sz = mfield_size;
1268 }
1269
1270 xfer = (count > (frame->size - mfield_size)) ?
1271 (frame->size - mfield_size) : count;
1272 if (copy_from_user(cpy_ptr, buf, xfer)) {
1273 rc = -EFAULT;
1274 break;
1275 }
1276
1277 frame->used = xfer + mfield_size;
1278 audio->out_head ^= 1;
1279 count -= xfer;
1280 buf += xfer;
1281 audqcelp_send_data(audio, 0);
1282 }
1283 if (eos_condition == AUDQCELP_EOS_SET)
1284 rc = audqcelp_process_eos(audio, start, mfield_size);
1285 mutex_unlock(&audio->write_lock);
1286 if (!rc) {
1287 if (buf > start)
1288 return buf - start;
1289 }
1290 return rc;
1291}
1292
1293static int audqcelp_release(struct inode *inode, struct file *file)
1294{
1295 struct audio *audio = file->private_data;
1296
1297 MM_INFO("audio instance 0x%08x freeing\n", (int) audio);
1298 mutex_lock(&audio->lock);
1299 auddev_unregister_evt_listner(AUDDEV_CLNT_DEC, audio->dec_id);
1300 audqcelp_disable(audio);
1301 audqcelp_flush(audio);
1302 audqcelp_flush_pcm_buf(audio);
1303 msm_adsp_put(audio->audplay);
1304 audpp_adec_free(audio->dec_id);
1305#ifdef CONFIG_HAS_EARLYSUSPEND
1306 unregister_early_suspend(&audio->suspend_ctl.node);
1307#endif
1308 audio->opened = 0;
1309 audio->event_abort = 1;
1310 wake_up(&audio->event_wait);
1311 audqcelp_reset_event_queue(audio);
Laura Abbott61399692012-04-30 14:25:46 -07001312 iounmap(audio->map_v_write);
Santosh Mardifdc227a2011-07-11 17:20:34 +05301313 free_contiguous_memory_by_paddr(audio->phys);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001314 if (audio->read_data) {
Laura Abbott61399692012-04-30 14:25:46 -07001315 iounmap(audio->map_v_read);
Santosh Mardifdc227a2011-07-11 17:20:34 +05301316 free_contiguous_memory_by_paddr(audio->read_phys);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001317 }
1318 mutex_unlock(&audio->lock);
1319#ifdef CONFIG_DEBUG_FS
1320 if (audio->dentry)
1321 debugfs_remove(audio->dentry);
1322#endif
1323 kfree(audio);
1324 return 0;
1325}
1326
Vinay Vaka051db722012-01-24 19:48:32 +05301327#ifdef CONFIG_HAS_EARLYSUSPEND
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001328static void audqcelp_post_event(struct audio *audio, int type,
1329 union msm_audio_event_payload payload)
1330{
1331 struct audqcelp_event *e_node = NULL;
1332 unsigned long flags;
1333
1334 spin_lock_irqsave(&audio->event_queue_lock, flags);
1335
1336 if (!list_empty(&audio->free_event_queue)) {
1337 e_node = list_first_entry(&audio->free_event_queue,
1338 struct audqcelp_event, list);
1339 list_del(&e_node->list);
1340 } else {
1341 e_node = kmalloc(sizeof(struct audqcelp_event), GFP_ATOMIC);
1342 if (!e_node) {
1343 MM_ERR("No mem to post event %d\n", type);
1344 return;
1345 }
1346 }
1347
1348 e_node->event_type = type;
1349 e_node->payload = payload;
1350
1351 list_add_tail(&e_node->list, &audio->event_queue);
1352 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
1353 wake_up(&audio->event_wait);
1354}
1355
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001356static void audqcelp_suspend(struct early_suspend *h)
1357{
1358 struct audqcelp_suspend_ctl *ctl =
1359 container_of(h, struct audqcelp_suspend_ctl, node);
1360 union msm_audio_event_payload payload;
1361
1362 MM_DBG("\n"); /* Macro prints the file name and function */
1363 audqcelp_post_event(ctl->audio, AUDIO_EVENT_SUSPEND, payload);
1364}
1365
1366static void audqcelp_resume(struct early_suspend *h)
1367{
1368 struct audqcelp_suspend_ctl *ctl =
1369 container_of(h, struct audqcelp_suspend_ctl, node);
1370 union msm_audio_event_payload payload;
1371
1372 MM_DBG("\n"); /* Macro prints the file name and function */
1373 audqcelp_post_event(ctl->audio, AUDIO_EVENT_RESUME, payload);
1374}
1375#endif
1376
1377#ifdef CONFIG_DEBUG_FS
1378static ssize_t audqcelp_debug_open(struct inode *inode, struct file *file)
1379{
1380 file->private_data = inode->i_private;
1381 return 0;
1382}
1383
1384static ssize_t audqcelp_debug_read(struct file *file, char __user *buf,
1385 size_t count, loff_t *ppos)
1386{
1387 const int debug_bufmax = 1024;
1388 static char buffer[1024];
1389 int n = 0, i;
1390 struct audio *audio = file->private_data;
1391
1392 mutex_lock(&audio->lock);
1393 n = scnprintf(buffer, debug_bufmax, "opened %d\n", audio->opened);
1394 n += scnprintf(buffer + n, debug_bufmax - n,
1395 "enabled %d\n", audio->enabled);
1396 n += scnprintf(buffer + n, debug_bufmax - n,
1397 "stopped %d\n", audio->stopped);
1398 n += scnprintf(buffer + n, debug_bufmax - n,
1399 "pcm_feedback %d\n", audio->pcm_feedback);
1400 n += scnprintf(buffer + n, debug_bufmax - n,
1401 "out_buf_sz %d\n", audio->out[0].size);
1402 n += scnprintf(buffer + n, debug_bufmax - n,
1403 "pcm_buf_count %d \n", audio->pcm_buf_count);
1404 n += scnprintf(buffer + n, debug_bufmax - n,
1405 "pcm_buf_sz %d \n", audio->in[0].size);
1406 n += scnprintf(buffer + n, debug_bufmax - n,
1407 "volume %x \n", audio->vol_pan.volume);
1408 mutex_unlock(&audio->lock);
1409 /* Following variables are only useful for debugging when
1410 * when playback halts unexpectedly. Thus, no mutual exclusion
1411 * enforced
1412 */
1413 n += scnprintf(buffer + n, debug_bufmax - n,
1414 "wflush %d\n", audio->wflush);
1415 n += scnprintf(buffer + n, debug_bufmax - n,
1416 "rflush %d\n", audio->rflush);
1417 n += scnprintf(buffer + n, debug_bufmax - n,
1418 "running %d \n", audio->running);
1419 n += scnprintf(buffer + n, debug_bufmax - n,
1420 "dec state %d \n", audio->dec_state);
1421 n += scnprintf(buffer + n, debug_bufmax - n,
1422 "out_needed %d \n", audio->out_needed);
1423 n += scnprintf(buffer + n, debug_bufmax - n,
1424 "out_head %d \n", audio->out_head);
1425 n += scnprintf(buffer + n, debug_bufmax - n,
1426 "out_tail %d \n", audio->out_tail);
1427 n += scnprintf(buffer + n, debug_bufmax - n,
1428 "out[0].used %d \n", audio->out[0].used);
1429 n += scnprintf(buffer + n, debug_bufmax - n,
1430 "out[1].used %d \n", audio->out[1].used);
1431 n += scnprintf(buffer + n, debug_bufmax - n,
1432 "buffer_refresh %d \n", audio->buf_refresh);
1433 n += scnprintf(buffer + n, debug_bufmax - n,
1434 "read_next %d \n", audio->read_next);
1435 n += scnprintf(buffer + n, debug_bufmax - n,
1436 "fill_next %d \n", audio->fill_next);
1437 for (i = 0; i < audio->pcm_buf_count; i++)
1438 n += scnprintf(buffer + n, debug_bufmax - n,
1439 "in[%d].size %d \n", i, audio->in[i].used);
1440 buffer[n] = 0;
1441 return simple_read_from_buffer(buf, count, ppos, buffer, n);
1442}
1443
1444static const struct file_operations audqcelp_debug_fops = {
1445 .read = audqcelp_debug_read,
1446 .open = audqcelp_debug_open,
1447};
1448#endif
1449
1450static int audqcelp_open(struct inode *inode, struct file *file)
1451{
1452 struct audio *audio = NULL;
1453 int rc, dec_attrb, decid, i;
1454 struct audqcelp_event *e_node = NULL;
1455#ifdef CONFIG_DEBUG_FS
1456 /* 4 bytes represents decoder number, 1 byte for terminate string */
1457 char name[sizeof "msm_qcelp_" + 5];
1458#endif
1459
1460 /* Create audio instance, set to zero */
1461 audio = kzalloc(sizeof(struct audio), GFP_KERNEL);
1462 if (!audio) {
1463 MM_ERR("no memory to allocate audio instance\n");
1464 rc = -ENOMEM;
1465 goto done;
1466 }
1467 MM_INFO("audio instance 0x%08x created\n", (int)audio);
1468
1469 /* Allocate the decoder */
1470 dec_attrb = AUDDEC_DEC_QCELP;
1471 if ((file->f_mode & FMODE_WRITE) &&
1472 (file->f_mode & FMODE_READ)) {
1473 dec_attrb |= MSM_AUD_MODE_NONTUNNEL;
1474 audio->pcm_feedback = NON_TUNNEL_MODE_PLAYBACK;
1475 } else if ((file->f_mode & FMODE_WRITE) &&
1476 !(file->f_mode & FMODE_READ)) {
1477 dec_attrb |= MSM_AUD_MODE_TUNNEL;
1478 audio->pcm_feedback = TUNNEL_MODE_PLAYBACK;
1479 } else {
1480 kfree(audio);
1481 rc = -EACCES;
1482 goto done;
1483 }
1484 decid = audpp_adec_alloc(dec_attrb, &audio->module_name,
1485 &audio->queue_id);
1486 if (decid < 0) {
1487 MM_ERR("No free decoder available, freeing instance 0x%08x\n",
1488 (int)audio);
1489 rc = -ENODEV;
1490 kfree(audio);
1491 goto done;
1492 }
1493 audio->dec_id = decid & MSM_AUD_DECODER_MASK;
1494
Santosh Mardifdc227a2011-07-11 17:20:34 +05301495 audio->phys = allocate_contiguous_ebi_nomap(DMASZ, SZ_4K);
1496 if (!audio->phys) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001497 MM_ERR("could not allocate write buffers, freeing instance \
1498 0x%08x\n", (int)audio);
1499 rc = -ENOMEM;
1500 audpp_adec_free(audio->dec_id);
1501 kfree(audio);
1502 goto done;
1503 } else {
Laura Abbott61399692012-04-30 14:25:46 -07001504 audio->map_v_write = ioremap(audio->phys, DMASZ);
Santosh Mardifdc227a2011-07-11 17:20:34 +05301505 if (IS_ERR(audio->map_v_write)) {
1506 MM_ERR("could not map write phys address, freeing \
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001507 instance 0x%08x\n", (int)audio);
1508 rc = -ENOMEM;
Santosh Mardifdc227a2011-07-11 17:20:34 +05301509 free_contiguous_memory_by_paddr(audio->phys);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001510 audpp_adec_free(audio->dec_id);
1511 kfree(audio);
1512 goto done;
1513 }
Laura Abbott61399692012-04-30 14:25:46 -07001514 audio->data = audio->map_v_write;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001515 MM_DBG("write buf: phy addr 0x%08x kernel addr 0x%08x\n",
1516 audio->phys, (int)audio->data);
1517 }
1518
1519 rc = msm_adsp_get(audio->module_name, &audio->audplay,
1520 &audplay_adsp_ops_qcelp, audio);
1521 if (rc) {
1522 MM_ERR("failed to get %s module, freeing instance 0x%08x\n",
1523 audio->module_name, (int)audio);
1524 goto err;
1525 }
1526
1527 /* Initialize all locks of audio instance */
1528 mutex_init(&audio->lock);
1529 mutex_init(&audio->write_lock);
1530 mutex_init(&audio->read_lock);
1531 mutex_init(&audio->get_event_lock);
1532 spin_lock_init(&audio->dsp_lock);
1533 init_waitqueue_head(&audio->write_wait);
1534 init_waitqueue_head(&audio->read_wait);
1535 INIT_LIST_HEAD(&audio->free_event_queue);
1536 INIT_LIST_HEAD(&audio->event_queue);
1537 init_waitqueue_head(&audio->wait);
1538 init_waitqueue_head(&audio->event_wait);
1539 spin_lock_init(&audio->event_queue_lock);
1540 init_waitqueue_head(&audio->avsync_wait);
1541
1542 /* Initialize buffer */
1543 audio->out[0].data = audio->data + 0;
1544 audio->out[0].addr = audio->phys + 0;
1545 audio->out[0].size = BUFSZ;
1546
1547 audio->out[1].data = audio->data + BUFSZ;
1548 audio->out[1].addr = audio->phys + BUFSZ;
1549 audio->out[1].size = BUFSZ;
1550
1551 audio->vol_pan.volume = 0x2000;
1552
1553 audqcelp_flush(audio);
1554
1555 file->private_data = audio;
1556 audio->opened = 1;
1557
1558 audio->device_events = AUDDEV_EVT_DEV_RDY
1559 |AUDDEV_EVT_DEV_RLS|
1560 AUDDEV_EVT_STREAM_VOL_CHG;
1561
1562 rc = auddev_register_evt_listner(audio->device_events,
1563 AUDDEV_CLNT_DEC,
1564 audio->dec_id,
1565 qcelp_listner,
1566 (void *)audio);
1567 if (rc) {
1568 MM_ERR("%s: failed to register listnet\n", __func__);
1569 goto event_err;
1570 }
1571
1572#ifdef CONFIG_DEBUG_FS
1573 snprintf(name, sizeof name, "msm_qcelp_%04x", audio->dec_id);
1574 audio->dentry = debugfs_create_file(name, S_IFREG | S_IRUGO,
1575 NULL, (void *) audio, &audqcelp_debug_fops);
1576
1577 if (IS_ERR(audio->dentry))
1578 MM_DBG("debugfs_create_file failed\n");
1579#endif
1580#ifdef CONFIG_HAS_EARLYSUSPEND
1581 audio->suspend_ctl.node.level = EARLY_SUSPEND_LEVEL_DISABLE_FB;
1582 audio->suspend_ctl.node.resume = audqcelp_resume;
1583 audio->suspend_ctl.node.suspend = audqcelp_suspend;
1584 audio->suspend_ctl.audio = audio;
1585 register_early_suspend(&audio->suspend_ctl.node);
1586#endif
1587 for (i = 0; i < AUDQCELP_EVENT_NUM; i++) {
1588 e_node = kmalloc(sizeof(struct audqcelp_event), GFP_KERNEL);
1589 if (e_node)
1590 list_add_tail(&e_node->list, &audio->free_event_queue);
1591 else {
1592 MM_ERR("event pkt alloc failed\n");
1593 break;
1594 }
1595 }
1596done:
1597 return rc;
1598event_err:
1599 msm_adsp_put(audio->audplay);
1600err:
Laura Abbott61399692012-04-30 14:25:46 -07001601 iounmap(audio->map_v_write);
Santosh Mardifdc227a2011-07-11 17:20:34 +05301602 free_contiguous_memory_by_paddr(audio->phys);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001603 audpp_adec_free(audio->dec_id);
1604 kfree(audio);
1605 return rc;
1606}
1607
1608static const struct file_operations audio_qcelp_fops = {
1609 .owner = THIS_MODULE,
1610 .open = audqcelp_open,
1611 .release = audqcelp_release,
1612 .read = audqcelp_read,
1613 .write = audqcelp_write,
1614 .unlocked_ioctl = audqcelp_ioctl,
1615 .fsync = audqcelp_fsync,
1616};
1617
1618struct miscdevice audio_qcelp_misc = {
1619 .minor = MISC_DYNAMIC_MINOR,
1620 .name = "msm_qcelp",
1621 .fops = &audio_qcelp_fops,
1622};
1623
1624static int __init audqcelp_init(void)
1625{
1626 return misc_register(&audio_qcelp_misc);
1627}
1628
1629static void __exit audqcelp_exit(void)
1630{
1631 misc_deregister(&audio_qcelp_misc);
1632}
1633
1634module_init(audqcelp_init);
1635module_exit(audqcelp_exit);
1636
1637MODULE_DESCRIPTION("MSM QCELP 13K driver");
1638MODULE_LICENSE("GPL v2");