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 14596fd7ce5SGreg Kroah-Hartman static ssize_t tty_read(struct file *, char __user *, size_t, loff_t *); 14696fd7ce5SGreg Kroah-Hartman static ssize_t tty_write(struct file *, const char __user *, size_t, loff_t *); 14796fd7ce5SGreg Kroah-Hartman ssize_t redirected_tty_write(struct file *, const char __user *, 14896fd7ce5SGreg Kroah-Hartman size_t, loff_t *); 149afc9a42bSAl Viro static __poll_t tty_poll(struct file *, poll_table *); 15096fd7ce5SGreg Kroah-Hartman static int tty_open(struct inode *, struct file *); 15196fd7ce5SGreg Kroah-Hartman long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg); 15296fd7ce5SGreg Kroah-Hartman #ifdef CONFIG_COMPAT 15396fd7ce5SGreg Kroah-Hartman static long tty_compat_ioctl(struct file *file, unsigned int cmd, 15496fd7ce5SGreg Kroah-Hartman unsigned long arg); 15596fd7ce5SGreg Kroah-Hartman #else 15696fd7ce5SGreg Kroah-Hartman #define tty_compat_ioctl NULL 15796fd7ce5SGreg Kroah-Hartman #endif 15896fd7ce5SGreg Kroah-Hartman static int __tty_fasync(int fd, struct file *filp, int on); 15996fd7ce5SGreg Kroah-Hartman static int tty_fasync(int fd, struct file *filp, int on); 16096fd7ce5SGreg Kroah-Hartman static void release_tty(struct tty_struct *tty, int idx); 16196fd7ce5SGreg Kroah-Hartman 16296fd7ce5SGreg Kroah-Hartman /** 16396fd7ce5SGreg Kroah-Hartman * free_tty_struct - free a disused tty 16496fd7ce5SGreg Kroah-Hartman * @tty: tty struct to free 16596fd7ce5SGreg Kroah-Hartman * 16696fd7ce5SGreg Kroah-Hartman * Free the write buffers, tty queue and tty memory itself. 16796fd7ce5SGreg Kroah-Hartman * 16896fd7ce5SGreg Kroah-Hartman * Locking: none. Must be called after tty is definitely unused 16996fd7ce5SGreg Kroah-Hartman */ 17096fd7ce5SGreg Kroah-Hartman 171a3123fd0SPeter Hurley static void free_tty_struct(struct tty_struct *tty) 17296fd7ce5SGreg Kroah-Hartman { 173c8b710b3SPeter Hurley tty_ldisc_deinit(tty); 17496fd7ce5SGreg Kroah-Hartman put_device(tty->dev); 17596fd7ce5SGreg Kroah-Hartman kfree(tty->write_buf); 17689c8d91eSAlan Cox tty->magic = 0xDEADDEAD; 17796fd7ce5SGreg Kroah-Hartman kfree(tty); 17896fd7ce5SGreg Kroah-Hartman } 17996fd7ce5SGreg Kroah-Hartman 18096fd7ce5SGreg Kroah-Hartman static inline struct tty_struct *file_tty(struct file *file) 18196fd7ce5SGreg Kroah-Hartman { 18296fd7ce5SGreg Kroah-Hartman return ((struct tty_file_private *)file->private_data)->tty; 18396fd7ce5SGreg Kroah-Hartman } 18496fd7ce5SGreg Kroah-Hartman 185fa90e1c9SJiri Slaby int tty_alloc_file(struct file *file) 18696fd7ce5SGreg Kroah-Hartman { 18796fd7ce5SGreg Kroah-Hartman struct tty_file_private *priv; 18896fd7ce5SGreg Kroah-Hartman 18996fd7ce5SGreg Kroah-Hartman priv = kmalloc(sizeof(*priv), GFP_KERNEL); 19096fd7ce5SGreg Kroah-Hartman if (!priv) 19196fd7ce5SGreg Kroah-Hartman return -ENOMEM; 19296fd7ce5SGreg Kroah-Hartman 193fa90e1c9SJiri Slaby file->private_data = priv; 194fa90e1c9SJiri Slaby 195fa90e1c9SJiri Slaby return 0; 196fa90e1c9SJiri Slaby } 197fa90e1c9SJiri Slaby 198fa90e1c9SJiri Slaby /* Associate a new file with the tty structure */ 199fa90e1c9SJiri Slaby void tty_add_file(struct tty_struct *tty, struct file *file) 200fa90e1c9SJiri Slaby { 201fa90e1c9SJiri Slaby struct tty_file_private *priv = file->private_data; 202fa90e1c9SJiri Slaby 20396fd7ce5SGreg Kroah-Hartman priv->tty = tty; 20496fd7ce5SGreg Kroah-Hartman priv->file = file; 20596fd7ce5SGreg Kroah-Hartman 2064a510969SPeter Hurley spin_lock(&tty->files_lock); 20796fd7ce5SGreg Kroah-Hartman list_add(&priv->list, &tty->tty_files); 2084a510969SPeter Hurley spin_unlock(&tty->files_lock); 209fa90e1c9SJiri Slaby } 21096fd7ce5SGreg Kroah-Hartman 211fa90e1c9SJiri Slaby /** 212fa90e1c9SJiri Slaby * tty_free_file - free file->private_data 213fa90e1c9SJiri Slaby * 214fa90e1c9SJiri Slaby * This shall be used only for fail path handling when tty_add_file was not 215fa90e1c9SJiri Slaby * called yet. 216fa90e1c9SJiri Slaby */ 217fa90e1c9SJiri Slaby void tty_free_file(struct file *file) 218fa90e1c9SJiri Slaby { 219fa90e1c9SJiri Slaby struct tty_file_private *priv = file->private_data; 220fa90e1c9SJiri Slaby 221fa90e1c9SJiri Slaby file->private_data = NULL; 222fa90e1c9SJiri Slaby kfree(priv); 22396fd7ce5SGreg Kroah-Hartman } 22496fd7ce5SGreg Kroah-Hartman 22596fd7ce5SGreg Kroah-Hartman /* Delete file from its tty */ 2262520e274SJosh Triplett static void tty_del_file(struct file *file) 22796fd7ce5SGreg Kroah-Hartman { 22896fd7ce5SGreg Kroah-Hartman struct tty_file_private *priv = file->private_data; 2294a510969SPeter Hurley struct tty_struct *tty = priv->tty; 23096fd7ce5SGreg Kroah-Hartman 2314a510969SPeter Hurley spin_lock(&tty->files_lock); 23296fd7ce5SGreg Kroah-Hartman list_del(&priv->list); 2334a510969SPeter Hurley spin_unlock(&tty->files_lock); 234fa90e1c9SJiri Slaby tty_free_file(file); 23596fd7ce5SGreg Kroah-Hartman } 23696fd7ce5SGreg Kroah-Hartman 23796fd7ce5SGreg Kroah-Hartman /** 23896fd7ce5SGreg Kroah-Hartman * tty_name - return tty naming 23996fd7ce5SGreg Kroah-Hartman * @tty: tty structure 24096fd7ce5SGreg Kroah-Hartman * 24196fd7ce5SGreg Kroah-Hartman * Convert a tty structure into a name. The name reflects the kernel 24296fd7ce5SGreg Kroah-Hartman * naming policy and if udev is in use may not reflect user space 24396fd7ce5SGreg Kroah-Hartman * 24496fd7ce5SGreg Kroah-Hartman * Locking: none 24596fd7ce5SGreg Kroah-Hartman */ 24696fd7ce5SGreg Kroah-Hartman 247429b4749SRasmus Villemoes const char *tty_name(const struct tty_struct *tty) 24896fd7ce5SGreg Kroah-Hartman { 24996fd7ce5SGreg Kroah-Hartman if (!tty) /* Hmm. NULL pointer. That's fun. */ 250917162c9SRasmus Villemoes return "NULL tty"; 251917162c9SRasmus Villemoes return tty->name; 25296fd7ce5SGreg Kroah-Hartman } 25396fd7ce5SGreg Kroah-Hartman 25496fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_name); 25596fd7ce5SGreg Kroah-Hartman 2560a083eddSPeter Hurley const char *tty_driver_name(const struct tty_struct *tty) 2570a083eddSPeter Hurley { 2580a083eddSPeter Hurley if (!tty || !tty->driver) 2590a083eddSPeter Hurley return ""; 2600a083eddSPeter Hurley return tty->driver->name; 2610a083eddSPeter Hurley } 2620a083eddSPeter Hurley 26382b8f888SPeter Hurley static int tty_paranoia_check(struct tty_struct *tty, struct inode *inode, 26496fd7ce5SGreg Kroah-Hartman const char *routine) 26596fd7ce5SGreg Kroah-Hartman { 26696fd7ce5SGreg Kroah-Hartman #ifdef TTY_PARANOIA_CHECK 26796fd7ce5SGreg Kroah-Hartman if (!tty) { 26889222e62SPeter Hurley pr_warn("(%d:%d): %s: NULL tty\n", 26996fd7ce5SGreg Kroah-Hartman imajor(inode), iminor(inode), routine); 27096fd7ce5SGreg Kroah-Hartman return 1; 27196fd7ce5SGreg Kroah-Hartman } 27296fd7ce5SGreg Kroah-Hartman if (tty->magic != TTY_MAGIC) { 27389222e62SPeter Hurley pr_warn("(%d:%d): %s: bad magic number\n", 27496fd7ce5SGreg Kroah-Hartman imajor(inode), iminor(inode), routine); 27596fd7ce5SGreg Kroah-Hartman return 1; 27696fd7ce5SGreg Kroah-Hartman } 27796fd7ce5SGreg Kroah-Hartman #endif 27896fd7ce5SGreg Kroah-Hartman return 0; 27996fd7ce5SGreg Kroah-Hartman } 28096fd7ce5SGreg Kroah-Hartman 281deb287e7SPeter Hurley /* Caller must hold tty_lock */ 28296fd7ce5SGreg Kroah-Hartman static int check_tty_count(struct tty_struct *tty, const char *routine) 28396fd7ce5SGreg Kroah-Hartman { 28496fd7ce5SGreg Kroah-Hartman #ifdef CHECK_TTY_COUNT 28596fd7ce5SGreg Kroah-Hartman struct list_head *p; 286a09ac397SOkash Khawaja int count = 0, kopen_count = 0; 28796fd7ce5SGreg Kroah-Hartman 2884a510969SPeter Hurley spin_lock(&tty->files_lock); 28996fd7ce5SGreg Kroah-Hartman list_for_each(p, &tty->tty_files) { 29096fd7ce5SGreg Kroah-Hartman count++; 29196fd7ce5SGreg Kroah-Hartman } 2924a510969SPeter Hurley spin_unlock(&tty->files_lock); 29396fd7ce5SGreg Kroah-Hartman if (tty->driver->type == TTY_DRIVER_TYPE_PTY && 29496fd7ce5SGreg Kroah-Hartman tty->driver->subtype == PTY_TYPE_SLAVE && 29596fd7ce5SGreg Kroah-Hartman tty->link && tty->link->count) 29696fd7ce5SGreg Kroah-Hartman count++; 297a09ac397SOkash Khawaja if (tty_port_kopened(tty->port)) 298a09ac397SOkash Khawaja kopen_count++; 299a09ac397SOkash Khawaja if (tty->count != (count + kopen_count)) { 300a09ac397SOkash Khawaja tty_warn(tty, "%s: tty->count(%d) != (#fd's(%d) + #kopen's(%d))\n", 301a09ac397SOkash Khawaja routine, tty->count, count, kopen_count); 302a09ac397SOkash Khawaja return (count + kopen_count); 30396fd7ce5SGreg Kroah-Hartman } 30496fd7ce5SGreg Kroah-Hartman #endif 30596fd7ce5SGreg Kroah-Hartman return 0; 30696fd7ce5SGreg Kroah-Hartman } 30796fd7ce5SGreg Kroah-Hartman 30896fd7ce5SGreg Kroah-Hartman /** 30996fd7ce5SGreg Kroah-Hartman * get_tty_driver - find device of a tty 31096fd7ce5SGreg Kroah-Hartman * @dev_t: device identifier 31196fd7ce5SGreg Kroah-Hartman * @index: returns the index of the tty 31296fd7ce5SGreg Kroah-Hartman * 31396fd7ce5SGreg Kroah-Hartman * This routine returns a tty driver structure, given a device number 31496fd7ce5SGreg Kroah-Hartman * and also passes back the index number. 31596fd7ce5SGreg Kroah-Hartman * 31696fd7ce5SGreg Kroah-Hartman * Locking: caller must hold tty_mutex 31796fd7ce5SGreg Kroah-Hartman */ 31896fd7ce5SGreg Kroah-Hartman 31996fd7ce5SGreg Kroah-Hartman static struct tty_driver *get_tty_driver(dev_t device, int *index) 32096fd7ce5SGreg Kroah-Hartman { 32196fd7ce5SGreg Kroah-Hartman struct tty_driver *p; 32296fd7ce5SGreg Kroah-Hartman 32396fd7ce5SGreg Kroah-Hartman list_for_each_entry(p, &tty_drivers, tty_drivers) { 32496fd7ce5SGreg Kroah-Hartman dev_t base = MKDEV(p->major, p->minor_start); 32596fd7ce5SGreg Kroah-Hartman if (device < base || device >= base + p->num) 32696fd7ce5SGreg Kroah-Hartman continue; 32796fd7ce5SGreg Kroah-Hartman *index = device - base; 32896fd7ce5SGreg Kroah-Hartman return tty_driver_kref_get(p); 32996fd7ce5SGreg Kroah-Hartman } 33096fd7ce5SGreg Kroah-Hartman return NULL; 33196fd7ce5SGreg Kroah-Hartman } 33296fd7ce5SGreg Kroah-Hartman 333fc61ed51SOkash Khawaja /** 334fc61ed51SOkash Khawaja * tty_dev_name_to_number - return dev_t for device name 335fc61ed51SOkash Khawaja * @name: user space name of device under /dev 336fc61ed51SOkash Khawaja * @number: pointer to dev_t that this function will populate 337fc61ed51SOkash Khawaja * 338fc61ed51SOkash Khawaja * This function converts device names like ttyS0 or ttyUSB1 into dev_t 339fc61ed51SOkash Khawaja * like (4, 64) or (188, 1). If no corresponding driver is registered then 340fc61ed51SOkash Khawaja * the function returns -ENODEV. 341fc61ed51SOkash Khawaja * 342fc61ed51SOkash Khawaja * Locking: this acquires tty_mutex to protect the tty_drivers list from 343fc61ed51SOkash Khawaja * being modified while we are traversing it, and makes sure to 344fc61ed51SOkash Khawaja * release it before exiting. 345fc61ed51SOkash Khawaja */ 346fc61ed51SOkash Khawaja int tty_dev_name_to_number(const char *name, dev_t *number) 347fc61ed51SOkash Khawaja { 348fc61ed51SOkash Khawaja struct tty_driver *p; 349fc61ed51SOkash Khawaja int ret; 350fc61ed51SOkash Khawaja int index, prefix_length = 0; 351fc61ed51SOkash Khawaja const char *str; 352fc61ed51SOkash Khawaja 353fc61ed51SOkash Khawaja for (str = name; *str && !isdigit(*str); str++) 354fc61ed51SOkash Khawaja ; 355fc61ed51SOkash Khawaja 356fc61ed51SOkash Khawaja if (!*str) 357fc61ed51SOkash Khawaja return -EINVAL; 358fc61ed51SOkash Khawaja 359fc61ed51SOkash Khawaja ret = kstrtoint(str, 10, &index); 360fc61ed51SOkash Khawaja if (ret) 361fc61ed51SOkash Khawaja return ret; 362fc61ed51SOkash Khawaja 363fc61ed51SOkash Khawaja prefix_length = str - name; 364fc61ed51SOkash Khawaja mutex_lock(&tty_mutex); 365fc61ed51SOkash Khawaja 366fc61ed51SOkash Khawaja list_for_each_entry(p, &tty_drivers, tty_drivers) 367fc61ed51SOkash Khawaja if (prefix_length == strlen(p->name) && strncmp(name, 368fc61ed51SOkash Khawaja p->name, prefix_length) == 0) { 369fc61ed51SOkash Khawaja if (index < p->num) { 370fc61ed51SOkash Khawaja *number = MKDEV(p->major, p->minor_start + index); 371fc61ed51SOkash Khawaja goto out; 372fc61ed51SOkash Khawaja } 373fc61ed51SOkash Khawaja } 374fc61ed51SOkash Khawaja 375fc61ed51SOkash Khawaja /* if here then driver wasn't found */ 376fc61ed51SOkash Khawaja ret = -ENODEV; 377fc61ed51SOkash Khawaja out: 378fc61ed51SOkash Khawaja mutex_unlock(&tty_mutex); 379fc61ed51SOkash Khawaja return ret; 380fc61ed51SOkash Khawaja } 381fc61ed51SOkash Khawaja EXPORT_SYMBOL_GPL(tty_dev_name_to_number); 382fc61ed51SOkash Khawaja 38396fd7ce5SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL 38496fd7ce5SGreg Kroah-Hartman 38596fd7ce5SGreg Kroah-Hartman /** 38696fd7ce5SGreg Kroah-Hartman * tty_find_polling_driver - find device of a polled tty 38796fd7ce5SGreg Kroah-Hartman * @name: name string to match 38896fd7ce5SGreg Kroah-Hartman * @line: pointer to resulting tty line nr 38996fd7ce5SGreg Kroah-Hartman * 39096fd7ce5SGreg Kroah-Hartman * This routine returns a tty driver structure, given a name 39196fd7ce5SGreg Kroah-Hartman * and the condition that the tty driver is capable of polled 39296fd7ce5SGreg Kroah-Hartman * operation. 39396fd7ce5SGreg Kroah-Hartman */ 39496fd7ce5SGreg Kroah-Hartman struct tty_driver *tty_find_polling_driver(char *name, int *line) 39596fd7ce5SGreg Kroah-Hartman { 39696fd7ce5SGreg Kroah-Hartman struct tty_driver *p, *res = NULL; 39796fd7ce5SGreg Kroah-Hartman int tty_line = 0; 39896fd7ce5SGreg Kroah-Hartman int len; 39996fd7ce5SGreg Kroah-Hartman char *str, *stp; 40096fd7ce5SGreg Kroah-Hartman 40196fd7ce5SGreg Kroah-Hartman for (str = name; *str; str++) 40296fd7ce5SGreg Kroah-Hartman if ((*str >= '0' && *str <= '9') || *str == ',') 40396fd7ce5SGreg Kroah-Hartman break; 40496fd7ce5SGreg Kroah-Hartman if (!*str) 40596fd7ce5SGreg Kroah-Hartman return NULL; 40696fd7ce5SGreg Kroah-Hartman 40796fd7ce5SGreg Kroah-Hartman len = str - name; 40896fd7ce5SGreg Kroah-Hartman tty_line = simple_strtoul(str, &str, 10); 40996fd7ce5SGreg Kroah-Hartman 41096fd7ce5SGreg Kroah-Hartman mutex_lock(&tty_mutex); 41196fd7ce5SGreg Kroah-Hartman /* Search through the tty devices to look for a match */ 41296fd7ce5SGreg Kroah-Hartman list_for_each_entry(p, &tty_drivers, tty_drivers) { 41333a1a7beSMiles Chen if (!len || strncmp(name, p->name, len) != 0) 41496fd7ce5SGreg Kroah-Hartman continue; 41596fd7ce5SGreg Kroah-Hartman stp = str; 41696fd7ce5SGreg Kroah-Hartman if (*stp == ',') 41796fd7ce5SGreg Kroah-Hartman stp++; 41896fd7ce5SGreg Kroah-Hartman if (*stp == '\0') 41996fd7ce5SGreg Kroah-Hartman stp = NULL; 42096fd7ce5SGreg Kroah-Hartman 42196fd7ce5SGreg Kroah-Hartman if (tty_line >= 0 && tty_line < p->num && p->ops && 42296fd7ce5SGreg Kroah-Hartman p->ops->poll_init && !p->ops->poll_init(p, tty_line, stp)) { 42396fd7ce5SGreg Kroah-Hartman res = tty_driver_kref_get(p); 42496fd7ce5SGreg Kroah-Hartman *line = tty_line; 42596fd7ce5SGreg Kroah-Hartman break; 42696fd7ce5SGreg Kroah-Hartman } 42796fd7ce5SGreg Kroah-Hartman } 42896fd7ce5SGreg Kroah-Hartman mutex_unlock(&tty_mutex); 42996fd7ce5SGreg Kroah-Hartman 43096fd7ce5SGreg Kroah-Hartman return res; 43196fd7ce5SGreg Kroah-Hartman } 43296fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(tty_find_polling_driver); 43396fd7ce5SGreg Kroah-Hartman #endif 43496fd7ce5SGreg Kroah-Hartman 43596fd7ce5SGreg Kroah-Hartman static ssize_t hung_up_tty_read(struct file *file, char __user *buf, 43696fd7ce5SGreg Kroah-Hartman size_t count, loff_t *ppos) 43796fd7ce5SGreg Kroah-Hartman { 43896fd7ce5SGreg Kroah-Hartman return 0; 43996fd7ce5SGreg Kroah-Hartman } 44096fd7ce5SGreg Kroah-Hartman 44196fd7ce5SGreg Kroah-Hartman static ssize_t hung_up_tty_write(struct file *file, const char __user *buf, 44296fd7ce5SGreg Kroah-Hartman size_t count, loff_t *ppos) 44396fd7ce5SGreg Kroah-Hartman { 44496fd7ce5SGreg Kroah-Hartman return -EIO; 44596fd7ce5SGreg Kroah-Hartman } 44696fd7ce5SGreg Kroah-Hartman 44796fd7ce5SGreg Kroah-Hartman /* No kernel lock held - none needed ;) */ 448afc9a42bSAl Viro static __poll_t hung_up_tty_poll(struct file *filp, poll_table *wait) 44996fd7ce5SGreg Kroah-Hartman { 450a9a08845SLinus Torvalds return EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP | EPOLLRDNORM | EPOLLWRNORM; 45196fd7ce5SGreg Kroah-Hartman } 45296fd7ce5SGreg Kroah-Hartman 45396fd7ce5SGreg Kroah-Hartman static long hung_up_tty_ioctl(struct file *file, unsigned int cmd, 45496fd7ce5SGreg Kroah-Hartman unsigned long arg) 45596fd7ce5SGreg Kroah-Hartman { 45696fd7ce5SGreg Kroah-Hartman return cmd == TIOCSPGRP ? -ENOTTY : -EIO; 45796fd7ce5SGreg Kroah-Hartman } 45896fd7ce5SGreg Kroah-Hartman 45996fd7ce5SGreg Kroah-Hartman static long hung_up_tty_compat_ioctl(struct file *file, 46096fd7ce5SGreg Kroah-Hartman unsigned int cmd, unsigned long arg) 46196fd7ce5SGreg Kroah-Hartman { 46296fd7ce5SGreg Kroah-Hartman return cmd == TIOCSPGRP ? -ENOTTY : -EIO; 46396fd7ce5SGreg Kroah-Hartman } 46496fd7ce5SGreg Kroah-Hartman 465f557474cSPeter Hurley static int hung_up_tty_fasync(int fd, struct file *file, int on) 466f557474cSPeter Hurley { 467f557474cSPeter Hurley return -ENOTTY; 468f557474cSPeter Hurley } 469f557474cSPeter Hurley 470d01c3289SMasatake YAMATO static void tty_show_fdinfo(struct seq_file *m, struct file *file) 471d01c3289SMasatake YAMATO { 472d01c3289SMasatake YAMATO struct tty_struct *tty = file_tty(file); 473d01c3289SMasatake YAMATO 474d01c3289SMasatake YAMATO if (tty && tty->ops && tty->ops->show_fdinfo) 475d01c3289SMasatake YAMATO tty->ops->show_fdinfo(tty, m); 476d01c3289SMasatake YAMATO } 477d01c3289SMasatake YAMATO 47896fd7ce5SGreg Kroah-Hartman static const struct file_operations tty_fops = { 47996fd7ce5SGreg Kroah-Hartman .llseek = no_llseek, 48096fd7ce5SGreg Kroah-Hartman .read = tty_read, 48196fd7ce5SGreg Kroah-Hartman .write = tty_write, 48296fd7ce5SGreg Kroah-Hartman .poll = tty_poll, 48396fd7ce5SGreg Kroah-Hartman .unlocked_ioctl = tty_ioctl, 48496fd7ce5SGreg Kroah-Hartman .compat_ioctl = tty_compat_ioctl, 48596fd7ce5SGreg Kroah-Hartman .open = tty_open, 48696fd7ce5SGreg Kroah-Hartman .release = tty_release, 48796fd7ce5SGreg Kroah-Hartman .fasync = tty_fasync, 488d01c3289SMasatake YAMATO .show_fdinfo = tty_show_fdinfo, 48996fd7ce5SGreg Kroah-Hartman }; 49096fd7ce5SGreg Kroah-Hartman 49196fd7ce5SGreg Kroah-Hartman static const struct file_operations console_fops = { 49296fd7ce5SGreg Kroah-Hartman .llseek = no_llseek, 49396fd7ce5SGreg Kroah-Hartman .read = tty_read, 49496fd7ce5SGreg Kroah-Hartman .write = redirected_tty_write, 49596fd7ce5SGreg Kroah-Hartman .poll = tty_poll, 49696fd7ce5SGreg Kroah-Hartman .unlocked_ioctl = tty_ioctl, 49796fd7ce5SGreg Kroah-Hartman .compat_ioctl = tty_compat_ioctl, 49896fd7ce5SGreg Kroah-Hartman .open = tty_open, 49996fd7ce5SGreg Kroah-Hartman .release = tty_release, 50096fd7ce5SGreg Kroah-Hartman .fasync = tty_fasync, 50196fd7ce5SGreg Kroah-Hartman }; 50296fd7ce5SGreg Kroah-Hartman 50396fd7ce5SGreg Kroah-Hartman static const struct file_operations hung_up_tty_fops = { 50496fd7ce5SGreg Kroah-Hartman .llseek = no_llseek, 50596fd7ce5SGreg Kroah-Hartman .read = hung_up_tty_read, 50696fd7ce5SGreg Kroah-Hartman .write = hung_up_tty_write, 50796fd7ce5SGreg Kroah-Hartman .poll = hung_up_tty_poll, 50896fd7ce5SGreg Kroah-Hartman .unlocked_ioctl = hung_up_tty_ioctl, 50996fd7ce5SGreg Kroah-Hartman .compat_ioctl = hung_up_tty_compat_ioctl, 51096fd7ce5SGreg Kroah-Hartman .release = tty_release, 511f557474cSPeter Hurley .fasync = hung_up_tty_fasync, 51296fd7ce5SGreg Kroah-Hartman }; 51396fd7ce5SGreg Kroah-Hartman 51496fd7ce5SGreg Kroah-Hartman static DEFINE_SPINLOCK(redirect_lock); 51596fd7ce5SGreg Kroah-Hartman static struct file *redirect; 51696fd7ce5SGreg Kroah-Hartman 5177c0cca7cSGreg Kroah-Hartman extern void tty_sysctl_init(void); 5187c0cca7cSGreg Kroah-Hartman 51996fd7ce5SGreg Kroah-Hartman /** 52096fd7ce5SGreg Kroah-Hartman * tty_wakeup - request more data 52196fd7ce5SGreg Kroah-Hartman * @tty: terminal 52296fd7ce5SGreg Kroah-Hartman * 52396fd7ce5SGreg Kroah-Hartman * Internal and external helper for wakeups of tty. This function 52496fd7ce5SGreg Kroah-Hartman * informs the line discipline if present that the driver is ready 52596fd7ce5SGreg Kroah-Hartman * to receive more output data. 52696fd7ce5SGreg Kroah-Hartman */ 52796fd7ce5SGreg Kroah-Hartman 52896fd7ce5SGreg Kroah-Hartman void tty_wakeup(struct tty_struct *tty) 52996fd7ce5SGreg Kroah-Hartman { 53096fd7ce5SGreg Kroah-Hartman struct tty_ldisc *ld; 53196fd7ce5SGreg Kroah-Hartman 53296fd7ce5SGreg Kroah-Hartman if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) { 53396fd7ce5SGreg Kroah-Hartman ld = tty_ldisc_ref(tty); 53496fd7ce5SGreg Kroah-Hartman if (ld) { 53596fd7ce5SGreg Kroah-Hartman if (ld->ops->write_wakeup) 53696fd7ce5SGreg Kroah-Hartman ld->ops->write_wakeup(tty); 53796fd7ce5SGreg Kroah-Hartman tty_ldisc_deref(ld); 53896fd7ce5SGreg Kroah-Hartman } 53996fd7ce5SGreg Kroah-Hartman } 540a9a08845SLinus Torvalds wake_up_interruptible_poll(&tty->write_wait, EPOLLOUT); 54196fd7ce5SGreg Kroah-Hartman } 54296fd7ce5SGreg Kroah-Hartman 54396fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(tty_wakeup); 54496fd7ce5SGreg Kroah-Hartman 54596fd7ce5SGreg Kroah-Hartman /** 54696fd7ce5SGreg Kroah-Hartman * __tty_hangup - actual handler for hangup events 54796fd7ce5SGreg Kroah-Hartman * @work: tty device 54896fd7ce5SGreg Kroah-Hartman * 549ef4f527cSKevin Cernekee * This can be called by a "kworker" kernel thread. That is process 55096fd7ce5SGreg Kroah-Hartman * synchronous but doesn't hold any locks, so we need to make sure we 55196fd7ce5SGreg Kroah-Hartman * have the appropriate locks for what we're doing. 55296fd7ce5SGreg Kroah-Hartman * 55396fd7ce5SGreg Kroah-Hartman * The hangup event clears any pending redirections onto the hung up 55496fd7ce5SGreg Kroah-Hartman * device. It ensures future writes will error and it does the needed 55596fd7ce5SGreg Kroah-Hartman * line discipline hangup and signal delivery. The tty object itself 55696fd7ce5SGreg Kroah-Hartman * remains intact. 55796fd7ce5SGreg Kroah-Hartman * 55896fd7ce5SGreg Kroah-Hartman * Locking: 55996fd7ce5SGreg Kroah-Hartman * BTM 56096fd7ce5SGreg Kroah-Hartman * redirect lock for undoing redirection 56196fd7ce5SGreg Kroah-Hartman * file list lock for manipulating list of ttys 562137084bbSPeter Hurley * tty_ldiscs_lock from called functions 5636a1c0680SPeter Hurley * termios_rwsem resetting termios data 56496fd7ce5SGreg Kroah-Hartman * tasklist_lock to walk task list for hangup event 56596fd7ce5SGreg Kroah-Hartman * ->siglock to protect ->signal/->sighand 56696fd7ce5SGreg Kroah-Hartman */ 567f91e2590SPeter Hurley static void __tty_hangup(struct tty_struct *tty, int exit_session) 56896fd7ce5SGreg Kroah-Hartman { 56996fd7ce5SGreg Kroah-Hartman struct file *cons_filp = NULL; 57096fd7ce5SGreg Kroah-Hartman struct file *filp, *f = NULL; 57196fd7ce5SGreg Kroah-Hartman struct tty_file_private *priv; 57296fd7ce5SGreg Kroah-Hartman int closecount = 0, n; 573ea648a47SPeter Hurley int refs; 57496fd7ce5SGreg Kroah-Hartman 57596fd7ce5SGreg Kroah-Hartman if (!tty) 57696fd7ce5SGreg Kroah-Hartman return; 57796fd7ce5SGreg Kroah-Hartman 57896fd7ce5SGreg Kroah-Hartman 57996fd7ce5SGreg Kroah-Hartman spin_lock(&redirect_lock); 58096fd7ce5SGreg Kroah-Hartman if (redirect && file_tty(redirect) == tty) { 58196fd7ce5SGreg Kroah-Hartman f = redirect; 58296fd7ce5SGreg Kroah-Hartman redirect = NULL; 58396fd7ce5SGreg Kroah-Hartman } 58496fd7ce5SGreg Kroah-Hartman spin_unlock(&redirect_lock); 58596fd7ce5SGreg Kroah-Hartman 58689c8d91eSAlan Cox tty_lock(tty); 58796fd7ce5SGreg Kroah-Hartman 588cb50e523SPeter Hurley if (test_bit(TTY_HUPPED, &tty->flags)) { 589cb50e523SPeter Hurley tty_unlock(tty); 590cb50e523SPeter Hurley return; 591cb50e523SPeter Hurley } 592cb50e523SPeter Hurley 59328b0f8a6STejun Heo /* 59428b0f8a6STejun Heo * Some console devices aren't actually hung up for technical and 59528b0f8a6STejun Heo * historical reasons, which can lead to indefinite interruptible 59628b0f8a6STejun Heo * sleep in n_tty_read(). The following explicitly tells 59728b0f8a6STejun Heo * n_tty_read() to abort readers. 59828b0f8a6STejun Heo */ 59928b0f8a6STejun Heo set_bit(TTY_HUPPING, &tty->flags); 60028b0f8a6STejun Heo 60196fd7ce5SGreg Kroah-Hartman /* inuse_filps is protected by the single tty lock, 60296fd7ce5SGreg Kroah-Hartman this really needs to change if we want to flush the 60396fd7ce5SGreg Kroah-Hartman workqueue with the lock held */ 60496fd7ce5SGreg Kroah-Hartman check_tty_count(tty, "tty_hangup"); 60596fd7ce5SGreg Kroah-Hartman 6064a510969SPeter Hurley spin_lock(&tty->files_lock); 60796fd7ce5SGreg Kroah-Hartman /* This breaks for file handles being sent over AF_UNIX sockets ? */ 60896fd7ce5SGreg Kroah-Hartman list_for_each_entry(priv, &tty->tty_files, list) { 60996fd7ce5SGreg Kroah-Hartman filp = priv->file; 61096fd7ce5SGreg Kroah-Hartman if (filp->f_op->write == redirected_tty_write) 61196fd7ce5SGreg Kroah-Hartman cons_filp = filp; 61296fd7ce5SGreg Kroah-Hartman if (filp->f_op->write != tty_write) 61396fd7ce5SGreg Kroah-Hartman continue; 61496fd7ce5SGreg Kroah-Hartman closecount++; 61596fd7ce5SGreg Kroah-Hartman __tty_fasync(-1, filp, 0); /* can't block */ 61696fd7ce5SGreg Kroah-Hartman filp->f_op = &hung_up_tty_fops; 61796fd7ce5SGreg Kroah-Hartman } 6184a510969SPeter Hurley spin_unlock(&tty->files_lock); 61996fd7ce5SGreg Kroah-Hartman 62025fdf243SPeter Hurley refs = tty_signal_session_leader(tty, exit_session); 62125fdf243SPeter Hurley /* Account for the p->signal references we killed */ 62225fdf243SPeter Hurley while (refs--) 62325fdf243SPeter Hurley tty_kref_put(tty); 62425fdf243SPeter Hurley 625892d1fa7SPeter Hurley tty_ldisc_hangup(tty, cons_filp != NULL); 62696fd7ce5SGreg Kroah-Hartman 62720cc225bSPeter Hurley spin_lock_irq(&tty->ctrl_lock); 62896fd7ce5SGreg Kroah-Hartman clear_bit(TTY_THROTTLED, &tty->flags); 62996fd7ce5SGreg Kroah-Hartman clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); 63096fd7ce5SGreg Kroah-Hartman put_pid(tty->session); 63196fd7ce5SGreg Kroah-Hartman put_pid(tty->pgrp); 63296fd7ce5SGreg Kroah-Hartman tty->session = NULL; 63396fd7ce5SGreg Kroah-Hartman tty->pgrp = NULL; 63496fd7ce5SGreg Kroah-Hartman tty->ctrl_status = 0; 63520cc225bSPeter Hurley spin_unlock_irq(&tty->ctrl_lock); 63696fd7ce5SGreg Kroah-Hartman 63796fd7ce5SGreg Kroah-Hartman /* 63896fd7ce5SGreg Kroah-Hartman * If one of the devices matches a console pointer, we 63996fd7ce5SGreg Kroah-Hartman * cannot just call hangup() because that will cause 64096fd7ce5SGreg Kroah-Hartman * tty->count and state->count to go out of sync. 64196fd7ce5SGreg Kroah-Hartman * So we just call close() the right number of times. 64296fd7ce5SGreg Kroah-Hartman */ 64396fd7ce5SGreg Kroah-Hartman if (cons_filp) { 64496fd7ce5SGreg Kroah-Hartman if (tty->ops->close) 64596fd7ce5SGreg Kroah-Hartman for (n = 0; n < closecount; n++) 64696fd7ce5SGreg Kroah-Hartman tty->ops->close(tty, cons_filp); 64796fd7ce5SGreg Kroah-Hartman } else if (tty->ops->hangup) 6487c6d340fSPeter Hurley tty->ops->hangup(tty); 64996fd7ce5SGreg Kroah-Hartman /* 650892d1fa7SPeter Hurley * We don't want to have driver/ldisc interactions beyond the ones 651892d1fa7SPeter Hurley * we did here. The driver layer expects no calls after ->hangup() 652892d1fa7SPeter Hurley * from the ldisc side, which is now guaranteed. 65396fd7ce5SGreg Kroah-Hartman */ 65496fd7ce5SGreg Kroah-Hartman set_bit(TTY_HUPPED, &tty->flags); 65528b0f8a6STejun Heo clear_bit(TTY_HUPPING, &tty->flags); 65689c8d91eSAlan Cox tty_unlock(tty); 65796fd7ce5SGreg Kroah-Hartman 65896fd7ce5SGreg Kroah-Hartman if (f) 65996fd7ce5SGreg Kroah-Hartman fput(f); 66096fd7ce5SGreg Kroah-Hartman } 66196fd7ce5SGreg Kroah-Hartman 66296fd7ce5SGreg Kroah-Hartman static void do_tty_hangup(struct work_struct *work) 66396fd7ce5SGreg Kroah-Hartman { 66496fd7ce5SGreg Kroah-Hartman struct tty_struct *tty = 66596fd7ce5SGreg Kroah-Hartman container_of(work, struct tty_struct, hangup_work); 66696fd7ce5SGreg Kroah-Hartman 667f91e2590SPeter Hurley __tty_hangup(tty, 0); 66896fd7ce5SGreg Kroah-Hartman } 66996fd7ce5SGreg Kroah-Hartman 67096fd7ce5SGreg Kroah-Hartman /** 67196fd7ce5SGreg Kroah-Hartman * tty_hangup - trigger a hangup event 67296fd7ce5SGreg Kroah-Hartman * @tty: tty to hangup 67396fd7ce5SGreg Kroah-Hartman * 67496fd7ce5SGreg Kroah-Hartman * A carrier loss (virtual or otherwise) has occurred on this like 67596fd7ce5SGreg Kroah-Hartman * schedule a hangup sequence to run after this event. 67696fd7ce5SGreg Kroah-Hartman */ 67796fd7ce5SGreg Kroah-Hartman 67896fd7ce5SGreg Kroah-Hartman void tty_hangup(struct tty_struct *tty) 67996fd7ce5SGreg Kroah-Hartman { 680d435cefeSPeter Hurley tty_debug_hangup(tty, "hangup\n"); 68196fd7ce5SGreg Kroah-Hartman schedule_work(&tty->hangup_work); 68296fd7ce5SGreg Kroah-Hartman } 68396fd7ce5SGreg Kroah-Hartman 68496fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_hangup); 68596fd7ce5SGreg Kroah-Hartman 68696fd7ce5SGreg Kroah-Hartman /** 68796fd7ce5SGreg Kroah-Hartman * tty_vhangup - process vhangup 68896fd7ce5SGreg Kroah-Hartman * @tty: tty to hangup 68996fd7ce5SGreg Kroah-Hartman * 69096fd7ce5SGreg Kroah-Hartman * The user has asked via system call for the terminal to be hung up. 69196fd7ce5SGreg Kroah-Hartman * We do this synchronously so that when the syscall returns the process 69296fd7ce5SGreg Kroah-Hartman * is complete. That guarantee is necessary for security reasons. 69396fd7ce5SGreg Kroah-Hartman */ 69496fd7ce5SGreg Kroah-Hartman 69596fd7ce5SGreg Kroah-Hartman void tty_vhangup(struct tty_struct *tty) 69696fd7ce5SGreg Kroah-Hartman { 697d435cefeSPeter Hurley tty_debug_hangup(tty, "vhangup\n"); 698f91e2590SPeter Hurley __tty_hangup(tty, 0); 69996fd7ce5SGreg Kroah-Hartman } 70096fd7ce5SGreg Kroah-Hartman 70196fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_vhangup); 70296fd7ce5SGreg Kroah-Hartman 70396fd7ce5SGreg Kroah-Hartman 70496fd7ce5SGreg Kroah-Hartman /** 70596fd7ce5SGreg Kroah-Hartman * tty_vhangup_self - process vhangup for own ctty 70696fd7ce5SGreg Kroah-Hartman * 70796fd7ce5SGreg Kroah-Hartman * Perform a vhangup on the current controlling tty 70896fd7ce5SGreg Kroah-Hartman */ 70996fd7ce5SGreg Kroah-Hartman 71096fd7ce5SGreg Kroah-Hartman void tty_vhangup_self(void) 71196fd7ce5SGreg Kroah-Hartman { 71296fd7ce5SGreg Kroah-Hartman struct tty_struct *tty; 71396fd7ce5SGreg Kroah-Hartman 71496fd7ce5SGreg Kroah-Hartman tty = get_current_tty(); 71596fd7ce5SGreg Kroah-Hartman if (tty) { 71696fd7ce5SGreg Kroah-Hartman tty_vhangup(tty); 71796fd7ce5SGreg Kroah-Hartman tty_kref_put(tty); 71896fd7ce5SGreg Kroah-Hartman } 71996fd7ce5SGreg Kroah-Hartman } 72096fd7ce5SGreg Kroah-Hartman 72196fd7ce5SGreg Kroah-Hartman /** 722f91e2590SPeter Hurley * tty_vhangup_session - hangup session leader exit 723f91e2590SPeter Hurley * @tty: tty to hangup 724f91e2590SPeter Hurley * 725f91e2590SPeter Hurley * The session leader is exiting and hanging up its controlling terminal. 726f91e2590SPeter Hurley * Every process in the foreground process group is signalled SIGHUP. 727f91e2590SPeter Hurley * 728f91e2590SPeter Hurley * We do this synchronously so that when the syscall returns the process 729f91e2590SPeter Hurley * is complete. That guarantee is necessary for security reasons. 730f91e2590SPeter Hurley */ 731f91e2590SPeter Hurley 732a1235b3eSNicolas Pitre void tty_vhangup_session(struct tty_struct *tty) 733f91e2590SPeter Hurley { 734d435cefeSPeter Hurley tty_debug_hangup(tty, "session hangup\n"); 735f91e2590SPeter Hurley __tty_hangup(tty, 1); 736f91e2590SPeter Hurley } 737f91e2590SPeter Hurley 738f91e2590SPeter Hurley /** 73996fd7ce5SGreg Kroah-Hartman * tty_hung_up_p - was tty hung up 74096fd7ce5SGreg Kroah-Hartman * @filp: file pointer of tty 74196fd7ce5SGreg Kroah-Hartman * 74296fd7ce5SGreg Kroah-Hartman * Return true if the tty has been subject to a vhangup or a carrier 74396fd7ce5SGreg Kroah-Hartman * loss 74496fd7ce5SGreg Kroah-Hartman */ 74596fd7ce5SGreg Kroah-Hartman 74696fd7ce5SGreg Kroah-Hartman int tty_hung_up_p(struct file *filp) 74796fd7ce5SGreg Kroah-Hartman { 748ed3f0af8SAlan Cox return (filp && filp->f_op == &hung_up_tty_fops); 74996fd7ce5SGreg Kroah-Hartman } 75096fd7ce5SGreg Kroah-Hartman 75196fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_hung_up_p); 75296fd7ce5SGreg Kroah-Hartman 75396fd7ce5SGreg Kroah-Hartman /** 75496fd7ce5SGreg Kroah-Hartman * stop_tty - propagate flow control 75596fd7ce5SGreg Kroah-Hartman * @tty: tty to stop 75696fd7ce5SGreg Kroah-Hartman * 75701adc807SPeter Hurley * Perform flow control to the driver. May be called 75896fd7ce5SGreg Kroah-Hartman * on an already stopped device and will not re-call the driver 75996fd7ce5SGreg Kroah-Hartman * method. 76096fd7ce5SGreg Kroah-Hartman * 76196fd7ce5SGreg Kroah-Hartman * This functionality is used by both the line disciplines for 76296fd7ce5SGreg Kroah-Hartman * halting incoming flow and by the driver. It may therefore be 76396fd7ce5SGreg Kroah-Hartman * called from any context, may be under the tty atomic_write_lock 76496fd7ce5SGreg Kroah-Hartman * but not always. 76596fd7ce5SGreg Kroah-Hartman * 76696fd7ce5SGreg Kroah-Hartman * Locking: 767f9e053dcSPeter Hurley * flow_lock 76896fd7ce5SGreg Kroah-Hartman */ 76996fd7ce5SGreg Kroah-Hartman 770f9e053dcSPeter Hurley void __stop_tty(struct tty_struct *tty) 77196fd7ce5SGreg Kroah-Hartman { 772f9e053dcSPeter Hurley if (tty->stopped) 77396fd7ce5SGreg Kroah-Hartman return; 77496fd7ce5SGreg Kroah-Hartman tty->stopped = 1; 77596fd7ce5SGreg Kroah-Hartman if (tty->ops->stop) 776c961bfb1SPeter Hurley tty->ops->stop(tty); 77796fd7ce5SGreg Kroah-Hartman } 77896fd7ce5SGreg Kroah-Hartman 779f9e053dcSPeter Hurley void stop_tty(struct tty_struct *tty) 780f9e053dcSPeter Hurley { 781f9e053dcSPeter Hurley unsigned long flags; 782f9e053dcSPeter Hurley 783f9e053dcSPeter Hurley spin_lock_irqsave(&tty->flow_lock, flags); 784f9e053dcSPeter Hurley __stop_tty(tty); 785f9e053dcSPeter Hurley spin_unlock_irqrestore(&tty->flow_lock, flags); 786f9e053dcSPeter Hurley } 78796fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(stop_tty); 78896fd7ce5SGreg Kroah-Hartman 78996fd7ce5SGreg Kroah-Hartman /** 79096fd7ce5SGreg Kroah-Hartman * start_tty - propagate flow control 79196fd7ce5SGreg Kroah-Hartman * @tty: tty to start 79296fd7ce5SGreg Kroah-Hartman * 79301adc807SPeter Hurley * Start a tty that has been stopped if at all possible. If this 79401adc807SPeter Hurley * tty was previous stopped and is now being started, the driver 79501adc807SPeter Hurley * start method is invoked and the line discipline woken. 79696fd7ce5SGreg Kroah-Hartman * 79796fd7ce5SGreg Kroah-Hartman * Locking: 798f9e053dcSPeter Hurley * flow_lock 79996fd7ce5SGreg Kroah-Hartman */ 80096fd7ce5SGreg Kroah-Hartman 801f9e053dcSPeter Hurley void __start_tty(struct tty_struct *tty) 80296fd7ce5SGreg Kroah-Hartman { 803f9e053dcSPeter Hurley if (!tty->stopped || tty->flow_stopped) 80496fd7ce5SGreg Kroah-Hartman return; 80596fd7ce5SGreg Kroah-Hartman tty->stopped = 0; 80696fd7ce5SGreg Kroah-Hartman if (tty->ops->start) 807c961bfb1SPeter Hurley tty->ops->start(tty); 80896fd7ce5SGreg Kroah-Hartman tty_wakeup(tty); 80996fd7ce5SGreg Kroah-Hartman } 81096fd7ce5SGreg Kroah-Hartman 811f9e053dcSPeter Hurley void start_tty(struct tty_struct *tty) 812f9e053dcSPeter Hurley { 813f9e053dcSPeter Hurley unsigned long flags; 814f9e053dcSPeter Hurley 815f9e053dcSPeter Hurley spin_lock_irqsave(&tty->flow_lock, flags); 816f9e053dcSPeter Hurley __start_tty(tty); 817f9e053dcSPeter Hurley spin_unlock_irqrestore(&tty->flow_lock, flags); 818f9e053dcSPeter Hurley } 81996fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(start_tty); 82096fd7ce5SGreg Kroah-Hartman 821c884f871SArnd Bergmann static void tty_update_time(struct timespec64 *time) 82237b7f3c7SJiri Slaby { 823c884f871SArnd Bergmann time64_t sec = ktime_get_real_seconds(); 824fbf47635SGreg Kroah-Hartman 825fbf47635SGreg Kroah-Hartman /* 826fbf47635SGreg Kroah-Hartman * We only care if the two values differ in anything other than the 827fbf47635SGreg Kroah-Hartman * lower three bits (i.e every 8 seconds). If so, then we can update 828fbf47635SGreg Kroah-Hartman * the time of the tty device, otherwise it could be construded as a 829fbf47635SGreg Kroah-Hartman * security leak to let userspace know the exact timing of the tty. 830fbf47635SGreg Kroah-Hartman */ 831fbf47635SGreg Kroah-Hartman if ((sec ^ time->tv_sec) & ~7) 83237b7f3c7SJiri Slaby time->tv_sec = sec; 83337b7f3c7SJiri Slaby } 83437b7f3c7SJiri Slaby 83596fd7ce5SGreg Kroah-Hartman /** 83696fd7ce5SGreg Kroah-Hartman * tty_read - read method for tty device files 83796fd7ce5SGreg Kroah-Hartman * @file: pointer to tty file 83896fd7ce5SGreg Kroah-Hartman * @buf: user buffer 83996fd7ce5SGreg Kroah-Hartman * @count: size of user buffer 84096fd7ce5SGreg Kroah-Hartman * @ppos: unused 84196fd7ce5SGreg Kroah-Hartman * 84296fd7ce5SGreg Kroah-Hartman * Perform the read system call function on this terminal device. Checks 84396fd7ce5SGreg Kroah-Hartman * for hung up devices before calling the line discipline method. 84496fd7ce5SGreg Kroah-Hartman * 84596fd7ce5SGreg Kroah-Hartman * Locking: 84696fd7ce5SGreg Kroah-Hartman * Locks the line discipline internally while needed. Multiple 84796fd7ce5SGreg Kroah-Hartman * read calls may be outstanding in parallel. 84896fd7ce5SGreg Kroah-Hartman */ 84996fd7ce5SGreg Kroah-Hartman 85096fd7ce5SGreg Kroah-Hartman static ssize_t tty_read(struct file *file, char __user *buf, size_t count, 85196fd7ce5SGreg Kroah-Hartman loff_t *ppos) 85296fd7ce5SGreg Kroah-Hartman { 85396fd7ce5SGreg Kroah-Hartman int i; 85437b7f3c7SJiri Slaby struct inode *inode = file_inode(file); 85596fd7ce5SGreg Kroah-Hartman struct tty_struct *tty = file_tty(file); 85696fd7ce5SGreg Kroah-Hartman struct tty_ldisc *ld; 85796fd7ce5SGreg Kroah-Hartman 85837b7f3c7SJiri Slaby if (tty_paranoia_check(tty, inode, "tty_read")) 85996fd7ce5SGreg Kroah-Hartman return -EIO; 86018900ca6SPeter Hurley if (!tty || tty_io_error(tty)) 86196fd7ce5SGreg Kroah-Hartman return -EIO; 86296fd7ce5SGreg Kroah-Hartman 86396fd7ce5SGreg Kroah-Hartman /* We want to wait for the line discipline to sort out in this 86496fd7ce5SGreg Kroah-Hartman situation */ 86596fd7ce5SGreg Kroah-Hartman ld = tty_ldisc_ref_wait(tty); 866e55afd11SPeter Hurley if (!ld) 867e55afd11SPeter Hurley return hung_up_tty_read(file, buf, count, ppos); 86896fd7ce5SGreg Kroah-Hartman if (ld->ops->read) 869c961bfb1SPeter Hurley i = ld->ops->read(tty, file, buf, count); 87096fd7ce5SGreg Kroah-Hartman else 87196fd7ce5SGreg Kroah-Hartman i = -EIO; 87296fd7ce5SGreg Kroah-Hartman tty_ldisc_deref(ld); 873b0de59b5SJiri Slaby 874c884f871SArnd Bergmann if (i > 0) 875c884f871SArnd Bergmann tty_update_time(&inode->i_atime); 87637b7f3c7SJiri Slaby 87796fd7ce5SGreg Kroah-Hartman return i; 87896fd7ce5SGreg Kroah-Hartman } 87996fd7ce5SGreg Kroah-Hartman 880136d5258SPeter Hurley static void tty_write_unlock(struct tty_struct *tty) 88196fd7ce5SGreg Kroah-Hartman { 88296fd7ce5SGreg Kroah-Hartman mutex_unlock(&tty->atomic_write_lock); 883a9a08845SLinus Torvalds wake_up_interruptible_poll(&tty->write_wait, EPOLLOUT); 88496fd7ce5SGreg Kroah-Hartman } 88596fd7ce5SGreg Kroah-Hartman 886136d5258SPeter Hurley static int tty_write_lock(struct tty_struct *tty, int ndelay) 88796fd7ce5SGreg Kroah-Hartman { 88896fd7ce5SGreg Kroah-Hartman if (!mutex_trylock(&tty->atomic_write_lock)) { 88996fd7ce5SGreg Kroah-Hartman if (ndelay) 89096fd7ce5SGreg Kroah-Hartman return -EAGAIN; 89196fd7ce5SGreg Kroah-Hartman if (mutex_lock_interruptible(&tty->atomic_write_lock)) 89296fd7ce5SGreg Kroah-Hartman return -ERESTARTSYS; 89396fd7ce5SGreg Kroah-Hartman } 89496fd7ce5SGreg Kroah-Hartman return 0; 89596fd7ce5SGreg Kroah-Hartman } 89696fd7ce5SGreg Kroah-Hartman 89796fd7ce5SGreg Kroah-Hartman /* 89896fd7ce5SGreg Kroah-Hartman * Split writes up in sane blocksizes to avoid 89996fd7ce5SGreg Kroah-Hartman * denial-of-service type attacks 90096fd7ce5SGreg Kroah-Hartman */ 90196fd7ce5SGreg Kroah-Hartman static inline ssize_t do_tty_write( 90296fd7ce5SGreg Kroah-Hartman ssize_t (*write)(struct tty_struct *, struct file *, const unsigned char *, size_t), 90396fd7ce5SGreg Kroah-Hartman struct tty_struct *tty, 90496fd7ce5SGreg Kroah-Hartman struct file *file, 90596fd7ce5SGreg Kroah-Hartman const char __user *buf, 90696fd7ce5SGreg Kroah-Hartman size_t count) 90796fd7ce5SGreg Kroah-Hartman { 90896fd7ce5SGreg Kroah-Hartman ssize_t ret, written = 0; 90996fd7ce5SGreg Kroah-Hartman unsigned int chunk; 91096fd7ce5SGreg Kroah-Hartman 91196fd7ce5SGreg Kroah-Hartman ret = tty_write_lock(tty, file->f_flags & O_NDELAY); 91296fd7ce5SGreg Kroah-Hartman if (ret < 0) 91396fd7ce5SGreg Kroah-Hartman return ret; 91496fd7ce5SGreg Kroah-Hartman 91596fd7ce5SGreg Kroah-Hartman /* 91696fd7ce5SGreg Kroah-Hartman * We chunk up writes into a temporary buffer. This 91796fd7ce5SGreg Kroah-Hartman * simplifies low-level drivers immensely, since they 91896fd7ce5SGreg Kroah-Hartman * don't have locking issues and user mode accesses. 91996fd7ce5SGreg Kroah-Hartman * 92096fd7ce5SGreg Kroah-Hartman * But if TTY_NO_WRITE_SPLIT is set, we should use a 92196fd7ce5SGreg Kroah-Hartman * big chunk-size.. 92296fd7ce5SGreg Kroah-Hartman * 92396fd7ce5SGreg Kroah-Hartman * The default chunk-size is 2kB, because the NTTY 92496fd7ce5SGreg Kroah-Hartman * layer has problems with bigger chunks. It will 92596fd7ce5SGreg Kroah-Hartman * claim to be able to handle more characters than 92696fd7ce5SGreg Kroah-Hartman * it actually does. 92796fd7ce5SGreg Kroah-Hartman * 92896fd7ce5SGreg Kroah-Hartman * FIXME: This can probably go away now except that 64K chunks 92996fd7ce5SGreg Kroah-Hartman * are too likely to fail unless switched to vmalloc... 93096fd7ce5SGreg Kroah-Hartman */ 93196fd7ce5SGreg Kroah-Hartman chunk = 2048; 93296fd7ce5SGreg Kroah-Hartman if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags)) 93396fd7ce5SGreg Kroah-Hartman chunk = 65536; 93496fd7ce5SGreg Kroah-Hartman if (count < chunk) 93596fd7ce5SGreg Kroah-Hartman chunk = count; 93696fd7ce5SGreg Kroah-Hartman 93796fd7ce5SGreg Kroah-Hartman /* write_buf/write_cnt is protected by the atomic_write_lock mutex */ 93896fd7ce5SGreg Kroah-Hartman if (tty->write_cnt < chunk) { 93996fd7ce5SGreg Kroah-Hartman unsigned char *buf_chunk; 94096fd7ce5SGreg Kroah-Hartman 94196fd7ce5SGreg Kroah-Hartman if (chunk < 1024) 94296fd7ce5SGreg Kroah-Hartman chunk = 1024; 94396fd7ce5SGreg Kroah-Hartman 94496fd7ce5SGreg Kroah-Hartman buf_chunk = kmalloc(chunk, GFP_KERNEL); 94596fd7ce5SGreg Kroah-Hartman if (!buf_chunk) { 94696fd7ce5SGreg Kroah-Hartman ret = -ENOMEM; 94796fd7ce5SGreg Kroah-Hartman goto out; 94896fd7ce5SGreg Kroah-Hartman } 94996fd7ce5SGreg Kroah-Hartman kfree(tty->write_buf); 95096fd7ce5SGreg Kroah-Hartman tty->write_cnt = chunk; 95196fd7ce5SGreg Kroah-Hartman tty->write_buf = buf_chunk; 95296fd7ce5SGreg Kroah-Hartman } 95396fd7ce5SGreg Kroah-Hartman 95496fd7ce5SGreg Kroah-Hartman /* Do the write .. */ 95596fd7ce5SGreg Kroah-Hartman for (;;) { 95696fd7ce5SGreg Kroah-Hartman size_t size = count; 95796fd7ce5SGreg Kroah-Hartman if (size > chunk) 95896fd7ce5SGreg Kroah-Hartman size = chunk; 95996fd7ce5SGreg Kroah-Hartman ret = -EFAULT; 96096fd7ce5SGreg Kroah-Hartman if (copy_from_user(tty->write_buf, buf, size)) 96196fd7ce5SGreg Kroah-Hartman break; 96296fd7ce5SGreg Kroah-Hartman ret = write(tty, file, tty->write_buf, size); 96396fd7ce5SGreg Kroah-Hartman if (ret <= 0) 96496fd7ce5SGreg Kroah-Hartman break; 96596fd7ce5SGreg Kroah-Hartman written += ret; 96696fd7ce5SGreg Kroah-Hartman buf += ret; 96796fd7ce5SGreg Kroah-Hartman count -= ret; 96896fd7ce5SGreg Kroah-Hartman if (!count) 96996fd7ce5SGreg Kroah-Hartman break; 97096fd7ce5SGreg Kroah-Hartman ret = -ERESTARTSYS; 97196fd7ce5SGreg Kroah-Hartman if (signal_pending(current)) 97296fd7ce5SGreg Kroah-Hartman break; 97396fd7ce5SGreg Kroah-Hartman cond_resched(); 97496fd7ce5SGreg Kroah-Hartman } 97537b7f3c7SJiri Slaby if (written) { 976c884f871SArnd Bergmann tty_update_time(&file_inode(file)->i_mtime); 97796fd7ce5SGreg Kroah-Hartman ret = written; 97837b7f3c7SJiri Slaby } 97996fd7ce5SGreg Kroah-Hartman out: 98096fd7ce5SGreg Kroah-Hartman tty_write_unlock(tty); 98196fd7ce5SGreg Kroah-Hartman return ret; 98296fd7ce5SGreg Kroah-Hartman } 98396fd7ce5SGreg Kroah-Hartman 98496fd7ce5SGreg Kroah-Hartman /** 98596fd7ce5SGreg Kroah-Hartman * tty_write_message - write a message to a certain tty, not just the console. 98696fd7ce5SGreg Kroah-Hartman * @tty: the destination tty_struct 98796fd7ce5SGreg Kroah-Hartman * @msg: the message to write 98896fd7ce5SGreg Kroah-Hartman * 98996fd7ce5SGreg Kroah-Hartman * This is used for messages that need to be redirected to a specific tty. 99096fd7ce5SGreg Kroah-Hartman * We don't put it into the syslog queue right now maybe in the future if 99196fd7ce5SGreg Kroah-Hartman * really needed. 99296fd7ce5SGreg Kroah-Hartman * 99396fd7ce5SGreg Kroah-Hartman * We must still hold the BTM and test the CLOSING flag for the moment. 99496fd7ce5SGreg Kroah-Hartman */ 99596fd7ce5SGreg Kroah-Hartman 99696fd7ce5SGreg Kroah-Hartman void tty_write_message(struct tty_struct *tty, char *msg) 99796fd7ce5SGreg Kroah-Hartman { 99896fd7ce5SGreg Kroah-Hartman if (tty) { 99996fd7ce5SGreg Kroah-Hartman mutex_lock(&tty->atomic_write_lock); 100089c8d91eSAlan Cox tty_lock(tty); 10014b41b953SPeter Hurley if (tty->ops->write && tty->count > 0) 100296fd7ce5SGreg Kroah-Hartman tty->ops->write(tty, msg, strlen(msg)); 100389c8d91eSAlan Cox tty_unlock(tty); 100496fd7ce5SGreg Kroah-Hartman tty_write_unlock(tty); 100596fd7ce5SGreg Kroah-Hartman } 100696fd7ce5SGreg Kroah-Hartman return; 100796fd7ce5SGreg Kroah-Hartman } 100896fd7ce5SGreg Kroah-Hartman 100996fd7ce5SGreg Kroah-Hartman 101096fd7ce5SGreg Kroah-Hartman /** 101196fd7ce5SGreg Kroah-Hartman * tty_write - write method for tty device file 101296fd7ce5SGreg Kroah-Hartman * @file: tty file pointer 101396fd7ce5SGreg Kroah-Hartman * @buf: user data to write 101496fd7ce5SGreg Kroah-Hartman * @count: bytes to write 101596fd7ce5SGreg Kroah-Hartman * @ppos: unused 101696fd7ce5SGreg Kroah-Hartman * 101796fd7ce5SGreg Kroah-Hartman * Write data to a tty device via the line discipline. 101896fd7ce5SGreg Kroah-Hartman * 101996fd7ce5SGreg Kroah-Hartman * Locking: 102096fd7ce5SGreg Kroah-Hartman * Locks the line discipline as required 102196fd7ce5SGreg Kroah-Hartman * Writes to the tty driver are serialized by the atomic_write_lock 102296fd7ce5SGreg Kroah-Hartman * and are then processed in chunks to the device. The line discipline 102396fd7ce5SGreg Kroah-Hartman * write method will not be invoked in parallel for each device. 102496fd7ce5SGreg Kroah-Hartman */ 102596fd7ce5SGreg Kroah-Hartman 102696fd7ce5SGreg Kroah-Hartman static ssize_t tty_write(struct file *file, const char __user *buf, 102796fd7ce5SGreg Kroah-Hartman size_t count, loff_t *ppos) 102896fd7ce5SGreg Kroah-Hartman { 102996fd7ce5SGreg Kroah-Hartman struct tty_struct *tty = file_tty(file); 103096fd7ce5SGreg Kroah-Hartman struct tty_ldisc *ld; 103196fd7ce5SGreg Kroah-Hartman ssize_t ret; 103296fd7ce5SGreg Kroah-Hartman 10336131ffaaSAl Viro if (tty_paranoia_check(tty, file_inode(file), "tty_write")) 103496fd7ce5SGreg Kroah-Hartman return -EIO; 103518900ca6SPeter Hurley if (!tty || !tty->ops->write || tty_io_error(tty)) 103696fd7ce5SGreg Kroah-Hartman return -EIO; 103796fd7ce5SGreg Kroah-Hartman /* Short term debug to catch buggy drivers */ 103896fd7ce5SGreg Kroah-Hartman if (tty->ops->write_room == NULL) 1039339f36baSPeter Hurley tty_err(tty, "missing write_room method\n"); 104096fd7ce5SGreg Kroah-Hartman ld = tty_ldisc_ref_wait(tty); 1041e55afd11SPeter Hurley if (!ld) 1042e55afd11SPeter Hurley return hung_up_tty_write(file, buf, count, ppos); 104396fd7ce5SGreg Kroah-Hartman if (!ld->ops->write) 104496fd7ce5SGreg Kroah-Hartman ret = -EIO; 104596fd7ce5SGreg Kroah-Hartman else 104696fd7ce5SGreg Kroah-Hartman ret = do_tty_write(ld->ops->write, tty, file, buf, count); 104796fd7ce5SGreg Kroah-Hartman tty_ldisc_deref(ld); 104896fd7ce5SGreg Kroah-Hartman return ret; 104996fd7ce5SGreg Kroah-Hartman } 105096fd7ce5SGreg Kroah-Hartman 105196fd7ce5SGreg Kroah-Hartman ssize_t redirected_tty_write(struct file *file, const char __user *buf, 105296fd7ce5SGreg Kroah-Hartman size_t count, loff_t *ppos) 105396fd7ce5SGreg Kroah-Hartman { 105496fd7ce5SGreg Kroah-Hartman struct file *p = NULL; 105596fd7ce5SGreg Kroah-Hartman 105696fd7ce5SGreg Kroah-Hartman spin_lock(&redirect_lock); 1057cb0942b8SAl Viro if (redirect) 1058cb0942b8SAl Viro p = get_file(redirect); 105996fd7ce5SGreg Kroah-Hartman spin_unlock(&redirect_lock); 106096fd7ce5SGreg Kroah-Hartman 106196fd7ce5SGreg Kroah-Hartman if (p) { 106296fd7ce5SGreg Kroah-Hartman ssize_t res; 106396fd7ce5SGreg Kroah-Hartman res = vfs_write(p, buf, count, &p->f_pos); 106496fd7ce5SGreg Kroah-Hartman fput(p); 106596fd7ce5SGreg Kroah-Hartman return res; 106696fd7ce5SGreg Kroah-Hartman } 106796fd7ce5SGreg Kroah-Hartman return tty_write(file, buf, count, ppos); 106896fd7ce5SGreg Kroah-Hartman } 106996fd7ce5SGreg Kroah-Hartman 1070136d5258SPeter Hurley /** 1071136d5258SPeter Hurley * tty_send_xchar - send priority character 1072136d5258SPeter Hurley * 1073136d5258SPeter Hurley * Send a high priority character to the tty even if stopped 1074136d5258SPeter Hurley * 1075136d5258SPeter Hurley * Locking: none for xchar method, write ordering for write method. 1076136d5258SPeter Hurley */ 1077136d5258SPeter Hurley 1078136d5258SPeter Hurley int tty_send_xchar(struct tty_struct *tty, char ch) 1079136d5258SPeter Hurley { 1080136d5258SPeter Hurley int was_stopped = tty->stopped; 1081136d5258SPeter Hurley 1082136d5258SPeter Hurley if (tty->ops->send_xchar) { 1083ee0c1a65SPeter Hurley down_read(&tty->termios_rwsem); 1084136d5258SPeter Hurley tty->ops->send_xchar(tty, ch); 1085ee0c1a65SPeter Hurley up_read(&tty->termios_rwsem); 1086136d5258SPeter Hurley return 0; 1087136d5258SPeter Hurley } 1088136d5258SPeter Hurley 1089136d5258SPeter Hurley if (tty_write_lock(tty, 0) < 0) 1090136d5258SPeter Hurley return -ERESTARTSYS; 1091136d5258SPeter Hurley 1092ee0c1a65SPeter Hurley down_read(&tty->termios_rwsem); 1093136d5258SPeter Hurley if (was_stopped) 1094136d5258SPeter Hurley start_tty(tty); 1095136d5258SPeter Hurley tty->ops->write(tty, &ch, 1); 1096136d5258SPeter Hurley if (was_stopped) 1097136d5258SPeter Hurley stop_tty(tty); 1098ee0c1a65SPeter Hurley up_read(&tty->termios_rwsem); 1099136d5258SPeter Hurley tty_write_unlock(tty); 1100136d5258SPeter Hurley return 0; 1101136d5258SPeter Hurley } 1102136d5258SPeter Hurley 110396fd7ce5SGreg Kroah-Hartman static char ptychar[] = "pqrstuvwxyzabcde"; 110496fd7ce5SGreg Kroah-Hartman 110596fd7ce5SGreg Kroah-Hartman /** 110696fd7ce5SGreg Kroah-Hartman * pty_line_name - generate name for a pty 110796fd7ce5SGreg Kroah-Hartman * @driver: the tty driver in use 110896fd7ce5SGreg Kroah-Hartman * @index: the minor number 110996fd7ce5SGreg Kroah-Hartman * @p: output buffer of at least 6 bytes 111096fd7ce5SGreg Kroah-Hartman * 111196fd7ce5SGreg Kroah-Hartman * Generate a name from a driver reference and write it to the output 111296fd7ce5SGreg Kroah-Hartman * buffer. 111396fd7ce5SGreg Kroah-Hartman * 111496fd7ce5SGreg Kroah-Hartman * Locking: None 111596fd7ce5SGreg Kroah-Hartman */ 111696fd7ce5SGreg Kroah-Hartman static void pty_line_name(struct tty_driver *driver, int index, char *p) 111796fd7ce5SGreg Kroah-Hartman { 111896fd7ce5SGreg Kroah-Hartman int i = index + driver->name_base; 111996fd7ce5SGreg Kroah-Hartman /* ->name is initialized to "ttyp", but "tty" is expected */ 112096fd7ce5SGreg Kroah-Hartman sprintf(p, "%s%c%x", 112196fd7ce5SGreg Kroah-Hartman driver->subtype == PTY_TYPE_SLAVE ? "tty" : driver->name, 112296fd7ce5SGreg Kroah-Hartman ptychar[i >> 4 & 0xf], i & 0xf); 112396fd7ce5SGreg Kroah-Hartman } 112496fd7ce5SGreg Kroah-Hartman 112596fd7ce5SGreg Kroah-Hartman /** 112696fd7ce5SGreg Kroah-Hartman * tty_line_name - generate name for a tty 112796fd7ce5SGreg Kroah-Hartman * @driver: the tty driver in use 112896fd7ce5SGreg Kroah-Hartman * @index: the minor number 112996fd7ce5SGreg Kroah-Hartman * @p: output buffer of at least 7 bytes 113096fd7ce5SGreg Kroah-Hartman * 113196fd7ce5SGreg Kroah-Hartman * Generate a name from a driver reference and write it to the output 11325c0a2450SGreg Kroah-Hartman * buffer. 113396fd7ce5SGreg Kroah-Hartman * 113496fd7ce5SGreg Kroah-Hartman * Locking: None 113596fd7ce5SGreg Kroah-Hartman */ 1136723abd87SHannes Reinecke static ssize_t tty_line_name(struct tty_driver *driver, int index, char *p) 113796fd7ce5SGreg Kroah-Hartman { 11380019b408SJiri Slaby if (driver->flags & TTY_DRIVER_UNNUMBERED_NODE) 1139723abd87SHannes Reinecke return sprintf(p, "%s", driver->name); 11400019b408SJiri Slaby else 1141723abd87SHannes Reinecke return sprintf(p, "%s%d", driver->name, 1142723abd87SHannes Reinecke index + driver->name_base); 114396fd7ce5SGreg Kroah-Hartman } 114496fd7ce5SGreg Kroah-Hartman 114596fd7ce5SGreg Kroah-Hartman /** 114696fd7ce5SGreg Kroah-Hartman * tty_driver_lookup_tty() - find an existing tty, if any 114796fd7ce5SGreg Kroah-Hartman * @driver: the driver for the tty 114896fd7ce5SGreg Kroah-Hartman * @idx: the minor number 114996fd7ce5SGreg Kroah-Hartman * 1150aa3cb814SPeter Hurley * Return the tty, if found. If not found, return NULL or ERR_PTR() if the 1151aa3cb814SPeter Hurley * driver lookup() method returns an error. 115296fd7ce5SGreg Kroah-Hartman * 1153aa3cb814SPeter Hurley * Locking: tty_mutex must be held. If the tty is found, bump the tty kref. 115496fd7ce5SGreg Kroah-Hartman */ 115596fd7ce5SGreg Kroah-Hartman static struct tty_struct *tty_driver_lookup_tty(struct tty_driver *driver, 11568ead9dd5SLinus Torvalds struct file *file, int idx) 115796fd7ce5SGreg Kroah-Hartman { 1158aa3cb814SPeter Hurley struct tty_struct *tty; 115996fd7ce5SGreg Kroah-Hartman 1160aa3cb814SPeter Hurley if (driver->ops->lookup) 116112e84c71SOkash Khawaja if (!file) 116212e84c71SOkash Khawaja tty = ERR_PTR(-EIO); 116312e84c71SOkash Khawaja else 11648ead9dd5SLinus Torvalds tty = driver->ops->lookup(driver, file, idx); 1165aa3cb814SPeter Hurley else 1166aa3cb814SPeter Hurley tty = driver->ttys[idx]; 1167aa3cb814SPeter Hurley 1168aa3cb814SPeter Hurley if (!IS_ERR(tty)) 1169aa3cb814SPeter Hurley tty_kref_get(tty); 1170aa3cb814SPeter Hurley return tty; 117196fd7ce5SGreg Kroah-Hartman } 117296fd7ce5SGreg Kroah-Hartman 117396fd7ce5SGreg Kroah-Hartman /** 117496fd7ce5SGreg Kroah-Hartman * tty_init_termios - helper for termios setup 117596fd7ce5SGreg Kroah-Hartman * @tty: the tty to set up 117696fd7ce5SGreg Kroah-Hartman * 11778daa89e0SJohan Hovold * Initialise the termios structure for this tty. This runs under 117896fd7ce5SGreg Kroah-Hartman * the tty_mutex currently so we can be relaxed about ordering. 117996fd7ce5SGreg Kroah-Hartman */ 118096fd7ce5SGreg Kroah-Hartman 1181a3123fd0SPeter Hurley void tty_init_termios(struct tty_struct *tty) 118296fd7ce5SGreg Kroah-Hartman { 118396fd7ce5SGreg Kroah-Hartman struct ktermios *tp; 118496fd7ce5SGreg Kroah-Hartman int idx = tty->index; 118596fd7ce5SGreg Kroah-Hartman 118636b3c070SAlan Cox if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) 118736b3c070SAlan Cox tty->termios = tty->driver->init_termios; 118836b3c070SAlan Cox else { 118936b3c070SAlan Cox /* Check for lazy saved data */ 119096fd7ce5SGreg Kroah-Hartman tp = tty->driver->termios[idx]; 1191ece53405SPeter Hurley if (tp != NULL) { 1192adc8d746SAlan Cox tty->termios = *tp; 1193ece53405SPeter Hurley tty->termios.c_line = tty->driver->init_termios.c_line; 1194ece53405SPeter Hurley } else 119536b3c070SAlan Cox tty->termios = tty->driver->init_termios; 119636b3c070SAlan Cox } 119796fd7ce5SGreg Kroah-Hartman /* Compatibility until drivers always set this */ 1198adc8d746SAlan Cox tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios); 1199adc8d746SAlan Cox tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios); 120096fd7ce5SGreg Kroah-Hartman } 120196fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(tty_init_termios); 120296fd7ce5SGreg Kroah-Hartman 120366d450e8SJiri Slaby int tty_standard_install(struct tty_driver *driver, struct tty_struct *tty) 120466d450e8SJiri Slaby { 1205a3123fd0SPeter Hurley tty_init_termios(tty); 120666d450e8SJiri Slaby tty_driver_kref_get(driver); 120766d450e8SJiri Slaby tty->count++; 120866d450e8SJiri Slaby driver->ttys[tty->index] = tty; 120966d450e8SJiri Slaby return 0; 121066d450e8SJiri Slaby } 121166d450e8SJiri Slaby EXPORT_SYMBOL_GPL(tty_standard_install); 121266d450e8SJiri Slaby 121396fd7ce5SGreg Kroah-Hartman /** 121496fd7ce5SGreg Kroah-Hartman * tty_driver_install_tty() - install a tty entry in the driver 121596fd7ce5SGreg Kroah-Hartman * @driver: the driver for the tty 121696fd7ce5SGreg Kroah-Hartman * @tty: the tty 121796fd7ce5SGreg Kroah-Hartman * 121896fd7ce5SGreg Kroah-Hartman * Install a tty object into the driver tables. The tty->index field 121996fd7ce5SGreg Kroah-Hartman * will be set by the time this is called. This method is responsible 122096fd7ce5SGreg Kroah-Hartman * for ensuring any need additional structures are allocated and 122196fd7ce5SGreg Kroah-Hartman * configured. 122296fd7ce5SGreg Kroah-Hartman * 122396fd7ce5SGreg Kroah-Hartman * Locking: tty_mutex for now 122496fd7ce5SGreg Kroah-Hartman */ 122596fd7ce5SGreg Kroah-Hartman static int tty_driver_install_tty(struct tty_driver *driver, 122696fd7ce5SGreg Kroah-Hartman struct tty_struct *tty) 122796fd7ce5SGreg Kroah-Hartman { 122866d450e8SJiri Slaby return driver->ops->install ? driver->ops->install(driver, tty) : 122966d450e8SJiri Slaby tty_standard_install(driver, tty); 123096fd7ce5SGreg Kroah-Hartman } 123196fd7ce5SGreg Kroah-Hartman 123296fd7ce5SGreg Kroah-Hartman /** 123396fd7ce5SGreg Kroah-Hartman * tty_driver_remove_tty() - remove a tty from the driver tables 123496fd7ce5SGreg Kroah-Hartman * @driver: the driver for the tty 123596fd7ce5SGreg Kroah-Hartman * @idx: the minor number 123696fd7ce5SGreg Kroah-Hartman * 123796fd7ce5SGreg Kroah-Hartman * Remvoe a tty object from the driver tables. The tty->index field 123896fd7ce5SGreg Kroah-Hartman * will be set by the time this is called. 123996fd7ce5SGreg Kroah-Hartman * 124096fd7ce5SGreg Kroah-Hartman * Locking: tty_mutex for now 124196fd7ce5SGreg Kroah-Hartman */ 124205de87edSPeter Hurley static void tty_driver_remove_tty(struct tty_driver *driver, struct tty_struct *tty) 124396fd7ce5SGreg Kroah-Hartman { 124496fd7ce5SGreg Kroah-Hartman if (driver->ops->remove) 124596fd7ce5SGreg Kroah-Hartman driver->ops->remove(driver, tty); 124696fd7ce5SGreg Kroah-Hartman else 124796fd7ce5SGreg Kroah-Hartman driver->ttys[tty->index] = NULL; 124896fd7ce5SGreg Kroah-Hartman } 124996fd7ce5SGreg Kroah-Hartman 125096fd7ce5SGreg Kroah-Hartman /* 125196fd7ce5SGreg Kroah-Hartman * tty_reopen() - fast re-open of an open tty 125296fd7ce5SGreg Kroah-Hartman * @tty - the tty to open 125396fd7ce5SGreg Kroah-Hartman * 125496fd7ce5SGreg Kroah-Hartman * Return 0 on success, -errno on error. 12555d93e748SPeter Hurley * Re-opens on master ptys are not allowed and return -EIO. 125696fd7ce5SGreg Kroah-Hartman * 12575d93e748SPeter Hurley * Locking: Caller must hold tty_lock 125896fd7ce5SGreg Kroah-Hartman */ 125996fd7ce5SGreg Kroah-Hartman static int tty_reopen(struct tty_struct *tty) 126096fd7ce5SGreg Kroah-Hartman { 126196fd7ce5SGreg Kroah-Hartman struct tty_driver *driver = tty->driver; 1262d3736d82SDmitry Safonov struct tty_ldisc *ld; 1263d3736d82SDmitry Safonov int retval = 0; 126496fd7ce5SGreg Kroah-Hartman 126596fd7ce5SGreg Kroah-Hartman if (driver->type == TTY_DRIVER_TYPE_PTY && 12665d93e748SPeter Hurley driver->subtype == PTY_TYPE_MASTER) 126796fd7ce5SGreg Kroah-Hartman return -EIO; 126896fd7ce5SGreg Kroah-Hartman 12697f22f6c9SPeter Hurley if (!tty->count) 12707f22f6c9SPeter Hurley return -EAGAIN; 12717f22f6c9SPeter Hurley 127286f2c00fSPeter Hurley if (test_bit(TTY_EXCLUSIVE, &tty->flags) && !capable(CAP_SYS_ADMIN)) 127386f2c00fSPeter Hurley return -EBUSY; 127486f2c00fSPeter Hurley 1275d3736d82SDmitry Safonov ld = tty_ldisc_ref_wait(tty); 1276d3736d82SDmitry Safonov if (ld) { 1277d3736d82SDmitry Safonov tty_ldisc_deref(ld); 1278d3736d82SDmitry Safonov } else { 127983d817f4SDmitry Safonov retval = tty_ldisc_lock(tty, 5 * HZ); 128083d817f4SDmitry Safonov if (retval) 128183d817f4SDmitry Safonov return retval; 128296fd7ce5SGreg Kroah-Hartman 1283cf62a1a1SDmitry Safonov if (!tty->ldisc) 1284fe324167SDmitry Safonov retval = tty_ldisc_reinit(tty, tty->termios.c_line); 128583d817f4SDmitry Safonov tty_ldisc_unlock(tty); 1286d3736d82SDmitry Safonov } 1287cf62a1a1SDmitry Safonov 1288cf62a1a1SDmitry Safonov if (retval == 0) 1289cf62a1a1SDmitry Safonov tty->count++; 1290cf62a1a1SDmitry Safonov 1291fe324167SDmitry Safonov return retval; 129296fd7ce5SGreg Kroah-Hartman } 129396fd7ce5SGreg Kroah-Hartman 129496fd7ce5SGreg Kroah-Hartman /** 129596fd7ce5SGreg Kroah-Hartman * tty_init_dev - initialise a tty device 129696fd7ce5SGreg Kroah-Hartman * @driver: tty driver we are opening a device on 129796fd7ce5SGreg Kroah-Hartman * @idx: device index 129896fd7ce5SGreg Kroah-Hartman * @ret_tty: returned tty structure 129996fd7ce5SGreg Kroah-Hartman * 130096fd7ce5SGreg Kroah-Hartman * Prepare a tty device. This may not be a "new" clean device but 130196fd7ce5SGreg Kroah-Hartman * could also be an active device. The pty drivers require special 130296fd7ce5SGreg Kroah-Hartman * handling because of this. 130396fd7ce5SGreg Kroah-Hartman * 130496fd7ce5SGreg Kroah-Hartman * Locking: 130596fd7ce5SGreg Kroah-Hartman * The function is called under the tty_mutex, which 130696fd7ce5SGreg Kroah-Hartman * protects us from the tty struct or driver itself going away. 130796fd7ce5SGreg Kroah-Hartman * 130896fd7ce5SGreg Kroah-Hartman * On exit the tty device has the line discipline attached and 130996fd7ce5SGreg Kroah-Hartman * a reference count of 1. If a pair was created for pty/tty use 131096fd7ce5SGreg Kroah-Hartman * and the other was a pty master then it too has a reference count of 1. 131196fd7ce5SGreg Kroah-Hartman * 131296fd7ce5SGreg Kroah-Hartman * WSH 06/09/97: Rewritten to remove races and properly clean up after a 131396fd7ce5SGreg Kroah-Hartman * failed open. The new code protects the open with a mutex, so it's 131496fd7ce5SGreg Kroah-Hartman * really quite straightforward. The mutex locking can probably be 131596fd7ce5SGreg Kroah-Hartman * relaxed for the (most common) case of reopening a tty. 131696fd7ce5SGreg Kroah-Hartman */ 131796fd7ce5SGreg Kroah-Hartman 1318593a27c4SKonstantin Khlebnikov struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx) 131996fd7ce5SGreg Kroah-Hartman { 132096fd7ce5SGreg Kroah-Hartman struct tty_struct *tty; 132196fd7ce5SGreg Kroah-Hartman int retval; 132296fd7ce5SGreg Kroah-Hartman 132396fd7ce5SGreg Kroah-Hartman /* 132496fd7ce5SGreg Kroah-Hartman * First time open is complex, especially for PTY devices. 132596fd7ce5SGreg Kroah-Hartman * This code guarantees that either everything succeeds and the 132696fd7ce5SGreg Kroah-Hartman * TTY is ready for operation, or else the table slots are vacated 132796fd7ce5SGreg Kroah-Hartman * and the allocated memory released. (Except that the termios 132816b00ae8SJohan Hovold * may be retained.) 132996fd7ce5SGreg Kroah-Hartman */ 133096fd7ce5SGreg Kroah-Hartman 133196fd7ce5SGreg Kroah-Hartman if (!try_module_get(driver->owner)) 133296fd7ce5SGreg Kroah-Hartman return ERR_PTR(-ENODEV); 133396fd7ce5SGreg Kroah-Hartman 13342c964a2fSRasmus Villemoes tty = alloc_tty_struct(driver, idx); 1335d5543503SJiri Slaby if (!tty) { 1336d5543503SJiri Slaby retval = -ENOMEM; 1337d5543503SJiri Slaby goto err_module_put; 1338d5543503SJiri Slaby } 133996fd7ce5SGreg Kroah-Hartman 134089c8d91eSAlan Cox tty_lock(tty); 134196fd7ce5SGreg Kroah-Hartman retval = tty_driver_install_tty(driver, tty); 1342d5543503SJiri Slaby if (retval < 0) 1343c8b710b3SPeter Hurley goto err_free_tty; 134496fd7ce5SGreg Kroah-Hartman 134504831dc1SJiri Slaby if (!tty->port) 134604831dc1SJiri Slaby tty->port = driver->ports[idx]; 134704831dc1SJiri Slaby 13482ae0b31eSJiri Slaby if (WARN_RATELIMIT(!tty->port, 13492ae0b31eSJiri Slaby "%s: %s driver does not set tty->port. This would crash the kernel. Fix the driver!\n", 13502ae0b31eSJiri Slaby __func__, tty->driver->name)) { 13512ae0b31eSJiri Slaby retval = -EINVAL; 13522ae0b31eSJiri Slaby goto err_release_lock; 13532ae0b31eSJiri Slaby } 13545d4121c0SJiri Slaby 1355b027e229SGaurav Kohli retval = tty_ldisc_lock(tty, 5 * HZ); 1356b027e229SGaurav Kohli if (retval) 1357b027e229SGaurav Kohli goto err_release_lock; 1358967fab69SJiri Slaby tty->port->itty = tty; 1359967fab69SJiri Slaby 136096fd7ce5SGreg Kroah-Hartman /* 136196fd7ce5SGreg Kroah-Hartman * Structures all installed ... call the ldisc open routines. 136296fd7ce5SGreg Kroah-Hartman * If we fail here just call release_tty to clean up. No need 136396fd7ce5SGreg Kroah-Hartman * to decrement the use counts, as release_tty doesn't care. 136496fd7ce5SGreg Kroah-Hartman */ 136596fd7ce5SGreg Kroah-Hartman retval = tty_ldisc_setup(tty, tty->link); 136696fd7ce5SGreg Kroah-Hartman if (retval) 1367d5543503SJiri Slaby goto err_release_tty; 1368b027e229SGaurav Kohli tty_ldisc_unlock(tty); 136989c8d91eSAlan Cox /* Return the tty locked so that it cannot vanish under the caller */ 137096fd7ce5SGreg Kroah-Hartman return tty; 137196fd7ce5SGreg Kroah-Hartman 1372c8b710b3SPeter Hurley err_free_tty: 137389c8d91eSAlan Cox tty_unlock(tty); 1374d5543503SJiri Slaby free_tty_struct(tty); 1375d5543503SJiri Slaby err_module_put: 137696fd7ce5SGreg Kroah-Hartman module_put(driver->owner); 1377d5543503SJiri Slaby return ERR_PTR(retval); 137896fd7ce5SGreg Kroah-Hartman 137996fd7ce5SGreg Kroah-Hartman /* call the tty release_tty routine to clean out this slot */ 1380d5543503SJiri Slaby err_release_tty: 1381b027e229SGaurav Kohli tty_ldisc_unlock(tty); 1382339f36baSPeter Hurley tty_info_ratelimited(tty, "ldisc open failed (%d), clearing slot %d\n", 1383339f36baSPeter Hurley retval, idx); 1384b027e229SGaurav Kohli err_release_lock: 1385b027e229SGaurav Kohli tty_unlock(tty); 138696fd7ce5SGreg Kroah-Hartman release_tty(tty, idx); 138796fd7ce5SGreg Kroah-Hartman return ERR_PTR(retval); 138896fd7ce5SGreg Kroah-Hartman } 138996fd7ce5SGreg Kroah-Hartman 1390f51ccf46SJohan Hovold /** 1391f51ccf46SJohan Hovold * tty_save_termios() - save tty termios data in driver table 1392f51ccf46SJohan Hovold * @tty: tty whose termios data to save 1393f51ccf46SJohan Hovold * 1394f51ccf46SJohan Hovold * Locking: Caller guarantees serialisation with tty_init_termios(). 1395f51ccf46SJohan Hovold */ 1396f51ccf46SJohan Hovold void tty_save_termios(struct tty_struct *tty) 139796fd7ce5SGreg Kroah-Hartman { 139896fd7ce5SGreg Kroah-Hartman struct ktermios *tp; 139996fd7ce5SGreg Kroah-Hartman int idx = tty->index; 140036b3c070SAlan Cox 140136b3c070SAlan Cox /* If the port is going to reset then it has no termios to save */ 140236b3c070SAlan Cox if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) 140336b3c070SAlan Cox return; 140436b3c070SAlan Cox 140536b3c070SAlan Cox /* Stash the termios data */ 1406adc8d746SAlan Cox tp = tty->driver->termios[idx]; 140736b3c070SAlan Cox if (tp == NULL) { 140836b3c070SAlan Cox tp = kmalloc(sizeof(struct ktermios), GFP_KERNEL); 140925080652SPeter Hurley if (tp == NULL) 141036b3c070SAlan Cox return; 14114ac5d705SDan Carpenter tty->driver->termios[idx] = tp; 141236b3c070SAlan Cox } 141336b3c070SAlan Cox *tp = tty->termios; 141496fd7ce5SGreg Kroah-Hartman } 1415f51ccf46SJohan Hovold EXPORT_SYMBOL_GPL(tty_save_termios); 141696fd7ce5SGreg Kroah-Hartman 1417a2965b7bSPeter Hurley /** 1418949aa64fSPeter Hurley * tty_flush_works - flush all works of a tty/pty pair 1419949aa64fSPeter Hurley * @tty: tty device to flush works for (or either end of a pty pair) 1420a2965b7bSPeter Hurley * 1421949aa64fSPeter Hurley * Sync flush all works belonging to @tty (and the 'other' tty). 1422a2965b7bSPeter Hurley */ 1423a2965b7bSPeter Hurley static void tty_flush_works(struct tty_struct *tty) 1424a2965b7bSPeter Hurley { 1425a2965b7bSPeter Hurley flush_work(&tty->SAK_work); 1426a2965b7bSPeter Hurley flush_work(&tty->hangup_work); 1427949aa64fSPeter Hurley if (tty->link) { 1428949aa64fSPeter Hurley flush_work(&tty->link->SAK_work); 1429949aa64fSPeter Hurley flush_work(&tty->link->hangup_work); 1430949aa64fSPeter Hurley } 1431a2965b7bSPeter Hurley } 143296fd7ce5SGreg Kroah-Hartman 143396fd7ce5SGreg Kroah-Hartman /** 143496fd7ce5SGreg Kroah-Hartman * release_one_tty - release tty structure memory 143596fd7ce5SGreg Kroah-Hartman * @kref: kref of tty we are obliterating 143696fd7ce5SGreg Kroah-Hartman * 143796fd7ce5SGreg Kroah-Hartman * Releases memory associated with a tty structure, and clears out the 143896fd7ce5SGreg Kroah-Hartman * driver table slots. This function is called when a device is no longer 143996fd7ce5SGreg Kroah-Hartman * in use. It also gets called when setup of a device fails. 144096fd7ce5SGreg Kroah-Hartman * 144196fd7ce5SGreg Kroah-Hartman * Locking: 144296fd7ce5SGreg Kroah-Hartman * takes the file list lock internally when working on the list 144396fd7ce5SGreg Kroah-Hartman * of ttys that the driver keeps. 144496fd7ce5SGreg Kroah-Hartman * 144596fd7ce5SGreg Kroah-Hartman * This method gets called from a work queue so that the driver private 144696fd7ce5SGreg Kroah-Hartman * cleanup ops can sleep (needed for USB at least) 144796fd7ce5SGreg Kroah-Hartman */ 144896fd7ce5SGreg Kroah-Hartman static void release_one_tty(struct work_struct *work) 144996fd7ce5SGreg Kroah-Hartman { 145096fd7ce5SGreg Kroah-Hartman struct tty_struct *tty = 145196fd7ce5SGreg Kroah-Hartman container_of(work, struct tty_struct, hangup_work); 145296fd7ce5SGreg Kroah-Hartman struct tty_driver *driver = tty->driver; 1453b216df53SCyrill Gorcunov struct module *owner = driver->owner; 145496fd7ce5SGreg Kroah-Hartman 145596fd7ce5SGreg Kroah-Hartman if (tty->ops->cleanup) 145696fd7ce5SGreg Kroah-Hartman tty->ops->cleanup(tty); 145796fd7ce5SGreg Kroah-Hartman 145896fd7ce5SGreg Kroah-Hartman tty->magic = 0; 145996fd7ce5SGreg Kroah-Hartman tty_driver_kref_put(driver); 1460b216df53SCyrill Gorcunov module_put(owner); 146196fd7ce5SGreg Kroah-Hartman 14624a510969SPeter Hurley spin_lock(&tty->files_lock); 146396fd7ce5SGreg Kroah-Hartman list_del_init(&tty->tty_files); 14644a510969SPeter Hurley spin_unlock(&tty->files_lock); 146596fd7ce5SGreg Kroah-Hartman 146696fd7ce5SGreg Kroah-Hartman put_pid(tty->pgrp); 146796fd7ce5SGreg Kroah-Hartman put_pid(tty->session); 146896fd7ce5SGreg Kroah-Hartman free_tty_struct(tty); 146996fd7ce5SGreg Kroah-Hartman } 147096fd7ce5SGreg Kroah-Hartman 147196fd7ce5SGreg Kroah-Hartman static void queue_release_one_tty(struct kref *kref) 147296fd7ce5SGreg Kroah-Hartman { 147396fd7ce5SGreg Kroah-Hartman struct tty_struct *tty = container_of(kref, struct tty_struct, kref); 147496fd7ce5SGreg Kroah-Hartman 147596fd7ce5SGreg Kroah-Hartman /* The hangup queue is now free so we can reuse it rather than 147696fd7ce5SGreg Kroah-Hartman waste a chunk of memory for each port */ 147796fd7ce5SGreg Kroah-Hartman INIT_WORK(&tty->hangup_work, release_one_tty); 147896fd7ce5SGreg Kroah-Hartman schedule_work(&tty->hangup_work); 147996fd7ce5SGreg Kroah-Hartman } 148096fd7ce5SGreg Kroah-Hartman 148196fd7ce5SGreg Kroah-Hartman /** 148296fd7ce5SGreg Kroah-Hartman * tty_kref_put - release a tty kref 148396fd7ce5SGreg Kroah-Hartman * @tty: tty device 148496fd7ce5SGreg Kroah-Hartman * 148596fd7ce5SGreg Kroah-Hartman * Release a reference to a tty device and if need be let the kref 148696fd7ce5SGreg Kroah-Hartman * layer destruct the object for us 148796fd7ce5SGreg Kroah-Hartman */ 148896fd7ce5SGreg Kroah-Hartman 148996fd7ce5SGreg Kroah-Hartman void tty_kref_put(struct tty_struct *tty) 149096fd7ce5SGreg Kroah-Hartman { 149196fd7ce5SGreg Kroah-Hartman if (tty) 149296fd7ce5SGreg Kroah-Hartman kref_put(&tty->kref, queue_release_one_tty); 149396fd7ce5SGreg Kroah-Hartman } 149496fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_kref_put); 149596fd7ce5SGreg Kroah-Hartman 149696fd7ce5SGreg Kroah-Hartman /** 149796fd7ce5SGreg Kroah-Hartman * release_tty - release tty structure memory 149896fd7ce5SGreg Kroah-Hartman * 149996fd7ce5SGreg Kroah-Hartman * Release both @tty and a possible linked partner (think pty pair), 150096fd7ce5SGreg Kroah-Hartman * and decrement the refcount of the backing module. 150196fd7ce5SGreg Kroah-Hartman * 150296fd7ce5SGreg Kroah-Hartman * Locking: 1503d155255aSAlan Cox * tty_mutex 150496fd7ce5SGreg Kroah-Hartman * takes the file list lock internally when working on the list 150596fd7ce5SGreg Kroah-Hartman * of ttys that the driver keeps. 150696fd7ce5SGreg Kroah-Hartman * 150796fd7ce5SGreg Kroah-Hartman */ 150896fd7ce5SGreg Kroah-Hartman static void release_tty(struct tty_struct *tty, int idx) 150996fd7ce5SGreg Kroah-Hartman { 151096fd7ce5SGreg Kroah-Hartman /* This should always be true but check for the moment */ 151196fd7ce5SGreg Kroah-Hartman WARN_ON(tty->index != idx); 1512d155255aSAlan Cox WARN_ON(!mutex_is_locked(&tty_mutex)); 151336b3c070SAlan Cox if (tty->ops->shutdown) 151436b3c070SAlan Cox tty->ops->shutdown(tty); 1515f51ccf46SJohan Hovold tty_save_termios(tty); 151636b3c070SAlan Cox tty_driver_remove_tty(tty->driver, tty); 1517967fab69SJiri Slaby tty->port->itty = NULL; 151864e377dcSPeter Hurley if (tty->link) 151964e377dcSPeter Hurley tty->link->port->itty = NULL; 1520e176058fSPeter Hurley tty_buffer_cancel_work(tty->port); 15212b022ab7SSahara if (tty->link) 15222b022ab7SSahara tty_buffer_cancel_work(tty->link->port); 152336b3c070SAlan Cox 152496fd7ce5SGreg Kroah-Hartman tty_kref_put(tty->link); 152596fd7ce5SGreg Kroah-Hartman tty_kref_put(tty); 152696fd7ce5SGreg Kroah-Hartman } 152796fd7ce5SGreg Kroah-Hartman 152896fd7ce5SGreg Kroah-Hartman /** 1529955787caSJiri Slaby * tty_release_checks - check a tty before real release 1530955787caSJiri Slaby * @tty: tty to check 1531955787caSJiri Slaby * @o_tty: link of @tty (if any) 1532955787caSJiri Slaby * @idx: index of the tty 1533955787caSJiri Slaby * 1534955787caSJiri Slaby * Performs some paranoid checking before true release of the @tty. 1535955787caSJiri Slaby * This is a no-op unless TTY_PARANOIA_CHECK is defined. 1536955787caSJiri Slaby */ 1537359b9fb5SPeter Hurley static int tty_release_checks(struct tty_struct *tty, int idx) 1538955787caSJiri Slaby { 1539955787caSJiri Slaby #ifdef TTY_PARANOIA_CHECK 1540955787caSJiri Slaby if (idx < 0 || idx >= tty->driver->num) { 1541e2dfa3d3SPeter Hurley tty_debug(tty, "bad idx %d\n", idx); 1542955787caSJiri Slaby return -1; 1543955787caSJiri Slaby } 1544955787caSJiri Slaby 1545955787caSJiri Slaby /* not much to check for devpts */ 1546955787caSJiri Slaby if (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM) 1547955787caSJiri Slaby return 0; 1548955787caSJiri Slaby 1549955787caSJiri Slaby if (tty != tty->driver->ttys[idx]) { 1550e2dfa3d3SPeter Hurley tty_debug(tty, "bad driver table[%d] = %p\n", 1551e2dfa3d3SPeter Hurley idx, tty->driver->ttys[idx]); 1552955787caSJiri Slaby return -1; 1553955787caSJiri Slaby } 1554955787caSJiri Slaby if (tty->driver->other) { 1555359b9fb5SPeter Hurley struct tty_struct *o_tty = tty->link; 1556359b9fb5SPeter Hurley 1557955787caSJiri Slaby if (o_tty != tty->driver->other->ttys[idx]) { 1558e2dfa3d3SPeter Hurley tty_debug(tty, "bad other table[%d] = %p\n", 1559e2dfa3d3SPeter Hurley idx, tty->driver->other->ttys[idx]); 1560955787caSJiri Slaby return -1; 1561955787caSJiri Slaby } 1562955787caSJiri Slaby if (o_tty->link != tty) { 1563e2dfa3d3SPeter Hurley tty_debug(tty, "bad link = %p\n", o_tty->link); 1564955787caSJiri Slaby return -1; 1565955787caSJiri Slaby } 1566955787caSJiri Slaby } 1567955787caSJiri Slaby #endif 1568955787caSJiri Slaby return 0; 1569955787caSJiri Slaby } 1570955787caSJiri Slaby 1571955787caSJiri Slaby /** 1572a09ac397SOkash Khawaja * tty_kclose - closes tty opened by tty_kopen 1573a09ac397SOkash Khawaja * @tty: tty device 1574a09ac397SOkash Khawaja * 1575a09ac397SOkash Khawaja * Performs the final steps to release and free a tty device. It is the 1576a09ac397SOkash Khawaja * same as tty_release_struct except that it also resets TTY_PORT_KOPENED 1577a09ac397SOkash Khawaja * flag on tty->port. 1578a09ac397SOkash Khawaja */ 1579a09ac397SOkash Khawaja void tty_kclose(struct tty_struct *tty) 1580a09ac397SOkash Khawaja { 1581a09ac397SOkash Khawaja /* 1582a09ac397SOkash Khawaja * Ask the line discipline code to release its structures 1583a09ac397SOkash Khawaja */ 1584a09ac397SOkash Khawaja tty_ldisc_release(tty); 1585a09ac397SOkash Khawaja 1586a09ac397SOkash Khawaja /* Wait for pending work before tty destruction commmences */ 1587a09ac397SOkash Khawaja tty_flush_works(tty); 1588a09ac397SOkash Khawaja 1589a09ac397SOkash Khawaja tty_debug_hangup(tty, "freeing structure\n"); 1590a09ac397SOkash Khawaja /* 1591a09ac397SOkash Khawaja * The release_tty function takes care of the details of clearing 1592a09ac397SOkash Khawaja * the slots and preserving the termios structure. The tty_unlock_pair 1593a09ac397SOkash Khawaja * should be safe as we keep a kref while the tty is locked (so the 1594a09ac397SOkash Khawaja * unlock never unlocks a freed tty). 1595a09ac397SOkash Khawaja */ 1596a09ac397SOkash Khawaja mutex_lock(&tty_mutex); 1597a09ac397SOkash Khawaja tty_port_set_kopened(tty->port, 0); 1598a09ac397SOkash Khawaja release_tty(tty, tty->index); 1599a09ac397SOkash Khawaja mutex_unlock(&tty_mutex); 1600a09ac397SOkash Khawaja } 1601a09ac397SOkash Khawaja EXPORT_SYMBOL_GPL(tty_kclose); 1602a09ac397SOkash Khawaja 1603a09ac397SOkash Khawaja /** 16049ed90d20SRob Herring * tty_release_struct - release a tty struct 16059ed90d20SRob Herring * @tty: tty device 16069ed90d20SRob Herring * @idx: index of the tty 16079ed90d20SRob Herring * 16089ed90d20SRob Herring * Performs the final steps to release and free a tty device. It is 16099ed90d20SRob Herring * roughly the reverse of tty_init_dev. 16109ed90d20SRob Herring */ 16119ed90d20SRob Herring void tty_release_struct(struct tty_struct *tty, int idx) 16129ed90d20SRob Herring { 16139ed90d20SRob Herring /* 16149ed90d20SRob Herring * Ask the line discipline code to release its structures 16159ed90d20SRob Herring */ 16169ed90d20SRob Herring tty_ldisc_release(tty); 16179ed90d20SRob Herring 16189ed90d20SRob Herring /* Wait for pending work before tty destruction commmences */ 16199ed90d20SRob Herring tty_flush_works(tty); 16209ed90d20SRob Herring 16219ed90d20SRob Herring tty_debug_hangup(tty, "freeing structure\n"); 16229ed90d20SRob Herring /* 16239ed90d20SRob Herring * The release_tty function takes care of the details of clearing 16249ed90d20SRob Herring * the slots and preserving the termios structure. The tty_unlock_pair 16259ed90d20SRob Herring * should be safe as we keep a kref while the tty is locked (so the 16269ed90d20SRob Herring * unlock never unlocks a freed tty). 16279ed90d20SRob Herring */ 16289ed90d20SRob Herring mutex_lock(&tty_mutex); 16299ed90d20SRob Herring release_tty(tty, idx); 16309ed90d20SRob Herring mutex_unlock(&tty_mutex); 16319ed90d20SRob Herring } 16329ed90d20SRob Herring EXPORT_SYMBOL_GPL(tty_release_struct); 16339ed90d20SRob Herring 16349ed90d20SRob Herring /** 163596fd7ce5SGreg Kroah-Hartman * tty_release - vfs callback for close 163696fd7ce5SGreg Kroah-Hartman * @inode: inode of tty 163796fd7ce5SGreg Kroah-Hartman * @filp: file pointer for handle to tty 163896fd7ce5SGreg Kroah-Hartman * 163996fd7ce5SGreg Kroah-Hartman * Called the last time each file handle is closed that references 164096fd7ce5SGreg Kroah-Hartman * this tty. There may however be several such references. 164196fd7ce5SGreg Kroah-Hartman * 164296fd7ce5SGreg Kroah-Hartman * Locking: 164396fd7ce5SGreg Kroah-Hartman * Takes bkl. See tty_release_dev 164496fd7ce5SGreg Kroah-Hartman * 164596fd7ce5SGreg Kroah-Hartman * Even releasing the tty structures is a tricky business.. We have 164696fd7ce5SGreg Kroah-Hartman * to be very careful that the structures are all released at the 164796fd7ce5SGreg Kroah-Hartman * same time, as interrupts might otherwise get the wrong pointers. 164896fd7ce5SGreg Kroah-Hartman * 164996fd7ce5SGreg Kroah-Hartman * WSH 09/09/97: rewritten to avoid some nasty race conditions that could 165096fd7ce5SGreg Kroah-Hartman * lead to double frees or releasing memory still in use. 165196fd7ce5SGreg Kroah-Hartman */ 165296fd7ce5SGreg Kroah-Hartman 165396fd7ce5SGreg Kroah-Hartman int tty_release(struct inode *inode, struct file *filp) 165496fd7ce5SGreg Kroah-Hartman { 165596fd7ce5SGreg Kroah-Hartman struct tty_struct *tty = file_tty(filp); 16567ffb6da9SPeter Hurley struct tty_struct *o_tty = NULL; 16577ffb6da9SPeter Hurley int do_sleep, final; 165896fd7ce5SGreg Kroah-Hartman int idx; 165937b16457SPeter Hurley long timeout = 0; 1660494c1eacSPeter Hurley int once = 1; 166196fd7ce5SGreg Kroah-Hartman 16629de44bd6SJiri Slaby if (tty_paranoia_check(tty, inode, __func__)) 166396fd7ce5SGreg Kroah-Hartman return 0; 166496fd7ce5SGreg Kroah-Hartman 166589c8d91eSAlan Cox tty_lock(tty); 16669de44bd6SJiri Slaby check_tty_count(tty, __func__); 166796fd7ce5SGreg Kroah-Hartman 166896fd7ce5SGreg Kroah-Hartman __tty_fasync(-1, filp, 0); 166996fd7ce5SGreg Kroah-Hartman 167096fd7ce5SGreg Kroah-Hartman idx = tty->index; 16717ffb6da9SPeter Hurley if (tty->driver->type == TTY_DRIVER_TYPE_PTY && 16727ffb6da9SPeter Hurley tty->driver->subtype == PTY_TYPE_MASTER) 167396fd7ce5SGreg Kroah-Hartman o_tty = tty->link; 167496fd7ce5SGreg Kroah-Hartman 1675359b9fb5SPeter Hurley if (tty_release_checks(tty, idx)) { 167689c8d91eSAlan Cox tty_unlock(tty); 167796fd7ce5SGreg Kroah-Hartman return 0; 167896fd7ce5SGreg Kroah-Hartman } 167996fd7ce5SGreg Kroah-Hartman 1680d435cefeSPeter Hurley tty_debug_hangup(tty, "releasing (count=%d)\n", tty->count); 168196fd7ce5SGreg Kroah-Hartman 168296fd7ce5SGreg Kroah-Hartman if (tty->ops->close) 168396fd7ce5SGreg Kroah-Hartman tty->ops->close(tty, filp); 168496fd7ce5SGreg Kroah-Hartman 16852aff5e2bSPeter Hurley /* If tty is pty master, lock the slave pty (stable lock order) */ 16862aff5e2bSPeter Hurley tty_lock_slave(o_tty); 16872aff5e2bSPeter Hurley 168896fd7ce5SGreg Kroah-Hartman /* 168996fd7ce5SGreg Kroah-Hartman * Sanity check: if tty->count is going to zero, there shouldn't be 169096fd7ce5SGreg Kroah-Hartman * any waiters on tty->read_wait or tty->write_wait. We test the 169196fd7ce5SGreg Kroah-Hartman * wait queues and kick everyone out _before_ actually starting to 169296fd7ce5SGreg Kroah-Hartman * close. This ensures that we won't block while releasing the tty 169396fd7ce5SGreg Kroah-Hartman * structure. 169496fd7ce5SGreg Kroah-Hartman * 169596fd7ce5SGreg Kroah-Hartman * The test for the o_tty closing is necessary, since the master and 169696fd7ce5SGreg Kroah-Hartman * slave sides may close in any order. If the slave side closes out 169796fd7ce5SGreg Kroah-Hartman * first, its count will be one, since the master side holds an open. 1698324c1650SPeter Hurley * Thus this test wouldn't be triggered at the time the slave closed, 169996fd7ce5SGreg Kroah-Hartman * so we do it now. 170096fd7ce5SGreg Kroah-Hartman */ 1701d5e370a4SPeter Hurley while (1) { 170296fd7ce5SGreg Kroah-Hartman do_sleep = 0; 170396fd7ce5SGreg Kroah-Hartman 1704324c1650SPeter Hurley if (tty->count <= 1) { 170596fd7ce5SGreg Kroah-Hartman if (waitqueue_active(&tty->read_wait)) { 1706a9a08845SLinus Torvalds wake_up_poll(&tty->read_wait, EPOLLIN); 170796fd7ce5SGreg Kroah-Hartman do_sleep++; 170896fd7ce5SGreg Kroah-Hartman } 170996fd7ce5SGreg Kroah-Hartman if (waitqueue_active(&tty->write_wait)) { 1710a9a08845SLinus Torvalds wake_up_poll(&tty->write_wait, EPOLLOUT); 171196fd7ce5SGreg Kroah-Hartman do_sleep++; 171296fd7ce5SGreg Kroah-Hartman } 171396fd7ce5SGreg Kroah-Hartman } 17147ffb6da9SPeter Hurley if (o_tty && o_tty->count <= 1) { 171596fd7ce5SGreg Kroah-Hartman if (waitqueue_active(&o_tty->read_wait)) { 1716a9a08845SLinus Torvalds wake_up_poll(&o_tty->read_wait, EPOLLIN); 171796fd7ce5SGreg Kroah-Hartman do_sleep++; 171896fd7ce5SGreg Kroah-Hartman } 171996fd7ce5SGreg Kroah-Hartman if (waitqueue_active(&o_tty->write_wait)) { 1720a9a08845SLinus Torvalds wake_up_poll(&o_tty->write_wait, EPOLLOUT); 172196fd7ce5SGreg Kroah-Hartman do_sleep++; 172296fd7ce5SGreg Kroah-Hartman } 172396fd7ce5SGreg Kroah-Hartman } 172496fd7ce5SGreg Kroah-Hartman if (!do_sleep) 172596fd7ce5SGreg Kroah-Hartman break; 172696fd7ce5SGreg Kroah-Hartman 1727494c1eacSPeter Hurley if (once) { 1728494c1eacSPeter Hurley once = 0; 1729339f36baSPeter Hurley tty_warn(tty, "read/write wait queue active!\n"); 1730494c1eacSPeter Hurley } 173137b16457SPeter Hurley schedule_timeout_killable(timeout); 173237b16457SPeter Hurley if (timeout < 120 * HZ) 173337b16457SPeter Hurley timeout = 2 * timeout + 1; 173437b16457SPeter Hurley else 173537b16457SPeter Hurley timeout = MAX_SCHEDULE_TIMEOUT; 173696fd7ce5SGreg Kroah-Hartman } 173796fd7ce5SGreg Kroah-Hartman 17387ffb6da9SPeter Hurley if (o_tty) { 173996fd7ce5SGreg Kroah-Hartman if (--o_tty->count < 0) { 1740339f36baSPeter Hurley tty_warn(tty, "bad slave count (%d)\n", o_tty->count); 174196fd7ce5SGreg Kroah-Hartman o_tty->count = 0; 174296fd7ce5SGreg Kroah-Hartman } 174396fd7ce5SGreg Kroah-Hartman } 174496fd7ce5SGreg Kroah-Hartman if (--tty->count < 0) { 1745339f36baSPeter Hurley tty_warn(tty, "bad tty->count (%d)\n", tty->count); 174696fd7ce5SGreg Kroah-Hartman tty->count = 0; 174796fd7ce5SGreg Kroah-Hartman } 174896fd7ce5SGreg Kroah-Hartman 174996fd7ce5SGreg Kroah-Hartman /* 175096fd7ce5SGreg Kroah-Hartman * We've decremented tty->count, so we need to remove this file 175196fd7ce5SGreg Kroah-Hartman * descriptor off the tty->tty_files list; this serves two 175296fd7ce5SGreg Kroah-Hartman * purposes: 175396fd7ce5SGreg Kroah-Hartman * - check_tty_count sees the correct number of file descriptors 175496fd7ce5SGreg Kroah-Hartman * associated with this tty. 175596fd7ce5SGreg Kroah-Hartman * - do_tty_hangup no longer sees this file descriptor as 175696fd7ce5SGreg Kroah-Hartman * something that needs to be handled for hangups. 175796fd7ce5SGreg Kroah-Hartman */ 175896fd7ce5SGreg Kroah-Hartman tty_del_file(filp); 175996fd7ce5SGreg Kroah-Hartman 176096fd7ce5SGreg Kroah-Hartman /* 176196fd7ce5SGreg Kroah-Hartman * Perform some housekeeping before deciding whether to return. 176296fd7ce5SGreg Kroah-Hartman * 176396fd7ce5SGreg Kroah-Hartman * If _either_ side is closing, make sure there aren't any 176496fd7ce5SGreg Kroah-Hartman * processes that still think tty or o_tty is their controlling 176596fd7ce5SGreg Kroah-Hartman * tty. 176696fd7ce5SGreg Kroah-Hartman */ 1767324c1650SPeter Hurley if (!tty->count) { 176896fd7ce5SGreg Kroah-Hartman read_lock(&tasklist_lock); 176996fd7ce5SGreg Kroah-Hartman session_clear_tty(tty->session); 17707ffb6da9SPeter Hurley if (o_tty) 177196fd7ce5SGreg Kroah-Hartman session_clear_tty(o_tty->session); 177296fd7ce5SGreg Kroah-Hartman read_unlock(&tasklist_lock); 177396fd7ce5SGreg Kroah-Hartman } 177496fd7ce5SGreg Kroah-Hartman 1775324c1650SPeter Hurley /* check whether both sides are closing ... */ 17767ffb6da9SPeter Hurley final = !tty->count && !(o_tty && o_tty->count); 1777324c1650SPeter Hurley 17782aff5e2bSPeter Hurley tty_unlock_slave(o_tty); 17792aff5e2bSPeter Hurley tty_unlock(tty); 17802aff5e2bSPeter Hurley 178104980706SPeter Hurley /* At this point, the tty->count == 0 should ensure a dead tty 1782d155255aSAlan Cox cannot be re-opened by a racing opener */ 178396fd7ce5SGreg Kroah-Hartman 1784324c1650SPeter Hurley if (!final) 178596fd7ce5SGreg Kroah-Hartman return 0; 178696fd7ce5SGreg Kroah-Hartman 1787accff793SPeter Hurley tty_debug_hangup(tty, "final close\n"); 1788a2965b7bSPeter Hurley 17899ed90d20SRob Herring tty_release_struct(tty, idx); 179096fd7ce5SGreg Kroah-Hartman return 0; 179196fd7ce5SGreg Kroah-Hartman } 179296fd7ce5SGreg Kroah-Hartman 179396fd7ce5SGreg Kroah-Hartman /** 179452494eebSPeter Hurley * tty_open_current_tty - get locked tty of current task 1795b82154acSJiri Slaby * @device: device number 1796b82154acSJiri Slaby * @filp: file pointer to tty 179752494eebSPeter Hurley * @return: locked tty of the current task iff @device is /dev/tty 179852494eebSPeter Hurley * 179952494eebSPeter Hurley * Performs a re-open of the current task's controlling tty. 1800b82154acSJiri Slaby * 1801b82154acSJiri Slaby * We cannot return driver and index like for the other nodes because 1802b82154acSJiri Slaby * devpts will not work then. It expects inodes to be from devpts FS. 1803b82154acSJiri Slaby */ 1804b82154acSJiri Slaby static struct tty_struct *tty_open_current_tty(dev_t device, struct file *filp) 1805b82154acSJiri Slaby { 1806b82154acSJiri Slaby struct tty_struct *tty; 180752494eebSPeter Hurley int retval; 1808b82154acSJiri Slaby 1809b82154acSJiri Slaby if (device != MKDEV(TTYAUX_MAJOR, 0)) 1810b82154acSJiri Slaby return NULL; 1811b82154acSJiri Slaby 1812b82154acSJiri Slaby tty = get_current_tty(); 1813b82154acSJiri Slaby if (!tty) 1814b82154acSJiri Slaby return ERR_PTR(-ENXIO); 1815b82154acSJiri Slaby 1816b82154acSJiri Slaby filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */ 1817b82154acSJiri Slaby /* noctty = 1; */ 181852494eebSPeter Hurley tty_lock(tty); 181952494eebSPeter Hurley tty_kref_put(tty); /* safe to drop the kref now */ 182052494eebSPeter Hurley 182152494eebSPeter Hurley retval = tty_reopen(tty); 182252494eebSPeter Hurley if (retval < 0) { 182352494eebSPeter Hurley tty_unlock(tty); 182452494eebSPeter Hurley tty = ERR_PTR(retval); 182552494eebSPeter Hurley } 1826b82154acSJiri Slaby return tty; 1827b82154acSJiri Slaby } 1828b82154acSJiri Slaby 1829b82154acSJiri Slaby /** 18305b5e7040SJiri Slaby * tty_lookup_driver - lookup a tty driver for a given device file 18315b5e7040SJiri Slaby * @device: device number 18325b5e7040SJiri Slaby * @filp: file pointer to tty 18335b5e7040SJiri Slaby * @index: index for the device in the @return driver 18345b5e7040SJiri Slaby * @return: driver for this inode (with increased refcount) 18355b5e7040SJiri Slaby * 18365b5e7040SJiri Slaby * If @return is not erroneous, the caller is responsible to decrement the 18375b5e7040SJiri Slaby * refcount by tty_driver_kref_put. 18385b5e7040SJiri Slaby * 18395b5e7040SJiri Slaby * Locking: tty_mutex protects get_tty_driver 18405b5e7040SJiri Slaby */ 18415b5e7040SJiri Slaby static struct tty_driver *tty_lookup_driver(dev_t device, struct file *filp, 184211e1d4aaSPeter Hurley int *index) 18435b5e7040SJiri Slaby { 18448c8af41dSLin Yi struct tty_driver *driver = NULL; 18455b5e7040SJiri Slaby 18462cd0050cSJiri Slaby switch (device) { 18475b5e7040SJiri Slaby #ifdef CONFIG_VT 18482cd0050cSJiri Slaby case MKDEV(TTY_MAJOR, 0): { 18495b5e7040SJiri Slaby extern struct tty_driver *console_driver; 18505b5e7040SJiri Slaby driver = tty_driver_kref_get(console_driver); 18515b5e7040SJiri Slaby *index = fg_console; 18522cd0050cSJiri Slaby break; 18535b5e7040SJiri Slaby } 18545b5e7040SJiri Slaby #endif 18552cd0050cSJiri Slaby case MKDEV(TTYAUX_MAJOR, 1): { 18565b5e7040SJiri Slaby struct tty_driver *console_driver = console_device(index); 18575b5e7040SJiri Slaby if (console_driver) { 18585b5e7040SJiri Slaby driver = tty_driver_kref_get(console_driver); 185912e84c71SOkash Khawaja if (driver && filp) { 18605b5e7040SJiri Slaby /* Don't let /dev/console block */ 18615b5e7040SJiri Slaby filp->f_flags |= O_NONBLOCK; 18622cd0050cSJiri Slaby break; 18635b5e7040SJiri Slaby } 18645b5e7040SJiri Slaby } 18658c8af41dSLin Yi if (driver) 18668c8af41dSLin Yi tty_driver_kref_put(driver); 18675b5e7040SJiri Slaby return ERR_PTR(-ENODEV); 18685b5e7040SJiri Slaby } 18692cd0050cSJiri Slaby default: 18705b5e7040SJiri Slaby driver = get_tty_driver(device, index); 18715b5e7040SJiri Slaby if (!driver) 18725b5e7040SJiri Slaby return ERR_PTR(-ENODEV); 18732cd0050cSJiri Slaby break; 18742cd0050cSJiri Slaby } 18755b5e7040SJiri Slaby return driver; 18765b5e7040SJiri Slaby } 18775b5e7040SJiri Slaby 18785b5e7040SJiri Slaby /** 1879a09ac397SOkash Khawaja * tty_kopen - open a tty device for kernel 1880a09ac397SOkash Khawaja * @device: dev_t of device to open 1881a09ac397SOkash Khawaja * 1882a09ac397SOkash Khawaja * Opens tty exclusively for kernel. Performs the driver lookup, 1883a09ac397SOkash Khawaja * makes sure it's not already opened and performs the first-time 1884a09ac397SOkash Khawaja * tty initialization. 1885a09ac397SOkash Khawaja * 1886a09ac397SOkash Khawaja * Returns the locked initialized &tty_struct 1887a09ac397SOkash Khawaja * 1888a09ac397SOkash Khawaja * Claims the global tty_mutex to serialize: 1889a09ac397SOkash Khawaja * - concurrent first-time tty initialization 1890a09ac397SOkash Khawaja * - concurrent tty driver removal w/ lookup 1891a09ac397SOkash Khawaja * - concurrent tty removal from driver table 1892a09ac397SOkash Khawaja */ 1893a09ac397SOkash Khawaja struct tty_struct *tty_kopen(dev_t device) 1894a09ac397SOkash Khawaja { 1895a09ac397SOkash Khawaja struct tty_struct *tty; 1896f1d31743SUwe Kleine-König struct tty_driver *driver; 1897a09ac397SOkash Khawaja int index = -1; 1898a09ac397SOkash Khawaja 1899a09ac397SOkash Khawaja mutex_lock(&tty_mutex); 1900a09ac397SOkash Khawaja driver = tty_lookup_driver(device, NULL, &index); 1901a09ac397SOkash Khawaja if (IS_ERR(driver)) { 1902a09ac397SOkash Khawaja mutex_unlock(&tty_mutex); 1903a09ac397SOkash Khawaja return ERR_CAST(driver); 1904a09ac397SOkash Khawaja } 1905a09ac397SOkash Khawaja 1906a09ac397SOkash Khawaja /* check whether we're reopening an existing tty */ 1907a09ac397SOkash Khawaja tty = tty_driver_lookup_tty(driver, NULL, index); 1908a09ac397SOkash Khawaja if (IS_ERR(tty)) 1909a09ac397SOkash Khawaja goto out; 1910a09ac397SOkash Khawaja 1911a09ac397SOkash Khawaja if (tty) { 1912a09ac397SOkash Khawaja /* drop kref from tty_driver_lookup_tty() */ 1913a09ac397SOkash Khawaja tty_kref_put(tty); 1914a09ac397SOkash Khawaja tty = ERR_PTR(-EBUSY); 1915a09ac397SOkash Khawaja } else { /* tty_init_dev returns tty with the tty_lock held */ 1916a09ac397SOkash Khawaja tty = tty_init_dev(driver, index); 1917a09ac397SOkash Khawaja if (IS_ERR(tty)) 1918a09ac397SOkash Khawaja goto out; 1919a09ac397SOkash Khawaja tty_port_set_kopened(tty->port, 1); 1920a09ac397SOkash Khawaja } 1921a09ac397SOkash Khawaja out: 1922a09ac397SOkash Khawaja mutex_unlock(&tty_mutex); 1923a09ac397SOkash Khawaja tty_driver_kref_put(driver); 1924a09ac397SOkash Khawaja return tty; 1925a09ac397SOkash Khawaja } 1926a09ac397SOkash Khawaja EXPORT_SYMBOL_GPL(tty_kopen); 1927a09ac397SOkash Khawaja 1928a09ac397SOkash Khawaja /** 1929d6203d0cSPeter Hurley * tty_open_by_driver - open a tty device 1930d6203d0cSPeter Hurley * @device: dev_t of device to open 1931d6203d0cSPeter Hurley * @filp: file pointer to tty 1932d6203d0cSPeter Hurley * 1933d6203d0cSPeter Hurley * Performs the driver lookup, checks for a reopen, or otherwise 1934d6203d0cSPeter Hurley * performs the first-time tty initialization. 1935d6203d0cSPeter Hurley * 1936d6203d0cSPeter Hurley * Returns the locked initialized or re-opened &tty_struct 1937d6203d0cSPeter Hurley * 1938d6203d0cSPeter Hurley * Claims the global tty_mutex to serialize: 1939d6203d0cSPeter Hurley * - concurrent first-time tty initialization 1940d6203d0cSPeter Hurley * - concurrent tty driver removal w/ lookup 1941d6203d0cSPeter Hurley * - concurrent tty removal from driver table 1942d6203d0cSPeter Hurley */ 194314ce3848SSudip Mukherjee static struct tty_struct *tty_open_by_driver(dev_t device, 1944d6203d0cSPeter Hurley struct file *filp) 1945d6203d0cSPeter Hurley { 1946d6203d0cSPeter Hurley struct tty_struct *tty; 1947d6203d0cSPeter Hurley struct tty_driver *driver = NULL; 1948d6203d0cSPeter Hurley int index = -1; 1949d6203d0cSPeter Hurley int retval; 1950d6203d0cSPeter Hurley 1951d6203d0cSPeter Hurley mutex_lock(&tty_mutex); 1952d6203d0cSPeter Hurley driver = tty_lookup_driver(device, filp, &index); 1953d6203d0cSPeter Hurley if (IS_ERR(driver)) { 1954d6203d0cSPeter Hurley mutex_unlock(&tty_mutex); 1955d6203d0cSPeter Hurley return ERR_CAST(driver); 1956d6203d0cSPeter Hurley } 1957d6203d0cSPeter Hurley 1958d6203d0cSPeter Hurley /* check whether we're reopening an existing tty */ 19598ead9dd5SLinus Torvalds tty = tty_driver_lookup_tty(driver, filp, index); 1960d6203d0cSPeter Hurley if (IS_ERR(tty)) { 1961d6203d0cSPeter Hurley mutex_unlock(&tty_mutex); 1962d6203d0cSPeter Hurley goto out; 1963d6203d0cSPeter Hurley } 1964d6203d0cSPeter Hurley 1965d6203d0cSPeter Hurley if (tty) { 1966a09ac397SOkash Khawaja if (tty_port_kopened(tty->port)) { 1967a09ac397SOkash Khawaja tty_kref_put(tty); 1968a09ac397SOkash Khawaja mutex_unlock(&tty_mutex); 1969a09ac397SOkash Khawaja tty = ERR_PTR(-EBUSY); 1970a09ac397SOkash Khawaja goto out; 1971a09ac397SOkash Khawaja } 1972d6203d0cSPeter Hurley mutex_unlock(&tty_mutex); 1973d6203d0cSPeter Hurley retval = tty_lock_interruptible(tty); 19745e00bbfbSPeter Hurley tty_kref_put(tty); /* drop kref from tty_driver_lookup_tty() */ 1975d6203d0cSPeter Hurley if (retval) { 1976d6203d0cSPeter Hurley if (retval == -EINTR) 1977d6203d0cSPeter Hurley retval = -ERESTARTSYS; 1978d6203d0cSPeter Hurley tty = ERR_PTR(retval); 1979d6203d0cSPeter Hurley goto out; 1980d6203d0cSPeter Hurley } 1981d6203d0cSPeter Hurley retval = tty_reopen(tty); 1982d6203d0cSPeter Hurley if (retval < 0) { 1983d6203d0cSPeter Hurley tty_unlock(tty); 1984d6203d0cSPeter Hurley tty = ERR_PTR(retval); 1985d6203d0cSPeter Hurley } 1986d6203d0cSPeter Hurley } else { /* Returns with the tty_lock held for now */ 1987d6203d0cSPeter Hurley tty = tty_init_dev(driver, index); 1988d6203d0cSPeter Hurley mutex_unlock(&tty_mutex); 1989d6203d0cSPeter Hurley } 1990d6203d0cSPeter Hurley out: 1991d6203d0cSPeter Hurley tty_driver_kref_put(driver); 1992d6203d0cSPeter Hurley return tty; 1993d6203d0cSPeter Hurley } 1994d6203d0cSPeter Hurley 1995d6203d0cSPeter Hurley /** 199696fd7ce5SGreg Kroah-Hartman * tty_open - open a tty device 199796fd7ce5SGreg Kroah-Hartman * @inode: inode of device file 199896fd7ce5SGreg Kroah-Hartman * @filp: file pointer to tty 199996fd7ce5SGreg Kroah-Hartman * 200096fd7ce5SGreg Kroah-Hartman * tty_open and tty_release keep up the tty count that contains the 200196fd7ce5SGreg Kroah-Hartman * number of opens done on a tty. We cannot use the inode-count, as 200296fd7ce5SGreg Kroah-Hartman * different inodes might point to the same tty. 200396fd7ce5SGreg Kroah-Hartman * 200496fd7ce5SGreg Kroah-Hartman * Open-counting is needed for pty masters, as well as for keeping 200596fd7ce5SGreg Kroah-Hartman * track of serial lines: DTR is dropped when the last close happens. 200696fd7ce5SGreg Kroah-Hartman * (This is not done solely through tty->count, now. - Ted 1/27/92) 200796fd7ce5SGreg Kroah-Hartman * 200896fd7ce5SGreg Kroah-Hartman * The termios state of a pty is reset on first open so that 200996fd7ce5SGreg Kroah-Hartman * settings don't persist across reuse. 201096fd7ce5SGreg Kroah-Hartman * 20115b5e7040SJiri Slaby * Locking: tty_mutex protects tty, tty_lookup_driver and tty_init_dev. 201296fd7ce5SGreg Kroah-Hartman * tty->count should protect the rest. 201396fd7ce5SGreg Kroah-Hartman * ->siglock protects ->signal/->sighand 201489c8d91eSAlan Cox * 201589c8d91eSAlan Cox * Note: the tty_unlock/lock cases without a ref are only safe due to 201689c8d91eSAlan Cox * tty_mutex 201796fd7ce5SGreg Kroah-Hartman */ 201896fd7ce5SGreg Kroah-Hartman 201996fd7ce5SGreg Kroah-Hartman static int tty_open(struct inode *inode, struct file *filp) 202096fd7ce5SGreg Kroah-Hartman { 2021b82154acSJiri Slaby struct tty_struct *tty; 202296fd7ce5SGreg Kroah-Hartman int noctty, retval; 202396fd7ce5SGreg Kroah-Hartman dev_t device = inode->i_rdev; 202496fd7ce5SGreg Kroah-Hartman unsigned saved_flags = filp->f_flags; 202596fd7ce5SGreg Kroah-Hartman 202696fd7ce5SGreg Kroah-Hartman nonseekable_open(inode, filp); 202796fd7ce5SGreg Kroah-Hartman 202896fd7ce5SGreg Kroah-Hartman retry_open: 2029fa90e1c9SJiri Slaby retval = tty_alloc_file(filp); 2030fa90e1c9SJiri Slaby if (retval) 2031fa90e1c9SJiri Slaby return -ENOMEM; 2032fa90e1c9SJiri Slaby 2033b82154acSJiri Slaby tty = tty_open_current_tty(device, filp); 2034d6203d0cSPeter Hurley if (!tty) 203514ce3848SSudip Mukherjee tty = tty_open_by_driver(device, filp); 203652494eebSPeter Hurley 203796fd7ce5SGreg Kroah-Hartman if (IS_ERR(tty)) { 2038d6203d0cSPeter Hurley tty_free_file(filp); 2039ba5db448SJiri Slaby retval = PTR_ERR(tty); 20407f22f6c9SPeter Hurley if (retval != -EAGAIN || signal_pending(current)) 2041d6203d0cSPeter Hurley return retval; 20427f22f6c9SPeter Hurley schedule(); 20437f22f6c9SPeter Hurley goto retry_open; 204496fd7ce5SGreg Kroah-Hartman } 204596fd7ce5SGreg Kroah-Hartman 2046fa90e1c9SJiri Slaby tty_add_file(tty, filp); 204796fd7ce5SGreg Kroah-Hartman 20489de44bd6SJiri Slaby check_tty_count(tty, __func__); 2049d435cefeSPeter Hurley tty_debug_hangup(tty, "opening (count=%d)\n", tty->count); 2050accff793SPeter Hurley 205196fd7ce5SGreg Kroah-Hartman if (tty->ops->open) 205296fd7ce5SGreg Kroah-Hartman retval = tty->ops->open(tty, filp); 205396fd7ce5SGreg Kroah-Hartman else 205496fd7ce5SGreg Kroah-Hartman retval = -ENODEV; 205596fd7ce5SGreg Kroah-Hartman filp->f_flags = saved_flags; 205696fd7ce5SGreg Kroah-Hartman 205796fd7ce5SGreg Kroah-Hartman if (retval) { 2058d435cefeSPeter Hurley tty_debug_hangup(tty, "open error %d, releasing\n", retval); 2059accff793SPeter Hurley 206089c8d91eSAlan Cox tty_unlock(tty); /* need to call tty_release without BTM */ 206196fd7ce5SGreg Kroah-Hartman tty_release(inode, filp); 206296fd7ce5SGreg Kroah-Hartman if (retval != -ERESTARTSYS) 206396fd7ce5SGreg Kroah-Hartman return retval; 206496fd7ce5SGreg Kroah-Hartman 206596fd7ce5SGreg Kroah-Hartman if (signal_pending(current)) 206696fd7ce5SGreg Kroah-Hartman return retval; 206796fd7ce5SGreg Kroah-Hartman 206896fd7ce5SGreg Kroah-Hartman schedule(); 206996fd7ce5SGreg Kroah-Hartman /* 207096fd7ce5SGreg Kroah-Hartman * Need to reset f_op in case a hangup happened. 207196fd7ce5SGreg Kroah-Hartman */ 20721256937fSPeter Hurley if (tty_hung_up_p(filp)) 207396fd7ce5SGreg Kroah-Hartman filp->f_op = &tty_fops; 207496fd7ce5SGreg Kroah-Hartman goto retry_open; 207596fd7ce5SGreg Kroah-Hartman } 2076d4855e1fSPeter Hurley clear_bit(TTY_HUPPED, &tty->flags); 207796fd7ce5SGreg Kroah-Hartman 207811e1d4aaSPeter Hurley noctty = (filp->f_flags & O_NOCTTY) || 2079da5a0fc6SPeter Hurley (IS_ENABLED(CONFIG_VT) && device == MKDEV(TTY_MAJOR, 0)) || 208011e1d4aaSPeter Hurley device == MKDEV(TTYAUX_MAJOR, 1) || 208111e1d4aaSPeter Hurley (tty->driver->type == TTY_DRIVER_TYPE_PTY && 208211e1d4aaSPeter Hurley tty->driver->subtype == PTY_TYPE_MASTER); 2083a1235b3eSNicolas Pitre if (!noctty) 2084a1235b3eSNicolas Pitre tty_open_proc_set_tty(filp, tty); 208589c8d91eSAlan Cox tty_unlock(tty); 208696fd7ce5SGreg Kroah-Hartman return 0; 208796fd7ce5SGreg Kroah-Hartman } 208896fd7ce5SGreg Kroah-Hartman 208996fd7ce5SGreg Kroah-Hartman 209096fd7ce5SGreg Kroah-Hartman 209196fd7ce5SGreg Kroah-Hartman /** 209296fd7ce5SGreg Kroah-Hartman * tty_poll - check tty status 209396fd7ce5SGreg Kroah-Hartman * @filp: file being polled 209496fd7ce5SGreg Kroah-Hartman * @wait: poll wait structures to update 209596fd7ce5SGreg Kroah-Hartman * 209696fd7ce5SGreg Kroah-Hartman * Call the line discipline polling method to obtain the poll 209796fd7ce5SGreg Kroah-Hartman * status of the device. 209896fd7ce5SGreg Kroah-Hartman * 209996fd7ce5SGreg Kroah-Hartman * Locking: locks called line discipline but ldisc poll method 210096fd7ce5SGreg Kroah-Hartman * may be re-entered freely by other callers. 210196fd7ce5SGreg Kroah-Hartman */ 210296fd7ce5SGreg Kroah-Hartman 2103afc9a42bSAl Viro static __poll_t tty_poll(struct file *filp, poll_table *wait) 210496fd7ce5SGreg Kroah-Hartman { 210596fd7ce5SGreg Kroah-Hartman struct tty_struct *tty = file_tty(filp); 210696fd7ce5SGreg Kroah-Hartman struct tty_ldisc *ld; 2107e6c8adcaSAl Viro __poll_t ret = 0; 210896fd7ce5SGreg Kroah-Hartman 21096131ffaaSAl Viro if (tty_paranoia_check(tty, file_inode(filp), "tty_poll")) 211096fd7ce5SGreg Kroah-Hartman return 0; 211196fd7ce5SGreg Kroah-Hartman 211296fd7ce5SGreg Kroah-Hartman ld = tty_ldisc_ref_wait(tty); 2113e55afd11SPeter Hurley if (!ld) 2114e55afd11SPeter Hurley return hung_up_tty_poll(filp, wait); 211596fd7ce5SGreg Kroah-Hartman if (ld->ops->poll) 2116c961bfb1SPeter Hurley ret = ld->ops->poll(tty, filp, wait); 211796fd7ce5SGreg Kroah-Hartman tty_ldisc_deref(ld); 211896fd7ce5SGreg Kroah-Hartman return ret; 211996fd7ce5SGreg Kroah-Hartman } 212096fd7ce5SGreg Kroah-Hartman 212196fd7ce5SGreg Kroah-Hartman static int __tty_fasync(int fd, struct file *filp, int on) 212296fd7ce5SGreg Kroah-Hartman { 212396fd7ce5SGreg Kroah-Hartman struct tty_struct *tty = file_tty(filp); 212496fd7ce5SGreg Kroah-Hartman unsigned long flags; 212596fd7ce5SGreg Kroah-Hartman int retval = 0; 212696fd7ce5SGreg Kroah-Hartman 21276131ffaaSAl Viro if (tty_paranoia_check(tty, file_inode(filp), "tty_fasync")) 212896fd7ce5SGreg Kroah-Hartman goto out; 212996fd7ce5SGreg Kroah-Hartman 213096fd7ce5SGreg Kroah-Hartman retval = fasync_helper(fd, filp, on, &tty->fasync); 213196fd7ce5SGreg Kroah-Hartman if (retval <= 0) 213296fd7ce5SGreg Kroah-Hartman goto out; 213396fd7ce5SGreg Kroah-Hartman 213496fd7ce5SGreg Kroah-Hartman if (on) { 213596fd7ce5SGreg Kroah-Hartman enum pid_type type; 213696fd7ce5SGreg Kroah-Hartman struct pid *pid; 2137f6c8dbe6SPeter Hurley 213896fd7ce5SGreg Kroah-Hartman spin_lock_irqsave(&tty->ctrl_lock, flags); 213996fd7ce5SGreg Kroah-Hartman if (tty->pgrp) { 214096fd7ce5SGreg Kroah-Hartman pid = tty->pgrp; 214196fd7ce5SGreg Kroah-Hartman type = PIDTYPE_PGID; 214296fd7ce5SGreg Kroah-Hartman } else { 214396fd7ce5SGreg Kroah-Hartman pid = task_pid(current); 214401919134SEric W. Biederman type = PIDTYPE_TGID; 214596fd7ce5SGreg Kroah-Hartman } 214696fd7ce5SGreg Kroah-Hartman get_pid(pid); 214796fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&tty->ctrl_lock, flags); 2148e0b93eddSJeff Layton __f_setown(filp, pid, type, 0); 214996fd7ce5SGreg Kroah-Hartman put_pid(pid); 2150e0b93eddSJeff Layton retval = 0; 215196fd7ce5SGreg Kroah-Hartman } 215296fd7ce5SGreg Kroah-Hartman out: 215396fd7ce5SGreg Kroah-Hartman return retval; 215496fd7ce5SGreg Kroah-Hartman } 215596fd7ce5SGreg Kroah-Hartman 215696fd7ce5SGreg Kroah-Hartman static int tty_fasync(int fd, struct file *filp, int on) 215796fd7ce5SGreg Kroah-Hartman { 215889c8d91eSAlan Cox struct tty_struct *tty = file_tty(filp); 2159a8f3a297SPeter Hurley int retval = -ENOTTY; 216089c8d91eSAlan Cox 216189c8d91eSAlan Cox tty_lock(tty); 2162a8f3a297SPeter Hurley if (!tty_hung_up_p(filp)) 216396fd7ce5SGreg Kroah-Hartman retval = __tty_fasync(fd, filp, on); 216489c8d91eSAlan Cox tty_unlock(tty); 216589c8d91eSAlan Cox 216696fd7ce5SGreg Kroah-Hartman return retval; 216796fd7ce5SGreg Kroah-Hartman } 216896fd7ce5SGreg Kroah-Hartman 216996fd7ce5SGreg Kroah-Hartman /** 217096fd7ce5SGreg Kroah-Hartman * tiocsti - fake input character 217196fd7ce5SGreg Kroah-Hartman * @tty: tty to fake input into 217296fd7ce5SGreg Kroah-Hartman * @p: pointer to character 217396fd7ce5SGreg Kroah-Hartman * 217496fd7ce5SGreg Kroah-Hartman * Fake input to a tty device. Does the necessary locking and 217596fd7ce5SGreg Kroah-Hartman * input management. 217696fd7ce5SGreg Kroah-Hartman * 217796fd7ce5SGreg Kroah-Hartman * FIXME: does not honour flow control ?? 217896fd7ce5SGreg Kroah-Hartman * 217996fd7ce5SGreg Kroah-Hartman * Locking: 2180137084bbSPeter Hurley * Called functions take tty_ldiscs_lock 218196fd7ce5SGreg Kroah-Hartman * current->signal->tty check is safe without locks 218296fd7ce5SGreg Kroah-Hartman * 218396fd7ce5SGreg Kroah-Hartman * FIXME: may race normal receive processing 218496fd7ce5SGreg Kroah-Hartman */ 218596fd7ce5SGreg Kroah-Hartman 218696fd7ce5SGreg Kroah-Hartman static int tiocsti(struct tty_struct *tty, char __user *p) 218796fd7ce5SGreg Kroah-Hartman { 218896fd7ce5SGreg Kroah-Hartman char ch, mbz = 0; 218996fd7ce5SGreg Kroah-Hartman struct tty_ldisc *ld; 219096fd7ce5SGreg Kroah-Hartman 219196fd7ce5SGreg Kroah-Hartman if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN)) 219296fd7ce5SGreg Kroah-Hartman return -EPERM; 219396fd7ce5SGreg Kroah-Hartman if (get_user(ch, p)) 219496fd7ce5SGreg Kroah-Hartman return -EFAULT; 219596fd7ce5SGreg Kroah-Hartman tty_audit_tiocsti(tty, ch); 219696fd7ce5SGreg Kroah-Hartman ld = tty_ldisc_ref_wait(tty); 2197e55afd11SPeter Hurley if (!ld) 2198e55afd11SPeter Hurley return -EIO; 219927cfb3a5SGreg Kroah-Hartman if (ld->ops->receive_buf) 220096fd7ce5SGreg Kroah-Hartman ld->ops->receive_buf(tty, &ch, &mbz, 1); 220196fd7ce5SGreg Kroah-Hartman tty_ldisc_deref(ld); 220296fd7ce5SGreg Kroah-Hartman return 0; 220396fd7ce5SGreg Kroah-Hartman } 220496fd7ce5SGreg Kroah-Hartman 220596fd7ce5SGreg Kroah-Hartman /** 220696fd7ce5SGreg Kroah-Hartman * tiocgwinsz - implement window query ioctl 220796fd7ce5SGreg Kroah-Hartman * @tty; tty 220896fd7ce5SGreg Kroah-Hartman * @arg: user buffer for result 220996fd7ce5SGreg Kroah-Hartman * 221096fd7ce5SGreg Kroah-Hartman * Copies the kernel idea of the window size into the user buffer. 221196fd7ce5SGreg Kroah-Hartman * 2212dee4a0beSPeter Hurley * Locking: tty->winsize_mutex is taken to ensure the winsize data 221396fd7ce5SGreg Kroah-Hartman * is consistent. 221496fd7ce5SGreg Kroah-Hartman */ 221596fd7ce5SGreg Kroah-Hartman 221696fd7ce5SGreg Kroah-Hartman static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg) 221796fd7ce5SGreg Kroah-Hartman { 221896fd7ce5SGreg Kroah-Hartman int err; 221996fd7ce5SGreg Kroah-Hartman 2220dee4a0beSPeter Hurley mutex_lock(&tty->winsize_mutex); 222196fd7ce5SGreg Kroah-Hartman err = copy_to_user(arg, &tty->winsize, sizeof(*arg)); 2222dee4a0beSPeter Hurley mutex_unlock(&tty->winsize_mutex); 222396fd7ce5SGreg Kroah-Hartman 222496fd7ce5SGreg Kroah-Hartman return err ? -EFAULT: 0; 222596fd7ce5SGreg Kroah-Hartman } 222696fd7ce5SGreg Kroah-Hartman 222796fd7ce5SGreg Kroah-Hartman /** 222896fd7ce5SGreg Kroah-Hartman * tty_do_resize - resize event 222996fd7ce5SGreg Kroah-Hartman * @tty: tty being resized 223096fd7ce5SGreg Kroah-Hartman * @rows: rows (character) 223196fd7ce5SGreg Kroah-Hartman * @cols: cols (character) 223296fd7ce5SGreg Kroah-Hartman * 223396fd7ce5SGreg Kroah-Hartman * Update the termios variables and send the necessary signals to 223496fd7ce5SGreg Kroah-Hartman * peform a terminal resize correctly 223596fd7ce5SGreg Kroah-Hartman */ 223696fd7ce5SGreg Kroah-Hartman 223796fd7ce5SGreg Kroah-Hartman int tty_do_resize(struct tty_struct *tty, struct winsize *ws) 223896fd7ce5SGreg Kroah-Hartman { 223996fd7ce5SGreg Kroah-Hartman struct pid *pgrp; 224096fd7ce5SGreg Kroah-Hartman 224196fd7ce5SGreg Kroah-Hartman /* Lock the tty */ 2242dee4a0beSPeter Hurley mutex_lock(&tty->winsize_mutex); 224396fd7ce5SGreg Kroah-Hartman if (!memcmp(ws, &tty->winsize, sizeof(*ws))) 224496fd7ce5SGreg Kroah-Hartman goto done; 224596fd7ce5SGreg Kroah-Hartman 22465b239542SPeter Hurley /* Signal the foreground process group */ 22475b239542SPeter Hurley pgrp = tty_get_pgrp(tty); 224896fd7ce5SGreg Kroah-Hartman if (pgrp) 224996fd7ce5SGreg Kroah-Hartman kill_pgrp(pgrp, SIGWINCH, 1); 225096fd7ce5SGreg Kroah-Hartman put_pid(pgrp); 225196fd7ce5SGreg Kroah-Hartman 225296fd7ce5SGreg Kroah-Hartman tty->winsize = *ws; 225396fd7ce5SGreg Kroah-Hartman done: 2254dee4a0beSPeter Hurley mutex_unlock(&tty->winsize_mutex); 225596fd7ce5SGreg Kroah-Hartman return 0; 225696fd7ce5SGreg Kroah-Hartman } 22574d334fd1SMartin Schwidefsky EXPORT_SYMBOL(tty_do_resize); 225896fd7ce5SGreg Kroah-Hartman 225996fd7ce5SGreg Kroah-Hartman /** 226096fd7ce5SGreg Kroah-Hartman * tiocswinsz - implement window size set ioctl 226196fd7ce5SGreg Kroah-Hartman * @tty; tty side of tty 226296fd7ce5SGreg Kroah-Hartman * @arg: user buffer for result 226396fd7ce5SGreg Kroah-Hartman * 226496fd7ce5SGreg Kroah-Hartman * Copies the user idea of the window size to the kernel. Traditionally 226596fd7ce5SGreg Kroah-Hartman * this is just advisory information but for the Linux console it 226696fd7ce5SGreg Kroah-Hartman * actually has driver level meaning and triggers a VC resize. 226796fd7ce5SGreg Kroah-Hartman * 226896fd7ce5SGreg Kroah-Hartman * Locking: 226925985edcSLucas De Marchi * Driver dependent. The default do_resize method takes the 227096fd7ce5SGreg Kroah-Hartman * tty termios mutex and ctrl_lock. The console takes its own lock 227196fd7ce5SGreg Kroah-Hartman * then calls into the default method. 227296fd7ce5SGreg Kroah-Hartman */ 227396fd7ce5SGreg Kroah-Hartman 227496fd7ce5SGreg Kroah-Hartman static int tiocswinsz(struct tty_struct *tty, struct winsize __user *arg) 227596fd7ce5SGreg Kroah-Hartman { 227696fd7ce5SGreg Kroah-Hartman struct winsize tmp_ws; 227796fd7ce5SGreg Kroah-Hartman if (copy_from_user(&tmp_ws, arg, sizeof(*arg))) 227896fd7ce5SGreg Kroah-Hartman return -EFAULT; 227996fd7ce5SGreg Kroah-Hartman 228096fd7ce5SGreg Kroah-Hartman if (tty->ops->resize) 228196fd7ce5SGreg Kroah-Hartman return tty->ops->resize(tty, &tmp_ws); 228296fd7ce5SGreg Kroah-Hartman else 228396fd7ce5SGreg Kroah-Hartman return tty_do_resize(tty, &tmp_ws); 228496fd7ce5SGreg Kroah-Hartman } 228596fd7ce5SGreg Kroah-Hartman 228696fd7ce5SGreg Kroah-Hartman /** 228796fd7ce5SGreg Kroah-Hartman * tioccons - allow admin to move logical console 228896fd7ce5SGreg Kroah-Hartman * @file: the file to become console 228996fd7ce5SGreg Kroah-Hartman * 229025985edcSLucas De Marchi * Allow the administrator to move the redirected console device 229196fd7ce5SGreg Kroah-Hartman * 229296fd7ce5SGreg Kroah-Hartman * Locking: uses redirect_lock to guard the redirect information 229396fd7ce5SGreg Kroah-Hartman */ 229496fd7ce5SGreg Kroah-Hartman 229596fd7ce5SGreg Kroah-Hartman static int tioccons(struct file *file) 229696fd7ce5SGreg Kroah-Hartman { 229796fd7ce5SGreg Kroah-Hartman if (!capable(CAP_SYS_ADMIN)) 229896fd7ce5SGreg Kroah-Hartman return -EPERM; 229996fd7ce5SGreg Kroah-Hartman if (file->f_op->write == redirected_tty_write) { 230096fd7ce5SGreg Kroah-Hartman struct file *f; 230196fd7ce5SGreg Kroah-Hartman spin_lock(&redirect_lock); 230296fd7ce5SGreg Kroah-Hartman f = redirect; 230396fd7ce5SGreg Kroah-Hartman redirect = NULL; 230496fd7ce5SGreg Kroah-Hartman spin_unlock(&redirect_lock); 230596fd7ce5SGreg Kroah-Hartman if (f) 230696fd7ce5SGreg Kroah-Hartman fput(f); 230796fd7ce5SGreg Kroah-Hartman return 0; 230896fd7ce5SGreg Kroah-Hartman } 230996fd7ce5SGreg Kroah-Hartman spin_lock(&redirect_lock); 231096fd7ce5SGreg Kroah-Hartman if (redirect) { 231196fd7ce5SGreg Kroah-Hartman spin_unlock(&redirect_lock); 231296fd7ce5SGreg Kroah-Hartman return -EBUSY; 231396fd7ce5SGreg Kroah-Hartman } 2314cb0942b8SAl Viro redirect = get_file(file); 231596fd7ce5SGreg Kroah-Hartman spin_unlock(&redirect_lock); 231696fd7ce5SGreg Kroah-Hartman return 0; 231796fd7ce5SGreg Kroah-Hartman } 231896fd7ce5SGreg Kroah-Hartman 231996fd7ce5SGreg Kroah-Hartman /** 232096fd7ce5SGreg Kroah-Hartman * tiocsetd - set line discipline 232196fd7ce5SGreg Kroah-Hartman * @tty: tty device 232296fd7ce5SGreg Kroah-Hartman * @p: pointer to user data 232396fd7ce5SGreg Kroah-Hartman * 232496fd7ce5SGreg Kroah-Hartman * Set the line discipline according to user request. 232596fd7ce5SGreg Kroah-Hartman * 232696fd7ce5SGreg Kroah-Hartman * Locking: see tty_set_ldisc, this function is just a helper 232796fd7ce5SGreg Kroah-Hartman */ 232896fd7ce5SGreg Kroah-Hartman 232996fd7ce5SGreg Kroah-Hartman static int tiocsetd(struct tty_struct *tty, int __user *p) 233096fd7ce5SGreg Kroah-Hartman { 2331c12da96fSPeter Hurley int disc; 233296fd7ce5SGreg Kroah-Hartman int ret; 233396fd7ce5SGreg Kroah-Hartman 2334c12da96fSPeter Hurley if (get_user(disc, p)) 233596fd7ce5SGreg Kroah-Hartman return -EFAULT; 233696fd7ce5SGreg Kroah-Hartman 2337c12da96fSPeter Hurley ret = tty_set_ldisc(tty, disc); 233896fd7ce5SGreg Kroah-Hartman 233996fd7ce5SGreg Kroah-Hartman return ret; 234096fd7ce5SGreg Kroah-Hartman } 234196fd7ce5SGreg Kroah-Hartman 234296fd7ce5SGreg Kroah-Hartman /** 23435c17c861SPeter Hurley * tiocgetd - get line discipline 23445c17c861SPeter Hurley * @tty: tty device 23455c17c861SPeter Hurley * @p: pointer to user data 23465c17c861SPeter Hurley * 23475c17c861SPeter Hurley * Retrieves the line discipline id directly from the ldisc. 23485c17c861SPeter Hurley * 23495c17c861SPeter Hurley * Locking: waits for ldisc reference (in case the line discipline 23505c17c861SPeter Hurley * is changing or the tty is being hungup) 23515c17c861SPeter Hurley */ 23525c17c861SPeter Hurley 23535c17c861SPeter Hurley static int tiocgetd(struct tty_struct *tty, int __user *p) 23545c17c861SPeter Hurley { 23555c17c861SPeter Hurley struct tty_ldisc *ld; 23565c17c861SPeter Hurley int ret; 23575c17c861SPeter Hurley 23585c17c861SPeter Hurley ld = tty_ldisc_ref_wait(tty); 2359e55afd11SPeter Hurley if (!ld) 2360e55afd11SPeter Hurley return -EIO; 23615c17c861SPeter Hurley ret = put_user(ld->ops->num, p); 23625c17c861SPeter Hurley tty_ldisc_deref(ld); 23635c17c861SPeter Hurley return ret; 23645c17c861SPeter Hurley } 23655c17c861SPeter Hurley 23665c17c861SPeter Hurley /** 236796fd7ce5SGreg Kroah-Hartman * send_break - performed time break 236896fd7ce5SGreg Kroah-Hartman * @tty: device to break on 236996fd7ce5SGreg Kroah-Hartman * @duration: timeout in mS 237096fd7ce5SGreg Kroah-Hartman * 237196fd7ce5SGreg Kroah-Hartman * Perform a timed break on hardware that lacks its own driver level 237296fd7ce5SGreg Kroah-Hartman * timed break functionality. 237396fd7ce5SGreg Kroah-Hartman * 237496fd7ce5SGreg Kroah-Hartman * Locking: 237596fd7ce5SGreg Kroah-Hartman * atomic_write_lock serializes 237696fd7ce5SGreg Kroah-Hartman * 237796fd7ce5SGreg Kroah-Hartman */ 237896fd7ce5SGreg Kroah-Hartman 237996fd7ce5SGreg Kroah-Hartman static int send_break(struct tty_struct *tty, unsigned int duration) 238096fd7ce5SGreg Kroah-Hartman { 238196fd7ce5SGreg Kroah-Hartman int retval; 238296fd7ce5SGreg Kroah-Hartman 238396fd7ce5SGreg Kroah-Hartman if (tty->ops->break_ctl == NULL) 238496fd7ce5SGreg Kroah-Hartman return 0; 238596fd7ce5SGreg Kroah-Hartman 238696fd7ce5SGreg Kroah-Hartman if (tty->driver->flags & TTY_DRIVER_HARDWARE_BREAK) 238796fd7ce5SGreg Kroah-Hartman retval = tty->ops->break_ctl(tty, duration); 238896fd7ce5SGreg Kroah-Hartman else { 238996fd7ce5SGreg Kroah-Hartman /* Do the work ourselves */ 239096fd7ce5SGreg Kroah-Hartman if (tty_write_lock(tty, 0) < 0) 239196fd7ce5SGreg Kroah-Hartman return -EINTR; 239296fd7ce5SGreg Kroah-Hartman retval = tty->ops->break_ctl(tty, -1); 239396fd7ce5SGreg Kroah-Hartman if (retval) 239496fd7ce5SGreg Kroah-Hartman goto out; 239596fd7ce5SGreg Kroah-Hartman if (!signal_pending(current)) 239696fd7ce5SGreg Kroah-Hartman msleep_interruptible(duration); 239796fd7ce5SGreg Kroah-Hartman retval = tty->ops->break_ctl(tty, 0); 239896fd7ce5SGreg Kroah-Hartman out: 239996fd7ce5SGreg Kroah-Hartman tty_write_unlock(tty); 240096fd7ce5SGreg Kroah-Hartman if (signal_pending(current)) 240196fd7ce5SGreg Kroah-Hartman retval = -EINTR; 240296fd7ce5SGreg Kroah-Hartman } 240396fd7ce5SGreg Kroah-Hartman return retval; 240496fd7ce5SGreg Kroah-Hartman } 240596fd7ce5SGreg Kroah-Hartman 240696fd7ce5SGreg Kroah-Hartman /** 240796fd7ce5SGreg Kroah-Hartman * tty_tiocmget - get modem status 240896fd7ce5SGreg Kroah-Hartman * @tty: tty device 240996fd7ce5SGreg Kroah-Hartman * @file: user file pointer 241096fd7ce5SGreg Kroah-Hartman * @p: pointer to result 241196fd7ce5SGreg Kroah-Hartman * 241296fd7ce5SGreg Kroah-Hartman * Obtain the modem status bits from the tty driver if the feature 241396fd7ce5SGreg Kroah-Hartman * is supported. Return -EINVAL if it is not available. 241496fd7ce5SGreg Kroah-Hartman * 241596fd7ce5SGreg Kroah-Hartman * Locking: none (up to the driver) 241696fd7ce5SGreg Kroah-Hartman */ 241796fd7ce5SGreg Kroah-Hartman 241860b33c13SAlan Cox static int tty_tiocmget(struct tty_struct *tty, int __user *p) 241996fd7ce5SGreg Kroah-Hartman { 242096fd7ce5SGreg Kroah-Hartman int retval = -EINVAL; 242196fd7ce5SGreg Kroah-Hartman 242296fd7ce5SGreg Kroah-Hartman if (tty->ops->tiocmget) { 242360b33c13SAlan Cox retval = tty->ops->tiocmget(tty); 242496fd7ce5SGreg Kroah-Hartman 242596fd7ce5SGreg Kroah-Hartman if (retval >= 0) 242696fd7ce5SGreg Kroah-Hartman retval = put_user(retval, p); 242796fd7ce5SGreg Kroah-Hartman } 242896fd7ce5SGreg Kroah-Hartman return retval; 242996fd7ce5SGreg Kroah-Hartman } 243096fd7ce5SGreg Kroah-Hartman 243196fd7ce5SGreg Kroah-Hartman /** 243296fd7ce5SGreg Kroah-Hartman * tty_tiocmset - set modem status 243396fd7ce5SGreg Kroah-Hartman * @tty: tty device 243496fd7ce5SGreg Kroah-Hartman * @cmd: command - clear bits, set bits or set all 243596fd7ce5SGreg Kroah-Hartman * @p: pointer to desired bits 243696fd7ce5SGreg Kroah-Hartman * 243796fd7ce5SGreg Kroah-Hartman * Set the modem status bits from the tty driver if the feature 243896fd7ce5SGreg Kroah-Hartman * is supported. Return -EINVAL if it is not available. 243996fd7ce5SGreg Kroah-Hartman * 244096fd7ce5SGreg Kroah-Hartman * Locking: none (up to the driver) 244196fd7ce5SGreg Kroah-Hartman */ 244296fd7ce5SGreg Kroah-Hartman 244320b9d177SAlan Cox static int tty_tiocmset(struct tty_struct *tty, unsigned int cmd, 244496fd7ce5SGreg Kroah-Hartman unsigned __user *p) 244596fd7ce5SGreg Kroah-Hartman { 244696fd7ce5SGreg Kroah-Hartman int retval; 244796fd7ce5SGreg Kroah-Hartman unsigned int set, clear, val; 244896fd7ce5SGreg Kroah-Hartman 244996fd7ce5SGreg Kroah-Hartman if (tty->ops->tiocmset == NULL) 245096fd7ce5SGreg Kroah-Hartman return -EINVAL; 245196fd7ce5SGreg Kroah-Hartman 245296fd7ce5SGreg Kroah-Hartman retval = get_user(val, p); 245396fd7ce5SGreg Kroah-Hartman if (retval) 245496fd7ce5SGreg Kroah-Hartman return retval; 245596fd7ce5SGreg Kroah-Hartman set = clear = 0; 245696fd7ce5SGreg Kroah-Hartman switch (cmd) { 245796fd7ce5SGreg Kroah-Hartman case TIOCMBIS: 245896fd7ce5SGreg Kroah-Hartman set = val; 245996fd7ce5SGreg Kroah-Hartman break; 246096fd7ce5SGreg Kroah-Hartman case TIOCMBIC: 246196fd7ce5SGreg Kroah-Hartman clear = val; 246296fd7ce5SGreg Kroah-Hartman break; 246396fd7ce5SGreg Kroah-Hartman case TIOCMSET: 246496fd7ce5SGreg Kroah-Hartman set = val; 246596fd7ce5SGreg Kroah-Hartman clear = ~val; 246696fd7ce5SGreg Kroah-Hartman break; 246796fd7ce5SGreg Kroah-Hartman } 246896fd7ce5SGreg Kroah-Hartman set &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP; 246996fd7ce5SGreg Kroah-Hartman clear &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP; 247020b9d177SAlan Cox return tty->ops->tiocmset(tty, set, clear); 247196fd7ce5SGreg Kroah-Hartman } 247296fd7ce5SGreg Kroah-Hartman 247396fd7ce5SGreg Kroah-Hartman static int tty_tiocgicount(struct tty_struct *tty, void __user *arg) 247496fd7ce5SGreg Kroah-Hartman { 247596fd7ce5SGreg Kroah-Hartman int retval = -EINVAL; 247696fd7ce5SGreg Kroah-Hartman struct serial_icounter_struct icount; 247796fd7ce5SGreg Kroah-Hartman memset(&icount, 0, sizeof(icount)); 247896fd7ce5SGreg Kroah-Hartman if (tty->ops->get_icount) 247996fd7ce5SGreg Kroah-Hartman retval = tty->ops->get_icount(tty, &icount); 248096fd7ce5SGreg Kroah-Hartman if (retval != 0) 248196fd7ce5SGreg Kroah-Hartman return retval; 248296fd7ce5SGreg Kroah-Hartman if (copy_to_user(arg, &icount, sizeof(icount))) 248396fd7ce5SGreg Kroah-Hartman return -EFAULT; 248496fd7ce5SGreg Kroah-Hartman return 0; 248596fd7ce5SGreg Kroah-Hartman } 248696fd7ce5SGreg Kroah-Hartman 24872f46a2c1SAl Viro static int tty_tiocsserial(struct tty_struct *tty, struct serial_struct __user *ss) 24888a8ae62fSJiri Slaby { 24898a8ae62fSJiri Slaby static DEFINE_RATELIMIT_STATE(depr_flags, 24908a8ae62fSJiri Slaby DEFAULT_RATELIMIT_INTERVAL, 24918a8ae62fSJiri Slaby DEFAULT_RATELIMIT_BURST); 24928a8ae62fSJiri Slaby char comm[TASK_COMM_LEN]; 24932f46a2c1SAl Viro struct serial_struct v; 24948a8ae62fSJiri Slaby int flags; 24958a8ae62fSJiri Slaby 24962f46a2c1SAl Viro if (copy_from_user(&v, ss, sizeof(struct serial_struct))) 24972f46a2c1SAl Viro return -EFAULT; 24988a8ae62fSJiri Slaby 24992f46a2c1SAl Viro flags = v.flags & ASYNC_DEPRECATED; 25008a8ae62fSJiri Slaby 25018a8ae62fSJiri Slaby if (flags && __ratelimit(&depr_flags)) 2502d437fa91SJoe Perches pr_warn("%s: '%s' is using deprecated serial flags (with no effect): %.8x\n", 25038a8ae62fSJiri Slaby __func__, get_task_comm(comm, current), flags); 25042f46a2c1SAl Viro if (!tty->ops->set_serial) 2505930236a3SAl Viro return -ENOTTY; 25062f46a2c1SAl Viro return tty->ops->set_serial(tty, &v); 25072f46a2c1SAl Viro } 25082f46a2c1SAl Viro 25092f46a2c1SAl Viro static int tty_tiocgserial(struct tty_struct *tty, struct serial_struct __user *ss) 25102f46a2c1SAl Viro { 25112f46a2c1SAl Viro struct serial_struct v; 25122f46a2c1SAl Viro int err; 25132f46a2c1SAl Viro 25142f46a2c1SAl Viro memset(&v, 0, sizeof(struct serial_struct)); 25152f46a2c1SAl Viro if (!tty->ops->get_serial) 2516930236a3SAl Viro return -ENOTTY; 25172f46a2c1SAl Viro err = tty->ops->get_serial(tty, &v); 25182f46a2c1SAl Viro if (!err && copy_to_user(ss, &v, sizeof(struct serial_struct))) 25192f46a2c1SAl Viro err = -EFAULT; 25202f46a2c1SAl Viro return err; 25218a8ae62fSJiri Slaby } 25228a8ae62fSJiri Slaby 25238f166e00SPeter Hurley /* 25248f166e00SPeter Hurley * if pty, return the slave side (real_tty) 25258f166e00SPeter Hurley * otherwise, return self 25268f166e00SPeter Hurley */ 25278f166e00SPeter Hurley static struct tty_struct *tty_pair_get_tty(struct tty_struct *tty) 252896fd7ce5SGreg Kroah-Hartman { 252996fd7ce5SGreg Kroah-Hartman if (tty->driver->type == TTY_DRIVER_TYPE_PTY && 253096fd7ce5SGreg Kroah-Hartman tty->driver->subtype == PTY_TYPE_MASTER) 253196fd7ce5SGreg Kroah-Hartman tty = tty->link; 253296fd7ce5SGreg Kroah-Hartman return tty; 253396fd7ce5SGreg Kroah-Hartman } 253496fd7ce5SGreg Kroah-Hartman 253596fd7ce5SGreg Kroah-Hartman /* 253696fd7ce5SGreg Kroah-Hartman * Split this up, as gcc can choke on it otherwise.. 253796fd7ce5SGreg Kroah-Hartman */ 253896fd7ce5SGreg Kroah-Hartman long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 253996fd7ce5SGreg Kroah-Hartman { 254096fd7ce5SGreg Kroah-Hartman struct tty_struct *tty = file_tty(file); 254196fd7ce5SGreg Kroah-Hartman struct tty_struct *real_tty; 254296fd7ce5SGreg Kroah-Hartman void __user *p = (void __user *)arg; 254396fd7ce5SGreg Kroah-Hartman int retval; 254496fd7ce5SGreg Kroah-Hartman struct tty_ldisc *ld; 254596fd7ce5SGreg Kroah-Hartman 25466131ffaaSAl Viro if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl")) 254796fd7ce5SGreg Kroah-Hartman return -EINVAL; 254896fd7ce5SGreg Kroah-Hartman 254996fd7ce5SGreg Kroah-Hartman real_tty = tty_pair_get_tty(tty); 255096fd7ce5SGreg Kroah-Hartman 255196fd7ce5SGreg Kroah-Hartman /* 255296fd7ce5SGreg Kroah-Hartman * Factor out some common prep work 255396fd7ce5SGreg Kroah-Hartman */ 255496fd7ce5SGreg Kroah-Hartman switch (cmd) { 255596fd7ce5SGreg Kroah-Hartman case TIOCSETD: 255696fd7ce5SGreg Kroah-Hartman case TIOCSBRK: 255796fd7ce5SGreg Kroah-Hartman case TIOCCBRK: 255896fd7ce5SGreg Kroah-Hartman case TCSBRK: 255996fd7ce5SGreg Kroah-Hartman case TCSBRKP: 256096fd7ce5SGreg Kroah-Hartman retval = tty_check_change(tty); 256196fd7ce5SGreg Kroah-Hartman if (retval) 256296fd7ce5SGreg Kroah-Hartman return retval; 256396fd7ce5SGreg Kroah-Hartman if (cmd != TIOCCBRK) { 256496fd7ce5SGreg Kroah-Hartman tty_wait_until_sent(tty, 0); 256596fd7ce5SGreg Kroah-Hartman if (signal_pending(current)) 256696fd7ce5SGreg Kroah-Hartman return -EINTR; 256796fd7ce5SGreg Kroah-Hartman } 256896fd7ce5SGreg Kroah-Hartman break; 256996fd7ce5SGreg Kroah-Hartman } 257096fd7ce5SGreg Kroah-Hartman 257196fd7ce5SGreg Kroah-Hartman /* 257296fd7ce5SGreg Kroah-Hartman * Now do the stuff. 257396fd7ce5SGreg Kroah-Hartman */ 257496fd7ce5SGreg Kroah-Hartman switch (cmd) { 257596fd7ce5SGreg Kroah-Hartman case TIOCSTI: 257696fd7ce5SGreg Kroah-Hartman return tiocsti(tty, p); 257796fd7ce5SGreg Kroah-Hartman case TIOCGWINSZ: 257896fd7ce5SGreg Kroah-Hartman return tiocgwinsz(real_tty, p); 257996fd7ce5SGreg Kroah-Hartman case TIOCSWINSZ: 258096fd7ce5SGreg Kroah-Hartman return tiocswinsz(real_tty, p); 258196fd7ce5SGreg Kroah-Hartman case TIOCCONS: 258296fd7ce5SGreg Kroah-Hartman return real_tty != tty ? -EINVAL : tioccons(file); 258396fd7ce5SGreg Kroah-Hartman case TIOCEXCL: 258496fd7ce5SGreg Kroah-Hartman set_bit(TTY_EXCLUSIVE, &tty->flags); 258596fd7ce5SGreg Kroah-Hartman return 0; 258696fd7ce5SGreg Kroah-Hartman case TIOCNXCL: 258796fd7ce5SGreg Kroah-Hartman clear_bit(TTY_EXCLUSIVE, &tty->flags); 258896fd7ce5SGreg Kroah-Hartman return 0; 258984fd7bdfSCyrill Gorcunov case TIOCGEXCL: 259084fd7bdfSCyrill Gorcunov { 259184fd7bdfSCyrill Gorcunov int excl = test_bit(TTY_EXCLUSIVE, &tty->flags); 259284fd7bdfSCyrill Gorcunov return put_user(excl, (int __user *)p); 259384fd7bdfSCyrill Gorcunov } 259496fd7ce5SGreg Kroah-Hartman case TIOCGETD: 25955c17c861SPeter Hurley return tiocgetd(tty, p); 259696fd7ce5SGreg Kroah-Hartman case TIOCSETD: 259796fd7ce5SGreg Kroah-Hartman return tiocsetd(tty, p); 25983c95c985SKay Sievers case TIOCVHANGUP: 25993c95c985SKay Sievers if (!capable(CAP_SYS_ADMIN)) 26003c95c985SKay Sievers return -EPERM; 26013c95c985SKay Sievers tty_vhangup(tty); 26023c95c985SKay Sievers return 0; 2603b7b8de08SWerner Fink case TIOCGDEV: 2604b7b8de08SWerner Fink { 2605b7b8de08SWerner Fink unsigned int ret = new_encode_dev(tty_devnum(real_tty)); 2606b7b8de08SWerner Fink return put_user(ret, (unsigned int __user *)p); 2607b7b8de08SWerner Fink } 260896fd7ce5SGreg Kroah-Hartman /* 260996fd7ce5SGreg Kroah-Hartman * Break handling 261096fd7ce5SGreg Kroah-Hartman */ 261196fd7ce5SGreg Kroah-Hartman case TIOCSBRK: /* Turn break on, unconditionally */ 261296fd7ce5SGreg Kroah-Hartman if (tty->ops->break_ctl) 261396fd7ce5SGreg Kroah-Hartman return tty->ops->break_ctl(tty, -1); 261496fd7ce5SGreg Kroah-Hartman return 0; 261596fd7ce5SGreg Kroah-Hartman case TIOCCBRK: /* Turn break off, unconditionally */ 261696fd7ce5SGreg Kroah-Hartman if (tty->ops->break_ctl) 261796fd7ce5SGreg Kroah-Hartman return tty->ops->break_ctl(tty, 0); 261896fd7ce5SGreg Kroah-Hartman return 0; 261996fd7ce5SGreg Kroah-Hartman case TCSBRK: /* SVID version: non-zero arg --> no break */ 262096fd7ce5SGreg Kroah-Hartman /* non-zero arg means wait for all output data 262196fd7ce5SGreg Kroah-Hartman * to be sent (performed above) but don't send break. 262296fd7ce5SGreg Kroah-Hartman * This is used by the tcdrain() termios function. 262396fd7ce5SGreg Kroah-Hartman */ 262496fd7ce5SGreg Kroah-Hartman if (!arg) 262596fd7ce5SGreg Kroah-Hartman return send_break(tty, 250); 262696fd7ce5SGreg Kroah-Hartman return 0; 262796fd7ce5SGreg Kroah-Hartman case TCSBRKP: /* support for POSIX tcsendbreak() */ 262896fd7ce5SGreg Kroah-Hartman return send_break(tty, arg ? arg*100 : 250); 262996fd7ce5SGreg Kroah-Hartman 263096fd7ce5SGreg Kroah-Hartman case TIOCMGET: 263160b33c13SAlan Cox return tty_tiocmget(tty, p); 263296fd7ce5SGreg Kroah-Hartman case TIOCMSET: 263396fd7ce5SGreg Kroah-Hartman case TIOCMBIC: 263496fd7ce5SGreg Kroah-Hartman case TIOCMBIS: 263520b9d177SAlan Cox return tty_tiocmset(tty, cmd, p); 263696fd7ce5SGreg Kroah-Hartman case TIOCGICOUNT: 2637a3096199SAl Viro return tty_tiocgicount(tty, p); 263896fd7ce5SGreg Kroah-Hartman case TCFLSH: 263996fd7ce5SGreg Kroah-Hartman switch (arg) { 264096fd7ce5SGreg Kroah-Hartman case TCIFLUSH: 264196fd7ce5SGreg Kroah-Hartman case TCIOFLUSH: 264296fd7ce5SGreg Kroah-Hartman /* flush tty buffer and allow ldisc to process ioctl */ 264386c80a8eSPeter Hurley tty_buffer_flush(tty, NULL); 264496fd7ce5SGreg Kroah-Hartman break; 264596fd7ce5SGreg Kroah-Hartman } 264696fd7ce5SGreg Kroah-Hartman break; 26478a8ae62fSJiri Slaby case TIOCSSERIAL: 2648930236a3SAl Viro return tty_tiocsserial(tty, p); 26492f46a2c1SAl Viro case TIOCGSERIAL: 2650930236a3SAl Viro return tty_tiocgserial(tty, p); 2651311fc65cSEric W. Biederman case TIOCGPTPEER: 2652311fc65cSEric W. Biederman /* Special because the struct file is needed */ 2653311fc65cSEric W. Biederman return ptm_open_peer(file, tty, (int)arg); 2654a1235b3eSNicolas Pitre default: 2655a1235b3eSNicolas Pitre retval = tty_jobctrl_ioctl(tty, real_tty, file, cmd, arg); 2656a1235b3eSNicolas Pitre if (retval != -ENOIOCTLCMD) 2657a1235b3eSNicolas Pitre return retval; 265896fd7ce5SGreg Kroah-Hartman } 265996fd7ce5SGreg Kroah-Hartman if (tty->ops->ioctl) { 2660c961bfb1SPeter Hurley retval = tty->ops->ioctl(tty, cmd, arg); 266196fd7ce5SGreg Kroah-Hartman if (retval != -ENOIOCTLCMD) 266296fd7ce5SGreg Kroah-Hartman return retval; 266396fd7ce5SGreg Kroah-Hartman } 266496fd7ce5SGreg Kroah-Hartman ld = tty_ldisc_ref_wait(tty); 2665e55afd11SPeter Hurley if (!ld) 2666e55afd11SPeter Hurley return hung_up_tty_ioctl(file, cmd, arg); 266796fd7ce5SGreg Kroah-Hartman retval = -EINVAL; 266896fd7ce5SGreg Kroah-Hartman if (ld->ops->ioctl) { 266996fd7ce5SGreg Kroah-Hartman retval = ld->ops->ioctl(tty, file, cmd, arg); 267096fd7ce5SGreg Kroah-Hartman if (retval == -ENOIOCTLCMD) 2671bbb63c51SWanlong Gao retval = -ENOTTY; 267296fd7ce5SGreg Kroah-Hartman } 267396fd7ce5SGreg Kroah-Hartman tty_ldisc_deref(ld); 267496fd7ce5SGreg Kroah-Hartman return retval; 267596fd7ce5SGreg Kroah-Hartman } 267696fd7ce5SGreg Kroah-Hartman 267796fd7ce5SGreg Kroah-Hartman #ifdef CONFIG_COMPAT 267877654350SAl Viro 267977654350SAl Viro struct serial_struct32 { 268077654350SAl Viro compat_int_t type; 268177654350SAl Viro compat_int_t line; 268277654350SAl Viro compat_uint_t port; 268377654350SAl Viro compat_int_t irq; 268477654350SAl Viro compat_int_t flags; 268577654350SAl Viro compat_int_t xmit_fifo_size; 268677654350SAl Viro compat_int_t custom_divisor; 268777654350SAl Viro compat_int_t baud_base; 268877654350SAl Viro unsigned short close_delay; 268977654350SAl Viro char io_type; 269077654350SAl Viro char reserved_char[1]; 269177654350SAl Viro compat_int_t hub6; 269277654350SAl Viro unsigned short closing_wait; /* time to wait before closing */ 269377654350SAl Viro unsigned short closing_wait2; /* no longer used... */ 269477654350SAl Viro compat_uint_t iomem_base; 269577654350SAl Viro unsigned short iomem_reg_shift; 269677654350SAl Viro unsigned int port_high; 269777654350SAl Viro /* compat_ulong_t iomap_base FIXME */ 269877654350SAl Viro compat_int_t reserved[1]; 269977654350SAl Viro }; 270077654350SAl Viro 270177654350SAl Viro static int compat_tty_tiocsserial(struct tty_struct *tty, 270277654350SAl Viro struct serial_struct32 __user *ss) 270377654350SAl Viro { 270477654350SAl Viro static DEFINE_RATELIMIT_STATE(depr_flags, 270577654350SAl Viro DEFAULT_RATELIMIT_INTERVAL, 270677654350SAl Viro DEFAULT_RATELIMIT_BURST); 270777654350SAl Viro char comm[TASK_COMM_LEN]; 270877654350SAl Viro struct serial_struct32 v32; 270977654350SAl Viro struct serial_struct v; 271077654350SAl Viro int flags; 271177654350SAl Viro 271277654350SAl Viro if (copy_from_user(&v32, ss, sizeof(struct serial_struct32))) 271377654350SAl Viro return -EFAULT; 271477654350SAl Viro 271577654350SAl Viro memcpy(&v, &v32, offsetof(struct serial_struct32, iomem_base)); 271677654350SAl Viro v.iomem_base = compat_ptr(v32.iomem_base); 271777654350SAl Viro v.iomem_reg_shift = v32.iomem_reg_shift; 271877654350SAl Viro v.port_high = v32.port_high; 271977654350SAl Viro v.iomap_base = 0; 272077654350SAl Viro 272177654350SAl Viro flags = v.flags & ASYNC_DEPRECATED; 272277654350SAl Viro 272377654350SAl Viro if (flags && __ratelimit(&depr_flags)) 272477654350SAl Viro pr_warn("%s: '%s' is using deprecated serial flags (with no effect): %.8x\n", 272577654350SAl Viro __func__, get_task_comm(comm, current), flags); 272677654350SAl Viro if (!tty->ops->set_serial) 272777654350SAl Viro return -ENOTTY; 272877654350SAl Viro return tty->ops->set_serial(tty, &v); 272977654350SAl Viro } 273077654350SAl Viro 273177654350SAl Viro static int compat_tty_tiocgserial(struct tty_struct *tty, 273277654350SAl Viro struct serial_struct32 __user *ss) 273377654350SAl Viro { 273477654350SAl Viro struct serial_struct32 v32; 273577654350SAl Viro struct serial_struct v; 273677654350SAl Viro int err; 273777654350SAl Viro memset(&v, 0, sizeof(struct serial_struct)); 273877654350SAl Viro 273977654350SAl Viro if (!tty->ops->set_serial) 274077654350SAl Viro return -ENOTTY; 274177654350SAl Viro err = tty->ops->get_serial(tty, &v); 274277654350SAl Viro if (!err) { 274377654350SAl Viro memcpy(&v32, &v, offsetof(struct serial_struct32, iomem_base)); 274477654350SAl Viro v32.iomem_base = (unsigned long)v.iomem_base >> 32 ? 274577654350SAl Viro 0xfffffff : ptr_to_compat(v.iomem_base); 274677654350SAl Viro v32.iomem_reg_shift = v.iomem_reg_shift; 274777654350SAl Viro v32.port_high = v.port_high; 274877654350SAl Viro if (copy_to_user(ss, &v32, sizeof(struct serial_struct32))) 274977654350SAl Viro err = -EFAULT; 275077654350SAl Viro } 275177654350SAl Viro return err; 275277654350SAl Viro } 275396fd7ce5SGreg Kroah-Hartman static long tty_compat_ioctl(struct file *file, unsigned int cmd, 275496fd7ce5SGreg Kroah-Hartman unsigned long arg) 275596fd7ce5SGreg Kroah-Hartman { 275696fd7ce5SGreg Kroah-Hartman struct tty_struct *tty = file_tty(file); 275796fd7ce5SGreg Kroah-Hartman struct tty_ldisc *ld; 275896fd7ce5SGreg Kroah-Hartman int retval = -ENOIOCTLCMD; 275996fd7ce5SGreg Kroah-Hartman 2760e2112038SAl Viro switch (cmd) { 2761c7dc504eSArnd Bergmann case TIOCOUTQ: 2762e2112038SAl Viro case TIOCSTI: 2763e2112038SAl Viro case TIOCGWINSZ: 2764e2112038SAl Viro case TIOCSWINSZ: 2765e2112038SAl Viro case TIOCGEXCL: 2766e2112038SAl Viro case TIOCGETD: 2767e2112038SAl Viro case TIOCSETD: 2768e2112038SAl Viro case TIOCGDEV: 2769e2112038SAl Viro case TIOCMGET: 2770e2112038SAl Viro case TIOCMSET: 2771e2112038SAl Viro case TIOCMBIC: 2772e2112038SAl Viro case TIOCMBIS: 2773e2112038SAl Viro case TIOCGICOUNT: 2774e2112038SAl Viro case TIOCGPGRP: 2775e2112038SAl Viro case TIOCSPGRP: 2776e2112038SAl Viro case TIOCGSID: 2777e2112038SAl Viro case TIOCSERGETLSR: 2778e2112038SAl Viro case TIOCGRS485: 2779e2112038SAl Viro case TIOCSRS485: 2780e2112038SAl Viro #ifdef TIOCGETP 2781e2112038SAl Viro case TIOCGETP: 2782e2112038SAl Viro case TIOCSETP: 2783e2112038SAl Viro case TIOCSETN: 2784e2112038SAl Viro #endif 2785e2112038SAl Viro #ifdef TIOCGETC 2786e2112038SAl Viro case TIOCGETC: 2787e2112038SAl Viro case TIOCSETC: 2788e2112038SAl Viro #endif 2789e2112038SAl Viro #ifdef TIOCGLTC 2790e2112038SAl Viro case TIOCGLTC: 2791e2112038SAl Viro case TIOCSLTC: 2792e2112038SAl Viro #endif 2793e2112038SAl Viro case TCSETSF: 2794e2112038SAl Viro case TCSETSW: 2795e2112038SAl Viro case TCSETS: 2796e2112038SAl Viro case TCGETS: 2797e2112038SAl Viro #ifdef TCGETS2 2798e2112038SAl Viro case TCGETS2: 2799e2112038SAl Viro case TCSETSF2: 2800e2112038SAl Viro case TCSETSW2: 2801e2112038SAl Viro case TCSETS2: 2802e2112038SAl Viro #endif 2803e2112038SAl Viro case TCGETA: 2804e2112038SAl Viro case TCSETAF: 2805e2112038SAl Viro case TCSETAW: 2806e2112038SAl Viro case TCSETA: 2807e2112038SAl Viro case TIOCGLCKTRMIOS: 2808e2112038SAl Viro case TIOCSLCKTRMIOS: 2809e2112038SAl Viro #ifdef TCGETX 2810e2112038SAl Viro case TCGETX: 2811e2112038SAl Viro case TCSETX: 2812e2112038SAl Viro case TCSETXW: 2813e2112038SAl Viro case TCSETXF: 2814e2112038SAl Viro #endif 2815e2112038SAl Viro case TIOCGSOFTCAR: 2816e2112038SAl Viro case TIOCSSOFTCAR: 2817b7aff093SArnd Bergmann 2818b7aff093SArnd Bergmann case PPPIOCGCHAN: 2819b7aff093SArnd Bergmann case PPPIOCGUNIT: 2820e2112038SAl Viro return tty_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); 2821e2112038SAl Viro case TIOCCONS: 2822e2112038SAl Viro case TIOCEXCL: 2823e2112038SAl Viro case TIOCNXCL: 2824e2112038SAl Viro case TIOCVHANGUP: 2825e2112038SAl Viro case TIOCSBRK: 2826e2112038SAl Viro case TIOCCBRK: 2827e2112038SAl Viro case TCSBRK: 2828e2112038SAl Viro case TCSBRKP: 2829e2112038SAl Viro case TCFLSH: 2830e2112038SAl Viro case TIOCGPTPEER: 2831e2112038SAl Viro case TIOCNOTTY: 2832e2112038SAl Viro case TIOCSCTTY: 2833e2112038SAl Viro case TCXONC: 2834e2112038SAl Viro case TIOCMIWAIT: 2835e2112038SAl Viro case TIOCSERCONFIG: 2836e2112038SAl Viro return tty_ioctl(file, cmd, arg); 2837e2112038SAl Viro } 2838e2112038SAl Viro 28396131ffaaSAl Viro if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl")) 284096fd7ce5SGreg Kroah-Hartman return -EINVAL; 284196fd7ce5SGreg Kroah-Hartman 284277654350SAl Viro switch (cmd) { 284377654350SAl Viro case TIOCSSERIAL: 284477654350SAl Viro return compat_tty_tiocsserial(tty, compat_ptr(arg)); 284577654350SAl Viro case TIOCGSERIAL: 284677654350SAl Viro return compat_tty_tiocgserial(tty, compat_ptr(arg)); 284777654350SAl Viro } 284896fd7ce5SGreg Kroah-Hartman if (tty->ops->compat_ioctl) { 2849c961bfb1SPeter Hurley retval = tty->ops->compat_ioctl(tty, cmd, arg); 285096fd7ce5SGreg Kroah-Hartman if (retval != -ENOIOCTLCMD) 285196fd7ce5SGreg Kroah-Hartman return retval; 285296fd7ce5SGreg Kroah-Hartman } 285396fd7ce5SGreg Kroah-Hartman 285496fd7ce5SGreg Kroah-Hartman ld = tty_ldisc_ref_wait(tty); 2855e55afd11SPeter Hurley if (!ld) 2856e55afd11SPeter Hurley return hung_up_tty_compat_ioctl(file, cmd, arg); 285796fd7ce5SGreg Kroah-Hartman if (ld->ops->compat_ioctl) 285896fd7ce5SGreg Kroah-Hartman retval = ld->ops->compat_ioctl(tty, file, cmd, arg); 2859f0193d3eSAl Viro if (retval == -ENOIOCTLCMD && ld->ops->ioctl) 2860f0193d3eSAl Viro retval = ld->ops->ioctl(tty, file, 2861f0193d3eSAl Viro (unsigned long)compat_ptr(cmd), arg); 286296fd7ce5SGreg Kroah-Hartman tty_ldisc_deref(ld); 286396fd7ce5SGreg Kroah-Hartman 286496fd7ce5SGreg Kroah-Hartman return retval; 286596fd7ce5SGreg Kroah-Hartman } 286696fd7ce5SGreg Kroah-Hartman #endif 286796fd7ce5SGreg Kroah-Hartman 2868c3c073f8SAl Viro static int this_tty(const void *t, struct file *file, unsigned fd) 2869c3c073f8SAl Viro { 2870c3c073f8SAl Viro if (likely(file->f_op->read != tty_read)) 2871c3c073f8SAl Viro return 0; 2872c3c073f8SAl Viro return file_tty(file) != t ? 0 : fd + 1; 2873c3c073f8SAl Viro } 2874c3c073f8SAl Viro 287596fd7ce5SGreg Kroah-Hartman /* 287696fd7ce5SGreg Kroah-Hartman * This implements the "Secure Attention Key" --- the idea is to 287796fd7ce5SGreg Kroah-Hartman * prevent trojan horses by killing all processes associated with this 287896fd7ce5SGreg Kroah-Hartman * tty when the user hits the "Secure Attention Key". Required for 287996fd7ce5SGreg Kroah-Hartman * super-paranoid applications --- see the Orange Book for more details. 288096fd7ce5SGreg Kroah-Hartman * 288196fd7ce5SGreg Kroah-Hartman * This code could be nicer; ideally it should send a HUP, wait a few 288296fd7ce5SGreg Kroah-Hartman * seconds, then send a INT, and then a KILL signal. But you then 288396fd7ce5SGreg Kroah-Hartman * have to coordinate with the init process, since all processes associated 288496fd7ce5SGreg Kroah-Hartman * with the current tty must be dead before the new getty is allowed 288596fd7ce5SGreg Kroah-Hartman * to spawn. 288696fd7ce5SGreg Kroah-Hartman * 288796fd7ce5SGreg Kroah-Hartman * Now, if it would be correct ;-/ The current code has a nasty hole - 288896fd7ce5SGreg Kroah-Hartman * it doesn't catch files in flight. We may send the descriptor to ourselves 288996fd7ce5SGreg Kroah-Hartman * via AF_UNIX socket, close it and later fetch from socket. FIXME. 289096fd7ce5SGreg Kroah-Hartman * 289196fd7ce5SGreg Kroah-Hartman * Nasty bug: do_SAK is being called in interrupt context. This can 289296fd7ce5SGreg Kroah-Hartman * deadlock. We punt it up to process context. AKPM - 16Mar2001 289396fd7ce5SGreg Kroah-Hartman */ 289496fd7ce5SGreg Kroah-Hartman void __do_SAK(struct tty_struct *tty) 289596fd7ce5SGreg Kroah-Hartman { 289696fd7ce5SGreg Kroah-Hartman #ifdef TTY_SOFT_SAK 289796fd7ce5SGreg Kroah-Hartman tty_hangup(tty); 289896fd7ce5SGreg Kroah-Hartman #else 289996fd7ce5SGreg Kroah-Hartman struct task_struct *g, *p; 290096fd7ce5SGreg Kroah-Hartman struct pid *session; 290196fd7ce5SGreg Kroah-Hartman int i; 290296fd7ce5SGreg Kroah-Hartman 290396fd7ce5SGreg Kroah-Hartman if (!tty) 290496fd7ce5SGreg Kroah-Hartman return; 290596fd7ce5SGreg Kroah-Hartman session = tty->session; 290696fd7ce5SGreg Kroah-Hartman 290796fd7ce5SGreg Kroah-Hartman tty_ldisc_flush(tty); 290896fd7ce5SGreg Kroah-Hartman 290996fd7ce5SGreg Kroah-Hartman tty_driver_flush_buffer(tty); 291096fd7ce5SGreg Kroah-Hartman 291196fd7ce5SGreg Kroah-Hartman read_lock(&tasklist_lock); 291296fd7ce5SGreg Kroah-Hartman /* Kill the entire session */ 291396fd7ce5SGreg Kroah-Hartman do_each_pid_task(session, PIDTYPE_SID, p) { 29149b42bb75SPeter Hurley tty_notice(tty, "SAK: killed process %d (%s): by session\n", 291596fd7ce5SGreg Kroah-Hartman task_pid_nr(p), p->comm); 2916a8ebd171SEric W. Biederman group_send_sig_info(SIGKILL, SEND_SIG_PRIV, p, PIDTYPE_SID); 291796fd7ce5SGreg Kroah-Hartman } while_each_pid_task(session, PIDTYPE_SID, p); 29189b42bb75SPeter Hurley 29199b42bb75SPeter Hurley /* Now kill any processes that happen to have the tty open */ 292096fd7ce5SGreg Kroah-Hartman do_each_thread(g, p) { 292196fd7ce5SGreg Kroah-Hartman if (p->signal->tty == tty) { 29229b42bb75SPeter Hurley tty_notice(tty, "SAK: killed process %d (%s): by controlling tty\n", 292396fd7ce5SGreg Kroah-Hartman task_pid_nr(p), p->comm); 2924a8ebd171SEric W. Biederman group_send_sig_info(SIGKILL, SEND_SIG_PRIV, p, PIDTYPE_SID); 292596fd7ce5SGreg Kroah-Hartman continue; 292696fd7ce5SGreg Kroah-Hartman } 292796fd7ce5SGreg Kroah-Hartman task_lock(p); 2928c3c073f8SAl Viro i = iterate_fd(p->files, 0, this_tty, tty); 2929c3c073f8SAl Viro if (i != 0) { 29309b42bb75SPeter Hurley tty_notice(tty, "SAK: killed process %d (%s): by fd#%d\n", 2931c3c073f8SAl Viro task_pid_nr(p), p->comm, i - 1); 2932a8ebd171SEric W. Biederman group_send_sig_info(SIGKILL, SEND_SIG_PRIV, p, PIDTYPE_SID); 293396fd7ce5SGreg Kroah-Hartman } 293496fd7ce5SGreg Kroah-Hartman task_unlock(p); 293596fd7ce5SGreg Kroah-Hartman } while_each_thread(g, p); 293696fd7ce5SGreg Kroah-Hartman read_unlock(&tasklist_lock); 293796fd7ce5SGreg Kroah-Hartman #endif 293896fd7ce5SGreg Kroah-Hartman } 293996fd7ce5SGreg Kroah-Hartman 294096fd7ce5SGreg Kroah-Hartman static void do_SAK_work(struct work_struct *work) 294196fd7ce5SGreg Kroah-Hartman { 294296fd7ce5SGreg Kroah-Hartman struct tty_struct *tty = 294396fd7ce5SGreg Kroah-Hartman container_of(work, struct tty_struct, SAK_work); 294496fd7ce5SGreg Kroah-Hartman __do_SAK(tty); 294596fd7ce5SGreg Kroah-Hartman } 294696fd7ce5SGreg Kroah-Hartman 294796fd7ce5SGreg Kroah-Hartman /* 294896fd7ce5SGreg Kroah-Hartman * The tq handling here is a little racy - tty->SAK_work may already be queued. 294996fd7ce5SGreg Kroah-Hartman * Fortunately we don't need to worry, because if ->SAK_work is already queued, 295096fd7ce5SGreg Kroah-Hartman * the values which we write to it will be identical to the values which it 295196fd7ce5SGreg Kroah-Hartman * already has. --akpm 295296fd7ce5SGreg Kroah-Hartman */ 295396fd7ce5SGreg Kroah-Hartman void do_SAK(struct tty_struct *tty) 295496fd7ce5SGreg Kroah-Hartman { 295596fd7ce5SGreg Kroah-Hartman if (!tty) 295696fd7ce5SGreg Kroah-Hartman return; 295796fd7ce5SGreg Kroah-Hartman schedule_work(&tty->SAK_work); 295896fd7ce5SGreg Kroah-Hartman } 295996fd7ce5SGreg Kroah-Hartman 296096fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(do_SAK); 296196fd7ce5SGreg Kroah-Hartman 296296fd7ce5SGreg Kroah-Hartman /* Must put_device() after it's unused! */ 296396fd7ce5SGreg Kroah-Hartman static struct device *tty_get_device(struct tty_struct *tty) 296496fd7ce5SGreg Kroah-Hartman { 296596fd7ce5SGreg Kroah-Hartman dev_t devt = tty_devnum(tty); 29664495dfddSSuzuki K Poulose return class_find_device_by_devt(tty_class, devt); 296796fd7ce5SGreg Kroah-Hartman } 296896fd7ce5SGreg Kroah-Hartman 296996fd7ce5SGreg Kroah-Hartman 297096fd7ce5SGreg Kroah-Hartman /** 29712c964a2fSRasmus Villemoes * alloc_tty_struct 297296fd7ce5SGreg Kroah-Hartman * 29732c964a2fSRasmus Villemoes * This subroutine allocates and initializes a tty structure. 297496fd7ce5SGreg Kroah-Hartman * 29752c964a2fSRasmus Villemoes * Locking: none - tty in question is not exposed at this point 297696fd7ce5SGreg Kroah-Hartman */ 297796fd7ce5SGreg Kroah-Hartman 29782c964a2fSRasmus Villemoes struct tty_struct *alloc_tty_struct(struct tty_driver *driver, int idx) 297996fd7ce5SGreg Kroah-Hartman { 29802c964a2fSRasmus Villemoes struct tty_struct *tty; 29812c964a2fSRasmus Villemoes 29822c964a2fSRasmus Villemoes tty = kzalloc(sizeof(*tty), GFP_KERNEL); 29832c964a2fSRasmus Villemoes if (!tty) 29842c964a2fSRasmus Villemoes return NULL; 29852c964a2fSRasmus Villemoes 298696fd7ce5SGreg Kroah-Hartman kref_init(&tty->kref); 298796fd7ce5SGreg Kroah-Hartman tty->magic = TTY_MAGIC; 2988903f9db1STetsuo Handa if (tty_ldisc_init(tty)) { 2989903f9db1STetsuo Handa kfree(tty); 2990903f9db1STetsuo Handa return NULL; 2991903f9db1STetsuo Handa } 299296fd7ce5SGreg Kroah-Hartman tty->session = NULL; 299396fd7ce5SGreg Kroah-Hartman tty->pgrp = NULL; 299489c8d91eSAlan Cox mutex_init(&tty->legacy_mutex); 2995d8c1f929SPeter Hurley mutex_init(&tty->throttle_mutex); 29966a1c0680SPeter Hurley init_rwsem(&tty->termios_rwsem); 2997dee4a0beSPeter Hurley mutex_init(&tty->winsize_mutex); 299836697529SPeter Hurley init_ldsem(&tty->ldisc_sem); 299996fd7ce5SGreg Kroah-Hartman init_waitqueue_head(&tty->write_wait); 300096fd7ce5SGreg Kroah-Hartman init_waitqueue_head(&tty->read_wait); 300196fd7ce5SGreg Kroah-Hartman INIT_WORK(&tty->hangup_work, do_tty_hangup); 300296fd7ce5SGreg Kroah-Hartman mutex_init(&tty->atomic_write_lock); 300396fd7ce5SGreg Kroah-Hartman spin_lock_init(&tty->ctrl_lock); 3004f9e053dcSPeter Hurley spin_lock_init(&tty->flow_lock); 30054a510969SPeter Hurley spin_lock_init(&tty->files_lock); 300696fd7ce5SGreg Kroah-Hartman INIT_LIST_HEAD(&tty->tty_files); 300796fd7ce5SGreg Kroah-Hartman INIT_WORK(&tty->SAK_work, do_SAK_work); 300896fd7ce5SGreg Kroah-Hartman 300996fd7ce5SGreg Kroah-Hartman tty->driver = driver; 301096fd7ce5SGreg Kroah-Hartman tty->ops = driver->ops; 301196fd7ce5SGreg Kroah-Hartman tty->index = idx; 301296fd7ce5SGreg Kroah-Hartman tty_line_name(driver, idx, tty->name); 301396fd7ce5SGreg Kroah-Hartman tty->dev = tty_get_device(tty); 30142c964a2fSRasmus Villemoes 30152c964a2fSRasmus Villemoes return tty; 301696fd7ce5SGreg Kroah-Hartman } 301796fd7ce5SGreg Kroah-Hartman 301896fd7ce5SGreg Kroah-Hartman /** 301996fd7ce5SGreg Kroah-Hartman * tty_put_char - write one character to a tty 302096fd7ce5SGreg Kroah-Hartman * @tty: tty 302196fd7ce5SGreg Kroah-Hartman * @ch: character 302296fd7ce5SGreg Kroah-Hartman * 302396fd7ce5SGreg Kroah-Hartman * Write one byte to the tty using the provided put_char method 302496fd7ce5SGreg Kroah-Hartman * if present. Returns the number of characters successfully output. 302596fd7ce5SGreg Kroah-Hartman * 302696fd7ce5SGreg Kroah-Hartman * Note: the specific put_char operation in the driver layer may go 302796fd7ce5SGreg Kroah-Hartman * away soon. Don't call it directly, use this method 302896fd7ce5SGreg Kroah-Hartman */ 302996fd7ce5SGreg Kroah-Hartman 303096fd7ce5SGreg Kroah-Hartman int tty_put_char(struct tty_struct *tty, unsigned char ch) 303196fd7ce5SGreg Kroah-Hartman { 303296fd7ce5SGreg Kroah-Hartman if (tty->ops->put_char) 303396fd7ce5SGreg Kroah-Hartman return tty->ops->put_char(tty, ch); 303496fd7ce5SGreg Kroah-Hartman return tty->ops->write(tty, &ch, 1); 303596fd7ce5SGreg Kroah-Hartman } 303696fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(tty_put_char); 303796fd7ce5SGreg Kroah-Hartman 303896fd7ce5SGreg Kroah-Hartman struct class *tty_class; 303996fd7ce5SGreg Kroah-Hartman 30407e73eca6SJiri Slaby static int tty_cdev_add(struct tty_driver *driver, dev_t dev, 30417e73eca6SJiri Slaby unsigned int index, unsigned int count) 30427e73eca6SJiri Slaby { 3043c1a752baSLeon Yu int err; 3044c1a752baSLeon Yu 30457e73eca6SJiri Slaby /* init here, since reused cdevs cause crashes */ 3046a3a10ce3SRichard Watts driver->cdevs[index] = cdev_alloc(); 3047a3a10ce3SRichard Watts if (!driver->cdevs[index]) 3048a3a10ce3SRichard Watts return -ENOMEM; 3049c1a752baSLeon Yu driver->cdevs[index]->ops = &tty_fops; 3050a3a10ce3SRichard Watts driver->cdevs[index]->owner = driver->owner; 3051c1a752baSLeon Yu err = cdev_add(driver->cdevs[index], dev, count); 3052c1a752baSLeon Yu if (err) 3053c1a752baSLeon Yu kobject_put(&driver->cdevs[index]->kobj); 3054c1a752baSLeon Yu return err; 30557e73eca6SJiri Slaby } 30567e73eca6SJiri Slaby 305796fd7ce5SGreg Kroah-Hartman /** 305896fd7ce5SGreg Kroah-Hartman * tty_register_device - register a tty device 305996fd7ce5SGreg Kroah-Hartman * @driver: the tty driver that describes the tty device 306096fd7ce5SGreg Kroah-Hartman * @index: the index in the tty driver for this tty device 306196fd7ce5SGreg Kroah-Hartman * @device: a struct device that is associated with this tty device. 306296fd7ce5SGreg Kroah-Hartman * This field is optional, if there is no known struct device 306396fd7ce5SGreg Kroah-Hartman * for this tty device it can be set to NULL safely. 306496fd7ce5SGreg Kroah-Hartman * 306596fd7ce5SGreg Kroah-Hartman * Returns a pointer to the struct device for this tty device 306696fd7ce5SGreg Kroah-Hartman * (or ERR_PTR(-EFOO) on error). 306796fd7ce5SGreg Kroah-Hartman * 306896fd7ce5SGreg Kroah-Hartman * This call is required to be made to register an individual tty device 306996fd7ce5SGreg Kroah-Hartman * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If 307096fd7ce5SGreg Kroah-Hartman * that bit is not set, this function should not be called by a tty 307196fd7ce5SGreg Kroah-Hartman * driver. 307296fd7ce5SGreg Kroah-Hartman * 307396fd7ce5SGreg Kroah-Hartman * Locking: ?? 307496fd7ce5SGreg Kroah-Hartman */ 307596fd7ce5SGreg Kroah-Hartman 307696fd7ce5SGreg Kroah-Hartman struct device *tty_register_device(struct tty_driver *driver, unsigned index, 307796fd7ce5SGreg Kroah-Hartman struct device *device) 307896fd7ce5SGreg Kroah-Hartman { 30796915c0e4STomas Hlavacek return tty_register_device_attr(driver, index, device, NULL, NULL); 30806915c0e4STomas Hlavacek } 30816915c0e4STomas Hlavacek EXPORT_SYMBOL(tty_register_device); 30826915c0e4STomas Hlavacek 3083b1b79916STomas Hlavacek static void tty_device_create_release(struct device *dev) 3084b1b79916STomas Hlavacek { 308583db1df4SPeter Hurley dev_dbg(dev, "releasing...\n"); 3086b1b79916STomas Hlavacek kfree(dev); 3087b1b79916STomas Hlavacek } 3088b1b79916STomas Hlavacek 30896915c0e4STomas Hlavacek /** 30906915c0e4STomas Hlavacek * tty_register_device_attr - register a tty device 30916915c0e4STomas Hlavacek * @driver: the tty driver that describes the tty device 30926915c0e4STomas Hlavacek * @index: the index in the tty driver for this tty device 30936915c0e4STomas Hlavacek * @device: a struct device that is associated with this tty device. 30946915c0e4STomas Hlavacek * This field is optional, if there is no known struct device 30956915c0e4STomas Hlavacek * for this tty device it can be set to NULL safely. 30966915c0e4STomas Hlavacek * @drvdata: Driver data to be set to device. 30976915c0e4STomas Hlavacek * @attr_grp: Attribute group to be set on device. 30986915c0e4STomas Hlavacek * 30996915c0e4STomas Hlavacek * Returns a pointer to the struct device for this tty device 31006915c0e4STomas Hlavacek * (or ERR_PTR(-EFOO) on error). 31016915c0e4STomas Hlavacek * 31026915c0e4STomas Hlavacek * This call is required to be made to register an individual tty device 31036915c0e4STomas Hlavacek * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If 31046915c0e4STomas Hlavacek * that bit is not set, this function should not be called by a tty 31056915c0e4STomas Hlavacek * driver. 31066915c0e4STomas Hlavacek * 31076915c0e4STomas Hlavacek * Locking: ?? 31086915c0e4STomas Hlavacek */ 31096915c0e4STomas Hlavacek struct device *tty_register_device_attr(struct tty_driver *driver, 31106915c0e4STomas Hlavacek unsigned index, struct device *device, 31116915c0e4STomas Hlavacek void *drvdata, 31126915c0e4STomas Hlavacek const struct attribute_group **attr_grp) 31136915c0e4STomas Hlavacek { 311496fd7ce5SGreg Kroah-Hartman char name[64]; 31156915c0e4STomas Hlavacek dev_t devt = MKDEV(driver->major, driver->minor_start) + index; 311693857eddSJohan Hovold struct ktermios *tp; 31176a7e6f78SJohan Hovold struct device *dev; 31186a7e6f78SJohan Hovold int retval; 311996fd7ce5SGreg Kroah-Hartman 312096fd7ce5SGreg Kroah-Hartman if (index >= driver->num) { 3121656fb867SPeter Hurley pr_err("%s: Attempt to register invalid tty line number (%d)\n", 3122656fb867SPeter Hurley driver->name, index); 312396fd7ce5SGreg Kroah-Hartman return ERR_PTR(-EINVAL); 312496fd7ce5SGreg Kroah-Hartman } 312596fd7ce5SGreg Kroah-Hartman 312696fd7ce5SGreg Kroah-Hartman if (driver->type == TTY_DRIVER_TYPE_PTY) 312796fd7ce5SGreg Kroah-Hartman pty_line_name(driver, index, name); 312896fd7ce5SGreg Kroah-Hartman else 312996fd7ce5SGreg Kroah-Hartman tty_line_name(driver, index, name); 313096fd7ce5SGreg Kroah-Hartman 31316915c0e4STomas Hlavacek dev = kzalloc(sizeof(*dev), GFP_KERNEL); 31326a7e6f78SJohan Hovold if (!dev) 31336a7e6f78SJohan Hovold return ERR_PTR(-ENOMEM); 31346915c0e4STomas Hlavacek 31356915c0e4STomas Hlavacek dev->devt = devt; 31366915c0e4STomas Hlavacek dev->class = tty_class; 31376915c0e4STomas Hlavacek dev->parent = device; 3138b1b79916STomas Hlavacek dev->release = tty_device_create_release; 31396915c0e4STomas Hlavacek dev_set_name(dev, "%s", name); 31406915c0e4STomas Hlavacek dev->groups = attr_grp; 31416915c0e4STomas Hlavacek dev_set_drvdata(dev, drvdata); 31426915c0e4STomas Hlavacek 31436a7e6f78SJohan Hovold dev_set_uevent_suppress(dev, 1); 31446a7e6f78SJohan Hovold 31456915c0e4STomas Hlavacek retval = device_register(dev); 31466915c0e4STomas Hlavacek if (retval) 31476a7e6f78SJohan Hovold goto err_put; 31486a7e6f78SJohan Hovold 31496a7e6f78SJohan Hovold if (!(driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)) { 315093857eddSJohan Hovold /* 315193857eddSJohan Hovold * Free any saved termios data so that the termios state is 315293857eddSJohan Hovold * reset when reusing a minor number. 315393857eddSJohan Hovold */ 315493857eddSJohan Hovold tp = driver->termios[index]; 315593857eddSJohan Hovold if (tp) { 315693857eddSJohan Hovold driver->termios[index] = NULL; 315793857eddSJohan Hovold kfree(tp); 315893857eddSJohan Hovold } 315993857eddSJohan Hovold 31606a7e6f78SJohan Hovold retval = tty_cdev_add(driver, devt, index, 1); 31616a7e6f78SJohan Hovold if (retval) 31626a7e6f78SJohan Hovold goto err_del; 31636a7e6f78SJohan Hovold } 31646a7e6f78SJohan Hovold 31656a7e6f78SJohan Hovold dev_set_uevent_suppress(dev, 0); 31666a7e6f78SJohan Hovold kobject_uevent(&dev->kobj, KOBJ_ADD); 31676915c0e4STomas Hlavacek 31686915c0e4STomas Hlavacek return dev; 31696915c0e4STomas Hlavacek 31706a7e6f78SJohan Hovold err_del: 31716a7e6f78SJohan Hovold device_del(dev); 31726a7e6f78SJohan Hovold err_put: 31736915c0e4STomas Hlavacek put_device(dev); 31746a7e6f78SJohan Hovold 31756915c0e4STomas Hlavacek return ERR_PTR(retval); 31766915c0e4STomas Hlavacek } 31776915c0e4STomas Hlavacek EXPORT_SYMBOL_GPL(tty_register_device_attr); 317896fd7ce5SGreg Kroah-Hartman 317996fd7ce5SGreg Kroah-Hartman /** 318096fd7ce5SGreg Kroah-Hartman * tty_unregister_device - unregister a tty device 318196fd7ce5SGreg Kroah-Hartman * @driver: the tty driver that describes the tty device 318296fd7ce5SGreg Kroah-Hartman * @index: the index in the tty driver for this tty device 318396fd7ce5SGreg Kroah-Hartman * 318496fd7ce5SGreg Kroah-Hartman * If a tty device is registered with a call to tty_register_device() then 318596fd7ce5SGreg Kroah-Hartman * this function must be called when the tty device is gone. 318696fd7ce5SGreg Kroah-Hartman * 318796fd7ce5SGreg Kroah-Hartman * Locking: ?? 318896fd7ce5SGreg Kroah-Hartman */ 318996fd7ce5SGreg Kroah-Hartman 319096fd7ce5SGreg Kroah-Hartman void tty_unregister_device(struct tty_driver *driver, unsigned index) 319196fd7ce5SGreg Kroah-Hartman { 319296fd7ce5SGreg Kroah-Hartman device_destroy(tty_class, 319396fd7ce5SGreg Kroah-Hartman MKDEV(driver->major, driver->minor_start) + index); 3194a3a10ce3SRichard Watts if (!(driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)) { 3195a3a10ce3SRichard Watts cdev_del(driver->cdevs[index]); 3196a3a10ce3SRichard Watts driver->cdevs[index] = NULL; 3197a3a10ce3SRichard Watts } 319896fd7ce5SGreg Kroah-Hartman } 319996fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_unregister_device); 320096fd7ce5SGreg Kroah-Hartman 32017f0bc6a6SJiri Slaby /** 32027f0bc6a6SJiri Slaby * __tty_alloc_driver -- allocate tty driver 32037f0bc6a6SJiri Slaby * @lines: count of lines this driver can handle at most 320487838ae3SThadeu Lima de Souza Cascardo * @owner: module which is responsible for this driver 32057f0bc6a6SJiri Slaby * @flags: some of TTY_DRIVER_* flags, will be set in driver->flags 32067f0bc6a6SJiri Slaby * 32077f0bc6a6SJiri Slaby * This should not be called directly, some of the provided macros should be 32087f0bc6a6SJiri Slaby * used instead. Use IS_ERR and friends on @retval. 32097f0bc6a6SJiri Slaby */ 32107f0bc6a6SJiri Slaby struct tty_driver *__tty_alloc_driver(unsigned int lines, struct module *owner, 32117f0bc6a6SJiri Slaby unsigned long flags) 321296fd7ce5SGreg Kroah-Hartman { 321396fd7ce5SGreg Kroah-Hartman struct tty_driver *driver; 32147e73eca6SJiri Slaby unsigned int cdevs = 1; 321516a02081SJiri Slaby int err; 321696fd7ce5SGreg Kroah-Hartman 32170019b408SJiri Slaby if (!lines || (flags & TTY_DRIVER_UNNUMBERED_NODE && lines > 1)) 32187f0bc6a6SJiri Slaby return ERR_PTR(-EINVAL); 32197f0bc6a6SJiri Slaby 322096fd7ce5SGreg Kroah-Hartman driver = kzalloc(sizeof(struct tty_driver), GFP_KERNEL); 32217f0bc6a6SJiri Slaby if (!driver) 32227f0bc6a6SJiri Slaby return ERR_PTR(-ENOMEM); 32237f0bc6a6SJiri Slaby 322496fd7ce5SGreg Kroah-Hartman kref_init(&driver->kref); 322596fd7ce5SGreg Kroah-Hartman driver->magic = TTY_DRIVER_MAGIC; 322696fd7ce5SGreg Kroah-Hartman driver->num = lines; 32271a54a76dSJiri Slaby driver->owner = owner; 32287f0bc6a6SJiri Slaby driver->flags = flags; 322916a02081SJiri Slaby 323016a02081SJiri Slaby if (!(flags & TTY_DRIVER_DEVPTS_MEM)) { 323116a02081SJiri Slaby driver->ttys = kcalloc(lines, sizeof(*driver->ttys), 323216a02081SJiri Slaby GFP_KERNEL); 323316a02081SJiri Slaby driver->termios = kcalloc(lines, sizeof(*driver->termios), 323416a02081SJiri Slaby GFP_KERNEL); 323516a02081SJiri Slaby if (!driver->ttys || !driver->termios) { 323616a02081SJiri Slaby err = -ENOMEM; 323716a02081SJiri Slaby goto err_free_all; 323816a02081SJiri Slaby } 323916a02081SJiri Slaby } 324016a02081SJiri Slaby 324116a02081SJiri Slaby if (!(flags & TTY_DRIVER_DYNAMIC_ALLOC)) { 324216a02081SJiri Slaby driver->ports = kcalloc(lines, sizeof(*driver->ports), 324316a02081SJiri Slaby GFP_KERNEL); 324416a02081SJiri Slaby if (!driver->ports) { 324516a02081SJiri Slaby err = -ENOMEM; 324616a02081SJiri Slaby goto err_free_all; 324716a02081SJiri Slaby } 32487e73eca6SJiri Slaby cdevs = lines; 32497e73eca6SJiri Slaby } 32507e73eca6SJiri Slaby 32517e73eca6SJiri Slaby driver->cdevs = kcalloc(cdevs, sizeof(*driver->cdevs), GFP_KERNEL); 32527e73eca6SJiri Slaby if (!driver->cdevs) { 32537e73eca6SJiri Slaby err = -ENOMEM; 32547e73eca6SJiri Slaby goto err_free_all; 325516a02081SJiri Slaby } 32567f0bc6a6SJiri Slaby 325796fd7ce5SGreg Kroah-Hartman return driver; 325816a02081SJiri Slaby err_free_all: 325916a02081SJiri Slaby kfree(driver->ports); 326016a02081SJiri Slaby kfree(driver->ttys); 326116a02081SJiri Slaby kfree(driver->termios); 3262a3a10ce3SRichard Watts kfree(driver->cdevs); 326316a02081SJiri Slaby kfree(driver); 326416a02081SJiri Slaby return ERR_PTR(err); 326596fd7ce5SGreg Kroah-Hartman } 32667f0bc6a6SJiri Slaby EXPORT_SYMBOL(__tty_alloc_driver); 326796fd7ce5SGreg Kroah-Hartman 326896fd7ce5SGreg Kroah-Hartman static void destruct_tty_driver(struct kref *kref) 326996fd7ce5SGreg Kroah-Hartman { 327096fd7ce5SGreg Kroah-Hartman struct tty_driver *driver = container_of(kref, struct tty_driver, kref); 327196fd7ce5SGreg Kroah-Hartman int i; 327296fd7ce5SGreg Kroah-Hartman struct ktermios *tp; 327396fd7ce5SGreg Kroah-Hartman 327496fd7ce5SGreg Kroah-Hartman if (driver->flags & TTY_DRIVER_INSTALLED) { 327596fd7ce5SGreg Kroah-Hartman for (i = 0; i < driver->num; i++) { 327696fd7ce5SGreg Kroah-Hartman tp = driver->termios[i]; 327796fd7ce5SGreg Kroah-Hartman if (tp) { 327896fd7ce5SGreg Kroah-Hartman driver->termios[i] = NULL; 327996fd7ce5SGreg Kroah-Hartman kfree(tp); 328096fd7ce5SGreg Kroah-Hartman } 328196fd7ce5SGreg Kroah-Hartman if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) 328296fd7ce5SGreg Kroah-Hartman tty_unregister_device(driver, i); 328396fd7ce5SGreg Kroah-Hartman } 328496fd7ce5SGreg Kroah-Hartman proc_tty_unregister_driver(driver); 32857e73eca6SJiri Slaby if (driver->flags & TTY_DRIVER_DYNAMIC_ALLOC) 3286a3a10ce3SRichard Watts cdev_del(driver->cdevs[0]); 328796fd7ce5SGreg Kroah-Hartman } 32887e73eca6SJiri Slaby kfree(driver->cdevs); 328904831dc1SJiri Slaby kfree(driver->ports); 329016a02081SJiri Slaby kfree(driver->termios); 329116a02081SJiri Slaby kfree(driver->ttys); 329296fd7ce5SGreg Kroah-Hartman kfree(driver); 329396fd7ce5SGreg Kroah-Hartman } 329496fd7ce5SGreg Kroah-Hartman 329596fd7ce5SGreg Kroah-Hartman void tty_driver_kref_put(struct tty_driver *driver) 329696fd7ce5SGreg Kroah-Hartman { 329796fd7ce5SGreg Kroah-Hartman kref_put(&driver->kref, destruct_tty_driver); 329896fd7ce5SGreg Kroah-Hartman } 329996fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_driver_kref_put); 330096fd7ce5SGreg Kroah-Hartman 330196fd7ce5SGreg Kroah-Hartman void tty_set_operations(struct tty_driver *driver, 330296fd7ce5SGreg Kroah-Hartman const struct tty_operations *op) 330396fd7ce5SGreg Kroah-Hartman { 330496fd7ce5SGreg Kroah-Hartman driver->ops = op; 330596fd7ce5SGreg Kroah-Hartman }; 330696fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_set_operations); 330796fd7ce5SGreg Kroah-Hartman 330896fd7ce5SGreg Kroah-Hartman void put_tty_driver(struct tty_driver *d) 330996fd7ce5SGreg Kroah-Hartman { 331096fd7ce5SGreg Kroah-Hartman tty_driver_kref_put(d); 331196fd7ce5SGreg Kroah-Hartman } 331296fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(put_tty_driver); 331396fd7ce5SGreg Kroah-Hartman 331496fd7ce5SGreg Kroah-Hartman /* 331596fd7ce5SGreg Kroah-Hartman * Called by a tty driver to register itself. 331696fd7ce5SGreg Kroah-Hartman */ 331796fd7ce5SGreg Kroah-Hartman int tty_register_driver(struct tty_driver *driver) 331896fd7ce5SGreg Kroah-Hartman { 331996fd7ce5SGreg Kroah-Hartman int error; 332096fd7ce5SGreg Kroah-Hartman int i; 332196fd7ce5SGreg Kroah-Hartman dev_t dev; 332296fd7ce5SGreg Kroah-Hartman struct device *d; 332396fd7ce5SGreg Kroah-Hartman 332496fd7ce5SGreg Kroah-Hartman if (!driver->major) { 332596fd7ce5SGreg Kroah-Hartman error = alloc_chrdev_region(&dev, driver->minor_start, 332696fd7ce5SGreg Kroah-Hartman driver->num, driver->name); 332796fd7ce5SGreg Kroah-Hartman if (!error) { 332896fd7ce5SGreg Kroah-Hartman driver->major = MAJOR(dev); 332996fd7ce5SGreg Kroah-Hartman driver->minor_start = MINOR(dev); 333096fd7ce5SGreg Kroah-Hartman } 333196fd7ce5SGreg Kroah-Hartman } else { 333296fd7ce5SGreg Kroah-Hartman dev = MKDEV(driver->major, driver->minor_start); 333396fd7ce5SGreg Kroah-Hartman error = register_chrdev_region(dev, driver->num, driver->name); 333496fd7ce5SGreg Kroah-Hartman } 33359bb8a3d4SJiri Slaby if (error < 0) 333616a02081SJiri Slaby goto err; 333796fd7ce5SGreg Kroah-Hartman 33387e73eca6SJiri Slaby if (driver->flags & TTY_DRIVER_DYNAMIC_ALLOC) { 33397e73eca6SJiri Slaby error = tty_cdev_add(driver, dev, 0, driver->num); 33409bb8a3d4SJiri Slaby if (error) 33419bb8a3d4SJiri Slaby goto err_unreg_char; 33427e73eca6SJiri Slaby } 334396fd7ce5SGreg Kroah-Hartman 334496fd7ce5SGreg Kroah-Hartman mutex_lock(&tty_mutex); 334596fd7ce5SGreg Kroah-Hartman list_add(&driver->tty_drivers, &tty_drivers); 334696fd7ce5SGreg Kroah-Hartman mutex_unlock(&tty_mutex); 334796fd7ce5SGreg Kroah-Hartman 334896fd7ce5SGreg Kroah-Hartman if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) { 334996fd7ce5SGreg Kroah-Hartman for (i = 0; i < driver->num; i++) { 335096fd7ce5SGreg Kroah-Hartman d = tty_register_device(driver, i, NULL); 335196fd7ce5SGreg Kroah-Hartman if (IS_ERR(d)) { 335296fd7ce5SGreg Kroah-Hartman error = PTR_ERR(d); 335316a02081SJiri Slaby goto err_unreg_devs; 335496fd7ce5SGreg Kroah-Hartman } 335596fd7ce5SGreg Kroah-Hartman } 335696fd7ce5SGreg Kroah-Hartman } 335796fd7ce5SGreg Kroah-Hartman proc_tty_register_driver(driver); 335896fd7ce5SGreg Kroah-Hartman driver->flags |= TTY_DRIVER_INSTALLED; 335996fd7ce5SGreg Kroah-Hartman return 0; 336096fd7ce5SGreg Kroah-Hartman 336116a02081SJiri Slaby err_unreg_devs: 336296fd7ce5SGreg Kroah-Hartman for (i--; i >= 0; i--) 336396fd7ce5SGreg Kroah-Hartman tty_unregister_device(driver, i); 336496fd7ce5SGreg Kroah-Hartman 336596fd7ce5SGreg Kroah-Hartman mutex_lock(&tty_mutex); 336696fd7ce5SGreg Kroah-Hartman list_del(&driver->tty_drivers); 336796fd7ce5SGreg Kroah-Hartman mutex_unlock(&tty_mutex); 336896fd7ce5SGreg Kroah-Hartman 33699bb8a3d4SJiri Slaby err_unreg_char: 337096fd7ce5SGreg Kroah-Hartman unregister_chrdev_region(dev, driver->num); 337116a02081SJiri Slaby err: 337296fd7ce5SGreg Kroah-Hartman return error; 337396fd7ce5SGreg Kroah-Hartman } 337496fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_register_driver); 337596fd7ce5SGreg Kroah-Hartman 337696fd7ce5SGreg Kroah-Hartman /* 337796fd7ce5SGreg Kroah-Hartman * Called by a tty driver to unregister itself. 337896fd7ce5SGreg Kroah-Hartman */ 337996fd7ce5SGreg Kroah-Hartman int tty_unregister_driver(struct tty_driver *driver) 338096fd7ce5SGreg Kroah-Hartman { 338196fd7ce5SGreg Kroah-Hartman #if 0 338296fd7ce5SGreg Kroah-Hartman /* FIXME */ 338396fd7ce5SGreg Kroah-Hartman if (driver->refcount) 338496fd7ce5SGreg Kroah-Hartman return -EBUSY; 338596fd7ce5SGreg Kroah-Hartman #endif 338696fd7ce5SGreg Kroah-Hartman unregister_chrdev_region(MKDEV(driver->major, driver->minor_start), 338796fd7ce5SGreg Kroah-Hartman driver->num); 338896fd7ce5SGreg Kroah-Hartman mutex_lock(&tty_mutex); 338996fd7ce5SGreg Kroah-Hartman list_del(&driver->tty_drivers); 339096fd7ce5SGreg Kroah-Hartman mutex_unlock(&tty_mutex); 339196fd7ce5SGreg Kroah-Hartman return 0; 339296fd7ce5SGreg Kroah-Hartman } 339396fd7ce5SGreg Kroah-Hartman 339496fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_unregister_driver); 339596fd7ce5SGreg Kroah-Hartman 339696fd7ce5SGreg Kroah-Hartman dev_t tty_devnum(struct tty_struct *tty) 339796fd7ce5SGreg Kroah-Hartman { 339896fd7ce5SGreg Kroah-Hartman return MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index; 339996fd7ce5SGreg Kroah-Hartman } 340096fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_devnum); 340196fd7ce5SGreg Kroah-Hartman 340296fd7ce5SGreg Kroah-Hartman void tty_default_fops(struct file_operations *fops) 340396fd7ce5SGreg Kroah-Hartman { 340496fd7ce5SGreg Kroah-Hartman *fops = tty_fops; 340596fd7ce5SGreg Kroah-Hartman } 340696fd7ce5SGreg Kroah-Hartman 34072c9ede55SAl Viro static char *tty_devnode(struct device *dev, umode_t *mode) 340896fd7ce5SGreg Kroah-Hartman { 340996fd7ce5SGreg Kroah-Hartman if (!mode) 341096fd7ce5SGreg Kroah-Hartman return NULL; 341196fd7ce5SGreg Kroah-Hartman if (dev->devt == MKDEV(TTYAUX_MAJOR, 0) || 341296fd7ce5SGreg Kroah-Hartman dev->devt == MKDEV(TTYAUX_MAJOR, 2)) 341396fd7ce5SGreg Kroah-Hartman *mode = 0666; 341496fd7ce5SGreg Kroah-Hartman return NULL; 341596fd7ce5SGreg Kroah-Hartman } 341696fd7ce5SGreg Kroah-Hartman 341796fd7ce5SGreg Kroah-Hartman static int __init tty_class_init(void) 341896fd7ce5SGreg Kroah-Hartman { 341996fd7ce5SGreg Kroah-Hartman tty_class = class_create(THIS_MODULE, "tty"); 342096fd7ce5SGreg Kroah-Hartman if (IS_ERR(tty_class)) 342196fd7ce5SGreg Kroah-Hartman return PTR_ERR(tty_class); 342296fd7ce5SGreg Kroah-Hartman tty_class->devnode = tty_devnode; 342396fd7ce5SGreg Kroah-Hartman return 0; 342496fd7ce5SGreg Kroah-Hartman } 342596fd7ce5SGreg Kroah-Hartman 342696fd7ce5SGreg Kroah-Hartman postcore_initcall(tty_class_init); 342796fd7ce5SGreg Kroah-Hartman 342896fd7ce5SGreg Kroah-Hartman /* 3/2004 jmc: why do these devices exist? */ 342996fd7ce5SGreg Kroah-Hartman static struct cdev tty_cdev, console_cdev; 343096fd7ce5SGreg Kroah-Hartman 3431fbc92a34SKay Sievers static ssize_t show_cons_active(struct device *dev, 3432fbc92a34SKay Sievers struct device_attribute *attr, char *buf) 3433fbc92a34SKay Sievers { 3434fbc92a34SKay Sievers struct console *cs[16]; 3435fbc92a34SKay Sievers int i = 0; 3436fbc92a34SKay Sievers struct console *c; 3437fbc92a34SKay Sievers ssize_t count = 0; 3438fbc92a34SKay Sievers 3439ac751efaSTorben Hohn console_lock(); 3440a2a6a822SKay Sievers for_each_console(c) { 3441fbc92a34SKay Sievers if (!c->device) 3442fbc92a34SKay Sievers continue; 3443fbc92a34SKay Sievers if (!c->write) 3444fbc92a34SKay Sievers continue; 3445fbc92a34SKay Sievers if ((c->flags & CON_ENABLED) == 0) 3446fbc92a34SKay Sievers continue; 3447fbc92a34SKay Sievers cs[i++] = c; 3448fbc92a34SKay Sievers if (i >= ARRAY_SIZE(cs)) 3449fbc92a34SKay Sievers break; 3450fbc92a34SKay Sievers } 3451723abd87SHannes Reinecke while (i--) { 3452723abd87SHannes Reinecke int index = cs[i]->index; 3453723abd87SHannes Reinecke struct tty_driver *drv = cs[i]->device(cs[i], &index); 3454723abd87SHannes Reinecke 3455723abd87SHannes Reinecke /* don't resolve tty0 as some programs depend on it */ 3456723abd87SHannes Reinecke if (drv && (cs[i]->index > 0 || drv->major != TTY_MAJOR)) 3457723abd87SHannes Reinecke count += tty_line_name(drv, index, buf + count); 3458723abd87SHannes Reinecke else 3459723abd87SHannes Reinecke count += sprintf(buf + count, "%s%d", 3460723abd87SHannes Reinecke cs[i]->name, cs[i]->index); 3461723abd87SHannes Reinecke 3462723abd87SHannes Reinecke count += sprintf(buf + count, "%c", i ? ' ':'\n'); 3463723abd87SHannes Reinecke } 3464ac751efaSTorben Hohn console_unlock(); 3465fbc92a34SKay Sievers 3466fbc92a34SKay Sievers return count; 3467fbc92a34SKay Sievers } 3468fbc92a34SKay Sievers static DEVICE_ATTR(active, S_IRUGO, show_cons_active, NULL); 3469fbc92a34SKay Sievers 34701083a7beSTakashi Iwai static struct attribute *cons_dev_attrs[] = { 34711083a7beSTakashi Iwai &dev_attr_active.attr, 34721083a7beSTakashi Iwai NULL 34731083a7beSTakashi Iwai }; 34741083a7beSTakashi Iwai 34751083a7beSTakashi Iwai ATTRIBUTE_GROUPS(cons_dev); 34761083a7beSTakashi Iwai 3477fbc92a34SKay Sievers static struct device *consdev; 3478fbc92a34SKay Sievers 3479fbc92a34SKay Sievers void console_sysfs_notify(void) 3480fbc92a34SKay Sievers { 3481fbc92a34SKay Sievers if (consdev) 3482fbc92a34SKay Sievers sysfs_notify(&consdev->kobj, NULL, "active"); 3483fbc92a34SKay Sievers } 3484fbc92a34SKay Sievers 348596fd7ce5SGreg Kroah-Hartman /* 348696fd7ce5SGreg Kroah-Hartman * Ok, now we can initialize the rest of the tty devices and can count 348796fd7ce5SGreg Kroah-Hartman * on memory allocations, interrupts etc.. 348896fd7ce5SGreg Kroah-Hartman */ 348996fd7ce5SGreg Kroah-Hartman int __init tty_init(void) 349096fd7ce5SGreg Kroah-Hartman { 34917c0cca7cSGreg Kroah-Hartman tty_sysctl_init(); 349296fd7ce5SGreg Kroah-Hartman cdev_init(&tty_cdev, &tty_fops); 349396fd7ce5SGreg Kroah-Hartman if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) || 349496fd7ce5SGreg Kroah-Hartman register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0) 349596fd7ce5SGreg Kroah-Hartman panic("Couldn't register /dev/tty driver\n"); 3496fbc92a34SKay Sievers device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), NULL, "tty"); 349796fd7ce5SGreg Kroah-Hartman 349896fd7ce5SGreg Kroah-Hartman cdev_init(&console_cdev, &console_fops); 349996fd7ce5SGreg Kroah-Hartman if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) || 350096fd7ce5SGreg Kroah-Hartman register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0) 350196fd7ce5SGreg Kroah-Hartman panic("Couldn't register /dev/console driver\n"); 35021083a7beSTakashi Iwai consdev = device_create_with_groups(tty_class, NULL, 35031083a7beSTakashi Iwai MKDEV(TTYAUX_MAJOR, 1), NULL, 35041083a7beSTakashi Iwai cons_dev_groups, "console"); 3505fbc92a34SKay Sievers if (IS_ERR(consdev)) 3506fbc92a34SKay Sievers consdev = NULL; 350796fd7ce5SGreg Kroah-Hartman 350896fd7ce5SGreg Kroah-Hartman #ifdef CONFIG_VT 350996fd7ce5SGreg Kroah-Hartman vty_init(&console_fops); 351096fd7ce5SGreg Kroah-Hartman #endif 351196fd7ce5SGreg Kroah-Hartman return 0; 351296fd7ce5SGreg Kroah-Hartman } 351396fd7ce5SGreg Kroah-Hartman 3514