blob: 37a6726b7c9b5542d1d3327aedf70cbac99b50ff [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/*
2 * qcelp audio input device
3 *
4 * Copyright (C) 2008 Google, Inc.
5 * Copyright (C) 2008 HTC Corporation
Asish Bhattacharya4a064192013-10-01 17:15:05 +05306 * Copyright (c) 2009-2013, The Linux Foundation. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07007 *
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
Santosh Mardifdc227a2011-07-11 17:20:34 +053019#include <asm/atomic.h>
20#include <asm/ioctls.h>
21
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070022#include <linux/module.h>
23#include <linux/fs.h>
24#include <linux/miscdevice.h>
25#include <linux/uaccess.h>
26#include <linux/sched.h>
27#include <linux/wait.h>
28#include <linux/dma-mapping.h>
29#include <linux/msm_audio_qcp.h>
Sidipotu Ashok1e4d3472012-09-05 15:36:11 +053030#include <linux/msm_ion.h>
Santosh Mardifdc227a2011-07-11 17:20:34 +053031#include <linux/memory_alloc.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070032
33#include <mach/msm_adsp.h>
Santosh Mardifdc227a2011-07-11 17:20:34 +053034#include <mach/iommu.h>
35#include <mach/iommu_domains.h>
Kalyani polepeddy7413d2b2011-10-14 15:42:15 +053036#include <mach/socinfo.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070037#include <mach/qdsp5v2/qdsp5audreccmdi.h>
38#include <mach/qdsp5v2/qdsp5audrecmsg.h>
39#include <mach/qdsp5v2/audpreproc.h>
40#include <mach/qdsp5v2/audio_dev_ctl.h>
41#include <mach/debug_mm.h>
Santosh Mardifdc227a2011-07-11 17:20:34 +053042#include <mach/msm_memtypes.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070043
44#define META_OUT_SIZE 24
45/* FRAME_NUM must be a power of two */
46#define FRAME_NUM 8
47#define QCELP_FRAME_SIZE 36 /* 36 bytes data */
48#define FRAME_SIZE (22 * 2) /* 36 bytes data */
49 /* 36 bytes data + 24 meta field*/
50#define NT_FRAME_SIZE (QCELP_FRAME_SIZE + META_OUT_SIZE)
51#define DMASZ (NT_FRAME_SIZE * FRAME_NUM)
52#define OUT_FRAME_NUM (2)
53#define OUT_BUFFER_SIZE (4 * 1024 + META_OUT_SIZE)
54#define BUFFER_SIZE (OUT_BUFFER_SIZE * OUT_FRAME_NUM)
55
56
57#define AUDPREPROC_QCELP_EOS_FLG_OFFSET 0x0A
58#define AUDPREPROC_QCELP_EOS_FLG_MASK 0x01
59#define AUDPREPROC_QCELP_EOS_NONE 0x0 /* No EOS detected */
60#define AUDPREPROC_QCELP_EOS_SET 0x1 /* EOS set in meta field */
61
62struct buffer {
63 void *data;
64 uint32_t size;
65 uint32_t read;
66 uint32_t addr;
67 uint32_t used;
68 uint32_t mfield_sz;
69};
70
71struct audio_in {
72 struct buffer in[FRAME_NUM];
73
74 spinlock_t dsp_lock;
75
76 atomic_t in_bytes;
77 atomic_t in_samples;
78
79 struct mutex lock;
80 struct mutex read_lock;
81 wait_queue_head_t wait;
82 wait_queue_head_t wait_enable;
83 /*write section*/
84 struct buffer out[OUT_FRAME_NUM];
85
86 uint8_t out_head;
87 uint8_t out_tail;
88 uint8_t out_needed; /* number of buffers the dsp is waiting for */
89 uint32_t out_count;
90
91 struct mutex write_lock;
92 wait_queue_head_t write_wait;
93 int32_t out_phys; /* physical address of write buffer */
94 char *out_data;
95 int mfield; /* meta field embedded in data */
96 int wflush; /*write flush */
97 int rflush; /*read flush*/
98 int out_frame_cnt;
99
100 struct msm_adsp_module *audrec;
101
102 struct audrec_session_info session_info; /*audrec session info*/
103
104 /* configuration to use on next enable */
105 uint32_t buffer_size; /* Frame size (36 bytes) */
106 uint32_t samp_rate;
107 uint32_t channel_mode;
108 uint32_t enc_type;
109
110 struct msm_audio_qcelp_enc_config cfg;
111 uint32_t rec_mode;
112
113 uint32_t dsp_cnt;
114 uint32_t in_head; /* next buffer dsp will write */
115 uint32_t in_tail; /* next buffer read() will read */
116 uint32_t in_count; /* number of buffers available to read() */
117 uint32_t mode;
118 uint32_t eos_ack;
119 uint32_t flush_ack;
120
121 const char *module_name;
122 unsigned queue_ids;
123 uint16_t enc_id;
124
125 uint16_t source; /* Encoding source bit mask */
126 uint32_t device_events;
127 uint32_t in_call;
128 uint32_t dev_cnt;
129 int voice_state;
130 spinlock_t dev_lock;
131
132 /* data allocated for various buffers */
133 char *data;
134 dma_addr_t phys;
Laura Abbott61399692012-04-30 14:25:46 -0700135 void *map_v_read;
136 void *map_v_write;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700137
138 int opened;
139 int enabled;
140 int running;
141 int stopped; /* set when stopped, cleared on flush */
Kalyani polepeddy7413d2b2011-10-14 15:42:15 +0530142 char *build_id;
Sidipotu Ashok1e4d3472012-09-05 15:36:11 +0530143 struct ion_client *client;
144 struct ion_handle *input_buff_handle;
145 struct ion_handle *output_buff_handle;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700146};
147
148struct audio_frame {
149 uint16_t frame_count_lsw;
150 uint16_t frame_count_msw;
151 uint16_t frame_length;
152 uint16_t erased_pcm;
153 unsigned char raw_bitstream[]; /* samples */
154} __attribute__((packed));
155
156struct audio_frame_nt {
157 uint16_t metadata_len;
158 uint16_t frame_count_lsw;
159 uint16_t frame_count_msw;
160 uint16_t frame_length;
161 uint16_t erased_pcm;
162 uint16_t reserved;
163 uint16_t time_stamp_dword_lsw;
164 uint16_t time_stamp_dword_msw;
165 uint16_t time_stamp_lsw;
166 uint16_t time_stamp_msw;
167 uint16_t nflag_lsw;
168 uint16_t nflag_msw;
169 unsigned char raw_bitstream[]; /* samples */
170} __attribute__((packed));
171
172struct qcelp_encoded_meta_out {
173 uint16_t metadata_len;
174 uint16_t time_stamp_dword_lsw;
175 uint16_t time_stamp_dword_msw;
176 uint16_t time_stamp_lsw;
177 uint16_t time_stamp_msw;
178 uint16_t nflag_lsw;
179 uint16_t nflag_msw;
180};
181
182/* Audrec Queue command sent macro's */
183#define audrec_send_bitstreamqueue(audio, cmd, len) \
184 msm_adsp_write(audio->audrec, ((audio->queue_ids & 0xFFFF0000) >> 16),\
185 cmd, len)
186
187#define audrec_send_audrecqueue(audio, cmd, len) \
188 msm_adsp_write(audio->audrec, (audio->queue_ids & 0x0000FFFF),\
189 cmd, len)
190
191/* DSP command send functions */
192static int audqcelp_in_enc_config(struct audio_in *audio, int enable);
193static int audqcelp_in_param_config(struct audio_in *audio);
194static int audqcelp_in_mem_config(struct audio_in *audio);
195static int audqcelp_in_record_config(struct audio_in *audio, int enable);
196static int audqcelp_dsp_read_buffer(struct audio_in *audio, uint32_t read_cnt);
197
198static void audqcelp_in_get_dsp_frames(struct audio_in *audio);
199static int audpcm_config(struct audio_in *audio);
200static void audqcelp_out_flush(struct audio_in *audio);
201static int audpreproc_cmd_cfg_routing_mode(struct audio_in *audio);
202static void audpreproc_pcm_send_data(struct audio_in *audio, unsigned needed);
203static void audqcelp_nt_in_get_dsp_frames(struct audio_in *audio);
204
205static void audqcelp_in_flush(struct audio_in *audio);
206
207static void qcelp_in_listener(u32 evt_id, union auddev_evt_data *evt_payload,
208 void *private_data)
209{
210 struct audio_in *audio = (struct audio_in *) private_data;
211 unsigned long flags;
212
213 MM_DBG("evt_id = 0x%8x\n", evt_id);
214 switch (evt_id) {
215 case AUDDEV_EVT_DEV_RDY: {
216 MM_DBG("AUDDEV_EVT_DEV_RDY\n");
217 spin_lock_irqsave(&audio->dev_lock, flags);
218 audio->dev_cnt++;
219 if (!audio->in_call)
220 audio->source |= (0x1 << evt_payload->routing_id);
221 spin_unlock_irqrestore(&audio->dev_lock, flags);
222
223 if ((audio->running == 1) && (audio->enabled == 1) &&
224 (audio->mode == MSM_AUD_ENC_MODE_TUNNEL))
225 audqcelp_in_record_config(audio, 1);
226 }
227 break;
228 case AUDDEV_EVT_DEV_RLS: {
229 MM_DBG("AUDDEV_EVT_DEV_RLS\n");
230 spin_lock_irqsave(&audio->dev_lock, flags);
231 audio->dev_cnt--;
232 if (!audio->in_call)
233 audio->source &= ~(0x1 << evt_payload->routing_id);
234 spin_unlock_irqrestore(&audio->dev_lock, flags);
235
236 if ((!audio->running) || (!audio->enabled))
237 break;
238
239 if (audio->mode == MSM_AUD_ENC_MODE_TUNNEL) {
240 /* Turn of as per source */
241 if (audio->source)
242 audqcelp_in_record_config(audio, 1);
243 else
244 /* Turn off all */
245 audqcelp_in_record_config(audio, 0);
246 }
247 }
248 break;
249 case AUDDEV_EVT_VOICE_STATE_CHG: {
250 MM_DBG("AUDDEV_EVT_VOICE_STATE_CHG, state = %d\n",
251 evt_payload->voice_state);
252 audio->voice_state = evt_payload->voice_state;
253 if (audio->in_call && audio->running &&
254 (audio->mode == MSM_AUD_ENC_MODE_TUNNEL)) {
255 if (audio->voice_state == VOICE_STATE_INCALL)
256 audqcelp_in_record_config(audio, 1);
257 else if (audio->voice_state == VOICE_STATE_OFFCALL) {
258 audqcelp_in_record_config(audio, 0);
259 wake_up(&audio->wait);
260 }
261 }
262
263 break;
264 }
265 default:
266 MM_ERR("wrong event %d\n", evt_id);
267 break;
268 }
269}
270
271/* ------------------- dsp preproc event handler--------------------- */
272static void audpreproc_dsp_event(void *data, unsigned id, void *msg)
273{
274 struct audio_in *audio = data;
275
276 switch (id) {
277 case AUDPREPROC_ERROR_MSG: {
278 struct audpreproc_err_msg *err_msg = msg;
279
280 MM_ERR("ERROR_MSG: stream id %d err idx %d\n",
281 err_msg->stream_id, err_msg->aud_preproc_err_idx);
282 /* Error case */
283 wake_up(&audio->wait_enable);
284 break;
285 }
286 case AUDPREPROC_CMD_CFG_DONE_MSG: {
287 MM_DBG("CMD_CFG_DONE_MSG \n");
288 break;
289 }
290 case AUDPREPROC_CMD_ENC_CFG_DONE_MSG: {
291 struct audpreproc_cmd_enc_cfg_done_msg *enc_cfg_msg = msg;
292
293 MM_DBG("CMD_ENC_CFG_DONE_MSG: stream id %d enc type \
294 0x%8x\n", enc_cfg_msg->stream_id,
295 enc_cfg_msg->rec_enc_type);
296 /* Encoder enable success */
297 if (enc_cfg_msg->rec_enc_type & ENCODE_ENABLE) {
298 if(audio->mode == MSM_AUD_ENC_MODE_NONTUNNEL) {
299 MM_DBG("routing command\n");
300 audpreproc_cmd_cfg_routing_mode(audio);
301 } else {
302 audqcelp_in_param_config(audio);
303 }
304 } else { /* Encoder disable success */
305 audio->running = 0;
306 if (audio->mode == MSM_AUD_ENC_MODE_TUNNEL)
307 audqcelp_in_record_config(audio, 0);
308 else
309 wake_up(&audio->wait_enable);
310 }
311 break;
312 }
313 case AUDPREPROC_CMD_ENC_PARAM_CFG_DONE_MSG: {
314 MM_DBG("CMD_ENC_PARAM_CFG_DONE_MSG\n");
315 if (audio->mode == MSM_AUD_ENC_MODE_TUNNEL)
316 audqcelp_in_mem_config(audio);
317 else
318 audpcm_config(audio);
319 break;
320 }
321 case AUDPREPROC_CMD_ROUTING_MODE_DONE_MSG: {
322 struct audpreproc_cmd_routing_mode_done\
323 *routing_cfg_done_msg = msg;
324 if (routing_cfg_done_msg->configuration == 0) {
325 MM_INFO("routing configuration failed\n");
326 audio->running = 0;
327 } else
328 audqcelp_in_param_config(audio);
329 break;
330 }
331 case AUDPREPROC_AFE_CMD_AUDIO_RECORD_CFG_DONE_MSG: {
332 MM_DBG("AFE_CMD_AUDIO_RECORD_CFG_DONE_MSG \n");
333 wake_up(&audio->wait_enable);
334 break;
335 }
336 default:
337 MM_ERR("Unknown Event id %d\n", id);
338 }
339}
340
341/* ------------------- dsp audrec event handler--------------------- */
342static void audrec_dsp_event(void *data, unsigned id, size_t len,
343 void (*getevent)(void *ptr, size_t len))
344{
345 struct audio_in *audio = data;
346
347 switch (id) {
348 case AUDREC_CMD_MEM_CFG_DONE_MSG: {
349 MM_DBG("CMD_MEM_CFG_DONE MSG DONE\n");
350 audio->running = 1;
351 if (audio->mode == MSM_AUD_ENC_MODE_TUNNEL) {
352 if ((!audio->in_call && (audio->dev_cnt > 0)) ||
353 (audio->in_call &&
354 (audio->voice_state \
355 == VOICE_STATE_INCALL)))
356 audqcelp_in_record_config(audio, 1);
357 } else {
358 audpreproc_pcm_send_data(audio, 1);
359 wake_up(&audio->wait_enable);
360 }
361 break;
362 }
363 case AUDREC_FATAL_ERR_MSG: {
364 struct audrec_fatal_err_msg fatal_err_msg;
365
366 getevent(&fatal_err_msg, AUDREC_FATAL_ERR_MSG_LEN);
367 MM_ERR("FATAL_ERR_MSG: err id %d\n",
368 fatal_err_msg.audrec_err_id);
369 /* Error stop the encoder */
370 audio->stopped = 1;
371 wake_up(&audio->wait);
372 if (audio->mode == MSM_AUD_ENC_MODE_NONTUNNEL)
373 wake_up(&audio->write_wait);
374 break;
375 }
376 case AUDREC_UP_PACKET_READY_MSG: {
377 struct audrec_up_pkt_ready_msg pkt_ready_msg;
378
379 getevent(&pkt_ready_msg, AUDREC_UP_PACKET_READY_MSG_LEN);
380 MM_DBG("UP_PACKET_READY_MSG: write cnt lsw %d \
381 write cnt msw %d read cnt lsw %d read cnt msw %d \n",\
382 pkt_ready_msg.audrec_packet_write_cnt_lsw, \
383 pkt_ready_msg.audrec_packet_write_cnt_msw, \
384 pkt_ready_msg.audrec_up_prev_read_cnt_lsw, \
385 pkt_ready_msg.audrec_up_prev_read_cnt_msw);
386
387 audqcelp_in_get_dsp_frames(audio);
388 break;
389 }
390 case AUDREC_CMD_PCM_BUFFER_PTR_UPDATE_ARM_TO_ENC_MSG: {
391 MM_DBG("ptr_update recieved from DSP\n");
392 audpreproc_pcm_send_data(audio, 1);
393 break;
394 }
395 case AUDREC_CMD_PCM_CFG_ARM_TO_ENC_DONE_MSG: {
396 MM_ERR("AUDREC_CMD_PCM_CFG_ARM_TO_ENC_DONE_MSG");
397 audqcelp_in_mem_config(audio);
398 break;
399 }
400 case AUDREC_UP_NT_PACKET_READY_MSG: {
401 struct audrec_up_nt_packet_ready_msg pkt_ready_msg;
402
403 getevent(&pkt_ready_msg, AUDREC_UP_NT_PACKET_READY_MSG_LEN);
404 MM_DBG("UP_NT_PACKET_READY_MSG: write cnt lsw %d \
405 write cnt msw %d read cnt lsw %d read cnt msw %d \n",\
406 pkt_ready_msg.audrec_packetwrite_cnt_lsw, \
407 pkt_ready_msg.audrec_packetwrite_cnt_msw, \
408 pkt_ready_msg.audrec_upprev_readcount_lsw, \
409 pkt_ready_msg.audrec_upprev_readcount_msw);
410
411 audqcelp_nt_in_get_dsp_frames(audio);
412 break;
413 }
414 case AUDREC_CMD_EOS_ACK_MSG: {
415 MM_DBG("eos ack recieved\n");
416 break;
417 }
418 case AUDREC_CMD_FLUSH_DONE_MSG: {
419 audio->wflush = 0;
420 audio->rflush = 0;
421 audio->flush_ack = 1;
422 wake_up(&audio->write_wait);
423 MM_DBG("flush ack recieved\n");
424 break;
425 }
426 case ADSP_MESSAGE_ID: {
427 MM_DBG("Received ADSP event:module audrectask\n");
428 break;
429 }
430 default:
431 MM_ERR("Unknown Event id %d\n", id);
432 }
433}
434
435static void audqcelp_in_get_dsp_frames(struct audio_in *audio)
436{
437 struct audio_frame *frame;
438 uint32_t index;
439 unsigned long flags;
440
441 MM_DBG("head = %d\n", audio->in_head);
442 index = audio->in_head;
443
444 frame = (void *) (((char *)audio->in[index].data) - \
445 sizeof(*frame));
446
447 spin_lock_irqsave(&audio->dsp_lock, flags);
448 audio->in[index].size = frame->frame_length;
449
450 /* statistics of read */
451 atomic_add(audio->in[index].size, &audio->in_bytes);
452 atomic_add(1, &audio->in_samples);
453
454 audio->in_head = (audio->in_head + 1) & (FRAME_NUM - 1);
455
456 /* If overflow, move the tail index foward. */
457 if (audio->in_head == audio->in_tail) {
458 MM_ERR("Error! not able to keep up the read\n");
459 audio->in_tail = (audio->in_tail + 1) & (FRAME_NUM - 1);
460 MM_ERR("in_count = %d\n", audio->in_count);
461 } else
462 audio->in_count++;
463
464 audqcelp_dsp_read_buffer(audio, audio->dsp_cnt++);
465 spin_unlock_irqrestore(&audio->dsp_lock, flags);
466
467 wake_up(&audio->wait);
468}
469
470static void audqcelp_nt_in_get_dsp_frames(struct audio_in *audio)
471{
472 struct audio_frame_nt *nt_frame;
473 uint32_t index;
474 unsigned long flags;
475 MM_DBG("head = %d\n", audio->in_head);
476 index = audio->in_head;
477 nt_frame = (void *) (((char *)audio->in[index].data) - \
478 sizeof(struct audio_frame_nt));
479 spin_lock_irqsave(&audio->dsp_lock, flags);
480 audio->in[index].size = nt_frame->frame_length;
481 /* statistics of read */
482 atomic_add(audio->in[index].size, &audio->in_bytes);
483 atomic_add(1, &audio->in_samples);
484
485 audio->in_head = (audio->in_head + 1) & (FRAME_NUM - 1);
486
487 /* If overflow, move the tail index foward. */
488 if (audio->in_head == audio->in_tail)
489 MM_DBG("Error! not able to keep up the read\n");
490 else
491 audio->in_count++;
492
493 spin_unlock_irqrestore(&audio->dsp_lock, flags);
494 wake_up(&audio->wait);
495}
496
497
498struct msm_adsp_ops audrec_qcelp_adsp_ops = {
499 .event = audrec_dsp_event,
500};
501
502static int audpreproc_pcm_buffer_ptr_refresh(struct audio_in *audio,
503 unsigned idx, unsigned len)
504{
505 struct audrec_cmd_pcm_buffer_ptr_refresh_arm_enc cmd;
506
507 if (len == META_OUT_SIZE)
508 len = len / 2;
509 else
510 len = (len + META_OUT_SIZE) / 2;
511 MM_DBG("len = %d\n", len);
512 memset(&cmd, 0, sizeof(cmd));
513 cmd.cmd_id = AUDREC_CMD_PCM_BUFFER_PTR_REFRESH_ARM_TO_ENC;
514 cmd.num_buffers = 1;
515 if (cmd.num_buffers == 1) {
516 cmd.buf_address_length[0] = (audio->out[idx].addr &
517 0xffff0000) >> 16;
518 cmd.buf_address_length[1] = (audio->out[idx].addr &
519 0x0000ffff);
520 cmd.buf_address_length[2] = (len & 0xffff0000) >> 16;
521 cmd.buf_address_length[3] = (len & 0x0000ffff);
522 }
523 audio->out_frame_cnt++;
524 return audrec_send_audrecqueue(audio, (void *)&cmd,
525 (unsigned int)sizeof(cmd));
526}
527
528
529static int audpcm_config(struct audio_in *audio)
530{
531 struct audrec_cmd_pcm_cfg_arm_to_enc cmd;
532 MM_DBG("\n");
533 memset(&cmd, 0, sizeof(cmd));
534 cmd.cmd_id = AUDREC_CMD_PCM_CFG_ARM_TO_ENC;
535 cmd.config_update_flag = AUDREC_PCM_CONFIG_UPDATE_FLAG_ENABLE;
536 cmd.enable_flag = AUDREC_ENABLE_FLAG_VALUE;
537 cmd.sampling_freq = audio->samp_rate;
538 if (!audio->channel_mode)
539 cmd.channels = 1;
540 else
541 cmd.channels = 2;
542 cmd.frequency_of_intimation = 1;
543 cmd.max_number_of_buffers = OUT_FRAME_NUM;
544 return audrec_send_audrecqueue(audio, (void *)&cmd,
545 (unsigned int)sizeof(cmd));
546}
547
548
549static int audpreproc_cmd_cfg_routing_mode(struct audio_in *audio)
550{
551 struct audpreproc_audrec_cmd_routing_mode cmd;
552
553 MM_DBG("\n");
554 memset(&cmd, 0, sizeof(cmd));
555 cmd.cmd_id = AUDPREPROC_AUDREC_CMD_ROUTING_MODE;
556 cmd.stream_id = audio->enc_id;
557 if (audio->mode == MSM_ADSP_ENC_MODE_NON_TUNNEL)
558 cmd.routing_mode = 1;
559 return audpreproc_send_audreccmdqueue(&cmd, sizeof(cmd));
560}
561
562
563
564static int audqcelp_in_enc_config(struct audio_in *audio, int enable)
565{
566 struct audpreproc_audrec_cmd_enc_cfg cmd;
567
568 memset(&cmd, 0, sizeof(cmd));
Kalyani polepeddy7413d2b2011-10-14 15:42:15 +0530569 if (audio->build_id[17] == '1') {
570 cmd.cmd_id = AUDPREPROC_AUDREC_CMD_ENC_CFG_2;
571 MM_ERR("sending AUDPREPROC_AUDREC_CMD_ENC_CFG_2 command");
572 } else {
573 cmd.cmd_id = AUDPREPROC_AUDREC_CMD_ENC_CFG;
574 MM_ERR("sending AUDPREPROC_AUDREC_CMD_ENC_CFG command");
575 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700576 cmd.stream_id = audio->enc_id;
577
578 if (enable)
579 cmd.audrec_enc_type = audio->enc_type | ENCODE_ENABLE;
580 else
581 cmd.audrec_enc_type &= ~(ENCODE_ENABLE);
582
583 return audpreproc_send_audreccmdqueue(&cmd, sizeof(cmd));
584}
585
586static int audqcelp_in_param_config(struct audio_in *audio)
587{
588 struct audpreproc_audrec_cmd_parm_cfg_qcelp13k cmd;
589
590 memset(&cmd, 0, sizeof(cmd));
591 cmd.common.cmd_id = AUDPREPROC_AUDREC_CMD_PARAM_CFG;
592 cmd.common.stream_id = audio->enc_id;
593
594 cmd.enc_min_rate = audio->cfg.min_bit_rate;
595 cmd.enc_max_rate = audio->cfg.max_bit_rate;
596 cmd.rate_modulation_cmd = 0; /* Default set to 0 */
597 cmd.reduced_rate_level = 0; /* Default set to 0 */
598
599 return audpreproc_send_audreccmdqueue(&cmd, sizeof(cmd));
600}
601
602/* To Do: msm_snddev_route_enc(audio->enc_id); */
603static int audqcelp_in_record_config(struct audio_in *audio, int enable)
604{
605 struct audpreproc_afe_cmd_audio_record_cfg cmd;
606 memset(&cmd, 0, sizeof(cmd));
607 cmd.cmd_id = AUDPREPROC_AFE_CMD_AUDIO_RECORD_CFG;
608 cmd.stream_id = audio->enc_id;
609 if (enable)
610 cmd.destination_activity = AUDIO_RECORDING_TURN_ON;
611 else
612 cmd.destination_activity = AUDIO_RECORDING_TURN_OFF;
613
614 cmd.source_mix_mask = audio->source;
615 if (audio->enc_id == 2) {
616 if ((cmd.source_mix_mask &
617 INTERNAL_CODEC_TX_SOURCE_MIX_MASK) ||
618 (cmd.source_mix_mask & AUX_CODEC_TX_SOURCE_MIX_MASK) ||
619 (cmd.source_mix_mask & VOICE_UL_SOURCE_MIX_MASK) ||
620 (cmd.source_mix_mask & VOICE_DL_SOURCE_MIX_MASK)) {
621 cmd.pipe_id = SOURCE_PIPE_1;
622 }
623 if (cmd.source_mix_mask &
624 AUDPP_A2DP_PIPE_SOURCE_MIX_MASK)
625 cmd.pipe_id |= SOURCE_PIPE_0;
626 }
627 MM_DBG("stream_id %x destination_activity %x \
628 source_mix_mask %x pipe_id %x",\
629 cmd.stream_id, cmd.destination_activity,
630 cmd.source_mix_mask, cmd.pipe_id);
631 return audpreproc_send_audreccmdqueue(&cmd, sizeof(cmd));
632}
633
634static int audqcelp_in_mem_config(struct audio_in *audio)
635{
636 struct audrec_cmd_arecmem_cfg cmd;
637 uint16_t *data = (void *) audio->data;
638 int n;
639
640 memset(&cmd, 0, sizeof(cmd));
641 cmd.cmd_id = AUDREC_CMD_MEM_CFG_CMD;
642 cmd.audrec_up_pkt_intm_count = 1;
643 cmd.audrec_ext_pkt_start_addr_msw = audio->phys >> 16;
644 cmd.audrec_ext_pkt_start_addr_lsw = audio->phys;
645 cmd.audrec_ext_pkt_buf_number = FRAME_NUM;
646 MM_DBG("audio->phys = %x\n", audio->phys);
647 /* prepare buffer pointers:
648 * T:36 bytes qcelp ppacket + 4 halfword header
649 * NT:36 bytes qcelp packet + 12 halfword header
650 */
651 for (n = 0; n < FRAME_NUM; n++) {
652 if (audio->mode == MSM_AUD_ENC_MODE_TUNNEL) {
653 audio->in[n].data = data + 4;
654 data += (FRAME_SIZE/2);
655 MM_DBG("0x%8x\n", (int)(audio->in[n].data - 8));
656 } else {
657 audio->in[n].data = data + 12;
658 data += ((QCELP_FRAME_SIZE) / 2) + 12;
659 MM_DBG("0x%8x\n", (int)(audio->in[n].data - 24));
660 }
661 }
662 return audrec_send_audrecqueue(audio, &cmd, sizeof(cmd));
663}
664
665static int audqcelp_dsp_read_buffer(struct audio_in *audio, uint32_t read_cnt)
666{
667 struct up_audrec_packet_ext_ptr cmd;
668
669 memset(&cmd, 0, sizeof(cmd));
670 cmd.cmd_id = UP_AUDREC_PACKET_EXT_PTR;
671 cmd.audrec_up_curr_read_count_msw = read_cnt >> 16;
672 cmd.audrec_up_curr_read_count_lsw = read_cnt;
673
674 return audrec_send_bitstreamqueue(audio, &cmd, sizeof(cmd));
675}
676static int audqcelp_flush_command(struct audio_in *audio)
677{
678 struct audrec_cmd_flush cmd;
679 MM_DBG("\n");
680 memset(&cmd, 0, sizeof(cmd));
681 cmd.cmd_id = AUDREC_CMD_FLUSH;
682 return audrec_send_audrecqueue(audio, &cmd, sizeof(cmd));
683}
684
685/* must be called with audio->lock held */
686static int audqcelp_in_enable(struct audio_in *audio)
687{
688 if (audio->enabled)
689 return 0;
690
691 if (audpreproc_enable(audio->enc_id, &audpreproc_dsp_event, audio)) {
692 MM_ERR("msm_adsp_enable(audpreproc) failed\n");
693 return -ENODEV;
694 }
695
696 if (msm_adsp_enable(audio->audrec)) {
697 MM_ERR("msm_adsp_enable(audrec) failed\n");
698 audpreproc_disable(audio->enc_id, audio);
699 return -ENODEV;
700 }
701 audio->enabled = 1;
702 audqcelp_in_enc_config(audio, 1);
703
704 return 0;
705}
706
707/* must be called with audio->lock held */
708static int audqcelp_in_disable(struct audio_in *audio)
709{
710 if (audio->enabled) {
711 audio->enabled = 0;
712 audqcelp_in_enc_config(audio, 0);
713 wake_up(&audio->wait);
714 wait_event_interruptible_timeout(audio->wait_enable,
715 audio->running == 0, 1*HZ);
716 msm_adsp_disable(audio->audrec);
717 audpreproc_disable(audio->enc_id, audio);
718 }
719 return 0;
720}
721
722static void audqcelp_ioport_reset(struct audio_in *audio)
723{
724 /* Make sure read/write thread are free from
725 * sleep and knowing that system is not able
726 * to process io request at the moment
727 */
728 wake_up(&audio->write_wait);
729 mutex_lock(&audio->write_lock);
730 audqcelp_in_flush(audio);
731 mutex_unlock(&audio->write_lock);
732 wake_up(&audio->wait);
733 mutex_lock(&audio->read_lock);
734 audqcelp_out_flush(audio);
735 mutex_unlock(&audio->read_lock);
736}
737
738static void audqcelp_in_flush(struct audio_in *audio)
739{
740 int i;
741
742 audio->dsp_cnt = 0;
743 audio->in_head = 0;
744 audio->in_tail = 0;
745 audio->in_count = 0;
746 audio->eos_ack = 0;
747 for (i = 0; i < FRAME_NUM; i++) {
748 audio->in[i].size = 0;
749 audio->in[i].read = 0;
750 }
751 MM_DBG("in_bytes %d\n", atomic_read(&audio->in_bytes));
752 MM_DBG("in_samples %d\n", atomic_read(&audio->in_samples));
753 atomic_set(&audio->in_bytes, 0);
754 atomic_set(&audio->in_samples, 0);
755}
756
757static void audqcelp_out_flush(struct audio_in *audio)
758{
759 int i;
760
761 audio->out_head = 0;
762 audio->out_tail = 0;
763 audio->out_count = 0;
764 for (i = 0; i < OUT_FRAME_NUM; i++) {
765 audio->out[i].size = 0;
766 audio->out[i].read = 0;
767 audio->out[i].used = 0;
768 }
769}
770
771/* ------------------- device --------------------- */
772static long audqcelp_in_ioctl(struct file *file,
773 unsigned int cmd, unsigned long arg)
774{
775 struct audio_in *audio = file->private_data;
776 int rc = 0;
777
778 MM_DBG("\n");
779 if (cmd == AUDIO_GET_STATS) {
780 struct msm_audio_stats stats;
Asish Bhattacharya4a064192013-10-01 17:15:05 +0530781 memset(&stats, 0, sizeof(stats));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700782 stats.byte_count = atomic_read(&audio->in_bytes);
783 stats.sample_count = atomic_read(&audio->in_samples);
784 if (copy_to_user((void *) arg, &stats, sizeof(stats)))
785 return -EFAULT;
786 return rc;
787 }
788
789 mutex_lock(&audio->lock);
790 switch (cmd) {
791 case AUDIO_START: {
792 uint32_t freq;
793 freq = 48000;
794 MM_DBG("AUDIO_START\n");
795 if (audio->in_call && (audio->voice_state !=
796 VOICE_STATE_INCALL)) {
797 rc = -EPERM;
798 break;
799 }
800 rc = msm_snddev_request_freq(&freq, audio->enc_id,
801 SNDDEV_CAP_TX, AUDDEV_CLNT_ENC);
802 MM_DBG("sample rate configured %d\n", freq);
803 if (rc < 0) {
804 MM_DBG(" Sample rate can not be set, return code %d\n",
805 rc);
806 msm_snddev_withdraw_freq(audio->enc_id,
807 SNDDEV_CAP_TX, AUDDEV_CLNT_ENC);
808 MM_DBG("msm_snddev_withdraw_freq\n");
809 break;
810 }
811 /*update aurec session info in audpreproc layer*/
812 audio->session_info.session_id = audio->enc_id;
813 audio->session_info.sampling_freq = audio->samp_rate;
814 audpreproc_update_audrec_info(&audio->session_info);
815 rc = audqcelp_in_enable(audio);
816 if (!rc) {
817 rc =
818 wait_event_interruptible_timeout(audio->wait_enable,
819 audio->running != 0, 1*HZ);
820 MM_DBG("state %d rc = %d\n", audio->running, rc);
821
822 if (audio->running == 0)
823 rc = -ENODEV;
824 else
825 rc = 0;
826 }
827 audio->stopped = 0;
828 break;
829 }
830 case AUDIO_STOP: {
831 /*reset the sampling frequency information at audpreproc layer*/
832 audio->session_info.sampling_freq = 0;
833 audpreproc_update_audrec_info(&audio->session_info);
834 rc = audqcelp_in_disable(audio);
835 rc = msm_snddev_withdraw_freq(audio->enc_id,
836 SNDDEV_CAP_TX, AUDDEV_CLNT_ENC);
837 MM_DBG("msm_snddev_withdraw_freq\n");
838 audio->stopped = 1;
839 break;
840 }
841 case AUDIO_FLUSH: {
842 MM_DBG("AUDIO_FLUSH\n");
843 audio->rflush = 1;
844 audio->wflush = 1;
845 audqcelp_ioport_reset(audio);
846 if (audio->running) {
847 audqcelp_flush_command(audio);
848 rc = wait_event_interruptible(audio->write_wait,
849 !audio->wflush);
850 if (rc < 0) {
851 MM_ERR("AUDIO_FLUSH interrupted\n");
852 rc = -EINTR;
853 }
854 } else {
855 audio->rflush = 0;
856 audio->wflush = 0;
857 }
858 break;
859 }
860 case AUDIO_SET_STREAM_CONFIG: {
861 struct msm_audio_stream_config cfg;
862 if (copy_from_user(&cfg, (void *) arg, sizeof(cfg))) {
863 rc = -EFAULT;
864 break;
865 }
866 /* Allow only single frame */
867 if (audio->mode == MSM_AUD_ENC_MODE_TUNNEL) {
868 if (cfg.buffer_size != (FRAME_SIZE - 8)) {
869 rc = -EINVAL;
870 break;
871 }
872 } else {
873 if (cfg.buffer_size != (QCELP_FRAME_SIZE + 14)) {
874 rc = -EINVAL;
875 break;
876 }
877 }
878 audio->buffer_size = cfg.buffer_size;
879 break;
880 }
881 case AUDIO_GET_STREAM_CONFIG: {
882 struct msm_audio_stream_config cfg;
883 memset(&cfg, 0, sizeof(cfg));
884 cfg.buffer_size = audio->buffer_size;
885 cfg.buffer_count = FRAME_NUM;
886 if (copy_to_user((void *) arg, &cfg, sizeof(cfg)))
887 rc = -EFAULT;
888 break;
889 }
890 case AUDIO_GET_QCELP_ENC_CONFIG: {
891 if (copy_to_user((void *) arg, &audio->cfg, sizeof(audio->cfg)))
892 rc = -EFAULT;
893 break;
894 }
895 case AUDIO_SET_QCELP_ENC_CONFIG: {
896 struct msm_audio_qcelp_enc_config cfg;
897 if (copy_from_user(&cfg, (void *) arg, sizeof(cfg))) {
898 rc = -EFAULT;
899 break;
900 }
901 MM_DBG("0X%8x, 0x%8x, 0x%8x\n", cfg.min_bit_rate, \
902 cfg.max_bit_rate, cfg.cdma_rate);
903 if (cfg.min_bit_rate > CDMA_RATE_FULL || \
904 cfg.min_bit_rate < CDMA_RATE_EIGHTH) {
905 MM_ERR("invalid min bitrate\n");
906 rc = -EFAULT;
907 break;
908 }
909 if (cfg.max_bit_rate > CDMA_RATE_FULL || \
910 cfg.max_bit_rate < CDMA_RATE_EIGHTH) {
911 MM_ERR("invalid max bitrate\n");
912 rc = -EFAULT;
913 break;
914 }
915 /* Recording Does not support Erase and Blank */
916 if (cfg.cdma_rate > CDMA_RATE_FULL ||
917 cfg.cdma_rate < CDMA_RATE_EIGHTH) {
918 MM_ERR("invalid qcelp cdma rate\n");
919 rc = -EFAULT;
920 break;
921 }
922 memcpy(&audio->cfg, &cfg, sizeof(cfg));
923 break;
924 }
925 case AUDIO_GET_CONFIG: {
926 struct msm_audio_config cfg;
927 memset(&cfg, 0, sizeof(cfg));
928 cfg.buffer_size = OUT_BUFFER_SIZE;
929 cfg.buffer_count = OUT_FRAME_NUM;
930 cfg.sample_rate = audio->samp_rate;
931 cfg.channel_count = audio->channel_mode;
932 if (copy_to_user((void *)arg, &cfg, sizeof(cfg)))
933 rc = -EFAULT;
934 break;
935 }
936 case AUDIO_SET_INCALL: {
937 struct msm_voicerec_mode cfg;
938 unsigned long flags;
939 if (audio->mode == MSM_AUD_ENC_MODE_TUNNEL) {
940 if (copy_from_user(&cfg, (void *) arg, sizeof(cfg))) {
941 rc = -EFAULT;
942 break;
943 }
944 if (cfg.rec_mode != VOC_REC_BOTH &&
945 cfg.rec_mode != VOC_REC_UPLINK &&
946 cfg.rec_mode != VOC_REC_DOWNLINK) {
947 MM_ERR("invalid rec_mode\n");
948 rc = -EINVAL;
949 break;
950 } else {
951 spin_lock_irqsave(&audio->dev_lock, flags);
952 if (cfg.rec_mode == VOC_REC_UPLINK)
953 audio->source = \
954 VOICE_UL_SOURCE_MIX_MASK;
955 else if (cfg.rec_mode == VOC_REC_DOWNLINK)
956 audio->source = \
957 VOICE_DL_SOURCE_MIX_MASK;
958 else
959 audio->source = \
960 VOICE_DL_SOURCE_MIX_MASK |
961 VOICE_UL_SOURCE_MIX_MASK ;
962 audio->in_call = 1;
963 spin_unlock_irqrestore(&audio->dev_lock, flags);
964 }
965 }
966 break;
967 }
968 case AUDIO_GET_SESSION_ID: {
969 if (copy_to_user((void *) arg, &audio->enc_id,
970 sizeof(unsigned short))) {
971 rc = -EFAULT;
972 }
973 break;
974 }
975 default:
976 rc = -EINVAL;
977 }
978 mutex_unlock(&audio->lock);
979 return rc;
980}
981
982static ssize_t audqcelp_in_read(struct file *file,
983 char __user *buf,
984 size_t count, loff_t *pos)
985{
986 struct audio_in *audio = file->private_data;
987 unsigned long flags;
988 const char __user *start = buf;
989 void *data;
990 uint32_t index;
991 uint32_t size;
992 int rc = 0;
993 struct qcelp_encoded_meta_out meta_field;
994 struct audio_frame_nt *nt_frame;
995 MM_DBG(" count = %d\n", count);
996 mutex_lock(&audio->read_lock);
997 while (count > 0) {
998 rc = wait_event_interruptible(
999 audio->wait, (audio->in_count > 0) || audio->stopped ||
1000 audio->rflush ||
1001 ((audio->mode == MSM_AUD_ENC_MODE_TUNNEL) &&
1002 audio->in_call && audio->running &&
1003 (audio->voice_state == VOICE_STATE_OFFCALL)));
1004 if (rc < 0)
1005 break;
1006
1007 if (audio->rflush) {
1008 rc = -EBUSY;
1009 break;
1010 }
1011 if (audio->stopped && !audio->in_count) {
1012 MM_DBG("Driver in stop state, No more buffer to read");
1013 rc = 0;/* End of File */
1014 break;
1015 } else if ((audio->mode == MSM_AUD_ENC_MODE_TUNNEL) &&
1016 audio->in_call && audio->running &&
1017 (audio->voice_state \
1018 == VOICE_STATE_OFFCALL)) {
1019 MM_DBG("Not Permitted Voice Terminated\n");
1020 rc = -EPERM; /* Voice Call stopped */
1021 break;
1022 }
1023
1024 index = audio->in_tail;
1025 data = (uint8_t *) audio->in[index].data;
1026 size = audio->in[index].size;
1027
1028 if (audio->mode == MSM_AUD_ENC_MODE_NONTUNNEL) {
1029 nt_frame = (struct audio_frame_nt *)(data -
1030 sizeof(struct audio_frame_nt));
1031 memcpy((char *)&meta_field.time_stamp_dword_lsw,
1032 (char *)&nt_frame->time_stamp_dword_lsw,
1033 (sizeof(struct qcelp_encoded_meta_out) - \
1034 sizeof(uint16_t)));
1035 meta_field.metadata_len =
1036 sizeof(struct qcelp_encoded_meta_out);
1037 if (copy_to_user((char *)start,
1038 (char *)&meta_field,
1039 sizeof(struct qcelp_encoded_meta_out))) {
1040 rc = -EFAULT;
1041 break;
1042 }
1043 if (nt_frame->nflag_lsw & 0x0001) {
1044 MM_ERR("recieved EOS in read call\n");
1045 audio->eos_ack = 1;
1046 }
1047 buf += sizeof(struct qcelp_encoded_meta_out);
1048 count -= sizeof(struct qcelp_encoded_meta_out);
1049 }
1050 if (count >= size) {
1051 if (copy_to_user(buf, data, size)) {
1052 rc = -EFAULT;
1053 break;
1054 }
1055 spin_lock_irqsave(&audio->dsp_lock, flags);
1056 if (index != audio->in_tail) {
1057 /* overrun -- data is
1058 * invalid and we need to retry */
1059 spin_unlock_irqrestore(&audio->dsp_lock, flags);
1060 continue;
1061 }
1062 audio->in[index].size = 0;
1063 audio->in_tail = (audio->in_tail + 1) & (FRAME_NUM - 1);
1064 audio->in_count--;
1065 spin_unlock_irqrestore(&audio->dsp_lock, flags);
1066 count -= size;
1067 buf += size;
1068 if ((audio->mode == MSM_AUD_ENC_MODE_NONTUNNEL)) {
1069 if (!audio->eos_ack) {
1070 MM_DBG("sending read ptr command\
1071 %d %d\n",
1072 audio->dsp_cnt,
1073 audio->in_tail);
1074 audqcelp_dsp_read_buffer(audio,
1075 audio->dsp_cnt++);
1076 }
1077 }
1078 } else {
1079 MM_ERR("short read\n");
1080 break;
1081 }
1082 break;
1083 }
1084 mutex_unlock(&audio->read_lock);
1085
1086 if (buf > start)
1087 return buf - start;
1088
1089 return rc;
1090}
1091
1092static void audpreproc_pcm_send_data(struct audio_in *audio, unsigned needed)
1093{
1094 struct buffer *frame;
1095 unsigned long flags;
1096 MM_DBG("\n");
1097 spin_lock_irqsave(&audio->dsp_lock, flags);
1098 if (!audio->running)
1099 goto done;
1100
1101 if (needed && !audio->wflush) {
1102 /* We were called from the callback because the DSP
1103 * requested more data. Note that the DSP does want
1104 * more data, and if a buffer was in-flight, mark it
1105 * as available (since the DSP must now be done with
1106 * it).
1107 */
1108 audio->out_needed = 1;
1109 frame = audio->out + audio->out_tail;
1110 if (frame->used == 0xffffffff) {
1111 MM_DBG("frame %d free\n", audio->out_tail);
1112 frame->used = 0;
1113 audio->out_tail ^= 1;
1114 wake_up(&audio->write_wait);
1115 }
1116 }
1117
1118 if (audio->out_needed) {
1119 /* If the DSP currently wants data and we have a
1120 * buffer available, we will send it and reset
1121 * the needed flag. We'll mark the buffer as in-flight
1122 * so that it won't be recycled until the next buffer
1123 * is requested
1124 */
1125
1126 frame = audio->out + audio->out_tail;
1127 if (frame->used) {
1128 BUG_ON(frame->used == 0xffffffff);
1129 audpreproc_pcm_buffer_ptr_refresh(audio,
1130 audio->out_tail,
1131 frame->used);
1132 frame->used = 0xffffffff;
1133 audio->out_needed = 0;
1134 }
1135 }
1136 done:
1137 spin_unlock_irqrestore(&audio->dsp_lock, flags);
1138}
1139
1140
Steve Mucklef132c6c2012-06-06 18:30:57 -07001141static int audqcelp_in_fsync(struct file *file, loff_t ppos1, loff_t ppos2, int datasync)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001142
1143{
1144 struct audio_in *audio = file->private_data;
1145 int rc = 0;
1146
1147 MM_DBG("\n"); /* Macro prints the file name and function */
1148 if (!audio->running || (audio->mode == MSM_AUD_ENC_MODE_TUNNEL)) {
1149 rc = -EINVAL;
1150 goto done_nolock;
1151 }
1152
1153 mutex_lock(&audio->write_lock);
1154
1155 rc = wait_event_interruptible(audio->write_wait,
1156 audio->wflush);
1157 MM_DBG("waked on by some event audio->wflush = %d\n", audio->wflush);
1158
1159 if (rc < 0)
1160 goto done;
1161 else if (audio->wflush) {
1162 rc = -EBUSY;
1163 goto done;
1164 }
1165done:
1166 mutex_unlock(&audio->write_lock);
1167done_nolock:
1168 return rc;
1169
1170}
1171
1172 int audpreproc_qcelp_process_eos(struct audio_in *audio,
1173 const char __user *buf_start, unsigned short mfield_size)
1174{
1175 struct buffer *frame;
1176 int rc = 0;
1177
1178 frame = audio->out + audio->out_head;
1179
1180 rc = wait_event_interruptible(audio->write_wait,
1181 (audio->out_needed &&
1182 audio->out[0].used == 0 &&
1183 audio->out[1].used == 0)
1184 || (audio->stopped)
1185 || (audio->wflush));
1186
1187 if (rc < 0)
1188 goto done;
1189 if (audio->stopped || audio->wflush) {
1190 rc = -EBUSY;
1191 goto done;
1192 }
1193 if (copy_from_user(frame->data, buf_start, mfield_size)) {
1194 rc = -EFAULT;
1195 goto done;
1196 }
1197
1198 frame->mfield_sz = mfield_size;
1199 audio->out_head ^= 1;
1200 frame->used = mfield_size;
1201 MM_DBG("copying meta_out frame->used = %d\n", frame->used);
1202 audpreproc_pcm_send_data(audio, 0);
1203done:
1204 return rc;
1205}
1206
1207static ssize_t audqcelp_in_write(struct file *file,
1208 const char __user *buf,
1209 size_t count, loff_t *pos)
1210{
1211 struct audio_in *audio = file->private_data;
1212 const char __user *start = buf;
1213 struct buffer *frame;
1214 char *cpy_ptr;
1215 int rc = 0, eos_condition = AUDPREPROC_QCELP_EOS_NONE;
1216 unsigned short mfield_size = 0;
1217 int write_count = 0;
1218
1219 MM_DBG("cnt=%d\n", count);
1220 if (count & 1)
1221 return -EINVAL;
1222
1223 if (audio->mode != MSM_AUD_ENC_MODE_NONTUNNEL)
1224 return -EINVAL;
1225
1226 mutex_lock(&audio->write_lock);
1227 frame = audio->out + audio->out_head;
1228 /* if supplied count is more than driver buffer size
1229 * then only copy driver buffer size
1230 */
1231 if (count > frame->size)
1232 count = frame->size;
1233
1234 write_count = count;
1235 cpy_ptr = frame->data;
1236 rc = wait_event_interruptible(audio->write_wait,
1237 (frame->used == 0)
1238 || (audio->stopped)
1239 || (audio->wflush));
1240 if (rc < 0)
1241 goto error;
1242
1243 if (audio->stopped || audio->wflush) {
1244 rc = -EBUSY;
1245 goto error;
1246 }
1247 if (audio->mfield) {
1248 if (buf == start) {
1249 /* Processing beginning of user buffer */
1250 if (__get_user(mfield_size,
1251 (unsigned short __user *) buf)) {
1252 rc = -EFAULT;
1253 goto error;
1254 } else if (mfield_size > count) {
1255 rc = -EINVAL;
1256 goto error;
1257 }
1258 MM_DBG("mf offset_val %x\n", mfield_size);
1259 if (copy_from_user(cpy_ptr, buf, mfield_size)) {
1260 rc = -EFAULT;
1261 goto error;
1262 }
1263 /* Check if EOS flag is set and buffer has
1264 * contains just meta field
1265 */
1266 if (cpy_ptr[AUDPREPROC_QCELP_EOS_FLG_OFFSET] &
1267 AUDPREPROC_QCELP_EOS_FLG_MASK) {
1268 eos_condition = AUDPREPROC_QCELP_EOS_SET;
1269 MM_DBG("EOS SET\n");
1270 if (mfield_size == count) {
1271 buf += mfield_size;
1272 eos_condition = 0;
1273 goto exit;
1274 } else
1275 cpy_ptr[AUDPREPROC_QCELP_EOS_FLG_OFFSET] &=
1276 ~AUDPREPROC_QCELP_EOS_FLG_MASK;
1277 }
1278 cpy_ptr += mfield_size;
1279 count -= mfield_size;
1280 buf += mfield_size;
1281 } else {
1282 mfield_size = 0;
1283 MM_DBG("continuous buffer\n");
1284 }
1285 frame->mfield_sz = mfield_size;
1286 }
1287 MM_DBG("copying the stream count = %d\n", count);
1288 if (copy_from_user(cpy_ptr, buf, count)) {
1289 rc = -EFAULT;
1290 goto error;
1291 }
1292exit:
1293 frame->used = count;
1294 audio->out_head ^= 1;
1295 if (!audio->flush_ack)
1296 audpreproc_pcm_send_data(audio, 0);
1297 else {
1298 audpreproc_pcm_send_data(audio, 1);
1299 audio->flush_ack = 0;
1300 }
1301 if (eos_condition == AUDPREPROC_QCELP_EOS_SET)
1302 rc = audpreproc_qcelp_process_eos(audio, start, mfield_size);
1303 mutex_unlock(&audio->write_lock);
1304 return write_count;
1305error:
1306 mutex_unlock(&audio->write_lock);
1307 return rc;
1308}
1309
1310static int audqcelp_in_release(struct inode *inode, struct file *file)
1311{
1312 struct audio_in *audio = file->private_data;
1313
1314 mutex_lock(&audio->lock);
1315 audio->in_call = 0;
1316 /* with draw frequency for session
1317 incase not stopped the driver */
1318 msm_snddev_withdraw_freq(audio->enc_id, SNDDEV_CAP_TX,
1319 AUDDEV_CLNT_ENC);
1320 auddev_unregister_evt_listner(AUDDEV_CLNT_ENC, audio->enc_id);
1321 /*reset the sampling frequency information at audpreproc layer*/
1322 audio->session_info.sampling_freq = 0;
1323 audpreproc_update_audrec_info(&audio->session_info);
1324 audqcelp_in_disable(audio);
1325 audqcelp_in_flush(audio);
1326 msm_adsp_put(audio->audrec);
1327 audpreproc_aenc_free(audio->enc_id);
1328 audio->audrec = NULL;
1329 audio->opened = 0;
1330 if (audio->data) {
Sidipotu Ashok1e4d3472012-09-05 15:36:11 +05301331 ion_unmap_kernel(audio->client, audio->input_buff_handle);
1332 ion_free(audio->client, audio->input_buff_handle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001333 audio->data = NULL;
1334 }
1335 if (audio->out_data) {
Sidipotu Ashok1e4d3472012-09-05 15:36:11 +05301336 ion_unmap_kernel(audio->client, audio->output_buff_handle);
1337 ion_free(audio->client, audio->output_buff_handle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001338 audio->out_data = NULL;
1339 }
Sidipotu Ashok1e4d3472012-09-05 15:36:11 +05301340 ion_client_destroy(audio->client);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001341 mutex_unlock(&audio->lock);
1342 return 0;
1343}
1344
1345struct audio_in the_audio_qcelp_in;
1346static int audqcelp_in_open(struct inode *inode, struct file *file)
1347{
1348 struct audio_in *audio = &the_audio_qcelp_in;
1349 int rc;
1350 int encid;
Sidipotu Ashok1e4d3472012-09-05 15:36:11 +05301351 int len = 0;
1352 unsigned long ionflag = 0;
1353 ion_phys_addr_t addr = 0;
1354 struct ion_handle *handle = NULL;
1355 struct ion_client *client = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001356
1357 mutex_lock(&audio->lock);
1358 if (audio->opened) {
1359 rc = -EBUSY;
1360 goto done;
1361 }
Sidipotu Ashok1e4d3472012-09-05 15:36:11 +05301362 client = msm_ion_client_create(UINT_MAX, "Audio_EVRC_in_client");
1363 if (IS_ERR_OR_NULL(client)) {
1364 MM_ERR("Unable to create ION client\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001365 rc = -ENOMEM;
Sidipotu Ashok1e4d3472012-09-05 15:36:11 +05301366 goto client_create_error;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001367 }
Sidipotu Ashok1e4d3472012-09-05 15:36:11 +05301368 audio->client = client;
1369
1370 MM_DBG("allocating mem sz = %d\n", DMASZ);
1371 handle = ion_alloc(client, DMASZ, SZ_4K,
Hanumant Singh7d72bad2012-08-29 18:39:44 -07001372 ION_HEAP(ION_AUDIO_HEAP_ID), 0);
Sidipotu Ashok1e4d3472012-09-05 15:36:11 +05301373 if (IS_ERR_OR_NULL(handle)) {
1374 MM_ERR("Unable to create allocate O/P buffers\n");
1375 rc = -ENOMEM;
1376 goto output_buff_alloc_error;
1377 }
1378
1379 audio->output_buff_handle = handle;
1380
1381 rc = ion_phys(client , handle, &addr, &len);
1382 if (rc) {
1383 MM_ERR("O/P buffers:Invalid phy: %x sz: %x\n",
1384 (unsigned int) addr, (unsigned int) len);
1385 rc = -ENOMEM;
1386 goto output_buff_get_phys_error;
1387 } else {
1388 MM_INFO("O/P buffers:valid phy: %x sz: %x\n",
1389 (unsigned int) addr, (unsigned int) len);
1390 }
1391 audio->phys = (int32_t)addr;
1392
1393 rc = ion_handle_get_flags(client, handle, &ionflag);
1394 if (rc) {
1395 MM_ERR("could not get flags for the handle\n");
1396 rc = -ENOMEM;
1397 goto output_buff_get_flags_error;
1398 }
1399
Mitchel Humpherys911b4b72012-09-12 14:42:50 -07001400 audio->map_v_read = ion_map_kernel(client, handle);
Sidipotu Ashok1e4d3472012-09-05 15:36:11 +05301401 if (IS_ERR(audio->map_v_read)) {
1402 MM_ERR("could not map read buffers,freeing instance 0x%08x\n",
1403 (int)audio);
1404 rc = -ENOMEM;
1405 goto output_buff_map_error;
1406 }
1407 audio->data = audio->map_v_read;
1408
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001409 MM_DBG("Memory addr = 0x%8x phy addr = 0x%8x\n",\
1410 (int) audio->data, (int) audio->phys);
1411 if ((file->f_mode & FMODE_WRITE) &&
1412 (file->f_mode & FMODE_READ)) {
1413 audio->mode = MSM_AUD_ENC_MODE_NONTUNNEL;
1414 MM_DBG("Opened for non tunnel mode encoding\n");
1415 } else if (!(file->f_mode & FMODE_WRITE) &&
1416 (file->f_mode & FMODE_READ)) {
1417 audio->mode = MSM_AUD_ENC_MODE_TUNNEL;
1418 MM_DBG("Opened for tunnel mode encoding\n");
1419 } else {
1420 MM_ERR("Invalid mode\n");
1421 rc = -EACCES;
1422 goto done;
1423 }
1424
1425 /* Settings will be re-config at AUDIO_SET_CONFIG,
1426 * but at least we need to have initial config
1427 */
1428 if (audio->mode == MSM_AUD_ENC_MODE_NONTUNNEL)
1429 audio->buffer_size = (QCELP_FRAME_SIZE + 14);
1430 else
1431 audio->buffer_size = (FRAME_SIZE - 8);
1432 audio->enc_type = ENC_TYPE_V13K | audio->mode;
1433 audio->samp_rate = 8000;
1434 audio->channel_mode = AUDREC_CMD_MODE_MONO;
1435 audio->cfg.cdma_rate = CDMA_RATE_FULL;
1436 audio->cfg.min_bit_rate = CDMA_RATE_FULL;
1437 audio->cfg.max_bit_rate = CDMA_RATE_FULL;
1438 audio->source = INTERNAL_CODEC_TX_SOURCE_MIX_MASK;
1439 audio->rec_mode = VOC_REC_UPLINK;
1440
1441 encid = audpreproc_aenc_alloc(audio->enc_type, &audio->module_name,
1442 &audio->queue_ids);
1443 if (encid < 0) {
1444 MM_ERR("No free encoder available\n");
1445 rc = -ENODEV;
1446 goto done;
1447 }
1448 audio->enc_id = encid;
1449
1450 rc = msm_adsp_get(audio->module_name, &audio->audrec,
1451 &audrec_qcelp_adsp_ops, audio);
1452
1453 if (rc) {
1454 audpreproc_aenc_free(audio->enc_id);
1455 goto done;
1456 }
1457
1458 audio->stopped = 0;
1459 audio->source = 0;
1460 audio->wflush = 0;
1461 audio->rflush = 0;
1462 audio->flush_ack = 0;
1463
1464 audqcelp_in_flush(audio);
1465 audqcelp_out_flush(audio);
1466
Sidipotu Ashok1e4d3472012-09-05 15:36:11 +05301467 MM_DBG("allocating BUFFER_SIZE %d\n", BUFFER_SIZE);
1468 handle = ion_alloc(client, BUFFER_SIZE,
Hanumant Singh7d72bad2012-08-29 18:39:44 -07001469 SZ_4K, ION_HEAP(ION_AUDIO_HEAP_ID), 0);
Sidipotu Ashok1e4d3472012-09-05 15:36:11 +05301470 if (IS_ERR_OR_NULL(handle)) {
1471 MM_ERR("Unable to create allocate I/P buffers\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001472 rc = -ENOMEM;
Sidipotu Ashok1e4d3472012-09-05 15:36:11 +05301473 goto input_buff_alloc_error;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001474 }
1475
Sidipotu Ashok1e4d3472012-09-05 15:36:11 +05301476 audio->input_buff_handle = handle;
1477
1478 rc = ion_phys(client , handle, &addr, &len);
1479 if (rc) {
1480 MM_ERR("I/P buffers:Invalid phy: %x sz: %x\n",
1481 (unsigned int) addr, (unsigned int) len);
1482 rc = -ENOMEM;
1483 goto input_buff_alloc_error;
1484 } else {
1485 MM_INFO("Got valid phy: %x sz: %x\n",
1486 (unsigned int) addr,
1487 (unsigned int) len);
1488 }
1489 audio->out_phys = (int32_t)addr;
1490
1491 rc = ion_handle_get_flags(client,
1492 handle, &ionflag);
1493 if (rc) {
1494 MM_ERR("could not get flags for the handle\n");
1495 rc = -ENOMEM;
1496 goto input_buff_alloc_error;
1497 }
1498
Mitchel Humpherys911b4b72012-09-12 14:42:50 -07001499 audio->map_v_write = ion_map_kernel(client, handle);
Sidipotu Ashok1e4d3472012-09-05 15:36:11 +05301500 if (IS_ERR(audio->map_v_write)) {
1501 MM_ERR("could not map write buffers\n");
1502 rc = -ENOMEM;
1503 goto input_buff_map_error;
1504 }
1505 audio->out_data = audio->map_v_write;
1506 MM_DBG("write buf: phy addr 0x%08x kernel addr 0x%08x\n",
1507 (unsigned int)addr,
1508 (unsigned int)audio->out_data);
1509
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001510 /* Initialize buffer */
1511 audio->out[0].data = audio->out_data + 0;
1512 audio->out[0].addr = audio->out_phys + 0;
1513 audio->out[0].size = OUT_BUFFER_SIZE;
1514
1515 audio->out[1].data = audio->out_data + OUT_BUFFER_SIZE;
1516 audio->out[1].addr = audio->out_phys + OUT_BUFFER_SIZE;
1517 audio->out[1].size = OUT_BUFFER_SIZE;
1518
1519 MM_DBG("audio->out[0].data = %d audio->out[1].data = %d",
1520 (unsigned int)audio->out[0].data,
1521 (unsigned int)audio->out[1].data);
1522 audio->device_events = AUDDEV_EVT_DEV_RDY | AUDDEV_EVT_DEV_RLS |
1523 AUDDEV_EVT_VOICE_STATE_CHG;
1524
1525 audio->voice_state = msm_get_voice_state();
1526 rc = auddev_register_evt_listner(audio->device_events,
1527 AUDDEV_CLNT_ENC, audio->enc_id,
1528 qcelp_in_listener, (void *) audio);
1529 if (rc) {
1530 MM_ERR("failed to register device event listener\n");
Laura Abbott61399692012-04-30 14:25:46 -07001531 iounmap(audio->map_v_write);
Santosh Mardifdc227a2011-07-11 17:20:34 +05301532 free_contiguous_memory_by_paddr(audio->out_phys);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001533 goto evt_error;
1534 }
1535 audio->mfield = META_OUT_SIZE;
1536 file->private_data = audio;
1537 audio->opened = 1;
1538 audio->out_frame_cnt++;
Kalyani polepeddy7413d2b2011-10-14 15:42:15 +05301539 audio->build_id = socinfo_get_build_id();
Vinay Vaka4ff52a62011-12-13 15:38:35 +05301540 MM_DBG("Modem build id = %s\n", audio->build_id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001541done:
1542 mutex_unlock(&audio->lock);
1543 return rc;
1544evt_error:
1545 msm_adsp_put(audio->audrec);
1546 audpreproc_aenc_free(audio->enc_id);
1547 mutex_unlock(&audio->lock);
Sidipotu Ashok1e4d3472012-09-05 15:36:11 +05301548input_buff_map_error:
1549 ion_free(client, audio->input_buff_handle);
1550input_buff_alloc_error:
1551 ion_unmap_kernel(client, audio->output_buff_handle);
1552output_buff_map_error:
1553output_buff_get_phys_error:
1554output_buff_get_flags_error:
1555 ion_free(client, audio->output_buff_handle);
1556output_buff_alloc_error:
1557 ion_client_destroy(client);
1558client_create_error:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001559 return rc;
1560}
1561
1562static const struct file_operations audio_in_fops = {
1563 .owner = THIS_MODULE,
1564 .open = audqcelp_in_open,
1565 .release = audqcelp_in_release,
1566 .read = audqcelp_in_read,
1567 .write = audqcelp_in_write,
1568 .fsync = audqcelp_in_fsync,
1569 .unlocked_ioctl = audqcelp_in_ioctl,
1570};
1571
1572struct miscdevice audio_qcelp_in_misc = {
1573 .minor = MISC_DYNAMIC_MINOR,
1574 .name = "msm_qcelp_in",
1575 .fops = &audio_in_fops,
1576};
1577
1578static int __init audqcelp_in_init(void)
1579{
1580 mutex_init(&the_audio_qcelp_in.lock);
1581 mutex_init(&the_audio_qcelp_in.read_lock);
1582 spin_lock_init(&the_audio_qcelp_in.dsp_lock);
1583 spin_lock_init(&the_audio_qcelp_in.dev_lock);
1584 init_waitqueue_head(&the_audio_qcelp_in.wait);
1585 init_waitqueue_head(&the_audio_qcelp_in.wait_enable);
1586 mutex_init(&the_audio_qcelp_in.write_lock);
1587 init_waitqueue_head(&the_audio_qcelp_in.write_wait);
1588 return misc_register(&audio_qcelp_in_misc);
1589}
1590
1591device_initcall(audqcelp_in_init);