-
Notifications
You must be signed in to change notification settings - Fork 4
/
msm.c
executable file
·2869 lines (2474 loc) · 76.4 KB
/
msm.c
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
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2011-2012, Code Aurora Forum. 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/workqueue.h>
#include <linux/wakelock.h>
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/types.h>
#include <linux/list.h>
#include <linux/ioctl.h>
#include <linux/spinlock.h>
#include <linux/proc_fs.h>
#include "msm.h"
/* extern void sensor_native_control_front(void __user *arg);aswoogi_zsl */
#define MSM_MAX_CAMERA_SENSORS 5
#ifdef CONFIG_MSM_CAMERA_DEBUG
#define D(fmt, args...) pr_debug("msm: " fmt, ##args)
#else
#define D(fmt, args...) do {} while (0)
#endif
static unsigned msm_camera_v4l2_nr = -1;
static struct msm_cam_server_dev g_server_dev;
static struct class *msm_class;
static dev_t msm_devno;
static int vnode_count;
module_param(msm_camera_v4l2_nr, uint, 0644);
MODULE_PARM_DESC(msm_camera_v4l2_nr, "videoX start number, -1 is autodetect");
static int msm_setup_v4l2_event_queue(struct v4l2_fh *eventHandle,
struct video_device *pvdev);
static void msm_queue_init(struct msm_device_queue *queue, const char *name)
{
D("%s\n", __func__);
spin_lock_init(&queue->lock);
queue->len = 0;
queue->max = 0;
queue->name = name;
INIT_LIST_HEAD(&queue->list);
init_waitqueue_head(&queue->wait);
}
static void msm_enqueue(struct msm_device_queue *queue,
struct list_head *entry)
{
unsigned long flags;
spin_lock_irqsave(&queue->lock, flags);
queue->len++;
if (queue->len > queue->max) {
queue->max = queue->len;
pr_info("%s: queue %s new max is %d\n", __func__,
queue->name, queue->max);
}
list_add_tail(entry, &queue->list);
wake_up(&queue->wait);
D("%s: woke up %s\n", __func__, queue->name);
spin_unlock_irqrestore(&queue->lock, flags);
}
/* callback function from all subdevices of a msm_cam_v4l2_device */
static void msm_cam_v4l2_subdev_notify(struct v4l2_subdev *sd,
unsigned int notification, void *arg)
{
struct msm_cam_v4l2_device *pcam;
if (sd == NULL)
return;
pcam = to_pcam(sd->v4l2_dev);
if (pcam == NULL)
return;
/* forward to media controller for any changes*/
if (pcam->mctl.mctl_notify)
pcam->mctl.mctl_notify(&pcam->mctl, notification, arg);
}
static int msm_ctrl_cmd_done(void __user *arg)
{
void __user *uptr;
struct msm_queue_cmd *qcmd;
struct msm_ctrl_cmd *command = &g_server_dev.ctrl;
D("%s\n", __func__);
if (copy_from_user(command, arg,
sizeof(struct msm_ctrl_cmd)))
return -EINVAL;
qcmd = kzalloc(sizeof(struct msm_queue_cmd), GFP_KERNEL);
atomic_set(&qcmd->on_heap, 1);
uptr = command->value;
qcmd->command = command;
if (command->length > 0) {
command->value = g_server_dev.ctrl_data;
if (command->length > sizeof(g_server_dev.ctrl_data)) {
pr_err("%s: user data %d is too big (max %d)\n",
__func__, command->length,
sizeof(g_server_dev.ctrl_data));
free_qcmd(qcmd);
return -EINVAL;
}
if (copy_from_user(command->value, uptr, command->length)) {
free_qcmd(qcmd);
return -EINVAL;
}
}
msm_enqueue(&g_server_dev.ctrl_q, &qcmd->list_control);
return 0;
}
/* send control command to config and wait for results*/
static int msm_server_control(struct msm_cam_server_dev *server_dev,
struct msm_ctrl_cmd *out)
{
int rc = 0;
void *value;
struct msm_queue_cmd *rcmd;
struct msm_ctrl_cmd *ctrlcmd;
struct msm_device_queue *queue = &server_dev->ctrl_q;
void *ctrlcmd_data;
struct v4l2_event v4l2_evt;
struct msm_isp_event_ctrl *isp_event;
isp_event = kzalloc(sizeof(struct msm_isp_event_ctrl), GFP_KERNEL);
if (!isp_event) {
pr_err("%s Insufficient memory. return", __func__);
return -ENOMEM;
}
D("%s\n", __func__);
g_server_dev.server_evt_id++;
if (g_server_dev.server_evt_id == 0)
g_server_dev.server_evt_id++;
isp_event->evt_id = g_server_dev.server_evt_id;
v4l2_evt.type = V4L2_EVENT_PRIVATE_START + MSM_CAM_RESP_V4L2;
v4l2_evt.id = 0;
/* setup event object to transfer the command; */
*((uint32_t *)v4l2_evt.u.data) = (uint32_t)isp_event;
isp_event->resptype = MSM_CAM_RESP_V4L2;
isp_event->isp_data.ctrl = *out;
if (out->length > 0 && out->value != NULL) {
ctrlcmd_data = kzalloc(out->length, GFP_KERNEL);
if (!ctrlcmd_data) {
kfree(isp_event);
pr_err("%s Insufficient memory. return", __func__);
return -ENOMEM;
}
memcpy(ctrlcmd_data, out->value, out->length);
isp_event->isp_data.ctrl.value = ctrlcmd_data;
}
/* now send command to config thread in userspace,
* and wait for results */
v4l2_event_queue(server_dev->server_command_queue.pvdev,
&v4l2_evt);
D("%s v4l2_event_queue: type = 0x%x\n", __func__, v4l2_evt.type);
/* wait for config return status */
D("Waiting for config status\n");
rc = wait_event_interruptible_timeout(queue->wait,
!list_empty_careful(&queue->list),
msecs_to_jiffies(out->timeout_ms));
D("Waiting is over for config status\n");
if (list_empty_careful(&queue->list)) {
if (!rc)
rc = -ETIMEDOUT;
if (rc < 0) {
if (g_server_dev.server_evt_id == 0)
g_server_dev.server_evt_id++;
pr_err("%s: wait_event error %d\n", __func__, rc);
return rc;
}
}
rcmd = msm_dequeue(queue, list_control);
BUG_ON(!rcmd);
D("%s Finished servicing ioctl\n", __func__);
ctrlcmd = (struct msm_ctrl_cmd *)(rcmd->command);
value = out->value;
if (ctrlcmd->length > 0)
memcpy(value, ctrlcmd->value, ctrlcmd->length);
memcpy(out, ctrlcmd, sizeof(struct msm_ctrl_cmd));
out->value = value;
if (rcmd) {
free_qcmd(rcmd);
rcmd = NULL;
}
D("%s: rc %d\n", __func__, rc);
/* rc is the time elapsed. */
if (rc >= 0) {
/* TODO: Refactor msm_ctrl_cmd::status field */
if (out->status == 0)
rc = -1;
else if (out->status == 1 || out->status == 4)
rc = 0;
else
rc = -EINVAL;
}
return rc;
}
/*send open command to server*/
static int msm_send_open_server(int vnode_id)
{
int rc = 0;
struct msm_ctrl_cmd ctrlcmd;
D("%s\n", __func__);
ctrlcmd.type = MSM_V4L2_OPEN;
ctrlcmd.timeout_ms = 5000;
ctrlcmd.length = strnlen(g_server_dev.config_info.config_dev_name[0],
MAX_DEV_NAME_LEN)+1;
ctrlcmd.value = (char *)g_server_dev.config_info.config_dev_name[0];
ctrlcmd.vnode_id = vnode_id;
ctrlcmd.config_ident = g_server_dev.config_info.config_dev_id[0];
/* send command to config thread in usersspace, and get return value */
rc = msm_server_control(&g_server_dev, &ctrlcmd);
return rc;
}
static int msm_send_close_server(int vnode_id)
{
int rc = 0;
struct msm_ctrl_cmd ctrlcmd;
D("%s\n", __func__);
ctrlcmd.type = MSM_V4L2_CLOSE;
ctrlcmd.timeout_ms = 10000;
ctrlcmd.length = strnlen(g_server_dev.config_info.config_dev_name[0],
MAX_DEV_NAME_LEN)+1;
ctrlcmd.value = (char *)g_server_dev.config_info.config_dev_name[0];
ctrlcmd.vnode_id = vnode_id;
ctrlcmd.config_ident = g_server_dev.config_info.config_dev_id[0];
/* send command to config thread in usersspace, and get return value */
rc = msm_server_control(&g_server_dev, &ctrlcmd);
return rc;
}
static int msm_server_set_fmt(struct msm_cam_v4l2_device *pcam, int idx,
struct v4l2_format *pfmt)
{
int rc = 0;
int i = 0;
struct v4l2_pix_format *pix = &pfmt->fmt.pix;
struct msm_ctrl_cmd ctrlcmd;
struct img_plane_info plane_info;
plane_info.width = pix->width;
plane_info.height = pix->height;
plane_info.pixelformat = pix->pixelformat;
plane_info.buffer_type = pfmt->type;
plane_info.ext_mode = pcam->dev_inst[idx]->image_mode;
plane_info.num_planes = 1;
D("%s: %d, %d, 0x%x\n", __func__,
pfmt->fmt.pix.width, pfmt->fmt.pix.height,
pfmt->fmt.pix.pixelformat);
if (pfmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
D("%s, Attention! Wrong buf-type %d\n", __func__, pfmt->type);
for (i = 0; i < pcam->num_fmts; i++)
if (pcam->usr_fmts[i].fourcc == pix->pixelformat)
break;
if (i == pcam->num_fmts) {
pr_err("%s: User requested pixelformat %x not supported\n",
__func__, pix->pixelformat);
return -EINVAL;
}
ctrlcmd.type = MSM_V4L2_VID_CAP_TYPE;
ctrlcmd.length = sizeof(struct img_plane_info);
ctrlcmd.value = (void *)&plane_info;
ctrlcmd.timeout_ms = 10000;
ctrlcmd.vnode_id = pcam->vnode_id;
ctrlcmd.config_ident = g_server_dev.config_info.config_dev_id[0];
/* send command to config thread in usersspace, and get return value */
rc = msm_server_control(&g_server_dev, &ctrlcmd);
if (rc >= 0) {
pcam->dev_inst[idx]->vid_fmt = *pfmt;
pcam->dev_inst[idx]->sensor_pxlcode
= pcam->usr_fmts[i].pxlcode;
D("%s:inst=0x%x,idx=%d,width=%d,heigth=%d\n",
__func__, (u32)pcam->dev_inst[idx], idx,
pcam->dev_inst[idx]->vid_fmt.fmt.pix.width,
pcam->dev_inst[idx]->vid_fmt.fmt.pix.height);
pcam->dev_inst[idx]->plane_info = plane_info;
}
return rc;
}
static int msm_server_set_fmt_mplane(struct msm_cam_v4l2_device *pcam, int idx,
struct v4l2_format *pfmt)
{
int rc = 0;
int i = 0;
struct v4l2_pix_format_mplane *pix_mp = &pfmt->fmt.pix_mp;
struct msm_ctrl_cmd ctrlcmd;
struct img_plane_info plane_info;
plane_info.width = pix_mp->width;
plane_info.height = pix_mp->height;
plane_info.pixelformat = pix_mp->pixelformat;
plane_info.buffer_type = pfmt->type;
plane_info.ext_mode = pcam->dev_inst[idx]->image_mode;
plane_info.num_planes = pix_mp->num_planes;
if (plane_info.num_planes <= 0 ||
plane_info.num_planes > VIDEO_MAX_PLANES) {
pr_err("%s Invalid number of planes set %d", __func__,
plane_info.num_planes);
return -EINVAL;
}
D("%s: %d, %d, 0x%x\n", __func__,
pfmt->fmt.pix_mp.width, pfmt->fmt.pix_mp.height,
pfmt->fmt.pix_mp.pixelformat);
if (pfmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
pr_err("%s, Attention! Wrong buf-type %d\n",
__func__, pfmt->type);
return -EINVAL;
}
for (i = 0; i < pcam->num_fmts; i++)
if (pcam->usr_fmts[i].fourcc == pix_mp->pixelformat)
break;
if (i == pcam->num_fmts) {
pr_err("%s: User requested pixelformat %x not supported\n",
__func__, pix_mp->pixelformat);
return -EINVAL;
}
ctrlcmd.type = MSM_V4L2_VID_CAP_TYPE;
ctrlcmd.length = sizeof(struct img_plane_info);
ctrlcmd.value = (void *)&plane_info;
ctrlcmd.timeout_ms = 10000;
ctrlcmd.vnode_id = pcam->vnode_id;
/* send command to config thread in usersspace, and get return value */
rc = msm_server_control(&g_server_dev, &ctrlcmd);
if (rc >= 0) {
pcam->dev_inst[idx]->vid_fmt = *pfmt;
pcam->dev_inst[idx]->sensor_pxlcode
= pcam->usr_fmts[i].pxlcode;
D("%s:inst=0x%x,idx=%d,width=%d,heigth=%d\n",
__func__, (u32)pcam->dev_inst[idx], idx,
pcam->dev_inst[idx]->vid_fmt.fmt.pix_mp.width,
pcam->dev_inst[idx]->vid_fmt.fmt.pix_mp.height);
pcam->dev_inst[idx]->plane_info = plane_info;
}
return rc;
}
static int msm_server_streamon(struct msm_cam_v4l2_device *pcam, int idx)
{
int rc = 0;
struct msm_ctrl_cmd ctrlcmd;
D("%s\n", __func__);
ctrlcmd.type = MSM_V4L2_STREAM_ON;
ctrlcmd.timeout_ms = 3000;
ctrlcmd.length = 0;
ctrlcmd.value = NULL;
ctrlcmd.stream_type = pcam->dev_inst[idx]->image_mode;
ctrlcmd.vnode_id = pcam->vnode_id;
ctrlcmd.config_ident = g_server_dev.config_info.config_dev_id[0];
/* send command to config thread in usersspace, and get return value */
rc = msm_server_control(&g_server_dev, &ctrlcmd);
return rc;
}
static int msm_server_streamoff(struct msm_cam_v4l2_device *pcam, int idx)
{
int rc = 0;
struct msm_ctrl_cmd ctrlcmd;
D("%s, pcam = 0x%x\n", __func__, (u32)pcam);
ctrlcmd.type = MSM_V4L2_STREAM_OFF;
ctrlcmd.timeout_ms = 3000;
ctrlcmd.length = 0;
ctrlcmd.value = NULL;
ctrlcmd.stream_type = pcam->dev_inst[idx]->image_mode;
ctrlcmd.vnode_id = pcam->vnode_id;
ctrlcmd.config_ident = g_server_dev.config_info.config_dev_id[0];
/* send command to config thread in usersspace, and get return value */
rc = msm_server_control(&g_server_dev, &ctrlcmd);
return rc;
}
static int msm_server_proc_ctrl_cmd(struct msm_cam_v4l2_device *pcam,
struct v4l2_control *ctrl, int is_set_cmd)
{
int rc = 0;
struct msm_ctrl_cmd ctrlcmd, *tmp_cmd;
uint8_t *ctrl_data = NULL;
void __user *uptr_cmd;
void __user *uptr_value;
uint32_t cmd_len = sizeof(struct msm_ctrl_cmd);
uint32_t value_len;
tmp_cmd = (struct msm_ctrl_cmd *)ctrl->value;
uptr_cmd = (void __user *)ctrl->value;
uptr_value = (void __user *)tmp_cmd->value;
value_len = tmp_cmd->length;
D("%s: cmd type = %d, up1=0x%x, ulen1=%d, up2=0x%x, ulen2=%d\n",
__func__, tmp_cmd->type, (uint32_t)uptr_cmd, cmd_len,
(uint32_t)uptr_value, tmp_cmd->length);
ctrl_data = kzalloc(value_len+cmd_len, GFP_KERNEL);
if (ctrl_data == 0) {
pr_err("%s could not allocate memory\n", __func__);
rc = -ENOMEM;
goto end;
}
tmp_cmd = (struct msm_ctrl_cmd *)ctrl_data;
if (copy_from_user((void *)ctrl_data, uptr_cmd,
cmd_len)) {
pr_err("%s: copy_from_user failed.\n", __func__);
rc = -EINVAL;
goto end;
}
tmp_cmd->value = (void *)(ctrl_data+cmd_len);
if (uptr_value && tmp_cmd->length > 0) {
if (copy_from_user((void *)tmp_cmd->value, uptr_value,
value_len)) {
pr_err("%s: copy_from_user failed, size=%d\n",
__func__, value_len);
rc = -EINVAL;
goto end;
}
} else
tmp_cmd->value = NULL;
ctrlcmd.type = MSM_V4L2_SET_CTRL_CMD;
ctrlcmd.length = cmd_len + value_len;
ctrlcmd.value = (void *)ctrl_data;
ctrlcmd.timeout_ms = 1000;
ctrlcmd.vnode_id = pcam->vnode_id;
ctrlcmd.config_ident = g_server_dev.config_info.config_dev_id[0];
/* send command to config thread in usersspace, and get return value */
rc = msm_server_control(&g_server_dev, &ctrlcmd);
D("%s: msm_server_control rc=%d\n", __func__, rc);
if (rc == 0) {
if (uptr_value && tmp_cmd->length > 0 &&
copy_to_user((void __user *)uptr_value,
(void *)(ctrl_data+cmd_len), tmp_cmd->length)) {
pr_err("%s: copy_to_user failed, size=%d\n",
__func__, tmp_cmd->length);
rc = -EINVAL;
goto end;
}
tmp_cmd->value = uptr_value;
if (copy_to_user((void __user *)uptr_cmd,
(void *)tmp_cmd, cmd_len)) {
pr_err("%s: copy_to_user failed in cpy, size=%d\n",
__func__, cmd_len);
rc = -EINVAL;
goto end;
}
}
end:
D("%s: END, type = %d, vaddr = 0x%x, vlen = %d, status = %d, rc = %d\n",
__func__, tmp_cmd->type, (uint32_t)tmp_cmd->value,
tmp_cmd->length, tmp_cmd->status, rc);
kfree(ctrl_data);
ctrl_data = NULL;
return rc;
}
static int msm_server_s_ctrl(struct msm_cam_v4l2_device *pcam,
struct v4l2_control *ctrl)
{
int rc = 0;
struct msm_ctrl_cmd ctrlcmd;
uint8_t ctrl_data[max_control_command_size];
WARN_ON(ctrl == NULL);
if (ctrl && ctrl->id == MSM_V4L2_PID_CTRL_CMD)
return msm_server_proc_ctrl_cmd(pcam, ctrl, 1);
memset(ctrl_data, 0, sizeof(ctrl_data));
ctrlcmd.type = MSM_V4L2_SET_CTRL;
ctrlcmd.length = sizeof(struct v4l2_control);
ctrlcmd.value = (void *)ctrl_data;
memcpy(ctrlcmd.value, ctrl, ctrlcmd.length);
ctrlcmd.timeout_ms = 1000;
ctrlcmd.vnode_id = pcam->vnode_id;
ctrlcmd.config_ident = g_server_dev.config_info.config_dev_id[0];
/* send command to config thread in usersspace, and get return value */
rc = msm_server_control(&g_server_dev, &ctrlcmd);
return rc;
}
static int msm_server_g_ctrl(struct msm_cam_v4l2_device *pcam,
struct v4l2_control *ctrl)
{
int rc = 0;
struct msm_ctrl_cmd ctrlcmd;
uint8_t ctrl_data[max_control_command_size];
WARN_ON(ctrl == NULL);
if (ctrl && ctrl->id == MSM_V4L2_PID_CTRL_CMD)
return msm_server_proc_ctrl_cmd(pcam, ctrl, 0);
memset(ctrl_data, 0, sizeof(ctrl_data));
ctrlcmd.type = MSM_V4L2_GET_CTRL;
ctrlcmd.length = sizeof(struct v4l2_control);
ctrlcmd.value = (void *)ctrl_data;
memcpy(ctrlcmd.value, ctrl, ctrlcmd.length);
ctrlcmd.timeout_ms = 1000;
ctrlcmd.config_ident = g_server_dev.config_info.config_dev_id[0];
/* send command to config thread in usersspace, and get return value */
rc = msm_server_control(&g_server_dev, &ctrlcmd);
ctrl->value = ((struct v4l2_control *)ctrlcmd.value)->value;
return rc;
}
static int msm_server_q_ctrl(struct msm_cam_v4l2_device *pcam,
struct v4l2_queryctrl *queryctrl)
{
int rc = 0;
struct msm_ctrl_cmd ctrlcmd;
uint8_t ctrl_data[max_control_command_size];
WARN_ON(queryctrl == NULL);
memset(ctrl_data, 0, sizeof(ctrl_data));
ctrlcmd.type = MSM_V4L2_QUERY_CTRL;
ctrlcmd.length = sizeof(struct v4l2_queryctrl);
ctrlcmd.value = (void *)ctrl_data;
memcpy(ctrlcmd.value, queryctrl, ctrlcmd.length);
ctrlcmd.timeout_ms = 1000;
ctrlcmd.config_ident = g_server_dev.config_info.config_dev_id[0];
/* send command to config thread in userspace, and get return value */
rc = msm_server_control(&g_server_dev, &ctrlcmd);
D("%s: rc = %d\n", __func__, rc);
if (rc >= 0)
memcpy(queryctrl, ctrlcmd.value, sizeof(struct v4l2_queryctrl));
return rc;
}
static int msm_server_get_fmt(struct msm_cam_v4l2_device *pcam,
int idx, struct v4l2_format *pfmt)
{
struct v4l2_pix_format *pix = &pfmt->fmt.pix;
pix->width = pcam->dev_inst[idx]->vid_fmt.fmt.pix.width;
pix->height = pcam->dev_inst[idx]->vid_fmt.fmt.pix.height;
pix->field = pcam->dev_inst[idx]->vid_fmt.fmt.pix.field;
pix->pixelformat = pcam->dev_inst[idx]->vid_fmt.fmt.pix.pixelformat;
pix->bytesperline = pcam->dev_inst[idx]->vid_fmt.fmt.pix.bytesperline;
pix->colorspace = pcam->dev_inst[idx]->vid_fmt.fmt.pix.colorspace;
/* if (pix->bytesperline < 0)
return pix->bytesperline; */
pix->sizeimage = pix->height * pix->bytesperline;
return 0;
}
static int msm_server_get_fmt_mplane(struct msm_cam_v4l2_device *pcam,
int idx, struct v4l2_format *pfmt)
{
*pfmt = pcam->dev_inst[idx]->vid_fmt;
return 0;
}
static int msm_server_try_fmt(struct msm_cam_v4l2_device *pcam,
struct v4l2_format *pfmt)
{
int rc = 0;
int i = 0;
struct v4l2_pix_format *pix = &pfmt->fmt.pix;
D("%s: 0x%x\n", __func__, pix->pixelformat);
if (pfmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
pr_err("%s: pfmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE!\n",
__func__);
return -EINVAL;
}
/* check if the format is supported by this host-sensor combo */
for (i = 0; i < pcam->num_fmts; i++) {
D("%s: usr_fmts.fourcc: 0x%x\n", __func__,
pcam->usr_fmts[i].fourcc);
if (pcam->usr_fmts[i].fourcc == pix->pixelformat)
break;
}
if (i == pcam->num_fmts) {
pr_err("%s: Format %x not found\n", __func__, pix->pixelformat);
return -EINVAL;
}
return rc;
}
static int msm_server_try_fmt_mplane(struct msm_cam_v4l2_device *pcam,
struct v4l2_format *pfmt)
{
int rc = 0;
int i = 0;
struct v4l2_pix_format_mplane *pix_mp = &pfmt->fmt.pix_mp;
D("%s: 0x%x\n", __func__, pix_mp->pixelformat);
if (pfmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
pr_err("%s: Incorrect format type %d ",
__func__, pfmt->type);
return -EINVAL;
}
/* check if the format is supported by this host-sensor combo */
for (i = 0; i < pcam->num_fmts; i++) {
D("%s: usr_fmts.fourcc: 0x%x\n", __func__,
pcam->usr_fmts[i].fourcc);
if (pcam->usr_fmts[i].fourcc == pix_mp->pixelformat)
break;
}
if (i == pcam->num_fmts) {
pr_err("%s: Format %x not found\n",
__func__, pix_mp->pixelformat);
return -EINVAL;
}
return rc;
}
static int msm_camera_get_crop(struct msm_cam_v4l2_device *pcam,
int idx, struct v4l2_crop *crop)
{
int rc = 0;
struct msm_ctrl_cmd ctrlcmd;
BUG_ON(crop == NULL);
ctrlcmd.type = MSM_V4L2_GET_CROP;
ctrlcmd.length = sizeof(struct v4l2_crop);
ctrlcmd.value = (void *)crop;
ctrlcmd.timeout_ms = 1000;
ctrlcmd.vnode_id = pcam->vnode_id;
ctrlcmd.stream_type = pcam->dev_inst[idx]->image_mode;
ctrlcmd.config_ident = g_server_dev.config_info.config_dev_id[0];
/* send command to config thread in userspace, and get return value */
rc = msm_server_control(&g_server_dev, &ctrlcmd);
D("%s: rc = %d\n", __func__, rc);
return rc;
}
/*
*
* implementation of v4l2_ioctl_ops
*
*/
static int msm_camera_v4l2_querycap(struct file *f, void *pctx,
struct v4l2_capability *pcaps)
{
D("%s\n", __func__);
WARN_ON(pctx != f->private_data);
/* some other day, some other time */
/*cap->version = LINUX_VERSION_CODE; */
pcaps->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
return 0;
}
static int msm_camera_v4l2_queryctrl(struct file *f, void *pctx,
struct v4l2_queryctrl *pqctrl)
{
int rc = 0;
struct msm_cam_v4l2_device *pcam = video_drvdata(f);
D("%s\n", __func__);
WARN_ON(pctx != f->private_data);
mutex_lock(&pcam->vid_lock);
rc = msm_server_q_ctrl(pcam, pqctrl);
mutex_unlock(&pcam->vid_lock);
return rc;
}
static int msm_camera_v4l2_g_ctrl(struct file *f, void *pctx,
struct v4l2_control *c)
{
int rc = 0;
struct msm_cam_v4l2_device *pcam = video_drvdata(f);
D("%s\n", __func__);
WARN_ON(pctx != f->private_data);
mutex_lock(&pcam->vid_lock);
rc = msm_server_g_ctrl(pcam, c);
mutex_unlock(&pcam->vid_lock);
return rc;
}
static int msm_camera_v4l2_s_ctrl(struct file *f, void *pctx,
struct v4l2_control *ctrl)
{
int rc = 0;
struct msm_cam_v4l2_device *pcam = video_drvdata(f);
struct msm_cam_v4l2_dev_inst *pcam_inst;
pcam_inst = container_of(f->private_data,
struct msm_cam_v4l2_dev_inst, eventHandle);
D("%s\n", __func__);
WARN_ON(pctx != f->private_data);
mutex_lock(&pcam->vid_lock);
switch (ctrl->id) {
case MSM_V4L2_PID_MMAP_INST:
D("%s: mmap_inst=(0x%p, %d)\n",
__func__, pcam_inst, pcam_inst->my_index);
pcam_inst->is_mem_map_inst = 1;
break;
case MSM_V4L2_PID_MMAP_ENTRY:
if (copy_from_user(&pcam_inst->mem_map,
(void *)ctrl->value,
sizeof(struct msm_mem_map_info))) {
rc = -EFAULT;
} else
D("%s:mmap entry:cookie=0x%x,mem_type=%d,len=%d\n",
__func__, pcam_inst->mem_map.cookie,
pcam_inst->mem_map.mem_type,
pcam_inst->mem_map.length);
break;
default:
if (ctrl->id == MSM_V4L2_PID_CAM_MODE)
pcam->op_mode = ctrl->value;
rc = msm_server_s_ctrl(pcam, ctrl);
break;
}
mutex_unlock(&pcam->vid_lock);
return rc;
}
int disable_force_offset;
EXPORT_SYMBOL(disable_force_offset);
static int msm_camera_v4l2_reqbufs(struct file *f, void *pctx,
struct v4l2_requestbuffers *pb)
{
int rc = 0, i, j;
struct msm_cam_v4l2_dev_inst *pcam_inst;
disable_force_offset=1;
pcam_inst = container_of(f->private_data,
struct msm_cam_v4l2_dev_inst, eventHandle);
D("%s\n", __func__);
WARN_ON(pctx != f->private_data);
mutex_lock(&pcam_inst->inst_lock);
rc = vb2_reqbufs(&pcam_inst->vid_bufq, pb);
if (rc < 0) {
pr_err("%s reqbufs failed %d ", __func__, rc);
mutex_unlock(&pcam_inst->inst_lock);
return rc;
}
if (!pb->count) {
/* Deallocation. free buf_offset array */
D("%s Inst %p freeing buffer offsets array",
__func__, pcam_inst);
for (j = 0 ; j < pcam_inst->buf_count ; j++) {
kfree(pcam_inst->buf_offset[j]);
pcam_inst->buf_offset[j] = NULL;
}
kfree(pcam_inst->buf_offset);
pcam_inst->buf_offset = NULL;
/* If the userspace has deallocated all the
* buffers, then release the vb2 queue */
if (pcam_inst->vbqueue_initialized) {
vb2_queue_release(&pcam_inst->vid_bufq);
pcam_inst->vbqueue_initialized = 0;
}
} else {
D("%s Inst %p Allocating buf_offset array",
__func__, pcam_inst);
/* Allocation. allocate buf_offset array */
pcam_inst->buf_offset = (struct msm_cam_buf_offset **)
kzalloc(pb->count * sizeof(struct msm_cam_buf_offset *),
GFP_KERNEL);
if (!pcam_inst->buf_offset) {
pr_err("%s out of memory ", __func__);
mutex_unlock(&pcam_inst->inst_lock);
return -ENOMEM;
}
for (i = 0; i < pb->count; i++) {
pcam_inst->buf_offset[i] =
kzalloc(sizeof(struct msm_cam_buf_offset) *
pcam_inst->plane_info.num_planes, GFP_KERNEL);
if (!pcam_inst->buf_offset[i]) {
pr_err("%s out of memory ", __func__);
for (j = i-1 ; j >= 0; j--) {
kfree(pcam_inst->buf_offset[j]);
pcam_inst->buf_offset[j] = NULL;
}
kfree(pcam_inst->buf_offset);
pcam_inst->buf_offset = NULL;
mutex_unlock(&pcam_inst->inst_lock);
return -ENOMEM;
}
}
}
pcam_inst->buf_count = pb->count;
mutex_unlock(&pcam_inst->inst_lock);
return rc;
}
static int msm_camera_v4l2_querybuf(struct file *f, void *pctx,
struct v4l2_buffer *pb)
{
/* get the video device */
int rc = 0;
struct msm_cam_v4l2_dev_inst *pcam_inst;
pcam_inst = container_of(f->private_data,
struct msm_cam_v4l2_dev_inst, eventHandle);
D("%s\n", __func__);
WARN_ON(pctx != f->private_data);
mutex_lock(&pcam_inst->inst_lock);
rc = vb2_querybuf(&pcam_inst->vid_bufq, pb);
mutex_unlock(&pcam_inst->inst_lock);
return rc;
}
static int current_offset;
static int msm_camera_v4l2_qbuf(struct file *f, void *pctx,
struct v4l2_buffer *pb)
{
int rc = 0, i = 0;
/* get the camera device */
struct msm_cam_v4l2_dev_inst *pcam_inst;
pcam_inst = container_of(f->private_data,
struct msm_cam_v4l2_dev_inst, eventHandle);
D("%s Inst = %p\n", __func__, pcam_inst);
WARN_ON(pctx != f->private_data);
mutex_lock(&pcam_inst->inst_lock);
if (!pcam_inst->buf_offset) {
pr_err("%s Buffer is already released. Returning. ", __func__);
mutex_unlock(&pcam_inst->inst_lock);
return -EINVAL;
}
if (pb->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
/* Reject the buffer if planes array was not allocated */
if (pb->m.planes == NULL) {
pr_err("%s Planes array is null ", __func__);
mutex_unlock(&pcam_inst->inst_lock);
return -EINVAL;
}
for (i = 0; i < pcam_inst->plane_info.num_planes; i++) {
if (i == 0) {
D("%s stored offsets for plane %d as"
"addr offset 0, data offset 0",
__func__, i);
pcam_inst->buf_offset[pb->index][i].data_offset = 0;
pcam_inst->buf_offset[pb->index][i].addr_offset = 0;
} else {
if (disable_force_offset) {
current_offset = pb->m.planes[i].reserved[0];
disable_force_offset = 0;
}
D("%s stored offsets for plane %d as"
"addr offset %d, data offset 0",
__func__, i, current_offset);
pcam_inst->buf_offset[pb->index][i].data_offset = 0;
pcam_inst->buf_offset[pb->index][i].addr_offset = current_offset;
}
}
} else {
D("%s stored reserved info %d", __func__, pb->reserved);
pcam_inst->buf_offset[pb->index][0].addr_offset = pb->reserved;
}
rc = vb2_qbuf(&pcam_inst->vid_bufq, pb);
D("%s, videobuf_qbuf returns %d\n", __func__, rc);
mutex_unlock(&pcam_inst->inst_lock);
return rc;
}
static int msm_camera_v4l2_dqbuf(struct file *f, void *pctx,
struct v4l2_buffer *pb)
{
int rc = 0;
/* get the camera device */
struct msm_cam_v4l2_dev_inst *pcam_inst;
pcam_inst = container_of(f->private_data,
struct msm_cam_v4l2_dev_inst, eventHandle);
D("%s\n", __func__);
WARN_ON(pctx != f->private_data);
mutex_lock(&pcam_inst->inst_lock);
if (0 == pcam_inst->streamon) {
mutex_unlock(&pcam_inst->inst_lock);
return -EACCES;
}
rc = vb2_dqbuf(&pcam_inst->vid_bufq, pb, f->f_flags & O_NONBLOCK);
D("%s, videobuf_dqbuf returns %d\n", __func__, rc);
mutex_unlock(&pcam_inst->inst_lock);
return rc;
}
static int msm_camera_v4l2_streamon(struct file *f, void *pctx,
enum v4l2_buf_type buf_type)
{
int rc = 0;
/* get the camera device */
struct msm_cam_v4l2_device *pcam = video_drvdata(f);
struct msm_cam_v4l2_dev_inst *pcam_inst;
pcam_inst = container_of(f->private_data,
struct msm_cam_v4l2_dev_inst, eventHandle);
D("%s Inst %p\n", __func__, pcam_inst);
WARN_ON(pctx != f->private_data);
mutex_lock(&pcam->vid_lock);
mutex_lock(&pcam_inst->inst_lock);
if ((buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) &&
(buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)) {
pr_err("%s Invalid buffer type ", __func__);
mutex_unlock(&pcam_inst->inst_lock);
mutex_unlock(&pcam->vid_lock);
return -EINVAL;
}
D("%s Calling videobuf_streamon", __func__);
/* if HW streaming on is successful, start buffer streaming */
rc = vb2_streamon(&pcam_inst->vid_bufq, buf_type);
D("%s, videobuf_streamon returns %d\n", __func__, rc);
/* turn HW (VFE/sensor) streaming */
pcam_inst->streamon = 1;
rc = msm_server_streamon(pcam, pcam_inst->my_index);
mutex_unlock(&pcam_inst->inst_lock);
mutex_unlock(&pcam->vid_lock);
D("%s rc = %d\n", __func__, rc);
return rc;
}
static int msm_camera_v4l2_streamoff(struct file *f, void *pctx,
enum v4l2_buf_type buf_type)
{
int rc = 0;
/* get the camera device */
struct msm_cam_v4l2_device *pcam = video_drvdata(f);
struct msm_cam_v4l2_dev_inst *pcam_inst;
pcam_inst = container_of(f->private_data,
struct msm_cam_v4l2_dev_inst, eventHandle);