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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
|
/*
* Copyright (c) 2012-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/module.h>
#include <linux/kernel.h>
#include <linux/init.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 <media/adv7481.h>
#include "msm_ba_internal.h"
#include "msm_ba_debug.h"
#include "msm_ba_common.h"
#define MSM_BA_DEV_NAME "msm_ba_8064"
#define MSM_BA_MAX_EVENTS 10
int msm_ba_poll(void *instance, struct file *filp,
struct poll_table_struct *wait)
{
struct msm_ba_inst *inst = instance;
int rc = 0;
if (!inst)
return -EINVAL;
poll_wait(filp, &inst->event_handler.wait, wait);
if (v4l2_event_pending(&inst->event_handler))
rc |= POLLPRI;
return rc;
}
EXPORT_SYMBOL(msm_ba_poll);
int msm_ba_querycap(void *instance, struct v4l2_capability *cap)
{
struct msm_ba_inst *inst = instance;
if (!inst || !cap) {
dprintk(BA_ERR,
"Invalid input, inst = 0x%pK, cap = 0x%pK", inst, cap);
return -EINVAL;
}
strlcpy(cap->driver, MSM_BA_DRV_NAME, sizeof(cap->driver));
strlcpy(cap->card, MSM_BA_DEV_NAME, sizeof(cap->card));
cap->bus_info[0] = 0;
cap->version = MSM_BA_VERSION;
cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
V4L2_CAP_STREAMING;
memset(cap->reserved, 0x00, sizeof(cap->reserved));
return 0;
}
EXPORT_SYMBOL(msm_ba_querycap);
int msm_ba_s_parm(void *instance, struct v4l2_streamparm *a)
{
struct msm_ba_inst *inst = instance;
if (!inst || !a)
return -EINVAL;
return -EINVAL;
}
EXPORT_SYMBOL(msm_ba_s_parm);
int msm_ba_enum_input(void *instance, struct v4l2_input *input)
{
struct msm_ba_input *ba_input = NULL;
struct msm_ba_inst *inst = instance;
int rc = 0;
if (!inst || !input)
return -EINVAL;
if (input->index >= inst->dev_ctxt->num_inputs)
return -EINVAL;
ba_input = msm_ba_find_input(input->index);
if (ba_input) {
input->type = V4L2_INPUT_TYPE_CAMERA;
input->std = V4L2_STD_ALL;
strlcpy(input->name, ba_input->name, sizeof(input->name));
if (ba_input->input_type == BA_INPUT_HDMI ||
ba_input->input_type == BA_INPUT_MHL)
input->capabilities = V4L2_IN_CAP_CUSTOM_TIMINGS;
else
input->capabilities = V4L2_IN_CAP_STD;
dprintk(BA_DBG, "msm_ba_find_input: name %s", input->name);
}
return rc;
}
EXPORT_SYMBOL(msm_ba_enum_input);
int msm_ba_g_input(void *instance, unsigned int *index)
{
struct msm_ba_inst *inst = instance;
struct msm_ba_input *ba_input = NULL;
int rc = 0;
if (!inst || !index)
return -EINVAL;
do {
/* First find current input */
ba_input = msm_ba_find_input(inst->sd_input.index);
if (ba_input) {
if (ba_input->input_user_type ==
BA_INPUT_USERTYPE_KERNEL) {
inst->sd_input.index++;
continue;
}
break;
}
} while (ba_input);
if (ba_input)
*index = inst->sd_input.index;
else
rc = -ENOENT;
return rc;
}
EXPORT_SYMBOL(msm_ba_g_input);
int msm_ba_s_input(void *instance, unsigned int index)
{
struct msm_ba_inst *inst = instance;
struct msm_ba_input *ba_input = NULL;
int rc = 0;
int rc_sig = 0;
if (!inst)
return -EINVAL;
if (index > inst->dev_ctxt->num_inputs)
return -EINVAL;
/* Find requested input */
ba_input = msm_ba_find_input(index);
if (!ba_input) {
dprintk(BA_ERR, "Could not find input index: %d", index);
return -EINVAL;
}
if (!ba_input->sd) {
dprintk(BA_ERR, "No sd registered");
return -EINVAL;
}
if (ba_input->in_use &&
inst->event_handler.prio == V4L2_PRIORITY_RECORD) {
dprintk(BA_WARN, "Input %d in use", index);
return -EBUSY;
}
if (ba_input->ba_out_in_use) {
if (inst->ext_ops) {
if (inst->restore) {
dprintk(BA_DBG, "Stream off in set input: %d",
ba_input->bridge_chip_ip);
rc_sig = v4l2_subdev_call(ba_input->sd,
video, s_stream, 0);
if (rc_sig)
dprintk(BA_ERR,
"%s: Error in stream off. rc_sig %d",
__func__, rc_sig);
}
} else {
dprintk(BA_WARN, "Sd %d in use", ba_input->ba_out);
return -EBUSY;
}
}
rc = v4l2_subdev_call(ba_input->sd, video, s_routing,
ba_input->bridge_chip_ip, 0, 0);
if (rc) {
dprintk(BA_ERR, "Error: %d setting input: %d",
rc, ba_input->bridge_chip_ip);
return rc;
}
msm_ba_reset_ip_in_use_from_sd(ba_input->sd);
inst->sd_input.index = index;
strlcpy(inst->sd_input.name, ba_input->name,
sizeof(inst->sd_input.name));
inst->sd = ba_input->sd;
ba_input->in_use = 1;
/* get current signal status */
rc_sig = v4l2_subdev_call(
ba_input->sd, video, g_input_status, &ba_input->signal_status);
dprintk(BA_DBG, "Set input %s : %d - signal status: %d",
ba_input->name, index, ba_input->signal_status);
if (!rc_sig && !ba_input->signal_status) {
struct v4l2_event sd_event = {
.id = 0,
.type = V4L2_EVENT_MSM_BA_SIGNAL_IN_LOCK};
int *ptr = (int *)sd_event.u.data;
ptr[0] = index;
ptr[1] = ba_input->signal_status;
msm_ba_queue_v4l2_event(inst, &sd_event);
}
return rc;
}
EXPORT_SYMBOL(msm_ba_s_input);
int msm_ba_enum_output(void *instance, struct v4l2_output *output)
{
struct msm_ba_input *ba_input = NULL;
struct msm_ba_inst *inst = instance;
int rc = 0;
if (!inst || !output)
return -EINVAL;
ba_input = msm_ba_find_output(output->index);
if (!ba_input)
return -EINVAL;
output->type = V4L2_OUTPUT_TYPE_ANALOG;
output->std = V4L2_STD_ALL;
strlcpy(output->name, ba_input->sd->name, sizeof(output->name));
output->capabilities = V4L2_OUT_CAP_STD;
return rc;
}
EXPORT_SYMBOL(msm_ba_enum_output);
int msm_ba_g_output(void *instance, unsigned int *index)
{
struct msm_ba_inst *inst = instance;
int rc = 0;
if (!inst || !index)
return -EINVAL;
*index = inst->sd_output.index;
return rc;
}
EXPORT_SYMBOL(msm_ba_g_output);
int msm_ba_s_output(void *instance, unsigned int index)
{
struct msm_ba_inst *inst = instance;
struct msm_ba_input *ba_input = NULL;
int rc = 0;
if (!inst)
return -EINVAL;
ba_input = msm_ba_find_output(index);
if (ba_input) {
if (!ba_input->sd) {
dprintk(BA_ERR, "No sd registered");
return -EINVAL;
}
ba_input->ba_out = index;
inst->sd_output.index = index;
inst->sd = ba_input->sd;
inst->sd_input.index = ba_input->ba_ip_idx;
} else {
dprintk(BA_ERR, "Could not find output index: %d", index);
rc = -EINVAL;
}
return rc;
}
EXPORT_SYMBOL(msm_ba_s_output);
int msm_ba_enum_fmt(void *instance, struct v4l2_fmtdesc *f)
{
struct msm_ba_inst *inst = instance;
if (!inst || !f)
return -EINVAL;
return -EINVAL;
}
EXPORT_SYMBOL(msm_ba_enum_fmt);
int msm_ba_s_fmt(void *instance, struct v4l2_format *f)
{
struct msm_ba_inst *inst = instance;
if (!inst || !f)
return -EINVAL;
return -EINVAL;
}
EXPORT_SYMBOL(msm_ba_s_fmt);
int msm_ba_g_fmt(void *instance, struct v4l2_format *f)
{
struct msm_ba_inst *inst = instance;
struct v4l2_subdev *sd = NULL;
struct msm_ba_input *ba_input = NULL;
v4l2_std_id new_std = V4L2_STD_UNKNOWN;
struct v4l2_dv_timings sd_dv_timings;
struct v4l2_subdev_format sd_fmt;
int rc = 0;
if (!inst || !f)
return -EINVAL;
sd = inst->sd;
if (!sd) {
dprintk(BA_ERR, "No sd registered");
return -EINVAL;
}
ba_input = msm_ba_find_input(inst->sd_input.index);
if (!ba_input) {
dprintk(BA_ERR, "Could not find input index: %d",
inst->sd_input.index);
return -EINVAL;
}
if (ba_input->input_type != BA_INPUT_HDMI) {
rc = v4l2_subdev_call(sd, video, querystd, &new_std);
if (rc) {
dprintk(BA_ERR, "querystd failed %d for sd: %s",
rc, sd->name);
return -EINVAL;
}
inst->sd_input.std = new_std;
} else {
rc = v4l2_subdev_call(sd, video, g_dv_timings, &sd_dv_timings);
if (rc) {
dprintk(BA_ERR, "g_dv_timings failed %d for sd: %s",
rc, sd->name);
return -EINVAL;
}
}
rc = v4l2_subdev_call(sd, pad, get_fmt, NULL, &sd_fmt);
if (rc) {
dprintk(BA_ERR, "get_fmt failed %d for sd: %s",
rc, sd->name);
} else {
f->fmt.pix.height = sd_fmt.format.height;
f->fmt.pix.width = sd_fmt.format.width;
f->fmt.pix.field = sd_fmt.format.field;
switch (sd_fmt.format.code) {
case MEDIA_BUS_FMT_YUYV8_2X8:
f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
break;
case MEDIA_BUS_FMT_YVYU8_2X8:
f->fmt.pix.pixelformat = V4L2_PIX_FMT_YVYU;
break;
case MEDIA_BUS_FMT_VYUY8_2X8:
f->fmt.pix.pixelformat = V4L2_PIX_FMT_VYUY;
break;
case MEDIA_BUS_FMT_UYVY8_2X8:
f->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
break;
default:
dprintk(BA_ERR, "Unknown sd_mbus_fmt.code 0x%x",
sd_fmt.format.code);
f->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
break;
}
}
return rc;
}
EXPORT_SYMBOL(msm_ba_g_fmt);
int msm_ba_s_ctrl(void *instance, struct v4l2_control *control)
{
struct msm_ba_inst *inst = instance;
if (!inst || !control)
return -EINVAL;
return v4l2_s_ctrl(NULL, &inst->ctrl_handler, control);
}
EXPORT_SYMBOL(msm_ba_s_ctrl);
int msm_ba_g_ctrl(void *instance, struct v4l2_control *control)
{
struct msm_ba_inst *inst = instance;
if (!inst || !control)
return -EINVAL;
return v4l2_g_ctrl(&inst->ctrl_handler, control);
}
EXPORT_SYMBOL(msm_ba_g_ctrl);
int msm_ba_s_ext_ctrl(void *instance, struct v4l2_ext_controls *control)
{
struct msm_ba_inst *inst = instance;
if (!inst || !control)
return -EINVAL;
return -EINVAL;
}
EXPORT_SYMBOL(msm_ba_s_ext_ctrl);
int msm_ba_streamon(void *instance, enum v4l2_buf_type i)
{
struct msm_ba_inst *inst = instance;
struct v4l2_subdev *sd = NULL;
int rc = 0;
if (!inst)
return -EINVAL;
sd = inst->sd;
if (!sd) {
dprintk(BA_ERR, "No sd registered");
return -EINVAL;
}
rc = v4l2_subdev_call(sd, video, s_stream, 1);
if (rc)
dprintk(BA_ERR, "Stream on failed on input: %d",
inst->sd_input.index);
else
msm_ba_set_out_in_use(sd, 1);
dprintk(BA_DBG, "Stream on: %s : %d",
inst->sd_input.name, inst->sd_input.index);
return rc;
}
EXPORT_SYMBOL(msm_ba_streamon);
int msm_ba_streamoff(void *instance, enum v4l2_buf_type i)
{
struct msm_ba_inst *inst = instance;
struct v4l2_subdev *sd = NULL;
int rc = 0;
if (!inst)
return -EINVAL;
sd = inst->sd;
if (!sd) {
dprintk(BA_ERR, "No sd registered");
return -EINVAL;
}
rc = v4l2_subdev_call(sd, video, s_stream, 0);
if (rc)
dprintk(BA_ERR, "Stream off failed on input: %d",
inst->sd_input.index);
dprintk(BA_DBG, "Stream off: %s : %d",
inst->sd_input.name, inst->sd_input.index);
msm_ba_set_out_in_use(sd, 0);
return rc;
}
EXPORT_SYMBOL(msm_ba_streamoff);
long msm_ba_private_ioctl(void *instance, int cmd, void *arg)
{
long rc = 0;
struct msm_ba_inst *inst = instance;
struct v4l2_subdev *sd = NULL;
int *s_ioctl = arg;
dprintk(BA_DBG, "Enter %s with command: 0x%x", __func__, cmd);
if (!inst)
return -EINVAL;
switch (cmd) {
case VIDIOC_HDMI_RX_CEC_S_LOGICAL: {
dprintk(BA_DBG, "VIDIOC_HDMI_RX_CEC_S_LOGICAL");
sd = inst->sd;
if (!sd) {
dprintk(BA_ERR, "No sd registered");
return -EINVAL;
}
if (s_ioctl) {
rc = v4l2_subdev_call(sd, core, ioctl, cmd, s_ioctl);
if (rc)
dprintk(BA_ERR, "%s failed: %ld on cmd: 0x%x",
__func__, rc, cmd);
} else {
dprintk(BA_ERR, "%s: NULL argument provided", __func__);
rc = -EINVAL;
}
}
break;
case VIDIOC_HDMI_RX_CEC_CLEAR_LOGICAL: {
dprintk(BA_DBG, "VIDIOC_HDMI_RX_CEC_CLEAR_LOGICAL");
sd = inst->sd;
if (!sd) {
dprintk(BA_ERR, "No sd registered");
return -EINVAL;
}
rc = v4l2_subdev_call(sd, core, ioctl, cmd, s_ioctl);
if (rc)
dprintk(BA_ERR, "%s failed: %ld on cmd: 0x%x",
__func__, rc, cmd);
}
break;
case VIDIOC_HDMI_RX_CEC_G_PHYSICAL: {
dprintk(BA_DBG, "VIDIOC_HDMI_RX_CEC_G_PHYSICAL");
sd = inst->sd;
if (!sd) {
dprintk(BA_ERR, "No sd registered");
return -EINVAL;
}
if (s_ioctl) {
rc = v4l2_subdev_call(sd, core, ioctl, cmd, s_ioctl);
if (rc)
dprintk(BA_ERR, "%s failed: %ld on cmd: 0x%x",
__func__, rc, cmd);
} else {
dprintk(BA_ERR, "%s: NULL argument provided", __func__);
rc = -EINVAL;
}
}
break;
case VIDIOC_HDMI_RX_CEC_G_CONNECTED: {
dprintk(BA_DBG, "VIDIOC_HDMI_RX_CEC_G_CONNECTED");
sd = inst->sd;
if (!sd) {
dprintk(BA_ERR, "No sd registered");
return -EINVAL;
}
if (s_ioctl) {
rc = v4l2_subdev_call(sd, core, ioctl, cmd, s_ioctl);
if (rc)
dprintk(BA_ERR, "%s failed: %ld on cmd: 0x%x",
__func__, rc, cmd);
} else {
dprintk(BA_ERR, "%s: NULL argument provided", __func__);
rc = -EINVAL;
}
}
break;
case VIDIOC_HDMI_RX_CEC_S_ENABLE: {
dprintk(BA_DBG, "VIDIOC_HDMI_RX_CEC_S_ENABLE");
sd = inst->sd;
if (!sd) {
dprintk(BA_ERR, "No sd registered");
return -EINVAL;
}
if (s_ioctl) {
rc = v4l2_subdev_call(sd, core, ioctl, cmd, s_ioctl);
if (rc)
dprintk(BA_ERR, "%s failed: %ld on cmd: 0x%x",
__func__, rc, cmd);
} else {
dprintk(BA_ERR, "%s: NULL argument provided", __func__);
rc = -EINVAL;
}
}
break;
case VIDIOC_G_CSI_PARAMS: {
dprintk(BA_DBG, "VIDIOC_G_CSI_PARAMS");
sd = inst->sd;
if (!sd) {
dprintk(BA_ERR, "No sd registered");
return -EINVAL;
}
if (arg) {
rc = v4l2_subdev_call(sd, core, ioctl, cmd, arg);
if (rc)
dprintk(BA_ERR, "%s failed: %ld on cmd: 0x%x",
__func__, rc, cmd);
} else {
dprintk(BA_ERR, "%s: NULL argument provided", __func__);
rc = -EINVAL;
}
}
break;
case VIDIOC_G_AVI_INFOFRAME: {
dprintk(BA_DBG, "VIDIOC_G_AVI_INFOFRAME\n");
sd = inst->sd;
if (!sd) {
dprintk(BA_ERR, "No sd registered");
return -EINVAL;
}
if (arg) {
rc = v4l2_subdev_call(sd, core, ioctl, cmd, arg);
if (rc)
dprintk(BA_ERR, "%s failed: %ld on cmd: 0x%x",
__func__, rc, cmd);
} else {
dprintk(BA_ERR, "%s: NULL argument provided", __func__);
rc = -EINVAL;
}
}
break;
case VIDIOC_G_FIELD_INFO: {
dprintk(BA_DBG, "VIDIOC_G_FIELD_INFO");
sd = inst->sd;
if (!sd) {
dprintk(BA_ERR, "No sd registered");
return -EINVAL;
}
if (arg) {
rc = v4l2_subdev_call(sd, core, ioctl, cmd, arg);
if (rc)
dprintk(BA_ERR, "%s failed: %ld on cmd: 0x%x",
__func__, rc, cmd);
} else {
dprintk(BA_ERR, "%s: NULL argument provided", __func__);
rc = -EINVAL;
}
}
break;
default:
dprintk(BA_WARN, "Not a typewriter! Command: 0x%x", cmd);
rc = -ENOTTY;
break;
}
return rc;
}
EXPORT_SYMBOL(msm_ba_private_ioctl);
int msm_ba_save_restore_input(void *instance, enum msm_ba_save_restore_ip sr)
{
struct msm_ba_inst *inst = instance;
struct msm_ba_input *ba_input = NULL;
int rc = 0;
if (!inst)
return -EINVAL;
if (sr == BA_SR_RESTORE_IP &&
inst->restore) {
dprintk(BA_DBG, "Restoring input: %d",
inst->saved_input);
rc = v4l2_subdev_call(inst->sd, video, s_routing,
inst->saved_input, 0, 0);
if (rc)
dprintk(BA_ERR, "Failed to restore input: %d",
inst->saved_input);
msm_ba_reset_ip_in_use_from_sd(inst->sd);
ba_input = msm_ba_find_input_from_sd(inst->sd,
inst->saved_input);
if (ba_input)
ba_input->in_use = 1;
else
dprintk(BA_WARN, "Could not find input %d from sd: %s",
inst->saved_input, inst->sd->name);
inst->restore = 0;
inst->saved_input = BA_IP_MAX;
dprintk(BA_DBG, "Stream on from save restore");
rc = msm_ba_streamon(inst, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
} else if (sr == BA_SR_SAVE_IP) {
ba_input = msm_ba_find_input(inst->sd_input.index);
if (ba_input == NULL) {
dprintk(BA_ERR, "Could not find input %d",
inst->sd_input.index);
} else if (ba_input->ba_out_in_use) {
inst->restore = 1;
inst->saved_input =
msm_ba_find_ip_in_use_from_sd(inst->sd);
if (inst->saved_input == BA_IP_MAX) {
dprintk(BA_ERR, "Could not find input to save");
inst->restore = 0;
}
dprintk(BA_DBG, "Saving input: %d",
inst->saved_input);
rc = -EBUSY;
}
} else {
dprintk(BA_DBG, "Nothing to do in save and restore");
}
return rc;
}
EXPORT_SYMBOL(msm_ba_save_restore_input);
static void msm_ba_release_subdev_node(struct video_device *vdev)
{
struct v4l2_subdev *sd = video_get_drvdata(vdev);
sd->devnode = NULL;
kfree(vdev);
}
static int msm_ba_register_v4l2_subdev(struct v4l2_device *v4l2_dev,
struct v4l2_subdev *sd)
{
struct video_device *vdev;
int rc = 0;
dprintk(BA_DBG, "Enter %s: v4l2_dev 0x%pK, v4l2_subdev 0x%pK",
__func__, v4l2_dev, sd);
if (NULL == v4l2_dev || NULL == sd || !sd->name[0]) {
dprintk(BA_ERR, "Invalid input");
return -EINVAL;
}
rc = v4l2_device_register_subdev(v4l2_dev, sd);
if (rc < 0) {
dprintk(BA_ERR,
"%s(%d), V4L2 subdev register failed for %s rc: %d",
__func__, __LINE__, sd->name, rc);
return rc;
}
if (sd->flags & V4L2_SUBDEV_FL_HAS_DEVNODE) {
vdev = video_device_alloc();
if (vdev == NULL) {
dprintk(BA_ERR, "%s Not enough memory", __func__);
return -ENOMEM;
}
video_set_drvdata(vdev, sd);
strlcpy(vdev->name, sd->name, sizeof(vdev->name));
vdev->v4l2_dev = v4l2_dev;
vdev->fops = &v4l2_subdev_fops;
vdev->release = msm_ba_release_subdev_node;
rc = __video_register_device(vdev, VFL_TYPE_SUBDEV, -1, 1,
sd->owner);
if (rc < 0) {
dprintk(BA_ERR, "%s Error registering video device %s",
__func__, sd->name);
kfree(vdev);
} else {
#if defined(CONFIG_MEDIA_CONTROLLER)
sd->entity.info.dev.major = VIDEO_MAJOR;
sd->entity.info.dev.minor = vdev->minor;
sd->entity.name = video_device_node_name(vdev);
#endif
sd->devnode = vdev;
}
}
dprintk(BA_DBG, "Exit %s with rc: %d", __func__, rc);
return rc;
}
int msm_ba_register_subdev_node(struct v4l2_subdev *sd)
{
struct ba_ctxt *ba_ctxt;
int rc = 0;
ba_ctxt = msm_ba_get_ba_context();
rc = msm_ba_register_v4l2_subdev(&ba_ctxt->dev_ctxt->v4l2_dev, sd);
if (!rc) {
ba_ctxt->dev_ctxt->num_ba_subdevs++;
msm_ba_add_inputs(sd);
}
return rc;
}
EXPORT_SYMBOL(msm_ba_register_subdev_node);
static void __msm_ba_sd_unregister(struct v4l2_subdev *sub_dev)
{
struct ba_ctxt *ba_ctxt;
ba_ctxt = msm_ba_get_ba_context();
mutex_lock(&ba_ctxt->ba_cs);
v4l2_device_unregister_subdev(sub_dev);
ba_ctxt->dev_ctxt->num_ba_subdevs--;
msm_ba_del_inputs(sub_dev);
dprintk(BA_DBG, "%s(%d), BA Unreg Sub Device : num ba devices %d : %s",
__func__, __LINE__,
ba_ctxt->dev_ctxt->num_ba_subdevs, sub_dev->name);
mutex_unlock(&ba_ctxt->ba_cs);
}
int msm_ba_unregister_subdev_node(struct v4l2_subdev *sub_dev)
{
struct ba_ctxt *ba_ctxt;
ba_ctxt = msm_ba_get_ba_context();
if (!ba_ctxt || !ba_ctxt->dev_ctxt)
return -ENODEV;
if (!sub_dev)
return -EINVAL;
__msm_ba_sd_unregister(sub_dev);
return 0;
}
EXPORT_SYMBOL(msm_ba_unregister_subdev_node);
static int msm_ba_setup_event_queue(void *inst,
struct video_device *pvdev)
{
int rc = 0;
struct msm_ba_inst *ba_inst = (struct msm_ba_inst *)inst;
v4l2_fh_init(&ba_inst->event_handler, pvdev);
v4l2_fh_add(&ba_inst->event_handler);
return rc;
}
int msm_ba_subscribe_event(void *inst,
const struct v4l2_event_subscription *sub)
{
int rc = 0;
struct msm_ba_inst *ba_inst = (struct msm_ba_inst *)inst;
if (!inst || !sub)
return -EINVAL;
rc = v4l2_event_subscribe(&ba_inst->event_handler, sub,
MSM_BA_MAX_EVENTS, NULL);
return rc;
}
EXPORT_SYMBOL(msm_ba_subscribe_event);
int msm_ba_unsubscribe_event(void *inst,
const struct v4l2_event_subscription *sub)
{
int rc = 0;
struct msm_ba_inst *ba_inst = (struct msm_ba_inst *)inst;
if (!inst || !sub)
return -EINVAL;
rc = v4l2_event_unsubscribe(&ba_inst->event_handler, sub);
return rc;
}
EXPORT_SYMBOL(msm_ba_unsubscribe_event);
void msm_ba_subdev_event_hndlr(struct v4l2_subdev *sd,
unsigned int notification, void *arg)
{
struct msm_ba_dev *dev_ctxt = NULL;
struct msm_ba_input *ba_input;
struct msm_ba_sd_event *ba_sd_event;
int bridge_chip_ip;
if (!sd || !arg) {
dprintk(BA_ERR, "%s null v4l2 subdev or arg", __func__);
return;
}
bridge_chip_ip = ((int *)((struct v4l2_event *)arg)->u.data)[0];
ba_input = msm_ba_find_input_from_sd(sd, bridge_chip_ip);
if (!ba_input) {
dprintk(BA_WARN, "Could not find input %d from sd: %s",
bridge_chip_ip, sd->name);
return;
}
ba_sd_event = kzalloc(sizeof(*ba_sd_event), GFP_KERNEL);
if (!ba_sd_event) {
dprintk(BA_ERR, "%s out of memory", __func__);
return;
}
dev_ctxt = get_ba_dev();
ba_sd_event->sd_event = *(struct v4l2_event *)arg;
((int *)ba_sd_event->sd_event.u.data)[0] = ba_input->ba_ip_idx;
mutex_lock(&dev_ctxt->dev_cs);
list_add_tail(&ba_sd_event->list, &dev_ctxt->sd_events);
mutex_unlock(&dev_ctxt->dev_cs);
schedule_delayed_work(&dev_ctxt->sd_events_work, 0);
}
void *msm_ba_open(const struct msm_ba_ext_ops *ext_ops)
{
struct msm_ba_inst *inst = NULL;
struct msm_ba_dev *dev_ctxt = NULL;
int rc = 0;
dev_ctxt = get_ba_dev();
if (!dev_ctxt) {
dprintk(BA_ERR, "Failed to get ba dev");
return NULL;
}
inst = kzalloc(sizeof(*inst), GFP_KERNEL);
if (!inst) {
dprintk(BA_ERR, "Failed to allocate memory");
return NULL;
}
mutex_init(&inst->inst_cs);
init_waitqueue_head(&inst->kernel_event_queue);
inst->state = MSM_BA_DEV_UNINIT_DONE;
inst->dev_ctxt = dev_ctxt;
rc = msm_ba_ctrl_init(inst);
if (rc) {
dprintk(BA_WARN, "Failed to initialize controls: %d", rc);
msm_ba_ctrl_deinit(inst);
}
if (!list_empty(&(inst->dev_ctxt->v4l2_dev.subdevs)))
inst->sd = list_first_entry(&(inst->dev_ctxt->v4l2_dev.subdevs),
struct v4l2_subdev, list);
msm_ba_setup_event_queue(inst, dev_ctxt->vdev);
mutex_lock(&dev_ctxt->dev_cs);
list_add_tail(&inst->list, &dev_ctxt->instances);
mutex_unlock(&dev_ctxt->dev_cs);
dev_ctxt->state = BA_DEV_INIT;
dev_ctxt->state = BA_DEV_INIT_DONE;
inst->state = MSM_BA_DEV_INIT_DONE;
inst->sd_input.index = 0;
inst->event_handler.prio = V4L2_PRIORITY_DEFAULT;
inst->debugfs_root =
msm_ba_debugfs_init_inst(inst, dev_ctxt->debugfs_root);
inst->ext_ops = ext_ops;
return inst;
}
EXPORT_SYMBOL(msm_ba_open);
int msm_ba_close(void *instance)
{
struct msm_ba_inst *inst = instance;
struct msm_ba_inst *temp;
struct msm_ba_dev *dev_ctxt;
struct list_head *ptr;
struct list_head *next;
int rc = 0;
if (!inst)
return -EINVAL;
dev_ctxt = inst->dev_ctxt;
mutex_lock(&dev_ctxt->dev_cs);
list_for_each_safe(ptr, next, &dev_ctxt->instances) {
temp = list_entry(ptr, struct msm_ba_inst, list);
if (temp == inst)
list_del(&inst->list);
}
mutex_unlock(&dev_ctxt->dev_cs);
msm_ba_ctrl_deinit(inst);
v4l2_fh_del(&inst->event_handler);
v4l2_fh_exit(&inst->event_handler);
debugfs_remove_recursive(inst->debugfs_root);
dprintk(BA_DBG, "Closed BA instance: %pK", inst);
kfree(inst);
return rc;
}
EXPORT_SYMBOL(msm_ba_close);
|