xref: /OK3568_Linux_fs/kernel/Documentation/driver-api/console.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun.. SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun
3*4882a593Smuzhiyun===============
4*4882a593SmuzhiyunConsole Drivers
5*4882a593Smuzhiyun===============
6*4882a593Smuzhiyun
7*4882a593SmuzhiyunThe Linux kernel has 2 general types of console drivers.  The first type is
8*4882a593Smuzhiyunassigned by the kernel to all the virtual consoles during the boot process.
9*4882a593SmuzhiyunThis type will be called 'system driver', and only one system driver is allowed
10*4882a593Smuzhiyunto exist. The system driver is persistent and it can never be unloaded, though
11*4882a593Smuzhiyunit may become inactive.
12*4882a593Smuzhiyun
13*4882a593SmuzhiyunThe second type has to be explicitly loaded and unloaded. This will be called
14*4882a593Smuzhiyun'modular driver' by this document. Multiple modular drivers can coexist at
15*4882a593Smuzhiyunany time with each driver sharing the console with other drivers including
16*4882a593Smuzhiyunthe system driver. However, modular drivers cannot take over the console
17*4882a593Smuzhiyunthat is currently occupied by another modular driver. (Exception: Drivers that
18*4882a593Smuzhiyuncall do_take_over_console() will succeed in the takeover regardless of the type
19*4882a593Smuzhiyunof driver occupying the consoles.) They can only take over the console that is
20*4882a593Smuzhiyunoccupied by the system driver. In the same token, if the modular driver is
21*4882a593Smuzhiyunreleased by the console, the system driver will take over.
22*4882a593Smuzhiyun
23*4882a593SmuzhiyunModular drivers, from the programmer's point of view, have to call::
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun	 do_take_over_console() - load and bind driver to console layer
26*4882a593Smuzhiyun	 give_up_console() - unload driver; it will only work if driver
27*4882a593Smuzhiyun			     is fully unbound
28*4882a593Smuzhiyun
29*4882a593SmuzhiyunIn newer kernels, the following are also available::
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun	 do_register_con_driver()
32*4882a593Smuzhiyun	 do_unregister_con_driver()
33*4882a593Smuzhiyun
34*4882a593SmuzhiyunIf sysfs is enabled, the contents of /sys/class/vtconsole can be
35*4882a593Smuzhiyunexamined. This shows the console backends currently registered by the
36*4882a593Smuzhiyunsystem which are named vtcon<n> where <n> is an integer from 0 to 15.
37*4882a593SmuzhiyunThus::
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun       ls /sys/class/vtconsole
40*4882a593Smuzhiyun       .  ..  vtcon0  vtcon1
41*4882a593Smuzhiyun
42*4882a593SmuzhiyunEach directory in /sys/class/vtconsole has 3 files::
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun     ls /sys/class/vtconsole/vtcon0
45*4882a593Smuzhiyun     .  ..  bind  name  uevent
46*4882a593Smuzhiyun
47*4882a593SmuzhiyunWhat do these files signify?
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun     1. bind - this is a read/write file. It shows the status of the driver if
50*4882a593Smuzhiyun        read, or acts to bind or unbind the driver to the virtual consoles
51*4882a593Smuzhiyun        when written to. The possible values are:
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun	0
54*4882a593Smuzhiyun	  - means the driver is not bound and if echo'ed, commands the driver
55*4882a593Smuzhiyun	    to unbind
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun        1
58*4882a593Smuzhiyun	  - means the driver is bound and if echo'ed, commands the driver to
59*4882a593Smuzhiyun	    bind
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun     2. name - read-only file. Shows the name of the driver in this format::
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun	  cat /sys/class/vtconsole/vtcon0/name
64*4882a593Smuzhiyun	  (S) VGA+
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun	      '(S)' stands for a (S)ystem driver, i.e., it cannot be directly
67*4882a593Smuzhiyun	      commanded to bind or unbind
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun	      'VGA+' is the name of the driver
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun	  cat /sys/class/vtconsole/vtcon1/name
72*4882a593Smuzhiyun	  (M) frame buffer device
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun	      In this case, '(M)' stands for a (M)odular driver, one that can be
75*4882a593Smuzhiyun	      directly commanded to bind or unbind.
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun     3. uevent - ignore this file
78*4882a593Smuzhiyun
79*4882a593SmuzhiyunWhen unbinding, the modular driver is detached first, and then the system
80*4882a593Smuzhiyundriver takes over the consoles vacated by the driver. Binding, on the other
81*4882a593Smuzhiyunhand, will bind the driver to the consoles that are currently occupied by a
82*4882a593Smuzhiyunsystem driver.
83*4882a593Smuzhiyun
84*4882a593SmuzhiyunNOTE1:
85*4882a593Smuzhiyun  Binding and unbinding must be selected in Kconfig. It's under::
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun    Device Drivers ->
88*4882a593Smuzhiyun	Character devices ->
89*4882a593Smuzhiyun		Support for binding and unbinding console drivers
90*4882a593Smuzhiyun
91*4882a593SmuzhiyunNOTE2:
92*4882a593Smuzhiyun  If any of the virtual consoles are in KD_GRAPHICS mode, then binding or
93*4882a593Smuzhiyun  unbinding will not succeed. An example of an application that sets the
94*4882a593Smuzhiyun  console to KD_GRAPHICS is X.
95*4882a593Smuzhiyun
96*4882a593SmuzhiyunHow useful is this feature? This is very useful for console driver
97*4882a593Smuzhiyundevelopers. By unbinding the driver from the console layer, one can unload the
98*4882a593Smuzhiyundriver, make changes, recompile, reload and rebind the driver without any need
99*4882a593Smuzhiyunfor rebooting the kernel. For regular users who may want to switch from
100*4882a593Smuzhiyunframebuffer console to VGA console and vice versa, this feature also makes
101*4882a593Smuzhiyunthis possible. (NOTE NOTE NOTE: Please read fbcon.txt under Documentation/fb
102*4882a593Smuzhiyunfor more details.)
103*4882a593Smuzhiyun
104*4882a593SmuzhiyunNotes for developers
105*4882a593Smuzhiyun====================
106*4882a593Smuzhiyun
107*4882a593Smuzhiyundo_take_over_console() is now broken up into::
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun     do_register_con_driver()
110*4882a593Smuzhiyun     do_bind_con_driver() - private function
111*4882a593Smuzhiyun
112*4882a593Smuzhiyungive_up_console() is a wrapper to do_unregister_con_driver(), and a driver must
113*4882a593Smuzhiyunbe fully unbound for this call to succeed. con_is_bound() will check if the
114*4882a593Smuzhiyundriver is bound or not.
115*4882a593Smuzhiyun
116*4882a593SmuzhiyunGuidelines for console driver writers
117*4882a593Smuzhiyun=====================================
118*4882a593Smuzhiyun
119*4882a593SmuzhiyunIn order for binding to and unbinding from the console to properly work,
120*4882a593Smuzhiyunconsole drivers must follow these guidelines:
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun1. All drivers, except system drivers, must call either do_register_con_driver()
123*4882a593Smuzhiyun   or do_take_over_console(). do_register_con_driver() will just add the driver
124*4882a593Smuzhiyun   to the console's internal list. It won't take over the
125*4882a593Smuzhiyun   console. do_take_over_console(), as it name implies, will also take over (or
126*4882a593Smuzhiyun   bind to) the console.
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun2. All resources allocated during con->con_init() must be released in
129*4882a593Smuzhiyun   con->con_deinit().
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun3. All resources allocated in con->con_startup() must be released when the
132*4882a593Smuzhiyun   driver, which was previously bound, becomes unbound.  The console layer
133*4882a593Smuzhiyun   does not have a complementary call to con->con_startup() so it's up to the
134*4882a593Smuzhiyun   driver to check when it's legal to release these resources. Calling
135*4882a593Smuzhiyun   con_is_bound() in con->con_deinit() will help.  If the call returned
136*4882a593Smuzhiyun   false(), then it's safe to release the resources.  This balance has to be
137*4882a593Smuzhiyun   ensured because con->con_startup() can be called again when a request to
138*4882a593Smuzhiyun   rebind the driver to the console arrives.
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun4. Upon exit of the driver, ensure that the driver is totally unbound. If the
141*4882a593Smuzhiyun   condition is satisfied, then the driver must call do_unregister_con_driver()
142*4882a593Smuzhiyun   or give_up_console().
143*4882a593Smuzhiyun
144*4882a593Smuzhiyun5. do_unregister_con_driver() can also be called on conditions which make it
145*4882a593Smuzhiyun   impossible for the driver to service console requests.  This can happen
146*4882a593Smuzhiyun   with the framebuffer console that suddenly lost all of its drivers.
147*4882a593Smuzhiyun
148*4882a593SmuzhiyunThe current crop of console drivers should still work correctly, but binding
149*4882a593Smuzhiyunand unbinding them may cause problems. With minimal fixes, these drivers can
150*4882a593Smuzhiyunbe made to work correctly.
151*4882a593Smuzhiyun
152*4882a593SmuzhiyunAntonino Daplas <adaplas@pol.net>
153