1e3b3d0f5SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 296fd7ce5SGreg Kroah-Hartman /* 396fd7ce5SGreg Kroah-Hartman * Copyright (C) 1991, 1992 Linus Torvalds 496fd7ce5SGreg Kroah-Hartman */ 596fd7ce5SGreg Kroah-Hartman 696fd7ce5SGreg Kroah-Hartman /* 796fd7ce5SGreg Kroah-Hartman * 'tty_io.c' gives an orthogonal feeling to tty's, be they consoles 896fd7ce5SGreg Kroah-Hartman * or rs-channels. It also implements echoing, cooked mode etc. 996fd7ce5SGreg Kroah-Hartman * 1096fd7ce5SGreg Kroah-Hartman * Kill-line thanks to John T Kohl, who also corrected VMIN = VTIME = 0. 1196fd7ce5SGreg Kroah-Hartman * 1296fd7ce5SGreg Kroah-Hartman * Modified by Theodore Ts'o, 9/14/92, to dynamically allocate the 1396fd7ce5SGreg Kroah-Hartman * tty_struct and tty_queue structures. Previously there was an array 1496fd7ce5SGreg Kroah-Hartman * of 256 tty_struct's which was statically allocated, and the 1596fd7ce5SGreg Kroah-Hartman * tty_queue structures were allocated at boot time. Both are now 1696fd7ce5SGreg Kroah-Hartman * dynamically allocated only when the tty is open. 1796fd7ce5SGreg Kroah-Hartman * 1896fd7ce5SGreg Kroah-Hartman * Also restructured routines so that there is more of a separation 1996fd7ce5SGreg Kroah-Hartman * between the high-level tty routines (tty_io.c and tty_ioctl.c) and 2096fd7ce5SGreg Kroah-Hartman * the low-level tty routines (serial.c, pty.c, console.c). This 2196fd7ce5SGreg Kroah-Hartman * makes for cleaner and more compact code. -TYT, 9/17/92 2296fd7ce5SGreg Kroah-Hartman * 2396fd7ce5SGreg Kroah-Hartman * Modified by Fred N. van Kempen, 01/29/93, to add line disciplines 2496fd7ce5SGreg Kroah-Hartman * which can be dynamically activated and de-activated by the line 2596fd7ce5SGreg Kroah-Hartman * discipline handling modules (like SLIP). 2696fd7ce5SGreg Kroah-Hartman * 2796fd7ce5SGreg Kroah-Hartman * NOTE: pay no attention to the line discipline code (yet); its 2896fd7ce5SGreg Kroah-Hartman * interface is still subject to change in this version... 2996fd7ce5SGreg Kroah-Hartman * -- TYT, 1/31/92 3096fd7ce5SGreg Kroah-Hartman * 3196fd7ce5SGreg Kroah-Hartman * Added functionality to the OPOST tty handling. No delays, but all 3296fd7ce5SGreg Kroah-Hartman * other bits should be there. 3396fd7ce5SGreg Kroah-Hartman * -- Nick Holloway <alfie@dcs.warwick.ac.uk>, 27th May 1993. 3496fd7ce5SGreg Kroah-Hartman * 3596fd7ce5SGreg Kroah-Hartman * Rewrote canonical mode and added more termios flags. 3696fd7ce5SGreg Kroah-Hartman * -- julian@uhunix.uhcc.hawaii.edu (J. Cowley), 13Jan94 3796fd7ce5SGreg Kroah-Hartman * 3896fd7ce5SGreg Kroah-Hartman * Reorganized FASYNC support so mouse code can share it. 3996fd7ce5SGreg Kroah-Hartman * -- ctm@ardi.com, 9Sep95 4096fd7ce5SGreg Kroah-Hartman * 4196fd7ce5SGreg Kroah-Hartman * New TIOCLINUX variants added. 4296fd7ce5SGreg Kroah-Hartman * -- mj@k332.feld.cvut.cz, 19-Nov-95 4396fd7ce5SGreg Kroah-Hartman * 4496fd7ce5SGreg Kroah-Hartman * Restrict vt switching via ioctl() 4596fd7ce5SGreg Kroah-Hartman * -- grif@cs.ucr.edu, 5-Dec-95 4696fd7ce5SGreg Kroah-Hartman * 4796fd7ce5SGreg Kroah-Hartman * Move console and virtual terminal code to more appropriate files, 4896fd7ce5SGreg Kroah-Hartman * implement CONFIG_VT and generalize console device interface. 4996fd7ce5SGreg Kroah-Hartman * -- Marko Kohtala <Marko.Kohtala@hut.fi>, March 97 5096fd7ce5SGreg Kroah-Hartman * 5196fd7ce5SGreg Kroah-Hartman * Rewrote tty_init_dev and tty_release_dev to eliminate races. 5296fd7ce5SGreg Kroah-Hartman * -- Bill Hawes <whawes@star.net>, June 97 5396fd7ce5SGreg Kroah-Hartman * 5496fd7ce5SGreg Kroah-Hartman * Added devfs support. 5596fd7ce5SGreg Kroah-Hartman * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 13-Jan-1998 5696fd7ce5SGreg Kroah-Hartman * 5796fd7ce5SGreg Kroah-Hartman * Added support for a Unix98-style ptmx device. 5896fd7ce5SGreg Kroah-Hartman * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998 5996fd7ce5SGreg Kroah-Hartman * 6096fd7ce5SGreg Kroah-Hartman * Reduced memory usage for older ARM systems 6196fd7ce5SGreg Kroah-Hartman * -- Russell King <rmk@arm.linux.org.uk> 6296fd7ce5SGreg Kroah-Hartman * 6396fd7ce5SGreg Kroah-Hartman * Move do_SAK() into process context. Less stack use in devfs functions. 6496fd7ce5SGreg Kroah-Hartman * alloc_tty_struct() always uses kmalloc() 6596fd7ce5SGreg Kroah-Hartman * -- Andrew Morton <andrewm@uow.edu.eu> 17Mar01 6696fd7ce5SGreg Kroah-Hartman */ 6796fd7ce5SGreg Kroah-Hartman 6896fd7ce5SGreg Kroah-Hartman #include <linux/types.h> 6996fd7ce5SGreg Kroah-Hartman #include <linux/major.h> 7096fd7ce5SGreg Kroah-Hartman #include <linux/errno.h> 7196fd7ce5SGreg Kroah-Hartman #include <linux/signal.h> 7296fd7ce5SGreg Kroah-Hartman #include <linux/fcntl.h> 733f07c014SIngo Molnar #include <linux/sched/signal.h> 7429930025SIngo Molnar #include <linux/sched/task.h> 7596fd7ce5SGreg Kroah-Hartman #include <linux/interrupt.h> 7696fd7ce5SGreg Kroah-Hartman #include <linux/tty.h> 7796fd7ce5SGreg Kroah-Hartman #include <linux/tty_driver.h> 7896fd7ce5SGreg Kroah-Hartman #include <linux/tty_flip.h> 7996fd7ce5SGreg Kroah-Hartman #include <linux/devpts_fs.h> 8096fd7ce5SGreg Kroah-Hartman #include <linux/file.h> 8196fd7ce5SGreg Kroah-Hartman #include <linux/fdtable.h> 8296fd7ce5SGreg Kroah-Hartman #include <linux/console.h> 8396fd7ce5SGreg Kroah-Hartman #include <linux/timer.h> 8496fd7ce5SGreg Kroah-Hartman #include <linux/ctype.h> 8596fd7ce5SGreg Kroah-Hartman #include <linux/kd.h> 8696fd7ce5SGreg Kroah-Hartman #include <linux/mm.h> 8796fd7ce5SGreg Kroah-Hartman #include <linux/string.h> 8896fd7ce5SGreg Kroah-Hartman #include <linux/slab.h> 8996fd7ce5SGreg Kroah-Hartman #include <linux/poll.h> 90b7aff093SArnd Bergmann #include <linux/ppp-ioctl.h> 9196fd7ce5SGreg Kroah-Hartman #include <linux/proc_fs.h> 9296fd7ce5SGreg Kroah-Hartman #include <linux/init.h> 9396fd7ce5SGreg Kroah-Hartman #include <linux/module.h> 9496fd7ce5SGreg Kroah-Hartman #include <linux/device.h> 9596fd7ce5SGreg Kroah-Hartman #include <linux/wait.h> 9696fd7ce5SGreg Kroah-Hartman #include <linux/bitops.h> 9796fd7ce5SGreg Kroah-Hartman #include <linux/delay.h> 9896fd7ce5SGreg Kroah-Hartman #include <linux/seq_file.h> 9996fd7ce5SGreg Kroah-Hartman #include <linux/serial.h> 1005a3c6b25SManuel Zerpies #include <linux/ratelimit.h> 101e2112038SAl Viro #include <linux/compat.h> 10296fd7ce5SGreg Kroah-Hartman 10396fd7ce5SGreg Kroah-Hartman #include <linux/uaccess.h> 10496fd7ce5SGreg Kroah-Hartman 10596fd7ce5SGreg Kroah-Hartman #include <linux/kbd_kern.h> 10696fd7ce5SGreg Kroah-Hartman #include <linux/vt_kern.h> 10796fd7ce5SGreg Kroah-Hartman #include <linux/selection.h> 10896fd7ce5SGreg Kroah-Hartman 10996fd7ce5SGreg Kroah-Hartman #include <linux/kmod.h> 11096fd7ce5SGreg Kroah-Hartman #include <linux/nsproxy.h> 11196fd7ce5SGreg Kroah-Hartman 11296fd7ce5SGreg Kroah-Hartman #undef TTY_DEBUG_HANGUP 113accff793SPeter Hurley #ifdef TTY_DEBUG_HANGUP 114accff793SPeter Hurley # define tty_debug_hangup(tty, f, args...) tty_debug(tty, f, ##args) 115accff793SPeter Hurley #else 116accff793SPeter Hurley # define tty_debug_hangup(tty, f, args...) do { } while (0) 117accff793SPeter Hurley #endif 11896fd7ce5SGreg Kroah-Hartman 11996fd7ce5SGreg Kroah-Hartman #define TTY_PARANOIA_CHECK 1 12096fd7ce5SGreg Kroah-Hartman #define CHECK_TTY_COUNT 1 12196fd7ce5SGreg Kroah-Hartman 12296fd7ce5SGreg Kroah-Hartman struct ktermios tty_std_termios = { /* for the benefit of tty drivers */ 12396fd7ce5SGreg Kroah-Hartman .c_iflag = ICRNL | IXON, 12496fd7ce5SGreg Kroah-Hartman .c_oflag = OPOST | ONLCR, 12596fd7ce5SGreg Kroah-Hartman .c_cflag = B38400 | CS8 | CREAD | HUPCL, 12696fd7ce5SGreg Kroah-Hartman .c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK | 12796fd7ce5SGreg Kroah-Hartman ECHOCTL | ECHOKE | IEXTEN, 12896fd7ce5SGreg Kroah-Hartman .c_cc = INIT_C_CC, 12996fd7ce5SGreg Kroah-Hartman .c_ispeed = 38400, 130133b1306SPeter Hurley .c_ospeed = 38400, 131133b1306SPeter Hurley /* .c_line = N_TTY, */ 13296fd7ce5SGreg Kroah-Hartman }; 13396fd7ce5SGreg Kroah-Hartman 13496fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_std_termios); 13596fd7ce5SGreg Kroah-Hartman 13696fd7ce5SGreg Kroah-Hartman /* This list gets poked at by procfs and various bits of boot up code. This 13796fd7ce5SGreg Kroah-Hartman could do with some rationalisation such as pulling the tty proc function 13896fd7ce5SGreg Kroah-Hartman into this file */ 13996fd7ce5SGreg Kroah-Hartman 14096fd7ce5SGreg Kroah-Hartman LIST_HEAD(tty_drivers); /* linked list of tty drivers */ 14196fd7ce5SGreg Kroah-Hartman 142d1d027efSPeter Hurley /* Mutex to protect creating and releasing a tty */ 14396fd7ce5SGreg Kroah-Hartman DEFINE_MUTEX(tty_mutex); 14496fd7ce5SGreg Kroah-Hartman 145dd78b0c4SLinus Torvalds static ssize_t tty_read(struct kiocb *, struct iov_iter *); 146*9bb48c82SLinus Torvalds static ssize_t tty_write(struct kiocb *, struct iov_iter *); 147*9bb48c82SLinus Torvalds ssize_t redirected_tty_write(struct kiocb *, struct iov_iter *); 148afc9a42bSAl Viro static __poll_t tty_poll(struct file *, poll_table *); 14996fd7ce5SGreg Kroah-Hartman static int tty_open(struct inode *, struct file *); 15096fd7ce5SGreg Kroah-Hartman long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg); 15196fd7ce5SGreg Kroah-Hartman #ifdef CONFIG_COMPAT 15296fd7ce5SGreg Kroah-Hartman static long tty_compat_ioctl(struct file *file, unsigned int cmd, 15396fd7ce5SGreg Kroah-Hartman unsigned long arg); 15496fd7ce5SGreg Kroah-Hartman #else 15596fd7ce5SGreg Kroah-Hartman #define tty_compat_ioctl NULL 15696fd7ce5SGreg Kroah-Hartman #endif 15796fd7ce5SGreg Kroah-Hartman static int __tty_fasync(int fd, struct file *filp, int on); 15896fd7ce5SGreg Kroah-Hartman static int tty_fasync(int fd, struct file *filp, int on); 15996fd7ce5SGreg Kroah-Hartman static void release_tty(struct tty_struct *tty, int idx); 16096fd7ce5SGreg Kroah-Hartman 16196fd7ce5SGreg Kroah-Hartman /** 16296fd7ce5SGreg Kroah-Hartman * free_tty_struct - free a disused tty 16396fd7ce5SGreg Kroah-Hartman * @tty: tty struct to free 16496fd7ce5SGreg Kroah-Hartman * 16596fd7ce5SGreg Kroah-Hartman * Free the write buffers, tty queue and tty memory itself. 16696fd7ce5SGreg Kroah-Hartman * 16796fd7ce5SGreg Kroah-Hartman * Locking: none. Must be called after tty is definitely unused 16896fd7ce5SGreg Kroah-Hartman */ 16996fd7ce5SGreg Kroah-Hartman 170a3123fd0SPeter Hurley static void free_tty_struct(struct tty_struct *tty) 17196fd7ce5SGreg Kroah-Hartman { 172c8b710b3SPeter Hurley tty_ldisc_deinit(tty); 17396fd7ce5SGreg Kroah-Hartman put_device(tty->dev); 17496fd7ce5SGreg Kroah-Hartman kfree(tty->write_buf); 17589c8d91eSAlan Cox tty->magic = 0xDEADDEAD; 17696fd7ce5SGreg Kroah-Hartman kfree(tty); 17796fd7ce5SGreg Kroah-Hartman } 17896fd7ce5SGreg Kroah-Hartman 17996fd7ce5SGreg Kroah-Hartman static inline struct tty_struct *file_tty(struct file *file) 18096fd7ce5SGreg Kroah-Hartman { 18196fd7ce5SGreg Kroah-Hartman return ((struct tty_file_private *)file->private_data)->tty; 18296fd7ce5SGreg Kroah-Hartman } 18396fd7ce5SGreg Kroah-Hartman 184fa90e1c9SJiri Slaby int tty_alloc_file(struct file *file) 18596fd7ce5SGreg Kroah-Hartman { 18696fd7ce5SGreg Kroah-Hartman struct tty_file_private *priv; 18796fd7ce5SGreg Kroah-Hartman 18896fd7ce5SGreg Kroah-Hartman priv = kmalloc(sizeof(*priv), GFP_KERNEL); 18996fd7ce5SGreg Kroah-Hartman if (!priv) 19096fd7ce5SGreg Kroah-Hartman return -ENOMEM; 19196fd7ce5SGreg Kroah-Hartman 192fa90e1c9SJiri Slaby file->private_data = priv; 193fa90e1c9SJiri Slaby 194fa90e1c9SJiri Slaby return 0; 195fa90e1c9SJiri Slaby } 196fa90e1c9SJiri Slaby 197fa90e1c9SJiri Slaby /* Associate a new file with the tty structure */ 198fa90e1c9SJiri Slaby void tty_add_file(struct tty_struct *tty, struct file *file) 199fa90e1c9SJiri Slaby { 200fa90e1c9SJiri Slaby struct tty_file_private *priv = file->private_data; 201fa90e1c9SJiri Slaby 20296fd7ce5SGreg Kroah-Hartman priv->tty = tty; 20396fd7ce5SGreg Kroah-Hartman priv->file = file; 20496fd7ce5SGreg Kroah-Hartman 2054a510969SPeter Hurley spin_lock(&tty->files_lock); 20696fd7ce5SGreg Kroah-Hartman list_add(&priv->list, &tty->tty_files); 2074a510969SPeter Hurley spin_unlock(&tty->files_lock); 208fa90e1c9SJiri Slaby } 20996fd7ce5SGreg Kroah-Hartman 210fa90e1c9SJiri Slaby /** 211fa90e1c9SJiri Slaby * tty_free_file - free file->private_data 212fa90e1c9SJiri Slaby * 213fa90e1c9SJiri Slaby * This shall be used only for fail path handling when tty_add_file was not 214fa90e1c9SJiri Slaby * called yet. 215fa90e1c9SJiri Slaby */ 216fa90e1c9SJiri Slaby void tty_free_file(struct file *file) 217fa90e1c9SJiri Slaby { 218fa90e1c9SJiri Slaby struct tty_file_private *priv = file->private_data; 219fa90e1c9SJiri Slaby 220fa90e1c9SJiri Slaby file->private_data = NULL; 221fa90e1c9SJiri Slaby kfree(priv); 22296fd7ce5SGreg Kroah-Hartman } 22396fd7ce5SGreg Kroah-Hartman 22496fd7ce5SGreg Kroah-Hartman /* Delete file from its tty */ 2252520e274SJosh Triplett static void tty_del_file(struct file *file) 22696fd7ce5SGreg Kroah-Hartman { 22796fd7ce5SGreg Kroah-Hartman struct tty_file_private *priv = file->private_data; 2284a510969SPeter Hurley struct tty_struct *tty = priv->tty; 22996fd7ce5SGreg Kroah-Hartman 2304a510969SPeter Hurley spin_lock(&tty->files_lock); 23196fd7ce5SGreg Kroah-Hartman list_del(&priv->list); 2324a510969SPeter Hurley spin_unlock(&tty->files_lock); 233fa90e1c9SJiri Slaby tty_free_file(file); 23496fd7ce5SGreg Kroah-Hartman } 23596fd7ce5SGreg Kroah-Hartman 23696fd7ce5SGreg Kroah-Hartman /** 23796fd7ce5SGreg Kroah-Hartman * tty_name - return tty naming 23896fd7ce5SGreg Kroah-Hartman * @tty: tty structure 23996fd7ce5SGreg Kroah-Hartman * 24096fd7ce5SGreg Kroah-Hartman * Convert a tty structure into a name. The name reflects the kernel 24196fd7ce5SGreg Kroah-Hartman * naming policy and if udev is in use may not reflect user space 24296fd7ce5SGreg Kroah-Hartman * 24396fd7ce5SGreg Kroah-Hartman * Locking: none 24496fd7ce5SGreg Kroah-Hartman */ 24596fd7ce5SGreg Kroah-Hartman 246429b4749SRasmus Villemoes const char *tty_name(const struct tty_struct *tty) 24796fd7ce5SGreg Kroah-Hartman { 24896fd7ce5SGreg Kroah-Hartman if (!tty) /* Hmm. NULL pointer. That's fun. */ 249917162c9SRasmus Villemoes return "NULL tty"; 250917162c9SRasmus Villemoes return tty->name; 25196fd7ce5SGreg Kroah-Hartman } 25296fd7ce5SGreg Kroah-Hartman 25396fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_name); 25496fd7ce5SGreg Kroah-Hartman 2550a083eddSPeter Hurley const char *tty_driver_name(const struct tty_struct *tty) 2560a083eddSPeter Hurley { 2570a083eddSPeter Hurley if (!tty || !tty->driver) 2580a083eddSPeter Hurley return ""; 2590a083eddSPeter Hurley return tty->driver->name; 2600a083eddSPeter Hurley } 2610a083eddSPeter Hurley 26282b8f888SPeter Hurley static int tty_paranoia_check(struct tty_struct *tty, struct inode *inode, 26396fd7ce5SGreg Kroah-Hartman const char *routine) 26496fd7ce5SGreg Kroah-Hartman { 26596fd7ce5SGreg Kroah-Hartman #ifdef TTY_PARANOIA_CHECK 26696fd7ce5SGreg Kroah-Hartman if (!tty) { 26789222e62SPeter Hurley pr_warn("(%d:%d): %s: NULL tty\n", 26896fd7ce5SGreg Kroah-Hartman imajor(inode), iminor(inode), routine); 26996fd7ce5SGreg Kroah-Hartman return 1; 27096fd7ce5SGreg Kroah-Hartman } 27196fd7ce5SGreg Kroah-Hartman if (tty->magic != TTY_MAGIC) { 27289222e62SPeter Hurley pr_warn("(%d:%d): %s: bad magic number\n", 27396fd7ce5SGreg Kroah-Hartman imajor(inode), iminor(inode), routine); 27496fd7ce5SGreg Kroah-Hartman return 1; 27596fd7ce5SGreg Kroah-Hartman } 27696fd7ce5SGreg Kroah-Hartman #endif 27796fd7ce5SGreg Kroah-Hartman return 0; 27896fd7ce5SGreg Kroah-Hartman } 27996fd7ce5SGreg Kroah-Hartman 280deb287e7SPeter Hurley /* Caller must hold tty_lock */ 28196fd7ce5SGreg Kroah-Hartman static int check_tty_count(struct tty_struct *tty, const char *routine) 28296fd7ce5SGreg Kroah-Hartman { 28396fd7ce5SGreg Kroah-Hartman #ifdef CHECK_TTY_COUNT 28496fd7ce5SGreg Kroah-Hartman struct list_head *p; 285a09ac397SOkash Khawaja int count = 0, kopen_count = 0; 28696fd7ce5SGreg Kroah-Hartman 2874a510969SPeter Hurley spin_lock(&tty->files_lock); 28896fd7ce5SGreg Kroah-Hartman list_for_each(p, &tty->tty_files) { 28996fd7ce5SGreg Kroah-Hartman count++; 29096fd7ce5SGreg Kroah-Hartman } 2914a510969SPeter Hurley spin_unlock(&tty->files_lock); 29296fd7ce5SGreg Kroah-Hartman if (tty->driver->type == TTY_DRIVER_TYPE_PTY && 29396fd7ce5SGreg Kroah-Hartman tty->driver->subtype == PTY_TYPE_SLAVE && 29496fd7ce5SGreg Kroah-Hartman tty->link && tty->link->count) 29596fd7ce5SGreg Kroah-Hartman count++; 296a09ac397SOkash Khawaja if (tty_port_kopened(tty->port)) 297a09ac397SOkash Khawaja kopen_count++; 298a09ac397SOkash Khawaja if (tty->count != (count + kopen_count)) { 299a09ac397SOkash Khawaja tty_warn(tty, "%s: tty->count(%d) != (#fd's(%d) + #kopen's(%d))\n", 300a09ac397SOkash Khawaja routine, tty->count, count, kopen_count); 301a09ac397SOkash Khawaja return (count + kopen_count); 30296fd7ce5SGreg Kroah-Hartman } 30396fd7ce5SGreg Kroah-Hartman #endif 30496fd7ce5SGreg Kroah-Hartman return 0; 30596fd7ce5SGreg Kroah-Hartman } 30696fd7ce5SGreg Kroah-Hartman 30796fd7ce5SGreg Kroah-Hartman /** 30896fd7ce5SGreg Kroah-Hartman * get_tty_driver - find device of a tty 309fa441954SJiri Slaby * @device: device identifier 31096fd7ce5SGreg Kroah-Hartman * @index: returns the index of the tty 31196fd7ce5SGreg Kroah-Hartman * 31296fd7ce5SGreg Kroah-Hartman * This routine returns a tty driver structure, given a device number 31396fd7ce5SGreg Kroah-Hartman * and also passes back the index number. 31496fd7ce5SGreg Kroah-Hartman * 31596fd7ce5SGreg Kroah-Hartman * Locking: caller must hold tty_mutex 31696fd7ce5SGreg Kroah-Hartman */ 31796fd7ce5SGreg Kroah-Hartman 31896fd7ce5SGreg Kroah-Hartman static struct tty_driver *get_tty_driver(dev_t device, int *index) 31996fd7ce5SGreg Kroah-Hartman { 32096fd7ce5SGreg Kroah-Hartman struct tty_driver *p; 32196fd7ce5SGreg Kroah-Hartman 32296fd7ce5SGreg Kroah-Hartman list_for_each_entry(p, &tty_drivers, tty_drivers) { 32396fd7ce5SGreg Kroah-Hartman dev_t base = MKDEV(p->major, p->minor_start); 32496fd7ce5SGreg Kroah-Hartman if (device < base || device >= base + p->num) 32596fd7ce5SGreg Kroah-Hartman continue; 32696fd7ce5SGreg Kroah-Hartman *index = device - base; 32796fd7ce5SGreg Kroah-Hartman return tty_driver_kref_get(p); 32896fd7ce5SGreg Kroah-Hartman } 32996fd7ce5SGreg Kroah-Hartman return NULL; 33096fd7ce5SGreg Kroah-Hartman } 33196fd7ce5SGreg Kroah-Hartman 332fc61ed51SOkash Khawaja /** 333fc61ed51SOkash Khawaja * tty_dev_name_to_number - return dev_t for device name 334fc61ed51SOkash Khawaja * @name: user space name of device under /dev 335fc61ed51SOkash Khawaja * @number: pointer to dev_t that this function will populate 336fc61ed51SOkash Khawaja * 337fc61ed51SOkash Khawaja * This function converts device names like ttyS0 or ttyUSB1 into dev_t 338fc61ed51SOkash Khawaja * like (4, 64) or (188, 1). If no corresponding driver is registered then 339fc61ed51SOkash Khawaja * the function returns -ENODEV. 340fc61ed51SOkash Khawaja * 341fc61ed51SOkash Khawaja * Locking: this acquires tty_mutex to protect the tty_drivers list from 342fc61ed51SOkash Khawaja * being modified while we are traversing it, and makes sure to 343fc61ed51SOkash Khawaja * release it before exiting. 344fc61ed51SOkash Khawaja */ 345fc61ed51SOkash Khawaja int tty_dev_name_to_number(const char *name, dev_t *number) 346fc61ed51SOkash Khawaja { 347fc61ed51SOkash Khawaja struct tty_driver *p; 348fc61ed51SOkash Khawaja int ret; 349fc61ed51SOkash Khawaja int index, prefix_length = 0; 350fc61ed51SOkash Khawaja const char *str; 351fc61ed51SOkash Khawaja 352fc61ed51SOkash Khawaja for (str = name; *str && !isdigit(*str); str++) 353fc61ed51SOkash Khawaja ; 354fc61ed51SOkash Khawaja 355fc61ed51SOkash Khawaja if (!*str) 356fc61ed51SOkash Khawaja return -EINVAL; 357fc61ed51SOkash Khawaja 358fc61ed51SOkash Khawaja ret = kstrtoint(str, 10, &index); 359fc61ed51SOkash Khawaja if (ret) 360fc61ed51SOkash Khawaja return ret; 361fc61ed51SOkash Khawaja 362fc61ed51SOkash Khawaja prefix_length = str - name; 363fc61ed51SOkash Khawaja mutex_lock(&tty_mutex); 364fc61ed51SOkash Khawaja 365fc61ed51SOkash Khawaja list_for_each_entry(p, &tty_drivers, tty_drivers) 366fc61ed51SOkash Khawaja if (prefix_length == strlen(p->name) && strncmp(name, 367fc61ed51SOkash Khawaja p->name, prefix_length) == 0) { 368fc61ed51SOkash Khawaja if (index < p->num) { 369fc61ed51SOkash Khawaja *number = MKDEV(p->major, p->minor_start + index); 370fc61ed51SOkash Khawaja goto out; 371fc61ed51SOkash Khawaja } 372fc61ed51SOkash Khawaja } 373fc61ed51SOkash Khawaja 374fc61ed51SOkash Khawaja /* if here then driver wasn't found */ 375fc61ed51SOkash Khawaja ret = -ENODEV; 376fc61ed51SOkash Khawaja out: 377fc61ed51SOkash Khawaja mutex_unlock(&tty_mutex); 378fc61ed51SOkash Khawaja return ret; 379fc61ed51SOkash Khawaja } 380fc61ed51SOkash Khawaja EXPORT_SYMBOL_GPL(tty_dev_name_to_number); 381fc61ed51SOkash Khawaja 38296fd7ce5SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL 38396fd7ce5SGreg Kroah-Hartman 38496fd7ce5SGreg Kroah-Hartman /** 38596fd7ce5SGreg Kroah-Hartman * tty_find_polling_driver - find device of a polled tty 38696fd7ce5SGreg Kroah-Hartman * @name: name string to match 38796fd7ce5SGreg Kroah-Hartman * @line: pointer to resulting tty line nr 38896fd7ce5SGreg Kroah-Hartman * 38996fd7ce5SGreg Kroah-Hartman * This routine returns a tty driver structure, given a name 39096fd7ce5SGreg Kroah-Hartman * and the condition that the tty driver is capable of polled 39196fd7ce5SGreg Kroah-Hartman * operation. 39296fd7ce5SGreg Kroah-Hartman */ 39396fd7ce5SGreg Kroah-Hartman struct tty_driver *tty_find_polling_driver(char *name, int *line) 39496fd7ce5SGreg Kroah-Hartman { 39596fd7ce5SGreg Kroah-Hartman struct tty_driver *p, *res = NULL; 39696fd7ce5SGreg Kroah-Hartman int tty_line = 0; 39796fd7ce5SGreg Kroah-Hartman int len; 39896fd7ce5SGreg Kroah-Hartman char *str, *stp; 39996fd7ce5SGreg Kroah-Hartman 40096fd7ce5SGreg Kroah-Hartman for (str = name; *str; str++) 40196fd7ce5SGreg Kroah-Hartman if ((*str >= '0' && *str <= '9') || *str == ',') 40296fd7ce5SGreg Kroah-Hartman break; 40396fd7ce5SGreg Kroah-Hartman if (!*str) 40496fd7ce5SGreg Kroah-Hartman return NULL; 40596fd7ce5SGreg Kroah-Hartman 40696fd7ce5SGreg Kroah-Hartman len = str - name; 40796fd7ce5SGreg Kroah-Hartman tty_line = simple_strtoul(str, &str, 10); 40896fd7ce5SGreg Kroah-Hartman 40996fd7ce5SGreg Kroah-Hartman mutex_lock(&tty_mutex); 41096fd7ce5SGreg Kroah-Hartman /* Search through the tty devices to look for a match */ 41196fd7ce5SGreg Kroah-Hartman list_for_each_entry(p, &tty_drivers, tty_drivers) { 41233a1a7beSMiles Chen if (!len || strncmp(name, p->name, len) != 0) 41396fd7ce5SGreg Kroah-Hartman continue; 41496fd7ce5SGreg Kroah-Hartman stp = str; 41596fd7ce5SGreg Kroah-Hartman if (*stp == ',') 41696fd7ce5SGreg Kroah-Hartman stp++; 41796fd7ce5SGreg Kroah-Hartman if (*stp == '\0') 41896fd7ce5SGreg Kroah-Hartman stp = NULL; 41996fd7ce5SGreg Kroah-Hartman 42096fd7ce5SGreg Kroah-Hartman if (tty_line >= 0 && tty_line < p->num && p->ops && 42196fd7ce5SGreg Kroah-Hartman p->ops->poll_init && !p->ops->poll_init(p, tty_line, stp)) { 42296fd7ce5SGreg Kroah-Hartman res = tty_driver_kref_get(p); 42396fd7ce5SGreg Kroah-Hartman *line = tty_line; 42496fd7ce5SGreg Kroah-Hartman break; 42596fd7ce5SGreg Kroah-Hartman } 42696fd7ce5SGreg Kroah-Hartman } 42796fd7ce5SGreg Kroah-Hartman mutex_unlock(&tty_mutex); 42896fd7ce5SGreg Kroah-Hartman 42996fd7ce5SGreg Kroah-Hartman return res; 43096fd7ce5SGreg Kroah-Hartman } 43196fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(tty_find_polling_driver); 43296fd7ce5SGreg Kroah-Hartman #endif 43396fd7ce5SGreg Kroah-Hartman 43496fd7ce5SGreg Kroah-Hartman static ssize_t hung_up_tty_read(struct file *file, char __user *buf, 43596fd7ce5SGreg Kroah-Hartman size_t count, loff_t *ppos) 43696fd7ce5SGreg Kroah-Hartman { 43796fd7ce5SGreg Kroah-Hartman return 0; 43896fd7ce5SGreg Kroah-Hartman } 43996fd7ce5SGreg Kroah-Hartman 44096fd7ce5SGreg Kroah-Hartman static ssize_t hung_up_tty_write(struct file *file, const char __user *buf, 44196fd7ce5SGreg Kroah-Hartman size_t count, loff_t *ppos) 44296fd7ce5SGreg Kroah-Hartman { 44396fd7ce5SGreg Kroah-Hartman return -EIO; 44496fd7ce5SGreg Kroah-Hartman } 44596fd7ce5SGreg Kroah-Hartman 44696fd7ce5SGreg Kroah-Hartman /* No kernel lock held - none needed ;) */ 447afc9a42bSAl Viro static __poll_t hung_up_tty_poll(struct file *filp, poll_table *wait) 44896fd7ce5SGreg Kroah-Hartman { 449a9a08845SLinus Torvalds return EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP | EPOLLRDNORM | EPOLLWRNORM; 45096fd7ce5SGreg Kroah-Hartman } 45196fd7ce5SGreg Kroah-Hartman 45296fd7ce5SGreg Kroah-Hartman static long hung_up_tty_ioctl(struct file *file, unsigned int cmd, 45396fd7ce5SGreg Kroah-Hartman unsigned long arg) 45496fd7ce5SGreg Kroah-Hartman { 45596fd7ce5SGreg Kroah-Hartman return cmd == TIOCSPGRP ? -ENOTTY : -EIO; 45696fd7ce5SGreg Kroah-Hartman } 45796fd7ce5SGreg Kroah-Hartman 45896fd7ce5SGreg Kroah-Hartman static long hung_up_tty_compat_ioctl(struct file *file, 45996fd7ce5SGreg Kroah-Hartman unsigned int cmd, unsigned long arg) 46096fd7ce5SGreg Kroah-Hartman { 46196fd7ce5SGreg Kroah-Hartman return cmd == TIOCSPGRP ? -ENOTTY : -EIO; 46296fd7ce5SGreg Kroah-Hartman } 46396fd7ce5SGreg Kroah-Hartman 464f557474cSPeter Hurley static int hung_up_tty_fasync(int fd, struct file *file, int on) 465f557474cSPeter Hurley { 466f557474cSPeter Hurley return -ENOTTY; 467f557474cSPeter Hurley } 468f557474cSPeter Hurley 469d01c3289SMasatake YAMATO static void tty_show_fdinfo(struct seq_file *m, struct file *file) 470d01c3289SMasatake YAMATO { 471d01c3289SMasatake YAMATO struct tty_struct *tty = file_tty(file); 472d01c3289SMasatake YAMATO 473d01c3289SMasatake YAMATO if (tty && tty->ops && tty->ops->show_fdinfo) 474d01c3289SMasatake YAMATO tty->ops->show_fdinfo(tty, m); 475d01c3289SMasatake YAMATO } 476d01c3289SMasatake YAMATO 47796fd7ce5SGreg Kroah-Hartman static const struct file_operations tty_fops = { 47896fd7ce5SGreg Kroah-Hartman .llseek = no_llseek, 479dd78b0c4SLinus Torvalds .read_iter = tty_read, 480*9bb48c82SLinus Torvalds .write_iter = tty_write, 481dd78b0c4SLinus Torvalds .splice_read = generic_file_splice_read, 482*9bb48c82SLinus Torvalds .splice_write = iter_file_splice_write, 48396fd7ce5SGreg Kroah-Hartman .poll = tty_poll, 48496fd7ce5SGreg Kroah-Hartman .unlocked_ioctl = tty_ioctl, 48596fd7ce5SGreg Kroah-Hartman .compat_ioctl = tty_compat_ioctl, 48696fd7ce5SGreg Kroah-Hartman .open = tty_open, 48796fd7ce5SGreg Kroah-Hartman .release = tty_release, 48896fd7ce5SGreg Kroah-Hartman .fasync = tty_fasync, 489d01c3289SMasatake YAMATO .show_fdinfo = tty_show_fdinfo, 49096fd7ce5SGreg Kroah-Hartman }; 49196fd7ce5SGreg Kroah-Hartman 49296fd7ce5SGreg Kroah-Hartman static const struct file_operations console_fops = { 49396fd7ce5SGreg Kroah-Hartman .llseek = no_llseek, 494dd78b0c4SLinus Torvalds .read_iter = tty_read, 495*9bb48c82SLinus Torvalds .write_iter = redirected_tty_write, 496dd78b0c4SLinus Torvalds .splice_read = generic_file_splice_read, 497*9bb48c82SLinus Torvalds .splice_write = iter_file_splice_write, 49896fd7ce5SGreg Kroah-Hartman .poll = tty_poll, 49996fd7ce5SGreg Kroah-Hartman .unlocked_ioctl = tty_ioctl, 50096fd7ce5SGreg Kroah-Hartman .compat_ioctl = tty_compat_ioctl, 50196fd7ce5SGreg Kroah-Hartman .open = tty_open, 50296fd7ce5SGreg Kroah-Hartman .release = tty_release, 50396fd7ce5SGreg Kroah-Hartman .fasync = tty_fasync, 50496fd7ce5SGreg Kroah-Hartman }; 50596fd7ce5SGreg Kroah-Hartman 50696fd7ce5SGreg Kroah-Hartman static const struct file_operations hung_up_tty_fops = { 50796fd7ce5SGreg Kroah-Hartman .llseek = no_llseek, 50896fd7ce5SGreg Kroah-Hartman .read = hung_up_tty_read, 50996fd7ce5SGreg Kroah-Hartman .write = hung_up_tty_write, 51096fd7ce5SGreg Kroah-Hartman .poll = hung_up_tty_poll, 51196fd7ce5SGreg Kroah-Hartman .unlocked_ioctl = hung_up_tty_ioctl, 51296fd7ce5SGreg Kroah-Hartman .compat_ioctl = hung_up_tty_compat_ioctl, 51396fd7ce5SGreg Kroah-Hartman .release = tty_release, 514f557474cSPeter Hurley .fasync = hung_up_tty_fasync, 51596fd7ce5SGreg Kroah-Hartman }; 51696fd7ce5SGreg Kroah-Hartman 51796fd7ce5SGreg Kroah-Hartman static DEFINE_SPINLOCK(redirect_lock); 51896fd7ce5SGreg Kroah-Hartman static struct file *redirect; 51996fd7ce5SGreg Kroah-Hartman 5207c0cca7cSGreg Kroah-Hartman extern void tty_sysctl_init(void); 5217c0cca7cSGreg Kroah-Hartman 52296fd7ce5SGreg Kroah-Hartman /** 52396fd7ce5SGreg Kroah-Hartman * tty_wakeup - request more data 52496fd7ce5SGreg Kroah-Hartman * @tty: terminal 52596fd7ce5SGreg Kroah-Hartman * 52696fd7ce5SGreg Kroah-Hartman * Internal and external helper for wakeups of tty. This function 52796fd7ce5SGreg Kroah-Hartman * informs the line discipline if present that the driver is ready 52896fd7ce5SGreg Kroah-Hartman * to receive more output data. 52996fd7ce5SGreg Kroah-Hartman */ 53096fd7ce5SGreg Kroah-Hartman 53196fd7ce5SGreg Kroah-Hartman void tty_wakeup(struct tty_struct *tty) 53296fd7ce5SGreg Kroah-Hartman { 53396fd7ce5SGreg Kroah-Hartman struct tty_ldisc *ld; 53496fd7ce5SGreg Kroah-Hartman 53596fd7ce5SGreg Kroah-Hartman if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) { 53696fd7ce5SGreg Kroah-Hartman ld = tty_ldisc_ref(tty); 53796fd7ce5SGreg Kroah-Hartman if (ld) { 53896fd7ce5SGreg Kroah-Hartman if (ld->ops->write_wakeup) 53996fd7ce5SGreg Kroah-Hartman ld->ops->write_wakeup(tty); 54096fd7ce5SGreg Kroah-Hartman tty_ldisc_deref(ld); 54196fd7ce5SGreg Kroah-Hartman } 54296fd7ce5SGreg Kroah-Hartman } 543a9a08845SLinus Torvalds wake_up_interruptible_poll(&tty->write_wait, EPOLLOUT); 54496fd7ce5SGreg Kroah-Hartman } 54596fd7ce5SGreg Kroah-Hartman 54696fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(tty_wakeup); 54796fd7ce5SGreg Kroah-Hartman 54896fd7ce5SGreg Kroah-Hartman /** 54996fd7ce5SGreg Kroah-Hartman * __tty_hangup - actual handler for hangup events 550fa441954SJiri Slaby * @tty: tty device 55196fd7ce5SGreg Kroah-Hartman * 552ef4f527cSKevin Cernekee * This can be called by a "kworker" kernel thread. That is process 55396fd7ce5SGreg Kroah-Hartman * synchronous but doesn't hold any locks, so we need to make sure we 55496fd7ce5SGreg Kroah-Hartman * have the appropriate locks for what we're doing. 55596fd7ce5SGreg Kroah-Hartman * 55696fd7ce5SGreg Kroah-Hartman * The hangup event clears any pending redirections onto the hung up 55796fd7ce5SGreg Kroah-Hartman * device. It ensures future writes will error and it does the needed 55896fd7ce5SGreg Kroah-Hartman * line discipline hangup and signal delivery. The tty object itself 55996fd7ce5SGreg Kroah-Hartman * remains intact. 56096fd7ce5SGreg Kroah-Hartman * 56196fd7ce5SGreg Kroah-Hartman * Locking: 56296fd7ce5SGreg Kroah-Hartman * BTM 56396fd7ce5SGreg Kroah-Hartman * redirect lock for undoing redirection 56496fd7ce5SGreg Kroah-Hartman * file list lock for manipulating list of ttys 565137084bbSPeter Hurley * tty_ldiscs_lock from called functions 5666a1c0680SPeter Hurley * termios_rwsem resetting termios data 56796fd7ce5SGreg Kroah-Hartman * tasklist_lock to walk task list for hangup event 56896fd7ce5SGreg Kroah-Hartman * ->siglock to protect ->signal/->sighand 56996fd7ce5SGreg Kroah-Hartman */ 570f91e2590SPeter Hurley static void __tty_hangup(struct tty_struct *tty, int exit_session) 57196fd7ce5SGreg Kroah-Hartman { 57296fd7ce5SGreg Kroah-Hartman struct file *cons_filp = NULL; 57396fd7ce5SGreg Kroah-Hartman struct file *filp, *f = NULL; 57496fd7ce5SGreg Kroah-Hartman struct tty_file_private *priv; 57596fd7ce5SGreg Kroah-Hartman int closecount = 0, n; 576ea648a47SPeter Hurley int refs; 57796fd7ce5SGreg Kroah-Hartman 57896fd7ce5SGreg Kroah-Hartman if (!tty) 57996fd7ce5SGreg Kroah-Hartman return; 58096fd7ce5SGreg Kroah-Hartman 58196fd7ce5SGreg Kroah-Hartman 58296fd7ce5SGreg Kroah-Hartman spin_lock(&redirect_lock); 58396fd7ce5SGreg Kroah-Hartman if (redirect && file_tty(redirect) == tty) { 58496fd7ce5SGreg Kroah-Hartman f = redirect; 58596fd7ce5SGreg Kroah-Hartman redirect = NULL; 58696fd7ce5SGreg Kroah-Hartman } 58796fd7ce5SGreg Kroah-Hartman spin_unlock(&redirect_lock); 58896fd7ce5SGreg Kroah-Hartman 58989c8d91eSAlan Cox tty_lock(tty); 59096fd7ce5SGreg Kroah-Hartman 591cb50e523SPeter Hurley if (test_bit(TTY_HUPPED, &tty->flags)) { 592cb50e523SPeter Hurley tty_unlock(tty); 593cb50e523SPeter Hurley return; 594cb50e523SPeter Hurley } 595cb50e523SPeter Hurley 59628b0f8a6STejun Heo /* 59728b0f8a6STejun Heo * Some console devices aren't actually hung up for technical and 59828b0f8a6STejun Heo * historical reasons, which can lead to indefinite interruptible 59928b0f8a6STejun Heo * sleep in n_tty_read(). The following explicitly tells 60028b0f8a6STejun Heo * n_tty_read() to abort readers. 60128b0f8a6STejun Heo */ 60228b0f8a6STejun Heo set_bit(TTY_HUPPING, &tty->flags); 60328b0f8a6STejun Heo 60496fd7ce5SGreg Kroah-Hartman /* inuse_filps is protected by the single tty lock, 60596fd7ce5SGreg Kroah-Hartman this really needs to change if we want to flush the 60696fd7ce5SGreg Kroah-Hartman workqueue with the lock held */ 60796fd7ce5SGreg Kroah-Hartman check_tty_count(tty, "tty_hangup"); 60896fd7ce5SGreg Kroah-Hartman 6094a510969SPeter Hurley spin_lock(&tty->files_lock); 61096fd7ce5SGreg Kroah-Hartman /* This breaks for file handles being sent over AF_UNIX sockets ? */ 61196fd7ce5SGreg Kroah-Hartman list_for_each_entry(priv, &tty->tty_files, list) { 61296fd7ce5SGreg Kroah-Hartman filp = priv->file; 613*9bb48c82SLinus Torvalds if (filp->f_op->write_iter == redirected_tty_write) 61496fd7ce5SGreg Kroah-Hartman cons_filp = filp; 615*9bb48c82SLinus Torvalds if (filp->f_op->write_iter != tty_write) 61696fd7ce5SGreg Kroah-Hartman continue; 61796fd7ce5SGreg Kroah-Hartman closecount++; 61896fd7ce5SGreg Kroah-Hartman __tty_fasync(-1, filp, 0); /* can't block */ 61996fd7ce5SGreg Kroah-Hartman filp->f_op = &hung_up_tty_fops; 62096fd7ce5SGreg Kroah-Hartman } 6214a510969SPeter Hurley spin_unlock(&tty->files_lock); 62296fd7ce5SGreg Kroah-Hartman 62325fdf243SPeter Hurley refs = tty_signal_session_leader(tty, exit_session); 62425fdf243SPeter Hurley /* Account for the p->signal references we killed */ 62525fdf243SPeter Hurley while (refs--) 62625fdf243SPeter Hurley tty_kref_put(tty); 62725fdf243SPeter Hurley 628892d1fa7SPeter Hurley tty_ldisc_hangup(tty, cons_filp != NULL); 62996fd7ce5SGreg Kroah-Hartman 63020cc225bSPeter Hurley spin_lock_irq(&tty->ctrl_lock); 63196fd7ce5SGreg Kroah-Hartman clear_bit(TTY_THROTTLED, &tty->flags); 63296fd7ce5SGreg Kroah-Hartman clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); 63396fd7ce5SGreg Kroah-Hartman put_pid(tty->session); 63496fd7ce5SGreg Kroah-Hartman put_pid(tty->pgrp); 63596fd7ce5SGreg Kroah-Hartman tty->session = NULL; 63696fd7ce5SGreg Kroah-Hartman tty->pgrp = NULL; 63796fd7ce5SGreg Kroah-Hartman tty->ctrl_status = 0; 63820cc225bSPeter Hurley spin_unlock_irq(&tty->ctrl_lock); 63996fd7ce5SGreg Kroah-Hartman 64096fd7ce5SGreg Kroah-Hartman /* 64196fd7ce5SGreg Kroah-Hartman * If one of the devices matches a console pointer, we 64296fd7ce5SGreg Kroah-Hartman * cannot just call hangup() because that will cause 64396fd7ce5SGreg Kroah-Hartman * tty->count and state->count to go out of sync. 64496fd7ce5SGreg Kroah-Hartman * So we just call close() the right number of times. 64596fd7ce5SGreg Kroah-Hartman */ 64696fd7ce5SGreg Kroah-Hartman if (cons_filp) { 64796fd7ce5SGreg Kroah-Hartman if (tty->ops->close) 64896fd7ce5SGreg Kroah-Hartman for (n = 0; n < closecount; n++) 64996fd7ce5SGreg Kroah-Hartman tty->ops->close(tty, cons_filp); 65096fd7ce5SGreg Kroah-Hartman } else if (tty->ops->hangup) 6517c6d340fSPeter Hurley tty->ops->hangup(tty); 65296fd7ce5SGreg Kroah-Hartman /* 653892d1fa7SPeter Hurley * We don't want to have driver/ldisc interactions beyond the ones 654892d1fa7SPeter Hurley * we did here. The driver layer expects no calls after ->hangup() 655892d1fa7SPeter Hurley * from the ldisc side, which is now guaranteed. 65696fd7ce5SGreg Kroah-Hartman */ 65796fd7ce5SGreg Kroah-Hartman set_bit(TTY_HUPPED, &tty->flags); 65828b0f8a6STejun Heo clear_bit(TTY_HUPPING, &tty->flags); 65989c8d91eSAlan Cox tty_unlock(tty); 66096fd7ce5SGreg Kroah-Hartman 66196fd7ce5SGreg Kroah-Hartman if (f) 66296fd7ce5SGreg Kroah-Hartman fput(f); 66396fd7ce5SGreg Kroah-Hartman } 66496fd7ce5SGreg Kroah-Hartman 66596fd7ce5SGreg Kroah-Hartman static void do_tty_hangup(struct work_struct *work) 66696fd7ce5SGreg Kroah-Hartman { 66796fd7ce5SGreg Kroah-Hartman struct tty_struct *tty = 66896fd7ce5SGreg Kroah-Hartman container_of(work, struct tty_struct, hangup_work); 66996fd7ce5SGreg Kroah-Hartman 670f91e2590SPeter Hurley __tty_hangup(tty, 0); 67196fd7ce5SGreg Kroah-Hartman } 67296fd7ce5SGreg Kroah-Hartman 67396fd7ce5SGreg Kroah-Hartman /** 67496fd7ce5SGreg Kroah-Hartman * tty_hangup - trigger a hangup event 67596fd7ce5SGreg Kroah-Hartman * @tty: tty to hangup 67696fd7ce5SGreg Kroah-Hartman * 67796fd7ce5SGreg Kroah-Hartman * A carrier loss (virtual or otherwise) has occurred on this like 67896fd7ce5SGreg Kroah-Hartman * schedule a hangup sequence to run after this event. 67996fd7ce5SGreg Kroah-Hartman */ 68096fd7ce5SGreg Kroah-Hartman 68196fd7ce5SGreg Kroah-Hartman void tty_hangup(struct tty_struct *tty) 68296fd7ce5SGreg Kroah-Hartman { 683d435cefeSPeter Hurley tty_debug_hangup(tty, "hangup\n"); 68496fd7ce5SGreg Kroah-Hartman schedule_work(&tty->hangup_work); 68596fd7ce5SGreg Kroah-Hartman } 68696fd7ce5SGreg Kroah-Hartman 68796fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_hangup); 68896fd7ce5SGreg Kroah-Hartman 68996fd7ce5SGreg Kroah-Hartman /** 69096fd7ce5SGreg Kroah-Hartman * tty_vhangup - process vhangup 69196fd7ce5SGreg Kroah-Hartman * @tty: tty to hangup 69296fd7ce5SGreg Kroah-Hartman * 69396fd7ce5SGreg Kroah-Hartman * The user has asked via system call for the terminal to be hung up. 69496fd7ce5SGreg Kroah-Hartman * We do this synchronously so that when the syscall returns the process 69596fd7ce5SGreg Kroah-Hartman * is complete. That guarantee is necessary for security reasons. 69696fd7ce5SGreg Kroah-Hartman */ 69796fd7ce5SGreg Kroah-Hartman 69896fd7ce5SGreg Kroah-Hartman void tty_vhangup(struct tty_struct *tty) 69996fd7ce5SGreg Kroah-Hartman { 700d435cefeSPeter Hurley tty_debug_hangup(tty, "vhangup\n"); 701f91e2590SPeter Hurley __tty_hangup(tty, 0); 70296fd7ce5SGreg Kroah-Hartman } 70396fd7ce5SGreg Kroah-Hartman 70496fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_vhangup); 70596fd7ce5SGreg Kroah-Hartman 70696fd7ce5SGreg Kroah-Hartman 70796fd7ce5SGreg Kroah-Hartman /** 70896fd7ce5SGreg Kroah-Hartman * tty_vhangup_self - process vhangup for own ctty 70996fd7ce5SGreg Kroah-Hartman * 71096fd7ce5SGreg Kroah-Hartman * Perform a vhangup on the current controlling tty 71196fd7ce5SGreg Kroah-Hartman */ 71296fd7ce5SGreg Kroah-Hartman 71396fd7ce5SGreg Kroah-Hartman void tty_vhangup_self(void) 71496fd7ce5SGreg Kroah-Hartman { 71596fd7ce5SGreg Kroah-Hartman struct tty_struct *tty; 71696fd7ce5SGreg Kroah-Hartman 71796fd7ce5SGreg Kroah-Hartman tty = get_current_tty(); 71896fd7ce5SGreg Kroah-Hartman if (tty) { 71996fd7ce5SGreg Kroah-Hartman tty_vhangup(tty); 72096fd7ce5SGreg Kroah-Hartman tty_kref_put(tty); 72196fd7ce5SGreg Kroah-Hartman } 72296fd7ce5SGreg Kroah-Hartman } 72396fd7ce5SGreg Kroah-Hartman 72496fd7ce5SGreg Kroah-Hartman /** 725f91e2590SPeter Hurley * tty_vhangup_session - hangup session leader exit 726f91e2590SPeter Hurley * @tty: tty to hangup 727f91e2590SPeter Hurley * 728f91e2590SPeter Hurley * The session leader is exiting and hanging up its controlling terminal. 729f91e2590SPeter Hurley * Every process in the foreground process group is signalled SIGHUP. 730f91e2590SPeter Hurley * 731f91e2590SPeter Hurley * We do this synchronously so that when the syscall returns the process 732f91e2590SPeter Hurley * is complete. That guarantee is necessary for security reasons. 733f91e2590SPeter Hurley */ 734f91e2590SPeter Hurley 735a1235b3eSNicolas Pitre void tty_vhangup_session(struct tty_struct *tty) 736f91e2590SPeter Hurley { 737d435cefeSPeter Hurley tty_debug_hangup(tty, "session hangup\n"); 738f91e2590SPeter Hurley __tty_hangup(tty, 1); 739f91e2590SPeter Hurley } 740f91e2590SPeter Hurley 741f91e2590SPeter Hurley /** 74296fd7ce5SGreg Kroah-Hartman * tty_hung_up_p - was tty hung up 74396fd7ce5SGreg Kroah-Hartman * @filp: file pointer of tty 74496fd7ce5SGreg Kroah-Hartman * 74596fd7ce5SGreg Kroah-Hartman * Return true if the tty has been subject to a vhangup or a carrier 74696fd7ce5SGreg Kroah-Hartman * loss 74796fd7ce5SGreg Kroah-Hartman */ 74896fd7ce5SGreg Kroah-Hartman 74996fd7ce5SGreg Kroah-Hartman int tty_hung_up_p(struct file *filp) 75096fd7ce5SGreg Kroah-Hartman { 751ed3f0af8SAlan Cox return (filp && filp->f_op == &hung_up_tty_fops); 75296fd7ce5SGreg Kroah-Hartman } 75396fd7ce5SGreg Kroah-Hartman 75496fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_hung_up_p); 75596fd7ce5SGreg Kroah-Hartman 75696fd7ce5SGreg Kroah-Hartman /** 75796fd7ce5SGreg Kroah-Hartman * stop_tty - propagate flow control 75896fd7ce5SGreg Kroah-Hartman * @tty: tty to stop 75996fd7ce5SGreg Kroah-Hartman * 76001adc807SPeter Hurley * Perform flow control to the driver. May be called 76196fd7ce5SGreg Kroah-Hartman * on an already stopped device and will not re-call the driver 76296fd7ce5SGreg Kroah-Hartman * method. 76396fd7ce5SGreg Kroah-Hartman * 76496fd7ce5SGreg Kroah-Hartman * This functionality is used by both the line disciplines for 76596fd7ce5SGreg Kroah-Hartman * halting incoming flow and by the driver. It may therefore be 76696fd7ce5SGreg Kroah-Hartman * called from any context, may be under the tty atomic_write_lock 76796fd7ce5SGreg Kroah-Hartman * but not always. 76896fd7ce5SGreg Kroah-Hartman * 76996fd7ce5SGreg Kroah-Hartman * Locking: 770f9e053dcSPeter Hurley * flow_lock 77196fd7ce5SGreg Kroah-Hartman */ 77296fd7ce5SGreg Kroah-Hartman 773f9e053dcSPeter Hurley void __stop_tty(struct tty_struct *tty) 77496fd7ce5SGreg Kroah-Hartman { 775f9e053dcSPeter Hurley if (tty->stopped) 77696fd7ce5SGreg Kroah-Hartman return; 77796fd7ce5SGreg Kroah-Hartman tty->stopped = 1; 77896fd7ce5SGreg Kroah-Hartman if (tty->ops->stop) 779c961bfb1SPeter Hurley tty->ops->stop(tty); 78096fd7ce5SGreg Kroah-Hartman } 78196fd7ce5SGreg Kroah-Hartman 782f9e053dcSPeter Hurley void stop_tty(struct tty_struct *tty) 783f9e053dcSPeter Hurley { 784f9e053dcSPeter Hurley unsigned long flags; 785f9e053dcSPeter Hurley 786f9e053dcSPeter Hurley spin_lock_irqsave(&tty->flow_lock, flags); 787f9e053dcSPeter Hurley __stop_tty(tty); 788f9e053dcSPeter Hurley spin_unlock_irqrestore(&tty->flow_lock, flags); 789f9e053dcSPeter Hurley } 79096fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(stop_tty); 79196fd7ce5SGreg Kroah-Hartman 79296fd7ce5SGreg Kroah-Hartman /** 79396fd7ce5SGreg Kroah-Hartman * start_tty - propagate flow control 79496fd7ce5SGreg Kroah-Hartman * @tty: tty to start 79596fd7ce5SGreg Kroah-Hartman * 79601adc807SPeter Hurley * Start a tty that has been stopped if at all possible. If this 79701adc807SPeter Hurley * tty was previous stopped and is now being started, the driver 79801adc807SPeter Hurley * start method is invoked and the line discipline woken. 79996fd7ce5SGreg Kroah-Hartman * 80096fd7ce5SGreg Kroah-Hartman * Locking: 801f9e053dcSPeter Hurley * flow_lock 80296fd7ce5SGreg Kroah-Hartman */ 80396fd7ce5SGreg Kroah-Hartman 804f9e053dcSPeter Hurley void __start_tty(struct tty_struct *tty) 80596fd7ce5SGreg Kroah-Hartman { 806f9e053dcSPeter Hurley if (!tty->stopped || tty->flow_stopped) 80796fd7ce5SGreg Kroah-Hartman return; 80896fd7ce5SGreg Kroah-Hartman tty->stopped = 0; 80996fd7ce5SGreg Kroah-Hartman if (tty->ops->start) 810c961bfb1SPeter Hurley tty->ops->start(tty); 81196fd7ce5SGreg Kroah-Hartman tty_wakeup(tty); 81296fd7ce5SGreg Kroah-Hartman } 81396fd7ce5SGreg Kroah-Hartman 814f9e053dcSPeter Hurley void start_tty(struct tty_struct *tty) 815f9e053dcSPeter Hurley { 816f9e053dcSPeter Hurley unsigned long flags; 817f9e053dcSPeter Hurley 818f9e053dcSPeter Hurley spin_lock_irqsave(&tty->flow_lock, flags); 819f9e053dcSPeter Hurley __start_tty(tty); 820f9e053dcSPeter Hurley spin_unlock_irqrestore(&tty->flow_lock, flags); 821f9e053dcSPeter Hurley } 82296fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(start_tty); 82396fd7ce5SGreg Kroah-Hartman 824c884f871SArnd Bergmann static void tty_update_time(struct timespec64 *time) 82537b7f3c7SJiri Slaby { 826c884f871SArnd Bergmann time64_t sec = ktime_get_real_seconds(); 827fbf47635SGreg Kroah-Hartman 828fbf47635SGreg Kroah-Hartman /* 829fbf47635SGreg Kroah-Hartman * We only care if the two values differ in anything other than the 830fbf47635SGreg Kroah-Hartman * lower three bits (i.e every 8 seconds). If so, then we can update 831fbf47635SGreg Kroah-Hartman * the time of the tty device, otherwise it could be construded as a 832fbf47635SGreg Kroah-Hartman * security leak to let userspace know the exact timing of the tty. 833fbf47635SGreg Kroah-Hartman */ 834fbf47635SGreg Kroah-Hartman if ((sec ^ time->tv_sec) & ~7) 83537b7f3c7SJiri Slaby time->tv_sec = sec; 83637b7f3c7SJiri Slaby } 83737b7f3c7SJiri Slaby 8383b830a9cSLinus Torvalds /* 8393b830a9cSLinus Torvalds * Iterate on the ldisc ->read() function until we've gotten all 8403b830a9cSLinus Torvalds * the data the ldisc has for us. 8413b830a9cSLinus Torvalds * 8423b830a9cSLinus Torvalds * The "cookie" is something that the ldisc read function can fill 8433b830a9cSLinus Torvalds * in to let us know that there is more data to be had. 8443b830a9cSLinus Torvalds * 8453b830a9cSLinus Torvalds * We promise to continue to call the ldisc until it stops returning 8463b830a9cSLinus Torvalds * data or clears the cookie. The cookie may be something that the 8473b830a9cSLinus Torvalds * ldisc maintains state for and needs to free. 8483b830a9cSLinus Torvalds */ 849dd78b0c4SLinus Torvalds static int iterate_tty_read(struct tty_ldisc *ld, struct tty_struct *tty, 850dd78b0c4SLinus Torvalds struct file *file, struct iov_iter *to) 8513b830a9cSLinus Torvalds { 8523b830a9cSLinus Torvalds int retval = 0; 8533b830a9cSLinus Torvalds void *cookie = NULL; 8543b830a9cSLinus Torvalds unsigned long offset = 0; 8553b830a9cSLinus Torvalds char kernel_buf[64]; 856dd78b0c4SLinus Torvalds size_t count = iov_iter_count(to); 8573b830a9cSLinus Torvalds 8583b830a9cSLinus Torvalds do { 859dd78b0c4SLinus Torvalds int size, copied; 8603b830a9cSLinus Torvalds 8613b830a9cSLinus Torvalds size = count > sizeof(kernel_buf) ? sizeof(kernel_buf) : count; 8623b830a9cSLinus Torvalds size = ld->ops->read(tty, file, kernel_buf, size, &cookie, offset); 8633b830a9cSLinus Torvalds if (!size) 8643b830a9cSLinus Torvalds break; 8653b830a9cSLinus Torvalds 8663b830a9cSLinus Torvalds /* 8673b830a9cSLinus Torvalds * A ldisc read error return will override any previously copied 8683b830a9cSLinus Torvalds * data (eg -EOVERFLOW from HDLC) 8693b830a9cSLinus Torvalds */ 8703b830a9cSLinus Torvalds if (size < 0) { 8713b830a9cSLinus Torvalds memzero_explicit(kernel_buf, sizeof(kernel_buf)); 8723b830a9cSLinus Torvalds return size; 8733b830a9cSLinus Torvalds } 8743b830a9cSLinus Torvalds 875dd78b0c4SLinus Torvalds copied = copy_to_iter(kernel_buf, size, to); 876dd78b0c4SLinus Torvalds offset += copied; 877dd78b0c4SLinus Torvalds count -= copied; 8783b830a9cSLinus Torvalds 8793b830a9cSLinus Torvalds /* 8803b830a9cSLinus Torvalds * If the user copy failed, we still need to do another ->read() 8813b830a9cSLinus Torvalds * call if we had a cookie to let the ldisc clear up. 8823b830a9cSLinus Torvalds * 8833b830a9cSLinus Torvalds * But make sure size is zeroed. 8843b830a9cSLinus Torvalds */ 885dd78b0c4SLinus Torvalds if (unlikely(copied != size)) { 8863b830a9cSLinus Torvalds count = 0; 8873b830a9cSLinus Torvalds retval = -EFAULT; 8883b830a9cSLinus Torvalds } 8893b830a9cSLinus Torvalds } while (cookie); 8903b830a9cSLinus Torvalds 8913b830a9cSLinus Torvalds /* We always clear tty buffer in case they contained passwords */ 8923b830a9cSLinus Torvalds memzero_explicit(kernel_buf, sizeof(kernel_buf)); 8933b830a9cSLinus Torvalds return offset ? offset : retval; 8943b830a9cSLinus Torvalds } 8953b830a9cSLinus Torvalds 8963b830a9cSLinus Torvalds 89796fd7ce5SGreg Kroah-Hartman /** 89896fd7ce5SGreg Kroah-Hartman * tty_read - read method for tty device files 89996fd7ce5SGreg Kroah-Hartman * @file: pointer to tty file 90096fd7ce5SGreg Kroah-Hartman * @buf: user buffer 90196fd7ce5SGreg Kroah-Hartman * @count: size of user buffer 90296fd7ce5SGreg Kroah-Hartman * @ppos: unused 90396fd7ce5SGreg Kroah-Hartman * 90496fd7ce5SGreg Kroah-Hartman * Perform the read system call function on this terminal device. Checks 90596fd7ce5SGreg Kroah-Hartman * for hung up devices before calling the line discipline method. 90696fd7ce5SGreg Kroah-Hartman * 90796fd7ce5SGreg Kroah-Hartman * Locking: 90896fd7ce5SGreg Kroah-Hartman * Locks the line discipline internally while needed. Multiple 90996fd7ce5SGreg Kroah-Hartman * read calls may be outstanding in parallel. 91096fd7ce5SGreg Kroah-Hartman */ 91196fd7ce5SGreg Kroah-Hartman 912dd78b0c4SLinus Torvalds static ssize_t tty_read(struct kiocb *iocb, struct iov_iter *to) 91396fd7ce5SGreg Kroah-Hartman { 91496fd7ce5SGreg Kroah-Hartman int i; 915dd78b0c4SLinus Torvalds struct file *file = iocb->ki_filp; 91637b7f3c7SJiri Slaby struct inode *inode = file_inode(file); 91796fd7ce5SGreg Kroah-Hartman struct tty_struct *tty = file_tty(file); 91896fd7ce5SGreg Kroah-Hartman struct tty_ldisc *ld; 91996fd7ce5SGreg Kroah-Hartman 92037b7f3c7SJiri Slaby if (tty_paranoia_check(tty, inode, "tty_read")) 92196fd7ce5SGreg Kroah-Hartman return -EIO; 92218900ca6SPeter Hurley if (!tty || tty_io_error(tty)) 92396fd7ce5SGreg Kroah-Hartman return -EIO; 92496fd7ce5SGreg Kroah-Hartman 92596fd7ce5SGreg Kroah-Hartman /* We want to wait for the line discipline to sort out in this 92696fd7ce5SGreg Kroah-Hartman situation */ 92796fd7ce5SGreg Kroah-Hartman ld = tty_ldisc_ref_wait(tty); 92896fd7ce5SGreg Kroah-Hartman i = -EIO; 929dd78b0c4SLinus Torvalds if (ld && ld->ops->read) 930dd78b0c4SLinus Torvalds i = iterate_tty_read(ld, tty, file, to); 93196fd7ce5SGreg Kroah-Hartman tty_ldisc_deref(ld); 932b0de59b5SJiri Slaby 933c884f871SArnd Bergmann if (i > 0) 934c884f871SArnd Bergmann tty_update_time(&inode->i_atime); 93537b7f3c7SJiri Slaby 93696fd7ce5SGreg Kroah-Hartman return i; 93796fd7ce5SGreg Kroah-Hartman } 93896fd7ce5SGreg Kroah-Hartman 939136d5258SPeter Hurley static void tty_write_unlock(struct tty_struct *tty) 94096fd7ce5SGreg Kroah-Hartman { 94196fd7ce5SGreg Kroah-Hartman mutex_unlock(&tty->atomic_write_lock); 942a9a08845SLinus Torvalds wake_up_interruptible_poll(&tty->write_wait, EPOLLOUT); 94396fd7ce5SGreg Kroah-Hartman } 94496fd7ce5SGreg Kroah-Hartman 945136d5258SPeter Hurley static int tty_write_lock(struct tty_struct *tty, int ndelay) 94696fd7ce5SGreg Kroah-Hartman { 94796fd7ce5SGreg Kroah-Hartman if (!mutex_trylock(&tty->atomic_write_lock)) { 94896fd7ce5SGreg Kroah-Hartman if (ndelay) 94996fd7ce5SGreg Kroah-Hartman return -EAGAIN; 95096fd7ce5SGreg Kroah-Hartman if (mutex_lock_interruptible(&tty->atomic_write_lock)) 95196fd7ce5SGreg Kroah-Hartman return -ERESTARTSYS; 95296fd7ce5SGreg Kroah-Hartman } 95396fd7ce5SGreg Kroah-Hartman return 0; 95496fd7ce5SGreg Kroah-Hartman } 95596fd7ce5SGreg Kroah-Hartman 95696fd7ce5SGreg Kroah-Hartman /* 95796fd7ce5SGreg Kroah-Hartman * Split writes up in sane blocksizes to avoid 95896fd7ce5SGreg Kroah-Hartman * denial-of-service type attacks 95996fd7ce5SGreg Kroah-Hartman */ 96096fd7ce5SGreg Kroah-Hartman static inline ssize_t do_tty_write( 96196fd7ce5SGreg Kroah-Hartman ssize_t (*write)(struct tty_struct *, struct file *, const unsigned char *, size_t), 96296fd7ce5SGreg Kroah-Hartman struct tty_struct *tty, 96396fd7ce5SGreg Kroah-Hartman struct file *file, 964*9bb48c82SLinus Torvalds struct iov_iter *from) 96596fd7ce5SGreg Kroah-Hartman { 966*9bb48c82SLinus Torvalds size_t count = iov_iter_count(from); 96796fd7ce5SGreg Kroah-Hartman ssize_t ret, written = 0; 96896fd7ce5SGreg Kroah-Hartman unsigned int chunk; 96996fd7ce5SGreg Kroah-Hartman 97096fd7ce5SGreg Kroah-Hartman ret = tty_write_lock(tty, file->f_flags & O_NDELAY); 97196fd7ce5SGreg Kroah-Hartman if (ret < 0) 97296fd7ce5SGreg Kroah-Hartman return ret; 97396fd7ce5SGreg Kroah-Hartman 97496fd7ce5SGreg Kroah-Hartman /* 97596fd7ce5SGreg Kroah-Hartman * We chunk up writes into a temporary buffer. This 97696fd7ce5SGreg Kroah-Hartman * simplifies low-level drivers immensely, since they 97796fd7ce5SGreg Kroah-Hartman * don't have locking issues and user mode accesses. 97896fd7ce5SGreg Kroah-Hartman * 97996fd7ce5SGreg Kroah-Hartman * But if TTY_NO_WRITE_SPLIT is set, we should use a 98096fd7ce5SGreg Kroah-Hartman * big chunk-size.. 98196fd7ce5SGreg Kroah-Hartman * 98296fd7ce5SGreg Kroah-Hartman * The default chunk-size is 2kB, because the NTTY 98396fd7ce5SGreg Kroah-Hartman * layer has problems with bigger chunks. It will 98496fd7ce5SGreg Kroah-Hartman * claim to be able to handle more characters than 98596fd7ce5SGreg Kroah-Hartman * it actually does. 98696fd7ce5SGreg Kroah-Hartman * 98796fd7ce5SGreg Kroah-Hartman * FIXME: This can probably go away now except that 64K chunks 98896fd7ce5SGreg Kroah-Hartman * are too likely to fail unless switched to vmalloc... 98996fd7ce5SGreg Kroah-Hartman */ 99096fd7ce5SGreg Kroah-Hartman chunk = 2048; 99196fd7ce5SGreg Kroah-Hartman if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags)) 99296fd7ce5SGreg Kroah-Hartman chunk = 65536; 99396fd7ce5SGreg Kroah-Hartman if (count < chunk) 99496fd7ce5SGreg Kroah-Hartman chunk = count; 99596fd7ce5SGreg Kroah-Hartman 99696fd7ce5SGreg Kroah-Hartman /* write_buf/write_cnt is protected by the atomic_write_lock mutex */ 99796fd7ce5SGreg Kroah-Hartman if (tty->write_cnt < chunk) { 99896fd7ce5SGreg Kroah-Hartman unsigned char *buf_chunk; 99996fd7ce5SGreg Kroah-Hartman 100096fd7ce5SGreg Kroah-Hartman if (chunk < 1024) 100196fd7ce5SGreg Kroah-Hartman chunk = 1024; 100296fd7ce5SGreg Kroah-Hartman 100396fd7ce5SGreg Kroah-Hartman buf_chunk = kmalloc(chunk, GFP_KERNEL); 100496fd7ce5SGreg Kroah-Hartman if (!buf_chunk) { 100596fd7ce5SGreg Kroah-Hartman ret = -ENOMEM; 100696fd7ce5SGreg Kroah-Hartman goto out; 100796fd7ce5SGreg Kroah-Hartman } 100896fd7ce5SGreg Kroah-Hartman kfree(tty->write_buf); 100996fd7ce5SGreg Kroah-Hartman tty->write_cnt = chunk; 101096fd7ce5SGreg Kroah-Hartman tty->write_buf = buf_chunk; 101196fd7ce5SGreg Kroah-Hartman } 101296fd7ce5SGreg Kroah-Hartman 101396fd7ce5SGreg Kroah-Hartman /* Do the write .. */ 101496fd7ce5SGreg Kroah-Hartman for (;;) { 101596fd7ce5SGreg Kroah-Hartman size_t size = count; 101696fd7ce5SGreg Kroah-Hartman if (size > chunk) 101796fd7ce5SGreg Kroah-Hartman size = chunk; 1018*9bb48c82SLinus Torvalds 101996fd7ce5SGreg Kroah-Hartman ret = -EFAULT; 1020*9bb48c82SLinus Torvalds if (copy_from_iter(tty->write_buf, size, from) != size) 102196fd7ce5SGreg Kroah-Hartman break; 1022*9bb48c82SLinus Torvalds 102396fd7ce5SGreg Kroah-Hartman ret = write(tty, file, tty->write_buf, size); 102496fd7ce5SGreg Kroah-Hartman if (ret <= 0) 102596fd7ce5SGreg Kroah-Hartman break; 1026*9bb48c82SLinus Torvalds 1027*9bb48c82SLinus Torvalds /* FIXME! Have Al check this! */ 1028*9bb48c82SLinus Torvalds if (ret != size) 1029*9bb48c82SLinus Torvalds iov_iter_revert(from, size-ret); 1030*9bb48c82SLinus Torvalds 103196fd7ce5SGreg Kroah-Hartman written += ret; 103296fd7ce5SGreg Kroah-Hartman count -= ret; 103396fd7ce5SGreg Kroah-Hartman if (!count) 103496fd7ce5SGreg Kroah-Hartman break; 103596fd7ce5SGreg Kroah-Hartman ret = -ERESTARTSYS; 103696fd7ce5SGreg Kroah-Hartman if (signal_pending(current)) 103796fd7ce5SGreg Kroah-Hartman break; 103896fd7ce5SGreg Kroah-Hartman cond_resched(); 103996fd7ce5SGreg Kroah-Hartman } 104037b7f3c7SJiri Slaby if (written) { 1041c884f871SArnd Bergmann tty_update_time(&file_inode(file)->i_mtime); 104296fd7ce5SGreg Kroah-Hartman ret = written; 104337b7f3c7SJiri Slaby } 104496fd7ce5SGreg Kroah-Hartman out: 104596fd7ce5SGreg Kroah-Hartman tty_write_unlock(tty); 104696fd7ce5SGreg Kroah-Hartman return ret; 104796fd7ce5SGreg Kroah-Hartman } 104896fd7ce5SGreg Kroah-Hartman 104996fd7ce5SGreg Kroah-Hartman /** 105096fd7ce5SGreg Kroah-Hartman * tty_write_message - write a message to a certain tty, not just the console. 105196fd7ce5SGreg Kroah-Hartman * @tty: the destination tty_struct 105296fd7ce5SGreg Kroah-Hartman * @msg: the message to write 105396fd7ce5SGreg Kroah-Hartman * 105496fd7ce5SGreg Kroah-Hartman * This is used for messages that need to be redirected to a specific tty. 105596fd7ce5SGreg Kroah-Hartman * We don't put it into the syslog queue right now maybe in the future if 105696fd7ce5SGreg Kroah-Hartman * really needed. 105796fd7ce5SGreg Kroah-Hartman * 105896fd7ce5SGreg Kroah-Hartman * We must still hold the BTM and test the CLOSING flag for the moment. 105996fd7ce5SGreg Kroah-Hartman */ 106096fd7ce5SGreg Kroah-Hartman 106196fd7ce5SGreg Kroah-Hartman void tty_write_message(struct tty_struct *tty, char *msg) 106296fd7ce5SGreg Kroah-Hartman { 106396fd7ce5SGreg Kroah-Hartman if (tty) { 106496fd7ce5SGreg Kroah-Hartman mutex_lock(&tty->atomic_write_lock); 106589c8d91eSAlan Cox tty_lock(tty); 10664b41b953SPeter Hurley if (tty->ops->write && tty->count > 0) 106796fd7ce5SGreg Kroah-Hartman tty->ops->write(tty, msg, strlen(msg)); 106889c8d91eSAlan Cox tty_unlock(tty); 106996fd7ce5SGreg Kroah-Hartman tty_write_unlock(tty); 107096fd7ce5SGreg Kroah-Hartman } 107196fd7ce5SGreg Kroah-Hartman return; 107296fd7ce5SGreg Kroah-Hartman } 107396fd7ce5SGreg Kroah-Hartman 107496fd7ce5SGreg Kroah-Hartman 107596fd7ce5SGreg Kroah-Hartman /** 107696fd7ce5SGreg Kroah-Hartman * tty_write - write method for tty device file 107796fd7ce5SGreg Kroah-Hartman * @file: tty file pointer 107896fd7ce5SGreg Kroah-Hartman * @buf: user data to write 107996fd7ce5SGreg Kroah-Hartman * @count: bytes to write 108096fd7ce5SGreg Kroah-Hartman * @ppos: unused 108196fd7ce5SGreg Kroah-Hartman * 108296fd7ce5SGreg Kroah-Hartman * Write data to a tty device via the line discipline. 108396fd7ce5SGreg Kroah-Hartman * 108496fd7ce5SGreg Kroah-Hartman * Locking: 108596fd7ce5SGreg Kroah-Hartman * Locks the line discipline as required 108696fd7ce5SGreg Kroah-Hartman * Writes to the tty driver are serialized by the atomic_write_lock 108796fd7ce5SGreg Kroah-Hartman * and are then processed in chunks to the device. The line discipline 108896fd7ce5SGreg Kroah-Hartman * write method will not be invoked in parallel for each device. 108996fd7ce5SGreg Kroah-Hartman */ 109096fd7ce5SGreg Kroah-Hartman 1091*9bb48c82SLinus Torvalds static ssize_t tty_write(struct kiocb *iocb, struct iov_iter *from) 109296fd7ce5SGreg Kroah-Hartman { 1093*9bb48c82SLinus Torvalds struct file *file = iocb->ki_filp; 109496fd7ce5SGreg Kroah-Hartman struct tty_struct *tty = file_tty(file); 109596fd7ce5SGreg Kroah-Hartman struct tty_ldisc *ld; 109696fd7ce5SGreg Kroah-Hartman ssize_t ret; 109796fd7ce5SGreg Kroah-Hartman 10986131ffaaSAl Viro if (tty_paranoia_check(tty, file_inode(file), "tty_write")) 109996fd7ce5SGreg Kroah-Hartman return -EIO; 110018900ca6SPeter Hurley if (!tty || !tty->ops->write || tty_io_error(tty)) 110196fd7ce5SGreg Kroah-Hartman return -EIO; 110296fd7ce5SGreg Kroah-Hartman /* Short term debug to catch buggy drivers */ 110396fd7ce5SGreg Kroah-Hartman if (tty->ops->write_room == NULL) 1104339f36baSPeter Hurley tty_err(tty, "missing write_room method\n"); 110596fd7ce5SGreg Kroah-Hartman ld = tty_ldisc_ref_wait(tty); 1106*9bb48c82SLinus Torvalds if (!ld || !ld->ops->write) 110796fd7ce5SGreg Kroah-Hartman ret = -EIO; 110896fd7ce5SGreg Kroah-Hartman else 1109*9bb48c82SLinus Torvalds ret = do_tty_write(ld->ops->write, tty, file, from); 111096fd7ce5SGreg Kroah-Hartman tty_ldisc_deref(ld); 111196fd7ce5SGreg Kroah-Hartman return ret; 111296fd7ce5SGreg Kroah-Hartman } 111396fd7ce5SGreg Kroah-Hartman 1114*9bb48c82SLinus Torvalds ssize_t redirected_tty_write(struct kiocb *iocb, struct iov_iter *iter) 111596fd7ce5SGreg Kroah-Hartman { 111696fd7ce5SGreg Kroah-Hartman struct file *p = NULL; 111796fd7ce5SGreg Kroah-Hartman 111896fd7ce5SGreg Kroah-Hartman spin_lock(&redirect_lock); 1119cb0942b8SAl Viro if (redirect) 1120cb0942b8SAl Viro p = get_file(redirect); 112196fd7ce5SGreg Kroah-Hartman spin_unlock(&redirect_lock); 112296fd7ce5SGreg Kroah-Hartman 112396fd7ce5SGreg Kroah-Hartman if (p) { 112496fd7ce5SGreg Kroah-Hartman ssize_t res; 1125*9bb48c82SLinus Torvalds res = vfs_iocb_iter_write(p, iocb, iter); 112696fd7ce5SGreg Kroah-Hartman fput(p); 112796fd7ce5SGreg Kroah-Hartman return res; 112896fd7ce5SGreg Kroah-Hartman } 1129*9bb48c82SLinus Torvalds return tty_write(iocb, iter); 113096fd7ce5SGreg Kroah-Hartman } 113196fd7ce5SGreg Kroah-Hartman 1132136d5258SPeter Hurley /** 1133136d5258SPeter Hurley * tty_send_xchar - send priority character 1134136d5258SPeter Hurley * 1135136d5258SPeter Hurley * Send a high priority character to the tty even if stopped 1136136d5258SPeter Hurley * 1137136d5258SPeter Hurley * Locking: none for xchar method, write ordering for write method. 1138136d5258SPeter Hurley */ 1139136d5258SPeter Hurley 1140136d5258SPeter Hurley int tty_send_xchar(struct tty_struct *tty, char ch) 1141136d5258SPeter Hurley { 1142136d5258SPeter Hurley int was_stopped = tty->stopped; 1143136d5258SPeter Hurley 1144136d5258SPeter Hurley if (tty->ops->send_xchar) { 1145ee0c1a65SPeter Hurley down_read(&tty->termios_rwsem); 1146136d5258SPeter Hurley tty->ops->send_xchar(tty, ch); 1147ee0c1a65SPeter Hurley up_read(&tty->termios_rwsem); 1148136d5258SPeter Hurley return 0; 1149136d5258SPeter Hurley } 1150136d5258SPeter Hurley 1151136d5258SPeter Hurley if (tty_write_lock(tty, 0) < 0) 1152136d5258SPeter Hurley return -ERESTARTSYS; 1153136d5258SPeter Hurley 1154ee0c1a65SPeter Hurley down_read(&tty->termios_rwsem); 1155136d5258SPeter Hurley if (was_stopped) 1156136d5258SPeter Hurley start_tty(tty); 1157136d5258SPeter Hurley tty->ops->write(tty, &ch, 1); 1158136d5258SPeter Hurley if (was_stopped) 1159136d5258SPeter Hurley stop_tty(tty); 1160ee0c1a65SPeter Hurley up_read(&tty->termios_rwsem); 1161136d5258SPeter Hurley tty_write_unlock(tty); 1162136d5258SPeter Hurley return 0; 1163136d5258SPeter Hurley } 1164136d5258SPeter Hurley 116596fd7ce5SGreg Kroah-Hartman static char ptychar[] = "pqrstuvwxyzabcde"; 116696fd7ce5SGreg Kroah-Hartman 116796fd7ce5SGreg Kroah-Hartman /** 116896fd7ce5SGreg Kroah-Hartman * pty_line_name - generate name for a pty 116996fd7ce5SGreg Kroah-Hartman * @driver: the tty driver in use 117096fd7ce5SGreg Kroah-Hartman * @index: the minor number 117196fd7ce5SGreg Kroah-Hartman * @p: output buffer of at least 6 bytes 117296fd7ce5SGreg Kroah-Hartman * 117396fd7ce5SGreg Kroah-Hartman * Generate a name from a driver reference and write it to the output 117496fd7ce5SGreg Kroah-Hartman * buffer. 117596fd7ce5SGreg Kroah-Hartman * 117696fd7ce5SGreg Kroah-Hartman * Locking: None 117796fd7ce5SGreg Kroah-Hartman */ 117896fd7ce5SGreg Kroah-Hartman static void pty_line_name(struct tty_driver *driver, int index, char *p) 117996fd7ce5SGreg Kroah-Hartman { 118096fd7ce5SGreg Kroah-Hartman int i = index + driver->name_base; 118196fd7ce5SGreg Kroah-Hartman /* ->name is initialized to "ttyp", but "tty" is expected */ 118296fd7ce5SGreg Kroah-Hartman sprintf(p, "%s%c%x", 118396fd7ce5SGreg Kroah-Hartman driver->subtype == PTY_TYPE_SLAVE ? "tty" : driver->name, 118496fd7ce5SGreg Kroah-Hartman ptychar[i >> 4 & 0xf], i & 0xf); 118596fd7ce5SGreg Kroah-Hartman } 118696fd7ce5SGreg Kroah-Hartman 118796fd7ce5SGreg Kroah-Hartman /** 118896fd7ce5SGreg Kroah-Hartman * tty_line_name - generate name for a tty 118996fd7ce5SGreg Kroah-Hartman * @driver: the tty driver in use 119096fd7ce5SGreg Kroah-Hartman * @index: the minor number 119196fd7ce5SGreg Kroah-Hartman * @p: output buffer of at least 7 bytes 119296fd7ce5SGreg Kroah-Hartman * 119396fd7ce5SGreg Kroah-Hartman * Generate a name from a driver reference and write it to the output 11945c0a2450SGreg Kroah-Hartman * buffer. 119596fd7ce5SGreg Kroah-Hartman * 119696fd7ce5SGreg Kroah-Hartman * Locking: None 119796fd7ce5SGreg Kroah-Hartman */ 1198723abd87SHannes Reinecke static ssize_t tty_line_name(struct tty_driver *driver, int index, char *p) 119996fd7ce5SGreg Kroah-Hartman { 12000019b408SJiri Slaby if (driver->flags & TTY_DRIVER_UNNUMBERED_NODE) 1201723abd87SHannes Reinecke return sprintf(p, "%s", driver->name); 12020019b408SJiri Slaby else 1203723abd87SHannes Reinecke return sprintf(p, "%s%d", driver->name, 1204723abd87SHannes Reinecke index + driver->name_base); 120596fd7ce5SGreg Kroah-Hartman } 120696fd7ce5SGreg Kroah-Hartman 120796fd7ce5SGreg Kroah-Hartman /** 120896fd7ce5SGreg Kroah-Hartman * tty_driver_lookup_tty() - find an existing tty, if any 120996fd7ce5SGreg Kroah-Hartman * @driver: the driver for the tty 121096fd7ce5SGreg Kroah-Hartman * @idx: the minor number 121196fd7ce5SGreg Kroah-Hartman * 1212aa3cb814SPeter Hurley * Return the tty, if found. If not found, return NULL or ERR_PTR() if the 1213aa3cb814SPeter Hurley * driver lookup() method returns an error. 121496fd7ce5SGreg Kroah-Hartman * 1215aa3cb814SPeter Hurley * Locking: tty_mutex must be held. If the tty is found, bump the tty kref. 121696fd7ce5SGreg Kroah-Hartman */ 121796fd7ce5SGreg Kroah-Hartman static struct tty_struct *tty_driver_lookup_tty(struct tty_driver *driver, 12188ead9dd5SLinus Torvalds struct file *file, int idx) 121996fd7ce5SGreg Kroah-Hartman { 1220aa3cb814SPeter Hurley struct tty_struct *tty; 122196fd7ce5SGreg Kroah-Hartman 1222aa3cb814SPeter Hurley if (driver->ops->lookup) 122312e84c71SOkash Khawaja if (!file) 122412e84c71SOkash Khawaja tty = ERR_PTR(-EIO); 122512e84c71SOkash Khawaja else 12268ead9dd5SLinus Torvalds tty = driver->ops->lookup(driver, file, idx); 1227aa3cb814SPeter Hurley else 1228aa3cb814SPeter Hurley tty = driver->ttys[idx]; 1229aa3cb814SPeter Hurley 1230aa3cb814SPeter Hurley if (!IS_ERR(tty)) 1231aa3cb814SPeter Hurley tty_kref_get(tty); 1232aa3cb814SPeter Hurley return tty; 123396fd7ce5SGreg Kroah-Hartman } 123496fd7ce5SGreg Kroah-Hartman 123596fd7ce5SGreg Kroah-Hartman /** 123696fd7ce5SGreg Kroah-Hartman * tty_init_termios - helper for termios setup 123796fd7ce5SGreg Kroah-Hartman * @tty: the tty to set up 123896fd7ce5SGreg Kroah-Hartman * 12398daa89e0SJohan Hovold * Initialise the termios structure for this tty. This runs under 124096fd7ce5SGreg Kroah-Hartman * the tty_mutex currently so we can be relaxed about ordering. 124196fd7ce5SGreg Kroah-Hartman */ 124296fd7ce5SGreg Kroah-Hartman 1243a3123fd0SPeter Hurley void tty_init_termios(struct tty_struct *tty) 124496fd7ce5SGreg Kroah-Hartman { 124596fd7ce5SGreg Kroah-Hartman struct ktermios *tp; 124696fd7ce5SGreg Kroah-Hartman int idx = tty->index; 124796fd7ce5SGreg Kroah-Hartman 124836b3c070SAlan Cox if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) 124936b3c070SAlan Cox tty->termios = tty->driver->init_termios; 125036b3c070SAlan Cox else { 125136b3c070SAlan Cox /* Check for lazy saved data */ 125296fd7ce5SGreg Kroah-Hartman tp = tty->driver->termios[idx]; 1253ece53405SPeter Hurley if (tp != NULL) { 1254adc8d746SAlan Cox tty->termios = *tp; 1255ece53405SPeter Hurley tty->termios.c_line = tty->driver->init_termios.c_line; 1256ece53405SPeter Hurley } else 125736b3c070SAlan Cox tty->termios = tty->driver->init_termios; 125836b3c070SAlan Cox } 125996fd7ce5SGreg Kroah-Hartman /* Compatibility until drivers always set this */ 1260adc8d746SAlan Cox tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios); 1261adc8d746SAlan Cox tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios); 126296fd7ce5SGreg Kroah-Hartman } 126396fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(tty_init_termios); 126496fd7ce5SGreg Kroah-Hartman 126566d450e8SJiri Slaby int tty_standard_install(struct tty_driver *driver, struct tty_struct *tty) 126666d450e8SJiri Slaby { 1267a3123fd0SPeter Hurley tty_init_termios(tty); 126866d450e8SJiri Slaby tty_driver_kref_get(driver); 126966d450e8SJiri Slaby tty->count++; 127066d450e8SJiri Slaby driver->ttys[tty->index] = tty; 127166d450e8SJiri Slaby return 0; 127266d450e8SJiri Slaby } 127366d450e8SJiri Slaby EXPORT_SYMBOL_GPL(tty_standard_install); 127466d450e8SJiri Slaby 127596fd7ce5SGreg Kroah-Hartman /** 127696fd7ce5SGreg Kroah-Hartman * tty_driver_install_tty() - install a tty entry in the driver 127796fd7ce5SGreg Kroah-Hartman * @driver: the driver for the tty 127896fd7ce5SGreg Kroah-Hartman * @tty: the tty 127996fd7ce5SGreg Kroah-Hartman * 128096fd7ce5SGreg Kroah-Hartman * Install a tty object into the driver tables. The tty->index field 128196fd7ce5SGreg Kroah-Hartman * will be set by the time this is called. This method is responsible 128296fd7ce5SGreg Kroah-Hartman * for ensuring any need additional structures are allocated and 128396fd7ce5SGreg Kroah-Hartman * configured. 128496fd7ce5SGreg Kroah-Hartman * 128596fd7ce5SGreg Kroah-Hartman * Locking: tty_mutex for now 128696fd7ce5SGreg Kroah-Hartman */ 128796fd7ce5SGreg Kroah-Hartman static int tty_driver_install_tty(struct tty_driver *driver, 128896fd7ce5SGreg Kroah-Hartman struct tty_struct *tty) 128996fd7ce5SGreg Kroah-Hartman { 129066d450e8SJiri Slaby return driver->ops->install ? driver->ops->install(driver, tty) : 129166d450e8SJiri Slaby tty_standard_install(driver, tty); 129296fd7ce5SGreg Kroah-Hartman } 129396fd7ce5SGreg Kroah-Hartman 129496fd7ce5SGreg Kroah-Hartman /** 129596fd7ce5SGreg Kroah-Hartman * tty_driver_remove_tty() - remove a tty from the driver tables 129696fd7ce5SGreg Kroah-Hartman * @driver: the driver for the tty 1297fa441954SJiri Slaby * @tty: tty to remove 129896fd7ce5SGreg Kroah-Hartman * 129996fd7ce5SGreg Kroah-Hartman * Remvoe a tty object from the driver tables. The tty->index field 130096fd7ce5SGreg Kroah-Hartman * will be set by the time this is called. 130196fd7ce5SGreg Kroah-Hartman * 130296fd7ce5SGreg Kroah-Hartman * Locking: tty_mutex for now 130396fd7ce5SGreg Kroah-Hartman */ 130405de87edSPeter Hurley static void tty_driver_remove_tty(struct tty_driver *driver, struct tty_struct *tty) 130596fd7ce5SGreg Kroah-Hartman { 130696fd7ce5SGreg Kroah-Hartman if (driver->ops->remove) 130796fd7ce5SGreg Kroah-Hartman driver->ops->remove(driver, tty); 130896fd7ce5SGreg Kroah-Hartman else 130996fd7ce5SGreg Kroah-Hartman driver->ttys[tty->index] = NULL; 131096fd7ce5SGreg Kroah-Hartman } 131196fd7ce5SGreg Kroah-Hartman 1312fa441954SJiri Slaby /** 131396fd7ce5SGreg Kroah-Hartman * tty_reopen() - fast re-open of an open tty 1314fa441954SJiri Slaby * @tty: the tty to open 131596fd7ce5SGreg Kroah-Hartman * 131696fd7ce5SGreg Kroah-Hartman * Return 0 on success, -errno on error. 13175d93e748SPeter Hurley * Re-opens on master ptys are not allowed and return -EIO. 131896fd7ce5SGreg Kroah-Hartman * 13195d93e748SPeter Hurley * Locking: Caller must hold tty_lock 132096fd7ce5SGreg Kroah-Hartman */ 132196fd7ce5SGreg Kroah-Hartman static int tty_reopen(struct tty_struct *tty) 132296fd7ce5SGreg Kroah-Hartman { 132396fd7ce5SGreg Kroah-Hartman struct tty_driver *driver = tty->driver; 1324d3736d82SDmitry Safonov struct tty_ldisc *ld; 1325d3736d82SDmitry Safonov int retval = 0; 132696fd7ce5SGreg Kroah-Hartman 132796fd7ce5SGreg Kroah-Hartman if (driver->type == TTY_DRIVER_TYPE_PTY && 13285d93e748SPeter Hurley driver->subtype == PTY_TYPE_MASTER) 132996fd7ce5SGreg Kroah-Hartman return -EIO; 133096fd7ce5SGreg Kroah-Hartman 13317f22f6c9SPeter Hurley if (!tty->count) 13327f22f6c9SPeter Hurley return -EAGAIN; 13337f22f6c9SPeter Hurley 133486f2c00fSPeter Hurley if (test_bit(TTY_EXCLUSIVE, &tty->flags) && !capable(CAP_SYS_ADMIN)) 133586f2c00fSPeter Hurley return -EBUSY; 133686f2c00fSPeter Hurley 1337d3736d82SDmitry Safonov ld = tty_ldisc_ref_wait(tty); 1338d3736d82SDmitry Safonov if (ld) { 1339d3736d82SDmitry Safonov tty_ldisc_deref(ld); 1340d3736d82SDmitry Safonov } else { 134183d817f4SDmitry Safonov retval = tty_ldisc_lock(tty, 5 * HZ); 134283d817f4SDmitry Safonov if (retval) 134383d817f4SDmitry Safonov return retval; 134496fd7ce5SGreg Kroah-Hartman 1345cf62a1a1SDmitry Safonov if (!tty->ldisc) 1346fe324167SDmitry Safonov retval = tty_ldisc_reinit(tty, tty->termios.c_line); 134783d817f4SDmitry Safonov tty_ldisc_unlock(tty); 1348d3736d82SDmitry Safonov } 1349cf62a1a1SDmitry Safonov 1350cf62a1a1SDmitry Safonov if (retval == 0) 1351cf62a1a1SDmitry Safonov tty->count++; 1352cf62a1a1SDmitry Safonov 1353fe324167SDmitry Safonov return retval; 135496fd7ce5SGreg Kroah-Hartman } 135596fd7ce5SGreg Kroah-Hartman 135696fd7ce5SGreg Kroah-Hartman /** 135796fd7ce5SGreg Kroah-Hartman * tty_init_dev - initialise a tty device 135896fd7ce5SGreg Kroah-Hartman * @driver: tty driver we are opening a device on 135996fd7ce5SGreg Kroah-Hartman * @idx: device index 136096fd7ce5SGreg Kroah-Hartman * 136196fd7ce5SGreg Kroah-Hartman * Prepare a tty device. This may not be a "new" clean device but 136296fd7ce5SGreg Kroah-Hartman * could also be an active device. The pty drivers require special 136396fd7ce5SGreg Kroah-Hartman * handling because of this. 136496fd7ce5SGreg Kroah-Hartman * 136596fd7ce5SGreg Kroah-Hartman * Locking: 136696fd7ce5SGreg Kroah-Hartman * The function is called under the tty_mutex, which 136796fd7ce5SGreg Kroah-Hartman * protects us from the tty struct or driver itself going away. 136896fd7ce5SGreg Kroah-Hartman * 136996fd7ce5SGreg Kroah-Hartman * On exit the tty device has the line discipline attached and 137096fd7ce5SGreg Kroah-Hartman * a reference count of 1. If a pair was created for pty/tty use 137196fd7ce5SGreg Kroah-Hartman * and the other was a pty master then it too has a reference count of 1. 137296fd7ce5SGreg Kroah-Hartman * 137396fd7ce5SGreg Kroah-Hartman * WSH 06/09/97: Rewritten to remove races and properly clean up after a 137496fd7ce5SGreg Kroah-Hartman * failed open. The new code protects the open with a mutex, so it's 137596fd7ce5SGreg Kroah-Hartman * really quite straightforward. The mutex locking can probably be 137696fd7ce5SGreg Kroah-Hartman * relaxed for the (most common) case of reopening a tty. 1377fa441954SJiri Slaby * 1378fa441954SJiri Slaby * Return: returned tty structure 137996fd7ce5SGreg Kroah-Hartman */ 138096fd7ce5SGreg Kroah-Hartman 1381593a27c4SKonstantin Khlebnikov struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx) 138296fd7ce5SGreg Kroah-Hartman { 138396fd7ce5SGreg Kroah-Hartman struct tty_struct *tty; 138496fd7ce5SGreg Kroah-Hartman int retval; 138596fd7ce5SGreg Kroah-Hartman 138696fd7ce5SGreg Kroah-Hartman /* 138796fd7ce5SGreg Kroah-Hartman * First time open is complex, especially for PTY devices. 138896fd7ce5SGreg Kroah-Hartman * This code guarantees that either everything succeeds and the 138996fd7ce5SGreg Kroah-Hartman * TTY is ready for operation, or else the table slots are vacated 139096fd7ce5SGreg Kroah-Hartman * and the allocated memory released. (Except that the termios 139116b00ae8SJohan Hovold * may be retained.) 139296fd7ce5SGreg Kroah-Hartman */ 139396fd7ce5SGreg Kroah-Hartman 139496fd7ce5SGreg Kroah-Hartman if (!try_module_get(driver->owner)) 139596fd7ce5SGreg Kroah-Hartman return ERR_PTR(-ENODEV); 139696fd7ce5SGreg Kroah-Hartman 13972c964a2fSRasmus Villemoes tty = alloc_tty_struct(driver, idx); 1398d5543503SJiri Slaby if (!tty) { 1399d5543503SJiri Slaby retval = -ENOMEM; 1400d5543503SJiri Slaby goto err_module_put; 1401d5543503SJiri Slaby } 140296fd7ce5SGreg Kroah-Hartman 140389c8d91eSAlan Cox tty_lock(tty); 140496fd7ce5SGreg Kroah-Hartman retval = tty_driver_install_tty(driver, tty); 1405d5543503SJiri Slaby if (retval < 0) 1406c8b710b3SPeter Hurley goto err_free_tty; 140796fd7ce5SGreg Kroah-Hartman 140804831dc1SJiri Slaby if (!tty->port) 140904831dc1SJiri Slaby tty->port = driver->ports[idx]; 141004831dc1SJiri Slaby 14112ae0b31eSJiri Slaby if (WARN_RATELIMIT(!tty->port, 14122ae0b31eSJiri Slaby "%s: %s driver does not set tty->port. This would crash the kernel. Fix the driver!\n", 14132ae0b31eSJiri Slaby __func__, tty->driver->name)) { 14142ae0b31eSJiri Slaby retval = -EINVAL; 14152ae0b31eSJiri Slaby goto err_release_lock; 14162ae0b31eSJiri Slaby } 14175d4121c0SJiri Slaby 1418b027e229SGaurav Kohli retval = tty_ldisc_lock(tty, 5 * HZ); 1419b027e229SGaurav Kohli if (retval) 1420b027e229SGaurav Kohli goto err_release_lock; 1421967fab69SJiri Slaby tty->port->itty = tty; 1422967fab69SJiri Slaby 142396fd7ce5SGreg Kroah-Hartman /* 142496fd7ce5SGreg Kroah-Hartman * Structures all installed ... call the ldisc open routines. 142596fd7ce5SGreg Kroah-Hartman * If we fail here just call release_tty to clean up. No need 142696fd7ce5SGreg Kroah-Hartman * to decrement the use counts, as release_tty doesn't care. 142796fd7ce5SGreg Kroah-Hartman */ 142896fd7ce5SGreg Kroah-Hartman retval = tty_ldisc_setup(tty, tty->link); 142996fd7ce5SGreg Kroah-Hartman if (retval) 1430d5543503SJiri Slaby goto err_release_tty; 1431b027e229SGaurav Kohli tty_ldisc_unlock(tty); 143289c8d91eSAlan Cox /* Return the tty locked so that it cannot vanish under the caller */ 143396fd7ce5SGreg Kroah-Hartman return tty; 143496fd7ce5SGreg Kroah-Hartman 1435c8b710b3SPeter Hurley err_free_tty: 143689c8d91eSAlan Cox tty_unlock(tty); 1437d5543503SJiri Slaby free_tty_struct(tty); 1438d5543503SJiri Slaby err_module_put: 143996fd7ce5SGreg Kroah-Hartman module_put(driver->owner); 1440d5543503SJiri Slaby return ERR_PTR(retval); 144196fd7ce5SGreg Kroah-Hartman 144296fd7ce5SGreg Kroah-Hartman /* call the tty release_tty routine to clean out this slot */ 1443d5543503SJiri Slaby err_release_tty: 1444b027e229SGaurav Kohli tty_ldisc_unlock(tty); 1445339f36baSPeter Hurley tty_info_ratelimited(tty, "ldisc open failed (%d), clearing slot %d\n", 1446339f36baSPeter Hurley retval, idx); 1447b027e229SGaurav Kohli err_release_lock: 1448b027e229SGaurav Kohli tty_unlock(tty); 144996fd7ce5SGreg Kroah-Hartman release_tty(tty, idx); 145096fd7ce5SGreg Kroah-Hartman return ERR_PTR(retval); 145196fd7ce5SGreg Kroah-Hartman } 145296fd7ce5SGreg Kroah-Hartman 1453f51ccf46SJohan Hovold /** 1454f51ccf46SJohan Hovold * tty_save_termios() - save tty termios data in driver table 1455f51ccf46SJohan Hovold * @tty: tty whose termios data to save 1456f51ccf46SJohan Hovold * 1457f51ccf46SJohan Hovold * Locking: Caller guarantees serialisation with tty_init_termios(). 1458f51ccf46SJohan Hovold */ 1459f51ccf46SJohan Hovold void tty_save_termios(struct tty_struct *tty) 146096fd7ce5SGreg Kroah-Hartman { 146196fd7ce5SGreg Kroah-Hartman struct ktermios *tp; 146296fd7ce5SGreg Kroah-Hartman int idx = tty->index; 146336b3c070SAlan Cox 146436b3c070SAlan Cox /* If the port is going to reset then it has no termios to save */ 146536b3c070SAlan Cox if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) 146636b3c070SAlan Cox return; 146736b3c070SAlan Cox 146836b3c070SAlan Cox /* Stash the termios data */ 1469adc8d746SAlan Cox tp = tty->driver->termios[idx]; 147036b3c070SAlan Cox if (tp == NULL) { 1471a3241891SGustavo A. R. Silva tp = kmalloc(sizeof(*tp), GFP_KERNEL); 147225080652SPeter Hurley if (tp == NULL) 147336b3c070SAlan Cox return; 14744ac5d705SDan Carpenter tty->driver->termios[idx] = tp; 147536b3c070SAlan Cox } 147636b3c070SAlan Cox *tp = tty->termios; 147796fd7ce5SGreg Kroah-Hartman } 1478f51ccf46SJohan Hovold EXPORT_SYMBOL_GPL(tty_save_termios); 147996fd7ce5SGreg Kroah-Hartman 1480a2965b7bSPeter Hurley /** 1481949aa64fSPeter Hurley * tty_flush_works - flush all works of a tty/pty pair 1482949aa64fSPeter Hurley * @tty: tty device to flush works for (or either end of a pty pair) 1483a2965b7bSPeter Hurley * 1484949aa64fSPeter Hurley * Sync flush all works belonging to @tty (and the 'other' tty). 1485a2965b7bSPeter Hurley */ 1486a2965b7bSPeter Hurley static void tty_flush_works(struct tty_struct *tty) 1487a2965b7bSPeter Hurley { 1488a2965b7bSPeter Hurley flush_work(&tty->SAK_work); 1489a2965b7bSPeter Hurley flush_work(&tty->hangup_work); 1490949aa64fSPeter Hurley if (tty->link) { 1491949aa64fSPeter Hurley flush_work(&tty->link->SAK_work); 1492949aa64fSPeter Hurley flush_work(&tty->link->hangup_work); 1493949aa64fSPeter Hurley } 1494a2965b7bSPeter Hurley } 149596fd7ce5SGreg Kroah-Hartman 149696fd7ce5SGreg Kroah-Hartman /** 149796fd7ce5SGreg Kroah-Hartman * release_one_tty - release tty structure memory 1498fa441954SJiri Slaby * @work: work of tty we are obliterating 149996fd7ce5SGreg Kroah-Hartman * 150096fd7ce5SGreg Kroah-Hartman * Releases memory associated with a tty structure, and clears out the 150196fd7ce5SGreg Kroah-Hartman * driver table slots. This function is called when a device is no longer 150296fd7ce5SGreg Kroah-Hartman * in use. It also gets called when setup of a device fails. 150396fd7ce5SGreg Kroah-Hartman * 150496fd7ce5SGreg Kroah-Hartman * Locking: 150596fd7ce5SGreg Kroah-Hartman * takes the file list lock internally when working on the list 150696fd7ce5SGreg Kroah-Hartman * of ttys that the driver keeps. 150796fd7ce5SGreg Kroah-Hartman * 150896fd7ce5SGreg Kroah-Hartman * This method gets called from a work queue so that the driver private 150996fd7ce5SGreg Kroah-Hartman * cleanup ops can sleep (needed for USB at least) 151096fd7ce5SGreg Kroah-Hartman */ 151196fd7ce5SGreg Kroah-Hartman static void release_one_tty(struct work_struct *work) 151296fd7ce5SGreg Kroah-Hartman { 151396fd7ce5SGreg Kroah-Hartman struct tty_struct *tty = 151496fd7ce5SGreg Kroah-Hartman container_of(work, struct tty_struct, hangup_work); 151596fd7ce5SGreg Kroah-Hartman struct tty_driver *driver = tty->driver; 1516b216df53SCyrill Gorcunov struct module *owner = driver->owner; 151796fd7ce5SGreg Kroah-Hartman 151896fd7ce5SGreg Kroah-Hartman if (tty->ops->cleanup) 151996fd7ce5SGreg Kroah-Hartman tty->ops->cleanup(tty); 152096fd7ce5SGreg Kroah-Hartman 152196fd7ce5SGreg Kroah-Hartman tty->magic = 0; 152296fd7ce5SGreg Kroah-Hartman tty_driver_kref_put(driver); 1523b216df53SCyrill Gorcunov module_put(owner); 152496fd7ce5SGreg Kroah-Hartman 15254a510969SPeter Hurley spin_lock(&tty->files_lock); 152696fd7ce5SGreg Kroah-Hartman list_del_init(&tty->tty_files); 15274a510969SPeter Hurley spin_unlock(&tty->files_lock); 152896fd7ce5SGreg Kroah-Hartman 152996fd7ce5SGreg Kroah-Hartman put_pid(tty->pgrp); 153096fd7ce5SGreg Kroah-Hartman put_pid(tty->session); 153196fd7ce5SGreg Kroah-Hartman free_tty_struct(tty); 153296fd7ce5SGreg Kroah-Hartman } 153396fd7ce5SGreg Kroah-Hartman 153496fd7ce5SGreg Kroah-Hartman static void queue_release_one_tty(struct kref *kref) 153596fd7ce5SGreg Kroah-Hartman { 153696fd7ce5SGreg Kroah-Hartman struct tty_struct *tty = container_of(kref, struct tty_struct, kref); 153796fd7ce5SGreg Kroah-Hartman 153896fd7ce5SGreg Kroah-Hartman /* The hangup queue is now free so we can reuse it rather than 153996fd7ce5SGreg Kroah-Hartman waste a chunk of memory for each port */ 154096fd7ce5SGreg Kroah-Hartman INIT_WORK(&tty->hangup_work, release_one_tty); 154196fd7ce5SGreg Kroah-Hartman schedule_work(&tty->hangup_work); 154296fd7ce5SGreg Kroah-Hartman } 154396fd7ce5SGreg Kroah-Hartman 154496fd7ce5SGreg Kroah-Hartman /** 154596fd7ce5SGreg Kroah-Hartman * tty_kref_put - release a tty kref 154696fd7ce5SGreg Kroah-Hartman * @tty: tty device 154796fd7ce5SGreg Kroah-Hartman * 154896fd7ce5SGreg Kroah-Hartman * Release a reference to a tty device and if need be let the kref 154996fd7ce5SGreg Kroah-Hartman * layer destruct the object for us 155096fd7ce5SGreg Kroah-Hartman */ 155196fd7ce5SGreg Kroah-Hartman 155296fd7ce5SGreg Kroah-Hartman void tty_kref_put(struct tty_struct *tty) 155396fd7ce5SGreg Kroah-Hartman { 155496fd7ce5SGreg Kroah-Hartman if (tty) 155596fd7ce5SGreg Kroah-Hartman kref_put(&tty->kref, queue_release_one_tty); 155696fd7ce5SGreg Kroah-Hartman } 155796fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_kref_put); 155896fd7ce5SGreg Kroah-Hartman 155996fd7ce5SGreg Kroah-Hartman /** 156096fd7ce5SGreg Kroah-Hartman * release_tty - release tty structure memory 156196fd7ce5SGreg Kroah-Hartman * 156296fd7ce5SGreg Kroah-Hartman * Release both @tty and a possible linked partner (think pty pair), 156396fd7ce5SGreg Kroah-Hartman * and decrement the refcount of the backing module. 156496fd7ce5SGreg Kroah-Hartman * 156596fd7ce5SGreg Kroah-Hartman * Locking: 1566d155255aSAlan Cox * tty_mutex 156796fd7ce5SGreg Kroah-Hartman * takes the file list lock internally when working on the list 156896fd7ce5SGreg Kroah-Hartman * of ttys that the driver keeps. 156996fd7ce5SGreg Kroah-Hartman * 157096fd7ce5SGreg Kroah-Hartman */ 157196fd7ce5SGreg Kroah-Hartman static void release_tty(struct tty_struct *tty, int idx) 157296fd7ce5SGreg Kroah-Hartman { 157396fd7ce5SGreg Kroah-Hartman /* This should always be true but check for the moment */ 157496fd7ce5SGreg Kroah-Hartman WARN_ON(tty->index != idx); 1575d155255aSAlan Cox WARN_ON(!mutex_is_locked(&tty_mutex)); 157636b3c070SAlan Cox if (tty->ops->shutdown) 157736b3c070SAlan Cox tty->ops->shutdown(tty); 1578f51ccf46SJohan Hovold tty_save_termios(tty); 157936b3c070SAlan Cox tty_driver_remove_tty(tty->driver, tty); 15804466d6d2SMatthias Reichl if (tty->port) 1581967fab69SJiri Slaby tty->port->itty = NULL; 158264e377dcSPeter Hurley if (tty->link) 158364e377dcSPeter Hurley tty->link->port->itty = NULL; 15844466d6d2SMatthias Reichl if (tty->port) 1585e176058fSPeter Hurley tty_buffer_cancel_work(tty->port); 15862b022ab7SSahara if (tty->link) 15872b022ab7SSahara tty_buffer_cancel_work(tty->link->port); 158836b3c070SAlan Cox 158996fd7ce5SGreg Kroah-Hartman tty_kref_put(tty->link); 159096fd7ce5SGreg Kroah-Hartman tty_kref_put(tty); 159196fd7ce5SGreg Kroah-Hartman } 159296fd7ce5SGreg Kroah-Hartman 159396fd7ce5SGreg Kroah-Hartman /** 1594955787caSJiri Slaby * tty_release_checks - check a tty before real release 1595955787caSJiri Slaby * @tty: tty to check 1596955787caSJiri Slaby * @idx: index of the tty 1597955787caSJiri Slaby * 1598955787caSJiri Slaby * Performs some paranoid checking before true release of the @tty. 1599955787caSJiri Slaby * This is a no-op unless TTY_PARANOIA_CHECK is defined. 1600955787caSJiri Slaby */ 1601359b9fb5SPeter Hurley static int tty_release_checks(struct tty_struct *tty, int idx) 1602955787caSJiri Slaby { 1603955787caSJiri Slaby #ifdef TTY_PARANOIA_CHECK 1604955787caSJiri Slaby if (idx < 0 || idx >= tty->driver->num) { 1605e2dfa3d3SPeter Hurley tty_debug(tty, "bad idx %d\n", idx); 1606955787caSJiri Slaby return -1; 1607955787caSJiri Slaby } 1608955787caSJiri Slaby 1609955787caSJiri Slaby /* not much to check for devpts */ 1610955787caSJiri Slaby if (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM) 1611955787caSJiri Slaby return 0; 1612955787caSJiri Slaby 1613955787caSJiri Slaby if (tty != tty->driver->ttys[idx]) { 1614e2dfa3d3SPeter Hurley tty_debug(tty, "bad driver table[%d] = %p\n", 1615e2dfa3d3SPeter Hurley idx, tty->driver->ttys[idx]); 1616955787caSJiri Slaby return -1; 1617955787caSJiri Slaby } 1618955787caSJiri Slaby if (tty->driver->other) { 1619359b9fb5SPeter Hurley struct tty_struct *o_tty = tty->link; 1620359b9fb5SPeter Hurley 1621955787caSJiri Slaby if (o_tty != tty->driver->other->ttys[idx]) { 1622e2dfa3d3SPeter Hurley tty_debug(tty, "bad other table[%d] = %p\n", 1623e2dfa3d3SPeter Hurley idx, tty->driver->other->ttys[idx]); 1624955787caSJiri Slaby return -1; 1625955787caSJiri Slaby } 1626955787caSJiri Slaby if (o_tty->link != tty) { 1627e2dfa3d3SPeter Hurley tty_debug(tty, "bad link = %p\n", o_tty->link); 1628955787caSJiri Slaby return -1; 1629955787caSJiri Slaby } 1630955787caSJiri Slaby } 1631955787caSJiri Slaby #endif 1632955787caSJiri Slaby return 0; 1633955787caSJiri Slaby } 1634955787caSJiri Slaby 1635955787caSJiri Slaby /** 1636a09ac397SOkash Khawaja * tty_kclose - closes tty opened by tty_kopen 1637a09ac397SOkash Khawaja * @tty: tty device 1638a09ac397SOkash Khawaja * 1639a09ac397SOkash Khawaja * Performs the final steps to release and free a tty device. It is the 1640a09ac397SOkash Khawaja * same as tty_release_struct except that it also resets TTY_PORT_KOPENED 1641a09ac397SOkash Khawaja * flag on tty->port. 1642a09ac397SOkash Khawaja */ 1643a09ac397SOkash Khawaja void tty_kclose(struct tty_struct *tty) 1644a09ac397SOkash Khawaja { 1645a09ac397SOkash Khawaja /* 1646a09ac397SOkash Khawaja * Ask the line discipline code to release its structures 1647a09ac397SOkash Khawaja */ 1648a09ac397SOkash Khawaja tty_ldisc_release(tty); 1649a09ac397SOkash Khawaja 1650a09ac397SOkash Khawaja /* Wait for pending work before tty destruction commmences */ 1651a09ac397SOkash Khawaja tty_flush_works(tty); 1652a09ac397SOkash Khawaja 1653a09ac397SOkash Khawaja tty_debug_hangup(tty, "freeing structure\n"); 1654a09ac397SOkash Khawaja /* 1655a09ac397SOkash Khawaja * The release_tty function takes care of the details of clearing 1656ed069827SEric Biggers * the slots and preserving the termios structure. 1657a09ac397SOkash Khawaja */ 1658a09ac397SOkash Khawaja mutex_lock(&tty_mutex); 1659a09ac397SOkash Khawaja tty_port_set_kopened(tty->port, 0); 1660a09ac397SOkash Khawaja release_tty(tty, tty->index); 1661a09ac397SOkash Khawaja mutex_unlock(&tty_mutex); 1662a09ac397SOkash Khawaja } 1663a09ac397SOkash Khawaja EXPORT_SYMBOL_GPL(tty_kclose); 1664a09ac397SOkash Khawaja 1665a09ac397SOkash Khawaja /** 16669ed90d20SRob Herring * tty_release_struct - release a tty struct 16679ed90d20SRob Herring * @tty: tty device 16689ed90d20SRob Herring * @idx: index of the tty 16699ed90d20SRob Herring * 16709ed90d20SRob Herring * Performs the final steps to release and free a tty device. It is 16719ed90d20SRob Herring * roughly the reverse of tty_init_dev. 16729ed90d20SRob Herring */ 16739ed90d20SRob Herring void tty_release_struct(struct tty_struct *tty, int idx) 16749ed90d20SRob Herring { 16759ed90d20SRob Herring /* 16769ed90d20SRob Herring * Ask the line discipline code to release its structures 16779ed90d20SRob Herring */ 16789ed90d20SRob Herring tty_ldisc_release(tty); 16799ed90d20SRob Herring 16809ed90d20SRob Herring /* Wait for pending work before tty destruction commmences */ 16819ed90d20SRob Herring tty_flush_works(tty); 16829ed90d20SRob Herring 16839ed90d20SRob Herring tty_debug_hangup(tty, "freeing structure\n"); 16849ed90d20SRob Herring /* 16859ed90d20SRob Herring * The release_tty function takes care of the details of clearing 1686ed069827SEric Biggers * the slots and preserving the termios structure. 16879ed90d20SRob Herring */ 16889ed90d20SRob Herring mutex_lock(&tty_mutex); 16899ed90d20SRob Herring release_tty(tty, idx); 16909ed90d20SRob Herring mutex_unlock(&tty_mutex); 16919ed90d20SRob Herring } 16929ed90d20SRob Herring EXPORT_SYMBOL_GPL(tty_release_struct); 16939ed90d20SRob Herring 16949ed90d20SRob Herring /** 169596fd7ce5SGreg Kroah-Hartman * tty_release - vfs callback for close 169696fd7ce5SGreg Kroah-Hartman * @inode: inode of tty 169796fd7ce5SGreg Kroah-Hartman * @filp: file pointer for handle to tty 169896fd7ce5SGreg Kroah-Hartman * 169996fd7ce5SGreg Kroah-Hartman * Called the last time each file handle is closed that references 170096fd7ce5SGreg Kroah-Hartman * this tty. There may however be several such references. 170196fd7ce5SGreg Kroah-Hartman * 170296fd7ce5SGreg Kroah-Hartman * Locking: 170396fd7ce5SGreg Kroah-Hartman * Takes bkl. See tty_release_dev 170496fd7ce5SGreg Kroah-Hartman * 170596fd7ce5SGreg Kroah-Hartman * Even releasing the tty structures is a tricky business.. We have 170696fd7ce5SGreg Kroah-Hartman * to be very careful that the structures are all released at the 170796fd7ce5SGreg Kroah-Hartman * same time, as interrupts might otherwise get the wrong pointers. 170896fd7ce5SGreg Kroah-Hartman * 170996fd7ce5SGreg Kroah-Hartman * WSH 09/09/97: rewritten to avoid some nasty race conditions that could 171096fd7ce5SGreg Kroah-Hartman * lead to double frees or releasing memory still in use. 171196fd7ce5SGreg Kroah-Hartman */ 171296fd7ce5SGreg Kroah-Hartman 171396fd7ce5SGreg Kroah-Hartman int tty_release(struct inode *inode, struct file *filp) 171496fd7ce5SGreg Kroah-Hartman { 171596fd7ce5SGreg Kroah-Hartman struct tty_struct *tty = file_tty(filp); 17167ffb6da9SPeter Hurley struct tty_struct *o_tty = NULL; 17177ffb6da9SPeter Hurley int do_sleep, final; 171896fd7ce5SGreg Kroah-Hartman int idx; 171937b16457SPeter Hurley long timeout = 0; 1720494c1eacSPeter Hurley int once = 1; 172196fd7ce5SGreg Kroah-Hartman 17229de44bd6SJiri Slaby if (tty_paranoia_check(tty, inode, __func__)) 172396fd7ce5SGreg Kroah-Hartman return 0; 172496fd7ce5SGreg Kroah-Hartman 172589c8d91eSAlan Cox tty_lock(tty); 17269de44bd6SJiri Slaby check_tty_count(tty, __func__); 172796fd7ce5SGreg Kroah-Hartman 172896fd7ce5SGreg Kroah-Hartman __tty_fasync(-1, filp, 0); 172996fd7ce5SGreg Kroah-Hartman 173096fd7ce5SGreg Kroah-Hartman idx = tty->index; 17317ffb6da9SPeter Hurley if (tty->driver->type == TTY_DRIVER_TYPE_PTY && 17327ffb6da9SPeter Hurley tty->driver->subtype == PTY_TYPE_MASTER) 173396fd7ce5SGreg Kroah-Hartman o_tty = tty->link; 173496fd7ce5SGreg Kroah-Hartman 1735359b9fb5SPeter Hurley if (tty_release_checks(tty, idx)) { 173689c8d91eSAlan Cox tty_unlock(tty); 173796fd7ce5SGreg Kroah-Hartman return 0; 173896fd7ce5SGreg Kroah-Hartman } 173996fd7ce5SGreg Kroah-Hartman 1740d435cefeSPeter Hurley tty_debug_hangup(tty, "releasing (count=%d)\n", tty->count); 174196fd7ce5SGreg Kroah-Hartman 174296fd7ce5SGreg Kroah-Hartman if (tty->ops->close) 174396fd7ce5SGreg Kroah-Hartman tty->ops->close(tty, filp); 174496fd7ce5SGreg Kroah-Hartman 17452aff5e2bSPeter Hurley /* If tty is pty master, lock the slave pty (stable lock order) */ 17462aff5e2bSPeter Hurley tty_lock_slave(o_tty); 17472aff5e2bSPeter Hurley 174896fd7ce5SGreg Kroah-Hartman /* 174996fd7ce5SGreg Kroah-Hartman * Sanity check: if tty->count is going to zero, there shouldn't be 175096fd7ce5SGreg Kroah-Hartman * any waiters on tty->read_wait or tty->write_wait. We test the 175196fd7ce5SGreg Kroah-Hartman * wait queues and kick everyone out _before_ actually starting to 175296fd7ce5SGreg Kroah-Hartman * close. This ensures that we won't block while releasing the tty 175396fd7ce5SGreg Kroah-Hartman * structure. 175496fd7ce5SGreg Kroah-Hartman * 175596fd7ce5SGreg Kroah-Hartman * The test for the o_tty closing is necessary, since the master and 175696fd7ce5SGreg Kroah-Hartman * slave sides may close in any order. If the slave side closes out 175796fd7ce5SGreg Kroah-Hartman * first, its count will be one, since the master side holds an open. 1758324c1650SPeter Hurley * Thus this test wouldn't be triggered at the time the slave closed, 175996fd7ce5SGreg Kroah-Hartman * so we do it now. 176096fd7ce5SGreg Kroah-Hartman */ 1761d5e370a4SPeter Hurley while (1) { 176296fd7ce5SGreg Kroah-Hartman do_sleep = 0; 176396fd7ce5SGreg Kroah-Hartman 1764324c1650SPeter Hurley if (tty->count <= 1) { 176596fd7ce5SGreg Kroah-Hartman if (waitqueue_active(&tty->read_wait)) { 1766a9a08845SLinus Torvalds wake_up_poll(&tty->read_wait, EPOLLIN); 176796fd7ce5SGreg Kroah-Hartman do_sleep++; 176896fd7ce5SGreg Kroah-Hartman } 176996fd7ce5SGreg Kroah-Hartman if (waitqueue_active(&tty->write_wait)) { 1770a9a08845SLinus Torvalds wake_up_poll(&tty->write_wait, EPOLLOUT); 177196fd7ce5SGreg Kroah-Hartman do_sleep++; 177296fd7ce5SGreg Kroah-Hartman } 177396fd7ce5SGreg Kroah-Hartman } 17747ffb6da9SPeter Hurley if (o_tty && o_tty->count <= 1) { 177596fd7ce5SGreg Kroah-Hartman if (waitqueue_active(&o_tty->read_wait)) { 1776a9a08845SLinus Torvalds wake_up_poll(&o_tty->read_wait, EPOLLIN); 177796fd7ce5SGreg Kroah-Hartman do_sleep++; 177896fd7ce5SGreg Kroah-Hartman } 177996fd7ce5SGreg Kroah-Hartman if (waitqueue_active(&o_tty->write_wait)) { 1780a9a08845SLinus Torvalds wake_up_poll(&o_tty->write_wait, EPOLLOUT); 178196fd7ce5SGreg Kroah-Hartman do_sleep++; 178296fd7ce5SGreg Kroah-Hartman } 178396fd7ce5SGreg Kroah-Hartman } 178496fd7ce5SGreg Kroah-Hartman if (!do_sleep) 178596fd7ce5SGreg Kroah-Hartman break; 178696fd7ce5SGreg Kroah-Hartman 1787494c1eacSPeter Hurley if (once) { 1788494c1eacSPeter Hurley once = 0; 1789339f36baSPeter Hurley tty_warn(tty, "read/write wait queue active!\n"); 1790494c1eacSPeter Hurley } 179137b16457SPeter Hurley schedule_timeout_killable(timeout); 179237b16457SPeter Hurley if (timeout < 120 * HZ) 179337b16457SPeter Hurley timeout = 2 * timeout + 1; 179437b16457SPeter Hurley else 179537b16457SPeter Hurley timeout = MAX_SCHEDULE_TIMEOUT; 179696fd7ce5SGreg Kroah-Hartman } 179796fd7ce5SGreg Kroah-Hartman 17987ffb6da9SPeter Hurley if (o_tty) { 179996fd7ce5SGreg Kroah-Hartman if (--o_tty->count < 0) { 1800339f36baSPeter Hurley tty_warn(tty, "bad slave count (%d)\n", o_tty->count); 180196fd7ce5SGreg Kroah-Hartman o_tty->count = 0; 180296fd7ce5SGreg Kroah-Hartman } 180396fd7ce5SGreg Kroah-Hartman } 180496fd7ce5SGreg Kroah-Hartman if (--tty->count < 0) { 1805339f36baSPeter Hurley tty_warn(tty, "bad tty->count (%d)\n", tty->count); 180696fd7ce5SGreg Kroah-Hartman tty->count = 0; 180796fd7ce5SGreg Kroah-Hartman } 180896fd7ce5SGreg Kroah-Hartman 180996fd7ce5SGreg Kroah-Hartman /* 181096fd7ce5SGreg Kroah-Hartman * We've decremented tty->count, so we need to remove this file 181196fd7ce5SGreg Kroah-Hartman * descriptor off the tty->tty_files list; this serves two 181296fd7ce5SGreg Kroah-Hartman * purposes: 181396fd7ce5SGreg Kroah-Hartman * - check_tty_count sees the correct number of file descriptors 181496fd7ce5SGreg Kroah-Hartman * associated with this tty. 181596fd7ce5SGreg Kroah-Hartman * - do_tty_hangup no longer sees this file descriptor as 181696fd7ce5SGreg Kroah-Hartman * something that needs to be handled for hangups. 181796fd7ce5SGreg Kroah-Hartman */ 181896fd7ce5SGreg Kroah-Hartman tty_del_file(filp); 181996fd7ce5SGreg Kroah-Hartman 182096fd7ce5SGreg Kroah-Hartman /* 182196fd7ce5SGreg Kroah-Hartman * Perform some housekeeping before deciding whether to return. 182296fd7ce5SGreg Kroah-Hartman * 182396fd7ce5SGreg Kroah-Hartman * If _either_ side is closing, make sure there aren't any 182496fd7ce5SGreg Kroah-Hartman * processes that still think tty or o_tty is their controlling 182596fd7ce5SGreg Kroah-Hartman * tty. 182696fd7ce5SGreg Kroah-Hartman */ 1827324c1650SPeter Hurley if (!tty->count) { 182896fd7ce5SGreg Kroah-Hartman read_lock(&tasklist_lock); 182996fd7ce5SGreg Kroah-Hartman session_clear_tty(tty->session); 18307ffb6da9SPeter Hurley if (o_tty) 183196fd7ce5SGreg Kroah-Hartman session_clear_tty(o_tty->session); 183296fd7ce5SGreg Kroah-Hartman read_unlock(&tasklist_lock); 183396fd7ce5SGreg Kroah-Hartman } 183496fd7ce5SGreg Kroah-Hartman 1835324c1650SPeter Hurley /* check whether both sides are closing ... */ 18367ffb6da9SPeter Hurley final = !tty->count && !(o_tty && o_tty->count); 1837324c1650SPeter Hurley 18382aff5e2bSPeter Hurley tty_unlock_slave(o_tty); 18392aff5e2bSPeter Hurley tty_unlock(tty); 18402aff5e2bSPeter Hurley 184104980706SPeter Hurley /* At this point, the tty->count == 0 should ensure a dead tty 1842d155255aSAlan Cox cannot be re-opened by a racing opener */ 184396fd7ce5SGreg Kroah-Hartman 1844324c1650SPeter Hurley if (!final) 184596fd7ce5SGreg Kroah-Hartman return 0; 184696fd7ce5SGreg Kroah-Hartman 1847accff793SPeter Hurley tty_debug_hangup(tty, "final close\n"); 1848a2965b7bSPeter Hurley 18499ed90d20SRob Herring tty_release_struct(tty, idx); 185096fd7ce5SGreg Kroah-Hartman return 0; 185196fd7ce5SGreg Kroah-Hartman } 185296fd7ce5SGreg Kroah-Hartman 185396fd7ce5SGreg Kroah-Hartman /** 185452494eebSPeter Hurley * tty_open_current_tty - get locked tty of current task 1855b82154acSJiri Slaby * @device: device number 1856b82154acSJiri Slaby * @filp: file pointer to tty 185752494eebSPeter Hurley * @return: locked tty of the current task iff @device is /dev/tty 185852494eebSPeter Hurley * 185952494eebSPeter Hurley * Performs a re-open of the current task's controlling tty. 1860b82154acSJiri Slaby * 1861b82154acSJiri Slaby * We cannot return driver and index like for the other nodes because 1862b82154acSJiri Slaby * devpts will not work then. It expects inodes to be from devpts FS. 1863b82154acSJiri Slaby */ 1864b82154acSJiri Slaby static struct tty_struct *tty_open_current_tty(dev_t device, struct file *filp) 1865b82154acSJiri Slaby { 1866b82154acSJiri Slaby struct tty_struct *tty; 186752494eebSPeter Hurley int retval; 1868b82154acSJiri Slaby 1869b82154acSJiri Slaby if (device != MKDEV(TTYAUX_MAJOR, 0)) 1870b82154acSJiri Slaby return NULL; 1871b82154acSJiri Slaby 1872b82154acSJiri Slaby tty = get_current_tty(); 1873b82154acSJiri Slaby if (!tty) 1874b82154acSJiri Slaby return ERR_PTR(-ENXIO); 1875b82154acSJiri Slaby 1876b82154acSJiri Slaby filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */ 1877b82154acSJiri Slaby /* noctty = 1; */ 187852494eebSPeter Hurley tty_lock(tty); 187952494eebSPeter Hurley tty_kref_put(tty); /* safe to drop the kref now */ 188052494eebSPeter Hurley 188152494eebSPeter Hurley retval = tty_reopen(tty); 188252494eebSPeter Hurley if (retval < 0) { 188352494eebSPeter Hurley tty_unlock(tty); 188452494eebSPeter Hurley tty = ERR_PTR(retval); 188552494eebSPeter Hurley } 1886b82154acSJiri Slaby return tty; 1887b82154acSJiri Slaby } 1888b82154acSJiri Slaby 1889b82154acSJiri Slaby /** 18905b5e7040SJiri Slaby * tty_lookup_driver - lookup a tty driver for a given device file 18915b5e7040SJiri Slaby * @device: device number 18925b5e7040SJiri Slaby * @filp: file pointer to tty 18935b5e7040SJiri Slaby * @index: index for the device in the @return driver 18945b5e7040SJiri Slaby * @return: driver for this inode (with increased refcount) 18955b5e7040SJiri Slaby * 18965b5e7040SJiri Slaby * If @return is not erroneous, the caller is responsible to decrement the 18975b5e7040SJiri Slaby * refcount by tty_driver_kref_put. 18985b5e7040SJiri Slaby * 18995b5e7040SJiri Slaby * Locking: tty_mutex protects get_tty_driver 19005b5e7040SJiri Slaby */ 19015b5e7040SJiri Slaby static struct tty_driver *tty_lookup_driver(dev_t device, struct file *filp, 190211e1d4aaSPeter Hurley int *index) 19035b5e7040SJiri Slaby { 19048c8af41dSLin Yi struct tty_driver *driver = NULL; 19055b5e7040SJiri Slaby 19062cd0050cSJiri Slaby switch (device) { 19075b5e7040SJiri Slaby #ifdef CONFIG_VT 19082cd0050cSJiri Slaby case MKDEV(TTY_MAJOR, 0): { 19095b5e7040SJiri Slaby extern struct tty_driver *console_driver; 19105b5e7040SJiri Slaby driver = tty_driver_kref_get(console_driver); 19115b5e7040SJiri Slaby *index = fg_console; 19122cd0050cSJiri Slaby break; 19135b5e7040SJiri Slaby } 19145b5e7040SJiri Slaby #endif 19152cd0050cSJiri Slaby case MKDEV(TTYAUX_MAJOR, 1): { 19165b5e7040SJiri Slaby struct tty_driver *console_driver = console_device(index); 19175b5e7040SJiri Slaby if (console_driver) { 19185b5e7040SJiri Slaby driver = tty_driver_kref_get(console_driver); 191912e84c71SOkash Khawaja if (driver && filp) { 19205b5e7040SJiri Slaby /* Don't let /dev/console block */ 19215b5e7040SJiri Slaby filp->f_flags |= O_NONBLOCK; 19222cd0050cSJiri Slaby break; 19235b5e7040SJiri Slaby } 19245b5e7040SJiri Slaby } 19258c8af41dSLin Yi if (driver) 19268c8af41dSLin Yi tty_driver_kref_put(driver); 19275b5e7040SJiri Slaby return ERR_PTR(-ENODEV); 19285b5e7040SJiri Slaby } 19292cd0050cSJiri Slaby default: 19305b5e7040SJiri Slaby driver = get_tty_driver(device, index); 19315b5e7040SJiri Slaby if (!driver) 19325b5e7040SJiri Slaby return ERR_PTR(-ENODEV); 19332cd0050cSJiri Slaby break; 19342cd0050cSJiri Slaby } 19355b5e7040SJiri Slaby return driver; 19365b5e7040SJiri Slaby } 19375b5e7040SJiri Slaby 19385b5e7040SJiri Slaby /** 1939a09ac397SOkash Khawaja * tty_kopen - open a tty device for kernel 1940a09ac397SOkash Khawaja * @device: dev_t of device to open 1941a09ac397SOkash Khawaja * 1942a09ac397SOkash Khawaja * Opens tty exclusively for kernel. Performs the driver lookup, 1943a09ac397SOkash Khawaja * makes sure it's not already opened and performs the first-time 1944a09ac397SOkash Khawaja * tty initialization. 1945a09ac397SOkash Khawaja * 1946a09ac397SOkash Khawaja * Returns the locked initialized &tty_struct 1947a09ac397SOkash Khawaja * 1948a09ac397SOkash Khawaja * Claims the global tty_mutex to serialize: 1949a09ac397SOkash Khawaja * - concurrent first-time tty initialization 1950a09ac397SOkash Khawaja * - concurrent tty driver removal w/ lookup 1951a09ac397SOkash Khawaja * - concurrent tty removal from driver table 1952a09ac397SOkash Khawaja */ 1953a09ac397SOkash Khawaja struct tty_struct *tty_kopen(dev_t device) 1954a09ac397SOkash Khawaja { 1955a09ac397SOkash Khawaja struct tty_struct *tty; 1956f1d31743SUwe Kleine-König struct tty_driver *driver; 1957a09ac397SOkash Khawaja int index = -1; 1958a09ac397SOkash Khawaja 1959a09ac397SOkash Khawaja mutex_lock(&tty_mutex); 1960a09ac397SOkash Khawaja driver = tty_lookup_driver(device, NULL, &index); 1961a09ac397SOkash Khawaja if (IS_ERR(driver)) { 1962a09ac397SOkash Khawaja mutex_unlock(&tty_mutex); 1963a09ac397SOkash Khawaja return ERR_CAST(driver); 1964a09ac397SOkash Khawaja } 1965a09ac397SOkash Khawaja 1966a09ac397SOkash Khawaja /* check whether we're reopening an existing tty */ 1967a09ac397SOkash Khawaja tty = tty_driver_lookup_tty(driver, NULL, index); 1968a09ac397SOkash Khawaja if (IS_ERR(tty)) 1969a09ac397SOkash Khawaja goto out; 1970a09ac397SOkash Khawaja 1971a09ac397SOkash Khawaja if (tty) { 1972a09ac397SOkash Khawaja /* drop kref from tty_driver_lookup_tty() */ 1973a09ac397SOkash Khawaja tty_kref_put(tty); 1974a09ac397SOkash Khawaja tty = ERR_PTR(-EBUSY); 1975a09ac397SOkash Khawaja } else { /* tty_init_dev returns tty with the tty_lock held */ 1976a09ac397SOkash Khawaja tty = tty_init_dev(driver, index); 1977a09ac397SOkash Khawaja if (IS_ERR(tty)) 1978a09ac397SOkash Khawaja goto out; 1979a09ac397SOkash Khawaja tty_port_set_kopened(tty->port, 1); 1980a09ac397SOkash Khawaja } 1981a09ac397SOkash Khawaja out: 1982a09ac397SOkash Khawaja mutex_unlock(&tty_mutex); 1983a09ac397SOkash Khawaja tty_driver_kref_put(driver); 1984a09ac397SOkash Khawaja return tty; 1985a09ac397SOkash Khawaja } 1986a09ac397SOkash Khawaja EXPORT_SYMBOL_GPL(tty_kopen); 1987a09ac397SOkash Khawaja 1988a09ac397SOkash Khawaja /** 1989d6203d0cSPeter Hurley * tty_open_by_driver - open a tty device 1990d6203d0cSPeter Hurley * @device: dev_t of device to open 1991d6203d0cSPeter Hurley * @filp: file pointer to tty 1992d6203d0cSPeter Hurley * 1993d6203d0cSPeter Hurley * Performs the driver lookup, checks for a reopen, or otherwise 1994d6203d0cSPeter Hurley * performs the first-time tty initialization. 1995d6203d0cSPeter Hurley * 1996d6203d0cSPeter Hurley * Returns the locked initialized or re-opened &tty_struct 1997d6203d0cSPeter Hurley * 1998d6203d0cSPeter Hurley * Claims the global tty_mutex to serialize: 1999d6203d0cSPeter Hurley * - concurrent first-time tty initialization 2000d6203d0cSPeter Hurley * - concurrent tty driver removal w/ lookup 2001d6203d0cSPeter Hurley * - concurrent tty removal from driver table 2002d6203d0cSPeter Hurley */ 200314ce3848SSudip Mukherjee static struct tty_struct *tty_open_by_driver(dev_t device, 2004d6203d0cSPeter Hurley struct file *filp) 2005d6203d0cSPeter Hurley { 2006d6203d0cSPeter Hurley struct tty_struct *tty; 2007d6203d0cSPeter Hurley struct tty_driver *driver = NULL; 2008d6203d0cSPeter Hurley int index = -1; 2009d6203d0cSPeter Hurley int retval; 2010d6203d0cSPeter Hurley 2011d6203d0cSPeter Hurley mutex_lock(&tty_mutex); 2012d6203d0cSPeter Hurley driver = tty_lookup_driver(device, filp, &index); 2013d6203d0cSPeter Hurley if (IS_ERR(driver)) { 2014d6203d0cSPeter Hurley mutex_unlock(&tty_mutex); 2015d6203d0cSPeter Hurley return ERR_CAST(driver); 2016d6203d0cSPeter Hurley } 2017d6203d0cSPeter Hurley 2018d6203d0cSPeter Hurley /* check whether we're reopening an existing tty */ 20198ead9dd5SLinus Torvalds tty = tty_driver_lookup_tty(driver, filp, index); 2020d6203d0cSPeter Hurley if (IS_ERR(tty)) { 2021d6203d0cSPeter Hurley mutex_unlock(&tty_mutex); 2022d6203d0cSPeter Hurley goto out; 2023d6203d0cSPeter Hurley } 2024d6203d0cSPeter Hurley 2025d6203d0cSPeter Hurley if (tty) { 2026a09ac397SOkash Khawaja if (tty_port_kopened(tty->port)) { 2027a09ac397SOkash Khawaja tty_kref_put(tty); 2028a09ac397SOkash Khawaja mutex_unlock(&tty_mutex); 2029a09ac397SOkash Khawaja tty = ERR_PTR(-EBUSY); 2030a09ac397SOkash Khawaja goto out; 2031a09ac397SOkash Khawaja } 2032d6203d0cSPeter Hurley mutex_unlock(&tty_mutex); 2033d6203d0cSPeter Hurley retval = tty_lock_interruptible(tty); 20345e00bbfbSPeter Hurley tty_kref_put(tty); /* drop kref from tty_driver_lookup_tty() */ 2035d6203d0cSPeter Hurley if (retval) { 2036d6203d0cSPeter Hurley if (retval == -EINTR) 2037d6203d0cSPeter Hurley retval = -ERESTARTSYS; 2038d6203d0cSPeter Hurley tty = ERR_PTR(retval); 2039d6203d0cSPeter Hurley goto out; 2040d6203d0cSPeter Hurley } 2041d6203d0cSPeter Hurley retval = tty_reopen(tty); 2042d6203d0cSPeter Hurley if (retval < 0) { 2043d6203d0cSPeter Hurley tty_unlock(tty); 2044d6203d0cSPeter Hurley tty = ERR_PTR(retval); 2045d6203d0cSPeter Hurley } 2046d6203d0cSPeter Hurley } else { /* Returns with the tty_lock held for now */ 2047d6203d0cSPeter Hurley tty = tty_init_dev(driver, index); 2048d6203d0cSPeter Hurley mutex_unlock(&tty_mutex); 2049d6203d0cSPeter Hurley } 2050d6203d0cSPeter Hurley out: 2051d6203d0cSPeter Hurley tty_driver_kref_put(driver); 2052d6203d0cSPeter Hurley return tty; 2053d6203d0cSPeter Hurley } 2054d6203d0cSPeter Hurley 2055d6203d0cSPeter Hurley /** 205696fd7ce5SGreg Kroah-Hartman * tty_open - open a tty device 205796fd7ce5SGreg Kroah-Hartman * @inode: inode of device file 205896fd7ce5SGreg Kroah-Hartman * @filp: file pointer to tty 205996fd7ce5SGreg Kroah-Hartman * 206096fd7ce5SGreg Kroah-Hartman * tty_open and tty_release keep up the tty count that contains the 206196fd7ce5SGreg Kroah-Hartman * number of opens done on a tty. We cannot use the inode-count, as 206296fd7ce5SGreg Kroah-Hartman * different inodes might point to the same tty. 206396fd7ce5SGreg Kroah-Hartman * 206496fd7ce5SGreg Kroah-Hartman * Open-counting is needed for pty masters, as well as for keeping 206596fd7ce5SGreg Kroah-Hartman * track of serial lines: DTR is dropped when the last close happens. 206696fd7ce5SGreg Kroah-Hartman * (This is not done solely through tty->count, now. - Ted 1/27/92) 206796fd7ce5SGreg Kroah-Hartman * 206896fd7ce5SGreg Kroah-Hartman * The termios state of a pty is reset on first open so that 206996fd7ce5SGreg Kroah-Hartman * settings don't persist across reuse. 207096fd7ce5SGreg Kroah-Hartman * 20715b5e7040SJiri Slaby * Locking: tty_mutex protects tty, tty_lookup_driver and tty_init_dev. 207296fd7ce5SGreg Kroah-Hartman * tty->count should protect the rest. 207396fd7ce5SGreg Kroah-Hartman * ->siglock protects ->signal/->sighand 207489c8d91eSAlan Cox * 207589c8d91eSAlan Cox * Note: the tty_unlock/lock cases without a ref are only safe due to 207689c8d91eSAlan Cox * tty_mutex 207796fd7ce5SGreg Kroah-Hartman */ 207896fd7ce5SGreg Kroah-Hartman 207996fd7ce5SGreg Kroah-Hartman static int tty_open(struct inode *inode, struct file *filp) 208096fd7ce5SGreg Kroah-Hartman { 2081b82154acSJiri Slaby struct tty_struct *tty; 208296fd7ce5SGreg Kroah-Hartman int noctty, retval; 208396fd7ce5SGreg Kroah-Hartman dev_t device = inode->i_rdev; 208496fd7ce5SGreg Kroah-Hartman unsigned saved_flags = filp->f_flags; 208596fd7ce5SGreg Kroah-Hartman 208696fd7ce5SGreg Kroah-Hartman nonseekable_open(inode, filp); 208796fd7ce5SGreg Kroah-Hartman 208896fd7ce5SGreg Kroah-Hartman retry_open: 2089fa90e1c9SJiri Slaby retval = tty_alloc_file(filp); 2090fa90e1c9SJiri Slaby if (retval) 2091fa90e1c9SJiri Slaby return -ENOMEM; 2092fa90e1c9SJiri Slaby 2093b82154acSJiri Slaby tty = tty_open_current_tty(device, filp); 2094d6203d0cSPeter Hurley if (!tty) 209514ce3848SSudip Mukherjee tty = tty_open_by_driver(device, filp); 209652494eebSPeter Hurley 209796fd7ce5SGreg Kroah-Hartman if (IS_ERR(tty)) { 2098d6203d0cSPeter Hurley tty_free_file(filp); 2099ba5db448SJiri Slaby retval = PTR_ERR(tty); 21007f22f6c9SPeter Hurley if (retval != -EAGAIN || signal_pending(current)) 2101d6203d0cSPeter Hurley return retval; 21027f22f6c9SPeter Hurley schedule(); 21037f22f6c9SPeter Hurley goto retry_open; 210496fd7ce5SGreg Kroah-Hartman } 210596fd7ce5SGreg Kroah-Hartman 2106fa90e1c9SJiri Slaby tty_add_file(tty, filp); 210796fd7ce5SGreg Kroah-Hartman 21089de44bd6SJiri Slaby check_tty_count(tty, __func__); 2109d435cefeSPeter Hurley tty_debug_hangup(tty, "opening (count=%d)\n", tty->count); 2110accff793SPeter Hurley 211196fd7ce5SGreg Kroah-Hartman if (tty->ops->open) 211296fd7ce5SGreg Kroah-Hartman retval = tty->ops->open(tty, filp); 211396fd7ce5SGreg Kroah-Hartman else 211496fd7ce5SGreg Kroah-Hartman retval = -ENODEV; 211596fd7ce5SGreg Kroah-Hartman filp->f_flags = saved_flags; 211696fd7ce5SGreg Kroah-Hartman 211796fd7ce5SGreg Kroah-Hartman if (retval) { 2118d435cefeSPeter Hurley tty_debug_hangup(tty, "open error %d, releasing\n", retval); 2119accff793SPeter Hurley 212089c8d91eSAlan Cox tty_unlock(tty); /* need to call tty_release without BTM */ 212196fd7ce5SGreg Kroah-Hartman tty_release(inode, filp); 212296fd7ce5SGreg Kroah-Hartman if (retval != -ERESTARTSYS) 212396fd7ce5SGreg Kroah-Hartman return retval; 212496fd7ce5SGreg Kroah-Hartman 212596fd7ce5SGreg Kroah-Hartman if (signal_pending(current)) 212696fd7ce5SGreg Kroah-Hartman return retval; 212796fd7ce5SGreg Kroah-Hartman 212896fd7ce5SGreg Kroah-Hartman schedule(); 212996fd7ce5SGreg Kroah-Hartman /* 213096fd7ce5SGreg Kroah-Hartman * Need to reset f_op in case a hangup happened. 213196fd7ce5SGreg Kroah-Hartman */ 21321256937fSPeter Hurley if (tty_hung_up_p(filp)) 213396fd7ce5SGreg Kroah-Hartman filp->f_op = &tty_fops; 213496fd7ce5SGreg Kroah-Hartman goto retry_open; 213596fd7ce5SGreg Kroah-Hartman } 2136d4855e1fSPeter Hurley clear_bit(TTY_HUPPED, &tty->flags); 213796fd7ce5SGreg Kroah-Hartman 213811e1d4aaSPeter Hurley noctty = (filp->f_flags & O_NOCTTY) || 2139da5a0fc6SPeter Hurley (IS_ENABLED(CONFIG_VT) && device == MKDEV(TTY_MAJOR, 0)) || 214011e1d4aaSPeter Hurley device == MKDEV(TTYAUX_MAJOR, 1) || 214111e1d4aaSPeter Hurley (tty->driver->type == TTY_DRIVER_TYPE_PTY && 214211e1d4aaSPeter Hurley tty->driver->subtype == PTY_TYPE_MASTER); 2143a1235b3eSNicolas Pitre if (!noctty) 2144a1235b3eSNicolas Pitre tty_open_proc_set_tty(filp, tty); 214589c8d91eSAlan Cox tty_unlock(tty); 214696fd7ce5SGreg Kroah-Hartman return 0; 214796fd7ce5SGreg Kroah-Hartman } 214896fd7ce5SGreg Kroah-Hartman 214996fd7ce5SGreg Kroah-Hartman 215096fd7ce5SGreg Kroah-Hartman 215196fd7ce5SGreg Kroah-Hartman /** 215296fd7ce5SGreg Kroah-Hartman * tty_poll - check tty status 215396fd7ce5SGreg Kroah-Hartman * @filp: file being polled 215496fd7ce5SGreg Kroah-Hartman * @wait: poll wait structures to update 215596fd7ce5SGreg Kroah-Hartman * 215696fd7ce5SGreg Kroah-Hartman * Call the line discipline polling method to obtain the poll 215796fd7ce5SGreg Kroah-Hartman * status of the device. 215896fd7ce5SGreg Kroah-Hartman * 215996fd7ce5SGreg Kroah-Hartman * Locking: locks called line discipline but ldisc poll method 216096fd7ce5SGreg Kroah-Hartman * may be re-entered freely by other callers. 216196fd7ce5SGreg Kroah-Hartman */ 216296fd7ce5SGreg Kroah-Hartman 2163afc9a42bSAl Viro static __poll_t tty_poll(struct file *filp, poll_table *wait) 216496fd7ce5SGreg Kroah-Hartman { 216596fd7ce5SGreg Kroah-Hartman struct tty_struct *tty = file_tty(filp); 216696fd7ce5SGreg Kroah-Hartman struct tty_ldisc *ld; 2167e6c8adcaSAl Viro __poll_t ret = 0; 216896fd7ce5SGreg Kroah-Hartman 21696131ffaaSAl Viro if (tty_paranoia_check(tty, file_inode(filp), "tty_poll")) 217096fd7ce5SGreg Kroah-Hartman return 0; 217196fd7ce5SGreg Kroah-Hartman 217296fd7ce5SGreg Kroah-Hartman ld = tty_ldisc_ref_wait(tty); 2173e55afd11SPeter Hurley if (!ld) 2174e55afd11SPeter Hurley return hung_up_tty_poll(filp, wait); 217596fd7ce5SGreg Kroah-Hartman if (ld->ops->poll) 2176c961bfb1SPeter Hurley ret = ld->ops->poll(tty, filp, wait); 217796fd7ce5SGreg Kroah-Hartman tty_ldisc_deref(ld); 217896fd7ce5SGreg Kroah-Hartman return ret; 217996fd7ce5SGreg Kroah-Hartman } 218096fd7ce5SGreg Kroah-Hartman 218196fd7ce5SGreg Kroah-Hartman static int __tty_fasync(int fd, struct file *filp, int on) 218296fd7ce5SGreg Kroah-Hartman { 218396fd7ce5SGreg Kroah-Hartman struct tty_struct *tty = file_tty(filp); 218496fd7ce5SGreg Kroah-Hartman unsigned long flags; 218596fd7ce5SGreg Kroah-Hartman int retval = 0; 218696fd7ce5SGreg Kroah-Hartman 21876131ffaaSAl Viro if (tty_paranoia_check(tty, file_inode(filp), "tty_fasync")) 218896fd7ce5SGreg Kroah-Hartman goto out; 218996fd7ce5SGreg Kroah-Hartman 219096fd7ce5SGreg Kroah-Hartman retval = fasync_helper(fd, filp, on, &tty->fasync); 219196fd7ce5SGreg Kroah-Hartman if (retval <= 0) 219296fd7ce5SGreg Kroah-Hartman goto out; 219396fd7ce5SGreg Kroah-Hartman 219496fd7ce5SGreg Kroah-Hartman if (on) { 219596fd7ce5SGreg Kroah-Hartman enum pid_type type; 219696fd7ce5SGreg Kroah-Hartman struct pid *pid; 2197f6c8dbe6SPeter Hurley 219896fd7ce5SGreg Kroah-Hartman spin_lock_irqsave(&tty->ctrl_lock, flags); 219996fd7ce5SGreg Kroah-Hartman if (tty->pgrp) { 220096fd7ce5SGreg Kroah-Hartman pid = tty->pgrp; 220196fd7ce5SGreg Kroah-Hartman type = PIDTYPE_PGID; 220296fd7ce5SGreg Kroah-Hartman } else { 220396fd7ce5SGreg Kroah-Hartman pid = task_pid(current); 220401919134SEric W. Biederman type = PIDTYPE_TGID; 220596fd7ce5SGreg Kroah-Hartman } 220696fd7ce5SGreg Kroah-Hartman get_pid(pid); 220796fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&tty->ctrl_lock, flags); 2208e0b93eddSJeff Layton __f_setown(filp, pid, type, 0); 220996fd7ce5SGreg Kroah-Hartman put_pid(pid); 2210e0b93eddSJeff Layton retval = 0; 221196fd7ce5SGreg Kroah-Hartman } 221296fd7ce5SGreg Kroah-Hartman out: 221396fd7ce5SGreg Kroah-Hartman return retval; 221496fd7ce5SGreg Kroah-Hartman } 221596fd7ce5SGreg Kroah-Hartman 221696fd7ce5SGreg Kroah-Hartman static int tty_fasync(int fd, struct file *filp, int on) 221796fd7ce5SGreg Kroah-Hartman { 221889c8d91eSAlan Cox struct tty_struct *tty = file_tty(filp); 2219a8f3a297SPeter Hurley int retval = -ENOTTY; 222089c8d91eSAlan Cox 222189c8d91eSAlan Cox tty_lock(tty); 2222a8f3a297SPeter Hurley if (!tty_hung_up_p(filp)) 222396fd7ce5SGreg Kroah-Hartman retval = __tty_fasync(fd, filp, on); 222489c8d91eSAlan Cox tty_unlock(tty); 222589c8d91eSAlan Cox 222696fd7ce5SGreg Kroah-Hartman return retval; 222796fd7ce5SGreg Kroah-Hartman } 222896fd7ce5SGreg Kroah-Hartman 222996fd7ce5SGreg Kroah-Hartman /** 223096fd7ce5SGreg Kroah-Hartman * tiocsti - fake input character 223196fd7ce5SGreg Kroah-Hartman * @tty: tty to fake input into 223296fd7ce5SGreg Kroah-Hartman * @p: pointer to character 223396fd7ce5SGreg Kroah-Hartman * 223496fd7ce5SGreg Kroah-Hartman * Fake input to a tty device. Does the necessary locking and 223596fd7ce5SGreg Kroah-Hartman * input management. 223696fd7ce5SGreg Kroah-Hartman * 223796fd7ce5SGreg Kroah-Hartman * FIXME: does not honour flow control ?? 223896fd7ce5SGreg Kroah-Hartman * 223996fd7ce5SGreg Kroah-Hartman * Locking: 2240137084bbSPeter Hurley * Called functions take tty_ldiscs_lock 224196fd7ce5SGreg Kroah-Hartman * current->signal->tty check is safe without locks 224296fd7ce5SGreg Kroah-Hartman * 224396fd7ce5SGreg Kroah-Hartman * FIXME: may race normal receive processing 224496fd7ce5SGreg Kroah-Hartman */ 224596fd7ce5SGreg Kroah-Hartman 224696fd7ce5SGreg Kroah-Hartman static int tiocsti(struct tty_struct *tty, char __user *p) 224796fd7ce5SGreg Kroah-Hartman { 224896fd7ce5SGreg Kroah-Hartman char ch, mbz = 0; 224996fd7ce5SGreg Kroah-Hartman struct tty_ldisc *ld; 225096fd7ce5SGreg Kroah-Hartman 225196fd7ce5SGreg Kroah-Hartman if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN)) 225296fd7ce5SGreg Kroah-Hartman return -EPERM; 225396fd7ce5SGreg Kroah-Hartman if (get_user(ch, p)) 225496fd7ce5SGreg Kroah-Hartman return -EFAULT; 225596fd7ce5SGreg Kroah-Hartman tty_audit_tiocsti(tty, ch); 225696fd7ce5SGreg Kroah-Hartman ld = tty_ldisc_ref_wait(tty); 2257e55afd11SPeter Hurley if (!ld) 2258e55afd11SPeter Hurley return -EIO; 225927cfb3a5SGreg Kroah-Hartman if (ld->ops->receive_buf) 226096fd7ce5SGreg Kroah-Hartman ld->ops->receive_buf(tty, &ch, &mbz, 1); 226196fd7ce5SGreg Kroah-Hartman tty_ldisc_deref(ld); 226296fd7ce5SGreg Kroah-Hartman return 0; 226396fd7ce5SGreg Kroah-Hartman } 226496fd7ce5SGreg Kroah-Hartman 226596fd7ce5SGreg Kroah-Hartman /** 226696fd7ce5SGreg Kroah-Hartman * tiocgwinsz - implement window query ioctl 2267fa441954SJiri Slaby * @tty: tty 226896fd7ce5SGreg Kroah-Hartman * @arg: user buffer for result 226996fd7ce5SGreg Kroah-Hartman * 227096fd7ce5SGreg Kroah-Hartman * Copies the kernel idea of the window size into the user buffer. 227196fd7ce5SGreg Kroah-Hartman * 2272dee4a0beSPeter Hurley * Locking: tty->winsize_mutex is taken to ensure the winsize data 227396fd7ce5SGreg Kroah-Hartman * is consistent. 227496fd7ce5SGreg Kroah-Hartman */ 227596fd7ce5SGreg Kroah-Hartman 227696fd7ce5SGreg Kroah-Hartman static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg) 227796fd7ce5SGreg Kroah-Hartman { 227896fd7ce5SGreg Kroah-Hartman int err; 227996fd7ce5SGreg Kroah-Hartman 2280dee4a0beSPeter Hurley mutex_lock(&tty->winsize_mutex); 228196fd7ce5SGreg Kroah-Hartman err = copy_to_user(arg, &tty->winsize, sizeof(*arg)); 2282dee4a0beSPeter Hurley mutex_unlock(&tty->winsize_mutex); 228396fd7ce5SGreg Kroah-Hartman 228496fd7ce5SGreg Kroah-Hartman return err ? -EFAULT: 0; 228596fd7ce5SGreg Kroah-Hartman } 228696fd7ce5SGreg Kroah-Hartman 228796fd7ce5SGreg Kroah-Hartman /** 228896fd7ce5SGreg Kroah-Hartman * tty_do_resize - resize event 228996fd7ce5SGreg Kroah-Hartman * @tty: tty being resized 2290fa441954SJiri Slaby * @ws: new dimensions 229196fd7ce5SGreg Kroah-Hartman * 229296fd7ce5SGreg Kroah-Hartman * Update the termios variables and send the necessary signals to 229396fd7ce5SGreg Kroah-Hartman * peform a terminal resize correctly 229496fd7ce5SGreg Kroah-Hartman */ 229596fd7ce5SGreg Kroah-Hartman 229696fd7ce5SGreg Kroah-Hartman int tty_do_resize(struct tty_struct *tty, struct winsize *ws) 229796fd7ce5SGreg Kroah-Hartman { 229896fd7ce5SGreg Kroah-Hartman struct pid *pgrp; 229996fd7ce5SGreg Kroah-Hartman 230096fd7ce5SGreg Kroah-Hartman /* Lock the tty */ 2301dee4a0beSPeter Hurley mutex_lock(&tty->winsize_mutex); 230296fd7ce5SGreg Kroah-Hartman if (!memcmp(ws, &tty->winsize, sizeof(*ws))) 230396fd7ce5SGreg Kroah-Hartman goto done; 230496fd7ce5SGreg Kroah-Hartman 23055b239542SPeter Hurley /* Signal the foreground process group */ 23065b239542SPeter Hurley pgrp = tty_get_pgrp(tty); 230796fd7ce5SGreg Kroah-Hartman if (pgrp) 230896fd7ce5SGreg Kroah-Hartman kill_pgrp(pgrp, SIGWINCH, 1); 230996fd7ce5SGreg Kroah-Hartman put_pid(pgrp); 231096fd7ce5SGreg Kroah-Hartman 231196fd7ce5SGreg Kroah-Hartman tty->winsize = *ws; 231296fd7ce5SGreg Kroah-Hartman done: 2313dee4a0beSPeter Hurley mutex_unlock(&tty->winsize_mutex); 231496fd7ce5SGreg Kroah-Hartman return 0; 231596fd7ce5SGreg Kroah-Hartman } 23164d334fd1SMartin Schwidefsky EXPORT_SYMBOL(tty_do_resize); 231796fd7ce5SGreg Kroah-Hartman 231896fd7ce5SGreg Kroah-Hartman /** 231996fd7ce5SGreg Kroah-Hartman * tiocswinsz - implement window size set ioctl 2320fa441954SJiri Slaby * @tty: tty side of tty 232196fd7ce5SGreg Kroah-Hartman * @arg: user buffer for result 232296fd7ce5SGreg Kroah-Hartman * 232396fd7ce5SGreg Kroah-Hartman * Copies the user idea of the window size to the kernel. Traditionally 232496fd7ce5SGreg Kroah-Hartman * this is just advisory information but for the Linux console it 232596fd7ce5SGreg Kroah-Hartman * actually has driver level meaning and triggers a VC resize. 232696fd7ce5SGreg Kroah-Hartman * 232796fd7ce5SGreg Kroah-Hartman * Locking: 232825985edcSLucas De Marchi * Driver dependent. The default do_resize method takes the 232996fd7ce5SGreg Kroah-Hartman * tty termios mutex and ctrl_lock. The console takes its own lock 233096fd7ce5SGreg Kroah-Hartman * then calls into the default method. 233196fd7ce5SGreg Kroah-Hartman */ 233296fd7ce5SGreg Kroah-Hartman 233396fd7ce5SGreg Kroah-Hartman static int tiocswinsz(struct tty_struct *tty, struct winsize __user *arg) 233496fd7ce5SGreg Kroah-Hartman { 233596fd7ce5SGreg Kroah-Hartman struct winsize tmp_ws; 233696fd7ce5SGreg Kroah-Hartman if (copy_from_user(&tmp_ws, arg, sizeof(*arg))) 233796fd7ce5SGreg Kroah-Hartman return -EFAULT; 233896fd7ce5SGreg Kroah-Hartman 233996fd7ce5SGreg Kroah-Hartman if (tty->ops->resize) 234096fd7ce5SGreg Kroah-Hartman return tty->ops->resize(tty, &tmp_ws); 234196fd7ce5SGreg Kroah-Hartman else 234296fd7ce5SGreg Kroah-Hartman return tty_do_resize(tty, &tmp_ws); 234396fd7ce5SGreg Kroah-Hartman } 234496fd7ce5SGreg Kroah-Hartman 234596fd7ce5SGreg Kroah-Hartman /** 234696fd7ce5SGreg Kroah-Hartman * tioccons - allow admin to move logical console 234796fd7ce5SGreg Kroah-Hartman * @file: the file to become console 234896fd7ce5SGreg Kroah-Hartman * 234925985edcSLucas De Marchi * Allow the administrator to move the redirected console device 235096fd7ce5SGreg Kroah-Hartman * 235196fd7ce5SGreg Kroah-Hartman * Locking: uses redirect_lock to guard the redirect information 235296fd7ce5SGreg Kroah-Hartman */ 235396fd7ce5SGreg Kroah-Hartman 235496fd7ce5SGreg Kroah-Hartman static int tioccons(struct file *file) 235596fd7ce5SGreg Kroah-Hartman { 235696fd7ce5SGreg Kroah-Hartman if (!capable(CAP_SYS_ADMIN)) 235796fd7ce5SGreg Kroah-Hartman return -EPERM; 2358*9bb48c82SLinus Torvalds if (file->f_op->write_iter == redirected_tty_write) { 235996fd7ce5SGreg Kroah-Hartman struct file *f; 236096fd7ce5SGreg Kroah-Hartman spin_lock(&redirect_lock); 236196fd7ce5SGreg Kroah-Hartman f = redirect; 236296fd7ce5SGreg Kroah-Hartman redirect = NULL; 236396fd7ce5SGreg Kroah-Hartman spin_unlock(&redirect_lock); 236496fd7ce5SGreg Kroah-Hartman if (f) 236596fd7ce5SGreg Kroah-Hartman fput(f); 236696fd7ce5SGreg Kroah-Hartman return 0; 236796fd7ce5SGreg Kroah-Hartman } 236896fd7ce5SGreg Kroah-Hartman spin_lock(&redirect_lock); 236996fd7ce5SGreg Kroah-Hartman if (redirect) { 237096fd7ce5SGreg Kroah-Hartman spin_unlock(&redirect_lock); 237196fd7ce5SGreg Kroah-Hartman return -EBUSY; 237296fd7ce5SGreg Kroah-Hartman } 2373cb0942b8SAl Viro redirect = get_file(file); 237496fd7ce5SGreg Kroah-Hartman spin_unlock(&redirect_lock); 237596fd7ce5SGreg Kroah-Hartman return 0; 237696fd7ce5SGreg Kroah-Hartman } 237796fd7ce5SGreg Kroah-Hartman 237896fd7ce5SGreg Kroah-Hartman /** 237996fd7ce5SGreg Kroah-Hartman * tiocsetd - set line discipline 238096fd7ce5SGreg Kroah-Hartman * @tty: tty device 238196fd7ce5SGreg Kroah-Hartman * @p: pointer to user data 238296fd7ce5SGreg Kroah-Hartman * 238396fd7ce5SGreg Kroah-Hartman * Set the line discipline according to user request. 238496fd7ce5SGreg Kroah-Hartman * 238596fd7ce5SGreg Kroah-Hartman * Locking: see tty_set_ldisc, this function is just a helper 238696fd7ce5SGreg Kroah-Hartman */ 238796fd7ce5SGreg Kroah-Hartman 238896fd7ce5SGreg Kroah-Hartman static int tiocsetd(struct tty_struct *tty, int __user *p) 238996fd7ce5SGreg Kroah-Hartman { 2390c12da96fSPeter Hurley int disc; 239196fd7ce5SGreg Kroah-Hartman int ret; 239296fd7ce5SGreg Kroah-Hartman 2393c12da96fSPeter Hurley if (get_user(disc, p)) 239496fd7ce5SGreg Kroah-Hartman return -EFAULT; 239596fd7ce5SGreg Kroah-Hartman 2396c12da96fSPeter Hurley ret = tty_set_ldisc(tty, disc); 239796fd7ce5SGreg Kroah-Hartman 239896fd7ce5SGreg Kroah-Hartman return ret; 239996fd7ce5SGreg Kroah-Hartman } 240096fd7ce5SGreg Kroah-Hartman 240196fd7ce5SGreg Kroah-Hartman /** 24025c17c861SPeter Hurley * tiocgetd - get line discipline 24035c17c861SPeter Hurley * @tty: tty device 24045c17c861SPeter Hurley * @p: pointer to user data 24055c17c861SPeter Hurley * 24065c17c861SPeter Hurley * Retrieves the line discipline id directly from the ldisc. 24075c17c861SPeter Hurley * 24085c17c861SPeter Hurley * Locking: waits for ldisc reference (in case the line discipline 24095c17c861SPeter Hurley * is changing or the tty is being hungup) 24105c17c861SPeter Hurley */ 24115c17c861SPeter Hurley 24125c17c861SPeter Hurley static int tiocgetd(struct tty_struct *tty, int __user *p) 24135c17c861SPeter Hurley { 24145c17c861SPeter Hurley struct tty_ldisc *ld; 24155c17c861SPeter Hurley int ret; 24165c17c861SPeter Hurley 24175c17c861SPeter Hurley ld = tty_ldisc_ref_wait(tty); 2418e55afd11SPeter Hurley if (!ld) 2419e55afd11SPeter Hurley return -EIO; 24205c17c861SPeter Hurley ret = put_user(ld->ops->num, p); 24215c17c861SPeter Hurley tty_ldisc_deref(ld); 24225c17c861SPeter Hurley return ret; 24235c17c861SPeter Hurley } 24245c17c861SPeter Hurley 24255c17c861SPeter Hurley /** 242696fd7ce5SGreg Kroah-Hartman * send_break - performed time break 242796fd7ce5SGreg Kroah-Hartman * @tty: device to break on 242896fd7ce5SGreg Kroah-Hartman * @duration: timeout in mS 242996fd7ce5SGreg Kroah-Hartman * 243096fd7ce5SGreg Kroah-Hartman * Perform a timed break on hardware that lacks its own driver level 243196fd7ce5SGreg Kroah-Hartman * timed break functionality. 243296fd7ce5SGreg Kroah-Hartman * 243396fd7ce5SGreg Kroah-Hartman * Locking: 243496fd7ce5SGreg Kroah-Hartman * atomic_write_lock serializes 243596fd7ce5SGreg Kroah-Hartman * 243696fd7ce5SGreg Kroah-Hartman */ 243796fd7ce5SGreg Kroah-Hartman 243896fd7ce5SGreg Kroah-Hartman static int send_break(struct tty_struct *tty, unsigned int duration) 243996fd7ce5SGreg Kroah-Hartman { 244096fd7ce5SGreg Kroah-Hartman int retval; 244196fd7ce5SGreg Kroah-Hartman 244296fd7ce5SGreg Kroah-Hartman if (tty->ops->break_ctl == NULL) 244396fd7ce5SGreg Kroah-Hartman return 0; 244496fd7ce5SGreg Kroah-Hartman 244596fd7ce5SGreg Kroah-Hartman if (tty->driver->flags & TTY_DRIVER_HARDWARE_BREAK) 244696fd7ce5SGreg Kroah-Hartman retval = tty->ops->break_ctl(tty, duration); 244796fd7ce5SGreg Kroah-Hartman else { 244896fd7ce5SGreg Kroah-Hartman /* Do the work ourselves */ 244996fd7ce5SGreg Kroah-Hartman if (tty_write_lock(tty, 0) < 0) 245096fd7ce5SGreg Kroah-Hartman return -EINTR; 245196fd7ce5SGreg Kroah-Hartman retval = tty->ops->break_ctl(tty, -1); 245296fd7ce5SGreg Kroah-Hartman if (retval) 245396fd7ce5SGreg Kroah-Hartman goto out; 245496fd7ce5SGreg Kroah-Hartman if (!signal_pending(current)) 245596fd7ce5SGreg Kroah-Hartman msleep_interruptible(duration); 245696fd7ce5SGreg Kroah-Hartman retval = tty->ops->break_ctl(tty, 0); 245796fd7ce5SGreg Kroah-Hartman out: 245896fd7ce5SGreg Kroah-Hartman tty_write_unlock(tty); 245996fd7ce5SGreg Kroah-Hartman if (signal_pending(current)) 246096fd7ce5SGreg Kroah-Hartman retval = -EINTR; 246196fd7ce5SGreg Kroah-Hartman } 246296fd7ce5SGreg Kroah-Hartman return retval; 246396fd7ce5SGreg Kroah-Hartman } 246496fd7ce5SGreg Kroah-Hartman 246596fd7ce5SGreg Kroah-Hartman /** 246696fd7ce5SGreg Kroah-Hartman * tty_tiocmget - get modem status 246796fd7ce5SGreg Kroah-Hartman * @tty: tty device 246896fd7ce5SGreg Kroah-Hartman * @p: pointer to result 246996fd7ce5SGreg Kroah-Hartman * 247096fd7ce5SGreg Kroah-Hartman * Obtain the modem status bits from the tty driver if the feature 247196fd7ce5SGreg Kroah-Hartman * is supported. Return -EINVAL if it is not available. 247296fd7ce5SGreg Kroah-Hartman * 247396fd7ce5SGreg Kroah-Hartman * Locking: none (up to the driver) 247496fd7ce5SGreg Kroah-Hartman */ 247596fd7ce5SGreg Kroah-Hartman 247660b33c13SAlan Cox static int tty_tiocmget(struct tty_struct *tty, int __user *p) 247796fd7ce5SGreg Kroah-Hartman { 247896fd7ce5SGreg Kroah-Hartman int retval = -EINVAL; 247996fd7ce5SGreg Kroah-Hartman 248096fd7ce5SGreg Kroah-Hartman if (tty->ops->tiocmget) { 248160b33c13SAlan Cox retval = tty->ops->tiocmget(tty); 248296fd7ce5SGreg Kroah-Hartman 248396fd7ce5SGreg Kroah-Hartman if (retval >= 0) 248496fd7ce5SGreg Kroah-Hartman retval = put_user(retval, p); 248596fd7ce5SGreg Kroah-Hartman } 248696fd7ce5SGreg Kroah-Hartman return retval; 248796fd7ce5SGreg Kroah-Hartman } 248896fd7ce5SGreg Kroah-Hartman 248996fd7ce5SGreg Kroah-Hartman /** 249096fd7ce5SGreg Kroah-Hartman * tty_tiocmset - set modem status 249196fd7ce5SGreg Kroah-Hartman * @tty: tty device 249296fd7ce5SGreg Kroah-Hartman * @cmd: command - clear bits, set bits or set all 249396fd7ce5SGreg Kroah-Hartman * @p: pointer to desired bits 249496fd7ce5SGreg Kroah-Hartman * 249596fd7ce5SGreg Kroah-Hartman * Set the modem status bits from the tty driver if the feature 249696fd7ce5SGreg Kroah-Hartman * is supported. Return -EINVAL if it is not available. 249796fd7ce5SGreg Kroah-Hartman * 249896fd7ce5SGreg Kroah-Hartman * Locking: none (up to the driver) 249996fd7ce5SGreg Kroah-Hartman */ 250096fd7ce5SGreg Kroah-Hartman 250120b9d177SAlan Cox static int tty_tiocmset(struct tty_struct *tty, unsigned int cmd, 250296fd7ce5SGreg Kroah-Hartman unsigned __user *p) 250396fd7ce5SGreg Kroah-Hartman { 250496fd7ce5SGreg Kroah-Hartman int retval; 250596fd7ce5SGreg Kroah-Hartman unsigned int set, clear, val; 250696fd7ce5SGreg Kroah-Hartman 250796fd7ce5SGreg Kroah-Hartman if (tty->ops->tiocmset == NULL) 250896fd7ce5SGreg Kroah-Hartman return -EINVAL; 250996fd7ce5SGreg Kroah-Hartman 251096fd7ce5SGreg Kroah-Hartman retval = get_user(val, p); 251196fd7ce5SGreg Kroah-Hartman if (retval) 251296fd7ce5SGreg Kroah-Hartman return retval; 251396fd7ce5SGreg Kroah-Hartman set = clear = 0; 251496fd7ce5SGreg Kroah-Hartman switch (cmd) { 251596fd7ce5SGreg Kroah-Hartman case TIOCMBIS: 251696fd7ce5SGreg Kroah-Hartman set = val; 251796fd7ce5SGreg Kroah-Hartman break; 251896fd7ce5SGreg Kroah-Hartman case TIOCMBIC: 251996fd7ce5SGreg Kroah-Hartman clear = val; 252096fd7ce5SGreg Kroah-Hartman break; 252196fd7ce5SGreg Kroah-Hartman case TIOCMSET: 252296fd7ce5SGreg Kroah-Hartman set = val; 252396fd7ce5SGreg Kroah-Hartman clear = ~val; 252496fd7ce5SGreg Kroah-Hartman break; 252596fd7ce5SGreg Kroah-Hartman } 252696fd7ce5SGreg Kroah-Hartman set &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP; 252796fd7ce5SGreg Kroah-Hartman clear &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP; 252820b9d177SAlan Cox return tty->ops->tiocmset(tty, set, clear); 252996fd7ce5SGreg Kroah-Hartman } 253096fd7ce5SGreg Kroah-Hartman 253196fd7ce5SGreg Kroah-Hartman static int tty_tiocgicount(struct tty_struct *tty, void __user *arg) 253296fd7ce5SGreg Kroah-Hartman { 253396fd7ce5SGreg Kroah-Hartman int retval = -EINVAL; 253496fd7ce5SGreg Kroah-Hartman struct serial_icounter_struct icount; 253596fd7ce5SGreg Kroah-Hartman memset(&icount, 0, sizeof(icount)); 253696fd7ce5SGreg Kroah-Hartman if (tty->ops->get_icount) 253796fd7ce5SGreg Kroah-Hartman retval = tty->ops->get_icount(tty, &icount); 253896fd7ce5SGreg Kroah-Hartman if (retval != 0) 253996fd7ce5SGreg Kroah-Hartman return retval; 254096fd7ce5SGreg Kroah-Hartman if (copy_to_user(arg, &icount, sizeof(icount))) 254196fd7ce5SGreg Kroah-Hartman return -EFAULT; 254296fd7ce5SGreg Kroah-Hartman return 0; 254396fd7ce5SGreg Kroah-Hartman } 254496fd7ce5SGreg Kroah-Hartman 25452f46a2c1SAl Viro static int tty_tiocsserial(struct tty_struct *tty, struct serial_struct __user *ss) 25468a8ae62fSJiri Slaby { 25478a8ae62fSJiri Slaby static DEFINE_RATELIMIT_STATE(depr_flags, 25488a8ae62fSJiri Slaby DEFAULT_RATELIMIT_INTERVAL, 25498a8ae62fSJiri Slaby DEFAULT_RATELIMIT_BURST); 25508a8ae62fSJiri Slaby char comm[TASK_COMM_LEN]; 25512f46a2c1SAl Viro struct serial_struct v; 25528a8ae62fSJiri Slaby int flags; 25538a8ae62fSJiri Slaby 2554a3241891SGustavo A. R. Silva if (copy_from_user(&v, ss, sizeof(*ss))) 25552f46a2c1SAl Viro return -EFAULT; 25568a8ae62fSJiri Slaby 25572f46a2c1SAl Viro flags = v.flags & ASYNC_DEPRECATED; 25588a8ae62fSJiri Slaby 25598a8ae62fSJiri Slaby if (flags && __ratelimit(&depr_flags)) 2560d437fa91SJoe Perches pr_warn("%s: '%s' is using deprecated serial flags (with no effect): %.8x\n", 25618a8ae62fSJiri Slaby __func__, get_task_comm(comm, current), flags); 25622f46a2c1SAl Viro if (!tty->ops->set_serial) 2563930236a3SAl Viro return -ENOTTY; 25642f46a2c1SAl Viro return tty->ops->set_serial(tty, &v); 25652f46a2c1SAl Viro } 25662f46a2c1SAl Viro 25672f46a2c1SAl Viro static int tty_tiocgserial(struct tty_struct *tty, struct serial_struct __user *ss) 25682f46a2c1SAl Viro { 25692f46a2c1SAl Viro struct serial_struct v; 25702f46a2c1SAl Viro int err; 25712f46a2c1SAl Viro 2572a3241891SGustavo A. R. Silva memset(&v, 0, sizeof(v)); 25732f46a2c1SAl Viro if (!tty->ops->get_serial) 2574930236a3SAl Viro return -ENOTTY; 25752f46a2c1SAl Viro err = tty->ops->get_serial(tty, &v); 2576a3241891SGustavo A. R. Silva if (!err && copy_to_user(ss, &v, sizeof(v))) 25772f46a2c1SAl Viro err = -EFAULT; 25782f46a2c1SAl Viro return err; 25798a8ae62fSJiri Slaby } 25808a8ae62fSJiri Slaby 25818f166e00SPeter Hurley /* 25828f166e00SPeter Hurley * if pty, return the slave side (real_tty) 25838f166e00SPeter Hurley * otherwise, return self 25848f166e00SPeter Hurley */ 25858f166e00SPeter Hurley static struct tty_struct *tty_pair_get_tty(struct tty_struct *tty) 258696fd7ce5SGreg Kroah-Hartman { 258796fd7ce5SGreg Kroah-Hartman if (tty->driver->type == TTY_DRIVER_TYPE_PTY && 258896fd7ce5SGreg Kroah-Hartman tty->driver->subtype == PTY_TYPE_MASTER) 258996fd7ce5SGreg Kroah-Hartman tty = tty->link; 259096fd7ce5SGreg Kroah-Hartman return tty; 259196fd7ce5SGreg Kroah-Hartman } 259296fd7ce5SGreg Kroah-Hartman 259396fd7ce5SGreg Kroah-Hartman /* 259496fd7ce5SGreg Kroah-Hartman * Split this up, as gcc can choke on it otherwise.. 259596fd7ce5SGreg Kroah-Hartman */ 259696fd7ce5SGreg Kroah-Hartman long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 259796fd7ce5SGreg Kroah-Hartman { 259896fd7ce5SGreg Kroah-Hartman struct tty_struct *tty = file_tty(file); 259996fd7ce5SGreg Kroah-Hartman struct tty_struct *real_tty; 260096fd7ce5SGreg Kroah-Hartman void __user *p = (void __user *)arg; 260196fd7ce5SGreg Kroah-Hartman int retval; 260296fd7ce5SGreg Kroah-Hartman struct tty_ldisc *ld; 260396fd7ce5SGreg Kroah-Hartman 26046131ffaaSAl Viro if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl")) 260596fd7ce5SGreg Kroah-Hartman return -EINVAL; 260696fd7ce5SGreg Kroah-Hartman 260796fd7ce5SGreg Kroah-Hartman real_tty = tty_pair_get_tty(tty); 260896fd7ce5SGreg Kroah-Hartman 260996fd7ce5SGreg Kroah-Hartman /* 261096fd7ce5SGreg Kroah-Hartman * Factor out some common prep work 261196fd7ce5SGreg Kroah-Hartman */ 261296fd7ce5SGreg Kroah-Hartman switch (cmd) { 261396fd7ce5SGreg Kroah-Hartman case TIOCSETD: 261496fd7ce5SGreg Kroah-Hartman case TIOCSBRK: 261596fd7ce5SGreg Kroah-Hartman case TIOCCBRK: 261696fd7ce5SGreg Kroah-Hartman case TCSBRK: 261796fd7ce5SGreg Kroah-Hartman case TCSBRKP: 261896fd7ce5SGreg Kroah-Hartman retval = tty_check_change(tty); 261996fd7ce5SGreg Kroah-Hartman if (retval) 262096fd7ce5SGreg Kroah-Hartman return retval; 262196fd7ce5SGreg Kroah-Hartman if (cmd != TIOCCBRK) { 262296fd7ce5SGreg Kroah-Hartman tty_wait_until_sent(tty, 0); 262396fd7ce5SGreg Kroah-Hartman if (signal_pending(current)) 262496fd7ce5SGreg Kroah-Hartman return -EINTR; 262596fd7ce5SGreg Kroah-Hartman } 262696fd7ce5SGreg Kroah-Hartman break; 262796fd7ce5SGreg Kroah-Hartman } 262896fd7ce5SGreg Kroah-Hartman 262996fd7ce5SGreg Kroah-Hartman /* 263096fd7ce5SGreg Kroah-Hartman * Now do the stuff. 263196fd7ce5SGreg Kroah-Hartman */ 263296fd7ce5SGreg Kroah-Hartman switch (cmd) { 263396fd7ce5SGreg Kroah-Hartman case TIOCSTI: 263496fd7ce5SGreg Kroah-Hartman return tiocsti(tty, p); 263596fd7ce5SGreg Kroah-Hartman case TIOCGWINSZ: 263696fd7ce5SGreg Kroah-Hartman return tiocgwinsz(real_tty, p); 263796fd7ce5SGreg Kroah-Hartman case TIOCSWINSZ: 263896fd7ce5SGreg Kroah-Hartman return tiocswinsz(real_tty, p); 263996fd7ce5SGreg Kroah-Hartman case TIOCCONS: 264096fd7ce5SGreg Kroah-Hartman return real_tty != tty ? -EINVAL : tioccons(file); 264196fd7ce5SGreg Kroah-Hartman case TIOCEXCL: 264296fd7ce5SGreg Kroah-Hartman set_bit(TTY_EXCLUSIVE, &tty->flags); 264396fd7ce5SGreg Kroah-Hartman return 0; 264496fd7ce5SGreg Kroah-Hartman case TIOCNXCL: 264596fd7ce5SGreg Kroah-Hartman clear_bit(TTY_EXCLUSIVE, &tty->flags); 264696fd7ce5SGreg Kroah-Hartman return 0; 264784fd7bdfSCyrill Gorcunov case TIOCGEXCL: 264884fd7bdfSCyrill Gorcunov { 264984fd7bdfSCyrill Gorcunov int excl = test_bit(TTY_EXCLUSIVE, &tty->flags); 265084fd7bdfSCyrill Gorcunov return put_user(excl, (int __user *)p); 265184fd7bdfSCyrill Gorcunov } 265296fd7ce5SGreg Kroah-Hartman case TIOCGETD: 26535c17c861SPeter Hurley return tiocgetd(tty, p); 265496fd7ce5SGreg Kroah-Hartman case TIOCSETD: 265596fd7ce5SGreg Kroah-Hartman return tiocsetd(tty, p); 26563c95c985SKay Sievers case TIOCVHANGUP: 26573c95c985SKay Sievers if (!capable(CAP_SYS_ADMIN)) 26583c95c985SKay Sievers return -EPERM; 26593c95c985SKay Sievers tty_vhangup(tty); 26603c95c985SKay Sievers return 0; 2661b7b8de08SWerner Fink case TIOCGDEV: 2662b7b8de08SWerner Fink { 2663b7b8de08SWerner Fink unsigned int ret = new_encode_dev(tty_devnum(real_tty)); 2664b7b8de08SWerner Fink return put_user(ret, (unsigned int __user *)p); 2665b7b8de08SWerner Fink } 266696fd7ce5SGreg Kroah-Hartman /* 266796fd7ce5SGreg Kroah-Hartman * Break handling 266896fd7ce5SGreg Kroah-Hartman */ 266996fd7ce5SGreg Kroah-Hartman case TIOCSBRK: /* Turn break on, unconditionally */ 267096fd7ce5SGreg Kroah-Hartman if (tty->ops->break_ctl) 267196fd7ce5SGreg Kroah-Hartman return tty->ops->break_ctl(tty, -1); 267296fd7ce5SGreg Kroah-Hartman return 0; 267396fd7ce5SGreg Kroah-Hartman case TIOCCBRK: /* Turn break off, unconditionally */ 267496fd7ce5SGreg Kroah-Hartman if (tty->ops->break_ctl) 267596fd7ce5SGreg Kroah-Hartman return tty->ops->break_ctl(tty, 0); 267696fd7ce5SGreg Kroah-Hartman return 0; 267796fd7ce5SGreg Kroah-Hartman case TCSBRK: /* SVID version: non-zero arg --> no break */ 267896fd7ce5SGreg Kroah-Hartman /* non-zero arg means wait for all output data 267996fd7ce5SGreg Kroah-Hartman * to be sent (performed above) but don't send break. 268096fd7ce5SGreg Kroah-Hartman * This is used by the tcdrain() termios function. 268196fd7ce5SGreg Kroah-Hartman */ 268296fd7ce5SGreg Kroah-Hartman if (!arg) 268396fd7ce5SGreg Kroah-Hartman return send_break(tty, 250); 268496fd7ce5SGreg Kroah-Hartman return 0; 268596fd7ce5SGreg Kroah-Hartman case TCSBRKP: /* support for POSIX tcsendbreak() */ 268696fd7ce5SGreg Kroah-Hartman return send_break(tty, arg ? arg*100 : 250); 268796fd7ce5SGreg Kroah-Hartman 268896fd7ce5SGreg Kroah-Hartman case TIOCMGET: 268960b33c13SAlan Cox return tty_tiocmget(tty, p); 269096fd7ce5SGreg Kroah-Hartman case TIOCMSET: 269196fd7ce5SGreg Kroah-Hartman case TIOCMBIC: 269296fd7ce5SGreg Kroah-Hartman case TIOCMBIS: 269320b9d177SAlan Cox return tty_tiocmset(tty, cmd, p); 269496fd7ce5SGreg Kroah-Hartman case TIOCGICOUNT: 2695a3096199SAl Viro return tty_tiocgicount(tty, p); 269696fd7ce5SGreg Kroah-Hartman case TCFLSH: 269796fd7ce5SGreg Kroah-Hartman switch (arg) { 269896fd7ce5SGreg Kroah-Hartman case TCIFLUSH: 269996fd7ce5SGreg Kroah-Hartman case TCIOFLUSH: 270096fd7ce5SGreg Kroah-Hartman /* flush tty buffer and allow ldisc to process ioctl */ 270186c80a8eSPeter Hurley tty_buffer_flush(tty, NULL); 270296fd7ce5SGreg Kroah-Hartman break; 270396fd7ce5SGreg Kroah-Hartman } 270496fd7ce5SGreg Kroah-Hartman break; 27058a8ae62fSJiri Slaby case TIOCSSERIAL: 2706930236a3SAl Viro return tty_tiocsserial(tty, p); 27072f46a2c1SAl Viro case TIOCGSERIAL: 2708930236a3SAl Viro return tty_tiocgserial(tty, p); 2709311fc65cSEric W. Biederman case TIOCGPTPEER: 2710311fc65cSEric W. Biederman /* Special because the struct file is needed */ 2711311fc65cSEric W. Biederman return ptm_open_peer(file, tty, (int)arg); 2712a1235b3eSNicolas Pitre default: 2713a1235b3eSNicolas Pitre retval = tty_jobctrl_ioctl(tty, real_tty, file, cmd, arg); 2714a1235b3eSNicolas Pitre if (retval != -ENOIOCTLCMD) 2715a1235b3eSNicolas Pitre return retval; 271696fd7ce5SGreg Kroah-Hartman } 271796fd7ce5SGreg Kroah-Hartman if (tty->ops->ioctl) { 2718c961bfb1SPeter Hurley retval = tty->ops->ioctl(tty, cmd, arg); 271996fd7ce5SGreg Kroah-Hartman if (retval != -ENOIOCTLCMD) 272096fd7ce5SGreg Kroah-Hartman return retval; 272196fd7ce5SGreg Kroah-Hartman } 272296fd7ce5SGreg Kroah-Hartman ld = tty_ldisc_ref_wait(tty); 2723e55afd11SPeter Hurley if (!ld) 2724e55afd11SPeter Hurley return hung_up_tty_ioctl(file, cmd, arg); 272596fd7ce5SGreg Kroah-Hartman retval = -EINVAL; 272696fd7ce5SGreg Kroah-Hartman if (ld->ops->ioctl) { 272796fd7ce5SGreg Kroah-Hartman retval = ld->ops->ioctl(tty, file, cmd, arg); 272896fd7ce5SGreg Kroah-Hartman if (retval == -ENOIOCTLCMD) 2729bbb63c51SWanlong Gao retval = -ENOTTY; 273096fd7ce5SGreg Kroah-Hartman } 273196fd7ce5SGreg Kroah-Hartman tty_ldisc_deref(ld); 273296fd7ce5SGreg Kroah-Hartman return retval; 273396fd7ce5SGreg Kroah-Hartman } 273496fd7ce5SGreg Kroah-Hartman 273596fd7ce5SGreg Kroah-Hartman #ifdef CONFIG_COMPAT 273677654350SAl Viro 273777654350SAl Viro struct serial_struct32 { 273877654350SAl Viro compat_int_t type; 273977654350SAl Viro compat_int_t line; 274077654350SAl Viro compat_uint_t port; 274177654350SAl Viro compat_int_t irq; 274277654350SAl Viro compat_int_t flags; 274377654350SAl Viro compat_int_t xmit_fifo_size; 274477654350SAl Viro compat_int_t custom_divisor; 274577654350SAl Viro compat_int_t baud_base; 274677654350SAl Viro unsigned short close_delay; 274777654350SAl Viro char io_type; 2748e5b9f4b1SGustavo A. R. Silva char reserved_char; 274977654350SAl Viro compat_int_t hub6; 275077654350SAl Viro unsigned short closing_wait; /* time to wait before closing */ 275177654350SAl Viro unsigned short closing_wait2; /* no longer used... */ 275277654350SAl Viro compat_uint_t iomem_base; 275377654350SAl Viro unsigned short iomem_reg_shift; 275477654350SAl Viro unsigned int port_high; 275577654350SAl Viro /* compat_ulong_t iomap_base FIXME */ 2756e5b9f4b1SGustavo A. R. Silva compat_int_t reserved; 275777654350SAl Viro }; 275877654350SAl Viro 275977654350SAl Viro static int compat_tty_tiocsserial(struct tty_struct *tty, 276077654350SAl Viro struct serial_struct32 __user *ss) 276177654350SAl Viro { 276277654350SAl Viro static DEFINE_RATELIMIT_STATE(depr_flags, 276377654350SAl Viro DEFAULT_RATELIMIT_INTERVAL, 276477654350SAl Viro DEFAULT_RATELIMIT_BURST); 276577654350SAl Viro char comm[TASK_COMM_LEN]; 276677654350SAl Viro struct serial_struct32 v32; 276777654350SAl Viro struct serial_struct v; 276877654350SAl Viro int flags; 276977654350SAl Viro 2770a3241891SGustavo A. R. Silva if (copy_from_user(&v32, ss, sizeof(*ss))) 277177654350SAl Viro return -EFAULT; 277277654350SAl Viro 277377654350SAl Viro memcpy(&v, &v32, offsetof(struct serial_struct32, iomem_base)); 277477654350SAl Viro v.iomem_base = compat_ptr(v32.iomem_base); 277577654350SAl Viro v.iomem_reg_shift = v32.iomem_reg_shift; 277677654350SAl Viro v.port_high = v32.port_high; 277777654350SAl Viro v.iomap_base = 0; 277877654350SAl Viro 277977654350SAl Viro flags = v.flags & ASYNC_DEPRECATED; 278077654350SAl Viro 278177654350SAl Viro if (flags && __ratelimit(&depr_flags)) 278277654350SAl Viro pr_warn("%s: '%s' is using deprecated serial flags (with no effect): %.8x\n", 278377654350SAl Viro __func__, get_task_comm(comm, current), flags); 278477654350SAl Viro if (!tty->ops->set_serial) 278577654350SAl Viro return -ENOTTY; 278677654350SAl Viro return tty->ops->set_serial(tty, &v); 278777654350SAl Viro } 278877654350SAl Viro 278977654350SAl Viro static int compat_tty_tiocgserial(struct tty_struct *tty, 279077654350SAl Viro struct serial_struct32 __user *ss) 279177654350SAl Viro { 279277654350SAl Viro struct serial_struct32 v32; 279377654350SAl Viro struct serial_struct v; 279477654350SAl Viro int err; 279517329563SEric Biggers 279617329563SEric Biggers memset(&v, 0, sizeof(v)); 279717329563SEric Biggers memset(&v32, 0, sizeof(v32)); 279877654350SAl Viro 27996e622cd8SEric Biggers if (!tty->ops->get_serial) 280077654350SAl Viro return -ENOTTY; 280177654350SAl Viro err = tty->ops->get_serial(tty, &v); 280277654350SAl Viro if (!err) { 280377654350SAl Viro memcpy(&v32, &v, offsetof(struct serial_struct32, iomem_base)); 280477654350SAl Viro v32.iomem_base = (unsigned long)v.iomem_base >> 32 ? 280577654350SAl Viro 0xfffffff : ptr_to_compat(v.iomem_base); 280677654350SAl Viro v32.iomem_reg_shift = v.iomem_reg_shift; 280777654350SAl Viro v32.port_high = v.port_high; 2808a3241891SGustavo A. R. Silva if (copy_to_user(ss, &v32, sizeof(v32))) 280977654350SAl Viro err = -EFAULT; 281077654350SAl Viro } 281177654350SAl Viro return err; 281277654350SAl Viro } 281396fd7ce5SGreg Kroah-Hartman static long tty_compat_ioctl(struct file *file, unsigned int cmd, 281496fd7ce5SGreg Kroah-Hartman unsigned long arg) 281596fd7ce5SGreg Kroah-Hartman { 281696fd7ce5SGreg Kroah-Hartman struct tty_struct *tty = file_tty(file); 281796fd7ce5SGreg Kroah-Hartman struct tty_ldisc *ld; 281896fd7ce5SGreg Kroah-Hartman int retval = -ENOIOCTLCMD; 281996fd7ce5SGreg Kroah-Hartman 2820e2112038SAl Viro switch (cmd) { 2821c7dc504eSArnd Bergmann case TIOCOUTQ: 2822e2112038SAl Viro case TIOCSTI: 2823e2112038SAl Viro case TIOCGWINSZ: 2824e2112038SAl Viro case TIOCSWINSZ: 2825e2112038SAl Viro case TIOCGEXCL: 2826e2112038SAl Viro case TIOCGETD: 2827e2112038SAl Viro case TIOCSETD: 2828e2112038SAl Viro case TIOCGDEV: 2829e2112038SAl Viro case TIOCMGET: 2830e2112038SAl Viro case TIOCMSET: 2831e2112038SAl Viro case TIOCMBIC: 2832e2112038SAl Viro case TIOCMBIS: 2833e2112038SAl Viro case TIOCGICOUNT: 2834e2112038SAl Viro case TIOCGPGRP: 2835e2112038SAl Viro case TIOCSPGRP: 2836e2112038SAl Viro case TIOCGSID: 2837e2112038SAl Viro case TIOCSERGETLSR: 2838e2112038SAl Viro case TIOCGRS485: 2839e2112038SAl Viro case TIOCSRS485: 2840e2112038SAl Viro #ifdef TIOCGETP 2841e2112038SAl Viro case TIOCGETP: 2842e2112038SAl Viro case TIOCSETP: 2843e2112038SAl Viro case TIOCSETN: 2844e2112038SAl Viro #endif 2845e2112038SAl Viro #ifdef TIOCGETC 2846e2112038SAl Viro case TIOCGETC: 2847e2112038SAl Viro case TIOCSETC: 2848e2112038SAl Viro #endif 2849e2112038SAl Viro #ifdef TIOCGLTC 2850e2112038SAl Viro case TIOCGLTC: 2851e2112038SAl Viro case TIOCSLTC: 2852e2112038SAl Viro #endif 2853e2112038SAl Viro case TCSETSF: 2854e2112038SAl Viro case TCSETSW: 2855e2112038SAl Viro case TCSETS: 2856e2112038SAl Viro case TCGETS: 2857e2112038SAl Viro #ifdef TCGETS2 2858e2112038SAl Viro case TCGETS2: 2859e2112038SAl Viro case TCSETSF2: 2860e2112038SAl Viro case TCSETSW2: 2861e2112038SAl Viro case TCSETS2: 2862e2112038SAl Viro #endif 2863e2112038SAl Viro case TCGETA: 2864e2112038SAl Viro case TCSETAF: 2865e2112038SAl Viro case TCSETAW: 2866e2112038SAl Viro case TCSETA: 2867e2112038SAl Viro case TIOCGLCKTRMIOS: 2868e2112038SAl Viro case TIOCSLCKTRMIOS: 2869e2112038SAl Viro #ifdef TCGETX 2870e2112038SAl Viro case TCGETX: 2871e2112038SAl Viro case TCSETX: 2872e2112038SAl Viro case TCSETXW: 2873e2112038SAl Viro case TCSETXF: 2874e2112038SAl Viro #endif 2875e2112038SAl Viro case TIOCGSOFTCAR: 2876e2112038SAl Viro case TIOCSSOFTCAR: 2877b7aff093SArnd Bergmann 2878b7aff093SArnd Bergmann case PPPIOCGCHAN: 2879b7aff093SArnd Bergmann case PPPIOCGUNIT: 2880e2112038SAl Viro return tty_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); 2881e2112038SAl Viro case TIOCCONS: 2882e2112038SAl Viro case TIOCEXCL: 2883e2112038SAl Viro case TIOCNXCL: 2884e2112038SAl Viro case TIOCVHANGUP: 2885e2112038SAl Viro case TIOCSBRK: 2886e2112038SAl Viro case TIOCCBRK: 2887e2112038SAl Viro case TCSBRK: 2888e2112038SAl Viro case TCSBRKP: 2889e2112038SAl Viro case TCFLSH: 2890e2112038SAl Viro case TIOCGPTPEER: 2891e2112038SAl Viro case TIOCNOTTY: 2892e2112038SAl Viro case TIOCSCTTY: 2893e2112038SAl Viro case TCXONC: 2894e2112038SAl Viro case TIOCMIWAIT: 2895e2112038SAl Viro case TIOCSERCONFIG: 2896e2112038SAl Viro return tty_ioctl(file, cmd, arg); 2897e2112038SAl Viro } 2898e2112038SAl Viro 28996131ffaaSAl Viro if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl")) 290096fd7ce5SGreg Kroah-Hartman return -EINVAL; 290196fd7ce5SGreg Kroah-Hartman 290277654350SAl Viro switch (cmd) { 290377654350SAl Viro case TIOCSSERIAL: 290477654350SAl Viro return compat_tty_tiocsserial(tty, compat_ptr(arg)); 290577654350SAl Viro case TIOCGSERIAL: 290677654350SAl Viro return compat_tty_tiocgserial(tty, compat_ptr(arg)); 290777654350SAl Viro } 290896fd7ce5SGreg Kroah-Hartman if (tty->ops->compat_ioctl) { 2909c961bfb1SPeter Hurley retval = tty->ops->compat_ioctl(tty, cmd, arg); 291096fd7ce5SGreg Kroah-Hartman if (retval != -ENOIOCTLCMD) 291196fd7ce5SGreg Kroah-Hartman return retval; 291296fd7ce5SGreg Kroah-Hartman } 291396fd7ce5SGreg Kroah-Hartman 291496fd7ce5SGreg Kroah-Hartman ld = tty_ldisc_ref_wait(tty); 2915e55afd11SPeter Hurley if (!ld) 2916e55afd11SPeter Hurley return hung_up_tty_compat_ioctl(file, cmd, arg); 291796fd7ce5SGreg Kroah-Hartman if (ld->ops->compat_ioctl) 291896fd7ce5SGreg Kroah-Hartman retval = ld->ops->compat_ioctl(tty, file, cmd, arg); 2919f0193d3eSAl Viro if (retval == -ENOIOCTLCMD && ld->ops->ioctl) 2920f0193d3eSAl Viro retval = ld->ops->ioctl(tty, file, 2921f0193d3eSAl Viro (unsigned long)compat_ptr(cmd), arg); 292296fd7ce5SGreg Kroah-Hartman tty_ldisc_deref(ld); 292396fd7ce5SGreg Kroah-Hartman 292496fd7ce5SGreg Kroah-Hartman return retval; 292596fd7ce5SGreg Kroah-Hartman } 292696fd7ce5SGreg Kroah-Hartman #endif 292796fd7ce5SGreg Kroah-Hartman 2928c3c073f8SAl Viro static int this_tty(const void *t, struct file *file, unsigned fd) 2929c3c073f8SAl Viro { 2930dd78b0c4SLinus Torvalds if (likely(file->f_op->read_iter != tty_read)) 2931c3c073f8SAl Viro return 0; 2932c3c073f8SAl Viro return file_tty(file) != t ? 0 : fd + 1; 2933c3c073f8SAl Viro } 2934c3c073f8SAl Viro 293596fd7ce5SGreg Kroah-Hartman /* 293696fd7ce5SGreg Kroah-Hartman * This implements the "Secure Attention Key" --- the idea is to 293796fd7ce5SGreg Kroah-Hartman * prevent trojan horses by killing all processes associated with this 293896fd7ce5SGreg Kroah-Hartman * tty when the user hits the "Secure Attention Key". Required for 293996fd7ce5SGreg Kroah-Hartman * super-paranoid applications --- see the Orange Book for more details. 294096fd7ce5SGreg Kroah-Hartman * 294196fd7ce5SGreg Kroah-Hartman * This code could be nicer; ideally it should send a HUP, wait a few 294296fd7ce5SGreg Kroah-Hartman * seconds, then send a INT, and then a KILL signal. But you then 294396fd7ce5SGreg Kroah-Hartman * have to coordinate with the init process, since all processes associated 294496fd7ce5SGreg Kroah-Hartman * with the current tty must be dead before the new getty is allowed 294596fd7ce5SGreg Kroah-Hartman * to spawn. 294696fd7ce5SGreg Kroah-Hartman * 294796fd7ce5SGreg Kroah-Hartman * Now, if it would be correct ;-/ The current code has a nasty hole - 294896fd7ce5SGreg Kroah-Hartman * it doesn't catch files in flight. We may send the descriptor to ourselves 294996fd7ce5SGreg Kroah-Hartman * via AF_UNIX socket, close it and later fetch from socket. FIXME. 295096fd7ce5SGreg Kroah-Hartman * 295196fd7ce5SGreg Kroah-Hartman * Nasty bug: do_SAK is being called in interrupt context. This can 295296fd7ce5SGreg Kroah-Hartman * deadlock. We punt it up to process context. AKPM - 16Mar2001 295396fd7ce5SGreg Kroah-Hartman */ 295496fd7ce5SGreg Kroah-Hartman void __do_SAK(struct tty_struct *tty) 295596fd7ce5SGreg Kroah-Hartman { 295696fd7ce5SGreg Kroah-Hartman #ifdef TTY_SOFT_SAK 295796fd7ce5SGreg Kroah-Hartman tty_hangup(tty); 295896fd7ce5SGreg Kroah-Hartman #else 295996fd7ce5SGreg Kroah-Hartman struct task_struct *g, *p; 296096fd7ce5SGreg Kroah-Hartman struct pid *session; 296196fd7ce5SGreg Kroah-Hartman int i; 2962c8bcd9c5SJann Horn unsigned long flags; 296396fd7ce5SGreg Kroah-Hartman 296496fd7ce5SGreg Kroah-Hartman if (!tty) 296596fd7ce5SGreg Kroah-Hartman return; 2966c8bcd9c5SJann Horn 2967c8bcd9c5SJann Horn spin_lock_irqsave(&tty->ctrl_lock, flags); 2968c8bcd9c5SJann Horn session = get_pid(tty->session); 2969c8bcd9c5SJann Horn spin_unlock_irqrestore(&tty->ctrl_lock, flags); 297096fd7ce5SGreg Kroah-Hartman 297196fd7ce5SGreg Kroah-Hartman tty_ldisc_flush(tty); 297296fd7ce5SGreg Kroah-Hartman 297396fd7ce5SGreg Kroah-Hartman tty_driver_flush_buffer(tty); 297496fd7ce5SGreg Kroah-Hartman 297596fd7ce5SGreg Kroah-Hartman read_lock(&tasklist_lock); 297696fd7ce5SGreg Kroah-Hartman /* Kill the entire session */ 297796fd7ce5SGreg Kroah-Hartman do_each_pid_task(session, PIDTYPE_SID, p) { 29789b42bb75SPeter Hurley tty_notice(tty, "SAK: killed process %d (%s): by session\n", 297996fd7ce5SGreg Kroah-Hartman task_pid_nr(p), p->comm); 2980a8ebd171SEric W. Biederman group_send_sig_info(SIGKILL, SEND_SIG_PRIV, p, PIDTYPE_SID); 298196fd7ce5SGreg Kroah-Hartman } while_each_pid_task(session, PIDTYPE_SID, p); 29829b42bb75SPeter Hurley 29839b42bb75SPeter Hurley /* Now kill any processes that happen to have the tty open */ 298496fd7ce5SGreg Kroah-Hartman do_each_thread(g, p) { 298596fd7ce5SGreg Kroah-Hartman if (p->signal->tty == tty) { 29869b42bb75SPeter Hurley tty_notice(tty, "SAK: killed process %d (%s): by controlling tty\n", 298796fd7ce5SGreg Kroah-Hartman task_pid_nr(p), p->comm); 2988a8ebd171SEric W. Biederman group_send_sig_info(SIGKILL, SEND_SIG_PRIV, p, PIDTYPE_SID); 298996fd7ce5SGreg Kroah-Hartman continue; 299096fd7ce5SGreg Kroah-Hartman } 299196fd7ce5SGreg Kroah-Hartman task_lock(p); 2992c3c073f8SAl Viro i = iterate_fd(p->files, 0, this_tty, tty); 2993c3c073f8SAl Viro if (i != 0) { 29949b42bb75SPeter Hurley tty_notice(tty, "SAK: killed process %d (%s): by fd#%d\n", 2995c3c073f8SAl Viro task_pid_nr(p), p->comm, i - 1); 2996a8ebd171SEric W. Biederman group_send_sig_info(SIGKILL, SEND_SIG_PRIV, p, PIDTYPE_SID); 299796fd7ce5SGreg Kroah-Hartman } 299896fd7ce5SGreg Kroah-Hartman task_unlock(p); 299996fd7ce5SGreg Kroah-Hartman } while_each_thread(g, p); 300096fd7ce5SGreg Kroah-Hartman read_unlock(&tasklist_lock); 3001c8bcd9c5SJann Horn put_pid(session); 300296fd7ce5SGreg Kroah-Hartman #endif 300396fd7ce5SGreg Kroah-Hartman } 300496fd7ce5SGreg Kroah-Hartman 300596fd7ce5SGreg Kroah-Hartman static void do_SAK_work(struct work_struct *work) 300696fd7ce5SGreg Kroah-Hartman { 300796fd7ce5SGreg Kroah-Hartman struct tty_struct *tty = 300896fd7ce5SGreg Kroah-Hartman container_of(work, struct tty_struct, SAK_work); 300996fd7ce5SGreg Kroah-Hartman __do_SAK(tty); 301096fd7ce5SGreg Kroah-Hartman } 301196fd7ce5SGreg Kroah-Hartman 301296fd7ce5SGreg Kroah-Hartman /* 301396fd7ce5SGreg Kroah-Hartman * The tq handling here is a little racy - tty->SAK_work may already be queued. 301496fd7ce5SGreg Kroah-Hartman * Fortunately we don't need to worry, because if ->SAK_work is already queued, 301596fd7ce5SGreg Kroah-Hartman * the values which we write to it will be identical to the values which it 301696fd7ce5SGreg Kroah-Hartman * already has. --akpm 301796fd7ce5SGreg Kroah-Hartman */ 301896fd7ce5SGreg Kroah-Hartman void do_SAK(struct tty_struct *tty) 301996fd7ce5SGreg Kroah-Hartman { 302096fd7ce5SGreg Kroah-Hartman if (!tty) 302196fd7ce5SGreg Kroah-Hartman return; 302296fd7ce5SGreg Kroah-Hartman schedule_work(&tty->SAK_work); 302396fd7ce5SGreg Kroah-Hartman } 302496fd7ce5SGreg Kroah-Hartman 302596fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(do_SAK); 302696fd7ce5SGreg Kroah-Hartman 302796fd7ce5SGreg Kroah-Hartman /* Must put_device() after it's unused! */ 302896fd7ce5SGreg Kroah-Hartman static struct device *tty_get_device(struct tty_struct *tty) 302996fd7ce5SGreg Kroah-Hartman { 303096fd7ce5SGreg Kroah-Hartman dev_t devt = tty_devnum(tty); 30314495dfddSSuzuki K Poulose return class_find_device_by_devt(tty_class, devt); 303296fd7ce5SGreg Kroah-Hartman } 303396fd7ce5SGreg Kroah-Hartman 303496fd7ce5SGreg Kroah-Hartman 303596fd7ce5SGreg Kroah-Hartman /** 30362c964a2fSRasmus Villemoes * alloc_tty_struct 303796fd7ce5SGreg Kroah-Hartman * 30382c964a2fSRasmus Villemoes * This subroutine allocates and initializes a tty structure. 303996fd7ce5SGreg Kroah-Hartman * 30402c964a2fSRasmus Villemoes * Locking: none - tty in question is not exposed at this point 304196fd7ce5SGreg Kroah-Hartman */ 304296fd7ce5SGreg Kroah-Hartman 30432c964a2fSRasmus Villemoes struct tty_struct *alloc_tty_struct(struct tty_driver *driver, int idx) 304496fd7ce5SGreg Kroah-Hartman { 30452c964a2fSRasmus Villemoes struct tty_struct *tty; 30462c964a2fSRasmus Villemoes 30472c964a2fSRasmus Villemoes tty = kzalloc(sizeof(*tty), GFP_KERNEL); 30482c964a2fSRasmus Villemoes if (!tty) 30492c964a2fSRasmus Villemoes return NULL; 30502c964a2fSRasmus Villemoes 305196fd7ce5SGreg Kroah-Hartman kref_init(&tty->kref); 305296fd7ce5SGreg Kroah-Hartman tty->magic = TTY_MAGIC; 3053903f9db1STetsuo Handa if (tty_ldisc_init(tty)) { 3054903f9db1STetsuo Handa kfree(tty); 3055903f9db1STetsuo Handa return NULL; 3056903f9db1STetsuo Handa } 305796fd7ce5SGreg Kroah-Hartman tty->session = NULL; 305896fd7ce5SGreg Kroah-Hartman tty->pgrp = NULL; 305989c8d91eSAlan Cox mutex_init(&tty->legacy_mutex); 3060d8c1f929SPeter Hurley mutex_init(&tty->throttle_mutex); 30616a1c0680SPeter Hurley init_rwsem(&tty->termios_rwsem); 3062dee4a0beSPeter Hurley mutex_init(&tty->winsize_mutex); 306336697529SPeter Hurley init_ldsem(&tty->ldisc_sem); 306496fd7ce5SGreg Kroah-Hartman init_waitqueue_head(&tty->write_wait); 306596fd7ce5SGreg Kroah-Hartman init_waitqueue_head(&tty->read_wait); 306696fd7ce5SGreg Kroah-Hartman INIT_WORK(&tty->hangup_work, do_tty_hangup); 306796fd7ce5SGreg Kroah-Hartman mutex_init(&tty->atomic_write_lock); 306896fd7ce5SGreg Kroah-Hartman spin_lock_init(&tty->ctrl_lock); 3069f9e053dcSPeter Hurley spin_lock_init(&tty->flow_lock); 30704a510969SPeter Hurley spin_lock_init(&tty->files_lock); 307196fd7ce5SGreg Kroah-Hartman INIT_LIST_HEAD(&tty->tty_files); 307296fd7ce5SGreg Kroah-Hartman INIT_WORK(&tty->SAK_work, do_SAK_work); 307396fd7ce5SGreg Kroah-Hartman 307496fd7ce5SGreg Kroah-Hartman tty->driver = driver; 307596fd7ce5SGreg Kroah-Hartman tty->ops = driver->ops; 307696fd7ce5SGreg Kroah-Hartman tty->index = idx; 307796fd7ce5SGreg Kroah-Hartman tty_line_name(driver, idx, tty->name); 307896fd7ce5SGreg Kroah-Hartman tty->dev = tty_get_device(tty); 30792c964a2fSRasmus Villemoes 30802c964a2fSRasmus Villemoes return tty; 308196fd7ce5SGreg Kroah-Hartman } 308296fd7ce5SGreg Kroah-Hartman 308396fd7ce5SGreg Kroah-Hartman /** 308496fd7ce5SGreg Kroah-Hartman * tty_put_char - write one character to a tty 308596fd7ce5SGreg Kroah-Hartman * @tty: tty 308696fd7ce5SGreg Kroah-Hartman * @ch: character 308796fd7ce5SGreg Kroah-Hartman * 308896fd7ce5SGreg Kroah-Hartman * Write one byte to the tty using the provided put_char method 308996fd7ce5SGreg Kroah-Hartman * if present. Returns the number of characters successfully output. 309096fd7ce5SGreg Kroah-Hartman * 309196fd7ce5SGreg Kroah-Hartman * Note: the specific put_char operation in the driver layer may go 309296fd7ce5SGreg Kroah-Hartman * away soon. Don't call it directly, use this method 309396fd7ce5SGreg Kroah-Hartman */ 309496fd7ce5SGreg Kroah-Hartman 309596fd7ce5SGreg Kroah-Hartman int tty_put_char(struct tty_struct *tty, unsigned char ch) 309696fd7ce5SGreg Kroah-Hartman { 309796fd7ce5SGreg Kroah-Hartman if (tty->ops->put_char) 309896fd7ce5SGreg Kroah-Hartman return tty->ops->put_char(tty, ch); 309996fd7ce5SGreg Kroah-Hartman return tty->ops->write(tty, &ch, 1); 310096fd7ce5SGreg Kroah-Hartman } 310196fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(tty_put_char); 310296fd7ce5SGreg Kroah-Hartman 310396fd7ce5SGreg Kroah-Hartman struct class *tty_class; 310496fd7ce5SGreg Kroah-Hartman 31057e73eca6SJiri Slaby static int tty_cdev_add(struct tty_driver *driver, dev_t dev, 31067e73eca6SJiri Slaby unsigned int index, unsigned int count) 31077e73eca6SJiri Slaby { 3108c1a752baSLeon Yu int err; 3109c1a752baSLeon Yu 31107e73eca6SJiri Slaby /* init here, since reused cdevs cause crashes */ 3111a3a10ce3SRichard Watts driver->cdevs[index] = cdev_alloc(); 3112a3a10ce3SRichard Watts if (!driver->cdevs[index]) 3113a3a10ce3SRichard Watts return -ENOMEM; 3114c1a752baSLeon Yu driver->cdevs[index]->ops = &tty_fops; 3115a3a10ce3SRichard Watts driver->cdevs[index]->owner = driver->owner; 3116c1a752baSLeon Yu err = cdev_add(driver->cdevs[index], dev, count); 3117c1a752baSLeon Yu if (err) 3118c1a752baSLeon Yu kobject_put(&driver->cdevs[index]->kobj); 3119c1a752baSLeon Yu return err; 31207e73eca6SJiri Slaby } 31217e73eca6SJiri Slaby 312296fd7ce5SGreg Kroah-Hartman /** 312396fd7ce5SGreg Kroah-Hartman * tty_register_device - register a tty device 312496fd7ce5SGreg Kroah-Hartman * @driver: the tty driver that describes the tty device 312596fd7ce5SGreg Kroah-Hartman * @index: the index in the tty driver for this tty device 312696fd7ce5SGreg Kroah-Hartman * @device: a struct device that is associated with this tty device. 312796fd7ce5SGreg Kroah-Hartman * This field is optional, if there is no known struct device 312896fd7ce5SGreg Kroah-Hartman * for this tty device it can be set to NULL safely. 312996fd7ce5SGreg Kroah-Hartman * 313096fd7ce5SGreg Kroah-Hartman * Returns a pointer to the struct device for this tty device 313196fd7ce5SGreg Kroah-Hartman * (or ERR_PTR(-EFOO) on error). 313296fd7ce5SGreg Kroah-Hartman * 313396fd7ce5SGreg Kroah-Hartman * This call is required to be made to register an individual tty device 313496fd7ce5SGreg Kroah-Hartman * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If 313596fd7ce5SGreg Kroah-Hartman * that bit is not set, this function should not be called by a tty 313696fd7ce5SGreg Kroah-Hartman * driver. 313796fd7ce5SGreg Kroah-Hartman * 313896fd7ce5SGreg Kroah-Hartman * Locking: ?? 313996fd7ce5SGreg Kroah-Hartman */ 314096fd7ce5SGreg Kroah-Hartman 314196fd7ce5SGreg Kroah-Hartman struct device *tty_register_device(struct tty_driver *driver, unsigned index, 314296fd7ce5SGreg Kroah-Hartman struct device *device) 314396fd7ce5SGreg Kroah-Hartman { 31446915c0e4STomas Hlavacek return tty_register_device_attr(driver, index, device, NULL, NULL); 31456915c0e4STomas Hlavacek } 31466915c0e4STomas Hlavacek EXPORT_SYMBOL(tty_register_device); 31476915c0e4STomas Hlavacek 3148b1b79916STomas Hlavacek static void tty_device_create_release(struct device *dev) 3149b1b79916STomas Hlavacek { 315083db1df4SPeter Hurley dev_dbg(dev, "releasing...\n"); 3151b1b79916STomas Hlavacek kfree(dev); 3152b1b79916STomas Hlavacek } 3153b1b79916STomas Hlavacek 31546915c0e4STomas Hlavacek /** 31556915c0e4STomas Hlavacek * tty_register_device_attr - register a tty device 31566915c0e4STomas Hlavacek * @driver: the tty driver that describes the tty device 31576915c0e4STomas Hlavacek * @index: the index in the tty driver for this tty device 31586915c0e4STomas Hlavacek * @device: a struct device that is associated with this tty device. 31596915c0e4STomas Hlavacek * This field is optional, if there is no known struct device 31606915c0e4STomas Hlavacek * for this tty device it can be set to NULL safely. 31616915c0e4STomas Hlavacek * @drvdata: Driver data to be set to device. 31626915c0e4STomas Hlavacek * @attr_grp: Attribute group to be set on device. 31636915c0e4STomas Hlavacek * 31646915c0e4STomas Hlavacek * Returns a pointer to the struct device for this tty device 31656915c0e4STomas Hlavacek * (or ERR_PTR(-EFOO) on error). 31666915c0e4STomas Hlavacek * 31676915c0e4STomas Hlavacek * This call is required to be made to register an individual tty device 31686915c0e4STomas Hlavacek * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If 31696915c0e4STomas Hlavacek * that bit is not set, this function should not be called by a tty 31706915c0e4STomas Hlavacek * driver. 31716915c0e4STomas Hlavacek * 31726915c0e4STomas Hlavacek * Locking: ?? 31736915c0e4STomas Hlavacek */ 31746915c0e4STomas Hlavacek struct device *tty_register_device_attr(struct tty_driver *driver, 31756915c0e4STomas Hlavacek unsigned index, struct device *device, 31766915c0e4STomas Hlavacek void *drvdata, 31776915c0e4STomas Hlavacek const struct attribute_group **attr_grp) 31786915c0e4STomas Hlavacek { 317996fd7ce5SGreg Kroah-Hartman char name[64]; 31806915c0e4STomas Hlavacek dev_t devt = MKDEV(driver->major, driver->minor_start) + index; 318193857eddSJohan Hovold struct ktermios *tp; 31826a7e6f78SJohan Hovold struct device *dev; 31836a7e6f78SJohan Hovold int retval; 318496fd7ce5SGreg Kroah-Hartman 318596fd7ce5SGreg Kroah-Hartman if (index >= driver->num) { 3186656fb867SPeter Hurley pr_err("%s: Attempt to register invalid tty line number (%d)\n", 3187656fb867SPeter Hurley driver->name, index); 318896fd7ce5SGreg Kroah-Hartman return ERR_PTR(-EINVAL); 318996fd7ce5SGreg Kroah-Hartman } 319096fd7ce5SGreg Kroah-Hartman 319196fd7ce5SGreg Kroah-Hartman if (driver->type == TTY_DRIVER_TYPE_PTY) 319296fd7ce5SGreg Kroah-Hartman pty_line_name(driver, index, name); 319396fd7ce5SGreg Kroah-Hartman else 319496fd7ce5SGreg Kroah-Hartman tty_line_name(driver, index, name); 319596fd7ce5SGreg Kroah-Hartman 31966915c0e4STomas Hlavacek dev = kzalloc(sizeof(*dev), GFP_KERNEL); 31976a7e6f78SJohan Hovold if (!dev) 31986a7e6f78SJohan Hovold return ERR_PTR(-ENOMEM); 31996915c0e4STomas Hlavacek 32006915c0e4STomas Hlavacek dev->devt = devt; 32016915c0e4STomas Hlavacek dev->class = tty_class; 32026915c0e4STomas Hlavacek dev->parent = device; 3203b1b79916STomas Hlavacek dev->release = tty_device_create_release; 32046915c0e4STomas Hlavacek dev_set_name(dev, "%s", name); 32056915c0e4STomas Hlavacek dev->groups = attr_grp; 32066915c0e4STomas Hlavacek dev_set_drvdata(dev, drvdata); 32076915c0e4STomas Hlavacek 32086a7e6f78SJohan Hovold dev_set_uevent_suppress(dev, 1); 32096a7e6f78SJohan Hovold 32106915c0e4STomas Hlavacek retval = device_register(dev); 32116915c0e4STomas Hlavacek if (retval) 32126a7e6f78SJohan Hovold goto err_put; 32136a7e6f78SJohan Hovold 32146a7e6f78SJohan Hovold if (!(driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)) { 321593857eddSJohan Hovold /* 321693857eddSJohan Hovold * Free any saved termios data so that the termios state is 321793857eddSJohan Hovold * reset when reusing a minor number. 321893857eddSJohan Hovold */ 321993857eddSJohan Hovold tp = driver->termios[index]; 322093857eddSJohan Hovold if (tp) { 322193857eddSJohan Hovold driver->termios[index] = NULL; 322293857eddSJohan Hovold kfree(tp); 322393857eddSJohan Hovold } 322493857eddSJohan Hovold 32256a7e6f78SJohan Hovold retval = tty_cdev_add(driver, devt, index, 1); 32266a7e6f78SJohan Hovold if (retval) 32276a7e6f78SJohan Hovold goto err_del; 32286a7e6f78SJohan Hovold } 32296a7e6f78SJohan Hovold 32306a7e6f78SJohan Hovold dev_set_uevent_suppress(dev, 0); 32316a7e6f78SJohan Hovold kobject_uevent(&dev->kobj, KOBJ_ADD); 32326915c0e4STomas Hlavacek 32336915c0e4STomas Hlavacek return dev; 32346915c0e4STomas Hlavacek 32356a7e6f78SJohan Hovold err_del: 32366a7e6f78SJohan Hovold device_del(dev); 32376a7e6f78SJohan Hovold err_put: 32386915c0e4STomas Hlavacek put_device(dev); 32396a7e6f78SJohan Hovold 32406915c0e4STomas Hlavacek return ERR_PTR(retval); 32416915c0e4STomas Hlavacek } 32426915c0e4STomas Hlavacek EXPORT_SYMBOL_GPL(tty_register_device_attr); 324396fd7ce5SGreg Kroah-Hartman 324496fd7ce5SGreg Kroah-Hartman /** 324596fd7ce5SGreg Kroah-Hartman * tty_unregister_device - unregister a tty device 324696fd7ce5SGreg Kroah-Hartman * @driver: the tty driver that describes the tty device 324796fd7ce5SGreg Kroah-Hartman * @index: the index in the tty driver for this tty device 324896fd7ce5SGreg Kroah-Hartman * 324996fd7ce5SGreg Kroah-Hartman * If a tty device is registered with a call to tty_register_device() then 325096fd7ce5SGreg Kroah-Hartman * this function must be called when the tty device is gone. 325196fd7ce5SGreg Kroah-Hartman * 325296fd7ce5SGreg Kroah-Hartman * Locking: ?? 325396fd7ce5SGreg Kroah-Hartman */ 325496fd7ce5SGreg Kroah-Hartman 325596fd7ce5SGreg Kroah-Hartman void tty_unregister_device(struct tty_driver *driver, unsigned index) 325696fd7ce5SGreg Kroah-Hartman { 325796fd7ce5SGreg Kroah-Hartman device_destroy(tty_class, 325896fd7ce5SGreg Kroah-Hartman MKDEV(driver->major, driver->minor_start) + index); 3259a3a10ce3SRichard Watts if (!(driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)) { 3260a3a10ce3SRichard Watts cdev_del(driver->cdevs[index]); 3261a3a10ce3SRichard Watts driver->cdevs[index] = NULL; 3262a3a10ce3SRichard Watts } 326396fd7ce5SGreg Kroah-Hartman } 326496fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_unregister_device); 326596fd7ce5SGreg Kroah-Hartman 32667f0bc6a6SJiri Slaby /** 32677f0bc6a6SJiri Slaby * __tty_alloc_driver -- allocate tty driver 32687f0bc6a6SJiri Slaby * @lines: count of lines this driver can handle at most 326987838ae3SThadeu Lima de Souza Cascardo * @owner: module which is responsible for this driver 32707f0bc6a6SJiri Slaby * @flags: some of TTY_DRIVER_* flags, will be set in driver->flags 32717f0bc6a6SJiri Slaby * 32727f0bc6a6SJiri Slaby * This should not be called directly, some of the provided macros should be 32737f0bc6a6SJiri Slaby * used instead. Use IS_ERR and friends on @retval. 32747f0bc6a6SJiri Slaby */ 32757f0bc6a6SJiri Slaby struct tty_driver *__tty_alloc_driver(unsigned int lines, struct module *owner, 32767f0bc6a6SJiri Slaby unsigned long flags) 327796fd7ce5SGreg Kroah-Hartman { 327896fd7ce5SGreg Kroah-Hartman struct tty_driver *driver; 32797e73eca6SJiri Slaby unsigned int cdevs = 1; 328016a02081SJiri Slaby int err; 328196fd7ce5SGreg Kroah-Hartman 32820019b408SJiri Slaby if (!lines || (flags & TTY_DRIVER_UNNUMBERED_NODE && lines > 1)) 32837f0bc6a6SJiri Slaby return ERR_PTR(-EINVAL); 32847f0bc6a6SJiri Slaby 3285a3241891SGustavo A. R. Silva driver = kzalloc(sizeof(*driver), GFP_KERNEL); 32867f0bc6a6SJiri Slaby if (!driver) 32877f0bc6a6SJiri Slaby return ERR_PTR(-ENOMEM); 32887f0bc6a6SJiri Slaby 328996fd7ce5SGreg Kroah-Hartman kref_init(&driver->kref); 329096fd7ce5SGreg Kroah-Hartman driver->magic = TTY_DRIVER_MAGIC; 329196fd7ce5SGreg Kroah-Hartman driver->num = lines; 32921a54a76dSJiri Slaby driver->owner = owner; 32937f0bc6a6SJiri Slaby driver->flags = flags; 329416a02081SJiri Slaby 329516a02081SJiri Slaby if (!(flags & TTY_DRIVER_DEVPTS_MEM)) { 329616a02081SJiri Slaby driver->ttys = kcalloc(lines, sizeof(*driver->ttys), 329716a02081SJiri Slaby GFP_KERNEL); 329816a02081SJiri Slaby driver->termios = kcalloc(lines, sizeof(*driver->termios), 329916a02081SJiri Slaby GFP_KERNEL); 330016a02081SJiri Slaby if (!driver->ttys || !driver->termios) { 330116a02081SJiri Slaby err = -ENOMEM; 330216a02081SJiri Slaby goto err_free_all; 330316a02081SJiri Slaby } 330416a02081SJiri Slaby } 330516a02081SJiri Slaby 330616a02081SJiri Slaby if (!(flags & TTY_DRIVER_DYNAMIC_ALLOC)) { 330716a02081SJiri Slaby driver->ports = kcalloc(lines, sizeof(*driver->ports), 330816a02081SJiri Slaby GFP_KERNEL); 330916a02081SJiri Slaby if (!driver->ports) { 331016a02081SJiri Slaby err = -ENOMEM; 331116a02081SJiri Slaby goto err_free_all; 331216a02081SJiri Slaby } 33137e73eca6SJiri Slaby cdevs = lines; 33147e73eca6SJiri Slaby } 33157e73eca6SJiri Slaby 33167e73eca6SJiri Slaby driver->cdevs = kcalloc(cdevs, sizeof(*driver->cdevs), GFP_KERNEL); 33177e73eca6SJiri Slaby if (!driver->cdevs) { 33187e73eca6SJiri Slaby err = -ENOMEM; 33197e73eca6SJiri Slaby goto err_free_all; 332016a02081SJiri Slaby } 33217f0bc6a6SJiri Slaby 332296fd7ce5SGreg Kroah-Hartman return driver; 332316a02081SJiri Slaby err_free_all: 332416a02081SJiri Slaby kfree(driver->ports); 332516a02081SJiri Slaby kfree(driver->ttys); 332616a02081SJiri Slaby kfree(driver->termios); 3327a3a10ce3SRichard Watts kfree(driver->cdevs); 332816a02081SJiri Slaby kfree(driver); 332916a02081SJiri Slaby return ERR_PTR(err); 333096fd7ce5SGreg Kroah-Hartman } 33317f0bc6a6SJiri Slaby EXPORT_SYMBOL(__tty_alloc_driver); 333296fd7ce5SGreg Kroah-Hartman 333396fd7ce5SGreg Kroah-Hartman static void destruct_tty_driver(struct kref *kref) 333496fd7ce5SGreg Kroah-Hartman { 333596fd7ce5SGreg Kroah-Hartman struct tty_driver *driver = container_of(kref, struct tty_driver, kref); 333696fd7ce5SGreg Kroah-Hartman int i; 333796fd7ce5SGreg Kroah-Hartman struct ktermios *tp; 333896fd7ce5SGreg Kroah-Hartman 333996fd7ce5SGreg Kroah-Hartman if (driver->flags & TTY_DRIVER_INSTALLED) { 334096fd7ce5SGreg Kroah-Hartman for (i = 0; i < driver->num; i++) { 334196fd7ce5SGreg Kroah-Hartman tp = driver->termios[i]; 334296fd7ce5SGreg Kroah-Hartman if (tp) { 334396fd7ce5SGreg Kroah-Hartman driver->termios[i] = NULL; 334496fd7ce5SGreg Kroah-Hartman kfree(tp); 334596fd7ce5SGreg Kroah-Hartman } 334696fd7ce5SGreg Kroah-Hartman if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) 334796fd7ce5SGreg Kroah-Hartman tty_unregister_device(driver, i); 334896fd7ce5SGreg Kroah-Hartman } 334996fd7ce5SGreg Kroah-Hartman proc_tty_unregister_driver(driver); 33507e73eca6SJiri Slaby if (driver->flags & TTY_DRIVER_DYNAMIC_ALLOC) 3351a3a10ce3SRichard Watts cdev_del(driver->cdevs[0]); 335296fd7ce5SGreg Kroah-Hartman } 33537e73eca6SJiri Slaby kfree(driver->cdevs); 335404831dc1SJiri Slaby kfree(driver->ports); 335516a02081SJiri Slaby kfree(driver->termios); 335616a02081SJiri Slaby kfree(driver->ttys); 335796fd7ce5SGreg Kroah-Hartman kfree(driver); 335896fd7ce5SGreg Kroah-Hartman } 335996fd7ce5SGreg Kroah-Hartman 336096fd7ce5SGreg Kroah-Hartman void tty_driver_kref_put(struct tty_driver *driver) 336196fd7ce5SGreg Kroah-Hartman { 336296fd7ce5SGreg Kroah-Hartman kref_put(&driver->kref, destruct_tty_driver); 336396fd7ce5SGreg Kroah-Hartman } 336496fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_driver_kref_put); 336596fd7ce5SGreg Kroah-Hartman 336696fd7ce5SGreg Kroah-Hartman void tty_set_operations(struct tty_driver *driver, 336796fd7ce5SGreg Kroah-Hartman const struct tty_operations *op) 336896fd7ce5SGreg Kroah-Hartman { 336996fd7ce5SGreg Kroah-Hartman driver->ops = op; 337096fd7ce5SGreg Kroah-Hartman }; 337196fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_set_operations); 337296fd7ce5SGreg Kroah-Hartman 337396fd7ce5SGreg Kroah-Hartman void put_tty_driver(struct tty_driver *d) 337496fd7ce5SGreg Kroah-Hartman { 337596fd7ce5SGreg Kroah-Hartman tty_driver_kref_put(d); 337696fd7ce5SGreg Kroah-Hartman } 337796fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(put_tty_driver); 337896fd7ce5SGreg Kroah-Hartman 337996fd7ce5SGreg Kroah-Hartman /* 338096fd7ce5SGreg Kroah-Hartman * Called by a tty driver to register itself. 338196fd7ce5SGreg Kroah-Hartman */ 338296fd7ce5SGreg Kroah-Hartman int tty_register_driver(struct tty_driver *driver) 338396fd7ce5SGreg Kroah-Hartman { 338496fd7ce5SGreg Kroah-Hartman int error; 338596fd7ce5SGreg Kroah-Hartman int i; 338696fd7ce5SGreg Kroah-Hartman dev_t dev; 338796fd7ce5SGreg Kroah-Hartman struct device *d; 338896fd7ce5SGreg Kroah-Hartman 338996fd7ce5SGreg Kroah-Hartman if (!driver->major) { 339096fd7ce5SGreg Kroah-Hartman error = alloc_chrdev_region(&dev, driver->minor_start, 339196fd7ce5SGreg Kroah-Hartman driver->num, driver->name); 339296fd7ce5SGreg Kroah-Hartman if (!error) { 339396fd7ce5SGreg Kroah-Hartman driver->major = MAJOR(dev); 339496fd7ce5SGreg Kroah-Hartman driver->minor_start = MINOR(dev); 339596fd7ce5SGreg Kroah-Hartman } 339696fd7ce5SGreg Kroah-Hartman } else { 339796fd7ce5SGreg Kroah-Hartman dev = MKDEV(driver->major, driver->minor_start); 339896fd7ce5SGreg Kroah-Hartman error = register_chrdev_region(dev, driver->num, driver->name); 339996fd7ce5SGreg Kroah-Hartman } 34009bb8a3d4SJiri Slaby if (error < 0) 340116a02081SJiri Slaby goto err; 340296fd7ce5SGreg Kroah-Hartman 34037e73eca6SJiri Slaby if (driver->flags & TTY_DRIVER_DYNAMIC_ALLOC) { 34047e73eca6SJiri Slaby error = tty_cdev_add(driver, dev, 0, driver->num); 34059bb8a3d4SJiri Slaby if (error) 34069bb8a3d4SJiri Slaby goto err_unreg_char; 34077e73eca6SJiri Slaby } 340896fd7ce5SGreg Kroah-Hartman 340996fd7ce5SGreg Kroah-Hartman mutex_lock(&tty_mutex); 341096fd7ce5SGreg Kroah-Hartman list_add(&driver->tty_drivers, &tty_drivers); 341196fd7ce5SGreg Kroah-Hartman mutex_unlock(&tty_mutex); 341296fd7ce5SGreg Kroah-Hartman 341396fd7ce5SGreg Kroah-Hartman if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) { 341496fd7ce5SGreg Kroah-Hartman for (i = 0; i < driver->num; i++) { 341596fd7ce5SGreg Kroah-Hartman d = tty_register_device(driver, i, NULL); 341696fd7ce5SGreg Kroah-Hartman if (IS_ERR(d)) { 341796fd7ce5SGreg Kroah-Hartman error = PTR_ERR(d); 341816a02081SJiri Slaby goto err_unreg_devs; 341996fd7ce5SGreg Kroah-Hartman } 342096fd7ce5SGreg Kroah-Hartman } 342196fd7ce5SGreg Kroah-Hartman } 342296fd7ce5SGreg Kroah-Hartman proc_tty_register_driver(driver); 342396fd7ce5SGreg Kroah-Hartman driver->flags |= TTY_DRIVER_INSTALLED; 342496fd7ce5SGreg Kroah-Hartman return 0; 342596fd7ce5SGreg Kroah-Hartman 342616a02081SJiri Slaby err_unreg_devs: 342796fd7ce5SGreg Kroah-Hartman for (i--; i >= 0; i--) 342896fd7ce5SGreg Kroah-Hartman tty_unregister_device(driver, i); 342996fd7ce5SGreg Kroah-Hartman 343096fd7ce5SGreg Kroah-Hartman mutex_lock(&tty_mutex); 343196fd7ce5SGreg Kroah-Hartman list_del(&driver->tty_drivers); 343296fd7ce5SGreg Kroah-Hartman mutex_unlock(&tty_mutex); 343396fd7ce5SGreg Kroah-Hartman 34349bb8a3d4SJiri Slaby err_unreg_char: 343596fd7ce5SGreg Kroah-Hartman unregister_chrdev_region(dev, driver->num); 343616a02081SJiri Slaby err: 343796fd7ce5SGreg Kroah-Hartman return error; 343896fd7ce5SGreg Kroah-Hartman } 343996fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_register_driver); 344096fd7ce5SGreg Kroah-Hartman 344196fd7ce5SGreg Kroah-Hartman /* 344296fd7ce5SGreg Kroah-Hartman * Called by a tty driver to unregister itself. 344396fd7ce5SGreg Kroah-Hartman */ 344496fd7ce5SGreg Kroah-Hartman int tty_unregister_driver(struct tty_driver *driver) 344596fd7ce5SGreg Kroah-Hartman { 344696fd7ce5SGreg Kroah-Hartman #if 0 344796fd7ce5SGreg Kroah-Hartman /* FIXME */ 344896fd7ce5SGreg Kroah-Hartman if (driver->refcount) 344996fd7ce5SGreg Kroah-Hartman return -EBUSY; 345096fd7ce5SGreg Kroah-Hartman #endif 345196fd7ce5SGreg Kroah-Hartman unregister_chrdev_region(MKDEV(driver->major, driver->minor_start), 345296fd7ce5SGreg Kroah-Hartman driver->num); 345396fd7ce5SGreg Kroah-Hartman mutex_lock(&tty_mutex); 345496fd7ce5SGreg Kroah-Hartman list_del(&driver->tty_drivers); 345596fd7ce5SGreg Kroah-Hartman mutex_unlock(&tty_mutex); 345696fd7ce5SGreg Kroah-Hartman return 0; 345796fd7ce5SGreg Kroah-Hartman } 345896fd7ce5SGreg Kroah-Hartman 345996fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_unregister_driver); 346096fd7ce5SGreg Kroah-Hartman 346196fd7ce5SGreg Kroah-Hartman dev_t tty_devnum(struct tty_struct *tty) 346296fd7ce5SGreg Kroah-Hartman { 346396fd7ce5SGreg Kroah-Hartman return MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index; 346496fd7ce5SGreg Kroah-Hartman } 346596fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_devnum); 346696fd7ce5SGreg Kroah-Hartman 346796fd7ce5SGreg Kroah-Hartman void tty_default_fops(struct file_operations *fops) 346896fd7ce5SGreg Kroah-Hartman { 346996fd7ce5SGreg Kroah-Hartman *fops = tty_fops; 347096fd7ce5SGreg Kroah-Hartman } 347196fd7ce5SGreg Kroah-Hartman 34722c9ede55SAl Viro static char *tty_devnode(struct device *dev, umode_t *mode) 347396fd7ce5SGreg Kroah-Hartman { 347496fd7ce5SGreg Kroah-Hartman if (!mode) 347596fd7ce5SGreg Kroah-Hartman return NULL; 347696fd7ce5SGreg Kroah-Hartman if (dev->devt == MKDEV(TTYAUX_MAJOR, 0) || 347796fd7ce5SGreg Kroah-Hartman dev->devt == MKDEV(TTYAUX_MAJOR, 2)) 347896fd7ce5SGreg Kroah-Hartman *mode = 0666; 347996fd7ce5SGreg Kroah-Hartman return NULL; 348096fd7ce5SGreg Kroah-Hartman } 348196fd7ce5SGreg Kroah-Hartman 348296fd7ce5SGreg Kroah-Hartman static int __init tty_class_init(void) 348396fd7ce5SGreg Kroah-Hartman { 348496fd7ce5SGreg Kroah-Hartman tty_class = class_create(THIS_MODULE, "tty"); 348596fd7ce5SGreg Kroah-Hartman if (IS_ERR(tty_class)) 348696fd7ce5SGreg Kroah-Hartman return PTR_ERR(tty_class); 348796fd7ce5SGreg Kroah-Hartman tty_class->devnode = tty_devnode; 348896fd7ce5SGreg Kroah-Hartman return 0; 348996fd7ce5SGreg Kroah-Hartman } 349096fd7ce5SGreg Kroah-Hartman 349196fd7ce5SGreg Kroah-Hartman postcore_initcall(tty_class_init); 349296fd7ce5SGreg Kroah-Hartman 349396fd7ce5SGreg Kroah-Hartman /* 3/2004 jmc: why do these devices exist? */ 349496fd7ce5SGreg Kroah-Hartman static struct cdev tty_cdev, console_cdev; 349596fd7ce5SGreg Kroah-Hartman 3496fbc92a34SKay Sievers static ssize_t show_cons_active(struct device *dev, 3497fbc92a34SKay Sievers struct device_attribute *attr, char *buf) 3498fbc92a34SKay Sievers { 3499fbc92a34SKay Sievers struct console *cs[16]; 3500fbc92a34SKay Sievers int i = 0; 3501fbc92a34SKay Sievers struct console *c; 3502fbc92a34SKay Sievers ssize_t count = 0; 3503fbc92a34SKay Sievers 3504ac751efaSTorben Hohn console_lock(); 3505a2a6a822SKay Sievers for_each_console(c) { 3506fbc92a34SKay Sievers if (!c->device) 3507fbc92a34SKay Sievers continue; 3508fbc92a34SKay Sievers if (!c->write) 3509fbc92a34SKay Sievers continue; 3510fbc92a34SKay Sievers if ((c->flags & CON_ENABLED) == 0) 3511fbc92a34SKay Sievers continue; 3512fbc92a34SKay Sievers cs[i++] = c; 3513fbc92a34SKay Sievers if (i >= ARRAY_SIZE(cs)) 3514fbc92a34SKay Sievers break; 3515fbc92a34SKay Sievers } 3516723abd87SHannes Reinecke while (i--) { 3517723abd87SHannes Reinecke int index = cs[i]->index; 3518723abd87SHannes Reinecke struct tty_driver *drv = cs[i]->device(cs[i], &index); 3519723abd87SHannes Reinecke 3520723abd87SHannes Reinecke /* don't resolve tty0 as some programs depend on it */ 3521723abd87SHannes Reinecke if (drv && (cs[i]->index > 0 || drv->major != TTY_MAJOR)) 3522723abd87SHannes Reinecke count += tty_line_name(drv, index, buf + count); 3523723abd87SHannes Reinecke else 3524723abd87SHannes Reinecke count += sprintf(buf + count, "%s%d", 3525723abd87SHannes Reinecke cs[i]->name, cs[i]->index); 3526723abd87SHannes Reinecke 3527723abd87SHannes Reinecke count += sprintf(buf + count, "%c", i ? ' ':'\n'); 3528723abd87SHannes Reinecke } 3529ac751efaSTorben Hohn console_unlock(); 3530fbc92a34SKay Sievers 3531fbc92a34SKay Sievers return count; 3532fbc92a34SKay Sievers } 3533fbc92a34SKay Sievers static DEVICE_ATTR(active, S_IRUGO, show_cons_active, NULL); 3534fbc92a34SKay Sievers 35351083a7beSTakashi Iwai static struct attribute *cons_dev_attrs[] = { 35361083a7beSTakashi Iwai &dev_attr_active.attr, 35371083a7beSTakashi Iwai NULL 35381083a7beSTakashi Iwai }; 35391083a7beSTakashi Iwai 35401083a7beSTakashi Iwai ATTRIBUTE_GROUPS(cons_dev); 35411083a7beSTakashi Iwai 3542fbc92a34SKay Sievers static struct device *consdev; 3543fbc92a34SKay Sievers 3544fbc92a34SKay Sievers void console_sysfs_notify(void) 3545fbc92a34SKay Sievers { 3546fbc92a34SKay Sievers if (consdev) 3547fbc92a34SKay Sievers sysfs_notify(&consdev->kobj, NULL, "active"); 3548fbc92a34SKay Sievers } 3549fbc92a34SKay Sievers 355096fd7ce5SGreg Kroah-Hartman /* 355196fd7ce5SGreg Kroah-Hartman * Ok, now we can initialize the rest of the tty devices and can count 355296fd7ce5SGreg Kroah-Hartman * on memory allocations, interrupts etc.. 355396fd7ce5SGreg Kroah-Hartman */ 355496fd7ce5SGreg Kroah-Hartman int __init tty_init(void) 355596fd7ce5SGreg Kroah-Hartman { 35567c0cca7cSGreg Kroah-Hartman tty_sysctl_init(); 355796fd7ce5SGreg Kroah-Hartman cdev_init(&tty_cdev, &tty_fops); 355896fd7ce5SGreg Kroah-Hartman if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) || 355996fd7ce5SGreg Kroah-Hartman register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0) 356096fd7ce5SGreg Kroah-Hartman panic("Couldn't register /dev/tty driver\n"); 3561fbc92a34SKay Sievers device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), NULL, "tty"); 356296fd7ce5SGreg Kroah-Hartman 356396fd7ce5SGreg Kroah-Hartman cdev_init(&console_cdev, &console_fops); 356496fd7ce5SGreg Kroah-Hartman if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) || 356596fd7ce5SGreg Kroah-Hartman register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0) 356696fd7ce5SGreg Kroah-Hartman panic("Couldn't register /dev/console driver\n"); 35671083a7beSTakashi Iwai consdev = device_create_with_groups(tty_class, NULL, 35681083a7beSTakashi Iwai MKDEV(TTYAUX_MAJOR, 1), NULL, 35691083a7beSTakashi Iwai cons_dev_groups, "console"); 3570fbc92a34SKay Sievers if (IS_ERR(consdev)) 3571fbc92a34SKay Sievers consdev = NULL; 357296fd7ce5SGreg Kroah-Hartman 357396fd7ce5SGreg Kroah-Hartman #ifdef CONFIG_VT 357496fd7ce5SGreg Kroah-Hartman vty_init(&console_fops); 357596fd7ce5SGreg Kroah-Hartman #endif 357696fd7ce5SGreg Kroah-Hartman return 0; 357796fd7ce5SGreg Kroah-Hartman } 357896fd7ce5SGreg Kroah-Hartman 3579