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