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