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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
|
/*
* Copyright (c) 2012-2017, 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/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/of.h>
#include <linux/platform_device.h>
#include <linux/debugfs.h>
#include <linux/io.h>
#include <linux/errno.h>
#include <linux/mutex.h>
#include <linux/videodev2.h>
#include <media/v4l2-device.h>
#include <media/msm_ba.h>
#include "msm_ba_internal.h"
#include "msm_ba_debug.h"
#define BASE_DEVICE_NUMBER 35
static struct ba_ctxt *gp_ba_ctxt;
struct ba_ctxt *msm_ba_get_ba_context(void)
{
return gp_ba_ctxt;
}
static void msm_ba_set_ba_context(struct ba_ctxt *ba_ctxt)
{
gp_ba_ctxt = ba_ctxt;
}
static inline struct msm_ba_inst *get_ba_inst(struct file *filp, void *fh)
{
return container_of(filp->private_data,
struct msm_ba_inst, event_handler);
}
static int msm_ba_v4l2_open(struct file *filp)
{
struct video_device *vdev = video_devdata(filp);
struct msm_ba_inst *ba_inst;
ba_inst = msm_ba_open(NULL);
if (!ba_inst) {
dprintk(BA_ERR,
"Failed to create video instance");
return -ENOMEM;
}
clear_bit(V4L2_FL_USES_V4L2_FH, &vdev->flags);
filp->private_data = &(ba_inst->event_handler);
return 0;
}
static int msm_ba_v4l2_close(struct file *filp)
{
int rc = 0;
struct msm_ba_inst *ba_inst;
ba_inst = get_ba_inst(filp, NULL);
rc = msm_ba_close(ba_inst);
return rc;
}
static int msm_ba_v4l2_querycap(struct file *filp, void *fh,
struct v4l2_capability *cap)
{
struct msm_ba_inst *ba_inst = get_ba_inst(filp, fh);
return msm_ba_querycap((void *)ba_inst, cap);
}
static int msm_ba_v4l2_enum_input(struct file *file, void *fh,
struct v4l2_input *input)
{
struct msm_ba_inst *ba_inst = get_ba_inst(file, fh);
return msm_ba_enum_input((void *)ba_inst, input);
}
static int msm_ba_v4l2_g_input(struct file *file, void *fh,
unsigned int *index)
{
struct msm_ba_inst *ba_inst = get_ba_inst(file, fh);
return msm_ba_g_input((void *)ba_inst, index);
}
static int msm_ba_v4l2_s_input(struct file *file, void *fh,
unsigned int index)
{
struct msm_ba_inst *ba_inst = get_ba_inst(file, fh);
return msm_ba_s_input((void *)ba_inst, index);
}
static int msm_ba_v4l2_enum_output(struct file *file, void *fh,
struct v4l2_output *output)
{
struct msm_ba_inst *ba_inst = get_ba_inst(file, fh);
return msm_ba_enum_output((void *)ba_inst, output);
}
static int msm_ba_v4l2_g_output(struct file *file, void *fh,
unsigned int *index)
{
struct msm_ba_inst *ba_inst = get_ba_inst(file, fh);
return msm_ba_g_output((void *)ba_inst, index);
}
static int msm_ba_v4l2_s_output(struct file *file, void *fh,
unsigned int index)
{
struct msm_ba_inst *ba_inst = get_ba_inst(file, fh);
return msm_ba_s_output((void *)ba_inst, index);
}
static int msm_ba_v4l2_enum_fmt(struct file *file, void *fh,
struct v4l2_fmtdesc *f)
{
struct msm_ba_inst *ba_inst = get_ba_inst(file, fh);
return msm_ba_enum_fmt((void *)ba_inst, f);
}
static int msm_ba_v4l2_s_fmt(struct file *file, void *fh,
struct v4l2_format *f)
{
struct msm_ba_inst *ba_inst = get_ba_inst(file, fh);
return msm_ba_s_fmt((void *)ba_inst, f);
}
static int msm_ba_v4l2_g_fmt(struct file *file, void *fh,
struct v4l2_format *f)
{
struct msm_ba_inst *ba_inst = get_ba_inst(file, fh);
return msm_ba_g_fmt((void *)ba_inst, f);
}
static int msm_ba_v4l2_s_ctrl(struct file *file, void *fh,
struct v4l2_control *a)
{
struct msm_ba_inst *ba_inst = get_ba_inst(file, fh);
return msm_ba_s_ctrl((void *)ba_inst, a);
}
static int msm_ba_v4l2_g_ctrl(struct file *file, void *fh,
struct v4l2_control *a)
{
struct msm_ba_inst *ba_inst = get_ba_inst(file, fh);
return msm_ba_g_ctrl((void *)ba_inst, a);
}
static int msm_ba_v4l2_s_ext_ctrl(struct file *file, void *fh,
struct v4l2_ext_controls *a)
{
struct msm_ba_inst *ba_inst = get_ba_inst(file, fh);
return msm_ba_s_ext_ctrl((void *)ba_inst, a);
}
static int msm_ba_v4l2_streamon(struct file *file, void *fh,
enum v4l2_buf_type i)
{
struct msm_ba_inst *ba_inst = get_ba_inst(file, fh);
return msm_ba_streamon((void *)ba_inst, i);
}
static int msm_ba_v4l2_streamoff(struct file *file, void *fh,
enum v4l2_buf_type i)
{
struct msm_ba_inst *ba_inst = get_ba_inst(file, fh);
return msm_ba_streamoff((void *)ba_inst, i);
}
static int msm_ba_v4l2_subscribe_event(struct v4l2_fh *fh,
const struct v4l2_event_subscription *sub)
{
struct msm_ba_inst *ba_inst = container_of(fh,
struct msm_ba_inst, event_handler);
return msm_ba_subscribe_event((void *)ba_inst, sub);
}
static int msm_ba_v4l2_unsubscribe_event(struct v4l2_fh *fh,
const struct v4l2_event_subscription *sub)
{
struct msm_ba_inst *ba_inst = container_of(fh,
struct msm_ba_inst, event_handler);
return msm_ba_unsubscribe_event((void *)ba_inst, sub);
}
static int msm_ba_v4l2_s_parm(struct file *file, void *fh,
struct v4l2_streamparm *a)
{
struct msm_ba_inst *ba_inst = get_ba_inst(file, fh);
return msm_ba_s_parm((void *)ba_inst, a);
}
static int msm_ba_v4l2_g_parm(struct file *file, void *fh,
struct v4l2_streamparm *a)
{
return 0;
}
static long msm_ba_v4l2_private_ioctl(struct file *file, void *fh,
bool valid_prio, unsigned int cmd, void *arg)
{
struct msm_ba_inst *ba_inst = get_ba_inst(file, fh);
return msm_ba_private_ioctl((void *)ba_inst, cmd, (void *)arg);
}
static const struct v4l2_ioctl_ops msm_ba_v4l2_ioctl_ops = {
.vidioc_querycap = msm_ba_v4l2_querycap,
.vidioc_enum_fmt_vid_cap = msm_ba_v4l2_enum_fmt,
.vidioc_enum_fmt_vid_out = msm_ba_v4l2_enum_fmt,
.vidioc_s_fmt_vid_cap = msm_ba_v4l2_s_fmt,
.vidioc_s_fmt_vid_cap_mplane = msm_ba_v4l2_s_fmt,
.vidioc_g_fmt_vid_cap = msm_ba_v4l2_g_fmt,
.vidioc_g_fmt_vid_cap_mplane = msm_ba_v4l2_g_fmt,
.vidioc_streamon = msm_ba_v4l2_streamon,
.vidioc_streamoff = msm_ba_v4l2_streamoff,
.vidioc_s_ctrl = msm_ba_v4l2_s_ctrl,
.vidioc_g_ctrl = msm_ba_v4l2_g_ctrl,
.vidioc_s_ext_ctrls = msm_ba_v4l2_s_ext_ctrl,
.vidioc_subscribe_event = msm_ba_v4l2_subscribe_event,
.vidioc_unsubscribe_event = msm_ba_v4l2_unsubscribe_event,
.vidioc_s_parm = msm_ba_v4l2_s_parm,
.vidioc_g_parm = msm_ba_v4l2_g_parm,
.vidioc_enum_input = msm_ba_v4l2_enum_input,
.vidioc_g_input = msm_ba_v4l2_g_input,
.vidioc_s_input = msm_ba_v4l2_s_input,
.vidioc_enum_output = msm_ba_v4l2_enum_output,
.vidioc_g_output = msm_ba_v4l2_g_output,
.vidioc_s_output = msm_ba_v4l2_s_output,
.vidioc_default = msm_ba_v4l2_private_ioctl,
};
static unsigned int msm_ba_v4l2_poll(struct file *filp,
struct poll_table_struct *pt)
{
struct msm_ba_inst *ba_inst = get_ba_inst(filp, NULL);
return msm_ba_poll((void *)ba_inst, filp, pt);
}
static void msm_ba_release_video_device(struct video_device *pvdev)
{
}
static const struct v4l2_file_operations msm_ba_v4l2_ba_fops = {
.owner = THIS_MODULE,
.open = msm_ba_v4l2_open,
.release = msm_ba_v4l2_close,
.unlocked_ioctl = video_ioctl2,
.poll = msm_ba_v4l2_poll,
};
static int parse_ba_dt(struct platform_device *pdev)
{
uint32_t profile_count = 0;
struct device_node *np = pdev->dev.of_node;
struct device_node *child_np = NULL;
struct msm_ba_dev *dev_ctxt = NULL;
struct ba_ctxt *ba_ctxt = msm_ba_get_ba_context();
char *key = NULL;
uint32_t err = 0, i = 0;
dev_ctxt = ba_ctxt->dev_ctxt;
profile_count = of_get_child_count(np);
if (profile_count == 0) {
dprintk(BA_ERR, "%s: Error reading DT. node=%s",
__func__, np->full_name);
return -ENODEV;
}
dev_ctxt->msm_ba_inp_cfg = devm_kzalloc(&pdev->dev,
sizeof(struct msm_ba_input_config) * profile_count,
GFP_KERNEL);
if (!dev_ctxt->msm_ba_inp_cfg)
return -ENOMEM;
i = 0;
for_each_child_of_node(np, child_np) {
key = "qcom,type";
err = of_property_read_u32(child_np, key,
&dev_ctxt->msm_ba_inp_cfg[i].input_type);
if (err)
goto read_fail;
key = "qcom,name";
err = of_property_read_string(child_np, key,
&dev_ctxt->msm_ba_inp_cfg[i].name);
if (err)
goto read_fail;
key = "qcom,ba-input";
err = of_property_read_u32(child_np, key,
&dev_ctxt->msm_ba_inp_cfg[i].ba_ip);
if (err)
goto read_fail;
key = "qcom,ba-output";
err = of_property_read_u32(child_np, key,
&dev_ctxt->msm_ba_inp_cfg[i].ba_out);
if (err)
goto read_fail;
key = "qcom,sd-name";
err = of_property_read_string(child_np, key,
&dev_ctxt->msm_ba_inp_cfg[i].sd_name);
if (err)
goto read_fail;
key = "qcom,ba-node";
err = of_property_read_u32(child_np, key,
&dev_ctxt->msm_ba_inp_cfg[i].ba_node);
if (err)
goto read_fail;
key = "qcom,user-type";
err = of_property_read_u32(child_np, key,
&dev_ctxt->msm_ba_inp_cfg[i].input_user_type);
if (err)
goto read_fail;
i++;
}
dev_ctxt->num_config_inputs = i;
read_fail:
if (err) {
dprintk(BA_INFO, "%s: Error reading DT. node=%s key=%s",
__func__, np->full_name, key);
devm_kfree(&pdev->dev, dev_ctxt->msm_ba_inp_cfg);
dev_ctxt->num_config_inputs = 0;
}
return err;
}
static int msm_ba_device_init(struct platform_device *pdev,
struct msm_ba_dev **ret_dev_ctxt)
{
struct msm_ba_dev *dev_ctxt;
int nr = BASE_DEVICE_NUMBER;
int rc = 0;
dprintk(BA_INFO, "Enter %s", __func__);
if ((ret_dev_ctxt == NULL) ||
(*ret_dev_ctxt != NULL) ||
(pdev == NULL)) {
dprintk(BA_ERR, "%s(%d) Invalid params",
__func__, __LINE__);
return -EINVAL;
}
dev_ctxt = devm_kzalloc(&pdev->dev, sizeof(struct msm_ba_dev),
GFP_KERNEL);
if (dev_ctxt == NULL)
return -ENOMEM;
platform_set_drvdata(pdev, dev_ctxt);
INIT_LIST_HEAD(&dev_ctxt->inputs);
INIT_LIST_HEAD(&dev_ctxt->instances);
INIT_LIST_HEAD(&dev_ctxt->sd_events);
INIT_DELAYED_WORK(&dev_ctxt->sd_events_work,
msm_ba_subdev_event_hndlr_delayed);
mutex_init(&dev_ctxt->dev_cs);
dev_ctxt->state = BA_DEV_UNINIT;
strlcpy(dev_ctxt->v4l2_dev.name, MSM_BA_DRV_NAME,
sizeof(dev_ctxt->v4l2_dev.name));
dev_ctxt->v4l2_dev.dev = &pdev->dev;
dev_ctxt->v4l2_dev.notify = msm_ba_subdev_event_hndlr;
rc = v4l2_device_register(dev_ctxt->v4l2_dev.dev, &dev_ctxt->v4l2_dev);
if (!rc) {
dev_ctxt->vdev = video_device_alloc();
if (dev_ctxt->vdev == NULL) {
v4l2_device_unregister(&dev_ctxt->v4l2_dev);
rc = -ENOMEM;
} else {
strlcpy(dev_ctxt->vdev->name,
pdev->name, sizeof(dev_ctxt->vdev->name));
dev_ctxt->vdev->v4l2_dev = &dev_ctxt->v4l2_dev;
dev_ctxt->vdev->release = msm_ba_release_video_device;
dev_ctxt->vdev->fops = &msm_ba_v4l2_ba_fops;
dev_ctxt->vdev->ioctl_ops = &msm_ba_v4l2_ioctl_ops;
dev_ctxt->vdev->minor = nr;
dev_ctxt->vdev->vfl_type = VFL_TYPE_GRABBER;
video_set_drvdata(dev_ctxt->vdev, &dev_ctxt);
strlcpy(dev_ctxt->mdev.model, MSM_BA_DRV_NAME,
sizeof(dev_ctxt->mdev.model));
dev_ctxt->mdev.dev = &pdev->dev;
rc = media_device_register(&dev_ctxt->mdev);
dev_ctxt->v4l2_dev.mdev = &dev_ctxt->mdev;
rc = media_entity_init(&dev_ctxt->vdev->entity,
0, NULL, 0);
dev_ctxt->vdev->entity.type = MEDIA_ENT_T_DEVNODE_V4L;
dev_ctxt->vdev->entity.group_id = 2;
rc = video_register_device(dev_ctxt->vdev,
VFL_TYPE_GRABBER, nr);
if (!rc) {
dev_ctxt->vdev->entity.name =
video_device_node_name(dev_ctxt->vdev);
*ret_dev_ctxt = dev_ctxt;
} else {
dprintk(BA_ERR,
"Failed to register BA video device");
}
}
} else {
dprintk(BA_ERR, "Failed to register v4l2 device");
}
if (rc) {
devm_kfree(&pdev->dev, dev_ctxt);
dev_ctxt = NULL;
}
dprintk(BA_INFO, "Exit %s with error %d", __func__, rc);
return rc;
}
static int msm_ba_probe(struct platform_device *pdev)
{
struct ba_ctxt *ba_ctxt;
int rc = 0;
dprintk(BA_INFO, "Enter %s: pdev %pK device id = %d",
__func__, pdev, pdev->id);
ba_ctxt = msm_ba_get_ba_context();
if (ba_ctxt == NULL) {
dprintk(BA_ERR, "BA context not yet created");
return -EINVAL;
}
rc = msm_ba_device_init(pdev, &ba_ctxt->dev_ctxt);
if (rc)
dprintk(BA_ERR, "Failed to init device");
else
ba_ctxt->dev_ctxt->debugfs_root = msm_ba_debugfs_init_dev(
ba_ctxt->dev_ctxt, ba_ctxt->debugfs_root);
rc = parse_ba_dt(pdev);
if (rc < 0) {
dprintk(BA_ERR, "%s: devicetree error. Exit init", __func__);
return rc;
}
dprintk(BA_INFO, "Exit %s with error %d", __func__, rc);
return rc;
}
static int msm_ba_remove(struct platform_device *pdev)
{
struct msm_ba_dev *dev_ctxt = platform_get_drvdata(pdev);
struct msm_ba_sd_event *ba_sd_event = NULL;
struct msm_ba_sd_event *ba_sd_event_tmp = NULL;
int rc = 0;
if (dev_ctxt == NULL) {
dprintk(BA_ERR, "%s invalid device", __func__);
rc = -EINVAL;
} else {
video_unregister_device(dev_ctxt->vdev);
v4l2_device_unregister(&dev_ctxt->v4l2_dev);
cancel_delayed_work_sync(&dev_ctxt->sd_events_work);
list_for_each_entry_safe(ba_sd_event, ba_sd_event_tmp,
&dev_ctxt->sd_events, list) {
list_del(&ba_sd_event->list);
kfree(ba_sd_event);
}
devm_kfree(&pdev->dev, dev_ctxt->msm_ba_inp_cfg);
devm_kfree(&pdev->dev, dev_ctxt);
dev_ctxt = NULL;
}
dprintk(BA_INFO, "Exit %s with error %d", __func__, rc);
return rc;
}
static int msm_ba_create(void)
{
struct ba_ctxt *ba_ctxt;
int rc = 0;
ba_ctxt = msm_ba_get_ba_context();
if (ba_ctxt != NULL) {
dprintk(BA_ERR, "BA context already created");
return -EINVAL;
}
ba_ctxt = kzalloc(sizeof(struct ba_ctxt), GFP_KERNEL);
if (ba_ctxt == NULL)
return -ENOMEM;
memset(ba_ctxt, 0x00, sizeof(struct ba_ctxt));
mutex_init(&ba_ctxt->ba_cs);
ba_ctxt->debugfs_root = msm_ba_debugfs_init_drv();
if (!ba_ctxt->debugfs_root)
dprintk(BA_ERR,
"Failed to create debugfs for msm_ba");
msm_ba_set_ba_context(ba_ctxt);
dprintk(BA_DBG, "%s(%d), BA create complete",
__func__, __LINE__);
return rc;
}
static int msm_ba_destroy(void)
{
struct ba_ctxt *ba_ctxt;
int rc = 0;
ba_ctxt = msm_ba_get_ba_context();
if (ba_ctxt == NULL) {
dprintk(BA_ERR, "BA context non existent");
return -EINVAL;
}
if (ba_ctxt->dev_ctxt != NULL) {
dprintk(BA_ERR, "Device instances exist on BA context");
return -EBUSY;
}
mutex_destroy(&ba_ctxt->ba_cs);
kfree(ba_ctxt);
ba_ctxt = NULL;
msm_ba_set_ba_context(ba_ctxt);
return rc;
}
static const struct of_device_id msm_ba_dt_match[] = {
{.compatible = "qcom,msm-ba"},
{}
};
MODULE_DEVICE_TABLE(of, msm_ba_dt_match);
static struct platform_driver msm_ba_driver = {
.probe = msm_ba_probe,
.remove = msm_ba_remove,
.driver = {
.name = "msm_ba_v4l2",
.owner = THIS_MODULE,
.of_match_table = msm_ba_dt_match,
},
};
static int __init msm_ba_mod_init(void)
{
int rc = 0;
dprintk(BA_INFO, "Enter %s", __func__);
rc = msm_ba_create();
if (!rc) {
rc = platform_driver_register(&msm_ba_driver);
if (rc) {
dprintk(BA_ERR,
"Failed to register platform driver");
msm_ba_destroy();
}
}
dprintk(BA_INFO, "Exit %s with error %d", __func__, rc);
return rc;
}
static void __exit msm_ba_mod_exit(void)
{
int rc = 0;
dprintk(BA_INFO, "Enter %s", __func__);
platform_driver_unregister(&msm_ba_driver);
rc = msm_ba_destroy();
dprintk(BA_INFO, "Exit %s", __func__);
}
module_init(msm_ba_mod_init);
module_exit(msm_ba_mod_exit);
|