blob: f26ef1ace4f115fe37f1440ea39c38ff38902825 [file] [log] [blame]
Alan Cox01e1abb2008-07-22 11:16:55 +01001#include <linux/types.h>
Alan Cox01e1abb2008-07-22 11:16:55 +01002#include <linux/errno.h>
Jiri Slaby8b3ffa12011-11-16 16:27:10 +01003#include <linux/kmod.h>
Alan Cox01e1abb2008-07-22 11:16:55 +01004#include <linux/sched.h>
5#include <linux/interrupt.h>
6#include <linux/tty.h>
7#include <linux/tty_driver.h>
Alan Cox01e1abb2008-07-22 11:16:55 +01008#include <linux/file.h>
Alan Cox01e1abb2008-07-22 11:16:55 +01009#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>
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
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
Alan Cox01e1abb2008-07-22 11:16:55 +010034/*
35 * This guards the refcounted line discipline lists. The lock
36 * must be taken with irqs off because there are hangup path
37 * callers who will do ldisc lookups and cannot sleep.
38 */
39
Ivo Siebenc9739942012-10-17 14:03:14 +020040static DEFINE_RAW_SPINLOCK(tty_ldisc_lock);
Alan Cox01e1abb2008-07-22 11:16:55 +010041static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_wait);
42/* Line disc dispatch table */
43static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS];
44
45/**
46 * tty_register_ldisc - install a line discipline
47 * @disc: ldisc number
48 * @new_ldisc: pointer to the ldisc object
49 *
50 * Installs a new line discipline into the kernel. The discipline
51 * is set up as unreferenced and then made available to the kernel
52 * from this point onwards.
53 *
54 * Locking:
55 * takes tty_ldisc_lock to guard against ldisc races
56 */
57
58int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc)
59{
60 unsigned long flags;
61 int ret = 0;
62
63 if (disc < N_TTY || disc >= NR_LDISCS)
64 return -EINVAL;
65
Ivo Siebenc9739942012-10-17 14:03:14 +020066 raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
Alan Cox01e1abb2008-07-22 11:16:55 +010067 tty_ldiscs[disc] = new_ldisc;
68 new_ldisc->num = disc;
69 new_ldisc->refcount = 0;
Ivo Siebenc9739942012-10-17 14:03:14 +020070 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
Alan Cox01e1abb2008-07-22 11:16:55 +010071
72 return ret;
73}
74EXPORT_SYMBOL(tty_register_ldisc);
75
76/**
77 * tty_unregister_ldisc - unload a line discipline
78 * @disc: ldisc number
79 * @new_ldisc: pointer to the ldisc object
80 *
81 * Remove a line discipline from the kernel providing it is not
82 * currently in use.
83 *
84 * Locking:
85 * takes tty_ldisc_lock to guard against ldisc races
86 */
87
88int tty_unregister_ldisc(int disc)
89{
90 unsigned long flags;
91 int ret = 0;
92
93 if (disc < N_TTY || disc >= NR_LDISCS)
94 return -EINVAL;
95
Ivo Siebenc9739942012-10-17 14:03:14 +020096 raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
Alan Cox01e1abb2008-07-22 11:16:55 +010097 if (tty_ldiscs[disc]->refcount)
98 ret = -EBUSY;
99 else
100 tty_ldiscs[disc] = NULL;
Ivo Siebenc9739942012-10-17 14:03:14 +0200101 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
Alan Cox01e1abb2008-07-22 11:16:55 +0100102
103 return ret;
104}
105EXPORT_SYMBOL(tty_unregister_ldisc);
106
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700107static struct tty_ldisc_ops *get_ldops(int disc)
108{
109 unsigned long flags;
110 struct tty_ldisc_ops *ldops, *ret;
111
Ivo Siebenc9739942012-10-17 14:03:14 +0200112 raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700113 ret = ERR_PTR(-EINVAL);
114 ldops = tty_ldiscs[disc];
115 if (ldops) {
116 ret = ERR_PTR(-EAGAIN);
117 if (try_module_get(ldops->owner)) {
118 ldops->refcount++;
119 ret = ldops;
120 }
121 }
Ivo Siebenc9739942012-10-17 14:03:14 +0200122 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700123 return ret;
124}
125
126static void put_ldops(struct tty_ldisc_ops *ldops)
127{
128 unsigned long flags;
129
Ivo Siebenc9739942012-10-17 14:03:14 +0200130 raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700131 ldops->refcount--;
132 module_put(ldops->owner);
Ivo Siebenc9739942012-10-17 14:03:14 +0200133 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700134}
Alan Cox01e1abb2008-07-22 11:16:55 +0100135
136/**
Alan Cox01e1abb2008-07-22 11:16:55 +0100137 * tty_ldisc_get - take a reference to an ldisc
138 * @disc: ldisc number
Alan Cox01e1abb2008-07-22 11:16:55 +0100139 *
140 * Takes a reference to a line discipline. Deals with refcounts and
141 * module locking counts. Returns NULL if the discipline is not available.
142 * Returns a pointer to the discipline and bumps the ref count if it is
143 * available
144 *
145 * Locking:
146 * takes tty_ldisc_lock to guard against ldisc races
147 */
148
Alan Coxc65c9bc2009-06-11 12:50:12 +0100149static struct tty_ldisc *tty_ldisc_get(int disc)
Alan Cox01e1abb2008-07-22 11:16:55 +0100150{
Alan Coxc65c9bc2009-06-11 12:50:12 +0100151 struct tty_ldisc *ld;
Linus Torvalds182274f2009-08-03 16:01:28 -0700152 struct tty_ldisc_ops *ldops;
Alan Cox01e1abb2008-07-22 11:16:55 +0100153
154 if (disc < N_TTY || disc >= NR_LDISCS)
Alan Coxc65c9bc2009-06-11 12:50:12 +0100155 return ERR_PTR(-EINVAL);
Linus Torvalds182274f2009-08-03 16:01:28 -0700156
157 /*
158 * Get the ldisc ops - we may need to request them to be loaded
159 * dynamically and try again.
160 */
161 ldops = get_ldops(disc);
162 if (IS_ERR(ldops)) {
Alan Cox01e1abb2008-07-22 11:16:55 +0100163 request_module("tty-ldisc-%d", disc);
Linus Torvalds182274f2009-08-03 16:01:28 -0700164 ldops = get_ldops(disc);
165 if (IS_ERR(ldops))
166 return ERR_CAST(ldops);
Alan Cox01e1abb2008-07-22 11:16:55 +0100167 }
Linus Torvalds182274f2009-08-03 16:01:28 -0700168
169 ld = kmalloc(sizeof(struct tty_ldisc), GFP_KERNEL);
170 if (ld == NULL) {
171 put_ldops(ldops);
172 return ERR_PTR(-ENOMEM);
173 }
174
175 ld->ops = ldops;
176 atomic_set(&ld->users, 1);
Ivo Sieben1541f842012-05-03 14:37:43 +0200177 init_waitqueue_head(&ld->wq_idle);
178
Alan Coxc65c9bc2009-06-11 12:50:12 +0100179 return ld;
Alan Cox01e1abb2008-07-22 11:16:55 +0100180}
181
Alan Cox852e99d2009-06-11 12:51:41 +0100182static void *tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos)
Alan Cox01e1abb2008-07-22 11:16:55 +0100183{
184 return (*pos < NR_LDISCS) ? pos : NULL;
185}
186
Alan Cox852e99d2009-06-11 12:51:41 +0100187static void *tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos)
Alan Cox01e1abb2008-07-22 11:16:55 +0100188{
189 (*pos)++;
190 return (*pos < NR_LDISCS) ? pos : NULL;
191}
192
193static void tty_ldiscs_seq_stop(struct seq_file *m, void *v)
194{
195}
196
197static int tty_ldiscs_seq_show(struct seq_file *m, void *v)
198{
199 int i = *(loff_t *)v;
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700200 struct tty_ldisc_ops *ldops;
Alan Cox852e99d2009-06-11 12:51:41 +0100201
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700202 ldops = get_ldops(i);
203 if (IS_ERR(ldops))
Alan Cox01e1abb2008-07-22 11:16:55 +0100204 return 0;
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700205 seq_printf(m, "%-10s %2d\n", ldops->name ? ldops->name : "???", i);
206 put_ldops(ldops);
Alan Cox01e1abb2008-07-22 11:16:55 +0100207 return 0;
208}
209
210static const struct seq_operations tty_ldiscs_seq_ops = {
211 .start = tty_ldiscs_seq_start,
212 .next = tty_ldiscs_seq_next,
213 .stop = tty_ldiscs_seq_stop,
214 .show = tty_ldiscs_seq_show,
215};
216
217static int proc_tty_ldiscs_open(struct inode *inode, struct file *file)
218{
219 return seq_open(file, &tty_ldiscs_seq_ops);
220}
221
222const struct file_operations tty_ldiscs_proc_fops = {
223 .owner = THIS_MODULE,
224 .open = proc_tty_ldiscs_open,
225 .read = seq_read,
226 .llseek = seq_lseek,
227 .release = seq_release,
228};
229
230/**
Alan Cox01e1abb2008-07-22 11:16:55 +0100231 * tty_ldisc_try - internal helper
232 * @tty: the tty
233 *
234 * Make a single attempt to grab and bump the refcount on
235 * the tty ldisc. Return 0 on failure or 1 on success. This is
236 * used to implement both the waiting and non waiting versions
237 * of tty_ldisc_ref
238 *
239 * Locking: takes tty_ldisc_lock
240 */
241
Linus Torvalds65b77042009-08-03 11:11:19 -0700242static struct tty_ldisc *tty_ldisc_try(struct tty_struct *tty)
Alan Cox01e1abb2008-07-22 11:16:55 +0100243{
244 unsigned long flags;
245 struct tty_ldisc *ld;
Alan Cox01e1abb2008-07-22 11:16:55 +0100246
Peter Hurley16759f62013-03-11 16:44:41 -0400247 /* FIXME: this allows reference acquire after TTY_LDISC is cleared */
Ivo Siebenc9739942012-10-17 14:03:14 +0200248 raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
Linus Torvalds65b77042009-08-03 11:11:19 -0700249 ld = NULL;
Peter Hurley16759f62013-03-11 16:44:41 -0400250 if (test_bit(TTY_LDISC, &tty->flags) && tty->ldisc) {
251 ld = tty->ldisc;
252 atomic_inc(&ld->users);
253 }
Ivo Siebenc9739942012-10-17 14:03:14 +0200254 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
Linus Torvalds65b77042009-08-03 11:11:19 -0700255 return ld;
Alan Cox01e1abb2008-07-22 11:16:55 +0100256}
257
258/**
259 * tty_ldisc_ref_wait - wait for the tty ldisc
260 * @tty: tty device
261 *
262 * Dereference the line discipline for the terminal and take a
263 * reference to it. If the line discipline is in flux then
264 * wait patiently until it changes.
265 *
266 * Note: Must not be called from an IRQ/timer context. The caller
267 * must also be careful not to hold other locks that will deadlock
268 * against a discipline change, such as an existing ldisc reference
269 * (which we check for)
270 *
271 * Locking: call functions take tty_ldisc_lock
272 */
273
274struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
275{
Linus Torvalds65b77042009-08-03 11:11:19 -0700276 struct tty_ldisc *ld;
277
Alan Cox01e1abb2008-07-22 11:16:55 +0100278 /* wait_event is a macro */
Linus Torvalds65b77042009-08-03 11:11:19 -0700279 wait_event(tty_ldisc_wait, (ld = tty_ldisc_try(tty)) != NULL);
280 return ld;
Alan Cox01e1abb2008-07-22 11:16:55 +0100281}
Alan Cox01e1abb2008-07-22 11:16:55 +0100282EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
283
284/**
285 * tty_ldisc_ref - get the tty ldisc
286 * @tty: tty device
287 *
288 * Dereference the line discipline for the terminal and take a
289 * reference to it. If the line discipline is in flux then
290 * return NULL. Can be called from IRQ and timer functions.
291 *
292 * Locking: called functions take tty_ldisc_lock
293 */
294
295struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
296{
Linus Torvalds65b77042009-08-03 11:11:19 -0700297 return tty_ldisc_try(tty);
Alan Cox01e1abb2008-07-22 11:16:55 +0100298}
Alan Cox01e1abb2008-07-22 11:16:55 +0100299EXPORT_SYMBOL_GPL(tty_ldisc_ref);
300
301/**
302 * tty_ldisc_deref - free a tty ldisc reference
303 * @ld: reference to free up
304 *
305 * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May
306 * be called in IRQ context.
307 *
308 * Locking: takes tty_ldisc_lock
309 */
310
311void tty_ldisc_deref(struct tty_ldisc *ld)
312{
Peter Hurleyebc9bae2013-03-11 16:44:40 -0400313 unsigned long flags;
314
315 if (WARN_ON_ONCE(!ld))
316 return;
317
318 raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
319 /*
320 * WARNs if one-too-many reader references were released
321 * - the last reference must be released with tty_ldisc_put
322 */
323 WARN_ON(atomic_dec_and_test(&ld->users));
324 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
325
326 if (waitqueue_active(&ld->wq_idle))
327 wake_up(&ld->wq_idle);
Alan Cox01e1abb2008-07-22 11:16:55 +0100328}
Alan Cox01e1abb2008-07-22 11:16:55 +0100329EXPORT_SYMBOL_GPL(tty_ldisc_deref);
330
Peter Hurleyebc9bae2013-03-11 16:44:40 -0400331/**
332 * tty_ldisc_put - release the ldisc
333 *
334 * Complement of tty_ldisc_get().
335 */
Linus Torvalds65b77042009-08-03 11:11:19 -0700336static inline void tty_ldisc_put(struct tty_ldisc *ld)
337{
Peter Hurleyebc9bae2013-03-11 16:44:40 -0400338 unsigned long flags;
339
340 if (WARN_ON_ONCE(!ld))
341 return;
342
343 raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
344
345 /* unreleased reader reference(s) will cause this WARN */
346 WARN_ON(!atomic_dec_and_test(&ld->users));
347
348 ld->ops->refcount--;
349 module_put(ld->ops->owner);
350 kfree(ld);
351 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
Linus Torvalds65b77042009-08-03 11:11:19 -0700352}
353
Alan Cox01e1abb2008-07-22 11:16:55 +0100354/**
355 * tty_ldisc_enable - allow ldisc use
356 * @tty: terminal to activate ldisc on
357 *
358 * Set the TTY_LDISC flag when the line discipline can be called
Alan Coxc9b39762009-01-02 13:44:56 +0000359 * again. Do necessary wakeups for existing sleepers. Clear the LDISC
360 * changing flag to indicate any ldisc change is now over.
Alan Cox01e1abb2008-07-22 11:16:55 +0100361 *
Alan Coxc9b39762009-01-02 13:44:56 +0000362 * Note: nobody should set the TTY_LDISC bit except via this function.
363 * Clearing directly is allowed.
Alan Cox01e1abb2008-07-22 11:16:55 +0100364 */
365
Peter Hurleyd9121562013-03-11 16:44:33 -0400366static void tty_ldisc_enable(struct tty_struct *tty)
Alan Cox01e1abb2008-07-22 11:16:55 +0100367{
Peter Hurley21622932013-03-11 16:44:21 -0400368 clear_bit(TTY_LDISC_HALTED, &tty->flags);
Alan Cox01e1abb2008-07-22 11:16:55 +0100369 set_bit(TTY_LDISC, &tty->flags);
Alan Coxc9b39762009-01-02 13:44:56 +0000370 clear_bit(TTY_LDISC_CHANGING, &tty->flags);
Alan Cox01e1abb2008-07-22 11:16:55 +0100371 wake_up(&tty_ldisc_wait);
372}
373
374/**
Alan Coxf2c4c652009-06-11 12:50:58 +0100375 * tty_ldisc_flush - flush line discipline queue
376 * @tty: tty
377 *
378 * Flush the line discipline queue (if any) for this tty. If there
379 * is no line discipline active this is a no-op.
380 */
381
382void tty_ldisc_flush(struct tty_struct *tty)
383{
384 struct tty_ldisc *ld = tty_ldisc_ref(tty);
385 if (ld) {
386 if (ld->ops->flush_buffer)
387 ld->ops->flush_buffer(tty);
388 tty_ldisc_deref(ld);
389 }
390 tty_buffer_flush(tty);
391}
Alan Coxf2c4c652009-06-11 12:50:58 +0100392EXPORT_SYMBOL_GPL(tty_ldisc_flush);
393
394/**
Alan Cox01e1abb2008-07-22 11:16:55 +0100395 * tty_set_termios_ldisc - set ldisc field
396 * @tty: tty structure
397 * @num: line discipline number
398 *
399 * This is probably overkill for real world processors but
400 * they are not on hot paths so a little discipline won't do
401 * any harm.
402 *
403 * Locking: takes termios_mutex
404 */
405
406static void tty_set_termios_ldisc(struct tty_struct *tty, int num)
407{
408 mutex_lock(&tty->termios_mutex);
Alan Coxadc8d742012-07-14 15:31:47 +0100409 tty->termios.c_line = num;
Alan Cox01e1abb2008-07-22 11:16:55 +0100410 mutex_unlock(&tty->termios_mutex);
411}
412
Alan Coxc65c9bc2009-06-11 12:50:12 +0100413/**
414 * tty_ldisc_open - open a line discipline
415 * @tty: tty we are opening the ldisc on
416 * @ld: discipline to open
417 *
418 * A helper opening method. Also a convenient debugging and check
419 * point.
Arnd Bergmannec79d602010-06-01 22:53:01 +0200420 *
421 * Locking: always called with BTM already held.
Alan Coxc65c9bc2009-06-11 12:50:12 +0100422 */
423
424static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld)
425{
426 WARN_ON(test_and_set_bit(TTY_LDISC_OPEN, &tty->flags));
Alan Coxf18f9492009-11-30 13:18:35 +0000427 if (ld->ops->open) {
428 int ret;
Arnd Bergmannec79d602010-06-01 22:53:01 +0200429 /* BTM here locks versus a hangup event */
Alan Coxf18f9492009-11-30 13:18:35 +0000430 ret = ld->ops->open(tty);
Jiri Slaby7f90cfc2010-11-25 00:27:54 +0100431 if (ret)
432 clear_bit(TTY_LDISC_OPEN, &tty->flags);
Alan Coxf18f9492009-11-30 13:18:35 +0000433 return ret;
434 }
Alan Coxc65c9bc2009-06-11 12:50:12 +0100435 return 0;
436}
437
438/**
439 * tty_ldisc_close - close a line discipline
440 * @tty: tty we are opening the ldisc on
441 * @ld: discipline to close
442 *
443 * A helper close method. Also a convenient debugging and check
444 * point.
445 */
446
447static void tty_ldisc_close(struct tty_struct *tty, struct tty_ldisc *ld)
448{
449 WARN_ON(!test_bit(TTY_LDISC_OPEN, &tty->flags));
450 clear_bit(TTY_LDISC_OPEN, &tty->flags);
451 if (ld->ops->close)
452 ld->ops->close(tty);
453}
Alan Cox01e1abb2008-07-22 11:16:55 +0100454
455/**
456 * tty_ldisc_restore - helper for tty ldisc change
457 * @tty: tty to recover
458 * @old: previous ldisc
459 *
460 * Restore the previous line discipline or N_TTY when a line discipline
461 * change fails due to an open error
462 */
463
464static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old)
465{
466 char buf[64];
Alan Coxc65c9bc2009-06-11 12:50:12 +0100467 struct tty_ldisc *new_ldisc;
468 int r;
Alan Cox01e1abb2008-07-22 11:16:55 +0100469
470 /* There is an outstanding reference here so this is safe */
Alan Coxc65c9bc2009-06-11 12:50:12 +0100471 old = tty_ldisc_get(old->ops->num);
472 WARN_ON(IS_ERR(old));
Peter Hurleyf48070452013-03-11 16:44:42 -0400473 tty->ldisc = old;
Alan Cox01e1abb2008-07-22 11:16:55 +0100474 tty_set_termios_ldisc(tty, old->ops->num);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100475 if (tty_ldisc_open(tty, old) < 0) {
476 tty_ldisc_put(old);
Alan Cox01e1abb2008-07-22 11:16:55 +0100477 /* This driver is always present */
Alan Cox852e99d2009-06-11 12:51:41 +0100478 new_ldisc = tty_ldisc_get(N_TTY);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100479 if (IS_ERR(new_ldisc))
Alan Cox01e1abb2008-07-22 11:16:55 +0100480 panic("n_tty: get");
Peter Hurleyf48070452013-03-11 16:44:42 -0400481 tty->ldisc = new_ldisc;
Alan Cox01e1abb2008-07-22 11:16:55 +0100482 tty_set_termios_ldisc(tty, N_TTY);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100483 r = tty_ldisc_open(tty, new_ldisc);
484 if (r < 0)
485 panic("Couldn't open N_TTY ldisc for "
486 "%s --- error %d.",
487 tty_name(tty, buf), r);
Alan Cox01e1abb2008-07-22 11:16:55 +0100488 }
489}
490
491/**
Jiri Slaby100eeae2010-10-31 23:17:51 +0100492 * tty_ldisc_wait_idle - wait for the ldisc to become idle
493 * @tty: tty to wait for
Jiri Slabydf92d052011-11-16 16:27:07 +0100494 * @timeout: for how long to wait at most
Jiri Slaby100eeae2010-10-31 23:17:51 +0100495 *
496 * Wait for the line discipline to become idle. The discipline must
497 * have been halted for this to guarantee it remains idle.
498 */
Jiri Slabydf92d052011-11-16 16:27:07 +0100499static int tty_ldisc_wait_idle(struct tty_struct *tty, long timeout)
Jiri Slaby100eeae2010-10-31 23:17:51 +0100500{
Jiri Slabydf92d052011-11-16 16:27:07 +0100501 long ret;
Ivo Sieben1541f842012-05-03 14:37:43 +0200502 ret = wait_event_timeout(tty->ldisc->wq_idle,
Jiri Slabydf92d052011-11-16 16:27:07 +0100503 atomic_read(&tty->ldisc->users) == 1, timeout);
Jiri Slaby100eeae2010-10-31 23:17:51 +0100504 return ret > 0 ? 0 : -EBUSY;
505}
506
507/**
Peter Hurley11cf48e2013-03-11 16:44:27 -0400508 * tty_ldisc_halt - shut down the line discipline
509 * @tty: tty device
Peter Hurleyf4cf7a32013-03-11 16:44:29 -0400510 * @o_tty: paired pty device (can be NULL)
Peter Hurleycf528472013-03-11 16:44:28 -0400511 * @timeout: # of jiffies to wait for ldisc refs to be released
Peter Hurley11cf48e2013-03-11 16:44:27 -0400512 *
Peter Hurleyf4cf7a32013-03-11 16:44:29 -0400513 * Shut down the line discipline and work queue for this tty device and
514 * its paired pty (if exists). Clearing the TTY_LDISC flag ensures
Peter Hurley4f98d462013-03-11 16:44:34 -0400515 * no further references can be obtained, while waiting for existing
516 * references to be released ensures no more data is fed to the ldisc.
Peter Hurleycf528472013-03-11 16:44:28 -0400517 *
Peter Hurley11cf48e2013-03-11 16:44:27 -0400518 * You need to do a 'flush_scheduled_work()' (outside the ldisc_mutex)
519 * in order to make sure any currently executing ldisc work is also
520 * flushed.
521 */
522
Peter Hurleyf4cf7a32013-03-11 16:44:29 -0400523static int tty_ldisc_halt(struct tty_struct *tty, struct tty_struct *o_tty,
Peter Hurley4f98d462013-03-11 16:44:34 -0400524 long timeout)
Peter Hurley11cf48e2013-03-11 16:44:27 -0400525{
Peter Hurley4f98d462013-03-11 16:44:34 -0400526 int retval;
Peter Hurleycf528472013-03-11 16:44:28 -0400527
Peter Hurley11cf48e2013-03-11 16:44:27 -0400528 clear_bit(TTY_LDISC, &tty->flags);
Peter Hurleyf4cf7a32013-03-11 16:44:29 -0400529 if (o_tty)
530 clear_bit(TTY_LDISC, &o_tty->flags);
531
Peter Hurleycf528472013-03-11 16:44:28 -0400532 retval = tty_ldisc_wait_idle(tty, timeout);
Peter Hurleyf4cf7a32013-03-11 16:44:29 -0400533 if (!retval && o_tty)
534 retval = tty_ldisc_wait_idle(o_tty, timeout);
Peter Hurleycf528472013-03-11 16:44:28 -0400535 if (retval)
536 return retval;
537
Peter Hurley11cf48e2013-03-11 16:44:27 -0400538 set_bit(TTY_LDISC_HALTED, &tty->flags);
Peter Hurley4f98d462013-03-11 16:44:34 -0400539 if (o_tty)
Peter Hurleyf4cf7a32013-03-11 16:44:29 -0400540 set_bit(TTY_LDISC_HALTED, &o_tty->flags);
Peter Hurleyf4cf7a32013-03-11 16:44:29 -0400541
Peter Hurleycf528472013-03-11 16:44:28 -0400542 return 0;
Peter Hurley11cf48e2013-03-11 16:44:27 -0400543}
544
545/**
Peter Hurley76bc35e2013-03-11 16:44:26 -0400546 * tty_ldisc_hangup_halt - halt the line discipline for hangup
547 * @tty: tty being hung up
Peter Hurley168942c2013-03-11 16:44:24 -0400548 *
Peter Hurley76bc35e2013-03-11 16:44:26 -0400549 * Shut down the line discipline and work queue for the tty device
550 * being hungup. Clear the TTY_LDISC flag to ensure no further
Peter Hurley4f98d462013-03-11 16:44:34 -0400551 * references can be obtained and wait for remaining references to be
552 * released to ensure no more data is fed to this ldisc.
Peter Hurley168942c2013-03-11 16:44:24 -0400553 * Caller must hold legacy and ->ldisc_mutex.
Peter Hurley2276ad92013-03-11 16:44:25 -0400554 *
555 * NB: tty_set_ldisc() is prevented from changing the ldisc concurrently
556 * with this function by checking the TTY_HUPPING flag.
Peter Hurley168942c2013-03-11 16:44:24 -0400557 */
Peter Hurley76bc35e2013-03-11 16:44:26 -0400558static bool tty_ldisc_hangup_halt(struct tty_struct *tty)
Peter Hurley168942c2013-03-11 16:44:24 -0400559{
Peter Hurley2276ad92013-03-11 16:44:25 -0400560 char cur_n[TASK_COMM_LEN], tty_n[64];
561 long timeout = 3 * HZ;
Peter Hurley168942c2013-03-11 16:44:24 -0400562
Peter Hurley76bc35e2013-03-11 16:44:26 -0400563 clear_bit(TTY_LDISC, &tty->flags);
564
Peter Hurley2276ad92013-03-11 16:44:25 -0400565 if (tty->ldisc) { /* Not yet closed */
566 tty_unlock(tty);
567
568 while (tty_ldisc_wait_idle(tty, timeout) == -EBUSY) {
569 timeout = MAX_SCHEDULE_TIMEOUT;
570 printk_ratelimited(KERN_WARNING
571 "%s: waiting (%s) for %s took too long, but we keep waiting...\n",
572 __func__, get_task_comm(cur_n, current),
573 tty_name(tty, tty_n));
Peter Hurley168942c2013-03-11 16:44:24 -0400574 }
Peter Hurley76bc35e2013-03-11 16:44:26 -0400575
Peter Hurley76bc35e2013-03-11 16:44:26 -0400576 set_bit(TTY_LDISC_HALTED, &tty->flags);
577
Peter Hurley2276ad92013-03-11 16:44:25 -0400578 /* must reacquire both locks and preserve lock order */
579 mutex_unlock(&tty->ldisc_mutex);
580 tty_lock(tty);
581 mutex_lock(&tty->ldisc_mutex);
Peter Hurley168942c2013-03-11 16:44:24 -0400582 }
583 return !!tty->ldisc;
584}
585
586/**
Alan Cox01e1abb2008-07-22 11:16:55 +0100587 * tty_set_ldisc - set line discipline
588 * @tty: the terminal to set
589 * @ldisc: the line discipline
590 *
591 * Set the discipline of a tty line. Must be called from a process
Alan Coxc65c9bc2009-06-11 12:50:12 +0100592 * context. The ldisc change logic has to protect itself against any
593 * overlapping ldisc change (including on the other end of pty pairs),
594 * the close of one side of a tty/pty pair, and eventually hangup.
Alan Cox01e1abb2008-07-22 11:16:55 +0100595 *
Alan Coxc65c9bc2009-06-11 12:50:12 +0100596 * Locking: takes tty_ldisc_lock, termios_mutex
Alan Cox01e1abb2008-07-22 11:16:55 +0100597 */
598
599int tty_set_ldisc(struct tty_struct *tty, int ldisc)
600{
601 int retval;
Alan Coxc65c9bc2009-06-11 12:50:12 +0100602 struct tty_ldisc *o_ldisc, *new_ldisc;
Alan Cox01e1abb2008-07-22 11:16:55 +0100603 struct tty_struct *o_tty;
604
Alan Coxc65c9bc2009-06-11 12:50:12 +0100605 new_ldisc = tty_ldisc_get(ldisc);
606 if (IS_ERR(new_ldisc))
607 return PTR_ERR(new_ldisc);
608
Alan Cox89c8d912012-08-08 16:30:13 +0100609 tty_lock(tty);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100610 /*
611 * We need to look at the tty locking here for pty/tty pairs
612 * when both sides try to change in parallel.
613 */
614
615 o_tty = tty->link; /* o_tty is the pty side or NULL */
616
617
618 /*
619 * Check the no-op case
620 */
621
622 if (tty->ldisc->ops->num == ldisc) {
Alan Cox89c8d912012-08-08 16:30:13 +0100623 tty_unlock(tty);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100624 tty_ldisc_put(new_ldisc);
625 return 0;
626 }
Alan Cox01e1abb2008-07-22 11:16:55 +0100627
Alan Cox89c8d912012-08-08 16:30:13 +0100628 tty_unlock(tty);
Alan Cox01e1abb2008-07-22 11:16:55 +0100629 /*
630 * Problem: What do we do if this blocks ?
Alan Coxc65c9bc2009-06-11 12:50:12 +0100631 * We could deadlock here
Alan Cox01e1abb2008-07-22 11:16:55 +0100632 */
633
634 tty_wait_until_sent(tty, 0);
635
Alan Cox89c8d912012-08-08 16:30:13 +0100636 tty_lock(tty);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100637 mutex_lock(&tty->ldisc_mutex);
Alan Cox01e1abb2008-07-22 11:16:55 +0100638
639 /*
Alan Coxc65c9bc2009-06-11 12:50:12 +0100640 * We could be midstream of another ldisc change which has
641 * dropped the lock during processing. If so we need to wait.
642 */
643
644 while (test_bit(TTY_LDISC_CHANGING, &tty->flags)) {
645 mutex_unlock(&tty->ldisc_mutex);
Alan Cox89c8d912012-08-08 16:30:13 +0100646 tty_unlock(tty);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100647 wait_event(tty_ldisc_wait,
648 test_bit(TTY_LDISC_CHANGING, &tty->flags) == 0);
Alan Cox89c8d912012-08-08 16:30:13 +0100649 tty_lock(tty);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100650 mutex_lock(&tty->ldisc_mutex);
651 }
Alan Coxeeb89d92009-11-30 13:18:29 +0000652
Alan Coxc65c9bc2009-06-11 12:50:12 +0100653 set_bit(TTY_LDISC_CHANGING, &tty->flags);
Alan Cox852e99d2009-06-11 12:51:41 +0100654
Alan Coxc65c9bc2009-06-11 12:50:12 +0100655 /*
Alan Cox01e1abb2008-07-22 11:16:55 +0100656 * No more input please, we are switching. The new ldisc
657 * will update this value in the ldisc open function
658 */
659
660 tty->receive_room = 0;
661
662 o_ldisc = tty->ldisc;
Alan Coxeeb89d92009-11-30 13:18:29 +0000663
Alan Cox89c8d912012-08-08 16:30:13 +0100664 tty_unlock(tty);
Alan Cox01e1abb2008-07-22 11:16:55 +0100665 /*
666 * Make sure we don't change while someone holds a
667 * reference to the line discipline. The TTY_LDISC bit
668 * prevents anyone taking a reference once it is clear.
669 * We need the lock to avoid racing reference takers.
Alan Coxc9b39762009-01-02 13:44:56 +0000670 *
671 * We must clear the TTY_LDISC bit here to avoid a livelock
672 * with a userspace app continually trying to use the tty in
673 * parallel to the change and re-referencing the tty.
Alan Cox01e1abb2008-07-22 11:16:55 +0100674 */
675
Peter Hurley4f98d462013-03-11 16:44:34 -0400676 retval = tty_ldisc_halt(tty, o_tty, 5 * HZ);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100677
Alan Cox01e1abb2008-07-22 11:16:55 +0100678 /*
Peter Hurleya2965b72013-03-11 16:44:35 -0400679 * Wait for hangup to complete, if pending.
Alan Coxc65c9bc2009-06-11 12:50:12 +0100680 * We must drop the mutex here in case a hangup is also in process.
Alan Cox01e1abb2008-07-22 11:16:55 +0100681 */
682
Alan Coxc65c9bc2009-06-11 12:50:12 +0100683 mutex_unlock(&tty->ldisc_mutex);
684
Peter Hurleya2965b72013-03-11 16:44:35 -0400685 flush_work(&tty->hangup_work);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100686
Alan Cox89c8d912012-08-08 16:30:13 +0100687 tty_lock(tty);
Arnd Bergmann60af22d2010-06-01 22:53:06 +0200688 mutex_lock(&tty->ldisc_mutex);
Jiri Slaby100eeae2010-10-31 23:17:51 +0100689
690 /* handle wait idle failure locked */
691 if (retval) {
692 tty_ldisc_put(new_ldisc);
693 goto enable;
694 }
695
Shachar Shemesh40c9f612012-07-10 07:54:13 +0300696 if (test_bit(TTY_HUPPING, &tty->flags)) {
Alan Coxc65c9bc2009-06-11 12:50:12 +0100697 /* We were raced by the hangup method. It will have stomped
698 the ldisc data and closed the ldisc down */
699 clear_bit(TTY_LDISC_CHANGING, &tty->flags);
700 mutex_unlock(&tty->ldisc_mutex);
701 tty_ldisc_put(new_ldisc);
Alan Cox89c8d912012-08-08 16:30:13 +0100702 tty_unlock(tty);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100703 return -EIO;
704 }
705
Alan Cox01e1abb2008-07-22 11:16:55 +0100706 /* Shutdown the current discipline. */
Alan Coxc65c9bc2009-06-11 12:50:12 +0100707 tty_ldisc_close(tty, o_ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100708
709 /* Now set up the new line discipline. */
Peter Hurleyf48070452013-03-11 16:44:42 -0400710 tty->ldisc = new_ldisc;
Alan Cox01e1abb2008-07-22 11:16:55 +0100711 tty_set_termios_ldisc(tty, ldisc);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100712
713 retval = tty_ldisc_open(tty, new_ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100714 if (retval < 0) {
Alan Coxc65c9bc2009-06-11 12:50:12 +0100715 /* Back to the old one or N_TTY if we can't */
716 tty_ldisc_put(new_ldisc);
717 tty_ldisc_restore(tty, o_ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100718 }
Alan Coxc65c9bc2009-06-11 12:50:12 +0100719
Alan Cox01e1abb2008-07-22 11:16:55 +0100720 /* At this point we hold a reference to the new ldisc and a
721 a reference to the old ldisc. If we ended up flipping back
722 to the existing ldisc we have two references to it */
723
Alan Coxc65c9bc2009-06-11 12:50:12 +0100724 if (tty->ldisc->ops->num != o_ldisc->ops->num && tty->ops->set_ldisc)
Alan Cox01e1abb2008-07-22 11:16:55 +0100725 tty->ops->set_ldisc(tty);
726
Alan Coxc65c9bc2009-06-11 12:50:12 +0100727 tty_ldisc_put(o_ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100728
Jiri Slaby100eeae2010-10-31 23:17:51 +0100729enable:
Alan Cox01e1abb2008-07-22 11:16:55 +0100730 /*
Alan Coxc65c9bc2009-06-11 12:50:12 +0100731 * Allow ldisc referencing to occur again
Alan Cox01e1abb2008-07-22 11:16:55 +0100732 */
733
734 tty_ldisc_enable(tty);
735 if (o_tty)
736 tty_ldisc_enable(o_tty);
737
Alan Coxc65c9bc2009-06-11 12:50:12 +0100738 /* Restart the work queue in case no characters kick it off. Safe if
Alan Cox01e1abb2008-07-22 11:16:55 +0100739 already running */
Peter Hurley4f98d462013-03-11 16:44:34 -0400740 schedule_work(&tty->port->buf.work);
741 if (o_tty)
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200742 schedule_work(&o_tty->port->buf.work);
Peter Hurley4f98d462013-03-11 16:44:34 -0400743
Alan Coxc65c9bc2009-06-11 12:50:12 +0100744 mutex_unlock(&tty->ldisc_mutex);
Alan Cox89c8d912012-08-08 16:30:13 +0100745 tty_unlock(tty);
Alan Cox01e1abb2008-07-22 11:16:55 +0100746 return retval;
747}
748
Alan Coxc65c9bc2009-06-11 12:50:12 +0100749/**
750 * tty_reset_termios - reset terminal state
751 * @tty: tty to reset
752 *
753 * Restore a terminal to the driver default state.
754 */
755
756static void tty_reset_termios(struct tty_struct *tty)
757{
758 mutex_lock(&tty->termios_mutex);
Alan Coxadc8d742012-07-14 15:31:47 +0100759 tty->termios = tty->driver->init_termios;
760 tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios);
761 tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100762 mutex_unlock(&tty->termios_mutex);
763}
764
765
766/**
767 * tty_ldisc_reinit - reinitialise the tty ldisc
768 * @tty: tty to reinit
Alan Cox638b9642010-02-08 10:09:26 +0000769 * @ldisc: line discipline to reinitialize
Alan Coxc65c9bc2009-06-11 12:50:12 +0100770 *
Alan Cox638b9642010-02-08 10:09:26 +0000771 * Switch the tty to a line discipline and leave the ldisc
772 * state closed
Alan Coxc65c9bc2009-06-11 12:50:12 +0100773 */
774
Philippe Rétornaz1c95ba12010-10-27 17:13:21 +0200775static int tty_ldisc_reinit(struct tty_struct *tty, int ldisc)
Alan Coxc65c9bc2009-06-11 12:50:12 +0100776{
Philippe Rétornaz1c95ba12010-10-27 17:13:21 +0200777 struct tty_ldisc *ld = tty_ldisc_get(ldisc);
778
779 if (IS_ERR(ld))
780 return -1;
Alan Coxc65c9bc2009-06-11 12:50:12 +0100781
782 tty_ldisc_close(tty, tty->ldisc);
783 tty_ldisc_put(tty->ldisc);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100784 /*
785 * Switch the line discipline back
786 */
Peter Hurleyf48070452013-03-11 16:44:42 -0400787 tty->ldisc = ld;
Alan Cox638b9642010-02-08 10:09:26 +0000788 tty_set_termios_ldisc(tty, ldisc);
Philippe Rétornaz1c95ba12010-10-27 17:13:21 +0200789
790 return 0;
Alan Coxc65c9bc2009-06-11 12:50:12 +0100791}
792
793/**
794 * tty_ldisc_hangup - hangup ldisc reset
795 * @tty: tty being hung up
796 *
797 * Some tty devices reset their termios when they receive a hangup
798 * event. In that situation we must also switch back to N_TTY properly
799 * before we reset the termios data.
800 *
801 * Locking: We can take the ldisc mutex as the rest of the code is
802 * careful to allow for this.
803 *
804 * In the pty pair case this occurs in the close() path of the
805 * tty itself so we must be careful about locking rules.
806 */
807
808void tty_ldisc_hangup(struct tty_struct *tty)
809{
810 struct tty_ldisc *ld;
Alan Cox638b9642010-02-08 10:09:26 +0000811 int reset = tty->driver->flags & TTY_DRIVER_RESET_TERMIOS;
812 int err = 0;
Alan Coxc65c9bc2009-06-11 12:50:12 +0100813
Peter Hurleyfc575ee2013-03-11 16:44:38 -0400814 tty_ldisc_debug(tty, "closing ldisc: %p\n", tty->ldisc);
815
Alan Coxc65c9bc2009-06-11 12:50:12 +0100816 /*
817 * FIXME! What are the locking issues here? This may me overdoing
818 * things... This question is especially important now that we've
819 * removed the irqlock.
820 */
821 ld = tty_ldisc_ref(tty);
822 if (ld != NULL) {
823 /* We may have no line discipline at this point */
824 if (ld->ops->flush_buffer)
825 ld->ops->flush_buffer(tty);
826 tty_driver_flush_buffer(tty);
827 if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
828 ld->ops->write_wakeup)
829 ld->ops->write_wakeup(tty);
830 if (ld->ops->hangup)
831 ld->ops->hangup(tty);
832 tty_ldisc_deref(ld);
833 }
834 /*
835 * FIXME: Once we trust the LDISC code better we can wait here for
836 * ldisc completion and fix the driver call race
837 */
838 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
839 wake_up_interruptible_poll(&tty->read_wait, POLLIN);
840 /*
841 * Shutdown the current line discipline, and reset it to
Alan Cox638b9642010-02-08 10:09:26 +0000842 * N_TTY if need be.
843 *
844 * Avoid racing set_ldisc or tty_ldisc_release
Alan Coxc65c9bc2009-06-11 12:50:12 +0100845 */
Alan Cox638b9642010-02-08 10:09:26 +0000846 mutex_lock(&tty->ldisc_mutex);
Arnd Bergmann60af22d2010-06-01 22:53:06 +0200847
Peter Hurley76bc35e2013-03-11 16:44:26 -0400848 if (tty_ldisc_hangup_halt(tty)) {
Peter Hurleyc8785242013-03-11 16:44:36 -0400849
850 /* At this point we have a halted ldisc; we want to close it and
851 reopen a new ldisc. We could defer the reopen to the next
852 open but it means auditing a lot of other paths so this is
853 a FIXME */
Alan Cox638b9642010-02-08 10:09:26 +0000854 if (reset == 0) {
Philippe Rétornaz1c95ba12010-10-27 17:13:21 +0200855
Alan Coxadc8d742012-07-14 15:31:47 +0100856 if (!tty_ldisc_reinit(tty, tty->termios.c_line))
Philippe Rétornaz1c95ba12010-10-27 17:13:21 +0200857 err = tty_ldisc_open(tty, tty->ldisc);
858 else
859 err = 1;
Alan Coxc8d50042009-07-16 16:05:08 +0100860 }
Alan Cox638b9642010-02-08 10:09:26 +0000861 /* If the re-open fails or we reset then go to N_TTY. The
862 N_TTY open cannot fail */
863 if (reset || err) {
Philippe Rétornaz1c95ba12010-10-27 17:13:21 +0200864 BUG_ON(tty_ldisc_reinit(tty, N_TTY));
Alan Cox638b9642010-02-08 10:09:26 +0000865 WARN_ON(tty_ldisc_open(tty, tty->ldisc));
866 }
867 tty_ldisc_enable(tty);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100868 }
Alan Cox638b9642010-02-08 10:09:26 +0000869 mutex_unlock(&tty->ldisc_mutex);
870 if (reset)
871 tty_reset_termios(tty);
Peter Hurleyfc575ee2013-03-11 16:44:38 -0400872
873 tty_ldisc_debug(tty, "re-opened ldisc: %p\n", tty->ldisc);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100874}
Alan Cox01e1abb2008-07-22 11:16:55 +0100875
876/**
877 * tty_ldisc_setup - open line discipline
878 * @tty: tty being shut down
879 * @o_tty: pair tty for pty/tty pairs
880 *
881 * Called during the initial open of a tty/pty pair in order to set up the
Alan Coxc65c9bc2009-06-11 12:50:12 +0100882 * line disciplines and bind them to the tty. This has no locking issues
883 * as the device isn't yet active.
Alan Cox01e1abb2008-07-22 11:16:55 +0100884 */
885
886int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty)
887{
Alan Coxc65c9bc2009-06-11 12:50:12 +0100888 struct tty_ldisc *ld = tty->ldisc;
Alan Cox01e1abb2008-07-22 11:16:55 +0100889 int retval;
890
Alan Coxc65c9bc2009-06-11 12:50:12 +0100891 retval = tty_ldisc_open(tty, ld);
892 if (retval)
893 return retval;
894
895 if (o_tty) {
896 retval = tty_ldisc_open(o_tty, o_tty->ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100897 if (retval) {
Alan Coxc65c9bc2009-06-11 12:50:12 +0100898 tty_ldisc_close(tty, ld);
Alan Cox01e1abb2008-07-22 11:16:55 +0100899 return retval;
900 }
901 tty_ldisc_enable(o_tty);
902 }
903 tty_ldisc_enable(tty);
904 return 0;
905}
Alan Cox89c8d912012-08-08 16:30:13 +0100906
907static void tty_ldisc_kill(struct tty_struct *tty)
908{
909 mutex_lock(&tty->ldisc_mutex);
910 /*
911 * Now kill off the ldisc
912 */
913 tty_ldisc_close(tty, tty->ldisc);
914 tty_ldisc_put(tty->ldisc);
915 /* Force an oops if we mess this up */
916 tty->ldisc = NULL;
917
918 /* Ensure the next open requests the N_TTY ldisc */
919 tty_set_termios_ldisc(tty, N_TTY);
920 mutex_unlock(&tty->ldisc_mutex);
921}
922
Alan Cox01e1abb2008-07-22 11:16:55 +0100923/**
924 * tty_ldisc_release - release line discipline
925 * @tty: tty being shut down
926 * @o_tty: pair tty for pty/tty pairs
927 *
Alan Cox852e99d2009-06-11 12:51:41 +0100928 * Called during the final close of a tty/pty pair in order to shut down
929 * the line discpline layer. On exit the ldisc assigned is N_TTY and the
Alan Coxc65c9bc2009-06-11 12:50:12 +0100930 * ldisc has not been opened.
Alan Cox01e1abb2008-07-22 11:16:55 +0100931 */
932
933void tty_ldisc_release(struct tty_struct *tty, struct tty_struct *o_tty)
934{
Alan Cox01e1abb2008-07-22 11:16:55 +0100935 /*
Peter Hurleya2965b72013-03-11 16:44:35 -0400936 * Shutdown this line discipline. As this is the final close,
937 * it does not race with the set_ldisc code path.
Alan Cox01e1abb2008-07-22 11:16:55 +0100938 */
Alan Cox01e1abb2008-07-22 11:16:55 +0100939
Peter Hurleyfc575ee2013-03-11 16:44:38 -0400940 tty_ldisc_debug(tty, "closing ldisc: %p\n", tty->ldisc);
941
Peter Hurley4f98d462013-03-11 16:44:34 -0400942 tty_ldisc_halt(tty, o_tty, MAX_SCHEDULE_TIMEOUT);
Sebastian Andrzej Siewior852e4a82012-12-25 23:02:48 +0100943
944 tty_lock_pair(tty, o_tty);
Alan Cox6d31a882012-07-14 15:31:27 +0100945 /* This will need doing differently if we need to lock */
Alan Cox89c8d912012-08-08 16:30:13 +0100946 tty_ldisc_kill(tty);
Alan Cox89c8d912012-08-08 16:30:13 +0100947 if (o_tty)
948 tty_ldisc_kill(o_tty);
949
950 tty_unlock_pair(tty, o_tty);
Alan Coxaef29bc2009-06-29 15:21:47 +0100951 /* And the memory resources remaining (buffers, termios) will be
952 disposed of when the kref hits zero */
Peter Hurleyfc575ee2013-03-11 16:44:38 -0400953
954 tty_ldisc_debug(tty, "ldisc closed\n");
Alan Cox01e1abb2008-07-22 11:16:55 +0100955}
956
957/**
958 * tty_ldisc_init - ldisc setup for new tty
959 * @tty: tty being allocated
960 *
961 * Set up the line discipline objects for a newly allocated tty. Note that
962 * the tty structure is not completely set up when this call is made.
963 */
964
965void tty_ldisc_init(struct tty_struct *tty)
966{
Alan Coxc65c9bc2009-06-11 12:50:12 +0100967 struct tty_ldisc *ld = tty_ldisc_get(N_TTY);
968 if (IS_ERR(ld))
Alan Cox01e1abb2008-07-22 11:16:55 +0100969 panic("n_tty: init_tty");
Peter Hurleyf48070452013-03-11 16:44:42 -0400970 tty->ldisc = ld;
Alan Cox01e1abb2008-07-22 11:16:55 +0100971}
972
Jiri Slaby67166712011-03-23 10:48:35 +0100973/**
974 * tty_ldisc_init - ldisc cleanup for new tty
975 * @tty: tty that was allocated recently
976 *
977 * The tty structure must not becompletely set up (tty_ldisc_setup) when
978 * this call is made.
979 */
980void tty_ldisc_deinit(struct tty_struct *tty)
981{
Peter Hurleyebc9bae2013-03-11 16:44:40 -0400982 tty_ldisc_put(tty->ldisc);
Peter Hurleyf48070452013-03-11 16:44:42 -0400983 tty->ldisc = NULL;
Jiri Slaby67166712011-03-23 10:48:35 +0100984}
985
Alan Cox01e1abb2008-07-22 11:16:55 +0100986void tty_ldisc_begin(void)
987{
988 /* Setup the default TTY line discipline. */
989 (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
990}