Merge branches 'battery-2.6.34', 'bugzilla-10805', 'bugzilla-14668', 'bugzilla-531916...
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / staging / dream / qdsp5 / audio_mp3.c
1 /* arch/arm/mach-msm/qdsp5/audio_mp3.c
2 *
3 * mp3 audio output device
4 *
5 * Copyright (C) 2008 Google, Inc.
6 * Copyright (C) 2008 HTC Corporation
7 *
8 * This software is licensed under the terms of the GNU General Public
9 * License version 2, as published by the Free Software Foundation, and
10 * may be copied, distributed, and modified under those terms.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 */
18
19 #include <linux/module.h>
20 #include <linux/fs.h>
21 #include <linux/miscdevice.h>
22 #include <linux/uaccess.h>
23 #include <linux/kthread.h>
24 #include <linux/wait.h>
25 #include <linux/dma-mapping.h>
26
27 #include <linux/delay.h>
28
29 #include <asm/atomic.h>
30 #include <asm/ioctls.h>
31 #include <mach/msm_adsp.h>
32
33 #include <linux/msm_audio.h>
34
35 #include "audmgr.h"
36
37 #include <mach/qdsp5/qdsp5audppcmdi.h>
38 #include <mach/qdsp5/qdsp5audppmsg.h>
39 #include <mach/qdsp5/qdsp5audplaycmdi.h>
40 #include <mach/qdsp5/qdsp5audplaymsg.h>
41
42 /* for queue ids - should be relative to module number*/
43 #include "adsp.h"
44
45 #ifdef DEBUG
46 #define dprintk(format, arg...) \
47 printk(KERN_DEBUG format, ## arg)
48 #else
49 #define dprintk(format, arg...) do {} while (0)
50 #endif
51
52 /* Size must be power of 2 */
53 #define BUFSZ_MAX 32768
54 #define BUFSZ_MIN 4096
55 #define DMASZ_MAX (BUFSZ_MAX * 2)
56 #define DMASZ_MIN (BUFSZ_MIN * 2)
57
58 #define AUDPLAY_INVALID_READ_PTR_OFFSET 0xFFFF
59 #define AUDDEC_DEC_MP3 2
60
61 #define PCM_BUFSZ_MIN 4800 /* Hold one stereo MP3 frame */
62 #define PCM_BUF_MAX_COUNT 5 /* DSP only accepts 5 buffers at most
63 but support 2 buffers currently */
64 #define ROUTING_MODE_FTRT 1
65 #define ROUTING_MODE_RT 2
66 /* Decoder status received from AUDPPTASK */
67 #define AUDPP_DEC_STATUS_SLEEP 0
68 #define AUDPP_DEC_STATUS_INIT 1
69 #define AUDPP_DEC_STATUS_CFG 2
70 #define AUDPP_DEC_STATUS_PLAY 3
71
72 struct buffer {
73 void *data;
74 unsigned size;
75 unsigned used; /* Input usage actual DSP produced PCM size */
76 unsigned addr;
77 };
78
79 struct audio {
80 struct buffer out[2];
81
82 spinlock_t dsp_lock;
83
84 uint8_t out_head;
85 uint8_t out_tail;
86 uint8_t out_needed; /* number of buffers the dsp is waiting for */
87 unsigned out_dma_sz;
88
89 atomic_t out_bytes;
90
91 struct mutex lock;
92 struct mutex write_lock;
93 wait_queue_head_t write_wait;
94
95 /* Host PCM section */
96 struct buffer in[PCM_BUF_MAX_COUNT];
97 struct mutex read_lock;
98 wait_queue_head_t read_wait; /* Wait queue for read */
99 char *read_data; /* pointer to reader buffer */
100 dma_addr_t read_phys; /* physical address of reader buffer */
101 uint8_t read_next; /* index to input buffers to be read next */
102 uint8_t fill_next; /* index to buffer that DSP should be filling */
103 uint8_t pcm_buf_count; /* number of pcm buffer allocated */
104 /* ---- End of Host PCM section */
105
106 struct msm_adsp_module *audplay;
107
108 /* configuration to use on next enable */
109 uint32_t out_sample_rate;
110 uint32_t out_channel_mode;
111
112 struct audmgr audmgr;
113
114 /* data allocated for various buffers */
115 char *data;
116 dma_addr_t phys;
117
118 int rflush; /* Read flush */
119 int wflush; /* Write flush */
120 int opened;
121 int enabled;
122 int running;
123 int stopped; /* set when stopped, cleared on flush */
124 int pcm_feedback;
125 int buf_refresh;
126
127 int reserved; /* A byte is being reserved */
128 char rsv_byte; /* Handle odd length user data */
129
130 unsigned volume;
131
132 uint16_t dec_id;
133 uint32_t read_ptr_offset;
134 };
135
136 static int auddec_dsp_config(struct audio *audio, int enable);
137 static void audpp_cmd_cfg_adec_params(struct audio *audio);
138 static void audpp_cmd_cfg_routing_mode(struct audio *audio);
139 static void audplay_send_data(struct audio *audio, unsigned needed);
140 static void audplay_config_hostpcm(struct audio *audio);
141 static void audplay_buffer_refresh(struct audio *audio);
142 static void audio_dsp_event(void *private, unsigned id, uint16_t *msg);
143
144 /* must be called with audio->lock held */
145 static int audio_enable(struct audio *audio)
146 {
147 struct audmgr_config cfg;
148 int rc;
149
150 pr_info("audio_enable()\n");
151
152 if (audio->enabled)
153 return 0;
154
155 audio->out_tail = 0;
156 audio->out_needed = 0;
157
158 cfg.tx_rate = RPC_AUD_DEF_SAMPLE_RATE_NONE;
159 cfg.rx_rate = RPC_AUD_DEF_SAMPLE_RATE_48000;
160 cfg.def_method = RPC_AUD_DEF_METHOD_PLAYBACK;
161 cfg.codec = RPC_AUD_DEF_CODEC_MP3;
162 cfg.snd_method = RPC_SND_METHOD_MIDI;
163
164 rc = audmgr_enable(&audio->audmgr, &cfg);
165 if (rc < 0)
166 return rc;
167
168 if (msm_adsp_enable(audio->audplay)) {
169 pr_err("audio: msm_adsp_enable(audplay) failed\n");
170 audmgr_disable(&audio->audmgr);
171 return -ENODEV;
172 }
173
174 if (audpp_enable(audio->dec_id, audio_dsp_event, audio)) {
175 pr_err("audio: audpp_enable() failed\n");
176 msm_adsp_disable(audio->audplay);
177 audmgr_disable(&audio->audmgr);
178 return -ENODEV;
179 }
180
181 audio->enabled = 1;
182 return 0;
183 }
184
185 /* must be called with audio->lock held */
186 static int audio_disable(struct audio *audio)
187 {
188 pr_info("audio_disable()\n");
189 if (audio->enabled) {
190 audio->enabled = 0;
191 auddec_dsp_config(audio, 0);
192 wake_up(&audio->write_wait);
193 wake_up(&audio->read_wait);
194 msm_adsp_disable(audio->audplay);
195 audpp_disable(audio->dec_id, audio);
196 audmgr_disable(&audio->audmgr);
197 audio->out_needed = 0;
198 }
199 return 0;
200 }
201
202 /* ------------------- dsp --------------------- */
203 static void audio_update_pcm_buf_entry(struct audio *audio, uint32_t *payload)
204 {
205 uint8_t index;
206 unsigned long flags;
207
208 if (audio->rflush) {
209 audio->buf_refresh = 1;
210 return;
211 }
212 spin_lock_irqsave(&audio->dsp_lock, flags);
213 for (index = 0; index < payload[1]; index++) {
214 if (audio->in[audio->fill_next].addr ==
215 payload[2 + index * 2]) {
216 pr_info("audio_update_pcm_buf_entry: in[%d] ready\n",
217 audio->fill_next);
218 audio->in[audio->fill_next].used =
219 payload[3 + index * 2];
220 if ((++audio->fill_next) == audio->pcm_buf_count)
221 audio->fill_next = 0;
222
223 } else {
224 pr_err
225 ("audio_update_pcm_buf_entry: expected=%x ret=%x\n"
226 , audio->in[audio->fill_next].addr,
227 payload[1 + index * 2]);
228 break;
229 }
230 }
231 if (audio->in[audio->fill_next].used == 0) {
232 audplay_buffer_refresh(audio);
233 } else {
234 pr_info("audio_update_pcm_buf_entry: read cannot keep up\n");
235 audio->buf_refresh = 1;
236 }
237 wake_up(&audio->read_wait);
238 spin_unlock_irqrestore(&audio->dsp_lock, flags);
239
240 }
241
242 static void audplay_dsp_event(void *data, unsigned id, size_t len,
243 void (*getevent) (void *ptr, size_t len))
244 {
245 struct audio *audio = data;
246 uint32_t msg[28];
247 getevent(msg, sizeof(msg));
248
249 dprintk("audplay_dsp_event: msg_id=%x\n", id);
250
251 switch (id) {
252 case AUDPLAY_MSG_DEC_NEEDS_DATA:
253 audplay_send_data(audio, 1);
254 break;
255
256 case AUDPLAY_MSG_BUFFER_UPDATE:
257 audio_update_pcm_buf_entry(audio, msg);
258 break;
259
260 default:
261 pr_err("unexpected message from decoder \n");
262 break;
263 }
264 }
265
266 static void audio_dsp_event(void *private, unsigned id, uint16_t *msg)
267 {
268 struct audio *audio = private;
269
270 switch (id) {
271 case AUDPP_MSG_STATUS_MSG:{
272 unsigned status = msg[1];
273
274 switch (status) {
275 case AUDPP_DEC_STATUS_SLEEP:
276 pr_info("decoder status: sleep \n");
277 break;
278
279 case AUDPP_DEC_STATUS_INIT:
280 pr_info("decoder status: init \n");
281 audpp_cmd_cfg_routing_mode(audio);
282 break;
283
284 case AUDPP_DEC_STATUS_CFG:
285 pr_info("decoder status: cfg \n");
286 break;
287 case AUDPP_DEC_STATUS_PLAY:
288 pr_info("decoder status: play \n");
289 if (audio->pcm_feedback) {
290 audplay_config_hostpcm(audio);
291 audplay_buffer_refresh(audio);
292 }
293 break;
294 default:
295 pr_err("unknown decoder status \n");
296 break;
297 }
298 break;
299 }
300 case AUDPP_MSG_CFG_MSG:
301 if (msg[0] == AUDPP_MSG_ENA_ENA) {
302 pr_info("audio_dsp_event: CFG_MSG ENABLE\n");
303 auddec_dsp_config(audio, 1);
304 audio->out_needed = 0;
305 audio->running = 1;
306 audpp_set_volume_and_pan(audio->dec_id, audio->volume,
307 0);
308 audpp_avsync(audio->dec_id, 22050);
309 } else if (msg[0] == AUDPP_MSG_ENA_DIS) {
310 pr_info("audio_dsp_event: CFG_MSG DISABLE\n");
311 audpp_avsync(audio->dec_id, 0);
312 audio->running = 0;
313 } else {
314 pr_err("audio_dsp_event: CFG_MSG %d?\n", msg[0]);
315 }
316 break;
317 case AUDPP_MSG_ROUTING_ACK:
318 pr_info("audio_dsp_event: ROUTING_ACK mode=%d\n", msg[1]);
319 audpp_cmd_cfg_adec_params(audio);
320 break;
321
322 case AUDPP_MSG_FLUSH_ACK:
323 dprintk("%s: FLUSH_ACK\n", __func__);
324 audio->wflush = 0;
325 audio->rflush = 0;
326 if (audio->pcm_feedback)
327 audplay_buffer_refresh(audio);
328 break;
329
330 default:
331 pr_err("audio_dsp_event: UNKNOWN (%d)\n", id);
332 }
333
334 }
335
336
337 struct msm_adsp_ops audplay_adsp_ops = {
338 .event = audplay_dsp_event,
339 };
340
341
342 #define audplay_send_queue0(audio, cmd, len) \
343 msm_adsp_write(audio->audplay, QDSP_uPAudPlay0BitStreamCtrlQueue, \
344 cmd, len)
345
346 static int auddec_dsp_config(struct audio *audio, int enable)
347 {
348 audpp_cmd_cfg_dec_type cmd;
349
350 memset(&cmd, 0, sizeof(cmd));
351 cmd.cmd_id = AUDPP_CMD_CFG_DEC_TYPE;
352 if (enable)
353 cmd.dec0_cfg = AUDPP_CMD_UPDATDE_CFG_DEC |
354 AUDPP_CMD_ENA_DEC_V |
355 AUDDEC_DEC_MP3;
356 else
357 cmd.dec0_cfg = AUDPP_CMD_UPDATDE_CFG_DEC |
358 AUDPP_CMD_DIS_DEC_V;
359
360 return audpp_send_queue1(&cmd, sizeof(cmd));
361 }
362
363 static void audpp_cmd_cfg_adec_params(struct audio *audio)
364 {
365 audpp_cmd_cfg_adec_params_mp3 cmd;
366
367 memset(&cmd, 0, sizeof(cmd));
368 cmd.common.cmd_id = AUDPP_CMD_CFG_ADEC_PARAMS;
369 cmd.common.length = AUDPP_CMD_CFG_ADEC_PARAMS_MP3_LEN;
370 cmd.common.dec_id = audio->dec_id;
371 cmd.common.input_sampling_frequency = audio->out_sample_rate;
372
373 audpp_send_queue2(&cmd, sizeof(cmd));
374 }
375
376 static void audpp_cmd_cfg_routing_mode(struct audio *audio)
377 {
378 struct audpp_cmd_routing_mode cmd;
379 pr_info("audpp_cmd_cfg_routing_mode()\n");
380 memset(&cmd, 0, sizeof(cmd));
381 cmd.cmd_id = AUDPP_CMD_ROUTING_MODE;
382 cmd.object_number = audio->dec_id;
383 if (audio->pcm_feedback)
384 cmd.routing_mode = ROUTING_MODE_FTRT;
385 else
386 cmd.routing_mode = ROUTING_MODE_RT;
387
388 audpp_send_queue1(&cmd, sizeof(cmd));
389 }
390
391 static int audplay_dsp_send_data_avail(struct audio *audio,
392 unsigned idx, unsigned len)
393 {
394 audplay_cmd_bitstream_data_avail cmd;
395
396 cmd.cmd_id = AUDPLAY_CMD_BITSTREAM_DATA_AVAIL;
397 cmd.decoder_id = audio->dec_id;
398 cmd.buf_ptr = audio->out[idx].addr;
399 cmd.buf_size = len/2;
400 cmd.partition_number = 0;
401 return audplay_send_queue0(audio, &cmd, sizeof(cmd));
402 }
403
404 static void audplay_buffer_refresh(struct audio *audio)
405 {
406 struct audplay_cmd_buffer_refresh refresh_cmd;
407
408 refresh_cmd.cmd_id = AUDPLAY_CMD_BUFFER_REFRESH;
409 refresh_cmd.num_buffers = 1;
410 refresh_cmd.buf0_address = audio->in[audio->fill_next].addr;
411 refresh_cmd.buf0_length = audio->in[audio->fill_next].size -
412 (audio->in[audio->fill_next].size % 576); /* Mp3 frame size */
413 refresh_cmd.buf_read_count = 0;
414 pr_info("audplay_buffer_fresh: buf0_addr=%x buf0_len=%d\n",
415 refresh_cmd.buf0_address, refresh_cmd.buf0_length);
416 (void)audplay_send_queue0(audio, &refresh_cmd, sizeof(refresh_cmd));
417 }
418
419 static void audplay_config_hostpcm(struct audio *audio)
420 {
421 struct audplay_cmd_hpcm_buf_cfg cfg_cmd;
422
423 pr_info("audplay_config_hostpcm()\n");
424 cfg_cmd.cmd_id = AUDPLAY_CMD_HPCM_BUF_CFG;
425 cfg_cmd.max_buffers = 1;
426 cfg_cmd.byte_swap = 0;
427 cfg_cmd.hostpcm_config = (0x8000) | (0x4000);
428 cfg_cmd.feedback_frequency = 1;
429 cfg_cmd.partition_number = 0;
430 (void)audplay_send_queue0(audio, &cfg_cmd, sizeof(cfg_cmd));
431
432 }
433
434 static void audplay_send_data(struct audio *audio, unsigned needed)
435 {
436 struct buffer *frame;
437 unsigned long flags;
438
439 spin_lock_irqsave(&audio->dsp_lock, flags);
440 if (!audio->running)
441 goto done;
442
443 if (audio->wflush) {
444 audio->out_needed = 1;
445 goto done;
446 }
447
448 if (needed && !audio->wflush) {
449 /* We were called from the callback because the DSP
450 * requested more data. Note that the DSP does want
451 * more data, and if a buffer was in-flight, mark it
452 * as available (since the DSP must now be done with
453 * it).
454 */
455 audio->out_needed = 1;
456 frame = audio->out + audio->out_tail;
457 if (frame->used == 0xffffffff) {
458 dprintk("frame %d free\n", audio->out_tail);
459 frame->used = 0;
460 audio->out_tail ^= 1;
461 wake_up(&audio->write_wait);
462 }
463 }
464
465 if (audio->out_needed) {
466 /* If the DSP currently wants data and we have a
467 * buffer available, we will send it and reset
468 * the needed flag. We'll mark the buffer as in-flight
469 * so that it won't be recycled until the next buffer
470 * is requested
471 */
472
473 frame = audio->out + audio->out_tail;
474 if (frame->used) {
475 BUG_ON(frame->used == 0xffffffff);
476 dprintk("frame %d busy\n", audio->out_tail);
477 audplay_dsp_send_data_avail(audio, audio->out_tail,
478 frame->used);
479 frame->used = 0xffffffff;
480 audio->out_needed = 0;
481 }
482 }
483 done:
484 spin_unlock_irqrestore(&audio->dsp_lock, flags);
485 }
486
487 /* ------------------- device --------------------- */
488
489 static void audio_flush(struct audio *audio)
490 {
491 audio->out[0].used = 0;
492 audio->out[1].used = 0;
493 audio->out_head = 0;
494 audio->out_tail = 0;
495 audio->reserved = 0;
496 atomic_set(&audio->out_bytes, 0);
497 }
498
499 static void audio_flush_pcm_buf(struct audio *audio)
500 {
501 uint8_t index;
502
503 for (index = 0; index < PCM_BUF_MAX_COUNT; index++)
504 audio->in[index].used = 0;
505
506 audio->read_next = 0;
507 audio->fill_next = 0;
508 }
509
510 static void audio_ioport_reset(struct audio *audio)
511 {
512 /* Make sure read/write thread are free from
513 * sleep and knowing that system is not able
514 * to process io request at the moment
515 */
516 wake_up(&audio->write_wait);
517 mutex_lock(&audio->write_lock);
518 audio_flush(audio);
519 mutex_unlock(&audio->write_lock);
520 wake_up(&audio->read_wait);
521 mutex_lock(&audio->read_lock);
522 audio_flush_pcm_buf(audio);
523 mutex_unlock(&audio->read_lock);
524 }
525
526 static long audio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
527 {
528 struct audio *audio = file->private_data;
529 int rc = 0;
530
531 pr_info("audio_ioctl() cmd = %d\n", cmd);
532
533 if (cmd == AUDIO_GET_STATS) {
534 struct msm_audio_stats stats;
535 stats.byte_count = audpp_avsync_byte_count(audio->dec_id);
536 stats.sample_count = audpp_avsync_sample_count(audio->dec_id);
537 if (copy_to_user((void *) arg, &stats, sizeof(stats)))
538 return -EFAULT;
539 return 0;
540 }
541 if (cmd == AUDIO_SET_VOLUME) {
542 unsigned long flags;
543 spin_lock_irqsave(&audio->dsp_lock, flags);
544 audio->volume = arg;
545 if (audio->running)
546 audpp_set_volume_and_pan(audio->dec_id, arg, 0);
547 spin_unlock_irqrestore(&audio->dsp_lock, flags);
548 return 0;
549 }
550 mutex_lock(&audio->lock);
551 switch (cmd) {
552 case AUDIO_START:
553 rc = audio_enable(audio);
554 break;
555 case AUDIO_STOP:
556 rc = audio_disable(audio);
557 audio->stopped = 1;
558 audio_ioport_reset(audio);
559 audio->stopped = 0;
560 break;
561 case AUDIO_FLUSH:
562 dprintk("%s: AUDIO_FLUSH\n", __func__);
563 audio->rflush = 1;
564 audio->wflush = 1;
565 audio_ioport_reset(audio);
566 audio->rflush = 0;
567 audio->wflush = 0;
568
569 if (audio->buf_refresh) {
570 audio->buf_refresh = 0;
571 audplay_buffer_refresh(audio);
572 }
573 break;
574
575 case AUDIO_SET_CONFIG: {
576 struct msm_audio_config config;
577 if (copy_from_user(&config, (void *) arg, sizeof(config))) {
578 rc = -EFAULT;
579 break;
580 }
581 if (config.channel_count == 1) {
582 config.channel_count = AUDPP_CMD_PCM_INTF_MONO_V;
583 } else if (config.channel_count == 2) {
584 config.channel_count = AUDPP_CMD_PCM_INTF_STEREO_V;
585 } else {
586 rc = -EINVAL;
587 break;
588 }
589 audio->out_sample_rate = config.sample_rate;
590 audio->out_channel_mode = config.channel_count;
591 rc = 0;
592 break;
593 }
594 case AUDIO_GET_CONFIG: {
595 struct msm_audio_config config;
596 config.buffer_size = (audio->out_dma_sz >> 1);
597 config.buffer_count = 2;
598 config.sample_rate = audio->out_sample_rate;
599 if (audio->out_channel_mode == AUDPP_CMD_PCM_INTF_MONO_V) {
600 config.channel_count = 1;
601 } else {
602 config.channel_count = 2;
603 }
604 config.unused[0] = 0;
605 config.unused[1] = 0;
606 config.unused[2] = 0;
607 config.unused[3] = 0;
608 if (copy_to_user((void *) arg, &config, sizeof(config))) {
609 rc = -EFAULT;
610 } else {
611 rc = 0;
612 }
613 break;
614 }
615 case AUDIO_GET_PCM_CONFIG:{
616 struct msm_audio_pcm_config config;
617 config.pcm_feedback = 0;
618 config.buffer_count = PCM_BUF_MAX_COUNT;
619 config.buffer_size = PCM_BUFSZ_MIN;
620 if (copy_to_user((void *)arg, &config,
621 sizeof(config)))
622 rc = -EFAULT;
623 else
624 rc = 0;
625 break;
626 }
627 case AUDIO_SET_PCM_CONFIG:{
628 struct msm_audio_pcm_config config;
629 if (copy_from_user
630 (&config, (void *)arg, sizeof(config))) {
631 rc = -EFAULT;
632 break;
633 }
634 if ((config.buffer_count > PCM_BUF_MAX_COUNT) ||
635 (config.buffer_count == 1))
636 config.buffer_count = PCM_BUF_MAX_COUNT;
637
638 if (config.buffer_size < PCM_BUFSZ_MIN)
639 config.buffer_size = PCM_BUFSZ_MIN;
640
641 /* Check if pcm feedback is required */
642 if ((config.pcm_feedback) && (!audio->read_data)) {
643 pr_info("ioctl: allocate PCM buffer %d\n",
644 config.buffer_count *
645 config.buffer_size);
646 audio->read_data =
647 dma_alloc_coherent(NULL,
648 config.buffer_size *
649 config.buffer_count,
650 &audio->read_phys,
651 GFP_KERNEL);
652 if (!audio->read_data) {
653 pr_err("audio_mp3: malloc pcm buf failed\n");
654 rc = -1;
655 } else {
656 uint8_t index;
657 uint32_t offset = 0;
658 audio->pcm_feedback = 1;
659 audio->buf_refresh = 0;
660 audio->pcm_buf_count =
661 config.buffer_count;
662 audio->read_next = 0;
663 audio->fill_next = 0;
664
665 for (index = 0;
666 index < config.buffer_count;
667 index++) {
668 audio->in[index].data =
669 audio->read_data + offset;
670 audio->in[index].addr =
671 audio->read_phys + offset;
672 audio->in[index].size =
673 config.buffer_size;
674 audio->in[index].used = 0;
675 offset += config.buffer_size;
676 }
677 rc = 0;
678 }
679 } else {
680 rc = 0;
681 }
682 break;
683 }
684 case AUDIO_PAUSE:
685 dprintk("%s: AUDIO_PAUSE %ld\n", __func__, arg);
686 rc = audpp_pause(audio->dec_id, (int) arg);
687 break;
688 default:
689 rc = -EINVAL;
690 }
691 mutex_unlock(&audio->lock);
692 return rc;
693 }
694
695 static ssize_t audio_read(struct file *file, char __user *buf, size_t count,
696 loff_t *pos)
697 {
698 struct audio *audio = file->private_data;
699 const char __user *start = buf;
700 int rc = 0;
701
702 if (!audio->pcm_feedback)
703 return 0; /* PCM feedback disabled. Nothing to read */
704
705 mutex_lock(&audio->read_lock);
706 pr_info("audio_read() %d \n", count);
707 while (count > 0) {
708 rc = wait_event_interruptible(audio->read_wait,
709 (audio->in[audio->read_next].
710 used > 0) || (audio->stopped)
711 || (audio->rflush));
712
713 if (rc < 0)
714 break;
715
716 if (audio->stopped || audio->rflush) {
717 rc = -EBUSY;
718 break;
719 }
720
721 if (count < audio->in[audio->read_next].used) {
722 /* Read must happen in frame boundary. Since
723 * driver does not know frame size, read count
724 * must be greater or equal
725 * to size of PCM samples
726 */
727 pr_info("audio_read: no partial frame done reading\n");
728 break;
729 } else {
730 pr_info("audio_read: read from in[%d]\n",
731 audio->read_next);
732 if (copy_to_user
733 (buf, audio->in[audio->read_next].data,
734 audio->in[audio->read_next].used)) {
735 pr_err("audio_read: invalid addr %x \n",
736 (unsigned int)buf);
737 rc = -EFAULT;
738 break;
739 }
740 count -= audio->in[audio->read_next].used;
741 buf += audio->in[audio->read_next].used;
742 audio->in[audio->read_next].used = 0;
743 if ((++audio->read_next) == audio->pcm_buf_count)
744 audio->read_next = 0;
745 if (audio->in[audio->read_next].used == 0)
746 break; /* No data ready at this moment
747 * Exit while loop to prevent
748 * output thread sleep too long
749 */
750 }
751 }
752
753 /* don't feed output buffer to HW decoder during flushing
754 * buffer refresh command will be sent once flush completes
755 * send buf refresh command here can confuse HW decoder
756 */
757 if (audio->buf_refresh && !audio->rflush) {
758 audio->buf_refresh = 0;
759 pr_info("audio_read: kick start pcm feedback again\n");
760 audplay_buffer_refresh(audio);
761 }
762
763 mutex_unlock(&audio->read_lock);
764
765 if (buf > start)
766 rc = buf - start;
767
768 pr_info("audio_read: read %d bytes\n", rc);
769 return rc;
770 }
771
772 static ssize_t audio_write(struct file *file, const char __user *buf,
773 size_t count, loff_t *pos)
774 {
775 struct audio *audio = file->private_data;
776 const char __user *start = buf;
777 struct buffer *frame;
778 size_t xfer;
779 char *cpy_ptr;
780 int rc = 0;
781 unsigned dsize;
782
783 mutex_lock(&audio->write_lock);
784 while (count > 0) {
785 frame = audio->out + audio->out_head;
786 cpy_ptr = frame->data;
787 dsize = 0;
788 rc = wait_event_interruptible(audio->write_wait,
789 (frame->used == 0)
790 || (audio->stopped)
791 || (audio->wflush));
792 if (rc < 0)
793 break;
794 if (audio->stopped || audio->wflush) {
795 rc = -EBUSY;
796 break;
797 }
798
799 if (audio->reserved) {
800 dprintk("%s: append reserved byte %x\n",
801 __func__, audio->rsv_byte);
802 *cpy_ptr = audio->rsv_byte;
803 xfer = (count > (frame->size - 1)) ?
804 frame->size - 1 : count;
805 cpy_ptr++;
806 dsize = 1;
807 audio->reserved = 0;
808 } else
809 xfer = (count > frame->size) ? frame->size : count;
810
811 if (copy_from_user(cpy_ptr, buf, xfer)) {
812 rc = -EFAULT;
813 break;
814 }
815
816 dsize += xfer;
817 if (dsize & 1) {
818 audio->rsv_byte = ((char *) frame->data)[dsize - 1];
819 dprintk("%s: odd length buf reserve last byte %x\n",
820 __func__, audio->rsv_byte);
821 audio->reserved = 1;
822 dsize--;
823 }
824 count -= xfer;
825 buf += xfer;
826
827 if (dsize > 0) {
828 audio->out_head ^= 1;
829 frame->used = dsize;
830 audplay_send_data(audio, 0);
831 }
832 }
833 mutex_unlock(&audio->write_lock);
834 if (buf > start)
835 return buf - start;
836 return rc;
837 }
838
839 static int audio_release(struct inode *inode, struct file *file)
840 {
841 struct audio *audio = file->private_data;
842
843 dprintk("audio_release()\n");
844
845 mutex_lock(&audio->lock);
846 audio_disable(audio);
847 audio_flush(audio);
848 audio_flush_pcm_buf(audio);
849 msm_adsp_put(audio->audplay);
850 audio->audplay = NULL;
851 audio->opened = 0;
852 audio->reserved = 0;
853 dma_free_coherent(NULL, audio->out_dma_sz, audio->data, audio->phys);
854 audio->data = NULL;
855 if (audio->read_data != NULL) {
856 dma_free_coherent(NULL,
857 audio->in[0].size * audio->pcm_buf_count,
858 audio->read_data, audio->read_phys);
859 audio->read_data = NULL;
860 }
861 audio->pcm_feedback = 0;
862 mutex_unlock(&audio->lock);
863 return 0;
864 }
865
866 static struct audio the_mp3_audio;
867
868 static int audio_open(struct inode *inode, struct file *file)
869 {
870 struct audio *audio = &the_mp3_audio;
871 int rc;
872 unsigned pmem_sz;
873
874 mutex_lock(&audio->lock);
875
876 if (audio->opened) {
877 pr_err("audio: busy\n");
878 rc = -EBUSY;
879 goto done;
880 }
881
882 pmem_sz = DMASZ_MAX;
883
884 while (pmem_sz >= DMASZ_MIN) {
885 audio->data = dma_alloc_coherent(NULL, pmem_sz,
886 &audio->phys, GFP_KERNEL);
887 if (audio->data)
888 break;
889 else if (pmem_sz == DMASZ_MIN) {
890 pr_err("audio: could not allocate DMA buffers\n");
891 rc = -ENOMEM;
892 goto done;
893 } else
894 pmem_sz >>= 1;
895 }
896
897 dprintk("%s: allocated %d bytes DMA buffer\n", __func__, pmem_sz);
898
899 rc = audmgr_open(&audio->audmgr);
900 if (rc) {
901 dma_free_coherent(NULL, pmem_sz,
902 audio->data, audio->phys);
903 goto done;
904 }
905
906 rc = msm_adsp_get("AUDPLAY0TASK", &audio->audplay, &audplay_adsp_ops,
907 audio);
908 if (rc) {
909 pr_err("audio: failed to get audplay0 dsp module\n");
910 dma_free_coherent(NULL, pmem_sz,
911 audio->data, audio->phys);
912 audmgr_close(&audio->audmgr);
913 goto done;
914 }
915
916 audio->out_dma_sz = pmem_sz;
917 pmem_sz >>= 1; /* Shift by 1 to get size of ping pong buffer */
918
919 audio->out_sample_rate = 44100;
920 audio->out_channel_mode = AUDPP_CMD_PCM_INTF_STEREO_V;
921 audio->dec_id = 0;
922
923 audio->out[0].data = audio->data + 0;
924 audio->out[0].addr = audio->phys + 0;
925 audio->out[0].size = pmem_sz;
926
927 audio->out[1].data = audio->data + pmem_sz;
928 audio->out[1].addr = audio->phys + pmem_sz;
929 audio->out[1].size = pmem_sz;
930
931 audio->volume = 0x2000; /* equal to Q13 number 1.0 Unit Gain */
932
933 audio_flush(audio);
934
935 file->private_data = audio;
936 audio->opened = 1;
937 rc = 0;
938 done:
939 mutex_unlock(&audio->lock);
940 return rc;
941 }
942
943 static struct file_operations audio_mp3_fops = {
944 .owner = THIS_MODULE,
945 .open = audio_open,
946 .release = audio_release,
947 .read = audio_read,
948 .write = audio_write,
949 .unlocked_ioctl = audio_ioctl,
950 };
951
952 struct miscdevice audio_mp3_misc = {
953 .minor = MISC_DYNAMIC_MINOR,
954 .name = "msm_mp3",
955 .fops = &audio_mp3_fops,
956 };
957
958 static int __init audio_init(void)
959 {
960 mutex_init(&the_mp3_audio.lock);
961 mutex_init(&the_mp3_audio.write_lock);
962 mutex_init(&the_mp3_audio.read_lock);
963 spin_lock_init(&the_mp3_audio.dsp_lock);
964 init_waitqueue_head(&the_mp3_audio.write_wait);
965 init_waitqueue_head(&the_mp3_audio.read_wait);
966 the_mp3_audio.read_data = NULL;
967 return misc_register(&audio_mp3_misc);
968 }
969
970 device_initcall(audio_init);