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