blob: 1a5eb3a1f42147fcc14e93321bd21cdc453fd761 [file] [log] [blame]
Zhijun He046205c2015-01-08 17:40:00 -08001/*
2 * Copyright (C) 2015 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//#define LOG_NDEBUG 0
18#define LOG_TAG "UsbCameraHAL"
Zhijun He046205c2015-01-08 17:40:00 -080019
20#include <cstdlib>
Mark Salyzynd88dfe82017-04-11 08:56:09 -070021
22#include <log/log.h>
23#include <utils/Mutex.h>
24
Zhijun He046205c2015-01-08 17:40:00 -080025#include <hardware/camera_common.h>
26#include <hardware/hardware.h>
Mark Salyzynd88dfe82017-04-11 08:56:09 -070027
Zhijun He046205c2015-01-08 17:40:00 -080028#include "CameraHAL.h"
Mark Salyzynd88dfe82017-04-11 08:56:09 -070029#include "UsbCamera.h"
Zhijun He046205c2015-01-08 17:40:00 -080030
31/*
32 * This file serves as the entry point to the HAL. It contains the module
33 * structure and functions used by the framework to load and interface to this
34 * HAL, as well as the handles to the individual camera devices.
35 */
36
37namespace usb_camera_hal {
38
39static CameraHAL gCameraHAL;
40
41CameraHAL::CameraHAL()
42 : mCallbacks(NULL) {
43 // Should not allocate the camera devices for now, as it is unclear if the device is plugged.
44
45 // Start hotplug thread
46 mHotplugThread = new HotplugThread(this);
47 mHotplugThread->run("usb-camera-hotplug");
48}
49
50CameraHAL::~CameraHAL() {
51 // Stop hotplug thread
52 {
53 android::Mutex::Autolock al(mModuleLock);
54 if (mHotplugThread != NULL) {
55 mHotplugThread->requestExit();
56 }
57
58 // Delete camera device from mCameras
59 }
60
61 // Joining done without holding mLock, otherwise deadlocks may ensue
62 // as the threads try to access parent states.
63 if (mHotplugThread != NULL) {
64 mHotplugThread->join();
65 }
66
67 delete mHotplugThread;
68}
69
70int CameraHAL::getNumberOfCameras() {
71 android::Mutex::Autolock al(mModuleLock);
Greg Kaiser7a915ef2016-06-30 16:59:38 -070072 ALOGV("%s: %zu", __func__, mCameras.size());
Zhijun He046205c2015-01-08 17:40:00 -080073 return static_cast<int>(mCameras.size());
74}
75
76int CameraHAL::getCameraInfo(int id, struct camera_info* info) {
77 android::Mutex::Autolock al(mModuleLock);
78 ALOGV("%s: camera id %d: info=%p", __func__, id, info);
79 if (id < 0 || id >= static_cast<int>(mCameras.size())) {
80 ALOGE("%s: Invalid camera id %d", __func__, id);
81 return -ENODEV;
82 }
83
84 return mCameras[id]->getInfo(info);
85}
86
87int CameraHAL::setCallbacks(const camera_module_callbacks_t *callbacks) {
88 ALOGV("%s : callbacks=%p", __func__, callbacks);
89 mCallbacks = callbacks;
90 return 0;
91}
92
93int CameraHAL::open(const hw_module_t* mod, const char* name, hw_device_t** dev) {
94 int id;
95 char *nameEnd;
96
97 android::Mutex::Autolock al(mModuleLock);
98 ALOGV("%s: module=%p, name=%s, device=%p", __func__, mod, name, dev);
99 if (*name == '\0') {
100 ALOGE("%s: Invalid camera id name is NULL", __func__);
101 return -EINVAL;
102 }
103 id = strtol(name, &nameEnd, 10);
104 if (*nameEnd != '\0') {
105 ALOGE("%s: Invalid camera id name %s", __func__, name);
106 return -EINVAL;
107 } else if (id < 0 || id >= static_cast<int>(mCameras.size())) {
108 ALOGE("%s: Invalid camera id %d", __func__, id);
109 return -ENODEV;
110 }
111 return mCameras[id]->open(mod, dev);
112}
113
114extern "C" {
115
116static int get_number_of_cameras() {
117 return gCameraHAL.getNumberOfCameras();
118}
119
120static int get_camera_info(int id, struct camera_info* info) {
121 return gCameraHAL.getCameraInfo(id, info);
122}
123
124static int set_callbacks(const camera_module_callbacks_t *callbacks) {
125 return gCameraHAL.setCallbacks(callbacks);
126}
127
128static int open_dev(const hw_module_t* mod, const char* name, hw_device_t** dev) {
129 return gCameraHAL.open(mod, name, dev);
130}
131
132static hw_module_methods_t gCameraModuleMethods = {
Greg Kaiser7a915ef2016-06-30 16:59:38 -0700133 .open = open_dev
Zhijun He046205c2015-01-08 17:40:00 -0800134};
135
136camera_module_t HAL_MODULE_INFO_SYM __attribute__ ((visibility("default"))) = {
Greg Kaiser7a915ef2016-06-30 16:59:38 -0700137 .common = {
138 .tag = HARDWARE_MODULE_TAG,
139 .module_api_version = CAMERA_MODULE_API_VERSION_2_4,
140 .hal_api_version = HARDWARE_HAL_API_VERSION,
141 .id = CAMERA_HARDWARE_MODULE_ID,
142 .name = "Default USB Camera HAL",
143 .author = "The Android Open Source Project",
144 .methods = &gCameraModuleMethods,
145 .dso = NULL,
146 .reserved = {0},
Zhijun He046205c2015-01-08 17:40:00 -0800147 },
Greg Kaiser7a915ef2016-06-30 16:59:38 -0700148 .get_number_of_cameras = get_number_of_cameras,
149 .get_camera_info = get_camera_info,
150 .set_callbacks = set_callbacks,
151 .get_vendor_tag_ops = NULL,
152 .open_legacy = NULL,
153 .set_torch_mode = NULL,
154 .init = NULL,
155 .reserved = {0},
Zhijun He046205c2015-01-08 17:40:00 -0800156};
157} // extern "C"
158
159} // namespace usb_camera_hal