xref: /OK3568_Linux_fs/external/camera_engine_rkaiq/rkaiq/xcore/xcam_common.cpp (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * xcam_common.cpp - xcam common
3  *
4  *  Copyright (c) 2014-2015 Intel Corporation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * Author: Wind Yuan <feng.yuan@intel.com>
19  */
20 
21 #include <base/xcam_common.h>
22 #include <base/xcam_log.h>
23 #include <fcntl.h>
24 #include <errno.h>
25 #include <sys/ioctl.h>
26 #include <stdarg.h>
27 
xcam_malloc(size_t size)28 void * xcam_malloc(size_t size)
29 {
30     return malloc (size);
31 }
32 
xcam_malloc0(size_t size)33 void * xcam_malloc0(size_t size)
34 {
35     void * ptr = malloc (size);
36     memset (ptr, 0, size);
37     return ptr;
38 }
39 
xcam_free(void * ptr)40 void xcam_free(void *ptr)
41 {
42     if (ptr)
43         free (ptr);
44 }
45 
xcam_device_ioctl(int fd,unsigned long cmd,void * arg)46 int xcam_device_ioctl (int fd, unsigned long cmd, void *arg)
47 {
48     int ret = 0;
49     int tried_time = 0;
50 
51     if (fd < 0)
52         return -1;
53 
54     while (1) {
55         ret = ioctl (fd, cmd, arg);
56         if (ret >= 0)
57             break;
58         if (errno != EINTR && errno != EAGAIN)
59             break;
60         if (++tried_time > 5)
61             break;
62     }
63 
64     if (ret >= 0) {
65         XCAM_LOG_DEBUG ("ioctl return ok on fd(%d), cmd:0x%.8x", fd, cmd);
66     } else {
67         XCAM_LOG_DEBUG ("ioctl failed on fd(%d), cmd:0x%.8x, error:%s",
68                         fd, cmd, strerror(errno));
69     }
70     return ret;
71 }
72 
73 const char *
xcam_fourcc_to_string(uint32_t fourcc)74 xcam_fourcc_to_string (uint32_t fourcc)
75 {
76     static char str[5];
77 
78     xcam_mem_clear (str);
79     memcpy (str, &fourcc, 4);
80     return str;
81 }
82