Greg Kroah-Hartman | e3b3d0f | 2017-11-06 18:11:51 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 2 | #include <linux/types.h> |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 3 | #include <linux/errno.h> |
Jiri Slaby | 8b3ffa1 | 2011-11-16 16:27:10 +0100 | [diff] [blame] | 4 | #include <linux/kmod.h> |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 5 | #include <linux/sched.h> |
| 6 | #include <linux/interrupt.h> |
| 7 | #include <linux/tty.h> |
| 8 | #include <linux/tty_driver.h> |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 9 | #include <linux/file.h> |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 10 | #include <linux/mm.h> |
| 11 | #include <linux/string.h> |
| 12 | #include <linux/slab.h> |
| 13 | #include <linux/poll.h> |
| 14 | #include <linux/proc_fs.h> |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 15 | #include <linux/module.h> |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 16 | #include <linux/device.h> |
| 17 | #include <linux/wait.h> |
| 18 | #include <linux/bitops.h> |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 19 | #include <linux/seq_file.h> |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 20 | #include <linux/uaccess.h> |
Jiri Slaby | 0c73c08 | 2011-11-16 16:27:09 +0100 | [diff] [blame] | 21 | #include <linux/ratelimit.h> |
Greg Kroah-Hartman | 98602c0 | 2021-04-08 14:51:22 +0200 | [diff] [blame] | 22 | #include "tty.h" |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 23 | |
Peter Hurley | fc575ee | 2013-03-11 16:44:38 -0400 | [diff] [blame] | 24 | #undef LDISC_DEBUG_HANGUP |
| 25 | |
| 26 | #ifdef LDISC_DEBUG_HANGUP |
Peter Hurley | 0a6adc1 | 2015-07-12 22:49:10 -0400 | [diff] [blame] | 27 | #define tty_ldisc_debug(tty, f, args...) tty_debug(tty, f, ##args) |
Peter Hurley | fc575ee | 2013-03-11 16:44:38 -0400 | [diff] [blame] | 28 | #else |
| 29 | #define tty_ldisc_debug(tty, f, args...) |
| 30 | #endif |
| 31 | |
Peter Hurley | d2c4389 | 2013-06-15 07:04:47 -0400 | [diff] [blame] | 32 | /* lockdep nested classes for tty->ldisc_sem */ |
| 33 | enum { |
| 34 | LDISC_SEM_NORMAL, |
| 35 | LDISC_SEM_OTHER, |
| 36 | }; |
| 37 | |
| 38 | |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 39 | /* |
| 40 | * This guards the refcounted line discipline lists. The lock |
| 41 | * must be taken with irqs off because there are hangup path |
| 42 | * callers who will do ldisc lookups and cannot sleep. |
| 43 | */ |
| 44 | |
Peter Hurley | 137084b | 2013-06-15 07:04:46 -0400 | [diff] [blame] | 45 | static DEFINE_RAW_SPINLOCK(tty_ldiscs_lock); |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 46 | /* Line disc dispatch table */ |
| 47 | static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS]; |
| 48 | |
| 49 | /** |
| 50 | * tty_register_ldisc - install a line discipline |
| 51 | * @disc: ldisc number |
| 52 | * @new_ldisc: pointer to the ldisc object |
| 53 | * |
| 54 | * Installs a new line discipline into the kernel. The discipline |
| 55 | * is set up as unreferenced and then made available to the kernel |
| 56 | * from this point onwards. |
| 57 | * |
| 58 | * Locking: |
Peter Hurley | 137084b | 2013-06-15 07:04:46 -0400 | [diff] [blame] | 59 | * takes tty_ldiscs_lock to guard against ldisc races |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 60 | */ |
| 61 | |
Jiri Slaby | fbadf70a | 2021-05-05 11:19:07 +0200 | [diff] [blame] | 62 | int tty_register_ldisc(struct tty_ldisc_ops *new_ldisc) |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 63 | { |
| 64 | unsigned long flags; |
| 65 | int ret = 0; |
| 66 | |
Jiri Slaby | fbadf70a | 2021-05-05 11:19:07 +0200 | [diff] [blame] | 67 | if (new_ldisc->num < N_TTY || new_ldisc->num >= NR_LDISCS) |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 68 | return -EINVAL; |
| 69 | |
Peter Hurley | 137084b | 2013-06-15 07:04:46 -0400 | [diff] [blame] | 70 | raw_spin_lock_irqsave(&tty_ldiscs_lock, flags); |
Jiri Slaby | fbadf70a | 2021-05-05 11:19:07 +0200 | [diff] [blame] | 71 | tty_ldiscs[new_ldisc->num] = new_ldisc; |
Peter Hurley | 137084b | 2013-06-15 07:04:46 -0400 | [diff] [blame] | 72 | raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags); |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 73 | |
| 74 | return ret; |
| 75 | } |
| 76 | EXPORT_SYMBOL(tty_register_ldisc); |
| 77 | |
| 78 | /** |
| 79 | * tty_unregister_ldisc - unload a line discipline |
| 80 | * @disc: ldisc number |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 81 | * |
| 82 | * Remove a line discipline from the kernel providing it is not |
| 83 | * currently in use. |
| 84 | * |
| 85 | * Locking: |
Peter Hurley | 137084b | 2013-06-15 07:04:46 -0400 | [diff] [blame] | 86 | * takes tty_ldiscs_lock to guard against ldisc races |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 87 | */ |
| 88 | |
Jiri Slaby | f81ee8b | 2021-05-05 11:19:09 +0200 | [diff] [blame] | 89 | int tty_unregister_ldisc(struct tty_ldisc_ops *ldisc) |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 90 | { |
| 91 | unsigned long flags; |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 92 | |
Peter Hurley | 137084b | 2013-06-15 07:04:46 -0400 | [diff] [blame] | 93 | raw_spin_lock_irqsave(&tty_ldiscs_lock, flags); |
Jiri Slaby | 1947520 | 2021-05-05 11:19:10 +0200 | [diff] [blame] | 94 | tty_ldiscs[ldisc->num] = NULL; |
Peter Hurley | 137084b | 2013-06-15 07:04:46 -0400 | [diff] [blame] | 95 | raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags); |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 96 | |
Jiri Slaby | 1947520 | 2021-05-05 11:19:10 +0200 | [diff] [blame] | 97 | return 0; |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 98 | } |
| 99 | EXPORT_SYMBOL(tty_unregister_ldisc); |
| 100 | |
Linus Torvalds | f0de0e8 | 2009-08-03 16:00:15 -0700 | [diff] [blame] | 101 | static struct tty_ldisc_ops *get_ldops(int disc) |
| 102 | { |
| 103 | unsigned long flags; |
| 104 | struct tty_ldisc_ops *ldops, *ret; |
| 105 | |
Peter Hurley | 137084b | 2013-06-15 07:04:46 -0400 | [diff] [blame] | 106 | raw_spin_lock_irqsave(&tty_ldiscs_lock, flags); |
Linus Torvalds | f0de0e8 | 2009-08-03 16:00:15 -0700 | [diff] [blame] | 107 | ret = ERR_PTR(-EINVAL); |
| 108 | ldops = tty_ldiscs[disc]; |
| 109 | if (ldops) { |
| 110 | ret = ERR_PTR(-EAGAIN); |
Jiri Slaby | 1947520 | 2021-05-05 11:19:10 +0200 | [diff] [blame] | 111 | if (try_module_get(ldops->owner)) |
Linus Torvalds | f0de0e8 | 2009-08-03 16:00:15 -0700 | [diff] [blame] | 112 | ret = ldops; |
Linus Torvalds | f0de0e8 | 2009-08-03 16:00:15 -0700 | [diff] [blame] | 113 | } |
Peter Hurley | 137084b | 2013-06-15 07:04:46 -0400 | [diff] [blame] | 114 | raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags); |
Linus Torvalds | f0de0e8 | 2009-08-03 16:00:15 -0700 | [diff] [blame] | 115 | return ret; |
| 116 | } |
| 117 | |
| 118 | static void put_ldops(struct tty_ldisc_ops *ldops) |
| 119 | { |
| 120 | unsigned long flags; |
| 121 | |
Peter Hurley | 137084b | 2013-06-15 07:04:46 -0400 | [diff] [blame] | 122 | raw_spin_lock_irqsave(&tty_ldiscs_lock, flags); |
Linus Torvalds | f0de0e8 | 2009-08-03 16:00:15 -0700 | [diff] [blame] | 123 | module_put(ldops->owner); |
Peter Hurley | 137084b | 2013-06-15 07:04:46 -0400 | [diff] [blame] | 124 | raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags); |
Linus Torvalds | f0de0e8 | 2009-08-03 16:00:15 -0700 | [diff] [blame] | 125 | } |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 126 | |
Lee Jones | 8eddcca | 2020-11-12 10:58:54 +0000 | [diff] [blame] | 127 | static int tty_ldisc_autoload = IS_BUILTIN(CONFIG_LDISC_AUTOLOAD); |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 128 | /** |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 129 | * tty_ldisc_get - take a reference to an ldisc |
Lee Jones | 8a3bdec | 2020-11-04 19:35:42 +0000 | [diff] [blame] | 130 | * @tty: tty device |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 131 | * @disc: ldisc number |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 132 | * |
| 133 | * Takes a reference to a line discipline. Deals with refcounts and |
Peter Hurley | c0cc1c5 | 2016-01-10 22:40:59 -0800 | [diff] [blame] | 134 | * module locking counts. |
| 135 | * |
| 136 | * Returns: -EINVAL if the discipline index is not [N_TTY..NR_LDISCS] or |
| 137 | * if the discipline is not registered |
| 138 | * -EAGAIN if request_module() failed to load or register the |
Xiaofei Tan | b895854 | 2021-04-07 15:06:50 +0800 | [diff] [blame] | 139 | * discipline |
Peter Hurley | c0cc1c5 | 2016-01-10 22:40:59 -0800 | [diff] [blame] | 140 | * -ENOMEM if allocation failure |
| 141 | * |
| 142 | * Otherwise, returns a pointer to the discipline and bumps the |
| 143 | * ref count |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 144 | * |
| 145 | * Locking: |
Peter Hurley | 137084b | 2013-06-15 07:04:46 -0400 | [diff] [blame] | 146 | * takes tty_ldiscs_lock to guard against ldisc races |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 147 | */ |
| 148 | |
Peter Hurley | 3669752 | 2013-06-15 07:04:48 -0400 | [diff] [blame] | 149 | static struct tty_ldisc *tty_ldisc_get(struct tty_struct *tty, int disc) |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 150 | { |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 151 | struct tty_ldisc *ld; |
Linus Torvalds | 182274f | 2009-08-03 16:01:28 -0700 | [diff] [blame] | 152 | struct tty_ldisc_ops *ldops; |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 153 | |
| 154 | if (disc < N_TTY || disc >= NR_LDISCS) |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 155 | return ERR_PTR(-EINVAL); |
Linus Torvalds | 182274f | 2009-08-03 16:01:28 -0700 | [diff] [blame] | 156 | |
| 157 | /* |
| 158 | * Get the ldisc ops - we may need to request them to be loaded |
| 159 | * dynamically and try again. |
| 160 | */ |
| 161 | ldops = get_ldops(disc); |
| 162 | if (IS_ERR(ldops)) { |
Greg Kroah-Hartman | 7c0cca7 | 2019-01-21 17:26:42 +0100 | [diff] [blame] | 163 | if (!capable(CAP_SYS_MODULE) && !tty_ldisc_autoload) |
| 164 | return ERR_PTR(-EPERM); |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 165 | request_module("tty-ldisc-%d", disc); |
Linus Torvalds | 182274f | 2009-08-03 16:01:28 -0700 | [diff] [blame] | 166 | ldops = get_ldops(disc); |
| 167 | if (IS_ERR(ldops)) |
| 168 | return ERR_CAST(ldops); |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 169 | } |
Linus Torvalds | 182274f | 2009-08-03 16:01:28 -0700 | [diff] [blame] | 170 | |
Tetsuo Handa | bcdd0ca | 2018-04-25 20:12:31 +0900 | [diff] [blame] | 171 | /* |
| 172 | * There is no way to handle allocation failure of only 16 bytes. |
| 173 | * Let's simplify error handling and save more memory. |
| 174 | */ |
| 175 | ld = kmalloc(sizeof(struct tty_ldisc), GFP_KERNEL | __GFP_NOFAIL); |
Linus Torvalds | 182274f | 2009-08-03 16:01:28 -0700 | [diff] [blame] | 176 | ld->ops = ldops; |
Peter Hurley | 3669752 | 2013-06-15 07:04:48 -0400 | [diff] [blame] | 177 | ld->tty = tty; |
Ivo Sieben | 1541f84 | 2012-05-03 14:37:43 +0200 | [diff] [blame] | 178 | |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 179 | return ld; |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 180 | } |
| 181 | |
Lee Jones | 8eddcca | 2020-11-12 10:58:54 +0000 | [diff] [blame] | 182 | /* |
Peter Hurley | 734de24 | 2013-03-11 16:44:43 -0400 | [diff] [blame] | 183 | * tty_ldisc_put - release the ldisc |
| 184 | * |
| 185 | * Complement of tty_ldisc_get(). |
| 186 | */ |
Denys Vlasenko | cb128f6 | 2015-10-27 17:40:02 +0100 | [diff] [blame] | 187 | static void tty_ldisc_put(struct tty_ldisc *ld) |
Peter Hurley | 734de24 | 2013-03-11 16:44:43 -0400 | [diff] [blame] | 188 | { |
Peter Hurley | 734de24 | 2013-03-11 16:44:43 -0400 | [diff] [blame] | 189 | if (WARN_ON_ONCE(!ld)) |
| 190 | return; |
| 191 | |
Peter Hurley | 3669752 | 2013-06-15 07:04:48 -0400 | [diff] [blame] | 192 | put_ldops(ld->ops); |
Peter Hurley | 734de24 | 2013-03-11 16:44:43 -0400 | [diff] [blame] | 193 | kfree(ld); |
Peter Hurley | 734de24 | 2013-03-11 16:44:43 -0400 | [diff] [blame] | 194 | } |
| 195 | |
Alan Cox | 852e99d | 2009-06-11 12:51:41 +0100 | [diff] [blame] | 196 | static void *tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos) |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 197 | { |
| 198 | return (*pos < NR_LDISCS) ? pos : NULL; |
| 199 | } |
| 200 | |
Alan Cox | 852e99d | 2009-06-11 12:51:41 +0100 | [diff] [blame] | 201 | static void *tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos) |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 202 | { |
| 203 | (*pos)++; |
| 204 | return (*pos < NR_LDISCS) ? pos : NULL; |
| 205 | } |
| 206 | |
| 207 | static void tty_ldiscs_seq_stop(struct seq_file *m, void *v) |
| 208 | { |
| 209 | } |
| 210 | |
| 211 | static int tty_ldiscs_seq_show(struct seq_file *m, void *v) |
| 212 | { |
| 213 | int i = *(loff_t *)v; |
Linus Torvalds | f0de0e8 | 2009-08-03 16:00:15 -0700 | [diff] [blame] | 214 | struct tty_ldisc_ops *ldops; |
Alan Cox | 852e99d | 2009-06-11 12:51:41 +0100 | [diff] [blame] | 215 | |
Linus Torvalds | f0de0e8 | 2009-08-03 16:00:15 -0700 | [diff] [blame] | 216 | ldops = get_ldops(i); |
| 217 | if (IS_ERR(ldops)) |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 218 | return 0; |
Linus Torvalds | f0de0e8 | 2009-08-03 16:00:15 -0700 | [diff] [blame] | 219 | seq_printf(m, "%-10s %2d\n", ldops->name ? ldops->name : "???", i); |
| 220 | put_ldops(ldops); |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 221 | return 0; |
| 222 | } |
| 223 | |
Christoph Hellwig | fddda2b | 2018-04-13 19:44:18 +0200 | [diff] [blame] | 224 | const struct seq_operations tty_ldiscs_seq_ops = { |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 225 | .start = tty_ldiscs_seq_start, |
| 226 | .next = tty_ldiscs_seq_next, |
| 227 | .stop = tty_ldiscs_seq_stop, |
| 228 | .show = tty_ldiscs_seq_show, |
| 229 | }; |
| 230 | |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 231 | /** |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 232 | * tty_ldisc_ref_wait - wait for the tty ldisc |
| 233 | * @tty: tty device |
| 234 | * |
| 235 | * Dereference the line discipline for the terminal and take a |
| 236 | * reference to it. If the line discipline is in flux then |
| 237 | * wait patiently until it changes. |
| 238 | * |
Peter Hurley | 892d1fa | 2016-01-10 22:41:06 -0800 | [diff] [blame] | 239 | * Returns: NULL if the tty has been hungup and not re-opened with |
| 240 | * a new file descriptor, otherwise valid ldisc reference |
| 241 | * |
Lee Jones | 8eddcca | 2020-11-12 10:58:54 +0000 | [diff] [blame] | 242 | * Note 1: Must not be called from an IRQ/timer context. The caller |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 243 | * must also be careful not to hold other locks that will deadlock |
| 244 | * against a discipline change, such as an existing ldisc reference |
| 245 | * (which we check for) |
| 246 | * |
Lee Jones | 8eddcca | 2020-11-12 10:58:54 +0000 | [diff] [blame] | 247 | * Note 2: a file_operations routine (read/poll/write) should use this |
Peter Hurley | e55afd1 | 2016-01-10 22:41:01 -0800 | [diff] [blame] | 248 | * function to wait for any ldisc lifetime events to finish. |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 249 | */ |
| 250 | |
| 251 | struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty) |
| 252 | { |
Dmitry Vyukov | a4a3e06 | 2017-03-04 13:46:12 +0100 | [diff] [blame] | 253 | struct tty_ldisc *ld; |
| 254 | |
Peter Hurley | 3669752 | 2013-06-15 07:04:48 -0400 | [diff] [blame] | 255 | ldsem_down_read(&tty->ldisc_sem, MAX_SCHEDULE_TIMEOUT); |
Dmitry Vyukov | a4a3e06 | 2017-03-04 13:46:12 +0100 | [diff] [blame] | 256 | ld = tty->ldisc; |
| 257 | if (!ld) |
Peter Hurley | a570a49 | 2016-01-10 22:41:02 -0800 | [diff] [blame] | 258 | ldsem_up_read(&tty->ldisc_sem); |
Dmitry Vyukov | a4a3e06 | 2017-03-04 13:46:12 +0100 | [diff] [blame] | 259 | return ld; |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 260 | } |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 261 | EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait); |
| 262 | |
| 263 | /** |
| 264 | * tty_ldisc_ref - get the tty ldisc |
| 265 | * @tty: tty device |
| 266 | * |
| 267 | * Dereference the line discipline for the terminal and take a |
| 268 | * reference to it. If the line discipline is in flux then |
| 269 | * return NULL. Can be called from IRQ and timer functions. |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 270 | */ |
| 271 | |
| 272 | struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty) |
| 273 | { |
Peter Hurley | 3669752 | 2013-06-15 07:04:48 -0400 | [diff] [blame] | 274 | struct tty_ldisc *ld = NULL; |
| 275 | |
| 276 | if (ldsem_down_read_trylock(&tty->ldisc_sem)) { |
| 277 | ld = tty->ldisc; |
| 278 | if (!ld) |
| 279 | ldsem_up_read(&tty->ldisc_sem); |
| 280 | } |
| 281 | return ld; |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 282 | } |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 283 | EXPORT_SYMBOL_GPL(tty_ldisc_ref); |
| 284 | |
| 285 | /** |
| 286 | * tty_ldisc_deref - free a tty ldisc reference |
| 287 | * @ld: reference to free up |
| 288 | * |
| 289 | * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May |
| 290 | * be called in IRQ context. |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 291 | */ |
| 292 | |
| 293 | void tty_ldisc_deref(struct tty_ldisc *ld) |
| 294 | { |
Peter Hurley | 3669752 | 2013-06-15 07:04:48 -0400 | [diff] [blame] | 295 | ldsem_up_read(&ld->tty->ldisc_sem); |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 296 | } |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 297 | EXPORT_SYMBOL_GPL(tty_ldisc_deref); |
| 298 | |
Peter Hurley | d2c4389 | 2013-06-15 07:04:47 -0400 | [diff] [blame] | 299 | |
Peter Hurley | c2bb524 | 2016-01-09 21:13:51 -0800 | [diff] [blame] | 300 | static inline int |
Peter Hurley | e80a10e | 2014-11-05 12:13:06 -0500 | [diff] [blame] | 301 | __tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout) |
Peter Hurley | d2c4389 | 2013-06-15 07:04:47 -0400 | [diff] [blame] | 302 | { |
| 303 | return ldsem_down_write(&tty->ldisc_sem, timeout); |
| 304 | } |
| 305 | |
Peter Hurley | c2bb524 | 2016-01-09 21:13:51 -0800 | [diff] [blame] | 306 | static inline int |
Peter Hurley | e80a10e | 2014-11-05 12:13:06 -0500 | [diff] [blame] | 307 | __tty_ldisc_lock_nested(struct tty_struct *tty, unsigned long timeout) |
Peter Hurley | d2c4389 | 2013-06-15 07:04:47 -0400 | [diff] [blame] | 308 | { |
| 309 | return ldsem_down_write_nested(&tty->ldisc_sem, |
| 310 | LDISC_SEM_OTHER, timeout); |
| 311 | } |
| 312 | |
Peter Hurley | e80a10e | 2014-11-05 12:13:06 -0500 | [diff] [blame] | 313 | static inline void __tty_ldisc_unlock(struct tty_struct *tty) |
Peter Hurley | d2c4389 | 2013-06-15 07:04:47 -0400 | [diff] [blame] | 314 | { |
Guillaume Gomez | 52772ea | 2015-10-04 21:19:18 +0200 | [diff] [blame] | 315 | ldsem_up_write(&tty->ldisc_sem); |
Peter Hurley | d2c4389 | 2013-06-15 07:04:47 -0400 | [diff] [blame] | 316 | } |
| 317 | |
Gaurav Kohli | b027e22 | 2018-01-23 13:16:34 +0530 | [diff] [blame] | 318 | int tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout) |
Peter Hurley | fae76e9 | 2014-11-05 12:13:07 -0500 | [diff] [blame] | 319 | { |
| 320 | int ret; |
| 321 | |
Dmitry Safonov | c96cf92 | 2018-11-01 00:24:48 +0000 | [diff] [blame] | 322 | /* Kindly asking blocked readers to release the read side */ |
| 323 | set_bit(TTY_LDISC_CHANGING, &tty->flags); |
| 324 | wake_up_interruptible_all(&tty->read_wait); |
| 325 | wake_up_interruptible_all(&tty->write_wait); |
| 326 | |
Peter Hurley | fae76e9 | 2014-11-05 12:13:07 -0500 | [diff] [blame] | 327 | ret = __tty_ldisc_lock(tty, timeout); |
| 328 | if (!ret) |
| 329 | return -EBUSY; |
| 330 | set_bit(TTY_LDISC_HALTED, &tty->flags); |
| 331 | return 0; |
| 332 | } |
| 333 | |
Gaurav Kohli | b027e22 | 2018-01-23 13:16:34 +0530 | [diff] [blame] | 334 | void tty_ldisc_unlock(struct tty_struct *tty) |
Peter Hurley | fae76e9 | 2014-11-05 12:13:07 -0500 | [diff] [blame] | 335 | { |
| 336 | clear_bit(TTY_LDISC_HALTED, &tty->flags); |
Dmitry Safonov | c96cf92 | 2018-11-01 00:24:48 +0000 | [diff] [blame] | 337 | /* Can be cleared here - ldisc_unlock will wake up writers firstly */ |
| 338 | clear_bit(TTY_LDISC_CHANGING, &tty->flags); |
Peter Hurley | fae76e9 | 2014-11-05 12:13:07 -0500 | [diff] [blame] | 339 | __tty_ldisc_unlock(tty); |
| 340 | } |
| 341 | |
Peter Hurley | c2bb524 | 2016-01-09 21:13:51 -0800 | [diff] [blame] | 342 | static int |
Peter Hurley | d2c4389 | 2013-06-15 07:04:47 -0400 | [diff] [blame] | 343 | tty_ldisc_lock_pair_timeout(struct tty_struct *tty, struct tty_struct *tty2, |
| 344 | unsigned long timeout) |
| 345 | { |
| 346 | int ret; |
| 347 | |
| 348 | if (tty < tty2) { |
Peter Hurley | e80a10e | 2014-11-05 12:13:06 -0500 | [diff] [blame] | 349 | ret = __tty_ldisc_lock(tty, timeout); |
Peter Hurley | d2c4389 | 2013-06-15 07:04:47 -0400 | [diff] [blame] | 350 | if (ret) { |
Peter Hurley | e80a10e | 2014-11-05 12:13:06 -0500 | [diff] [blame] | 351 | ret = __tty_ldisc_lock_nested(tty2, timeout); |
Peter Hurley | d2c4389 | 2013-06-15 07:04:47 -0400 | [diff] [blame] | 352 | if (!ret) |
Peter Hurley | e80a10e | 2014-11-05 12:13:06 -0500 | [diff] [blame] | 353 | __tty_ldisc_unlock(tty); |
Peter Hurley | d2c4389 | 2013-06-15 07:04:47 -0400 | [diff] [blame] | 354 | } |
| 355 | } else { |
| 356 | /* if this is possible, it has lots of implications */ |
| 357 | WARN_ON_ONCE(tty == tty2); |
| 358 | if (tty2 && tty != tty2) { |
Peter Hurley | e80a10e | 2014-11-05 12:13:06 -0500 | [diff] [blame] | 359 | ret = __tty_ldisc_lock(tty2, timeout); |
Peter Hurley | d2c4389 | 2013-06-15 07:04:47 -0400 | [diff] [blame] | 360 | if (ret) { |
Peter Hurley | e80a10e | 2014-11-05 12:13:06 -0500 | [diff] [blame] | 361 | ret = __tty_ldisc_lock_nested(tty, timeout); |
Peter Hurley | d2c4389 | 2013-06-15 07:04:47 -0400 | [diff] [blame] | 362 | if (!ret) |
Peter Hurley | e80a10e | 2014-11-05 12:13:06 -0500 | [diff] [blame] | 363 | __tty_ldisc_unlock(tty2); |
Peter Hurley | d2c4389 | 2013-06-15 07:04:47 -0400 | [diff] [blame] | 364 | } |
| 365 | } else |
Peter Hurley | e80a10e | 2014-11-05 12:13:06 -0500 | [diff] [blame] | 366 | ret = __tty_ldisc_lock(tty, timeout); |
Peter Hurley | d2c4389 | 2013-06-15 07:04:47 -0400 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | if (!ret) |
| 370 | return -EBUSY; |
| 371 | |
| 372 | set_bit(TTY_LDISC_HALTED, &tty->flags); |
| 373 | if (tty2) |
| 374 | set_bit(TTY_LDISC_HALTED, &tty2->flags); |
| 375 | return 0; |
| 376 | } |
| 377 | |
Peter Hurley | c2bb524 | 2016-01-09 21:13:51 -0800 | [diff] [blame] | 378 | static void tty_ldisc_lock_pair(struct tty_struct *tty, struct tty_struct *tty2) |
Peter Hurley | d2c4389 | 2013-06-15 07:04:47 -0400 | [diff] [blame] | 379 | { |
| 380 | tty_ldisc_lock_pair_timeout(tty, tty2, MAX_SCHEDULE_TIMEOUT); |
| 381 | } |
| 382 | |
Peter Hurley | c2bb524 | 2016-01-09 21:13:51 -0800 | [diff] [blame] | 383 | static void tty_ldisc_unlock_pair(struct tty_struct *tty, |
| 384 | struct tty_struct *tty2) |
Peter Hurley | d2c4389 | 2013-06-15 07:04:47 -0400 | [diff] [blame] | 385 | { |
Peter Hurley | e80a10e | 2014-11-05 12:13:06 -0500 | [diff] [blame] | 386 | __tty_ldisc_unlock(tty); |
Peter Hurley | d2c4389 | 2013-06-15 07:04:47 -0400 | [diff] [blame] | 387 | if (tty2) |
Peter Hurley | e80a10e | 2014-11-05 12:13:06 -0500 | [diff] [blame] | 388 | __tty_ldisc_unlock(tty2); |
Peter Hurley | d2c4389 | 2013-06-15 07:04:47 -0400 | [diff] [blame] | 389 | } |
| 390 | |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 391 | /** |
Alan Cox | f2c4c65 | 2009-06-11 12:50:58 +0100 | [diff] [blame] | 392 | * tty_ldisc_flush - flush line discipline queue |
| 393 | * @tty: tty |
| 394 | * |
Peter Hurley | 86c80a8 | 2014-11-05 12:13:09 -0500 | [diff] [blame] | 395 | * Flush the line discipline queue (if any) and the tty flip buffers |
| 396 | * for this tty. |
Alan Cox | f2c4c65 | 2009-06-11 12:50:58 +0100 | [diff] [blame] | 397 | */ |
| 398 | |
| 399 | void tty_ldisc_flush(struct tty_struct *tty) |
| 400 | { |
| 401 | struct tty_ldisc *ld = tty_ldisc_ref(tty); |
Peter Hurley | 86c80a8 | 2014-11-05 12:13:09 -0500 | [diff] [blame] | 402 | |
| 403 | tty_buffer_flush(tty, ld); |
| 404 | if (ld) |
Alan Cox | f2c4c65 | 2009-06-11 12:50:58 +0100 | [diff] [blame] | 405 | tty_ldisc_deref(ld); |
Alan Cox | f2c4c65 | 2009-06-11 12:50:58 +0100 | [diff] [blame] | 406 | } |
Alan Cox | f2c4c65 | 2009-06-11 12:50:58 +0100 | [diff] [blame] | 407 | EXPORT_SYMBOL_GPL(tty_ldisc_flush); |
| 408 | |
| 409 | /** |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 410 | * tty_set_termios_ldisc - set ldisc field |
| 411 | * @tty: tty structure |
Peter Hurley | c12da96 | 2016-01-10 22:41:04 -0800 | [diff] [blame] | 412 | * @disc: line discipline number |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 413 | * |
| 414 | * This is probably overkill for real world processors but |
| 415 | * they are not on hot paths so a little discipline won't do |
| 416 | * any harm. |
| 417 | * |
Peter Hurley | dd42bf1 | 2015-11-27 14:30:21 -0500 | [diff] [blame] | 418 | * The line discipline-related tty_struct fields are reset to |
| 419 | * prevent the ldisc driver from re-using stale information for |
| 420 | * the new ldisc instance. |
| 421 | * |
Peter Hurley | 6a1c068 | 2013-06-15 09:14:23 -0400 | [diff] [blame] | 422 | * Locking: takes termios_rwsem |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 423 | */ |
| 424 | |
Peter Hurley | c12da96 | 2016-01-10 22:41:04 -0800 | [diff] [blame] | 425 | static void tty_set_termios_ldisc(struct tty_struct *tty, int disc) |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 426 | { |
Peter Hurley | 6a1c068 | 2013-06-15 09:14:23 -0400 | [diff] [blame] | 427 | down_write(&tty->termios_rwsem); |
Peter Hurley | c12da96 | 2016-01-10 22:41:04 -0800 | [diff] [blame] | 428 | tty->termios.c_line = disc; |
Peter Hurley | 6a1c068 | 2013-06-15 09:14:23 -0400 | [diff] [blame] | 429 | up_write(&tty->termios_rwsem); |
Peter Hurley | dd42bf1 | 2015-11-27 14:30:21 -0500 | [diff] [blame] | 430 | |
| 431 | tty->disc_data = NULL; |
| 432 | tty->receive_room = 0; |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 433 | } |
| 434 | |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 435 | /** |
| 436 | * tty_ldisc_open - open a line discipline |
| 437 | * @tty: tty we are opening the ldisc on |
| 438 | * @ld: discipline to open |
| 439 | * |
| 440 | * A helper opening method. Also a convenient debugging and check |
| 441 | * point. |
Arnd Bergmann | ec79d60 | 2010-06-01 22:53:01 +0200 | [diff] [blame] | 442 | * |
| 443 | * Locking: always called with BTM already held. |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 444 | */ |
| 445 | |
| 446 | static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld) |
| 447 | { |
| 448 | WARN_ON(test_and_set_bit(TTY_LDISC_OPEN, &tty->flags)); |
Alan Cox | f18f949 | 2009-11-30 13:18:35 +0000 | [diff] [blame] | 449 | if (ld->ops->open) { |
| 450 | int ret; |
Xiaofei Tan | 5d3945e | 2021-04-07 15:06:46 +0800 | [diff] [blame] | 451 | /* BTM here locks versus a hangup event */ |
Alan Cox | f18f949 | 2009-11-30 13:18:35 +0000 | [diff] [blame] | 452 | ret = ld->ops->open(tty); |
Jiri Slaby | 7f90cfc | 2010-11-25 00:27:54 +0100 | [diff] [blame] | 453 | if (ret) |
| 454 | clear_bit(TTY_LDISC_OPEN, &tty->flags); |
Peter Hurley | fb6edc9 | 2015-07-12 22:49:12 -0400 | [diff] [blame] | 455 | |
Peter Hurley | a570a49 | 2016-01-10 22:41:02 -0800 | [diff] [blame] | 456 | tty_ldisc_debug(tty, "%p: opened\n", ld); |
Alan Cox | f18f949 | 2009-11-30 13:18:35 +0000 | [diff] [blame] | 457 | return ret; |
| 458 | } |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 459 | return 0; |
| 460 | } |
| 461 | |
| 462 | /** |
| 463 | * tty_ldisc_close - close a line discipline |
| 464 | * @tty: tty we are opening the ldisc on |
| 465 | * @ld: discipline to close |
| 466 | * |
| 467 | * A helper close method. Also a convenient debugging and check |
| 468 | * point. |
| 469 | */ |
| 470 | |
| 471 | static void tty_ldisc_close(struct tty_struct *tty, struct tty_ldisc *ld) |
| 472 | { |
Nikolay Borisov | 9ffbe8a | 2019-05-31 13:06:51 +0300 | [diff] [blame] | 473 | lockdep_assert_held_write(&tty->ldisc_sem); |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 474 | WARN_ON(!test_bit(TTY_LDISC_OPEN, &tty->flags)); |
| 475 | clear_bit(TTY_LDISC_OPEN, &tty->flags); |
| 476 | if (ld->ops->close) |
| 477 | ld->ops->close(tty); |
Peter Hurley | a570a49 | 2016-01-10 22:41:02 -0800 | [diff] [blame] | 478 | tty_ldisc_debug(tty, "%p: closed\n", ld); |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 479 | } |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 480 | |
| 481 | /** |
Alan Cox | 8a8dabf | 2017-06-02 13:49:30 +0100 | [diff] [blame] | 482 | * tty_ldisc_failto - helper for ldisc failback |
| 483 | * @tty: tty to open the ldisc on |
| 484 | * @ld: ldisc we are trying to fail back to |
| 485 | * |
| 486 | * Helper to try and recover a tty when switching back to the old |
| 487 | * ldisc fails and we need something attached. |
| 488 | */ |
| 489 | |
| 490 | static int tty_ldisc_failto(struct tty_struct *tty, int ld) |
| 491 | { |
| 492 | struct tty_ldisc *disc = tty_ldisc_get(tty, ld); |
| 493 | int r; |
| 494 | |
Nikolay Borisov | 9ffbe8a | 2019-05-31 13:06:51 +0300 | [diff] [blame] | 495 | lockdep_assert_held_write(&tty->ldisc_sem); |
Alan Cox | 8a8dabf | 2017-06-02 13:49:30 +0100 | [diff] [blame] | 496 | if (IS_ERR(disc)) |
| 497 | return PTR_ERR(disc); |
| 498 | tty->ldisc = disc; |
| 499 | tty_set_termios_ldisc(tty, ld); |
Xiaofei Tan | 408795b | 2021-04-07 15:06:49 +0800 | [diff] [blame] | 500 | r = tty_ldisc_open(tty, disc); |
| 501 | if (r < 0) |
Alan Cox | 8a8dabf | 2017-06-02 13:49:30 +0100 | [diff] [blame] | 502 | tty_ldisc_put(disc); |
| 503 | return r; |
| 504 | } |
| 505 | |
| 506 | /** |
Greg Kroah-Hartman | a8983d0 | 2017-04-14 10:57:52 +0200 | [diff] [blame] | 507 | * tty_ldisc_restore - helper for tty ldisc change |
| 508 | * @tty: tty to recover |
| 509 | * @old: previous ldisc |
| 510 | * |
| 511 | * Restore the previous line discipline or N_TTY when a line discipline |
| 512 | * change fails due to an open error |
| 513 | */ |
| 514 | |
| 515 | static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old) |
| 516 | { |
Greg Kroah-Hartman | a8983d0 | 2017-04-14 10:57:52 +0200 | [diff] [blame] | 517 | /* There is an outstanding reference here so this is safe */ |
Tetsuo Handa | 598c2d4 | 2018-04-16 20:06:34 +0900 | [diff] [blame] | 518 | if (tty_ldisc_failto(tty, old->ops->num) < 0) { |
| 519 | const char *name = tty_name(tty); |
| 520 | |
| 521 | pr_warn("Falling back ldisc for %s.\n", name); |
Xiaofei Tan | 72a8dcd | 2021-04-07 15:06:48 +0800 | [diff] [blame] | 522 | /* |
| 523 | * The traditional behaviour is to fall back to N_TTY, we |
| 524 | * want to avoid falling back to N_NULL unless we have no |
| 525 | * choice to avoid the risk of breaking anything |
| 526 | */ |
Alan Cox | 8a8dabf | 2017-06-02 13:49:30 +0100 | [diff] [blame] | 527 | if (tty_ldisc_failto(tty, N_TTY) < 0 && |
| 528 | tty_ldisc_failto(tty, N_NULL) < 0) |
Tetsuo Handa | 598c2d4 | 2018-04-16 20:06:34 +0900 | [diff] [blame] | 529 | panic("Couldn't open N_NULL ldisc for %s.", name); |
Greg Kroah-Hartman | a8983d0 | 2017-04-14 10:57:52 +0200 | [diff] [blame] | 530 | } |
| 531 | } |
| 532 | |
| 533 | /** |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 534 | * tty_set_ldisc - set line discipline |
| 535 | * @tty: the terminal to set |
Jiri Slaby | fa44195 | 2020-08-18 10:56:51 +0200 | [diff] [blame] | 536 | * @disc: the line discipline number |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 537 | * |
| 538 | * Set the discipline of a tty line. Must be called from a process |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 539 | * context. The ldisc change logic has to protect itself against any |
| 540 | * overlapping ldisc change (including on the other end of pty pairs), |
| 541 | * the close of one side of a tty/pty pair, and eventually hangup. |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 542 | */ |
| 543 | |
Peter Hurley | c12da96 | 2016-01-10 22:41:04 -0800 | [diff] [blame] | 544 | int tty_set_ldisc(struct tty_struct *tty, int disc) |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 545 | { |
Greg Kroah-Hartman | a8983d0 | 2017-04-14 10:57:52 +0200 | [diff] [blame] | 546 | int retval; |
| 547 | struct tty_ldisc *old_ldisc, *new_ldisc; |
| 548 | |
| 549 | new_ldisc = tty_ldisc_get(tty, disc); |
| 550 | if (IS_ERR(new_ldisc)) |
| 551 | return PTR_ERR(new_ldisc); |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 552 | |
Peter Hurley | c8483bc | 2014-11-05 12:12:45 -0500 | [diff] [blame] | 553 | tty_lock(tty); |
Peter Hurley | 276a661 | 2014-11-05 12:13:08 -0500 | [diff] [blame] | 554 | retval = tty_ldisc_lock(tty, 5 * HZ); |
Peter Hurley | 63d8cb3 | 2015-11-08 09:29:38 -0500 | [diff] [blame] | 555 | if (retval) |
| 556 | goto err; |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 557 | |
Peter Hurley | a570a49 | 2016-01-10 22:41:02 -0800 | [diff] [blame] | 558 | if (!tty->ldisc) { |
| 559 | retval = -EIO; |
| 560 | goto out; |
| 561 | } |
| 562 | |
Peter Hurley | 63d8cb3 | 2015-11-08 09:29:38 -0500 | [diff] [blame] | 563 | /* Check the no-op case */ |
Greg Kroah-Hartman | a8983d0 | 2017-04-14 10:57:52 +0200 | [diff] [blame] | 564 | if (tty->ldisc->ops->num == disc) |
Peter Hurley | 63d8cb3 | 2015-11-08 09:29:38 -0500 | [diff] [blame] | 565 | goto out; |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 566 | |
Peter Hurley | 63d8cb3 | 2015-11-08 09:29:38 -0500 | [diff] [blame] | 567 | if (test_bit(TTY_HUPPED, &tty->flags)) { |
| 568 | /* We were raced by hangup */ |
| 569 | retval = -EIO; |
| 570 | goto out; |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 571 | } |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 572 | |
Greg Kroah-Hartman | a8983d0 | 2017-04-14 10:57:52 +0200 | [diff] [blame] | 573 | old_ldisc = tty->ldisc; |
| 574 | |
| 575 | /* Shutdown the old discipline. */ |
| 576 | tty_ldisc_close(tty, old_ldisc); |
| 577 | |
| 578 | /* Now set up the new line discipline. */ |
| 579 | tty->ldisc = new_ldisc; |
| 580 | tty_set_termios_ldisc(tty, disc); |
| 581 | |
| 582 | retval = tty_ldisc_open(tty, new_ldisc); |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 583 | if (retval < 0) { |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 584 | /* Back to the old one or N_TTY if we can't */ |
Greg Kroah-Hartman | a8983d0 | 2017-04-14 10:57:52 +0200 | [diff] [blame] | 585 | tty_ldisc_put(new_ldisc); |
| 586 | tty_ldisc_restore(tty, old_ldisc); |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 587 | } |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 588 | |
Greg Kroah-Hartman | a8983d0 | 2017-04-14 10:57:52 +0200 | [diff] [blame] | 589 | if (tty->ldisc->ops->num != old_ldisc->ops->num && tty->ops->set_ldisc) { |
Peter Hurley | 9191aaa | 2014-11-05 13:11:41 -0500 | [diff] [blame] | 590 | down_read(&tty->termios_rwsem); |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 591 | tty->ops->set_ldisc(tty); |
Peter Hurley | 9191aaa | 2014-11-05 13:11:41 -0500 | [diff] [blame] | 592 | up_read(&tty->termios_rwsem); |
| 593 | } |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 594 | |
Xiaofei Tan | 72a8dcd | 2021-04-07 15:06:48 +0800 | [diff] [blame] | 595 | /* |
| 596 | * At this point we hold a reference to the new ldisc and a |
| 597 | * reference to the old ldisc, or we hold two references to |
| 598 | * the old ldisc (if it was restored as part of error cleanup |
| 599 | * above). In either case, releasing a single reference from |
| 600 | * the old ldisc is correct. |
| 601 | */ |
Greg Kroah-Hartman | a8983d0 | 2017-04-14 10:57:52 +0200 | [diff] [blame] | 602 | new_ldisc = old_ldisc; |
Peter Hurley | 63d8cb3 | 2015-11-08 09:29:38 -0500 | [diff] [blame] | 603 | out: |
Peter Hurley | 276a661 | 2014-11-05 12:13:08 -0500 | [diff] [blame] | 604 | tty_ldisc_unlock(tty); |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 605 | |
Xiaofei Tan | 72a8dcd | 2021-04-07 15:06:48 +0800 | [diff] [blame] | 606 | /* |
| 607 | * Restart the work queue in case no characters kick it off. Safe if |
| 608 | * already running |
| 609 | */ |
Peter Hurley | 17a6921 | 2015-11-08 07:53:06 -0500 | [diff] [blame] | 610 | tty_buffer_restart_work(tty->port); |
Peter Hurley | 63d8cb3 | 2015-11-08 09:29:38 -0500 | [diff] [blame] | 611 | err: |
Greg Kroah-Hartman | a8983d0 | 2017-04-14 10:57:52 +0200 | [diff] [blame] | 612 | tty_ldisc_put(new_ldisc); /* drop the extra reference */ |
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 613 | tty_unlock(tty); |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 614 | return retval; |
| 615 | } |
Okash Khawaja | 1ab92da | 2017-05-15 18:45:33 +0100 | [diff] [blame] | 616 | EXPORT_SYMBOL_GPL(tty_set_ldisc); |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 617 | |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 618 | /** |
Peter Hurley | 6ffeb4b | 2016-01-10 22:41:03 -0800 | [diff] [blame] | 619 | * tty_ldisc_kill - teardown ldisc |
| 620 | * @tty: tty being released |
| 621 | * |
| 622 | * Perform final close of the ldisc and reset tty->ldisc |
| 623 | */ |
| 624 | static void tty_ldisc_kill(struct tty_struct *tty) |
| 625 | { |
Nikolay Borisov | 9ffbe8a | 2019-05-31 13:06:51 +0300 | [diff] [blame] | 626 | lockdep_assert_held_write(&tty->ldisc_sem); |
Peter Hurley | 6ffeb4b | 2016-01-10 22:41:03 -0800 | [diff] [blame] | 627 | if (!tty->ldisc) |
| 628 | return; |
| 629 | /* |
| 630 | * Now kill off the ldisc |
| 631 | */ |
| 632 | tty_ldisc_close(tty, tty->ldisc); |
| 633 | tty_ldisc_put(tty->ldisc); |
| 634 | /* Force an oops if we mess this up */ |
| 635 | tty->ldisc = NULL; |
| 636 | } |
| 637 | |
| 638 | /** |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 639 | * tty_reset_termios - reset terminal state |
| 640 | * @tty: tty to reset |
| 641 | * |
| 642 | * Restore a terminal to the driver default state. |
| 643 | */ |
| 644 | |
| 645 | static void tty_reset_termios(struct tty_struct *tty) |
| 646 | { |
Peter Hurley | 6a1c068 | 2013-06-15 09:14:23 -0400 | [diff] [blame] | 647 | down_write(&tty->termios_rwsem); |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 648 | tty->termios = tty->driver->init_termios; |
| 649 | tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios); |
| 650 | tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios); |
Peter Hurley | 6a1c068 | 2013-06-15 09:14:23 -0400 | [diff] [blame] | 651 | up_write(&tty->termios_rwsem); |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | |
| 655 | /** |
| 656 | * tty_ldisc_reinit - reinitialise the tty ldisc |
| 657 | * @tty: tty to reinit |
Peter Hurley | c12da96 | 2016-01-10 22:41:04 -0800 | [diff] [blame] | 658 | * @disc: line discipline to reinitialize |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 659 | * |
Peter Hurley | 7896f30 | 2016-01-10 22:41:05 -0800 | [diff] [blame] | 660 | * Completely reinitialize the line discipline state, by closing the |
Peter Hurley | 892d1fa | 2016-01-10 22:41:06 -0800 | [diff] [blame] | 661 | * current instance, if there is one, and opening a new instance. If |
| 662 | * an error occurs opening the new non-N_TTY instance, the instance |
| 663 | * is dropped and tty->ldisc reset to NULL. The caller can then retry |
| 664 | * with N_TTY instead. |
Peter Hurley | 7896f30 | 2016-01-10 22:41:05 -0800 | [diff] [blame] | 665 | * |
| 666 | * Returns 0 if successful, otherwise error code < 0 |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 667 | */ |
| 668 | |
Peter Hurley | 892d1fa | 2016-01-10 22:41:06 -0800 | [diff] [blame] | 669 | int tty_ldisc_reinit(struct tty_struct *tty, int disc) |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 670 | { |
Peter Hurley | 7896f30 | 2016-01-10 22:41:05 -0800 | [diff] [blame] | 671 | struct tty_ldisc *ld; |
| 672 | int retval; |
Philippe Rétornaz | 1c95ba1 | 2010-10-27 17:13:21 +0200 | [diff] [blame] | 673 | |
Nikolay Borisov | 9ffbe8a | 2019-05-31 13:06:51 +0300 | [diff] [blame] | 674 | lockdep_assert_held_write(&tty->ldisc_sem); |
Peter Hurley | 7896f30 | 2016-01-10 22:41:05 -0800 | [diff] [blame] | 675 | ld = tty_ldisc_get(tty, disc); |
Greg Kroah-Hartman | a8983d0 | 2017-04-14 10:57:52 +0200 | [diff] [blame] | 676 | if (IS_ERR(ld)) { |
| 677 | BUG_ON(disc == N_TTY); |
Peter Hurley | 7896f30 | 2016-01-10 22:41:05 -0800 | [diff] [blame] | 678 | return PTR_ERR(ld); |
Greg Kroah-Hartman | a8983d0 | 2017-04-14 10:57:52 +0200 | [diff] [blame] | 679 | } |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 680 | |
Peter Hurley | 7896f30 | 2016-01-10 22:41:05 -0800 | [diff] [blame] | 681 | if (tty->ldisc) { |
| 682 | tty_ldisc_close(tty, tty->ldisc); |
| 683 | tty_ldisc_put(tty->ldisc); |
| 684 | } |
| 685 | |
| 686 | /* switch the line discipline */ |
Peter Hurley | f4807045 | 2013-03-11 16:44:42 -0400 | [diff] [blame] | 687 | tty->ldisc = ld; |
Peter Hurley | c12da96 | 2016-01-10 22:41:04 -0800 | [diff] [blame] | 688 | tty_set_termios_ldisc(tty, disc); |
Peter Hurley | 7896f30 | 2016-01-10 22:41:05 -0800 | [diff] [blame] | 689 | retval = tty_ldisc_open(tty, tty->ldisc); |
| 690 | if (retval) { |
Johannes Weiner | e65c62b | 2017-10-13 15:58:08 -0700 | [diff] [blame] | 691 | tty_ldisc_put(tty->ldisc); |
| 692 | tty->ldisc = NULL; |
Peter Hurley | 7896f30 | 2016-01-10 22:41:05 -0800 | [diff] [blame] | 693 | } |
| 694 | return retval; |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | /** |
| 698 | * tty_ldisc_hangup - hangup ldisc reset |
| 699 | * @tty: tty being hung up |
Lee Jones | 8eddcca | 2020-11-12 10:58:54 +0000 | [diff] [blame] | 700 | * @reinit: whether to re-initialise the tty |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 701 | * |
| 702 | * Some tty devices reset their termios when they receive a hangup |
| 703 | * event. In that situation we must also switch back to N_TTY properly |
| 704 | * before we reset the termios data. |
| 705 | * |
| 706 | * Locking: We can take the ldisc mutex as the rest of the code is |
| 707 | * careful to allow for this. |
| 708 | * |
| 709 | * In the pty pair case this occurs in the close() path of the |
| 710 | * tty itself so we must be careful about locking rules. |
| 711 | */ |
| 712 | |
Peter Hurley | 892d1fa | 2016-01-10 22:41:06 -0800 | [diff] [blame] | 713 | void tty_ldisc_hangup(struct tty_struct *tty, bool reinit) |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 714 | { |
| 715 | struct tty_ldisc *ld; |
| 716 | |
Peter Hurley | a570a49 | 2016-01-10 22:41:02 -0800 | [diff] [blame] | 717 | tty_ldisc_debug(tty, "%p: hangup\n", tty->ldisc); |
Peter Hurley | fc575ee | 2013-03-11 16:44:38 -0400 | [diff] [blame] | 718 | |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 719 | ld = tty_ldisc_ref(tty); |
| 720 | if (ld != NULL) { |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 721 | if (ld->ops->flush_buffer) |
| 722 | ld->ops->flush_buffer(tty); |
| 723 | tty_driver_flush_buffer(tty); |
| 724 | if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) && |
| 725 | ld->ops->write_wakeup) |
| 726 | ld->ops->write_wakeup(tty); |
| 727 | if (ld->ops->hangup) |
| 728 | ld->ops->hangup(tty); |
| 729 | tty_ldisc_deref(ld); |
| 730 | } |
Peter Hurley | 3669752 | 2013-06-15 07:04:48 -0400 | [diff] [blame] | 731 | |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 732 | wake_up_interruptible_poll(&tty->write_wait, EPOLLOUT); |
| 733 | wake_up_interruptible_poll(&tty->read_wait, EPOLLIN); |
Peter Hurley | 3669752 | 2013-06-15 07:04:48 -0400 | [diff] [blame] | 734 | |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 735 | /* |
| 736 | * Shutdown the current line discipline, and reset it to |
Alan Cox | 638b964 | 2010-02-08 10:09:26 +0000 | [diff] [blame] | 737 | * N_TTY if need be. |
| 738 | * |
| 739 | * Avoid racing set_ldisc or tty_ldisc_release |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 740 | */ |
Peter Hurley | fae76e9 | 2014-11-05 12:13:07 -0500 | [diff] [blame] | 741 | tty_ldisc_lock(tty, MAX_SCHEDULE_TIMEOUT); |
Arnd Bergmann | 60af22d | 2010-06-01 22:53:06 +0200 | [diff] [blame] | 742 | |
Peter Hurley | 892d1fa | 2016-01-10 22:41:06 -0800 | [diff] [blame] | 743 | if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) |
Alan Cox | 638b964 | 2010-02-08 10:09:26 +0000 | [diff] [blame] | 744 | tty_reset_termios(tty); |
Peter Hurley | fc575ee | 2013-03-11 16:44:38 -0400 | [diff] [blame] | 745 | |
Peter Hurley | 892d1fa | 2016-01-10 22:41:06 -0800 | [diff] [blame] | 746 | if (tty->ldisc) { |
| 747 | if (reinit) { |
Johannes Weiner | e65c62b | 2017-10-13 15:58:08 -0700 | [diff] [blame] | 748 | if (tty_ldisc_reinit(tty, tty->termios.c_line) < 0 && |
| 749 | tty_ldisc_reinit(tty, N_TTY) < 0) |
| 750 | WARN_ON(tty_ldisc_reinit(tty, N_NULL) < 0); |
Peter Hurley | 892d1fa | 2016-01-10 22:41:06 -0800 | [diff] [blame] | 751 | } else |
| 752 | tty_ldisc_kill(tty); |
| 753 | } |
| 754 | tty_ldisc_unlock(tty); |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 755 | } |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 756 | |
| 757 | /** |
| 758 | * tty_ldisc_setup - open line discipline |
| 759 | * @tty: tty being shut down |
| 760 | * @o_tty: pair tty for pty/tty pairs |
| 761 | * |
| 762 | * Called during the initial open of a tty/pty pair in order to set up the |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 763 | * line disciplines and bind them to the tty. This has no locking issues |
| 764 | * as the device isn't yet active. |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 765 | */ |
| 766 | |
| 767 | int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty) |
| 768 | { |
Peter Hurley | 9de2a7c | 2016-01-10 22:41:08 -0800 | [diff] [blame] | 769 | int retval = tty_ldisc_open(tty, tty->ldisc); |
Xiaofei Tan | d723835 | 2021-04-07 15:06:47 +0800 | [diff] [blame] | 770 | |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 771 | if (retval) |
| 772 | return retval; |
| 773 | |
| 774 | if (o_tty) { |
Dmitry Safonov | 110b892 | 2018-11-01 00:24:51 +0000 | [diff] [blame] | 775 | /* |
| 776 | * Called without o_tty->ldisc_sem held, as o_tty has been |
| 777 | * just allocated and no one has a reference to it. |
| 778 | */ |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 779 | retval = tty_ldisc_open(o_tty, o_tty->ldisc); |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 780 | if (retval) { |
Peter Hurley | 9de2a7c | 2016-01-10 22:41:08 -0800 | [diff] [blame] | 781 | tty_ldisc_close(tty, tty->ldisc); |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 782 | return retval; |
| 783 | } |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 784 | } |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 785 | return 0; |
| 786 | } |
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 787 | |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 788 | /** |
| 789 | * tty_ldisc_release - release line discipline |
Peter Hurley | 62462ae | 2014-11-05 12:12:58 -0500 | [diff] [blame] | 790 | * @tty: tty being shut down (or one end of pty pair) |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 791 | * |
Peter Hurley | 62462ae | 2014-11-05 12:12:58 -0500 | [diff] [blame] | 792 | * Called during the final close of a tty or a pty pair in order to shut |
Peter Hurley | 5b6e683 | 2016-01-10 22:41:00 -0800 | [diff] [blame] | 793 | * down the line discpline layer. On exit, each tty's ldisc is NULL. |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 794 | */ |
| 795 | |
Peter Hurley | 62462ae | 2014-11-05 12:12:58 -0500 | [diff] [blame] | 796 | void tty_ldisc_release(struct tty_struct *tty) |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 797 | { |
Peter Hurley | 62462ae | 2014-11-05 12:12:58 -0500 | [diff] [blame] | 798 | struct tty_struct *o_tty = tty->link; |
| 799 | |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 800 | /* |
Peter Hurley | a2965b7 | 2013-03-11 16:44:35 -0400 | [diff] [blame] | 801 | * Shutdown this line discipline. As this is the final close, |
| 802 | * it does not race with the set_ldisc code path. |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 803 | */ |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 804 | |
Peter Hurley | 3669752 | 2013-06-15 07:04:48 -0400 | [diff] [blame] | 805 | tty_ldisc_lock_pair(tty, o_tty); |
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 806 | tty_ldisc_kill(tty); |
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 807 | if (o_tty) |
| 808 | tty_ldisc_kill(o_tty); |
Peter Hurley | 3669752 | 2013-06-15 07:04:48 -0400 | [diff] [blame] | 809 | tty_ldisc_unlock_pair(tty, o_tty); |
| 810 | |
Xiaofei Tan | 72a8dcd | 2021-04-07 15:06:48 +0800 | [diff] [blame] | 811 | /* |
| 812 | * And the memory resources remaining (buffers, termios) will be |
| 813 | * disposed of when the kref hits zero |
| 814 | */ |
Peter Hurley | fc575ee | 2013-03-11 16:44:38 -0400 | [diff] [blame] | 815 | |
Peter Hurley | fb6edc9 | 2015-07-12 22:49:12 -0400 | [diff] [blame] | 816 | tty_ldisc_debug(tty, "released\n"); |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 817 | } |
Okash Khawaja | 1ab92da | 2017-05-15 18:45:33 +0100 | [diff] [blame] | 818 | EXPORT_SYMBOL_GPL(tty_ldisc_release); |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 819 | |
| 820 | /** |
| 821 | * tty_ldisc_init - ldisc setup for new tty |
| 822 | * @tty: tty being allocated |
| 823 | * |
| 824 | * Set up the line discipline objects for a newly allocated tty. Note that |
| 825 | * the tty structure is not completely set up when this call is made. |
| 826 | */ |
| 827 | |
Tetsuo Handa | 903f9db | 2018-04-05 19:40:16 +0900 | [diff] [blame] | 828 | int tty_ldisc_init(struct tty_struct *tty) |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 829 | { |
Peter Hurley | 3669752 | 2013-06-15 07:04:48 -0400 | [diff] [blame] | 830 | struct tty_ldisc *ld = tty_ldisc_get(tty, N_TTY); |
Xiaofei Tan | d723835 | 2021-04-07 15:06:47 +0800 | [diff] [blame] | 831 | |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 832 | if (IS_ERR(ld)) |
Tetsuo Handa | 903f9db | 2018-04-05 19:40:16 +0900 | [diff] [blame] | 833 | return PTR_ERR(ld); |
Peter Hurley | f4807045 | 2013-03-11 16:44:42 -0400 | [diff] [blame] | 834 | tty->ldisc = ld; |
Tetsuo Handa | 903f9db | 2018-04-05 19:40:16 +0900 | [diff] [blame] | 835 | return 0; |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 836 | } |
| 837 | |
Jiri Slaby | 6716671 | 2011-03-23 10:48:35 +0100 | [diff] [blame] | 838 | /** |
Peter Hurley | c8b710b | 2016-01-09 21:13:46 -0800 | [diff] [blame] | 839 | * tty_ldisc_deinit - ldisc cleanup for new tty |
Jiri Slaby | 6716671 | 2011-03-23 10:48:35 +0100 | [diff] [blame] | 840 | * @tty: tty that was allocated recently |
| 841 | * |
| 842 | * The tty structure must not becompletely set up (tty_ldisc_setup) when |
| 843 | * this call is made. |
| 844 | */ |
| 845 | void tty_ldisc_deinit(struct tty_struct *tty) |
| 846 | { |
Dmitry Safonov | 110b892 | 2018-11-01 00:24:51 +0000 | [diff] [blame] | 847 | /* no ldisc_sem, tty is being destroyed */ |
Peter Hurley | c8b710b | 2016-01-09 21:13:46 -0800 | [diff] [blame] | 848 | if (tty->ldisc) |
| 849 | tty_ldisc_put(tty->ldisc); |
Peter Hurley | f4807045 | 2013-03-11 16:44:42 -0400 | [diff] [blame] | 850 | tty->ldisc = NULL; |
Jiri Slaby | 6716671 | 2011-03-23 10:48:35 +0100 | [diff] [blame] | 851 | } |
Greg Kroah-Hartman | 7c0cca7 | 2019-01-21 17:26:42 +0100 | [diff] [blame] | 852 | |
Greg Kroah-Hartman | 7c0cca7 | 2019-01-21 17:26:42 +0100 | [diff] [blame] | 853 | static struct ctl_table tty_table[] = { |
| 854 | { |
| 855 | .procname = "ldisc_autoload", |
| 856 | .data = &tty_ldisc_autoload, |
| 857 | .maxlen = sizeof(tty_ldisc_autoload), |
| 858 | .mode = 0644, |
| 859 | .proc_handler = proc_dointvec, |
Matteo Croce | eec4844 | 2019-07-18 15:58:50 -0700 | [diff] [blame] | 860 | .extra1 = SYSCTL_ZERO, |
| 861 | .extra2 = SYSCTL_ONE, |
Greg Kroah-Hartman | 7c0cca7 | 2019-01-21 17:26:42 +0100 | [diff] [blame] | 862 | }, |
| 863 | { } |
| 864 | }; |
| 865 | |
| 866 | static struct ctl_table tty_dir_table[] = { |
| 867 | { |
| 868 | .procname = "tty", |
| 869 | .mode = 0555, |
| 870 | .child = tty_table, |
| 871 | }, |
| 872 | { } |
| 873 | }; |
| 874 | |
| 875 | static struct ctl_table tty_root_table[] = { |
| 876 | { |
| 877 | .procname = "dev", |
| 878 | .mode = 0555, |
| 879 | .child = tty_dir_table, |
| 880 | }, |
| 881 | { } |
| 882 | }; |
| 883 | |
| 884 | void tty_sysctl_init(void) |
| 885 | { |
| 886 | register_sysctl_table(tty_root_table); |
| 887 | } |