Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1 | /* arch/arm/mach-msm/qdsp5/audio_out.c |
| 2 | * |
| 3 | * pcm audio output device |
| 4 | * |
| 5 | * Copyright (C) 2008 Google, Inc. |
| 6 | * Copyright (C) 2008 HTC Corporation |
| 7 | * |
| 8 | * This software is licensed under the terms of the GNU General Public |
| 9 | * License version 2, as published by the Free Software Foundation, and |
| 10 | * may be copied, distributed, and modified under those terms. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | */ |
| 18 | |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/fs.h> |
| 21 | #include <linux/miscdevice.h> |
| 22 | #include <linux/uaccess.h> |
| 23 | #include <linux/kthread.h> |
| 24 | #include <linux/wait.h> |
| 25 | #include <linux/dma-mapping.h> |
| 26 | #include <linux/debugfs.h> |
| 27 | #include <linux/delay.h> |
| 28 | #include <linux/wakelock.h> |
| 29 | |
| 30 | #include <linux/msm_audio.h> |
| 31 | |
| 32 | #include <asm/atomic.h> |
| 33 | #include <asm/ioctls.h> |
| 34 | #include <mach/msm_adsp.h> |
| 35 | |
| 36 | #include "audmgr.h" |
| 37 | |
| 38 | #include <mach/qdsp5/qdsp5audppcmdi.h> |
| 39 | #include <mach/qdsp5/qdsp5audppmsg.h> |
| 40 | |
| 41 | #include <mach/htc_pwrsink.h> |
| 42 | #include <mach/debug_mm.h> |
| 43 | |
| 44 | #include "evlog.h" |
| 45 | |
| 46 | #define LOG_AUDIO_EVENTS 1 |
| 47 | #define LOG_AUDIO_FAULTS 0 |
| 48 | |
| 49 | enum { |
| 50 | EV_NULL, |
| 51 | EV_OPEN, |
| 52 | EV_WRITE, |
| 53 | EV_RETURN, |
| 54 | EV_IOCTL, |
| 55 | EV_WRITE_WAIT, |
| 56 | EV_WAIT_EVENT, |
| 57 | EV_FILL_BUFFER, |
| 58 | EV_SEND_BUFFER, |
| 59 | EV_DSP_EVENT, |
| 60 | EV_ENABLE, |
| 61 | }; |
| 62 | |
| 63 | #if (LOG_AUDIO_EVENTS != 1) |
| 64 | static inline void LOG(unsigned id, unsigned arg) {} |
| 65 | #else |
| 66 | static const char *pcm_log_strings[] = { |
| 67 | "NULL", |
| 68 | "OPEN", |
| 69 | "WRITE", |
| 70 | "RETURN", |
| 71 | "IOCTL", |
| 72 | "WRITE_WAIT", |
| 73 | "WAIT_EVENT", |
| 74 | "FILL_BUFFER", |
| 75 | "SEND_BUFFER", |
| 76 | "DSP_EVENT", |
| 77 | "ENABLE", |
| 78 | }; |
| 79 | |
| 80 | DECLARE_LOG(pcm_log, 64, pcm_log_strings); |
| 81 | |
| 82 | static int __init _pcm_log_init(void) |
| 83 | { |
| 84 | return ev_log_init(&pcm_log); |
| 85 | } |
| 86 | module_init(_pcm_log_init); |
| 87 | |
| 88 | #define LOG(id,arg) ev_log_write(&pcm_log, id, arg) |
| 89 | #endif |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | #define BUFSZ (960 * 5) |
| 96 | #define DMASZ (BUFSZ * 2) |
| 97 | |
| 98 | #define COMMON_OBJ_ID 6 |
| 99 | |
| 100 | struct buffer { |
| 101 | void *data; |
| 102 | unsigned size; |
| 103 | unsigned used; |
| 104 | unsigned addr; |
| 105 | }; |
| 106 | |
| 107 | struct audio { |
| 108 | struct buffer out[2]; |
| 109 | |
| 110 | spinlock_t dsp_lock; |
| 111 | |
| 112 | uint8_t out_head; |
| 113 | uint8_t out_tail; |
| 114 | uint8_t out_needed; /* number of buffers the dsp is waiting for */ |
| 115 | |
| 116 | atomic_t out_bytes; |
| 117 | |
| 118 | struct mutex lock; |
| 119 | struct mutex write_lock; |
| 120 | wait_queue_head_t wait; |
| 121 | |
| 122 | /* configuration to use on next enable */ |
| 123 | uint32_t out_sample_rate; |
| 124 | uint32_t out_channel_mode; |
| 125 | uint32_t out_weight; |
| 126 | uint32_t out_buffer_size; |
| 127 | |
| 128 | struct audmgr audmgr; |
| 129 | |
| 130 | /* data allocated for various buffers */ |
| 131 | char *data; |
| 132 | dma_addr_t phys; |
| 133 | |
| 134 | int teos; /* valid only if tunnel mode & no data left for decoder */ |
| 135 | int opened; |
| 136 | int enabled; |
| 137 | int running; |
| 138 | int stopped; /* set when stopped, cleared on flush */ |
| 139 | |
| 140 | struct wake_lock wakelock; |
| 141 | struct wake_lock idlelock; |
| 142 | |
| 143 | audpp_cmd_cfg_object_params_volume vol_pan; |
| 144 | }; |
| 145 | |
| 146 | struct audio_copp { |
| 147 | int mbadrc_enable; |
| 148 | int mbadrc_needs_commit; |
| 149 | char *mbadrc_data; |
| 150 | dma_addr_t mbadrc_phys; |
| 151 | |
| 152 | audpp_cmd_cfg_object_params_mbadrc mbadrc; |
| 153 | |
| 154 | int eq_enable; |
| 155 | int eq_needs_commit; |
| 156 | audpp_cmd_cfg_object_params_eqalizer eq; |
| 157 | |
| 158 | int rx_iir_enable; |
| 159 | int rx_iir_needs_commit; |
| 160 | audpp_cmd_cfg_object_params_pcm iir; |
| 161 | |
| 162 | audpp_cmd_cfg_object_params_volume vol_pan; |
| 163 | |
| 164 | int qconcert_plus_enable; |
| 165 | int qconcert_plus_needs_commit; |
| 166 | audpp_cmd_cfg_object_params_qconcert qconcert_plus; |
| 167 | |
| 168 | int status; |
| 169 | int opened; |
| 170 | struct mutex lock; |
| 171 | |
| 172 | struct audpp_event_callback ecb; |
| 173 | } the_audio_copp; |
| 174 | |
| 175 | static void audio_prevent_sleep(struct audio *audio) |
| 176 | { |
| 177 | MM_DBG("\n"); /* Macro prints the file name and function */ |
| 178 | wake_lock(&audio->wakelock); |
| 179 | wake_lock(&audio->idlelock); |
| 180 | } |
| 181 | |
| 182 | static void audio_allow_sleep(struct audio *audio) |
| 183 | { |
| 184 | wake_unlock(&audio->wakelock); |
| 185 | wake_unlock(&audio->idlelock); |
| 186 | MM_DBG("\n"); /* Macro prints the file name and function */ |
| 187 | } |
| 188 | |
| 189 | static int audio_dsp_out_enable(struct audio *audio, int yes); |
| 190 | static int audio_dsp_send_buffer(struct audio *audio, unsigned id, unsigned len); |
| 191 | |
| 192 | static void audio_dsp_event(void *private, unsigned id, uint16_t *msg); |
| 193 | |
| 194 | /* must be called with audio->lock held */ |
| 195 | static int audio_enable(struct audio *audio) |
| 196 | { |
| 197 | struct audmgr_config cfg; |
| 198 | int rc; |
| 199 | |
| 200 | MM_DBG("\n"); /* Macro prints the file name and function */ |
| 201 | |
| 202 | if (audio->enabled) |
| 203 | return 0; |
| 204 | |
| 205 | /* refuse to start if we're not ready */ |
| 206 | if (!audio->out[0].used || !audio->out[1].used) |
| 207 | return -EIO; |
| 208 | |
| 209 | /* we start buffers 0 and 1, so buffer 0 will be the |
| 210 | * next one the dsp will want |
| 211 | */ |
| 212 | audio->out_tail = 0; |
| 213 | audio->out_needed = 0; |
| 214 | |
| 215 | cfg.tx_rate = RPC_AUD_DEF_SAMPLE_RATE_NONE; |
| 216 | cfg.rx_rate = RPC_AUD_DEF_SAMPLE_RATE_48000; |
| 217 | cfg.def_method = RPC_AUD_DEF_METHOD_HOST_PCM; |
| 218 | cfg.codec = RPC_AUD_DEF_CODEC_PCM; |
| 219 | cfg.snd_method = RPC_SND_METHOD_MIDI; |
| 220 | |
| 221 | audio_prevent_sleep(audio); |
| 222 | rc = audmgr_enable(&audio->audmgr, &cfg); |
| 223 | if (rc < 0) { |
| 224 | audio_allow_sleep(audio); |
| 225 | return rc; |
| 226 | } |
| 227 | |
| 228 | if (audpp_enable(-1, audio_dsp_event, audio)) { |
| 229 | MM_ERR("audpp_enable() failed\n"); |
| 230 | audmgr_disable(&audio->audmgr); |
| 231 | audio_allow_sleep(audio); |
| 232 | return -ENODEV; |
| 233 | } |
| 234 | |
| 235 | audio->enabled = 1; |
| 236 | htc_pwrsink_set(PWRSINK_AUDIO, 100); |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | /* must be called with audio->lock held */ |
| 241 | static int audio_disable(struct audio *audio) |
| 242 | { |
| 243 | MM_DBG("\n"); /* Macro prints the file name and function */ |
| 244 | if (audio->enabled) { |
| 245 | audio->enabled = 0; |
| 246 | audio_dsp_out_enable(audio, 0); |
| 247 | |
| 248 | audpp_disable(-1, audio); |
| 249 | |
| 250 | wake_up(&audio->wait); |
| 251 | audmgr_disable(&audio->audmgr); |
| 252 | audio->out_needed = 0; |
| 253 | audio_allow_sleep(audio); |
| 254 | } |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | void audio_commit_pending_pp_params(void *priv, unsigned id, uint16_t *msg) |
| 259 | { |
| 260 | struct audio_copp *audio_copp = priv; |
| 261 | |
| 262 | if (AUDPP_MSG_CFG_MSG == id && msg[0] == AUDPP_MSG_ENA_DIS) |
| 263 | return; |
| 264 | |
| 265 | if (!audio_copp->status) |
| 266 | return; |
| 267 | |
| 268 | audpp_dsp_set_mbadrc(COMMON_OBJ_ID, audio_copp->mbadrc_enable, |
| 269 | &audio_copp->mbadrc); |
| 270 | |
| 271 | audpp_dsp_set_eq(COMMON_OBJ_ID, audio_copp->eq_enable, |
| 272 | &audio_copp->eq); |
| 273 | |
| 274 | audpp_dsp_set_rx_iir(COMMON_OBJ_ID, audio_copp->rx_iir_enable, |
| 275 | &audio_copp->iir); |
| 276 | audpp_dsp_set_vol_pan(COMMON_OBJ_ID, &audio_copp->vol_pan); |
| 277 | |
| 278 | audpp_dsp_set_qconcert_plus(COMMON_OBJ_ID, |
| 279 | audio_copp->qconcert_plus_enable, |
| 280 | &audio_copp->qconcert_plus); |
| 281 | } |
| 282 | EXPORT_SYMBOL(audio_commit_pending_pp_params); |
| 283 | |
| 284 | /* ------------------- dsp --------------------- */ |
| 285 | static void audio_dsp_event(void *private, unsigned id, uint16_t *msg) |
| 286 | { |
| 287 | struct audio *audio = private; |
| 288 | struct buffer *frame; |
| 289 | unsigned long flags; |
| 290 | |
| 291 | LOG(EV_DSP_EVENT, id); |
| 292 | switch (id) { |
| 293 | case AUDPP_MSG_HOST_PCM_INTF_MSG: { |
| 294 | unsigned id = msg[2]; |
| 295 | unsigned idx = msg[3] - 1; |
| 296 | |
| 297 | /* MM_INFO("HOST_PCM id %d idx %d\n", id, idx); */ |
| 298 | if (id != AUDPP_MSG_HOSTPCM_ID_ARM_RX) { |
| 299 | MM_ERR("bogus id\n"); |
| 300 | break; |
| 301 | } |
| 302 | if (idx > 1) { |
| 303 | MM_ERR("bogus buffer idx\n"); |
| 304 | break; |
| 305 | } |
| 306 | |
| 307 | spin_lock_irqsave(&audio->dsp_lock, flags); |
| 308 | if (audio->running) { |
| 309 | atomic_add(audio->out[idx].used, &audio->out_bytes); |
| 310 | audio->out[idx].used = 0; |
| 311 | |
| 312 | frame = audio->out + audio->out_tail; |
| 313 | if (frame->used) { |
| 314 | audio_dsp_send_buffer( |
| 315 | audio, audio->out_tail, frame->used); |
| 316 | audio->out_tail ^= 1; |
| 317 | } else { |
| 318 | audio->out_needed++; |
| 319 | } |
| 320 | wake_up(&audio->wait); |
| 321 | } |
| 322 | spin_unlock_irqrestore(&audio->dsp_lock, flags); |
| 323 | break; |
| 324 | } |
| 325 | case AUDPP_MSG_PCMDMAMISSED: |
| 326 | MM_INFO("PCMDMAMISSED %d\n", msg[0]); |
| 327 | audio->teos = 1; |
| 328 | wake_up(&audio->wait); |
| 329 | break; |
| 330 | case AUDPP_MSG_CFG_MSG: |
| 331 | if (msg[0] == AUDPP_MSG_ENA_ENA) { |
| 332 | LOG(EV_ENABLE, 1); |
| 333 | MM_DBG("CFG_MSG ENABLE\n"); |
| 334 | audio->out_needed = 0; |
| 335 | audio->running = 1; |
| 336 | audpp_dsp_set_vol_pan(5, &audio->vol_pan); |
| 337 | audio_dsp_out_enable(audio, 1); |
| 338 | } else if (msg[0] == AUDPP_MSG_ENA_DIS) { |
| 339 | LOG(EV_ENABLE, 0); |
| 340 | MM_DBG("CFG_MSG DISABLE\n"); |
| 341 | audio->running = 0; |
| 342 | } else { |
| 343 | MM_ERR("CFG_MSG %d?\n", msg[0]); |
| 344 | } |
| 345 | break; |
| 346 | default: |
| 347 | MM_ERR("UNKNOWN (%d)\n", id); |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | static int audio_dsp_out_enable(struct audio *audio, int yes) |
| 352 | { |
| 353 | audpp_cmd_pcm_intf cmd; |
| 354 | |
| 355 | memset(&cmd, 0, sizeof(cmd)); |
| 356 | cmd.cmd_id = AUDPP_CMD_PCM_INTF_2; |
| 357 | cmd.object_num = AUDPP_CMD_PCM_INTF_OBJECT_NUM; |
| 358 | cmd.config = AUDPP_CMD_PCM_INTF_CONFIG_CMD_V; |
| 359 | cmd.intf_type = AUDPP_CMD_PCM_INTF_RX_ENA_ARMTODSP_V; |
| 360 | |
| 361 | if (yes) { |
| 362 | cmd.write_buf1LSW = audio->out[0].addr; |
| 363 | cmd.write_buf1MSW = audio->out[0].addr >> 16; |
| 364 | if (audio->out[0].used) |
| 365 | cmd.write_buf1_len = audio->out[0].used; |
| 366 | else |
| 367 | cmd.write_buf1_len = audio->out[0].size; |
| 368 | cmd.write_buf2LSW = audio->out[1].addr; |
| 369 | cmd.write_buf2MSW = audio->out[1].addr >> 16; |
| 370 | if (audio->out[1].used) |
| 371 | cmd.write_buf2_len = audio->out[1].used; |
| 372 | else |
| 373 | cmd.write_buf2_len = audio->out[1].size; |
| 374 | cmd.arm_to_rx_flag = AUDPP_CMD_PCM_INTF_ENA_V; |
| 375 | cmd.weight_decoder_to_rx = audio->out_weight; |
| 376 | cmd.weight_arm_to_rx = 1; |
| 377 | cmd.partition_number_arm_to_dsp = 0; |
| 378 | cmd.sample_rate = audio->out_sample_rate; |
| 379 | cmd.channel_mode = audio->out_channel_mode; |
| 380 | } |
| 381 | |
| 382 | return audpp_send_queue2(&cmd, sizeof(cmd)); |
| 383 | } |
| 384 | |
| 385 | static int audio_dsp_send_buffer(struct audio *audio, unsigned idx, unsigned len) |
| 386 | { |
| 387 | audpp_cmd_pcm_intf_send_buffer cmd; |
| 388 | |
| 389 | cmd.cmd_id = AUDPP_CMD_PCM_INTF_2; |
| 390 | cmd.host_pcm_object = AUDPP_CMD_PCM_INTF_OBJECT_NUM; |
| 391 | cmd.config = AUDPP_CMD_PCM_INTF_BUFFER_CMD_V; |
| 392 | cmd.intf_type = AUDPP_CMD_PCM_INTF_RX_ENA_ARMTODSP_V; |
| 393 | cmd.dsp_to_arm_buf_id = 0; |
| 394 | cmd.arm_to_dsp_buf_id = idx + 1; |
| 395 | cmd.arm_to_dsp_buf_len = len; |
| 396 | |
| 397 | LOG(EV_SEND_BUFFER, idx); |
| 398 | dma_coherent_pre_ops(); |
| 399 | return audpp_send_queue2(&cmd, sizeof(cmd)); |
| 400 | } |
| 401 | |
| 402 | /* ------------------- device --------------------- */ |
| 403 | |
| 404 | static int audio_enable_mbadrc(struct audio_copp *audio_copp, int enable) |
| 405 | { |
| 406 | if (audio_copp->mbadrc_enable == enable && |
| 407 | !audio_copp->mbadrc_needs_commit) |
| 408 | return 0; |
| 409 | |
| 410 | audio_copp->mbadrc_enable = enable; |
| 411 | if (is_audpp_enable()) { |
| 412 | audpp_dsp_set_mbadrc(COMMON_OBJ_ID, enable, |
| 413 | &audio_copp->mbadrc); |
| 414 | audio_copp->mbadrc_needs_commit = 0; |
| 415 | } |
| 416 | |
| 417 | return 0; |
| 418 | } |
| 419 | |
| 420 | static int audio_enable_eq(struct audio_copp *audio_copp, int enable) |
| 421 | { |
| 422 | if (audio_copp->eq_enable == enable && |
| 423 | !audio_copp->eq_needs_commit) |
| 424 | return 0; |
| 425 | |
| 426 | audio_copp->eq_enable = enable; |
| 427 | |
| 428 | if (is_audpp_enable()) { |
| 429 | audpp_dsp_set_eq(COMMON_OBJ_ID, enable, &audio_copp->eq); |
| 430 | audio_copp->eq_needs_commit = 0; |
| 431 | } |
| 432 | return 0; |
| 433 | } |
| 434 | |
| 435 | static int audio_enable_rx_iir(struct audio_copp *audio_copp, int enable) |
| 436 | { |
| 437 | if (audio_copp->rx_iir_enable == enable && |
| 438 | !audio_copp->rx_iir_needs_commit) |
| 439 | return 0; |
| 440 | |
| 441 | audio_copp->rx_iir_enable = enable; |
| 442 | |
| 443 | if (is_audpp_enable()) { |
| 444 | audpp_dsp_set_rx_iir(COMMON_OBJ_ID, enable, &audio_copp->iir); |
| 445 | audio_copp->rx_iir_needs_commit = 0; |
| 446 | } |
| 447 | return 0; |
| 448 | } |
| 449 | |
| 450 | static int audio_enable_vol_pan(struct audio_copp *audio_copp) |
| 451 | { |
| 452 | if (is_audpp_enable()) |
| 453 | audpp_dsp_set_vol_pan(COMMON_OBJ_ID, &audio_copp->vol_pan); |
| 454 | return 0; |
| 455 | } |
| 456 | |
| 457 | static int audio_enable_qconcert_plus(struct audio_copp *audio_copp, int enable) |
| 458 | { |
| 459 | if (audio_copp->qconcert_plus_enable == enable && |
| 460 | !audio_copp->qconcert_plus_needs_commit) |
| 461 | return 0; |
| 462 | |
| 463 | audio_copp->qconcert_plus_enable = enable; |
| 464 | |
| 465 | if (is_audpp_enable()) { |
| 466 | audpp_dsp_set_qconcert_plus(COMMON_OBJ_ID, enable, |
| 467 | &audio_copp->qconcert_plus); |
| 468 | audio_copp->qconcert_plus_needs_commit = 0; |
| 469 | } |
| 470 | return 0; |
| 471 | } |
| 472 | |
| 473 | static void audio_flush(struct audio *audio) |
| 474 | { |
| 475 | audio->out[0].used = 0; |
| 476 | audio->out[1].used = 0; |
| 477 | audio->out_head = 0; |
| 478 | audio->out_tail = 0; |
| 479 | audio->stopped = 0; |
| 480 | } |
| 481 | |
| 482 | static long audio_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 483 | { |
| 484 | struct audio *audio = file->private_data; |
| 485 | int rc = -EINVAL; |
| 486 | unsigned long flags = 0; |
| 487 | |
| 488 | if (cmd == AUDIO_GET_STATS) { |
| 489 | struct msm_audio_stats stats; |
| 490 | stats.byte_count = atomic_read(&audio->out_bytes); |
| 491 | if (copy_to_user((void*) arg, &stats, sizeof(stats))) |
| 492 | return -EFAULT; |
| 493 | return 0; |
| 494 | } |
| 495 | |
| 496 | switch (cmd) { |
| 497 | case AUDIO_SET_VOLUME: |
| 498 | spin_lock_irqsave(&audio->dsp_lock, flags); |
| 499 | audio->vol_pan.volume = arg; |
| 500 | if (audio->running) |
| 501 | audpp_dsp_set_vol_pan(5, &audio->vol_pan); |
| 502 | spin_unlock_irqrestore(&audio->dsp_lock, flags); |
| 503 | return 0; |
| 504 | |
| 505 | case AUDIO_SET_PAN: |
| 506 | spin_lock_irqsave(&audio->dsp_lock, flags); |
| 507 | audio->vol_pan.pan = arg; |
| 508 | if (audio->running) |
| 509 | audpp_dsp_set_vol_pan(5, &audio->vol_pan); |
| 510 | spin_unlock_irqrestore(&audio->dsp_lock, flags); |
| 511 | return 0; |
| 512 | } |
| 513 | |
| 514 | LOG(EV_IOCTL, cmd); |
| 515 | mutex_lock(&audio->lock); |
| 516 | switch (cmd) { |
| 517 | case AUDIO_START: |
| 518 | rc = audio_enable(audio); |
| 519 | break; |
| 520 | case AUDIO_STOP: |
| 521 | rc = audio_disable(audio); |
| 522 | audio->stopped = 1; |
| 523 | break; |
| 524 | case AUDIO_FLUSH: |
| 525 | if (audio->stopped) { |
| 526 | /* Make sure we're stopped and we wake any threads |
| 527 | * that might be blocked holding the write_lock. |
| 528 | * While audio->stopped write threads will always |
| 529 | * exit immediately. |
| 530 | */ |
| 531 | wake_up(&audio->wait); |
| 532 | mutex_lock(&audio->write_lock); |
| 533 | audio_flush(audio); |
| 534 | mutex_unlock(&audio->write_lock); |
| 535 | } |
| 536 | case AUDIO_SET_CONFIG: { |
| 537 | struct msm_audio_config config; |
| 538 | if (copy_from_user(&config, (void*) arg, sizeof(config))) { |
| 539 | rc = -EFAULT; |
| 540 | break; |
| 541 | } |
| 542 | if (config.channel_count == 1) { |
| 543 | config.channel_count = AUDPP_CMD_PCM_INTF_MONO_V; |
| 544 | } else if (config.channel_count == 2) { |
| 545 | config.channel_count= AUDPP_CMD_PCM_INTF_STEREO_V; |
| 546 | } else { |
| 547 | rc = -EINVAL; |
| 548 | break; |
| 549 | } |
| 550 | audio->out_sample_rate = config.sample_rate; |
| 551 | audio->out_channel_mode = config.channel_count; |
| 552 | rc = 0; |
| 553 | break; |
| 554 | } |
| 555 | case AUDIO_GET_CONFIG: { |
| 556 | struct msm_audio_config config; |
| 557 | config.buffer_size = BUFSZ; |
| 558 | config.buffer_count = 2; |
| 559 | config.sample_rate = audio->out_sample_rate; |
| 560 | if (audio->out_channel_mode == AUDPP_CMD_PCM_INTF_MONO_V) { |
| 561 | config.channel_count = 1; |
| 562 | } else { |
| 563 | config.channel_count = 2; |
| 564 | } |
| 565 | config.unused[0] = 0; |
| 566 | config.unused[1] = 0; |
| 567 | config.unused[2] = 0; |
| 568 | if (copy_to_user((void*) arg, &config, sizeof(config))) { |
| 569 | rc = -EFAULT; |
| 570 | } else { |
| 571 | rc = 0; |
| 572 | } |
| 573 | break; |
| 574 | } |
| 575 | default: |
| 576 | rc = -EINVAL; |
| 577 | } |
| 578 | mutex_unlock(&audio->lock); |
| 579 | return rc; |
| 580 | } |
| 581 | |
| 582 | /* Only useful in tunnel-mode */ |
| 583 | static int audio_fsync(struct file *file, int datasync) |
| 584 | { |
| 585 | struct audio *audio = file->private_data; |
| 586 | int rc = 0; |
| 587 | |
| 588 | if (!audio->running) |
| 589 | return -EINVAL; |
| 590 | |
| 591 | mutex_lock(&audio->write_lock); |
| 592 | |
| 593 | rc = wait_event_interruptible(audio->wait, |
| 594 | (!audio->out[0].used && |
| 595 | !audio->out[1].used)); |
| 596 | |
| 597 | if (rc < 0) |
| 598 | goto done; |
| 599 | |
| 600 | /* pcm dmamiss message is sent continously when |
| 601 | * decoder is starved so no race condition concern |
| 602 | */ |
| 603 | |
| 604 | audio->teos = 0; |
| 605 | |
| 606 | rc = wait_event_interruptible(audio->wait, |
| 607 | audio->teos); |
| 608 | |
| 609 | done: |
| 610 | mutex_unlock(&audio->write_lock); |
| 611 | return rc; |
| 612 | } |
| 613 | |
| 614 | static ssize_t audio_read(struct file *file, char __user *buf, size_t count, loff_t *pos) |
| 615 | { |
| 616 | return -EINVAL; |
| 617 | } |
| 618 | |
| 619 | static inline int rt_policy(int policy) |
| 620 | { |
| 621 | if (unlikely(policy == SCHED_FIFO) || unlikely(policy == SCHED_RR)) |
| 622 | return 1; |
| 623 | return 0; |
| 624 | } |
| 625 | |
| 626 | static inline int task_has_rt_policy(struct task_struct *p) |
| 627 | { |
| 628 | return rt_policy(p->policy); |
| 629 | } |
| 630 | |
| 631 | static ssize_t audio_write(struct file *file, const char __user *buf, |
| 632 | size_t count, loff_t *pos) |
| 633 | { |
| 634 | struct sched_param s = { .sched_priority = 1 }; |
| 635 | struct audio *audio = file->private_data; |
| 636 | unsigned long flags; |
| 637 | const char __user *start = buf; |
| 638 | struct buffer *frame; |
| 639 | size_t xfer; |
| 640 | int old_prio = current->rt_priority; |
| 641 | int old_policy = current->policy; |
| 642 | int cap_nice = cap_raised(current_cap(), CAP_SYS_NICE); |
| 643 | int rc = 0; |
| 644 | |
| 645 | LOG(EV_WRITE, count | (audio->running << 28) | (audio->stopped << 24)); |
| 646 | |
| 647 | /* just for this write, set us real-time */ |
| 648 | if (!task_has_rt_policy(current)) { |
| 649 | struct cred *new = prepare_creds(); |
| 650 | cap_raise(new->cap_effective, CAP_SYS_NICE); |
| 651 | commit_creds(new); |
| 652 | if ((sched_setscheduler(current, SCHED_RR, &s)) < 0) |
| 653 | MM_ERR("sched_setscheduler failed\n"); |
| 654 | } |
| 655 | |
| 656 | mutex_lock(&audio->write_lock); |
| 657 | while (count > 0) { |
| 658 | frame = audio->out + audio->out_head; |
| 659 | |
| 660 | LOG(EV_WAIT_EVENT, 0); |
| 661 | rc = wait_event_interruptible(audio->wait, |
| 662 | (frame->used == 0) || (audio->stopped)); |
| 663 | LOG(EV_WAIT_EVENT, 1); |
| 664 | |
| 665 | if (rc < 0) |
| 666 | break; |
| 667 | if (audio->stopped) { |
| 668 | rc = -EBUSY; |
| 669 | break; |
| 670 | } |
| 671 | xfer = count > frame->size ? frame->size : count; |
| 672 | if (copy_from_user(frame->data, buf, xfer)) { |
| 673 | rc = -EFAULT; |
| 674 | break; |
| 675 | } |
| 676 | frame->used = xfer; |
| 677 | audio->out_head ^= 1; |
| 678 | count -= xfer; |
| 679 | buf += xfer; |
| 680 | |
| 681 | spin_lock_irqsave(&audio->dsp_lock, flags); |
| 682 | LOG(EV_FILL_BUFFER, audio->out_head ^ 1); |
| 683 | frame = audio->out + audio->out_tail; |
| 684 | if (frame->used && audio->out_needed) { |
| 685 | audio_dsp_send_buffer(audio, audio->out_tail, frame->used); |
| 686 | audio->out_tail ^= 1; |
| 687 | audio->out_needed--; |
| 688 | } |
| 689 | spin_unlock_irqrestore(&audio->dsp_lock, flags); |
| 690 | } |
| 691 | |
| 692 | mutex_unlock(&audio->write_lock); |
| 693 | |
| 694 | /* restore scheduling policy and priority */ |
| 695 | if (!rt_policy(old_policy)) { |
| 696 | struct sched_param v = { .sched_priority = old_prio }; |
| 697 | if ((sched_setscheduler(current, old_policy, &v)) < 0) |
| 698 | MM_ERR("sched_setscheduler failed\n"); |
| 699 | if (likely(!cap_nice)) { |
| 700 | struct cred *new = prepare_creds(); |
| 701 | cap_lower(new->cap_effective, CAP_SYS_NICE); |
| 702 | commit_creds(new); |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | LOG(EV_RETURN,(buf > start) ? (buf - start) : rc); |
| 707 | if (buf > start) |
| 708 | return buf - start; |
| 709 | return rc; |
| 710 | } |
| 711 | |
| 712 | static int audio_release(struct inode *inode, struct file *file) |
| 713 | { |
| 714 | struct audio *audio = file->private_data; |
| 715 | |
| 716 | LOG(EV_OPEN, 0); |
| 717 | mutex_lock(&audio->lock); |
| 718 | audio_disable(audio); |
| 719 | audio_flush(audio); |
| 720 | audio->opened = 0; |
| 721 | mutex_unlock(&audio->lock); |
| 722 | htc_pwrsink_set(PWRSINK_AUDIO, 0); |
| 723 | return 0; |
| 724 | } |
| 725 | |
| 726 | struct audio the_audio; |
| 727 | |
| 728 | static int audio_open(struct inode *inode, struct file *file) |
| 729 | { |
| 730 | struct audio *audio = &the_audio; |
| 731 | int rc; |
| 732 | |
| 733 | mutex_lock(&audio->lock); |
| 734 | |
| 735 | if (audio->opened) { |
| 736 | MM_ERR("busy\n"); |
| 737 | rc = -EBUSY; |
| 738 | goto done; |
| 739 | } |
| 740 | |
| 741 | if (!audio->data) { |
| 742 | audio->data = dma_alloc_coherent(NULL, DMASZ, |
| 743 | &audio->phys, GFP_KERNEL); |
| 744 | if (!audio->data) { |
| 745 | MM_ERR("could not allocate DMA buffers\n"); |
| 746 | rc = -ENOMEM; |
| 747 | goto done; |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | rc = audmgr_open(&audio->audmgr); |
| 752 | if (rc) |
| 753 | goto done; |
| 754 | |
| 755 | audio->out_buffer_size = BUFSZ; |
| 756 | audio->out_sample_rate = 44100; |
| 757 | audio->out_channel_mode = AUDPP_CMD_PCM_INTF_STEREO_V; |
| 758 | audio->out_weight = 100; |
| 759 | |
| 760 | audio->out[0].data = audio->data + 0; |
| 761 | audio->out[0].addr = audio->phys + 0; |
| 762 | audio->out[0].size = BUFSZ; |
| 763 | |
| 764 | audio->out[1].data = audio->data + BUFSZ; |
| 765 | audio->out[1].addr = audio->phys + BUFSZ; |
| 766 | audio->out[1].size = BUFSZ; |
| 767 | |
| 768 | audio->vol_pan.volume = 0x2000; |
| 769 | audio->vol_pan.pan = 0x0; |
| 770 | |
| 771 | audio_flush(audio); |
| 772 | |
| 773 | file->private_data = audio; |
| 774 | audio->opened = 1; |
| 775 | rc = 0; |
| 776 | LOG(EV_OPEN, 1); |
| 777 | done: |
| 778 | mutex_unlock(&audio->lock); |
| 779 | return rc; |
| 780 | } |
| 781 | |
| 782 | static long audpp_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 783 | { |
| 784 | struct audio_copp *audio_copp = file->private_data; |
| 785 | int rc = 0, enable; |
| 786 | uint16_t enable_mask; |
| 787 | int prev_state; |
| 788 | |
| 789 | mutex_lock(&audio_copp->lock); |
| 790 | switch (cmd) { |
| 791 | case AUDIO_ENABLE_AUDPP: |
| 792 | if (copy_from_user(&enable_mask, (void *) arg, |
| 793 | sizeof(enable_mask))) { |
| 794 | rc = -EFAULT; |
| 795 | break; |
| 796 | } |
| 797 | |
| 798 | enable = ((enable_mask & ADRC_ENABLE) || |
| 799 | (enable_mask & MBADRC_ENABLE)) ? 1 : 0; |
| 800 | audio_enable_mbadrc(audio_copp, enable); |
| 801 | enable = (enable_mask & EQ_ENABLE) ? 1 : 0; |
| 802 | audio_enable_eq(audio_copp, enable); |
| 803 | enable = (enable_mask & IIR_ENABLE) ? 1 : 0; |
| 804 | audio_enable_rx_iir(audio_copp, enable); |
| 805 | enable = (enable_mask & QCONCERT_PLUS_ENABLE) ? 1 : 0; |
| 806 | audio_enable_qconcert_plus(audio_copp, enable); |
| 807 | break; |
| 808 | |
| 809 | case AUDIO_SET_MBADRC: { |
| 810 | uint32_t mbadrc_coeff_buf; |
| 811 | prev_state = audio_copp->mbadrc_enable; |
| 812 | audio_copp->mbadrc_enable = 0; |
| 813 | if (copy_from_user(&audio_copp->mbadrc.num_bands, (void *) arg, |
| 814 | sizeof(audio_copp->mbadrc) - |
| 815 | (AUDPP_CMD_CFG_OBJECT_PARAMS_COMMON_LEN + 2))) |
| 816 | rc = -EFAULT; |
| 817 | else if (audio_copp->mbadrc.ext_buf_size) { |
| 818 | mbadrc_coeff_buf = (uint32_t) ((char *) arg + |
| 819 | sizeof(audio_copp->mbadrc) - |
| 820 | (AUDPP_CMD_CFG_OBJECT_PARAMS_COMMON_LEN + 2)); |
| 821 | if ((copy_from_user(audio_copp->mbadrc_data, |
| 822 | (void *) mbadrc_coeff_buf, |
| 823 | AUDPP_MBADRC_EXTERNAL_BUF_SIZE * 2))) { |
| 824 | rc = -EFAULT; |
| 825 | break; |
| 826 | } |
| 827 | audio_copp->mbadrc.ext_buf_lsw = |
| 828 | audio_copp->mbadrc_phys & 0xFFFF; |
| 829 | audio_copp->mbadrc.ext_buf_msw = |
| 830 | ((audio_copp->mbadrc_phys & 0xFFFF0000) >> 16); |
| 831 | } |
| 832 | audio_copp->mbadrc_enable = prev_state; |
| 833 | if (!rc) |
| 834 | audio_copp->mbadrc_needs_commit = 1; |
| 835 | break; |
| 836 | } |
| 837 | |
| 838 | case AUDIO_SET_ADRC: { |
| 839 | struct audpp_cmd_cfg_object_params_adrc adrc; |
| 840 | prev_state = audio_copp->mbadrc_enable; |
| 841 | audio_copp->mbadrc_enable = 0; |
| 842 | if (copy_from_user(&adrc.compression_th, (void *) arg, |
| 843 | sizeof(adrc) - 2)) { |
| 844 | rc = -EFAULT; |
| 845 | audio_copp->mbadrc_enable = prev_state; |
| 846 | break; |
| 847 | } |
| 848 | audio_copp->mbadrc.num_bands = 1; |
| 849 | audio_copp->mbadrc.down_samp_level = 8; |
| 850 | audio_copp->mbadrc.adrc_delay = adrc.adrc_delay; |
| 851 | audio_copp->mbadrc.ext_buf_size = 0; |
| 852 | audio_copp->mbadrc.ext_partition = 0; |
| 853 | audio_copp->mbadrc.adrc_band[0].subband_enable = 1; |
| 854 | audio_copp->mbadrc.adrc_band[0].adrc_sub_mute = 0; |
| 855 | audio_copp->mbadrc.adrc_band[0].rms_time = |
| 856 | adrc.rms_time; |
| 857 | audio_copp->mbadrc.adrc_band[0].compression_th = |
| 858 | adrc.compression_th; |
| 859 | audio_copp->mbadrc.adrc_band[0].compression_slope = |
| 860 | adrc.compression_slope; |
| 861 | audio_copp->mbadrc.adrc_band[0].attack_const_lsw = |
| 862 | adrc.attack_const_lsw; |
| 863 | audio_copp->mbadrc.adrc_band[0].attack_const_msw = |
| 864 | adrc.attack_const_msw; |
| 865 | audio_copp->mbadrc.adrc_band[0].release_const_lsw = |
| 866 | adrc.release_const_lsw; |
| 867 | audio_copp->mbadrc.adrc_band[0].release_const_msw = |
| 868 | adrc.release_const_msw; |
| 869 | audio_copp->mbadrc.adrc_band[0].makeup_gain = 0x2000; |
| 870 | audio_copp->mbadrc_enable = prev_state; |
| 871 | audio_copp->mbadrc_needs_commit = 1; |
| 872 | break; |
| 873 | } |
| 874 | |
| 875 | case AUDIO_SET_EQ: |
| 876 | prev_state = audio_copp->eq_enable; |
| 877 | audio_copp->eq_enable = 0; |
| 878 | if (copy_from_user(&audio_copp->eq.num_bands, (void *) arg, |
| 879 | sizeof(audio_copp->eq) - |
| 880 | (AUDPP_CMD_CFG_OBJECT_PARAMS_COMMON_LEN + 2))) |
| 881 | rc = -EFAULT; |
| 882 | audio_copp->eq_enable = prev_state; |
| 883 | audio_copp->eq_needs_commit = 1; |
| 884 | break; |
| 885 | |
| 886 | case AUDIO_SET_RX_IIR: |
| 887 | prev_state = audio_copp->rx_iir_enable; |
| 888 | audio_copp->rx_iir_enable = 0; |
| 889 | if (copy_from_user(&audio_copp->iir.num_bands, (void *) arg, |
| 890 | sizeof(audio_copp->iir) - |
| 891 | (AUDPP_CMD_CFG_OBJECT_PARAMS_COMMON_LEN + 2))) |
| 892 | rc = -EFAULT; |
| 893 | audio_copp->rx_iir_enable = prev_state; |
| 894 | audio_copp->rx_iir_needs_commit = 1; |
| 895 | break; |
| 896 | |
| 897 | case AUDIO_SET_VOLUME: |
| 898 | audio_copp->vol_pan.volume = arg; |
| 899 | audio_enable_vol_pan(audio_copp); |
| 900 | break; |
| 901 | |
| 902 | case AUDIO_SET_PAN: |
| 903 | audio_copp->vol_pan.pan = arg; |
| 904 | audio_enable_vol_pan(audio_copp); |
| 905 | break; |
| 906 | |
| 907 | case AUDIO_SET_QCONCERT_PLUS: |
| 908 | prev_state = audio_copp->qconcert_plus_enable; |
| 909 | audio_copp->qconcert_plus_enable = 0; |
| 910 | if (copy_from_user(&audio_copp->qconcert_plus.op_mode, |
| 911 | (void *) arg, |
| 912 | sizeof(audio_copp->qconcert_plus) - |
| 913 | (AUDPP_CMD_CFG_OBJECT_PARAMS_COMMON_LEN + 2))) |
| 914 | rc = -EFAULT; |
| 915 | audio_copp->qconcert_plus_enable = prev_state; |
| 916 | audio_copp->qconcert_plus_needs_commit = 1; |
| 917 | break; |
| 918 | |
| 919 | default: |
| 920 | rc = -EINVAL; |
| 921 | } |
| 922 | |
| 923 | mutex_unlock(&audio_copp->lock); |
| 924 | return rc; |
| 925 | } |
| 926 | |
| 927 | static int audpp_open(struct inode *inode, struct file *file) |
| 928 | { |
| 929 | struct audio_copp *audio_copp = &the_audio_copp; |
| 930 | int rc; |
| 931 | |
| 932 | mutex_lock(&audio_copp->lock); |
| 933 | if (audio_copp->opened) { |
| 934 | mutex_unlock(&audio_copp->lock); |
| 935 | return -EBUSY; |
| 936 | } |
| 937 | |
| 938 | audio_copp->opened = 1; |
| 939 | |
| 940 | if (!audio_copp->status) { |
| 941 | audio_copp->ecb.fn = audio_commit_pending_pp_params; |
| 942 | audio_copp->ecb.private = audio_copp; |
| 943 | rc = audpp_register_event_callback(&audio_copp->ecb); |
| 944 | if (rc) { |
| 945 | audio_copp->opened = 0; |
| 946 | mutex_unlock(&audio_copp->lock); |
| 947 | return rc; |
| 948 | } |
| 949 | audio_copp->mbadrc_data = dma_alloc_coherent(NULL, |
| 950 | AUDPP_MBADRC_EXTERNAL_BUF_SIZE * 2, |
| 951 | &audio_copp->mbadrc_phys, GFP_KERNEL); |
| 952 | if (!audio_copp->mbadrc_data) { |
| 953 | MM_ERR("could not allocate DMA buffers\n"); |
| 954 | audio_copp->opened = 0; |
| 955 | audpp_unregister_event_callback(&audio_copp->ecb); |
| 956 | mutex_unlock(&audio_copp->lock); |
| 957 | return -ENOMEM; |
| 958 | } |
| 959 | audio_copp->vol_pan.volume = 0x2000; |
| 960 | audio_copp->vol_pan.pan = 0x0; |
| 961 | audio_copp->status = 1; |
| 962 | } |
| 963 | |
| 964 | file->private_data = audio_copp; |
| 965 | mutex_unlock(&audio_copp->lock); |
| 966 | |
| 967 | return 0; |
| 968 | } |
| 969 | |
| 970 | static int audpp_release(struct inode *inode, struct file *file) |
| 971 | { |
| 972 | struct audio_copp *audio_copp = &the_audio_copp; |
| 973 | |
| 974 | audio_copp->opened = 0; |
| 975 | |
| 976 | return 0; |
| 977 | } |
| 978 | |
| 979 | static struct file_operations audio_fops = { |
| 980 | .owner = THIS_MODULE, |
| 981 | .open = audio_open, |
| 982 | .release = audio_release, |
| 983 | .read = audio_read, |
| 984 | .write = audio_write, |
| 985 | .unlocked_ioctl = audio_ioctl, |
| 986 | .fsync = audio_fsync, |
| 987 | }; |
| 988 | |
| 989 | static struct file_operations audpp_fops = { |
| 990 | .owner = THIS_MODULE, |
| 991 | .open = audpp_open, |
| 992 | .release = audpp_release, |
| 993 | .unlocked_ioctl = audpp_ioctl, |
| 994 | }; |
| 995 | |
| 996 | struct miscdevice audio_misc = { |
| 997 | .minor = MISC_DYNAMIC_MINOR, |
| 998 | .name = "msm_pcm_out", |
| 999 | .fops = &audio_fops, |
| 1000 | }; |
| 1001 | |
| 1002 | struct miscdevice audpp_misc = { |
| 1003 | .minor = MISC_DYNAMIC_MINOR, |
| 1004 | .name = "msm_pcm_ctl", |
| 1005 | .fops = &audpp_fops, |
| 1006 | }; |
| 1007 | |
| 1008 | static int __init audio_init(void) |
| 1009 | { |
| 1010 | mutex_init(&the_audio.lock); |
| 1011 | mutex_init(&the_audio.write_lock); |
| 1012 | mutex_init(&the_audio_copp.lock); |
| 1013 | spin_lock_init(&the_audio.dsp_lock); |
| 1014 | init_waitqueue_head(&the_audio.wait); |
| 1015 | wake_lock_init(&the_audio.wakelock, WAKE_LOCK_SUSPEND, "audio_pcm"); |
| 1016 | wake_lock_init(&the_audio.idlelock, WAKE_LOCK_IDLE, "audio_pcm_idle"); |
| 1017 | return (misc_register(&audio_misc) || misc_register(&audpp_misc)); |
| 1018 | } |
| 1019 | |
| 1020 | device_initcall(audio_init); |