ia64/xen-unstable

view linux-2.6-xen-sparse/drivers/char/tty_io.c @ 12272:8f552314e45a

[LINUX] Use pfn_to_page instead of relying in memory assumptions

This makes usage agnostic wrt the memory model being used.

Signed-off-by: Glauber de Oliveira Costa <gcosta@redhat.com>
author kfraser@localhost.localdomain
date Tue Nov 07 09:28:18 2006 +0000 (2006-11-07)
parents c5d4d47bbeb8
children 4fad820a2233
line source
1 /*
2 * linux/drivers/char/tty_io.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
7 /*
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.
10 *
11 * Kill-line thanks to John T Kohl, who also corrected VMIN = VTIME = 0.
12 *
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.
18 *
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
23 *
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).
27 *
28 * NOTE: pay no attention to the line discipline code (yet); its
29 * interface is still subject to change in this version...
30 * -- TYT, 1/31/92
31 *
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.
35 *
36 * Rewrote canonical mode and added more termios flags.
37 * -- julian@uhunix.uhcc.hawaii.edu (J. Cowley), 13Jan94
38 *
39 * Reorganized FASYNC support so mouse code can share it.
40 * -- ctm@ardi.com, 9Sep95
41 *
42 * New TIOCLINUX variants added.
43 * -- mj@k332.feld.cvut.cz, 19-Nov-95
44 *
45 * Restrict vt switching via ioctl()
46 * -- grif@cs.ucr.edu, 5-Dec-95
47 *
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
51 *
52 * Rewrote init_dev and release_dev to eliminate races.
53 * -- Bill Hawes <whawes@star.net>, June 97
54 *
55 * Added devfs support.
56 * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 13-Jan-1998
57 *
58 * Added support for a Unix98-style ptmx device.
59 * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998
60 *
61 * Reduced memory usage for older ARM systems
62 * -- Russell King <rmk@arm.linux.org.uk>
63 *
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
66 */
68 #include <linux/config.h>
69 #include <linux/types.h>
70 #include <linux/major.h>
71 #include <linux/errno.h>
72 #include <linux/signal.h>
73 #include <linux/fcntl.h>
74 #include <linux/sched.h>
75 #include <linux/interrupt.h>
76 #include <linux/tty.h>
77 #include <linux/tty_driver.h>
78 #include <linux/tty_flip.h>
79 #include <linux/devpts_fs.h>
80 #include <linux/file.h>
81 #include <linux/console.h>
82 #include <linux/timer.h>
83 #include <linux/ctype.h>
84 #include <linux/kd.h>
85 #include <linux/mm.h>
86 #include <linux/string.h>
87 #include <linux/slab.h>
88 #include <linux/poll.h>
89 #include <linux/proc_fs.h>
90 #include <linux/init.h>
91 #include <linux/module.h>
92 #include <linux/smp_lock.h>
93 #include <linux/device.h>
94 #include <linux/idr.h>
95 #include <linux/wait.h>
96 #include <linux/bitops.h>
97 #include <linux/delay.h>
99 #include <asm/uaccess.h>
100 #include <asm/system.h>
102 #include <linux/kbd_kern.h>
103 #include <linux/vt_kern.h>
104 #include <linux/selection.h>
105 #include <linux/devfs_fs_kernel.h>
107 #include <linux/kmod.h>
109 #undef TTY_DEBUG_HANGUP
111 #define TTY_PARANOIA_CHECK 1
112 #define CHECK_TTY_COUNT 1
114 struct termios tty_std_termios = { /* for the benefit of tty drivers */
115 .c_iflag = ICRNL | IXON,
116 .c_oflag = OPOST | ONLCR,
117 .c_cflag = B38400 | CS8 | CREAD | HUPCL,
118 .c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK |
119 ECHOCTL | ECHOKE | IEXTEN,
120 .c_cc = INIT_C_CC
121 };
123 EXPORT_SYMBOL(tty_std_termios);
125 /* This list gets poked at by procfs and various bits of boot up code. This
126 could do with some rationalisation such as pulling the tty proc function
127 into this file */
129 LIST_HEAD(tty_drivers); /* linked list of tty drivers */
131 /* Semaphore to protect creating and releasing a tty. This is shared with
132 vt.c for deeply disgusting hack reasons */
133 DECLARE_MUTEX(tty_sem);
135 int console_use_vt = 1;
137 #ifdef CONFIG_UNIX98_PTYS
138 extern struct tty_driver *ptm_driver; /* Unix98 pty masters; for /dev/ptmx */
139 extern int pty_limit; /* Config limit on Unix98 ptys */
140 static DEFINE_IDR(allocated_ptys);
141 static DECLARE_MUTEX(allocated_ptys_lock);
142 static int ptmx_open(struct inode *, struct file *);
143 #endif
145 extern void disable_early_printk(void);
147 static void initialize_tty_struct(struct tty_struct *tty);
149 static ssize_t tty_read(struct file *, char __user *, size_t, loff_t *);
150 static ssize_t tty_write(struct file *, const char __user *, size_t, loff_t *);
151 ssize_t redirected_tty_write(struct file *, const char __user *, size_t, loff_t *);
152 static unsigned int tty_poll(struct file *, poll_table *);
153 static int tty_open(struct inode *, struct file *);
154 static int tty_release(struct inode *, struct file *);
155 int tty_ioctl(struct inode * inode, struct file * file,
156 unsigned int cmd, unsigned long arg);
157 static int tty_fasync(int fd, struct file * filp, int on);
158 static void release_mem(struct tty_struct *tty, int idx);
161 static struct tty_struct *alloc_tty_struct(void)
162 {
163 struct tty_struct *tty;
165 tty = kmalloc(sizeof(struct tty_struct), GFP_KERNEL);
166 if (tty)
167 memset(tty, 0, sizeof(struct tty_struct));
168 return tty;
169 }
171 static void tty_buffer_free_all(struct tty_struct *);
173 static inline void free_tty_struct(struct tty_struct *tty)
174 {
175 kfree(tty->write_buf);
176 tty_buffer_free_all(tty);
177 kfree(tty);
178 }
180 #define TTY_NUMBER(tty) ((tty)->index + (tty)->driver->name_base)
182 char *tty_name(struct tty_struct *tty, char *buf)
183 {
184 if (!tty) /* Hmm. NULL pointer. That's fun. */
185 strcpy(buf, "NULL tty");
186 else
187 strcpy(buf, tty->name);
188 return buf;
189 }
191 EXPORT_SYMBOL(tty_name);
193 int tty_paranoia_check(struct tty_struct *tty, struct inode *inode,
194 const char *routine)
195 {
196 #ifdef TTY_PARANOIA_CHECK
197 if (!tty) {
198 printk(KERN_WARNING
199 "null TTY for (%d:%d) in %s\n",
200 imajor(inode), iminor(inode), routine);
201 return 1;
202 }
203 if (tty->magic != TTY_MAGIC) {
204 printk(KERN_WARNING
205 "bad magic number for tty struct (%d:%d) in %s\n",
206 imajor(inode), iminor(inode), routine);
207 return 1;
208 }
209 #endif
210 return 0;
211 }
213 static int check_tty_count(struct tty_struct *tty, const char *routine)
214 {
215 #ifdef CHECK_TTY_COUNT
216 struct list_head *p;
217 int count = 0;
219 file_list_lock();
220 list_for_each(p, &tty->tty_files) {
221 count++;
222 }
223 file_list_unlock();
224 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
225 tty->driver->subtype == PTY_TYPE_SLAVE &&
226 tty->link && tty->link->count)
227 count++;
228 if (tty->count != count) {
229 printk(KERN_WARNING "Warning: dev (%s) tty->count(%d) "
230 "!= #fd's(%d) in %s\n",
231 tty->name, tty->count, count, routine);
232 return count;
233 }
234 #endif
235 return 0;
236 }
238 /*
239 * Tty buffer allocation management
240 */
242 static void tty_buffer_free_all(struct tty_struct *tty)
243 {
244 struct tty_buffer *thead;
245 while((thead = tty->buf.head) != NULL) {
246 tty->buf.head = thead->next;
247 kfree(thead);
248 }
249 while((thead = tty->buf.free) != NULL) {
250 tty->buf.free = thead->next;
251 kfree(thead);
252 }
253 tty->buf.tail = NULL;
254 }
256 static void tty_buffer_init(struct tty_struct *tty)
257 {
258 spin_lock_init(&tty->buf.lock);
259 tty->buf.head = NULL;
260 tty->buf.tail = NULL;
261 tty->buf.free = NULL;
262 }
264 static struct tty_buffer *tty_buffer_alloc(size_t size)
265 {
266 struct tty_buffer *p = kmalloc(sizeof(struct tty_buffer) + 2 * size, GFP_ATOMIC);
267 if(p == NULL)
268 return NULL;
269 p->used = 0;
270 p->size = size;
271 p->next = NULL;
272 p->active = 0;
273 p->commit = 0;
274 p->read = 0;
275 p->char_buf_ptr = (char *)(p->data);
276 p->flag_buf_ptr = (unsigned char *)p->char_buf_ptr + size;
277 /* printk("Flip create %p\n", p); */
278 return p;
279 }
281 /* Must be called with the tty_read lock held. This needs to acquire strategy
282 code to decide if we should kfree or relink a given expired buffer */
284 static void tty_buffer_free(struct tty_struct *tty, struct tty_buffer *b)
285 {
286 /* Dumb strategy for now - should keep some stats */
287 /* printk("Flip dispose %p\n", b); */
288 if(b->size >= 512)
289 kfree(b);
290 else {
291 b->next = tty->buf.free;
292 tty->buf.free = b;
293 }
294 }
296 static struct tty_buffer *tty_buffer_find(struct tty_struct *tty, size_t size)
297 {
298 struct tty_buffer **tbh = &tty->buf.free;
299 while((*tbh) != NULL) {
300 struct tty_buffer *t = *tbh;
301 if(t->size >= size) {
302 *tbh = t->next;
303 t->next = NULL;
304 t->used = 0;
305 t->commit = 0;
306 t->read = 0;
307 /* DEBUG ONLY */
308 /* memset(t->data, '*', size); */
309 /* printk("Flip recycle %p\n", t); */
310 return t;
311 }
312 tbh = &((*tbh)->next);
313 }
314 /* Round the buffer size out */
315 size = (size + 0xFF) & ~ 0xFF;
316 return tty_buffer_alloc(size);
317 /* Should possibly check if this fails for the largest buffer we
318 have queued and recycle that ? */
319 }
321 int tty_buffer_request_room(struct tty_struct *tty, size_t size)
322 {
323 struct tty_buffer *b, *n;
324 int left;
325 unsigned long flags;
327 spin_lock_irqsave(&tty->buf.lock, flags);
329 /* OPTIMISATION: We could keep a per tty "zero" sized buffer to
330 remove this conditional if its worth it. This would be invisible
331 to the callers */
332 if ((b = tty->buf.tail) != NULL) {
333 left = b->size - b->used;
334 b->active = 1;
335 } else
336 left = 0;
338 if (left < size) {
339 /* This is the slow path - looking for new buffers to use */
340 if ((n = tty_buffer_find(tty, size)) != NULL) {
341 if (b != NULL) {
342 b->next = n;
343 b->active = 0;
344 b->commit = b->used;
345 } else
346 tty->buf.head = n;
347 tty->buf.tail = n;
348 n->active = 1;
349 } else
350 size = left;
351 }
353 spin_unlock_irqrestore(&tty->buf.lock, flags);
354 return size;
355 }
357 EXPORT_SYMBOL_GPL(tty_buffer_request_room);
359 int tty_insert_flip_string(struct tty_struct *tty, unsigned char *chars, size_t size)
360 {
361 int copied = 0;
362 do {
363 int space = tty_buffer_request_room(tty, size - copied);
364 struct tty_buffer *tb = tty->buf.tail;
365 /* If there is no space then tb may be NULL */
366 if(unlikely(space == 0))
367 break;
368 memcpy(tb->char_buf_ptr + tb->used, chars, space);
369 memset(tb->flag_buf_ptr + tb->used, TTY_NORMAL, space);
370 tb->used += space;
371 copied += space;
372 chars += space;
373 /* printk("Flip insert %d.\n", space); */
374 }
375 /* There is a small chance that we need to split the data over
376 several buffers. If this is the case we must loop */
377 while (unlikely(size > copied));
378 return copied;
379 }
381 EXPORT_SYMBOL_GPL(tty_insert_flip_string);
383 int tty_insert_flip_string_flags(struct tty_struct *tty, unsigned char *chars, char *flags, size_t size)
384 {
385 int copied = 0;
386 do {
387 int space = tty_buffer_request_room(tty, size - copied);
388 struct tty_buffer *tb = tty->buf.tail;
389 /* If there is no space then tb may be NULL */
390 if(unlikely(space == 0))
391 break;
392 memcpy(tb->char_buf_ptr + tb->used, chars, space);
393 memcpy(tb->flag_buf_ptr + tb->used, flags, space);
394 tb->used += space;
395 copied += space;
396 chars += space;
397 flags += space;
398 }
399 /* There is a small chance that we need to split the data over
400 several buffers. If this is the case we must loop */
401 while (unlikely(size > copied));
402 return copied;
403 }
405 EXPORT_SYMBOL_GPL(tty_insert_flip_string_flags);
408 /*
409 * Prepare a block of space in the buffer for data. Returns the length
410 * available and buffer pointer to the space which is now allocated and
411 * accounted for as ready for normal characters. This is used for drivers
412 * that need their own block copy routines into the buffer. There is no
413 * guarantee the buffer is a DMA target!
414 */
416 int tty_prepare_flip_string(struct tty_struct *tty, unsigned char **chars, size_t size)
417 {
418 int space = tty_buffer_request_room(tty, size);
419 if (likely(space)) {
420 struct tty_buffer *tb = tty->buf.tail;
421 *chars = tb->char_buf_ptr + tb->used;
422 memset(tb->flag_buf_ptr + tb->used, TTY_NORMAL, space);
423 tb->used += space;
424 }
425 return space;
426 }
428 EXPORT_SYMBOL_GPL(tty_prepare_flip_string);
430 /*
431 * Prepare a block of space in the buffer for data. Returns the length
432 * available and buffer pointer to the space which is now allocated and
433 * accounted for as ready for characters. This is used for drivers
434 * that need their own block copy routines into the buffer. There is no
435 * guarantee the buffer is a DMA target!
436 */
438 int tty_prepare_flip_string_flags(struct tty_struct *tty, unsigned char **chars, char **flags, size_t size)
439 {
440 int space = tty_buffer_request_room(tty, size);
441 if (likely(space)) {
442 struct tty_buffer *tb = tty->buf.tail;
443 *chars = tb->char_buf_ptr + tb->used;
444 *flags = tb->flag_buf_ptr + tb->used;
445 tb->used += space;
446 }
447 return space;
448 }
450 EXPORT_SYMBOL_GPL(tty_prepare_flip_string_flags);
454 /*
455 * This is probably overkill for real world processors but
456 * they are not on hot paths so a little discipline won't do
457 * any harm.
458 */
460 static void tty_set_termios_ldisc(struct tty_struct *tty, int num)
461 {
462 down(&tty->termios_sem);
463 tty->termios->c_line = num;
464 up(&tty->termios_sem);
465 }
467 /*
468 * This guards the refcounted line discipline lists. The lock
469 * must be taken with irqs off because there are hangup path
470 * callers who will do ldisc lookups and cannot sleep.
471 */
473 static DEFINE_SPINLOCK(tty_ldisc_lock);
474 static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_wait);
475 static struct tty_ldisc tty_ldiscs[NR_LDISCS]; /* line disc dispatch table */
477 int tty_register_ldisc(int disc, struct tty_ldisc *new_ldisc)
478 {
479 unsigned long flags;
480 int ret = 0;
482 if (disc < N_TTY || disc >= NR_LDISCS)
483 return -EINVAL;
485 spin_lock_irqsave(&tty_ldisc_lock, flags);
486 tty_ldiscs[disc] = *new_ldisc;
487 tty_ldiscs[disc].num = disc;
488 tty_ldiscs[disc].flags |= LDISC_FLAG_DEFINED;
489 tty_ldiscs[disc].refcount = 0;
490 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
492 return ret;
493 }
494 EXPORT_SYMBOL(tty_register_ldisc);
496 int tty_unregister_ldisc(int disc)
497 {
498 unsigned long flags;
499 int ret = 0;
501 if (disc < N_TTY || disc >= NR_LDISCS)
502 return -EINVAL;
504 spin_lock_irqsave(&tty_ldisc_lock, flags);
505 if (tty_ldiscs[disc].refcount)
506 ret = -EBUSY;
507 else
508 tty_ldiscs[disc].flags &= ~LDISC_FLAG_DEFINED;
509 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
511 return ret;
512 }
513 EXPORT_SYMBOL(tty_unregister_ldisc);
515 struct tty_ldisc *tty_ldisc_get(int disc)
516 {
517 unsigned long flags;
518 struct tty_ldisc *ld;
520 if (disc < N_TTY || disc >= NR_LDISCS)
521 return NULL;
523 spin_lock_irqsave(&tty_ldisc_lock, flags);
525 ld = &tty_ldiscs[disc];
526 /* Check the entry is defined */
527 if(ld->flags & LDISC_FLAG_DEFINED)
528 {
529 /* If the module is being unloaded we can't use it */
530 if (!try_module_get(ld->owner))
531 ld = NULL;
532 else /* lock it */
533 ld->refcount++;
534 }
535 else
536 ld = NULL;
537 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
538 return ld;
539 }
541 EXPORT_SYMBOL_GPL(tty_ldisc_get);
543 void tty_ldisc_put(int disc)
544 {
545 struct tty_ldisc *ld;
546 unsigned long flags;
548 if (disc < N_TTY || disc >= NR_LDISCS)
549 BUG();
551 spin_lock_irqsave(&tty_ldisc_lock, flags);
552 ld = &tty_ldiscs[disc];
553 if(ld->refcount == 0)
554 BUG();
555 ld->refcount --;
556 module_put(ld->owner);
557 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
558 }
560 EXPORT_SYMBOL_GPL(tty_ldisc_put);
562 static void tty_ldisc_assign(struct tty_struct *tty, struct tty_ldisc *ld)
563 {
564 tty->ldisc = *ld;
565 tty->ldisc.refcount = 0;
566 }
568 /**
569 * tty_ldisc_try - internal helper
570 * @tty: the tty
571 *
572 * Make a single attempt to grab and bump the refcount on
573 * the tty ldisc. Return 0 on failure or 1 on success. This is
574 * used to implement both the waiting and non waiting versions
575 * of tty_ldisc_ref
576 */
578 static int tty_ldisc_try(struct tty_struct *tty)
579 {
580 unsigned long flags;
581 struct tty_ldisc *ld;
582 int ret = 0;
584 spin_lock_irqsave(&tty_ldisc_lock, flags);
585 ld = &tty->ldisc;
586 if(test_bit(TTY_LDISC, &tty->flags))
587 {
588 ld->refcount++;
589 ret = 1;
590 }
591 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
592 return ret;
593 }
595 /**
596 * tty_ldisc_ref_wait - wait for the tty ldisc
597 * @tty: tty device
598 *
599 * Dereference the line discipline for the terminal and take a
600 * reference to it. If the line discipline is in flux then
601 * wait patiently until it changes.
602 *
603 * Note: Must not be called from an IRQ/timer context. The caller
604 * must also be careful not to hold other locks that will deadlock
605 * against a discipline change, such as an existing ldisc reference
606 * (which we check for)
607 */
609 struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
610 {
611 /* wait_event is a macro */
612 wait_event(tty_ldisc_wait, tty_ldisc_try(tty));
613 if(tty->ldisc.refcount == 0)
614 printk(KERN_ERR "tty_ldisc_ref_wait\n");
615 return &tty->ldisc;
616 }
618 EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
620 /**
621 * tty_ldisc_ref - get the tty ldisc
622 * @tty: tty device
623 *
624 * Dereference the line discipline for the terminal and take a
625 * reference to it. If the line discipline is in flux then
626 * return NULL. Can be called from IRQ and timer functions.
627 */
629 struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
630 {
631 if(tty_ldisc_try(tty))
632 return &tty->ldisc;
633 return NULL;
634 }
636 EXPORT_SYMBOL_GPL(tty_ldisc_ref);
638 /**
639 * tty_ldisc_deref - free a tty ldisc reference
640 * @ld: reference to free up
641 *
642 * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May
643 * be called in IRQ context.
644 */
646 void tty_ldisc_deref(struct tty_ldisc *ld)
647 {
648 unsigned long flags;
650 if(ld == NULL)
651 BUG();
653 spin_lock_irqsave(&tty_ldisc_lock, flags);
654 if(ld->refcount == 0)
655 printk(KERN_ERR "tty_ldisc_deref: no references.\n");
656 else
657 ld->refcount--;
658 if(ld->refcount == 0)
659 wake_up(&tty_ldisc_wait);
660 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
661 }
663 EXPORT_SYMBOL_GPL(tty_ldisc_deref);
665 /**
666 * tty_ldisc_enable - allow ldisc use
667 * @tty: terminal to activate ldisc on
668 *
669 * Set the TTY_LDISC flag when the line discipline can be called
670 * again. Do neccessary wakeups for existing sleepers.
671 *
672 * Note: nobody should set this bit except via this function. Clearing
673 * directly is allowed.
674 */
676 static void tty_ldisc_enable(struct tty_struct *tty)
677 {
678 set_bit(TTY_LDISC, &tty->flags);
679 wake_up(&tty_ldisc_wait);
680 }
682 /**
683 * tty_set_ldisc - set line discipline
684 * @tty: the terminal to set
685 * @ldisc: the line discipline
686 *
687 * Set the discipline of a tty line. Must be called from a process
688 * context.
689 */
691 static int tty_set_ldisc(struct tty_struct *tty, int ldisc)
692 {
693 int retval = 0;
694 struct tty_ldisc o_ldisc;
695 char buf[64];
696 int work;
697 unsigned long flags;
698 struct tty_ldisc *ld;
699 struct tty_struct *o_tty;
701 if ((ldisc < N_TTY) || (ldisc >= NR_LDISCS))
702 return -EINVAL;
704 restart:
706 ld = tty_ldisc_get(ldisc);
707 /* Eduardo Blanco <ejbs@cs.cs.com.uy> */
708 /* Cyrus Durgin <cider@speakeasy.org> */
709 if (ld == NULL) {
710 request_module("tty-ldisc-%d", ldisc);
711 ld = tty_ldisc_get(ldisc);
712 }
713 if (ld == NULL)
714 return -EINVAL;
716 /*
717 * No more input please, we are switching. The new ldisc
718 * will update this value in the ldisc open function
719 */
721 tty->receive_room = 0;
723 /*
724 * Problem: What do we do if this blocks ?
725 */
727 tty_wait_until_sent(tty, 0);
729 if (tty->ldisc.num == ldisc) {
730 tty_ldisc_put(ldisc);
731 return 0;
732 }
734 o_ldisc = tty->ldisc;
735 o_tty = tty->link;
737 /*
738 * Make sure we don't change while someone holds a
739 * reference to the line discipline. The TTY_LDISC bit
740 * prevents anyone taking a reference once it is clear.
741 * We need the lock to avoid racing reference takers.
742 */
744 spin_lock_irqsave(&tty_ldisc_lock, flags);
745 if (tty->ldisc.refcount || (o_tty && o_tty->ldisc.refcount)) {
746 if(tty->ldisc.refcount) {
747 /* Free the new ldisc we grabbed. Must drop the lock
748 first. */
749 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
750 tty_ldisc_put(ldisc);
751 /*
752 * There are several reasons we may be busy, including
753 * random momentary I/O traffic. We must therefore
754 * retry. We could distinguish between blocking ops
755 * and retries if we made tty_ldisc_wait() smarter. That
756 * is up for discussion.
757 */
758 if (wait_event_interruptible(tty_ldisc_wait, tty->ldisc.refcount == 0) < 0)
759 return -ERESTARTSYS;
760 goto restart;
761 }
762 if(o_tty && o_tty->ldisc.refcount) {
763 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
764 tty_ldisc_put(ldisc);
765 if (wait_event_interruptible(tty_ldisc_wait, o_tty->ldisc.refcount == 0) < 0)
766 return -ERESTARTSYS;
767 goto restart;
768 }
769 }
771 /* if the TTY_LDISC bit is set, then we are racing against another ldisc change */
773 if (!test_bit(TTY_LDISC, &tty->flags)) {
774 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
775 tty_ldisc_put(ldisc);
776 ld = tty_ldisc_ref_wait(tty);
777 tty_ldisc_deref(ld);
778 goto restart;
779 }
781 clear_bit(TTY_LDISC, &tty->flags);
782 clear_bit(TTY_DONT_FLIP, &tty->flags);
783 if (o_tty) {
784 clear_bit(TTY_LDISC, &o_tty->flags);
785 clear_bit(TTY_DONT_FLIP, &o_tty->flags);
786 }
787 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
789 /*
790 * From this point on we know nobody has an ldisc
791 * usage reference, nor can they obtain one until
792 * we say so later on.
793 */
795 work = cancel_delayed_work(&tty->buf.work);
796 /*
797 * Wait for ->hangup_work and ->buf.work handlers to terminate
798 */
800 flush_scheduled_work();
801 /* Shutdown the current discipline. */
802 if (tty->ldisc.close)
803 (tty->ldisc.close)(tty);
805 /* Now set up the new line discipline. */
806 tty_ldisc_assign(tty, ld);
807 tty_set_termios_ldisc(tty, ldisc);
808 if (tty->ldisc.open)
809 retval = (tty->ldisc.open)(tty);
810 if (retval < 0) {
811 tty_ldisc_put(ldisc);
812 /* There is an outstanding reference here so this is safe */
813 tty_ldisc_assign(tty, tty_ldisc_get(o_ldisc.num));
814 tty_set_termios_ldisc(tty, tty->ldisc.num);
815 if (tty->ldisc.open && (tty->ldisc.open(tty) < 0)) {
816 tty_ldisc_put(o_ldisc.num);
817 /* This driver is always present */
818 tty_ldisc_assign(tty, tty_ldisc_get(N_TTY));
819 tty_set_termios_ldisc(tty, N_TTY);
820 if (tty->ldisc.open) {
821 int r = tty->ldisc.open(tty);
823 if (r < 0)
824 panic("Couldn't open N_TTY ldisc for "
825 "%s --- error %d.",
826 tty_name(tty, buf), r);
827 }
828 }
829 }
830 /* At this point we hold a reference to the new ldisc and a
831 a reference to the old ldisc. If we ended up flipping back
832 to the existing ldisc we have two references to it */
834 if (tty->ldisc.num != o_ldisc.num && tty->driver->set_ldisc)
835 tty->driver->set_ldisc(tty);
837 tty_ldisc_put(o_ldisc.num);
839 /*
840 * Allow ldisc referencing to occur as soon as the driver
841 * ldisc callback completes.
842 */
844 tty_ldisc_enable(tty);
845 if (o_tty)
846 tty_ldisc_enable(o_tty);
848 /* Restart it in case no characters kick it off. Safe if
849 already running */
850 if (work)
851 schedule_delayed_work(&tty->buf.work, 1);
852 return retval;
853 }
855 /*
856 * This routine returns a tty driver structure, given a device number
857 */
858 static struct tty_driver *get_tty_driver(dev_t device, int *index)
859 {
860 struct tty_driver *p;
862 list_for_each_entry(p, &tty_drivers, tty_drivers) {
863 dev_t base = MKDEV(p->major, p->minor_start);
864 if (device < base || device >= base + p->num)
865 continue;
866 *index = device - base;
867 return p;
868 }
869 return NULL;
870 }
872 /*
873 * If we try to write to, or set the state of, a terminal and we're
874 * not in the foreground, send a SIGTTOU. If the signal is blocked or
875 * ignored, go ahead and perform the operation. (POSIX 7.2)
876 */
877 int tty_check_change(struct tty_struct * tty)
878 {
879 if (current->signal->tty != tty)
880 return 0;
881 if (tty->pgrp <= 0) {
882 printk(KERN_WARNING "tty_check_change: tty->pgrp <= 0!\n");
883 return 0;
884 }
885 if (process_group(current) == tty->pgrp)
886 return 0;
887 if (is_ignored(SIGTTOU))
888 return 0;
889 if (is_orphaned_pgrp(process_group(current)))
890 return -EIO;
891 (void) kill_pg(process_group(current), SIGTTOU, 1);
892 return -ERESTARTSYS;
893 }
895 EXPORT_SYMBOL(tty_check_change);
897 static ssize_t hung_up_tty_read(struct file * file, char __user * buf,
898 size_t count, loff_t *ppos)
899 {
900 return 0;
901 }
903 static ssize_t hung_up_tty_write(struct file * file, const char __user * buf,
904 size_t count, loff_t *ppos)
905 {
906 return -EIO;
907 }
909 /* No kernel lock held - none needed ;) */
910 static unsigned int hung_up_tty_poll(struct file * filp, poll_table * wait)
911 {
912 return POLLIN | POLLOUT | POLLERR | POLLHUP | POLLRDNORM | POLLWRNORM;
913 }
915 static int hung_up_tty_ioctl(struct inode * inode, struct file * file,
916 unsigned int cmd, unsigned long arg)
917 {
918 return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
919 }
921 static struct file_operations tty_fops = {
922 .llseek = no_llseek,
923 .read = tty_read,
924 .write = tty_write,
925 .poll = tty_poll,
926 .ioctl = tty_ioctl,
927 .open = tty_open,
928 .release = tty_release,
929 .fasync = tty_fasync,
930 };
932 #ifdef CONFIG_UNIX98_PTYS
933 static struct file_operations ptmx_fops = {
934 .llseek = no_llseek,
935 .read = tty_read,
936 .write = tty_write,
937 .poll = tty_poll,
938 .ioctl = tty_ioctl,
939 .open = ptmx_open,
940 .release = tty_release,
941 .fasync = tty_fasync,
942 };
943 #endif
945 static struct file_operations console_fops = {
946 .llseek = no_llseek,
947 .read = tty_read,
948 .write = redirected_tty_write,
949 .poll = tty_poll,
950 .ioctl = tty_ioctl,
951 .open = tty_open,
952 .release = tty_release,
953 .fasync = tty_fasync,
954 };
956 static struct file_operations hung_up_tty_fops = {
957 .llseek = no_llseek,
958 .read = hung_up_tty_read,
959 .write = hung_up_tty_write,
960 .poll = hung_up_tty_poll,
961 .ioctl = hung_up_tty_ioctl,
962 .release = tty_release,
963 };
965 static DEFINE_SPINLOCK(redirect_lock);
966 static struct file *redirect;
968 /**
969 * tty_wakeup - request more data
970 * @tty: terminal
971 *
972 * Internal and external helper for wakeups of tty. This function
973 * informs the line discipline if present that the driver is ready
974 * to receive more output data.
975 */
977 void tty_wakeup(struct tty_struct *tty)
978 {
979 struct tty_ldisc *ld;
981 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) {
982 ld = tty_ldisc_ref(tty);
983 if(ld) {
984 if(ld->write_wakeup)
985 ld->write_wakeup(tty);
986 tty_ldisc_deref(ld);
987 }
988 }
989 wake_up_interruptible(&tty->write_wait);
990 }
992 EXPORT_SYMBOL_GPL(tty_wakeup);
994 /**
995 * tty_ldisc_flush - flush line discipline queue
996 * @tty: tty
997 *
998 * Flush the line discipline queue (if any) for this tty. If there
999 * is no line discipline active this is a no-op.
1000 */
1002 void tty_ldisc_flush(struct tty_struct *tty)
1004 struct tty_ldisc *ld = tty_ldisc_ref(tty);
1005 if(ld) {
1006 if(ld->flush_buffer)
1007 ld->flush_buffer(tty);
1008 tty_ldisc_deref(ld);
1012 EXPORT_SYMBOL_GPL(tty_ldisc_flush);
1014 /*
1015 * This can be called by the "eventd" kernel thread. That is process synchronous,
1016 * but doesn't hold any locks, so we need to make sure we have the appropriate
1017 * locks for what we're doing..
1018 */
1019 static void do_tty_hangup(void *data)
1021 struct tty_struct *tty = (struct tty_struct *) data;
1022 struct file * cons_filp = NULL;
1023 struct file *filp, *f = NULL;
1024 struct task_struct *p;
1025 struct tty_ldisc *ld;
1026 int closecount = 0, n;
1028 if (!tty)
1029 return;
1031 /* inuse_filps is protected by the single kernel lock */
1032 lock_kernel();
1034 spin_lock(&redirect_lock);
1035 if (redirect && redirect->private_data == tty) {
1036 f = redirect;
1037 redirect = NULL;
1039 spin_unlock(&redirect_lock);
1041 check_tty_count(tty, "do_tty_hangup");
1042 file_list_lock();
1043 /* This breaks for file handles being sent over AF_UNIX sockets ? */
1044 list_for_each_entry(filp, &tty->tty_files, f_u.fu_list) {
1045 if (filp->f_op->write == redirected_tty_write)
1046 cons_filp = filp;
1047 if (filp->f_op->write != tty_write)
1048 continue;
1049 closecount++;
1050 tty_fasync(-1, filp, 0); /* can't block */
1051 filp->f_op = &hung_up_tty_fops;
1053 file_list_unlock();
1055 /* FIXME! What are the locking issues here? This may me overdoing things..
1056 * this question is especially important now that we've removed the irqlock. */
1058 ld = tty_ldisc_ref(tty);
1059 if(ld != NULL) /* We may have no line discipline at this point */
1061 if (ld->flush_buffer)
1062 ld->flush_buffer(tty);
1063 if (tty->driver->flush_buffer)
1064 tty->driver->flush_buffer(tty);
1065 if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
1066 ld->write_wakeup)
1067 ld->write_wakeup(tty);
1068 if (ld->hangup)
1069 ld->hangup(tty);
1072 /* FIXME: Once we trust the LDISC code better we can wait here for
1073 ldisc completion and fix the driver call race */
1075 wake_up_interruptible(&tty->write_wait);
1076 wake_up_interruptible(&tty->read_wait);
1078 /*
1079 * Shutdown the current line discipline, and reset it to
1080 * N_TTY.
1081 */
1082 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
1084 down(&tty->termios_sem);
1085 *tty->termios = tty->driver->init_termios;
1086 up(&tty->termios_sem);
1089 /* Defer ldisc switch */
1090 /* tty_deferred_ldisc_switch(N_TTY);
1092 This should get done automatically when the port closes and
1093 tty_release is called */
1095 read_lock(&tasklist_lock);
1096 if (tty->session > 0) {
1097 do_each_task_pid(tty->session, PIDTYPE_SID, p) {
1098 if (p->signal->tty == tty)
1099 p->signal->tty = NULL;
1100 if (!p->signal->leader)
1101 continue;
1102 send_group_sig_info(SIGHUP, SEND_SIG_PRIV, p);
1103 send_group_sig_info(SIGCONT, SEND_SIG_PRIV, p);
1104 if (tty->pgrp > 0)
1105 p->signal->tty_old_pgrp = tty->pgrp;
1106 } while_each_task_pid(tty->session, PIDTYPE_SID, p);
1108 read_unlock(&tasklist_lock);
1110 tty->flags = 0;
1111 tty->session = 0;
1112 tty->pgrp = -1;
1113 tty->ctrl_status = 0;
1114 /*
1115 * If one of the devices matches a console pointer, we
1116 * cannot just call hangup() because that will cause
1117 * tty->count and state->count to go out of sync.
1118 * So we just call close() the right number of times.
1119 */
1120 if (cons_filp) {
1121 if (tty->driver->close)
1122 for (n = 0; n < closecount; n++)
1123 tty->driver->close(tty, cons_filp);
1124 } else if (tty->driver->hangup)
1125 (tty->driver->hangup)(tty);
1127 /* We don't want to have driver/ldisc interactions beyond
1128 the ones we did here. The driver layer expects no
1129 calls after ->hangup() from the ldisc side. However we
1130 can't yet guarantee all that */
1132 set_bit(TTY_HUPPED, &tty->flags);
1133 if (ld) {
1134 tty_ldisc_enable(tty);
1135 tty_ldisc_deref(ld);
1137 unlock_kernel();
1138 if (f)
1139 fput(f);
1142 void tty_hangup(struct tty_struct * tty)
1144 #ifdef TTY_DEBUG_HANGUP
1145 char buf[64];
1147 printk(KERN_DEBUG "%s hangup...\n", tty_name(tty, buf));
1148 #endif
1149 schedule_work(&tty->hangup_work);
1152 EXPORT_SYMBOL(tty_hangup);
1154 void tty_vhangup(struct tty_struct * tty)
1156 #ifdef TTY_DEBUG_HANGUP
1157 char buf[64];
1159 printk(KERN_DEBUG "%s vhangup...\n", tty_name(tty, buf));
1160 #endif
1161 do_tty_hangup((void *) tty);
1163 EXPORT_SYMBOL(tty_vhangup);
1165 int tty_hung_up_p(struct file * filp)
1167 return (filp->f_op == &hung_up_tty_fops);
1170 EXPORT_SYMBOL(tty_hung_up_p);
1172 /*
1173 * This function is typically called only by the session leader, when
1174 * it wants to disassociate itself from its controlling tty.
1176 * It performs the following functions:
1177 * (1) Sends a SIGHUP and SIGCONT to the foreground process group
1178 * (2) Clears the tty from being controlling the session
1179 * (3) Clears the controlling tty for all processes in the
1180 * session group.
1182 * The argument on_exit is set to 1 if called when a process is
1183 * exiting; it is 0 if called by the ioctl TIOCNOTTY.
1184 */
1185 void disassociate_ctty(int on_exit)
1187 struct tty_struct *tty;
1188 struct task_struct *p;
1189 int tty_pgrp = -1;
1191 lock_kernel();
1193 down(&tty_sem);
1194 tty = current->signal->tty;
1195 if (tty) {
1196 tty_pgrp = tty->pgrp;
1197 up(&tty_sem);
1198 if (on_exit && tty->driver->type != TTY_DRIVER_TYPE_PTY)
1199 tty_vhangup(tty);
1200 } else {
1201 if (current->signal->tty_old_pgrp) {
1202 kill_pg(current->signal->tty_old_pgrp, SIGHUP, on_exit);
1203 kill_pg(current->signal->tty_old_pgrp, SIGCONT, on_exit);
1205 up(&tty_sem);
1206 unlock_kernel();
1207 return;
1209 if (tty_pgrp > 0) {
1210 kill_pg(tty_pgrp, SIGHUP, on_exit);
1211 if (!on_exit)
1212 kill_pg(tty_pgrp, SIGCONT, on_exit);
1215 /* Must lock changes to tty_old_pgrp */
1216 down(&tty_sem);
1217 current->signal->tty_old_pgrp = 0;
1218 tty->session = 0;
1219 tty->pgrp = -1;
1221 /* Now clear signal->tty under the lock */
1222 read_lock(&tasklist_lock);
1223 do_each_task_pid(current->signal->session, PIDTYPE_SID, p) {
1224 p->signal->tty = NULL;
1225 } while_each_task_pid(current->signal->session, PIDTYPE_SID, p);
1226 read_unlock(&tasklist_lock);
1227 up(&tty_sem);
1228 unlock_kernel();
1231 void stop_tty(struct tty_struct *tty)
1233 if (tty->stopped)
1234 return;
1235 tty->stopped = 1;
1236 if (tty->link && tty->link->packet) {
1237 tty->ctrl_status &= ~TIOCPKT_START;
1238 tty->ctrl_status |= TIOCPKT_STOP;
1239 wake_up_interruptible(&tty->link->read_wait);
1241 if (tty->driver->stop)
1242 (tty->driver->stop)(tty);
1245 EXPORT_SYMBOL(stop_tty);
1247 void start_tty(struct tty_struct *tty)
1249 if (!tty->stopped || tty->flow_stopped)
1250 return;
1251 tty->stopped = 0;
1252 if (tty->link && tty->link->packet) {
1253 tty->ctrl_status &= ~TIOCPKT_STOP;
1254 tty->ctrl_status |= TIOCPKT_START;
1255 wake_up_interruptible(&tty->link->read_wait);
1257 if (tty->driver->start)
1258 (tty->driver->start)(tty);
1260 /* If we have a running line discipline it may need kicking */
1261 tty_wakeup(tty);
1262 wake_up_interruptible(&tty->write_wait);
1265 EXPORT_SYMBOL(start_tty);
1267 static ssize_t tty_read(struct file * file, char __user * buf, size_t count,
1268 loff_t *ppos)
1270 int i;
1271 struct tty_struct * tty;
1272 struct inode *inode;
1273 struct tty_ldisc *ld;
1275 tty = (struct tty_struct *)file->private_data;
1276 inode = file->f_dentry->d_inode;
1277 if (tty_paranoia_check(tty, inode, "tty_read"))
1278 return -EIO;
1279 if (!tty || (test_bit(TTY_IO_ERROR, &tty->flags)))
1280 return -EIO;
1282 /* We want to wait for the line discipline to sort out in this
1283 situation */
1284 ld = tty_ldisc_ref_wait(tty);
1285 lock_kernel();
1286 if (ld->read)
1287 i = (ld->read)(tty,file,buf,count);
1288 else
1289 i = -EIO;
1290 tty_ldisc_deref(ld);
1291 unlock_kernel();
1292 if (i > 0)
1293 inode->i_atime = current_fs_time(inode->i_sb);
1294 return i;
1297 /*
1298 * Split writes up in sane blocksizes to avoid
1299 * denial-of-service type attacks
1300 */
1301 static inline ssize_t do_tty_write(
1302 ssize_t (*write)(struct tty_struct *, struct file *, const unsigned char *, size_t),
1303 struct tty_struct *tty,
1304 struct file *file,
1305 const char __user *buf,
1306 size_t count)
1308 ssize_t ret = 0, written = 0;
1309 unsigned int chunk;
1311 if (down_interruptible(&tty->atomic_write)) {
1312 return -ERESTARTSYS;
1315 /*
1316 * We chunk up writes into a temporary buffer. This
1317 * simplifies low-level drivers immensely, since they
1318 * don't have locking issues and user mode accesses.
1320 * But if TTY_NO_WRITE_SPLIT is set, we should use a
1321 * big chunk-size..
1323 * The default chunk-size is 2kB, because the NTTY
1324 * layer has problems with bigger chunks. It will
1325 * claim to be able to handle more characters than
1326 * it actually does.
1327 */
1328 chunk = 2048;
1329 if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags))
1330 chunk = 65536;
1331 if (count < chunk)
1332 chunk = count;
1334 /* write_buf/write_cnt is protected by the atomic_write semaphore */
1335 if (tty->write_cnt < chunk) {
1336 unsigned char *buf;
1338 if (chunk < 1024)
1339 chunk = 1024;
1341 buf = kmalloc(chunk, GFP_KERNEL);
1342 if (!buf) {
1343 up(&tty->atomic_write);
1344 return -ENOMEM;
1346 kfree(tty->write_buf);
1347 tty->write_cnt = chunk;
1348 tty->write_buf = buf;
1351 /* Do the write .. */
1352 for (;;) {
1353 size_t size = count;
1354 if (size > chunk)
1355 size = chunk;
1356 ret = -EFAULT;
1357 if (copy_from_user(tty->write_buf, buf, size))
1358 break;
1359 lock_kernel();
1360 ret = write(tty, file, tty->write_buf, size);
1361 unlock_kernel();
1362 if (ret <= 0)
1363 break;
1364 written += ret;
1365 buf += ret;
1366 count -= ret;
1367 if (!count)
1368 break;
1369 ret = -ERESTARTSYS;
1370 if (signal_pending(current))
1371 break;
1372 cond_resched();
1374 if (written) {
1375 struct inode *inode = file->f_dentry->d_inode;
1376 inode->i_mtime = current_fs_time(inode->i_sb);
1377 ret = written;
1379 up(&tty->atomic_write);
1380 return ret;
1384 static ssize_t tty_write(struct file * file, const char __user * buf, size_t count,
1385 loff_t *ppos)
1387 struct tty_struct * tty;
1388 struct inode *inode = file->f_dentry->d_inode;
1389 ssize_t ret;
1390 struct tty_ldisc *ld;
1392 tty = (struct tty_struct *)file->private_data;
1393 if (tty_paranoia_check(tty, inode, "tty_write"))
1394 return -EIO;
1395 if (!tty || !tty->driver->write || (test_bit(TTY_IO_ERROR, &tty->flags)))
1396 return -EIO;
1398 ld = tty_ldisc_ref_wait(tty);
1399 if (!ld->write)
1400 ret = -EIO;
1401 else
1402 ret = do_tty_write(ld->write, tty, file, buf, count);
1403 tty_ldisc_deref(ld);
1404 return ret;
1407 ssize_t redirected_tty_write(struct file * file, const char __user * buf, size_t count,
1408 loff_t *ppos)
1410 struct file *p = NULL;
1412 spin_lock(&redirect_lock);
1413 if (redirect) {
1414 get_file(redirect);
1415 p = redirect;
1417 spin_unlock(&redirect_lock);
1419 if (p) {
1420 ssize_t res;
1421 res = vfs_write(p, buf, count, &p->f_pos);
1422 fput(p);
1423 return res;
1426 return tty_write(file, buf, count, ppos);
1429 static char ptychar[] = "pqrstuvwxyzabcde";
1431 static inline void pty_line_name(struct tty_driver *driver, int index, char *p)
1433 int i = index + driver->name_base;
1434 /* ->name is initialized to "ttyp", but "tty" is expected */
1435 sprintf(p, "%s%c%x",
1436 driver->subtype == PTY_TYPE_SLAVE ? "tty" : driver->name,
1437 ptychar[i >> 4 & 0xf], i & 0xf);
1440 static inline void tty_line_name(struct tty_driver *driver, int index, char *p)
1442 sprintf(p, "%s%d", driver->name, index + driver->name_base);
1445 /*
1446 * WSH 06/09/97: Rewritten to remove races and properly clean up after a
1447 * failed open. The new code protects the open with a semaphore, so it's
1448 * really quite straightforward. The semaphore locking can probably be
1449 * relaxed for the (most common) case of reopening a tty.
1450 */
1451 static int init_dev(struct tty_driver *driver, int idx,
1452 struct tty_struct **ret_tty)
1454 struct tty_struct *tty, *o_tty;
1455 struct termios *tp, **tp_loc, *o_tp, **o_tp_loc;
1456 struct termios *ltp, **ltp_loc, *o_ltp, **o_ltp_loc;
1457 int retval=0;
1459 /* check whether we're reopening an existing tty */
1460 if (driver->flags & TTY_DRIVER_DEVPTS_MEM) {
1461 tty = devpts_get_tty(idx);
1462 if (tty && driver->subtype == PTY_TYPE_MASTER)
1463 tty = tty->link;
1464 } else {
1465 tty = driver->ttys[idx];
1467 if (tty) goto fast_track;
1469 /*
1470 * First time open is complex, especially for PTY devices.
1471 * This code guarantees that either everything succeeds and the
1472 * TTY is ready for operation, or else the table slots are vacated
1473 * and the allocated memory released. (Except that the termios
1474 * and locked termios may be retained.)
1475 */
1477 if (!try_module_get(driver->owner)) {
1478 retval = -ENODEV;
1479 goto end_init;
1482 o_tty = NULL;
1483 tp = o_tp = NULL;
1484 ltp = o_ltp = NULL;
1486 tty = alloc_tty_struct();
1487 if(!tty)
1488 goto fail_no_mem;
1489 initialize_tty_struct(tty);
1490 tty->driver = driver;
1491 tty->index = idx;
1492 tty_line_name(driver, idx, tty->name);
1494 if (driver->flags & TTY_DRIVER_DEVPTS_MEM) {
1495 tp_loc = &tty->termios;
1496 ltp_loc = &tty->termios_locked;
1497 } else {
1498 tp_loc = &driver->termios[idx];
1499 ltp_loc = &driver->termios_locked[idx];
1502 if (!*tp_loc) {
1503 tp = (struct termios *) kmalloc(sizeof(struct termios),
1504 GFP_KERNEL);
1505 if (!tp)
1506 goto free_mem_out;
1507 *tp = driver->init_termios;
1510 if (!*ltp_loc) {
1511 ltp = (struct termios *) kmalloc(sizeof(struct termios),
1512 GFP_KERNEL);
1513 if (!ltp)
1514 goto free_mem_out;
1515 memset(ltp, 0, sizeof(struct termios));
1518 if (driver->type == TTY_DRIVER_TYPE_PTY) {
1519 o_tty = alloc_tty_struct();
1520 if (!o_tty)
1521 goto free_mem_out;
1522 initialize_tty_struct(o_tty);
1523 o_tty->driver = driver->other;
1524 o_tty->index = idx;
1525 tty_line_name(driver->other, idx, o_tty->name);
1527 if (driver->flags & TTY_DRIVER_DEVPTS_MEM) {
1528 o_tp_loc = &o_tty->termios;
1529 o_ltp_loc = &o_tty->termios_locked;
1530 } else {
1531 o_tp_loc = &driver->other->termios[idx];
1532 o_ltp_loc = &driver->other->termios_locked[idx];
1535 if (!*o_tp_loc) {
1536 o_tp = (struct termios *)
1537 kmalloc(sizeof(struct termios), GFP_KERNEL);
1538 if (!o_tp)
1539 goto free_mem_out;
1540 *o_tp = driver->other->init_termios;
1543 if (!*o_ltp_loc) {
1544 o_ltp = (struct termios *)
1545 kmalloc(sizeof(struct termios), GFP_KERNEL);
1546 if (!o_ltp)
1547 goto free_mem_out;
1548 memset(o_ltp, 0, sizeof(struct termios));
1551 /*
1552 * Everything allocated ... set up the o_tty structure.
1553 */
1554 if (!(driver->other->flags & TTY_DRIVER_DEVPTS_MEM)) {
1555 driver->other->ttys[idx] = o_tty;
1557 if (!*o_tp_loc)
1558 *o_tp_loc = o_tp;
1559 if (!*o_ltp_loc)
1560 *o_ltp_loc = o_ltp;
1561 o_tty->termios = *o_tp_loc;
1562 o_tty->termios_locked = *o_ltp_loc;
1563 driver->other->refcount++;
1564 if (driver->subtype == PTY_TYPE_MASTER)
1565 o_tty->count++;
1567 /* Establish the links in both directions */
1568 tty->link = o_tty;
1569 o_tty->link = tty;
1572 /*
1573 * All structures have been allocated, so now we install them.
1574 * Failures after this point use release_mem to clean up, so
1575 * there's no need to null out the local pointers.
1576 */
1577 if (!(driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
1578 driver->ttys[idx] = tty;
1581 if (!*tp_loc)
1582 *tp_loc = tp;
1583 if (!*ltp_loc)
1584 *ltp_loc = ltp;
1585 tty->termios = *tp_loc;
1586 tty->termios_locked = *ltp_loc;
1587 driver->refcount++;
1588 tty->count++;
1590 /*
1591 * Structures all installed ... call the ldisc open routines.
1592 * If we fail here just call release_mem to clean up. No need
1593 * to decrement the use counts, as release_mem doesn't care.
1594 */
1596 if (tty->ldisc.open) {
1597 retval = (tty->ldisc.open)(tty);
1598 if (retval)
1599 goto release_mem_out;
1601 if (o_tty && o_tty->ldisc.open) {
1602 retval = (o_tty->ldisc.open)(o_tty);
1603 if (retval) {
1604 if (tty->ldisc.close)
1605 (tty->ldisc.close)(tty);
1606 goto release_mem_out;
1608 tty_ldisc_enable(o_tty);
1610 tty_ldisc_enable(tty);
1611 goto success;
1613 /*
1614 * This fast open can be used if the tty is already open.
1615 * No memory is allocated, and the only failures are from
1616 * attempting to open a closing tty or attempting multiple
1617 * opens on a pty master.
1618 */
1619 fast_track:
1620 if (test_bit(TTY_CLOSING, &tty->flags)) {
1621 retval = -EIO;
1622 goto end_init;
1624 if (driver->type == TTY_DRIVER_TYPE_PTY &&
1625 driver->subtype == PTY_TYPE_MASTER) {
1626 /*
1627 * special case for PTY masters: only one open permitted,
1628 * and the slave side open count is incremented as well.
1629 */
1630 if (tty->count) {
1631 retval = -EIO;
1632 goto end_init;
1634 tty->link->count++;
1636 tty->count++;
1637 tty->driver = driver; /* N.B. why do this every time?? */
1639 /* FIXME */
1640 if(!test_bit(TTY_LDISC, &tty->flags))
1641 printk(KERN_ERR "init_dev but no ldisc\n");
1642 success:
1643 *ret_tty = tty;
1645 /* All paths come through here to release the semaphore */
1646 end_init:
1647 return retval;
1649 /* Release locally allocated memory ... nothing placed in slots */
1650 free_mem_out:
1651 kfree(o_tp);
1652 if (o_tty)
1653 free_tty_struct(o_tty);
1654 kfree(ltp);
1655 kfree(tp);
1656 free_tty_struct(tty);
1658 fail_no_mem:
1659 module_put(driver->owner);
1660 retval = -ENOMEM;
1661 goto end_init;
1663 /* call the tty release_mem routine to clean out this slot */
1664 release_mem_out:
1665 printk(KERN_INFO "init_dev: ldisc open failed, "
1666 "clearing slot %d\n", idx);
1667 release_mem(tty, idx);
1668 goto end_init;
1671 /*
1672 * Releases memory associated with a tty structure, and clears out the
1673 * driver table slots.
1674 */
1675 static void release_mem(struct tty_struct *tty, int idx)
1677 struct tty_struct *o_tty;
1678 struct termios *tp;
1679 int devpts = tty->driver->flags & TTY_DRIVER_DEVPTS_MEM;
1681 if ((o_tty = tty->link) != NULL) {
1682 if (!devpts)
1683 o_tty->driver->ttys[idx] = NULL;
1684 if (o_tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) {
1685 tp = o_tty->termios;
1686 if (!devpts)
1687 o_tty->driver->termios[idx] = NULL;
1688 kfree(tp);
1690 tp = o_tty->termios_locked;
1691 if (!devpts)
1692 o_tty->driver->termios_locked[idx] = NULL;
1693 kfree(tp);
1695 o_tty->magic = 0;
1696 o_tty->driver->refcount--;
1697 file_list_lock();
1698 list_del_init(&o_tty->tty_files);
1699 file_list_unlock();
1700 free_tty_struct(o_tty);
1703 if (!devpts)
1704 tty->driver->ttys[idx] = NULL;
1705 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) {
1706 tp = tty->termios;
1707 if (!devpts)
1708 tty->driver->termios[idx] = NULL;
1709 kfree(tp);
1711 tp = tty->termios_locked;
1712 if (!devpts)
1713 tty->driver->termios_locked[idx] = NULL;
1714 kfree(tp);
1717 tty->magic = 0;
1718 tty->driver->refcount--;
1719 file_list_lock();
1720 list_del_init(&tty->tty_files);
1721 file_list_unlock();
1722 module_put(tty->driver->owner);
1723 free_tty_struct(tty);
1726 /*
1727 * Even releasing the tty structures is a tricky business.. We have
1728 * to be very careful that the structures are all released at the
1729 * same time, as interrupts might otherwise get the wrong pointers.
1731 * WSH 09/09/97: rewritten to avoid some nasty race conditions that could
1732 * lead to double frees or releasing memory still in use.
1733 */
1734 static void release_dev(struct file * filp)
1736 struct tty_struct *tty, *o_tty;
1737 int pty_master, tty_closing, o_tty_closing, do_sleep;
1738 int devpts_master, devpts;
1739 int idx;
1740 char buf[64];
1741 unsigned long flags;
1743 tty = (struct tty_struct *)filp->private_data;
1744 if (tty_paranoia_check(tty, filp->f_dentry->d_inode, "release_dev"))
1745 return;
1747 check_tty_count(tty, "release_dev");
1749 tty_fasync(-1, filp, 0);
1751 idx = tty->index;
1752 pty_master = (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
1753 tty->driver->subtype == PTY_TYPE_MASTER);
1754 devpts = (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM) != 0;
1755 devpts_master = pty_master && devpts;
1756 o_tty = tty->link;
1758 #ifdef TTY_PARANOIA_CHECK
1759 if (idx < 0 || idx >= tty->driver->num) {
1760 printk(KERN_DEBUG "release_dev: bad idx when trying to "
1761 "free (%s)\n", tty->name);
1762 return;
1764 if (!(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
1765 if (tty != tty->driver->ttys[idx]) {
1766 printk(KERN_DEBUG "release_dev: driver.table[%d] not tty "
1767 "for (%s)\n", idx, tty->name);
1768 return;
1770 if (tty->termios != tty->driver->termios[idx]) {
1771 printk(KERN_DEBUG "release_dev: driver.termios[%d] not termios "
1772 "for (%s)\n",
1773 idx, tty->name);
1774 return;
1776 if (tty->termios_locked != tty->driver->termios_locked[idx]) {
1777 printk(KERN_DEBUG "release_dev: driver.termios_locked[%d] not "
1778 "termios_locked for (%s)\n",
1779 idx, tty->name);
1780 return;
1783 #endif
1785 #ifdef TTY_DEBUG_HANGUP
1786 printk(KERN_DEBUG "release_dev of %s (tty count=%d)...",
1787 tty_name(tty, buf), tty->count);
1788 #endif
1790 #ifdef TTY_PARANOIA_CHECK
1791 if (tty->driver->other &&
1792 !(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
1793 if (o_tty != tty->driver->other->ttys[idx]) {
1794 printk(KERN_DEBUG "release_dev: other->table[%d] "
1795 "not o_tty for (%s)\n",
1796 idx, tty->name);
1797 return;
1799 if (o_tty->termios != tty->driver->other->termios[idx]) {
1800 printk(KERN_DEBUG "release_dev: other->termios[%d] "
1801 "not o_termios for (%s)\n",
1802 idx, tty->name);
1803 return;
1805 if (o_tty->termios_locked !=
1806 tty->driver->other->termios_locked[idx]) {
1807 printk(KERN_DEBUG "release_dev: other->termios_locked["
1808 "%d] not o_termios_locked for (%s)\n",
1809 idx, tty->name);
1810 return;
1812 if (o_tty->link != tty) {
1813 printk(KERN_DEBUG "release_dev: bad pty pointers\n");
1814 return;
1817 #endif
1818 if (tty->driver->close)
1819 tty->driver->close(tty, filp);
1821 /*
1822 * Sanity check: if tty->count is going to zero, there shouldn't be
1823 * any waiters on tty->read_wait or tty->write_wait. We test the
1824 * wait queues and kick everyone out _before_ actually starting to
1825 * close. This ensures that we won't block while releasing the tty
1826 * structure.
1828 * The test for the o_tty closing is necessary, since the master and
1829 * slave sides may close in any order. If the slave side closes out
1830 * first, its count will be one, since the master side holds an open.
1831 * Thus this test wouldn't be triggered at the time the slave closes,
1832 * so we do it now.
1834 * Note that it's possible for the tty to be opened again while we're
1835 * flushing out waiters. By recalculating the closing flags before
1836 * each iteration we avoid any problems.
1837 */
1838 while (1) {
1839 /* Guard against races with tty->count changes elsewhere and
1840 opens on /dev/tty */
1842 down(&tty_sem);
1843 tty_closing = tty->count <= 1;
1844 o_tty_closing = o_tty &&
1845 (o_tty->count <= (pty_master ? 1 : 0));
1846 do_sleep = 0;
1848 if (tty_closing) {
1849 if (waitqueue_active(&tty->read_wait)) {
1850 wake_up(&tty->read_wait);
1851 do_sleep++;
1853 if (waitqueue_active(&tty->write_wait)) {
1854 wake_up(&tty->write_wait);
1855 do_sleep++;
1858 if (o_tty_closing) {
1859 if (waitqueue_active(&o_tty->read_wait)) {
1860 wake_up(&o_tty->read_wait);
1861 do_sleep++;
1863 if (waitqueue_active(&o_tty->write_wait)) {
1864 wake_up(&o_tty->write_wait);
1865 do_sleep++;
1868 if (!do_sleep)
1869 break;
1871 printk(KERN_WARNING "release_dev: %s: read/write wait queue "
1872 "active!\n", tty_name(tty, buf));
1873 up(&tty_sem);
1874 schedule();
1877 /*
1878 * The closing flags are now consistent with the open counts on
1879 * both sides, and we've completed the last operation that could
1880 * block, so it's safe to proceed with closing.
1881 */
1882 if (pty_master) {
1883 if (--o_tty->count < 0) {
1884 printk(KERN_WARNING "release_dev: bad pty slave count "
1885 "(%d) for %s\n",
1886 o_tty->count, tty_name(o_tty, buf));
1887 o_tty->count = 0;
1890 if (--tty->count < 0) {
1891 printk(KERN_WARNING "release_dev: bad tty->count (%d) for %s\n",
1892 tty->count, tty_name(tty, buf));
1893 tty->count = 0;
1896 /*
1897 * We've decremented tty->count, so we need to remove this file
1898 * descriptor off the tty->tty_files list; this serves two
1899 * purposes:
1900 * - check_tty_count sees the correct number of file descriptors
1901 * associated with this tty.
1902 * - do_tty_hangup no longer sees this file descriptor as
1903 * something that needs to be handled for hangups.
1904 */
1905 file_kill(filp);
1906 filp->private_data = NULL;
1908 /*
1909 * Perform some housekeeping before deciding whether to return.
1911 * Set the TTY_CLOSING flag if this was the last open. In the
1912 * case of a pty we may have to wait around for the other side
1913 * to close, and TTY_CLOSING makes sure we can't be reopened.
1914 */
1915 if(tty_closing)
1916 set_bit(TTY_CLOSING, &tty->flags);
1917 if(o_tty_closing)
1918 set_bit(TTY_CLOSING, &o_tty->flags);
1920 /*
1921 * If _either_ side is closing, make sure there aren't any
1922 * processes that still think tty or o_tty is their controlling
1923 * tty.
1924 */
1925 if (tty_closing || o_tty_closing) {
1926 struct task_struct *p;
1928 read_lock(&tasklist_lock);
1929 do_each_task_pid(tty->session, PIDTYPE_SID, p) {
1930 p->signal->tty = NULL;
1931 } while_each_task_pid(tty->session, PIDTYPE_SID, p);
1932 if (o_tty)
1933 do_each_task_pid(o_tty->session, PIDTYPE_SID, p) {
1934 p->signal->tty = NULL;
1935 } while_each_task_pid(o_tty->session, PIDTYPE_SID, p);
1936 read_unlock(&tasklist_lock);
1939 up(&tty_sem);
1941 /* check whether both sides are closing ... */
1942 if (!tty_closing || (o_tty && !o_tty_closing))
1943 return;
1945 #ifdef TTY_DEBUG_HANGUP
1946 printk(KERN_DEBUG "freeing tty structure...");
1947 #endif
1948 /*
1949 * Prevent flush_to_ldisc() from rescheduling the work for later. Then
1950 * kill any delayed work. As this is the final close it does not
1951 * race with the set_ldisc code path.
1952 */
1953 clear_bit(TTY_LDISC, &tty->flags);
1954 clear_bit(TTY_DONT_FLIP, &tty->flags);
1955 cancel_delayed_work(&tty->buf.work);
1957 /*
1958 * Wait for ->hangup_work and ->buf.work handlers to terminate
1959 */
1961 flush_scheduled_work();
1963 /*
1964 * Wait for any short term users (we know they are just driver
1965 * side waiters as the file is closing so user count on the file
1966 * side is zero.
1967 */
1968 spin_lock_irqsave(&tty_ldisc_lock, flags);
1969 while(tty->ldisc.refcount)
1971 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
1972 wait_event(tty_ldisc_wait, tty->ldisc.refcount == 0);
1973 spin_lock_irqsave(&tty_ldisc_lock, flags);
1975 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
1976 /*
1977 * Shutdown the current line discipline, and reset it to N_TTY.
1978 * N.B. why reset ldisc when we're releasing the memory??
1980 * FIXME: this MUST get fixed for the new reflocking
1981 */
1982 if (tty->ldisc.close)
1983 (tty->ldisc.close)(tty);
1984 tty_ldisc_put(tty->ldisc.num);
1986 /*
1987 * Switch the line discipline back
1988 */
1989 tty_ldisc_assign(tty, tty_ldisc_get(N_TTY));
1990 tty_set_termios_ldisc(tty,N_TTY);
1991 if (o_tty) {
1992 /* FIXME: could o_tty be in setldisc here ? */
1993 clear_bit(TTY_LDISC, &o_tty->flags);
1994 if (o_tty->ldisc.close)
1995 (o_tty->ldisc.close)(o_tty);
1996 tty_ldisc_put(o_tty->ldisc.num);
1997 tty_ldisc_assign(o_tty, tty_ldisc_get(N_TTY));
1998 tty_set_termios_ldisc(o_tty,N_TTY);
2000 /*
2001 * The release_mem function takes care of the details of clearing
2002 * the slots and preserving the termios structure.
2003 */
2004 release_mem(tty, idx);
2006 #ifdef CONFIG_UNIX98_PTYS
2007 /* Make this pty number available for reallocation */
2008 if (devpts) {
2009 down(&allocated_ptys_lock);
2010 idr_remove(&allocated_ptys, idx);
2011 up(&allocated_ptys_lock);
2013 #endif
2017 /*
2018 * tty_open and tty_release keep up the tty count that contains the
2019 * number of opens done on a tty. We cannot use the inode-count, as
2020 * different inodes might point to the same tty.
2022 * Open-counting is needed for pty masters, as well as for keeping
2023 * track of serial lines: DTR is dropped when the last close happens.
2024 * (This is not done solely through tty->count, now. - Ted 1/27/92)
2026 * The termios state of a pty is reset on first open so that
2027 * settings don't persist across reuse.
2028 */
2029 static int tty_open(struct inode * inode, struct file * filp)
2031 struct tty_struct *tty;
2032 int noctty, retval;
2033 struct tty_driver *driver;
2034 int index;
2035 dev_t device = inode->i_rdev;
2036 unsigned short saved_flags = filp->f_flags;
2038 nonseekable_open(inode, filp);
2040 retry_open:
2041 noctty = filp->f_flags & O_NOCTTY;
2042 index = -1;
2043 retval = 0;
2045 down(&tty_sem);
2047 if (device == MKDEV(TTYAUX_MAJOR,0)) {
2048 if (!current->signal->tty) {
2049 up(&tty_sem);
2050 return -ENXIO;
2052 driver = current->signal->tty->driver;
2053 index = current->signal->tty->index;
2054 filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */
2055 /* noctty = 1; */
2056 goto got_driver;
2058 #ifdef CONFIG_VT
2059 if (console_use_vt && (device == MKDEV(TTY_MAJOR,0))) {
2060 extern struct tty_driver *console_driver;
2061 driver = console_driver;
2062 index = fg_console;
2063 noctty = 1;
2064 goto got_driver;
2066 #endif
2067 if (device == MKDEV(TTYAUX_MAJOR,1)) {
2068 driver = console_device(&index);
2069 if (driver) {
2070 /* Don't let /dev/console block */
2071 filp->f_flags |= O_NONBLOCK;
2072 noctty = 1;
2073 goto got_driver;
2075 up(&tty_sem);
2076 return -ENODEV;
2079 driver = get_tty_driver(device, &index);
2080 if (!driver) {
2081 up(&tty_sem);
2082 return -ENODEV;
2084 got_driver:
2085 retval = init_dev(driver, index, &tty);
2086 up(&tty_sem);
2087 if (retval)
2088 return retval;
2090 filp->private_data = tty;
2091 file_move(filp, &tty->tty_files);
2092 check_tty_count(tty, "tty_open");
2093 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
2094 tty->driver->subtype == PTY_TYPE_MASTER)
2095 noctty = 1;
2096 #ifdef TTY_DEBUG_HANGUP
2097 printk(KERN_DEBUG "opening %s...", tty->name);
2098 #endif
2099 if (!retval) {
2100 if (tty->driver->open)
2101 retval = tty->driver->open(tty, filp);
2102 else
2103 retval = -ENODEV;
2105 filp->f_flags = saved_flags;
2107 if (!retval && test_bit(TTY_EXCLUSIVE, &tty->flags) && !capable(CAP_SYS_ADMIN))
2108 retval = -EBUSY;
2110 if (retval) {
2111 #ifdef TTY_DEBUG_HANGUP
2112 printk(KERN_DEBUG "error %d in opening %s...", retval,
2113 tty->name);
2114 #endif
2115 release_dev(filp);
2116 if (retval != -ERESTARTSYS)
2117 return retval;
2118 if (signal_pending(current))
2119 return retval;
2120 schedule();
2121 /*
2122 * Need to reset f_op in case a hangup happened.
2123 */
2124 if (filp->f_op == &hung_up_tty_fops)
2125 filp->f_op = &tty_fops;
2126 goto retry_open;
2128 if (!noctty &&
2129 current->signal->leader &&
2130 !current->signal->tty &&
2131 tty->session == 0) {
2132 task_lock(current);
2133 current->signal->tty = tty;
2134 task_unlock(current);
2135 current->signal->tty_old_pgrp = 0;
2136 tty->session = current->signal->session;
2137 tty->pgrp = process_group(current);
2139 return 0;
2142 #ifdef CONFIG_UNIX98_PTYS
2143 static int ptmx_open(struct inode * inode, struct file * filp)
2145 struct tty_struct *tty;
2146 int retval;
2147 int index;
2148 int idr_ret;
2150 nonseekable_open(inode, filp);
2152 /* find a device that is not in use. */
2153 down(&allocated_ptys_lock);
2154 if (!idr_pre_get(&allocated_ptys, GFP_KERNEL)) {
2155 up(&allocated_ptys_lock);
2156 return -ENOMEM;
2158 idr_ret = idr_get_new(&allocated_ptys, NULL, &index);
2159 if (idr_ret < 0) {
2160 up(&allocated_ptys_lock);
2161 if (idr_ret == -EAGAIN)
2162 return -ENOMEM;
2163 return -EIO;
2165 if (index >= pty_limit) {
2166 idr_remove(&allocated_ptys, index);
2167 up(&allocated_ptys_lock);
2168 return -EIO;
2170 up(&allocated_ptys_lock);
2172 down(&tty_sem);
2173 retval = init_dev(ptm_driver, index, &tty);
2174 up(&tty_sem);
2176 if (retval)
2177 goto out;
2179 set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
2180 filp->private_data = tty;
2181 file_move(filp, &tty->tty_files);
2183 retval = -ENOMEM;
2184 if (devpts_pty_new(tty->link))
2185 goto out1;
2187 check_tty_count(tty, "tty_open");
2188 retval = ptm_driver->open(tty, filp);
2189 if (!retval)
2190 return 0;
2191 out1:
2192 release_dev(filp);
2193 out:
2194 down(&allocated_ptys_lock);
2195 idr_remove(&allocated_ptys, index);
2196 up(&allocated_ptys_lock);
2197 return retval;
2199 #endif
2201 static int tty_release(struct inode * inode, struct file * filp)
2203 lock_kernel();
2204 release_dev(filp);
2205 unlock_kernel();
2206 return 0;
2209 /* No kernel lock held - fine */
2210 static unsigned int tty_poll(struct file * filp, poll_table * wait)
2212 struct tty_struct * tty;
2213 struct tty_ldisc *ld;
2214 int ret = 0;
2216 tty = (struct tty_struct *)filp->private_data;
2217 if (tty_paranoia_check(tty, filp->f_dentry->d_inode, "tty_poll"))
2218 return 0;
2220 ld = tty_ldisc_ref_wait(tty);
2221 if (ld->poll)
2222 ret = (ld->poll)(tty, filp, wait);
2223 tty_ldisc_deref(ld);
2224 return ret;
2227 static int tty_fasync(int fd, struct file * filp, int on)
2229 struct tty_struct * tty;
2230 int retval;
2232 tty = (struct tty_struct *)filp->private_data;
2233 if (tty_paranoia_check(tty, filp->f_dentry->d_inode, "tty_fasync"))
2234 return 0;
2236 retval = fasync_helper(fd, filp, on, &tty->fasync);
2237 if (retval <= 0)
2238 return retval;
2240 if (on) {
2241 if (!waitqueue_active(&tty->read_wait))
2242 tty->minimum_to_wake = 1;
2243 retval = f_setown(filp, (-tty->pgrp) ? : current->pid, 0);
2244 if (retval)
2245 return retval;
2246 } else {
2247 if (!tty->fasync && !waitqueue_active(&tty->read_wait))
2248 tty->minimum_to_wake = N_TTY_BUF_SIZE;
2250 return 0;
2253 static int tiocsti(struct tty_struct *tty, char __user *p)
2255 char ch, mbz = 0;
2256 struct tty_ldisc *ld;
2258 if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN))
2259 return -EPERM;
2260 if (get_user(ch, p))
2261 return -EFAULT;
2262 ld = tty_ldisc_ref_wait(tty);
2263 ld->receive_buf(tty, &ch, &mbz, 1);
2264 tty_ldisc_deref(ld);
2265 return 0;
2268 static int tiocgwinsz(struct tty_struct *tty, struct winsize __user * arg)
2270 if (copy_to_user(arg, &tty->winsize, sizeof(*arg)))
2271 return -EFAULT;
2272 return 0;
2275 static int tiocswinsz(struct tty_struct *tty, struct tty_struct *real_tty,
2276 struct winsize __user * arg)
2278 struct winsize tmp_ws;
2280 if (copy_from_user(&tmp_ws, arg, sizeof(*arg)))
2281 return -EFAULT;
2282 if (!memcmp(&tmp_ws, &tty->winsize, sizeof(*arg)))
2283 return 0;
2284 #ifdef CONFIG_VT
2285 if (tty->driver->type == TTY_DRIVER_TYPE_CONSOLE) {
2286 int rc;
2288 acquire_console_sem();
2289 rc = vc_resize(tty->driver_data, tmp_ws.ws_col, tmp_ws.ws_row);
2290 release_console_sem();
2291 if (rc)
2292 return -ENXIO;
2294 #endif
2295 if (tty->pgrp > 0)
2296 kill_pg(tty->pgrp, SIGWINCH, 1);
2297 if ((real_tty->pgrp != tty->pgrp) && (real_tty->pgrp > 0))
2298 kill_pg(real_tty->pgrp, SIGWINCH, 1);
2299 tty->winsize = tmp_ws;
2300 real_tty->winsize = tmp_ws;
2301 return 0;
2304 static int tioccons(struct file *file)
2306 if (!capable(CAP_SYS_ADMIN))
2307 return -EPERM;
2308 if (file->f_op->write == redirected_tty_write) {
2309 struct file *f;
2310 spin_lock(&redirect_lock);
2311 f = redirect;
2312 redirect = NULL;
2313 spin_unlock(&redirect_lock);
2314 if (f)
2315 fput(f);
2316 return 0;
2318 spin_lock(&redirect_lock);
2319 if (redirect) {
2320 spin_unlock(&redirect_lock);
2321 return -EBUSY;
2323 get_file(file);
2324 redirect = file;
2325 spin_unlock(&redirect_lock);
2326 return 0;
2330 static int fionbio(struct file *file, int __user *p)
2332 int nonblock;
2334 if (get_user(nonblock, p))
2335 return -EFAULT;
2337 if (nonblock)
2338 file->f_flags |= O_NONBLOCK;
2339 else
2340 file->f_flags &= ~O_NONBLOCK;
2341 return 0;
2344 static int tiocsctty(struct tty_struct *tty, int arg)
2346 task_t *p;
2348 if (current->signal->leader &&
2349 (current->signal->session == tty->session))
2350 return 0;
2351 /*
2352 * The process must be a session leader and
2353 * not have a controlling tty already.
2354 */
2355 if (!current->signal->leader || current->signal->tty)
2356 return -EPERM;
2357 if (tty->session > 0) {
2358 /*
2359 * This tty is already the controlling
2360 * tty for another session group!
2361 */
2362 if ((arg == 1) && capable(CAP_SYS_ADMIN)) {
2363 /*
2364 * Steal it away
2365 */
2367 read_lock(&tasklist_lock);
2368 do_each_task_pid(tty->session, PIDTYPE_SID, p) {
2369 p->signal->tty = NULL;
2370 } while_each_task_pid(tty->session, PIDTYPE_SID, p);
2371 read_unlock(&tasklist_lock);
2372 } else
2373 return -EPERM;
2375 task_lock(current);
2376 current->signal->tty = tty;
2377 task_unlock(current);
2378 current->signal->tty_old_pgrp = 0;
2379 tty->session = current->signal->session;
2380 tty->pgrp = process_group(current);
2381 return 0;
2384 static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2386 /*
2387 * (tty == real_tty) is a cheap way of
2388 * testing if the tty is NOT a master pty.
2389 */
2390 if (tty == real_tty && current->signal->tty != real_tty)
2391 return -ENOTTY;
2392 return put_user(real_tty->pgrp, p);
2395 static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2397 pid_t pgrp;
2398 int retval = tty_check_change(real_tty);
2400 if (retval == -EIO)
2401 return -ENOTTY;
2402 if (retval)
2403 return retval;
2404 if (!current->signal->tty ||
2405 (current->signal->tty != real_tty) ||
2406 (real_tty->session != current->signal->session))
2407 return -ENOTTY;
2408 if (get_user(pgrp, p))
2409 return -EFAULT;
2410 if (pgrp < 0)
2411 return -EINVAL;
2412 if (session_of_pgrp(pgrp) != current->signal->session)
2413 return -EPERM;
2414 real_tty->pgrp = pgrp;
2415 return 0;
2418 static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2420 /*
2421 * (tty == real_tty) is a cheap way of
2422 * testing if the tty is NOT a master pty.
2423 */
2424 if (tty == real_tty && current->signal->tty != real_tty)
2425 return -ENOTTY;
2426 if (real_tty->session <= 0)
2427 return -ENOTTY;
2428 return put_user(real_tty->session, p);
2431 static int tiocsetd(struct tty_struct *tty, int __user *p)
2433 int ldisc;
2435 if (get_user(ldisc, p))
2436 return -EFAULT;
2437 return tty_set_ldisc(tty, ldisc);
2440 static int send_break(struct tty_struct *tty, unsigned int duration)
2442 tty->driver->break_ctl(tty, -1);
2443 if (!signal_pending(current)) {
2444 msleep_interruptible(duration);
2446 tty->driver->break_ctl(tty, 0);
2447 if (signal_pending(current))
2448 return -EINTR;
2449 return 0;
2452 static int
2453 tty_tiocmget(struct tty_struct *tty, struct file *file, int __user *p)
2455 int retval = -EINVAL;
2457 if (tty->driver->tiocmget) {
2458 retval = tty->driver->tiocmget(tty, file);
2460 if (retval >= 0)
2461 retval = put_user(retval, p);
2463 return retval;
2466 static int
2467 tty_tiocmset(struct tty_struct *tty, struct file *file, unsigned int cmd,
2468 unsigned __user *p)
2470 int retval = -EINVAL;
2472 if (tty->driver->tiocmset) {
2473 unsigned int set, clear, val;
2475 retval = get_user(val, p);
2476 if (retval)
2477 return retval;
2479 set = clear = 0;
2480 switch (cmd) {
2481 case TIOCMBIS:
2482 set = val;
2483 break;
2484 case TIOCMBIC:
2485 clear = val;
2486 break;
2487 case TIOCMSET:
2488 set = val;
2489 clear = ~val;
2490 break;
2493 set &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
2494 clear &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
2496 retval = tty->driver->tiocmset(tty, file, set, clear);
2498 return retval;
2501 /*
2502 * Split this up, as gcc can choke on it otherwise..
2503 */
2504 int tty_ioctl(struct inode * inode, struct file * file,
2505 unsigned int cmd, unsigned long arg)
2507 struct tty_struct *tty, *real_tty;
2508 void __user *p = (void __user *)arg;
2509 int retval;
2510 struct tty_ldisc *ld;
2512 tty = (struct tty_struct *)file->private_data;
2513 if (tty_paranoia_check(tty, inode, "tty_ioctl"))
2514 return -EINVAL;
2516 real_tty = tty;
2517 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
2518 tty->driver->subtype == PTY_TYPE_MASTER)
2519 real_tty = tty->link;
2521 /*
2522 * Break handling by driver
2523 */
2524 if (!tty->driver->break_ctl) {
2525 switch(cmd) {
2526 case TIOCSBRK:
2527 case TIOCCBRK:
2528 if (tty->driver->ioctl)
2529 return tty->driver->ioctl(tty, file, cmd, arg);
2530 return -EINVAL;
2532 /* These two ioctl's always return success; even if */
2533 /* the driver doesn't support them. */
2534 case TCSBRK:
2535 case TCSBRKP:
2536 if (!tty->driver->ioctl)
2537 return 0;
2538 retval = tty->driver->ioctl(tty, file, cmd, arg);
2539 if (retval == -ENOIOCTLCMD)
2540 retval = 0;
2541 return retval;
2545 /*
2546 * Factor out some common prep work
2547 */
2548 switch (cmd) {
2549 case TIOCSETD:
2550 case TIOCSBRK:
2551 case TIOCCBRK:
2552 case TCSBRK:
2553 case TCSBRKP:
2554 retval = tty_check_change(tty);
2555 if (retval)
2556 return retval;
2557 if (cmd != TIOCCBRK) {
2558 tty_wait_until_sent(tty, 0);
2559 if (signal_pending(current))
2560 return -EINTR;
2562 break;
2565 switch (cmd) {
2566 case TIOCSTI:
2567 return tiocsti(tty, p);
2568 case TIOCGWINSZ:
2569 return tiocgwinsz(tty, p);
2570 case TIOCSWINSZ:
2571 return tiocswinsz(tty, real_tty, p);
2572 case TIOCCONS:
2573 return real_tty!=tty ? -EINVAL : tioccons(file);
2574 case FIONBIO:
2575 return fionbio(file, p);
2576 case TIOCEXCL:
2577 set_bit(TTY_EXCLUSIVE, &tty->flags);
2578 return 0;
2579 case TIOCNXCL:
2580 clear_bit(TTY_EXCLUSIVE, &tty->flags);
2581 return 0;
2582 case TIOCNOTTY:
2583 if (current->signal->tty != tty)
2584 return -ENOTTY;
2585 if (current->signal->leader)
2586 disassociate_ctty(0);
2587 task_lock(current);
2588 current->signal->tty = NULL;
2589 task_unlock(current);
2590 return 0;
2591 case TIOCSCTTY:
2592 return tiocsctty(tty, arg);
2593 case TIOCGPGRP:
2594 return tiocgpgrp(tty, real_tty, p);
2595 case TIOCSPGRP:
2596 return tiocspgrp(tty, real_tty, p);
2597 case TIOCGSID:
2598 return tiocgsid(tty, real_tty, p);
2599 case TIOCGETD:
2600 /* FIXME: check this is ok */
2601 return put_user(tty->ldisc.num, (int __user *)p);
2602 case TIOCSETD:
2603 return tiocsetd(tty, p);
2604 #ifdef CONFIG_VT
2605 case TIOCLINUX:
2606 return tioclinux(tty, arg);
2607 #endif
2608 /*
2609 * Break handling
2610 */
2611 case TIOCSBRK: /* Turn break on, unconditionally */
2612 tty->driver->break_ctl(tty, -1);
2613 return 0;
2615 case TIOCCBRK: /* Turn break off, unconditionally */
2616 tty->driver->break_ctl(tty, 0);
2617 return 0;
2618 case TCSBRK: /* SVID version: non-zero arg --> no break */
2619 /*
2620 * XXX is the above comment correct, or the
2621 * code below correct? Is this ioctl used at
2622 * all by anyone?
2623 */
2624 if (!arg)
2625 return send_break(tty, 250);
2626 return 0;
2627 case TCSBRKP: /* support for POSIX tcsendbreak() */
2628 return send_break(tty, arg ? arg*100 : 250);
2630 case TIOCMGET:
2631 return tty_tiocmget(tty, file, p);
2633 case TIOCMSET:
2634 case TIOCMBIC:
2635 case TIOCMBIS:
2636 return tty_tiocmset(tty, file, cmd, p);
2638 if (tty->driver->ioctl) {
2639 retval = (tty->driver->ioctl)(tty, file, cmd, arg);
2640 if (retval != -ENOIOCTLCMD)
2641 return retval;
2643 ld = tty_ldisc_ref_wait(tty);
2644 retval = -EINVAL;
2645 if (ld->ioctl) {
2646 retval = ld->ioctl(tty, file, cmd, arg);
2647 if (retval == -ENOIOCTLCMD)
2648 retval = -EINVAL;
2650 tty_ldisc_deref(ld);
2651 return retval;
2655 /*
2656 * This implements the "Secure Attention Key" --- the idea is to
2657 * prevent trojan horses by killing all processes associated with this
2658 * tty when the user hits the "Secure Attention Key". Required for
2659 * super-paranoid applications --- see the Orange Book for more details.
2661 * This code could be nicer; ideally it should send a HUP, wait a few
2662 * seconds, then send a INT, and then a KILL signal. But you then
2663 * have to coordinate with the init process, since all processes associated
2664 * with the current tty must be dead before the new getty is allowed
2665 * to spawn.
2667 * Now, if it would be correct ;-/ The current code has a nasty hole -
2668 * it doesn't catch files in flight. We may send the descriptor to ourselves
2669 * via AF_UNIX socket, close it and later fetch from socket. FIXME.
2671 * Nasty bug: do_SAK is being called in interrupt context. This can
2672 * deadlock. We punt it up to process context. AKPM - 16Mar2001
2673 */
2674 static void __do_SAK(void *arg)
2676 #ifdef TTY_SOFT_SAK
2677 tty_hangup(tty);
2678 #else
2679 struct tty_struct *tty = arg;
2680 struct task_struct *p;
2681 int session;
2682 int i;
2683 struct file *filp;
2684 struct tty_ldisc *disc;
2685 struct fdtable *fdt;
2687 if (!tty)
2688 return;
2689 session = tty->session;
2691 /* We don't want an ldisc switch during this */
2692 disc = tty_ldisc_ref(tty);
2693 if (disc && disc->flush_buffer)
2694 disc->flush_buffer(tty);
2695 tty_ldisc_deref(disc);
2697 if (tty->driver->flush_buffer)
2698 tty->driver->flush_buffer(tty);
2700 read_lock(&tasklist_lock);
2701 do_each_task_pid(session, PIDTYPE_SID, p) {
2702 if (p->signal->tty == tty || session > 0) {
2703 printk(KERN_NOTICE "SAK: killed process %d"
2704 " (%s): p->signal->session==tty->session\n",
2705 p->pid, p->comm);
2706 send_sig(SIGKILL, p, 1);
2707 continue;
2709 task_lock(p);
2710 if (p->files) {
2711 /*
2712 * We don't take a ref to the file, so we must
2713 * hold ->file_lock instead.
2714 */
2715 spin_lock(&p->files->file_lock);
2716 fdt = files_fdtable(p->files);
2717 for (i=0; i < fdt->max_fds; i++) {
2718 filp = fcheck_files(p->files, i);
2719 if (!filp)
2720 continue;
2721 if (filp->f_op->read == tty_read &&
2722 filp->private_data == tty) {
2723 printk(KERN_NOTICE "SAK: killed process %d"
2724 " (%s): fd#%d opened to the tty\n",
2725 p->pid, p->comm, i);
2726 send_sig(SIGKILL, p, 1);
2727 break;
2730 spin_unlock(&p->files->file_lock);
2732 task_unlock(p);
2733 } while_each_task_pid(session, PIDTYPE_SID, p);
2734 read_unlock(&tasklist_lock);
2735 #endif
2738 /*
2739 * The tq handling here is a little racy - tty->SAK_work may already be queued.
2740 * Fortunately we don't need to worry, because if ->SAK_work is already queued,
2741 * the values which we write to it will be identical to the values which it
2742 * already has. --akpm
2743 */
2744 void do_SAK(struct tty_struct *tty)
2746 if (!tty)
2747 return;
2748 PREPARE_WORK(&tty->SAK_work, __do_SAK, tty);
2749 schedule_work(&tty->SAK_work);
2752 EXPORT_SYMBOL(do_SAK);
2754 /*
2755 * This routine is called out of the software interrupt to flush data
2756 * from the buffer chain to the line discipline.
2757 */
2759 static void flush_to_ldisc(void *private_)
2761 struct tty_struct *tty = (struct tty_struct *) private_;
2762 unsigned long flags;
2763 struct tty_ldisc *disc;
2764 struct tty_buffer *tbuf, *head;
2765 int count;
2766 char *char_buf;
2767 unsigned char *flag_buf;
2769 disc = tty_ldisc_ref(tty);
2770 if (disc == NULL) /* !TTY_LDISC */
2771 return;
2773 if (test_bit(TTY_DONT_FLIP, &tty->flags)) {
2774 /*
2775 * Do it after the next timer tick:
2776 */
2777 schedule_delayed_work(&tty->buf.work, 1);
2778 goto out;
2780 spin_lock_irqsave(&tty->buf.lock, flags);
2781 head = tty->buf.head;
2782 tty->buf.head = NULL;
2783 while((tbuf = head) != NULL) {
2784 while ((count = tbuf->commit - tbuf->read) != 0) {
2785 char_buf = tbuf->char_buf_ptr + tbuf->read;
2786 flag_buf = tbuf->flag_buf_ptr + tbuf->read;
2787 tbuf->read += count;
2788 spin_unlock_irqrestore(&tty->buf.lock, flags);
2789 disc->receive_buf(tty, char_buf, flag_buf, count);
2790 spin_lock_irqsave(&tty->buf.lock, flags);
2792 if (tbuf->active) {
2793 tty->buf.head = head;
2794 break;
2796 head = tbuf->next;
2797 if (head == NULL)
2798 tty->buf.tail = NULL;
2799 tty_buffer_free(tty, tbuf);
2801 spin_unlock_irqrestore(&tty->buf.lock, flags);
2802 out:
2803 tty_ldisc_deref(disc);
2806 /*
2807 * Routine which returns the baud rate of the tty
2809 * Note that the baud_table needs to be kept in sync with the
2810 * include/asm/termbits.h file.
2811 */
2812 static int baud_table[] = {
2813 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
2814 9600, 19200, 38400, 57600, 115200, 230400, 460800,
2815 #ifdef __sparc__
2816 76800, 153600, 307200, 614400, 921600
2817 #else
2818 500000, 576000, 921600, 1000000, 1152000, 1500000, 2000000,
2819 2500000, 3000000, 3500000, 4000000
2820 #endif
2821 };
2823 static int n_baud_table = ARRAY_SIZE(baud_table);
2825 /**
2826 * tty_termios_baud_rate
2827 * @termios: termios structure
2829 * Convert termios baud rate data into a speed. This should be called
2830 * with the termios lock held if this termios is a terminal termios
2831 * structure. May change the termios data.
2832 */
2834 int tty_termios_baud_rate(struct termios *termios)
2836 unsigned int cbaud;
2838 cbaud = termios->c_cflag & CBAUD;
2840 if (cbaud & CBAUDEX) {
2841 cbaud &= ~CBAUDEX;
2843 if (cbaud < 1 || cbaud + 15 > n_baud_table)
2844 termios->c_cflag &= ~CBAUDEX;
2845 else
2846 cbaud += 15;
2848 return baud_table[cbaud];
2851 EXPORT_SYMBOL(tty_termios_baud_rate);
2853 /**
2854 * tty_get_baud_rate - get tty bit rates
2855 * @tty: tty to query
2857 * Returns the baud rate as an integer for this terminal. The
2858 * termios lock must be held by the caller and the terminal bit
2859 * flags may be updated.
2860 */
2862 int tty_get_baud_rate(struct tty_struct *tty)
2864 int baud = tty_termios_baud_rate(tty->termios);
2866 if (baud == 38400 && tty->alt_speed) {
2867 if (!tty->warned) {
2868 printk(KERN_WARNING "Use of setserial/setrocket to "
2869 "set SPD_* flags is deprecated\n");
2870 tty->warned = 1;
2872 baud = tty->alt_speed;
2875 return baud;
2878 EXPORT_SYMBOL(tty_get_baud_rate);
2880 /**
2881 * tty_flip_buffer_push - terminal
2882 * @tty: tty to push
2884 * Queue a push of the terminal flip buffers to the line discipline. This
2885 * function must not be called from IRQ context if tty->low_latency is set.
2887 * In the event of the queue being busy for flipping the work will be
2888 * held off and retried later.
2889 */
2891 void tty_flip_buffer_push(struct tty_struct *tty)
2893 unsigned long flags;
2894 spin_lock_irqsave(&tty->buf.lock, flags);
2895 if (tty->buf.tail != NULL) {
2896 tty->buf.tail->active = 0;
2897 tty->buf.tail->commit = tty->buf.tail->used;
2899 spin_unlock_irqrestore(&tty->buf.lock, flags);
2901 if (tty->low_latency)
2902 flush_to_ldisc((void *) tty);
2903 else
2904 schedule_delayed_work(&tty->buf.work, 1);
2907 EXPORT_SYMBOL(tty_flip_buffer_push);
2910 /*
2911 * This subroutine initializes a tty structure.
2912 */
2913 static void initialize_tty_struct(struct tty_struct *tty)
2915 memset(tty, 0, sizeof(struct tty_struct));
2916 tty->magic = TTY_MAGIC;
2917 tty_ldisc_assign(tty, tty_ldisc_get(N_TTY));
2918 tty->pgrp = -1;
2919 tty->overrun_time = jiffies;
2920 tty->buf.head = tty->buf.tail = NULL;
2921 tty_buffer_init(tty);
2922 INIT_WORK(&tty->buf.work, flush_to_ldisc, tty);
2923 init_MUTEX(&tty->buf.pty_sem);
2924 init_MUTEX(&tty->termios_sem);
2925 init_waitqueue_head(&tty->write_wait);
2926 init_waitqueue_head(&tty->read_wait);
2927 INIT_WORK(&tty->hangup_work, do_tty_hangup, tty);
2928 sema_init(&tty->atomic_read, 1);
2929 sema_init(&tty->atomic_write, 1);
2930 spin_lock_init(&tty->read_lock);
2931 INIT_LIST_HEAD(&tty->tty_files);
2932 INIT_WORK(&tty->SAK_work, NULL, NULL);
2935 /*
2936 * The default put_char routine if the driver did not define one.
2937 */
2938 static void tty_default_put_char(struct tty_struct *tty, unsigned char ch)
2940 tty->driver->write(tty, &ch, 1);
2943 static struct class *tty_class;
2945 /**
2946 * tty_register_device - register a tty device
2947 * @driver: the tty driver that describes the tty device
2948 * @index: the index in the tty driver for this tty device
2949 * @device: a struct device that is associated with this tty device.
2950 * This field is optional, if there is no known struct device for this
2951 * tty device it can be set to NULL safely.
2953 * This call is required to be made to register an individual tty device if
2954 * the tty driver's flags have the TTY_DRIVER_NO_DEVFS bit set. If that
2955 * bit is not set, this function should not be called.
2956 */
2957 void tty_register_device(struct tty_driver *driver, unsigned index,
2958 struct device *device)
2960 char name[64];
2961 dev_t dev = MKDEV(driver->major, driver->minor_start) + index;
2963 if (index >= driver->num) {
2964 printk(KERN_ERR "Attempt to register invalid tty line number "
2965 " (%d).\n", index);
2966 return;
2969 devfs_mk_cdev(dev, S_IFCHR | S_IRUSR | S_IWUSR,
2970 "%s%d", driver->devfs_name, index + driver->name_base);
2972 if (driver->type == TTY_DRIVER_TYPE_PTY)
2973 pty_line_name(driver, index, name);
2974 else
2975 tty_line_name(driver, index, name);
2976 class_device_create(tty_class, NULL, dev, device, "%s", name);
2979 /**
2980 * tty_unregister_device - unregister a tty device
2981 * @driver: the tty driver that describes the tty device
2982 * @index: the index in the tty driver for this tty device
2984 * If a tty device is registered with a call to tty_register_device() then
2985 * this function must be made when the tty device is gone.
2986 */
2987 void tty_unregister_device(struct tty_driver *driver, unsigned index)
2989 devfs_remove("%s%d", driver->devfs_name, index + driver->name_base);
2990 class_device_destroy(tty_class, MKDEV(driver->major, driver->minor_start) + index);
2993 EXPORT_SYMBOL(tty_register_device);
2994 EXPORT_SYMBOL(tty_unregister_device);
2996 struct tty_driver *alloc_tty_driver(int lines)
2998 struct tty_driver *driver;
3000 driver = kmalloc(sizeof(struct tty_driver), GFP_KERNEL);
3001 if (driver) {
3002 memset(driver, 0, sizeof(struct tty_driver));
3003 driver->magic = TTY_DRIVER_MAGIC;
3004 driver->num = lines;
3005 /* later we'll move allocation of tables here */
3007 return driver;
3010 void put_tty_driver(struct tty_driver *driver)
3012 kfree(driver);
3015 void tty_set_operations(struct tty_driver *driver, struct tty_operations *op)
3017 driver->open = op->open;
3018 driver->close = op->close;
3019 driver->write = op->write;
3020 driver->put_char = op->put_char;
3021 driver->flush_chars = op->flush_chars;
3022 driver->write_room = op->write_room;
3023 driver->chars_in_buffer = op->chars_in_buffer;
3024 driver->ioctl = op->ioctl;
3025 driver->set_termios = op->set_termios;
3026 driver->throttle = op->throttle;
3027 driver->unthrottle = op->unthrottle;
3028 driver->stop = op->stop;
3029 driver->start = op->start;
3030 driver->hangup = op->hangup;
3031 driver->break_ctl = op->break_ctl;
3032 driver->flush_buffer = op->flush_buffer;
3033 driver->set_ldisc = op->set_ldisc;
3034 driver->wait_until_sent = op->wait_until_sent;
3035 driver->send_xchar = op->send_xchar;
3036 driver->read_proc = op->read_proc;
3037 driver->write_proc = op->write_proc;
3038 driver->tiocmget = op->tiocmget;
3039 driver->tiocmset = op->tiocmset;
3043 EXPORT_SYMBOL(alloc_tty_driver);
3044 EXPORT_SYMBOL(put_tty_driver);
3045 EXPORT_SYMBOL(tty_set_operations);
3047 /*
3048 * Called by a tty driver to register itself.
3049 */
3050 int tty_register_driver(struct tty_driver *driver)
3052 int error;
3053 int i;
3054 dev_t dev;
3055 void **p = NULL;
3057 if (driver->flags & TTY_DRIVER_INSTALLED)
3058 return 0;
3060 if (!(driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
3061 p = kmalloc(driver->num * 3 * sizeof(void *), GFP_KERNEL);
3062 if (!p)
3063 return -ENOMEM;
3064 memset(p, 0, driver->num * 3 * sizeof(void *));
3067 if (!driver->major) {
3068 error = alloc_chrdev_region(&dev, driver->minor_start, driver->num,
3069 (char*)driver->name);
3070 if (!error) {
3071 driver->major = MAJOR(dev);
3072 driver->minor_start = MINOR(dev);
3074 } else {
3075 dev = MKDEV(driver->major, driver->minor_start);
3076 error = register_chrdev_region(dev, driver->num,
3077 (char*)driver->name);
3079 if (error < 0) {
3080 kfree(p);
3081 return error;
3084 if (p) {
3085 driver->ttys = (struct tty_struct **)p;
3086 driver->termios = (struct termios **)(p + driver->num);
3087 driver->termios_locked = (struct termios **)(p + driver->num * 2);
3088 } else {
3089 driver->ttys = NULL;
3090 driver->termios = NULL;
3091 driver->termios_locked = NULL;
3094 cdev_init(&driver->cdev, &tty_fops);
3095 driver->cdev.owner = driver->owner;
3096 error = cdev_add(&driver->cdev, dev, driver->num);
3097 if (error) {
3098 cdev_del(&driver->cdev);
3099 unregister_chrdev_region(dev, driver->num);
3100 driver->ttys = NULL;
3101 driver->termios = driver->termios_locked = NULL;
3102 kfree(p);
3103 return error;
3106 if (!driver->put_char)
3107 driver->put_char = tty_default_put_char;
3109 list_add(&driver->tty_drivers, &tty_drivers);
3111 if ( !(driver->flags & TTY_DRIVER_NO_DEVFS) ) {
3112 for(i = 0; i < driver->num; i++)
3113 tty_register_device(driver, i, NULL);
3115 proc_tty_register_driver(driver);
3116 return 0;
3119 EXPORT_SYMBOL(tty_register_driver);
3121 /*
3122 * Called by a tty driver to unregister itself.
3123 */
3124 int tty_unregister_driver(struct tty_driver *driver)
3126 int i;
3127 struct termios *tp;
3128 void *p;
3130 if (driver->refcount)
3131 return -EBUSY;
3133 unregister_chrdev_region(MKDEV(driver->major, driver->minor_start),
3134 driver->num);
3136 list_del(&driver->tty_drivers);
3138 /*
3139 * Free the termios and termios_locked structures because
3140 * we don't want to get memory leaks when modular tty
3141 * drivers are removed from the kernel.
3142 */
3143 for (i = 0; i < driver->num; i++) {
3144 tp = driver->termios[i];
3145 if (tp) {
3146 driver->termios[i] = NULL;
3147 kfree(tp);
3149 tp = driver->termios_locked[i];
3150 if (tp) {
3151 driver->termios_locked[i] = NULL;
3152 kfree(tp);
3154 if (!(driver->flags & TTY_DRIVER_NO_DEVFS))
3155 tty_unregister_device(driver, i);
3157 p = driver->ttys;
3158 proc_tty_unregister_driver(driver);
3159 driver->ttys = NULL;
3160 driver->termios = driver->termios_locked = NULL;
3161 kfree(p);
3162 cdev_del(&driver->cdev);
3163 return 0;
3166 EXPORT_SYMBOL(tty_unregister_driver);
3169 /*
3170 * Initialize the console device. This is called *early*, so
3171 * we can't necessarily depend on lots of kernel help here.
3172 * Just do some early initializations, and do the complex setup
3173 * later.
3174 */
3175 void __init console_init(void)
3177 initcall_t *call;
3179 /* Setup the default TTY line discipline. */
3180 (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
3182 /*
3183 * set up the console device so that later boot sequences can
3184 * inform about problems etc..
3185 */
3186 #ifdef CONFIG_EARLY_PRINTK
3187 disable_early_printk();
3188 #endif
3189 call = __con_initcall_start;
3190 while (call < __con_initcall_end) {
3191 (*call)();
3192 call++;
3196 #ifdef CONFIG_VT
3197 extern int vty_init(void);
3198 #endif
3200 static int __init tty_class_init(void)
3202 tty_class = class_create(THIS_MODULE, "tty");
3203 if (IS_ERR(tty_class))
3204 return PTR_ERR(tty_class);
3205 return 0;
3208 postcore_initcall(tty_class_init);
3210 /* 3/2004 jmc: why do these devices exist? */
3212 static struct cdev tty_cdev, console_cdev;
3213 #ifdef CONFIG_UNIX98_PTYS
3214 static struct cdev ptmx_cdev;
3215 #endif
3216 #ifdef CONFIG_VT
3217 static struct cdev vc0_cdev;
3218 #endif
3220 /*
3221 * Ok, now we can initialize the rest of the tty devices and can count
3222 * on memory allocations, interrupts etc..
3223 */
3224 static int __init tty_init(void)
3226 cdev_init(&tty_cdev, &tty_fops);
3227 if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) ||
3228 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0)
3229 panic("Couldn't register /dev/tty driver\n");
3230 devfs_mk_cdev(MKDEV(TTYAUX_MAJOR, 0), S_IFCHR|S_IRUGO|S_IWUGO, "tty");
3231 class_device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), NULL, "tty");
3233 cdev_init(&console_cdev, &console_fops);
3234 if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) ||
3235 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0)
3236 panic("Couldn't register /dev/console driver\n");
3237 devfs_mk_cdev(MKDEV(TTYAUX_MAJOR, 1), S_IFCHR|S_IRUSR|S_IWUSR, "console");
3238 class_device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 1), NULL, "console");
3240 #ifdef CONFIG_UNIX98_PTYS
3241 cdev_init(&ptmx_cdev, &ptmx_fops);
3242 if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) ||
3243 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0)
3244 panic("Couldn't register /dev/ptmx driver\n");
3245 devfs_mk_cdev(MKDEV(TTYAUX_MAJOR, 2), S_IFCHR|S_IRUGO|S_IWUGO, "ptmx");
3246 class_device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), NULL, "ptmx");
3247 #endif
3249 #ifdef CONFIG_VT
3250 if (!console_use_vt)
3251 goto out_vt;
3252 cdev_init(&vc0_cdev, &console_fops);
3253 if (cdev_add(&vc0_cdev, MKDEV(TTY_MAJOR, 0), 1) ||
3254 register_chrdev_region(MKDEV(TTY_MAJOR, 0), 1, "/dev/vc/0") < 0)
3255 panic("Couldn't register /dev/tty0 driver\n");
3256 devfs_mk_cdev(MKDEV(TTY_MAJOR, 0), S_IFCHR|S_IRUSR|S_IWUSR, "vc/0");
3257 class_device_create(tty_class, NULL, MKDEV(TTY_MAJOR, 0), NULL, "tty0");
3259 vty_init();
3260 out_vt:
3261 #endif
3262 return 0;
3264 module_init(tty_init);