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