Paul Kocialkowski | b31c446 | 2013-09-01 19:22:36 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 Paul Kocialkowski |
| 3 | * |
| 4 | * Based on crespo libcamera and exynos4 hal libcamera: |
| 5 | * Copyright 2008, The Android Open Source Project |
| 6 | * Copyright 2010, Samsung Electronics Co. LTD |
| 7 | * |
| 8 | * This program is free software: you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation, either version 3 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | */ |
| 21 | |
| 22 | #include <fcntl.h> |
| 23 | #include <unistd.h> |
| 24 | #include <errno.h> |
| 25 | #include <malloc.h> |
| 26 | #include <poll.h> |
| 27 | #include <sys/stat.h> |
| 28 | #include <sys/types.h> |
| 29 | #include <sys/time.h> |
| 30 | #include <sys/mman.h> |
| 31 | #include <sys/ioctl.h> |
| 32 | |
| 33 | #include <asm/types.h> |
| 34 | |
| 35 | #define LOG_TAG "exynos_v4l2" |
| 36 | #include <utils/Log.h> |
| 37 | |
| 38 | #include "exynos_camera.h" |
| 39 | |
| 40 | int exynos_v4l2_init(struct exynos_camera *exynos_camera) |
| 41 | { |
| 42 | int i; |
| 43 | |
| 44 | for (i = 0; i < EXYNOS_CAMERA_MAX_V4L2_NODES_COUNT; i++) |
| 45 | exynos_camera->v4l2_fds[i] = -1; |
| 46 | |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | int exynos_v4l2_index(struct exynos_camera *exynos_camera, int exynos_v4l2_id) |
| 51 | { |
| 52 | int index; |
| 53 | int i; |
| 54 | |
| 55 | if (exynos_camera == NULL || exynos_camera->config == NULL || |
| 56 | exynos_camera->config->v4l2_nodes == NULL) |
| 57 | return -EINVAL; |
| 58 | |
| 59 | if (exynos_v4l2_id > exynos_camera->config->v4l2_nodes_count) |
| 60 | return -1; |
| 61 | |
| 62 | index = -1; |
| 63 | for (i = 0; i < exynos_camera->config->v4l2_nodes_count; i++) { |
| 64 | if (exynos_camera->config->v4l2_nodes[i].id == exynos_v4l2_id && |
| 65 | exynos_camera->config->v4l2_nodes[i].node != NULL) { |
| 66 | index = i; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | return index; |
| 71 | } |
| 72 | |
| 73 | int exynos_v4l2_fd(struct exynos_camera *exynos_camera, int exynos_v4l2_id) |
| 74 | { |
| 75 | int index; |
| 76 | |
| 77 | if (exynos_camera == NULL) |
| 78 | return -EINVAL; |
| 79 | |
| 80 | index = exynos_v4l2_index(exynos_camera, exynos_v4l2_id); |
| 81 | if (index < 0) { |
| 82 | ALOGE("%s: Unable to get v4l2 index for id %d", __func__, exynos_v4l2_id); |
| 83 | return -1; |
| 84 | } |
| 85 | |
| 86 | return exynos_camera->v4l2_fds[index]; |
| 87 | } |
| 88 | |
| 89 | int exynos_v4l2_open(struct exynos_camera *exynos_camera, int exynos_v4l2_id) |
| 90 | { |
| 91 | char *node; |
| 92 | int index; |
| 93 | int fd; |
| 94 | |
| 95 | if (exynos_camera == NULL || exynos_camera->config == NULL || |
| 96 | exynos_camera->config->v4l2_nodes == NULL) |
| 97 | return -EINVAL; |
| 98 | |
| 99 | index = exynos_v4l2_index(exynos_camera, exynos_v4l2_id); |
| 100 | if (index < 0) { |
| 101 | ALOGE("%s: Unable to get v4l2 node for id %d", __func__, exynos_v4l2_id); |
| 102 | return -1; |
| 103 | } |
| 104 | |
| 105 | node = exynos_camera->config->v4l2_nodes[index].node; |
| 106 | fd = open(node, O_RDWR); |
| 107 | if (fd < 0) { |
| 108 | ALOGE("%s: Unable to open v4l2 node for id %d", __func__, exynos_v4l2_id); |
| 109 | return -1; |
| 110 | } |
| 111 | |
| 112 | exynos_camera->v4l2_fds[index] = fd; |
| 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | void exynos_v4l2_close(struct exynos_camera *exynos_camera, int exynos_v4l2_id) |
| 118 | { |
| 119 | int index; |
| 120 | |
| 121 | if (exynos_camera == NULL || exynos_camera->config == NULL || |
| 122 | exynos_camera->config->v4l2_nodes == NULL) |
| 123 | return; |
| 124 | |
| 125 | index = exynos_v4l2_index(exynos_camera, exynos_v4l2_id); |
| 126 | if (index < 0) { |
| 127 | ALOGE("%s: Unable to get v4l2 node for id %d", __func__, exynos_v4l2_id); |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | if (exynos_camera->v4l2_fds[index] >= 0) |
| 132 | close(exynos_camera->v4l2_fds[index]); |
| 133 | |
| 134 | exynos_camera->v4l2_fds[index] = -1; |
| 135 | } |
| 136 | |
| 137 | int exynos_v4l2_ioctl(struct exynos_camera *exynos_camera, int exynos_v4l2_id, |
| 138 | int request, void *data) |
| 139 | { |
| 140 | int fd; |
| 141 | |
| 142 | if (exynos_camera == NULL) |
| 143 | return -EINVAL; |
| 144 | |
| 145 | fd = exynos_v4l2_fd(exynos_camera, exynos_v4l2_id); |
| 146 | if (fd < 0) { |
| 147 | ALOGE("%s: Unable to get v4l2 fd for id %d", __func__, exynos_v4l2_id); |
| 148 | return -1; |
| 149 | } |
| 150 | |
| 151 | return ioctl(fd, request, data); |
| 152 | } |
| 153 | |
| 154 | int exynos_v4l2_poll(struct exynos_camera *exynos_camera, int exynos_v4l2_id) |
| 155 | { |
| 156 | struct pollfd events; |
| 157 | int fd; |
| 158 | int rc; |
| 159 | |
| 160 | if (exynos_camera == NULL) |
| 161 | return -EINVAL; |
| 162 | |
| 163 | fd = exynos_v4l2_fd(exynos_camera, exynos_v4l2_id); |
| 164 | if (fd < 0) { |
| 165 | ALOGE("%s: Unable to get v4l2 fd for id %d", __func__, exynos_v4l2_id); |
| 166 | return -1; |
| 167 | } |
| 168 | |
| 169 | memset(&events, 0, sizeof(events)); |
| 170 | events.fd = fd; |
| 171 | events.events = POLLIN | POLLERR; |
| 172 | |
| 173 | rc = poll(&events, 1, 1000); |
| 174 | if (rc < 0 || events.revents & POLLERR) |
| 175 | return -1; |
| 176 | |
| 177 | return rc; |
| 178 | } |
| 179 | |
| 180 | int exynos_v4l2_qbuf(struct exynos_camera *exynos_camera, int exynos_v4l2_id, |
| 181 | int type, int memory, int index, unsigned long userptr) |
| 182 | { |
| 183 | struct v4l2_buffer buffer; |
| 184 | int rc; |
| 185 | |
| 186 | if (exynos_camera == NULL || index < 0) |
| 187 | return -EINVAL; |
| 188 | |
| 189 | memset(&buffer, 0, sizeof(buffer)); |
| 190 | buffer.type = type; |
| 191 | buffer.memory = memory; |
| 192 | buffer.index = index; |
| 193 | |
| 194 | if (userptr) |
| 195 | buffer.m.userptr = userptr; |
| 196 | |
| 197 | rc = exynos_v4l2_ioctl(exynos_camera, exynos_v4l2_id, VIDIOC_QBUF, &buffer); |
| 198 | return rc; |
| 199 | } |
| 200 | |
| 201 | int exynos_v4l2_qbuf_cap(struct exynos_camera *exynos_camera, int exynos_v4l2_id, |
| 202 | int index) |
| 203 | { |
| 204 | return exynos_v4l2_qbuf(exynos_camera, exynos_v4l2_id, V4L2_BUF_TYPE_VIDEO_CAPTURE, |
| 205 | V4L2_MEMORY_MMAP, index, 0); |
| 206 | } |
| 207 | |
| 208 | int exynos_v4l2_qbuf_out(struct exynos_camera *exynos_camera, int exynos_v4l2_id, |
| 209 | int index, unsigned long userptr) |
| 210 | { |
| 211 | return exynos_v4l2_qbuf(exynos_camera, exynos_v4l2_id, V4L2_BUF_TYPE_VIDEO_OUTPUT, |
| 212 | V4L2_MEMORY_USERPTR, index, userptr); |
| 213 | } |
| 214 | |
| 215 | int exynos_v4l2_dqbuf(struct exynos_camera *exynos_camera, int exynos_v4l2_id, |
| 216 | int type, int memory) |
| 217 | { |
| 218 | struct v4l2_buffer buffer; |
| 219 | int rc; |
| 220 | |
| 221 | if (exynos_camera == NULL) |
| 222 | return -EINVAL; |
| 223 | |
| 224 | memset(&buffer, 0, sizeof(buffer)); |
| 225 | buffer.type = type; |
| 226 | buffer.memory = memory; |
| 227 | |
| 228 | rc = exynos_v4l2_ioctl(exynos_camera, exynos_v4l2_id, VIDIOC_DQBUF, &buffer); |
| 229 | if (rc < 0) |
| 230 | return rc; |
| 231 | |
| 232 | return buffer.index; |
| 233 | } |
| 234 | |
Dheeraj CVR | a50844d | 2013-12-14 10:54:53 -0600 | [diff] [blame] | 235 | int exynos_v4l2_s_ext_ctrl_face_detection(struct exynos_camera *exynos_camera, |
| 236 | int id, void *value) |
| 237 | { |
| 238 | struct v4l2_ext_control ext_ctrl_fd[111]; |
| 239 | struct v4l2_ext_controls ext_ctrls_fd; |
| 240 | struct v4l2_ext_controls *ctrls; |
| 241 | camera_frame_metadata_t *facedata = (camera_frame_metadata_t *)value; |
| 242 | int i, ret; |
| 243 | |
| 244 | ext_ctrl_fd[0].id = V4L2_CID_IS_FD_GET_FACE_COUNT; |
| 245 | for (i = 0; i < exynos_camera->max_detected_faces; i++) { |
| 246 | ext_ctrl_fd[22*i+1].id = V4L2_CID_IS_FD_GET_FACE_FRAME_NUMBER; |
| 247 | ext_ctrl_fd[22*i+2].id = V4L2_CID_IS_FD_GET_FACE_CONFIDENCE; |
| 248 | ext_ctrl_fd[22*i+3].id = V4L2_CID_IS_FD_GET_FACE_SMILE_LEVEL; |
| 249 | ext_ctrl_fd[22*i+4].id = V4L2_CID_IS_FD_GET_FACE_BLINK_LEVEL; |
| 250 | ext_ctrl_fd[22*i+5].id = V4L2_CID_IS_FD_GET_FACE_TOPLEFT_X; |
| 251 | ext_ctrl_fd[22*i+6].id = V4L2_CID_IS_FD_GET_FACE_TOPLEFT_Y; |
| 252 | ext_ctrl_fd[22*i+7].id = V4L2_CID_IS_FD_GET_FACE_BOTTOMRIGHT_X; |
| 253 | ext_ctrl_fd[22*i+8].id = V4L2_CID_IS_FD_GET_FACE_BOTTOMRIGHT_Y; |
| 254 | ext_ctrl_fd[22*i+9].id = V4L2_CID_IS_FD_GET_LEFT_EYE_TOPLEFT_X; |
| 255 | ext_ctrl_fd[22*i+10].id = V4L2_CID_IS_FD_GET_LEFT_EYE_TOPLEFT_Y; |
| 256 | ext_ctrl_fd[22*i+11].id = V4L2_CID_IS_FD_GET_LEFT_EYE_BOTTOMRIGHT_X; |
| 257 | ext_ctrl_fd[22*i+12].id = V4L2_CID_IS_FD_GET_LEFT_EYE_BOTTOMRIGHT_Y; |
| 258 | ext_ctrl_fd[22*i+13].id = V4L2_CID_IS_FD_GET_RIGHT_EYE_TOPLEFT_X; |
| 259 | ext_ctrl_fd[22*i+14].id = V4L2_CID_IS_FD_GET_RIGHT_EYE_TOPLEFT_Y; |
| 260 | ext_ctrl_fd[22*i+15].id = V4L2_CID_IS_FD_GET_RIGHT_EYE_BOTTOMRIGHT_X; |
| 261 | ext_ctrl_fd[22*i+16].id = V4L2_CID_IS_FD_GET_RIGHT_EYE_BOTTOMRIGHT_Y; |
| 262 | ext_ctrl_fd[22*i+17].id = V4L2_CID_IS_FD_GET_MOUTH_TOPLEFT_X; |
| 263 | ext_ctrl_fd[22*i+18].id = V4L2_CID_IS_FD_GET_MOUTH_TOPLEFT_Y; |
| 264 | ext_ctrl_fd[22*i+19].id = V4L2_CID_IS_FD_GET_MOUTH_BOTTOMRIGHT_X; |
| 265 | ext_ctrl_fd[22*i+20].id = V4L2_CID_IS_FD_GET_MOUTH_BOTTOMRIGHT_Y; |
| 266 | ext_ctrl_fd[22*i+21].id = V4L2_CID_IS_FD_GET_ANGLE; |
| 267 | ext_ctrl_fd[22*i+22].id = V4L2_CID_IS_FD_GET_NEXT; |
| 268 | } |
| 269 | |
| 270 | ext_ctrls_fd.ctrl_class = V4L2_CTRL_CLASS_CAMERA; |
| 271 | ext_ctrls_fd.count = 111; |
| 272 | ext_ctrls_fd.controls = ext_ctrl_fd; |
| 273 | ctrls = &ext_ctrls_fd; |
| 274 | |
| 275 | ret = exynos_v4l2_ioctl(exynos_camera, id, VIDIOC_G_EXT_CTRLS, &ext_ctrls_fd); |
| 276 | |
| 277 | facedata->number_of_faces = ext_ctrls_fd.controls[0].value; |
| 278 | |
| 279 | for(i = 0; i < facedata->number_of_faces; i++) { |
| 280 | facedata->faces[i].rect[0] = ext_ctrl_fd[22*i+5].value; |
| 281 | facedata->faces[i].rect[1] = ext_ctrl_fd[22*i+6].value; |
| 282 | facedata->faces[i].rect[2] = ext_ctrl_fd[22*i+7].value; |
| 283 | facedata->faces[i].rect[3] = ext_ctrl_fd[22*i+8].value; |
| 284 | facedata->faces[i].score = ext_ctrl_fd[22*i+2].value; |
| 285 | /* TODO : id is unique value for each face. We need to suppot this. */ |
| 286 | facedata->faces[i].id = 0; |
| 287 | facedata->faces[i].left_eye[0] = (ext_ctrl_fd[22*i+9].value + ext_ctrl_fd[22*i+11].value) / 2; |
| 288 | facedata->faces[i].left_eye[1] = (ext_ctrl_fd[22*i+10].value + ext_ctrl_fd[22*i+12].value) / 2; |
| 289 | facedata->faces[i].right_eye[0] = (ext_ctrl_fd[22*i+13].value + ext_ctrl_fd[22*i+15].value) / 2; |
| 290 | facedata->faces[i].right_eye[1] = (ext_ctrl_fd[22*i+14].value + ext_ctrl_fd[22*i+16].value) / 2; |
| 291 | facedata->faces[i].mouth[0] = (ext_ctrl_fd[22*i+17].value + ext_ctrl_fd[22*i+19].value) / 2; |
| 292 | facedata->faces[i].mouth[1] = (ext_ctrl_fd[22*i+18].value + ext_ctrl_fd[22*i+20].value) / 2; |
| 293 | } |
| 294 | |
| 295 | return ret; |
| 296 | } |
| 297 | |
Paul Kocialkowski | b31c446 | 2013-09-01 19:22:36 +0200 | [diff] [blame] | 298 | int exynos_v4l2_dqbuf_cap(struct exynos_camera *exynos_camera, |
| 299 | int exynos_v4l2_id) |
| 300 | { |
| 301 | return exynos_v4l2_dqbuf(exynos_camera, exynos_v4l2_id, V4L2_BUF_TYPE_VIDEO_CAPTURE, |
| 302 | V4L2_MEMORY_MMAP); |
| 303 | } |
| 304 | |
| 305 | int exynos_v4l2_dqbuf_out(struct exynos_camera *exynos_camera, |
| 306 | int exynos_v4l2_id) |
| 307 | { |
| 308 | return exynos_v4l2_dqbuf(exynos_camera, exynos_v4l2_id, V4L2_BUF_TYPE_VIDEO_OUTPUT, |
| 309 | V4L2_MEMORY_USERPTR); |
| 310 | } |
| 311 | |
| 312 | int exynos_v4l2_reqbufs(struct exynos_camera *exynos_camera, |
| 313 | int exynos_v4l2_id, int type, int memory, int count) |
| 314 | { |
| 315 | struct v4l2_requestbuffers requestbuffers; |
| 316 | int rc; |
| 317 | |
| 318 | if (exynos_camera == NULL || count < 0) |
| 319 | return -EINVAL; |
| 320 | |
| 321 | requestbuffers.type = type; |
| 322 | requestbuffers.count = count; |
| 323 | requestbuffers.memory = memory; |
| 324 | |
| 325 | rc = exynos_v4l2_ioctl(exynos_camera, exynos_v4l2_id, VIDIOC_REQBUFS, &requestbuffers); |
| 326 | if (rc < 0) |
| 327 | return rc; |
| 328 | |
| 329 | return requestbuffers.count; |
| 330 | } |
| 331 | |
| 332 | int exynos_v4l2_reqbufs_cap(struct exynos_camera *exynos_camera, |
| 333 | int exynos_v4l2_id, int count) |
| 334 | { |
| 335 | return exynos_v4l2_reqbufs(exynos_camera, exynos_v4l2_id, V4L2_BUF_TYPE_VIDEO_CAPTURE, |
| 336 | V4L2_MEMORY_MMAP, count); |
| 337 | } |
| 338 | |
| 339 | int exynos_v4l2_reqbufs_out(struct exynos_camera *exynos_camera, |
| 340 | int exynos_v4l2_id, int count) |
| 341 | { |
| 342 | return exynos_v4l2_reqbufs(exynos_camera, exynos_v4l2_id, V4L2_BUF_TYPE_VIDEO_OUTPUT, |
| 343 | V4L2_MEMORY_USERPTR, count); |
| 344 | } |
| 345 | |
| 346 | int exynos_v4l2_querybuf(struct exynos_camera *exynos_camera, |
| 347 | int exynos_v4l2_id, int type, int memory, int index) |
| 348 | { |
| 349 | struct v4l2_buffer buffer; |
| 350 | int rc; |
| 351 | |
| 352 | if (exynos_camera == NULL) |
| 353 | return -EINVAL; |
| 354 | |
| 355 | memset(&buffer, 0, sizeof(buffer)); |
| 356 | buffer.type = type; |
| 357 | buffer.memory = memory; |
| 358 | buffer.index = index; |
| 359 | |
| 360 | rc = exynos_v4l2_ioctl(exynos_camera, exynos_v4l2_id, VIDIOC_QUERYBUF, &buffer); |
| 361 | if (rc < 0) |
| 362 | return rc; |
| 363 | |
| 364 | return buffer.length; |
| 365 | } |
| 366 | |
| 367 | int exynos_v4l2_querybuf_cap(struct exynos_camera *exynos_camera, |
| 368 | int exynos_v4l2_id, int index) |
| 369 | { |
| 370 | return exynos_v4l2_querybuf(exynos_camera, exynos_v4l2_id, V4L2_BUF_TYPE_VIDEO_CAPTURE, |
| 371 | V4L2_MEMORY_MMAP, index); |
| 372 | } |
| 373 | |
| 374 | int exynos_v4l2_querybuf_out(struct exynos_camera *exynos_camera, |
| 375 | int exynos_v4l2_id, int index) |
| 376 | { |
| 377 | return exynos_v4l2_querybuf(exynos_camera, exynos_v4l2_id, V4L2_BUF_TYPE_VIDEO_OUTPUT, |
| 378 | V4L2_MEMORY_USERPTR, index); |
| 379 | } |
| 380 | |
| 381 | int exynos_v4l2_querycap(struct exynos_camera *exynos_camera, |
| 382 | int exynos_v4l2_id, int flags) |
| 383 | { |
| 384 | struct v4l2_capability cap; |
| 385 | int rc; |
| 386 | |
| 387 | if (exynos_camera == NULL) |
| 388 | return -EINVAL; |
| 389 | |
| 390 | rc = exynos_v4l2_ioctl(exynos_camera, exynos_v4l2_id, VIDIOC_QUERYCAP, &cap); |
| 391 | if (rc < 0) |
| 392 | return rc; |
| 393 | |
| 394 | if (!(cap.capabilities & flags)) |
| 395 | return -1; |
| 396 | |
| 397 | return 0; |
| 398 | } |
| 399 | |
| 400 | int exynos_v4l2_querycap_cap(struct exynos_camera *exynos_camera, |
| 401 | int exynos_v4l2_id) |
| 402 | { |
| 403 | return exynos_v4l2_querycap(exynos_camera, exynos_v4l2_id, V4L2_CAP_VIDEO_CAPTURE); |
| 404 | } |
| 405 | |
| 406 | int exynos_v4l2_querycap_out(struct exynos_camera *exynos_camera, |
| 407 | int exynos_v4l2_id) |
| 408 | { |
| 409 | return exynos_v4l2_querycap(exynos_camera, exynos_v4l2_id, V4L2_CAP_VIDEO_OUTPUT); |
| 410 | } |
| 411 | |
| 412 | int exynos_v4l2_streamon(struct exynos_camera *exynos_camera, |
| 413 | int exynos_v4l2_id, int type) |
| 414 | { |
| 415 | enum v4l2_buf_type buf_type; |
| 416 | int rc; |
| 417 | |
| 418 | if (exynos_camera == NULL) |
| 419 | return -EINVAL; |
| 420 | |
| 421 | buf_type = type; |
| 422 | |
| 423 | rc = exynos_v4l2_ioctl(exynos_camera, exynos_v4l2_id, VIDIOC_STREAMON, &buf_type); |
| 424 | return rc; |
| 425 | } |
| 426 | |
| 427 | int exynos_v4l2_streamon_cap(struct exynos_camera *exynos_camera, |
| 428 | int exynos_v4l2_id) |
| 429 | { |
| 430 | return exynos_v4l2_streamon(exynos_camera, exynos_v4l2_id, V4L2_BUF_TYPE_VIDEO_CAPTURE); |
| 431 | } |
| 432 | |
| 433 | int exynos_v4l2_streamon_out(struct exynos_camera *exynos_camera, |
| 434 | int exynos_v4l2_id) |
| 435 | { |
| 436 | return exynos_v4l2_streamon(exynos_camera, exynos_v4l2_id, V4L2_BUF_TYPE_VIDEO_OUTPUT); |
| 437 | } |
| 438 | |
| 439 | int exynos_v4l2_streamoff(struct exynos_camera *exynos_camera, |
| 440 | int exynos_v4l2_id, int type) |
| 441 | { |
| 442 | enum v4l2_buf_type buf_type; |
| 443 | int rc; |
| 444 | |
| 445 | if (exynos_camera == NULL) |
| 446 | return -EINVAL; |
| 447 | |
| 448 | buf_type = type; |
| 449 | |
| 450 | rc = exynos_v4l2_ioctl(exynos_camera, exynos_v4l2_id, VIDIOC_STREAMOFF, &buf_type); |
| 451 | return rc; |
| 452 | } |
| 453 | |
| 454 | int exynos_v4l2_streamoff_cap(struct exynos_camera *exynos_camera, |
| 455 | int exynos_v4l2_id) |
| 456 | { |
| 457 | return exynos_v4l2_streamoff(exynos_camera, exynos_v4l2_id, V4L2_BUF_TYPE_VIDEO_CAPTURE); |
| 458 | } |
| 459 | |
| 460 | int exynos_v4l2_streamoff_out(struct exynos_camera *exynos_camera, |
| 461 | int exynos_v4l2_id) |
| 462 | { |
| 463 | return exynos_v4l2_streamoff(exynos_camera, exynos_v4l2_id, V4L2_BUF_TYPE_VIDEO_OUTPUT); |
| 464 | } |
| 465 | |
| 466 | int exynos_v4l2_g_fmt(struct exynos_camera *exynos_camera, int exynos_v4l2_id, |
| 467 | int type, int *width, int *height, int *fmt) |
| 468 | { |
| 469 | struct v4l2_format format; |
| 470 | int rc; |
| 471 | |
| 472 | if (exynos_camera == NULL) |
| 473 | return -EINVAL; |
| 474 | |
| 475 | format.type = type; |
| 476 | format.fmt.pix.field = V4L2_FIELD_NONE; |
| 477 | |
| 478 | rc = exynos_v4l2_ioctl(exynos_camera, exynos_v4l2_id, VIDIOC_G_FMT, &format); |
| 479 | if (rc < 0) |
| 480 | return rc; |
| 481 | |
| 482 | if (width != NULL) |
| 483 | *width = format.fmt.pix.width; |
| 484 | if (height != NULL) |
| 485 | *height = format.fmt.pix.height; |
| 486 | if (fmt != NULL) |
| 487 | *fmt = format.fmt.pix.pixelformat; |
| 488 | |
| 489 | return 0; |
| 490 | } |
| 491 | |
| 492 | int exynos_v4l2_g_fmt_cap(struct exynos_camera *exynos_camera, |
| 493 | int exynos_v4l2_id, int *width, int *height, int *fmt) |
| 494 | { |
| 495 | return exynos_v4l2_g_fmt(exynos_camera, exynos_v4l2_id, V4L2_BUF_TYPE_VIDEO_CAPTURE, |
| 496 | width, height, fmt); |
| 497 | } |
| 498 | |
| 499 | int exynos_v4l2_g_fmt_out(struct exynos_camera *exynos_camera, |
| 500 | int exynos_v4l2_id, int *width, int *height, int *fmt) |
| 501 | { |
| 502 | return exynos_v4l2_g_fmt(exynos_camera, exynos_v4l2_id, V4L2_BUF_TYPE_VIDEO_OUTPUT, |
| 503 | width, height, fmt); |
| 504 | } |
| 505 | |
| 506 | int exynos_v4l2_s_fmt_pix(struct exynos_camera *exynos_camera, |
| 507 | int exynos_v4l2_id, int type, int width, int height, int fmt, int field, |
| 508 | int priv) |
| 509 | { |
| 510 | struct v4l2_format format; |
| 511 | int rc; |
| 512 | |
| 513 | if (exynos_camera == NULL) |
| 514 | return -EINVAL; |
| 515 | |
| 516 | memset(&format, 0, sizeof(format)); |
| 517 | format.type = type; |
| 518 | format.fmt.pix.width = width; |
| 519 | format.fmt.pix.height = height; |
| 520 | format.fmt.pix.pixelformat = fmt; |
| 521 | format.fmt.pix.field = field; |
| 522 | format.fmt.pix.priv = priv; |
| 523 | |
| 524 | rc = exynos_v4l2_ioctl(exynos_camera, exynos_v4l2_id, VIDIOC_S_FMT, &format); |
| 525 | return rc; |
| 526 | |
| 527 | return 0; |
| 528 | } |
| 529 | |
| 530 | int exynos_v4l2_s_fmt_pix_cap(struct exynos_camera *exynos_camera, |
| 531 | int exynos_v4l2_id, int width, int height, int fmt, int priv) |
| 532 | { |
| 533 | return exynos_v4l2_s_fmt_pix(exynos_camera, exynos_v4l2_id, V4L2_BUF_TYPE_VIDEO_CAPTURE, |
| 534 | width, height, fmt, V4L2_FIELD_NONE, priv); |
| 535 | } |
| 536 | |
| 537 | int exynos_v4l2_s_fmt_pix_out(struct exynos_camera *exynos_camera, |
| 538 | int exynos_v4l2_id, int width, int height, int fmt, int priv) |
| 539 | { |
| 540 | return exynos_v4l2_s_fmt_pix(exynos_camera, exynos_v4l2_id, V4L2_BUF_TYPE_VIDEO_OUTPUT, |
| 541 | width, height, fmt, V4L2_FIELD_NONE, priv); |
| 542 | } |
| 543 | |
| 544 | int exynos_v4l2_s_fmt_win(struct exynos_camera *exynos_camera, |
| 545 | int exynos_v4l2_id, int left, int top, int width, int height) |
| 546 | { |
| 547 | struct v4l2_format format; |
| 548 | int rc; |
| 549 | |
| 550 | if (exynos_camera == NULL) |
| 551 | return -EINVAL; |
| 552 | |
| 553 | memset(&format, 0, sizeof(format)); |
| 554 | format.type = V4L2_BUF_TYPE_VIDEO_OVERLAY; |
| 555 | format.fmt.win.w.left = left; |
| 556 | format.fmt.win.w.top = top; |
| 557 | format.fmt.win.w.width = width; |
| 558 | format.fmt.win.w.height = height; |
| 559 | |
| 560 | |
| 561 | rc = exynos_v4l2_ioctl(exynos_camera, exynos_v4l2_id, VIDIOC_S_FMT, &format); |
| 562 | return rc; |
| 563 | } |
| 564 | |
| 565 | int exynos_v4l2_enum_fmt(struct exynos_camera *exynos_camera, |
| 566 | int exynos_v4l2_id, int type, int fmt) |
| 567 | { |
| 568 | struct v4l2_fmtdesc fmtdesc; |
| 569 | int rc; |
| 570 | |
| 571 | if (exynos_camera == NULL) |
| 572 | return -EINVAL; |
| 573 | |
| 574 | fmtdesc.type = type; |
| 575 | fmtdesc.index = 0; |
| 576 | |
| 577 | do { |
| 578 | rc = exynos_v4l2_ioctl(exynos_camera, exynos_v4l2_id, VIDIOC_ENUM_FMT, &fmtdesc); |
| 579 | if (rc < 0) |
| 580 | return rc; |
| 581 | |
| 582 | if (fmtdesc.pixelformat == (unsigned int) fmt) |
| 583 | return 0; |
| 584 | |
| 585 | fmtdesc.index++; |
| 586 | } while (rc >= 0); |
| 587 | |
| 588 | return -1; |
| 589 | } |
| 590 | |
| 591 | int exynos_v4l2_enum_fmt_cap(struct exynos_camera *exynos_camera, |
| 592 | int exynos_v4l2_id, int fmt) |
| 593 | { |
| 594 | return exynos_v4l2_enum_fmt(exynos_camera, exynos_v4l2_id, V4L2_BUF_TYPE_VIDEO_CAPTURE, |
| 595 | fmt); |
| 596 | } |
| 597 | |
| 598 | int exynos_v4l2_enum_fmt_out(struct exynos_camera *exynos_camera, |
| 599 | int exynos_v4l2_id, int fmt) |
| 600 | { |
| 601 | return exynos_v4l2_enum_fmt(exynos_camera, exynos_v4l2_id, V4L2_BUF_TYPE_VIDEO_OUTPUT, |
| 602 | fmt); |
| 603 | } |
| 604 | |
| 605 | int exynos_v4l2_enum_input(struct exynos_camera *exynos_camera, |
| 606 | int exynos_v4l2_id, int id) |
| 607 | { |
| 608 | struct v4l2_input input; |
| 609 | int rc; |
| 610 | |
| 611 | if (exynos_camera == NULL || id < 0) |
| 612 | return -EINVAL; |
| 613 | |
| 614 | input.index = id; |
| 615 | |
| 616 | rc = exynos_v4l2_ioctl(exynos_camera, exynos_v4l2_id, VIDIOC_ENUMINPUT, &input); |
| 617 | if (rc < 0) |
| 618 | return rc; |
| 619 | |
| 620 | if (input.name[0] == '\0') |
| 621 | return -1; |
| 622 | |
| 623 | return 0; |
| 624 | } |
| 625 | |
| 626 | int exynos_v4l2_s_input(struct exynos_camera *exynos_camera, int exynos_v4l2_id, |
| 627 | int id) |
| 628 | { |
| 629 | struct v4l2_input input; |
| 630 | int rc; |
| 631 | |
| 632 | if (exynos_camera == NULL || id < 0) |
| 633 | return -EINVAL; |
| 634 | |
| 635 | input.index = id; |
| 636 | |
| 637 | rc = exynos_v4l2_ioctl(exynos_camera, exynos_v4l2_id, VIDIOC_S_INPUT, &input); |
| 638 | return rc; |
| 639 | } |
| 640 | |
| 641 | int exynos_v4l2_g_ext_ctrls(struct exynos_camera *exynos_camera, |
| 642 | int exynos_v4l2_id, struct v4l2_ext_control *control, int count) |
| 643 | { |
| 644 | struct v4l2_ext_controls controls; |
| 645 | int rc; |
| 646 | |
| 647 | if (exynos_camera == NULL || control == NULL) |
| 648 | return -EINVAL; |
| 649 | |
| 650 | memset(&controls, 0, sizeof(controls)); |
| 651 | controls.ctrl_class = V4L2_CTRL_CLASS_CAMERA; |
| 652 | controls.count = count; |
| 653 | controls.controls = control; |
| 654 | |
| 655 | rc = exynos_v4l2_ioctl(exynos_camera, exynos_v4l2_id, VIDIOC_G_EXT_CTRLS, &controls); |
| 656 | return rc; |
| 657 | } |
| 658 | |
| 659 | int exynos_v4l2_g_ctrl(struct exynos_camera *exynos_camera, int exynos_v4l2_id, |
| 660 | int id, int *value) |
| 661 | { |
| 662 | struct v4l2_control control; |
| 663 | int rc; |
| 664 | |
| 665 | if (exynos_camera == NULL) |
| 666 | return -EINVAL; |
| 667 | |
| 668 | control.id = id; |
| 669 | |
| 670 | rc = exynos_v4l2_ioctl(exynos_camera, exynos_v4l2_id, VIDIOC_G_CTRL, &control); |
| 671 | if (rc < 0) |
| 672 | return rc; |
| 673 | |
| 674 | if (value != NULL) |
| 675 | *value = control.value; |
| 676 | |
| 677 | return 0; |
| 678 | } |
| 679 | |
| 680 | int exynos_v4l2_s_ctrl(struct exynos_camera *exynos_camera, int exynos_v4l2_id, |
| 681 | int id, int value) |
| 682 | { |
| 683 | struct v4l2_control control; |
| 684 | int rc; |
| 685 | |
| 686 | if (exynos_camera == NULL) |
| 687 | return -EINVAL; |
| 688 | |
| 689 | control.id = id; |
| 690 | control.value = value; |
| 691 | |
| 692 | rc = exynos_v4l2_ioctl(exynos_camera, exynos_v4l2_id, VIDIOC_S_CTRL, &control); |
| 693 | if (rc < 0) |
| 694 | return rc; |
| 695 | |
| 696 | return control.value; |
| 697 | } |
| 698 | |
| 699 | int exynos_v4l2_s_parm(struct exynos_camera *exynos_camera, int exynos_v4l2_id, |
| 700 | int type, struct v4l2_streamparm *streamparm) |
| 701 | { |
| 702 | int rc; |
| 703 | |
| 704 | if (exynos_camera == NULL || streamparm == NULL) |
| 705 | return -EINVAL; |
| 706 | |
| 707 | streamparm->type = type; |
| 708 | |
| 709 | rc = exynos_v4l2_ioctl(exynos_camera, exynos_v4l2_id, VIDIOC_S_PARM, streamparm); |
| 710 | return rc; |
| 711 | } |
| 712 | |
| 713 | int exynos_v4l2_s_parm_cap(struct exynos_camera *exynos_camera, |
| 714 | int exynos_v4l2_id, struct v4l2_streamparm *streamparm) |
| 715 | { |
| 716 | return exynos_v4l2_s_parm(exynos_camera, exynos_v4l2_id, V4L2_BUF_TYPE_VIDEO_CAPTURE, |
| 717 | streamparm); |
| 718 | } |
| 719 | |
| 720 | int exynos_v4l2_s_parm_out(struct exynos_camera *exynos_camera, |
| 721 | int exynos_v4l2_id, struct v4l2_streamparm *streamparm) |
| 722 | { |
| 723 | return exynos_v4l2_s_parm(exynos_camera, exynos_v4l2_id, V4L2_BUF_TYPE_VIDEO_OUTPUT, |
| 724 | streamparm); |
| 725 | } |
| 726 | |
| 727 | int exynos_v4l2_s_crop(struct exynos_camera *exynos_camera, int exynos_v4l2_id, |
| 728 | int type, int left, int top, int width, int height) |
| 729 | { |
| 730 | struct v4l2_crop crop; |
| 731 | int rc; |
| 732 | |
| 733 | if (exynos_camera == NULL) |
| 734 | return -EINVAL; |
| 735 | |
| 736 | crop.type = type; |
| 737 | crop.c.left = left; |
| 738 | crop.c.top = top; |
| 739 | crop.c.width = width; |
| 740 | crop.c.height = height; |
| 741 | |
| 742 | rc = exynos_v4l2_ioctl(exynos_camera, exynos_v4l2_id, VIDIOC_S_CROP, &crop); |
| 743 | return rc; |
| 744 | } |
| 745 | |
| 746 | int exynos_v4l2_s_crop_cap(struct exynos_camera *exynos_camera, |
| 747 | int exynos_v4l2_id, int left, int top, int width, int height) |
| 748 | { |
| 749 | return exynos_v4l2_s_crop(exynos_camera, exynos_v4l2_id, V4L2_BUF_TYPE_VIDEO_CAPTURE, |
| 750 | left, top, width, height); |
| 751 | } |
| 752 | |
| 753 | int exynos_v4l2_s_crop_out(struct exynos_camera *exynos_camera, |
| 754 | int exynos_v4l2_id, int left, int top, int width, int height) |
| 755 | { |
| 756 | return exynos_v4l2_s_crop(exynos_camera, exynos_v4l2_id, V4L2_BUF_TYPE_VIDEO_OUTPUT, |
| 757 | left, top, width, height); |
| 758 | } |
| 759 | |
| 760 | int exynos_v4l2_g_fbuf(struct exynos_camera *exynos_camera, int exynos_v4l2_id, |
| 761 | void **base, int *width, int *height, int *fmt) |
| 762 | { |
| 763 | struct v4l2_framebuffer framebuffer; |
| 764 | int rc; |
| 765 | |
| 766 | if (exynos_camera == NULL) |
| 767 | return -EINVAL; |
| 768 | |
| 769 | rc = exynos_v4l2_ioctl(exynos_camera, exynos_v4l2_id, VIDIOC_G_FBUF, &framebuffer); |
| 770 | if (rc < 0) |
| 771 | return rc; |
| 772 | |
| 773 | if (base != NULL) |
| 774 | *base = framebuffer.base; |
| 775 | if (width != NULL) |
| 776 | *width = framebuffer.fmt.width; |
| 777 | if (height != NULL) |
| 778 | *height = framebuffer.fmt.height; |
| 779 | if (fmt != NULL) |
| 780 | *fmt = framebuffer.fmt.pixelformat; |
| 781 | |
| 782 | return 0; |
| 783 | } |
| 784 | |
| 785 | int exynos_v4l2_s_fbuf(struct exynos_camera *exynos_camera, int exynos_v4l2_id, |
| 786 | void *base, int width, int height, int fmt) |
| 787 | { |
| 788 | struct v4l2_framebuffer framebuffer; |
| 789 | int rc; |
| 790 | |
| 791 | if (exynos_camera == NULL) |
| 792 | return -EINVAL; |
| 793 | |
| 794 | memset(&framebuffer, 0, sizeof(framebuffer)); |
| 795 | framebuffer.base = base; |
| 796 | framebuffer.fmt.width = width; |
| 797 | framebuffer.fmt.height = height; |
| 798 | framebuffer.fmt.pixelformat = fmt; |
| 799 | |
| 800 | rc = exynos_v4l2_ioctl(exynos_camera, exynos_v4l2_id, VIDIOC_S_FBUF, &framebuffer); |
| 801 | return rc; |
| 802 | } |