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