1 /*
2  *  linux/drivers/char/ttyprintk.c
3  *
4  *  Copyright (C) 2010  Samo Pogacnik
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the smems of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  */
10 
11 /*
12  * This pseudo device allows user to make printk messages. It is possible
13  * to store "console" messages inline with kernel messages for better analyses
14  * of the boot process, for example.
15  */
16 
17 #include <linux/device.h>
18 #include <linux/serial.h>
19 #include <linux/tty.h>
20 #include <linux/module.h>
21 #include <linux/spinlock.h>
22 
23 struct ttyprintk_port {
24 	struct tty_port port;
25 	spinlock_t spinlock;
26 };
27 
28 static struct ttyprintk_port tpk_port;
29 
30 /*
31  * Our simple preformatting supports transparent output of (time-stamped)
32  * printk messages (also suitable for logging service):
33  * - any cr is replaced by nl
34  * - adds a ttyprintk source tag in front of each line
35  * - too long message is fragmented, with '\'nl between fragments
36  * - TPK_STR_SIZE isn't really the write_room limiting factor, because
37  *   it is emptied on the fly during preformatting.
38  */
39 #define TPK_STR_SIZE 508 /* should be bigger then max expected line length */
40 #define TPK_MAX_ROOM 4096 /* we could assume 4K for instance */
41 static int tpk_curr;
42 
43 static char tpk_buffer[TPK_STR_SIZE + 4];
44 
tpk_flush(void)45 static void tpk_flush(void)
46 {
47 	if (tpk_curr > 0) {
48 		tpk_buffer[tpk_curr] = '\0';
49 		pr_info("[U] %s\n", tpk_buffer);
50 		tpk_curr = 0;
51 	}
52 }
53 
tpk_printk(const unsigned char * buf,int count)54 static int tpk_printk(const unsigned char *buf, int count)
55 {
56 	int i = tpk_curr;
57 
58 	if (buf == NULL) {
59 		tpk_flush();
60 		return i;
61 	}
62 
63 	for (i = 0; i < count; i++) {
64 		if (tpk_curr >= TPK_STR_SIZE) {
65 			/* end of tmp buffer reached: cut the message in two */
66 			tpk_buffer[tpk_curr++] = '\\';
67 			tpk_flush();
68 		}
69 
70 		switch (buf[i]) {
71 		case '\r':
72 			tpk_flush();
73 			if ((i + 1) < count && buf[i + 1] == '\n')
74 				i++;
75 			break;
76 		case '\n':
77 			tpk_flush();
78 			break;
79 		default:
80 			tpk_buffer[tpk_curr++] = buf[i];
81 			break;
82 		}
83 	}
84 
85 	return count;
86 }
87 
88 /*
89  * TTY operations open function.
90  */
tpk_open(struct tty_struct * tty,struct file * filp)91 static int tpk_open(struct tty_struct *tty, struct file *filp)
92 {
93 	tty->driver_data = &tpk_port;
94 
95 	return tty_port_open(&tpk_port.port, tty, filp);
96 }
97 
98 /*
99  * TTY operations close function.
100  */
tpk_close(struct tty_struct * tty,struct file * filp)101 static void tpk_close(struct tty_struct *tty, struct file *filp)
102 {
103 	struct ttyprintk_port *tpkp = tty->driver_data;
104 	unsigned long flags;
105 
106 	spin_lock_irqsave(&tpkp->spinlock, flags);
107 	/* flush tpk_printk buffer */
108 	tpk_printk(NULL, 0);
109 	spin_unlock_irqrestore(&tpkp->spinlock, flags);
110 
111 	tty_port_close(&tpkp->port, tty, filp);
112 }
113 
114 /*
115  * TTY operations write function.
116  */
tpk_write(struct tty_struct * tty,const unsigned char * buf,int count)117 static int tpk_write(struct tty_struct *tty,
118 		const unsigned char *buf, int count)
119 {
120 	struct ttyprintk_port *tpkp = tty->driver_data;
121 	unsigned long flags;
122 	int ret;
123 
124 
125 	/* exclusive use of tpk_printk within this tty */
126 	spin_lock_irqsave(&tpkp->spinlock, flags);
127 	ret = tpk_printk(buf, count);
128 	spin_unlock_irqrestore(&tpkp->spinlock, flags);
129 
130 	return ret;
131 }
132 
133 /*
134  * TTY operations write_room function.
135  */
tpk_write_room(struct tty_struct * tty)136 static int tpk_write_room(struct tty_struct *tty)
137 {
138 	return TPK_MAX_ROOM;
139 }
140 
141 /*
142  * TTY operations ioctl function.
143  */
tpk_ioctl(struct tty_struct * tty,unsigned int cmd,unsigned long arg)144 static int tpk_ioctl(struct tty_struct *tty,
145 			unsigned int cmd, unsigned long arg)
146 {
147 	struct ttyprintk_port *tpkp = tty->driver_data;
148 
149 	if (!tpkp)
150 		return -EINVAL;
151 
152 	switch (cmd) {
153 	/* Stop TIOCCONS */
154 	case TIOCCONS:
155 		return -EOPNOTSUPP;
156 	default:
157 		return -ENOIOCTLCMD;
158 	}
159 	return 0;
160 }
161 
162 /*
163  * TTY operations hangup function.
164  */
tpk_hangup(struct tty_struct * tty)165 static void tpk_hangup(struct tty_struct *tty)
166 {
167 	struct ttyprintk_port *tpkp = tty->driver_data;
168 
169 	tty_port_hangup(&tpkp->port);
170 }
171 
172 static const struct tty_operations ttyprintk_ops = {
173 	.open = tpk_open,
174 	.close = tpk_close,
175 	.write = tpk_write,
176 	.write_room = tpk_write_room,
177 	.ioctl = tpk_ioctl,
178 	.hangup = tpk_hangup,
179 };
180 
181 static const struct tty_port_operations null_ops = { };
182 
183 static struct tty_driver *ttyprintk_driver;
184 
ttyprintk_init(void)185 static int __init ttyprintk_init(void)
186 {
187 	int ret = -ENOMEM;
188 
189 	spin_lock_init(&tpk_port.spinlock);
190 
191 	ttyprintk_driver = tty_alloc_driver(1,
192 			TTY_DRIVER_RESET_TERMIOS |
193 			TTY_DRIVER_REAL_RAW |
194 			TTY_DRIVER_UNNUMBERED_NODE);
195 	if (IS_ERR(ttyprintk_driver))
196 		return PTR_ERR(ttyprintk_driver);
197 
198 	tty_port_init(&tpk_port.port);
199 	tpk_port.port.ops = &null_ops;
200 
201 	ttyprintk_driver->driver_name = "ttyprintk";
202 	ttyprintk_driver->name = "ttyprintk";
203 	ttyprintk_driver->major = TTYAUX_MAJOR;
204 	ttyprintk_driver->minor_start = 3;
205 	ttyprintk_driver->type = TTY_DRIVER_TYPE_CONSOLE;
206 	ttyprintk_driver->init_termios = tty_std_termios;
207 	ttyprintk_driver->init_termios.c_oflag = OPOST | OCRNL | ONOCR | ONLRET;
208 	tty_set_operations(ttyprintk_driver, &ttyprintk_ops);
209 	tty_port_link_device(&tpk_port.port, ttyprintk_driver, 0);
210 
211 	ret = tty_register_driver(ttyprintk_driver);
212 	if (ret < 0) {
213 		printk(KERN_ERR "Couldn't register ttyprintk driver\n");
214 		goto error;
215 	}
216 
217 	return 0;
218 
219 error:
220 	put_tty_driver(ttyprintk_driver);
221 	tty_port_destroy(&tpk_port.port);
222 	return ret;
223 }
224 
ttyprintk_exit(void)225 static void __exit ttyprintk_exit(void)
226 {
227 	tty_unregister_driver(ttyprintk_driver);
228 	put_tty_driver(ttyprintk_driver);
229 	tty_port_destroy(&tpk_port.port);
230 }
231 
232 device_initcall(ttyprintk_init);
233 module_exit(ttyprintk_exit);
234 
235 MODULE_LICENSE("GPL");
236