2 * linux/drivers/char/tty_io.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
8 * 'tty_io.c' gives an orthogonal feeling to tty's, be they consoles
9 * or rs-channels. It also implements echoing, cooked mode etc.
11 * Kill-line thanks to John T Kohl, who also corrected VMIN = VTIME = 0.
13 * Modified by Theodore Ts'o, 9/14/92, to dynamically allocate the
14 * tty_struct and tty_queue structures. Previously there was an array
15 * of 256 tty_struct's which was statically allocated, and the
16 * tty_queue structures were allocated at boot time. Both are now
17 * dynamically allocated only when the tty is open.
19 * Also restructured routines so that there is more of a separation
20 * between the high-level tty routines (tty_io.c and tty_ioctl.c) and
21 * the low-level tty routines (serial.c, pty.c, console.c). This
22 * makes for cleaner and more compact code. -TYT, 9/17/92
24 * Modified by Fred N. van Kempen, 01/29/93, to add line disciplines
25 * which can be dynamically activated and de-activated by the line
26 * discipline handling modules (like SLIP).
28 * NOTE: pay no attention to the line discipline code (yet); its
29 * interface is still subject to change in this version...
32 * Added functionality to the OPOST tty handling. No delays, but all
33 * other bits should be there.
34 * -- Nick Holloway <alfie@dcs.warwick.ac.uk>, 27th May 1993.
36 * Rewrote canonical mode and added more termios flags.
37 * -- julian@uhunix.uhcc.hawaii.edu (J. Cowley), 13Jan94
39 * Reorganized FASYNC support so mouse code can share it.
40 * -- ctm@ardi.com, 9Sep95
42 * New TIOCLINUX variants added.
43 * -- mj@k332.feld.cvut.cz, 19-Nov-95
45 * Restrict vt switching via ioctl()
46 * -- grif@cs.ucr.edu, 5-Dec-95
48 * Move console and virtual terminal code to more appropriate files,
49 * implement CONFIG_VT and generalize console device interface.
50 * -- Marko Kohtala <Marko.Kohtala@hut.fi>, March 97
52 * Rewrote init_dev and release_dev to eliminate races.
53 * -- Bill Hawes <whawes@star.net>, June 97
55 * Added devfs support.
56 * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 13-Jan-1998
58 * Added support for a Unix98-style ptmx device.
59 * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998
61 * Reduced memory usage for older ARM systems
62 * -- Russell King <rmk@arm.linux.org.uk>
64 * Move do_SAK() into process context. Less stack use in devfs functions.
65 * alloc_tty_struct() always uses kmalloc() -- Andrew Morton <andrewm@uow.edu.eu> 17Mar01
68 #include <linux/types.h>
69 #include <linux/major.h>
70 #include <linux/errno.h>
71 #include <linux/signal.h>
72 #include <linux/fcntl.h>
73 #include <linux/sched.h>
74 #include <linux/interrupt.h>
75 #include <linux/tty.h>
76 #include <linux/tty_driver.h>
77 #include <linux/tty_flip.h>
78 #include <linux/devpts_fs.h>
79 #include <linux/file.h>
80 #include <linux/console.h>
81 #include <linux/timer.h>
82 #include <linux/ctype.h>
85 #include <linux/string.h>
86 #include <linux/slab.h>
87 #include <linux/poll.h>
88 #include <linux/proc_fs.h>
89 #include <linux/init.h>
90 #include <linux/module.h>
91 #include <linux/smp_lock.h>
92 #include <linux/device.h>
93 #include <linux/idr.h>
94 #include <linux/wait.h>
95 #include <linux/bitops.h>
96 #include <linux/delay.h>
98 #include <asm/uaccess.h>
99 #include <asm/system.h>
101 #include <linux/kbd_kern.h>
102 #include <linux/vt_kern.h>
103 #include <linux/selection.h>
105 #include <linux/kmod.h>
107 #undef TTY_DEBUG_HANGUP
109 #define TTY_PARANOIA_CHECK 1
110 #define CHECK_TTY_COUNT 1
112 struct termios tty_std_termios = { /* for the benefit of tty drivers */
113 .c_iflag = ICRNL | IXON,
114 .c_oflag = OPOST | ONLCR,
115 .c_cflag = B38400 | CS8 | CREAD | HUPCL,
116 .c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK |
117 ECHOCTL | ECHOKE | IEXTEN,
121 EXPORT_SYMBOL(tty_std_termios);
123 /* This list gets poked at by procfs and various bits of boot up code. This
124 could do with some rationalisation such as pulling the tty proc function
127 LIST_HEAD(tty_drivers); /* linked list of tty drivers */
129 /* Semaphore to protect creating and releasing a tty. This is shared with
130 vt.c for deeply disgusting hack reasons */
131 DEFINE_MUTEX(tty_mutex);
133 #ifdef CONFIG_UNIX98_PTYS
134 extern struct tty_driver *ptm_driver; /* Unix98 pty masters; for /dev/ptmx */
135 extern int pty_limit; /* Config limit on Unix98 ptys */
136 static DEFINE_IDR(allocated_ptys);
137 static DECLARE_MUTEX(allocated_ptys_lock);
138 static int ptmx_open(struct inode *, struct file *);
141 extern void disable_early_printk(void);
143 static void initialize_tty_struct(struct tty_struct *tty);
145 static ssize_t tty_read(struct file *, char __user *, size_t, loff_t *);
146 static ssize_t tty_write(struct file *, const char __user *, size_t, loff_t *);
147 ssize_t redirected_tty_write(struct file *, const char __user *, size_t, loff_t *);
148 static unsigned int tty_poll(struct file *, poll_table *);
149 static int tty_open(struct inode *, struct file *);
150 static int tty_release(struct inode *, struct file *);
151 int tty_ioctl(struct inode * inode, struct file * file,
152 unsigned int cmd, unsigned long arg);
153 static int tty_fasync(int fd, struct file * filp, int on);
154 static void release_mem(struct tty_struct *tty, int idx);
157 * alloc_tty_struct - allocate a tty object
159 * Return a new empty tty structure. The data fields have not
160 * been initialized in any way but has been zeroed
166 static struct tty_struct *alloc_tty_struct(void)
168 struct tty_struct *tty;
170 tty = kmalloc(sizeof(struct tty_struct), GFP_KERNEL);
172 memset(tty, 0, sizeof(struct tty_struct));
176 static void tty_buffer_free_all(struct tty_struct *);
179 * free_tty_struct - free a disused tty
180 * @tty: tty struct to free
182 * Free the write buffers, tty queue and tty memory itself.
184 * Locking: none. Must be called after tty is definitely unused
187 static inline void free_tty_struct(struct tty_struct *tty)
189 kfree(tty->write_buf);
190 tty_buffer_free_all(tty);
194 #define TTY_NUMBER(tty) ((tty)->index + (tty)->driver->name_base)
197 * tty_name - return tty naming
198 * @tty: tty structure
199 * @buf: buffer for output
201 * Convert a tty structure into a name. The name reflects the kernel
202 * naming policy and if udev is in use may not reflect user space
207 char *tty_name(struct tty_struct *tty, char *buf)
209 if (!tty) /* Hmm. NULL pointer. That's fun. */
210 strcpy(buf, "NULL tty");
212 strcpy(buf, tty->name);
216 EXPORT_SYMBOL(tty_name);
218 int tty_paranoia_check(struct tty_struct *tty, struct inode *inode,
221 #ifdef TTY_PARANOIA_CHECK
224 "null TTY for (%d:%d) in %s\n",
225 imajor(inode), iminor(inode), routine);
228 if (tty->magic != TTY_MAGIC) {
230 "bad magic number for tty struct (%d:%d) in %s\n",
231 imajor(inode), iminor(inode), routine);
238 static int check_tty_count(struct tty_struct *tty, const char *routine)
240 #ifdef CHECK_TTY_COUNT
245 list_for_each(p, &tty->tty_files) {
249 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
250 tty->driver->subtype == PTY_TYPE_SLAVE &&
251 tty->link && tty->link->count)
253 if (tty->count != count) {
254 printk(KERN_WARNING "Warning: dev (%s) tty->count(%d) "
255 "!= #fd's(%d) in %s\n",
256 tty->name, tty->count, count, routine);
264 * Tty buffer allocation management
269 * tty_buffer_free_all - free buffers used by a tty
270 * @tty: tty to free from
272 * Remove all the buffers pending on a tty whether queued with data
273 * or in the free ring. Must be called when the tty is no longer in use
278 static void tty_buffer_free_all(struct tty_struct *tty)
280 struct tty_buffer *thead;
281 while((thead = tty->buf.head) != NULL) {
282 tty->buf.head = thead->next;
285 while((thead = tty->buf.free) != NULL) {
286 tty->buf.free = thead->next;
289 tty->buf.tail = NULL;
292 static void tty_buffer_init(struct tty_struct *tty)
294 spin_lock_init(&tty->buf.lock);
295 tty->buf.head = NULL;
296 tty->buf.tail = NULL;
297 tty->buf.free = NULL;
300 static struct tty_buffer *tty_buffer_alloc(size_t size)
302 struct tty_buffer *p = kmalloc(sizeof(struct tty_buffer) + 2 * size, GFP_ATOMIC);
310 p->char_buf_ptr = (char *)(p->data);
311 p->flag_buf_ptr = (unsigned char *)p->char_buf_ptr + size;
312 /* printk("Flip create %p\n", p); */
316 /* Must be called with the tty_read lock held. This needs to acquire strategy
317 code to decide if we should kfree or relink a given expired buffer */
319 static void tty_buffer_free(struct tty_struct *tty, struct tty_buffer *b)
321 /* Dumb strategy for now - should keep some stats */
322 /* printk("Flip dispose %p\n", b); */
326 b->next = tty->buf.free;
331 static struct tty_buffer *tty_buffer_find(struct tty_struct *tty, size_t size)
333 struct tty_buffer **tbh = &tty->buf.free;
334 while((*tbh) != NULL) {
335 struct tty_buffer *t = *tbh;
336 if(t->size >= size) {
343 /* memset(t->data, '*', size); */
344 /* printk("Flip recycle %p\n", t); */
347 tbh = &((*tbh)->next);
349 /* Round the buffer size out */
350 size = (size + 0xFF) & ~ 0xFF;
351 return tty_buffer_alloc(size);
352 /* Should possibly check if this fails for the largest buffer we
353 have queued and recycle that ? */
356 int tty_buffer_request_room(struct tty_struct *tty, size_t size)
358 struct tty_buffer *b, *n;
362 spin_lock_irqsave(&tty->buf.lock, flags);
364 /* OPTIMISATION: We could keep a per tty "zero" sized buffer to
365 remove this conditional if its worth it. This would be invisible
367 if ((b = tty->buf.tail) != NULL)
368 left = b->size - b->used;
373 /* This is the slow path - looking for new buffers to use */
374 if ((n = tty_buffer_find(tty, size)) != NULL) {
385 spin_unlock_irqrestore(&tty->buf.lock, flags);
388 EXPORT_SYMBOL_GPL(tty_buffer_request_room);
391 * tty_insert_flip_string - Add characters to the tty buffer
392 * @tty: tty structure
396 * Queue a series of bytes to the tty buffering. All the characters
397 * passed are marked as without error. Returns the number added.
399 * Locking: Called functions may take tty->buf.lock
402 int tty_insert_flip_string(struct tty_struct *tty, const unsigned char *chars,
407 int space = tty_buffer_request_room(tty, size - copied);
408 struct tty_buffer *tb = tty->buf.tail;
409 /* If there is no space then tb may be NULL */
410 if(unlikely(space == 0))
412 memcpy(tb->char_buf_ptr + tb->used, chars, space);
413 memset(tb->flag_buf_ptr + tb->used, TTY_NORMAL, space);
418 /* There is a small chance that we need to split the data over
419 several buffers. If this is the case we must loop */
420 while (unlikely(size > copied));
423 EXPORT_SYMBOL(tty_insert_flip_string);
426 * tty_insert_flip_string_flags - Add characters to the tty buffer
427 * @tty: tty structure
432 * Queue a series of bytes to the tty buffering. For each character
433 * the flags array indicates the status of the character. Returns the
436 * Locking: Called functions may take tty->buf.lock
439 int tty_insert_flip_string_flags(struct tty_struct *tty,
440 const unsigned char *chars, const char *flags, size_t size)
444 int space = tty_buffer_request_room(tty, size - copied);
445 struct tty_buffer *tb = tty->buf.tail;
446 /* If there is no space then tb may be NULL */
447 if(unlikely(space == 0))
449 memcpy(tb->char_buf_ptr + tb->used, chars, space);
450 memcpy(tb->flag_buf_ptr + tb->used, flags, space);
456 /* There is a small chance that we need to split the data over
457 several buffers. If this is the case we must loop */
458 while (unlikely(size > copied));
461 EXPORT_SYMBOL(tty_insert_flip_string_flags);
464 * tty_schedule_flip - push characters to ldisc
465 * @tty: tty to push from
467 * Takes any pending buffers and transfers their ownership to the
468 * ldisc side of the queue. It then schedules those characters for
469 * processing by the line discipline.
471 * Locking: Takes tty->buf.lock
474 void tty_schedule_flip(struct tty_struct *tty)
477 spin_lock_irqsave(&tty->buf.lock, flags);
478 if (tty->buf.tail != NULL)
479 tty->buf.tail->commit = tty->buf.tail->used;
480 spin_unlock_irqrestore(&tty->buf.lock, flags);
481 schedule_delayed_work(&tty->buf.work, 1);
483 EXPORT_SYMBOL(tty_schedule_flip);
486 * tty_prepare_flip_string - make room for characters
488 * @chars: return pointer for character write area
489 * @size: desired size
491 * Prepare a block of space in the buffer for data. Returns the length
492 * available and buffer pointer to the space which is now allocated and
493 * accounted for as ready for normal characters. This is used for drivers
494 * that need their own block copy routines into the buffer. There is no
495 * guarantee the buffer is a DMA target!
497 * Locking: May call functions taking tty->buf.lock
500 int tty_prepare_flip_string(struct tty_struct *tty, unsigned char **chars, size_t size)
502 int space = tty_buffer_request_room(tty, size);
504 struct tty_buffer *tb = tty->buf.tail;
505 *chars = tb->char_buf_ptr + tb->used;
506 memset(tb->flag_buf_ptr + tb->used, TTY_NORMAL, space);
512 EXPORT_SYMBOL_GPL(tty_prepare_flip_string);
515 * tty_prepare_flip_string_flags - make room for characters
517 * @chars: return pointer for character write area
518 * @flags: return pointer for status flag write area
519 * @size: desired size
521 * Prepare a block of space in the buffer for data. Returns the length
522 * available and buffer pointer to the space which is now allocated and
523 * accounted for as ready for characters. This is used for drivers
524 * that need their own block copy routines into the buffer. There is no
525 * guarantee the buffer is a DMA target!
527 * Locking: May call functions taking tty->buf.lock
530 int tty_prepare_flip_string_flags(struct tty_struct *tty, unsigned char **chars, char **flags, size_t size)
532 int space = tty_buffer_request_room(tty, size);
534 struct tty_buffer *tb = tty->buf.tail;
535 *chars = tb->char_buf_ptr + tb->used;
536 *flags = tb->flag_buf_ptr + tb->used;
542 EXPORT_SYMBOL_GPL(tty_prepare_flip_string_flags);
547 * tty_set_termios_ldisc - set ldisc field
548 * @tty: tty structure
549 * @num: line discipline number
551 * This is probably overkill for real world processors but
552 * they are not on hot paths so a little discipline won't do
555 * Locking: takes termios_sem
558 static void tty_set_termios_ldisc(struct tty_struct *tty, int num)
560 down(&tty->termios_sem);
561 tty->termios->c_line = num;
562 up(&tty->termios_sem);
566 * This guards the refcounted line discipline lists. The lock
567 * must be taken with irqs off because there are hangup path
568 * callers who will do ldisc lookups and cannot sleep.
571 static DEFINE_SPINLOCK(tty_ldisc_lock);
572 static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_wait);
573 static struct tty_ldisc tty_ldiscs[NR_LDISCS]; /* line disc dispatch table */
576 * tty_register_ldisc - install a line discipline
577 * @disc: ldisc number
578 * @new_ldisc: pointer to the ldisc object
580 * Installs a new line discipline into the kernel. The discipline
581 * is set up as unreferenced and then made available to the kernel
582 * from this point onwards.
585 * takes tty_ldisc_lock to guard against ldisc races
588 int tty_register_ldisc(int disc, struct tty_ldisc *new_ldisc)
593 if (disc < N_TTY || disc >= NR_LDISCS)
596 spin_lock_irqsave(&tty_ldisc_lock, flags);
597 tty_ldiscs[disc] = *new_ldisc;
598 tty_ldiscs[disc].num = disc;
599 tty_ldiscs[disc].flags |= LDISC_FLAG_DEFINED;
600 tty_ldiscs[disc].refcount = 0;
601 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
605 EXPORT_SYMBOL(tty_register_ldisc);
608 * tty_unregister_ldisc - unload a line discipline
609 * @disc: ldisc number
610 * @new_ldisc: pointer to the ldisc object
612 * Remove a line discipline from the kernel providing it is not
616 * takes tty_ldisc_lock to guard against ldisc races
619 int tty_unregister_ldisc(int disc)
624 if (disc < N_TTY || disc >= NR_LDISCS)
627 spin_lock_irqsave(&tty_ldisc_lock, flags);
628 if (tty_ldiscs[disc].refcount)
631 tty_ldiscs[disc].flags &= ~LDISC_FLAG_DEFINED;
632 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
636 EXPORT_SYMBOL(tty_unregister_ldisc);
639 * tty_ldisc_get - take a reference to an ldisc
640 * @disc: ldisc number
642 * Takes a reference to a line discipline. Deals with refcounts and
643 * module locking counts. Returns NULL if the discipline is not available.
644 * Returns a pointer to the discipline and bumps the ref count if it is
648 * takes tty_ldisc_lock to guard against ldisc races
651 struct tty_ldisc *tty_ldisc_get(int disc)
654 struct tty_ldisc *ld;
656 if (disc < N_TTY || disc >= NR_LDISCS)
659 spin_lock_irqsave(&tty_ldisc_lock, flags);
661 ld = &tty_ldiscs[disc];
662 /* Check the entry is defined */
663 if(ld->flags & LDISC_FLAG_DEFINED)
665 /* If the module is being unloaded we can't use it */
666 if (!try_module_get(ld->owner))
673 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
677 EXPORT_SYMBOL_GPL(tty_ldisc_get);
680 * tty_ldisc_put - drop ldisc reference
681 * @disc: ldisc number
683 * Drop a reference to a line discipline. Manage refcounts and
684 * module usage counts
687 * takes tty_ldisc_lock to guard against ldisc races
690 void tty_ldisc_put(int disc)
692 struct tty_ldisc *ld;
695 BUG_ON(disc < N_TTY || disc >= NR_LDISCS);
697 spin_lock_irqsave(&tty_ldisc_lock, flags);
698 ld = &tty_ldiscs[disc];
699 BUG_ON(ld->refcount == 0);
701 module_put(ld->owner);
702 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
705 EXPORT_SYMBOL_GPL(tty_ldisc_put);
708 * tty_ldisc_assign - set ldisc on a tty
709 * @tty: tty to assign
710 * @ld: line discipline
712 * Install an instance of a line discipline into a tty structure. The
713 * ldisc must have a reference count above zero to ensure it remains/
714 * The tty instance refcount starts at zero.
717 * Caller must hold references
720 static void tty_ldisc_assign(struct tty_struct *tty, struct tty_ldisc *ld)
723 tty->ldisc.refcount = 0;
727 * tty_ldisc_try - internal helper
730 * Make a single attempt to grab and bump the refcount on
731 * the tty ldisc. Return 0 on failure or 1 on success. This is
732 * used to implement both the waiting and non waiting versions
735 * Locking: takes tty_ldisc_lock
738 static int tty_ldisc_try(struct tty_struct *tty)
741 struct tty_ldisc *ld;
744 spin_lock_irqsave(&tty_ldisc_lock, flags);
746 if(test_bit(TTY_LDISC, &tty->flags))
751 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
756 * tty_ldisc_ref_wait - wait for the tty ldisc
759 * Dereference the line discipline for the terminal and take a
760 * reference to it. If the line discipline is in flux then
761 * wait patiently until it changes.
763 * Note: Must not be called from an IRQ/timer context. The caller
764 * must also be careful not to hold other locks that will deadlock
765 * against a discipline change, such as an existing ldisc reference
766 * (which we check for)
768 * Locking: call functions take tty_ldisc_lock
771 struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
773 /* wait_event is a macro */
774 wait_event(tty_ldisc_wait, tty_ldisc_try(tty));
775 if(tty->ldisc.refcount == 0)
776 printk(KERN_ERR "tty_ldisc_ref_wait\n");
780 EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
783 * tty_ldisc_ref - get the tty ldisc
786 * Dereference the line discipline for the terminal and take a
787 * reference to it. If the line discipline is in flux then
788 * return NULL. Can be called from IRQ and timer functions.
790 * Locking: called functions take tty_ldisc_lock
793 struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
795 if(tty_ldisc_try(tty))
800 EXPORT_SYMBOL_GPL(tty_ldisc_ref);
803 * tty_ldisc_deref - free a tty ldisc reference
804 * @ld: reference to free up
806 * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May
807 * be called in IRQ context.
809 * Locking: takes tty_ldisc_lock
812 void tty_ldisc_deref(struct tty_ldisc *ld)
818 spin_lock_irqsave(&tty_ldisc_lock, flags);
819 if(ld->refcount == 0)
820 printk(KERN_ERR "tty_ldisc_deref: no references.\n");
823 if(ld->refcount == 0)
824 wake_up(&tty_ldisc_wait);
825 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
828 EXPORT_SYMBOL_GPL(tty_ldisc_deref);
831 * tty_ldisc_enable - allow ldisc use
832 * @tty: terminal to activate ldisc on
834 * Set the TTY_LDISC flag when the line discipline can be called
835 * again. Do neccessary wakeups for existing sleepers.
837 * Note: nobody should set this bit except via this function. Clearing
838 * directly is allowed.
841 static void tty_ldisc_enable(struct tty_struct *tty)
843 set_bit(TTY_LDISC, &tty->flags);
844 wake_up(&tty_ldisc_wait);
848 * tty_set_ldisc - set line discipline
849 * @tty: the terminal to set
850 * @ldisc: the line discipline
852 * Set the discipline of a tty line. Must be called from a process
855 * Locking: takes tty_ldisc_lock.
856 * called functions take termios_sem
859 static int tty_set_ldisc(struct tty_struct *tty, int ldisc)
862 struct tty_ldisc o_ldisc;
866 struct tty_ldisc *ld;
867 struct tty_struct *o_tty;
869 if ((ldisc < N_TTY) || (ldisc >= NR_LDISCS))
874 ld = tty_ldisc_get(ldisc);
875 /* Eduardo Blanco <ejbs@cs.cs.com.uy> */
876 /* Cyrus Durgin <cider@speakeasy.org> */
878 request_module("tty-ldisc-%d", ldisc);
879 ld = tty_ldisc_get(ldisc);
885 * No more input please, we are switching. The new ldisc
886 * will update this value in the ldisc open function
889 tty->receive_room = 0;
892 * Problem: What do we do if this blocks ?
895 tty_wait_until_sent(tty, 0);
897 if (tty->ldisc.num == ldisc) {
898 tty_ldisc_put(ldisc);
902 o_ldisc = tty->ldisc;
906 * Make sure we don't change while someone holds a
907 * reference to the line discipline. The TTY_LDISC bit
908 * prevents anyone taking a reference once it is clear.
909 * We need the lock to avoid racing reference takers.
912 spin_lock_irqsave(&tty_ldisc_lock, flags);
913 if (tty->ldisc.refcount || (o_tty && o_tty->ldisc.refcount)) {
914 if(tty->ldisc.refcount) {
915 /* Free the new ldisc we grabbed. Must drop the lock
917 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
918 tty_ldisc_put(ldisc);
920 * There are several reasons we may be busy, including
921 * random momentary I/O traffic. We must therefore
922 * retry. We could distinguish between blocking ops
923 * and retries if we made tty_ldisc_wait() smarter. That
924 * is up for discussion.
926 if (wait_event_interruptible(tty_ldisc_wait, tty->ldisc.refcount == 0) < 0)
930 if(o_tty && o_tty->ldisc.refcount) {
931 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
932 tty_ldisc_put(ldisc);
933 if (wait_event_interruptible(tty_ldisc_wait, o_tty->ldisc.refcount == 0) < 0)
939 /* if the TTY_LDISC bit is set, then we are racing against another ldisc change */
941 if (!test_bit(TTY_LDISC, &tty->flags)) {
942 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
943 tty_ldisc_put(ldisc);
944 ld = tty_ldisc_ref_wait(tty);
949 clear_bit(TTY_LDISC, &tty->flags);
951 clear_bit(TTY_LDISC, &o_tty->flags);
952 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
955 * From this point on we know nobody has an ldisc
956 * usage reference, nor can they obtain one until
957 * we say so later on.
960 work = cancel_delayed_work(&tty->buf.work);
962 * Wait for ->hangup_work and ->buf.work handlers to terminate
965 flush_scheduled_work();
966 /* Shutdown the current discipline. */
967 if (tty->ldisc.close)
968 (tty->ldisc.close)(tty);
970 /* Now set up the new line discipline. */
971 tty_ldisc_assign(tty, ld);
972 tty_set_termios_ldisc(tty, ldisc);
974 retval = (tty->ldisc.open)(tty);
976 tty_ldisc_put(ldisc);
977 /* There is an outstanding reference here so this is safe */
978 tty_ldisc_assign(tty, tty_ldisc_get(o_ldisc.num));
979 tty_set_termios_ldisc(tty, tty->ldisc.num);
980 if (tty->ldisc.open && (tty->ldisc.open(tty) < 0)) {
981 tty_ldisc_put(o_ldisc.num);
982 /* This driver is always present */
983 tty_ldisc_assign(tty, tty_ldisc_get(N_TTY));
984 tty_set_termios_ldisc(tty, N_TTY);
985 if (tty->ldisc.open) {
986 int r = tty->ldisc.open(tty);
989 panic("Couldn't open N_TTY ldisc for "
991 tty_name(tty, buf), r);
995 /* At this point we hold a reference to the new ldisc and a
996 a reference to the old ldisc. If we ended up flipping back
997 to the existing ldisc we have two references to it */
999 if (tty->ldisc.num != o_ldisc.num && tty->driver->set_ldisc)
1000 tty->driver->set_ldisc(tty);
1002 tty_ldisc_put(o_ldisc.num);
1005 * Allow ldisc referencing to occur as soon as the driver
1006 * ldisc callback completes.
1009 tty_ldisc_enable(tty);
1011 tty_ldisc_enable(o_tty);
1013 /* Restart it in case no characters kick it off. Safe if
1016 schedule_delayed_work(&tty->buf.work, 1);
1021 * get_tty_driver - find device of a tty
1022 * @dev_t: device identifier
1023 * @index: returns the index of the tty
1025 * This routine returns a tty driver structure, given a device number
1026 * and also passes back the index number.
1028 * Locking: caller must hold tty_mutex
1031 static struct tty_driver *get_tty_driver(dev_t device, int *index)
1033 struct tty_driver *p;
1035 list_for_each_entry(p, &tty_drivers, tty_drivers) {
1036 dev_t base = MKDEV(p->major, p->minor_start);
1037 if (device < base || device >= base + p->num)
1039 *index = device - base;
1046 * tty_check_change - check for POSIX terminal changes
1047 * @tty: tty to check
1049 * If we try to write to, or set the state of, a terminal and we're
1050 * not in the foreground, send a SIGTTOU. If the signal is blocked or
1051 * ignored, go ahead and perform the operation. (POSIX 7.2)
1056 int tty_check_change(struct tty_struct * tty)
1058 if (current->signal->tty != tty)
1060 if (tty->pgrp <= 0) {
1061 printk(KERN_WARNING "tty_check_change: tty->pgrp <= 0!\n");
1064 if (process_group(current) == tty->pgrp)
1066 if (is_ignored(SIGTTOU))
1068 if (is_orphaned_pgrp(process_group(current)))
1070 (void) kill_pg(process_group(current), SIGTTOU, 1);
1071 return -ERESTARTSYS;
1074 EXPORT_SYMBOL(tty_check_change);
1076 static ssize_t hung_up_tty_read(struct file * file, char __user * buf,
1077 size_t count, loff_t *ppos)
1082 static ssize_t hung_up_tty_write(struct file * file, const char __user * buf,
1083 size_t count, loff_t *ppos)
1088 /* No kernel lock held - none needed ;) */
1089 static unsigned int hung_up_tty_poll(struct file * filp, poll_table * wait)
1091 return POLLIN | POLLOUT | POLLERR | POLLHUP | POLLRDNORM | POLLWRNORM;
1094 static int hung_up_tty_ioctl(struct inode * inode, struct file * file,
1095 unsigned int cmd, unsigned long arg)
1097 return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
1100 static const struct file_operations tty_fops = {
1101 .llseek = no_llseek,
1107 .release = tty_release,
1108 .fasync = tty_fasync,
1111 #ifdef CONFIG_UNIX98_PTYS
1112 static const struct file_operations ptmx_fops = {
1113 .llseek = no_llseek,
1119 .release = tty_release,
1120 .fasync = tty_fasync,
1124 static const struct file_operations console_fops = {
1125 .llseek = no_llseek,
1127 .write = redirected_tty_write,
1131 .release = tty_release,
1132 .fasync = tty_fasync,
1135 static const struct file_operations hung_up_tty_fops = {
1136 .llseek = no_llseek,
1137 .read = hung_up_tty_read,
1138 .write = hung_up_tty_write,
1139 .poll = hung_up_tty_poll,
1140 .ioctl = hung_up_tty_ioctl,
1141 .release = tty_release,
1144 static DEFINE_SPINLOCK(redirect_lock);
1145 static struct file *redirect;
1148 * tty_wakeup - request more data
1151 * Internal and external helper for wakeups of tty. This function
1152 * informs the line discipline if present that the driver is ready
1153 * to receive more output data.
1156 void tty_wakeup(struct tty_struct *tty)
1158 struct tty_ldisc *ld;
1160 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) {
1161 ld = tty_ldisc_ref(tty);
1163 if(ld->write_wakeup)
1164 ld->write_wakeup(tty);
1165 tty_ldisc_deref(ld);
1168 wake_up_interruptible(&tty->write_wait);
1171 EXPORT_SYMBOL_GPL(tty_wakeup);
1174 * tty_ldisc_flush - flush line discipline queue
1177 * Flush the line discipline queue (if any) for this tty. If there
1178 * is no line discipline active this is a no-op.
1181 void tty_ldisc_flush(struct tty_struct *tty)
1183 struct tty_ldisc *ld = tty_ldisc_ref(tty);
1185 if(ld->flush_buffer)
1186 ld->flush_buffer(tty);
1187 tty_ldisc_deref(ld);
1191 EXPORT_SYMBOL_GPL(tty_ldisc_flush);
1194 * do_tty_hangup - actual handler for hangup events
1197 * This can be called by the "eventd" kernel thread. That is process
1198 * synchronous but doesn't hold any locks, so we need to make sure we
1199 * have the appropriate locks for what we're doing.
1201 * The hangup event clears any pending redirections onto the hung up
1202 * device. It ensures future writes will error and it does the needed
1203 * line discipline hangup and signal delivery. The tty object itself
1208 * redirect lock for undoing redirection
1209 * file list lock for manipulating list of ttys
1210 * tty_ldisc_lock from called functions
1211 * termios_sem resetting termios data
1212 * tasklist_lock to walk task list for hangup event
1215 static void do_tty_hangup(void *data)
1217 struct tty_struct *tty = (struct tty_struct *) data;
1218 struct file * cons_filp = NULL;
1219 struct file *filp, *f = NULL;
1220 struct task_struct *p;
1221 struct tty_ldisc *ld;
1222 int closecount = 0, n;
1227 /* inuse_filps is protected by the single kernel lock */
1230 spin_lock(&redirect_lock);
1231 if (redirect && redirect->private_data == tty) {
1235 spin_unlock(&redirect_lock);
1237 check_tty_count(tty, "do_tty_hangup");
1239 /* This breaks for file handles being sent over AF_UNIX sockets ? */
1240 list_for_each_entry(filp, &tty->tty_files, f_u.fu_list) {
1241 if (filp->f_op->write == redirected_tty_write)
1243 if (filp->f_op->write != tty_write)
1246 tty_fasync(-1, filp, 0); /* can't block */
1247 filp->f_op = &hung_up_tty_fops;
1251 /* FIXME! What are the locking issues here? This may me overdoing things..
1252 * this question is especially important now that we've removed the irqlock. */
1254 ld = tty_ldisc_ref(tty);
1255 if(ld != NULL) /* We may have no line discipline at this point */
1257 if (ld->flush_buffer)
1258 ld->flush_buffer(tty);
1259 if (tty->driver->flush_buffer)
1260 tty->driver->flush_buffer(tty);
1261 if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
1263 ld->write_wakeup(tty);
1268 /* FIXME: Once we trust the LDISC code better we can wait here for
1269 ldisc completion and fix the driver call race */
1271 wake_up_interruptible(&tty->write_wait);
1272 wake_up_interruptible(&tty->read_wait);
1275 * Shutdown the current line discipline, and reset it to
1278 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
1280 down(&tty->termios_sem);
1281 *tty->termios = tty->driver->init_termios;
1282 up(&tty->termios_sem);
1285 /* Defer ldisc switch */
1286 /* tty_deferred_ldisc_switch(N_TTY);
1288 This should get done automatically when the port closes and
1289 tty_release is called */
1291 read_lock(&tasklist_lock);
1292 if (tty->session > 0) {
1293 do_each_task_pid(tty->session, PIDTYPE_SID, p) {
1294 if (p->signal->tty == tty)
1295 p->signal->tty = NULL;
1296 if (!p->signal->leader)
1298 group_send_sig_info(SIGHUP, SEND_SIG_PRIV, p);
1299 group_send_sig_info(SIGCONT, SEND_SIG_PRIV, p);
1301 p->signal->tty_old_pgrp = tty->pgrp;
1302 } while_each_task_pid(tty->session, PIDTYPE_SID, p);
1304 read_unlock(&tasklist_lock);
1309 tty->ctrl_status = 0;
1311 * If one of the devices matches a console pointer, we
1312 * cannot just call hangup() because that will cause
1313 * tty->count and state->count to go out of sync.
1314 * So we just call close() the right number of times.
1317 if (tty->driver->close)
1318 for (n = 0; n < closecount; n++)
1319 tty->driver->close(tty, cons_filp);
1320 } else if (tty->driver->hangup)
1321 (tty->driver->hangup)(tty);
1323 /* We don't want to have driver/ldisc interactions beyond
1324 the ones we did here. The driver layer expects no
1325 calls after ->hangup() from the ldisc side. However we
1326 can't yet guarantee all that */
1328 set_bit(TTY_HUPPED, &tty->flags);
1330 tty_ldisc_enable(tty);
1331 tty_ldisc_deref(ld);
1339 * tty_hangup - trigger a hangup event
1340 * @tty: tty to hangup
1342 * A carrier loss (virtual or otherwise) has occurred on this like
1343 * schedule a hangup sequence to run after this event.
1346 void tty_hangup(struct tty_struct * tty)
1348 #ifdef TTY_DEBUG_HANGUP
1351 printk(KERN_DEBUG "%s hangup...\n", tty_name(tty, buf));
1353 schedule_work(&tty->hangup_work);
1356 EXPORT_SYMBOL(tty_hangup);
1359 * tty_vhangup - process vhangup
1360 * @tty: tty to hangup
1362 * The user has asked via system call for the terminal to be hung up.
1363 * We do this synchronously so that when the syscall returns the process
1364 * is complete. That guarantee is neccessary for security reasons.
1367 void tty_vhangup(struct tty_struct * tty)
1369 #ifdef TTY_DEBUG_HANGUP
1372 printk(KERN_DEBUG "%s vhangup...\n", tty_name(tty, buf));
1374 do_tty_hangup((void *) tty);
1376 EXPORT_SYMBOL(tty_vhangup);
1379 * tty_hung_up_p - was tty hung up
1380 * @filp: file pointer of tty
1382 * Return true if the tty has been subject to a vhangup or a carrier
1386 int tty_hung_up_p(struct file * filp)
1388 return (filp->f_op == &hung_up_tty_fops);
1391 EXPORT_SYMBOL(tty_hung_up_p);
1394 * disassociate_ctty - disconnect controlling tty
1395 * @on_exit: true if exiting so need to "hang up" the session
1397 * This function is typically called only by the session leader, when
1398 * it wants to disassociate itself from its controlling tty.
1400 * It performs the following functions:
1401 * (1) Sends a SIGHUP and SIGCONT to the foreground process group
1402 * (2) Clears the tty from being controlling the session
1403 * (3) Clears the controlling tty for all processes in the
1406 * The argument on_exit is set to 1 if called when a process is
1407 * exiting; it is 0 if called by the ioctl TIOCNOTTY.
1409 * Locking: tty_mutex is taken to protect current->signal->tty
1410 * BKL is taken for hysterical raisins
1411 * Tasklist lock is taken (under tty_mutex) to walk process
1412 * lists for the session.
1415 void disassociate_ctty(int on_exit)
1417 struct tty_struct *tty;
1418 struct task_struct *p;
1423 mutex_lock(&tty_mutex);
1424 tty = current->signal->tty;
1426 tty_pgrp = tty->pgrp;
1427 mutex_unlock(&tty_mutex);
1428 if (on_exit && tty->driver->type != TTY_DRIVER_TYPE_PTY)
1431 if (current->signal->tty_old_pgrp) {
1432 kill_pg(current->signal->tty_old_pgrp, SIGHUP, on_exit);
1433 kill_pg(current->signal->tty_old_pgrp, SIGCONT, on_exit);
1435 mutex_unlock(&tty_mutex);
1440 kill_pg(tty_pgrp, SIGHUP, on_exit);
1442 kill_pg(tty_pgrp, SIGCONT, on_exit);
1445 /* Must lock changes to tty_old_pgrp */
1446 mutex_lock(&tty_mutex);
1447 current->signal->tty_old_pgrp = 0;
1451 /* Now clear signal->tty under the lock */
1452 read_lock(&tasklist_lock);
1453 do_each_task_pid(current->signal->session, PIDTYPE_SID, p) {
1454 p->signal->tty = NULL;
1455 } while_each_task_pid(current->signal->session, PIDTYPE_SID, p);
1456 read_unlock(&tasklist_lock);
1457 mutex_unlock(&tty_mutex);
1463 * stop_tty - propogate flow control
1466 * Perform flow control to the driver. For PTY/TTY pairs we
1467 * must also propogate the TIOCKPKT status. May be called
1468 * on an already stopped device and will not re-call the driver
1471 * This functionality is used by both the line disciplines for
1472 * halting incoming flow and by the driver. It may therefore be
1473 * called from any context, may be under the tty atomic_write_lock
1477 * Broken. Relies on BKL which is unsafe here.
1480 void stop_tty(struct tty_struct *tty)
1485 if (tty->link && tty->link->packet) {
1486 tty->ctrl_status &= ~TIOCPKT_START;
1487 tty->ctrl_status |= TIOCPKT_STOP;
1488 wake_up_interruptible(&tty->link->read_wait);
1490 if (tty->driver->stop)
1491 (tty->driver->stop)(tty);
1494 EXPORT_SYMBOL(stop_tty);
1497 * start_tty - propogate flow control
1498 * @tty: tty to start
1500 * Start a tty that has been stopped if at all possible. Perform
1501 * any neccessary wakeups and propogate the TIOCPKT status. If this
1502 * is the tty was previous stopped and is being started then the
1503 * driver start method is invoked and the line discipline woken.
1506 * Broken. Relies on BKL which is unsafe here.
1509 void start_tty(struct tty_struct *tty)
1511 if (!tty->stopped || tty->flow_stopped)
1514 if (tty->link && tty->link->packet) {
1515 tty->ctrl_status &= ~TIOCPKT_STOP;
1516 tty->ctrl_status |= TIOCPKT_START;
1517 wake_up_interruptible(&tty->link->read_wait);
1519 if (tty->driver->start)
1520 (tty->driver->start)(tty);
1522 /* If we have a running line discipline it may need kicking */
1524 wake_up_interruptible(&tty->write_wait);
1527 EXPORT_SYMBOL(start_tty);
1530 * tty_read - read method for tty device files
1531 * @file: pointer to tty file
1533 * @count: size of user buffer
1536 * Perform the read system call function on this terminal device. Checks
1537 * for hung up devices before calling the line discipline method.
1540 * Locks the line discipline internally while needed
1541 * For historical reasons the line discipline read method is
1542 * invoked under the BKL. This will go away in time so do not rely on it
1543 * in new code. Multiple read calls may be outstanding in parallel.
1546 static ssize_t tty_read(struct file * file, char __user * buf, size_t count,
1550 struct tty_struct * tty;
1551 struct inode *inode;
1552 struct tty_ldisc *ld;
1554 tty = (struct tty_struct *)file->private_data;
1555 inode = file->f_dentry->d_inode;
1556 if (tty_paranoia_check(tty, inode, "tty_read"))
1558 if (!tty || (test_bit(TTY_IO_ERROR, &tty->flags)))
1561 /* We want to wait for the line discipline to sort out in this
1563 ld = tty_ldisc_ref_wait(tty);
1566 i = (ld->read)(tty,file,buf,count);
1569 tty_ldisc_deref(ld);
1572 inode->i_atime = current_fs_time(inode->i_sb);
1577 * Split writes up in sane blocksizes to avoid
1578 * denial-of-service type attacks
1580 static inline ssize_t do_tty_write(
1581 ssize_t (*write)(struct tty_struct *, struct file *, const unsigned char *, size_t),
1582 struct tty_struct *tty,
1584 const char __user *buf,
1587 ssize_t ret = 0, written = 0;
1590 /* FIXME: O_NDELAY ... */
1591 if (mutex_lock_interruptible(&tty->atomic_write_lock)) {
1592 return -ERESTARTSYS;
1596 * We chunk up writes into a temporary buffer. This
1597 * simplifies low-level drivers immensely, since they
1598 * don't have locking issues and user mode accesses.
1600 * But if TTY_NO_WRITE_SPLIT is set, we should use a
1603 * The default chunk-size is 2kB, because the NTTY
1604 * layer has problems with bigger chunks. It will
1605 * claim to be able to handle more characters than
1608 * FIXME: This can probably go away now except that 64K chunks
1609 * are too likely to fail unless switched to vmalloc...
1612 if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags))
1617 /* write_buf/write_cnt is protected by the atomic_write_lock mutex */
1618 if (tty->write_cnt < chunk) {
1624 buf = kmalloc(chunk, GFP_KERNEL);
1626 mutex_unlock(&tty->atomic_write_lock);
1629 kfree(tty->write_buf);
1630 tty->write_cnt = chunk;
1631 tty->write_buf = buf;
1634 /* Do the write .. */
1636 size_t size = count;
1640 if (copy_from_user(tty->write_buf, buf, size))
1643 ret = write(tty, file, tty->write_buf, size);
1653 if (signal_pending(current))
1658 struct inode *inode = file->f_dentry->d_inode;
1659 inode->i_mtime = current_fs_time(inode->i_sb);
1662 mutex_unlock(&tty->atomic_write_lock);
1668 * tty_write - write method for tty device file
1669 * @file: tty file pointer
1670 * @buf: user data to write
1671 * @count: bytes to write
1674 * Write data to a tty device via the line discipline.
1677 * Locks the line discipline as required
1678 * Writes to the tty driver are serialized by the atomic_write_lock
1679 * and are then processed in chunks to the device. The line discipline
1680 * write method will not be involked in parallel for each device
1681 * The line discipline write method is called under the big
1682 * kernel lock for historical reasons. New code should not rely on this.
1685 static ssize_t tty_write(struct file * file, const char __user * buf, size_t count,
1688 struct tty_struct * tty;
1689 struct inode *inode = file->f_dentry->d_inode;
1691 struct tty_ldisc *ld;
1693 tty = (struct tty_struct *)file->private_data;
1694 if (tty_paranoia_check(tty, inode, "tty_write"))
1696 if (!tty || !tty->driver->write || (test_bit(TTY_IO_ERROR, &tty->flags)))
1699 ld = tty_ldisc_ref_wait(tty);
1703 ret = do_tty_write(ld->write, tty, file, buf, count);
1704 tty_ldisc_deref(ld);
1708 ssize_t redirected_tty_write(struct file * file, const char __user * buf, size_t count,
1711 struct file *p = NULL;
1713 spin_lock(&redirect_lock);
1718 spin_unlock(&redirect_lock);
1722 res = vfs_write(p, buf, count, &p->f_pos);
1727 return tty_write(file, buf, count, ppos);
1730 static char ptychar[] = "pqrstuvwxyzabcde";
1733 * pty_line_name - generate name for a pty
1734 * @driver: the tty driver in use
1735 * @index: the minor number
1736 * @p: output buffer of at least 6 bytes
1738 * Generate a name from a driver reference and write it to the output
1743 static void pty_line_name(struct tty_driver *driver, int index, char *p)
1745 int i = index + driver->name_base;
1746 /* ->name is initialized to "ttyp", but "tty" is expected */
1747 sprintf(p, "%s%c%x",
1748 driver->subtype == PTY_TYPE_SLAVE ? "tty" : driver->name,
1749 ptychar[i >> 4 & 0xf], i & 0xf);
1753 * pty_line_name - generate name for a tty
1754 * @driver: the tty driver in use
1755 * @index: the minor number
1756 * @p: output buffer of at least 7 bytes
1758 * Generate a name from a driver reference and write it to the output
1763 static void tty_line_name(struct tty_driver *driver, int index, char *p)
1765 sprintf(p, "%s%d", driver->name, index + driver->name_base);
1769 * init_dev - initialise a tty device
1770 * @driver: tty driver we are opening a device on
1771 * @idx: device index
1772 * @tty: returned tty structure
1774 * Prepare a tty device. This may not be a "new" clean device but
1775 * could also be an active device. The pty drivers require special
1776 * handling because of this.
1779 * The function is called under the tty_mutex, which
1780 * protects us from the tty struct or driver itself going away.
1782 * On exit the tty device has the line discipline attached and
1783 * a reference count of 1. If a pair was created for pty/tty use
1784 * and the other was a pty master then it too has a reference count of 1.
1786 * WSH 06/09/97: Rewritten to remove races and properly clean up after a
1787 * failed open. The new code protects the open with a mutex, so it's
1788 * really quite straightforward. The mutex locking can probably be
1789 * relaxed for the (most common) case of reopening a tty.
1792 static int init_dev(struct tty_driver *driver, int idx,
1793 struct tty_struct **ret_tty)
1795 struct tty_struct *tty, *o_tty;
1796 struct termios *tp, **tp_loc, *o_tp, **o_tp_loc;
1797 struct termios *ltp, **ltp_loc, *o_ltp, **o_ltp_loc;
1800 /* check whether we're reopening an existing tty */
1801 if (driver->flags & TTY_DRIVER_DEVPTS_MEM) {
1802 tty = devpts_get_tty(idx);
1803 if (tty && driver->subtype == PTY_TYPE_MASTER)
1806 tty = driver->ttys[idx];
1808 if (tty) goto fast_track;
1811 * First time open is complex, especially for PTY devices.
1812 * This code guarantees that either everything succeeds and the
1813 * TTY is ready for operation, or else the table slots are vacated
1814 * and the allocated memory released. (Except that the termios
1815 * and locked termios may be retained.)
1818 if (!try_module_get(driver->owner)) {
1827 tty = alloc_tty_struct();
1830 initialize_tty_struct(tty);
1831 tty->driver = driver;
1833 tty_line_name(driver, idx, tty->name);
1835 if (driver->flags & TTY_DRIVER_DEVPTS_MEM) {
1836 tp_loc = &tty->termios;
1837 ltp_loc = &tty->termios_locked;
1839 tp_loc = &driver->termios[idx];
1840 ltp_loc = &driver->termios_locked[idx];
1844 tp = (struct termios *) kmalloc(sizeof(struct termios),
1848 *tp = driver->init_termios;
1852 ltp = (struct termios *) kmalloc(sizeof(struct termios),
1856 memset(ltp, 0, sizeof(struct termios));
1859 if (driver->type == TTY_DRIVER_TYPE_PTY) {
1860 o_tty = alloc_tty_struct();
1863 initialize_tty_struct(o_tty);
1864 o_tty->driver = driver->other;
1866 tty_line_name(driver->other, idx, o_tty->name);
1868 if (driver->flags & TTY_DRIVER_DEVPTS_MEM) {
1869 o_tp_loc = &o_tty->termios;
1870 o_ltp_loc = &o_tty->termios_locked;
1872 o_tp_loc = &driver->other->termios[idx];
1873 o_ltp_loc = &driver->other->termios_locked[idx];
1877 o_tp = (struct termios *)
1878 kmalloc(sizeof(struct termios), GFP_KERNEL);
1881 *o_tp = driver->other->init_termios;
1885 o_ltp = (struct termios *)
1886 kmalloc(sizeof(struct termios), GFP_KERNEL);
1889 memset(o_ltp, 0, sizeof(struct termios));
1893 * Everything allocated ... set up the o_tty structure.
1895 if (!(driver->other->flags & TTY_DRIVER_DEVPTS_MEM)) {
1896 driver->other->ttys[idx] = o_tty;
1902 o_tty->termios = *o_tp_loc;
1903 o_tty->termios_locked = *o_ltp_loc;
1904 driver->other->refcount++;
1905 if (driver->subtype == PTY_TYPE_MASTER)
1908 /* Establish the links in both directions */
1914 * All structures have been allocated, so now we install them.
1915 * Failures after this point use release_mem to clean up, so
1916 * there's no need to null out the local pointers.
1918 if (!(driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
1919 driver->ttys[idx] = tty;
1926 tty->termios = *tp_loc;
1927 tty->termios_locked = *ltp_loc;
1932 * Structures all installed ... call the ldisc open routines.
1933 * If we fail here just call release_mem to clean up. No need
1934 * to decrement the use counts, as release_mem doesn't care.
1937 if (tty->ldisc.open) {
1938 retval = (tty->ldisc.open)(tty);
1940 goto release_mem_out;
1942 if (o_tty && o_tty->ldisc.open) {
1943 retval = (o_tty->ldisc.open)(o_tty);
1945 if (tty->ldisc.close)
1946 (tty->ldisc.close)(tty);
1947 goto release_mem_out;
1949 tty_ldisc_enable(o_tty);
1951 tty_ldisc_enable(tty);
1955 * This fast open can be used if the tty is already open.
1956 * No memory is allocated, and the only failures are from
1957 * attempting to open a closing tty or attempting multiple
1958 * opens on a pty master.
1961 if (test_bit(TTY_CLOSING, &tty->flags)) {
1965 if (driver->type == TTY_DRIVER_TYPE_PTY &&
1966 driver->subtype == PTY_TYPE_MASTER) {
1968 * special case for PTY masters: only one open permitted,
1969 * and the slave side open count is incremented as well.
1978 tty->driver = driver; /* N.B. why do this every time?? */
1981 if(!test_bit(TTY_LDISC, &tty->flags))
1982 printk(KERN_ERR "init_dev but no ldisc\n");
1986 /* All paths come through here to release the mutex */
1990 /* Release locally allocated memory ... nothing placed in slots */
1994 free_tty_struct(o_tty);
1997 free_tty_struct(tty);
2000 module_put(driver->owner);
2004 /* call the tty release_mem routine to clean out this slot */
2006 printk(KERN_INFO "init_dev: ldisc open failed, "
2007 "clearing slot %d\n", idx);
2008 release_mem(tty, idx);
2013 * release_mem - release tty structure memory
2015 * Releases memory associated with a tty structure, and clears out the
2016 * driver table slots. This function is called when a device is no longer
2017 * in use. It also gets called when setup of a device fails.
2020 * tty_mutex - sometimes only
2021 * takes the file list lock internally when working on the list
2022 * of ttys that the driver keeps.
2023 * FIXME: should we require tty_mutex is held here ??
2026 static void release_mem(struct tty_struct *tty, int idx)
2028 struct tty_struct *o_tty;
2030 int devpts = tty->driver->flags & TTY_DRIVER_DEVPTS_MEM;
2032 if ((o_tty = tty->link) != NULL) {
2034 o_tty->driver->ttys[idx] = NULL;
2035 if (o_tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) {
2036 tp = o_tty->termios;
2038 o_tty->driver->termios[idx] = NULL;
2041 tp = o_tty->termios_locked;
2043 o_tty->driver->termios_locked[idx] = NULL;
2047 o_tty->driver->refcount--;
2049 list_del_init(&o_tty->tty_files);
2051 free_tty_struct(o_tty);
2055 tty->driver->ttys[idx] = NULL;
2056 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) {
2059 tty->driver->termios[idx] = NULL;
2062 tp = tty->termios_locked;
2064 tty->driver->termios_locked[idx] = NULL;
2069 tty->driver->refcount--;
2071 list_del_init(&tty->tty_files);
2073 module_put(tty->driver->owner);
2074 free_tty_struct(tty);
2078 * Even releasing the tty structures is a tricky business.. We have
2079 * to be very careful that the structures are all released at the
2080 * same time, as interrupts might otherwise get the wrong pointers.
2082 * WSH 09/09/97: rewritten to avoid some nasty race conditions that could
2083 * lead to double frees or releasing memory still in use.
2085 static void release_dev(struct file * filp)
2087 struct tty_struct *tty, *o_tty;
2088 int pty_master, tty_closing, o_tty_closing, do_sleep;
2092 unsigned long flags;
2094 tty = (struct tty_struct *)filp->private_data;
2095 if (tty_paranoia_check(tty, filp->f_dentry->d_inode, "release_dev"))
2098 check_tty_count(tty, "release_dev");
2100 tty_fasync(-1, filp, 0);
2103 pty_master = (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
2104 tty->driver->subtype == PTY_TYPE_MASTER);
2105 devpts = (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM) != 0;
2108 #ifdef TTY_PARANOIA_CHECK
2109 if (idx < 0 || idx >= tty->driver->num) {
2110 printk(KERN_DEBUG "release_dev: bad idx when trying to "
2111 "free (%s)\n", tty->name);
2114 if (!(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
2115 if (tty != tty->driver->ttys[idx]) {
2116 printk(KERN_DEBUG "release_dev: driver.table[%d] not tty "
2117 "for (%s)\n", idx, tty->name);
2120 if (tty->termios != tty->driver->termios[idx]) {
2121 printk(KERN_DEBUG "release_dev: driver.termios[%d] not termios "
2126 if (tty->termios_locked != tty->driver->termios_locked[idx]) {
2127 printk(KERN_DEBUG "release_dev: driver.termios_locked[%d] not "
2128 "termios_locked for (%s)\n",
2135 #ifdef TTY_DEBUG_HANGUP
2136 printk(KERN_DEBUG "release_dev of %s (tty count=%d)...",
2137 tty_name(tty, buf), tty->count);
2140 #ifdef TTY_PARANOIA_CHECK
2141 if (tty->driver->other &&
2142 !(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
2143 if (o_tty != tty->driver->other->ttys[idx]) {
2144 printk(KERN_DEBUG "release_dev: other->table[%d] "
2145 "not o_tty for (%s)\n",
2149 if (o_tty->termios != tty->driver->other->termios[idx]) {
2150 printk(KERN_DEBUG "release_dev: other->termios[%d] "
2151 "not o_termios for (%s)\n",
2155 if (o_tty->termios_locked !=
2156 tty->driver->other->termios_locked[idx]) {
2157 printk(KERN_DEBUG "release_dev: other->termios_locked["
2158 "%d] not o_termios_locked for (%s)\n",
2162 if (o_tty->link != tty) {
2163 printk(KERN_DEBUG "release_dev: bad pty pointers\n");
2168 if (tty->driver->close)
2169 tty->driver->close(tty, filp);
2172 * Sanity check: if tty->count is going to zero, there shouldn't be
2173 * any waiters on tty->read_wait or tty->write_wait. We test the
2174 * wait queues and kick everyone out _before_ actually starting to
2175 * close. This ensures that we won't block while releasing the tty
2178 * The test for the o_tty closing is necessary, since the master and
2179 * slave sides may close in any order. If the slave side closes out
2180 * first, its count will be one, since the master side holds an open.
2181 * Thus this test wouldn't be triggered at the time the slave closes,
2184 * Note that it's possible for the tty to be opened again while we're
2185 * flushing out waiters. By recalculating the closing flags before
2186 * each iteration we avoid any problems.
2189 /* Guard against races with tty->count changes elsewhere and
2190 opens on /dev/tty */
2192 mutex_lock(&tty_mutex);
2193 tty_closing = tty->count <= 1;
2194 o_tty_closing = o_tty &&
2195 (o_tty->count <= (pty_master ? 1 : 0));
2199 if (waitqueue_active(&tty->read_wait)) {
2200 wake_up(&tty->read_wait);
2203 if (waitqueue_active(&tty->write_wait)) {
2204 wake_up(&tty->write_wait);
2208 if (o_tty_closing) {
2209 if (waitqueue_active(&o_tty->read_wait)) {
2210 wake_up(&o_tty->read_wait);
2213 if (waitqueue_active(&o_tty->write_wait)) {
2214 wake_up(&o_tty->write_wait);
2221 printk(KERN_WARNING "release_dev: %s: read/write wait queue "
2222 "active!\n", tty_name(tty, buf));
2223 mutex_unlock(&tty_mutex);
2228 * The closing flags are now consistent with the open counts on
2229 * both sides, and we've completed the last operation that could
2230 * block, so it's safe to proceed with closing.
2233 if (--o_tty->count < 0) {
2234 printk(KERN_WARNING "release_dev: bad pty slave count "
2236 o_tty->count, tty_name(o_tty, buf));
2240 if (--tty->count < 0) {
2241 printk(KERN_WARNING "release_dev: bad tty->count (%d) for %s\n",
2242 tty->count, tty_name(tty, buf));
2247 * We've decremented tty->count, so we need to remove this file
2248 * descriptor off the tty->tty_files list; this serves two
2250 * - check_tty_count sees the correct number of file descriptors
2251 * associated with this tty.
2252 * - do_tty_hangup no longer sees this file descriptor as
2253 * something that needs to be handled for hangups.
2256 filp->private_data = NULL;
2259 * Perform some housekeeping before deciding whether to return.
2261 * Set the TTY_CLOSING flag if this was the last open. In the
2262 * case of a pty we may have to wait around for the other side
2263 * to close, and TTY_CLOSING makes sure we can't be reopened.
2266 set_bit(TTY_CLOSING, &tty->flags);
2268 set_bit(TTY_CLOSING, &o_tty->flags);
2271 * If _either_ side is closing, make sure there aren't any
2272 * processes that still think tty or o_tty is their controlling
2275 if (tty_closing || o_tty_closing) {
2276 struct task_struct *p;
2278 read_lock(&tasklist_lock);
2279 do_each_task_pid(tty->session, PIDTYPE_SID, p) {
2280 p->signal->tty = NULL;
2281 } while_each_task_pid(tty->session, PIDTYPE_SID, p);
2283 do_each_task_pid(o_tty->session, PIDTYPE_SID, p) {
2284 p->signal->tty = NULL;
2285 } while_each_task_pid(o_tty->session, PIDTYPE_SID, p);
2286 read_unlock(&tasklist_lock);
2289 mutex_unlock(&tty_mutex);
2291 /* check whether both sides are closing ... */
2292 if (!tty_closing || (o_tty && !o_tty_closing))
2295 #ifdef TTY_DEBUG_HANGUP
2296 printk(KERN_DEBUG "freeing tty structure...");
2299 * Prevent flush_to_ldisc() from rescheduling the work for later. Then
2300 * kill any delayed work. As this is the final close it does not
2301 * race with the set_ldisc code path.
2303 clear_bit(TTY_LDISC, &tty->flags);
2304 cancel_delayed_work(&tty->buf.work);
2307 * Wait for ->hangup_work and ->buf.work handlers to terminate
2310 flush_scheduled_work();
2313 * Wait for any short term users (we know they are just driver
2314 * side waiters as the file is closing so user count on the file
2317 spin_lock_irqsave(&tty_ldisc_lock, flags);
2318 while(tty->ldisc.refcount)
2320 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
2321 wait_event(tty_ldisc_wait, tty->ldisc.refcount == 0);
2322 spin_lock_irqsave(&tty_ldisc_lock, flags);
2324 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
2326 * Shutdown the current line discipline, and reset it to N_TTY.
2327 * N.B. why reset ldisc when we're releasing the memory??
2329 * FIXME: this MUST get fixed for the new reflocking
2331 if (tty->ldisc.close)
2332 (tty->ldisc.close)(tty);
2333 tty_ldisc_put(tty->ldisc.num);
2336 * Switch the line discipline back
2338 tty_ldisc_assign(tty, tty_ldisc_get(N_TTY));
2339 tty_set_termios_ldisc(tty,N_TTY);
2341 /* FIXME: could o_tty be in setldisc here ? */
2342 clear_bit(TTY_LDISC, &o_tty->flags);
2343 if (o_tty->ldisc.close)
2344 (o_tty->ldisc.close)(o_tty);
2345 tty_ldisc_put(o_tty->ldisc.num);
2346 tty_ldisc_assign(o_tty, tty_ldisc_get(N_TTY));
2347 tty_set_termios_ldisc(o_tty,N_TTY);
2350 * The release_mem function takes care of the details of clearing
2351 * the slots and preserving the termios structure.
2353 release_mem(tty, idx);
2355 #ifdef CONFIG_UNIX98_PTYS
2356 /* Make this pty number available for reallocation */
2358 down(&allocated_ptys_lock);
2359 idr_remove(&allocated_ptys, idx);
2360 up(&allocated_ptys_lock);
2367 * tty_open - open a tty device
2368 * @inode: inode of device file
2369 * @filp: file pointer to tty
2371 * tty_open and tty_release keep up the tty count that contains the
2372 * number of opens done on a tty. We cannot use the inode-count, as
2373 * different inodes might point to the same tty.
2375 * Open-counting is needed for pty masters, as well as for keeping
2376 * track of serial lines: DTR is dropped when the last close happens.
2377 * (This is not done solely through tty->count, now. - Ted 1/27/92)
2379 * The termios state of a pty is reset on first open so that
2380 * settings don't persist across reuse.
2382 * Locking: tty_mutex protects current->signal->tty, get_tty_driver and
2383 * init_dev work. tty->count should protect the rest.
2384 * task_lock is held to update task details for sessions
2387 static int tty_open(struct inode * inode, struct file * filp)
2389 struct tty_struct *tty;
2391 struct tty_driver *driver;
2393 dev_t device = inode->i_rdev;
2394 unsigned short saved_flags = filp->f_flags;
2396 nonseekable_open(inode, filp);
2399 noctty = filp->f_flags & O_NOCTTY;
2403 mutex_lock(&tty_mutex);
2405 if (device == MKDEV(TTYAUX_MAJOR,0)) {
2406 if (!current->signal->tty) {
2407 mutex_unlock(&tty_mutex);
2410 driver = current->signal->tty->driver;
2411 index = current->signal->tty->index;
2412 filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */
2417 if (device == MKDEV(TTY_MAJOR,0)) {
2418 extern struct tty_driver *console_driver;
2419 driver = console_driver;
2425 if (device == MKDEV(TTYAUX_MAJOR,1)) {
2426 driver = console_device(&index);
2428 /* Don't let /dev/console block */
2429 filp->f_flags |= O_NONBLOCK;
2433 mutex_unlock(&tty_mutex);
2437 driver = get_tty_driver(device, &index);
2439 mutex_unlock(&tty_mutex);
2443 retval = init_dev(driver, index, &tty);
2444 mutex_unlock(&tty_mutex);
2448 filp->private_data = tty;
2449 file_move(filp, &tty->tty_files);
2450 check_tty_count(tty, "tty_open");
2451 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
2452 tty->driver->subtype == PTY_TYPE_MASTER)
2454 #ifdef TTY_DEBUG_HANGUP
2455 printk(KERN_DEBUG "opening %s...", tty->name);
2458 if (tty->driver->open)
2459 retval = tty->driver->open(tty, filp);
2463 filp->f_flags = saved_flags;
2465 if (!retval && test_bit(TTY_EXCLUSIVE, &tty->flags) && !capable(CAP_SYS_ADMIN))
2469 #ifdef TTY_DEBUG_HANGUP
2470 printk(KERN_DEBUG "error %d in opening %s...", retval,
2474 if (retval != -ERESTARTSYS)
2476 if (signal_pending(current))
2480 * Need to reset f_op in case a hangup happened.
2482 if (filp->f_op == &hung_up_tty_fops)
2483 filp->f_op = &tty_fops;
2487 current->signal->leader &&
2488 !current->signal->tty &&
2489 tty->session == 0) {
2491 current->signal->tty = tty;
2492 task_unlock(current);
2493 current->signal->tty_old_pgrp = 0;
2494 tty->session = current->signal->session;
2495 tty->pgrp = process_group(current);
2500 #ifdef CONFIG_UNIX98_PTYS
2502 * ptmx_open - open a unix 98 pty master
2503 * @inode: inode of device file
2504 * @filp: file pointer to tty
2506 * Allocate a unix98 pty master device from the ptmx driver.
2508 * Locking: tty_mutex protects theinit_dev work. tty->count should
2510 * allocated_ptys_lock handles the list of free pty numbers
2513 static int ptmx_open(struct inode * inode, struct file * filp)
2515 struct tty_struct *tty;
2520 nonseekable_open(inode, filp);
2522 /* find a device that is not in use. */
2523 down(&allocated_ptys_lock);
2524 if (!idr_pre_get(&allocated_ptys, GFP_KERNEL)) {
2525 up(&allocated_ptys_lock);
2528 idr_ret = idr_get_new(&allocated_ptys, NULL, &index);
2530 up(&allocated_ptys_lock);
2531 if (idr_ret == -EAGAIN)
2535 if (index >= pty_limit) {
2536 idr_remove(&allocated_ptys, index);
2537 up(&allocated_ptys_lock);
2540 up(&allocated_ptys_lock);
2542 mutex_lock(&tty_mutex);
2543 retval = init_dev(ptm_driver, index, &tty);
2544 mutex_unlock(&tty_mutex);
2549 set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
2550 filp->private_data = tty;
2551 file_move(filp, &tty->tty_files);
2554 if (devpts_pty_new(tty->link))
2557 check_tty_count(tty, "tty_open");
2558 retval = ptm_driver->open(tty, filp);
2565 down(&allocated_ptys_lock);
2566 idr_remove(&allocated_ptys, index);
2567 up(&allocated_ptys_lock);
2573 * tty_release - vfs callback for close
2574 * @inode: inode of tty
2575 * @filp: file pointer for handle to tty
2577 * Called the last time each file handle is closed that references
2578 * this tty. There may however be several such references.
2581 * Takes bkl. See release_dev
2584 static int tty_release(struct inode * inode, struct file * filp)
2593 * tty_poll - check tty status
2594 * @filp: file being polled
2595 * @wait: poll wait structures to update
2597 * Call the line discipline polling method to obtain the poll
2598 * status of the device.
2600 * Locking: locks called line discipline but ldisc poll method
2601 * may be re-entered freely by other callers.
2604 static unsigned int tty_poll(struct file * filp, poll_table * wait)
2606 struct tty_struct * tty;
2607 struct tty_ldisc *ld;
2610 tty = (struct tty_struct *)filp->private_data;
2611 if (tty_paranoia_check(tty, filp->f_dentry->d_inode, "tty_poll"))
2614 ld = tty_ldisc_ref_wait(tty);
2616 ret = (ld->poll)(tty, filp, wait);
2617 tty_ldisc_deref(ld);
2621 static int tty_fasync(int fd, struct file * filp, int on)
2623 struct tty_struct * tty;
2626 tty = (struct tty_struct *)filp->private_data;
2627 if (tty_paranoia_check(tty, filp->f_dentry->d_inode, "tty_fasync"))
2630 retval = fasync_helper(fd, filp, on, &tty->fasync);
2635 if (!waitqueue_active(&tty->read_wait))
2636 tty->minimum_to_wake = 1;
2637 retval = f_setown(filp, (-tty->pgrp) ? : current->pid, 0);
2641 if (!tty->fasync && !waitqueue_active(&tty->read_wait))
2642 tty->minimum_to_wake = N_TTY_BUF_SIZE;
2648 * tiocsti - fake input character
2649 * @tty: tty to fake input into
2650 * @p: pointer to character
2652 * Fake input to a tty device. Does the neccessary locking and
2655 * FIXME: does not honour flow control ??
2658 * Called functions take tty_ldisc_lock
2659 * current->signal->tty check is safe without locks
2662 static int tiocsti(struct tty_struct *tty, char __user *p)
2665 struct tty_ldisc *ld;
2667 if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN))
2669 if (get_user(ch, p))
2671 ld = tty_ldisc_ref_wait(tty);
2672 ld->receive_buf(tty, &ch, &mbz, 1);
2673 tty_ldisc_deref(ld);
2678 * tiocgwinsz - implement window query ioctl
2680 * @arg: user buffer for result
2682 * Copies the kernel idea of the window size into the user buffer. No
2685 * FIXME: Returning random values racing a window size set is wrong
2686 * should lock here against that
2689 static int tiocgwinsz(struct tty_struct *tty, struct winsize __user * arg)
2691 if (copy_to_user(arg, &tty->winsize, sizeof(*arg)))
2697 * tiocswinsz - implement window size set ioctl
2699 * @arg: user buffer for result
2701 * Copies the user idea of the window size to the kernel. Traditionally
2702 * this is just advisory information but for the Linux console it
2703 * actually has driver level meaning and triggers a VC resize.
2706 * The console_sem is used to ensure we do not try and resize
2707 * the console twice at once.
2708 * FIXME: Two racing size sets may leave the console and kernel
2709 * parameters disagreeing. Is this exploitable ?
2710 * FIXME: Random values racing a window size get is wrong
2711 * should lock here against that
2714 static int tiocswinsz(struct tty_struct *tty, struct tty_struct *real_tty,
2715 struct winsize __user * arg)
2717 struct winsize tmp_ws;
2719 if (copy_from_user(&tmp_ws, arg, sizeof(*arg)))
2721 if (!memcmp(&tmp_ws, &tty->winsize, sizeof(*arg)))
2724 if (tty->driver->type == TTY_DRIVER_TYPE_CONSOLE) {
2727 acquire_console_sem();
2728 rc = vc_resize(tty->driver_data, tmp_ws.ws_col, tmp_ws.ws_row);
2729 release_console_sem();
2735 kill_pg(tty->pgrp, SIGWINCH, 1);
2736 if ((real_tty->pgrp != tty->pgrp) && (real_tty->pgrp > 0))
2737 kill_pg(real_tty->pgrp, SIGWINCH, 1);
2738 tty->winsize = tmp_ws;
2739 real_tty->winsize = tmp_ws;
2744 * tioccons - allow admin to move logical console
2745 * @file: the file to become console
2747 * Allow the adminstrator to move the redirected console device
2749 * Locking: uses redirect_lock to guard the redirect information
2752 static int tioccons(struct file *file)
2754 if (!capable(CAP_SYS_ADMIN))
2756 if (file->f_op->write == redirected_tty_write) {
2758 spin_lock(&redirect_lock);
2761 spin_unlock(&redirect_lock);
2766 spin_lock(&redirect_lock);
2768 spin_unlock(&redirect_lock);
2773 spin_unlock(&redirect_lock);
2778 * fionbio - non blocking ioctl
2779 * @file: file to set blocking value
2780 * @p: user parameter
2782 * Historical tty interfaces had a blocking control ioctl before
2783 * the generic functionality existed. This piece of history is preserved
2784 * in the expected tty API of posix OS's.
2786 * Locking: none, the open fle handle ensures it won't go away.
2789 static int fionbio(struct file *file, int __user *p)
2793 if (get_user(nonblock, p))
2797 file->f_flags |= O_NONBLOCK;
2799 file->f_flags &= ~O_NONBLOCK;
2804 * tiocsctty - set controlling tty
2805 * @tty: tty structure
2806 * @arg: user argument
2808 * This ioctl is used to manage job control. It permits a session
2809 * leader to set this tty as the controlling tty for the session.
2812 * Takes tasklist lock internally to walk sessions
2813 * Takes task_lock() when updating signal->tty
2815 * FIXME: tty_mutex is needed to protect signal->tty references.
2816 * FIXME: why task_lock on the signal->tty reference ??
2820 static int tiocsctty(struct tty_struct *tty, int arg)
2822 struct task_struct *p;
2824 if (current->signal->leader &&
2825 (current->signal->session == tty->session))
2828 * The process must be a session leader and
2829 * not have a controlling tty already.
2831 if (!current->signal->leader || current->signal->tty)
2833 if (tty->session > 0) {
2835 * This tty is already the controlling
2836 * tty for another session group!
2838 if ((arg == 1) && capable(CAP_SYS_ADMIN)) {
2843 read_lock(&tasklist_lock);
2844 do_each_task_pid(tty->session, PIDTYPE_SID, p) {
2845 p->signal->tty = NULL;
2846 } while_each_task_pid(tty->session, PIDTYPE_SID, p);
2847 read_unlock(&tasklist_lock);
2852 current->signal->tty = tty;
2853 task_unlock(current);
2854 current->signal->tty_old_pgrp = 0;
2855 tty->session = current->signal->session;
2856 tty->pgrp = process_group(current);
2861 * tiocgpgrp - get process group
2862 * @tty: tty passed by user
2863 * @real_tty: tty side of the tty pased by the user if a pty else the tty
2866 * Obtain the process group of the tty. If there is no process group
2869 * Locking: none. Reference to ->signal->tty is safe.
2872 static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2875 * (tty == real_tty) is a cheap way of
2876 * testing if the tty is NOT a master pty.
2878 if (tty == real_tty && current->signal->tty != real_tty)
2880 return put_user(real_tty->pgrp, p);
2884 * tiocspgrp - attempt to set process group
2885 * @tty: tty passed by user
2886 * @real_tty: tty side device matching tty passed by user
2889 * Set the process group of the tty to the session passed. Only
2890 * permitted where the tty session is our session.
2894 * FIXME: current->signal->tty referencing is unsafe.
2897 static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2900 int retval = tty_check_change(real_tty);
2906 if (!current->signal->tty ||
2907 (current->signal->tty != real_tty) ||
2908 (real_tty->session != current->signal->session))
2910 if (get_user(pgrp, p))
2914 if (session_of_pgrp(pgrp) != current->signal->session)
2916 real_tty->pgrp = pgrp;
2921 * tiocgsid - get session id
2922 * @tty: tty passed by user
2923 * @real_tty: tty side of the tty pased by the user if a pty else the tty
2924 * @p: pointer to returned session id
2926 * Obtain the session id of the tty. If there is no session
2929 * Locking: none. Reference to ->signal->tty is safe.
2932 static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2935 * (tty == real_tty) is a cheap way of
2936 * testing if the tty is NOT a master pty.
2938 if (tty == real_tty && current->signal->tty != real_tty)
2940 if (real_tty->session <= 0)
2942 return put_user(real_tty->session, p);
2946 * tiocsetd - set line discipline
2948 * @p: pointer to user data
2950 * Set the line discipline according to user request.
2952 * Locking: see tty_set_ldisc, this function is just a helper
2955 static int tiocsetd(struct tty_struct *tty, int __user *p)
2959 if (get_user(ldisc, p))
2961 return tty_set_ldisc(tty, ldisc);
2965 * send_break - performed time break
2966 * @tty: device to break on
2967 * @duration: timeout in mS
2969 * Perform a timed break on hardware that lacks its own driver level
2970 * timed break functionality.
2976 * What if two overlap
2979 static int send_break(struct tty_struct *tty, unsigned int duration)
2981 tty->driver->break_ctl(tty, -1);
2982 if (!signal_pending(current)) {
2983 msleep_interruptible(duration);
2985 tty->driver->break_ctl(tty, 0);
2986 if (signal_pending(current))
2992 * tiocmget - get modem status
2994 * @file: user file pointer
2995 * @p: pointer to result
2997 * Obtain the modem status bits from the tty driver if the feature
2998 * is supported. Return -EINVAL if it is not available.
3000 * Locking: none (up to the driver)
3003 static int tty_tiocmget(struct tty_struct *tty, struct file *file, int __user *p)
3005 int retval = -EINVAL;
3007 if (tty->driver->tiocmget) {
3008 retval = tty->driver->tiocmget(tty, file);
3011 retval = put_user(retval, p);
3017 * tiocmset - set modem status
3019 * @file: user file pointer
3020 * @cmd: command - clear bits, set bits or set all
3021 * @p: pointer to desired bits
3023 * Set the modem status bits from the tty driver if the feature
3024 * is supported. Return -EINVAL if it is not available.
3026 * Locking: none (up to the driver)
3029 static int tty_tiocmset(struct tty_struct *tty, struct file *file, unsigned int cmd,
3032 int retval = -EINVAL;
3034 if (tty->driver->tiocmset) {
3035 unsigned int set, clear, val;
3037 retval = get_user(val, p);
3055 set &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
3056 clear &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
3058 retval = tty->driver->tiocmset(tty, file, set, clear);
3064 * Split this up, as gcc can choke on it otherwise..
3066 int tty_ioctl(struct inode * inode, struct file * file,
3067 unsigned int cmd, unsigned long arg)
3069 struct tty_struct *tty, *real_tty;
3070 void __user *p = (void __user *)arg;
3072 struct tty_ldisc *ld;
3074 tty = (struct tty_struct *)file->private_data;
3075 if (tty_paranoia_check(tty, inode, "tty_ioctl"))
3079 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
3080 tty->driver->subtype == PTY_TYPE_MASTER)
3081 real_tty = tty->link;
3084 * Break handling by driver
3086 if (!tty->driver->break_ctl) {
3090 if (tty->driver->ioctl)
3091 return tty->driver->ioctl(tty, file, cmd, arg);
3094 /* These two ioctl's always return success; even if */
3095 /* the driver doesn't support them. */
3098 if (!tty->driver->ioctl)
3100 retval = tty->driver->ioctl(tty, file, cmd, arg);
3101 if (retval == -ENOIOCTLCMD)
3108 * Factor out some common prep work
3116 retval = tty_check_change(tty);
3119 if (cmd != TIOCCBRK) {
3120 tty_wait_until_sent(tty, 0);
3121 if (signal_pending(current))
3129 return tiocsti(tty, p);
3131 return tiocgwinsz(tty, p);
3133 return tiocswinsz(tty, real_tty, p);
3135 return real_tty!=tty ? -EINVAL : tioccons(file);
3137 return fionbio(file, p);
3139 set_bit(TTY_EXCLUSIVE, &tty->flags);
3142 clear_bit(TTY_EXCLUSIVE, &tty->flags);
3145 /* FIXME: taks lock or tty_mutex ? */
3146 if (current->signal->tty != tty)
3148 if (current->signal->leader)
3149 disassociate_ctty(0);
3151 current->signal->tty = NULL;
3152 task_unlock(current);
3155 return tiocsctty(tty, arg);
3157 return tiocgpgrp(tty, real_tty, p);
3159 return tiocspgrp(tty, real_tty, p);
3161 return tiocgsid(tty, real_tty, p);
3163 /* FIXME: check this is ok */
3164 return put_user(tty->ldisc.num, (int __user *)p);
3166 return tiocsetd(tty, p);
3169 return tioclinux(tty, arg);
3174 case TIOCSBRK: /* Turn break on, unconditionally */
3175 tty->driver->break_ctl(tty, -1);
3178 case TIOCCBRK: /* Turn break off, unconditionally */
3179 tty->driver->break_ctl(tty, 0);
3181 case TCSBRK: /* SVID version: non-zero arg --> no break */
3182 /* non-zero arg means wait for all output data
3183 * to be sent (performed above) but don't send break.
3184 * This is used by the tcdrain() termios function.
3187 return send_break(tty, 250);
3189 case TCSBRKP: /* support for POSIX tcsendbreak() */
3190 return send_break(tty, arg ? arg*100 : 250);
3193 return tty_tiocmget(tty, file, p);
3198 return tty_tiocmset(tty, file, cmd, p);
3200 if (tty->driver->ioctl) {
3201 retval = (tty->driver->ioctl)(tty, file, cmd, arg);
3202 if (retval != -ENOIOCTLCMD)
3205 ld = tty_ldisc_ref_wait(tty);
3208 retval = ld->ioctl(tty, file, cmd, arg);
3209 if (retval == -ENOIOCTLCMD)
3212 tty_ldisc_deref(ld);
3218 * This implements the "Secure Attention Key" --- the idea is to
3219 * prevent trojan horses by killing all processes associated with this
3220 * tty when the user hits the "Secure Attention Key". Required for
3221 * super-paranoid applications --- see the Orange Book for more details.
3223 * This code could be nicer; ideally it should send a HUP, wait a few
3224 * seconds, then send a INT, and then a KILL signal. But you then
3225 * have to coordinate with the init process, since all processes associated
3226 * with the current tty must be dead before the new getty is allowed
3229 * Now, if it would be correct ;-/ The current code has a nasty hole -
3230 * it doesn't catch files in flight. We may send the descriptor to ourselves
3231 * via AF_UNIX socket, close it and later fetch from socket. FIXME.
3233 * Nasty bug: do_SAK is being called in interrupt context. This can
3234 * deadlock. We punt it up to process context. AKPM - 16Mar2001
3236 static void __do_SAK(void *arg)
3241 struct tty_struct *tty = arg;
3242 struct task_struct *g, *p;
3246 struct tty_ldisc *disc;
3247 struct fdtable *fdt;
3251 session = tty->session;
3253 /* We don't want an ldisc switch during this */
3254 disc = tty_ldisc_ref(tty);
3255 if (disc && disc->flush_buffer)
3256 disc->flush_buffer(tty);
3257 tty_ldisc_deref(disc);
3259 if (tty->driver->flush_buffer)
3260 tty->driver->flush_buffer(tty);
3262 read_lock(&tasklist_lock);
3263 /* Kill the entire session */
3264 do_each_task_pid(session, PIDTYPE_SID, p) {
3265 printk(KERN_NOTICE "SAK: killed process %d"
3266 " (%s): p->signal->session==tty->session\n",
3268 send_sig(SIGKILL, p, 1);
3269 } while_each_task_pid(session, PIDTYPE_SID, p);
3270 /* Now kill any processes that happen to have the
3273 do_each_thread(g, p) {
3274 if (p->signal->tty == tty) {
3275 printk(KERN_NOTICE "SAK: killed process %d"
3276 " (%s): p->signal->session==tty->session\n",
3278 send_sig(SIGKILL, p, 1);
3284 * We don't take a ref to the file, so we must
3285 * hold ->file_lock instead.
3287 spin_lock(&p->files->file_lock);
3288 fdt = files_fdtable(p->files);
3289 for (i=0; i < fdt->max_fds; i++) {
3290 filp = fcheck_files(p->files, i);
3293 if (filp->f_op->read == tty_read &&
3294 filp->private_data == tty) {
3295 printk(KERN_NOTICE "SAK: killed process %d"
3296 " (%s): fd#%d opened to the tty\n",
3297 p->pid, p->comm, i);
3298 force_sig(SIGKILL, p);
3302 spin_unlock(&p->files->file_lock);
3305 } while_each_thread(g, p);
3306 read_unlock(&tasklist_lock);
3311 * The tq handling here is a little racy - tty->SAK_work may already be queued.
3312 * Fortunately we don't need to worry, because if ->SAK_work is already queued,
3313 * the values which we write to it will be identical to the values which it
3314 * already has. --akpm
3316 void do_SAK(struct tty_struct *tty)
3320 PREPARE_WORK(&tty->SAK_work, __do_SAK, tty);
3321 schedule_work(&tty->SAK_work);
3324 EXPORT_SYMBOL(do_SAK);
3328 * @private_: tty structure passed from work queue.
3330 * This routine is called out of the software interrupt to flush data
3331 * from the buffer chain to the line discipline.
3333 * Locking: holds tty->buf.lock to guard buffer list. Drops the lock
3334 * while invoking the line discipline receive_buf method. The
3335 * receive_buf method is single threaded for each tty instance.
3338 static void flush_to_ldisc(void *private_)
3340 struct tty_struct *tty = (struct tty_struct *) private_;
3341 unsigned long flags;
3342 struct tty_ldisc *disc;
3343 struct tty_buffer *tbuf, *head;
3345 unsigned char *flag_buf;
3347 disc = tty_ldisc_ref(tty);
3348 if (disc == NULL) /* !TTY_LDISC */
3351 spin_lock_irqsave(&tty->buf.lock, flags);
3352 head = tty->buf.head;
3354 tty->buf.head = NULL;
3356 int count = head->commit - head->read;
3358 if (head->next == NULL)
3362 tty_buffer_free(tty, tbuf);
3365 if (!tty->receive_room) {
3366 schedule_delayed_work(&tty->buf.work, 1);
3369 if (count > tty->receive_room)
3370 count = tty->receive_room;
3371 char_buf = head->char_buf_ptr + head->read;
3372 flag_buf = head->flag_buf_ptr + head->read;
3373 head->read += count;
3374 spin_unlock_irqrestore(&tty->buf.lock, flags);
3375 disc->receive_buf(tty, char_buf, flag_buf, count);
3376 spin_lock_irqsave(&tty->buf.lock, flags);
3378 tty->buf.head = head;
3380 spin_unlock_irqrestore(&tty->buf.lock, flags);
3382 tty_ldisc_deref(disc);
3386 * Routine which returns the baud rate of the tty
3388 * Note that the baud_table needs to be kept in sync with the
3389 * include/asm/termbits.h file.
3391 static int baud_table[] = {
3392 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
3393 9600, 19200, 38400, 57600, 115200, 230400, 460800,
3395 76800, 153600, 307200, 614400, 921600
3397 500000, 576000, 921600, 1000000, 1152000, 1500000, 2000000,
3398 2500000, 3000000, 3500000, 4000000
3402 static int n_baud_table = ARRAY_SIZE(baud_table);
3405 * tty_termios_baud_rate
3406 * @termios: termios structure
3408 * Convert termios baud rate data into a speed. This should be called
3409 * with the termios lock held if this termios is a terminal termios
3410 * structure. May change the termios data.
3415 int tty_termios_baud_rate(struct termios *termios)
3419 cbaud = termios->c_cflag & CBAUD;
3421 if (cbaud & CBAUDEX) {
3424 if (cbaud < 1 || cbaud + 15 > n_baud_table)
3425 termios->c_cflag &= ~CBAUDEX;
3429 return baud_table[cbaud];
3432 EXPORT_SYMBOL(tty_termios_baud_rate);
3435 * tty_get_baud_rate - get tty bit rates
3436 * @tty: tty to query
3438 * Returns the baud rate as an integer for this terminal. The
3439 * termios lock must be held by the caller and the terminal bit
3440 * flags may be updated.
3445 int tty_get_baud_rate(struct tty_struct *tty)
3447 int baud = tty_termios_baud_rate(tty->termios);
3449 if (baud == 38400 && tty->alt_speed) {
3451 printk(KERN_WARNING "Use of setserial/setrocket to "
3452 "set SPD_* flags is deprecated\n");
3455 baud = tty->alt_speed;
3461 EXPORT_SYMBOL(tty_get_baud_rate);
3464 * tty_flip_buffer_push - terminal
3467 * Queue a push of the terminal flip buffers to the line discipline. This
3468 * function must not be called from IRQ context if tty->low_latency is set.
3470 * In the event of the queue being busy for flipping the work will be
3471 * held off and retried later.
3473 * Locking: tty buffer lock. Driver locks in low latency mode.
3476 void tty_flip_buffer_push(struct tty_struct *tty)
3478 unsigned long flags;
3479 spin_lock_irqsave(&tty->buf.lock, flags);
3480 if (tty->buf.tail != NULL)
3481 tty->buf.tail->commit = tty->buf.tail->used;
3482 spin_unlock_irqrestore(&tty->buf.lock, flags);
3484 if (tty->low_latency)
3485 flush_to_ldisc((void *) tty);
3487 schedule_delayed_work(&tty->buf.work, 1);
3490 EXPORT_SYMBOL(tty_flip_buffer_push);
3494 * initialize_tty_struct
3495 * @tty: tty to initialize
3497 * This subroutine initializes a tty structure that has been newly
3500 * Locking: none - tty in question must not be exposed at this point
3503 static void initialize_tty_struct(struct tty_struct *tty)
3505 memset(tty, 0, sizeof(struct tty_struct));
3506 tty->magic = TTY_MAGIC;
3507 tty_ldisc_assign(tty, tty_ldisc_get(N_TTY));
3509 tty->overrun_time = jiffies;
3510 tty->buf.head = tty->buf.tail = NULL;
3511 tty_buffer_init(tty);
3512 INIT_WORK(&tty->buf.work, flush_to_ldisc, tty);
3513 init_MUTEX(&tty->buf.pty_sem);
3514 init_MUTEX(&tty->termios_sem);
3515 init_waitqueue_head(&tty->write_wait);
3516 init_waitqueue_head(&tty->read_wait);
3517 INIT_WORK(&tty->hangup_work, do_tty_hangup, tty);
3518 mutex_init(&tty->atomic_read_lock);
3519 mutex_init(&tty->atomic_write_lock);
3520 spin_lock_init(&tty->read_lock);
3521 INIT_LIST_HEAD(&tty->tty_files);
3522 INIT_WORK(&tty->SAK_work, NULL, NULL);
3526 * The default put_char routine if the driver did not define one.
3529 static void tty_default_put_char(struct tty_struct *tty, unsigned char ch)
3531 tty->driver->write(tty, &ch, 1);
3534 static struct class *tty_class;
3537 * tty_register_device - register a tty device
3538 * @driver: the tty driver that describes the tty device
3539 * @index: the index in the tty driver for this tty device
3540 * @device: a struct device that is associated with this tty device.
3541 * This field is optional, if there is no known struct device
3542 * for this tty device it can be set to NULL safely.
3544 * Returns a pointer to the class device (or ERR_PTR(-EFOO) on error).
3546 * This call is required to be made to register an individual tty device
3547 * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If
3548 * that bit is not set, this function should not be called by a tty
3554 struct class_device *tty_register_device(struct tty_driver *driver,
3555 unsigned index, struct device *device)
3558 dev_t dev = MKDEV(driver->major, driver->minor_start) + index;
3560 if (index >= driver->num) {
3561 printk(KERN_ERR "Attempt to register invalid tty line number "
3563 return ERR_PTR(-EINVAL);
3566 if (driver->type == TTY_DRIVER_TYPE_PTY)
3567 pty_line_name(driver, index, name);
3569 tty_line_name(driver, index, name);
3571 return class_device_create(tty_class, NULL, dev, device, "%s", name);
3575 * tty_unregister_device - unregister a tty device
3576 * @driver: the tty driver that describes the tty device
3577 * @index: the index in the tty driver for this tty device
3579 * If a tty device is registered with a call to tty_register_device() then
3580 * this function must be called when the tty device is gone.
3585 void tty_unregister_device(struct tty_driver *driver, unsigned index)
3587 class_device_destroy(tty_class, MKDEV(driver->major, driver->minor_start) + index);
3590 EXPORT_SYMBOL(tty_register_device);
3591 EXPORT_SYMBOL(tty_unregister_device);
3593 struct tty_driver *alloc_tty_driver(int lines)
3595 struct tty_driver *driver;
3597 driver = kmalloc(sizeof(struct tty_driver), GFP_KERNEL);
3599 memset(driver, 0, sizeof(struct tty_driver));
3600 driver->magic = TTY_DRIVER_MAGIC;
3601 driver->num = lines;
3602 /* later we'll move allocation of tables here */
3607 void put_tty_driver(struct tty_driver *driver)
3612 void tty_set_operations(struct tty_driver *driver, struct tty_operations *op)
3614 driver->open = op->open;
3615 driver->close = op->close;
3616 driver->write = op->write;
3617 driver->put_char = op->put_char;
3618 driver->flush_chars = op->flush_chars;
3619 driver->write_room = op->write_room;
3620 driver->chars_in_buffer = op->chars_in_buffer;
3621 driver->ioctl = op->ioctl;
3622 driver->set_termios = op->set_termios;
3623 driver->throttle = op->throttle;
3624 driver->unthrottle = op->unthrottle;
3625 driver->stop = op->stop;
3626 driver->start = op->start;
3627 driver->hangup = op->hangup;
3628 driver->break_ctl = op->break_ctl;
3629 driver->flush_buffer = op->flush_buffer;
3630 driver->set_ldisc = op->set_ldisc;
3631 driver->wait_until_sent = op->wait_until_sent;
3632 driver->send_xchar = op->send_xchar;
3633 driver->read_proc = op->read_proc;
3634 driver->write_proc = op->write_proc;
3635 driver->tiocmget = op->tiocmget;
3636 driver->tiocmset = op->tiocmset;
3640 EXPORT_SYMBOL(alloc_tty_driver);
3641 EXPORT_SYMBOL(put_tty_driver);
3642 EXPORT_SYMBOL(tty_set_operations);
3645 * Called by a tty driver to register itself.
3647 int tty_register_driver(struct tty_driver *driver)
3654 if (driver->flags & TTY_DRIVER_INSTALLED)
3657 if (!(driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
3658 p = kmalloc(driver->num * 3 * sizeof(void *), GFP_KERNEL);
3661 memset(p, 0, driver->num * 3 * sizeof(void *));
3664 if (!driver->major) {
3665 error = alloc_chrdev_region(&dev, driver->minor_start, driver->num,
3666 (char*)driver->name);
3668 driver->major = MAJOR(dev);
3669 driver->minor_start = MINOR(dev);
3672 dev = MKDEV(driver->major, driver->minor_start);
3673 error = register_chrdev_region(dev, driver->num,
3674 (char*)driver->name);
3682 driver->ttys = (struct tty_struct **)p;
3683 driver->termios = (struct termios **)(p + driver->num);
3684 driver->termios_locked = (struct termios **)(p + driver->num * 2);
3686 driver->ttys = NULL;
3687 driver->termios = NULL;
3688 driver->termios_locked = NULL;
3691 cdev_init(&driver->cdev, &tty_fops);
3692 driver->cdev.owner = driver->owner;
3693 error = cdev_add(&driver->cdev, dev, driver->num);
3695 unregister_chrdev_region(dev, driver->num);
3696 driver->ttys = NULL;
3697 driver->termios = driver->termios_locked = NULL;
3702 if (!driver->put_char)
3703 driver->put_char = tty_default_put_char;
3705 list_add(&driver->tty_drivers, &tty_drivers);
3707 if ( !(driver->flags & TTY_DRIVER_DYNAMIC_DEV) ) {
3708 for(i = 0; i < driver->num; i++)
3709 tty_register_device(driver, i, NULL);
3711 proc_tty_register_driver(driver);
3715 EXPORT_SYMBOL(tty_register_driver);
3718 * Called by a tty driver to unregister itself.
3720 int tty_unregister_driver(struct tty_driver *driver)
3726 if (driver->refcount)
3729 unregister_chrdev_region(MKDEV(driver->major, driver->minor_start),
3732 list_del(&driver->tty_drivers);
3735 * Free the termios and termios_locked structures because
3736 * we don't want to get memory leaks when modular tty
3737 * drivers are removed from the kernel.
3739 for (i = 0; i < driver->num; i++) {
3740 tp = driver->termios[i];
3742 driver->termios[i] = NULL;
3745 tp = driver->termios_locked[i];
3747 driver->termios_locked[i] = NULL;
3750 if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV))
3751 tty_unregister_device(driver, i);
3754 proc_tty_unregister_driver(driver);
3755 driver->ttys = NULL;
3756 driver->termios = driver->termios_locked = NULL;
3758 cdev_del(&driver->cdev);
3762 EXPORT_SYMBOL(tty_unregister_driver);
3766 * Initialize the console device. This is called *early*, so
3767 * we can't necessarily depend on lots of kernel help here.
3768 * Just do some early initializations, and do the complex setup
3771 void __init console_init(void)
3775 /* Setup the default TTY line discipline. */
3776 (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
3779 * set up the console device so that later boot sequences can
3780 * inform about problems etc..
3782 #ifdef CONFIG_EARLY_PRINTK
3783 disable_early_printk();
3785 call = __con_initcall_start;
3786 while (call < __con_initcall_end) {
3793 extern int vty_init(void);
3796 static int __init tty_class_init(void)
3798 tty_class = class_create(THIS_MODULE, "tty");
3799 if (IS_ERR(tty_class))
3800 return PTR_ERR(tty_class);
3804 postcore_initcall(tty_class_init);
3806 /* 3/2004 jmc: why do these devices exist? */
3808 static struct cdev tty_cdev, console_cdev;
3809 #ifdef CONFIG_UNIX98_PTYS
3810 static struct cdev ptmx_cdev;
3813 static struct cdev vc0_cdev;
3817 * Ok, now we can initialize the rest of the tty devices and can count
3818 * on memory allocations, interrupts etc..
3820 static int __init tty_init(void)
3822 cdev_init(&tty_cdev, &tty_fops);
3823 if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) ||
3824 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0)
3825 panic("Couldn't register /dev/tty driver\n");
3826 class_device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), NULL, "tty");
3828 cdev_init(&console_cdev, &console_fops);
3829 if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) ||
3830 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0)
3831 panic("Couldn't register /dev/console driver\n");
3832 class_device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 1), NULL, "console");
3834 #ifdef CONFIG_UNIX98_PTYS
3835 cdev_init(&ptmx_cdev, &ptmx_fops);
3836 if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) ||
3837 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0)
3838 panic("Couldn't register /dev/ptmx driver\n");
3839 class_device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), NULL, "ptmx");
3843 cdev_init(&vc0_cdev, &console_fops);
3844 if (cdev_add(&vc0_cdev, MKDEV(TTY_MAJOR, 0), 1) ||
3845 register_chrdev_region(MKDEV(TTY_MAJOR, 0), 1, "/dev/vc/0") < 0)
3846 panic("Couldn't register /dev/tty0 driver\n");
3847 class_device_create(tty_class, NULL, MKDEV(TTY_MAJOR, 0), NULL, "tty0");
3853 module_init(tty_init);