1 /*
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"
19 
20 #include <cstdlib>
21 
22 #include <log/log.h>
23 #include <utils/Mutex.h>
24 
25 #include <hardware/camera_common.h>
26 #include <hardware/hardware.h>
27 
28 #include "CameraHAL.h"
29 #include "UsbCamera.h"
30 
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 
37 namespace usb_camera_hal {
38 
39 static CameraHAL gCameraHAL;
40 
CameraHAL()41 CameraHAL::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 
~CameraHAL()50 CameraHAL::~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 
getNumberOfCameras()70 int CameraHAL::getNumberOfCameras() {
71     android::Mutex::Autolock al(mModuleLock);
72     ALOGV("%s: %zu", __func__, mCameras.size());
73     return static_cast<int>(mCameras.size());
74 }
75 
getCameraInfo(int id,struct camera_info * info)76 int 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 
setCallbacks(const camera_module_callbacks_t * callbacks)87 int CameraHAL::setCallbacks(const camera_module_callbacks_t *callbacks) {
88     ALOGV("%s : callbacks=%p", __func__, callbacks);
89     mCallbacks = callbacks;
90     return 0;
91 }
92 
open(const hw_module_t * mod,const char * name,hw_device_t ** dev)93 int 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 
114 extern "C" {
115 
get_number_of_cameras()116 static int get_number_of_cameras() {
117     return gCameraHAL.getNumberOfCameras();
118 }
119 
get_camera_info(int id,struct camera_info * info)120 static int get_camera_info(int id, struct camera_info* info) {
121     return gCameraHAL.getCameraInfo(id, info);
122 }
123 
set_callbacks(const camera_module_callbacks_t * callbacks)124 static int set_callbacks(const camera_module_callbacks_t *callbacks) {
125     return gCameraHAL.setCallbacks(callbacks);
126 }
127 
open_dev(const hw_module_t * mod,const char * name,hw_device_t ** dev)128 static int open_dev(const hw_module_t* mod, const char* name, hw_device_t** dev) {
129     return gCameraHAL.open(mod, name, dev);
130 }
131 
132 static hw_module_methods_t gCameraModuleMethods = {
133     .open = open_dev
134 };
135 
136 camera_module_t HAL_MODULE_INFO_SYM __attribute__ ((visibility("default"))) = {
137     .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},
147     },
148     .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},
156 };
157 } // extern "C"
158 
159 } // namespace usb_camera_hal
160