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