Commit 21622939fc452c7fb739464b8e49368c3ceaa0ee

Authored by Peter Hurley
Committed by Greg Kroah-Hartman
1 parent 91debb0383

tty: Add diagnostic for halted line discipline

Flip buffer work must not be scheduled by the line discipline
after the line discipline has been halted; issue warning.

Note: drivers can still schedule flip buffer work.

Signed-off-by: Peter Hurley <peter@hurleysoftware.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Showing 3 changed files with 15 additions and 1 deletions Inline Diff

1 /* 1 /*
2 * n_tty.c --- implements the N_TTY line discipline. 2 * n_tty.c --- implements the N_TTY line discipline.
3 * 3 *
4 * This code used to be in tty_io.c, but things are getting hairy 4 * This code used to be in tty_io.c, but things are getting hairy
5 * enough that it made sense to split things off. (The N_TTY 5 * enough that it made sense to split things off. (The N_TTY
6 * processing has changed so much that it's hardly recognizable, 6 * processing has changed so much that it's hardly recognizable,
7 * anyway...) 7 * anyway...)
8 * 8 *
9 * Note that the open routine for N_TTY is guaranteed never to return 9 * Note that the open routine for N_TTY is guaranteed never to return
10 * an error. This is because Linux will fall back to setting a line 10 * an error. This is because Linux will fall back to setting a line
11 * to N_TTY if it can not switch to any other line discipline. 11 * to N_TTY if it can not switch to any other line discipline.
12 * 12 *
13 * Written by Theodore Ts'o, Copyright 1994. 13 * Written by Theodore Ts'o, Copyright 1994.
14 * 14 *
15 * This file also contains code originally written by Linus Torvalds, 15 * This file also contains code originally written by Linus Torvalds,
16 * Copyright 1991, 1992, 1993, and by Julian Cowley, Copyright 1994. 16 * Copyright 1991, 1992, 1993, and by Julian Cowley, Copyright 1994.
17 * 17 *
18 * This file may be redistributed under the terms of the GNU General Public 18 * This file may be redistributed under the terms of the GNU General Public
19 * License. 19 * License.
20 * 20 *
21 * Reduced memory usage for older ARM systems - Russell King. 21 * Reduced memory usage for older ARM systems - Russell King.
22 * 22 *
23 * 2000/01/20 Fixed SMP locking on put_tty_queue using bits of 23 * 2000/01/20 Fixed SMP locking on put_tty_queue using bits of
24 * the patch by Andrew J. Kroll <ag784@freenet.buffalo.edu> 24 * the patch by Andrew J. Kroll <ag784@freenet.buffalo.edu>
25 * who actually finally proved there really was a race. 25 * who actually finally proved there really was a race.
26 * 26 *
27 * 2002/03/18 Implemented n_tty_wakeup to send SIGIO POLL_OUTs to 27 * 2002/03/18 Implemented n_tty_wakeup to send SIGIO POLL_OUTs to
28 * waiting writing processes-Sapan Bhatia <sapan@corewars.org>. 28 * waiting writing processes-Sapan Bhatia <sapan@corewars.org>.
29 * Also fixed a bug in BLOCKING mode where n_tty_write returns 29 * Also fixed a bug in BLOCKING mode where n_tty_write returns
30 * EAGAIN 30 * EAGAIN
31 */ 31 */
32 32
33 #include <linux/types.h> 33 #include <linux/types.h>
34 #include <linux/major.h> 34 #include <linux/major.h>
35 #include <linux/errno.h> 35 #include <linux/errno.h>
36 #include <linux/signal.h> 36 #include <linux/signal.h>
37 #include <linux/fcntl.h> 37 #include <linux/fcntl.h>
38 #include <linux/sched.h> 38 #include <linux/sched.h>
39 #include <linux/interrupt.h> 39 #include <linux/interrupt.h>
40 #include <linux/tty.h> 40 #include <linux/tty.h>
41 #include <linux/timer.h> 41 #include <linux/timer.h>
42 #include <linux/ctype.h> 42 #include <linux/ctype.h>
43 #include <linux/mm.h> 43 #include <linux/mm.h>
44 #include <linux/string.h> 44 #include <linux/string.h>
45 #include <linux/slab.h> 45 #include <linux/slab.h>
46 #include <linux/poll.h> 46 #include <linux/poll.h>
47 #include <linux/bitops.h> 47 #include <linux/bitops.h>
48 #include <linux/audit.h> 48 #include <linux/audit.h>
49 #include <linux/file.h> 49 #include <linux/file.h>
50 #include <linux/uaccess.h> 50 #include <linux/uaccess.h>
51 #include <linux/module.h> 51 #include <linux/module.h>
52 #include <linux/ratelimit.h> 52 #include <linux/ratelimit.h>
53 53
54 54
55 /* number of characters left in xmit buffer before select has we have room */ 55 /* number of characters left in xmit buffer before select has we have room */
56 #define WAKEUP_CHARS 256 56 #define WAKEUP_CHARS 256
57 57
58 /* 58 /*
59 * This defines the low- and high-watermarks for throttling and 59 * This defines the low- and high-watermarks for throttling and
60 * unthrottling the TTY driver. These watermarks are used for 60 * unthrottling the TTY driver. These watermarks are used for
61 * controlling the space in the read buffer. 61 * controlling the space in the read buffer.
62 */ 62 */
63 #define TTY_THRESHOLD_THROTTLE 128 /* now based on remaining room */ 63 #define TTY_THRESHOLD_THROTTLE 128 /* now based on remaining room */
64 #define TTY_THRESHOLD_UNTHROTTLE 128 64 #define TTY_THRESHOLD_UNTHROTTLE 128
65 65
66 /* 66 /*
67 * Special byte codes used in the echo buffer to represent operations 67 * Special byte codes used in the echo buffer to represent operations
68 * or special handling of characters. Bytes in the echo buffer that 68 * or special handling of characters. Bytes in the echo buffer that
69 * are not part of such special blocks are treated as normal character 69 * are not part of such special blocks are treated as normal character
70 * codes. 70 * codes.
71 */ 71 */
72 #define ECHO_OP_START 0xff 72 #define ECHO_OP_START 0xff
73 #define ECHO_OP_MOVE_BACK_COL 0x80 73 #define ECHO_OP_MOVE_BACK_COL 0x80
74 #define ECHO_OP_SET_CANON_COL 0x81 74 #define ECHO_OP_SET_CANON_COL 0x81
75 #define ECHO_OP_ERASE_TAB 0x82 75 #define ECHO_OP_ERASE_TAB 0x82
76 76
77 struct n_tty_data { 77 struct n_tty_data {
78 unsigned int column; 78 unsigned int column;
79 unsigned long overrun_time; 79 unsigned long overrun_time;
80 int num_overrun; 80 int num_overrun;
81 81
82 unsigned char lnext:1, erasing:1, raw:1, real_raw:1, icanon:1; 82 unsigned char lnext:1, erasing:1, raw:1, real_raw:1, icanon:1;
83 unsigned char echo_overrun:1; 83 unsigned char echo_overrun:1;
84 84
85 DECLARE_BITMAP(process_char_map, 256); 85 DECLARE_BITMAP(process_char_map, 256);
86 DECLARE_BITMAP(read_flags, N_TTY_BUF_SIZE); 86 DECLARE_BITMAP(read_flags, N_TTY_BUF_SIZE);
87 87
88 char *read_buf; 88 char *read_buf;
89 int read_head; 89 int read_head;
90 int read_tail; 90 int read_tail;
91 int read_cnt; 91 int read_cnt;
92 92
93 unsigned char *echo_buf; 93 unsigned char *echo_buf;
94 unsigned int echo_pos; 94 unsigned int echo_pos;
95 unsigned int echo_cnt; 95 unsigned int echo_cnt;
96 96
97 int canon_data; 97 int canon_data;
98 unsigned long canon_head; 98 unsigned long canon_head;
99 unsigned int canon_column; 99 unsigned int canon_column;
100 100
101 struct mutex atomic_read_lock; 101 struct mutex atomic_read_lock;
102 struct mutex output_lock; 102 struct mutex output_lock;
103 struct mutex echo_lock; 103 struct mutex echo_lock;
104 raw_spinlock_t read_lock; 104 raw_spinlock_t read_lock;
105 }; 105 };
106 106
107 static inline int tty_put_user(struct tty_struct *tty, unsigned char x, 107 static inline int tty_put_user(struct tty_struct *tty, unsigned char x,
108 unsigned char __user *ptr) 108 unsigned char __user *ptr)
109 { 109 {
110 struct n_tty_data *ldata = tty->disc_data; 110 struct n_tty_data *ldata = tty->disc_data;
111 111
112 tty_audit_add_data(tty, &x, 1, ldata->icanon); 112 tty_audit_add_data(tty, &x, 1, ldata->icanon);
113 return put_user(x, ptr); 113 return put_user(x, ptr);
114 } 114 }
115 115
116 /** 116 /**
117 * n_tty_set__room - receive space 117 * n_tty_set__room - receive space
118 * @tty: terminal 118 * @tty: terminal
119 * 119 *
120 * Called by the driver to find out how much data it is 120 * Called by the driver to find out how much data it is
121 * permitted to feed to the line discipline without any being lost 121 * permitted to feed to the line discipline without any being lost
122 * and thus to manage flow control. Not serialized. Answers for the 122 * and thus to manage flow control. Not serialized. Answers for the
123 * "instant". 123 * "instant".
124 */ 124 */
125 125
126 static void n_tty_set_room(struct tty_struct *tty) 126 static void n_tty_set_room(struct tty_struct *tty)
127 { 127 {
128 struct n_tty_data *ldata = tty->disc_data; 128 struct n_tty_data *ldata = tty->disc_data;
129 int left; 129 int left;
130 int old_left; 130 int old_left;
131 131
132 /* ldata->read_cnt is not read locked ? */ 132 /* ldata->read_cnt is not read locked ? */
133 if (I_PARMRK(tty)) { 133 if (I_PARMRK(tty)) {
134 /* Multiply read_cnt by 3, since each byte might take up to 134 /* Multiply read_cnt by 3, since each byte might take up to
135 * three times as many spaces when PARMRK is set (depending on 135 * three times as many spaces when PARMRK is set (depending on
136 * its flags, e.g. parity error). */ 136 * its flags, e.g. parity error). */
137 left = N_TTY_BUF_SIZE - ldata->read_cnt * 3 - 1; 137 left = N_TTY_BUF_SIZE - ldata->read_cnt * 3 - 1;
138 } else 138 } else
139 left = N_TTY_BUF_SIZE - ldata->read_cnt - 1; 139 left = N_TTY_BUF_SIZE - ldata->read_cnt - 1;
140 140
141 /* 141 /*
142 * If we are doing input canonicalization, and there are no 142 * If we are doing input canonicalization, and there are no
143 * pending newlines, let characters through without limit, so 143 * pending newlines, let characters through without limit, so
144 * that erase characters will be handled. Other excess 144 * that erase characters will be handled. Other excess
145 * characters will be beeped. 145 * characters will be beeped.
146 */ 146 */
147 if (left <= 0) 147 if (left <= 0)
148 left = ldata->icanon && !ldata->canon_data; 148 left = ldata->icanon && !ldata->canon_data;
149 old_left = tty->receive_room; 149 old_left = tty->receive_room;
150 tty->receive_room = left; 150 tty->receive_room = left;
151 151
152 /* Did this open up the receive buffer? We may need to flip */ 152 /* Did this open up the receive buffer? We may need to flip */
153 if (left && !old_left) { 153 if (left && !old_left) {
154 WARN_RATELIMIT(tty->port->itty == NULL, 154 WARN_RATELIMIT(tty->port->itty == NULL,
155 "scheduling with invalid itty\n"); 155 "scheduling with invalid itty\n");
156 /* see if ldisc has been killed - if so, this means that
157 * even though the ldisc has been halted and ->buf.work
158 * cancelled, ->buf.work is about to be rescheduled
159 */
160 WARN_RATELIMIT(test_bit(TTY_LDISC_HALTED, &tty->flags),
161 "scheduling buffer work for halted ldisc\n");
156 schedule_work(&tty->port->buf.work); 162 schedule_work(&tty->port->buf.work);
157 } 163 }
158 } 164 }
159 165
160 static void put_tty_queue_nolock(unsigned char c, struct n_tty_data *ldata) 166 static void put_tty_queue_nolock(unsigned char c, struct n_tty_data *ldata)
161 { 167 {
162 if (ldata->read_cnt < N_TTY_BUF_SIZE) { 168 if (ldata->read_cnt < N_TTY_BUF_SIZE) {
163 ldata->read_buf[ldata->read_head] = c; 169 ldata->read_buf[ldata->read_head] = c;
164 ldata->read_head = (ldata->read_head + 1) & (N_TTY_BUF_SIZE-1); 170 ldata->read_head = (ldata->read_head + 1) & (N_TTY_BUF_SIZE-1);
165 ldata->read_cnt++; 171 ldata->read_cnt++;
166 } 172 }
167 } 173 }
168 174
169 /** 175 /**
170 * put_tty_queue - add character to tty 176 * put_tty_queue - add character to tty
171 * @c: character 177 * @c: character
172 * @ldata: n_tty data 178 * @ldata: n_tty data
173 * 179 *
174 * Add a character to the tty read_buf queue. This is done under the 180 * Add a character to the tty read_buf queue. This is done under the
175 * read_lock to serialize character addition and also to protect us 181 * read_lock to serialize character addition and also to protect us
176 * against parallel reads or flushes 182 * against parallel reads or flushes
177 */ 183 */
178 184
179 static void put_tty_queue(unsigned char c, struct n_tty_data *ldata) 185 static void put_tty_queue(unsigned char c, struct n_tty_data *ldata)
180 { 186 {
181 unsigned long flags; 187 unsigned long flags;
182 /* 188 /*
183 * The problem of stomping on the buffers ends here. 189 * The problem of stomping on the buffers ends here.
184 * Why didn't anyone see this one coming? --AJK 190 * Why didn't anyone see this one coming? --AJK
185 */ 191 */
186 raw_spin_lock_irqsave(&ldata->read_lock, flags); 192 raw_spin_lock_irqsave(&ldata->read_lock, flags);
187 put_tty_queue_nolock(c, ldata); 193 put_tty_queue_nolock(c, ldata);
188 raw_spin_unlock_irqrestore(&ldata->read_lock, flags); 194 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
189 } 195 }
190 196
191 /** 197 /**
192 * reset_buffer_flags - reset buffer state 198 * reset_buffer_flags - reset buffer state
193 * @tty: terminal to reset 199 * @tty: terminal to reset
194 * 200 *
195 * Reset the read buffer counters, clear the flags, 201 * Reset the read buffer counters, clear the flags,
196 * and make sure the driver is unthrottled. Called 202 * and make sure the driver is unthrottled. Called
197 * from n_tty_open() and n_tty_flush_buffer(). 203 * from n_tty_open() and n_tty_flush_buffer().
198 * 204 *
199 * Locking: tty_read_lock for read fields. 205 * Locking: tty_read_lock for read fields.
200 */ 206 */
201 207
202 static void reset_buffer_flags(struct tty_struct *tty) 208 static void reset_buffer_flags(struct tty_struct *tty)
203 { 209 {
204 struct n_tty_data *ldata = tty->disc_data; 210 struct n_tty_data *ldata = tty->disc_data;
205 unsigned long flags; 211 unsigned long flags;
206 212
207 raw_spin_lock_irqsave(&ldata->read_lock, flags); 213 raw_spin_lock_irqsave(&ldata->read_lock, flags);
208 ldata->read_head = ldata->read_tail = ldata->read_cnt = 0; 214 ldata->read_head = ldata->read_tail = ldata->read_cnt = 0;
209 raw_spin_unlock_irqrestore(&ldata->read_lock, flags); 215 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
210 216
211 mutex_lock(&ldata->echo_lock); 217 mutex_lock(&ldata->echo_lock);
212 ldata->echo_pos = ldata->echo_cnt = ldata->echo_overrun = 0; 218 ldata->echo_pos = ldata->echo_cnt = ldata->echo_overrun = 0;
213 mutex_unlock(&ldata->echo_lock); 219 mutex_unlock(&ldata->echo_lock);
214 220
215 ldata->canon_head = ldata->canon_data = ldata->erasing = 0; 221 ldata->canon_head = ldata->canon_data = ldata->erasing = 0;
216 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE); 222 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
217 n_tty_set_room(tty); 223 n_tty_set_room(tty);
218 } 224 }
219 225
220 /** 226 /**
221 * n_tty_flush_buffer - clean input queue 227 * n_tty_flush_buffer - clean input queue
222 * @tty: terminal device 228 * @tty: terminal device
223 * 229 *
224 * Flush the input buffer. Called when the line discipline is 230 * Flush the input buffer. Called when the line discipline is
225 * being closed, when the tty layer wants the buffer flushed (eg 231 * being closed, when the tty layer wants the buffer flushed (eg
226 * at hangup) or when the N_TTY line discipline internally has to 232 * at hangup) or when the N_TTY line discipline internally has to
227 * clean the pending queue (for example some signals). 233 * clean the pending queue (for example some signals).
228 * 234 *
229 * Locking: ctrl_lock, read_lock. 235 * Locking: ctrl_lock, read_lock.
230 */ 236 */
231 237
232 static void n_tty_flush_buffer(struct tty_struct *tty) 238 static void n_tty_flush_buffer(struct tty_struct *tty)
233 { 239 {
234 unsigned long flags; 240 unsigned long flags;
235 /* clear everything and unthrottle the driver */ 241 /* clear everything and unthrottle the driver */
236 reset_buffer_flags(tty); 242 reset_buffer_flags(tty);
237 243
238 if (!tty->link) 244 if (!tty->link)
239 return; 245 return;
240 246
241 spin_lock_irqsave(&tty->ctrl_lock, flags); 247 spin_lock_irqsave(&tty->ctrl_lock, flags);
242 if (tty->link->packet) { 248 if (tty->link->packet) {
243 tty->ctrl_status |= TIOCPKT_FLUSHREAD; 249 tty->ctrl_status |= TIOCPKT_FLUSHREAD;
244 wake_up_interruptible(&tty->link->read_wait); 250 wake_up_interruptible(&tty->link->read_wait);
245 } 251 }
246 spin_unlock_irqrestore(&tty->ctrl_lock, flags); 252 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
247 } 253 }
248 254
249 /** 255 /**
250 * n_tty_chars_in_buffer - report available bytes 256 * n_tty_chars_in_buffer - report available bytes
251 * @tty: tty device 257 * @tty: tty device
252 * 258 *
253 * Report the number of characters buffered to be delivered to user 259 * Report the number of characters buffered to be delivered to user
254 * at this instant in time. 260 * at this instant in time.
255 * 261 *
256 * Locking: read_lock 262 * Locking: read_lock
257 */ 263 */
258 264
259 static ssize_t n_tty_chars_in_buffer(struct tty_struct *tty) 265 static ssize_t n_tty_chars_in_buffer(struct tty_struct *tty)
260 { 266 {
261 struct n_tty_data *ldata = tty->disc_data; 267 struct n_tty_data *ldata = tty->disc_data;
262 unsigned long flags; 268 unsigned long flags;
263 ssize_t n = 0; 269 ssize_t n = 0;
264 270
265 raw_spin_lock_irqsave(&ldata->read_lock, flags); 271 raw_spin_lock_irqsave(&ldata->read_lock, flags);
266 if (!ldata->icanon) { 272 if (!ldata->icanon) {
267 n = ldata->read_cnt; 273 n = ldata->read_cnt;
268 } else if (ldata->canon_data) { 274 } else if (ldata->canon_data) {
269 n = (ldata->canon_head > ldata->read_tail) ? 275 n = (ldata->canon_head > ldata->read_tail) ?
270 ldata->canon_head - ldata->read_tail : 276 ldata->canon_head - ldata->read_tail :
271 ldata->canon_head + (N_TTY_BUF_SIZE - ldata->read_tail); 277 ldata->canon_head + (N_TTY_BUF_SIZE - ldata->read_tail);
272 } 278 }
273 raw_spin_unlock_irqrestore(&ldata->read_lock, flags); 279 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
274 return n; 280 return n;
275 } 281 }
276 282
277 /** 283 /**
278 * is_utf8_continuation - utf8 multibyte check 284 * is_utf8_continuation - utf8 multibyte check
279 * @c: byte to check 285 * @c: byte to check
280 * 286 *
281 * Returns true if the utf8 character 'c' is a multibyte continuation 287 * Returns true if the utf8 character 'c' is a multibyte continuation
282 * character. We use this to correctly compute the on screen size 288 * character. We use this to correctly compute the on screen size
283 * of the character when printing 289 * of the character when printing
284 */ 290 */
285 291
286 static inline int is_utf8_continuation(unsigned char c) 292 static inline int is_utf8_continuation(unsigned char c)
287 { 293 {
288 return (c & 0xc0) == 0x80; 294 return (c & 0xc0) == 0x80;
289 } 295 }
290 296
291 /** 297 /**
292 * is_continuation - multibyte check 298 * is_continuation - multibyte check
293 * @c: byte to check 299 * @c: byte to check
294 * 300 *
295 * Returns true if the utf8 character 'c' is a multibyte continuation 301 * Returns true if the utf8 character 'c' is a multibyte continuation
296 * character and the terminal is in unicode mode. 302 * character and the terminal is in unicode mode.
297 */ 303 */
298 304
299 static inline int is_continuation(unsigned char c, struct tty_struct *tty) 305 static inline int is_continuation(unsigned char c, struct tty_struct *tty)
300 { 306 {
301 return I_IUTF8(tty) && is_utf8_continuation(c); 307 return I_IUTF8(tty) && is_utf8_continuation(c);
302 } 308 }
303 309
304 /** 310 /**
305 * do_output_char - output one character 311 * do_output_char - output one character
306 * @c: character (or partial unicode symbol) 312 * @c: character (or partial unicode symbol)
307 * @tty: terminal device 313 * @tty: terminal device
308 * @space: space available in tty driver write buffer 314 * @space: space available in tty driver write buffer
309 * 315 *
310 * This is a helper function that handles one output character 316 * This is a helper function that handles one output character
311 * (including special characters like TAB, CR, LF, etc.), 317 * (including special characters like TAB, CR, LF, etc.),
312 * doing OPOST processing and putting the results in the 318 * doing OPOST processing and putting the results in the
313 * tty driver's write buffer. 319 * tty driver's write buffer.
314 * 320 *
315 * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY 321 * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY
316 * and NLDLY. They simply aren't relevant in the world today. 322 * and NLDLY. They simply aren't relevant in the world today.
317 * If you ever need them, add them here. 323 * If you ever need them, add them here.
318 * 324 *
319 * Returns the number of bytes of buffer space used or -1 if 325 * Returns the number of bytes of buffer space used or -1 if
320 * no space left. 326 * no space left.
321 * 327 *
322 * Locking: should be called under the output_lock to protect 328 * Locking: should be called under the output_lock to protect
323 * the column state and space left in the buffer 329 * the column state and space left in the buffer
324 */ 330 */
325 331
326 static int do_output_char(unsigned char c, struct tty_struct *tty, int space) 332 static int do_output_char(unsigned char c, struct tty_struct *tty, int space)
327 { 333 {
328 struct n_tty_data *ldata = tty->disc_data; 334 struct n_tty_data *ldata = tty->disc_data;
329 int spaces; 335 int spaces;
330 336
331 if (!space) 337 if (!space)
332 return -1; 338 return -1;
333 339
334 switch (c) { 340 switch (c) {
335 case '\n': 341 case '\n':
336 if (O_ONLRET(tty)) 342 if (O_ONLRET(tty))
337 ldata->column = 0; 343 ldata->column = 0;
338 if (O_ONLCR(tty)) { 344 if (O_ONLCR(tty)) {
339 if (space < 2) 345 if (space < 2)
340 return -1; 346 return -1;
341 ldata->canon_column = ldata->column = 0; 347 ldata->canon_column = ldata->column = 0;
342 tty->ops->write(tty, "\r\n", 2); 348 tty->ops->write(tty, "\r\n", 2);
343 return 2; 349 return 2;
344 } 350 }
345 ldata->canon_column = ldata->column; 351 ldata->canon_column = ldata->column;
346 break; 352 break;
347 case '\r': 353 case '\r':
348 if (O_ONOCR(tty) && ldata->column == 0) 354 if (O_ONOCR(tty) && ldata->column == 0)
349 return 0; 355 return 0;
350 if (O_OCRNL(tty)) { 356 if (O_OCRNL(tty)) {
351 c = '\n'; 357 c = '\n';
352 if (O_ONLRET(tty)) 358 if (O_ONLRET(tty))
353 ldata->canon_column = ldata->column = 0; 359 ldata->canon_column = ldata->column = 0;
354 break; 360 break;
355 } 361 }
356 ldata->canon_column = ldata->column = 0; 362 ldata->canon_column = ldata->column = 0;
357 break; 363 break;
358 case '\t': 364 case '\t':
359 spaces = 8 - (ldata->column & 7); 365 spaces = 8 - (ldata->column & 7);
360 if (O_TABDLY(tty) == XTABS) { 366 if (O_TABDLY(tty) == XTABS) {
361 if (space < spaces) 367 if (space < spaces)
362 return -1; 368 return -1;
363 ldata->column += spaces; 369 ldata->column += spaces;
364 tty->ops->write(tty, " ", spaces); 370 tty->ops->write(tty, " ", spaces);
365 return spaces; 371 return spaces;
366 } 372 }
367 ldata->column += spaces; 373 ldata->column += spaces;
368 break; 374 break;
369 case '\b': 375 case '\b':
370 if (ldata->column > 0) 376 if (ldata->column > 0)
371 ldata->column--; 377 ldata->column--;
372 break; 378 break;
373 default: 379 default:
374 if (!iscntrl(c)) { 380 if (!iscntrl(c)) {
375 if (O_OLCUC(tty)) 381 if (O_OLCUC(tty))
376 c = toupper(c); 382 c = toupper(c);
377 if (!is_continuation(c, tty)) 383 if (!is_continuation(c, tty))
378 ldata->column++; 384 ldata->column++;
379 } 385 }
380 break; 386 break;
381 } 387 }
382 388
383 tty_put_char(tty, c); 389 tty_put_char(tty, c);
384 return 1; 390 return 1;
385 } 391 }
386 392
387 /** 393 /**
388 * process_output - output post processor 394 * process_output - output post processor
389 * @c: character (or partial unicode symbol) 395 * @c: character (or partial unicode symbol)
390 * @tty: terminal device 396 * @tty: terminal device
391 * 397 *
392 * Output one character with OPOST processing. 398 * Output one character with OPOST processing.
393 * Returns -1 when the output device is full and the character 399 * Returns -1 when the output device is full and the character
394 * must be retried. 400 * must be retried.
395 * 401 *
396 * Locking: output_lock to protect column state and space left 402 * Locking: output_lock to protect column state and space left
397 * (also, this is called from n_tty_write under the 403 * (also, this is called from n_tty_write under the
398 * tty layer write lock) 404 * tty layer write lock)
399 */ 405 */
400 406
401 static int process_output(unsigned char c, struct tty_struct *tty) 407 static int process_output(unsigned char c, struct tty_struct *tty)
402 { 408 {
403 struct n_tty_data *ldata = tty->disc_data; 409 struct n_tty_data *ldata = tty->disc_data;
404 int space, retval; 410 int space, retval;
405 411
406 mutex_lock(&ldata->output_lock); 412 mutex_lock(&ldata->output_lock);
407 413
408 space = tty_write_room(tty); 414 space = tty_write_room(tty);
409 retval = do_output_char(c, tty, space); 415 retval = do_output_char(c, tty, space);
410 416
411 mutex_unlock(&ldata->output_lock); 417 mutex_unlock(&ldata->output_lock);
412 if (retval < 0) 418 if (retval < 0)
413 return -1; 419 return -1;
414 else 420 else
415 return 0; 421 return 0;
416 } 422 }
417 423
418 /** 424 /**
419 * process_output_block - block post processor 425 * process_output_block - block post processor
420 * @tty: terminal device 426 * @tty: terminal device
421 * @buf: character buffer 427 * @buf: character buffer
422 * @nr: number of bytes to output 428 * @nr: number of bytes to output
423 * 429 *
424 * Output a block of characters with OPOST processing. 430 * Output a block of characters with OPOST processing.
425 * Returns the number of characters output. 431 * Returns the number of characters output.
426 * 432 *
427 * This path is used to speed up block console writes, among other 433 * This path is used to speed up block console writes, among other
428 * things when processing blocks of output data. It handles only 434 * things when processing blocks of output data. It handles only
429 * the simple cases normally found and helps to generate blocks of 435 * the simple cases normally found and helps to generate blocks of
430 * symbols for the console driver and thus improve performance. 436 * symbols for the console driver and thus improve performance.
431 * 437 *
432 * Locking: output_lock to protect column state and space left 438 * Locking: output_lock to protect column state and space left
433 * (also, this is called from n_tty_write under the 439 * (also, this is called from n_tty_write under the
434 * tty layer write lock) 440 * tty layer write lock)
435 */ 441 */
436 442
437 static ssize_t process_output_block(struct tty_struct *tty, 443 static ssize_t process_output_block(struct tty_struct *tty,
438 const unsigned char *buf, unsigned int nr) 444 const unsigned char *buf, unsigned int nr)
439 { 445 {
440 struct n_tty_data *ldata = tty->disc_data; 446 struct n_tty_data *ldata = tty->disc_data;
441 int space; 447 int space;
442 int i; 448 int i;
443 const unsigned char *cp; 449 const unsigned char *cp;
444 450
445 mutex_lock(&ldata->output_lock); 451 mutex_lock(&ldata->output_lock);
446 452
447 space = tty_write_room(tty); 453 space = tty_write_room(tty);
448 if (!space) { 454 if (!space) {
449 mutex_unlock(&ldata->output_lock); 455 mutex_unlock(&ldata->output_lock);
450 return 0; 456 return 0;
451 } 457 }
452 if (nr > space) 458 if (nr > space)
453 nr = space; 459 nr = space;
454 460
455 for (i = 0, cp = buf; i < nr; i++, cp++) { 461 for (i = 0, cp = buf; i < nr; i++, cp++) {
456 unsigned char c = *cp; 462 unsigned char c = *cp;
457 463
458 switch (c) { 464 switch (c) {
459 case '\n': 465 case '\n':
460 if (O_ONLRET(tty)) 466 if (O_ONLRET(tty))
461 ldata->column = 0; 467 ldata->column = 0;
462 if (O_ONLCR(tty)) 468 if (O_ONLCR(tty))
463 goto break_out; 469 goto break_out;
464 ldata->canon_column = ldata->column; 470 ldata->canon_column = ldata->column;
465 break; 471 break;
466 case '\r': 472 case '\r':
467 if (O_ONOCR(tty) && ldata->column == 0) 473 if (O_ONOCR(tty) && ldata->column == 0)
468 goto break_out; 474 goto break_out;
469 if (O_OCRNL(tty)) 475 if (O_OCRNL(tty))
470 goto break_out; 476 goto break_out;
471 ldata->canon_column = ldata->column = 0; 477 ldata->canon_column = ldata->column = 0;
472 break; 478 break;
473 case '\t': 479 case '\t':
474 goto break_out; 480 goto break_out;
475 case '\b': 481 case '\b':
476 if (ldata->column > 0) 482 if (ldata->column > 0)
477 ldata->column--; 483 ldata->column--;
478 break; 484 break;
479 default: 485 default:
480 if (!iscntrl(c)) { 486 if (!iscntrl(c)) {
481 if (O_OLCUC(tty)) 487 if (O_OLCUC(tty))
482 goto break_out; 488 goto break_out;
483 if (!is_continuation(c, tty)) 489 if (!is_continuation(c, tty))
484 ldata->column++; 490 ldata->column++;
485 } 491 }
486 break; 492 break;
487 } 493 }
488 } 494 }
489 break_out: 495 break_out:
490 i = tty->ops->write(tty, buf, i); 496 i = tty->ops->write(tty, buf, i);
491 497
492 mutex_unlock(&ldata->output_lock); 498 mutex_unlock(&ldata->output_lock);
493 return i; 499 return i;
494 } 500 }
495 501
496 /** 502 /**
497 * process_echoes - write pending echo characters 503 * process_echoes - write pending echo characters
498 * @tty: terminal device 504 * @tty: terminal device
499 * 505 *
500 * Write previously buffered echo (and other ldisc-generated) 506 * Write previously buffered echo (and other ldisc-generated)
501 * characters to the tty. 507 * characters to the tty.
502 * 508 *
503 * Characters generated by the ldisc (including echoes) need to 509 * Characters generated by the ldisc (including echoes) need to
504 * be buffered because the driver's write buffer can fill during 510 * be buffered because the driver's write buffer can fill during
505 * heavy program output. Echoing straight to the driver will 511 * heavy program output. Echoing straight to the driver will
506 * often fail under these conditions, causing lost characters and 512 * often fail under these conditions, causing lost characters and
507 * resulting mismatches of ldisc state information. 513 * resulting mismatches of ldisc state information.
508 * 514 *
509 * Since the ldisc state must represent the characters actually sent 515 * Since the ldisc state must represent the characters actually sent
510 * to the driver at the time of the write, operations like certain 516 * to the driver at the time of the write, operations like certain
511 * changes in column state are also saved in the buffer and executed 517 * changes in column state are also saved in the buffer and executed
512 * here. 518 * here.
513 * 519 *
514 * A circular fifo buffer is used so that the most recent characters 520 * A circular fifo buffer is used so that the most recent characters
515 * are prioritized. Also, when control characters are echoed with a 521 * are prioritized. Also, when control characters are echoed with a
516 * prefixed "^", the pair is treated atomically and thus not separated. 522 * prefixed "^", the pair is treated atomically and thus not separated.
517 * 523 *
518 * Locking: output_lock to protect column state and space left, 524 * Locking: output_lock to protect column state and space left,
519 * echo_lock to protect the echo buffer 525 * echo_lock to protect the echo buffer
520 */ 526 */
521 527
522 static void process_echoes(struct tty_struct *tty) 528 static void process_echoes(struct tty_struct *tty)
523 { 529 {
524 struct n_tty_data *ldata = tty->disc_data; 530 struct n_tty_data *ldata = tty->disc_data;
525 int space, nr; 531 int space, nr;
526 unsigned char c; 532 unsigned char c;
527 unsigned char *cp, *buf_end; 533 unsigned char *cp, *buf_end;
528 534
529 if (!ldata->echo_cnt) 535 if (!ldata->echo_cnt)
530 return; 536 return;
531 537
532 mutex_lock(&ldata->output_lock); 538 mutex_lock(&ldata->output_lock);
533 mutex_lock(&ldata->echo_lock); 539 mutex_lock(&ldata->echo_lock);
534 540
535 space = tty_write_room(tty); 541 space = tty_write_room(tty);
536 542
537 buf_end = ldata->echo_buf + N_TTY_BUF_SIZE; 543 buf_end = ldata->echo_buf + N_TTY_BUF_SIZE;
538 cp = ldata->echo_buf + ldata->echo_pos; 544 cp = ldata->echo_buf + ldata->echo_pos;
539 nr = ldata->echo_cnt; 545 nr = ldata->echo_cnt;
540 while (nr > 0) { 546 while (nr > 0) {
541 c = *cp; 547 c = *cp;
542 if (c == ECHO_OP_START) { 548 if (c == ECHO_OP_START) {
543 unsigned char op; 549 unsigned char op;
544 unsigned char *opp; 550 unsigned char *opp;
545 int no_space_left = 0; 551 int no_space_left = 0;
546 552
547 /* 553 /*
548 * If the buffer byte is the start of a multi-byte 554 * If the buffer byte is the start of a multi-byte
549 * operation, get the next byte, which is either the 555 * operation, get the next byte, which is either the
550 * op code or a control character value. 556 * op code or a control character value.
551 */ 557 */
552 opp = cp + 1; 558 opp = cp + 1;
553 if (opp == buf_end) 559 if (opp == buf_end)
554 opp -= N_TTY_BUF_SIZE; 560 opp -= N_TTY_BUF_SIZE;
555 op = *opp; 561 op = *opp;
556 562
557 switch (op) { 563 switch (op) {
558 unsigned int num_chars, num_bs; 564 unsigned int num_chars, num_bs;
559 565
560 case ECHO_OP_ERASE_TAB: 566 case ECHO_OP_ERASE_TAB:
561 if (++opp == buf_end) 567 if (++opp == buf_end)
562 opp -= N_TTY_BUF_SIZE; 568 opp -= N_TTY_BUF_SIZE;
563 num_chars = *opp; 569 num_chars = *opp;
564 570
565 /* 571 /*
566 * Determine how many columns to go back 572 * Determine how many columns to go back
567 * in order to erase the tab. 573 * in order to erase the tab.
568 * This depends on the number of columns 574 * This depends on the number of columns
569 * used by other characters within the tab 575 * used by other characters within the tab
570 * area. If this (modulo 8) count is from 576 * area. If this (modulo 8) count is from
571 * the start of input rather than from a 577 * the start of input rather than from a
572 * previous tab, we offset by canon column. 578 * previous tab, we offset by canon column.
573 * Otherwise, tab spacing is normal. 579 * Otherwise, tab spacing is normal.
574 */ 580 */
575 if (!(num_chars & 0x80)) 581 if (!(num_chars & 0x80))
576 num_chars += ldata->canon_column; 582 num_chars += ldata->canon_column;
577 num_bs = 8 - (num_chars & 7); 583 num_bs = 8 - (num_chars & 7);
578 584
579 if (num_bs > space) { 585 if (num_bs > space) {
580 no_space_left = 1; 586 no_space_left = 1;
581 break; 587 break;
582 } 588 }
583 space -= num_bs; 589 space -= num_bs;
584 while (num_bs--) { 590 while (num_bs--) {
585 tty_put_char(tty, '\b'); 591 tty_put_char(tty, '\b');
586 if (ldata->column > 0) 592 if (ldata->column > 0)
587 ldata->column--; 593 ldata->column--;
588 } 594 }
589 cp += 3; 595 cp += 3;
590 nr -= 3; 596 nr -= 3;
591 break; 597 break;
592 598
593 case ECHO_OP_SET_CANON_COL: 599 case ECHO_OP_SET_CANON_COL:
594 ldata->canon_column = ldata->column; 600 ldata->canon_column = ldata->column;
595 cp += 2; 601 cp += 2;
596 nr -= 2; 602 nr -= 2;
597 break; 603 break;
598 604
599 case ECHO_OP_MOVE_BACK_COL: 605 case ECHO_OP_MOVE_BACK_COL:
600 if (ldata->column > 0) 606 if (ldata->column > 0)
601 ldata->column--; 607 ldata->column--;
602 cp += 2; 608 cp += 2;
603 nr -= 2; 609 nr -= 2;
604 break; 610 break;
605 611
606 case ECHO_OP_START: 612 case ECHO_OP_START:
607 /* This is an escaped echo op start code */ 613 /* This is an escaped echo op start code */
608 if (!space) { 614 if (!space) {
609 no_space_left = 1; 615 no_space_left = 1;
610 break; 616 break;
611 } 617 }
612 tty_put_char(tty, ECHO_OP_START); 618 tty_put_char(tty, ECHO_OP_START);
613 ldata->column++; 619 ldata->column++;
614 space--; 620 space--;
615 cp += 2; 621 cp += 2;
616 nr -= 2; 622 nr -= 2;
617 break; 623 break;
618 624
619 default: 625 default:
620 /* 626 /*
621 * If the op is not a special byte code, 627 * If the op is not a special byte code,
622 * it is a ctrl char tagged to be echoed 628 * it is a ctrl char tagged to be echoed
623 * as "^X" (where X is the letter 629 * as "^X" (where X is the letter
624 * representing the control char). 630 * representing the control char).
625 * Note that we must ensure there is 631 * Note that we must ensure there is
626 * enough space for the whole ctrl pair. 632 * enough space for the whole ctrl pair.
627 * 633 *
628 */ 634 */
629 if (space < 2) { 635 if (space < 2) {
630 no_space_left = 1; 636 no_space_left = 1;
631 break; 637 break;
632 } 638 }
633 tty_put_char(tty, '^'); 639 tty_put_char(tty, '^');
634 tty_put_char(tty, op ^ 0100); 640 tty_put_char(tty, op ^ 0100);
635 ldata->column += 2; 641 ldata->column += 2;
636 space -= 2; 642 space -= 2;
637 cp += 2; 643 cp += 2;
638 nr -= 2; 644 nr -= 2;
639 } 645 }
640 646
641 if (no_space_left) 647 if (no_space_left)
642 break; 648 break;
643 } else { 649 } else {
644 if (O_OPOST(tty) && 650 if (O_OPOST(tty) &&
645 !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) { 651 !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
646 int retval = do_output_char(c, tty, space); 652 int retval = do_output_char(c, tty, space);
647 if (retval < 0) 653 if (retval < 0)
648 break; 654 break;
649 space -= retval; 655 space -= retval;
650 } else { 656 } else {
651 if (!space) 657 if (!space)
652 break; 658 break;
653 tty_put_char(tty, c); 659 tty_put_char(tty, c);
654 space -= 1; 660 space -= 1;
655 } 661 }
656 cp += 1; 662 cp += 1;
657 nr -= 1; 663 nr -= 1;
658 } 664 }
659 665
660 /* When end of circular buffer reached, wrap around */ 666 /* When end of circular buffer reached, wrap around */
661 if (cp >= buf_end) 667 if (cp >= buf_end)
662 cp -= N_TTY_BUF_SIZE; 668 cp -= N_TTY_BUF_SIZE;
663 } 669 }
664 670
665 if (nr == 0) { 671 if (nr == 0) {
666 ldata->echo_pos = 0; 672 ldata->echo_pos = 0;
667 ldata->echo_cnt = 0; 673 ldata->echo_cnt = 0;
668 ldata->echo_overrun = 0; 674 ldata->echo_overrun = 0;
669 } else { 675 } else {
670 int num_processed = ldata->echo_cnt - nr; 676 int num_processed = ldata->echo_cnt - nr;
671 ldata->echo_pos += num_processed; 677 ldata->echo_pos += num_processed;
672 ldata->echo_pos &= N_TTY_BUF_SIZE - 1; 678 ldata->echo_pos &= N_TTY_BUF_SIZE - 1;
673 ldata->echo_cnt = nr; 679 ldata->echo_cnt = nr;
674 if (num_processed > 0) 680 if (num_processed > 0)
675 ldata->echo_overrun = 0; 681 ldata->echo_overrun = 0;
676 } 682 }
677 683
678 mutex_unlock(&ldata->echo_lock); 684 mutex_unlock(&ldata->echo_lock);
679 mutex_unlock(&ldata->output_lock); 685 mutex_unlock(&ldata->output_lock);
680 686
681 if (tty->ops->flush_chars) 687 if (tty->ops->flush_chars)
682 tty->ops->flush_chars(tty); 688 tty->ops->flush_chars(tty);
683 } 689 }
684 690
685 /** 691 /**
686 * add_echo_byte - add a byte to the echo buffer 692 * add_echo_byte - add a byte to the echo buffer
687 * @c: unicode byte to echo 693 * @c: unicode byte to echo
688 * @ldata: n_tty data 694 * @ldata: n_tty data
689 * 695 *
690 * Add a character or operation byte to the echo buffer. 696 * Add a character or operation byte to the echo buffer.
691 * 697 *
692 * Should be called under the echo lock to protect the echo buffer. 698 * Should be called under the echo lock to protect the echo buffer.
693 */ 699 */
694 700
695 static void add_echo_byte(unsigned char c, struct n_tty_data *ldata) 701 static void add_echo_byte(unsigned char c, struct n_tty_data *ldata)
696 { 702 {
697 int new_byte_pos; 703 int new_byte_pos;
698 704
699 if (ldata->echo_cnt == N_TTY_BUF_SIZE) { 705 if (ldata->echo_cnt == N_TTY_BUF_SIZE) {
700 /* Circular buffer is already at capacity */ 706 /* Circular buffer is already at capacity */
701 new_byte_pos = ldata->echo_pos; 707 new_byte_pos = ldata->echo_pos;
702 708
703 /* 709 /*
704 * Since the buffer start position needs to be advanced, 710 * Since the buffer start position needs to be advanced,
705 * be sure to step by a whole operation byte group. 711 * be sure to step by a whole operation byte group.
706 */ 712 */
707 if (ldata->echo_buf[ldata->echo_pos] == ECHO_OP_START) { 713 if (ldata->echo_buf[ldata->echo_pos] == ECHO_OP_START) {
708 if (ldata->echo_buf[(ldata->echo_pos + 1) & 714 if (ldata->echo_buf[(ldata->echo_pos + 1) &
709 (N_TTY_BUF_SIZE - 1)] == 715 (N_TTY_BUF_SIZE - 1)] ==
710 ECHO_OP_ERASE_TAB) { 716 ECHO_OP_ERASE_TAB) {
711 ldata->echo_pos += 3; 717 ldata->echo_pos += 3;
712 ldata->echo_cnt -= 2; 718 ldata->echo_cnt -= 2;
713 } else { 719 } else {
714 ldata->echo_pos += 2; 720 ldata->echo_pos += 2;
715 ldata->echo_cnt -= 1; 721 ldata->echo_cnt -= 1;
716 } 722 }
717 } else { 723 } else {
718 ldata->echo_pos++; 724 ldata->echo_pos++;
719 } 725 }
720 ldata->echo_pos &= N_TTY_BUF_SIZE - 1; 726 ldata->echo_pos &= N_TTY_BUF_SIZE - 1;
721 727
722 ldata->echo_overrun = 1; 728 ldata->echo_overrun = 1;
723 } else { 729 } else {
724 new_byte_pos = ldata->echo_pos + ldata->echo_cnt; 730 new_byte_pos = ldata->echo_pos + ldata->echo_cnt;
725 new_byte_pos &= N_TTY_BUF_SIZE - 1; 731 new_byte_pos &= N_TTY_BUF_SIZE - 1;
726 ldata->echo_cnt++; 732 ldata->echo_cnt++;
727 } 733 }
728 734
729 ldata->echo_buf[new_byte_pos] = c; 735 ldata->echo_buf[new_byte_pos] = c;
730 } 736 }
731 737
732 /** 738 /**
733 * echo_move_back_col - add operation to move back a column 739 * echo_move_back_col - add operation to move back a column
734 * @ldata: n_tty data 740 * @ldata: n_tty data
735 * 741 *
736 * Add an operation to the echo buffer to move back one column. 742 * Add an operation to the echo buffer to move back one column.
737 * 743 *
738 * Locking: echo_lock to protect the echo buffer 744 * Locking: echo_lock to protect the echo buffer
739 */ 745 */
740 746
741 static void echo_move_back_col(struct n_tty_data *ldata) 747 static void echo_move_back_col(struct n_tty_data *ldata)
742 { 748 {
743 mutex_lock(&ldata->echo_lock); 749 mutex_lock(&ldata->echo_lock);
744 add_echo_byte(ECHO_OP_START, ldata); 750 add_echo_byte(ECHO_OP_START, ldata);
745 add_echo_byte(ECHO_OP_MOVE_BACK_COL, ldata); 751 add_echo_byte(ECHO_OP_MOVE_BACK_COL, ldata);
746 mutex_unlock(&ldata->echo_lock); 752 mutex_unlock(&ldata->echo_lock);
747 } 753 }
748 754
749 /** 755 /**
750 * echo_set_canon_col - add operation to set the canon column 756 * echo_set_canon_col - add operation to set the canon column
751 * @ldata: n_tty data 757 * @ldata: n_tty data
752 * 758 *
753 * Add an operation to the echo buffer to set the canon column 759 * Add an operation to the echo buffer to set the canon column
754 * to the current column. 760 * to the current column.
755 * 761 *
756 * Locking: echo_lock to protect the echo buffer 762 * Locking: echo_lock to protect the echo buffer
757 */ 763 */
758 764
759 static void echo_set_canon_col(struct n_tty_data *ldata) 765 static void echo_set_canon_col(struct n_tty_data *ldata)
760 { 766 {
761 mutex_lock(&ldata->echo_lock); 767 mutex_lock(&ldata->echo_lock);
762 add_echo_byte(ECHO_OP_START, ldata); 768 add_echo_byte(ECHO_OP_START, ldata);
763 add_echo_byte(ECHO_OP_SET_CANON_COL, ldata); 769 add_echo_byte(ECHO_OP_SET_CANON_COL, ldata);
764 mutex_unlock(&ldata->echo_lock); 770 mutex_unlock(&ldata->echo_lock);
765 } 771 }
766 772
767 /** 773 /**
768 * echo_erase_tab - add operation to erase a tab 774 * echo_erase_tab - add operation to erase a tab
769 * @num_chars: number of character columns already used 775 * @num_chars: number of character columns already used
770 * @after_tab: true if num_chars starts after a previous tab 776 * @after_tab: true if num_chars starts after a previous tab
771 * @ldata: n_tty data 777 * @ldata: n_tty data
772 * 778 *
773 * Add an operation to the echo buffer to erase a tab. 779 * Add an operation to the echo buffer to erase a tab.
774 * 780 *
775 * Called by the eraser function, which knows how many character 781 * Called by the eraser function, which knows how many character
776 * columns have been used since either a previous tab or the start 782 * columns have been used since either a previous tab or the start
777 * of input. This information will be used later, along with 783 * of input. This information will be used later, along with
778 * canon column (if applicable), to go back the correct number 784 * canon column (if applicable), to go back the correct number
779 * of columns. 785 * of columns.
780 * 786 *
781 * Locking: echo_lock to protect the echo buffer 787 * Locking: echo_lock to protect the echo buffer
782 */ 788 */
783 789
784 static void echo_erase_tab(unsigned int num_chars, int after_tab, 790 static void echo_erase_tab(unsigned int num_chars, int after_tab,
785 struct n_tty_data *ldata) 791 struct n_tty_data *ldata)
786 { 792 {
787 mutex_lock(&ldata->echo_lock); 793 mutex_lock(&ldata->echo_lock);
788 794
789 add_echo_byte(ECHO_OP_START, ldata); 795 add_echo_byte(ECHO_OP_START, ldata);
790 add_echo_byte(ECHO_OP_ERASE_TAB, ldata); 796 add_echo_byte(ECHO_OP_ERASE_TAB, ldata);
791 797
792 /* We only need to know this modulo 8 (tab spacing) */ 798 /* We only need to know this modulo 8 (tab spacing) */
793 num_chars &= 7; 799 num_chars &= 7;
794 800
795 /* Set the high bit as a flag if num_chars is after a previous tab */ 801 /* Set the high bit as a flag if num_chars is after a previous tab */
796 if (after_tab) 802 if (after_tab)
797 num_chars |= 0x80; 803 num_chars |= 0x80;
798 804
799 add_echo_byte(num_chars, ldata); 805 add_echo_byte(num_chars, ldata);
800 806
801 mutex_unlock(&ldata->echo_lock); 807 mutex_unlock(&ldata->echo_lock);
802 } 808 }
803 809
804 /** 810 /**
805 * echo_char_raw - echo a character raw 811 * echo_char_raw - echo a character raw
806 * @c: unicode byte to echo 812 * @c: unicode byte to echo
807 * @tty: terminal device 813 * @tty: terminal device
808 * 814 *
809 * Echo user input back onto the screen. This must be called only when 815 * Echo user input back onto the screen. This must be called only when
810 * L_ECHO(tty) is true. Called from the driver receive_buf path. 816 * L_ECHO(tty) is true. Called from the driver receive_buf path.
811 * 817 *
812 * This variant does not treat control characters specially. 818 * This variant does not treat control characters specially.
813 * 819 *
814 * Locking: echo_lock to protect the echo buffer 820 * Locking: echo_lock to protect the echo buffer
815 */ 821 */
816 822
817 static void echo_char_raw(unsigned char c, struct n_tty_data *ldata) 823 static void echo_char_raw(unsigned char c, struct n_tty_data *ldata)
818 { 824 {
819 mutex_lock(&ldata->echo_lock); 825 mutex_lock(&ldata->echo_lock);
820 if (c == ECHO_OP_START) { 826 if (c == ECHO_OP_START) {
821 add_echo_byte(ECHO_OP_START, ldata); 827 add_echo_byte(ECHO_OP_START, ldata);
822 add_echo_byte(ECHO_OP_START, ldata); 828 add_echo_byte(ECHO_OP_START, ldata);
823 } else { 829 } else {
824 add_echo_byte(c, ldata); 830 add_echo_byte(c, ldata);
825 } 831 }
826 mutex_unlock(&ldata->echo_lock); 832 mutex_unlock(&ldata->echo_lock);
827 } 833 }
828 834
829 /** 835 /**
830 * echo_char - echo a character 836 * echo_char - echo a character
831 * @c: unicode byte to echo 837 * @c: unicode byte to echo
832 * @tty: terminal device 838 * @tty: terminal device
833 * 839 *
834 * Echo user input back onto the screen. This must be called only when 840 * Echo user input back onto the screen. This must be called only when
835 * L_ECHO(tty) is true. Called from the driver receive_buf path. 841 * L_ECHO(tty) is true. Called from the driver receive_buf path.
836 * 842 *
837 * This variant tags control characters to be echoed as "^X" 843 * This variant tags control characters to be echoed as "^X"
838 * (where X is the letter representing the control char). 844 * (where X is the letter representing the control char).
839 * 845 *
840 * Locking: echo_lock to protect the echo buffer 846 * Locking: echo_lock to protect the echo buffer
841 */ 847 */
842 848
843 static void echo_char(unsigned char c, struct tty_struct *tty) 849 static void echo_char(unsigned char c, struct tty_struct *tty)
844 { 850 {
845 struct n_tty_data *ldata = tty->disc_data; 851 struct n_tty_data *ldata = tty->disc_data;
846 852
847 mutex_lock(&ldata->echo_lock); 853 mutex_lock(&ldata->echo_lock);
848 854
849 if (c == ECHO_OP_START) { 855 if (c == ECHO_OP_START) {
850 add_echo_byte(ECHO_OP_START, ldata); 856 add_echo_byte(ECHO_OP_START, ldata);
851 add_echo_byte(ECHO_OP_START, ldata); 857 add_echo_byte(ECHO_OP_START, ldata);
852 } else { 858 } else {
853 if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t') 859 if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t')
854 add_echo_byte(ECHO_OP_START, ldata); 860 add_echo_byte(ECHO_OP_START, ldata);
855 add_echo_byte(c, ldata); 861 add_echo_byte(c, ldata);
856 } 862 }
857 863
858 mutex_unlock(&ldata->echo_lock); 864 mutex_unlock(&ldata->echo_lock);
859 } 865 }
860 866
861 /** 867 /**
862 * finish_erasing - complete erase 868 * finish_erasing - complete erase
863 * @ldata: n_tty data 869 * @ldata: n_tty data
864 */ 870 */
865 871
866 static inline void finish_erasing(struct n_tty_data *ldata) 872 static inline void finish_erasing(struct n_tty_data *ldata)
867 { 873 {
868 if (ldata->erasing) { 874 if (ldata->erasing) {
869 echo_char_raw('/', ldata); 875 echo_char_raw('/', ldata);
870 ldata->erasing = 0; 876 ldata->erasing = 0;
871 } 877 }
872 } 878 }
873 879
874 /** 880 /**
875 * eraser - handle erase function 881 * eraser - handle erase function
876 * @c: character input 882 * @c: character input
877 * @tty: terminal device 883 * @tty: terminal device
878 * 884 *
879 * Perform erase and necessary output when an erase character is 885 * Perform erase and necessary output when an erase character is
880 * present in the stream from the driver layer. Handles the complexities 886 * present in the stream from the driver layer. Handles the complexities
881 * of UTF-8 multibyte symbols. 887 * of UTF-8 multibyte symbols.
882 * 888 *
883 * Locking: read_lock for tty buffers 889 * Locking: read_lock for tty buffers
884 */ 890 */
885 891
886 static void eraser(unsigned char c, struct tty_struct *tty) 892 static void eraser(unsigned char c, struct tty_struct *tty)
887 { 893 {
888 struct n_tty_data *ldata = tty->disc_data; 894 struct n_tty_data *ldata = tty->disc_data;
889 enum { ERASE, WERASE, KILL } kill_type; 895 enum { ERASE, WERASE, KILL } kill_type;
890 int head, seen_alnums, cnt; 896 int head, seen_alnums, cnt;
891 unsigned long flags; 897 unsigned long flags;
892 898
893 /* FIXME: locking needed ? */ 899 /* FIXME: locking needed ? */
894 if (ldata->read_head == ldata->canon_head) { 900 if (ldata->read_head == ldata->canon_head) {
895 /* process_output('\a', tty); */ /* what do you think? */ 901 /* process_output('\a', tty); */ /* what do you think? */
896 return; 902 return;
897 } 903 }
898 if (c == ERASE_CHAR(tty)) 904 if (c == ERASE_CHAR(tty))
899 kill_type = ERASE; 905 kill_type = ERASE;
900 else if (c == WERASE_CHAR(tty)) 906 else if (c == WERASE_CHAR(tty))
901 kill_type = WERASE; 907 kill_type = WERASE;
902 else { 908 else {
903 if (!L_ECHO(tty)) { 909 if (!L_ECHO(tty)) {
904 raw_spin_lock_irqsave(&ldata->read_lock, flags); 910 raw_spin_lock_irqsave(&ldata->read_lock, flags);
905 ldata->read_cnt -= ((ldata->read_head - ldata->canon_head) & 911 ldata->read_cnt -= ((ldata->read_head - ldata->canon_head) &
906 (N_TTY_BUF_SIZE - 1)); 912 (N_TTY_BUF_SIZE - 1));
907 ldata->read_head = ldata->canon_head; 913 ldata->read_head = ldata->canon_head;
908 raw_spin_unlock_irqrestore(&ldata->read_lock, flags); 914 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
909 return; 915 return;
910 } 916 }
911 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) { 917 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
912 raw_spin_lock_irqsave(&ldata->read_lock, flags); 918 raw_spin_lock_irqsave(&ldata->read_lock, flags);
913 ldata->read_cnt -= ((ldata->read_head - ldata->canon_head) & 919 ldata->read_cnt -= ((ldata->read_head - ldata->canon_head) &
914 (N_TTY_BUF_SIZE - 1)); 920 (N_TTY_BUF_SIZE - 1));
915 ldata->read_head = ldata->canon_head; 921 ldata->read_head = ldata->canon_head;
916 raw_spin_unlock_irqrestore(&ldata->read_lock, flags); 922 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
917 finish_erasing(ldata); 923 finish_erasing(ldata);
918 echo_char(KILL_CHAR(tty), tty); 924 echo_char(KILL_CHAR(tty), tty);
919 /* Add a newline if ECHOK is on and ECHOKE is off. */ 925 /* Add a newline if ECHOK is on and ECHOKE is off. */
920 if (L_ECHOK(tty)) 926 if (L_ECHOK(tty))
921 echo_char_raw('\n', ldata); 927 echo_char_raw('\n', ldata);
922 return; 928 return;
923 } 929 }
924 kill_type = KILL; 930 kill_type = KILL;
925 } 931 }
926 932
927 seen_alnums = 0; 933 seen_alnums = 0;
928 /* FIXME: Locking ?? */ 934 /* FIXME: Locking ?? */
929 while (ldata->read_head != ldata->canon_head) { 935 while (ldata->read_head != ldata->canon_head) {
930 head = ldata->read_head; 936 head = ldata->read_head;
931 937
932 /* erase a single possibly multibyte character */ 938 /* erase a single possibly multibyte character */
933 do { 939 do {
934 head = (head - 1) & (N_TTY_BUF_SIZE-1); 940 head = (head - 1) & (N_TTY_BUF_SIZE-1);
935 c = ldata->read_buf[head]; 941 c = ldata->read_buf[head];
936 } while (is_continuation(c, tty) && head != ldata->canon_head); 942 } while (is_continuation(c, tty) && head != ldata->canon_head);
937 943
938 /* do not partially erase */ 944 /* do not partially erase */
939 if (is_continuation(c, tty)) 945 if (is_continuation(c, tty))
940 break; 946 break;
941 947
942 if (kill_type == WERASE) { 948 if (kill_type == WERASE) {
943 /* Equivalent to BSD's ALTWERASE. */ 949 /* Equivalent to BSD's ALTWERASE. */
944 if (isalnum(c) || c == '_') 950 if (isalnum(c) || c == '_')
945 seen_alnums++; 951 seen_alnums++;
946 else if (seen_alnums) 952 else if (seen_alnums)
947 break; 953 break;
948 } 954 }
949 cnt = (ldata->read_head - head) & (N_TTY_BUF_SIZE-1); 955 cnt = (ldata->read_head - head) & (N_TTY_BUF_SIZE-1);
950 raw_spin_lock_irqsave(&ldata->read_lock, flags); 956 raw_spin_lock_irqsave(&ldata->read_lock, flags);
951 ldata->read_head = head; 957 ldata->read_head = head;
952 ldata->read_cnt -= cnt; 958 ldata->read_cnt -= cnt;
953 raw_spin_unlock_irqrestore(&ldata->read_lock, flags); 959 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
954 if (L_ECHO(tty)) { 960 if (L_ECHO(tty)) {
955 if (L_ECHOPRT(tty)) { 961 if (L_ECHOPRT(tty)) {
956 if (!ldata->erasing) { 962 if (!ldata->erasing) {
957 echo_char_raw('\\', ldata); 963 echo_char_raw('\\', ldata);
958 ldata->erasing = 1; 964 ldata->erasing = 1;
959 } 965 }
960 /* if cnt > 1, output a multi-byte character */ 966 /* if cnt > 1, output a multi-byte character */
961 echo_char(c, tty); 967 echo_char(c, tty);
962 while (--cnt > 0) { 968 while (--cnt > 0) {
963 head = (head+1) & (N_TTY_BUF_SIZE-1); 969 head = (head+1) & (N_TTY_BUF_SIZE-1);
964 echo_char_raw(ldata->read_buf[head], 970 echo_char_raw(ldata->read_buf[head],
965 ldata); 971 ldata);
966 echo_move_back_col(ldata); 972 echo_move_back_col(ldata);
967 } 973 }
968 } else if (kill_type == ERASE && !L_ECHOE(tty)) { 974 } else if (kill_type == ERASE && !L_ECHOE(tty)) {
969 echo_char(ERASE_CHAR(tty), tty); 975 echo_char(ERASE_CHAR(tty), tty);
970 } else if (c == '\t') { 976 } else if (c == '\t') {
971 unsigned int num_chars = 0; 977 unsigned int num_chars = 0;
972 int after_tab = 0; 978 int after_tab = 0;
973 unsigned long tail = ldata->read_head; 979 unsigned long tail = ldata->read_head;
974 980
975 /* 981 /*
976 * Count the columns used for characters 982 * Count the columns used for characters
977 * since the start of input or after a 983 * since the start of input or after a
978 * previous tab. 984 * previous tab.
979 * This info is used to go back the correct 985 * This info is used to go back the correct
980 * number of columns. 986 * number of columns.
981 */ 987 */
982 while (tail != ldata->canon_head) { 988 while (tail != ldata->canon_head) {
983 tail = (tail-1) & (N_TTY_BUF_SIZE-1); 989 tail = (tail-1) & (N_TTY_BUF_SIZE-1);
984 c = ldata->read_buf[tail]; 990 c = ldata->read_buf[tail];
985 if (c == '\t') { 991 if (c == '\t') {
986 after_tab = 1; 992 after_tab = 1;
987 break; 993 break;
988 } else if (iscntrl(c)) { 994 } else if (iscntrl(c)) {
989 if (L_ECHOCTL(tty)) 995 if (L_ECHOCTL(tty))
990 num_chars += 2; 996 num_chars += 2;
991 } else if (!is_continuation(c, tty)) { 997 } else if (!is_continuation(c, tty)) {
992 num_chars++; 998 num_chars++;
993 } 999 }
994 } 1000 }
995 echo_erase_tab(num_chars, after_tab, ldata); 1001 echo_erase_tab(num_chars, after_tab, ldata);
996 } else { 1002 } else {
997 if (iscntrl(c) && L_ECHOCTL(tty)) { 1003 if (iscntrl(c) && L_ECHOCTL(tty)) {
998 echo_char_raw('\b', ldata); 1004 echo_char_raw('\b', ldata);
999 echo_char_raw(' ', ldata); 1005 echo_char_raw(' ', ldata);
1000 echo_char_raw('\b', ldata); 1006 echo_char_raw('\b', ldata);
1001 } 1007 }
1002 if (!iscntrl(c) || L_ECHOCTL(tty)) { 1008 if (!iscntrl(c) || L_ECHOCTL(tty)) {
1003 echo_char_raw('\b', ldata); 1009 echo_char_raw('\b', ldata);
1004 echo_char_raw(' ', ldata); 1010 echo_char_raw(' ', ldata);
1005 echo_char_raw('\b', ldata); 1011 echo_char_raw('\b', ldata);
1006 } 1012 }
1007 } 1013 }
1008 } 1014 }
1009 if (kill_type == ERASE) 1015 if (kill_type == ERASE)
1010 break; 1016 break;
1011 } 1017 }
1012 if (ldata->read_head == ldata->canon_head && L_ECHO(tty)) 1018 if (ldata->read_head == ldata->canon_head && L_ECHO(tty))
1013 finish_erasing(ldata); 1019 finish_erasing(ldata);
1014 } 1020 }
1015 1021
1016 /** 1022 /**
1017 * isig - handle the ISIG optio 1023 * isig - handle the ISIG optio
1018 * @sig: signal 1024 * @sig: signal
1019 * @tty: terminal 1025 * @tty: terminal
1020 * 1026 *
1021 * Called when a signal is being sent due to terminal input. 1027 * Called when a signal is being sent due to terminal input.
1022 * Called from the driver receive_buf path so serialized. 1028 * Called from the driver receive_buf path so serialized.
1023 * 1029 *
1024 * Locking: ctrl_lock 1030 * Locking: ctrl_lock
1025 */ 1031 */
1026 1032
1027 static inline void isig(int sig, struct tty_struct *tty) 1033 static inline void isig(int sig, struct tty_struct *tty)
1028 { 1034 {
1029 struct pid *tty_pgrp = tty_get_pgrp(tty); 1035 struct pid *tty_pgrp = tty_get_pgrp(tty);
1030 if (tty_pgrp) { 1036 if (tty_pgrp) {
1031 kill_pgrp(tty_pgrp, sig, 1); 1037 kill_pgrp(tty_pgrp, sig, 1);
1032 put_pid(tty_pgrp); 1038 put_pid(tty_pgrp);
1033 } 1039 }
1034 } 1040 }
1035 1041
1036 /** 1042 /**
1037 * n_tty_receive_break - handle break 1043 * n_tty_receive_break - handle break
1038 * @tty: terminal 1044 * @tty: terminal
1039 * 1045 *
1040 * An RS232 break event has been hit in the incoming bitstream. This 1046 * An RS232 break event has been hit in the incoming bitstream. This
1041 * can cause a variety of events depending upon the termios settings. 1047 * can cause a variety of events depending upon the termios settings.
1042 * 1048 *
1043 * Called from the receive_buf path so single threaded. 1049 * Called from the receive_buf path so single threaded.
1044 */ 1050 */
1045 1051
1046 static inline void n_tty_receive_break(struct tty_struct *tty) 1052 static inline void n_tty_receive_break(struct tty_struct *tty)
1047 { 1053 {
1048 struct n_tty_data *ldata = tty->disc_data; 1054 struct n_tty_data *ldata = tty->disc_data;
1049 1055
1050 if (I_IGNBRK(tty)) 1056 if (I_IGNBRK(tty))
1051 return; 1057 return;
1052 if (I_BRKINT(tty)) { 1058 if (I_BRKINT(tty)) {
1053 isig(SIGINT, tty); 1059 isig(SIGINT, tty);
1054 if (!L_NOFLSH(tty)) { 1060 if (!L_NOFLSH(tty)) {
1055 n_tty_flush_buffer(tty); 1061 n_tty_flush_buffer(tty);
1056 tty_driver_flush_buffer(tty); 1062 tty_driver_flush_buffer(tty);
1057 } 1063 }
1058 return; 1064 return;
1059 } 1065 }
1060 if (I_PARMRK(tty)) { 1066 if (I_PARMRK(tty)) {
1061 put_tty_queue('\377', ldata); 1067 put_tty_queue('\377', ldata);
1062 put_tty_queue('\0', ldata); 1068 put_tty_queue('\0', ldata);
1063 } 1069 }
1064 put_tty_queue('\0', ldata); 1070 put_tty_queue('\0', ldata);
1065 wake_up_interruptible(&tty->read_wait); 1071 wake_up_interruptible(&tty->read_wait);
1066 } 1072 }
1067 1073
1068 /** 1074 /**
1069 * n_tty_receive_overrun - handle overrun reporting 1075 * n_tty_receive_overrun - handle overrun reporting
1070 * @tty: terminal 1076 * @tty: terminal
1071 * 1077 *
1072 * Data arrived faster than we could process it. While the tty 1078 * Data arrived faster than we could process it. While the tty
1073 * driver has flagged this the bits that were missed are gone 1079 * driver has flagged this the bits that were missed are gone
1074 * forever. 1080 * forever.
1075 * 1081 *
1076 * Called from the receive_buf path so single threaded. Does not 1082 * Called from the receive_buf path so single threaded. Does not
1077 * need locking as num_overrun and overrun_time are function 1083 * need locking as num_overrun and overrun_time are function
1078 * private. 1084 * private.
1079 */ 1085 */
1080 1086
1081 static inline void n_tty_receive_overrun(struct tty_struct *tty) 1087 static inline void n_tty_receive_overrun(struct tty_struct *tty)
1082 { 1088 {
1083 struct n_tty_data *ldata = tty->disc_data; 1089 struct n_tty_data *ldata = tty->disc_data;
1084 char buf[64]; 1090 char buf[64];
1085 1091
1086 ldata->num_overrun++; 1092 ldata->num_overrun++;
1087 if (time_after(jiffies, ldata->overrun_time + HZ) || 1093 if (time_after(jiffies, ldata->overrun_time + HZ) ||
1088 time_after(ldata->overrun_time, jiffies)) { 1094 time_after(ldata->overrun_time, jiffies)) {
1089 printk(KERN_WARNING "%s: %d input overrun(s)\n", 1095 printk(KERN_WARNING "%s: %d input overrun(s)\n",
1090 tty_name(tty, buf), 1096 tty_name(tty, buf),
1091 ldata->num_overrun); 1097 ldata->num_overrun);
1092 ldata->overrun_time = jiffies; 1098 ldata->overrun_time = jiffies;
1093 ldata->num_overrun = 0; 1099 ldata->num_overrun = 0;
1094 } 1100 }
1095 } 1101 }
1096 1102
1097 /** 1103 /**
1098 * n_tty_receive_parity_error - error notifier 1104 * n_tty_receive_parity_error - error notifier
1099 * @tty: terminal device 1105 * @tty: terminal device
1100 * @c: character 1106 * @c: character
1101 * 1107 *
1102 * Process a parity error and queue the right data to indicate 1108 * Process a parity error and queue the right data to indicate
1103 * the error case if necessary. Locking as per n_tty_receive_buf. 1109 * the error case if necessary. Locking as per n_tty_receive_buf.
1104 */ 1110 */
1105 static inline void n_tty_receive_parity_error(struct tty_struct *tty, 1111 static inline void n_tty_receive_parity_error(struct tty_struct *tty,
1106 unsigned char c) 1112 unsigned char c)
1107 { 1113 {
1108 struct n_tty_data *ldata = tty->disc_data; 1114 struct n_tty_data *ldata = tty->disc_data;
1109 1115
1110 if (I_IGNPAR(tty)) 1116 if (I_IGNPAR(tty))
1111 return; 1117 return;
1112 if (I_PARMRK(tty)) { 1118 if (I_PARMRK(tty)) {
1113 put_tty_queue('\377', ldata); 1119 put_tty_queue('\377', ldata);
1114 put_tty_queue('\0', ldata); 1120 put_tty_queue('\0', ldata);
1115 put_tty_queue(c, ldata); 1121 put_tty_queue(c, ldata);
1116 } else if (I_INPCK(tty)) 1122 } else if (I_INPCK(tty))
1117 put_tty_queue('\0', ldata); 1123 put_tty_queue('\0', ldata);
1118 else 1124 else
1119 put_tty_queue(c, ldata); 1125 put_tty_queue(c, ldata);
1120 wake_up_interruptible(&tty->read_wait); 1126 wake_up_interruptible(&tty->read_wait);
1121 } 1127 }
1122 1128
1123 /** 1129 /**
1124 * n_tty_receive_char - perform processing 1130 * n_tty_receive_char - perform processing
1125 * @tty: terminal device 1131 * @tty: terminal device
1126 * @c: character 1132 * @c: character
1127 * 1133 *
1128 * Process an individual character of input received from the driver. 1134 * Process an individual character of input received from the driver.
1129 * This is serialized with respect to itself by the rules for the 1135 * This is serialized with respect to itself by the rules for the
1130 * driver above. 1136 * driver above.
1131 */ 1137 */
1132 1138
1133 static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c) 1139 static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
1134 { 1140 {
1135 struct n_tty_data *ldata = tty->disc_data; 1141 struct n_tty_data *ldata = tty->disc_data;
1136 unsigned long flags; 1142 unsigned long flags;
1137 int parmrk; 1143 int parmrk;
1138 1144
1139 if (ldata->raw) { 1145 if (ldata->raw) {
1140 put_tty_queue(c, ldata); 1146 put_tty_queue(c, ldata);
1141 return; 1147 return;
1142 } 1148 }
1143 1149
1144 if (I_ISTRIP(tty)) 1150 if (I_ISTRIP(tty))
1145 c &= 0x7f; 1151 c &= 0x7f;
1146 if (I_IUCLC(tty) && L_IEXTEN(tty)) 1152 if (I_IUCLC(tty) && L_IEXTEN(tty))
1147 c = tolower(c); 1153 c = tolower(c);
1148 1154
1149 if (L_EXTPROC(tty)) { 1155 if (L_EXTPROC(tty)) {
1150 put_tty_queue(c, ldata); 1156 put_tty_queue(c, ldata);
1151 return; 1157 return;
1152 } 1158 }
1153 1159
1154 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) && 1160 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) &&
1155 I_IXANY(tty) && c != START_CHAR(tty) && c != STOP_CHAR(tty) && 1161 I_IXANY(tty) && c != START_CHAR(tty) && c != STOP_CHAR(tty) &&
1156 c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) && c != SUSP_CHAR(tty)) { 1162 c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) && c != SUSP_CHAR(tty)) {
1157 start_tty(tty); 1163 start_tty(tty);
1158 process_echoes(tty); 1164 process_echoes(tty);
1159 } 1165 }
1160 1166
1161 if (tty->closing) { 1167 if (tty->closing) {
1162 if (I_IXON(tty)) { 1168 if (I_IXON(tty)) {
1163 if (c == START_CHAR(tty)) { 1169 if (c == START_CHAR(tty)) {
1164 start_tty(tty); 1170 start_tty(tty);
1165 process_echoes(tty); 1171 process_echoes(tty);
1166 } else if (c == STOP_CHAR(tty)) 1172 } else if (c == STOP_CHAR(tty))
1167 stop_tty(tty); 1173 stop_tty(tty);
1168 } 1174 }
1169 return; 1175 return;
1170 } 1176 }
1171 1177
1172 /* 1178 /*
1173 * If the previous character was LNEXT, or we know that this 1179 * If the previous character was LNEXT, or we know that this
1174 * character is not one of the characters that we'll have to 1180 * character is not one of the characters that we'll have to
1175 * handle specially, do shortcut processing to speed things 1181 * handle specially, do shortcut processing to speed things
1176 * up. 1182 * up.
1177 */ 1183 */
1178 if (!test_bit(c, ldata->process_char_map) || ldata->lnext) { 1184 if (!test_bit(c, ldata->process_char_map) || ldata->lnext) {
1179 ldata->lnext = 0; 1185 ldata->lnext = 0;
1180 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0; 1186 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
1181 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) { 1187 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
1182 /* beep if no space */ 1188 /* beep if no space */
1183 if (L_ECHO(tty)) 1189 if (L_ECHO(tty))
1184 process_output('\a', tty); 1190 process_output('\a', tty);
1185 return; 1191 return;
1186 } 1192 }
1187 if (L_ECHO(tty)) { 1193 if (L_ECHO(tty)) {
1188 finish_erasing(ldata); 1194 finish_erasing(ldata);
1189 /* Record the column of first canon char. */ 1195 /* Record the column of first canon char. */
1190 if (ldata->canon_head == ldata->read_head) 1196 if (ldata->canon_head == ldata->read_head)
1191 echo_set_canon_col(ldata); 1197 echo_set_canon_col(ldata);
1192 echo_char(c, tty); 1198 echo_char(c, tty);
1193 process_echoes(tty); 1199 process_echoes(tty);
1194 } 1200 }
1195 if (parmrk) 1201 if (parmrk)
1196 put_tty_queue(c, ldata); 1202 put_tty_queue(c, ldata);
1197 put_tty_queue(c, ldata); 1203 put_tty_queue(c, ldata);
1198 return; 1204 return;
1199 } 1205 }
1200 1206
1201 if (I_IXON(tty)) { 1207 if (I_IXON(tty)) {
1202 if (c == START_CHAR(tty)) { 1208 if (c == START_CHAR(tty)) {
1203 start_tty(tty); 1209 start_tty(tty);
1204 process_echoes(tty); 1210 process_echoes(tty);
1205 return; 1211 return;
1206 } 1212 }
1207 if (c == STOP_CHAR(tty)) { 1213 if (c == STOP_CHAR(tty)) {
1208 stop_tty(tty); 1214 stop_tty(tty);
1209 return; 1215 return;
1210 } 1216 }
1211 } 1217 }
1212 1218
1213 if (L_ISIG(tty)) { 1219 if (L_ISIG(tty)) {
1214 int signal; 1220 int signal;
1215 signal = SIGINT; 1221 signal = SIGINT;
1216 if (c == INTR_CHAR(tty)) 1222 if (c == INTR_CHAR(tty))
1217 goto send_signal; 1223 goto send_signal;
1218 signal = SIGQUIT; 1224 signal = SIGQUIT;
1219 if (c == QUIT_CHAR(tty)) 1225 if (c == QUIT_CHAR(tty))
1220 goto send_signal; 1226 goto send_signal;
1221 signal = SIGTSTP; 1227 signal = SIGTSTP;
1222 if (c == SUSP_CHAR(tty)) { 1228 if (c == SUSP_CHAR(tty)) {
1223 send_signal: 1229 send_signal:
1224 if (!L_NOFLSH(tty)) { 1230 if (!L_NOFLSH(tty)) {
1225 n_tty_flush_buffer(tty); 1231 n_tty_flush_buffer(tty);
1226 tty_driver_flush_buffer(tty); 1232 tty_driver_flush_buffer(tty);
1227 } 1233 }
1228 if (I_IXON(tty)) 1234 if (I_IXON(tty))
1229 start_tty(tty); 1235 start_tty(tty);
1230 if (L_ECHO(tty)) { 1236 if (L_ECHO(tty)) {
1231 echo_char(c, tty); 1237 echo_char(c, tty);
1232 process_echoes(tty); 1238 process_echoes(tty);
1233 } 1239 }
1234 isig(signal, tty); 1240 isig(signal, tty);
1235 return; 1241 return;
1236 } 1242 }
1237 } 1243 }
1238 1244
1239 if (c == '\r') { 1245 if (c == '\r') {
1240 if (I_IGNCR(tty)) 1246 if (I_IGNCR(tty))
1241 return; 1247 return;
1242 if (I_ICRNL(tty)) 1248 if (I_ICRNL(tty))
1243 c = '\n'; 1249 c = '\n';
1244 } else if (c == '\n' && I_INLCR(tty)) 1250 } else if (c == '\n' && I_INLCR(tty))
1245 c = '\r'; 1251 c = '\r';
1246 1252
1247 if (ldata->icanon) { 1253 if (ldata->icanon) {
1248 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) || 1254 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
1249 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) { 1255 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
1250 eraser(c, tty); 1256 eraser(c, tty);
1251 process_echoes(tty); 1257 process_echoes(tty);
1252 return; 1258 return;
1253 } 1259 }
1254 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) { 1260 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
1255 ldata->lnext = 1; 1261 ldata->lnext = 1;
1256 if (L_ECHO(tty)) { 1262 if (L_ECHO(tty)) {
1257 finish_erasing(ldata); 1263 finish_erasing(ldata);
1258 if (L_ECHOCTL(tty)) { 1264 if (L_ECHOCTL(tty)) {
1259 echo_char_raw('^', ldata); 1265 echo_char_raw('^', ldata);
1260 echo_char_raw('\b', ldata); 1266 echo_char_raw('\b', ldata);
1261 process_echoes(tty); 1267 process_echoes(tty);
1262 } 1268 }
1263 } 1269 }
1264 return; 1270 return;
1265 } 1271 }
1266 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) && 1272 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
1267 L_IEXTEN(tty)) { 1273 L_IEXTEN(tty)) {
1268 unsigned long tail = ldata->canon_head; 1274 unsigned long tail = ldata->canon_head;
1269 1275
1270 finish_erasing(ldata); 1276 finish_erasing(ldata);
1271 echo_char(c, tty); 1277 echo_char(c, tty);
1272 echo_char_raw('\n', ldata); 1278 echo_char_raw('\n', ldata);
1273 while (tail != ldata->read_head) { 1279 while (tail != ldata->read_head) {
1274 echo_char(ldata->read_buf[tail], tty); 1280 echo_char(ldata->read_buf[tail], tty);
1275 tail = (tail+1) & (N_TTY_BUF_SIZE-1); 1281 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
1276 } 1282 }
1277 process_echoes(tty); 1283 process_echoes(tty);
1278 return; 1284 return;
1279 } 1285 }
1280 if (c == '\n') { 1286 if (c == '\n') {
1281 if (ldata->read_cnt >= N_TTY_BUF_SIZE) { 1287 if (ldata->read_cnt >= N_TTY_BUF_SIZE) {
1282 if (L_ECHO(tty)) 1288 if (L_ECHO(tty))
1283 process_output('\a', tty); 1289 process_output('\a', tty);
1284 return; 1290 return;
1285 } 1291 }
1286 if (L_ECHO(tty) || L_ECHONL(tty)) { 1292 if (L_ECHO(tty) || L_ECHONL(tty)) {
1287 echo_char_raw('\n', ldata); 1293 echo_char_raw('\n', ldata);
1288 process_echoes(tty); 1294 process_echoes(tty);
1289 } 1295 }
1290 goto handle_newline; 1296 goto handle_newline;
1291 } 1297 }
1292 if (c == EOF_CHAR(tty)) { 1298 if (c == EOF_CHAR(tty)) {
1293 if (ldata->read_cnt >= N_TTY_BUF_SIZE) 1299 if (ldata->read_cnt >= N_TTY_BUF_SIZE)
1294 return; 1300 return;
1295 if (ldata->canon_head != ldata->read_head) 1301 if (ldata->canon_head != ldata->read_head)
1296 set_bit(TTY_PUSH, &tty->flags); 1302 set_bit(TTY_PUSH, &tty->flags);
1297 c = __DISABLED_CHAR; 1303 c = __DISABLED_CHAR;
1298 goto handle_newline; 1304 goto handle_newline;
1299 } 1305 }
1300 if ((c == EOL_CHAR(tty)) || 1306 if ((c == EOL_CHAR(tty)) ||
1301 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) { 1307 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
1302 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) 1308 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty))
1303 ? 1 : 0; 1309 ? 1 : 0;
1304 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk)) { 1310 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk)) {
1305 if (L_ECHO(tty)) 1311 if (L_ECHO(tty))
1306 process_output('\a', tty); 1312 process_output('\a', tty);
1307 return; 1313 return;
1308 } 1314 }
1309 /* 1315 /*
1310 * XXX are EOL_CHAR and EOL2_CHAR echoed?!? 1316 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
1311 */ 1317 */
1312 if (L_ECHO(tty)) { 1318 if (L_ECHO(tty)) {
1313 /* Record the column of first canon char. */ 1319 /* Record the column of first canon char. */
1314 if (ldata->canon_head == ldata->read_head) 1320 if (ldata->canon_head == ldata->read_head)
1315 echo_set_canon_col(ldata); 1321 echo_set_canon_col(ldata);
1316 echo_char(c, tty); 1322 echo_char(c, tty);
1317 process_echoes(tty); 1323 process_echoes(tty);
1318 } 1324 }
1319 /* 1325 /*
1320 * XXX does PARMRK doubling happen for 1326 * XXX does PARMRK doubling happen for
1321 * EOL_CHAR and EOL2_CHAR? 1327 * EOL_CHAR and EOL2_CHAR?
1322 */ 1328 */
1323 if (parmrk) 1329 if (parmrk)
1324 put_tty_queue(c, ldata); 1330 put_tty_queue(c, ldata);
1325 1331
1326 handle_newline: 1332 handle_newline:
1327 raw_spin_lock_irqsave(&ldata->read_lock, flags); 1333 raw_spin_lock_irqsave(&ldata->read_lock, flags);
1328 set_bit(ldata->read_head, ldata->read_flags); 1334 set_bit(ldata->read_head, ldata->read_flags);
1329 put_tty_queue_nolock(c, ldata); 1335 put_tty_queue_nolock(c, ldata);
1330 ldata->canon_head = ldata->read_head; 1336 ldata->canon_head = ldata->read_head;
1331 ldata->canon_data++; 1337 ldata->canon_data++;
1332 raw_spin_unlock_irqrestore(&ldata->read_lock, flags); 1338 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
1333 kill_fasync(&tty->fasync, SIGIO, POLL_IN); 1339 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1334 if (waitqueue_active(&tty->read_wait)) 1340 if (waitqueue_active(&tty->read_wait))
1335 wake_up_interruptible(&tty->read_wait); 1341 wake_up_interruptible(&tty->read_wait);
1336 return; 1342 return;
1337 } 1343 }
1338 } 1344 }
1339 1345
1340 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0; 1346 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
1341 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) { 1347 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
1342 /* beep if no space */ 1348 /* beep if no space */
1343 if (L_ECHO(tty)) 1349 if (L_ECHO(tty))
1344 process_output('\a', tty); 1350 process_output('\a', tty);
1345 return; 1351 return;
1346 } 1352 }
1347 if (L_ECHO(tty)) { 1353 if (L_ECHO(tty)) {
1348 finish_erasing(ldata); 1354 finish_erasing(ldata);
1349 if (c == '\n') 1355 if (c == '\n')
1350 echo_char_raw('\n', ldata); 1356 echo_char_raw('\n', ldata);
1351 else { 1357 else {
1352 /* Record the column of first canon char. */ 1358 /* Record the column of first canon char. */
1353 if (ldata->canon_head == ldata->read_head) 1359 if (ldata->canon_head == ldata->read_head)
1354 echo_set_canon_col(ldata); 1360 echo_set_canon_col(ldata);
1355 echo_char(c, tty); 1361 echo_char(c, tty);
1356 } 1362 }
1357 process_echoes(tty); 1363 process_echoes(tty);
1358 } 1364 }
1359 1365
1360 if (parmrk) 1366 if (parmrk)
1361 put_tty_queue(c, ldata); 1367 put_tty_queue(c, ldata);
1362 1368
1363 put_tty_queue(c, ldata); 1369 put_tty_queue(c, ldata);
1364 } 1370 }
1365 1371
1366 1372
1367 /** 1373 /**
1368 * n_tty_write_wakeup - asynchronous I/O notifier 1374 * n_tty_write_wakeup - asynchronous I/O notifier
1369 * @tty: tty device 1375 * @tty: tty device
1370 * 1376 *
1371 * Required for the ptys, serial driver etc. since processes 1377 * Required for the ptys, serial driver etc. since processes
1372 * that attach themselves to the master and rely on ASYNC 1378 * that attach themselves to the master and rely on ASYNC
1373 * IO must be woken up 1379 * IO must be woken up
1374 */ 1380 */
1375 1381
1376 static void n_tty_write_wakeup(struct tty_struct *tty) 1382 static void n_tty_write_wakeup(struct tty_struct *tty)
1377 { 1383 {
1378 if (tty->fasync && test_and_clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) 1384 if (tty->fasync && test_and_clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags))
1379 kill_fasync(&tty->fasync, SIGIO, POLL_OUT); 1385 kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
1380 } 1386 }
1381 1387
1382 /** 1388 /**
1383 * n_tty_receive_buf - data receive 1389 * n_tty_receive_buf - data receive
1384 * @tty: terminal device 1390 * @tty: terminal device
1385 * @cp: buffer 1391 * @cp: buffer
1386 * @fp: flag buffer 1392 * @fp: flag buffer
1387 * @count: characters 1393 * @count: characters
1388 * 1394 *
1389 * Called by the terminal driver when a block of characters has 1395 * Called by the terminal driver when a block of characters has
1390 * been received. This function must be called from soft contexts 1396 * been received. This function must be called from soft contexts
1391 * not from interrupt context. The driver is responsible for making 1397 * not from interrupt context. The driver is responsible for making
1392 * calls one at a time and in order (or using flush_to_ldisc) 1398 * calls one at a time and in order (or using flush_to_ldisc)
1393 */ 1399 */
1394 1400
1395 static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp, 1401 static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
1396 char *fp, int count) 1402 char *fp, int count)
1397 { 1403 {
1398 struct n_tty_data *ldata = tty->disc_data; 1404 struct n_tty_data *ldata = tty->disc_data;
1399 const unsigned char *p; 1405 const unsigned char *p;
1400 char *f, flags = TTY_NORMAL; 1406 char *f, flags = TTY_NORMAL;
1401 int i; 1407 int i;
1402 char buf[64]; 1408 char buf[64];
1403 unsigned long cpuflags; 1409 unsigned long cpuflags;
1404 1410
1405 if (ldata->real_raw) { 1411 if (ldata->real_raw) {
1406 raw_spin_lock_irqsave(&ldata->read_lock, cpuflags); 1412 raw_spin_lock_irqsave(&ldata->read_lock, cpuflags);
1407 i = min(N_TTY_BUF_SIZE - ldata->read_cnt, 1413 i = min(N_TTY_BUF_SIZE - ldata->read_cnt,
1408 N_TTY_BUF_SIZE - ldata->read_head); 1414 N_TTY_BUF_SIZE - ldata->read_head);
1409 i = min(count, i); 1415 i = min(count, i);
1410 memcpy(ldata->read_buf + ldata->read_head, cp, i); 1416 memcpy(ldata->read_buf + ldata->read_head, cp, i);
1411 ldata->read_head = (ldata->read_head + i) & (N_TTY_BUF_SIZE-1); 1417 ldata->read_head = (ldata->read_head + i) & (N_TTY_BUF_SIZE-1);
1412 ldata->read_cnt += i; 1418 ldata->read_cnt += i;
1413 cp += i; 1419 cp += i;
1414 count -= i; 1420 count -= i;
1415 1421
1416 i = min(N_TTY_BUF_SIZE - ldata->read_cnt, 1422 i = min(N_TTY_BUF_SIZE - ldata->read_cnt,
1417 N_TTY_BUF_SIZE - ldata->read_head); 1423 N_TTY_BUF_SIZE - ldata->read_head);
1418 i = min(count, i); 1424 i = min(count, i);
1419 memcpy(ldata->read_buf + ldata->read_head, cp, i); 1425 memcpy(ldata->read_buf + ldata->read_head, cp, i);
1420 ldata->read_head = (ldata->read_head + i) & (N_TTY_BUF_SIZE-1); 1426 ldata->read_head = (ldata->read_head + i) & (N_TTY_BUF_SIZE-1);
1421 ldata->read_cnt += i; 1427 ldata->read_cnt += i;
1422 raw_spin_unlock_irqrestore(&ldata->read_lock, cpuflags); 1428 raw_spin_unlock_irqrestore(&ldata->read_lock, cpuflags);
1423 } else { 1429 } else {
1424 for (i = count, p = cp, f = fp; i; i--, p++) { 1430 for (i = count, p = cp, f = fp; i; i--, p++) {
1425 if (f) 1431 if (f)
1426 flags = *f++; 1432 flags = *f++;
1427 switch (flags) { 1433 switch (flags) {
1428 case TTY_NORMAL: 1434 case TTY_NORMAL:
1429 n_tty_receive_char(tty, *p); 1435 n_tty_receive_char(tty, *p);
1430 break; 1436 break;
1431 case TTY_BREAK: 1437 case TTY_BREAK:
1432 n_tty_receive_break(tty); 1438 n_tty_receive_break(tty);
1433 break; 1439 break;
1434 case TTY_PARITY: 1440 case TTY_PARITY:
1435 case TTY_FRAME: 1441 case TTY_FRAME:
1436 n_tty_receive_parity_error(tty, *p); 1442 n_tty_receive_parity_error(tty, *p);
1437 break; 1443 break;
1438 case TTY_OVERRUN: 1444 case TTY_OVERRUN:
1439 n_tty_receive_overrun(tty); 1445 n_tty_receive_overrun(tty);
1440 break; 1446 break;
1441 default: 1447 default:
1442 printk(KERN_ERR "%s: unknown flag %d\n", 1448 printk(KERN_ERR "%s: unknown flag %d\n",
1443 tty_name(tty, buf), flags); 1449 tty_name(tty, buf), flags);
1444 break; 1450 break;
1445 } 1451 }
1446 } 1452 }
1447 if (tty->ops->flush_chars) 1453 if (tty->ops->flush_chars)
1448 tty->ops->flush_chars(tty); 1454 tty->ops->flush_chars(tty);
1449 } 1455 }
1450 1456
1451 n_tty_set_room(tty); 1457 n_tty_set_room(tty);
1452 1458
1453 if ((!ldata->icanon && (ldata->read_cnt >= tty->minimum_to_wake)) || 1459 if ((!ldata->icanon && (ldata->read_cnt >= tty->minimum_to_wake)) ||
1454 L_EXTPROC(tty)) { 1460 L_EXTPROC(tty)) {
1455 kill_fasync(&tty->fasync, SIGIO, POLL_IN); 1461 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1456 if (waitqueue_active(&tty->read_wait)) 1462 if (waitqueue_active(&tty->read_wait))
1457 wake_up_interruptible(&tty->read_wait); 1463 wake_up_interruptible(&tty->read_wait);
1458 } 1464 }
1459 1465
1460 /* 1466 /*
1461 * Check the remaining room for the input canonicalization 1467 * Check the remaining room for the input canonicalization
1462 * mode. We don't want to throttle the driver if we're in 1468 * mode. We don't want to throttle the driver if we're in
1463 * canonical mode and don't have a newline yet! 1469 * canonical mode and don't have a newline yet!
1464 */ 1470 */
1465 while (1) { 1471 while (1) {
1466 tty_set_flow_change(tty, TTY_THROTTLE_SAFE); 1472 tty_set_flow_change(tty, TTY_THROTTLE_SAFE);
1467 if (tty->receive_room >= TTY_THRESHOLD_THROTTLE) 1473 if (tty->receive_room >= TTY_THRESHOLD_THROTTLE)
1468 break; 1474 break;
1469 if (!tty_throttle_safe(tty)) 1475 if (!tty_throttle_safe(tty))
1470 break; 1476 break;
1471 } 1477 }
1472 __tty_set_flow_change(tty, 0); 1478 __tty_set_flow_change(tty, 0);
1473 } 1479 }
1474 1480
1475 int is_ignored(int sig) 1481 int is_ignored(int sig)
1476 { 1482 {
1477 return (sigismember(&current->blocked, sig) || 1483 return (sigismember(&current->blocked, sig) ||
1478 current->sighand->action[sig-1].sa.sa_handler == SIG_IGN); 1484 current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
1479 } 1485 }
1480 1486
1481 /** 1487 /**
1482 * n_tty_set_termios - termios data changed 1488 * n_tty_set_termios - termios data changed
1483 * @tty: terminal 1489 * @tty: terminal
1484 * @old: previous data 1490 * @old: previous data
1485 * 1491 *
1486 * Called by the tty layer when the user changes termios flags so 1492 * Called by the tty layer when the user changes termios flags so
1487 * that the line discipline can plan ahead. This function cannot sleep 1493 * that the line discipline can plan ahead. This function cannot sleep
1488 * and is protected from re-entry by the tty layer. The user is 1494 * and is protected from re-entry by the tty layer. The user is
1489 * guaranteed that this function will not be re-entered or in progress 1495 * guaranteed that this function will not be re-entered or in progress
1490 * when the ldisc is closed. 1496 * when the ldisc is closed.
1491 * 1497 *
1492 * Locking: Caller holds tty->termios_mutex 1498 * Locking: Caller holds tty->termios_mutex
1493 */ 1499 */
1494 1500
1495 static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old) 1501 static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
1496 { 1502 {
1497 struct n_tty_data *ldata = tty->disc_data; 1503 struct n_tty_data *ldata = tty->disc_data;
1498 int canon_change = 1; 1504 int canon_change = 1;
1499 1505
1500 if (old) 1506 if (old)
1501 canon_change = (old->c_lflag ^ tty->termios.c_lflag) & ICANON; 1507 canon_change = (old->c_lflag ^ tty->termios.c_lflag) & ICANON;
1502 if (canon_change) { 1508 if (canon_change) {
1503 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE); 1509 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
1504 ldata->canon_head = ldata->read_tail; 1510 ldata->canon_head = ldata->read_tail;
1505 ldata->canon_data = 0; 1511 ldata->canon_data = 0;
1506 ldata->erasing = 0; 1512 ldata->erasing = 0;
1507 } 1513 }
1508 1514
1509 if (canon_change && !L_ICANON(tty) && ldata->read_cnt) 1515 if (canon_change && !L_ICANON(tty) && ldata->read_cnt)
1510 wake_up_interruptible(&tty->read_wait); 1516 wake_up_interruptible(&tty->read_wait);
1511 1517
1512 ldata->icanon = (L_ICANON(tty) != 0); 1518 ldata->icanon = (L_ICANON(tty) != 0);
1513 if (test_bit(TTY_HW_COOK_IN, &tty->flags)) { 1519 if (test_bit(TTY_HW_COOK_IN, &tty->flags)) {
1514 ldata->raw = 1; 1520 ldata->raw = 1;
1515 ldata->real_raw = 1; 1521 ldata->real_raw = 1;
1516 n_tty_set_room(tty); 1522 n_tty_set_room(tty);
1517 return; 1523 return;
1518 } 1524 }
1519 if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) || 1525 if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
1520 I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) || 1526 I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
1521 I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) || 1527 I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
1522 I_PARMRK(tty)) { 1528 I_PARMRK(tty)) {
1523 bitmap_zero(ldata->process_char_map, 256); 1529 bitmap_zero(ldata->process_char_map, 256);
1524 1530
1525 if (I_IGNCR(tty) || I_ICRNL(tty)) 1531 if (I_IGNCR(tty) || I_ICRNL(tty))
1526 set_bit('\r', ldata->process_char_map); 1532 set_bit('\r', ldata->process_char_map);
1527 if (I_INLCR(tty)) 1533 if (I_INLCR(tty))
1528 set_bit('\n', ldata->process_char_map); 1534 set_bit('\n', ldata->process_char_map);
1529 1535
1530 if (L_ICANON(tty)) { 1536 if (L_ICANON(tty)) {
1531 set_bit(ERASE_CHAR(tty), ldata->process_char_map); 1537 set_bit(ERASE_CHAR(tty), ldata->process_char_map);
1532 set_bit(KILL_CHAR(tty), ldata->process_char_map); 1538 set_bit(KILL_CHAR(tty), ldata->process_char_map);
1533 set_bit(EOF_CHAR(tty), ldata->process_char_map); 1539 set_bit(EOF_CHAR(tty), ldata->process_char_map);
1534 set_bit('\n', ldata->process_char_map); 1540 set_bit('\n', ldata->process_char_map);
1535 set_bit(EOL_CHAR(tty), ldata->process_char_map); 1541 set_bit(EOL_CHAR(tty), ldata->process_char_map);
1536 if (L_IEXTEN(tty)) { 1542 if (L_IEXTEN(tty)) {
1537 set_bit(WERASE_CHAR(tty), 1543 set_bit(WERASE_CHAR(tty),
1538 ldata->process_char_map); 1544 ldata->process_char_map);
1539 set_bit(LNEXT_CHAR(tty), 1545 set_bit(LNEXT_CHAR(tty),
1540 ldata->process_char_map); 1546 ldata->process_char_map);
1541 set_bit(EOL2_CHAR(tty), 1547 set_bit(EOL2_CHAR(tty),
1542 ldata->process_char_map); 1548 ldata->process_char_map);
1543 if (L_ECHO(tty)) 1549 if (L_ECHO(tty))
1544 set_bit(REPRINT_CHAR(tty), 1550 set_bit(REPRINT_CHAR(tty),
1545 ldata->process_char_map); 1551 ldata->process_char_map);
1546 } 1552 }
1547 } 1553 }
1548 if (I_IXON(tty)) { 1554 if (I_IXON(tty)) {
1549 set_bit(START_CHAR(tty), ldata->process_char_map); 1555 set_bit(START_CHAR(tty), ldata->process_char_map);
1550 set_bit(STOP_CHAR(tty), ldata->process_char_map); 1556 set_bit(STOP_CHAR(tty), ldata->process_char_map);
1551 } 1557 }
1552 if (L_ISIG(tty)) { 1558 if (L_ISIG(tty)) {
1553 set_bit(INTR_CHAR(tty), ldata->process_char_map); 1559 set_bit(INTR_CHAR(tty), ldata->process_char_map);
1554 set_bit(QUIT_CHAR(tty), ldata->process_char_map); 1560 set_bit(QUIT_CHAR(tty), ldata->process_char_map);
1555 set_bit(SUSP_CHAR(tty), ldata->process_char_map); 1561 set_bit(SUSP_CHAR(tty), ldata->process_char_map);
1556 } 1562 }
1557 clear_bit(__DISABLED_CHAR, ldata->process_char_map); 1563 clear_bit(__DISABLED_CHAR, ldata->process_char_map);
1558 ldata->raw = 0; 1564 ldata->raw = 0;
1559 ldata->real_raw = 0; 1565 ldata->real_raw = 0;
1560 } else { 1566 } else {
1561 ldata->raw = 1; 1567 ldata->raw = 1;
1562 if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) && 1568 if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
1563 (I_IGNPAR(tty) || !I_INPCK(tty)) && 1569 (I_IGNPAR(tty) || !I_INPCK(tty)) &&
1564 (tty->driver->flags & TTY_DRIVER_REAL_RAW)) 1570 (tty->driver->flags & TTY_DRIVER_REAL_RAW))
1565 ldata->real_raw = 1; 1571 ldata->real_raw = 1;
1566 else 1572 else
1567 ldata->real_raw = 0; 1573 ldata->real_raw = 0;
1568 } 1574 }
1569 n_tty_set_room(tty); 1575 n_tty_set_room(tty);
1570 /* The termios change make the tty ready for I/O */ 1576 /* The termios change make the tty ready for I/O */
1571 wake_up_interruptible(&tty->write_wait); 1577 wake_up_interruptible(&tty->write_wait);
1572 wake_up_interruptible(&tty->read_wait); 1578 wake_up_interruptible(&tty->read_wait);
1573 } 1579 }
1574 1580
1575 /** 1581 /**
1576 * n_tty_close - close the ldisc for this tty 1582 * n_tty_close - close the ldisc for this tty
1577 * @tty: device 1583 * @tty: device
1578 * 1584 *
1579 * Called from the terminal layer when this line discipline is 1585 * Called from the terminal layer when this line discipline is
1580 * being shut down, either because of a close or becsuse of a 1586 * being shut down, either because of a close or becsuse of a
1581 * discipline change. The function will not be called while other 1587 * discipline change. The function will not be called while other
1582 * ldisc methods are in progress. 1588 * ldisc methods are in progress.
1583 */ 1589 */
1584 1590
1585 static void n_tty_close(struct tty_struct *tty) 1591 static void n_tty_close(struct tty_struct *tty)
1586 { 1592 {
1587 struct n_tty_data *ldata = tty->disc_data; 1593 struct n_tty_data *ldata = tty->disc_data;
1588 1594
1589 n_tty_flush_buffer(tty); 1595 n_tty_flush_buffer(tty);
1590 kfree(ldata->read_buf); 1596 kfree(ldata->read_buf);
1591 kfree(ldata->echo_buf); 1597 kfree(ldata->echo_buf);
1592 kfree(ldata); 1598 kfree(ldata);
1593 tty->disc_data = NULL; 1599 tty->disc_data = NULL;
1594 } 1600 }
1595 1601
1596 /** 1602 /**
1597 * n_tty_open - open an ldisc 1603 * n_tty_open - open an ldisc
1598 * @tty: terminal to open 1604 * @tty: terminal to open
1599 * 1605 *
1600 * Called when this line discipline is being attached to the 1606 * Called when this line discipline is being attached to the
1601 * terminal device. Can sleep. Called serialized so that no 1607 * terminal device. Can sleep. Called serialized so that no
1602 * other events will occur in parallel. No further open will occur 1608 * other events will occur in parallel. No further open will occur
1603 * until a close. 1609 * until a close.
1604 */ 1610 */
1605 1611
1606 static int n_tty_open(struct tty_struct *tty) 1612 static int n_tty_open(struct tty_struct *tty)
1607 { 1613 {
1608 struct n_tty_data *ldata; 1614 struct n_tty_data *ldata;
1609 1615
1610 ldata = kzalloc(sizeof(*ldata), GFP_KERNEL); 1616 ldata = kzalloc(sizeof(*ldata), GFP_KERNEL);
1611 if (!ldata) 1617 if (!ldata)
1612 goto err; 1618 goto err;
1613 1619
1614 ldata->overrun_time = jiffies; 1620 ldata->overrun_time = jiffies;
1615 mutex_init(&ldata->atomic_read_lock); 1621 mutex_init(&ldata->atomic_read_lock);
1616 mutex_init(&ldata->output_lock); 1622 mutex_init(&ldata->output_lock);
1617 mutex_init(&ldata->echo_lock); 1623 mutex_init(&ldata->echo_lock);
1618 raw_spin_lock_init(&ldata->read_lock); 1624 raw_spin_lock_init(&ldata->read_lock);
1619 1625
1620 /* These are ugly. Currently a malloc failure here can panic */ 1626 /* These are ugly. Currently a malloc failure here can panic */
1621 ldata->read_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL); 1627 ldata->read_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
1622 ldata->echo_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL); 1628 ldata->echo_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
1623 if (!ldata->read_buf || !ldata->echo_buf) 1629 if (!ldata->read_buf || !ldata->echo_buf)
1624 goto err_free_bufs; 1630 goto err_free_bufs;
1625 1631
1626 tty->disc_data = ldata; 1632 tty->disc_data = ldata;
1633 /* indicate buffer work may resume */
1634 clear_bit(TTY_LDISC_HALTED, &tty->flags);
1627 reset_buffer_flags(tty); 1635 reset_buffer_flags(tty);
1628 tty_unthrottle(tty); 1636 tty_unthrottle(tty);
1629 ldata->column = 0; 1637 ldata->column = 0;
1630 n_tty_set_termios(tty, NULL); 1638 n_tty_set_termios(tty, NULL);
1631 tty->minimum_to_wake = 1; 1639 tty->minimum_to_wake = 1;
1632 tty->closing = 0; 1640 tty->closing = 0;
1633 1641
1634 return 0; 1642 return 0;
1635 err_free_bufs: 1643 err_free_bufs:
1636 kfree(ldata->read_buf); 1644 kfree(ldata->read_buf);
1637 kfree(ldata->echo_buf); 1645 kfree(ldata->echo_buf);
1638 kfree(ldata); 1646 kfree(ldata);
1639 err: 1647 err:
1640 return -ENOMEM; 1648 return -ENOMEM;
1641 } 1649 }
1642 1650
1643 static inline int input_available_p(struct tty_struct *tty, int amt) 1651 static inline int input_available_p(struct tty_struct *tty, int amt)
1644 { 1652 {
1645 struct n_tty_data *ldata = tty->disc_data; 1653 struct n_tty_data *ldata = tty->disc_data;
1646 1654
1647 tty_flush_to_ldisc(tty); 1655 tty_flush_to_ldisc(tty);
1648 if (ldata->icanon && !L_EXTPROC(tty)) { 1656 if (ldata->icanon && !L_EXTPROC(tty)) {
1649 if (ldata->canon_data) 1657 if (ldata->canon_data)
1650 return 1; 1658 return 1;
1651 } else if (ldata->read_cnt >= (amt ? amt : 1)) 1659 } else if (ldata->read_cnt >= (amt ? amt : 1))
1652 return 1; 1660 return 1;
1653 1661
1654 return 0; 1662 return 0;
1655 } 1663 }
1656 1664
1657 /** 1665 /**
1658 * copy_from_read_buf - copy read data directly 1666 * copy_from_read_buf - copy read data directly
1659 * @tty: terminal device 1667 * @tty: terminal device
1660 * @b: user data 1668 * @b: user data
1661 * @nr: size of data 1669 * @nr: size of data
1662 * 1670 *
1663 * Helper function to speed up n_tty_read. It is only called when 1671 * Helper function to speed up n_tty_read. It is only called when
1664 * ICANON is off; it copies characters straight from the tty queue to 1672 * ICANON is off; it copies characters straight from the tty queue to
1665 * user space directly. It can be profitably called twice; once to 1673 * user space directly. It can be profitably called twice; once to
1666 * drain the space from the tail pointer to the (physical) end of the 1674 * drain the space from the tail pointer to the (physical) end of the
1667 * buffer, and once to drain the space from the (physical) beginning of 1675 * buffer, and once to drain the space from the (physical) beginning of
1668 * the buffer to head pointer. 1676 * the buffer to head pointer.
1669 * 1677 *
1670 * Called under the ldata->atomic_read_lock sem 1678 * Called under the ldata->atomic_read_lock sem
1671 * 1679 *
1672 */ 1680 */
1673 1681
1674 static int copy_from_read_buf(struct tty_struct *tty, 1682 static int copy_from_read_buf(struct tty_struct *tty,
1675 unsigned char __user **b, 1683 unsigned char __user **b,
1676 size_t *nr) 1684 size_t *nr)
1677 1685
1678 { 1686 {
1679 struct n_tty_data *ldata = tty->disc_data; 1687 struct n_tty_data *ldata = tty->disc_data;
1680 int retval; 1688 int retval;
1681 size_t n; 1689 size_t n;
1682 unsigned long flags; 1690 unsigned long flags;
1683 bool is_eof; 1691 bool is_eof;
1684 1692
1685 retval = 0; 1693 retval = 0;
1686 raw_spin_lock_irqsave(&ldata->read_lock, flags); 1694 raw_spin_lock_irqsave(&ldata->read_lock, flags);
1687 n = min(ldata->read_cnt, N_TTY_BUF_SIZE - ldata->read_tail); 1695 n = min(ldata->read_cnt, N_TTY_BUF_SIZE - ldata->read_tail);
1688 n = min(*nr, n); 1696 n = min(*nr, n);
1689 raw_spin_unlock_irqrestore(&ldata->read_lock, flags); 1697 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
1690 if (n) { 1698 if (n) {
1691 retval = copy_to_user(*b, &ldata->read_buf[ldata->read_tail], n); 1699 retval = copy_to_user(*b, &ldata->read_buf[ldata->read_tail], n);
1692 n -= retval; 1700 n -= retval;
1693 is_eof = n == 1 && 1701 is_eof = n == 1 &&
1694 ldata->read_buf[ldata->read_tail] == EOF_CHAR(tty); 1702 ldata->read_buf[ldata->read_tail] == EOF_CHAR(tty);
1695 tty_audit_add_data(tty, &ldata->read_buf[ldata->read_tail], n, 1703 tty_audit_add_data(tty, &ldata->read_buf[ldata->read_tail], n,
1696 ldata->icanon); 1704 ldata->icanon);
1697 raw_spin_lock_irqsave(&ldata->read_lock, flags); 1705 raw_spin_lock_irqsave(&ldata->read_lock, flags);
1698 ldata->read_tail = (ldata->read_tail + n) & (N_TTY_BUF_SIZE-1); 1706 ldata->read_tail = (ldata->read_tail + n) & (N_TTY_BUF_SIZE-1);
1699 ldata->read_cnt -= n; 1707 ldata->read_cnt -= n;
1700 /* Turn single EOF into zero-length read */ 1708 /* Turn single EOF into zero-length read */
1701 if (L_EXTPROC(tty) && ldata->icanon && is_eof && !ldata->read_cnt) 1709 if (L_EXTPROC(tty) && ldata->icanon && is_eof && !ldata->read_cnt)
1702 n = 0; 1710 n = 0;
1703 raw_spin_unlock_irqrestore(&ldata->read_lock, flags); 1711 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
1704 *b += n; 1712 *b += n;
1705 *nr -= n; 1713 *nr -= n;
1706 } 1714 }
1707 return retval; 1715 return retval;
1708 } 1716 }
1709 1717
1710 extern ssize_t redirected_tty_write(struct file *, const char __user *, 1718 extern ssize_t redirected_tty_write(struct file *, const char __user *,
1711 size_t, loff_t *); 1719 size_t, loff_t *);
1712 1720
1713 /** 1721 /**
1714 * job_control - check job control 1722 * job_control - check job control
1715 * @tty: tty 1723 * @tty: tty
1716 * @file: file handle 1724 * @file: file handle
1717 * 1725 *
1718 * Perform job control management checks on this file/tty descriptor 1726 * Perform job control management checks on this file/tty descriptor
1719 * and if appropriate send any needed signals and return a negative 1727 * and if appropriate send any needed signals and return a negative
1720 * error code if action should be taken. 1728 * error code if action should be taken.
1721 * 1729 *
1722 * Locking: redirected write test is safe 1730 * Locking: redirected write test is safe
1723 * current->signal->tty check is safe 1731 * current->signal->tty check is safe
1724 * ctrl_lock to safely reference tty->pgrp 1732 * ctrl_lock to safely reference tty->pgrp
1725 */ 1733 */
1726 1734
1727 static int job_control(struct tty_struct *tty, struct file *file) 1735 static int job_control(struct tty_struct *tty, struct file *file)
1728 { 1736 {
1729 /* Job control check -- must be done at start and after 1737 /* Job control check -- must be done at start and after
1730 every sleep (POSIX.1 7.1.1.4). */ 1738 every sleep (POSIX.1 7.1.1.4). */
1731 /* NOTE: not yet done after every sleep pending a thorough 1739 /* NOTE: not yet done after every sleep pending a thorough
1732 check of the logic of this change. -- jlc */ 1740 check of the logic of this change. -- jlc */
1733 /* don't stop on /dev/console */ 1741 /* don't stop on /dev/console */
1734 if (file->f_op->write == redirected_tty_write || 1742 if (file->f_op->write == redirected_tty_write ||
1735 current->signal->tty != tty) 1743 current->signal->tty != tty)
1736 return 0; 1744 return 0;
1737 1745
1738 spin_lock_irq(&tty->ctrl_lock); 1746 spin_lock_irq(&tty->ctrl_lock);
1739 if (!tty->pgrp) 1747 if (!tty->pgrp)
1740 printk(KERN_ERR "n_tty_read: no tty->pgrp!\n"); 1748 printk(KERN_ERR "n_tty_read: no tty->pgrp!\n");
1741 else if (task_pgrp(current) != tty->pgrp) { 1749 else if (task_pgrp(current) != tty->pgrp) {
1742 spin_unlock_irq(&tty->ctrl_lock); 1750 spin_unlock_irq(&tty->ctrl_lock);
1743 if (is_ignored(SIGTTIN) || is_current_pgrp_orphaned()) 1751 if (is_ignored(SIGTTIN) || is_current_pgrp_orphaned())
1744 return -EIO; 1752 return -EIO;
1745 kill_pgrp(task_pgrp(current), SIGTTIN, 1); 1753 kill_pgrp(task_pgrp(current), SIGTTIN, 1);
1746 set_thread_flag(TIF_SIGPENDING); 1754 set_thread_flag(TIF_SIGPENDING);
1747 return -ERESTARTSYS; 1755 return -ERESTARTSYS;
1748 } 1756 }
1749 spin_unlock_irq(&tty->ctrl_lock); 1757 spin_unlock_irq(&tty->ctrl_lock);
1750 return 0; 1758 return 0;
1751 } 1759 }
1752 1760
1753 1761
1754 /** 1762 /**
1755 * n_tty_read - read function for tty 1763 * n_tty_read - read function for tty
1756 * @tty: tty device 1764 * @tty: tty device
1757 * @file: file object 1765 * @file: file object
1758 * @buf: userspace buffer pointer 1766 * @buf: userspace buffer pointer
1759 * @nr: size of I/O 1767 * @nr: size of I/O
1760 * 1768 *
1761 * Perform reads for the line discipline. We are guaranteed that the 1769 * Perform reads for the line discipline. We are guaranteed that the
1762 * line discipline will not be closed under us but we may get multiple 1770 * line discipline will not be closed under us but we may get multiple
1763 * parallel readers and must handle this ourselves. We may also get 1771 * parallel readers and must handle this ourselves. We may also get
1764 * a hangup. Always called in user context, may sleep. 1772 * a hangup. Always called in user context, may sleep.
1765 * 1773 *
1766 * This code must be sure never to sleep through a hangup. 1774 * This code must be sure never to sleep through a hangup.
1767 */ 1775 */
1768 1776
1769 static ssize_t n_tty_read(struct tty_struct *tty, struct file *file, 1777 static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
1770 unsigned char __user *buf, size_t nr) 1778 unsigned char __user *buf, size_t nr)
1771 { 1779 {
1772 struct n_tty_data *ldata = tty->disc_data; 1780 struct n_tty_data *ldata = tty->disc_data;
1773 unsigned char __user *b = buf; 1781 unsigned char __user *b = buf;
1774 DECLARE_WAITQUEUE(wait, current); 1782 DECLARE_WAITQUEUE(wait, current);
1775 int c; 1783 int c;
1776 int minimum, time; 1784 int minimum, time;
1777 ssize_t retval = 0; 1785 ssize_t retval = 0;
1778 ssize_t size; 1786 ssize_t size;
1779 long timeout; 1787 long timeout;
1780 unsigned long flags; 1788 unsigned long flags;
1781 int packet; 1789 int packet;
1782 1790
1783 do_it_again: 1791 do_it_again:
1784 c = job_control(tty, file); 1792 c = job_control(tty, file);
1785 if (c < 0) 1793 if (c < 0)
1786 return c; 1794 return c;
1787 1795
1788 minimum = time = 0; 1796 minimum = time = 0;
1789 timeout = MAX_SCHEDULE_TIMEOUT; 1797 timeout = MAX_SCHEDULE_TIMEOUT;
1790 if (!ldata->icanon) { 1798 if (!ldata->icanon) {
1791 time = (HZ / 10) * TIME_CHAR(tty); 1799 time = (HZ / 10) * TIME_CHAR(tty);
1792 minimum = MIN_CHAR(tty); 1800 minimum = MIN_CHAR(tty);
1793 if (minimum) { 1801 if (minimum) {
1794 if (time) 1802 if (time)
1795 tty->minimum_to_wake = 1; 1803 tty->minimum_to_wake = 1;
1796 else if (!waitqueue_active(&tty->read_wait) || 1804 else if (!waitqueue_active(&tty->read_wait) ||
1797 (tty->minimum_to_wake > minimum)) 1805 (tty->minimum_to_wake > minimum))
1798 tty->minimum_to_wake = minimum; 1806 tty->minimum_to_wake = minimum;
1799 } else { 1807 } else {
1800 timeout = 0; 1808 timeout = 0;
1801 if (time) { 1809 if (time) {
1802 timeout = time; 1810 timeout = time;
1803 time = 0; 1811 time = 0;
1804 } 1812 }
1805 tty->minimum_to_wake = minimum = 1; 1813 tty->minimum_to_wake = minimum = 1;
1806 } 1814 }
1807 } 1815 }
1808 1816
1809 /* 1817 /*
1810 * Internal serialization of reads. 1818 * Internal serialization of reads.
1811 */ 1819 */
1812 if (file->f_flags & O_NONBLOCK) { 1820 if (file->f_flags & O_NONBLOCK) {
1813 if (!mutex_trylock(&ldata->atomic_read_lock)) 1821 if (!mutex_trylock(&ldata->atomic_read_lock))
1814 return -EAGAIN; 1822 return -EAGAIN;
1815 } else { 1823 } else {
1816 if (mutex_lock_interruptible(&ldata->atomic_read_lock)) 1824 if (mutex_lock_interruptible(&ldata->atomic_read_lock))
1817 return -ERESTARTSYS; 1825 return -ERESTARTSYS;
1818 } 1826 }
1819 packet = tty->packet; 1827 packet = tty->packet;
1820 1828
1821 add_wait_queue(&tty->read_wait, &wait); 1829 add_wait_queue(&tty->read_wait, &wait);
1822 while (nr) { 1830 while (nr) {
1823 /* First test for status change. */ 1831 /* First test for status change. */
1824 if (packet && tty->link->ctrl_status) { 1832 if (packet && tty->link->ctrl_status) {
1825 unsigned char cs; 1833 unsigned char cs;
1826 if (b != buf) 1834 if (b != buf)
1827 break; 1835 break;
1828 spin_lock_irqsave(&tty->link->ctrl_lock, flags); 1836 spin_lock_irqsave(&tty->link->ctrl_lock, flags);
1829 cs = tty->link->ctrl_status; 1837 cs = tty->link->ctrl_status;
1830 tty->link->ctrl_status = 0; 1838 tty->link->ctrl_status = 0;
1831 spin_unlock_irqrestore(&tty->link->ctrl_lock, flags); 1839 spin_unlock_irqrestore(&tty->link->ctrl_lock, flags);
1832 if (tty_put_user(tty, cs, b++)) { 1840 if (tty_put_user(tty, cs, b++)) {
1833 retval = -EFAULT; 1841 retval = -EFAULT;
1834 b--; 1842 b--;
1835 break; 1843 break;
1836 } 1844 }
1837 nr--; 1845 nr--;
1838 break; 1846 break;
1839 } 1847 }
1840 /* This statement must be first before checking for input 1848 /* This statement must be first before checking for input
1841 so that any interrupt will set the state back to 1849 so that any interrupt will set the state back to
1842 TASK_RUNNING. */ 1850 TASK_RUNNING. */
1843 set_current_state(TASK_INTERRUPTIBLE); 1851 set_current_state(TASK_INTERRUPTIBLE);
1844 1852
1845 if (((minimum - (b - buf)) < tty->minimum_to_wake) && 1853 if (((minimum - (b - buf)) < tty->minimum_to_wake) &&
1846 ((minimum - (b - buf)) >= 1)) 1854 ((minimum - (b - buf)) >= 1))
1847 tty->minimum_to_wake = (minimum - (b - buf)); 1855 tty->minimum_to_wake = (minimum - (b - buf));
1848 1856
1849 if (!input_available_p(tty, 0)) { 1857 if (!input_available_p(tty, 0)) {
1850 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) { 1858 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
1851 retval = -EIO; 1859 retval = -EIO;
1852 break; 1860 break;
1853 } 1861 }
1854 if (tty_hung_up_p(file)) 1862 if (tty_hung_up_p(file))
1855 break; 1863 break;
1856 if (!timeout) 1864 if (!timeout)
1857 break; 1865 break;
1858 if (file->f_flags & O_NONBLOCK) { 1866 if (file->f_flags & O_NONBLOCK) {
1859 retval = -EAGAIN; 1867 retval = -EAGAIN;
1860 break; 1868 break;
1861 } 1869 }
1862 if (signal_pending(current)) { 1870 if (signal_pending(current)) {
1863 retval = -ERESTARTSYS; 1871 retval = -ERESTARTSYS;
1864 break; 1872 break;
1865 } 1873 }
1866 /* FIXME: does n_tty_set_room need locking ? */ 1874 /* FIXME: does n_tty_set_room need locking ? */
1867 n_tty_set_room(tty); 1875 n_tty_set_room(tty);
1868 timeout = schedule_timeout(timeout); 1876 timeout = schedule_timeout(timeout);
1869 continue; 1877 continue;
1870 } 1878 }
1871 __set_current_state(TASK_RUNNING); 1879 __set_current_state(TASK_RUNNING);
1872 1880
1873 /* Deal with packet mode. */ 1881 /* Deal with packet mode. */
1874 if (packet && b == buf) { 1882 if (packet && b == buf) {
1875 if (tty_put_user(tty, TIOCPKT_DATA, b++)) { 1883 if (tty_put_user(tty, TIOCPKT_DATA, b++)) {
1876 retval = -EFAULT; 1884 retval = -EFAULT;
1877 b--; 1885 b--;
1878 break; 1886 break;
1879 } 1887 }
1880 nr--; 1888 nr--;
1881 } 1889 }
1882 1890
1883 if (ldata->icanon && !L_EXTPROC(tty)) { 1891 if (ldata->icanon && !L_EXTPROC(tty)) {
1884 /* N.B. avoid overrun if nr == 0 */ 1892 /* N.B. avoid overrun if nr == 0 */
1885 raw_spin_lock_irqsave(&ldata->read_lock, flags); 1893 raw_spin_lock_irqsave(&ldata->read_lock, flags);
1886 while (nr && ldata->read_cnt) { 1894 while (nr && ldata->read_cnt) {
1887 int eol; 1895 int eol;
1888 1896
1889 eol = test_and_clear_bit(ldata->read_tail, 1897 eol = test_and_clear_bit(ldata->read_tail,
1890 ldata->read_flags); 1898 ldata->read_flags);
1891 c = ldata->read_buf[ldata->read_tail]; 1899 c = ldata->read_buf[ldata->read_tail];
1892 ldata->read_tail = ((ldata->read_tail+1) & 1900 ldata->read_tail = ((ldata->read_tail+1) &
1893 (N_TTY_BUF_SIZE-1)); 1901 (N_TTY_BUF_SIZE-1));
1894 ldata->read_cnt--; 1902 ldata->read_cnt--;
1895 if (eol) { 1903 if (eol) {
1896 /* this test should be redundant: 1904 /* this test should be redundant:
1897 * we shouldn't be reading data if 1905 * we shouldn't be reading data if
1898 * canon_data is 0 1906 * canon_data is 0
1899 */ 1907 */
1900 if (--ldata->canon_data < 0) 1908 if (--ldata->canon_data < 0)
1901 ldata->canon_data = 0; 1909 ldata->canon_data = 0;
1902 } 1910 }
1903 raw_spin_unlock_irqrestore(&ldata->read_lock, flags); 1911 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
1904 1912
1905 if (!eol || (c != __DISABLED_CHAR)) { 1913 if (!eol || (c != __DISABLED_CHAR)) {
1906 if (tty_put_user(tty, c, b++)) { 1914 if (tty_put_user(tty, c, b++)) {
1907 retval = -EFAULT; 1915 retval = -EFAULT;
1908 b--; 1916 b--;
1909 raw_spin_lock_irqsave(&ldata->read_lock, flags); 1917 raw_spin_lock_irqsave(&ldata->read_lock, flags);
1910 break; 1918 break;
1911 } 1919 }
1912 nr--; 1920 nr--;
1913 } 1921 }
1914 if (eol) { 1922 if (eol) {
1915 tty_audit_push(tty); 1923 tty_audit_push(tty);
1916 raw_spin_lock_irqsave(&ldata->read_lock, flags); 1924 raw_spin_lock_irqsave(&ldata->read_lock, flags);
1917 break; 1925 break;
1918 } 1926 }
1919 raw_spin_lock_irqsave(&ldata->read_lock, flags); 1927 raw_spin_lock_irqsave(&ldata->read_lock, flags);
1920 } 1928 }
1921 raw_spin_unlock_irqrestore(&ldata->read_lock, flags); 1929 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
1922 if (retval) 1930 if (retval)
1923 break; 1931 break;
1924 } else { 1932 } else {
1925 int uncopied; 1933 int uncopied;
1926 /* The copy function takes the read lock and handles 1934 /* The copy function takes the read lock and handles
1927 locking internally for this case */ 1935 locking internally for this case */
1928 uncopied = copy_from_read_buf(tty, &b, &nr); 1936 uncopied = copy_from_read_buf(tty, &b, &nr);
1929 uncopied += copy_from_read_buf(tty, &b, &nr); 1937 uncopied += copy_from_read_buf(tty, &b, &nr);
1930 if (uncopied) { 1938 if (uncopied) {
1931 retval = -EFAULT; 1939 retval = -EFAULT;
1932 break; 1940 break;
1933 } 1941 }
1934 } 1942 }
1935 1943
1936 /* If there is enough space in the read buffer now, let the 1944 /* If there is enough space in the read buffer now, let the
1937 * low-level driver know. We use n_tty_chars_in_buffer() to 1945 * low-level driver know. We use n_tty_chars_in_buffer() to
1938 * check the buffer, as it now knows about canonical mode. 1946 * check the buffer, as it now knows about canonical mode.
1939 * Otherwise, if the driver is throttled and the line is 1947 * Otherwise, if the driver is throttled and the line is
1940 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode, 1948 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
1941 * we won't get any more characters. 1949 * we won't get any more characters.
1942 */ 1950 */
1943 while (1) { 1951 while (1) {
1944 tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE); 1952 tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE);
1945 if (n_tty_chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE) 1953 if (n_tty_chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE)
1946 break; 1954 break;
1947 if (!tty->count) 1955 if (!tty->count)
1948 break; 1956 break;
1949 n_tty_set_room(tty); 1957 n_tty_set_room(tty);
1950 if (!tty_unthrottle_safe(tty)) 1958 if (!tty_unthrottle_safe(tty))
1951 break; 1959 break;
1952 } 1960 }
1953 __tty_set_flow_change(tty, 0); 1961 __tty_set_flow_change(tty, 0);
1954 1962
1955 if (b - buf >= minimum) 1963 if (b - buf >= minimum)
1956 break; 1964 break;
1957 if (time) 1965 if (time)
1958 timeout = time; 1966 timeout = time;
1959 } 1967 }
1960 mutex_unlock(&ldata->atomic_read_lock); 1968 mutex_unlock(&ldata->atomic_read_lock);
1961 remove_wait_queue(&tty->read_wait, &wait); 1969 remove_wait_queue(&tty->read_wait, &wait);
1962 1970
1963 if (!waitqueue_active(&tty->read_wait)) 1971 if (!waitqueue_active(&tty->read_wait))
1964 tty->minimum_to_wake = minimum; 1972 tty->minimum_to_wake = minimum;
1965 1973
1966 __set_current_state(TASK_RUNNING); 1974 __set_current_state(TASK_RUNNING);
1967 size = b - buf; 1975 size = b - buf;
1968 if (size) { 1976 if (size) {
1969 retval = size; 1977 retval = size;
1970 if (nr) 1978 if (nr)
1971 clear_bit(TTY_PUSH, &tty->flags); 1979 clear_bit(TTY_PUSH, &tty->flags);
1972 } else if (test_and_clear_bit(TTY_PUSH, &tty->flags)) 1980 } else if (test_and_clear_bit(TTY_PUSH, &tty->flags))
1973 goto do_it_again; 1981 goto do_it_again;
1974 1982
1975 n_tty_set_room(tty); 1983 n_tty_set_room(tty);
1976 return retval; 1984 return retval;
1977 } 1985 }
1978 1986
1979 /** 1987 /**
1980 * n_tty_write - write function for tty 1988 * n_tty_write - write function for tty
1981 * @tty: tty device 1989 * @tty: tty device
1982 * @file: file object 1990 * @file: file object
1983 * @buf: userspace buffer pointer 1991 * @buf: userspace buffer pointer
1984 * @nr: size of I/O 1992 * @nr: size of I/O
1985 * 1993 *
1986 * Write function of the terminal device. This is serialized with 1994 * Write function of the terminal device. This is serialized with
1987 * respect to other write callers but not to termios changes, reads 1995 * respect to other write callers but not to termios changes, reads
1988 * and other such events. Since the receive code will echo characters, 1996 * and other such events. Since the receive code will echo characters,
1989 * thus calling driver write methods, the output_lock is used in 1997 * thus calling driver write methods, the output_lock is used in
1990 * the output processing functions called here as well as in the 1998 * the output processing functions called here as well as in the
1991 * echo processing function to protect the column state and space 1999 * echo processing function to protect the column state and space
1992 * left in the buffer. 2000 * left in the buffer.
1993 * 2001 *
1994 * This code must be sure never to sleep through a hangup. 2002 * This code must be sure never to sleep through a hangup.
1995 * 2003 *
1996 * Locking: output_lock to protect column state and space left 2004 * Locking: output_lock to protect column state and space left
1997 * (note that the process_output*() functions take this 2005 * (note that the process_output*() functions take this
1998 * lock themselves) 2006 * lock themselves)
1999 */ 2007 */
2000 2008
2001 static ssize_t n_tty_write(struct tty_struct *tty, struct file *file, 2009 static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
2002 const unsigned char *buf, size_t nr) 2010 const unsigned char *buf, size_t nr)
2003 { 2011 {
2004 const unsigned char *b = buf; 2012 const unsigned char *b = buf;
2005 DECLARE_WAITQUEUE(wait, current); 2013 DECLARE_WAITQUEUE(wait, current);
2006 int c; 2014 int c;
2007 ssize_t retval = 0; 2015 ssize_t retval = 0;
2008 2016
2009 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */ 2017 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
2010 if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) { 2018 if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) {
2011 retval = tty_check_change(tty); 2019 retval = tty_check_change(tty);
2012 if (retval) 2020 if (retval)
2013 return retval; 2021 return retval;
2014 } 2022 }
2015 2023
2016 /* Write out any echoed characters that are still pending */ 2024 /* Write out any echoed characters that are still pending */
2017 process_echoes(tty); 2025 process_echoes(tty);
2018 2026
2019 add_wait_queue(&tty->write_wait, &wait); 2027 add_wait_queue(&tty->write_wait, &wait);
2020 while (1) { 2028 while (1) {
2021 set_current_state(TASK_INTERRUPTIBLE); 2029 set_current_state(TASK_INTERRUPTIBLE);
2022 if (signal_pending(current)) { 2030 if (signal_pending(current)) {
2023 retval = -ERESTARTSYS; 2031 retval = -ERESTARTSYS;
2024 break; 2032 break;
2025 } 2033 }
2026 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) { 2034 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
2027 retval = -EIO; 2035 retval = -EIO;
2028 break; 2036 break;
2029 } 2037 }
2030 if (O_OPOST(tty) && !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) { 2038 if (O_OPOST(tty) && !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
2031 while (nr > 0) { 2039 while (nr > 0) {
2032 ssize_t num = process_output_block(tty, b, nr); 2040 ssize_t num = process_output_block(tty, b, nr);
2033 if (num < 0) { 2041 if (num < 0) {
2034 if (num == -EAGAIN) 2042 if (num == -EAGAIN)
2035 break; 2043 break;
2036 retval = num; 2044 retval = num;
2037 goto break_out; 2045 goto break_out;
2038 } 2046 }
2039 b += num; 2047 b += num;
2040 nr -= num; 2048 nr -= num;
2041 if (nr == 0) 2049 if (nr == 0)
2042 break; 2050 break;
2043 c = *b; 2051 c = *b;
2044 if (process_output(c, tty) < 0) 2052 if (process_output(c, tty) < 0)
2045 break; 2053 break;
2046 b++; nr--; 2054 b++; nr--;
2047 } 2055 }
2048 if (tty->ops->flush_chars) 2056 if (tty->ops->flush_chars)
2049 tty->ops->flush_chars(tty); 2057 tty->ops->flush_chars(tty);
2050 } else { 2058 } else {
2051 while (nr > 0) { 2059 while (nr > 0) {
2052 c = tty->ops->write(tty, b, nr); 2060 c = tty->ops->write(tty, b, nr);
2053 if (c < 0) { 2061 if (c < 0) {
2054 retval = c; 2062 retval = c;
2055 goto break_out; 2063 goto break_out;
2056 } 2064 }
2057 if (!c) 2065 if (!c)
2058 break; 2066 break;
2059 b += c; 2067 b += c;
2060 nr -= c; 2068 nr -= c;
2061 } 2069 }
2062 } 2070 }
2063 if (!nr) 2071 if (!nr)
2064 break; 2072 break;
2065 if (file->f_flags & O_NONBLOCK) { 2073 if (file->f_flags & O_NONBLOCK) {
2066 retval = -EAGAIN; 2074 retval = -EAGAIN;
2067 break; 2075 break;
2068 } 2076 }
2069 schedule(); 2077 schedule();
2070 } 2078 }
2071 break_out: 2079 break_out:
2072 __set_current_state(TASK_RUNNING); 2080 __set_current_state(TASK_RUNNING);
2073 remove_wait_queue(&tty->write_wait, &wait); 2081 remove_wait_queue(&tty->write_wait, &wait);
2074 if (b - buf != nr && tty->fasync) 2082 if (b - buf != nr && tty->fasync)
2075 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); 2083 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
2076 return (b - buf) ? b - buf : retval; 2084 return (b - buf) ? b - buf : retval;
2077 } 2085 }
2078 2086
2079 /** 2087 /**
2080 * n_tty_poll - poll method for N_TTY 2088 * n_tty_poll - poll method for N_TTY
2081 * @tty: terminal device 2089 * @tty: terminal device
2082 * @file: file accessing it 2090 * @file: file accessing it
2083 * @wait: poll table 2091 * @wait: poll table
2084 * 2092 *
2085 * Called when the line discipline is asked to poll() for data or 2093 * Called when the line discipline is asked to poll() for data or
2086 * for special events. This code is not serialized with respect to 2094 * for special events. This code is not serialized with respect to
2087 * other events save open/close. 2095 * other events save open/close.
2088 * 2096 *
2089 * This code must be sure never to sleep through a hangup. 2097 * This code must be sure never to sleep through a hangup.
2090 * Called without the kernel lock held - fine 2098 * Called without the kernel lock held - fine
2091 */ 2099 */
2092 2100
2093 static unsigned int n_tty_poll(struct tty_struct *tty, struct file *file, 2101 static unsigned int n_tty_poll(struct tty_struct *tty, struct file *file,
2094 poll_table *wait) 2102 poll_table *wait)
2095 { 2103 {
2096 unsigned int mask = 0; 2104 unsigned int mask = 0;
2097 2105
2098 poll_wait(file, &tty->read_wait, wait); 2106 poll_wait(file, &tty->read_wait, wait);
2099 poll_wait(file, &tty->write_wait, wait); 2107 poll_wait(file, &tty->write_wait, wait);
2100 if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty))) 2108 if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty)))
2101 mask |= POLLIN | POLLRDNORM; 2109 mask |= POLLIN | POLLRDNORM;
2102 if (tty->packet && tty->link->ctrl_status) 2110 if (tty->packet && tty->link->ctrl_status)
2103 mask |= POLLPRI | POLLIN | POLLRDNORM; 2111 mask |= POLLPRI | POLLIN | POLLRDNORM;
2104 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) 2112 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
2105 mask |= POLLHUP; 2113 mask |= POLLHUP;
2106 if (tty_hung_up_p(file)) 2114 if (tty_hung_up_p(file))
2107 mask |= POLLHUP; 2115 mask |= POLLHUP;
2108 if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) { 2116 if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) {
2109 if (MIN_CHAR(tty) && !TIME_CHAR(tty)) 2117 if (MIN_CHAR(tty) && !TIME_CHAR(tty))
2110 tty->minimum_to_wake = MIN_CHAR(tty); 2118 tty->minimum_to_wake = MIN_CHAR(tty);
2111 else 2119 else
2112 tty->minimum_to_wake = 1; 2120 tty->minimum_to_wake = 1;
2113 } 2121 }
2114 if (tty->ops->write && !tty_is_writelocked(tty) && 2122 if (tty->ops->write && !tty_is_writelocked(tty) &&
2115 tty_chars_in_buffer(tty) < WAKEUP_CHARS && 2123 tty_chars_in_buffer(tty) < WAKEUP_CHARS &&
2116 tty_write_room(tty) > 0) 2124 tty_write_room(tty) > 0)
2117 mask |= POLLOUT | POLLWRNORM; 2125 mask |= POLLOUT | POLLWRNORM;
2118 return mask; 2126 return mask;
2119 } 2127 }
2120 2128
2121 static unsigned long inq_canon(struct n_tty_data *ldata) 2129 static unsigned long inq_canon(struct n_tty_data *ldata)
2122 { 2130 {
2123 int nr, head, tail; 2131 int nr, head, tail;
2124 2132
2125 if (!ldata->canon_data) 2133 if (!ldata->canon_data)
2126 return 0; 2134 return 0;
2127 head = ldata->canon_head; 2135 head = ldata->canon_head;
2128 tail = ldata->read_tail; 2136 tail = ldata->read_tail;
2129 nr = (head - tail) & (N_TTY_BUF_SIZE-1); 2137 nr = (head - tail) & (N_TTY_BUF_SIZE-1);
2130 /* Skip EOF-chars.. */ 2138 /* Skip EOF-chars.. */
2131 while (head != tail) { 2139 while (head != tail) {
2132 if (test_bit(tail, ldata->read_flags) && 2140 if (test_bit(tail, ldata->read_flags) &&
2133 ldata->read_buf[tail] == __DISABLED_CHAR) 2141 ldata->read_buf[tail] == __DISABLED_CHAR)
2134 nr--; 2142 nr--;
2135 tail = (tail+1) & (N_TTY_BUF_SIZE-1); 2143 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
2136 } 2144 }
2137 return nr; 2145 return nr;
2138 } 2146 }
2139 2147
2140 static int n_tty_ioctl(struct tty_struct *tty, struct file *file, 2148 static int n_tty_ioctl(struct tty_struct *tty, struct file *file,
2141 unsigned int cmd, unsigned long arg) 2149 unsigned int cmd, unsigned long arg)
2142 { 2150 {
2143 struct n_tty_data *ldata = tty->disc_data; 2151 struct n_tty_data *ldata = tty->disc_data;
2144 int retval; 2152 int retval;
2145 2153
2146 switch (cmd) { 2154 switch (cmd) {
2147 case TIOCOUTQ: 2155 case TIOCOUTQ:
2148 return put_user(tty_chars_in_buffer(tty), (int __user *) arg); 2156 return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
2149 case TIOCINQ: 2157 case TIOCINQ:
2150 /* FIXME: Locking */ 2158 /* FIXME: Locking */
2151 retval = ldata->read_cnt; 2159 retval = ldata->read_cnt;
2152 if (L_ICANON(tty)) 2160 if (L_ICANON(tty))
2153 retval = inq_canon(ldata); 2161 retval = inq_canon(ldata);
2154 return put_user(retval, (unsigned int __user *) arg); 2162 return put_user(retval, (unsigned int __user *) arg);
2155 default: 2163 default:
2156 return n_tty_ioctl_helper(tty, file, cmd, arg); 2164 return n_tty_ioctl_helper(tty, file, cmd, arg);
2157 } 2165 }
2158 } 2166 }
2159 2167
2160 struct tty_ldisc_ops tty_ldisc_N_TTY = { 2168 struct tty_ldisc_ops tty_ldisc_N_TTY = {
2161 .magic = TTY_LDISC_MAGIC, 2169 .magic = TTY_LDISC_MAGIC,
2162 .name = "n_tty", 2170 .name = "n_tty",
2163 .open = n_tty_open, 2171 .open = n_tty_open,
2164 .close = n_tty_close, 2172 .close = n_tty_close,
2165 .flush_buffer = n_tty_flush_buffer, 2173 .flush_buffer = n_tty_flush_buffer,
2166 .chars_in_buffer = n_tty_chars_in_buffer, 2174 .chars_in_buffer = n_tty_chars_in_buffer,
2167 .read = n_tty_read, 2175 .read = n_tty_read,
2168 .write = n_tty_write, 2176 .write = n_tty_write,
2169 .ioctl = n_tty_ioctl, 2177 .ioctl = n_tty_ioctl,
2170 .set_termios = n_tty_set_termios, 2178 .set_termios = n_tty_set_termios,
2171 .poll = n_tty_poll, 2179 .poll = n_tty_poll,
2172 .receive_buf = n_tty_receive_buf, 2180 .receive_buf = n_tty_receive_buf,
2173 .write_wakeup = n_tty_write_wakeup 2181 .write_wakeup = n_tty_write_wakeup
2174 }; 2182 };
2175 2183
2176 /** 2184 /**
2177 * n_tty_inherit_ops - inherit N_TTY methods 2185 * n_tty_inherit_ops - inherit N_TTY methods
2178 * @ops: struct tty_ldisc_ops where to save N_TTY methods 2186 * @ops: struct tty_ldisc_ops where to save N_TTY methods
2179 * 2187 *
2180 * Enables a 'subclass' line discipline to 'inherit' N_TTY 2188 * Enables a 'subclass' line discipline to 'inherit' N_TTY
2181 * methods. 2189 * methods.
2182 */ 2190 */
2183 2191
2184 void n_tty_inherit_ops(struct tty_ldisc_ops *ops) 2192 void n_tty_inherit_ops(struct tty_ldisc_ops *ops)
2185 { 2193 {
2186 *ops = tty_ldisc_N_TTY; 2194 *ops = tty_ldisc_N_TTY;
2187 ops->owner = NULL; 2195 ops->owner = NULL;
2188 ops->refcount = ops->flags = 0; 2196 ops->refcount = ops->flags = 0;
2189 } 2197 }
2190 EXPORT_SYMBOL_GPL(n_tty_inherit_ops); 2198 EXPORT_SYMBOL_GPL(n_tty_inherit_ops);
2191 2199
drivers/tty/tty_ldisc.c
1 #include <linux/types.h> 1 #include <linux/types.h>
2 #include <linux/errno.h> 2 #include <linux/errno.h>
3 #include <linux/kmod.h> 3 #include <linux/kmod.h>
4 #include <linux/sched.h> 4 #include <linux/sched.h>
5 #include <linux/interrupt.h> 5 #include <linux/interrupt.h>
6 #include <linux/tty.h> 6 #include <linux/tty.h>
7 #include <linux/tty_driver.h> 7 #include <linux/tty_driver.h>
8 #include <linux/file.h> 8 #include <linux/file.h>
9 #include <linux/mm.h> 9 #include <linux/mm.h>
10 #include <linux/string.h> 10 #include <linux/string.h>
11 #include <linux/slab.h> 11 #include <linux/slab.h>
12 #include <linux/poll.h> 12 #include <linux/poll.h>
13 #include <linux/proc_fs.h> 13 #include <linux/proc_fs.h>
14 #include <linux/init.h> 14 #include <linux/init.h>
15 #include <linux/module.h> 15 #include <linux/module.h>
16 #include <linux/device.h> 16 #include <linux/device.h>
17 #include <linux/wait.h> 17 #include <linux/wait.h>
18 #include <linux/bitops.h> 18 #include <linux/bitops.h>
19 #include <linux/seq_file.h> 19 #include <linux/seq_file.h>
20 #include <linux/uaccess.h> 20 #include <linux/uaccess.h>
21 #include <linux/ratelimit.h> 21 #include <linux/ratelimit.h>
22 22
23 /* 23 /*
24 * This guards the refcounted line discipline lists. The lock 24 * This guards the refcounted line discipline lists. The lock
25 * must be taken with irqs off because there are hangup path 25 * must be taken with irqs off because there are hangup path
26 * callers who will do ldisc lookups and cannot sleep. 26 * callers who will do ldisc lookups and cannot sleep.
27 */ 27 */
28 28
29 static DEFINE_RAW_SPINLOCK(tty_ldisc_lock); 29 static DEFINE_RAW_SPINLOCK(tty_ldisc_lock);
30 static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_wait); 30 static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_wait);
31 /* Line disc dispatch table */ 31 /* Line disc dispatch table */
32 static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS]; 32 static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS];
33 33
34 static inline struct tty_ldisc *get_ldisc(struct tty_ldisc *ld) 34 static inline struct tty_ldisc *get_ldisc(struct tty_ldisc *ld)
35 { 35 {
36 if (ld) 36 if (ld)
37 atomic_inc(&ld->users); 37 atomic_inc(&ld->users);
38 return ld; 38 return ld;
39 } 39 }
40 40
41 static void put_ldisc(struct tty_ldisc *ld) 41 static void put_ldisc(struct tty_ldisc *ld)
42 { 42 {
43 unsigned long flags; 43 unsigned long flags;
44 44
45 if (WARN_ON_ONCE(!ld)) 45 if (WARN_ON_ONCE(!ld))
46 return; 46 return;
47 47
48 /* 48 /*
49 * If this is the last user, free the ldisc, and 49 * If this is the last user, free the ldisc, and
50 * release the ldisc ops. 50 * release the ldisc ops.
51 * 51 *
52 * We really want an "atomic_dec_and_raw_lock_irqsave()", 52 * We really want an "atomic_dec_and_raw_lock_irqsave()",
53 * but we don't have it, so this does it by hand. 53 * but we don't have it, so this does it by hand.
54 */ 54 */
55 raw_spin_lock_irqsave(&tty_ldisc_lock, flags); 55 raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
56 if (atomic_dec_and_test(&ld->users)) { 56 if (atomic_dec_and_test(&ld->users)) {
57 struct tty_ldisc_ops *ldo = ld->ops; 57 struct tty_ldisc_ops *ldo = ld->ops;
58 58
59 ldo->refcount--; 59 ldo->refcount--;
60 module_put(ldo->owner); 60 module_put(ldo->owner);
61 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags); 61 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
62 62
63 kfree(ld); 63 kfree(ld);
64 return; 64 return;
65 } 65 }
66 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags); 66 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
67 67
68 if (waitqueue_active(&ld->wq_idle)) 68 if (waitqueue_active(&ld->wq_idle))
69 wake_up(&ld->wq_idle); 69 wake_up(&ld->wq_idle);
70 } 70 }
71 71
72 /** 72 /**
73 * tty_register_ldisc - install a line discipline 73 * tty_register_ldisc - install a line discipline
74 * @disc: ldisc number 74 * @disc: ldisc number
75 * @new_ldisc: pointer to the ldisc object 75 * @new_ldisc: pointer to the ldisc object
76 * 76 *
77 * Installs a new line discipline into the kernel. The discipline 77 * Installs a new line discipline into the kernel. The discipline
78 * is set up as unreferenced and then made available to the kernel 78 * is set up as unreferenced and then made available to the kernel
79 * from this point onwards. 79 * from this point onwards.
80 * 80 *
81 * Locking: 81 * Locking:
82 * takes tty_ldisc_lock to guard against ldisc races 82 * takes tty_ldisc_lock to guard against ldisc races
83 */ 83 */
84 84
85 int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc) 85 int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc)
86 { 86 {
87 unsigned long flags; 87 unsigned long flags;
88 int ret = 0; 88 int ret = 0;
89 89
90 if (disc < N_TTY || disc >= NR_LDISCS) 90 if (disc < N_TTY || disc >= NR_LDISCS)
91 return -EINVAL; 91 return -EINVAL;
92 92
93 raw_spin_lock_irqsave(&tty_ldisc_lock, flags); 93 raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
94 tty_ldiscs[disc] = new_ldisc; 94 tty_ldiscs[disc] = new_ldisc;
95 new_ldisc->num = disc; 95 new_ldisc->num = disc;
96 new_ldisc->refcount = 0; 96 new_ldisc->refcount = 0;
97 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags); 97 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
98 98
99 return ret; 99 return ret;
100 } 100 }
101 EXPORT_SYMBOL(tty_register_ldisc); 101 EXPORT_SYMBOL(tty_register_ldisc);
102 102
103 /** 103 /**
104 * tty_unregister_ldisc - unload a line discipline 104 * tty_unregister_ldisc - unload a line discipline
105 * @disc: ldisc number 105 * @disc: ldisc number
106 * @new_ldisc: pointer to the ldisc object 106 * @new_ldisc: pointer to the ldisc object
107 * 107 *
108 * Remove a line discipline from the kernel providing it is not 108 * Remove a line discipline from the kernel providing it is not
109 * currently in use. 109 * currently in use.
110 * 110 *
111 * Locking: 111 * Locking:
112 * takes tty_ldisc_lock to guard against ldisc races 112 * takes tty_ldisc_lock to guard against ldisc races
113 */ 113 */
114 114
115 int tty_unregister_ldisc(int disc) 115 int tty_unregister_ldisc(int disc)
116 { 116 {
117 unsigned long flags; 117 unsigned long flags;
118 int ret = 0; 118 int ret = 0;
119 119
120 if (disc < N_TTY || disc >= NR_LDISCS) 120 if (disc < N_TTY || disc >= NR_LDISCS)
121 return -EINVAL; 121 return -EINVAL;
122 122
123 raw_spin_lock_irqsave(&tty_ldisc_lock, flags); 123 raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
124 if (tty_ldiscs[disc]->refcount) 124 if (tty_ldiscs[disc]->refcount)
125 ret = -EBUSY; 125 ret = -EBUSY;
126 else 126 else
127 tty_ldiscs[disc] = NULL; 127 tty_ldiscs[disc] = NULL;
128 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags); 128 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
129 129
130 return ret; 130 return ret;
131 } 131 }
132 EXPORT_SYMBOL(tty_unregister_ldisc); 132 EXPORT_SYMBOL(tty_unregister_ldisc);
133 133
134 static struct tty_ldisc_ops *get_ldops(int disc) 134 static struct tty_ldisc_ops *get_ldops(int disc)
135 { 135 {
136 unsigned long flags; 136 unsigned long flags;
137 struct tty_ldisc_ops *ldops, *ret; 137 struct tty_ldisc_ops *ldops, *ret;
138 138
139 raw_spin_lock_irqsave(&tty_ldisc_lock, flags); 139 raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
140 ret = ERR_PTR(-EINVAL); 140 ret = ERR_PTR(-EINVAL);
141 ldops = tty_ldiscs[disc]; 141 ldops = tty_ldiscs[disc];
142 if (ldops) { 142 if (ldops) {
143 ret = ERR_PTR(-EAGAIN); 143 ret = ERR_PTR(-EAGAIN);
144 if (try_module_get(ldops->owner)) { 144 if (try_module_get(ldops->owner)) {
145 ldops->refcount++; 145 ldops->refcount++;
146 ret = ldops; 146 ret = ldops;
147 } 147 }
148 } 148 }
149 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags); 149 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
150 return ret; 150 return ret;
151 } 151 }
152 152
153 static void put_ldops(struct tty_ldisc_ops *ldops) 153 static void put_ldops(struct tty_ldisc_ops *ldops)
154 { 154 {
155 unsigned long flags; 155 unsigned long flags;
156 156
157 raw_spin_lock_irqsave(&tty_ldisc_lock, flags); 157 raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
158 ldops->refcount--; 158 ldops->refcount--;
159 module_put(ldops->owner); 159 module_put(ldops->owner);
160 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags); 160 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
161 } 161 }
162 162
163 /** 163 /**
164 * tty_ldisc_get - take a reference to an ldisc 164 * tty_ldisc_get - take a reference to an ldisc
165 * @disc: ldisc number 165 * @disc: ldisc number
166 * 166 *
167 * Takes a reference to a line discipline. Deals with refcounts and 167 * Takes a reference to a line discipline. Deals with refcounts and
168 * module locking counts. Returns NULL if the discipline is not available. 168 * module locking counts. Returns NULL if the discipline is not available.
169 * Returns a pointer to the discipline and bumps the ref count if it is 169 * Returns a pointer to the discipline and bumps the ref count if it is
170 * available 170 * available
171 * 171 *
172 * Locking: 172 * Locking:
173 * takes tty_ldisc_lock to guard against ldisc races 173 * takes tty_ldisc_lock to guard against ldisc races
174 */ 174 */
175 175
176 static struct tty_ldisc *tty_ldisc_get(int disc) 176 static struct tty_ldisc *tty_ldisc_get(int disc)
177 { 177 {
178 struct tty_ldisc *ld; 178 struct tty_ldisc *ld;
179 struct tty_ldisc_ops *ldops; 179 struct tty_ldisc_ops *ldops;
180 180
181 if (disc < N_TTY || disc >= NR_LDISCS) 181 if (disc < N_TTY || disc >= NR_LDISCS)
182 return ERR_PTR(-EINVAL); 182 return ERR_PTR(-EINVAL);
183 183
184 /* 184 /*
185 * Get the ldisc ops - we may need to request them to be loaded 185 * Get the ldisc ops - we may need to request them to be loaded
186 * dynamically and try again. 186 * dynamically and try again.
187 */ 187 */
188 ldops = get_ldops(disc); 188 ldops = get_ldops(disc);
189 if (IS_ERR(ldops)) { 189 if (IS_ERR(ldops)) {
190 request_module("tty-ldisc-%d", disc); 190 request_module("tty-ldisc-%d", disc);
191 ldops = get_ldops(disc); 191 ldops = get_ldops(disc);
192 if (IS_ERR(ldops)) 192 if (IS_ERR(ldops))
193 return ERR_CAST(ldops); 193 return ERR_CAST(ldops);
194 } 194 }
195 195
196 ld = kmalloc(sizeof(struct tty_ldisc), GFP_KERNEL); 196 ld = kmalloc(sizeof(struct tty_ldisc), GFP_KERNEL);
197 if (ld == NULL) { 197 if (ld == NULL) {
198 put_ldops(ldops); 198 put_ldops(ldops);
199 return ERR_PTR(-ENOMEM); 199 return ERR_PTR(-ENOMEM);
200 } 200 }
201 201
202 ld->ops = ldops; 202 ld->ops = ldops;
203 atomic_set(&ld->users, 1); 203 atomic_set(&ld->users, 1);
204 init_waitqueue_head(&ld->wq_idle); 204 init_waitqueue_head(&ld->wq_idle);
205 205
206 return ld; 206 return ld;
207 } 207 }
208 208
209 static void *tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos) 209 static void *tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos)
210 { 210 {
211 return (*pos < NR_LDISCS) ? pos : NULL; 211 return (*pos < NR_LDISCS) ? pos : NULL;
212 } 212 }
213 213
214 static void *tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos) 214 static void *tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos)
215 { 215 {
216 (*pos)++; 216 (*pos)++;
217 return (*pos < NR_LDISCS) ? pos : NULL; 217 return (*pos < NR_LDISCS) ? pos : NULL;
218 } 218 }
219 219
220 static void tty_ldiscs_seq_stop(struct seq_file *m, void *v) 220 static void tty_ldiscs_seq_stop(struct seq_file *m, void *v)
221 { 221 {
222 } 222 }
223 223
224 static int tty_ldiscs_seq_show(struct seq_file *m, void *v) 224 static int tty_ldiscs_seq_show(struct seq_file *m, void *v)
225 { 225 {
226 int i = *(loff_t *)v; 226 int i = *(loff_t *)v;
227 struct tty_ldisc_ops *ldops; 227 struct tty_ldisc_ops *ldops;
228 228
229 ldops = get_ldops(i); 229 ldops = get_ldops(i);
230 if (IS_ERR(ldops)) 230 if (IS_ERR(ldops))
231 return 0; 231 return 0;
232 seq_printf(m, "%-10s %2d\n", ldops->name ? ldops->name : "???", i); 232 seq_printf(m, "%-10s %2d\n", ldops->name ? ldops->name : "???", i);
233 put_ldops(ldops); 233 put_ldops(ldops);
234 return 0; 234 return 0;
235 } 235 }
236 236
237 static const struct seq_operations tty_ldiscs_seq_ops = { 237 static const struct seq_operations tty_ldiscs_seq_ops = {
238 .start = tty_ldiscs_seq_start, 238 .start = tty_ldiscs_seq_start,
239 .next = tty_ldiscs_seq_next, 239 .next = tty_ldiscs_seq_next,
240 .stop = tty_ldiscs_seq_stop, 240 .stop = tty_ldiscs_seq_stop,
241 .show = tty_ldiscs_seq_show, 241 .show = tty_ldiscs_seq_show,
242 }; 242 };
243 243
244 static int proc_tty_ldiscs_open(struct inode *inode, struct file *file) 244 static int proc_tty_ldiscs_open(struct inode *inode, struct file *file)
245 { 245 {
246 return seq_open(file, &tty_ldiscs_seq_ops); 246 return seq_open(file, &tty_ldiscs_seq_ops);
247 } 247 }
248 248
249 const struct file_operations tty_ldiscs_proc_fops = { 249 const struct file_operations tty_ldiscs_proc_fops = {
250 .owner = THIS_MODULE, 250 .owner = THIS_MODULE,
251 .open = proc_tty_ldiscs_open, 251 .open = proc_tty_ldiscs_open,
252 .read = seq_read, 252 .read = seq_read,
253 .llseek = seq_lseek, 253 .llseek = seq_lseek,
254 .release = seq_release, 254 .release = seq_release,
255 }; 255 };
256 256
257 /** 257 /**
258 * tty_ldisc_assign - set ldisc on a tty 258 * tty_ldisc_assign - set ldisc on a tty
259 * @tty: tty to assign 259 * @tty: tty to assign
260 * @ld: line discipline 260 * @ld: line discipline
261 * 261 *
262 * Install an instance of a line discipline into a tty structure. The 262 * Install an instance of a line discipline into a tty structure. The
263 * ldisc must have a reference count above zero to ensure it remains. 263 * ldisc must have a reference count above zero to ensure it remains.
264 * The tty instance refcount starts at zero. 264 * The tty instance refcount starts at zero.
265 * 265 *
266 * Locking: 266 * Locking:
267 * Caller must hold references 267 * Caller must hold references
268 */ 268 */
269 269
270 static void tty_ldisc_assign(struct tty_struct *tty, struct tty_ldisc *ld) 270 static void tty_ldisc_assign(struct tty_struct *tty, struct tty_ldisc *ld)
271 { 271 {
272 tty->ldisc = ld; 272 tty->ldisc = ld;
273 } 273 }
274 274
275 /** 275 /**
276 * tty_ldisc_try - internal helper 276 * tty_ldisc_try - internal helper
277 * @tty: the tty 277 * @tty: the tty
278 * 278 *
279 * Make a single attempt to grab and bump the refcount on 279 * Make a single attempt to grab and bump the refcount on
280 * the tty ldisc. Return 0 on failure or 1 on success. This is 280 * the tty ldisc. Return 0 on failure or 1 on success. This is
281 * used to implement both the waiting and non waiting versions 281 * used to implement both the waiting and non waiting versions
282 * of tty_ldisc_ref 282 * of tty_ldisc_ref
283 * 283 *
284 * Locking: takes tty_ldisc_lock 284 * Locking: takes tty_ldisc_lock
285 */ 285 */
286 286
287 static struct tty_ldisc *tty_ldisc_try(struct tty_struct *tty) 287 static struct tty_ldisc *tty_ldisc_try(struct tty_struct *tty)
288 { 288 {
289 unsigned long flags; 289 unsigned long flags;
290 struct tty_ldisc *ld; 290 struct tty_ldisc *ld;
291 291
292 raw_spin_lock_irqsave(&tty_ldisc_lock, flags); 292 raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
293 ld = NULL; 293 ld = NULL;
294 if (test_bit(TTY_LDISC, &tty->flags)) 294 if (test_bit(TTY_LDISC, &tty->flags))
295 ld = get_ldisc(tty->ldisc); 295 ld = get_ldisc(tty->ldisc);
296 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags); 296 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
297 return ld; 297 return ld;
298 } 298 }
299 299
300 /** 300 /**
301 * tty_ldisc_ref_wait - wait for the tty ldisc 301 * tty_ldisc_ref_wait - wait for the tty ldisc
302 * @tty: tty device 302 * @tty: tty device
303 * 303 *
304 * Dereference the line discipline for the terminal and take a 304 * Dereference the line discipline for the terminal and take a
305 * reference to it. If the line discipline is in flux then 305 * reference to it. If the line discipline is in flux then
306 * wait patiently until it changes. 306 * wait patiently until it changes.
307 * 307 *
308 * Note: Must not be called from an IRQ/timer context. The caller 308 * Note: Must not be called from an IRQ/timer context. The caller
309 * must also be careful not to hold other locks that will deadlock 309 * must also be careful not to hold other locks that will deadlock
310 * against a discipline change, such as an existing ldisc reference 310 * against a discipline change, such as an existing ldisc reference
311 * (which we check for) 311 * (which we check for)
312 * 312 *
313 * Locking: call functions take tty_ldisc_lock 313 * Locking: call functions take tty_ldisc_lock
314 */ 314 */
315 315
316 struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty) 316 struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
317 { 317 {
318 struct tty_ldisc *ld; 318 struct tty_ldisc *ld;
319 319
320 /* wait_event is a macro */ 320 /* wait_event is a macro */
321 wait_event(tty_ldisc_wait, (ld = tty_ldisc_try(tty)) != NULL); 321 wait_event(tty_ldisc_wait, (ld = tty_ldisc_try(tty)) != NULL);
322 return ld; 322 return ld;
323 } 323 }
324 EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait); 324 EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
325 325
326 /** 326 /**
327 * tty_ldisc_ref - get the tty ldisc 327 * tty_ldisc_ref - get the tty ldisc
328 * @tty: tty device 328 * @tty: tty device
329 * 329 *
330 * Dereference the line discipline for the terminal and take a 330 * Dereference the line discipline for the terminal and take a
331 * reference to it. If the line discipline is in flux then 331 * reference to it. If the line discipline is in flux then
332 * return NULL. Can be called from IRQ and timer functions. 332 * return NULL. Can be called from IRQ and timer functions.
333 * 333 *
334 * Locking: called functions take tty_ldisc_lock 334 * Locking: called functions take tty_ldisc_lock
335 */ 335 */
336 336
337 struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty) 337 struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
338 { 338 {
339 return tty_ldisc_try(tty); 339 return tty_ldisc_try(tty);
340 } 340 }
341 EXPORT_SYMBOL_GPL(tty_ldisc_ref); 341 EXPORT_SYMBOL_GPL(tty_ldisc_ref);
342 342
343 /** 343 /**
344 * tty_ldisc_deref - free a tty ldisc reference 344 * tty_ldisc_deref - free a tty ldisc reference
345 * @ld: reference to free up 345 * @ld: reference to free up
346 * 346 *
347 * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May 347 * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May
348 * be called in IRQ context. 348 * be called in IRQ context.
349 * 349 *
350 * Locking: takes tty_ldisc_lock 350 * Locking: takes tty_ldisc_lock
351 */ 351 */
352 352
353 void tty_ldisc_deref(struct tty_ldisc *ld) 353 void tty_ldisc_deref(struct tty_ldisc *ld)
354 { 354 {
355 put_ldisc(ld); 355 put_ldisc(ld);
356 } 356 }
357 EXPORT_SYMBOL_GPL(tty_ldisc_deref); 357 EXPORT_SYMBOL_GPL(tty_ldisc_deref);
358 358
359 static inline void tty_ldisc_put(struct tty_ldisc *ld) 359 static inline void tty_ldisc_put(struct tty_ldisc *ld)
360 { 360 {
361 put_ldisc(ld); 361 put_ldisc(ld);
362 } 362 }
363 363
364 /** 364 /**
365 * tty_ldisc_enable - allow ldisc use 365 * tty_ldisc_enable - allow ldisc use
366 * @tty: terminal to activate ldisc on 366 * @tty: terminal to activate ldisc on
367 * 367 *
368 * Set the TTY_LDISC flag when the line discipline can be called 368 * Set the TTY_LDISC flag when the line discipline can be called
369 * again. Do necessary wakeups for existing sleepers. Clear the LDISC 369 * again. Do necessary wakeups for existing sleepers. Clear the LDISC
370 * changing flag to indicate any ldisc change is now over. 370 * changing flag to indicate any ldisc change is now over.
371 * 371 *
372 * Note: nobody should set the TTY_LDISC bit except via this function. 372 * Note: nobody should set the TTY_LDISC bit except via this function.
373 * Clearing directly is allowed. 373 * Clearing directly is allowed.
374 */ 374 */
375 375
376 void tty_ldisc_enable(struct tty_struct *tty) 376 void tty_ldisc_enable(struct tty_struct *tty)
377 { 377 {
378 clear_bit(TTY_LDISC_HALTED, &tty->flags);
378 set_bit(TTY_LDISC, &tty->flags); 379 set_bit(TTY_LDISC, &tty->flags);
379 clear_bit(TTY_LDISC_CHANGING, &tty->flags); 380 clear_bit(TTY_LDISC_CHANGING, &tty->flags);
380 wake_up(&tty_ldisc_wait); 381 wake_up(&tty_ldisc_wait);
381 } 382 }
382 383
383 /** 384 /**
384 * tty_ldisc_flush - flush line discipline queue 385 * tty_ldisc_flush - flush line discipline queue
385 * @tty: tty 386 * @tty: tty
386 * 387 *
387 * Flush the line discipline queue (if any) for this tty. If there 388 * Flush the line discipline queue (if any) for this tty. If there
388 * is no line discipline active this is a no-op. 389 * is no line discipline active this is a no-op.
389 */ 390 */
390 391
391 void tty_ldisc_flush(struct tty_struct *tty) 392 void tty_ldisc_flush(struct tty_struct *tty)
392 { 393 {
393 struct tty_ldisc *ld = tty_ldisc_ref(tty); 394 struct tty_ldisc *ld = tty_ldisc_ref(tty);
394 if (ld) { 395 if (ld) {
395 if (ld->ops->flush_buffer) 396 if (ld->ops->flush_buffer)
396 ld->ops->flush_buffer(tty); 397 ld->ops->flush_buffer(tty);
397 tty_ldisc_deref(ld); 398 tty_ldisc_deref(ld);
398 } 399 }
399 tty_buffer_flush(tty); 400 tty_buffer_flush(tty);
400 } 401 }
401 EXPORT_SYMBOL_GPL(tty_ldisc_flush); 402 EXPORT_SYMBOL_GPL(tty_ldisc_flush);
402 403
403 /** 404 /**
404 * tty_set_termios_ldisc - set ldisc field 405 * tty_set_termios_ldisc - set ldisc field
405 * @tty: tty structure 406 * @tty: tty structure
406 * @num: line discipline number 407 * @num: line discipline number
407 * 408 *
408 * This is probably overkill for real world processors but 409 * This is probably overkill for real world processors but
409 * they are not on hot paths so a little discipline won't do 410 * they are not on hot paths so a little discipline won't do
410 * any harm. 411 * any harm.
411 * 412 *
412 * Locking: takes termios_mutex 413 * Locking: takes termios_mutex
413 */ 414 */
414 415
415 static void tty_set_termios_ldisc(struct tty_struct *tty, int num) 416 static void tty_set_termios_ldisc(struct tty_struct *tty, int num)
416 { 417 {
417 mutex_lock(&tty->termios_mutex); 418 mutex_lock(&tty->termios_mutex);
418 tty->termios.c_line = num; 419 tty->termios.c_line = num;
419 mutex_unlock(&tty->termios_mutex); 420 mutex_unlock(&tty->termios_mutex);
420 } 421 }
421 422
422 /** 423 /**
423 * tty_ldisc_open - open a line discipline 424 * tty_ldisc_open - open a line discipline
424 * @tty: tty we are opening the ldisc on 425 * @tty: tty we are opening the ldisc on
425 * @ld: discipline to open 426 * @ld: discipline to open
426 * 427 *
427 * A helper opening method. Also a convenient debugging and check 428 * A helper opening method. Also a convenient debugging and check
428 * point. 429 * point.
429 * 430 *
430 * Locking: always called with BTM already held. 431 * Locking: always called with BTM already held.
431 */ 432 */
432 433
433 static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld) 434 static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld)
434 { 435 {
435 WARN_ON(test_and_set_bit(TTY_LDISC_OPEN, &tty->flags)); 436 WARN_ON(test_and_set_bit(TTY_LDISC_OPEN, &tty->flags));
436 if (ld->ops->open) { 437 if (ld->ops->open) {
437 int ret; 438 int ret;
438 /* BTM here locks versus a hangup event */ 439 /* BTM here locks versus a hangup event */
439 ret = ld->ops->open(tty); 440 ret = ld->ops->open(tty);
440 if (ret) 441 if (ret)
441 clear_bit(TTY_LDISC_OPEN, &tty->flags); 442 clear_bit(TTY_LDISC_OPEN, &tty->flags);
442 return ret; 443 return ret;
443 } 444 }
444 return 0; 445 return 0;
445 } 446 }
446 447
447 /** 448 /**
448 * tty_ldisc_close - close a line discipline 449 * tty_ldisc_close - close a line discipline
449 * @tty: tty we are opening the ldisc on 450 * @tty: tty we are opening the ldisc on
450 * @ld: discipline to close 451 * @ld: discipline to close
451 * 452 *
452 * A helper close method. Also a convenient debugging and check 453 * A helper close method. Also a convenient debugging and check
453 * point. 454 * point.
454 */ 455 */
455 456
456 static void tty_ldisc_close(struct tty_struct *tty, struct tty_ldisc *ld) 457 static void tty_ldisc_close(struct tty_struct *tty, struct tty_ldisc *ld)
457 { 458 {
458 WARN_ON(!test_bit(TTY_LDISC_OPEN, &tty->flags)); 459 WARN_ON(!test_bit(TTY_LDISC_OPEN, &tty->flags));
459 clear_bit(TTY_LDISC_OPEN, &tty->flags); 460 clear_bit(TTY_LDISC_OPEN, &tty->flags);
460 if (ld->ops->close) 461 if (ld->ops->close)
461 ld->ops->close(tty); 462 ld->ops->close(tty);
462 } 463 }
463 464
464 /** 465 /**
465 * tty_ldisc_restore - helper for tty ldisc change 466 * tty_ldisc_restore - helper for tty ldisc change
466 * @tty: tty to recover 467 * @tty: tty to recover
467 * @old: previous ldisc 468 * @old: previous ldisc
468 * 469 *
469 * Restore the previous line discipline or N_TTY when a line discipline 470 * Restore the previous line discipline or N_TTY when a line discipline
470 * change fails due to an open error 471 * change fails due to an open error
471 */ 472 */
472 473
473 static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old) 474 static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old)
474 { 475 {
475 char buf[64]; 476 char buf[64];
476 struct tty_ldisc *new_ldisc; 477 struct tty_ldisc *new_ldisc;
477 int r; 478 int r;
478 479
479 /* There is an outstanding reference here so this is safe */ 480 /* There is an outstanding reference here so this is safe */
480 old = tty_ldisc_get(old->ops->num); 481 old = tty_ldisc_get(old->ops->num);
481 WARN_ON(IS_ERR(old)); 482 WARN_ON(IS_ERR(old));
482 tty_ldisc_assign(tty, old); 483 tty_ldisc_assign(tty, old);
483 tty_set_termios_ldisc(tty, old->ops->num); 484 tty_set_termios_ldisc(tty, old->ops->num);
484 if (tty_ldisc_open(tty, old) < 0) { 485 if (tty_ldisc_open(tty, old) < 0) {
485 tty_ldisc_put(old); 486 tty_ldisc_put(old);
486 /* This driver is always present */ 487 /* This driver is always present */
487 new_ldisc = tty_ldisc_get(N_TTY); 488 new_ldisc = tty_ldisc_get(N_TTY);
488 if (IS_ERR(new_ldisc)) 489 if (IS_ERR(new_ldisc))
489 panic("n_tty: get"); 490 panic("n_tty: get");
490 tty_ldisc_assign(tty, new_ldisc); 491 tty_ldisc_assign(tty, new_ldisc);
491 tty_set_termios_ldisc(tty, N_TTY); 492 tty_set_termios_ldisc(tty, N_TTY);
492 r = tty_ldisc_open(tty, new_ldisc); 493 r = tty_ldisc_open(tty, new_ldisc);
493 if (r < 0) 494 if (r < 0)
494 panic("Couldn't open N_TTY ldisc for " 495 panic("Couldn't open N_TTY ldisc for "
495 "%s --- error %d.", 496 "%s --- error %d.",
496 tty_name(tty, buf), r); 497 tty_name(tty, buf), r);
497 } 498 }
498 } 499 }
499 500
500 /** 501 /**
501 * tty_ldisc_halt - shut down the line discipline 502 * tty_ldisc_halt - shut down the line discipline
502 * @tty: tty device 503 * @tty: tty device
503 * 504 *
504 * Shut down the line discipline and work queue for this tty device. 505 * Shut down the line discipline and work queue for this tty device.
505 * The TTY_LDISC flag being cleared ensures no further references can 506 * The TTY_LDISC flag being cleared ensures no further references can
506 * be obtained while the delayed work queue halt ensures that no more 507 * be obtained while the delayed work queue halt ensures that no more
507 * data is fed to the ldisc. 508 * data is fed to the ldisc.
508 * 509 *
509 * You need to do a 'flush_scheduled_work()' (outside the ldisc_mutex) 510 * You need to do a 'flush_scheduled_work()' (outside the ldisc_mutex)
510 * in order to make sure any currently executing ldisc work is also 511 * in order to make sure any currently executing ldisc work is also
511 * flushed. 512 * flushed.
512 */ 513 */
513 514
514 static int tty_ldisc_halt(struct tty_struct *tty) 515 static int tty_ldisc_halt(struct tty_struct *tty)
515 { 516 {
517 int scheduled;
516 clear_bit(TTY_LDISC, &tty->flags); 518 clear_bit(TTY_LDISC, &tty->flags);
517 return cancel_work_sync(&tty->port->buf.work); 519 scheduled = cancel_work_sync(&tty->port->buf.work);
520 set_bit(TTY_LDISC_HALTED, &tty->flags);
521 return scheduled;
518 } 522 }
519 523
520 /** 524 /**
521 * tty_ldisc_flush_works - flush all works of a tty 525 * tty_ldisc_flush_works - flush all works of a tty
522 * @tty: tty device to flush works for 526 * @tty: tty device to flush works for
523 * 527 *
524 * Sync flush all works belonging to @tty. 528 * Sync flush all works belonging to @tty.
525 */ 529 */
526 static void tty_ldisc_flush_works(struct tty_struct *tty) 530 static void tty_ldisc_flush_works(struct tty_struct *tty)
527 { 531 {
528 flush_work(&tty->hangup_work); 532 flush_work(&tty->hangup_work);
529 flush_work(&tty->SAK_work); 533 flush_work(&tty->SAK_work);
530 flush_work(&tty->port->buf.work); 534 flush_work(&tty->port->buf.work);
531 } 535 }
532 536
533 /** 537 /**
534 * tty_ldisc_wait_idle - wait for the ldisc to become idle 538 * tty_ldisc_wait_idle - wait for the ldisc to become idle
535 * @tty: tty to wait for 539 * @tty: tty to wait for
536 * @timeout: for how long to wait at most 540 * @timeout: for how long to wait at most
537 * 541 *
538 * Wait for the line discipline to become idle. The discipline must 542 * Wait for the line discipline to become idle. The discipline must
539 * have been halted for this to guarantee it remains idle. 543 * have been halted for this to guarantee it remains idle.
540 */ 544 */
541 static int tty_ldisc_wait_idle(struct tty_struct *tty, long timeout) 545 static int tty_ldisc_wait_idle(struct tty_struct *tty, long timeout)
542 { 546 {
543 long ret; 547 long ret;
544 ret = wait_event_timeout(tty->ldisc->wq_idle, 548 ret = wait_event_timeout(tty->ldisc->wq_idle,
545 atomic_read(&tty->ldisc->users) == 1, timeout); 549 atomic_read(&tty->ldisc->users) == 1, timeout);
546 return ret > 0 ? 0 : -EBUSY; 550 return ret > 0 ? 0 : -EBUSY;
547 } 551 }
548 552
549 /** 553 /**
550 * tty_set_ldisc - set line discipline 554 * tty_set_ldisc - set line discipline
551 * @tty: the terminal to set 555 * @tty: the terminal to set
552 * @ldisc: the line discipline 556 * @ldisc: the line discipline
553 * 557 *
554 * Set the discipline of a tty line. Must be called from a process 558 * Set the discipline of a tty line. Must be called from a process
555 * context. The ldisc change logic has to protect itself against any 559 * context. The ldisc change logic has to protect itself against any
556 * overlapping ldisc change (including on the other end of pty pairs), 560 * overlapping ldisc change (including on the other end of pty pairs),
557 * the close of one side of a tty/pty pair, and eventually hangup. 561 * the close of one side of a tty/pty pair, and eventually hangup.
558 * 562 *
559 * Locking: takes tty_ldisc_lock, termios_mutex 563 * Locking: takes tty_ldisc_lock, termios_mutex
560 */ 564 */
561 565
562 int tty_set_ldisc(struct tty_struct *tty, int ldisc) 566 int tty_set_ldisc(struct tty_struct *tty, int ldisc)
563 { 567 {
564 int retval; 568 int retval;
565 struct tty_ldisc *o_ldisc, *new_ldisc; 569 struct tty_ldisc *o_ldisc, *new_ldisc;
566 int work, o_work = 0; 570 int work, o_work = 0;
567 struct tty_struct *o_tty; 571 struct tty_struct *o_tty;
568 572
569 new_ldisc = tty_ldisc_get(ldisc); 573 new_ldisc = tty_ldisc_get(ldisc);
570 if (IS_ERR(new_ldisc)) 574 if (IS_ERR(new_ldisc))
571 return PTR_ERR(new_ldisc); 575 return PTR_ERR(new_ldisc);
572 576
573 tty_lock(tty); 577 tty_lock(tty);
574 /* 578 /*
575 * We need to look at the tty locking here for pty/tty pairs 579 * We need to look at the tty locking here for pty/tty pairs
576 * when both sides try to change in parallel. 580 * when both sides try to change in parallel.
577 */ 581 */
578 582
579 o_tty = tty->link; /* o_tty is the pty side or NULL */ 583 o_tty = tty->link; /* o_tty is the pty side or NULL */
580 584
581 585
582 /* 586 /*
583 * Check the no-op case 587 * Check the no-op case
584 */ 588 */
585 589
586 if (tty->ldisc->ops->num == ldisc) { 590 if (tty->ldisc->ops->num == ldisc) {
587 tty_unlock(tty); 591 tty_unlock(tty);
588 tty_ldisc_put(new_ldisc); 592 tty_ldisc_put(new_ldisc);
589 return 0; 593 return 0;
590 } 594 }
591 595
592 tty_unlock(tty); 596 tty_unlock(tty);
593 /* 597 /*
594 * Problem: What do we do if this blocks ? 598 * Problem: What do we do if this blocks ?
595 * We could deadlock here 599 * We could deadlock here
596 */ 600 */
597 601
598 tty_wait_until_sent(tty, 0); 602 tty_wait_until_sent(tty, 0);
599 603
600 tty_lock(tty); 604 tty_lock(tty);
601 mutex_lock(&tty->ldisc_mutex); 605 mutex_lock(&tty->ldisc_mutex);
602 606
603 /* 607 /*
604 * We could be midstream of another ldisc change which has 608 * We could be midstream of another ldisc change which has
605 * dropped the lock during processing. If so we need to wait. 609 * dropped the lock during processing. If so we need to wait.
606 */ 610 */
607 611
608 while (test_bit(TTY_LDISC_CHANGING, &tty->flags)) { 612 while (test_bit(TTY_LDISC_CHANGING, &tty->flags)) {
609 mutex_unlock(&tty->ldisc_mutex); 613 mutex_unlock(&tty->ldisc_mutex);
610 tty_unlock(tty); 614 tty_unlock(tty);
611 wait_event(tty_ldisc_wait, 615 wait_event(tty_ldisc_wait,
612 test_bit(TTY_LDISC_CHANGING, &tty->flags) == 0); 616 test_bit(TTY_LDISC_CHANGING, &tty->flags) == 0);
613 tty_lock(tty); 617 tty_lock(tty);
614 mutex_lock(&tty->ldisc_mutex); 618 mutex_lock(&tty->ldisc_mutex);
615 } 619 }
616 620
617 set_bit(TTY_LDISC_CHANGING, &tty->flags); 621 set_bit(TTY_LDISC_CHANGING, &tty->flags);
618 622
619 /* 623 /*
620 * No more input please, we are switching. The new ldisc 624 * No more input please, we are switching. The new ldisc
621 * will update this value in the ldisc open function 625 * will update this value in the ldisc open function
622 */ 626 */
623 627
624 tty->receive_room = 0; 628 tty->receive_room = 0;
625 629
626 o_ldisc = tty->ldisc; 630 o_ldisc = tty->ldisc;
627 631
628 tty_unlock(tty); 632 tty_unlock(tty);
629 /* 633 /*
630 * Make sure we don't change while someone holds a 634 * Make sure we don't change while someone holds a
631 * reference to the line discipline. The TTY_LDISC bit 635 * reference to the line discipline. The TTY_LDISC bit
632 * prevents anyone taking a reference once it is clear. 636 * prevents anyone taking a reference once it is clear.
633 * We need the lock to avoid racing reference takers. 637 * We need the lock to avoid racing reference takers.
634 * 638 *
635 * We must clear the TTY_LDISC bit here to avoid a livelock 639 * We must clear the TTY_LDISC bit here to avoid a livelock
636 * with a userspace app continually trying to use the tty in 640 * with a userspace app continually trying to use the tty in
637 * parallel to the change and re-referencing the tty. 641 * parallel to the change and re-referencing the tty.
638 */ 642 */
639 643
640 work = tty_ldisc_halt(tty); 644 work = tty_ldisc_halt(tty);
641 if (o_tty) 645 if (o_tty)
642 o_work = tty_ldisc_halt(o_tty); 646 o_work = tty_ldisc_halt(o_tty);
643 647
644 /* 648 /*
645 * Wait for ->hangup_work and ->buf.work handlers to terminate. 649 * Wait for ->hangup_work and ->buf.work handlers to terminate.
646 * We must drop the mutex here in case a hangup is also in process. 650 * We must drop the mutex here in case a hangup is also in process.
647 */ 651 */
648 652
649 mutex_unlock(&tty->ldisc_mutex); 653 mutex_unlock(&tty->ldisc_mutex);
650 654
651 tty_ldisc_flush_works(tty); 655 tty_ldisc_flush_works(tty);
652 656
653 retval = tty_ldisc_wait_idle(tty, 5 * HZ); 657 retval = tty_ldisc_wait_idle(tty, 5 * HZ);
654 658
655 tty_lock(tty); 659 tty_lock(tty);
656 mutex_lock(&tty->ldisc_mutex); 660 mutex_lock(&tty->ldisc_mutex);
657 661
658 /* handle wait idle failure locked */ 662 /* handle wait idle failure locked */
659 if (retval) { 663 if (retval) {
660 tty_ldisc_put(new_ldisc); 664 tty_ldisc_put(new_ldisc);
661 goto enable; 665 goto enable;
662 } 666 }
663 667
664 if (test_bit(TTY_HUPPING, &tty->flags)) { 668 if (test_bit(TTY_HUPPING, &tty->flags)) {
665 /* We were raced by the hangup method. It will have stomped 669 /* We were raced by the hangup method. It will have stomped
666 the ldisc data and closed the ldisc down */ 670 the ldisc data and closed the ldisc down */
667 clear_bit(TTY_LDISC_CHANGING, &tty->flags); 671 clear_bit(TTY_LDISC_CHANGING, &tty->flags);
668 mutex_unlock(&tty->ldisc_mutex); 672 mutex_unlock(&tty->ldisc_mutex);
669 tty_ldisc_put(new_ldisc); 673 tty_ldisc_put(new_ldisc);
670 tty_unlock(tty); 674 tty_unlock(tty);
671 return -EIO; 675 return -EIO;
672 } 676 }
673 677
674 /* Shutdown the current discipline. */ 678 /* Shutdown the current discipline. */
675 tty_ldisc_close(tty, o_ldisc); 679 tty_ldisc_close(tty, o_ldisc);
676 680
677 /* Now set up the new line discipline. */ 681 /* Now set up the new line discipline. */
678 tty_ldisc_assign(tty, new_ldisc); 682 tty_ldisc_assign(tty, new_ldisc);
679 tty_set_termios_ldisc(tty, ldisc); 683 tty_set_termios_ldisc(tty, ldisc);
680 684
681 retval = tty_ldisc_open(tty, new_ldisc); 685 retval = tty_ldisc_open(tty, new_ldisc);
682 if (retval < 0) { 686 if (retval < 0) {
683 /* Back to the old one or N_TTY if we can't */ 687 /* Back to the old one or N_TTY if we can't */
684 tty_ldisc_put(new_ldisc); 688 tty_ldisc_put(new_ldisc);
685 tty_ldisc_restore(tty, o_ldisc); 689 tty_ldisc_restore(tty, o_ldisc);
686 } 690 }
687 691
688 /* At this point we hold a reference to the new ldisc and a 692 /* At this point we hold a reference to the new ldisc and a
689 a reference to the old ldisc. If we ended up flipping back 693 a reference to the old ldisc. If we ended up flipping back
690 to the existing ldisc we have two references to it */ 694 to the existing ldisc we have two references to it */
691 695
692 if (tty->ldisc->ops->num != o_ldisc->ops->num && tty->ops->set_ldisc) 696 if (tty->ldisc->ops->num != o_ldisc->ops->num && tty->ops->set_ldisc)
693 tty->ops->set_ldisc(tty); 697 tty->ops->set_ldisc(tty);
694 698
695 tty_ldisc_put(o_ldisc); 699 tty_ldisc_put(o_ldisc);
696 700
697 enable: 701 enable:
698 /* 702 /*
699 * Allow ldisc referencing to occur again 703 * Allow ldisc referencing to occur again
700 */ 704 */
701 705
702 tty_ldisc_enable(tty); 706 tty_ldisc_enable(tty);
703 if (o_tty) 707 if (o_tty)
704 tty_ldisc_enable(o_tty); 708 tty_ldisc_enable(o_tty);
705 709
706 /* Restart the work queue in case no characters kick it off. Safe if 710 /* Restart the work queue in case no characters kick it off. Safe if
707 already running */ 711 already running */
708 if (work) 712 if (work)
709 schedule_work(&tty->port->buf.work); 713 schedule_work(&tty->port->buf.work);
710 if (o_work) 714 if (o_work)
711 schedule_work(&o_tty->port->buf.work); 715 schedule_work(&o_tty->port->buf.work);
712 mutex_unlock(&tty->ldisc_mutex); 716 mutex_unlock(&tty->ldisc_mutex);
713 tty_unlock(tty); 717 tty_unlock(tty);
714 return retval; 718 return retval;
715 } 719 }
716 720
717 /** 721 /**
718 * tty_reset_termios - reset terminal state 722 * tty_reset_termios - reset terminal state
719 * @tty: tty to reset 723 * @tty: tty to reset
720 * 724 *
721 * Restore a terminal to the driver default state. 725 * Restore a terminal to the driver default state.
722 */ 726 */
723 727
724 static void tty_reset_termios(struct tty_struct *tty) 728 static void tty_reset_termios(struct tty_struct *tty)
725 { 729 {
726 mutex_lock(&tty->termios_mutex); 730 mutex_lock(&tty->termios_mutex);
727 tty->termios = tty->driver->init_termios; 731 tty->termios = tty->driver->init_termios;
728 tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios); 732 tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios);
729 tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios); 733 tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios);
730 mutex_unlock(&tty->termios_mutex); 734 mutex_unlock(&tty->termios_mutex);
731 } 735 }
732 736
733 737
734 /** 738 /**
735 * tty_ldisc_reinit - reinitialise the tty ldisc 739 * tty_ldisc_reinit - reinitialise the tty ldisc
736 * @tty: tty to reinit 740 * @tty: tty to reinit
737 * @ldisc: line discipline to reinitialize 741 * @ldisc: line discipline to reinitialize
738 * 742 *
739 * Switch the tty to a line discipline and leave the ldisc 743 * Switch the tty to a line discipline and leave the ldisc
740 * state closed 744 * state closed
741 */ 745 */
742 746
743 static int tty_ldisc_reinit(struct tty_struct *tty, int ldisc) 747 static int tty_ldisc_reinit(struct tty_struct *tty, int ldisc)
744 { 748 {
745 struct tty_ldisc *ld = tty_ldisc_get(ldisc); 749 struct tty_ldisc *ld = tty_ldisc_get(ldisc);
746 750
747 if (IS_ERR(ld)) 751 if (IS_ERR(ld))
748 return -1; 752 return -1;
749 753
750 tty_ldisc_close(tty, tty->ldisc); 754 tty_ldisc_close(tty, tty->ldisc);
751 tty_ldisc_put(tty->ldisc); 755 tty_ldisc_put(tty->ldisc);
752 tty->ldisc = NULL; 756 tty->ldisc = NULL;
753 /* 757 /*
754 * Switch the line discipline back 758 * Switch the line discipline back
755 */ 759 */
756 tty_ldisc_assign(tty, ld); 760 tty_ldisc_assign(tty, ld);
757 tty_set_termios_ldisc(tty, ldisc); 761 tty_set_termios_ldisc(tty, ldisc);
758 762
759 return 0; 763 return 0;
760 } 764 }
761 765
762 /** 766 /**
763 * tty_ldisc_hangup - hangup ldisc reset 767 * tty_ldisc_hangup - hangup ldisc reset
764 * @tty: tty being hung up 768 * @tty: tty being hung up
765 * 769 *
766 * Some tty devices reset their termios when they receive a hangup 770 * Some tty devices reset their termios when they receive a hangup
767 * event. In that situation we must also switch back to N_TTY properly 771 * event. In that situation we must also switch back to N_TTY properly
768 * before we reset the termios data. 772 * before we reset the termios data.
769 * 773 *
770 * Locking: We can take the ldisc mutex as the rest of the code is 774 * Locking: We can take the ldisc mutex as the rest of the code is
771 * careful to allow for this. 775 * careful to allow for this.
772 * 776 *
773 * In the pty pair case this occurs in the close() path of the 777 * In the pty pair case this occurs in the close() path of the
774 * tty itself so we must be careful about locking rules. 778 * tty itself so we must be careful about locking rules.
775 */ 779 */
776 780
777 void tty_ldisc_hangup(struct tty_struct *tty) 781 void tty_ldisc_hangup(struct tty_struct *tty)
778 { 782 {
779 struct tty_ldisc *ld; 783 struct tty_ldisc *ld;
780 int reset = tty->driver->flags & TTY_DRIVER_RESET_TERMIOS; 784 int reset = tty->driver->flags & TTY_DRIVER_RESET_TERMIOS;
781 int err = 0; 785 int err = 0;
782 786
783 /* 787 /*
784 * FIXME! What are the locking issues here? This may me overdoing 788 * FIXME! What are the locking issues here? This may me overdoing
785 * things... This question is especially important now that we've 789 * things... This question is especially important now that we've
786 * removed the irqlock. 790 * removed the irqlock.
787 */ 791 */
788 ld = tty_ldisc_ref(tty); 792 ld = tty_ldisc_ref(tty);
789 if (ld != NULL) { 793 if (ld != NULL) {
790 /* We may have no line discipline at this point */ 794 /* We may have no line discipline at this point */
791 if (ld->ops->flush_buffer) 795 if (ld->ops->flush_buffer)
792 ld->ops->flush_buffer(tty); 796 ld->ops->flush_buffer(tty);
793 tty_driver_flush_buffer(tty); 797 tty_driver_flush_buffer(tty);
794 if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) && 798 if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
795 ld->ops->write_wakeup) 799 ld->ops->write_wakeup)
796 ld->ops->write_wakeup(tty); 800 ld->ops->write_wakeup(tty);
797 if (ld->ops->hangup) 801 if (ld->ops->hangup)
798 ld->ops->hangup(tty); 802 ld->ops->hangup(tty);
799 tty_ldisc_deref(ld); 803 tty_ldisc_deref(ld);
800 } 804 }
801 /* 805 /*
802 * FIXME: Once we trust the LDISC code better we can wait here for 806 * FIXME: Once we trust the LDISC code better we can wait here for
803 * ldisc completion and fix the driver call race 807 * ldisc completion and fix the driver call race
804 */ 808 */
805 wake_up_interruptible_poll(&tty->write_wait, POLLOUT); 809 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
806 wake_up_interruptible_poll(&tty->read_wait, POLLIN); 810 wake_up_interruptible_poll(&tty->read_wait, POLLIN);
807 /* 811 /*
808 * Shutdown the current line discipline, and reset it to 812 * Shutdown the current line discipline, and reset it to
809 * N_TTY if need be. 813 * N_TTY if need be.
810 * 814 *
811 * Avoid racing set_ldisc or tty_ldisc_release 815 * Avoid racing set_ldisc or tty_ldisc_release
812 */ 816 */
813 mutex_lock(&tty->ldisc_mutex); 817 mutex_lock(&tty->ldisc_mutex);
814 818
815 /* 819 /*
816 * this is like tty_ldisc_halt, but we need to give up 820 * this is like tty_ldisc_halt, but we need to give up
817 * the BTM before calling cancel_work_sync, which may 821 * the BTM before calling cancel_work_sync, which may
818 * need to wait for another function taking the BTM 822 * need to wait for another function taking the BTM
819 */ 823 */
820 clear_bit(TTY_LDISC, &tty->flags); 824 clear_bit(TTY_LDISC, &tty->flags);
821 tty_unlock(tty); 825 tty_unlock(tty);
822 cancel_work_sync(&tty->port->buf.work); 826 cancel_work_sync(&tty->port->buf.work);
827 set_bit(TTY_LDISC_HALTED, &tty->flags);
823 mutex_unlock(&tty->ldisc_mutex); 828 mutex_unlock(&tty->ldisc_mutex);
824 retry: 829 retry:
825 tty_lock(tty); 830 tty_lock(tty);
826 mutex_lock(&tty->ldisc_mutex); 831 mutex_lock(&tty->ldisc_mutex);
827 832
828 /* At this point we have a closed ldisc and we want to 833 /* At this point we have a closed ldisc and we want to
829 reopen it. We could defer this to the next open but 834 reopen it. We could defer this to the next open but
830 it means auditing a lot of other paths so this is 835 it means auditing a lot of other paths so this is
831 a FIXME */ 836 a FIXME */
832 if (tty->ldisc) { /* Not yet closed */ 837 if (tty->ldisc) { /* Not yet closed */
833 if (atomic_read(&tty->ldisc->users) != 1) { 838 if (atomic_read(&tty->ldisc->users) != 1) {
834 char cur_n[TASK_COMM_LEN], tty_n[64]; 839 char cur_n[TASK_COMM_LEN], tty_n[64];
835 long timeout = 3 * HZ; 840 long timeout = 3 * HZ;
836 tty_unlock(tty); 841 tty_unlock(tty);
837 842
838 while (tty_ldisc_wait_idle(tty, timeout) == -EBUSY) { 843 while (tty_ldisc_wait_idle(tty, timeout) == -EBUSY) {
839 timeout = MAX_SCHEDULE_TIMEOUT; 844 timeout = MAX_SCHEDULE_TIMEOUT;
840 printk_ratelimited(KERN_WARNING 845 printk_ratelimited(KERN_WARNING
841 "%s: waiting (%s) for %s took too long, but we keep waiting...\n", 846 "%s: waiting (%s) for %s took too long, but we keep waiting...\n",
842 __func__, get_task_comm(cur_n, current), 847 __func__, get_task_comm(cur_n, current),
843 tty_name(tty, tty_n)); 848 tty_name(tty, tty_n));
844 } 849 }
845 mutex_unlock(&tty->ldisc_mutex); 850 mutex_unlock(&tty->ldisc_mutex);
846 goto retry; 851 goto retry;
847 } 852 }
848 853
849 if (reset == 0) { 854 if (reset == 0) {
850 855
851 if (!tty_ldisc_reinit(tty, tty->termios.c_line)) 856 if (!tty_ldisc_reinit(tty, tty->termios.c_line))
852 err = tty_ldisc_open(tty, tty->ldisc); 857 err = tty_ldisc_open(tty, tty->ldisc);
853 else 858 else
854 err = 1; 859 err = 1;
855 } 860 }
856 /* If the re-open fails or we reset then go to N_TTY. The 861 /* If the re-open fails or we reset then go to N_TTY. The
857 N_TTY open cannot fail */ 862 N_TTY open cannot fail */
858 if (reset || err) { 863 if (reset || err) {
859 BUG_ON(tty_ldisc_reinit(tty, N_TTY)); 864 BUG_ON(tty_ldisc_reinit(tty, N_TTY));
860 WARN_ON(tty_ldisc_open(tty, tty->ldisc)); 865 WARN_ON(tty_ldisc_open(tty, tty->ldisc));
861 } 866 }
862 tty_ldisc_enable(tty); 867 tty_ldisc_enable(tty);
863 } 868 }
864 mutex_unlock(&tty->ldisc_mutex); 869 mutex_unlock(&tty->ldisc_mutex);
865 if (reset) 870 if (reset)
866 tty_reset_termios(tty); 871 tty_reset_termios(tty);
867 } 872 }
868 873
869 /** 874 /**
870 * tty_ldisc_setup - open line discipline 875 * tty_ldisc_setup - open line discipline
871 * @tty: tty being shut down 876 * @tty: tty being shut down
872 * @o_tty: pair tty for pty/tty pairs 877 * @o_tty: pair tty for pty/tty pairs
873 * 878 *
874 * Called during the initial open of a tty/pty pair in order to set up the 879 * Called during the initial open of a tty/pty pair in order to set up the
875 * line disciplines and bind them to the tty. This has no locking issues 880 * line disciplines and bind them to the tty. This has no locking issues
876 * as the device isn't yet active. 881 * as the device isn't yet active.
877 */ 882 */
878 883
879 int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty) 884 int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty)
880 { 885 {
881 struct tty_ldisc *ld = tty->ldisc; 886 struct tty_ldisc *ld = tty->ldisc;
882 int retval; 887 int retval;
883 888
884 retval = tty_ldisc_open(tty, ld); 889 retval = tty_ldisc_open(tty, ld);
885 if (retval) 890 if (retval)
886 return retval; 891 return retval;
887 892
888 if (o_tty) { 893 if (o_tty) {
889 retval = tty_ldisc_open(o_tty, o_tty->ldisc); 894 retval = tty_ldisc_open(o_tty, o_tty->ldisc);
890 if (retval) { 895 if (retval) {
891 tty_ldisc_close(tty, ld); 896 tty_ldisc_close(tty, ld);
892 return retval; 897 return retval;
893 } 898 }
894 tty_ldisc_enable(o_tty); 899 tty_ldisc_enable(o_tty);
895 } 900 }
896 tty_ldisc_enable(tty); 901 tty_ldisc_enable(tty);
897 return 0; 902 return 0;
898 } 903 }
899 904
900 static void tty_ldisc_kill(struct tty_struct *tty) 905 static void tty_ldisc_kill(struct tty_struct *tty)
901 { 906 {
902 /* There cannot be users from userspace now. But there still might be 907 /* There cannot be users from userspace now. But there still might be
903 * drivers holding a reference via tty_ldisc_ref. Do not steal them the 908 * drivers holding a reference via tty_ldisc_ref. Do not steal them the
904 * ldisc until they are done. */ 909 * ldisc until they are done. */
905 tty_ldisc_wait_idle(tty, MAX_SCHEDULE_TIMEOUT); 910 tty_ldisc_wait_idle(tty, MAX_SCHEDULE_TIMEOUT);
906 911
907 mutex_lock(&tty->ldisc_mutex); 912 mutex_lock(&tty->ldisc_mutex);
908 /* 913 /*
909 * Now kill off the ldisc 914 * Now kill off the ldisc
910 */ 915 */
911 tty_ldisc_close(tty, tty->ldisc); 916 tty_ldisc_close(tty, tty->ldisc);
912 tty_ldisc_put(tty->ldisc); 917 tty_ldisc_put(tty->ldisc);
913 /* Force an oops if we mess this up */ 918 /* Force an oops if we mess this up */
914 tty->ldisc = NULL; 919 tty->ldisc = NULL;
915 920
916 /* Ensure the next open requests the N_TTY ldisc */ 921 /* Ensure the next open requests the N_TTY ldisc */
917 tty_set_termios_ldisc(tty, N_TTY); 922 tty_set_termios_ldisc(tty, N_TTY);
918 mutex_unlock(&tty->ldisc_mutex); 923 mutex_unlock(&tty->ldisc_mutex);
919 } 924 }
920 925
921 /** 926 /**
922 * tty_ldisc_release - release line discipline 927 * tty_ldisc_release - release line discipline
923 * @tty: tty being shut down 928 * @tty: tty being shut down
924 * @o_tty: pair tty for pty/tty pairs 929 * @o_tty: pair tty for pty/tty pairs
925 * 930 *
926 * Called during the final close of a tty/pty pair in order to shut down 931 * Called during the final close of a tty/pty pair in order to shut down
927 * the line discpline layer. On exit the ldisc assigned is N_TTY and the 932 * the line discpline layer. On exit the ldisc assigned is N_TTY and the
928 * ldisc has not been opened. 933 * ldisc has not been opened.
929 */ 934 */
930 935
931 void tty_ldisc_release(struct tty_struct *tty, struct tty_struct *o_tty) 936 void tty_ldisc_release(struct tty_struct *tty, struct tty_struct *o_tty)
932 { 937 {
933 /* 938 /*
934 * Prevent flush_to_ldisc() from rescheduling the work for later. Then 939 * Prevent flush_to_ldisc() from rescheduling the work for later. Then
935 * kill any delayed work. As this is the final close it does not 940 * kill any delayed work. As this is the final close it does not
936 * race with the set_ldisc code path. 941 * race with the set_ldisc code path.
937 */ 942 */
938 943
939 tty_ldisc_halt(tty); 944 tty_ldisc_halt(tty);
940 if (o_tty) 945 if (o_tty)
941 tty_ldisc_halt(o_tty); 946 tty_ldisc_halt(o_tty);
942 947
943 tty_ldisc_flush_works(tty); 948 tty_ldisc_flush_works(tty);
944 if (o_tty) 949 if (o_tty)
945 tty_ldisc_flush_works(o_tty); 950 tty_ldisc_flush_works(o_tty);
946 951
947 tty_lock_pair(tty, o_tty); 952 tty_lock_pair(tty, o_tty);
948 /* This will need doing differently if we need to lock */ 953 /* This will need doing differently if we need to lock */
949 tty_ldisc_kill(tty); 954 tty_ldisc_kill(tty);
950 if (o_tty) 955 if (o_tty)
951 tty_ldisc_kill(o_tty); 956 tty_ldisc_kill(o_tty);
952 957
953 tty_unlock_pair(tty, o_tty); 958 tty_unlock_pair(tty, o_tty);
954 /* And the memory resources remaining (buffers, termios) will be 959 /* And the memory resources remaining (buffers, termios) will be
955 disposed of when the kref hits zero */ 960 disposed of when the kref hits zero */
956 } 961 }
957 962
958 /** 963 /**
959 * tty_ldisc_init - ldisc setup for new tty 964 * tty_ldisc_init - ldisc setup for new tty
960 * @tty: tty being allocated 965 * @tty: tty being allocated
961 * 966 *
962 * Set up the line discipline objects for a newly allocated tty. Note that 967 * Set up the line discipline objects for a newly allocated tty. Note that
963 * the tty structure is not completely set up when this call is made. 968 * the tty structure is not completely set up when this call is made.
964 */ 969 */
965 970
966 void tty_ldisc_init(struct tty_struct *tty) 971 void tty_ldisc_init(struct tty_struct *tty)
967 { 972 {
968 struct tty_ldisc *ld = tty_ldisc_get(N_TTY); 973 struct tty_ldisc *ld = tty_ldisc_get(N_TTY);
969 if (IS_ERR(ld)) 974 if (IS_ERR(ld))
970 panic("n_tty: init_tty"); 975 panic("n_tty: init_tty");
971 tty_ldisc_assign(tty, ld); 976 tty_ldisc_assign(tty, ld);
972 } 977 }
973 978
974 /** 979 /**
975 * tty_ldisc_init - ldisc cleanup for new tty 980 * tty_ldisc_init - ldisc cleanup for new tty
976 * @tty: tty that was allocated recently 981 * @tty: tty that was allocated recently
977 * 982 *
978 * The tty structure must not becompletely set up (tty_ldisc_setup) when 983 * The tty structure must not becompletely set up (tty_ldisc_setup) when
979 * this call is made. 984 * this call is made.
980 */ 985 */
981 void tty_ldisc_deinit(struct tty_struct *tty) 986 void tty_ldisc_deinit(struct tty_struct *tty)
982 { 987 {
983 put_ldisc(tty->ldisc); 988 put_ldisc(tty->ldisc);
984 tty_ldisc_assign(tty, NULL); 989 tty_ldisc_assign(tty, NULL);
985 } 990 }
986 991
987 void tty_ldisc_begin(void) 992 void tty_ldisc_begin(void)
988 { 993 {
989 /* Setup the default TTY line discipline. */ 994 /* Setup the default TTY line discipline. */
990 (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY); 995 (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
991 } 996 }
992 997
1 #ifndef _LINUX_TTY_H 1 #ifndef _LINUX_TTY_H
2 #define _LINUX_TTY_H 2 #define _LINUX_TTY_H
3 3
4 #include <linux/fs.h> 4 #include <linux/fs.h>
5 #include <linux/major.h> 5 #include <linux/major.h>
6 #include <linux/termios.h> 6 #include <linux/termios.h>
7 #include <linux/workqueue.h> 7 #include <linux/workqueue.h>
8 #include <linux/tty_driver.h> 8 #include <linux/tty_driver.h>
9 #include <linux/tty_ldisc.h> 9 #include <linux/tty_ldisc.h>
10 #include <linux/mutex.h> 10 #include <linux/mutex.h>
11 #include <linux/tty_flags.h> 11 #include <linux/tty_flags.h>
12 #include <uapi/linux/tty.h> 12 #include <uapi/linux/tty.h>
13 13
14 14
15 15
16 /* 16 /*
17 * (Note: the *_driver.minor_start values 1, 64, 128, 192 are 17 * (Note: the *_driver.minor_start values 1, 64, 128, 192 are
18 * hardcoded at present.) 18 * hardcoded at present.)
19 */ 19 */
20 #define NR_UNIX98_PTY_DEFAULT 4096 /* Default maximum for Unix98 ptys */ 20 #define NR_UNIX98_PTY_DEFAULT 4096 /* Default maximum for Unix98 ptys */
21 #define NR_UNIX98_PTY_RESERVE 1024 /* Default reserve for main devpts */ 21 #define NR_UNIX98_PTY_RESERVE 1024 /* Default reserve for main devpts */
22 #define NR_UNIX98_PTY_MAX (1 << MINORBITS) /* Absolute limit */ 22 #define NR_UNIX98_PTY_MAX (1 << MINORBITS) /* Absolute limit */
23 23
24 /* 24 /*
25 * This character is the same as _POSIX_VDISABLE: it cannot be used as 25 * This character is the same as _POSIX_VDISABLE: it cannot be used as
26 * a c_cc[] character, but indicates that a particular special character 26 * a c_cc[] character, but indicates that a particular special character
27 * isn't in use (eg VINTR has no character etc) 27 * isn't in use (eg VINTR has no character etc)
28 */ 28 */
29 #define __DISABLED_CHAR '\0' 29 #define __DISABLED_CHAR '\0'
30 30
31 struct tty_buffer { 31 struct tty_buffer {
32 struct tty_buffer *next; 32 struct tty_buffer *next;
33 char *char_buf_ptr; 33 char *char_buf_ptr;
34 unsigned char *flag_buf_ptr; 34 unsigned char *flag_buf_ptr;
35 int used; 35 int used;
36 int size; 36 int size;
37 int commit; 37 int commit;
38 int read; 38 int read;
39 /* Data points here */ 39 /* Data points here */
40 unsigned long data[0]; 40 unsigned long data[0];
41 }; 41 };
42 42
43 /* 43 /*
44 * We default to dicing tty buffer allocations to this many characters 44 * We default to dicing tty buffer allocations to this many characters
45 * in order to avoid multiple page allocations. We know the size of 45 * in order to avoid multiple page allocations. We know the size of
46 * tty_buffer itself but it must also be taken into account that the 46 * tty_buffer itself but it must also be taken into account that the
47 * the buffer is 256 byte aligned. See tty_buffer_find for the allocation 47 * the buffer is 256 byte aligned. See tty_buffer_find for the allocation
48 * logic this must match 48 * logic this must match
49 */ 49 */
50 50
51 #define TTY_BUFFER_PAGE (((PAGE_SIZE - sizeof(struct tty_buffer)) / 2) & ~0xFF) 51 #define TTY_BUFFER_PAGE (((PAGE_SIZE - sizeof(struct tty_buffer)) / 2) & ~0xFF)
52 52
53 53
54 struct tty_bufhead { 54 struct tty_bufhead {
55 struct work_struct work; 55 struct work_struct work;
56 spinlock_t lock; 56 spinlock_t lock;
57 struct tty_buffer *head; /* Queue head */ 57 struct tty_buffer *head; /* Queue head */
58 struct tty_buffer *tail; /* Active buffer */ 58 struct tty_buffer *tail; /* Active buffer */
59 struct tty_buffer *free; /* Free queue head */ 59 struct tty_buffer *free; /* Free queue head */
60 int memory_used; /* Buffer space used excluding 60 int memory_used; /* Buffer space used excluding
61 free queue */ 61 free queue */
62 }; 62 };
63 /* 63 /*
64 * When a break, frame error, or parity error happens, these codes are 64 * When a break, frame error, or parity error happens, these codes are
65 * stuffed into the flags buffer. 65 * stuffed into the flags buffer.
66 */ 66 */
67 #define TTY_NORMAL 0 67 #define TTY_NORMAL 0
68 #define TTY_BREAK 1 68 #define TTY_BREAK 1
69 #define TTY_FRAME 2 69 #define TTY_FRAME 2
70 #define TTY_PARITY 3 70 #define TTY_PARITY 3
71 #define TTY_OVERRUN 4 71 #define TTY_OVERRUN 4
72 72
73 #define INTR_CHAR(tty) ((tty)->termios.c_cc[VINTR]) 73 #define INTR_CHAR(tty) ((tty)->termios.c_cc[VINTR])
74 #define QUIT_CHAR(tty) ((tty)->termios.c_cc[VQUIT]) 74 #define QUIT_CHAR(tty) ((tty)->termios.c_cc[VQUIT])
75 #define ERASE_CHAR(tty) ((tty)->termios.c_cc[VERASE]) 75 #define ERASE_CHAR(tty) ((tty)->termios.c_cc[VERASE])
76 #define KILL_CHAR(tty) ((tty)->termios.c_cc[VKILL]) 76 #define KILL_CHAR(tty) ((tty)->termios.c_cc[VKILL])
77 #define EOF_CHAR(tty) ((tty)->termios.c_cc[VEOF]) 77 #define EOF_CHAR(tty) ((tty)->termios.c_cc[VEOF])
78 #define TIME_CHAR(tty) ((tty)->termios.c_cc[VTIME]) 78 #define TIME_CHAR(tty) ((tty)->termios.c_cc[VTIME])
79 #define MIN_CHAR(tty) ((tty)->termios.c_cc[VMIN]) 79 #define MIN_CHAR(tty) ((tty)->termios.c_cc[VMIN])
80 #define SWTC_CHAR(tty) ((tty)->termios.c_cc[VSWTC]) 80 #define SWTC_CHAR(tty) ((tty)->termios.c_cc[VSWTC])
81 #define START_CHAR(tty) ((tty)->termios.c_cc[VSTART]) 81 #define START_CHAR(tty) ((tty)->termios.c_cc[VSTART])
82 #define STOP_CHAR(tty) ((tty)->termios.c_cc[VSTOP]) 82 #define STOP_CHAR(tty) ((tty)->termios.c_cc[VSTOP])
83 #define SUSP_CHAR(tty) ((tty)->termios.c_cc[VSUSP]) 83 #define SUSP_CHAR(tty) ((tty)->termios.c_cc[VSUSP])
84 #define EOL_CHAR(tty) ((tty)->termios.c_cc[VEOL]) 84 #define EOL_CHAR(tty) ((tty)->termios.c_cc[VEOL])
85 #define REPRINT_CHAR(tty) ((tty)->termios.c_cc[VREPRINT]) 85 #define REPRINT_CHAR(tty) ((tty)->termios.c_cc[VREPRINT])
86 #define DISCARD_CHAR(tty) ((tty)->termios.c_cc[VDISCARD]) 86 #define DISCARD_CHAR(tty) ((tty)->termios.c_cc[VDISCARD])
87 #define WERASE_CHAR(tty) ((tty)->termios.c_cc[VWERASE]) 87 #define WERASE_CHAR(tty) ((tty)->termios.c_cc[VWERASE])
88 #define LNEXT_CHAR(tty) ((tty)->termios.c_cc[VLNEXT]) 88 #define LNEXT_CHAR(tty) ((tty)->termios.c_cc[VLNEXT])
89 #define EOL2_CHAR(tty) ((tty)->termios.c_cc[VEOL2]) 89 #define EOL2_CHAR(tty) ((tty)->termios.c_cc[VEOL2])
90 90
91 #define _I_FLAG(tty, f) ((tty)->termios.c_iflag & (f)) 91 #define _I_FLAG(tty, f) ((tty)->termios.c_iflag & (f))
92 #define _O_FLAG(tty, f) ((tty)->termios.c_oflag & (f)) 92 #define _O_FLAG(tty, f) ((tty)->termios.c_oflag & (f))
93 #define _C_FLAG(tty, f) ((tty)->termios.c_cflag & (f)) 93 #define _C_FLAG(tty, f) ((tty)->termios.c_cflag & (f))
94 #define _L_FLAG(tty, f) ((tty)->termios.c_lflag & (f)) 94 #define _L_FLAG(tty, f) ((tty)->termios.c_lflag & (f))
95 95
96 #define I_IGNBRK(tty) _I_FLAG((tty), IGNBRK) 96 #define I_IGNBRK(tty) _I_FLAG((tty), IGNBRK)
97 #define I_BRKINT(tty) _I_FLAG((tty), BRKINT) 97 #define I_BRKINT(tty) _I_FLAG((tty), BRKINT)
98 #define I_IGNPAR(tty) _I_FLAG((tty), IGNPAR) 98 #define I_IGNPAR(tty) _I_FLAG((tty), IGNPAR)
99 #define I_PARMRK(tty) _I_FLAG((tty), PARMRK) 99 #define I_PARMRK(tty) _I_FLAG((tty), PARMRK)
100 #define I_INPCK(tty) _I_FLAG((tty), INPCK) 100 #define I_INPCK(tty) _I_FLAG((tty), INPCK)
101 #define I_ISTRIP(tty) _I_FLAG((tty), ISTRIP) 101 #define I_ISTRIP(tty) _I_FLAG((tty), ISTRIP)
102 #define I_INLCR(tty) _I_FLAG((tty), INLCR) 102 #define I_INLCR(tty) _I_FLAG((tty), INLCR)
103 #define I_IGNCR(tty) _I_FLAG((tty), IGNCR) 103 #define I_IGNCR(tty) _I_FLAG((tty), IGNCR)
104 #define I_ICRNL(tty) _I_FLAG((tty), ICRNL) 104 #define I_ICRNL(tty) _I_FLAG((tty), ICRNL)
105 #define I_IUCLC(tty) _I_FLAG((tty), IUCLC) 105 #define I_IUCLC(tty) _I_FLAG((tty), IUCLC)
106 #define I_IXON(tty) _I_FLAG((tty), IXON) 106 #define I_IXON(tty) _I_FLAG((tty), IXON)
107 #define I_IXANY(tty) _I_FLAG((tty), IXANY) 107 #define I_IXANY(tty) _I_FLAG((tty), IXANY)
108 #define I_IXOFF(tty) _I_FLAG((tty), IXOFF) 108 #define I_IXOFF(tty) _I_FLAG((tty), IXOFF)
109 #define I_IMAXBEL(tty) _I_FLAG((tty), IMAXBEL) 109 #define I_IMAXBEL(tty) _I_FLAG((tty), IMAXBEL)
110 #define I_IUTF8(tty) _I_FLAG((tty), IUTF8) 110 #define I_IUTF8(tty) _I_FLAG((tty), IUTF8)
111 111
112 #define O_OPOST(tty) _O_FLAG((tty), OPOST) 112 #define O_OPOST(tty) _O_FLAG((tty), OPOST)
113 #define O_OLCUC(tty) _O_FLAG((tty), OLCUC) 113 #define O_OLCUC(tty) _O_FLAG((tty), OLCUC)
114 #define O_ONLCR(tty) _O_FLAG((tty), ONLCR) 114 #define O_ONLCR(tty) _O_FLAG((tty), ONLCR)
115 #define O_OCRNL(tty) _O_FLAG((tty), OCRNL) 115 #define O_OCRNL(tty) _O_FLAG((tty), OCRNL)
116 #define O_ONOCR(tty) _O_FLAG((tty), ONOCR) 116 #define O_ONOCR(tty) _O_FLAG((tty), ONOCR)
117 #define O_ONLRET(tty) _O_FLAG((tty), ONLRET) 117 #define O_ONLRET(tty) _O_FLAG((tty), ONLRET)
118 #define O_OFILL(tty) _O_FLAG((tty), OFILL) 118 #define O_OFILL(tty) _O_FLAG((tty), OFILL)
119 #define O_OFDEL(tty) _O_FLAG((tty), OFDEL) 119 #define O_OFDEL(tty) _O_FLAG((tty), OFDEL)
120 #define O_NLDLY(tty) _O_FLAG((tty), NLDLY) 120 #define O_NLDLY(tty) _O_FLAG((tty), NLDLY)
121 #define O_CRDLY(tty) _O_FLAG((tty), CRDLY) 121 #define O_CRDLY(tty) _O_FLAG((tty), CRDLY)
122 #define O_TABDLY(tty) _O_FLAG((tty), TABDLY) 122 #define O_TABDLY(tty) _O_FLAG((tty), TABDLY)
123 #define O_BSDLY(tty) _O_FLAG((tty), BSDLY) 123 #define O_BSDLY(tty) _O_FLAG((tty), BSDLY)
124 #define O_VTDLY(tty) _O_FLAG((tty), VTDLY) 124 #define O_VTDLY(tty) _O_FLAG((tty), VTDLY)
125 #define O_FFDLY(tty) _O_FLAG((tty), FFDLY) 125 #define O_FFDLY(tty) _O_FLAG((tty), FFDLY)
126 126
127 #define C_BAUD(tty) _C_FLAG((tty), CBAUD) 127 #define C_BAUD(tty) _C_FLAG((tty), CBAUD)
128 #define C_CSIZE(tty) _C_FLAG((tty), CSIZE) 128 #define C_CSIZE(tty) _C_FLAG((tty), CSIZE)
129 #define C_CSTOPB(tty) _C_FLAG((tty), CSTOPB) 129 #define C_CSTOPB(tty) _C_FLAG((tty), CSTOPB)
130 #define C_CREAD(tty) _C_FLAG((tty), CREAD) 130 #define C_CREAD(tty) _C_FLAG((tty), CREAD)
131 #define C_PARENB(tty) _C_FLAG((tty), PARENB) 131 #define C_PARENB(tty) _C_FLAG((tty), PARENB)
132 #define C_PARODD(tty) _C_FLAG((tty), PARODD) 132 #define C_PARODD(tty) _C_FLAG((tty), PARODD)
133 #define C_HUPCL(tty) _C_FLAG((tty), HUPCL) 133 #define C_HUPCL(tty) _C_FLAG((tty), HUPCL)
134 #define C_CLOCAL(tty) _C_FLAG((tty), CLOCAL) 134 #define C_CLOCAL(tty) _C_FLAG((tty), CLOCAL)
135 #define C_CIBAUD(tty) _C_FLAG((tty), CIBAUD) 135 #define C_CIBAUD(tty) _C_FLAG((tty), CIBAUD)
136 #define C_CRTSCTS(tty) _C_FLAG((tty), CRTSCTS) 136 #define C_CRTSCTS(tty) _C_FLAG((tty), CRTSCTS)
137 137
138 #define L_ISIG(tty) _L_FLAG((tty), ISIG) 138 #define L_ISIG(tty) _L_FLAG((tty), ISIG)
139 #define L_ICANON(tty) _L_FLAG((tty), ICANON) 139 #define L_ICANON(tty) _L_FLAG((tty), ICANON)
140 #define L_XCASE(tty) _L_FLAG((tty), XCASE) 140 #define L_XCASE(tty) _L_FLAG((tty), XCASE)
141 #define L_ECHO(tty) _L_FLAG((tty), ECHO) 141 #define L_ECHO(tty) _L_FLAG((tty), ECHO)
142 #define L_ECHOE(tty) _L_FLAG((tty), ECHOE) 142 #define L_ECHOE(tty) _L_FLAG((tty), ECHOE)
143 #define L_ECHOK(tty) _L_FLAG((tty), ECHOK) 143 #define L_ECHOK(tty) _L_FLAG((tty), ECHOK)
144 #define L_ECHONL(tty) _L_FLAG((tty), ECHONL) 144 #define L_ECHONL(tty) _L_FLAG((tty), ECHONL)
145 #define L_NOFLSH(tty) _L_FLAG((tty), NOFLSH) 145 #define L_NOFLSH(tty) _L_FLAG((tty), NOFLSH)
146 #define L_TOSTOP(tty) _L_FLAG((tty), TOSTOP) 146 #define L_TOSTOP(tty) _L_FLAG((tty), TOSTOP)
147 #define L_ECHOCTL(tty) _L_FLAG((tty), ECHOCTL) 147 #define L_ECHOCTL(tty) _L_FLAG((tty), ECHOCTL)
148 #define L_ECHOPRT(tty) _L_FLAG((tty), ECHOPRT) 148 #define L_ECHOPRT(tty) _L_FLAG((tty), ECHOPRT)
149 #define L_ECHOKE(tty) _L_FLAG((tty), ECHOKE) 149 #define L_ECHOKE(tty) _L_FLAG((tty), ECHOKE)
150 #define L_FLUSHO(tty) _L_FLAG((tty), FLUSHO) 150 #define L_FLUSHO(tty) _L_FLAG((tty), FLUSHO)
151 #define L_PENDIN(tty) _L_FLAG((tty), PENDIN) 151 #define L_PENDIN(tty) _L_FLAG((tty), PENDIN)
152 #define L_IEXTEN(tty) _L_FLAG((tty), IEXTEN) 152 #define L_IEXTEN(tty) _L_FLAG((tty), IEXTEN)
153 #define L_EXTPROC(tty) _L_FLAG((tty), EXTPROC) 153 #define L_EXTPROC(tty) _L_FLAG((tty), EXTPROC)
154 154
155 struct device; 155 struct device;
156 struct signal_struct; 156 struct signal_struct;
157 157
158 /* 158 /*
159 * Port level information. Each device keeps its own port level information 159 * Port level information. Each device keeps its own port level information
160 * so provide a common structure for those ports wanting to use common support 160 * so provide a common structure for those ports wanting to use common support
161 * routines. 161 * routines.
162 * 162 *
163 * The tty port has a different lifetime to the tty so must be kept apart. 163 * The tty port has a different lifetime to the tty so must be kept apart.
164 * In addition be careful as tty -> port mappings are valid for the life 164 * In addition be careful as tty -> port mappings are valid for the life
165 * of the tty object but in many cases port -> tty mappings are valid only 165 * of the tty object but in many cases port -> tty mappings are valid only
166 * until a hangup so don't use the wrong path. 166 * until a hangup so don't use the wrong path.
167 */ 167 */
168 168
169 struct tty_port; 169 struct tty_port;
170 170
171 struct tty_port_operations { 171 struct tty_port_operations {
172 /* Return 1 if the carrier is raised */ 172 /* Return 1 if the carrier is raised */
173 int (*carrier_raised)(struct tty_port *port); 173 int (*carrier_raised)(struct tty_port *port);
174 /* Control the DTR line */ 174 /* Control the DTR line */
175 void (*dtr_rts)(struct tty_port *port, int raise); 175 void (*dtr_rts)(struct tty_port *port, int raise);
176 /* Called when the last close completes or a hangup finishes 176 /* Called when the last close completes or a hangup finishes
177 IFF the port was initialized. Do not use to free resources. Called 177 IFF the port was initialized. Do not use to free resources. Called
178 under the port mutex to serialize against activate/shutdowns */ 178 under the port mutex to serialize against activate/shutdowns */
179 void (*shutdown)(struct tty_port *port); 179 void (*shutdown)(struct tty_port *port);
180 void (*drop)(struct tty_port *port); 180 void (*drop)(struct tty_port *port);
181 /* Called under the port mutex from tty_port_open, serialized using 181 /* Called under the port mutex from tty_port_open, serialized using
182 the port mutex */ 182 the port mutex */
183 /* FIXME: long term getting the tty argument *out* of this would be 183 /* FIXME: long term getting the tty argument *out* of this would be
184 good for consoles */ 184 good for consoles */
185 int (*activate)(struct tty_port *port, struct tty_struct *tty); 185 int (*activate)(struct tty_port *port, struct tty_struct *tty);
186 /* Called on the final put of a port */ 186 /* Called on the final put of a port */
187 void (*destruct)(struct tty_port *port); 187 void (*destruct)(struct tty_port *port);
188 }; 188 };
189 189
190 struct tty_port { 190 struct tty_port {
191 struct tty_bufhead buf; /* Locked internally */ 191 struct tty_bufhead buf; /* Locked internally */
192 struct tty_struct *tty; /* Back pointer */ 192 struct tty_struct *tty; /* Back pointer */
193 struct tty_struct *itty; /* internal back ptr */ 193 struct tty_struct *itty; /* internal back ptr */
194 const struct tty_port_operations *ops; /* Port operations */ 194 const struct tty_port_operations *ops; /* Port operations */
195 spinlock_t lock; /* Lock protecting tty field */ 195 spinlock_t lock; /* Lock protecting tty field */
196 int blocked_open; /* Waiting to open */ 196 int blocked_open; /* Waiting to open */
197 int count; /* Usage count */ 197 int count; /* Usage count */
198 wait_queue_head_t open_wait; /* Open waiters */ 198 wait_queue_head_t open_wait; /* Open waiters */
199 wait_queue_head_t close_wait; /* Close waiters */ 199 wait_queue_head_t close_wait; /* Close waiters */
200 wait_queue_head_t delta_msr_wait; /* Modem status change */ 200 wait_queue_head_t delta_msr_wait; /* Modem status change */
201 unsigned long flags; /* TTY flags ASY_*/ 201 unsigned long flags; /* TTY flags ASY_*/
202 unsigned long iflags; /* TTYP_ internal flags */ 202 unsigned long iflags; /* TTYP_ internal flags */
203 #define TTYP_FLUSHING 1 /* Flushing to ldisc in progress */ 203 #define TTYP_FLUSHING 1 /* Flushing to ldisc in progress */
204 #define TTYP_FLUSHPENDING 2 /* Queued buffer flush pending */ 204 #define TTYP_FLUSHPENDING 2 /* Queued buffer flush pending */
205 unsigned char console:1, /* port is a console */ 205 unsigned char console:1, /* port is a console */
206 low_latency:1; /* direct buffer flush */ 206 low_latency:1; /* direct buffer flush */
207 struct mutex mutex; /* Locking */ 207 struct mutex mutex; /* Locking */
208 struct mutex buf_mutex; /* Buffer alloc lock */ 208 struct mutex buf_mutex; /* Buffer alloc lock */
209 unsigned char *xmit_buf; /* Optional buffer */ 209 unsigned char *xmit_buf; /* Optional buffer */
210 unsigned int close_delay; /* Close port delay */ 210 unsigned int close_delay; /* Close port delay */
211 unsigned int closing_wait; /* Delay for output */ 211 unsigned int closing_wait; /* Delay for output */
212 int drain_delay; /* Set to zero if no pure time 212 int drain_delay; /* Set to zero if no pure time
213 based drain is needed else 213 based drain is needed else
214 set to size of fifo */ 214 set to size of fifo */
215 struct kref kref; /* Ref counter */ 215 struct kref kref; /* Ref counter */
216 }; 216 };
217 217
218 /* 218 /*
219 * Where all of the state associated with a tty is kept while the tty 219 * Where all of the state associated with a tty is kept while the tty
220 * is open. Since the termios state should be kept even if the tty 220 * is open. Since the termios state should be kept even if the tty
221 * has been closed --- for things like the baud rate, etc --- it is 221 * has been closed --- for things like the baud rate, etc --- it is
222 * not stored here, but rather a pointer to the real state is stored 222 * not stored here, but rather a pointer to the real state is stored
223 * here. Possible the winsize structure should have the same 223 * here. Possible the winsize structure should have the same
224 * treatment, but (1) the default 80x24 is usually right and (2) it's 224 * treatment, but (1) the default 80x24 is usually right and (2) it's
225 * most often used by a windowing system, which will set the correct 225 * most often used by a windowing system, which will set the correct
226 * size each time the window is created or resized anyway. 226 * size each time the window is created or resized anyway.
227 * - TYT, 9/14/92 227 * - TYT, 9/14/92
228 */ 228 */
229 229
230 struct tty_operations; 230 struct tty_operations;
231 231
232 struct tty_struct { 232 struct tty_struct {
233 int magic; 233 int magic;
234 struct kref kref; 234 struct kref kref;
235 struct device *dev; 235 struct device *dev;
236 struct tty_driver *driver; 236 struct tty_driver *driver;
237 const struct tty_operations *ops; 237 const struct tty_operations *ops;
238 int index; 238 int index;
239 239
240 /* Protects ldisc changes: Lock tty not pty */ 240 /* Protects ldisc changes: Lock tty not pty */
241 struct mutex ldisc_mutex; 241 struct mutex ldisc_mutex;
242 struct tty_ldisc *ldisc; 242 struct tty_ldisc *ldisc;
243 243
244 struct mutex atomic_write_lock; 244 struct mutex atomic_write_lock;
245 struct mutex legacy_mutex; 245 struct mutex legacy_mutex;
246 struct mutex termios_mutex; 246 struct mutex termios_mutex;
247 spinlock_t ctrl_lock; 247 spinlock_t ctrl_lock;
248 /* Termios values are protected by the termios mutex */ 248 /* Termios values are protected by the termios mutex */
249 struct ktermios termios, termios_locked; 249 struct ktermios termios, termios_locked;
250 struct termiox *termiox; /* May be NULL for unsupported */ 250 struct termiox *termiox; /* May be NULL for unsupported */
251 char name[64]; 251 char name[64];
252 struct pid *pgrp; /* Protected by ctrl lock */ 252 struct pid *pgrp; /* Protected by ctrl lock */
253 struct pid *session; 253 struct pid *session;
254 unsigned long flags; 254 unsigned long flags;
255 int count; 255 int count;
256 struct winsize winsize; /* termios mutex */ 256 struct winsize winsize; /* termios mutex */
257 unsigned char stopped:1, hw_stopped:1, flow_stopped:1, packet:1; 257 unsigned char stopped:1, hw_stopped:1, flow_stopped:1, packet:1;
258 unsigned char ctrl_status; /* ctrl_lock */ 258 unsigned char ctrl_status; /* ctrl_lock */
259 unsigned int receive_room; /* Bytes free for queue */ 259 unsigned int receive_room; /* Bytes free for queue */
260 int flow_change; 260 int flow_change;
261 261
262 struct tty_struct *link; 262 struct tty_struct *link;
263 struct fasync_struct *fasync; 263 struct fasync_struct *fasync;
264 int alt_speed; /* For magic substitution of 38400 bps */ 264 int alt_speed; /* For magic substitution of 38400 bps */
265 wait_queue_head_t write_wait; 265 wait_queue_head_t write_wait;
266 wait_queue_head_t read_wait; 266 wait_queue_head_t read_wait;
267 struct work_struct hangup_work; 267 struct work_struct hangup_work;
268 void *disc_data; 268 void *disc_data;
269 void *driver_data; 269 void *driver_data;
270 struct list_head tty_files; 270 struct list_head tty_files;
271 271
272 #define N_TTY_BUF_SIZE 4096 272 #define N_TTY_BUF_SIZE 4096
273 273
274 unsigned char closing:1; 274 unsigned char closing:1;
275 unsigned short minimum_to_wake; 275 unsigned short minimum_to_wake;
276 unsigned char *write_buf; 276 unsigned char *write_buf;
277 int write_cnt; 277 int write_cnt;
278 /* If the tty has a pending do_SAK, queue it here - akpm */ 278 /* If the tty has a pending do_SAK, queue it here - akpm */
279 struct work_struct SAK_work; 279 struct work_struct SAK_work;
280 struct tty_port *port; 280 struct tty_port *port;
281 }; 281 };
282 282
283 /* Each of a tty's open files has private_data pointing to tty_file_private */ 283 /* Each of a tty's open files has private_data pointing to tty_file_private */
284 struct tty_file_private { 284 struct tty_file_private {
285 struct tty_struct *tty; 285 struct tty_struct *tty;
286 struct file *file; 286 struct file *file;
287 struct list_head list; 287 struct list_head list;
288 }; 288 };
289 289
290 /* tty magic number */ 290 /* tty magic number */
291 #define TTY_MAGIC 0x5401 291 #define TTY_MAGIC 0x5401
292 292
293 /* 293 /*
294 * These bits are used in the flags field of the tty structure. 294 * These bits are used in the flags field of the tty structure.
295 * 295 *
296 * So that interrupts won't be able to mess up the queues, 296 * So that interrupts won't be able to mess up the queues,
297 * copy_to_cooked must be atomic with respect to itself, as must 297 * copy_to_cooked must be atomic with respect to itself, as must
298 * tty->write. Thus, you must use the inline functions set_bit() and 298 * tty->write. Thus, you must use the inline functions set_bit() and
299 * clear_bit() to make things atomic. 299 * clear_bit() to make things atomic.
300 */ 300 */
301 #define TTY_THROTTLED 0 /* Call unthrottle() at threshold min */ 301 #define TTY_THROTTLED 0 /* Call unthrottle() at threshold min */
302 #define TTY_IO_ERROR 1 /* Cause an I/O error (may be no ldisc too) */ 302 #define TTY_IO_ERROR 1 /* Cause an I/O error (may be no ldisc too) */
303 #define TTY_OTHER_CLOSED 2 /* Other side (if any) has closed */ 303 #define TTY_OTHER_CLOSED 2 /* Other side (if any) has closed */
304 #define TTY_EXCLUSIVE 3 /* Exclusive open mode */ 304 #define TTY_EXCLUSIVE 3 /* Exclusive open mode */
305 #define TTY_DEBUG 4 /* Debugging */ 305 #define TTY_DEBUG 4 /* Debugging */
306 #define TTY_DO_WRITE_WAKEUP 5 /* Call write_wakeup after queuing new */ 306 #define TTY_DO_WRITE_WAKEUP 5 /* Call write_wakeup after queuing new */
307 #define TTY_PUSH 6 /* n_tty private */ 307 #define TTY_PUSH 6 /* n_tty private */
308 #define TTY_CLOSING 7 /* ->close() in progress */ 308 #define TTY_CLOSING 7 /* ->close() in progress */
309 #define TTY_LDISC 9 /* Line discipline attached */ 309 #define TTY_LDISC 9 /* Line discipline attached */
310 #define TTY_LDISC_CHANGING 10 /* Line discipline changing */ 310 #define TTY_LDISC_CHANGING 10 /* Line discipline changing */
311 #define TTY_LDISC_OPEN 11 /* Line discipline is open */ 311 #define TTY_LDISC_OPEN 11 /* Line discipline is open */
312 #define TTY_HW_COOK_OUT 14 /* Hardware can do output cooking */ 312 #define TTY_HW_COOK_OUT 14 /* Hardware can do output cooking */
313 #define TTY_HW_COOK_IN 15 /* Hardware can do input cooking */ 313 #define TTY_HW_COOK_IN 15 /* Hardware can do input cooking */
314 #define TTY_PTY_LOCK 16 /* pty private */ 314 #define TTY_PTY_LOCK 16 /* pty private */
315 #define TTY_NO_WRITE_SPLIT 17 /* Preserve write boundaries to driver */ 315 #define TTY_NO_WRITE_SPLIT 17 /* Preserve write boundaries to driver */
316 #define TTY_HUPPED 18 /* Post driver->hangup() */ 316 #define TTY_HUPPED 18 /* Post driver->hangup() */
317 #define TTY_HUPPING 21 /* ->hangup() in progress */ 317 #define TTY_HUPPING 21 /* ->hangup() in progress */
318 #define TTY_LDISC_HALTED 22 /* Line discipline is halted */
318 319
319 #define TTY_WRITE_FLUSH(tty) tty_write_flush((tty)) 320 #define TTY_WRITE_FLUSH(tty) tty_write_flush((tty))
320 321
321 /* Values for tty->flow_change */ 322 /* Values for tty->flow_change */
322 #define TTY_THROTTLE_SAFE 1 323 #define TTY_THROTTLE_SAFE 1
323 #define TTY_UNTHROTTLE_SAFE 2 324 #define TTY_UNTHROTTLE_SAFE 2
324 325
325 static inline void __tty_set_flow_change(struct tty_struct *tty, int val) 326 static inline void __tty_set_flow_change(struct tty_struct *tty, int val)
326 { 327 {
327 tty->flow_change = val; 328 tty->flow_change = val;
328 } 329 }
329 330
330 static inline void tty_set_flow_change(struct tty_struct *tty, int val) 331 static inline void tty_set_flow_change(struct tty_struct *tty, int val)
331 { 332 {
332 tty->flow_change = val; 333 tty->flow_change = val;
333 smp_mb(); 334 smp_mb();
334 } 335 }
335 336
336 #ifdef CONFIG_TTY 337 #ifdef CONFIG_TTY
337 extern void console_init(void); 338 extern void console_init(void);
338 extern void tty_kref_put(struct tty_struct *tty); 339 extern void tty_kref_put(struct tty_struct *tty);
339 extern struct pid *tty_get_pgrp(struct tty_struct *tty); 340 extern struct pid *tty_get_pgrp(struct tty_struct *tty);
340 extern void tty_vhangup_self(void); 341 extern void tty_vhangup_self(void);
341 extern void disassociate_ctty(int priv); 342 extern void disassociate_ctty(int priv);
342 extern dev_t tty_devnum(struct tty_struct *tty); 343 extern dev_t tty_devnum(struct tty_struct *tty);
343 extern void proc_clear_tty(struct task_struct *p); 344 extern void proc_clear_tty(struct task_struct *p);
344 extern struct tty_struct *get_current_tty(void); 345 extern struct tty_struct *get_current_tty(void);
345 /* tty_io.c */ 346 /* tty_io.c */
346 extern int __init tty_init(void); 347 extern int __init tty_init(void);
347 #else 348 #else
348 static inline void console_init(void) 349 static inline void console_init(void)
349 { } 350 { }
350 static inline void tty_kref_put(struct tty_struct *tty) 351 static inline void tty_kref_put(struct tty_struct *tty)
351 { } 352 { }
352 static inline struct pid *tty_get_pgrp(struct tty_struct *tty) 353 static inline struct pid *tty_get_pgrp(struct tty_struct *tty)
353 { return NULL; } 354 { return NULL; }
354 static inline void tty_vhangup_self(void) 355 static inline void tty_vhangup_self(void)
355 { } 356 { }
356 static inline void disassociate_ctty(int priv) 357 static inline void disassociate_ctty(int priv)
357 { } 358 { }
358 static inline dev_t tty_devnum(struct tty_struct *tty) 359 static inline dev_t tty_devnum(struct tty_struct *tty)
359 { return 0; } 360 { return 0; }
360 static inline void proc_clear_tty(struct task_struct *p) 361 static inline void proc_clear_tty(struct task_struct *p)
361 { } 362 { }
362 static inline struct tty_struct *get_current_tty(void) 363 static inline struct tty_struct *get_current_tty(void)
363 { return NULL; } 364 { return NULL; }
364 /* tty_io.c */ 365 /* tty_io.c */
365 static inline int __init tty_init(void) 366 static inline int __init tty_init(void)
366 { return 0; } 367 { return 0; }
367 #endif 368 #endif
368 369
369 extern void tty_write_flush(struct tty_struct *); 370 extern void tty_write_flush(struct tty_struct *);
370 371
371 extern struct ktermios tty_std_termios; 372 extern struct ktermios tty_std_termios;
372 373
373 extern int vcs_init(void); 374 extern int vcs_init(void);
374 375
375 extern struct class *tty_class; 376 extern struct class *tty_class;
376 377
377 /** 378 /**
378 * tty_kref_get - get a tty reference 379 * tty_kref_get - get a tty reference
379 * @tty: tty device 380 * @tty: tty device
380 * 381 *
381 * Return a new reference to a tty object. The caller must hold 382 * Return a new reference to a tty object. The caller must hold
382 * sufficient locks/counts to ensure that their existing reference cannot 383 * sufficient locks/counts to ensure that their existing reference cannot
383 * go away 384 * go away
384 */ 385 */
385 386
386 static inline struct tty_struct *tty_kref_get(struct tty_struct *tty) 387 static inline struct tty_struct *tty_kref_get(struct tty_struct *tty)
387 { 388 {
388 if (tty) 389 if (tty)
389 kref_get(&tty->kref); 390 kref_get(&tty->kref);
390 return tty; 391 return tty;
391 } 392 }
392 393
393 extern int tty_paranoia_check(struct tty_struct *tty, struct inode *inode, 394 extern int tty_paranoia_check(struct tty_struct *tty, struct inode *inode,
394 const char *routine); 395 const char *routine);
395 extern char *tty_name(struct tty_struct *tty, char *buf); 396 extern char *tty_name(struct tty_struct *tty, char *buf);
396 extern void tty_wait_until_sent(struct tty_struct *tty, long timeout); 397 extern void tty_wait_until_sent(struct tty_struct *tty, long timeout);
397 extern int tty_check_change(struct tty_struct *tty); 398 extern int tty_check_change(struct tty_struct *tty);
398 extern void stop_tty(struct tty_struct *tty); 399 extern void stop_tty(struct tty_struct *tty);
399 extern void start_tty(struct tty_struct *tty); 400 extern void start_tty(struct tty_struct *tty);
400 extern int tty_register_driver(struct tty_driver *driver); 401 extern int tty_register_driver(struct tty_driver *driver);
401 extern int tty_unregister_driver(struct tty_driver *driver); 402 extern int tty_unregister_driver(struct tty_driver *driver);
402 extern struct device *tty_register_device(struct tty_driver *driver, 403 extern struct device *tty_register_device(struct tty_driver *driver,
403 unsigned index, struct device *dev); 404 unsigned index, struct device *dev);
404 extern struct device *tty_register_device_attr(struct tty_driver *driver, 405 extern struct device *tty_register_device_attr(struct tty_driver *driver,
405 unsigned index, struct device *device, 406 unsigned index, struct device *device,
406 void *drvdata, 407 void *drvdata,
407 const struct attribute_group **attr_grp); 408 const struct attribute_group **attr_grp);
408 extern void tty_unregister_device(struct tty_driver *driver, unsigned index); 409 extern void tty_unregister_device(struct tty_driver *driver, unsigned index);
409 extern int tty_read_raw_data(struct tty_struct *tty, unsigned char *bufp, 410 extern int tty_read_raw_data(struct tty_struct *tty, unsigned char *bufp,
410 int buflen); 411 int buflen);
411 extern void tty_write_message(struct tty_struct *tty, char *msg); 412 extern void tty_write_message(struct tty_struct *tty, char *msg);
412 extern int tty_put_char(struct tty_struct *tty, unsigned char c); 413 extern int tty_put_char(struct tty_struct *tty, unsigned char c);
413 extern int tty_chars_in_buffer(struct tty_struct *tty); 414 extern int tty_chars_in_buffer(struct tty_struct *tty);
414 extern int tty_write_room(struct tty_struct *tty); 415 extern int tty_write_room(struct tty_struct *tty);
415 extern void tty_driver_flush_buffer(struct tty_struct *tty); 416 extern void tty_driver_flush_buffer(struct tty_struct *tty);
416 extern void tty_throttle(struct tty_struct *tty); 417 extern void tty_throttle(struct tty_struct *tty);
417 extern void tty_unthrottle(struct tty_struct *tty); 418 extern void tty_unthrottle(struct tty_struct *tty);
418 extern int tty_throttle_safe(struct tty_struct *tty); 419 extern int tty_throttle_safe(struct tty_struct *tty);
419 extern int tty_unthrottle_safe(struct tty_struct *tty); 420 extern int tty_unthrottle_safe(struct tty_struct *tty);
420 extern int tty_do_resize(struct tty_struct *tty, struct winsize *ws); 421 extern int tty_do_resize(struct tty_struct *tty, struct winsize *ws);
421 extern void tty_driver_remove_tty(struct tty_driver *driver, 422 extern void tty_driver_remove_tty(struct tty_driver *driver,
422 struct tty_struct *tty); 423 struct tty_struct *tty);
423 extern void tty_free_termios(struct tty_struct *tty); 424 extern void tty_free_termios(struct tty_struct *tty);
424 extern int is_current_pgrp_orphaned(void); 425 extern int is_current_pgrp_orphaned(void);
425 extern int is_ignored(int sig); 426 extern int is_ignored(int sig);
426 extern int tty_signal(int sig, struct tty_struct *tty); 427 extern int tty_signal(int sig, struct tty_struct *tty);
427 extern void tty_hangup(struct tty_struct *tty); 428 extern void tty_hangup(struct tty_struct *tty);
428 extern void tty_vhangup(struct tty_struct *tty); 429 extern void tty_vhangup(struct tty_struct *tty);
429 extern void tty_vhangup_locked(struct tty_struct *tty); 430 extern void tty_vhangup_locked(struct tty_struct *tty);
430 extern void tty_unhangup(struct file *filp); 431 extern void tty_unhangup(struct file *filp);
431 extern int tty_hung_up_p(struct file *filp); 432 extern int tty_hung_up_p(struct file *filp);
432 extern void do_SAK(struct tty_struct *tty); 433 extern void do_SAK(struct tty_struct *tty);
433 extern void __do_SAK(struct tty_struct *tty); 434 extern void __do_SAK(struct tty_struct *tty);
434 extern void no_tty(void); 435 extern void no_tty(void);
435 extern void tty_flush_to_ldisc(struct tty_struct *tty); 436 extern void tty_flush_to_ldisc(struct tty_struct *tty);
436 extern void tty_buffer_free_all(struct tty_port *port); 437 extern void tty_buffer_free_all(struct tty_port *port);
437 extern void tty_buffer_flush(struct tty_struct *tty); 438 extern void tty_buffer_flush(struct tty_struct *tty);
438 extern void tty_buffer_init(struct tty_port *port); 439 extern void tty_buffer_init(struct tty_port *port);
439 extern speed_t tty_termios_baud_rate(struct ktermios *termios); 440 extern speed_t tty_termios_baud_rate(struct ktermios *termios);
440 extern speed_t tty_termios_input_baud_rate(struct ktermios *termios); 441 extern speed_t tty_termios_input_baud_rate(struct ktermios *termios);
441 extern void tty_termios_encode_baud_rate(struct ktermios *termios, 442 extern void tty_termios_encode_baud_rate(struct ktermios *termios,
442 speed_t ibaud, speed_t obaud); 443 speed_t ibaud, speed_t obaud);
443 extern void tty_encode_baud_rate(struct tty_struct *tty, 444 extern void tty_encode_baud_rate(struct tty_struct *tty,
444 speed_t ibaud, speed_t obaud); 445 speed_t ibaud, speed_t obaud);
445 446
446 /** 447 /**
447 * tty_get_baud_rate - get tty bit rates 448 * tty_get_baud_rate - get tty bit rates
448 * @tty: tty to query 449 * @tty: tty to query
449 * 450 *
450 * Returns the baud rate as an integer for this terminal. The 451 * Returns the baud rate as an integer for this terminal. The
451 * termios lock must be held by the caller and the terminal bit 452 * termios lock must be held by the caller and the terminal bit
452 * flags may be updated. 453 * flags may be updated.
453 * 454 *
454 * Locking: none 455 * Locking: none
455 */ 456 */
456 static inline speed_t tty_get_baud_rate(struct tty_struct *tty) 457 static inline speed_t tty_get_baud_rate(struct tty_struct *tty)
457 { 458 {
458 return tty_termios_baud_rate(&tty->termios); 459 return tty_termios_baud_rate(&tty->termios);
459 } 460 }
460 461
461 extern void tty_termios_copy_hw(struct ktermios *new, struct ktermios *old); 462 extern void tty_termios_copy_hw(struct ktermios *new, struct ktermios *old);
462 extern int tty_termios_hw_change(struct ktermios *a, struct ktermios *b); 463 extern int tty_termios_hw_change(struct ktermios *a, struct ktermios *b);
463 extern int tty_set_termios(struct tty_struct *tty, struct ktermios *kt); 464 extern int tty_set_termios(struct tty_struct *tty, struct ktermios *kt);
464 465
465 extern struct tty_ldisc *tty_ldisc_ref(struct tty_struct *); 466 extern struct tty_ldisc *tty_ldisc_ref(struct tty_struct *);
466 extern void tty_ldisc_deref(struct tty_ldisc *); 467 extern void tty_ldisc_deref(struct tty_ldisc *);
467 extern struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *); 468 extern struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *);
468 extern void tty_ldisc_hangup(struct tty_struct *tty); 469 extern void tty_ldisc_hangup(struct tty_struct *tty);
469 extern const struct file_operations tty_ldiscs_proc_fops; 470 extern const struct file_operations tty_ldiscs_proc_fops;
470 471
471 extern void tty_wakeup(struct tty_struct *tty); 472 extern void tty_wakeup(struct tty_struct *tty);
472 extern void tty_ldisc_flush(struct tty_struct *tty); 473 extern void tty_ldisc_flush(struct tty_struct *tty);
473 474
474 extern long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg); 475 extern long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
475 extern int tty_mode_ioctl(struct tty_struct *tty, struct file *file, 476 extern int tty_mode_ioctl(struct tty_struct *tty, struct file *file,
476 unsigned int cmd, unsigned long arg); 477 unsigned int cmd, unsigned long arg);
477 extern int tty_perform_flush(struct tty_struct *tty, unsigned long arg); 478 extern int tty_perform_flush(struct tty_struct *tty, unsigned long arg);
478 extern void tty_default_fops(struct file_operations *fops); 479 extern void tty_default_fops(struct file_operations *fops);
479 extern struct tty_struct *alloc_tty_struct(void); 480 extern struct tty_struct *alloc_tty_struct(void);
480 extern int tty_alloc_file(struct file *file); 481 extern int tty_alloc_file(struct file *file);
481 extern void tty_add_file(struct tty_struct *tty, struct file *file); 482 extern void tty_add_file(struct tty_struct *tty, struct file *file);
482 extern void tty_free_file(struct file *file); 483 extern void tty_free_file(struct file *file);
483 extern void free_tty_struct(struct tty_struct *tty); 484 extern void free_tty_struct(struct tty_struct *tty);
484 extern void initialize_tty_struct(struct tty_struct *tty, 485 extern void initialize_tty_struct(struct tty_struct *tty,
485 struct tty_driver *driver, int idx); 486 struct tty_driver *driver, int idx);
486 extern void deinitialize_tty_struct(struct tty_struct *tty); 487 extern void deinitialize_tty_struct(struct tty_struct *tty);
487 extern struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx); 488 extern struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx);
488 extern int tty_release(struct inode *inode, struct file *filp); 489 extern int tty_release(struct inode *inode, struct file *filp);
489 extern int tty_init_termios(struct tty_struct *tty); 490 extern int tty_init_termios(struct tty_struct *tty);
490 extern int tty_standard_install(struct tty_driver *driver, 491 extern int tty_standard_install(struct tty_driver *driver,
491 struct tty_struct *tty); 492 struct tty_struct *tty);
492 493
493 extern struct tty_struct *tty_pair_get_tty(struct tty_struct *tty); 494 extern struct tty_struct *tty_pair_get_tty(struct tty_struct *tty);
494 extern struct tty_struct *tty_pair_get_pty(struct tty_struct *tty); 495 extern struct tty_struct *tty_pair_get_pty(struct tty_struct *tty);
495 496
496 extern struct mutex tty_mutex; 497 extern struct mutex tty_mutex;
497 extern spinlock_t tty_files_lock; 498 extern spinlock_t tty_files_lock;
498 499
499 extern void tty_write_unlock(struct tty_struct *tty); 500 extern void tty_write_unlock(struct tty_struct *tty);
500 extern int tty_write_lock(struct tty_struct *tty, int ndelay); 501 extern int tty_write_lock(struct tty_struct *tty, int ndelay);
501 #define tty_is_writelocked(tty) (mutex_is_locked(&tty->atomic_write_lock)) 502 #define tty_is_writelocked(tty) (mutex_is_locked(&tty->atomic_write_lock))
502 503
503 extern void tty_port_init(struct tty_port *port); 504 extern void tty_port_init(struct tty_port *port);
504 extern void tty_port_link_device(struct tty_port *port, 505 extern void tty_port_link_device(struct tty_port *port,
505 struct tty_driver *driver, unsigned index); 506 struct tty_driver *driver, unsigned index);
506 extern struct device *tty_port_register_device(struct tty_port *port, 507 extern struct device *tty_port_register_device(struct tty_port *port,
507 struct tty_driver *driver, unsigned index, 508 struct tty_driver *driver, unsigned index,
508 struct device *device); 509 struct device *device);
509 extern struct device *tty_port_register_device_attr(struct tty_port *port, 510 extern struct device *tty_port_register_device_attr(struct tty_port *port,
510 struct tty_driver *driver, unsigned index, 511 struct tty_driver *driver, unsigned index,
511 struct device *device, void *drvdata, 512 struct device *device, void *drvdata,
512 const struct attribute_group **attr_grp); 513 const struct attribute_group **attr_grp);
513 extern int tty_port_alloc_xmit_buf(struct tty_port *port); 514 extern int tty_port_alloc_xmit_buf(struct tty_port *port);
514 extern void tty_port_free_xmit_buf(struct tty_port *port); 515 extern void tty_port_free_xmit_buf(struct tty_port *port);
515 extern void tty_port_destroy(struct tty_port *port); 516 extern void tty_port_destroy(struct tty_port *port);
516 extern void tty_port_put(struct tty_port *port); 517 extern void tty_port_put(struct tty_port *port);
517 518
518 static inline struct tty_port *tty_port_get(struct tty_port *port) 519 static inline struct tty_port *tty_port_get(struct tty_port *port)
519 { 520 {
520 if (port) 521 if (port)
521 kref_get(&port->kref); 522 kref_get(&port->kref);
522 return port; 523 return port;
523 } 524 }
524 525
525 /* If the cts flow control is enabled, return true. */ 526 /* If the cts flow control is enabled, return true. */
526 static inline bool tty_port_cts_enabled(struct tty_port *port) 527 static inline bool tty_port_cts_enabled(struct tty_port *port)
527 { 528 {
528 return port->flags & ASYNC_CTS_FLOW; 529 return port->flags & ASYNC_CTS_FLOW;
529 } 530 }
530 531
531 extern struct tty_struct *tty_port_tty_get(struct tty_port *port); 532 extern struct tty_struct *tty_port_tty_get(struct tty_port *port);
532 extern void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty); 533 extern void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty);
533 extern int tty_port_carrier_raised(struct tty_port *port); 534 extern int tty_port_carrier_raised(struct tty_port *port);
534 extern void tty_port_raise_dtr_rts(struct tty_port *port); 535 extern void tty_port_raise_dtr_rts(struct tty_port *port);
535 extern void tty_port_lower_dtr_rts(struct tty_port *port); 536 extern void tty_port_lower_dtr_rts(struct tty_port *port);
536 extern void tty_port_hangup(struct tty_port *port); 537 extern void tty_port_hangup(struct tty_port *port);
537 extern void tty_port_tty_hangup(struct tty_port *port, bool check_clocal); 538 extern void tty_port_tty_hangup(struct tty_port *port, bool check_clocal);
538 extern void tty_port_tty_wakeup(struct tty_port *port); 539 extern void tty_port_tty_wakeup(struct tty_port *port);
539 extern int tty_port_block_til_ready(struct tty_port *port, 540 extern int tty_port_block_til_ready(struct tty_port *port,
540 struct tty_struct *tty, struct file *filp); 541 struct tty_struct *tty, struct file *filp);
541 extern int tty_port_close_start(struct tty_port *port, 542 extern int tty_port_close_start(struct tty_port *port,
542 struct tty_struct *tty, struct file *filp); 543 struct tty_struct *tty, struct file *filp);
543 extern void tty_port_close_end(struct tty_port *port, struct tty_struct *tty); 544 extern void tty_port_close_end(struct tty_port *port, struct tty_struct *tty);
544 extern void tty_port_close(struct tty_port *port, 545 extern void tty_port_close(struct tty_port *port,
545 struct tty_struct *tty, struct file *filp); 546 struct tty_struct *tty, struct file *filp);
546 extern int tty_port_install(struct tty_port *port, struct tty_driver *driver, 547 extern int tty_port_install(struct tty_port *port, struct tty_driver *driver,
547 struct tty_struct *tty); 548 struct tty_struct *tty);
548 extern int tty_port_open(struct tty_port *port, 549 extern int tty_port_open(struct tty_port *port,
549 struct tty_struct *tty, struct file *filp); 550 struct tty_struct *tty, struct file *filp);
550 static inline int tty_port_users(struct tty_port *port) 551 static inline int tty_port_users(struct tty_port *port)
551 { 552 {
552 return port->count + port->blocked_open; 553 return port->count + port->blocked_open;
553 } 554 }
554 555
555 extern int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc); 556 extern int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc);
556 extern int tty_unregister_ldisc(int disc); 557 extern int tty_unregister_ldisc(int disc);
557 extern int tty_set_ldisc(struct tty_struct *tty, int ldisc); 558 extern int tty_set_ldisc(struct tty_struct *tty, int ldisc);
558 extern int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty); 559 extern int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty);
559 extern void tty_ldisc_release(struct tty_struct *tty, struct tty_struct *o_tty); 560 extern void tty_ldisc_release(struct tty_struct *tty, struct tty_struct *o_tty);
560 extern void tty_ldisc_init(struct tty_struct *tty); 561 extern void tty_ldisc_init(struct tty_struct *tty);
561 extern void tty_ldisc_deinit(struct tty_struct *tty); 562 extern void tty_ldisc_deinit(struct tty_struct *tty);
562 extern void tty_ldisc_begin(void); 563 extern void tty_ldisc_begin(void);
563 /* This last one is just for the tty layer internals and shouldn't be used elsewhere */ 564 /* This last one is just for the tty layer internals and shouldn't be used elsewhere */
564 extern void tty_ldisc_enable(struct tty_struct *tty); 565 extern void tty_ldisc_enable(struct tty_struct *tty);
565 566
566 567
567 /* n_tty.c */ 568 /* n_tty.c */
568 extern struct tty_ldisc_ops tty_ldisc_N_TTY; 569 extern struct tty_ldisc_ops tty_ldisc_N_TTY;
569 extern void n_tty_inherit_ops(struct tty_ldisc_ops *ops); 570 extern void n_tty_inherit_ops(struct tty_ldisc_ops *ops);
570 571
571 /* tty_audit.c */ 572 /* tty_audit.c */
572 #ifdef CONFIG_AUDIT 573 #ifdef CONFIG_AUDIT
573 extern void tty_audit_add_data(struct tty_struct *tty, unsigned char *data, 574 extern void tty_audit_add_data(struct tty_struct *tty, unsigned char *data,
574 size_t size, unsigned icanon); 575 size_t size, unsigned icanon);
575 extern void tty_audit_exit(void); 576 extern void tty_audit_exit(void);
576 extern void tty_audit_fork(struct signal_struct *sig); 577 extern void tty_audit_fork(struct signal_struct *sig);
577 extern void tty_audit_tiocsti(struct tty_struct *tty, char ch); 578 extern void tty_audit_tiocsti(struct tty_struct *tty, char ch);
578 extern void tty_audit_push(struct tty_struct *tty); 579 extern void tty_audit_push(struct tty_struct *tty);
579 extern int tty_audit_push_task(struct task_struct *tsk, 580 extern int tty_audit_push_task(struct task_struct *tsk,
580 kuid_t loginuid, u32 sessionid); 581 kuid_t loginuid, u32 sessionid);
581 #else 582 #else
582 static inline void tty_audit_add_data(struct tty_struct *tty, 583 static inline void tty_audit_add_data(struct tty_struct *tty,
583 unsigned char *data, size_t size, unsigned icanon) 584 unsigned char *data, size_t size, unsigned icanon)
584 { 585 {
585 } 586 }
586 static inline void tty_audit_tiocsti(struct tty_struct *tty, char ch) 587 static inline void tty_audit_tiocsti(struct tty_struct *tty, char ch)
587 { 588 {
588 } 589 }
589 static inline void tty_audit_exit(void) 590 static inline void tty_audit_exit(void)
590 { 591 {
591 } 592 }
592 static inline void tty_audit_fork(struct signal_struct *sig) 593 static inline void tty_audit_fork(struct signal_struct *sig)
593 { 594 {
594 } 595 }
595 static inline void tty_audit_push(struct tty_struct *tty) 596 static inline void tty_audit_push(struct tty_struct *tty)
596 { 597 {
597 } 598 }
598 static inline int tty_audit_push_task(struct task_struct *tsk, 599 static inline int tty_audit_push_task(struct task_struct *tsk,
599 kuid_t loginuid, u32 sessionid) 600 kuid_t loginuid, u32 sessionid)
600 { 601 {
601 return 0; 602 return 0;
602 } 603 }
603 #endif 604 #endif
604 605
605 /* tty_ioctl.c */ 606 /* tty_ioctl.c */
606 extern int n_tty_ioctl_helper(struct tty_struct *tty, struct file *file, 607 extern int n_tty_ioctl_helper(struct tty_struct *tty, struct file *file,
607 unsigned int cmd, unsigned long arg); 608 unsigned int cmd, unsigned long arg);
608 extern long n_tty_compat_ioctl_helper(struct tty_struct *tty, struct file *file, 609 extern long n_tty_compat_ioctl_helper(struct tty_struct *tty, struct file *file,
609 unsigned int cmd, unsigned long arg); 610 unsigned int cmd, unsigned long arg);
610 611
611 /* serial.c */ 612 /* serial.c */
612 613
613 extern void serial_console_init(void); 614 extern void serial_console_init(void);
614 615
615 /* pcxx.c */ 616 /* pcxx.c */
616 617
617 extern int pcxe_open(struct tty_struct *tty, struct file *filp); 618 extern int pcxe_open(struct tty_struct *tty, struct file *filp);
618 619
619 /* vt.c */ 620 /* vt.c */
620 621
621 extern int vt_ioctl(struct tty_struct *tty, 622 extern int vt_ioctl(struct tty_struct *tty,
622 unsigned int cmd, unsigned long arg); 623 unsigned int cmd, unsigned long arg);
623 624
624 extern long vt_compat_ioctl(struct tty_struct *tty, 625 extern long vt_compat_ioctl(struct tty_struct *tty,
625 unsigned int cmd, unsigned long arg); 626 unsigned int cmd, unsigned long arg);
626 627
627 /* tty_mutex.c */ 628 /* tty_mutex.c */
628 /* functions for preparation of BKL removal */ 629 /* functions for preparation of BKL removal */
629 extern void __lockfunc tty_lock(struct tty_struct *tty); 630 extern void __lockfunc tty_lock(struct tty_struct *tty);
630 extern void __lockfunc tty_unlock(struct tty_struct *tty); 631 extern void __lockfunc tty_unlock(struct tty_struct *tty);
631 extern void __lockfunc tty_lock_pair(struct tty_struct *tty, 632 extern void __lockfunc tty_lock_pair(struct tty_struct *tty,
632 struct tty_struct *tty2); 633 struct tty_struct *tty2);
633 extern void __lockfunc tty_unlock_pair(struct tty_struct *tty, 634 extern void __lockfunc tty_unlock_pair(struct tty_struct *tty,
634 struct tty_struct *tty2); 635 struct tty_struct *tty2);
635 636
636 /* 637 /*
637 * this shall be called only from where BTM is held (like close) 638 * this shall be called only from where BTM is held (like close)
638 * 639 *
639 * We need this to ensure nobody waits for us to finish while we are waiting. 640 * We need this to ensure nobody waits for us to finish while we are waiting.
640 * Without this we were encountering system stalls. 641 * Without this we were encountering system stalls.
641 * 642 *
642 * This should be indeed removed with BTM removal later. 643 * This should be indeed removed with BTM removal later.
643 * 644 *
644 * Locking: BTM required. Nobody is allowed to hold port->mutex. 645 * Locking: BTM required. Nobody is allowed to hold port->mutex.
645 */ 646 */
646 static inline void tty_wait_until_sent_from_close(struct tty_struct *tty, 647 static inline void tty_wait_until_sent_from_close(struct tty_struct *tty,
647 long timeout) 648 long timeout)
648 { 649 {
649 tty_unlock(tty); /* tty->ops->close holds the BTM, drop it while waiting */ 650 tty_unlock(tty); /* tty->ops->close holds the BTM, drop it while waiting */
650 tty_wait_until_sent(tty, timeout); 651 tty_wait_until_sent(tty, timeout);
651 tty_lock(tty); 652 tty_lock(tty);
652 } 653 }
653 654
654 /* 655 /*
655 * wait_event_interruptible_tty -- wait for a condition with the tty lock held 656 * wait_event_interruptible_tty -- wait for a condition with the tty lock held
656 * 657 *
657 * The condition we are waiting for might take a long time to 658 * The condition we are waiting for might take a long time to
658 * become true, or might depend on another thread taking the 659 * become true, or might depend on another thread taking the
659 * BTM. In either case, we need to drop the BTM to guarantee 660 * BTM. In either case, we need to drop the BTM to guarantee
660 * forward progress. This is a leftover from the conversion 661 * forward progress. This is a leftover from the conversion
661 * from the BKL and should eventually get removed as the BTM 662 * from the BKL and should eventually get removed as the BTM
662 * falls out of use. 663 * falls out of use.
663 * 664 *
664 * Do not use in new code. 665 * Do not use in new code.
665 */ 666 */
666 #define wait_event_interruptible_tty(tty, wq, condition) \ 667 #define wait_event_interruptible_tty(tty, wq, condition) \
667 ({ \ 668 ({ \
668 int __ret = 0; \ 669 int __ret = 0; \
669 if (!(condition)) { \ 670 if (!(condition)) { \
670 __wait_event_interruptible_tty(tty, wq, condition, __ret); \ 671 __wait_event_interruptible_tty(tty, wq, condition, __ret); \
671 } \ 672 } \
672 __ret; \ 673 __ret; \
673 }) 674 })
674 675
675 #define __wait_event_interruptible_tty(tty, wq, condition, ret) \ 676 #define __wait_event_interruptible_tty(tty, wq, condition, ret) \
676 do { \ 677 do { \
677 DEFINE_WAIT(__wait); \ 678 DEFINE_WAIT(__wait); \
678 \ 679 \
679 for (;;) { \ 680 for (;;) { \
680 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \ 681 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \
681 if (condition) \ 682 if (condition) \
682 break; \ 683 break; \
683 if (!signal_pending(current)) { \ 684 if (!signal_pending(current)) { \
684 tty_unlock(tty); \ 685 tty_unlock(tty); \
685 schedule(); \ 686 schedule(); \
686 tty_lock(tty); \ 687 tty_lock(tty); \
687 continue; \ 688 continue; \
688 } \ 689 } \
689 ret = -ERESTARTSYS; \ 690 ret = -ERESTARTSYS; \
690 break; \ 691 break; \
691 } \ 692 } \
692 finish_wait(&wq, &__wait); \ 693 finish_wait(&wq, &__wait); \
693 } while (0) 694 } while (0)
694 695
695 696
696 #endif 697 #endif
697 698