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