blob: 5819667f5bdb02c38bcfe44dd1787c068a1d0a59 [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
Peter Hurleyd2c43892013-06-15 07:04:47 -040034/* lockdep nested classes for tty->ldisc_sem */
35enum {
36 LDISC_SEM_NORMAL,
37 LDISC_SEM_OTHER,
38};
39
40
Alan Cox01e1abb2008-07-22 11:16:55 +010041/*
42 * This guards the refcounted line discipline lists. The lock
43 * must be taken with irqs off because there are hangup path
44 * callers who will do ldisc lookups and cannot sleep.
45 */
46
Peter Hurley137084b2013-06-15 07:04:46 -040047static DEFINE_RAW_SPINLOCK(tty_ldiscs_lock);
Alan Cox01e1abb2008-07-22 11:16:55 +010048/* Line disc dispatch table */
49static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS];
50
51/**
52 * tty_register_ldisc - install a line discipline
53 * @disc: ldisc number
54 * @new_ldisc: pointer to the ldisc object
55 *
56 * Installs a new line discipline into the kernel. The discipline
57 * is set up as unreferenced and then made available to the kernel
58 * from this point onwards.
59 *
60 * Locking:
Peter Hurley137084b2013-06-15 07:04:46 -040061 * takes tty_ldiscs_lock to guard against ldisc races
Alan Cox01e1abb2008-07-22 11:16:55 +010062 */
63
64int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc)
65{
66 unsigned long flags;
67 int ret = 0;
68
69 if (disc < N_TTY || disc >= NR_LDISCS)
70 return -EINVAL;
71
Peter Hurley137084b2013-06-15 07:04:46 -040072 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
Alan Cox01e1abb2008-07-22 11:16:55 +010073 tty_ldiscs[disc] = new_ldisc;
74 new_ldisc->num = disc;
75 new_ldisc->refcount = 0;
Peter Hurley137084b2013-06-15 07:04:46 -040076 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
Alan Cox01e1abb2008-07-22 11:16:55 +010077
78 return ret;
79}
80EXPORT_SYMBOL(tty_register_ldisc);
81
82/**
83 * tty_unregister_ldisc - unload a line discipline
84 * @disc: ldisc number
85 * @new_ldisc: pointer to the ldisc object
86 *
87 * Remove a line discipline from the kernel providing it is not
88 * currently in use.
89 *
90 * Locking:
Peter Hurley137084b2013-06-15 07:04:46 -040091 * takes tty_ldiscs_lock to guard against ldisc races
Alan Cox01e1abb2008-07-22 11:16:55 +010092 */
93
94int tty_unregister_ldisc(int disc)
95{
96 unsigned long flags;
97 int ret = 0;
98
99 if (disc < N_TTY || disc >= NR_LDISCS)
100 return -EINVAL;
101
Peter Hurley137084b2013-06-15 07:04:46 -0400102 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
Alan Cox01e1abb2008-07-22 11:16:55 +0100103 if (tty_ldiscs[disc]->refcount)
104 ret = -EBUSY;
105 else
106 tty_ldiscs[disc] = NULL;
Peter Hurley137084b2013-06-15 07:04:46 -0400107 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
Alan Cox01e1abb2008-07-22 11:16:55 +0100108
109 return ret;
110}
111EXPORT_SYMBOL(tty_unregister_ldisc);
112
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700113static struct tty_ldisc_ops *get_ldops(int disc)
114{
115 unsigned long flags;
116 struct tty_ldisc_ops *ldops, *ret;
117
Peter Hurley137084b2013-06-15 07:04:46 -0400118 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700119 ret = ERR_PTR(-EINVAL);
120 ldops = tty_ldiscs[disc];
121 if (ldops) {
122 ret = ERR_PTR(-EAGAIN);
123 if (try_module_get(ldops->owner)) {
124 ldops->refcount++;
125 ret = ldops;
126 }
127 }
Peter Hurley137084b2013-06-15 07:04:46 -0400128 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700129 return ret;
130}
131
132static void put_ldops(struct tty_ldisc_ops *ldops)
133{
134 unsigned long flags;
135
Peter Hurley137084b2013-06-15 07:04:46 -0400136 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700137 ldops->refcount--;
138 module_put(ldops->owner);
Peter Hurley137084b2013-06-15 07:04:46 -0400139 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700140}
Alan Cox01e1abb2008-07-22 11:16:55 +0100141
142/**
Alan Cox01e1abb2008-07-22 11:16:55 +0100143 * tty_ldisc_get - take a reference to an ldisc
144 * @disc: ldisc number
Alan Cox01e1abb2008-07-22 11:16:55 +0100145 *
146 * Takes a reference to a line discipline. Deals with refcounts and
147 * module locking counts. Returns NULL if the discipline is not available.
148 * Returns a pointer to the discipline and bumps the ref count if it is
149 * available
150 *
151 * Locking:
Peter Hurley137084b2013-06-15 07:04:46 -0400152 * takes tty_ldiscs_lock to guard against ldisc races
Alan Cox01e1abb2008-07-22 11:16:55 +0100153 */
154
Peter Hurley36697522013-06-15 07:04:48 -0400155static struct tty_ldisc *tty_ldisc_get(struct tty_struct *tty, int disc)
Alan Cox01e1abb2008-07-22 11:16:55 +0100156{
Alan Coxc65c9bc2009-06-11 12:50:12 +0100157 struct tty_ldisc *ld;
Linus Torvalds182274f2009-08-03 16:01:28 -0700158 struct tty_ldisc_ops *ldops;
Alan Cox01e1abb2008-07-22 11:16:55 +0100159
160 if (disc < N_TTY || disc >= NR_LDISCS)
Alan Coxc65c9bc2009-06-11 12:50:12 +0100161 return ERR_PTR(-EINVAL);
Linus Torvalds182274f2009-08-03 16:01:28 -0700162
163 /*
164 * Get the ldisc ops - we may need to request them to be loaded
165 * dynamically and try again.
166 */
167 ldops = get_ldops(disc);
168 if (IS_ERR(ldops)) {
Alan Cox01e1abb2008-07-22 11:16:55 +0100169 request_module("tty-ldisc-%d", disc);
Linus Torvalds182274f2009-08-03 16:01:28 -0700170 ldops = get_ldops(disc);
171 if (IS_ERR(ldops))
172 return ERR_CAST(ldops);
Alan Cox01e1abb2008-07-22 11:16:55 +0100173 }
Linus Torvalds182274f2009-08-03 16:01:28 -0700174
175 ld = kmalloc(sizeof(struct tty_ldisc), GFP_KERNEL);
176 if (ld == NULL) {
177 put_ldops(ldops);
178 return ERR_PTR(-ENOMEM);
179 }
180
181 ld->ops = ldops;
Peter Hurley36697522013-06-15 07:04:48 -0400182 ld->tty = tty;
Ivo Sieben1541f842012-05-03 14:37:43 +0200183
Alan Coxc65c9bc2009-06-11 12:50:12 +0100184 return ld;
Alan Cox01e1abb2008-07-22 11:16:55 +0100185}
186
Peter Hurley734de242013-03-11 16:44:43 -0400187/**
188 * tty_ldisc_put - release the ldisc
189 *
190 * Complement of tty_ldisc_get().
191 */
192static inline void tty_ldisc_put(struct tty_ldisc *ld)
193{
Peter Hurley734de242013-03-11 16:44:43 -0400194 if (WARN_ON_ONCE(!ld))
195 return;
196
Peter Hurley36697522013-06-15 07:04:48 -0400197 put_ldops(ld->ops);
Peter Hurley734de242013-03-11 16:44:43 -0400198 kfree(ld);
Peter Hurley734de242013-03-11 16:44:43 -0400199}
200
Alan Cox852e99d2009-06-11 12:51:41 +0100201static void *tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos)
Alan Cox01e1abb2008-07-22 11:16:55 +0100202{
203 return (*pos < NR_LDISCS) ? pos : NULL;
204}
205
Alan Cox852e99d2009-06-11 12:51:41 +0100206static void *tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos)
Alan Cox01e1abb2008-07-22 11:16:55 +0100207{
208 (*pos)++;
209 return (*pos < NR_LDISCS) ? pos : NULL;
210}
211
212static void tty_ldiscs_seq_stop(struct seq_file *m, void *v)
213{
214}
215
216static int tty_ldiscs_seq_show(struct seq_file *m, void *v)
217{
218 int i = *(loff_t *)v;
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700219 struct tty_ldisc_ops *ldops;
Alan Cox852e99d2009-06-11 12:51:41 +0100220
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700221 ldops = get_ldops(i);
222 if (IS_ERR(ldops))
Alan Cox01e1abb2008-07-22 11:16:55 +0100223 return 0;
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700224 seq_printf(m, "%-10s %2d\n", ldops->name ? ldops->name : "???", i);
225 put_ldops(ldops);
Alan Cox01e1abb2008-07-22 11:16:55 +0100226 return 0;
227}
228
229static const struct seq_operations tty_ldiscs_seq_ops = {
230 .start = tty_ldiscs_seq_start,
231 .next = tty_ldiscs_seq_next,
232 .stop = tty_ldiscs_seq_stop,
233 .show = tty_ldiscs_seq_show,
234};
235
236static int proc_tty_ldiscs_open(struct inode *inode, struct file *file)
237{
238 return seq_open(file, &tty_ldiscs_seq_ops);
239}
240
241const struct file_operations tty_ldiscs_proc_fops = {
242 .owner = THIS_MODULE,
243 .open = proc_tty_ldiscs_open,
244 .read = seq_read,
245 .llseek = seq_lseek,
246 .release = seq_release,
247};
248
249/**
Alan Cox01e1abb2008-07-22 11:16:55 +0100250 * tty_ldisc_ref_wait - wait for the tty ldisc
251 * @tty: tty device
252 *
253 * Dereference the line discipline for the terminal and take a
254 * reference to it. If the line discipline is in flux then
255 * wait patiently until it changes.
256 *
257 * Note: Must not be called from an IRQ/timer context. The caller
258 * must also be careful not to hold other locks that will deadlock
259 * against a discipline change, such as an existing ldisc reference
260 * (which we check for)
261 *
Peter Hurley36697522013-06-15 07:04:48 -0400262 * Note: only callable from a file_operations routine (which
263 * guarantees tty->ldisc != NULL when the lock is acquired).
Alan Cox01e1abb2008-07-22 11:16:55 +0100264 */
265
266struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
267{
Peter Hurley36697522013-06-15 07:04:48 -0400268 ldsem_down_read(&tty->ldisc_sem, MAX_SCHEDULE_TIMEOUT);
269 WARN_ON(!tty->ldisc);
270 return tty->ldisc;
Alan Cox01e1abb2008-07-22 11:16:55 +0100271}
Alan Cox01e1abb2008-07-22 11:16:55 +0100272EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
273
274/**
275 * tty_ldisc_ref - get the tty ldisc
276 * @tty: tty device
277 *
278 * Dereference the line discipline for the terminal and take a
279 * reference to it. If the line discipline is in flux then
280 * return NULL. Can be called from IRQ and timer functions.
Alan Cox01e1abb2008-07-22 11:16:55 +0100281 */
282
283struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
284{
Peter Hurley36697522013-06-15 07:04:48 -0400285 struct tty_ldisc *ld = NULL;
286
287 if (ldsem_down_read_trylock(&tty->ldisc_sem)) {
288 ld = tty->ldisc;
289 if (!ld)
290 ldsem_up_read(&tty->ldisc_sem);
291 }
292 return ld;
Alan Cox01e1abb2008-07-22 11:16:55 +0100293}
Alan Cox01e1abb2008-07-22 11:16:55 +0100294EXPORT_SYMBOL_GPL(tty_ldisc_ref);
295
296/**
297 * tty_ldisc_deref - free a tty ldisc reference
298 * @ld: reference to free up
299 *
300 * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May
301 * be called in IRQ context.
Alan Cox01e1abb2008-07-22 11:16:55 +0100302 */
303
304void tty_ldisc_deref(struct tty_ldisc *ld)
305{
Peter Hurley36697522013-06-15 07:04:48 -0400306 ldsem_up_read(&ld->tty->ldisc_sem);
Alan Cox01e1abb2008-07-22 11:16:55 +0100307}
Alan Cox01e1abb2008-07-22 11:16:55 +0100308EXPORT_SYMBOL_GPL(tty_ldisc_deref);
309
Peter Hurleyd2c43892013-06-15 07:04:47 -0400310
311static inline int __lockfunc
312tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout)
313{
314 return ldsem_down_write(&tty->ldisc_sem, timeout);
315}
316
317static inline int __lockfunc
318tty_ldisc_lock_nested(struct tty_struct *tty, unsigned long timeout)
319{
320 return ldsem_down_write_nested(&tty->ldisc_sem,
321 LDISC_SEM_OTHER, timeout);
322}
323
324static inline void tty_ldisc_unlock(struct tty_struct *tty)
325{
326 return ldsem_up_write(&tty->ldisc_sem);
327}
328
329static int __lockfunc
330tty_ldisc_lock_pair_timeout(struct tty_struct *tty, struct tty_struct *tty2,
331 unsigned long timeout)
332{
333 int ret;
334
335 if (tty < tty2) {
336 ret = tty_ldisc_lock(tty, timeout);
337 if (ret) {
338 ret = tty_ldisc_lock_nested(tty2, timeout);
339 if (!ret)
340 tty_ldisc_unlock(tty);
341 }
342 } else {
343 /* if this is possible, it has lots of implications */
344 WARN_ON_ONCE(tty == tty2);
345 if (tty2 && tty != tty2) {
346 ret = tty_ldisc_lock(tty2, timeout);
347 if (ret) {
348 ret = tty_ldisc_lock_nested(tty, timeout);
349 if (!ret)
350 tty_ldisc_unlock(tty2);
351 }
352 } else
353 ret = tty_ldisc_lock(tty, timeout);
354 }
355
356 if (!ret)
357 return -EBUSY;
358
359 set_bit(TTY_LDISC_HALTED, &tty->flags);
360 if (tty2)
361 set_bit(TTY_LDISC_HALTED, &tty2->flags);
362 return 0;
363}
364
365static void __lockfunc
366tty_ldisc_lock_pair(struct tty_struct *tty, struct tty_struct *tty2)
367{
368 tty_ldisc_lock_pair_timeout(tty, tty2, MAX_SCHEDULE_TIMEOUT);
369}
370
371static void __lockfunc tty_ldisc_unlock_pair(struct tty_struct *tty,
372 struct tty_struct *tty2)
373{
374 tty_ldisc_unlock(tty);
375 if (tty2)
376 tty_ldisc_unlock(tty2);
377}
378
379static void __lockfunc tty_ldisc_enable_pair(struct tty_struct *tty,
380 struct tty_struct *tty2)
381{
382 clear_bit(TTY_LDISC_HALTED, &tty->flags);
383 if (tty2)
384 clear_bit(TTY_LDISC_HALTED, &tty2->flags);
385
386 tty_ldisc_unlock_pair(tty, tty2);
387}
388
Alan Cox01e1abb2008-07-22 11:16:55 +0100389/**
Alan Coxf2c4c652009-06-11 12:50:58 +0100390 * tty_ldisc_flush - flush line discipline queue
391 * @tty: tty
392 *
393 * Flush the line discipline queue (if any) for this tty. If there
394 * is no line discipline active this is a no-op.
395 */
396
397void tty_ldisc_flush(struct tty_struct *tty)
398{
399 struct tty_ldisc *ld = tty_ldisc_ref(tty);
400 if (ld) {
401 if (ld->ops->flush_buffer)
402 ld->ops->flush_buffer(tty);
403 tty_ldisc_deref(ld);
404 }
405 tty_buffer_flush(tty);
406}
Alan Coxf2c4c652009-06-11 12:50:58 +0100407EXPORT_SYMBOL_GPL(tty_ldisc_flush);
408
409/**
Alan Cox01e1abb2008-07-22 11:16:55 +0100410 * tty_set_termios_ldisc - set ldisc field
411 * @tty: tty structure
412 * @num: line discipline number
413 *
414 * This is probably overkill for real world processors but
415 * they are not on hot paths so a little discipline won't do
416 * any harm.
417 *
418 * Locking: takes termios_mutex
419 */
420
421static void tty_set_termios_ldisc(struct tty_struct *tty, int num)
422{
423 mutex_lock(&tty->termios_mutex);
Alan Coxadc8d742012-07-14 15:31:47 +0100424 tty->termios.c_line = num;
Alan Cox01e1abb2008-07-22 11:16:55 +0100425 mutex_unlock(&tty->termios_mutex);
426}
427
Alan Coxc65c9bc2009-06-11 12:50:12 +0100428/**
429 * tty_ldisc_open - open a line discipline
430 * @tty: tty we are opening the ldisc on
431 * @ld: discipline to open
432 *
433 * A helper opening method. Also a convenient debugging and check
434 * point.
Arnd Bergmannec79d602010-06-01 22:53:01 +0200435 *
436 * Locking: always called with BTM already held.
Alan Coxc65c9bc2009-06-11 12:50:12 +0100437 */
438
439static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld)
440{
441 WARN_ON(test_and_set_bit(TTY_LDISC_OPEN, &tty->flags));
Alan Coxf18f9492009-11-30 13:18:35 +0000442 if (ld->ops->open) {
443 int ret;
Arnd Bergmannec79d602010-06-01 22:53:01 +0200444 /* BTM here locks versus a hangup event */
Alan Coxf18f9492009-11-30 13:18:35 +0000445 ret = ld->ops->open(tty);
Jiri Slaby7f90cfc2010-11-25 00:27:54 +0100446 if (ret)
447 clear_bit(TTY_LDISC_OPEN, &tty->flags);
Alan Coxf18f9492009-11-30 13:18:35 +0000448 return ret;
449 }
Alan Coxc65c9bc2009-06-11 12:50:12 +0100450 return 0;
451}
452
453/**
454 * tty_ldisc_close - close a line discipline
455 * @tty: tty we are opening the ldisc on
456 * @ld: discipline to close
457 *
458 * A helper close method. Also a convenient debugging and check
459 * point.
460 */
461
462static void tty_ldisc_close(struct tty_struct *tty, struct tty_ldisc *ld)
463{
464 WARN_ON(!test_bit(TTY_LDISC_OPEN, &tty->flags));
465 clear_bit(TTY_LDISC_OPEN, &tty->flags);
466 if (ld->ops->close)
467 ld->ops->close(tty);
468}
Alan Cox01e1abb2008-07-22 11:16:55 +0100469
470/**
471 * tty_ldisc_restore - helper for tty ldisc change
472 * @tty: tty to recover
473 * @old: previous ldisc
474 *
475 * Restore the previous line discipline or N_TTY when a line discipline
476 * change fails due to an open error
477 */
478
479static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old)
480{
481 char buf[64];
Alan Coxc65c9bc2009-06-11 12:50:12 +0100482 struct tty_ldisc *new_ldisc;
483 int r;
Alan Cox01e1abb2008-07-22 11:16:55 +0100484
485 /* There is an outstanding reference here so this is safe */
Peter Hurley36697522013-06-15 07:04:48 -0400486 old = tty_ldisc_get(tty, old->ops->num);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100487 WARN_ON(IS_ERR(old));
Peter Hurleyf48070452013-03-11 16:44:42 -0400488 tty->ldisc = old;
Alan Cox01e1abb2008-07-22 11:16:55 +0100489 tty_set_termios_ldisc(tty, old->ops->num);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100490 if (tty_ldisc_open(tty, old) < 0) {
491 tty_ldisc_put(old);
Alan Cox01e1abb2008-07-22 11:16:55 +0100492 /* This driver is always present */
Peter Hurley36697522013-06-15 07:04:48 -0400493 new_ldisc = tty_ldisc_get(tty, N_TTY);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100494 if (IS_ERR(new_ldisc))
Alan Cox01e1abb2008-07-22 11:16:55 +0100495 panic("n_tty: get");
Peter Hurleyf48070452013-03-11 16:44:42 -0400496 tty->ldisc = new_ldisc;
Alan Cox01e1abb2008-07-22 11:16:55 +0100497 tty_set_termios_ldisc(tty, N_TTY);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100498 r = tty_ldisc_open(tty, new_ldisc);
499 if (r < 0)
500 panic("Couldn't open N_TTY ldisc for "
501 "%s --- error %d.",
502 tty_name(tty, buf), r);
Alan Cox01e1abb2008-07-22 11:16:55 +0100503 }
504}
505
506/**
507 * tty_set_ldisc - set line discipline
508 * @tty: the terminal to set
509 * @ldisc: the line discipline
510 *
511 * Set the discipline of a tty line. Must be called from a process
Alan Coxc65c9bc2009-06-11 12:50:12 +0100512 * context. The ldisc change logic has to protect itself against any
513 * overlapping ldisc change (including on the other end of pty pairs),
514 * the close of one side of a tty/pty pair, and eventually hangup.
Alan Cox01e1abb2008-07-22 11:16:55 +0100515 */
516
517int tty_set_ldisc(struct tty_struct *tty, int ldisc)
518{
519 int retval;
Peter Hurley9fbfa342013-06-15 07:04:49 -0400520 struct tty_ldisc *old_ldisc, *new_ldisc;
Peter Hurley36697522013-06-15 07:04:48 -0400521 struct tty_struct *o_tty = tty->link;
Alan Cox01e1abb2008-07-22 11:16:55 +0100522
Peter Hurley36697522013-06-15 07:04:48 -0400523 new_ldisc = tty_ldisc_get(tty, ldisc);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100524 if (IS_ERR(new_ldisc))
525 return PTR_ERR(new_ldisc);
526
Peter Hurley36697522013-06-15 07:04:48 -0400527 retval = tty_ldisc_lock_pair_timeout(tty, o_tty, 5 * HZ);
528 if (retval) {
529 tty_ldisc_put(new_ldisc);
530 return retval;
531 }
Alan Coxc65c9bc2009-06-11 12:50:12 +0100532
533 /*
534 * Check the no-op case
535 */
536
537 if (tty->ldisc->ops->num == ldisc) {
Peter Hurley36697522013-06-15 07:04:48 -0400538 tty_ldisc_enable_pair(tty, o_tty);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100539 tty_ldisc_put(new_ldisc);
540 return 0;
541 }
Alan Cox01e1abb2008-07-22 11:16:55 +0100542
Peter Hurley36697522013-06-15 07:04:48 -0400543 /* FIXME: why 'shutoff' input if the ldisc is locked? */
Alan Cox01e1abb2008-07-22 11:16:55 +0100544 tty->receive_room = 0;
545
Peter Hurley9fbfa342013-06-15 07:04:49 -0400546 old_ldisc = tty->ldisc;
Alan Cox89c8d912012-08-08 16:30:13 +0100547 tty_lock(tty);
Jiri Slaby100eeae2010-10-31 23:17:51 +0100548
Peter Hurleye97733c2013-06-15 07:04:50 -0400549 if (test_bit(TTY_HUPPING, &tty->flags) ||
550 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 Hurley36697522013-06-15 07:04:48 -0400553 tty_ldisc_enable_pair(tty, o_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
Alan Cox01e1abb2008-07-22 11:16:55 +0100573 /* At this point we hold a reference to the new ldisc and a
574 a reference to the old ldisc. If we ended up flipping back
575 to the existing ldisc we have two references to it */
576
Peter Hurley9fbfa342013-06-15 07:04:49 -0400577 if (tty->ldisc->ops->num != old_ldisc->ops->num && tty->ops->set_ldisc)
Alan Cox01e1abb2008-07-22 11:16:55 +0100578 tty->ops->set_ldisc(tty);
579
Peter Hurley9fbfa342013-06-15 07:04:49 -0400580 tty_ldisc_put(old_ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100581
582 /*
Alan Coxc65c9bc2009-06-11 12:50:12 +0100583 * Allow ldisc referencing to occur again
Alan Cox01e1abb2008-07-22 11:16:55 +0100584 */
Peter Hurley36697522013-06-15 07:04:48 -0400585 tty_ldisc_enable_pair(tty, o_tty);
Alan Cox01e1abb2008-07-22 11:16:55 +0100586
Alan Coxc65c9bc2009-06-11 12:50:12 +0100587 /* Restart the work queue in case no characters kick it off. Safe if
Alan Cox01e1abb2008-07-22 11:16:55 +0100588 already running */
Peter Hurley4f98d462013-03-11 16:44:34 -0400589 schedule_work(&tty->port->buf.work);
590 if (o_tty)
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200591 schedule_work(&o_tty->port->buf.work);
Peter Hurley4f98d462013-03-11 16:44:34 -0400592
Alan Cox89c8d912012-08-08 16:30:13 +0100593 tty_unlock(tty);
Alan Cox01e1abb2008-07-22 11:16:55 +0100594 return retval;
595}
596
Alan Coxc65c9bc2009-06-11 12:50:12 +0100597/**
598 * tty_reset_termios - reset terminal state
599 * @tty: tty to reset
600 *
601 * Restore a terminal to the driver default state.
602 */
603
604static void tty_reset_termios(struct tty_struct *tty)
605{
606 mutex_lock(&tty->termios_mutex);
Alan Coxadc8d742012-07-14 15:31:47 +0100607 tty->termios = tty->driver->init_termios;
608 tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios);
609 tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100610 mutex_unlock(&tty->termios_mutex);
611}
612
613
614/**
615 * tty_ldisc_reinit - reinitialise the tty ldisc
616 * @tty: tty to reinit
Alan Cox638b9642010-02-08 10:09:26 +0000617 * @ldisc: line discipline to reinitialize
Alan Coxc65c9bc2009-06-11 12:50:12 +0100618 *
Alan Cox638b9642010-02-08 10:09:26 +0000619 * Switch the tty to a line discipline and leave the ldisc
620 * state closed
Alan Coxc65c9bc2009-06-11 12:50:12 +0100621 */
622
Philippe Rétornaz1c95ba12010-10-27 17:13:21 +0200623static int tty_ldisc_reinit(struct tty_struct *tty, int ldisc)
Alan Coxc65c9bc2009-06-11 12:50:12 +0100624{
Peter Hurley36697522013-06-15 07:04:48 -0400625 struct tty_ldisc *ld = tty_ldisc_get(tty, ldisc);
Philippe Rétornaz1c95ba12010-10-27 17:13:21 +0200626
627 if (IS_ERR(ld))
628 return -1;
Alan Coxc65c9bc2009-06-11 12:50:12 +0100629
630 tty_ldisc_close(tty, tty->ldisc);
631 tty_ldisc_put(tty->ldisc);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100632 /*
633 * Switch the line discipline back
634 */
Peter Hurleyf48070452013-03-11 16:44:42 -0400635 tty->ldisc = ld;
Alan Cox638b9642010-02-08 10:09:26 +0000636 tty_set_termios_ldisc(tty, ldisc);
Philippe Rétornaz1c95ba12010-10-27 17:13:21 +0200637
638 return 0;
Alan Coxc65c9bc2009-06-11 12:50:12 +0100639}
640
641/**
642 * tty_ldisc_hangup - hangup ldisc reset
643 * @tty: tty being hung up
644 *
645 * Some tty devices reset their termios when they receive a hangup
646 * event. In that situation we must also switch back to N_TTY properly
647 * before we reset the termios data.
648 *
649 * Locking: We can take the ldisc mutex as the rest of the code is
650 * careful to allow for this.
651 *
652 * In the pty pair case this occurs in the close() path of the
653 * tty itself so we must be careful about locking rules.
654 */
655
656void tty_ldisc_hangup(struct tty_struct *tty)
657{
658 struct tty_ldisc *ld;
Alan Cox638b9642010-02-08 10:09:26 +0000659 int reset = tty->driver->flags & TTY_DRIVER_RESET_TERMIOS;
660 int err = 0;
Alan Coxc65c9bc2009-06-11 12:50:12 +0100661
Peter Hurleyfc575ee2013-03-11 16:44:38 -0400662 tty_ldisc_debug(tty, "closing ldisc: %p\n", tty->ldisc);
663
Alan Coxc65c9bc2009-06-11 12:50:12 +0100664 ld = tty_ldisc_ref(tty);
665 if (ld != NULL) {
Alan Coxc65c9bc2009-06-11 12:50:12 +0100666 if (ld->ops->flush_buffer)
667 ld->ops->flush_buffer(tty);
668 tty_driver_flush_buffer(tty);
669 if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
670 ld->ops->write_wakeup)
671 ld->ops->write_wakeup(tty);
672 if (ld->ops->hangup)
673 ld->ops->hangup(tty);
674 tty_ldisc_deref(ld);
675 }
Peter Hurley36697522013-06-15 07:04:48 -0400676
Alan Coxc65c9bc2009-06-11 12:50:12 +0100677 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
678 wake_up_interruptible_poll(&tty->read_wait, POLLIN);
Peter Hurley36697522013-06-15 07:04:48 -0400679
680 tty_unlock(tty);
681
Alan Coxc65c9bc2009-06-11 12:50:12 +0100682 /*
683 * Shutdown the current line discipline, and reset it to
Alan Cox638b9642010-02-08 10:09:26 +0000684 * N_TTY if need be.
685 *
686 * Avoid racing set_ldisc or tty_ldisc_release
Alan Coxc65c9bc2009-06-11 12:50:12 +0100687 */
Peter Hurley36697522013-06-15 07:04:48 -0400688 tty_ldisc_lock_pair(tty, tty->link);
689 tty_lock(tty);
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 Hurley36697522013-06-15 07:04:48 -0400711 tty_ldisc_enable_pair(tty, tty->link);
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
763 * @tty: tty being shut down
764 * @o_tty: pair tty for pty/tty pairs
765 *
Alan Cox852e99d2009-06-11 12:51:41 +0100766 * Called during the final close of a tty/pty pair in order to shut down
767 * the line discpline layer. On exit the ldisc assigned is N_TTY and the
Alan Coxc65c9bc2009-06-11 12:50:12 +0100768 * ldisc has not been opened.
Alan Cox01e1abb2008-07-22 11:16:55 +0100769 */
770
771void tty_ldisc_release(struct tty_struct *tty, struct tty_struct *o_tty)
772{
Alan Cox01e1abb2008-07-22 11:16:55 +0100773 /*
Peter Hurleya2965b72013-03-11 16:44:35 -0400774 * Shutdown this line discipline. As this is the final close,
775 * it does not race with the set_ldisc code path.
Alan Cox01e1abb2008-07-22 11:16:55 +0100776 */
Alan Cox01e1abb2008-07-22 11:16:55 +0100777
Peter Hurleyfc575ee2013-03-11 16:44:38 -0400778 tty_ldisc_debug(tty, "closing ldisc: %p\n", tty->ldisc);
779
Peter Hurley36697522013-06-15 07:04:48 -0400780 tty_ldisc_lock_pair(tty, o_tty);
Sebastian Andrzej Siewior852e4a82012-12-25 23:02:48 +0100781 tty_lock_pair(tty, o_tty);
Peter Hurley36697522013-06-15 07:04:48 -0400782
Alan Cox89c8d912012-08-08 16:30:13 +0100783 tty_ldisc_kill(tty);
Alan Cox89c8d912012-08-08 16:30:13 +0100784 if (o_tty)
785 tty_ldisc_kill(o_tty);
786
787 tty_unlock_pair(tty, o_tty);
Peter Hurley36697522013-06-15 07:04:48 -0400788 tty_ldisc_unlock_pair(tty, o_tty);
789
Alan Coxaef29bc2009-06-29 15:21:47 +0100790 /* And the memory resources remaining (buffers, termios) will be
791 disposed of when the kref hits zero */
Peter Hurleyfc575ee2013-03-11 16:44:38 -0400792
793 tty_ldisc_debug(tty, "ldisc closed\n");
Alan Cox01e1abb2008-07-22 11:16:55 +0100794}
795
796/**
797 * tty_ldisc_init - ldisc setup for new tty
798 * @tty: tty being allocated
799 *
800 * Set up the line discipline objects for a newly allocated tty. Note that
801 * the tty structure is not completely set up when this call is made.
802 */
803
804void tty_ldisc_init(struct tty_struct *tty)
805{
Peter Hurley36697522013-06-15 07:04:48 -0400806 struct tty_ldisc *ld = tty_ldisc_get(tty, N_TTY);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100807 if (IS_ERR(ld))
Alan Cox01e1abb2008-07-22 11:16:55 +0100808 panic("n_tty: init_tty");
Peter Hurleyf48070452013-03-11 16:44:42 -0400809 tty->ldisc = ld;
Alan Cox01e1abb2008-07-22 11:16:55 +0100810}
811
Jiri Slaby67166712011-03-23 10:48:35 +0100812/**
813 * tty_ldisc_init - ldisc cleanup for new tty
814 * @tty: tty that was allocated recently
815 *
816 * The tty structure must not becompletely set up (tty_ldisc_setup) when
817 * this call is made.
818 */
819void tty_ldisc_deinit(struct tty_struct *tty)
820{
Peter Hurleyebc9bae2013-03-11 16:44:40 -0400821 tty_ldisc_put(tty->ldisc);
Peter Hurleyf48070452013-03-11 16:44:42 -0400822 tty->ldisc = NULL;
Jiri Slaby67166712011-03-23 10:48:35 +0100823}
824
Alan Cox01e1abb2008-07-22 11:16:55 +0100825void tty_ldisc_begin(void)
826{
827 /* Setup the default TTY line discipline. */
828 (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
829}