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