blob: 3a5411ed2625a188480b4c83d6dc1e9c5f1889ae [file] [log] [blame]
Phani Kumar Uppalapati4e3c5db2013-01-08 10:54:14 -08001/* Copyright (c) 2010-2013, The Linux Foundation. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
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_qcp.h>
Phani Kumar Uppalapati4e3c5db2013-01-08 10:54:14 -080023#include <linux/atomic.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070024#include <asm/ioctls.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070025#include "audio_utils.h"
26
27/* Buffer with meta*/
28#define PCM_BUF_SIZE (4096 + sizeof(struct meta_in))
29
30/* Maximum 10 frames in buffer with meta */
31#define FRAME_SIZE (1 + ((35+sizeof(struct meta_out_dsp)) * 10))
32
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070033/* ------------------- device --------------------- */
34static long qcelp_in_ioctl(struct file *file,
35 unsigned int cmd, unsigned long arg)
36{
37 struct q6audio_in *audio = file->private_data;
38 int rc = 0;
39 int cnt = 0;
40
41 switch (cmd) {
42 case AUDIO_START: {
43 struct msm_audio_qcelp_enc_config *enc_cfg;
44 enc_cfg = audio->enc_cfg;
45 pr_debug("%s:session id %d: default buf alloc[%d]\n", __func__,
46 audio->ac->session, audio->buf_alloc);
47 if (audio->enabled == 1) {
48 pr_info("%s:AUDIO_START already over\n", __func__);
49 rc = 0;
50 break;
51 }
52 rc = audio_in_buf_alloc(audio);
53 if (rc < 0) {
54 pr_err("%s:session id %d: buffer allocation failed\n",
55 __func__, audio->ac->session);
56 break;
57 }
58
59 /* reduced_rate_level, rate_modulation_cmd set to zero
60 currently not configurable from user space */
61 rc = q6asm_enc_cfg_blk_qcelp(audio->ac,
62 audio->buf_cfg.frames_per_buf,
63 enc_cfg->min_bit_rate,
64 enc_cfg->max_bit_rate, 0, 0);
65
66 if (rc < 0) {
Phani Kumar Uppalapati4e3c5db2013-01-08 10:54:14 -080067 pr_err("%s:session id %d: cmd qcelp media format block failed\n",
68 __func__, audio->ac->session);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070069 break;
70 }
71 if (audio->feedback == NON_TUNNEL_MODE) {
72 rc = q6asm_media_format_block_pcm(audio->ac,
73 audio->pcm_cfg.sample_rate,
74 audio->pcm_cfg.channel_count);
75
76 if (rc < 0) {
Phani Kumar Uppalapati4e3c5db2013-01-08 10:54:14 -080077 pr_err("%s:session id %d: media format block failed\n",
78 __func__, audio->ac->session);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070079 break;
80 }
81 }
82 pr_debug("%s:session id %d: AUDIO_START enable[%d]\n", __func__,
83 audio->ac->session, audio->enabled);
84 rc = audio_in_enable(audio);
85 if (!rc) {
86 audio->enabled = 1;
87 } else {
88 audio->enabled = 0;
Phani Kumar Uppalapati4e3c5db2013-01-08 10:54:14 -080089 pr_err("%s:session id %d: Audio Start procedure failed rc=%d\n",
90 __func__, audio->ac->session, rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070091 break;
92 }
93 while (cnt++ < audio->str_cfg.buffer_count)
94 q6asm_read(audio->ac); /* Push buffer to DSP */
95 rc = 0;
96 pr_debug("%s:session id %d: AUDIO_START success enable[%d]\n",
97 __func__, audio->ac->session, audio->enabled);
98 break;
99 }
100 case AUDIO_STOP: {
101 pr_debug("%s:session id %d: AUDIO_STOP\n", __func__,
102 audio->ac->session);
103 rc = audio_in_disable(audio);
104 if (rc < 0) {
Phani Kumar Uppalapati4e3c5db2013-01-08 10:54:14 -0800105 pr_err("%s:session id %d: Audio Stop procedure failed rc=%d\n",
106 __func__, audio->ac->session,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700107 rc);
108 break;
109 }
110 break;
111 }
112 case AUDIO_GET_QCELP_ENC_CONFIG: {
113 if (copy_to_user((void *)arg, audio->enc_cfg,
114 sizeof(struct msm_audio_qcelp_enc_config)))
115 rc = -EFAULT;
116 break;
117 }
118 case AUDIO_SET_QCELP_ENC_CONFIG: {
119 struct msm_audio_qcelp_enc_config cfg;
120 struct msm_audio_qcelp_enc_config *enc_cfg;
121 enc_cfg = audio->enc_cfg;
122 if (copy_from_user(&cfg, (void *) arg,
123 sizeof(struct msm_audio_qcelp_enc_config))) {
124 rc = -EFAULT;
125 break;
126 }
127
128 if (cfg.min_bit_rate > 4 ||
129 cfg.min_bit_rate < 1) {
130 pr_err("%s:session id %d: invalid min bitrate\n",
131 __func__, audio->ac->session);
132 rc = -EINVAL;
133 break;
134 }
135 if (cfg.max_bit_rate > 4 ||
136 cfg.max_bit_rate < 1) {
137 pr_err("%s:session id %d: invalid max bitrate\n",
138 __func__, audio->ac->session);
139 rc = -EINVAL;
140 break;
141 }
142 enc_cfg->min_bit_rate = cfg.min_bit_rate;
143 enc_cfg->max_bit_rate = cfg.max_bit_rate;
Phani Kumar Uppalapati4e3c5db2013-01-08 10:54:14 -0800144 pr_debug("%s:session id %d: min_bit_rate= 0x%x max_bit_rate=0x%x\n",
145 __func__,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700146 audio->ac->session, enc_cfg->min_bit_rate,
147 enc_cfg->max_bit_rate);
148 break;
149 }
150 default:
151 rc = -EINVAL;
152 }
153 return rc;
154}
155
156static int qcelp_in_open(struct inode *inode, struct file *file)
157{
158 struct q6audio_in *audio = NULL;
159 struct msm_audio_qcelp_enc_config *enc_cfg;
160 int rc = 0;
161
162 audio = kzalloc(sizeof(struct q6audio_in), GFP_KERNEL);
163
164 if (audio == NULL) {
Phani Kumar Uppalapati4e3c5db2013-01-08 10:54:14 -0800165 pr_err("%s: Could not allocate memory for qcelp driver\n",
166 __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700167 return -ENOMEM;
168 }
169 /* Allocate memory for encoder config param */
170 audio->enc_cfg = kzalloc(sizeof(struct msm_audio_qcelp_enc_config),
171 GFP_KERNEL);
172 if (audio->enc_cfg == NULL) {
Phani Kumar Uppalapati4e3c5db2013-01-08 10:54:14 -0800173 pr_err("%s:session id %d: Could not allocate memory for aac config param\n",
174 __func__, audio->ac->session);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700175 kfree(audio);
176 return -ENOMEM;
177 }
178 enc_cfg = audio->enc_cfg;
179
180 mutex_init(&audio->lock);
181 mutex_init(&audio->read_lock);
182 mutex_init(&audio->write_lock);
183 spin_lock_init(&audio->dsp_lock);
184 init_waitqueue_head(&audio->read_wait);
185 init_waitqueue_head(&audio->write_wait);
186
187 /* Settings will be re-config at AUDIO_SET_CONFIG,
188 * but at least we need to have initial config
189 */
190 audio->str_cfg.buffer_size = FRAME_SIZE;
191 audio->str_cfg.buffer_count = FRAME_NUM;
192 audio->min_frame_size = 35;
193 audio->max_frames_per_buf = 10;
194 audio->pcm_cfg.buffer_size = PCM_BUF_SIZE;
195 audio->pcm_cfg.buffer_count = PCM_BUF_COUNT;
196 enc_cfg->min_bit_rate = 4;
197 enc_cfg->max_bit_rate = 4;
198 audio->pcm_cfg.channel_count = 1;
199 audio->pcm_cfg.sample_rate = 8000;
200 audio->buf_cfg.meta_info_enable = 0x01;
201 audio->buf_cfg.frames_per_buf = 0x01;
Phani Kumar Uppalapati4e3c5db2013-01-08 10:54:14 -0800202 audio->event_abort = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700203
Harmandeep Singhc35fa07d2012-05-31 07:08:59 -0700204 audio->ac = q6asm_audio_client_alloc((app_cb)q6asm_in_cb,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700205 (void *)audio);
206
207 if (!audio->ac) {
Phani Kumar Uppalapati4e3c5db2013-01-08 10:54:14 -0800208 pr_err("%s: Could not allocate memory for audio client\n",
209 __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700210 kfree(audio->enc_cfg);
211 kfree(audio);
212 return -ENOMEM;
213 }
214
215 /* open qcelp encoder in T/NT mode */
216 if ((file->f_mode & FMODE_WRITE) &&
217 (file->f_mode & FMODE_READ)) {
218 audio->feedback = NON_TUNNEL_MODE;
219 rc = q6asm_open_read_write(audio->ac, FORMAT_V13K,
220 FORMAT_LINEAR_PCM);
221 if (rc < 0) {
222 pr_err("%s:session id %d: NT mode Open failed rc=%d\n",
223 __func__, audio->ac->session, rc);
224 rc = -ENODEV;
225 goto fail;
226 }
227 pr_info("%s:session id %d: NT mode encoder success\n", __func__,
228 audio->ac->session);
229 } else if (!(file->f_mode & FMODE_WRITE) &&
230 (file->f_mode & FMODE_READ)) {
231 audio->feedback = TUNNEL_MODE;
232 rc = q6asm_open_read(audio->ac, FORMAT_V13K);
233 if (rc < 0) {
234 pr_err("%s:session id %d: T mode Open failed rc=%d\n",
235 __func__, audio->ac->session, rc);
236 rc = -ENODEV;
237 goto fail;
238 }
239 /* register for tx overflow (valid for tunnel mode only) */
240 rc = q6asm_reg_tx_overflow(audio->ac, 0x01);
241 if (rc < 0) {
Phani Kumar Uppalapati4e3c5db2013-01-08 10:54:14 -0800242 pr_err("%s:session id %d: TX Overflow registration failed rc=%d\n",
243 __func__, audio->ac->session, rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700244 rc = -ENODEV;
245 goto fail;
246 }
247 pr_info("%s:session id %d: T mode encoder success\n", __func__,
248 audio->ac->session);
249 } else {
250 pr_err("%s:session id %d: Unexpected mode\n", __func__,
251 audio->ac->session);
252 rc = -EACCES;
253 goto fail;
254 }
255
256 audio->opened = 1;
257 atomic_set(&audio->in_count, PCM_BUF_COUNT);
258 atomic_set(&audio->out_count, 0x00);
259 audio->enc_ioctl = qcelp_in_ioctl;
260 file->private_data = audio;
261
262 pr_info("%s:session id %d: success\n", __func__, audio->ac->session);
263 return 0;
264fail:
265 q6asm_audio_client_free(audio->ac);
266 kfree(audio->enc_cfg);
267 kfree(audio);
268 return rc;
269}
270
271static const struct file_operations audio_in_fops = {
272 .owner = THIS_MODULE,
273 .open = qcelp_in_open,
274 .release = audio_in_release,
275 .read = audio_in_read,
276 .write = audio_in_write,
277 .unlocked_ioctl = audio_in_ioctl,
278};
279
280struct miscdevice audio_qcelp_in_misc = {
281 .minor = MISC_DYNAMIC_MINOR,
282 .name = "msm_qcelp_in",
283 .fops = &audio_in_fops,
284};
285
286static int __init qcelp_in_init(void)
287{
288 return misc_register(&audio_qcelp_in_misc);
289}
290
291device_initcall(qcelp_in_init);