Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 1 | /* |
| 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-Cohen | 63f6982 | 2016-06-10 11:40:35 -0700 | [diff] [blame] | 21 | #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-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 30 | #include <cstdlib> |
Ari Hausman-Cohen | 63f6982 | 2016-06-10 11:40:35 -0700 | [diff] [blame] | 31 | #include <unordered_set> |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 32 | |
Ari Hausman-Cohen | 345bd3a | 2016-06-13 15:33:53 -0700 | [diff] [blame] | 33 | #include <android-base/parseint.h> |
| 34 | |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 35 | #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 | |
| 46 | namespace v4l2_camera_hal { |
| 47 | |
| 48 | // Default global camera hal. |
| 49 | static V4L2CameraHAL gCameraHAL; |
| 50 | |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 51 | V4L2CameraHAL::V4L2CameraHAL() : mCameras(), mCallbacks(NULL) { |
| 52 | HAL_LOG_ENTER(); |
Ari Hausman-Cohen | 63f6982 | 2016-06-10 11:40:35 -0700 | [diff] [blame] | 53 | // 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()); |
| 98 | std::unique_ptr<V4L2Camera> cam(new V4L2Camera(id++, node)); |
| 99 | mCameras.push_back(std::move(cam)); |
| 100 | } |
| 101 | } |
| 102 | TEMP_FAILURE_RETRY(close(fd)); |
| 103 | } |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | V4L2CameraHAL::~V4L2CameraHAL() { |
| 107 | HAL_LOG_ENTER(); |
| 108 | } |
| 109 | |
| 110 | int V4L2CameraHAL::getNumberOfCameras() { |
| 111 | HAL_LOGV("returns %d", mCameras.size()); |
| 112 | return mCameras.size(); |
| 113 | } |
| 114 | |
| 115 | int V4L2CameraHAL::getCameraInfo(int id, camera_info_t* info) { |
| 116 | HAL_LOG_ENTER(); |
| 117 | if (id < 0 || id >= mCameras.size()) { |
| 118 | return -EINVAL; |
| 119 | } |
| 120 | // TODO(b/29185945): Hotplugging: return -EINVAL if unplugged. |
| 121 | return mCameras[id]->getInfo(info); |
| 122 | } |
| 123 | |
| 124 | int V4L2CameraHAL::setCallbacks(const camera_module_callbacks_t* callbacks) { |
| 125 | HAL_LOG_ENTER(); |
| 126 | mCallbacks = callbacks; |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | void V4L2CameraHAL::getVendorTagOps(vendor_tag_ops_t* ops) { |
| 131 | HAL_LOG_ENTER(); |
| 132 | // No vendor ops for this HAL. From <hardware/camera_common.h>: |
| 133 | // "leave ops unchanged if no vendor tags are defined." |
| 134 | } |
| 135 | |
| 136 | int V4L2CameraHAL::openLegacy(const hw_module_t* module, const char* id, |
| 137 | uint32_t halVersion, hw_device_t** device) { |
| 138 | HAL_LOG_ENTER(); |
| 139 | // Not supported. |
| 140 | return -ENOSYS; |
| 141 | } |
| 142 | |
| 143 | int V4L2CameraHAL::setTorchMode(const char* camera_id, bool enabled) { |
| 144 | HAL_LOG_ENTER(); |
| 145 | // TODO(b/29158098): HAL is required to respond appropriately if |
| 146 | // the desired camera actually does support flash. |
| 147 | return -ENOSYS; |
| 148 | } |
| 149 | |
Ari Hausman-Cohen | 63f6982 | 2016-06-10 11:40:35 -0700 | [diff] [blame] | 150 | int V4L2CameraHAL::openDevice(const hw_module_t* module, const char* name, |
| 151 | hw_device_t** device) { |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 152 | HAL_LOG_ENTER(); |
| 153 | |
| 154 | if (module != &HAL_MODULE_INFO_SYM.common) { |
| 155 | HAL_LOGE("Invalid module %p expected %p", module, |
| 156 | &HAL_MODULE_INFO_SYM.common); |
| 157 | return -EINVAL; |
| 158 | } |
| 159 | |
Ari Hausman-Cohen | 345bd3a | 2016-06-13 15:33:53 -0700 | [diff] [blame] | 160 | int id; |
| 161 | if (!android::base::ParseInt(name, &id, 0, getNumberOfCameras() - 1)) { |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 162 | return -EINVAL; |
| 163 | } |
| 164 | // TODO(b/29185945): Hotplugging: return -EINVAL if unplugged. |
Ari Hausman-Cohen | 345bd3a | 2016-06-13 15:33:53 -0700 | [diff] [blame] | 165 | return mCameras[id]->openDevice(module, device); |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | /* |
| 169 | * The framework calls the following wrappers, which in turn |
| 170 | * call the corresponding methods of the global HAL object. |
| 171 | */ |
| 172 | |
| 173 | static int get_number_of_cameras() { |
| 174 | return gCameraHAL.getNumberOfCameras(); |
| 175 | } |
| 176 | |
| 177 | static int get_camera_info(int id, struct camera_info* info) { |
| 178 | return gCameraHAL.getCameraInfo(id, info); |
| 179 | } |
| 180 | |
| 181 | static int set_callbacks(const camera_module_callbacks_t *callbacks) { |
| 182 | return gCameraHAL.setCallbacks(callbacks); |
| 183 | } |
| 184 | |
| 185 | static void get_vendor_tag_ops(vendor_tag_ops_t* ops) { |
| 186 | return gCameraHAL.getVendorTagOps(ops); |
| 187 | } |
| 188 | |
| 189 | static int open_legacy(const hw_module_t* module, const char* id, |
| 190 | uint32_t halVersion, hw_device_t** device) { |
| 191 | return gCameraHAL.openLegacy(module, id, halVersion, device); |
| 192 | } |
| 193 | |
| 194 | static int set_torch_mode(const char* camera_id, bool enabled) { |
| 195 | return gCameraHAL.setTorchMode(camera_id, enabled); |
| 196 | } |
| 197 | |
| 198 | static int open_dev(const hw_module_t* module, const char* name, |
| 199 | hw_device_t** device) { |
Ari Hausman-Cohen | 63f6982 | 2016-06-10 11:40:35 -0700 | [diff] [blame] | 200 | return gCameraHAL.openDevice(module, name, device); |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | } // namespace v4l2_camera_hal |
| 204 | |
| 205 | static hw_module_methods_t v4l2_module_methods = { |
| 206 | .open = v4l2_camera_hal::open_dev |
| 207 | }; |
| 208 | |
Ari Hausman-Cohen | 77db7d0 | 2016-06-13 17:15:06 -0700 | [diff] [blame] | 209 | camera_module_t HAL_MODULE_INFO_SYM __attribute__ ((visibility("default"))) = { |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 210 | .common = { |
| 211 | .tag = HARDWARE_MODULE_TAG, |
| 212 | .module_api_version = CAMERA_MODULE_API_VERSION_2_4, |
| 213 | .hal_api_version = HARDWARE_HAL_API_VERSION, |
| 214 | .id = CAMERA_HARDWARE_MODULE_ID, |
| 215 | .name = "V4L2 Camera HAL v3", |
| 216 | .author = "The Android Open Source Project", |
| 217 | .methods = &v4l2_module_methods, |
| 218 | .dso = nullptr, |
| 219 | .reserved = {0}, |
| 220 | }, |
| 221 | .get_number_of_cameras = v4l2_camera_hal::get_number_of_cameras, |
| 222 | .get_camera_info = v4l2_camera_hal::get_camera_info, |
| 223 | .set_callbacks = v4l2_camera_hal::set_callbacks, |
| 224 | .get_vendor_tag_ops = v4l2_camera_hal::get_vendor_tag_ops, |
| 225 | .open_legacy = v4l2_camera_hal::open_legacy, |
| 226 | .set_torch_mode = v4l2_camera_hal::set_torch_mode, |
| 227 | .init = nullptr, |
| 228 | .reserved = {nullptr, nullptr, nullptr, nullptr, nullptr} |
| 229 | }; |