1# Device Driver Porting 2 3This section describes how to port device drivers. 4 5## LCD Driver Porting 6 7To port an LCD driver, write the driver, create an instance of the corresponding model in the driver, and complete the registration. 8 9The LCD drivers are stored in **//drivers/hdf_core/framework/model/display/driver/panel**. 10 111. Create a panel driver. 12 13 Create an HDF driver and call the **RegisterPanel** method to register a model instance during driver initialization. 14 15 16 ``` 17 int32_t LCDxxEntryInit(struct HdfDeviceObject *object) 18 { 19 struct PanelData *panel = CreateYourPanel(); 20 // Register a model instance. 21 if (RegisterPanel(panel) != HDF_SUCCESS) { 22 HDF_LOGE("%s: RegisterPanel failed", __func__); 23 return HDF_FAILURE; 24 } 25 return HDF_SUCCESS; 26 } 27 28 struct HdfDriverEntry g_xxxxDevEntry = { 29 .moduleVersion = 1, 30 .moduleName = "LCD_XXXX", 31 .Init = LCDxxEntryInit, 32 }; 33 34 HDF_INIT(g_xxxxDevEntry); 35 ``` 36 372. Configure and load the panel driver. 38 39 Modify the source code file **//vendor/vendor_name/product_name/config/device_info/device_info.hcs**. Add configurations for the device named **device\_lcd** for the display host. 40 41 >  **CAUTION** 42 > Make sure the value of **moduleName** is the same as that of **moduleName** in the panel driver. 43 44 45 ``` 46 root { 47 ... 48 display :: host { 49 device_lcd :: device { 50 deviceN :: deviceNode { 51 policy = 0; 52 priority = 100; 53 preload = 2; 54 moduleName = "LCD_XXXX"; 55 } 56 } 57 } 58 } 59 ``` 60 61 62## Touchscreen Driver Porting 63 64This section describes how to port a touchscreen driver. The touchscreen drivers are stored in the source code directory **//drivers/hdf_core/framework/model/input/driver/touchscreen**. To port a touchscreen driver, register a **ChipDevice** model instance with the system. 65 66For details about how to develop a touchscreen driver, see [Touchscreen Development Guidelines](../driver/driver-peripherals-touch-des.md). 67 681. Create a touchscreen driver. 69 70 Create the **touch\_ic\_name.c** file in the **touchscreen** directory. Write the following content: 71 72 73 ``` 74 #include "hdf_touch.h" 75 76 static int32_t HdfXXXXChipInit(struct HdfDeviceObject *device) 77 { 78 ChipDevice *tpImpl = CreateXXXXTpImpl(); 79 if(RegisterChipDevice(tpImpl) != HDF_SUCCESS) { // Register the ChipDevice model instance. 80 ReleaseXXXXTpImpl(tpImpl); 81 return HDF_FAILURE; 82 } 83 return HDF_SUCCESS; 84 } 85 86 struct HdfDriverEntry g_touchXXXXChipEntry = { 87 .moduleVersion = 1, 88 .moduleName = "HDF_TOUCH_XXXX", // Make sure the value is the same as that in the subsequent configuration. 89 .Init = HdfXXXXChipInit, 90 }; 91 92 HDF_INIT(g_touchXXXXChipEntry); 93 ``` 94 95 The following methods need to be implemented in **ChipDevice**: 96 97 | Method | Description | 98 | -------- | -------- | 99 | int32_t (\*Init)(ChipDevice \*device) | Initializes the device. | 100 | int32_t (\*Detect)(ChipDevice \*device) | Detects the device. | 101 | int32_t (\*Suspend)(ChipDevice \*device) | Places the device in sleep mode. | 102 | int32_t (\*Resume)(ChipDevice \*device) | Wakes up the device. | 103 | int32_t (\*DataHandle)(ChipDevice \*device) | Reads data from the device and writes touch point data to device > driver > frameData. | 104 | int32_t (\*UpdateFirmware)(ChipDevice \*device) | Updates the firmware. | 105 1062. Configure the product and load the driver. 107 108 All device information of the product is defined in the source code file **//vendor/vendor_name/product_name/config/device_info/device_info.hcs**. Modify the file and add configurations to the **device** named **device\_touch\_chip** in the **host** of the **input** command. 109 110 >  **NOTE** 111 > 112 > Make sure the value of **moduleName** is the same as that of **moduleName** in the touchscreen driver. 113 114 115 ``` 116 deviceN :: deviceNode { 117 policy = 0; 118 priority = 130; 119 preload = 0; 120 permission = 0660; 121 moduleName = "HDF_TOUCH_XXXX"; 122 deviceMatchAttr = "touch_XXXX_configs"; 123 } 124 ``` 125 126 127## WLAN Driver Porting 128 129The WLAN driver is divided into two parts. One of the parts manages WLAN devices, and the other part manages WLAN traffic. 130 131**Figure 1** OpenHarmony WLAN driver architecture<a name="fig155920160203"></a> 132 133  134 135As shown in Figure 1, the part on the left manages WLAN devices, and the part on the right manages WLAN traffic. The HDF WLAN framework abstracts these two parts. The porting process of the driver can be considered as the implementation of the APIs required by the two parts. These APIs are described as follows: 136 137| API | Header File | Description | 138| -------- | -------- | -------- | 139| HdfChipDriverFactory | drivers\hdf_core\framework\include\wifi\hdf_wlan_chipdriver_manager.h | Factory of the ChipDriver, which is used to support multiple WLAN interfaces of a chip. | 140| HdfChipDriver | drivers\hdf_core\framework\include\wifi\wifi_module.h | Manages a specific WLAN interface. | 141| NetDeviceInterFace | drivers\hdf_core\framework\include\wifi\net_device.h | Communicates with the protocol stack, such as sending data and setting the status of network interfaces. | 142 143>  **NOTE** 144> 145> For details about the API development, see [WLAN Development Guidelines](../driver/driver-peripherals-external-des.md). 146 147The porting procedure is as follows: 148 1491. Create a WLAN chip driver. 150 151 Create the **hdf_wlan_chip_name.c** file in **/device/vendor_name/peripheral/wifi/chip_name/**. The sample code is as follows: 152 153 ``` 154 static int32_t HdfWlanXXXChipDriverInit(struct HdfDeviceObject *device) { 155 static struct HdfChipDriverFactory factory = CreateChipDriverFactory(); // Implement the method. 156 struct HdfChipDriverManager *driverMgr = HdfWlanGetChipDriverMgr(); 157 if (driverMgr->RegChipDriver(&factory) != HDF_SUCCESS) { // Register the driver factory. 158 HDF_LOGE("%s fail: driverMgr is NULL!", __func__); 159 return HDF_FAILURE; 160 } 161 return HDF_SUCCESS; 162 } 163 164 struct HdfDriverEntry g_hdfXXXChipEntry = { 165 .moduleVersion = 1, 166 .Init = HdfWlanXXXChipDriverInit, 167 .Release = HdfWlanXXXChipRelease, 168 .moduleName = "HDF_WIFI_CHIP_XXX" // Make sure the name is the same as the configured one. 169 }; 170 171 HDF_INIT(g_hdfXXXChipEntry); 172 ``` 173 174 In the **CreateChipDriverFactory** method, create an object of the **HdfChipDriverFactory** type. This object provides the following methods: 175 176 | Method | Description | 177 | -------- | -------- | 178 | const char \*driverName | Indicates the driver name. | 179 | int32_t (\*InitChip)(struct HdfWlanDevice \*device) | Initializes the chip. | 180 | int32_t (\*DeinitChip)(struct HdfWlanDevice \*device) | Deinitializes the chip. | 181 | void (\*ReleaseFactory)(struct HdfChipDriverFactory \*factory) | Releases the **HdfChipDriverFactory** object. | 182 | struct HdfChipDriver \*(\*Build)(struct HdfWlanDevice \*device, uint8_t ifIndex) | Creates an **HdfChipDriver**. In the input parameters, **device** indicates the device information, and **ifIndex** indicates the sequence number of this interface in the chip. | 183 | void (\*Release)(struct HdfChipDriver \*chipDriver) | Releases the chip driver. | 184 | uint8_t (\*GetMaxIFCount)(struct HdfChipDriverFactory \*factory) | Obtains the maximum number of interfaces supported by the current chip. | 185 186 The **Build** method creates an **HdfChipDriver** object that manages the specified network interface. This object needs to provide the following methods: 187 188 | Method | Description | 189 | -------- | -------- | 190 | int32_t (\*init)(struct HdfChipDriver \*chipDriver, NetDevice \*netDev) | Initializes the current network interface. The **NetDeviceInterFace** needs to be provided for the **netDev**. | 191 | int32_t (\*deinit)(struct HdfChipDriver \*chipDriver, NetDevice \*netDev) | Deinitializes the current network interface. | 192 | struct HdfMac80211BaseOps \*ops | Provides the WLAN basic capability interface set. | 193 | struct HdfMac80211STAOps \*staOps | Provides the interface set required for supporting the STA mode. | 194 | struct HdfMac80211APOps \*apOps | Provides the interface set required for supporting the AP mode. | 195 1962. Create a configuration file to describe the chips supported by the driver. 197 198 Create a chip configuration file in the product configuration directory and save it to the source code path **//vendor/vendor\_name/product\_name/config/wifi/wlan\_chip\_chip\_name.hcs**. 199 200 201 202 203 ``` 204 root { 205 wlan_config { 206 chip_name :& chipList { 207 chip_name :: chipInst { 208 match_attr = "hdf_wlan_chips_chip_name"; /* Indicates the configuration matching attribute, which is used to provide the configuration root of the driver. */ 209 driverName = "driverName"; /* Indicates the driver name, which must be the same as that of driverName in HdfChipDriverFactory.*/ 210 sdio { 211 vendorId = 0xXXXX; /* your vendor id */ 212 deviceId = [0xXXXX]; /*your supported devices */ 213 } 214 } 215 } 216 } 217 } 218 ``` 219 220 >  **NOTE** 221 > 222 > Replace the values of **vendor\_name**, **product\_name**, and **chip\_name** in the path and file with the actual names. 223 > 224 > Set **vendorId** and **deviceId** to the actual vendor ID and chip ID, respectively. 225 2263. Edit the configuration file and load the driver. 227 228 All device information of the product is defined in the source code file **//vendor/vendor\_name/product\_name/config/device\_info/device\_info.hcs**. Modify the file and add configurations to the **device** named **device\_wlan\_chips** in the **host** of the **network** command. The sample code is as follows: 229 230 231 ``` 232 deviceN :: deviceNode { 233 policy = 0; 234 preload = 2; 235 moduleName = "HDF_WLAN_CHIPS"; 236 deviceMatchAttr = "hdf_wlan_chips_chip_name"; 237 serviceName = "driverName"; 238 } 239 ``` 240 241 >  **NOTE** 242 > 243 > Make sure the value of **moduleName** is the same as that of **moduleName** in the WLAN driver. 244 2454. Modify the **Kconfig** file to make the ported WLAN driver appear in the kernel configuration. 246 247 Add configurations to **device/vendor\_name/drivers/Kconfig**. The sample code is as follows: 248 249 250 ``` 251 config DRIVERS_HDF_WIFI_chip_name 252 bool "Enable chip_name Host driver" 253 default n 254 depends on DRIVERS_HDF_WLAN help 255 Answer Y to enable chip_name Host driver. 256 ``` 257 258 >  **NOTE** 259 > 260 > Replace **chip\_name** with the actual chip name. 261 2625. Modify the build script to enable the driver to participate in the kernel build. 263 264 Add the following content to the end of the source code file **//device/vendor\_name/drivers/lite.mk**: 265 266 267 ``` 268 ifeq ($(LOSCFG_DRIVERS_HDF_WIFI_chip_name), y) 269 # After the build is complete, an object named hdf_wlan_chipdriver_chip_name needs to be linked. You are advised to use this name to prevent conflicts. 270 LITEOS_BASELIB += -lhdf_wlan_chipdriver_chip_name 271 # Add the build directory gpio. 272 LIB_SUBDIRS += ../peripheral/wifi/chip_name 273 endif 274 ``` 275 276 >  **NOTE** 277 > 278 > Replace **chip_name** with the actual chip name. 279 280 281