1# Drivers<a name="EN-US_TOPIC_0000001096875497"></a> 2 3- [Introduction](#section7301810197) 4- [Directory Structure](#section161941989596) 5- [Constraints](#section119744591305) 6 - [Available APIs](#section1551164914237) 7 - [Usage](#section129654513264) 8 9- [Repositories Involved](#section1371113476307) 10 11## Introduction<a name="section7301810197"></a> 12 13A kernel driver acts as a bridge between software and hardware. It allows accessing hardware resources of the OpenHarmony kernel through file system APIs, providing a mode of communication between the user space and kernel space and between processes. The main kernel drivers include **mem**, **random**, **video**, **quickstart**, **hievent**, and **tzdriver**. The preceding four types of drivers are stored in the **kernel/liteos\_a/drivers/char** directory, and the last two types **hievent** and **tzdriver** are stored in the **drivers/liteos** directory. Each type of driver represents a capability. You can select a driver as required to transmit data. The following figure shows the kernel driver architecture. 14 15**Figure 1** Kernel driver architecture<a name="fig2996151913212"></a> 16![](figures/kernel-driver-architecture.png "kernel-driver-architecture") 17 18Wherein: 19 20- **mem** indicates the driver for accessing physical input/output \(I/O\) devices in user space. It is used together with the **mmap** function. 21- **random** indicates the driver for devices to obtain random numbers, including true random number generators \(TRNGs\) and pseudorandom number generators \(PRNGs\). The device nodes for a TRNG and a PRNG are **/dev/random** and **/dev/urandom**, respectively. Comparatively, TRNGs provide a higher randomness. 22- **video** indicates the framebuffer driver framework. You need to initialize APIs, register the framebuffer driver, and operate the framebuffer driver through file system APIs. 23 24## Directory Structure<a name="section161941989596"></a> 25 26``` 27/drivers/liteos 28├── hievent # Driver for event log management 29├── include # Header files exposed externally 30├── tzdriver # Used for switching and communication between the rich execution environment (REE) and trusted execution environment (TEE) and provides device nodes that can be accessed from the application layer. 31``` 32 33## Constraints<a name="section119744591305"></a> 34 35Since the **tzdriver** implementation is not completely open-source, third-party vendors need to obtain the support for it through cooperation. 36 37The hievent-related capabilities are not supported currently and will be opened in the future. In addition, do not modify APIs and the format of their parameters defined in the **hievent** folder. 38 39The video-related source code is stored in the **video/fb.c** and **video/fb.h** files under the **third\_party/NuttX** directory. 40 41### Available APIs<a name="section1551164914237"></a> 42 43As shown in the preceding kernel driver architecture, a kernel driver can be taken as a special file. You can call standard file system APIs, such as **open**, **close**, **read**, **write**, and **ioctl** to perform operations on the driver. The following lists related APIs: 44 45- fb\_register 46 47 **Function prototype:** 48 49 int fb\_register\(int display, int plane\); 50 51 **Function description:** Loads the framebuffer driver and registers the **/dev/fb0** device node. If the operation is successful, **0** is returned. Otherwise, an error code is returned. 52 53 **Parameter description** 54 55 <a name="table13709103919318"></a> 56 <table><thead align="left"><tr id="row1170923910316"><th class="cellrowborder" valign="top" width="11.92%" id="mcps1.1.3.1.1"><p id="p1709123911313"><a name="p1709123911313"></a><a name="p1709123911313"></a>Parameter</p> 57 </th> 58 <th class="cellrowborder" valign="top" width="88.08%" id="mcps1.1.3.1.2"><p id="p1970910395313"><a name="p1970910395313"></a><a name="p1970910395313"></a>Description</p> 59 </th> 60 </tr> 61 </thead> 62 <tbody><tr id="row7709113923117"><td class="cellrowborder" valign="top" width="11.92%" headers="mcps1.1.3.1.1 "><p id="p1870983993114"><a name="p1870983993114"></a><a name="p1870983993114"></a>display</p> 63 </td> 64 <td class="cellrowborder" valign="top" width="88.08%" headers="mcps1.1.3.1.2 "><p id="p870963913111"><a name="p870963913111"></a><a name="p870963913111"></a>Indicates the display layer number. The value is usually <strong id="b832218348241"><a name="b832218348241"></a><a name="b832218348241"></a>0</strong>. If the hardware supports multiple display layers, this parameter can be set to other values.</p> 65 </td> 66 </tr> 67 <tr id="row0709163973120"><td class="cellrowborder" valign="top" width="11.92%" headers="mcps1.1.3.1.1 "><p id="p170993918319"><a name="p170993918319"></a><a name="p170993918319"></a>plane</p> 68 </td> 69 <td class="cellrowborder" valign="top" width="88.08%" headers="mcps1.1.3.1.2 "><p id="p9294182194420"><a name="p9294182194420"></a><a name="p9294182194420"></a>Indicates the color plane. The value is usually <strong id="b135661135142019"><a name="b135661135142019"></a><a name="b135661135142019"></a>0</strong>.</p> 70 </td> 71 </tr> 72 </tbody> 73 </table> 74 75 76- fb\_unregister 77 78 **Function prototype:** 79 80 int fb\_unregister\(int display\); 81 82 **Function description:** Uninstalls the framebuffer driver. If the operation is successful, **0** is returned. Otherwise, an error code is returned. 83 84 **Parameter description** 85 86 <a name="table1616493545014"></a> 87 <table><thead align="left"><tr id="row181651335105012"><th class="cellrowborder" valign="top" width="11.92%" id="mcps1.1.3.1.1"><p id="p4165123514507"><a name="p4165123514507"></a><a name="p4165123514507"></a>Parameter</p> 88 </th> 89 <th class="cellrowborder" valign="top" width="88.08%" id="mcps1.1.3.1.2"><p id="p6165103514502"><a name="p6165103514502"></a><a name="p6165103514502"></a>Description</p> 90 </th> 91 </tr> 92 </thead> 93 <tbody><tr id="row71650351503"><td class="cellrowborder" valign="top" width="11.92%" headers="mcps1.1.3.1.1 "><p id="p11651835145015"><a name="p11651835145015"></a><a name="p11651835145015"></a>display</p> 94 </td> 95 <td class="cellrowborder" valign="top" width="88.08%" headers="mcps1.1.3.1.2 "><p id="p16165183519506"><a name="p16165183519506"></a><a name="p16165183519506"></a>Indicates the display layer number.</p> 96 </td> 97 </tr> 98 </tbody> 99 </table> 100 101 102- up\_fbinitialize 103 104 **Function prototype:** 105 106 int up\_fbinitialize\(int display\); 107 108 **Function description:** Initializes the framebuffer driver. This function needs your implementation by calling **fb\_register** to register a device node for providing the framebuffer hardware driver capability. If the operation is successful, **0** is returned. Otherwise, an error code is returned. 109 110 **Parameter description** 111 112 <a name="table6333115110543"></a> 113 <table><thead align="left"><tr id="row1333315117540"><th class="cellrowborder" valign="top" width="11.92%" id="mcps1.1.3.1.1"><p id="p1233320514548"><a name="p1233320514548"></a><a name="p1233320514548"></a>Parameter</p> 114 </th> 115 <th class="cellrowborder" valign="top" width="88.08%" id="mcps1.1.3.1.2"><p id="p12334125165413"><a name="p12334125165413"></a><a name="p12334125165413"></a>Description</p> 116 </th> 117 </tr> 118 </thead> 119 <tbody><tr id="row16334155113548"><td class="cellrowborder" valign="top" width="11.92%" headers="mcps1.1.3.1.1 "><p id="p18334145115546"><a name="p18334145115546"></a><a name="p18334145115546"></a>display</p> 120 </td> 121 <td class="cellrowborder" valign="top" width="88.08%" headers="mcps1.1.3.1.2 "><p id="p15334951125417"><a name="p15334951125417"></a><a name="p15334951125417"></a>Indicates the display layer number.</p> 122 </td> 123 </tr> 124 </tbody> 125 </table> 126 127 128- up\_fbuninitialize 129 130 **Function prototype:** 131 132 void up\_fbuninitialize\(int display\); 133 134 **Function description:** Uninstalls the framebuffer driver. This function needs your implementation by calling **fb\_unregister** to unregister the device node. No value is returned for this function. 135 136 **Parameter description** 137 138 <a name="table18929574553"></a> 139 <table><thead align="left"><tr id="row1993012716553"><th class="cellrowborder" valign="top" width="11.92%" id="mcps1.1.3.1.1"><p id="p1593012795510"><a name="p1593012795510"></a><a name="p1593012795510"></a>Parameter</p> 140 </th> 141 <th class="cellrowborder" valign="top" width="88.08%" id="mcps1.1.3.1.2"><p id="p1693014715516"><a name="p1693014715516"></a><a name="p1693014715516"></a>Description</p> 142 </th> 143 </tr> 144 </thead> 145 <tbody><tr id="row4930076554"><td class="cellrowborder" valign="top" width="11.92%" headers="mcps1.1.3.1.1 "><p id="p18930147105517"><a name="p18930147105517"></a><a name="p18930147105517"></a>display</p> 146 </td> 147 <td class="cellrowborder" valign="top" width="88.08%" headers="mcps1.1.3.1.2 "><p id="p89309705512"><a name="p89309705512"></a><a name="p89309705512"></a>Indicates the display layer number.</p> 148 </td> 149 </tr> 150 </tbody> 151 </table> 152 153 154- up\_fbgetvplane 155 156 **Function prototype:** 157 158 struct fb\_vtable\_s \*up\_fbgetvplane\(int display, int vplane\); 159 160 **Function description:** Obtains a framebuffer driver handle, which needs your implementation. If the operation is successful, a valid pointer is returned. Otherwise, **NULL** is returned. 161 162 **Parameter description** 163 164 <a name="table0350124169"></a> 165 <table><thead align="left"><tr id="row183519416611"><th class="cellrowborder" valign="top" width="11.92%" id="mcps1.1.3.1.1"><p id="p7351184160"><a name="p7351184160"></a><a name="p7351184160"></a>Parameter</p> 166 </th> 167 <th class="cellrowborder" valign="top" width="88.08%" id="mcps1.1.3.1.2"><p id="p18351347614"><a name="p18351347614"></a><a name="p18351347614"></a>Description</p> 168 </th> 169 </tr> 170 </thead> 171 <tbody><tr id="row8351174461"><td class="cellrowborder" valign="top" width="11.92%" headers="mcps1.1.3.1.1 "><p id="p143519414618"><a name="p143519414618"></a><a name="p143519414618"></a>display</p> 172 </td> 173 <td class="cellrowborder" valign="top" width="88.08%" headers="mcps1.1.3.1.2 "><p id="p1035110419612"><a name="p1035110419612"></a><a name="p1035110419612"></a>Indicates the display layer number.</p> 174 </td> 175 </tr> 176 <tr id="row187413238610"><td class="cellrowborder" valign="top" width="11.92%" headers="mcps1.1.3.1.1 "><p id="p118752231869"><a name="p118752231869"></a><a name="p118752231869"></a>vplane</p> 177 </td> 178 <td class="cellrowborder" valign="top" width="88.08%" headers="mcps1.1.3.1.2 "><p id="p16875102311611"><a name="p16875102311611"></a><a name="p16875102311611"></a>Indicates the specified color plane.</p> 179 </td> 180 </tr> 181 </tbody> 182 </table> 183 184 185- pse\_ran\_dev\_register 186 187 **Function prototype:** 188 189 int pse\_ran\_dev\_register\(void\); 190 191 **Function description:** Initializes the PRNG device driver and registers the **/dev/random** device node. If the operation is successful, **0** is returned. Otherwise, an error code is returned. 192 193 **Parameter description:** none 194 195 196- ran\_dev\_register 197 198 **Function prototype:** 199 200 int ran\_dev\_register\(void\); 201 202 **Function description:** Initializes the TRNG driver and registers the **/dev/urandom** device node. If the operation is successful, **0** is returned. Otherwise, an error code is returned. 203 204 **Parameter description:** none 205 206 207- mem\_dev\_register 208 209 **Function prototype:** 210 211 int mem\_dev\_register\(void\); 212 213 **Function description:** Initializes the mem driver and registers the **/dev/mem** device node. If the operation is successful, **0** is returned. Otherwise, an error code is returned. 214 215 **Parameter description:** none 216 217 218### Usage<a name="section129654513264"></a> 219 220- The following uses the framebuffer driver as an example: 221 222``` 223int up_fbinitialize(int display) 224{ 225 // Provide framebuffer hardware driver capabilities. The specific code logic needs your implementation. 226} 227 228void up_fbuninitialize(int display) 229{ 230 // This function is used together with up_fbinitialize. The specific code logic needs your implementation. 231} 232 233struct fb_vtable_s *up_fbgetvplane(int display, int vplane) 234{ 235 // Register a hardware driver based on information described by the fb_vtable_s structure. You can use the driver capabilities through the video framework layer. The specific code logic needs your implementation. 236} 237 238int FrameBufferFunc(void) 239{ 240 int ret; 241 int fd = -1; 242 struct hifb_info info; 243 char *pShowScreen = NULL; 244 245 ret = fb_register(0, 0); // Register the /dev/fb0 device node. This function will call the preceding functions to enable hardware driver capabilities. 246 if (ret != 0) { 247 return -1; 248 } 249 250 fd = open(file, O_RDWR, 0); 251 if (fd < 0) { 252 return -1; 253 } 254 255 if (ioctl(fd, FBIOGET_SCREENINFO_HIFB, &info) < 0) { 256 return -1; 257 } 258 info.vinfo.xres = 1920; 259 info.vinfo.yres = 1080; 260 info.oinfo.sarea.w = 1920; 261 info.oinfo.sarea.h = 1080; 262 info.oinfo.bpp = 16; 263 info.activate = 0; 264 info.vinfo.fmt = HIFB_FMT_ARGB1555; 265 266 if (ioctl(fd, FBIOPUT_SCREENINFO_HIFB, &info) < 0) { 267 return -1; 268 } 269 270 if (ioctl(fd, FBIOGET_SCREENINFO_HIFB, &info) < 0) { 271 return -1; 272 } 273 274 pShowScreen = mmap(HI_NULL, info.oinfo.fblen, PROT_READ | PROT_WRITE, MAP_SHARED, pstInfo->fd, 0); 275 if (pShowScreen == -1) { 276 return -1; 277 } 278 279 // Fill in the memory to which pShowScreen points and display the graphics through the ioctl call. 280 281 munmap(pShowScreen, info.oinfo.fblen); 282 283 close(fd); 284 285 ret = fb_unregister(0); 286 if (ret != 0) { 287 return -1; 288 } 289} 290``` 291 292## Repositories Involved<a name="section1371113476307"></a> 293 294[Kernel subsystem](https://gitee.com/openharmony/docs/blob/HEAD/en/readme/kernel.md) 295 296[drivers\_liteos](https://gitee.com/openharmony/drivers_liteos/blob/master/README.md) 297 298[kernel\_liteos\_a](https://gitee.com/openharmony/kernel_liteos_a/blob/HEAD/README.md) 299 300