18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * linux/drivers/char/ttyprintk.c 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2010 Samo Pogacnik 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci/* 98c2ecf20Sopenharmony_ci * This pseudo device allows user to make printk messages. It is possible 108c2ecf20Sopenharmony_ci * to store "console" messages inline with kernel messages for better analyses 118c2ecf20Sopenharmony_ci * of the boot process, for example. 128c2ecf20Sopenharmony_ci */ 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ci#include <linux/device.h> 158c2ecf20Sopenharmony_ci#include <linux/serial.h> 168c2ecf20Sopenharmony_ci#include <linux/tty.h> 178c2ecf20Sopenharmony_ci#include <linux/module.h> 188c2ecf20Sopenharmony_ci#include <linux/spinlock.h> 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_cistruct ttyprintk_port { 218c2ecf20Sopenharmony_ci struct tty_port port; 228c2ecf20Sopenharmony_ci spinlock_t spinlock; 238c2ecf20Sopenharmony_ci}; 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_cistatic struct ttyprintk_port tpk_port; 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_ci/* 288c2ecf20Sopenharmony_ci * Our simple preformatting supports transparent output of (time-stamped) 298c2ecf20Sopenharmony_ci * printk messages (also suitable for logging service): 308c2ecf20Sopenharmony_ci * - any cr is replaced by nl 318c2ecf20Sopenharmony_ci * - adds a ttyprintk source tag in front of each line 328c2ecf20Sopenharmony_ci * - too long message is fragmented, with '\'nl between fragments 338c2ecf20Sopenharmony_ci * - TPK_STR_SIZE isn't really the write_room limiting factor, because 348c2ecf20Sopenharmony_ci * it is emptied on the fly during preformatting. 358c2ecf20Sopenharmony_ci */ 368c2ecf20Sopenharmony_ci#define TPK_STR_SIZE 508 /* should be bigger then max expected line length */ 378c2ecf20Sopenharmony_ci#define TPK_MAX_ROOM 4096 /* we could assume 4K for instance */ 388c2ecf20Sopenharmony_ci#define TPK_PREFIX KERN_SOH __stringify(CONFIG_TTY_PRINTK_LEVEL) 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_cistatic int tpk_curr; 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_cistatic char tpk_buffer[TPK_STR_SIZE + 4]; 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_cistatic void tpk_flush(void) 458c2ecf20Sopenharmony_ci{ 468c2ecf20Sopenharmony_ci if (tpk_curr > 0) { 478c2ecf20Sopenharmony_ci tpk_buffer[tpk_curr] = '\0'; 488c2ecf20Sopenharmony_ci printk(TPK_PREFIX "[U] %s\n", tpk_buffer); 498c2ecf20Sopenharmony_ci tpk_curr = 0; 508c2ecf20Sopenharmony_ci } 518c2ecf20Sopenharmony_ci} 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_cistatic int tpk_printk(const unsigned char *buf, int count) 548c2ecf20Sopenharmony_ci{ 558c2ecf20Sopenharmony_ci int i = tpk_curr; 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ci if (buf == NULL) { 588c2ecf20Sopenharmony_ci tpk_flush(); 598c2ecf20Sopenharmony_ci return i; 608c2ecf20Sopenharmony_ci } 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ci for (i = 0; i < count; i++) { 638c2ecf20Sopenharmony_ci if (tpk_curr >= TPK_STR_SIZE) { 648c2ecf20Sopenharmony_ci /* end of tmp buffer reached: cut the message in two */ 658c2ecf20Sopenharmony_ci tpk_buffer[tpk_curr++] = '\\'; 668c2ecf20Sopenharmony_ci tpk_flush(); 678c2ecf20Sopenharmony_ci } 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_ci switch (buf[i]) { 708c2ecf20Sopenharmony_ci case '\r': 718c2ecf20Sopenharmony_ci tpk_flush(); 728c2ecf20Sopenharmony_ci if ((i + 1) < count && buf[i + 1] == '\n') 738c2ecf20Sopenharmony_ci i++; 748c2ecf20Sopenharmony_ci break; 758c2ecf20Sopenharmony_ci case '\n': 768c2ecf20Sopenharmony_ci tpk_flush(); 778c2ecf20Sopenharmony_ci break; 788c2ecf20Sopenharmony_ci default: 798c2ecf20Sopenharmony_ci tpk_buffer[tpk_curr++] = buf[i]; 808c2ecf20Sopenharmony_ci break; 818c2ecf20Sopenharmony_ci } 828c2ecf20Sopenharmony_ci } 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci return count; 858c2ecf20Sopenharmony_ci} 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ci/* 888c2ecf20Sopenharmony_ci * TTY operations open function. 898c2ecf20Sopenharmony_ci */ 908c2ecf20Sopenharmony_cistatic int tpk_open(struct tty_struct *tty, struct file *filp) 918c2ecf20Sopenharmony_ci{ 928c2ecf20Sopenharmony_ci tty->driver_data = &tpk_port; 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_ci return tty_port_open(&tpk_port.port, tty, filp); 958c2ecf20Sopenharmony_ci} 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ci/* 988c2ecf20Sopenharmony_ci * TTY operations close function. 998c2ecf20Sopenharmony_ci */ 1008c2ecf20Sopenharmony_cistatic void tpk_close(struct tty_struct *tty, struct file *filp) 1018c2ecf20Sopenharmony_ci{ 1028c2ecf20Sopenharmony_ci struct ttyprintk_port *tpkp = tty->driver_data; 1038c2ecf20Sopenharmony_ci unsigned long flags; 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ci spin_lock_irqsave(&tpkp->spinlock, flags); 1068c2ecf20Sopenharmony_ci /* flush tpk_printk buffer */ 1078c2ecf20Sopenharmony_ci tpk_printk(NULL, 0); 1088c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&tpkp->spinlock, flags); 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_ci tty_port_close(&tpkp->port, tty, filp); 1118c2ecf20Sopenharmony_ci} 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_ci/* 1148c2ecf20Sopenharmony_ci * TTY operations write function. 1158c2ecf20Sopenharmony_ci */ 1168c2ecf20Sopenharmony_cistatic int tpk_write(struct tty_struct *tty, 1178c2ecf20Sopenharmony_ci const unsigned char *buf, int count) 1188c2ecf20Sopenharmony_ci{ 1198c2ecf20Sopenharmony_ci struct ttyprintk_port *tpkp = tty->driver_data; 1208c2ecf20Sopenharmony_ci unsigned long flags; 1218c2ecf20Sopenharmony_ci int ret; 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ci /* exclusive use of tpk_printk within this tty */ 1258c2ecf20Sopenharmony_ci spin_lock_irqsave(&tpkp->spinlock, flags); 1268c2ecf20Sopenharmony_ci ret = tpk_printk(buf, count); 1278c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&tpkp->spinlock, flags); 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci return ret; 1308c2ecf20Sopenharmony_ci} 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_ci/* 1338c2ecf20Sopenharmony_ci * TTY operations write_room function. 1348c2ecf20Sopenharmony_ci */ 1358c2ecf20Sopenharmony_cistatic int tpk_write_room(struct tty_struct *tty) 1368c2ecf20Sopenharmony_ci{ 1378c2ecf20Sopenharmony_ci return TPK_MAX_ROOM; 1388c2ecf20Sopenharmony_ci} 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ci/* 1418c2ecf20Sopenharmony_ci * TTY operations ioctl function. 1428c2ecf20Sopenharmony_ci */ 1438c2ecf20Sopenharmony_cistatic int tpk_ioctl(struct tty_struct *tty, 1448c2ecf20Sopenharmony_ci unsigned int cmd, unsigned long arg) 1458c2ecf20Sopenharmony_ci{ 1468c2ecf20Sopenharmony_ci struct ttyprintk_port *tpkp = tty->driver_data; 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ci if (!tpkp) 1498c2ecf20Sopenharmony_ci return -EINVAL; 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ci switch (cmd) { 1528c2ecf20Sopenharmony_ci /* Stop TIOCCONS */ 1538c2ecf20Sopenharmony_ci case TIOCCONS: 1548c2ecf20Sopenharmony_ci return -EOPNOTSUPP; 1558c2ecf20Sopenharmony_ci default: 1568c2ecf20Sopenharmony_ci return -ENOIOCTLCMD; 1578c2ecf20Sopenharmony_ci } 1588c2ecf20Sopenharmony_ci return 0; 1598c2ecf20Sopenharmony_ci} 1608c2ecf20Sopenharmony_ci 1618c2ecf20Sopenharmony_ci/* 1628c2ecf20Sopenharmony_ci * TTY operations hangup function. 1638c2ecf20Sopenharmony_ci */ 1648c2ecf20Sopenharmony_cistatic void tpk_hangup(struct tty_struct *tty) 1658c2ecf20Sopenharmony_ci{ 1668c2ecf20Sopenharmony_ci struct ttyprintk_port *tpkp = tty->driver_data; 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_ci tty_port_hangup(&tpkp->port); 1698c2ecf20Sopenharmony_ci} 1708c2ecf20Sopenharmony_ci 1718c2ecf20Sopenharmony_cistatic const struct tty_operations ttyprintk_ops = { 1728c2ecf20Sopenharmony_ci .open = tpk_open, 1738c2ecf20Sopenharmony_ci .close = tpk_close, 1748c2ecf20Sopenharmony_ci .write = tpk_write, 1758c2ecf20Sopenharmony_ci .write_room = tpk_write_room, 1768c2ecf20Sopenharmony_ci .ioctl = tpk_ioctl, 1778c2ecf20Sopenharmony_ci .hangup = tpk_hangup, 1788c2ecf20Sopenharmony_ci}; 1798c2ecf20Sopenharmony_ci 1808c2ecf20Sopenharmony_cistatic const struct tty_port_operations null_ops = { }; 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_cistatic struct tty_driver *ttyprintk_driver; 1838c2ecf20Sopenharmony_ci 1848c2ecf20Sopenharmony_cistatic int __init ttyprintk_init(void) 1858c2ecf20Sopenharmony_ci{ 1868c2ecf20Sopenharmony_ci int ret; 1878c2ecf20Sopenharmony_ci 1888c2ecf20Sopenharmony_ci spin_lock_init(&tpk_port.spinlock); 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_ci ttyprintk_driver = tty_alloc_driver(1, 1918c2ecf20Sopenharmony_ci TTY_DRIVER_RESET_TERMIOS | 1928c2ecf20Sopenharmony_ci TTY_DRIVER_REAL_RAW | 1938c2ecf20Sopenharmony_ci TTY_DRIVER_UNNUMBERED_NODE); 1948c2ecf20Sopenharmony_ci if (IS_ERR(ttyprintk_driver)) 1958c2ecf20Sopenharmony_ci return PTR_ERR(ttyprintk_driver); 1968c2ecf20Sopenharmony_ci 1978c2ecf20Sopenharmony_ci tty_port_init(&tpk_port.port); 1988c2ecf20Sopenharmony_ci tpk_port.port.ops = &null_ops; 1998c2ecf20Sopenharmony_ci 2008c2ecf20Sopenharmony_ci ttyprintk_driver->driver_name = "ttyprintk"; 2018c2ecf20Sopenharmony_ci ttyprintk_driver->name = "ttyprintk"; 2028c2ecf20Sopenharmony_ci ttyprintk_driver->major = TTYAUX_MAJOR; 2038c2ecf20Sopenharmony_ci ttyprintk_driver->minor_start = 3; 2048c2ecf20Sopenharmony_ci ttyprintk_driver->type = TTY_DRIVER_TYPE_CONSOLE; 2058c2ecf20Sopenharmony_ci ttyprintk_driver->init_termios = tty_std_termios; 2068c2ecf20Sopenharmony_ci ttyprintk_driver->init_termios.c_oflag = OPOST | OCRNL | ONOCR | ONLRET; 2078c2ecf20Sopenharmony_ci tty_set_operations(ttyprintk_driver, &ttyprintk_ops); 2088c2ecf20Sopenharmony_ci tty_port_link_device(&tpk_port.port, ttyprintk_driver, 0); 2098c2ecf20Sopenharmony_ci 2108c2ecf20Sopenharmony_ci ret = tty_register_driver(ttyprintk_driver); 2118c2ecf20Sopenharmony_ci if (ret < 0) { 2128c2ecf20Sopenharmony_ci printk(KERN_ERR "Couldn't register ttyprintk driver\n"); 2138c2ecf20Sopenharmony_ci goto error; 2148c2ecf20Sopenharmony_ci } 2158c2ecf20Sopenharmony_ci 2168c2ecf20Sopenharmony_ci return 0; 2178c2ecf20Sopenharmony_ci 2188c2ecf20Sopenharmony_cierror: 2198c2ecf20Sopenharmony_ci put_tty_driver(ttyprintk_driver); 2208c2ecf20Sopenharmony_ci tty_port_destroy(&tpk_port.port); 2218c2ecf20Sopenharmony_ci return ret; 2228c2ecf20Sopenharmony_ci} 2238c2ecf20Sopenharmony_ci 2248c2ecf20Sopenharmony_cistatic void __exit ttyprintk_exit(void) 2258c2ecf20Sopenharmony_ci{ 2268c2ecf20Sopenharmony_ci tty_unregister_driver(ttyprintk_driver); 2278c2ecf20Sopenharmony_ci put_tty_driver(ttyprintk_driver); 2288c2ecf20Sopenharmony_ci tty_port_destroy(&tpk_port.port); 2298c2ecf20Sopenharmony_ci} 2308c2ecf20Sopenharmony_ci 2318c2ecf20Sopenharmony_cidevice_initcall(ttyprintk_init); 2328c2ecf20Sopenharmony_cimodule_exit(ttyprintk_exit); 2338c2ecf20Sopenharmony_ci 2348c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 235