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