blob: 328ff5b544a5622405d98bf6c1da9107bdf74ef6 [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
Linus Torvalds65b77042009-08-03 11:11:19 -070045static inline struct tty_ldisc *get_ldisc(struct tty_ldisc *ld)
46{
47 if (ld)
48 atomic_inc(&ld->users);
49 return ld;
50}
51
Linus Torvaldscbe93522009-08-03 14:54:56 -070052static void put_ldisc(struct tty_ldisc *ld)
Linus Torvalds65b77042009-08-03 11:11:19 -070053{
Linus Torvaldscbe93522009-08-03 14:54:56 -070054 unsigned long flags;
55
Linus Torvalds65b77042009-08-03 11:11:19 -070056 if (WARN_ON_ONCE(!ld))
57 return;
58
59 /*
60 * If this is the last user, free the ldisc, and
61 * release the ldisc ops.
Linus Torvaldscbe93522009-08-03 14:54:56 -070062 *
Ivo Siebenc9739942012-10-17 14:03:14 +020063 * We really want an "atomic_dec_and_raw_lock_irqsave()",
Linus Torvaldscbe93522009-08-03 14:54:56 -070064 * but we don't have it, so this does it by hand.
Linus Torvalds65b77042009-08-03 11:11:19 -070065 */
Ivo Siebenc9739942012-10-17 14:03:14 +020066 raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
67 if (atomic_dec_and_test(&ld->users)) {
Linus Torvalds65b77042009-08-03 11:11:19 -070068 struct tty_ldisc_ops *ldo = ld->ops;
69
Linus Torvalds65b77042009-08-03 11:11:19 -070070 ldo->refcount--;
71 module_put(ldo->owner);
Ivo Siebenc9739942012-10-17 14:03:14 +020072 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
Linus Torvaldscbe93522009-08-03 14:54:56 -070073
74 kfree(ld);
75 return;
Linus Torvalds65b77042009-08-03 11:11:19 -070076 }
Ivo Siebenc9739942012-10-17 14:03:14 +020077 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
Ivo Siebenbd5d7ce2012-12-18 15:48:50 +010078
79 if (waitqueue_active(&ld->wq_idle))
80 wake_up(&ld->wq_idle);
Linus Torvalds65b77042009-08-03 11:11:19 -070081}
82
Alan Cox01e1abb2008-07-22 11:16:55 +010083/**
84 * tty_register_ldisc - install a line discipline
85 * @disc: ldisc number
86 * @new_ldisc: pointer to the ldisc object
87 *
88 * Installs a new line discipline into the kernel. The discipline
89 * is set up as unreferenced and then made available to the kernel
90 * from this point onwards.
91 *
92 * Locking:
93 * takes tty_ldisc_lock to guard against ldisc races
94 */
95
96int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc)
97{
98 unsigned long flags;
99 int ret = 0;
100
101 if (disc < N_TTY || disc >= NR_LDISCS)
102 return -EINVAL;
103
Ivo Siebenc9739942012-10-17 14:03:14 +0200104 raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
Alan Cox01e1abb2008-07-22 11:16:55 +0100105 tty_ldiscs[disc] = new_ldisc;
106 new_ldisc->num = disc;
107 new_ldisc->refcount = 0;
Ivo Siebenc9739942012-10-17 14:03:14 +0200108 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
Alan Cox01e1abb2008-07-22 11:16:55 +0100109
110 return ret;
111}
112EXPORT_SYMBOL(tty_register_ldisc);
113
114/**
115 * tty_unregister_ldisc - unload a line discipline
116 * @disc: ldisc number
117 * @new_ldisc: pointer to the ldisc object
118 *
119 * Remove a line discipline from the kernel providing it is not
120 * currently in use.
121 *
122 * Locking:
123 * takes tty_ldisc_lock to guard against ldisc races
124 */
125
126int tty_unregister_ldisc(int disc)
127{
128 unsigned long flags;
129 int ret = 0;
130
131 if (disc < N_TTY || disc >= NR_LDISCS)
132 return -EINVAL;
133
Ivo Siebenc9739942012-10-17 14:03:14 +0200134 raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
Alan Cox01e1abb2008-07-22 11:16:55 +0100135 if (tty_ldiscs[disc]->refcount)
136 ret = -EBUSY;
137 else
138 tty_ldiscs[disc] = NULL;
Ivo Siebenc9739942012-10-17 14:03:14 +0200139 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
Alan Cox01e1abb2008-07-22 11:16:55 +0100140
141 return ret;
142}
143EXPORT_SYMBOL(tty_unregister_ldisc);
144
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700145static struct tty_ldisc_ops *get_ldops(int disc)
146{
147 unsigned long flags;
148 struct tty_ldisc_ops *ldops, *ret;
149
Ivo Siebenc9739942012-10-17 14:03:14 +0200150 raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700151 ret = ERR_PTR(-EINVAL);
152 ldops = tty_ldiscs[disc];
153 if (ldops) {
154 ret = ERR_PTR(-EAGAIN);
155 if (try_module_get(ldops->owner)) {
156 ldops->refcount++;
157 ret = ldops;
158 }
159 }
Ivo Siebenc9739942012-10-17 14:03:14 +0200160 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700161 return ret;
162}
163
164static void put_ldops(struct tty_ldisc_ops *ldops)
165{
166 unsigned long flags;
167
Ivo Siebenc9739942012-10-17 14:03:14 +0200168 raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700169 ldops->refcount--;
170 module_put(ldops->owner);
Ivo Siebenc9739942012-10-17 14:03:14 +0200171 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700172}
Alan Cox01e1abb2008-07-22 11:16:55 +0100173
174/**
Alan Cox01e1abb2008-07-22 11:16:55 +0100175 * tty_ldisc_get - take a reference to an ldisc
176 * @disc: ldisc number
Alan Cox01e1abb2008-07-22 11:16:55 +0100177 *
178 * Takes a reference to a line discipline. Deals with refcounts and
179 * module locking counts. Returns NULL if the discipline is not available.
180 * Returns a pointer to the discipline and bumps the ref count if it is
181 * available
182 *
183 * Locking:
184 * takes tty_ldisc_lock to guard against ldisc races
185 */
186
Alan Coxc65c9bc2009-06-11 12:50:12 +0100187static struct tty_ldisc *tty_ldisc_get(int disc)
Alan Cox01e1abb2008-07-22 11:16:55 +0100188{
Alan Coxc65c9bc2009-06-11 12:50:12 +0100189 struct tty_ldisc *ld;
Linus Torvalds182274f2009-08-03 16:01:28 -0700190 struct tty_ldisc_ops *ldops;
Alan Cox01e1abb2008-07-22 11:16:55 +0100191
192 if (disc < N_TTY || disc >= NR_LDISCS)
Alan Coxc65c9bc2009-06-11 12:50:12 +0100193 return ERR_PTR(-EINVAL);
Linus Torvalds182274f2009-08-03 16:01:28 -0700194
195 /*
196 * Get the ldisc ops - we may need to request them to be loaded
197 * dynamically and try again.
198 */
199 ldops = get_ldops(disc);
200 if (IS_ERR(ldops)) {
Alan Cox01e1abb2008-07-22 11:16:55 +0100201 request_module("tty-ldisc-%d", disc);
Linus Torvalds182274f2009-08-03 16:01:28 -0700202 ldops = get_ldops(disc);
203 if (IS_ERR(ldops))
204 return ERR_CAST(ldops);
Alan Cox01e1abb2008-07-22 11:16:55 +0100205 }
Linus Torvalds182274f2009-08-03 16:01:28 -0700206
207 ld = kmalloc(sizeof(struct tty_ldisc), GFP_KERNEL);
208 if (ld == NULL) {
209 put_ldops(ldops);
210 return ERR_PTR(-ENOMEM);
211 }
212
213 ld->ops = ldops;
214 atomic_set(&ld->users, 1);
Ivo Sieben1541f842012-05-03 14:37:43 +0200215 init_waitqueue_head(&ld->wq_idle);
216
Alan Coxc65c9bc2009-06-11 12:50:12 +0100217 return ld;
Alan Cox01e1abb2008-07-22 11:16:55 +0100218}
219
Alan Cox852e99d2009-06-11 12:51:41 +0100220static void *tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos)
Alan Cox01e1abb2008-07-22 11:16:55 +0100221{
222 return (*pos < NR_LDISCS) ? pos : NULL;
223}
224
Alan Cox852e99d2009-06-11 12:51:41 +0100225static void *tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos)
Alan Cox01e1abb2008-07-22 11:16:55 +0100226{
227 (*pos)++;
228 return (*pos < NR_LDISCS) ? pos : NULL;
229}
230
231static void tty_ldiscs_seq_stop(struct seq_file *m, void *v)
232{
233}
234
235static int tty_ldiscs_seq_show(struct seq_file *m, void *v)
236{
237 int i = *(loff_t *)v;
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700238 struct tty_ldisc_ops *ldops;
Alan Cox852e99d2009-06-11 12:51:41 +0100239
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700240 ldops = get_ldops(i);
241 if (IS_ERR(ldops))
Alan Cox01e1abb2008-07-22 11:16:55 +0100242 return 0;
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700243 seq_printf(m, "%-10s %2d\n", ldops->name ? ldops->name : "???", i);
244 put_ldops(ldops);
Alan Cox01e1abb2008-07-22 11:16:55 +0100245 return 0;
246}
247
248static const struct seq_operations tty_ldiscs_seq_ops = {
249 .start = tty_ldiscs_seq_start,
250 .next = tty_ldiscs_seq_next,
251 .stop = tty_ldiscs_seq_stop,
252 .show = tty_ldiscs_seq_show,
253};
254
255static int proc_tty_ldiscs_open(struct inode *inode, struct file *file)
256{
257 return seq_open(file, &tty_ldiscs_seq_ops);
258}
259
260const struct file_operations tty_ldiscs_proc_fops = {
261 .owner = THIS_MODULE,
262 .open = proc_tty_ldiscs_open,
263 .read = seq_read,
264 .llseek = seq_lseek,
265 .release = seq_release,
266};
267
268/**
269 * tty_ldisc_assign - set ldisc on a tty
270 * @tty: tty to assign
271 * @ld: line discipline
272 *
273 * Install an instance of a line discipline into a tty structure. The
Alan Cox8d2ead72009-06-16 17:00:26 +0100274 * ldisc must have a reference count above zero to ensure it remains.
Alan Cox01e1abb2008-07-22 11:16:55 +0100275 * The tty instance refcount starts at zero.
276 *
277 * Locking:
278 * Caller must hold references
279 */
280
281static void tty_ldisc_assign(struct tty_struct *tty, struct tty_ldisc *ld)
282{
Alan Coxc65c9bc2009-06-11 12:50:12 +0100283 tty->ldisc = ld;
Alan Cox01e1abb2008-07-22 11:16:55 +0100284}
285
286/**
287 * tty_ldisc_try - internal helper
288 * @tty: the tty
289 *
290 * Make a single attempt to grab and bump the refcount on
291 * the tty ldisc. Return 0 on failure or 1 on success. This is
292 * used to implement both the waiting and non waiting versions
293 * of tty_ldisc_ref
294 *
295 * Locking: takes tty_ldisc_lock
296 */
297
Linus Torvalds65b77042009-08-03 11:11:19 -0700298static struct tty_ldisc *tty_ldisc_try(struct tty_struct *tty)
Alan Cox01e1abb2008-07-22 11:16:55 +0100299{
300 unsigned long flags;
301 struct tty_ldisc *ld;
Alan Cox01e1abb2008-07-22 11:16:55 +0100302
Ivo Siebenc9739942012-10-17 14:03:14 +0200303 raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
Linus Torvalds65b77042009-08-03 11:11:19 -0700304 ld = NULL;
305 if (test_bit(TTY_LDISC, &tty->flags))
306 ld = get_ldisc(tty->ldisc);
Ivo Siebenc9739942012-10-17 14:03:14 +0200307 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
Linus Torvalds65b77042009-08-03 11:11:19 -0700308 return ld;
Alan Cox01e1abb2008-07-22 11:16:55 +0100309}
310
311/**
312 * tty_ldisc_ref_wait - wait for the tty ldisc
313 * @tty: tty device
314 *
315 * Dereference the line discipline for the terminal and take a
316 * reference to it. If the line discipline is in flux then
317 * wait patiently until it changes.
318 *
319 * Note: Must not be called from an IRQ/timer context. The caller
320 * must also be careful not to hold other locks that will deadlock
321 * against a discipline change, such as an existing ldisc reference
322 * (which we check for)
323 *
324 * Locking: call functions take tty_ldisc_lock
325 */
326
327struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
328{
Linus Torvalds65b77042009-08-03 11:11:19 -0700329 struct tty_ldisc *ld;
330
Alan Cox01e1abb2008-07-22 11:16:55 +0100331 /* wait_event is a macro */
Linus Torvalds65b77042009-08-03 11:11:19 -0700332 wait_event(tty_ldisc_wait, (ld = tty_ldisc_try(tty)) != NULL);
333 return ld;
Alan Cox01e1abb2008-07-22 11:16:55 +0100334}
Alan Cox01e1abb2008-07-22 11:16:55 +0100335EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
336
337/**
338 * tty_ldisc_ref - get the tty ldisc
339 * @tty: tty device
340 *
341 * Dereference the line discipline for the terminal and take a
342 * reference to it. If the line discipline is in flux then
343 * return NULL. Can be called from IRQ and timer functions.
344 *
345 * Locking: called functions take tty_ldisc_lock
346 */
347
348struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
349{
Linus Torvalds65b77042009-08-03 11:11:19 -0700350 return tty_ldisc_try(tty);
Alan Cox01e1abb2008-07-22 11:16:55 +0100351}
Alan Cox01e1abb2008-07-22 11:16:55 +0100352EXPORT_SYMBOL_GPL(tty_ldisc_ref);
353
354/**
355 * tty_ldisc_deref - free a tty ldisc reference
356 * @ld: reference to free up
357 *
358 * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May
359 * be called in IRQ context.
360 *
361 * Locking: takes tty_ldisc_lock
362 */
363
364void tty_ldisc_deref(struct tty_ldisc *ld)
365{
Linus Torvalds65b77042009-08-03 11:11:19 -0700366 put_ldisc(ld);
Alan Cox01e1abb2008-07-22 11:16:55 +0100367}
Alan Cox01e1abb2008-07-22 11:16:55 +0100368EXPORT_SYMBOL_GPL(tty_ldisc_deref);
369
Linus Torvalds65b77042009-08-03 11:11:19 -0700370static inline void tty_ldisc_put(struct tty_ldisc *ld)
371{
372 put_ldisc(ld);
373}
374
Alan Cox01e1abb2008-07-22 11:16:55 +0100375/**
376 * tty_ldisc_enable - allow ldisc use
377 * @tty: terminal to activate ldisc on
378 *
379 * Set the TTY_LDISC flag when the line discipline can be called
Alan Coxc9b39762009-01-02 13:44:56 +0000380 * again. Do necessary wakeups for existing sleepers. Clear the LDISC
381 * changing flag to indicate any ldisc change is now over.
Alan Cox01e1abb2008-07-22 11:16:55 +0100382 *
Alan Coxc9b39762009-01-02 13:44:56 +0000383 * Note: nobody should set the TTY_LDISC bit except via this function.
384 * Clearing directly is allowed.
Alan Cox01e1abb2008-07-22 11:16:55 +0100385 */
386
Peter Hurleyd9121562013-03-11 16:44:33 -0400387static void tty_ldisc_enable(struct tty_struct *tty)
Alan Cox01e1abb2008-07-22 11:16:55 +0100388{
Peter Hurley21622932013-03-11 16:44:21 -0400389 clear_bit(TTY_LDISC_HALTED, &tty->flags);
Alan Cox01e1abb2008-07-22 11:16:55 +0100390 set_bit(TTY_LDISC, &tty->flags);
Alan Coxc9b39762009-01-02 13:44:56 +0000391 clear_bit(TTY_LDISC_CHANGING, &tty->flags);
Alan Cox01e1abb2008-07-22 11:16:55 +0100392 wake_up(&tty_ldisc_wait);
393}
394
395/**
Alan Coxf2c4c652009-06-11 12:50:58 +0100396 * tty_ldisc_flush - flush line discipline queue
397 * @tty: tty
398 *
399 * Flush the line discipline queue (if any) for this tty. If there
400 * is no line discipline active this is a no-op.
401 */
402
403void tty_ldisc_flush(struct tty_struct *tty)
404{
405 struct tty_ldisc *ld = tty_ldisc_ref(tty);
406 if (ld) {
407 if (ld->ops->flush_buffer)
408 ld->ops->flush_buffer(tty);
409 tty_ldisc_deref(ld);
410 }
411 tty_buffer_flush(tty);
412}
Alan Coxf2c4c652009-06-11 12:50:58 +0100413EXPORT_SYMBOL_GPL(tty_ldisc_flush);
414
415/**
Alan Cox01e1abb2008-07-22 11:16:55 +0100416 * tty_set_termios_ldisc - set ldisc field
417 * @tty: tty structure
418 * @num: line discipline number
419 *
420 * This is probably overkill for real world processors but
421 * they are not on hot paths so a little discipline won't do
422 * any harm.
423 *
424 * Locking: takes termios_mutex
425 */
426
427static void tty_set_termios_ldisc(struct tty_struct *tty, int num)
428{
429 mutex_lock(&tty->termios_mutex);
Alan Coxadc8d742012-07-14 15:31:47 +0100430 tty->termios.c_line = num;
Alan Cox01e1abb2008-07-22 11:16:55 +0100431 mutex_unlock(&tty->termios_mutex);
432}
433
Alan Coxc65c9bc2009-06-11 12:50:12 +0100434/**
435 * tty_ldisc_open - open a line discipline
436 * @tty: tty we are opening the ldisc on
437 * @ld: discipline to open
438 *
439 * A helper opening method. Also a convenient debugging and check
440 * point.
Arnd Bergmannec79d602010-06-01 22:53:01 +0200441 *
442 * Locking: always called with BTM already held.
Alan Coxc65c9bc2009-06-11 12:50:12 +0100443 */
444
445static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld)
446{
447 WARN_ON(test_and_set_bit(TTY_LDISC_OPEN, &tty->flags));
Alan Coxf18f9492009-11-30 13:18:35 +0000448 if (ld->ops->open) {
449 int ret;
Arnd Bergmannec79d602010-06-01 22:53:01 +0200450 /* BTM here locks versus a hangup event */
Alan Coxf18f9492009-11-30 13:18:35 +0000451 ret = ld->ops->open(tty);
Jiri Slaby7f90cfc2010-11-25 00:27:54 +0100452 if (ret)
453 clear_bit(TTY_LDISC_OPEN, &tty->flags);
Alan Coxf18f9492009-11-30 13:18:35 +0000454 return ret;
455 }
Alan Coxc65c9bc2009-06-11 12:50:12 +0100456 return 0;
457}
458
459/**
460 * tty_ldisc_close - close a line discipline
461 * @tty: tty we are opening the ldisc on
462 * @ld: discipline to close
463 *
464 * A helper close method. Also a convenient debugging and check
465 * point.
466 */
467
468static void tty_ldisc_close(struct tty_struct *tty, struct tty_ldisc *ld)
469{
470 WARN_ON(!test_bit(TTY_LDISC_OPEN, &tty->flags));
471 clear_bit(TTY_LDISC_OPEN, &tty->flags);
472 if (ld->ops->close)
473 ld->ops->close(tty);
474}
Alan Cox01e1abb2008-07-22 11:16:55 +0100475
476/**
477 * tty_ldisc_restore - helper for tty ldisc change
478 * @tty: tty to recover
479 * @old: previous ldisc
480 *
481 * Restore the previous line discipline or N_TTY when a line discipline
482 * change fails due to an open error
483 */
484
485static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old)
486{
487 char buf[64];
Alan Coxc65c9bc2009-06-11 12:50:12 +0100488 struct tty_ldisc *new_ldisc;
489 int r;
Alan Cox01e1abb2008-07-22 11:16:55 +0100490
491 /* There is an outstanding reference here so this is safe */
Alan Coxc65c9bc2009-06-11 12:50:12 +0100492 old = tty_ldisc_get(old->ops->num);
493 WARN_ON(IS_ERR(old));
Alan Cox01e1abb2008-07-22 11:16:55 +0100494 tty_ldisc_assign(tty, old);
495 tty_set_termios_ldisc(tty, old->ops->num);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100496 if (tty_ldisc_open(tty, old) < 0) {
497 tty_ldisc_put(old);
Alan Cox01e1abb2008-07-22 11:16:55 +0100498 /* This driver is always present */
Alan Cox852e99d2009-06-11 12:51:41 +0100499 new_ldisc = tty_ldisc_get(N_TTY);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100500 if (IS_ERR(new_ldisc))
Alan Cox01e1abb2008-07-22 11:16:55 +0100501 panic("n_tty: get");
Alan Coxc65c9bc2009-06-11 12:50:12 +0100502 tty_ldisc_assign(tty, new_ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100503 tty_set_termios_ldisc(tty, N_TTY);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100504 r = tty_ldisc_open(tty, new_ldisc);
505 if (r < 0)
506 panic("Couldn't open N_TTY ldisc for "
507 "%s --- error %d.",
508 tty_name(tty, buf), r);
Alan Cox01e1abb2008-07-22 11:16:55 +0100509 }
510}
511
512/**
Jiri Slaby100eeae2010-10-31 23:17:51 +0100513 * tty_ldisc_wait_idle - wait for the ldisc to become idle
514 * @tty: tty to wait for
Jiri Slabydf92d052011-11-16 16:27:07 +0100515 * @timeout: for how long to wait at most
Jiri Slaby100eeae2010-10-31 23:17:51 +0100516 *
517 * Wait for the line discipline to become idle. The discipline must
518 * have been halted for this to guarantee it remains idle.
519 */
Jiri Slabydf92d052011-11-16 16:27:07 +0100520static int tty_ldisc_wait_idle(struct tty_struct *tty, long timeout)
Jiri Slaby100eeae2010-10-31 23:17:51 +0100521{
Jiri Slabydf92d052011-11-16 16:27:07 +0100522 long ret;
Ivo Sieben1541f842012-05-03 14:37:43 +0200523 ret = wait_event_timeout(tty->ldisc->wq_idle,
Jiri Slabydf92d052011-11-16 16:27:07 +0100524 atomic_read(&tty->ldisc->users) == 1, timeout);
Jiri Slaby100eeae2010-10-31 23:17:51 +0100525 return ret > 0 ? 0 : -EBUSY;
526}
527
528/**
Peter Hurley11cf48e2013-03-11 16:44:27 -0400529 * tty_ldisc_halt - shut down the line discipline
530 * @tty: tty device
Peter Hurleyf4cf7a32013-03-11 16:44:29 -0400531 * @o_tty: paired pty device (can be NULL)
Peter Hurleycf528472013-03-11 16:44:28 -0400532 * @timeout: # of jiffies to wait for ldisc refs to be released
Peter Hurley11cf48e2013-03-11 16:44:27 -0400533 *
Peter Hurleyf4cf7a32013-03-11 16:44:29 -0400534 * Shut down the line discipline and work queue for this tty device and
535 * its paired pty (if exists). Clearing the TTY_LDISC flag ensures
Peter Hurley4f98d462013-03-11 16:44:34 -0400536 * no further references can be obtained, while waiting for existing
537 * references to be released ensures no more data is fed to the ldisc.
Peter Hurleycf528472013-03-11 16:44:28 -0400538 *
Peter Hurley11cf48e2013-03-11 16:44:27 -0400539 * You need to do a 'flush_scheduled_work()' (outside the ldisc_mutex)
540 * in order to make sure any currently executing ldisc work is also
541 * flushed.
542 */
543
Peter Hurleyf4cf7a32013-03-11 16:44:29 -0400544static int tty_ldisc_halt(struct tty_struct *tty, struct tty_struct *o_tty,
Peter Hurley4f98d462013-03-11 16:44:34 -0400545 long timeout)
Peter Hurley11cf48e2013-03-11 16:44:27 -0400546{
Peter Hurley4f98d462013-03-11 16:44:34 -0400547 int retval;
Peter Hurleycf528472013-03-11 16:44:28 -0400548
Peter Hurley11cf48e2013-03-11 16:44:27 -0400549 clear_bit(TTY_LDISC, &tty->flags);
Peter Hurleyf4cf7a32013-03-11 16:44:29 -0400550 if (o_tty)
551 clear_bit(TTY_LDISC, &o_tty->flags);
552
Peter Hurleycf528472013-03-11 16:44:28 -0400553 retval = tty_ldisc_wait_idle(tty, timeout);
Peter Hurleyf4cf7a32013-03-11 16:44:29 -0400554 if (!retval && o_tty)
555 retval = tty_ldisc_wait_idle(o_tty, timeout);
Peter Hurleycf528472013-03-11 16:44:28 -0400556 if (retval)
557 return retval;
558
Peter Hurley11cf48e2013-03-11 16:44:27 -0400559 set_bit(TTY_LDISC_HALTED, &tty->flags);
Peter Hurley4f98d462013-03-11 16:44:34 -0400560 if (o_tty)
Peter Hurleyf4cf7a32013-03-11 16:44:29 -0400561 set_bit(TTY_LDISC_HALTED, &o_tty->flags);
Peter Hurleyf4cf7a32013-03-11 16:44:29 -0400562
Peter Hurleycf528472013-03-11 16:44:28 -0400563 return 0;
Peter Hurley11cf48e2013-03-11 16:44:27 -0400564}
565
566/**
Peter Hurley76bc35e2013-03-11 16:44:26 -0400567 * tty_ldisc_hangup_halt - halt the line discipline for hangup
568 * @tty: tty being hung up
Peter Hurley168942c2013-03-11 16:44:24 -0400569 *
Peter Hurley76bc35e2013-03-11 16:44:26 -0400570 * Shut down the line discipline and work queue for the tty device
571 * being hungup. Clear the TTY_LDISC flag to ensure no further
Peter Hurley4f98d462013-03-11 16:44:34 -0400572 * references can be obtained and wait for remaining references to be
573 * released to ensure no more data is fed to this ldisc.
Peter Hurley168942c2013-03-11 16:44:24 -0400574 * Caller must hold legacy and ->ldisc_mutex.
Peter Hurley2276ad92013-03-11 16:44:25 -0400575 *
576 * NB: tty_set_ldisc() is prevented from changing the ldisc concurrently
577 * with this function by checking the TTY_HUPPING flag.
Peter Hurley168942c2013-03-11 16:44:24 -0400578 */
Peter Hurley76bc35e2013-03-11 16:44:26 -0400579static bool tty_ldisc_hangup_halt(struct tty_struct *tty)
Peter Hurley168942c2013-03-11 16:44:24 -0400580{
Peter Hurley2276ad92013-03-11 16:44:25 -0400581 char cur_n[TASK_COMM_LEN], tty_n[64];
582 long timeout = 3 * HZ;
Peter Hurley168942c2013-03-11 16:44:24 -0400583
Peter Hurley76bc35e2013-03-11 16:44:26 -0400584 clear_bit(TTY_LDISC, &tty->flags);
585
Peter Hurley2276ad92013-03-11 16:44:25 -0400586 if (tty->ldisc) { /* Not yet closed */
587 tty_unlock(tty);
588
589 while (tty_ldisc_wait_idle(tty, timeout) == -EBUSY) {
590 timeout = MAX_SCHEDULE_TIMEOUT;
591 printk_ratelimited(KERN_WARNING
592 "%s: waiting (%s) for %s took too long, but we keep waiting...\n",
593 __func__, get_task_comm(cur_n, current),
594 tty_name(tty, tty_n));
Peter Hurley168942c2013-03-11 16:44:24 -0400595 }
Peter Hurley76bc35e2013-03-11 16:44:26 -0400596
Peter Hurley76bc35e2013-03-11 16:44:26 -0400597 set_bit(TTY_LDISC_HALTED, &tty->flags);
598
Peter Hurley2276ad92013-03-11 16:44:25 -0400599 /* must reacquire both locks and preserve lock order */
600 mutex_unlock(&tty->ldisc_mutex);
601 tty_lock(tty);
602 mutex_lock(&tty->ldisc_mutex);
Peter Hurley168942c2013-03-11 16:44:24 -0400603 }
604 return !!tty->ldisc;
605}
606
607/**
Alan Cox01e1abb2008-07-22 11:16:55 +0100608 * tty_set_ldisc - set line discipline
609 * @tty: the terminal to set
610 * @ldisc: the line discipline
611 *
612 * Set the discipline of a tty line. Must be called from a process
Alan Coxc65c9bc2009-06-11 12:50:12 +0100613 * context. The ldisc change logic has to protect itself against any
614 * overlapping ldisc change (including on the other end of pty pairs),
615 * the close of one side of a tty/pty pair, and eventually hangup.
Alan Cox01e1abb2008-07-22 11:16:55 +0100616 *
Alan Coxc65c9bc2009-06-11 12:50:12 +0100617 * Locking: takes tty_ldisc_lock, termios_mutex
Alan Cox01e1abb2008-07-22 11:16:55 +0100618 */
619
620int tty_set_ldisc(struct tty_struct *tty, int ldisc)
621{
622 int retval;
Alan Coxc65c9bc2009-06-11 12:50:12 +0100623 struct tty_ldisc *o_ldisc, *new_ldisc;
Alan Cox01e1abb2008-07-22 11:16:55 +0100624 struct tty_struct *o_tty;
625
Alan Coxc65c9bc2009-06-11 12:50:12 +0100626 new_ldisc = tty_ldisc_get(ldisc);
627 if (IS_ERR(new_ldisc))
628 return PTR_ERR(new_ldisc);
629
Alan Cox89c8d912012-08-08 16:30:13 +0100630 tty_lock(tty);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100631 /*
632 * We need to look at the tty locking here for pty/tty pairs
633 * when both sides try to change in parallel.
634 */
635
636 o_tty = tty->link; /* o_tty is the pty side or NULL */
637
638
639 /*
640 * Check the no-op case
641 */
642
643 if (tty->ldisc->ops->num == ldisc) {
Alan Cox89c8d912012-08-08 16:30:13 +0100644 tty_unlock(tty);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100645 tty_ldisc_put(new_ldisc);
646 return 0;
647 }
Alan Cox01e1abb2008-07-22 11:16:55 +0100648
Alan Cox89c8d912012-08-08 16:30:13 +0100649 tty_unlock(tty);
Alan Cox01e1abb2008-07-22 11:16:55 +0100650 /*
651 * Problem: What do we do if this blocks ?
Alan Coxc65c9bc2009-06-11 12:50:12 +0100652 * We could deadlock here
Alan Cox01e1abb2008-07-22 11:16:55 +0100653 */
654
655 tty_wait_until_sent(tty, 0);
656
Alan Cox89c8d912012-08-08 16:30:13 +0100657 tty_lock(tty);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100658 mutex_lock(&tty->ldisc_mutex);
Alan Cox01e1abb2008-07-22 11:16:55 +0100659
660 /*
Alan Coxc65c9bc2009-06-11 12:50:12 +0100661 * We could be midstream of another ldisc change which has
662 * dropped the lock during processing. If so we need to wait.
663 */
664
665 while (test_bit(TTY_LDISC_CHANGING, &tty->flags)) {
666 mutex_unlock(&tty->ldisc_mutex);
Alan Cox89c8d912012-08-08 16:30:13 +0100667 tty_unlock(tty);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100668 wait_event(tty_ldisc_wait,
669 test_bit(TTY_LDISC_CHANGING, &tty->flags) == 0);
Alan Cox89c8d912012-08-08 16:30:13 +0100670 tty_lock(tty);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100671 mutex_lock(&tty->ldisc_mutex);
672 }
Alan Coxeeb89d92009-11-30 13:18:29 +0000673
Alan Coxc65c9bc2009-06-11 12:50:12 +0100674 set_bit(TTY_LDISC_CHANGING, &tty->flags);
Alan Cox852e99d2009-06-11 12:51:41 +0100675
Alan Coxc65c9bc2009-06-11 12:50:12 +0100676 /*
Alan Cox01e1abb2008-07-22 11:16:55 +0100677 * No more input please, we are switching. The new ldisc
678 * will update this value in the ldisc open function
679 */
680
681 tty->receive_room = 0;
682
683 o_ldisc = tty->ldisc;
Alan Coxeeb89d92009-11-30 13:18:29 +0000684
Alan Cox89c8d912012-08-08 16:30:13 +0100685 tty_unlock(tty);
Alan Cox01e1abb2008-07-22 11:16:55 +0100686 /*
687 * Make sure we don't change while someone holds a
688 * reference to the line discipline. The TTY_LDISC bit
689 * prevents anyone taking a reference once it is clear.
690 * We need the lock to avoid racing reference takers.
Alan Coxc9b39762009-01-02 13:44:56 +0000691 *
692 * We must clear the TTY_LDISC bit here to avoid a livelock
693 * with a userspace app continually trying to use the tty in
694 * parallel to the change and re-referencing the tty.
Alan Cox01e1abb2008-07-22 11:16:55 +0100695 */
696
Peter Hurley4f98d462013-03-11 16:44:34 -0400697 retval = tty_ldisc_halt(tty, o_tty, 5 * HZ);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100698
Alan Cox01e1abb2008-07-22 11:16:55 +0100699 /*
Peter Hurleya2965b72013-03-11 16:44:35 -0400700 * Wait for hangup to complete, if pending.
Alan Coxc65c9bc2009-06-11 12:50:12 +0100701 * We must drop the mutex here in case a hangup is also in process.
Alan Cox01e1abb2008-07-22 11:16:55 +0100702 */
703
Alan Coxc65c9bc2009-06-11 12:50:12 +0100704 mutex_unlock(&tty->ldisc_mutex);
705
Peter Hurleya2965b72013-03-11 16:44:35 -0400706 flush_work(&tty->hangup_work);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100707
Alan Cox89c8d912012-08-08 16:30:13 +0100708 tty_lock(tty);
Arnd Bergmann60af22d2010-06-01 22:53:06 +0200709 mutex_lock(&tty->ldisc_mutex);
Jiri Slaby100eeae2010-10-31 23:17:51 +0100710
711 /* handle wait idle failure locked */
712 if (retval) {
713 tty_ldisc_put(new_ldisc);
714 goto enable;
715 }
716
Shachar Shemesh40c9f612012-07-10 07:54:13 +0300717 if (test_bit(TTY_HUPPING, &tty->flags)) {
Alan Coxc65c9bc2009-06-11 12:50:12 +0100718 /* We were raced by the hangup method. It will have stomped
719 the ldisc data and closed the ldisc down */
720 clear_bit(TTY_LDISC_CHANGING, &tty->flags);
721 mutex_unlock(&tty->ldisc_mutex);
722 tty_ldisc_put(new_ldisc);
Alan Cox89c8d912012-08-08 16:30:13 +0100723 tty_unlock(tty);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100724 return -EIO;
725 }
726
Alan Cox01e1abb2008-07-22 11:16:55 +0100727 /* Shutdown the current discipline. */
Alan Coxc65c9bc2009-06-11 12:50:12 +0100728 tty_ldisc_close(tty, o_ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100729
730 /* Now set up the new line discipline. */
Alan Coxc65c9bc2009-06-11 12:50:12 +0100731 tty_ldisc_assign(tty, new_ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100732 tty_set_termios_ldisc(tty, ldisc);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100733
734 retval = tty_ldisc_open(tty, new_ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100735 if (retval < 0) {
Alan Coxc65c9bc2009-06-11 12:50:12 +0100736 /* Back to the old one or N_TTY if we can't */
737 tty_ldisc_put(new_ldisc);
738 tty_ldisc_restore(tty, o_ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100739 }
Alan Coxc65c9bc2009-06-11 12:50:12 +0100740
Alan Cox01e1abb2008-07-22 11:16:55 +0100741 /* At this point we hold a reference to the new ldisc and a
742 a reference to the old ldisc. If we ended up flipping back
743 to the existing ldisc we have two references to it */
744
Alan Coxc65c9bc2009-06-11 12:50:12 +0100745 if (tty->ldisc->ops->num != o_ldisc->ops->num && tty->ops->set_ldisc)
Alan Cox01e1abb2008-07-22 11:16:55 +0100746 tty->ops->set_ldisc(tty);
747
Alan Coxc65c9bc2009-06-11 12:50:12 +0100748 tty_ldisc_put(o_ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100749
Jiri Slaby100eeae2010-10-31 23:17:51 +0100750enable:
Alan Cox01e1abb2008-07-22 11:16:55 +0100751 /*
Alan Coxc65c9bc2009-06-11 12:50:12 +0100752 * Allow ldisc referencing to occur again
Alan Cox01e1abb2008-07-22 11:16:55 +0100753 */
754
755 tty_ldisc_enable(tty);
756 if (o_tty)
757 tty_ldisc_enable(o_tty);
758
Alan Coxc65c9bc2009-06-11 12:50:12 +0100759 /* Restart the work queue in case no characters kick it off. Safe if
Alan Cox01e1abb2008-07-22 11:16:55 +0100760 already running */
Peter Hurley4f98d462013-03-11 16:44:34 -0400761 schedule_work(&tty->port->buf.work);
762 if (o_tty)
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200763 schedule_work(&o_tty->port->buf.work);
Peter Hurley4f98d462013-03-11 16:44:34 -0400764
Alan Coxc65c9bc2009-06-11 12:50:12 +0100765 mutex_unlock(&tty->ldisc_mutex);
Alan Cox89c8d912012-08-08 16:30:13 +0100766 tty_unlock(tty);
Alan Cox01e1abb2008-07-22 11:16:55 +0100767 return retval;
768}
769
Alan Coxc65c9bc2009-06-11 12:50:12 +0100770/**
771 * tty_reset_termios - reset terminal state
772 * @tty: tty to reset
773 *
774 * Restore a terminal to the driver default state.
775 */
776
777static void tty_reset_termios(struct tty_struct *tty)
778{
779 mutex_lock(&tty->termios_mutex);
Alan Coxadc8d742012-07-14 15:31:47 +0100780 tty->termios = tty->driver->init_termios;
781 tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios);
782 tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100783 mutex_unlock(&tty->termios_mutex);
784}
785
786
787/**
788 * tty_ldisc_reinit - reinitialise the tty ldisc
789 * @tty: tty to reinit
Alan Cox638b9642010-02-08 10:09:26 +0000790 * @ldisc: line discipline to reinitialize
Alan Coxc65c9bc2009-06-11 12:50:12 +0100791 *
Alan Cox638b9642010-02-08 10:09:26 +0000792 * Switch the tty to a line discipline and leave the ldisc
793 * state closed
Alan Coxc65c9bc2009-06-11 12:50:12 +0100794 */
795
Philippe Rétornaz1c95ba12010-10-27 17:13:21 +0200796static int tty_ldisc_reinit(struct tty_struct *tty, int ldisc)
Alan Coxc65c9bc2009-06-11 12:50:12 +0100797{
Philippe Rétornaz1c95ba12010-10-27 17:13:21 +0200798 struct tty_ldisc *ld = tty_ldisc_get(ldisc);
799
800 if (IS_ERR(ld))
801 return -1;
Alan Coxc65c9bc2009-06-11 12:50:12 +0100802
803 tty_ldisc_close(tty, tty->ldisc);
804 tty_ldisc_put(tty->ldisc);
805 tty->ldisc = NULL;
806 /*
807 * Switch the line discipline back
808 */
Alan Coxc65c9bc2009-06-11 12:50:12 +0100809 tty_ldisc_assign(tty, ld);
Alan Cox638b9642010-02-08 10:09:26 +0000810 tty_set_termios_ldisc(tty, ldisc);
Philippe Rétornaz1c95ba12010-10-27 17:13:21 +0200811
812 return 0;
Alan Coxc65c9bc2009-06-11 12:50:12 +0100813}
814
815/**
816 * tty_ldisc_hangup - hangup ldisc reset
817 * @tty: tty being hung up
818 *
819 * Some tty devices reset their termios when they receive a hangup
820 * event. In that situation we must also switch back to N_TTY properly
821 * before we reset the termios data.
822 *
823 * Locking: We can take the ldisc mutex as the rest of the code is
824 * careful to allow for this.
825 *
826 * In the pty pair case this occurs in the close() path of the
827 * tty itself so we must be careful about locking rules.
828 */
829
830void tty_ldisc_hangup(struct tty_struct *tty)
831{
832 struct tty_ldisc *ld;
Alan Cox638b9642010-02-08 10:09:26 +0000833 int reset = tty->driver->flags & TTY_DRIVER_RESET_TERMIOS;
834 int err = 0;
Alan Coxc65c9bc2009-06-11 12:50:12 +0100835
Peter Hurleyfc575ee2013-03-11 16:44:38 -0400836 tty_ldisc_debug(tty, "closing ldisc: %p\n", tty->ldisc);
837
Alan Coxc65c9bc2009-06-11 12:50:12 +0100838 /*
839 * FIXME! What are the locking issues here? This may me overdoing
840 * things... This question is especially important now that we've
841 * removed the irqlock.
842 */
843 ld = tty_ldisc_ref(tty);
844 if (ld != NULL) {
845 /* We may have no line discipline at this point */
846 if (ld->ops->flush_buffer)
847 ld->ops->flush_buffer(tty);
848 tty_driver_flush_buffer(tty);
849 if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
850 ld->ops->write_wakeup)
851 ld->ops->write_wakeup(tty);
852 if (ld->ops->hangup)
853 ld->ops->hangup(tty);
854 tty_ldisc_deref(ld);
855 }
856 /*
857 * FIXME: Once we trust the LDISC code better we can wait here for
858 * ldisc completion and fix the driver call race
859 */
860 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
861 wake_up_interruptible_poll(&tty->read_wait, POLLIN);
862 /*
863 * Shutdown the current line discipline, and reset it to
Alan Cox638b9642010-02-08 10:09:26 +0000864 * N_TTY if need be.
865 *
866 * Avoid racing set_ldisc or tty_ldisc_release
Alan Coxc65c9bc2009-06-11 12:50:12 +0100867 */
Alan Cox638b9642010-02-08 10:09:26 +0000868 mutex_lock(&tty->ldisc_mutex);
Arnd Bergmann60af22d2010-06-01 22:53:06 +0200869
Peter Hurley76bc35e2013-03-11 16:44:26 -0400870 if (tty_ldisc_hangup_halt(tty)) {
Peter Hurleyc8785242013-03-11 16:44:36 -0400871
872 /* At this point we have a halted ldisc; we want to close it and
873 reopen a new ldisc. We could defer the reopen to the next
874 open but it means auditing a lot of other paths so this is
875 a FIXME */
Alan Cox638b9642010-02-08 10:09:26 +0000876 if (reset == 0) {
Philippe Rétornaz1c95ba12010-10-27 17:13:21 +0200877
Alan Coxadc8d742012-07-14 15:31:47 +0100878 if (!tty_ldisc_reinit(tty, tty->termios.c_line))
Philippe Rétornaz1c95ba12010-10-27 17:13:21 +0200879 err = tty_ldisc_open(tty, tty->ldisc);
880 else
881 err = 1;
Alan Coxc8d50042009-07-16 16:05:08 +0100882 }
Alan Cox638b9642010-02-08 10:09:26 +0000883 /* If the re-open fails or we reset then go to N_TTY. The
884 N_TTY open cannot fail */
885 if (reset || err) {
Philippe Rétornaz1c95ba12010-10-27 17:13:21 +0200886 BUG_ON(tty_ldisc_reinit(tty, N_TTY));
Alan Cox638b9642010-02-08 10:09:26 +0000887 WARN_ON(tty_ldisc_open(tty, tty->ldisc));
888 }
889 tty_ldisc_enable(tty);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100890 }
Alan Cox638b9642010-02-08 10:09:26 +0000891 mutex_unlock(&tty->ldisc_mutex);
892 if (reset)
893 tty_reset_termios(tty);
Peter Hurleyfc575ee2013-03-11 16:44:38 -0400894
895 tty_ldisc_debug(tty, "re-opened ldisc: %p\n", tty->ldisc);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100896}
Alan Cox01e1abb2008-07-22 11:16:55 +0100897
898/**
899 * tty_ldisc_setup - open line discipline
900 * @tty: tty being shut down
901 * @o_tty: pair tty for pty/tty pairs
902 *
903 * Called during the initial open of a tty/pty pair in order to set up the
Alan Coxc65c9bc2009-06-11 12:50:12 +0100904 * line disciplines and bind them to the tty. This has no locking issues
905 * as the device isn't yet active.
Alan Cox01e1abb2008-07-22 11:16:55 +0100906 */
907
908int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty)
909{
Alan Coxc65c9bc2009-06-11 12:50:12 +0100910 struct tty_ldisc *ld = tty->ldisc;
Alan Cox01e1abb2008-07-22 11:16:55 +0100911 int retval;
912
Alan Coxc65c9bc2009-06-11 12:50:12 +0100913 retval = tty_ldisc_open(tty, ld);
914 if (retval)
915 return retval;
916
917 if (o_tty) {
918 retval = tty_ldisc_open(o_tty, o_tty->ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100919 if (retval) {
Alan Coxc65c9bc2009-06-11 12:50:12 +0100920 tty_ldisc_close(tty, ld);
Alan Cox01e1abb2008-07-22 11:16:55 +0100921 return retval;
922 }
923 tty_ldisc_enable(o_tty);
924 }
925 tty_ldisc_enable(tty);
926 return 0;
927}
Alan Cox89c8d912012-08-08 16:30:13 +0100928
929static void tty_ldisc_kill(struct tty_struct *tty)
930{
931 mutex_lock(&tty->ldisc_mutex);
932 /*
933 * Now kill off the ldisc
934 */
935 tty_ldisc_close(tty, tty->ldisc);
936 tty_ldisc_put(tty->ldisc);
937 /* Force an oops if we mess this up */
938 tty->ldisc = NULL;
939
940 /* Ensure the next open requests the N_TTY ldisc */
941 tty_set_termios_ldisc(tty, N_TTY);
942 mutex_unlock(&tty->ldisc_mutex);
943}
944
Alan Cox01e1abb2008-07-22 11:16:55 +0100945/**
946 * tty_ldisc_release - release line discipline
947 * @tty: tty being shut down
948 * @o_tty: pair tty for pty/tty pairs
949 *
Alan Cox852e99d2009-06-11 12:51:41 +0100950 * Called during the final close of a tty/pty pair in order to shut down
951 * the line discpline layer. On exit the ldisc assigned is N_TTY and the
Alan Coxc65c9bc2009-06-11 12:50:12 +0100952 * ldisc has not been opened.
Alan Cox01e1abb2008-07-22 11:16:55 +0100953 */
954
955void tty_ldisc_release(struct tty_struct *tty, struct tty_struct *o_tty)
956{
Alan Cox01e1abb2008-07-22 11:16:55 +0100957 /*
Peter Hurleya2965b72013-03-11 16:44:35 -0400958 * Shutdown this line discipline. As this is the final close,
959 * it does not race with the set_ldisc code path.
Alan Cox01e1abb2008-07-22 11:16:55 +0100960 */
Alan Cox01e1abb2008-07-22 11:16:55 +0100961
Peter Hurleyfc575ee2013-03-11 16:44:38 -0400962 tty_ldisc_debug(tty, "closing ldisc: %p\n", tty->ldisc);
963
Peter Hurley4f98d462013-03-11 16:44:34 -0400964 tty_ldisc_halt(tty, o_tty, MAX_SCHEDULE_TIMEOUT);
Sebastian Andrzej Siewior852e4a82012-12-25 23:02:48 +0100965
966 tty_lock_pair(tty, o_tty);
Alan Cox6d31a882012-07-14 15:31:27 +0100967 /* This will need doing differently if we need to lock */
Alan Cox89c8d912012-08-08 16:30:13 +0100968 tty_ldisc_kill(tty);
Alan Cox89c8d912012-08-08 16:30:13 +0100969 if (o_tty)
970 tty_ldisc_kill(o_tty);
971
972 tty_unlock_pair(tty, o_tty);
Alan Coxaef29bc2009-06-29 15:21:47 +0100973 /* And the memory resources remaining (buffers, termios) will be
974 disposed of when the kref hits zero */
Peter Hurleyfc575ee2013-03-11 16:44:38 -0400975
976 tty_ldisc_debug(tty, "ldisc closed\n");
Alan Cox01e1abb2008-07-22 11:16:55 +0100977}
978
979/**
980 * tty_ldisc_init - ldisc setup for new tty
981 * @tty: tty being allocated
982 *
983 * Set up the line discipline objects for a newly allocated tty. Note that
984 * the tty structure is not completely set up when this call is made.
985 */
986
987void tty_ldisc_init(struct tty_struct *tty)
988{
Alan Coxc65c9bc2009-06-11 12:50:12 +0100989 struct tty_ldisc *ld = tty_ldisc_get(N_TTY);
990 if (IS_ERR(ld))
Alan Cox01e1abb2008-07-22 11:16:55 +0100991 panic("n_tty: init_tty");
Alan Coxc65c9bc2009-06-11 12:50:12 +0100992 tty_ldisc_assign(tty, ld);
Alan Cox01e1abb2008-07-22 11:16:55 +0100993}
994
Jiri Slaby67166712011-03-23 10:48:35 +0100995/**
996 * tty_ldisc_init - ldisc cleanup for new tty
997 * @tty: tty that was allocated recently
998 *
999 * The tty structure must not becompletely set up (tty_ldisc_setup) when
1000 * this call is made.
1001 */
1002void tty_ldisc_deinit(struct tty_struct *tty)
1003{
1004 put_ldisc(tty->ldisc);
1005 tty_ldisc_assign(tty, NULL);
1006}
1007
Alan Cox01e1abb2008-07-22 11:16:55 +01001008void tty_ldisc_begin(void)
1009{
1010 /* Setup the default TTY line discipline. */
1011 (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
1012}