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#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
34
35
36
37
38
39
40static DEFINE_RAW_SPINLOCK(tty_ldisc_lock);
41static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_wait);
42
43static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS];
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc)
59{
60 unsigned long flags;
61 int ret = 0;
62
63 if (disc < N_TTY || disc >= NR_LDISCS)
64 return -EINVAL;
65
66 raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
67 tty_ldiscs[disc] = new_ldisc;
68 new_ldisc->num = disc;
69 new_ldisc->refcount = 0;
70 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
71
72 return ret;
73}
74EXPORT_SYMBOL(tty_register_ldisc);
75
76
77
78
79
80
81
82
83
84
85
86
87
88int tty_unregister_ldisc(int disc)
89{
90 unsigned long flags;
91 int ret = 0;
92
93 if (disc < N_TTY || disc >= NR_LDISCS)
94 return -EINVAL;
95
96 raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
97 if (tty_ldiscs[disc]->refcount)
98 ret = -EBUSY;
99 else
100 tty_ldiscs[disc] = NULL;
101 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
102
103 return ret;
104}
105EXPORT_SYMBOL(tty_unregister_ldisc);
106
107static struct tty_ldisc_ops *get_ldops(int disc)
108{
109 unsigned long flags;
110 struct tty_ldisc_ops *ldops, *ret;
111
112 raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
113 ret = ERR_PTR(-EINVAL);
114 ldops = tty_ldiscs[disc];
115 if (ldops) {
116 ret = ERR_PTR(-EAGAIN);
117 if (try_module_get(ldops->owner)) {
118 ldops->refcount++;
119 ret = ldops;
120 }
121 }
122 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
123 return ret;
124}
125
126static void put_ldops(struct tty_ldisc_ops *ldops)
127{
128 unsigned long flags;
129
130 raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
131 ldops->refcount--;
132 module_put(ldops->owner);
133 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
134}
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149static struct tty_ldisc *tty_ldisc_get(int disc)
150{
151 struct tty_ldisc *ld;
152 struct tty_ldisc_ops *ldops;
153
154 if (disc < N_TTY || disc >= NR_LDISCS)
155 return ERR_PTR(-EINVAL);
156
157
158
159
160
161 ldops = get_ldops(disc);
162 if (IS_ERR(ldops)) {
163 request_module("tty-ldisc-%d", disc);
164 ldops = get_ldops(disc);
165 if (IS_ERR(ldops))
166 return ERR_CAST(ldops);
167 }
168
169 ld = kmalloc(sizeof(struct tty_ldisc), GFP_KERNEL);
170 if (ld == NULL) {
171 put_ldops(ldops);
172 return ERR_PTR(-ENOMEM);
173 }
174
175 ld->ops = ldops;
176 atomic_set(&ld->users, 1);
177 init_waitqueue_head(&ld->wq_idle);
178
179 return ld;
180}
181
182
183
184
185
186
187static inline void tty_ldisc_put(struct tty_ldisc *ld)
188{
189 unsigned long flags;
190
191 if (WARN_ON_ONCE(!ld))
192 return;
193
194 raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
195
196
197 WARN_ON(!atomic_dec_and_test(&ld->users));
198
199 ld->ops->refcount--;
200 module_put(ld->ops->owner);
201 kfree(ld);
202 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
203}
204
205static void *tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos)
206{
207 return (*pos < NR_LDISCS) ? pos : NULL;
208}
209
210static void *tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos)
211{
212 (*pos)++;
213 return (*pos < NR_LDISCS) ? pos : NULL;
214}
215
216static void tty_ldiscs_seq_stop(struct seq_file *m, void *v)
217{
218}
219
220static int tty_ldiscs_seq_show(struct seq_file *m, void *v)
221{
222 int i = *(loff_t *)v;
223 struct tty_ldisc_ops *ldops;
224
225 ldops = get_ldops(i);
226 if (IS_ERR(ldops))
227 return 0;
228 seq_printf(m, "%-10s %2d\n", ldops->name ? ldops->name : "???", i);
229 put_ldops(ldops);
230 return 0;
231}
232
233static const struct seq_operations tty_ldiscs_seq_ops = {
234 .start = tty_ldiscs_seq_start,
235 .next = tty_ldiscs_seq_next,
236 .stop = tty_ldiscs_seq_stop,
237 .show = tty_ldiscs_seq_show,
238};
239
240static int proc_tty_ldiscs_open(struct inode *inode, struct file *file)
241{
242 return seq_open(file, &tty_ldiscs_seq_ops);
243}
244
245const struct file_operations tty_ldiscs_proc_fops = {
246 .owner = THIS_MODULE,
247 .open = proc_tty_ldiscs_open,
248 .read = seq_read,
249 .llseek = seq_lseek,
250 .release = seq_release,
251};
252
253
254
255
256
257
258
259
260
261
262
263
264
265static struct tty_ldisc *tty_ldisc_try(struct tty_struct *tty)
266{
267 unsigned long flags;
268 struct tty_ldisc *ld;
269
270
271 raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
272 ld = NULL;
273 if (test_bit(TTY_LDISC, &tty->flags) && tty->ldisc) {
274 ld = tty->ldisc;
275 atomic_inc(&ld->users);
276 }
277 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
278 return ld;
279}
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
298{
299 struct tty_ldisc *ld;
300
301
302 wait_event(tty_ldisc_wait, (ld = tty_ldisc_try(tty)) != NULL);
303 return ld;
304}
305EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
306
307
308
309
310
311
312
313
314
315
316
317
318struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
319{
320 return tty_ldisc_try(tty);
321}
322EXPORT_SYMBOL_GPL(tty_ldisc_ref);
323
324
325
326
327
328
329
330
331
332
333
334void tty_ldisc_deref(struct tty_ldisc *ld)
335{
336 unsigned long flags;
337
338 if (WARN_ON_ONCE(!ld))
339 return;
340
341 raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
342
343
344
345
346 WARN_ON(atomic_dec_and_test(&ld->users));
347 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
348
349 if (waitqueue_active(&ld->wq_idle))
350 wake_up(&ld->wq_idle);
351}
352EXPORT_SYMBOL_GPL(tty_ldisc_deref);
353
354
355
356
357
358
359
360
361
362
363
364
365
366static void tty_ldisc_enable(struct tty_struct *tty)
367{
368 clear_bit(TTY_LDISC_HALTED, &tty->flags);
369 set_bit(TTY_LDISC, &tty->flags);
370 clear_bit(TTY_LDISC_CHANGING, &tty->flags);
371 wake_up(&tty_ldisc_wait);
372}
373
374
375
376
377
378
379
380
381
382void tty_ldisc_flush(struct tty_struct *tty)
383{
384 struct tty_ldisc *ld = tty_ldisc_ref(tty);
385 if (ld) {
386 if (ld->ops->flush_buffer)
387 ld->ops->flush_buffer(tty);
388 tty_ldisc_deref(ld);
389 }
390 tty_buffer_flush(tty);
391}
392EXPORT_SYMBOL_GPL(tty_ldisc_flush);
393
394
395
396
397
398
399
400
401
402
403
404
405
406static void tty_set_termios_ldisc(struct tty_struct *tty, int num)
407{
408 mutex_lock(&tty->termios_mutex);
409 tty->termios.c_line = num;
410 mutex_unlock(&tty->termios_mutex);
411}
412
413
414
415
416
417
418
419
420
421
422
423
424static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld)
425{
426 WARN_ON(test_and_set_bit(TTY_LDISC_OPEN, &tty->flags));
427 if (ld->ops->open) {
428 int ret;
429
430 ret = ld->ops->open(tty);
431 if (ret)
432 clear_bit(TTY_LDISC_OPEN, &tty->flags);
433 return ret;
434 }
435 return 0;
436}
437
438
439
440
441
442
443
444
445
446
447static void tty_ldisc_close(struct tty_struct *tty, struct tty_ldisc *ld)
448{
449 WARN_ON(!test_bit(TTY_LDISC_OPEN, &tty->flags));
450 clear_bit(TTY_LDISC_OPEN, &tty->flags);
451 if (ld->ops->close)
452 ld->ops->close(tty);
453}
454
455
456
457
458
459
460
461
462
463
464static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old)
465{
466 char buf[64];
467 struct tty_ldisc *new_ldisc;
468 int r;
469
470
471 old = tty_ldisc_get(old->ops->num);
472 WARN_ON(IS_ERR(old));
473 tty->ldisc = old;
474 tty_set_termios_ldisc(tty, old->ops->num);
475 if (tty_ldisc_open(tty, old) < 0) {
476 tty_ldisc_put(old);
477
478 new_ldisc = tty_ldisc_get(N_TTY);
479 if (IS_ERR(new_ldisc))
480 panic("n_tty: get");
481 tty->ldisc = new_ldisc;
482 tty_set_termios_ldisc(tty, N_TTY);
483 r = tty_ldisc_open(tty, new_ldisc);
484 if (r < 0)
485 panic("Couldn't open N_TTY ldisc for "
486 "%s --- error %d.",
487 tty_name(tty, buf), r);
488 }
489}
490
491
492
493
494
495
496
497
498
499static int tty_ldisc_wait_idle(struct tty_struct *tty, long timeout)
500{
501 long ret;
502 ret = wait_event_timeout(tty->ldisc->wq_idle,
503 atomic_read(&tty->ldisc->users) == 1, timeout);
504 return ret > 0 ? 0 : -EBUSY;
505}
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523static int tty_ldisc_halt(struct tty_struct *tty, struct tty_struct *o_tty,
524 long timeout)
525{
526 int retval;
527
528 clear_bit(TTY_LDISC, &tty->flags);
529 if (o_tty)
530 clear_bit(TTY_LDISC, &o_tty->flags);
531
532 retval = tty_ldisc_wait_idle(tty, timeout);
533 if (!retval && o_tty)
534 retval = tty_ldisc_wait_idle(o_tty, timeout);
535 if (retval)
536 return retval;
537
538 set_bit(TTY_LDISC_HALTED, &tty->flags);
539 if (o_tty)
540 set_bit(TTY_LDISC_HALTED, &o_tty->flags);
541
542 return 0;
543}
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558static bool tty_ldisc_hangup_halt(struct tty_struct *tty)
559{
560 char cur_n[TASK_COMM_LEN], tty_n[64];
561 long timeout = 3 * HZ;
562
563 clear_bit(TTY_LDISC, &tty->flags);
564
565 if (tty->ldisc) {
566 tty_unlock(tty);
567
568 while (tty_ldisc_wait_idle(tty, timeout) == -EBUSY) {
569 timeout = MAX_SCHEDULE_TIMEOUT;
570 printk_ratelimited(KERN_WARNING
571 "%s: waiting (%s) for %s took too long, but we keep waiting...\n",
572 __func__, get_task_comm(cur_n, current),
573 tty_name(tty, tty_n));
574 }
575
576 set_bit(TTY_LDISC_HALTED, &tty->flags);
577
578
579 mutex_unlock(&tty->ldisc_mutex);
580 tty_lock(tty);
581 mutex_lock(&tty->ldisc_mutex);
582 }
583 return !!tty->ldisc;
584}
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599int tty_set_ldisc(struct tty_struct *tty, int ldisc)
600{
601 int retval;
602 struct tty_ldisc *o_ldisc, *new_ldisc;
603 struct tty_struct *o_tty;
604
605 new_ldisc = tty_ldisc_get(ldisc);
606 if (IS_ERR(new_ldisc))
607 return PTR_ERR(new_ldisc);
608
609 tty_lock(tty);
610
611
612
613
614
615 o_tty = tty->link;
616
617
618
619
620
621
622 if (tty->ldisc->ops->num == ldisc) {
623 tty_unlock(tty);
624 tty_ldisc_put(new_ldisc);
625 return 0;
626 }
627
628 mutex_lock(&tty->ldisc_mutex);
629
630
631
632
633
634
635 while (test_bit(TTY_LDISC_CHANGING, &tty->flags)) {
636 mutex_unlock(&tty->ldisc_mutex);
637 tty_unlock(tty);
638 wait_event(tty_ldisc_wait,
639 test_bit(TTY_LDISC_CHANGING, &tty->flags) == 0);
640 tty_lock(tty);
641 mutex_lock(&tty->ldisc_mutex);
642 }
643
644 set_bit(TTY_LDISC_CHANGING, &tty->flags);
645
646
647
648
649
650
651 tty->receive_room = 0;
652
653 o_ldisc = tty->ldisc;
654
655 tty_unlock(tty);
656
657
658
659
660
661
662
663
664
665
666
667 retval = tty_ldisc_halt(tty, o_tty, 5 * HZ);
668
669
670
671
672
673
674 mutex_unlock(&tty->ldisc_mutex);
675
676 flush_work(&tty->hangup_work);
677
678 tty_lock(tty);
679 mutex_lock(&tty->ldisc_mutex);
680
681
682 if (retval) {
683 tty_ldisc_put(new_ldisc);
684 goto enable;
685 }
686
687 if (test_bit(TTY_HUPPING, &tty->flags)) {
688
689
690 clear_bit(TTY_LDISC_CHANGING, &tty->flags);
691 mutex_unlock(&tty->ldisc_mutex);
692 tty_ldisc_put(new_ldisc);
693 tty_unlock(tty);
694 return -EIO;
695 }
696
697
698 tty_ldisc_close(tty, o_ldisc);
699
700
701 tty->ldisc = new_ldisc;
702 tty_set_termios_ldisc(tty, ldisc);
703
704 retval = tty_ldisc_open(tty, new_ldisc);
705 if (retval < 0) {
706
707 tty_ldisc_put(new_ldisc);
708 tty_ldisc_restore(tty, o_ldisc);
709 }
710
711
712
713
714
715 if (tty->ldisc->ops->num != o_ldisc->ops->num && tty->ops->set_ldisc)
716 tty->ops->set_ldisc(tty);
717
718 tty_ldisc_put(o_ldisc);
719
720enable:
721
722
723
724
725 tty_ldisc_enable(tty);
726 if (o_tty)
727 tty_ldisc_enable(o_tty);
728
729
730
731 schedule_work(&tty->port->buf.work);
732 if (o_tty)
733 schedule_work(&o_tty->port->buf.work);
734
735 mutex_unlock(&tty->ldisc_mutex);
736 tty_unlock(tty);
737 return retval;
738}
739
740
741
742
743
744
745
746
747static void tty_reset_termios(struct tty_struct *tty)
748{
749 mutex_lock(&tty->termios_mutex);
750 tty->termios = tty->driver->init_termios;
751 tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios);
752 tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios);
753 mutex_unlock(&tty->termios_mutex);
754}
755
756
757
758
759
760
761
762
763
764
765
766static int tty_ldisc_reinit(struct tty_struct *tty, int ldisc)
767{
768 struct tty_ldisc *ld = tty_ldisc_get(ldisc);
769
770 if (IS_ERR(ld))
771 return -1;
772
773 tty_ldisc_close(tty, tty->ldisc);
774 tty_ldisc_put(tty->ldisc);
775
776
777
778 tty->ldisc = ld;
779 tty_set_termios_ldisc(tty, ldisc);
780
781 return 0;
782}
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799void tty_ldisc_hangup(struct tty_struct *tty)
800{
801 struct tty_ldisc *ld;
802 int reset = tty->driver->flags & TTY_DRIVER_RESET_TERMIOS;
803 int err = 0;
804
805 tty_ldisc_debug(tty, "closing ldisc: %p\n", tty->ldisc);
806
807
808
809
810
811
812 ld = tty_ldisc_ref(tty);
813 if (ld != NULL) {
814
815 if (ld->ops->flush_buffer)
816 ld->ops->flush_buffer(tty);
817 tty_driver_flush_buffer(tty);
818 if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
819 ld->ops->write_wakeup)
820 ld->ops->write_wakeup(tty);
821 if (ld->ops->hangup)
822 ld->ops->hangup(tty);
823 tty_ldisc_deref(ld);
824 }
825
826
827
828
829 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
830 wake_up_interruptible_poll(&tty->read_wait, POLLIN);
831
832
833
834
835
836
837 mutex_lock(&tty->ldisc_mutex);
838
839 if (tty_ldisc_hangup_halt(tty)) {
840
841
842
843
844
845 if (reset == 0) {
846
847 if (!tty_ldisc_reinit(tty, tty->termios.c_line))
848 err = tty_ldisc_open(tty, tty->ldisc);
849 else
850 err = 1;
851 }
852
853
854 if (reset || err) {
855 BUG_ON(tty_ldisc_reinit(tty, N_TTY));
856 WARN_ON(tty_ldisc_open(tty, tty->ldisc));
857 }
858 tty_ldisc_enable(tty);
859 }
860 mutex_unlock(&tty->ldisc_mutex);
861 if (reset)
862 tty_reset_termios(tty);
863
864 tty_ldisc_debug(tty, "re-opened ldisc: %p\n", tty->ldisc);
865}
866
867
868
869
870
871
872
873
874
875
876
877int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty)
878{
879 struct tty_ldisc *ld = tty->ldisc;
880 int retval;
881
882 retval = tty_ldisc_open(tty, ld);
883 if (retval)
884 return retval;
885
886 if (o_tty) {
887 retval = tty_ldisc_open(o_tty, o_tty->ldisc);
888 if (retval) {
889 tty_ldisc_close(tty, ld);
890 return retval;
891 }
892 tty_ldisc_enable(o_tty);
893 }
894 tty_ldisc_enable(tty);
895 return 0;
896}
897
898static void tty_ldisc_kill(struct tty_struct *tty)
899{
900 mutex_lock(&tty->ldisc_mutex);
901
902
903
904 tty_ldisc_close(tty, tty->ldisc);
905 tty_ldisc_put(tty->ldisc);
906
907 tty->ldisc = NULL;
908
909
910 tty_set_termios_ldisc(tty, N_TTY);
911 mutex_unlock(&tty->ldisc_mutex);
912}
913
914
915
916
917
918
919
920
921
922
923
924void tty_ldisc_release(struct tty_struct *tty, struct tty_struct *o_tty)
925{
926
927
928
929
930
931 tty_ldisc_debug(tty, "closing ldisc: %p\n", tty->ldisc);
932
933 tty_ldisc_halt(tty, o_tty, MAX_SCHEDULE_TIMEOUT);
934
935 tty_lock_pair(tty, o_tty);
936
937 tty_ldisc_kill(tty);
938 if (o_tty)
939 tty_ldisc_kill(o_tty);
940
941 tty_unlock_pair(tty, o_tty);
942
943
944
945 tty_ldisc_debug(tty, "ldisc closed\n");
946}
947
948
949
950
951
952
953
954
955
956void tty_ldisc_init(struct tty_struct *tty)
957{
958 struct tty_ldisc *ld = tty_ldisc_get(N_TTY);
959 if (IS_ERR(ld))
960 panic("n_tty: init_tty");
961 tty->ldisc = ld;
962}
963
964
965
966
967
968
969
970
971void tty_ldisc_deinit(struct tty_struct *tty)
972{
973 tty_ldisc_put(tty->ldisc);
974 tty->ldisc = NULL;
975}
976
977void tty_ldisc_begin(void)
978{
979
980 (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
981}
982