blob: fe37ec331289b227a31e2c6b967660a2f68bf85f [file] [log] [blame]
Greg Kroah-Hartmane3b3d0f2017-11-06 18:11:51 +01001// SPDX-License-Identifier: GPL-2.0
Alan Cox01e1abb2008-07-22 11:16:55 +01002#include <linux/types.h>
Alan Cox01e1abb2008-07-22 11:16:55 +01003#include <linux/errno.h>
Jiri Slaby8b3ffa12011-11-16 16:27:10 +01004#include <linux/kmod.h>
Alan Cox01e1abb2008-07-22 11:16:55 +01005#include <linux/sched.h>
6#include <linux/interrupt.h>
7#include <linux/tty.h>
8#include <linux/tty_driver.h>
Alan Cox01e1abb2008-07-22 11:16:55 +01009#include <linux/file.h>
Alan Cox01e1abb2008-07-22 11:16:55 +010010#include <linux/mm.h>
11#include <linux/string.h>
12#include <linux/slab.h>
13#include <linux/poll.h>
14#include <linux/proc_fs.h>
Alan Cox01e1abb2008-07-22 11:16:55 +010015#include <linux/module.h>
Alan Cox01e1abb2008-07-22 11:16:55 +010016#include <linux/device.h>
17#include <linux/wait.h>
18#include <linux/bitops.h>
Alan Cox01e1abb2008-07-22 11:16:55 +010019#include <linux/seq_file.h>
Alan Cox01e1abb2008-07-22 11:16:55 +010020#include <linux/uaccess.h>
Jiri Slaby0c73c082011-11-16 16:27:09 +010021#include <linux/ratelimit.h>
Alan Cox01e1abb2008-07-22 11:16:55 +010022
Peter Hurleyfc575ee2013-03-11 16:44:38 -040023#undef LDISC_DEBUG_HANGUP
24
25#ifdef LDISC_DEBUG_HANGUP
Peter Hurley0a6adc12015-07-12 22:49:10 -040026#define tty_ldisc_debug(tty, f, args...) tty_debug(tty, f, ##args)
Peter Hurleyfc575ee2013-03-11 16:44:38 -040027#else
28#define tty_ldisc_debug(tty, f, args...)
29#endif
30
Peter Hurleyd2c43892013-06-15 07:04:47 -040031/* lockdep nested classes for tty->ldisc_sem */
32enum {
33 LDISC_SEM_NORMAL,
34 LDISC_SEM_OTHER,
35};
36
37
Alan Cox01e1abb2008-07-22 11:16:55 +010038/*
39 * This guards the refcounted line discipline lists. The lock
40 * must be taken with irqs off because there are hangup path
41 * callers who will do ldisc lookups and cannot sleep.
42 */
43
Peter Hurley137084b2013-06-15 07:04:46 -040044static DEFINE_RAW_SPINLOCK(tty_ldiscs_lock);
Alan Cox01e1abb2008-07-22 11:16:55 +010045/* Line disc dispatch table */
46static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS];
47
48/**
49 * tty_register_ldisc - install a line discipline
50 * @disc: ldisc number
51 * @new_ldisc: pointer to the ldisc object
52 *
53 * Installs a new line discipline into the kernel. The discipline
54 * is set up as unreferenced and then made available to the kernel
55 * from this point onwards.
56 *
57 * Locking:
Peter Hurley137084b2013-06-15 07:04:46 -040058 * takes tty_ldiscs_lock to guard against ldisc races
Alan Cox01e1abb2008-07-22 11:16:55 +010059 */
60
61int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc)
62{
63 unsigned long flags;
64 int ret = 0;
65
66 if (disc < N_TTY || disc >= NR_LDISCS)
67 return -EINVAL;
68
Peter Hurley137084b2013-06-15 07:04:46 -040069 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
Alan Cox01e1abb2008-07-22 11:16:55 +010070 tty_ldiscs[disc] = new_ldisc;
71 new_ldisc->num = disc;
72 new_ldisc->refcount = 0;
Peter Hurley137084b2013-06-15 07:04:46 -040073 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
Alan Cox01e1abb2008-07-22 11:16:55 +010074
75 return ret;
76}
77EXPORT_SYMBOL(tty_register_ldisc);
78
79/**
80 * tty_unregister_ldisc - unload a line discipline
81 * @disc: ldisc number
Alan Cox01e1abb2008-07-22 11:16:55 +010082 *
83 * Remove a line discipline from the kernel providing it is not
84 * currently in use.
85 *
86 * Locking:
Peter Hurley137084b2013-06-15 07:04:46 -040087 * takes tty_ldiscs_lock to guard against ldisc races
Alan Cox01e1abb2008-07-22 11:16:55 +010088 */
89
90int tty_unregister_ldisc(int disc)
91{
92 unsigned long flags;
93 int ret = 0;
94
95 if (disc < N_TTY || disc >= NR_LDISCS)
96 return -EINVAL;
97
Peter Hurley137084b2013-06-15 07:04:46 -040098 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
Alan Cox01e1abb2008-07-22 11:16:55 +010099 if (tty_ldiscs[disc]->refcount)
100 ret = -EBUSY;
101 else
102 tty_ldiscs[disc] = NULL;
Peter Hurley137084b2013-06-15 07:04:46 -0400103 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
Alan Cox01e1abb2008-07-22 11:16:55 +0100104
105 return ret;
106}
107EXPORT_SYMBOL(tty_unregister_ldisc);
108
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700109static struct tty_ldisc_ops *get_ldops(int disc)
110{
111 unsigned long flags;
112 struct tty_ldisc_ops *ldops, *ret;
113
Peter Hurley137084b2013-06-15 07:04:46 -0400114 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700115 ret = ERR_PTR(-EINVAL);
116 ldops = tty_ldiscs[disc];
117 if (ldops) {
118 ret = ERR_PTR(-EAGAIN);
119 if (try_module_get(ldops->owner)) {
120 ldops->refcount++;
121 ret = ldops;
122 }
123 }
Peter Hurley137084b2013-06-15 07:04:46 -0400124 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700125 return ret;
126}
127
128static void put_ldops(struct tty_ldisc_ops *ldops)
129{
130 unsigned long flags;
131
Peter Hurley137084b2013-06-15 07:04:46 -0400132 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700133 ldops->refcount--;
134 module_put(ldops->owner);
Peter Hurley137084b2013-06-15 07:04:46 -0400135 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700136}
Alan Cox01e1abb2008-07-22 11:16:55 +0100137
138/**
Alan Cox01e1abb2008-07-22 11:16:55 +0100139 * tty_ldisc_get - take a reference to an ldisc
140 * @disc: ldisc number
Alan Cox01e1abb2008-07-22 11:16:55 +0100141 *
142 * Takes a reference to a line discipline. Deals with refcounts and
Peter Hurleyc0cc1c52016-01-10 22:40:59 -0800143 * module locking counts.
144 *
145 * Returns: -EINVAL if the discipline index is not [N_TTY..NR_LDISCS] or
146 * if the discipline is not registered
147 * -EAGAIN if request_module() failed to load or register the
148 * the discipline
149 * -ENOMEM if allocation failure
150 *
151 * Otherwise, returns a pointer to the discipline and bumps the
152 * ref count
Alan Cox01e1abb2008-07-22 11:16:55 +0100153 *
154 * Locking:
Peter Hurley137084b2013-06-15 07:04:46 -0400155 * takes tty_ldiscs_lock to guard against ldisc races
Alan Cox01e1abb2008-07-22 11:16:55 +0100156 */
157
Pavel Machek530c4ba2019-09-24 10:32:44 +0200158static int tty_ldisc_autoload = IS_BUILTIN(CONFIG_LDISC_AUTOLOAD);
Greg Kroah-Hartman7c0cca72019-01-21 17:26:42 +0100159
Peter Hurley36697522013-06-15 07:04:48 -0400160static struct tty_ldisc *tty_ldisc_get(struct tty_struct *tty, int disc)
Alan Cox01e1abb2008-07-22 11:16:55 +0100161{
Alan Coxc65c9bc2009-06-11 12:50:12 +0100162 struct tty_ldisc *ld;
Linus Torvalds182274f2009-08-03 16:01:28 -0700163 struct tty_ldisc_ops *ldops;
Alan Cox01e1abb2008-07-22 11:16:55 +0100164
165 if (disc < N_TTY || disc >= NR_LDISCS)
Alan Coxc65c9bc2009-06-11 12:50:12 +0100166 return ERR_PTR(-EINVAL);
Linus Torvalds182274f2009-08-03 16:01:28 -0700167
168 /*
169 * Get the ldisc ops - we may need to request them to be loaded
170 * dynamically and try again.
171 */
172 ldops = get_ldops(disc);
173 if (IS_ERR(ldops)) {
Greg Kroah-Hartman7c0cca72019-01-21 17:26:42 +0100174 if (!capable(CAP_SYS_MODULE) && !tty_ldisc_autoload)
175 return ERR_PTR(-EPERM);
Alan Cox01e1abb2008-07-22 11:16:55 +0100176 request_module("tty-ldisc-%d", disc);
Linus Torvalds182274f2009-08-03 16:01:28 -0700177 ldops = get_ldops(disc);
178 if (IS_ERR(ldops))
179 return ERR_CAST(ldops);
Alan Cox01e1abb2008-07-22 11:16:55 +0100180 }
Linus Torvalds182274f2009-08-03 16:01:28 -0700181
Tetsuo Handabcdd0ca2018-04-25 20:12:31 +0900182 /*
183 * There is no way to handle allocation failure of only 16 bytes.
184 * Let's simplify error handling and save more memory.
185 */
186 ld = kmalloc(sizeof(struct tty_ldisc), GFP_KERNEL | __GFP_NOFAIL);
Linus Torvalds182274f2009-08-03 16:01:28 -0700187 ld->ops = ldops;
Peter Hurley36697522013-06-15 07:04:48 -0400188 ld->tty = tty;
Ivo Sieben1541f842012-05-03 14:37:43 +0200189
Alan Coxc65c9bc2009-06-11 12:50:12 +0100190 return ld;
Alan Cox01e1abb2008-07-22 11:16:55 +0100191}
192
Peter Hurley734de242013-03-11 16:44:43 -0400193/**
194 * tty_ldisc_put - release the ldisc
195 *
196 * Complement of tty_ldisc_get().
197 */
Denys Vlasenkocb128f62015-10-27 17:40:02 +0100198static void tty_ldisc_put(struct tty_ldisc *ld)
Peter Hurley734de242013-03-11 16:44:43 -0400199{
Peter Hurley734de242013-03-11 16:44:43 -0400200 if (WARN_ON_ONCE(!ld))
201 return;
202
Peter Hurley36697522013-06-15 07:04:48 -0400203 put_ldops(ld->ops);
Peter Hurley734de242013-03-11 16:44:43 -0400204 kfree(ld);
Peter Hurley734de242013-03-11 16:44:43 -0400205}
206
Alan Cox852e99d2009-06-11 12:51:41 +0100207static void *tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos)
Alan Cox01e1abb2008-07-22 11:16:55 +0100208{
209 return (*pos < NR_LDISCS) ? pos : NULL;
210}
211
Alan Cox852e99d2009-06-11 12:51:41 +0100212static void *tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos)
Alan Cox01e1abb2008-07-22 11:16:55 +0100213{
214 (*pos)++;
215 return (*pos < NR_LDISCS) ? pos : NULL;
216}
217
218static void tty_ldiscs_seq_stop(struct seq_file *m, void *v)
219{
220}
221
222static int tty_ldiscs_seq_show(struct seq_file *m, void *v)
223{
224 int i = *(loff_t *)v;
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700225 struct tty_ldisc_ops *ldops;
Alan Cox852e99d2009-06-11 12:51:41 +0100226
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700227 ldops = get_ldops(i);
228 if (IS_ERR(ldops))
Alan Cox01e1abb2008-07-22 11:16:55 +0100229 return 0;
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700230 seq_printf(m, "%-10s %2d\n", ldops->name ? ldops->name : "???", i);
231 put_ldops(ldops);
Alan Cox01e1abb2008-07-22 11:16:55 +0100232 return 0;
233}
234
Christoph Hellwigfddda2b2018-04-13 19:44:18 +0200235const struct seq_operations tty_ldiscs_seq_ops = {
Alan Cox01e1abb2008-07-22 11:16:55 +0100236 .start = tty_ldiscs_seq_start,
237 .next = tty_ldiscs_seq_next,
238 .stop = tty_ldiscs_seq_stop,
239 .show = tty_ldiscs_seq_show,
240};
241
Alan Cox01e1abb2008-07-22 11:16:55 +0100242/**
Alan Cox01e1abb2008-07-22 11:16:55 +0100243 * tty_ldisc_ref_wait - wait for the tty ldisc
244 * @tty: tty device
245 *
246 * Dereference the line discipline for the terminal and take a
247 * reference to it. If the line discipline is in flux then
248 * wait patiently until it changes.
249 *
Peter Hurley892d1fa2016-01-10 22:41:06 -0800250 * Returns: NULL if the tty has been hungup and not re-opened with
251 * a new file descriptor, otherwise valid ldisc reference
252 *
Alan Cox01e1abb2008-07-22 11:16:55 +0100253 * Note: Must not be called from an IRQ/timer context. The caller
254 * must also be careful not to hold other locks that will deadlock
255 * against a discipline change, such as an existing ldisc reference
256 * (which we check for)
257 *
Peter Hurleye55afd12016-01-10 22:41:01 -0800258 * Note: a file_operations routine (read/poll/write) should use this
259 * function to wait for any ldisc lifetime events to finish.
Alan Cox01e1abb2008-07-22 11:16:55 +0100260 */
261
262struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
263{
Dmitry Vyukova4a3e062017-03-04 13:46:12 +0100264 struct tty_ldisc *ld;
265
Peter Hurley36697522013-06-15 07:04:48 -0400266 ldsem_down_read(&tty->ldisc_sem, MAX_SCHEDULE_TIMEOUT);
Dmitry Vyukova4a3e062017-03-04 13:46:12 +0100267 ld = tty->ldisc;
268 if (!ld)
Peter Hurleya570a492016-01-10 22:41:02 -0800269 ldsem_up_read(&tty->ldisc_sem);
Dmitry Vyukova4a3e062017-03-04 13:46:12 +0100270 return ld;
Alan Cox01e1abb2008-07-22 11:16:55 +0100271}
Alan Cox01e1abb2008-07-22 11:16:55 +0100272EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
273
274/**
275 * tty_ldisc_ref - get the tty ldisc
276 * @tty: tty device
277 *
278 * Dereference the line discipline for the terminal and take a
279 * reference to it. If the line discipline is in flux then
280 * return NULL. Can be called from IRQ and timer functions.
Alan Cox01e1abb2008-07-22 11:16:55 +0100281 */
282
283struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
284{
Peter Hurley36697522013-06-15 07:04:48 -0400285 struct tty_ldisc *ld = NULL;
286
287 if (ldsem_down_read_trylock(&tty->ldisc_sem)) {
288 ld = tty->ldisc;
289 if (!ld)
290 ldsem_up_read(&tty->ldisc_sem);
291 }
292 return ld;
Alan Cox01e1abb2008-07-22 11:16:55 +0100293}
Alan Cox01e1abb2008-07-22 11:16:55 +0100294EXPORT_SYMBOL_GPL(tty_ldisc_ref);
295
296/**
297 * tty_ldisc_deref - free a tty ldisc reference
298 * @ld: reference to free up
299 *
300 * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May
301 * be called in IRQ context.
Alan Cox01e1abb2008-07-22 11:16:55 +0100302 */
303
304void tty_ldisc_deref(struct tty_ldisc *ld)
305{
Peter Hurley36697522013-06-15 07:04:48 -0400306 ldsem_up_read(&ld->tty->ldisc_sem);
Alan Cox01e1abb2008-07-22 11:16:55 +0100307}
Alan Cox01e1abb2008-07-22 11:16:55 +0100308EXPORT_SYMBOL_GPL(tty_ldisc_deref);
309
Peter Hurleyd2c43892013-06-15 07:04:47 -0400310
Peter Hurleyc2bb5242016-01-09 21:13:51 -0800311static inline int
Peter Hurleye80a10e2014-11-05 12:13:06 -0500312__tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout)
Peter Hurleyd2c43892013-06-15 07:04:47 -0400313{
314 return ldsem_down_write(&tty->ldisc_sem, timeout);
315}
316
Peter Hurleyc2bb5242016-01-09 21:13:51 -0800317static inline int
Peter Hurleye80a10e2014-11-05 12:13:06 -0500318__tty_ldisc_lock_nested(struct tty_struct *tty, unsigned long timeout)
Peter Hurleyd2c43892013-06-15 07:04:47 -0400319{
320 return ldsem_down_write_nested(&tty->ldisc_sem,
321 LDISC_SEM_OTHER, timeout);
322}
323
Peter Hurleye80a10e2014-11-05 12:13:06 -0500324static inline void __tty_ldisc_unlock(struct tty_struct *tty)
Peter Hurleyd2c43892013-06-15 07:04:47 -0400325{
Guillaume Gomez52772ea2015-10-04 21:19:18 +0200326 ldsem_up_write(&tty->ldisc_sem);
Peter Hurleyd2c43892013-06-15 07:04:47 -0400327}
328
Gaurav Kohlib027e222018-01-23 13:16:34 +0530329int tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout)
Peter Hurleyfae76e92014-11-05 12:13:07 -0500330{
331 int ret;
332
Dmitry Safonovc96cf922018-11-01 00:24:48 +0000333 /* Kindly asking blocked readers to release the read side */
334 set_bit(TTY_LDISC_CHANGING, &tty->flags);
335 wake_up_interruptible_all(&tty->read_wait);
336 wake_up_interruptible_all(&tty->write_wait);
337
Peter Hurleyfae76e92014-11-05 12:13:07 -0500338 ret = __tty_ldisc_lock(tty, timeout);
339 if (!ret)
340 return -EBUSY;
341 set_bit(TTY_LDISC_HALTED, &tty->flags);
342 return 0;
343}
344
Gaurav Kohlib027e222018-01-23 13:16:34 +0530345void tty_ldisc_unlock(struct tty_struct *tty)
Peter Hurleyfae76e92014-11-05 12:13:07 -0500346{
347 clear_bit(TTY_LDISC_HALTED, &tty->flags);
Dmitry Safonovc96cf922018-11-01 00:24:48 +0000348 /* Can be cleared here - ldisc_unlock will wake up writers firstly */
349 clear_bit(TTY_LDISC_CHANGING, &tty->flags);
Peter Hurleyfae76e92014-11-05 12:13:07 -0500350 __tty_ldisc_unlock(tty);
351}
352
Peter Hurleyc2bb5242016-01-09 21:13:51 -0800353static int
Peter Hurleyd2c43892013-06-15 07:04:47 -0400354tty_ldisc_lock_pair_timeout(struct tty_struct *tty, struct tty_struct *tty2,
355 unsigned long timeout)
356{
357 int ret;
358
359 if (tty < tty2) {
Peter Hurleye80a10e2014-11-05 12:13:06 -0500360 ret = __tty_ldisc_lock(tty, timeout);
Peter Hurleyd2c43892013-06-15 07:04:47 -0400361 if (ret) {
Peter Hurleye80a10e2014-11-05 12:13:06 -0500362 ret = __tty_ldisc_lock_nested(tty2, timeout);
Peter Hurleyd2c43892013-06-15 07:04:47 -0400363 if (!ret)
Peter Hurleye80a10e2014-11-05 12:13:06 -0500364 __tty_ldisc_unlock(tty);
Peter Hurleyd2c43892013-06-15 07:04:47 -0400365 }
366 } else {
367 /* if this is possible, it has lots of implications */
368 WARN_ON_ONCE(tty == tty2);
369 if (tty2 && tty != tty2) {
Peter Hurleye80a10e2014-11-05 12:13:06 -0500370 ret = __tty_ldisc_lock(tty2, timeout);
Peter Hurleyd2c43892013-06-15 07:04:47 -0400371 if (ret) {
Peter Hurleye80a10e2014-11-05 12:13:06 -0500372 ret = __tty_ldisc_lock_nested(tty, timeout);
Peter Hurleyd2c43892013-06-15 07:04:47 -0400373 if (!ret)
Peter Hurleye80a10e2014-11-05 12:13:06 -0500374 __tty_ldisc_unlock(tty2);
Peter Hurleyd2c43892013-06-15 07:04:47 -0400375 }
376 } else
Peter Hurleye80a10e2014-11-05 12:13:06 -0500377 ret = __tty_ldisc_lock(tty, timeout);
Peter Hurleyd2c43892013-06-15 07:04:47 -0400378 }
379
380 if (!ret)
381 return -EBUSY;
382
383 set_bit(TTY_LDISC_HALTED, &tty->flags);
384 if (tty2)
385 set_bit(TTY_LDISC_HALTED, &tty2->flags);
386 return 0;
387}
388
Peter Hurleyc2bb5242016-01-09 21:13:51 -0800389static void tty_ldisc_lock_pair(struct tty_struct *tty, struct tty_struct *tty2)
Peter Hurleyd2c43892013-06-15 07:04:47 -0400390{
391 tty_ldisc_lock_pair_timeout(tty, tty2, MAX_SCHEDULE_TIMEOUT);
392}
393
Peter Hurleyc2bb5242016-01-09 21:13:51 -0800394static void tty_ldisc_unlock_pair(struct tty_struct *tty,
395 struct tty_struct *tty2)
Peter Hurleyd2c43892013-06-15 07:04:47 -0400396{
Peter Hurleye80a10e2014-11-05 12:13:06 -0500397 __tty_ldisc_unlock(tty);
Peter Hurleyd2c43892013-06-15 07:04:47 -0400398 if (tty2)
Peter Hurleye80a10e2014-11-05 12:13:06 -0500399 __tty_ldisc_unlock(tty2);
Peter Hurleyd2c43892013-06-15 07:04:47 -0400400}
401
Alan Cox01e1abb2008-07-22 11:16:55 +0100402/**
Alan Coxf2c4c652009-06-11 12:50:58 +0100403 * tty_ldisc_flush - flush line discipline queue
404 * @tty: tty
405 *
Peter Hurley86c80a82014-11-05 12:13:09 -0500406 * Flush the line discipline queue (if any) and the tty flip buffers
407 * for this tty.
Alan Coxf2c4c652009-06-11 12:50:58 +0100408 */
409
410void tty_ldisc_flush(struct tty_struct *tty)
411{
412 struct tty_ldisc *ld = tty_ldisc_ref(tty);
Peter Hurley86c80a82014-11-05 12:13:09 -0500413
414 tty_buffer_flush(tty, ld);
415 if (ld)
Alan Coxf2c4c652009-06-11 12:50:58 +0100416 tty_ldisc_deref(ld);
Alan Coxf2c4c652009-06-11 12:50:58 +0100417}
Alan Coxf2c4c652009-06-11 12:50:58 +0100418EXPORT_SYMBOL_GPL(tty_ldisc_flush);
419
420/**
Alan Cox01e1abb2008-07-22 11:16:55 +0100421 * tty_set_termios_ldisc - set ldisc field
422 * @tty: tty structure
Peter Hurleyc12da962016-01-10 22:41:04 -0800423 * @disc: line discipline number
Alan Cox01e1abb2008-07-22 11:16:55 +0100424 *
425 * This is probably overkill for real world processors but
426 * they are not on hot paths so a little discipline won't do
427 * any harm.
428 *
Peter Hurleydd42bf12015-11-27 14:30:21 -0500429 * The line discipline-related tty_struct fields are reset to
430 * prevent the ldisc driver from re-using stale information for
431 * the new ldisc instance.
432 *
Peter Hurley6a1c0682013-06-15 09:14:23 -0400433 * Locking: takes termios_rwsem
Alan Cox01e1abb2008-07-22 11:16:55 +0100434 */
435
Peter Hurleyc12da962016-01-10 22:41:04 -0800436static void tty_set_termios_ldisc(struct tty_struct *tty, int disc)
Alan Cox01e1abb2008-07-22 11:16:55 +0100437{
Peter Hurley6a1c0682013-06-15 09:14:23 -0400438 down_write(&tty->termios_rwsem);
Peter Hurleyc12da962016-01-10 22:41:04 -0800439 tty->termios.c_line = disc;
Peter Hurley6a1c0682013-06-15 09:14:23 -0400440 up_write(&tty->termios_rwsem);
Peter Hurleydd42bf12015-11-27 14:30:21 -0500441
442 tty->disc_data = NULL;
443 tty->receive_room = 0;
Alan Cox01e1abb2008-07-22 11:16:55 +0100444}
445
Alan Coxc65c9bc2009-06-11 12:50:12 +0100446/**
447 * tty_ldisc_open - open a line discipline
448 * @tty: tty we are opening the ldisc on
449 * @ld: discipline to open
450 *
451 * A helper opening method. Also a convenient debugging and check
452 * point.
Arnd Bergmannec79d602010-06-01 22:53:01 +0200453 *
454 * Locking: always called with BTM already held.
Alan Coxc65c9bc2009-06-11 12:50:12 +0100455 */
456
457static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld)
458{
459 WARN_ON(test_and_set_bit(TTY_LDISC_OPEN, &tty->flags));
Alan Coxf18f9492009-11-30 13:18:35 +0000460 if (ld->ops->open) {
461 int ret;
Arnd Bergmannec79d602010-06-01 22:53:01 +0200462 /* BTM here locks versus a hangup event */
Alan Coxf18f9492009-11-30 13:18:35 +0000463 ret = ld->ops->open(tty);
Jiri Slaby7f90cfc2010-11-25 00:27:54 +0100464 if (ret)
465 clear_bit(TTY_LDISC_OPEN, &tty->flags);
Peter Hurleyfb6edc92015-07-12 22:49:12 -0400466
Peter Hurleya570a492016-01-10 22:41:02 -0800467 tty_ldisc_debug(tty, "%p: opened\n", ld);
Alan Coxf18f9492009-11-30 13:18:35 +0000468 return ret;
469 }
Alan Coxc65c9bc2009-06-11 12:50:12 +0100470 return 0;
471}
472
473/**
474 * tty_ldisc_close - close a line discipline
475 * @tty: tty we are opening the ldisc on
476 * @ld: discipline to close
477 *
478 * A helper close method. Also a convenient debugging and check
479 * point.
480 */
481
482static void tty_ldisc_close(struct tty_struct *tty, struct tty_ldisc *ld)
483{
Nikolay Borisov9ffbe8a2019-05-31 13:06:51 +0300484 lockdep_assert_held_write(&tty->ldisc_sem);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100485 WARN_ON(!test_bit(TTY_LDISC_OPEN, &tty->flags));
486 clear_bit(TTY_LDISC_OPEN, &tty->flags);
487 if (ld->ops->close)
488 ld->ops->close(tty);
Peter Hurleya570a492016-01-10 22:41:02 -0800489 tty_ldisc_debug(tty, "%p: closed\n", ld);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100490}
Alan Cox01e1abb2008-07-22 11:16:55 +0100491
492/**
Alan Cox8a8dabf2017-06-02 13:49:30 +0100493 * tty_ldisc_failto - helper for ldisc failback
494 * @tty: tty to open the ldisc on
495 * @ld: ldisc we are trying to fail back to
496 *
497 * Helper to try and recover a tty when switching back to the old
498 * ldisc fails and we need something attached.
499 */
500
501static int tty_ldisc_failto(struct tty_struct *tty, int ld)
502{
503 struct tty_ldisc *disc = tty_ldisc_get(tty, ld);
504 int r;
505
Nikolay Borisov9ffbe8a2019-05-31 13:06:51 +0300506 lockdep_assert_held_write(&tty->ldisc_sem);
Alan Cox8a8dabf2017-06-02 13:49:30 +0100507 if (IS_ERR(disc))
508 return PTR_ERR(disc);
509 tty->ldisc = disc;
510 tty_set_termios_ldisc(tty, ld);
511 if ((r = tty_ldisc_open(tty, disc)) < 0)
512 tty_ldisc_put(disc);
513 return r;
514}
515
516/**
Greg Kroah-Hartmana8983d02017-04-14 10:57:52 +0200517 * tty_ldisc_restore - helper for tty ldisc change
518 * @tty: tty to recover
519 * @old: previous ldisc
520 *
521 * Restore the previous line discipline or N_TTY when a line discipline
522 * change fails due to an open error
523 */
524
525static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old)
526{
Greg Kroah-Hartmana8983d02017-04-14 10:57:52 +0200527 /* There is an outstanding reference here so this is safe */
Tetsuo Handa598c2d42018-04-16 20:06:34 +0900528 if (tty_ldisc_failto(tty, old->ops->num) < 0) {
529 const char *name = tty_name(tty);
530
531 pr_warn("Falling back ldisc for %s.\n", name);
Alan Cox8a8dabf2017-06-02 13:49:30 +0100532 /* The traditional behaviour is to fall back to N_TTY, we
533 want to avoid falling back to N_NULL unless we have no
534 choice to avoid the risk of breaking anything */
535 if (tty_ldisc_failto(tty, N_TTY) < 0 &&
536 tty_ldisc_failto(tty, N_NULL) < 0)
Tetsuo Handa598c2d42018-04-16 20:06:34 +0900537 panic("Couldn't open N_NULL ldisc for %s.", name);
Greg Kroah-Hartmana8983d02017-04-14 10:57:52 +0200538 }
539}
540
541/**
Alan Cox01e1abb2008-07-22 11:16:55 +0100542 * tty_set_ldisc - set line discipline
543 * @tty: the terminal to set
Jiri Slabyfa441952020-08-18 10:56:51 +0200544 * @disc: the line discipline number
Alan Cox01e1abb2008-07-22 11:16:55 +0100545 *
546 * Set the discipline of a tty line. Must be called from a process
Alan Coxc65c9bc2009-06-11 12:50:12 +0100547 * context. The ldisc change logic has to protect itself against any
548 * overlapping ldisc change (including on the other end of pty pairs),
549 * the close of one side of a tty/pty pair, and eventually hangup.
Alan Cox01e1abb2008-07-22 11:16:55 +0100550 */
551
Peter Hurleyc12da962016-01-10 22:41:04 -0800552int tty_set_ldisc(struct tty_struct *tty, int disc)
Alan Cox01e1abb2008-07-22 11:16:55 +0100553{
Greg Kroah-Hartmana8983d02017-04-14 10:57:52 +0200554 int retval;
555 struct tty_ldisc *old_ldisc, *new_ldisc;
556
557 new_ldisc = tty_ldisc_get(tty, disc);
558 if (IS_ERR(new_ldisc))
559 return PTR_ERR(new_ldisc);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100560
Peter Hurleyc8483bc2014-11-05 12:12:45 -0500561 tty_lock(tty);
Peter Hurley276a6612014-11-05 12:13:08 -0500562 retval = tty_ldisc_lock(tty, 5 * HZ);
Peter Hurley63d8cb32015-11-08 09:29:38 -0500563 if (retval)
564 goto err;
Alan Coxc65c9bc2009-06-11 12:50:12 +0100565
Peter Hurleya570a492016-01-10 22:41:02 -0800566 if (!tty->ldisc) {
567 retval = -EIO;
568 goto out;
569 }
570
Peter Hurley63d8cb32015-11-08 09:29:38 -0500571 /* Check the no-op case */
Greg Kroah-Hartmana8983d02017-04-14 10:57:52 +0200572 if (tty->ldisc->ops->num == disc)
Peter Hurley63d8cb32015-11-08 09:29:38 -0500573 goto out;
Alan Coxc65c9bc2009-06-11 12:50:12 +0100574
Peter Hurley63d8cb32015-11-08 09:29:38 -0500575 if (test_bit(TTY_HUPPED, &tty->flags)) {
576 /* We were raced by hangup */
577 retval = -EIO;
578 goto out;
Alan Coxc65c9bc2009-06-11 12:50:12 +0100579 }
Alan Cox01e1abb2008-07-22 11:16:55 +0100580
Greg Kroah-Hartmana8983d02017-04-14 10:57:52 +0200581 old_ldisc = tty->ldisc;
582
583 /* Shutdown the old discipline. */
584 tty_ldisc_close(tty, old_ldisc);
585
586 /* Now set up the new line discipline. */
587 tty->ldisc = new_ldisc;
588 tty_set_termios_ldisc(tty, disc);
589
590 retval = tty_ldisc_open(tty, new_ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100591 if (retval < 0) {
Alan Coxc65c9bc2009-06-11 12:50:12 +0100592 /* Back to the old one or N_TTY if we can't */
Greg Kroah-Hartmana8983d02017-04-14 10:57:52 +0200593 tty_ldisc_put(new_ldisc);
594 tty_ldisc_restore(tty, old_ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100595 }
Alan Coxc65c9bc2009-06-11 12:50:12 +0100596
Greg Kroah-Hartmana8983d02017-04-14 10:57:52 +0200597 if (tty->ldisc->ops->num != old_ldisc->ops->num && tty->ops->set_ldisc) {
Peter Hurley9191aaa2014-11-05 13:11:41 -0500598 down_read(&tty->termios_rwsem);
Alan Cox01e1abb2008-07-22 11:16:55 +0100599 tty->ops->set_ldisc(tty);
Peter Hurley9191aaa2014-11-05 13:11:41 -0500600 up_read(&tty->termios_rwsem);
601 }
Alan Cox01e1abb2008-07-22 11:16:55 +0100602
Greg Kroah-Hartmana8983d02017-04-14 10:57:52 +0200603 /* At this point we hold a reference to the new ldisc and a
604 reference to the old ldisc, or we hold two references to
605 the old ldisc (if it was restored as part of error cleanup
606 above). In either case, releasing a single reference from
607 the old ldisc is correct. */
608 new_ldisc = old_ldisc;
Peter Hurley63d8cb32015-11-08 09:29:38 -0500609out:
Peter Hurley276a6612014-11-05 12:13:08 -0500610 tty_ldisc_unlock(tty);
Alan Cox01e1abb2008-07-22 11:16:55 +0100611
Alan Coxc65c9bc2009-06-11 12:50:12 +0100612 /* Restart the work queue in case no characters kick it off. Safe if
Alan Cox01e1abb2008-07-22 11:16:55 +0100613 already running */
Peter Hurley17a69212015-11-08 07:53:06 -0500614 tty_buffer_restart_work(tty->port);
Peter Hurley63d8cb32015-11-08 09:29:38 -0500615err:
Greg Kroah-Hartmana8983d02017-04-14 10:57:52 +0200616 tty_ldisc_put(new_ldisc); /* drop the extra reference */
Alan Cox89c8d912012-08-08 16:30:13 +0100617 tty_unlock(tty);
Alan Cox01e1abb2008-07-22 11:16:55 +0100618 return retval;
619}
Okash Khawaja1ab92da2017-05-15 18:45:33 +0100620EXPORT_SYMBOL_GPL(tty_set_ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100621
Alan Coxc65c9bc2009-06-11 12:50:12 +0100622/**
Peter Hurley6ffeb4b2016-01-10 22:41:03 -0800623 * tty_ldisc_kill - teardown ldisc
624 * @tty: tty being released
625 *
626 * Perform final close of the ldisc and reset tty->ldisc
627 */
628static void tty_ldisc_kill(struct tty_struct *tty)
629{
Nikolay Borisov9ffbe8a2019-05-31 13:06:51 +0300630 lockdep_assert_held_write(&tty->ldisc_sem);
Peter Hurley6ffeb4b2016-01-10 22:41:03 -0800631 if (!tty->ldisc)
632 return;
633 /*
634 * Now kill off the ldisc
635 */
636 tty_ldisc_close(tty, tty->ldisc);
637 tty_ldisc_put(tty->ldisc);
638 /* Force an oops if we mess this up */
639 tty->ldisc = NULL;
640}
641
642/**
Alan Coxc65c9bc2009-06-11 12:50:12 +0100643 * tty_reset_termios - reset terminal state
644 * @tty: tty to reset
645 *
646 * Restore a terminal to the driver default state.
647 */
648
649static void tty_reset_termios(struct tty_struct *tty)
650{
Peter Hurley6a1c0682013-06-15 09:14:23 -0400651 down_write(&tty->termios_rwsem);
Alan Coxadc8d742012-07-14 15:31:47 +0100652 tty->termios = tty->driver->init_termios;
653 tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios);
654 tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios);
Peter Hurley6a1c0682013-06-15 09:14:23 -0400655 up_write(&tty->termios_rwsem);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100656}
657
658
659/**
660 * tty_ldisc_reinit - reinitialise the tty ldisc
661 * @tty: tty to reinit
Peter Hurleyc12da962016-01-10 22:41:04 -0800662 * @disc: line discipline to reinitialize
Alan Coxc65c9bc2009-06-11 12:50:12 +0100663 *
Peter Hurley7896f302016-01-10 22:41:05 -0800664 * Completely reinitialize the line discipline state, by closing the
Peter Hurley892d1fa2016-01-10 22:41:06 -0800665 * current instance, if there is one, and opening a new instance. If
666 * an error occurs opening the new non-N_TTY instance, the instance
667 * is dropped and tty->ldisc reset to NULL. The caller can then retry
668 * with N_TTY instead.
Peter Hurley7896f302016-01-10 22:41:05 -0800669 *
670 * Returns 0 if successful, otherwise error code < 0
Alan Coxc65c9bc2009-06-11 12:50:12 +0100671 */
672
Peter Hurley892d1fa2016-01-10 22:41:06 -0800673int tty_ldisc_reinit(struct tty_struct *tty, int disc)
Alan Coxc65c9bc2009-06-11 12:50:12 +0100674{
Peter Hurley7896f302016-01-10 22:41:05 -0800675 struct tty_ldisc *ld;
676 int retval;
Philippe Rétornaz1c95ba12010-10-27 17:13:21 +0200677
Nikolay Borisov9ffbe8a2019-05-31 13:06:51 +0300678 lockdep_assert_held_write(&tty->ldisc_sem);
Peter Hurley7896f302016-01-10 22:41:05 -0800679 ld = tty_ldisc_get(tty, disc);
Greg Kroah-Hartmana8983d02017-04-14 10:57:52 +0200680 if (IS_ERR(ld)) {
681 BUG_ON(disc == N_TTY);
Peter Hurley7896f302016-01-10 22:41:05 -0800682 return PTR_ERR(ld);
Greg Kroah-Hartmana8983d02017-04-14 10:57:52 +0200683 }
Alan Coxc65c9bc2009-06-11 12:50:12 +0100684
Peter Hurley7896f302016-01-10 22:41:05 -0800685 if (tty->ldisc) {
686 tty_ldisc_close(tty, tty->ldisc);
687 tty_ldisc_put(tty->ldisc);
688 }
689
690 /* switch the line discipline */
Peter Hurleyf48070452013-03-11 16:44:42 -0400691 tty->ldisc = ld;
Peter Hurleyc12da962016-01-10 22:41:04 -0800692 tty_set_termios_ldisc(tty, disc);
Peter Hurley7896f302016-01-10 22:41:05 -0800693 retval = tty_ldisc_open(tty, tty->ldisc);
694 if (retval) {
Johannes Weinere65c62b2017-10-13 15:58:08 -0700695 tty_ldisc_put(tty->ldisc);
696 tty->ldisc = NULL;
Peter Hurley7896f302016-01-10 22:41:05 -0800697 }
698 return retval;
Alan Coxc65c9bc2009-06-11 12:50:12 +0100699}
700
701/**
702 * tty_ldisc_hangup - hangup ldisc reset
703 * @tty: tty being hung up
704 *
705 * Some tty devices reset their termios when they receive a hangup
706 * event. In that situation we must also switch back to N_TTY properly
707 * before we reset the termios data.
708 *
709 * Locking: We can take the ldisc mutex as the rest of the code is
710 * careful to allow for this.
711 *
712 * In the pty pair case this occurs in the close() path of the
713 * tty itself so we must be careful about locking rules.
714 */
715
Peter Hurley892d1fa2016-01-10 22:41:06 -0800716void tty_ldisc_hangup(struct tty_struct *tty, bool reinit)
Alan Coxc65c9bc2009-06-11 12:50:12 +0100717{
718 struct tty_ldisc *ld;
719
Peter Hurleya570a492016-01-10 22:41:02 -0800720 tty_ldisc_debug(tty, "%p: hangup\n", tty->ldisc);
Peter Hurleyfc575ee2013-03-11 16:44:38 -0400721
Alan Coxc65c9bc2009-06-11 12:50:12 +0100722 ld = tty_ldisc_ref(tty);
723 if (ld != NULL) {
Alan Coxc65c9bc2009-06-11 12:50:12 +0100724 if (ld->ops->flush_buffer)
725 ld->ops->flush_buffer(tty);
726 tty_driver_flush_buffer(tty);
727 if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
728 ld->ops->write_wakeup)
729 ld->ops->write_wakeup(tty);
730 if (ld->ops->hangup)
731 ld->ops->hangup(tty);
732 tty_ldisc_deref(ld);
733 }
Peter Hurley36697522013-06-15 07:04:48 -0400734
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800735 wake_up_interruptible_poll(&tty->write_wait, EPOLLOUT);
736 wake_up_interruptible_poll(&tty->read_wait, EPOLLIN);
Peter Hurley36697522013-06-15 07:04:48 -0400737
Alan Coxc65c9bc2009-06-11 12:50:12 +0100738 /*
739 * Shutdown the current line discipline, and reset it to
Alan Cox638b9642010-02-08 10:09:26 +0000740 * N_TTY if need be.
741 *
742 * Avoid racing set_ldisc or tty_ldisc_release
Alan Coxc65c9bc2009-06-11 12:50:12 +0100743 */
Peter Hurleyfae76e92014-11-05 12:13:07 -0500744 tty_ldisc_lock(tty, MAX_SCHEDULE_TIMEOUT);
Arnd Bergmann60af22d2010-06-01 22:53:06 +0200745
Peter Hurley892d1fa2016-01-10 22:41:06 -0800746 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
Alan Cox638b9642010-02-08 10:09:26 +0000747 tty_reset_termios(tty);
Peter Hurleyfc575ee2013-03-11 16:44:38 -0400748
Peter Hurley892d1fa2016-01-10 22:41:06 -0800749 if (tty->ldisc) {
750 if (reinit) {
Johannes Weinere65c62b2017-10-13 15:58:08 -0700751 if (tty_ldisc_reinit(tty, tty->termios.c_line) < 0 &&
752 tty_ldisc_reinit(tty, N_TTY) < 0)
753 WARN_ON(tty_ldisc_reinit(tty, N_NULL) < 0);
Peter Hurley892d1fa2016-01-10 22:41:06 -0800754 } else
755 tty_ldisc_kill(tty);
756 }
757 tty_ldisc_unlock(tty);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100758}
Alan Cox01e1abb2008-07-22 11:16:55 +0100759
760/**
761 * tty_ldisc_setup - open line discipline
762 * @tty: tty being shut down
763 * @o_tty: pair tty for pty/tty pairs
764 *
765 * Called during the initial open of a tty/pty pair in order to set up the
Alan Coxc65c9bc2009-06-11 12:50:12 +0100766 * line disciplines and bind them to the tty. This has no locking issues
767 * as the device isn't yet active.
Alan Cox01e1abb2008-07-22 11:16:55 +0100768 */
769
770int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty)
771{
Peter Hurley9de2a7c2016-01-10 22:41:08 -0800772 int retval = tty_ldisc_open(tty, tty->ldisc);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100773 if (retval)
774 return retval;
775
776 if (o_tty) {
Dmitry Safonov110b8922018-11-01 00:24:51 +0000777 /*
778 * Called without o_tty->ldisc_sem held, as o_tty has been
779 * just allocated and no one has a reference to it.
780 */
Alan Coxc65c9bc2009-06-11 12:50:12 +0100781 retval = tty_ldisc_open(o_tty, o_tty->ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100782 if (retval) {
Peter Hurley9de2a7c2016-01-10 22:41:08 -0800783 tty_ldisc_close(tty, tty->ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100784 return retval;
785 }
Alan Cox01e1abb2008-07-22 11:16:55 +0100786 }
Alan Cox01e1abb2008-07-22 11:16:55 +0100787 return 0;
788}
Alan Cox89c8d912012-08-08 16:30:13 +0100789
Alan Cox01e1abb2008-07-22 11:16:55 +0100790/**
791 * tty_ldisc_release - release line discipline
Peter Hurley62462ae2014-11-05 12:12:58 -0500792 * @tty: tty being shut down (or one end of pty pair)
Alan Cox01e1abb2008-07-22 11:16:55 +0100793 *
Peter Hurley62462ae2014-11-05 12:12:58 -0500794 * Called during the final close of a tty or a pty pair in order to shut
Peter Hurley5b6e6832016-01-10 22:41:00 -0800795 * down the line discpline layer. On exit, each tty's ldisc is NULL.
Alan Cox01e1abb2008-07-22 11:16:55 +0100796 */
797
Peter Hurley62462ae2014-11-05 12:12:58 -0500798void tty_ldisc_release(struct tty_struct *tty)
Alan Cox01e1abb2008-07-22 11:16:55 +0100799{
Peter Hurley62462ae2014-11-05 12:12:58 -0500800 struct tty_struct *o_tty = tty->link;
801
Alan Cox01e1abb2008-07-22 11:16:55 +0100802 /*
Peter Hurleya2965b72013-03-11 16:44:35 -0400803 * Shutdown this line discipline. As this is the final close,
804 * it does not race with the set_ldisc code path.
Alan Cox01e1abb2008-07-22 11:16:55 +0100805 */
Alan Cox01e1abb2008-07-22 11:16:55 +0100806
Peter Hurley36697522013-06-15 07:04:48 -0400807 tty_ldisc_lock_pair(tty, o_tty);
Alan Cox89c8d912012-08-08 16:30:13 +0100808 tty_ldisc_kill(tty);
Alan Cox89c8d912012-08-08 16:30:13 +0100809 if (o_tty)
810 tty_ldisc_kill(o_tty);
Peter Hurley36697522013-06-15 07:04:48 -0400811 tty_ldisc_unlock_pair(tty, o_tty);
812
Alan Coxaef29bc2009-06-29 15:21:47 +0100813 /* And the memory resources remaining (buffers, termios) will be
814 disposed of when the kref hits zero */
Peter Hurleyfc575ee2013-03-11 16:44:38 -0400815
Peter Hurleyfb6edc92015-07-12 22:49:12 -0400816 tty_ldisc_debug(tty, "released\n");
Alan Cox01e1abb2008-07-22 11:16:55 +0100817}
Okash Khawaja1ab92da2017-05-15 18:45:33 +0100818EXPORT_SYMBOL_GPL(tty_ldisc_release);
Alan Cox01e1abb2008-07-22 11:16:55 +0100819
820/**
821 * tty_ldisc_init - ldisc setup for new tty
822 * @tty: tty being allocated
823 *
824 * Set up the line discipline objects for a newly allocated tty. Note that
825 * the tty structure is not completely set up when this call is made.
826 */
827
Tetsuo Handa903f9db2018-04-05 19:40:16 +0900828int tty_ldisc_init(struct tty_struct *tty)
Alan Cox01e1abb2008-07-22 11:16:55 +0100829{
Peter Hurley36697522013-06-15 07:04:48 -0400830 struct tty_ldisc *ld = tty_ldisc_get(tty, N_TTY);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100831 if (IS_ERR(ld))
Tetsuo Handa903f9db2018-04-05 19:40:16 +0900832 return PTR_ERR(ld);
Peter Hurleyf48070452013-03-11 16:44:42 -0400833 tty->ldisc = ld;
Tetsuo Handa903f9db2018-04-05 19:40:16 +0900834 return 0;
Alan Cox01e1abb2008-07-22 11:16:55 +0100835}
836
Jiri Slaby67166712011-03-23 10:48:35 +0100837/**
Peter Hurleyc8b710b2016-01-09 21:13:46 -0800838 * tty_ldisc_deinit - ldisc cleanup for new tty
Jiri Slaby67166712011-03-23 10:48:35 +0100839 * @tty: tty that was allocated recently
840 *
841 * The tty structure must not becompletely set up (tty_ldisc_setup) when
842 * this call is made.
843 */
844void tty_ldisc_deinit(struct tty_struct *tty)
845{
Dmitry Safonov110b8922018-11-01 00:24:51 +0000846 /* no ldisc_sem, tty is being destroyed */
Peter Hurleyc8b710b2016-01-09 21:13:46 -0800847 if (tty->ldisc)
848 tty_ldisc_put(tty->ldisc);
Peter Hurleyf48070452013-03-11 16:44:42 -0400849 tty->ldisc = NULL;
Jiri Slaby67166712011-03-23 10:48:35 +0100850}
Greg Kroah-Hartman7c0cca72019-01-21 17:26:42 +0100851
Greg Kroah-Hartman7c0cca72019-01-21 17:26:42 +0100852static struct ctl_table tty_table[] = {
853 {
854 .procname = "ldisc_autoload",
855 .data = &tty_ldisc_autoload,
856 .maxlen = sizeof(tty_ldisc_autoload),
857 .mode = 0644,
858 .proc_handler = proc_dointvec,
Matteo Croceeec48442019-07-18 15:58:50 -0700859 .extra1 = SYSCTL_ZERO,
860 .extra2 = SYSCTL_ONE,
Greg Kroah-Hartman7c0cca72019-01-21 17:26:42 +0100861 },
862 { }
863};
864
865static struct ctl_table tty_dir_table[] = {
866 {
867 .procname = "tty",
868 .mode = 0555,
869 .child = tty_table,
870 },
871 { }
872};
873
874static struct ctl_table tty_root_table[] = {
875 {
876 .procname = "dev",
877 .mode = 0555,
878 .child = tty_dir_table,
879 },
880 { }
881};
882
883void tty_sysctl_init(void)
884{
885 register_sysctl_table(tty_root_table);
886}