Merge git://git.kernel.org/pub/scm/linux/kernel/git/mason/btrfs-unstable
[linux-2.6/mini2440.git] / drivers / char / tty_ldisc.c
blobf78f5b0127a88501ec0b21cf9d42c517209ad933
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>
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>
31 #include <linux/uaccess.h>
32 #include <asm/system.h>
34 #include <linux/kbd_kern.h>
35 #include <linux/vt_kern.h>
36 #include <linux/selection.h>
38 #include <linux/kmod.h>
39 #include <linux/nsproxy.h>
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.
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];
52 /**
53 * tty_register_ldisc - install a line discipline
54 * @disc: ldisc number
55 * @new_ldisc: pointer to the ldisc object
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.
61 * Locking:
62 * takes tty_ldisc_lock to guard against ldisc races
65 int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc)
67 unsigned long flags;
68 int ret = 0;
70 if (disc < N_TTY || disc >= NR_LDISCS)
71 return -EINVAL;
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);
79 return ret;
81 EXPORT_SYMBOL(tty_register_ldisc);
83 /**
84 * tty_unregister_ldisc - unload a line discipline
85 * @disc: ldisc number
86 * @new_ldisc: pointer to the ldisc object
88 * Remove a line discipline from the kernel providing it is not
89 * currently in use.
91 * Locking:
92 * takes tty_ldisc_lock to guard against ldisc races
95 int tty_unregister_ldisc(int disc)
97 unsigned long flags;
98 int ret = 0;
100 if (disc < N_TTY || disc >= NR_LDISCS)
101 return -EINVAL;
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);
110 return ret;
112 EXPORT_SYMBOL(tty_unregister_ldisc);
116 * tty_ldisc_try_get - try and reference an ldisc
117 * @disc: ldisc number
118 * @ld: tty ldisc structure to complete
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
125 static int tty_ldisc_try_get(int disc, struct tty_ldisc *ld)
127 unsigned long flags;
128 struct tty_ldisc_ops *ldops;
129 int err = -EINVAL;
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;
146 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
147 return err;
151 * tty_ldisc_get - take a reference to an ldisc
152 * @disc: ldisc number
153 * @ld: tty line discipline structure to use
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
160 * Locking:
161 * takes tty_ldisc_lock to guard against ldisc races
164 static int tty_ldisc_get(int disc, struct tty_ldisc *ld)
166 int err;
168 if (disc < N_TTY || disc >= NR_LDISCS)
169 return -EINVAL;
170 err = tty_ldisc_try_get(disc, ld);
171 if (err < 0) {
172 request_module("tty-ldisc-%d", disc);
173 err = tty_ldisc_try_get(disc, ld);
175 return err;
179 * tty_ldisc_put - drop ldisc reference
180 * @disc: ldisc number
182 * Drop a reference to a line discipline. Manage refcounts and
183 * module usage counts
185 * Locking:
186 * takes tty_ldisc_lock to guard against ldisc races
189 static void tty_ldisc_put(struct tty_ldisc_ops *ld)
191 unsigned long flags;
192 int disc = ld->num;
194 BUG_ON(disc < N_TTY || disc >= NR_LDISCS);
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);
204 static void * tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos)
206 return (*pos < NR_LDISCS) ? pos : NULL;
209 static void * tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos)
211 (*pos)++;
212 return (*pos < NR_LDISCS) ? pos : NULL;
215 static void tty_ldiscs_seq_stop(struct seq_file *m, void *v)
219 static int tty_ldiscs_seq_show(struct seq_file *m, void *v)
221 int i = *(loff_t *)v;
222 struct tty_ldisc ld;
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;
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,
238 static int proc_tty_ldiscs_open(struct inode *inode, struct file *file)
240 return seq_open(file, &tty_ldiscs_seq_ops);
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,
252 * tty_ldisc_assign - set ldisc on a tty
253 * @tty: tty to assign
254 * @ld: line discipline
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.
260 * Locking:
261 * Caller must hold references
264 static void tty_ldisc_assign(struct tty_struct *tty, struct tty_ldisc *ld)
266 ld->refcount = 0;
267 tty->ldisc = *ld;
271 * tty_ldisc_try - internal helper
272 * @tty: the tty
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
279 * Locking: takes tty_ldisc_lock
282 static int tty_ldisc_try(struct tty_struct *tty)
284 unsigned long flags;
285 struct tty_ldisc *ld;
286 int ret = 0;
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;
294 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
295 return ret;
299 * tty_ldisc_ref_wait - wait for the tty ldisc
300 * @tty: tty device
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.
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)
311 * Locking: call functions take tty_ldisc_lock
314 struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
316 /* wait_event is a macro */
317 wait_event(tty_ldisc_wait, tty_ldisc_try(tty));
318 WARN_ON(tty->ldisc.refcount == 0);
319 return &tty->ldisc;
322 EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
325 * tty_ldisc_ref - get the tty ldisc
326 * @tty: tty device
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.
332 * Locking: called functions take tty_ldisc_lock
335 struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
337 if (tty_ldisc_try(tty))
338 return &tty->ldisc;
339 return NULL;
342 EXPORT_SYMBOL_GPL(tty_ldisc_ref);
345 * tty_ldisc_deref - free a tty ldisc reference
346 * @ld: reference to free up
348 * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May
349 * be called in IRQ context.
351 * Locking: takes tty_ldisc_lock
354 void tty_ldisc_deref(struct tty_ldisc *ld)
356 unsigned long flags;
358 BUG_ON(ld == NULL);
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);
370 EXPORT_SYMBOL_GPL(tty_ldisc_deref);
373 * tty_ldisc_enable - allow ldisc use
374 * @tty: terminal to activate ldisc on
376 * Set the TTY_LDISC flag when the line discipline can be called
377 * again. Do necessary wakeups for existing sleepers. Clear the LDISC
378 * changing flag to indicate any ldisc change is now over.
380 * Note: nobody should set the TTY_LDISC bit except via this function.
381 * Clearing directly is allowed.
384 void tty_ldisc_enable(struct tty_struct *tty)
386 set_bit(TTY_LDISC, &tty->flags);
387 clear_bit(TTY_LDISC_CHANGING, &tty->flags);
388 wake_up(&tty_ldisc_wait);
392 * tty_set_termios_ldisc - set ldisc field
393 * @tty: tty structure
394 * @num: line discipline number
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.
400 * Locking: takes termios_mutex
403 static void tty_set_termios_ldisc(struct tty_struct *tty, int num)
405 mutex_lock(&tty->termios_mutex);
406 tty->termios->c_line = num;
407 mutex_unlock(&tty->termios_mutex);
412 * tty_ldisc_restore - helper for tty ldisc change
413 * @tty: tty to recover
414 * @old: previous ldisc
416 * Restore the previous line discipline or N_TTY when a line discipline
417 * change fails due to an open error
420 static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old)
422 char buf[64];
423 struct tty_ldisc new_ldisc;
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);
447 * tty_set_ldisc - set line discipline
448 * @tty: the terminal to set
449 * @ldisc: the line discipline
451 * Set the discipline of a tty line. Must be called from a process
452 * context.
454 * Locking: takes tty_ldisc_lock.
455 * called functions take termios_mutex
458 int tty_set_ldisc(struct tty_struct *tty, int ldisc)
460 int retval;
461 struct tty_ldisc o_ldisc, new_ldisc;
462 int work;
463 unsigned long flags;
464 struct tty_struct *o_tty;
466 restart:
467 /* This is a bit ugly for now but means we can break the 'ldisc
468 is part of the tty struct' assumption later */
469 retval = tty_ldisc_get(ldisc, &new_ldisc);
470 if (retval)
471 return retval;
474 * Problem: What do we do if this blocks ?
477 tty_wait_until_sent(tty, 0);
479 if (tty->ldisc.ops->num == ldisc) {
480 tty_ldisc_put(new_ldisc.ops);
481 return 0;
485 * No more input please, we are switching. The new ldisc
486 * will update this value in the ldisc open function
489 tty->receive_room = 0;
491 o_ldisc = tty->ldisc;
492 o_tty = tty->link;
495 * Make sure we don't change while someone holds a
496 * reference to the line discipline. The TTY_LDISC bit
497 * prevents anyone taking a reference once it is clear.
498 * We need the lock to avoid racing reference takers.
500 * We must clear the TTY_LDISC bit here to avoid a livelock
501 * with a userspace app continually trying to use the tty in
502 * parallel to the change and re-referencing the tty.
504 clear_bit(TTY_LDISC, &tty->flags);
505 if (o_tty)
506 clear_bit(TTY_LDISC, &o_tty->flags);
508 spin_lock_irqsave(&tty_ldisc_lock, flags);
509 if (tty->ldisc.refcount || (o_tty && o_tty->ldisc.refcount)) {
510 if (tty->ldisc.refcount) {
511 /* Free the new ldisc we grabbed. Must drop the lock
512 first. */
513 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
514 tty_ldisc_put(o_ldisc.ops);
516 * There are several reasons we may be busy, including
517 * random momentary I/O traffic. We must therefore
518 * retry. We could distinguish between blocking ops
519 * and retries if we made tty_ldisc_wait() smarter.
520 * That is up for discussion.
522 if (wait_event_interruptible(tty_ldisc_wait, tty->ldisc.refcount == 0) < 0)
523 return -ERESTARTSYS;
524 goto restart;
526 if (o_tty && o_tty->ldisc.refcount) {
527 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
528 tty_ldisc_put(o_tty->ldisc.ops);
529 if (wait_event_interruptible(tty_ldisc_wait, o_tty->ldisc.refcount == 0) < 0)
530 return -ERESTARTSYS;
531 goto restart;
535 * If the TTY_LDISC bit is set, then we are racing against
536 * another ldisc change
538 if (test_bit(TTY_LDISC_CHANGING, &tty->flags)) {
539 struct tty_ldisc *ld;
540 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
541 tty_ldisc_put(new_ldisc.ops);
542 ld = tty_ldisc_ref_wait(tty);
543 tty_ldisc_deref(ld);
544 goto restart;
547 * This flag is used to avoid two parallel ldisc changes. Once
548 * open and close are fine grained locked this may work better
549 * as a mutex shared with the open/close/hup paths
551 set_bit(TTY_LDISC_CHANGING, &tty->flags);
552 if (o_tty)
553 set_bit(TTY_LDISC_CHANGING, &o_tty->flags);
554 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
557 * From this point on we know nobody has an ldisc
558 * usage reference, nor can they obtain one until
559 * we say so later on.
562 work = cancel_delayed_work(&tty->buf.work);
564 * Wait for ->hangup_work and ->buf.work handlers to terminate
565 * MUST NOT hold locks here.
567 flush_scheduled_work();
568 /* Shutdown the current discipline. */
569 if (o_ldisc.ops->close)
570 (o_ldisc.ops->close)(tty);
572 /* Now set up the new line discipline. */
573 tty_ldisc_assign(tty, &new_ldisc);
574 tty_set_termios_ldisc(tty, ldisc);
575 if (new_ldisc.ops->open)
576 retval = (new_ldisc.ops->open)(tty);
577 if (retval < 0) {
578 tty_ldisc_put(new_ldisc.ops);
579 tty_ldisc_restore(tty, &o_ldisc);
581 /* At this point we hold a reference to the new ldisc and a
582 a reference to the old ldisc. If we ended up flipping back
583 to the existing ldisc we have two references to it */
585 if (tty->ldisc.ops->num != o_ldisc.ops->num && tty->ops->set_ldisc)
586 tty->ops->set_ldisc(tty);
588 tty_ldisc_put(o_ldisc.ops);
591 * Allow ldisc referencing to occur as soon as the driver
592 * ldisc callback completes.
595 tty_ldisc_enable(tty);
596 if (o_tty)
597 tty_ldisc_enable(o_tty);
599 /* Restart it in case no characters kick it off. Safe if
600 already running */
601 if (work)
602 schedule_delayed_work(&tty->buf.work, 1);
603 return retval;
608 * tty_ldisc_setup - open line discipline
609 * @tty: tty being shut down
610 * @o_tty: pair tty for pty/tty pairs
612 * Called during the initial open of a tty/pty pair in order to set up the
613 * line discplines and bind them to the tty.
616 int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty)
618 struct tty_ldisc *ld = &tty->ldisc;
619 int retval;
621 if (ld->ops->open) {
622 retval = (ld->ops->open)(tty);
623 if (retval)
624 return retval;
626 if (o_tty && o_tty->ldisc.ops->open) {
627 retval = (o_tty->ldisc.ops->open)(o_tty);
628 if (retval) {
629 if (ld->ops->close)
630 (ld->ops->close)(tty);
631 return retval;
633 tty_ldisc_enable(o_tty);
635 tty_ldisc_enable(tty);
636 return 0;
640 * tty_ldisc_release - release line discipline
641 * @tty: tty being shut down
642 * @o_tty: pair tty for pty/tty pairs
644 * Called during the final close of a tty/pty pair in order to shut down the
645 * line discpline layer.
648 void tty_ldisc_release(struct tty_struct *tty, struct tty_struct *o_tty)
650 unsigned long flags;
651 struct tty_ldisc ld;
653 * Prevent flush_to_ldisc() from rescheduling the work for later. Then
654 * kill any delayed work. As this is the final close it does not
655 * race with the set_ldisc code path.
657 clear_bit(TTY_LDISC, &tty->flags);
658 cancel_delayed_work(&tty->buf.work);
661 * Wait for ->hangup_work and ->buf.work handlers to terminate
664 flush_scheduled_work();
667 * Wait for any short term users (we know they are just driver
668 * side waiters as the file is closing so user count on the file
669 * side is zero.
671 spin_lock_irqsave(&tty_ldisc_lock, flags);
672 while (tty->ldisc.refcount) {
673 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
674 wait_event(tty_ldisc_wait, tty->ldisc.refcount == 0);
675 spin_lock_irqsave(&tty_ldisc_lock, flags);
677 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
679 * Shutdown the current line discipline, and reset it to N_TTY.
681 * FIXME: this MUST get fixed for the new reflocking
683 if (tty->ldisc.ops->close)
684 (tty->ldisc.ops->close)(tty);
685 tty_ldisc_put(tty->ldisc.ops);
688 * Switch the line discipline back
690 WARN_ON(tty_ldisc_get(N_TTY, &ld));
691 tty_ldisc_assign(tty, &ld);
692 tty_set_termios_ldisc(tty, N_TTY);
693 if (o_tty) {
694 /* FIXME: could o_tty be in setldisc here ? */
695 clear_bit(TTY_LDISC, &o_tty->flags);
696 if (o_tty->ldisc.ops->close)
697 (o_tty->ldisc.ops->close)(o_tty);
698 tty_ldisc_put(o_tty->ldisc.ops);
699 WARN_ON(tty_ldisc_get(N_TTY, &ld));
700 tty_ldisc_assign(o_tty, &ld);
701 tty_set_termios_ldisc(o_tty, N_TTY);
706 * tty_ldisc_init - ldisc setup for new tty
707 * @tty: tty being allocated
709 * Set up the line discipline objects for a newly allocated tty. Note that
710 * the tty structure is not completely set up when this call is made.
713 void tty_ldisc_init(struct tty_struct *tty)
715 struct tty_ldisc ld;
716 if (tty_ldisc_get(N_TTY, &ld) < 0)
717 panic("n_tty: init_tty");
718 tty_ldisc_assign(tty, &ld);
721 void tty_ldisc_begin(void)
723 /* Setup the default TTY line discipline. */
724 (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);