GNU Linux-libre 4.4.289-gnu1
[releases.git] / drivers / char / ttyprintk.c
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 fragmeted, with '\'nl between fragments
36  * - TPK_STR_SIZE isn't really the write_room limiting factor, bcause
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 const char *tpk_tag = "[U] "; /* U for User */
42 static int tpk_curr;
43
44 static int tpk_printk(const unsigned char *buf, int count)
45 {
46         static char tmp[TPK_STR_SIZE + 4];
47         int i = tpk_curr;
48
49         if (buf == NULL) {
50                 /* flush tmp[] */
51                 if (tpk_curr > 0) {
52                         /* non nl or cr terminated message - add nl */
53                         tmp[tpk_curr + 0] = '\n';
54                         tmp[tpk_curr + 1] = '\0';
55                         printk(KERN_INFO "%s%s", tpk_tag, tmp);
56                         tpk_curr = 0;
57                 }
58                 return i;
59         }
60
61         for (i = 0; i < count; i++) {
62                 tmp[tpk_curr] = buf[i];
63                 if (tpk_curr < TPK_STR_SIZE) {
64                         switch (buf[i]) {
65                         case '\r':
66                                 /* replace cr with nl */
67                                 tmp[tpk_curr + 0] = '\n';
68                                 tmp[tpk_curr + 1] = '\0';
69                                 printk(KERN_INFO "%s%s", tpk_tag, tmp);
70                                 tpk_curr = 0;
71                                 if ((i + 1) < count && buf[i + 1] == '\n')
72                                         i++;
73                                 break;
74                         case '\n':
75                                 tmp[tpk_curr + 1] = '\0';
76                                 printk(KERN_INFO "%s%s", tpk_tag, tmp);
77                                 tpk_curr = 0;
78                                 break;
79                         default:
80                                 tpk_curr++;
81                         }
82                 } else {
83                         /* end of tmp buffer reached: cut the message in two */
84                         tmp[tpk_curr + 1] = '\\';
85                         tmp[tpk_curr + 2] = '\n';
86                         tmp[tpk_curr + 3] = '\0';
87                         printk(KERN_INFO "%s%s", tpk_tag, tmp);
88                         tpk_curr = 0;
89                 }
90         }
91
92         return count;
93 }
94
95 /*
96  * TTY operations open function.
97  */
98 static int tpk_open(struct tty_struct *tty, struct file *filp)
99 {
100         tty->driver_data = &tpk_port;
101
102         return tty_port_open(&tpk_port.port, tty, filp);
103 }
104
105 /*
106  * TTY operations close function.
107  */
108 static void tpk_close(struct tty_struct *tty, struct file *filp)
109 {
110         struct ttyprintk_port *tpkp = tty->driver_data;
111         unsigned long flags;
112
113         spin_lock_irqsave(&tpkp->spinlock, flags);
114         /* flush tpk_printk buffer */
115         tpk_printk(NULL, 0);
116         spin_unlock_irqrestore(&tpkp->spinlock, flags);
117
118         tty_port_close(&tpkp->port, tty, filp);
119 }
120
121 /*
122  * TTY operations write function.
123  */
124 static int tpk_write(struct tty_struct *tty,
125                 const unsigned char *buf, int count)
126 {
127         struct ttyprintk_port *tpkp = tty->driver_data;
128         unsigned long flags;
129         int ret;
130
131
132         /* exclusive use of tpk_printk within this tty */
133         spin_lock_irqsave(&tpkp->spinlock, flags);
134         ret = tpk_printk(buf, count);
135         spin_unlock_irqrestore(&tpkp->spinlock, flags);
136
137         return ret;
138 }
139
140 /*
141  * TTY operations write_room function.
142  */
143 static int tpk_write_room(struct tty_struct *tty)
144 {
145         return TPK_MAX_ROOM;
146 }
147
148 /*
149  * TTY operations ioctl function.
150  */
151 static int tpk_ioctl(struct tty_struct *tty,
152                         unsigned int cmd, unsigned long arg)
153 {
154         struct ttyprintk_port *tpkp = tty->driver_data;
155
156         if (!tpkp)
157                 return -EINVAL;
158
159         switch (cmd) {
160         /* Stop TIOCCONS */
161         case TIOCCONS:
162                 return -EOPNOTSUPP;
163         default:
164                 return -ENOIOCTLCMD;
165         }
166         return 0;
167 }
168
169 /*
170  * TTY operations hangup function.
171  */
172 static void tpk_hangup(struct tty_struct *tty)
173 {
174         struct ttyprintk_port *tpkp = tty->driver_data;
175
176         tty_port_hangup(&tpkp->port);
177 }
178
179 static const struct tty_operations ttyprintk_ops = {
180         .open = tpk_open,
181         .close = tpk_close,
182         .write = tpk_write,
183         .write_room = tpk_write_room,
184         .ioctl = tpk_ioctl,
185         .hangup = tpk_hangup,
186 };
187
188 static struct tty_port_operations null_ops = { };
189
190 static struct tty_driver *ttyprintk_driver;
191
192 static int __init ttyprintk_init(void)
193 {
194         int ret = -ENOMEM;
195
196         spin_lock_init(&tpk_port.spinlock);
197
198         ttyprintk_driver = tty_alloc_driver(1,
199                         TTY_DRIVER_RESET_TERMIOS |
200                         TTY_DRIVER_REAL_RAW |
201                         TTY_DRIVER_UNNUMBERED_NODE);
202         if (IS_ERR(ttyprintk_driver))
203                 return PTR_ERR(ttyprintk_driver);
204
205         tty_port_init(&tpk_port.port);
206         tpk_port.port.ops = &null_ops;
207
208         ttyprintk_driver->driver_name = "ttyprintk";
209         ttyprintk_driver->name = "ttyprintk";
210         ttyprintk_driver->major = TTYAUX_MAJOR;
211         ttyprintk_driver->minor_start = 3;
212         ttyprintk_driver->type = TTY_DRIVER_TYPE_CONSOLE;
213         ttyprintk_driver->init_termios = tty_std_termios;
214         ttyprintk_driver->init_termios.c_oflag = OPOST | OCRNL | ONOCR | ONLRET;
215         tty_set_operations(ttyprintk_driver, &ttyprintk_ops);
216         tty_port_link_device(&tpk_port.port, ttyprintk_driver, 0);
217
218         ret = tty_register_driver(ttyprintk_driver);
219         if (ret < 0) {
220                 printk(KERN_ERR "Couldn't register ttyprintk driver\n");
221                 goto error;
222         }
223
224         return 0;
225
226 error:
227         put_tty_driver(ttyprintk_driver);
228         tty_port_destroy(&tpk_port.port);
229         return ret;
230 }
231
232 static void __exit ttyprintk_exit(void)
233 {
234         tty_unregister_driver(ttyprintk_driver);
235         put_tty_driver(ttyprintk_driver);
236         tty_port_destroy(&tpk_port.port);
237 }
238
239 device_initcall(ttyprintk_init);
240 module_exit(ttyprintk_exit);
241
242 MODULE_LICENSE("GPL");