Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2)  * ARAnyM console driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * This file is subject to the terms and conditions of the GNU General Public
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * License.  See the file COPYING in the main directory of this archive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/console.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/tty.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/tty_driver.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/tty_flip.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <asm/natfeat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) static int stderr_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) static struct tty_port nfcon_tty_port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) static struct tty_driver *nfcon_tty_driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) static void nfputs(const char *str, unsigned int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	char buf[68];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	unsigned long phys = virt_to_phys(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	buf[64] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	while (count > 64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 		memcpy(buf, str, 64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 		nf_call(stderr_id, phys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 		str += 64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 		count -= 64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	memcpy(buf, str, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	buf[count] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	nf_call(stderr_id, phys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) static void nfcon_write(struct console *con, const char *str,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 			unsigned int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	nfputs(str, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) static struct tty_driver *nfcon_device(struct console *con, int *index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	*index = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	return (con->flags & CON_ENABLED) ? nfcon_tty_driver : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) static struct console nf_console = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	.name	= "nfcon",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	.write	= nfcon_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	.device	= nfcon_device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	.flags	= CON_PRINTBUFFER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	.index	= -1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) static int nfcon_tty_open(struct tty_struct *tty, struct file *filp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) static void nfcon_tty_close(struct tty_struct *tty, struct file *filp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) static int nfcon_tty_write(struct tty_struct *tty, const unsigned char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 			   int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	nfputs(buf, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) static int nfcon_tty_put_char(struct tty_struct *tty, unsigned char ch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	char temp[2] = { ch, 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	nf_call(stderr_id, virt_to_phys(temp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) static int nfcon_tty_write_room(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	return 64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) static const struct tty_operations nfcon_tty_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	.open		= nfcon_tty_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	.close		= nfcon_tty_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	.write		= nfcon_tty_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	.put_char	= nfcon_tty_put_char,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	.write_room	= nfcon_tty_write_room,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) #ifndef MODULE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static int __init nf_debug_setup(char *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	if (strcmp(arg, "nfcon"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	stderr_id = nf_get_id("NF_STDERR");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	if (stderr_id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		nf_console.flags |= CON_ENABLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		register_console(&nf_console);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) early_param("debug", nf_debug_setup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) #endif /* !MODULE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) static int __init nfcon_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	stderr_id = nf_get_id("NF_STDERR");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	if (!stderr_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	nfcon_tty_driver = alloc_tty_driver(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	if (!nfcon_tty_driver)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	tty_port_init(&nfcon_tty_port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	nfcon_tty_driver->driver_name = "nfcon";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	nfcon_tty_driver->name = "nfcon";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	nfcon_tty_driver->type = TTY_DRIVER_TYPE_SYSTEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	nfcon_tty_driver->subtype = SYSTEM_TYPE_TTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	nfcon_tty_driver->init_termios = tty_std_termios;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	nfcon_tty_driver->flags = TTY_DRIVER_REAL_RAW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	tty_set_operations(nfcon_tty_driver, &nfcon_tty_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	tty_port_link_device(&nfcon_tty_port, nfcon_tty_driver, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	res = tty_register_driver(nfcon_tty_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	if (res) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		pr_err("failed to register nfcon tty driver\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		put_tty_driver(nfcon_tty_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		tty_port_destroy(&nfcon_tty_port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	if (!(nf_console.flags & CON_ENABLED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		register_console(&nf_console);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) static void __exit nfcon_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	unregister_console(&nf_console);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	tty_unregister_driver(nfcon_tty_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	put_tty_driver(nfcon_tty_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	tty_port_destroy(&nfcon_tty_port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) module_init(nfcon_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) module_exit(nfcon_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) MODULE_LICENSE("GPL");