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