blob: d9b384a8258c040c124baa8d0eb4ff4ba0aa8528 [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
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05307 * Copyright (c) 2009-2011, 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>
55#include <mach/msm_subsystem_map.h>
56
57#include "audmgr.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070058
59/* Size must be power of 2 */
60#define BUFSZ_MAX 8206 /* Includes meta in size */
61#define BUFSZ_MIN 2062 /* Includes meta in size */
62#define DMASZ_MAX (BUFSZ_MAX * 2)
63#define DMASZ_MIN (BUFSZ_MIN * 2)
64
65#define AUDPLAY_INVALID_READ_PTR_OFFSET 0xFFFF
66#define AUDDEC_DEC_WMAPRO 13
67
68#define PCM_BUFSZ_MIN 8216 /* Hold one stereo WMAPRO frame and meta out*/
69#define PCM_BUF_MAX_COUNT 5 /* DSP only accepts 5 buffers at most
70 but support 2 buffers currently */
71#define ROUTING_MODE_FTRT 1
72#define ROUTING_MODE_RT 2
73/* Decoder status received from AUDPPTASK */
74#define AUDPP_DEC_STATUS_SLEEP 0
75#define AUDPP_DEC_STATUS_INIT 1
76#define AUDPP_DEC_STATUS_CFG 2
77#define AUDPP_DEC_STATUS_PLAY 3
78
79#define AUDWMAPRO_METAFIELD_MASK 0xFFFF0000
80#define AUDWMAPRO_EOS_FLG_OFFSET 0x0A /* Offset from beginning of buffer */
81#define AUDWMAPRO_EOS_FLG_MASK 0x01
82#define AUDWMAPRO_EOS_NONE 0x0 /* No EOS detected */
83#define AUDWMAPRO_EOS_SET 0x1 /* EOS set in meta field */
84
85#define AUDWMAPRO_EVENT_NUM 10 /* Default no. of pre-allocated event packets */
86
87struct buffer {
88 void *data;
89 unsigned size;
90 unsigned used; /* Input usage actual DSP produced PCM size */
91 unsigned addr;
92 unsigned short mfield_sz; /*only useful for data has meta field */
93};
94
95#ifdef CONFIG_HAS_EARLYSUSPEND
96struct audwmapro_suspend_ctl {
97 struct early_suspend node;
98 struct audio *audio;
99};
100#endif
101
102struct audwmapro_event{
103 struct list_head list;
104 int event_type;
105 union msm_audio_event_payload payload;
106};
107
108struct audio {
109 struct buffer out[2];
110
111 spinlock_t dsp_lock;
112
113 uint8_t out_head;
114 uint8_t out_tail;
115 uint8_t out_needed; /* number of buffers the dsp is waiting for */
116 unsigned out_dma_sz;
117
118 atomic_t out_bytes;
119
120 struct mutex lock;
121 struct mutex write_lock;
122 wait_queue_head_t write_wait;
123
124 /* Host PCM section */
125 struct buffer in[PCM_BUF_MAX_COUNT];
126 struct mutex read_lock;
127 wait_queue_head_t read_wait; /* Wait queue for read */
128 char *read_data; /* pointer to reader buffer */
129 int32_t read_phys; /* physical address of reader buffer */
130 uint8_t read_next; /* index to input buffers to be read next */
131 uint8_t fill_next; /* index to buffer that DSP should be filling */
132 uint8_t pcm_buf_count; /* number of pcm buffer allocated */
133 /* ---- End of Host PCM section */
134
135 struct msm_adsp_module *audplay;
136
137 /* configuration to use on next enable */
138 uint32_t out_sample_rate;
139 uint32_t out_channel_mode;
140
141 struct msm_audio_wmapro_config wmapro_config;
142 struct audmgr audmgr;
143
144 /* data allocated for various buffers */
145 char *data;
146 int32_t phys; /* physical address of write buffer */
Santosh Mardi0be3b8e2011-07-06 10:00:21 +0530147 struct msm_mapped_buffer *map_v_read;
148 struct msm_mapped_buffer *map_v_write;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700149
150 int mfield; /* meta field embedded in data */
151 int rflush; /* Read flush */
152 int wflush; /* Write flush */
153 int opened;
154 int enabled;
155 int running;
156 int stopped; /* set when stopped, cleared on flush */
157 int pcm_feedback;
158 int buf_refresh;
159 int rmt_resource_released;
160 int teos; /* valid only if tunnel mode & no data left for decoder */
161 enum msm_aud_decoder_state dec_state; /* Represents decoder state */
162 int reserved; /* A byte is being reserved */
163 char rsv_byte; /* Handle odd length user data */
164
165 const char *module_name;
166 unsigned queue_id;
167 uint16_t dec_id;
168 uint32_t read_ptr_offset;
169
170#ifdef CONFIG_HAS_EARLYSUSPEND
171 struct audwmapro_suspend_ctl suspend_ctl;
172#endif
173
174#ifdef CONFIG_DEBUG_FS
175 struct dentry *dentry;
176#endif
177
178 wait_queue_head_t wait;
179 struct list_head free_event_queue;
180 struct list_head event_queue;
181 wait_queue_head_t event_wait;
182 spinlock_t event_queue_lock;
183 struct mutex get_event_lock;
184 int event_abort;
185
186 int eq_enable;
187 int eq_needs_commit;
188 audpp_cmd_cfg_object_params_eqalizer eq;
189 audpp_cmd_cfg_object_params_volume vol_pan;
190};
191
192static int auddec_dsp_config(struct audio *audio, int enable);
193static void audpp_cmd_cfg_adec_params(struct audio *audio);
194static void audpp_cmd_cfg_routing_mode(struct audio *audio);
195static void audplay_send_data(struct audio *audio, unsigned needed);
196static void audplay_config_hostpcm(struct audio *audio);
197static void audplay_buffer_refresh(struct audio *audio);
198static void audio_dsp_event(void *private, unsigned id, uint16_t *msg);
199#ifdef CONFIG_HAS_EARLYSUSPEND
200static void audwmapro_post_event(struct audio *audio, int type,
201 union msm_audio_event_payload payload);
202#endif
203
204static int rmt_put_resource(struct audio *audio)
205{
206 struct aud_codec_config_cmd cmd;
207 unsigned short client_idx;
208
209 cmd.cmd_id = RM_CMD_AUD_CODEC_CFG;
210 cmd.client_id = RM_AUD_CLIENT_ID;
211 cmd.task_id = audio->dec_id;
212 cmd.enable = RMT_DISABLE;
213 cmd.dec_type = AUDDEC_DEC_WMAPRO;
214 client_idx = ((cmd.client_id << 8) | cmd.task_id);
215
216 return put_adsp_resource(client_idx, &cmd, sizeof(cmd));
217}
218
219static int rmt_get_resource(struct audio *audio)
220{
221 struct aud_codec_config_cmd cmd;
222 unsigned short client_idx;
223
224 cmd.cmd_id = RM_CMD_AUD_CODEC_CFG;
225 cmd.client_id = RM_AUD_CLIENT_ID;
226 cmd.task_id = audio->dec_id;
227 cmd.enable = RMT_ENABLE;
228 cmd.dec_type = AUDDEC_DEC_WMAPRO;
229 client_idx = ((cmd.client_id << 8) | cmd.task_id);
230
231 return get_adsp_resource(client_idx, &cmd, sizeof(cmd));
232}
233
234/* must be called with audio->lock held */
235static int audio_enable(struct audio *audio)
236{
237 struct audmgr_config cfg;
238 int rc;
239
240 MM_DBG("\n"); /* Macro prints the file name and function */
241 if (audio->enabled)
242 return 0;
243
244 if (audio->rmt_resource_released == 1) {
245 audio->rmt_resource_released = 0;
246 rc = rmt_get_resource(audio);
247 if (rc) {
248 MM_ERR("ADSP resources are not available for WMAPRO \
249 session 0x%08x on decoder: %d\n Ignoring \
250 error and going ahead with the playback\n",
251 (int)audio, audio->dec_id);
252 }
253 }
254
255 audio->dec_state = MSM_AUD_DECODER_STATE_NONE;
256 audio->out_tail = 0;
257 audio->out_needed = 0;
258
259 cfg.tx_rate = RPC_AUD_DEF_SAMPLE_RATE_NONE;
260 cfg.rx_rate = RPC_AUD_DEF_SAMPLE_RATE_48000;
261 cfg.def_method = RPC_AUD_DEF_METHOD_PLAYBACK;
262 cfg.codec = RPC_AUD_DEF_CODEC_WMA;
263 cfg.snd_method = RPC_SND_METHOD_MIDI;
264
265 rc = audmgr_enable(&audio->audmgr, &cfg);
266 if (rc < 0)
267 return rc;
268
269 if (msm_adsp_enable(audio->audplay)) {
270 MM_ERR("msm_adsp_enable(audplay) failed\n");
271 audmgr_disable(&audio->audmgr);
272 return -ENODEV;
273 }
274
275 if (audpp_enable(audio->dec_id, audio_dsp_event, audio)) {
276 MM_ERR("audpp_enable() failed\n");
277 msm_adsp_disable(audio->audplay);
278 audmgr_disable(&audio->audmgr);
279 return -ENODEV;
280 }
281
282 audio->enabled = 1;
283 return 0;
284}
285
286/* must be called with audio->lock held */
287static int audio_disable(struct audio *audio)
288{
289 int rc = 0;
290 MM_DBG("\n"); /* Macro prints the file name and function */
291 if (audio->enabled) {
292 audio->enabled = 0;
293 audio->dec_state = MSM_AUD_DECODER_STATE_NONE;
294 auddec_dsp_config(audio, 0);
295 rc = wait_event_interruptible_timeout(audio->wait,
296 audio->dec_state != MSM_AUD_DECODER_STATE_NONE,
297 msecs_to_jiffies(MSM_AUD_DECODER_WAIT_MS));
298 if (rc == 0)
299 rc = -ETIMEDOUT;
300 else if (audio->dec_state != MSM_AUD_DECODER_STATE_CLOSE)
301 rc = -EFAULT;
302 else
303 rc = 0;
304 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{
651 audio->out[0].used = 0;
652 audio->out[1].used = 0;
653 audio->out_head = 0;
654 audio->out_tail = 0;
655 audio->reserved = 0;
656 atomic_set(&audio->out_bytes, 0);
657}
658
659static void audio_flush_pcm_buf(struct audio *audio)
660{
661 uint8_t index;
662
663 for (index = 0; index < PCM_BUF_MAX_COUNT; index++)
664 audio->in[index].used = 0;
665 audio->buf_refresh = 0;
666 audio->read_next = 0;
667 audio->fill_next = 0;
668}
669
670static void audio_ioport_reset(struct audio *audio)
671{
672 /* Make sure read/write thread are free from
673 * sleep and knowing that system is not able
674 * to process io request at the moment
675 */
676 wake_up(&audio->write_wait);
677 mutex_lock(&audio->write_lock);
678 audio_flush(audio);
679 mutex_unlock(&audio->write_lock);
680 wake_up(&audio->read_wait);
681 mutex_lock(&audio->read_lock);
682 audio_flush_pcm_buf(audio);
683 mutex_unlock(&audio->read_lock);
684}
685
686static int audwmapro_events_pending(struct audio *audio)
687{
688 unsigned long flags;
689 int empty;
690
691 spin_lock_irqsave(&audio->event_queue_lock, flags);
692 empty = !list_empty(&audio->event_queue);
693 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
694 return empty || audio->event_abort;
695}
696
697static void audwmapro_reset_event_queue(struct audio *audio)
698{
699 unsigned long flags;
700 struct audwmapro_event *drv_evt;
701 struct list_head *ptr, *next;
702
703 spin_lock_irqsave(&audio->event_queue_lock, flags);
704 list_for_each_safe(ptr, next, &audio->event_queue) {
705 drv_evt = list_first_entry(&audio->event_queue,
706 struct audwmapro_event, list);
707 list_del(&drv_evt->list);
708 kfree(drv_evt);
709 }
710 list_for_each_safe(ptr, next, &audio->free_event_queue) {
711 drv_evt = list_first_entry(&audio->free_event_queue,
712 struct audwmapro_event, list);
713 list_del(&drv_evt->list);
714 kfree(drv_evt);
715 }
716 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
717
718 return;
719}
720
721static long audwmapro_process_event_req(struct audio *audio, void __user *arg)
722{
723 long rc;
724 struct msm_audio_event usr_evt;
725 struct audwmapro_event *drv_evt = NULL;
726 int timeout;
727 unsigned long flags;
728
729 if (copy_from_user(&usr_evt, arg, sizeof(struct msm_audio_event)))
730 return -EFAULT;
731
732 timeout = (int) usr_evt.timeout_ms;
733
734 if (timeout > 0) {
735 rc = wait_event_interruptible_timeout(audio->event_wait,
736 audwmapro_events_pending(audio),
737 msecs_to_jiffies(timeout));
738 if (rc == 0)
739 return -ETIMEDOUT;
740 } else {
741 rc = wait_event_interruptible(
742 audio->event_wait, audwmapro_events_pending(audio));
743 }
744
745 if (rc < 0)
746 return rc;
747
748 if (audio->event_abort) {
749 audio->event_abort = 0;
750 return -ENODEV;
751 }
752
753 rc = 0;
754
755 spin_lock_irqsave(&audio->event_queue_lock, flags);
756 if (!list_empty(&audio->event_queue)) {
757 drv_evt = list_first_entry(&audio->event_queue,
758 struct audwmapro_event, list);
759 list_del(&drv_evt->list);
760 }
761
762 if (drv_evt) {
763 usr_evt.event_type = drv_evt->event_type;
764 usr_evt.event_payload = drv_evt->payload;
765 list_add_tail(&drv_evt->list, &audio->free_event_queue);
766 } else
767 rc = -1;
768 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
769
770 if (!rc && copy_to_user(arg, &usr_evt, sizeof(usr_evt)))
771 rc = -EFAULT;
772
773 return rc;
774}
775
776static int audio_enable_eq(struct audio *audio, int enable)
777{
778 if (audio->eq_enable == enable && !audio->eq_needs_commit)
779 return 0;
780
781 audio->eq_enable = enable;
782
783 if (audio->running) {
784 audpp_dsp_set_eq(audio->dec_id, enable, &audio->eq);
785 audio->eq_needs_commit = 0;
786 }
787 return 0;
788}
789
790static long audio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
791{
792 struct audio *audio = file->private_data;
793 int rc = -EINVAL;
794 unsigned long flags = 0;
795 uint16_t enable_mask;
796 int enable;
797 int prev_state;
798
799 MM_DBG("cmd = %d\n", cmd);
800
801 if (cmd == AUDIO_GET_STATS) {
802 struct msm_audio_stats stats;
803 stats.byte_count = audpp_avsync_byte_count(audio->dec_id);
804 stats.sample_count = audpp_avsync_sample_count(audio->dec_id);
805 if (copy_to_user((void *)arg, &stats, sizeof(stats)))
806 return -EFAULT;
807 return 0;
808 }
809
810 switch (cmd) {
811 case AUDIO_ENABLE_AUDPP:
812 if (copy_from_user(&enable_mask, (void *) arg,
813 sizeof(enable_mask))) {
814 rc = -EFAULT;
815 break;
816 }
817
818 spin_lock_irqsave(&audio->dsp_lock, flags);
819 enable = (enable_mask & EQ_ENABLE) ? 1 : 0;
820 audio_enable_eq(audio, enable);
821 spin_unlock_irqrestore(&audio->dsp_lock, flags);
822 rc = 0;
823 break;
824 case AUDIO_SET_VOLUME:
825 spin_lock_irqsave(&audio->dsp_lock, flags);
826 audio->vol_pan.volume = arg;
827 if (audio->running)
828 audpp_dsp_set_vol_pan(audio->dec_id, &audio->vol_pan);
829 spin_unlock_irqrestore(&audio->dsp_lock, flags);
830 rc = 0;
831 break;
832
833 case AUDIO_SET_PAN:
834 spin_lock_irqsave(&audio->dsp_lock, flags);
835 audio->vol_pan.pan = arg;
836 if (audio->running)
837 audpp_dsp_set_vol_pan(audio->dec_id, &audio->vol_pan);
838 spin_unlock_irqrestore(&audio->dsp_lock, flags);
839 rc = 0;
840 break;
841
842 case AUDIO_SET_EQ:
843 prev_state = audio->eq_enable;
844 audio->eq_enable = 0;
845 if (copy_from_user(&audio->eq.num_bands, (void *) arg,
846 sizeof(audio->eq) -
847 (AUDPP_CMD_CFG_OBJECT_PARAMS_COMMON_LEN + 2))) {
848 rc = -EFAULT;
849 break;
850 }
851 audio->eq_enable = prev_state;
852 audio->eq_needs_commit = 1;
853 rc = 0;
854 break;
855 }
856
857 if (-EINVAL != rc)
858 return rc;
859
860 if (cmd == AUDIO_GET_EVENT) {
861 MM_DBG("AUDIO_GET_EVENT\n");
862 if (mutex_trylock(&audio->get_event_lock)) {
863 rc = audwmapro_process_event_req(audio,
864 (void __user *) arg);
865 mutex_unlock(&audio->get_event_lock);
866 } else
867 rc = -EBUSY;
868 return rc;
869 }
870
871 if (cmd == AUDIO_ABORT_GET_EVENT) {
872 audio->event_abort = 1;
873 wake_up(&audio->event_wait);
874 return 0;
875 }
876
877 mutex_lock(&audio->lock);
878 switch (cmd) {
879 case AUDIO_START:
880 MM_DBG("AUDIO_START\n");
881 rc = audio_enable(audio);
882 if (!rc) {
883 rc = wait_event_interruptible_timeout(audio->wait,
884 audio->dec_state != MSM_AUD_DECODER_STATE_NONE,
885 msecs_to_jiffies(MSM_AUD_DECODER_WAIT_MS));
886 MM_INFO("dec_state %d rc = %d\n", audio->dec_state, rc);
887
888 if (audio->dec_state != MSM_AUD_DECODER_STATE_SUCCESS)
889 rc = -ENODEV;
890 else
891 rc = 0;
892 }
893 break;
894 case AUDIO_STOP:
895 MM_DBG("AUDIO_STOP\n");
896 rc = audio_disable(audio);
897 audio->stopped = 1;
898 audio_ioport_reset(audio);
899 audio->stopped = 0;
900 break;
901 case AUDIO_FLUSH:
902 MM_DBG("AUDIO_FLUSH\n");
903 audio->rflush = 1;
904 audio->wflush = 1;
905 audio_ioport_reset(audio);
906 if (audio->running) {
907 audpp_flush(audio->dec_id);
908 rc = wait_event_interruptible(audio->write_wait,
909 !audio->wflush);
910 if (rc < 0) {
911 MM_ERR("AUDIO_FLUSH interrupted\n");
912 rc = -EINTR;
913 }
914 } else {
915 audio->rflush = 0;
916 audio->wflush = 0;
917 }
918 break;
919 case AUDIO_SET_CONFIG: {
920 struct msm_audio_config config;
921 if (copy_from_user(&config, (void *) arg, sizeof(config))) {
922 rc = -EFAULT;
923 break;
924 }
925 if (config.channel_count == 1) {
926 config.channel_count = AUDPP_CMD_PCM_INTF_MONO_V;
927 } else if (config.channel_count == 2) {
928 config.channel_count = AUDPP_CMD_PCM_INTF_STEREO_V;
929 } else {
930 rc = -EINVAL;
931 break;
932 }
933 audio->mfield = config.meta_field;
934 audio->out_sample_rate = config.sample_rate;
935 audio->out_channel_mode = config.channel_count;
936 rc = 0;
937 break;
938 }
939 case AUDIO_GET_CONFIG: {
940 struct msm_audio_config config;
941 config.buffer_size = (audio->out_dma_sz >> 1);
942 config.buffer_count = 2;
943 config.sample_rate = audio->out_sample_rate;
944 if (audio->out_channel_mode == AUDPP_CMD_PCM_INTF_MONO_V)
945 config.channel_count = 1;
946 else
947 config.channel_count = 2;
948 config.meta_field = 0;
949 config.unused[0] = 0;
950 config.unused[1] = 0;
951 config.unused[2] = 0;
952 if (copy_to_user((void *) arg, &config, sizeof(config)))
953 rc = -EFAULT;
954 else
955 rc = 0;
956
957 break;
958 }
959 case AUDIO_GET_WMAPRO_CONFIG:{
960 if (copy_to_user((void *)arg, &audio->wmapro_config,
961 sizeof(audio->wmapro_config)))
962 rc = -EFAULT;
963 else
964 rc = 0;
965 break;
966 }
967 case AUDIO_SET_WMAPRO_CONFIG:{
968 struct msm_audio_wmapro_config usr_config;
969
970 if (copy_from_user
971 (&usr_config, (void *)arg,
972 sizeof(usr_config))) {
973 rc = -EFAULT;
974 break;
975 }
976
977 audio->wmapro_config = usr_config;
978
979 /* Need to swap the first and last words of advancedencodeopt2
980 * as DSP cannot read 32-bit variable at a time. Need to be
981 * split into two 16-bit and swap them as required by DSP */
982
983 audio->wmapro_config.advancedencodeopt2 =
984 ((audio->wmapro_config.advancedencodeopt2 & 0xFFFF0000)
985 >> 16) | ((audio->wmapro_config.advancedencodeopt2
986 << 16) & 0xFFFF0000);
987 rc = 0;
988 break;
989 }
990 case AUDIO_GET_PCM_CONFIG:{
991 struct msm_audio_pcm_config config;
992 config.pcm_feedback = audio->pcm_feedback;
993 config.buffer_count = PCM_BUF_MAX_COUNT;
994 config.buffer_size = PCM_BUFSZ_MIN;
995 if (copy_to_user((void *)arg, &config,
996 sizeof(config)))
997 rc = -EFAULT;
998 else
999 rc = 0;
1000 break;
1001 }
1002 case AUDIO_SET_PCM_CONFIG:{
1003 struct msm_audio_pcm_config config;
1004 if (copy_from_user
1005 (&config, (void *)arg, sizeof(config))) {
1006 rc = -EFAULT;
1007 break;
1008 }
1009 if (config.pcm_feedback != audio->pcm_feedback) {
1010 MM_ERR("Not sufficient permission to"
1011 "change the playback mode\n");
1012 rc = -EACCES;
1013 break;
1014 }
1015 if ((config.buffer_count > PCM_BUF_MAX_COUNT) ||
1016 (config.buffer_count == 1))
1017 config.buffer_count = PCM_BUF_MAX_COUNT;
1018
1019 if (config.buffer_size < PCM_BUFSZ_MIN)
1020 config.buffer_size = PCM_BUFSZ_MIN;
1021
1022 /* Check if pcm feedback is required */
1023 if ((config.pcm_feedback) && (!audio->read_data)) {
1024 MM_DBG("allocate PCM buffer %d\n",
1025 config.buffer_count *
1026 config.buffer_size);
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05301027 audio->read_phys =
1028 allocate_contiguous_ebi_nomap(
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001029 config.buffer_size *
1030 config.buffer_count,
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05301031 SZ_4K);
1032 if (!audio->read_phys) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001033 rc = -ENOMEM;
1034 break;
1035 }
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05301036 audio->map_v_read = msm_subsystem_map_buffer(
1037 audio->read_phys,
1038 config.buffer_size *
1039 config.buffer_count,
1040 MSM_SUBSYSTEM_MAP_KADDR,
1041 NULL, 0);
1042
1043 if (IS_ERR(audio->map_v_read)) {
1044 MM_ERR("map of read buf failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001045 rc = -ENOMEM;
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05301046 free_contiguous_memory_by_paddr(
1047 audio->read_phys);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001048 } else {
1049 uint8_t index;
1050 uint32_t offset = 0;
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05301051 audio->read_data =
1052 audio->map_v_read->vaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001053 audio->pcm_feedback = 1;
1054 audio->buf_refresh = 0;
1055 audio->pcm_buf_count =
1056 config.buffer_count;
1057 audio->read_next = 0;
1058 audio->fill_next = 0;
1059
1060 for (index = 0;
1061 index < config.buffer_count;
1062 index++) {
1063 audio->in[index].data =
1064 audio->read_data + offset;
1065 audio->in[index].addr =
1066 audio->read_phys + offset;
1067 audio->in[index].size =
1068 config.buffer_size;
1069 audio->in[index].used = 0;
1070 offset += config.buffer_size;
1071 }
1072 MM_DBG("read buf: phy addr \
1073 0x%08x kernel addr 0x%08x\n",
1074 audio->read_phys,
1075 (int)audio->read_data);
1076 rc = 0;
1077 }
1078 } else {
1079 rc = 0;
1080 }
1081 break;
1082 }
1083 case AUDIO_PAUSE:
1084 MM_DBG("AUDIO_PAUSE %ld\n", arg);
1085 rc = audpp_pause(audio->dec_id, (int) arg);
1086 break;
1087 default:
1088 rc = -EINVAL;
1089 }
1090 mutex_unlock(&audio->lock);
1091 return rc;
1092}
1093
1094/* Only useful in tunnel-mode */
1095static int audio_fsync(struct file *file, int datasync)
1096{
1097 struct audio *audio = file->private_data;
1098 struct buffer *frame;
1099 int rc = 0;
1100
1101 MM_DBG("\n"); /* Macro prints the file name and function */
1102
1103 if (!audio->running || audio->pcm_feedback) {
1104 rc = -EINVAL;
1105 goto done_nolock;
1106 }
1107
1108 mutex_lock(&audio->write_lock);
1109
1110 rc = wait_event_interruptible(audio->write_wait,
1111 (!audio->out[0].used &&
1112 !audio->out[1].used &&
1113 audio->out_needed) || audio->wflush);
1114
1115 if (rc < 0)
1116 goto done;
1117 else if (audio->wflush) {
1118 rc = -EBUSY;
1119 goto done;
1120 }
1121
1122 if (audio->reserved) {
1123 MM_DBG("send reserved byte\n");
1124 frame = audio->out + audio->out_tail;
1125 ((char *) frame->data)[0] = audio->rsv_byte;
1126 ((char *) frame->data)[1] = 0;
1127 frame->used = 2;
1128 audplay_send_data(audio, 0);
1129
1130 rc = wait_event_interruptible(audio->write_wait,
1131 (!audio->out[0].used &&
1132 !audio->out[1].used &&
1133 audio->out_needed) || audio->wflush);
1134
1135 if (rc < 0)
1136 goto done;
1137 else if (audio->wflush) {
1138 rc = -EBUSY;
1139 goto done;
1140 }
1141 }
1142
1143 /* pcm dmamiss message is sent continously
1144 * when decoder is starved so no race
1145 * condition concern
1146 */
1147 audio->teos = 0;
1148
1149 rc = wait_event_interruptible(audio->write_wait,
1150 audio->teos || audio->wflush);
1151
1152 if (audio->wflush)
1153 rc = -EBUSY;
1154
1155done:
1156 mutex_unlock(&audio->write_lock);
1157done_nolock:
1158 return rc;
1159}
1160
1161static ssize_t audio_read(struct file *file, char __user *buf, size_t count,
1162 loff_t *pos)
1163{
1164 struct audio *audio = file->private_data;
1165 const char __user *start = buf;
1166 int rc = 0;
1167
1168 if (!audio->pcm_feedback)
1169 return 0; /* PCM feedback is not enabled. Nothing to read */
1170
1171 mutex_lock(&audio->read_lock);
1172 MM_DBG("%d \n", count);
1173 while (count > 0) {
1174 rc = wait_event_interruptible(audio->read_wait,
1175 (audio->in[audio->read_next].used > 0) ||
1176 (audio->stopped) || (audio->rflush));
1177
1178 if (rc < 0)
1179 break;
1180
1181 if (audio->stopped || audio->rflush) {
1182 rc = -EBUSY;
1183 break;
1184 }
1185
1186 if (count < audio->in[audio->read_next].used) {
1187 /* Read must happen in frame boundary. Since driver
1188 does not know frame size, read count must be greater
1189 or equal to size of PCM samples */
1190 MM_DBG("audio_read: no partial frame done reading\n");
1191 break;
1192 } else {
1193 MM_DBG("audio_read: read from in[%d]\n",
1194 audio->read_next);
1195 if (copy_to_user
1196 (buf, audio->in[audio->read_next].data,
1197 audio->in[audio->read_next].used)) {
1198 MM_ERR("invalid addr %x \n", (unsigned int)buf);
1199 rc = -EFAULT;
1200 break;
1201 }
1202 count -= audio->in[audio->read_next].used;
1203 buf += audio->in[audio->read_next].used;
1204 audio->in[audio->read_next].used = 0;
1205 if ((++audio->read_next) == audio->pcm_buf_count)
1206 audio->read_next = 0;
1207 break; /* Force to exit while loop
1208 * to prevent output thread
1209 * sleep too long if data is
1210 * not ready at this moment.
1211 */
1212 }
1213 }
1214
1215 /* don't feed output buffer to HW decoder during flushing
1216 * buffer refresh command will be sent once flush completes
1217 * send buf refresh command here can confuse HW decoder
1218 */
1219 if (audio->buf_refresh && !audio->rflush) {
1220 audio->buf_refresh = 0;
1221 MM_DBG("kick start pcm feedback again\n");
1222 audplay_buffer_refresh(audio);
1223 }
1224
1225 mutex_unlock(&audio->read_lock);
1226
1227 if (buf > start)
1228 rc = buf - start;
1229
1230 MM_DBG("read %d bytes\n", rc);
1231 return rc;
1232}
1233
1234static int audwmapro_process_eos(struct audio *audio,
1235 const char __user *buf_start, unsigned short mfield_size)
1236{
1237 int rc = 0;
1238 struct buffer *frame;
1239 char *buf_ptr;
1240
1241 if (audio->reserved) {
1242 MM_DBG("flush reserve byte\n");
1243 frame = audio->out + audio->out_head;
1244 buf_ptr = frame->data;
1245 rc = wait_event_interruptible(audio->write_wait,
1246 (frame->used == 0)
1247 || (audio->stopped)
1248 || (audio->wflush));
1249 if (rc < 0)
1250 goto done;
1251 if (audio->stopped || audio->wflush) {
1252 rc = -EBUSY;
1253 goto done;
1254 }
1255
1256 buf_ptr[0] = audio->rsv_byte;
1257 buf_ptr[1] = 0;
1258 audio->out_head ^= 1;
1259 frame->mfield_sz = 0;
1260 frame->used = 2;
1261 audio->reserved = 0;
1262 audplay_send_data(audio, 0);
1263 }
1264
1265 frame = audio->out + audio->out_head;
1266
1267 rc = wait_event_interruptible(audio->write_wait,
1268 (audio->out_needed &&
1269 audio->out[0].used == 0 &&
1270 audio->out[1].used == 0)
1271 || (audio->stopped)
1272 || (audio->wflush));
1273
1274 if (rc < 0)
1275 goto done;
1276 if (audio->stopped || audio->wflush) {
1277 rc = -EBUSY;
1278 goto done;
1279 }
1280
1281 if (copy_from_user(frame->data, buf_start, mfield_size)) {
1282 rc = -EFAULT;
1283 goto done;
1284 }
1285
1286 frame->mfield_sz = mfield_size;
1287 audio->out_head ^= 1;
1288 frame->used = mfield_size;
1289 audplay_send_data(audio, 0);
1290done:
1291 return rc;
1292}
1293
1294static ssize_t audio_write(struct file *file, const char __user *buf,
1295 size_t count, loff_t *pos)
1296{
1297 struct audio *audio = file->private_data;
1298 const char __user *start = buf;
1299 struct buffer *frame;
1300 size_t xfer;
1301 char *cpy_ptr;
1302 int rc = 0, eos_condition = AUDWMAPRO_EOS_NONE;
1303 unsigned dsize;
1304 unsigned short mfield_size = 0;
1305
1306 MM_DBG("cnt=%d\n", count);
1307
1308 mutex_lock(&audio->write_lock);
1309 while (count > 0) {
1310 frame = audio->out + audio->out_head;
1311 cpy_ptr = frame->data;
1312 dsize = 0;
1313 rc = wait_event_interruptible(audio->write_wait,
1314 (frame->used == 0)
1315 || (audio->stopped)
1316 || (audio->wflush));
1317 if (rc < 0)
1318 break;
1319 if (audio->stopped || audio->wflush) {
1320 rc = -EBUSY;
1321 break;
1322 }
1323 if (audio->mfield) {
1324 if (buf == start) {
1325 /* Processing beginning of user buffer */
1326 if (__get_user(mfield_size,
1327 (unsigned short __user *) buf)) {
1328 rc = -EFAULT;
1329 break;
1330 } else if (mfield_size > count) {
1331 rc = -EINVAL;
1332 break;
1333 }
1334 MM_DBG("audio_write: mf offset_val %x\n",
1335 mfield_size);
1336 if (copy_from_user(cpy_ptr, buf, mfield_size)) {
1337 rc = -EFAULT;
1338 break;
1339 }
1340 /* Check if EOS flag is set and buffer has
1341 * contains just meta field
1342 */
1343 if (cpy_ptr[AUDWMAPRO_EOS_FLG_OFFSET] &
1344 AUDWMAPRO_EOS_FLG_MASK) {
1345 MM_DBG("audio_write: EOS SET\n");
1346 eos_condition = AUDWMAPRO_EOS_SET;
1347 if (mfield_size == count) {
1348 buf += mfield_size;
1349 break;
1350 } else
1351 cpy_ptr[AUDWMAPRO_EOS_FLG_OFFSET]
1352 &= ~AUDWMAPRO_EOS_FLG_MASK;
1353 }
1354 cpy_ptr += mfield_size;
1355 count -= mfield_size;
1356 dsize += mfield_size;
1357 buf += mfield_size;
1358 } else {
1359 mfield_size = 0;
1360 MM_DBG("audio_write: continuous buffer\n");
1361 }
1362 frame->mfield_sz = mfield_size;
1363 }
1364
1365 if (audio->reserved) {
1366 MM_DBG("append reserved byte %x\n", audio->rsv_byte);
1367 *cpy_ptr = audio->rsv_byte;
1368 xfer = (count > ((frame->size - mfield_size) - 1)) ?
1369 (frame->size - mfield_size) - 1 : count;
1370 cpy_ptr++;
1371 dsize += 1;
1372 audio->reserved = 0;
1373 } else
1374 xfer = (count > (frame->size - mfield_size)) ?
1375 (frame->size - mfield_size) : count;
1376
1377 if (copy_from_user(cpy_ptr, buf, xfer)) {
1378 rc = -EFAULT;
1379 break;
1380 }
1381
1382 dsize += xfer;
1383 if (dsize & 1) {
1384 audio->rsv_byte = ((char *) frame->data)[dsize - 1];
1385 MM_DBG("odd length buf reserve last byte %x\n",
1386 audio->rsv_byte);
1387 audio->reserved = 1;
1388 dsize--;
1389 }
1390 count -= xfer;
1391 buf += xfer;
1392
1393 if (dsize > 0) {
1394 audio->out_head ^= 1;
1395 frame->used = dsize;
1396 audplay_send_data(audio, 0);
1397 }
1398 }
1399 if (eos_condition == AUDWMAPRO_EOS_SET)
1400 rc = audwmapro_process_eos(audio, start, mfield_size);
1401 mutex_unlock(&audio->write_lock);
1402 if (!rc) {
1403 if (buf > start)
1404 return buf - start;
1405 }
1406 return rc;
1407}
1408
1409static int audio_release(struct inode *inode, struct file *file)
1410{
1411 struct audio *audio = file->private_data;
1412
1413 MM_INFO("audio instance 0x%08x freeing\n", (int)audio);
1414 mutex_lock(&audio->lock);
1415 audio_disable(audio);
1416 if (audio->rmt_resource_released == 0)
1417 rmt_put_resource(audio);
1418 audio_flush(audio);
1419 audio_flush_pcm_buf(audio);
1420 msm_adsp_put(audio->audplay);
1421 audpp_adec_free(audio->dec_id);
1422#ifdef CONFIG_HAS_EARLYSUSPEND
1423 unregister_early_suspend(&audio->suspend_ctl.node);
1424#endif
1425 audio->event_abort = 1;
1426 wake_up(&audio->event_wait);
1427 audwmapro_reset_event_queue(audio);
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05301428 msm_subsystem_unmap_buffer(audio->map_v_write);
1429 free_contiguous_memory_by_paddr(audio->phys);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001430 if (audio->read_data) {
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05301431 msm_subsystem_unmap_buffer(audio->map_v_read);
1432 free_contiguous_memory_by_paddr(audio->read_phys);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001433 }
1434 mutex_unlock(&audio->lock);
1435#ifdef CONFIG_DEBUG_FS
1436 if (audio->dentry)
1437 debugfs_remove(audio->dentry);
1438#endif
1439 kfree(audio);
1440 return 0;
1441}
1442
1443#ifdef CONFIG_HAS_EARLYSUSPEND
1444static void audwmapro_post_event(struct audio *audio, int type,
1445 union msm_audio_event_payload payload)
1446{
1447 struct audwmapro_event *e_node = NULL;
1448 unsigned long flags;
1449
1450 spin_lock_irqsave(&audio->event_queue_lock, flags);
1451
1452 if (!list_empty(&audio->free_event_queue)) {
1453 e_node = list_first_entry(&audio->free_event_queue,
1454 struct audwmapro_event, list);
1455 list_del(&e_node->list);
1456 } else {
1457 e_node = kmalloc(sizeof(struct audwmapro_event), GFP_ATOMIC);
1458 if (!e_node) {
1459 MM_ERR("No mem to post event %d\n", type);
1460 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
1461 return;
1462 }
1463 }
1464
1465 e_node->event_type = type;
1466 e_node->payload = payload;
1467
1468 list_add_tail(&e_node->list, &audio->event_queue);
1469 spin_unlock_irqrestore(&audio->event_queue_lock, flags);
1470 wake_up(&audio->event_wait);
1471}
1472
1473static void audwmapro_suspend(struct early_suspend *h)
1474{
1475 struct audwmapro_suspend_ctl *ctl =
1476 container_of(h, struct audwmapro_suspend_ctl, node);
1477 union msm_audio_event_payload payload;
1478
1479 MM_DBG("\n"); /* Macro prints the file name and function */
1480 audwmapro_post_event(ctl->audio, AUDIO_EVENT_SUSPEND, payload);
1481}
1482
1483static void audwmapro_resume(struct early_suspend *h)
1484{
1485 struct audwmapro_suspend_ctl *ctl =
1486 container_of(h, struct audwmapro_suspend_ctl, node);
1487 union msm_audio_event_payload payload;
1488
1489 MM_DBG("\n"); /* Macro prints the file name and function */
1490 audwmapro_post_event(ctl->audio, AUDIO_EVENT_RESUME, payload);
1491}
1492#endif
1493
1494#ifdef CONFIG_DEBUG_FS
1495static ssize_t audwmapro_debug_open(struct inode *inode, struct file *file)
1496{
1497 file->private_data = inode->i_private;
1498 return 0;
1499}
1500
1501static ssize_t audwmapro_debug_read(struct file *file, char __user *buf,
1502 size_t count, loff_t *ppos)
1503{
1504 const int debug_bufmax = 4096;
1505 static char buffer[4096];
1506 int n = 0, i;
1507 struct audio *audio = file->private_data;
1508
1509 mutex_lock(&audio->lock);
1510 n = scnprintf(buffer, debug_bufmax, "opened %d\n", audio->opened);
1511 n += scnprintf(buffer + n, debug_bufmax - n,
1512 "enabled %d\n", audio->enabled);
1513 n += scnprintf(buffer + n, debug_bufmax - n,
1514 "stopped %d\n", audio->stopped);
1515 n += scnprintf(buffer + n, debug_bufmax - n,
1516 "pcm_feedback %d\n", audio->pcm_feedback);
1517 n += scnprintf(buffer + n, debug_bufmax - n,
1518 "out_buf_sz %d\n", audio->out[0].size);
1519 n += scnprintf(buffer + n, debug_bufmax - n,
1520 "pcm_buf_count %d \n", audio->pcm_buf_count);
1521 n += scnprintf(buffer + n, debug_bufmax - n,
1522 "pcm_buf_sz %d \n", audio->in[0].size);
1523 n += scnprintf(buffer + n, debug_bufmax - n,
1524 "volume %x \n", audio->vol_pan.volume);
1525 n += scnprintf(buffer + n, debug_bufmax - n,
1526 "sample rate %d \n", audio->out_sample_rate);
1527 n += scnprintf(buffer + n, debug_bufmax - n,
1528 "channel mode %d \n", audio->out_channel_mode);
1529 mutex_unlock(&audio->lock);
1530 /* Following variables are only useful for debugging when
1531 * when playback halts unexpectedly. Thus, no mutual exclusion
1532 * enforced
1533 */
1534 n += scnprintf(buffer + n, debug_bufmax - n,
1535 "wflush %d\n", audio->wflush);
1536 n += scnprintf(buffer + n, debug_bufmax - n,
1537 "rflush %d\n", audio->rflush);
1538 n += scnprintf(buffer + n, debug_bufmax - n,
1539 "running %d \n", audio->running);
1540 n += scnprintf(buffer + n, debug_bufmax - n,
1541 "dec state %d \n", audio->dec_state);
1542 n += scnprintf(buffer + n, debug_bufmax - n,
1543 "out_needed %d \n", audio->out_needed);
1544 n += scnprintf(buffer + n, debug_bufmax - n,
1545 "out_head %d \n", audio->out_head);
1546 n += scnprintf(buffer + n, debug_bufmax - n,
1547 "out_tail %d \n", audio->out_tail);
1548 n += scnprintf(buffer + n, debug_bufmax - n,
1549 "out[0].used %d \n", audio->out[0].used);
1550 n += scnprintf(buffer + n, debug_bufmax - n,
1551 "out[1].used %d \n", audio->out[1].used);
1552 n += scnprintf(buffer + n, debug_bufmax - n,
1553 "buffer_refresh %d \n", audio->buf_refresh);
1554 n += scnprintf(buffer + n, debug_bufmax - n,
1555 "read_next %d \n", audio->read_next);
1556 n += scnprintf(buffer + n, debug_bufmax - n,
1557 "fill_next %d \n", audio->fill_next);
1558 for (i = 0; i < audio->pcm_buf_count; i++)
1559 n += scnprintf(buffer + n, debug_bufmax - n,
1560 "in[%d].size %d \n", i, audio->in[i].used);
1561 buffer[n] = 0;
1562 return simple_read_from_buffer(buf, count, ppos, buffer, n);
1563}
1564
1565static const struct file_operations audwmapro_debug_fops = {
1566 .read = audwmapro_debug_read,
1567 .open = audwmapro_debug_open,
1568};
1569#endif
1570
1571static int audio_open(struct inode *inode, struct file *file)
1572{
1573 struct audio *audio = NULL;
1574 int rc, dec_attrb, decid, i;
1575 unsigned pmem_sz = DMASZ_MAX;
1576 struct audwmapro_event *e_node = NULL;
1577#ifdef CONFIG_DEBUG_FS
1578 /* 4 bytes represents decoder number, 1 byte for terminate string */
1579 char name[sizeof "msm_wmapro_" + 5];
1580#endif
1581
1582 /* Allocate Mem for audio instance */
1583 audio = kzalloc(sizeof(struct audio), GFP_KERNEL);
1584 if (!audio) {
1585 MM_ERR("no memory to allocate audio instance \n");
1586 rc = -ENOMEM;
1587 goto done;
1588 }
1589 MM_INFO("audio instance 0x%08x created\n", (int)audio);
1590
1591 /* Allocate the decoder */
1592 dec_attrb = AUDDEC_DEC_WMAPRO;
1593 if ((file->f_mode & FMODE_WRITE) &&
1594 (file->f_mode & FMODE_READ)) {
1595 dec_attrb |= MSM_AUD_MODE_NONTUNNEL;
1596 audio->pcm_feedback = NON_TUNNEL_MODE_PLAYBACK;
1597 } else if ((file->f_mode & FMODE_WRITE) &&
1598 !(file->f_mode & FMODE_READ)) {
1599 dec_attrb |= MSM_AUD_MODE_TUNNEL;
1600 audio->pcm_feedback = TUNNEL_MODE_PLAYBACK;
1601 } else {
1602 kfree(audio);
1603 rc = -EACCES;
1604 goto done;
1605 }
1606
1607 decid = audpp_adec_alloc(dec_attrb, &audio->module_name,
1608 &audio->queue_id);
1609
1610 if (decid < 0) {
1611 MM_ERR("No free decoder available, freeing instance 0x%08x\n",
1612 (int)audio);
1613 rc = -ENODEV;
1614 kfree(audio);
1615 goto done;
1616 }
1617 audio->dec_id = decid & MSM_AUD_DECODER_MASK;
1618
1619 while (pmem_sz >= DMASZ_MIN) {
1620 MM_DBG("pmemsz = %d\n", pmem_sz);
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05301621 audio->phys = allocate_contiguous_ebi_nomap(pmem_sz, SZ_4K);
1622 if (audio->phys) {
1623 audio->map_v_write = msm_subsystem_map_buffer(
1624 audio->phys, pmem_sz,
1625 MSM_SUBSYSTEM_MAP_KADDR,
1626 NULL, 0);
1627 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 }
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05301637 audio->data = audio->map_v_write->vaddr;
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:
Santosh Mardi0be3b8e2011-07-06 10:00:21 +05301737 msm_subsystem_unmap_buffer(audio->map_v_write);
1738 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);