blob: 99ad02b037f6d488cddf433508c0cbf20b40e405 [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* arch/arm/mach-msm/qdsp5/audio_evrc_in.c
2 *
3 * evrc audio input device
4 *
Manish Dewangana4f1df02012-02-08 17:06:54 +05305 * Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07006 *
7 * This code is based in part on arch/arm/mach-msm/qdsp5v2/audio_evrc_in.c,
8 * Copyright (C) 2008 Google, Inc.
9 * Copyright (C) 2008 HTC Corporation
10 *
11 * This software is licensed under the terms of the GNU General Public
12 * License version 2, as published by the Free Software Foundation, and
13 * may be copied, distributed, and modified under those terms.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
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
30#include <linux/delay.h>
31
32#include <linux/msm_audio_qcp.h>
33
Santosh Mardi7faa0fa2011-09-22 15:32:23 +053034
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070035#include <linux/memory_alloc.h>
36
37#include <asm/atomic.h>
38#include <asm/ioctls.h>
Santosh Mardi7faa0fa2011-09-22 15:32:23 +053039#include <mach/msm_memtypes.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070040#include <mach/msm_adsp.h>
41#include <mach/msm_rpcrouter.h>
Santosh Mardi7faa0fa2011-09-22 15:32:23 +053042#include <mach/iommu.h>
43#include <mach/iommu_domains.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070044
45#include "audmgr.h"
46
47#include <mach/qdsp5/qdsp5audpreproc.h>
48#include <mach/qdsp5/qdsp5audpreproccmdi.h>
49#include <mach/qdsp5/qdsp5audpreprocmsg.h>
50#include <mach/qdsp5/qdsp5audreccmdi.h>
51#include <mach/qdsp5/qdsp5audrecmsg.h>
52#include <mach/debug_mm.h>
53
54#define FRAME_HEADER_SIZE 8 /* 8 bytes frame header */
55#define NT_FRAME_HEADER_SIZE 24 /* 24 bytes frame header */
56/* FRAME_NUM must be a power of two */
57#define FRAME_NUM 8
58#define EVRC_FRAME_SIZE 36 /* 36 bytes data */
59/*Tunnel mode : 36 bytes data + 8 byte header*/
60#define FRAME_SIZE (EVRC_FRAME_SIZE + FRAME_HEADER_SIZE)
61 /* 36 bytes data + 24 meta field*/
62#define NT_FRAME_SIZE (EVRC_FRAME_SIZE + NT_FRAME_HEADER_SIZE)
63#define DMASZ (FRAME_SIZE * FRAME_NUM)
64#define NT_DMASZ (NT_FRAME_SIZE * FRAME_NUM)
65#define OUT_FRAME_NUM 2
66#define OUT_BUFFER_SIZE (4 * 1024 + NT_FRAME_HEADER_SIZE)
67#define BUFFER_SIZE (OUT_BUFFER_SIZE * OUT_FRAME_NUM)
68
69#define AUDPREPROC_EVRC_EOS_FLG_OFFSET 0x0A /* Offset from beginning of buffer*/
70#define AUDPREPROC_EVRC_EOS_FLG_MASK 0x01
71#define AUDPREPROC_EVRC_EOS_NONE 0x0 /* No EOS detected */
72#define AUDPREPROC_EVRC_EOS_SET 0x1 /* EOS set in meta field */
73
74struct buffer {
75 void *data;
76 uint32_t size;
77 uint32_t read;
78 uint32_t addr;
79 uint32_t used;
80 uint32_t mfield_sz;
81};
82
83struct audio_evrc_in {
84 struct buffer in[FRAME_NUM];
85
86 spinlock_t dsp_lock;
87
88 atomic_t in_bytes;
89 atomic_t in_samples;
90
91 struct mutex lock;
92 struct mutex read_lock;
93 wait_queue_head_t wait;
94 wait_queue_head_t wait_enable;
95 /*write section*/
96 struct buffer out[OUT_FRAME_NUM];
97
98 uint8_t out_head;
99 uint8_t out_tail;
100 uint8_t out_needed; /* number of buffers the dsp is waiting for */
101 uint32_t out_count;
102
103 struct mutex write_lock;
104 wait_queue_head_t write_wait;
105 int32_t out_phys; /* physical address of write buffer */
106 char *out_data;
107 int mfield; /* meta field embedded in data */
108 int wflush; /*write flush */
109 int rflush; /*read flush*/
110 int out_frame_cnt;
111
112 struct msm_adsp_module *audrec;
113 struct msm_adsp_module *audpre;
114
115
116 /* configuration to use on next enable */
117 uint32_t samp_rate;
118 uint32_t channel_mode;
119 uint32_t buffer_size; /* Frame size (36 bytes) */
120 uint32_t enc_type; /* 11 for EVRC */
121 uint32_t mode; /* T or NT Mode*/
122
123 struct msm_audio_evrc_enc_config cfg;
124
125 uint32_t dsp_cnt;
126 uint32_t in_head; /* next buffer dsp will write */
127 uint32_t in_tail; /* next buffer read() will read */
128 uint32_t in_count; /* number of buffers available to read() */
129
130 uint32_t eos_ack;
131 uint32_t flush_ack;
132
133 const char *module_name;
134 unsigned queue_ids;
135 uint16_t enc_id; /* Session Id */
136
137 unsigned short samp_rate_index;
138 uint32_t audrec_obj_idx ;
139
140 struct audmgr audmgr;
141
142 /* data allocated for various buffers */
143 char *data;
144 dma_addr_t phys;
145
Laura Abbott35111d32012-04-27 18:41:48 -0700146 void *map_v_read;
147 void *map_v_write;
Santosh Mardi7faa0fa2011-09-22 15:32:23 +0530148
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700149 int opened;
150 int enabled;
151 int running;
152 int stopped; /* set when stopped, cleared on flush */
153};
154
155struct audio_frame {
156 uint16_t frame_count_lsw;
157 uint16_t frame_count_msw;
158 uint16_t frame_length;
159 uint16_t erased_pcm;
160 unsigned char raw_bitstream[];
161} __packed;
162
163struct audio_frame_nt {
164 uint16_t metadata_len;
165 uint16_t frame_count_lsw;
166 uint16_t frame_count_msw;
167 uint16_t frame_length;
168 uint16_t erased_pcm;
169 uint16_t reserved;
170 uint16_t time_stamp_dword_lsw;
171 uint16_t time_stamp_dword_msw;
172 uint16_t time_stamp_lsw;
173 uint16_t time_stamp_msw;
174 uint16_t nflag_lsw;
175 uint16_t nflag_msw;
176 unsigned char raw_bitstream[]; /* samples */
177} __packed;
178
179struct evrc_encoded_meta_out {
180 uint16_t metadata_len;
181 uint16_t time_stamp_dword_lsw;
182 uint16_t time_stamp_dword_msw;
183 uint16_t time_stamp_lsw;
184 uint16_t time_stamp_msw;
185 uint16_t nflag_lsw;
186 uint16_t nflag_msw;
187};
188
189/* Audrec Queue command sent macro's */
190#define audio_send_queue_pre(audio, cmd, len) \
191 msm_adsp_write(audio->audpre, QDSP_uPAudPreProcCmdQueue, cmd, len)
192
193#define audio_send_queue_recbs(audio, cmd, len) \
194 msm_adsp_write(audio->audrec, ((audio->queue_ids & 0xFFFF0000) >> 16),\
195 cmd, len)
196#define audio_send_queue_rec(audio, cmd, len) \
197 msm_adsp_write(audio->audrec, (audio->queue_ids & 0x0000FFFF),\
198 cmd, len)
199
200static int audevrc_in_dsp_enable(struct audio_evrc_in *audio, int enable);
201static int audevrc_in_encparam_config(struct audio_evrc_in *audio);
202static int audevrc_in_encmem_config(struct audio_evrc_in *audio);
203static int audevrc_in_dsp_read_buffer(struct audio_evrc_in *audio,
204 uint32_t read_cnt);
205static void audevrc_in_flush(struct audio_evrc_in *audio);
206
207static void audevrc_in_get_dsp_frames(struct audio_evrc_in *audio);
208static int audpcm_config(struct audio_evrc_in *audio);
209static void audevrc_out_flush(struct audio_evrc_in *audio);
210static int audevrc_in_routing_mode_config(struct audio_evrc_in *audio);
211static void audrec_pcm_send_data(struct audio_evrc_in *audio, unsigned needed);
212static void audevrc_nt_in_get_dsp_frames(struct audio_evrc_in *audio);
213static void audevrc_in_flush(struct audio_evrc_in *audio);
214
215static unsigned convert_samp_index(unsigned index)
216{
217 switch (index) {
218 case RPC_AUD_DEF_SAMPLE_RATE_48000: return 48000;
219 case RPC_AUD_DEF_SAMPLE_RATE_44100: return 44100;
220 case RPC_AUD_DEF_SAMPLE_RATE_32000: return 32000;
221 case RPC_AUD_DEF_SAMPLE_RATE_24000: return 24000;
222 case RPC_AUD_DEF_SAMPLE_RATE_22050: return 22050;
223 case RPC_AUD_DEF_SAMPLE_RATE_16000: return 16000;
224 case RPC_AUD_DEF_SAMPLE_RATE_12000: return 12000;
225 case RPC_AUD_DEF_SAMPLE_RATE_11025: return 11025;
226 case RPC_AUD_DEF_SAMPLE_RATE_8000: return 8000;
227 default: return 11025;
228 }
229}
230
231/* must be called with audio->lock held */
232static int audevrc_in_enable(struct audio_evrc_in *audio)
233{
234 struct audmgr_config cfg;
235 int rc;
236
237 if (audio->enabled)
238 return 0;
239
240 cfg.tx_rate = audio->samp_rate;
241 cfg.rx_rate = RPC_AUD_DEF_SAMPLE_RATE_NONE;
242 cfg.def_method = RPC_AUD_DEF_METHOD_RECORD;
243 cfg.codec = RPC_AUD_DEF_CODEC_EVRC;
244 cfg.snd_method = RPC_SND_METHOD_MIDI;
245
246 if (audio->mode == MSM_AUD_ENC_MODE_TUNNEL) {
247 rc = audmgr_enable(&audio->audmgr, &cfg);
248 if (rc < 0)
249 return rc;
250
251 if (msm_adsp_enable(audio->audpre)) {
252 audmgr_disable(&audio->audmgr);
253 MM_ERR("msm_adsp_enable(audpre) failed\n");
254 return -ENODEV;
255 }
256 }
257 if (msm_adsp_enable(audio->audrec)) {
258 if (audio->mode == MSM_AUD_ENC_MODE_TUNNEL) {
259 audmgr_disable(&audio->audmgr);
260 msm_adsp_disable(audio->audpre);
261 }
262 MM_ERR("msm_adsp_enable(audrec) failed\n");
263 return -ENODEV;
264 }
265
266 audio->enabled = 1;
267 audevrc_in_dsp_enable(audio, 1);
268
269 return 0;
270}
271
272/* must be called with audio->lock held */
273static int audevrc_in_disable(struct audio_evrc_in *audio)
274{
275 if (audio->enabled) {
276 audio->enabled = 0;
277
278 audevrc_in_dsp_enable(audio, 0);
279
280 wake_up(&audio->wait);
281 wait_event_interruptible_timeout(audio->wait_enable,
282 audio->running == 0, 1*HZ);
Manish Dewangan89a9f232012-02-09 17:14:40 +0530283 audio->stopped = 1;
284 wake_up(&audio->wait);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700285 msm_adsp_disable(audio->audrec);
286 if (audio->mode == MSM_AUD_ENC_MODE_TUNNEL) {
287 msm_adsp_disable(audio->audpre);
288 audmgr_disable(&audio->audmgr);
289 }
290 }
291 return 0;
292}
293
294/* ------------------- dsp --------------------- */
295static void audpre_dsp_event(void *data, unsigned id, size_t len,
296 void (*getevent)(void *ptr, size_t len))
297{
298 uint16_t msg[2];
299 getevent(msg, sizeof(msg));
300
301 switch (id) {
302 case AUDPREPROC_MSG_CMD_CFG_DONE_MSG:
303 MM_DBG("type %d, status_flag %d\n", msg[0], msg[1]);
304 break;
305 case AUDPREPROC_MSG_ERROR_MSG_ID:
306 MM_ERR("err_index %d\n", msg[0]);
307 break;
308 case ADSP_MESSAGE_ID:
309 MM_DBG("Received ADSP event: module enable(audpreproctask)\n");
310 break;
311 default:
312 MM_ERR("unknown event %d\n", id);
313 }
314}
315
316static void audevrc_in_get_dsp_frames(struct audio_evrc_in *audio)
317{
318 struct audio_frame *frame;
319 uint32_t index;
320 unsigned long flags;
321
322 index = audio->in_head;
323
324 frame = (void *) (((char *)audio->in[index].data) -
325 sizeof(*frame));
326 spin_lock_irqsave(&audio->dsp_lock, flags);
327 audio->in[index].size = frame->frame_length;
328
329 /* statistics of read */
330 atomic_add(audio->in[index].size, &audio->in_bytes);
331 atomic_add(1, &audio->in_samples);
332
333 audio->in_head = (audio->in_head + 1) & (FRAME_NUM - 1);
334
335 /* If overflow, move the tail index foward. */
336 if (audio->in_head == audio->in_tail) {
337 MM_ERR("Error! not able to keep up the read\n");
338 audio->in_tail = (audio->in_tail + 1) & (FRAME_NUM - 1);
339 MM_ERR("in_count = %d\n", audio->in_count);
340 } else
341 audio->in_count++;
342
343 audevrc_in_dsp_read_buffer(audio, audio->dsp_cnt++);
344 spin_unlock_irqrestore(&audio->dsp_lock, flags);
345
346 wake_up(&audio->wait);
347}
348
349static void audevrc_nt_in_get_dsp_frames(struct audio_evrc_in *audio)
350{
351 struct audio_frame_nt *nt_frame;
352 uint32_t index;
353 unsigned long flags;
354
355 index = audio->in_head;
356 nt_frame = (void *) (((char *)audio->in[index].data) - \
357 sizeof(struct audio_frame_nt));
358 spin_lock_irqsave(&audio->dsp_lock, flags);
359 audio->in[index].size = nt_frame->frame_length;
360 /* statistics of read */
361 atomic_add(audio->in[index].size, &audio->in_bytes);
362 atomic_add(1, &audio->in_samples);
363
364 audio->in_head = (audio->in_head + 1) & (FRAME_NUM - 1);
365
366 /* If overflow, move the tail index foward. */
367 if (audio->in_head == audio->in_tail)
368 MM_DBG("Error! not able to keep up the read\n");
369 else
370 audio->in_count++;
371
372 spin_unlock_irqrestore(&audio->dsp_lock, flags);
373 wake_up(&audio->wait);
374}
375
376static int audrec_pcm_buffer_ptr_refresh(struct audio_evrc_in *audio,
377 unsigned idx, unsigned len)
378{
379 struct audrec_cmd_pcm_buffer_ptr_refresh_arm_enc cmd;
380
381 if (len == NT_FRAME_HEADER_SIZE)
382 len = len / 2;
383 else
384 len = (len + NT_FRAME_HEADER_SIZE) / 2;
385 MM_DBG("len = %d\n", len);
386 memset(&cmd, 0, sizeof(cmd));
387 cmd.cmd_id = AUDREC_CMD_PCM_BUFFER_PTR_REFRESH_ARM_TO_ENC;
388 cmd.num_buffers = 1;
389 if (cmd.num_buffers == 1) {
390 cmd.buf_address_length[0] = (audio->out[idx].addr &
391 0xffff0000) >> 16;
392 cmd.buf_address_length[1] = (audio->out[idx].addr &
393 0x0000ffff);
394 cmd.buf_address_length[2] = (len & 0xffff0000) >> 16;
395 cmd.buf_address_length[3] = (len & 0x0000ffff);
396 }
397 audio->out_frame_cnt++;
398 return audio_send_queue_rec(audio, &cmd, sizeof(cmd));
399}
400
401static int audpcm_config(struct audio_evrc_in *audio)
402{
403 struct audrec_cmd_pcm_cfg_arm_to_enc cmd;
404 MM_DBG("\n");
405 memset(&cmd, 0, sizeof(cmd));
406 cmd.cmd_id = AUDREC_CMD_PCM_CFG_ARM_TO_ENC;
407 cmd.config_update_flag = AUDREC_PCM_CONFIG_UPDATE_FLAG_ENABLE;
408 cmd.enable_flag = AUDREC_ENABLE_FLAG_VALUE;
409 cmd.sampling_freq = convert_samp_index(audio->samp_rate);
410 if (!audio->channel_mode)
411 cmd.channels = 1;
412 else
413 cmd.channels = 2;
414 cmd.frequency_of_intimation = 1;
415 cmd.max_number_of_buffers = OUT_FRAME_NUM;
416 return audio_send_queue_rec(audio, &cmd, sizeof(cmd));
417}
418
419
420static int audevrc_in_routing_mode_config(struct audio_evrc_in *audio)
421{
422 struct audrec_cmd_routing_mode cmd;
423
424 MM_DBG("\n");
425 memset(&cmd, 0, sizeof(cmd));
426 cmd.cmd_id = AUDREC_CMD_ROUTING_MODE;
427 if (audio->mode == MSM_AUD_ENC_MODE_NONTUNNEL)
428 cmd.routing_mode = 1;
429 return audio_send_queue_rec(audio, &cmd, sizeof(cmd));
430}
431
432static void audrec_dsp_event(void *data, unsigned id, size_t len,
433 void (*getevent)(void *ptr, size_t len))
434{
435 struct audio_evrc_in *audio = NULL;
436
437 if (data)
438 audio = data;
439 else {
440 MM_ERR("invalid data for event %x\n", id);
441 return;
442 }
443
444 switch (id) {
445 case AUDREC_MSG_CMD_CFG_DONE_MSG: {
446 struct audrec_msg_cmd_cfg_done_msg cmd_cfg_done_msg;
447 getevent(&cmd_cfg_done_msg, AUDREC_MSG_CMD_CFG_DONE_MSG_LEN);
448 if (cmd_cfg_done_msg.audrec_enc_type & \
449 AUDREC_MSG_CFG_DONE_ENC_ENA) {
450 audio->audrec_obj_idx = cmd_cfg_done_msg.audrec_obj_idx;
451 MM_DBG("CFG ENABLED\n");
452 if (audio->mode == MSM_AUD_ENC_MODE_NONTUNNEL) {
453 MM_DBG("routing command\n");
454 audevrc_in_routing_mode_config(audio);
455 } else {
456 audevrc_in_encmem_config(audio);
457 }
458 } else {
459 MM_DBG("CFG SLEEP\n");
460 audio->running = 0;
461 wake_up(&audio->wait_enable);
462 }
463 break;
464 }
465 case AUDREC_MSG_CMD_ROUTING_MODE_DONE_MSG: {
466 struct audrec_msg_cmd_routing_mode_done_msg \
467 routing_msg;
468 getevent(&routing_msg, AUDREC_MSG_CMD_ROUTING_MODE_DONE_MSG);
469 MM_DBG("AUDREC_MSG_CMD_ROUTING_MODE_DONE_MSG");
470 if (routing_msg.configuration == 0) {
471 MM_ERR("routing configuration failed\n");
472 audio->running = 0;
473 wake_up(&audio->wait_enable);
474 } else
475 audevrc_in_encmem_config(audio);
476 break;
477 }
478 case AUDREC_MSG_CMD_AREC_MEM_CFG_DONE_MSG: {
479 MM_DBG("AREC_MEM_CFG_DONE_MSG\n");
480 if (audio->mode == MSM_AUD_ENC_MODE_TUNNEL)
481 audevrc_in_encparam_config(audio);
482 else
483 audpcm_config(audio);
484 break;
485 }
486 case AUDREC_CMD_PCM_CFG_ARM_TO_ENC_DONE_MSG: {
487 MM_DBG("AUDREC_CMD_PCM_CFG_ARM_TO_ENC_DONE_MSG");
488 audevrc_in_encparam_config(audio);
489 break;
490 }
491 case AUDREC_MSG_CMD_AREC_PARAM_CFG_DONE_MSG: {
492 MM_DBG("AUDREC_MSG_CMD_AREC_PARAM_CFG_DONE_MSG\n");
493 audio->running = 1;
494 wake_up(&audio->wait_enable);
495 if (audio->mode == MSM_AUD_ENC_MODE_NONTUNNEL)
496 audrec_pcm_send_data(audio, 1);
497 break;
498 }
499 case AUDREC_CMD_PCM_BUFFER_PTR_UPDATE_ARM_TO_ENC_MSG: {
500 MM_DBG("ptr_update recieved from DSP\n");
501 audrec_pcm_send_data(audio, 1);
502 break;
503 }
504 case AUDREC_MSG_NO_EXT_PKT_AVAILABLE_MSG: {
505 struct audrec_msg_no_ext_pkt_avail_msg err_msg;
506 getevent(&err_msg, AUDREC_MSG_NO_EXT_PKT_AVAILABLE_MSG_LEN);
507 MM_DBG("NO_EXT_PKT_AVAILABLE_MSG %x\n",\
508 err_msg.audrec_err_id);
509 break;
510 }
511 case AUDREC_MSG_PACKET_READY_MSG: {
512 struct audrec_msg_packet_ready_msg pkt_ready_msg;
513
514 getevent(&pkt_ready_msg, AUDREC_MSG_PACKET_READY_MSG_LEN);
515 MM_DBG("UP_PACKET_READY_MSG: write cnt msw %d \
516 write cnt lsw %d read cnt msw %d read cnt lsw %d \n",\
517 pkt_ready_msg.pkt_counter_msw, \
518 pkt_ready_msg.pkt_counter_lsw, \
519 pkt_ready_msg.pkt_read_cnt_msw, \
520 pkt_ready_msg.pkt_read_cnt_lsw);
521
522 audevrc_in_get_dsp_frames(audio);
523 break;
524 }
525 case AUDREC_UP_NT_PACKET_READY_MSG: {
526 struct audrec_up_nt_packet_ready_msg pkt_ready_msg;
527
528 getevent(&pkt_ready_msg, AUDREC_UP_NT_PACKET_READY_MSG_LEN);
529 MM_DBG("UP_NT_PACKET_READY_MSG: write cnt lsw %d \
530 write cnt msw %d read cnt lsw %d read cnt msw %d \n",\
531 pkt_ready_msg.audrec_packetwrite_cnt_lsw, \
532 pkt_ready_msg.audrec_packetwrite_cnt_msw, \
533 pkt_ready_msg.audrec_upprev_readcount_lsw, \
534 pkt_ready_msg.audrec_upprev_readcount_msw);
535
536 audevrc_nt_in_get_dsp_frames(audio);
537 break;
538 }
539 case AUDREC_CMD_FLUSH_DONE_MSG: {
540 audio->wflush = 0;
541 audio->rflush = 0;
542 audio->flush_ack = 1;
543 wake_up(&audio->write_wait);
544 MM_DBG("flush ack recieved\n");
545 break;
546 }
547 case ADSP_MESSAGE_ID:
548 MM_DBG("Received ADSP event: module \
549 enable/disable(audrectask)\n");
550 break;
551 default:
552 MM_ERR("unknown event %d\n", id);
553 }
554}
555
556static struct msm_adsp_ops audpre_evrc_adsp_ops = {
557 .event = audpre_dsp_event,
558};
559
560static struct msm_adsp_ops audrec_evrc_adsp_ops = {
561 .event = audrec_dsp_event,
562};
563
564static int audevrc_in_dsp_enable(struct audio_evrc_in *audio, int enable)
565{
566 struct audrec_cmd_enc_cfg cmd;
567
568 memset(&cmd, 0, sizeof(cmd));
569 cmd.cmd_id = AUDREC_CMD_ENC_CFG;
570 cmd.audrec_enc_type = (audio->enc_type & 0xFF) |
571 (enable ? AUDREC_CMD_ENC_ENA : AUDREC_CMD_ENC_DIS);
572 /* Don't care */
573 cmd.audrec_obj_idx = audio->audrec_obj_idx;
574
575 return audio_send_queue_rec(audio, &cmd, sizeof(cmd));
576}
577
578static int audevrc_in_encmem_config(struct audio_evrc_in *audio)
579{
580 struct audrec_cmd_arecmem_cfg cmd;
581 uint16_t *data = (void *) audio->data;
582 int n;
583 int header_len = 0;
584
585 memset(&cmd, 0, sizeof(cmd));
586
587 cmd.cmd_id = AUDREC_CMD_ARECMEM_CFG;
588 cmd.audrec_obj_idx = audio->audrec_obj_idx;
589 /* Rate at which packet complete message comes */
590 cmd.audrec_up_pkt_intm_cnt = 1;
591 cmd.audrec_extpkt_buffer_msw = audio->phys >> 16;
592 cmd.audrec_extpkt_buffer_lsw = audio->phys;
593 /* Max Buffer no available for frames */
594 cmd.audrec_extpkt_buffer_num = FRAME_NUM;
595
596 /* prepare buffer pointers:
597 * T:36 bytes evrc packet + 4 halfword header
598 * NT:36 bytes evrc packet + 12 halfword header
599 */
600 if (audio->mode == MSM_AUD_ENC_MODE_TUNNEL)
601 header_len = FRAME_HEADER_SIZE/2;
602 else
603 header_len = NT_FRAME_HEADER_SIZE/2;
604
605 for (n = 0; n < FRAME_NUM; n++) {
606 audio->in[n].data = data + header_len;
607 data += (EVRC_FRAME_SIZE/2) + header_len;
608 MM_DBG("0x%8x\n", (int)(audio->in[n].data - header_len*2));
609 }
610
611 return audio_send_queue_rec(audio, &cmd, sizeof(cmd));
612}
613
614static int audevrc_in_encparam_config(struct audio_evrc_in *audio)
615{
616 struct audrec_cmd_arecparam_evrc_cfg cmd;
617
618 memset(&cmd, 0, sizeof(cmd));
619 cmd.common.cmd_id = AUDREC_CMD_ARECPARAM_CFG;
620 cmd.common.audrec_obj_idx = audio->audrec_obj_idx;
621 cmd.enc_min_rate = audio->cfg.min_bit_rate;
622 cmd.enc_max_rate = audio->cfg.max_bit_rate;
623 cmd.rate_modulation_cmd = 0; /* Default set to 0 */
624
625 return audio_send_queue_rec(audio, &cmd, sizeof(cmd));
626}
627
628static int audevrc_flush_command(struct audio_evrc_in *audio)
629{
630 struct audrec_cmd_flush cmd;
631 MM_DBG("\n");
632 memset(&cmd, 0, sizeof(cmd));
633 cmd.cmd_id = AUDREC_CMD_FLUSH;
634 return audio_send_queue_rec(audio, &cmd, sizeof(cmd));
635}
636
637static int audevrc_in_dsp_read_buffer(struct audio_evrc_in *audio,
638 uint32_t read_cnt)
639{
640 audrec_cmd_packet_ext_ptr cmd;
641
642 memset(&cmd, 0, sizeof(cmd));
643 cmd.cmd_id = AUDREC_CMD_PACKET_EXT_PTR;
644 cmd.type = audio->audrec_obj_idx;
645 cmd.curr_rec_count_msw = read_cnt >> 16;
646 cmd.curr_rec_count_lsw = read_cnt;
647
648 return audio_send_queue_recbs(audio, &cmd, sizeof(cmd));
649}
650
651/* ------------------- device --------------------- */
652
653static void audevrc_ioport_reset(struct audio_evrc_in *audio)
654{
655 /* Make sure read/write thread are free from
656 * sleep and knowing that system is not able
657 * to process io request at the moment
658 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700659 wake_up(&audio->wait);
660 mutex_lock(&audio->read_lock);
Manish Dewangana4f1df02012-02-08 17:06:54 +0530661 audevrc_in_flush(audio);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700662 mutex_unlock(&audio->read_lock);
Manish Dewangana4f1df02012-02-08 17:06:54 +0530663 wake_up(&audio->write_wait);
664 mutex_lock(&audio->write_lock);
665 audevrc_out_flush(audio);
666 mutex_unlock(&audio->write_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700667}
668
669static void audevrc_in_flush(struct audio_evrc_in *audio)
670{
671 int i;
Manish Dewangana4f1df02012-02-08 17:06:54 +0530672 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700673
674 audio->dsp_cnt = 0;
Manish Dewangana4f1df02012-02-08 17:06:54 +0530675 spin_lock_irqsave(&audio->dsp_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700676 audio->in_head = 0;
677 audio->in_tail = 0;
678 audio->in_count = 0;
679 audio->eos_ack = 0;
680 for (i = FRAME_NUM-1; i >= 0; i--) {
681 audio->in[i].size = 0;
682 audio->in[i].read = 0;
683 }
Manish Dewangana4f1df02012-02-08 17:06:54 +0530684 spin_unlock_irqrestore(&audio->dsp_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700685 MM_DBG("in_bytes %d\n", atomic_read(&audio->in_bytes));
686 MM_DBG("in_samples %d\n", atomic_read(&audio->in_samples));
687 atomic_set(&audio->in_bytes, 0);
688 atomic_set(&audio->in_samples, 0);
689}
690
691static void audevrc_out_flush(struct audio_evrc_in *audio)
692{
693 int i;
Manish Dewangana4f1df02012-02-08 17:06:54 +0530694 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700695
696 audio->out_head = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700697 audio->out_count = 0;
Manish Dewangana4f1df02012-02-08 17:06:54 +0530698 spin_lock_irqsave(&audio->dsp_lock, flags);
699 audio->out_tail = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700700 for (i = OUT_FRAME_NUM-1; i >= 0; i--) {
701 audio->out[i].size = 0;
702 audio->out[i].read = 0;
703 audio->out[i].used = 0;
704 }
Manish Dewangana4f1df02012-02-08 17:06:54 +0530705 spin_unlock_irqrestore(&audio->dsp_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700706}
707
708/* ------------------- device --------------------- */
709static long audevrc_in_ioctl(struct file *file,
710 unsigned int cmd, unsigned long arg)
711{
712 struct audio_evrc_in *audio = file->private_data;
713 int rc = 0;
714
715 MM_DBG("\n");
716 if (cmd == AUDIO_GET_STATS) {
717 struct msm_audio_stats stats;
718 stats.byte_count = atomic_read(&audio->in_bytes);
719 stats.sample_count = atomic_read(&audio->in_samples);
720 if (copy_to_user((void *) arg, &stats, sizeof(stats)))
721 return -EFAULT;
722 return rc;
723 }
724
725 mutex_lock(&audio->lock);
726 switch (cmd) {
727 case AUDIO_START: {
728 rc = audevrc_in_enable(audio);
729 if (!rc) {
730 rc =
731 wait_event_interruptible_timeout(audio->wait_enable,
732 audio->running != 0, 1*HZ);
733 MM_DBG("state %d rc = %d\n", audio->running, rc);
734
735 if (audio->running == 0)
736 rc = -ENODEV;
737 else
738 rc = 0;
739 }
740 audio->stopped = 0;
741 break;
742 }
743 case AUDIO_STOP: {
744 rc = audevrc_in_disable(audio);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700745 break;
746 }
747 case AUDIO_FLUSH: {
748 MM_DBG("AUDIO_FLUSH\n");
749 audio->rflush = 1;
750 audio->wflush = 1;
751 audevrc_ioport_reset(audio);
752 if (audio->running) {
753 audevrc_flush_command(audio);
754 rc = wait_event_interruptible(audio->write_wait,
755 !audio->wflush);
756 if (rc < 0) {
757 MM_ERR("AUDIO_FLUSH interrupted\n");
758 rc = -EINTR;
759 }
760 } else {
761 audio->rflush = 0;
762 audio->wflush = 0;
763 }
764 break;
765 }
766 case AUDIO_GET_CONFIG: {
767 struct msm_audio_config cfg;
768 memset(&cfg, 0, sizeof(cfg));
769 cfg.buffer_size = OUT_BUFFER_SIZE;
770 cfg.buffer_count = OUT_FRAME_NUM;
771 cfg.sample_rate = convert_samp_index(audio->samp_rate);
772 cfg.channel_count = 1;
773 cfg.type = 0;
774 cfg.unused[0] = 0;
775 cfg.unused[1] = 0;
776 cfg.unused[2] = 0;
777 if (copy_to_user((void *) arg, &cfg, sizeof(cfg)))
778 rc = -EFAULT;
779 else
780 rc = 0;
781 break;
782 }
783 case AUDIO_GET_STREAM_CONFIG: {
784 struct msm_audio_stream_config cfg;
785 memset(&cfg, 0, sizeof(cfg));
786 cfg.buffer_size = audio->buffer_size;
787 cfg.buffer_count = FRAME_NUM;
788 if (copy_to_user((void *)arg, &cfg, sizeof(cfg)))
789 rc = -EFAULT;
790 else
791 rc = 0;
792 break;
793 }
794 case AUDIO_SET_STREAM_CONFIG: {
795 struct msm_audio_stream_config cfg;
796 if (copy_from_user(&cfg, (void *) arg, sizeof(cfg))) {
797 rc = -EFAULT;
798 break;
799 }
800 /* Allow only single frame */
801 if (audio->mode == MSM_AUD_ENC_MODE_TUNNEL) {
802 if (cfg.buffer_size != (FRAME_SIZE - 8))
803 rc = -EINVAL;
804 break;
805 } else {
806 if (cfg.buffer_size != (EVRC_FRAME_SIZE + 14))
807 rc = -EINVAL;
808 break;
809 }
810 audio->buffer_size = cfg.buffer_size;
811 break;
812 }
813 case AUDIO_GET_EVRC_ENC_CONFIG: {
814 if (copy_to_user((void *) arg, &audio->cfg, sizeof(audio->cfg)))
815 rc = -EFAULT;
816 break;
817 }
818 case AUDIO_SET_EVRC_ENC_CONFIG: {
819 struct msm_audio_evrc_enc_config cfg;
820 if (copy_from_user(&cfg, (void *) arg, sizeof(cfg))) {
821 rc = -EFAULT;
822 break;
823 }
824 MM_DBG("0X%8x, 0x%8x, 0x%8x\n", cfg.min_bit_rate,
825 cfg.max_bit_rate, cfg.cdma_rate);
826 if (cfg.min_bit_rate > CDMA_RATE_FULL || \
827 cfg.min_bit_rate < CDMA_RATE_EIGHTH) {
828 MM_ERR("invalid min bitrate\n");
829 rc = -EFAULT;
830 break;
831 }
832 if (cfg.max_bit_rate > CDMA_RATE_FULL || \
833 cfg.max_bit_rate < CDMA_RATE_EIGHTH) {
834 MM_ERR("invalid max bitrate\n");
835 rc = -EFAULT;
836 break;
837 }
838 /* Recording Does not support Erase and Blank */
839 if (cfg.cdma_rate > CDMA_RATE_FULL ||
840 cfg.cdma_rate < CDMA_RATE_EIGHTH) {
841 MM_ERR("invalid qcelp cdma rate\n");
842 rc = -EFAULT;
843 break;
844 }
845 memcpy(&audio->cfg, &cfg, sizeof(cfg));
846 break;
847 }
848 default:
849 rc = -EINVAL;
850 }
851 mutex_unlock(&audio->lock);
852 return rc;
853}
854
855static ssize_t audevrc_in_read(struct file *file,
856 char __user *buf,
857 size_t count, loff_t *pos)
858{
859 struct audio_evrc_in *audio = file->private_data;
860 unsigned long flags;
861 const char __user *start = buf;
862 void *data;
863 uint32_t index;
864 uint32_t size;
865 int rc = 0;
866 struct evrc_encoded_meta_out meta_field;
867 struct audio_frame_nt *nt_frame;
868 MM_DBG("count = %d\n", count);
869 mutex_lock(&audio->read_lock);
870 while (count > 0) {
871 rc = wait_event_interruptible(
872 audio->wait, (audio->in_count > 0) || audio->stopped ||
873 audio->rflush);
874 if (rc < 0)
875 break;
876
877 if (audio->rflush) {
878 rc = -EBUSY;
879 break;
880 }
881 if (audio->stopped && !audio->in_count) {
882 MM_DBG("Driver in stop state, No more buffer to read");
883 rc = 0;/* End of File */
884 break;
885 }
886
887 index = audio->in_tail;
888 data = (uint8_t *) audio->in[index].data;
889 size = audio->in[index].size;
890
891 if (audio->mode == MSM_AUD_ENC_MODE_NONTUNNEL) {
892 nt_frame = (struct audio_frame_nt *)(data -
893 sizeof(struct audio_frame_nt));
894 memcpy((char *)&meta_field.time_stamp_dword_lsw,
895 (char *)&nt_frame->time_stamp_dword_lsw,
896 (sizeof(struct evrc_encoded_meta_out) - \
897 sizeof(uint16_t)));
898 meta_field.metadata_len =
899 sizeof(struct evrc_encoded_meta_out);
900 if (copy_to_user((char *)start, (char *)&meta_field,
901 sizeof(struct evrc_encoded_meta_out))) {
902 rc = -EFAULT;
903 break;
904 }
905 if (nt_frame->nflag_lsw & 0x0001) {
906 MM_ERR("recieved EOS in read call\n");
907 audio->eos_ack = 1;
908 }
909 buf += sizeof(struct evrc_encoded_meta_out);
910 count -= sizeof(struct evrc_encoded_meta_out);
911 }
912 if (count >= size) {
913 /* order the reads on the buffer */
914 dma_coherent_post_ops();
915 if (copy_to_user(buf, data, size)) {
916 rc = -EFAULT;
917 break;
918 }
919 spin_lock_irqsave(&audio->dsp_lock, flags);
920 if (index != audio->in_tail) {
921 /* overrun -- data is
922 * invalid and we need to retry */
923 spin_unlock_irqrestore(&audio->dsp_lock, flags);
924 continue;
925 }
926 audio->in[index].size = 0;
927 audio->in_tail = (audio->in_tail + 1) & (FRAME_NUM - 1);
928 audio->in_count--;
929 spin_unlock_irqrestore(&audio->dsp_lock, flags);
930 count -= size;
931 buf += size;
932 if ((audio->mode == MSM_AUD_ENC_MODE_NONTUNNEL)) {
933 if (!audio->eos_ack) {
934 MM_DBG("sending read ptr command \
935 %d %d\n",
936 audio->dsp_cnt,
937 audio->in_tail);
938 audevrc_in_dsp_read_buffer(audio,
939 audio->dsp_cnt++);
940 }
941 }
942 } else {
943 MM_ERR("short read\n");
944 break;
945 }
946 break;
947 }
948 mutex_unlock(&audio->read_lock);
949
950 if (buf > start)
951 return buf - start;
952
953 return rc;
954}
955
956static void audrec_pcm_send_data(struct audio_evrc_in *audio, unsigned needed)
957{
958 struct buffer *frame;
959 unsigned long flags;
960 MM_DBG("\n");
961 spin_lock_irqsave(&audio->dsp_lock, flags);
962 if (!audio->running)
963 goto done;
964
965 if (needed && !audio->wflush) {
966 /* We were called from the callback because the DSP
967 * requested more data. Note that the DSP does want
968 * more data, and if a buffer was in-flight, mark it
969 * as available (since the DSP must now be done with
970 * it).
971 */
972 audio->out_needed = 1;
973 frame = audio->out + audio->out_tail;
974 if (frame->used == 0xffffffff) {
975 MM_DBG("frame %d free\n", audio->out_tail);
976 frame->used = 0;
977 audio->out_tail ^= 1;
978 wake_up(&audio->write_wait);
979 }
980 }
981
982 if (audio->out_needed) {
983 /* If the DSP currently wants data and we have a
984 * buffer available, we will send it and reset
985 * the needed flag. We'll mark the buffer as in-flight
986 * so that it won't be recycled until the next buffer
987 * is requested
988 */
989
990 frame = audio->out + audio->out_tail;
991 if (frame->used) {
992 BUG_ON(frame->used == 0xffffffff);
993 audrec_pcm_buffer_ptr_refresh(audio,
994 audio->out_tail,
995 frame->used);
996 frame->used = 0xffffffff;
997 audio->out_needed = 0;
998 }
999 }
1000 done:
1001 spin_unlock_irqrestore(&audio->dsp_lock, flags);
1002}
1003
1004static int audevrc_in_fsync(struct file *file, int datasync)
1005
1006{
1007 struct audio_evrc_in *audio = file->private_data;
1008 int rc = 0;
1009
1010 MM_DBG("\n"); /* Macro prints the file name and function */
1011 if (!audio->running || (audio->mode == MSM_AUD_ENC_MODE_TUNNEL)) {
1012 rc = -EINVAL;
1013 goto done_nolock;
1014 }
1015
1016 mutex_lock(&audio->write_lock);
1017
1018 rc = wait_event_interruptible(audio->write_wait,
1019 audio->wflush);
1020 MM_DBG("waked on by some event audio->wflush = %d\n", audio->wflush);
1021
1022 if (rc < 0)
1023 goto done;
1024 else if (audio->wflush) {
1025 rc = -EBUSY;
1026 goto done;
1027 }
1028done:
1029 mutex_unlock(&audio->write_lock);
1030done_nolock:
1031 return rc;
1032
1033}
1034
1035int audrec_evrc_process_eos(struct audio_evrc_in *audio,
1036 const char __user *buf_start, unsigned short mfield_size)
1037{
1038 struct buffer *frame;
1039 int rc = 0;
1040
1041 frame = audio->out + audio->out_head;
1042
1043 rc = wait_event_interruptible(audio->write_wait,
1044 (audio->out_needed &&
1045 audio->out[0].used == 0 &&
1046 audio->out[1].used == 0)
1047 || (audio->stopped)
1048 || (audio->wflush));
1049
1050 if (rc < 0)
1051 goto done;
1052 if (audio->stopped || audio->wflush) {
1053 rc = -EBUSY;
1054 goto done;
1055 }
1056 if (copy_from_user(frame->data, buf_start, mfield_size)) {
1057 rc = -EFAULT;
1058 goto done;
1059 }
1060
1061 frame->mfield_sz = mfield_size;
1062 audio->out_head ^= 1;
1063 frame->used = mfield_size;
1064 MM_DBG("copying meta_out frame->used = %d\n", frame->used);
1065 audrec_pcm_send_data(audio, 0);
1066done:
1067 return rc;
1068}
1069
1070static ssize_t audevrc_in_write(struct file *file,
1071 const char __user *buf,
1072 size_t count, loff_t *pos)
1073{
1074 struct audio_evrc_in *audio = file->private_data;
1075 const char __user *start = buf;
1076 struct buffer *frame;
1077 char *cpy_ptr;
1078 int rc = 0, eos_condition = AUDPREPROC_EVRC_EOS_NONE;
1079 unsigned short mfield_size = 0;
1080 int write_count = 0;
1081 MM_DBG("cnt=%d\n", count);
1082
1083 if (count & 1)
1084 return -EINVAL;
1085
1086 if (audio->mode != MSM_AUD_ENC_MODE_NONTUNNEL)
1087 return -EINVAL;
1088
1089 mutex_lock(&audio->write_lock);
1090 frame = audio->out + audio->out_head;
1091 /* if supplied count is more than driver buffer size
1092 * then only copy driver buffer size
1093 */
1094 if (count > frame->size)
1095 count = frame->size;
1096
1097 write_count = count;
1098 cpy_ptr = frame->data;
1099 rc = wait_event_interruptible(audio->write_wait,
1100 (frame->used == 0)
1101 || (audio->stopped)
1102 || (audio->wflush));
1103 if (rc < 0)
1104 goto error;
1105
1106 if (audio->stopped || audio->wflush) {
1107 rc = -EBUSY;
1108 goto error;
1109 }
1110 if (audio->mfield) {
1111 if (buf == start) {
1112 /* Processing beginning of user buffer */
1113 if (__get_user(mfield_size,
1114 (unsigned short __user *) buf)) {
1115 rc = -EFAULT;
1116 goto error;
1117 } else if (mfield_size > count) {
1118 rc = -EINVAL;
1119 goto error;
1120 }
1121 MM_DBG("mf offset_val %x\n", mfield_size);
1122 if (copy_from_user(cpy_ptr, buf, mfield_size)) {
1123 rc = -EFAULT;
1124 goto error;
1125 }
1126 /* Check if EOS flag is set and buffer has
1127 * contains just meta field
1128 */
1129 if (cpy_ptr[AUDPREPROC_EVRC_EOS_FLG_OFFSET] &
1130 AUDPREPROC_EVRC_EOS_FLG_MASK) {
1131 eos_condition = AUDPREPROC_EVRC_EOS_SET;
1132 MM_DBG("EOS SET\n");
1133 if (mfield_size == count) {
1134 buf += mfield_size;
1135 eos_condition = 0;
1136 goto exit;
1137 } else
1138 cpy_ptr[AUDPREPROC_EVRC_EOS_FLG_OFFSET] &=
1139 ~AUDPREPROC_EVRC_EOS_FLG_MASK;
1140 }
1141 cpy_ptr += mfield_size;
1142 count -= mfield_size;
1143 buf += mfield_size;
1144 } else {
1145 mfield_size = 0;
1146 MM_DBG("continuous buffer\n");
1147 }
1148 frame->mfield_sz = mfield_size;
1149 }
1150 MM_DBG("copying the stream count = %d\n", count);
1151 if (copy_from_user(cpy_ptr, buf, count)) {
1152 rc = -EFAULT;
1153 goto error;
1154 }
1155exit:
1156 frame->used = count;
1157 audio->out_head ^= 1;
1158 if (!audio->flush_ack)
1159 audrec_pcm_send_data(audio, 0);
1160 else {
1161 audrec_pcm_send_data(audio, 1);
1162 audio->flush_ack = 0;
1163 }
1164 if (eos_condition == AUDPREPROC_EVRC_EOS_SET)
1165 rc = audrec_evrc_process_eos(audio, start, mfield_size);
1166 mutex_unlock(&audio->write_lock);
1167 return write_count;
1168error:
1169 mutex_unlock(&audio->write_lock);
1170 return rc;
1171}
1172
1173static int audevrc_in_release(struct inode *inode, struct file *file)
1174{
1175 struct audio_evrc_in *audio = file->private_data;
1176
1177 mutex_lock(&audio->lock);
1178 audevrc_in_disable(audio);
1179 audevrc_in_flush(audio);
1180 msm_adsp_put(audio->audrec);
1181
1182 if (audio->mode == MSM_AUD_ENC_MODE_TUNNEL)
1183 msm_adsp_put(audio->audpre);
1184
1185 audpreproc_aenc_free(audio->enc_id);
1186 audio->audrec = NULL;
1187 audio->audpre = NULL;
1188 audio->opened = 0;
1189 if ((audio->mode == MSM_AUD_ENC_MODE_NONTUNNEL) && \
1190 (audio->out_data)) {
Laura Abbott35111d32012-04-27 18:41:48 -07001191 iounmap(audio->map_v_write);
Santosh Mardi7faa0fa2011-09-22 15:32:23 +05301192 free_contiguous_memory_by_paddr(audio->out_phys);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001193 audio->out_data = NULL;
1194 }
1195 if (audio->data) {
Laura Abbott35111d32012-04-27 18:41:48 -07001196 iounmap(audio->map_v_read);
Santosh Mardi7faa0fa2011-09-22 15:32:23 +05301197 free_contiguous_memory_by_paddr(audio->phys);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001198 audio->data = NULL;
1199 }
1200 mutex_unlock(&audio->lock);
1201 return 0;
1202}
1203
1204static struct audio_evrc_in the_audio_evrc_in;
1205
1206static int audevrc_in_open(struct inode *inode, struct file *file)
1207{
1208 struct audio_evrc_in *audio = &the_audio_evrc_in;
1209 int rc;
1210 int encid;
1211 int dma_size = 0;
1212
1213 mutex_lock(&audio->lock);
1214 if (audio->opened) {
1215 rc = -EBUSY;
1216 goto done;
1217 }
1218 if ((file->f_mode & FMODE_WRITE) &&
1219 (file->f_mode & FMODE_READ)) {
1220 audio->mode = MSM_AUD_ENC_MODE_NONTUNNEL;
1221 dma_size = NT_DMASZ;
1222 MM_DBG("Opened for non tunnel mode encoding\n");
1223 } else if (!(file->f_mode & FMODE_WRITE) &&
1224 (file->f_mode & FMODE_READ)) {
1225 audio->mode = MSM_AUD_ENC_MODE_TUNNEL;
1226 dma_size = DMASZ;
1227 MM_DBG("Opened for tunnel mode encoding\n");
1228 } else {
1229 MM_ERR("Invalid mode\n");
1230 rc = -EACCES;
1231 goto done;
1232 }
1233
1234 /* Settings will be re-config at AUDIO_SET_CONFIG,
1235 * but at least we need to have initial config
1236 */
1237 audio->samp_rate = RPC_AUD_DEF_SAMPLE_RATE_8000,
1238 audio->samp_rate_index = AUDREC_CMD_SAMP_RATE_INDX_8000;
1239 audio->channel_mode = AUDREC_CMD_STEREO_MODE_MONO;
1240 if (audio->mode == MSM_AUD_ENC_MODE_NONTUNNEL)
1241 audio->buffer_size = (EVRC_FRAME_SIZE + 14);
1242 else
1243 audio->buffer_size = EVRC_FRAME_SIZE;
1244 audio->enc_type = AUDREC_CMD_TYPE_0_INDEX_EVRC | audio->mode;
1245
1246 audio->cfg.cdma_rate = CDMA_RATE_FULL;
1247 audio->cfg.min_bit_rate = CDMA_RATE_FULL;
1248 audio->cfg.max_bit_rate = CDMA_RATE_FULL;
1249
1250 if (audio->mode == MSM_AUD_ENC_MODE_TUNNEL) {
1251 rc = audmgr_open(&audio->audmgr);
1252 if (rc)
1253 goto done;
1254 }
1255
1256 encid = audpreproc_aenc_alloc(audio->enc_type, &audio->module_name,
1257 &audio->queue_ids);
1258 if (encid < 0) {
1259 MM_ERR("No free encoder available\n");
1260 rc = -ENODEV;
1261 goto done;
1262 }
1263 audio->enc_id = encid;
1264
1265 rc = msm_adsp_get(audio->module_name, &audio->audrec,
1266 &audrec_evrc_adsp_ops, audio);
1267 if (rc) {
1268 audpreproc_aenc_free(audio->enc_id);
1269 goto done;
1270 }
1271
1272 if (audio->mode == MSM_AUD_ENC_MODE_TUNNEL) {
1273 rc = msm_adsp_get("AUDPREPROCTASK", &audio->audpre,
1274 &audpre_evrc_adsp_ops, audio);
1275 if (rc) {
1276 msm_adsp_put(audio->audrec);
1277 audpreproc_aenc_free(audio->enc_id);
1278 goto done;
1279 }
1280 }
1281
1282 audio->dsp_cnt = 0;
1283 audio->stopped = 0;
1284 audio->wflush = 0;
1285 audio->rflush = 0;
1286 audio->flush_ack = 0;
1287
1288 audevrc_in_flush(audio);
1289 audevrc_out_flush(audio);
1290
Santosh Mardi7faa0fa2011-09-22 15:32:23 +05301291 audio->phys = allocate_contiguous_ebi_nomap(dma_size, SZ_4K);
1292 if (!audio->phys) {
1293 MM_ERR("could not allocate physical read buffers\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001294 rc = -ENOMEM;
1295 goto evt_error;
1296 } else {
Laura Abbott35111d32012-04-27 18:41:48 -07001297 audio->map_v_read = ioremap(audio->phys, dma_size);
Santosh Mardi7faa0fa2011-09-22 15:32:23 +05301298 if (IS_ERR(audio->map_v_read)) {
1299 MM_ERR("could not map physical address\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001300 rc = -ENOMEM;
Santosh Mardi7faa0fa2011-09-22 15:32:23 +05301301 free_contiguous_memory_by_paddr(audio->phys);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001302 goto evt_error;
1303 }
Laura Abbott35111d32012-04-27 18:41:48 -07001304 audio->data = audio->map_v_read;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001305 MM_DBG("read buf: phy addr 0x%08x kernel addr 0x%08x\n",
1306 audio->phys, (int)audio->data);
1307 }
1308 audio->out_data = NULL;
1309 if (audio->mode == MSM_AUD_ENC_MODE_NONTUNNEL) {
Santosh Mardi7faa0fa2011-09-22 15:32:23 +05301310 audio->out_phys = allocate_contiguous_ebi_nomap(BUFFER_SIZE,
1311 SZ_4K);
1312 if (!audio->out_phys) {
1313 MM_ERR("could not allocate physical write buffers\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001314 rc = -ENOMEM;
Laura Abbott35111d32012-04-27 18:41:48 -07001315 iounmap(audio->map_v_read);
Santosh Mardi7faa0fa2011-09-22 15:32:23 +05301316 free_contiguous_memory_by_paddr(audio->phys);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001317 goto evt_error;
1318 } else {
Laura Abbott35111d32012-04-27 18:41:48 -07001319 audio->map_v_write = ioremap(
1320 audio->out_phys, BUFFER_SIZE);
Santosh Mardi7faa0fa2011-09-22 15:32:23 +05301321
1322 if (IS_ERR(audio->map_v_write)) {
1323 MM_ERR("could not map write phys address\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001324 rc = -ENOMEM;
Laura Abbott35111d32012-04-27 18:41:48 -07001325 iounmap(audio->map_v_read);
Santosh Mardi7faa0fa2011-09-22 15:32:23 +05301326 free_contiguous_memory_by_paddr(audio->phys);
1327 free_contiguous_memory_by_paddr(\
1328 audio->out_phys);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001329 goto evt_error;
1330 }
Laura Abbott35111d32012-04-27 18:41:48 -07001331 audio->out_data = audio->map_v_write;
Santosh Mardi7faa0fa2011-09-22 15:32:23 +05301332 MM_DBG("wr buf: phy addr 0x%08x kernel addr 0x%08x\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001333 audio->out_phys, (int)audio->out_data);
1334 }
1335
1336 /* Initialize buffer */
1337 audio->out[0].data = audio->out_data + 0;
1338 audio->out[0].addr = audio->out_phys + 0;
1339 audio->out[0].size = OUT_BUFFER_SIZE;
1340
1341 audio->out[1].data = audio->out_data + OUT_BUFFER_SIZE;
1342 audio->out[1].addr = audio->out_phys + OUT_BUFFER_SIZE;
1343 audio->out[1].size = OUT_BUFFER_SIZE;
1344
1345 MM_DBG("audio->out[0].data = %d audio->out[1].data = %d",
1346 (unsigned int)audio->out[0].data,
1347 (unsigned int)audio->out[1].data);
1348 audio->mfield = NT_FRAME_HEADER_SIZE;
1349 audio->out_frame_cnt++;
1350 }
1351 file->private_data = audio;
1352 audio->opened = 1;
1353
1354done:
1355 mutex_unlock(&audio->lock);
1356 return rc;
1357evt_error:
1358 msm_adsp_put(audio->audrec);
1359 if (audio->mode == MSM_AUD_ENC_MODE_TUNNEL)
1360 msm_adsp_put(audio->audpre);
1361
1362 audpreproc_aenc_free(audio->enc_id);
1363 mutex_unlock(&audio->lock);
1364 return rc;
1365}
1366
1367static const struct file_operations audio_evrc_in_fops = {
1368 .owner = THIS_MODULE,
1369 .open = audevrc_in_open,
1370 .release = audevrc_in_release,
1371 .read = audevrc_in_read,
1372 .write = audevrc_in_write,
1373 .fsync = audevrc_in_fsync,
1374 .unlocked_ioctl = audevrc_in_ioctl,
1375};
1376
1377static struct miscdevice audevrc_in_misc = {
1378 .minor = MISC_DYNAMIC_MINOR,
1379 .name = "msm_evrc_in",
1380 .fops = &audio_evrc_in_fops,
1381};
1382
1383static int __init audevrc_in_init(void)
1384{
1385 mutex_init(&the_audio_evrc_in.lock);
1386 mutex_init(&the_audio_evrc_in.read_lock);
1387 spin_lock_init(&the_audio_evrc_in.dsp_lock);
1388 init_waitqueue_head(&the_audio_evrc_in.wait);
1389 init_waitqueue_head(&the_audio_evrc_in.wait_enable);
1390 mutex_init(&the_audio_evrc_in.write_lock);
1391 init_waitqueue_head(&the_audio_evrc_in.write_wait);
1392 return misc_register(&audevrc_in_misc);
1393}
1394device_initcall(audevrc_in_init);