1 /* 2 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. 3 * 4 * HDF is dual licensed: you can use it either under the terms of 5 * the GPL, or the BSD license, at your option. 6 * See the LICENSE file in the root of this repository for complete details. 7 */ 8 9 #include "hdf_driver_loader.h" 10 #include "hdf_driver.h" 11 #include "hdf_log.h" 12 #include "hdf_object_manager.h" 13 14 #define HDF_LOG_TAG driver_loader 15 HdfDriverEntryConstruct(void)16int32_t HdfDriverEntryConstruct(void) 17 { 18 int i; 19 struct HdfDriverEntry *driverEntry = NULL; 20 size_t *addrBegin = NULL; 21 int32_t count = (int32_t)(((uint8_t *)(HDF_DRIVER_END()) - (uint8_t *)(HDF_DRIVER_BEGIN())) / sizeof(size_t)); 22 if (count <= 0) { 23 HDF_LOGE("%s: no hdf driver exist", __func__); 24 return HDF_FAILURE; 25 } 26 27 addrBegin = (size_t *)(HDF_DRIVER_BEGIN()); 28 for (i = 0; i < count; i++) { 29 driverEntry = (struct HdfDriverEntry *)(*addrBegin); 30 if (HdfRegisterDriverEntry(driverEntry) != HDF_SUCCESS) { 31 HDF_LOGE("failed to register driver %s, skip and try another", driverEntry ? driverEntry->moduleName : ""); 32 continue; 33 } 34 addrBegin++; 35 } 36 return HDF_SUCCESS; 37 } 38 HdfDriverLoaderGetDriver(const char * moduleName)39struct HdfDriver *HdfDriverLoaderGetDriver(const char *moduleName) 40 { 41 if (moduleName == NULL) { 42 HDF_LOGE("%s: failed to get device entry, moduleName is NULL", __func__); 43 return NULL; 44 } 45 46 return HdfDriverManagerGetDriver(moduleName); 47 } 48 HdfDriverLoaderReclaimDriver(struct HdfDriver * driver)49void HdfDriverLoaderReclaimDriver(struct HdfDriver *driver) 50 { 51 // kernel driver do not need release 52 (void)driver; 53 } 54 HdfDriverLoaderConstruct(struct HdfDriverLoader * inst)55void HdfDriverLoaderConstruct(struct HdfDriverLoader *inst) 56 { 57 if (inst != NULL) { 58 inst->super.GetDriver = HdfDriverLoaderGetDriver; 59 inst->super.ReclaimDriver = HdfDriverLoaderReclaimDriver; 60 } 61 } 62 HdfDriverLoaderCreate(void)63struct HdfObject *HdfDriverLoaderCreate(void) 64 { 65 static bool isDriverLoaderInit = false; 66 static struct HdfDriverLoader driverLoader; 67 if (!isDriverLoaderInit) { 68 if (HdfDriverEntryConstruct() != HDF_SUCCESS) { 69 return NULL; 70 } 71 HdfDriverLoaderConstruct(&driverLoader); 72 isDriverLoaderInit = true; 73 } 74 return (struct HdfObject *)&driverLoader; 75 } 76 HdfDriverLoaderGetInstance(void)77struct IDriverLoader *HdfDriverLoaderGetInstance(void) 78 { 79 static struct IDriverLoader *instance = NULL; 80 if (instance == NULL) { 81 instance = (struct IDriverLoader *)HdfObjectManagerGetObject(HDF_OBJECT_ID_DRIVER_LOADER); 82 } 83 return instance; 84 } 85