diff options
Diffstat (limited to 'drivers/char/tty_io.c')
-rw-r--r-- | drivers/char/tty_io.c | 719 |
1 files changed, 44 insertions, 675 deletions
diff --git a/drivers/char/tty_io.c b/drivers/char/tty_io.c index 82f6a8c86332..15e597d03002 100644 --- a/drivers/char/tty_io.c +++ b/drivers/char/tty_io.c | |||
@@ -656,558 +656,6 @@ EXPORT_SYMBOL_GPL(tty_prepare_flip_string_flags); | |||
656 | 656 | ||
657 | 657 | ||
658 | /** | 658 | /** |
659 | * tty_set_termios_ldisc - set ldisc field | ||
660 | * @tty: tty structure | ||
661 | * @num: line discipline number | ||
662 | * | ||
663 | * This is probably overkill for real world processors but | ||
664 | * they are not on hot paths so a little discipline won't do | ||
665 | * any harm. | ||
666 | * | ||
667 | * Locking: takes termios_mutex | ||
668 | */ | ||
669 | |||
670 | static void tty_set_termios_ldisc(struct tty_struct *tty, int num) | ||
671 | { | ||
672 | mutex_lock(&tty->termios_mutex); | ||
673 | tty->termios->c_line = num; | ||
674 | mutex_unlock(&tty->termios_mutex); | ||
675 | } | ||
676 | |||
677 | /* | ||
678 | * This guards the refcounted line discipline lists. The lock | ||
679 | * must be taken with irqs off because there are hangup path | ||
680 | * callers who will do ldisc lookups and cannot sleep. | ||
681 | */ | ||
682 | |||
683 | static DEFINE_SPINLOCK(tty_ldisc_lock); | ||
684 | static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_wait); | ||
685 | /* Line disc dispatch table */ | ||
686 | static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS]; | ||
687 | |||
688 | /** | ||
689 | * tty_register_ldisc - install a line discipline | ||
690 | * @disc: ldisc number | ||
691 | * @new_ldisc: pointer to the ldisc object | ||
692 | * | ||
693 | * Installs a new line discipline into the kernel. The discipline | ||
694 | * is set up as unreferenced and then made available to the kernel | ||
695 | * from this point onwards. | ||
696 | * | ||
697 | * Locking: | ||
698 | * takes tty_ldisc_lock to guard against ldisc races | ||
699 | */ | ||
700 | |||
701 | int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc) | ||
702 | { | ||
703 | unsigned long flags; | ||
704 | int ret = 0; | ||
705 | |||
706 | if (disc < N_TTY || disc >= NR_LDISCS) | ||
707 | return -EINVAL; | ||
708 | |||
709 | spin_lock_irqsave(&tty_ldisc_lock, flags); | ||
710 | tty_ldiscs[disc] = new_ldisc; | ||
711 | new_ldisc->num = disc; | ||
712 | new_ldisc->refcount = 0; | ||
713 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); | ||
714 | |||
715 | return ret; | ||
716 | } | ||
717 | EXPORT_SYMBOL(tty_register_ldisc); | ||
718 | |||
719 | /** | ||
720 | * tty_unregister_ldisc - unload a line discipline | ||
721 | * @disc: ldisc number | ||
722 | * @new_ldisc: pointer to the ldisc object | ||
723 | * | ||
724 | * Remove a line discipline from the kernel providing it is not | ||
725 | * currently in use. | ||
726 | * | ||
727 | * Locking: | ||
728 | * takes tty_ldisc_lock to guard against ldisc races | ||
729 | */ | ||
730 | |||
731 | int tty_unregister_ldisc(int disc) | ||
732 | { | ||
733 | unsigned long flags; | ||
734 | int ret = 0; | ||
735 | |||
736 | if (disc < N_TTY || disc >= NR_LDISCS) | ||
737 | return -EINVAL; | ||
738 | |||
739 | spin_lock_irqsave(&tty_ldisc_lock, flags); | ||
740 | if (tty_ldiscs[disc]->refcount) | ||
741 | ret = -EBUSY; | ||
742 | else | ||
743 | tty_ldiscs[disc] = NULL; | ||
744 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); | ||
745 | |||
746 | return ret; | ||
747 | } | ||
748 | EXPORT_SYMBOL(tty_unregister_ldisc); | ||
749 | |||
750 | |||
751 | /** | ||
752 | * tty_ldisc_try_get - try and reference an ldisc | ||
753 | * @disc: ldisc number | ||
754 | * @ld: tty ldisc structure to complete | ||
755 | * | ||
756 | * Attempt to open and lock a line discipline into place. Return | ||
757 | * the line discipline refcounted and assigned in ld. On an error | ||
758 | * report the error code back | ||
759 | */ | ||
760 | |||
761 | static int tty_ldisc_try_get(int disc, struct tty_ldisc *ld) | ||
762 | { | ||
763 | unsigned long flags; | ||
764 | struct tty_ldisc_ops *ldops; | ||
765 | int err = -EINVAL; | ||
766 | |||
767 | spin_lock_irqsave(&tty_ldisc_lock, flags); | ||
768 | ld->ops = NULL; | ||
769 | ldops = tty_ldiscs[disc]; | ||
770 | /* Check the entry is defined */ | ||
771 | if (ldops) { | ||
772 | /* If the module is being unloaded we can't use it */ | ||
773 | if (!try_module_get(ldops->owner)) | ||
774 | err = -EAGAIN; | ||
775 | else { | ||
776 | /* lock it */ | ||
777 | ldops->refcount++; | ||
778 | ld->ops = ldops; | ||
779 | err = 0; | ||
780 | } | ||
781 | } | ||
782 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); | ||
783 | return err; | ||
784 | } | ||
785 | |||
786 | /** | ||
787 | * tty_ldisc_get - take a reference to an ldisc | ||
788 | * @disc: ldisc number | ||
789 | * @ld: tty line discipline structure to use | ||
790 | * | ||
791 | * Takes a reference to a line discipline. Deals with refcounts and | ||
792 | * module locking counts. Returns NULL if the discipline is not available. | ||
793 | * Returns a pointer to the discipline and bumps the ref count if it is | ||
794 | * available | ||
795 | * | ||
796 | * Locking: | ||
797 | * takes tty_ldisc_lock to guard against ldisc races | ||
798 | */ | ||
799 | |||
800 | static int tty_ldisc_get(int disc, struct tty_ldisc *ld) | ||
801 | { | ||
802 | int err; | ||
803 | |||
804 | if (disc < N_TTY || disc >= NR_LDISCS) | ||
805 | return -EINVAL; | ||
806 | err = tty_ldisc_try_get(disc, ld); | ||
807 | if (err == -EAGAIN) { | ||
808 | request_module("tty-ldisc-%d", disc); | ||
809 | err = tty_ldisc_try_get(disc, ld); | ||
810 | } | ||
811 | return err; | ||
812 | } | ||
813 | |||
814 | /** | ||
815 | * tty_ldisc_put - drop ldisc reference | ||
816 | * @disc: ldisc number | ||
817 | * | ||
818 | * Drop a reference to a line discipline. Manage refcounts and | ||
819 | * module usage counts | ||
820 | * | ||
821 | * Locking: | ||
822 | * takes tty_ldisc_lock to guard against ldisc races | ||
823 | */ | ||
824 | |||
825 | static void tty_ldisc_put(struct tty_ldisc_ops *ld) | ||
826 | { | ||
827 | unsigned long flags; | ||
828 | int disc = ld->num; | ||
829 | |||
830 | BUG_ON(disc < N_TTY || disc >= NR_LDISCS); | ||
831 | |||
832 | spin_lock_irqsave(&tty_ldisc_lock, flags); | ||
833 | ld = tty_ldiscs[disc]; | ||
834 | BUG_ON(ld->refcount == 0); | ||
835 | ld->refcount--; | ||
836 | module_put(ld->owner); | ||
837 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); | ||
838 | } | ||
839 | |||
840 | static void * tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos) | ||
841 | { | ||
842 | return (*pos < NR_LDISCS) ? pos : NULL; | ||
843 | } | ||
844 | |||
845 | static void * tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos) | ||
846 | { | ||
847 | (*pos)++; | ||
848 | return (*pos < NR_LDISCS) ? pos : NULL; | ||
849 | } | ||
850 | |||
851 | static void tty_ldiscs_seq_stop(struct seq_file *m, void *v) | ||
852 | { | ||
853 | } | ||
854 | |||
855 | static int tty_ldiscs_seq_show(struct seq_file *m, void *v) | ||
856 | { | ||
857 | int i = *(loff_t *)v; | ||
858 | struct tty_ldisc ld; | ||
859 | |||
860 | if (tty_ldisc_get(i, &ld) < 0) | ||
861 | return 0; | ||
862 | seq_printf(m, "%-10s %2d\n", ld.ops->name ? ld.ops->name : "???", i); | ||
863 | tty_ldisc_put(ld.ops); | ||
864 | return 0; | ||
865 | } | ||
866 | |||
867 | static const struct seq_operations tty_ldiscs_seq_ops = { | ||
868 | .start = tty_ldiscs_seq_start, | ||
869 | .next = tty_ldiscs_seq_next, | ||
870 | .stop = tty_ldiscs_seq_stop, | ||
871 | .show = tty_ldiscs_seq_show, | ||
872 | }; | ||
873 | |||
874 | static int proc_tty_ldiscs_open(struct inode *inode, struct file *file) | ||
875 | { | ||
876 | return seq_open(file, &tty_ldiscs_seq_ops); | ||
877 | } | ||
878 | |||
879 | const struct file_operations tty_ldiscs_proc_fops = { | ||
880 | .owner = THIS_MODULE, | ||
881 | .open = proc_tty_ldiscs_open, | ||
882 | .read = seq_read, | ||
883 | .llseek = seq_lseek, | ||
884 | .release = seq_release, | ||
885 | }; | ||
886 | |||
887 | /** | ||
888 | * tty_ldisc_assign - set ldisc on a tty | ||
889 | * @tty: tty to assign | ||
890 | * @ld: line discipline | ||
891 | * | ||
892 | * Install an instance of a line discipline into a tty structure. The | ||
893 | * ldisc must have a reference count above zero to ensure it remains/ | ||
894 | * The tty instance refcount starts at zero. | ||
895 | * | ||
896 | * Locking: | ||
897 | * Caller must hold references | ||
898 | */ | ||
899 | |||
900 | static void tty_ldisc_assign(struct tty_struct *tty, struct tty_ldisc *ld) | ||
901 | { | ||
902 | ld->refcount = 0; | ||
903 | tty->ldisc = *ld; | ||
904 | } | ||
905 | |||
906 | /** | ||
907 | * tty_ldisc_try - internal helper | ||
908 | * @tty: the tty | ||
909 | * | ||
910 | * Make a single attempt to grab and bump the refcount on | ||
911 | * the tty ldisc. Return 0 on failure or 1 on success. This is | ||
912 | * used to implement both the waiting and non waiting versions | ||
913 | * of tty_ldisc_ref | ||
914 | * | ||
915 | * Locking: takes tty_ldisc_lock | ||
916 | */ | ||
917 | |||
918 | static int tty_ldisc_try(struct tty_struct *tty) | ||
919 | { | ||
920 | unsigned long flags; | ||
921 | struct tty_ldisc *ld; | ||
922 | int ret = 0; | ||
923 | |||
924 | spin_lock_irqsave(&tty_ldisc_lock, flags); | ||
925 | ld = &tty->ldisc; | ||
926 | if (test_bit(TTY_LDISC, &tty->flags)) { | ||
927 | ld->refcount++; | ||
928 | ret = 1; | ||
929 | } | ||
930 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); | ||
931 | return ret; | ||
932 | } | ||
933 | |||
934 | /** | ||
935 | * tty_ldisc_ref_wait - wait for the tty ldisc | ||
936 | * @tty: tty device | ||
937 | * | ||
938 | * Dereference the line discipline for the terminal and take a | ||
939 | * reference to it. If the line discipline is in flux then | ||
940 | * wait patiently until it changes. | ||
941 | * | ||
942 | * Note: Must not be called from an IRQ/timer context. The caller | ||
943 | * must also be careful not to hold other locks that will deadlock | ||
944 | * against a discipline change, such as an existing ldisc reference | ||
945 | * (which we check for) | ||
946 | * | ||
947 | * Locking: call functions take tty_ldisc_lock | ||
948 | */ | ||
949 | |||
950 | struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty) | ||
951 | { | ||
952 | /* wait_event is a macro */ | ||
953 | wait_event(tty_ldisc_wait, tty_ldisc_try(tty)); | ||
954 | if (tty->ldisc.refcount == 0) | ||
955 | printk(KERN_ERR "tty_ldisc_ref_wait\n"); | ||
956 | return &tty->ldisc; | ||
957 | } | ||
958 | |||
959 | EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait); | ||
960 | |||
961 | /** | ||
962 | * tty_ldisc_ref - get the tty ldisc | ||
963 | * @tty: tty device | ||
964 | * | ||
965 | * Dereference the line discipline for the terminal and take a | ||
966 | * reference to it. If the line discipline is in flux then | ||
967 | * return NULL. Can be called from IRQ and timer functions. | ||
968 | * | ||
969 | * Locking: called functions take tty_ldisc_lock | ||
970 | */ | ||
971 | |||
972 | struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty) | ||
973 | { | ||
974 | if (tty_ldisc_try(tty)) | ||
975 | return &tty->ldisc; | ||
976 | return NULL; | ||
977 | } | ||
978 | |||
979 | EXPORT_SYMBOL_GPL(tty_ldisc_ref); | ||
980 | |||
981 | /** | ||
982 | * tty_ldisc_deref - free a tty ldisc reference | ||
983 | * @ld: reference to free up | ||
984 | * | ||
985 | * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May | ||
986 | * be called in IRQ context. | ||
987 | * | ||
988 | * Locking: takes tty_ldisc_lock | ||
989 | */ | ||
990 | |||
991 | void tty_ldisc_deref(struct tty_ldisc *ld) | ||
992 | { | ||
993 | unsigned long flags; | ||
994 | |||
995 | BUG_ON(ld == NULL); | ||
996 | |||
997 | spin_lock_irqsave(&tty_ldisc_lock, flags); | ||
998 | if (ld->refcount == 0) | ||
999 | printk(KERN_ERR "tty_ldisc_deref: no references.\n"); | ||
1000 | else | ||
1001 | ld->refcount--; | ||
1002 | if (ld->refcount == 0) | ||
1003 | wake_up(&tty_ldisc_wait); | ||
1004 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); | ||
1005 | } | ||
1006 | |||
1007 | EXPORT_SYMBOL_GPL(tty_ldisc_deref); | ||
1008 | |||
1009 | /** | ||
1010 | * tty_ldisc_enable - allow ldisc use | ||
1011 | * @tty: terminal to activate ldisc on | ||
1012 | * | ||
1013 | * Set the TTY_LDISC flag when the line discipline can be called | ||
1014 | * again. Do necessary wakeups for existing sleepers. | ||
1015 | * | ||
1016 | * Note: nobody should set this bit except via this function. Clearing | ||
1017 | * directly is allowed. | ||
1018 | */ | ||
1019 | |||
1020 | static void tty_ldisc_enable(struct tty_struct *tty) | ||
1021 | { | ||
1022 | set_bit(TTY_LDISC, &tty->flags); | ||
1023 | wake_up(&tty_ldisc_wait); | ||
1024 | } | ||
1025 | |||
1026 | /** | ||
1027 | * tty_ldisc_restore - helper for tty ldisc change | ||
1028 | * @tty: tty to recover | ||
1029 | * @old: previous ldisc | ||
1030 | * | ||
1031 | * Restore the previous line discipline or N_TTY when a line discipline | ||
1032 | * change fails due to an open error | ||
1033 | */ | ||
1034 | |||
1035 | static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old) | ||
1036 | { | ||
1037 | char buf[64]; | ||
1038 | struct tty_ldisc new_ldisc; | ||
1039 | |||
1040 | /* There is an outstanding reference here so this is safe */ | ||
1041 | tty_ldisc_get(old->ops->num, old); | ||
1042 | tty_ldisc_assign(tty, old); | ||
1043 | tty_set_termios_ldisc(tty, old->ops->num); | ||
1044 | if (old->ops->open && (old->ops->open(tty) < 0)) { | ||
1045 | tty_ldisc_put(old->ops); | ||
1046 | /* This driver is always present */ | ||
1047 | if (tty_ldisc_get(N_TTY, &new_ldisc) < 0) | ||
1048 | panic("n_tty: get"); | ||
1049 | tty_ldisc_assign(tty, &new_ldisc); | ||
1050 | tty_set_termios_ldisc(tty, N_TTY); | ||
1051 | if (new_ldisc.ops->open) { | ||
1052 | int r = new_ldisc.ops->open(tty); | ||
1053 | if (r < 0) | ||
1054 | panic("Couldn't open N_TTY ldisc for " | ||
1055 | "%s --- error %d.", | ||
1056 | tty_name(tty, buf), r); | ||
1057 | } | ||
1058 | } | ||
1059 | } | ||
1060 | |||
1061 | /** | ||
1062 | * tty_set_ldisc - set line discipline | ||
1063 | * @tty: the terminal to set | ||
1064 | * @ldisc: the line discipline | ||
1065 | * | ||
1066 | * Set the discipline of a tty line. Must be called from a process | ||
1067 | * context. | ||
1068 | * | ||
1069 | * Locking: takes tty_ldisc_lock. | ||
1070 | * called functions take termios_mutex | ||
1071 | */ | ||
1072 | |||
1073 | static int tty_set_ldisc(struct tty_struct *tty, int ldisc) | ||
1074 | { | ||
1075 | int retval; | ||
1076 | struct tty_ldisc o_ldisc, new_ldisc; | ||
1077 | int work; | ||
1078 | unsigned long flags; | ||
1079 | struct tty_struct *o_tty; | ||
1080 | |||
1081 | restart: | ||
1082 | /* This is a bit ugly for now but means we can break the 'ldisc | ||
1083 | is part of the tty struct' assumption later */ | ||
1084 | retval = tty_ldisc_get(ldisc, &new_ldisc); | ||
1085 | if (retval) | ||
1086 | return retval; | ||
1087 | |||
1088 | /* | ||
1089 | * Problem: What do we do if this blocks ? | ||
1090 | */ | ||
1091 | |||
1092 | tty_wait_until_sent(tty, 0); | ||
1093 | |||
1094 | if (tty->ldisc.ops->num == ldisc) { | ||
1095 | tty_ldisc_put(new_ldisc.ops); | ||
1096 | return 0; | ||
1097 | } | ||
1098 | |||
1099 | /* | ||
1100 | * No more input please, we are switching. The new ldisc | ||
1101 | * will update this value in the ldisc open function | ||
1102 | */ | ||
1103 | |||
1104 | tty->receive_room = 0; | ||
1105 | |||
1106 | o_ldisc = tty->ldisc; | ||
1107 | o_tty = tty->link; | ||
1108 | |||
1109 | /* | ||
1110 | * Make sure we don't change while someone holds a | ||
1111 | * reference to the line discipline. The TTY_LDISC bit | ||
1112 | * prevents anyone taking a reference once it is clear. | ||
1113 | * We need the lock to avoid racing reference takers. | ||
1114 | */ | ||
1115 | |||
1116 | spin_lock_irqsave(&tty_ldisc_lock, flags); | ||
1117 | if (tty->ldisc.refcount || (o_tty && o_tty->ldisc.refcount)) { | ||
1118 | if (tty->ldisc.refcount) { | ||
1119 | /* Free the new ldisc we grabbed. Must drop the lock | ||
1120 | first. */ | ||
1121 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); | ||
1122 | tty_ldisc_put(o_ldisc.ops); | ||
1123 | /* | ||
1124 | * There are several reasons we may be busy, including | ||
1125 | * random momentary I/O traffic. We must therefore | ||
1126 | * retry. We could distinguish between blocking ops | ||
1127 | * and retries if we made tty_ldisc_wait() smarter. | ||
1128 | * That is up for discussion. | ||
1129 | */ | ||
1130 | if (wait_event_interruptible(tty_ldisc_wait, tty->ldisc.refcount == 0) < 0) | ||
1131 | return -ERESTARTSYS; | ||
1132 | goto restart; | ||
1133 | } | ||
1134 | if (o_tty && o_tty->ldisc.refcount) { | ||
1135 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); | ||
1136 | tty_ldisc_put(o_tty->ldisc.ops); | ||
1137 | if (wait_event_interruptible(tty_ldisc_wait, o_tty->ldisc.refcount == 0) < 0) | ||
1138 | return -ERESTARTSYS; | ||
1139 | goto restart; | ||
1140 | } | ||
1141 | } | ||
1142 | /* | ||
1143 | * If the TTY_LDISC bit is set, then we are racing against | ||
1144 | * another ldisc change | ||
1145 | */ | ||
1146 | if (!test_bit(TTY_LDISC, &tty->flags)) { | ||
1147 | struct tty_ldisc *ld; | ||
1148 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); | ||
1149 | tty_ldisc_put(new_ldisc.ops); | ||
1150 | ld = tty_ldisc_ref_wait(tty); | ||
1151 | tty_ldisc_deref(ld); | ||
1152 | goto restart; | ||
1153 | } | ||
1154 | |||
1155 | clear_bit(TTY_LDISC, &tty->flags); | ||
1156 | if (o_tty) | ||
1157 | clear_bit(TTY_LDISC, &o_tty->flags); | ||
1158 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); | ||
1159 | |||
1160 | /* | ||
1161 | * From this point on we know nobody has an ldisc | ||
1162 | * usage reference, nor can they obtain one until | ||
1163 | * we say so later on. | ||
1164 | */ | ||
1165 | |||
1166 | work = cancel_delayed_work(&tty->buf.work); | ||
1167 | /* | ||
1168 | * Wait for ->hangup_work and ->buf.work handlers to terminate | ||
1169 | * MUST NOT hold locks here. | ||
1170 | */ | ||
1171 | flush_scheduled_work(); | ||
1172 | /* Shutdown the current discipline. */ | ||
1173 | if (o_ldisc.ops->close) | ||
1174 | (o_ldisc.ops->close)(tty); | ||
1175 | |||
1176 | /* Now set up the new line discipline. */ | ||
1177 | tty_ldisc_assign(tty, &new_ldisc); | ||
1178 | tty_set_termios_ldisc(tty, ldisc); | ||
1179 | if (new_ldisc.ops->open) | ||
1180 | retval = (new_ldisc.ops->open)(tty); | ||
1181 | if (retval < 0) { | ||
1182 | tty_ldisc_put(new_ldisc.ops); | ||
1183 | tty_ldisc_restore(tty, &o_ldisc); | ||
1184 | } | ||
1185 | /* At this point we hold a reference to the new ldisc and a | ||
1186 | a reference to the old ldisc. If we ended up flipping back | ||
1187 | to the existing ldisc we have two references to it */ | ||
1188 | |||
1189 | if (tty->ldisc.ops->num != o_ldisc.ops->num && tty->ops->set_ldisc) | ||
1190 | tty->ops->set_ldisc(tty); | ||
1191 | |||
1192 | tty_ldisc_put(o_ldisc.ops); | ||
1193 | |||
1194 | /* | ||
1195 | * Allow ldisc referencing to occur as soon as the driver | ||
1196 | * ldisc callback completes. | ||
1197 | */ | ||
1198 | |||
1199 | tty_ldisc_enable(tty); | ||
1200 | if (o_tty) | ||
1201 | tty_ldisc_enable(o_tty); | ||
1202 | |||
1203 | /* Restart it in case no characters kick it off. Safe if | ||
1204 | already running */ | ||
1205 | if (work) | ||
1206 | schedule_delayed_work(&tty->buf.work, 1); | ||
1207 | return retval; | ||
1208 | } | ||
1209 | |||
1210 | /** | ||
1211 | * get_tty_driver - find device of a tty | 659 | * get_tty_driver - find device of a tty |
1212 | * @dev_t: device identifier | 660 | * @dev_t: device identifier |
1213 | * @index: returns the index of the tty | 661 | * @index: returns the index of the tty |
@@ -2193,7 +1641,6 @@ static int init_dev(struct tty_driver *driver, int idx, | |||
2193 | struct ktermios *tp, **tp_loc, *o_tp, **o_tp_loc; | 1641 | struct ktermios *tp, **tp_loc, *o_tp, **o_tp_loc; |
2194 | struct ktermios *ltp, **ltp_loc, *o_ltp, **o_ltp_loc; | 1642 | struct ktermios *ltp, **ltp_loc, *o_ltp, **o_ltp_loc; |
2195 | int retval = 0; | 1643 | int retval = 0; |
2196 | struct tty_ldisc *ld; | ||
2197 | 1644 | ||
2198 | /* check whether we're reopening an existing tty */ | 1645 | /* check whether we're reopening an existing tty */ |
2199 | if (driver->flags & TTY_DRIVER_DEVPTS_MEM) { | 1646 | if (driver->flags & TTY_DRIVER_DEVPTS_MEM) { |
@@ -2342,25 +1789,12 @@ static int init_dev(struct tty_driver *driver, int idx, | |||
2342 | * If we fail here just call release_tty to clean up. No need | 1789 | * If we fail here just call release_tty to clean up. No need |
2343 | * to decrement the use counts, as release_tty doesn't care. | 1790 | * to decrement the use counts, as release_tty doesn't care. |
2344 | */ | 1791 | */ |
2345 | |||
2346 | ld = &tty->ldisc; | ||
2347 | 1792 | ||
2348 | if (ld->ops->open) { | 1793 | retval = tty_ldisc_setup(tty, o_tty); |
2349 | retval = (ld->ops->open)(tty); | 1794 | |
2350 | if (retval) | 1795 | if (retval) |
2351 | goto release_mem_out; | 1796 | goto release_mem_out; |
2352 | } | 1797 | goto success; |
2353 | if (o_tty && o_tty->ldisc.ops->open) { | ||
2354 | retval = (o_tty->ldisc.ops->open)(o_tty); | ||
2355 | if (retval) { | ||
2356 | if (ld->ops->close) | ||
2357 | (ld->ops->close)(tty); | ||
2358 | goto release_mem_out; | ||
2359 | } | ||
2360 | tty_ldisc_enable(o_tty); | ||
2361 | } | ||
2362 | tty_ldisc_enable(tty); | ||
2363 | goto success; | ||
2364 | 1798 | ||
2365 | /* | 1799 | /* |
2366 | * This fast open can be used if the tty is already open. | 1800 | * This fast open can be used if the tty is already open. |
@@ -2498,12 +1932,10 @@ static void release_tty(struct tty_struct *tty, int idx) | |||
2498 | static void release_dev(struct file *filp) | 1932 | static void release_dev(struct file *filp) |
2499 | { | 1933 | { |
2500 | struct tty_struct *tty, *o_tty; | 1934 | struct tty_struct *tty, *o_tty; |
2501 | struct tty_ldisc ld; | ||
2502 | int pty_master, tty_closing, o_tty_closing, do_sleep; | 1935 | int pty_master, tty_closing, o_tty_closing, do_sleep; |
2503 | int devpts; | 1936 | int devpts; |
2504 | int idx; | 1937 | int idx; |
2505 | char buf[64]; | 1938 | char buf[64]; |
2506 | unsigned long flags; | ||
2507 | 1939 | ||
2508 | tty = (struct tty_struct *)filp->private_data; | 1940 | tty = (struct tty_struct *)filp->private_data; |
2509 | if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, | 1941 | if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, |
@@ -2705,56 +2137,9 @@ static void release_dev(struct file *filp) | |||
2705 | printk(KERN_DEBUG "freeing tty structure..."); | 2137 | printk(KERN_DEBUG "freeing tty structure..."); |
2706 | #endif | 2138 | #endif |
2707 | /* | 2139 | /* |
2708 | * Prevent flush_to_ldisc() from rescheduling the work for later. Then | 2140 | * Ask the line discipline code to release its structures |
2709 | * kill any delayed work. As this is the final close it does not | ||
2710 | * race with the set_ldisc code path. | ||
2711 | */ | 2141 | */ |
2712 | clear_bit(TTY_LDISC, &tty->flags); | 2142 | tty_ldisc_release(tty, o_tty); |
2713 | cancel_delayed_work(&tty->buf.work); | ||
2714 | |||
2715 | /* | ||
2716 | * Wait for ->hangup_work and ->buf.work handlers to terminate | ||
2717 | */ | ||
2718 | |||
2719 | flush_scheduled_work(); | ||
2720 | |||
2721 | /* | ||
2722 | * Wait for any short term users (we know they are just driver | ||
2723 | * side waiters as the file is closing so user count on the file | ||
2724 | * side is zero. | ||
2725 | */ | ||
2726 | spin_lock_irqsave(&tty_ldisc_lock, flags); | ||
2727 | while (tty->ldisc.refcount) { | ||
2728 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); | ||
2729 | wait_event(tty_ldisc_wait, tty->ldisc.refcount == 0); | ||
2730 | spin_lock_irqsave(&tty_ldisc_lock, flags); | ||
2731 | } | ||
2732 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); | ||
2733 | /* | ||
2734 | * Shutdown the current line discipline, and reset it to N_TTY. | ||
2735 | * | ||
2736 | * FIXME: this MUST get fixed for the new reflocking | ||
2737 | */ | ||
2738 | if (tty->ldisc.ops->close) | ||
2739 | (tty->ldisc.ops->close)(tty); | ||
2740 | tty_ldisc_put(tty->ldisc.ops); | ||
2741 | |||
2742 | /* | ||
2743 | * Switch the line discipline back | ||
2744 | */ | ||
2745 | WARN_ON(tty_ldisc_get(N_TTY, &ld)); | ||
2746 | tty_ldisc_assign(tty, &ld); | ||
2747 | tty_set_termios_ldisc(tty, N_TTY); | ||
2748 | if (o_tty) { | ||
2749 | /* FIXME: could o_tty be in setldisc here ? */ | ||
2750 | clear_bit(TTY_LDISC, &o_tty->flags); | ||
2751 | if (o_tty->ldisc.ops->close) | ||
2752 | (o_tty->ldisc.ops->close)(o_tty); | ||
2753 | tty_ldisc_put(o_tty->ldisc.ops); | ||
2754 | WARN_ON(tty_ldisc_get(N_TTY, &ld)); | ||
2755 | tty_ldisc_assign(o_tty, &ld); | ||
2756 | tty_set_termios_ldisc(o_tty, N_TTY); | ||
2757 | } | ||
2758 | /* | 2143 | /* |
2759 | * The release_tty function takes care of the details of clearing | 2144 | * The release_tty function takes care of the details of clearing |
2760 | * the slots and preserving the termios structure. | 2145 | * the slots and preserving the termios structure. |
@@ -3464,16 +2849,29 @@ static int tiocsetd(struct tty_struct *tty, int __user *p) | |||
3464 | 2849 | ||
3465 | static int send_break(struct tty_struct *tty, unsigned int duration) | 2850 | static int send_break(struct tty_struct *tty, unsigned int duration) |
3466 | { | 2851 | { |
3467 | if (tty_write_lock(tty, 0) < 0) | 2852 | int retval; |
3468 | return -EINTR; | 2853 | |
3469 | tty->ops->break_ctl(tty, -1); | 2854 | if (tty->ops->break_ctl == NULL) |
3470 | if (!signal_pending(current)) | 2855 | return 0; |
3471 | msleep_interruptible(duration); | 2856 | |
3472 | tty->ops->break_ctl(tty, 0); | 2857 | if (tty->driver->flags & TTY_DRIVER_HARDWARE_BREAK) |
3473 | tty_write_unlock(tty); | 2858 | retval = tty->ops->break_ctl(tty, duration); |
3474 | if (signal_pending(current)) | 2859 | else { |
3475 | return -EINTR; | 2860 | /* Do the work ourselves */ |
3476 | return 0; | 2861 | if (tty_write_lock(tty, 0) < 0) |
2862 | return -EINTR; | ||
2863 | retval = tty->ops->break_ctl(tty, -1); | ||
2864 | if (retval) | ||
2865 | goto out; | ||
2866 | if (!signal_pending(current)) | ||
2867 | msleep_interruptible(duration); | ||
2868 | retval = tty->ops->break_ctl(tty, 0); | ||
2869 | out: | ||
2870 | tty_write_unlock(tty); | ||
2871 | if (signal_pending(current)) | ||
2872 | retval = -EINTR; | ||
2873 | } | ||
2874 | return retval; | ||
3477 | } | 2875 | } |
3478 | 2876 | ||
3479 | /** | 2877 | /** |
@@ -3564,36 +2962,6 @@ long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | |||
3564 | tty->driver->subtype == PTY_TYPE_MASTER) | 2962 | tty->driver->subtype == PTY_TYPE_MASTER) |
3565 | real_tty = tty->link; | 2963 | real_tty = tty->link; |
3566 | 2964 | ||
3567 | /* | ||
3568 | * Break handling by driver | ||
3569 | */ | ||
3570 | |||
3571 | retval = -EINVAL; | ||
3572 | |||
3573 | if (!tty->ops->break_ctl) { | ||
3574 | switch (cmd) { | ||
3575 | case TIOCSBRK: | ||
3576 | case TIOCCBRK: | ||
3577 | if (tty->ops->ioctl) | ||
3578 | retval = tty->ops->ioctl(tty, file, cmd, arg); | ||
3579 | if (retval != -EINVAL && retval != -ENOIOCTLCMD) | ||
3580 | printk(KERN_WARNING "tty: driver %s needs updating to use break_ctl\n", tty->driver->name); | ||
3581 | return retval; | ||
3582 | |||
3583 | /* These two ioctl's always return success; even if */ | ||
3584 | /* the driver doesn't support them. */ | ||
3585 | case TCSBRK: | ||
3586 | case TCSBRKP: | ||
3587 | if (!tty->ops->ioctl) | ||
3588 | return 0; | ||
3589 | retval = tty->ops->ioctl(tty, file, cmd, arg); | ||
3590 | if (retval != -EINVAL && retval != -ENOIOCTLCMD) | ||
3591 | printk(KERN_WARNING "tty: driver %s needs updating to use break_ctl\n", tty->driver->name); | ||
3592 | if (retval == -ENOIOCTLCMD) | ||
3593 | retval = 0; | ||
3594 | return retval; | ||
3595 | } | ||
3596 | } | ||
3597 | 2965 | ||
3598 | /* | 2966 | /* |
3599 | * Factor out some common prep work | 2967 | * Factor out some common prep work |
@@ -3615,6 +2983,9 @@ long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | |||
3615 | break; | 2983 | break; |
3616 | } | 2984 | } |
3617 | 2985 | ||
2986 | /* | ||
2987 | * Now do the stuff. | ||
2988 | */ | ||
3618 | switch (cmd) { | 2989 | switch (cmd) { |
3619 | case TIOCSTI: | 2990 | case TIOCSTI: |
3620 | return tiocsti(tty, p); | 2991 | return tiocsti(tty, p); |
@@ -3658,12 +3029,11 @@ long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | |||
3658 | */ | 3029 | */ |
3659 | case TIOCSBRK: /* Turn break on, unconditionally */ | 3030 | case TIOCSBRK: /* Turn break on, unconditionally */ |
3660 | if (tty->ops->break_ctl) | 3031 | if (tty->ops->break_ctl) |
3661 | tty->ops->break_ctl(tty, -1); | 3032 | return tty->ops->break_ctl(tty, -1); |
3662 | return 0; | 3033 | return 0; |
3663 | |||
3664 | case TIOCCBRK: /* Turn break off, unconditionally */ | 3034 | case TIOCCBRK: /* Turn break off, unconditionally */ |
3665 | if (tty->ops->break_ctl) | 3035 | if (tty->ops->break_ctl) |
3666 | tty->ops->break_ctl(tty, 0); | 3036 | return tty->ops->break_ctl(tty, 0); |
3667 | return 0; | 3037 | return 0; |
3668 | case TCSBRK: /* SVID version: non-zero arg --> no break */ | 3038 | case TCSBRK: /* SVID version: non-zero arg --> no break */ |
3669 | /* non-zero arg means wait for all output data | 3039 | /* non-zero arg means wait for all output data |
@@ -3962,12 +3332,9 @@ EXPORT_SYMBOL(tty_flip_buffer_push); | |||
3962 | 3332 | ||
3963 | static void initialize_tty_struct(struct tty_struct *tty) | 3333 | static void initialize_tty_struct(struct tty_struct *tty) |
3964 | { | 3334 | { |
3965 | struct tty_ldisc ld; | ||
3966 | memset(tty, 0, sizeof(struct tty_struct)); | 3335 | memset(tty, 0, sizeof(struct tty_struct)); |
3967 | tty->magic = TTY_MAGIC; | 3336 | tty->magic = TTY_MAGIC; |
3968 | if (tty_ldisc_get(N_TTY, &ld) < 0) | 3337 | tty_ldisc_init(tty); |
3969 | panic("n_tty: init_tty"); | ||
3970 | tty_ldisc_assign(tty, &ld); | ||
3971 | tty->session = NULL; | 3338 | tty->session = NULL; |
3972 | tty->pgrp = NULL; | 3339 | tty->pgrp = NULL; |
3973 | tty->overrun_time = jiffies; | 3340 | tty->overrun_time = jiffies; |
@@ -4045,7 +3412,7 @@ struct device *tty_register_device(struct tty_driver *driver, unsigned index, | |||
4045 | else | 3412 | else |
4046 | tty_line_name(driver, index, name); | 3413 | tty_line_name(driver, index, name); |
4047 | 3414 | ||
4048 | return device_create(tty_class, device, dev, name); | 3415 | return device_create_drvdata(tty_class, device, dev, NULL, name); |
4049 | } | 3416 | } |
4050 | 3417 | ||
4051 | /** | 3418 | /** |
@@ -4280,7 +3647,7 @@ void __init console_init(void) | |||
4280 | initcall_t *call; | 3647 | initcall_t *call; |
4281 | 3648 | ||
4282 | /* Setup the default TTY line discipline. */ | 3649 | /* Setup the default TTY line discipline. */ |
4283 | (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY); | 3650 | tty_ldisc_begin(); |
4284 | 3651 | ||
4285 | /* | 3652 | /* |
4286 | * set up the console device so that later boot sequences can | 3653 | * set up the console device so that later boot sequences can |
@@ -4323,20 +3690,22 @@ static int __init tty_init(void) | |||
4323 | if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) || | 3690 | if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) || |
4324 | register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0) | 3691 | register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0) |
4325 | panic("Couldn't register /dev/tty driver\n"); | 3692 | panic("Couldn't register /dev/tty driver\n"); |
4326 | device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), "tty"); | 3693 | device_create_drvdata(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), NULL, |
3694 | "tty"); | ||
4327 | 3695 | ||
4328 | cdev_init(&console_cdev, &console_fops); | 3696 | cdev_init(&console_cdev, &console_fops); |
4329 | if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) || | 3697 | if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) || |
4330 | register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0) | 3698 | register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0) |
4331 | panic("Couldn't register /dev/console driver\n"); | 3699 | panic("Couldn't register /dev/console driver\n"); |
4332 | device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 1), "console"); | 3700 | device_create_drvdata(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 1), NULL, |
3701 | "console"); | ||
4333 | 3702 | ||
4334 | #ifdef CONFIG_UNIX98_PTYS | 3703 | #ifdef CONFIG_UNIX98_PTYS |
4335 | cdev_init(&ptmx_cdev, &ptmx_fops); | 3704 | cdev_init(&ptmx_cdev, &ptmx_fops); |
4336 | if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) || | 3705 | if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) || |
4337 | register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0) | 3706 | register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0) |
4338 | panic("Couldn't register /dev/ptmx driver\n"); | 3707 | panic("Couldn't register /dev/ptmx driver\n"); |
4339 | device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), "ptmx"); | 3708 | device_create_drvdata(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), NULL, "ptmx"); |
4340 | #endif | 3709 | #endif |
4341 | 3710 | ||
4342 | #ifdef CONFIG_VT | 3711 | #ifdef CONFIG_VT |
@@ -4344,7 +3713,7 @@ static int __init tty_init(void) | |||
4344 | if (cdev_add(&vc0_cdev, MKDEV(TTY_MAJOR, 0), 1) || | 3713 | if (cdev_add(&vc0_cdev, MKDEV(TTY_MAJOR, 0), 1) || |
4345 | register_chrdev_region(MKDEV(TTY_MAJOR, 0), 1, "/dev/vc/0") < 0) | 3714 | register_chrdev_region(MKDEV(TTY_MAJOR, 0), 1, "/dev/vc/0") < 0) |
4346 | panic("Couldn't register /dev/tty0 driver\n"); | 3715 | panic("Couldn't register /dev/tty0 driver\n"); |
4347 | device_create(tty_class, NULL, MKDEV(TTY_MAJOR, 0), "tty0"); | 3716 | device_create_drvdata(tty_class, NULL, MKDEV(TTY_MAJOR, 0), NULL, "tty0"); |
4348 | 3717 | ||
4349 | vty_init(); | 3718 | vty_init(); |
4350 | #endif | 3719 | #endif |