162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * linux/drivers/char/ttyprintk.c 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Copyright (C) 2010 Samo Pogacnik 662306a36Sopenharmony_ci */ 762306a36Sopenharmony_ci 862306a36Sopenharmony_ci/* 962306a36Sopenharmony_ci * This pseudo device allows user to make printk messages. It is possible 1062306a36Sopenharmony_ci * to store "console" messages inline with kernel messages for better analyses 1162306a36Sopenharmony_ci * of the boot process, for example. 1262306a36Sopenharmony_ci */ 1362306a36Sopenharmony_ci 1462306a36Sopenharmony_ci#include <linux/console.h> 1562306a36Sopenharmony_ci#include <linux/device.h> 1662306a36Sopenharmony_ci#include <linux/serial.h> 1762306a36Sopenharmony_ci#include <linux/tty.h> 1862306a36Sopenharmony_ci#include <linux/module.h> 1962306a36Sopenharmony_ci#include <linux/spinlock.h> 2062306a36Sopenharmony_ci 2162306a36Sopenharmony_cistruct ttyprintk_port { 2262306a36Sopenharmony_ci struct tty_port port; 2362306a36Sopenharmony_ci spinlock_t spinlock; 2462306a36Sopenharmony_ci}; 2562306a36Sopenharmony_ci 2662306a36Sopenharmony_cistatic struct ttyprintk_port tpk_port; 2762306a36Sopenharmony_ci 2862306a36Sopenharmony_ci/* 2962306a36Sopenharmony_ci * Our simple preformatting supports transparent output of (time-stamped) 3062306a36Sopenharmony_ci * printk messages (also suitable for logging service): 3162306a36Sopenharmony_ci * - any cr is replaced by nl 3262306a36Sopenharmony_ci * - adds a ttyprintk source tag in front of each line 3362306a36Sopenharmony_ci * - too long message is fragmented, with '\'nl between fragments 3462306a36Sopenharmony_ci * - TPK_STR_SIZE isn't really the write_room limiting factor, because 3562306a36Sopenharmony_ci * it is emptied on the fly during preformatting. 3662306a36Sopenharmony_ci */ 3762306a36Sopenharmony_ci#define TPK_STR_SIZE 508 /* should be bigger then max expected line length */ 3862306a36Sopenharmony_ci#define TPK_MAX_ROOM 4096 /* we could assume 4K for instance */ 3962306a36Sopenharmony_ci#define TPK_PREFIX KERN_SOH __stringify(CONFIG_TTY_PRINTK_LEVEL) 4062306a36Sopenharmony_ci 4162306a36Sopenharmony_cistatic int tpk_curr; 4262306a36Sopenharmony_ci 4362306a36Sopenharmony_cistatic char tpk_buffer[TPK_STR_SIZE + 4]; 4462306a36Sopenharmony_ci 4562306a36Sopenharmony_cistatic void tpk_flush(void) 4662306a36Sopenharmony_ci{ 4762306a36Sopenharmony_ci if (tpk_curr > 0) { 4862306a36Sopenharmony_ci tpk_buffer[tpk_curr] = '\0'; 4962306a36Sopenharmony_ci printk(TPK_PREFIX "[U] %s\n", tpk_buffer); 5062306a36Sopenharmony_ci tpk_curr = 0; 5162306a36Sopenharmony_ci } 5262306a36Sopenharmony_ci} 5362306a36Sopenharmony_ci 5462306a36Sopenharmony_cistatic int tpk_printk(const u8 *buf, int count) 5562306a36Sopenharmony_ci{ 5662306a36Sopenharmony_ci int i; 5762306a36Sopenharmony_ci 5862306a36Sopenharmony_ci for (i = 0; i < count; i++) { 5962306a36Sopenharmony_ci if (tpk_curr >= TPK_STR_SIZE) { 6062306a36Sopenharmony_ci /* end of tmp buffer reached: cut the message in two */ 6162306a36Sopenharmony_ci tpk_buffer[tpk_curr++] = '\\'; 6262306a36Sopenharmony_ci tpk_flush(); 6362306a36Sopenharmony_ci } 6462306a36Sopenharmony_ci 6562306a36Sopenharmony_ci switch (buf[i]) { 6662306a36Sopenharmony_ci case '\r': 6762306a36Sopenharmony_ci tpk_flush(); 6862306a36Sopenharmony_ci if ((i + 1) < count && buf[i + 1] == '\n') 6962306a36Sopenharmony_ci i++; 7062306a36Sopenharmony_ci break; 7162306a36Sopenharmony_ci case '\n': 7262306a36Sopenharmony_ci tpk_flush(); 7362306a36Sopenharmony_ci break; 7462306a36Sopenharmony_ci default: 7562306a36Sopenharmony_ci tpk_buffer[tpk_curr++] = buf[i]; 7662306a36Sopenharmony_ci break; 7762306a36Sopenharmony_ci } 7862306a36Sopenharmony_ci } 7962306a36Sopenharmony_ci 8062306a36Sopenharmony_ci return count; 8162306a36Sopenharmony_ci} 8262306a36Sopenharmony_ci 8362306a36Sopenharmony_ci/* 8462306a36Sopenharmony_ci * TTY operations open function. 8562306a36Sopenharmony_ci */ 8662306a36Sopenharmony_cistatic int tpk_open(struct tty_struct *tty, struct file *filp) 8762306a36Sopenharmony_ci{ 8862306a36Sopenharmony_ci tty->driver_data = &tpk_port; 8962306a36Sopenharmony_ci 9062306a36Sopenharmony_ci return tty_port_open(&tpk_port.port, tty, filp); 9162306a36Sopenharmony_ci} 9262306a36Sopenharmony_ci 9362306a36Sopenharmony_ci/* 9462306a36Sopenharmony_ci * TTY operations close function. 9562306a36Sopenharmony_ci */ 9662306a36Sopenharmony_cistatic void tpk_close(struct tty_struct *tty, struct file *filp) 9762306a36Sopenharmony_ci{ 9862306a36Sopenharmony_ci struct ttyprintk_port *tpkp = tty->driver_data; 9962306a36Sopenharmony_ci 10062306a36Sopenharmony_ci tty_port_close(&tpkp->port, tty, filp); 10162306a36Sopenharmony_ci} 10262306a36Sopenharmony_ci 10362306a36Sopenharmony_ci/* 10462306a36Sopenharmony_ci * TTY operations write function. 10562306a36Sopenharmony_ci */ 10662306a36Sopenharmony_cistatic ssize_t tpk_write(struct tty_struct *tty, const u8 *buf, size_t count) 10762306a36Sopenharmony_ci{ 10862306a36Sopenharmony_ci struct ttyprintk_port *tpkp = tty->driver_data; 10962306a36Sopenharmony_ci unsigned long flags; 11062306a36Sopenharmony_ci int ret; 11162306a36Sopenharmony_ci 11262306a36Sopenharmony_ci /* exclusive use of tpk_printk within this tty */ 11362306a36Sopenharmony_ci spin_lock_irqsave(&tpkp->spinlock, flags); 11462306a36Sopenharmony_ci ret = tpk_printk(buf, count); 11562306a36Sopenharmony_ci spin_unlock_irqrestore(&tpkp->spinlock, flags); 11662306a36Sopenharmony_ci 11762306a36Sopenharmony_ci return ret; 11862306a36Sopenharmony_ci} 11962306a36Sopenharmony_ci 12062306a36Sopenharmony_ci/* 12162306a36Sopenharmony_ci * TTY operations write_room function. 12262306a36Sopenharmony_ci */ 12362306a36Sopenharmony_cistatic unsigned int tpk_write_room(struct tty_struct *tty) 12462306a36Sopenharmony_ci{ 12562306a36Sopenharmony_ci return TPK_MAX_ROOM; 12662306a36Sopenharmony_ci} 12762306a36Sopenharmony_ci 12862306a36Sopenharmony_ci/* 12962306a36Sopenharmony_ci * TTY operations hangup function. 13062306a36Sopenharmony_ci */ 13162306a36Sopenharmony_cistatic void tpk_hangup(struct tty_struct *tty) 13262306a36Sopenharmony_ci{ 13362306a36Sopenharmony_ci struct ttyprintk_port *tpkp = tty->driver_data; 13462306a36Sopenharmony_ci 13562306a36Sopenharmony_ci tty_port_hangup(&tpkp->port); 13662306a36Sopenharmony_ci} 13762306a36Sopenharmony_ci 13862306a36Sopenharmony_ci/* 13962306a36Sopenharmony_ci * TTY port operations shutdown function. 14062306a36Sopenharmony_ci */ 14162306a36Sopenharmony_cistatic void tpk_port_shutdown(struct tty_port *tport) 14262306a36Sopenharmony_ci{ 14362306a36Sopenharmony_ci struct ttyprintk_port *tpkp = 14462306a36Sopenharmony_ci container_of(tport, struct ttyprintk_port, port); 14562306a36Sopenharmony_ci unsigned long flags; 14662306a36Sopenharmony_ci 14762306a36Sopenharmony_ci spin_lock_irqsave(&tpkp->spinlock, flags); 14862306a36Sopenharmony_ci tpk_flush(); 14962306a36Sopenharmony_ci spin_unlock_irqrestore(&tpkp->spinlock, flags); 15062306a36Sopenharmony_ci} 15162306a36Sopenharmony_ci 15262306a36Sopenharmony_cistatic const struct tty_operations ttyprintk_ops = { 15362306a36Sopenharmony_ci .open = tpk_open, 15462306a36Sopenharmony_ci .close = tpk_close, 15562306a36Sopenharmony_ci .write = tpk_write, 15662306a36Sopenharmony_ci .write_room = tpk_write_room, 15762306a36Sopenharmony_ci .hangup = tpk_hangup, 15862306a36Sopenharmony_ci}; 15962306a36Sopenharmony_ci 16062306a36Sopenharmony_cistatic const struct tty_port_operations tpk_port_ops = { 16162306a36Sopenharmony_ci .shutdown = tpk_port_shutdown, 16262306a36Sopenharmony_ci}; 16362306a36Sopenharmony_ci 16462306a36Sopenharmony_cistatic struct tty_driver *ttyprintk_driver; 16562306a36Sopenharmony_ci 16662306a36Sopenharmony_cistatic struct tty_driver *ttyprintk_console_device(struct console *c, 16762306a36Sopenharmony_ci int *index) 16862306a36Sopenharmony_ci{ 16962306a36Sopenharmony_ci *index = 0; 17062306a36Sopenharmony_ci return ttyprintk_driver; 17162306a36Sopenharmony_ci} 17262306a36Sopenharmony_ci 17362306a36Sopenharmony_cistatic struct console ttyprintk_console = { 17462306a36Sopenharmony_ci .name = "ttyprintk", 17562306a36Sopenharmony_ci .device = ttyprintk_console_device, 17662306a36Sopenharmony_ci}; 17762306a36Sopenharmony_ci 17862306a36Sopenharmony_cistatic int __init ttyprintk_init(void) 17962306a36Sopenharmony_ci{ 18062306a36Sopenharmony_ci int ret; 18162306a36Sopenharmony_ci 18262306a36Sopenharmony_ci spin_lock_init(&tpk_port.spinlock); 18362306a36Sopenharmony_ci 18462306a36Sopenharmony_ci ttyprintk_driver = tty_alloc_driver(1, 18562306a36Sopenharmony_ci TTY_DRIVER_RESET_TERMIOS | 18662306a36Sopenharmony_ci TTY_DRIVER_REAL_RAW | 18762306a36Sopenharmony_ci TTY_DRIVER_UNNUMBERED_NODE); 18862306a36Sopenharmony_ci if (IS_ERR(ttyprintk_driver)) 18962306a36Sopenharmony_ci return PTR_ERR(ttyprintk_driver); 19062306a36Sopenharmony_ci 19162306a36Sopenharmony_ci tty_port_init(&tpk_port.port); 19262306a36Sopenharmony_ci tpk_port.port.ops = &tpk_port_ops; 19362306a36Sopenharmony_ci 19462306a36Sopenharmony_ci ttyprintk_driver->driver_name = "ttyprintk"; 19562306a36Sopenharmony_ci ttyprintk_driver->name = "ttyprintk"; 19662306a36Sopenharmony_ci ttyprintk_driver->major = TTYAUX_MAJOR; 19762306a36Sopenharmony_ci ttyprintk_driver->minor_start = 3; 19862306a36Sopenharmony_ci ttyprintk_driver->type = TTY_DRIVER_TYPE_CONSOLE; 19962306a36Sopenharmony_ci ttyprintk_driver->init_termios = tty_std_termios; 20062306a36Sopenharmony_ci ttyprintk_driver->init_termios.c_oflag = OPOST | OCRNL | ONOCR | ONLRET; 20162306a36Sopenharmony_ci tty_set_operations(ttyprintk_driver, &ttyprintk_ops); 20262306a36Sopenharmony_ci tty_port_link_device(&tpk_port.port, ttyprintk_driver, 0); 20362306a36Sopenharmony_ci 20462306a36Sopenharmony_ci ret = tty_register_driver(ttyprintk_driver); 20562306a36Sopenharmony_ci if (ret < 0) { 20662306a36Sopenharmony_ci printk(KERN_ERR "Couldn't register ttyprintk driver\n"); 20762306a36Sopenharmony_ci goto error; 20862306a36Sopenharmony_ci } 20962306a36Sopenharmony_ci 21062306a36Sopenharmony_ci register_console(&ttyprintk_console); 21162306a36Sopenharmony_ci 21262306a36Sopenharmony_ci return 0; 21362306a36Sopenharmony_ci 21462306a36Sopenharmony_cierror: 21562306a36Sopenharmony_ci tty_driver_kref_put(ttyprintk_driver); 21662306a36Sopenharmony_ci tty_port_destroy(&tpk_port.port); 21762306a36Sopenharmony_ci return ret; 21862306a36Sopenharmony_ci} 21962306a36Sopenharmony_ci 22062306a36Sopenharmony_cistatic void __exit ttyprintk_exit(void) 22162306a36Sopenharmony_ci{ 22262306a36Sopenharmony_ci unregister_console(&ttyprintk_console); 22362306a36Sopenharmony_ci tty_unregister_driver(ttyprintk_driver); 22462306a36Sopenharmony_ci tty_driver_kref_put(ttyprintk_driver); 22562306a36Sopenharmony_ci tty_port_destroy(&tpk_port.port); 22662306a36Sopenharmony_ci} 22762306a36Sopenharmony_ci 22862306a36Sopenharmony_cidevice_initcall(ttyprintk_init); 22962306a36Sopenharmony_cimodule_exit(ttyprintk_exit); 23062306a36Sopenharmony_ci 23162306a36Sopenharmony_ciMODULE_LICENSE("GPL"); 232