root/drivers/char/pty.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. pty_close
  2. pty_unthrottle
  3. pty_write
  4. pty_write_room
  5. pty_chars_in_buffer
  6. pty_flush_buffer
  7. pty_open
  8. pty_init

   1 /*
   2  *  linux/drivers/char/pty.c
   3  *
   4  *  Copyright (C) 1991, 1992  Linus Torvalds
   5  */
   6 
   7 /*
   8  *      pty.c
   9  *
  10  * This module exports the following pty function:
  11  * 
  12  *      int  pty_open(struct tty_struct * tty, struct file * filp);
  13  */
  14 
  15 #include <linux/errno.h>
  16 #include <linux/sched.h>
  17 #include <linux/interrupt.h>
  18 #include <linux/tty.h>
  19 #include <linux/tty_flip.h>
  20 #include <linux/fcntl.h>
  21 #include <linux/string.h>
  22 #include <linux/major.h>
  23 #include <linux/mm.h>
  24 
  25 #include <asm/segment.h>
  26 #include <asm/system.h>
  27 #include <asm/bitops.h>
  28 
  29 struct pty_struct {
  30         int     magic;
  31         struct wait_queue * open_wait;
  32 };
  33 
  34 #define PTY_MAGIC 0x5001
  35 
  36 #define PTY_BUF_SIZE PAGE_SIZE/2
  37 
  38 /*
  39  * tmp_buf is used as a temporary buffer by pty_write.  We need to
  40  * lock it in case the memcpy_fromfs blocks while swapping in a page,
  41  * and some other program tries to do a pty write at the same time.
  42  * Since the lock will only come under contention when the system is
  43  * swapping and available memory is low, it makes sense to share one
  44  * buffer across all the PTY's, since it significantly saves memory if
  45  * large numbers of PTY's are open.
  46  */
  47 static unsigned char *tmp_buf;
  48 static struct semaphore tmp_buf_sem = MUTEX;
  49 
  50 struct tty_driver pty_driver, pty_slave_driver;
  51 static int pty_refcount;
  52 
  53 static struct tty_struct *pty_table[NR_PTYS];
  54 static struct termios *pty_termios[NR_PTYS];
  55 static struct termios *pty_termios_locked[NR_PTYS];
  56 static struct tty_struct *ttyp_table[NR_PTYS];
  57 static struct termios *ttyp_termios[NR_PTYS];
  58 static struct termios *ttyp_termios_locked[NR_PTYS];
  59 static struct pty_struct pty_state[NR_PTYS];
  60 
  61 #define MIN(a,b)        ((a) < (b) ? (a) : (b))
  62 
  63 static void pty_close(struct tty_struct * tty, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
  64 {
  65         if (!tty)
  66                 return;
  67         if (tty->driver.subtype == PTY_TYPE_MASTER) {
  68                 if (tty->count > 1)
  69                         printk("master pty_close: count = %d!!\n", tty->count);
  70         } else {
  71                 if (tty->count > 2)
  72                         return;
  73         }
  74         wake_up_interruptible(&tty->read_wait);
  75         wake_up_interruptible(&tty->write_wait);
  76         if (!tty->link)
  77                 return;
  78         wake_up_interruptible(&tty->link->read_wait);
  79         wake_up_interruptible(&tty->link->write_wait);
  80         if (tty->driver.subtype == PTY_TYPE_MASTER)
  81                 tty_hangup(tty->link);
  82         else {
  83                 start_tty(tty);
  84                 set_bit(TTY_SLAVE_CLOSED, &tty->link->flags);
  85         }
  86 }
  87 
  88 /*
  89  * The unthrottle routine is called by the line discipline to signal
  90  * that it can receive more characters.  For PTY's, the TTY_THROTTLED
  91  * flag is always set, to force the line discipline to always call the
  92  * unthrottle routine when there are fewer than TTY_THRESHOLD_UNTHROTTLE 
  93  * characters in the queue.  This is necessary since each time this
  94  * happens, we need to wake up any sleeping processes that could be
  95  * (1) trying to send data to the pty, or (2) waiting in wait_until_sent()
  96  * for the pty buffer to be drained.
  97  */
  98 static void pty_unthrottle(struct tty_struct * tty)
     /* [previous][next][first][last][top][bottom][index][help] */
  99 {
 100         struct tty_struct *o_tty = tty->link;
 101 
 102         if (!o_tty)
 103                 return;
 104 
 105         if ((o_tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
 106             o_tty->ldisc.write_wakeup)
 107                 (o_tty->ldisc.write_wakeup)(o_tty);
 108         wake_up_interruptible(&o_tty->write_wait);
 109         set_bit(TTY_THROTTLED, &tty->flags);
 110 }
 111 
 112 static int pty_write(struct tty_struct * tty, int from_user,
     /* [previous][next][first][last][top][bottom][index][help] */
 113                        unsigned char *buf, int count)
 114 {
 115         struct tty_struct *to = tty->link;
 116         int     c=0, n, r;
 117         char    *temp_buffer;
 118 
 119         if (!to || tty->stopped)
 120                 return 0;
 121         
 122         if (from_user) {
 123                 down(&tmp_buf_sem);
 124                 temp_buffer = tmp_buf +
 125                         ((tty->driver.subtype-1) * PTY_BUF_SIZE);
 126                 while (count > 0) {
 127                         n = MIN(count, PTY_BUF_SIZE);
 128                         memcpy_fromfs(temp_buffer, buf, n);
 129                         r = to->ldisc.receive_room(to);
 130                         if (r <= 0)
 131                                 break;
 132                         n = MIN(n, r);
 133                         to->ldisc.receive_buf(to, temp_buffer, 0, n);
 134                         buf += n;  c+= n;
 135                         count -= n;
 136                 }
 137                 up(&tmp_buf_sem);
 138         } else {
 139                 c = MIN(count, to->ldisc.receive_room(to));
 140                 to->ldisc.receive_buf(to, buf, 0, c);
 141         }
 142         
 143         return c;
 144 }
 145 
 146 static int pty_write_room(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 147 {
 148         struct tty_struct *to = tty->link;
 149 
 150         if (!to || tty->stopped)
 151                 return 0;
 152 
 153         return to->ldisc.receive_room(to);
 154 }
 155 
 156 static int pty_chars_in_buffer(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 157 {
 158         struct tty_struct *to = tty->link;
 159 
 160         if (!to || !to->ldisc.chars_in_buffer)
 161                 return 0;
 162 
 163         return to->ldisc.chars_in_buffer(to);
 164 }
 165 
 166 static void pty_flush_buffer(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 167 {
 168         struct tty_struct *to = tty->link;
 169         
 170         if (!to)
 171                 return;
 172         
 173         if (to->ldisc.flush_buffer)
 174                 to->ldisc.flush_buffer(to);
 175         
 176         if (to->packet) {
 177                 tty->ctrl_status |= TIOCPKT_FLUSHWRITE;
 178                 wake_up_interruptible(&to->read_wait);
 179         }
 180 }
 181 
 182 int pty_open(struct tty_struct *tty, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
 183 {
 184         int     line;
 185         struct  pty_struct *pty;
 186         
 187         if (!tty || !tty->link)
 188                 return -ENODEV;
 189         line = MINOR(tty->device) - tty->driver.minor_start;
 190         if ((line < 0) || (line >= NR_PTYS))
 191                 return -ENODEV;
 192         pty = pty_state + line;
 193         tty->driver_data = pty;
 194 
 195         if (!tmp_buf) {
 196                 tmp_buf = (unsigned char *) get_free_page(GFP_KERNEL);
 197                 if (!tmp_buf)
 198                         return -ENOMEM;
 199         }
 200 
 201         if (tty->driver.subtype == PTY_TYPE_SLAVE)
 202                 clear_bit(TTY_SLAVE_CLOSED, &tty->link->flags);
 203         wake_up_interruptible(&pty->open_wait);
 204         set_bit(TTY_THROTTLED, &tty->flags);
 205         if (filp->f_flags & O_NDELAY)
 206                 return 0;
 207         while (!tty->link->count && !(current->signal & ~current->blocked))
 208                 interruptible_sleep_on(&pty->open_wait);
 209         if (!tty->link->count)
 210                 return -ERESTARTSYS;
 211         return 0;
 212 }
 213 
 214 long pty_init(long kmem_start)
     /* [previous][next][first][last][top][bottom][index][help] */
 215 {
 216         memset(&pty_state, 0, sizeof(pty_state));
 217         memset(&pty_driver, 0, sizeof(struct tty_driver));
 218         pty_driver.magic = TTY_DRIVER_MAGIC;
 219         pty_driver.name = "pty";
 220         pty_driver.major = TTY_MAJOR;
 221         pty_driver.minor_start = 128;
 222         pty_driver.num = NR_PTYS;
 223         pty_driver.type = TTY_DRIVER_TYPE_PTY;
 224         pty_driver.subtype = PTY_TYPE_MASTER;
 225         pty_driver.init_termios = tty_std_termios;
 226         pty_driver.init_termios.c_iflag = 0;
 227         pty_driver.init_termios.c_oflag = 0;
 228         pty_driver.init_termios.c_cflag = B38400 | CS8 | CREAD;
 229         pty_driver.init_termios.c_lflag = 0;
 230         pty_driver.flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW;
 231         pty_driver.refcount = &pty_refcount;
 232         pty_driver.table = pty_table;
 233         pty_driver.termios = pty_termios;
 234         pty_driver.termios_locked = pty_termios_locked;
 235         pty_driver.other = &pty_slave_driver;
 236 
 237         pty_driver.open = pty_open;
 238         pty_driver.close = pty_close;
 239         pty_driver.write = pty_write;
 240         pty_driver.write_room = pty_write_room;
 241         pty_driver.flush_buffer = pty_flush_buffer;
 242         pty_driver.chars_in_buffer = pty_chars_in_buffer;
 243         pty_driver.unthrottle = pty_unthrottle;
 244 
 245         pty_slave_driver = pty_driver;
 246         pty_slave_driver.name = "ttyp";
 247         pty_slave_driver.subtype = PTY_TYPE_SLAVE;
 248         pty_slave_driver.minor_start = 192;
 249         pty_slave_driver.init_termios = tty_std_termios;
 250         pty_slave_driver.init_termios.c_cflag = B38400 | CS8 | CREAD;
 251         pty_slave_driver.table = ttyp_table;
 252         pty_slave_driver.termios = ttyp_termios;
 253         pty_slave_driver.termios_locked = ttyp_termios_locked;
 254         pty_slave_driver.other = &pty_driver;
 255 
 256         tmp_buf = 0;
 257 
 258         if (tty_register_driver(&pty_driver))
 259                 panic("Couldn't register pty driver");
 260         if (tty_register_driver(&pty_slave_driver))
 261                 panic("Couldn't register pty slave driver");
 262         
 263         return kmem_start;
 264 }

/* [previous][next][first][last][top][bottom][index][help] */