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