tty: n_gsm: check error while registering tty devices
[linux-block.git] / drivers / tty / tty_ldisc.c
CommitLineData
e3b3d0f5 1// SPDX-License-Identifier: GPL-2.0
01e1abb2 2#include <linux/types.h>
01e1abb2 3#include <linux/errno.h>
8b3ffa17 4#include <linux/kmod.h>
01e1abb2
AC
5#include <linux/sched.h>
6#include <linux/interrupt.h>
7#include <linux/tty.h>
8#include <linux/tty_driver.h>
01e1abb2 9#include <linux/file.h>
01e1abb2
AC
10#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>
01e1abb2 15#include <linux/module.h>
01e1abb2
AC
16#include <linux/device.h>
17#include <linux/wait.h>
18#include <linux/bitops.h>
01e1abb2 19#include <linux/seq_file.h>
01e1abb2 20#include <linux/uaccess.h>
0c73c08e 21#include <linux/ratelimit.h>
01e1abb2 22
fc575ee6
PH
23#undef LDISC_DEBUG_HANGUP
24
25#ifdef LDISC_DEBUG_HANGUP
0a6adc13 26#define tty_ldisc_debug(tty, f, args...) tty_debug(tty, f, ##args)
fc575ee6
PH
27#else
28#define tty_ldisc_debug(tty, f, args...)
29#endif
30
d2c43890
PH
31/* lockdep nested classes for tty->ldisc_sem */
32enum {
33 LDISC_SEM_NORMAL,
34 LDISC_SEM_OTHER,
35};
36
37
01e1abb2
AC
38/*
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
137084bb 44static DEFINE_RAW_SPINLOCK(tty_ldiscs_lock);
01e1abb2
AC
45/* 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:
137084bb 58 * takes tty_ldiscs_lock to guard against ldisc races
01e1abb2
AC
59 */
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
137084bb 69 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
01e1abb2
AC
70 tty_ldiscs[disc] = new_ldisc;
71 new_ldisc->num = disc;
72 new_ldisc->refcount = 0;
137084bb 73 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
01e1abb2
AC
74
75 return ret;
76}
77EXPORT_SYMBOL(tty_register_ldisc);
78
79/**
80 * tty_unregister_ldisc - unload a line discipline
81 * @disc: ldisc number
01e1abb2
AC
82 *
83 * Remove a line discipline from the kernel providing it is not
84 * currently in use.
85 *
86 * Locking:
137084bb 87 * takes tty_ldiscs_lock to guard against ldisc races
01e1abb2
AC
88 */
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
137084bb 98 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
01e1abb2
AC
99 if (tty_ldiscs[disc]->refcount)
100 ret = -EBUSY;
101 else
102 tty_ldiscs[disc] = NULL;
137084bb 103 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
01e1abb2
AC
104
105 return ret;
106}
107EXPORT_SYMBOL(tty_unregister_ldisc);
108
f0de0e8d
LT
109static struct tty_ldisc_ops *get_ldops(int disc)
110{
111 unsigned long flags;
112 struct tty_ldisc_ops *ldops, *ret;
113
137084bb 114 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
f0de0e8d
LT
115 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 }
137084bb 124 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
f0de0e8d
LT
125 return ret;
126}
127
128static void put_ldops(struct tty_ldisc_ops *ldops)
129{
130 unsigned long flags;
131
137084bb 132 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
f0de0e8d
LT
133 ldops->refcount--;
134 module_put(ldops->owner);
137084bb 135 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
f0de0e8d 136}
01e1abb2 137
8eddcca2 138static int tty_ldisc_autoload = IS_BUILTIN(CONFIG_LDISC_AUTOLOAD);
01e1abb2
AC
139/**
140 * tty_ldisc_get - take a reference to an ldisc
8a3bdec1 141 * @tty: tty device
01e1abb2 142 * @disc: ldisc number
01e1abb2
AC
143 *
144 * Takes a reference to a line discipline. Deals with refcounts and
c0cc1c5d
PH
145 * module locking counts.
146 *
147 * Returns: -EINVAL if the discipline index is not [N_TTY..NR_LDISCS] or
148 * if the discipline is not registered
149 * -EAGAIN if request_module() failed to load or register the
b8958546 150 * discipline
c0cc1c5d
PH
151 * -ENOMEM if allocation failure
152 *
153 * Otherwise, returns a pointer to the discipline and bumps the
154 * ref count
01e1abb2
AC
155 *
156 * Locking:
137084bb 157 * takes tty_ldiscs_lock to guard against ldisc races
01e1abb2
AC
158 */
159
36697529 160static struct tty_ldisc *tty_ldisc_get(struct tty_struct *tty, int disc)
01e1abb2 161{
c65c9bc3 162 struct tty_ldisc *ld;
182274f8 163 struct tty_ldisc_ops *ldops;
01e1abb2
AC
164
165 if (disc < N_TTY || disc >= NR_LDISCS)
c65c9bc3 166 return ERR_PTR(-EINVAL);
182274f8
LT
167
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)) {
7c0cca7c
GKH
174 if (!capable(CAP_SYS_MODULE) && !tty_ldisc_autoload)
175 return ERR_PTR(-EPERM);
01e1abb2 176 request_module("tty-ldisc-%d", disc);
182274f8
LT
177 ldops = get_ldops(disc);
178 if (IS_ERR(ldops))
179 return ERR_CAST(ldops);
180 }
181
bcdd0ca8
TH
182 /*
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);
182274f8 187 ld->ops = ldops;
36697529 188 ld->tty = tty;
1541f845 189
c65c9bc3 190 return ld;
01e1abb2
AC
191}
192
8eddcca2 193/*
734de249
PH
194 * tty_ldisc_put - release the ldisc
195 *
196 * Complement of tty_ldisc_get().
197 */
cb128f69 198static void tty_ldisc_put(struct tty_ldisc *ld)
734de249 199{
734de249
PH
200 if (WARN_ON_ONCE(!ld))
201 return;
202
36697529 203 put_ldops(ld->ops);
734de249 204 kfree(ld);
734de249
PH
205}
206
852e99d2 207static void *tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos)
01e1abb2
AC
208{
209 return (*pos < NR_LDISCS) ? pos : NULL;
210}
211
852e99d2 212static void *tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos)
01e1abb2
AC
213{
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;
f0de0e8d 225 struct tty_ldisc_ops *ldops;
852e99d2 226
f0de0e8d
LT
227 ldops = get_ldops(i);
228 if (IS_ERR(ldops))
01e1abb2 229 return 0;
f0de0e8d
LT
230 seq_printf(m, "%-10s %2d\n", ldops->name ? ldops->name : "???", i);
231 put_ldops(ldops);
01e1abb2
AC
232 return 0;
233}
234
fddda2b7 235const struct seq_operations tty_ldiscs_seq_ops = {
01e1abb2
AC
236 .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
01e1abb2
AC
242/**
243 * 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 *
892d1fa7
PH
250 * Returns: NULL if the tty has been hungup and not re-opened with
251 * a new file descriptor, otherwise valid ldisc reference
252 *
8eddcca2 253 * Note 1: Must not be called from an IRQ/timer context. The caller
01e1abb2
AC
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 *
8eddcca2 258 * Note 2: a file_operations routine (read/poll/write) should use this
e55afd11 259 * function to wait for any ldisc lifetime events to finish.
01e1abb2
AC
260 */
261
262struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
263{
a4a3e061
DV
264 struct tty_ldisc *ld;
265
36697529 266 ldsem_down_read(&tty->ldisc_sem, MAX_SCHEDULE_TIMEOUT);
a4a3e061
DV
267 ld = tty->ldisc;
268 if (!ld)
a570a49a 269 ldsem_up_read(&tty->ldisc_sem);
a4a3e061 270 return ld;
01e1abb2 271}
01e1abb2
AC
272EXPORT_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.
01e1abb2
AC
281 */
282
283struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
284{
36697529
PH
285 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;
01e1abb2 293}
01e1abb2
AC
294EXPORT_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.
01e1abb2
AC
302 */
303
304void tty_ldisc_deref(struct tty_ldisc *ld)
305{
36697529 306 ldsem_up_read(&ld->tty->ldisc_sem);
01e1abb2 307}
01e1abb2
AC
308EXPORT_SYMBOL_GPL(tty_ldisc_deref);
309
d2c43890 310
c2bb524b 311static inline int
e80a10ee 312__tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout)
d2c43890
PH
313{
314 return ldsem_down_write(&tty->ldisc_sem, timeout);
315}
316
c2bb524b 317static inline int
e80a10ee 318__tty_ldisc_lock_nested(struct tty_struct *tty, unsigned long timeout)
d2c43890
PH
319{
320 return ldsem_down_write_nested(&tty->ldisc_sem,
321 LDISC_SEM_OTHER, timeout);
322}
323
e80a10ee 324static inline void __tty_ldisc_unlock(struct tty_struct *tty)
d2c43890 325{
52772ea6 326 ldsem_up_write(&tty->ldisc_sem);
d2c43890
PH
327}
328
b027e229 329int tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout)
fae76e9a
PH
330{
331 int ret;
332
c96cf923
DS
333 /* 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
fae76e9a
PH
338 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
b027e229 345void tty_ldisc_unlock(struct tty_struct *tty)
fae76e9a
PH
346{
347 clear_bit(TTY_LDISC_HALTED, &tty->flags);
c96cf923
DS
348 /* Can be cleared here - ldisc_unlock will wake up writers firstly */
349 clear_bit(TTY_LDISC_CHANGING, &tty->flags);
fae76e9a
PH
350 __tty_ldisc_unlock(tty);
351}
352
c2bb524b 353static int
d2c43890
PH
354tty_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) {
e80a10ee 360 ret = __tty_ldisc_lock(tty, timeout);
d2c43890 361 if (ret) {
e80a10ee 362 ret = __tty_ldisc_lock_nested(tty2, timeout);
d2c43890 363 if (!ret)
e80a10ee 364 __tty_ldisc_unlock(tty);
d2c43890
PH
365 }
366 } else {
367 /* if this is possible, it has lots of implications */
368 WARN_ON_ONCE(tty == tty2);
369 if (tty2 && tty != tty2) {
e80a10ee 370 ret = __tty_ldisc_lock(tty2, timeout);
d2c43890 371 if (ret) {
e80a10ee 372 ret = __tty_ldisc_lock_nested(tty, timeout);
d2c43890 373 if (!ret)
e80a10ee 374 __tty_ldisc_unlock(tty2);
d2c43890
PH
375 }
376 } else
e80a10ee 377 ret = __tty_ldisc_lock(tty, timeout);
d2c43890
PH
378 }
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
c2bb524b 389static void tty_ldisc_lock_pair(struct tty_struct *tty, struct tty_struct *tty2)
d2c43890
PH
390{
391 tty_ldisc_lock_pair_timeout(tty, tty2, MAX_SCHEDULE_TIMEOUT);
392}
393
c2bb524b
PH
394static void tty_ldisc_unlock_pair(struct tty_struct *tty,
395 struct tty_struct *tty2)
d2c43890 396{
e80a10ee 397 __tty_ldisc_unlock(tty);
d2c43890 398 if (tty2)
e80a10ee 399 __tty_ldisc_unlock(tty2);
d2c43890
PH
400}
401
f2c4c65c
AC
402/**
403 * tty_ldisc_flush - flush line discipline queue
404 * @tty: tty
405 *
86c80a8e
PH
406 * Flush the line discipline queue (if any) and the tty flip buffers
407 * for this tty.
f2c4c65c
AC
408 */
409
410void tty_ldisc_flush(struct tty_struct *tty)
411{
412 struct tty_ldisc *ld = tty_ldisc_ref(tty);
86c80a8e
PH
413
414 tty_buffer_flush(tty, ld);
415 if (ld)
f2c4c65c 416 tty_ldisc_deref(ld);
f2c4c65c 417}
f2c4c65c
AC
418EXPORT_SYMBOL_GPL(tty_ldisc_flush);
419
01e1abb2
AC
420/**
421 * tty_set_termios_ldisc - set ldisc field
422 * @tty: tty structure
c12da96f 423 * @disc: line discipline number
01e1abb2
AC
424 *
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 *
dd42bf11
PH
429 * 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 *
6a1c0680 433 * Locking: takes termios_rwsem
01e1abb2
AC
434 */
435
c12da96f 436static void tty_set_termios_ldisc(struct tty_struct *tty, int disc)
01e1abb2 437{
6a1c0680 438 down_write(&tty->termios_rwsem);
c12da96f 439 tty->termios.c_line = disc;
6a1c0680 440 up_write(&tty->termios_rwsem);
dd42bf11
PH
441
442 tty->disc_data = NULL;
443 tty->receive_room = 0;
01e1abb2
AC
444}
445
c65c9bc3
AC
446/**
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.
ec79d605
AB
453 *
454 * Locking: always called with BTM already held.
c65c9bc3
AC
455 */
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));
f18f9498
AC
460 if (ld->ops->open) {
461 int ret;
5d3945e8 462 /* BTM here locks versus a hangup event */
f18f9498 463 ret = ld->ops->open(tty);
7f90cfc5
JS
464 if (ret)
465 clear_bit(TTY_LDISC_OPEN, &tty->flags);
fb6edc91 466
a570a49a 467 tty_ldisc_debug(tty, "%p: opened\n", ld);
f18f9498
AC
468 return ret;
469 }
c65c9bc3
AC
470 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{
9ffbe8ac 484 lockdep_assert_held_write(&tty->ldisc_sem);
c65c9bc3
AC
485 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);
a570a49a 489 tty_ldisc_debug(tty, "%p: closed\n", ld);
c65c9bc3 490}
01e1abb2 491
8a8dabf2
AC
492/**
493 * 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
9ffbe8ac 506 lockdep_assert_held_write(&tty->ldisc_sem);
8a8dabf2
AC
507 if (IS_ERR(disc))
508 return PTR_ERR(disc);
509 tty->ldisc = disc;
510 tty_set_termios_ldisc(tty, ld);
408795b0
XT
511 r = tty_ldisc_open(tty, disc);
512 if (r < 0)
8a8dabf2
AC
513 tty_ldisc_put(disc);
514 return r;
515}
516
a8983d01
GKH
517/**
518 * tty_ldisc_restore - helper for tty ldisc change
519 * @tty: tty to recover
520 * @old: previous ldisc
521 *
522 * Restore the previous line discipline or N_TTY when a line discipline
523 * change fails due to an open error
524 */
525
526static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old)
527{
a8983d01 528 /* There is an outstanding reference here so this is safe */
598c2d41
TH
529 if (tty_ldisc_failto(tty, old->ops->num) < 0) {
530 const char *name = tty_name(tty);
531
532 pr_warn("Falling back ldisc for %s.\n", name);
72a8dcd7
XT
533 /*
534 * The traditional behaviour is to fall back to N_TTY, we
535 * want to avoid falling back to N_NULL unless we have no
536 * choice to avoid the risk of breaking anything
537 */
8a8dabf2
AC
538 if (tty_ldisc_failto(tty, N_TTY) < 0 &&
539 tty_ldisc_failto(tty, N_NULL) < 0)
598c2d41 540 panic("Couldn't open N_NULL ldisc for %s.", name);
a8983d01
GKH
541 }
542}
543
01e1abb2
AC
544/**
545 * tty_set_ldisc - set line discipline
546 * @tty: the terminal to set
fa441954 547 * @disc: the line discipline number
01e1abb2
AC
548 *
549 * Set the discipline of a tty line. Must be called from a process
c65c9bc3
AC
550 * context. The ldisc change logic has to protect itself against any
551 * overlapping ldisc change (including on the other end of pty pairs),
552 * the close of one side of a tty/pty pair, and eventually hangup.
01e1abb2
AC
553 */
554
c12da96f 555int tty_set_ldisc(struct tty_struct *tty, int disc)
01e1abb2 556{
a8983d01
GKH
557 int retval;
558 struct tty_ldisc *old_ldisc, *new_ldisc;
559
560 new_ldisc = tty_ldisc_get(tty, disc);
561 if (IS_ERR(new_ldisc))
562 return PTR_ERR(new_ldisc);
01e1abb2 563
c8483bc9 564 tty_lock(tty);
276a661a 565 retval = tty_ldisc_lock(tty, 5 * HZ);
63d8cb3f
PH
566 if (retval)
567 goto err;
01e1abb2 568
a570a49a
PH
569 if (!tty->ldisc) {
570 retval = -EIO;
571 goto out;
572 }
573
63d8cb3f 574 /* Check the no-op case */
a8983d01 575 if (tty->ldisc->ops->num == disc)
63d8cb3f 576 goto out;
c65c9bc3 577
63d8cb3f
PH
578 if (test_bit(TTY_HUPPED, &tty->flags)) {
579 /* We were raced by hangup */
580 retval = -EIO;
581 goto out;
01e1abb2
AC
582 }
583
a8983d01
GKH
584 old_ldisc = tty->ldisc;
585
586 /* Shutdown the old discipline. */
587 tty_ldisc_close(tty, old_ldisc);
588
589 /* Now set up the new line discipline. */
590 tty->ldisc = new_ldisc;
591 tty_set_termios_ldisc(tty, disc);
592
593 retval = tty_ldisc_open(tty, new_ldisc);
01e1abb2 594 if (retval < 0) {
c65c9bc3 595 /* Back to the old one or N_TTY if we can't */
a8983d01
GKH
596 tty_ldisc_put(new_ldisc);
597 tty_ldisc_restore(tty, old_ldisc);
01e1abb2 598 }
c65c9bc3 599
a8983d01 600 if (tty->ldisc->ops->num != old_ldisc->ops->num && tty->ops->set_ldisc) {
9191aaaa 601 down_read(&tty->termios_rwsem);
01e1abb2 602 tty->ops->set_ldisc(tty);
9191aaaa
PH
603 up_read(&tty->termios_rwsem);
604 }
01e1abb2 605
72a8dcd7
XT
606 /*
607 * At this point we hold a reference to the new ldisc and a
608 * reference to the old ldisc, or we hold two references to
609 * the old ldisc (if it was restored as part of error cleanup
610 * above). In either case, releasing a single reference from
611 * the old ldisc is correct.
612 */
a8983d01 613 new_ldisc = old_ldisc;
63d8cb3f 614out:
276a661a 615 tty_ldisc_unlock(tty);
01e1abb2 616
72a8dcd7
XT
617 /*
618 * Restart the work queue in case no characters kick it off. Safe if
619 * already running
620 */
17a69219 621 tty_buffer_restart_work(tty->port);
63d8cb3f 622err:
a8983d01 623 tty_ldisc_put(new_ldisc); /* drop the extra reference */
89c8d91e 624 tty_unlock(tty);
01e1abb2
AC
625 return retval;
626}
1ab92da3 627EXPORT_SYMBOL_GPL(tty_set_ldisc);
01e1abb2 628
6ffeb4b2
PH
629/**
630 * tty_ldisc_kill - teardown ldisc
631 * @tty: tty being released
632 *
633 * Perform final close of the ldisc and reset tty->ldisc
634 */
635static void tty_ldisc_kill(struct tty_struct *tty)
636{
9ffbe8ac 637 lockdep_assert_held_write(&tty->ldisc_sem);
6ffeb4b2
PH
638 if (!tty->ldisc)
639 return;
640 /*
641 * Now kill off the ldisc
642 */
643 tty_ldisc_close(tty, tty->ldisc);
644 tty_ldisc_put(tty->ldisc);
645 /* Force an oops if we mess this up */
646 tty->ldisc = NULL;
647}
648
c65c9bc3
AC
649/**
650 * tty_reset_termios - reset terminal state
651 * @tty: tty to reset
652 *
653 * Restore a terminal to the driver default state.
654 */
655
656static void tty_reset_termios(struct tty_struct *tty)
657{
6a1c0680 658 down_write(&tty->termios_rwsem);
adc8d746
AC
659 tty->termios = tty->driver->init_termios;
660 tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios);
661 tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios);
6a1c0680 662 up_write(&tty->termios_rwsem);
c65c9bc3
AC
663}
664
665
666/**
667 * tty_ldisc_reinit - reinitialise the tty ldisc
668 * @tty: tty to reinit
c12da96f 669 * @disc: line discipline to reinitialize
c65c9bc3 670 *
7896f30d 671 * Completely reinitialize the line discipline state, by closing the
892d1fa7
PH
672 * current instance, if there is one, and opening a new instance. If
673 * an error occurs opening the new non-N_TTY instance, the instance
674 * is dropped and tty->ldisc reset to NULL. The caller can then retry
675 * with N_TTY instead.
7896f30d
PH
676 *
677 * Returns 0 if successful, otherwise error code < 0
c65c9bc3
AC
678 */
679
892d1fa7 680int tty_ldisc_reinit(struct tty_struct *tty, int disc)
c65c9bc3 681{
7896f30d
PH
682 struct tty_ldisc *ld;
683 int retval;
1c95ba1e 684
9ffbe8ac 685 lockdep_assert_held_write(&tty->ldisc_sem);
7896f30d 686 ld = tty_ldisc_get(tty, disc);
a8983d01
GKH
687 if (IS_ERR(ld)) {
688 BUG_ON(disc == N_TTY);
7896f30d 689 return PTR_ERR(ld);
a8983d01 690 }
c65c9bc3 691
7896f30d
PH
692 if (tty->ldisc) {
693 tty_ldisc_close(tty, tty->ldisc);
694 tty_ldisc_put(tty->ldisc);
695 }
696
697 /* switch the line discipline */
f4807045 698 tty->ldisc = ld;
c12da96f 699 tty_set_termios_ldisc(tty, disc);
7896f30d
PH
700 retval = tty_ldisc_open(tty, tty->ldisc);
701 if (retval) {
e65c62b1
JW
702 tty_ldisc_put(tty->ldisc);
703 tty->ldisc = NULL;
7896f30d
PH
704 }
705 return retval;
c65c9bc3
AC
706}
707
708/**
709 * tty_ldisc_hangup - hangup ldisc reset
710 * @tty: tty being hung up
8eddcca2 711 * @reinit: whether to re-initialise the tty
c65c9bc3
AC
712 *
713 * Some tty devices reset their termios when they receive a hangup
714 * event. In that situation we must also switch back to N_TTY properly
715 * before we reset the termios data.
716 *
717 * Locking: We can take the ldisc mutex as the rest of the code is
718 * careful to allow for this.
719 *
720 * In the pty pair case this occurs in the close() path of the
721 * tty itself so we must be careful about locking rules.
722 */
723
892d1fa7 724void tty_ldisc_hangup(struct tty_struct *tty, bool reinit)
c65c9bc3
AC
725{
726 struct tty_ldisc *ld;
727
a570a49a 728 tty_ldisc_debug(tty, "%p: hangup\n", tty->ldisc);
fc575ee6 729
c65c9bc3
AC
730 ld = tty_ldisc_ref(tty);
731 if (ld != NULL) {
c65c9bc3
AC
732 if (ld->ops->flush_buffer)
733 ld->ops->flush_buffer(tty);
734 tty_driver_flush_buffer(tty);
735 if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
736 ld->ops->write_wakeup)
737 ld->ops->write_wakeup(tty);
738 if (ld->ops->hangup)
739 ld->ops->hangup(tty);
740 tty_ldisc_deref(ld);
741 }
36697529 742
a9a08845
LT
743 wake_up_interruptible_poll(&tty->write_wait, EPOLLOUT);
744 wake_up_interruptible_poll(&tty->read_wait, EPOLLIN);
36697529 745
c65c9bc3
AC
746 /*
747 * Shutdown the current line discipline, and reset it to
638b9648
AC
748 * N_TTY if need be.
749 *
750 * Avoid racing set_ldisc or tty_ldisc_release
c65c9bc3 751 */
fae76e9a 752 tty_ldisc_lock(tty, MAX_SCHEDULE_TIMEOUT);
60af22d2 753
892d1fa7
PH
754 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
755 tty_reset_termios(tty);
c8785241 756
892d1fa7
PH
757 if (tty->ldisc) {
758 if (reinit) {
e65c62b1
JW
759 if (tty_ldisc_reinit(tty, tty->termios.c_line) < 0 &&
760 tty_ldisc_reinit(tty, N_TTY) < 0)
761 WARN_ON(tty_ldisc_reinit(tty, N_NULL) < 0);
892d1fa7
PH
762 } else
763 tty_ldisc_kill(tty);
c65c9bc3 764 }
fae76e9a 765 tty_ldisc_unlock(tty);
c65c9bc3 766}
01e1abb2
AC
767
768/**
769 * tty_ldisc_setup - open line discipline
770 * @tty: tty being shut down
771 * @o_tty: pair tty for pty/tty pairs
772 *
773 * Called during the initial open of a tty/pty pair in order to set up the
c65c9bc3
AC
774 * line disciplines and bind them to the tty. This has no locking issues
775 * as the device isn't yet active.
01e1abb2
AC
776 */
777
778int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty)
779{
9de2a7ce 780 int retval = tty_ldisc_open(tty, tty->ldisc);
d7238359 781
c65c9bc3
AC
782 if (retval)
783 return retval;
784
785 if (o_tty) {
110b8928
DS
786 /*
787 * Called without o_tty->ldisc_sem held, as o_tty has been
788 * just allocated and no one has a reference to it.
789 */
c65c9bc3 790 retval = tty_ldisc_open(o_tty, o_tty->ldisc);
01e1abb2 791 if (retval) {
9de2a7ce 792 tty_ldisc_close(tty, tty->ldisc);
01e1abb2
AC
793 return retval;
794 }
01e1abb2 795 }
01e1abb2
AC
796 return 0;
797}
89c8d91e 798
01e1abb2
AC
799/**
800 * tty_ldisc_release - release line discipline
62462aef 801 * @tty: tty being shut down (or one end of pty pair)
3ee175d9 802 *
62462aef 803 * Called during the final close of a tty or a pty pair in order to shut
5b6e6832 804 * down the line discpline layer. On exit, each tty's ldisc is NULL.
01e1abb2
AC
805 */
806
62462aef 807void tty_ldisc_release(struct tty_struct *tty)
01e1abb2 808{
62462aef
PH
809 struct tty_struct *o_tty = tty->link;
810
01e1abb2 811 /*
a2965b7b
PH
812 * Shutdown this line discipline. As this is the final close,
813 * it does not race with the set_ldisc code path.
01e1abb2 814 */
01e1abb2 815
36697529 816 tty_ldisc_lock_pair(tty, o_tty);
89c8d91e 817 tty_ldisc_kill(tty);
c65c9bc3 818 if (o_tty)
89c8d91e 819 tty_ldisc_kill(o_tty);
36697529
PH
820 tty_ldisc_unlock_pair(tty, o_tty);
821
72a8dcd7
XT
822 /*
823 * And the memory resources remaining (buffers, termios) will be
824 * disposed of when the kref hits zero
825 */
fc575ee6 826
fb6edc91 827 tty_ldisc_debug(tty, "released\n");
01e1abb2 828}
1ab92da3 829EXPORT_SYMBOL_GPL(tty_ldisc_release);
01e1abb2
AC
830
831/**
832 * tty_ldisc_init - ldisc setup for new tty
833 * @tty: tty being allocated
834 *
835 * Set up the line discipline objects for a newly allocated tty. Note that
836 * the tty structure is not completely set up when this call is made.
837 */
838
903f9db1 839int tty_ldisc_init(struct tty_struct *tty)
01e1abb2 840{
36697529 841 struct tty_ldisc *ld = tty_ldisc_get(tty, N_TTY);
d7238359 842
c65c9bc3 843 if (IS_ERR(ld))
903f9db1 844 return PTR_ERR(ld);
f4807045 845 tty->ldisc = ld;
903f9db1 846 return 0;
01e1abb2
AC
847}
848
6716671d 849/**
c8b710b3 850 * tty_ldisc_deinit - ldisc cleanup for new tty
6716671d
JS
851 * @tty: tty that was allocated recently
852 *
853 * The tty structure must not becompletely set up (tty_ldisc_setup) when
854 * this call is made.
855 */
856void tty_ldisc_deinit(struct tty_struct *tty)
857{
110b8928 858 /* no ldisc_sem, tty is being destroyed */
c8b710b3
PH
859 if (tty->ldisc)
860 tty_ldisc_put(tty->ldisc);
f4807045 861 tty->ldisc = NULL;
6716671d 862}
7c0cca7c 863
7c0cca7c
GKH
864static struct ctl_table tty_table[] = {
865 {
866 .procname = "ldisc_autoload",
867 .data = &tty_ldisc_autoload,
868 .maxlen = sizeof(tty_ldisc_autoload),
869 .mode = 0644,
870 .proc_handler = proc_dointvec,
eec4844f
MC
871 .extra1 = SYSCTL_ZERO,
872 .extra2 = SYSCTL_ONE,
7c0cca7c
GKH
873 },
874 { }
875};
876
877static struct ctl_table tty_dir_table[] = {
878 {
879 .procname = "tty",
880 .mode = 0555,
881 .child = tty_table,
882 },
883 { }
884};
885
886static struct ctl_table tty_root_table[] = {
887 {
888 .procname = "dev",
889 .mode = 0555,
890 .child = tty_dir_table,
891 },
892 { }
893};
894
895void tty_sysctl_init(void)
896{
897 register_sysctl_table(tty_root_table);
898}