liyujie
2025-08-28 d9927380ed7c8366f762049be9f3fee225860833
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
 
/*
 ******************************************************************************
 *
 * subdev.c
 *
 * Hawkview ISP - subdev.c module
 *
 * Copyright (c) 2016 by Allwinnertech Co., Ltd.  http://www.allwinnertech.com
 *
 * Version          Author         Date            Description
 *
 *   3.0          Yang Feng       2016/03/17    VIDEO INPUT
 *
 *****************************************************************************
 */
 
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
 
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
 
#include <linux/v4l2-subdev.h>
 
#include "media.h"
#include "tools.h"
#include "../include/isp_debug.h"
 
int v4l2_subdev_open(struct media_entity *entity)
{
   if (entity->fd != -1)
       return 0;
 
   entity->fd = open(entity->devname, O_RDWR | O_NONBLOCK);
   if (entity->fd == -1) {
       ISP_ERR("%s: Failed to open subdev device node %s\n", __func__,
           entity->devname);
       return -errno;
   }
   return 0;
}
 
void v4l2_subdev_close(struct media_entity *entity)
{
   if (entity->fd == -1)
       return;
 
   close(entity->fd);
   entity->fd = -1;
}
 
/* -----------------------------------------------------------------------------
 * Controls
 */
 
int v4l2_get_control(struct media_entity *entity, unsigned int id,
               int32_t *value)
{
   struct v4l2_control ctrl;
   int ret;
 
   if (entity->fd == -1)
       return -1;
 
   ctrl.id = id;
 
   ret = ioctl(entity->fd, VIDIOC_G_CTRL, &ctrl);
   if (ret < 0) {
       ISP_ERR("unable to get control: %s (%d).\n",
           strerror(errno), errno);
       return -errno;
   }
 
   *value = ctrl.value;
   return 0;
}
 
int v4l2_set_control(struct media_entity *entity, unsigned int id,
               int32_t *value)
{
   struct v4l2_control ctrl;
   int ret;
 
   if (entity->fd == -1)
       return -1;
 
   ctrl.id = id;
   ctrl.value = *value;
 
   ret = ioctl(entity->fd, VIDIOC_S_CTRL, &ctrl);
   if (ret < 0) {
       ISP_ERR("unable to set control: %s (%d).\n",
           strerror(errno), errno);
       return -errno;
   }
 
   *value = ctrl.value;
   return 0;
}
 
int v4l2_get_controls(struct media_entity *entity, unsigned int count,
                struct v4l2_ext_control *ctrls)
{
   struct v4l2_ext_controls controls;
   int ret;
 
   if (entity->fd == -1)
       return -1;
 
   memset(&controls, 0, sizeof controls);
   controls.count = count;
   controls.controls = ctrls;
 
   ret = ioctl(entity->fd, VIDIOC_G_EXT_CTRLS, &controls);
   if (ret < 0)
       ISP_ERR("unable to get multiple controls: %s (%d).\n",
           strerror(errno), errno);
 
   return ret;
}
 
int v4l2_set_controls(struct media_entity *entity, unsigned int count,
                struct v4l2_ext_control *ctrls)
{
   struct v4l2_ext_controls controls;
   int ret;
 
   if (entity->fd == -1)
       return -1;
 
   memset(&controls, 0, sizeof controls);
   controls.count = count;
   controls.controls = ctrls;
 
   ret = ioctl(entity->fd, VIDIOC_S_EXT_CTRLS, &controls);
   if (ret < 0)
       ISP_ERR("unable to set multiple controls: %s (%d).\n",
           strerror(errno), errno);
 
   return ret;
}