1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2019 Axis Communications AB
5 * The console is useful for userspace applications which expect a console
6 * device to work without modifications even when no console is available
9 * In order to use this driver, you should redirect the console to this
10 * TTY, or boot the kernel with console=ttynull.
12 * Based on ttyprintk.c:
13 * Copyright (C) 2010 Samo Pogacnik
16 #include <linux/console.h>
17 #include <linux/module.h>
18 #include <linux/tty.h>
20 static const struct tty_port_operations ttynull_port_ops
;
21 static struct tty_driver
*ttynull_driver
;
22 static struct tty_port ttynull_port
;
24 static int ttynull_open(struct tty_struct
*tty
, struct file
*filp
)
26 return tty_port_open(&ttynull_port
, tty
, filp
);
29 static void ttynull_close(struct tty_struct
*tty
, struct file
*filp
)
31 tty_port_close(&ttynull_port
, tty
, filp
);
34 static void ttynull_hangup(struct tty_struct
*tty
)
36 tty_port_hangup(&ttynull_port
);
39 static int ttynull_write(struct tty_struct
*tty
, const unsigned char *buf
,
45 static int ttynull_write_room(struct tty_struct
*tty
)
50 static const struct tty_operations ttynull_ops
= {
52 .close
= ttynull_close
,
53 .hangup
= ttynull_hangup
,
54 .write
= ttynull_write
,
55 .write_room
= ttynull_write_room
,
58 static struct tty_driver
*ttynull_device(struct console
*c
, int *index
)
61 return ttynull_driver
;
64 static struct console ttynull_console
= {
66 .device
= ttynull_device
,
69 void __init
register_ttynull_console(void)
74 if (add_preferred_console(ttynull_console
.name
, 0, NULL
))
77 register_console(&ttynull_console
);
80 static int __init
ttynull_init(void)
82 struct tty_driver
*driver
;
85 driver
= tty_alloc_driver(1,
86 TTY_DRIVER_RESET_TERMIOS
|
88 TTY_DRIVER_UNNUMBERED_NODE
);
90 return PTR_ERR(driver
);
92 tty_port_init(&ttynull_port
);
93 ttynull_port
.ops
= &ttynull_port_ops
;
95 driver
->driver_name
= "ttynull";
96 driver
->name
= "ttynull";
97 driver
->type
= TTY_DRIVER_TYPE_CONSOLE
;
98 driver
->init_termios
= tty_std_termios
;
99 driver
->init_termios
.c_oflag
= OPOST
| OCRNL
| ONOCR
| ONLRET
;
100 tty_set_operations(driver
, &ttynull_ops
);
101 tty_port_link_device(&ttynull_port
, driver
, 0);
103 ret
= tty_register_driver(driver
);
105 put_tty_driver(driver
);
106 tty_port_destroy(&ttynull_port
);
110 ttynull_driver
= driver
;
111 register_console(&ttynull_console
);
116 static void __exit
ttynull_exit(void)
118 unregister_console(&ttynull_console
);
119 tty_unregister_driver(ttynull_driver
);
120 put_tty_driver(ttynull_driver
);
121 tty_port_destroy(&ttynull_port
);
124 module_init(ttynull_init
);
125 module_exit(ttynull_exit
);
127 MODULE_LICENSE("GPL v2");