blob: 9994aabc9591ed87c4f2e6045c3e9986bc60bce6 [file] [log] [blame]
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301/* Copyright (c) 2016-2017, The Linux Foundation. All rights reserved.
2 *
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/types.h>
15#include <linux/msm_audio_g711_dec.h>
16#include <linux/compat.h>
17#include "audio_utils_aio.h"
18
19static struct miscdevice audio_g711mlaw_misc;
20static struct ws_mgr audio_g711_ws_mgr;
21
22static const struct file_operations audio_g711_debug_fops = {
23 .read = audio_aio_debug_read,
24 .open = audio_aio_debug_open,
25};
26
27static struct dentry *config_debugfs_create_file(const char *name, void *data)
28{
29 return debugfs_create_file(name, S_IFREG | 0444,
30 NULL, (void *)data, &audio_g711_debug_fops);
31}
32
33static int g711_channel_map(u8 *channel_mapping, uint32_t channels);
34
35static long audio_ioctl_shared(struct file *file, unsigned int cmd,
36 void *arg)
37{
38 struct q6audio_aio *audio = file->private_data;
39 int rc = 0;
40
41 switch (cmd) {
42 case AUDIO_START: {
43 struct asm_g711_dec_cfg g711_dec_cfg;
44 struct msm_audio_g711_dec_config *g711_dec_config;
45 u8 channel_mapping[PCM_FORMAT_MAX_NUM_CHANNEL];
46
47 memset(channel_mapping, 0, PCM_FORMAT_MAX_NUM_CHANNEL);
48 memset(&g711_dec_cfg, 0, sizeof(g711_dec_cfg));
49
50 if (g711_channel_map(channel_mapping,
51 audio->pcm_cfg.channel_count)) {
52 pr_err("%s: setting channel map failed %d\n",
53 __func__, audio->pcm_cfg.channel_count);
54 }
55
56 pr_debug("%s[%pK]: AUDIO_START session_id[%d]\n", __func__,
57 audio, audio->ac->session);
58 if (audio->feedback == NON_TUNNEL_MODE) {
59 /* Configure PCM output block */
60 rc = q6asm_enc_cfg_blk_pcm_v2(audio->ac,
61 audio->pcm_cfg.sample_rate,
62 audio->pcm_cfg.channel_count,
63 16, /*bits per sample*/
64 false, false, channel_mapping);
65 if (rc < 0) {
66 pr_err("%s: pcm output block config failed rc=%d\n",
67 __func__, rc);
68 break;
69 }
70 }
71 g711_dec_config =
72 (struct msm_audio_g711_dec_config *)audio->codec_cfg;
73 g711_dec_cfg.sample_rate = g711_dec_config->sample_rate;
74 /* Configure Media format block */
75 rc = q6asm_media_format_block_g711(audio->ac, &g711_dec_cfg,
76 audio->ac->stream_id);
77 if (rc < 0) {
78 pr_err("%s: cmd media format block failed rc=%d\n",
79 __func__, rc);
80 break;
81 }
82 rc = audio_aio_enable(audio);
83 audio->eos_rsp = 0;
84 audio->eos_flag = 0;
85 if (!rc) {
86 audio->enabled = 1;
87 } else {
88 audio->enabled = 0;
89 pr_err("%s: Audio Start procedure failed rc=%d\n",
90 __func__, rc);
91 break;
92 }
93 pr_debug("%s: AUDIO_START success enable[%d]\n",
94 __func__, audio->enabled);
95 if (audio->stopped == 1)
96 audio->stopped = 0;
97 break;
98 }
99 default:
100 pr_debug("%s: Unknown ioctl cmd = %d", __func__, cmd);
101 break;
102 }
103 return rc;
104}
105
106static long audio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
107{
108 struct q6audio_aio *audio = file->private_data;
109 int rc = 0;
110
111 switch (cmd) {
112 case AUDIO_START: {
113 rc = audio_ioctl_shared(file, cmd, (void *)arg);
114 break;
115 }
116 case AUDIO_GET_G711_DEC_CONFIG: {
117 if (copy_to_user((void *)arg, audio->codec_cfg,
118 sizeof(struct msm_audio_g711_dec_config))) {
119 pr_err("%s: AUDIO_GET_G711_DEC_CONFIG failed\n",
120 __func__);
121 rc = -EFAULT;
122 }
123 break;
124 }
125 case AUDIO_SET_G711_DEC_CONFIG: {
126 if (copy_from_user(audio->codec_cfg, (void *)arg,
127 sizeof(struct msm_audio_g711_dec_config))) {
128 pr_err("%s: AUDIO_SET_G711_DEC_CONFIG failed\n",
129 __func__);
130 rc = -EFAULT;
131 }
132 break;
133 }
134 default: {
135 rc = audio->codec_ioctl(file, cmd, arg);
136 if (rc)
137 pr_err("%s: Failed in audio_aio_ioctl: %d cmd=%d\n",
138 __func__, rc, cmd);
139 break;
140 }
141 }
142 return rc;
143}
144
145#ifdef CONFIG_COMPAT
146struct msm_audio_g711_dec_config_32 {
147 u32 sample_rate;
148};
149
150enum {
151 AUDIO_SET_G711_DEC_CONFIG_32 = _IOW(AUDIO_IOCTL_MAGIC,
152 (AUDIO_MAX_COMMON_IOCTL_NUM+0), struct msm_audio_g711_dec_config_32),
153 AUDIO_GET_G711_DEC_CONFIG_32 = _IOR(AUDIO_IOCTL_MAGIC,
154 (AUDIO_MAX_COMMON_IOCTL_NUM+1), struct msm_audio_g711_dec_config_32)
155};
156
157static long audio_compat_ioctl(struct file *file, unsigned int cmd,
158 unsigned long arg)
159{
160 struct q6audio_aio *audio = file->private_data;
161 int rc = 0;
162
163 switch (cmd) {
164 case AUDIO_START: {
165 rc = audio_ioctl_shared(file, cmd, (void *)arg);
166 break;
167 }
168 case AUDIO_GET_G711_DEC_CONFIG_32: {
169 struct msm_audio_g711_dec_config *g711_dec_config;
170 struct msm_audio_g711_dec_config_32 g711_dec_config_32;
171
172 memset(&g711_dec_config_32, 0, sizeof(g711_dec_config_32));
173
174 g711_dec_config =
175 (struct msm_audio_g711_dec_config *)audio->codec_cfg;
176 g711_dec_config_32.sample_rate = g711_dec_config->sample_rate;
177
178 if (copy_to_user((void *)arg, &g711_dec_config_32,
179 sizeof(g711_dec_config_32))) {
180 pr_err("%s: copy_to_user for AUDIO_GET_G711_DEC_CONFIG failed\n",
181 __func__);
182 rc = -EFAULT;
183 }
184 break;
185 }
186 case AUDIO_SET_G711_DEC_CONFIG_32: {
187 struct msm_audio_g711_dec_config *g711_dec_config;
188 struct msm_audio_g711_dec_config_32 g711_dec_config_32;
189
190 memset(&g711_dec_config_32, 0, sizeof(g711_dec_config_32));
191
192 if (copy_from_user(&g711_dec_config_32, (void *)arg,
193 sizeof(g711_dec_config_32))) {
194 pr_err("%s: copy_from_user for AUDIO_SET_G711_DEC_CONFIG failed\n",
195 __func__);
196 rc = -EFAULT;
197 break;
198 }
199 g711_dec_config =
200 (struct msm_audio_g711_dec_config *)audio->codec_cfg;
201 g711_dec_config->sample_rate = g711_dec_config_32.sample_rate;
202
203 break;
204 }
205 default: {
206 rc = audio->codec_compat_ioctl(file, cmd, arg);
207 if (rc)
208 pr_err("%s: Failed in audio_aio_compat_ioctl: %d cmd=%d\n",
209 __func__, rc, cmd);
210 break;
211 }
212 }
213 return rc;
214}
215#else
216#define audio_compat_ioctl NULL
217#endif
218
219static int audio_open(struct inode *inode, struct file *file)
220{
221 struct q6audio_aio *audio = NULL;
222 int rc = 0;
223 /* 4 bytes represents decoder number, 1 byte for terminate string */
224 char name[sizeof "msm_g711_" + 5];
225
226 audio = kzalloc(sizeof(struct q6audio_aio), GFP_KERNEL);
227
228 if (!audio)
229 return -ENOMEM;
230 audio->codec_cfg = kzalloc(sizeof(struct msm_audio_g711_dec_config),
231 GFP_KERNEL);
232 if (!audio->codec_cfg) {
233 kfree(audio);
234 return -ENOMEM;
235 }
236
237 audio->pcm_cfg.buffer_size = PCM_BUFSZ_MIN;
238 audio->miscdevice = &audio_g711mlaw_misc;
239 audio->wakelock_voted = false;
240 audio->audio_ws_mgr = &audio_g711_ws_mgr;
241
242 init_waitqueue_head(&audio->event_wait);
243
244 audio->ac = q6asm_audio_client_alloc((app_cb) q6_audio_cb,
245 (void *)audio);
246
247 if (!audio->ac) {
248 pr_err("%s: Could not allocate memory for audio client\n",
249 __func__);
250 kfree(audio->codec_cfg);
251 kfree(audio);
252 return -ENOMEM;
253 }
254 rc = audio_aio_open(audio, file);
255 if (rc < 0) {
256 pr_err("%s: audio_aio_open rc=%d\n",
257 __func__, rc);
258 goto fail;
259 }
260 /* open in T/NT mode */ /*foramt:G711_ALAW*/
261 if ((file->f_mode & FMODE_WRITE) && (file->f_mode & FMODE_READ)) {
262 rc = q6asm_open_read_write(audio->ac, FORMAT_LINEAR_PCM,
263 FORMAT_G711_MLAW_FS);
264 if (rc < 0) {
265 pr_err("%s: NT mode Open failed rc=%d\n", __func__, rc);
266 goto fail;
267 }
268 audio->feedback = NON_TUNNEL_MODE;
269 /* open G711 decoder, expected frames is always 1*/
270 audio->buf_cfg.frames_per_buf = 0x01;
271 audio->buf_cfg.meta_info_enable = 0x01;
272 } else if ((file->f_mode & FMODE_WRITE) &&
273 !(file->f_mode & FMODE_READ)) {
274 rc = q6asm_open_write(audio->ac, FORMAT_G711_MLAW_FS);
275 if (rc < 0) {
276 pr_err("%s: T mode Open failed rc=%d\n", __func__, rc);
277 goto fail;
278 }
279 audio->feedback = TUNNEL_MODE;
280 audio->buf_cfg.meta_info_enable = 0x00;
281 } else {
282 pr_err("%s: %d mode is not supported\n", __func__,
283 file->f_mode);
284 rc = -EACCES;
285 goto fail;
286 }
287
288 snprintf(name, sizeof(name), "msm_g711_%04x", audio->ac->session);
289 audio->dentry = config_debugfs_create_file(name, (void *)audio);
290
291 if (IS_ERR_OR_NULL(audio->dentry))
292 pr_debug("%s: debugfs_create_file failed\n", __func__);
293 pr_debug("%s: g711dec success mode[%d]session[%d]\n", __func__,
294 audio->feedback,
295 audio->ac->session);
296 return rc;
297fail:
298 q6asm_audio_client_free(audio->ac);
299 kfree(audio->codec_cfg);
300 kfree(audio);
301 return rc;
302}
303
304static int g711_channel_map(u8 *channel_mapping, uint32_t channels)
305{
306 u8 *lchannel_mapping;
307
308 lchannel_mapping = channel_mapping;
309 pr_debug("%s: channels passed: %d\n", __func__, channels);
310 if (channels == 1) {
311 lchannel_mapping[0] = PCM_CHANNEL_FC;
312 } else if (channels == 2) {
313 lchannel_mapping[0] = PCM_CHANNEL_FL;
314 lchannel_mapping[1] = PCM_CHANNEL_FR;
315 } else if (channels == 3) {
316 lchannel_mapping[0] = PCM_CHANNEL_FC;
317 lchannel_mapping[1] = PCM_CHANNEL_FL;
318 lchannel_mapping[2] = PCM_CHANNEL_FR;
319 } else if (channels == 4) {
320 lchannel_mapping[0] = PCM_CHANNEL_FC;
321 lchannel_mapping[1] = PCM_CHANNEL_FL;
322 lchannel_mapping[2] = PCM_CHANNEL_FR;
323 lchannel_mapping[3] = PCM_CHANNEL_CS;
324 } else if (channels == 5) {
325 lchannel_mapping[0] = PCM_CHANNEL_FC;
326 lchannel_mapping[1] = PCM_CHANNEL_FL;
327 lchannel_mapping[2] = PCM_CHANNEL_FR;
328 lchannel_mapping[3] = PCM_CHANNEL_LS;
329 lchannel_mapping[4] = PCM_CHANNEL_RS;
330 } else if (channels == 6) {
331 lchannel_mapping[0] = PCM_CHANNEL_FC;
332 lchannel_mapping[1] = PCM_CHANNEL_FL;
333 lchannel_mapping[2] = PCM_CHANNEL_FR;
334 lchannel_mapping[3] = PCM_CHANNEL_LS;
335 lchannel_mapping[4] = PCM_CHANNEL_RS;
336 lchannel_mapping[5] = PCM_CHANNEL_LFE;
337 } else if (channels == 7) {
338 lchannel_mapping[0] = PCM_CHANNEL_FC;
339 lchannel_mapping[1] = PCM_CHANNEL_FL;
340 lchannel_mapping[2] = PCM_CHANNEL_FR;
341 lchannel_mapping[3] = PCM_CHANNEL_LS;
342 lchannel_mapping[4] = PCM_CHANNEL_RS;
343 lchannel_mapping[5] = PCM_CHANNEL_CS;
344 lchannel_mapping[6] = PCM_CHANNEL_LFE;
345 } else if (channels == 8) {
346 lchannel_mapping[0] = PCM_CHANNEL_FC;
347 lchannel_mapping[1] = PCM_CHANNEL_FLC;
348 lchannel_mapping[2] = PCM_CHANNEL_FRC;
349 lchannel_mapping[3] = PCM_CHANNEL_FL;
350 lchannel_mapping[4] = PCM_CHANNEL_FR;
351 lchannel_mapping[5] = PCM_CHANNEL_LS;
352 lchannel_mapping[6] = PCM_CHANNEL_RS;
353 lchannel_mapping[7] = PCM_CHANNEL_LFE;
354 } else {
355 pr_err("%s: ERROR.unsupported num_ch = %u\n",
356 __func__, channels);
357 return -EINVAL;
358 }
359 return 0;
360}
361
362static const struct file_operations audio_g711_fops = {
363 .owner = THIS_MODULE,
364 .open = audio_open,
365 .release = audio_aio_release,
366 .unlocked_ioctl = audio_ioctl,
367 .compat_ioctl = audio_compat_ioctl,
368 .fsync = audio_aio_fsync,
369};
370
371static struct miscdevice audio_g711mlaw_misc = {
372 .minor = MISC_DYNAMIC_MINOR,
373 .name = "msm_g711mlaw",
374 .fops = &audio_g711_fops,
375};
376
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530377int __init audio_g711mlaw_init(void)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530378{
379 int ret = misc_register(&audio_g711mlaw_misc);
380
381 if (ret == 0)
382 device_init_wakeup(audio_g711mlaw_misc.this_device, true);
383 audio_g711_ws_mgr.ref_cnt = 0;
384 mutex_init(&audio_g711_ws_mgr.ws_lock);
385
386 return ret;
387}
388
Asish Bhattacharya5faacb32017-12-04 17:23:15 +0530389void audio_g711mlaw_exit(void)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530390{
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530391 mutex_destroy(&audio_g711_ws_mgr.ws_lock);
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530392 misc_deregister(&audio_g711mlaw_misc);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530393}
394