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
280 * tty_buffer_free_all - free buffers used by a tty
281 * @tty: tty to free from
283 * Remove all the buffers pending on a tty whether queued with data
284 * or in the free ring. Must be called when the tty is no longer in use
289 static void tty_buffer_free_all(struct tty_struct *tty)
291 struct tty_buffer *thead;
292 while((thead = tty->buf.head) != NULL) {
293 tty->buf.head = thead->next;
296 while((thead = tty->buf.free) != NULL) {
297 tty->buf.free = thead->next;
300 tty->buf.tail = NULL;
301 tty->buf.memory_used = 0;
305 * tty_buffer_init - prepare a tty buffer structure
306 * @tty: tty to initialise
308 * Set up the initial state of the buffer management for a tty device.
309 * Must be called before the other tty buffer functions are used.
314 static void tty_buffer_init(struct tty_struct *tty)
316 spin_lock_init(&tty->buf.lock);
317 tty->buf.head = NULL;
318 tty->buf.tail = NULL;
319 tty->buf.free = NULL;
320 tty->buf.memory_used = 0;
324 * tty_buffer_alloc - allocate a tty buffer
326 * @size: desired size (characters)
328 * Allocate a new tty buffer to hold the desired number of characters.
329 * Return NULL if out of memory or the allocation would exceed the
332 * Locking: Caller must hold tty->buf.lock
335 static struct tty_buffer *tty_buffer_alloc(struct tty_struct *tty, size_t size)
337 struct tty_buffer *p;
339 if (tty->buf.memory_used + size > 65536)
341 p = kmalloc(sizeof(struct tty_buffer) + 2 * size, GFP_ATOMIC);
349 p->char_buf_ptr = (char *)(p->data);
350 p->flag_buf_ptr = (unsigned char *)p->char_buf_ptr + size;
351 tty->buf.memory_used += size;
356 * tty_buffer_free - free a tty buffer
357 * @tty: tty owning the buffer
358 * @b: the buffer to free
360 * Free a tty buffer, or add it to the free list according to our
363 * Locking: Caller must hold tty->buf.lock
366 static void tty_buffer_free(struct tty_struct *tty, struct tty_buffer *b)
368 /* Dumb strategy for now - should keep some stats */
369 tty->buf.memory_used -= b->size;
370 WARN_ON(tty->buf.memory_used < 0);
375 b->next = tty->buf.free;
381 * tty_buffer_find - find a free tty buffer
382 * @tty: tty owning the buffer
383 * @size: characters wanted
385 * Locate an existing suitable tty buffer or if we are lacking one then
386 * allocate a new one. We round our buffers off in 256 character chunks
387 * to get better allocation behaviour.
389 * Locking: Caller must hold tty->buf.lock
392 static struct tty_buffer *tty_buffer_find(struct tty_struct *tty, size_t size)
394 struct tty_buffer **tbh = &tty->buf.free;
395 while((*tbh) != NULL) {
396 struct tty_buffer *t = *tbh;
397 if(t->size >= size) {
403 tty->buf.memory_used += t->size;
406 tbh = &((*tbh)->next);
408 /* Round the buffer size out */
409 size = (size + 0xFF) & ~ 0xFF;
410 return tty_buffer_alloc(tty, size);
411 /* Should possibly check if this fails for the largest buffer we
412 have queued and recycle that ? */
416 * tty_buffer_request_room - grow tty buffer if needed
417 * @tty: tty structure
418 * @size: size desired
420 * Make at least size bytes of linear space available for the tty
421 * buffer. If we fail return the size we managed to find.
423 * Locking: Takes tty->buf.lock
425 int tty_buffer_request_room(struct tty_struct *tty, size_t size)
427 struct tty_buffer *b, *n;
431 spin_lock_irqsave(&tty->buf.lock, flags);
433 /* OPTIMISATION: We could keep a per tty "zero" sized buffer to
434 remove this conditional if its worth it. This would be invisible
436 if ((b = tty->buf.tail) != NULL)
437 left = b->size - b->used;
442 /* This is the slow path - looking for new buffers to use */
443 if ((n = tty_buffer_find(tty, size)) != NULL) {
454 spin_unlock_irqrestore(&tty->buf.lock, flags);
457 EXPORT_SYMBOL_GPL(tty_buffer_request_room);
460 * tty_insert_flip_string - Add characters to the tty buffer
461 * @tty: tty structure
465 * Queue a series of bytes to the tty buffering. All the characters
466 * passed are marked as without error. Returns the number added.
468 * Locking: Called functions may take tty->buf.lock
471 int tty_insert_flip_string(struct tty_struct *tty, const unsigned char *chars,
476 int space = tty_buffer_request_room(tty, size - copied);
477 struct tty_buffer *tb = tty->buf.tail;
478 /* If there is no space then tb may be NULL */
479 if(unlikely(space == 0))
481 memcpy(tb->char_buf_ptr + tb->used, chars, space);
482 memset(tb->flag_buf_ptr + tb->used, TTY_NORMAL, space);
486 /* There is a small chance that we need to split the data over
487 several buffers. If this is the case we must loop */
488 } while (unlikely(size > copied));
491 EXPORT_SYMBOL(tty_insert_flip_string);
494 * tty_insert_flip_string_flags - Add characters to the tty buffer
495 * @tty: tty structure
500 * Queue a series of bytes to the tty buffering. For each character
501 * the flags array indicates the status of the character. Returns the
504 * Locking: Called functions may take tty->buf.lock
507 int tty_insert_flip_string_flags(struct tty_struct *tty,
508 const unsigned char *chars, const char *flags, size_t size)
512 int space = tty_buffer_request_room(tty, size - copied);
513 struct tty_buffer *tb = tty->buf.tail;
514 /* If there is no space then tb may be NULL */
515 if(unlikely(space == 0))
517 memcpy(tb->char_buf_ptr + tb->used, chars, space);
518 memcpy(tb->flag_buf_ptr + tb->used, flags, space);
523 /* There is a small chance that we need to split the data over
524 several buffers. If this is the case we must loop */
525 } while (unlikely(size > copied));
528 EXPORT_SYMBOL(tty_insert_flip_string_flags);
531 * tty_schedule_flip - push characters to ldisc
532 * @tty: tty to push from
534 * Takes any pending buffers and transfers their ownership to the
535 * ldisc side of the queue. It then schedules those characters for
536 * processing by the line discipline.
538 * Locking: Takes tty->buf.lock
541 void tty_schedule_flip(struct tty_struct *tty)
544 spin_lock_irqsave(&tty->buf.lock, flags);
545 if (tty->buf.tail != NULL)
546 tty->buf.tail->commit = tty->buf.tail->used;
547 spin_unlock_irqrestore(&tty->buf.lock, flags);
548 schedule_delayed_work(&tty->buf.work, 1);
550 EXPORT_SYMBOL(tty_schedule_flip);
553 * tty_prepare_flip_string - make room for characters
555 * @chars: return pointer for character write area
556 * @size: desired size
558 * Prepare a block of space in the buffer for data. Returns the length
559 * available and buffer pointer to the space which is now allocated and
560 * accounted for as ready for normal characters. This is used for drivers
561 * that need their own block copy routines into the buffer. There is no
562 * guarantee the buffer is a DMA target!
564 * Locking: May call functions taking tty->buf.lock
567 int tty_prepare_flip_string(struct tty_struct *tty, unsigned char **chars, size_t size)
569 int space = tty_buffer_request_room(tty, size);
571 struct tty_buffer *tb = tty->buf.tail;
572 *chars = tb->char_buf_ptr + tb->used;
573 memset(tb->flag_buf_ptr + tb->used, TTY_NORMAL, space);
579 EXPORT_SYMBOL_GPL(tty_prepare_flip_string);
582 * tty_prepare_flip_string_flags - make room for characters
584 * @chars: return pointer for character write area
585 * @flags: return pointer for status flag write area
586 * @size: desired size
588 * Prepare a block of space in the buffer for data. Returns the length
589 * available and buffer pointer to the space which is now allocated and
590 * accounted for as ready for characters. This is used for drivers
591 * that need their own block copy routines into the buffer. There is no
592 * guarantee the buffer is a DMA target!
594 * Locking: May call functions taking tty->buf.lock
597 int tty_prepare_flip_string_flags(struct tty_struct *tty, unsigned char **chars, char **flags, size_t size)
599 int space = tty_buffer_request_room(tty, size);
601 struct tty_buffer *tb = tty->buf.tail;
602 *chars = tb->char_buf_ptr + tb->used;
603 *flags = tb->flag_buf_ptr + tb->used;
609 EXPORT_SYMBOL_GPL(tty_prepare_flip_string_flags);
614 * tty_set_termios_ldisc - set ldisc field
615 * @tty: tty structure
616 * @num: line discipline number
618 * This is probably overkill for real world processors but
619 * they are not on hot paths so a little discipline won't do
622 * Locking: takes termios_sem
625 static void tty_set_termios_ldisc(struct tty_struct *tty, int num)
627 down(&tty->termios_sem);
628 tty->termios->c_line = num;
629 up(&tty->termios_sem);
633 * This guards the refcounted line discipline lists. The lock
634 * must be taken with irqs off because there are hangup path
635 * callers who will do ldisc lookups and cannot sleep.
638 static DEFINE_SPINLOCK(tty_ldisc_lock);
639 static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_wait);
640 static struct tty_ldisc tty_ldiscs[NR_LDISCS]; /* line disc dispatch table */
643 * tty_register_ldisc - install a line discipline
644 * @disc: ldisc number
645 * @new_ldisc: pointer to the ldisc object
647 * Installs a new line discipline into the kernel. The discipline
648 * is set up as unreferenced and then made available to the kernel
649 * from this point onwards.
652 * takes tty_ldisc_lock to guard against ldisc races
655 int tty_register_ldisc(int disc, struct tty_ldisc *new_ldisc)
660 if (disc < N_TTY || disc >= NR_LDISCS)
663 spin_lock_irqsave(&tty_ldisc_lock, flags);
664 tty_ldiscs[disc] = *new_ldisc;
665 tty_ldiscs[disc].num = disc;
666 tty_ldiscs[disc].flags |= LDISC_FLAG_DEFINED;
667 tty_ldiscs[disc].refcount = 0;
668 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
672 EXPORT_SYMBOL(tty_register_ldisc);
675 * tty_unregister_ldisc - unload a line discipline
676 * @disc: ldisc number
677 * @new_ldisc: pointer to the ldisc object
679 * Remove a line discipline from the kernel providing it is not
683 * takes tty_ldisc_lock to guard against ldisc races
686 int tty_unregister_ldisc(int disc)
691 if (disc < N_TTY || disc >= NR_LDISCS)
694 spin_lock_irqsave(&tty_ldisc_lock, flags);
695 if (tty_ldiscs[disc].refcount)
698 tty_ldiscs[disc].flags &= ~LDISC_FLAG_DEFINED;
699 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
703 EXPORT_SYMBOL(tty_unregister_ldisc);
706 * tty_ldisc_get - take a reference to an ldisc
707 * @disc: ldisc number
709 * Takes a reference to a line discipline. Deals with refcounts and
710 * module locking counts. Returns NULL if the discipline is not available.
711 * Returns a pointer to the discipline and bumps the ref count if it is
715 * takes tty_ldisc_lock to guard against ldisc races
718 struct tty_ldisc *tty_ldisc_get(int disc)
721 struct tty_ldisc *ld;
723 if (disc < N_TTY || disc >= NR_LDISCS)
726 spin_lock_irqsave(&tty_ldisc_lock, flags);
728 ld = &tty_ldiscs[disc];
729 /* Check the entry is defined */
730 if(ld->flags & LDISC_FLAG_DEFINED)
732 /* If the module is being unloaded we can't use it */
733 if (!try_module_get(ld->owner))
740 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
744 EXPORT_SYMBOL_GPL(tty_ldisc_get);
747 * tty_ldisc_put - drop ldisc reference
748 * @disc: ldisc number
750 * Drop a reference to a line discipline. Manage refcounts and
751 * module usage counts
754 * takes tty_ldisc_lock to guard against ldisc races
757 void tty_ldisc_put(int disc)
759 struct tty_ldisc *ld;
762 BUG_ON(disc < N_TTY || disc >= NR_LDISCS);
764 spin_lock_irqsave(&tty_ldisc_lock, flags);
765 ld = &tty_ldiscs[disc];
766 BUG_ON(ld->refcount == 0);
768 module_put(ld->owner);
769 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
772 EXPORT_SYMBOL_GPL(tty_ldisc_put);
775 * tty_ldisc_assign - set ldisc on a tty
776 * @tty: tty to assign
777 * @ld: line discipline
779 * Install an instance of a line discipline into a tty structure. The
780 * ldisc must have a reference count above zero to ensure it remains/
781 * The tty instance refcount starts at zero.
784 * Caller must hold references
787 static void tty_ldisc_assign(struct tty_struct *tty, struct tty_ldisc *ld)
790 tty->ldisc.refcount = 0;
794 * tty_ldisc_try - internal helper
797 * Make a single attempt to grab and bump the refcount on
798 * the tty ldisc. Return 0 on failure or 1 on success. This is
799 * used to implement both the waiting and non waiting versions
802 * Locking: takes tty_ldisc_lock
805 static int tty_ldisc_try(struct tty_struct *tty)
808 struct tty_ldisc *ld;
811 spin_lock_irqsave(&tty_ldisc_lock, flags);
813 if(test_bit(TTY_LDISC, &tty->flags))
818 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
823 * tty_ldisc_ref_wait - wait for the tty ldisc
826 * Dereference the line discipline for the terminal and take a
827 * reference to it. If the line discipline is in flux then
828 * wait patiently until it changes.
830 * Note: Must not be called from an IRQ/timer context. The caller
831 * must also be careful not to hold other locks that will deadlock
832 * against a discipline change, such as an existing ldisc reference
833 * (which we check for)
835 * Locking: call functions take tty_ldisc_lock
838 struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
840 /* wait_event is a macro */
841 wait_event(tty_ldisc_wait, tty_ldisc_try(tty));
842 if(tty->ldisc.refcount == 0)
843 printk(KERN_ERR "tty_ldisc_ref_wait\n");
847 EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
850 * tty_ldisc_ref - get the tty ldisc
853 * Dereference the line discipline for the terminal and take a
854 * reference to it. If the line discipline is in flux then
855 * return NULL. Can be called from IRQ and timer functions.
857 * Locking: called functions take tty_ldisc_lock
860 struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
862 if(tty_ldisc_try(tty))
867 EXPORT_SYMBOL_GPL(tty_ldisc_ref);
870 * tty_ldisc_deref - free a tty ldisc reference
871 * @ld: reference to free up
873 * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May
874 * be called in IRQ context.
876 * Locking: takes tty_ldisc_lock
879 void tty_ldisc_deref(struct tty_ldisc *ld)
885 spin_lock_irqsave(&tty_ldisc_lock, flags);
886 if(ld->refcount == 0)
887 printk(KERN_ERR "tty_ldisc_deref: no references.\n");
890 if(ld->refcount == 0)
891 wake_up(&tty_ldisc_wait);
892 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
895 EXPORT_SYMBOL_GPL(tty_ldisc_deref);
898 * tty_ldisc_enable - allow ldisc use
899 * @tty: terminal to activate ldisc on
901 * Set the TTY_LDISC flag when the line discipline can be called
902 * again. Do neccessary wakeups for existing sleepers.
904 * Note: nobody should set this bit except via this function. Clearing
905 * directly is allowed.
908 static void tty_ldisc_enable(struct tty_struct *tty)
910 set_bit(TTY_LDISC, &tty->flags);
911 wake_up(&tty_ldisc_wait);
915 * tty_set_ldisc - set line discipline
916 * @tty: the terminal to set
917 * @ldisc: the line discipline
919 * Set the discipline of a tty line. Must be called from a process
922 * Locking: takes tty_ldisc_lock.
923 * called functions take termios_sem
926 static int tty_set_ldisc(struct tty_struct *tty, int ldisc)
929 struct tty_ldisc o_ldisc;
933 struct tty_ldisc *ld;
934 struct tty_struct *o_tty;
936 if ((ldisc < N_TTY) || (ldisc >= NR_LDISCS))
941 ld = tty_ldisc_get(ldisc);
942 /* Eduardo Blanco <ejbs@cs.cs.com.uy> */
943 /* Cyrus Durgin <cider@speakeasy.org> */
945 request_module("tty-ldisc-%d", ldisc);
946 ld = tty_ldisc_get(ldisc);
952 * No more input please, we are switching. The new ldisc
953 * will update this value in the ldisc open function
956 tty->receive_room = 0;
959 * Problem: What do we do if this blocks ?
962 tty_wait_until_sent(tty, 0);
964 if (tty->ldisc.num == ldisc) {
965 tty_ldisc_put(ldisc);
969 o_ldisc = tty->ldisc;
973 * Make sure we don't change while someone holds a
974 * reference to the line discipline. The TTY_LDISC bit
975 * prevents anyone taking a reference once it is clear.
976 * We need the lock to avoid racing reference takers.
979 spin_lock_irqsave(&tty_ldisc_lock, flags);
980 if (tty->ldisc.refcount || (o_tty && o_tty->ldisc.refcount)) {
981 if(tty->ldisc.refcount) {
982 /* Free the new ldisc we grabbed. Must drop the lock
984 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
985 tty_ldisc_put(ldisc);
987 * There are several reasons we may be busy, including
988 * random momentary I/O traffic. We must therefore
989 * retry. We could distinguish between blocking ops
990 * and retries if we made tty_ldisc_wait() smarter. That
991 * is up for discussion.
993 if (wait_event_interruptible(tty_ldisc_wait, tty->ldisc.refcount == 0) < 0)
997 if(o_tty && o_tty->ldisc.refcount) {
998 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
999 tty_ldisc_put(ldisc);
1000 if (wait_event_interruptible(tty_ldisc_wait, o_tty->ldisc.refcount == 0) < 0)
1001 return -ERESTARTSYS;
1006 /* if the TTY_LDISC bit is set, then we are racing against another ldisc change */
1008 if (!test_bit(TTY_LDISC, &tty->flags)) {
1009 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
1010 tty_ldisc_put(ldisc);
1011 ld = tty_ldisc_ref_wait(tty);
1012 tty_ldisc_deref(ld);
1016 clear_bit(TTY_LDISC, &tty->flags);
1018 clear_bit(TTY_LDISC, &o_tty->flags);
1019 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
1022 * From this point on we know nobody has an ldisc
1023 * usage reference, nor can they obtain one until
1024 * we say so later on.
1027 work = cancel_delayed_work(&tty->buf.work);
1029 * Wait for ->hangup_work and ->buf.work handlers to terminate
1032 flush_scheduled_work();
1033 /* Shutdown the current discipline. */
1034 if (tty->ldisc.close)
1035 (tty->ldisc.close)(tty);
1037 /* Now set up the new line discipline. */
1038 tty_ldisc_assign(tty, ld);
1039 tty_set_termios_ldisc(tty, ldisc);
1040 if (tty->ldisc.open)
1041 retval = (tty->ldisc.open)(tty);
1043 tty_ldisc_put(ldisc);
1044 /* There is an outstanding reference here so this is safe */
1045 tty_ldisc_assign(tty, tty_ldisc_get(o_ldisc.num));
1046 tty_set_termios_ldisc(tty, tty->ldisc.num);
1047 if (tty->ldisc.open && (tty->ldisc.open(tty) < 0)) {
1048 tty_ldisc_put(o_ldisc.num);
1049 /* This driver is always present */
1050 tty_ldisc_assign(tty, tty_ldisc_get(N_TTY));
1051 tty_set_termios_ldisc(tty, N_TTY);
1052 if (tty->ldisc.open) {
1053 int r = tty->ldisc.open(tty);
1056 panic("Couldn't open N_TTY ldisc for "
1058 tty_name(tty, buf), r);
1062 /* At this point we hold a reference to the new ldisc and a
1063 a reference to the old ldisc. If we ended up flipping back
1064 to the existing ldisc we have two references to it */
1066 if (tty->ldisc.num != o_ldisc.num && tty->driver->set_ldisc)
1067 tty->driver->set_ldisc(tty);
1069 tty_ldisc_put(o_ldisc.num);
1072 * Allow ldisc referencing to occur as soon as the driver
1073 * ldisc callback completes.
1076 tty_ldisc_enable(tty);
1078 tty_ldisc_enable(o_tty);
1080 /* Restart it in case no characters kick it off. Safe if
1083 schedule_delayed_work(&tty->buf.work, 1);
1088 * get_tty_driver - find device of a tty
1089 * @dev_t: device identifier
1090 * @index: returns the index of the tty
1092 * This routine returns a tty driver structure, given a device number
1093 * and also passes back the index number.
1095 * Locking: caller must hold tty_mutex
1098 static struct tty_driver *get_tty_driver(dev_t device, int *index)
1100 struct tty_driver *p;
1102 list_for_each_entry(p, &tty_drivers, tty_drivers) {
1103 dev_t base = MKDEV(p->major, p->minor_start);
1104 if (device < base || device >= base + p->num)
1106 *index = device - base;
1113 * tty_check_change - check for POSIX terminal changes
1114 * @tty: tty to check
1116 * If we try to write to, or set the state of, a terminal and we're
1117 * not in the foreground, send a SIGTTOU. If the signal is blocked or
1118 * ignored, go ahead and perform the operation. (POSIX 7.2)
1123 int tty_check_change(struct tty_struct * tty)
1125 if (current->signal->tty != tty)
1127 if (tty->pgrp <= 0) {
1128 printk(KERN_WARNING "tty_check_change: tty->pgrp <= 0!\n");
1131 if (process_group(current) == tty->pgrp)
1133 if (is_ignored(SIGTTOU))
1135 if (is_orphaned_pgrp(process_group(current)))
1137 (void) kill_pg(process_group(current), SIGTTOU, 1);
1138 return -ERESTARTSYS;
1141 EXPORT_SYMBOL(tty_check_change);
1143 static ssize_t hung_up_tty_read(struct file * file, char __user * buf,
1144 size_t count, loff_t *ppos)
1149 static ssize_t hung_up_tty_write(struct file * file, const char __user * buf,
1150 size_t count, loff_t *ppos)
1155 /* No kernel lock held - none needed ;) */
1156 static unsigned int hung_up_tty_poll(struct file * filp, poll_table * wait)
1158 return POLLIN | POLLOUT | POLLERR | POLLHUP | POLLRDNORM | POLLWRNORM;
1161 static int hung_up_tty_ioctl(struct inode * inode, struct file * file,
1162 unsigned int cmd, unsigned long arg)
1164 return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
1167 static const struct file_operations tty_fops = {
1168 .llseek = no_llseek,
1174 .release = tty_release,
1175 .fasync = tty_fasync,
1178 #ifdef CONFIG_UNIX98_PTYS
1179 static const struct file_operations ptmx_fops = {
1180 .llseek = no_llseek,
1186 .release = tty_release,
1187 .fasync = tty_fasync,
1191 static const struct file_operations console_fops = {
1192 .llseek = no_llseek,
1194 .write = redirected_tty_write,
1198 .release = tty_release,
1199 .fasync = tty_fasync,
1202 static const struct file_operations hung_up_tty_fops = {
1203 .llseek = no_llseek,
1204 .read = hung_up_tty_read,
1205 .write = hung_up_tty_write,
1206 .poll = hung_up_tty_poll,
1207 .ioctl = hung_up_tty_ioctl,
1208 .release = tty_release,
1211 static DEFINE_SPINLOCK(redirect_lock);
1212 static struct file *redirect;
1215 * tty_wakeup - request more data
1218 * Internal and external helper for wakeups of tty. This function
1219 * informs the line discipline if present that the driver is ready
1220 * to receive more output data.
1223 void tty_wakeup(struct tty_struct *tty)
1225 struct tty_ldisc *ld;
1227 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) {
1228 ld = tty_ldisc_ref(tty);
1230 if(ld->write_wakeup)
1231 ld->write_wakeup(tty);
1232 tty_ldisc_deref(ld);
1235 wake_up_interruptible(&tty->write_wait);
1238 EXPORT_SYMBOL_GPL(tty_wakeup);
1241 * tty_ldisc_flush - flush line discipline queue
1244 * Flush the line discipline queue (if any) for this tty. If there
1245 * is no line discipline active this is a no-op.
1248 void tty_ldisc_flush(struct tty_struct *tty)
1250 struct tty_ldisc *ld = tty_ldisc_ref(tty);
1252 if(ld->flush_buffer)
1253 ld->flush_buffer(tty);
1254 tty_ldisc_deref(ld);
1258 EXPORT_SYMBOL_GPL(tty_ldisc_flush);
1261 * do_tty_hangup - actual handler for hangup events
1264 * This can be called by the "eventd" kernel thread. That is process
1265 * synchronous but doesn't hold any locks, so we need to make sure we
1266 * have the appropriate locks for what we're doing.
1268 * The hangup event clears any pending redirections onto the hung up
1269 * device. It ensures future writes will error and it does the needed
1270 * line discipline hangup and signal delivery. The tty object itself
1275 * redirect lock for undoing redirection
1276 * file list lock for manipulating list of ttys
1277 * tty_ldisc_lock from called functions
1278 * termios_sem resetting termios data
1279 * tasklist_lock to walk task list for hangup event
1282 static void do_tty_hangup(void *data)
1284 struct tty_struct *tty = (struct tty_struct *) data;
1285 struct file * cons_filp = NULL;
1286 struct file *filp, *f = NULL;
1287 struct task_struct *p;
1288 struct tty_ldisc *ld;
1289 int closecount = 0, n;
1294 /* inuse_filps is protected by the single kernel lock */
1297 spin_lock(&redirect_lock);
1298 if (redirect && redirect->private_data == tty) {
1302 spin_unlock(&redirect_lock);
1304 check_tty_count(tty, "do_tty_hangup");
1306 /* This breaks for file handles being sent over AF_UNIX sockets ? */
1307 list_for_each_entry(filp, &tty->tty_files, f_u.fu_list) {
1308 if (filp->f_op->write == redirected_tty_write)
1310 if (filp->f_op->write != tty_write)
1313 tty_fasync(-1, filp, 0); /* can't block */
1314 filp->f_op = &hung_up_tty_fops;
1318 /* FIXME! What are the locking issues here? This may me overdoing things..
1319 * this question is especially important now that we've removed the irqlock. */
1321 ld = tty_ldisc_ref(tty);
1322 if(ld != NULL) /* We may have no line discipline at this point */
1324 if (ld->flush_buffer)
1325 ld->flush_buffer(tty);
1326 if (tty->driver->flush_buffer)
1327 tty->driver->flush_buffer(tty);
1328 if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
1330 ld->write_wakeup(tty);
1335 /* FIXME: Once we trust the LDISC code better we can wait here for
1336 ldisc completion and fix the driver call race */
1338 wake_up_interruptible(&tty->write_wait);
1339 wake_up_interruptible(&tty->read_wait);
1342 * Shutdown the current line discipline, and reset it to
1345 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
1347 down(&tty->termios_sem);
1348 *tty->termios = tty->driver->init_termios;
1349 up(&tty->termios_sem);
1352 /* Defer ldisc switch */
1353 /* tty_deferred_ldisc_switch(N_TTY);
1355 This should get done automatically when the port closes and
1356 tty_release is called */
1358 read_lock(&tasklist_lock);
1359 if (tty->session > 0) {
1360 do_each_task_pid(tty->session, PIDTYPE_SID, p) {
1361 if (p->signal->tty == tty)
1362 p->signal->tty = NULL;
1363 if (!p->signal->leader)
1365 group_send_sig_info(SIGHUP, SEND_SIG_PRIV, p);
1366 group_send_sig_info(SIGCONT, SEND_SIG_PRIV, p);
1368 p->signal->tty_old_pgrp = tty->pgrp;
1369 } while_each_task_pid(tty->session, PIDTYPE_SID, p);
1371 read_unlock(&tasklist_lock);
1376 tty->ctrl_status = 0;
1378 * If one of the devices matches a console pointer, we
1379 * cannot just call hangup() because that will cause
1380 * tty->count and state->count to go out of sync.
1381 * So we just call close() the right number of times.
1384 if (tty->driver->close)
1385 for (n = 0; n < closecount; n++)
1386 tty->driver->close(tty, cons_filp);
1387 } else if (tty->driver->hangup)
1388 (tty->driver->hangup)(tty);
1390 /* We don't want to have driver/ldisc interactions beyond
1391 the ones we did here. The driver layer expects no
1392 calls after ->hangup() from the ldisc side. However we
1393 can't yet guarantee all that */
1395 set_bit(TTY_HUPPED, &tty->flags);
1397 tty_ldisc_enable(tty);
1398 tty_ldisc_deref(ld);
1406 * tty_hangup - trigger a hangup event
1407 * @tty: tty to hangup
1409 * A carrier loss (virtual or otherwise) has occurred on this like
1410 * schedule a hangup sequence to run after this event.
1413 void tty_hangup(struct tty_struct * tty)
1415 #ifdef TTY_DEBUG_HANGUP
1418 printk(KERN_DEBUG "%s hangup...\n", tty_name(tty, buf));
1420 schedule_work(&tty->hangup_work);
1423 EXPORT_SYMBOL(tty_hangup);
1426 * tty_vhangup - process vhangup
1427 * @tty: tty to hangup
1429 * The user has asked via system call for the terminal to be hung up.
1430 * We do this synchronously so that when the syscall returns the process
1431 * is complete. That guarantee is neccessary for security reasons.
1434 void tty_vhangup(struct tty_struct * tty)
1436 #ifdef TTY_DEBUG_HANGUP
1439 printk(KERN_DEBUG "%s vhangup...\n", tty_name(tty, buf));
1441 do_tty_hangup((void *) tty);
1443 EXPORT_SYMBOL(tty_vhangup);
1446 * tty_hung_up_p - was tty hung up
1447 * @filp: file pointer of tty
1449 * Return true if the tty has been subject to a vhangup or a carrier
1453 int tty_hung_up_p(struct file * filp)
1455 return (filp->f_op == &hung_up_tty_fops);
1458 EXPORT_SYMBOL(tty_hung_up_p);
1461 * disassociate_ctty - disconnect controlling tty
1462 * @on_exit: true if exiting so need to "hang up" the session
1464 * This function is typically called only by the session leader, when
1465 * it wants to disassociate itself from its controlling tty.
1467 * It performs the following functions:
1468 * (1) Sends a SIGHUP and SIGCONT to the foreground process group
1469 * (2) Clears the tty from being controlling the session
1470 * (3) Clears the controlling tty for all processes in the
1473 * The argument on_exit is set to 1 if called when a process is
1474 * exiting; it is 0 if called by the ioctl TIOCNOTTY.
1476 * Locking: tty_mutex is taken to protect current->signal->tty
1477 * BKL is taken for hysterical raisins
1478 * Tasklist lock is taken (under tty_mutex) to walk process
1479 * lists for the session.
1482 void disassociate_ctty(int on_exit)
1484 struct tty_struct *tty;
1485 struct task_struct *p;
1490 mutex_lock(&tty_mutex);
1491 tty = current->signal->tty;
1493 tty_pgrp = tty->pgrp;
1494 mutex_unlock(&tty_mutex);
1495 if (on_exit && tty->driver->type != TTY_DRIVER_TYPE_PTY)
1498 if (current->signal->tty_old_pgrp) {
1499 kill_pg(current->signal->tty_old_pgrp, SIGHUP, on_exit);
1500 kill_pg(current->signal->tty_old_pgrp, SIGCONT, on_exit);
1502 mutex_unlock(&tty_mutex);
1507 kill_pg(tty_pgrp, SIGHUP, on_exit);
1509 kill_pg(tty_pgrp, SIGCONT, on_exit);
1512 /* Must lock changes to tty_old_pgrp */
1513 mutex_lock(&tty_mutex);
1514 current->signal->tty_old_pgrp = 0;
1518 /* Now clear signal->tty under the lock */
1519 read_lock(&tasklist_lock);
1520 do_each_task_pid(current->signal->session, PIDTYPE_SID, p) {
1521 p->signal->tty = NULL;
1522 } while_each_task_pid(current->signal->session, PIDTYPE_SID, p);
1523 read_unlock(&tasklist_lock);
1524 mutex_unlock(&tty_mutex);
1530 * stop_tty - propogate flow control
1533 * Perform flow control to the driver. For PTY/TTY pairs we
1534 * must also propogate the TIOCKPKT status. May be called
1535 * on an already stopped device and will not re-call the driver
1538 * This functionality is used by both the line disciplines for
1539 * halting incoming flow and by the driver. It may therefore be
1540 * called from any context, may be under the tty atomic_write_lock
1544 * Broken. Relies on BKL which is unsafe here.
1547 void stop_tty(struct tty_struct *tty)
1552 if (tty->link && tty->link->packet) {
1553 tty->ctrl_status &= ~TIOCPKT_START;
1554 tty->ctrl_status |= TIOCPKT_STOP;
1555 wake_up_interruptible(&tty->link->read_wait);
1557 if (tty->driver->stop)
1558 (tty->driver->stop)(tty);
1561 EXPORT_SYMBOL(stop_tty);
1564 * start_tty - propogate flow control
1565 * @tty: tty to start
1567 * Start a tty that has been stopped if at all possible. Perform
1568 * any neccessary wakeups and propogate the TIOCPKT status. If this
1569 * is the tty was previous stopped and is being started then the
1570 * driver start method is invoked and the line discipline woken.
1573 * Broken. Relies on BKL which is unsafe here.
1576 void start_tty(struct tty_struct *tty)
1578 if (!tty->stopped || tty->flow_stopped)
1581 if (tty->link && tty->link->packet) {
1582 tty->ctrl_status &= ~TIOCPKT_STOP;
1583 tty->ctrl_status |= TIOCPKT_START;
1584 wake_up_interruptible(&tty->link->read_wait);
1586 if (tty->driver->start)
1587 (tty->driver->start)(tty);
1589 /* If we have a running line discipline it may need kicking */
1591 wake_up_interruptible(&tty->write_wait);
1594 EXPORT_SYMBOL(start_tty);
1597 * tty_read - read method for tty device files
1598 * @file: pointer to tty file
1600 * @count: size of user buffer
1603 * Perform the read system call function on this terminal device. Checks
1604 * for hung up devices before calling the line discipline method.
1607 * Locks the line discipline internally while needed
1608 * For historical reasons the line discipline read method is
1609 * invoked under the BKL. This will go away in time so do not rely on it
1610 * in new code. Multiple read calls may be outstanding in parallel.
1613 static ssize_t tty_read(struct file * file, char __user * buf, size_t count,
1617 struct tty_struct * tty;
1618 struct inode *inode;
1619 struct tty_ldisc *ld;
1621 tty = (struct tty_struct *)file->private_data;
1622 inode = file->f_dentry->d_inode;
1623 if (tty_paranoia_check(tty, inode, "tty_read"))
1625 if (!tty || (test_bit(TTY_IO_ERROR, &tty->flags)))
1628 /* We want to wait for the line discipline to sort out in this
1630 ld = tty_ldisc_ref_wait(tty);
1633 i = (ld->read)(tty,file,buf,count);
1636 tty_ldisc_deref(ld);
1639 inode->i_atime = current_fs_time(inode->i_sb);
1644 * Split writes up in sane blocksizes to avoid
1645 * denial-of-service type attacks
1647 static inline ssize_t do_tty_write(
1648 ssize_t (*write)(struct tty_struct *, struct file *, const unsigned char *, size_t),
1649 struct tty_struct *tty,
1651 const char __user *buf,
1654 ssize_t ret = 0, written = 0;
1657 /* FIXME: O_NDELAY ... */
1658 if (mutex_lock_interruptible(&tty->atomic_write_lock)) {
1659 return -ERESTARTSYS;
1663 * We chunk up writes into a temporary buffer. This
1664 * simplifies low-level drivers immensely, since they
1665 * don't have locking issues and user mode accesses.
1667 * But if TTY_NO_WRITE_SPLIT is set, we should use a
1670 * The default chunk-size is 2kB, because the NTTY
1671 * layer has problems with bigger chunks. It will
1672 * claim to be able to handle more characters than
1675 * FIXME: This can probably go away now except that 64K chunks
1676 * are too likely to fail unless switched to vmalloc...
1679 if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags))
1684 /* write_buf/write_cnt is protected by the atomic_write_lock mutex */
1685 if (tty->write_cnt < chunk) {
1691 buf = kmalloc(chunk, GFP_KERNEL);
1693 mutex_unlock(&tty->atomic_write_lock);
1696 kfree(tty->write_buf);
1697 tty->write_cnt = chunk;
1698 tty->write_buf = buf;
1701 /* Do the write .. */
1703 size_t size = count;
1707 if (copy_from_user(tty->write_buf, buf, size))
1710 ret = write(tty, file, tty->write_buf, size);
1720 if (signal_pending(current))
1725 struct inode *inode = file->f_dentry->d_inode;
1726 inode->i_mtime = current_fs_time(inode->i_sb);
1729 mutex_unlock(&tty->atomic_write_lock);
1735 * tty_write - write method for tty device file
1736 * @file: tty file pointer
1737 * @buf: user data to write
1738 * @count: bytes to write
1741 * Write data to a tty device via the line discipline.
1744 * Locks the line discipline as required
1745 * Writes to the tty driver are serialized by the atomic_write_lock
1746 * and are then processed in chunks to the device. The line discipline
1747 * write method will not be involked in parallel for each device
1748 * The line discipline write method is called under the big
1749 * kernel lock for historical reasons. New code should not rely on this.
1752 static ssize_t tty_write(struct file * file, const char __user * buf, size_t count,
1755 struct tty_struct * tty;
1756 struct inode *inode = file->f_dentry->d_inode;
1758 struct tty_ldisc *ld;
1760 tty = (struct tty_struct *)file->private_data;
1761 if (tty_paranoia_check(tty, inode, "tty_write"))
1763 if (!tty || !tty->driver->write || (test_bit(TTY_IO_ERROR, &tty->flags)))
1766 ld = tty_ldisc_ref_wait(tty);
1770 ret = do_tty_write(ld->write, tty, file, buf, count);
1771 tty_ldisc_deref(ld);
1775 ssize_t redirected_tty_write(struct file * file, const char __user * buf, size_t count,
1778 struct file *p = NULL;
1780 spin_lock(&redirect_lock);
1785 spin_unlock(&redirect_lock);
1789 res = vfs_write(p, buf, count, &p->f_pos);
1794 return tty_write(file, buf, count, ppos);
1797 static char ptychar[] = "pqrstuvwxyzabcde";
1800 * pty_line_name - generate name for a pty
1801 * @driver: the tty driver in use
1802 * @index: the minor number
1803 * @p: output buffer of at least 6 bytes
1805 * Generate a name from a driver reference and write it to the output
1810 static void pty_line_name(struct tty_driver *driver, int index, char *p)
1812 int i = index + driver->name_base;
1813 /* ->name is initialized to "ttyp", but "tty" is expected */
1814 sprintf(p, "%s%c%x",
1815 driver->subtype == PTY_TYPE_SLAVE ? "tty" : driver->name,
1816 ptychar[i >> 4 & 0xf], i & 0xf);
1820 * pty_line_name - generate name for a tty
1821 * @driver: the tty driver in use
1822 * @index: the minor number
1823 * @p: output buffer of at least 7 bytes
1825 * Generate a name from a driver reference and write it to the output
1830 static void tty_line_name(struct tty_driver *driver, int index, char *p)
1832 sprintf(p, "%s%d", driver->name, index + driver->name_base);
1836 * init_dev - initialise a tty device
1837 * @driver: tty driver we are opening a device on
1838 * @idx: device index
1839 * @tty: returned tty structure
1841 * Prepare a tty device. This may not be a "new" clean device but
1842 * could also be an active device. The pty drivers require special
1843 * handling because of this.
1846 * The function is called under the tty_mutex, which
1847 * protects us from the tty struct or driver itself going away.
1849 * On exit the tty device has the line discipline attached and
1850 * a reference count of 1. If a pair was created for pty/tty use
1851 * and the other was a pty master then it too has a reference count of 1.
1853 * WSH 06/09/97: Rewritten to remove races and properly clean up after a
1854 * failed open. The new code protects the open with a mutex, so it's
1855 * really quite straightforward. The mutex locking can probably be
1856 * relaxed for the (most common) case of reopening a tty.
1859 static int init_dev(struct tty_driver *driver, int idx,
1860 struct tty_struct **ret_tty)
1862 struct tty_struct *tty, *o_tty;
1863 struct termios *tp, **tp_loc, *o_tp, **o_tp_loc;
1864 struct termios *ltp, **ltp_loc, *o_ltp, **o_ltp_loc;
1867 /* check whether we're reopening an existing tty */
1868 if (driver->flags & TTY_DRIVER_DEVPTS_MEM) {
1869 tty = devpts_get_tty(idx);
1870 if (tty && driver->subtype == PTY_TYPE_MASTER)
1873 tty = driver->ttys[idx];
1875 if (tty) goto fast_track;
1878 * First time open is complex, especially for PTY devices.
1879 * This code guarantees that either everything succeeds and the
1880 * TTY is ready for operation, or else the table slots are vacated
1881 * and the allocated memory released. (Except that the termios
1882 * and locked termios may be retained.)
1885 if (!try_module_get(driver->owner)) {
1894 tty = alloc_tty_struct();
1897 initialize_tty_struct(tty);
1898 tty->driver = driver;
1900 tty_line_name(driver, idx, tty->name);
1902 if (driver->flags & TTY_DRIVER_DEVPTS_MEM) {
1903 tp_loc = &tty->termios;
1904 ltp_loc = &tty->termios_locked;
1906 tp_loc = &driver->termios[idx];
1907 ltp_loc = &driver->termios_locked[idx];
1911 tp = (struct termios *) kmalloc(sizeof(struct termios),
1915 *tp = driver->init_termios;
1919 ltp = (struct termios *) kmalloc(sizeof(struct termios),
1923 memset(ltp, 0, sizeof(struct termios));
1926 if (driver->type == TTY_DRIVER_TYPE_PTY) {
1927 o_tty = alloc_tty_struct();
1930 initialize_tty_struct(o_tty);
1931 o_tty->driver = driver->other;
1933 tty_line_name(driver->other, idx, o_tty->name);
1935 if (driver->flags & TTY_DRIVER_DEVPTS_MEM) {
1936 o_tp_loc = &o_tty->termios;
1937 o_ltp_loc = &o_tty->termios_locked;
1939 o_tp_loc = &driver->other->termios[idx];
1940 o_ltp_loc = &driver->other->termios_locked[idx];
1944 o_tp = (struct termios *)
1945 kmalloc(sizeof(struct termios), GFP_KERNEL);
1948 *o_tp = driver->other->init_termios;
1952 o_ltp = (struct termios *)
1953 kmalloc(sizeof(struct termios), GFP_KERNEL);
1956 memset(o_ltp, 0, sizeof(struct termios));
1960 * Everything allocated ... set up the o_tty structure.
1962 if (!(driver->other->flags & TTY_DRIVER_DEVPTS_MEM)) {
1963 driver->other->ttys[idx] = o_tty;
1969 o_tty->termios = *o_tp_loc;
1970 o_tty->termios_locked = *o_ltp_loc;
1971 driver->other->refcount++;
1972 if (driver->subtype == PTY_TYPE_MASTER)
1975 /* Establish the links in both directions */
1981 * All structures have been allocated, so now we install them.
1982 * Failures after this point use release_mem to clean up, so
1983 * there's no need to null out the local pointers.
1985 if (!(driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
1986 driver->ttys[idx] = tty;
1993 tty->termios = *tp_loc;
1994 tty->termios_locked = *ltp_loc;
1999 * Structures all installed ... call the ldisc open routines.
2000 * If we fail here just call release_mem to clean up. No need
2001 * to decrement the use counts, as release_mem doesn't care.
2004 if (tty->ldisc.open) {
2005 retval = (tty->ldisc.open)(tty);
2007 goto release_mem_out;
2009 if (o_tty && o_tty->ldisc.open) {
2010 retval = (o_tty->ldisc.open)(o_tty);
2012 if (tty->ldisc.close)
2013 (tty->ldisc.close)(tty);
2014 goto release_mem_out;
2016 tty_ldisc_enable(o_tty);
2018 tty_ldisc_enable(tty);
2022 * This fast open can be used if the tty is already open.
2023 * No memory is allocated, and the only failures are from
2024 * attempting to open a closing tty or attempting multiple
2025 * opens on a pty master.
2028 if (test_bit(TTY_CLOSING, &tty->flags)) {
2032 if (driver->type == TTY_DRIVER_TYPE_PTY &&
2033 driver->subtype == PTY_TYPE_MASTER) {
2035 * special case for PTY masters: only one open permitted,
2036 * and the slave side open count is incremented as well.
2045 tty->driver = driver; /* N.B. why do this every time?? */
2048 if(!test_bit(TTY_LDISC, &tty->flags))
2049 printk(KERN_ERR "init_dev but no ldisc\n");
2053 /* All paths come through here to release the mutex */
2057 /* Release locally allocated memory ... nothing placed in slots */
2061 free_tty_struct(o_tty);
2064 free_tty_struct(tty);
2067 module_put(driver->owner);
2071 /* call the tty release_mem routine to clean out this slot */
2073 printk(KERN_INFO "init_dev: ldisc open failed, "
2074 "clearing slot %d\n", idx);
2075 release_mem(tty, idx);
2080 * release_mem - release tty structure memory
2082 * Releases memory associated with a tty structure, and clears out the
2083 * driver table slots. This function is called when a device is no longer
2084 * in use. It also gets called when setup of a device fails.
2087 * tty_mutex - sometimes only
2088 * takes the file list lock internally when working on the list
2089 * of ttys that the driver keeps.
2090 * FIXME: should we require tty_mutex is held here ??
2093 static void release_mem(struct tty_struct *tty, int idx)
2095 struct tty_struct *o_tty;
2097 int devpts = tty->driver->flags & TTY_DRIVER_DEVPTS_MEM;
2099 if ((o_tty = tty->link) != NULL) {
2101 o_tty->driver->ttys[idx] = NULL;
2102 if (o_tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) {
2103 tp = o_tty->termios;
2105 o_tty->driver->termios[idx] = NULL;
2108 tp = o_tty->termios_locked;
2110 o_tty->driver->termios_locked[idx] = NULL;
2114 o_tty->driver->refcount--;
2116 list_del_init(&o_tty->tty_files);
2118 free_tty_struct(o_tty);
2122 tty->driver->ttys[idx] = NULL;
2123 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) {
2126 tty->driver->termios[idx] = NULL;
2129 tp = tty->termios_locked;
2131 tty->driver->termios_locked[idx] = NULL;
2136 tty->driver->refcount--;
2138 list_del_init(&tty->tty_files);
2140 module_put(tty->driver->owner);
2141 free_tty_struct(tty);
2145 * Even releasing the tty structures is a tricky business.. We have
2146 * to be very careful that the structures are all released at the
2147 * same time, as interrupts might otherwise get the wrong pointers.
2149 * WSH 09/09/97: rewritten to avoid some nasty race conditions that could
2150 * lead to double frees or releasing memory still in use.
2152 static void release_dev(struct file * filp)
2154 struct tty_struct *tty, *o_tty;
2155 int pty_master, tty_closing, o_tty_closing, do_sleep;
2159 unsigned long flags;
2161 tty = (struct tty_struct *)filp->private_data;
2162 if (tty_paranoia_check(tty, filp->f_dentry->d_inode, "release_dev"))
2165 check_tty_count(tty, "release_dev");
2167 tty_fasync(-1, filp, 0);
2170 pty_master = (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
2171 tty->driver->subtype == PTY_TYPE_MASTER);
2172 devpts = (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM) != 0;
2175 #ifdef TTY_PARANOIA_CHECK
2176 if (idx < 0 || idx >= tty->driver->num) {
2177 printk(KERN_DEBUG "release_dev: bad idx when trying to "
2178 "free (%s)\n", tty->name);
2181 if (!(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
2182 if (tty != tty->driver->ttys[idx]) {
2183 printk(KERN_DEBUG "release_dev: driver.table[%d] not tty "
2184 "for (%s)\n", idx, tty->name);
2187 if (tty->termios != tty->driver->termios[idx]) {
2188 printk(KERN_DEBUG "release_dev: driver.termios[%d] not termios "
2193 if (tty->termios_locked != tty->driver->termios_locked[idx]) {
2194 printk(KERN_DEBUG "release_dev: driver.termios_locked[%d] not "
2195 "termios_locked for (%s)\n",
2202 #ifdef TTY_DEBUG_HANGUP
2203 printk(KERN_DEBUG "release_dev of %s (tty count=%d)...",
2204 tty_name(tty, buf), tty->count);
2207 #ifdef TTY_PARANOIA_CHECK
2208 if (tty->driver->other &&
2209 !(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
2210 if (o_tty != tty->driver->other->ttys[idx]) {
2211 printk(KERN_DEBUG "release_dev: other->table[%d] "
2212 "not o_tty for (%s)\n",
2216 if (o_tty->termios != tty->driver->other->termios[idx]) {
2217 printk(KERN_DEBUG "release_dev: other->termios[%d] "
2218 "not o_termios for (%s)\n",
2222 if (o_tty->termios_locked !=
2223 tty->driver->other->termios_locked[idx]) {
2224 printk(KERN_DEBUG "release_dev: other->termios_locked["
2225 "%d] not o_termios_locked for (%s)\n",
2229 if (o_tty->link != tty) {
2230 printk(KERN_DEBUG "release_dev: bad pty pointers\n");
2235 if (tty->driver->close)
2236 tty->driver->close(tty, filp);
2239 * Sanity check: if tty->count is going to zero, there shouldn't be
2240 * any waiters on tty->read_wait or tty->write_wait. We test the
2241 * wait queues and kick everyone out _before_ actually starting to
2242 * close. This ensures that we won't block while releasing the tty
2245 * The test for the o_tty closing is necessary, since the master and
2246 * slave sides may close in any order. If the slave side closes out
2247 * first, its count will be one, since the master side holds an open.
2248 * Thus this test wouldn't be triggered at the time the slave closes,
2251 * Note that it's possible for the tty to be opened again while we're
2252 * flushing out waiters. By recalculating the closing flags before
2253 * each iteration we avoid any problems.
2256 /* Guard against races with tty->count changes elsewhere and
2257 opens on /dev/tty */
2259 mutex_lock(&tty_mutex);
2260 tty_closing = tty->count <= 1;
2261 o_tty_closing = o_tty &&
2262 (o_tty->count <= (pty_master ? 1 : 0));
2266 if (waitqueue_active(&tty->read_wait)) {
2267 wake_up(&tty->read_wait);
2270 if (waitqueue_active(&tty->write_wait)) {
2271 wake_up(&tty->write_wait);
2275 if (o_tty_closing) {
2276 if (waitqueue_active(&o_tty->read_wait)) {
2277 wake_up(&o_tty->read_wait);
2280 if (waitqueue_active(&o_tty->write_wait)) {
2281 wake_up(&o_tty->write_wait);
2288 printk(KERN_WARNING "release_dev: %s: read/write wait queue "
2289 "active!\n", tty_name(tty, buf));
2290 mutex_unlock(&tty_mutex);
2295 * The closing flags are now consistent with the open counts on
2296 * both sides, and we've completed the last operation that could
2297 * block, so it's safe to proceed with closing.
2300 if (--o_tty->count < 0) {
2301 printk(KERN_WARNING "release_dev: bad pty slave count "
2303 o_tty->count, tty_name(o_tty, buf));
2307 if (--tty->count < 0) {
2308 printk(KERN_WARNING "release_dev: bad tty->count (%d) for %s\n",
2309 tty->count, tty_name(tty, buf));
2314 * We've decremented tty->count, so we need to remove this file
2315 * descriptor off the tty->tty_files list; this serves two
2317 * - check_tty_count sees the correct number of file descriptors
2318 * associated with this tty.
2319 * - do_tty_hangup no longer sees this file descriptor as
2320 * something that needs to be handled for hangups.
2323 filp->private_data = NULL;
2326 * Perform some housekeeping before deciding whether to return.
2328 * Set the TTY_CLOSING flag if this was the last open. In the
2329 * case of a pty we may have to wait around for the other side
2330 * to close, and TTY_CLOSING makes sure we can't be reopened.
2333 set_bit(TTY_CLOSING, &tty->flags);
2335 set_bit(TTY_CLOSING, &o_tty->flags);
2338 * If _either_ side is closing, make sure there aren't any
2339 * processes that still think tty or o_tty is their controlling
2342 if (tty_closing || o_tty_closing) {
2343 struct task_struct *p;
2345 read_lock(&tasklist_lock);
2346 do_each_task_pid(tty->session, PIDTYPE_SID, p) {
2347 p->signal->tty = NULL;
2348 } while_each_task_pid(tty->session, PIDTYPE_SID, p);
2350 do_each_task_pid(o_tty->session, PIDTYPE_SID, p) {
2351 p->signal->tty = NULL;
2352 } while_each_task_pid(o_tty->session, PIDTYPE_SID, p);
2353 read_unlock(&tasklist_lock);
2356 mutex_unlock(&tty_mutex);
2358 /* check whether both sides are closing ... */
2359 if (!tty_closing || (o_tty && !o_tty_closing))
2362 #ifdef TTY_DEBUG_HANGUP
2363 printk(KERN_DEBUG "freeing tty structure...");
2366 * Prevent flush_to_ldisc() from rescheduling the work for later. Then
2367 * kill any delayed work. As this is the final close it does not
2368 * race with the set_ldisc code path.
2370 clear_bit(TTY_LDISC, &tty->flags);
2371 cancel_delayed_work(&tty->buf.work);
2374 * Wait for ->hangup_work and ->buf.work handlers to terminate
2377 flush_scheduled_work();
2380 * Wait for any short term users (we know they are just driver
2381 * side waiters as the file is closing so user count on the file
2384 spin_lock_irqsave(&tty_ldisc_lock, flags);
2385 while(tty->ldisc.refcount)
2387 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
2388 wait_event(tty_ldisc_wait, tty->ldisc.refcount == 0);
2389 spin_lock_irqsave(&tty_ldisc_lock, flags);
2391 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
2393 * Shutdown the current line discipline, and reset it to N_TTY.
2394 * N.B. why reset ldisc when we're releasing the memory??
2396 * FIXME: this MUST get fixed for the new reflocking
2398 if (tty->ldisc.close)
2399 (tty->ldisc.close)(tty);
2400 tty_ldisc_put(tty->ldisc.num);
2403 * Switch the line discipline back
2405 tty_ldisc_assign(tty, tty_ldisc_get(N_TTY));
2406 tty_set_termios_ldisc(tty,N_TTY);
2408 /* FIXME: could o_tty be in setldisc here ? */
2409 clear_bit(TTY_LDISC, &o_tty->flags);
2410 if (o_tty->ldisc.close)
2411 (o_tty->ldisc.close)(o_tty);
2412 tty_ldisc_put(o_tty->ldisc.num);
2413 tty_ldisc_assign(o_tty, tty_ldisc_get(N_TTY));
2414 tty_set_termios_ldisc(o_tty,N_TTY);
2417 * The release_mem function takes care of the details of clearing
2418 * the slots and preserving the termios structure.
2420 release_mem(tty, idx);
2422 #ifdef CONFIG_UNIX98_PTYS
2423 /* Make this pty number available for reallocation */
2425 down(&allocated_ptys_lock);
2426 idr_remove(&allocated_ptys, idx);
2427 up(&allocated_ptys_lock);
2434 * tty_open - open a tty device
2435 * @inode: inode of device file
2436 * @filp: file pointer to tty
2438 * tty_open and tty_release keep up the tty count that contains the
2439 * number of opens done on a tty. We cannot use the inode-count, as
2440 * different inodes might point to the same tty.
2442 * Open-counting is needed for pty masters, as well as for keeping
2443 * track of serial lines: DTR is dropped when the last close happens.
2444 * (This is not done solely through tty->count, now. - Ted 1/27/92)
2446 * The termios state of a pty is reset on first open so that
2447 * settings don't persist across reuse.
2449 * Locking: tty_mutex protects current->signal->tty, get_tty_driver and
2450 * init_dev work. tty->count should protect the rest.
2451 * task_lock is held to update task details for sessions
2454 static int tty_open(struct inode * inode, struct file * filp)
2456 struct tty_struct *tty;
2458 struct tty_driver *driver;
2460 dev_t device = inode->i_rdev;
2461 unsigned short saved_flags = filp->f_flags;
2463 nonseekable_open(inode, filp);
2466 noctty = filp->f_flags & O_NOCTTY;
2470 mutex_lock(&tty_mutex);
2472 if (device == MKDEV(TTYAUX_MAJOR,0)) {
2473 if (!current->signal->tty) {
2474 mutex_unlock(&tty_mutex);
2477 driver = current->signal->tty->driver;
2478 index = current->signal->tty->index;
2479 filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */
2484 if (device == MKDEV(TTY_MAJOR,0)) {
2485 extern struct tty_driver *console_driver;
2486 driver = console_driver;
2492 if (device == MKDEV(TTYAUX_MAJOR,1)) {
2493 driver = console_device(&index);
2495 /* Don't let /dev/console block */
2496 filp->f_flags |= O_NONBLOCK;
2500 mutex_unlock(&tty_mutex);
2504 driver = get_tty_driver(device, &index);
2506 mutex_unlock(&tty_mutex);
2510 retval = init_dev(driver, index, &tty);
2511 mutex_unlock(&tty_mutex);
2515 filp->private_data = tty;
2516 file_move(filp, &tty->tty_files);
2517 check_tty_count(tty, "tty_open");
2518 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
2519 tty->driver->subtype == PTY_TYPE_MASTER)
2521 #ifdef TTY_DEBUG_HANGUP
2522 printk(KERN_DEBUG "opening %s...", tty->name);
2525 if (tty->driver->open)
2526 retval = tty->driver->open(tty, filp);
2530 filp->f_flags = saved_flags;
2532 if (!retval && test_bit(TTY_EXCLUSIVE, &tty->flags) && !capable(CAP_SYS_ADMIN))
2536 #ifdef TTY_DEBUG_HANGUP
2537 printk(KERN_DEBUG "error %d in opening %s...", retval,
2541 if (retval != -ERESTARTSYS)
2543 if (signal_pending(current))
2547 * Need to reset f_op in case a hangup happened.
2549 if (filp->f_op == &hung_up_tty_fops)
2550 filp->f_op = &tty_fops;
2554 current->signal->leader &&
2555 !current->signal->tty &&
2556 tty->session == 0) {
2558 current->signal->tty = tty;
2559 task_unlock(current);
2560 current->signal->tty_old_pgrp = 0;
2561 tty->session = current->signal->session;
2562 tty->pgrp = process_group(current);
2567 #ifdef CONFIG_UNIX98_PTYS
2569 * ptmx_open - open a unix 98 pty master
2570 * @inode: inode of device file
2571 * @filp: file pointer to tty
2573 * Allocate a unix98 pty master device from the ptmx driver.
2575 * Locking: tty_mutex protects theinit_dev work. tty->count should
2577 * allocated_ptys_lock handles the list of free pty numbers
2580 static int ptmx_open(struct inode * inode, struct file * filp)
2582 struct tty_struct *tty;
2587 nonseekable_open(inode, filp);
2589 /* find a device that is not in use. */
2590 down(&allocated_ptys_lock);
2591 if (!idr_pre_get(&allocated_ptys, GFP_KERNEL)) {
2592 up(&allocated_ptys_lock);
2595 idr_ret = idr_get_new(&allocated_ptys, NULL, &index);
2597 up(&allocated_ptys_lock);
2598 if (idr_ret == -EAGAIN)
2602 if (index >= pty_limit) {
2603 idr_remove(&allocated_ptys, index);
2604 up(&allocated_ptys_lock);
2607 up(&allocated_ptys_lock);
2609 mutex_lock(&tty_mutex);
2610 retval = init_dev(ptm_driver, index, &tty);
2611 mutex_unlock(&tty_mutex);
2616 set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
2617 filp->private_data = tty;
2618 file_move(filp, &tty->tty_files);
2621 if (devpts_pty_new(tty->link))
2624 check_tty_count(tty, "tty_open");
2625 retval = ptm_driver->open(tty, filp);
2632 down(&allocated_ptys_lock);
2633 idr_remove(&allocated_ptys, index);
2634 up(&allocated_ptys_lock);
2640 * tty_release - vfs callback for close
2641 * @inode: inode of tty
2642 * @filp: file pointer for handle to tty
2644 * Called the last time each file handle is closed that references
2645 * this tty. There may however be several such references.
2648 * Takes bkl. See release_dev
2651 static int tty_release(struct inode * inode, struct file * filp)
2660 * tty_poll - check tty status
2661 * @filp: file being polled
2662 * @wait: poll wait structures to update
2664 * Call the line discipline polling method to obtain the poll
2665 * status of the device.
2667 * Locking: locks called line discipline but ldisc poll method
2668 * may be re-entered freely by other callers.
2671 static unsigned int tty_poll(struct file * filp, poll_table * wait)
2673 struct tty_struct * tty;
2674 struct tty_ldisc *ld;
2677 tty = (struct tty_struct *)filp->private_data;
2678 if (tty_paranoia_check(tty, filp->f_dentry->d_inode, "tty_poll"))
2681 ld = tty_ldisc_ref_wait(tty);
2683 ret = (ld->poll)(tty, filp, wait);
2684 tty_ldisc_deref(ld);
2688 static int tty_fasync(int fd, struct file * filp, int on)
2690 struct tty_struct * tty;
2693 tty = (struct tty_struct *)filp->private_data;
2694 if (tty_paranoia_check(tty, filp->f_dentry->d_inode, "tty_fasync"))
2697 retval = fasync_helper(fd, filp, on, &tty->fasync);
2702 if (!waitqueue_active(&tty->read_wait))
2703 tty->minimum_to_wake = 1;
2704 retval = f_setown(filp, (-tty->pgrp) ? : current->pid, 0);
2708 if (!tty->fasync && !waitqueue_active(&tty->read_wait))
2709 tty->minimum_to_wake = N_TTY_BUF_SIZE;
2715 * tiocsti - fake input character
2716 * @tty: tty to fake input into
2717 * @p: pointer to character
2719 * Fake input to a tty device. Does the neccessary locking and
2722 * FIXME: does not honour flow control ??
2725 * Called functions take tty_ldisc_lock
2726 * current->signal->tty check is safe without locks
2729 static int tiocsti(struct tty_struct *tty, char __user *p)
2732 struct tty_ldisc *ld;
2734 if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN))
2736 if (get_user(ch, p))
2738 ld = tty_ldisc_ref_wait(tty);
2739 ld->receive_buf(tty, &ch, &mbz, 1);
2740 tty_ldisc_deref(ld);
2745 * tiocgwinsz - implement window query ioctl
2747 * @arg: user buffer for result
2749 * Copies the kernel idea of the window size into the user buffer. No
2752 * FIXME: Returning random values racing a window size set is wrong
2753 * should lock here against that
2756 static int tiocgwinsz(struct tty_struct *tty, struct winsize __user * arg)
2758 if (copy_to_user(arg, &tty->winsize, sizeof(*arg)))
2764 * tiocswinsz - implement window size set ioctl
2766 * @arg: user buffer for result
2768 * Copies the user idea of the window size to the kernel. Traditionally
2769 * this is just advisory information but for the Linux console it
2770 * actually has driver level meaning and triggers a VC resize.
2773 * The console_sem is used to ensure we do not try and resize
2774 * the console twice at once.
2775 * FIXME: Two racing size sets may leave the console and kernel
2776 * parameters disagreeing. Is this exploitable ?
2777 * FIXME: Random values racing a window size get is wrong
2778 * should lock here against that
2781 static int tiocswinsz(struct tty_struct *tty, struct tty_struct *real_tty,
2782 struct winsize __user * arg)
2784 struct winsize tmp_ws;
2786 if (copy_from_user(&tmp_ws, arg, sizeof(*arg)))
2788 if (!memcmp(&tmp_ws, &tty->winsize, sizeof(*arg)))
2791 if (tty->driver->type == TTY_DRIVER_TYPE_CONSOLE) {
2794 acquire_console_sem();
2795 rc = vc_resize(tty->driver_data, tmp_ws.ws_col, tmp_ws.ws_row);
2796 release_console_sem();
2802 kill_pg(tty->pgrp, SIGWINCH, 1);
2803 if ((real_tty->pgrp != tty->pgrp) && (real_tty->pgrp > 0))
2804 kill_pg(real_tty->pgrp, SIGWINCH, 1);
2805 tty->winsize = tmp_ws;
2806 real_tty->winsize = tmp_ws;
2811 * tioccons - allow admin to move logical console
2812 * @file: the file to become console
2814 * Allow the adminstrator to move the redirected console device
2816 * Locking: uses redirect_lock to guard the redirect information
2819 static int tioccons(struct file *file)
2821 if (!capable(CAP_SYS_ADMIN))
2823 if (file->f_op->write == redirected_tty_write) {
2825 spin_lock(&redirect_lock);
2828 spin_unlock(&redirect_lock);
2833 spin_lock(&redirect_lock);
2835 spin_unlock(&redirect_lock);
2840 spin_unlock(&redirect_lock);
2845 * fionbio - non blocking ioctl
2846 * @file: file to set blocking value
2847 * @p: user parameter
2849 * Historical tty interfaces had a blocking control ioctl before
2850 * the generic functionality existed. This piece of history is preserved
2851 * in the expected tty API of posix OS's.
2853 * Locking: none, the open fle handle ensures it won't go away.
2856 static int fionbio(struct file *file, int __user *p)
2860 if (get_user(nonblock, p))
2864 file->f_flags |= O_NONBLOCK;
2866 file->f_flags &= ~O_NONBLOCK;
2871 * tiocsctty - set controlling tty
2872 * @tty: tty structure
2873 * @arg: user argument
2875 * This ioctl is used to manage job control. It permits a session
2876 * leader to set this tty as the controlling tty for the session.
2879 * Takes tasklist lock internally to walk sessions
2880 * Takes task_lock() when updating signal->tty
2882 * FIXME: tty_mutex is needed to protect signal->tty references.
2883 * FIXME: why task_lock on the signal->tty reference ??
2887 static int tiocsctty(struct tty_struct *tty, int arg)
2889 struct task_struct *p;
2891 if (current->signal->leader &&
2892 (current->signal->session == tty->session))
2895 * The process must be a session leader and
2896 * not have a controlling tty already.
2898 if (!current->signal->leader || current->signal->tty)
2900 if (tty->session > 0) {
2902 * This tty is already the controlling
2903 * tty for another session group!
2905 if ((arg == 1) && capable(CAP_SYS_ADMIN)) {
2910 read_lock(&tasklist_lock);
2911 do_each_task_pid(tty->session, PIDTYPE_SID, p) {
2912 p->signal->tty = NULL;
2913 } while_each_task_pid(tty->session, PIDTYPE_SID, p);
2914 read_unlock(&tasklist_lock);
2919 current->signal->tty = tty;
2920 task_unlock(current);
2921 current->signal->tty_old_pgrp = 0;
2922 tty->session = current->signal->session;
2923 tty->pgrp = process_group(current);
2928 * tiocgpgrp - get process group
2929 * @tty: tty passed by user
2930 * @real_tty: tty side of the tty pased by the user if a pty else the tty
2933 * Obtain the process group of the tty. If there is no process group
2936 * Locking: none. Reference to ->signal->tty is safe.
2939 static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2942 * (tty == real_tty) is a cheap way of
2943 * testing if the tty is NOT a master pty.
2945 if (tty == real_tty && current->signal->tty != real_tty)
2947 return put_user(real_tty->pgrp, p);
2951 * tiocspgrp - attempt to set process group
2952 * @tty: tty passed by user
2953 * @real_tty: tty side device matching tty passed by user
2956 * Set the process group of the tty to the session passed. Only
2957 * permitted where the tty session is our session.
2961 * FIXME: current->signal->tty referencing is unsafe.
2964 static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2967 int retval = tty_check_change(real_tty);
2973 if (!current->signal->tty ||
2974 (current->signal->tty != real_tty) ||
2975 (real_tty->session != current->signal->session))
2977 if (get_user(pgrp, p))
2981 if (session_of_pgrp(pgrp) != current->signal->session)
2983 real_tty->pgrp = pgrp;
2988 * tiocgsid - get session id
2989 * @tty: tty passed by user
2990 * @real_tty: tty side of the tty pased by the user if a pty else the tty
2991 * @p: pointer to returned session id
2993 * Obtain the session id of the tty. If there is no session
2996 * Locking: none. Reference to ->signal->tty is safe.
2999 static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
3002 * (tty == real_tty) is a cheap way of
3003 * testing if the tty is NOT a master pty.
3005 if (tty == real_tty && current->signal->tty != real_tty)
3007 if (real_tty->session <= 0)
3009 return put_user(real_tty->session, p);
3013 * tiocsetd - set line discipline
3015 * @p: pointer to user data
3017 * Set the line discipline according to user request.
3019 * Locking: see tty_set_ldisc, this function is just a helper
3022 static int tiocsetd(struct tty_struct *tty, int __user *p)
3026 if (get_user(ldisc, p))
3028 return tty_set_ldisc(tty, ldisc);
3032 * send_break - performed time break
3033 * @tty: device to break on
3034 * @duration: timeout in mS
3036 * Perform a timed break on hardware that lacks its own driver level
3037 * timed break functionality.
3043 * What if two overlap
3046 static int send_break(struct tty_struct *tty, unsigned int duration)
3048 tty->driver->break_ctl(tty, -1);
3049 if (!signal_pending(current)) {
3050 msleep_interruptible(duration);
3052 tty->driver->break_ctl(tty, 0);
3053 if (signal_pending(current))
3059 * tiocmget - get modem status
3061 * @file: user file pointer
3062 * @p: pointer to result
3064 * Obtain the modem status bits from the tty driver if the feature
3065 * is supported. Return -EINVAL if it is not available.
3067 * Locking: none (up to the driver)
3070 static int tty_tiocmget(struct tty_struct *tty, struct file *file, int __user *p)
3072 int retval = -EINVAL;
3074 if (tty->driver->tiocmget) {
3075 retval = tty->driver->tiocmget(tty, file);
3078 retval = put_user(retval, p);
3084 * tiocmset - set modem status
3086 * @file: user file pointer
3087 * @cmd: command - clear bits, set bits or set all
3088 * @p: pointer to desired bits
3090 * Set the modem status bits from the tty driver if the feature
3091 * is supported. Return -EINVAL if it is not available.
3093 * Locking: none (up to the driver)
3096 static int tty_tiocmset(struct tty_struct *tty, struct file *file, unsigned int cmd,
3099 int retval = -EINVAL;
3101 if (tty->driver->tiocmset) {
3102 unsigned int set, clear, val;
3104 retval = get_user(val, p);
3122 set &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
3123 clear &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
3125 retval = tty->driver->tiocmset(tty, file, set, clear);
3131 * Split this up, as gcc can choke on it otherwise..
3133 int tty_ioctl(struct inode * inode, struct file * file,
3134 unsigned int cmd, unsigned long arg)
3136 struct tty_struct *tty, *real_tty;
3137 void __user *p = (void __user *)arg;
3139 struct tty_ldisc *ld;
3141 tty = (struct tty_struct *)file->private_data;
3142 if (tty_paranoia_check(tty, inode, "tty_ioctl"))
3146 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
3147 tty->driver->subtype == PTY_TYPE_MASTER)
3148 real_tty = tty->link;
3151 * Break handling by driver
3153 if (!tty->driver->break_ctl) {
3157 if (tty->driver->ioctl)
3158 return tty->driver->ioctl(tty, file, cmd, arg);
3161 /* These two ioctl's always return success; even if */
3162 /* the driver doesn't support them. */
3165 if (!tty->driver->ioctl)
3167 retval = tty->driver->ioctl(tty, file, cmd, arg);
3168 if (retval == -ENOIOCTLCMD)
3175 * Factor out some common prep work
3183 retval = tty_check_change(tty);
3186 if (cmd != TIOCCBRK) {
3187 tty_wait_until_sent(tty, 0);
3188 if (signal_pending(current))
3196 return tiocsti(tty, p);
3198 return tiocgwinsz(tty, p);
3200 return tiocswinsz(tty, real_tty, p);
3202 return real_tty!=tty ? -EINVAL : tioccons(file);
3204 return fionbio(file, p);
3206 set_bit(TTY_EXCLUSIVE, &tty->flags);
3209 clear_bit(TTY_EXCLUSIVE, &tty->flags);
3212 /* FIXME: taks lock or tty_mutex ? */
3213 if (current->signal->tty != tty)
3215 if (current->signal->leader)
3216 disassociate_ctty(0);
3218 current->signal->tty = NULL;
3219 task_unlock(current);
3222 return tiocsctty(tty, arg);
3224 return tiocgpgrp(tty, real_tty, p);
3226 return tiocspgrp(tty, real_tty, p);
3228 return tiocgsid(tty, real_tty, p);
3230 /* FIXME: check this is ok */
3231 return put_user(tty->ldisc.num, (int __user *)p);
3233 return tiocsetd(tty, p);
3236 return tioclinux(tty, arg);
3241 case TIOCSBRK: /* Turn break on, unconditionally */
3242 tty->driver->break_ctl(tty, -1);
3245 case TIOCCBRK: /* Turn break off, unconditionally */
3246 tty->driver->break_ctl(tty, 0);
3248 case TCSBRK: /* SVID version: non-zero arg --> no break */
3249 /* non-zero arg means wait for all output data
3250 * to be sent (performed above) but don't send break.
3251 * This is used by the tcdrain() termios function.
3254 return send_break(tty, 250);
3256 case TCSBRKP: /* support for POSIX tcsendbreak() */
3257 return send_break(tty, arg ? arg*100 : 250);
3260 return tty_tiocmget(tty, file, p);
3265 return tty_tiocmset(tty, file, cmd, p);
3267 if (tty->driver->ioctl) {
3268 retval = (tty->driver->ioctl)(tty, file, cmd, arg);
3269 if (retval != -ENOIOCTLCMD)
3272 ld = tty_ldisc_ref_wait(tty);
3275 retval = ld->ioctl(tty, file, cmd, arg);
3276 if (retval == -ENOIOCTLCMD)
3279 tty_ldisc_deref(ld);
3285 * This implements the "Secure Attention Key" --- the idea is to
3286 * prevent trojan horses by killing all processes associated with this
3287 * tty when the user hits the "Secure Attention Key". Required for
3288 * super-paranoid applications --- see the Orange Book for more details.
3290 * This code could be nicer; ideally it should send a HUP, wait a few
3291 * seconds, then send a INT, and then a KILL signal. But you then
3292 * have to coordinate with the init process, since all processes associated
3293 * with the current tty must be dead before the new getty is allowed
3296 * Now, if it would be correct ;-/ The current code has a nasty hole -
3297 * it doesn't catch files in flight. We may send the descriptor to ourselves
3298 * via AF_UNIX socket, close it and later fetch from socket. FIXME.
3300 * Nasty bug: do_SAK is being called in interrupt context. This can
3301 * deadlock. We punt it up to process context. AKPM - 16Mar2001
3303 static void __do_SAK(void *arg)
3308 struct tty_struct *tty = arg;
3309 struct task_struct *g, *p;
3313 struct tty_ldisc *disc;
3314 struct fdtable *fdt;
3318 session = tty->session;
3320 /* We don't want an ldisc switch during this */
3321 disc = tty_ldisc_ref(tty);
3322 if (disc && disc->flush_buffer)
3323 disc->flush_buffer(tty);
3324 tty_ldisc_deref(disc);
3326 if (tty->driver->flush_buffer)
3327 tty->driver->flush_buffer(tty);
3329 read_lock(&tasklist_lock);
3330 /* Kill the entire session */
3331 do_each_task_pid(session, PIDTYPE_SID, p) {
3332 printk(KERN_NOTICE "SAK: killed process %d"
3333 " (%s): p->signal->session==tty->session\n",
3335 send_sig(SIGKILL, p, 1);
3336 } while_each_task_pid(session, PIDTYPE_SID, p);
3337 /* Now kill any processes that happen to have the
3340 do_each_thread(g, p) {
3341 if (p->signal->tty == tty) {
3342 printk(KERN_NOTICE "SAK: killed process %d"
3343 " (%s): p->signal->session==tty->session\n",
3345 send_sig(SIGKILL, p, 1);
3351 * We don't take a ref to the file, so we must
3352 * hold ->file_lock instead.
3354 spin_lock(&p->files->file_lock);
3355 fdt = files_fdtable(p->files);
3356 for (i=0; i < fdt->max_fds; i++) {
3357 filp = fcheck_files(p->files, i);
3360 if (filp->f_op->read == tty_read &&
3361 filp->private_data == tty) {
3362 printk(KERN_NOTICE "SAK: killed process %d"
3363 " (%s): fd#%d opened to the tty\n",
3364 p->pid, p->comm, i);
3365 force_sig(SIGKILL, p);
3369 spin_unlock(&p->files->file_lock);
3372 } while_each_thread(g, p);
3373 read_unlock(&tasklist_lock);
3378 * The tq handling here is a little racy - tty->SAK_work may already be queued.
3379 * Fortunately we don't need to worry, because if ->SAK_work is already queued,
3380 * the values which we write to it will be identical to the values which it
3381 * already has. --akpm
3383 void do_SAK(struct tty_struct *tty)
3387 PREPARE_WORK(&tty->SAK_work, __do_SAK, tty);
3388 schedule_work(&tty->SAK_work);
3391 EXPORT_SYMBOL(do_SAK);
3395 * @private_: tty structure passed from work queue.
3397 * This routine is called out of the software interrupt to flush data
3398 * from the buffer chain to the line discipline.
3400 * Locking: holds tty->buf.lock to guard buffer list. Drops the lock
3401 * while invoking the line discipline receive_buf method. The
3402 * receive_buf method is single threaded for each tty instance.
3405 static void flush_to_ldisc(void *private_)
3407 struct tty_struct *tty = (struct tty_struct *) private_;
3408 unsigned long flags;
3409 struct tty_ldisc *disc;
3410 struct tty_buffer *tbuf, *head;
3412 unsigned char *flag_buf;
3414 disc = tty_ldisc_ref(tty);
3415 if (disc == NULL) /* !TTY_LDISC */
3418 spin_lock_irqsave(&tty->buf.lock, flags);
3419 head = tty->buf.head;
3421 tty->buf.head = NULL;
3423 int count = head->commit - head->read;
3425 if (head->next == NULL)
3429 tty_buffer_free(tty, tbuf);
3432 if (!tty->receive_room) {
3433 schedule_delayed_work(&tty->buf.work, 1);
3436 if (count > tty->receive_room)
3437 count = tty->receive_room;
3438 char_buf = head->char_buf_ptr + head->read;
3439 flag_buf = head->flag_buf_ptr + head->read;
3440 head->read += count;
3441 spin_unlock_irqrestore(&tty->buf.lock, flags);
3442 disc->receive_buf(tty, char_buf, flag_buf, count);
3443 spin_lock_irqsave(&tty->buf.lock, flags);
3445 tty->buf.head = head;
3447 spin_unlock_irqrestore(&tty->buf.lock, flags);
3449 tty_ldisc_deref(disc);
3453 * Routine which returns the baud rate of the tty
3455 * Note that the baud_table needs to be kept in sync with the
3456 * include/asm/termbits.h file.
3458 static int baud_table[] = {
3459 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
3460 9600, 19200, 38400, 57600, 115200, 230400, 460800,
3462 76800, 153600, 307200, 614400, 921600
3464 500000, 576000, 921600, 1000000, 1152000, 1500000, 2000000,
3465 2500000, 3000000, 3500000, 4000000
3469 static int n_baud_table = ARRAY_SIZE(baud_table);
3472 * tty_termios_baud_rate
3473 * @termios: termios structure
3475 * Convert termios baud rate data into a speed. This should be called
3476 * with the termios lock held if this termios is a terminal termios
3477 * structure. May change the termios data.
3482 int tty_termios_baud_rate(struct termios *termios)
3486 cbaud = termios->c_cflag & CBAUD;
3488 if (cbaud & CBAUDEX) {
3491 if (cbaud < 1 || cbaud + 15 > n_baud_table)
3492 termios->c_cflag &= ~CBAUDEX;
3496 return baud_table[cbaud];
3499 EXPORT_SYMBOL(tty_termios_baud_rate);
3502 * tty_get_baud_rate - get tty bit rates
3503 * @tty: tty to query
3505 * Returns the baud rate as an integer for this terminal. The
3506 * termios lock must be held by the caller and the terminal bit
3507 * flags may be updated.
3512 int tty_get_baud_rate(struct tty_struct *tty)
3514 int baud = tty_termios_baud_rate(tty->termios);
3516 if (baud == 38400 && tty->alt_speed) {
3518 printk(KERN_WARNING "Use of setserial/setrocket to "
3519 "set SPD_* flags is deprecated\n");
3522 baud = tty->alt_speed;
3528 EXPORT_SYMBOL(tty_get_baud_rate);
3531 * tty_flip_buffer_push - terminal
3534 * Queue a push of the terminal flip buffers to the line discipline. This
3535 * function must not be called from IRQ context if tty->low_latency is set.
3537 * In the event of the queue being busy for flipping the work will be
3538 * held off and retried later.
3540 * Locking: tty buffer lock. Driver locks in low latency mode.
3543 void tty_flip_buffer_push(struct tty_struct *tty)
3545 unsigned long flags;
3546 spin_lock_irqsave(&tty->buf.lock, flags);
3547 if (tty->buf.tail != NULL)
3548 tty->buf.tail->commit = tty->buf.tail->used;
3549 spin_unlock_irqrestore(&tty->buf.lock, flags);
3551 if (tty->low_latency)
3552 flush_to_ldisc((void *) tty);
3554 schedule_delayed_work(&tty->buf.work, 1);
3557 EXPORT_SYMBOL(tty_flip_buffer_push);
3561 * initialize_tty_struct
3562 * @tty: tty to initialize
3564 * This subroutine initializes a tty structure that has been newly
3567 * Locking: none - tty in question must not be exposed at this point
3570 static void initialize_tty_struct(struct tty_struct *tty)
3572 memset(tty, 0, sizeof(struct tty_struct));
3573 tty->magic = TTY_MAGIC;
3574 tty_ldisc_assign(tty, tty_ldisc_get(N_TTY));
3576 tty->overrun_time = jiffies;
3577 tty->buf.head = tty->buf.tail = NULL;
3578 tty_buffer_init(tty);
3579 INIT_WORK(&tty->buf.work, flush_to_ldisc, tty);
3580 init_MUTEX(&tty->buf.pty_sem);
3581 init_MUTEX(&tty->termios_sem);
3582 init_waitqueue_head(&tty->write_wait);
3583 init_waitqueue_head(&tty->read_wait);
3584 INIT_WORK(&tty->hangup_work, do_tty_hangup, tty);
3585 mutex_init(&tty->atomic_read_lock);
3586 mutex_init(&tty->atomic_write_lock);
3587 spin_lock_init(&tty->read_lock);
3588 INIT_LIST_HEAD(&tty->tty_files);
3589 INIT_WORK(&tty->SAK_work, NULL, NULL);
3593 * The default put_char routine if the driver did not define one.
3596 static void tty_default_put_char(struct tty_struct *tty, unsigned char ch)
3598 tty->driver->write(tty, &ch, 1);
3601 static struct class *tty_class;
3604 * tty_register_device - register a tty device
3605 * @driver: the tty driver that describes the tty device
3606 * @index: the index in the tty driver for this tty device
3607 * @device: a struct device that is associated with this tty device.
3608 * This field is optional, if there is no known struct device
3609 * for this tty device it can be set to NULL safely.
3611 * Returns a pointer to the class device (or ERR_PTR(-EFOO) on error).
3613 * This call is required to be made to register an individual tty device
3614 * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If
3615 * that bit is not set, this function should not be called by a tty
3621 struct class_device *tty_register_device(struct tty_driver *driver,
3622 unsigned index, struct device *device)
3625 dev_t dev = MKDEV(driver->major, driver->minor_start) + index;
3627 if (index >= driver->num) {
3628 printk(KERN_ERR "Attempt to register invalid tty line number "
3630 return ERR_PTR(-EINVAL);
3633 if (driver->type == TTY_DRIVER_TYPE_PTY)
3634 pty_line_name(driver, index, name);
3636 tty_line_name(driver, index, name);
3638 return class_device_create(tty_class, NULL, dev, device, "%s", name);
3642 * tty_unregister_device - unregister a tty device
3643 * @driver: the tty driver that describes the tty device
3644 * @index: the index in the tty driver for this tty device
3646 * If a tty device is registered with a call to tty_register_device() then
3647 * this function must be called when the tty device is gone.
3652 void tty_unregister_device(struct tty_driver *driver, unsigned index)
3654 class_device_destroy(tty_class, MKDEV(driver->major, driver->minor_start) + index);
3657 EXPORT_SYMBOL(tty_register_device);
3658 EXPORT_SYMBOL(tty_unregister_device);
3660 struct tty_driver *alloc_tty_driver(int lines)
3662 struct tty_driver *driver;
3664 driver = kmalloc(sizeof(struct tty_driver), GFP_KERNEL);
3666 memset(driver, 0, sizeof(struct tty_driver));
3667 driver->magic = TTY_DRIVER_MAGIC;
3668 driver->num = lines;
3669 /* later we'll move allocation of tables here */
3674 void put_tty_driver(struct tty_driver *driver)
3679 void tty_set_operations(struct tty_driver *driver, struct tty_operations *op)
3681 driver->open = op->open;
3682 driver->close = op->close;
3683 driver->write = op->write;
3684 driver->put_char = op->put_char;
3685 driver->flush_chars = op->flush_chars;
3686 driver->write_room = op->write_room;
3687 driver->chars_in_buffer = op->chars_in_buffer;
3688 driver->ioctl = op->ioctl;
3689 driver->set_termios = op->set_termios;
3690 driver->throttle = op->throttle;
3691 driver->unthrottle = op->unthrottle;
3692 driver->stop = op->stop;
3693 driver->start = op->start;
3694 driver->hangup = op->hangup;
3695 driver->break_ctl = op->break_ctl;
3696 driver->flush_buffer = op->flush_buffer;
3697 driver->set_ldisc = op->set_ldisc;
3698 driver->wait_until_sent = op->wait_until_sent;
3699 driver->send_xchar = op->send_xchar;
3700 driver->read_proc = op->read_proc;
3701 driver->write_proc = op->write_proc;
3702 driver->tiocmget = op->tiocmget;
3703 driver->tiocmset = op->tiocmset;
3707 EXPORT_SYMBOL(alloc_tty_driver);
3708 EXPORT_SYMBOL(put_tty_driver);
3709 EXPORT_SYMBOL(tty_set_operations);
3712 * Called by a tty driver to register itself.
3714 int tty_register_driver(struct tty_driver *driver)
3721 if (driver->flags & TTY_DRIVER_INSTALLED)
3724 if (!(driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
3725 p = kmalloc(driver->num * 3 * sizeof(void *), GFP_KERNEL);
3728 memset(p, 0, driver->num * 3 * sizeof(void *));
3731 if (!driver->major) {
3732 error = alloc_chrdev_region(&dev, driver->minor_start, driver->num,
3733 (char*)driver->name);
3735 driver->major = MAJOR(dev);
3736 driver->minor_start = MINOR(dev);
3739 dev = MKDEV(driver->major, driver->minor_start);
3740 error = register_chrdev_region(dev, driver->num,
3741 (char*)driver->name);
3749 driver->ttys = (struct tty_struct **)p;
3750 driver->termios = (struct termios **)(p + driver->num);
3751 driver->termios_locked = (struct termios **)(p + driver->num * 2);
3753 driver->ttys = NULL;
3754 driver->termios = NULL;
3755 driver->termios_locked = NULL;
3758 cdev_init(&driver->cdev, &tty_fops);
3759 driver->cdev.owner = driver->owner;
3760 error = cdev_add(&driver->cdev, dev, driver->num);
3762 unregister_chrdev_region(dev, driver->num);
3763 driver->ttys = NULL;
3764 driver->termios = driver->termios_locked = NULL;
3769 if (!driver->put_char)
3770 driver->put_char = tty_default_put_char;
3772 list_add(&driver->tty_drivers, &tty_drivers);
3774 if ( !(driver->flags & TTY_DRIVER_DYNAMIC_DEV) ) {
3775 for(i = 0; i < driver->num; i++)
3776 tty_register_device(driver, i, NULL);
3778 proc_tty_register_driver(driver);
3782 EXPORT_SYMBOL(tty_register_driver);
3785 * Called by a tty driver to unregister itself.
3787 int tty_unregister_driver(struct tty_driver *driver)
3793 if (driver->refcount)
3796 unregister_chrdev_region(MKDEV(driver->major, driver->minor_start),
3799 list_del(&driver->tty_drivers);
3802 * Free the termios and termios_locked structures because
3803 * we don't want to get memory leaks when modular tty
3804 * drivers are removed from the kernel.
3806 for (i = 0; i < driver->num; i++) {
3807 tp = driver->termios[i];
3809 driver->termios[i] = NULL;
3812 tp = driver->termios_locked[i];
3814 driver->termios_locked[i] = NULL;
3817 if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV))
3818 tty_unregister_device(driver, i);
3821 proc_tty_unregister_driver(driver);
3822 driver->ttys = NULL;
3823 driver->termios = driver->termios_locked = NULL;
3825 cdev_del(&driver->cdev);
3829 EXPORT_SYMBOL(tty_unregister_driver);
3833 * Initialize the console device. This is called *early*, so
3834 * we can't necessarily depend on lots of kernel help here.
3835 * Just do some early initializations, and do the complex setup
3838 void __init console_init(void)
3842 /* Setup the default TTY line discipline. */
3843 (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
3846 * set up the console device so that later boot sequences can
3847 * inform about problems etc..
3849 #ifdef CONFIG_EARLY_PRINTK
3850 disable_early_printk();
3852 call = __con_initcall_start;
3853 while (call < __con_initcall_end) {
3860 extern int vty_init(void);
3863 static int __init tty_class_init(void)
3865 tty_class = class_create(THIS_MODULE, "tty");
3866 if (IS_ERR(tty_class))
3867 return PTR_ERR(tty_class);
3871 postcore_initcall(tty_class_init);
3873 /* 3/2004 jmc: why do these devices exist? */
3875 static struct cdev tty_cdev, console_cdev;
3876 #ifdef CONFIG_UNIX98_PTYS
3877 static struct cdev ptmx_cdev;
3880 static struct cdev vc0_cdev;
3884 * Ok, now we can initialize the rest of the tty devices and can count
3885 * on memory allocations, interrupts etc..
3887 static int __init tty_init(void)
3889 cdev_init(&tty_cdev, &tty_fops);
3890 if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) ||
3891 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0)
3892 panic("Couldn't register /dev/tty driver\n");
3893 class_device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), NULL, "tty");
3895 cdev_init(&console_cdev, &console_fops);
3896 if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) ||
3897 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0)
3898 panic("Couldn't register /dev/console driver\n");
3899 class_device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 1), NULL, "console");
3901 #ifdef CONFIG_UNIX98_PTYS
3902 cdev_init(&ptmx_cdev, &ptmx_fops);
3903 if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) ||
3904 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0)
3905 panic("Couldn't register /dev/ptmx driver\n");
3906 class_device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), NULL, "ptmx");
3910 cdev_init(&vc0_cdev, &console_fops);
3911 if (cdev_add(&vc0_cdev, MKDEV(TTY_MAJOR, 0), 1) ||
3912 register_chrdev_region(MKDEV(TTY_MAJOR, 0), 1, "/dev/vc/0") < 0)
3913 panic("Couldn't register /dev/tty0 driver\n");
3914 class_device_create(tty_class, NULL, MKDEV(TTY_MAJOR, 0), NULL, "tty0");
3920 module_init(tty_init);