diff options
author | Alan Cox <alan@redhat.com> | 2008-10-13 05:43:38 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2008-10-13 12:51:43 -0400 |
commit | fe9cd962a62cb5f666cf48b9941d3f3cde134254 (patch) | |
tree | c493585da2814bc8061c84fd2052a4407105d1e1 /drivers | |
parent | a6f37daa8b892fd29dd71be0de61460a478cb122 (diff) |
pty: Coding style and polish
We've done the heavy lifting now its time to mop up a bit
Signed-off-by: Alan Cox <alan@redhat.com>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'drivers')
-rw-r--r-- | drivers/char/pty.c | 86 |
1 files changed, 44 insertions, 42 deletions
diff --git a/drivers/char/pty.c b/drivers/char/pty.c index a391badef52a..c3ab8c3110c9 100644 --- a/drivers/char/pty.c +++ b/drivers/char/pty.c | |||
@@ -8,10 +8,12 @@ | |||
8 | * Added TTY_DO_WRITE_WAKEUP to enable n_tty to send POLL_OUT to | 8 | * Added TTY_DO_WRITE_WAKEUP to enable n_tty to send POLL_OUT to |
9 | * waiting writers -- Sapan Bhatia <sapan@corewars.org> | 9 | * waiting writers -- Sapan Bhatia <sapan@corewars.org> |
10 | * | 10 | * |
11 | * | 11 | * When reading this code see also fs/devpts. In particular note that the |
12 | * driver_data field is used by the devpts side as a binding to the devpts | ||
13 | * inode. | ||
12 | */ | 14 | */ |
13 | 15 | ||
14 | #include <linux/module.h> /* For EXPORT_SYMBOL */ | 16 | #include <linux/module.h> |
15 | 17 | ||
16 | #include <linux/errno.h> | 18 | #include <linux/errno.h> |
17 | #include <linux/interrupt.h> | 19 | #include <linux/interrupt.h> |
@@ -24,26 +26,24 @@ | |||
24 | #include <linux/init.h> | 26 | #include <linux/init.h> |
25 | #include <linux/sysctl.h> | 27 | #include <linux/sysctl.h> |
26 | #include <linux/device.h> | 28 | #include <linux/device.h> |
27 | 29 | #include <linux/uaccess.h> | |
28 | #include <asm/uaccess.h> | ||
29 | #include <asm/system.h> | ||
30 | #include <linux/bitops.h> | 30 | #include <linux/bitops.h> |
31 | #include <linux/devpts_fs.h> | 31 | #include <linux/devpts_fs.h> |
32 | 32 | ||
33 | #include <asm/system.h> | ||
34 | |||
33 | /* These are global because they are accessed in tty_io.c */ | 35 | /* These are global because they are accessed in tty_io.c */ |
34 | #ifdef CONFIG_UNIX98_PTYS | 36 | #ifdef CONFIG_UNIX98_PTYS |
35 | struct tty_driver *ptm_driver; | 37 | struct tty_driver *ptm_driver; |
36 | static struct tty_driver *pts_driver; | 38 | static struct tty_driver *pts_driver; |
37 | #endif | 39 | #endif |
38 | 40 | ||
39 | static void pty_close(struct tty_struct * tty, struct file * filp) | 41 | static void pty_close(struct tty_struct *tty, struct file *filp) |
40 | { | 42 | { |
41 | if (!tty) | 43 | BUG_ON(!tty); |
42 | return; | 44 | if (tty->driver->subtype == PTY_TYPE_MASTER) |
43 | if (tty->driver->subtype == PTY_TYPE_MASTER) { | 45 | WARN_ON(tty->count > 1); |
44 | if (tty->count > 1) | 46 | else { |
45 | printk("master pty_close: count = %d!!\n", tty->count); | ||
46 | } else { | ||
47 | if (tty->count > 2) | 47 | if (tty->count > 2) |
48 | return; | 48 | return; |
49 | } | 49 | } |
@@ -70,13 +70,13 @@ static void pty_close(struct tty_struct * tty, struct file * filp) | |||
70 | * The unthrottle routine is called by the line discipline to signal | 70 | * The unthrottle routine is called by the line discipline to signal |
71 | * that it can receive more characters. For PTY's, the TTY_THROTTLED | 71 | * that it can receive more characters. For PTY's, the TTY_THROTTLED |
72 | * flag is always set, to force the line discipline to always call the | 72 | * flag is always set, to force the line discipline to always call the |
73 | * unthrottle routine when there are fewer than TTY_THRESHOLD_UNTHROTTLE | 73 | * unthrottle routine when there are fewer than TTY_THRESHOLD_UNTHROTTLE |
74 | * characters in the queue. This is necessary since each time this | 74 | * characters in the queue. This is necessary since each time this |
75 | * happens, we need to wake up any sleeping processes that could be | 75 | * happens, we need to wake up any sleeping processes that could be |
76 | * (1) trying to send data to the pty, or (2) waiting in wait_until_sent() | 76 | * (1) trying to send data to the pty, or (2) waiting in wait_until_sent() |
77 | * for the pty buffer to be drained. | 77 | * for the pty buffer to be drained. |
78 | */ | 78 | */ |
79 | static void pty_unthrottle(struct tty_struct * tty) | 79 | static void pty_unthrottle(struct tty_struct *tty) |
80 | { | 80 | { |
81 | struct tty_struct *o_tty = tty->link; | 81 | struct tty_struct *o_tty = tty->link; |
82 | 82 | ||
@@ -88,7 +88,7 @@ static void pty_unthrottle(struct tty_struct * tty) | |||
88 | } | 88 | } |
89 | 89 | ||
90 | /* | 90 | /* |
91 | * WSH 05/24/97: modified to | 91 | * WSH 05/24/97: modified to |
92 | * (1) use space in tty->flip instead of a shared temp buffer | 92 | * (1) use space in tty->flip instead of a shared temp buffer |
93 | * The flip buffers aren't being used for a pty, so there's lots | 93 | * The flip buffers aren't being used for a pty, so there's lots |
94 | * of space available. The buffer is protected by a per-pty | 94 | * of space available. The buffer is protected by a per-pty |
@@ -101,7 +101,8 @@ static void pty_unthrottle(struct tty_struct * tty) | |||
101 | * not our partners. We can't just take the other one blindly without | 101 | * not our partners. We can't just take the other one blindly without |
102 | * risking deadlocks. | 102 | * risking deadlocks. |
103 | */ | 103 | */ |
104 | static int pty_write(struct tty_struct * tty, const unsigned char *buf, int count) | 104 | static int pty_write(struct tty_struct *tty, const unsigned char *buf, |
105 | int count) | ||
105 | { | 106 | { |
106 | struct tty_struct *to = tty->link; | 107 | struct tty_struct *to = tty->link; |
107 | int c; | 108 | int c; |
@@ -113,7 +114,7 @@ static int pty_write(struct tty_struct * tty, const unsigned char *buf, int coun | |||
113 | if (c > count) | 114 | if (c > count) |
114 | c = count; | 115 | c = count; |
115 | to->ldisc.ops->receive_buf(to, buf, NULL, c); | 116 | to->ldisc.ops->receive_buf(to, buf, NULL, c); |
116 | 117 | ||
117 | return c; | 118 | return c; |
118 | } | 119 | } |
119 | 120 | ||
@@ -129,17 +130,17 @@ static int pty_write_room(struct tty_struct *tty) | |||
129 | 130 | ||
130 | /* | 131 | /* |
131 | * WSH 05/24/97: Modified for asymmetric MASTER/SLAVE behavior | 132 | * WSH 05/24/97: Modified for asymmetric MASTER/SLAVE behavior |
132 | * The chars_in_buffer() value is used by the ldisc select() function | 133 | * The chars_in_buffer() value is used by the ldisc select() function |
133 | * to hold off writing when chars_in_buffer > WAKEUP_CHARS (== 256). | 134 | * to hold off writing when chars_in_buffer > WAKEUP_CHARS (== 256). |
134 | * The pty driver chars_in_buffer() Master/Slave must behave differently: | 135 | * The pty driver chars_in_buffer() Master/Slave must behave differently: |
135 | * | 136 | * |
136 | * The Master side needs to allow typed-ahead commands to accumulate | 137 | * The Master side needs to allow typed-ahead commands to accumulate |
137 | * while being canonicalized, so we report "our buffer" as empty until | 138 | * while being canonicalized, so we report "our buffer" as empty until |
138 | * some threshold is reached, and then report the count. (Any count > | 139 | * some threshold is reached, and then report the count. (Any count > |
139 | * WAKEUP_CHARS is regarded by select() as "full".) To avoid deadlock | 140 | * WAKEUP_CHARS is regarded by select() as "full".) To avoid deadlock |
140 | * the count returned must be 0 if no canonical data is available to be | 141 | * the count returned must be 0 if no canonical data is available to be |
141 | * read. (The N_TTY ldisc.chars_in_buffer now knows this.) | 142 | * read. (The N_TTY ldisc.chars_in_buffer now knows this.) |
142 | * | 143 | * |
143 | * The Slave side passes all characters in raw mode to the Master side's | 144 | * The Slave side passes all characters in raw mode to the Master side's |
144 | * buffer where they can be read immediately, so in this case we can | 145 | * buffer where they can be read immediately, so in this case we can |
145 | * return the true count in the buffer. | 146 | * return the true count in the buffer. |
@@ -156,21 +157,22 @@ static int pty_chars_in_buffer(struct tty_struct *tty) | |||
156 | /* The ldisc must report 0 if no characters available to be read */ | 157 | /* The ldisc must report 0 if no characters available to be read */ |
157 | count = to->ldisc.ops->chars_in_buffer(to); | 158 | count = to->ldisc.ops->chars_in_buffer(to); |
158 | 159 | ||
159 | if (tty->driver->subtype == PTY_TYPE_SLAVE) return count; | 160 | if (tty->driver->subtype == PTY_TYPE_SLAVE) |
161 | return count; | ||
160 | 162 | ||
161 | /* Master side driver ... if the other side's read buffer is less than | 163 | /* Master side driver ... if the other side's read buffer is less than |
162 | * half full, return 0 to allow writers to proceed; otherwise return | 164 | * half full, return 0 to allow writers to proceed; otherwise return |
163 | * the count. This leaves a comfortable margin to avoid overflow, | 165 | * the count. This leaves a comfortable margin to avoid overflow, |
164 | * and still allows half a buffer's worth of typed-ahead commands. | 166 | * and still allows half a buffer's worth of typed-ahead commands. |
165 | */ | 167 | */ |
166 | return ((count < N_TTY_BUF_SIZE/2) ? 0 : count); | 168 | return (count < N_TTY_BUF_SIZE/2) ? 0 : count; |
167 | } | 169 | } |
168 | 170 | ||
169 | /* Set the lock flag on a pty */ | 171 | /* Set the lock flag on a pty */ |
170 | static int pty_set_lock(struct tty_struct *tty, int __user * arg) | 172 | static int pty_set_lock(struct tty_struct *tty, int __user *arg) |
171 | { | 173 | { |
172 | int val; | 174 | int val; |
173 | if (get_user(val,arg)) | 175 | if (get_user(val, arg)) |
174 | return -EFAULT; | 176 | return -EFAULT; |
175 | if (val) | 177 | if (val) |
176 | set_bit(TTY_PTY_LOCK, &tty->flags); | 178 | set_bit(TTY_PTY_LOCK, &tty->flags); |
@@ -183,13 +185,13 @@ static void pty_flush_buffer(struct tty_struct *tty) | |||
183 | { | 185 | { |
184 | struct tty_struct *to = tty->link; | 186 | struct tty_struct *to = tty->link; |
185 | unsigned long flags; | 187 | unsigned long flags; |
186 | 188 | ||
187 | if (!to) | 189 | if (!to) |
188 | return; | 190 | return; |
189 | 191 | ||
190 | if (to->ldisc.ops->flush_buffer) | 192 | if (to->ldisc.ops->flush_buffer) |
191 | to->ldisc.ops->flush_buffer(to); | 193 | to->ldisc.ops->flush_buffer(to); |
192 | 194 | ||
193 | if (to->packet) { | 195 | if (to->packet) { |
194 | spin_lock_irqsave(&tty->ctrl_lock, flags); | 196 | spin_lock_irqsave(&tty->ctrl_lock, flags); |
195 | tty->ctrl_status |= TIOCPKT_FLUSHWRITE; | 197 | tty->ctrl_status |= TIOCPKT_FLUSHWRITE; |
@@ -198,7 +200,7 @@ static void pty_flush_buffer(struct tty_struct *tty) | |||
198 | } | 200 | } |
199 | } | 201 | } |
200 | 202 | ||
201 | static int pty_open(struct tty_struct *tty, struct file * filp) | 203 | static int pty_open(struct tty_struct *tty, struct file *filp) |
202 | { | 204 | { |
203 | int retval = -ENODEV; | 205 | int retval = -ENODEV; |
204 | 206 | ||
@@ -221,10 +223,11 @@ out: | |||
221 | return retval; | 223 | return retval; |
222 | } | 224 | } |
223 | 225 | ||
224 | static void pty_set_termios(struct tty_struct *tty, struct ktermios *old_termios) | 226 | static void pty_set_termios(struct tty_struct *tty, |
227 | struct ktermios *old_termios) | ||
225 | { | 228 | { |
226 | tty->termios->c_cflag &= ~(CSIZE | PARENB); | 229 | tty->termios->c_cflag &= ~(CSIZE | PARENB); |
227 | tty->termios->c_cflag |= (CS8 | CREAD); | 230 | tty->termios->c_cflag |= (CS8 | CREAD); |
228 | } | 231 | } |
229 | 232 | ||
230 | static int pty_install(struct tty_driver *driver, struct tty_struct *tty) | 233 | static int pty_install(struct tty_driver *driver, struct tty_struct *tty) |
@@ -254,7 +257,7 @@ static int pty_install(struct tty_driver *driver, struct tty_struct *tty) | |||
254 | tty_free_termios(tty); | 257 | tty_free_termios(tty); |
255 | goto free_mem_out; | 258 | goto free_mem_out; |
256 | } | 259 | } |
257 | 260 | ||
258 | /* | 261 | /* |
259 | * Everything allocated ... set up the o_tty structure. | 262 | * Everything allocated ... set up the o_tty structure. |
260 | */ | 263 | */ |
@@ -381,9 +384,9 @@ static inline void legacy_pty_init(void) { } | |||
381 | * Otherwise one can eat up all kernel memory by opening /dev/ptmx repeatedly. | 384 | * Otherwise one can eat up all kernel memory by opening /dev/ptmx repeatedly. |
382 | */ | 385 | */ |
383 | int pty_limit = NR_UNIX98_PTY_DEFAULT; | 386 | int pty_limit = NR_UNIX98_PTY_DEFAULT; |
384 | static int pty_limit_min = 0; | 387 | static int pty_limit_min; |
385 | static int pty_limit_max = NR_UNIX98_PTY_MAX; | 388 | static int pty_limit_max = NR_UNIX98_PTY_MAX; |
386 | static int pty_count = 0; | 389 | static int pty_count; |
387 | 390 | ||
388 | static struct cdev ptmx_cdev; | 391 | static struct cdev ptmx_cdev; |
389 | 392 | ||
@@ -537,11 +540,10 @@ static int pty_unix98_install(struct tty_driver *driver, struct tty_struct *tty) | |||
537 | pty_count++; | 540 | pty_count++; |
538 | return 0; | 541 | return 0; |
539 | free_mem_out: | 542 | free_mem_out: |
540 | kfree(o_tty->termios); | 543 | pty_unix98_shutdown(o_tty); |
541 | module_put(o_tty->driver->owner); | 544 | module_put(o_tty->driver->owner); |
542 | free_tty_struct(o_tty); | 545 | free_tty_struct(o_tty); |
543 | kfree(tty->termios_locked); | 546 | pty_unix98_shutdown(tty); |
544 | kfree(tty->termios); | ||
545 | free_tty_struct(tty); | 547 | free_tty_struct(tty); |
546 | module_put(driver->owner); | 548 | module_put(driver->owner); |
547 | return -ENOMEM; | 549 | return -ENOMEM; |
@@ -691,13 +693,13 @@ static void __init unix98_pty_init(void) | |||
691 | TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_DEVPTS_MEM; | 693 | TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_DEVPTS_MEM; |
692 | pts_driver->other = ptm_driver; | 694 | pts_driver->other = ptm_driver; |
693 | tty_set_operations(pts_driver, &pty_unix98_ops); | 695 | tty_set_operations(pts_driver, &pty_unix98_ops); |
694 | 696 | ||
695 | if (tty_register_driver(ptm_driver)) | 697 | if (tty_register_driver(ptm_driver)) |
696 | panic("Couldn't register Unix98 ptm driver"); | 698 | panic("Couldn't register Unix98 ptm driver"); |
697 | if (tty_register_driver(pts_driver)) | 699 | if (tty_register_driver(pts_driver)) |
698 | panic("Couldn't register Unix98 pts driver"); | 700 | panic("Couldn't register Unix98 pts driver"); |
699 | 701 | ||
700 | register_sysctl_table(pty_root_table); | 702 | register_sysctl_table(pty_root_table); |
701 | 703 | ||
702 | /* Now create the /dev/ptmx special device */ | 704 | /* Now create the /dev/ptmx special device */ |
703 | tty_default_fops(&ptmx_fops); | 705 | tty_default_fops(&ptmx_fops); |