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
|
/* Copyright (c) 2018, The Linux Foundation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 and
* only version 2 as published by the Free Software Foundation.
*
* 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/slab.h>
#include <linux/fs.h>
#include <linux/module.h>
#include <linux/uaccess.h>
#include <linux/mutex.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
#include <linux/device.h>
#include <linux/qdsp6v2/audio-anc-dev-mgr.h>
#define DEVICE_NAME "msm_audio_anc"
struct audio_anc_info {
struct cdev myc;
struct class *anc_class;
};
static int major;
static struct audio_anc_info audio_anc;
static size_t get_user_anc_cmd_size(int32_t anc_cmd)
{
size_t size = 0;
switch (anc_cmd) {
case ANC_CMD_START:
case ANC_CMD_STOP:
size = 0;
break;
case ANC_CMD_ALGO_MODULE:
size = sizeof(struct audio_anc_algo_module_info);
break;
case ANC_CMD_ALGO_CALIBRATION:
size = sizeof(struct audio_anc_algo_calibration_header);
break;
default:
pr_err("%s:Invalid anc cmd %d!",
__func__, anc_cmd);
}
return size;
}
static int call_set_anc(int32_t anc_cmd,
size_t anc_cmd_size, void *data)
{
int ret = 0;
pr_err("%s EXT_ANC anc_cmd %x\n", __func__, anc_cmd);
switch (anc_cmd) {
case ANC_CMD_START:
ret = msm_anc_dev_start();
break;
case ANC_CMD_STOP:
ret = msm_anc_dev_stop();
break;
case ANC_CMD_ALGO_MODULE:
case ANC_CMD_ALGO_CALIBRATION:
ret = msm_anc_dev_set_info(data, anc_cmd);
break;
default:
break;
}
pr_err("%s EXT_ANC ret %x\n", __func__, ret);
return ret;
}
static int call_get_anc(int32_t anc_cmd,
size_t anc_cmd_size, void *data)
{
int ret = 0;
switch (anc_cmd) {
case ANC_CMD_ALGO_CALIBRATION:
ret = msm_anc_dev_get_info(data, anc_cmd);
break;
default:
break;
}
return ret;
}
static int audio_anc_open(struct inode *inode, struct file *f)
{
int ret = 0;
pr_debug("%s\n", __func__);
return ret;
}
static int audio_anc_close(struct inode *inode, struct file *f)
{
int ret = 0;
pr_debug("%s\n", __func__);
return ret;
}
static long audio_anc_shared_ioctl(struct file *file, unsigned int cmd,
void __user *arg)
{
int ret = 0;
int32_t size;
struct audio_anc_packet *data = NULL;
pr_err("%s EXT_ANC cmd %x\n", __func__, cmd);
switch (cmd) {
case AUDIO_ANC_SET_PARAM:
case AUDIO_ANC_GET_PARAM:
break;
default:
pr_err("%s: ioctl not found!\n", __func__);
ret = -EFAULT;
goto done;
}
if (copy_from_user(&size, (void *)arg, sizeof(size))) {
pr_err("%s: Could not copy size value from user\n", __func__);
ret = -EFAULT;
goto done;
} else if (size < sizeof(struct audio_anc_header)) {
pr_err("%s: Invalid size sent to driver: %d, min size is %zd\n",
__func__, size, sizeof(struct audio_anc_header));
ret = -EINVAL;
goto done;
}
data = kmalloc(size, GFP_KERNEL);
if (data == NULL) {
ret = -ENOMEM;
pr_err("%s: Could not allocate memory of size %d for ioctl\n",
__func__, size);
goto done;
} else if (copy_from_user(data, (void *)arg, size)) {
pr_err("%s: Could not copy data from user\n",
__func__);
ret = -EFAULT;
goto done;
} else if ((data->hdr.anc_cmd < 0) ||
(data->hdr.anc_cmd >= ANC_CMD_MAX)) {
pr_err("%s: anc_cmd %d is Invalid!\n",
__func__, data->hdr.anc_cmd);
ret = -EINVAL;
goto done;
} else if ((data->hdr.anc_cmd_size <
get_user_anc_cmd_size(data->hdr.anc_cmd)) ||
(data->hdr.anc_cmd_size >
sizeof(union audio_anc_data))) {
pr_err("%s: anc_cmd size %d is Invalid! Min is %zd Max is %zd!\n",
__func__, data->hdr.anc_cmd_size,
get_user_anc_cmd_size(data->hdr.anc_cmd),
sizeof(union audio_anc_data));
ret = -EINVAL;
goto done;
} else if ((data->hdr.anc_cmd_size + sizeof(data->hdr)) > size) {
pr_err("%s: anc_cmd size %d + anc cmd hdr size %zd is is greater than user buffer siz %d!\n",
__func__, data->hdr.anc_cmd_size, sizeof(data->hdr),
size);
ret = -EFAULT;
goto done;
}
switch (cmd) {
case AUDIO_ANC_SET_PARAM:
ret = call_set_anc(data->hdr.anc_cmd,
data->hdr.anc_cmd_size, &data->anc_data);
break;
case AUDIO_ANC_GET_PARAM:
ret = call_get_anc(data->hdr.anc_cmd,
data->hdr.anc_cmd_size, &data->anc_data);
break;
}
if (cmd == AUDIO_ANC_GET_PARAM) {
if (data->hdr.anc_cmd_size == 0)
goto done;
if (data == NULL)
goto done;
if (copy_to_user(arg, data,
sizeof(data->hdr) + data->hdr.anc_cmd_size)) {
pr_err("%s: Could not copy anc data to user\n",
__func__);
ret = -EFAULT;
goto done;
}
}
done:
kfree(data);
pr_err("%s EXT_ANC ret %x\n", __func__, ret);
return ret;
}
static long audio_anc_ioctl(struct file *f,
unsigned int cmd, unsigned long arg)
{
return audio_anc_shared_ioctl(f, cmd, (void __user *)arg);
}
#ifdef CONFIG_COMPAT
#define AUDIO_ANC_SET_PARAM32 _IOWR(ANC_IOCTL_MAGIC, \
300, compat_uptr_t)
#define AUDIO_ANC_GET_PARAM32 _IOWR(ANC_IOCTL_MAGIC, \
301, compat_uptr_t)
static long audio_anc_compat_ioctl(struct file *f,
unsigned int cmd, unsigned long arg)
{
unsigned int cmd64;
int ret = 0;
switch (cmd) {
case AUDIO_ANC_SET_PARAM32:
cmd64 = AUDIO_ANC_SET_PARAM;
break;
case AUDIO_ANC_GET_PARAM32:
cmd64 = AUDIO_ANC_GET_PARAM;
break;
default:
pr_err("%s: ioctl not found!\n", __func__);
ret = -EFAULT;
goto done;
}
ret = audio_anc_shared_ioctl(f, cmd64, compat_ptr(arg));
done:
return ret;
}
#endif
static const struct file_operations audio_anc_fops = {
.owner = THIS_MODULE,
.open = audio_anc_open,
.release = audio_anc_close,
.unlocked_ioctl = audio_anc_ioctl,
#ifdef CONFIG_COMPAT
.compat_ioctl = audio_anc_compat_ioctl,
#endif
};
int msm_anc_dev_create(struct platform_device *pdev)
{
int result = 0;
dev_t dev = MKDEV(major, 0);
struct device *device_handle;
pr_debug("%s\n", __func__);
if (major) {
result = register_chrdev_region(dev, 1, DEVICE_NAME);
} else {
result = alloc_chrdev_region(&dev, 0, 1, DEVICE_NAME);
major = MAJOR(dev);
}
if (result < 0) {
pr_err("%s: Registering msm_audio_anc device failed\n",
__func__);
goto done;
}
audio_anc.anc_class = class_create(THIS_MODULE, "msm_audio_anc");
if (IS_ERR(audio_anc.anc_class)) {
result = PTR_ERR(audio_anc.anc_class);
pr_err("%s: Error creating anc class: %d\n",
__func__, result);
goto unregister_chrdev_region;
}
cdev_init(&audio_anc.myc, &audio_anc_fops);
result = cdev_add(&audio_anc.myc, dev, 1);
if (result < 0) {
pr_err("%s: Registering file operations failed\n",
__func__);
goto class_destroy;
}
device_handle = device_create(audio_anc.anc_class,
NULL, audio_anc.myc.dev, NULL, "msm_audio_anc");
if (IS_ERR(device_handle)) {
result = PTR_ERR(device_handle);
pr_err("%s: device_create failed: %d\n", __func__, result);
goto class_destroy;
}
pr_debug("exit %s\n", __func__);
return 0;
class_destroy:
class_destroy(audio_anc.anc_class);
unregister_chrdev_region:
unregister_chrdev_region(MKDEV(major, 0), 1);
done:
pr_err("exit %s\n", __func__);
return result;
}
int msm_anc_dev_destroy(struct platform_device *pdev)
{
device_destroy(audio_anc.anc_class, audio_anc.myc.dev);
cdev_del(&audio_anc.myc);
class_destroy(audio_anc.anc_class);
unregister_chrdev_region(MKDEV(major, 0), 1);
return 0;
}
static int __init audio_anc_init(void)
{
return msm_anc_dev_init();
}
static void __exit audio_anc_exit(void)
{
msm_anc_dev_deinit();
}
module_init(audio_anc_init);
module_exit(audio_anc_exit);
MODULE_DESCRIPTION("SoC QDSP6v2 Audio ANC driver");
MODULE_LICENSE("GPL v2");
|