Antonino A. Daplas | 79062a0 | 2006-06-26 00:27:11 -0700 | [diff] [blame] | 1 | Console Drivers |
| 2 | =============== |
| 3 | |
Randy Dunlap | 3d83d31 | 2018-08-06 16:24:55 -0700 | [diff] [blame] | 4 | The Linux kernel has 2 general types of console drivers. The first type is |
Antonino A. Daplas | 79062a0 | 2006-06-26 00:27:11 -0700 | [diff] [blame] | 5 | assigned by the kernel to all the virtual consoles during the boot process. |
| 6 | This type will be called 'system driver', and only one system driver is allowed |
| 7 | to exist. The system driver is persistent and it can never be unloaded, though |
| 8 | it may become inactive. |
| 9 | |
| 10 | The second type has to be explicitly loaded and unloaded. This will be called |
| 11 | 'modular driver' by this document. Multiple modular drivers can coexist at |
| 12 | any time with each driver sharing the console with other drivers including |
| 13 | the system driver. However, modular drivers cannot take over the console |
| 14 | that is currently occupied by another modular driver. (Exception: Drivers that |
Wang YanQing | 6b5f146 | 2013-05-21 23:29:11 +0800 | [diff] [blame] | 15 | call do_take_over_console() will succeed in the takeover regardless of the type |
Antonino A. Daplas | 79062a0 | 2006-06-26 00:27:11 -0700 | [diff] [blame] | 16 | of driver occupying the consoles.) They can only take over the console that is |
| 17 | occupied by the system driver. In the same token, if the modular driver is |
| 18 | released by the console, the system driver will take over. |
| 19 | |
Randy Dunlap | 3d83d31 | 2018-08-06 16:24:55 -0700 | [diff] [blame] | 20 | Modular drivers, from the programmer's point of view, have to call: |
Antonino A. Daplas | 79062a0 | 2006-06-26 00:27:11 -0700 | [diff] [blame] | 21 | |
Wang YanQing | 6b5f146 | 2013-05-21 23:29:11 +0800 | [diff] [blame] | 22 | do_take_over_console() - load and bind driver to console layer |
Randy Dunlap | 3d83d31 | 2018-08-06 16:24:55 -0700 | [diff] [blame] | 23 | give_up_console() - unload driver; it will only work if driver |
| 24 | is fully unbound |
Antonino A. Daplas | 79062a0 | 2006-06-26 00:27:11 -0700 | [diff] [blame] | 25 | |
| 26 | In newer kernels, the following are also available: |
| 27 | |
Wang YanQing | 6b5f146 | 2013-05-21 23:29:11 +0800 | [diff] [blame] | 28 | do_register_con_driver() |
| 29 | do_unregister_con_driver() |
Antonino A. Daplas | 79062a0 | 2006-06-26 00:27:11 -0700 | [diff] [blame] | 30 | |
Antonino A. Daplas | 6690075 | 2006-06-26 00:27:14 -0700 | [diff] [blame] | 31 | If sysfs is enabled, the contents of /sys/class/vtconsole can be |
| 32 | examined. This shows the console backends currently registered by the |
Rolf Eike Beer | 4b8a8b8 | 2007-07-19 01:48:33 -0700 | [diff] [blame] | 33 | system which are named vtcon<n> where <n> is an integer from 0 to 15. Thus: |
Antonino A. Daplas | 79062a0 | 2006-06-26 00:27:11 -0700 | [diff] [blame] | 34 | |
Antonino A. Daplas | 6690075 | 2006-06-26 00:27:14 -0700 | [diff] [blame] | 35 | ls /sys/class/vtconsole |
| 36 | . .. vtcon0 vtcon1 |
Antonino A. Daplas | 79062a0 | 2006-06-26 00:27:11 -0700 | [diff] [blame] | 37 | |
Antonino A. Daplas | 6690075 | 2006-06-26 00:27:14 -0700 | [diff] [blame] | 38 | Each directory in /sys/class/vtconsole has 3 files: |
Antonino A. Daplas | 79062a0 | 2006-06-26 00:27:11 -0700 | [diff] [blame] | 39 | |
Antonino A. Daplas | 6690075 | 2006-06-26 00:27:14 -0700 | [diff] [blame] | 40 | ls /sys/class/vtconsole/vtcon0 |
| 41 | . .. bind name uevent |
Antonino A. Daplas | 79062a0 | 2006-06-26 00:27:11 -0700 | [diff] [blame] | 42 | |
Antonino A. Daplas | 6690075 | 2006-06-26 00:27:14 -0700 | [diff] [blame] | 43 | What do these files signify? |
Antonino A. Daplas | 79062a0 | 2006-06-26 00:27:11 -0700 | [diff] [blame] | 44 | |
Antonino A. Daplas | 6690075 | 2006-06-26 00:27:14 -0700 | [diff] [blame] | 45 | 1. bind - this is a read/write file. It shows the status of the driver if |
| 46 | read, or acts to bind or unbind the driver to the virtual consoles |
| 47 | when written to. The possible values are: |
Antonino A. Daplas | 79062a0 | 2006-06-26 00:27:11 -0700 | [diff] [blame] | 48 | |
Antonino A. Daplas | 6690075 | 2006-06-26 00:27:14 -0700 | [diff] [blame] | 49 | 0 - means the driver is not bound and if echo'ed, commands the driver |
| 50 | to unbind |
Antonino A. Daplas | 79062a0 | 2006-06-26 00:27:11 -0700 | [diff] [blame] | 51 | |
Antonino A. Daplas | 6690075 | 2006-06-26 00:27:14 -0700 | [diff] [blame] | 52 | 1 - means the driver is bound and if echo'ed, commands the driver to |
| 53 | bind |
Antonino A. Daplas | 79062a0 | 2006-06-26 00:27:11 -0700 | [diff] [blame] | 54 | |
Antonino A. Daplas | 6690075 | 2006-06-26 00:27:14 -0700 | [diff] [blame] | 55 | 2. name - read-only file. Shows the name of the driver in this format: |
| 56 | |
| 57 | cat /sys/class/vtconsole/vtcon0/name |
| 58 | (S) VGA+ |
| 59 | |
Randy Dunlap | 3d83d31 | 2018-08-06 16:24:55 -0700 | [diff] [blame] | 60 | '(S)' stands for a (S)ystem driver, i.e., it cannot be directly |
Antonino A. Daplas | 6690075 | 2006-06-26 00:27:14 -0700 | [diff] [blame] | 61 | commanded to bind or unbind |
| 62 | |
| 63 | 'VGA+' is the name of the driver |
| 64 | |
| 65 | cat /sys/class/vtconsole/vtcon1/name |
| 66 | (M) frame buffer device |
| 67 | |
| 68 | In this case, '(M)' stands for a (M)odular driver, one that can be |
| 69 | directly commanded to bind or unbind. |
| 70 | |
| 71 | 3. uevent - ignore this file |
Antonino A. Daplas | 79062a0 | 2006-06-26 00:27:11 -0700 | [diff] [blame] | 72 | |
| 73 | When unbinding, the modular driver is detached first, and then the system |
| 74 | driver takes over the consoles vacated by the driver. Binding, on the other |
| 75 | hand, will bind the driver to the consoles that are currently occupied by a |
| 76 | system driver. |
| 77 | |
Thadeu Lima de Souza Cascardo | a8ab010 | 2010-02-01 15:57:15 -0200 | [diff] [blame] | 78 | NOTE1: Binding and unbinding must be selected in Kconfig. It's under: |
Antonino A. Daplas | 6690075 | 2006-06-26 00:27:14 -0700 | [diff] [blame] | 79 | |
| 80 | Device Drivers -> Character devices -> Support for binding and unbinding |
| 81 | console drivers |
| 82 | |
| 83 | NOTE2: If any of the virtual consoles are in KD_GRAPHICS mode, then binding or |
| 84 | unbinding will not succeed. An example of an application that sets the console |
| 85 | to KD_GRAPHICS is X. |
| 86 | |
Antonino A. Daplas | 79062a0 | 2006-06-26 00:27:11 -0700 | [diff] [blame] | 87 | How useful is this feature? This is very useful for console driver |
| 88 | developers. By unbinding the driver from the console layer, one can unload the |
| 89 | driver, make changes, recompile, reload and rebind the driver without any need |
| 90 | for rebooting the kernel. For regular users who may want to switch from |
| 91 | framebuffer console to VGA console and vice versa, this feature also makes |
| 92 | this possible. (NOTE NOTE NOTE: Please read fbcon.txt under Documentation/fb |
Randy Dunlap | 3d83d31 | 2018-08-06 16:24:55 -0700 | [diff] [blame] | 93 | for more details.) |
Antonino A. Daplas | 79062a0 | 2006-06-26 00:27:11 -0700 | [diff] [blame] | 94 | |
| 95 | Notes for developers: |
| 96 | ===================== |
| 97 | |
Wang YanQing | 6b5f146 | 2013-05-21 23:29:11 +0800 | [diff] [blame] | 98 | do_take_over_console() is now broken up into: |
Antonino A. Daplas | 79062a0 | 2006-06-26 00:27:11 -0700 | [diff] [blame] | 99 | |
Wang YanQing | 6b5f146 | 2013-05-21 23:29:11 +0800 | [diff] [blame] | 100 | do_register_con_driver() |
| 101 | do_bind_con_driver() - private function |
Antonino A. Daplas | 79062a0 | 2006-06-26 00:27:11 -0700 | [diff] [blame] | 102 | |
Wang YanQing | 6b5f146 | 2013-05-21 23:29:11 +0800 | [diff] [blame] | 103 | give_up_console() is a wrapper to do_unregister_con_driver(), and a driver must |
Antonino A. Daplas | 79062a0 | 2006-06-26 00:27:11 -0700 | [diff] [blame] | 104 | be fully unbound for this call to succeed. con_is_bound() will check if the |
| 105 | driver is bound or not. |
| 106 | |
| 107 | Guidelines for console driver writers: |
| 108 | ===================================== |
| 109 | |
| 110 | In order for binding to and unbinding from the console to properly work, |
| 111 | console drivers must follow these guidelines: |
| 112 | |
Wang YanQing | 6b5f146 | 2013-05-21 23:29:11 +0800 | [diff] [blame] | 113 | 1. All drivers, except system drivers, must call either do_register_con_driver() |
Randy Dunlap | 3d83d31 | 2018-08-06 16:24:55 -0700 | [diff] [blame] | 114 | or do_take_over_console(). do_register_con_driver() will just add the driver |
| 115 | to the console's internal list. It won't take over the |
Wang YanQing | 6b5f146 | 2013-05-21 23:29:11 +0800 | [diff] [blame] | 116 | console. do_take_over_console(), as it name implies, will also take over (or |
Antonino A. Daplas | 79062a0 | 2006-06-26 00:27:11 -0700 | [diff] [blame] | 117 | bind to) the console. |
| 118 | |
| 119 | 2. All resources allocated during con->con_init() must be released in |
| 120 | con->con_deinit(). |
| 121 | |
| 122 | 3. All resources allocated in con->con_startup() must be released when the |
| 123 | driver, which was previously bound, becomes unbound. The console layer |
| 124 | does not have a complementary call to con->con_startup() so it's up to the |
| 125 | driver to check when it's legal to release these resources. Calling |
| 126 | con_is_bound() in con->con_deinit() will help. If the call returned |
| 127 | false(), then it's safe to release the resources. This balance has to be |
| 128 | ensured because con->con_startup() can be called again when a request to |
| 129 | rebind the driver to the console arrives. |
| 130 | |
| 131 | 4. Upon exit of the driver, ensure that the driver is totally unbound. If the |
Wang YanQing | 6b5f146 | 2013-05-21 23:29:11 +0800 | [diff] [blame] | 132 | condition is satisfied, then the driver must call do_unregister_con_driver() |
Antonino A. Daplas | 79062a0 | 2006-06-26 00:27:11 -0700 | [diff] [blame] | 133 | or give_up_console(). |
| 134 | |
Wang YanQing | 6b5f146 | 2013-05-21 23:29:11 +0800 | [diff] [blame] | 135 | 5. do_unregister_con_driver() can also be called on conditions which make it |
Antonino A. Daplas | 79062a0 | 2006-06-26 00:27:11 -0700 | [diff] [blame] | 136 | impossible for the driver to service console requests. This can happen |
| 137 | with the framebuffer console that suddenly lost all of its drivers. |
| 138 | |
| 139 | The current crop of console drivers should still work correctly, but binding |
| 140 | and unbinding them may cause problems. With minimal fixes, these drivers can |
| 141 | be made to work correctly. |
| 142 | |
| 143 | ========================== |
| 144 | Antonino Daplas <adaplas@pol.net> |
| 145 | |