blob: a878e129dbe5bf6c7028073e351e3beaa997c395 [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/*
2 * aac audio input device
3 *
4 * Copyright (C) 2008 Google, Inc.
5 * Copyright (C) 2008 HTC Corporation
Duy Truong790f06d2013-02-13 16:38:12 -08006 * Copyright (c) 2009-2012, 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>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070021#include <linux/module.h>
22#include <linux/fs.h>
23#include <linux/miscdevice.h>
24#include <linux/uaccess.h>
25#include <linux/sched.h>
26#include <linux/wait.h>
27#include <linux/dma-mapping.h>
28#include <linux/msm_audio_aac.h>
29#include <linux/android_pmem.h>
Santosh Mardifdc227a2011-07-11 17:20:34 +053030#include <linux/memory_alloc.h>
31#include <mach/msm_memtypes.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>
Santosh Mardifdc227a2011-07-11 17:20:34 +053035#include <mach/iommu_domains.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070036#include <mach/qdsp5v2/qdsp5audreccmdi.h>
37#include <mach/qdsp5v2/qdsp5audrecmsg.h>
38#include <mach/qdsp5v2/audpreproc.h>
39#include <mach/qdsp5v2/audio_dev_ctl.h>
40#include <mach/debug_mm.h>
Kalyani polepeddy7413d2b2011-10-14 15:42:15 +053041#include <mach/socinfo.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070042
43/* FRAME_NUM must be a power of two */
44#define FRAME_NUM (8)
45#define FRAME_SIZE (772 * 2) /* 1536 bytes data */
46#define NT_FRAME_SIZE (780 * 2) /* 1536 bytes data + 24 meta field*/
47#define AAC_FRAME_SIZE 1536
48#define DMASZ (FRAME_SIZE * FRAME_NUM)
49#define OUT_FRAME_NUM (2)
50#define META_OUT_SIZE (24)
51#define META_IN_SIZE (14)
52#define OUT_BUFFER_SIZE (32 * 1024 + META_OUT_SIZE)
53#define BUFFER_SIZE (OUT_BUFFER_SIZE * OUT_FRAME_NUM)
54
55#define AUDPREPROC_AAC_EOS_FLG_OFFSET 0x0A /* Offset from beginning of buffer */
56#define AUDPREPROC_AAC_EOS_FLG_MASK 0x01
57#define AUDPREPROC_AAC_EOS_NONE 0x0 /* No EOS detected */
58#define AUDPREPROC_AAC_EOS_SET 0x1 /* EOS set in meta field */
59
60#define PCM_CONFIG_UPDATE_FLAG_ENABLE -1
61#define PCM_CONFIG_UPDATE_FLAG_DISABLE 0
62
63#define ENABLE_FLAG_VALUE -1
64#define DISABLE_FLAG_VALUE 0
65
66struct buffer {
67 void *data;
68 uint32_t size;
69 uint32_t read;
70 uint32_t addr;
71 uint32_t used;
72 uint32_t mfield_sz;
73};
74
75struct audio_in {
76 struct buffer in[FRAME_NUM];
77
78 spinlock_t dsp_lock;
79
80 atomic_t in_bytes;
81 atomic_t in_samples;
82
83 struct mutex lock;
84 struct mutex read_lock;
85 wait_queue_head_t wait;
86 wait_queue_head_t wait_enable;
87 /*write section*/
88 struct buffer out[OUT_FRAME_NUM];
89
90 uint8_t out_head;
91 uint8_t out_tail;
92 uint8_t out_needed; /* number of buffers the dsp is waiting for */
93 uint32_t out_count;
94
95 struct mutex write_lock;
96 wait_queue_head_t write_wait;
97 int32_t out_phys; /* physical address of write buffer */
98 char *out_data;
Laura Abbott61399692012-04-30 14:25:46 -070099 void *map_v_read;
100 void *map_v_write;
Santosh Mardifdc227a2011-07-11 17:20:34 +0530101
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700102 int mfield; /* meta field embedded in data */
103 int wflush; /*write flush */
104 int rflush; /*read flush*/
105 int out_frame_cnt;
106
107 struct msm_adsp_module *audrec;
108
109 /* configuration to use on next enable */
110 uint32_t buffer_size; /* Frame size (36 bytes) */
111 uint32_t samp_rate;
112 uint32_t channel_mode;
113 uint32_t bit_rate; /* bit rate for AAC */
114 uint32_t record_quality; /* record quality (bits/sample/channel) */
115 uint32_t enc_type;
116
117 uint32_t dsp_cnt;
118 uint32_t in_head; /* next buffer dsp will write */
119 uint32_t in_tail; /* next buffer read() will read */
120 uint32_t in_count; /* number of buffers available to read() */
121 uint32_t mode;
122 uint32_t eos_ack;
123 uint32_t flush_ack;
124
125 const char *module_name;
126 unsigned queue_ids;
127 uint16_t enc_id;
128
129 struct audrec_session_info session_info; /*audrec session info*/
130 uint16_t source; /* Encoding source bit mask */
131 uint32_t device_events; /* device events interested in */
132 uint32_t dev_cnt;
133 spinlock_t dev_lock;
134
135 /* data allocated for various buffers */
136 char *data;
137 dma_addr_t phys;
138
139 int opened;
140 int enabled;
141 int running;
142 int stopped; /* set when stopped, cleared on flush */
143 int abort; /* set when error, like sample rate mismatch */
Kalyani polepeddy7413d2b2011-10-14 15:42:15 +0530144 char *build_id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700145};
146
147struct audio_frame {
148 uint16_t frame_count_lsw;
149 uint16_t frame_count_msw;
150 uint16_t frame_length;
151 uint16_t erased_pcm;
152 unsigned char raw_bitstream[]; /* samples */
153} __attribute__((packed));
154
155struct audio_frame_nt {
156 uint16_t metadata_len;
157 uint16_t frame_count_lsw;
158 uint16_t frame_count_msw;
159 uint16_t frame_length;
160 uint16_t erased_pcm;
161 uint16_t reserved;
162 uint16_t time_stamp_dword_lsw;
163 uint16_t time_stamp_dword_msw;
164 uint16_t time_stamp_lsw;
165 uint16_t time_stamp_msw;
166 uint16_t nflag_lsw;
167 uint16_t nflag_msw;
168 unsigned char raw_bitstream[]; /* samples */
169} __attribute__((packed));
170
171struct aac_encoded_meta_in {
172 uint16_t metadata_len;
173 uint16_t time_stamp_dword_lsw;
174 uint16_t time_stamp_dword_msw;
175 uint16_t time_stamp_lsw;
176 uint16_t time_stamp_msw;
177 uint16_t nflag_lsw;
178 uint16_t nflag_msw;
179};
180
181/* Audrec Queue command sent macro's */
182#define audrec_send_bitstreamqueue(audio, cmd, len) \
183 msm_adsp_write(audio->audrec, ((audio->queue_ids & 0xFFFF0000) >> 16),\
184 cmd, len)
185
186#define audrec_send_audrecqueue(audio, cmd, len) \
187 msm_adsp_write(audio->audrec, (audio->queue_ids & 0x0000FFFF),\
188 cmd, len)
189
190/* DSP command send functions */
191static int audaac_in_enc_config(struct audio_in *audio, int enable);
192static int audaac_in_param_config(struct audio_in *audio);
193static int audaac_in_mem_config(struct audio_in *audio);
194static int audaac_in_record_config(struct audio_in *audio, int enable);
195static int audaac_dsp_read_buffer(struct audio_in *audio, uint32_t read_cnt);
196
197static void audaac_in_get_dsp_frames(struct audio_in *audio);
198static int audpcm_config(struct audio_in *audio);
199static void audaac_out_flush(struct audio_in *audio);
200static int audpreproc_cmd_cfg_routing_mode(struct audio_in *audio);
201static void audpreproc_pcm_send_data(struct audio_in *audio, unsigned needed);
202static void audaac_nt_in_get_dsp_frames(struct audio_in *audio);
203
204static void audaac_in_flush(struct audio_in *audio);
205
206static void aac_in_listener(u32 evt_id, union auddev_evt_data *evt_payload,
207 void *private_data)
208{
209 struct audio_in *audio = (struct audio_in *) private_data;
210 unsigned long flags;
211
212 MM_DBG("evt_id = 0x%8x\n", evt_id);
213 switch (evt_id) {
214 case AUDDEV_EVT_DEV_RDY: {
215 MM_DBG("AUDDEV_EVT_DEV_RDY\n");
216 spin_lock_irqsave(&audio->dev_lock, flags);
217 audio->dev_cnt++;
218 audio->source |= (0x1 << evt_payload->routing_id);
219 spin_unlock_irqrestore(&audio->dev_lock, flags);
220
221 if ((audio->running == 1) && (audio->enabled == 1) &&
222 (audio->mode == MSM_AUD_ENC_MODE_TUNNEL))
223 audaac_in_record_config(audio, 1);
224
225 break;
226 }
227 case AUDDEV_EVT_DEV_RLS: {
228 MM_DBG("AUDDEV_EVT_DEV_RLS\n");
229 spin_lock_irqsave(&audio->dev_lock, flags);
230 audio->dev_cnt--;
231 audio->source &= ~(0x1 << evt_payload->routing_id);
232 spin_unlock_irqrestore(&audio->dev_lock, flags);
233
234 if ((!audio->running) || (!audio->enabled))
235 break;
236
237 if (audio->mode == MSM_AUD_ENC_MODE_TUNNEL) {
238 /* Turn of as per source */
239 if (audio->source)
240 audaac_in_record_config(audio, 1);
241 else
242 /* Turn off all */
243 audaac_in_record_config(audio, 0);
244 }
245 break;
246 }
247 case AUDDEV_EVT_FREQ_CHG: {
248 MM_DBG("Encoder Driver got sample rate change event\n");
249 MM_DBG("sample rate %d\n", evt_payload->freq_info.sample_rate);
250 MM_DBG("dev_type %d\n", evt_payload->freq_info.dev_type);
251 MM_DBG("acdb_dev_id %d\n", evt_payload->freq_info.acdb_dev_id);
252 if ((audio->running == 1) && (audio->enabled == 1)) {
253 /* Stop Recording sample rate does not match
254 with device sample rate */
255 if (evt_payload->freq_info.sample_rate !=
256 audio->samp_rate) {
257 if (audio->mode == MSM_AUD_ENC_MODE_TUNNEL)
258 audaac_in_record_config(audio, 0);
259 audio->abort = 1;
260 wake_up(&audio->wait);
261 }
262 }
263 break;
264 }
265 default:
266 MM_ERR("wrong event %d\n", evt_id);
267 break;
268 }
269}
270
271/* Convert Bit Rate to Record Quality field of DSP */
272static unsigned int bitrate_to_record_quality(unsigned int sample_rate,
273 unsigned int channel, unsigned int bit_rate) {
274 unsigned int temp;
275
276 temp = sample_rate * channel;
277 MM_DBG(" sample rate * channel = %d \n", temp);
278 /* To represent in Q12 fixed format */
279 temp = (bit_rate * 4096) / temp;
280 MM_DBG(" Record Quality = 0x%8x \n", temp);
281 return temp;
282}
283
284/* ------------------- dsp preproc event handler--------------------- */
285static void audpreproc_dsp_event(void *data, unsigned id, void *msg)
286{
287 struct audio_in *audio = data;
288
289 switch (id) {
290 case AUDPREPROC_ERROR_MSG: {
291 struct audpreproc_err_msg *err_msg = msg;
292
293 MM_ERR("ERROR_MSG: stream id %d err idx %d\n",
294 err_msg->stream_id, err_msg->aud_preproc_err_idx);
295 /* Error case */
296 wake_up(&audio->wait_enable);
297 break;
298 }
299 case AUDPREPROC_CMD_CFG_DONE_MSG: {
300 MM_DBG("CMD_CFG_DONE_MSG \n");
301 break;
302 }
303 case AUDPREPROC_CMD_ENC_CFG_DONE_MSG: {
304 struct audpreproc_cmd_enc_cfg_done_msg *enc_cfg_msg = msg;
305
306 MM_DBG("CMD_ENC_CFG_DONE_MSG: stream id %d enc type \
307 0x%8x\n", enc_cfg_msg->stream_id,
308 enc_cfg_msg->rec_enc_type);
309 /* Encoder enable success */
310 if (enc_cfg_msg->rec_enc_type & ENCODE_ENABLE) {
311 if(audio->mode == MSM_AUD_ENC_MODE_NONTUNNEL) {
312 MM_DBG("routing command\n");
313 audpreproc_cmd_cfg_routing_mode(audio);
314 } else {
315 audaac_in_param_config(audio);
316 }
317 } else { /* Encoder disable success */
318 audio->running = 0;
319 if (audio->mode == MSM_AUD_ENC_MODE_TUNNEL)
320 audaac_in_record_config(audio, 0);
321 else
322 wake_up(&audio->wait_enable);
323 }
324 break;
325 }
326 case AUDPREPROC_CMD_ENC_PARAM_CFG_DONE_MSG: {
327 MM_DBG("CMD_ENC_PARAM_CFG_DONE_MSG\n");
328 if (audio->mode == MSM_AUD_ENC_MODE_TUNNEL)
329 audaac_in_mem_config(audio);
330 else
331 audpcm_config(audio);
332 break;
333 }
334 case AUDPREPROC_CMD_ROUTING_MODE_DONE_MSG: {
335 struct audpreproc_cmd_routing_mode_done\
336 *routing_cfg_done_msg = msg;
337 if (routing_cfg_done_msg->configuration == 0) {
338 MM_INFO("routing configuration failed\n");
339 audio->running = 0;
340 } else
341 audaac_in_param_config(audio);
342 break;
343 }
344 case AUDPREPROC_AFE_CMD_AUDIO_RECORD_CFG_DONE_MSG: {
345 MM_DBG("AFE_CMD_AUDIO_RECORD_CFG_DONE_MSG\n");
346 wake_up(&audio->wait_enable);
347 break;
348 }
349 default:
350 MM_ERR("Unknown Event id %d\n", id);
351 }
352}
353
354/* ------------------- dsp audrec event handler--------------------- */
355static void audrec_dsp_event(void *data, unsigned id, size_t len,
356 void (*getevent)(void *ptr, size_t len))
357{
358 struct audio_in *audio = data;
359
360 switch (id) {
361 case AUDREC_CMD_MEM_CFG_DONE_MSG: {
362 MM_DBG("CMD_MEM_CFG_DONE MSG DONE\n");
363 audio->running = 1;
364 if (audio->mode == MSM_AUD_ENC_MODE_TUNNEL) {
365 if (audio->dev_cnt > 0)
366 audaac_in_record_config(audio, 1);
367 } else {
368 audpreproc_pcm_send_data(audio, 1);
369 wake_up(&audio->wait_enable);
370 }
371 break;
372 }
373 case AUDREC_FATAL_ERR_MSG: {
374 struct audrec_fatal_err_msg fatal_err_msg;
375
376 getevent(&fatal_err_msg, AUDREC_FATAL_ERR_MSG_LEN);
377 MM_ERR("FATAL_ERR_MSG: err id %d\n",
378 fatal_err_msg.audrec_err_id);
379 /* Error stop the encoder */
380 audio->stopped = 1;
381 wake_up(&audio->wait);
382 if (audio->mode == MSM_AUD_ENC_MODE_NONTUNNEL)
383 wake_up(&audio->write_wait);
384 break;
385 }
386 case AUDREC_UP_PACKET_READY_MSG: {
387 struct audrec_up_pkt_ready_msg pkt_ready_msg;
388
389 getevent(&pkt_ready_msg, AUDREC_UP_PACKET_READY_MSG_LEN);
390 MM_DBG("UP_PACKET_READY_MSG: write cnt lsw %d \
391 write cnt msw %d read cnt lsw %d read cnt msw %d \n",\
392 pkt_ready_msg.audrec_packet_write_cnt_lsw, \
393 pkt_ready_msg.audrec_packet_write_cnt_msw, \
394 pkt_ready_msg.audrec_up_prev_read_cnt_lsw, \
395 pkt_ready_msg.audrec_up_prev_read_cnt_msw);
396
397 audaac_in_get_dsp_frames(audio);
398 break;
399 }
400 case AUDREC_CMD_PCM_BUFFER_PTR_UPDATE_ARM_TO_ENC_MSG: {
401 MM_DBG("ptr_update recieved from DSP\n");
402 audpreproc_pcm_send_data(audio, 1);
403 break;
404 }
405 case AUDREC_CMD_PCM_CFG_ARM_TO_ENC_DONE_MSG: {
406 MM_ERR("AUDREC_CMD_PCM_CFG_ARM_TO_ENC_DONE_MSG");
407 audaac_in_mem_config(audio);
408 break;
409 }
410 case AUDREC_UP_NT_PACKET_READY_MSG: {
411 struct audrec_up_nt_packet_ready_msg pkt_ready_msg;
412
413 getevent(&pkt_ready_msg, AUDREC_UP_NT_PACKET_READY_MSG_LEN);
414 MM_DBG("UP_NT_PACKET_READY_MSG: write cnt lsw %d \
415 write cnt msw %d read cnt lsw %d read cnt msw %d \n",\
416 pkt_ready_msg.audrec_packetwrite_cnt_lsw, \
417 pkt_ready_msg.audrec_packetwrite_cnt_msw, \
418 pkt_ready_msg.audrec_upprev_readcount_lsw, \
419 pkt_ready_msg.audrec_upprev_readcount_msw);
420
421 audaac_nt_in_get_dsp_frames(audio);
422 break;
423 }
424 case AUDREC_CMD_EOS_ACK_MSG: {
425 MM_DBG("eos ack recieved\n");
426 break;
427 }
428 case AUDREC_CMD_FLUSH_DONE_MSG: {
429 audio->wflush = 0;
430 audio->rflush = 0;
431 audio->flush_ack = 1;
432 wake_up(&audio->write_wait);
433 MM_DBG("flush ack recieved\n");
434 break;
435 }
436 case ADSP_MESSAGE_ID: {
437 MM_DBG("Received ADSP event:module audrectask\n");
438 break;
439 }
440 default:
441 MM_ERR("Unknown Event id %d\n", id);
442 }
443}
444
445static void audaac_in_get_dsp_frames(struct audio_in *audio)
446{
447 struct audio_frame *frame;
448 uint32_t index;
449 unsigned long flags;
450
451 MM_DBG("head = %d\n", audio->in_head);
452 index = audio->in_head;
453
454 frame = (void *) (((char *)audio->in[index].data) - \
455 sizeof(*frame));
456
457 spin_lock_irqsave(&audio->dsp_lock, flags);
458 audio->in[index].size = frame->frame_length;
459
460 /* statistics of read */
461 atomic_add(audio->in[index].size, &audio->in_bytes);
462 atomic_add(1, &audio->in_samples);
463
464 audio->in_head = (audio->in_head + 1) & (FRAME_NUM - 1);
465
466 /* If overflow, move the tail index foward. */
467 if (audio->in_head == audio->in_tail) {
468 MM_ERR("Error! not able to keep up the read\n");
469 audio->in_tail = (audio->in_tail + 1) & (FRAME_NUM - 1);
470 } else
471 audio->in_count++;
472
473 audaac_dsp_read_buffer(audio, audio->dsp_cnt++);
474 spin_unlock_irqrestore(&audio->dsp_lock, flags);
475
476 wake_up(&audio->wait);
477}
478
479static void audaac_nt_in_get_dsp_frames(struct audio_in *audio)
480{
481 struct audio_frame_nt *nt_frame;
482 uint32_t index;
483 unsigned long flags;
484 MM_DBG("head = %d\n", audio->in_head);
485 index = audio->in_head;
486 nt_frame = (void *) (((char *)audio->in[index].data) - \
487 sizeof(struct audio_frame_nt));
488 spin_lock_irqsave(&audio->dsp_lock, flags);
489 audio->in[index].size = nt_frame->frame_length;
490 /* statistics of read */
491 atomic_add(audio->in[index].size, &audio->in_bytes);
492 atomic_add(1, &audio->in_samples);
493
494 audio->in_head = (audio->in_head + 1) & (FRAME_NUM - 1);
495
496 /* If overflow, move the tail index foward. */
497 if (audio->in_head == audio->in_tail)
498 MM_DBG("Error! not able to keep up the read\n");
499 else
500 audio->in_count++;
501
502 spin_unlock_irqrestore(&audio->dsp_lock, flags);
503 wake_up(&audio->wait);
504}
505
506
507struct msm_adsp_ops audrec_aac_adsp_ops = {
508 .event = audrec_dsp_event,
509};
510
511static int audpreproc_pcm_buffer_ptr_refresh(struct audio_in *audio,
512 unsigned idx, unsigned len)
513{
514 struct audrec_cmd_pcm_buffer_ptr_refresh_arm_enc cmd;
515
516 if (len == META_OUT_SIZE)
517 len = len / 2;
518 else
519 len = (len + META_OUT_SIZE) / 2;
520 MM_DBG("len = %d\n", len);
521 memset(&cmd, 0, sizeof(cmd));
522 cmd.cmd_id = AUDREC_CMD_PCM_BUFFER_PTR_REFRESH_ARM_TO_ENC;
523 cmd.num_buffers = 1;
524 if (cmd.num_buffers == 1) {
525 cmd.buf_address_length[0] = (audio->out[idx].addr &
526 0xffff0000) >> 16;
527 cmd.buf_address_length[1] = (audio->out[idx].addr &
528 0x0000ffff);
529 cmd.buf_address_length[2] = (len & 0xffff0000) >> 16;
530 cmd.buf_address_length[3] = (len & 0x0000ffff);
531 }
532 audio->out_frame_cnt++;
533 return audrec_send_audrecqueue(audio, (void *)&cmd,
534 (unsigned int)sizeof(cmd));
535}
536
537
538static int audpcm_config(struct audio_in *audio)
539{
540 struct audrec_cmd_pcm_cfg_arm_to_enc cmd;
541 MM_DBG("\n");
542 memset(&cmd, 0, sizeof(cmd));
543 cmd.cmd_id = AUDREC_CMD_PCM_CFG_ARM_TO_ENC;
544 cmd.config_update_flag = PCM_CONFIG_UPDATE_FLAG_ENABLE;
545 cmd.enable_flag = ENABLE_FLAG_VALUE;
546 cmd.sampling_freq = audio->samp_rate;
547 if (!audio->channel_mode)
548 cmd.channels = 1;
549 else
550 cmd.channels = 2;
551 cmd.frequency_of_intimation = 1;
552 cmd.max_number_of_buffers = OUT_FRAME_NUM;
553 return audrec_send_audrecqueue(audio, (void *)&cmd,
554 (unsigned int)sizeof(cmd));
555}
556
557
558static int audpreproc_cmd_cfg_routing_mode(struct audio_in *audio)
559{
560 struct audpreproc_audrec_cmd_routing_mode cmd;
561
562 MM_DBG("\n");
563 memset(&cmd, 0, sizeof(cmd));
564 cmd.cmd_id = AUDPREPROC_AUDREC_CMD_ROUTING_MODE;
565 cmd.stream_id = audio->enc_id;
566 if (audio->mode == MSM_ADSP_ENC_MODE_NON_TUNNEL)
567 cmd.routing_mode = 1;
568 return audpreproc_send_audreccmdqueue(&cmd, sizeof(cmd));
569}
570
571
572
573static int audaac_in_enc_config(struct audio_in *audio, int enable)
574{
575 struct audpreproc_audrec_cmd_enc_cfg cmd;
576 memset(&cmd, 0, sizeof(cmd));
Kalyani polepeddy7413d2b2011-10-14 15:42:15 +0530577 if (audio->build_id[17] == '1') {
578 cmd.cmd_id = AUDPREPROC_AUDREC_CMD_ENC_CFG_2;
579 MM_ERR("sending AUDPREPROC_AUDREC_CMD_ENC_CFG_2 command");
580 } else {
581 cmd.cmd_id = AUDPREPROC_AUDREC_CMD_ENC_CFG;
582 MM_ERR("sending AUDPREPROC_AUDREC_CMD_ENC_CFG command");
583 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700584 cmd.stream_id = audio->enc_id;
585
586 if (enable)
587 cmd.audrec_enc_type = audio->enc_type | ENCODE_ENABLE;
588 else
589 cmd.audrec_enc_type &= ~(ENCODE_ENABLE);
590
591 return audpreproc_send_audreccmdqueue(&cmd, sizeof(cmd));
592}
593
594static int audaac_in_param_config(struct audio_in *audio)
595{
596 struct audpreproc_audrec_cmd_parm_cfg_aac cmd;
597
598 MM_DBG("\n");
599 memset(&cmd, 0, sizeof(cmd));
600 cmd.common.cmd_id = AUDPREPROC_AUDREC_CMD_PARAM_CFG;
601 cmd.common.stream_id = audio->enc_id;
602
603 cmd.aud_rec_samplerate_idx = audio->samp_rate;
604 cmd.aud_rec_stereo_mode = audio->channel_mode;
605 cmd.recording_quality = audio->record_quality;
606
607 return audpreproc_send_audreccmdqueue(&cmd, sizeof(cmd));
608}
609
610/* To Do: msm_snddev_route_enc(audio->enc_id); */
611static int audaac_in_record_config(struct audio_in *audio, int enable)
612{
613 struct audpreproc_afe_cmd_audio_record_cfg cmd;
614 MM_DBG("\n");
615 memset(&cmd, 0, sizeof(cmd));
616 cmd.cmd_id = AUDPREPROC_AFE_CMD_AUDIO_RECORD_CFG;
617 cmd.stream_id = audio->enc_id;
618 if (enable)
619 cmd.destination_activity = AUDIO_RECORDING_TURN_ON;
620 else
621 cmd.destination_activity = AUDIO_RECORDING_TURN_OFF;
622
623 cmd.source_mix_mask = audio->source;
624 if (audio->enc_id == 2) {
625 if ((cmd.source_mix_mask & INTERNAL_CODEC_TX_SOURCE_MIX_MASK) ||
626 (cmd.source_mix_mask & AUX_CODEC_TX_SOURCE_MIX_MASK) ||
627 (cmd.source_mix_mask & VOICE_UL_SOURCE_MIX_MASK) ||
628 (cmd.source_mix_mask & VOICE_DL_SOURCE_MIX_MASK)) {
629 cmd.pipe_id = SOURCE_PIPE_1;
630 }
631 if (cmd.source_mix_mask &
632 AUDPP_A2DP_PIPE_SOURCE_MIX_MASK)
633 cmd.pipe_id |= SOURCE_PIPE_0;
634 }
635 return audpreproc_send_audreccmdqueue(&cmd, sizeof(cmd));
636}
637
638static int audaac_in_mem_config(struct audio_in *audio)
639{
640 struct audrec_cmd_arecmem_cfg cmd;
641 uint16_t *data = (void *) audio->data;
642 int n;
643 memset(&cmd, 0, sizeof(cmd));
644 cmd.cmd_id = AUDREC_CMD_MEM_CFG_CMD;
645 cmd.audrec_up_pkt_intm_count = 1;
646 cmd.audrec_ext_pkt_start_addr_msw = audio->phys >> 16;
647 cmd.audrec_ext_pkt_start_addr_lsw = audio->phys;
648 cmd.audrec_ext_pkt_buf_number = FRAME_NUM;
649 MM_DBG("audio->phys = %x\n", audio->phys);
650 /* prepare buffer pointers:
651 * 1536 bytes aac packet + 4 halfword header
652 */
653 for (n = 0; n < FRAME_NUM; n++) {
654 if (audio->mode == MSM_AUD_ENC_MODE_TUNNEL) {
655 audio->in[n].data = data + 4;
656 data += (FRAME_SIZE/2);
657 MM_DBG("0x%8x\n", (int)(audio->in[n].data - 8));
658 } else {
659 audio->in[n].data = data + 12;
660 data += ((AAC_FRAME_SIZE) / 2) + 12;
661 MM_DBG("0x%8x\n", (int)(audio->in[n].data - 24));
662 }
663 }
664 return audrec_send_audrecqueue(audio, &cmd, sizeof(cmd));
665}
666
667static int audaac_dsp_read_buffer(struct audio_in *audio, uint32_t read_cnt)
668{
669 struct up_audrec_packet_ext_ptr cmd;
670
671 memset(&cmd, 0, sizeof(cmd));
672 cmd.cmd_id = UP_AUDREC_PACKET_EXT_PTR;
673 cmd.audrec_up_curr_read_count_msw = read_cnt >> 16;
674 cmd.audrec_up_curr_read_count_lsw = read_cnt;
675
676 return audrec_send_bitstreamqueue(audio, &cmd, sizeof(cmd));
677}
678static int audaac_flush_command(struct audio_in *audio)
679{
680 struct audrec_cmd_flush cmd;
681 MM_DBG("\n");
682 memset(&cmd, 0, sizeof(cmd));
683 cmd.cmd_id = AUDREC_CMD_FLUSH;
684 return audrec_send_audrecqueue(audio, &cmd, sizeof(cmd));
685}
686
687/* must be called with audio->lock held */
688static int audaac_in_enable(struct audio_in *audio)
689{
690 if (audio->enabled)
691 return 0;
692
693 if (audpreproc_enable(audio->enc_id, &audpreproc_dsp_event, audio)) {
694 MM_ERR("msm_adsp_enable(audpreproc) failed\n");
695 return -ENODEV;
696 }
697
698 if (msm_adsp_enable(audio->audrec)) {
699 MM_ERR("msm_adsp_enable(audrec) failed\n");
700 audpreproc_disable(audio->enc_id, audio);
701 return -ENODEV;
702 }
703 audio->enabled = 1;
704 audaac_in_enc_config(audio, 1);
705
706 return 0;
707}
708
709/* must be called with audio->lock held */
710static int audaac_in_disable(struct audio_in *audio)
711{
712 if (audio->enabled) {
713 audio->enabled = 0;
714 audaac_in_enc_config(audio, 0);
715 wake_up(&audio->wait);
716 wait_event_interruptible_timeout(audio->wait_enable,
717 audio->running == 0, 1*HZ);
718 msm_adsp_disable(audio->audrec);
719 audpreproc_disable(audio->enc_id, audio);
720 }
721 return 0;
722}
723
724static void audaac_ioport_reset(struct audio_in *audio)
725{
726 /* Make sure read/write thread are free from
727 * sleep and knowing that system is not able
728 * to process io request at the moment
729 */
730 wake_up(&audio->write_wait);
731 mutex_lock(&audio->write_lock);
732 audaac_in_flush(audio);
733 mutex_unlock(&audio->write_lock);
734 wake_up(&audio->wait);
735 mutex_lock(&audio->read_lock);
736 audaac_out_flush(audio);
737 mutex_unlock(&audio->read_lock);
738}
739
740static void audaac_in_flush(struct audio_in *audio)
741{
742 int i;
743
744 audio->dsp_cnt = 0;
745 audio->in_head = 0;
746 audio->in_tail = 0;
747 audio->in_count = 0;
748 audio->eos_ack = 0;
749 for (i = 0; i < FRAME_NUM; i++) {
750 audio->in[i].size = 0;
751 audio->in[i].read = 0;
752 }
753 MM_DBG("in_bytes %d\n", atomic_read(&audio->in_bytes));
754 MM_DBG("in_samples %d\n", atomic_read(&audio->in_samples));
755 atomic_set(&audio->in_bytes, 0);
756 atomic_set(&audio->in_samples, 0);
757}
758
759static void audaac_out_flush(struct audio_in *audio)
760{
761 int i;
762
763 audio->out_head = 0;
764 audio->out_tail = 0;
765 audio->out_count = 0;
766 for (i = 0; i < OUT_FRAME_NUM; i++) {
767 audio->out[i].size = 0;
768 audio->out[i].read = 0;
769 audio->out[i].used = 0;
770 }
771}
772
773/* ------------------- device --------------------- */
774static long audaac_in_ioctl(struct file *file,
775 unsigned int cmd, unsigned long arg)
776{
777 struct audio_in *audio = file->private_data;
778 int rc = 0;
779
780 if (cmd == AUDIO_GET_STATS) {
781 struct msm_audio_stats stats;
782 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 /* Poll at 48KHz always */
794 freq = 48000;
795 MM_DBG("AUDIO_START\n");
796 rc = msm_snddev_request_freq(&freq, audio->enc_id,
797 SNDDEV_CAP_TX, AUDDEV_CLNT_ENC);
798 MM_DBG("sample rate configured %d sample rate requested %d\n",
799 freq, audio->samp_rate);
800 if (rc < 0) {
801 MM_DBG(" Sample rate can not be set, return code %d\n",
802 rc);
803 msm_snddev_withdraw_freq(audio->enc_id,
804 SNDDEV_CAP_TX, AUDDEV_CLNT_ENC);
805 MM_DBG("msm_snddev_withdraw_freq\n");
806 break;
807 }
808 /*update aurec session info in audpreproc layer*/
809 audio->session_info.session_id = audio->enc_id;
810 audio->session_info.sampling_freq = audio->samp_rate;
811 audpreproc_update_audrec_info(&audio->session_info);
812 rc = audaac_in_enable(audio);
813 if (!rc) {
814 rc =
815 wait_event_interruptible_timeout(audio->wait_enable,
816 audio->running != 0, 1*HZ);
817 MM_DBG("state %d rc = %d\n", audio->running, rc);
818
819 if (audio->running == 0)
820 rc = -ENODEV;
821 else
822 rc = 0;
823 }
824 audio->stopped = 0;
825 break;
826 }
827 case AUDIO_STOP: {
828 audio->session_info.sampling_freq = 0;
829 audpreproc_update_audrec_info(&audio->session_info);
830 rc = audaac_in_disable(audio);
831 rc = msm_snddev_withdraw_freq(audio->enc_id,
832 SNDDEV_CAP_TX, AUDDEV_CLNT_ENC);
833 MM_DBG("msm_snddev_withdraw_freq\n");
834 audio->stopped = 1;
835 audio->abort = 0;
836 break;
837 }
838 case AUDIO_FLUSH:
839 MM_DBG("AUDIO_FLUSH\n");
840 audio->rflush = 1;
841 audio->wflush = 1;
842 audaac_ioport_reset(audio);
843 if (audio->running) {
844 audaac_flush_command(audio);
845 rc = wait_event_interruptible(audio->write_wait,
846 !audio->wflush);
847 if (rc < 0) {
848 MM_ERR("AUDIO_FLUSH interrupted\n");
849 rc = -EINTR;
850 }
851 } else {
852 audio->rflush = 0;
853 audio->wflush = 0;
854 }
855 break;
856 case AUDIO_GET_STREAM_CONFIG: {
857 struct msm_audio_stream_config cfg;
858 memset(&cfg, 0, sizeof(cfg));
859 cfg.buffer_size = audio->buffer_size;
860 cfg.buffer_count = FRAME_NUM;
861 if (copy_to_user((void *)arg, &cfg, sizeof(cfg)))
862 rc = -EFAULT;
863 break;
864 }
865 case AUDIO_SET_STREAM_CONFIG: {
866 struct msm_audio_stream_config cfg;
867 if (copy_from_user(&cfg, (void *)arg, sizeof(cfg))) {
868 rc = -EFAULT;
869 break;
870 }
871 /* Allow only single frame */
872 if (audio->mode == MSM_AUD_ENC_MODE_TUNNEL) {
873 if (cfg.buffer_size != (FRAME_SIZE - 8)) {
874 rc = -EINVAL;
875 break;
876 }
877 } else {
878 if (cfg.buffer_size != (NT_FRAME_SIZE - 24)) {
879 rc = -EINVAL;
880 break;
881 }
882 }
883 audio->buffer_size = cfg.buffer_size;
884 break;
885 }
886 case AUDIO_GET_CONFIG: {
887 struct msm_audio_pcm_config cfg;
888 memset(&cfg, 0, sizeof(cfg));
889 cfg.buffer_size = OUT_BUFFER_SIZE;
890 cfg.buffer_count = OUT_FRAME_NUM;
891 if (copy_to_user((void *)arg, &cfg, sizeof(cfg)))
892 rc = -EFAULT;
893 break;
894 }
895 case AUDIO_GET_AAC_ENC_CONFIG: {
896 struct msm_audio_aac_enc_config cfg;
897 if (audio->channel_mode == AUDREC_CMD_MODE_MONO)
898 cfg.channels = 1;
899 else
900 cfg.channels = 2;
901 cfg.sample_rate = audio->samp_rate;
902 cfg.bit_rate = audio->bit_rate;
903 cfg.stream_format = AUDIO_AAC_FORMAT_RAW;
904 if (copy_to_user((void *)arg, &cfg, sizeof(cfg)))
905 rc = -EFAULT;
906 break;
907 }
908 case AUDIO_SET_AAC_ENC_CONFIG: {
909 struct msm_audio_aac_enc_config cfg;
910 unsigned int record_quality;
911 if (copy_from_user(&cfg, (void *)arg, sizeof(cfg))) {
912 rc = -EFAULT;
913 break;
914 }
915 if (cfg.stream_format != AUDIO_AAC_FORMAT_RAW) {
916 MM_ERR("unsupported AAC format\n");
917 rc = -EINVAL;
918 break;
919 }
920 record_quality = bitrate_to_record_quality(cfg.sample_rate,
921 cfg.channels, cfg.bit_rate);
922 /* Range of Record Quality Supported by DSP, Q12 format */
923 if ((record_quality < 0x800) || (record_quality > 0x4000)) {
924 MM_ERR("Unsupported bit rate \n");
925 rc = -EINVAL;
926 break;
927 }
928 MM_DBG("channels = %d\n", cfg.channels);
929 if (cfg.channels == 1) {
930 cfg.channels = AUDREC_CMD_MODE_MONO;
931 } else if (cfg.channels == 2) {
932 cfg.channels = AUDREC_CMD_MODE_STEREO;
933 } else {
934 rc = -EINVAL;
935 break;
936 }
937 MM_DBG("channels = %d\n", cfg.channels);
938 audio->samp_rate = cfg.sample_rate;
939 audio->channel_mode = cfg.channels;
940 audio->bit_rate = cfg.bit_rate;
941 audio->record_quality = record_quality;
942 MM_DBG(" Record Quality = 0x%8x \n", audio->record_quality);
943 break;
944 }
945 case AUDIO_GET_SESSION_ID: {
946 if (copy_to_user((void *) arg, &audio->enc_id,
947 sizeof(unsigned short))) {
948 rc = -EFAULT;
949 }
950 break;
951 }
952 default:
953 rc = -EINVAL;
954 }
955 mutex_unlock(&audio->lock);
956 return rc;
957}
958
959static ssize_t audaac_in_read(struct file *file,
960 char __user *buf,
961 size_t count, loff_t *pos)
962{
963 struct audio_in *audio = file->private_data;
964 unsigned long flags;
965 const char __user *start = buf;
966 void *data;
967 uint32_t index;
968 uint32_t size;
969 int rc = 0;
970 struct aac_encoded_meta_in meta_field;
971 struct audio_frame_nt *nt_frame;
972 MM_DBG(" count = %d\n", count);
973 mutex_lock(&audio->read_lock);
974 while (count > 0) {
975 rc = wait_event_interruptible(
976 audio->wait, (audio->in_count > 0) || audio->stopped ||
977 audio->abort || audio->rflush);
978
979 if (rc < 0)
980 break;
981
982 if (audio->rflush) {
983 rc = -EBUSY;
984 break;
985 }
986
987 if (audio->stopped && !audio->in_count) {
988 MM_DBG("Driver in stop state, No more buffer to read");
989 rc = 0;/* End of File */
990 break;
991 }
992
993 if (audio->abort) {
994 rc = -EPERM; /* Not permitted due to abort */
995 break;
996 }
997
998 index = audio->in_tail;
999 data = (uint8_t *) audio->in[index].data;
1000 size = audio->in[index].size;
1001
1002 if (audio->mode == MSM_AUD_ENC_MODE_NONTUNNEL) {
1003 nt_frame = (struct audio_frame_nt *)(data -
1004 sizeof(struct audio_frame_nt));
1005 memcpy((char *)&meta_field.time_stamp_dword_lsw,
1006 (char *)&nt_frame->time_stamp_dword_lsw, 12);
1007 meta_field.metadata_len =
1008 sizeof(struct aac_encoded_meta_in);
1009 if (copy_to_user((char *)start, (char *)&meta_field,
1010 sizeof(struct aac_encoded_meta_in))) {
1011 rc = -EFAULT;
1012 break;
1013 }
1014 if (nt_frame->nflag_lsw & 0x0001) {
1015 MM_ERR("recieved EOS in read call\n");
1016 audio->eos_ack = 1;
1017 }
1018 buf += sizeof(struct aac_encoded_meta_in);
1019 count -= sizeof(struct aac_encoded_meta_in);
1020 }
1021 if (count >= size) {
1022 if (copy_to_user(buf, data, size)) {
1023 rc = -EFAULT;
1024 break;
1025 }
1026 spin_lock_irqsave(&audio->dsp_lock, flags);
1027 if (index != audio->in_tail) {
1028 /* overrun -- data is
1029 * invalid and we need to retry */
1030 spin_unlock_irqrestore(&audio->dsp_lock, flags);
1031 continue;
1032 }
1033 audio->in[index].size = 0;
1034 audio->in_tail = (audio->in_tail + 1) & (FRAME_NUM - 1);
1035 audio->in_count--;
1036 spin_unlock_irqrestore(&audio->dsp_lock, flags);
1037 count -= size;
1038 buf += size;
1039 if ((audio->mode == MSM_AUD_ENC_MODE_NONTUNNEL) &&
1040 (!audio->eos_ack)) {
1041 MM_DBG("sending read ptr command %d %d\n",
1042 audio->dsp_cnt,
1043 audio->in_tail);
1044 audaac_dsp_read_buffer(audio,
1045 audio->dsp_cnt++);
1046 break;
1047 }
1048 } else {
1049 MM_ERR("short read\n");
1050 break;
1051 }
1052 break;
1053 }
1054 mutex_unlock(&audio->read_lock);
1055 if (buf > start)
1056 return buf - start;
1057
1058 return rc;
1059}
1060
1061static void audpreproc_pcm_send_data(struct audio_in *audio, unsigned needed)
1062{
1063 struct buffer *frame;
1064 unsigned long flags;
1065 MM_DBG("\n");
1066 spin_lock_irqsave(&audio->dsp_lock, flags);
1067 if (!audio->running)
1068 goto done;
1069
1070 if (needed && !audio->wflush) {
1071 /* We were called from the callback because the DSP
1072 * requested more data. Note that the DSP does want
1073 * more data, and if a buffer was in-flight, mark it
1074 * as available (since the DSP must now be done with
1075 * it).
1076 */
1077 audio->out_needed = 1;
1078 frame = audio->out + audio->out_tail;
1079 if (frame->used == 0xffffffff) {
1080 MM_DBG("frame %d free\n", audio->out_tail);
1081 frame->used = 0;
1082 audio->out_tail ^= 1;
1083 wake_up(&audio->write_wait);
1084 }
1085 }
1086
1087 if (audio->out_needed) {
1088 /* If the DSP currently wants data and we have a
1089 * buffer available, we will send it and reset
1090 * the needed flag. We'll mark the buffer as in-flight
1091 * so that it won't be recycled until the next buffer
1092 * is requested
1093 */
1094
1095 frame = audio->out + audio->out_tail;
1096 if (frame->used) {
1097 BUG_ON(frame->used == 0xffffffff);
1098 MM_DBG("frame %d busy\n", audio->out_tail);
1099 audpreproc_pcm_buffer_ptr_refresh(audio,
1100 audio->out_tail,
1101 frame->used);
1102 frame->used = 0xffffffff;
1103 audio->out_needed = 0;
1104 }
1105 }
1106 done:
1107 spin_unlock_irqrestore(&audio->dsp_lock, flags);
1108}
1109
1110
Steve Mucklef132c6c2012-06-06 18:30:57 -07001111static int audaac_in_fsync(struct file *file, loff_t ppos1, loff_t ppos2, int datasync)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001112
1113{
1114 struct audio_in *audio = file->private_data;
1115 int rc = 0;
1116
1117 MM_DBG("\n"); /* Macro prints the file name and function */
1118 if (!audio->running || (audio->mode == MSM_AUD_ENC_MODE_TUNNEL)) {
1119 rc = -EINVAL;
1120 goto done_nolock;
1121 }
1122
1123 mutex_lock(&audio->write_lock);
1124
1125 rc = wait_event_interruptible(audio->write_wait,
1126 audio->wflush);
1127 MM_DBG("waked on by some event audio->wflush = %d\n", audio->wflush);
1128
1129 if (rc < 0)
1130 goto done;
1131 else if (audio->wflush) {
1132 rc = -EBUSY;
1133 goto done;
1134 }
1135done:
1136 mutex_unlock(&audio->write_lock);
1137done_nolock:
1138 return rc;
1139
1140}
1141
1142 int audpreproc_aac_process_eos(struct audio_in *audio,
1143 const char __user *buf_start, unsigned short mfield_size)
1144{
1145 struct buffer *frame;
1146 int rc = 0;
1147
1148 frame = audio->out + audio->out_head;
1149
1150 rc = wait_event_interruptible(audio->write_wait,
1151 (audio->out_needed &&
1152 audio->out[0].used == 0 &&
1153 audio->out[1].used == 0)
1154 || (audio->stopped)
1155 || (audio->wflush));
1156
1157 if (rc < 0)
1158 goto done;
1159 if (audio->stopped || audio->wflush) {
1160 rc = -EBUSY;
1161 goto done;
1162 }
1163 if (copy_from_user(frame->data, buf_start, mfield_size)) {
1164 rc = -EFAULT;
1165 goto done;
1166 }
1167
1168 frame->mfield_sz = mfield_size;
1169 audio->out_head ^= 1;
1170 frame->used = mfield_size;
1171 MM_DBG("copying meta_out frame->used = %d\n", frame->used);
1172 audpreproc_pcm_send_data(audio, 0);
1173done:
1174 return rc;
1175}
1176
1177static ssize_t audaac_in_write(struct file *file,
1178 const char __user *buf,
1179 size_t count, loff_t *pos)
1180{
1181 struct audio_in *audio = file->private_data;
1182 const char __user *start = buf;
1183 struct buffer *frame;
1184 char *cpy_ptr;
1185 int rc = 0, eos_condition = AUDPREPROC_AAC_EOS_NONE;
1186 unsigned short mfield_size = 0;
1187 int write_count = count;
1188 MM_DBG("cnt=%d\n", count);
1189
1190 if (count & 1)
1191 return -EINVAL;
1192
1193 mutex_lock(&audio->write_lock);
1194 frame = audio->out + audio->out_head;
1195 cpy_ptr = frame->data;
1196 rc = wait_event_interruptible(audio->write_wait,
1197 (frame->used == 0)
1198 || (audio->stopped)
1199 || (audio->wflush));
1200 if (rc < 0)
1201 goto error;
1202
1203 if (audio->stopped || audio->wflush) {
1204 rc = -EBUSY;
1205 goto error;
1206 }
1207 if (audio->mfield) {
1208 if (buf == start) {
1209 /* Processing beginning of user buffer */
1210 if (__get_user(mfield_size,
1211 (unsigned short __user *) buf)) {
1212 rc = -EFAULT;
1213 goto error;
1214 } else if (mfield_size > count) {
1215 rc = -EINVAL;
1216 goto error;
1217 }
1218 MM_DBG("mf offset_val %x\n", mfield_size);
1219 if (copy_from_user(cpy_ptr, buf, mfield_size)) {
1220 rc = -EFAULT;
1221 goto error;
1222 }
1223 /* Check if EOS flag is set and buffer has
1224 * contains just meta field
1225 */
1226 if (cpy_ptr[AUDPREPROC_AAC_EOS_FLG_OFFSET] &
1227 AUDPREPROC_AAC_EOS_FLG_MASK) {
1228 MM_DBG("EOS SET\n");
1229 eos_condition = AUDPREPROC_AAC_EOS_SET;
1230 if (mfield_size == count) {
1231 buf += mfield_size;
1232 if (audio->mode ==
1233 MSM_AUD_ENC_MODE_NONTUNNEL) {
1234 eos_condition = 0;
1235 goto exit;
1236 }
1237 goto error;
1238 } else
1239 cpy_ptr[AUDPREPROC_AAC_EOS_FLG_OFFSET] &=
1240 ~AUDPREPROC_AAC_EOS_FLG_MASK;
1241 }
1242 cpy_ptr += mfield_size;
1243 count -= mfield_size;
1244 buf += mfield_size;
1245 } else {
1246 mfield_size = 0;
1247 MM_DBG("continuous buffer\n");
1248 }
1249 frame->mfield_sz = mfield_size;
1250 }
1251 MM_DBG("copying the stream count = %d\n", count);
1252 if (copy_from_user(cpy_ptr, buf, count)) {
1253 rc = -EFAULT;
1254 goto error;
1255 }
1256exit:
1257 frame->used = count;
1258 audio->out_head ^= 1;
1259 if (!audio->flush_ack)
1260 audpreproc_pcm_send_data(audio, 0);
1261 else {
1262 audpreproc_pcm_send_data(audio, 1);
1263 audio->flush_ack = 0;
1264 }
1265 if (eos_condition == AUDPREPROC_AAC_EOS_SET)
1266 rc = audpreproc_aac_process_eos(audio, start, mfield_size);
1267 mutex_unlock(&audio->write_lock);
1268 return write_count;
1269error:
1270 mutex_unlock(&audio->write_lock);
1271 return rc;
1272}
1273
1274static int audaac_in_release(struct inode *inode, struct file *file)
1275{
1276 struct audio_in *audio = file->private_data;
1277
1278 mutex_lock(&audio->lock);
1279 /* with draw frequency for session
1280 incase not stopped the driver */
1281 msm_snddev_withdraw_freq(audio->enc_id, SNDDEV_CAP_TX,
1282 AUDDEV_CLNT_ENC);
1283 auddev_unregister_evt_listner(AUDDEV_CLNT_ENC, audio->enc_id);
1284 /*reset the sampling frequency information at audpreproc layer*/
1285 audio->session_info.sampling_freq = 0;
1286 audpreproc_update_audrec_info(&audio->session_info);
1287 audaac_in_disable(audio);
1288 audaac_in_flush(audio);
1289 msm_adsp_put(audio->audrec);
1290 audpreproc_aenc_free(audio->enc_id);
1291 audio->audrec = NULL;
1292 audio->opened = 0;
1293 if (audio->data) {
Laura Abbott61399692012-04-30 14:25:46 -07001294 iounmap(audio->map_v_read);
Santosh Mardifdc227a2011-07-11 17:20:34 +05301295 free_contiguous_memory_by_paddr(audio->phys);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001296 audio->data = NULL;
1297 }
1298 if (audio->out_data) {
Laura Abbott61399692012-04-30 14:25:46 -07001299 iounmap(audio->map_v_write);
Santosh Mardifdc227a2011-07-11 17:20:34 +05301300 free_contiguous_memory_by_paddr(audio->out_phys);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001301 audio->out_data = NULL;
1302 }
1303 mutex_unlock(&audio->lock);
1304 return 0;
1305}
1306
1307struct audio_in the_audio_aac_in;
1308
1309static int audaac_in_open(struct inode *inode, struct file *file)
1310{
1311 struct audio_in *audio = &the_audio_aac_in;
1312 int rc;
1313 int encid;
1314
1315 mutex_lock(&audio->lock);
1316 if (audio->opened) {
1317 rc = -EBUSY;
1318 goto done;
1319 }
Santosh Mardifdc227a2011-07-11 17:20:34 +05301320 audio->phys = allocate_contiguous_ebi_nomap(DMASZ, SZ_4K);
1321 if (audio->phys) {
Laura Abbott61399692012-04-30 14:25:46 -07001322 audio->map_v_read = ioremap(audio->phys, DMASZ);
Santosh Mardifdc227a2011-07-11 17:20:34 +05301323 if (IS_ERR(audio->map_v_read)) {
1324 MM_ERR("could not map DMA buffers\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001325 rc = -ENOMEM;
Santosh Mardifdc227a2011-07-11 17:20:34 +05301326 free_contiguous_memory_by_paddr(audio->phys);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001327 goto done;
1328 }
Laura Abbott61399692012-04-30 14:25:46 -07001329 audio->data = audio->map_v_read;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001330 } else {
1331 MM_ERR("could not allocate DMA buffers\n");
1332 rc = -ENOMEM;
1333 goto done;
1334 }
1335 MM_DBG("Memory addr = 0x%8x phy addr = 0x%8x\n",\
1336 (int) audio->data, (int) audio->phys);
1337 if ((file->f_mode & FMODE_WRITE) &&
1338 (file->f_mode & FMODE_READ)) {
1339 audio->mode = MSM_AUD_ENC_MODE_NONTUNNEL;
1340 } else if (!(file->f_mode & FMODE_WRITE) &&
1341 (file->f_mode & FMODE_READ)) {
1342 audio->mode = MSM_AUD_ENC_MODE_TUNNEL;
1343 MM_DBG("Opened for tunnel mode encoding\n");
1344 } else {
1345 rc = -EACCES;
1346 goto done;
1347 }
1348
1349 /* Settings will be re-config at AUDIO_SET_CONFIG,
1350 * but at least we need to have initial config
1351 */
1352 if (audio->mode == MSM_AUD_ENC_MODE_NONTUNNEL)
1353 audio->buffer_size = (NT_FRAME_SIZE - 24);
1354 else
1355 audio->buffer_size = (FRAME_SIZE - 8);
1356 audio->enc_type = ENC_TYPE_AAC | audio->mode;
1357 audio->samp_rate = 8000;
1358 audio->channel_mode = AUDREC_CMD_MODE_MONO;
1359 /* For AAC, bit rate hard coded, default settings is
1360 * sample rate (8000) x channel count (1) x recording quality (1.75)
1361 * = 14000 bps */
1362 audio->bit_rate = 14000;
1363 audio->record_quality = 0x1c00;
1364 MM_DBG("enc_type = %x\n", audio->enc_type);
1365 encid = audpreproc_aenc_alloc(audio->enc_type, &audio->module_name,
1366 &audio->queue_ids);
1367 if (encid < 0) {
1368 MM_ERR("No free encoder available\n");
1369 rc = -ENODEV;
1370 goto done;
1371 }
1372 audio->enc_id = encid;
1373
1374 rc = msm_adsp_get(audio->module_name, &audio->audrec,
1375 &audrec_aac_adsp_ops, audio);
1376
1377 if (rc) {
1378 audpreproc_aenc_free(audio->enc_id);
1379 goto done;
1380 }
1381
1382 audio->stopped = 0;
1383 audio->source = 0;
1384 audio->abort = 0;
1385 audio->wflush = 0;
1386 audio->rflush = 0;
1387 audio->flush_ack = 0;
1388
1389 audaac_in_flush(audio);
1390 audaac_out_flush(audio);
1391
Santosh Mardifdc227a2011-07-11 17:20:34 +05301392 audio->out_phys = allocate_contiguous_ebi_nomap(BUFFER_SIZE, SZ_4K);
1393 if (!audio->out_phys) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001394 MM_ERR("could not allocate write buffers\n");
1395 rc = -ENOMEM;
1396 goto evt_error;
1397 } else {
Laura Abbott61399692012-04-30 14:25:46 -07001398 audio->map_v_write = ioremap(
1399 audio->out_phys, BUFFER_SIZE);
Santosh Mardifdc227a2011-07-11 17:20:34 +05301400 if (IS_ERR(audio->map_v_write)) {
1401 MM_ERR("could not map write phys address\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001402 rc = -ENOMEM;
Santosh Mardifdc227a2011-07-11 17:20:34 +05301403 free_contiguous_memory_by_paddr(audio->out_phys);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001404 goto evt_error;
1405 }
Laura Abbott61399692012-04-30 14:25:46 -07001406 audio->out_data = audio->map_v_write;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001407 MM_DBG("write buf: phy addr 0x%08x kernel addr 0x%08x\n",
1408 audio->out_phys, (int)audio->out_data);
1409 }
Kalyani polepeddy7413d2b2011-10-14 15:42:15 +05301410 audio->build_id = socinfo_get_build_id();
Vinay Vaka4ff52a62011-12-13 15:38:35 +05301411 MM_DBG("Modem build id = %s\n", audio->build_id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001412
1413 /* Initialize buffer */
1414 audio->out[0].data = audio->out_data + 0;
1415 audio->out[0].addr = audio->out_phys + 0;
1416 audio->out[0].size = OUT_BUFFER_SIZE;
1417
1418 audio->out[1].data = audio->out_data + OUT_BUFFER_SIZE;
1419 audio->out[1].addr = audio->out_phys + OUT_BUFFER_SIZE;
1420 audio->out[1].size = OUT_BUFFER_SIZE;
1421
1422 MM_DBG("audio->out[0].data = %d audio->out[1].data = %d",
1423 (unsigned int)audio->out[0].data,
1424 (unsigned int)audio->out[1].data);
1425 audio->device_events = AUDDEV_EVT_DEV_RDY | AUDDEV_EVT_DEV_RLS |
1426 AUDDEV_EVT_FREQ_CHG;
1427
1428 rc = auddev_register_evt_listner(audio->device_events,
1429 AUDDEV_CLNT_ENC, audio->enc_id,
1430 aac_in_listener, (void *) audio);
1431 if (rc) {
1432 MM_ERR("failed to register device event listener\n");
Laura Abbott61399692012-04-30 14:25:46 -07001433 iounmap(audio->map_v_write);
Santosh Mardifdc227a2011-07-11 17:20:34 +05301434 free_contiguous_memory_by_paddr(audio->out_phys);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001435 goto evt_error;
1436 }
1437 audio->mfield = META_OUT_SIZE;
1438 file->private_data = audio;
1439 audio->opened = 1;
1440 audio->out_frame_cnt++;
1441 rc = 0;
1442done:
1443 mutex_unlock(&audio->lock);
1444 return rc;
1445evt_error:
1446 msm_adsp_put(audio->audrec);
1447 audpreproc_aenc_free(audio->enc_id);
1448 mutex_unlock(&audio->lock);
1449 return rc;
1450}
1451
1452static const struct file_operations audio_in_fops = {
1453 .owner = THIS_MODULE,
1454 .open = audaac_in_open,
1455 .release = audaac_in_release,
1456 .read = audaac_in_read,
1457 .write = audaac_in_write,
1458 .fsync = audaac_in_fsync,
1459 .unlocked_ioctl = audaac_in_ioctl,
1460};
1461
1462struct miscdevice audio_aac_in_misc = {
1463 .minor = MISC_DYNAMIC_MINOR,
1464 .name = "msm_aac_in",
1465 .fops = &audio_in_fops,
1466};
1467
1468static int __init audaac_in_init(void)
1469{
1470 mutex_init(&the_audio_aac_in.lock);
1471 mutex_init(&the_audio_aac_in.read_lock);
1472 spin_lock_init(&the_audio_aac_in.dsp_lock);
1473 spin_lock_init(&the_audio_aac_in.dev_lock);
1474 init_waitqueue_head(&the_audio_aac_in.wait);
1475 init_waitqueue_head(&the_audio_aac_in.wait_enable);
1476 mutex_init(&the_audio_aac_in.write_lock);
1477 init_waitqueue_head(&the_audio_aac_in.write_wait);
1478
1479 return misc_register(&audio_aac_in_misc);
1480}
1481
1482device_initcall(audaac_in_init);