blob: c5b848a78e49fe71970573096ba03523207784ba [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
23/*
24 * This guards the refcounted line discipline lists. The lock
25 * must be taken with irqs off because there are hangup path
26 * callers who will do ldisc lookups and cannot sleep.
27 */
28
Ivo Siebenc9739942012-10-17 14:03:14 +020029static DEFINE_RAW_SPINLOCK(tty_ldisc_lock);
Alan Cox01e1abb2008-07-22 11:16:55 +010030static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_wait);
31/* Line disc dispatch table */
32static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS];
33
Linus Torvalds65b77042009-08-03 11:11:19 -070034static inline struct tty_ldisc *get_ldisc(struct tty_ldisc *ld)
35{
36 if (ld)
37 atomic_inc(&ld->users);
38 return ld;
39}
40
Linus Torvaldscbe93522009-08-03 14:54:56 -070041static void put_ldisc(struct tty_ldisc *ld)
Linus Torvalds65b77042009-08-03 11:11:19 -070042{
Linus Torvaldscbe93522009-08-03 14:54:56 -070043 unsigned long flags;
44
Linus Torvalds65b77042009-08-03 11:11:19 -070045 if (WARN_ON_ONCE(!ld))
46 return;
47
48 /*
49 * If this is the last user, free the ldisc, and
50 * release the ldisc ops.
Linus Torvaldscbe93522009-08-03 14:54:56 -070051 *
Ivo Siebenc9739942012-10-17 14:03:14 +020052 * We really want an "atomic_dec_and_raw_lock_irqsave()",
Linus Torvaldscbe93522009-08-03 14:54:56 -070053 * but we don't have it, so this does it by hand.
Linus Torvalds65b77042009-08-03 11:11:19 -070054 */
Ivo Siebenc9739942012-10-17 14:03:14 +020055 raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
56 if (atomic_dec_and_test(&ld->users)) {
Linus Torvalds65b77042009-08-03 11:11:19 -070057 struct tty_ldisc_ops *ldo = ld->ops;
58
Linus Torvalds65b77042009-08-03 11:11:19 -070059 ldo->refcount--;
60 module_put(ldo->owner);
Ivo Siebenc9739942012-10-17 14:03:14 +020061 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
Linus Torvaldscbe93522009-08-03 14:54:56 -070062
63 kfree(ld);
64 return;
Linus Torvalds65b77042009-08-03 11:11:19 -070065 }
Ivo Siebenc9739942012-10-17 14:03:14 +020066 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
Ivo Siebenbd5d7ce2012-12-18 15:48:50 +010067
68 if (waitqueue_active(&ld->wq_idle))
69 wake_up(&ld->wq_idle);
Linus Torvalds65b77042009-08-03 11:11:19 -070070}
71
Alan Cox01e1abb2008-07-22 11:16:55 +010072/**
73 * tty_register_ldisc - install a line discipline
74 * @disc: ldisc number
75 * @new_ldisc: pointer to the ldisc object
76 *
77 * Installs a new line discipline into the kernel. The discipline
78 * is set up as unreferenced and then made available to the kernel
79 * from this point onwards.
80 *
81 * Locking:
82 * takes tty_ldisc_lock to guard against ldisc races
83 */
84
85int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc)
86{
87 unsigned long flags;
88 int ret = 0;
89
90 if (disc < N_TTY || disc >= NR_LDISCS)
91 return -EINVAL;
92
Ivo Siebenc9739942012-10-17 14:03:14 +020093 raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
Alan Cox01e1abb2008-07-22 11:16:55 +010094 tty_ldiscs[disc] = new_ldisc;
95 new_ldisc->num = disc;
96 new_ldisc->refcount = 0;
Ivo Siebenc9739942012-10-17 14:03:14 +020097 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
Alan Cox01e1abb2008-07-22 11:16:55 +010098
99 return ret;
100}
101EXPORT_SYMBOL(tty_register_ldisc);
102
103/**
104 * tty_unregister_ldisc - unload a line discipline
105 * @disc: ldisc number
106 * @new_ldisc: pointer to the ldisc object
107 *
108 * Remove a line discipline from the kernel providing it is not
109 * currently in use.
110 *
111 * Locking:
112 * takes tty_ldisc_lock to guard against ldisc races
113 */
114
115int tty_unregister_ldisc(int disc)
116{
117 unsigned long flags;
118 int ret = 0;
119
120 if (disc < N_TTY || disc >= NR_LDISCS)
121 return -EINVAL;
122
Ivo Siebenc9739942012-10-17 14:03:14 +0200123 raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
Alan Cox01e1abb2008-07-22 11:16:55 +0100124 if (tty_ldiscs[disc]->refcount)
125 ret = -EBUSY;
126 else
127 tty_ldiscs[disc] = NULL;
Ivo Siebenc9739942012-10-17 14:03:14 +0200128 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
Alan Cox01e1abb2008-07-22 11:16:55 +0100129
130 return ret;
131}
132EXPORT_SYMBOL(tty_unregister_ldisc);
133
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700134static struct tty_ldisc_ops *get_ldops(int disc)
135{
136 unsigned long flags;
137 struct tty_ldisc_ops *ldops, *ret;
138
Ivo Siebenc9739942012-10-17 14:03:14 +0200139 raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700140 ret = ERR_PTR(-EINVAL);
141 ldops = tty_ldiscs[disc];
142 if (ldops) {
143 ret = ERR_PTR(-EAGAIN);
144 if (try_module_get(ldops->owner)) {
145 ldops->refcount++;
146 ret = ldops;
147 }
148 }
Ivo Siebenc9739942012-10-17 14:03:14 +0200149 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700150 return ret;
151}
152
153static void put_ldops(struct tty_ldisc_ops *ldops)
154{
155 unsigned long flags;
156
Ivo Siebenc9739942012-10-17 14:03:14 +0200157 raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700158 ldops->refcount--;
159 module_put(ldops->owner);
Ivo Siebenc9739942012-10-17 14:03:14 +0200160 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700161}
Alan Cox01e1abb2008-07-22 11:16:55 +0100162
163/**
Alan Cox01e1abb2008-07-22 11:16:55 +0100164 * tty_ldisc_get - take a reference to an ldisc
165 * @disc: ldisc number
Alan Cox01e1abb2008-07-22 11:16:55 +0100166 *
167 * Takes a reference to a line discipline. Deals with refcounts and
168 * module locking counts. Returns NULL if the discipline is not available.
169 * Returns a pointer to the discipline and bumps the ref count if it is
170 * available
171 *
172 * Locking:
173 * takes tty_ldisc_lock to guard against ldisc races
174 */
175
Alan Coxc65c9bc2009-06-11 12:50:12 +0100176static struct tty_ldisc *tty_ldisc_get(int disc)
Alan Cox01e1abb2008-07-22 11:16:55 +0100177{
Alan Coxc65c9bc2009-06-11 12:50:12 +0100178 struct tty_ldisc *ld;
Linus Torvalds182274f2009-08-03 16:01:28 -0700179 struct tty_ldisc_ops *ldops;
Alan Cox01e1abb2008-07-22 11:16:55 +0100180
181 if (disc < N_TTY || disc >= NR_LDISCS)
Alan Coxc65c9bc2009-06-11 12:50:12 +0100182 return ERR_PTR(-EINVAL);
Linus Torvalds182274f2009-08-03 16:01:28 -0700183
184 /*
185 * Get the ldisc ops - we may need to request them to be loaded
186 * dynamically and try again.
187 */
188 ldops = get_ldops(disc);
189 if (IS_ERR(ldops)) {
Alan Cox01e1abb2008-07-22 11:16:55 +0100190 request_module("tty-ldisc-%d", disc);
Linus Torvalds182274f2009-08-03 16:01:28 -0700191 ldops = get_ldops(disc);
192 if (IS_ERR(ldops))
193 return ERR_CAST(ldops);
Alan Cox01e1abb2008-07-22 11:16:55 +0100194 }
Linus Torvalds182274f2009-08-03 16:01:28 -0700195
196 ld = kmalloc(sizeof(struct tty_ldisc), GFP_KERNEL);
197 if (ld == NULL) {
198 put_ldops(ldops);
199 return ERR_PTR(-ENOMEM);
200 }
201
202 ld->ops = ldops;
203 atomic_set(&ld->users, 1);
Ivo Sieben1541f842012-05-03 14:37:43 +0200204 init_waitqueue_head(&ld->wq_idle);
205
Alan Coxc65c9bc2009-06-11 12:50:12 +0100206 return ld;
Alan Cox01e1abb2008-07-22 11:16:55 +0100207}
208
Alan Cox852e99d2009-06-11 12:51:41 +0100209static void *tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos)
Alan Cox01e1abb2008-07-22 11:16:55 +0100210{
211 return (*pos < NR_LDISCS) ? pos : NULL;
212}
213
Alan Cox852e99d2009-06-11 12:51:41 +0100214static void *tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos)
Alan Cox01e1abb2008-07-22 11:16:55 +0100215{
216 (*pos)++;
217 return (*pos < NR_LDISCS) ? pos : NULL;
218}
219
220static void tty_ldiscs_seq_stop(struct seq_file *m, void *v)
221{
222}
223
224static int tty_ldiscs_seq_show(struct seq_file *m, void *v)
225{
226 int i = *(loff_t *)v;
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700227 struct tty_ldisc_ops *ldops;
Alan Cox852e99d2009-06-11 12:51:41 +0100228
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700229 ldops = get_ldops(i);
230 if (IS_ERR(ldops))
Alan Cox01e1abb2008-07-22 11:16:55 +0100231 return 0;
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700232 seq_printf(m, "%-10s %2d\n", ldops->name ? ldops->name : "???", i);
233 put_ldops(ldops);
Alan Cox01e1abb2008-07-22 11:16:55 +0100234 return 0;
235}
236
237static const struct seq_operations tty_ldiscs_seq_ops = {
238 .start = tty_ldiscs_seq_start,
239 .next = tty_ldiscs_seq_next,
240 .stop = tty_ldiscs_seq_stop,
241 .show = tty_ldiscs_seq_show,
242};
243
244static int proc_tty_ldiscs_open(struct inode *inode, struct file *file)
245{
246 return seq_open(file, &tty_ldiscs_seq_ops);
247}
248
249const struct file_operations tty_ldiscs_proc_fops = {
250 .owner = THIS_MODULE,
251 .open = proc_tty_ldiscs_open,
252 .read = seq_read,
253 .llseek = seq_lseek,
254 .release = seq_release,
255};
256
257/**
258 * tty_ldisc_assign - set ldisc on a tty
259 * @tty: tty to assign
260 * @ld: line discipline
261 *
262 * Install an instance of a line discipline into a tty structure. The
Alan Cox8d2ead72009-06-16 17:00:26 +0100263 * ldisc must have a reference count above zero to ensure it remains.
Alan Cox01e1abb2008-07-22 11:16:55 +0100264 * The tty instance refcount starts at zero.
265 *
266 * Locking:
267 * Caller must hold references
268 */
269
270static void tty_ldisc_assign(struct tty_struct *tty, struct tty_ldisc *ld)
271{
Alan Coxc65c9bc2009-06-11 12:50:12 +0100272 tty->ldisc = ld;
Alan Cox01e1abb2008-07-22 11:16:55 +0100273}
274
275/**
276 * tty_ldisc_try - internal helper
277 * @tty: the tty
278 *
279 * Make a single attempt to grab and bump the refcount on
280 * the tty ldisc. Return 0 on failure or 1 on success. This is
281 * used to implement both the waiting and non waiting versions
282 * of tty_ldisc_ref
283 *
284 * Locking: takes tty_ldisc_lock
285 */
286
Linus Torvalds65b77042009-08-03 11:11:19 -0700287static struct tty_ldisc *tty_ldisc_try(struct tty_struct *tty)
Alan Cox01e1abb2008-07-22 11:16:55 +0100288{
289 unsigned long flags;
290 struct tty_ldisc *ld;
Alan Cox01e1abb2008-07-22 11:16:55 +0100291
Ivo Siebenc9739942012-10-17 14:03:14 +0200292 raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
Linus Torvalds65b77042009-08-03 11:11:19 -0700293 ld = NULL;
294 if (test_bit(TTY_LDISC, &tty->flags))
295 ld = get_ldisc(tty->ldisc);
Ivo Siebenc9739942012-10-17 14:03:14 +0200296 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
Linus Torvalds65b77042009-08-03 11:11:19 -0700297 return ld;
Alan Cox01e1abb2008-07-22 11:16:55 +0100298}
299
300/**
301 * tty_ldisc_ref_wait - wait for the tty ldisc
302 * @tty: tty device
303 *
304 * Dereference the line discipline for the terminal and take a
305 * reference to it. If the line discipline is in flux then
306 * wait patiently until it changes.
307 *
308 * Note: Must not be called from an IRQ/timer context. The caller
309 * must also be careful not to hold other locks that will deadlock
310 * against a discipline change, such as an existing ldisc reference
311 * (which we check for)
312 *
313 * Locking: call functions take tty_ldisc_lock
314 */
315
316struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
317{
Linus Torvalds65b77042009-08-03 11:11:19 -0700318 struct tty_ldisc *ld;
319
Alan Cox01e1abb2008-07-22 11:16:55 +0100320 /* wait_event is a macro */
Linus Torvalds65b77042009-08-03 11:11:19 -0700321 wait_event(tty_ldisc_wait, (ld = tty_ldisc_try(tty)) != NULL);
322 return ld;
Alan Cox01e1abb2008-07-22 11:16:55 +0100323}
Alan Cox01e1abb2008-07-22 11:16:55 +0100324EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
325
326/**
327 * tty_ldisc_ref - get the tty ldisc
328 * @tty: tty device
329 *
330 * Dereference the line discipline for the terminal and take a
331 * reference to it. If the line discipline is in flux then
332 * return NULL. Can be called from IRQ and timer functions.
333 *
334 * Locking: called functions take tty_ldisc_lock
335 */
336
337struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
338{
Linus Torvalds65b77042009-08-03 11:11:19 -0700339 return tty_ldisc_try(tty);
Alan Cox01e1abb2008-07-22 11:16:55 +0100340}
Alan Cox01e1abb2008-07-22 11:16:55 +0100341EXPORT_SYMBOL_GPL(tty_ldisc_ref);
342
343/**
344 * tty_ldisc_deref - free a tty ldisc reference
345 * @ld: reference to free up
346 *
347 * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May
348 * be called in IRQ context.
349 *
350 * Locking: takes tty_ldisc_lock
351 */
352
353void tty_ldisc_deref(struct tty_ldisc *ld)
354{
Linus Torvalds65b77042009-08-03 11:11:19 -0700355 put_ldisc(ld);
Alan Cox01e1abb2008-07-22 11:16:55 +0100356}
Alan Cox01e1abb2008-07-22 11:16:55 +0100357EXPORT_SYMBOL_GPL(tty_ldisc_deref);
358
Linus Torvalds65b77042009-08-03 11:11:19 -0700359static inline void tty_ldisc_put(struct tty_ldisc *ld)
360{
361 put_ldisc(ld);
362}
363
Alan Cox01e1abb2008-07-22 11:16:55 +0100364/**
365 * tty_ldisc_enable - allow ldisc use
366 * @tty: terminal to activate ldisc on
367 *
368 * Set the TTY_LDISC flag when the line discipline can be called
Alan Coxc9b39762009-01-02 13:44:56 +0000369 * again. Do necessary wakeups for existing sleepers. Clear the LDISC
370 * changing flag to indicate any ldisc change is now over.
Alan Cox01e1abb2008-07-22 11:16:55 +0100371 *
Alan Coxc9b39762009-01-02 13:44:56 +0000372 * Note: nobody should set the TTY_LDISC bit except via this function.
373 * Clearing directly is allowed.
Alan Cox01e1abb2008-07-22 11:16:55 +0100374 */
375
376void tty_ldisc_enable(struct tty_struct *tty)
377{
Peter Hurley21622932013-03-11 16:44:21 -0400378 clear_bit(TTY_LDISC_HALTED, &tty->flags);
Alan Cox01e1abb2008-07-22 11:16:55 +0100379 set_bit(TTY_LDISC, &tty->flags);
Alan Coxc9b39762009-01-02 13:44:56 +0000380 clear_bit(TTY_LDISC_CHANGING, &tty->flags);
Alan Cox01e1abb2008-07-22 11:16:55 +0100381 wake_up(&tty_ldisc_wait);
382}
383
384/**
Alan Coxf2c4c652009-06-11 12:50:58 +0100385 * tty_ldisc_flush - flush line discipline queue
386 * @tty: tty
387 *
388 * Flush the line discipline queue (if any) for this tty. If there
389 * is no line discipline active this is a no-op.
390 */
391
392void tty_ldisc_flush(struct tty_struct *tty)
393{
394 struct tty_ldisc *ld = tty_ldisc_ref(tty);
395 if (ld) {
396 if (ld->ops->flush_buffer)
397 ld->ops->flush_buffer(tty);
398 tty_ldisc_deref(ld);
399 }
400 tty_buffer_flush(tty);
401}
Alan Coxf2c4c652009-06-11 12:50:58 +0100402EXPORT_SYMBOL_GPL(tty_ldisc_flush);
403
404/**
Alan Cox01e1abb2008-07-22 11:16:55 +0100405 * tty_set_termios_ldisc - set ldisc field
406 * @tty: tty structure
407 * @num: line discipline number
408 *
409 * This is probably overkill for real world processors but
410 * they are not on hot paths so a little discipline won't do
411 * any harm.
412 *
413 * Locking: takes termios_mutex
414 */
415
416static void tty_set_termios_ldisc(struct tty_struct *tty, int num)
417{
418 mutex_lock(&tty->termios_mutex);
Alan Coxadc8d742012-07-14 15:31:47 +0100419 tty->termios.c_line = num;
Alan Cox01e1abb2008-07-22 11:16:55 +0100420 mutex_unlock(&tty->termios_mutex);
421}
422
Alan Coxc65c9bc2009-06-11 12:50:12 +0100423/**
424 * tty_ldisc_open - open a line discipline
425 * @tty: tty we are opening the ldisc on
426 * @ld: discipline to open
427 *
428 * A helper opening method. Also a convenient debugging and check
429 * point.
Arnd Bergmannec79d602010-06-01 22:53:01 +0200430 *
431 * Locking: always called with BTM already held.
Alan Coxc65c9bc2009-06-11 12:50:12 +0100432 */
433
434static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld)
435{
436 WARN_ON(test_and_set_bit(TTY_LDISC_OPEN, &tty->flags));
Alan Coxf18f9492009-11-30 13:18:35 +0000437 if (ld->ops->open) {
438 int ret;
Arnd Bergmannec79d602010-06-01 22:53:01 +0200439 /* BTM here locks versus a hangup event */
Alan Coxf18f9492009-11-30 13:18:35 +0000440 ret = ld->ops->open(tty);
Jiri Slaby7f90cfc2010-11-25 00:27:54 +0100441 if (ret)
442 clear_bit(TTY_LDISC_OPEN, &tty->flags);
Alan Coxf18f9492009-11-30 13:18:35 +0000443 return ret;
444 }
Alan Coxc65c9bc2009-06-11 12:50:12 +0100445 return 0;
446}
447
448/**
449 * tty_ldisc_close - close a line discipline
450 * @tty: tty we are opening the ldisc on
451 * @ld: discipline to close
452 *
453 * A helper close method. Also a convenient debugging and check
454 * point.
455 */
456
457static void tty_ldisc_close(struct tty_struct *tty, struct tty_ldisc *ld)
458{
459 WARN_ON(!test_bit(TTY_LDISC_OPEN, &tty->flags));
460 clear_bit(TTY_LDISC_OPEN, &tty->flags);
461 if (ld->ops->close)
462 ld->ops->close(tty);
463}
Alan Cox01e1abb2008-07-22 11:16:55 +0100464
465/**
466 * tty_ldisc_restore - helper for tty ldisc change
467 * @tty: tty to recover
468 * @old: previous ldisc
469 *
470 * Restore the previous line discipline or N_TTY when a line discipline
471 * change fails due to an open error
472 */
473
474static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old)
475{
476 char buf[64];
Alan Coxc65c9bc2009-06-11 12:50:12 +0100477 struct tty_ldisc *new_ldisc;
478 int r;
Alan Cox01e1abb2008-07-22 11:16:55 +0100479
480 /* There is an outstanding reference here so this is safe */
Alan Coxc65c9bc2009-06-11 12:50:12 +0100481 old = tty_ldisc_get(old->ops->num);
482 WARN_ON(IS_ERR(old));
Alan Cox01e1abb2008-07-22 11:16:55 +0100483 tty_ldisc_assign(tty, old);
484 tty_set_termios_ldisc(tty, old->ops->num);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100485 if (tty_ldisc_open(tty, old) < 0) {
486 tty_ldisc_put(old);
Alan Cox01e1abb2008-07-22 11:16:55 +0100487 /* This driver is always present */
Alan Cox852e99d2009-06-11 12:51:41 +0100488 new_ldisc = tty_ldisc_get(N_TTY);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100489 if (IS_ERR(new_ldisc))
Alan Cox01e1abb2008-07-22 11:16:55 +0100490 panic("n_tty: get");
Alan Coxc65c9bc2009-06-11 12:50:12 +0100491 tty_ldisc_assign(tty, new_ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100492 tty_set_termios_ldisc(tty, N_TTY);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100493 r = tty_ldisc_open(tty, new_ldisc);
494 if (r < 0)
495 panic("Couldn't open N_TTY ldisc for "
496 "%s --- error %d.",
497 tty_name(tty, buf), r);
Alan Cox01e1abb2008-07-22 11:16:55 +0100498 }
499}
500
501/**
Alan Coxc65c9bc2009-06-11 12:50:12 +0100502 * tty_ldisc_halt - shut down the line discipline
Alan Coxe8b70e72009-06-11 12:48:02 +0100503 * @tty: tty device
504 *
505 * Shut down the line discipline and work queue for this tty device.
506 * The TTY_LDISC flag being cleared ensures no further references can
507 * be obtained while the delayed work queue halt ensures that no more
508 * data is fed to the ldisc.
509 *
Linus Torvalds5c58cef2009-08-25 09:12:43 -0700510 * You need to do a 'flush_scheduled_work()' (outside the ldisc_mutex)
511 * in order to make sure any currently executing ldisc work is also
512 * flushed.
Alan Coxe8b70e72009-06-11 12:48:02 +0100513 */
514
Alan Coxc65c9bc2009-06-11 12:50:12 +0100515static int tty_ldisc_halt(struct tty_struct *tty)
Alan Coxe8b70e72009-06-11 12:48:02 +0100516{
Peter Hurley21622932013-03-11 16:44:21 -0400517 int scheduled;
Alan Coxe8b70e72009-06-11 12:48:02 +0100518 clear_bit(TTY_LDISC, &tty->flags);
Peter Hurley21622932013-03-11 16:44:21 -0400519 scheduled = cancel_work_sync(&tty->port->buf.work);
520 set_bit(TTY_LDISC_HALTED, &tty->flags);
521 return scheduled;
Alan Coxe8b70e72009-06-11 12:48:02 +0100522}
523
524/**
Tejun Heo0a1f1a02011-01-24 17:54:12 +0100525 * tty_ldisc_flush_works - flush all works of a tty
526 * @tty: tty device to flush works for
527 *
528 * Sync flush all works belonging to @tty.
529 */
530static void tty_ldisc_flush_works(struct tty_struct *tty)
531{
Tejun Heo43829732012-08-20 14:51:24 -0700532 flush_work(&tty->hangup_work);
533 flush_work(&tty->SAK_work);
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200534 flush_work(&tty->port->buf.work);
Tejun Heo0a1f1a02011-01-24 17:54:12 +0100535}
536
537/**
Jiri Slaby100eeae2010-10-31 23:17:51 +0100538 * tty_ldisc_wait_idle - wait for the ldisc to become idle
539 * @tty: tty to wait for
Jiri Slabydf92d052011-11-16 16:27:07 +0100540 * @timeout: for how long to wait at most
Jiri Slaby100eeae2010-10-31 23:17:51 +0100541 *
542 * Wait for the line discipline to become idle. The discipline must
543 * have been halted for this to guarantee it remains idle.
544 */
Jiri Slabydf92d052011-11-16 16:27:07 +0100545static int tty_ldisc_wait_idle(struct tty_struct *tty, long timeout)
Jiri Slaby100eeae2010-10-31 23:17:51 +0100546{
Jiri Slabydf92d052011-11-16 16:27:07 +0100547 long ret;
Ivo Sieben1541f842012-05-03 14:37:43 +0200548 ret = wait_event_timeout(tty->ldisc->wq_idle,
Jiri Slabydf92d052011-11-16 16:27:07 +0100549 atomic_read(&tty->ldisc->users) == 1, timeout);
Jiri Slaby100eeae2010-10-31 23:17:51 +0100550 return ret > 0 ? 0 : -EBUSY;
551}
552
553/**
Peter Hurley168942c2013-03-11 16:44:24 -0400554 * tty_ldisc_hangup_wait_idle - wait for the ldisc to become idle
555 * @tty: tty to wait for
556 *
557 * Wait for the line discipline to become idle. The discipline must
558 * have been halted for this to guarantee it remains idle.
559 *
560 * Caller must hold legacy and ->ldisc_mutex.
561 */
562static bool tty_ldisc_hangup_wait_idle(struct tty_struct *tty)
563{
564 while (tty->ldisc) { /* Not yet closed */
565 if (atomic_read(&tty->ldisc->users) != 1) {
566 char cur_n[TASK_COMM_LEN], tty_n[64];
567 long timeout = 3 * HZ;
568 tty_unlock(tty);
569
570 while (tty_ldisc_wait_idle(tty, timeout) == -EBUSY) {
571 timeout = MAX_SCHEDULE_TIMEOUT;
572 printk_ratelimited(KERN_WARNING
573 "%s: waiting (%s) for %s took too long, but we keep waiting...\n",
574 __func__, get_task_comm(cur_n, current),
575 tty_name(tty, tty_n));
576 }
577 /* must reacquire both locks and preserve lock order */
578 mutex_unlock(&tty->ldisc_mutex);
579 tty_lock(tty);
580 mutex_lock(&tty->ldisc_mutex);
581 continue;
582 }
583 break;
584 }
585 return !!tty->ldisc;
586}
587
588/**
Alan Cox01e1abb2008-07-22 11:16:55 +0100589 * tty_set_ldisc - set line discipline
590 * @tty: the terminal to set
591 * @ldisc: the line discipline
592 *
593 * Set the discipline of a tty line. Must be called from a process
Alan Coxc65c9bc2009-06-11 12:50:12 +0100594 * context. The ldisc change logic has to protect itself against any
595 * overlapping ldisc change (including on the other end of pty pairs),
596 * the close of one side of a tty/pty pair, and eventually hangup.
Alan Cox01e1abb2008-07-22 11:16:55 +0100597 *
Alan Coxc65c9bc2009-06-11 12:50:12 +0100598 * Locking: takes tty_ldisc_lock, termios_mutex
Alan Cox01e1abb2008-07-22 11:16:55 +0100599 */
600
601int tty_set_ldisc(struct tty_struct *tty, int ldisc)
602{
603 int retval;
Alan Coxc65c9bc2009-06-11 12:50:12 +0100604 struct tty_ldisc *o_ldisc, *new_ldisc;
605 int work, o_work = 0;
Alan Cox01e1abb2008-07-22 11:16:55 +0100606 struct tty_struct *o_tty;
607
Alan Coxc65c9bc2009-06-11 12:50:12 +0100608 new_ldisc = tty_ldisc_get(ldisc);
609 if (IS_ERR(new_ldisc))
610 return PTR_ERR(new_ldisc);
611
Alan Cox89c8d912012-08-08 16:30:13 +0100612 tty_lock(tty);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100613 /*
614 * We need to look at the tty locking here for pty/tty pairs
615 * when both sides try to change in parallel.
616 */
617
618 o_tty = tty->link; /* o_tty is the pty side or NULL */
619
620
621 /*
622 * Check the no-op case
623 */
624
625 if (tty->ldisc->ops->num == ldisc) {
Alan Cox89c8d912012-08-08 16:30:13 +0100626 tty_unlock(tty);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100627 tty_ldisc_put(new_ldisc);
628 return 0;
629 }
Alan Cox01e1abb2008-07-22 11:16:55 +0100630
Alan Cox89c8d912012-08-08 16:30:13 +0100631 tty_unlock(tty);
Alan Cox01e1abb2008-07-22 11:16:55 +0100632 /*
633 * Problem: What do we do if this blocks ?
Alan Coxc65c9bc2009-06-11 12:50:12 +0100634 * We could deadlock here
Alan Cox01e1abb2008-07-22 11:16:55 +0100635 */
636
637 tty_wait_until_sent(tty, 0);
638
Alan Cox89c8d912012-08-08 16:30:13 +0100639 tty_lock(tty);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100640 mutex_lock(&tty->ldisc_mutex);
Alan Cox01e1abb2008-07-22 11:16:55 +0100641
642 /*
Alan Coxc65c9bc2009-06-11 12:50:12 +0100643 * We could be midstream of another ldisc change which has
644 * dropped the lock during processing. If so we need to wait.
645 */
646
647 while (test_bit(TTY_LDISC_CHANGING, &tty->flags)) {
648 mutex_unlock(&tty->ldisc_mutex);
Alan Cox89c8d912012-08-08 16:30:13 +0100649 tty_unlock(tty);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100650 wait_event(tty_ldisc_wait,
651 test_bit(TTY_LDISC_CHANGING, &tty->flags) == 0);
Alan Cox89c8d912012-08-08 16:30:13 +0100652 tty_lock(tty);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100653 mutex_lock(&tty->ldisc_mutex);
654 }
Alan Coxeeb89d92009-11-30 13:18:29 +0000655
Alan Coxc65c9bc2009-06-11 12:50:12 +0100656 set_bit(TTY_LDISC_CHANGING, &tty->flags);
Alan Cox852e99d2009-06-11 12:51:41 +0100657
Alan Coxc65c9bc2009-06-11 12:50:12 +0100658 /*
Alan Cox01e1abb2008-07-22 11:16:55 +0100659 * No more input please, we are switching. The new ldisc
660 * will update this value in the ldisc open function
661 */
662
663 tty->receive_room = 0;
664
665 o_ldisc = tty->ldisc;
Alan Coxeeb89d92009-11-30 13:18:29 +0000666
Alan Cox89c8d912012-08-08 16:30:13 +0100667 tty_unlock(tty);
Alan Cox01e1abb2008-07-22 11:16:55 +0100668 /*
669 * Make sure we don't change while someone holds a
670 * reference to the line discipline. The TTY_LDISC bit
671 * prevents anyone taking a reference once it is clear.
672 * We need the lock to avoid racing reference takers.
Alan Coxc9b39762009-01-02 13:44:56 +0000673 *
674 * We must clear the TTY_LDISC bit here to avoid a livelock
675 * with a userspace app continually trying to use the tty in
676 * parallel to the change and re-referencing the tty.
Alan Cox01e1abb2008-07-22 11:16:55 +0100677 */
678
Alan Coxc65c9bc2009-06-11 12:50:12 +0100679 work = tty_ldisc_halt(tty);
Alan Cox01e1abb2008-07-22 11:16:55 +0100680 if (o_tty)
Alan Coxc65c9bc2009-06-11 12:50:12 +0100681 o_work = tty_ldisc_halt(o_tty);
682
Alan Cox01e1abb2008-07-22 11:16:55 +0100683 /*
Alan Coxc65c9bc2009-06-11 12:50:12 +0100684 * Wait for ->hangup_work and ->buf.work handlers to terminate.
685 * We must drop the mutex here in case a hangup is also in process.
Alan Cox01e1abb2008-07-22 11:16:55 +0100686 */
687
Alan Coxc65c9bc2009-06-11 12:50:12 +0100688 mutex_unlock(&tty->ldisc_mutex);
689
Tejun Heo0a1f1a02011-01-24 17:54:12 +0100690 tty_ldisc_flush_works(tty);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100691
Jiri Slabydf92d052011-11-16 16:27:07 +0100692 retval = tty_ldisc_wait_idle(tty, 5 * HZ);
Jiri Slaby100eeae2010-10-31 23:17:51 +0100693
Alan Cox89c8d912012-08-08 16:30:13 +0100694 tty_lock(tty);
Arnd Bergmann60af22d2010-06-01 22:53:06 +0200695 mutex_lock(&tty->ldisc_mutex);
Jiri Slaby100eeae2010-10-31 23:17:51 +0100696
697 /* handle wait idle failure locked */
698 if (retval) {
699 tty_ldisc_put(new_ldisc);
700 goto enable;
701 }
702
Shachar Shemesh40c9f612012-07-10 07:54:13 +0300703 if (test_bit(TTY_HUPPING, &tty->flags)) {
Alan Coxc65c9bc2009-06-11 12:50:12 +0100704 /* We were raced by the hangup method. It will have stomped
705 the ldisc data and closed the ldisc down */
706 clear_bit(TTY_LDISC_CHANGING, &tty->flags);
707 mutex_unlock(&tty->ldisc_mutex);
708 tty_ldisc_put(new_ldisc);
Alan Cox89c8d912012-08-08 16:30:13 +0100709 tty_unlock(tty);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100710 return -EIO;
711 }
712
Alan Cox01e1abb2008-07-22 11:16:55 +0100713 /* Shutdown the current discipline. */
Alan Coxc65c9bc2009-06-11 12:50:12 +0100714 tty_ldisc_close(tty, o_ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100715
716 /* Now set up the new line discipline. */
Alan Coxc65c9bc2009-06-11 12:50:12 +0100717 tty_ldisc_assign(tty, new_ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100718 tty_set_termios_ldisc(tty, ldisc);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100719
720 retval = tty_ldisc_open(tty, new_ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100721 if (retval < 0) {
Alan Coxc65c9bc2009-06-11 12:50:12 +0100722 /* Back to the old one or N_TTY if we can't */
723 tty_ldisc_put(new_ldisc);
724 tty_ldisc_restore(tty, o_ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100725 }
Alan Coxc65c9bc2009-06-11 12:50:12 +0100726
Alan Cox01e1abb2008-07-22 11:16:55 +0100727 /* At this point we hold a reference to the new ldisc and a
728 a reference to the old ldisc. If we ended up flipping back
729 to the existing ldisc we have two references to it */
730
Alan Coxc65c9bc2009-06-11 12:50:12 +0100731 if (tty->ldisc->ops->num != o_ldisc->ops->num && tty->ops->set_ldisc)
Alan Cox01e1abb2008-07-22 11:16:55 +0100732 tty->ops->set_ldisc(tty);
733
Alan Coxc65c9bc2009-06-11 12:50:12 +0100734 tty_ldisc_put(o_ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100735
Jiri Slaby100eeae2010-10-31 23:17:51 +0100736enable:
Alan Cox01e1abb2008-07-22 11:16:55 +0100737 /*
Alan Coxc65c9bc2009-06-11 12:50:12 +0100738 * Allow ldisc referencing to occur again
Alan Cox01e1abb2008-07-22 11:16:55 +0100739 */
740
741 tty_ldisc_enable(tty);
742 if (o_tty)
743 tty_ldisc_enable(o_tty);
744
Alan Coxc65c9bc2009-06-11 12:50:12 +0100745 /* Restart the work queue in case no characters kick it off. Safe if
Alan Cox01e1abb2008-07-22 11:16:55 +0100746 already running */
747 if (work)
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200748 schedule_work(&tty->port->buf.work);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100749 if (o_work)
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200750 schedule_work(&o_tty->port->buf.work);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100751 mutex_unlock(&tty->ldisc_mutex);
Alan Cox89c8d912012-08-08 16:30:13 +0100752 tty_unlock(tty);
Alan Cox01e1abb2008-07-22 11:16:55 +0100753 return retval;
754}
755
Alan Coxc65c9bc2009-06-11 12:50:12 +0100756/**
757 * tty_reset_termios - reset terminal state
758 * @tty: tty to reset
759 *
760 * Restore a terminal to the driver default state.
761 */
762
763static void tty_reset_termios(struct tty_struct *tty)
764{
765 mutex_lock(&tty->termios_mutex);
Alan Coxadc8d742012-07-14 15:31:47 +0100766 tty->termios = tty->driver->init_termios;
767 tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios);
768 tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100769 mutex_unlock(&tty->termios_mutex);
770}
771
772
773/**
774 * tty_ldisc_reinit - reinitialise the tty ldisc
775 * @tty: tty to reinit
Alan Cox638b9642010-02-08 10:09:26 +0000776 * @ldisc: line discipline to reinitialize
Alan Coxc65c9bc2009-06-11 12:50:12 +0100777 *
Alan Cox638b9642010-02-08 10:09:26 +0000778 * Switch the tty to a line discipline and leave the ldisc
779 * state closed
Alan Coxc65c9bc2009-06-11 12:50:12 +0100780 */
781
Philippe Rétornaz1c95ba12010-10-27 17:13:21 +0200782static int tty_ldisc_reinit(struct tty_struct *tty, int ldisc)
Alan Coxc65c9bc2009-06-11 12:50:12 +0100783{
Philippe Rétornaz1c95ba12010-10-27 17:13:21 +0200784 struct tty_ldisc *ld = tty_ldisc_get(ldisc);
785
786 if (IS_ERR(ld))
787 return -1;
Alan Coxc65c9bc2009-06-11 12:50:12 +0100788
789 tty_ldisc_close(tty, tty->ldisc);
790 tty_ldisc_put(tty->ldisc);
791 tty->ldisc = NULL;
792 /*
793 * Switch the line discipline back
794 */
Alan Coxc65c9bc2009-06-11 12:50:12 +0100795 tty_ldisc_assign(tty, ld);
Alan Cox638b9642010-02-08 10:09:26 +0000796 tty_set_termios_ldisc(tty, ldisc);
Philippe Rétornaz1c95ba12010-10-27 17:13:21 +0200797
798 return 0;
Alan Coxc65c9bc2009-06-11 12:50:12 +0100799}
800
801/**
802 * tty_ldisc_hangup - hangup ldisc reset
803 * @tty: tty being hung up
804 *
805 * Some tty devices reset their termios when they receive a hangup
806 * event. In that situation we must also switch back to N_TTY properly
807 * before we reset the termios data.
808 *
809 * Locking: We can take the ldisc mutex as the rest of the code is
810 * careful to allow for this.
811 *
812 * In the pty pair case this occurs in the close() path of the
813 * tty itself so we must be careful about locking rules.
814 */
815
816void tty_ldisc_hangup(struct tty_struct *tty)
817{
818 struct tty_ldisc *ld;
Alan Cox638b9642010-02-08 10:09:26 +0000819 int reset = tty->driver->flags & TTY_DRIVER_RESET_TERMIOS;
820 int err = 0;
Alan Coxc65c9bc2009-06-11 12:50:12 +0100821
822 /*
823 * FIXME! What are the locking issues here? This may me overdoing
824 * things... This question is especially important now that we've
825 * removed the irqlock.
826 */
827 ld = tty_ldisc_ref(tty);
828 if (ld != NULL) {
829 /* We may have no line discipline at this point */
830 if (ld->ops->flush_buffer)
831 ld->ops->flush_buffer(tty);
832 tty_driver_flush_buffer(tty);
833 if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
834 ld->ops->write_wakeup)
835 ld->ops->write_wakeup(tty);
836 if (ld->ops->hangup)
837 ld->ops->hangup(tty);
838 tty_ldisc_deref(ld);
839 }
840 /*
841 * FIXME: Once we trust the LDISC code better we can wait here for
842 * ldisc completion and fix the driver call race
843 */
844 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
845 wake_up_interruptible_poll(&tty->read_wait, POLLIN);
846 /*
847 * Shutdown the current line discipline, and reset it to
Alan Cox638b9642010-02-08 10:09:26 +0000848 * N_TTY if need be.
849 *
850 * Avoid racing set_ldisc or tty_ldisc_release
Alan Coxc65c9bc2009-06-11 12:50:12 +0100851 */
Alan Cox638b9642010-02-08 10:09:26 +0000852 mutex_lock(&tty->ldisc_mutex);
Arnd Bergmann60af22d2010-06-01 22:53:06 +0200853
854 /*
855 * this is like tty_ldisc_halt, but we need to give up
Linus Torvaldsf23eb2b2011-03-22 16:17:32 -0700856 * the BTM before calling cancel_work_sync, which may
857 * need to wait for another function taking the BTM
Arnd Bergmann60af22d2010-06-01 22:53:06 +0200858 */
859 clear_bit(TTY_LDISC, &tty->flags);
Alan Cox89c8d912012-08-08 16:30:13 +0100860 tty_unlock(tty);
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200861 cancel_work_sync(&tty->port->buf.work);
Peter Hurley21622932013-03-11 16:44:21 -0400862 set_bit(TTY_LDISC_HALTED, &tty->flags);
Arnd Bergmann60af22d2010-06-01 22:53:06 +0200863 mutex_unlock(&tty->ldisc_mutex);
Alan Cox89c8d912012-08-08 16:30:13 +0100864 tty_lock(tty);
Arnd Bergmann60af22d2010-06-01 22:53:06 +0200865 mutex_lock(&tty->ldisc_mutex);
866
Alan Cox638b9642010-02-08 10:09:26 +0000867 /* At this point we have a closed ldisc and we want to
868 reopen it. We could defer this to the next open but
869 it means auditing a lot of other paths so this is
870 a FIXME */
Peter Hurley168942c2013-03-11 16:44:24 -0400871 if (tty_ldisc_hangup_wait_idle(tty)) {
Alan Cox638b9642010-02-08 10:09:26 +0000872 if (reset == 0) {
Philippe Rétornaz1c95ba12010-10-27 17:13:21 +0200873
Alan Coxadc8d742012-07-14 15:31:47 +0100874 if (!tty_ldisc_reinit(tty, tty->termios.c_line))
Philippe Rétornaz1c95ba12010-10-27 17:13:21 +0200875 err = tty_ldisc_open(tty, tty->ldisc);
876 else
877 err = 1;
Alan Coxc8d50042009-07-16 16:05:08 +0100878 }
Alan Cox638b9642010-02-08 10:09:26 +0000879 /* If the re-open fails or we reset then go to N_TTY. The
880 N_TTY open cannot fail */
881 if (reset || err) {
Philippe Rétornaz1c95ba12010-10-27 17:13:21 +0200882 BUG_ON(tty_ldisc_reinit(tty, N_TTY));
Alan Cox638b9642010-02-08 10:09:26 +0000883 WARN_ON(tty_ldisc_open(tty, tty->ldisc));
884 }
885 tty_ldisc_enable(tty);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100886 }
Alan Cox638b9642010-02-08 10:09:26 +0000887 mutex_unlock(&tty->ldisc_mutex);
888 if (reset)
889 tty_reset_termios(tty);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100890}
Alan Cox01e1abb2008-07-22 11:16:55 +0100891
892/**
893 * tty_ldisc_setup - open line discipline
894 * @tty: tty being shut down
895 * @o_tty: pair tty for pty/tty pairs
896 *
897 * Called during the initial open of a tty/pty pair in order to set up the
Alan Coxc65c9bc2009-06-11 12:50:12 +0100898 * line disciplines and bind them to the tty. This has no locking issues
899 * as the device isn't yet active.
Alan Cox01e1abb2008-07-22 11:16:55 +0100900 */
901
902int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty)
903{
Alan Coxc65c9bc2009-06-11 12:50:12 +0100904 struct tty_ldisc *ld = tty->ldisc;
Alan Cox01e1abb2008-07-22 11:16:55 +0100905 int retval;
906
Alan Coxc65c9bc2009-06-11 12:50:12 +0100907 retval = tty_ldisc_open(tty, ld);
908 if (retval)
909 return retval;
910
911 if (o_tty) {
912 retval = tty_ldisc_open(o_tty, o_tty->ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100913 if (retval) {
Alan Coxc65c9bc2009-06-11 12:50:12 +0100914 tty_ldisc_close(tty, ld);
Alan Cox01e1abb2008-07-22 11:16:55 +0100915 return retval;
916 }
917 tty_ldisc_enable(o_tty);
918 }
919 tty_ldisc_enable(tty);
920 return 0;
921}
Alan Cox89c8d912012-08-08 16:30:13 +0100922
923static void tty_ldisc_kill(struct tty_struct *tty)
924{
Jiri Slaby31e12122012-10-18 22:26:33 +0200925 /* There cannot be users from userspace now. But there still might be
926 * drivers holding a reference via tty_ldisc_ref. Do not steal them the
927 * ldisc until they are done. */
928 tty_ldisc_wait_idle(tty, MAX_SCHEDULE_TIMEOUT);
929
Alan Cox89c8d912012-08-08 16:30:13 +0100930 mutex_lock(&tty->ldisc_mutex);
931 /*
932 * Now kill off the ldisc
933 */
934 tty_ldisc_close(tty, tty->ldisc);
935 tty_ldisc_put(tty->ldisc);
936 /* Force an oops if we mess this up */
937 tty->ldisc = NULL;
938
939 /* Ensure the next open requests the N_TTY ldisc */
940 tty_set_termios_ldisc(tty, N_TTY);
941 mutex_unlock(&tty->ldisc_mutex);
942}
943
Alan Cox01e1abb2008-07-22 11:16:55 +0100944/**
945 * tty_ldisc_release - release line discipline
946 * @tty: tty being shut down
947 * @o_tty: pair tty for pty/tty pairs
948 *
Alan Cox852e99d2009-06-11 12:51:41 +0100949 * Called during the final close of a tty/pty pair in order to shut down
950 * the line discpline layer. On exit the ldisc assigned is N_TTY and the
Alan Coxc65c9bc2009-06-11 12:50:12 +0100951 * ldisc has not been opened.
Alan Cox01e1abb2008-07-22 11:16:55 +0100952 */
953
954void tty_ldisc_release(struct tty_struct *tty, struct tty_struct *o_tty)
955{
Alan Cox01e1abb2008-07-22 11:16:55 +0100956 /*
957 * Prevent flush_to_ldisc() from rescheduling the work for later. Then
958 * kill any delayed work. As this is the final close it does not
959 * race with the set_ldisc code path.
960 */
Alan Cox01e1abb2008-07-22 11:16:55 +0100961
Alan Coxe8b70e72009-06-11 12:48:02 +0100962 tty_ldisc_halt(tty);
Sebastian Andrzej Siewior852e4a82012-12-25 23:02:48 +0100963 if (o_tty)
Alan Cox89c8d912012-08-08 16:30:13 +0100964 tty_ldisc_halt(o_tty);
Alan Coxd1552552012-07-27 18:02:54 +0100965
Sebastian Andrzej Siewior852e4a82012-12-25 23:02:48 +0100966 tty_ldisc_flush_works(tty);
967 if (o_tty)
968 tty_ldisc_flush_works(o_tty);
969
970 tty_lock_pair(tty, o_tty);
Alan Cox6d31a882012-07-14 15:31:27 +0100971 /* This will need doing differently if we need to lock */
Alan Cox89c8d912012-08-08 16:30:13 +0100972 tty_ldisc_kill(tty);
Alan Cox89c8d912012-08-08 16:30:13 +0100973 if (o_tty)
974 tty_ldisc_kill(o_tty);
975
976 tty_unlock_pair(tty, o_tty);
Alan Coxaef29bc2009-06-29 15:21:47 +0100977 /* And the memory resources remaining (buffers, termios) will be
978 disposed of when the kref hits zero */
Alan Cox01e1abb2008-07-22 11:16:55 +0100979}
980
981/**
982 * tty_ldisc_init - ldisc setup for new tty
983 * @tty: tty being allocated
984 *
985 * Set up the line discipline objects for a newly allocated tty. Note that
986 * the tty structure is not completely set up when this call is made.
987 */
988
989void tty_ldisc_init(struct tty_struct *tty)
990{
Alan Coxc65c9bc2009-06-11 12:50:12 +0100991 struct tty_ldisc *ld = tty_ldisc_get(N_TTY);
992 if (IS_ERR(ld))
Alan Cox01e1abb2008-07-22 11:16:55 +0100993 panic("n_tty: init_tty");
Alan Coxc65c9bc2009-06-11 12:50:12 +0100994 tty_ldisc_assign(tty, ld);
Alan Cox01e1abb2008-07-22 11:16:55 +0100995}
996
Jiri Slaby67166712011-03-23 10:48:35 +0100997/**
998 * tty_ldisc_init - ldisc cleanup for new tty
999 * @tty: tty that was allocated recently
1000 *
1001 * The tty structure must not becompletely set up (tty_ldisc_setup) when
1002 * this call is made.
1003 */
1004void tty_ldisc_deinit(struct tty_struct *tty)
1005{
1006 put_ldisc(tty->ldisc);
1007 tty_ldisc_assign(tty, NULL);
1008}
1009
Alan Cox01e1abb2008-07-22 11:16:55 +01001010void tty_ldisc_begin(void)
1011{
1012 /* Setup the default TTY line discipline. */
1013 (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
1014}