linux/drivers/staging/dream/qdsp5/audio_mp3.c
<<
>>
Prefs
   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...) \
  47printk(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
  72struct buffer {
  73        void *data;
  74        unsigned size;
  75        unsigned used;          /* Input usage actual DSP produced PCM size  */
  76        unsigned addr;
  77};
  78
  79struct 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
 136static int auddec_dsp_config(struct audio *audio, int enable);
 137static void audpp_cmd_cfg_adec_params(struct audio *audio);
 138static void audpp_cmd_cfg_routing_mode(struct audio *audio);
 139static void audplay_send_data(struct audio *audio, unsigned needed);
 140static void audplay_config_hostpcm(struct audio *audio);
 141static void audplay_buffer_refresh(struct audio *audio);
 142static void audio_dsp_event(void *private, unsigned id, uint16_t *msg);
 143
 144/* must be called with audio->lock held */
 145static 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 */
 186static 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 --------------------- */
 203static 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
 242static 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
 266static 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
 337struct 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
 346static 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
 363static 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
 376static 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
 391static 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
 404static 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
 419static 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
 434static 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        }
 483done:
 484        spin_unlock_irqrestore(&audio->dsp_lock, flags);
 485}
 486
 487/* ------------------- device --------------------- */
 488
 489static 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
 499static 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
 510static 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
 526static 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 \
 654                                        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