Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -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 | |
Ari Hausman-Cohen | 3841a7f | 2016-07-19 17:27:52 -0700 | [diff] [blame^] | 17 | #include "v4l2_wrapper.h" |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 18 | |
| 19 | #include <fcntl.h> |
| 20 | #include <linux/videodev2.h> |
| 21 | #include <sys/stat.h> |
| 22 | #include <sys/types.h> |
| 23 | |
| 24 | #include <mutex> |
| 25 | |
| 26 | #include <nativehelper/ScopedFd.h> |
| 27 | |
Ari Hausman-Cohen | 3841a7f | 2016-07-19 17:27:52 -0700 | [diff] [blame^] | 28 | #include "common.h" |
| 29 | #include "stream.h" |
| 30 | #include "stream_format.h" |
| 31 | #include "v4l2_gralloc.h" |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 32 | |
| 33 | namespace v4l2_camera_hal { |
| 34 | |
Ari Hausman-Cohen | 4ab4962 | 2016-07-21 14:33:54 -0700 | [diff] [blame] | 35 | V4L2Wrapper* V4L2Wrapper::NewV4L2Wrapper(const std::string device_path) { |
| 36 | HAL_LOG_ENTER(); |
| 37 | |
| 38 | std::unique_ptr<V4L2Gralloc> gralloc(V4L2Gralloc::NewV4L2Gralloc()); |
| 39 | if (!gralloc) { |
| 40 | HAL_LOGE("Failed to initialize gralloc helper."); |
| 41 | return nullptr; |
| 42 | } |
| 43 | |
| 44 | return new V4L2Wrapper(device_path, std::move(gralloc)); |
| 45 | } |
| 46 | |
| 47 | V4L2Wrapper::V4L2Wrapper(const std::string device_path, |
| 48 | std::unique_ptr<V4L2Gralloc> gralloc) |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 49 | : device_path_(std::move(device_path)), |
| 50 | gralloc_(std::move(gralloc)), |
| 51 | max_buffers_(0) { |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 52 | HAL_LOG_ENTER(); |
| 53 | } |
| 54 | |
| 55 | V4L2Wrapper::~V4L2Wrapper() { HAL_LOG_ENTER(); } |
| 56 | |
| 57 | int V4L2Wrapper::Connect() { |
| 58 | HAL_LOG_ENTER(); |
| 59 | std::lock_guard<std::mutex> lock(device_lock_); |
| 60 | |
| 61 | if (connected()) { |
| 62 | HAL_LOGE("Camera device %s is already connected. Close it first", |
| 63 | device_path_.c_str()); |
| 64 | return -EIO; |
| 65 | } |
| 66 | |
| 67 | int fd = TEMP_FAILURE_RETRY(open(device_path_.c_str(), O_RDWR)); |
| 68 | if (fd < 0) { |
| 69 | HAL_LOGE("failed to open %s (%s)", device_path_.c_str(), strerror(errno)); |
| 70 | return -errno; |
| 71 | } |
| 72 | device_fd_.reset(fd); |
| 73 | |
| 74 | // Check if this connection has the extended control query capability. |
| 75 | v4l2_query_ext_ctrl query; |
| 76 | query.id = V4L2_CTRL_FLAG_NEXT_CTRL | V4L2_CTRL_FLAG_NEXT_COMPOUND; |
Ari Hausman-Cohen | 4ab4962 | 2016-07-21 14:33:54 -0700 | [diff] [blame] | 77 | // Already holding the lock, so don't call IoctlLocked. |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 78 | int res = TEMP_FAILURE_RETRY( |
| 79 | ioctl(device_fd_.get(), VIDIOC_QUERY_EXT_CTRL, &query)); |
| 80 | extended_query_supported_ = (res == 0); |
| 81 | |
| 82 | // TODO(b/29185945): confirm this is a supported device. |
| 83 | // This is checked by the HAL, but the device at device_path_ may |
| 84 | // not be the same one that was there when the HAL was loaded. |
| 85 | // (Alternatively, better hotplugging support may make this unecessary |
| 86 | // by disabling cameras that get disconnected and checking newly connected |
| 87 | // cameras, so Connect() is never called on an unsupported camera) |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | void V4L2Wrapper::Disconnect() { |
| 92 | HAL_LOG_ENTER(); |
| 93 | std::lock_guard<std::mutex> lock(device_lock_); |
| 94 | |
| 95 | device_fd_.reset(); // Includes close(). |
| 96 | format_.reset(); |
| 97 | max_buffers_ = 0; |
Ari Hausman-Cohen | 4ab4962 | 2016-07-21 14:33:54 -0700 | [diff] [blame] | 98 | // Closing the device releases all queued buffers back to the user. |
| 99 | gralloc_->unlockAllBuffers(); |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | // Helper function. Should be used instead of ioctl throughout this class. |
| 103 | template <typename T> |
| 104 | int V4L2Wrapper::IoctlLocked(int request, T data) { |
| 105 | HAL_LOG_ENTER(); |
| 106 | std::lock_guard<std::mutex> lock(device_lock_); |
| 107 | |
| 108 | if (!connected()) { |
| 109 | HAL_LOGE("Device %s not connected.", device_path_.c_str()); |
| 110 | return -ENODEV; |
| 111 | } |
| 112 | return TEMP_FAILURE_RETRY(ioctl(device_fd_.get(), request, data)); |
| 113 | } |
| 114 | |
| 115 | int V4L2Wrapper::StreamOn() { |
| 116 | HAL_LOG_ENTER(); |
| 117 | |
| 118 | if (!format_) { |
| 119 | HAL_LOGE("Stream format must be set before turning on stream."); |
| 120 | return -EINVAL; |
| 121 | } |
| 122 | |
Ari Hausman-Cohen | 4ab4962 | 2016-07-21 14:33:54 -0700 | [diff] [blame] | 123 | int32_t type = format_->type(); |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 124 | if (IoctlLocked(VIDIOC_STREAMON, &type) < 0) { |
| 125 | HAL_LOGE("STREAMON fails: %s", strerror(errno)); |
| 126 | return -ENODEV; |
| 127 | } |
| 128 | |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | int V4L2Wrapper::StreamOff() { |
| 133 | HAL_LOG_ENTER(); |
| 134 | |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 135 | if (!format_) { |
| 136 | HAL_LOGE("Stream format must be set to turn off stream."); |
| 137 | return -ENODEV; |
| 138 | } |
| 139 | |
Ari Hausman-Cohen | 4ab4962 | 2016-07-21 14:33:54 -0700 | [diff] [blame] | 140 | int32_t type = format_->type(); |
| 141 | int res = IoctlLocked(VIDIOC_STREAMOFF, &type); |
| 142 | // Calling STREAMOFF releases all queued buffers back to the user. |
| 143 | int gralloc_res = gralloc_->unlockAllBuffers(); |
| 144 | if (res < 0) { |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 145 | HAL_LOGE("STREAMOFF fails: %s", strerror(errno)); |
| 146 | return -ENODEV; |
| 147 | } |
Ari Hausman-Cohen | 4ab4962 | 2016-07-21 14:33:54 -0700 | [diff] [blame] | 148 | if (gralloc_res < 0) { |
| 149 | HAL_LOGE("Failed to unlock all buffers after turning stream off."); |
| 150 | return gralloc_res; |
| 151 | } |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 152 | |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | int V4L2Wrapper::QueryControl(uint32_t control_id, |
| 157 | v4l2_query_ext_ctrl* result) { |
| 158 | HAL_LOG_ENTER(); |
| 159 | int res; |
| 160 | |
| 161 | memset(result, 0, sizeof(*result)); |
| 162 | |
| 163 | if (extended_query_supported_) { |
| 164 | result->id = control_id; |
| 165 | res = IoctlLocked(VIDIOC_QUERY_EXT_CTRL, result); |
| 166 | // Assuming the operation was supported (not ENOTTY), no more to do. |
| 167 | if (errno != ENOTTY) { |
| 168 | if (res) { |
| 169 | HAL_LOGE("QUERY_EXT_CTRL fails: %s", strerror(errno)); |
| 170 | return -ENODEV; |
| 171 | } |
| 172 | return 0; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | // Extended control querying not supported, fall back to basic control query. |
| 177 | v4l2_queryctrl query; |
| 178 | query.id = control_id; |
| 179 | if (IoctlLocked(VIDIOC_QUERYCTRL, &query)) { |
| 180 | HAL_LOGE("QUERYCTRL fails: %s", strerror(errno)); |
| 181 | return -ENODEV; |
| 182 | } |
| 183 | |
| 184 | // Convert the basic result to the extended result. |
| 185 | result->id = query.id; |
| 186 | result->type = query.type; |
| 187 | memcpy(result->name, query.name, sizeof(query.name)); |
| 188 | result->minimum = query.minimum; |
| 189 | if (query.type == V4L2_CTRL_TYPE_BITMASK) { |
| 190 | // According to the V4L2 documentation, when type is BITMASK, |
| 191 | // max and default should be interpreted as __u32. Practically, |
| 192 | // this means the conversion from 32 bit to 64 will pad with 0s not 1s. |
| 193 | result->maximum = static_cast<uint32_t>(query.maximum); |
| 194 | result->default_value = static_cast<uint32_t>(query.default_value); |
| 195 | } else { |
| 196 | result->maximum = query.maximum; |
| 197 | result->default_value = query.default_value; |
| 198 | } |
| 199 | result->step = static_cast<uint32_t>(query.step); |
| 200 | result->flags = query.flags; |
| 201 | result->elems = 1; |
| 202 | switch (result->type) { |
| 203 | case V4L2_CTRL_TYPE_INTEGER64: |
| 204 | result->elem_size = sizeof(int64_t); |
| 205 | break; |
| 206 | case V4L2_CTRL_TYPE_STRING: |
| 207 | result->elem_size = result->maximum + 1; |
| 208 | break; |
| 209 | default: |
| 210 | result->elem_size = sizeof(int32_t); |
| 211 | break; |
| 212 | } |
| 213 | |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | int V4L2Wrapper::GetControl(uint32_t control_id, int32_t* value) { |
| 218 | HAL_LOG_ENTER(); |
| 219 | |
| 220 | v4l2_control control; |
| 221 | control.id = control_id; |
| 222 | if (IoctlLocked(VIDIOC_G_CTRL, &control) < 0) { |
| 223 | HAL_LOGE("G_CTRL fails: %s", strerror(errno)); |
| 224 | return -ENODEV; |
| 225 | } |
| 226 | *value = control.value; |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | int V4L2Wrapper::SetControl(uint32_t control_id, int32_t desired, |
| 231 | int32_t* result) { |
| 232 | HAL_LOG_ENTER(); |
| 233 | |
| 234 | v4l2_control control{control_id, desired}; |
| 235 | if (IoctlLocked(VIDIOC_S_CTRL, &control) < 0) { |
| 236 | HAL_LOGE("S_CTRL fails: %s", strerror(errno)); |
| 237 | return -ENODEV; |
| 238 | } |
| 239 | *result = control.value; |
| 240 | return 0; |
| 241 | } |
| 242 | |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 243 | int V4L2Wrapper::SetFormat(const default_camera_hal::Stream& stream, |
| 244 | uint32_t* result_max_buffers) { |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 245 | HAL_LOG_ENTER(); |
| 246 | |
| 247 | // Should be checked earlier; sanity check. |
| 248 | if (stream.isInputType()) { |
| 249 | HAL_LOGE("Input streams not supported."); |
| 250 | return -EINVAL; |
| 251 | } |
| 252 | |
| 253 | StreamFormat desired_format(stream); |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 254 | if (format_ && desired_format == *format_) { |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 255 | HAL_LOGV("Already in correct format, skipping format setting."); |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | // Not in the correct format, set our format. |
| 260 | v4l2_format new_format; |
| 261 | desired_format.FillFormatRequest(&new_format); |
| 262 | // TODO(b/29334616): When async, this will need to check if the stream |
| 263 | // is on, and if so, lock it off while setting format. |
| 264 | if (IoctlLocked(VIDIOC_S_FMT, &new_format) < 0) { |
| 265 | HAL_LOGE("S_FMT failed: %s", strerror(errno)); |
| 266 | return -ENODEV; |
| 267 | } |
| 268 | |
| 269 | // Check that the driver actually set to the requested values. |
| 270 | if (desired_format != new_format) { |
| 271 | HAL_LOGE("Device doesn't support desired stream configuration."); |
| 272 | return -EINVAL; |
| 273 | } |
| 274 | |
| 275 | // Keep track of our new format. |
| 276 | format_.reset(new StreamFormat(new_format)); |
| 277 | |
| 278 | // Format changed, setup new buffers. |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 279 | int res = SetupBuffers(); |
| 280 | if (res) { |
| 281 | HAL_LOGE("Failed to set up buffers for new format."); |
| 282 | return res; |
| 283 | } |
| 284 | *result_max_buffers = max_buffers_; |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 285 | return 0; |
| 286 | } |
| 287 | |
| 288 | int V4L2Wrapper::SetupBuffers() { |
| 289 | HAL_LOG_ENTER(); |
| 290 | |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 291 | if (!format_) { |
| 292 | HAL_LOGE("Stream format must be set before setting up buffers."); |
| 293 | return -ENODEV; |
| 294 | } |
| 295 | |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 296 | // "Request" a buffer (since we're using a userspace buffer, this just |
| 297 | // tells V4L2 to switch into userspace buffer mode). |
| 298 | v4l2_requestbuffers req_buffers; |
| 299 | memset(&req_buffers, 0, sizeof(req_buffers)); |
Ari Hausman-Cohen | 4ab4962 | 2016-07-21 14:33:54 -0700 | [diff] [blame] | 300 | req_buffers.type = format_->type(); |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 301 | req_buffers.memory = V4L2_MEMORY_USERPTR; |
| 302 | req_buffers.count = 1; |
Ari Hausman-Cohen | 4ab4962 | 2016-07-21 14:33:54 -0700 | [diff] [blame] | 303 | |
| 304 | int res = IoctlLocked(VIDIOC_REQBUFS, &req_buffers); |
| 305 | // Calling REQBUFS releases all queued buffers back to the user. |
| 306 | int gralloc_res = gralloc_->unlockAllBuffers(); |
| 307 | if (res < 0) { |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 308 | HAL_LOGE("REQBUFS failed: %s", strerror(errno)); |
| 309 | return -ENODEV; |
| 310 | } |
Ari Hausman-Cohen | 4ab4962 | 2016-07-21 14:33:54 -0700 | [diff] [blame] | 311 | if (gralloc_res < 0) { |
| 312 | HAL_LOGE("Failed to unlock all buffers when setting up new buffers."); |
| 313 | return gralloc_res; |
| 314 | } |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 315 | |
| 316 | // V4L2 will set req_buffers.count to a number of buffers it can handle. |
| 317 | max_buffers_ = req_buffers.count; |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 318 | // Sanity check. |
| 319 | if (max_buffers_ < 1) { |
| 320 | HAL_LOGE("REQBUFS claims it can't handle any buffers."); |
| 321 | return -ENODEV; |
| 322 | } |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 323 | return 0; |
| 324 | } |
| 325 | |
Ari Hausman-Cohen | 4ab4962 | 2016-07-21 14:33:54 -0700 | [diff] [blame] | 326 | int V4L2Wrapper::EnqueueBuffer(const camera3_stream_buffer_t* camera_buffer) { |
| 327 | HAL_LOG_ENTER(); |
| 328 | |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 329 | if (!format_) { |
| 330 | HAL_LOGE("Stream format must be set before enqueuing buffers."); |
| 331 | return -ENODEV; |
| 332 | } |
| 333 | |
Ari Hausman-Cohen | 4ab4962 | 2016-07-21 14:33:54 -0700 | [diff] [blame] | 334 | // Set up a v4l2 buffer struct. |
| 335 | v4l2_buffer device_buffer; |
| 336 | memset(&device_buffer, 0, sizeof(device_buffer)); |
| 337 | device_buffer.type = format_->type(); |
| 338 | |
| 339 | // Use QUERYBUF to ensure our buffer/device is in good shape. |
| 340 | if (IoctlLocked(VIDIOC_QUERYBUF, &device_buffer) < 0) { |
| 341 | HAL_LOGE("QUERYBUF fails: %s", strerror(errno)); |
| 342 | return -ENODEV; |
| 343 | } |
| 344 | |
| 345 | // Configure the device buffer based on the stream buffer. |
| 346 | device_buffer.memory = V4L2_MEMORY_USERPTR; |
| 347 | // TODO(b/29334616): when this is async, actually limit the number |
| 348 | // of buffers used to the known max, and set this according to the |
| 349 | // queue length. |
| 350 | device_buffer.index = 0; |
| 351 | // Lock the buffer for writing. |
| 352 | int res = |
| 353 | gralloc_->lock(camera_buffer, format_->bytes_per_line(), &device_buffer); |
| 354 | if (res) { |
| 355 | HAL_LOGE("Gralloc failed to lock buffer."); |
| 356 | return res; |
| 357 | } |
| 358 | if (IoctlLocked(VIDIOC_QBUF, &device_buffer) < 0) { |
| 359 | HAL_LOGE("QBUF (%d) fails: %s", 0, strerror(errno)); |
| 360 | gralloc_->unlock(&device_buffer); |
| 361 | return -ENODEV; |
| 362 | } |
| 363 | |
| 364 | return 0; |
| 365 | } |
| 366 | |
| 367 | int V4L2Wrapper::DequeueBuffer(v4l2_buffer* buffer) { |
| 368 | HAL_LOG_ENTER(); |
| 369 | |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 370 | if (!format_) { |
| 371 | HAL_LOGE("Stream format must be set before dequeueing buffers."); |
| 372 | return -ENODEV; |
| 373 | } |
| 374 | |
Ari Hausman-Cohen | 4ab4962 | 2016-07-21 14:33:54 -0700 | [diff] [blame] | 375 | memset(buffer, 0, sizeof(*buffer)); |
| 376 | buffer->type = format_->type(); |
| 377 | buffer->memory = V4L2_MEMORY_USERPTR; |
| 378 | if (IoctlLocked(VIDIOC_DQBUF, buffer) < 0) { |
| 379 | HAL_LOGE("DQBUF fails: %s", strerror(errno)); |
| 380 | return -ENODEV; |
| 381 | } |
| 382 | |
| 383 | // Now that we're done painting the buffer, we can unlock it. |
| 384 | int res = gralloc_->unlock(buffer); |
| 385 | if (res) { |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 386 | HAL_LOGE("Gralloc failed to unlock buffer after dequeueing."); |
Ari Hausman-Cohen | 4ab4962 | 2016-07-21 14:33:54 -0700 | [diff] [blame] | 387 | return res; |
| 388 | } |
| 389 | |
| 390 | return 0; |
| 391 | } |
| 392 | |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 393 | } // namespace v4l2_camera_hal |