summaryrefslogtreecommitdiffstats
path: root/camera/exynos_v4l2.c
blob: cb7039ba43ca7685f79a90d81852fbc7dfff3803 (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
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
/*
 * Copyright (C) 2013 Paul Kocialkowski
 *
 * Based on crespo libcamera and exynos4 hal libcamera:
 * Copyright 2008, The Android Open Source Project
 * Copyright 2010, Samsung Electronics Co. LTD
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <malloc.h>
#include <poll.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <sys/ioctl.h>

#include <asm/types.h>

#define LOG_TAG "exynos_v4l2"
#include <utils/Log.h>

#include "exynos_camera.h"

/*
 * Utils
 */

int exynos_v4l2_find_index(struct exynos_camera *exynos_camera, int exynos_v4l2_id)
{
	int index;
	int i;

	if (exynos_camera == NULL || exynos_camera->config == NULL ||
		exynos_camera->config->v4l2_nodes == NULL)
		return -EINVAL;

	if (exynos_v4l2_id > exynos_camera->config->v4l2_nodes_count)
		return -1;

	index = -1;
	for (i = 0; i < exynos_camera->config->v4l2_nodes_count; i++) {
		if (exynos_camera->config->v4l2_nodes[i].id == exynos_v4l2_id &&
			exynos_camera->config->v4l2_nodes[i].node != NULL) {
			index = i;
		}
	}

	return index;
}

int exynos_v4l2_find_fd(struct exynos_camera *exynos_camera, int exynos_v4l2_id)
{
	int index;

	if (exynos_camera == NULL)
		return -EINVAL;

	index = exynos_v4l2_find_index(exynos_camera, exynos_v4l2_id);
	if (index < 0)
		return -1;

	return exynos_camera->v4l2_fds[index];
}

/*
 * File ops
 */

int exynos_v4l2_open(struct exynos_camera *exynos_camera, int exynos_v4l2_id)
{
	char *node;
	int index;
	int fd;

	if (exynos_camera == NULL || exynos_camera->config == NULL ||
		exynos_camera->config->v4l2_nodes == NULL)
		return -EINVAL;

	index = exynos_v4l2_find_index(exynos_camera, exynos_v4l2_id);
	if (index < 0) {
		ALOGE("%s: Unable to find v4l2 node #%d", __func__, exynos_v4l2_id);
		return -1;
	}

	node = exynos_camera->config->v4l2_nodes[index].node;
	fd = open(node, O_RDWR);
	if (fd < 0) {
		ALOGE("%s: Unable to open v4l2 node #%d", __func__, exynos_v4l2_id);
		return -1;
	}

	exynos_camera->v4l2_fds[index] = fd;

	return 0;
}

void exynos_v4l2_close(struct exynos_camera *exynos_camera, int exynos_v4l2_id)
{
	int index;

	if (exynos_camera == NULL || exynos_camera->config == NULL ||
		exynos_camera->config->v4l2_nodes == NULL)
		return;

	index = exynos_v4l2_find_index(exynos_camera, exynos_v4l2_id);
	if (index < 0) {
		ALOGE("%s: Unable to find v4l2 node #%d", __func__, exynos_v4l2_id);
		return;
	}

	if (exynos_camera->v4l2_fds[index] > 0)
		close(exynos_camera->v4l2_fds[index]);

	exynos_camera->v4l2_fds[index] = -1;
}

int exynos_v4l2_ioctl(struct exynos_camera *exynos_camera, int exynos_v4l2_id,
	int request, void *data)
{
	int fd;

	if (exynos_camera == NULL)
		return -EINVAL;

	fd = exynos_v4l2_find_fd(exynos_camera, exynos_v4l2_id);
	if (fd < 0) {
		ALOGE("%s: Unable to find v4l2 fd #%d", __func__, exynos_v4l2_id);
		return -1;
	}

	return ioctl(fd, request, data);
}

int exynos_v4l2_poll(struct exynos_camera *exynos_camera, int exynos_v4l2_id)
{
	struct pollfd events;
	int fd;
	int rc;

	if (exynos_camera == NULL)
		return -EINVAL;

	fd = exynos_v4l2_find_fd(exynos_camera, exynos_v4l2_id);
	if (fd < 0) {
		ALOGE("%s: Unable to find v4l2 fd #%d", __func__, exynos_v4l2_id);
		return -1;
	}

	memset(&events, 0, sizeof(events));
	events.fd = fd;
	events.events = POLLIN | POLLERR;

	rc = poll(&events, 1, 1000);
	if (rc < 0 || events.revents & POLLERR) {
		ALOGE("%s: poll failed", __func__);
		return -1;
	}

	return rc;
}

/*
 * VIDIOC
 */

int exynos_v4l2_qbuf(struct exynos_camera *exynos_camera, int exynos_v4l2_id,
	int type, int memory, int index)
{
	struct v4l2_buffer buffer;
	int rc;

	if (exynos_camera == NULL || index < 0)
		return -EINVAL;

	buffer.type = type;
	buffer.memory = memory;
	buffer.index = index;

	rc = exynos_v4l2_ioctl(exynos_camera, exynos_v4l2_id, VIDIOC_QBUF, &buffer);
	if (rc < 0) {
		ALOGE("%s: ioctl failed", __func__);
		return -1;
	}

	return 0;
}

int exynos_v4l2_qbuf_cap(struct exynos_camera *exynos_camera, int exynos_v4l2_id,
	int index)
{
	return exynos_v4l2_qbuf(exynos_camera, exynos_v4l2_id, V4L2_BUF_TYPE_VIDEO_CAPTURE,
		V4L2_MEMORY_MMAP, index);
}

int exynos_v4l2_qbuf_out(struct exynos_camera *exynos_camera, int exynos_v4l2_id,
	int index)
{
	return exynos_v4l2_qbuf(exynos_camera, exynos_v4l2_id, V4L2_BUF_TYPE_VIDEO_OUTPUT,
		V4L2_MEMORY_USERPTR, index);
}

int exynos_v4l2_dqbuf(struct exynos_camera *exynos_camera, int exynos_v4l2_id,
	int type, int memory)
{
	struct v4l2_buffer buffer;
	int rc;

	if (exynos_camera == NULL)
		return -EINVAL;

	memset(&buffer, 0, sizeof(buffer));
	buffer.type = type;
	buffer.memory = memory;

	rc = exynos_v4l2_ioctl(exynos_camera, exynos_v4l2_id, VIDIOC_DQBUF, &buffer);
	if (rc < 0) {
		ALOGE("%s: ioctl failed", __func__);
		return -1;
	}

	return buffer.index;
}

int exynos_v4l2_dqbuf_cap(struct exynos_camera *exynos_camera, int exynos_v4l2_id)
{
	return exynos_v4l2_dqbuf(exynos_camera, exynos_v4l2_id, V4L2_BUF_TYPE_VIDEO_CAPTURE,
		V4L2_MEMORY_MMAP);
}

int exynos_v4l2_dqbuf_out(struct exynos_camera *exynos_camera, int exynos_v4l2_id)
{
	return exynos_v4l2_dqbuf(exynos_camera, exynos_v4l2_id, V4L2_BUF_TYPE_VIDEO_OUTPUT,
		V4L2_MEMORY_USERPTR);
}

int exynos_v4l2_reqbufs(struct exynos_camera *exynos_camera, int exynos_v4l2_id,
	int type, int memory, int count)
{
	struct v4l2_requestbuffers requestbuffers;
	int rc;

	if (exynos_camera == NULL || count < 0)
		return -EINVAL;

	requestbuffers.type = type;
	requestbuffers.count = count;
	requestbuffers.memory = memory;

	rc = exynos_v4l2_ioctl(exynos_camera, exynos_v4l2_id, VIDIOC_REQBUFS, &requestbuffers);
	if (rc < 0) {
		ALOGE("%s: ioctl failed", __func__);
		return -1;
	}

	return requestbuffers.count;
}

int exynos_v4l2_reqbufs_cap(struct exynos_camera *exynos_camera, int exynos_v4l2_id,
	int count)
{
	return exynos_v4l2_reqbufs(exynos_camera, exynos_v4l2_id, V4L2_BUF_TYPE_VIDEO_CAPTURE,
		V4L2_MEMORY_MMAP, count);
}

int exynos_v4l2_reqbufs_out(struct exynos_camera *exynos_camera, int exynos_v4l2_id,
	int count)
{
	return exynos_v4l2_reqbufs(exynos_camera, exynos_v4l2_id, V4L2_BUF_TYPE_VIDEO_OUTPUT,
		V4L2_MEMORY_USERPTR, count);
}

int exynos_v4l2_querybuf(struct exynos_camera *exynos_camera, int exynos_v4l2_id,
	int type, int memory, int index)
{
	struct v4l2_buffer buffer;
	int rc;

	if (exynos_camera == NULL)
		return -EINVAL;

	memset(&buffer, 0, sizeof(buffer));
	buffer.type = type;
	buffer.memory = memory;
	buffer.index = index;

	rc = exynos_v4l2_ioctl(exynos_camera, exynos_v4l2_id, VIDIOC_QUERYBUF, &buffer);
	if (rc < 0) {
		ALOGE("%s: ioctl failed", __func__);
		return -1;
	}

	return buffer.length;
}

int exynos_v4l2_querybuf_cap(struct exynos_camera *exynos_camera, int exynos_v4l2_id,
	int index)
{
	return exynos_v4l2_querybuf(exynos_camera, exynos_v4l2_id, V4L2_BUF_TYPE_VIDEO_CAPTURE,
		V4L2_MEMORY_MMAP, index);
}

int exynos_v4l2_querybuf_out(struct exynos_camera *exynos_camera, int exynos_v4l2_id,
	int index)
{
	return exynos_v4l2_querybuf(exynos_camera, exynos_v4l2_id, V4L2_BUF_TYPE_VIDEO_OUTPUT,
		V4L2_MEMORY_USERPTR, index);
}

int exynos_v4l2_querycap(struct exynos_camera *exynos_camera, int exynos_v4l2_id,
	int flags)
{
	struct v4l2_capability cap;
	int rc;

	if (exynos_camera == NULL)
		return -EINVAL;

	rc = exynos_v4l2_ioctl(exynos_camera, exynos_v4l2_id, VIDIOC_QUERYCAP, &cap);
	if (rc < 0) {
		ALOGE("%s: ioctl failed", __func__);
		return -1;
	}

	if (!(cap.capabilities & flags))
		return -1;

	return 0;
}

int exynos_v4l2_querycap_cap(struct exynos_camera *exynos_camera, int exynos_v4l2_id)
{
	return exynos_v4l2_querycap(exynos_camera, exynos_v4l2_id, V4L2_CAP_VIDEO_CAPTURE);
}

int exynos_v4l2_querycap_out(struct exynos_camera *exynos_camera, int exynos_v4l2_id)
{
	return exynos_v4l2_querycap(exynos_camera, exynos_v4l2_id, V4L2_CAP_VIDEO_OUTPUT);
}

int exynos_v4l2_streamon(struct exynos_camera *exynos_camera, int exynos_v4l2_id,
	int type)
{
	enum v4l2_buf_type buf_type;
	int rc;

	if (exynos_camera == NULL)
		return -EINVAL;

	buf_type = type;

	rc = exynos_v4l2_ioctl(exynos_camera, exynos_v4l2_id, VIDIOC_STREAMON, &buf_type);
	if (rc < 0) {
		ALOGE("%s: ioctl failed", __func__);
		return -1;
	}

	return 0;
}

int exynos_v4l2_streamon_cap(struct exynos_camera *exynos_camera, int exynos_v4l2_id)
{
	return exynos_v4l2_streamon(exynos_camera, exynos_v4l2_id, V4L2_BUF_TYPE_VIDEO_CAPTURE);
}

int exynos_v4l2_streamon_out(struct exynos_camera *exynos_camera, int exynos_v4l2_id)
{
	return exynos_v4l2_streamon(exynos_camera, exynos_v4l2_id, V4L2_BUF_TYPE_VIDEO_OUTPUT);
}

int exynos_v4l2_streamoff(struct exynos_camera *exynos_camera, int exynos_v4l2_id,
	int type)
{
	enum v4l2_buf_type buf_type;
	int rc;

	if (exynos_camera == NULL)
		return -EINVAL;

	buf_type = type;

	rc = exynos_v4l2_ioctl(exynos_camera, exynos_v4l2_id, VIDIOC_STREAMOFF, &buf_type);
	if (rc < 0) {
		ALOGE("%s: ioctl failed", __func__);
		return -1;
	}

	return 0;
}

int exynos_v4l2_streamoff_cap(struct exynos_camera *exynos_camera, int exynos_v4l2_id)
{
	return exynos_v4l2_streamoff(exynos_camera, exynos_v4l2_id, V4L2_BUF_TYPE_VIDEO_CAPTURE);
}

int exynos_v4l2_streamoff_out(struct exynos_camera *exynos_camera, int exynos_v4l2_id)
{
	return exynos_v4l2_streamoff(exynos_camera, exynos_v4l2_id, V4L2_BUF_TYPE_VIDEO_OUTPUT);
}

int exynos_v4l2_g_fmt(struct exynos_camera *exynos_camera, int exynos_v4l2_id,
	int type, int *width, int *height, int *fmt)
{
	struct v4l2_format format;
	int rc;

	if (exynos_camera == NULL)
		return -EINVAL;

	format.type = type;
	format.fmt.pix.field = V4L2_FIELD_NONE;

	rc = exynos_v4l2_ioctl(exynos_camera, exynos_v4l2_id, VIDIOC_G_FMT, &format);
	if (rc < 0) {
		ALOGE("%s: ioctl failed", __func__);
		return -1;
	}

	if (width != NULL)
		*width = format.fmt.pix.width;
	if (height != NULL)
		*height = format.fmt.pix.height;
	if (fmt != NULL)
		*fmt = format.fmt.pix.pixelformat;

	return 0;
}

int exynos_v4l2_g_fmt_cap(struct exynos_camera *exynos_camera, int exynos_v4l2_id,
	int *width, int *height, int *fmt)
{
	return exynos_v4l2_g_fmt(exynos_camera, exynos_v4l2_id, V4L2_BUF_TYPE_VIDEO_CAPTURE,
		width, height, fmt);
}

int exynos_v4l2_g_fmt_out(struct exynos_camera *exynos_camera, int exynos_v4l2_id,
	int *width, int *height, int *fmt)
{
	return exynos_v4l2_g_fmt(exynos_camera, exynos_v4l2_id, V4L2_BUF_TYPE_VIDEO_OUTPUT,
		width, height, fmt);
}

int exynos_v4l2_s_fmt_pix(struct exynos_camera *exynos_camera, int exynos_v4l2_id,
	int type, int width, int height, int fmt, int priv)
{
	struct v4l2_format format;
	int rc;

	if (exynos_camera == NULL)
		return -EINVAL;

	memset(&format, 0, sizeof(format));
	format.type = type;
	format.fmt.pix.width = width;
	format.fmt.pix.height = height;
	format.fmt.pix.pixelformat = fmt;
	format.fmt.pix.field = V4L2_FIELD_NONE;
	format.fmt.pix.priv = priv;

	rc = exynos_v4l2_ioctl(exynos_camera, exynos_v4l2_id, VIDIOC_S_FMT, &format);
	if (rc < 0) {
		ALOGE("%s: ioctl failed", __func__);
		return -1;
	}

	return 0;
}

int exynos_v4l2_s_fmt_pix_cap(struct exynos_camera *exynos_camera, int exynos_v4l2_id,
	int width, int height, int fmt, int priv)
{
	return exynos_v4l2_s_fmt_pix(exynos_camera, exynos_v4l2_id, V4L2_BUF_TYPE_VIDEO_CAPTURE,
		width, height, fmt, priv);
}

int exynos_v4l2_s_fmt_pix_out(struct exynos_camera *exynos_camera, int exynos_v4l2_id,
	int width, int height, int fmt, int priv)
{
	return exynos_v4l2_s_fmt_pix(exynos_camera, exynos_v4l2_id, V4L2_BUF_TYPE_VIDEO_OUTPUT,
		width, height, fmt, priv);
}

int exynos_v4l2_s_fmt_win(struct exynos_camera *exynos_camera, int exynos_v4l2_id,
	int left, int top, int width, int height)
{
	struct v4l2_format format;
	int rc;

	if (exynos_camera == NULL)
		return -EINVAL;

	memset(&format, 0, sizeof(format));
	format.type = V4L2_BUF_TYPE_VIDEO_OVERLAY;
	format.fmt.win.w.left = left;
	format.fmt.win.w.top = top;
	format.fmt.win.w.width = width;
	format.fmt.win.w.height = height;


	rc = exynos_v4l2_ioctl(exynos_camera, exynos_v4l2_id, VIDIOC_S_FMT, &format);
	if (rc < 0) {
		ALOGE("%s: ioctl failed", __func__);
		return -1;
	}

	return 0;
}

int exynos_v4l2_enum_fmt(struct exynos_camera *exynos_camera, int exynos_v4l2_id,
	int type, int fmt)
{
	struct v4l2_fmtdesc fmtdesc;
	int rc;

	if (exynos_camera == NULL)
		return -EINVAL;

	fmtdesc.type = type;
	fmtdesc.index = 0;

	do {
		rc = exynos_v4l2_ioctl(exynos_camera, exynos_v4l2_id, VIDIOC_ENUM_FMT, &fmtdesc);
		if (rc < 0) {
			ALOGE("%s: ioctl failed", __func__);
			return -1;
		}

		if (fmtdesc.pixelformat == (unsigned int) fmt)
			return 0;

		fmtdesc.index++;
	} while (rc >= 0);

	return -1;
}

int exynos_v4l2_enum_fmt_cap(struct exynos_camera *exynos_camera, int exynos_v4l2_id,
	int fmt)
{
	return exynos_v4l2_enum_fmt(exynos_camera, exynos_v4l2_id, V4L2_BUF_TYPE_VIDEO_CAPTURE,
		fmt);
}

int exynos_v4l2_enum_fmt_out(struct exynos_camera *exynos_camera, int exynos_v4l2_id,
	int fmt)
{
	return exynos_v4l2_enum_fmt(exynos_camera, exynos_v4l2_id, V4L2_BUF_TYPE_VIDEO_OUTPUT,
		fmt);
}

int exynos_v4l2_enum_input(struct exynos_camera *exynos_camera, int exynos_v4l2_id,
	int id)
{
	struct v4l2_input input;
	int rc;

	if (exynos_camera == NULL || id < 0)
		return -EINVAL;

	input.index = id;

	rc = exynos_v4l2_ioctl(exynos_camera, exynos_v4l2_id, VIDIOC_ENUMINPUT, &input);
	if (rc < 0) {
		ALOGE("%s: ioctl failed", __func__);
		return -1;
	}

	if (input.name[0] == '\0')
		return -1;

	return 0;
}

int exynos_v4l2_s_input(struct exynos_camera *exynos_camera, int exynos_v4l2_id,
	int id)
{
	struct v4l2_input input;
	int rc;

	if (exynos_camera == NULL || id < 0)
		return -EINVAL;

	input.index = id;

	rc = exynos_v4l2_ioctl(exynos_camera, exynos_v4l2_id, VIDIOC_S_INPUT, &input);
	if (rc < 0) {
		ALOGE("%s: ioctl failed", __func__);
		return -1;
	}

	return 0;
}

int exynos_v4l2_g_ext_ctrls(struct exynos_camera *exynos_camera, int exynos_v4l2_id,
	struct v4l2_ext_control *control, int count)
{
	struct v4l2_ext_controls controls;
	int rc;

	if (exynos_camera == NULL || control == NULL)
		return -EINVAL;

	memset(&controls, 0, sizeof(controls));
	controls.ctrl_class = V4L2_CTRL_CLASS_CAMERA;
	controls.count = count;
	controls.controls = control;

	rc = exynos_v4l2_ioctl(exynos_camera, exynos_v4l2_id, VIDIOC_G_EXT_CTRLS, &controls);
	if (rc < 0) {
		ALOGE("%s: ioctl failed", __func__);
		return -1;
	}

	return 0;
}

int exynos_v4l2_g_ctrl(struct exynos_camera *exynos_camera, int exynos_v4l2_id,
	int id, int *value)
{
	struct v4l2_control control;
	int rc;

	if (exynos_camera == NULL)
		return -EINVAL;

	control.id = id;

	rc = exynos_v4l2_ioctl(exynos_camera, exynos_v4l2_id, VIDIOC_G_CTRL, &control);
	if (rc < 0) {
		ALOGE("%s: ioctl failed", __func__);
		return -1;
	}

	if (value != NULL)
		*value = control.value;

	return 0;
}

int exynos_v4l2_s_ctrl(struct exynos_camera *exynos_camera, int exynos_v4l2_id,
	int id, int value)
{
	struct v4l2_control control;
	int rc;

	if (exynos_camera == NULL)
		return -EINVAL;

	control.id = id;
	control.value = value;

	rc = exynos_v4l2_ioctl(exynos_camera, exynos_v4l2_id, VIDIOC_S_CTRL, &control);
	if (rc < 0) {
		ALOGE("%s: ioctl failed", __func__);
		return -1;
	}

	return control.value;
}

int exynos_v4l2_s_parm(struct exynos_camera *exynos_camera, int exynos_v4l2_id,
	int type, struct v4l2_streamparm *streamparm)
{
	int rc;

	if (exynos_camera == NULL || streamparm == NULL)
		return -EINVAL;

	streamparm->type = type;

	rc = exynos_v4l2_ioctl(exynos_camera, exynos_v4l2_id, VIDIOC_S_PARM, streamparm);
	if (rc < 0) {
		ALOGE("%s: ioctl failed", __func__);
		return -1;
	}

	return 0;
}

int exynos_v4l2_s_parm_cap(struct exynos_camera *exynos_camera, int exynos_v4l2_id,
	struct v4l2_streamparm *streamparm)
{
	return exynos_v4l2_s_parm(exynos_camera, exynos_v4l2_id, V4L2_BUF_TYPE_VIDEO_CAPTURE,
		streamparm);
}

int exynos_v4l2_s_parm_out(struct exynos_camera *exynos_camera, int exynos_v4l2_id,
	struct v4l2_streamparm *streamparm)
{
	return exynos_v4l2_s_parm(exynos_camera, exynos_v4l2_id, V4L2_BUF_TYPE_VIDEO_OUTPUT,
		streamparm);
}

int exynos_v4l2_s_crop(struct exynos_camera *exynos_camera, int exynos_v4l2_id,
	int type, int left, int top, int width, int height)
{
	struct v4l2_crop crop;
	int rc;

	if (exynos_camera == NULL)
		return -EINVAL;

	crop.type = type;
	crop.c.left = left;
	crop.c.top = top;
	crop.c.width = width;
	crop.c.height = height;

	rc = exynos_v4l2_ioctl(exynos_camera, exynos_v4l2_id, VIDIOC_S_CROP, &crop);
	if (rc < 0) {
		ALOGE("%s: ioctl failed", __func__);
		return -1;
	}

	return 0;
}

int exynos_v4l2_s_crop_cap(struct exynos_camera *exynos_camera, int exynos_v4l2_id,
	int left, int top, int width, int height)
{
	return exynos_v4l2_s_crop(exynos_camera, exynos_v4l2_id, V4L2_BUF_TYPE_VIDEO_CAPTURE,
		left, top, width, height);
}

int exynos_v4l2_s_crop_out(struct exynos_camera *exynos_camera, int exynos_v4l2_id,
	int left, int top, int width, int height)
{
	return exynos_v4l2_s_crop(exynos_camera, exynos_v4l2_id, V4L2_BUF_TYPE_VIDEO_OUTPUT,
		left, top, width, height);
}

int exynos_v4l2_g_fbuf(struct exynos_camera *exynos_camera, int exynos_v4l2_id,
	void **base, int *width, int *height, int *fmt)
{
	struct v4l2_framebuffer framebuffer;
	int rc;

	if (exynos_camera == NULL)
		return -EINVAL;

	rc = exynos_v4l2_ioctl(exynos_camera, exynos_v4l2_id, VIDIOC_G_FBUF, &framebuffer);
	if (rc < 0) {
		ALOGE("%s: ioctl failed", __func__);
		return -1;
	}

	if (base != NULL)
		*base = framebuffer.base;
	if (width != NULL)
		*width = framebuffer.fmt.width;
	if (height != NULL)
		*height = framebuffer.fmt.height;
	if (fmt != NULL)
		*fmt = framebuffer.fmt.pixelformat;

	return 0;
}

int exynos_v4l2_s_fbuf(struct exynos_camera *exynos_camera, int exynos_v4l2_id,
	void *base, int width, int height, int fmt)
{
	struct v4l2_framebuffer framebuffer;
	int rc;

	if (exynos_camera == NULL)
		return -EINVAL;

	memset(&framebuffer, 0, sizeof(framebuffer));
	framebuffer.base = base;
	framebuffer.fmt.width = width;
	framebuffer.fmt.height = height;
	framebuffer.fmt.pixelformat = fmt;

	rc = exynos_v4l2_ioctl(exynos_camera, exynos_v4l2_id, VIDIOC_S_FBUF, &framebuffer);
	if (rc < 0) {
		ALOGE("%s: ioctl failed", __func__);
		return -1;
	}

	return 0;
}