tty: Wait for SAK work before waiting for hangup work
[firefly-linux-kernel-4.4.55.git] / drivers / tty / tty_ldisc.c
1 #include <linux/types.h>
2 #include <linux/errno.h>
3 #include <linux/kmod.h>
4 #include <linux/sched.h>
5 #include <linux/interrupt.h>
6 #include <linux/tty.h>
7 #include <linux/tty_driver.h>
8 #include <linux/file.h>
9 #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>
16 #include <linux/device.h>
17 #include <linux/wait.h>
18 #include <linux/bitops.h>
19 #include <linux/seq_file.h>
20 #include <linux/uaccess.h>
21 #include <linux/ratelimit.h>
22
23 /*
24  *      This guards the refcounted line discipline lists. The lock
25  *      must be taken with irqs off because there are hangup path
26  *      callers who will do ldisc lookups and cannot sleep.
27  */
28
29 static DEFINE_RAW_SPINLOCK(tty_ldisc_lock);
30 static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_wait);
31 /* Line disc dispatch table */
32 static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS];
33
34 static inline struct tty_ldisc *get_ldisc(struct tty_ldisc *ld)
35 {
36         if (ld)
37                 atomic_inc(&ld->users);
38         return ld;
39 }
40
41 static void put_ldisc(struct tty_ldisc *ld)
42 {
43         unsigned long flags;
44
45         if (WARN_ON_ONCE(!ld))
46                 return;
47
48         /*
49          * If this is the last user, free the ldisc, and
50          * release the ldisc ops.
51          *
52          * We really want an "atomic_dec_and_raw_lock_irqsave()",
53          * but we don't have it, so this does it by hand.
54          */
55         raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
56         if (atomic_dec_and_test(&ld->users)) {
57                 struct tty_ldisc_ops *ldo = ld->ops;
58
59                 ldo->refcount--;
60                 module_put(ldo->owner);
61                 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
62
63                 kfree(ld);
64                 return;
65         }
66         raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
67
68         if (waitqueue_active(&ld->wq_idle))
69                 wake_up(&ld->wq_idle);
70 }
71
72 /**
73  *      tty_register_ldisc      -       install a line discipline
74  *      @disc: ldisc number
75  *      @new_ldisc: pointer to the ldisc object
76  *
77  *      Installs a new line discipline into the kernel. The discipline
78  *      is set up as unreferenced and then made available to the kernel
79  *      from this point onwards.
80  *
81  *      Locking:
82  *              takes tty_ldisc_lock to guard against ldisc races
83  */
84
85 int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc)
86 {
87         unsigned long flags;
88         int ret = 0;
89
90         if (disc < N_TTY || disc >= NR_LDISCS)
91                 return -EINVAL;
92
93         raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
94         tty_ldiscs[disc] = new_ldisc;
95         new_ldisc->num = disc;
96         new_ldisc->refcount = 0;
97         raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
98
99         return ret;
100 }
101 EXPORT_SYMBOL(tty_register_ldisc);
102
103 /**
104  *      tty_unregister_ldisc    -       unload a line discipline
105  *      @disc: ldisc number
106  *      @new_ldisc: pointer to the ldisc object
107  *
108  *      Remove a line discipline from the kernel providing it is not
109  *      currently in use.
110  *
111  *      Locking:
112  *              takes tty_ldisc_lock to guard against ldisc races
113  */
114
115 int tty_unregister_ldisc(int disc)
116 {
117         unsigned long flags;
118         int ret = 0;
119
120         if (disc < N_TTY || disc >= NR_LDISCS)
121                 return -EINVAL;
122
123         raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
124         if (tty_ldiscs[disc]->refcount)
125                 ret = -EBUSY;
126         else
127                 tty_ldiscs[disc] = NULL;
128         raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
129
130         return ret;
131 }
132 EXPORT_SYMBOL(tty_unregister_ldisc);
133
134 static struct tty_ldisc_ops *get_ldops(int disc)
135 {
136         unsigned long flags;
137         struct tty_ldisc_ops *ldops, *ret;
138
139         raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
140         ret = ERR_PTR(-EINVAL);
141         ldops = tty_ldiscs[disc];
142         if (ldops) {
143                 ret = ERR_PTR(-EAGAIN);
144                 if (try_module_get(ldops->owner)) {
145                         ldops->refcount++;
146                         ret = ldops;
147                 }
148         }
149         raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
150         return ret;
151 }
152
153 static void put_ldops(struct tty_ldisc_ops *ldops)
154 {
155         unsigned long flags;
156
157         raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
158         ldops->refcount--;
159         module_put(ldops->owner);
160         raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
161 }
162
163 /**
164  *      tty_ldisc_get           -       take a reference to an ldisc
165  *      @disc: ldisc number
166  *
167  *      Takes a reference to a line discipline. Deals with refcounts and
168  *      module locking counts. Returns NULL if the discipline is not available.
169  *      Returns a pointer to the discipline and bumps the ref count if it is
170  *      available
171  *
172  *      Locking:
173  *              takes tty_ldisc_lock to guard against ldisc races
174  */
175
176 static struct tty_ldisc *tty_ldisc_get(int disc)
177 {
178         struct tty_ldisc *ld;
179         struct tty_ldisc_ops *ldops;
180
181         if (disc < N_TTY || disc >= NR_LDISCS)
182                 return ERR_PTR(-EINVAL);
183
184         /*
185          * Get the ldisc ops - we may need to request them to be loaded
186          * dynamically and try again.
187          */
188         ldops = get_ldops(disc);
189         if (IS_ERR(ldops)) {
190                 request_module("tty-ldisc-%d", disc);
191                 ldops = get_ldops(disc);
192                 if (IS_ERR(ldops))
193                         return ERR_CAST(ldops);
194         }
195
196         ld = kmalloc(sizeof(struct tty_ldisc), GFP_KERNEL);
197         if (ld == NULL) {
198                 put_ldops(ldops);
199                 return ERR_PTR(-ENOMEM);
200         }
201
202         ld->ops = ldops;
203         atomic_set(&ld->users, 1);
204         init_waitqueue_head(&ld->wq_idle);
205
206         return ld;
207 }
208
209 static void *tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos)
210 {
211         return (*pos < NR_LDISCS) ? pos : NULL;
212 }
213
214 static void *tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos)
215 {
216         (*pos)++;
217         return (*pos < NR_LDISCS) ? pos : NULL;
218 }
219
220 static void tty_ldiscs_seq_stop(struct seq_file *m, void *v)
221 {
222 }
223
224 static int tty_ldiscs_seq_show(struct seq_file *m, void *v)
225 {
226         int i = *(loff_t *)v;
227         struct tty_ldisc_ops *ldops;
228
229         ldops = get_ldops(i);
230         if (IS_ERR(ldops))
231                 return 0;
232         seq_printf(m, "%-10s %2d\n", ldops->name ? ldops->name : "???", i);
233         put_ldops(ldops);
234         return 0;
235 }
236
237 static const struct seq_operations tty_ldiscs_seq_ops = {
238         .start  = tty_ldiscs_seq_start,
239         .next   = tty_ldiscs_seq_next,
240         .stop   = tty_ldiscs_seq_stop,
241         .show   = tty_ldiscs_seq_show,
242 };
243
244 static int proc_tty_ldiscs_open(struct inode *inode, struct file *file)
245 {
246         return seq_open(file, &tty_ldiscs_seq_ops);
247 }
248
249 const struct file_operations tty_ldiscs_proc_fops = {
250         .owner          = THIS_MODULE,
251         .open           = proc_tty_ldiscs_open,
252         .read           = seq_read,
253         .llseek         = seq_lseek,
254         .release        = seq_release,
255 };
256
257 /**
258  *      tty_ldisc_assign        -       set ldisc on a tty
259  *      @tty: tty to assign
260  *      @ld: line discipline
261  *
262  *      Install an instance of a line discipline into a tty structure. The
263  *      ldisc must have a reference count above zero to ensure it remains.
264  *      The tty instance refcount starts at zero.
265  *
266  *      Locking:
267  *              Caller must hold references
268  */
269
270 static void tty_ldisc_assign(struct tty_struct *tty, struct tty_ldisc *ld)
271 {
272         tty->ldisc = ld;
273 }
274
275 /**
276  *      tty_ldisc_try           -       internal helper
277  *      @tty: the tty
278  *
279  *      Make a single attempt to grab and bump the refcount on
280  *      the tty ldisc. Return 0 on failure or 1 on success. This is
281  *      used to implement both the waiting and non waiting versions
282  *      of tty_ldisc_ref
283  *
284  *      Locking: takes tty_ldisc_lock
285  */
286
287 static struct tty_ldisc *tty_ldisc_try(struct tty_struct *tty)
288 {
289         unsigned long flags;
290         struct tty_ldisc *ld;
291
292         raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
293         ld = NULL;
294         if (test_bit(TTY_LDISC, &tty->flags))
295                 ld = get_ldisc(tty->ldisc);
296         raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
297         return ld;
298 }
299
300 /**
301  *      tty_ldisc_ref_wait      -       wait for the tty ldisc
302  *      @tty: tty device
303  *
304  *      Dereference the line discipline for the terminal and take a
305  *      reference to it. If the line discipline is in flux then
306  *      wait patiently until it changes.
307  *
308  *      Note: Must not be called from an IRQ/timer context. The caller
309  *      must also be careful not to hold other locks that will deadlock
310  *      against a discipline change, such as an existing ldisc reference
311  *      (which we check for)
312  *
313  *      Locking: call functions take tty_ldisc_lock
314  */
315
316 struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
317 {
318         struct tty_ldisc *ld;
319
320         /* wait_event is a macro */
321         wait_event(tty_ldisc_wait, (ld = tty_ldisc_try(tty)) != NULL);
322         return ld;
323 }
324 EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
325
326 /**
327  *      tty_ldisc_ref           -       get the tty ldisc
328  *      @tty: tty device
329  *
330  *      Dereference the line discipline for the terminal and take a
331  *      reference to it. If the line discipline is in flux then
332  *      return NULL. Can be called from IRQ and timer functions.
333  *
334  *      Locking: called functions take tty_ldisc_lock
335  */
336
337 struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
338 {
339         return tty_ldisc_try(tty);
340 }
341 EXPORT_SYMBOL_GPL(tty_ldisc_ref);
342
343 /**
344  *      tty_ldisc_deref         -       free a tty ldisc reference
345  *      @ld: reference to free up
346  *
347  *      Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May
348  *      be called in IRQ context.
349  *
350  *      Locking: takes tty_ldisc_lock
351  */
352
353 void tty_ldisc_deref(struct tty_ldisc *ld)
354 {
355         put_ldisc(ld);
356 }
357 EXPORT_SYMBOL_GPL(tty_ldisc_deref);
358
359 static inline void tty_ldisc_put(struct tty_ldisc *ld)
360 {
361         put_ldisc(ld);
362 }
363
364 /**
365  *      tty_ldisc_enable        -       allow ldisc use
366  *      @tty: terminal to activate ldisc on
367  *
368  *      Set the TTY_LDISC flag when the line discipline can be called
369  *      again. Do necessary wakeups for existing sleepers. Clear the LDISC
370  *      changing flag to indicate any ldisc change is now over.
371  *
372  *      Note: nobody should set the TTY_LDISC bit except via this function.
373  *      Clearing directly is allowed.
374  */
375
376 void tty_ldisc_enable(struct tty_struct *tty)
377 {
378         clear_bit(TTY_LDISC_HALTED, &tty->flags);
379         set_bit(TTY_LDISC, &tty->flags);
380         clear_bit(TTY_LDISC_CHANGING, &tty->flags);
381         wake_up(&tty_ldisc_wait);
382 }
383
384 /**
385  *      tty_ldisc_flush -       flush line discipline queue
386  *      @tty: tty
387  *
388  *      Flush the line discipline queue (if any) for this tty. If there
389  *      is no line discipline active this is a no-op.
390  */
391
392 void tty_ldisc_flush(struct tty_struct *tty)
393 {
394         struct tty_ldisc *ld = tty_ldisc_ref(tty);
395         if (ld) {
396                 if (ld->ops->flush_buffer)
397                         ld->ops->flush_buffer(tty);
398                 tty_ldisc_deref(ld);
399         }
400         tty_buffer_flush(tty);
401 }
402 EXPORT_SYMBOL_GPL(tty_ldisc_flush);
403
404 /**
405  *      tty_set_termios_ldisc           -       set ldisc field
406  *      @tty: tty structure
407  *      @num: line discipline number
408  *
409  *      This is probably overkill for real world processors but
410  *      they are not on hot paths so a little discipline won't do
411  *      any harm.
412  *
413  *      Locking: takes termios_mutex
414  */
415
416 static void tty_set_termios_ldisc(struct tty_struct *tty, int num)
417 {
418         mutex_lock(&tty->termios_mutex);
419         tty->termios.c_line = num;
420         mutex_unlock(&tty->termios_mutex);
421 }
422
423 /**
424  *      tty_ldisc_open          -       open a line discipline
425  *      @tty: tty we are opening the ldisc on
426  *      @ld: discipline to open
427  *
428  *      A helper opening method. Also a convenient debugging and check
429  *      point.
430  *
431  *      Locking: always called with BTM already held.
432  */
433
434 static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld)
435 {
436         WARN_ON(test_and_set_bit(TTY_LDISC_OPEN, &tty->flags));
437         if (ld->ops->open) {
438                 int ret;
439                 /* BTM here locks versus a hangup event */
440                 ret = ld->ops->open(tty);
441                 if (ret)
442                         clear_bit(TTY_LDISC_OPEN, &tty->flags);
443                 return ret;
444         }
445         return 0;
446 }
447
448 /**
449  *      tty_ldisc_close         -       close a line discipline
450  *      @tty: tty we are opening the ldisc on
451  *      @ld: discipline to close
452  *
453  *      A helper close method. Also a convenient debugging and check
454  *      point.
455  */
456
457 static void tty_ldisc_close(struct tty_struct *tty, struct tty_ldisc *ld)
458 {
459         WARN_ON(!test_bit(TTY_LDISC_OPEN, &tty->flags));
460         clear_bit(TTY_LDISC_OPEN, &tty->flags);
461         if (ld->ops->close)
462                 ld->ops->close(tty);
463 }
464
465 /**
466  *      tty_ldisc_restore       -       helper for tty ldisc change
467  *      @tty: tty to recover
468  *      @old: previous ldisc
469  *
470  *      Restore the previous line discipline or N_TTY when a line discipline
471  *      change fails due to an open error
472  */
473
474 static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old)
475 {
476         char buf[64];
477         struct tty_ldisc *new_ldisc;
478         int r;
479
480         /* There is an outstanding reference here so this is safe */
481         old = tty_ldisc_get(old->ops->num);
482         WARN_ON(IS_ERR(old));
483         tty_ldisc_assign(tty, old);
484         tty_set_termios_ldisc(tty, old->ops->num);
485         if (tty_ldisc_open(tty, old) < 0) {
486                 tty_ldisc_put(old);
487                 /* This driver is always present */
488                 new_ldisc = tty_ldisc_get(N_TTY);
489                 if (IS_ERR(new_ldisc))
490                         panic("n_tty: get");
491                 tty_ldisc_assign(tty, new_ldisc);
492                 tty_set_termios_ldisc(tty, N_TTY);
493                 r = tty_ldisc_open(tty, new_ldisc);
494                 if (r < 0)
495                         panic("Couldn't open N_TTY ldisc for "
496                               "%s --- error %d.",
497                               tty_name(tty, buf), r);
498         }
499 }
500
501 /**
502  *      tty_ldisc_flush_works   -       flush all works of a tty
503  *      @tty: tty device to flush works for
504  *
505  *      Sync flush all works belonging to @tty.
506  */
507 static void tty_ldisc_flush_works(struct tty_struct *tty)
508 {
509         flush_work(&tty->SAK_work);
510         flush_work(&tty->hangup_work);
511         flush_work(&tty->port->buf.work);
512 }
513
514 /**
515  *      tty_ldisc_wait_idle     -       wait for the ldisc to become idle
516  *      @tty: tty to wait for
517  *      @timeout: for how long to wait at most
518  *
519  *      Wait for the line discipline to become idle. The discipline must
520  *      have been halted for this to guarantee it remains idle.
521  */
522 static int tty_ldisc_wait_idle(struct tty_struct *tty, long timeout)
523 {
524         long ret;
525         ret = wait_event_timeout(tty->ldisc->wq_idle,
526                         atomic_read(&tty->ldisc->users) == 1, timeout);
527         return ret > 0 ? 0 : -EBUSY;
528 }
529
530 /**
531  *      tty_ldisc_halt          -       shut down the line discipline
532  *      @tty: tty device
533  *      @o_tty: paired pty device (can be NULL)
534  *      @pending: returns true if work was scheduled when cancelled
535  *                (can be set to NULL)
536  *      @o_pending: returns true if work was scheduled when cancelled
537  *                  (can be set to NULL)
538  *      @timeout: # of jiffies to wait for ldisc refs to be released
539  *
540  *      Shut down the line discipline and work queue for this tty device and
541  *      its paired pty (if exists). Clearing the TTY_LDISC flag ensures
542  *      no further references can be obtained while the work queue halt
543  *      ensures that no more data is fed to the ldisc.
544  *
545  *      Furthermore, guarantee that existing ldisc references have been
546  *      released, which in turn, guarantees that no future buffer work
547  *      can be rescheduled.
548  *
549  *      You need to do a 'flush_scheduled_work()' (outside the ldisc_mutex)
550  *      in order to make sure any currently executing ldisc work is also
551  *      flushed.
552  */
553
554 static int tty_ldisc_halt(struct tty_struct *tty, struct tty_struct *o_tty,
555                           int *pending, int *o_pending, long timeout)
556 {
557         int scheduled, o_scheduled, retval;
558
559         clear_bit(TTY_LDISC, &tty->flags);
560         if (o_tty)
561                 clear_bit(TTY_LDISC, &o_tty->flags);
562
563         retval = tty_ldisc_wait_idle(tty, timeout);
564         if (!retval && o_tty)
565                 retval = tty_ldisc_wait_idle(o_tty, timeout);
566         if (retval)
567                 return retval;
568
569         scheduled = cancel_work_sync(&tty->port->buf.work);
570         set_bit(TTY_LDISC_HALTED, &tty->flags);
571         if (o_tty) {
572                 o_scheduled = cancel_work_sync(&o_tty->port->buf.work);
573                 set_bit(TTY_LDISC_HALTED, &o_tty->flags);
574         }
575
576         if (pending)
577                 *pending = scheduled;
578         if (o_tty && o_pending)
579                 *o_pending = o_scheduled;
580         return 0;
581 }
582
583 /**
584  *      tty_ldisc_hangup_halt - halt the line discipline for hangup
585  *      @tty: tty being hung up
586  *
587  *      Shut down the line discipline and work queue for the tty device
588  *      being hungup. Clear the TTY_LDISC flag to ensure no further
589  *      references can be obtained, wait for remaining references to be
590  *      released, and cancel pending buffer work to ensure no more
591  *      data is fed to this ldisc.
592  *      Caller must hold legacy and ->ldisc_mutex.
593  *
594  *      NB: tty_set_ldisc() is prevented from changing the ldisc concurrently
595  *      with this function by checking the TTY_HUPPING flag.
596  *
597  *      NB: if tty->ldisc is NULL then buffer work does not need to be
598  *      cancelled because it must already have done as a precondition
599  *      of closing the ldisc and setting tty->ldisc to NULL
600  */
601 static bool tty_ldisc_hangup_halt(struct tty_struct *tty)
602 {
603         char cur_n[TASK_COMM_LEN], tty_n[64];
604         long timeout = 3 * HZ;
605
606         clear_bit(TTY_LDISC, &tty->flags);
607
608         if (tty->ldisc) {       /* Not yet closed */
609                 tty_unlock(tty);
610
611                 while (tty_ldisc_wait_idle(tty, timeout) == -EBUSY) {
612                         timeout = MAX_SCHEDULE_TIMEOUT;
613                         printk_ratelimited(KERN_WARNING
614                                 "%s: waiting (%s) for %s took too long, but we keep waiting...\n",
615                                 __func__, get_task_comm(cur_n, current),
616                                 tty_name(tty, tty_n));
617                 }
618
619                 cancel_work_sync(&tty->port->buf.work);
620                 set_bit(TTY_LDISC_HALTED, &tty->flags);
621
622                 /* must reacquire both locks and preserve lock order */
623                 mutex_unlock(&tty->ldisc_mutex);
624                 tty_lock(tty);
625                 mutex_lock(&tty->ldisc_mutex);
626         }
627         return !!tty->ldisc;
628 }
629
630 /**
631  *      tty_set_ldisc           -       set line discipline
632  *      @tty: the terminal to set
633  *      @ldisc: the line discipline
634  *
635  *      Set the discipline of a tty line. Must be called from a process
636  *      context. The ldisc change logic has to protect itself against any
637  *      overlapping ldisc change (including on the other end of pty pairs),
638  *      the close of one side of a tty/pty pair, and eventually hangup.
639  *
640  *      Locking: takes tty_ldisc_lock, termios_mutex
641  */
642
643 int tty_set_ldisc(struct tty_struct *tty, int ldisc)
644 {
645         int retval;
646         struct tty_ldisc *o_ldisc, *new_ldisc;
647         int work, o_work = 0;
648         struct tty_struct *o_tty;
649
650         new_ldisc = tty_ldisc_get(ldisc);
651         if (IS_ERR(new_ldisc))
652                 return PTR_ERR(new_ldisc);
653
654         tty_lock(tty);
655         /*
656          *      We need to look at the tty locking here for pty/tty pairs
657          *      when both sides try to change in parallel.
658          */
659
660         o_tty = tty->link;      /* o_tty is the pty side or NULL */
661
662
663         /*
664          *      Check the no-op case
665          */
666
667         if (tty->ldisc->ops->num == ldisc) {
668                 tty_unlock(tty);
669                 tty_ldisc_put(new_ldisc);
670                 return 0;
671         }
672
673         tty_unlock(tty);
674         /*
675          *      Problem: What do we do if this blocks ?
676          *      We could deadlock here
677          */
678
679         tty_wait_until_sent(tty, 0);
680
681         tty_lock(tty);
682         mutex_lock(&tty->ldisc_mutex);
683
684         /*
685          *      We could be midstream of another ldisc change which has
686          *      dropped the lock during processing. If so we need to wait.
687          */
688
689         while (test_bit(TTY_LDISC_CHANGING, &tty->flags)) {
690                 mutex_unlock(&tty->ldisc_mutex);
691                 tty_unlock(tty);
692                 wait_event(tty_ldisc_wait,
693                         test_bit(TTY_LDISC_CHANGING, &tty->flags) == 0);
694                 tty_lock(tty);
695                 mutex_lock(&tty->ldisc_mutex);
696         }
697
698         set_bit(TTY_LDISC_CHANGING, &tty->flags);
699
700         /*
701          *      No more input please, we are switching. The new ldisc
702          *      will update this value in the ldisc open function
703          */
704
705         tty->receive_room = 0;
706
707         o_ldisc = tty->ldisc;
708
709         tty_unlock(tty);
710         /*
711          *      Make sure we don't change while someone holds a
712          *      reference to the line discipline. The TTY_LDISC bit
713          *      prevents anyone taking a reference once it is clear.
714          *      We need the lock to avoid racing reference takers.
715          *
716          *      We must clear the TTY_LDISC bit here to avoid a livelock
717          *      with a userspace app continually trying to use the tty in
718          *      parallel to the change and re-referencing the tty.
719          */
720
721         retval = tty_ldisc_halt(tty, o_tty, &work, &o_work, 5 * HZ);
722
723         /*
724          * Wait for ->hangup_work and ->buf.work handlers to terminate.
725          * We must drop the mutex here in case a hangup is also in process.
726          */
727
728         mutex_unlock(&tty->ldisc_mutex);
729
730         tty_ldisc_flush_works(tty);
731
732         tty_lock(tty);
733         mutex_lock(&tty->ldisc_mutex);
734
735         /* handle wait idle failure locked */
736         if (retval) {
737                 tty_ldisc_put(new_ldisc);
738                 goto enable;
739         }
740
741         if (test_bit(TTY_HUPPING, &tty->flags)) {
742                 /* We were raced by the hangup method. It will have stomped
743                    the ldisc data and closed the ldisc down */
744                 clear_bit(TTY_LDISC_CHANGING, &tty->flags);
745                 mutex_unlock(&tty->ldisc_mutex);
746                 tty_ldisc_put(new_ldisc);
747                 tty_unlock(tty);
748                 return -EIO;
749         }
750
751         /* Shutdown the current discipline. */
752         tty_ldisc_close(tty, o_ldisc);
753
754         /* Now set up the new line discipline. */
755         tty_ldisc_assign(tty, new_ldisc);
756         tty_set_termios_ldisc(tty, ldisc);
757
758         retval = tty_ldisc_open(tty, new_ldisc);
759         if (retval < 0) {
760                 /* Back to the old one or N_TTY if we can't */
761                 tty_ldisc_put(new_ldisc);
762                 tty_ldisc_restore(tty, o_ldisc);
763         }
764
765         /* At this point we hold a reference to the new ldisc and a
766            a reference to the old ldisc. If we ended up flipping back
767            to the existing ldisc we have two references to it */
768
769         if (tty->ldisc->ops->num != o_ldisc->ops->num && tty->ops->set_ldisc)
770                 tty->ops->set_ldisc(tty);
771
772         tty_ldisc_put(o_ldisc);
773
774 enable:
775         /*
776          *      Allow ldisc referencing to occur again
777          */
778
779         tty_ldisc_enable(tty);
780         if (o_tty)
781                 tty_ldisc_enable(o_tty);
782
783         /* Restart the work queue in case no characters kick it off. Safe if
784            already running */
785         if (work)
786                 schedule_work(&tty->port->buf.work);
787         if (o_work)
788                 schedule_work(&o_tty->port->buf.work);
789         mutex_unlock(&tty->ldisc_mutex);
790         tty_unlock(tty);
791         return retval;
792 }
793
794 /**
795  *      tty_reset_termios       -       reset terminal state
796  *      @tty: tty to reset
797  *
798  *      Restore a terminal to the driver default state.
799  */
800
801 static void tty_reset_termios(struct tty_struct *tty)
802 {
803         mutex_lock(&tty->termios_mutex);
804         tty->termios = tty->driver->init_termios;
805         tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios);
806         tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios);
807         mutex_unlock(&tty->termios_mutex);
808 }
809
810
811 /**
812  *      tty_ldisc_reinit        -       reinitialise the tty ldisc
813  *      @tty: tty to reinit
814  *      @ldisc: line discipline to reinitialize
815  *
816  *      Switch the tty to a line discipline and leave the ldisc
817  *      state closed
818  */
819
820 static int tty_ldisc_reinit(struct tty_struct *tty, int ldisc)
821 {
822         struct tty_ldisc *ld = tty_ldisc_get(ldisc);
823
824         if (IS_ERR(ld))
825                 return -1;
826
827         tty_ldisc_close(tty, tty->ldisc);
828         tty_ldisc_put(tty->ldisc);
829         tty->ldisc = NULL;
830         /*
831          *      Switch the line discipline back
832          */
833         tty_ldisc_assign(tty, ld);
834         tty_set_termios_ldisc(tty, ldisc);
835
836         return 0;
837 }
838
839 /**
840  *      tty_ldisc_hangup                -       hangup ldisc reset
841  *      @tty: tty being hung up
842  *
843  *      Some tty devices reset their termios when they receive a hangup
844  *      event. In that situation we must also switch back to N_TTY properly
845  *      before we reset the termios data.
846  *
847  *      Locking: We can take the ldisc mutex as the rest of the code is
848  *      careful to allow for this.
849  *
850  *      In the pty pair case this occurs in the close() path of the
851  *      tty itself so we must be careful about locking rules.
852  */
853
854 void tty_ldisc_hangup(struct tty_struct *tty)
855 {
856         struct tty_ldisc *ld;
857         int reset = tty->driver->flags & TTY_DRIVER_RESET_TERMIOS;
858         int err = 0;
859
860         /*
861          * FIXME! What are the locking issues here? This may me overdoing
862          * things... This question is especially important now that we've
863          * removed the irqlock.
864          */
865         ld = tty_ldisc_ref(tty);
866         if (ld != NULL) {
867                 /* We may have no line discipline at this point */
868                 if (ld->ops->flush_buffer)
869                         ld->ops->flush_buffer(tty);
870                 tty_driver_flush_buffer(tty);
871                 if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
872                     ld->ops->write_wakeup)
873                         ld->ops->write_wakeup(tty);
874                 if (ld->ops->hangup)
875                         ld->ops->hangup(tty);
876                 tty_ldisc_deref(ld);
877         }
878         /*
879          * FIXME: Once we trust the LDISC code better we can wait here for
880          * ldisc completion and fix the driver call race
881          */
882         wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
883         wake_up_interruptible_poll(&tty->read_wait, POLLIN);
884         /*
885          * Shutdown the current line discipline, and reset it to
886          * N_TTY if need be.
887          *
888          * Avoid racing set_ldisc or tty_ldisc_release
889          */
890         mutex_lock(&tty->ldisc_mutex);
891
892         /* At this point we have a closed ldisc and we want to
893            reopen it. We could defer this to the next open but
894            it means auditing a lot of other paths so this is
895            a FIXME */
896         if (tty_ldisc_hangup_halt(tty)) {
897                 if (reset == 0) {
898
899                         if (!tty_ldisc_reinit(tty, tty->termios.c_line))
900                                 err = tty_ldisc_open(tty, tty->ldisc);
901                         else
902                                 err = 1;
903                 }
904                 /* If the re-open fails or we reset then go to N_TTY. The
905                    N_TTY open cannot fail */
906                 if (reset || err) {
907                         BUG_ON(tty_ldisc_reinit(tty, N_TTY));
908                         WARN_ON(tty_ldisc_open(tty, tty->ldisc));
909                 }
910                 tty_ldisc_enable(tty);
911         }
912         mutex_unlock(&tty->ldisc_mutex);
913         if (reset)
914                 tty_reset_termios(tty);
915 }
916
917 /**
918  *      tty_ldisc_setup                 -       open line discipline
919  *      @tty: tty being shut down
920  *      @o_tty: pair tty for pty/tty pairs
921  *
922  *      Called during the initial open of a tty/pty pair in order to set up the
923  *      line disciplines and bind them to the tty. This has no locking issues
924  *      as the device isn't yet active.
925  */
926
927 int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty)
928 {
929         struct tty_ldisc *ld = tty->ldisc;
930         int retval;
931
932         retval = tty_ldisc_open(tty, ld);
933         if (retval)
934                 return retval;
935
936         if (o_tty) {
937                 retval = tty_ldisc_open(o_tty, o_tty->ldisc);
938                 if (retval) {
939                         tty_ldisc_close(tty, ld);
940                         return retval;
941                 }
942                 tty_ldisc_enable(o_tty);
943         }
944         tty_ldisc_enable(tty);
945         return 0;
946 }
947
948 static void tty_ldisc_kill(struct tty_struct *tty)
949 {
950         mutex_lock(&tty->ldisc_mutex);
951         /*
952          * Now kill off the ldisc
953          */
954         tty_ldisc_close(tty, tty->ldisc);
955         tty_ldisc_put(tty->ldisc);
956         /* Force an oops if we mess this up */
957         tty->ldisc = NULL;
958
959         /* Ensure the next open requests the N_TTY ldisc */
960         tty_set_termios_ldisc(tty, N_TTY);
961         mutex_unlock(&tty->ldisc_mutex);
962 }
963
964 /**
965  *      tty_ldisc_release               -       release line discipline
966  *      @tty: tty being shut down
967  *      @o_tty: pair tty for pty/tty pairs
968  *
969  *      Called during the final close of a tty/pty pair in order to shut down
970  *      the line discpline layer. On exit the ldisc assigned is N_TTY and the
971  *      ldisc has not been opened.
972  */
973
974 void tty_ldisc_release(struct tty_struct *tty, struct tty_struct *o_tty)
975 {
976         /*
977          * Prevent flush_to_ldisc() from rescheduling the work for later.  Then
978          * kill any delayed work. As this is the final close it does not
979          * race with the set_ldisc code path.
980          */
981
982         tty_ldisc_halt(tty, o_tty, NULL, NULL, MAX_SCHEDULE_TIMEOUT);
983         tty_ldisc_flush_works(tty);
984         if (o_tty)
985                 tty_ldisc_flush_works(o_tty);
986
987         tty_lock_pair(tty, o_tty);
988         /* This will need doing differently if we need to lock */
989         tty_ldisc_kill(tty);
990         if (o_tty)
991                 tty_ldisc_kill(o_tty);
992
993         tty_unlock_pair(tty, o_tty);
994         /* And the memory resources remaining (buffers, termios) will be
995            disposed of when the kref hits zero */
996 }
997
998 /**
999  *      tty_ldisc_init          -       ldisc setup for new tty
1000  *      @tty: tty being allocated
1001  *
1002  *      Set up the line discipline objects for a newly allocated tty. Note that
1003  *      the tty structure is not completely set up when this call is made.
1004  */
1005
1006 void tty_ldisc_init(struct tty_struct *tty)
1007 {
1008         struct tty_ldisc *ld = tty_ldisc_get(N_TTY);
1009         if (IS_ERR(ld))
1010                 panic("n_tty: init_tty");
1011         tty_ldisc_assign(tty, ld);
1012 }
1013
1014 /**
1015  *      tty_ldisc_init          -       ldisc cleanup for new tty
1016  *      @tty: tty that was allocated recently
1017  *
1018  *      The tty structure must not becompletely set up (tty_ldisc_setup) when
1019  *      this call is made.
1020  */
1021 void tty_ldisc_deinit(struct tty_struct *tty)
1022 {
1023         put_ldisc(tty->ldisc);
1024         tty_ldisc_assign(tty, NULL);
1025 }
1026
1027 void tty_ldisc_begin(void)
1028 {
1029         /* Setup the default TTY line discipline. */
1030         (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
1031 }