]>
Commit | Line | Data |
---|---|---|
e3b3d0f5 | 1 | // SPDX-License-Identifier: GPL-2.0 |
01e1abb2 | 2 | #include <linux/types.h> |
01e1abb2 | 3 | #include <linux/errno.h> |
8b3ffa17 | 4 | #include <linux/kmod.h> |
01e1abb2 AC |
5 | #include <linux/sched.h> |
6 | #include <linux/interrupt.h> | |
7 | #include <linux/tty.h> | |
8 | #include <linux/tty_driver.h> | |
01e1abb2 | 9 | #include <linux/file.h> |
01e1abb2 AC |
10 | #include <linux/mm.h> |
11 | #include <linux/string.h> | |
12 | #include <linux/slab.h> | |
13 | #include <linux/poll.h> | |
14 | #include <linux/proc_fs.h> | |
01e1abb2 | 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 | |
0a6adc13 | 26 | #define tty_ldisc_debug(tty, f, args...) tty_debug(tty, f, ##args) |
fc575ee6 PH |
27 | #else |
28 | #define tty_ldisc_debug(tty, f, args...) | |
29 | #endif | |
30 | ||
d2c43890 PH |
31 | /* lockdep nested classes for tty->ldisc_sem */ |
32 | enum { | |
33 | LDISC_SEM_NORMAL, | |
34 | LDISC_SEM_OTHER, | |
35 | }; | |
36 | ||
37 | ||
01e1abb2 AC |
38 | /* |
39 | * This guards the refcounted line discipline lists. The lock | |
40 | * must be taken with irqs off because there are hangup path | |
41 | * callers who will do ldisc lookups and cannot sleep. | |
42 | */ | |
43 | ||
137084bb | 44 | static DEFINE_RAW_SPINLOCK(tty_ldiscs_lock); |
01e1abb2 AC |
45 | /* Line disc dispatch table */ |
46 | static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS]; | |
47 | ||
48 | /** | |
49 | * tty_register_ldisc - install a line discipline | |
50 | * @disc: ldisc number | |
51 | * @new_ldisc: pointer to the ldisc object | |
52 | * | |
53 | * Installs a new line discipline into the kernel. The discipline | |
54 | * is set up as unreferenced and then made available to the kernel | |
55 | * from this point onwards. | |
56 | * | |
57 | * Locking: | |
137084bb | 58 | * takes tty_ldiscs_lock to guard against ldisc races |
01e1abb2 AC |
59 | */ |
60 | ||
61 | int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc) | |
62 | { | |
63 | unsigned long flags; | |
64 | int ret = 0; | |
65 | ||
66 | if (disc < N_TTY || disc >= NR_LDISCS) | |
67 | return -EINVAL; | |
68 | ||
137084bb | 69 | raw_spin_lock_irqsave(&tty_ldiscs_lock, flags); |
01e1abb2 AC |
70 | tty_ldiscs[disc] = new_ldisc; |
71 | new_ldisc->num = disc; | |
72 | new_ldisc->refcount = 0; | |
137084bb | 73 | raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags); |
01e1abb2 AC |
74 | |
75 | return ret; | |
76 | } | |
77 | EXPORT_SYMBOL(tty_register_ldisc); | |
78 | ||
79 | /** | |
80 | * tty_unregister_ldisc - unload a line discipline | |
81 | * @disc: ldisc number | |
82 | * @new_ldisc: pointer to the ldisc object | |
83 | * | |
84 | * Remove a line discipline from the kernel providing it is not | |
85 | * currently in use. | |
86 | * | |
87 | * Locking: | |
137084bb | 88 | * takes tty_ldiscs_lock to guard against ldisc races |
01e1abb2 AC |
89 | */ |
90 | ||
91 | int tty_unregister_ldisc(int disc) | |
92 | { | |
93 | unsigned long flags; | |
94 | int ret = 0; | |
95 | ||
96 | if (disc < N_TTY || disc >= NR_LDISCS) | |
97 | return -EINVAL; | |
98 | ||
137084bb | 99 | raw_spin_lock_irqsave(&tty_ldiscs_lock, flags); |
01e1abb2 AC |
100 | if (tty_ldiscs[disc]->refcount) |
101 | ret = -EBUSY; | |
102 | else | |
103 | tty_ldiscs[disc] = NULL; | |
137084bb | 104 | raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags); |
01e1abb2 AC |
105 | |
106 | return ret; | |
107 | } | |
108 | EXPORT_SYMBOL(tty_unregister_ldisc); | |
109 | ||
f0de0e8d LT |
110 | static struct tty_ldisc_ops *get_ldops(int disc) |
111 | { | |
112 | unsigned long flags; | |
113 | struct tty_ldisc_ops *ldops, *ret; | |
114 | ||
137084bb | 115 | raw_spin_lock_irqsave(&tty_ldiscs_lock, flags); |
f0de0e8d LT |
116 | ret = ERR_PTR(-EINVAL); |
117 | ldops = tty_ldiscs[disc]; | |
118 | if (ldops) { | |
119 | ret = ERR_PTR(-EAGAIN); | |
120 | if (try_module_get(ldops->owner)) { | |
121 | ldops->refcount++; | |
122 | ret = ldops; | |
123 | } | |
124 | } | |
137084bb | 125 | raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags); |
f0de0e8d LT |
126 | return ret; |
127 | } | |
128 | ||
129 | static void put_ldops(struct tty_ldisc_ops *ldops) | |
130 | { | |
131 | unsigned long flags; | |
132 | ||
137084bb | 133 | raw_spin_lock_irqsave(&tty_ldiscs_lock, flags); |
f0de0e8d LT |
134 | ldops->refcount--; |
135 | module_put(ldops->owner); | |
137084bb | 136 | raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags); |
f0de0e8d | 137 | } |
01e1abb2 | 138 | |
01e1abb2 AC |
139 | /** |
140 | * tty_ldisc_get - take a reference to an ldisc | |
141 | * @disc: ldisc number | |
01e1abb2 AC |
142 | * |
143 | * Takes a reference to a line discipline. Deals with refcounts and | |
c0cc1c5d PH |
144 | * module locking counts. |
145 | * | |
146 | * Returns: -EINVAL if the discipline index is not [N_TTY..NR_LDISCS] or | |
147 | * if the discipline is not registered | |
148 | * -EAGAIN if request_module() failed to load or register the | |
149 | * the discipline | |
150 | * -ENOMEM if allocation failure | |
151 | * | |
152 | * Otherwise, returns a pointer to the discipline and bumps the | |
153 | * ref count | |
01e1abb2 AC |
154 | * |
155 | * Locking: | |
137084bb | 156 | * takes tty_ldiscs_lock to guard against ldisc races |
01e1abb2 AC |
157 | */ |
158 | ||
36697529 | 159 | static struct tty_ldisc *tty_ldisc_get(struct tty_struct *tty, int disc) |
01e1abb2 | 160 | { |
c65c9bc3 | 161 | struct tty_ldisc *ld; |
182274f8 | 162 | struct tty_ldisc_ops *ldops; |
01e1abb2 AC |
163 | |
164 | if (disc < N_TTY || disc >= NR_LDISCS) | |
c65c9bc3 | 165 | return ERR_PTR(-EINVAL); |
182274f8 LT |
166 | |
167 | /* | |
168 | * Get the ldisc ops - we may need to request them to be loaded | |
169 | * dynamically and try again. | |
170 | */ | |
171 | ldops = get_ldops(disc); | |
172 | if (IS_ERR(ldops)) { | |
01e1abb2 | 173 | request_module("tty-ldisc-%d", disc); |
182274f8 LT |
174 | ldops = get_ldops(disc); |
175 | if (IS_ERR(ldops)) | |
176 | return ERR_CAST(ldops); | |
177 | } | |
178 | ||
34807b61 TH |
179 | /* |
180 | * There is no way to handle allocation failure of only 16 bytes. | |
181 | * Let's simplify error handling and save more memory. | |
182 | */ | |
183 | ld = kmalloc(sizeof(struct tty_ldisc), GFP_KERNEL | __GFP_NOFAIL); | |
182274f8 | 184 | ld->ops = ldops; |
36697529 | 185 | ld->tty = tty; |
1541f845 | 186 | |
c65c9bc3 | 187 | return ld; |
01e1abb2 AC |
188 | } |
189 | ||
734de249 PH |
190 | /** |
191 | * tty_ldisc_put - release the ldisc | |
192 | * | |
193 | * Complement of tty_ldisc_get(). | |
194 | */ | |
cb128f69 | 195 | static void tty_ldisc_put(struct tty_ldisc *ld) |
734de249 | 196 | { |
734de249 PH |
197 | if (WARN_ON_ONCE(!ld)) |
198 | return; | |
199 | ||
36697529 | 200 | put_ldops(ld->ops); |
734de249 | 201 | kfree(ld); |
734de249 PH |
202 | } |
203 | ||
852e99d2 | 204 | static void *tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos) |
01e1abb2 AC |
205 | { |
206 | return (*pos < NR_LDISCS) ? pos : NULL; | |
207 | } | |
208 | ||
852e99d2 | 209 | static void *tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos) |
01e1abb2 AC |
210 | { |
211 | (*pos)++; | |
212 | return (*pos < NR_LDISCS) ? pos : NULL; | |
213 | } | |
214 | ||
215 | static void tty_ldiscs_seq_stop(struct seq_file *m, void *v) | |
216 | { | |
217 | } | |
218 | ||
219 | static int tty_ldiscs_seq_show(struct seq_file *m, void *v) | |
220 | { | |
221 | int i = *(loff_t *)v; | |
f0de0e8d | 222 | struct tty_ldisc_ops *ldops; |
852e99d2 | 223 | |
f0de0e8d LT |
224 | ldops = get_ldops(i); |
225 | if (IS_ERR(ldops)) | |
01e1abb2 | 226 | return 0; |
f0de0e8d LT |
227 | seq_printf(m, "%-10s %2d\n", ldops->name ? ldops->name : "???", i); |
228 | put_ldops(ldops); | |
01e1abb2 AC |
229 | return 0; |
230 | } | |
231 | ||
232 | static const struct seq_operations tty_ldiscs_seq_ops = { | |
233 | .start = tty_ldiscs_seq_start, | |
234 | .next = tty_ldiscs_seq_next, | |
235 | .stop = tty_ldiscs_seq_stop, | |
236 | .show = tty_ldiscs_seq_show, | |
237 | }; | |
238 | ||
239 | static int proc_tty_ldiscs_open(struct inode *inode, struct file *file) | |
240 | { | |
241 | return seq_open(file, &tty_ldiscs_seq_ops); | |
242 | } | |
243 | ||
244 | const struct file_operations tty_ldiscs_proc_fops = { | |
245 | .owner = THIS_MODULE, | |
246 | .open = proc_tty_ldiscs_open, | |
247 | .read = seq_read, | |
248 | .llseek = seq_lseek, | |
249 | .release = seq_release, | |
250 | }; | |
251 | ||
01e1abb2 AC |
252 | /** |
253 | * tty_ldisc_ref_wait - wait for the tty ldisc | |
254 | * @tty: tty device | |
255 | * | |
256 | * Dereference the line discipline for the terminal and take a | |
257 | * reference to it. If the line discipline is in flux then | |
258 | * wait patiently until it changes. | |
259 | * | |
892d1fa7 PH |
260 | * Returns: NULL if the tty has been hungup and not re-opened with |
261 | * a new file descriptor, otherwise valid ldisc reference | |
262 | * | |
01e1abb2 AC |
263 | * Note: Must not be called from an IRQ/timer context. The caller |
264 | * must also be careful not to hold other locks that will deadlock | |
265 | * against a discipline change, such as an existing ldisc reference | |
266 | * (which we check for) | |
267 | * | |
e55afd11 PH |
268 | * Note: a file_operations routine (read/poll/write) should use this |
269 | * function to wait for any ldisc lifetime events to finish. | |
01e1abb2 AC |
270 | */ |
271 | ||
272 | struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty) | |
273 | { | |
a4a3e061 DV |
274 | struct tty_ldisc *ld; |
275 | ||
36697529 | 276 | ldsem_down_read(&tty->ldisc_sem, MAX_SCHEDULE_TIMEOUT); |
a4a3e061 DV |
277 | ld = tty->ldisc; |
278 | if (!ld) | |
a570a49a | 279 | ldsem_up_read(&tty->ldisc_sem); |
a4a3e061 | 280 | return ld; |
01e1abb2 | 281 | } |
01e1abb2 AC |
282 | EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait); |
283 | ||
284 | /** | |
285 | * tty_ldisc_ref - get the tty ldisc | |
286 | * @tty: tty device | |
287 | * | |
288 | * Dereference the line discipline for the terminal and take a | |
289 | * reference to it. If the line discipline is in flux then | |
290 | * return NULL. Can be called from IRQ and timer functions. | |
01e1abb2 AC |
291 | */ |
292 | ||
293 | struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty) | |
294 | { | |
36697529 PH |
295 | struct tty_ldisc *ld = NULL; |
296 | ||
297 | if (ldsem_down_read_trylock(&tty->ldisc_sem)) { | |
298 | ld = tty->ldisc; | |
299 | if (!ld) | |
300 | ldsem_up_read(&tty->ldisc_sem); | |
301 | } | |
302 | return ld; | |
01e1abb2 | 303 | } |
01e1abb2 AC |
304 | EXPORT_SYMBOL_GPL(tty_ldisc_ref); |
305 | ||
306 | /** | |
307 | * tty_ldisc_deref - free a tty ldisc reference | |
308 | * @ld: reference to free up | |
309 | * | |
310 | * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May | |
311 | * be called in IRQ context. | |
01e1abb2 AC |
312 | */ |
313 | ||
314 | void tty_ldisc_deref(struct tty_ldisc *ld) | |
315 | { | |
36697529 | 316 | ldsem_up_read(&ld->tty->ldisc_sem); |
01e1abb2 | 317 | } |
01e1abb2 AC |
318 | EXPORT_SYMBOL_GPL(tty_ldisc_deref); |
319 | ||
d2c43890 | 320 | |
c2bb524b | 321 | static inline int |
e80a10ee | 322 | __tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout) |
d2c43890 PH |
323 | { |
324 | return ldsem_down_write(&tty->ldisc_sem, timeout); | |
325 | } | |
326 | ||
c2bb524b | 327 | static inline int |
e80a10ee | 328 | __tty_ldisc_lock_nested(struct tty_struct *tty, unsigned long timeout) |
d2c43890 PH |
329 | { |
330 | return ldsem_down_write_nested(&tty->ldisc_sem, | |
331 | LDISC_SEM_OTHER, timeout); | |
332 | } | |
333 | ||
e80a10ee | 334 | static inline void __tty_ldisc_unlock(struct tty_struct *tty) |
d2c43890 | 335 | { |
52772ea6 | 336 | ldsem_up_write(&tty->ldisc_sem); |
d2c43890 PH |
337 | } |
338 | ||
c9a742ab | 339 | int tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout) |
fae76e9a PH |
340 | { |
341 | int ret; | |
342 | ||
cb4321d2 DS |
343 | /* Kindly asking blocked readers to release the read side */ |
344 | set_bit(TTY_LDISC_CHANGING, &tty->flags); | |
345 | wake_up_interruptible_all(&tty->read_wait); | |
346 | wake_up_interruptible_all(&tty->write_wait); | |
347 | ||
fae76e9a PH |
348 | ret = __tty_ldisc_lock(tty, timeout); |
349 | if (!ret) | |
350 | return -EBUSY; | |
351 | set_bit(TTY_LDISC_HALTED, &tty->flags); | |
352 | return 0; | |
353 | } | |
354 | ||
c9a742ab | 355 | void tty_ldisc_unlock(struct tty_struct *tty) |
fae76e9a PH |
356 | { |
357 | clear_bit(TTY_LDISC_HALTED, &tty->flags); | |
cb4321d2 DS |
358 | /* Can be cleared here - ldisc_unlock will wake up writers firstly */ |
359 | clear_bit(TTY_LDISC_CHANGING, &tty->flags); | |
fae76e9a PH |
360 | __tty_ldisc_unlock(tty); |
361 | } | |
362 | ||
c2bb524b | 363 | static int |
d2c43890 PH |
364 | tty_ldisc_lock_pair_timeout(struct tty_struct *tty, struct tty_struct *tty2, |
365 | unsigned long timeout) | |
366 | { | |
367 | int ret; | |
368 | ||
369 | if (tty < tty2) { | |
e80a10ee | 370 | ret = __tty_ldisc_lock(tty, timeout); |
d2c43890 | 371 | if (ret) { |
e80a10ee | 372 | ret = __tty_ldisc_lock_nested(tty2, timeout); |
d2c43890 | 373 | if (!ret) |
e80a10ee | 374 | __tty_ldisc_unlock(tty); |
d2c43890 PH |
375 | } |
376 | } else { | |
377 | /* if this is possible, it has lots of implications */ | |
378 | WARN_ON_ONCE(tty == tty2); | |
379 | if (tty2 && tty != tty2) { | |
e80a10ee | 380 | ret = __tty_ldisc_lock(tty2, timeout); |
d2c43890 | 381 | if (ret) { |
e80a10ee | 382 | ret = __tty_ldisc_lock_nested(tty, timeout); |
d2c43890 | 383 | if (!ret) |
e80a10ee | 384 | __tty_ldisc_unlock(tty2); |
d2c43890 PH |
385 | } |
386 | } else | |
e80a10ee | 387 | ret = __tty_ldisc_lock(tty, timeout); |
d2c43890 PH |
388 | } |
389 | ||
390 | if (!ret) | |
391 | return -EBUSY; | |
392 | ||
393 | set_bit(TTY_LDISC_HALTED, &tty->flags); | |
394 | if (tty2) | |
395 | set_bit(TTY_LDISC_HALTED, &tty2->flags); | |
396 | return 0; | |
397 | } | |
398 | ||
c2bb524b | 399 | static void tty_ldisc_lock_pair(struct tty_struct *tty, struct tty_struct *tty2) |
d2c43890 PH |
400 | { |
401 | tty_ldisc_lock_pair_timeout(tty, tty2, MAX_SCHEDULE_TIMEOUT); | |
402 | } | |
403 | ||
c2bb524b PH |
404 | static void tty_ldisc_unlock_pair(struct tty_struct *tty, |
405 | struct tty_struct *tty2) | |
d2c43890 | 406 | { |
e80a10ee | 407 | __tty_ldisc_unlock(tty); |
d2c43890 | 408 | if (tty2) |
e80a10ee | 409 | __tty_ldisc_unlock(tty2); |
d2c43890 PH |
410 | } |
411 | ||
f2c4c65c AC |
412 | /** |
413 | * tty_ldisc_flush - flush line discipline queue | |
414 | * @tty: tty | |
415 | * | |
86c80a8e PH |
416 | * Flush the line discipline queue (if any) and the tty flip buffers |
417 | * for this tty. | |
f2c4c65c AC |
418 | */ |
419 | ||
420 | void tty_ldisc_flush(struct tty_struct *tty) | |
421 | { | |
422 | struct tty_ldisc *ld = tty_ldisc_ref(tty); | |
86c80a8e PH |
423 | |
424 | tty_buffer_flush(tty, ld); | |
425 | if (ld) | |
f2c4c65c | 426 | tty_ldisc_deref(ld); |
f2c4c65c | 427 | } |
f2c4c65c AC |
428 | EXPORT_SYMBOL_GPL(tty_ldisc_flush); |
429 | ||
01e1abb2 AC |
430 | /** |
431 | * tty_set_termios_ldisc - set ldisc field | |
432 | * @tty: tty structure | |
c12da96f | 433 | * @disc: line discipline number |
01e1abb2 AC |
434 | * |
435 | * This is probably overkill for real world processors but | |
436 | * they are not on hot paths so a little discipline won't do | |
437 | * any harm. | |
438 | * | |
dd42bf11 PH |
439 | * The line discipline-related tty_struct fields are reset to |
440 | * prevent the ldisc driver from re-using stale information for | |
441 | * the new ldisc instance. | |
442 | * | |
6a1c0680 | 443 | * Locking: takes termios_rwsem |
01e1abb2 AC |
444 | */ |
445 | ||
c12da96f | 446 | static void tty_set_termios_ldisc(struct tty_struct *tty, int disc) |
01e1abb2 | 447 | { |
6a1c0680 | 448 | down_write(&tty->termios_rwsem); |
c12da96f | 449 | tty->termios.c_line = disc; |
6a1c0680 | 450 | up_write(&tty->termios_rwsem); |
dd42bf11 PH |
451 | |
452 | tty->disc_data = NULL; | |
453 | tty->receive_room = 0; | |
01e1abb2 AC |
454 | } |
455 | ||
c65c9bc3 AC |
456 | /** |
457 | * tty_ldisc_open - open a line discipline | |
458 | * @tty: tty we are opening the ldisc on | |
459 | * @ld: discipline to open | |
460 | * | |
461 | * A helper opening method. Also a convenient debugging and check | |
462 | * point. | |
ec79d605 AB |
463 | * |
464 | * Locking: always called with BTM already held. | |
c65c9bc3 AC |
465 | */ |
466 | ||
467 | static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld) | |
468 | { | |
469 | WARN_ON(test_and_set_bit(TTY_LDISC_OPEN, &tty->flags)); | |
f18f9498 AC |
470 | if (ld->ops->open) { |
471 | int ret; | |
ec79d605 | 472 | /* BTM here locks versus a hangup event */ |
f18f9498 | 473 | ret = ld->ops->open(tty); |
7f90cfc5 JS |
474 | if (ret) |
475 | clear_bit(TTY_LDISC_OPEN, &tty->flags); | |
fb6edc91 | 476 | |
a570a49a | 477 | tty_ldisc_debug(tty, "%p: opened\n", ld); |
f18f9498 AC |
478 | return ret; |
479 | } | |
c65c9bc3 AC |
480 | return 0; |
481 | } | |
482 | ||
483 | /** | |
484 | * tty_ldisc_close - close a line discipline | |
485 | * @tty: tty we are opening the ldisc on | |
486 | * @ld: discipline to close | |
487 | * | |
488 | * A helper close method. Also a convenient debugging and check | |
489 | * point. | |
490 | */ | |
491 | ||
492 | static void tty_ldisc_close(struct tty_struct *tty, struct tty_ldisc *ld) | |
493 | { | |
494 | WARN_ON(!test_bit(TTY_LDISC_OPEN, &tty->flags)); | |
495 | clear_bit(TTY_LDISC_OPEN, &tty->flags); | |
496 | if (ld->ops->close) | |
497 | ld->ops->close(tty); | |
a570a49a | 498 | tty_ldisc_debug(tty, "%p: closed\n", ld); |
c65c9bc3 | 499 | } |
01e1abb2 | 500 | |
8a8dabf2 AC |
501 | /** |
502 | * tty_ldisc_failto - helper for ldisc failback | |
503 | * @tty: tty to open the ldisc on | |
504 | * @ld: ldisc we are trying to fail back to | |
505 | * | |
506 | * Helper to try and recover a tty when switching back to the old | |
507 | * ldisc fails and we need something attached. | |
508 | */ | |
509 | ||
510 | static int tty_ldisc_failto(struct tty_struct *tty, int ld) | |
511 | { | |
512 | struct tty_ldisc *disc = tty_ldisc_get(tty, ld); | |
513 | int r; | |
514 | ||
515 | if (IS_ERR(disc)) | |
516 | return PTR_ERR(disc); | |
517 | tty->ldisc = disc; | |
518 | tty_set_termios_ldisc(tty, ld); | |
519 | if ((r = tty_ldisc_open(tty, disc)) < 0) | |
520 | tty_ldisc_put(disc); | |
521 | return r; | |
522 | } | |
523 | ||
a8983d01 GKH |
524 | /** |
525 | * tty_ldisc_restore - helper for tty ldisc change | |
526 | * @tty: tty to recover | |
527 | * @old: previous ldisc | |
528 | * | |
529 | * Restore the previous line discipline or N_TTY when a line discipline | |
530 | * change fails due to an open error | |
531 | */ | |
532 | ||
533 | static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old) | |
534 | { | |
a8983d01 | 535 | /* There is an outstanding reference here so this is safe */ |
809bc83f TH |
536 | if (tty_ldisc_failto(tty, old->ops->num) < 0) { |
537 | const char *name = tty_name(tty); | |
538 | ||
539 | pr_warn("Falling back ldisc for %s.\n", name); | |
8a8dabf2 AC |
540 | /* The traditional behaviour is to fall back to N_TTY, we |
541 | want to avoid falling back to N_NULL unless we have no | |
542 | choice to avoid the risk of breaking anything */ | |
543 | if (tty_ldisc_failto(tty, N_TTY) < 0 && | |
544 | tty_ldisc_failto(tty, N_NULL) < 0) | |
809bc83f | 545 | panic("Couldn't open N_NULL ldisc for %s.", name); |
a8983d01 GKH |
546 | } |
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 AC |
558 | */ |
559 | ||
c12da96f | 560 | int tty_set_ldisc(struct tty_struct *tty, int disc) |
01e1abb2 | 561 | { |
a8983d01 GKH |
562 | int retval; |
563 | struct tty_ldisc *old_ldisc, *new_ldisc; | |
564 | ||
565 | new_ldisc = tty_ldisc_get(tty, disc); | |
566 | if (IS_ERR(new_ldisc)) | |
567 | return PTR_ERR(new_ldisc); | |
01e1abb2 | 568 | |
c8483bc9 | 569 | tty_lock(tty); |
276a661a | 570 | retval = tty_ldisc_lock(tty, 5 * HZ); |
63d8cb3f PH |
571 | if (retval) |
572 | goto err; | |
01e1abb2 | 573 | |
a570a49a PH |
574 | if (!tty->ldisc) { |
575 | retval = -EIO; | |
576 | goto out; | |
577 | } | |
578 | ||
63d8cb3f | 579 | /* Check the no-op case */ |
a8983d01 | 580 | if (tty->ldisc->ops->num == disc) |
63d8cb3f | 581 | goto out; |
c65c9bc3 | 582 | |
63d8cb3f PH |
583 | if (test_bit(TTY_HUPPED, &tty->flags)) { |
584 | /* We were raced by hangup */ | |
585 | retval = -EIO; | |
586 | goto out; | |
01e1abb2 AC |
587 | } |
588 | ||
a8983d01 GKH |
589 | old_ldisc = tty->ldisc; |
590 | ||
591 | /* Shutdown the old discipline. */ | |
592 | tty_ldisc_close(tty, old_ldisc); | |
593 | ||
594 | /* Now set up the new line discipline. */ | |
595 | tty->ldisc = new_ldisc; | |
596 | tty_set_termios_ldisc(tty, disc); | |
597 | ||
598 | retval = tty_ldisc_open(tty, new_ldisc); | |
01e1abb2 | 599 | if (retval < 0) { |
c65c9bc3 | 600 | /* Back to the old one or N_TTY if we can't */ |
a8983d01 GKH |
601 | tty_ldisc_put(new_ldisc); |
602 | tty_ldisc_restore(tty, old_ldisc); | |
01e1abb2 | 603 | } |
c65c9bc3 | 604 | |
a8983d01 | 605 | if (tty->ldisc->ops->num != old_ldisc->ops->num && tty->ops->set_ldisc) { |
9191aaaa | 606 | down_read(&tty->termios_rwsem); |
01e1abb2 | 607 | tty->ops->set_ldisc(tty); |
9191aaaa PH |
608 | up_read(&tty->termios_rwsem); |
609 | } | |
01e1abb2 | 610 | |
a8983d01 GKH |
611 | /* At this point we hold a reference to the new ldisc and a |
612 | reference to the old ldisc, or we hold two references to | |
613 | the old ldisc (if it was restored as part of error cleanup | |
614 | above). In either case, releasing a single reference from | |
615 | the old ldisc is correct. */ | |
616 | new_ldisc = old_ldisc; | |
63d8cb3f | 617 | out: |
276a661a | 618 | tty_ldisc_unlock(tty); |
01e1abb2 | 619 | |
c65c9bc3 | 620 | /* Restart the work queue in case no characters kick it off. Safe if |
01e1abb2 | 621 | already running */ |
17a69219 | 622 | tty_buffer_restart_work(tty->port); |
63d8cb3f | 623 | err: |
a8983d01 | 624 | tty_ldisc_put(new_ldisc); /* drop the extra reference */ |
89c8d91e | 625 | tty_unlock(tty); |
01e1abb2 AC |
626 | return retval; |
627 | } | |
1ab92da3 | 628 | EXPORT_SYMBOL_GPL(tty_set_ldisc); |
01e1abb2 | 629 | |
6ffeb4b2 PH |
630 | /** |
631 | * tty_ldisc_kill - teardown ldisc | |
632 | * @tty: tty being released | |
633 | * | |
634 | * Perform final close of the ldisc and reset tty->ldisc | |
635 | */ | |
636 | static void tty_ldisc_kill(struct tty_struct *tty) | |
637 | { | |
638 | if (!tty->ldisc) | |
639 | return; | |
640 | /* | |
641 | * Now kill off the ldisc | |
642 | */ | |
643 | tty_ldisc_close(tty, tty->ldisc); | |
644 | tty_ldisc_put(tty->ldisc); | |
645 | /* Force an oops if we mess this up */ | |
646 | tty->ldisc = NULL; | |
647 | } | |
648 | ||
c65c9bc3 AC |
649 | /** |
650 | * tty_reset_termios - reset terminal state | |
651 | * @tty: tty to reset | |
652 | * | |
653 | * Restore a terminal to the driver default state. | |
654 | */ | |
655 | ||
656 | static void tty_reset_termios(struct tty_struct *tty) | |
657 | { | |
6a1c0680 | 658 | down_write(&tty->termios_rwsem); |
adc8d746 AC |
659 | tty->termios = tty->driver->init_termios; |
660 | tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios); | |
661 | tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios); | |
6a1c0680 | 662 | up_write(&tty->termios_rwsem); |
c65c9bc3 AC |
663 | } |
664 | ||
665 | ||
666 | /** | |
667 | * tty_ldisc_reinit - reinitialise the tty ldisc | |
668 | * @tty: tty to reinit | |
c12da96f | 669 | * @disc: line discipline to reinitialize |
c65c9bc3 | 670 | * |
7896f30d | 671 | * Completely reinitialize the line discipline state, by closing the |
892d1fa7 PH |
672 | * current instance, if there is one, and opening a new instance. If |
673 | * an error occurs opening the new non-N_TTY instance, the instance | |
674 | * is dropped and tty->ldisc reset to NULL. The caller can then retry | |
675 | * with N_TTY instead. | |
7896f30d PH |
676 | * |
677 | * Returns 0 if successful, otherwise error code < 0 | |
c65c9bc3 AC |
678 | */ |
679 | ||
892d1fa7 | 680 | int tty_ldisc_reinit(struct tty_struct *tty, int disc) |
c65c9bc3 | 681 | { |
7896f30d PH |
682 | struct tty_ldisc *ld; |
683 | int retval; | |
1c95ba1e | 684 | |
7896f30d | 685 | ld = tty_ldisc_get(tty, disc); |
a8983d01 GKH |
686 | if (IS_ERR(ld)) { |
687 | BUG_ON(disc == N_TTY); | |
7896f30d | 688 | return PTR_ERR(ld); |
a8983d01 | 689 | } |
c65c9bc3 | 690 | |
7896f30d PH |
691 | if (tty->ldisc) { |
692 | tty_ldisc_close(tty, tty->ldisc); | |
693 | tty_ldisc_put(tty->ldisc); | |
694 | } | |
695 | ||
696 | /* switch the line discipline */ | |
f4807045 | 697 | tty->ldisc = ld; |
c12da96f | 698 | tty_set_termios_ldisc(tty, disc); |
7896f30d PH |
699 | retval = tty_ldisc_open(tty, tty->ldisc); |
700 | if (retval) { | |
e65c62b1 JW |
701 | tty_ldisc_put(tty->ldisc); |
702 | tty->ldisc = NULL; | |
7896f30d PH |
703 | } |
704 | return retval; | |
c65c9bc3 AC |
705 | } |
706 | ||
707 | /** | |
708 | * tty_ldisc_hangup - hangup ldisc reset | |
709 | * @tty: tty being hung up | |
710 | * | |
711 | * Some tty devices reset their termios when they receive a hangup | |
712 | * event. In that situation we must also switch back to N_TTY properly | |
713 | * before we reset the termios data. | |
714 | * | |
715 | * Locking: We can take the ldisc mutex as the rest of the code is | |
716 | * careful to allow for this. | |
717 | * | |
718 | * In the pty pair case this occurs in the close() path of the | |
719 | * tty itself so we must be careful about locking rules. | |
720 | */ | |
721 | ||
892d1fa7 | 722 | void tty_ldisc_hangup(struct tty_struct *tty, bool reinit) |
c65c9bc3 AC |
723 | { |
724 | struct tty_ldisc *ld; | |
725 | ||
a570a49a | 726 | tty_ldisc_debug(tty, "%p: hangup\n", tty->ldisc); |
fc575ee6 | 727 | |
c65c9bc3 AC |
728 | ld = tty_ldisc_ref(tty); |
729 | if (ld != NULL) { | |
c65c9bc3 AC |
730 | if (ld->ops->flush_buffer) |
731 | ld->ops->flush_buffer(tty); | |
732 | tty_driver_flush_buffer(tty); | |
733 | if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) && | |
734 | ld->ops->write_wakeup) | |
735 | ld->ops->write_wakeup(tty); | |
736 | if (ld->ops->hangup) | |
737 | ld->ops->hangup(tty); | |
738 | tty_ldisc_deref(ld); | |
739 | } | |
36697529 | 740 | |
c65c9bc3 AC |
741 | wake_up_interruptible_poll(&tty->write_wait, POLLOUT); |
742 | wake_up_interruptible_poll(&tty->read_wait, POLLIN); | |
36697529 | 743 | |
c65c9bc3 AC |
744 | /* |
745 | * Shutdown the current line discipline, and reset it to | |
638b9648 AC |
746 | * N_TTY if need be. |
747 | * | |
748 | * Avoid racing set_ldisc or tty_ldisc_release | |
c65c9bc3 | 749 | */ |
fae76e9a | 750 | tty_ldisc_lock(tty, MAX_SCHEDULE_TIMEOUT); |
60af22d2 | 751 | |
892d1fa7 PH |
752 | if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) |
753 | tty_reset_termios(tty); | |
c8785241 | 754 | |
892d1fa7 PH |
755 | if (tty->ldisc) { |
756 | if (reinit) { | |
e65c62b1 JW |
757 | if (tty_ldisc_reinit(tty, tty->termios.c_line) < 0 && |
758 | tty_ldisc_reinit(tty, N_TTY) < 0) | |
759 | WARN_ON(tty_ldisc_reinit(tty, N_NULL) < 0); | |
892d1fa7 PH |
760 | } else |
761 | tty_ldisc_kill(tty); | |
c65c9bc3 | 762 | } |
fae76e9a | 763 | tty_ldisc_unlock(tty); |
c65c9bc3 | 764 | } |
01e1abb2 AC |
765 | |
766 | /** | |
767 | * tty_ldisc_setup - open line discipline | |
768 | * @tty: tty being shut down | |
769 | * @o_tty: pair tty for pty/tty pairs | |
770 | * | |
771 | * Called during the initial open of a tty/pty pair in order to set up the | |
c65c9bc3 AC |
772 | * line disciplines and bind them to the tty. This has no locking issues |
773 | * as the device isn't yet active. | |
01e1abb2 AC |
774 | */ |
775 | ||
776 | int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty) | |
777 | { | |
9de2a7ce | 778 | int retval = tty_ldisc_open(tty, tty->ldisc); |
c65c9bc3 AC |
779 | if (retval) |
780 | return retval; | |
781 | ||
782 | if (o_tty) { | |
783 | retval = tty_ldisc_open(o_tty, o_tty->ldisc); | |
01e1abb2 | 784 | if (retval) { |
9de2a7ce | 785 | tty_ldisc_close(tty, tty->ldisc); |
01e1abb2 AC |
786 | return retval; |
787 | } | |
01e1abb2 | 788 | } |
01e1abb2 AC |
789 | return 0; |
790 | } | |
89c8d91e | 791 | |
01e1abb2 AC |
792 | /** |
793 | * tty_ldisc_release - release line discipline | |
62462aef | 794 | * @tty: tty being shut down (or one end of pty pair) |
3ee175d9 | 795 | * |
62462aef | 796 | * Called during the final close of a tty or a pty pair in order to shut |
5b6e6832 | 797 | * down the line discpline layer. On exit, each tty's ldisc is NULL. |
01e1abb2 AC |
798 | */ |
799 | ||
62462aef | 800 | void tty_ldisc_release(struct tty_struct *tty) |
01e1abb2 | 801 | { |
62462aef PH |
802 | struct tty_struct *o_tty = tty->link; |
803 | ||
01e1abb2 | 804 | /* |
a2965b7b PH |
805 | * Shutdown this line discipline. As this is the final close, |
806 | * it does not race with the set_ldisc code path. | |
01e1abb2 | 807 | */ |
01e1abb2 | 808 | |
36697529 | 809 | tty_ldisc_lock_pair(tty, o_tty); |
89c8d91e | 810 | tty_ldisc_kill(tty); |
c65c9bc3 | 811 | if (o_tty) |
89c8d91e | 812 | tty_ldisc_kill(o_tty); |
36697529 PH |
813 | tty_ldisc_unlock_pair(tty, o_tty); |
814 | ||
aef29bc2 AC |
815 | /* And the memory resources remaining (buffers, termios) will be |
816 | disposed of when the kref hits zero */ | |
fc575ee6 | 817 | |
fb6edc91 | 818 | tty_ldisc_debug(tty, "released\n"); |
01e1abb2 | 819 | } |
1ab92da3 | 820 | EXPORT_SYMBOL_GPL(tty_ldisc_release); |
01e1abb2 AC |
821 | |
822 | /** | |
823 | * tty_ldisc_init - ldisc setup for new tty | |
824 | * @tty: tty being allocated | |
825 | * | |
826 | * Set up the line discipline objects for a newly allocated tty. Note that | |
827 | * the tty structure is not completely set up when this call is made. | |
828 | */ | |
829 | ||
f46dad0d | 830 | int tty_ldisc_init(struct tty_struct *tty) |
01e1abb2 | 831 | { |
36697529 | 832 | struct tty_ldisc *ld = tty_ldisc_get(tty, N_TTY); |
c65c9bc3 | 833 | if (IS_ERR(ld)) |
f46dad0d | 834 | return PTR_ERR(ld); |
f4807045 | 835 | tty->ldisc = ld; |
f46dad0d | 836 | return 0; |
01e1abb2 AC |
837 | } |
838 | ||
6716671d | 839 | /** |
c8b710b3 | 840 | * tty_ldisc_deinit - ldisc cleanup for new tty |
6716671d JS |
841 | * @tty: tty that was allocated recently |
842 | * | |
843 | * The tty structure must not becompletely set up (tty_ldisc_setup) when | |
844 | * this call is made. | |
845 | */ | |
846 | void tty_ldisc_deinit(struct tty_struct *tty) | |
847 | { | |
c8b710b3 PH |
848 | if (tty->ldisc) |
849 | tty_ldisc_put(tty->ldisc); | |
f4807045 | 850 | tty->ldisc = NULL; |
6716671d | 851 | } |