linux/drivers/staging/dream/qdsp5/audio_evrc.c
<<
>>
Prefs
   1/* arch/arm/mach-msm/audio_evrc.c
   2 *
   3 * Copyright (c) 2008 QUALCOMM USA, INC.
   4 *
   5 * This code also borrows from audio_aac.c, which is
   6 * Copyright (C) 2008 Google, Inc.
   7 * Copyright (C) 2008 HTC Corporation
   8 *
   9 * This software is licensed under the terms of the GNU General Public
  10 * License version 2, as published by the Free Software Foundation, and
  11 * may be copied, distributed, and modified under those terms.
  12 *
  13 * This program is distributed in the hope that it will be useful,
  14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16 *
  17 * See the GNU General Public License for more details.
  18 * You should have received a copy of the GNU General Public License
  19 * along with this program; if not, you can find it at http://www.fsf.org.
  20 */
  21
  22#include <linux/module.h>
  23#include <linux/fs.h>
  24#include <linux/miscdevice.h>
  25#include <linux/uaccess.h>
  26#include <linux/kthread.h>
  27#include <linux/wait.h>
  28#include <linux/dma-mapping.h>
  29#include <linux/delay.h>
  30
  31#include <asm/atomic.h>
  32#include <asm/ioctls.h>
  33#include <mach/msm_adsp.h>
  34#include <linux/msm_audio.h>
  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#include "adsp.h"
  43
  44#ifdef DEBUG
  45#define dprintk(format, arg...) \
  46        printk(KERN_DEBUG format, ## arg)
  47#else
  48#define dprintk(format, arg...) do {} while (0)
  49#endif
  50
  51/* Hold 30 packets of 24 bytes each*/
  52#define BUFSZ                   720
  53#define DMASZ                   (BUFSZ * 2)
  54
  55#define AUDDEC_DEC_EVRC         12
  56
  57#define PCM_BUFSZ_MIN           1600    /* 100ms worth of data */
  58#define PCM_BUF_MAX_COUNT       5
  59/* DSP only accepts 5 buffers at most
  60 * but support 2 buffers currently
  61 */
  62#define EVRC_DECODED_FRSZ       320     /* EVRC 20ms 8KHz mono PCM size */
  63
  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
  88        atomic_t out_bytes;
  89
  90        struct mutex lock;
  91        struct mutex write_lock;
  92        wait_queue_head_t write_wait;
  93
  94        /* Host PCM section */
  95        struct buffer in[PCM_BUF_MAX_COUNT];
  96        struct mutex read_lock;
  97        wait_queue_head_t read_wait;    /* Wait queue for read */
  98        char *read_data;        /* pointer to reader buffer */
  99        dma_addr_t read_phys;   /* physical address of reader buffer */
 100        uint8_t read_next;      /* index to input buffers to be read next */
 101        uint8_t fill_next;      /* index to buffer that DSP should be filling */
 102        uint8_t pcm_buf_count;  /* number of pcm buffer allocated */
 103        /* ---- End of Host PCM section */
 104
 105        struct msm_adsp_module *audplay;
 106        struct audmgr audmgr;
 107
 108        /* data allocated for various buffers */
 109        char *data;
 110        dma_addr_t phys;
 111
 112        uint8_t opened:1;
 113        uint8_t enabled:1;
 114        uint8_t running:1;
 115        uint8_t stopped:1;      /* set when stopped, cleared on flush */
 116        uint8_t pcm_feedback:1;
 117        uint8_t buf_refresh:1;
 118
 119        unsigned volume;
 120        uint16_t dec_id;
 121        uint32_t read_ptr_offset;
 122};
 123static struct audio the_evrc_audio;
 124
 125static int auddec_dsp_config(struct audio *audio, int enable);
 126static void audpp_cmd_cfg_adec_params(struct audio *audio);
 127static void audpp_cmd_cfg_routing_mode(struct audio *audio);
 128static void audevrc_send_data(struct audio *audio, unsigned needed);
 129static void audevrc_dsp_event(void *private, unsigned id, uint16_t *msg);
 130static void audevrc_config_hostpcm(struct audio *audio);
 131static void audevrc_buffer_refresh(struct audio *audio);
 132
 133/* must be called with audio->lock held */
 134static int audevrc_enable(struct audio *audio)
 135{
 136        struct audmgr_config cfg;
 137        int rc;
 138
 139        if (audio->enabled)
 140                return 0;
 141
 142        audio->out_tail = 0;
 143        audio->out_needed = 0;
 144
 145        cfg.tx_rate = RPC_AUD_DEF_SAMPLE_RATE_NONE;
 146        cfg.rx_rate = RPC_AUD_DEF_SAMPLE_RATE_48000;
 147        cfg.def_method = RPC_AUD_DEF_METHOD_PLAYBACK;
 148        cfg.codec = RPC_AUD_DEF_CODEC_EVRC;
 149        cfg.snd_method = RPC_SND_METHOD_MIDI;
 150
 151        rc = audmgr_enable(&audio->audmgr, &cfg);
 152        if (rc < 0)
 153                return rc;
 154
 155        if (msm_adsp_enable(audio->audplay)) {
 156                pr_err("audio: msm_adsp_enable(audplay) failed\n");
 157                audmgr_disable(&audio->audmgr);
 158                return -ENODEV;
 159        }
 160
 161        if (audpp_enable(audio->dec_id, audevrc_dsp_event, audio)) {
 162                pr_err("audio: audpp_enable() failed\n");
 163                msm_adsp_disable(audio->audplay);
 164                audmgr_disable(&audio->audmgr);
 165                return -ENODEV;
 166        }
 167        audio->enabled = 1;
 168        return 0;
 169}
 170
 171/* must be called with audio->lock held */
 172static int audevrc_disable(struct audio *audio)
 173{
 174        if (audio->enabled) {
 175                audio->enabled = 0;
 176                auddec_dsp_config(audio, 0);
 177                wake_up(&audio->write_wait);
 178                wake_up(&audio->read_wait);
 179                msm_adsp_disable(audio->audplay);
 180                audpp_disable(audio->dec_id, audio);
 181                audmgr_disable(&audio->audmgr);
 182                audio->out_needed = 0;
 183        }
 184        return 0;
 185}
 186
 187/* ------------------- dsp --------------------- */
 188
 189static void audevrc_update_pcm_buf_entry(struct audio *audio,
 190                                         uint32_t *payload)
 191{
 192        uint8_t index;
 193        unsigned long flags;
 194
 195        spin_lock_irqsave(&audio->dsp_lock, flags);
 196        for (index = 0; index < payload[1]; index++) {
 197                if (audio->in[audio->fill_next].addr
 198                                == payload[2 + index * 2]) {
 199                        dprintk("audevrc_update_pcm_buf_entry: in[%d] ready\n",
 200                                audio->fill_next);
 201                        audio->in[audio->fill_next].used =
 202                                payload[3 + index * 2];
 203                        if ((++audio->fill_next) == audio->pcm_buf_count)
 204                                audio->fill_next = 0;
 205
 206                } else {
 207                        pr_err
 208                        ("audevrc_update_pcm_buf_entry: expected=%x ret=%x\n",
 209                                audio->in[audio->fill_next].addr,
 210                                payload[1 + index * 2]);
 211                        break;
 212                }
 213        }
 214        if (audio->in[audio->fill_next].used == 0) {
 215                audevrc_buffer_refresh(audio);
 216        } else {
 217                dprintk("audevrc_update_pcm_buf_entry: read cannot keep up\n");
 218                audio->buf_refresh = 1;
 219        }
 220
 221        spin_unlock_irqrestore(&audio->dsp_lock, flags);
 222        wake_up(&audio->read_wait);
 223}
 224
 225static void audplay_dsp_event(void *data, unsigned id, size_t len,
 226                              void (*getevent) (void *ptr, size_t len))
 227{
 228        struct audio *audio = data;
 229        uint32_t msg[28];
 230        getevent(msg, sizeof(msg));
 231
 232        dprintk("audplay_dsp_event: msg_id=%x\n", id);
 233        switch (id) {
 234        case AUDPLAY_MSG_DEC_NEEDS_DATA:
 235                audevrc_send_data(audio, 1);
 236                break;
 237        case AUDPLAY_MSG_BUFFER_UPDATE:
 238                dprintk("audevrc_update_pcm_buf_entry:======> \n");
 239                audevrc_update_pcm_buf_entry(audio, msg);
 240                break;
 241        default:
 242                pr_err("unexpected message from decoder \n");
 243        }
 244}
 245
 246static void audevrc_dsp_event(void *private, unsigned id, uint16_t *msg)
 247{
 248        struct audio *audio = private;
 249
 250        switch (id) {
 251        case AUDPP_MSG_STATUS_MSG:{
 252                        unsigned status = msg[1];
 253
 254                        switch (status) {
 255                        case AUDPP_DEC_STATUS_SLEEP:
 256                                dprintk("decoder status: sleep \n");
 257                                break;
 258
 259                        case AUDPP_DEC_STATUS_INIT:
 260                                dprintk("decoder status: init \n");
 261                                audpp_cmd_cfg_routing_mode(audio);
 262                                break;
 263
 264                        case AUDPP_DEC_STATUS_CFG:
 265                                dprintk("decoder status: cfg \n");
 266                                break;
 267                        case AUDPP_DEC_STATUS_PLAY:
 268                                dprintk("decoder status: play \n");
 269                                if (audio->pcm_feedback) {
 270                                        audevrc_config_hostpcm(audio);
 271                                        audevrc_buffer_refresh(audio);
 272                                }
 273                                break;
 274                        default:
 275                                pr_err("unknown decoder status \n");
 276                        }
 277                        break;
 278                }
 279        case AUDPP_MSG_CFG_MSG:
 280                if (msg[0] == AUDPP_MSG_ENA_ENA) {
 281                        dprintk("audevrc_dsp_event: CFG_MSG ENABLE\n");
 282                        auddec_dsp_config(audio, 1);
 283                        audio->out_needed = 0;
 284                        audio->running = 1;
 285                        audpp_set_volume_and_pan(audio->dec_id, audio->volume,
 286                                                 0);
 287                        audpp_avsync(audio->dec_id, 22050);
 288                } else if (msg[0] == AUDPP_MSG_ENA_DIS) {
 289                        dprintk("audevrc_dsp_event: CFG_MSG DISABLE\n");
 290                        audpp_avsync(audio->dec_id, 0);
 291                        audio->running = 0;
 292                } else {
 293                        pr_err("audevrc_dsp_event: CFG_MSG %d?\n", msg[0]);
 294                }
 295                break;
 296        case AUDPP_MSG_ROUTING_ACK:
 297                dprintk("audevrc_dsp_event: ROUTING_ACK\n");
 298                audpp_cmd_cfg_adec_params(audio);
 299                break;
 300
 301        default:
 302                pr_err("audevrc_dsp_event: UNKNOWN (%d)\n", id);
 303        }
 304
 305}
 306
 307struct msm_adsp_ops audplay_adsp_ops_evrc = {
 308        .event = audplay_dsp_event,
 309};
 310
 311#define audplay_send_queue0(audio, cmd, len) \
 312        msm_adsp_write(audio->audplay, QDSP_uPAudPlay0BitStreamCtrlQueue, \
 313                       cmd, len)
 314
 315static int auddec_dsp_config(struct audio *audio, int enable)
 316{
 317        audpp_cmd_cfg_dec_type cmd;
 318
 319        memset(&cmd, 0, sizeof(cmd));
 320        cmd.cmd_id = AUDPP_CMD_CFG_DEC_TYPE;
 321        if (enable)
 322                cmd.dec0_cfg = AUDPP_CMD_UPDATDE_CFG_DEC |
 323                    AUDPP_CMD_ENA_DEC_V | AUDDEC_DEC_EVRC;
 324        else
 325                cmd.dec0_cfg = AUDPP_CMD_UPDATDE_CFG_DEC | AUDPP_CMD_DIS_DEC_V;
 326
 327        return audpp_send_queue1(&cmd, sizeof(cmd));
 328}
 329
 330static void audpp_cmd_cfg_adec_params(struct audio *audio)
 331{
 332        struct audpp_cmd_cfg_adec_params_evrc cmd;
 333
 334        memset(&cmd, 0, sizeof(cmd));
 335        cmd.common.cmd_id = AUDPP_CMD_CFG_ADEC_PARAMS;
 336        cmd.common.length = sizeof(cmd);
 337        cmd.common.dec_id = audio->dec_id;
 338        cmd.common.input_sampling_frequency = 8000;
 339        cmd.stereo_cfg = AUDPP_CMD_PCM_INTF_MONO_V;
 340
 341        audpp_send_queue2(&cmd, sizeof(cmd));
 342}
 343
 344static void audpp_cmd_cfg_routing_mode(struct audio *audio)
 345{
 346        struct audpp_cmd_routing_mode cmd;
 347        dprintk("audpp_cmd_cfg_routing_mode()\n");
 348        memset(&cmd, 0, sizeof(cmd));
 349        cmd.cmd_id = AUDPP_CMD_ROUTING_MODE;
 350        cmd.object_number = audio->dec_id;
 351        if (audio->pcm_feedback)
 352                cmd.routing_mode = ROUTING_MODE_FTRT;
 353        else
 354                cmd.routing_mode = ROUTING_MODE_RT;
 355
 356        audpp_send_queue1(&cmd, sizeof(cmd));
 357}
 358
 359static int audplay_dsp_send_data_avail(struct audio *audio,
 360                                       unsigned idx, unsigned len)
 361{
 362        audplay_cmd_bitstream_data_avail cmd;
 363
 364        cmd.cmd_id = AUDPLAY_CMD_BITSTREAM_DATA_AVAIL;
 365        cmd.decoder_id = audio->dec_id;
 366        cmd.buf_ptr = audio->out[idx].addr;
 367        cmd.buf_size = len / 2;
 368        cmd.partition_number = 0;
 369        return audplay_send_queue0(audio, &cmd, sizeof(cmd));
 370}
 371
 372static void audevrc_buffer_refresh(struct audio *audio)
 373{
 374        struct audplay_cmd_buffer_refresh refresh_cmd;
 375
 376        refresh_cmd.cmd_id = AUDPLAY_CMD_BUFFER_REFRESH;
 377        refresh_cmd.num_buffers = 1;
 378        refresh_cmd.buf0_address = audio->in[audio->fill_next].addr;
 379        refresh_cmd.buf0_length = audio->in[audio->fill_next].size;
 380
 381        refresh_cmd.buf_read_count = 0;
 382        dprintk("audplay_buffer_fresh: buf0_addr=%x buf0_len=%d\n",
 383                refresh_cmd.buf0_address, refresh_cmd.buf0_length);
 384        audplay_send_queue0(audio, &refresh_cmd, sizeof(refresh_cmd));
 385}
 386
 387static void audevrc_config_hostpcm(struct audio *audio)
 388{
 389        struct audplay_cmd_hpcm_buf_cfg cfg_cmd;
 390
 391        dprintk("audevrc_config_hostpcm()\n");
 392        cfg_cmd.cmd_id = AUDPLAY_CMD_HPCM_BUF_CFG;
 393        cfg_cmd.max_buffers = 1;
 394        cfg_cmd.byte_swap = 0;
 395        cfg_cmd.hostpcm_config = (0x8000) | (0x4000);
 396        cfg_cmd.feedback_frequency = 1;
 397        cfg_cmd.partition_number = 0;
 398        audplay_send_queue0(audio, &cfg_cmd, sizeof(cfg_cmd));
 399
 400}
 401
 402static void audevrc_send_data(struct audio *audio, unsigned needed)
 403{
 404        struct buffer *frame;
 405        unsigned long flags;
 406
 407        spin_lock_irqsave(&audio->dsp_lock, flags);
 408        if (!audio->running)
 409                goto done;
 410
 411        if (needed) {
 412                /* We were called from the callback because the DSP
 413                 * requested more data.  Note that the DSP does want
 414                 * more data, and if a buffer was in-flight, mark it
 415                 * as available (since the DSP must now be done with
 416                 * it).
 417                 */
 418                audio->out_needed = 1;
 419                frame = audio->out + audio->out_tail;
 420                if (frame->used == 0xffffffff) {
 421                        dprintk("frame %d free\n", audio->out_tail);
 422                        frame->used = 0;
 423                        audio->out_tail ^= 1;
 424                        wake_up(&audio->write_wait);
 425                }
 426        }
 427
 428        if (audio->out_needed) {
 429                /* If the DSP currently wants data and we have a
 430                 * buffer available, we will send it and reset
 431                 * the needed flag.  We'll mark the buffer as in-flight
 432                 * so that it won't be recycled until the next buffer
 433                 * is requested
 434                 */
 435
 436                frame = audio->out + audio->out_tail;
 437                if (frame->used) {
 438                        BUG_ON(frame->used == 0xffffffff);
 439                        dprintk("frame %d busy\n", audio->out_tail);
 440                        audplay_dsp_send_data_avail(audio, audio->out_tail,
 441                                                    frame->used);
 442                        frame->used = 0xffffffff;
 443                        audio->out_needed = 0;
 444                }
 445        }
 446done:
 447        spin_unlock_irqrestore(&audio->dsp_lock, flags);
 448}
 449
 450/* ------------------- device --------------------- */
 451
 452static void audevrc_flush(struct audio *audio)
 453{
 454        audio->out[0].used = 0;
 455        audio->out[1].used = 0;
 456        audio->out_head = 0;
 457        audio->out_tail = 0;
 458        audio->stopped = 0;
 459        atomic_set(&audio->out_bytes, 0);
 460}
 461
 462static void audevrc_flush_pcm_buf(struct audio *audio)
 463{
 464        uint8_t index;
 465
 466        for (index = 0; index < PCM_BUF_MAX_COUNT; index++)
 467                audio->in[index].used = 0;
 468
 469        audio->read_next = 0;
 470        audio->fill_next = 0;
 471}
 472
 473static long audevrc_ioctl(struct file *file, unsigned int cmd,
 474                          unsigned long arg)
 475{
 476        struct audio *audio = file->private_data;
 477        int rc = 0;
 478
 479        dprintk("audevrc_ioctl() cmd = %d\n", cmd);
 480
 481        if (cmd == AUDIO_GET_STATS) {
 482                struct msm_audio_stats stats;
 483                stats.byte_count = audpp_avsync_byte_count(audio->dec_id);
 484                stats.sample_count = audpp_avsync_sample_count(audio->dec_id);
 485                if (copy_to_user((void *)arg, &stats, sizeof(stats)))
 486                        return -EFAULT;
 487                return 0;
 488        }
 489        if (cmd == AUDIO_SET_VOLUME) {
 490                unsigned long flags;
 491                spin_lock_irqsave(&audio->dsp_lock, flags);
 492                audio->volume = arg;
 493                if (audio->running)
 494                        audpp_set_volume_and_pan(audio->dec_id, arg, 0);
 495                spin_unlock_irqrestore(&audio->dsp_lock, flags);
 496                return 0;
 497        }
 498        mutex_lock(&audio->lock);
 499        switch (cmd) {
 500        case AUDIO_START:
 501                rc = audevrc_enable(audio);
 502                break;
 503        case AUDIO_STOP:
 504                rc = audevrc_disable(audio);
 505                audio->stopped = 1;
 506                break;
 507        case AUDIO_SET_CONFIG:{
 508                        dprintk("AUDIO_SET_CONFIG not applicable \n");
 509                        break;
 510                }
 511        case AUDIO_GET_CONFIG:{
 512                        struct msm_audio_config config;
 513                        config.buffer_size = BUFSZ;
 514                        config.buffer_count = 2;
 515                        config.sample_rate = 8000;
 516                        config.channel_count = 1;
 517                        config.unused[0] = 0;
 518                        config.unused[1] = 0;
 519                        config.unused[2] = 0;
 520                        config.unused[3] = 0;
 521                        if (copy_to_user((void *)arg, &config, sizeof(config)))
 522                                rc = -EFAULT;
 523                        else
 524                                rc = 0;
 525                        break;
 526                }
 527        case AUDIO_GET_PCM_CONFIG:{
 528                        struct msm_audio_pcm_config config;
 529                        config.pcm_feedback = 0;
 530                        config.buffer_count = PCM_BUF_MAX_COUNT;
 531                        config.buffer_size = PCM_BUFSZ_MIN;
 532                        if (copy_to_user((void *)arg, &config, sizeof(config)))
 533                                rc = -EFAULT;
 534                        else
 535                                rc = 0;
 536                        break;
 537                }
 538        case AUDIO_SET_PCM_CONFIG:{
 539                        struct msm_audio_pcm_config config;
 540                        if (copy_from_user
 541                            (&config, (void *)arg, sizeof(config))) {
 542                                rc = -EFAULT;
 543                                break;
 544                        }
 545                        if ((config.buffer_count > PCM_BUF_MAX_COUNT) ||
 546                            (config.buffer_count == 1))
 547                                config.buffer_count = PCM_BUF_MAX_COUNT;
 548
 549                        if (config.buffer_size < PCM_BUFSZ_MIN)
 550                                config.buffer_size = PCM_BUFSZ_MIN;
 551
 552                        /* Check if pcm feedback is required */
 553                        if ((config.pcm_feedback) && (!audio->read_data)) {
 554                                dprintk("audevrc_ioctl: allocate PCM buf %d\n",
 555                                        config.buffer_count *
 556                                        config.buffer_size);
 557                                audio->read_data =
 558                                    dma_alloc_coherent(NULL,
 559                                                       config.buffer_size *
 560                                                       config.buffer_count,
 561                                                       &audio->read_phys,
 562                                                       GFP_KERNEL);
 563                                if (!audio->read_data) {
 564                                        pr_err
 565                                        ("audevrc_ioctl: no mem for pcm buf\n");
 566                                        rc = -1;
 567                                } else {
 568                                        uint8_t index;
 569                                        uint32_t offset = 0;
 570                                        audio->pcm_feedback = 1;
 571                                        audio->buf_refresh = 0;
 572                                        audio->pcm_buf_count =
 573                                            config.buffer_count;
 574                                        audio->read_next = 0;
 575                                        audio->fill_next = 0;
 576
 577                                        for (index = 0;
 578                                             index < config.buffer_count;
 579                                             index++) {
 580                                                audio->in[index].data =
 581                                                    audio->read_data + offset;
 582                                                audio->in[index].addr =
 583                                                    audio->read_phys + offset;
 584                                                audio->in[index].size =
 585                                                    config.buffer_size;
 586                                                audio->in[index].used = 0;
 587                                                offset += config.buffer_size;
 588                                        }
 589                                        rc = 0;
 590                                }
 591                        } else {
 592                                rc = 0;
 593                        }
 594                        break;
 595                }
 596        case AUDIO_PAUSE:
 597                dprintk("%s: AUDIO_PAUSE %ld\n", __func__, arg);
 598                rc = audpp_pause(audio->dec_id, (int) arg);
 599                break;
 600        default:
 601                rc = -EINVAL;
 602        }
 603        mutex_unlock(&audio->lock);
 604        return rc;
 605}
 606
 607static ssize_t audevrc_read(struct file *file, char __user *buf, size_t count,
 608                            loff_t *pos)
 609{
 610        struct audio *audio = file->private_data;
 611        const char __user *start = buf;
 612        int rc = 0;
 613        if (!audio->pcm_feedback) {
 614                return 0;
 615                /* PCM feedback is not enabled. Nothing to read */
 616        }
 617        mutex_lock(&audio->read_lock);
 618        dprintk("audevrc_read() \n");
 619        while (count > 0) {
 620                rc = wait_event_interruptible(audio->read_wait,
 621                                              (audio->in[audio->read_next].
 622                                               used > 0) || (audio->stopped));
 623                dprintk("audevrc_read() wait terminated \n");
 624                if (rc < 0)
 625                        break;
 626                if (audio->stopped) {
 627                        rc = -EBUSY;
 628                        break;
 629                }
 630                if (count < audio->in[audio->read_next].used) {
 631                        /* Read must happen in frame boundary. Since driver does
 632                         * not know frame size, read count must be greater or
 633                         * equal to size of PCM samples
 634                         */
 635                        dprintk("audevrc_read:read stop - partial frame\n");
 636                        break;
 637                } else {
 638                        dprintk("audevrc_read: read from in[%d]\n",
 639                                audio->read_next);
 640                        if (copy_to_user
 641                            (buf, audio->in[audio->read_next].data,
 642                             audio->in[audio->read_next].used)) {
 643                                pr_err("audevrc_read: invalid addr %x \n",
 644                                       (unsigned int)buf);
 645                                rc = -EFAULT;
 646                                break;
 647                        }
 648                        count -= audio->in[audio->read_next].used;
 649                        buf += audio->in[audio->read_next].used;
 650                        audio->in[audio->read_next].used = 0;
 651                        if ((++audio->read_next) == audio->pcm_buf_count)
 652                                audio->read_next = 0;
 653                        if (audio->in[audio->read_next].used == 0)
 654                                break;  /* No data ready at this moment
 655                                         * Exit while loop to prevent
 656                                         * output thread sleep too long
 657                                         */
 658
 659                }
 660        }
 661        if (audio->buf_refresh) {
 662                audio->buf_refresh = 0;
 663                dprintk("audevrc_read: kick start pcm feedback again\n");
 664                audevrc_buffer_refresh(audio);
 665        }
 666        mutex_unlock(&audio->read_lock);
 667        if (buf > start)
 668                rc = buf - start;
 669        dprintk("audevrc_read: read %d bytes\n", rc);
 670        return rc;
 671}
 672
 673static ssize_t audevrc_write(struct file *file, const char __user *buf,
 674                             size_t count, loff_t *pos)
 675{
 676        struct audio *audio = file->private_data;
 677        const char __user *start = buf;
 678        struct buffer *frame;
 679        size_t xfer;
 680        int rc = 0;
 681
 682        if (count & 1)
 683                return -EINVAL;
 684        mutex_lock(&audio->write_lock);
 685        dprintk("audevrc_write() \n");
 686        while (count > 0) {
 687                frame = audio->out + audio->out_head;
 688                rc = wait_event_interruptible(audio->write_wait,
 689                                              (frame->used == 0)
 690                                              || (audio->stopped));
 691                if (rc < 0)
 692                        break;
 693                if (audio->stopped) {
 694                        rc = -EBUSY;
 695                        break;
 696                }
 697                xfer = (count > frame->size) ? frame->size : count;
 698                if (copy_from_user(frame->data, buf, xfer)) {
 699                        rc = -EFAULT;
 700                        break;
 701                }
 702
 703                frame->used = xfer;
 704                audio->out_head ^= 1;
 705                count -= xfer;
 706                buf += xfer;
 707
 708                audevrc_send_data(audio, 0);
 709
 710        }
 711        mutex_unlock(&audio->write_lock);
 712        if (buf > start)
 713                return buf - start;
 714        return rc;
 715}
 716
 717static int audevrc_release(struct inode *inode, struct file *file)
 718{
 719        struct audio *audio = file->private_data;
 720
 721        dprintk("audevrc_release()\n");
 722
 723        mutex_lock(&audio->lock);
 724        audevrc_disable(audio);
 725        audevrc_flush(audio);
 726        audevrc_flush_pcm_buf(audio);
 727        msm_adsp_put(audio->audplay);
 728        audio->audplay = NULL;
 729        audio->opened = 0;
 730        dma_free_coherent(NULL, DMASZ, audio->data, audio->phys);
 731        audio->data = NULL;
 732        if (audio->read_data != NULL) {
 733                dma_free_coherent(NULL,
 734                                  audio->in[0].size * audio->pcm_buf_count,
 735                                  audio->read_data, audio->read_phys);
 736                audio->read_data = NULL;
 737        }
 738        audio->pcm_feedback = 0;
 739        mutex_unlock(&audio->lock);
 740        return 0;
 741}
 742
 743static struct audio the_evrc_audio;
 744
 745static int audevrc_open(struct inode *inode, struct file *file)
 746{
 747        struct audio *audio = &the_evrc_audio;
 748        int rc;
 749
 750        if (audio->opened) {
 751                pr_err("audio: busy\n");
 752                return -EBUSY;
 753        }
 754
 755        /* Acquire Lock */
 756        mutex_lock(&audio->lock);
 757
 758        if (!audio->data) {
 759                audio->data = dma_alloc_coherent(NULL, DMASZ,
 760                                                 &audio->phys, GFP_KERNEL);
 761                if (!audio->data) {
 762                        pr_err("audio: could not allocate DMA buffers\n");
 763                        rc = -ENOMEM;
 764                        goto dma_fail;
 765                }
 766        }
 767
 768        rc = audmgr_open(&audio->audmgr);
 769        if (rc)
 770                goto audmgr_fail;
 771
 772        rc = msm_adsp_get("AUDPLAY0TASK", &audio->audplay,
 773                          &audplay_adsp_ops_evrc, audio);
 774        if (rc) {
 775                pr_err("audio: failed to get audplay0 dsp module\n");
 776                goto adsp_fail;
 777        }
 778
 779        audio->dec_id = 0;
 780
 781        audio->out[0].data = audio->data + 0;
 782        audio->out[0].addr = audio->phys + 0;
 783        audio->out[0].size = BUFSZ;
 784
 785        audio->out[1].data = audio->data + BUFSZ;
 786        audio->out[1].addr = audio->phys + BUFSZ;
 787        audio->out[1].size = BUFSZ;
 788
 789        audio->volume = 0x3FFF;
 790
 791        audevrc_flush(audio);
 792
 793        audio->opened = 1;
 794        file->private_data = audio;
 795
 796        mutex_unlock(&audio->lock);
 797        return rc;
 798
 799adsp_fail:
 800        audmgr_close(&audio->audmgr);
 801audmgr_fail:
 802        dma_free_coherent(NULL, DMASZ, audio->data, audio->phys);
 803dma_fail:
 804        mutex_unlock(&audio->lock);
 805        return rc;
 806}
 807
 808static struct file_operations audio_evrc_fops = {
 809        .owner = THIS_MODULE,
 810        .open = audevrc_open,
 811        .release = audevrc_release,
 812        .read = audevrc_read,
 813        .write = audevrc_write,
 814        .unlocked_ioctl = audevrc_ioctl,
 815};
 816
 817struct miscdevice audio_evrc_misc = {
 818        .minor = MISC_DYNAMIC_MINOR,
 819        .name = "msm_evrc",
 820        .fops = &audio_evrc_fops,
 821};
 822
 823static int __init audevrc_init(void)
 824{
 825        mutex_init(&the_evrc_audio.lock);
 826        mutex_init(&the_evrc_audio.write_lock);
 827        mutex_init(&the_evrc_audio.read_lock);
 828        spin_lock_init(&the_evrc_audio.dsp_lock);
 829        init_waitqueue_head(&the_evrc_audio.write_wait);
 830        init_waitqueue_head(&the_evrc_audio.read_wait);
 831        the_evrc_audio.read_data = NULL;
 832        return misc_register(&audio_evrc_misc);
 833}
 834
 835static void __exit audevrc_exit(void)
 836{
 837        misc_deregister(&audio_evrc_misc);
 838}
 839
 840module_init(audevrc_init);
 841module_exit(audevrc_exit);
 842
 843MODULE_DESCRIPTION("MSM EVRC driver");
 844MODULE_LICENSE("GPL v2");
 845MODULE_AUTHOR("QUALCOMM Inc");
 846