1 /*
2  * Copyright (c) 2020-2022 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_wlan_chipdriver_manager.h"
10 #include "gpio_if.h"
11 #include "osal_mem.h"
12 #include "hdf_log.h"
13 #include "wifi_module.h"
14 #include "securec.h"
15 
16 #define HDF_LOG_TAG HDF_WIFI_CORE
17 
18 static struct HdfChipDriverFactory *g_wlanChipDriverManagerFactory[MAX_CHIPDRIVER_COUNT] = {NULL};
19 
20 /* chip driver manager method get chip driver factory by chip name */
HdfWlanGetChipDriverByName(const char * driverName)21 static struct HdfChipDriverFactory *HdfWlanGetChipDriverByName(const char *driverName)
22 {
23     int32_t i;
24     if (driverName == NULL) {
25         HDF_LOGE("%s fail : driverName is NULL", __func__);
26         return NULL;
27     }
28 
29     for (i = 0; i < MAX_CHIPDRIVER_COUNT; i++) {
30         if (g_wlanChipDriverManagerFactory[i] != NULL && g_wlanChipDriverManagerFactory[i]->driverName != NULL) {
31             struct HdfChipDriverFactory *factory = g_wlanChipDriverManagerFactory[i];
32             if (strcmp(factory->driverName, driverName) == 0) {
33                 return factory;
34             }
35         }
36     }
37     return NULL;
38 }
39 
40 /* chip driver manager register */
HdfWlanRegChipDriver(struct HdfChipDriverFactory * obj)41 static int32_t HdfWlanRegChipDriver(struct HdfChipDriverFactory *obj)
42 {
43     int32_t index;
44     if (obj == NULL || obj->driverName == NULL) {
45         HDF_LOGE("%s: HdfChipDriverFactory obj is NULL", __func__);
46         return HDF_ERR_INVALID_PARAM;
47     }
48     if (HdfWlanGetChipDriverByName(obj->driverName) != NULL) {
49         HDF_LOGI("%s: Chipdriver factory is already registered.name=%s", __func__, obj->driverName);
50         return HDF_SUCCESS;
51     }
52     for (index = 0; index < MAX_CHIPDRIVER_COUNT; index++) {
53         if (g_wlanChipDriverManagerFactory[index] == NULL) {
54             g_wlanChipDriverManagerFactory[index] = obj;
55             HDF_LOGI("%s: Chip driver %s registered.", __func__, obj->driverName);
56             return HDF_SUCCESS;
57         }
58     }
59     HDF_LOGE("%s: Factory table is full", __func__);
60     return HDF_FAILURE;
61 }
62 
63 /* to release the chipdriver manager factory */
ChipDriverMgrDeInit(void)64 int32_t ChipDriverMgrDeInit(void)
65 {
66     int cnt;
67     for (cnt = 0; cnt < MAX_CHIPDRIVER_COUNT; cnt++) {
68         if (g_wlanChipDriverManagerFactory[cnt] != NULL) {
69             if (g_wlanChipDriverManagerFactory[cnt]->ReleaseFactory != NULL) {
70                 g_wlanChipDriverManagerFactory[cnt]->ReleaseFactory(g_wlanChipDriverManagerFactory[cnt]);
71             }
72             g_wlanChipDriverManagerFactory[cnt] = NULL;
73         }
74     }
75 
76     return HDF_SUCCESS;
77 }
78 
79 static struct HdfChipDriverManager g_chipDriverManager = {
80     .chipFactoryInsts = g_wlanChipDriverManagerFactory,
81     .RegChipDriver = HdfWlanRegChipDriver,
82     .GetChipDriverByName = HdfWlanGetChipDriverByName,
83 };
HdfWlanGetChipDriverMgr(void)84 struct HdfChipDriverManager *HdfWlanGetChipDriverMgr(void)
85 {
86     return &g_chipDriverManager;
87 }