1# WLAN<a name="EN-US_TOPIC_0000001051643558"></a> 2 3## Overview<a name="section729758162218"></a> 4 5The WLAN module is developed based on the Hardware Driver Foundation \(HDF\). It supports cross-OS migration, component adaptation, and modular assembly and compilation. Based on the unified APIs provided by the WLAN module, driver developers of WLAN vendors can adapt their driver code and are capable of creating, disabling, scanning, and connecting to WLAN hotspots. The WLAN driver provides the Hardware Driver Interface \(HDI\) layer with the capabilities of setting and obtaining the device MAC address and setting the transmit power. The following figure shows the framework of the WLAN module: 6 7**Figure 1** WLAN framework<a name="fig4415112614415"></a> 8 9 10### WLAN Driver API Architecture<a name="section178022416377"></a> 11 12The WLAN module provides the following three types of APIs: 13 141. Capability APIs for the HDI layer 15 162. Capability APIs directly invoked by drivers 17 183. Capability APIs for vendors 19 20**Figure 2** Available APIs of the WLAN module<a name="fig1492411431166"></a> 21 22 23## Available APIs<a name="section7331102018815"></a> 24 25The WLAN driver module provides APIs that can be directly called by driver developers, such as creating/releasing a **WifiModule**, connecting to/disconnecting from a WLAN hotspot, applying for/releasing a **NetBuf**, and converting between the **pbuf** structure of Lightweight IP \(lwIP\) and a **NetBuf**. [Table 1](#table1521573319472) describes some APIs. 26 27**Table 1** APIs that can be directly called by driver developers 28 29<a name="table1521573319472"></a> 30<table><thead align="left"><tr id="row121519334474"><th class="cellrowborder" valign="top" width="15.079999999999998%" id="mcps1.2.4.1.1"><p id="p1221510339475"><a name="p1221510339475"></a><a name="p1221510339475"></a>File</p> 31</th> 32<th class="cellrowborder" valign="top" width="60.33%" id="mcps1.2.4.1.2"><p id="p0215153344716"><a name="p0215153344716"></a><a name="p0215153344716"></a>Function</p> 33</th> 34<th class="cellrowborder" valign="top" width="24.59%" id="mcps1.2.4.1.3"><p id="p1421503315478"><a name="p1421503315478"></a><a name="p1421503315478"></a>Description</p> 35</th> 36</tr> 37</thead> 38<tbody><tr id="row112150333476"><td class="cellrowborder" rowspan="4" valign="top" width="15.079999999999998%" headers="mcps1.2.4.1.1 "><p id="p2155710125317"><a name="p2155710125317"></a><a name="p2155710125317"></a>wifi_module.h</p> 39<p id="p189132019183"><a name="p189132019183"></a><a name="p189132019183"></a></p> 40</td> 41<td class="cellrowborder" valign="top" width="60.33%" headers="mcps1.2.4.1.2 "><p id="p363110387399"><a name="p363110387399"></a><a name="p363110387399"></a>struct WifiModule *WifiModuleCreate(const struct HdfConfigWifiModuleConfig *config);</p> 42</td> 43<td class="cellrowborder" valign="top" width="24.59%" headers="mcps1.2.4.1.3 "><p id="p1363012387393"><a name="p1363012387393"></a><a name="p1363012387393"></a>Creates a <strong id="b17218103820131"><a name="b17218103820131"></a><a name="b17218103820131"></a>WifiModule</strong>.</p> 44</td> 45</tr> 46<tr id="row112151233194714"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p7629163817393"><a name="p7629163817393"></a><a name="p7629163817393"></a>void WifiModuleDelete(struct WifiModule *module);</p> 47</td> 48<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p2627638173917"><a name="p2627638173917"></a><a name="p2627638173917"></a>Deletes a <strong id="b17674114211319"><a name="b17674114211319"></a><a name="b17674114211319"></a>WifiModule</strong> and releases its data.</p> 49</td> 50</tr> 51<tr id="row1121533316475"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p12626103814399"><a name="p12626103814399"></a><a name="p12626103814399"></a>int32_t DelFeature(struct WifiModule *module, uint16_t featureType);</p> 52</td> 53<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p1162543816393"><a name="p1162543816393"></a><a name="p1162543816393"></a>Deletes a feature from a <strong id="b12747104710136"><a name="b12747104710136"></a><a name="b12747104710136"></a>WifiModule</strong>.</p> 54</td> 55</tr> 56<tr id="row172153335473"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p162433816392"><a name="p162433816392"></a><a name="p162433816392"></a>int32_t AddFeature(struct WifiModule *module, uint16_t featureType, struct WifiFeature *featureData);</p> 57</td> 58<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p186235383393"><a name="p186235383393"></a><a name="p186235383393"></a>Adds a feature to a <strong id="b61351114111410"><a name="b61351114111410"></a><a name="b61351114111410"></a>WifiModule</strong>.</p> 59</td> 60</tr> 61<tr id="row451796205011"><td class="cellrowborder" rowspan="4" valign="top" width="15.079999999999998%" headers="mcps1.2.4.1.1 "><p id="p2659417135013"><a name="p2659417135013"></a><a name="p2659417135013"></a>wifi_mac80211_ops.h</p> 62</td> 63<td class="cellrowborder" valign="top" width="60.33%" headers="mcps1.2.4.1.2 "><p id="p175181615011"><a name="p175181615011"></a><a name="p175181615011"></a>int32_t (*startAp)(NetDevice *netDev);</p> 64</td> 65<td class="cellrowborder" valign="top" width="24.59%" headers="mcps1.2.4.1.3 "><p id="p195182610507"><a name="p195182610507"></a><a name="p195182610507"></a>Starts an AP.</p> 66</td> 67</tr> 68<tr id="row5518663503"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p125181260501"><a name="p125181260501"></a><a name="p125181260501"></a>int32_t (*stopAp)(NetDevice *netDev);</p> 69</td> 70<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p1151815635014"><a name="p1151815635014"></a><a name="p1151815635014"></a>Stops an AP.</p> 71</td> 72</tr> 73<tr id="row851915617503"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p20519865500"><a name="p20519865500"></a><a name="p20519865500"></a>int32_t (*connect)(NetDevice *netDev, WifiConnectParams *param);</p> 74</td> 75<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p14519469509"><a name="p14519469509"></a><a name="p14519469509"></a>Connects to a hotspot.</p> 76</td> 77</tr> 78<tr id="row18519136185016"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p145195620502"><a name="p145195620502"></a><a name="p145195620502"></a>int32_t (*disconnect)(NetDevice *netDev, uint16_t reasonCode);</p> 79</td> 80<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p175191863503"><a name="p175191863503"></a><a name="p175191863503"></a>Disconnects from a hotspot.</p> 81</td> 82</tr> 83<tr id="row176421942125016"><td class="cellrowborder" rowspan="5" valign="top" width="15.079999999999998%" headers="mcps1.2.4.1.1 "><p id="p7937165012500"><a name="p7937165012500"></a><a name="p7937165012500"></a>hdf_netbuf.h</p> 84</td> 85<td class="cellrowborder" valign="top" width="60.33%" headers="mcps1.2.4.1.2 "><p id="p1964211423505"><a name="p1964211423505"></a><a name="p1964211423505"></a>static inline void NetBufQueueInit(struct NetBufQueue *q);</p> 86</td> 87<td class="cellrowborder" valign="top" width="24.59%" headers="mcps1.2.4.1.3 "><p id="p364254211507"><a name="p364254211507"></a><a name="p364254211507"></a>Initializes a <strong id="b8331245144114"><a name="b8331245144114"></a><a name="b8331245144114"></a>NetBuf</strong> queue.</p> 88</td> 89</tr> 90<tr id="row664264225020"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p166421942115017"><a name="p166421942115017"></a><a name="p166421942115017"></a>struct NetBuf *NetBufAlloc(uint32_t size);</p> 91</td> 92<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p3642164215501"><a name="p3642164215501"></a><a name="p3642164215501"></a>Applies for a <strong id="b15893135016416"><a name="b15893135016416"></a><a name="b15893135016416"></a>NetBuf</strong>.</p> 93</td> 94</tr> 95<tr id="row19642134215018"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p964310425501"><a name="p964310425501"></a><a name="p964310425501"></a>void NetBufFree(struct NetBuf *nb);</p> 96</td> 97<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p1464312427503"><a name="p1464312427503"></a><a name="p1464312427503"></a>Releases a <strong id="b87021256204112"><a name="b87021256204112"></a><a name="b87021256204112"></a>NetBuf</strong>.</p> 98</td> 99</tr> 100<tr id="row7643194215013"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p20643164218508"><a name="p20643164218508"></a><a name="p20643164218508"></a>struct NetBuf *Pbuf2NetBuf(const struct NetDevice *netdev, struct pbuf *lwipBuf);</p> 101</td> 102<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p186437429509"><a name="p186437429509"></a><a name="p186437429509"></a>Converts the <strong id="b108101241316"><a name="b108101241316"></a><a name="b108101241316"></a>pbuf</strong> structure of <strong id="b148154412318"><a name="b148154412318"></a><a name="b148154412318"></a>lwIP</strong> to a <strong id="b8994115217371"><a name="b8994115217371"></a><a name="b8994115217371"></a>NetBuf</strong>.</p> 103</td> 104</tr> 105<tr id="row7657132317518"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p86576231557"><a name="p86576231557"></a><a name="p86576231557"></a>struct pbuf *NetBuf2Pbuf(const struct NetBuf *nb);</p> 106</td> 107<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p1965702312510"><a name="p1965702312510"></a><a name="p1965702312510"></a>Converts a <strong id="b92856616388"><a name="b92856616388"></a><a name="b92856616388"></a>NetBuf</strong> to the <strong id="b993215663110"><a name="b993215663110"></a><a name="b993215663110"></a>pbuf</strong> structure of <strong id="b10933563310"><a name="b10933563310"></a><a name="b10933563310"></a>lwIP</strong>.</p> 108</td> 109</tr> 110</tbody> 111</table> 112 113The WLAN driver module provides APIs for driver developers, such as initializing/deregistering, opening/stopping a **NetDevice**, and obtaining the state of a **NetDevice**. [Table 2](#table74613501475) describes some APIs. 114 115**Table 2** APIs for driver developers of WLAN vendors to implement 116 117<a name="table74613501475"></a> 118<table><thead align="left"><tr id="row194625016476"><th class="cellrowborder" valign="top" width="20.75%" id="mcps1.2.4.1.1"><p id="p10468502479"><a name="p10468502479"></a><a name="p10468502479"></a>File</p> 119</th> 120<th class="cellrowborder" valign="top" width="52.75%" id="mcps1.2.4.1.2"><p id="p184615501477"><a name="p184615501477"></a><a name="p184615501477"></a>Function</p> 121</th> 122<th class="cellrowborder" valign="top" width="26.5%" id="mcps1.2.4.1.3"><p id="p1146135044719"><a name="p1146135044719"></a><a name="p1146135044719"></a>Description</p> 123</th> 124</tr> 125</thead> 126<tbody><tr id="row04616509472"><td class="cellrowborder" rowspan="6" valign="top" width="20.75%" headers="mcps1.2.4.1.1 "><p id="p14615017477"><a name="p14615017477"></a><a name="p14615017477"></a>net_device.h</p> 127</td> 128<td class="cellrowborder" valign="top" width="52.75%" headers="mcps1.2.4.1.2 "><p id="p144943564611"><a name="p144943564611"></a><a name="p144943564611"></a>int32_t (*init)(struct NetDevice *netDev);</p> 129</td> 130<td class="cellrowborder" valign="top" width="26.5%" headers="mcps1.2.4.1.3 "><p id="p18822442135411"><a name="p18822442135411"></a><a name="p18822442135411"></a>Initializes a <strong id="b1947112374310"><a name="b1947112374310"></a><a name="b1947112374310"></a>NetDevice</strong>.</p> 131</td> 132</tr> 133<tr id="row1546250114713"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p1490010315564"><a name="p1490010315564"></a><a name="p1490010315564"></a>struct NetDevStats *(*getStats)(struct NetDevice *netDev);</p> 134</td> 135<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p5900163115564"><a name="p5900163115564"></a><a name="p5900163115564"></a>Obtains the state of a <strong id="b4952125574216"><a name="b4952125574216"></a><a name="b4952125574216"></a>NetDevice</strong>.</p> 136</td> 137</tr> 138<tr id="row1646165010470"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p16909135319564"><a name="p16909135319564"></a><a name="p16909135319564"></a>int32_t (*setMacAddr)(struct NetDevice *netDev, void *addr);</p> 139</td> 140<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p122001431115713"><a name="p122001431115713"></a><a name="p122001431115713"></a>Sets the MAC address.</p> 141</td> 142</tr> 143<tr id="row12471250184711"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p154213655215"><a name="p154213655215"></a><a name="p154213655215"></a>void (*deInit)(struct NetDevice *netDev);</p> 144</td> 145<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p14845675719"><a name="p14845675719"></a><a name="p14845675719"></a>Deinitializes a <strong id="b18383143517427"><a name="b18383143517427"></a><a name="b18383143517427"></a>NetDevice</strong>.</p> 146</td> 147</tr> 148<tr id="row13471050104719"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p16686131655218"><a name="p16686131655218"></a><a name="p16686131655218"></a>int32_t (*open)(struct NetDevice *netDev);</p> 149</td> 150<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p164825613576"><a name="p164825613576"></a><a name="p164825613576"></a>Opens a <strong id="b226313919425"><a name="b226313919425"></a><a name="b226313919425"></a>NetDevice</strong>.</p> 151</td> 152</tr> 153<tr id="row1747125054714"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p2310615407"><a name="p2310615407"></a><a name="p2310615407"></a>int32_t (*stop)(struct NetDevice *netDev);</p> 154</td> 155<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p1982212428542"><a name="p1982212428542"></a><a name="p1982212428542"></a>Stops a <strong id="b81039430429"><a name="b81039430429"></a><a name="b81039430429"></a>NetDevice</strong>.</p> 156</td> 157</tr> 158</tbody> 159</table> 160 161The WLAN driver provides the HDI layer with the APIs for creating and destroying an **IWiFi** object and setting the MAC address. [Table 3](#table141076311618) describes some APIs. 162 163**Table 3** APIs provided by the WLAN HAL module 164 165<a name="table141076311618"></a> 166<table><thead align="left"><tr id="row010716312120"><th class="cellrowborder" valign="top" width="15.950000000000001%" id="mcps1.2.4.1.1"><p id="p1110713311116"><a name="p1110713311116"></a><a name="p1110713311116"></a>Header File</p> 167</th> 168<th class="cellrowborder" valign="top" width="59.46%" id="mcps1.2.4.1.2"><p id="p161073311610"><a name="p161073311610"></a><a name="p161073311610"></a>Function</p> 169</th> 170<th class="cellrowborder" valign="top" width="24.59%" id="mcps1.2.4.1.3"><p id="p110716315118"><a name="p110716315118"></a><a name="p110716315118"></a>Description</p> 171</th> 172</tr> 173</thead> 174<tbody><tr id="row41077311218"><td class="cellrowborder" rowspan="4" valign="top" width="15.950000000000001%" headers="mcps1.2.4.1.1 "><p id="p1810719311013"><a name="p1810719311013"></a><a name="p1810719311013"></a>wifi_hal.h</p> 175<p id="p12107931715"><a name="p12107931715"></a><a name="p12107931715"></a></p> 176</td> 177<td class="cellrowborder" valign="top" width="59.46%" headers="mcps1.2.4.1.2 "><p id="p1910815311519"><a name="p1910815311519"></a><a name="p1910815311519"></a>int32_t WifiConstruct(struct IWiFi **wifiInstance);</p> 178</td> 179<td class="cellrowborder" valign="top" width="24.59%" headers="mcps1.2.4.1.3 "><p id="p12108133112115"><a name="p12108133112115"></a><a name="p12108133112115"></a>Creates an <strong id="b218481014265"><a name="b218481014265"></a><a name="b218481014265"></a>IWiFi</strong> object with basic capabilities.</p> 180</td> 181</tr> 182<tr id="row20108183110111"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p71083311617"><a name="p71083311617"></a><a name="p71083311617"></a>int32_t WifiDestruct(struct IWiFi **wifiInstance);</p> 183</td> 184<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p1010810315118"><a name="p1010810315118"></a><a name="p1010810315118"></a>Destroys an <strong id="b1269381514265"><a name="b1269381514265"></a><a name="b1269381514265"></a>IWiFi</strong> object.</p> 185</td> 186</tr> 187<tr id="row19108131417"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p810818315118"><a name="p810818315118"></a><a name="p810818315118"></a>int32_t (*start)(struct IWiFi *);</p> 188</td> 189<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p9108113116119"><a name="p9108113116119"></a><a name="p9108113116119"></a>Creates a channel between the HAL and the driver and obtains the NIC supported by the driver.</p> 190</td> 191</tr> 192<tr id="row810803112116"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p1310823110116"><a name="p1310823110116"></a><a name="p1310823110116"></a>int32_t (*stop)(struct IWiFi *);</p> 193</td> 194<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p12108183112110"><a name="p12108183112110"></a><a name="p12108183112110"></a>Stops the channel between the HAL and the driver.</p> 195</td> 196</tr> 197<tr id="row91081731717"><td class="cellrowborder" rowspan="4" valign="top" width="15.950000000000001%" headers="mcps1.2.4.1.1 "><p id="p1910814312115"><a name="p1910814312115"></a><a name="p1910814312115"></a>wifi_hal_base_feature.h</p> 198</td> 199<td class="cellrowborder" valign="top" width="59.46%" headers="mcps1.2.4.1.2 "><p id="p2108123113111"><a name="p2108123113111"></a><a name="p2108123113111"></a>int32_t (*getFeatureType)(const struct IWiFiBaseFeature *);</p> 200</td> 201<td class="cellrowborder" valign="top" width="24.59%" headers="mcps1.2.4.1.3 "><p id="p910843113113"><a name="p910843113113"></a><a name="p910843113113"></a>Obtains the feature type.</p> 202</td> 203</tr> 204<tr id="row161081931318"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p1610814318119"><a name="p1610814318119"></a><a name="p1610814318119"></a>int32_t (*setMacAddress)(const struct IWiFiBaseFeature *, unsigned char *, uint8_t);</p> 205</td> 206<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p101081031818"><a name="p101081031818"></a><a name="p101081031818"></a>Sets the MAC address.</p> 207</td> 208</tr> 209<tr id="row191081631318"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p191087311317"><a name="p191087311317"></a><a name="p191087311317"></a>int32_t (*getDeviceMacAddress)(const struct IWiFiBaseFeature *, unsigned char *, uint8_t);</p> 210</td> 211<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p1110810312012"><a name="p1110810312012"></a><a name="p1110810312012"></a>Obtains the device MAC address.</p> 212</td> 213</tr> 214<tr id="row21080317115"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p310873115118"><a name="p310873115118"></a><a name="p310873115118"></a>int32_t (*setTxPower)(const struct IWiFiBaseFeature *, int32_t);</p> 215</td> 216<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p410817311911"><a name="p410817311911"></a><a name="p410817311911"></a>Sets the transmit power.</p> 217</td> 218</tr> 219</tbody> 220</table> 221 222## How to Develop<a name="section15957746172412"></a> 223 224The WLAN driver is developed based on the HDF and PLATFORM. It provides a unified driver model for WLAN modules of different vendors regardless of the operating system \(OS\) and system on a chip \(SoC\). 225 2261. Set hardware parameters such as **module** \(different features\) and **chip** in the **wifi\_config.hcs** file. 2272. Parse the **wifi\_config.hcs** file and generate a structure with the configured parameters. 2283. Initialize and create a module. 2294. Mount and initialize the chip. 2305. Initialize the bus. 2316. Mount the upper-layer WPA service. 232 233> **NOTE:** 234>Some of the above adaptation steps have been provided. For details, see [Development Example](#section1395253612512). The steps waiting to be performed by developers include setting configuration parameters based on hardware attributes, adapting and mounting a chip, and performing tests and verification. 235 236## Development Example<a name="section1395253612512"></a> 237 238This example describes how to initialize a WLAN module. The following uses the Hi3881 WLAN chip as an example: 239 2401. Set parameters for the WLAN module based on hardware attributes. 241 242``` 243/* Set parameters in the wlan_platform.hcs file based on hardware attributes. The following is an example of the WLAN platform configuration. */ 244hisi :& deviceList { 245 device0 :: deviceInst { 246 deviceInstId = 0; 247 powers { 248 power0 { 249 powerSeqDelay = 0; /* Power supply sequencing delay */ 250 powerType = 1; /* Power supply type. Value 0 indicates that the power supply is always on, and value 1 indicates power supply through general-purpose input/output (GPIO). */ 251 gpioId = 1; /* GPIO pin ID */ 252 activeLevel=1; /* Active level. Value 0 indicates a low level, and value 1 indicates a high level. */ 253 } 254 power1 { 255 powerSeqDelay = 0; /* Power supply sequencing delay */ 256 powerType = 0; /* Power supply type. Value 0 indicates that the power supply is always on, and value 1 indicates power supply through GPIO. */ 257 } 258 } 259 reset { 260 resetType = 0; /* Reset type. Value 0 indicates that reset is not supported, and value 1 indicates reset through GPIO. */ 261 gpioId = 2; /* GPIO pin ID */ 262 activeLevel=1; /* Active level. Value 0 indicates a low level, and value 1 indicates a high level. */ 263 resetHoldTime = 30; /* Hold time (ms) for a reset */ 264 } 265 bootUpTimeout = 30; /* Boot timeout duration (ms) */ 266 bus { 267 busType = 0; /* Bus type. Value 0 indicates secure digital input/output (SDIO). */ 268 busId = 2; /* Bus ID */ 269 funcNum = [1]; /* SDIO function number */ 270 timeout = 1000; /* Timeout duration for data read/write */ 271 blockSize = 512; /* Size of the data block to read or write */ 272 } 273 } 274} 275/* Add the configuration file wlan_chip_<Chip name>.hcs (for example, wlan_chip_hi3881.hcs) for each chip and set parameters. The following uses the Hi3881 chip as an example. */ 276root { 277 wlan_config { 278 hi3881 :& chipList { 279 chipHi3881 :: chipInst { 280 match_attr = "hdf_wlan_chips_hi3881"; /* Match attribute */ 281 chipName = "hi3881"; /* WLAN chip name */ 282 sdio { 283 vendorId = 0x0296; /* Vendor ID */ 284 deviceId = [0x5347]; /* Device ID */ 285 } 286 } 287 } 288 } 289} 290``` 291 2922. Mount the **init** and **deinit** functions of the WLAN chip and WLAN chip driver. 293 294``` 295/* WLAN module initialization and mount process */ 296#include "hdf_device_desc.h" 297#include "hdf_wifi_product.h" 298#include "hdf_log.h" 299#include "osal_mem.h" 300#include "hdf_wlan_chipdriver_manager.h" 301#include "securec.h" 302#include "wifi_module.h" 303#include "hi_wifi_api.h" 304#include "hi_types_base.h" 305 306#define HDF_LOG_TAG Hi3881Driver 307 308/* Functions for initializing and deinitializing the WLAN chip */ 309int32_t InitHi3881Chip(struct HdfWlanDevice *device); 310int32_t DeinitHi3881Chip(struct HdfWlanDevice *device); 311/* Functions for initializing and deinitializing the WLAN chip driver */ 312int32_t Hi3881Deinit(struct HdfChipDriver* chipDriver, struct NetDevice *netDevice); 313int32_t Hi3881Init(struct HdfChipDriver* chipDriver, struct NetDevice *netDevice); 314 315/* Initialize mac80211 and mount functions of the chip. */ 316hi_void HiMac80211Init(struct HdfChipDriver *chipDriver); 317 318static const char* const HI3881_DRIVER_NAME = "hisi"; 319 320/* Mount the WLAN chip driver and the functions of mac80211 and the chip. */ 321static struct HdfChipDriver *BuildHi3881Driver(struct HdfWlanDevice *device, uint8_t ifIndex) 322{ 323 struct HdfChipDriver *specificDriver = NULL; 324 if (device == NULL) { 325 HDF_LOGE("%s fail : channel is NULL", __func__); 326 return NULL; 327 } 328 (void)device; 329 (void)ifIndex; 330 specificDriver = (struct HdfChipDriver *)OsalMemCalloc(sizeof(struct HdfChipDriver)); 331 if (specificDriver == NULL) { 332 HDF_LOGE("%s fail: OsalMemCalloc fail!", __func__); 333 return NULL; 334 } 335 if (memset_s(specificDriver, sizeof(struct HdfChipDriver), 0, sizeof(struct HdfChipDriver)) != EOK) { 336 HDF_LOGE("%s fail: memset_s fail!", __func__); 337 OsalMemFree(specificDriver); 338 return NULL; 339 } 340 341 if (strcpy_s(specificDriver->name, MAX_WIFI_COMPONENT_NAME_LEN, HI3881_DRIVER_NAME) != EOK) { 342 HDF_LOGE("%s fail : strcpy_s fail", __func__); 343 OsalMemFree(specificDriver); 344 return NULL; 345 } 346 specificDriver->init = Hi3881Init; 347 specificDriver->deinit = Hi3881Deinit; 348 349 HiMac80211Init(specificDriver); 350 351 return specificDriver; 352} 353 354/* Release the WLAN chip driver. */ 355static void ReleaseHi3881Driver(struct HdfChipDriver *chipDriver) 356{ 357 if (chipDriver == NULL) { 358 return; 359 } 360 if (strcmp(chipDriver->name, HI3881_DRIVER_NAME) != 0) { 361 HDF_LOGE("%s:Not my driver!", __func__); 362 return; 363 } 364 OsalMemFree(chipDriver); 365} 366 367static uint8_t GetHi3881GetMaxIFCount(struct HdfChipDriverFactory *factory) { 368 (void)factory; 369 return 1; 370} 371 372/* Register functions related to the WLAN chip. */ 373static int32_t HDFWlanRegHisiDriverFactory(void) 374{ 375 static struct HdfChipDriverFactory tmpFactory = { 0 }; 376 struct HdfChipDriverManager *driverMgr = NULL; 377 driverMgr = HdfWlanGetChipDriverMgr(); 378 if (driverMgr == NULL && driverMgr->RegChipDriver != NULL) { 379 HDF_LOGE("%s fail: driverMgr is NULL!", __func__); 380 return HDF_FAILURE; 381 } 382 tmpFactory.driverName = HI3881_DRIVER_NAME; 383 tmpFactory.GetMaxIFCount = GetHi3881GetMaxIFCount; 384 tmpFactory.InitChip = InitHi3881Chip; 385 tmpFactory.DeinitChip = DeinitHi3881Chip; 386 tmpFactory.Build = BuildHi3881Driver; 387 tmpFactory.Release = ReleaseHi3881Driver; 388 tmpFactory.ReleaseFactory = NULL; 389 if (driverMgr->RegChipDriver(&tmpFactory) != HDF_SUCCESS) { 390 HDF_LOGE("%s fail: driverMgr is NULL!", __func__); 391 return HDF_FAILURE; 392 } 393 394 return HDF_SUCCESS; 395} 396 397static int32_t HdfWlanHisiChipDriverInit(struct HdfDeviceObject *device) 398{ 399 (void)device; 400 return HDFWlanRegHisiDriverFactory(); 401} 402 403struct HdfDriverEntry g_hdfHisiChipEntry = { 404 .moduleVersion = 1, 405 .Init = HdfWlanHisiChipDriverInit, 406 .moduleName = "HDF_WLAN_CHIPS" 407}; 408 409HDF_INIT(g_hdfHisiChipEntry); 410``` 411 412``` 413#include "hdf_wifi_product.h" 414#include "hi_wifi_api.h" 415#if (_PRE_OS_VERSION_LINUX == _PRE_OS_VERSION) 416#include "oal_thread.h" 417#include "osal_time.h" 418#endif 419#include "wifi_mac80211_ops.h" 420#include "wal_cfg80211.h" 421#include "net_adpater.h" 422#include "hdf_wlan_utils.h" 423 424#define HDF_LOG_TAG Hi3881Driver 425 426/* Initialize the WLAN chip. */ 427int32_t InitHi3881Chip(struct HdfWlanDevice *device) 428{ 429 uint8_t maxPortCount = 1; 430 int32_t ret = HI_SUCCESS; 431 uint8_t maxRetryCount = 2; 432 if (device == NULL) { 433 HDF_LOGE("%s:NULL ptr!", __func__); 434 return HI_FAIL; 435 } 436 437 do { 438 if (ret != HI_SUCCESS) { 439 if (device->reset != NULL && device->reset->Reset != NULL) { 440 device->reset->Reset(device->reset); 441 } 442 HDF_LOGE("%s:Retry init hi3881!last ret=%d", __func__, ret); 443 } 444 ret = hi_wifi_init(maxPortCount); 445 } while (ret != 0 && --maxRetryCount > 0); 446 447 if (ret != 0) { 448 HDF_LOGE("%s:Init hi3881 driver failed!", __func__); 449 return ret; 450 } 451 return HI_SUCCESS; 452} 453 454/* Deinitialize the WLAN chip. */ 455int32_t DeinitHi3881Chip(struct HdfWlanDevice *device) 456{ 457 (void)device; 458 int32_t ret = hi_wifi_deinit(); 459 if (ret != 0) { 460 HDF_LOGE("%s:Deinit failed!ret=%d", __func__, ret); 461 } 462 return ret; 463} 464 465/* Initialize the WLAN chip driver. */ 466int32_t Hi3881Init(struct HdfChipDriver *chipDriver, struct NetDevice *netDevice) 467{ 468 HDF_LOGI("%s: start...", __func__); 469 hi_u16 mode = wal_get_vap_mode(); 470 int32_t ret; 471 nl80211_iftype_uint8 type; 472 (void)chipDriver; 473 474 if (mode >= WAL_WIFI_MODE_BUTT) { 475 oam_error_log1(0, 0, "wal_init_drv_netdev:: invalid mode[%d]", mode); 476 return HI_FAIL; 477 } 478 479 if (mode == WAL_WIFI_MODE_STA) { 480 type = NL80211_IFTYPE_STATION; 481 } else if (mode == WAL_WIFI_MODE_AP) { 482 type = NL80211_IFTYPE_AP; 483 } else { 484 oam_error_log1(0, 0, "wal_init_drv_netdev:: invalid mode[%d]", mode); 485 return HI_FAIL; 486 } 487 488 ret = wal_init_drv_wlan_netdev(type, WAL_PHY_MODE_11N, netDevice); 489 if (ret != HI_SUCCESS) { 490 oam_error_log2(0, OAM_SF_ANY, "wal_init_drv_netdev %s failed.l_return:%d\n", netDevice->name, ret); 491 } 492 return ret; 493} 494 495/* Deinitialize the WLAN chip driver. */ 496int32_t Hi3881Deinit(struct HdfChipDriver *chipDriver, struct NetDevice *netDevice) 497{ 498 (void)chipDriver; 499 int32_t ret = wal_deinit_drv_wlan_netdev(netDevice); 500 if (ret != HDF_SUCCESS) { 501 return ret; 502 } 503 return ReleasePlatformNetDevice(netDevice); 504} 505``` 506 5073. During the chip initialization, call the **NetDeviceInit\(\)** function to initialize a network device, call the **NetDeviceAdd\(\)** function to add the network device to a protocol stack, and implement some function pointers of **netdev**. 508 509``` 510hi_s32 wal_init_drv_wlan_netdev(nl80211_iftype_uint8 type, wal_phy_mode mode, hi_char* ifname, hi_u32* len) 511{ 512 oal_net_device_stru *netdev = HI_NULL; 513 514 ...... 515 /* Initialize the network device and obtain the initialized instance. */ 516 netdev = NetDeviceInit(ifname, *len, LITE_OS); 517 oal_wireless_dev *wdev = (oal_wireless_dev *)oal_mem_alloc(OAL_MEM_POOL_ID_LOCAL, sizeof(oal_wireless_dev)); 518 ret = wal_init_netif(type, netdev, wdev); 519 520 ...... 521 522 return HI_SUCCESS; 523} 524/* Mount some function pointers of NetDeviceInterFace. */ 525oal_net_device_ops_stru g_wal_net_dev_ops = 526{ 527 .getStats = wal_netdev_get_stats, 528 .open = wal_netdev_open, 529 .stop = wal_netdev_stop, 530 .xmit = hmac_bridge_vap_xmit, 531 .ioctl = wal_net_device_ioctl, 532 .changeMtu = oal_net_device_change_mtu, 533 .init = oal_net_device_init, 534 .deInit = oal_net_free_netdev, 535#if (defined(_PRE_WLAN_FEATURE_FLOWCTL) || defined(_PRE_WLAN_FEATURE_OFFLOAD_FLOWCTL)) 536 .selectQueue = wal_netdev_select_queue, 537#endif 538 .setMacAddr = wal_netdev_set_mac_addr, 539#if (_PRE_OS_VERSION_LITEOS == _PRE_OS_VERSION) 540 .netifNotify = HI_NULL, 541#endif 542 .specialEtherTypeProcess = SpecialEtherTypeProcess, 543}; 544 545hi_s32 wal_init_netif(nl80211_iftype_uint8 type, oal_net_device_stru *netdev, const oal_wireless_dev *wdev) 546{ 547 /* Add the network device to a protocol stack. */ 548 hi_u32 ret = NetDeviceAdd(netdev, (Protocol80211IfType)type); 549 550 ...... 551 552 return HI_SUCCESS; 553} 554``` 555 5564. Implement functions of **WifiMac80211Ops**. 557 558``` 559/* Mount some function pointers of mac80211. */ 560 561/* MAC-layer APIs for basic capabilities that need to be implemented by the driver */ 562static struct HdfMac80211BaseOps g_baseOps = { 563 .SetMode = WalSetMode, 564 .AddKey = WalAddKey, 565 .DelKey = WalDelKey, 566 .SetDefaultKey = WalSetDefaultKey, 567 .GetDeviceMacAddr = WalGetDeviceMacAddr, 568 .SetMacAddr = WalSetMacAddr, 569 .SetTxPower = WalSetTxPower, 570 .GetValidFreqsWithBand = WalGetValidFreqsWithBand, 571 .GetHwCapability = WalGetHwCapability 572}; 573 574/* MAC-layer APIs for station capabilities that need to be implemented by the driver */ 575static struct HdfMac80211STAOps g_staOps = { 576 .Connect = WalConnect, 577 .Disconnect = WalDisconnect, 578 .StartScan = WalStartScan, 579 .AbortScan = WalAbortScan, 580 .SetScanningMacAddress = WalSetScanningMacAddress, 581}; 582 583/* MAC-layer APIs for AP capabilities that need to be implemented by the driver */ 584static struct HdfMac80211APOps g_apOps = { 585 .ConfigAp = WalConfigAp, 586 .StartAp = WalStartAp, 587 .StopAp = WalStopAp, 588 .ConfigBeacon = WalChangeBeacon, 589 .DelStation = WalDelStation, 590 .SetCountryCode = WalSetCountryCode, 591 .GetAssociatedStasCount = WalGetAssociatedStasCount, 592 .GetAssociatedStasInfo = WalGetAssociatedStasInfo 593}; 594 595/* Initialize mac80211 and mount functions of the chip. */ 596hi_void HiMac80211Init(struct HdfChipDriver *chipDriver) 597{ 598 if (chipDriver == NULL) { 599 oam_error_log(0, OAM_SF_ANY, "%s:input is NULL!", __func__); 600 return; 601 } 602 chipDriver->ops = &g_baseOps; 603 chipDriver->staOps = &g_staOps; 604 chipDriver->apOps = &g_apOps; 605} 606``` 607 608