18c2ecf20Sopenharmony_ci.. SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci 38c2ecf20Sopenharmony_ci=============== 48c2ecf20Sopenharmony_ciConsole Drivers 58c2ecf20Sopenharmony_ci=============== 68c2ecf20Sopenharmony_ci 78c2ecf20Sopenharmony_ciThe Linux kernel has 2 general types of console drivers. The first type is 88c2ecf20Sopenharmony_ciassigned by the kernel to all the virtual consoles during the boot process. 98c2ecf20Sopenharmony_ciThis type will be called 'system driver', and only one system driver is allowed 108c2ecf20Sopenharmony_cito exist. The system driver is persistent and it can never be unloaded, though 118c2ecf20Sopenharmony_ciit may become inactive. 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_ciThe second type has to be explicitly loaded and unloaded. This will be called 148c2ecf20Sopenharmony_ci'modular driver' by this document. Multiple modular drivers can coexist at 158c2ecf20Sopenharmony_ciany time with each driver sharing the console with other drivers including 168c2ecf20Sopenharmony_cithe system driver. However, modular drivers cannot take over the console 178c2ecf20Sopenharmony_cithat is currently occupied by another modular driver. (Exception: Drivers that 188c2ecf20Sopenharmony_cicall do_take_over_console() will succeed in the takeover regardless of the type 198c2ecf20Sopenharmony_ciof driver occupying the consoles.) They can only take over the console that is 208c2ecf20Sopenharmony_cioccupied by the system driver. In the same token, if the modular driver is 218c2ecf20Sopenharmony_cireleased by the console, the system driver will take over. 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ciModular drivers, from the programmer's point of view, have to call:: 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_ci do_take_over_console() - load and bind driver to console layer 268c2ecf20Sopenharmony_ci give_up_console() - unload driver; it will only work if driver 278c2ecf20Sopenharmony_ci is fully unbound 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_ciIn newer kernels, the following are also available:: 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ci do_register_con_driver() 328c2ecf20Sopenharmony_ci do_unregister_con_driver() 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_ciIf sysfs is enabled, the contents of /sys/class/vtconsole can be 358c2ecf20Sopenharmony_ciexamined. This shows the console backends currently registered by the 368c2ecf20Sopenharmony_cisystem which are named vtcon<n> where <n> is an integer from 0 to 15. 378c2ecf20Sopenharmony_ciThus:: 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ci ls /sys/class/vtconsole 408c2ecf20Sopenharmony_ci . .. vtcon0 vtcon1 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_ciEach directory in /sys/class/vtconsole has 3 files:: 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ci ls /sys/class/vtconsole/vtcon0 458c2ecf20Sopenharmony_ci . .. bind name uevent 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ciWhat do these files signify? 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_ci 1. bind - this is a read/write file. It shows the status of the driver if 508c2ecf20Sopenharmony_ci read, or acts to bind or unbind the driver to the virtual consoles 518c2ecf20Sopenharmony_ci when written to. The possible values are: 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_ci 0 548c2ecf20Sopenharmony_ci - means the driver is not bound and if echo'ed, commands the driver 558c2ecf20Sopenharmony_ci to unbind 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ci 1 588c2ecf20Sopenharmony_ci - means the driver is bound and if echo'ed, commands the driver to 598c2ecf20Sopenharmony_ci bind 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_ci 2. name - read-only file. Shows the name of the driver in this format:: 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ci cat /sys/class/vtconsole/vtcon0/name 648c2ecf20Sopenharmony_ci (S) VGA+ 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ci '(S)' stands for a (S)ystem driver, i.e., it cannot be directly 678c2ecf20Sopenharmony_ci commanded to bind or unbind 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_ci 'VGA+' is the name of the driver 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci cat /sys/class/vtconsole/vtcon1/name 728c2ecf20Sopenharmony_ci (M) frame buffer device 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_ci In this case, '(M)' stands for a (M)odular driver, one that can be 758c2ecf20Sopenharmony_ci directly commanded to bind or unbind. 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci 3. uevent - ignore this file 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_ciWhen unbinding, the modular driver is detached first, and then the system 808c2ecf20Sopenharmony_cidriver takes over the consoles vacated by the driver. Binding, on the other 818c2ecf20Sopenharmony_cihand, will bind the driver to the consoles that are currently occupied by a 828c2ecf20Sopenharmony_cisystem driver. 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ciNOTE1: 858c2ecf20Sopenharmony_ci Binding and unbinding must be selected in Kconfig. It's under:: 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ci Device Drivers -> 888c2ecf20Sopenharmony_ci Character devices -> 898c2ecf20Sopenharmony_ci Support for binding and unbinding console drivers 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ciNOTE2: 928c2ecf20Sopenharmony_ci If any of the virtual consoles are in KD_GRAPHICS mode, then binding or 938c2ecf20Sopenharmony_ci unbinding will not succeed. An example of an application that sets the 948c2ecf20Sopenharmony_ci console to KD_GRAPHICS is X. 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_ciHow useful is this feature? This is very useful for console driver 978c2ecf20Sopenharmony_cidevelopers. By unbinding the driver from the console layer, one can unload the 988c2ecf20Sopenharmony_cidriver, make changes, recompile, reload and rebind the driver without any need 998c2ecf20Sopenharmony_cifor rebooting the kernel. For regular users who may want to switch from 1008c2ecf20Sopenharmony_ciframebuffer console to VGA console and vice versa, this feature also makes 1018c2ecf20Sopenharmony_cithis possible. (NOTE NOTE NOTE: Please read fbcon.txt under Documentation/fb 1028c2ecf20Sopenharmony_cifor more details.) 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_ciNotes for developers 1058c2ecf20Sopenharmony_ci==================== 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_cido_take_over_console() is now broken up into:: 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ci do_register_con_driver() 1108c2ecf20Sopenharmony_ci do_bind_con_driver() - private function 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_cigive_up_console() is a wrapper to do_unregister_con_driver(), and a driver must 1138c2ecf20Sopenharmony_cibe fully unbound for this call to succeed. con_is_bound() will check if the 1148c2ecf20Sopenharmony_cidriver is bound or not. 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ciGuidelines for console driver writers 1178c2ecf20Sopenharmony_ci===================================== 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ciIn order for binding to and unbinding from the console to properly work, 1208c2ecf20Sopenharmony_ciconsole drivers must follow these guidelines: 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_ci1. All drivers, except system drivers, must call either do_register_con_driver() 1238c2ecf20Sopenharmony_ci or do_take_over_console(). do_register_con_driver() will just add the driver 1248c2ecf20Sopenharmony_ci to the console's internal list. It won't take over the 1258c2ecf20Sopenharmony_ci console. do_take_over_console(), as it name implies, will also take over (or 1268c2ecf20Sopenharmony_ci bind to) the console. 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_ci2. All resources allocated during con->con_init() must be released in 1298c2ecf20Sopenharmony_ci con->con_deinit(). 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ci3. All resources allocated in con->con_startup() must be released when the 1328c2ecf20Sopenharmony_ci driver, which was previously bound, becomes unbound. The console layer 1338c2ecf20Sopenharmony_ci does not have a complementary call to con->con_startup() so it's up to the 1348c2ecf20Sopenharmony_ci driver to check when it's legal to release these resources. Calling 1358c2ecf20Sopenharmony_ci con_is_bound() in con->con_deinit() will help. If the call returned 1368c2ecf20Sopenharmony_ci false(), then it's safe to release the resources. This balance has to be 1378c2ecf20Sopenharmony_ci ensured because con->con_startup() can be called again when a request to 1388c2ecf20Sopenharmony_ci rebind the driver to the console arrives. 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ci4. Upon exit of the driver, ensure that the driver is totally unbound. If the 1418c2ecf20Sopenharmony_ci condition is satisfied, then the driver must call do_unregister_con_driver() 1428c2ecf20Sopenharmony_ci or give_up_console(). 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ci5. do_unregister_con_driver() can also be called on conditions which make it 1458c2ecf20Sopenharmony_ci impossible for the driver to service console requests. This can happen 1468c2ecf20Sopenharmony_ci with the framebuffer console that suddenly lost all of its drivers. 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ciThe current crop of console drivers should still work correctly, but binding 1498c2ecf20Sopenharmony_ciand unbinding them may cause problems. With minimal fixes, these drivers can 1508c2ecf20Sopenharmony_cibe made to work correctly. 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_ciAntonino Daplas <adaplas@pol.net> 153