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