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_dlist.h"
10 #include "hdf_driver.h"
11 #include "hdf_log.h"
12 #include "osal_mem.h"
13 #include "osal_sysevent.h"
14
HdfDriverHead(void)15 static struct DListHead *HdfDriverHead(void)
16 {
17 static struct DListHead driverHead = {0};
18 if (driverHead.next == NULL) {
19 DListHeadInit(&driverHead);
20 }
21
22 return &driverHead;
23 }
24
HdfRegisterDriverEntry(const struct HdfDriverEntry * entry)25 int32_t HdfRegisterDriverEntry(const struct HdfDriverEntry *entry)
26 {
27 struct HdfDriver *newDriver = NULL;
28
29 if (entry == NULL || entry->moduleName == NULL) {
30 return HDF_ERR_INVALID_OBJECT;
31 }
32
33 newDriver = OsalMemCalloc(sizeof(struct HdfDriver));
34 if (newDriver == NULL) {
35 return HDF_DEV_ERR_NO_MEMORY;
36 }
37
38 newDriver->entry = entry;
39
40 DListInsertTail(&newDriver->node, HdfDriverHead());
41
42 return HDF_SUCCESS;
43 }
44
HdfUnregisterDriverEntry(const struct HdfDriverEntry * entry)45 int32_t HdfUnregisterDriverEntry(const struct HdfDriverEntry *entry)
46 {
47 struct DListHead *driverHead = NULL;
48 struct HdfDriver *driver = NULL;
49 struct HdfDriver *tmp = NULL;
50
51 if (entry == NULL || entry->moduleName == NULL) {
52 return HDF_ERR_INVALID_OBJECT;
53 }
54
55 driverHead = HdfDriverHead();
56 DLIST_FOR_EACH_ENTRY_SAFE(driver, tmp, driverHead, struct HdfDriver, node) {
57 if (driver->entry == entry) {
58 DListRemove(&driver->node);
59 OsalMemFree(driver);
60 break;
61 }
62 }
63
64 return HDF_SUCCESS;
65 }
66
HdfRegisterDriver(struct HdfDriver * driver)67 int32_t HdfRegisterDriver(struct HdfDriver *driver)
68 {
69 if (driver == NULL || driver->entry == NULL || driver->entry->moduleName == NULL) {
70 return HDF_ERR_INVALID_OBJECT;
71 }
72
73 DListInsertTail(&driver->node, HdfDriverHead());
74 return HDF_SUCCESS;
75 }
76
HdfUnregisterDriver(struct HdfDriver * driver)77 int32_t HdfUnregisterDriver(struct HdfDriver *driver)
78 {
79 struct DListHead *driverHead = NULL;
80 struct HdfDriver *it = NULL;
81 struct HdfDriver *tmp = NULL;
82
83 if (driver == NULL) {
84 return HDF_ERR_INVALID_PARAM;
85 }
86
87 driverHead = HdfDriverHead();
88 DLIST_FOR_EACH_ENTRY_SAFE(it, tmp, driverHead, struct HdfDriver, node) {
89 if (it == driver) {
90 DListRemove(&it->node);
91 break;
92 }
93 }
94
95 return HDF_SUCCESS;
96 }
97
HdfDriverManagerFoundDriver(const char * driverName)98 static struct HdfDriver *HdfDriverManagerFoundDriver(const char *driverName)
99 {
100 struct DListHead *driverHead = NULL;
101 struct HdfDriver *driver = NULL;
102 driverHead = HdfDriverHead();
103 DLIST_FOR_EACH_ENTRY(driver, driverHead, struct HdfDriver, node) {
104 if (driver->entry != NULL && driver->entry->moduleName != NULL &&
105 !strcmp(driver->entry->moduleName, driverName)) {
106 return driver;
107 }
108 }
109
110 return NULL;
111 }
112
HdfDriverManagerGetDriver(const char * driverName)113 struct HdfDriver *HdfDriverManagerGetDriver(const char *driverName)
114 {
115 struct HdfDriver *driver = NULL;
116 if (driverName == NULL) {
117 return NULL;
118 }
119 driver = HdfDriverManagerFoundDriver(driverName);
120 if (driver != NULL) {
121 return driver;
122 }
123
124 if (HdfSysEventSend != NULL) {
125 HDF_LOGI("%s:try to dynamic load driver %s", __func__, driverName);
126 if (HdfSysEventSend(HDF_SYSEVENT_CLASS_MODULE, KEVENT_MODULE_INSTALL, driverName, true) != HDF_SUCCESS) {
127 return NULL;
128 }
129
130 driver = HdfDriverManagerFoundDriver(driverName);
131 }
132 if (driver == NULL) {
133 HDF_LOGE("%s:driver %s not found", __func__, driverName);
134 }
135 return driver;
136 }
137
HdfDriverManagerGetDriverList(void)138 struct DListHead *HdfDriverManagerGetDriverList(void)
139 {
140 return HdfDriverHead();
141 }
142