blob: c07fb5d9bcf9b5429689a908417021fb5716e8f9 [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>
Alan Cox01e1abb2008-07-22 11:16:55 +010014#include <linux/module.h>
Alan Cox01e1abb2008-07-22 11:16:55 +010015#include <linux/device.h>
16#include <linux/wait.h>
17#include <linux/bitops.h>
Alan Cox01e1abb2008-07-22 11:16:55 +010018#include <linux/seq_file.h>
Alan Cox01e1abb2008-07-22 11:16:55 +010019#include <linux/uaccess.h>
Jiri Slaby0c73c082011-11-16 16:27:09 +010020#include <linux/ratelimit.h>
Alan Cox01e1abb2008-07-22 11:16:55 +010021
Peter Hurleyfc575ee2013-03-11 16:44:38 -040022#undef LDISC_DEBUG_HANGUP
23
24#ifdef LDISC_DEBUG_HANGUP
Rasmus Villemoes429b4742015-03-31 15:55:59 +020025#define tty_ldisc_debug(tty, f, args...) ({ \
26 printk(KERN_DEBUG "%s: %s: " f, __func__, tty_name(tty), ##args); \
Peter Hurleyfc575ee2013-03-11 16:44:38 -040027})
28#else
29#define tty_ldisc_debug(tty, f, args...)
30#endif
31
Peter Hurleyd2c43892013-06-15 07:04:47 -040032/* lockdep nested classes for tty->ldisc_sem */
33enum {
34 LDISC_SEM_NORMAL,
35 LDISC_SEM_OTHER,
36};
37
38
Alan Cox01e1abb2008-07-22 11:16:55 +010039/*
40 * This guards the refcounted line discipline lists. The lock
41 * must be taken with irqs off because there are hangup path
42 * callers who will do ldisc lookups and cannot sleep.
43 */
44
Peter Hurley137084b2013-06-15 07:04:46 -040045static DEFINE_RAW_SPINLOCK(tty_ldiscs_lock);
Alan Cox01e1abb2008-07-22 11:16:55 +010046/* Line disc dispatch table */
47static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS];
48
49/**
50 * tty_register_ldisc - install a line discipline
51 * @disc: ldisc number
52 * @new_ldisc: pointer to the ldisc object
53 *
54 * Installs a new line discipline into the kernel. The discipline
55 * is set up as unreferenced and then made available to the kernel
56 * from this point onwards.
57 *
58 * Locking:
Peter Hurley137084b2013-06-15 07:04:46 -040059 * takes tty_ldiscs_lock to guard against ldisc races
Alan Cox01e1abb2008-07-22 11:16:55 +010060 */
61
62int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc)
63{
64 unsigned long flags;
65 int ret = 0;
66
67 if (disc < N_TTY || disc >= NR_LDISCS)
68 return -EINVAL;
69
Peter Hurley137084b2013-06-15 07:04:46 -040070 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
Alan Cox01e1abb2008-07-22 11:16:55 +010071 tty_ldiscs[disc] = new_ldisc;
72 new_ldisc->num = disc;
73 new_ldisc->refcount = 0;
Peter Hurley137084b2013-06-15 07:04:46 -040074 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
Alan Cox01e1abb2008-07-22 11:16:55 +010075
76 return ret;
77}
78EXPORT_SYMBOL(tty_register_ldisc);
79
80/**
81 * tty_unregister_ldisc - unload a line discipline
82 * @disc: ldisc number
83 * @new_ldisc: pointer to the ldisc object
84 *
85 * Remove a line discipline from the kernel providing it is not
86 * currently in use.
87 *
88 * Locking:
Peter Hurley137084b2013-06-15 07:04:46 -040089 * takes tty_ldiscs_lock to guard against ldisc races
Alan Cox01e1abb2008-07-22 11:16:55 +010090 */
91
92int tty_unregister_ldisc(int disc)
93{
94 unsigned long flags;
95 int ret = 0;
96
97 if (disc < N_TTY || disc >= NR_LDISCS)
98 return -EINVAL;
99
Peter Hurley137084b2013-06-15 07:04:46 -0400100 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
Alan Cox01e1abb2008-07-22 11:16:55 +0100101 if (tty_ldiscs[disc]->refcount)
102 ret = -EBUSY;
103 else
104 tty_ldiscs[disc] = NULL;
Peter Hurley137084b2013-06-15 07:04:46 -0400105 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
Alan Cox01e1abb2008-07-22 11:16:55 +0100106
107 return ret;
108}
109EXPORT_SYMBOL(tty_unregister_ldisc);
110
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700111static struct tty_ldisc_ops *get_ldops(int disc)
112{
113 unsigned long flags;
114 struct tty_ldisc_ops *ldops, *ret;
115
Peter Hurley137084b2013-06-15 07:04:46 -0400116 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700117 ret = ERR_PTR(-EINVAL);
118 ldops = tty_ldiscs[disc];
119 if (ldops) {
120 ret = ERR_PTR(-EAGAIN);
121 if (try_module_get(ldops->owner)) {
122 ldops->refcount++;
123 ret = ldops;
124 }
125 }
Peter Hurley137084b2013-06-15 07:04:46 -0400126 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700127 return ret;
128}
129
130static void put_ldops(struct tty_ldisc_ops *ldops)
131{
132 unsigned long flags;
133
Peter Hurley137084b2013-06-15 07:04:46 -0400134 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700135 ldops->refcount--;
136 module_put(ldops->owner);
Peter Hurley137084b2013-06-15 07:04:46 -0400137 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700138}
Alan Cox01e1abb2008-07-22 11:16:55 +0100139
140/**
Alan Cox01e1abb2008-07-22 11:16:55 +0100141 * tty_ldisc_get - take a reference to an ldisc
142 * @disc: ldisc number
Alan Cox01e1abb2008-07-22 11:16:55 +0100143 *
144 * Takes a reference to a line discipline. Deals with refcounts and
145 * module locking counts. Returns NULL if the discipline is not available.
146 * Returns a pointer to the discipline and bumps the ref count if it is
147 * available
148 *
149 * Locking:
Peter Hurley137084b2013-06-15 07:04:46 -0400150 * takes tty_ldiscs_lock to guard against ldisc races
Alan Cox01e1abb2008-07-22 11:16:55 +0100151 */
152
Peter Hurley36697522013-06-15 07:04:48 -0400153static struct tty_ldisc *tty_ldisc_get(struct tty_struct *tty, int disc)
Alan Cox01e1abb2008-07-22 11:16:55 +0100154{
Alan Coxc65c9bc2009-06-11 12:50:12 +0100155 struct tty_ldisc *ld;
Linus Torvalds182274f2009-08-03 16:01:28 -0700156 struct tty_ldisc_ops *ldops;
Alan Cox01e1abb2008-07-22 11:16:55 +0100157
158 if (disc < N_TTY || disc >= NR_LDISCS)
Alan Coxc65c9bc2009-06-11 12:50:12 +0100159 return ERR_PTR(-EINVAL);
Linus Torvalds182274f2009-08-03 16:01:28 -0700160
161 /*
162 * Get the ldisc ops - we may need to request them to be loaded
163 * dynamically and try again.
164 */
165 ldops = get_ldops(disc);
166 if (IS_ERR(ldops)) {
Alan Cox01e1abb2008-07-22 11:16:55 +0100167 request_module("tty-ldisc-%d", disc);
Linus Torvalds182274f2009-08-03 16:01:28 -0700168 ldops = get_ldops(disc);
169 if (IS_ERR(ldops))
170 return ERR_CAST(ldops);
Alan Cox01e1abb2008-07-22 11:16:55 +0100171 }
Linus Torvalds182274f2009-08-03 16:01:28 -0700172
173 ld = kmalloc(sizeof(struct tty_ldisc), GFP_KERNEL);
174 if (ld == NULL) {
175 put_ldops(ldops);
176 return ERR_PTR(-ENOMEM);
177 }
178
179 ld->ops = ldops;
Peter Hurley36697522013-06-15 07:04:48 -0400180 ld->tty = tty;
Ivo Sieben1541f842012-05-03 14:37:43 +0200181
Alan Coxc65c9bc2009-06-11 12:50:12 +0100182 return ld;
Alan Cox01e1abb2008-07-22 11:16:55 +0100183}
184
Peter Hurley734de242013-03-11 16:44:43 -0400185/**
186 * tty_ldisc_put - release the ldisc
187 *
188 * Complement of tty_ldisc_get().
189 */
190static inline void tty_ldisc_put(struct tty_ldisc *ld)
191{
Peter Hurley734de242013-03-11 16:44:43 -0400192 if (WARN_ON_ONCE(!ld))
193 return;
194
Peter Hurley36697522013-06-15 07:04:48 -0400195 put_ldops(ld->ops);
Peter Hurley734de242013-03-11 16:44:43 -0400196 kfree(ld);
Peter Hurley734de242013-03-11 16:44:43 -0400197}
198
Alan Cox852e99d2009-06-11 12:51:41 +0100199static void *tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos)
Alan Cox01e1abb2008-07-22 11:16:55 +0100200{
201 return (*pos < NR_LDISCS) ? pos : NULL;
202}
203
Alan Cox852e99d2009-06-11 12:51:41 +0100204static void *tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos)
Alan Cox01e1abb2008-07-22 11:16:55 +0100205{
206 (*pos)++;
207 return (*pos < NR_LDISCS) ? pos : NULL;
208}
209
210static void tty_ldiscs_seq_stop(struct seq_file *m, void *v)
211{
212}
213
214static int tty_ldiscs_seq_show(struct seq_file *m, void *v)
215{
216 int i = *(loff_t *)v;
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700217 struct tty_ldisc_ops *ldops;
Alan Cox852e99d2009-06-11 12:51:41 +0100218
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700219 ldops = get_ldops(i);
220 if (IS_ERR(ldops))
Alan Cox01e1abb2008-07-22 11:16:55 +0100221 return 0;
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700222 seq_printf(m, "%-10s %2d\n", ldops->name ? ldops->name : "???", i);
223 put_ldops(ldops);
Alan Cox01e1abb2008-07-22 11:16:55 +0100224 return 0;
225}
226
227static const struct seq_operations tty_ldiscs_seq_ops = {
228 .start = tty_ldiscs_seq_start,
229 .next = tty_ldiscs_seq_next,
230 .stop = tty_ldiscs_seq_stop,
231 .show = tty_ldiscs_seq_show,
232};
233
234static int proc_tty_ldiscs_open(struct inode *inode, struct file *file)
235{
236 return seq_open(file, &tty_ldiscs_seq_ops);
237}
238
239const struct file_operations tty_ldiscs_proc_fops = {
240 .owner = THIS_MODULE,
241 .open = proc_tty_ldiscs_open,
242 .read = seq_read,
243 .llseek = seq_lseek,
244 .release = seq_release,
245};
246
247/**
Alan Cox01e1abb2008-07-22 11:16:55 +0100248 * tty_ldisc_ref_wait - wait for the tty ldisc
249 * @tty: tty device
250 *
251 * Dereference the line discipline for the terminal and take a
252 * reference to it. If the line discipline is in flux then
253 * wait patiently until it changes.
254 *
255 * Note: Must not be called from an IRQ/timer context. The caller
256 * must also be careful not to hold other locks that will deadlock
257 * against a discipline change, such as an existing ldisc reference
258 * (which we check for)
259 *
Peter Hurley36697522013-06-15 07:04:48 -0400260 * Note: only callable from a file_operations routine (which
261 * guarantees tty->ldisc != NULL when the lock is acquired).
Alan Cox01e1abb2008-07-22 11:16:55 +0100262 */
263
264struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
265{
Peter Hurley36697522013-06-15 07:04:48 -0400266 ldsem_down_read(&tty->ldisc_sem, MAX_SCHEDULE_TIMEOUT);
267 WARN_ON(!tty->ldisc);
268 return tty->ldisc;
Alan Cox01e1abb2008-07-22 11:16:55 +0100269}
Alan Cox01e1abb2008-07-22 11:16:55 +0100270EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
271
272/**
273 * tty_ldisc_ref - get the tty ldisc
274 * @tty: tty device
275 *
276 * Dereference the line discipline for the terminal and take a
277 * reference to it. If the line discipline is in flux then
278 * return NULL. Can be called from IRQ and timer functions.
Alan Cox01e1abb2008-07-22 11:16:55 +0100279 */
280
281struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
282{
Peter Hurley36697522013-06-15 07:04:48 -0400283 struct tty_ldisc *ld = NULL;
284
285 if (ldsem_down_read_trylock(&tty->ldisc_sem)) {
286 ld = tty->ldisc;
287 if (!ld)
288 ldsem_up_read(&tty->ldisc_sem);
289 }
290 return ld;
Alan Cox01e1abb2008-07-22 11:16:55 +0100291}
Alan Cox01e1abb2008-07-22 11:16:55 +0100292EXPORT_SYMBOL_GPL(tty_ldisc_ref);
293
294/**
295 * tty_ldisc_deref - free a tty ldisc reference
296 * @ld: reference to free up
297 *
298 * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May
299 * be called in IRQ context.
Alan Cox01e1abb2008-07-22 11:16:55 +0100300 */
301
302void tty_ldisc_deref(struct tty_ldisc *ld)
303{
Peter Hurley36697522013-06-15 07:04:48 -0400304 ldsem_up_read(&ld->tty->ldisc_sem);
Alan Cox01e1abb2008-07-22 11:16:55 +0100305}
Alan Cox01e1abb2008-07-22 11:16:55 +0100306EXPORT_SYMBOL_GPL(tty_ldisc_deref);
307
Peter Hurleyd2c43892013-06-15 07:04:47 -0400308
309static inline int __lockfunc
Peter Hurleye80a10e2014-11-05 12:13:06 -0500310__tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout)
Peter Hurleyd2c43892013-06-15 07:04:47 -0400311{
312 return ldsem_down_write(&tty->ldisc_sem, timeout);
313}
314
315static inline int __lockfunc
Peter Hurleye80a10e2014-11-05 12:13:06 -0500316__tty_ldisc_lock_nested(struct tty_struct *tty, unsigned long timeout)
Peter Hurleyd2c43892013-06-15 07:04:47 -0400317{
318 return ldsem_down_write_nested(&tty->ldisc_sem,
319 LDISC_SEM_OTHER, timeout);
320}
321
Peter Hurleye80a10e2014-11-05 12:13:06 -0500322static inline void __tty_ldisc_unlock(struct tty_struct *tty)
Peter Hurleyd2c43892013-06-15 07:04:47 -0400323{
324 return ldsem_up_write(&tty->ldisc_sem);
325}
326
327static int __lockfunc
Peter Hurleyfae76e92014-11-05 12:13:07 -0500328tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout)
329{
330 int ret;
331
332 ret = __tty_ldisc_lock(tty, timeout);
333 if (!ret)
334 return -EBUSY;
335 set_bit(TTY_LDISC_HALTED, &tty->flags);
336 return 0;
337}
338
339static void tty_ldisc_unlock(struct tty_struct *tty)
340{
341 clear_bit(TTY_LDISC_HALTED, &tty->flags);
342 __tty_ldisc_unlock(tty);
343}
344
345static int __lockfunc
Peter Hurleyd2c43892013-06-15 07:04:47 -0400346tty_ldisc_lock_pair_timeout(struct tty_struct *tty, struct tty_struct *tty2,
347 unsigned long timeout)
348{
349 int ret;
350
351 if (tty < tty2) {
Peter Hurleye80a10e2014-11-05 12:13:06 -0500352 ret = __tty_ldisc_lock(tty, timeout);
Peter Hurleyd2c43892013-06-15 07:04:47 -0400353 if (ret) {
Peter Hurleye80a10e2014-11-05 12:13:06 -0500354 ret = __tty_ldisc_lock_nested(tty2, timeout);
Peter Hurleyd2c43892013-06-15 07:04:47 -0400355 if (!ret)
Peter Hurleye80a10e2014-11-05 12:13:06 -0500356 __tty_ldisc_unlock(tty);
Peter Hurleyd2c43892013-06-15 07:04:47 -0400357 }
358 } else {
359 /* if this is possible, it has lots of implications */
360 WARN_ON_ONCE(tty == tty2);
361 if (tty2 && tty != tty2) {
Peter Hurleye80a10e2014-11-05 12:13:06 -0500362 ret = __tty_ldisc_lock(tty2, timeout);
Peter Hurleyd2c43892013-06-15 07:04:47 -0400363 if (ret) {
Peter Hurleye80a10e2014-11-05 12:13:06 -0500364 ret = __tty_ldisc_lock_nested(tty, timeout);
Peter Hurleyd2c43892013-06-15 07:04:47 -0400365 if (!ret)
Peter Hurleye80a10e2014-11-05 12:13:06 -0500366 __tty_ldisc_unlock(tty2);
Peter Hurleyd2c43892013-06-15 07:04:47 -0400367 }
368 } else
Peter Hurleye80a10e2014-11-05 12:13:06 -0500369 ret = __tty_ldisc_lock(tty, timeout);
Peter Hurleyd2c43892013-06-15 07:04:47 -0400370 }
371
372 if (!ret)
373 return -EBUSY;
374
375 set_bit(TTY_LDISC_HALTED, &tty->flags);
376 if (tty2)
377 set_bit(TTY_LDISC_HALTED, &tty2->flags);
378 return 0;
379}
380
381static void __lockfunc
382tty_ldisc_lock_pair(struct tty_struct *tty, struct tty_struct *tty2)
383{
384 tty_ldisc_lock_pair_timeout(tty, tty2, MAX_SCHEDULE_TIMEOUT);
385}
386
387static void __lockfunc tty_ldisc_unlock_pair(struct tty_struct *tty,
388 struct tty_struct *tty2)
389{
Peter Hurleye80a10e2014-11-05 12:13:06 -0500390 __tty_ldisc_unlock(tty);
Peter Hurleyd2c43892013-06-15 07:04:47 -0400391 if (tty2)
Peter Hurleye80a10e2014-11-05 12:13:06 -0500392 __tty_ldisc_unlock(tty2);
Peter Hurleyd2c43892013-06-15 07:04:47 -0400393}
394
Alan Cox01e1abb2008-07-22 11:16:55 +0100395/**
Alan Coxf2c4c652009-06-11 12:50:58 +0100396 * tty_ldisc_flush - flush line discipline queue
397 * @tty: tty
398 *
Peter Hurley86c80a82014-11-05 12:13:09 -0500399 * Flush the line discipline queue (if any) and the tty flip buffers
400 * for this tty.
Alan Coxf2c4c652009-06-11 12:50:58 +0100401 */
402
403void tty_ldisc_flush(struct tty_struct *tty)
404{
405 struct tty_ldisc *ld = tty_ldisc_ref(tty);
Peter Hurley86c80a82014-11-05 12:13:09 -0500406
407 tty_buffer_flush(tty, ld);
408 if (ld)
Alan Coxf2c4c652009-06-11 12:50:58 +0100409 tty_ldisc_deref(ld);
Alan Coxf2c4c652009-06-11 12:50:58 +0100410}
Alan Coxf2c4c652009-06-11 12:50:58 +0100411EXPORT_SYMBOL_GPL(tty_ldisc_flush);
412
413/**
Alan Cox01e1abb2008-07-22 11:16:55 +0100414 * tty_set_termios_ldisc - set ldisc field
415 * @tty: tty structure
416 * @num: line discipline number
417 *
418 * This is probably overkill for real world processors but
419 * they are not on hot paths so a little discipline won't do
420 * any harm.
421 *
Peter Hurley6a1c0682013-06-15 09:14:23 -0400422 * Locking: takes termios_rwsem
Alan Cox01e1abb2008-07-22 11:16:55 +0100423 */
424
425static void tty_set_termios_ldisc(struct tty_struct *tty, int num)
426{
Peter Hurley6a1c0682013-06-15 09:14:23 -0400427 down_write(&tty->termios_rwsem);
Alan Coxadc8d742012-07-14 15:31:47 +0100428 tty->termios.c_line = num;
Peter Hurley6a1c0682013-06-15 09:14:23 -0400429 up_write(&tty->termios_rwsem);
Alan Cox01e1abb2008-07-22 11:16:55 +0100430}
431
Alan Coxc65c9bc2009-06-11 12:50:12 +0100432/**
433 * tty_ldisc_open - open a line discipline
434 * @tty: tty we are opening the ldisc on
435 * @ld: discipline to open
436 *
437 * A helper opening method. Also a convenient debugging and check
438 * point.
Arnd Bergmannec79d602010-06-01 22:53:01 +0200439 *
440 * Locking: always called with BTM already held.
Alan Coxc65c9bc2009-06-11 12:50:12 +0100441 */
442
443static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld)
444{
445 WARN_ON(test_and_set_bit(TTY_LDISC_OPEN, &tty->flags));
Alan Coxf18f9492009-11-30 13:18:35 +0000446 if (ld->ops->open) {
447 int ret;
Arnd Bergmannec79d602010-06-01 22:53:01 +0200448 /* BTM here locks versus a hangup event */
Alan Coxf18f9492009-11-30 13:18:35 +0000449 ret = ld->ops->open(tty);
Jiri Slaby7f90cfc2010-11-25 00:27:54 +0100450 if (ret)
451 clear_bit(TTY_LDISC_OPEN, &tty->flags);
Alan Coxf18f9492009-11-30 13:18:35 +0000452 return ret;
453 }
Alan Coxc65c9bc2009-06-11 12:50:12 +0100454 return 0;
455}
456
457/**
458 * tty_ldisc_close - close a line discipline
459 * @tty: tty we are opening the ldisc on
460 * @ld: discipline to close
461 *
462 * A helper close method. Also a convenient debugging and check
463 * point.
464 */
465
466static void tty_ldisc_close(struct tty_struct *tty, struct tty_ldisc *ld)
467{
468 WARN_ON(!test_bit(TTY_LDISC_OPEN, &tty->flags));
469 clear_bit(TTY_LDISC_OPEN, &tty->flags);
470 if (ld->ops->close)
471 ld->ops->close(tty);
472}
Alan Cox01e1abb2008-07-22 11:16:55 +0100473
474/**
475 * tty_ldisc_restore - helper for tty ldisc change
476 * @tty: tty to recover
477 * @old: previous ldisc
478 *
479 * Restore the previous line discipline or N_TTY when a line discipline
480 * change fails due to an open error
481 */
482
483static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old)
484{
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 */
Peter Hurley36697522013-06-15 07:04:48 -0400489 old = tty_ldisc_get(tty, old->ops->num);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100490 WARN_ON(IS_ERR(old));
Peter Hurleyf48070452013-03-11 16:44:42 -0400491 tty->ldisc = old;
Alan Cox01e1abb2008-07-22 11:16:55 +0100492 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 */
Peter Hurley36697522013-06-15 07:04:48 -0400496 new_ldisc = tty_ldisc_get(tty, 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");
Peter Hurleyf48070452013-03-11 16:44:42 -0400499 tty->ldisc = 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.",
Rasmus Villemoes429b4742015-03-31 15:55:59 +0200505 tty_name(tty), r);
Alan Cox01e1abb2008-07-22 11:16:55 +0100506 }
507}
508
509/**
510 * tty_set_ldisc - set line discipline
511 * @tty: the terminal to set
512 * @ldisc: the line discipline
513 *
514 * Set the discipline of a tty line. Must be called from a process
Alan Coxc65c9bc2009-06-11 12:50:12 +0100515 * context. The ldisc change logic has to protect itself against any
516 * overlapping ldisc change (including on the other end of pty pairs),
517 * the close of one side of a tty/pty pair, and eventually hangup.
Alan Cox01e1abb2008-07-22 11:16:55 +0100518 */
519
520int tty_set_ldisc(struct tty_struct *tty, int ldisc)
521{
522 int retval;
Peter Hurley9fbfa342013-06-15 07:04:49 -0400523 struct tty_ldisc *old_ldisc, *new_ldisc;
Alan Cox01e1abb2008-07-22 11:16:55 +0100524
Peter Hurley36697522013-06-15 07:04:48 -0400525 new_ldisc = tty_ldisc_get(tty, ldisc);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100526 if (IS_ERR(new_ldisc))
527 return PTR_ERR(new_ldisc);
528
Peter Hurleyc8483bc2014-11-05 12:12:45 -0500529 tty_lock(tty);
Peter Hurley276a6612014-11-05 12:13:08 -0500530 retval = tty_ldisc_lock(tty, 5 * HZ);
Peter Hurley36697522013-06-15 07:04:48 -0400531 if (retval) {
532 tty_ldisc_put(new_ldisc);
Peter Hurleyc8483bc2014-11-05 12:12:45 -0500533 tty_unlock(tty);
Peter Hurley36697522013-06-15 07:04:48 -0400534 return retval;
535 }
Alan Coxc65c9bc2009-06-11 12:50:12 +0100536
537 /*
538 * Check the no-op case
539 */
540
541 if (tty->ldisc->ops->num == ldisc) {
Peter Hurley276a6612014-11-05 12:13:08 -0500542 tty_ldisc_unlock(tty);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100543 tty_ldisc_put(new_ldisc);
Peter Hurleyc8483bc2014-11-05 12:12:45 -0500544 tty_unlock(tty);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100545 return 0;
546 }
Alan Cox01e1abb2008-07-22 11:16:55 +0100547
Peter Hurley9fbfa342013-06-15 07:04:49 -0400548 old_ldisc = tty->ldisc;
Jiri Slaby100eeae2010-10-31 23:17:51 +0100549
Peter Hurley3ff51a12014-11-05 12:12:46 -0500550 if (test_bit(TTY_HUPPED, &tty->flags)) {
Alan Coxc65c9bc2009-06-11 12:50:12 +0100551 /* We were raced by the hangup method. It will have stomped
552 the ldisc data and closed the ldisc down */
Peter Hurley276a6612014-11-05 12:13:08 -0500553 tty_ldisc_unlock(tty);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100554 tty_ldisc_put(new_ldisc);
Alan Cox89c8d912012-08-08 16:30:13 +0100555 tty_unlock(tty);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100556 return -EIO;
557 }
558
Peter Hurley9fbfa342013-06-15 07:04:49 -0400559 /* Shutdown the old discipline. */
560 tty_ldisc_close(tty, old_ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100561
562 /* Now set up the new line discipline. */
Peter Hurleyf48070452013-03-11 16:44:42 -0400563 tty->ldisc = new_ldisc;
Alan Cox01e1abb2008-07-22 11:16:55 +0100564 tty_set_termios_ldisc(tty, ldisc);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100565
566 retval = tty_ldisc_open(tty, new_ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100567 if (retval < 0) {
Alan Coxc65c9bc2009-06-11 12:50:12 +0100568 /* Back to the old one or N_TTY if we can't */
569 tty_ldisc_put(new_ldisc);
Peter Hurley9fbfa342013-06-15 07:04:49 -0400570 tty_ldisc_restore(tty, old_ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100571 }
Alan Coxc65c9bc2009-06-11 12:50:12 +0100572
Peter Hurley9191aaa2014-11-05 13:11:41 -0500573 if (tty->ldisc->ops->num != old_ldisc->ops->num && tty->ops->set_ldisc) {
574 down_read(&tty->termios_rwsem);
Alan Cox01e1abb2008-07-22 11:16:55 +0100575 tty->ops->set_ldisc(tty);
Peter Hurley9191aaa2014-11-05 13:11:41 -0500576 up_read(&tty->termios_rwsem);
577 }
Alan Cox01e1abb2008-07-22 11:16:55 +0100578
Peter Hurleyb0e95852013-06-15 07:04:51 -0400579 /* At this point we hold a reference to the new ldisc and a
580 reference to the old ldisc, or we hold two references to
581 the old ldisc (if it was restored as part of error cleanup
582 above). In either case, releasing a single reference from
583 the old ldisc is correct. */
584
Peter Hurley9fbfa342013-06-15 07:04:49 -0400585 tty_ldisc_put(old_ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100586
587 /*
Alan Coxc65c9bc2009-06-11 12:50:12 +0100588 * Allow ldisc referencing to occur again
Alan Cox01e1abb2008-07-22 11:16:55 +0100589 */
Peter Hurley276a6612014-11-05 12:13:08 -0500590 tty_ldisc_unlock(tty);
Alan Cox01e1abb2008-07-22 11:16:55 +0100591
Alan Coxc65c9bc2009-06-11 12:50:12 +0100592 /* Restart the work queue in case no characters kick it off. Safe if
Alan Cox01e1abb2008-07-22 11:16:55 +0100593 already running */
Peter Hurley4f98d462013-03-11 16:44:34 -0400594 schedule_work(&tty->port->buf.work);
Peter Hurley4f98d462013-03-11 16:44:34 -0400595
Alan Cox89c8d912012-08-08 16:30:13 +0100596 tty_unlock(tty);
Alan Cox01e1abb2008-07-22 11:16:55 +0100597 return retval;
598}
599
Alan Coxc65c9bc2009-06-11 12:50:12 +0100600/**
601 * tty_reset_termios - reset terminal state
602 * @tty: tty to reset
603 *
604 * Restore a terminal to the driver default state.
605 */
606
607static void tty_reset_termios(struct tty_struct *tty)
608{
Peter Hurley6a1c0682013-06-15 09:14:23 -0400609 down_write(&tty->termios_rwsem);
Alan Coxadc8d742012-07-14 15:31:47 +0100610 tty->termios = tty->driver->init_termios;
611 tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios);
612 tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios);
Peter Hurley6a1c0682013-06-15 09:14:23 -0400613 up_write(&tty->termios_rwsem);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100614}
615
616
617/**
618 * tty_ldisc_reinit - reinitialise the tty ldisc
619 * @tty: tty to reinit
Alan Cox638b9642010-02-08 10:09:26 +0000620 * @ldisc: line discipline to reinitialize
Alan Coxc65c9bc2009-06-11 12:50:12 +0100621 *
Alan Cox638b9642010-02-08 10:09:26 +0000622 * Switch the tty to a line discipline and leave the ldisc
623 * state closed
Alan Coxc65c9bc2009-06-11 12:50:12 +0100624 */
625
Philippe Rétornaz1c95ba12010-10-27 17:13:21 +0200626static int tty_ldisc_reinit(struct tty_struct *tty, int ldisc)
Alan Coxc65c9bc2009-06-11 12:50:12 +0100627{
Peter Hurley36697522013-06-15 07:04:48 -0400628 struct tty_ldisc *ld = tty_ldisc_get(tty, ldisc);
Philippe Rétornaz1c95ba12010-10-27 17:13:21 +0200629
630 if (IS_ERR(ld))
631 return -1;
Alan Coxc65c9bc2009-06-11 12:50:12 +0100632
633 tty_ldisc_close(tty, tty->ldisc);
634 tty_ldisc_put(tty->ldisc);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100635 /*
636 * Switch the line discipline back
637 */
Peter Hurleyf48070452013-03-11 16:44:42 -0400638 tty->ldisc = ld;
Alan Cox638b9642010-02-08 10:09:26 +0000639 tty_set_termios_ldisc(tty, ldisc);
Philippe Rétornaz1c95ba12010-10-27 17:13:21 +0200640
641 return 0;
Alan Coxc65c9bc2009-06-11 12:50:12 +0100642}
643
644/**
645 * tty_ldisc_hangup - hangup ldisc reset
646 * @tty: tty being hung up
647 *
648 * Some tty devices reset their termios when they receive a hangup
649 * event. In that situation we must also switch back to N_TTY properly
650 * before we reset the termios data.
651 *
652 * Locking: We can take the ldisc mutex as the rest of the code is
653 * careful to allow for this.
654 *
655 * In the pty pair case this occurs in the close() path of the
656 * tty itself so we must be careful about locking rules.
657 */
658
659void tty_ldisc_hangup(struct tty_struct *tty)
660{
661 struct tty_ldisc *ld;
Alan Cox638b9642010-02-08 10:09:26 +0000662 int reset = tty->driver->flags & TTY_DRIVER_RESET_TERMIOS;
663 int err = 0;
Alan Coxc65c9bc2009-06-11 12:50:12 +0100664
Peter Hurleyfc575ee2013-03-11 16:44:38 -0400665 tty_ldisc_debug(tty, "closing ldisc: %p\n", tty->ldisc);
666
Alan Coxc65c9bc2009-06-11 12:50:12 +0100667 ld = tty_ldisc_ref(tty);
668 if (ld != NULL) {
Alan Coxc65c9bc2009-06-11 12:50:12 +0100669 if (ld->ops->flush_buffer)
670 ld->ops->flush_buffer(tty);
671 tty_driver_flush_buffer(tty);
672 if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
673 ld->ops->write_wakeup)
674 ld->ops->write_wakeup(tty);
675 if (ld->ops->hangup)
676 ld->ops->hangup(tty);
677 tty_ldisc_deref(ld);
678 }
Peter Hurley36697522013-06-15 07:04:48 -0400679
Alan Coxc65c9bc2009-06-11 12:50:12 +0100680 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
681 wake_up_interruptible_poll(&tty->read_wait, POLLIN);
Peter Hurley36697522013-06-15 07:04:48 -0400682
Alan Coxc65c9bc2009-06-11 12:50:12 +0100683 /*
684 * Shutdown the current line discipline, and reset it to
Alan Cox638b9642010-02-08 10:09:26 +0000685 * N_TTY if need be.
686 *
687 * Avoid racing set_ldisc or tty_ldisc_release
Alan Coxc65c9bc2009-06-11 12:50:12 +0100688 */
Peter Hurleyfae76e92014-11-05 12:13:07 -0500689 tty_ldisc_lock(tty, MAX_SCHEDULE_TIMEOUT);
Arnd Bergmann60af22d2010-06-01 22:53:06 +0200690
Peter Hurley36697522013-06-15 07:04:48 -0400691 if (tty->ldisc) {
Peter Hurleyc8785242013-03-11 16:44:36 -0400692
693 /* At this point we have a halted ldisc; we want to close it and
694 reopen a new ldisc. We could defer the reopen to the next
695 open but it means auditing a lot of other paths so this is
696 a FIXME */
Alan Cox638b9642010-02-08 10:09:26 +0000697 if (reset == 0) {
Philippe Rétornaz1c95ba12010-10-27 17:13:21 +0200698
Alan Coxadc8d742012-07-14 15:31:47 +0100699 if (!tty_ldisc_reinit(tty, tty->termios.c_line))
Philippe Rétornaz1c95ba12010-10-27 17:13:21 +0200700 err = tty_ldisc_open(tty, tty->ldisc);
701 else
702 err = 1;
Alan Coxc8d50042009-07-16 16:05:08 +0100703 }
Alan Cox638b9642010-02-08 10:09:26 +0000704 /* If the re-open fails or we reset then go to N_TTY. The
705 N_TTY open cannot fail */
706 if (reset || err) {
Philippe Rétornaz1c95ba12010-10-27 17:13:21 +0200707 BUG_ON(tty_ldisc_reinit(tty, N_TTY));
Alan Cox638b9642010-02-08 10:09:26 +0000708 WARN_ON(tty_ldisc_open(tty, tty->ldisc));
709 }
Alan Coxc65c9bc2009-06-11 12:50:12 +0100710 }
Peter Hurleyfae76e92014-11-05 12:13:07 -0500711 tty_ldisc_unlock(tty);
Alan Cox638b9642010-02-08 10:09:26 +0000712 if (reset)
713 tty_reset_termios(tty);
Peter Hurleyfc575ee2013-03-11 16:44:38 -0400714
715 tty_ldisc_debug(tty, "re-opened ldisc: %p\n", tty->ldisc);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100716}
Alan Cox01e1abb2008-07-22 11:16:55 +0100717
718/**
719 * tty_ldisc_setup - open line discipline
720 * @tty: tty being shut down
721 * @o_tty: pair tty for pty/tty pairs
722 *
723 * Called during the initial open of a tty/pty pair in order to set up the
Alan Coxc65c9bc2009-06-11 12:50:12 +0100724 * line disciplines and bind them to the tty. This has no locking issues
725 * as the device isn't yet active.
Alan Cox01e1abb2008-07-22 11:16:55 +0100726 */
727
728int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty)
729{
Alan Coxc65c9bc2009-06-11 12:50:12 +0100730 struct tty_ldisc *ld = tty->ldisc;
Alan Cox01e1abb2008-07-22 11:16:55 +0100731 int retval;
732
Alan Coxc65c9bc2009-06-11 12:50:12 +0100733 retval = tty_ldisc_open(tty, ld);
734 if (retval)
735 return retval;
736
737 if (o_tty) {
738 retval = tty_ldisc_open(o_tty, o_tty->ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100739 if (retval) {
Alan Coxc65c9bc2009-06-11 12:50:12 +0100740 tty_ldisc_close(tty, ld);
Alan Cox01e1abb2008-07-22 11:16:55 +0100741 return retval;
742 }
Alan Cox01e1abb2008-07-22 11:16:55 +0100743 }
Alan Cox01e1abb2008-07-22 11:16:55 +0100744 return 0;
745}
Alan Cox89c8d912012-08-08 16:30:13 +0100746
747static void tty_ldisc_kill(struct tty_struct *tty)
748{
Alan Cox89c8d912012-08-08 16:30:13 +0100749 /*
750 * Now kill off the ldisc
751 */
752 tty_ldisc_close(tty, tty->ldisc);
753 tty_ldisc_put(tty->ldisc);
754 /* Force an oops if we mess this up */
755 tty->ldisc = NULL;
756
757 /* Ensure the next open requests the N_TTY ldisc */
758 tty_set_termios_ldisc(tty, N_TTY);
Alan Cox89c8d912012-08-08 16:30:13 +0100759}
760
Alan Cox01e1abb2008-07-22 11:16:55 +0100761/**
762 * tty_ldisc_release - release line discipline
Peter Hurley62462ae2014-11-05 12:12:58 -0500763 * @tty: tty being shut down (or one end of pty pair)
Alan Cox01e1abb2008-07-22 11:16:55 +0100764 *
Peter Hurley62462ae2014-11-05 12:12:58 -0500765 * Called during the final close of a tty or a pty pair in order to shut
766 * down the line discpline layer. On exit, each ldisc assigned is N_TTY and
767 * each ldisc has not been opened.
Alan Cox01e1abb2008-07-22 11:16:55 +0100768 */
769
Peter Hurley62462ae2014-11-05 12:12:58 -0500770void tty_ldisc_release(struct tty_struct *tty)
Alan Cox01e1abb2008-07-22 11:16:55 +0100771{
Peter Hurley62462ae2014-11-05 12:12:58 -0500772 struct tty_struct *o_tty = tty->link;
773
Alan Cox01e1abb2008-07-22 11:16:55 +0100774 /*
Peter Hurleya2965b72013-03-11 16:44:35 -0400775 * Shutdown this line discipline. As this is the final close,
776 * it does not race with the set_ldisc code path.
Alan Cox01e1abb2008-07-22 11:16:55 +0100777 */
Alan Cox01e1abb2008-07-22 11:16:55 +0100778
Peter Hurleyfc575ee2013-03-11 16:44:38 -0400779 tty_ldisc_debug(tty, "closing ldisc: %p\n", tty->ldisc);
780
Peter Hurley36697522013-06-15 07:04:48 -0400781 tty_ldisc_lock_pair(tty, o_tty);
Alan Cox89c8d912012-08-08 16:30:13 +0100782 tty_ldisc_kill(tty);
Alan Cox89c8d912012-08-08 16:30:13 +0100783 if (o_tty)
784 tty_ldisc_kill(o_tty);
Peter Hurley36697522013-06-15 07:04:48 -0400785 tty_ldisc_unlock_pair(tty, o_tty);
786
Alan Coxaef29bc2009-06-29 15:21:47 +0100787 /* And the memory resources remaining (buffers, termios) will be
788 disposed of when the kref hits zero */
Peter Hurleyfc575ee2013-03-11 16:44:38 -0400789
790 tty_ldisc_debug(tty, "ldisc closed\n");
Alan Cox01e1abb2008-07-22 11:16:55 +0100791}
792
793/**
794 * tty_ldisc_init - ldisc setup for new tty
795 * @tty: tty being allocated
796 *
797 * Set up the line discipline objects for a newly allocated tty. Note that
798 * the tty structure is not completely set up when this call is made.
799 */
800
801void tty_ldisc_init(struct tty_struct *tty)
802{
Peter Hurley36697522013-06-15 07:04:48 -0400803 struct tty_ldisc *ld = tty_ldisc_get(tty, N_TTY);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100804 if (IS_ERR(ld))
Alan Cox01e1abb2008-07-22 11:16:55 +0100805 panic("n_tty: init_tty");
Peter Hurleyf48070452013-03-11 16:44:42 -0400806 tty->ldisc = ld;
Alan Cox01e1abb2008-07-22 11:16:55 +0100807}
808
Jiri Slaby67166712011-03-23 10:48:35 +0100809/**
810 * tty_ldisc_init - ldisc cleanup for new tty
811 * @tty: tty that was allocated recently
812 *
813 * The tty structure must not becompletely set up (tty_ldisc_setup) when
814 * this call is made.
815 */
816void tty_ldisc_deinit(struct tty_struct *tty)
817{
Peter Hurleyebc9bae2013-03-11 16:44:40 -0400818 tty_ldisc_put(tty->ldisc);
Peter Hurleyf48070452013-03-11 16:44:42 -0400819 tty->ldisc = NULL;
Jiri Slaby67166712011-03-23 10:48:35 +0100820}
821
Alan Cox01e1abb2008-07-22 11:16:55 +0100822void tty_ldisc_begin(void)
823{
824 /* Setup the default TTY line discipline. */
825 (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
826}