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> |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 14 | #include <linux/module.h> |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 15 | #include <linux/device.h> |
| 16 | #include <linux/wait.h> |
| 17 | #include <linux/bitops.h> |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 18 | #include <linux/seq_file.h> |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 19 | #include <linux/uaccess.h> |
Jiri Slaby | 0c73c08 | 2011-11-16 16:27:09 +0100 | [diff] [blame] | 20 | #include <linux/ratelimit.h> |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 21 | |
Peter Hurley | fc575ee | 2013-03-11 16:44:38 -0400 | [diff] [blame] | 22 | #undef LDISC_DEBUG_HANGUP |
| 23 | |
| 24 | #ifdef LDISC_DEBUG_HANGUP |
Rasmus Villemoes | 429b474 | 2015-03-31 15:55:59 +0200 | [diff] [blame] | 25 | #define tty_ldisc_debug(tty, f, args...) ({ \ |
| 26 | printk(KERN_DEBUG "%s: %s: " f, __func__, tty_name(tty), ##args); \ |
Peter Hurley | fc575ee | 2013-03-11 16:44:38 -0400 | [diff] [blame] | 27 | }) |
| 28 | #else |
| 29 | #define tty_ldisc_debug(tty, f, args...) |
| 30 | #endif |
| 31 | |
Peter Hurley | d2c4389 | 2013-06-15 07:04:47 -0400 | [diff] [blame] | 32 | /* lockdep nested classes for tty->ldisc_sem */ |
| 33 | enum { |
| 34 | LDISC_SEM_NORMAL, |
| 35 | LDISC_SEM_OTHER, |
| 36 | }; |
| 37 | |
| 38 | |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 39 | /* |
| 40 | * This guards the refcounted line discipline lists. The lock |
| 41 | * must be taken with irqs off because there are hangup path |
| 42 | * callers who will do ldisc lookups and cannot sleep. |
| 43 | */ |
| 44 | |
Peter Hurley | 137084b | 2013-06-15 07:04:46 -0400 | [diff] [blame] | 45 | static DEFINE_RAW_SPINLOCK(tty_ldiscs_lock); |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 46 | /* Line disc dispatch table */ |
| 47 | static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS]; |
| 48 | |
| 49 | /** |
| 50 | * tty_register_ldisc - install a line discipline |
| 51 | * @disc: ldisc number |
| 52 | * @new_ldisc: pointer to the ldisc object |
| 53 | * |
| 54 | * Installs a new line discipline into the kernel. The discipline |
| 55 | * is set up as unreferenced and then made available to the kernel |
| 56 | * from this point onwards. |
| 57 | * |
| 58 | * Locking: |
Peter Hurley | 137084b | 2013-06-15 07:04:46 -0400 | [diff] [blame] | 59 | * takes tty_ldiscs_lock to guard against ldisc races |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 60 | */ |
| 61 | |
| 62 | int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc) |
| 63 | { |
| 64 | unsigned long flags; |
| 65 | int ret = 0; |
| 66 | |
| 67 | if (disc < N_TTY || disc >= NR_LDISCS) |
| 68 | return -EINVAL; |
| 69 | |
Peter Hurley | 137084b | 2013-06-15 07:04:46 -0400 | [diff] [blame] | 70 | raw_spin_lock_irqsave(&tty_ldiscs_lock, flags); |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 71 | tty_ldiscs[disc] = new_ldisc; |
| 72 | new_ldisc->num = disc; |
| 73 | new_ldisc->refcount = 0; |
Peter Hurley | 137084b | 2013-06-15 07:04:46 -0400 | [diff] [blame] | 74 | raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags); |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 75 | |
| 76 | return ret; |
| 77 | } |
| 78 | EXPORT_SYMBOL(tty_register_ldisc); |
| 79 | |
| 80 | /** |
| 81 | * tty_unregister_ldisc - unload a line discipline |
| 82 | * @disc: ldisc number |
| 83 | * @new_ldisc: pointer to the ldisc object |
| 84 | * |
| 85 | * Remove a line discipline from the kernel providing it is not |
| 86 | * currently in use. |
| 87 | * |
| 88 | * Locking: |
Peter Hurley | 137084b | 2013-06-15 07:04:46 -0400 | [diff] [blame] | 89 | * takes tty_ldiscs_lock to guard against ldisc races |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 90 | */ |
| 91 | |
| 92 | int tty_unregister_ldisc(int disc) |
| 93 | { |
| 94 | unsigned long flags; |
| 95 | int ret = 0; |
| 96 | |
| 97 | if (disc < N_TTY || disc >= NR_LDISCS) |
| 98 | return -EINVAL; |
| 99 | |
Peter Hurley | 137084b | 2013-06-15 07:04:46 -0400 | [diff] [blame] | 100 | raw_spin_lock_irqsave(&tty_ldiscs_lock, flags); |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 101 | if (tty_ldiscs[disc]->refcount) |
| 102 | ret = -EBUSY; |
| 103 | else |
| 104 | tty_ldiscs[disc] = NULL; |
Peter Hurley | 137084b | 2013-06-15 07:04:46 -0400 | [diff] [blame] | 105 | raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags); |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 106 | |
| 107 | return ret; |
| 108 | } |
| 109 | EXPORT_SYMBOL(tty_unregister_ldisc); |
| 110 | |
Linus Torvalds | f0de0e8 | 2009-08-03 16:00:15 -0700 | [diff] [blame] | 111 | static struct tty_ldisc_ops *get_ldops(int disc) |
| 112 | { |
| 113 | unsigned long flags; |
| 114 | struct tty_ldisc_ops *ldops, *ret; |
| 115 | |
Peter Hurley | 137084b | 2013-06-15 07:04:46 -0400 | [diff] [blame] | 116 | raw_spin_lock_irqsave(&tty_ldiscs_lock, flags); |
Linus Torvalds | f0de0e8 | 2009-08-03 16:00:15 -0700 | [diff] [blame] | 117 | ret = ERR_PTR(-EINVAL); |
| 118 | ldops = tty_ldiscs[disc]; |
| 119 | if (ldops) { |
| 120 | ret = ERR_PTR(-EAGAIN); |
| 121 | if (try_module_get(ldops->owner)) { |
| 122 | ldops->refcount++; |
| 123 | ret = ldops; |
| 124 | } |
| 125 | } |
Peter Hurley | 137084b | 2013-06-15 07:04:46 -0400 | [diff] [blame] | 126 | raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags); |
Linus Torvalds | f0de0e8 | 2009-08-03 16:00:15 -0700 | [diff] [blame] | 127 | return ret; |
| 128 | } |
| 129 | |
| 130 | static void put_ldops(struct tty_ldisc_ops *ldops) |
| 131 | { |
| 132 | unsigned long flags; |
| 133 | |
Peter Hurley | 137084b | 2013-06-15 07:04:46 -0400 | [diff] [blame] | 134 | raw_spin_lock_irqsave(&tty_ldiscs_lock, flags); |
Linus Torvalds | f0de0e8 | 2009-08-03 16:00:15 -0700 | [diff] [blame] | 135 | ldops->refcount--; |
| 136 | module_put(ldops->owner); |
Peter Hurley | 137084b | 2013-06-15 07:04:46 -0400 | [diff] [blame] | 137 | raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags); |
Linus Torvalds | f0de0e8 | 2009-08-03 16:00:15 -0700 | [diff] [blame] | 138 | } |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 139 | |
| 140 | /** |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 141 | * tty_ldisc_get - take a reference to an ldisc |
| 142 | * @disc: ldisc number |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 143 | * |
| 144 | * Takes a reference to a line discipline. Deals with refcounts and |
| 145 | * module locking counts. Returns NULL if the discipline is not available. |
| 146 | * Returns a pointer to the discipline and bumps the ref count if it is |
| 147 | * available |
| 148 | * |
| 149 | * Locking: |
Peter Hurley | 137084b | 2013-06-15 07:04:46 -0400 | [diff] [blame] | 150 | * takes tty_ldiscs_lock to guard against ldisc races |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 151 | */ |
| 152 | |
Peter Hurley | 3669752 | 2013-06-15 07:04:48 -0400 | [diff] [blame] | 153 | static struct tty_ldisc *tty_ldisc_get(struct tty_struct *tty, int disc) |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 154 | { |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 155 | struct tty_ldisc *ld; |
Linus Torvalds | 182274f | 2009-08-03 16:01:28 -0700 | [diff] [blame] | 156 | struct tty_ldisc_ops *ldops; |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 157 | |
| 158 | if (disc < N_TTY || disc >= NR_LDISCS) |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 159 | return ERR_PTR(-EINVAL); |
Linus Torvalds | 182274f | 2009-08-03 16:01:28 -0700 | [diff] [blame] | 160 | |
| 161 | /* |
| 162 | * Get the ldisc ops - we may need to request them to be loaded |
| 163 | * dynamically and try again. |
| 164 | */ |
| 165 | ldops = get_ldops(disc); |
| 166 | if (IS_ERR(ldops)) { |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 167 | request_module("tty-ldisc-%d", disc); |
Linus Torvalds | 182274f | 2009-08-03 16:01:28 -0700 | [diff] [blame] | 168 | ldops = get_ldops(disc); |
| 169 | if (IS_ERR(ldops)) |
| 170 | return ERR_CAST(ldops); |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 171 | } |
Linus Torvalds | 182274f | 2009-08-03 16:01:28 -0700 | [diff] [blame] | 172 | |
| 173 | ld = kmalloc(sizeof(struct tty_ldisc), GFP_KERNEL); |
| 174 | if (ld == NULL) { |
| 175 | put_ldops(ldops); |
| 176 | return ERR_PTR(-ENOMEM); |
| 177 | } |
| 178 | |
| 179 | ld->ops = ldops; |
Peter Hurley | 3669752 | 2013-06-15 07:04:48 -0400 | [diff] [blame] | 180 | ld->tty = tty; |
Ivo Sieben | 1541f84 | 2012-05-03 14:37:43 +0200 | [diff] [blame] | 181 | |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 182 | return ld; |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 183 | } |
| 184 | |
Peter Hurley | 734de24 | 2013-03-11 16:44:43 -0400 | [diff] [blame] | 185 | /** |
| 186 | * tty_ldisc_put - release the ldisc |
| 187 | * |
| 188 | * Complement of tty_ldisc_get(). |
| 189 | */ |
| 190 | static inline void tty_ldisc_put(struct tty_ldisc *ld) |
| 191 | { |
Peter Hurley | 734de24 | 2013-03-11 16:44:43 -0400 | [diff] [blame] | 192 | if (WARN_ON_ONCE(!ld)) |
| 193 | return; |
| 194 | |
Peter Hurley | 3669752 | 2013-06-15 07:04:48 -0400 | [diff] [blame] | 195 | put_ldops(ld->ops); |
Peter Hurley | 734de24 | 2013-03-11 16:44:43 -0400 | [diff] [blame] | 196 | kfree(ld); |
Peter Hurley | 734de24 | 2013-03-11 16:44:43 -0400 | [diff] [blame] | 197 | } |
| 198 | |
Alan Cox | 852e99d | 2009-06-11 12:51:41 +0100 | [diff] [blame] | 199 | 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] | 200 | { |
| 201 | return (*pos < NR_LDISCS) ? pos : NULL; |
| 202 | } |
| 203 | |
Alan Cox | 852e99d | 2009-06-11 12:51:41 +0100 | [diff] [blame] | 204 | 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] | 205 | { |
| 206 | (*pos)++; |
| 207 | return (*pos < NR_LDISCS) ? pos : NULL; |
| 208 | } |
| 209 | |
| 210 | static void tty_ldiscs_seq_stop(struct seq_file *m, void *v) |
| 211 | { |
| 212 | } |
| 213 | |
| 214 | static int tty_ldiscs_seq_show(struct seq_file *m, void *v) |
| 215 | { |
| 216 | int i = *(loff_t *)v; |
Linus Torvalds | f0de0e8 | 2009-08-03 16:00:15 -0700 | [diff] [blame] | 217 | struct tty_ldisc_ops *ldops; |
Alan Cox | 852e99d | 2009-06-11 12:51:41 +0100 | [diff] [blame] | 218 | |
Linus Torvalds | f0de0e8 | 2009-08-03 16:00:15 -0700 | [diff] [blame] | 219 | ldops = get_ldops(i); |
| 220 | if (IS_ERR(ldops)) |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 221 | return 0; |
Linus Torvalds | f0de0e8 | 2009-08-03 16:00:15 -0700 | [diff] [blame] | 222 | seq_printf(m, "%-10s %2d\n", ldops->name ? ldops->name : "???", i); |
| 223 | put_ldops(ldops); |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | static const struct seq_operations tty_ldiscs_seq_ops = { |
| 228 | .start = tty_ldiscs_seq_start, |
| 229 | .next = tty_ldiscs_seq_next, |
| 230 | .stop = tty_ldiscs_seq_stop, |
| 231 | .show = tty_ldiscs_seq_show, |
| 232 | }; |
| 233 | |
| 234 | static int proc_tty_ldiscs_open(struct inode *inode, struct file *file) |
| 235 | { |
| 236 | return seq_open(file, &tty_ldiscs_seq_ops); |
| 237 | } |
| 238 | |
| 239 | const struct file_operations tty_ldiscs_proc_fops = { |
| 240 | .owner = THIS_MODULE, |
| 241 | .open = proc_tty_ldiscs_open, |
| 242 | .read = seq_read, |
| 243 | .llseek = seq_lseek, |
| 244 | .release = seq_release, |
| 245 | }; |
| 246 | |
| 247 | /** |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 248 | * tty_ldisc_ref_wait - wait for the tty ldisc |
| 249 | * @tty: tty device |
| 250 | * |
| 251 | * Dereference the line discipline for the terminal and take a |
| 252 | * reference to it. If the line discipline is in flux then |
| 253 | * wait patiently until it changes. |
| 254 | * |
| 255 | * Note: Must not be called from an IRQ/timer context. The caller |
| 256 | * must also be careful not to hold other locks that will deadlock |
| 257 | * against a discipline change, such as an existing ldisc reference |
| 258 | * (which we check for) |
| 259 | * |
Peter Hurley | 3669752 | 2013-06-15 07:04:48 -0400 | [diff] [blame] | 260 | * Note: only callable from a file_operations routine (which |
| 261 | * guarantees tty->ldisc != NULL when the lock is acquired). |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 262 | */ |
| 263 | |
| 264 | struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty) |
| 265 | { |
Peter Hurley | 3669752 | 2013-06-15 07:04:48 -0400 | [diff] [blame] | 266 | ldsem_down_read(&tty->ldisc_sem, MAX_SCHEDULE_TIMEOUT); |
| 267 | WARN_ON(!tty->ldisc); |
| 268 | return tty->ldisc; |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 269 | } |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 270 | EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait); |
| 271 | |
| 272 | /** |
| 273 | * tty_ldisc_ref - get the tty ldisc |
| 274 | * @tty: tty device |
| 275 | * |
| 276 | * Dereference the line discipline for the terminal and take a |
| 277 | * reference to it. If the line discipline is in flux then |
| 278 | * return NULL. Can be called from IRQ and timer functions. |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 279 | */ |
| 280 | |
| 281 | struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty) |
| 282 | { |
Peter Hurley | 3669752 | 2013-06-15 07:04:48 -0400 | [diff] [blame] | 283 | struct tty_ldisc *ld = NULL; |
| 284 | |
| 285 | if (ldsem_down_read_trylock(&tty->ldisc_sem)) { |
| 286 | ld = tty->ldisc; |
| 287 | if (!ld) |
| 288 | ldsem_up_read(&tty->ldisc_sem); |
| 289 | } |
| 290 | return ld; |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 291 | } |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 292 | EXPORT_SYMBOL_GPL(tty_ldisc_ref); |
| 293 | |
| 294 | /** |
| 295 | * tty_ldisc_deref - free a tty ldisc reference |
| 296 | * @ld: reference to free up |
| 297 | * |
| 298 | * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May |
| 299 | * be called in IRQ context. |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 300 | */ |
| 301 | |
| 302 | void tty_ldisc_deref(struct tty_ldisc *ld) |
| 303 | { |
Peter Hurley | 3669752 | 2013-06-15 07:04:48 -0400 | [diff] [blame] | 304 | ldsem_up_read(&ld->tty->ldisc_sem); |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 305 | } |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 306 | EXPORT_SYMBOL_GPL(tty_ldisc_deref); |
| 307 | |
Peter Hurley | d2c4389 | 2013-06-15 07:04:47 -0400 | [diff] [blame] | 308 | |
| 309 | static inline int __lockfunc |
Peter Hurley | e80a10e | 2014-11-05 12:13:06 -0500 | [diff] [blame] | 310 | __tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout) |
Peter Hurley | d2c4389 | 2013-06-15 07:04:47 -0400 | [diff] [blame] | 311 | { |
| 312 | return ldsem_down_write(&tty->ldisc_sem, timeout); |
| 313 | } |
| 314 | |
| 315 | static inline int __lockfunc |
Peter Hurley | e80a10e | 2014-11-05 12:13:06 -0500 | [diff] [blame] | 316 | __tty_ldisc_lock_nested(struct tty_struct *tty, unsigned long timeout) |
Peter Hurley | d2c4389 | 2013-06-15 07:04:47 -0400 | [diff] [blame] | 317 | { |
| 318 | return ldsem_down_write_nested(&tty->ldisc_sem, |
| 319 | LDISC_SEM_OTHER, timeout); |
| 320 | } |
| 321 | |
Peter Hurley | e80a10e | 2014-11-05 12:13:06 -0500 | [diff] [blame] | 322 | static inline void __tty_ldisc_unlock(struct tty_struct *tty) |
Peter Hurley | d2c4389 | 2013-06-15 07:04:47 -0400 | [diff] [blame] | 323 | { |
| 324 | return ldsem_up_write(&tty->ldisc_sem); |
| 325 | } |
| 326 | |
| 327 | static int __lockfunc |
Peter Hurley | fae76e9 | 2014-11-05 12:13:07 -0500 | [diff] [blame] | 328 | tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout) |
| 329 | { |
| 330 | int ret; |
| 331 | |
| 332 | ret = __tty_ldisc_lock(tty, timeout); |
| 333 | if (!ret) |
| 334 | return -EBUSY; |
| 335 | set_bit(TTY_LDISC_HALTED, &tty->flags); |
| 336 | return 0; |
| 337 | } |
| 338 | |
| 339 | static void tty_ldisc_unlock(struct tty_struct *tty) |
| 340 | { |
| 341 | clear_bit(TTY_LDISC_HALTED, &tty->flags); |
| 342 | __tty_ldisc_unlock(tty); |
| 343 | } |
| 344 | |
| 345 | static int __lockfunc |
Peter Hurley | d2c4389 | 2013-06-15 07:04:47 -0400 | [diff] [blame] | 346 | tty_ldisc_lock_pair_timeout(struct tty_struct *tty, struct tty_struct *tty2, |
| 347 | unsigned long timeout) |
| 348 | { |
| 349 | int ret; |
| 350 | |
| 351 | if (tty < tty2) { |
Peter Hurley | e80a10e | 2014-11-05 12:13:06 -0500 | [diff] [blame] | 352 | ret = __tty_ldisc_lock(tty, timeout); |
Peter Hurley | d2c4389 | 2013-06-15 07:04:47 -0400 | [diff] [blame] | 353 | if (ret) { |
Peter Hurley | e80a10e | 2014-11-05 12:13:06 -0500 | [diff] [blame] | 354 | ret = __tty_ldisc_lock_nested(tty2, timeout); |
Peter Hurley | d2c4389 | 2013-06-15 07:04:47 -0400 | [diff] [blame] | 355 | if (!ret) |
Peter Hurley | e80a10e | 2014-11-05 12:13:06 -0500 | [diff] [blame] | 356 | __tty_ldisc_unlock(tty); |
Peter Hurley | d2c4389 | 2013-06-15 07:04:47 -0400 | [diff] [blame] | 357 | } |
| 358 | } else { |
| 359 | /* if this is possible, it has lots of implications */ |
| 360 | WARN_ON_ONCE(tty == tty2); |
| 361 | if (tty2 && tty != tty2) { |
Peter Hurley | e80a10e | 2014-11-05 12:13:06 -0500 | [diff] [blame] | 362 | ret = __tty_ldisc_lock(tty2, timeout); |
Peter Hurley | d2c4389 | 2013-06-15 07:04:47 -0400 | [diff] [blame] | 363 | if (ret) { |
Peter Hurley | e80a10e | 2014-11-05 12:13:06 -0500 | [diff] [blame] | 364 | ret = __tty_ldisc_lock_nested(tty, timeout); |
Peter Hurley | d2c4389 | 2013-06-15 07:04:47 -0400 | [diff] [blame] | 365 | if (!ret) |
Peter Hurley | e80a10e | 2014-11-05 12:13:06 -0500 | [diff] [blame] | 366 | __tty_ldisc_unlock(tty2); |
Peter Hurley | d2c4389 | 2013-06-15 07:04:47 -0400 | [diff] [blame] | 367 | } |
| 368 | } else |
Peter Hurley | e80a10e | 2014-11-05 12:13:06 -0500 | [diff] [blame] | 369 | ret = __tty_ldisc_lock(tty, timeout); |
Peter Hurley | d2c4389 | 2013-06-15 07:04:47 -0400 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | if (!ret) |
| 373 | return -EBUSY; |
| 374 | |
| 375 | set_bit(TTY_LDISC_HALTED, &tty->flags); |
| 376 | if (tty2) |
| 377 | set_bit(TTY_LDISC_HALTED, &tty2->flags); |
| 378 | return 0; |
| 379 | } |
| 380 | |
| 381 | static void __lockfunc |
| 382 | tty_ldisc_lock_pair(struct tty_struct *tty, struct tty_struct *tty2) |
| 383 | { |
| 384 | tty_ldisc_lock_pair_timeout(tty, tty2, MAX_SCHEDULE_TIMEOUT); |
| 385 | } |
| 386 | |
| 387 | static void __lockfunc tty_ldisc_unlock_pair(struct tty_struct *tty, |
| 388 | struct tty_struct *tty2) |
| 389 | { |
Peter Hurley | e80a10e | 2014-11-05 12:13:06 -0500 | [diff] [blame] | 390 | __tty_ldisc_unlock(tty); |
Peter Hurley | d2c4389 | 2013-06-15 07:04:47 -0400 | [diff] [blame] | 391 | if (tty2) |
Peter Hurley | e80a10e | 2014-11-05 12:13:06 -0500 | [diff] [blame] | 392 | __tty_ldisc_unlock(tty2); |
Peter Hurley | d2c4389 | 2013-06-15 07:04:47 -0400 | [diff] [blame] | 393 | } |
| 394 | |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 395 | /** |
Alan Cox | f2c4c65 | 2009-06-11 12:50:58 +0100 | [diff] [blame] | 396 | * tty_ldisc_flush - flush line discipline queue |
| 397 | * @tty: tty |
| 398 | * |
Peter Hurley | 86c80a8 | 2014-11-05 12:13:09 -0500 | [diff] [blame] | 399 | * Flush the line discipline queue (if any) and the tty flip buffers |
| 400 | * for this tty. |
Alan Cox | f2c4c65 | 2009-06-11 12:50:58 +0100 | [diff] [blame] | 401 | */ |
| 402 | |
| 403 | void tty_ldisc_flush(struct tty_struct *tty) |
| 404 | { |
| 405 | struct tty_ldisc *ld = tty_ldisc_ref(tty); |
Peter Hurley | 86c80a8 | 2014-11-05 12:13:09 -0500 | [diff] [blame] | 406 | |
| 407 | tty_buffer_flush(tty, ld); |
| 408 | if (ld) |
Alan Cox | f2c4c65 | 2009-06-11 12:50:58 +0100 | [diff] [blame] | 409 | tty_ldisc_deref(ld); |
Alan Cox | f2c4c65 | 2009-06-11 12:50:58 +0100 | [diff] [blame] | 410 | } |
Alan Cox | f2c4c65 | 2009-06-11 12:50:58 +0100 | [diff] [blame] | 411 | EXPORT_SYMBOL_GPL(tty_ldisc_flush); |
| 412 | |
| 413 | /** |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 414 | * tty_set_termios_ldisc - set ldisc field |
| 415 | * @tty: tty structure |
| 416 | * @num: line discipline number |
| 417 | * |
| 418 | * This is probably overkill for real world processors but |
| 419 | * they are not on hot paths so a little discipline won't do |
| 420 | * any harm. |
| 421 | * |
Peter Hurley | 6a1c068 | 2013-06-15 09:14:23 -0400 | [diff] [blame] | 422 | * Locking: takes termios_rwsem |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 423 | */ |
| 424 | |
| 425 | static void tty_set_termios_ldisc(struct tty_struct *tty, int num) |
| 426 | { |
Peter Hurley | 6a1c068 | 2013-06-15 09:14:23 -0400 | [diff] [blame] | 427 | down_write(&tty->termios_rwsem); |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 428 | tty->termios.c_line = num; |
Peter Hurley | 6a1c068 | 2013-06-15 09:14:23 -0400 | [diff] [blame] | 429 | up_write(&tty->termios_rwsem); |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 430 | } |
| 431 | |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 432 | /** |
| 433 | * tty_ldisc_open - open a line discipline |
| 434 | * @tty: tty we are opening the ldisc on |
| 435 | * @ld: discipline to open |
| 436 | * |
| 437 | * A helper opening method. Also a convenient debugging and check |
| 438 | * point. |
Arnd Bergmann | ec79d60 | 2010-06-01 22:53:01 +0200 | [diff] [blame] | 439 | * |
| 440 | * Locking: always called with BTM already held. |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 441 | */ |
| 442 | |
| 443 | static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld) |
| 444 | { |
| 445 | WARN_ON(test_and_set_bit(TTY_LDISC_OPEN, &tty->flags)); |
Alan Cox | f18f949 | 2009-11-30 13:18:35 +0000 | [diff] [blame] | 446 | if (ld->ops->open) { |
| 447 | int ret; |
Arnd Bergmann | ec79d60 | 2010-06-01 22:53:01 +0200 | [diff] [blame] | 448 | /* BTM here locks versus a hangup event */ |
Alan Cox | f18f949 | 2009-11-30 13:18:35 +0000 | [diff] [blame] | 449 | ret = ld->ops->open(tty); |
Jiri Slaby | 7f90cfc | 2010-11-25 00:27:54 +0100 | [diff] [blame] | 450 | if (ret) |
| 451 | clear_bit(TTY_LDISC_OPEN, &tty->flags); |
Alan Cox | f18f949 | 2009-11-30 13:18:35 +0000 | [diff] [blame] | 452 | return ret; |
| 453 | } |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 454 | return 0; |
| 455 | } |
| 456 | |
| 457 | /** |
| 458 | * tty_ldisc_close - close a line discipline |
| 459 | * @tty: tty we are opening the ldisc on |
| 460 | * @ld: discipline to close |
| 461 | * |
| 462 | * A helper close method. Also a convenient debugging and check |
| 463 | * point. |
| 464 | */ |
| 465 | |
| 466 | static void tty_ldisc_close(struct tty_struct *tty, struct tty_ldisc *ld) |
| 467 | { |
| 468 | WARN_ON(!test_bit(TTY_LDISC_OPEN, &tty->flags)); |
| 469 | clear_bit(TTY_LDISC_OPEN, &tty->flags); |
| 470 | if (ld->ops->close) |
| 471 | ld->ops->close(tty); |
| 472 | } |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 473 | |
| 474 | /** |
| 475 | * tty_ldisc_restore - helper for tty ldisc change |
| 476 | * @tty: tty to recover |
| 477 | * @old: previous ldisc |
| 478 | * |
| 479 | * Restore the previous line discipline or N_TTY when a line discipline |
| 480 | * change fails due to an open error |
| 481 | */ |
| 482 | |
| 483 | static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old) |
| 484 | { |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 485 | struct tty_ldisc *new_ldisc; |
| 486 | int r; |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 487 | |
| 488 | /* There is an outstanding reference here so this is safe */ |
Peter Hurley | 3669752 | 2013-06-15 07:04:48 -0400 | [diff] [blame] | 489 | old = tty_ldisc_get(tty, old->ops->num); |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 490 | WARN_ON(IS_ERR(old)); |
Peter Hurley | f4807045 | 2013-03-11 16:44:42 -0400 | [diff] [blame] | 491 | tty->ldisc = old; |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 492 | tty_set_termios_ldisc(tty, old->ops->num); |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 493 | if (tty_ldisc_open(tty, old) < 0) { |
| 494 | tty_ldisc_put(old); |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 495 | /* This driver is always present */ |
Peter Hurley | 3669752 | 2013-06-15 07:04:48 -0400 | [diff] [blame] | 496 | new_ldisc = tty_ldisc_get(tty, N_TTY); |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 497 | if (IS_ERR(new_ldisc)) |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 498 | panic("n_tty: get"); |
Peter Hurley | f4807045 | 2013-03-11 16:44:42 -0400 | [diff] [blame] | 499 | tty->ldisc = new_ldisc; |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 500 | tty_set_termios_ldisc(tty, N_TTY); |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 501 | r = tty_ldisc_open(tty, new_ldisc); |
| 502 | if (r < 0) |
| 503 | panic("Couldn't open N_TTY ldisc for " |
| 504 | "%s --- error %d.", |
Rasmus Villemoes | 429b474 | 2015-03-31 15:55:59 +0200 | [diff] [blame] | 505 | tty_name(tty), r); |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 506 | } |
| 507 | } |
| 508 | |
| 509 | /** |
| 510 | * tty_set_ldisc - set line discipline |
| 511 | * @tty: the terminal to set |
| 512 | * @ldisc: the line discipline |
| 513 | * |
| 514 | * 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] | 515 | * context. The ldisc change logic has to protect itself against any |
| 516 | * overlapping ldisc change (including on the other end of pty pairs), |
| 517 | * 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] | 518 | */ |
| 519 | |
| 520 | int tty_set_ldisc(struct tty_struct *tty, int ldisc) |
| 521 | { |
| 522 | int retval; |
Peter Hurley | 9fbfa34 | 2013-06-15 07:04:49 -0400 | [diff] [blame] | 523 | struct tty_ldisc *old_ldisc, *new_ldisc; |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 524 | |
Peter Hurley | 3669752 | 2013-06-15 07:04:48 -0400 | [diff] [blame] | 525 | new_ldisc = tty_ldisc_get(tty, ldisc); |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 526 | if (IS_ERR(new_ldisc)) |
| 527 | return PTR_ERR(new_ldisc); |
| 528 | |
Peter Hurley | c8483bc | 2014-11-05 12:12:45 -0500 | [diff] [blame] | 529 | tty_lock(tty); |
Peter Hurley | 276a661 | 2014-11-05 12:13:08 -0500 | [diff] [blame] | 530 | retval = tty_ldisc_lock(tty, 5 * HZ); |
Peter Hurley | 3669752 | 2013-06-15 07:04:48 -0400 | [diff] [blame] | 531 | if (retval) { |
| 532 | tty_ldisc_put(new_ldisc); |
Peter Hurley | c8483bc | 2014-11-05 12:12:45 -0500 | [diff] [blame] | 533 | tty_unlock(tty); |
Peter Hurley | 3669752 | 2013-06-15 07:04:48 -0400 | [diff] [blame] | 534 | return retval; |
| 535 | } |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 536 | |
| 537 | /* |
| 538 | * Check the no-op case |
| 539 | */ |
| 540 | |
| 541 | if (tty->ldisc->ops->num == ldisc) { |
Peter Hurley | 276a661 | 2014-11-05 12:13:08 -0500 | [diff] [blame] | 542 | tty_ldisc_unlock(tty); |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 543 | tty_ldisc_put(new_ldisc); |
Peter Hurley | c8483bc | 2014-11-05 12:12:45 -0500 | [diff] [blame] | 544 | tty_unlock(tty); |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 545 | return 0; |
| 546 | } |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 547 | |
Peter Hurley | 9fbfa34 | 2013-06-15 07:04:49 -0400 | [diff] [blame] | 548 | old_ldisc = tty->ldisc; |
Jiri Slaby | 100eeae | 2010-10-31 23:17:51 +0100 | [diff] [blame] | 549 | |
Peter Hurley | 3ff51a1 | 2014-11-05 12:12:46 -0500 | [diff] [blame] | 550 | if (test_bit(TTY_HUPPED, &tty->flags)) { |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 551 | /* We were raced by the hangup method. It will have stomped |
| 552 | the ldisc data and closed the ldisc down */ |
Peter Hurley | 276a661 | 2014-11-05 12:13:08 -0500 | [diff] [blame] | 553 | tty_ldisc_unlock(tty); |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 554 | tty_ldisc_put(new_ldisc); |
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 555 | tty_unlock(tty); |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 556 | return -EIO; |
| 557 | } |
| 558 | |
Peter Hurley | 9fbfa34 | 2013-06-15 07:04:49 -0400 | [diff] [blame] | 559 | /* Shutdown the old discipline. */ |
| 560 | tty_ldisc_close(tty, old_ldisc); |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 561 | |
| 562 | /* Now set up the new line discipline. */ |
Peter Hurley | f4807045 | 2013-03-11 16:44:42 -0400 | [diff] [blame] | 563 | tty->ldisc = new_ldisc; |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 564 | tty_set_termios_ldisc(tty, ldisc); |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 565 | |
| 566 | retval = tty_ldisc_open(tty, new_ldisc); |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 567 | if (retval < 0) { |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 568 | /* Back to the old one or N_TTY if we can't */ |
| 569 | tty_ldisc_put(new_ldisc); |
Peter Hurley | 9fbfa34 | 2013-06-15 07:04:49 -0400 | [diff] [blame] | 570 | tty_ldisc_restore(tty, old_ldisc); |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 571 | } |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 572 | |
Peter Hurley | 9191aaa | 2014-11-05 13:11:41 -0500 | [diff] [blame] | 573 | if (tty->ldisc->ops->num != old_ldisc->ops->num && tty->ops->set_ldisc) { |
| 574 | down_read(&tty->termios_rwsem); |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 575 | tty->ops->set_ldisc(tty); |
Peter Hurley | 9191aaa | 2014-11-05 13:11:41 -0500 | [diff] [blame] | 576 | up_read(&tty->termios_rwsem); |
| 577 | } |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 578 | |
Peter Hurley | b0e9585 | 2013-06-15 07:04:51 -0400 | [diff] [blame] | 579 | /* At this point we hold a reference to the new ldisc and a |
| 580 | reference to the old ldisc, or we hold two references to |
| 581 | the old ldisc (if it was restored as part of error cleanup |
| 582 | above). In either case, releasing a single reference from |
| 583 | the old ldisc is correct. */ |
| 584 | |
Peter Hurley | 9fbfa34 | 2013-06-15 07:04:49 -0400 | [diff] [blame] | 585 | tty_ldisc_put(old_ldisc); |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 586 | |
| 587 | /* |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 588 | * Allow ldisc referencing to occur again |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 589 | */ |
Peter Hurley | 276a661 | 2014-11-05 12:13:08 -0500 | [diff] [blame] | 590 | tty_ldisc_unlock(tty); |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 591 | |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 592 | /* 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] | 593 | already running */ |
Peter Hurley | 4f98d46 | 2013-03-11 16:44:34 -0400 | [diff] [blame] | 594 | schedule_work(&tty->port->buf.work); |
Peter Hurley | 4f98d46 | 2013-03-11 16:44:34 -0400 | [diff] [blame] | 595 | |
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 596 | tty_unlock(tty); |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 597 | return retval; |
| 598 | } |
| 599 | |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 600 | /** |
| 601 | * tty_reset_termios - reset terminal state |
| 602 | * @tty: tty to reset |
| 603 | * |
| 604 | * Restore a terminal to the driver default state. |
| 605 | */ |
| 606 | |
| 607 | static void tty_reset_termios(struct tty_struct *tty) |
| 608 | { |
Peter Hurley | 6a1c068 | 2013-06-15 09:14:23 -0400 | [diff] [blame] | 609 | down_write(&tty->termios_rwsem); |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 610 | tty->termios = tty->driver->init_termios; |
| 611 | tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios); |
| 612 | tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios); |
Peter Hurley | 6a1c068 | 2013-06-15 09:14:23 -0400 | [diff] [blame] | 613 | up_write(&tty->termios_rwsem); |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | |
| 617 | /** |
| 618 | * tty_ldisc_reinit - reinitialise the tty ldisc |
| 619 | * @tty: tty to reinit |
Alan Cox | 638b964 | 2010-02-08 10:09:26 +0000 | [diff] [blame] | 620 | * @ldisc: line discipline to reinitialize |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 621 | * |
Alan Cox | 638b964 | 2010-02-08 10:09:26 +0000 | [diff] [blame] | 622 | * Switch the tty to a line discipline and leave the ldisc |
| 623 | * state closed |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 624 | */ |
| 625 | |
Philippe Rétornaz | 1c95ba1 | 2010-10-27 17:13:21 +0200 | [diff] [blame] | 626 | static int tty_ldisc_reinit(struct tty_struct *tty, int ldisc) |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 627 | { |
Peter Hurley | 3669752 | 2013-06-15 07:04:48 -0400 | [diff] [blame] | 628 | struct tty_ldisc *ld = tty_ldisc_get(tty, ldisc); |
Philippe Rétornaz | 1c95ba1 | 2010-10-27 17:13:21 +0200 | [diff] [blame] | 629 | |
| 630 | if (IS_ERR(ld)) |
| 631 | return -1; |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 632 | |
| 633 | tty_ldisc_close(tty, tty->ldisc); |
| 634 | tty_ldisc_put(tty->ldisc); |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 635 | /* |
| 636 | * Switch the line discipline back |
| 637 | */ |
Peter Hurley | f4807045 | 2013-03-11 16:44:42 -0400 | [diff] [blame] | 638 | tty->ldisc = ld; |
Alan Cox | 638b964 | 2010-02-08 10:09:26 +0000 | [diff] [blame] | 639 | tty_set_termios_ldisc(tty, ldisc); |
Philippe Rétornaz | 1c95ba1 | 2010-10-27 17:13:21 +0200 | [diff] [blame] | 640 | |
| 641 | return 0; |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | /** |
| 645 | * tty_ldisc_hangup - hangup ldisc reset |
| 646 | * @tty: tty being hung up |
| 647 | * |
| 648 | * Some tty devices reset their termios when they receive a hangup |
| 649 | * event. In that situation we must also switch back to N_TTY properly |
| 650 | * before we reset the termios data. |
| 651 | * |
| 652 | * Locking: We can take the ldisc mutex as the rest of the code is |
| 653 | * careful to allow for this. |
| 654 | * |
| 655 | * In the pty pair case this occurs in the close() path of the |
| 656 | * tty itself so we must be careful about locking rules. |
| 657 | */ |
| 658 | |
| 659 | void tty_ldisc_hangup(struct tty_struct *tty) |
| 660 | { |
| 661 | struct tty_ldisc *ld; |
Alan Cox | 638b964 | 2010-02-08 10:09:26 +0000 | [diff] [blame] | 662 | int reset = tty->driver->flags & TTY_DRIVER_RESET_TERMIOS; |
| 663 | int err = 0; |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 664 | |
Peter Hurley | fc575ee | 2013-03-11 16:44:38 -0400 | [diff] [blame] | 665 | tty_ldisc_debug(tty, "closing ldisc: %p\n", tty->ldisc); |
| 666 | |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 667 | ld = tty_ldisc_ref(tty); |
| 668 | if (ld != NULL) { |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 669 | if (ld->ops->flush_buffer) |
| 670 | ld->ops->flush_buffer(tty); |
| 671 | tty_driver_flush_buffer(tty); |
| 672 | if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) && |
| 673 | ld->ops->write_wakeup) |
| 674 | ld->ops->write_wakeup(tty); |
| 675 | if (ld->ops->hangup) |
| 676 | ld->ops->hangup(tty); |
| 677 | tty_ldisc_deref(ld); |
| 678 | } |
Peter Hurley | 3669752 | 2013-06-15 07:04:48 -0400 | [diff] [blame] | 679 | |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 680 | wake_up_interruptible_poll(&tty->write_wait, POLLOUT); |
| 681 | wake_up_interruptible_poll(&tty->read_wait, POLLIN); |
Peter Hurley | 3669752 | 2013-06-15 07:04:48 -0400 | [diff] [blame] | 682 | |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 683 | /* |
| 684 | * Shutdown the current line discipline, and reset it to |
Alan Cox | 638b964 | 2010-02-08 10:09:26 +0000 | [diff] [blame] | 685 | * N_TTY if need be. |
| 686 | * |
| 687 | * Avoid racing set_ldisc or tty_ldisc_release |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 688 | */ |
Peter Hurley | fae76e9 | 2014-11-05 12:13:07 -0500 | [diff] [blame] | 689 | tty_ldisc_lock(tty, MAX_SCHEDULE_TIMEOUT); |
Arnd Bergmann | 60af22d | 2010-06-01 22:53:06 +0200 | [diff] [blame] | 690 | |
Peter Hurley | 3669752 | 2013-06-15 07:04:48 -0400 | [diff] [blame] | 691 | if (tty->ldisc) { |
Peter Hurley | c878524 | 2013-03-11 16:44:36 -0400 | [diff] [blame] | 692 | |
| 693 | /* At this point we have a halted ldisc; we want to close it and |
| 694 | reopen a new ldisc. We could defer the reopen to the next |
| 695 | open but it means auditing a lot of other paths so this is |
| 696 | a FIXME */ |
Alan Cox | 638b964 | 2010-02-08 10:09:26 +0000 | [diff] [blame] | 697 | if (reset == 0) { |
Philippe Rétornaz | 1c95ba1 | 2010-10-27 17:13:21 +0200 | [diff] [blame] | 698 | |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 699 | if (!tty_ldisc_reinit(tty, tty->termios.c_line)) |
Philippe Rétornaz | 1c95ba1 | 2010-10-27 17:13:21 +0200 | [diff] [blame] | 700 | err = tty_ldisc_open(tty, tty->ldisc); |
| 701 | else |
| 702 | err = 1; |
Alan Cox | c8d5004 | 2009-07-16 16:05:08 +0100 | [diff] [blame] | 703 | } |
Alan Cox | 638b964 | 2010-02-08 10:09:26 +0000 | [diff] [blame] | 704 | /* If the re-open fails or we reset then go to N_TTY. The |
| 705 | N_TTY open cannot fail */ |
| 706 | if (reset || err) { |
Philippe Rétornaz | 1c95ba1 | 2010-10-27 17:13:21 +0200 | [diff] [blame] | 707 | BUG_ON(tty_ldisc_reinit(tty, N_TTY)); |
Alan Cox | 638b964 | 2010-02-08 10:09:26 +0000 | [diff] [blame] | 708 | WARN_ON(tty_ldisc_open(tty, tty->ldisc)); |
| 709 | } |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 710 | } |
Peter Hurley | fae76e9 | 2014-11-05 12:13:07 -0500 | [diff] [blame] | 711 | tty_ldisc_unlock(tty); |
Alan Cox | 638b964 | 2010-02-08 10:09:26 +0000 | [diff] [blame] | 712 | if (reset) |
| 713 | tty_reset_termios(tty); |
Peter Hurley | fc575ee | 2013-03-11 16:44:38 -0400 | [diff] [blame] | 714 | |
| 715 | tty_ldisc_debug(tty, "re-opened ldisc: %p\n", tty->ldisc); |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 716 | } |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 717 | |
| 718 | /** |
| 719 | * tty_ldisc_setup - open line discipline |
| 720 | * @tty: tty being shut down |
| 721 | * @o_tty: pair tty for pty/tty pairs |
| 722 | * |
| 723 | * 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] | 724 | * line disciplines and bind them to the tty. This has no locking issues |
| 725 | * as the device isn't yet active. |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 726 | */ |
| 727 | |
| 728 | int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty) |
| 729 | { |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 730 | struct tty_ldisc *ld = tty->ldisc; |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 731 | int retval; |
| 732 | |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 733 | retval = tty_ldisc_open(tty, ld); |
| 734 | if (retval) |
| 735 | return retval; |
| 736 | |
| 737 | if (o_tty) { |
| 738 | retval = tty_ldisc_open(o_tty, o_tty->ldisc); |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 739 | if (retval) { |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 740 | tty_ldisc_close(tty, ld); |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 741 | return retval; |
| 742 | } |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 743 | } |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 744 | return 0; |
| 745 | } |
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 746 | |
| 747 | static void tty_ldisc_kill(struct tty_struct *tty) |
| 748 | { |
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 749 | /* |
| 750 | * Now kill off the ldisc |
| 751 | */ |
| 752 | tty_ldisc_close(tty, tty->ldisc); |
| 753 | tty_ldisc_put(tty->ldisc); |
| 754 | /* Force an oops if we mess this up */ |
| 755 | tty->ldisc = NULL; |
| 756 | |
| 757 | /* Ensure the next open requests the N_TTY ldisc */ |
| 758 | tty_set_termios_ldisc(tty, N_TTY); |
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 759 | } |
| 760 | |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 761 | /** |
| 762 | * tty_ldisc_release - release line discipline |
Peter Hurley | 62462ae | 2014-11-05 12:12:58 -0500 | [diff] [blame] | 763 | * @tty: tty being shut down (or one end of pty pair) |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 764 | * |
Peter Hurley | 62462ae | 2014-11-05 12:12:58 -0500 | [diff] [blame] | 765 | * Called during the final close of a tty or a pty pair in order to shut |
| 766 | * down the line discpline layer. On exit, each ldisc assigned is N_TTY and |
| 767 | * each ldisc has not been opened. |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 768 | */ |
| 769 | |
Peter Hurley | 62462ae | 2014-11-05 12:12:58 -0500 | [diff] [blame] | 770 | void tty_ldisc_release(struct tty_struct *tty) |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 771 | { |
Peter Hurley | 62462ae | 2014-11-05 12:12:58 -0500 | [diff] [blame] | 772 | struct tty_struct *o_tty = tty->link; |
| 773 | |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 774 | /* |
Peter Hurley | a2965b7 | 2013-03-11 16:44:35 -0400 | [diff] [blame] | 775 | * Shutdown this line discipline. As this is the final close, |
| 776 | * it does not race with the set_ldisc code path. |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 777 | */ |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 778 | |
Peter Hurley | fc575ee | 2013-03-11 16:44:38 -0400 | [diff] [blame] | 779 | tty_ldisc_debug(tty, "closing ldisc: %p\n", tty->ldisc); |
| 780 | |
Peter Hurley | 3669752 | 2013-06-15 07:04:48 -0400 | [diff] [blame] | 781 | tty_ldisc_lock_pair(tty, o_tty); |
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 782 | tty_ldisc_kill(tty); |
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 783 | if (o_tty) |
| 784 | tty_ldisc_kill(o_tty); |
Peter Hurley | 3669752 | 2013-06-15 07:04:48 -0400 | [diff] [blame] | 785 | tty_ldisc_unlock_pair(tty, o_tty); |
| 786 | |
Alan Cox | aef29bc | 2009-06-29 15:21:47 +0100 | [diff] [blame] | 787 | /* And the memory resources remaining (buffers, termios) will be |
| 788 | disposed of when the kref hits zero */ |
Peter Hurley | fc575ee | 2013-03-11 16:44:38 -0400 | [diff] [blame] | 789 | |
| 790 | tty_ldisc_debug(tty, "ldisc closed\n"); |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | /** |
| 794 | * tty_ldisc_init - ldisc setup for new tty |
| 795 | * @tty: tty being allocated |
| 796 | * |
| 797 | * Set up the line discipline objects for a newly allocated tty. Note that |
| 798 | * the tty structure is not completely set up when this call is made. |
| 799 | */ |
| 800 | |
| 801 | void tty_ldisc_init(struct tty_struct *tty) |
| 802 | { |
Peter Hurley | 3669752 | 2013-06-15 07:04:48 -0400 | [diff] [blame] | 803 | struct tty_ldisc *ld = tty_ldisc_get(tty, N_TTY); |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 804 | if (IS_ERR(ld)) |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 805 | panic("n_tty: init_tty"); |
Peter Hurley | f4807045 | 2013-03-11 16:44:42 -0400 | [diff] [blame] | 806 | tty->ldisc = ld; |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 807 | } |
| 808 | |
Jiri Slaby | 6716671 | 2011-03-23 10:48:35 +0100 | [diff] [blame] | 809 | /** |
| 810 | * tty_ldisc_init - ldisc cleanup for new tty |
| 811 | * @tty: tty that was allocated recently |
| 812 | * |
| 813 | * The tty structure must not becompletely set up (tty_ldisc_setup) when |
| 814 | * this call is made. |
| 815 | */ |
| 816 | void tty_ldisc_deinit(struct tty_struct *tty) |
| 817 | { |
Peter Hurley | ebc9bae | 2013-03-11 16:44:40 -0400 | [diff] [blame] | 818 | tty_ldisc_put(tty->ldisc); |
Peter Hurley | f4807045 | 2013-03-11 16:44:42 -0400 | [diff] [blame] | 819 | tty->ldisc = NULL; |
Jiri Slaby | 6716671 | 2011-03-23 10:48:35 +0100 | [diff] [blame] | 820 | } |
| 821 | |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 822 | void tty_ldisc_begin(void) |
| 823 | { |
| 824 | /* Setup the default TTY line discipline. */ |
| 825 | (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY); |
| 826 | } |