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