blob: 5e959b536b28ea3ecd71b19437db1f8d88dc31f0 [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/*
Duy Truong790f06d2013-02-13 16:38:12 -08002 * Copyright (c) 2010-2012, The Linux Foundation. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 */
14#include <linux/module.h>
15#include <linux/fs.h>
16#include <linux/miscdevice.h>
17#include <linux/uaccess.h>
18#include <linux/sched.h>
19#include <linux/wait.h>
20#include <linux/dma-mapping.h>
21#include <linux/slab.h>
22#include <linux/msm_audio_aac.h>
23#include <asm/atomic.h>
24#include <asm/ioctls.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070025#include "audio_utils.h"
26
27
28/* Buffer with meta*/
29#define PCM_BUF_SIZE (4096 + sizeof(struct meta_in))
30
31/* Maximum 5 frames in buffer with meta */
32#define FRAME_SIZE (1 + ((1536+sizeof(struct meta_out_dsp)) * 5))
33
34#define AAC_FORMAT_ADTS 65535
35
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070036/* ------------------- device --------------------- */
37static long aac_in_ioctl(struct file *file,
38 unsigned int cmd, unsigned long arg)
39{
40 struct q6audio_in *audio = file->private_data;
41 int rc = 0;
42 int cnt = 0;
43
44 switch (cmd) {
45 case AUDIO_START: {
46 struct msm_audio_aac_enc_config *enc_cfg;
47 struct msm_audio_aac_config *aac_config;
48 uint32_t aac_mode = AAC_ENC_MODE_AAC_LC;
49
50 enc_cfg = audio->enc_cfg;
51 aac_config = audio->codec_cfg;
52 /* ENCODE CFG (after new set of API's are published )bharath*/
53 pr_debug("%s:session id %d: default buf alloc[%d]\n", __func__,
54 audio->ac->session, audio->buf_alloc);
55 if (audio->enabled == 1) {
56 pr_info("%s:AUDIO_START already over\n", __func__);
57 rc = 0;
58 break;
59 }
60
61 rc = audio_in_buf_alloc(audio);
62 if (rc < 0) {
63 pr_err("%s:session id %d: buffer allocation failed\n",
64 __func__, audio->ac->session);
65 break;
66 }
67
68 pr_debug("%s:sbr_ps_flag = %d, sbr_flag = %d\n", __func__,
69 aac_config->sbr_ps_on_flag, aac_config->sbr_on_flag);
70 if (aac_config->sbr_ps_on_flag)
71 aac_mode = AAC_ENC_MODE_EAAC_P;
72 else if (aac_config->sbr_on_flag)
73 aac_mode = AAC_ENC_MODE_AAC_P;
74 else
75 aac_mode = AAC_ENC_MODE_AAC_LC;
76
77 rc = q6asm_enc_cfg_blk_aac(audio->ac,
78 audio->buf_cfg.frames_per_buf,
79 enc_cfg->sample_rate,
80 enc_cfg->channels,
81 enc_cfg->bit_rate,
82 aac_mode,
83 enc_cfg->stream_format);
84 if (rc < 0) {
Harmandeep Singhc35fa07d2012-05-31 07:08:59 -070085 pr_err("%s:session id %d: cmd media format block"
86 "failed\n", __func__, audio->ac->session);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070087 break;
88 }
89 if (audio->feedback == NON_TUNNEL_MODE) {
90 rc = q6asm_media_format_block_pcm(audio->ac,
91 audio->pcm_cfg.sample_rate,
92 audio->pcm_cfg.channel_count);
93 if (rc < 0) {
Harmandeep Singhc35fa07d2012-05-31 07:08:59 -070094 pr_err("%s:session id %d: media format block"
95 "failed\n", __func__, audio->ac->session);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070096 break;
97 }
98 }
99 rc = audio_in_enable(audio);
100 if (!rc) {
101 audio->enabled = 1;
102 } else {
103 audio->enabled = 0;
Harmandeep Singhc35fa07d2012-05-31 07:08:59 -0700104 pr_err("%s:session id %d: Audio Start procedure"
105 "failed rc=%d\n", __func__, audio->ac->session, rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700106 break;
107 }
108 while (cnt++ < audio->str_cfg.buffer_count)
109 q6asm_read(audio->ac);
110 pr_debug("%s:session id %d: AUDIO_START success enable[%d]\n",
111 __func__, audio->ac->session, audio->enabled);
112 break;
113 }
114 case AUDIO_STOP: {
115 pr_debug("%s:session id %d: Rxed AUDIO_STOP\n", __func__,
116 audio->ac->session);
117 rc = audio_in_disable(audio);
118 if (rc < 0) {
Harmandeep Singhc35fa07d2012-05-31 07:08:59 -0700119 pr_err("%s:session id %d: Audio Stop procedure failed"
120 "rc=%d\n", __func__, audio->ac->session, rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700121 break;
122 }
123 break;
124 }
125 case AUDIO_GET_AAC_ENC_CONFIG: {
126 struct msm_audio_aac_enc_config cfg;
127 struct msm_audio_aac_enc_config *enc_cfg;
128 enc_cfg = audio->enc_cfg;
129 if (enc_cfg->channels == CH_MODE_MONO)
130 cfg.channels = 1;
131 else
132 cfg.channels = 2;
133 cfg.sample_rate = enc_cfg->sample_rate;
134 cfg.bit_rate = enc_cfg->bit_rate;
135 /* ADTS(-1) to ADTS(0x00), RAW(0x00) to RAW(0x03) */
136 cfg.stream_format = ((enc_cfg->stream_format == \
137 0x00) ? AUDIO_AAC_FORMAT_ADTS : AUDIO_AAC_FORMAT_RAW);
Harmandeep Singhc35fa07d2012-05-31 07:08:59 -0700138 pr_debug("%s:session id %d: Get-aac-cfg: format=%d sr=%d"
139 "bitrate=%d\n", __func__, audio->ac->session,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700140 cfg.stream_format, cfg.sample_rate, cfg.bit_rate);
141 if (copy_to_user((void *)arg, &cfg, sizeof(cfg)))
142 rc = -EFAULT;
143 break;
144 }
145 case AUDIO_SET_AAC_ENC_CONFIG: {
146 struct msm_audio_aac_enc_config cfg;
147 struct msm_audio_aac_enc_config *enc_cfg;
148 enc_cfg = audio->enc_cfg;
149 if (copy_from_user(&cfg, (void *)arg, sizeof(cfg))) {
150 rc = -EFAULT;
151 break;
152 }
153 pr_debug("%s:session id %d: Set-aac-cfg: stream=%d\n", __func__,
154 audio->ac->session, cfg.stream_format);
155
156 if ((cfg.stream_format != AUDIO_AAC_FORMAT_RAW) &&
157 (cfg.stream_format != AAC_FORMAT_ADTS)) {
158 pr_err("%s:session id %d: unsupported AAC format\n",
159 __func__, audio->ac->session);
160 rc = -EINVAL;
161 break;
162 }
163
164 if (cfg.channels == 1) {
165 cfg.channels = CH_MODE_MONO;
166 } else if (cfg.channels == 2) {
167 cfg.channels = CH_MODE_STEREO;
168 } else {
169 rc = -EINVAL;
170 break;
171 }
172 if ((cfg.sample_rate < 8000) && (cfg.sample_rate > 48000)) {
173 pr_err("%s: ERROR in setting samplerate = %d\n",
174 __func__, cfg.sample_rate);
175 rc = -EINVAL;
176 break;
177 }
Bharath Ramachandramurthy1ba3c0f2011-12-14 18:17:54 -0800178 /* For aac-lc, min_bit_rate = min(24Kbps, 0.5*SR*num_chan);
179 max_bi_rate = min(192Kbps, 6*SR*num_chan);
180 min_sample_rate = 8000Hz, max_rate=48000 */
181 if ((cfg.bit_rate < 4000) || (cfg.bit_rate > 192000)) {
Laxminath Kasam898cbdd2011-12-07 12:42:12 +0530182 pr_err("%s: ERROR in setting bitrate = %d\n",
183 __func__, cfg.bit_rate);
184 rc = -EINVAL;
185 break;
186 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700187 enc_cfg->sample_rate = cfg.sample_rate;
188 enc_cfg->channels = cfg.channels;
189 enc_cfg->bit_rate = cfg.bit_rate;
190 enc_cfg->stream_format =
191 ((cfg.stream_format == AUDIO_AAC_FORMAT_RAW) ? \
192 0x03 : 0x00);
Harmandeep Singhc35fa07d2012-05-31 07:08:59 -0700193 pr_debug("%s:session id %d: Set-aac-cfg:SR= 0x%x ch=0x%x"
194 "bitrate=0x%x, format(adts/raw) = %d\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700195 __func__, audio->ac->session, enc_cfg->sample_rate,
196 enc_cfg->channels, enc_cfg->bit_rate,
197 enc_cfg->stream_format);
198 break;
199 }
200 case AUDIO_GET_AAC_CONFIG: {
201 if (copy_to_user((void *)arg, &audio->codec_cfg,
202 sizeof(struct msm_audio_aac_config))) {
203 rc = -EFAULT;
204 break;
205 }
206 break;
207 }
208 case AUDIO_SET_AAC_CONFIG: {
209 struct msm_audio_aac_config aac_cfg;
210 struct msm_audio_aac_config *audio_aac_cfg;
211 struct msm_audio_aac_enc_config *enc_cfg;
212 enc_cfg = audio->enc_cfg;
213 audio_aac_cfg = audio->codec_cfg;
214
215 if (copy_from_user(&aac_cfg, (void *)arg,
216 sizeof(struct msm_audio_aac_config))) {
217 rc = -EFAULT;
218 break;
219 }
220 pr_debug("%s:session id %d: AUDIO_SET_AAC_CONFIG: sbr_flag = %d"
221 " sbr_ps_flag = %d\n", __func__,
222 audio->ac->session, aac_cfg.sbr_on_flag,
223 aac_cfg.sbr_ps_on_flag);
224 audio_aac_cfg->sbr_on_flag = aac_cfg.sbr_on_flag;
225 audio_aac_cfg->sbr_ps_on_flag = aac_cfg.sbr_ps_on_flag;
226 if ((audio_aac_cfg->sbr_on_flag == 1) ||
227 (audio_aac_cfg->sbr_ps_on_flag == 1)) {
228 if (enc_cfg->sample_rate < 24000) {
229 pr_err("%s: ERROR in setting samplerate = %d"
230 "\n", __func__, enc_cfg->sample_rate);
231 rc = -EINVAL;
232 break;
233 }
234 }
235 break;
236 }
237 default:
238 rc = -EINVAL;
239 }
240 return rc;
241}
242
243static int aac_in_open(struct inode *inode, struct file *file)
244{
245 struct q6audio_in *audio = NULL;
246 struct msm_audio_aac_enc_config *enc_cfg;
247 struct msm_audio_aac_config *aac_config;
248 int rc = 0;
249
250 audio = kzalloc(sizeof(struct q6audio_in), GFP_KERNEL);
251
252 if (audio == NULL) {
Harmandeep Singhc35fa07d2012-05-31 07:08:59 -0700253 pr_err("%s: Could not allocate memory for aac"
254 "driver\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700255 return -ENOMEM;
256 }
257 /* Allocate memory for encoder config param */
258 audio->enc_cfg = kzalloc(sizeof(struct msm_audio_aac_enc_config),
259 GFP_KERNEL);
260 if (audio->enc_cfg == NULL) {
Harmandeep Singhc35fa07d2012-05-31 07:08:59 -0700261 pr_err("%s:session id %d: Could not allocate memory for aac"
262 "config param\n", __func__, audio->ac->session);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700263 kfree(audio);
264 return -ENOMEM;
265 }
266 enc_cfg = audio->enc_cfg;
267
268 audio->codec_cfg = kzalloc(sizeof(struct msm_audio_aac_config),
269 GFP_KERNEL);
270 if (audio->codec_cfg == NULL) {
Harmandeep Singhc35fa07d2012-05-31 07:08:59 -0700271 pr_err("%s:session id %d: Could not allocate memory for aac"
272 "config\n", __func__, audio->ac->session);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700273 kfree(audio->enc_cfg);
274 kfree(audio);
275 return -ENOMEM;
276 }
277 aac_config = audio->codec_cfg;
278
279 mutex_init(&audio->lock);
280 mutex_init(&audio->read_lock);
281 mutex_init(&audio->write_lock);
282 spin_lock_init(&audio->dsp_lock);
283 init_waitqueue_head(&audio->read_wait);
284 init_waitqueue_head(&audio->write_wait);
285
286 /* Settings will be re-config at AUDIO_SET_CONFIG,
287 * but at least we need to have initial config
288 */
289 audio->str_cfg.buffer_size = FRAME_SIZE;
290 audio->str_cfg.buffer_count = FRAME_NUM;
291 audio->min_frame_size = 1536;
292 audio->max_frames_per_buf = 5;
293 enc_cfg->sample_rate = 8000;
294 enc_cfg->channels = 1;
295 enc_cfg->bit_rate = 16000;
296 enc_cfg->stream_format = 0x00;/* 0:ADTS, 3:RAW */
297 audio->buf_cfg.meta_info_enable = 0x01;
298 audio->buf_cfg.frames_per_buf = 0x01;
299 audio->pcm_cfg.buffer_count = PCM_BUF_COUNT;
300 audio->pcm_cfg.buffer_size = PCM_BUF_SIZE;
301 aac_config->format = AUDIO_AAC_FORMAT_ADTS;
302 aac_config->audio_object = AUDIO_AAC_OBJECT_LC;
303 aac_config->sbr_on_flag = 0;
304 aac_config->sbr_ps_on_flag = 0;
305 aac_config->channel_configuration = 1;
306
Harmandeep Singhc35fa07d2012-05-31 07:08:59 -0700307 audio->ac = q6asm_audio_client_alloc((app_cb)q6asm_in_cb,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700308 (void *)audio);
309
310 if (!audio->ac) {
Harmandeep Singhc35fa07d2012-05-31 07:08:59 -0700311 pr_err("%s: Could not allocate memory for"
312 "audio client\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700313 kfree(audio->enc_cfg);
314 kfree(audio->codec_cfg);
315 kfree(audio);
316 return -ENOMEM;
317 }
318 /* open aac encoder in tunnel mode */
319 audio->buf_cfg.frames_per_buf = 0x01;
320
321 if ((file->f_mode & FMODE_WRITE) &&
322 (file->f_mode & FMODE_READ)) {
323 audio->feedback = NON_TUNNEL_MODE;
324 rc = q6asm_open_read_write(audio->ac, FORMAT_MPEG4_AAC,
325 FORMAT_LINEAR_PCM);
326
327 if (rc < 0) {
328 pr_err("%s:session id %d: NT Open failed rc=%d\n",
329 __func__, audio->ac->session, rc);
330 rc = -ENODEV;
331 goto fail;
332 }
333 audio->buf_cfg.meta_info_enable = 0x01;
334 pr_info("%s:session id %d: NT mode encoder success\n", __func__,
335 audio->ac->session);
336 } else if (!(file->f_mode & FMODE_WRITE) &&
337 (file->f_mode & FMODE_READ)) {
338 audio->feedback = TUNNEL_MODE;
339 rc = q6asm_open_read(audio->ac, FORMAT_MPEG4_AAC);
340
341 if (rc < 0) {
342 pr_err("%s:session id %d: Tunnel Open failed rc=%d\n",
343 __func__, audio->ac->session, rc);
344 rc = -ENODEV;
345 goto fail;
346 }
347 /* register for tx overflow (valid for tunnel mode only) */
348 rc = q6asm_reg_tx_overflow(audio->ac, 0x01);
349 if (rc < 0) {
Harmandeep Singhc35fa07d2012-05-31 07:08:59 -0700350 pr_err("%s:session id %d: TX Overflow registration"
351 "failed rc=%d\n", __func__,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700352 audio->ac->session, rc);
353 rc = -ENODEV;
354 goto fail;
355 }
356 audio->buf_cfg.meta_info_enable = 0x00;
357 pr_info("%s:session id %d: T mode encoder success\n", __func__,
358 audio->ac->session);
359 } else {
360 pr_err("%s:session id %d: Unexpected mode\n", __func__,
361 audio->ac->session);
362 rc = -EACCES;
363 goto fail;
364 }
365 audio->opened = 1;
366 atomic_set(&audio->in_count, PCM_BUF_COUNT);
367 atomic_set(&audio->out_count, 0x00);
368 audio->enc_ioctl = aac_in_ioctl;
369 file->private_data = audio;
370
371 pr_info("%s:session id %d: success\n", __func__, audio->ac->session);
372 return 0;
373fail:
374 q6asm_audio_client_free(audio->ac);
375 kfree(audio->enc_cfg);
376 kfree(audio->codec_cfg);
377 kfree(audio);
378 return rc;
379}
380
381static const struct file_operations audio_in_fops = {
382 .owner = THIS_MODULE,
383 .open = aac_in_open,
384 .release = audio_in_release,
385 .read = audio_in_read,
386 .write = audio_in_write,
387 .unlocked_ioctl = audio_in_ioctl,
388};
389
390struct miscdevice audio_aac_in_misc = {
391 .minor = MISC_DYNAMIC_MINOR,
392 .name = "msm_aac_in",
393 .fops = &audio_in_fops,
394};
395
396static int __init aac_in_init(void)
397{
398 return misc_register(&audio_aac_in_misc);
399}
400device_initcall(aac_in_init);