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