1 /*
2  * Copyright (c) 2020-2023 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 "securec.h"
10 #include "gpio_if.h"
11 #include "hdf_wifi_core.h"
12 #include "devsvc_manager_clnt.h"
13 #include "hdf_device_desc.h"
14 #include "hdf_log.h"
15 #include "hdf_wifi_cmd.h"
16 #include "osal_mem.h"
17 #include "osal_time.h"
18 #include "osal_thread.h"
19 #include "message/message_router.h"
20 #include "hdf_wlan_chipdriver_manager.h"
21 #include "hdf_wlan_config.h"
22 #include "hdf_wlan_utils.h"
23 
24 #define HDF_LOG_TAG HDF_WIFI_CORE
25 #define SLEEPTIME 50
26 
HdfWifiGetBusIdx(void)27 int32_t HdfWifiGetBusIdx(void)
28 {
29     struct HdfConfigWlanRoot *rootConfig = NULL;
30     rootConfig = HdfWlanGetModuleConfigRoot();
31     if (rootConfig != NULL) {
32         return rootConfig->wlanConfig.deviceList.deviceInst[0].bus.busIdx;
33     }
34     return -1;
35 }
36 
37 /**
38  * @brief  as for now, we just support one wlan module in one board cause driver binds to wlan featere
39  * due to that reason, once we detected one chip, we stop rescan.
40  */
HdfWlanBusInit(struct HdfWlanDevice * data,const struct HdfConfigWlanBus * busConfig)41 static int HdfWlanBusInit(struct HdfWlanDevice *data, const struct HdfConfigWlanBus *busConfig)
42 {
43     int ret = HDF_SUCCESS;
44     struct BusDev *bus = NULL;
45     struct HdfConfigWlanChipList *tmpChipList = NULL;
46     struct HdfConfigWlanRoot *rootConfig = NULL;
47     HDF_LOGI("%s: Enter.", __func__);
48 
49     do {
50         if (busConfig->busEnable == 0) {
51             data->bus = NULL;
52             ret = HDF_SUCCESS;
53             HDF_LOGW("Do not call GPIO and HdfWlanCreateBusManager");
54         } else {
55             bus = HdfWlanCreateBusManager(busConfig);
56             if (bus == NULL) {
57                 HDF_LOGE("%s: Create bus manager failed!", __func__);
58                 ret = HDF_FAILURE;
59                 break;
60             }
61             data->bus = bus;
62             ret = HDF_SUCCESS;
63         }
64 
65         rootConfig = HdfWlanGetModuleConfigRoot();
66         if (rootConfig == NULL) {
67             HDF_LOGE("%s:ProbeDevice rootConfig NULL", __func__);
68             ret = HDF_FAILURE;
69             break;
70         }
71         tmpChipList = &rootConfig->wlanConfig.chipList;
72         data->driverName = tmpChipList->chipInst[0].driverName;
73         HDF_LOGI("%s: Driver name = %s", __func__, data->driverName);
74     } while (false);
75 
76     if (ret != HDF_SUCCESS) {
77         data->bus = NULL;
78     }
79     return ret;
80 }
81 
HdfWifiDriverBind(struct HdfDeviceObject * dev)82 static int32_t HdfWifiDriverBind(struct HdfDeviceObject *dev)
83 {
84     ErrorCode errCode;
85     static struct IDeviceIoService wifiService = {
86         .object.objectId = 1,
87         .Dispatch = DispatchToMessage,
88     };
89 
90     if (dev == NULL) {
91         HDF_LOGE("%s: Input param is null", __func__);
92         return HDF_FAILURE;
93     }
94     HDF_LOGI("%s: Wifi driver binding...!", __func__);
95     errCode = StartMessageRouter(MESSAGE_NODE_CONFIG_DEFAULT);
96     if (errCode != ME_SUCCESS) {
97         HDF_LOGE("%s:Start message router failed!", __func__);
98         return HDF_FAILURE;
99     }
100 
101     errCode = EnableDefaultDispatcher();
102     if (errCode != ME_SUCCESS) {
103         (void)ShutdownMessageRouter();
104         HDF_LOGE("%s:EnableDefaultDispatcher failed!", __func__);
105         return errCode;
106     }
107     dev->service = &wifiService;
108     HDF_LOGI("%s: Wifi driver bind successed!", __func__);
109     return HDF_SUCCESS;
110 }
111 
HdfWlanGetDeviceList(void)112 static struct HdfConfigWlanDeviceList *HdfWlanGetDeviceList(void)
113 {
114     struct HdfConfigWlanRoot *rootConfig = HdfWlanGetModuleConfigRoot();
115     if (rootConfig == NULL) {
116         HDF_LOGE("%s: HdfWlanGetModuleConfigRoot get failed", __func__);
117         return NULL;
118     }
119     HDF_LOGD("%s: HdfWlanGetDeviceList finished!", __func__);
120     return &rootConfig->wlanConfig.deviceList;
121 }
122 
123 /* get wlan related config */
HdfWlanGetConfig(const struct HdfDeviceObject * device)124 static int32_t HdfWlanGetConfig(const struct HdfDeviceObject *device)
125 {
126     if (device == NULL) {
127         HDF_LOGE("%s: Input pointer is null", __func__);
128         return HDF_FAILURE;
129     }
130     if (HdfParseWlanConfig(device->property) != HDF_SUCCESS) {
131         HDF_LOGE("%s:ParseWifiConfig failed!", __func__);
132         return HDF_FAILURE;
133     }
134     HDF_LOGI("%s: HdfWlanGetConfig successful!", __func__);
135     return HDF_SUCCESS;
136 }
137 
HdfWlanPowerOnProcess(struct PowerManager * powerMgr)138 static int32_t HdfWlanPowerOnProcess(struct PowerManager *powerMgr)
139 {
140     if (powerMgr == NULL) {
141         HDF_LOGE("%s:Input is NULL!", __func__);
142         return HDF_FAILURE;
143     }
144 
145     if (powerMgr->On(powerMgr) != HDF_SUCCESS) {
146         HDF_LOGE("%s:power on failed!", __func__);
147         return HDF_FAILURE;
148     }
149     HDF_LOGI("%s: HdfWlanPowerOnProcess successful!", __func__);
150     return HDF_SUCCESS;
151 }
HdfWlanResetProcess(struct ResetManager * resetMgr)152 static int32_t HdfWlanResetProcess(struct ResetManager *resetMgr)
153 {
154     if (resetMgr == NULL) {
155         HDF_LOGE("%s:input is NULL!", __func__);
156         return HDF_FAILURE;
157     }
158     if (resetMgr->Reset(resetMgr) != HDF_SUCCESS) {
159         HDF_LOGE("%s:reset failed!", __func__);
160         return HDF_FAILURE;
161     }
162     HDF_LOGI("%s: HdfWlanResetProcess success!", __func__);
163     return HDF_SUCCESS;
164 }
165 
HdfWlanGetDriverFactory(const char * driverName)166 static struct HdfChipDriverFactory *HdfWlanGetDriverFactory(const char *driverName)
167 {
168     struct HdfChipDriverManager *initMgr = NULL;
169     if (driverName == NULL) {
170         HDF_LOGE("%s: chipName is NULL", __func__);
171         return NULL;
172     }
173     initMgr = HdfWlanGetChipDriverMgr();
174     if (initMgr == NULL) {
175         HDF_LOGE("%s: initMgr is NULL", __func__);
176         return NULL;
177     }
178     HDF_LOGD("%s: HdfWlanGetDriverFactory finished!", __func__);
179     return initMgr->GetChipDriverByName(driverName);
180 }
181 
HdfWlanDeinitInterface(struct HdfWlanDevice * device,const char * ifName,struct HdfChipDriverFactory * factory)182 static int32_t HdfWlanDeinitInterface(struct HdfWlanDevice *device, const char *ifName,
183     struct HdfChipDriverFactory *factory)
184 {
185     int32_t ret;
186     struct NetDevice *netdev = NULL;
187     struct HdfChipDriver *chipDriver = NULL;
188 
189     netdev = NetDeviceGetInstByName(ifName);
190     if (netdev == NULL) {
191         HDF_LOGW("%s:netif already released!ifName=%s", __func__, ifName);
192         return HDF_SUCCESS;
193     }
194     chipDriver = GetChipDriver(netdev);
195     if (chipDriver == NULL) {
196         HDF_LOGE("%s:bad netdev!ChipDriver is NULL. ifName=%s", __func__, ifName);
197         return HDF_FAILURE;
198     }
199     ret = chipDriver->deinit(chipDriver, netdev);
200     if (ret != HDF_SUCCESS) {
201         HDF_LOGE("%s:chip driver deinit failed!ifName=%s", __func__, ifName);
202         return ret;
203     }
204     // check if chip driver released NetDevice
205     if (NetDeviceGetInstByName(ifName) != NULL) {
206         ret = ReleasePlatformNetDevice(netdev);
207         if (ret != HDF_SUCCESS) {
208             HDF_LOGE("%s:release netdevice failed!ifName=%s", __func__, ifName);
209             return ret;
210         }
211     } else {
212         HDF_LOGW("%s:chip driver release platform netdevice detected!ifName=%s", __func__, ifName);
213     }
214 
215     if (factory->Release != NULL) {
216         factory->Release(chipDriver);
217     }
218     chipDriver = NULL;
219     HDF_LOGI("%s: HdfWlanDeinitInterface successful", __func__);
220     return HDF_SUCCESS;
221 }
222 
HdfWlanInitInterface(struct HdfWlanDevice * device,struct HdfChipDriverFactory * factory,uint8_t index)223 static int32_t HdfWlanInitInterface(struct HdfWlanDevice *device, struct HdfChipDriverFactory *factory, uint8_t index)
224 {
225     int32_t ret = HDF_SUCCESS;
226     NetDevice *netDev = NULL;
227     struct HdfChipDriver *chipDriver = NULL;
228     do {
229         HDF_LOGI("%s: Wlan init interface starting...!", __func__);
230         struct HdfWifiNetDeviceData *data = NULL;
231         chipDriver = factory->Build(device, index);
232         if (chipDriver == NULL) {
233             HDF_LOGE("%s:chip driver %s build fail!", __func__, factory->driverName);
234             ret = HDF_FAILURE;
235             break;
236         }
237 
238         if (chipDriver->init == NULL) {
239             HDF_LOGI("%s: chip driver %s not 'init' api", __func__, factory->driverName);
240             ret = HDF_DEV_ERR_OP;
241             break;
242         }
243 
244         netDev = AllocPlatformNetDevice(device);
245         if (netDev == NULL) {
246             HDF_LOGE("%s:allocate net device failed!", __func__);
247             ret = HDF_FAILURE;
248             break;
249         }
250         data = GetPlatformData(netDev);
251         if (data == NULL) {
252             HDF_LOGE("%s:platform data is NULL!", __func__);
253             ret = HDF_FAILURE;
254             break;
255         }
256         data->device = device;
257 
258         ret = chipDriver->init(chipDriver, netDev);
259         if (ret != HDF_SUCCESS) {
260             HDF_LOGE("%s:init chip %s failed! ret=%d", __func__, factory->driverName, ret);
261             break;
262         }
263 
264         data->chipDriver = chipDriver;
265         chipDriver = NULL;
266     } while (false);
267 
268     if (ret != HDF_SUCCESS) {
269         if (chipDriver != NULL && factory->Release != NULL) {
270             factory->Release(chipDriver);
271         }
272         if (netDev != NULL) {
273             ReleasePlatformNetDevice(netDev);
274         }
275     }
276     return ret;
277 }
278 
HdfWlanDeinitInterfaces(struct HdfWlanDevice * device,struct HdfChipDriverFactory * chipDriverFact)279 static int32_t HdfWlanDeinitInterfaces(struct HdfWlanDevice *device, struct HdfChipDriverFactory *chipDriverFact)
280 {
281     int32_t ret = HDF_SUCCESS;
282     char *ifNames = NULL;
283     uint8_t ifNameCount = 0;
284     uint8_t i;
285 
286     ifNames = HdfWlanGetIfNames(device->id, &ifNameCount);
287     if (ifNames == NULL) {
288         HDF_LOGE("%s: HdfWlanGetIfNames failed!", __func__);
289         return HDF_FAILURE;
290     }
291 
292     for (i = 0; i < ifNameCount; i++) {
293         ret = HdfWlanDeinitInterface(device, ifNames + (i * IFNAMSIZ), chipDriverFact);
294         if (ret != HDF_SUCCESS) {
295             HDF_LOGE("%s:Deinit netif %s failed!ret=%d", __func__, ifNames + (i * IFNAMSIZ), ret);
296             break;
297         }
298     }
299     OsalMemFree(ifNames);
300     ifNames = NULL;
301     HDF_LOGI("%s: Wlan deinit interfaces finished!", __func__);
302     return ret;
303 }
304 
305 
306 /* build chip driver according to the detected chip and the load factory */
HdfWlanInitInterfaces(struct HdfWlanDevice * device,struct HdfChipDriverFactory * chipDriverFact)307 static int32_t HdfWlanInitInterfaces(struct HdfWlanDevice *device, struct HdfChipDriverFactory *chipDriverFact)
308 {
309     int32_t ret = HDF_SUCCESS;
310     uint8_t i;
311     uint8_t maxIFCount = chipDriverFact->GetMaxIFCount(chipDriverFact);
312     for (i = 0; i < maxIFCount; i++) {
313         ret = HdfWlanInitInterface(device, chipDriverFact, i);
314         if (ret != HDF_SUCCESS) {
315             HDF_LOGE("%s:Init NetInterface failed!driverName=%s,portIndex=%hhu", __func__, device->driverName, i);
316         }
317     }
318     HDF_LOGI("%s: %s init interfaces finished!", __func__, chipDriverFact->driverName);
319     return ret;
320 }
321 
ReleaseWlanDevice(struct HdfWlanDevice * device)322 static void ReleaseWlanDevice(struct HdfWlanDevice *device)
323 {
324     if (device->powers != NULL && device->powers->Release != NULL) {
325         device->powers->Release(device->powers);
326         device->powers = NULL;
327     }
328     if (device->reset != NULL && device->reset->Release != NULL) {
329         device->reset->Release(device->reset);
330         device->reset = NULL;
331     }
332     if (device->bus != NULL && device->bus->ops.deInit != NULL) {
333         device->bus->ops.deInit(device->bus);
334         device->bus = NULL;
335     }
336     OsalMemFree(device);
337 }
338 
ProbeDevice(struct HdfConfigWlanDevInst * deviceConfig)339 static struct HdfWlanDevice *ProbeDevice(struct HdfConfigWlanDevInst *deviceConfig)
340 {
341     if (deviceConfig == NULL) {
342         HDF_LOGE("%s: Input param is null!", __func__);
343         return NULL;
344     }
345     struct HdfWlanDevice *device = NULL;
346     int32_t ret = HDF_SUCCESS;
347 
348     device = (struct HdfWlanDevice *)OsalMemCalloc(sizeof(struct HdfWlanDevice));
349     if (device == NULL) {
350         HDF_LOGE("%s:ProbeDevice device NULL", __func__);
351         return NULL;
352     }
353 
354     do {
355         device->powers = HdfWlanCreatePowerManager(&deviceConfig->powers);
356         device->reset = HdfWlanCreateResetManager(&deviceConfig->reset, deviceConfig->bootUpTimeOut);
357 
358         ret = HdfWlanPowerOnProcess(device->powers);
359         if (ret != HDF_SUCCESS) {
360             HDF_LOGE("%s:HdfWlanPowerOnProcess failed!", __func__);
361             break;
362         }
363 
364         ret = HdfWlanResetProcess(device->reset);
365         if (ret != HDF_SUCCESS) {
366             HDF_LOGE("%s:HdfWlanResetProcess failed!", __func__);
367             break;
368         }
369 
370         ret = HdfWlanBusInit(device, &deviceConfig->bus);
371         if (ret != HDF_SUCCESS) {
372             HDF_LOGE("%s:NO Sdio Card in hdf wlan init proc", __func__);
373             break;
374         }
375     } while (false);
376 
377     if (ret != HDF_SUCCESS) {
378         ReleaseWlanDevice(device);
379         return NULL;
380     }
381     return device;
382 }
383 
HdfWifiDeinitDevice(struct HdfWlanDevice * device)384 int32_t HdfWifiDeinitDevice(struct HdfWlanDevice *device)
385 {
386     struct HdfChipDriverFactory *chipDriverFact = NULL;
387     int32_t ret;
388     if (device == NULL) {
389         HDF_LOGE("%s:Input is NULL!", __func__);
390         return HDF_FAILURE;
391     }
392     chipDriverFact = HdfWlanGetDriverFactory(device->driverName);
393     if (chipDriverFact == NULL) {
394         HDF_LOGE("%s: get chipDriverFact failed! driverName=%s", __func__, device->driverName);
395         return HDF_FAILURE;
396     }
397     ret = HdfWlanDeinitInterfaces(device, chipDriverFact);
398     if (ret != HDF_SUCCESS) {
399         HDF_LOGE("%s:Deinit interface of device %d failed!ret=%d", __func__, device->id, ret);
400         return ret;
401     }
402 
403     if (chipDriverFact->DeinitChip != NULL) {
404         return chipDriverFact->DeinitChip(device);
405     }
406     return HDF_SUCCESS;
407 }
408 
HdfWifiInitDevice(struct HdfWlanDevice * device)409 int32_t HdfWifiInitDevice(struct HdfWlanDevice *device)
410 {
411     int32_t ret;
412     struct HdfChipDriverFactory *chipDriverFact = NULL;
413 
414     chipDriverFact = HdfWlanGetDriverFactory(device->driverName);
415     if (chipDriverFact == NULL) {
416         HDF_LOGE("%s: get chipDriverFact failed! driverName=%s", __func__, device->driverName);
417         return HDF_FAILURE;
418     }
419     if (chipDriverFact->InitChip != NULL) {
420         ret = chipDriverFact->InitChip(device);
421         if (ret != HDF_SUCCESS) {
422             HDF_LOGE("%s:Init chip failed! ret=%d!", __func__, ret);
423             return ret;
424         }
425     }
426 
427     ret = HdfWlanInitInterfaces(device, chipDriverFact);
428     if (ret != HDF_SUCCESS) {
429         HDF_LOGE("%s: Init interfaces failed! ret=%d!", __func__, ret);
430     } else {
431         HDF_LOGD("%s: Init interfaces success! netIfMap=%x.", __func__, device->netIfMap);
432     }
433     return ret;
434 }
435 
436 /* thread callback function */
HdfWlanInitThread(void * para)437 static int32_t HdfWlanInitThread(void *para)
438 {
439     const uint32_t initDelaySec = 15;
440     struct HdfDeviceObject *device = (struct HdfDeviceObject *)para;
441     struct SubscriberCallback callback = { NULL };
442     struct HdfConfigWlanDeviceList *devList = NULL;
443     struct HdfWlanDevice *wlanDevice = NULL;
444     uint16_t i;
445     int32_t ret;
446 
447     OsalSleep(initDelaySec);
448     HDF_LOGI("%s: Driver HdfWiFi initing...", __func__);
449     if (device == NULL) {
450         HDF_LOGE("%s: Device is null!", __func__);
451         return HDF_FAILURE;
452     }
453 
454     devList = HdfWlanGetDeviceList();
455     if (devList == NULL) {
456         HDF_LOGW("%s:No device defined.", __func__);
457         return HDF_SUCCESS;
458     }
459     for (i = 0; i < devList->deviceListSize; i++) {
460         ret = HdfWlanConfigBusAbs(devList->deviceInst[i].bus.busIdx);
461         if (ret != HDF_SUCCESS) {
462             HDF_LOGE("%s:HdfWlanConfigSDIO %d failed!ret=%d", __func__, devList->deviceInst[i].bus.busIdx, ret);
463             continue;
464         }
465 
466         wlanDevice = ProbeDevice(&(devList->deviceInst[i]));
467         if (wlanDevice == NULL) {
468             HDF_LOGE("%s:Device %d detect failed!", __func__, i);
469             continue;
470         }
471 
472         ret = HdfWlanAddDevice(wlanDevice);
473         if (ret != HDF_SUCCESS) {
474             HDF_LOGE("%s:add device failed!", __func__);
475             ReleaseWlanDevice(wlanDevice);
476             continue;
477         }
478 
479         // Load chip driver
480         (void)DevSvcManagerClntSubscribeService(wlanDevice->driverName, callback);
481         (void)HdfWifiInitDevice(wlanDevice);
482     }
483 
484     HDF_LOGI("%s: HdfWlanInitThread successful!", __func__);
485     return HDF_SUCCESS;
486 }
487 /* load chip factory thread */
HdfWlanScanAndInitThread(struct HdfDeviceObject * device)488 static int32_t HdfWlanScanAndInitThread(struct HdfDeviceObject *device)
489 {
490     struct OsalThread factoryTread;
491     struct OsalThreadParam chipLoadCfg;
492     (void)memset_s(&chipLoadCfg, sizeof(chipLoadCfg), 0, sizeof(chipLoadCfg));
493     chipLoadCfg.name = "chip_factory_load";
494     chipLoadCfg.priority = OSAL_THREAD_PRI_DEFAULT;
495     chipLoadCfg.stackSize = 0x2000;
496 
497     int32_t ret = OsalThreadCreate(&factoryTread, HdfWlanInitThread, (void *)device);
498     if (ret != HDF_SUCCESS) {
499         HDF_LOGE("%s: fail to create thread", __func__);
500         return HDF_ERR_THREAD_CREATE_FAIL;
501     }
502     ret = OsalThreadStart(&factoryTread, &chipLoadCfg);
503     if (ret != HDF_SUCCESS) {
504         /* lay stress on the use of threadcreate */
505         HDF_LOGE("LoadChipFactoryThread: start LoadChipFactoryThread thread fail:%d", ret);
506         return HDF_DEV_ERR_DEV_INIT_FAIL;
507     }
508     HDF_LOGI("%s: HdfWlanScanAndInitThread successful!", __func__);
509     return HDF_SUCCESS;
510 }
511 
512 /* main init process including config, powers, load the factory, and chip init */
HdfWlanMainInit(struct HdfDeviceObject * device)513 static int32_t HdfWlanMainInit(struct HdfDeviceObject *device)
514 {
515     struct HdfConfigWlanRoot *rootConfig = NULL;
516     const struct HdfConfigWlanModuleConfig *moduleConfig = NULL;
517 
518     HDF_LOGI("%s: HdfWlanMainInit start...", __func__);
519     if (device == NULL) {
520         HDF_LOGE("%s: Input param is null!", __func__);
521         return HDF_FAILURE;
522     }
523     if (HdfWlanGetConfig(device) != HDF_SUCCESS) {
524         HDF_LOGE("%s:HdfWlanGetConfig get wlan config failed!", __func__);
525         return HDF_FAILURE;
526     }
527     /* feature init */
528     rootConfig = HdfWlanGetModuleConfigRoot();
529     if (rootConfig == NULL) {
530         HDF_LOGE("%s: HdfWlanGetModuleConfigRoot get failed", __func__);
531         return HDF_FAILURE;
532     }
533     moduleConfig = &rootConfig->wlanConfig.moduleConfig;
534     if (HdfWlanInitProduct(device, moduleConfig) != HDF_SUCCESS) {
535         HDF_LOGE("%s:HdfWlanInitProduct failed!", __func__);
536         return HDF_FAILURE;
537     }
538     if (HdfWlanScanAndInitThread(device) != HDF_SUCCESS) {
539         HDF_LOGE("%s: LoadChipFactoryThread failed, the load process failed!", __func__);
540         return HDF_FAILURE;
541     }
542     HDF_LOGI("%s: HdfWlanMainInit finished!", __func__);
543     return HDF_SUCCESS;
544 }
545 /* release the resource created in the init process */
HdfWlanDriverRelease(struct HdfDeviceObject * object)546 static void HdfWlanDriverRelease(struct HdfDeviceObject *object)
547 {
548     ErrorCode errCode;
549     HDF_LOGW("Driver HdfWiFi deiniting...");
550     (void)object;
551 
552     errCode = ShutdownMessageRouter();
553     if (errCode != ME_SUCCESS) {
554         HDF_LOGE("%s:shutdown router failed!err=%d", __func__, errCode);
555     }
556     HDF_LOGW("Driver HdfWiFi deinited.");
557 }
558 
559 struct HdfDriverEntry g_hdfWifiEntry = {
560     .moduleVersion = 1,
561     .Bind = HdfWifiDriverBind,
562     .Init = HdfWlanMainInit,
563     .Release = HdfWlanDriverRelease,
564     .moduleName = "HDF_WIFI"
565 };
566 
567 HDF_INIT(g_hdfWifiEntry);
568 
569