18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2019 Axis Communications AB
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Based on ttyprintk.c:
68c2ecf20Sopenharmony_ci *  Copyright (C) 2010 Samo Pogacnik
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/console.h>
108c2ecf20Sopenharmony_ci#include <linux/module.h>
118c2ecf20Sopenharmony_ci#include <linux/tty.h>
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_cistatic const struct tty_port_operations ttynull_port_ops;
148c2ecf20Sopenharmony_cistatic struct tty_driver *ttynull_driver;
158c2ecf20Sopenharmony_cistatic struct tty_port ttynull_port;
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_cistatic int ttynull_open(struct tty_struct *tty, struct file *filp)
188c2ecf20Sopenharmony_ci{
198c2ecf20Sopenharmony_ci	return tty_port_open(&ttynull_port, tty, filp);
208c2ecf20Sopenharmony_ci}
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_cistatic void ttynull_close(struct tty_struct *tty, struct file *filp)
238c2ecf20Sopenharmony_ci{
248c2ecf20Sopenharmony_ci	tty_port_close(&ttynull_port, tty, filp);
258c2ecf20Sopenharmony_ci}
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_cistatic void ttynull_hangup(struct tty_struct *tty)
288c2ecf20Sopenharmony_ci{
298c2ecf20Sopenharmony_ci	tty_port_hangup(&ttynull_port);
308c2ecf20Sopenharmony_ci}
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_cistatic int ttynull_write(struct tty_struct *tty, const unsigned char *buf,
338c2ecf20Sopenharmony_ci			 int count)
348c2ecf20Sopenharmony_ci{
358c2ecf20Sopenharmony_ci	return count;
368c2ecf20Sopenharmony_ci}
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_cistatic int ttynull_write_room(struct tty_struct *tty)
398c2ecf20Sopenharmony_ci{
408c2ecf20Sopenharmony_ci	return 65536;
418c2ecf20Sopenharmony_ci}
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cistatic const struct tty_operations ttynull_ops = {
448c2ecf20Sopenharmony_ci	.open = ttynull_open,
458c2ecf20Sopenharmony_ci	.close = ttynull_close,
468c2ecf20Sopenharmony_ci	.hangup = ttynull_hangup,
478c2ecf20Sopenharmony_ci	.write = ttynull_write,
488c2ecf20Sopenharmony_ci	.write_room = ttynull_write_room,
498c2ecf20Sopenharmony_ci};
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_cistatic struct tty_driver *ttynull_device(struct console *c, int *index)
528c2ecf20Sopenharmony_ci{
538c2ecf20Sopenharmony_ci	*index = 0;
548c2ecf20Sopenharmony_ci	return ttynull_driver;
558c2ecf20Sopenharmony_ci}
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_cistatic struct console ttynull_console = {
588c2ecf20Sopenharmony_ci	.name = "ttynull",
598c2ecf20Sopenharmony_ci	.device = ttynull_device,
608c2ecf20Sopenharmony_ci};
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_cistatic int __init ttynull_init(void)
638c2ecf20Sopenharmony_ci{
648c2ecf20Sopenharmony_ci	struct tty_driver *driver;
658c2ecf20Sopenharmony_ci	int ret;
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	driver = tty_alloc_driver(1,
688c2ecf20Sopenharmony_ci		TTY_DRIVER_RESET_TERMIOS |
698c2ecf20Sopenharmony_ci		TTY_DRIVER_REAL_RAW |
708c2ecf20Sopenharmony_ci		TTY_DRIVER_UNNUMBERED_NODE);
718c2ecf20Sopenharmony_ci	if (IS_ERR(driver))
728c2ecf20Sopenharmony_ci		return PTR_ERR(driver);
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	tty_port_init(&ttynull_port);
758c2ecf20Sopenharmony_ci	ttynull_port.ops = &ttynull_port_ops;
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	driver->driver_name = "ttynull";
788c2ecf20Sopenharmony_ci	driver->name = "ttynull";
798c2ecf20Sopenharmony_ci	driver->type = TTY_DRIVER_TYPE_CONSOLE;
808c2ecf20Sopenharmony_ci	driver->init_termios = tty_std_termios;
818c2ecf20Sopenharmony_ci	driver->init_termios.c_oflag = OPOST | OCRNL | ONOCR | ONLRET;
828c2ecf20Sopenharmony_ci	tty_set_operations(driver, &ttynull_ops);
838c2ecf20Sopenharmony_ci	tty_port_link_device(&ttynull_port, driver, 0);
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	ret = tty_register_driver(driver);
868c2ecf20Sopenharmony_ci	if (ret < 0) {
878c2ecf20Sopenharmony_ci		put_tty_driver(driver);
888c2ecf20Sopenharmony_ci		tty_port_destroy(&ttynull_port);
898c2ecf20Sopenharmony_ci		return ret;
908c2ecf20Sopenharmony_ci	}
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	ttynull_driver = driver;
938c2ecf20Sopenharmony_ci	register_console(&ttynull_console);
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	return 0;
968c2ecf20Sopenharmony_ci}
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_cistatic void __exit ttynull_exit(void)
998c2ecf20Sopenharmony_ci{
1008c2ecf20Sopenharmony_ci	unregister_console(&ttynull_console);
1018c2ecf20Sopenharmony_ci	tty_unregister_driver(ttynull_driver);
1028c2ecf20Sopenharmony_ci	put_tty_driver(ttynull_driver);
1038c2ecf20Sopenharmony_ci	tty_port_destroy(&ttynull_port);
1048c2ecf20Sopenharmony_ci}
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_cimodule_init(ttynull_init);
1078c2ecf20Sopenharmony_cimodule_exit(ttynull_exit);
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
110