blob: 774748497acedf2da7d3d195b57d29fe485cf94f [file] [log] [blame]
Samo Pogacnik24b4b672010-08-25 20:44:07 +02001/*
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>
Takashi Iwaib24313a2014-04-02 14:45:22 +020020#include <linux/module.h>
Zhenzhong Duan1a270e52020-01-13 11:48:42 +080021#include <linux/spinlock.h>
Samo Pogacnik24b4b672010-08-25 20:44:07 +020022
23struct ttyprintk_port {
24 struct tty_port port;
Zhenzhong Duan1a270e52020-01-13 11:48:42 +080025 spinlock_t spinlock;
Samo Pogacnik24b4b672010-08-25 20:44:07 +020026};
27
28static 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
Joe Perches0b3191d2016-09-06 09:49:04 -070035 * - too long message is fragmented, with '\'nl between fragments
36 * - TPK_STR_SIZE isn't really the write_room limiting factor, because
Samo Pogacnik24b4b672010-08-25 20:44:07 +020037 * 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 */
Samo Pogacnik24b4b672010-08-25 20:44:07 +020041static int tpk_curr;
42
Joe Perches0b3191d2016-09-06 09:49:04 -070043static char tpk_buffer[TPK_STR_SIZE + 4];
44
45static 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
Samo Pogacnik24b4b672010-08-25 20:44:07 +020054static int tpk_printk(const unsigned char *buf, int count)
55{
Samo Pogacnik24b4b672010-08-25 20:44:07 +020056 int i = tpk_curr;
57
58 if (buf == NULL) {
Joe Perches0b3191d2016-09-06 09:49:04 -070059 tpk_flush();
Samo Pogacnik24b4b672010-08-25 20:44:07 +020060 return i;
61 }
62
63 for (i = 0; i < count; i++) {
Joe Perches0b3191d2016-09-06 09:49:04 -070064 if (tpk_curr >= TPK_STR_SIZE) {
Samo Pogacnik24b4b672010-08-25 20:44:07 +020065 /* end of tmp buffer reached: cut the message in two */
Joe Perches0b3191d2016-09-06 09:49:04 -070066 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;
Samo Pogacnik24b4b672010-08-25 20:44:07 +020082 }
83 }
84
85 return count;
86}
87
88/*
89 * TTY operations open function.
90 */
91static 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 */
101static void tpk_close(struct tty_struct *tty, struct file *filp)
102{
103 struct ttyprintk_port *tpkp = tty->driver_data;
Zhenzhong Duan1a270e52020-01-13 11:48:42 +0800104 unsigned long flags;
Samo Pogacnik24b4b672010-08-25 20:44:07 +0200105
Zhenzhong Duan1a270e52020-01-13 11:48:42 +0800106 spin_lock_irqsave(&tpkp->spinlock, flags);
Samo Pogacnik24b4b672010-08-25 20:44:07 +0200107 /* flush tpk_printk buffer */
108 tpk_printk(NULL, 0);
Zhenzhong Duan1a270e52020-01-13 11:48:42 +0800109 spin_unlock_irqrestore(&tpkp->spinlock, flags);
Samo Pogacnik24b4b672010-08-25 20:44:07 +0200110
111 tty_port_close(&tpkp->port, tty, filp);
112}
113
114/*
115 * TTY operations write function.
116 */
117static int tpk_write(struct tty_struct *tty,
118 const unsigned char *buf, int count)
119{
120 struct ttyprintk_port *tpkp = tty->driver_data;
Zhenzhong Duan1a270e52020-01-13 11:48:42 +0800121 unsigned long flags;
Samo Pogacnik24b4b672010-08-25 20:44:07 +0200122 int ret;
123
124
125 /* exclusive use of tpk_printk within this tty */
Zhenzhong Duan1a270e52020-01-13 11:48:42 +0800126 spin_lock_irqsave(&tpkp->spinlock, flags);
Samo Pogacnik24b4b672010-08-25 20:44:07 +0200127 ret = tpk_printk(buf, count);
Zhenzhong Duan1a270e52020-01-13 11:48:42 +0800128 spin_unlock_irqrestore(&tpkp->spinlock, flags);
Samo Pogacnik24b4b672010-08-25 20:44:07 +0200129
130 return ret;
131}
132
133/*
134 * TTY operations write_room function.
135 */
136static int tpk_write_room(struct tty_struct *tty)
137{
138 return TPK_MAX_ROOM;
139}
140
141/*
142 * TTY operations ioctl function.
143 */
Alan Cox6caa76b2011-02-14 16:27:22 +0000144static int tpk_ioctl(struct tty_struct *tty,
Samo Pogacnik24b4b672010-08-25 20:44:07 +0200145 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
162static const struct tty_operations ttyprintk_ops = {
163 .open = tpk_open,
164 .close = tpk_close,
165 .write = tpk_write,
166 .write_room = tpk_write_room,
167 .ioctl = tpk_ioctl,
168};
169
Aya Mahfouz9b95d952015-12-15 01:50:19 +0200170static const struct tty_port_operations null_ops = { };
Samo Pogacnik24b4b672010-08-25 20:44:07 +0200171
172static struct tty_driver *ttyprintk_driver;
173
174static int __init ttyprintk_init(void)
175{
176 int ret = -ENOMEM;
Samo Pogacnik24b4b672010-08-25 20:44:07 +0200177
Zhenzhong Duan1a270e52020-01-13 11:48:42 +0800178 spin_lock_init(&tpk_port.spinlock);
Jiri Slaby536a3442012-08-07 21:47:40 +0200179
Jiri Slaby0019b402012-08-08 22:26:43 +0200180 ttyprintk_driver = tty_alloc_driver(1,
181 TTY_DRIVER_RESET_TERMIOS |
182 TTY_DRIVER_REAL_RAW |
183 TTY_DRIVER_UNNUMBERED_NODE);
Dan Carpenterc3a63442012-08-16 16:16:56 +0300184 if (IS_ERR(ttyprintk_driver))
185 return PTR_ERR(ttyprintk_driver);
Samo Pogacnik24b4b672010-08-25 20:44:07 +0200186
Jiri Slaby191c5f12012-11-15 09:49:56 +0100187 tty_port_init(&tpk_port.port);
Darrick J. Wongb5325a02013-05-10 15:40:13 -0700188 tpk_port.port.ops = &null_ops;
Jiri Slaby191c5f12012-11-15 09:49:56 +0100189
Samo Pogacnik24b4b672010-08-25 20:44:07 +0200190 ttyprintk_driver->driver_name = "ttyprintk";
191 ttyprintk_driver->name = "ttyprintk";
192 ttyprintk_driver->major = TTYAUX_MAJOR;
193 ttyprintk_driver->minor_start = 3;
Samo Pogacnik24b4b672010-08-25 20:44:07 +0200194 ttyprintk_driver->type = TTY_DRIVER_TYPE_CONSOLE;
195 ttyprintk_driver->init_termios = tty_std_termios;
196 ttyprintk_driver->init_termios.c_oflag = OPOST | OCRNL | ONOCR | ONLRET;
Samo Pogacnik24b4b672010-08-25 20:44:07 +0200197 tty_set_operations(ttyprintk_driver, &ttyprintk_ops);
Jiri Slabyb19e2ca2012-08-07 21:47:51 +0200198 tty_port_link_device(&tpk_port.port, ttyprintk_driver, 0);
Samo Pogacnik24b4b672010-08-25 20:44:07 +0200199
200 ret = tty_register_driver(ttyprintk_driver);
201 if (ret < 0) {
202 printk(KERN_ERR "Couldn't register ttyprintk driver\n");
203 goto error;
204 }
205
Samo Pogacnik24b4b672010-08-25 20:44:07 +0200206 return 0;
207
208error:
Samo Pogacnik24b4b672010-08-25 20:44:07 +0200209 put_tty_driver(ttyprintk_driver);
Jiri Slaby191c5f12012-11-15 09:49:56 +0100210 tty_port_destroy(&tpk_port.port);
Samo Pogacnik24b4b672010-08-25 20:44:07 +0200211 return ret;
212}
Takashi Iwaib24313a2014-04-02 14:45:22 +0200213
214static void __exit ttyprintk_exit(void)
215{
216 tty_unregister_driver(ttyprintk_driver);
217 put_tty_driver(ttyprintk_driver);
218 tty_port_destroy(&tpk_port.port);
219}
220
Paul Gortmakerdb50d2f2014-01-12 12:37:56 -0500221device_initcall(ttyprintk_init);
Takashi Iwaib24313a2014-04-02 14:45:22 +0200222module_exit(ttyprintk_exit);
223
224MODULE_LICENSE("GPL");