1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright (C) 1991, 1992 Linus Torvalds
4*4882a593Smuzhiyun */
5*4882a593Smuzhiyun
6*4882a593Smuzhiyun /*
7*4882a593Smuzhiyun * 'tty_io.c' gives an orthogonal feeling to tty's, be they consoles
8*4882a593Smuzhiyun * or rs-channels. It also implements echoing, cooked mode etc.
9*4882a593Smuzhiyun *
10*4882a593Smuzhiyun * Kill-line thanks to John T Kohl, who also corrected VMIN = VTIME = 0.
11*4882a593Smuzhiyun *
12*4882a593Smuzhiyun * Modified by Theodore Ts'o, 9/14/92, to dynamically allocate the
13*4882a593Smuzhiyun * tty_struct and tty_queue structures. Previously there was an array
14*4882a593Smuzhiyun * of 256 tty_struct's which was statically allocated, and the
15*4882a593Smuzhiyun * tty_queue structures were allocated at boot time. Both are now
16*4882a593Smuzhiyun * dynamically allocated only when the tty is open.
17*4882a593Smuzhiyun *
18*4882a593Smuzhiyun * Also restructured routines so that there is more of a separation
19*4882a593Smuzhiyun * between the high-level tty routines (tty_io.c and tty_ioctl.c) and
20*4882a593Smuzhiyun * the low-level tty routines (serial.c, pty.c, console.c). This
21*4882a593Smuzhiyun * makes for cleaner and more compact code. -TYT, 9/17/92
22*4882a593Smuzhiyun *
23*4882a593Smuzhiyun * Modified by Fred N. van Kempen, 01/29/93, to add line disciplines
24*4882a593Smuzhiyun * which can be dynamically activated and de-activated by the line
25*4882a593Smuzhiyun * discipline handling modules (like SLIP).
26*4882a593Smuzhiyun *
27*4882a593Smuzhiyun * NOTE: pay no attention to the line discipline code (yet); its
28*4882a593Smuzhiyun * interface is still subject to change in this version...
29*4882a593Smuzhiyun * -- TYT, 1/31/92
30*4882a593Smuzhiyun *
31*4882a593Smuzhiyun * Added functionality to the OPOST tty handling. No delays, but all
32*4882a593Smuzhiyun * other bits should be there.
33*4882a593Smuzhiyun * -- Nick Holloway <alfie@dcs.warwick.ac.uk>, 27th May 1993.
34*4882a593Smuzhiyun *
35*4882a593Smuzhiyun * Rewrote canonical mode and added more termios flags.
36*4882a593Smuzhiyun * -- julian@uhunix.uhcc.hawaii.edu (J. Cowley), 13Jan94
37*4882a593Smuzhiyun *
38*4882a593Smuzhiyun * Reorganized FASYNC support so mouse code can share it.
39*4882a593Smuzhiyun * -- ctm@ardi.com, 9Sep95
40*4882a593Smuzhiyun *
41*4882a593Smuzhiyun * New TIOCLINUX variants added.
42*4882a593Smuzhiyun * -- mj@k332.feld.cvut.cz, 19-Nov-95
43*4882a593Smuzhiyun *
44*4882a593Smuzhiyun * Restrict vt switching via ioctl()
45*4882a593Smuzhiyun * -- grif@cs.ucr.edu, 5-Dec-95
46*4882a593Smuzhiyun *
47*4882a593Smuzhiyun * Move console and virtual terminal code to more appropriate files,
48*4882a593Smuzhiyun * implement CONFIG_VT and generalize console device interface.
49*4882a593Smuzhiyun * -- Marko Kohtala <Marko.Kohtala@hut.fi>, March 97
50*4882a593Smuzhiyun *
51*4882a593Smuzhiyun * Rewrote tty_init_dev and tty_release_dev to eliminate races.
52*4882a593Smuzhiyun * -- Bill Hawes <whawes@star.net>, June 97
53*4882a593Smuzhiyun *
54*4882a593Smuzhiyun * Added devfs support.
55*4882a593Smuzhiyun * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 13-Jan-1998
56*4882a593Smuzhiyun *
57*4882a593Smuzhiyun * Added support for a Unix98-style ptmx device.
58*4882a593Smuzhiyun * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998
59*4882a593Smuzhiyun *
60*4882a593Smuzhiyun * Reduced memory usage for older ARM systems
61*4882a593Smuzhiyun * -- Russell King <rmk@arm.linux.org.uk>
62*4882a593Smuzhiyun *
63*4882a593Smuzhiyun * Move do_SAK() into process context. Less stack use in devfs functions.
64*4882a593Smuzhiyun * alloc_tty_struct() always uses kmalloc()
65*4882a593Smuzhiyun * -- Andrew Morton <andrewm@uow.edu.eu> 17Mar01
66*4882a593Smuzhiyun */
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun #include <linux/types.h>
69*4882a593Smuzhiyun #include <linux/major.h>
70*4882a593Smuzhiyun #include <linux/errno.h>
71*4882a593Smuzhiyun #include <linux/signal.h>
72*4882a593Smuzhiyun #include <linux/fcntl.h>
73*4882a593Smuzhiyun #include <linux/sched/signal.h>
74*4882a593Smuzhiyun #include <linux/sched/task.h>
75*4882a593Smuzhiyun #include <linux/interrupt.h>
76*4882a593Smuzhiyun #include <linux/tty.h>
77*4882a593Smuzhiyun #include <linux/tty_driver.h>
78*4882a593Smuzhiyun #include <linux/tty_flip.h>
79*4882a593Smuzhiyun #include <linux/devpts_fs.h>
80*4882a593Smuzhiyun #include <linux/file.h>
81*4882a593Smuzhiyun #include <linux/fdtable.h>
82*4882a593Smuzhiyun #include <linux/console.h>
83*4882a593Smuzhiyun #include <linux/timer.h>
84*4882a593Smuzhiyun #include <linux/ctype.h>
85*4882a593Smuzhiyun #include <linux/kd.h>
86*4882a593Smuzhiyun #include <linux/mm.h>
87*4882a593Smuzhiyun #include <linux/string.h>
88*4882a593Smuzhiyun #include <linux/slab.h>
89*4882a593Smuzhiyun #include <linux/poll.h>
90*4882a593Smuzhiyun #include <linux/ppp-ioctl.h>
91*4882a593Smuzhiyun #include <linux/proc_fs.h>
92*4882a593Smuzhiyun #include <linux/init.h>
93*4882a593Smuzhiyun #include <linux/module.h>
94*4882a593Smuzhiyun #include <linux/device.h>
95*4882a593Smuzhiyun #include <linux/wait.h>
96*4882a593Smuzhiyun #include <linux/bitops.h>
97*4882a593Smuzhiyun #include <linux/delay.h>
98*4882a593Smuzhiyun #include <linux/seq_file.h>
99*4882a593Smuzhiyun #include <linux/serial.h>
100*4882a593Smuzhiyun #include <linux/ratelimit.h>
101*4882a593Smuzhiyun #include <linux/compat.h>
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun #include <linux/uaccess.h>
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun #include <linux/kbd_kern.h>
106*4882a593Smuzhiyun #include <linux/vt_kern.h>
107*4882a593Smuzhiyun #include <linux/selection.h>
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun #include <linux/kmod.h>
110*4882a593Smuzhiyun #include <linux/nsproxy.h>
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun #undef TTY_DEBUG_HANGUP
113*4882a593Smuzhiyun #ifdef TTY_DEBUG_HANGUP
114*4882a593Smuzhiyun # define tty_debug_hangup(tty, f, args...) tty_debug(tty, f, ##args)
115*4882a593Smuzhiyun #else
116*4882a593Smuzhiyun # define tty_debug_hangup(tty, f, args...) do { } while (0)
117*4882a593Smuzhiyun #endif
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun #define TTY_PARANOIA_CHECK 1
120*4882a593Smuzhiyun #define CHECK_TTY_COUNT 1
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun struct ktermios tty_std_termios = { /* for the benefit of tty drivers */
123*4882a593Smuzhiyun .c_iflag = ICRNL | IXON,
124*4882a593Smuzhiyun .c_oflag = OPOST | ONLCR,
125*4882a593Smuzhiyun .c_cflag = B38400 | CS8 | CREAD | HUPCL,
126*4882a593Smuzhiyun .c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK |
127*4882a593Smuzhiyun ECHOCTL | ECHOKE | IEXTEN,
128*4882a593Smuzhiyun .c_cc = INIT_C_CC,
129*4882a593Smuzhiyun .c_ispeed = 38400,
130*4882a593Smuzhiyun .c_ospeed = 38400,
131*4882a593Smuzhiyun /* .c_line = N_TTY, */
132*4882a593Smuzhiyun };
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun EXPORT_SYMBOL(tty_std_termios);
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun /* This list gets poked at by procfs and various bits of boot up code. This
137*4882a593Smuzhiyun could do with some rationalisation such as pulling the tty proc function
138*4882a593Smuzhiyun into this file */
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun LIST_HEAD(tty_drivers); /* linked list of tty drivers */
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun /* Mutex to protect creating and releasing a tty */
143*4882a593Smuzhiyun DEFINE_MUTEX(tty_mutex);
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun static ssize_t tty_read(struct kiocb *, struct iov_iter *);
146*4882a593Smuzhiyun static ssize_t tty_write(struct kiocb *, struct iov_iter *);
147*4882a593Smuzhiyun static __poll_t tty_poll(struct file *, poll_table *);
148*4882a593Smuzhiyun static int tty_open(struct inode *, struct file *);
149*4882a593Smuzhiyun #ifdef CONFIG_COMPAT
150*4882a593Smuzhiyun static long tty_compat_ioctl(struct file *file, unsigned int cmd,
151*4882a593Smuzhiyun unsigned long arg);
152*4882a593Smuzhiyun #else
153*4882a593Smuzhiyun #define tty_compat_ioctl NULL
154*4882a593Smuzhiyun #endif
155*4882a593Smuzhiyun static int __tty_fasync(int fd, struct file *filp, int on);
156*4882a593Smuzhiyun static int tty_fasync(int fd, struct file *filp, int on);
157*4882a593Smuzhiyun static void release_tty(struct tty_struct *tty, int idx);
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun /**
160*4882a593Smuzhiyun * free_tty_struct - free a disused tty
161*4882a593Smuzhiyun * @tty: tty struct to free
162*4882a593Smuzhiyun *
163*4882a593Smuzhiyun * Free the write buffers, tty queue and tty memory itself.
164*4882a593Smuzhiyun *
165*4882a593Smuzhiyun * Locking: none. Must be called after tty is definitely unused
166*4882a593Smuzhiyun */
167*4882a593Smuzhiyun
free_tty_struct(struct tty_struct * tty)168*4882a593Smuzhiyun static void free_tty_struct(struct tty_struct *tty)
169*4882a593Smuzhiyun {
170*4882a593Smuzhiyun tty_ldisc_deinit(tty);
171*4882a593Smuzhiyun put_device(tty->dev);
172*4882a593Smuzhiyun kfree(tty->write_buf);
173*4882a593Smuzhiyun tty->magic = 0xDEADDEAD;
174*4882a593Smuzhiyun kfree(tty);
175*4882a593Smuzhiyun }
176*4882a593Smuzhiyun
file_tty(struct file * file)177*4882a593Smuzhiyun static inline struct tty_struct *file_tty(struct file *file)
178*4882a593Smuzhiyun {
179*4882a593Smuzhiyun return ((struct tty_file_private *)file->private_data)->tty;
180*4882a593Smuzhiyun }
181*4882a593Smuzhiyun
tty_alloc_file(struct file * file)182*4882a593Smuzhiyun int tty_alloc_file(struct file *file)
183*4882a593Smuzhiyun {
184*4882a593Smuzhiyun struct tty_file_private *priv;
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun priv = kmalloc(sizeof(*priv), GFP_KERNEL);
187*4882a593Smuzhiyun if (!priv)
188*4882a593Smuzhiyun return -ENOMEM;
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun file->private_data = priv;
191*4882a593Smuzhiyun
192*4882a593Smuzhiyun return 0;
193*4882a593Smuzhiyun }
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun /* Associate a new file with the tty structure */
tty_add_file(struct tty_struct * tty,struct file * file)196*4882a593Smuzhiyun void tty_add_file(struct tty_struct *tty, struct file *file)
197*4882a593Smuzhiyun {
198*4882a593Smuzhiyun struct tty_file_private *priv = file->private_data;
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun priv->tty = tty;
201*4882a593Smuzhiyun priv->file = file;
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun spin_lock(&tty->files_lock);
204*4882a593Smuzhiyun list_add(&priv->list, &tty->tty_files);
205*4882a593Smuzhiyun spin_unlock(&tty->files_lock);
206*4882a593Smuzhiyun }
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun /**
209*4882a593Smuzhiyun * tty_free_file - free file->private_data
210*4882a593Smuzhiyun *
211*4882a593Smuzhiyun * This shall be used only for fail path handling when tty_add_file was not
212*4882a593Smuzhiyun * called yet.
213*4882a593Smuzhiyun */
tty_free_file(struct file * file)214*4882a593Smuzhiyun void tty_free_file(struct file *file)
215*4882a593Smuzhiyun {
216*4882a593Smuzhiyun struct tty_file_private *priv = file->private_data;
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun file->private_data = NULL;
219*4882a593Smuzhiyun kfree(priv);
220*4882a593Smuzhiyun }
221*4882a593Smuzhiyun
222*4882a593Smuzhiyun /* Delete file from its tty */
tty_del_file(struct file * file)223*4882a593Smuzhiyun static void tty_del_file(struct file *file)
224*4882a593Smuzhiyun {
225*4882a593Smuzhiyun struct tty_file_private *priv = file->private_data;
226*4882a593Smuzhiyun struct tty_struct *tty = priv->tty;
227*4882a593Smuzhiyun
228*4882a593Smuzhiyun spin_lock(&tty->files_lock);
229*4882a593Smuzhiyun list_del(&priv->list);
230*4882a593Smuzhiyun spin_unlock(&tty->files_lock);
231*4882a593Smuzhiyun tty_free_file(file);
232*4882a593Smuzhiyun }
233*4882a593Smuzhiyun
234*4882a593Smuzhiyun /**
235*4882a593Smuzhiyun * tty_name - return tty naming
236*4882a593Smuzhiyun * @tty: tty structure
237*4882a593Smuzhiyun *
238*4882a593Smuzhiyun * Convert a tty structure into a name. The name reflects the kernel
239*4882a593Smuzhiyun * naming policy and if udev is in use may not reflect user space
240*4882a593Smuzhiyun *
241*4882a593Smuzhiyun * Locking: none
242*4882a593Smuzhiyun */
243*4882a593Smuzhiyun
tty_name(const struct tty_struct * tty)244*4882a593Smuzhiyun const char *tty_name(const struct tty_struct *tty)
245*4882a593Smuzhiyun {
246*4882a593Smuzhiyun if (!tty) /* Hmm. NULL pointer. That's fun. */
247*4882a593Smuzhiyun return "NULL tty";
248*4882a593Smuzhiyun return tty->name;
249*4882a593Smuzhiyun }
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun EXPORT_SYMBOL(tty_name);
252*4882a593Smuzhiyun
tty_driver_name(const struct tty_struct * tty)253*4882a593Smuzhiyun const char *tty_driver_name(const struct tty_struct *tty)
254*4882a593Smuzhiyun {
255*4882a593Smuzhiyun if (!tty || !tty->driver)
256*4882a593Smuzhiyun return "";
257*4882a593Smuzhiyun return tty->driver->name;
258*4882a593Smuzhiyun }
259*4882a593Smuzhiyun
tty_paranoia_check(struct tty_struct * tty,struct inode * inode,const char * routine)260*4882a593Smuzhiyun static int tty_paranoia_check(struct tty_struct *tty, struct inode *inode,
261*4882a593Smuzhiyun const char *routine)
262*4882a593Smuzhiyun {
263*4882a593Smuzhiyun #ifdef TTY_PARANOIA_CHECK
264*4882a593Smuzhiyun if (!tty) {
265*4882a593Smuzhiyun pr_warn("(%d:%d): %s: NULL tty\n",
266*4882a593Smuzhiyun imajor(inode), iminor(inode), routine);
267*4882a593Smuzhiyun return 1;
268*4882a593Smuzhiyun }
269*4882a593Smuzhiyun if (tty->magic != TTY_MAGIC) {
270*4882a593Smuzhiyun pr_warn("(%d:%d): %s: bad magic number\n",
271*4882a593Smuzhiyun imajor(inode), iminor(inode), routine);
272*4882a593Smuzhiyun return 1;
273*4882a593Smuzhiyun }
274*4882a593Smuzhiyun #endif
275*4882a593Smuzhiyun return 0;
276*4882a593Smuzhiyun }
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun /* Caller must hold tty_lock */
check_tty_count(struct tty_struct * tty,const char * routine)279*4882a593Smuzhiyun static int check_tty_count(struct tty_struct *tty, const char *routine)
280*4882a593Smuzhiyun {
281*4882a593Smuzhiyun #ifdef CHECK_TTY_COUNT
282*4882a593Smuzhiyun struct list_head *p;
283*4882a593Smuzhiyun int count = 0, kopen_count = 0;
284*4882a593Smuzhiyun
285*4882a593Smuzhiyun spin_lock(&tty->files_lock);
286*4882a593Smuzhiyun list_for_each(p, &tty->tty_files) {
287*4882a593Smuzhiyun count++;
288*4882a593Smuzhiyun }
289*4882a593Smuzhiyun spin_unlock(&tty->files_lock);
290*4882a593Smuzhiyun if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
291*4882a593Smuzhiyun tty->driver->subtype == PTY_TYPE_SLAVE &&
292*4882a593Smuzhiyun tty->link && tty->link->count)
293*4882a593Smuzhiyun count++;
294*4882a593Smuzhiyun if (tty_port_kopened(tty->port))
295*4882a593Smuzhiyun kopen_count++;
296*4882a593Smuzhiyun if (tty->count != (count + kopen_count)) {
297*4882a593Smuzhiyun tty_warn(tty, "%s: tty->count(%d) != (#fd's(%d) + #kopen's(%d))\n",
298*4882a593Smuzhiyun routine, tty->count, count, kopen_count);
299*4882a593Smuzhiyun return (count + kopen_count);
300*4882a593Smuzhiyun }
301*4882a593Smuzhiyun #endif
302*4882a593Smuzhiyun return 0;
303*4882a593Smuzhiyun }
304*4882a593Smuzhiyun
305*4882a593Smuzhiyun /**
306*4882a593Smuzhiyun * get_tty_driver - find device of a tty
307*4882a593Smuzhiyun * @device: device identifier
308*4882a593Smuzhiyun * @index: returns the index of the tty
309*4882a593Smuzhiyun *
310*4882a593Smuzhiyun * This routine returns a tty driver structure, given a device number
311*4882a593Smuzhiyun * and also passes back the index number.
312*4882a593Smuzhiyun *
313*4882a593Smuzhiyun * Locking: caller must hold tty_mutex
314*4882a593Smuzhiyun */
315*4882a593Smuzhiyun
get_tty_driver(dev_t device,int * index)316*4882a593Smuzhiyun static struct tty_driver *get_tty_driver(dev_t device, int *index)
317*4882a593Smuzhiyun {
318*4882a593Smuzhiyun struct tty_driver *p;
319*4882a593Smuzhiyun
320*4882a593Smuzhiyun list_for_each_entry(p, &tty_drivers, tty_drivers) {
321*4882a593Smuzhiyun dev_t base = MKDEV(p->major, p->minor_start);
322*4882a593Smuzhiyun if (device < base || device >= base + p->num)
323*4882a593Smuzhiyun continue;
324*4882a593Smuzhiyun *index = device - base;
325*4882a593Smuzhiyun return tty_driver_kref_get(p);
326*4882a593Smuzhiyun }
327*4882a593Smuzhiyun return NULL;
328*4882a593Smuzhiyun }
329*4882a593Smuzhiyun
330*4882a593Smuzhiyun /**
331*4882a593Smuzhiyun * tty_dev_name_to_number - return dev_t for device name
332*4882a593Smuzhiyun * @name: user space name of device under /dev
333*4882a593Smuzhiyun * @number: pointer to dev_t that this function will populate
334*4882a593Smuzhiyun *
335*4882a593Smuzhiyun * This function converts device names like ttyS0 or ttyUSB1 into dev_t
336*4882a593Smuzhiyun * like (4, 64) or (188, 1). If no corresponding driver is registered then
337*4882a593Smuzhiyun * the function returns -ENODEV.
338*4882a593Smuzhiyun *
339*4882a593Smuzhiyun * Locking: this acquires tty_mutex to protect the tty_drivers list from
340*4882a593Smuzhiyun * being modified while we are traversing it, and makes sure to
341*4882a593Smuzhiyun * release it before exiting.
342*4882a593Smuzhiyun */
tty_dev_name_to_number(const char * name,dev_t * number)343*4882a593Smuzhiyun int tty_dev_name_to_number(const char *name, dev_t *number)
344*4882a593Smuzhiyun {
345*4882a593Smuzhiyun struct tty_driver *p;
346*4882a593Smuzhiyun int ret;
347*4882a593Smuzhiyun int index, prefix_length = 0;
348*4882a593Smuzhiyun const char *str;
349*4882a593Smuzhiyun
350*4882a593Smuzhiyun for (str = name; *str && !isdigit(*str); str++)
351*4882a593Smuzhiyun ;
352*4882a593Smuzhiyun
353*4882a593Smuzhiyun if (!*str)
354*4882a593Smuzhiyun return -EINVAL;
355*4882a593Smuzhiyun
356*4882a593Smuzhiyun ret = kstrtoint(str, 10, &index);
357*4882a593Smuzhiyun if (ret)
358*4882a593Smuzhiyun return ret;
359*4882a593Smuzhiyun
360*4882a593Smuzhiyun prefix_length = str - name;
361*4882a593Smuzhiyun mutex_lock(&tty_mutex);
362*4882a593Smuzhiyun
363*4882a593Smuzhiyun list_for_each_entry(p, &tty_drivers, tty_drivers)
364*4882a593Smuzhiyun if (prefix_length == strlen(p->name) && strncmp(name,
365*4882a593Smuzhiyun p->name, prefix_length) == 0) {
366*4882a593Smuzhiyun if (index < p->num) {
367*4882a593Smuzhiyun *number = MKDEV(p->major, p->minor_start + index);
368*4882a593Smuzhiyun goto out;
369*4882a593Smuzhiyun }
370*4882a593Smuzhiyun }
371*4882a593Smuzhiyun
372*4882a593Smuzhiyun /* if here then driver wasn't found */
373*4882a593Smuzhiyun ret = -ENODEV;
374*4882a593Smuzhiyun out:
375*4882a593Smuzhiyun mutex_unlock(&tty_mutex);
376*4882a593Smuzhiyun return ret;
377*4882a593Smuzhiyun }
378*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(tty_dev_name_to_number);
379*4882a593Smuzhiyun
380*4882a593Smuzhiyun #ifdef CONFIG_CONSOLE_POLL
381*4882a593Smuzhiyun
382*4882a593Smuzhiyun /**
383*4882a593Smuzhiyun * tty_find_polling_driver - find device of a polled tty
384*4882a593Smuzhiyun * @name: name string to match
385*4882a593Smuzhiyun * @line: pointer to resulting tty line nr
386*4882a593Smuzhiyun *
387*4882a593Smuzhiyun * This routine returns a tty driver structure, given a name
388*4882a593Smuzhiyun * and the condition that the tty driver is capable of polled
389*4882a593Smuzhiyun * operation.
390*4882a593Smuzhiyun */
tty_find_polling_driver(char * name,int * line)391*4882a593Smuzhiyun struct tty_driver *tty_find_polling_driver(char *name, int *line)
392*4882a593Smuzhiyun {
393*4882a593Smuzhiyun struct tty_driver *p, *res = NULL;
394*4882a593Smuzhiyun int tty_line = 0;
395*4882a593Smuzhiyun int len;
396*4882a593Smuzhiyun char *str, *stp;
397*4882a593Smuzhiyun
398*4882a593Smuzhiyun for (str = name; *str; str++)
399*4882a593Smuzhiyun if ((*str >= '0' && *str <= '9') || *str == ',')
400*4882a593Smuzhiyun break;
401*4882a593Smuzhiyun if (!*str)
402*4882a593Smuzhiyun return NULL;
403*4882a593Smuzhiyun
404*4882a593Smuzhiyun len = str - name;
405*4882a593Smuzhiyun tty_line = simple_strtoul(str, &str, 10);
406*4882a593Smuzhiyun
407*4882a593Smuzhiyun mutex_lock(&tty_mutex);
408*4882a593Smuzhiyun /* Search through the tty devices to look for a match */
409*4882a593Smuzhiyun list_for_each_entry(p, &tty_drivers, tty_drivers) {
410*4882a593Smuzhiyun if (!len || strncmp(name, p->name, len) != 0)
411*4882a593Smuzhiyun continue;
412*4882a593Smuzhiyun stp = str;
413*4882a593Smuzhiyun if (*stp == ',')
414*4882a593Smuzhiyun stp++;
415*4882a593Smuzhiyun if (*stp == '\0')
416*4882a593Smuzhiyun stp = NULL;
417*4882a593Smuzhiyun
418*4882a593Smuzhiyun if (tty_line >= 0 && tty_line < p->num && p->ops &&
419*4882a593Smuzhiyun p->ops->poll_init && !p->ops->poll_init(p, tty_line, stp)) {
420*4882a593Smuzhiyun res = tty_driver_kref_get(p);
421*4882a593Smuzhiyun *line = tty_line;
422*4882a593Smuzhiyun break;
423*4882a593Smuzhiyun }
424*4882a593Smuzhiyun }
425*4882a593Smuzhiyun mutex_unlock(&tty_mutex);
426*4882a593Smuzhiyun
427*4882a593Smuzhiyun return res;
428*4882a593Smuzhiyun }
429*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(tty_find_polling_driver);
430*4882a593Smuzhiyun #endif
431*4882a593Smuzhiyun
hung_up_tty_read(struct kiocb * iocb,struct iov_iter * to)432*4882a593Smuzhiyun static ssize_t hung_up_tty_read(struct kiocb *iocb, struct iov_iter *to)
433*4882a593Smuzhiyun {
434*4882a593Smuzhiyun return 0;
435*4882a593Smuzhiyun }
436*4882a593Smuzhiyun
hung_up_tty_write(struct kiocb * iocb,struct iov_iter * from)437*4882a593Smuzhiyun static ssize_t hung_up_tty_write(struct kiocb *iocb, struct iov_iter *from)
438*4882a593Smuzhiyun {
439*4882a593Smuzhiyun return -EIO;
440*4882a593Smuzhiyun }
441*4882a593Smuzhiyun
442*4882a593Smuzhiyun /* No kernel lock held - none needed ;) */
hung_up_tty_poll(struct file * filp,poll_table * wait)443*4882a593Smuzhiyun static __poll_t hung_up_tty_poll(struct file *filp, poll_table *wait)
444*4882a593Smuzhiyun {
445*4882a593Smuzhiyun return EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP | EPOLLRDNORM | EPOLLWRNORM;
446*4882a593Smuzhiyun }
447*4882a593Smuzhiyun
hung_up_tty_ioctl(struct file * file,unsigned int cmd,unsigned long arg)448*4882a593Smuzhiyun static long hung_up_tty_ioctl(struct file *file, unsigned int cmd,
449*4882a593Smuzhiyun unsigned long arg)
450*4882a593Smuzhiyun {
451*4882a593Smuzhiyun return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
452*4882a593Smuzhiyun }
453*4882a593Smuzhiyun
hung_up_tty_compat_ioctl(struct file * file,unsigned int cmd,unsigned long arg)454*4882a593Smuzhiyun static long hung_up_tty_compat_ioctl(struct file *file,
455*4882a593Smuzhiyun unsigned int cmd, unsigned long arg)
456*4882a593Smuzhiyun {
457*4882a593Smuzhiyun return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
458*4882a593Smuzhiyun }
459*4882a593Smuzhiyun
hung_up_tty_fasync(int fd,struct file * file,int on)460*4882a593Smuzhiyun static int hung_up_tty_fasync(int fd, struct file *file, int on)
461*4882a593Smuzhiyun {
462*4882a593Smuzhiyun return -ENOTTY;
463*4882a593Smuzhiyun }
464*4882a593Smuzhiyun
tty_show_fdinfo(struct seq_file * m,struct file * file)465*4882a593Smuzhiyun static void tty_show_fdinfo(struct seq_file *m, struct file *file)
466*4882a593Smuzhiyun {
467*4882a593Smuzhiyun struct tty_struct *tty = file_tty(file);
468*4882a593Smuzhiyun
469*4882a593Smuzhiyun if (tty && tty->ops && tty->ops->show_fdinfo)
470*4882a593Smuzhiyun tty->ops->show_fdinfo(tty, m);
471*4882a593Smuzhiyun }
472*4882a593Smuzhiyun
473*4882a593Smuzhiyun static const struct file_operations tty_fops = {
474*4882a593Smuzhiyun .llseek = no_llseek,
475*4882a593Smuzhiyun .read_iter = tty_read,
476*4882a593Smuzhiyun .write_iter = tty_write,
477*4882a593Smuzhiyun .splice_read = generic_file_splice_read,
478*4882a593Smuzhiyun .splice_write = iter_file_splice_write,
479*4882a593Smuzhiyun .poll = tty_poll,
480*4882a593Smuzhiyun .unlocked_ioctl = tty_ioctl,
481*4882a593Smuzhiyun .compat_ioctl = tty_compat_ioctl,
482*4882a593Smuzhiyun .open = tty_open,
483*4882a593Smuzhiyun .release = tty_release,
484*4882a593Smuzhiyun .fasync = tty_fasync,
485*4882a593Smuzhiyun .show_fdinfo = tty_show_fdinfo,
486*4882a593Smuzhiyun };
487*4882a593Smuzhiyun
488*4882a593Smuzhiyun static const struct file_operations console_fops = {
489*4882a593Smuzhiyun .llseek = no_llseek,
490*4882a593Smuzhiyun .read_iter = tty_read,
491*4882a593Smuzhiyun .write_iter = redirected_tty_write,
492*4882a593Smuzhiyun .splice_read = generic_file_splice_read,
493*4882a593Smuzhiyun .splice_write = iter_file_splice_write,
494*4882a593Smuzhiyun .poll = tty_poll,
495*4882a593Smuzhiyun .unlocked_ioctl = tty_ioctl,
496*4882a593Smuzhiyun .compat_ioctl = tty_compat_ioctl,
497*4882a593Smuzhiyun .open = tty_open,
498*4882a593Smuzhiyun .release = tty_release,
499*4882a593Smuzhiyun .fasync = tty_fasync,
500*4882a593Smuzhiyun };
501*4882a593Smuzhiyun
502*4882a593Smuzhiyun static const struct file_operations hung_up_tty_fops = {
503*4882a593Smuzhiyun .llseek = no_llseek,
504*4882a593Smuzhiyun .read_iter = hung_up_tty_read,
505*4882a593Smuzhiyun .write_iter = hung_up_tty_write,
506*4882a593Smuzhiyun .poll = hung_up_tty_poll,
507*4882a593Smuzhiyun .unlocked_ioctl = hung_up_tty_ioctl,
508*4882a593Smuzhiyun .compat_ioctl = hung_up_tty_compat_ioctl,
509*4882a593Smuzhiyun .release = tty_release,
510*4882a593Smuzhiyun .fasync = hung_up_tty_fasync,
511*4882a593Smuzhiyun };
512*4882a593Smuzhiyun
513*4882a593Smuzhiyun static DEFINE_SPINLOCK(redirect_lock);
514*4882a593Smuzhiyun static struct file *redirect;
515*4882a593Smuzhiyun
516*4882a593Smuzhiyun extern void tty_sysctl_init(void);
517*4882a593Smuzhiyun
518*4882a593Smuzhiyun /**
519*4882a593Smuzhiyun * tty_wakeup - request more data
520*4882a593Smuzhiyun * @tty: terminal
521*4882a593Smuzhiyun *
522*4882a593Smuzhiyun * Internal and external helper for wakeups of tty. This function
523*4882a593Smuzhiyun * informs the line discipline if present that the driver is ready
524*4882a593Smuzhiyun * to receive more output data.
525*4882a593Smuzhiyun */
526*4882a593Smuzhiyun
tty_wakeup(struct tty_struct * tty)527*4882a593Smuzhiyun void tty_wakeup(struct tty_struct *tty)
528*4882a593Smuzhiyun {
529*4882a593Smuzhiyun struct tty_ldisc *ld;
530*4882a593Smuzhiyun
531*4882a593Smuzhiyun if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) {
532*4882a593Smuzhiyun ld = tty_ldisc_ref(tty);
533*4882a593Smuzhiyun if (ld) {
534*4882a593Smuzhiyun if (ld->ops->write_wakeup)
535*4882a593Smuzhiyun ld->ops->write_wakeup(tty);
536*4882a593Smuzhiyun tty_ldisc_deref(ld);
537*4882a593Smuzhiyun }
538*4882a593Smuzhiyun }
539*4882a593Smuzhiyun wake_up_interruptible_poll(&tty->write_wait, EPOLLOUT);
540*4882a593Smuzhiyun }
541*4882a593Smuzhiyun
542*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(tty_wakeup);
543*4882a593Smuzhiyun
544*4882a593Smuzhiyun /**
545*4882a593Smuzhiyun * __tty_hangup - actual handler for hangup events
546*4882a593Smuzhiyun * @tty: tty device
547*4882a593Smuzhiyun *
548*4882a593Smuzhiyun * This can be called by a "kworker" kernel thread. That is process
549*4882a593Smuzhiyun * synchronous but doesn't hold any locks, so we need to make sure we
550*4882a593Smuzhiyun * have the appropriate locks for what we're doing.
551*4882a593Smuzhiyun *
552*4882a593Smuzhiyun * The hangup event clears any pending redirections onto the hung up
553*4882a593Smuzhiyun * device. It ensures future writes will error and it does the needed
554*4882a593Smuzhiyun * line discipline hangup and signal delivery. The tty object itself
555*4882a593Smuzhiyun * remains intact.
556*4882a593Smuzhiyun *
557*4882a593Smuzhiyun * Locking:
558*4882a593Smuzhiyun * BTM
559*4882a593Smuzhiyun * redirect lock for undoing redirection
560*4882a593Smuzhiyun * file list lock for manipulating list of ttys
561*4882a593Smuzhiyun * tty_ldiscs_lock from called functions
562*4882a593Smuzhiyun * termios_rwsem resetting termios data
563*4882a593Smuzhiyun * tasklist_lock to walk task list for hangup event
564*4882a593Smuzhiyun * ->siglock to protect ->signal/->sighand
565*4882a593Smuzhiyun */
__tty_hangup(struct tty_struct * tty,int exit_session)566*4882a593Smuzhiyun static void __tty_hangup(struct tty_struct *tty, int exit_session)
567*4882a593Smuzhiyun {
568*4882a593Smuzhiyun struct file *cons_filp = NULL;
569*4882a593Smuzhiyun struct file *filp, *f = NULL;
570*4882a593Smuzhiyun struct tty_file_private *priv;
571*4882a593Smuzhiyun int closecount = 0, n;
572*4882a593Smuzhiyun int refs;
573*4882a593Smuzhiyun
574*4882a593Smuzhiyun if (!tty)
575*4882a593Smuzhiyun return;
576*4882a593Smuzhiyun
577*4882a593Smuzhiyun
578*4882a593Smuzhiyun spin_lock(&redirect_lock);
579*4882a593Smuzhiyun if (redirect && file_tty(redirect) == tty) {
580*4882a593Smuzhiyun f = redirect;
581*4882a593Smuzhiyun redirect = NULL;
582*4882a593Smuzhiyun }
583*4882a593Smuzhiyun spin_unlock(&redirect_lock);
584*4882a593Smuzhiyun
585*4882a593Smuzhiyun tty_lock(tty);
586*4882a593Smuzhiyun
587*4882a593Smuzhiyun if (test_bit(TTY_HUPPED, &tty->flags)) {
588*4882a593Smuzhiyun tty_unlock(tty);
589*4882a593Smuzhiyun return;
590*4882a593Smuzhiyun }
591*4882a593Smuzhiyun
592*4882a593Smuzhiyun /*
593*4882a593Smuzhiyun * Some console devices aren't actually hung up for technical and
594*4882a593Smuzhiyun * historical reasons, which can lead to indefinite interruptible
595*4882a593Smuzhiyun * sleep in n_tty_read(). The following explicitly tells
596*4882a593Smuzhiyun * n_tty_read() to abort readers.
597*4882a593Smuzhiyun */
598*4882a593Smuzhiyun set_bit(TTY_HUPPING, &tty->flags);
599*4882a593Smuzhiyun
600*4882a593Smuzhiyun /* inuse_filps is protected by the single tty lock,
601*4882a593Smuzhiyun this really needs to change if we want to flush the
602*4882a593Smuzhiyun workqueue with the lock held */
603*4882a593Smuzhiyun check_tty_count(tty, "tty_hangup");
604*4882a593Smuzhiyun
605*4882a593Smuzhiyun spin_lock(&tty->files_lock);
606*4882a593Smuzhiyun /* This breaks for file handles being sent over AF_UNIX sockets ? */
607*4882a593Smuzhiyun list_for_each_entry(priv, &tty->tty_files, list) {
608*4882a593Smuzhiyun filp = priv->file;
609*4882a593Smuzhiyun if (filp->f_op->write_iter == redirected_tty_write)
610*4882a593Smuzhiyun cons_filp = filp;
611*4882a593Smuzhiyun if (filp->f_op->write_iter != tty_write)
612*4882a593Smuzhiyun continue;
613*4882a593Smuzhiyun closecount++;
614*4882a593Smuzhiyun __tty_fasync(-1, filp, 0); /* can't block */
615*4882a593Smuzhiyun filp->f_op = &hung_up_tty_fops;
616*4882a593Smuzhiyun }
617*4882a593Smuzhiyun spin_unlock(&tty->files_lock);
618*4882a593Smuzhiyun
619*4882a593Smuzhiyun refs = tty_signal_session_leader(tty, exit_session);
620*4882a593Smuzhiyun /* Account for the p->signal references we killed */
621*4882a593Smuzhiyun while (refs--)
622*4882a593Smuzhiyun tty_kref_put(tty);
623*4882a593Smuzhiyun
624*4882a593Smuzhiyun tty_ldisc_hangup(tty, cons_filp != NULL);
625*4882a593Smuzhiyun
626*4882a593Smuzhiyun spin_lock_irq(&tty->ctrl_lock);
627*4882a593Smuzhiyun clear_bit(TTY_THROTTLED, &tty->flags);
628*4882a593Smuzhiyun clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
629*4882a593Smuzhiyun put_pid(tty->session);
630*4882a593Smuzhiyun put_pid(tty->pgrp);
631*4882a593Smuzhiyun tty->session = NULL;
632*4882a593Smuzhiyun tty->pgrp = NULL;
633*4882a593Smuzhiyun tty->ctrl_status = 0;
634*4882a593Smuzhiyun spin_unlock_irq(&tty->ctrl_lock);
635*4882a593Smuzhiyun
636*4882a593Smuzhiyun /*
637*4882a593Smuzhiyun * If one of the devices matches a console pointer, we
638*4882a593Smuzhiyun * cannot just call hangup() because that will cause
639*4882a593Smuzhiyun * tty->count and state->count to go out of sync.
640*4882a593Smuzhiyun * So we just call close() the right number of times.
641*4882a593Smuzhiyun */
642*4882a593Smuzhiyun if (cons_filp) {
643*4882a593Smuzhiyun if (tty->ops->close)
644*4882a593Smuzhiyun for (n = 0; n < closecount; n++)
645*4882a593Smuzhiyun tty->ops->close(tty, cons_filp);
646*4882a593Smuzhiyun } else if (tty->ops->hangup)
647*4882a593Smuzhiyun tty->ops->hangup(tty);
648*4882a593Smuzhiyun /*
649*4882a593Smuzhiyun * We don't want to have driver/ldisc interactions beyond the ones
650*4882a593Smuzhiyun * we did here. The driver layer expects no calls after ->hangup()
651*4882a593Smuzhiyun * from the ldisc side, which is now guaranteed.
652*4882a593Smuzhiyun */
653*4882a593Smuzhiyun set_bit(TTY_HUPPED, &tty->flags);
654*4882a593Smuzhiyun clear_bit(TTY_HUPPING, &tty->flags);
655*4882a593Smuzhiyun tty_unlock(tty);
656*4882a593Smuzhiyun
657*4882a593Smuzhiyun if (f)
658*4882a593Smuzhiyun fput(f);
659*4882a593Smuzhiyun }
660*4882a593Smuzhiyun
do_tty_hangup(struct work_struct * work)661*4882a593Smuzhiyun static void do_tty_hangup(struct work_struct *work)
662*4882a593Smuzhiyun {
663*4882a593Smuzhiyun struct tty_struct *tty =
664*4882a593Smuzhiyun container_of(work, struct tty_struct, hangup_work);
665*4882a593Smuzhiyun
666*4882a593Smuzhiyun __tty_hangup(tty, 0);
667*4882a593Smuzhiyun }
668*4882a593Smuzhiyun
669*4882a593Smuzhiyun /**
670*4882a593Smuzhiyun * tty_hangup - trigger a hangup event
671*4882a593Smuzhiyun * @tty: tty to hangup
672*4882a593Smuzhiyun *
673*4882a593Smuzhiyun * A carrier loss (virtual or otherwise) has occurred on this like
674*4882a593Smuzhiyun * schedule a hangup sequence to run after this event.
675*4882a593Smuzhiyun */
676*4882a593Smuzhiyun
tty_hangup(struct tty_struct * tty)677*4882a593Smuzhiyun void tty_hangup(struct tty_struct *tty)
678*4882a593Smuzhiyun {
679*4882a593Smuzhiyun tty_debug_hangup(tty, "hangup\n");
680*4882a593Smuzhiyun schedule_work(&tty->hangup_work);
681*4882a593Smuzhiyun }
682*4882a593Smuzhiyun
683*4882a593Smuzhiyun EXPORT_SYMBOL(tty_hangup);
684*4882a593Smuzhiyun
685*4882a593Smuzhiyun /**
686*4882a593Smuzhiyun * tty_vhangup - process vhangup
687*4882a593Smuzhiyun * @tty: tty to hangup
688*4882a593Smuzhiyun *
689*4882a593Smuzhiyun * The user has asked via system call for the terminal to be hung up.
690*4882a593Smuzhiyun * We do this synchronously so that when the syscall returns the process
691*4882a593Smuzhiyun * is complete. That guarantee is necessary for security reasons.
692*4882a593Smuzhiyun */
693*4882a593Smuzhiyun
tty_vhangup(struct tty_struct * tty)694*4882a593Smuzhiyun void tty_vhangup(struct tty_struct *tty)
695*4882a593Smuzhiyun {
696*4882a593Smuzhiyun tty_debug_hangup(tty, "vhangup\n");
697*4882a593Smuzhiyun __tty_hangup(tty, 0);
698*4882a593Smuzhiyun }
699*4882a593Smuzhiyun
700*4882a593Smuzhiyun EXPORT_SYMBOL(tty_vhangup);
701*4882a593Smuzhiyun
702*4882a593Smuzhiyun
703*4882a593Smuzhiyun /**
704*4882a593Smuzhiyun * tty_vhangup_self - process vhangup for own ctty
705*4882a593Smuzhiyun *
706*4882a593Smuzhiyun * Perform a vhangup on the current controlling tty
707*4882a593Smuzhiyun */
708*4882a593Smuzhiyun
tty_vhangup_self(void)709*4882a593Smuzhiyun void tty_vhangup_self(void)
710*4882a593Smuzhiyun {
711*4882a593Smuzhiyun struct tty_struct *tty;
712*4882a593Smuzhiyun
713*4882a593Smuzhiyun tty = get_current_tty();
714*4882a593Smuzhiyun if (tty) {
715*4882a593Smuzhiyun tty_vhangup(tty);
716*4882a593Smuzhiyun tty_kref_put(tty);
717*4882a593Smuzhiyun }
718*4882a593Smuzhiyun }
719*4882a593Smuzhiyun
720*4882a593Smuzhiyun /**
721*4882a593Smuzhiyun * tty_vhangup_session - hangup session leader exit
722*4882a593Smuzhiyun * @tty: tty to hangup
723*4882a593Smuzhiyun *
724*4882a593Smuzhiyun * The session leader is exiting and hanging up its controlling terminal.
725*4882a593Smuzhiyun * Every process in the foreground process group is signalled SIGHUP.
726*4882a593Smuzhiyun *
727*4882a593Smuzhiyun * We do this synchronously so that when the syscall returns the process
728*4882a593Smuzhiyun * is complete. That guarantee is necessary for security reasons.
729*4882a593Smuzhiyun */
730*4882a593Smuzhiyun
tty_vhangup_session(struct tty_struct * tty)731*4882a593Smuzhiyun void tty_vhangup_session(struct tty_struct *tty)
732*4882a593Smuzhiyun {
733*4882a593Smuzhiyun tty_debug_hangup(tty, "session hangup\n");
734*4882a593Smuzhiyun __tty_hangup(tty, 1);
735*4882a593Smuzhiyun }
736*4882a593Smuzhiyun
737*4882a593Smuzhiyun /**
738*4882a593Smuzhiyun * tty_hung_up_p - was tty hung up
739*4882a593Smuzhiyun * @filp: file pointer of tty
740*4882a593Smuzhiyun *
741*4882a593Smuzhiyun * Return true if the tty has been subject to a vhangup or a carrier
742*4882a593Smuzhiyun * loss
743*4882a593Smuzhiyun */
744*4882a593Smuzhiyun
tty_hung_up_p(struct file * filp)745*4882a593Smuzhiyun int tty_hung_up_p(struct file *filp)
746*4882a593Smuzhiyun {
747*4882a593Smuzhiyun return (filp && filp->f_op == &hung_up_tty_fops);
748*4882a593Smuzhiyun }
749*4882a593Smuzhiyun
750*4882a593Smuzhiyun EXPORT_SYMBOL(tty_hung_up_p);
751*4882a593Smuzhiyun
752*4882a593Smuzhiyun /**
753*4882a593Smuzhiyun * stop_tty - propagate flow control
754*4882a593Smuzhiyun * @tty: tty to stop
755*4882a593Smuzhiyun *
756*4882a593Smuzhiyun * Perform flow control to the driver. May be called
757*4882a593Smuzhiyun * on an already stopped device and will not re-call the driver
758*4882a593Smuzhiyun * method.
759*4882a593Smuzhiyun *
760*4882a593Smuzhiyun * This functionality is used by both the line disciplines for
761*4882a593Smuzhiyun * halting incoming flow and by the driver. It may therefore be
762*4882a593Smuzhiyun * called from any context, may be under the tty atomic_write_lock
763*4882a593Smuzhiyun * but not always.
764*4882a593Smuzhiyun *
765*4882a593Smuzhiyun * Locking:
766*4882a593Smuzhiyun * flow_lock
767*4882a593Smuzhiyun */
768*4882a593Smuzhiyun
__stop_tty(struct tty_struct * tty)769*4882a593Smuzhiyun void __stop_tty(struct tty_struct *tty)
770*4882a593Smuzhiyun {
771*4882a593Smuzhiyun if (tty->stopped)
772*4882a593Smuzhiyun return;
773*4882a593Smuzhiyun tty->stopped = 1;
774*4882a593Smuzhiyun if (tty->ops->stop)
775*4882a593Smuzhiyun tty->ops->stop(tty);
776*4882a593Smuzhiyun }
777*4882a593Smuzhiyun
stop_tty(struct tty_struct * tty)778*4882a593Smuzhiyun void stop_tty(struct tty_struct *tty)
779*4882a593Smuzhiyun {
780*4882a593Smuzhiyun unsigned long flags;
781*4882a593Smuzhiyun
782*4882a593Smuzhiyun spin_lock_irqsave(&tty->flow_lock, flags);
783*4882a593Smuzhiyun __stop_tty(tty);
784*4882a593Smuzhiyun spin_unlock_irqrestore(&tty->flow_lock, flags);
785*4882a593Smuzhiyun }
786*4882a593Smuzhiyun EXPORT_SYMBOL(stop_tty);
787*4882a593Smuzhiyun
788*4882a593Smuzhiyun /**
789*4882a593Smuzhiyun * start_tty - propagate flow control
790*4882a593Smuzhiyun * @tty: tty to start
791*4882a593Smuzhiyun *
792*4882a593Smuzhiyun * Start a tty that has been stopped if at all possible. If this
793*4882a593Smuzhiyun * tty was previous stopped and is now being started, the driver
794*4882a593Smuzhiyun * start method is invoked and the line discipline woken.
795*4882a593Smuzhiyun *
796*4882a593Smuzhiyun * Locking:
797*4882a593Smuzhiyun * flow_lock
798*4882a593Smuzhiyun */
799*4882a593Smuzhiyun
__start_tty(struct tty_struct * tty)800*4882a593Smuzhiyun void __start_tty(struct tty_struct *tty)
801*4882a593Smuzhiyun {
802*4882a593Smuzhiyun if (!tty->stopped || tty->flow_stopped)
803*4882a593Smuzhiyun return;
804*4882a593Smuzhiyun tty->stopped = 0;
805*4882a593Smuzhiyun if (tty->ops->start)
806*4882a593Smuzhiyun tty->ops->start(tty);
807*4882a593Smuzhiyun tty_wakeup(tty);
808*4882a593Smuzhiyun }
809*4882a593Smuzhiyun
start_tty(struct tty_struct * tty)810*4882a593Smuzhiyun void start_tty(struct tty_struct *tty)
811*4882a593Smuzhiyun {
812*4882a593Smuzhiyun unsigned long flags;
813*4882a593Smuzhiyun
814*4882a593Smuzhiyun spin_lock_irqsave(&tty->flow_lock, flags);
815*4882a593Smuzhiyun __start_tty(tty);
816*4882a593Smuzhiyun spin_unlock_irqrestore(&tty->flow_lock, flags);
817*4882a593Smuzhiyun }
818*4882a593Smuzhiyun EXPORT_SYMBOL(start_tty);
819*4882a593Smuzhiyun
tty_update_time(struct timespec64 * time)820*4882a593Smuzhiyun static void tty_update_time(struct timespec64 *time)
821*4882a593Smuzhiyun {
822*4882a593Smuzhiyun time64_t sec = ktime_get_real_seconds();
823*4882a593Smuzhiyun
824*4882a593Smuzhiyun /*
825*4882a593Smuzhiyun * We only care if the two values differ in anything other than the
826*4882a593Smuzhiyun * lower three bits (i.e every 8 seconds). If so, then we can update
827*4882a593Smuzhiyun * the time of the tty device, otherwise it could be construded as a
828*4882a593Smuzhiyun * security leak to let userspace know the exact timing of the tty.
829*4882a593Smuzhiyun */
830*4882a593Smuzhiyun if ((sec ^ time->tv_sec) & ~7)
831*4882a593Smuzhiyun time->tv_sec = sec;
832*4882a593Smuzhiyun }
833*4882a593Smuzhiyun
834*4882a593Smuzhiyun /*
835*4882a593Smuzhiyun * Iterate on the ldisc ->read() function until we've gotten all
836*4882a593Smuzhiyun * the data the ldisc has for us.
837*4882a593Smuzhiyun *
838*4882a593Smuzhiyun * The "cookie" is something that the ldisc read function can fill
839*4882a593Smuzhiyun * in to let us know that there is more data to be had.
840*4882a593Smuzhiyun *
841*4882a593Smuzhiyun * We promise to continue to call the ldisc until it stops returning
842*4882a593Smuzhiyun * data or clears the cookie. The cookie may be something that the
843*4882a593Smuzhiyun * ldisc maintains state for and needs to free.
844*4882a593Smuzhiyun */
iterate_tty_read(struct tty_ldisc * ld,struct tty_struct * tty,struct file * file,struct iov_iter * to)845*4882a593Smuzhiyun static int iterate_tty_read(struct tty_ldisc *ld, struct tty_struct *tty,
846*4882a593Smuzhiyun struct file *file, struct iov_iter *to)
847*4882a593Smuzhiyun {
848*4882a593Smuzhiyun int retval = 0;
849*4882a593Smuzhiyun void *cookie = NULL;
850*4882a593Smuzhiyun unsigned long offset = 0;
851*4882a593Smuzhiyun char kernel_buf[64];
852*4882a593Smuzhiyun size_t count = iov_iter_count(to);
853*4882a593Smuzhiyun
854*4882a593Smuzhiyun do {
855*4882a593Smuzhiyun int size, copied;
856*4882a593Smuzhiyun
857*4882a593Smuzhiyun size = count > sizeof(kernel_buf) ? sizeof(kernel_buf) : count;
858*4882a593Smuzhiyun size = ld->ops->read(tty, file, kernel_buf, size, &cookie, offset);
859*4882a593Smuzhiyun if (!size)
860*4882a593Smuzhiyun break;
861*4882a593Smuzhiyun
862*4882a593Smuzhiyun if (size < 0) {
863*4882a593Smuzhiyun /* Did we have an earlier error (ie -EFAULT)? */
864*4882a593Smuzhiyun if (retval)
865*4882a593Smuzhiyun break;
866*4882a593Smuzhiyun retval = size;
867*4882a593Smuzhiyun
868*4882a593Smuzhiyun /*
869*4882a593Smuzhiyun * -EOVERFLOW means we didn't have enough space
870*4882a593Smuzhiyun * for a whole packet, and we shouldn't return
871*4882a593Smuzhiyun * a partial result.
872*4882a593Smuzhiyun */
873*4882a593Smuzhiyun if (retval == -EOVERFLOW)
874*4882a593Smuzhiyun offset = 0;
875*4882a593Smuzhiyun break;
876*4882a593Smuzhiyun }
877*4882a593Smuzhiyun
878*4882a593Smuzhiyun copied = copy_to_iter(kernel_buf, size, to);
879*4882a593Smuzhiyun offset += copied;
880*4882a593Smuzhiyun count -= copied;
881*4882a593Smuzhiyun
882*4882a593Smuzhiyun /*
883*4882a593Smuzhiyun * If the user copy failed, we still need to do another ->read()
884*4882a593Smuzhiyun * call if we had a cookie to let the ldisc clear up.
885*4882a593Smuzhiyun *
886*4882a593Smuzhiyun * But make sure size is zeroed.
887*4882a593Smuzhiyun */
888*4882a593Smuzhiyun if (unlikely(copied != size)) {
889*4882a593Smuzhiyun count = 0;
890*4882a593Smuzhiyun retval = -EFAULT;
891*4882a593Smuzhiyun }
892*4882a593Smuzhiyun } while (cookie);
893*4882a593Smuzhiyun
894*4882a593Smuzhiyun /* We always clear tty buffer in case they contained passwords */
895*4882a593Smuzhiyun memzero_explicit(kernel_buf, sizeof(kernel_buf));
896*4882a593Smuzhiyun return offset ? offset : retval;
897*4882a593Smuzhiyun }
898*4882a593Smuzhiyun
899*4882a593Smuzhiyun
900*4882a593Smuzhiyun /**
901*4882a593Smuzhiyun * tty_read - read method for tty device files
902*4882a593Smuzhiyun * @file: pointer to tty file
903*4882a593Smuzhiyun * @buf: user buffer
904*4882a593Smuzhiyun * @count: size of user buffer
905*4882a593Smuzhiyun * @ppos: unused
906*4882a593Smuzhiyun *
907*4882a593Smuzhiyun * Perform the read system call function on this terminal device. Checks
908*4882a593Smuzhiyun * for hung up devices before calling the line discipline method.
909*4882a593Smuzhiyun *
910*4882a593Smuzhiyun * Locking:
911*4882a593Smuzhiyun * Locks the line discipline internally while needed. Multiple
912*4882a593Smuzhiyun * read calls may be outstanding in parallel.
913*4882a593Smuzhiyun */
914*4882a593Smuzhiyun
tty_read(struct kiocb * iocb,struct iov_iter * to)915*4882a593Smuzhiyun static ssize_t tty_read(struct kiocb *iocb, struct iov_iter *to)
916*4882a593Smuzhiyun {
917*4882a593Smuzhiyun int i;
918*4882a593Smuzhiyun struct file *file = iocb->ki_filp;
919*4882a593Smuzhiyun struct inode *inode = file_inode(file);
920*4882a593Smuzhiyun struct tty_struct *tty = file_tty(file);
921*4882a593Smuzhiyun struct tty_ldisc *ld;
922*4882a593Smuzhiyun
923*4882a593Smuzhiyun if (tty_paranoia_check(tty, inode, "tty_read"))
924*4882a593Smuzhiyun return -EIO;
925*4882a593Smuzhiyun if (!tty || tty_io_error(tty))
926*4882a593Smuzhiyun return -EIO;
927*4882a593Smuzhiyun
928*4882a593Smuzhiyun /* We want to wait for the line discipline to sort out in this
929*4882a593Smuzhiyun situation */
930*4882a593Smuzhiyun ld = tty_ldisc_ref_wait(tty);
931*4882a593Smuzhiyun if (!ld)
932*4882a593Smuzhiyun return hung_up_tty_read(iocb, to);
933*4882a593Smuzhiyun i = -EIO;
934*4882a593Smuzhiyun if (ld->ops->read)
935*4882a593Smuzhiyun i = iterate_tty_read(ld, tty, file, to);
936*4882a593Smuzhiyun tty_ldisc_deref(ld);
937*4882a593Smuzhiyun
938*4882a593Smuzhiyun if (i > 0)
939*4882a593Smuzhiyun tty_update_time(&inode->i_atime);
940*4882a593Smuzhiyun
941*4882a593Smuzhiyun return i;
942*4882a593Smuzhiyun }
943*4882a593Smuzhiyun
tty_write_unlock(struct tty_struct * tty)944*4882a593Smuzhiyun static void tty_write_unlock(struct tty_struct *tty)
945*4882a593Smuzhiyun {
946*4882a593Smuzhiyun mutex_unlock(&tty->atomic_write_lock);
947*4882a593Smuzhiyun wake_up_interruptible_poll(&tty->write_wait, EPOLLOUT);
948*4882a593Smuzhiyun }
949*4882a593Smuzhiyun
tty_write_lock(struct tty_struct * tty,int ndelay)950*4882a593Smuzhiyun static int tty_write_lock(struct tty_struct *tty, int ndelay)
951*4882a593Smuzhiyun {
952*4882a593Smuzhiyun if (!mutex_trylock(&tty->atomic_write_lock)) {
953*4882a593Smuzhiyun if (ndelay)
954*4882a593Smuzhiyun return -EAGAIN;
955*4882a593Smuzhiyun if (mutex_lock_interruptible(&tty->atomic_write_lock))
956*4882a593Smuzhiyun return -ERESTARTSYS;
957*4882a593Smuzhiyun }
958*4882a593Smuzhiyun return 0;
959*4882a593Smuzhiyun }
960*4882a593Smuzhiyun
961*4882a593Smuzhiyun /*
962*4882a593Smuzhiyun * Split writes up in sane blocksizes to avoid
963*4882a593Smuzhiyun * denial-of-service type attacks
964*4882a593Smuzhiyun */
do_tty_write(ssize_t (* write)(struct tty_struct *,struct file *,const unsigned char *,size_t),struct tty_struct * tty,struct file * file,struct iov_iter * from)965*4882a593Smuzhiyun static inline ssize_t do_tty_write(
966*4882a593Smuzhiyun ssize_t (*write)(struct tty_struct *, struct file *, const unsigned char *, size_t),
967*4882a593Smuzhiyun struct tty_struct *tty,
968*4882a593Smuzhiyun struct file *file,
969*4882a593Smuzhiyun struct iov_iter *from)
970*4882a593Smuzhiyun {
971*4882a593Smuzhiyun size_t count = iov_iter_count(from);
972*4882a593Smuzhiyun ssize_t ret, written = 0;
973*4882a593Smuzhiyun unsigned int chunk;
974*4882a593Smuzhiyun
975*4882a593Smuzhiyun ret = tty_write_lock(tty, file->f_flags & O_NDELAY);
976*4882a593Smuzhiyun if (ret < 0)
977*4882a593Smuzhiyun return ret;
978*4882a593Smuzhiyun
979*4882a593Smuzhiyun /*
980*4882a593Smuzhiyun * We chunk up writes into a temporary buffer. This
981*4882a593Smuzhiyun * simplifies low-level drivers immensely, since they
982*4882a593Smuzhiyun * don't have locking issues and user mode accesses.
983*4882a593Smuzhiyun *
984*4882a593Smuzhiyun * But if TTY_NO_WRITE_SPLIT is set, we should use a
985*4882a593Smuzhiyun * big chunk-size..
986*4882a593Smuzhiyun *
987*4882a593Smuzhiyun * The default chunk-size is 2kB, because the NTTY
988*4882a593Smuzhiyun * layer has problems with bigger chunks. It will
989*4882a593Smuzhiyun * claim to be able to handle more characters than
990*4882a593Smuzhiyun * it actually does.
991*4882a593Smuzhiyun *
992*4882a593Smuzhiyun * FIXME: This can probably go away now except that 64K chunks
993*4882a593Smuzhiyun * are too likely to fail unless switched to vmalloc...
994*4882a593Smuzhiyun */
995*4882a593Smuzhiyun chunk = 2048;
996*4882a593Smuzhiyun if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags))
997*4882a593Smuzhiyun chunk = 65536;
998*4882a593Smuzhiyun if (count < chunk)
999*4882a593Smuzhiyun chunk = count;
1000*4882a593Smuzhiyun
1001*4882a593Smuzhiyun /* write_buf/write_cnt is protected by the atomic_write_lock mutex */
1002*4882a593Smuzhiyun if (tty->write_cnt < chunk) {
1003*4882a593Smuzhiyun unsigned char *buf_chunk;
1004*4882a593Smuzhiyun
1005*4882a593Smuzhiyun if (chunk < 1024)
1006*4882a593Smuzhiyun chunk = 1024;
1007*4882a593Smuzhiyun
1008*4882a593Smuzhiyun buf_chunk = kmalloc(chunk, GFP_KERNEL);
1009*4882a593Smuzhiyun if (!buf_chunk) {
1010*4882a593Smuzhiyun ret = -ENOMEM;
1011*4882a593Smuzhiyun goto out;
1012*4882a593Smuzhiyun }
1013*4882a593Smuzhiyun kfree(tty->write_buf);
1014*4882a593Smuzhiyun tty->write_cnt = chunk;
1015*4882a593Smuzhiyun tty->write_buf = buf_chunk;
1016*4882a593Smuzhiyun }
1017*4882a593Smuzhiyun
1018*4882a593Smuzhiyun /* Do the write .. */
1019*4882a593Smuzhiyun for (;;) {
1020*4882a593Smuzhiyun size_t size = count;
1021*4882a593Smuzhiyun if (size > chunk)
1022*4882a593Smuzhiyun size = chunk;
1023*4882a593Smuzhiyun
1024*4882a593Smuzhiyun ret = -EFAULT;
1025*4882a593Smuzhiyun if (copy_from_iter(tty->write_buf, size, from) != size)
1026*4882a593Smuzhiyun break;
1027*4882a593Smuzhiyun
1028*4882a593Smuzhiyun ret = write(tty, file, tty->write_buf, size);
1029*4882a593Smuzhiyun if (ret <= 0)
1030*4882a593Smuzhiyun break;
1031*4882a593Smuzhiyun
1032*4882a593Smuzhiyun written += ret;
1033*4882a593Smuzhiyun if (ret > size)
1034*4882a593Smuzhiyun break;
1035*4882a593Smuzhiyun
1036*4882a593Smuzhiyun /* FIXME! Have Al check this! */
1037*4882a593Smuzhiyun if (ret != size)
1038*4882a593Smuzhiyun iov_iter_revert(from, size-ret);
1039*4882a593Smuzhiyun
1040*4882a593Smuzhiyun count -= ret;
1041*4882a593Smuzhiyun if (!count)
1042*4882a593Smuzhiyun break;
1043*4882a593Smuzhiyun ret = -ERESTARTSYS;
1044*4882a593Smuzhiyun if (signal_pending(current))
1045*4882a593Smuzhiyun break;
1046*4882a593Smuzhiyun cond_resched();
1047*4882a593Smuzhiyun }
1048*4882a593Smuzhiyun if (written) {
1049*4882a593Smuzhiyun tty_update_time(&file_inode(file)->i_mtime);
1050*4882a593Smuzhiyun ret = written;
1051*4882a593Smuzhiyun }
1052*4882a593Smuzhiyun out:
1053*4882a593Smuzhiyun tty_write_unlock(tty);
1054*4882a593Smuzhiyun return ret;
1055*4882a593Smuzhiyun }
1056*4882a593Smuzhiyun
1057*4882a593Smuzhiyun /**
1058*4882a593Smuzhiyun * tty_write_message - write a message to a certain tty, not just the console.
1059*4882a593Smuzhiyun * @tty: the destination tty_struct
1060*4882a593Smuzhiyun * @msg: the message to write
1061*4882a593Smuzhiyun *
1062*4882a593Smuzhiyun * This is used for messages that need to be redirected to a specific tty.
1063*4882a593Smuzhiyun * We don't put it into the syslog queue right now maybe in the future if
1064*4882a593Smuzhiyun * really needed.
1065*4882a593Smuzhiyun *
1066*4882a593Smuzhiyun * We must still hold the BTM and test the CLOSING flag for the moment.
1067*4882a593Smuzhiyun */
1068*4882a593Smuzhiyun
tty_write_message(struct tty_struct * tty,char * msg)1069*4882a593Smuzhiyun void tty_write_message(struct tty_struct *tty, char *msg)
1070*4882a593Smuzhiyun {
1071*4882a593Smuzhiyun if (tty) {
1072*4882a593Smuzhiyun mutex_lock(&tty->atomic_write_lock);
1073*4882a593Smuzhiyun tty_lock(tty);
1074*4882a593Smuzhiyun if (tty->ops->write && tty->count > 0)
1075*4882a593Smuzhiyun tty->ops->write(tty, msg, strlen(msg));
1076*4882a593Smuzhiyun tty_unlock(tty);
1077*4882a593Smuzhiyun tty_write_unlock(tty);
1078*4882a593Smuzhiyun }
1079*4882a593Smuzhiyun return;
1080*4882a593Smuzhiyun }
1081*4882a593Smuzhiyun
1082*4882a593Smuzhiyun
1083*4882a593Smuzhiyun /**
1084*4882a593Smuzhiyun * tty_write - write method for tty device file
1085*4882a593Smuzhiyun * @file: tty file pointer
1086*4882a593Smuzhiyun * @buf: user data to write
1087*4882a593Smuzhiyun * @count: bytes to write
1088*4882a593Smuzhiyun * @ppos: unused
1089*4882a593Smuzhiyun *
1090*4882a593Smuzhiyun * Write data to a tty device via the line discipline.
1091*4882a593Smuzhiyun *
1092*4882a593Smuzhiyun * Locking:
1093*4882a593Smuzhiyun * Locks the line discipline as required
1094*4882a593Smuzhiyun * Writes to the tty driver are serialized by the atomic_write_lock
1095*4882a593Smuzhiyun * and are then processed in chunks to the device. The line discipline
1096*4882a593Smuzhiyun * write method will not be invoked in parallel for each device.
1097*4882a593Smuzhiyun */
1098*4882a593Smuzhiyun
file_tty_write(struct file * file,struct kiocb * iocb,struct iov_iter * from)1099*4882a593Smuzhiyun static ssize_t file_tty_write(struct file *file, struct kiocb *iocb, struct iov_iter *from)
1100*4882a593Smuzhiyun {
1101*4882a593Smuzhiyun struct tty_struct *tty = file_tty(file);
1102*4882a593Smuzhiyun struct tty_ldisc *ld;
1103*4882a593Smuzhiyun ssize_t ret;
1104*4882a593Smuzhiyun
1105*4882a593Smuzhiyun if (tty_paranoia_check(tty, file_inode(file), "tty_write"))
1106*4882a593Smuzhiyun return -EIO;
1107*4882a593Smuzhiyun if (!tty || !tty->ops->write || tty_io_error(tty))
1108*4882a593Smuzhiyun return -EIO;
1109*4882a593Smuzhiyun /* Short term debug to catch buggy drivers */
1110*4882a593Smuzhiyun if (tty->ops->write_room == NULL)
1111*4882a593Smuzhiyun tty_err(tty, "missing write_room method\n");
1112*4882a593Smuzhiyun ld = tty_ldisc_ref_wait(tty);
1113*4882a593Smuzhiyun if (!ld)
1114*4882a593Smuzhiyun return hung_up_tty_write(iocb, from);
1115*4882a593Smuzhiyun if (!ld->ops->write)
1116*4882a593Smuzhiyun ret = -EIO;
1117*4882a593Smuzhiyun else
1118*4882a593Smuzhiyun ret = do_tty_write(ld->ops->write, tty, file, from);
1119*4882a593Smuzhiyun tty_ldisc_deref(ld);
1120*4882a593Smuzhiyun return ret;
1121*4882a593Smuzhiyun }
1122*4882a593Smuzhiyun
tty_write(struct kiocb * iocb,struct iov_iter * from)1123*4882a593Smuzhiyun static ssize_t tty_write(struct kiocb *iocb, struct iov_iter *from)
1124*4882a593Smuzhiyun {
1125*4882a593Smuzhiyun return file_tty_write(iocb->ki_filp, iocb, from);
1126*4882a593Smuzhiyun }
1127*4882a593Smuzhiyun
redirected_tty_write(struct kiocb * iocb,struct iov_iter * iter)1128*4882a593Smuzhiyun ssize_t redirected_tty_write(struct kiocb *iocb, struct iov_iter *iter)
1129*4882a593Smuzhiyun {
1130*4882a593Smuzhiyun struct file *p = NULL;
1131*4882a593Smuzhiyun
1132*4882a593Smuzhiyun spin_lock(&redirect_lock);
1133*4882a593Smuzhiyun if (redirect)
1134*4882a593Smuzhiyun p = get_file(redirect);
1135*4882a593Smuzhiyun spin_unlock(&redirect_lock);
1136*4882a593Smuzhiyun
1137*4882a593Smuzhiyun /*
1138*4882a593Smuzhiyun * We know the redirected tty is just another tty, we can can
1139*4882a593Smuzhiyun * call file_tty_write() directly with that file pointer.
1140*4882a593Smuzhiyun */
1141*4882a593Smuzhiyun if (p) {
1142*4882a593Smuzhiyun ssize_t res;
1143*4882a593Smuzhiyun res = file_tty_write(p, iocb, iter);
1144*4882a593Smuzhiyun fput(p);
1145*4882a593Smuzhiyun return res;
1146*4882a593Smuzhiyun }
1147*4882a593Smuzhiyun return tty_write(iocb, iter);
1148*4882a593Smuzhiyun }
1149*4882a593Smuzhiyun
1150*4882a593Smuzhiyun /**
1151*4882a593Smuzhiyun * tty_send_xchar - send priority character
1152*4882a593Smuzhiyun *
1153*4882a593Smuzhiyun * Send a high priority character to the tty even if stopped
1154*4882a593Smuzhiyun *
1155*4882a593Smuzhiyun * Locking: none for xchar method, write ordering for write method.
1156*4882a593Smuzhiyun */
1157*4882a593Smuzhiyun
tty_send_xchar(struct tty_struct * tty,char ch)1158*4882a593Smuzhiyun int tty_send_xchar(struct tty_struct *tty, char ch)
1159*4882a593Smuzhiyun {
1160*4882a593Smuzhiyun int was_stopped = tty->stopped;
1161*4882a593Smuzhiyun
1162*4882a593Smuzhiyun if (tty->ops->send_xchar) {
1163*4882a593Smuzhiyun down_read(&tty->termios_rwsem);
1164*4882a593Smuzhiyun tty->ops->send_xchar(tty, ch);
1165*4882a593Smuzhiyun up_read(&tty->termios_rwsem);
1166*4882a593Smuzhiyun return 0;
1167*4882a593Smuzhiyun }
1168*4882a593Smuzhiyun
1169*4882a593Smuzhiyun if (tty_write_lock(tty, 0) < 0)
1170*4882a593Smuzhiyun return -ERESTARTSYS;
1171*4882a593Smuzhiyun
1172*4882a593Smuzhiyun down_read(&tty->termios_rwsem);
1173*4882a593Smuzhiyun if (was_stopped)
1174*4882a593Smuzhiyun start_tty(tty);
1175*4882a593Smuzhiyun tty->ops->write(tty, &ch, 1);
1176*4882a593Smuzhiyun if (was_stopped)
1177*4882a593Smuzhiyun stop_tty(tty);
1178*4882a593Smuzhiyun up_read(&tty->termios_rwsem);
1179*4882a593Smuzhiyun tty_write_unlock(tty);
1180*4882a593Smuzhiyun return 0;
1181*4882a593Smuzhiyun }
1182*4882a593Smuzhiyun
1183*4882a593Smuzhiyun static char ptychar[] = "pqrstuvwxyzabcde";
1184*4882a593Smuzhiyun
1185*4882a593Smuzhiyun /**
1186*4882a593Smuzhiyun * pty_line_name - generate name for a pty
1187*4882a593Smuzhiyun * @driver: the tty driver in use
1188*4882a593Smuzhiyun * @index: the minor number
1189*4882a593Smuzhiyun * @p: output buffer of at least 6 bytes
1190*4882a593Smuzhiyun *
1191*4882a593Smuzhiyun * Generate a name from a driver reference and write it to the output
1192*4882a593Smuzhiyun * buffer.
1193*4882a593Smuzhiyun *
1194*4882a593Smuzhiyun * Locking: None
1195*4882a593Smuzhiyun */
pty_line_name(struct tty_driver * driver,int index,char * p)1196*4882a593Smuzhiyun static void pty_line_name(struct tty_driver *driver, int index, char *p)
1197*4882a593Smuzhiyun {
1198*4882a593Smuzhiyun int i = index + driver->name_base;
1199*4882a593Smuzhiyun /* ->name is initialized to "ttyp", but "tty" is expected */
1200*4882a593Smuzhiyun sprintf(p, "%s%c%x",
1201*4882a593Smuzhiyun driver->subtype == PTY_TYPE_SLAVE ? "tty" : driver->name,
1202*4882a593Smuzhiyun ptychar[i >> 4 & 0xf], i & 0xf);
1203*4882a593Smuzhiyun }
1204*4882a593Smuzhiyun
1205*4882a593Smuzhiyun /**
1206*4882a593Smuzhiyun * tty_line_name - generate name for a tty
1207*4882a593Smuzhiyun * @driver: the tty driver in use
1208*4882a593Smuzhiyun * @index: the minor number
1209*4882a593Smuzhiyun * @p: output buffer of at least 7 bytes
1210*4882a593Smuzhiyun *
1211*4882a593Smuzhiyun * Generate a name from a driver reference and write it to the output
1212*4882a593Smuzhiyun * buffer.
1213*4882a593Smuzhiyun *
1214*4882a593Smuzhiyun * Locking: None
1215*4882a593Smuzhiyun */
tty_line_name(struct tty_driver * driver,int index,char * p)1216*4882a593Smuzhiyun static ssize_t tty_line_name(struct tty_driver *driver, int index, char *p)
1217*4882a593Smuzhiyun {
1218*4882a593Smuzhiyun if (driver->flags & TTY_DRIVER_UNNUMBERED_NODE)
1219*4882a593Smuzhiyun return sprintf(p, "%s", driver->name);
1220*4882a593Smuzhiyun else
1221*4882a593Smuzhiyun return sprintf(p, "%s%d", driver->name,
1222*4882a593Smuzhiyun index + driver->name_base);
1223*4882a593Smuzhiyun }
1224*4882a593Smuzhiyun
1225*4882a593Smuzhiyun /**
1226*4882a593Smuzhiyun * tty_driver_lookup_tty() - find an existing tty, if any
1227*4882a593Smuzhiyun * @driver: the driver for the tty
1228*4882a593Smuzhiyun * @idx: the minor number
1229*4882a593Smuzhiyun *
1230*4882a593Smuzhiyun * Return the tty, if found. If not found, return NULL or ERR_PTR() if the
1231*4882a593Smuzhiyun * driver lookup() method returns an error.
1232*4882a593Smuzhiyun *
1233*4882a593Smuzhiyun * Locking: tty_mutex must be held. If the tty is found, bump the tty kref.
1234*4882a593Smuzhiyun */
tty_driver_lookup_tty(struct tty_driver * driver,struct file * file,int idx)1235*4882a593Smuzhiyun static struct tty_struct *tty_driver_lookup_tty(struct tty_driver *driver,
1236*4882a593Smuzhiyun struct file *file, int idx)
1237*4882a593Smuzhiyun {
1238*4882a593Smuzhiyun struct tty_struct *tty;
1239*4882a593Smuzhiyun
1240*4882a593Smuzhiyun if (driver->ops->lookup)
1241*4882a593Smuzhiyun if (!file)
1242*4882a593Smuzhiyun tty = ERR_PTR(-EIO);
1243*4882a593Smuzhiyun else
1244*4882a593Smuzhiyun tty = driver->ops->lookup(driver, file, idx);
1245*4882a593Smuzhiyun else
1246*4882a593Smuzhiyun tty = driver->ttys[idx];
1247*4882a593Smuzhiyun
1248*4882a593Smuzhiyun if (!IS_ERR(tty))
1249*4882a593Smuzhiyun tty_kref_get(tty);
1250*4882a593Smuzhiyun return tty;
1251*4882a593Smuzhiyun }
1252*4882a593Smuzhiyun
1253*4882a593Smuzhiyun /**
1254*4882a593Smuzhiyun * tty_init_termios - helper for termios setup
1255*4882a593Smuzhiyun * @tty: the tty to set up
1256*4882a593Smuzhiyun *
1257*4882a593Smuzhiyun * Initialise the termios structure for this tty. This runs under
1258*4882a593Smuzhiyun * the tty_mutex currently so we can be relaxed about ordering.
1259*4882a593Smuzhiyun */
1260*4882a593Smuzhiyun
tty_init_termios(struct tty_struct * tty)1261*4882a593Smuzhiyun void tty_init_termios(struct tty_struct *tty)
1262*4882a593Smuzhiyun {
1263*4882a593Smuzhiyun struct ktermios *tp;
1264*4882a593Smuzhiyun int idx = tty->index;
1265*4882a593Smuzhiyun
1266*4882a593Smuzhiyun if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
1267*4882a593Smuzhiyun tty->termios = tty->driver->init_termios;
1268*4882a593Smuzhiyun else {
1269*4882a593Smuzhiyun /* Check for lazy saved data */
1270*4882a593Smuzhiyun tp = tty->driver->termios[idx];
1271*4882a593Smuzhiyun if (tp != NULL) {
1272*4882a593Smuzhiyun tty->termios = *tp;
1273*4882a593Smuzhiyun tty->termios.c_line = tty->driver->init_termios.c_line;
1274*4882a593Smuzhiyun } else
1275*4882a593Smuzhiyun tty->termios = tty->driver->init_termios;
1276*4882a593Smuzhiyun }
1277*4882a593Smuzhiyun /* Compatibility until drivers always set this */
1278*4882a593Smuzhiyun tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios);
1279*4882a593Smuzhiyun tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios);
1280*4882a593Smuzhiyun }
1281*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(tty_init_termios);
1282*4882a593Smuzhiyun
tty_standard_install(struct tty_driver * driver,struct tty_struct * tty)1283*4882a593Smuzhiyun int tty_standard_install(struct tty_driver *driver, struct tty_struct *tty)
1284*4882a593Smuzhiyun {
1285*4882a593Smuzhiyun tty_init_termios(tty);
1286*4882a593Smuzhiyun tty_driver_kref_get(driver);
1287*4882a593Smuzhiyun tty->count++;
1288*4882a593Smuzhiyun driver->ttys[tty->index] = tty;
1289*4882a593Smuzhiyun return 0;
1290*4882a593Smuzhiyun }
1291*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(tty_standard_install);
1292*4882a593Smuzhiyun
1293*4882a593Smuzhiyun /**
1294*4882a593Smuzhiyun * tty_driver_install_tty() - install a tty entry in the driver
1295*4882a593Smuzhiyun * @driver: the driver for the tty
1296*4882a593Smuzhiyun * @tty: the tty
1297*4882a593Smuzhiyun *
1298*4882a593Smuzhiyun * Install a tty object into the driver tables. The tty->index field
1299*4882a593Smuzhiyun * will be set by the time this is called. This method is responsible
1300*4882a593Smuzhiyun * for ensuring any need additional structures are allocated and
1301*4882a593Smuzhiyun * configured.
1302*4882a593Smuzhiyun *
1303*4882a593Smuzhiyun * Locking: tty_mutex for now
1304*4882a593Smuzhiyun */
tty_driver_install_tty(struct tty_driver * driver,struct tty_struct * tty)1305*4882a593Smuzhiyun static int tty_driver_install_tty(struct tty_driver *driver,
1306*4882a593Smuzhiyun struct tty_struct *tty)
1307*4882a593Smuzhiyun {
1308*4882a593Smuzhiyun return driver->ops->install ? driver->ops->install(driver, tty) :
1309*4882a593Smuzhiyun tty_standard_install(driver, tty);
1310*4882a593Smuzhiyun }
1311*4882a593Smuzhiyun
1312*4882a593Smuzhiyun /**
1313*4882a593Smuzhiyun * tty_driver_remove_tty() - remove a tty from the driver tables
1314*4882a593Smuzhiyun * @driver: the driver for the tty
1315*4882a593Smuzhiyun * @tty: tty to remove
1316*4882a593Smuzhiyun *
1317*4882a593Smuzhiyun * Remvoe a tty object from the driver tables. The tty->index field
1318*4882a593Smuzhiyun * will be set by the time this is called.
1319*4882a593Smuzhiyun *
1320*4882a593Smuzhiyun * Locking: tty_mutex for now
1321*4882a593Smuzhiyun */
tty_driver_remove_tty(struct tty_driver * driver,struct tty_struct * tty)1322*4882a593Smuzhiyun static void tty_driver_remove_tty(struct tty_driver *driver, struct tty_struct *tty)
1323*4882a593Smuzhiyun {
1324*4882a593Smuzhiyun if (driver->ops->remove)
1325*4882a593Smuzhiyun driver->ops->remove(driver, tty);
1326*4882a593Smuzhiyun else
1327*4882a593Smuzhiyun driver->ttys[tty->index] = NULL;
1328*4882a593Smuzhiyun }
1329*4882a593Smuzhiyun
1330*4882a593Smuzhiyun /**
1331*4882a593Smuzhiyun * tty_reopen() - fast re-open of an open tty
1332*4882a593Smuzhiyun * @tty: the tty to open
1333*4882a593Smuzhiyun *
1334*4882a593Smuzhiyun * Return 0 on success, -errno on error.
1335*4882a593Smuzhiyun * Re-opens on master ptys are not allowed and return -EIO.
1336*4882a593Smuzhiyun *
1337*4882a593Smuzhiyun * Locking: Caller must hold tty_lock
1338*4882a593Smuzhiyun */
tty_reopen(struct tty_struct * tty)1339*4882a593Smuzhiyun static int tty_reopen(struct tty_struct *tty)
1340*4882a593Smuzhiyun {
1341*4882a593Smuzhiyun struct tty_driver *driver = tty->driver;
1342*4882a593Smuzhiyun struct tty_ldisc *ld;
1343*4882a593Smuzhiyun int retval = 0;
1344*4882a593Smuzhiyun
1345*4882a593Smuzhiyun if (driver->type == TTY_DRIVER_TYPE_PTY &&
1346*4882a593Smuzhiyun driver->subtype == PTY_TYPE_MASTER)
1347*4882a593Smuzhiyun return -EIO;
1348*4882a593Smuzhiyun
1349*4882a593Smuzhiyun if (!tty->count)
1350*4882a593Smuzhiyun return -EAGAIN;
1351*4882a593Smuzhiyun
1352*4882a593Smuzhiyun if (test_bit(TTY_EXCLUSIVE, &tty->flags) && !capable(CAP_SYS_ADMIN))
1353*4882a593Smuzhiyun return -EBUSY;
1354*4882a593Smuzhiyun
1355*4882a593Smuzhiyun ld = tty_ldisc_ref_wait(tty);
1356*4882a593Smuzhiyun if (ld) {
1357*4882a593Smuzhiyun tty_ldisc_deref(ld);
1358*4882a593Smuzhiyun } else {
1359*4882a593Smuzhiyun retval = tty_ldisc_lock(tty, 5 * HZ);
1360*4882a593Smuzhiyun if (retval)
1361*4882a593Smuzhiyun return retval;
1362*4882a593Smuzhiyun
1363*4882a593Smuzhiyun if (!tty->ldisc)
1364*4882a593Smuzhiyun retval = tty_ldisc_reinit(tty, tty->termios.c_line);
1365*4882a593Smuzhiyun tty_ldisc_unlock(tty);
1366*4882a593Smuzhiyun }
1367*4882a593Smuzhiyun
1368*4882a593Smuzhiyun if (retval == 0)
1369*4882a593Smuzhiyun tty->count++;
1370*4882a593Smuzhiyun
1371*4882a593Smuzhiyun return retval;
1372*4882a593Smuzhiyun }
1373*4882a593Smuzhiyun
1374*4882a593Smuzhiyun /**
1375*4882a593Smuzhiyun * tty_init_dev - initialise a tty device
1376*4882a593Smuzhiyun * @driver: tty driver we are opening a device on
1377*4882a593Smuzhiyun * @idx: device index
1378*4882a593Smuzhiyun *
1379*4882a593Smuzhiyun * Prepare a tty device. This may not be a "new" clean device but
1380*4882a593Smuzhiyun * could also be an active device. The pty drivers require special
1381*4882a593Smuzhiyun * handling because of this.
1382*4882a593Smuzhiyun *
1383*4882a593Smuzhiyun * Locking:
1384*4882a593Smuzhiyun * The function is called under the tty_mutex, which
1385*4882a593Smuzhiyun * protects us from the tty struct or driver itself going away.
1386*4882a593Smuzhiyun *
1387*4882a593Smuzhiyun * On exit the tty device has the line discipline attached and
1388*4882a593Smuzhiyun * a reference count of 1. If a pair was created for pty/tty use
1389*4882a593Smuzhiyun * and the other was a pty master then it too has a reference count of 1.
1390*4882a593Smuzhiyun *
1391*4882a593Smuzhiyun * WSH 06/09/97: Rewritten to remove races and properly clean up after a
1392*4882a593Smuzhiyun * failed open. The new code protects the open with a mutex, so it's
1393*4882a593Smuzhiyun * really quite straightforward. The mutex locking can probably be
1394*4882a593Smuzhiyun * relaxed for the (most common) case of reopening a tty.
1395*4882a593Smuzhiyun *
1396*4882a593Smuzhiyun * Return: returned tty structure
1397*4882a593Smuzhiyun */
1398*4882a593Smuzhiyun
tty_init_dev(struct tty_driver * driver,int idx)1399*4882a593Smuzhiyun struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx)
1400*4882a593Smuzhiyun {
1401*4882a593Smuzhiyun struct tty_struct *tty;
1402*4882a593Smuzhiyun int retval;
1403*4882a593Smuzhiyun
1404*4882a593Smuzhiyun /*
1405*4882a593Smuzhiyun * First time open is complex, especially for PTY devices.
1406*4882a593Smuzhiyun * This code guarantees that either everything succeeds and the
1407*4882a593Smuzhiyun * TTY is ready for operation, or else the table slots are vacated
1408*4882a593Smuzhiyun * and the allocated memory released. (Except that the termios
1409*4882a593Smuzhiyun * may be retained.)
1410*4882a593Smuzhiyun */
1411*4882a593Smuzhiyun
1412*4882a593Smuzhiyun if (!try_module_get(driver->owner))
1413*4882a593Smuzhiyun return ERR_PTR(-ENODEV);
1414*4882a593Smuzhiyun
1415*4882a593Smuzhiyun tty = alloc_tty_struct(driver, idx);
1416*4882a593Smuzhiyun if (!tty) {
1417*4882a593Smuzhiyun retval = -ENOMEM;
1418*4882a593Smuzhiyun goto err_module_put;
1419*4882a593Smuzhiyun }
1420*4882a593Smuzhiyun
1421*4882a593Smuzhiyun tty_lock(tty);
1422*4882a593Smuzhiyun retval = tty_driver_install_tty(driver, tty);
1423*4882a593Smuzhiyun if (retval < 0)
1424*4882a593Smuzhiyun goto err_free_tty;
1425*4882a593Smuzhiyun
1426*4882a593Smuzhiyun if (!tty->port)
1427*4882a593Smuzhiyun tty->port = driver->ports[idx];
1428*4882a593Smuzhiyun
1429*4882a593Smuzhiyun if (WARN_RATELIMIT(!tty->port,
1430*4882a593Smuzhiyun "%s: %s driver does not set tty->port. This would crash the kernel. Fix the driver!\n",
1431*4882a593Smuzhiyun __func__, tty->driver->name)) {
1432*4882a593Smuzhiyun retval = -EINVAL;
1433*4882a593Smuzhiyun goto err_release_lock;
1434*4882a593Smuzhiyun }
1435*4882a593Smuzhiyun
1436*4882a593Smuzhiyun retval = tty_ldisc_lock(tty, 5 * HZ);
1437*4882a593Smuzhiyun if (retval)
1438*4882a593Smuzhiyun goto err_release_lock;
1439*4882a593Smuzhiyun tty->port->itty = tty;
1440*4882a593Smuzhiyun
1441*4882a593Smuzhiyun /*
1442*4882a593Smuzhiyun * Structures all installed ... call the ldisc open routines.
1443*4882a593Smuzhiyun * If we fail here just call release_tty to clean up. No need
1444*4882a593Smuzhiyun * to decrement the use counts, as release_tty doesn't care.
1445*4882a593Smuzhiyun */
1446*4882a593Smuzhiyun retval = tty_ldisc_setup(tty, tty->link);
1447*4882a593Smuzhiyun if (retval)
1448*4882a593Smuzhiyun goto err_release_tty;
1449*4882a593Smuzhiyun tty_ldisc_unlock(tty);
1450*4882a593Smuzhiyun /* Return the tty locked so that it cannot vanish under the caller */
1451*4882a593Smuzhiyun return tty;
1452*4882a593Smuzhiyun
1453*4882a593Smuzhiyun err_free_tty:
1454*4882a593Smuzhiyun tty_unlock(tty);
1455*4882a593Smuzhiyun free_tty_struct(tty);
1456*4882a593Smuzhiyun err_module_put:
1457*4882a593Smuzhiyun module_put(driver->owner);
1458*4882a593Smuzhiyun return ERR_PTR(retval);
1459*4882a593Smuzhiyun
1460*4882a593Smuzhiyun /* call the tty release_tty routine to clean out this slot */
1461*4882a593Smuzhiyun err_release_tty:
1462*4882a593Smuzhiyun tty_ldisc_unlock(tty);
1463*4882a593Smuzhiyun tty_info_ratelimited(tty, "ldisc open failed (%d), clearing slot %d\n",
1464*4882a593Smuzhiyun retval, idx);
1465*4882a593Smuzhiyun err_release_lock:
1466*4882a593Smuzhiyun tty_unlock(tty);
1467*4882a593Smuzhiyun release_tty(tty, idx);
1468*4882a593Smuzhiyun return ERR_PTR(retval);
1469*4882a593Smuzhiyun }
1470*4882a593Smuzhiyun
1471*4882a593Smuzhiyun /**
1472*4882a593Smuzhiyun * tty_save_termios() - save tty termios data in driver table
1473*4882a593Smuzhiyun * @tty: tty whose termios data to save
1474*4882a593Smuzhiyun *
1475*4882a593Smuzhiyun * Locking: Caller guarantees serialisation with tty_init_termios().
1476*4882a593Smuzhiyun */
tty_save_termios(struct tty_struct * tty)1477*4882a593Smuzhiyun void tty_save_termios(struct tty_struct *tty)
1478*4882a593Smuzhiyun {
1479*4882a593Smuzhiyun struct ktermios *tp;
1480*4882a593Smuzhiyun int idx = tty->index;
1481*4882a593Smuzhiyun
1482*4882a593Smuzhiyun /* If the port is going to reset then it has no termios to save */
1483*4882a593Smuzhiyun if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
1484*4882a593Smuzhiyun return;
1485*4882a593Smuzhiyun
1486*4882a593Smuzhiyun /* Stash the termios data */
1487*4882a593Smuzhiyun tp = tty->driver->termios[idx];
1488*4882a593Smuzhiyun if (tp == NULL) {
1489*4882a593Smuzhiyun tp = kmalloc(sizeof(*tp), GFP_KERNEL);
1490*4882a593Smuzhiyun if (tp == NULL)
1491*4882a593Smuzhiyun return;
1492*4882a593Smuzhiyun tty->driver->termios[idx] = tp;
1493*4882a593Smuzhiyun }
1494*4882a593Smuzhiyun *tp = tty->termios;
1495*4882a593Smuzhiyun }
1496*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(tty_save_termios);
1497*4882a593Smuzhiyun
1498*4882a593Smuzhiyun /**
1499*4882a593Smuzhiyun * tty_flush_works - flush all works of a tty/pty pair
1500*4882a593Smuzhiyun * @tty: tty device to flush works for (or either end of a pty pair)
1501*4882a593Smuzhiyun *
1502*4882a593Smuzhiyun * Sync flush all works belonging to @tty (and the 'other' tty).
1503*4882a593Smuzhiyun */
tty_flush_works(struct tty_struct * tty)1504*4882a593Smuzhiyun static void tty_flush_works(struct tty_struct *tty)
1505*4882a593Smuzhiyun {
1506*4882a593Smuzhiyun flush_work(&tty->SAK_work);
1507*4882a593Smuzhiyun flush_work(&tty->hangup_work);
1508*4882a593Smuzhiyun if (tty->link) {
1509*4882a593Smuzhiyun flush_work(&tty->link->SAK_work);
1510*4882a593Smuzhiyun flush_work(&tty->link->hangup_work);
1511*4882a593Smuzhiyun }
1512*4882a593Smuzhiyun }
1513*4882a593Smuzhiyun
1514*4882a593Smuzhiyun /**
1515*4882a593Smuzhiyun * release_one_tty - release tty structure memory
1516*4882a593Smuzhiyun * @work: work of tty we are obliterating
1517*4882a593Smuzhiyun *
1518*4882a593Smuzhiyun * Releases memory associated with a tty structure, and clears out the
1519*4882a593Smuzhiyun * driver table slots. This function is called when a device is no longer
1520*4882a593Smuzhiyun * in use. It also gets called when setup of a device fails.
1521*4882a593Smuzhiyun *
1522*4882a593Smuzhiyun * Locking:
1523*4882a593Smuzhiyun * takes the file list lock internally when working on the list
1524*4882a593Smuzhiyun * of ttys that the driver keeps.
1525*4882a593Smuzhiyun *
1526*4882a593Smuzhiyun * This method gets called from a work queue so that the driver private
1527*4882a593Smuzhiyun * cleanup ops can sleep (needed for USB at least)
1528*4882a593Smuzhiyun */
release_one_tty(struct work_struct * work)1529*4882a593Smuzhiyun static void release_one_tty(struct work_struct *work)
1530*4882a593Smuzhiyun {
1531*4882a593Smuzhiyun struct tty_struct *tty =
1532*4882a593Smuzhiyun container_of(work, struct tty_struct, hangup_work);
1533*4882a593Smuzhiyun struct tty_driver *driver = tty->driver;
1534*4882a593Smuzhiyun struct module *owner = driver->owner;
1535*4882a593Smuzhiyun
1536*4882a593Smuzhiyun if (tty->ops->cleanup)
1537*4882a593Smuzhiyun tty->ops->cleanup(tty);
1538*4882a593Smuzhiyun
1539*4882a593Smuzhiyun tty->magic = 0;
1540*4882a593Smuzhiyun tty_driver_kref_put(driver);
1541*4882a593Smuzhiyun module_put(owner);
1542*4882a593Smuzhiyun
1543*4882a593Smuzhiyun spin_lock(&tty->files_lock);
1544*4882a593Smuzhiyun list_del_init(&tty->tty_files);
1545*4882a593Smuzhiyun spin_unlock(&tty->files_lock);
1546*4882a593Smuzhiyun
1547*4882a593Smuzhiyun put_pid(tty->pgrp);
1548*4882a593Smuzhiyun put_pid(tty->session);
1549*4882a593Smuzhiyun free_tty_struct(tty);
1550*4882a593Smuzhiyun }
1551*4882a593Smuzhiyun
queue_release_one_tty(struct kref * kref)1552*4882a593Smuzhiyun static void queue_release_one_tty(struct kref *kref)
1553*4882a593Smuzhiyun {
1554*4882a593Smuzhiyun struct tty_struct *tty = container_of(kref, struct tty_struct, kref);
1555*4882a593Smuzhiyun
1556*4882a593Smuzhiyun /* The hangup queue is now free so we can reuse it rather than
1557*4882a593Smuzhiyun waste a chunk of memory for each port */
1558*4882a593Smuzhiyun INIT_WORK(&tty->hangup_work, release_one_tty);
1559*4882a593Smuzhiyun schedule_work(&tty->hangup_work);
1560*4882a593Smuzhiyun }
1561*4882a593Smuzhiyun
1562*4882a593Smuzhiyun /**
1563*4882a593Smuzhiyun * tty_kref_put - release a tty kref
1564*4882a593Smuzhiyun * @tty: tty device
1565*4882a593Smuzhiyun *
1566*4882a593Smuzhiyun * Release a reference to a tty device and if need be let the kref
1567*4882a593Smuzhiyun * layer destruct the object for us
1568*4882a593Smuzhiyun */
1569*4882a593Smuzhiyun
tty_kref_put(struct tty_struct * tty)1570*4882a593Smuzhiyun void tty_kref_put(struct tty_struct *tty)
1571*4882a593Smuzhiyun {
1572*4882a593Smuzhiyun if (tty)
1573*4882a593Smuzhiyun kref_put(&tty->kref, queue_release_one_tty);
1574*4882a593Smuzhiyun }
1575*4882a593Smuzhiyun EXPORT_SYMBOL(tty_kref_put);
1576*4882a593Smuzhiyun
1577*4882a593Smuzhiyun /**
1578*4882a593Smuzhiyun * release_tty - release tty structure memory
1579*4882a593Smuzhiyun *
1580*4882a593Smuzhiyun * Release both @tty and a possible linked partner (think pty pair),
1581*4882a593Smuzhiyun * and decrement the refcount of the backing module.
1582*4882a593Smuzhiyun *
1583*4882a593Smuzhiyun * Locking:
1584*4882a593Smuzhiyun * tty_mutex
1585*4882a593Smuzhiyun * takes the file list lock internally when working on the list
1586*4882a593Smuzhiyun * of ttys that the driver keeps.
1587*4882a593Smuzhiyun *
1588*4882a593Smuzhiyun */
release_tty(struct tty_struct * tty,int idx)1589*4882a593Smuzhiyun static void release_tty(struct tty_struct *tty, int idx)
1590*4882a593Smuzhiyun {
1591*4882a593Smuzhiyun /* This should always be true but check for the moment */
1592*4882a593Smuzhiyun WARN_ON(tty->index != idx);
1593*4882a593Smuzhiyun WARN_ON(!mutex_is_locked(&tty_mutex));
1594*4882a593Smuzhiyun if (tty->ops->shutdown)
1595*4882a593Smuzhiyun tty->ops->shutdown(tty);
1596*4882a593Smuzhiyun tty_save_termios(tty);
1597*4882a593Smuzhiyun tty_driver_remove_tty(tty->driver, tty);
1598*4882a593Smuzhiyun if (tty->port)
1599*4882a593Smuzhiyun tty->port->itty = NULL;
1600*4882a593Smuzhiyun if (tty->link)
1601*4882a593Smuzhiyun tty->link->port->itty = NULL;
1602*4882a593Smuzhiyun if (tty->port)
1603*4882a593Smuzhiyun tty_buffer_cancel_work(tty->port);
1604*4882a593Smuzhiyun if (tty->link)
1605*4882a593Smuzhiyun tty_buffer_cancel_work(tty->link->port);
1606*4882a593Smuzhiyun
1607*4882a593Smuzhiyun tty_kref_put(tty->link);
1608*4882a593Smuzhiyun tty_kref_put(tty);
1609*4882a593Smuzhiyun }
1610*4882a593Smuzhiyun
1611*4882a593Smuzhiyun /**
1612*4882a593Smuzhiyun * tty_release_checks - check a tty before real release
1613*4882a593Smuzhiyun * @tty: tty to check
1614*4882a593Smuzhiyun * @idx: index of the tty
1615*4882a593Smuzhiyun *
1616*4882a593Smuzhiyun * Performs some paranoid checking before true release of the @tty.
1617*4882a593Smuzhiyun * This is a no-op unless TTY_PARANOIA_CHECK is defined.
1618*4882a593Smuzhiyun */
tty_release_checks(struct tty_struct * tty,int idx)1619*4882a593Smuzhiyun static int tty_release_checks(struct tty_struct *tty, int idx)
1620*4882a593Smuzhiyun {
1621*4882a593Smuzhiyun #ifdef TTY_PARANOIA_CHECK
1622*4882a593Smuzhiyun if (idx < 0 || idx >= tty->driver->num) {
1623*4882a593Smuzhiyun tty_debug(tty, "bad idx %d\n", idx);
1624*4882a593Smuzhiyun return -1;
1625*4882a593Smuzhiyun }
1626*4882a593Smuzhiyun
1627*4882a593Smuzhiyun /* not much to check for devpts */
1628*4882a593Smuzhiyun if (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)
1629*4882a593Smuzhiyun return 0;
1630*4882a593Smuzhiyun
1631*4882a593Smuzhiyun if (tty != tty->driver->ttys[idx]) {
1632*4882a593Smuzhiyun tty_debug(tty, "bad driver table[%d] = %p\n",
1633*4882a593Smuzhiyun idx, tty->driver->ttys[idx]);
1634*4882a593Smuzhiyun return -1;
1635*4882a593Smuzhiyun }
1636*4882a593Smuzhiyun if (tty->driver->other) {
1637*4882a593Smuzhiyun struct tty_struct *o_tty = tty->link;
1638*4882a593Smuzhiyun
1639*4882a593Smuzhiyun if (o_tty != tty->driver->other->ttys[idx]) {
1640*4882a593Smuzhiyun tty_debug(tty, "bad other table[%d] = %p\n",
1641*4882a593Smuzhiyun idx, tty->driver->other->ttys[idx]);
1642*4882a593Smuzhiyun return -1;
1643*4882a593Smuzhiyun }
1644*4882a593Smuzhiyun if (o_tty->link != tty) {
1645*4882a593Smuzhiyun tty_debug(tty, "bad link = %p\n", o_tty->link);
1646*4882a593Smuzhiyun return -1;
1647*4882a593Smuzhiyun }
1648*4882a593Smuzhiyun }
1649*4882a593Smuzhiyun #endif
1650*4882a593Smuzhiyun return 0;
1651*4882a593Smuzhiyun }
1652*4882a593Smuzhiyun
1653*4882a593Smuzhiyun /**
1654*4882a593Smuzhiyun * tty_kclose - closes tty opened by tty_kopen
1655*4882a593Smuzhiyun * @tty: tty device
1656*4882a593Smuzhiyun *
1657*4882a593Smuzhiyun * Performs the final steps to release and free a tty device. It is the
1658*4882a593Smuzhiyun * same as tty_release_struct except that it also resets TTY_PORT_KOPENED
1659*4882a593Smuzhiyun * flag on tty->port.
1660*4882a593Smuzhiyun */
tty_kclose(struct tty_struct * tty)1661*4882a593Smuzhiyun void tty_kclose(struct tty_struct *tty)
1662*4882a593Smuzhiyun {
1663*4882a593Smuzhiyun /*
1664*4882a593Smuzhiyun * Ask the line discipline code to release its structures
1665*4882a593Smuzhiyun */
1666*4882a593Smuzhiyun tty_ldisc_release(tty);
1667*4882a593Smuzhiyun
1668*4882a593Smuzhiyun /* Wait for pending work before tty destruction commmences */
1669*4882a593Smuzhiyun tty_flush_works(tty);
1670*4882a593Smuzhiyun
1671*4882a593Smuzhiyun tty_debug_hangup(tty, "freeing structure\n");
1672*4882a593Smuzhiyun /*
1673*4882a593Smuzhiyun * The release_tty function takes care of the details of clearing
1674*4882a593Smuzhiyun * the slots and preserving the termios structure.
1675*4882a593Smuzhiyun */
1676*4882a593Smuzhiyun mutex_lock(&tty_mutex);
1677*4882a593Smuzhiyun tty_port_set_kopened(tty->port, 0);
1678*4882a593Smuzhiyun release_tty(tty, tty->index);
1679*4882a593Smuzhiyun mutex_unlock(&tty_mutex);
1680*4882a593Smuzhiyun }
1681*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(tty_kclose);
1682*4882a593Smuzhiyun
1683*4882a593Smuzhiyun /**
1684*4882a593Smuzhiyun * tty_release_struct - release a tty struct
1685*4882a593Smuzhiyun * @tty: tty device
1686*4882a593Smuzhiyun * @idx: index of the tty
1687*4882a593Smuzhiyun *
1688*4882a593Smuzhiyun * Performs the final steps to release and free a tty device. It is
1689*4882a593Smuzhiyun * roughly the reverse of tty_init_dev.
1690*4882a593Smuzhiyun */
tty_release_struct(struct tty_struct * tty,int idx)1691*4882a593Smuzhiyun void tty_release_struct(struct tty_struct *tty, int idx)
1692*4882a593Smuzhiyun {
1693*4882a593Smuzhiyun /*
1694*4882a593Smuzhiyun * Ask the line discipline code to release its structures
1695*4882a593Smuzhiyun */
1696*4882a593Smuzhiyun tty_ldisc_release(tty);
1697*4882a593Smuzhiyun
1698*4882a593Smuzhiyun /* Wait for pending work before tty destruction commmences */
1699*4882a593Smuzhiyun tty_flush_works(tty);
1700*4882a593Smuzhiyun
1701*4882a593Smuzhiyun tty_debug_hangup(tty, "freeing structure\n");
1702*4882a593Smuzhiyun /*
1703*4882a593Smuzhiyun * The release_tty function takes care of the details of clearing
1704*4882a593Smuzhiyun * the slots and preserving the termios structure.
1705*4882a593Smuzhiyun */
1706*4882a593Smuzhiyun mutex_lock(&tty_mutex);
1707*4882a593Smuzhiyun release_tty(tty, idx);
1708*4882a593Smuzhiyun mutex_unlock(&tty_mutex);
1709*4882a593Smuzhiyun }
1710*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(tty_release_struct);
1711*4882a593Smuzhiyun
1712*4882a593Smuzhiyun /**
1713*4882a593Smuzhiyun * tty_release - vfs callback for close
1714*4882a593Smuzhiyun * @inode: inode of tty
1715*4882a593Smuzhiyun * @filp: file pointer for handle to tty
1716*4882a593Smuzhiyun *
1717*4882a593Smuzhiyun * Called the last time each file handle is closed that references
1718*4882a593Smuzhiyun * this tty. There may however be several such references.
1719*4882a593Smuzhiyun *
1720*4882a593Smuzhiyun * Locking:
1721*4882a593Smuzhiyun * Takes bkl. See tty_release_dev
1722*4882a593Smuzhiyun *
1723*4882a593Smuzhiyun * Even releasing the tty structures is a tricky business.. We have
1724*4882a593Smuzhiyun * to be very careful that the structures are all released at the
1725*4882a593Smuzhiyun * same time, as interrupts might otherwise get the wrong pointers.
1726*4882a593Smuzhiyun *
1727*4882a593Smuzhiyun * WSH 09/09/97: rewritten to avoid some nasty race conditions that could
1728*4882a593Smuzhiyun * lead to double frees or releasing memory still in use.
1729*4882a593Smuzhiyun */
1730*4882a593Smuzhiyun
tty_release(struct inode * inode,struct file * filp)1731*4882a593Smuzhiyun int tty_release(struct inode *inode, struct file *filp)
1732*4882a593Smuzhiyun {
1733*4882a593Smuzhiyun struct tty_struct *tty = file_tty(filp);
1734*4882a593Smuzhiyun struct tty_struct *o_tty = NULL;
1735*4882a593Smuzhiyun int do_sleep, final;
1736*4882a593Smuzhiyun int idx;
1737*4882a593Smuzhiyun long timeout = 0;
1738*4882a593Smuzhiyun int once = 1;
1739*4882a593Smuzhiyun
1740*4882a593Smuzhiyun if (tty_paranoia_check(tty, inode, __func__))
1741*4882a593Smuzhiyun return 0;
1742*4882a593Smuzhiyun
1743*4882a593Smuzhiyun tty_lock(tty);
1744*4882a593Smuzhiyun check_tty_count(tty, __func__);
1745*4882a593Smuzhiyun
1746*4882a593Smuzhiyun __tty_fasync(-1, filp, 0);
1747*4882a593Smuzhiyun
1748*4882a593Smuzhiyun idx = tty->index;
1749*4882a593Smuzhiyun if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
1750*4882a593Smuzhiyun tty->driver->subtype == PTY_TYPE_MASTER)
1751*4882a593Smuzhiyun o_tty = tty->link;
1752*4882a593Smuzhiyun
1753*4882a593Smuzhiyun if (tty_release_checks(tty, idx)) {
1754*4882a593Smuzhiyun tty_unlock(tty);
1755*4882a593Smuzhiyun return 0;
1756*4882a593Smuzhiyun }
1757*4882a593Smuzhiyun
1758*4882a593Smuzhiyun tty_debug_hangup(tty, "releasing (count=%d)\n", tty->count);
1759*4882a593Smuzhiyun
1760*4882a593Smuzhiyun if (tty->ops->close)
1761*4882a593Smuzhiyun tty->ops->close(tty, filp);
1762*4882a593Smuzhiyun
1763*4882a593Smuzhiyun /* If tty is pty master, lock the slave pty (stable lock order) */
1764*4882a593Smuzhiyun tty_lock_slave(o_tty);
1765*4882a593Smuzhiyun
1766*4882a593Smuzhiyun /*
1767*4882a593Smuzhiyun * Sanity check: if tty->count is going to zero, there shouldn't be
1768*4882a593Smuzhiyun * any waiters on tty->read_wait or tty->write_wait. We test the
1769*4882a593Smuzhiyun * wait queues and kick everyone out _before_ actually starting to
1770*4882a593Smuzhiyun * close. This ensures that we won't block while releasing the tty
1771*4882a593Smuzhiyun * structure.
1772*4882a593Smuzhiyun *
1773*4882a593Smuzhiyun * The test for the o_tty closing is necessary, since the master and
1774*4882a593Smuzhiyun * slave sides may close in any order. If the slave side closes out
1775*4882a593Smuzhiyun * first, its count will be one, since the master side holds an open.
1776*4882a593Smuzhiyun * Thus this test wouldn't be triggered at the time the slave closed,
1777*4882a593Smuzhiyun * so we do it now.
1778*4882a593Smuzhiyun */
1779*4882a593Smuzhiyun while (1) {
1780*4882a593Smuzhiyun do_sleep = 0;
1781*4882a593Smuzhiyun
1782*4882a593Smuzhiyun if (tty->count <= 1) {
1783*4882a593Smuzhiyun if (waitqueue_active(&tty->read_wait)) {
1784*4882a593Smuzhiyun wake_up_poll(&tty->read_wait, EPOLLIN);
1785*4882a593Smuzhiyun do_sleep++;
1786*4882a593Smuzhiyun }
1787*4882a593Smuzhiyun if (waitqueue_active(&tty->write_wait)) {
1788*4882a593Smuzhiyun wake_up_poll(&tty->write_wait, EPOLLOUT);
1789*4882a593Smuzhiyun do_sleep++;
1790*4882a593Smuzhiyun }
1791*4882a593Smuzhiyun }
1792*4882a593Smuzhiyun if (o_tty && o_tty->count <= 1) {
1793*4882a593Smuzhiyun if (waitqueue_active(&o_tty->read_wait)) {
1794*4882a593Smuzhiyun wake_up_poll(&o_tty->read_wait, EPOLLIN);
1795*4882a593Smuzhiyun do_sleep++;
1796*4882a593Smuzhiyun }
1797*4882a593Smuzhiyun if (waitqueue_active(&o_tty->write_wait)) {
1798*4882a593Smuzhiyun wake_up_poll(&o_tty->write_wait, EPOLLOUT);
1799*4882a593Smuzhiyun do_sleep++;
1800*4882a593Smuzhiyun }
1801*4882a593Smuzhiyun }
1802*4882a593Smuzhiyun if (!do_sleep)
1803*4882a593Smuzhiyun break;
1804*4882a593Smuzhiyun
1805*4882a593Smuzhiyun if (once) {
1806*4882a593Smuzhiyun once = 0;
1807*4882a593Smuzhiyun tty_warn(tty, "read/write wait queue active!\n");
1808*4882a593Smuzhiyun }
1809*4882a593Smuzhiyun schedule_timeout_killable(timeout);
1810*4882a593Smuzhiyun if (timeout < 120 * HZ)
1811*4882a593Smuzhiyun timeout = 2 * timeout + 1;
1812*4882a593Smuzhiyun else
1813*4882a593Smuzhiyun timeout = MAX_SCHEDULE_TIMEOUT;
1814*4882a593Smuzhiyun }
1815*4882a593Smuzhiyun
1816*4882a593Smuzhiyun if (o_tty) {
1817*4882a593Smuzhiyun if (--o_tty->count < 0) {
1818*4882a593Smuzhiyun tty_warn(tty, "bad slave count (%d)\n", o_tty->count);
1819*4882a593Smuzhiyun o_tty->count = 0;
1820*4882a593Smuzhiyun }
1821*4882a593Smuzhiyun }
1822*4882a593Smuzhiyun if (--tty->count < 0) {
1823*4882a593Smuzhiyun tty_warn(tty, "bad tty->count (%d)\n", tty->count);
1824*4882a593Smuzhiyun tty->count = 0;
1825*4882a593Smuzhiyun }
1826*4882a593Smuzhiyun
1827*4882a593Smuzhiyun /*
1828*4882a593Smuzhiyun * We've decremented tty->count, so we need to remove this file
1829*4882a593Smuzhiyun * descriptor off the tty->tty_files list; this serves two
1830*4882a593Smuzhiyun * purposes:
1831*4882a593Smuzhiyun * - check_tty_count sees the correct number of file descriptors
1832*4882a593Smuzhiyun * associated with this tty.
1833*4882a593Smuzhiyun * - do_tty_hangup no longer sees this file descriptor as
1834*4882a593Smuzhiyun * something that needs to be handled for hangups.
1835*4882a593Smuzhiyun */
1836*4882a593Smuzhiyun tty_del_file(filp);
1837*4882a593Smuzhiyun
1838*4882a593Smuzhiyun /*
1839*4882a593Smuzhiyun * Perform some housekeeping before deciding whether to return.
1840*4882a593Smuzhiyun *
1841*4882a593Smuzhiyun * If _either_ side is closing, make sure there aren't any
1842*4882a593Smuzhiyun * processes that still think tty or o_tty is their controlling
1843*4882a593Smuzhiyun * tty.
1844*4882a593Smuzhiyun */
1845*4882a593Smuzhiyun if (!tty->count) {
1846*4882a593Smuzhiyun read_lock(&tasklist_lock);
1847*4882a593Smuzhiyun session_clear_tty(tty->session);
1848*4882a593Smuzhiyun if (o_tty)
1849*4882a593Smuzhiyun session_clear_tty(o_tty->session);
1850*4882a593Smuzhiyun read_unlock(&tasklist_lock);
1851*4882a593Smuzhiyun }
1852*4882a593Smuzhiyun
1853*4882a593Smuzhiyun /* check whether both sides are closing ... */
1854*4882a593Smuzhiyun final = !tty->count && !(o_tty && o_tty->count);
1855*4882a593Smuzhiyun
1856*4882a593Smuzhiyun tty_unlock_slave(o_tty);
1857*4882a593Smuzhiyun tty_unlock(tty);
1858*4882a593Smuzhiyun
1859*4882a593Smuzhiyun /* At this point, the tty->count == 0 should ensure a dead tty
1860*4882a593Smuzhiyun cannot be re-opened by a racing opener */
1861*4882a593Smuzhiyun
1862*4882a593Smuzhiyun if (!final)
1863*4882a593Smuzhiyun return 0;
1864*4882a593Smuzhiyun
1865*4882a593Smuzhiyun tty_debug_hangup(tty, "final close\n");
1866*4882a593Smuzhiyun
1867*4882a593Smuzhiyun tty_release_struct(tty, idx);
1868*4882a593Smuzhiyun return 0;
1869*4882a593Smuzhiyun }
1870*4882a593Smuzhiyun
1871*4882a593Smuzhiyun /**
1872*4882a593Smuzhiyun * tty_open_current_tty - get locked tty of current task
1873*4882a593Smuzhiyun * @device: device number
1874*4882a593Smuzhiyun * @filp: file pointer to tty
1875*4882a593Smuzhiyun * @return: locked tty of the current task iff @device is /dev/tty
1876*4882a593Smuzhiyun *
1877*4882a593Smuzhiyun * Performs a re-open of the current task's controlling tty.
1878*4882a593Smuzhiyun *
1879*4882a593Smuzhiyun * We cannot return driver and index like for the other nodes because
1880*4882a593Smuzhiyun * devpts will not work then. It expects inodes to be from devpts FS.
1881*4882a593Smuzhiyun */
tty_open_current_tty(dev_t device,struct file * filp)1882*4882a593Smuzhiyun static struct tty_struct *tty_open_current_tty(dev_t device, struct file *filp)
1883*4882a593Smuzhiyun {
1884*4882a593Smuzhiyun struct tty_struct *tty;
1885*4882a593Smuzhiyun int retval;
1886*4882a593Smuzhiyun
1887*4882a593Smuzhiyun if (device != MKDEV(TTYAUX_MAJOR, 0))
1888*4882a593Smuzhiyun return NULL;
1889*4882a593Smuzhiyun
1890*4882a593Smuzhiyun tty = get_current_tty();
1891*4882a593Smuzhiyun if (!tty)
1892*4882a593Smuzhiyun return ERR_PTR(-ENXIO);
1893*4882a593Smuzhiyun
1894*4882a593Smuzhiyun filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */
1895*4882a593Smuzhiyun /* noctty = 1; */
1896*4882a593Smuzhiyun tty_lock(tty);
1897*4882a593Smuzhiyun tty_kref_put(tty); /* safe to drop the kref now */
1898*4882a593Smuzhiyun
1899*4882a593Smuzhiyun retval = tty_reopen(tty);
1900*4882a593Smuzhiyun if (retval < 0) {
1901*4882a593Smuzhiyun tty_unlock(tty);
1902*4882a593Smuzhiyun tty = ERR_PTR(retval);
1903*4882a593Smuzhiyun }
1904*4882a593Smuzhiyun return tty;
1905*4882a593Smuzhiyun }
1906*4882a593Smuzhiyun
1907*4882a593Smuzhiyun /**
1908*4882a593Smuzhiyun * tty_lookup_driver - lookup a tty driver for a given device file
1909*4882a593Smuzhiyun * @device: device number
1910*4882a593Smuzhiyun * @filp: file pointer to tty
1911*4882a593Smuzhiyun * @index: index for the device in the @return driver
1912*4882a593Smuzhiyun * @return: driver for this inode (with increased refcount)
1913*4882a593Smuzhiyun *
1914*4882a593Smuzhiyun * If @return is not erroneous, the caller is responsible to decrement the
1915*4882a593Smuzhiyun * refcount by tty_driver_kref_put.
1916*4882a593Smuzhiyun *
1917*4882a593Smuzhiyun * Locking: tty_mutex protects get_tty_driver
1918*4882a593Smuzhiyun */
tty_lookup_driver(dev_t device,struct file * filp,int * index)1919*4882a593Smuzhiyun static struct tty_driver *tty_lookup_driver(dev_t device, struct file *filp,
1920*4882a593Smuzhiyun int *index)
1921*4882a593Smuzhiyun {
1922*4882a593Smuzhiyun struct tty_driver *driver = NULL;
1923*4882a593Smuzhiyun
1924*4882a593Smuzhiyun switch (device) {
1925*4882a593Smuzhiyun #ifdef CONFIG_VT
1926*4882a593Smuzhiyun case MKDEV(TTY_MAJOR, 0): {
1927*4882a593Smuzhiyun extern struct tty_driver *console_driver;
1928*4882a593Smuzhiyun driver = tty_driver_kref_get(console_driver);
1929*4882a593Smuzhiyun *index = fg_console;
1930*4882a593Smuzhiyun break;
1931*4882a593Smuzhiyun }
1932*4882a593Smuzhiyun #endif
1933*4882a593Smuzhiyun case MKDEV(TTYAUX_MAJOR, 1): {
1934*4882a593Smuzhiyun struct tty_driver *console_driver = console_device(index);
1935*4882a593Smuzhiyun if (console_driver) {
1936*4882a593Smuzhiyun driver = tty_driver_kref_get(console_driver);
1937*4882a593Smuzhiyun if (driver && filp) {
1938*4882a593Smuzhiyun /* Don't let /dev/console block */
1939*4882a593Smuzhiyun filp->f_flags |= O_NONBLOCK;
1940*4882a593Smuzhiyun break;
1941*4882a593Smuzhiyun }
1942*4882a593Smuzhiyun }
1943*4882a593Smuzhiyun if (driver)
1944*4882a593Smuzhiyun tty_driver_kref_put(driver);
1945*4882a593Smuzhiyun return ERR_PTR(-ENODEV);
1946*4882a593Smuzhiyun }
1947*4882a593Smuzhiyun default:
1948*4882a593Smuzhiyun driver = get_tty_driver(device, index);
1949*4882a593Smuzhiyun if (!driver)
1950*4882a593Smuzhiyun return ERR_PTR(-ENODEV);
1951*4882a593Smuzhiyun break;
1952*4882a593Smuzhiyun }
1953*4882a593Smuzhiyun return driver;
1954*4882a593Smuzhiyun }
1955*4882a593Smuzhiyun
1956*4882a593Smuzhiyun /**
1957*4882a593Smuzhiyun * tty_kopen - open a tty device for kernel
1958*4882a593Smuzhiyun * @device: dev_t of device to open
1959*4882a593Smuzhiyun *
1960*4882a593Smuzhiyun * Opens tty exclusively for kernel. Performs the driver lookup,
1961*4882a593Smuzhiyun * makes sure it's not already opened and performs the first-time
1962*4882a593Smuzhiyun * tty initialization.
1963*4882a593Smuzhiyun *
1964*4882a593Smuzhiyun * Returns the locked initialized &tty_struct
1965*4882a593Smuzhiyun *
1966*4882a593Smuzhiyun * Claims the global tty_mutex to serialize:
1967*4882a593Smuzhiyun * - concurrent first-time tty initialization
1968*4882a593Smuzhiyun * - concurrent tty driver removal w/ lookup
1969*4882a593Smuzhiyun * - concurrent tty removal from driver table
1970*4882a593Smuzhiyun */
tty_kopen(dev_t device)1971*4882a593Smuzhiyun struct tty_struct *tty_kopen(dev_t device)
1972*4882a593Smuzhiyun {
1973*4882a593Smuzhiyun struct tty_struct *tty;
1974*4882a593Smuzhiyun struct tty_driver *driver;
1975*4882a593Smuzhiyun int index = -1;
1976*4882a593Smuzhiyun
1977*4882a593Smuzhiyun mutex_lock(&tty_mutex);
1978*4882a593Smuzhiyun driver = tty_lookup_driver(device, NULL, &index);
1979*4882a593Smuzhiyun if (IS_ERR(driver)) {
1980*4882a593Smuzhiyun mutex_unlock(&tty_mutex);
1981*4882a593Smuzhiyun return ERR_CAST(driver);
1982*4882a593Smuzhiyun }
1983*4882a593Smuzhiyun
1984*4882a593Smuzhiyun /* check whether we're reopening an existing tty */
1985*4882a593Smuzhiyun tty = tty_driver_lookup_tty(driver, NULL, index);
1986*4882a593Smuzhiyun if (IS_ERR(tty))
1987*4882a593Smuzhiyun goto out;
1988*4882a593Smuzhiyun
1989*4882a593Smuzhiyun if (tty) {
1990*4882a593Smuzhiyun /* drop kref from tty_driver_lookup_tty() */
1991*4882a593Smuzhiyun tty_kref_put(tty);
1992*4882a593Smuzhiyun tty = ERR_PTR(-EBUSY);
1993*4882a593Smuzhiyun } else { /* tty_init_dev returns tty with the tty_lock held */
1994*4882a593Smuzhiyun tty = tty_init_dev(driver, index);
1995*4882a593Smuzhiyun if (IS_ERR(tty))
1996*4882a593Smuzhiyun goto out;
1997*4882a593Smuzhiyun tty_port_set_kopened(tty->port, 1);
1998*4882a593Smuzhiyun }
1999*4882a593Smuzhiyun out:
2000*4882a593Smuzhiyun mutex_unlock(&tty_mutex);
2001*4882a593Smuzhiyun tty_driver_kref_put(driver);
2002*4882a593Smuzhiyun return tty;
2003*4882a593Smuzhiyun }
2004*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(tty_kopen);
2005*4882a593Smuzhiyun
2006*4882a593Smuzhiyun /**
2007*4882a593Smuzhiyun * tty_open_by_driver - open a tty device
2008*4882a593Smuzhiyun * @device: dev_t of device to open
2009*4882a593Smuzhiyun * @filp: file pointer to tty
2010*4882a593Smuzhiyun *
2011*4882a593Smuzhiyun * Performs the driver lookup, checks for a reopen, or otherwise
2012*4882a593Smuzhiyun * performs the first-time tty initialization.
2013*4882a593Smuzhiyun *
2014*4882a593Smuzhiyun * Returns the locked initialized or re-opened &tty_struct
2015*4882a593Smuzhiyun *
2016*4882a593Smuzhiyun * Claims the global tty_mutex to serialize:
2017*4882a593Smuzhiyun * - concurrent first-time tty initialization
2018*4882a593Smuzhiyun * - concurrent tty driver removal w/ lookup
2019*4882a593Smuzhiyun * - concurrent tty removal from driver table
2020*4882a593Smuzhiyun */
tty_open_by_driver(dev_t device,struct file * filp)2021*4882a593Smuzhiyun static struct tty_struct *tty_open_by_driver(dev_t device,
2022*4882a593Smuzhiyun struct file *filp)
2023*4882a593Smuzhiyun {
2024*4882a593Smuzhiyun struct tty_struct *tty;
2025*4882a593Smuzhiyun struct tty_driver *driver = NULL;
2026*4882a593Smuzhiyun int index = -1;
2027*4882a593Smuzhiyun int retval;
2028*4882a593Smuzhiyun
2029*4882a593Smuzhiyun mutex_lock(&tty_mutex);
2030*4882a593Smuzhiyun driver = tty_lookup_driver(device, filp, &index);
2031*4882a593Smuzhiyun if (IS_ERR(driver)) {
2032*4882a593Smuzhiyun mutex_unlock(&tty_mutex);
2033*4882a593Smuzhiyun return ERR_CAST(driver);
2034*4882a593Smuzhiyun }
2035*4882a593Smuzhiyun
2036*4882a593Smuzhiyun /* check whether we're reopening an existing tty */
2037*4882a593Smuzhiyun tty = tty_driver_lookup_tty(driver, filp, index);
2038*4882a593Smuzhiyun if (IS_ERR(tty)) {
2039*4882a593Smuzhiyun mutex_unlock(&tty_mutex);
2040*4882a593Smuzhiyun goto out;
2041*4882a593Smuzhiyun }
2042*4882a593Smuzhiyun
2043*4882a593Smuzhiyun if (tty) {
2044*4882a593Smuzhiyun if (tty_port_kopened(tty->port)) {
2045*4882a593Smuzhiyun tty_kref_put(tty);
2046*4882a593Smuzhiyun mutex_unlock(&tty_mutex);
2047*4882a593Smuzhiyun tty = ERR_PTR(-EBUSY);
2048*4882a593Smuzhiyun goto out;
2049*4882a593Smuzhiyun }
2050*4882a593Smuzhiyun mutex_unlock(&tty_mutex);
2051*4882a593Smuzhiyun retval = tty_lock_interruptible(tty);
2052*4882a593Smuzhiyun tty_kref_put(tty); /* drop kref from tty_driver_lookup_tty() */
2053*4882a593Smuzhiyun if (retval) {
2054*4882a593Smuzhiyun if (retval == -EINTR)
2055*4882a593Smuzhiyun retval = -ERESTARTSYS;
2056*4882a593Smuzhiyun tty = ERR_PTR(retval);
2057*4882a593Smuzhiyun goto out;
2058*4882a593Smuzhiyun }
2059*4882a593Smuzhiyun retval = tty_reopen(tty);
2060*4882a593Smuzhiyun if (retval < 0) {
2061*4882a593Smuzhiyun tty_unlock(tty);
2062*4882a593Smuzhiyun tty = ERR_PTR(retval);
2063*4882a593Smuzhiyun }
2064*4882a593Smuzhiyun } else { /* Returns with the tty_lock held for now */
2065*4882a593Smuzhiyun tty = tty_init_dev(driver, index);
2066*4882a593Smuzhiyun mutex_unlock(&tty_mutex);
2067*4882a593Smuzhiyun }
2068*4882a593Smuzhiyun out:
2069*4882a593Smuzhiyun tty_driver_kref_put(driver);
2070*4882a593Smuzhiyun return tty;
2071*4882a593Smuzhiyun }
2072*4882a593Smuzhiyun
2073*4882a593Smuzhiyun /**
2074*4882a593Smuzhiyun * tty_open - open a tty device
2075*4882a593Smuzhiyun * @inode: inode of device file
2076*4882a593Smuzhiyun * @filp: file pointer to tty
2077*4882a593Smuzhiyun *
2078*4882a593Smuzhiyun * tty_open and tty_release keep up the tty count that contains the
2079*4882a593Smuzhiyun * number of opens done on a tty. We cannot use the inode-count, as
2080*4882a593Smuzhiyun * different inodes might point to the same tty.
2081*4882a593Smuzhiyun *
2082*4882a593Smuzhiyun * Open-counting is needed for pty masters, as well as for keeping
2083*4882a593Smuzhiyun * track of serial lines: DTR is dropped when the last close happens.
2084*4882a593Smuzhiyun * (This is not done solely through tty->count, now. - Ted 1/27/92)
2085*4882a593Smuzhiyun *
2086*4882a593Smuzhiyun * The termios state of a pty is reset on first open so that
2087*4882a593Smuzhiyun * settings don't persist across reuse.
2088*4882a593Smuzhiyun *
2089*4882a593Smuzhiyun * Locking: tty_mutex protects tty, tty_lookup_driver and tty_init_dev.
2090*4882a593Smuzhiyun * tty->count should protect the rest.
2091*4882a593Smuzhiyun * ->siglock protects ->signal/->sighand
2092*4882a593Smuzhiyun *
2093*4882a593Smuzhiyun * Note: the tty_unlock/lock cases without a ref are only safe due to
2094*4882a593Smuzhiyun * tty_mutex
2095*4882a593Smuzhiyun */
2096*4882a593Smuzhiyun
tty_open(struct inode * inode,struct file * filp)2097*4882a593Smuzhiyun static int tty_open(struct inode *inode, struct file *filp)
2098*4882a593Smuzhiyun {
2099*4882a593Smuzhiyun struct tty_struct *tty;
2100*4882a593Smuzhiyun int noctty, retval;
2101*4882a593Smuzhiyun dev_t device = inode->i_rdev;
2102*4882a593Smuzhiyun unsigned saved_flags = filp->f_flags;
2103*4882a593Smuzhiyun
2104*4882a593Smuzhiyun nonseekable_open(inode, filp);
2105*4882a593Smuzhiyun
2106*4882a593Smuzhiyun retry_open:
2107*4882a593Smuzhiyun retval = tty_alloc_file(filp);
2108*4882a593Smuzhiyun if (retval)
2109*4882a593Smuzhiyun return -ENOMEM;
2110*4882a593Smuzhiyun
2111*4882a593Smuzhiyun tty = tty_open_current_tty(device, filp);
2112*4882a593Smuzhiyun if (!tty)
2113*4882a593Smuzhiyun tty = tty_open_by_driver(device, filp);
2114*4882a593Smuzhiyun
2115*4882a593Smuzhiyun if (IS_ERR(tty)) {
2116*4882a593Smuzhiyun tty_free_file(filp);
2117*4882a593Smuzhiyun retval = PTR_ERR(tty);
2118*4882a593Smuzhiyun if (retval != -EAGAIN || signal_pending(current))
2119*4882a593Smuzhiyun return retval;
2120*4882a593Smuzhiyun schedule();
2121*4882a593Smuzhiyun goto retry_open;
2122*4882a593Smuzhiyun }
2123*4882a593Smuzhiyun
2124*4882a593Smuzhiyun tty_add_file(tty, filp);
2125*4882a593Smuzhiyun
2126*4882a593Smuzhiyun check_tty_count(tty, __func__);
2127*4882a593Smuzhiyun tty_debug_hangup(tty, "opening (count=%d)\n", tty->count);
2128*4882a593Smuzhiyun
2129*4882a593Smuzhiyun if (tty->ops->open)
2130*4882a593Smuzhiyun retval = tty->ops->open(tty, filp);
2131*4882a593Smuzhiyun else
2132*4882a593Smuzhiyun retval = -ENODEV;
2133*4882a593Smuzhiyun filp->f_flags = saved_flags;
2134*4882a593Smuzhiyun
2135*4882a593Smuzhiyun if (retval) {
2136*4882a593Smuzhiyun tty_debug_hangup(tty, "open error %d, releasing\n", retval);
2137*4882a593Smuzhiyun
2138*4882a593Smuzhiyun tty_unlock(tty); /* need to call tty_release without BTM */
2139*4882a593Smuzhiyun tty_release(inode, filp);
2140*4882a593Smuzhiyun if (retval != -ERESTARTSYS)
2141*4882a593Smuzhiyun return retval;
2142*4882a593Smuzhiyun
2143*4882a593Smuzhiyun if (signal_pending(current))
2144*4882a593Smuzhiyun return retval;
2145*4882a593Smuzhiyun
2146*4882a593Smuzhiyun schedule();
2147*4882a593Smuzhiyun /*
2148*4882a593Smuzhiyun * Need to reset f_op in case a hangup happened.
2149*4882a593Smuzhiyun */
2150*4882a593Smuzhiyun if (tty_hung_up_p(filp))
2151*4882a593Smuzhiyun filp->f_op = &tty_fops;
2152*4882a593Smuzhiyun goto retry_open;
2153*4882a593Smuzhiyun }
2154*4882a593Smuzhiyun clear_bit(TTY_HUPPED, &tty->flags);
2155*4882a593Smuzhiyun
2156*4882a593Smuzhiyun noctty = (filp->f_flags & O_NOCTTY) ||
2157*4882a593Smuzhiyun (IS_ENABLED(CONFIG_VT) && device == MKDEV(TTY_MAJOR, 0)) ||
2158*4882a593Smuzhiyun device == MKDEV(TTYAUX_MAJOR, 1) ||
2159*4882a593Smuzhiyun (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
2160*4882a593Smuzhiyun tty->driver->subtype == PTY_TYPE_MASTER);
2161*4882a593Smuzhiyun if (!noctty)
2162*4882a593Smuzhiyun tty_open_proc_set_tty(filp, tty);
2163*4882a593Smuzhiyun tty_unlock(tty);
2164*4882a593Smuzhiyun return 0;
2165*4882a593Smuzhiyun }
2166*4882a593Smuzhiyun
2167*4882a593Smuzhiyun
2168*4882a593Smuzhiyun
2169*4882a593Smuzhiyun /**
2170*4882a593Smuzhiyun * tty_poll - check tty status
2171*4882a593Smuzhiyun * @filp: file being polled
2172*4882a593Smuzhiyun * @wait: poll wait structures to update
2173*4882a593Smuzhiyun *
2174*4882a593Smuzhiyun * Call the line discipline polling method to obtain the poll
2175*4882a593Smuzhiyun * status of the device.
2176*4882a593Smuzhiyun *
2177*4882a593Smuzhiyun * Locking: locks called line discipline but ldisc poll method
2178*4882a593Smuzhiyun * may be re-entered freely by other callers.
2179*4882a593Smuzhiyun */
2180*4882a593Smuzhiyun
tty_poll(struct file * filp,poll_table * wait)2181*4882a593Smuzhiyun static __poll_t tty_poll(struct file *filp, poll_table *wait)
2182*4882a593Smuzhiyun {
2183*4882a593Smuzhiyun struct tty_struct *tty = file_tty(filp);
2184*4882a593Smuzhiyun struct tty_ldisc *ld;
2185*4882a593Smuzhiyun __poll_t ret = 0;
2186*4882a593Smuzhiyun
2187*4882a593Smuzhiyun if (tty_paranoia_check(tty, file_inode(filp), "tty_poll"))
2188*4882a593Smuzhiyun return 0;
2189*4882a593Smuzhiyun
2190*4882a593Smuzhiyun ld = tty_ldisc_ref_wait(tty);
2191*4882a593Smuzhiyun if (!ld)
2192*4882a593Smuzhiyun return hung_up_tty_poll(filp, wait);
2193*4882a593Smuzhiyun if (ld->ops->poll)
2194*4882a593Smuzhiyun ret = ld->ops->poll(tty, filp, wait);
2195*4882a593Smuzhiyun tty_ldisc_deref(ld);
2196*4882a593Smuzhiyun return ret;
2197*4882a593Smuzhiyun }
2198*4882a593Smuzhiyun
__tty_fasync(int fd,struct file * filp,int on)2199*4882a593Smuzhiyun static int __tty_fasync(int fd, struct file *filp, int on)
2200*4882a593Smuzhiyun {
2201*4882a593Smuzhiyun struct tty_struct *tty = file_tty(filp);
2202*4882a593Smuzhiyun unsigned long flags;
2203*4882a593Smuzhiyun int retval = 0;
2204*4882a593Smuzhiyun
2205*4882a593Smuzhiyun if (tty_paranoia_check(tty, file_inode(filp), "tty_fasync"))
2206*4882a593Smuzhiyun goto out;
2207*4882a593Smuzhiyun
2208*4882a593Smuzhiyun retval = fasync_helper(fd, filp, on, &tty->fasync);
2209*4882a593Smuzhiyun if (retval <= 0)
2210*4882a593Smuzhiyun goto out;
2211*4882a593Smuzhiyun
2212*4882a593Smuzhiyun if (on) {
2213*4882a593Smuzhiyun enum pid_type type;
2214*4882a593Smuzhiyun struct pid *pid;
2215*4882a593Smuzhiyun
2216*4882a593Smuzhiyun spin_lock_irqsave(&tty->ctrl_lock, flags);
2217*4882a593Smuzhiyun if (tty->pgrp) {
2218*4882a593Smuzhiyun pid = tty->pgrp;
2219*4882a593Smuzhiyun type = PIDTYPE_PGID;
2220*4882a593Smuzhiyun } else {
2221*4882a593Smuzhiyun pid = task_pid(current);
2222*4882a593Smuzhiyun type = PIDTYPE_TGID;
2223*4882a593Smuzhiyun }
2224*4882a593Smuzhiyun get_pid(pid);
2225*4882a593Smuzhiyun spin_unlock_irqrestore(&tty->ctrl_lock, flags);
2226*4882a593Smuzhiyun __f_setown(filp, pid, type, 0);
2227*4882a593Smuzhiyun put_pid(pid);
2228*4882a593Smuzhiyun retval = 0;
2229*4882a593Smuzhiyun }
2230*4882a593Smuzhiyun out:
2231*4882a593Smuzhiyun return retval;
2232*4882a593Smuzhiyun }
2233*4882a593Smuzhiyun
tty_fasync(int fd,struct file * filp,int on)2234*4882a593Smuzhiyun static int tty_fasync(int fd, struct file *filp, int on)
2235*4882a593Smuzhiyun {
2236*4882a593Smuzhiyun struct tty_struct *tty = file_tty(filp);
2237*4882a593Smuzhiyun int retval = -ENOTTY;
2238*4882a593Smuzhiyun
2239*4882a593Smuzhiyun tty_lock(tty);
2240*4882a593Smuzhiyun if (!tty_hung_up_p(filp))
2241*4882a593Smuzhiyun retval = __tty_fasync(fd, filp, on);
2242*4882a593Smuzhiyun tty_unlock(tty);
2243*4882a593Smuzhiyun
2244*4882a593Smuzhiyun return retval;
2245*4882a593Smuzhiyun }
2246*4882a593Smuzhiyun
2247*4882a593Smuzhiyun /**
2248*4882a593Smuzhiyun * tiocsti - fake input character
2249*4882a593Smuzhiyun * @tty: tty to fake input into
2250*4882a593Smuzhiyun * @p: pointer to character
2251*4882a593Smuzhiyun *
2252*4882a593Smuzhiyun * Fake input to a tty device. Does the necessary locking and
2253*4882a593Smuzhiyun * input management.
2254*4882a593Smuzhiyun *
2255*4882a593Smuzhiyun * FIXME: does not honour flow control ??
2256*4882a593Smuzhiyun *
2257*4882a593Smuzhiyun * Locking:
2258*4882a593Smuzhiyun * Called functions take tty_ldiscs_lock
2259*4882a593Smuzhiyun * current->signal->tty check is safe without locks
2260*4882a593Smuzhiyun */
2261*4882a593Smuzhiyun
tiocsti(struct tty_struct * tty,char __user * p)2262*4882a593Smuzhiyun static int tiocsti(struct tty_struct *tty, char __user *p)
2263*4882a593Smuzhiyun {
2264*4882a593Smuzhiyun char ch, mbz = 0;
2265*4882a593Smuzhiyun struct tty_ldisc *ld;
2266*4882a593Smuzhiyun
2267*4882a593Smuzhiyun if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN))
2268*4882a593Smuzhiyun return -EPERM;
2269*4882a593Smuzhiyun if (get_user(ch, p))
2270*4882a593Smuzhiyun return -EFAULT;
2271*4882a593Smuzhiyun tty_audit_tiocsti(tty, ch);
2272*4882a593Smuzhiyun ld = tty_ldisc_ref_wait(tty);
2273*4882a593Smuzhiyun if (!ld)
2274*4882a593Smuzhiyun return -EIO;
2275*4882a593Smuzhiyun tty_buffer_lock_exclusive(tty->port);
2276*4882a593Smuzhiyun if (ld->ops->receive_buf)
2277*4882a593Smuzhiyun ld->ops->receive_buf(tty, &ch, &mbz, 1);
2278*4882a593Smuzhiyun tty_buffer_unlock_exclusive(tty->port);
2279*4882a593Smuzhiyun tty_ldisc_deref(ld);
2280*4882a593Smuzhiyun return 0;
2281*4882a593Smuzhiyun }
2282*4882a593Smuzhiyun
2283*4882a593Smuzhiyun /**
2284*4882a593Smuzhiyun * tiocgwinsz - implement window query ioctl
2285*4882a593Smuzhiyun * @tty: tty
2286*4882a593Smuzhiyun * @arg: user buffer for result
2287*4882a593Smuzhiyun *
2288*4882a593Smuzhiyun * Copies the kernel idea of the window size into the user buffer.
2289*4882a593Smuzhiyun *
2290*4882a593Smuzhiyun * Locking: tty->winsize_mutex is taken to ensure the winsize data
2291*4882a593Smuzhiyun * is consistent.
2292*4882a593Smuzhiyun */
2293*4882a593Smuzhiyun
tiocgwinsz(struct tty_struct * tty,struct winsize __user * arg)2294*4882a593Smuzhiyun static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg)
2295*4882a593Smuzhiyun {
2296*4882a593Smuzhiyun int err;
2297*4882a593Smuzhiyun
2298*4882a593Smuzhiyun mutex_lock(&tty->winsize_mutex);
2299*4882a593Smuzhiyun err = copy_to_user(arg, &tty->winsize, sizeof(*arg));
2300*4882a593Smuzhiyun mutex_unlock(&tty->winsize_mutex);
2301*4882a593Smuzhiyun
2302*4882a593Smuzhiyun return err ? -EFAULT: 0;
2303*4882a593Smuzhiyun }
2304*4882a593Smuzhiyun
2305*4882a593Smuzhiyun /**
2306*4882a593Smuzhiyun * tty_do_resize - resize event
2307*4882a593Smuzhiyun * @tty: tty being resized
2308*4882a593Smuzhiyun * @ws: new dimensions
2309*4882a593Smuzhiyun *
2310*4882a593Smuzhiyun * Update the termios variables and send the necessary signals to
2311*4882a593Smuzhiyun * peform a terminal resize correctly
2312*4882a593Smuzhiyun */
2313*4882a593Smuzhiyun
tty_do_resize(struct tty_struct * tty,struct winsize * ws)2314*4882a593Smuzhiyun int tty_do_resize(struct tty_struct *tty, struct winsize *ws)
2315*4882a593Smuzhiyun {
2316*4882a593Smuzhiyun struct pid *pgrp;
2317*4882a593Smuzhiyun
2318*4882a593Smuzhiyun /* Lock the tty */
2319*4882a593Smuzhiyun mutex_lock(&tty->winsize_mutex);
2320*4882a593Smuzhiyun if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
2321*4882a593Smuzhiyun goto done;
2322*4882a593Smuzhiyun
2323*4882a593Smuzhiyun /* Signal the foreground process group */
2324*4882a593Smuzhiyun pgrp = tty_get_pgrp(tty);
2325*4882a593Smuzhiyun if (pgrp)
2326*4882a593Smuzhiyun kill_pgrp(pgrp, SIGWINCH, 1);
2327*4882a593Smuzhiyun put_pid(pgrp);
2328*4882a593Smuzhiyun
2329*4882a593Smuzhiyun tty->winsize = *ws;
2330*4882a593Smuzhiyun done:
2331*4882a593Smuzhiyun mutex_unlock(&tty->winsize_mutex);
2332*4882a593Smuzhiyun return 0;
2333*4882a593Smuzhiyun }
2334*4882a593Smuzhiyun EXPORT_SYMBOL(tty_do_resize);
2335*4882a593Smuzhiyun
2336*4882a593Smuzhiyun /**
2337*4882a593Smuzhiyun * tiocswinsz - implement window size set ioctl
2338*4882a593Smuzhiyun * @tty: tty side of tty
2339*4882a593Smuzhiyun * @arg: user buffer for result
2340*4882a593Smuzhiyun *
2341*4882a593Smuzhiyun * Copies the user idea of the window size to the kernel. Traditionally
2342*4882a593Smuzhiyun * this is just advisory information but for the Linux console it
2343*4882a593Smuzhiyun * actually has driver level meaning and triggers a VC resize.
2344*4882a593Smuzhiyun *
2345*4882a593Smuzhiyun * Locking:
2346*4882a593Smuzhiyun * Driver dependent. The default do_resize method takes the
2347*4882a593Smuzhiyun * tty termios mutex and ctrl_lock. The console takes its own lock
2348*4882a593Smuzhiyun * then calls into the default method.
2349*4882a593Smuzhiyun */
2350*4882a593Smuzhiyun
tiocswinsz(struct tty_struct * tty,struct winsize __user * arg)2351*4882a593Smuzhiyun static int tiocswinsz(struct tty_struct *tty, struct winsize __user *arg)
2352*4882a593Smuzhiyun {
2353*4882a593Smuzhiyun struct winsize tmp_ws;
2354*4882a593Smuzhiyun if (copy_from_user(&tmp_ws, arg, sizeof(*arg)))
2355*4882a593Smuzhiyun return -EFAULT;
2356*4882a593Smuzhiyun
2357*4882a593Smuzhiyun if (tty->ops->resize)
2358*4882a593Smuzhiyun return tty->ops->resize(tty, &tmp_ws);
2359*4882a593Smuzhiyun else
2360*4882a593Smuzhiyun return tty_do_resize(tty, &tmp_ws);
2361*4882a593Smuzhiyun }
2362*4882a593Smuzhiyun
2363*4882a593Smuzhiyun /**
2364*4882a593Smuzhiyun * tioccons - allow admin to move logical console
2365*4882a593Smuzhiyun * @file: the file to become console
2366*4882a593Smuzhiyun *
2367*4882a593Smuzhiyun * Allow the administrator to move the redirected console device
2368*4882a593Smuzhiyun *
2369*4882a593Smuzhiyun * Locking: uses redirect_lock to guard the redirect information
2370*4882a593Smuzhiyun */
2371*4882a593Smuzhiyun
tioccons(struct file * file)2372*4882a593Smuzhiyun static int tioccons(struct file *file)
2373*4882a593Smuzhiyun {
2374*4882a593Smuzhiyun if (!capable(CAP_SYS_ADMIN))
2375*4882a593Smuzhiyun return -EPERM;
2376*4882a593Smuzhiyun if (file->f_op->write_iter == redirected_tty_write) {
2377*4882a593Smuzhiyun struct file *f;
2378*4882a593Smuzhiyun spin_lock(&redirect_lock);
2379*4882a593Smuzhiyun f = redirect;
2380*4882a593Smuzhiyun redirect = NULL;
2381*4882a593Smuzhiyun spin_unlock(&redirect_lock);
2382*4882a593Smuzhiyun if (f)
2383*4882a593Smuzhiyun fput(f);
2384*4882a593Smuzhiyun return 0;
2385*4882a593Smuzhiyun }
2386*4882a593Smuzhiyun if (file->f_op->write_iter != tty_write)
2387*4882a593Smuzhiyun return -ENOTTY;
2388*4882a593Smuzhiyun if (!(file->f_mode & FMODE_WRITE))
2389*4882a593Smuzhiyun return -EBADF;
2390*4882a593Smuzhiyun if (!(file->f_mode & FMODE_CAN_WRITE))
2391*4882a593Smuzhiyun return -EINVAL;
2392*4882a593Smuzhiyun spin_lock(&redirect_lock);
2393*4882a593Smuzhiyun if (redirect) {
2394*4882a593Smuzhiyun spin_unlock(&redirect_lock);
2395*4882a593Smuzhiyun return -EBUSY;
2396*4882a593Smuzhiyun }
2397*4882a593Smuzhiyun redirect = get_file(file);
2398*4882a593Smuzhiyun spin_unlock(&redirect_lock);
2399*4882a593Smuzhiyun return 0;
2400*4882a593Smuzhiyun }
2401*4882a593Smuzhiyun
2402*4882a593Smuzhiyun /**
2403*4882a593Smuzhiyun * tiocsetd - set line discipline
2404*4882a593Smuzhiyun * @tty: tty device
2405*4882a593Smuzhiyun * @p: pointer to user data
2406*4882a593Smuzhiyun *
2407*4882a593Smuzhiyun * Set the line discipline according to user request.
2408*4882a593Smuzhiyun *
2409*4882a593Smuzhiyun * Locking: see tty_set_ldisc, this function is just a helper
2410*4882a593Smuzhiyun */
2411*4882a593Smuzhiyun
tiocsetd(struct tty_struct * tty,int __user * p)2412*4882a593Smuzhiyun static int tiocsetd(struct tty_struct *tty, int __user *p)
2413*4882a593Smuzhiyun {
2414*4882a593Smuzhiyun int disc;
2415*4882a593Smuzhiyun int ret;
2416*4882a593Smuzhiyun
2417*4882a593Smuzhiyun if (get_user(disc, p))
2418*4882a593Smuzhiyun return -EFAULT;
2419*4882a593Smuzhiyun
2420*4882a593Smuzhiyun ret = tty_set_ldisc(tty, disc);
2421*4882a593Smuzhiyun
2422*4882a593Smuzhiyun return ret;
2423*4882a593Smuzhiyun }
2424*4882a593Smuzhiyun
2425*4882a593Smuzhiyun /**
2426*4882a593Smuzhiyun * tiocgetd - get line discipline
2427*4882a593Smuzhiyun * @tty: tty device
2428*4882a593Smuzhiyun * @p: pointer to user data
2429*4882a593Smuzhiyun *
2430*4882a593Smuzhiyun * Retrieves the line discipline id directly from the ldisc.
2431*4882a593Smuzhiyun *
2432*4882a593Smuzhiyun * Locking: waits for ldisc reference (in case the line discipline
2433*4882a593Smuzhiyun * is changing or the tty is being hungup)
2434*4882a593Smuzhiyun */
2435*4882a593Smuzhiyun
tiocgetd(struct tty_struct * tty,int __user * p)2436*4882a593Smuzhiyun static int tiocgetd(struct tty_struct *tty, int __user *p)
2437*4882a593Smuzhiyun {
2438*4882a593Smuzhiyun struct tty_ldisc *ld;
2439*4882a593Smuzhiyun int ret;
2440*4882a593Smuzhiyun
2441*4882a593Smuzhiyun ld = tty_ldisc_ref_wait(tty);
2442*4882a593Smuzhiyun if (!ld)
2443*4882a593Smuzhiyun return -EIO;
2444*4882a593Smuzhiyun ret = put_user(ld->ops->num, p);
2445*4882a593Smuzhiyun tty_ldisc_deref(ld);
2446*4882a593Smuzhiyun return ret;
2447*4882a593Smuzhiyun }
2448*4882a593Smuzhiyun
2449*4882a593Smuzhiyun /**
2450*4882a593Smuzhiyun * send_break - performed time break
2451*4882a593Smuzhiyun * @tty: device to break on
2452*4882a593Smuzhiyun * @duration: timeout in mS
2453*4882a593Smuzhiyun *
2454*4882a593Smuzhiyun * Perform a timed break on hardware that lacks its own driver level
2455*4882a593Smuzhiyun * timed break functionality.
2456*4882a593Smuzhiyun *
2457*4882a593Smuzhiyun * Locking:
2458*4882a593Smuzhiyun * atomic_write_lock serializes
2459*4882a593Smuzhiyun *
2460*4882a593Smuzhiyun */
2461*4882a593Smuzhiyun
send_break(struct tty_struct * tty,unsigned int duration)2462*4882a593Smuzhiyun static int send_break(struct tty_struct *tty, unsigned int duration)
2463*4882a593Smuzhiyun {
2464*4882a593Smuzhiyun int retval;
2465*4882a593Smuzhiyun
2466*4882a593Smuzhiyun if (tty->ops->break_ctl == NULL)
2467*4882a593Smuzhiyun return 0;
2468*4882a593Smuzhiyun
2469*4882a593Smuzhiyun if (tty->driver->flags & TTY_DRIVER_HARDWARE_BREAK)
2470*4882a593Smuzhiyun retval = tty->ops->break_ctl(tty, duration);
2471*4882a593Smuzhiyun else {
2472*4882a593Smuzhiyun /* Do the work ourselves */
2473*4882a593Smuzhiyun if (tty_write_lock(tty, 0) < 0)
2474*4882a593Smuzhiyun return -EINTR;
2475*4882a593Smuzhiyun retval = tty->ops->break_ctl(tty, -1);
2476*4882a593Smuzhiyun if (retval)
2477*4882a593Smuzhiyun goto out;
2478*4882a593Smuzhiyun if (!signal_pending(current))
2479*4882a593Smuzhiyun msleep_interruptible(duration);
2480*4882a593Smuzhiyun retval = tty->ops->break_ctl(tty, 0);
2481*4882a593Smuzhiyun out:
2482*4882a593Smuzhiyun tty_write_unlock(tty);
2483*4882a593Smuzhiyun if (signal_pending(current))
2484*4882a593Smuzhiyun retval = -EINTR;
2485*4882a593Smuzhiyun }
2486*4882a593Smuzhiyun return retval;
2487*4882a593Smuzhiyun }
2488*4882a593Smuzhiyun
2489*4882a593Smuzhiyun /**
2490*4882a593Smuzhiyun * tty_tiocmget - get modem status
2491*4882a593Smuzhiyun * @tty: tty device
2492*4882a593Smuzhiyun * @p: pointer to result
2493*4882a593Smuzhiyun *
2494*4882a593Smuzhiyun * Obtain the modem status bits from the tty driver if the feature
2495*4882a593Smuzhiyun * is supported. Return -ENOTTY if it is not available.
2496*4882a593Smuzhiyun *
2497*4882a593Smuzhiyun * Locking: none (up to the driver)
2498*4882a593Smuzhiyun */
2499*4882a593Smuzhiyun
tty_tiocmget(struct tty_struct * tty,int __user * p)2500*4882a593Smuzhiyun static int tty_tiocmget(struct tty_struct *tty, int __user *p)
2501*4882a593Smuzhiyun {
2502*4882a593Smuzhiyun int retval = -ENOTTY;
2503*4882a593Smuzhiyun
2504*4882a593Smuzhiyun if (tty->ops->tiocmget) {
2505*4882a593Smuzhiyun retval = tty->ops->tiocmget(tty);
2506*4882a593Smuzhiyun
2507*4882a593Smuzhiyun if (retval >= 0)
2508*4882a593Smuzhiyun retval = put_user(retval, p);
2509*4882a593Smuzhiyun }
2510*4882a593Smuzhiyun return retval;
2511*4882a593Smuzhiyun }
2512*4882a593Smuzhiyun
2513*4882a593Smuzhiyun /**
2514*4882a593Smuzhiyun * tty_tiocmset - set modem status
2515*4882a593Smuzhiyun * @tty: tty device
2516*4882a593Smuzhiyun * @cmd: command - clear bits, set bits or set all
2517*4882a593Smuzhiyun * @p: pointer to desired bits
2518*4882a593Smuzhiyun *
2519*4882a593Smuzhiyun * Set the modem status bits from the tty driver if the feature
2520*4882a593Smuzhiyun * is supported. Return -ENOTTY if it is not available.
2521*4882a593Smuzhiyun *
2522*4882a593Smuzhiyun * Locking: none (up to the driver)
2523*4882a593Smuzhiyun */
2524*4882a593Smuzhiyun
tty_tiocmset(struct tty_struct * tty,unsigned int cmd,unsigned __user * p)2525*4882a593Smuzhiyun static int tty_tiocmset(struct tty_struct *tty, unsigned int cmd,
2526*4882a593Smuzhiyun unsigned __user *p)
2527*4882a593Smuzhiyun {
2528*4882a593Smuzhiyun int retval;
2529*4882a593Smuzhiyun unsigned int set, clear, val;
2530*4882a593Smuzhiyun
2531*4882a593Smuzhiyun if (tty->ops->tiocmset == NULL)
2532*4882a593Smuzhiyun return -ENOTTY;
2533*4882a593Smuzhiyun
2534*4882a593Smuzhiyun retval = get_user(val, p);
2535*4882a593Smuzhiyun if (retval)
2536*4882a593Smuzhiyun return retval;
2537*4882a593Smuzhiyun set = clear = 0;
2538*4882a593Smuzhiyun switch (cmd) {
2539*4882a593Smuzhiyun case TIOCMBIS:
2540*4882a593Smuzhiyun set = val;
2541*4882a593Smuzhiyun break;
2542*4882a593Smuzhiyun case TIOCMBIC:
2543*4882a593Smuzhiyun clear = val;
2544*4882a593Smuzhiyun break;
2545*4882a593Smuzhiyun case TIOCMSET:
2546*4882a593Smuzhiyun set = val;
2547*4882a593Smuzhiyun clear = ~val;
2548*4882a593Smuzhiyun break;
2549*4882a593Smuzhiyun }
2550*4882a593Smuzhiyun set &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
2551*4882a593Smuzhiyun clear &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
2552*4882a593Smuzhiyun return tty->ops->tiocmset(tty, set, clear);
2553*4882a593Smuzhiyun }
2554*4882a593Smuzhiyun
tty_tiocgicount(struct tty_struct * tty,void __user * arg)2555*4882a593Smuzhiyun static int tty_tiocgicount(struct tty_struct *tty, void __user *arg)
2556*4882a593Smuzhiyun {
2557*4882a593Smuzhiyun int retval = -EINVAL;
2558*4882a593Smuzhiyun struct serial_icounter_struct icount;
2559*4882a593Smuzhiyun memset(&icount, 0, sizeof(icount));
2560*4882a593Smuzhiyun if (tty->ops->get_icount)
2561*4882a593Smuzhiyun retval = tty->ops->get_icount(tty, &icount);
2562*4882a593Smuzhiyun if (retval != 0)
2563*4882a593Smuzhiyun return retval;
2564*4882a593Smuzhiyun if (copy_to_user(arg, &icount, sizeof(icount)))
2565*4882a593Smuzhiyun return -EFAULT;
2566*4882a593Smuzhiyun return 0;
2567*4882a593Smuzhiyun }
2568*4882a593Smuzhiyun
tty_tiocsserial(struct tty_struct * tty,struct serial_struct __user * ss)2569*4882a593Smuzhiyun static int tty_tiocsserial(struct tty_struct *tty, struct serial_struct __user *ss)
2570*4882a593Smuzhiyun {
2571*4882a593Smuzhiyun static DEFINE_RATELIMIT_STATE(depr_flags,
2572*4882a593Smuzhiyun DEFAULT_RATELIMIT_INTERVAL,
2573*4882a593Smuzhiyun DEFAULT_RATELIMIT_BURST);
2574*4882a593Smuzhiyun char comm[TASK_COMM_LEN];
2575*4882a593Smuzhiyun struct serial_struct v;
2576*4882a593Smuzhiyun int flags;
2577*4882a593Smuzhiyun
2578*4882a593Smuzhiyun if (copy_from_user(&v, ss, sizeof(*ss)))
2579*4882a593Smuzhiyun return -EFAULT;
2580*4882a593Smuzhiyun
2581*4882a593Smuzhiyun flags = v.flags & ASYNC_DEPRECATED;
2582*4882a593Smuzhiyun
2583*4882a593Smuzhiyun if (flags && __ratelimit(&depr_flags))
2584*4882a593Smuzhiyun pr_warn("%s: '%s' is using deprecated serial flags (with no effect): %.8x\n",
2585*4882a593Smuzhiyun __func__, get_task_comm(comm, current), flags);
2586*4882a593Smuzhiyun if (!tty->ops->set_serial)
2587*4882a593Smuzhiyun return -ENOTTY;
2588*4882a593Smuzhiyun return tty->ops->set_serial(tty, &v);
2589*4882a593Smuzhiyun }
2590*4882a593Smuzhiyun
tty_tiocgserial(struct tty_struct * tty,struct serial_struct __user * ss)2591*4882a593Smuzhiyun static int tty_tiocgserial(struct tty_struct *tty, struct serial_struct __user *ss)
2592*4882a593Smuzhiyun {
2593*4882a593Smuzhiyun struct serial_struct v;
2594*4882a593Smuzhiyun int err;
2595*4882a593Smuzhiyun
2596*4882a593Smuzhiyun memset(&v, 0, sizeof(v));
2597*4882a593Smuzhiyun if (!tty->ops->get_serial)
2598*4882a593Smuzhiyun return -ENOTTY;
2599*4882a593Smuzhiyun err = tty->ops->get_serial(tty, &v);
2600*4882a593Smuzhiyun if (!err && copy_to_user(ss, &v, sizeof(v)))
2601*4882a593Smuzhiyun err = -EFAULT;
2602*4882a593Smuzhiyun return err;
2603*4882a593Smuzhiyun }
2604*4882a593Smuzhiyun
2605*4882a593Smuzhiyun /*
2606*4882a593Smuzhiyun * if pty, return the slave side (real_tty)
2607*4882a593Smuzhiyun * otherwise, return self
2608*4882a593Smuzhiyun */
tty_pair_get_tty(struct tty_struct * tty)2609*4882a593Smuzhiyun static struct tty_struct *tty_pair_get_tty(struct tty_struct *tty)
2610*4882a593Smuzhiyun {
2611*4882a593Smuzhiyun if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
2612*4882a593Smuzhiyun tty->driver->subtype == PTY_TYPE_MASTER)
2613*4882a593Smuzhiyun tty = tty->link;
2614*4882a593Smuzhiyun return tty;
2615*4882a593Smuzhiyun }
2616*4882a593Smuzhiyun
2617*4882a593Smuzhiyun /*
2618*4882a593Smuzhiyun * Split this up, as gcc can choke on it otherwise..
2619*4882a593Smuzhiyun */
tty_ioctl(struct file * file,unsigned int cmd,unsigned long arg)2620*4882a593Smuzhiyun long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2621*4882a593Smuzhiyun {
2622*4882a593Smuzhiyun struct tty_struct *tty = file_tty(file);
2623*4882a593Smuzhiyun struct tty_struct *real_tty;
2624*4882a593Smuzhiyun void __user *p = (void __user *)arg;
2625*4882a593Smuzhiyun int retval;
2626*4882a593Smuzhiyun struct tty_ldisc *ld;
2627*4882a593Smuzhiyun
2628*4882a593Smuzhiyun if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl"))
2629*4882a593Smuzhiyun return -EINVAL;
2630*4882a593Smuzhiyun
2631*4882a593Smuzhiyun real_tty = tty_pair_get_tty(tty);
2632*4882a593Smuzhiyun
2633*4882a593Smuzhiyun /*
2634*4882a593Smuzhiyun * Factor out some common prep work
2635*4882a593Smuzhiyun */
2636*4882a593Smuzhiyun switch (cmd) {
2637*4882a593Smuzhiyun case TIOCSETD:
2638*4882a593Smuzhiyun case TIOCSBRK:
2639*4882a593Smuzhiyun case TIOCCBRK:
2640*4882a593Smuzhiyun case TCSBRK:
2641*4882a593Smuzhiyun case TCSBRKP:
2642*4882a593Smuzhiyun retval = tty_check_change(tty);
2643*4882a593Smuzhiyun if (retval)
2644*4882a593Smuzhiyun return retval;
2645*4882a593Smuzhiyun if (cmd != TIOCCBRK) {
2646*4882a593Smuzhiyun tty_wait_until_sent(tty, 0);
2647*4882a593Smuzhiyun if (signal_pending(current))
2648*4882a593Smuzhiyun return -EINTR;
2649*4882a593Smuzhiyun }
2650*4882a593Smuzhiyun break;
2651*4882a593Smuzhiyun }
2652*4882a593Smuzhiyun
2653*4882a593Smuzhiyun /*
2654*4882a593Smuzhiyun * Now do the stuff.
2655*4882a593Smuzhiyun */
2656*4882a593Smuzhiyun switch (cmd) {
2657*4882a593Smuzhiyun case TIOCSTI:
2658*4882a593Smuzhiyun return tiocsti(tty, p);
2659*4882a593Smuzhiyun case TIOCGWINSZ:
2660*4882a593Smuzhiyun return tiocgwinsz(real_tty, p);
2661*4882a593Smuzhiyun case TIOCSWINSZ:
2662*4882a593Smuzhiyun return tiocswinsz(real_tty, p);
2663*4882a593Smuzhiyun case TIOCCONS:
2664*4882a593Smuzhiyun return real_tty != tty ? -EINVAL : tioccons(file);
2665*4882a593Smuzhiyun case TIOCEXCL:
2666*4882a593Smuzhiyun set_bit(TTY_EXCLUSIVE, &tty->flags);
2667*4882a593Smuzhiyun return 0;
2668*4882a593Smuzhiyun case TIOCNXCL:
2669*4882a593Smuzhiyun clear_bit(TTY_EXCLUSIVE, &tty->flags);
2670*4882a593Smuzhiyun return 0;
2671*4882a593Smuzhiyun case TIOCGEXCL:
2672*4882a593Smuzhiyun {
2673*4882a593Smuzhiyun int excl = test_bit(TTY_EXCLUSIVE, &tty->flags);
2674*4882a593Smuzhiyun return put_user(excl, (int __user *)p);
2675*4882a593Smuzhiyun }
2676*4882a593Smuzhiyun case TIOCGETD:
2677*4882a593Smuzhiyun return tiocgetd(tty, p);
2678*4882a593Smuzhiyun case TIOCSETD:
2679*4882a593Smuzhiyun return tiocsetd(tty, p);
2680*4882a593Smuzhiyun case TIOCVHANGUP:
2681*4882a593Smuzhiyun if (!capable(CAP_SYS_ADMIN))
2682*4882a593Smuzhiyun return -EPERM;
2683*4882a593Smuzhiyun tty_vhangup(tty);
2684*4882a593Smuzhiyun return 0;
2685*4882a593Smuzhiyun case TIOCGDEV:
2686*4882a593Smuzhiyun {
2687*4882a593Smuzhiyun unsigned int ret = new_encode_dev(tty_devnum(real_tty));
2688*4882a593Smuzhiyun return put_user(ret, (unsigned int __user *)p);
2689*4882a593Smuzhiyun }
2690*4882a593Smuzhiyun /*
2691*4882a593Smuzhiyun * Break handling
2692*4882a593Smuzhiyun */
2693*4882a593Smuzhiyun case TIOCSBRK: /* Turn break on, unconditionally */
2694*4882a593Smuzhiyun if (tty->ops->break_ctl)
2695*4882a593Smuzhiyun return tty->ops->break_ctl(tty, -1);
2696*4882a593Smuzhiyun return 0;
2697*4882a593Smuzhiyun case TIOCCBRK: /* Turn break off, unconditionally */
2698*4882a593Smuzhiyun if (tty->ops->break_ctl)
2699*4882a593Smuzhiyun return tty->ops->break_ctl(tty, 0);
2700*4882a593Smuzhiyun return 0;
2701*4882a593Smuzhiyun case TCSBRK: /* SVID version: non-zero arg --> no break */
2702*4882a593Smuzhiyun /* non-zero arg means wait for all output data
2703*4882a593Smuzhiyun * to be sent (performed above) but don't send break.
2704*4882a593Smuzhiyun * This is used by the tcdrain() termios function.
2705*4882a593Smuzhiyun */
2706*4882a593Smuzhiyun if (!arg)
2707*4882a593Smuzhiyun return send_break(tty, 250);
2708*4882a593Smuzhiyun return 0;
2709*4882a593Smuzhiyun case TCSBRKP: /* support for POSIX tcsendbreak() */
2710*4882a593Smuzhiyun return send_break(tty, arg ? arg*100 : 250);
2711*4882a593Smuzhiyun
2712*4882a593Smuzhiyun case TIOCMGET:
2713*4882a593Smuzhiyun return tty_tiocmget(tty, p);
2714*4882a593Smuzhiyun case TIOCMSET:
2715*4882a593Smuzhiyun case TIOCMBIC:
2716*4882a593Smuzhiyun case TIOCMBIS:
2717*4882a593Smuzhiyun return tty_tiocmset(tty, cmd, p);
2718*4882a593Smuzhiyun case TIOCGICOUNT:
2719*4882a593Smuzhiyun return tty_tiocgicount(tty, p);
2720*4882a593Smuzhiyun case TCFLSH:
2721*4882a593Smuzhiyun switch (arg) {
2722*4882a593Smuzhiyun case TCIFLUSH:
2723*4882a593Smuzhiyun case TCIOFLUSH:
2724*4882a593Smuzhiyun /* flush tty buffer and allow ldisc to process ioctl */
2725*4882a593Smuzhiyun tty_buffer_flush(tty, NULL);
2726*4882a593Smuzhiyun break;
2727*4882a593Smuzhiyun }
2728*4882a593Smuzhiyun break;
2729*4882a593Smuzhiyun case TIOCSSERIAL:
2730*4882a593Smuzhiyun return tty_tiocsserial(tty, p);
2731*4882a593Smuzhiyun case TIOCGSERIAL:
2732*4882a593Smuzhiyun return tty_tiocgserial(tty, p);
2733*4882a593Smuzhiyun case TIOCGPTPEER:
2734*4882a593Smuzhiyun /* Special because the struct file is needed */
2735*4882a593Smuzhiyun return ptm_open_peer(file, tty, (int)arg);
2736*4882a593Smuzhiyun default:
2737*4882a593Smuzhiyun retval = tty_jobctrl_ioctl(tty, real_tty, file, cmd, arg);
2738*4882a593Smuzhiyun if (retval != -ENOIOCTLCMD)
2739*4882a593Smuzhiyun return retval;
2740*4882a593Smuzhiyun }
2741*4882a593Smuzhiyun if (tty->ops->ioctl) {
2742*4882a593Smuzhiyun retval = tty->ops->ioctl(tty, cmd, arg);
2743*4882a593Smuzhiyun if (retval != -ENOIOCTLCMD)
2744*4882a593Smuzhiyun return retval;
2745*4882a593Smuzhiyun }
2746*4882a593Smuzhiyun ld = tty_ldisc_ref_wait(tty);
2747*4882a593Smuzhiyun if (!ld)
2748*4882a593Smuzhiyun return hung_up_tty_ioctl(file, cmd, arg);
2749*4882a593Smuzhiyun retval = -EINVAL;
2750*4882a593Smuzhiyun if (ld->ops->ioctl) {
2751*4882a593Smuzhiyun retval = ld->ops->ioctl(tty, file, cmd, arg);
2752*4882a593Smuzhiyun if (retval == -ENOIOCTLCMD)
2753*4882a593Smuzhiyun retval = -ENOTTY;
2754*4882a593Smuzhiyun }
2755*4882a593Smuzhiyun tty_ldisc_deref(ld);
2756*4882a593Smuzhiyun return retval;
2757*4882a593Smuzhiyun }
2758*4882a593Smuzhiyun
2759*4882a593Smuzhiyun #ifdef CONFIG_COMPAT
2760*4882a593Smuzhiyun
2761*4882a593Smuzhiyun struct serial_struct32 {
2762*4882a593Smuzhiyun compat_int_t type;
2763*4882a593Smuzhiyun compat_int_t line;
2764*4882a593Smuzhiyun compat_uint_t port;
2765*4882a593Smuzhiyun compat_int_t irq;
2766*4882a593Smuzhiyun compat_int_t flags;
2767*4882a593Smuzhiyun compat_int_t xmit_fifo_size;
2768*4882a593Smuzhiyun compat_int_t custom_divisor;
2769*4882a593Smuzhiyun compat_int_t baud_base;
2770*4882a593Smuzhiyun unsigned short close_delay;
2771*4882a593Smuzhiyun char io_type;
2772*4882a593Smuzhiyun char reserved_char;
2773*4882a593Smuzhiyun compat_int_t hub6;
2774*4882a593Smuzhiyun unsigned short closing_wait; /* time to wait before closing */
2775*4882a593Smuzhiyun unsigned short closing_wait2; /* no longer used... */
2776*4882a593Smuzhiyun compat_uint_t iomem_base;
2777*4882a593Smuzhiyun unsigned short iomem_reg_shift;
2778*4882a593Smuzhiyun unsigned int port_high;
2779*4882a593Smuzhiyun /* compat_ulong_t iomap_base FIXME */
2780*4882a593Smuzhiyun compat_int_t reserved;
2781*4882a593Smuzhiyun };
2782*4882a593Smuzhiyun
compat_tty_tiocsserial(struct tty_struct * tty,struct serial_struct32 __user * ss)2783*4882a593Smuzhiyun static int compat_tty_tiocsserial(struct tty_struct *tty,
2784*4882a593Smuzhiyun struct serial_struct32 __user *ss)
2785*4882a593Smuzhiyun {
2786*4882a593Smuzhiyun static DEFINE_RATELIMIT_STATE(depr_flags,
2787*4882a593Smuzhiyun DEFAULT_RATELIMIT_INTERVAL,
2788*4882a593Smuzhiyun DEFAULT_RATELIMIT_BURST);
2789*4882a593Smuzhiyun char comm[TASK_COMM_LEN];
2790*4882a593Smuzhiyun struct serial_struct32 v32;
2791*4882a593Smuzhiyun struct serial_struct v;
2792*4882a593Smuzhiyun int flags;
2793*4882a593Smuzhiyun
2794*4882a593Smuzhiyun if (copy_from_user(&v32, ss, sizeof(*ss)))
2795*4882a593Smuzhiyun return -EFAULT;
2796*4882a593Smuzhiyun
2797*4882a593Smuzhiyun memcpy(&v, &v32, offsetof(struct serial_struct32, iomem_base));
2798*4882a593Smuzhiyun v.iomem_base = compat_ptr(v32.iomem_base);
2799*4882a593Smuzhiyun v.iomem_reg_shift = v32.iomem_reg_shift;
2800*4882a593Smuzhiyun v.port_high = v32.port_high;
2801*4882a593Smuzhiyun v.iomap_base = 0;
2802*4882a593Smuzhiyun
2803*4882a593Smuzhiyun flags = v.flags & ASYNC_DEPRECATED;
2804*4882a593Smuzhiyun
2805*4882a593Smuzhiyun if (flags && __ratelimit(&depr_flags))
2806*4882a593Smuzhiyun pr_warn("%s: '%s' is using deprecated serial flags (with no effect): %.8x\n",
2807*4882a593Smuzhiyun __func__, get_task_comm(comm, current), flags);
2808*4882a593Smuzhiyun if (!tty->ops->set_serial)
2809*4882a593Smuzhiyun return -ENOTTY;
2810*4882a593Smuzhiyun return tty->ops->set_serial(tty, &v);
2811*4882a593Smuzhiyun }
2812*4882a593Smuzhiyun
compat_tty_tiocgserial(struct tty_struct * tty,struct serial_struct32 __user * ss)2813*4882a593Smuzhiyun static int compat_tty_tiocgserial(struct tty_struct *tty,
2814*4882a593Smuzhiyun struct serial_struct32 __user *ss)
2815*4882a593Smuzhiyun {
2816*4882a593Smuzhiyun struct serial_struct32 v32;
2817*4882a593Smuzhiyun struct serial_struct v;
2818*4882a593Smuzhiyun int err;
2819*4882a593Smuzhiyun
2820*4882a593Smuzhiyun memset(&v, 0, sizeof(v));
2821*4882a593Smuzhiyun memset(&v32, 0, sizeof(v32));
2822*4882a593Smuzhiyun
2823*4882a593Smuzhiyun if (!tty->ops->get_serial)
2824*4882a593Smuzhiyun return -ENOTTY;
2825*4882a593Smuzhiyun err = tty->ops->get_serial(tty, &v);
2826*4882a593Smuzhiyun if (!err) {
2827*4882a593Smuzhiyun memcpy(&v32, &v, offsetof(struct serial_struct32, iomem_base));
2828*4882a593Smuzhiyun v32.iomem_base = (unsigned long)v.iomem_base >> 32 ?
2829*4882a593Smuzhiyun 0xfffffff : ptr_to_compat(v.iomem_base);
2830*4882a593Smuzhiyun v32.iomem_reg_shift = v.iomem_reg_shift;
2831*4882a593Smuzhiyun v32.port_high = v.port_high;
2832*4882a593Smuzhiyun if (copy_to_user(ss, &v32, sizeof(v32)))
2833*4882a593Smuzhiyun err = -EFAULT;
2834*4882a593Smuzhiyun }
2835*4882a593Smuzhiyun return err;
2836*4882a593Smuzhiyun }
tty_compat_ioctl(struct file * file,unsigned int cmd,unsigned long arg)2837*4882a593Smuzhiyun static long tty_compat_ioctl(struct file *file, unsigned int cmd,
2838*4882a593Smuzhiyun unsigned long arg)
2839*4882a593Smuzhiyun {
2840*4882a593Smuzhiyun struct tty_struct *tty = file_tty(file);
2841*4882a593Smuzhiyun struct tty_ldisc *ld;
2842*4882a593Smuzhiyun int retval = -ENOIOCTLCMD;
2843*4882a593Smuzhiyun
2844*4882a593Smuzhiyun switch (cmd) {
2845*4882a593Smuzhiyun case TIOCOUTQ:
2846*4882a593Smuzhiyun case TIOCSTI:
2847*4882a593Smuzhiyun case TIOCGWINSZ:
2848*4882a593Smuzhiyun case TIOCSWINSZ:
2849*4882a593Smuzhiyun case TIOCGEXCL:
2850*4882a593Smuzhiyun case TIOCGETD:
2851*4882a593Smuzhiyun case TIOCSETD:
2852*4882a593Smuzhiyun case TIOCGDEV:
2853*4882a593Smuzhiyun case TIOCMGET:
2854*4882a593Smuzhiyun case TIOCMSET:
2855*4882a593Smuzhiyun case TIOCMBIC:
2856*4882a593Smuzhiyun case TIOCMBIS:
2857*4882a593Smuzhiyun case TIOCGICOUNT:
2858*4882a593Smuzhiyun case TIOCGPGRP:
2859*4882a593Smuzhiyun case TIOCSPGRP:
2860*4882a593Smuzhiyun case TIOCGSID:
2861*4882a593Smuzhiyun case TIOCSERGETLSR:
2862*4882a593Smuzhiyun case TIOCGRS485:
2863*4882a593Smuzhiyun case TIOCSRS485:
2864*4882a593Smuzhiyun #ifdef TIOCGETP
2865*4882a593Smuzhiyun case TIOCGETP:
2866*4882a593Smuzhiyun case TIOCSETP:
2867*4882a593Smuzhiyun case TIOCSETN:
2868*4882a593Smuzhiyun #endif
2869*4882a593Smuzhiyun #ifdef TIOCGETC
2870*4882a593Smuzhiyun case TIOCGETC:
2871*4882a593Smuzhiyun case TIOCSETC:
2872*4882a593Smuzhiyun #endif
2873*4882a593Smuzhiyun #ifdef TIOCGLTC
2874*4882a593Smuzhiyun case TIOCGLTC:
2875*4882a593Smuzhiyun case TIOCSLTC:
2876*4882a593Smuzhiyun #endif
2877*4882a593Smuzhiyun case TCSETSF:
2878*4882a593Smuzhiyun case TCSETSW:
2879*4882a593Smuzhiyun case TCSETS:
2880*4882a593Smuzhiyun case TCGETS:
2881*4882a593Smuzhiyun #ifdef TCGETS2
2882*4882a593Smuzhiyun case TCGETS2:
2883*4882a593Smuzhiyun case TCSETSF2:
2884*4882a593Smuzhiyun case TCSETSW2:
2885*4882a593Smuzhiyun case TCSETS2:
2886*4882a593Smuzhiyun #endif
2887*4882a593Smuzhiyun case TCGETA:
2888*4882a593Smuzhiyun case TCSETAF:
2889*4882a593Smuzhiyun case TCSETAW:
2890*4882a593Smuzhiyun case TCSETA:
2891*4882a593Smuzhiyun case TIOCGLCKTRMIOS:
2892*4882a593Smuzhiyun case TIOCSLCKTRMIOS:
2893*4882a593Smuzhiyun #ifdef TCGETX
2894*4882a593Smuzhiyun case TCGETX:
2895*4882a593Smuzhiyun case TCSETX:
2896*4882a593Smuzhiyun case TCSETXW:
2897*4882a593Smuzhiyun case TCSETXF:
2898*4882a593Smuzhiyun #endif
2899*4882a593Smuzhiyun case TIOCGSOFTCAR:
2900*4882a593Smuzhiyun case TIOCSSOFTCAR:
2901*4882a593Smuzhiyun
2902*4882a593Smuzhiyun case PPPIOCGCHAN:
2903*4882a593Smuzhiyun case PPPIOCGUNIT:
2904*4882a593Smuzhiyun return tty_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
2905*4882a593Smuzhiyun case TIOCCONS:
2906*4882a593Smuzhiyun case TIOCEXCL:
2907*4882a593Smuzhiyun case TIOCNXCL:
2908*4882a593Smuzhiyun case TIOCVHANGUP:
2909*4882a593Smuzhiyun case TIOCSBRK:
2910*4882a593Smuzhiyun case TIOCCBRK:
2911*4882a593Smuzhiyun case TCSBRK:
2912*4882a593Smuzhiyun case TCSBRKP:
2913*4882a593Smuzhiyun case TCFLSH:
2914*4882a593Smuzhiyun case TIOCGPTPEER:
2915*4882a593Smuzhiyun case TIOCNOTTY:
2916*4882a593Smuzhiyun case TIOCSCTTY:
2917*4882a593Smuzhiyun case TCXONC:
2918*4882a593Smuzhiyun case TIOCMIWAIT:
2919*4882a593Smuzhiyun case TIOCSERCONFIG:
2920*4882a593Smuzhiyun return tty_ioctl(file, cmd, arg);
2921*4882a593Smuzhiyun }
2922*4882a593Smuzhiyun
2923*4882a593Smuzhiyun if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl"))
2924*4882a593Smuzhiyun return -EINVAL;
2925*4882a593Smuzhiyun
2926*4882a593Smuzhiyun switch (cmd) {
2927*4882a593Smuzhiyun case TIOCSSERIAL:
2928*4882a593Smuzhiyun return compat_tty_tiocsserial(tty, compat_ptr(arg));
2929*4882a593Smuzhiyun case TIOCGSERIAL:
2930*4882a593Smuzhiyun return compat_tty_tiocgserial(tty, compat_ptr(arg));
2931*4882a593Smuzhiyun }
2932*4882a593Smuzhiyun if (tty->ops->compat_ioctl) {
2933*4882a593Smuzhiyun retval = tty->ops->compat_ioctl(tty, cmd, arg);
2934*4882a593Smuzhiyun if (retval != -ENOIOCTLCMD)
2935*4882a593Smuzhiyun return retval;
2936*4882a593Smuzhiyun }
2937*4882a593Smuzhiyun
2938*4882a593Smuzhiyun ld = tty_ldisc_ref_wait(tty);
2939*4882a593Smuzhiyun if (!ld)
2940*4882a593Smuzhiyun return hung_up_tty_compat_ioctl(file, cmd, arg);
2941*4882a593Smuzhiyun if (ld->ops->compat_ioctl)
2942*4882a593Smuzhiyun retval = ld->ops->compat_ioctl(tty, file, cmd, arg);
2943*4882a593Smuzhiyun if (retval == -ENOIOCTLCMD && ld->ops->ioctl)
2944*4882a593Smuzhiyun retval = ld->ops->ioctl(tty, file,
2945*4882a593Smuzhiyun (unsigned long)compat_ptr(cmd), arg);
2946*4882a593Smuzhiyun tty_ldisc_deref(ld);
2947*4882a593Smuzhiyun
2948*4882a593Smuzhiyun return retval;
2949*4882a593Smuzhiyun }
2950*4882a593Smuzhiyun #endif
2951*4882a593Smuzhiyun
this_tty(const void * t,struct file * file,unsigned fd)2952*4882a593Smuzhiyun static int this_tty(const void *t, struct file *file, unsigned fd)
2953*4882a593Smuzhiyun {
2954*4882a593Smuzhiyun if (likely(file->f_op->read_iter != tty_read))
2955*4882a593Smuzhiyun return 0;
2956*4882a593Smuzhiyun return file_tty(file) != t ? 0 : fd + 1;
2957*4882a593Smuzhiyun }
2958*4882a593Smuzhiyun
2959*4882a593Smuzhiyun /*
2960*4882a593Smuzhiyun * This implements the "Secure Attention Key" --- the idea is to
2961*4882a593Smuzhiyun * prevent trojan horses by killing all processes associated with this
2962*4882a593Smuzhiyun * tty when the user hits the "Secure Attention Key". Required for
2963*4882a593Smuzhiyun * super-paranoid applications --- see the Orange Book for more details.
2964*4882a593Smuzhiyun *
2965*4882a593Smuzhiyun * This code could be nicer; ideally it should send a HUP, wait a few
2966*4882a593Smuzhiyun * seconds, then send a INT, and then a KILL signal. But you then
2967*4882a593Smuzhiyun * have to coordinate with the init process, since all processes associated
2968*4882a593Smuzhiyun * with the current tty must be dead before the new getty is allowed
2969*4882a593Smuzhiyun * to spawn.
2970*4882a593Smuzhiyun *
2971*4882a593Smuzhiyun * Now, if it would be correct ;-/ The current code has a nasty hole -
2972*4882a593Smuzhiyun * it doesn't catch files in flight. We may send the descriptor to ourselves
2973*4882a593Smuzhiyun * via AF_UNIX socket, close it and later fetch from socket. FIXME.
2974*4882a593Smuzhiyun *
2975*4882a593Smuzhiyun * Nasty bug: do_SAK is being called in interrupt context. This can
2976*4882a593Smuzhiyun * deadlock. We punt it up to process context. AKPM - 16Mar2001
2977*4882a593Smuzhiyun */
__do_SAK(struct tty_struct * tty)2978*4882a593Smuzhiyun void __do_SAK(struct tty_struct *tty)
2979*4882a593Smuzhiyun {
2980*4882a593Smuzhiyun #ifdef TTY_SOFT_SAK
2981*4882a593Smuzhiyun tty_hangup(tty);
2982*4882a593Smuzhiyun #else
2983*4882a593Smuzhiyun struct task_struct *g, *p;
2984*4882a593Smuzhiyun struct pid *session;
2985*4882a593Smuzhiyun int i;
2986*4882a593Smuzhiyun unsigned long flags;
2987*4882a593Smuzhiyun
2988*4882a593Smuzhiyun if (!tty)
2989*4882a593Smuzhiyun return;
2990*4882a593Smuzhiyun
2991*4882a593Smuzhiyun spin_lock_irqsave(&tty->ctrl_lock, flags);
2992*4882a593Smuzhiyun session = get_pid(tty->session);
2993*4882a593Smuzhiyun spin_unlock_irqrestore(&tty->ctrl_lock, flags);
2994*4882a593Smuzhiyun
2995*4882a593Smuzhiyun tty_ldisc_flush(tty);
2996*4882a593Smuzhiyun
2997*4882a593Smuzhiyun tty_driver_flush_buffer(tty);
2998*4882a593Smuzhiyun
2999*4882a593Smuzhiyun read_lock(&tasklist_lock);
3000*4882a593Smuzhiyun /* Kill the entire session */
3001*4882a593Smuzhiyun do_each_pid_task(session, PIDTYPE_SID, p) {
3002*4882a593Smuzhiyun tty_notice(tty, "SAK: killed process %d (%s): by session\n",
3003*4882a593Smuzhiyun task_pid_nr(p), p->comm);
3004*4882a593Smuzhiyun group_send_sig_info(SIGKILL, SEND_SIG_PRIV, p, PIDTYPE_SID);
3005*4882a593Smuzhiyun } while_each_pid_task(session, PIDTYPE_SID, p);
3006*4882a593Smuzhiyun
3007*4882a593Smuzhiyun /* Now kill any processes that happen to have the tty open */
3008*4882a593Smuzhiyun do_each_thread(g, p) {
3009*4882a593Smuzhiyun if (p->signal->tty == tty) {
3010*4882a593Smuzhiyun tty_notice(tty, "SAK: killed process %d (%s): by controlling tty\n",
3011*4882a593Smuzhiyun task_pid_nr(p), p->comm);
3012*4882a593Smuzhiyun group_send_sig_info(SIGKILL, SEND_SIG_PRIV, p, PIDTYPE_SID);
3013*4882a593Smuzhiyun continue;
3014*4882a593Smuzhiyun }
3015*4882a593Smuzhiyun task_lock(p);
3016*4882a593Smuzhiyun i = iterate_fd(p->files, 0, this_tty, tty);
3017*4882a593Smuzhiyun if (i != 0) {
3018*4882a593Smuzhiyun tty_notice(tty, "SAK: killed process %d (%s): by fd#%d\n",
3019*4882a593Smuzhiyun task_pid_nr(p), p->comm, i - 1);
3020*4882a593Smuzhiyun group_send_sig_info(SIGKILL, SEND_SIG_PRIV, p, PIDTYPE_SID);
3021*4882a593Smuzhiyun }
3022*4882a593Smuzhiyun task_unlock(p);
3023*4882a593Smuzhiyun } while_each_thread(g, p);
3024*4882a593Smuzhiyun read_unlock(&tasklist_lock);
3025*4882a593Smuzhiyun put_pid(session);
3026*4882a593Smuzhiyun #endif
3027*4882a593Smuzhiyun }
3028*4882a593Smuzhiyun
do_SAK_work(struct work_struct * work)3029*4882a593Smuzhiyun static void do_SAK_work(struct work_struct *work)
3030*4882a593Smuzhiyun {
3031*4882a593Smuzhiyun struct tty_struct *tty =
3032*4882a593Smuzhiyun container_of(work, struct tty_struct, SAK_work);
3033*4882a593Smuzhiyun __do_SAK(tty);
3034*4882a593Smuzhiyun }
3035*4882a593Smuzhiyun
3036*4882a593Smuzhiyun /*
3037*4882a593Smuzhiyun * The tq handling here is a little racy - tty->SAK_work may already be queued.
3038*4882a593Smuzhiyun * Fortunately we don't need to worry, because if ->SAK_work is already queued,
3039*4882a593Smuzhiyun * the values which we write to it will be identical to the values which it
3040*4882a593Smuzhiyun * already has. --akpm
3041*4882a593Smuzhiyun */
do_SAK(struct tty_struct * tty)3042*4882a593Smuzhiyun void do_SAK(struct tty_struct *tty)
3043*4882a593Smuzhiyun {
3044*4882a593Smuzhiyun if (!tty)
3045*4882a593Smuzhiyun return;
3046*4882a593Smuzhiyun schedule_work(&tty->SAK_work);
3047*4882a593Smuzhiyun }
3048*4882a593Smuzhiyun
3049*4882a593Smuzhiyun EXPORT_SYMBOL(do_SAK);
3050*4882a593Smuzhiyun
3051*4882a593Smuzhiyun /* Must put_device() after it's unused! */
tty_get_device(struct tty_struct * tty)3052*4882a593Smuzhiyun static struct device *tty_get_device(struct tty_struct *tty)
3053*4882a593Smuzhiyun {
3054*4882a593Smuzhiyun dev_t devt = tty_devnum(tty);
3055*4882a593Smuzhiyun return class_find_device_by_devt(tty_class, devt);
3056*4882a593Smuzhiyun }
3057*4882a593Smuzhiyun
3058*4882a593Smuzhiyun
3059*4882a593Smuzhiyun /**
3060*4882a593Smuzhiyun * alloc_tty_struct
3061*4882a593Smuzhiyun *
3062*4882a593Smuzhiyun * This subroutine allocates and initializes a tty structure.
3063*4882a593Smuzhiyun *
3064*4882a593Smuzhiyun * Locking: none - tty in question is not exposed at this point
3065*4882a593Smuzhiyun */
3066*4882a593Smuzhiyun
alloc_tty_struct(struct tty_driver * driver,int idx)3067*4882a593Smuzhiyun struct tty_struct *alloc_tty_struct(struct tty_driver *driver, int idx)
3068*4882a593Smuzhiyun {
3069*4882a593Smuzhiyun struct tty_struct *tty;
3070*4882a593Smuzhiyun
3071*4882a593Smuzhiyun tty = kzalloc(sizeof(*tty), GFP_KERNEL);
3072*4882a593Smuzhiyun if (!tty)
3073*4882a593Smuzhiyun return NULL;
3074*4882a593Smuzhiyun
3075*4882a593Smuzhiyun kref_init(&tty->kref);
3076*4882a593Smuzhiyun tty->magic = TTY_MAGIC;
3077*4882a593Smuzhiyun if (tty_ldisc_init(tty)) {
3078*4882a593Smuzhiyun kfree(tty);
3079*4882a593Smuzhiyun return NULL;
3080*4882a593Smuzhiyun }
3081*4882a593Smuzhiyun tty->session = NULL;
3082*4882a593Smuzhiyun tty->pgrp = NULL;
3083*4882a593Smuzhiyun mutex_init(&tty->legacy_mutex);
3084*4882a593Smuzhiyun mutex_init(&tty->throttle_mutex);
3085*4882a593Smuzhiyun init_rwsem(&tty->termios_rwsem);
3086*4882a593Smuzhiyun mutex_init(&tty->winsize_mutex);
3087*4882a593Smuzhiyun init_ldsem(&tty->ldisc_sem);
3088*4882a593Smuzhiyun init_waitqueue_head(&tty->write_wait);
3089*4882a593Smuzhiyun init_waitqueue_head(&tty->read_wait);
3090*4882a593Smuzhiyun INIT_WORK(&tty->hangup_work, do_tty_hangup);
3091*4882a593Smuzhiyun mutex_init(&tty->atomic_write_lock);
3092*4882a593Smuzhiyun spin_lock_init(&tty->ctrl_lock);
3093*4882a593Smuzhiyun spin_lock_init(&tty->flow_lock);
3094*4882a593Smuzhiyun spin_lock_init(&tty->files_lock);
3095*4882a593Smuzhiyun INIT_LIST_HEAD(&tty->tty_files);
3096*4882a593Smuzhiyun INIT_WORK(&tty->SAK_work, do_SAK_work);
3097*4882a593Smuzhiyun
3098*4882a593Smuzhiyun tty->driver = driver;
3099*4882a593Smuzhiyun tty->ops = driver->ops;
3100*4882a593Smuzhiyun tty->index = idx;
3101*4882a593Smuzhiyun tty_line_name(driver, idx, tty->name);
3102*4882a593Smuzhiyun tty->dev = tty_get_device(tty);
3103*4882a593Smuzhiyun
3104*4882a593Smuzhiyun return tty;
3105*4882a593Smuzhiyun }
3106*4882a593Smuzhiyun
3107*4882a593Smuzhiyun /**
3108*4882a593Smuzhiyun * tty_put_char - write one character to a tty
3109*4882a593Smuzhiyun * @tty: tty
3110*4882a593Smuzhiyun * @ch: character
3111*4882a593Smuzhiyun *
3112*4882a593Smuzhiyun * Write one byte to the tty using the provided put_char method
3113*4882a593Smuzhiyun * if present. Returns the number of characters successfully output.
3114*4882a593Smuzhiyun *
3115*4882a593Smuzhiyun * Note: the specific put_char operation in the driver layer may go
3116*4882a593Smuzhiyun * away soon. Don't call it directly, use this method
3117*4882a593Smuzhiyun */
3118*4882a593Smuzhiyun
tty_put_char(struct tty_struct * tty,unsigned char ch)3119*4882a593Smuzhiyun int tty_put_char(struct tty_struct *tty, unsigned char ch)
3120*4882a593Smuzhiyun {
3121*4882a593Smuzhiyun if (tty->ops->put_char)
3122*4882a593Smuzhiyun return tty->ops->put_char(tty, ch);
3123*4882a593Smuzhiyun return tty->ops->write(tty, &ch, 1);
3124*4882a593Smuzhiyun }
3125*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(tty_put_char);
3126*4882a593Smuzhiyun
3127*4882a593Smuzhiyun struct class *tty_class;
3128*4882a593Smuzhiyun
tty_cdev_add(struct tty_driver * driver,dev_t dev,unsigned int index,unsigned int count)3129*4882a593Smuzhiyun static int tty_cdev_add(struct tty_driver *driver, dev_t dev,
3130*4882a593Smuzhiyun unsigned int index, unsigned int count)
3131*4882a593Smuzhiyun {
3132*4882a593Smuzhiyun int err;
3133*4882a593Smuzhiyun
3134*4882a593Smuzhiyun /* init here, since reused cdevs cause crashes */
3135*4882a593Smuzhiyun driver->cdevs[index] = cdev_alloc();
3136*4882a593Smuzhiyun if (!driver->cdevs[index])
3137*4882a593Smuzhiyun return -ENOMEM;
3138*4882a593Smuzhiyun driver->cdevs[index]->ops = &tty_fops;
3139*4882a593Smuzhiyun driver->cdevs[index]->owner = driver->owner;
3140*4882a593Smuzhiyun err = cdev_add(driver->cdevs[index], dev, count);
3141*4882a593Smuzhiyun if (err)
3142*4882a593Smuzhiyun kobject_put(&driver->cdevs[index]->kobj);
3143*4882a593Smuzhiyun return err;
3144*4882a593Smuzhiyun }
3145*4882a593Smuzhiyun
3146*4882a593Smuzhiyun /**
3147*4882a593Smuzhiyun * tty_register_device - register a tty device
3148*4882a593Smuzhiyun * @driver: the tty driver that describes the tty device
3149*4882a593Smuzhiyun * @index: the index in the tty driver for this tty device
3150*4882a593Smuzhiyun * @device: a struct device that is associated with this tty device.
3151*4882a593Smuzhiyun * This field is optional, if there is no known struct device
3152*4882a593Smuzhiyun * for this tty device it can be set to NULL safely.
3153*4882a593Smuzhiyun *
3154*4882a593Smuzhiyun * Returns a pointer to the struct device for this tty device
3155*4882a593Smuzhiyun * (or ERR_PTR(-EFOO) on error).
3156*4882a593Smuzhiyun *
3157*4882a593Smuzhiyun * This call is required to be made to register an individual tty device
3158*4882a593Smuzhiyun * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If
3159*4882a593Smuzhiyun * that bit is not set, this function should not be called by a tty
3160*4882a593Smuzhiyun * driver.
3161*4882a593Smuzhiyun *
3162*4882a593Smuzhiyun * Locking: ??
3163*4882a593Smuzhiyun */
3164*4882a593Smuzhiyun
tty_register_device(struct tty_driver * driver,unsigned index,struct device * device)3165*4882a593Smuzhiyun struct device *tty_register_device(struct tty_driver *driver, unsigned index,
3166*4882a593Smuzhiyun struct device *device)
3167*4882a593Smuzhiyun {
3168*4882a593Smuzhiyun return tty_register_device_attr(driver, index, device, NULL, NULL);
3169*4882a593Smuzhiyun }
3170*4882a593Smuzhiyun EXPORT_SYMBOL(tty_register_device);
3171*4882a593Smuzhiyun
tty_device_create_release(struct device * dev)3172*4882a593Smuzhiyun static void tty_device_create_release(struct device *dev)
3173*4882a593Smuzhiyun {
3174*4882a593Smuzhiyun dev_dbg(dev, "releasing...\n");
3175*4882a593Smuzhiyun kfree(dev);
3176*4882a593Smuzhiyun }
3177*4882a593Smuzhiyun
3178*4882a593Smuzhiyun /**
3179*4882a593Smuzhiyun * tty_register_device_attr - register a tty device
3180*4882a593Smuzhiyun * @driver: the tty driver that describes the tty device
3181*4882a593Smuzhiyun * @index: the index in the tty driver for this tty device
3182*4882a593Smuzhiyun * @device: a struct device that is associated with this tty device.
3183*4882a593Smuzhiyun * This field is optional, if there is no known struct device
3184*4882a593Smuzhiyun * for this tty device it can be set to NULL safely.
3185*4882a593Smuzhiyun * @drvdata: Driver data to be set to device.
3186*4882a593Smuzhiyun * @attr_grp: Attribute group to be set on device.
3187*4882a593Smuzhiyun *
3188*4882a593Smuzhiyun * Returns a pointer to the struct device for this tty device
3189*4882a593Smuzhiyun * (or ERR_PTR(-EFOO) on error).
3190*4882a593Smuzhiyun *
3191*4882a593Smuzhiyun * This call is required to be made to register an individual tty device
3192*4882a593Smuzhiyun * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If
3193*4882a593Smuzhiyun * that bit is not set, this function should not be called by a tty
3194*4882a593Smuzhiyun * driver.
3195*4882a593Smuzhiyun *
3196*4882a593Smuzhiyun * Locking: ??
3197*4882a593Smuzhiyun */
tty_register_device_attr(struct tty_driver * driver,unsigned index,struct device * device,void * drvdata,const struct attribute_group ** attr_grp)3198*4882a593Smuzhiyun struct device *tty_register_device_attr(struct tty_driver *driver,
3199*4882a593Smuzhiyun unsigned index, struct device *device,
3200*4882a593Smuzhiyun void *drvdata,
3201*4882a593Smuzhiyun const struct attribute_group **attr_grp)
3202*4882a593Smuzhiyun {
3203*4882a593Smuzhiyun char name[64];
3204*4882a593Smuzhiyun dev_t devt = MKDEV(driver->major, driver->minor_start) + index;
3205*4882a593Smuzhiyun struct ktermios *tp;
3206*4882a593Smuzhiyun struct device *dev;
3207*4882a593Smuzhiyun int retval;
3208*4882a593Smuzhiyun
3209*4882a593Smuzhiyun if (index >= driver->num) {
3210*4882a593Smuzhiyun pr_err("%s: Attempt to register invalid tty line number (%d)\n",
3211*4882a593Smuzhiyun driver->name, index);
3212*4882a593Smuzhiyun return ERR_PTR(-EINVAL);
3213*4882a593Smuzhiyun }
3214*4882a593Smuzhiyun
3215*4882a593Smuzhiyun if (driver->type == TTY_DRIVER_TYPE_PTY)
3216*4882a593Smuzhiyun pty_line_name(driver, index, name);
3217*4882a593Smuzhiyun else
3218*4882a593Smuzhiyun tty_line_name(driver, index, name);
3219*4882a593Smuzhiyun
3220*4882a593Smuzhiyun dev = kzalloc(sizeof(*dev), GFP_KERNEL);
3221*4882a593Smuzhiyun if (!dev)
3222*4882a593Smuzhiyun return ERR_PTR(-ENOMEM);
3223*4882a593Smuzhiyun
3224*4882a593Smuzhiyun dev->devt = devt;
3225*4882a593Smuzhiyun dev->class = tty_class;
3226*4882a593Smuzhiyun dev->parent = device;
3227*4882a593Smuzhiyun dev->release = tty_device_create_release;
3228*4882a593Smuzhiyun dev_set_name(dev, "%s", name);
3229*4882a593Smuzhiyun dev->groups = attr_grp;
3230*4882a593Smuzhiyun dev_set_drvdata(dev, drvdata);
3231*4882a593Smuzhiyun
3232*4882a593Smuzhiyun dev_set_uevent_suppress(dev, 1);
3233*4882a593Smuzhiyun
3234*4882a593Smuzhiyun retval = device_register(dev);
3235*4882a593Smuzhiyun if (retval)
3236*4882a593Smuzhiyun goto err_put;
3237*4882a593Smuzhiyun
3238*4882a593Smuzhiyun if (!(driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)) {
3239*4882a593Smuzhiyun /*
3240*4882a593Smuzhiyun * Free any saved termios data so that the termios state is
3241*4882a593Smuzhiyun * reset when reusing a minor number.
3242*4882a593Smuzhiyun */
3243*4882a593Smuzhiyun tp = driver->termios[index];
3244*4882a593Smuzhiyun if (tp) {
3245*4882a593Smuzhiyun driver->termios[index] = NULL;
3246*4882a593Smuzhiyun kfree(tp);
3247*4882a593Smuzhiyun }
3248*4882a593Smuzhiyun
3249*4882a593Smuzhiyun retval = tty_cdev_add(driver, devt, index, 1);
3250*4882a593Smuzhiyun if (retval)
3251*4882a593Smuzhiyun goto err_del;
3252*4882a593Smuzhiyun }
3253*4882a593Smuzhiyun
3254*4882a593Smuzhiyun dev_set_uevent_suppress(dev, 0);
3255*4882a593Smuzhiyun kobject_uevent(&dev->kobj, KOBJ_ADD);
3256*4882a593Smuzhiyun
3257*4882a593Smuzhiyun return dev;
3258*4882a593Smuzhiyun
3259*4882a593Smuzhiyun err_del:
3260*4882a593Smuzhiyun device_del(dev);
3261*4882a593Smuzhiyun err_put:
3262*4882a593Smuzhiyun put_device(dev);
3263*4882a593Smuzhiyun
3264*4882a593Smuzhiyun return ERR_PTR(retval);
3265*4882a593Smuzhiyun }
3266*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(tty_register_device_attr);
3267*4882a593Smuzhiyun
3268*4882a593Smuzhiyun /**
3269*4882a593Smuzhiyun * tty_unregister_device - unregister a tty device
3270*4882a593Smuzhiyun * @driver: the tty driver that describes the tty device
3271*4882a593Smuzhiyun * @index: the index in the tty driver for this tty device
3272*4882a593Smuzhiyun *
3273*4882a593Smuzhiyun * If a tty device is registered with a call to tty_register_device() then
3274*4882a593Smuzhiyun * this function must be called when the tty device is gone.
3275*4882a593Smuzhiyun *
3276*4882a593Smuzhiyun * Locking: ??
3277*4882a593Smuzhiyun */
3278*4882a593Smuzhiyun
tty_unregister_device(struct tty_driver * driver,unsigned index)3279*4882a593Smuzhiyun void tty_unregister_device(struct tty_driver *driver, unsigned index)
3280*4882a593Smuzhiyun {
3281*4882a593Smuzhiyun device_destroy(tty_class,
3282*4882a593Smuzhiyun MKDEV(driver->major, driver->minor_start) + index);
3283*4882a593Smuzhiyun if (!(driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)) {
3284*4882a593Smuzhiyun cdev_del(driver->cdevs[index]);
3285*4882a593Smuzhiyun driver->cdevs[index] = NULL;
3286*4882a593Smuzhiyun }
3287*4882a593Smuzhiyun }
3288*4882a593Smuzhiyun EXPORT_SYMBOL(tty_unregister_device);
3289*4882a593Smuzhiyun
3290*4882a593Smuzhiyun /**
3291*4882a593Smuzhiyun * __tty_alloc_driver -- allocate tty driver
3292*4882a593Smuzhiyun * @lines: count of lines this driver can handle at most
3293*4882a593Smuzhiyun * @owner: module which is responsible for this driver
3294*4882a593Smuzhiyun * @flags: some of TTY_DRIVER_* flags, will be set in driver->flags
3295*4882a593Smuzhiyun *
3296*4882a593Smuzhiyun * This should not be called directly, some of the provided macros should be
3297*4882a593Smuzhiyun * used instead. Use IS_ERR and friends on @retval.
3298*4882a593Smuzhiyun */
__tty_alloc_driver(unsigned int lines,struct module * owner,unsigned long flags)3299*4882a593Smuzhiyun struct tty_driver *__tty_alloc_driver(unsigned int lines, struct module *owner,
3300*4882a593Smuzhiyun unsigned long flags)
3301*4882a593Smuzhiyun {
3302*4882a593Smuzhiyun struct tty_driver *driver;
3303*4882a593Smuzhiyun unsigned int cdevs = 1;
3304*4882a593Smuzhiyun int err;
3305*4882a593Smuzhiyun
3306*4882a593Smuzhiyun if (!lines || (flags & TTY_DRIVER_UNNUMBERED_NODE && lines > 1))
3307*4882a593Smuzhiyun return ERR_PTR(-EINVAL);
3308*4882a593Smuzhiyun
3309*4882a593Smuzhiyun driver = kzalloc(sizeof(*driver), GFP_KERNEL);
3310*4882a593Smuzhiyun if (!driver)
3311*4882a593Smuzhiyun return ERR_PTR(-ENOMEM);
3312*4882a593Smuzhiyun
3313*4882a593Smuzhiyun kref_init(&driver->kref);
3314*4882a593Smuzhiyun driver->magic = TTY_DRIVER_MAGIC;
3315*4882a593Smuzhiyun driver->num = lines;
3316*4882a593Smuzhiyun driver->owner = owner;
3317*4882a593Smuzhiyun driver->flags = flags;
3318*4882a593Smuzhiyun
3319*4882a593Smuzhiyun if (!(flags & TTY_DRIVER_DEVPTS_MEM)) {
3320*4882a593Smuzhiyun driver->ttys = kcalloc(lines, sizeof(*driver->ttys),
3321*4882a593Smuzhiyun GFP_KERNEL);
3322*4882a593Smuzhiyun driver->termios = kcalloc(lines, sizeof(*driver->termios),
3323*4882a593Smuzhiyun GFP_KERNEL);
3324*4882a593Smuzhiyun if (!driver->ttys || !driver->termios) {
3325*4882a593Smuzhiyun err = -ENOMEM;
3326*4882a593Smuzhiyun goto err_free_all;
3327*4882a593Smuzhiyun }
3328*4882a593Smuzhiyun }
3329*4882a593Smuzhiyun
3330*4882a593Smuzhiyun if (!(flags & TTY_DRIVER_DYNAMIC_ALLOC)) {
3331*4882a593Smuzhiyun driver->ports = kcalloc(lines, sizeof(*driver->ports),
3332*4882a593Smuzhiyun GFP_KERNEL);
3333*4882a593Smuzhiyun if (!driver->ports) {
3334*4882a593Smuzhiyun err = -ENOMEM;
3335*4882a593Smuzhiyun goto err_free_all;
3336*4882a593Smuzhiyun }
3337*4882a593Smuzhiyun cdevs = lines;
3338*4882a593Smuzhiyun }
3339*4882a593Smuzhiyun
3340*4882a593Smuzhiyun driver->cdevs = kcalloc(cdevs, sizeof(*driver->cdevs), GFP_KERNEL);
3341*4882a593Smuzhiyun if (!driver->cdevs) {
3342*4882a593Smuzhiyun err = -ENOMEM;
3343*4882a593Smuzhiyun goto err_free_all;
3344*4882a593Smuzhiyun }
3345*4882a593Smuzhiyun
3346*4882a593Smuzhiyun return driver;
3347*4882a593Smuzhiyun err_free_all:
3348*4882a593Smuzhiyun kfree(driver->ports);
3349*4882a593Smuzhiyun kfree(driver->ttys);
3350*4882a593Smuzhiyun kfree(driver->termios);
3351*4882a593Smuzhiyun kfree(driver->cdevs);
3352*4882a593Smuzhiyun kfree(driver);
3353*4882a593Smuzhiyun return ERR_PTR(err);
3354*4882a593Smuzhiyun }
3355*4882a593Smuzhiyun EXPORT_SYMBOL(__tty_alloc_driver);
3356*4882a593Smuzhiyun
destruct_tty_driver(struct kref * kref)3357*4882a593Smuzhiyun static void destruct_tty_driver(struct kref *kref)
3358*4882a593Smuzhiyun {
3359*4882a593Smuzhiyun struct tty_driver *driver = container_of(kref, struct tty_driver, kref);
3360*4882a593Smuzhiyun int i;
3361*4882a593Smuzhiyun struct ktermios *tp;
3362*4882a593Smuzhiyun
3363*4882a593Smuzhiyun if (driver->flags & TTY_DRIVER_INSTALLED) {
3364*4882a593Smuzhiyun for (i = 0; i < driver->num; i++) {
3365*4882a593Smuzhiyun tp = driver->termios[i];
3366*4882a593Smuzhiyun if (tp) {
3367*4882a593Smuzhiyun driver->termios[i] = NULL;
3368*4882a593Smuzhiyun kfree(tp);
3369*4882a593Smuzhiyun }
3370*4882a593Smuzhiyun if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV))
3371*4882a593Smuzhiyun tty_unregister_device(driver, i);
3372*4882a593Smuzhiyun }
3373*4882a593Smuzhiyun proc_tty_unregister_driver(driver);
3374*4882a593Smuzhiyun if (driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)
3375*4882a593Smuzhiyun cdev_del(driver->cdevs[0]);
3376*4882a593Smuzhiyun }
3377*4882a593Smuzhiyun kfree(driver->cdevs);
3378*4882a593Smuzhiyun kfree(driver->ports);
3379*4882a593Smuzhiyun kfree(driver->termios);
3380*4882a593Smuzhiyun kfree(driver->ttys);
3381*4882a593Smuzhiyun kfree(driver);
3382*4882a593Smuzhiyun }
3383*4882a593Smuzhiyun
tty_driver_kref_put(struct tty_driver * driver)3384*4882a593Smuzhiyun void tty_driver_kref_put(struct tty_driver *driver)
3385*4882a593Smuzhiyun {
3386*4882a593Smuzhiyun kref_put(&driver->kref, destruct_tty_driver);
3387*4882a593Smuzhiyun }
3388*4882a593Smuzhiyun EXPORT_SYMBOL(tty_driver_kref_put);
3389*4882a593Smuzhiyun
tty_set_operations(struct tty_driver * driver,const struct tty_operations * op)3390*4882a593Smuzhiyun void tty_set_operations(struct tty_driver *driver,
3391*4882a593Smuzhiyun const struct tty_operations *op)
3392*4882a593Smuzhiyun {
3393*4882a593Smuzhiyun driver->ops = op;
3394*4882a593Smuzhiyun };
3395*4882a593Smuzhiyun EXPORT_SYMBOL(tty_set_operations);
3396*4882a593Smuzhiyun
put_tty_driver(struct tty_driver * d)3397*4882a593Smuzhiyun void put_tty_driver(struct tty_driver *d)
3398*4882a593Smuzhiyun {
3399*4882a593Smuzhiyun tty_driver_kref_put(d);
3400*4882a593Smuzhiyun }
3401*4882a593Smuzhiyun EXPORT_SYMBOL(put_tty_driver);
3402*4882a593Smuzhiyun
3403*4882a593Smuzhiyun /*
3404*4882a593Smuzhiyun * Called by a tty driver to register itself.
3405*4882a593Smuzhiyun */
tty_register_driver(struct tty_driver * driver)3406*4882a593Smuzhiyun int tty_register_driver(struct tty_driver *driver)
3407*4882a593Smuzhiyun {
3408*4882a593Smuzhiyun int error;
3409*4882a593Smuzhiyun int i;
3410*4882a593Smuzhiyun dev_t dev;
3411*4882a593Smuzhiyun struct device *d;
3412*4882a593Smuzhiyun
3413*4882a593Smuzhiyun if (!driver->major) {
3414*4882a593Smuzhiyun error = alloc_chrdev_region(&dev, driver->minor_start,
3415*4882a593Smuzhiyun driver->num, driver->name);
3416*4882a593Smuzhiyun if (!error) {
3417*4882a593Smuzhiyun driver->major = MAJOR(dev);
3418*4882a593Smuzhiyun driver->minor_start = MINOR(dev);
3419*4882a593Smuzhiyun }
3420*4882a593Smuzhiyun } else {
3421*4882a593Smuzhiyun dev = MKDEV(driver->major, driver->minor_start);
3422*4882a593Smuzhiyun error = register_chrdev_region(dev, driver->num, driver->name);
3423*4882a593Smuzhiyun }
3424*4882a593Smuzhiyun if (error < 0)
3425*4882a593Smuzhiyun goto err;
3426*4882a593Smuzhiyun
3427*4882a593Smuzhiyun if (driver->flags & TTY_DRIVER_DYNAMIC_ALLOC) {
3428*4882a593Smuzhiyun error = tty_cdev_add(driver, dev, 0, driver->num);
3429*4882a593Smuzhiyun if (error)
3430*4882a593Smuzhiyun goto err_unreg_char;
3431*4882a593Smuzhiyun }
3432*4882a593Smuzhiyun
3433*4882a593Smuzhiyun mutex_lock(&tty_mutex);
3434*4882a593Smuzhiyun list_add(&driver->tty_drivers, &tty_drivers);
3435*4882a593Smuzhiyun mutex_unlock(&tty_mutex);
3436*4882a593Smuzhiyun
3437*4882a593Smuzhiyun if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) {
3438*4882a593Smuzhiyun for (i = 0; i < driver->num; i++) {
3439*4882a593Smuzhiyun d = tty_register_device(driver, i, NULL);
3440*4882a593Smuzhiyun if (IS_ERR(d)) {
3441*4882a593Smuzhiyun error = PTR_ERR(d);
3442*4882a593Smuzhiyun goto err_unreg_devs;
3443*4882a593Smuzhiyun }
3444*4882a593Smuzhiyun }
3445*4882a593Smuzhiyun }
3446*4882a593Smuzhiyun proc_tty_register_driver(driver);
3447*4882a593Smuzhiyun driver->flags |= TTY_DRIVER_INSTALLED;
3448*4882a593Smuzhiyun return 0;
3449*4882a593Smuzhiyun
3450*4882a593Smuzhiyun err_unreg_devs:
3451*4882a593Smuzhiyun for (i--; i >= 0; i--)
3452*4882a593Smuzhiyun tty_unregister_device(driver, i);
3453*4882a593Smuzhiyun
3454*4882a593Smuzhiyun mutex_lock(&tty_mutex);
3455*4882a593Smuzhiyun list_del(&driver->tty_drivers);
3456*4882a593Smuzhiyun mutex_unlock(&tty_mutex);
3457*4882a593Smuzhiyun
3458*4882a593Smuzhiyun err_unreg_char:
3459*4882a593Smuzhiyun unregister_chrdev_region(dev, driver->num);
3460*4882a593Smuzhiyun err:
3461*4882a593Smuzhiyun return error;
3462*4882a593Smuzhiyun }
3463*4882a593Smuzhiyun EXPORT_SYMBOL(tty_register_driver);
3464*4882a593Smuzhiyun
3465*4882a593Smuzhiyun /*
3466*4882a593Smuzhiyun * Called by a tty driver to unregister itself.
3467*4882a593Smuzhiyun */
tty_unregister_driver(struct tty_driver * driver)3468*4882a593Smuzhiyun int tty_unregister_driver(struct tty_driver *driver)
3469*4882a593Smuzhiyun {
3470*4882a593Smuzhiyun #if 0
3471*4882a593Smuzhiyun /* FIXME */
3472*4882a593Smuzhiyun if (driver->refcount)
3473*4882a593Smuzhiyun return -EBUSY;
3474*4882a593Smuzhiyun #endif
3475*4882a593Smuzhiyun unregister_chrdev_region(MKDEV(driver->major, driver->minor_start),
3476*4882a593Smuzhiyun driver->num);
3477*4882a593Smuzhiyun mutex_lock(&tty_mutex);
3478*4882a593Smuzhiyun list_del(&driver->tty_drivers);
3479*4882a593Smuzhiyun mutex_unlock(&tty_mutex);
3480*4882a593Smuzhiyun return 0;
3481*4882a593Smuzhiyun }
3482*4882a593Smuzhiyun
3483*4882a593Smuzhiyun EXPORT_SYMBOL(tty_unregister_driver);
3484*4882a593Smuzhiyun
tty_devnum(struct tty_struct * tty)3485*4882a593Smuzhiyun dev_t tty_devnum(struct tty_struct *tty)
3486*4882a593Smuzhiyun {
3487*4882a593Smuzhiyun return MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index;
3488*4882a593Smuzhiyun }
3489*4882a593Smuzhiyun EXPORT_SYMBOL(tty_devnum);
3490*4882a593Smuzhiyun
tty_default_fops(struct file_operations * fops)3491*4882a593Smuzhiyun void tty_default_fops(struct file_operations *fops)
3492*4882a593Smuzhiyun {
3493*4882a593Smuzhiyun *fops = tty_fops;
3494*4882a593Smuzhiyun }
3495*4882a593Smuzhiyun
tty_devnode(struct device * dev,umode_t * mode)3496*4882a593Smuzhiyun static char *tty_devnode(struct device *dev, umode_t *mode)
3497*4882a593Smuzhiyun {
3498*4882a593Smuzhiyun if (!mode)
3499*4882a593Smuzhiyun return NULL;
3500*4882a593Smuzhiyun if (dev->devt == MKDEV(TTYAUX_MAJOR, 0) ||
3501*4882a593Smuzhiyun dev->devt == MKDEV(TTYAUX_MAJOR, 2))
3502*4882a593Smuzhiyun *mode = 0666;
3503*4882a593Smuzhiyun return NULL;
3504*4882a593Smuzhiyun }
3505*4882a593Smuzhiyun
tty_class_init(void)3506*4882a593Smuzhiyun static int __init tty_class_init(void)
3507*4882a593Smuzhiyun {
3508*4882a593Smuzhiyun tty_class = class_create(THIS_MODULE, "tty");
3509*4882a593Smuzhiyun if (IS_ERR(tty_class))
3510*4882a593Smuzhiyun return PTR_ERR(tty_class);
3511*4882a593Smuzhiyun tty_class->devnode = tty_devnode;
3512*4882a593Smuzhiyun return 0;
3513*4882a593Smuzhiyun }
3514*4882a593Smuzhiyun
3515*4882a593Smuzhiyun postcore_initcall(tty_class_init);
3516*4882a593Smuzhiyun
3517*4882a593Smuzhiyun /* 3/2004 jmc: why do these devices exist? */
3518*4882a593Smuzhiyun static struct cdev tty_cdev, console_cdev;
3519*4882a593Smuzhiyun
show_cons_active(struct device * dev,struct device_attribute * attr,char * buf)3520*4882a593Smuzhiyun static ssize_t show_cons_active(struct device *dev,
3521*4882a593Smuzhiyun struct device_attribute *attr, char *buf)
3522*4882a593Smuzhiyun {
3523*4882a593Smuzhiyun struct console *cs[16];
3524*4882a593Smuzhiyun int i = 0;
3525*4882a593Smuzhiyun struct console *c;
3526*4882a593Smuzhiyun ssize_t count = 0;
3527*4882a593Smuzhiyun
3528*4882a593Smuzhiyun console_lock();
3529*4882a593Smuzhiyun for_each_console(c) {
3530*4882a593Smuzhiyun if (!c->device)
3531*4882a593Smuzhiyun continue;
3532*4882a593Smuzhiyun if (!c->write)
3533*4882a593Smuzhiyun continue;
3534*4882a593Smuzhiyun if ((c->flags & CON_ENABLED) == 0)
3535*4882a593Smuzhiyun continue;
3536*4882a593Smuzhiyun cs[i++] = c;
3537*4882a593Smuzhiyun if (i >= ARRAY_SIZE(cs))
3538*4882a593Smuzhiyun break;
3539*4882a593Smuzhiyun }
3540*4882a593Smuzhiyun while (i--) {
3541*4882a593Smuzhiyun int index = cs[i]->index;
3542*4882a593Smuzhiyun struct tty_driver *drv = cs[i]->device(cs[i], &index);
3543*4882a593Smuzhiyun
3544*4882a593Smuzhiyun /* don't resolve tty0 as some programs depend on it */
3545*4882a593Smuzhiyun if (drv && (cs[i]->index > 0 || drv->major != TTY_MAJOR))
3546*4882a593Smuzhiyun count += tty_line_name(drv, index, buf + count);
3547*4882a593Smuzhiyun else
3548*4882a593Smuzhiyun count += sprintf(buf + count, "%s%d",
3549*4882a593Smuzhiyun cs[i]->name, cs[i]->index);
3550*4882a593Smuzhiyun
3551*4882a593Smuzhiyun count += sprintf(buf + count, "%c", i ? ' ':'\n');
3552*4882a593Smuzhiyun }
3553*4882a593Smuzhiyun console_unlock();
3554*4882a593Smuzhiyun
3555*4882a593Smuzhiyun return count;
3556*4882a593Smuzhiyun }
3557*4882a593Smuzhiyun static DEVICE_ATTR(active, S_IRUGO, show_cons_active, NULL);
3558*4882a593Smuzhiyun
3559*4882a593Smuzhiyun static struct attribute *cons_dev_attrs[] = {
3560*4882a593Smuzhiyun &dev_attr_active.attr,
3561*4882a593Smuzhiyun NULL
3562*4882a593Smuzhiyun };
3563*4882a593Smuzhiyun
3564*4882a593Smuzhiyun ATTRIBUTE_GROUPS(cons_dev);
3565*4882a593Smuzhiyun
3566*4882a593Smuzhiyun static struct device *consdev;
3567*4882a593Smuzhiyun
console_sysfs_notify(void)3568*4882a593Smuzhiyun void console_sysfs_notify(void)
3569*4882a593Smuzhiyun {
3570*4882a593Smuzhiyun if (consdev)
3571*4882a593Smuzhiyun sysfs_notify(&consdev->kobj, NULL, "active");
3572*4882a593Smuzhiyun }
3573*4882a593Smuzhiyun
3574*4882a593Smuzhiyun /*
3575*4882a593Smuzhiyun * Ok, now we can initialize the rest of the tty devices and can count
3576*4882a593Smuzhiyun * on memory allocations, interrupts etc..
3577*4882a593Smuzhiyun */
tty_init(void)3578*4882a593Smuzhiyun int __init tty_init(void)
3579*4882a593Smuzhiyun {
3580*4882a593Smuzhiyun tty_sysctl_init();
3581*4882a593Smuzhiyun cdev_init(&tty_cdev, &tty_fops);
3582*4882a593Smuzhiyun if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) ||
3583*4882a593Smuzhiyun register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0)
3584*4882a593Smuzhiyun panic("Couldn't register /dev/tty driver\n");
3585*4882a593Smuzhiyun device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), NULL, "tty");
3586*4882a593Smuzhiyun
3587*4882a593Smuzhiyun cdev_init(&console_cdev, &console_fops);
3588*4882a593Smuzhiyun if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) ||
3589*4882a593Smuzhiyun register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0)
3590*4882a593Smuzhiyun panic("Couldn't register /dev/console driver\n");
3591*4882a593Smuzhiyun consdev = device_create_with_groups(tty_class, NULL,
3592*4882a593Smuzhiyun MKDEV(TTYAUX_MAJOR, 1), NULL,
3593*4882a593Smuzhiyun cons_dev_groups, "console");
3594*4882a593Smuzhiyun if (IS_ERR(consdev))
3595*4882a593Smuzhiyun consdev = NULL;
3596*4882a593Smuzhiyun
3597*4882a593Smuzhiyun #ifdef CONFIG_VT
3598*4882a593Smuzhiyun vty_init(&console_fops);
3599*4882a593Smuzhiyun #endif
3600*4882a593Smuzhiyun return 0;
3601*4882a593Smuzhiyun }
3602*4882a593Smuzhiyun
3603