blob: 5ee0b2be7d729fa2becbc6550ae3a62a2e90ec77 [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/**
231 * tty_ldisc_assign - set ldisc on a tty
232 * @tty: tty to assign
233 * @ld: line discipline
234 *
235 * Install an instance of a line discipline into a tty structure. The
Alan Cox8d2ead72009-06-16 17:00:26 +0100236 * ldisc must have a reference count above zero to ensure it remains.
Alan Cox01e1abb2008-07-22 11:16:55 +0100237 * The tty instance refcount starts at zero.
238 *
239 * Locking:
240 * Caller must hold references
241 */
242
243static void tty_ldisc_assign(struct tty_struct *tty, struct tty_ldisc *ld)
244{
Alan Coxc65c9bc2009-06-11 12:50:12 +0100245 tty->ldisc = ld;
Alan Cox01e1abb2008-07-22 11:16:55 +0100246}
247
248/**
249 * tty_ldisc_try - internal helper
250 * @tty: the tty
251 *
252 * Make a single attempt to grab and bump the refcount on
253 * the tty ldisc. Return 0 on failure or 1 on success. This is
254 * used to implement both the waiting and non waiting versions
255 * of tty_ldisc_ref
256 *
257 * Locking: takes tty_ldisc_lock
258 */
259
Linus Torvalds65b77042009-08-03 11:11:19 -0700260static struct tty_ldisc *tty_ldisc_try(struct tty_struct *tty)
Alan Cox01e1abb2008-07-22 11:16:55 +0100261{
262 unsigned long flags;
263 struct tty_ldisc *ld;
Alan Cox01e1abb2008-07-22 11:16:55 +0100264
Peter Hurley16759f62013-03-11 16:44:41 -0400265 /* FIXME: this allows reference acquire after TTY_LDISC is cleared */
Ivo Siebenc9739942012-10-17 14:03:14 +0200266 raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
Linus Torvalds65b77042009-08-03 11:11:19 -0700267 ld = NULL;
Peter Hurley16759f62013-03-11 16:44:41 -0400268 if (test_bit(TTY_LDISC, &tty->flags) && tty->ldisc) {
269 ld = tty->ldisc;
270 atomic_inc(&ld->users);
271 }
Ivo Siebenc9739942012-10-17 14:03:14 +0200272 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
Linus Torvalds65b77042009-08-03 11:11:19 -0700273 return ld;
Alan Cox01e1abb2008-07-22 11:16:55 +0100274}
275
276/**
277 * tty_ldisc_ref_wait - wait for the tty ldisc
278 * @tty: tty device
279 *
280 * Dereference the line discipline for the terminal and take a
281 * reference to it. If the line discipline is in flux then
282 * wait patiently until it changes.
283 *
284 * Note: Must not be called from an IRQ/timer context. The caller
285 * must also be careful not to hold other locks that will deadlock
286 * against a discipline change, such as an existing ldisc reference
287 * (which we check for)
288 *
289 * Locking: call functions take tty_ldisc_lock
290 */
291
292struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
293{
Linus Torvalds65b77042009-08-03 11:11:19 -0700294 struct tty_ldisc *ld;
295
Alan Cox01e1abb2008-07-22 11:16:55 +0100296 /* wait_event is a macro */
Linus Torvalds65b77042009-08-03 11:11:19 -0700297 wait_event(tty_ldisc_wait, (ld = tty_ldisc_try(tty)) != NULL);
298 return ld;
Alan Cox01e1abb2008-07-22 11:16:55 +0100299}
Alan Cox01e1abb2008-07-22 11:16:55 +0100300EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
301
302/**
303 * tty_ldisc_ref - get the tty ldisc
304 * @tty: tty device
305 *
306 * Dereference the line discipline for the terminal and take a
307 * reference to it. If the line discipline is in flux then
308 * return NULL. Can be called from IRQ and timer functions.
309 *
310 * Locking: called functions take tty_ldisc_lock
311 */
312
313struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
314{
Linus Torvalds65b77042009-08-03 11:11:19 -0700315 return tty_ldisc_try(tty);
Alan Cox01e1abb2008-07-22 11:16:55 +0100316}
Alan Cox01e1abb2008-07-22 11:16:55 +0100317EXPORT_SYMBOL_GPL(tty_ldisc_ref);
318
319/**
320 * tty_ldisc_deref - free a tty ldisc reference
321 * @ld: reference to free up
322 *
323 * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May
324 * be called in IRQ context.
325 *
326 * Locking: takes tty_ldisc_lock
327 */
328
329void tty_ldisc_deref(struct tty_ldisc *ld)
330{
Peter Hurleyebc9bae2013-03-11 16:44:40 -0400331 unsigned long flags;
332
333 if (WARN_ON_ONCE(!ld))
334 return;
335
336 raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
337 /*
338 * WARNs if one-too-many reader references were released
339 * - the last reference must be released with tty_ldisc_put
340 */
341 WARN_ON(atomic_dec_and_test(&ld->users));
342 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
343
344 if (waitqueue_active(&ld->wq_idle))
345 wake_up(&ld->wq_idle);
Alan Cox01e1abb2008-07-22 11:16:55 +0100346}
Alan Cox01e1abb2008-07-22 11:16:55 +0100347EXPORT_SYMBOL_GPL(tty_ldisc_deref);
348
Peter Hurleyebc9bae2013-03-11 16:44:40 -0400349/**
350 * tty_ldisc_put - release the ldisc
351 *
352 * Complement of tty_ldisc_get().
353 */
Linus Torvalds65b77042009-08-03 11:11:19 -0700354static inline void tty_ldisc_put(struct tty_ldisc *ld)
355{
Peter Hurleyebc9bae2013-03-11 16:44:40 -0400356 unsigned long flags;
357
358 if (WARN_ON_ONCE(!ld))
359 return;
360
361 raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
362
363 /* unreleased reader reference(s) will cause this WARN */
364 WARN_ON(!atomic_dec_and_test(&ld->users));
365
366 ld->ops->refcount--;
367 module_put(ld->ops->owner);
368 kfree(ld);
369 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
Linus Torvalds65b77042009-08-03 11:11:19 -0700370}
371
Alan Cox01e1abb2008-07-22 11:16:55 +0100372/**
373 * tty_ldisc_enable - allow ldisc use
374 * @tty: terminal to activate ldisc on
375 *
376 * Set the TTY_LDISC flag when the line discipline can be called
Alan Coxc9b39762009-01-02 13:44:56 +0000377 * again. Do necessary wakeups for existing sleepers. Clear the LDISC
378 * changing flag to indicate any ldisc change is now over.
Alan Cox01e1abb2008-07-22 11:16:55 +0100379 *
Alan Coxc9b39762009-01-02 13:44:56 +0000380 * Note: nobody should set the TTY_LDISC bit except via this function.
381 * Clearing directly is allowed.
Alan Cox01e1abb2008-07-22 11:16:55 +0100382 */
383
Peter Hurleyd9121562013-03-11 16:44:33 -0400384static void tty_ldisc_enable(struct tty_struct *tty)
Alan Cox01e1abb2008-07-22 11:16:55 +0100385{
Peter Hurley21622932013-03-11 16:44:21 -0400386 clear_bit(TTY_LDISC_HALTED, &tty->flags);
Alan Cox01e1abb2008-07-22 11:16:55 +0100387 set_bit(TTY_LDISC, &tty->flags);
Alan Coxc9b39762009-01-02 13:44:56 +0000388 clear_bit(TTY_LDISC_CHANGING, &tty->flags);
Alan Cox01e1abb2008-07-22 11:16:55 +0100389 wake_up(&tty_ldisc_wait);
390}
391
392/**
Alan Coxf2c4c652009-06-11 12:50:58 +0100393 * tty_ldisc_flush - flush line discipline queue
394 * @tty: tty
395 *
396 * Flush the line discipline queue (if any) for this tty. If there
397 * is no line discipline active this is a no-op.
398 */
399
400void tty_ldisc_flush(struct tty_struct *tty)
401{
402 struct tty_ldisc *ld = tty_ldisc_ref(tty);
403 if (ld) {
404 if (ld->ops->flush_buffer)
405 ld->ops->flush_buffer(tty);
406 tty_ldisc_deref(ld);
407 }
408 tty_buffer_flush(tty);
409}
Alan Coxf2c4c652009-06-11 12:50:58 +0100410EXPORT_SYMBOL_GPL(tty_ldisc_flush);
411
412/**
Alan Cox01e1abb2008-07-22 11:16:55 +0100413 * tty_set_termios_ldisc - set ldisc field
414 * @tty: tty structure
415 * @num: line discipline number
416 *
417 * This is probably overkill for real world processors but
418 * they are not on hot paths so a little discipline won't do
419 * any harm.
420 *
421 * Locking: takes termios_mutex
422 */
423
424static void tty_set_termios_ldisc(struct tty_struct *tty, int num)
425{
426 mutex_lock(&tty->termios_mutex);
Alan Coxadc8d742012-07-14 15:31:47 +0100427 tty->termios.c_line = num;
Alan Cox01e1abb2008-07-22 11:16:55 +0100428 mutex_unlock(&tty->termios_mutex);
429}
430
Alan Coxc65c9bc2009-06-11 12:50:12 +0100431/**
432 * tty_ldisc_open - open a line discipline
433 * @tty: tty we are opening the ldisc on
434 * @ld: discipline to open
435 *
436 * A helper opening method. Also a convenient debugging and check
437 * point.
Arnd Bergmannec79d602010-06-01 22:53:01 +0200438 *
439 * Locking: always called with BTM already held.
Alan Coxc65c9bc2009-06-11 12:50:12 +0100440 */
441
442static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld)
443{
444 WARN_ON(test_and_set_bit(TTY_LDISC_OPEN, &tty->flags));
Alan Coxf18f9492009-11-30 13:18:35 +0000445 if (ld->ops->open) {
446 int ret;
Arnd Bergmannec79d602010-06-01 22:53:01 +0200447 /* BTM here locks versus a hangup event */
Alan Coxf18f9492009-11-30 13:18:35 +0000448 ret = ld->ops->open(tty);
Jiri Slaby7f90cfc2010-11-25 00:27:54 +0100449 if (ret)
450 clear_bit(TTY_LDISC_OPEN, &tty->flags);
Alan Coxf18f9492009-11-30 13:18:35 +0000451 return ret;
452 }
Alan Coxc65c9bc2009-06-11 12:50:12 +0100453 return 0;
454}
455
456/**
457 * tty_ldisc_close - close a line discipline
458 * @tty: tty we are opening the ldisc on
459 * @ld: discipline to close
460 *
461 * A helper close method. Also a convenient debugging and check
462 * point.
463 */
464
465static void tty_ldisc_close(struct tty_struct *tty, struct tty_ldisc *ld)
466{
467 WARN_ON(!test_bit(TTY_LDISC_OPEN, &tty->flags));
468 clear_bit(TTY_LDISC_OPEN, &tty->flags);
469 if (ld->ops->close)
470 ld->ops->close(tty);
471}
Alan Cox01e1abb2008-07-22 11:16:55 +0100472
473/**
474 * tty_ldisc_restore - helper for tty ldisc change
475 * @tty: tty to recover
476 * @old: previous ldisc
477 *
478 * Restore the previous line discipline or N_TTY when a line discipline
479 * change fails due to an open error
480 */
481
482static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old)
483{
484 char buf[64];
Alan Coxc65c9bc2009-06-11 12:50:12 +0100485 struct tty_ldisc *new_ldisc;
486 int r;
Alan Cox01e1abb2008-07-22 11:16:55 +0100487
488 /* There is an outstanding reference here so this is safe */
Alan Coxc65c9bc2009-06-11 12:50:12 +0100489 old = tty_ldisc_get(old->ops->num);
490 WARN_ON(IS_ERR(old));
Alan Cox01e1abb2008-07-22 11:16:55 +0100491 tty_ldisc_assign(tty, old);
492 tty_set_termios_ldisc(tty, old->ops->num);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100493 if (tty_ldisc_open(tty, old) < 0) {
494 tty_ldisc_put(old);
Alan Cox01e1abb2008-07-22 11:16:55 +0100495 /* This driver is always present */
Alan Cox852e99d2009-06-11 12:51:41 +0100496 new_ldisc = tty_ldisc_get(N_TTY);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100497 if (IS_ERR(new_ldisc))
Alan Cox01e1abb2008-07-22 11:16:55 +0100498 panic("n_tty: get");
Alan Coxc65c9bc2009-06-11 12:50:12 +0100499 tty_ldisc_assign(tty, new_ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100500 tty_set_termios_ldisc(tty, N_TTY);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100501 r = tty_ldisc_open(tty, new_ldisc);
502 if (r < 0)
503 panic("Couldn't open N_TTY ldisc for "
504 "%s --- error %d.",
505 tty_name(tty, buf), r);
Alan Cox01e1abb2008-07-22 11:16:55 +0100506 }
507}
508
509/**
Jiri Slaby100eeae2010-10-31 23:17:51 +0100510 * tty_ldisc_wait_idle - wait for the ldisc to become idle
511 * @tty: tty to wait for
Jiri Slabydf92d052011-11-16 16:27:07 +0100512 * @timeout: for how long to wait at most
Jiri Slaby100eeae2010-10-31 23:17:51 +0100513 *
514 * Wait for the line discipline to become idle. The discipline must
515 * have been halted for this to guarantee it remains idle.
516 */
Jiri Slabydf92d052011-11-16 16:27:07 +0100517static int tty_ldisc_wait_idle(struct tty_struct *tty, long timeout)
Jiri Slaby100eeae2010-10-31 23:17:51 +0100518{
Jiri Slabydf92d052011-11-16 16:27:07 +0100519 long ret;
Ivo Sieben1541f842012-05-03 14:37:43 +0200520 ret = wait_event_timeout(tty->ldisc->wq_idle,
Jiri Slabydf92d052011-11-16 16:27:07 +0100521 atomic_read(&tty->ldisc->users) == 1, timeout);
Jiri Slaby100eeae2010-10-31 23:17:51 +0100522 return ret > 0 ? 0 : -EBUSY;
523}
524
525/**
Peter Hurley11cf48e2013-03-11 16:44:27 -0400526 * tty_ldisc_halt - shut down the line discipline
527 * @tty: tty device
Peter Hurleyf4cf7a32013-03-11 16:44:29 -0400528 * @o_tty: paired pty device (can be NULL)
Peter Hurleycf528472013-03-11 16:44:28 -0400529 * @timeout: # of jiffies to wait for ldisc refs to be released
Peter Hurley11cf48e2013-03-11 16:44:27 -0400530 *
Peter Hurleyf4cf7a32013-03-11 16:44:29 -0400531 * Shut down the line discipline and work queue for this tty device and
532 * its paired pty (if exists). Clearing the TTY_LDISC flag ensures
Peter Hurley4f98d462013-03-11 16:44:34 -0400533 * no further references can be obtained, while waiting for existing
534 * references to be released ensures no more data is fed to the ldisc.
Peter Hurleycf528472013-03-11 16:44:28 -0400535 *
Peter Hurley11cf48e2013-03-11 16:44:27 -0400536 * You need to do a 'flush_scheduled_work()' (outside the ldisc_mutex)
537 * in order to make sure any currently executing ldisc work is also
538 * flushed.
539 */
540
Peter Hurleyf4cf7a32013-03-11 16:44:29 -0400541static int tty_ldisc_halt(struct tty_struct *tty, struct tty_struct *o_tty,
Peter Hurley4f98d462013-03-11 16:44:34 -0400542 long timeout)
Peter Hurley11cf48e2013-03-11 16:44:27 -0400543{
Peter Hurley4f98d462013-03-11 16:44:34 -0400544 int retval;
Peter Hurleycf528472013-03-11 16:44:28 -0400545
Peter Hurley11cf48e2013-03-11 16:44:27 -0400546 clear_bit(TTY_LDISC, &tty->flags);
Peter Hurleyf4cf7a32013-03-11 16:44:29 -0400547 if (o_tty)
548 clear_bit(TTY_LDISC, &o_tty->flags);
549
Peter Hurleycf528472013-03-11 16:44:28 -0400550 retval = tty_ldisc_wait_idle(tty, timeout);
Peter Hurleyf4cf7a32013-03-11 16:44:29 -0400551 if (!retval && o_tty)
552 retval = tty_ldisc_wait_idle(o_tty, timeout);
Peter Hurleycf528472013-03-11 16:44:28 -0400553 if (retval)
554 return retval;
555
Peter Hurley11cf48e2013-03-11 16:44:27 -0400556 set_bit(TTY_LDISC_HALTED, &tty->flags);
Peter Hurley4f98d462013-03-11 16:44:34 -0400557 if (o_tty)
Peter Hurleyf4cf7a32013-03-11 16:44:29 -0400558 set_bit(TTY_LDISC_HALTED, &o_tty->flags);
Peter Hurleyf4cf7a32013-03-11 16:44:29 -0400559
Peter Hurleycf528472013-03-11 16:44:28 -0400560 return 0;
Peter Hurley11cf48e2013-03-11 16:44:27 -0400561}
562
563/**
Peter Hurley76bc35e2013-03-11 16:44:26 -0400564 * tty_ldisc_hangup_halt - halt the line discipline for hangup
565 * @tty: tty being hung up
Peter Hurley168942c2013-03-11 16:44:24 -0400566 *
Peter Hurley76bc35e2013-03-11 16:44:26 -0400567 * Shut down the line discipline and work queue for the tty device
568 * being hungup. Clear the TTY_LDISC flag to ensure no further
Peter Hurley4f98d462013-03-11 16:44:34 -0400569 * references can be obtained and wait for remaining references to be
570 * released to ensure no more data is fed to this ldisc.
Peter Hurley168942c2013-03-11 16:44:24 -0400571 * Caller must hold legacy and ->ldisc_mutex.
Peter Hurley2276ad92013-03-11 16:44:25 -0400572 *
573 * NB: tty_set_ldisc() is prevented from changing the ldisc concurrently
574 * with this function by checking the TTY_HUPPING flag.
Peter Hurley168942c2013-03-11 16:44:24 -0400575 */
Peter Hurley76bc35e2013-03-11 16:44:26 -0400576static bool tty_ldisc_hangup_halt(struct tty_struct *tty)
Peter Hurley168942c2013-03-11 16:44:24 -0400577{
Peter Hurley2276ad92013-03-11 16:44:25 -0400578 char cur_n[TASK_COMM_LEN], tty_n[64];
579 long timeout = 3 * HZ;
Peter Hurley168942c2013-03-11 16:44:24 -0400580
Peter Hurley76bc35e2013-03-11 16:44:26 -0400581 clear_bit(TTY_LDISC, &tty->flags);
582
Peter Hurley2276ad92013-03-11 16:44:25 -0400583 if (tty->ldisc) { /* Not yet closed */
584 tty_unlock(tty);
585
586 while (tty_ldisc_wait_idle(tty, timeout) == -EBUSY) {
587 timeout = MAX_SCHEDULE_TIMEOUT;
588 printk_ratelimited(KERN_WARNING
589 "%s: waiting (%s) for %s took too long, but we keep waiting...\n",
590 __func__, get_task_comm(cur_n, current),
591 tty_name(tty, tty_n));
Peter Hurley168942c2013-03-11 16:44:24 -0400592 }
Peter Hurley76bc35e2013-03-11 16:44:26 -0400593
Peter Hurley76bc35e2013-03-11 16:44:26 -0400594 set_bit(TTY_LDISC_HALTED, &tty->flags);
595
Peter Hurley2276ad92013-03-11 16:44:25 -0400596 /* must reacquire both locks and preserve lock order */
597 mutex_unlock(&tty->ldisc_mutex);
598 tty_lock(tty);
599 mutex_lock(&tty->ldisc_mutex);
Peter Hurley168942c2013-03-11 16:44:24 -0400600 }
601 return !!tty->ldisc;
602}
603
604/**
Alan Cox01e1abb2008-07-22 11:16:55 +0100605 * tty_set_ldisc - set line discipline
606 * @tty: the terminal to set
607 * @ldisc: the line discipline
608 *
609 * Set the discipline of a tty line. Must be called from a process
Alan Coxc65c9bc2009-06-11 12:50:12 +0100610 * context. The ldisc change logic has to protect itself against any
611 * overlapping ldisc change (including on the other end of pty pairs),
612 * the close of one side of a tty/pty pair, and eventually hangup.
Alan Cox01e1abb2008-07-22 11:16:55 +0100613 *
Alan Coxc65c9bc2009-06-11 12:50:12 +0100614 * Locking: takes tty_ldisc_lock, termios_mutex
Alan Cox01e1abb2008-07-22 11:16:55 +0100615 */
616
617int tty_set_ldisc(struct tty_struct *tty, int ldisc)
618{
619 int retval;
Alan Coxc65c9bc2009-06-11 12:50:12 +0100620 struct tty_ldisc *o_ldisc, *new_ldisc;
Alan Cox01e1abb2008-07-22 11:16:55 +0100621 struct tty_struct *o_tty;
622
Alan Coxc65c9bc2009-06-11 12:50:12 +0100623 new_ldisc = tty_ldisc_get(ldisc);
624 if (IS_ERR(new_ldisc))
625 return PTR_ERR(new_ldisc);
626
Alan Cox89c8d912012-08-08 16:30:13 +0100627 tty_lock(tty);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100628 /*
629 * We need to look at the tty locking here for pty/tty pairs
630 * when both sides try to change in parallel.
631 */
632
633 o_tty = tty->link; /* o_tty is the pty side or NULL */
634
635
636 /*
637 * Check the no-op case
638 */
639
640 if (tty->ldisc->ops->num == ldisc) {
Alan Cox89c8d912012-08-08 16:30:13 +0100641 tty_unlock(tty);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100642 tty_ldisc_put(new_ldisc);
643 return 0;
644 }
Alan Cox01e1abb2008-07-22 11:16:55 +0100645
Alan Cox89c8d912012-08-08 16:30:13 +0100646 tty_unlock(tty);
Alan Cox01e1abb2008-07-22 11:16:55 +0100647 /*
648 * Problem: What do we do if this blocks ?
Alan Coxc65c9bc2009-06-11 12:50:12 +0100649 * We could deadlock here
Alan Cox01e1abb2008-07-22 11:16:55 +0100650 */
651
652 tty_wait_until_sent(tty, 0);
653
Alan Cox89c8d912012-08-08 16:30:13 +0100654 tty_lock(tty);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100655 mutex_lock(&tty->ldisc_mutex);
Alan Cox01e1abb2008-07-22 11:16:55 +0100656
657 /*
Alan Coxc65c9bc2009-06-11 12:50:12 +0100658 * We could be midstream of another ldisc change which has
659 * dropped the lock during processing. If so we need to wait.
660 */
661
662 while (test_bit(TTY_LDISC_CHANGING, &tty->flags)) {
663 mutex_unlock(&tty->ldisc_mutex);
Alan Cox89c8d912012-08-08 16:30:13 +0100664 tty_unlock(tty);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100665 wait_event(tty_ldisc_wait,
666 test_bit(TTY_LDISC_CHANGING, &tty->flags) == 0);
Alan Cox89c8d912012-08-08 16:30:13 +0100667 tty_lock(tty);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100668 mutex_lock(&tty->ldisc_mutex);
669 }
Alan Coxeeb89d92009-11-30 13:18:29 +0000670
Alan Coxc65c9bc2009-06-11 12:50:12 +0100671 set_bit(TTY_LDISC_CHANGING, &tty->flags);
Alan Cox852e99d2009-06-11 12:51:41 +0100672
Alan Coxc65c9bc2009-06-11 12:50:12 +0100673 /*
Alan Cox01e1abb2008-07-22 11:16:55 +0100674 * No more input please, we are switching. The new ldisc
675 * will update this value in the ldisc open function
676 */
677
678 tty->receive_room = 0;
679
680 o_ldisc = tty->ldisc;
Alan Coxeeb89d92009-11-30 13:18:29 +0000681
Alan Cox89c8d912012-08-08 16:30:13 +0100682 tty_unlock(tty);
Alan Cox01e1abb2008-07-22 11:16:55 +0100683 /*
684 * Make sure we don't change while someone holds a
685 * reference to the line discipline. The TTY_LDISC bit
686 * prevents anyone taking a reference once it is clear.
687 * We need the lock to avoid racing reference takers.
Alan Coxc9b39762009-01-02 13:44:56 +0000688 *
689 * We must clear the TTY_LDISC bit here to avoid a livelock
690 * with a userspace app continually trying to use the tty in
691 * parallel to the change and re-referencing the tty.
Alan Cox01e1abb2008-07-22 11:16:55 +0100692 */
693
Peter Hurley4f98d462013-03-11 16:44:34 -0400694 retval = tty_ldisc_halt(tty, o_tty, 5 * HZ);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100695
Alan Cox01e1abb2008-07-22 11:16:55 +0100696 /*
Peter Hurleya2965b72013-03-11 16:44:35 -0400697 * Wait for hangup to complete, if pending.
Alan Coxc65c9bc2009-06-11 12:50:12 +0100698 * We must drop the mutex here in case a hangup is also in process.
Alan Cox01e1abb2008-07-22 11:16:55 +0100699 */
700
Alan Coxc65c9bc2009-06-11 12:50:12 +0100701 mutex_unlock(&tty->ldisc_mutex);
702
Peter Hurleya2965b72013-03-11 16:44:35 -0400703 flush_work(&tty->hangup_work);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100704
Alan Cox89c8d912012-08-08 16:30:13 +0100705 tty_lock(tty);
Arnd Bergmann60af22d2010-06-01 22:53:06 +0200706 mutex_lock(&tty->ldisc_mutex);
Jiri Slaby100eeae2010-10-31 23:17:51 +0100707
708 /* handle wait idle failure locked */
709 if (retval) {
710 tty_ldisc_put(new_ldisc);
711 goto enable;
712 }
713
Shachar Shemesh40c9f612012-07-10 07:54:13 +0300714 if (test_bit(TTY_HUPPING, &tty->flags)) {
Alan Coxc65c9bc2009-06-11 12:50:12 +0100715 /* We were raced by the hangup method. It will have stomped
716 the ldisc data and closed the ldisc down */
717 clear_bit(TTY_LDISC_CHANGING, &tty->flags);
718 mutex_unlock(&tty->ldisc_mutex);
719 tty_ldisc_put(new_ldisc);
Alan Cox89c8d912012-08-08 16:30:13 +0100720 tty_unlock(tty);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100721 return -EIO;
722 }
723
Alan Cox01e1abb2008-07-22 11:16:55 +0100724 /* Shutdown the current discipline. */
Alan Coxc65c9bc2009-06-11 12:50:12 +0100725 tty_ldisc_close(tty, o_ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100726
727 /* Now set up the new line discipline. */
Alan Coxc65c9bc2009-06-11 12:50:12 +0100728 tty_ldisc_assign(tty, new_ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100729 tty_set_termios_ldisc(tty, ldisc);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100730
731 retval = tty_ldisc_open(tty, new_ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100732 if (retval < 0) {
Alan Coxc65c9bc2009-06-11 12:50:12 +0100733 /* Back to the old one or N_TTY if we can't */
734 tty_ldisc_put(new_ldisc);
735 tty_ldisc_restore(tty, o_ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100736 }
Alan Coxc65c9bc2009-06-11 12:50:12 +0100737
Alan Cox01e1abb2008-07-22 11:16:55 +0100738 /* At this point we hold a reference to the new ldisc and a
739 a reference to the old ldisc. If we ended up flipping back
740 to the existing ldisc we have two references to it */
741
Alan Coxc65c9bc2009-06-11 12:50:12 +0100742 if (tty->ldisc->ops->num != o_ldisc->ops->num && tty->ops->set_ldisc)
Alan Cox01e1abb2008-07-22 11:16:55 +0100743 tty->ops->set_ldisc(tty);
744
Alan Coxc65c9bc2009-06-11 12:50:12 +0100745 tty_ldisc_put(o_ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100746
Jiri Slaby100eeae2010-10-31 23:17:51 +0100747enable:
Alan Cox01e1abb2008-07-22 11:16:55 +0100748 /*
Alan Coxc65c9bc2009-06-11 12:50:12 +0100749 * Allow ldisc referencing to occur again
Alan Cox01e1abb2008-07-22 11:16:55 +0100750 */
751
752 tty_ldisc_enable(tty);
753 if (o_tty)
754 tty_ldisc_enable(o_tty);
755
Alan Coxc65c9bc2009-06-11 12:50:12 +0100756 /* Restart the work queue in case no characters kick it off. Safe if
Alan Cox01e1abb2008-07-22 11:16:55 +0100757 already running */
Peter Hurley4f98d462013-03-11 16:44:34 -0400758 schedule_work(&tty->port->buf.work);
759 if (o_tty)
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200760 schedule_work(&o_tty->port->buf.work);
Peter Hurley4f98d462013-03-11 16:44:34 -0400761
Alan Coxc65c9bc2009-06-11 12:50:12 +0100762 mutex_unlock(&tty->ldisc_mutex);
Alan Cox89c8d912012-08-08 16:30:13 +0100763 tty_unlock(tty);
Alan Cox01e1abb2008-07-22 11:16:55 +0100764 return retval;
765}
766
Alan Coxc65c9bc2009-06-11 12:50:12 +0100767/**
768 * tty_reset_termios - reset terminal state
769 * @tty: tty to reset
770 *
771 * Restore a terminal to the driver default state.
772 */
773
774static void tty_reset_termios(struct tty_struct *tty)
775{
776 mutex_lock(&tty->termios_mutex);
Alan Coxadc8d742012-07-14 15:31:47 +0100777 tty->termios = tty->driver->init_termios;
778 tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios);
779 tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100780 mutex_unlock(&tty->termios_mutex);
781}
782
783
784/**
785 * tty_ldisc_reinit - reinitialise the tty ldisc
786 * @tty: tty to reinit
Alan Cox638b9642010-02-08 10:09:26 +0000787 * @ldisc: line discipline to reinitialize
Alan Coxc65c9bc2009-06-11 12:50:12 +0100788 *
Alan Cox638b9642010-02-08 10:09:26 +0000789 * Switch the tty to a line discipline and leave the ldisc
790 * state closed
Alan Coxc65c9bc2009-06-11 12:50:12 +0100791 */
792
Philippe Rétornaz1c95ba12010-10-27 17:13:21 +0200793static int tty_ldisc_reinit(struct tty_struct *tty, int ldisc)
Alan Coxc65c9bc2009-06-11 12:50:12 +0100794{
Philippe Rétornaz1c95ba12010-10-27 17:13:21 +0200795 struct tty_ldisc *ld = tty_ldisc_get(ldisc);
796
797 if (IS_ERR(ld))
798 return -1;
Alan Coxc65c9bc2009-06-11 12:50:12 +0100799
800 tty_ldisc_close(tty, tty->ldisc);
801 tty_ldisc_put(tty->ldisc);
802 tty->ldisc = NULL;
803 /*
804 * Switch the line discipline back
805 */
Alan Coxc65c9bc2009-06-11 12:50:12 +0100806 tty_ldisc_assign(tty, ld);
Alan Cox638b9642010-02-08 10:09:26 +0000807 tty_set_termios_ldisc(tty, ldisc);
Philippe Rétornaz1c95ba12010-10-27 17:13:21 +0200808
809 return 0;
Alan Coxc65c9bc2009-06-11 12:50:12 +0100810}
811
812/**
813 * tty_ldisc_hangup - hangup ldisc reset
814 * @tty: tty being hung up
815 *
816 * Some tty devices reset their termios when they receive a hangup
817 * event. In that situation we must also switch back to N_TTY properly
818 * before we reset the termios data.
819 *
820 * Locking: We can take the ldisc mutex as the rest of the code is
821 * careful to allow for this.
822 *
823 * In the pty pair case this occurs in the close() path of the
824 * tty itself so we must be careful about locking rules.
825 */
826
827void tty_ldisc_hangup(struct tty_struct *tty)
828{
829 struct tty_ldisc *ld;
Alan Cox638b9642010-02-08 10:09:26 +0000830 int reset = tty->driver->flags & TTY_DRIVER_RESET_TERMIOS;
831 int err = 0;
Alan Coxc65c9bc2009-06-11 12:50:12 +0100832
Peter Hurleyfc575ee2013-03-11 16:44:38 -0400833 tty_ldisc_debug(tty, "closing ldisc: %p\n", tty->ldisc);
834
Alan Coxc65c9bc2009-06-11 12:50:12 +0100835 /*
836 * FIXME! What are the locking issues here? This may me overdoing
837 * things... This question is especially important now that we've
838 * removed the irqlock.
839 */
840 ld = tty_ldisc_ref(tty);
841 if (ld != NULL) {
842 /* We may have no line discipline at this point */
843 if (ld->ops->flush_buffer)
844 ld->ops->flush_buffer(tty);
845 tty_driver_flush_buffer(tty);
846 if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
847 ld->ops->write_wakeup)
848 ld->ops->write_wakeup(tty);
849 if (ld->ops->hangup)
850 ld->ops->hangup(tty);
851 tty_ldisc_deref(ld);
852 }
853 /*
854 * FIXME: Once we trust the LDISC code better we can wait here for
855 * ldisc completion and fix the driver call race
856 */
857 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
858 wake_up_interruptible_poll(&tty->read_wait, POLLIN);
859 /*
860 * Shutdown the current line discipline, and reset it to
Alan Cox638b9642010-02-08 10:09:26 +0000861 * N_TTY if need be.
862 *
863 * Avoid racing set_ldisc or tty_ldisc_release
Alan Coxc65c9bc2009-06-11 12:50:12 +0100864 */
Alan Cox638b9642010-02-08 10:09:26 +0000865 mutex_lock(&tty->ldisc_mutex);
Arnd Bergmann60af22d2010-06-01 22:53:06 +0200866
Peter Hurley76bc35e2013-03-11 16:44:26 -0400867 if (tty_ldisc_hangup_halt(tty)) {
Peter Hurleyc8785242013-03-11 16:44:36 -0400868
869 /* At this point we have a halted ldisc; we want to close it and
870 reopen a new ldisc. We could defer the reopen to the next
871 open but it means auditing a lot of other paths so this is
872 a FIXME */
Alan Cox638b9642010-02-08 10:09:26 +0000873 if (reset == 0) {
Philippe Rétornaz1c95ba12010-10-27 17:13:21 +0200874
Alan Coxadc8d742012-07-14 15:31:47 +0100875 if (!tty_ldisc_reinit(tty, tty->termios.c_line))
Philippe Rétornaz1c95ba12010-10-27 17:13:21 +0200876 err = tty_ldisc_open(tty, tty->ldisc);
877 else
878 err = 1;
Alan Coxc8d50042009-07-16 16:05:08 +0100879 }
Alan Cox638b9642010-02-08 10:09:26 +0000880 /* If the re-open fails or we reset then go to N_TTY. The
881 N_TTY open cannot fail */
882 if (reset || err) {
Philippe Rétornaz1c95ba12010-10-27 17:13:21 +0200883 BUG_ON(tty_ldisc_reinit(tty, N_TTY));
Alan Cox638b9642010-02-08 10:09:26 +0000884 WARN_ON(tty_ldisc_open(tty, tty->ldisc));
885 }
886 tty_ldisc_enable(tty);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100887 }
Alan Cox638b9642010-02-08 10:09:26 +0000888 mutex_unlock(&tty->ldisc_mutex);
889 if (reset)
890 tty_reset_termios(tty);
Peter Hurleyfc575ee2013-03-11 16:44:38 -0400891
892 tty_ldisc_debug(tty, "re-opened ldisc: %p\n", tty->ldisc);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100893}
Alan Cox01e1abb2008-07-22 11:16:55 +0100894
895/**
896 * tty_ldisc_setup - open line discipline
897 * @tty: tty being shut down
898 * @o_tty: pair tty for pty/tty pairs
899 *
900 * Called during the initial open of a tty/pty pair in order to set up the
Alan Coxc65c9bc2009-06-11 12:50:12 +0100901 * line disciplines and bind them to the tty. This has no locking issues
902 * as the device isn't yet active.
Alan Cox01e1abb2008-07-22 11:16:55 +0100903 */
904
905int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty)
906{
Alan Coxc65c9bc2009-06-11 12:50:12 +0100907 struct tty_ldisc *ld = tty->ldisc;
Alan Cox01e1abb2008-07-22 11:16:55 +0100908 int retval;
909
Alan Coxc65c9bc2009-06-11 12:50:12 +0100910 retval = tty_ldisc_open(tty, ld);
911 if (retval)
912 return retval;
913
914 if (o_tty) {
915 retval = tty_ldisc_open(o_tty, o_tty->ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100916 if (retval) {
Alan Coxc65c9bc2009-06-11 12:50:12 +0100917 tty_ldisc_close(tty, ld);
Alan Cox01e1abb2008-07-22 11:16:55 +0100918 return retval;
919 }
920 tty_ldisc_enable(o_tty);
921 }
922 tty_ldisc_enable(tty);
923 return 0;
924}
Alan Cox89c8d912012-08-08 16:30:13 +0100925
926static void tty_ldisc_kill(struct tty_struct *tty)
927{
928 mutex_lock(&tty->ldisc_mutex);
929 /*
930 * Now kill off the ldisc
931 */
932 tty_ldisc_close(tty, tty->ldisc);
933 tty_ldisc_put(tty->ldisc);
934 /* Force an oops if we mess this up */
935 tty->ldisc = NULL;
936
937 /* Ensure the next open requests the N_TTY ldisc */
938 tty_set_termios_ldisc(tty, N_TTY);
939 mutex_unlock(&tty->ldisc_mutex);
940}
941
Alan Cox01e1abb2008-07-22 11:16:55 +0100942/**
943 * tty_ldisc_release - release line discipline
944 * @tty: tty being shut down
945 * @o_tty: pair tty for pty/tty pairs
946 *
Alan Cox852e99d2009-06-11 12:51:41 +0100947 * Called during the final close of a tty/pty pair in order to shut down
948 * the line discpline layer. On exit the ldisc assigned is N_TTY and the
Alan Coxc65c9bc2009-06-11 12:50:12 +0100949 * ldisc has not been opened.
Alan Cox01e1abb2008-07-22 11:16:55 +0100950 */
951
952void tty_ldisc_release(struct tty_struct *tty, struct tty_struct *o_tty)
953{
Alan Cox01e1abb2008-07-22 11:16:55 +0100954 /*
Peter Hurleya2965b72013-03-11 16:44:35 -0400955 * Shutdown this line discipline. As this is the final close,
956 * it does not race with the set_ldisc code path.
Alan Cox01e1abb2008-07-22 11:16:55 +0100957 */
Alan Cox01e1abb2008-07-22 11:16:55 +0100958
Peter Hurleyfc575ee2013-03-11 16:44:38 -0400959 tty_ldisc_debug(tty, "closing ldisc: %p\n", tty->ldisc);
960
Peter Hurley4f98d462013-03-11 16:44:34 -0400961 tty_ldisc_halt(tty, o_tty, MAX_SCHEDULE_TIMEOUT);
Sebastian Andrzej Siewior852e4a82012-12-25 23:02:48 +0100962
963 tty_lock_pair(tty, o_tty);
Alan Cox6d31a882012-07-14 15:31:27 +0100964 /* This will need doing differently if we need to lock */
Alan Cox89c8d912012-08-08 16:30:13 +0100965 tty_ldisc_kill(tty);
Alan Cox89c8d912012-08-08 16:30:13 +0100966 if (o_tty)
967 tty_ldisc_kill(o_tty);
968
969 tty_unlock_pair(tty, o_tty);
Alan Coxaef29bc2009-06-29 15:21:47 +0100970 /* And the memory resources remaining (buffers, termios) will be
971 disposed of when the kref hits zero */
Peter Hurleyfc575ee2013-03-11 16:44:38 -0400972
973 tty_ldisc_debug(tty, "ldisc closed\n");
Alan Cox01e1abb2008-07-22 11:16:55 +0100974}
975
976/**
977 * tty_ldisc_init - ldisc setup for new tty
978 * @tty: tty being allocated
979 *
980 * Set up the line discipline objects for a newly allocated tty. Note that
981 * the tty structure is not completely set up when this call is made.
982 */
983
984void tty_ldisc_init(struct tty_struct *tty)
985{
Alan Coxc65c9bc2009-06-11 12:50:12 +0100986 struct tty_ldisc *ld = tty_ldisc_get(N_TTY);
987 if (IS_ERR(ld))
Alan Cox01e1abb2008-07-22 11:16:55 +0100988 panic("n_tty: init_tty");
Alan Coxc65c9bc2009-06-11 12:50:12 +0100989 tty_ldisc_assign(tty, ld);
Alan Cox01e1abb2008-07-22 11:16:55 +0100990}
991
Jiri Slaby67166712011-03-23 10:48:35 +0100992/**
993 * tty_ldisc_init - ldisc cleanup for new tty
994 * @tty: tty that was allocated recently
995 *
996 * The tty structure must not becompletely set up (tty_ldisc_setup) when
997 * this call is made.
998 */
999void tty_ldisc_deinit(struct tty_struct *tty)
1000{
Peter Hurleyebc9bae2013-03-11 16:44:40 -04001001 tty_ldisc_put(tty->ldisc);
Jiri Slaby67166712011-03-23 10:48:35 +01001002 tty_ldisc_assign(tty, NULL);
1003}
1004
Alan Cox01e1abb2008-07-22 11:16:55 +01001005void tty_ldisc_begin(void)
1006{
1007 /* Setup the default TTY line discipline. */
1008 (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
1009}