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 | |
118 | * @ld: tty ldisc structure to complete | |
119 | * | |
120 | * Attempt to open and lock a line discipline into place. Return | |
121 | * the line discipline refcounted and assigned in ld. On an error | |
122 | * report the error code back | |
123 | */ | |
124 | ||
125 | static int tty_ldisc_try_get(int disc, struct tty_ldisc *ld) | |
126 | { | |
127 | unsigned long flags; | |
128 | struct tty_ldisc_ops *ldops; | |
129 | int err = -EINVAL; | |
130 | ||
131 | spin_lock_irqsave(&tty_ldisc_lock, flags); | |
132 | ld->ops = NULL; | |
133 | ldops = tty_ldiscs[disc]; | |
134 | /* Check the entry is defined */ | |
135 | if (ldops) { | |
136 | /* If the module is being unloaded we can't use it */ | |
137 | if (!try_module_get(ldops->owner)) | |
138 | err = -EAGAIN; | |
139 | else { | |
140 | /* lock it */ | |
141 | ldops->refcount++; | |
142 | ld->ops = ldops; | |
143 | err = 0; | |
144 | } | |
145 | } | |
146 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); | |
147 | return err; | |
148 | } | |
149 | ||
150 | /** | |
151 | * tty_ldisc_get - take a reference to an ldisc | |
152 | * @disc: ldisc number | |
153 | * @ld: tty line discipline structure to use | |
154 | * | |
155 | * Takes a reference to a line discipline. Deals with refcounts and | |
156 | * module locking counts. Returns NULL if the discipline is not available. | |
157 | * Returns a pointer to the discipline and bumps the ref count if it is | |
158 | * available | |
159 | * | |
160 | * Locking: | |
161 | * takes tty_ldisc_lock to guard against ldisc races | |
162 | */ | |
163 | ||
164 | static int tty_ldisc_get(int disc, struct tty_ldisc *ld) | |
165 | { | |
166 | int err; | |
167 | ||
168 | if (disc < N_TTY || disc >= NR_LDISCS) | |
169 | return -EINVAL; | |
170 | err = tty_ldisc_try_get(disc, ld); | |
3ab36ab6 | 171 | if (err < 0) { |
01e1abb2 AC |
172 | request_module("tty-ldisc-%d", disc); |
173 | err = tty_ldisc_try_get(disc, ld); | |
174 | } | |
175 | return err; | |
176 | } | |
177 | ||
178 | /** | |
179 | * tty_ldisc_put - drop ldisc reference | |
180 | * @disc: ldisc number | |
181 | * | |
182 | * Drop a reference to a line discipline. Manage refcounts and | |
183 | * module usage counts | |
184 | * | |
185 | * Locking: | |
186 | * takes tty_ldisc_lock to guard against ldisc races | |
187 | */ | |
188 | ||
189 | static void tty_ldisc_put(struct tty_ldisc_ops *ld) | |
190 | { | |
191 | unsigned long flags; | |
192 | int disc = ld->num; | |
193 | ||
194 | BUG_ON(disc < N_TTY || disc >= NR_LDISCS); | |
195 | ||
196 | spin_lock_irqsave(&tty_ldisc_lock, flags); | |
197 | ld = tty_ldiscs[disc]; | |
198 | BUG_ON(ld->refcount == 0); | |
199 | ld->refcount--; | |
200 | module_put(ld->owner); | |
201 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); | |
202 | } | |
203 | ||
204 | static void * tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos) | |
205 | { | |
206 | return (*pos < NR_LDISCS) ? pos : NULL; | |
207 | } | |
208 | ||
209 | static void * tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos) | |
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; | |
222 | struct tty_ldisc ld; | |
223 | ||
224 | if (tty_ldisc_get(i, &ld) < 0) | |
225 | return 0; | |
226 | seq_printf(m, "%-10s %2d\n", ld.ops->name ? ld.ops->name : "???", i); | |
227 | tty_ldisc_put(ld.ops); | |
228 | return 0; | |
229 | } | |
230 | ||
231 | static const struct seq_operations tty_ldiscs_seq_ops = { | |
232 | .start = tty_ldiscs_seq_start, | |
233 | .next = tty_ldiscs_seq_next, | |
234 | .stop = tty_ldiscs_seq_stop, | |
235 | .show = tty_ldiscs_seq_show, | |
236 | }; | |
237 | ||
238 | static int proc_tty_ldiscs_open(struct inode *inode, struct file *file) | |
239 | { | |
240 | return seq_open(file, &tty_ldiscs_seq_ops); | |
241 | } | |
242 | ||
243 | const struct file_operations tty_ldiscs_proc_fops = { | |
244 | .owner = THIS_MODULE, | |
245 | .open = proc_tty_ldiscs_open, | |
246 | .read = seq_read, | |
247 | .llseek = seq_lseek, | |
248 | .release = seq_release, | |
249 | }; | |
250 | ||
251 | /** | |
252 | * tty_ldisc_assign - set ldisc on a tty | |
253 | * @tty: tty to assign | |
254 | * @ld: line discipline | |
255 | * | |
256 | * Install an instance of a line discipline into a tty structure. The | |
257 | * ldisc must have a reference count above zero to ensure it remains/ | |
258 | * The tty instance refcount starts at zero. | |
259 | * | |
260 | * Locking: | |
261 | * Caller must hold references | |
262 | */ | |
263 | ||
264 | static void tty_ldisc_assign(struct tty_struct *tty, struct tty_ldisc *ld) | |
265 | { | |
266 | ld->refcount = 0; | |
267 | tty->ldisc = *ld; | |
268 | } | |
269 | ||
270 | /** | |
271 | * tty_ldisc_try - internal helper | |
272 | * @tty: the tty | |
273 | * | |
274 | * Make a single attempt to grab and bump the refcount on | |
275 | * the tty ldisc. Return 0 on failure or 1 on success. This is | |
276 | * used to implement both the waiting and non waiting versions | |
277 | * of tty_ldisc_ref | |
278 | * | |
279 | * Locking: takes tty_ldisc_lock | |
280 | */ | |
281 | ||
282 | static int tty_ldisc_try(struct tty_struct *tty) | |
283 | { | |
284 | unsigned long flags; | |
285 | struct tty_ldisc *ld; | |
286 | int ret = 0; | |
287 | ||
288 | spin_lock_irqsave(&tty_ldisc_lock, flags); | |
289 | ld = &tty->ldisc; | |
290 | if (test_bit(TTY_LDISC, &tty->flags)) { | |
291 | ld->refcount++; | |
292 | ret = 1; | |
293 | } | |
294 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); | |
295 | return ret; | |
296 | } | |
297 | ||
298 | /** | |
299 | * tty_ldisc_ref_wait - wait for the tty ldisc | |
300 | * @tty: tty device | |
301 | * | |
302 | * Dereference the line discipline for the terminal and take a | |
303 | * reference to it. If the line discipline is in flux then | |
304 | * wait patiently until it changes. | |
305 | * | |
306 | * Note: Must not be called from an IRQ/timer context. The caller | |
307 | * must also be careful not to hold other locks that will deadlock | |
308 | * against a discipline change, such as an existing ldisc reference | |
309 | * (which we check for) | |
310 | * | |
311 | * Locking: call functions take tty_ldisc_lock | |
312 | */ | |
313 | ||
314 | struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty) | |
315 | { | |
316 | /* wait_event is a macro */ | |
317 | wait_event(tty_ldisc_wait, tty_ldisc_try(tty)); | |
c9b3976e | 318 | WARN_ON(tty->ldisc.refcount == 0); |
01e1abb2 AC |
319 | return &tty->ldisc; |
320 | } | |
321 | ||
322 | EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait); | |
323 | ||
324 | /** | |
325 | * tty_ldisc_ref - get the tty ldisc | |
326 | * @tty: tty device | |
327 | * | |
328 | * Dereference the line discipline for the terminal and take a | |
329 | * reference to it. If the line discipline is in flux then | |
330 | * return NULL. Can be called from IRQ and timer functions. | |
331 | * | |
332 | * Locking: called functions take tty_ldisc_lock | |
333 | */ | |
334 | ||
335 | struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty) | |
336 | { | |
337 | if (tty_ldisc_try(tty)) | |
338 | return &tty->ldisc; | |
339 | return NULL; | |
340 | } | |
341 | ||
342 | EXPORT_SYMBOL_GPL(tty_ldisc_ref); | |
343 | ||
344 | /** | |
345 | * tty_ldisc_deref - free a tty ldisc reference | |
346 | * @ld: reference to free up | |
347 | * | |
348 | * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May | |
349 | * be called in IRQ context. | |
350 | * | |
351 | * Locking: takes tty_ldisc_lock | |
352 | */ | |
353 | ||
354 | void tty_ldisc_deref(struct tty_ldisc *ld) | |
355 | { | |
356 | unsigned long flags; | |
357 | ||
358 | BUG_ON(ld == NULL); | |
359 | ||
360 | spin_lock_irqsave(&tty_ldisc_lock, flags); | |
361 | if (ld->refcount == 0) | |
362 | printk(KERN_ERR "tty_ldisc_deref: no references.\n"); | |
363 | else | |
364 | ld->refcount--; | |
365 | if (ld->refcount == 0) | |
366 | wake_up(&tty_ldisc_wait); | |
367 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); | |
368 | } | |
369 | ||
370 | EXPORT_SYMBOL_GPL(tty_ldisc_deref); | |
371 | ||
372 | /** | |
373 | * tty_ldisc_enable - allow ldisc use | |
374 | * @tty: terminal to activate ldisc on | |
375 | * | |
376 | * Set the TTY_LDISC flag when the line discipline can be called | |
c9b3976e AC |
377 | * again. Do necessary wakeups for existing sleepers. Clear the LDISC |
378 | * changing flag to indicate any ldisc change is now over. | |
01e1abb2 | 379 | * |
c9b3976e AC |
380 | * Note: nobody should set the TTY_LDISC bit except via this function. |
381 | * Clearing directly is allowed. | |
01e1abb2 AC |
382 | */ |
383 | ||
384 | void tty_ldisc_enable(struct tty_struct *tty) | |
385 | { | |
386 | set_bit(TTY_LDISC, &tty->flags); | |
c9b3976e | 387 | clear_bit(TTY_LDISC_CHANGING, &tty->flags); |
01e1abb2 AC |
388 | wake_up(&tty_ldisc_wait); |
389 | } | |
390 | ||
391 | /** | |
392 | * tty_set_termios_ldisc - set ldisc field | |
393 | * @tty: tty structure | |
394 | * @num: line discipline number | |
395 | * | |
396 | * This is probably overkill for real world processors but | |
397 | * they are not on hot paths so a little discipline won't do | |
398 | * any harm. | |
399 | * | |
400 | * Locking: takes termios_mutex | |
401 | */ | |
402 | ||
403 | static void tty_set_termios_ldisc(struct tty_struct *tty, int num) | |
404 | { | |
405 | mutex_lock(&tty->termios_mutex); | |
406 | tty->termios->c_line = num; | |
407 | mutex_unlock(&tty->termios_mutex); | |
408 | } | |
409 | ||
410 | ||
411 | /** | |
412 | * tty_ldisc_restore - helper for tty ldisc change | |
413 | * @tty: tty to recover | |
414 | * @old: previous ldisc | |
415 | * | |
416 | * Restore the previous line discipline or N_TTY when a line discipline | |
417 | * change fails due to an open error | |
418 | */ | |
419 | ||
420 | static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old) | |
421 | { | |
422 | char buf[64]; | |
423 | struct tty_ldisc new_ldisc; | |
424 | ||
425 | /* There is an outstanding reference here so this is safe */ | |
426 | tty_ldisc_get(old->ops->num, old); | |
427 | tty_ldisc_assign(tty, old); | |
428 | tty_set_termios_ldisc(tty, old->ops->num); | |
429 | if (old->ops->open && (old->ops->open(tty) < 0)) { | |
430 | tty_ldisc_put(old->ops); | |
431 | /* This driver is always present */ | |
432 | if (tty_ldisc_get(N_TTY, &new_ldisc) < 0) | |
433 | panic("n_tty: get"); | |
434 | tty_ldisc_assign(tty, &new_ldisc); | |
435 | tty_set_termios_ldisc(tty, N_TTY); | |
436 | if (new_ldisc.ops->open) { | |
437 | int r = new_ldisc.ops->open(tty); | |
438 | if (r < 0) | |
439 | panic("Couldn't open N_TTY ldisc for " | |
440 | "%s --- error %d.", | |
441 | tty_name(tty, buf), r); | |
442 | } | |
443 | } | |
444 | } | |
445 | ||
e8b70e7d AC |
446 | /** |
447 | * tty_ldisc_halt - shutdown the line discipline | |
448 | * @tty: tty device | |
449 | * | |
450 | * Shut down the line discipline and work queue for this tty device. | |
451 | * The TTY_LDISC flag being cleared ensures no further references can | |
452 | * be obtained while the delayed work queue halt ensures that no more | |
453 | * data is fed to the ldisc. | |
454 | * | |
455 | * In order to wait for any existing references to complete see | |
456 | * tty_ldisc_wait_idle. | |
457 | */ | |
458 | ||
459 | static void tty_ldisc_halt(struct tty_struct *tty) | |
460 | { | |
461 | clear_bit(TTY_LDISC, &tty->flags); | |
462 | cancel_delayed_work(&tty->buf.work); | |
463 | /* | |
464 | * Wait for ->hangup_work and ->buf.work handlers to terminate | |
465 | */ | |
466 | flush_scheduled_work(); | |
467 | } | |
468 | ||
469 | /** | |
470 | * tty_ldisc_wait_idle - wait for the ldisc to become idle | |
471 | * @tty: tty to wait for | |
472 | * | |
473 | * Wait for the line discipline to become idle. The discipline must | |
474 | * have been halted for this to guarantee it remains idle. | |
475 | * | |
476 | */ | |
477 | ||
478 | static void tty_ldisc_wait_idle(struct tty_struct *tty) | |
479 | { | |
480 | unsigned long flags; | |
481 | spin_lock_irqsave(&tty_ldisc_lock, flags); | |
482 | while (tty->ldisc.refcount) { | |
483 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); | |
484 | wait_event(tty_ldisc_wait, tty->ldisc.refcount == 0); | |
485 | spin_lock_irqsave(&tty_ldisc_lock, flags); | |
486 | } | |
487 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); | |
488 | } | |
489 | ||
01e1abb2 AC |
490 | /** |
491 | * tty_set_ldisc - set line discipline | |
492 | * @tty: the terminal to set | |
493 | * @ldisc: the line discipline | |
494 | * | |
495 | * Set the discipline of a tty line. Must be called from a process | |
496 | * context. | |
497 | * | |
498 | * Locking: takes tty_ldisc_lock. | |
499 | * called functions take termios_mutex | |
500 | */ | |
501 | ||
502 | int tty_set_ldisc(struct tty_struct *tty, int ldisc) | |
503 | { | |
504 | int retval; | |
505 | struct tty_ldisc o_ldisc, new_ldisc; | |
506 | int work; | |
507 | unsigned long flags; | |
508 | struct tty_struct *o_tty; | |
509 | ||
510 | restart: | |
511 | /* This is a bit ugly for now but means we can break the 'ldisc | |
512 | is part of the tty struct' assumption later */ | |
513 | retval = tty_ldisc_get(ldisc, &new_ldisc); | |
514 | if (retval) | |
515 | return retval; | |
516 | ||
517 | /* | |
518 | * Problem: What do we do if this blocks ? | |
519 | */ | |
520 | ||
521 | tty_wait_until_sent(tty, 0); | |
522 | ||
523 | if (tty->ldisc.ops->num == ldisc) { | |
524 | tty_ldisc_put(new_ldisc.ops); | |
525 | return 0; | |
526 | } | |
527 | ||
528 | /* | |
529 | * No more input please, we are switching. The new ldisc | |
530 | * will update this value in the ldisc open function | |
531 | */ | |
532 | ||
533 | tty->receive_room = 0; | |
534 | ||
535 | o_ldisc = tty->ldisc; | |
536 | o_tty = tty->link; | |
537 | ||
538 | /* | |
539 | * Make sure we don't change while someone holds a | |
540 | * reference to the line discipline. The TTY_LDISC bit | |
541 | * prevents anyone taking a reference once it is clear. | |
542 | * We need the lock to avoid racing reference takers. | |
c9b3976e AC |
543 | * |
544 | * We must clear the TTY_LDISC bit here to avoid a livelock | |
545 | * with a userspace app continually trying to use the tty in | |
546 | * parallel to the change and re-referencing the tty. | |
01e1abb2 | 547 | */ |
c9b3976e AC |
548 | clear_bit(TTY_LDISC, &tty->flags); |
549 | if (o_tty) | |
550 | clear_bit(TTY_LDISC, &o_tty->flags); | |
01e1abb2 AC |
551 | |
552 | spin_lock_irqsave(&tty_ldisc_lock, flags); | |
553 | if (tty->ldisc.refcount || (o_tty && o_tty->ldisc.refcount)) { | |
554 | if (tty->ldisc.refcount) { | |
555 | /* Free the new ldisc we grabbed. Must drop the lock | |
556 | first. */ | |
557 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); | |
558 | tty_ldisc_put(o_ldisc.ops); | |
559 | /* | |
560 | * There are several reasons we may be busy, including | |
561 | * random momentary I/O traffic. We must therefore | |
562 | * retry. We could distinguish between blocking ops | |
563 | * and retries if we made tty_ldisc_wait() smarter. | |
564 | * That is up for discussion. | |
565 | */ | |
566 | if (wait_event_interruptible(tty_ldisc_wait, tty->ldisc.refcount == 0) < 0) | |
567 | return -ERESTARTSYS; | |
568 | goto restart; | |
569 | } | |
570 | if (o_tty && o_tty->ldisc.refcount) { | |
571 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); | |
572 | tty_ldisc_put(o_tty->ldisc.ops); | |
573 | if (wait_event_interruptible(tty_ldisc_wait, o_tty->ldisc.refcount == 0) < 0) | |
574 | return -ERESTARTSYS; | |
575 | goto restart; | |
576 | } | |
577 | } | |
578 | /* | |
579 | * If the TTY_LDISC bit is set, then we are racing against | |
580 | * another ldisc change | |
581 | */ | |
c9b3976e | 582 | if (test_bit(TTY_LDISC_CHANGING, &tty->flags)) { |
01e1abb2 AC |
583 | struct tty_ldisc *ld; |
584 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); | |
585 | tty_ldisc_put(new_ldisc.ops); | |
586 | ld = tty_ldisc_ref_wait(tty); | |
587 | tty_ldisc_deref(ld); | |
588 | goto restart; | |
589 | } | |
c9b3976e AC |
590 | /* |
591 | * This flag is used to avoid two parallel ldisc changes. Once | |
592 | * open and close are fine grained locked this may work better | |
593 | * as a mutex shared with the open/close/hup paths | |
594 | */ | |
595 | set_bit(TTY_LDISC_CHANGING, &tty->flags); | |
01e1abb2 | 596 | if (o_tty) |
c9b3976e | 597 | set_bit(TTY_LDISC_CHANGING, &o_tty->flags); |
01e1abb2 AC |
598 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); |
599 | ||
600 | /* | |
601 | * From this point on we know nobody has an ldisc | |
602 | * usage reference, nor can they obtain one until | |
603 | * we say so later on. | |
604 | */ | |
605 | ||
606 | work = cancel_delayed_work(&tty->buf.work); | |
607 | /* | |
608 | * Wait for ->hangup_work and ->buf.work handlers to terminate | |
609 | * MUST NOT hold locks here. | |
610 | */ | |
611 | flush_scheduled_work(); | |
612 | /* Shutdown the current discipline. */ | |
613 | if (o_ldisc.ops->close) | |
614 | (o_ldisc.ops->close)(tty); | |
615 | ||
616 | /* Now set up the new line discipline. */ | |
617 | tty_ldisc_assign(tty, &new_ldisc); | |
618 | tty_set_termios_ldisc(tty, ldisc); | |
619 | if (new_ldisc.ops->open) | |
620 | retval = (new_ldisc.ops->open)(tty); | |
621 | if (retval < 0) { | |
622 | tty_ldisc_put(new_ldisc.ops); | |
623 | tty_ldisc_restore(tty, &o_ldisc); | |
624 | } | |
625 | /* At this point we hold a reference to the new ldisc and a | |
626 | a reference to the old ldisc. If we ended up flipping back | |
627 | to the existing ldisc we have two references to it */ | |
628 | ||
629 | if (tty->ldisc.ops->num != o_ldisc.ops->num && tty->ops->set_ldisc) | |
630 | tty->ops->set_ldisc(tty); | |
631 | ||
632 | tty_ldisc_put(o_ldisc.ops); | |
633 | ||
634 | /* | |
635 | * Allow ldisc referencing to occur as soon as the driver | |
636 | * ldisc callback completes. | |
637 | */ | |
638 | ||
639 | tty_ldisc_enable(tty); | |
640 | if (o_tty) | |
641 | tty_ldisc_enable(o_tty); | |
642 | ||
643 | /* Restart it in case no characters kick it off. Safe if | |
644 | already running */ | |
645 | if (work) | |
646 | schedule_delayed_work(&tty->buf.work, 1); | |
647 | return retval; | |
648 | } | |
649 | ||
650 | ||
651 | /** | |
652 | * tty_ldisc_setup - open line discipline | |
653 | * @tty: tty being shut down | |
654 | * @o_tty: pair tty for pty/tty pairs | |
655 | * | |
656 | * Called during the initial open of a tty/pty pair in order to set up the | |
657 | * line discplines and bind them to the tty. | |
658 | */ | |
659 | ||
660 | int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty) | |
661 | { | |
662 | struct tty_ldisc *ld = &tty->ldisc; | |
663 | int retval; | |
664 | ||
665 | if (ld->ops->open) { | |
666 | retval = (ld->ops->open)(tty); | |
667 | if (retval) | |
668 | return retval; | |
669 | } | |
670 | if (o_tty && o_tty->ldisc.ops->open) { | |
671 | retval = (o_tty->ldisc.ops->open)(o_tty); | |
672 | if (retval) { | |
673 | if (ld->ops->close) | |
674 | (ld->ops->close)(tty); | |
675 | return retval; | |
676 | } | |
677 | tty_ldisc_enable(o_tty); | |
678 | } | |
679 | tty_ldisc_enable(tty); | |
680 | return 0; | |
681 | } | |
682 | ||
e8b70e7d AC |
683 | static void tty_ldisc_reinit(struct tty_struct *tty) |
684 | { | |
685 | struct tty_ldisc ld; | |
686 | ||
687 | if (tty->ldisc.ops->close) | |
688 | (tty->ldisc.ops->close)(tty); | |
689 | tty_ldisc_put(tty->ldisc.ops); | |
690 | /* | |
691 | * Switch the line discipline back | |
692 | */ | |
693 | WARN_ON(tty_ldisc_get(N_TTY, &ld)); | |
694 | tty_ldisc_assign(tty, &ld); | |
695 | tty_set_termios_ldisc(tty, N_TTY); | |
696 | } | |
697 | ||
01e1abb2 AC |
698 | /** |
699 | * tty_ldisc_release - release line discipline | |
700 | * @tty: tty being shut down | |
701 | * @o_tty: pair tty for pty/tty pairs | |
702 | * | |
703 | * Called during the final close of a tty/pty pair in order to shut down the | |
704 | * line discpline layer. | |
705 | */ | |
706 | ||
707 | void tty_ldisc_release(struct tty_struct *tty, struct tty_struct *o_tty) | |
708 | { | |
e8b70e7d | 709 | |
01e1abb2 AC |
710 | /* |
711 | * Prevent flush_to_ldisc() from rescheduling the work for later. Then | |
712 | * kill any delayed work. As this is the final close it does not | |
713 | * race with the set_ldisc code path. | |
714 | */ | |
01e1abb2 | 715 | |
e8b70e7d | 716 | tty_ldisc_halt(tty); |
01e1abb2 AC |
717 | |
718 | /* | |
719 | * Wait for any short term users (we know they are just driver | |
720 | * side waiters as the file is closing so user count on the file | |
721 | * side is zero. | |
722 | */ | |
e8b70e7d AC |
723 | |
724 | tty_ldisc_wait_idle(tty); | |
725 | ||
01e1abb2 AC |
726 | /* |
727 | * Shutdown the current line discipline, and reset it to N_TTY. | |
728 | * | |
729 | * FIXME: this MUST get fixed for the new reflocking | |
730 | */ | |
01e1abb2 | 731 | |
e8b70e7d | 732 | tty_ldisc_reinit(tty); |
01e1abb2 AC |
733 | if (o_tty) { |
734 | /* FIXME: could o_tty be in setldisc here ? */ | |
735 | clear_bit(TTY_LDISC, &o_tty->flags); | |
e8b70e7d | 736 | tty_ldisc_reinit(o_tty); |
01e1abb2 AC |
737 | } |
738 | } | |
739 | ||
740 | /** | |
741 | * tty_ldisc_init - ldisc setup for new tty | |
742 | * @tty: tty being allocated | |
743 | * | |
744 | * Set up the line discipline objects for a newly allocated tty. Note that | |
745 | * the tty structure is not completely set up when this call is made. | |
746 | */ | |
747 | ||
748 | void tty_ldisc_init(struct tty_struct *tty) | |
749 | { | |
750 | struct tty_ldisc ld; | |
751 | if (tty_ldisc_get(N_TTY, &ld) < 0) | |
752 | panic("n_tty: init_tty"); | |
753 | tty_ldisc_assign(tty, &ld); | |
754 | } | |
755 | ||
756 | void tty_ldisc_begin(void) | |
757 | { | |
758 | /* Setup the default TTY line discipline. */ | |
759 | (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY); | |
760 | } |