1# WLAN<a name="EN-US_TOPIC_0000001051643558"></a> 2 3- [Overview](#section729758162218) 4 - [WLAN Driver API Architecture](#section178022416377) 5 - [Available APIs](#section149681312202415) 6 7- [Development Guidelines](#section15957746172412) 8 - [How to Develop](#section11776186132513) 9 10- [Development Example](#section1395253612512) 11 12## Overview<a name="section729758162218"></a> 13 14The 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](#fig967034316227): 15 16**Figure 1** WLAN framework<a name="fig967034316227"></a> 17 18 19 20 21### WLAN Driver API Architecture<a name="section178022416377"></a> 22 23The WLAN module provides the following three types of APIs: 24 251. Capability APIs for the HDI layer 26 272. Capability APIs directly invoked by drivers 28 293. Capability APIs for vendors 30 31**Figure 2** Available APIs of the WLAN module<a name="fig15016395217"></a> 32 33 34 35 36### Available APIs<a name="section149681312202415"></a> 37 38The 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. 39 40**Table 1** APIs that can be directly called by driver developers 41 42<a name="table1521573319472"></a> 43<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> 44</th> 45<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> 46</th> 47<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> 48</th> 49</tr> 50</thead> 51<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> 52<p id="p189132019183"><a name="p189132019183"></a><a name="p189132019183"></a></p> 53</td> 54<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> 55</td> 56<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> 57</td> 58</tr> 59<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> 60</td> 61<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> 62</td> 63</tr> 64<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> 65</td> 66<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> 67</td> 68</tr> 69<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> 70</td> 71<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> 72</td> 73</tr> 74<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> 75</td> 76<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> 77</td> 78<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> 79</td> 80</tr> 81<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> 82</td> 83<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> 84</td> 85</tr> 86<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> 87</td> 88<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> 89</td> 90</tr> 91<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> 92</td> 93<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> 94</td> 95</tr> 96<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> 97</td> 98<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> 99</td> 100<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> 101</td> 102</tr> 103<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> 104</td> 105<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> 106</td> 107</tr> 108<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> 109</td> 110<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> 111</td> 112</tr> 113<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> 114</td> 115<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> 116</td> 117</tr> 118<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> 119</td> 120<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> 121</td> 122</tr> 123</tbody> 124</table> 125 126The 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. 127 128**Table 2** APIs for driver developers of WLAN vendors to implement 129 130<a name="table74613501475"></a> 131<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> 132</th> 133<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> 134</th> 135<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> 136</th> 137</tr> 138</thead> 139<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> 140</td> 141<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> 142</td> 143<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> 144</td> 145</tr> 146<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> 147</td> 148<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> 149</td> 150</tr> 151<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> 152</td> 153<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> 154</td> 155</tr> 156<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> 157</td> 158<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> 159</td> 160</tr> 161<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> 162</td> 163<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> 164</td> 165</tr> 166<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> 167</td> 168<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> 169</td> 170</tr> 171</tbody> 172</table> 173 174The 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. 175 176**Table 3** APIs provided by the WLAN HAL module 177 178<a name="table141076311618"></a> 179<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> 180</th> 181<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> 182</th> 183<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> 184</th> 185</tr> 186</thead> 187<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> 188<p id="p12107931715"><a name="p12107931715"></a><a name="p12107931715"></a></p> 189</td> 190<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> 191</td> 192<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> 193</td> 194</tr> 195<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> 196</td> 197<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> 198</td> 199</tr> 200<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> 201</td> 202<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> 203</td> 204</tr> 205<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> 206</td> 207<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> 208</td> 209</tr> 210<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> 211</td> 212<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> 213</td> 214<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> 215</td> 216</tr> 217<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> 218</td> 219<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> 220</td> 221</tr> 222<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> 223</td> 224<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> 225</td> 226</tr> 227<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> 228</td> 229<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> 230</td> 231</tr> 232</tbody> 233</table> 234 235## Development Guidelines<a name="section15957746172412"></a> 236 237The 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\). 238 239### How to Develop<a name="section11776186132513"></a> 240 2411. Set hardware parameters such as **module** \(different features\) and **chip** in the **wifi\_config.hcs** file. 2422. Parse the **wifi\_config.hcs** file and generate a structure with the configured parameters. 2433. Initialize and create a module. 2444. Mount and initialize the chip. 2455. Initialize the bus. 2466. Mount the upper-layer WPA service. 247 248> **NOTE:** 249>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. 250 251## Development Example<a name="section1395253612512"></a> 252 253This example describes how to initialize a WLAN module. The following uses the Hi3881 WLAN chip as an example: 254 2551. Set parameters for the WLAN module based on hardware attributes. 256 257``` 258/* Set parameters in the wlan_platform.hcs file based on hardware attributes. The following is an example of the WLAN platform configuration. */ 259hisi :& deviceList { 260 device0 :: deviceInst { 261 deviceInstId = 0; 262 powers { 263 power0 { 264 powerSeqDelay = 0; /* Power supply sequencing delay */ 265 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). */ 266 gpioId = 1; /* GPIO pin ID */ 267 activeLevel=1; /* Active level. Value 0 indicates a low level, and value 1 indicates a high level. */ 268 } 269 power1 { 270 powerSeqDelay = 0; /* Power supply sequencing delay */ 271 powerType = 0; /* Power supply type. Value 0 indicates that the power supply is always on, and value 1 indicates power supply through GPIO. */ 272 } 273 } 274 reset { 275 resetType = 0; /* Reset type. Value 0 indicates that reset is not supported, and value 1 indicates reset through GPIO. */ 276 gpioId = 2; /* GPIO pin ID */ 277 activeLevel=1; /* Active level. Value 0 indicates a low level, and value 1 indicates a high level. */ 278 resetHoldTime = 30; /* Hold time (ms) for a reset */ 279 } 280 bootUpTimeout = 30; /* Boot timeout duration (ms) */ 281 bus { 282 busType = 0; /* Bus type. Value 0 indicates secure digital input/output (SDIO). */ 283 busId = 2; /* Bus ID */ 284 funcNum = [1]; /* SDIO function number */ 285 timeout = 1000; /* Timeout duration for data read/write */ 286 blockSize = 512; /* Size of the data block to read or write */ 287 } 288 } 289} 290/* 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. */ 291root { 292 wlan_config { 293 hi3881 :& chipList { 294 chipHi3881 :: chipInst { 295 match_attr = "hdf_wlan_chips_hi3881"; /* Match attribute */ 296 chipName = "hi3881"; /* WLAN chip name */ 297 sdio { 298 vendorId = 0x0296; /* Vendor ID */ 299 deviceId = [0x5347]; /* Device ID */ 300 } 301 } 302 } 303 } 304} 305``` 306 3072. Mount the **init** and **deinit** functions of the WLAN chip and WLAN chip driver. 308 309``` 310/* WLAN module initialization and mount process */ 311#include "hdf_device_desc.h" 312#include "hdf_wifi_product.h" 313#include "hdf_log.h" 314#include "osal_mem.h" 315#include "hdf_wlan_chipdriver_manager.h" 316#include "securec.h" 317#include "wifi_module.h" 318#include "hi_wifi_api.h" 319#include "hi_types_base.h" 320 321#define HDF_LOG_TAG Hi3881Driver 322 323/* Functions for initializing and deinitializing the WLAN chip */ 324int32_t InitHi3881Chip(struct HdfWlanDevice *device); 325int32_t DeinitHi3881Chip(struct HdfWlanDevice *device); 326/* Functions for initializing and deinitializing the WLAN chip driver */ 327int32_t Hi3881Deinit(struct HdfChipDriver* chipDriver, struct NetDevice *netDevice); 328int32_t Hi3881Init(struct HdfChipDriver* chipDriver, struct NetDevice *netDevice); 329 330/* Initialize mac80211 and mount functions of the chip. */ 331hi_void HiMac80211Init(struct HdfChipDriver *chipDriver); 332 333static const char* const HI3881_DRIVER_NAME = "hisi"; 334 335/* Mount the WLAN chip driver and the functions of mac80211 and the chip. */ 336static struct HdfChipDriver *BuildHi3881Driver(struct HdfWlanDevice *device, uint8_t ifIndex) 337{ 338 struct HdfChipDriver *specificDriver = NULL; 339 if (device == NULL) { 340 HDF_LOGE("%s fail : channel is NULL", __func__); 341 return NULL; 342 } 343 (void)device; 344 (void)ifIndex; 345 specificDriver = (struct HdfChipDriver *)OsalMemCalloc(sizeof(struct HdfChipDriver)); 346 if (specificDriver == NULL) { 347 HDF_LOGE("%s fail: OsalMemCalloc fail!", __func__); 348 return NULL; 349 } 350 if (memset_s(specificDriver, sizeof(struct HdfChipDriver), 0, sizeof(struct HdfChipDriver)) != EOK) { 351 HDF_LOGE("%s fail: memset_s fail!", __func__); 352 OsalMemFree(specificDriver); 353 return NULL; 354 } 355 356 if (strcpy_s(specificDriver->name, MAX_WIFI_COMPONENT_NAME_LEN, HI3881_DRIVER_NAME) != EOK) { 357 HDF_LOGE("%s fail : strcpy_s fail", __func__); 358 OsalMemFree(specificDriver); 359 return NULL; 360 } 361 specificDriver->init = Hi3881Init; 362 specificDriver->deinit = Hi3881Deinit; 363 364 HiMac80211Init(specificDriver); 365 366 return specificDriver; 367} 368 369/* Release the WLAN chip driver. */ 370static void ReleaseHi3881Driver(struct HdfChipDriver *chipDriver) 371{ 372 if (chipDriver == NULL) { 373 return; 374 } 375 if (strcmp(chipDriver->name, HI3881_DRIVER_NAME) != 0) { 376 HDF_LOGE("%s:Not my driver!", __func__); 377 return; 378 } 379 OsalMemFree(chipDriver); 380} 381 382static uint8_t GetHi3881GetMaxIFCount(struct HdfChipDriverFactory *factory) { 383 (void)factory; 384 return 1; 385} 386 387/* Register functions related to the WLAN chip. */ 388static int32_t HDFWlanRegHisiDriverFactory(void) 389{ 390 static struct HdfChipDriverFactory tmpFactory = { 0 }; 391 struct HdfChipDriverManager *driverMgr = NULL; 392 driverMgr = HdfWlanGetChipDriverMgr(); 393 if (driverMgr == NULL && driverMgr->RegChipDriver != NULL) { 394 HDF_LOGE("%s fail: driverMgr is NULL!", __func__); 395 return HDF_FAILURE; 396 } 397 tmpFactory.driverName = HI3881_DRIVER_NAME; 398 tmpFactory.GetMaxIFCount = GetHi3881GetMaxIFCount; 399 tmpFactory.InitChip = InitHi3881Chip; 400 tmpFactory.DeinitChip = DeinitHi3881Chip; 401 tmpFactory.Build = BuildHi3881Driver; 402 tmpFactory.Release = ReleaseHi3881Driver; 403 tmpFactory.ReleaseFactory = NULL; 404 if (driverMgr->RegChipDriver(&tmpFactory) != HDF_SUCCESS) { 405 HDF_LOGE("%s fail: driverMgr is NULL!", __func__); 406 return HDF_FAILURE; 407 } 408 409 return HDF_SUCCESS; 410} 411 412static int32_t HdfWlanHisiChipDriverInit(struct HdfDeviceObject *device) 413{ 414 (void)device; 415 return HDFWlanRegHisiDriverFactory(); 416} 417 418struct HdfDriverEntry g_hdfHisiChipEntry = { 419 .moduleVersion = 1, 420 .Init = HdfWlanHisiChipDriverInit, 421 .moduleName = "HDF_WLAN_CHIPS" 422}; 423 424HDF_INIT(g_hdfHisiChipEntry); 425``` 426 427``` 428#include "hdf_wifi_product.h" 429#include "hi_wifi_api.h" 430#if (_PRE_OS_VERSION_LINUX == _PRE_OS_VERSION) 431#include "oal_thread.h" 432#include "osal_time.h" 433#endif 434#include "wifi_mac80211_ops.h" 435#include "wal_cfg80211.h" 436#include "net_adpater.h" 437#include "hdf_wlan_utils.h" 438 439#define HDF_LOG_TAG Hi3881Driver 440 441/* Initialize the WLAN chip. */ 442int32_t InitHi3881Chip(struct HdfWlanDevice *device) 443{ 444 uint8_t maxPortCount = 1; 445 int32_t ret = HI_SUCCESS; 446 uint8_t maxRetryCount = 2; 447 if (device == NULL) { 448 HDF_LOGE("%s:NULL ptr!", __func__); 449 return HI_FAIL; 450 } 451 452 do { 453 if (ret != HI_SUCCESS) { 454 if (device->reset != NULL && device->reset->Reset != NULL) { 455 device->reset->Reset(device->reset); 456 } 457 HDF_LOGE("%s:Retry init hi3881!last ret=%d", __func__, ret); 458 } 459 ret = hi_wifi_init(maxPortCount); 460 } while (ret != 0 && --maxRetryCount > 0); 461 462 if (ret != 0) { 463 HDF_LOGE("%s:Init hi3881 driver failed!", __func__); 464 return ret; 465 } 466 return HI_SUCCESS; 467} 468 469/* Deinitialize the WLAN chip. */ 470int32_t DeinitHi3881Chip(struct HdfWlanDevice *device) 471{ 472 (void)device; 473 int32_t ret = hi_wifi_deinit(); 474 if (ret != 0) { 475 HDF_LOGE("%s:Deinit failed!ret=%d", __func__, ret); 476 } 477 return ret; 478} 479 480/* Initialize the WLAN chip driver. */ 481int32_t Hi3881Init(struct HdfChipDriver *chipDriver, struct NetDevice *netDevice) 482{ 483 HDF_LOGI("%s: start...", __func__); 484 hi_u16 mode = wal_get_vap_mode(); 485 int32_t ret; 486 nl80211_iftype_uint8 type; 487 (void)chipDriver; 488 489 if (mode >= WAL_WIFI_MODE_BUTT) { 490 oam_error_log1(0, 0, "wal_init_drv_netdev:: invalid mode[%d]", mode); 491 return HI_FAIL; 492 } 493 494 if (mode == WAL_WIFI_MODE_STA) { 495 type = NL80211_IFTYPE_STATION; 496 } else if (mode == WAL_WIFI_MODE_AP) { 497 type = NL80211_IFTYPE_AP; 498 } else { 499 oam_error_log1(0, 0, "wal_init_drv_netdev:: invalid mode[%d]", mode); 500 return HI_FAIL; 501 } 502 503 ret = wal_init_drv_wlan_netdev(type, WAL_PHY_MODE_11N, netDevice); 504 if (ret != HI_SUCCESS) { 505 oam_error_log2(0, OAM_SF_ANY, "wal_init_drv_netdev %s failed.l_return:%d\n", netDevice->name, ret); 506 } 507 return ret; 508} 509 510/* Deinitialize the WLAN chip driver. */ 511int32_t Hi3881Deinit(struct HdfChipDriver *chipDriver, struct NetDevice *netDevice) 512{ 513 (void)chipDriver; 514 int32_t ret = wal_deinit_drv_wlan_netdev(netDevice); 515 if (ret != HDF_SUCCESS) { 516 return ret; 517 } 518 return ReleasePlatformNetDevice(netDevice); 519} 520``` 521 5223. 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**. 523 524``` 525hi_s32 wal_init_drv_wlan_netdev(nl80211_iftype_uint8 type, wal_phy_mode mode, hi_char* ifname, hi_u32* len) 526{ 527 oal_net_device_stru *netdev = HI_NULL; 528 529 ...... 530 /* Initialize the network device and obtain the initialized instance. */ 531 netdev = NetDeviceInit(ifname, *len, LITE_OS); 532 oal_wireless_dev *wdev = (oal_wireless_dev *)oal_mem_alloc(OAL_MEM_POOL_ID_LOCAL, sizeof(oal_wireless_dev)); 533 ret = wal_init_netif(type, netdev, wdev); 534 535 ...... 536 537 return HI_SUCCESS; 538} 539/* Mount some function pointers of NetDeviceInterFace. */ 540oal_net_device_ops_stru g_wal_net_dev_ops = 541{ 542 .getStats = wal_netdev_get_stats, 543 .open = wal_netdev_open, 544 .stop = wal_netdev_stop, 545 .xmit = hmac_bridge_vap_xmit, 546 .ioctl = wal_net_device_ioctl, 547 .changeMtu = oal_net_device_change_mtu, 548 .init = oal_net_device_init, 549 .deInit = oal_net_free_netdev, 550#if (defined(_PRE_WLAN_FEATURE_FLOWCTL) || defined(_PRE_WLAN_FEATURE_OFFLOAD_FLOWCTL)) 551 .selectQueue = wal_netdev_select_queue, 552#endif 553 .setMacAddr = wal_netdev_set_mac_addr, 554#if (_PRE_OS_VERSION_LITEOS == _PRE_OS_VERSION) 555 .netifNotify = HI_NULL, 556#endif 557 .specialEtherTypeProcess = SpecialEtherTypeProcess, 558}; 559 560hi_s32 wal_init_netif(nl80211_iftype_uint8 type, oal_net_device_stru *netdev, const oal_wireless_dev *wdev) 561{ 562 /* Add the network device to a protocol stack. */ 563 hi_u32 ret = NetDeviceAdd(netdev, (Protocol80211IfType)type); 564 565 ...... 566 567 return HI_SUCCESS; 568} 569``` 570 5714. Implement functions of **WifiMac80211Ops**. 572 573``` 574/* Mount some function pointers of mac80211. */ 575 576/* MAC-layer APIs for basic capabilities that need to be implemented by the driver */ 577static struct HdfMac80211BaseOps g_baseOps = { 578 .SetMode = WalSetMode, 579 .AddKey = WalAddKey, 580 .DelKey = WalDelKey, 581 .SetDefaultKey = WalSetDefaultKey, 582 .GetDeviceMacAddr = WalGetDeviceMacAddr, 583 .SetMacAddr = WalSetMacAddr, 584 .SetTxPower = WalSetTxPower, 585 .GetValidFreqsWithBand = WalGetValidFreqsWithBand, 586 .GetHwCapability = WalGetHwCapability 587}; 588 589/* MAC-layer APIs for station capabilities that need to be implemented by the driver */ 590static struct HdfMac80211STAOps g_staOps = { 591 .Connect = WalConnect, 592 .Disconnect = WalDisconnect, 593 .StartScan = WalStartScan, 594 .AbortScan = WalAbortScan, 595 .SetScanningMacAddress = WalSetScanningMacAddress, 596}; 597 598/* MAC-layer APIs for AP capabilities that need to be implemented by the driver */ 599static struct HdfMac80211APOps g_apOps = { 600 .ConfigAp = WalConfigAp, 601 .StartAp = WalStartAp, 602 .StopAp = WalStopAp, 603 .ConfigBeacon = WalChangeBeacon, 604 .DelStation = WalDelStation, 605 .SetCountryCode = WalSetCountryCode, 606 .GetAssociatedStasCount = WalGetAssociatedStasCount, 607 .GetAssociatedStasInfo = WalGetAssociatedStasInfo 608}; 609 610/* Initialize mac80211 and mount functions of the chip. */ 611hi_void HiMac80211Init(struct HdfChipDriver *chipDriver) 612{ 613 if (chipDriver == NULL) { 614 oam_error_log(0, OAM_SF_ANY, "%s:input is NULL!", __func__); 615 return; 616 } 617 chipDriver->ops = &g_baseOps; 618 chipDriver->staOps = &g_staOps; 619 chipDriver->apOps = &g_apOps; 620} 621``` 622 623