global: Migrate CONFIG_STACKBASE to CFG
[platform/kernel/u-boot.git] / include / video_console.h
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * Copyright (c) 2015 Google, Inc
4  */
5
6 #ifndef __video_console_h
7 #define __video_console_h
8
9 #include <video.h>
10
11 struct video_priv;
12
13 #define VID_FRAC_DIV    256
14
15 #define VID_TO_PIXEL(x) ((x) / VID_FRAC_DIV)
16 #define VID_TO_POS(x)   ((x) * VID_FRAC_DIV)
17
18 /**
19  * struct vidconsole_priv - uclass-private data about a console device
20  *
21  * Drivers must set up @rows, @cols, @x_charsize, @y_charsize in their probe()
22  * method. Drivers may set up @xstart_frac if desired.
23  *
24  * @sdev:               stdio device, acting as an output sink
25  * @xcur_frac:          Current X position, in fractional units (VID_TO_POS(x))
26  * @ycur:               Current Y position in pixels (0=top)
27  * @rows:               Number of text rows
28  * @cols:               Number of text columns
29  * @x_charsize:         Character width in pixels
30  * @y_charsize:         Character height in pixels
31  * @tab_width_frac:     Tab width in fractional units
32  * @xsize_frac:         Width of the display in fractional units
33  * @xstart_frac:        Left margin for the text console in fractional units
34  * @last_ch:            Last character written to the text console on this line
35  * @escape:             TRUE if currently accumulating an ANSI escape sequence
36  * @escape_len:         Length of accumulated escape sequence so far
37  * @col_saved:          Saved X position, in fractional units (VID_TO_POS(x))
38  * @row_saved:          Saved Y position in pixels (0=top)
39  * @escape_buf:         Buffer to accumulate escape sequence
40  */
41 struct vidconsole_priv {
42         struct stdio_dev sdev;
43         int xcur_frac;
44         int ycur;
45         int rows;
46         int cols;
47         int x_charsize;
48         int y_charsize;
49         int tab_width_frac;
50         int xsize_frac;
51         int xstart_frac;
52         int last_ch;
53         /*
54          * ANSI escape sequences are accumulated character by character,
55          * starting after the ESC char (0x1b) until the entire sequence
56          * is consumed at which point it is acted upon.
57          */
58         int escape;
59         int escape_len;
60         int row_saved;
61         int col_saved;
62         char escape_buf[32];
63 };
64
65 /**
66  * struct vidconsole_ops - Video console operations
67  *
68  * These operations work on either an absolute console position (measured
69  * in pixels) or a text row number (measured in rows, where each row consists
70  * of an entire line of text - typically 16 pixels).
71  */
72 struct vidconsole_ops {
73         /**
74          * putc_xy() - write a single character to a position
75          *
76          * @dev:        Device to write to
77          * @x_frac:     Fractional pixel X position (0=left-most pixel) which
78          *              is the X position multipled by VID_FRAC_DIV.
79          * @y:          Pixel Y position (0=top-most pixel)
80          * @ch:         Character to write
81          * @return number of fractional pixels that the cursor should move,
82          * if all is OK, -EAGAIN if we ran out of space on this line, other -ve
83          * on error
84          */
85         int (*putc_xy)(struct udevice *dev, uint x_frac, uint y, char ch);
86
87         /**
88          * move_rows() - Move text rows from one place to another
89          *
90          * @dev:        Device to adjust
91          * @rowdst:     Destination text row (0=top)
92          * @rowsrc:     Source start text row
93          * @count:      Number of text rows to move
94          * @return 0 if OK, -ve on error
95          */
96         int (*move_rows)(struct udevice *dev, uint rowdst, uint rowsrc,
97                           uint count);
98
99         /**
100          * set_row() - Set the colour of a text row
101          *
102          * Every pixel contained within the text row is adjusted
103          *
104          * @dev:        Device to adjust
105          * @row:        Text row to adjust (0=top)
106          * @clr:        Raw colour (pixel value) to write to each pixel
107          * @return 0 if OK, -ve on error
108          */
109         int (*set_row)(struct udevice *dev, uint row, int clr);
110
111         /**
112          * entry_start() - Indicate that text entry is starting afresh
113          *
114          * Consoles which use proportional fonts need to track the position of
115          * each character output so that backspace will return to the correct
116          * place. This method signals to the console driver that a new entry
117          * line is being start (e.g. the user pressed return to start a new
118          * command). The driver can use this signal to empty its list of
119          * positions.
120          */
121         int (*entry_start)(struct udevice *dev);
122
123         /**
124          * backspace() - Handle erasing the last character
125          *
126          * With proportional fonts the vidconsole uclass cannot itself erase
127          * the previous character. This optional method will be called when
128          * a backspace is needed. The driver should erase the previous
129          * character and update the cursor position (xcur_frac, ycur) to the
130          * start of the previous character.
131          *
132          * If not implement, default behaviour will work for fixed-width
133          * characters.
134          */
135         int (*backspace)(struct udevice *dev);
136 };
137
138 /* Get a pointer to the driver operations for a video console device */
139 #define vidconsole_get_ops(dev)  ((struct vidconsole_ops *)(dev)->driver->ops)
140
141 /**
142  * vidconsole_putc_xy() - write a single character to a position
143  *
144  * @dev:        Device to write to
145  * @x_frac:     Fractional pixel X position (0=left-most pixel) which
146  *              is the X position multipled by VID_FRAC_DIV.
147  * @y:          Pixel Y position (0=top-most pixel)
148  * @ch:         Character to write
149  * Return: number of fractional pixels that the cursor should move,
150  * if all is OK, -EAGAIN if we ran out of space on this line, other -ve
151  * on error
152  */
153 int vidconsole_putc_xy(struct udevice *dev, uint x, uint y, char ch);
154
155 /**
156  * vidconsole_move_rows() - Move text rows from one place to another
157  *
158  * @dev:        Device to adjust
159  * @rowdst:     Destination text row (0=top)
160  * @rowsrc:     Source start text row
161  * @count:      Number of text rows to move
162  * Return: 0 if OK, -ve on error
163  */
164 int vidconsole_move_rows(struct udevice *dev, uint rowdst, uint rowsrc,
165                          uint count);
166
167 /**
168  * vidconsole_set_row() - Set the colour of a text row
169  *
170  * Every pixel contained within the text row is adjusted
171  *
172  * @dev:        Device to adjust
173  * @row:        Text row to adjust (0=top)
174  * @clr:        Raw colour (pixel value) to write to each pixel
175  * Return: 0 if OK, -ve on error
176  */
177 int vidconsole_set_row(struct udevice *dev, uint row, int clr);
178
179 /**
180  * vidconsole_put_char() - Output a character to the current console position
181  *
182  * Outputs a character to the console and advances the cursor. This function
183  * handles wrapping to new lines and scrolling the console. Special
184  * characters are handled also: \n, \r, \b and \t.
185  *
186  * The device always starts with the cursor at position 0,0 (top left). It
187  * can be adjusted manually using vidconsole_position_cursor().
188  *
189  * @dev:        Device to adjust
190  * @ch:         Character to write
191  * Return: 0 if OK, -ve on error
192  */
193 int vidconsole_put_char(struct udevice *dev, char ch);
194
195 /**
196  * vidconsole_put_string() - Output a string to the current console position
197  *
198  * Outputs a string to the console and advances the cursor. This function
199  * handles wrapping to new lines and scrolling the console. Special
200  * characters are handled also: \n, \r, \b and \t.
201  *
202  * The device always starts with the cursor at position 0,0 (top left). It
203  * can be adjusted manually using vidconsole_position_cursor().
204  *
205  * @dev:        Device to adjust
206  * @str:        String to write
207  * Return: 0 if OK, -ve on error
208  */
209 int vidconsole_put_string(struct udevice *dev, const char *str);
210
211 /**
212  * vidconsole_position_cursor() - Move the text cursor
213  *
214  * @dev:        Device to adjust
215  * @col:        New cursor text column
216  * @row:        New cursor text row
217  * Return: 0 if OK, -ve on error
218  */
219 void vidconsole_position_cursor(struct udevice *dev, unsigned col,
220                                 unsigned row);
221
222 /**
223  * vidconsole_set_cursor_pos() - set cursor position
224  *
225  * The cursor is set to the new position and the start-of-line information is
226  * updated to the same position, so that a newline will return to @x
227  *
228  * @dev:        video console device to update
229  * @x:          x position from left in pixels
230  * @y:          y position from top in pixels
231  */
232 void vidconsole_set_cursor_pos(struct udevice *dev, int x, int y);
233
234 /**
235  * vidconsole_list_fonts() - List the available fonts
236  *
237  * This shows a list on the console
238  */
239 void vidconsole_list_fonts(void);
240
241 /**
242  * vidconsole_select_font() - Select a font to use
243  *
244  * @dev: vidconsole device
245  * @name: Font name
246  * @size: Size of the font (norminal pixel height) or 0 for default
247  */
248 int vidconsole_select_font(struct udevice *dev, const char *name, uint size);
249
250 /**
251  * vidconsole_get_font() - get the current font name and size
252  *
253  * @dev: vidconsole device
254  * @sizep: Place to put the font size (nominal height in pixels)
255  * Returns: Current font name
256  */
257 const char *vidconsole_get_font(struct udevice *dev, uint *sizep);
258
259 #ifdef CONFIG_VIDEO_COPY
260 /**
261  * vidconsole_sync_copy() - Sync back to the copy framebuffer
262  *
263  * This ensures that the copy framebuffer has the same data as the framebuffer
264  * for a particular region. It should be called after the framebuffer is updated
265  *
266  * @from and @to can be in either order. The region between them is synced.
267  *
268  * @dev: Vidconsole device being updated
269  * @from: Start/end address within the framebuffer (->fb)
270  * @to: Other address within the frame buffer
271  * Return: 0 if OK, -EFAULT if the start address is before the start of the
272  *      frame buffer start
273  */
274 int vidconsole_sync_copy(struct udevice *dev, void *from, void *to);
275
276 /**
277  * vidconsole_memmove() - Perform a memmove() within the frame buffer
278  *
279  * This handles a memmove(), e.g. for scrolling. It also updates the copy
280  * framebuffer.
281  *
282  * @dev: Vidconsole device being updated
283  * @dst: Destination address within the framebuffer (->fb)
284  * @src: Source address within the framebuffer (->fb)
285  * @size: Number of bytes to transfer
286  * Return: 0 if OK, -EFAULT if the start address is before the start of the
287  *      frame buffer start
288  */
289 int vidconsole_memmove(struct udevice *dev, void *dst, const void *src,
290                        int size);
291 #else
292 static inline int vidconsole_sync_copy(struct udevice *dev, void *from,
293                                        void *to)
294 {
295         return 0;
296 }
297
298 static inline int vidconsole_memmove(struct udevice *dev, void *dst,
299                                      const void *src, int size)
300 {
301         memmove(dst, src, size);
302
303         return 0;
304 }
305
306 #endif
307
308 #endif