blob: 40797e9ff512b10915ab96e14b3b9120752845aa [file] [log] [blame]
Ari Hausman-Cohen73442152016-06-08 15:50:49 -07001/*
2 * Copyright 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17// Modified from hardware/libhardware/modules/camera/CameraHAL.cpp
18
19#include "V4L2CameraHAL.h"
20
Ari Hausman-Cohen63f69822016-06-10 11:40:35 -070021#include <dirent.h>
22#include <fcntl.h>
23#include <linux/videodev2.h>
24#include <sys/ioctl.h>
25#include <sys/types.h>
26#include <sys/stat.h>
27#include <unistd.h>
28
29#include <algorithm>
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070030#include <cstdlib>
Ari Hausman-Cohen63f69822016-06-10 11:40:35 -070031#include <unordered_set>
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070032
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070033#include <android-base/parseint.h>
34
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070035#include "Common.h"
36#include "V4L2Camera.h"
37
38/*
39 * This file serves as the entry point to the HAL. It is modified from the
40 * example default HAL available in hardware/libhardware/modules/camera.
41 * It contains the module structure and functions used by the framework
42 * to load and interface to this HAL, as well as the handles to the individual
43 * camera devices.
44 */
45
46namespace v4l2_camera_hal {
47
48// Default global camera hal.
49static V4L2CameraHAL gCameraHAL;
50
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070051V4L2CameraHAL::V4L2CameraHAL() : mCameras(), mCallbacks(NULL) {
52 HAL_LOG_ENTER();
Ari Hausman-Cohen63f69822016-06-10 11:40:35 -070053 // Adds all available V4L2 devices.
54 // List /dev nodes.
55 DIR* dir = opendir("/dev");
56 if (dir == NULL) {
57 HAL_LOGE("Failed to open /dev");
58 return;
59 }
60 // Find /dev/video* nodes.
61 dirent* ent;
62 std::vector<std::string> nodes;
63 while ((ent = readdir(dir))) {
64 std::string desired = "video";
65 size_t len = desired.size();
66 if (strncmp(desired.c_str(), ent->d_name, len) == 0) {
67 if (strlen(ent->d_name) > len && isdigit(ent->d_name[len])) {
68 // ent is a numbered video node.
69 nodes.push_back(std::string("/dev/") + ent->d_name);
70 HAL_LOGV("Found video node %s.", nodes.back().c_str());
71 }
72 }
73 }
74 // Test each for V4L2 support and uniqueness.
75 std::unordered_set<std::string> buses;
76 std::string bus;
77 v4l2_capability cap;
78 int fd;
79 int id = 0;
80 for (const auto& node : nodes) {
81 // Open the node.
82 fd = TEMP_FAILURE_RETRY(open(node.c_str(), O_RDWR));
83 if (fd < 0) {
84 HAL_LOGE("failed to open %s (%s).", node.c_str(), strerror(errno));
85 continue;
86 }
87 // Read V4L2 capabilities.
88 if (TEMP_FAILURE_RETRY(ioctl(fd, VIDIOC_QUERYCAP, &cap)) != 0) {
89 HAL_LOGE("VIDIOC_QUERYCAP on %s fail: %s.", node.c_str(),
90 strerror(errno));
91 } else if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
92 HAL_LOGE("%s is not a V4L2 video capture device.", node.c_str());
93 } else {
94 // If the node is unique, add a camera for it.
95 bus = reinterpret_cast<char*>(cap.bus_info);
96 if (buses.insert(bus).second) {
97 HAL_LOGV("Found unique bus at %s.", node.c_str());
Ari Hausman-Cohen681eaa22016-07-21 16:28:17 -070098 std::unique_ptr<V4L2Camera> cam(V4L2Camera::NewV4L2Camera(id++, node));
99 if (cam) {
100 mCameras.push_back(std::move(cam));
101 } else {
102 HAL_LOGE("Failed to initialize camera at %s.", node.c_str());
103 }
Ari Hausman-Cohen63f69822016-06-10 11:40:35 -0700104 }
105 }
106 TEMP_FAILURE_RETRY(close(fd));
107 }
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700108}
109
110V4L2CameraHAL::~V4L2CameraHAL() {
111 HAL_LOG_ENTER();
112}
113
114int V4L2CameraHAL::getNumberOfCameras() {
115 HAL_LOGV("returns %d", mCameras.size());
116 return mCameras.size();
117}
118
119int V4L2CameraHAL::getCameraInfo(int id, camera_info_t* info) {
120 HAL_LOG_ENTER();
121 if (id < 0 || id >= mCameras.size()) {
122 return -EINVAL;
123 }
124 // TODO(b/29185945): Hotplugging: return -EINVAL if unplugged.
125 return mCameras[id]->getInfo(info);
126}
127
128int V4L2CameraHAL::setCallbacks(const camera_module_callbacks_t* callbacks) {
129 HAL_LOG_ENTER();
130 mCallbacks = callbacks;
131 return 0;
132}
133
134void V4L2CameraHAL::getVendorTagOps(vendor_tag_ops_t* ops) {
135 HAL_LOG_ENTER();
136 // No vendor ops for this HAL. From <hardware/camera_common.h>:
137 // "leave ops unchanged if no vendor tags are defined."
138}
139
140int V4L2CameraHAL::openLegacy(const hw_module_t* module, const char* id,
141 uint32_t halVersion, hw_device_t** device) {
142 HAL_LOG_ENTER();
143 // Not supported.
144 return -ENOSYS;
145}
146
147int V4L2CameraHAL::setTorchMode(const char* camera_id, bool enabled) {
148 HAL_LOG_ENTER();
149 // TODO(b/29158098): HAL is required to respond appropriately if
150 // the desired camera actually does support flash.
151 return -ENOSYS;
152}
153
Ari Hausman-Cohen63f69822016-06-10 11:40:35 -0700154int V4L2CameraHAL::openDevice(const hw_module_t* module, const char* name,
155 hw_device_t** device) {
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700156 HAL_LOG_ENTER();
157
158 if (module != &HAL_MODULE_INFO_SYM.common) {
159 HAL_LOGE("Invalid module %p expected %p", module,
160 &HAL_MODULE_INFO_SYM.common);
161 return -EINVAL;
162 }
163
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -0700164 int id;
165 if (!android::base::ParseInt(name, &id, 0, getNumberOfCameras() - 1)) {
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700166 return -EINVAL;
167 }
168 // TODO(b/29185945): Hotplugging: return -EINVAL if unplugged.
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -0700169 return mCameras[id]->openDevice(module, device);
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700170}
171
172/*
173 * The framework calls the following wrappers, which in turn
174 * call the corresponding methods of the global HAL object.
175 */
176
177static int get_number_of_cameras() {
178 return gCameraHAL.getNumberOfCameras();
179}
180
181static int get_camera_info(int id, struct camera_info* info) {
182 return gCameraHAL.getCameraInfo(id, info);
183}
184
185static int set_callbacks(const camera_module_callbacks_t *callbacks) {
186 return gCameraHAL.setCallbacks(callbacks);
187}
188
189static void get_vendor_tag_ops(vendor_tag_ops_t* ops) {
190 return gCameraHAL.getVendorTagOps(ops);
191}
192
193static int open_legacy(const hw_module_t* module, const char* id,
194 uint32_t halVersion, hw_device_t** device) {
195 return gCameraHAL.openLegacy(module, id, halVersion, device);
196}
197
198static int set_torch_mode(const char* camera_id, bool enabled) {
199 return gCameraHAL.setTorchMode(camera_id, enabled);
200}
201
202static int open_dev(const hw_module_t* module, const char* name,
203 hw_device_t** device) {
Ari Hausman-Cohen63f69822016-06-10 11:40:35 -0700204 return gCameraHAL.openDevice(module, name, device);
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700205}
206
207} // namespace v4l2_camera_hal
208
209static hw_module_methods_t v4l2_module_methods = {
210 .open = v4l2_camera_hal::open_dev
211};
212
Ari Hausman-Cohen77db7d02016-06-13 17:15:06 -0700213camera_module_t HAL_MODULE_INFO_SYM __attribute__ ((visibility("default"))) = {
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700214 .common = {
215 .tag = HARDWARE_MODULE_TAG,
216 .module_api_version = CAMERA_MODULE_API_VERSION_2_4,
217 .hal_api_version = HARDWARE_HAL_API_VERSION,
218 .id = CAMERA_HARDWARE_MODULE_ID,
219 .name = "V4L2 Camera HAL v3",
220 .author = "The Android Open Source Project",
221 .methods = &v4l2_module_methods,
222 .dso = nullptr,
223 .reserved = {0},
224 },
225 .get_number_of_cameras = v4l2_camera_hal::get_number_of_cameras,
226 .get_camera_info = v4l2_camera_hal::get_camera_info,
227 .set_callbacks = v4l2_camera_hal::set_callbacks,
228 .get_vendor_tag_ops = v4l2_camera_hal::get_vendor_tag_ops,
229 .open_legacy = v4l2_camera_hal::open_legacy,
230 .set_torch_mode = v4l2_camera_hal::set_torch_mode,
231 .init = nullptr,
232 .reserved = {nullptr, nullptr, nullptr, nullptr, nullptr}
233};