Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/char/pty.c |
| 3 | * |
| 4 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 5 | * |
| 6 | * Added support for a Unix98-style ptmx device. |
| 7 | * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 9 | * When reading this code see also fs/devpts. In particular note that the |
| 10 | * driver_data field is used by the devpts side as a binding to the devpts |
| 11 | * inode. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | */ |
| 13 | |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 14 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | |
| 16 | #include <linux/errno.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/sysctl.h> |
Alan Cox | d81ed10 | 2008-10-13 10:41:42 +0100 | [diff] [blame] | 26 | #include <linux/device.h> |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 27 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/bitops.h> |
| 29 | #include <linux/devpts_fs.h> |
| 30 | |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 31 | #include <asm/system.h> |
| 32 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | /* These are global because they are accessed in tty_io.c */ |
| 34 | #ifdef CONFIG_UNIX98_PTYS |
Roel Kluin | da2bdf9 | 2009-01-07 18:09:15 -0800 | [diff] [blame] | 35 | static struct tty_driver *ptm_driver; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | static struct tty_driver *pts_driver; |
| 37 | #endif |
| 38 | |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 39 | static void pty_close(struct tty_struct *tty, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | { |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 41 | BUG_ON(!tty); |
| 42 | if (tty->driver->subtype == PTY_TYPE_MASTER) |
| 43 | WARN_ON(tty->count > 1); |
| 44 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | if (tty->count > 2) |
| 46 | return; |
| 47 | } |
| 48 | wake_up_interruptible(&tty->read_wait); |
| 49 | wake_up_interruptible(&tty->write_wait); |
| 50 | tty->packet = 0; |
| 51 | if (!tty->link) |
| 52 | return; |
| 53 | tty->link->packet = 0; |
| 54 | set_bit(TTY_OTHER_CLOSED, &tty->link->flags); |
| 55 | wake_up_interruptible(&tty->link->read_wait); |
| 56 | wake_up_interruptible(&tty->link->write_wait); |
| 57 | if (tty->driver->subtype == PTY_TYPE_MASTER) { |
| 58 | set_bit(TTY_OTHER_CLOSED, &tty->flags); |
| 59 | #ifdef CONFIG_UNIX98_PTYS |
| 60 | if (tty->driver == ptm_driver) |
Sukadev Bhattiprolu | 15f1a63 | 2008-10-13 10:42:59 +0100 | [diff] [blame] | 61 | devpts_pty_kill(tty->link); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | #endif |
| 63 | tty_vhangup(tty->link); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /* |
| 68 | * The unthrottle routine is called by the line discipline to signal |
| 69 | * that it can receive more characters. For PTY's, the TTY_THROTTLED |
| 70 | * flag is always set, to force the line discipline to always call the |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 71 | * unthrottle routine when there are fewer than TTY_THRESHOLD_UNTHROTTLE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | * characters in the queue. This is necessary since each time this |
| 73 | * happens, we need to wake up any sleeping processes that could be |
| 74 | * (1) trying to send data to the pty, or (2) waiting in wait_until_sent() |
| 75 | * for the pty buffer to be drained. |
| 76 | */ |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 77 | static void pty_unthrottle(struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | { |
| 79 | struct tty_struct *o_tty = tty->link; |
| 80 | |
| 81 | if (!o_tty) |
| 82 | return; |
| 83 | |
| 84 | tty_wakeup(o_tty); |
| 85 | set_bit(TTY_THROTTLED, &tty->flags); |
| 86 | } |
| 87 | |
| 88 | /* |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 89 | * WSH 05/24/97: modified to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | * (1) use space in tty->flip instead of a shared temp buffer |
| 91 | * The flip buffers aren't being used for a pty, so there's lots |
| 92 | * of space available. The buffer is protected by a per-pty |
| 93 | * semaphore that should almost never come under contention. |
| 94 | * (2) avoid redundant copying for cases where count >> receive_room |
| 95 | * N.B. Calls from user space may now return an error code instead of |
| 96 | * a count. |
| 97 | * |
| 98 | * FIXME: Our pty_write method is called with our ldisc lock held but |
| 99 | * not our partners. We can't just take the other one blindly without |
Paul Fulghum | 817d6d3 | 2006-06-28 04:26:47 -0700 | [diff] [blame] | 100 | * risking deadlocks. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | */ |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 102 | static int pty_write(struct tty_struct *tty, const unsigned char *buf, |
| 103 | int count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | { |
| 105 | struct tty_struct *to = tty->link; |
| 106 | int c; |
| 107 | |
| 108 | if (!to || tty->stopped) |
| 109 | return 0; |
| 110 | |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 111 | c = to->receive_room; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | if (c > count) |
| 113 | c = count; |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 114 | to->ldisc.ops->receive_buf(to, buf, NULL, c); |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 115 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | return c; |
| 117 | } |
| 118 | |
| 119 | static int pty_write_room(struct tty_struct *tty) |
| 120 | { |
| 121 | struct tty_struct *to = tty->link; |
| 122 | |
| 123 | if (!to || tty->stopped) |
| 124 | return 0; |
| 125 | |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 126 | return to->receive_room; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | /* |
| 130 | * WSH 05/24/97: Modified for asymmetric MASTER/SLAVE behavior |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 131 | * The chars_in_buffer() value is used by the ldisc select() function |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | * to hold off writing when chars_in_buffer > WAKEUP_CHARS (== 256). |
| 133 | * The pty driver chars_in_buffer() Master/Slave must behave differently: |
| 134 | * |
| 135 | * The Master side needs to allow typed-ahead commands to accumulate |
| 136 | * while being canonicalized, so we report "our buffer" as empty until |
| 137 | * some threshold is reached, and then report the count. (Any count > |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 138 | * WAKEUP_CHARS is regarded by select() as "full".) To avoid deadlock |
| 139 | * the count returned must be 0 if no canonical data is available to be |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | * read. (The N_TTY ldisc.chars_in_buffer now knows this.) |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 141 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | * The Slave side passes all characters in raw mode to the Master side's |
| 143 | * buffer where they can be read immediately, so in this case we can |
| 144 | * return the true count in the buffer. |
| 145 | */ |
| 146 | static int pty_chars_in_buffer(struct tty_struct *tty) |
| 147 | { |
| 148 | struct tty_struct *to = tty->link; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | int count; |
| 150 | |
| 151 | /* We should get the line discipline lock for "tty->link" */ |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 152 | if (!to || !to->ldisc.ops->chars_in_buffer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | return 0; |
| 154 | |
| 155 | /* The ldisc must report 0 if no characters available to be read */ |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 156 | count = to->ldisc.ops->chars_in_buffer(to); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 158 | if (tty->driver->subtype == PTY_TYPE_SLAVE) |
| 159 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 161 | /* Master side driver ... if the other side's read buffer is less than |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | * half full, return 0 to allow writers to proceed; otherwise return |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 163 | * the count. This leaves a comfortable margin to avoid overflow, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | * and still allows half a buffer's worth of typed-ahead commands. |
| 165 | */ |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 166 | return (count < N_TTY_BUF_SIZE/2) ? 0 : count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | /* Set the lock flag on a pty */ |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 170 | static int pty_set_lock(struct tty_struct *tty, int __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | { |
| 172 | int val; |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 173 | if (get_user(val, arg)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | return -EFAULT; |
| 175 | if (val) |
| 176 | set_bit(TTY_PTY_LOCK, &tty->flags); |
| 177 | else |
| 178 | clear_bit(TTY_PTY_LOCK, &tty->flags); |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | static void pty_flush_buffer(struct tty_struct *tty) |
| 183 | { |
| 184 | struct tty_struct *to = tty->link; |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 185 | unsigned long flags; |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 186 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | if (!to) |
| 188 | return; |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 189 | |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 190 | if (to->ldisc.ops->flush_buffer) |
| 191 | to->ldisc.ops->flush_buffer(to); |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 192 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | if (to->packet) { |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 194 | spin_lock_irqsave(&tty->ctrl_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | tty->ctrl_status |= TIOCPKT_FLUSHWRITE; |
| 196 | wake_up_interruptible(&to->read_wait); |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 197 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 201 | static int pty_open(struct tty_struct *tty, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | { |
| 203 | int retval = -ENODEV; |
| 204 | |
| 205 | if (!tty || !tty->link) |
| 206 | goto out; |
| 207 | |
| 208 | retval = -EIO; |
| 209 | if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) |
| 210 | goto out; |
| 211 | if (test_bit(TTY_PTY_LOCK, &tty->link->flags)) |
| 212 | goto out; |
| 213 | if (tty->link->count != 1) |
| 214 | goto out; |
| 215 | |
| 216 | clear_bit(TTY_OTHER_CLOSED, &tty->link->flags); |
| 217 | set_bit(TTY_THROTTLED, &tty->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | retval = 0; |
| 219 | out: |
| 220 | return retval; |
| 221 | } |
| 222 | |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 223 | static void pty_set_termios(struct tty_struct *tty, |
| 224 | struct ktermios *old_termios) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | { |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 226 | tty->termios->c_cflag &= ~(CSIZE | PARENB); |
| 227 | tty->termios->c_cflag |= (CS8 | CREAD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Alan Cox | fc6f623 | 2009-01-02 13:43:17 +0000 | [diff] [blame] | 230 | /** |
| 231 | * pty_do_resize - resize event |
| 232 | * @tty: tty being resized |
Alan Cox | c774bda | 2009-01-11 19:46:49 +0000 | [diff] [blame] | 233 | * @ws: window size being set. |
Alan Cox | fc6f623 | 2009-01-02 13:43:17 +0000 | [diff] [blame] | 234 | * |
| 235 | * Update the termios variables and send the neccessary signals to |
| 236 | * peform a terminal resize correctly |
| 237 | */ |
| 238 | |
| 239 | int pty_resize(struct tty_struct *tty, struct winsize *ws) |
| 240 | { |
| 241 | struct pid *pgrp, *rpgrp; |
| 242 | unsigned long flags; |
| 243 | struct tty_struct *pty = tty->link; |
| 244 | |
| 245 | /* For a PTY we need to lock the tty side */ |
| 246 | mutex_lock(&tty->termios_mutex); |
| 247 | if (!memcmp(ws, &tty->winsize, sizeof(*ws))) |
| 248 | goto done; |
| 249 | |
| 250 | /* Get the PID values and reference them so we can |
| 251 | avoid holding the tty ctrl lock while sending signals. |
| 252 | We need to lock these individually however. */ |
| 253 | |
| 254 | spin_lock_irqsave(&tty->ctrl_lock, flags); |
| 255 | pgrp = get_pid(tty->pgrp); |
| 256 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); |
| 257 | |
| 258 | spin_lock_irqsave(&pty->ctrl_lock, flags); |
| 259 | rpgrp = get_pid(pty->pgrp); |
| 260 | spin_unlock_irqrestore(&pty->ctrl_lock, flags); |
| 261 | |
| 262 | if (pgrp) |
| 263 | kill_pgrp(pgrp, SIGWINCH, 1); |
| 264 | if (rpgrp != pgrp && rpgrp) |
| 265 | kill_pgrp(rpgrp, SIGWINCH, 1); |
| 266 | |
| 267 | put_pid(pgrp); |
| 268 | put_pid(rpgrp); |
| 269 | |
| 270 | tty->winsize = *ws; |
| 271 | pty->winsize = *ws; /* Never used so will go away soon */ |
| 272 | done: |
| 273 | mutex_unlock(&tty->termios_mutex); |
| 274 | return 0; |
| 275 | } |
| 276 | |
Alan Cox | bf970ee | 2008-10-13 10:42:39 +0100 | [diff] [blame] | 277 | static int pty_install(struct tty_driver *driver, struct tty_struct *tty) |
| 278 | { |
| 279 | struct tty_struct *o_tty; |
| 280 | int idx = tty->index; |
| 281 | int retval; |
| 282 | |
| 283 | o_tty = alloc_tty_struct(); |
| 284 | if (!o_tty) |
| 285 | return -ENOMEM; |
| 286 | if (!try_module_get(driver->other->owner)) { |
| 287 | /* This cannot in fact currently happen */ |
| 288 | free_tty_struct(o_tty); |
| 289 | return -ENOMEM; |
| 290 | } |
| 291 | initialize_tty_struct(o_tty, driver->other, idx); |
| 292 | |
| 293 | /* We always use new tty termios data so we can do this |
| 294 | the easy way .. */ |
| 295 | retval = tty_init_termios(tty); |
| 296 | if (retval) |
| 297 | goto free_mem_out; |
| 298 | |
| 299 | retval = tty_init_termios(o_tty); |
| 300 | if (retval) { |
| 301 | tty_free_termios(tty); |
| 302 | goto free_mem_out; |
| 303 | } |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 304 | |
Alan Cox | bf970ee | 2008-10-13 10:42:39 +0100 | [diff] [blame] | 305 | /* |
| 306 | * Everything allocated ... set up the o_tty structure. |
| 307 | */ |
| 308 | driver->other->ttys[idx] = o_tty; |
| 309 | tty_driver_kref_get(driver->other); |
| 310 | if (driver->subtype == PTY_TYPE_MASTER) |
| 311 | o_tty->count++; |
| 312 | /* Establish the links in both directions */ |
| 313 | tty->link = o_tty; |
| 314 | o_tty->link = tty; |
| 315 | |
| 316 | tty_driver_kref_get(driver); |
| 317 | tty->count++; |
| 318 | driver->ttys[idx] = tty; |
| 319 | return 0; |
| 320 | free_mem_out: |
| 321 | module_put(o_tty->driver->owner); |
| 322 | free_tty_struct(o_tty); |
| 323 | return -ENOMEM; |
| 324 | } |
| 325 | |
| 326 | |
Jeff Dike | b68e31d | 2006-10-02 02:17:18 -0700 | [diff] [blame] | 327 | static const struct tty_operations pty_ops = { |
Alan Cox | bf970ee | 2008-10-13 10:42:39 +0100 | [diff] [blame] | 328 | .install = pty_install, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | .open = pty_open, |
| 330 | .close = pty_close, |
| 331 | .write = pty_write, |
| 332 | .write_room = pty_write_room, |
| 333 | .flush_buffer = pty_flush_buffer, |
| 334 | .chars_in_buffer = pty_chars_in_buffer, |
| 335 | .unthrottle = pty_unthrottle, |
| 336 | .set_termios = pty_set_termios, |
Alan Cox | fc6f623 | 2009-01-02 13:43:17 +0000 | [diff] [blame] | 337 | .resize = pty_resize |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | }; |
| 339 | |
| 340 | /* Traditional BSD devices */ |
| 341 | #ifdef CONFIG_LEGACY_PTYS |
| 342 | static struct tty_driver *pty_driver, *pty_slave_driver; |
| 343 | |
| 344 | static int pty_bsd_ioctl(struct tty_struct *tty, struct file *file, |
| 345 | unsigned int cmd, unsigned long arg) |
| 346 | { |
| 347 | switch (cmd) { |
| 348 | case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */ |
| 349 | return pty_set_lock(tty, (int __user *) arg); |
| 350 | } |
| 351 | return -ENOIOCTLCMD; |
| 352 | } |
| 353 | |
Kay Sievers | dc8c858 | 2007-08-15 12:25:38 +0200 | [diff] [blame] | 354 | static int legacy_count = CONFIG_LEGACY_PTY_COUNT; |
| 355 | module_param(legacy_count, int, 0); |
| 356 | |
Alan Cox | 3e8e88c | 2008-04-30 00:54:10 -0700 | [diff] [blame] | 357 | static const struct tty_operations pty_ops_bsd = { |
| 358 | .open = pty_open, |
| 359 | .close = pty_close, |
| 360 | .write = pty_write, |
| 361 | .write_room = pty_write_room, |
| 362 | .flush_buffer = pty_flush_buffer, |
| 363 | .chars_in_buffer = pty_chars_in_buffer, |
| 364 | .unthrottle = pty_unthrottle, |
| 365 | .set_termios = pty_set_termios, |
| 366 | .ioctl = pty_bsd_ioctl, |
Alan Cox | fc6f623 | 2009-01-02 13:43:17 +0000 | [diff] [blame] | 367 | .resize = pty_resize |
Alan Cox | 3e8e88c | 2008-04-30 00:54:10 -0700 | [diff] [blame] | 368 | }; |
| 369 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | static void __init legacy_pty_init(void) |
| 371 | { |
Kay Sievers | dc8c858 | 2007-08-15 12:25:38 +0200 | [diff] [blame] | 372 | if (legacy_count <= 0) |
| 373 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | |
Kay Sievers | dc8c858 | 2007-08-15 12:25:38 +0200 | [diff] [blame] | 375 | pty_driver = alloc_tty_driver(legacy_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | if (!pty_driver) |
| 377 | panic("Couldn't allocate pty driver"); |
| 378 | |
Kay Sievers | dc8c858 | 2007-08-15 12:25:38 +0200 | [diff] [blame] | 379 | pty_slave_driver = alloc_tty_driver(legacy_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | if (!pty_slave_driver) |
| 381 | panic("Couldn't allocate pty slave driver"); |
| 382 | |
| 383 | pty_driver->owner = THIS_MODULE; |
| 384 | pty_driver->driver_name = "pty_master"; |
| 385 | pty_driver->name = "pty"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | pty_driver->major = PTY_MASTER_MAJOR; |
| 387 | pty_driver->minor_start = 0; |
| 388 | pty_driver->type = TTY_DRIVER_TYPE_PTY; |
| 389 | pty_driver->subtype = PTY_TYPE_MASTER; |
| 390 | pty_driver->init_termios = tty_std_termios; |
| 391 | pty_driver->init_termios.c_iflag = 0; |
| 392 | pty_driver->init_termios.c_oflag = 0; |
| 393 | pty_driver->init_termios.c_cflag = B38400 | CS8 | CREAD; |
| 394 | pty_driver->init_termios.c_lflag = 0; |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 395 | pty_driver->init_termios.c_ispeed = 38400; |
| 396 | pty_driver->init_termios.c_ospeed = 38400; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | pty_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW; |
| 398 | pty_driver->other = pty_slave_driver; |
| 399 | tty_set_operations(pty_driver, &pty_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | |
| 401 | pty_slave_driver->owner = THIS_MODULE; |
| 402 | pty_slave_driver->driver_name = "pty_slave"; |
| 403 | pty_slave_driver->name = "ttyp"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | pty_slave_driver->major = PTY_SLAVE_MAJOR; |
| 405 | pty_slave_driver->minor_start = 0; |
| 406 | pty_slave_driver->type = TTY_DRIVER_TYPE_PTY; |
| 407 | pty_slave_driver->subtype = PTY_TYPE_SLAVE; |
| 408 | pty_slave_driver->init_termios = tty_std_termios; |
| 409 | pty_slave_driver->init_termios.c_cflag = B38400 | CS8 | CREAD; |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 410 | pty_slave_driver->init_termios.c_ispeed = 38400; |
| 411 | pty_slave_driver->init_termios.c_ospeed = 38400; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | pty_slave_driver->flags = TTY_DRIVER_RESET_TERMIOS | |
| 413 | TTY_DRIVER_REAL_RAW; |
| 414 | pty_slave_driver->other = pty_driver; |
| 415 | tty_set_operations(pty_slave_driver, &pty_ops); |
| 416 | |
| 417 | if (tty_register_driver(pty_driver)) |
| 418 | panic("Couldn't register pty driver"); |
| 419 | if (tty_register_driver(pty_slave_driver)) |
| 420 | panic("Couldn't register pty slave driver"); |
| 421 | } |
| 422 | #else |
| 423 | static inline void legacy_pty_init(void) { } |
| 424 | #endif |
| 425 | |
| 426 | /* Unix98 devices */ |
| 427 | #ifdef CONFIG_UNIX98_PTYS |
| 428 | /* |
| 429 | * sysctl support for setting limits on the number of Unix98 ptys allocated. |
| 430 | * Otherwise one can eat up all kernel memory by opening /dev/ptmx repeatedly. |
| 431 | */ |
| 432 | int pty_limit = NR_UNIX98_PTY_DEFAULT; |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 433 | static int pty_limit_min; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | static int pty_limit_max = NR_UNIX98_PTY_MAX; |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 435 | static int pty_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | |
Alan Cox | d81ed10 | 2008-10-13 10:41:42 +0100 | [diff] [blame] | 437 | static struct cdev ptmx_cdev; |
| 438 | |
Eric W. Biederman | 35834ca | 2007-10-18 03:05:31 -0700 | [diff] [blame] | 439 | static struct ctl_table pty_table[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | { |
| 441 | .ctl_name = PTY_MAX, |
| 442 | .procname = "max", |
| 443 | .maxlen = sizeof(int), |
| 444 | .mode = 0644, |
| 445 | .data = &pty_limit, |
| 446 | .proc_handler = &proc_dointvec_minmax, |
| 447 | .strategy = &sysctl_intvec, |
| 448 | .extra1 = &pty_limit_min, |
| 449 | .extra2 = &pty_limit_max, |
| 450 | }, { |
| 451 | .ctl_name = PTY_NR, |
| 452 | .procname = "nr", |
| 453 | .maxlen = sizeof(int), |
| 454 | .mode = 0444, |
Alan Cox | bf970ee | 2008-10-13 10:42:39 +0100 | [diff] [blame] | 455 | .data = &pty_count, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | .proc_handler = &proc_dointvec, |
| 457 | }, { |
| 458 | .ctl_name = 0 |
| 459 | } |
| 460 | }; |
| 461 | |
Eric W. Biederman | 35834ca | 2007-10-18 03:05:31 -0700 | [diff] [blame] | 462 | static struct ctl_table pty_kern_table[] = { |
| 463 | { |
| 464 | .ctl_name = KERN_PTY, |
| 465 | .procname = "pty", |
| 466 | .mode = 0555, |
| 467 | .child = pty_table, |
| 468 | }, |
| 469 | {} |
| 470 | }; |
| 471 | |
| 472 | static struct ctl_table pty_root_table[] = { |
| 473 | { |
| 474 | .ctl_name = CTL_KERN, |
| 475 | .procname = "kernel", |
| 476 | .mode = 0555, |
| 477 | .child = pty_kern_table, |
| 478 | }, |
| 479 | {} |
| 480 | }; |
| 481 | |
| 482 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | static int pty_unix98_ioctl(struct tty_struct *tty, struct file *file, |
| 484 | unsigned int cmd, unsigned long arg) |
| 485 | { |
| 486 | switch (cmd) { |
| 487 | case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */ |
| 488 | return pty_set_lock(tty, (int __user *)arg); |
| 489 | case TIOCGPTN: /* Get PT Number */ |
| 490 | return put_user(tty->index, (unsigned int __user *)arg); |
| 491 | } |
| 492 | |
| 493 | return -ENOIOCTLCMD; |
| 494 | } |
| 495 | |
Alan Cox | 99f1fe1 | 2008-10-13 10:42:00 +0100 | [diff] [blame] | 496 | /** |
| 497 | * ptm_unix98_lookup - find a pty master |
| 498 | * @driver: ptm driver |
| 499 | * @idx: tty index |
| 500 | * |
| 501 | * Look up a pty master device. Called under the tty_mutex for now. |
| 502 | * This provides our locking. |
| 503 | */ |
| 504 | |
Sukadev Bhattiprolu | 15f1a63 | 2008-10-13 10:42:59 +0100 | [diff] [blame] | 505 | static struct tty_struct *ptm_unix98_lookup(struct tty_driver *driver, |
| 506 | struct inode *ptm_inode, int idx) |
Alan Cox | 99f1fe1 | 2008-10-13 10:42:00 +0100 | [diff] [blame] | 507 | { |
Sukadev Bhattiprolu | 15f1a63 | 2008-10-13 10:42:59 +0100 | [diff] [blame] | 508 | struct tty_struct *tty = devpts_get_tty(ptm_inode, idx); |
Alan Cox | 99f1fe1 | 2008-10-13 10:42:00 +0100 | [diff] [blame] | 509 | if (tty) |
| 510 | tty = tty->link; |
| 511 | return tty; |
| 512 | } |
| 513 | |
| 514 | /** |
| 515 | * pts_unix98_lookup - find a pty slave |
| 516 | * @driver: pts driver |
| 517 | * @idx: tty index |
| 518 | * |
| 519 | * Look up a pty master device. Called under the tty_mutex for now. |
| 520 | * This provides our locking. |
| 521 | */ |
| 522 | |
Sukadev Bhattiprolu | 15f1a63 | 2008-10-13 10:42:59 +0100 | [diff] [blame] | 523 | static struct tty_struct *pts_unix98_lookup(struct tty_driver *driver, |
| 524 | struct inode *pts_inode, int idx) |
Alan Cox | 99f1fe1 | 2008-10-13 10:42:00 +0100 | [diff] [blame] | 525 | { |
Sukadev Bhattiprolu | 15f1a63 | 2008-10-13 10:42:59 +0100 | [diff] [blame] | 526 | struct tty_struct *tty = devpts_get_tty(pts_inode, idx); |
Alan Cox | 99f1fe1 | 2008-10-13 10:42:00 +0100 | [diff] [blame] | 527 | /* Master must be open before slave */ |
| 528 | if (!tty) |
| 529 | return ERR_PTR(-EIO); |
| 530 | return tty; |
| 531 | } |
| 532 | |
Alan Cox | bf970ee | 2008-10-13 10:42:39 +0100 | [diff] [blame] | 533 | static void pty_unix98_shutdown(struct tty_struct *tty) |
Alan Cox | feebed6 | 2008-10-13 10:41:30 +0100 | [diff] [blame] | 534 | { |
| 535 | /* We have our own method as we don't use the tty index */ |
| 536 | kfree(tty->termios); |
Alan Cox | feebed6 | 2008-10-13 10:41:30 +0100 | [diff] [blame] | 537 | } |
| 538 | |
Alan Cox | 8b0a88d | 2008-10-13 10:42:19 +0100 | [diff] [blame] | 539 | /* We have no need to install and remove our tty objects as devpts does all |
| 540 | the work for us */ |
| 541 | |
Alan Cox | bf970ee | 2008-10-13 10:42:39 +0100 | [diff] [blame] | 542 | static int pty_unix98_install(struct tty_driver *driver, struct tty_struct *tty) |
Alan Cox | 8b0a88d | 2008-10-13 10:42:19 +0100 | [diff] [blame] | 543 | { |
Alan Cox | bf970ee | 2008-10-13 10:42:39 +0100 | [diff] [blame] | 544 | struct tty_struct *o_tty; |
| 545 | int idx = tty->index; |
| 546 | |
| 547 | o_tty = alloc_tty_struct(); |
| 548 | if (!o_tty) |
| 549 | return -ENOMEM; |
| 550 | if (!try_module_get(driver->other->owner)) { |
| 551 | /* This cannot in fact currently happen */ |
| 552 | free_tty_struct(o_tty); |
| 553 | return -ENOMEM; |
| 554 | } |
| 555 | initialize_tty_struct(o_tty, driver->other, idx); |
| 556 | |
Alan Cox | 8dff04e | 2008-10-13 10:43:58 +0100 | [diff] [blame] | 557 | tty->termios = kzalloc(sizeof(struct ktermios[2]), GFP_KERNEL); |
Alan Cox | bf970ee | 2008-10-13 10:42:39 +0100 | [diff] [blame] | 558 | if (tty->termios == NULL) |
| 559 | goto free_mem_out; |
| 560 | *tty->termios = driver->init_termios; |
Alan Cox | 8dff04e | 2008-10-13 10:43:58 +0100 | [diff] [blame] | 561 | tty->termios_locked = tty->termios + 1; |
| 562 | |
| 563 | o_tty->termios = kzalloc(sizeof(struct ktermios[2]), GFP_KERNEL); |
Alan Cox | bf970ee | 2008-10-13 10:42:39 +0100 | [diff] [blame] | 564 | if (o_tty->termios == NULL) |
| 565 | goto free_mem_out; |
| 566 | *o_tty->termios = driver->other->init_termios; |
Alan Cox | 8dff04e | 2008-10-13 10:43:58 +0100 | [diff] [blame] | 567 | o_tty->termios_locked = o_tty->termios + 1; |
Alan Cox | bf970ee | 2008-10-13 10:42:39 +0100 | [diff] [blame] | 568 | |
| 569 | tty_driver_kref_get(driver->other); |
| 570 | if (driver->subtype == PTY_TYPE_MASTER) |
| 571 | o_tty->count++; |
| 572 | /* Establish the links in both directions */ |
| 573 | tty->link = o_tty; |
| 574 | o_tty->link = tty; |
| 575 | /* |
| 576 | * All structures have been allocated, so now we install them. |
| 577 | * Failures after this point use release_tty to clean up, so |
| 578 | * there's no need to null out the local pointers. |
| 579 | */ |
| 580 | tty_driver_kref_get(driver); |
| 581 | tty->count++; |
| 582 | pty_count++; |
Alan Cox | 8b0a88d | 2008-10-13 10:42:19 +0100 | [diff] [blame] | 583 | return 0; |
Alan Cox | bf970ee | 2008-10-13 10:42:39 +0100 | [diff] [blame] | 584 | free_mem_out: |
Alan Cox | 8dff04e | 2008-10-13 10:43:58 +0100 | [diff] [blame] | 585 | kfree(o_tty->termios); |
Alan Cox | bf970ee | 2008-10-13 10:42:39 +0100 | [diff] [blame] | 586 | module_put(o_tty->driver->owner); |
| 587 | free_tty_struct(o_tty); |
Alan Cox | 8dff04e | 2008-10-13 10:43:58 +0100 | [diff] [blame] | 588 | kfree(tty->termios); |
Alan Cox | bf970ee | 2008-10-13 10:42:39 +0100 | [diff] [blame] | 589 | return -ENOMEM; |
Alan Cox | 8b0a88d | 2008-10-13 10:42:19 +0100 | [diff] [blame] | 590 | } |
| 591 | |
Alan Cox | bf970ee | 2008-10-13 10:42:39 +0100 | [diff] [blame] | 592 | static void pty_unix98_remove(struct tty_driver *driver, struct tty_struct *tty) |
Alan Cox | 8b0a88d | 2008-10-13 10:42:19 +0100 | [diff] [blame] | 593 | { |
Alan Cox | bf970ee | 2008-10-13 10:42:39 +0100 | [diff] [blame] | 594 | pty_count--; |
Alan Cox | 8b0a88d | 2008-10-13 10:42:19 +0100 | [diff] [blame] | 595 | } |
| 596 | |
Alan Cox | feebed6 | 2008-10-13 10:41:30 +0100 | [diff] [blame] | 597 | static const struct tty_operations ptm_unix98_ops = { |
Alan Cox | 99f1fe1 | 2008-10-13 10:42:00 +0100 | [diff] [blame] | 598 | .lookup = ptm_unix98_lookup, |
Alan Cox | bf970ee | 2008-10-13 10:42:39 +0100 | [diff] [blame] | 599 | .install = pty_unix98_install, |
| 600 | .remove = pty_unix98_remove, |
Alan Cox | 3e8e88c | 2008-04-30 00:54:10 -0700 | [diff] [blame] | 601 | .open = pty_open, |
| 602 | .close = pty_close, |
| 603 | .write = pty_write, |
| 604 | .write_room = pty_write_room, |
| 605 | .flush_buffer = pty_flush_buffer, |
| 606 | .chars_in_buffer = pty_chars_in_buffer, |
| 607 | .unthrottle = pty_unthrottle, |
| 608 | .set_termios = pty_set_termios, |
Alan Cox | feebed6 | 2008-10-13 10:41:30 +0100 | [diff] [blame] | 609 | .ioctl = pty_unix98_ioctl, |
Alan Cox | fc6f623 | 2009-01-02 13:43:17 +0000 | [diff] [blame] | 610 | .shutdown = pty_unix98_shutdown, |
| 611 | .resize = pty_resize |
Alan Cox | 3e8e88c | 2008-04-30 00:54:10 -0700 | [diff] [blame] | 612 | }; |
| 613 | |
Alan Cox | 99f1fe1 | 2008-10-13 10:42:00 +0100 | [diff] [blame] | 614 | static const struct tty_operations pty_unix98_ops = { |
| 615 | .lookup = pts_unix98_lookup, |
Alan Cox | bf970ee | 2008-10-13 10:42:39 +0100 | [diff] [blame] | 616 | .install = pty_unix98_install, |
| 617 | .remove = pty_unix98_remove, |
Alan Cox | 99f1fe1 | 2008-10-13 10:42:00 +0100 | [diff] [blame] | 618 | .open = pty_open, |
| 619 | .close = pty_close, |
| 620 | .write = pty_write, |
| 621 | .write_room = pty_write_room, |
| 622 | .flush_buffer = pty_flush_buffer, |
| 623 | .chars_in_buffer = pty_chars_in_buffer, |
| 624 | .unthrottle = pty_unthrottle, |
| 625 | .set_termios = pty_set_termios, |
Alan Cox | bf970ee | 2008-10-13 10:42:39 +0100 | [diff] [blame] | 626 | .shutdown = pty_unix98_shutdown |
Alan Cox | 99f1fe1 | 2008-10-13 10:42:00 +0100 | [diff] [blame] | 627 | }; |
Alan Cox | d81ed10 | 2008-10-13 10:41:42 +0100 | [diff] [blame] | 628 | |
| 629 | /** |
| 630 | * ptmx_open - open a unix 98 pty master |
| 631 | * @inode: inode of device file |
| 632 | * @filp: file pointer to tty |
| 633 | * |
| 634 | * Allocate a unix98 pty master device from the ptmx driver. |
| 635 | * |
| 636 | * Locking: tty_mutex protects the init_dev work. tty->count should |
| 637 | * protect the rest. |
| 638 | * allocated_ptys_lock handles the list of free pty numbers |
| 639 | */ |
| 640 | |
| 641 | static int __ptmx_open(struct inode *inode, struct file *filp) |
| 642 | { |
| 643 | struct tty_struct *tty; |
| 644 | int retval; |
| 645 | int index; |
| 646 | |
| 647 | nonseekable_open(inode, filp); |
| 648 | |
| 649 | /* find a device that is not in use. */ |
Sukadev Bhattiprolu | 15f1a63 | 2008-10-13 10:42:59 +0100 | [diff] [blame] | 650 | index = devpts_new_index(inode); |
Alan Cox | d81ed10 | 2008-10-13 10:41:42 +0100 | [diff] [blame] | 651 | if (index < 0) |
| 652 | return index; |
| 653 | |
| 654 | mutex_lock(&tty_mutex); |
Alan Cox | 73ec06f | 2008-10-13 10:42:29 +0100 | [diff] [blame] | 655 | tty = tty_init_dev(ptm_driver, index, 1); |
Alan Cox | d81ed10 | 2008-10-13 10:41:42 +0100 | [diff] [blame] | 656 | mutex_unlock(&tty_mutex); |
| 657 | |
Alan Cox | 73ec06f | 2008-10-13 10:42:29 +0100 | [diff] [blame] | 658 | if (IS_ERR(tty)) { |
| 659 | retval = PTR_ERR(tty); |
Alan Cox | d81ed10 | 2008-10-13 10:41:42 +0100 | [diff] [blame] | 660 | goto out; |
Alan Cox | 73ec06f | 2008-10-13 10:42:29 +0100 | [diff] [blame] | 661 | } |
Alan Cox | d81ed10 | 2008-10-13 10:41:42 +0100 | [diff] [blame] | 662 | |
| 663 | set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */ |
| 664 | filp->private_data = tty; |
| 665 | file_move(filp, &tty->tty_files); |
| 666 | |
Sukadev Bhattiprolu | 15f1a63 | 2008-10-13 10:42:59 +0100 | [diff] [blame] | 667 | retval = devpts_pty_new(inode, tty->link); |
Alan Cox | d81ed10 | 2008-10-13 10:41:42 +0100 | [diff] [blame] | 668 | if (retval) |
| 669 | goto out1; |
| 670 | |
| 671 | retval = ptm_driver->ops->open(tty, filp); |
| 672 | if (!retval) |
| 673 | return 0; |
| 674 | out1: |
| 675 | tty_release_dev(filp); |
| 676 | return retval; |
| 677 | out: |
Sukadev Bhattiprolu | 15f1a63 | 2008-10-13 10:42:59 +0100 | [diff] [blame] | 678 | devpts_kill_index(inode, index); |
Alan Cox | d81ed10 | 2008-10-13 10:41:42 +0100 | [diff] [blame] | 679 | return retval; |
| 680 | } |
| 681 | |
| 682 | static int ptmx_open(struct inode *inode, struct file *filp) |
| 683 | { |
| 684 | int ret; |
| 685 | |
| 686 | lock_kernel(); |
| 687 | ret = __ptmx_open(inode, filp); |
| 688 | unlock_kernel(); |
| 689 | return ret; |
| 690 | } |
| 691 | |
| 692 | static struct file_operations ptmx_fops; |
| 693 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | static void __init unix98_pty_init(void) |
| 695 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | ptm_driver = alloc_tty_driver(NR_UNIX98_PTY_MAX); |
| 697 | if (!ptm_driver) |
| 698 | panic("Couldn't allocate Unix98 ptm driver"); |
| 699 | pts_driver = alloc_tty_driver(NR_UNIX98_PTY_MAX); |
| 700 | if (!pts_driver) |
| 701 | panic("Couldn't allocate Unix98 pts driver"); |
| 702 | |
| 703 | ptm_driver->owner = THIS_MODULE; |
| 704 | ptm_driver->driver_name = "pty_master"; |
| 705 | ptm_driver->name = "ptm"; |
| 706 | ptm_driver->major = UNIX98_PTY_MASTER_MAJOR; |
| 707 | ptm_driver->minor_start = 0; |
| 708 | ptm_driver->type = TTY_DRIVER_TYPE_PTY; |
| 709 | ptm_driver->subtype = PTY_TYPE_MASTER; |
| 710 | ptm_driver->init_termios = tty_std_termios; |
| 711 | ptm_driver->init_termios.c_iflag = 0; |
| 712 | ptm_driver->init_termios.c_oflag = 0; |
| 713 | ptm_driver->init_termios.c_cflag = B38400 | CS8 | CREAD; |
| 714 | ptm_driver->init_termios.c_lflag = 0; |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 715 | ptm_driver->init_termios.c_ispeed = 38400; |
| 716 | ptm_driver->init_termios.c_ospeed = 38400; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | ptm_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW | |
Greg Kroah-Hartman | 331b831 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 718 | TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_DEVPTS_MEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | ptm_driver->other = pts_driver; |
Alan Cox | feebed6 | 2008-10-13 10:41:30 +0100 | [diff] [blame] | 720 | tty_set_operations(ptm_driver, &ptm_unix98_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | |
| 722 | pts_driver->owner = THIS_MODULE; |
| 723 | pts_driver->driver_name = "pty_slave"; |
| 724 | pts_driver->name = "pts"; |
| 725 | pts_driver->major = UNIX98_PTY_SLAVE_MAJOR; |
| 726 | pts_driver->minor_start = 0; |
| 727 | pts_driver->type = TTY_DRIVER_TYPE_PTY; |
| 728 | pts_driver->subtype = PTY_TYPE_SLAVE; |
| 729 | pts_driver->init_termios = tty_std_termios; |
| 730 | pts_driver->init_termios.c_cflag = B38400 | CS8 | CREAD; |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 731 | pts_driver->init_termios.c_ispeed = 38400; |
| 732 | pts_driver->init_termios.c_ospeed = 38400; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | pts_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW | |
Greg Kroah-Hartman | 331b831 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 734 | TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_DEVPTS_MEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | pts_driver->other = ptm_driver; |
Alan Cox | 99f1fe1 | 2008-10-13 10:42:00 +0100 | [diff] [blame] | 736 | tty_set_operations(pts_driver, &pty_unix98_ops); |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 737 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | if (tty_register_driver(ptm_driver)) |
| 739 | panic("Couldn't register Unix98 ptm driver"); |
| 740 | if (tty_register_driver(pts_driver)) |
| 741 | panic("Couldn't register Unix98 pts driver"); |
| 742 | |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 743 | register_sysctl_table(pty_root_table); |
Alan Cox | d81ed10 | 2008-10-13 10:41:42 +0100 | [diff] [blame] | 744 | |
| 745 | /* Now create the /dev/ptmx special device */ |
| 746 | tty_default_fops(&ptmx_fops); |
| 747 | ptmx_fops.open = ptmx_open; |
| 748 | |
| 749 | cdev_init(&ptmx_cdev, &ptmx_fops); |
| 750 | if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) || |
| 751 | register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0) |
| 752 | panic("Couldn't register /dev/ptmx driver\n"); |
| 753 | device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), NULL, "ptmx"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | } |
Alan Cox | d81ed10 | 2008-10-13 10:41:42 +0100 | [diff] [blame] | 755 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | #else |
| 757 | static inline void unix98_pty_init(void) { } |
| 758 | #endif |
| 759 | |
| 760 | static int __init pty_init(void) |
| 761 | { |
| 762 | legacy_pty_init(); |
| 763 | unix98_pty_init(); |
| 764 | return 0; |
| 765 | } |
| 766 | module_init(pty_init); |