Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1 | /* Copyright (C) 2008 Google, Inc. |
| 2 | * Copyright (C) 2008 HTC Corporation |
| 3 | * Copyright (c) 2008-2010, Code Aurora Forum. All rights reserved. |
| 4 | * |
| 5 | * This software is licensed under the terms of the GNU General Public |
| 6 | * License version 2, as published by the Free Software Foundation, and |
| 7 | * may be copied, distributed, and modified under those terms. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | * |
| 13 | * See the GNU General Public License for more details. |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, you can find it at http://www.fsf.org. |
| 16 | */ |
| 17 | |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/err.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/moduleparam.h> |
| 22 | #include <linux/time.h> |
| 23 | #include <linux/wait.h> |
| 24 | #include <linux/platform_device.h> |
| 25 | #include <sound/core.h> |
| 26 | #include <sound/soc.h> |
| 27 | #include <sound/pcm.h> |
| 28 | #include <sound/initval.h> |
| 29 | #include <asm/dma.h> |
| 30 | #include <linux/dma-mapping.h> |
| 31 | #include <mach/qdsp5v2/audio_dev_ctl.h> |
| 32 | #include <mach/debug_mm.h> |
| 33 | |
| 34 | #include "msm7kv2-pcm.h" |
| 35 | |
| 36 | /* Audrec Queue command sent macro's */ |
| 37 | #define audrec_send_bitstreamqueue(audio, cmd, len) \ |
| 38 | msm_adsp_write(audio->audrec, ((audio->queue_id & 0xFFFF0000) >> 16),\ |
| 39 | cmd, len) |
| 40 | |
| 41 | #define audrec_send_audrecqueue(audio, cmd, len) \ |
| 42 | msm_adsp_write(audio->audrec, (audio->queue_id & 0x0000FFFF),\ |
| 43 | cmd, len) |
| 44 | |
| 45 | static int alsa_dsp_read_buffer(struct msm_audio *audio, |
| 46 | uint32_t read_cnt); |
| 47 | static void alsa_get_dsp_frames(struct msm_audio *prtd); |
| 48 | static int alsa_in_param_config(struct msm_audio *audio); |
| 49 | |
| 50 | static int alsa_in_mem_config(struct msm_audio *audio); |
| 51 | static int alsa_in_enc_config(struct msm_audio *audio, int enable); |
| 52 | |
| 53 | int intcnt; |
| 54 | struct audio_frame { |
| 55 | uint16_t count_low; |
| 56 | uint16_t count_high; |
| 57 | uint16_t bytes; |
| 58 | uint16_t unknown; |
| 59 | unsigned char samples[]; |
| 60 | } __attribute__ ((packed)); |
| 61 | |
| 62 | void alsa_dsp_event(void *data, unsigned id, uint16_t *msg) |
| 63 | { |
| 64 | struct msm_audio *prtd = data; |
| 65 | struct buffer *frame; |
| 66 | unsigned long flag = 0; |
| 67 | |
| 68 | MM_DBG("\n"); |
| 69 | switch (id) { |
| 70 | case AUDPP_MSG_HOST_PCM_INTF_MSG: { |
| 71 | unsigned id = msg[3]; |
| 72 | unsigned idx = msg[4] - 1; |
| 73 | |
| 74 | MM_DBG("HOST_PCM id %d idx %d\n", id, idx); |
| 75 | if (id != AUDPP_MSG_HOSTPCM_ID_ARM_RX) { |
| 76 | MM_ERR("bogus id\n"); |
| 77 | break; |
| 78 | } |
| 79 | if (idx > 1) { |
| 80 | MM_ERR("bogus buffer idx\n"); |
| 81 | break; |
| 82 | } |
| 83 | |
| 84 | /* Update with actual sent buffer size */ |
| 85 | if (prtd->out[idx].used != BUF_INVALID_LEN) |
| 86 | prtd->pcm_irq_pos += prtd->out[idx].used; |
| 87 | |
| 88 | if (prtd->pcm_irq_pos > prtd->pcm_size) |
| 89 | prtd->pcm_irq_pos = prtd->pcm_count; |
| 90 | |
| 91 | if (prtd->ops->playback) |
| 92 | prtd->ops->playback(prtd); |
| 93 | |
| 94 | if (prtd->mmap_flag) |
| 95 | break; |
| 96 | |
| 97 | spin_lock_irqsave(&the_locks.write_dsp_lock, flag); |
| 98 | if (prtd->running) { |
| 99 | prtd->out[idx].used = 0; |
| 100 | frame = prtd->out + prtd->out_tail; |
| 101 | if (frame->used) { |
| 102 | alsa_dsp_send_buffer( |
| 103 | prtd, prtd->out_tail, frame->used); |
| 104 | /* Reset eos_ack flag to avoid stale |
| 105 | * PCMDMAMISS been considered |
| 106 | */ |
| 107 | prtd->eos_ack = 0; |
| 108 | prtd->out_tail ^= 1; |
| 109 | } else { |
| 110 | prtd->out_needed++; |
| 111 | } |
| 112 | wake_up(&the_locks.write_wait); |
| 113 | } |
| 114 | spin_unlock_irqrestore(&the_locks.write_dsp_lock, flag); |
| 115 | break; |
| 116 | } |
| 117 | case AUDPP_MSG_PCMDMAMISSED: |
| 118 | MM_INFO("PCMDMAMISSED %d\n", msg[0]); |
| 119 | prtd->eos_ack++; |
| 120 | MM_DBG("PCMDMAMISSED Count per Buffer %d\n", prtd->eos_ack); |
| 121 | wake_up(&the_locks.eos_wait); |
| 122 | break; |
| 123 | case AUDPP_MSG_CFG_MSG: |
| 124 | if (msg[0] == AUDPP_MSG_ENA_ENA) { |
| 125 | MM_DBG("CFG_MSG ENABLE\n"); |
| 126 | prtd->out_needed = 0; |
| 127 | prtd->running = 1; |
| 128 | audpp_dsp_set_vol_pan(prtd->session_id, &prtd->vol_pan, |
| 129 | POPP); |
| 130 | audpp_route_stream(prtd->session_id, |
| 131 | msm_snddev_route_dec(prtd->session_id)); |
| 132 | audio_dsp_out_enable(prtd, 1); |
| 133 | } else if (msg[0] == AUDPP_MSG_ENA_DIS) { |
| 134 | MM_DBG("CFG_MSG DISABLE\n"); |
| 135 | prtd->running = 0; |
| 136 | } else { |
| 137 | MM_DBG("CFG_MSG %d?\n", msg[0]); |
| 138 | } |
| 139 | break; |
| 140 | default: |
| 141 | MM_DBG("UNKNOWN (%d)\n", id); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | static void audpreproc_dsp_event(void *data, unsigned id, void *msg) |
| 146 | { |
| 147 | struct msm_audio *prtd = data; |
| 148 | |
| 149 | switch (id) { |
| 150 | case AUDPREPROC_ERROR_MSG: { |
| 151 | struct audpreproc_err_msg *err_msg = msg; |
| 152 | |
| 153 | MM_ERR("ERROR_MSG: stream id %d err idx %d\n", |
| 154 | err_msg->stream_id, err_msg->aud_preproc_err_idx); |
| 155 | /* Error case */ |
| 156 | break; |
| 157 | } |
| 158 | case AUDPREPROC_CMD_CFG_DONE_MSG: { |
| 159 | MM_DBG("CMD_CFG_DONE_MSG\n"); |
| 160 | break; |
| 161 | } |
| 162 | case AUDPREPROC_CMD_ENC_CFG_DONE_MSG: { |
| 163 | struct audpreproc_cmd_enc_cfg_done_msg *enc_cfg_msg = msg; |
| 164 | |
| 165 | MM_DBG("CMD_ENC_CFG_DONE_MSG: stream id %d enc type \ |
| 166 | 0x%8x\n", enc_cfg_msg->stream_id, |
| 167 | enc_cfg_msg->rec_enc_type); |
| 168 | /* Encoder enable success */ |
| 169 | if (enc_cfg_msg->rec_enc_type & ENCODE_ENABLE) |
| 170 | alsa_in_param_config(prtd); |
| 171 | else { /* Encoder disable success */ |
| 172 | prtd->running = 0; |
| 173 | alsa_in_record_config(prtd, 0); |
| 174 | } |
| 175 | break; |
| 176 | } |
| 177 | case AUDPREPROC_CMD_ENC_PARAM_CFG_DONE_MSG: { |
| 178 | MM_DBG("CMD_ENC_PARAM_CFG_DONE_MSG\n"); |
| 179 | alsa_in_mem_config(prtd); |
| 180 | break; |
| 181 | } |
| 182 | case AUDPREPROC_AFE_CMD_AUDIO_RECORD_CFG_DONE_MSG: { |
| 183 | MM_DBG("AFE_CMD_AUDIO_RECORD_CFG_DONE_MSG\n"); |
| 184 | wake_up(&the_locks.enable_wait); |
| 185 | break; |
| 186 | } |
| 187 | default: |
| 188 | MM_DBG("Unknown Event id %d\n", id); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | static void audrec_dsp_event(void *data, unsigned id, size_t len, |
| 193 | void (*getevent) (void *ptr, size_t len)) |
| 194 | { |
| 195 | struct msm_audio *prtd = data; |
| 196 | unsigned long flag = 0; |
| 197 | |
| 198 | switch (id) { |
| 199 | case AUDREC_CMD_MEM_CFG_DONE_MSG: { |
| 200 | MM_DBG("AUDREC_CMD_MEM_CFG_DONE_MSG\n"); |
| 201 | prtd->running = 1; |
| 202 | alsa_in_record_config(prtd, 1); |
| 203 | break; |
| 204 | } |
| 205 | case AUDREC_FATAL_ERR_MSG: { |
| 206 | struct audrec_fatal_err_msg fatal_err_msg; |
| 207 | |
| 208 | getevent(&fatal_err_msg, AUDREC_FATAL_ERR_MSG_LEN); |
| 209 | MM_ERR("FATAL_ERR_MSG: err id %d\n", |
| 210 | fatal_err_msg.audrec_err_id); |
| 211 | /* Error stop the encoder */ |
| 212 | prtd->stopped = 1; |
| 213 | wake_up(&the_locks.read_wait); |
| 214 | break; |
| 215 | } |
| 216 | case AUDREC_UP_PACKET_READY_MSG: { |
| 217 | struct audrec_up_pkt_ready_msg pkt_ready_msg; |
| 218 | MM_DBG("AUDREC_UP_PACKET_READY_MSG\n"); |
| 219 | |
| 220 | getevent(&pkt_ready_msg, AUDREC_UP_PACKET_READY_MSG_LEN); |
| 221 | MM_DBG("UP_PACKET_READY_MSG: write cnt lsw %d \ |
| 222 | write cnt msw %d read cnt lsw %d read cnt msw %d \n",\ |
| 223 | pkt_ready_msg.audrec_packet_write_cnt_lsw, \ |
| 224 | pkt_ready_msg.audrec_packet_write_cnt_msw, \ |
| 225 | pkt_ready_msg.audrec_up_prev_read_cnt_lsw, \ |
| 226 | pkt_ready_msg.audrec_up_prev_read_cnt_msw); |
| 227 | |
| 228 | alsa_get_dsp_frames(prtd); |
| 229 | ++intcnt; |
| 230 | if (prtd->channel_mode == 1) { |
| 231 | spin_lock_irqsave(&the_locks.read_dsp_lock, flag); |
| 232 | if (prtd->pcm_irq_pos >= prtd->pcm_size) |
| 233 | prtd->pcm_irq_pos = 0; |
| 234 | spin_unlock_irqrestore(&the_locks.read_dsp_lock, flag); |
| 235 | |
| 236 | if (prtd->ops->capture) |
| 237 | prtd->ops->capture(prtd); |
| 238 | } else if ((prtd->channel_mode == 0) && (intcnt % 2 == 0)) { |
| 239 | spin_lock_irqsave(&the_locks.read_dsp_lock, flag); |
| 240 | if (prtd->pcm_irq_pos >= prtd->pcm_size) |
| 241 | prtd->pcm_irq_pos = 0; |
| 242 | spin_unlock_irqrestore(&the_locks.read_dsp_lock, flag); |
| 243 | if (prtd->ops->capture) |
| 244 | prtd->ops->capture(prtd); |
| 245 | } |
| 246 | break; |
| 247 | } |
| 248 | default: |
| 249 | MM_DBG("Unknown Event id %d\n", id); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | struct msm_adsp_ops alsa_audrec_adsp_ops = { |
| 254 | .event = audrec_dsp_event, |
| 255 | }; |
| 256 | |
| 257 | int alsa_audio_configure(struct msm_audio *prtd) |
| 258 | { |
| 259 | if (prtd->enabled) |
| 260 | return 0; |
| 261 | |
| 262 | MM_DBG("\n"); |
| 263 | if (prtd->dir == SNDRV_PCM_STREAM_PLAYBACK) { |
| 264 | prtd->out_weight = 100; |
| 265 | if (audpp_enable(-1, alsa_dsp_event, prtd)) { |
| 266 | MM_ERR("audpp_enable() failed\n"); |
| 267 | return -ENODEV; |
| 268 | } |
| 269 | } |
| 270 | if (prtd->dir == SNDRV_PCM_STREAM_CAPTURE) { |
| 271 | if (audpreproc_enable(prtd->session_id, |
| 272 | &audpreproc_dsp_event, prtd)) { |
| 273 | MM_ERR("audpreproc_enable failed\n"); |
| 274 | return -ENODEV; |
| 275 | } |
| 276 | |
| 277 | if (msm_adsp_enable(prtd->audrec)) { |
| 278 | MM_ERR("msm_adsp_enable(audrec) enable failed\n"); |
| 279 | audpreproc_disable(prtd->session_id, prtd); |
| 280 | return -ENODEV; |
| 281 | } |
| 282 | alsa_in_enc_config(prtd, 1); |
| 283 | |
| 284 | } |
| 285 | prtd->enabled = 1; |
| 286 | return 0; |
| 287 | } |
| 288 | EXPORT_SYMBOL(alsa_audio_configure); |
| 289 | |
| 290 | ssize_t alsa_send_buffer(struct msm_audio *prtd, const char __user *buf, |
| 291 | size_t count, loff_t *pos) |
| 292 | { |
| 293 | unsigned long flag = 0; |
| 294 | const char __user *start = buf; |
| 295 | struct buffer *frame; |
| 296 | size_t xfer; |
| 297 | int ret = 0; |
| 298 | |
| 299 | MM_DBG("\n"); |
| 300 | mutex_lock(&the_locks.write_lock); |
| 301 | while (count > 0) { |
| 302 | frame = prtd->out + prtd->out_head; |
| 303 | ret = wait_event_interruptible(the_locks.write_wait, |
| 304 | (frame->used == 0) |
| 305 | || (prtd->stopped)); |
| 306 | if (ret < 0) |
| 307 | break; |
| 308 | if (prtd->stopped) { |
| 309 | ret = -EBUSY; |
| 310 | break; |
| 311 | } |
| 312 | xfer = count > frame->size ? frame->size : count; |
| 313 | if (copy_from_user(frame->data, buf, xfer)) { |
| 314 | ret = -EFAULT; |
| 315 | break; |
| 316 | } |
| 317 | frame->used = xfer; |
| 318 | prtd->out_head ^= 1; |
| 319 | count -= xfer; |
| 320 | buf += xfer; |
| 321 | |
| 322 | spin_lock_irqsave(&the_locks.write_dsp_lock, flag); |
| 323 | frame = prtd->out + prtd->out_tail; |
| 324 | if (frame->used && prtd->out_needed) { |
| 325 | alsa_dsp_send_buffer(prtd, prtd->out_tail, |
| 326 | frame->used); |
| 327 | /* Reset eos_ack flag to avoid stale |
| 328 | * PCMDMAMISS been considered |
| 329 | */ |
| 330 | prtd->eos_ack = 0; |
| 331 | prtd->out_tail ^= 1; |
| 332 | prtd->out_needed--; |
| 333 | } |
| 334 | spin_unlock_irqrestore(&the_locks.write_dsp_lock, flag); |
| 335 | } |
| 336 | mutex_unlock(&the_locks.write_lock); |
| 337 | if (buf > start) |
| 338 | return buf - start; |
| 339 | return ret; |
| 340 | } |
| 341 | EXPORT_SYMBOL(alsa_send_buffer); |
| 342 | |
| 343 | int alsa_audio_disable(struct msm_audio *prtd) |
| 344 | { |
| 345 | if (prtd->enabled) { |
| 346 | MM_DBG("\n"); |
| 347 | mutex_lock(&the_locks.lock); |
| 348 | prtd->enabled = 0; |
| 349 | audio_dsp_out_enable(prtd, 0); |
| 350 | wake_up(&the_locks.write_wait); |
| 351 | audpp_disable(-1, prtd); |
| 352 | prtd->out_needed = 0; |
| 353 | mutex_unlock(&the_locks.lock); |
| 354 | } |
| 355 | return 0; |
| 356 | } |
| 357 | EXPORT_SYMBOL(alsa_audio_disable); |
| 358 | |
| 359 | int alsa_audrec_disable(struct msm_audio *prtd) |
| 360 | { |
| 361 | if (prtd->enabled) { |
| 362 | prtd->enabled = 0; |
| 363 | alsa_in_enc_config(prtd, 0); |
| 364 | wake_up(&the_locks.read_wait); |
| 365 | msm_adsp_disable(prtd->audrec); |
| 366 | prtd->out_needed = 0; |
| 367 | audpreproc_disable(prtd->session_id, prtd); |
| 368 | } |
| 369 | return 0; |
| 370 | } |
| 371 | EXPORT_SYMBOL(alsa_audrec_disable); |
| 372 | |
| 373 | static int alsa_in_enc_config(struct msm_audio *prtd, int enable) |
| 374 | { |
| 375 | struct audpreproc_audrec_cmd_enc_cfg cmd; |
| 376 | int i; |
| 377 | unsigned short *ptrmem = (unsigned short *)&cmd; |
| 378 | |
| 379 | memset(&cmd, 0, sizeof(cmd)); |
| 380 | cmd.cmd_id = AUDPREPROC_AUDREC_CMD_ENC_CFG; |
| 381 | cmd.stream_id = prtd->session_id; |
| 382 | |
| 383 | if (enable) |
| 384 | cmd.audrec_enc_type = prtd->type | ENCODE_ENABLE; |
| 385 | else |
| 386 | cmd.audrec_enc_type &= ~(ENCODE_ENABLE); |
| 387 | for (i = 0; i < sizeof(cmd)/2; i++, ++ptrmem) |
| 388 | MM_DBG("cmd[%d]=0x%04x\n", i, *ptrmem); |
| 389 | |
| 390 | return audpreproc_send_audreccmdqueue(&cmd, sizeof(cmd)); |
| 391 | } |
| 392 | |
| 393 | static int alsa_in_param_config(struct msm_audio *prtd) |
| 394 | { |
| 395 | struct audpreproc_audrec_cmd_parm_cfg_wav cmd; |
| 396 | int i; |
| 397 | unsigned short *ptrmem = (unsigned short *)&cmd; |
| 398 | |
| 399 | memset(&cmd, 0, sizeof(cmd)); |
| 400 | cmd.common.cmd_id = AUDPREPROC_AUDREC_CMD_PARAM_CFG; |
| 401 | cmd.common.stream_id = prtd->session_id; |
| 402 | |
| 403 | cmd.aud_rec_samplerate_idx = prtd->samp_rate; |
| 404 | cmd.aud_rec_stereo_mode = prtd->channel_mode; |
| 405 | for (i = 0; i < sizeof(cmd)/2; i++, ++ptrmem) |
| 406 | MM_DBG("cmd[%d]=0x%04x\n", i, *ptrmem); |
| 407 | |
| 408 | return audpreproc_send_audreccmdqueue(&cmd, sizeof(cmd)); |
| 409 | } |
| 410 | |
| 411 | int alsa_in_record_config(struct msm_audio *prtd, int enable) |
| 412 | { |
| 413 | struct audpreproc_afe_cmd_audio_record_cfg cmd; |
| 414 | int i; |
| 415 | unsigned short *ptrmem = (unsigned short *)&cmd; |
| 416 | |
| 417 | memset(&cmd, 0, sizeof(cmd)); |
| 418 | cmd.cmd_id = AUDPREPROC_AFE_CMD_AUDIO_RECORD_CFG; |
| 419 | cmd.stream_id = prtd->session_id; |
| 420 | if (enable) |
| 421 | cmd.destination_activity = AUDIO_RECORDING_TURN_ON; |
| 422 | else |
| 423 | cmd.destination_activity = AUDIO_RECORDING_TURN_OFF; |
| 424 | cmd.source_mix_mask = prtd->source; |
| 425 | if (prtd->session_id == 2) { |
| 426 | if ((cmd.source_mix_mask & |
| 427 | INTERNAL_CODEC_TX_SOURCE_MIX_MASK) || |
| 428 | (cmd.source_mix_mask & AUX_CODEC_TX_SOURCE_MIX_MASK) || |
| 429 | (cmd.source_mix_mask & VOICE_UL_SOURCE_MIX_MASK) || |
| 430 | (cmd.source_mix_mask & VOICE_DL_SOURCE_MIX_MASK)) { |
| 431 | cmd.pipe_id = SOURCE_PIPE_1; |
| 432 | } |
| 433 | if (cmd.source_mix_mask & |
| 434 | AUDPP_A2DP_PIPE_SOURCE_MIX_MASK) |
| 435 | cmd.pipe_id |= SOURCE_PIPE_0; |
| 436 | } |
| 437 | for (i = 0; i < sizeof(cmd)/2; i++, ++ptrmem) |
| 438 | MM_DBG("cmd[%d]=0x%04x\n", i, *ptrmem); |
| 439 | return audpreproc_send_audreccmdqueue(&cmd, sizeof(cmd)); |
| 440 | } |
| 441 | |
| 442 | static int alsa_in_mem_config(struct msm_audio *prtd) |
| 443 | { |
| 444 | struct audrec_cmd_arecmem_cfg cmd; |
| 445 | uint16_t *data = (void *) prtd->data; |
| 446 | int n; |
| 447 | int i; |
| 448 | unsigned short *ptrmem = (unsigned short *)&cmd; |
| 449 | |
| 450 | memset(&cmd, 0, sizeof(cmd)); |
| 451 | cmd.cmd_id = AUDREC_CMD_MEM_CFG_CMD; |
| 452 | cmd.audrec_up_pkt_intm_count = 1; |
| 453 | cmd.audrec_ext_pkt_start_addr_msw = prtd->phys >> 16; |
| 454 | cmd.audrec_ext_pkt_start_addr_lsw = prtd->phys; |
| 455 | cmd.audrec_ext_pkt_buf_number = FRAME_NUM; |
| 456 | |
| 457 | /* prepare buffer pointers: |
| 458 | * Mono: 1024 samples + 4 halfword header |
| 459 | * Stereo: 2048 samples + 4 halfword header |
| 460 | */ |
| 461 | for (n = 0; n < FRAME_NUM; n++) { |
| 462 | prtd->in[n].data = data + 4; |
| 463 | data += (4 + (prtd->channel_mode ? 2048 : 1024)); |
| 464 | MM_DBG("0x%8x\n", (int)(prtd->in[n].data - 8)); |
| 465 | } |
| 466 | for (i = 0; i < sizeof(cmd)/2; i++, ++ptrmem) |
| 467 | MM_DBG("cmd[%d]=0x%04x\n", i, *ptrmem); |
| 468 | |
| 469 | return audrec_send_audrecqueue(prtd, &cmd, sizeof(cmd)); |
| 470 | } |
| 471 | |
| 472 | int audio_dsp_out_enable(struct msm_audio *prtd, int yes) |
| 473 | { |
| 474 | struct audpp_cmd_pcm_intf cmd; |
| 475 | |
| 476 | memset(&cmd, 0, sizeof(cmd)); |
| 477 | cmd.cmd_id = AUDPP_CMD_PCM_INTF; |
| 478 | cmd.stream = AUDPP_CMD_POPP_STREAM; |
| 479 | cmd.stream_id = prtd->session_id; |
| 480 | cmd.config = AUDPP_CMD_PCM_INTF_CONFIG_CMD_V; |
| 481 | cmd.intf_type = AUDPP_CMD_PCM_INTF_RX_ENA_ARMTODSP_V; |
| 482 | |
| 483 | if (yes) { |
| 484 | cmd.write_buf1LSW = prtd->out[0].addr; |
| 485 | cmd.write_buf1MSW = prtd->out[0].addr >> 16; |
| 486 | cmd.write_buf1_len = prtd->out[0].size; |
| 487 | cmd.write_buf2LSW = prtd->out[1].addr; |
| 488 | cmd.write_buf2MSW = prtd->out[1].addr >> 16; |
| 489 | if (prtd->out[1].used) |
| 490 | cmd.write_buf2_len = prtd->out[1].used; |
| 491 | else |
| 492 | cmd.write_buf2_len = prtd->out[1].size; |
| 493 | cmd.arm_to_rx_flag = AUDPP_CMD_PCM_INTF_ENA_V; |
| 494 | cmd.weight_decoder_to_rx = prtd->out_weight; |
| 495 | cmd.weight_arm_to_rx = 1; |
| 496 | cmd.partition_number_arm_to_dsp = 0; |
| 497 | cmd.sample_rate = prtd->out_sample_rate; |
| 498 | cmd.channel_mode = prtd->out_channel_mode; |
| 499 | } |
| 500 | |
| 501 | return audpp_send_queue2(&cmd, sizeof(cmd)); |
| 502 | } |
| 503 | |
| 504 | int alsa_buffer_read(struct msm_audio *prtd, void __user *buf, |
| 505 | size_t count, loff_t *pos) |
| 506 | { |
| 507 | unsigned long flag; |
| 508 | void *data; |
| 509 | uint32_t index; |
| 510 | uint32_t size; |
| 511 | int ret = 0; |
| 512 | |
| 513 | mutex_lock(&the_locks.read_lock); |
| 514 | while (count > 0) { |
| 515 | ret = wait_event_interruptible(the_locks.read_wait, |
| 516 | (prtd->in_count > 0) |
| 517 | || prtd->stopped || |
| 518 | prtd->abort); |
| 519 | |
| 520 | if (ret < 0) |
| 521 | break; |
| 522 | |
| 523 | if (prtd->stopped) { |
| 524 | ret = -EBUSY; |
| 525 | break; |
| 526 | } |
| 527 | |
| 528 | if (prtd->abort) { |
| 529 | MM_DBG(" prtd->abort !\n"); |
| 530 | ret = -EPERM; /* Not permitted due to abort */ |
| 531 | break; |
| 532 | } |
| 533 | |
| 534 | index = prtd->in_tail; |
| 535 | data = (uint8_t *) prtd->in[index].data; |
| 536 | size = prtd->in[index].size; |
| 537 | if (count >= size) { |
| 538 | if (copy_to_user(buf, data, size)) { |
| 539 | ret = -EFAULT; |
| 540 | break; |
| 541 | } |
| 542 | spin_lock_irqsave(&the_locks.read_dsp_lock, flag); |
| 543 | if (index != prtd->in_tail) { |
| 544 | /* overrun: data is invalid, we need to retry */ |
| 545 | spin_unlock_irqrestore(&the_locks.read_dsp_lock, |
| 546 | flag); |
| 547 | continue; |
| 548 | } |
| 549 | prtd->in[index].size = 0; |
| 550 | prtd->in_tail = (prtd->in_tail + 1) & (FRAME_NUM - 1); |
| 551 | prtd->in_count--; |
| 552 | spin_unlock_irqrestore(&the_locks.read_dsp_lock, flag); |
| 553 | count -= size; |
| 554 | buf += size; |
| 555 | } else { |
| 556 | break; |
| 557 | } |
| 558 | } |
| 559 | mutex_unlock(&the_locks.read_lock); |
| 560 | return ret; |
| 561 | } |
| 562 | EXPORT_SYMBOL(alsa_buffer_read); |
| 563 | |
| 564 | int alsa_dsp_send_buffer(struct msm_audio *prtd, |
| 565 | unsigned idx, unsigned len) |
| 566 | { |
| 567 | struct audpp_cmd_pcm_intf_send_buffer cmd; |
| 568 | int i; |
| 569 | unsigned short *ptrmem = (unsigned short *)&cmd; |
| 570 | |
| 571 | cmd.cmd_id = AUDPP_CMD_PCM_INTF; |
| 572 | cmd.stream = AUDPP_CMD_POPP_STREAM; |
| 573 | cmd.stream_id = prtd->session_id; |
| 574 | cmd.config = AUDPP_CMD_PCM_INTF_BUFFER_CMD_V; |
| 575 | cmd.intf_type = AUDPP_CMD_PCM_INTF_RX_ENA_ARMTODSP_V; |
| 576 | cmd.dsp_to_arm_buf_id = 0; |
| 577 | cmd.arm_to_dsp_buf_id = idx + 1; |
| 578 | cmd.arm_to_dsp_buf_len = len; |
| 579 | for (i = 0; i < sizeof(cmd)/2; i++, ++ptrmem) |
| 580 | MM_DBG("cmd[%d]=0x%04x\n", i, *ptrmem); |
| 581 | |
| 582 | return audpp_send_queue2(&cmd, sizeof(cmd)); |
| 583 | } |
| 584 | |
| 585 | static int alsa_dsp_read_buffer(struct msm_audio *audio, uint32_t read_cnt) |
| 586 | { |
| 587 | struct up_audrec_packet_ext_ptr cmd; |
| 588 | int i; |
| 589 | unsigned short *ptrmem = (unsigned short *)&cmd; |
| 590 | |
| 591 | memset(&cmd, 0, sizeof(cmd)); |
| 592 | cmd.cmd_id = UP_AUDREC_PACKET_EXT_PTR; |
| 593 | cmd.audrec_up_curr_read_count_msw = read_cnt >> 16; |
| 594 | cmd.audrec_up_curr_read_count_lsw = read_cnt; |
| 595 | for (i = 0; i < sizeof(cmd)/2; i++, ++ptrmem) |
| 596 | MM_DBG("cmd[%d]=0x%04x\n", i, *ptrmem); |
| 597 | |
| 598 | return audrec_send_bitstreamqueue(audio, &cmd, sizeof(cmd)); |
| 599 | } |
| 600 | |
| 601 | static void alsa_get_dsp_frames(struct msm_audio *prtd) |
| 602 | { |
| 603 | struct audio_frame *frame; |
| 604 | uint32_t index = 0; |
| 605 | unsigned long flag; |
| 606 | |
| 607 | if (prtd->type == ENC_TYPE_WAV) { |
| 608 | index = prtd->in_head; |
| 609 | |
| 610 | frame = |
| 611 | (void *)(((char *)prtd->in[index].data) - sizeof(*frame)); |
| 612 | |
| 613 | spin_lock_irqsave(&the_locks.read_dsp_lock, flag); |
| 614 | prtd->in[index].size = frame->bytes; |
| 615 | MM_DBG("frame = %08x\n", (unsigned int) frame); |
| 616 | MM_DBG("prtd->in[index].size = %08x\n", |
| 617 | (unsigned int) prtd->in[index].size); |
| 618 | |
| 619 | prtd->in_head = (prtd->in_head + 1) & (FRAME_NUM - 1); |
| 620 | |
| 621 | /* If overflow, move the tail index foward. */ |
| 622 | if (prtd->in_head == prtd->in_tail) |
| 623 | prtd->in_tail = (prtd->in_tail + 1) & (FRAME_NUM - 1); |
| 624 | else |
| 625 | prtd->in_count++; |
| 626 | |
| 627 | prtd->pcm_irq_pos += frame->bytes; |
| 628 | alsa_dsp_read_buffer(prtd, prtd->dsp_cnt++); |
| 629 | spin_unlock_irqrestore(&the_locks.read_dsp_lock, flag); |
| 630 | |
| 631 | wake_up(&the_locks.read_wait); |
| 632 | } |
| 633 | } |