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