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  * The 16 colors supported by the console
20  */
21 enum color_idx {
22 	VID_BLACK = 0,
23 	VID_RED,
24 	VID_GREEN,
25 	VID_BROWN,
26 	VID_BLUE,
27 	VID_MAGENTA,
28 	VID_CYAN,
29 	VID_LIGHT_GRAY,
30 	VID_GRAY,
31 	VID_LIGHT_RED,
32 	VID_LIGTH_GREEN,
33 	VID_YELLOW,
34 	VID_LIGHT_BLUE,
35 	VID_LIGHT_MAGENTA,
36 	VID_LIGHT_CYAN,
37 	VID_WHITE,
38 
39 	VID_COLOR_COUNT
40 };
41 
42 /**
43  * struct vidconsole_priv - uclass-private data about a console device
44  *
45  * Drivers must set up @rows, @cols, @x_charsize, @y_charsize in their probe()
46  * method. Drivers may set up @xstart_frac if desired.
47  *
48  * @sdev:		stdio device, acting as an output sink
49  * @xcur_frac:		Current X position, in fractional units (VID_TO_POS(x))
50  * @ycur:		Current Y position in pixels (0=top)
51  * @rows:		Number of text rows
52  * @cols:		Number of text columns
53  * @x_charsize:		Character width in pixels
54  * @y_charsize:		Character height in pixels
55  * @tab_width_frac:	Tab width in fractional units
56  * @xsize_frac:		Width of the display in fractional units
57  * @xstart_frac:	Left margin for the text console in fractional units
58  * @last_ch:		Last character written to the text console on this line
59  * @escape:		TRUE if currently accumulating an ANSI escape sequence
60  * @escape_len:		Length of accumulated escape sequence so far
61  * @col_saved:		Saved X position, in fractional units (VID_TO_POS(x))
62  * @row_saved:		Saved Y position in pixels (0=top)
63  * @escape_buf:		Buffer to accumulate escape sequence
64  */
65 struct vidconsole_priv {
66 	struct stdio_dev sdev;
67 	int xcur_frac;
68 	int ycur;
69 	int rows;
70 	int cols;
71 	int x_charsize;
72 	int y_charsize;
73 	int tab_width_frac;
74 	int xsize_frac;
75 	int xstart_frac;
76 	int last_ch;
77 	/*
78 	 * ANSI escape sequences are accumulated character by character,
79 	 * starting after the ESC char (0x1b) until the entire sequence
80 	 * is consumed at which point it is acted upon.
81 	 */
82 	int escape;
83 	int escape_len;
84 	int row_saved;
85 	int col_saved;
86 	char escape_buf[32];
87 };
88 
89 /**
90  * struct vidconsole_ops - Video console operations
91  *
92  * These operations work on either an absolute console position (measured
93  * in pixels) or a text row number (measured in rows, where each row consists
94  * of an entire line of text - typically 16 pixels).
95  */
96 struct vidconsole_ops {
97 	/**
98 	 * putc_xy() - write a single character to a position
99 	 *
100 	 * @dev:	Device to write to
101 	 * @x_frac:	Fractional pixel X position (0=left-most pixel) which
102 	 *		is the X position multipled by VID_FRAC_DIV.
103 	 * @y:		Pixel Y position (0=top-most pixel)
104 	 * @ch:		Character to write
105 	 * @return number of fractional pixels that the cursor should move,
106 	 * if all is OK, -EAGAIN if we ran out of space on this line, other -ve
107 	 * on error
108 	 */
109 	int (*putc_xy)(struct udevice *dev, uint x_frac, uint y, char ch);
110 
111 	/**
112 	 * move_rows() - Move text rows from one place to another
113 	 *
114 	 * @dev:	Device to adjust
115 	 * @rowdst:	Destination text row (0=top)
116 	 * @rowsrc:	Source start text row
117 	 * @count:	Number of text rows to move
118 	 * @return 0 if OK, -ve on error
119 	 */
120 	int (*move_rows)(struct udevice *dev, uint rowdst, uint rowsrc,
121 			  uint count);
122 
123 	/**
124 	 * set_row() - Set the colour of a text row
125 	 *
126 	 * Every pixel contained within the text row is adjusted
127 	 *
128 	 * @dev:	Device to adjust
129 	 * @row:	Text row to adjust (0=top)
130 	 * @clr:	Raw colour (pixel value) to write to each pixel
131 	 * @return 0 if OK, -ve on error
132 	 */
133 	int (*set_row)(struct udevice *dev, uint row, int clr);
134 
135 	/**
136 	 * entry_start() - Indicate that text entry is starting afresh
137 	 *
138 	 * Consoles which use proportional fonts need to track the position of
139 	 * each character output so that backspace will return to the correct
140 	 * place. This method signals to the console driver that a new entry
141 	 * line is being start (e.g. the user pressed return to start a new
142 	 * command). The driver can use this signal to empty its list of
143 	 * positions.
144 	 */
145 	int (*entry_start)(struct udevice *dev);
146 
147 	/**
148 	 * backspace() - Handle erasing the last character
149 	 *
150 	 * With proportional fonts the vidconsole uclass cannot itself erase
151 	 * the previous character. This optional method will be called when
152 	 * a backspace is needed. The driver should erase the previous
153 	 * character and update the cursor position (xcur_frac, ycur) to the
154 	 * start of the previous character.
155 	 *
156 	 * If not implement, default behaviour will work for fixed-width
157 	 * characters.
158 	 */
159 	int (*backspace)(struct udevice *dev);
160 };
161 
162 /* Get a pointer to the driver operations for a video console device */
163 #define vidconsole_get_ops(dev)  ((struct vidconsole_ops *)(dev)->driver->ops)
164 
165 /**
166  * vidconsole_putc_xy() - write a single character to a position
167  *
168  * @dev:	Device to write to
169  * @x_frac:	Fractional pixel X position (0=left-most pixel) which
170  *		is the X position multipled by VID_FRAC_DIV.
171  * @y:		Pixel Y position (0=top-most pixel)
172  * @ch:		Character to write
173  * @return number of fractional pixels that the cursor should move,
174  * if all is OK, -EAGAIN if we ran out of space on this line, other -ve
175  * on error
176  */
177 int vidconsole_putc_xy(struct udevice *dev, uint x, uint y, char ch);
178 
179 /**
180  * vidconsole_move_rows() - Move text rows from one place to another
181  *
182  * @dev:	Device to adjust
183  * @rowdst:	Destination text row (0=top)
184  * @rowsrc:	Source start text row
185  * @count:	Number of text rows to move
186  * @return 0 if OK, -ve on error
187  */
188 int vidconsole_move_rows(struct udevice *dev, uint rowdst, uint rowsrc,
189 			 uint count);
190 
191 /**
192  * vidconsole_set_row() - Set the colour of a text row
193  *
194  * Every pixel contained within the text row is adjusted
195  *
196  * @dev:	Device to adjust
197  * @row:	Text row to adjust (0=top)
198  * @clr:	Raw colour (pixel value) to write to each pixel
199  * @return 0 if OK, -ve on error
200  */
201 int vidconsole_set_row(struct udevice *dev, uint row, int clr);
202 
203 /**
204  * vidconsole_put_char() - Output a character to the current console position
205  *
206  * Outputs a character to the console and advances the cursor. This function
207  * handles wrapping to new lines and scrolling the console. Special
208  * characters are handled also: \n, \r, \b and \t.
209  *
210  * The device always starts with the cursor at position 0,0 (top left). It
211  * can be adjusted manually using vidconsole_position_cursor().
212  *
213  * @dev:	Device to adjust
214  * @ch:		Character to write
215  * @return 0 if OK, -ve on error
216  */
217 int vidconsole_put_char(struct udevice *dev, char ch);
218 
219 /**
220  * vidconsole_put_string() - Output a string to the current console position
221  *
222  * Outputs a string to the console and advances the cursor. This function
223  * handles wrapping to new lines and scrolling the console. Special
224  * characters are handled also: \n, \r, \b and \t.
225  *
226  * The device always starts with the cursor at position 0,0 (top left). It
227  * can be adjusted manually using vidconsole_position_cursor().
228  *
229  * @dev:	Device to adjust
230  * @str:	String to write
231  * @return 0 if OK, -ve on error
232  */
233 int vidconsole_put_string(struct udevice *dev, const char *str);
234 
235 /**
236  * vidconsole_position_cursor() - Move the text cursor
237  *
238  * @dev:	Device to adjust
239  * @col:	New cursor text column
240  * @row:	New cursor text row
241  * @return 0 if OK, -ve on error
242  */
243 void vidconsole_position_cursor(struct udevice *dev, unsigned col,
244 				unsigned row);
245 
246 /**
247  * vid_console_color() - convert a color code to a pixel's internal
248  * representation
249  *
250  * The caller has to guarantee that the color index is less than
251  * VID_COLOR_COUNT.
252  *
253  * @priv	private data of the console device
254  * @idx		color index
255  * @return	color value
256  */
257 u32 vid_console_color(struct video_priv *priv, unsigned int idx);
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
vidconsole_sync_copy(struct udevice * dev,void * from,void * to)292 static inline int vidconsole_sync_copy(struct udevice *dev, void *from,
293 				       void *to)
294 {
295 	return 0;
296 }
297 
vidconsole_memmove(struct udevice * dev,void * dst,const void * src,int size)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
309