1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2019 Axis Communications AB
5 * Based on ttyprintk.c:
6 * Copyright (C) 2010 Samo Pogacnik
9 #include <linux/console.h>
10 #include <linux/module.h>
11 #include <linux/tty.h>
13 static const struct tty_port_operations ttynull_port_ops;
14 static struct tty_driver *ttynull_driver;
15 static struct tty_port ttynull_port;
17 static int ttynull_open(struct tty_struct *tty, struct file *filp)
19 return tty_port_open(&ttynull_port, tty, filp);
22 static void ttynull_close(struct tty_struct *tty, struct file *filp)
24 tty_port_close(&ttynull_port, tty, filp);
27 static void ttynull_hangup(struct tty_struct *tty)
29 tty_port_hangup(&ttynull_port);
32 static int ttynull_write(struct tty_struct *tty, const unsigned char *buf,
38 static unsigned int ttynull_write_room(struct tty_struct *tty)
43 static const struct tty_operations ttynull_ops = {
45 .close = ttynull_close,
46 .hangup = ttynull_hangup,
47 .write = ttynull_write,
48 .write_room = ttynull_write_room,
51 static struct tty_driver *ttynull_device(struct console *c, int *index)
54 return ttynull_driver;
57 static struct console ttynull_console = {
59 .device = ttynull_device,
62 static int __init ttynull_init(void)
64 struct tty_driver *driver;
67 driver = tty_alloc_driver(1,
68 TTY_DRIVER_RESET_TERMIOS |
70 TTY_DRIVER_UNNUMBERED_NODE);
72 return PTR_ERR(driver);
74 tty_port_init(&ttynull_port);
75 ttynull_port.ops = &ttynull_port_ops;
77 driver->driver_name = "ttynull";
78 driver->name = "ttynull";
79 driver->type = TTY_DRIVER_TYPE_CONSOLE;
80 driver->init_termios = tty_std_termios;
81 driver->init_termios.c_oflag = OPOST | OCRNL | ONOCR | ONLRET;
82 tty_set_operations(driver, &ttynull_ops);
83 tty_port_link_device(&ttynull_port, driver, 0);
85 ret = tty_register_driver(driver);
87 tty_driver_kref_put(driver);
88 tty_port_destroy(&ttynull_port);
92 ttynull_driver = driver;
93 register_console(&ttynull_console);
98 static void __exit ttynull_exit(void)
100 unregister_console(&ttynull_console);
101 tty_unregister_driver(ttynull_driver);
102 tty_driver_kref_put(ttynull_driver);
103 tty_port_destroy(&ttynull_port);
106 module_init(ttynull_init);
107 module_exit(ttynull_exit);
109 MODULE_LICENSE("GPL v2");