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