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)16 int32_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("%{public}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 %{public}s, skip and try another",
32                 driverEntry ? driverEntry->moduleName : "");
33             continue;
34         }
35         addrBegin++;
36     }
37     return HDF_SUCCESS;
38 }
39 
HdfDriverLoaderGetDriver(const char * moduleName)40 struct HdfDriver *HdfDriverLoaderGetDriver(const char *moduleName)
41 {
42     if (moduleName == NULL) {
43         HDF_LOGE("%{public}s: failed to get device entry, moduleName is NULL", __func__);
44         return NULL;
45     }
46 
47     return HdfDriverManagerGetDriver(moduleName);
48 }
49 
HdfDriverLoaderReclaimDriver(struct HdfDriver * driver)50 void HdfDriverLoaderReclaimDriver(struct HdfDriver *driver)
51 {
52     // kernel driver do not need release
53     (void)driver;
54 }
55 
HdfDriverLoaderConstruct(struct HdfDriverLoader * inst)56 void HdfDriverLoaderConstruct(struct HdfDriverLoader *inst)
57 {
58     if (inst != NULL) {
59         inst->super.GetDriver = HdfDriverLoaderGetDriver;
60         inst->super.ReclaimDriver = HdfDriverLoaderReclaimDriver;
61     }
62 }
63 
HdfDriverLoaderCreate(void)64 struct HdfObject *HdfDriverLoaderCreate(void)
65 {
66     static bool isDriverLoaderInit = false;
67     static struct HdfDriverLoader driverLoader;
68     if (!isDriverLoaderInit) {
69         if (HdfDriverEntryConstruct() != HDF_SUCCESS) {
70             return NULL;
71         }
72         HdfDriverLoaderConstruct(&driverLoader);
73         isDriverLoaderInit = true;
74     }
75     return (struct HdfObject *)&driverLoader;
76 }
77 
HdfDriverLoaderGetInstance(void)78 struct IDriverLoader *HdfDriverLoaderGetInstance(void)
79 {
80     static struct IDriverLoader *instance = NULL;
81     if (instance == NULL) {
82         instance = (struct IDriverLoader *)HdfObjectManagerGetObject(HDF_OBJECT_ID_DRIVER_LOADER);
83     }
84     return instance;
85 }
86