summaryrefslogtreecommitdiff
path: root/dsp/codecs/audio_amrwbplus.c
blob: 9d6c118712ecfed2153e22ea5240a9c669a40a03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/* amr-wbplus audio output device
 *
 * Copyright (C) 2008 Google, Inc.
 * Copyright (C) 2008 HTC Corporation
 * Copyright (c) 2010-2017, The Linux Foundation. All rights reserved.
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */
#include <linux/msm_audio_amrwbplus.h>
#include <linux/compat.h>
#include "audio_utils_aio.h"

static struct miscdevice audio_amrwbplus_misc;
static struct ws_mgr audio_amrwbplus_ws_mgr;

#ifdef CONFIG_DEBUG_FS
static const struct file_operations audio_amrwbplus_debug_fops = {
	.read = audio_aio_debug_read,
	.open = audio_aio_debug_open,
};
static void config_debug_fs(struct q6audio_aio *audio)
{
	if (audio != NULL) {
		char name[sizeof("msm_amrwbplus_") + 5];

		snprintf(name, sizeof(name), "msm_amrwbplus_%04x",
			audio->ac->session);
		audio->dentry = debugfs_create_file(name, S_IFREG | 0444,
						NULL, (void *)audio,
						&audio_amrwbplus_debug_fops);
		if (IS_ERR(audio->dentry))
			pr_debug("debugfs_create_file failed\n");
	}
}
#else
static void config_debug_fs(struct q6audio_aio *audio)
{
}
#endif

static long audio_ioctl_shared(struct file *file, unsigned int cmd,
					void *arg)
{
	struct asm_amrwbplus_cfg q6_amrwbplus_cfg;
	struct msm_audio_amrwbplus_config_v2 *amrwbplus_drv_config;
	struct q6audio_aio *audio = file->private_data;
	int rc = 0;

	switch (cmd) {
	case AUDIO_START:
		pr_err("%s[%pK]: AUDIO_START session_id[%d]\n", __func__,
			audio, audio->ac->session);
		if (audio->feedback == NON_TUNNEL_MODE) {
			/* Configure PCM output block */
			rc = q6asm_enc_cfg_blk_pcm(audio->ac,
			audio->pcm_cfg.sample_rate,
			audio->pcm_cfg.channel_count);
			if (rc < 0) {
				pr_err("pcm output block config failed\n");
				break;
			}
		}
		amrwbplus_drv_config =
		(struct msm_audio_amrwbplus_config_v2 *)audio->codec_cfg;

		q6_amrwbplus_cfg.size_bytes     =
			amrwbplus_drv_config->size_bytes;
		q6_amrwbplus_cfg.version        =
			amrwbplus_drv_config->version;
		q6_amrwbplus_cfg.num_channels   =
			amrwbplus_drv_config->num_channels;
		q6_amrwbplus_cfg.amr_band_mode  =
			amrwbplus_drv_config->amr_band_mode;
		q6_amrwbplus_cfg.amr_dtx_mode   =
			amrwbplus_drv_config->amr_dtx_mode;
		q6_amrwbplus_cfg.amr_frame_fmt  =
			amrwbplus_drv_config->amr_frame_fmt;
		q6_amrwbplus_cfg.amr_lsf_idx    =
			amrwbplus_drv_config->amr_lsf_idx;

		rc = q6asm_media_format_block_amrwbplus(audio->ac,
							&q6_amrwbplus_cfg);
		if (rc < 0) {
			pr_err("q6asm_media_format_block_amrwb+ failed...\n");
			break;
		}
		rc = audio_aio_enable(audio);
		audio->eos_rsp = 0;
		audio->eos_flag = 0;
		if (!rc) {
			audio->enabled = 1;
		} else {
			audio->enabled = 0;
			pr_err("Audio Start procedure failed rc=%d\n", rc);
			break;
		}
		pr_debug("%s:AUDIO_START sessionid[%d]enable[%d]\n", __func__,
			audio->ac->session,
			audio->enabled);
		if (audio->stopped == 1)
			audio->stopped = 0;

		break;
	default:
		pr_err("%s: Unknown ioctl cmd = %d", __func__, cmd);
		rc = -EINVAL;
		break;
	}
	return rc;
}

static long audio_ioctl(struct file *file, unsigned int cmd,
						unsigned long arg)
{
	struct q6audio_aio *audio = file->private_data;
	int rc = 0;

	switch (cmd) {
	case AUDIO_START: {
		rc = audio_ioctl_shared(file, cmd, (void *)arg);
		break;
	}
	case AUDIO_GET_AMRWBPLUS_CONFIG_V2: {
		if ((audio) && (arg) && (audio->codec_cfg)) {
			if (copy_to_user((void *)arg, audio->codec_cfg,
				sizeof(struct msm_audio_amrwbplus_config_v2))) {
				rc = -EFAULT;
				pr_err("%s: copy_to_user for AUDIO_GET_AMRWBPLUS_CONFIG_V2 failed\n",
					__func__);
				break;
			}
			} else {
				pr_err("%s: wb+ config v2 invalid parameters\n"
					, __func__);
				rc = -EFAULT;
				break;
			}
		break;
	}
	case AUDIO_SET_AMRWBPLUS_CONFIG_V2: {
		if ((audio) && (arg) && (audio->codec_cfg)) {
			if (copy_from_user(audio->codec_cfg, (void *)arg,
				sizeof(struct msm_audio_amrwbplus_config_v2))) {
				rc = -EFAULT;
				pr_err("%s: copy_from_user for AUDIO_SET_AMRWBPLUS_CONFIG_V2 failed\n",
					__func__);
				break;
			}
			} else {
				pr_err("%s: wb+ config invalid parameters\n",
					__func__);
				rc = -EFAULT;
				break;
			}
		break;
	}
	default: {
		pr_debug("%s[%pK]: Calling utils ioctl\n", __func__, audio);
		rc = audio->codec_ioctl(file, cmd, arg);
		break;
	}
	}
	return rc;
}
#ifdef CONFIG_COMPAT
struct msm_audio_amrwbplus_config_v2_32 {
	u32 size_bytes;
	u32 version;
	u32 num_channels;
	u32 amr_band_mode;
	u32 amr_dtx_mode;
	u32 amr_frame_fmt;
	u32 amr_lsf_idx;
};

enum {
	AUDIO_GET_AMRWBPLUS_CONFIG_V2_32 = _IOR(AUDIO_IOCTL_MAGIC,
		(AUDIO_MAX_COMMON_IOCTL_NUM+2),
		struct msm_audio_amrwbplus_config_v2_32),
	AUDIO_SET_AMRWBPLUS_CONFIG_V2_32 = _IOW(AUDIO_IOCTL_MAGIC,
		(AUDIO_MAX_COMMON_IOCTL_NUM+3),
		struct msm_audio_amrwbplus_config_v2_32)
};

static long audio_compat_ioctl(struct file *file, unsigned int cmd,
					unsigned long arg)
{
	struct q6audio_aio *audio = file->private_data;
	int rc = 0;

	switch (cmd) {
	case AUDIO_START: {
		rc = audio_ioctl_shared(file, cmd, (void *)arg);
		break;
	}
	case AUDIO_GET_AMRWBPLUS_CONFIG_V2_32: {
		if (audio && arg && (audio->codec_cfg)) {
			struct msm_audio_amrwbplus_config_v2 *amrwbplus_config;
			struct msm_audio_amrwbplus_config_v2_32
						amrwbplus_config_32;

			memset(&amrwbplus_config_32, 0,
					sizeof(amrwbplus_config_32));

			amrwbplus_config =
				(struct msm_audio_amrwbplus_config_v2 *)
				audio->codec_cfg;
			amrwbplus_config_32.size_bytes =
					amrwbplus_config->size_bytes;
			amrwbplus_config_32.version =
					amrwbplus_config->version;
			amrwbplus_config_32.num_channels =
					amrwbplus_config->num_channels;
			amrwbplus_config_32.amr_band_mode =
					amrwbplus_config->amr_band_mode;
			amrwbplus_config_32.amr_dtx_mode =
					amrwbplus_config->amr_dtx_mode;
			amrwbplus_config_32.amr_frame_fmt =
					amrwbplus_config->amr_frame_fmt;
			amrwbplus_config_32.amr_lsf_idx =
					amrwbplus_config->amr_lsf_idx;

			if (copy_to_user((void *)arg, &amrwbplus_config_32,
				sizeof(amrwbplus_config_32))) {
				rc = -EFAULT;
				pr_err("%s: copy_to_user for AUDIO_GET_AMRWBPLUS_CONFIG_V2_32 failed\n"
					, __func__);
			}
		} else {
			pr_err("%s: wb+ Get config v2 invalid parameters\n"
				, __func__);
			rc = -EFAULT;
		}
		break;
	}
	case AUDIO_SET_AMRWBPLUS_CONFIG_V2_32: {
		if ((audio) && (arg) && (audio->codec_cfg)) {
			struct msm_audio_amrwbplus_config_v2 *amrwbplus_config;
			struct msm_audio_amrwbplus_config_v2_32
							amrwbplus_config_32;

			if (copy_from_user(&amrwbplus_config_32, (void *)arg,
			sizeof(struct msm_audio_amrwbplus_config_v2_32))) {
				rc = -EFAULT;
				pr_err("%s: copy_from_user for AUDIO_SET_AMRWBPLUS_CONFIG_V2_32 failed\n"
					, __func__);
				break;
			}
			amrwbplus_config =
			 (struct msm_audio_amrwbplus_config_v2 *)
						audio->codec_cfg;
			amrwbplus_config->size_bytes =
					amrwbplus_config_32.size_bytes;
			amrwbplus_config->version =
					amrwbplus_config_32.version;
			amrwbplus_config->num_channels =
					amrwbplus_config_32.num_channels;
			amrwbplus_config->amr_band_mode =
					amrwbplus_config_32.amr_band_mode;
			amrwbplus_config->amr_dtx_mode =
					amrwbplus_config_32.amr_dtx_mode;
			amrwbplus_config->amr_frame_fmt =
					amrwbplus_config_32.amr_frame_fmt;
			amrwbplus_config->amr_lsf_idx =
					amrwbplus_config_32.amr_lsf_idx;
		} else {
			pr_err("%s: wb+ config invalid parameters\n",
				__func__);
			rc = -EFAULT;
		}
		break;
	}
	default: {
		pr_debug("%s[%pK]: Calling utils ioctl\n", __func__, audio);
		rc = audio->codec_compat_ioctl(file, cmd, arg);
		break;
	}
	}
	return rc;
}
#else
#define audio_compat_ioctl NULL
#endif

static int audio_open(struct inode *inode, struct file *file)
{
	struct q6audio_aio *audio = NULL;
	int rc = 0;

	audio = kzalloc(sizeof(struct q6audio_aio), GFP_KERNEL);

	if (audio == NULL)
		return -ENOMEM;

	audio->codec_cfg =
	kzalloc(sizeof(struct msm_audio_amrwbplus_config_v2), GFP_KERNEL);
	if (audio->codec_cfg == NULL) {
		kfree(audio);
		return -ENOMEM;
	}
	audio->pcm_cfg.buffer_size = PCM_BUFSZ_MIN;
	audio->miscdevice = &audio_amrwbplus_misc;
	audio->wakelock_voted = false;
	audio->audio_ws_mgr = &audio_amrwbplus_ws_mgr;

	audio->ac =
	q6asm_audio_client_alloc((app_cb) q6_audio_cb, (void *)audio);

	if (!audio->ac) {
		pr_err("Could not allocate memory for audio client\n");
		kfree(audio->codec_cfg);
		kfree(audio);
		return -ENOMEM;
	}
	rc = audio_aio_open(audio, file);
	if (rc < 0) {
		pr_err("%s: audio_aio_open rc=%d\n",
			__func__, rc);
		goto fail;
	}

	/* open in T/NT mode */
	if ((file->f_mode & FMODE_WRITE) && (file->f_mode & FMODE_READ)) {
		rc = q6asm_open_read_write(audio->ac, FORMAT_LINEAR_PCM,
					FORMAT_AMR_WB_PLUS);
		if (rc < 0) {
			pr_err("amrwbplus NT mode Open failed rc=%d\n", rc);
			rc = -ENODEV;
			goto fail;
		}
		audio->feedback = NON_TUNNEL_MODE;
		audio->buf_cfg.frames_per_buf = 0x01;
		audio->buf_cfg.meta_info_enable = 0x01;
	} else if ((file->f_mode & FMODE_WRITE) &&
			!(file->f_mode & FMODE_READ)) {
		rc = q6asm_open_write(audio->ac, FORMAT_AMR_WB_PLUS);
		if (rc < 0) {
			pr_err("wb+ T mode Open failed rc=%d\n", rc);
			rc = -ENODEV;
			goto fail;
		}
		audio->feedback = TUNNEL_MODE;
		audio->buf_cfg.meta_info_enable = 0x00;
	} else {
		pr_err("audio_amrwbplus Not supported mode\n");
		rc = -EACCES;
		goto fail;
	}

	config_debug_fs(audio);
	pr_debug("%s: AMRWBPLUS dec success mode[%d]session[%d]\n", __func__,
		audio->feedback,
		audio->ac->session);
	return 0;
fail:
	q6asm_audio_client_free(audio->ac);
	kfree(audio->codec_cfg);
	kfree(audio);
	return rc;
}

static const struct file_operations audio_amrwbplus_fops = {
	.owner = THIS_MODULE,
	.open = audio_open,
	.release = audio_aio_release,
	.unlocked_ioctl = audio_ioctl,
	.fsync = audio_aio_fsync,
	.compat_ioctl = audio_compat_ioctl
};

static struct miscdevice audio_amrwbplus_misc = {
	.minor = MISC_DYNAMIC_MINOR,
	.name = "msm_amrwbplus",
	.fops = &audio_amrwbplus_fops,
};

int __init audio_amrwbplus_init(void)
{
	int ret = misc_register(&audio_amrwbplus_misc);

	if (ret == 0)
		device_init_wakeup(audio_amrwbplus_misc.this_device, true);
	audio_amrwbplus_ws_mgr.ref_cnt = 0;
	mutex_init(&audio_amrwbplus_ws_mgr.ws_lock);

	return ret;
}

void audio_amrwbplus_exit(void)
{
	mutex_destroy(&audio_amrwbplus_ws_mgr.ws_lock);
	misc_deregister(&audio_amrwbplus_misc);
}