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