Merge tag 'v3.10.76' into update
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / tty / tty_ldisc.c
CommitLineData
01e1abb2 1#include <linux/types.h>
01e1abb2 2#include <linux/errno.h>
8b3ffa17 3#include <linux/kmod.h>
01e1abb2
AC
4#include <linux/sched.h>
5#include <linux/interrupt.h>
6#include <linux/tty.h>
7#include <linux/tty_driver.h>
01e1abb2 8#include <linux/file.h>
01e1abb2
AC
9#include <linux/mm.h>
10#include <linux/string.h>
11#include <linux/slab.h>
12#include <linux/poll.h>
13#include <linux/proc_fs.h>
14#include <linux/init.h>
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
26#define tty_ldisc_debug(tty, f, args...) ({ \
27 char __b[64]; \
28 printk(KERN_DEBUG "%s: %s: " f, __func__, tty_name(tty, __b), ##args); \
29})
30#else
31#define tty_ldisc_debug(tty, f, args...)
32#endif
33
6fa3eb70
S
34/* lockdep nested classes for tty->ldisc_sem */
35enum {
36 LDISC_SEM_NORMAL,
37 LDISC_SEM_OTHER,
38};
39
40
01e1abb2
AC
41/*
42 * This guards the refcounted line discipline lists. The lock
43 * must be taken with irqs off because there are hangup path
44 * callers who will do ldisc lookups and cannot sleep.
45 */
46
6fa3eb70 47static DEFINE_RAW_SPINLOCK(tty_ldiscs_lock);
01e1abb2
AC
48/* Line disc dispatch table */
49static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS];
50
51/**
52 * tty_register_ldisc - install a line discipline
53 * @disc: ldisc number
54 * @new_ldisc: pointer to the ldisc object
55 *
56 * Installs a new line discipline into the kernel. The discipline
57 * is set up as unreferenced and then made available to the kernel
58 * from this point onwards.
59 *
60 * Locking:
6fa3eb70 61 * takes tty_ldiscs_lock to guard against ldisc races
01e1abb2
AC
62 */
63
64int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc)
65{
66 unsigned long flags;
67 int ret = 0;
68
69 if (disc < N_TTY || disc >= NR_LDISCS)
70 return -EINVAL;
71
6fa3eb70 72 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
01e1abb2
AC
73 tty_ldiscs[disc] = new_ldisc;
74 new_ldisc->num = disc;
75 new_ldisc->refcount = 0;
6fa3eb70 76 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
01e1abb2
AC
77
78 return ret;
79}
80EXPORT_SYMBOL(tty_register_ldisc);
81
82/**
83 * tty_unregister_ldisc - unload a line discipline
84 * @disc: ldisc number
85 * @new_ldisc: pointer to the ldisc object
86 *
87 * Remove a line discipline from the kernel providing it is not
88 * currently in use.
89 *
90 * Locking:
6fa3eb70 91 * takes tty_ldiscs_lock to guard against ldisc races
01e1abb2
AC
92 */
93
94int tty_unregister_ldisc(int disc)
95{
96 unsigned long flags;
97 int ret = 0;
98
99 if (disc < N_TTY || disc >= NR_LDISCS)
100 return -EINVAL;
101
6fa3eb70 102 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
01e1abb2
AC
103 if (tty_ldiscs[disc]->refcount)
104 ret = -EBUSY;
105 else
106 tty_ldiscs[disc] = NULL;
6fa3eb70 107 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
01e1abb2
AC
108
109 return ret;
110}
111EXPORT_SYMBOL(tty_unregister_ldisc);
112
f0de0e8d
LT
113static struct tty_ldisc_ops *get_ldops(int disc)
114{
115 unsigned long flags;
116 struct tty_ldisc_ops *ldops, *ret;
117
6fa3eb70 118 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
f0de0e8d
LT
119 ret = ERR_PTR(-EINVAL);
120 ldops = tty_ldiscs[disc];
121 if (ldops) {
122 ret = ERR_PTR(-EAGAIN);
123 if (try_module_get(ldops->owner)) {
124 ldops->refcount++;
125 ret = ldops;
126 }
127 }
6fa3eb70 128 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
f0de0e8d
LT
129 return ret;
130}
131
132static void put_ldops(struct tty_ldisc_ops *ldops)
133{
134 unsigned long flags;
135
6fa3eb70 136 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
f0de0e8d
LT
137 ldops->refcount--;
138 module_put(ldops->owner);
6fa3eb70 139 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
f0de0e8d 140}
01e1abb2 141
01e1abb2
AC
142/**
143 * tty_ldisc_get - take a reference to an ldisc
144 * @disc: ldisc number
01e1abb2
AC
145 *
146 * Takes a reference to a line discipline. Deals with refcounts and
147 * module locking counts. Returns NULL if the discipline is not available.
148 * Returns a pointer to the discipline and bumps the ref count if it is
149 * available
150 *
151 * Locking:
6fa3eb70 152 * takes tty_ldiscs_lock to guard against ldisc races
01e1abb2
AC
153 */
154
6fa3eb70 155static struct tty_ldisc *tty_ldisc_get(struct tty_struct *tty, int disc)
01e1abb2 156{
c65c9bc3 157 struct tty_ldisc *ld;
182274f8 158 struct tty_ldisc_ops *ldops;
01e1abb2
AC
159
160 if (disc < N_TTY || disc >= NR_LDISCS)
c65c9bc3 161 return ERR_PTR(-EINVAL);
182274f8
LT
162
163 /*
164 * Get the ldisc ops - we may need to request them to be loaded
165 * dynamically and try again.
166 */
167 ldops = get_ldops(disc);
168 if (IS_ERR(ldops)) {
01e1abb2 169 request_module("tty-ldisc-%d", disc);
182274f8
LT
170 ldops = get_ldops(disc);
171 if (IS_ERR(ldops))
172 return ERR_CAST(ldops);
173 }
174
175 ld = kmalloc(sizeof(struct tty_ldisc), GFP_KERNEL);
176 if (ld == NULL) {
177 put_ldops(ldops);
178 return ERR_PTR(-ENOMEM);
01e1abb2 179 }
182274f8
LT
180
181 ld->ops = ldops;
6fa3eb70 182 ld->tty = tty;
1541f845 183
c65c9bc3 184 return ld;
01e1abb2
AC
185}
186
734de249
PH
187/**
188 * tty_ldisc_put - release the ldisc
189 *
190 * Complement of tty_ldisc_get().
191 */
192static inline void tty_ldisc_put(struct tty_ldisc *ld)
193{
734de249
PH
194 if (WARN_ON_ONCE(!ld))
195 return;
196
6fa3eb70 197 put_ldops(ld->ops);
734de249 198 kfree(ld);
734de249
PH
199}
200
852e99d2 201static void *tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos)
01e1abb2
AC
202{
203 return (*pos < NR_LDISCS) ? pos : NULL;
204}
205
852e99d2 206static void *tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos)
01e1abb2
AC
207{
208 (*pos)++;
209 return (*pos < NR_LDISCS) ? pos : NULL;
210}
211
212static void tty_ldiscs_seq_stop(struct seq_file *m, void *v)
213{
214}
215
216static int tty_ldiscs_seq_show(struct seq_file *m, void *v)
217{
218 int i = *(loff_t *)v;
f0de0e8d 219 struct tty_ldisc_ops *ldops;
852e99d2 220
f0de0e8d
LT
221 ldops = get_ldops(i);
222 if (IS_ERR(ldops))
01e1abb2 223 return 0;
f0de0e8d
LT
224 seq_printf(m, "%-10s %2d\n", ldops->name ? ldops->name : "???", i);
225 put_ldops(ldops);
01e1abb2
AC
226 return 0;
227}
228
229static const struct seq_operations tty_ldiscs_seq_ops = {
230 .start = tty_ldiscs_seq_start,
231 .next = tty_ldiscs_seq_next,
232 .stop = tty_ldiscs_seq_stop,
233 .show = tty_ldiscs_seq_show,
234};
235
236static int proc_tty_ldiscs_open(struct inode *inode, struct file *file)
237{
238 return seq_open(file, &tty_ldiscs_seq_ops);
239}
240
241const struct file_operations tty_ldiscs_proc_fops = {
242 .owner = THIS_MODULE,
243 .open = proc_tty_ldiscs_open,
244 .read = seq_read,
245 .llseek = seq_lseek,
246 .release = seq_release,
247};
248
01e1abb2
AC
249/**
250 * tty_ldisc_ref_wait - wait for the tty ldisc
251 * @tty: tty device
252 *
253 * Dereference the line discipline for the terminal and take a
254 * reference to it. If the line discipline is in flux then
255 * wait patiently until it changes.
256 *
257 * Note: Must not be called from an IRQ/timer context. The caller
258 * must also be careful not to hold other locks that will deadlock
259 * against a discipline change, such as an existing ldisc reference
260 * (which we check for)
261 *
6fa3eb70
S
262 * Note: only callable from a file_operations routine (which
263 * guarantees tty->ldisc != NULL when the lock is acquired).
01e1abb2
AC
264 */
265
266struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
267{
6fa3eb70
S
268 ldsem_down_read(&tty->ldisc_sem, MAX_SCHEDULE_TIMEOUT);
269 WARN_ON(!tty->ldisc);
270 return tty->ldisc;
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{
6fa3eb70
S
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{
6fa3eb70
S
306 ldsem_up_read(&ld->tty->ldisc_sem);
307}
308EXPORT_SYMBOL_GPL(tty_ldisc_deref);
ebc9baed 309
ebc9baed 310
6fa3eb70
S
311static inline int __lockfunc
312tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout)
313{
314 return ldsem_down_write(&tty->ldisc_sem, timeout);
315}
ebc9baed 316
6fa3eb70
S
317static inline int __lockfunc
318tty_ldisc_lock_nested(struct tty_struct *tty, unsigned long timeout)
319{
320 return ldsem_down_write_nested(&tty->ldisc_sem,
321 LDISC_SEM_OTHER, timeout);
01e1abb2 322}
01e1abb2 323
6fa3eb70
S
324static inline void tty_ldisc_unlock(struct tty_struct *tty)
325{
326 return ldsem_up_write(&tty->ldisc_sem);
327}
328
329static int __lockfunc
330tty_ldisc_lock_pair_timeout(struct tty_struct *tty, struct tty_struct *tty2,
331 unsigned long timeout)
332{
333 int ret;
334
335 if (tty < tty2) {
336 ret = tty_ldisc_lock(tty, timeout);
337 if (ret) {
338 ret = tty_ldisc_lock_nested(tty2, timeout);
339 if (!ret)
340 tty_ldisc_unlock(tty);
341 }
342 } else {
343 /* if this is possible, it has lots of implications */
344 WARN_ON_ONCE(tty == tty2);
345 if (tty2 && tty != tty2) {
346 ret = tty_ldisc_lock(tty2, timeout);
347 if (ret) {
348 ret = tty_ldisc_lock_nested(tty, timeout);
349 if (!ret)
350 tty_ldisc_unlock(tty2);
351 }
352 } else
353 ret = tty_ldisc_lock(tty, timeout);
354 }
355
356 if (!ret)
357 return -EBUSY;
358
359 set_bit(TTY_LDISC_HALTED, &tty->flags);
360 if (tty2)
361 set_bit(TTY_LDISC_HALTED, &tty2->flags);
362 return 0;
363}
364
365static void __lockfunc
366tty_ldisc_lock_pair(struct tty_struct *tty, struct tty_struct *tty2)
367{
368 tty_ldisc_lock_pair_timeout(tty, tty2, MAX_SCHEDULE_TIMEOUT);
369}
01e1abb2 370
6fa3eb70
S
371static void __lockfunc tty_ldisc_unlock_pair(struct tty_struct *tty,
372 struct tty_struct *tty2)
373{
374 tty_ldisc_unlock(tty);
375 if (tty2)
376 tty_ldisc_unlock(tty2);
377}
378
379static void __lockfunc tty_ldisc_enable_pair(struct tty_struct *tty,
380 struct tty_struct *tty2)
01e1abb2 381{
21622939 382 clear_bit(TTY_LDISC_HALTED, &tty->flags);
6fa3eb70
S
383 if (tty2)
384 clear_bit(TTY_LDISC_HALTED, &tty2->flags);
385
386 tty_ldisc_unlock_pair(tty, tty2);
01e1abb2
AC
387}
388
f2c4c65c
AC
389/**
390 * tty_ldisc_flush - flush line discipline queue
391 * @tty: tty
392 *
393 * Flush the line discipline queue (if any) for this tty. If there
394 * is no line discipline active this is a no-op.
395 */
396
397void tty_ldisc_flush(struct tty_struct *tty)
398{
399 struct tty_ldisc *ld = tty_ldisc_ref(tty);
400 if (ld) {
401 if (ld->ops->flush_buffer)
402 ld->ops->flush_buffer(tty);
403 tty_ldisc_deref(ld);
404 }
405 tty_buffer_flush(tty);
406}
f2c4c65c
AC
407EXPORT_SYMBOL_GPL(tty_ldisc_flush);
408
01e1abb2
AC
409/**
410 * tty_set_termios_ldisc - set ldisc field
411 * @tty: tty structure
412 * @num: line discipline number
413 *
414 * This is probably overkill for real world processors but
415 * they are not on hot paths so a little discipline won't do
416 * any harm.
417 *
4b9e9796
S
418 * The line discipline-related tty_struct fields are reset to
419 * prevent the ldisc driver from re-using stale information for
420 * the new ldisc instance.
421 *
01e1abb2
AC
422 * Locking: takes termios_mutex
423 */
424
425static void tty_set_termios_ldisc(struct tty_struct *tty, int num)
426{
427 mutex_lock(&tty->termios_mutex);
adc8d746 428 tty->termios.c_line = num;
01e1abb2 429 mutex_unlock(&tty->termios_mutex);
4b9e9796
S
430
431 tty->disc_data = NULL;
432 tty->receive_room = 0;
01e1abb2
AC
433}
434
c65c9bc3
AC
435/**
436 * tty_ldisc_open - open a line discipline
437 * @tty: tty we are opening the ldisc on
438 * @ld: discipline to open
439 *
440 * A helper opening method. Also a convenient debugging and check
441 * point.
ec79d605
AB
442 *
443 * Locking: always called with BTM already held.
c65c9bc3
AC
444 */
445
446static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld)
447{
448 WARN_ON(test_and_set_bit(TTY_LDISC_OPEN, &tty->flags));
f18f9498
AC
449 if (ld->ops->open) {
450 int ret;
ec79d605 451 /* BTM here locks versus a hangup event */
f18f9498 452 ret = ld->ops->open(tty);
7f90cfc5
JS
453 if (ret)
454 clear_bit(TTY_LDISC_OPEN, &tty->flags);
f18f9498
AC
455 return ret;
456 }
c65c9bc3
AC
457 return 0;
458}
459
460/**
461 * tty_ldisc_close - close a line discipline
462 * @tty: tty we are opening the ldisc on
463 * @ld: discipline to close
464 *
465 * A helper close method. Also a convenient debugging and check
466 * point.
467 */
468
469static void tty_ldisc_close(struct tty_struct *tty, struct tty_ldisc *ld)
470{
471 WARN_ON(!test_bit(TTY_LDISC_OPEN, &tty->flags));
472 clear_bit(TTY_LDISC_OPEN, &tty->flags);
473 if (ld->ops->close)
474 ld->ops->close(tty);
475}
01e1abb2
AC
476
477/**
478 * tty_ldisc_restore - helper for tty ldisc change
479 * @tty: tty to recover
480 * @old: previous ldisc
481 *
482 * Restore the previous line discipline or N_TTY when a line discipline
483 * change fails due to an open error
484 */
485
486static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old)
487{
488 char buf[64];
c65c9bc3
AC
489 struct tty_ldisc *new_ldisc;
490 int r;
01e1abb2
AC
491
492 /* There is an outstanding reference here so this is safe */
6fa3eb70 493 old = tty_ldisc_get(tty, old->ops->num);
c65c9bc3 494 WARN_ON(IS_ERR(old));
f4807045 495 tty->ldisc = old;
01e1abb2 496 tty_set_termios_ldisc(tty, old->ops->num);
c65c9bc3
AC
497 if (tty_ldisc_open(tty, old) < 0) {
498 tty_ldisc_put(old);
01e1abb2 499 /* This driver is always present */
6fa3eb70 500 new_ldisc = tty_ldisc_get(tty, N_TTY);
c65c9bc3 501 if (IS_ERR(new_ldisc))
01e1abb2 502 panic("n_tty: get");
f4807045 503 tty->ldisc = new_ldisc;
01e1abb2 504 tty_set_termios_ldisc(tty, N_TTY);
c65c9bc3
AC
505 r = tty_ldisc_open(tty, new_ldisc);
506 if (r < 0)
507 panic("Couldn't open N_TTY ldisc for "
508 "%s --- error %d.",
509 tty_name(tty, buf), r);
01e1abb2
AC
510 }
511}
512
513/**
514 * tty_set_ldisc - set line discipline
515 * @tty: the terminal to set
516 * @ldisc: the line discipline
517 *
518 * Set the discipline of a tty line. Must be called from a process
c65c9bc3
AC
519 * context. The ldisc change logic has to protect itself against any
520 * overlapping ldisc change (including on the other end of pty pairs),
521 * the close of one side of a tty/pty pair, and eventually hangup.
01e1abb2
AC
522 */
523
524int tty_set_ldisc(struct tty_struct *tty, int ldisc)
525{
526 int retval;
c65c9bc3 527 struct tty_ldisc *o_ldisc, *new_ldisc;
6fa3eb70 528 struct tty_struct *o_tty = tty->link;
01e1abb2 529
6fa3eb70 530 new_ldisc = tty_ldisc_get(tty, ldisc);
c65c9bc3
AC
531 if (IS_ERR(new_ldisc))
532 return PTR_ERR(new_ldisc);
01e1abb2 533
6fa3eb70
S
534 retval = tty_ldisc_lock_pair_timeout(tty, o_tty, 5 * HZ);
535 if (retval) {
536 tty_ldisc_put(new_ldisc);
537 return retval;
538 }
01e1abb2 539
c65c9bc3
AC
540 /*
541 * Check the no-op case
542 */
543
544 if (tty->ldisc->ops->num == ldisc) {
6fa3eb70 545 tty_ldisc_enable_pair(tty, o_tty);
c65c9bc3 546 tty_ldisc_put(new_ldisc);
01e1abb2
AC
547 return 0;
548 }
549
6fa3eb70 550 /* FIXME: why 'shutoff' input if the ldisc is locked? */
01e1abb2
AC
551 tty->receive_room = 0;
552
553 o_ldisc = tty->ldisc;
89c8d91e 554 tty_lock(tty);
100eeae2 555
6fa3eb70
S
556 /* FIXME: for testing only */
557 WARN_ON(test_bit(TTY_HUPPED, &tty->flags));
100eeae2 558
40c9f61e 559 if (test_bit(TTY_HUPPING, &tty->flags)) {
c65c9bc3
AC
560 /* We were raced by the hangup method. It will have stomped
561 the ldisc data and closed the ldisc down */
6fa3eb70 562 tty_ldisc_enable_pair(tty, o_tty);
c65c9bc3 563 tty_ldisc_put(new_ldisc);
89c8d91e 564 tty_unlock(tty);
c65c9bc3
AC
565 return -EIO;
566 }
567
01e1abb2 568 /* Shutdown the current discipline. */
c65c9bc3 569 tty_ldisc_close(tty, o_ldisc);
01e1abb2
AC
570
571 /* Now set up the new line discipline. */
f4807045 572 tty->ldisc = new_ldisc;
01e1abb2 573 tty_set_termios_ldisc(tty, ldisc);
c65c9bc3
AC
574
575 retval = tty_ldisc_open(tty, new_ldisc);
01e1abb2 576 if (retval < 0) {
c65c9bc3
AC
577 /* Back to the old one or N_TTY if we can't */
578 tty_ldisc_put(new_ldisc);
579 tty_ldisc_restore(tty, o_ldisc);
01e1abb2 580 }
c65c9bc3 581
01e1abb2
AC
582 /* At this point we hold a reference to the new ldisc and a
583 a reference to the old ldisc. If we ended up flipping back
584 to the existing ldisc we have two references to it */
585
c65c9bc3 586 if (tty->ldisc->ops->num != o_ldisc->ops->num && tty->ops->set_ldisc)
01e1abb2
AC
587 tty->ops->set_ldisc(tty);
588
c65c9bc3 589 tty_ldisc_put(o_ldisc);
01e1abb2
AC
590
591 /*
c65c9bc3 592 * Allow ldisc referencing to occur again
01e1abb2 593 */
6fa3eb70 594 tty_ldisc_enable_pair(tty, o_tty);
01e1abb2 595
c65c9bc3 596 /* Restart the work queue in case no characters kick it off. Safe if
01e1abb2 597 already running */
4f98d467
PH
598 schedule_work(&tty->port->buf.work);
599 if (o_tty)
ecbbfd44 600 schedule_work(&o_tty->port->buf.work);
4f98d467 601
89c8d91e 602 tty_unlock(tty);
01e1abb2
AC
603 return retval;
604}
605
c65c9bc3
AC
606/**
607 * tty_reset_termios - reset terminal state
608 * @tty: tty to reset
609 *
610 * Restore a terminal to the driver default state.
611 */
612
613static void tty_reset_termios(struct tty_struct *tty)
614{
615 mutex_lock(&tty->termios_mutex);
adc8d746
AC
616 tty->termios = tty->driver->init_termios;
617 tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios);
618 tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios);
c65c9bc3
AC
619 mutex_unlock(&tty->termios_mutex);
620}
621
622
623/**
624 * tty_ldisc_reinit - reinitialise the tty ldisc
625 * @tty: tty to reinit
638b9648 626 * @ldisc: line discipline to reinitialize
c65c9bc3 627 *
638b9648
AC
628 * Switch the tty to a line discipline and leave the ldisc
629 * state closed
c65c9bc3
AC
630 */
631
1c95ba1e 632static int tty_ldisc_reinit(struct tty_struct *tty, int ldisc)
c65c9bc3 633{
6fa3eb70 634 struct tty_ldisc *ld = tty_ldisc_get(tty, ldisc);
1c95ba1e
PR
635
636 if (IS_ERR(ld))
637 return -1;
c65c9bc3
AC
638
639 tty_ldisc_close(tty, tty->ldisc);
640 tty_ldisc_put(tty->ldisc);
c65c9bc3
AC
641 /*
642 * Switch the line discipline back
643 */
f4807045 644 tty->ldisc = ld;
638b9648 645 tty_set_termios_ldisc(tty, ldisc);
1c95ba1e
PR
646
647 return 0;
c65c9bc3
AC
648}
649
650/**
651 * tty_ldisc_hangup - hangup ldisc reset
652 * @tty: tty being hung up
653 *
654 * Some tty devices reset their termios when they receive a hangup
655 * event. In that situation we must also switch back to N_TTY properly
656 * before we reset the termios data.
657 *
658 * Locking: We can take the ldisc mutex as the rest of the code is
659 * careful to allow for this.
660 *
661 * In the pty pair case this occurs in the close() path of the
662 * tty itself so we must be careful about locking rules.
663 */
664
665void tty_ldisc_hangup(struct tty_struct *tty)
666{
667 struct tty_ldisc *ld;
638b9648
AC
668 int reset = tty->driver->flags & TTY_DRIVER_RESET_TERMIOS;
669 int err = 0;
c65c9bc3 670
fc575ee6
PH
671 tty_ldisc_debug(tty, "closing ldisc: %p\n", tty->ldisc);
672
c65c9bc3
AC
673 ld = tty_ldisc_ref(tty);
674 if (ld != NULL) {
c65c9bc3
AC
675 if (ld->ops->flush_buffer)
676 ld->ops->flush_buffer(tty);
677 tty_driver_flush_buffer(tty);
678 if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
679 ld->ops->write_wakeup)
680 ld->ops->write_wakeup(tty);
681 if (ld->ops->hangup)
682 ld->ops->hangup(tty);
683 tty_ldisc_deref(ld);
684 }
6fa3eb70 685
c65c9bc3
AC
686 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
687 wake_up_interruptible_poll(&tty->read_wait, POLLIN);
6fa3eb70
S
688
689 tty_unlock(tty);
690
c65c9bc3
AC
691 /*
692 * Shutdown the current line discipline, and reset it to
638b9648
AC
693 * N_TTY if need be.
694 *
695 * Avoid racing set_ldisc or tty_ldisc_release
c65c9bc3 696 */
6fa3eb70
S
697 tty_ldisc_lock_pair(tty, tty->link);
698 tty_lock(tty);
60af22d2 699
6fa3eb70 700 if (tty->ldisc) {
c8785241
PH
701
702 /* At this point we have a halted ldisc; we want to close it and
703 reopen a new ldisc. We could defer the reopen to the next
704 open but it means auditing a lot of other paths so this is
705 a FIXME */
638b9648 706 if (reset == 0) {
1c95ba1e 707
adc8d746 708 if (!tty_ldisc_reinit(tty, tty->termios.c_line))
1c95ba1e
PR
709 err = tty_ldisc_open(tty, tty->ldisc);
710 else
711 err = 1;
638b9648
AC
712 }
713 /* If the re-open fails or we reset then go to N_TTY. The
714 N_TTY open cannot fail */
715 if (reset || err) {
1c95ba1e 716 BUG_ON(tty_ldisc_reinit(tty, N_TTY));
c8d50041 717 WARN_ON(tty_ldisc_open(tty, tty->ldisc));
c8d50041 718 }
c65c9bc3 719 }
6fa3eb70 720 tty_ldisc_enable_pair(tty, tty->link);
638b9648
AC
721 if (reset)
722 tty_reset_termios(tty);
fc575ee6
PH
723
724 tty_ldisc_debug(tty, "re-opened ldisc: %p\n", tty->ldisc);
c65c9bc3 725}
01e1abb2
AC
726
727/**
728 * tty_ldisc_setup - open line discipline
729 * @tty: tty being shut down
730 * @o_tty: pair tty for pty/tty pairs
731 *
732 * Called during the initial open of a tty/pty pair in order to set up the
c65c9bc3
AC
733 * line disciplines and bind them to the tty. This has no locking issues
734 * as the device isn't yet active.
01e1abb2
AC
735 */
736
737int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty)
738{
c65c9bc3 739 struct tty_ldisc *ld = tty->ldisc;
01e1abb2
AC
740 int retval;
741
c65c9bc3
AC
742 retval = tty_ldisc_open(tty, ld);
743 if (retval)
744 return retval;
745
746 if (o_tty) {
747 retval = tty_ldisc_open(o_tty, o_tty->ldisc);
01e1abb2 748 if (retval) {
c65c9bc3 749 tty_ldisc_close(tty, ld);
01e1abb2
AC
750 return retval;
751 }
01e1abb2 752 }
01e1abb2
AC
753 return 0;
754}
89c8d91e
AC
755
756static void tty_ldisc_kill(struct tty_struct *tty)
757{
89c8d91e
AC
758 /*
759 * Now kill off the ldisc
760 */
761 tty_ldisc_close(tty, tty->ldisc);
762 tty_ldisc_put(tty->ldisc);
763 /* Force an oops if we mess this up */
764 tty->ldisc = NULL;
765
766 /* Ensure the next open requests the N_TTY ldisc */
767 tty_set_termios_ldisc(tty, N_TTY);
89c8d91e
AC
768}
769
01e1abb2
AC
770/**
771 * tty_ldisc_release - release line discipline
772 * @tty: tty being shut down
773 * @o_tty: pair tty for pty/tty pairs
774 *
852e99d2
AC
775 * Called during the final close of a tty/pty pair in order to shut down
776 * the line discpline layer. On exit the ldisc assigned is N_TTY and the
c65c9bc3 777 * ldisc has not been opened.
01e1abb2
AC
778 */
779
780void tty_ldisc_release(struct tty_struct *tty, struct tty_struct *o_tty)
781{
01e1abb2 782 /*
a2965b7b
PH
783 * Shutdown this line discipline. As this is the final close,
784 * it does not race with the set_ldisc code path.
01e1abb2 785 */
01e1abb2 786
fc575ee6
PH
787 tty_ldisc_debug(tty, "closing ldisc: %p\n", tty->ldisc);
788
6fa3eb70 789 tty_ldisc_lock_pair(tty, o_tty);
852e4a81 790 tty_lock_pair(tty, o_tty);
6fa3eb70 791
89c8d91e 792 tty_ldisc_kill(tty);
c65c9bc3 793 if (o_tty)
89c8d91e 794 tty_ldisc_kill(o_tty);
aef29bc2 795
89c8d91e 796 tty_unlock_pair(tty, o_tty);
6fa3eb70
S
797 tty_ldisc_unlock_pair(tty, o_tty);
798
aef29bc2
AC
799 /* And the memory resources remaining (buffers, termios) will be
800 disposed of when the kref hits zero */
fc575ee6
PH
801
802 tty_ldisc_debug(tty, "ldisc closed\n");
01e1abb2
AC
803}
804
805/**
806 * tty_ldisc_init - ldisc setup for new tty
807 * @tty: tty being allocated
808 *
809 * Set up the line discipline objects for a newly allocated tty. Note that
810 * the tty structure is not completely set up when this call is made.
811 */
812
813void tty_ldisc_init(struct tty_struct *tty)
814{
6fa3eb70 815 struct tty_ldisc *ld = tty_ldisc_get(tty, N_TTY);
c65c9bc3 816 if (IS_ERR(ld))
01e1abb2 817 panic("n_tty: init_tty");
f4807045 818 tty->ldisc = ld;
01e1abb2
AC
819}
820
6716671d
JS
821/**
822 * tty_ldisc_init - ldisc cleanup for new tty
823 * @tty: tty that was allocated recently
824 *
825 * The tty structure must not becompletely set up (tty_ldisc_setup) when
826 * this call is made.
827 */
828void tty_ldisc_deinit(struct tty_struct *tty)
829{
ebc9baed 830 tty_ldisc_put(tty->ldisc);
f4807045 831 tty->ldisc = NULL;
6716671d
JS
832}
833
01e1abb2
AC
834void tty_ldisc_begin(void)
835{
836 /* Setup the default TTY line discipline. */
837 (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
838}