1 /* 2 * linux/include/linux/console.h 3 * 4 * Copyright (C) 1993 Hamish Macdonald 5 * 6 * This file is subject to the terms and conditions of the GNU General Public 7 * License. See the file COPYING in the main directory of this archive 8 * for more details. 9 * 10 * Changed: 11 * 10-Mar-94: Arno Griffioen: Conversion for vt100 emulator port from PC LINUX 12 */ 13 14 #ifndef _LINUX_CONSOLE_H_ 15 #define _LINUX_CONSOLE_H_ 1 16 17 #include <linux/atomic.h> 18 #include <linux/bits.h> 19 #include <linux/rculist.h> 20 #include <linux/types.h> 21 22 struct vc_data; 23 struct console_font_op; 24 struct console_font; 25 struct module; 26 struct tty_struct; 27 struct notifier_block; 28 29 enum con_scroll { 30 SM_UP, 31 SM_DOWN, 32 }; 33 34 enum vc_intensity; 35 36 /** 37 * struct consw - callbacks for consoles 38 * 39 * @con_scroll: move lines from @top to @bottom in direction @dir by @lines. 40 * Return true if no generic handling should be done. 41 * Invoked by csi_M and printing to the console. 42 * @con_set_palette: sets the palette of the console to @table (optional) 43 * @con_scrolldelta: the contents of the console should be scrolled by @lines. 44 * Invoked by user. (optional) 45 */ 46 struct consw { 47 struct module *owner; 48 const char *(*con_startup)(void); 49 void (*con_init)(struct vc_data *vc, int init); 50 void (*con_deinit)(struct vc_data *vc); 51 void (*con_clear)(struct vc_data *vc, int sy, int sx, int height, 52 int width); 53 void (*con_putc)(struct vc_data *vc, int c, int ypos, int xpos); 54 void (*con_putcs)(struct vc_data *vc, const unsigned short *s, 55 int count, int ypos, int xpos); 56 void (*con_cursor)(struct vc_data *vc, int mode); 57 bool (*con_scroll)(struct vc_data *vc, unsigned int top, 58 unsigned int bottom, enum con_scroll dir, 59 unsigned int lines); 60 int (*con_switch)(struct vc_data *vc); 61 int (*con_blank)(struct vc_data *vc, int blank, int mode_switch); 62 int (*con_font_set)(struct vc_data *vc, struct console_font *font, 63 unsigned int flags); 64 int (*con_font_get)(struct vc_data *vc, struct console_font *font); 65 int (*con_font_default)(struct vc_data *vc, 66 struct console_font *font, char *name); 67 int (*con_resize)(struct vc_data *vc, unsigned int width, 68 unsigned int height, unsigned int user); 69 void (*con_set_palette)(struct vc_data *vc, 70 const unsigned char *table); 71 void (*con_scrolldelta)(struct vc_data *vc, int lines); 72 int (*con_set_origin)(struct vc_data *vc); 73 void (*con_save_screen)(struct vc_data *vc); 74 u8 (*con_build_attr)(struct vc_data *vc, u8 color, 75 enum vc_intensity intensity, 76 bool blink, bool underline, bool reverse, bool italic); 77 void (*con_invert_region)(struct vc_data *vc, u16 *p, int count); 78 u16 *(*con_screen_pos)(const struct vc_data *vc, int offset); 79 unsigned long (*con_getxy)(struct vc_data *vc, unsigned long position, 80 int *px, int *py); 81 /* 82 * Flush the video console driver's scrollback buffer 83 */ 84 void (*con_flush_scrollback)(struct vc_data *vc); 85 /* 86 * Prepare the console for the debugger. This includes, but is not 87 * limited to, unblanking the console, loading an appropriate 88 * palette, and allowing debugger generated output. 89 */ 90 int (*con_debug_enter)(struct vc_data *vc); 91 /* 92 * Restore the console to its pre-debug state as closely as possible. 93 */ 94 int (*con_debug_leave)(struct vc_data *vc); 95 }; 96 97 extern const struct consw *conswitchp; 98 99 extern const struct consw dummy_con; /* dummy console buffer */ 100 extern const struct consw vga_con; /* VGA text console */ 101 extern const struct consw newport_con; /* SGI Newport console */ 102 103 int con_is_bound(const struct consw *csw); 104 int do_unregister_con_driver(const struct consw *csw); 105 int do_take_over_console(const struct consw *sw, int first, int last, int deflt); 106 void give_up_console(const struct consw *sw); 107 #ifdef CONFIG_HW_CONSOLE 108 int con_debug_enter(struct vc_data *vc); 109 int con_debug_leave(void); 110 #else 111 static inline int con_debug_enter(struct vc_data *vc) 112 { 113 return 0; 114 } 115 static inline int con_debug_leave(void) 116 { 117 return 0; 118 } 119 #endif 120 121 /* cursor */ 122 #define CM_DRAW (1) 123 #define CM_ERASE (2) 124 #define CM_MOVE (3) 125 126 /* 127 * The interface for a console, or any other device that wants to capture 128 * console messages (printer driver?) 129 */ 130 131 /** 132 * cons_flags - General console flags 133 * @CON_PRINTBUFFER: Used by newly registered consoles to avoid duplicate 134 * output of messages that were already shown by boot 135 * consoles or read by userspace via syslog() syscall. 136 * @CON_CONSDEV: Indicates that the console driver is backing 137 * /dev/console. 138 * @CON_ENABLED: Indicates if a console is allowed to print records. If 139 * false, the console also will not advance to later 140 * records. 141 * @CON_BOOT: Marks the console driver as early console driver which 142 * is used during boot before the real driver becomes 143 * available. It will be automatically unregistered 144 * when the real console driver is registered unless 145 * "keep_bootcon" parameter is used. 146 * @CON_ANYTIME: A misnomed historical flag which tells the core code 147 * that the legacy @console::write callback can be invoked 148 * on a CPU which is marked OFFLINE. That is misleading as 149 * it suggests that there is no contextual limit for 150 * invoking the callback. The original motivation was 151 * readiness of the per-CPU areas. 152 * @CON_BRL: Indicates a braille device which is exempt from 153 * receiving the printk spam for obvious reasons. 154 * @CON_EXTENDED: The console supports the extended output format of 155 * /dev/kmesg which requires a larger output buffer. 156 */ 157 enum cons_flags { 158 CON_PRINTBUFFER = BIT(0), 159 CON_CONSDEV = BIT(1), 160 CON_ENABLED = BIT(2), 161 CON_BOOT = BIT(3), 162 CON_ANYTIME = BIT(4), 163 CON_BRL = BIT(5), 164 CON_EXTENDED = BIT(6), 165 }; 166 167 /** 168 * struct console - The console descriptor structure 169 * @name: The name of the console driver 170 * @write: Write callback to output messages (Optional) 171 * @read: Read callback for console input (Optional) 172 * @device: The underlying TTY device driver (Optional) 173 * @unblank: Callback to unblank the console (Optional) 174 * @setup: Callback for initializing the console (Optional) 175 * @exit: Callback for teardown of the console (Optional) 176 * @match: Callback for matching a console (Optional) 177 * @flags: Console flags. See enum cons_flags 178 * @index: Console index, e.g. port number 179 * @cflag: TTY control mode flags 180 * @ispeed: TTY input speed 181 * @ospeed: TTY output speed 182 * @seq: Sequence number of the next ringbuffer record to print 183 * @dropped: Number of unreported dropped ringbuffer records 184 * @data: Driver private data 185 * @node: hlist node for the console list 186 */ 187 struct console { 188 char name[16]; 189 void (*write)(struct console *co, const char *s, unsigned int count); 190 int (*read)(struct console *co, char *s, unsigned int count); 191 struct tty_driver *(*device)(struct console *co, int *index); 192 void (*unblank)(void); 193 int (*setup)(struct console *co, char *options); 194 int (*exit)(struct console *co); 195 int (*match)(struct console *co, char *name, int idx, char *options); 196 short flags; 197 short index; 198 int cflag; 199 uint ispeed; 200 uint ospeed; 201 u64 seq; 202 unsigned long dropped; 203 void *data; 204 struct hlist_node node; 205 }; 206 207 #ifdef CONFIG_LOCKDEP 208 extern void lockdep_assert_console_list_lock_held(void); 209 #else 210 static inline void lockdep_assert_console_list_lock_held(void) 211 { 212 } 213 #endif 214 215 #ifdef CONFIG_DEBUG_LOCK_ALLOC 216 extern bool console_srcu_read_lock_is_held(void); 217 #else 218 static inline bool console_srcu_read_lock_is_held(void) 219 { 220 return 1; 221 } 222 #endif 223 224 extern int console_srcu_read_lock(void); 225 extern void console_srcu_read_unlock(int cookie); 226 227 extern void console_list_lock(void) __acquires(console_mutex); 228 extern void console_list_unlock(void) __releases(console_mutex); 229 230 extern struct hlist_head console_list; 231 232 /** 233 * console_srcu_read_flags - Locklessly read the console flags 234 * @con: struct console pointer of console to read flags from 235 * 236 * This function provides the necessary READ_ONCE() and data_race() 237 * notation for locklessly reading the console flags. The READ_ONCE() 238 * in this function matches the WRITE_ONCE() when @flags are modified 239 * for registered consoles with console_srcu_write_flags(). 240 * 241 * Only use this function to read console flags when locklessly 242 * iterating the console list via srcu. 243 * 244 * Context: Any context. 245 */ 246 static inline short console_srcu_read_flags(const struct console *con) 247 { 248 WARN_ON_ONCE(!console_srcu_read_lock_is_held()); 249 250 /* 251 * Locklessly reading console->flags provides a consistent 252 * read value because there is at most one CPU modifying 253 * console->flags and that CPU is using only read-modify-write 254 * operations to do so. 255 */ 256 return data_race(READ_ONCE(con->flags)); 257 } 258 259 /** 260 * console_srcu_write_flags - Write flags for a registered console 261 * @con: struct console pointer of console to write flags to 262 * @flags: new flags value to write 263 * 264 * Only use this function to write flags for registered consoles. It 265 * requires holding the console_list_lock. 266 * 267 * Context: Any context. 268 */ 269 static inline void console_srcu_write_flags(struct console *con, short flags) 270 { 271 lockdep_assert_console_list_lock_held(); 272 273 /* This matches the READ_ONCE() in console_srcu_read_flags(). */ 274 WRITE_ONCE(con->flags, flags); 275 } 276 277 /* Variant of console_is_registered() when the console_list_lock is held. */ 278 static inline bool console_is_registered_locked(const struct console *con) 279 { 280 lockdep_assert_console_list_lock_held(); 281 return !hlist_unhashed(&con->node); 282 } 283 284 /* 285 * console_is_registered - Check if the console is registered 286 * @con: struct console pointer of console to check 287 * 288 * Context: Process context. May sleep while acquiring console list lock. 289 * Return: true if the console is in the console list, otherwise false. 290 * 291 * If false is returned for a console that was previously registered, it 292 * can be assumed that the console's unregistration is fully completed, 293 * including the exit() callback after console list removal. 294 */ 295 static inline bool console_is_registered(const struct console *con) 296 { 297 bool ret; 298 299 console_list_lock(); 300 ret = console_is_registered_locked(con); 301 console_list_unlock(); 302 return ret; 303 } 304 305 /** 306 * for_each_console_srcu() - Iterator over registered consoles 307 * @con: struct console pointer used as loop cursor 308 * 309 * Although SRCU guarantees the console list will be consistent, the 310 * struct console fields may be updated by other CPUs while iterating. 311 * 312 * Requires console_srcu_read_lock to be held. Can be invoked from 313 * any context. 314 */ 315 #define for_each_console_srcu(con) \ 316 hlist_for_each_entry_srcu(con, &console_list, node, \ 317 console_srcu_read_lock_is_held()) 318 319 /** 320 * for_each_console() - Iterator over registered consoles 321 * @con: struct console pointer used as loop cursor 322 * 323 * The console list and the console->flags are immutable while iterating. 324 * 325 * Requires console_list_lock to be held. 326 */ 327 #define for_each_console(con) \ 328 lockdep_assert_console_list_lock_held(); \ 329 hlist_for_each_entry(con, &console_list, node) 330 331 extern int console_set_on_cmdline; 332 extern struct console *early_console; 333 334 enum con_flush_mode { 335 CONSOLE_FLUSH_PENDING, 336 CONSOLE_REPLAY_ALL, 337 }; 338 339 extern int add_preferred_console(char *name, int idx, char *options); 340 extern void console_force_preferred_locked(struct console *con); 341 extern void register_console(struct console *); 342 extern int unregister_console(struct console *); 343 extern void console_lock(void); 344 extern int console_trylock(void); 345 extern void console_unlock(void); 346 extern void console_conditional_schedule(void); 347 extern void console_unblank(void); 348 extern void console_flush_on_panic(enum con_flush_mode mode); 349 extern struct tty_driver *console_device(int *); 350 extern void console_stop(struct console *); 351 extern void console_start(struct console *); 352 extern int is_console_locked(void); 353 extern int braille_register_console(struct console *, int index, 354 char *console_options, char *braille_options); 355 extern int braille_unregister_console(struct console *); 356 #ifdef CONFIG_TTY 357 extern void console_sysfs_notify(void); 358 #else 359 static inline void console_sysfs_notify(void) 360 { } 361 #endif 362 extern bool console_suspend_enabled; 363 364 /* Suspend and resume console messages over PM events */ 365 extern void suspend_console(void); 366 extern void resume_console(void); 367 368 int mda_console_init(void); 369 370 void vcs_make_sysfs(int index); 371 void vcs_remove_sysfs(int index); 372 373 /* Some debug stub to catch some of the obvious races in the VT code */ 374 #define WARN_CONSOLE_UNLOCKED() \ 375 WARN_ON(!atomic_read(&ignore_console_lock_warning) && \ 376 !is_console_locked() && !oops_in_progress) 377 /* 378 * Increment ignore_console_lock_warning if you need to quiet 379 * WARN_CONSOLE_UNLOCKED() for debugging purposes. 380 */ 381 extern atomic_t ignore_console_lock_warning; 382 383 /* VESA Blanking Levels */ 384 #define VESA_NO_BLANKING 0 385 #define VESA_VSYNC_SUSPEND 1 386 #define VESA_HSYNC_SUSPEND 2 387 #define VESA_POWERDOWN 3 388 389 extern void console_init(void); 390 391 /* For deferred console takeover */ 392 void dummycon_register_output_notifier(struct notifier_block *nb); 393 void dummycon_unregister_output_notifier(struct notifier_block *nb); 394 395 #endif /* _LINUX_CONSOLE_H */ 396