io_edgeport: Fix various bogus returns to the tty layer
[linux-2.6-block.git] / drivers / char / tty_io.c
CommitLineData
1da177e4
LT
1/*
2 * linux/drivers/char/tty_io.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7/*
8 * 'tty_io.c' gives an orthogonal feeling to tty's, be they consoles
9 * or rs-channels. It also implements echoing, cooked mode etc.
10 *
11 * Kill-line thanks to John T Kohl, who also corrected VMIN = VTIME = 0.
12 *
13 * Modified by Theodore Ts'o, 9/14/92, to dynamically allocate the
14 * tty_struct and tty_queue structures. Previously there was an array
15 * of 256 tty_struct's which was statically allocated, and the
16 * tty_queue structures were allocated at boot time. Both are now
17 * dynamically allocated only when the tty is open.
18 *
19 * Also restructured routines so that there is more of a separation
20 * between the high-level tty routines (tty_io.c and tty_ioctl.c) and
21 * the low-level tty routines (serial.c, pty.c, console.c). This
37bdfb07 22 * makes for cleaner and more compact code. -TYT, 9/17/92
1da177e4
LT
23 *
24 * Modified by Fred N. van Kempen, 01/29/93, to add line disciplines
25 * which can be dynamically activated and de-activated by the line
26 * discipline handling modules (like SLIP).
27 *
28 * NOTE: pay no attention to the line discipline code (yet); its
29 * interface is still subject to change in this version...
30 * -- TYT, 1/31/92
31 *
32 * Added functionality to the OPOST tty handling. No delays, but all
33 * other bits should be there.
34 * -- Nick Holloway <alfie@dcs.warwick.ac.uk>, 27th May 1993.
35 *
36 * Rewrote canonical mode and added more termios flags.
37 * -- julian@uhunix.uhcc.hawaii.edu (J. Cowley), 13Jan94
38 *
39 * Reorganized FASYNC support so mouse code can share it.
40 * -- ctm@ardi.com, 9Sep95
41 *
42 * New TIOCLINUX variants added.
43 * -- mj@k332.feld.cvut.cz, 19-Nov-95
37bdfb07 44 *
1da177e4
LT
45 * Restrict vt switching via ioctl()
46 * -- grif@cs.ucr.edu, 5-Dec-95
47 *
48 * Move console and virtual terminal code to more appropriate files,
49 * implement CONFIG_VT and generalize console device interface.
50 * -- Marko Kohtala <Marko.Kohtala@hut.fi>, March 97
51 *
52 * Rewrote init_dev and release_dev to eliminate races.
53 * -- Bill Hawes <whawes@star.net>, June 97
54 *
55 * Added devfs support.
56 * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 13-Jan-1998
57 *
58 * Added support for a Unix98-style ptmx device.
59 * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998
60 *
61 * Reduced memory usage for older ARM systems
62 * -- Russell King <rmk@arm.linux.org.uk>
63 *
64 * Move do_SAK() into process context. Less stack use in devfs functions.
37bdfb07
AC
65 * alloc_tty_struct() always uses kmalloc()
66 * -- Andrew Morton <andrewm@uow.edu.eu> 17Mar01
1da177e4
LT
67 */
68
1da177e4
LT
69#include <linux/types.h>
70#include <linux/major.h>
71#include <linux/errno.h>
72#include <linux/signal.h>
73#include <linux/fcntl.h>
74#include <linux/sched.h>
75#include <linux/interrupt.h>
76#include <linux/tty.h>
77#include <linux/tty_driver.h>
78#include <linux/tty_flip.h>
79#include <linux/devpts_fs.h>
80#include <linux/file.h>
9f3acc31 81#include <linux/fdtable.h>
1da177e4
LT
82#include <linux/console.h>
83#include <linux/timer.h>
84#include <linux/ctype.h>
85#include <linux/kd.h>
86#include <linux/mm.h>
87#include <linux/string.h>
88#include <linux/slab.h>
89#include <linux/poll.h>
90#include <linux/proc_fs.h>
91#include <linux/init.h>
92#include <linux/module.h>
93#include <linux/smp_lock.h>
94#include <linux/device.h>
1da177e4
LT
95#include <linux/wait.h>
96#include <linux/bitops.h>
b20f3ae5 97#include <linux/delay.h>
a352def2 98#include <linux/seq_file.h>
1da177e4 99
a352def2 100#include <linux/uaccess.h>
1da177e4
LT
101#include <asm/system.h>
102
103#include <linux/kbd_kern.h>
104#include <linux/vt_kern.h>
105#include <linux/selection.h>
1da177e4
LT
106
107#include <linux/kmod.h>
b488893a 108#include <linux/nsproxy.h>
1da177e4
LT
109
110#undef TTY_DEBUG_HANGUP
111
112#define TTY_PARANOIA_CHECK 1
113#define CHECK_TTY_COUNT 1
114
edc6afc5 115struct ktermios tty_std_termios = { /* for the benefit of tty drivers */
1da177e4
LT
116 .c_iflag = ICRNL | IXON,
117 .c_oflag = OPOST | ONLCR,
118 .c_cflag = B38400 | CS8 | CREAD | HUPCL,
119 .c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK |
120 ECHOCTL | ECHOKE | IEXTEN,
edc6afc5
AC
121 .c_cc = INIT_C_CC,
122 .c_ispeed = 38400,
123 .c_ospeed = 38400
1da177e4
LT
124};
125
126EXPORT_SYMBOL(tty_std_termios);
127
128/* This list gets poked at by procfs and various bits of boot up code. This
129 could do with some rationalisation such as pulling the tty proc function
130 into this file */
37bdfb07 131
1da177e4
LT
132LIST_HEAD(tty_drivers); /* linked list of tty drivers */
133
24ec839c 134/* Mutex to protect creating and releasing a tty. This is shared with
1da177e4 135 vt.c for deeply disgusting hack reasons */
70522e12 136DEFINE_MUTEX(tty_mutex);
de2a84f2 137EXPORT_SYMBOL(tty_mutex);
1da177e4
LT
138
139#ifdef CONFIG_UNIX98_PTYS
140extern struct tty_driver *ptm_driver; /* Unix98 pty masters; for /dev/ptmx */
1da177e4
LT
141static int ptmx_open(struct inode *, struct file *);
142#endif
143
1da177e4
LT
144static void initialize_tty_struct(struct tty_struct *tty);
145
146static ssize_t tty_read(struct file *, char __user *, size_t, loff_t *);
147static ssize_t tty_write(struct file *, const char __user *, size_t, loff_t *);
37bdfb07
AC
148ssize_t redirected_tty_write(struct file *, const char __user *,
149 size_t, loff_t *);
1da177e4
LT
150static unsigned int tty_poll(struct file *, poll_table *);
151static int tty_open(struct inode *, struct file *);
152static int tty_release(struct inode *, struct file *);
04f378b1 153long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
e10cc1df 154#ifdef CONFIG_COMPAT
37bdfb07 155static long tty_compat_ioctl(struct file *file, unsigned int cmd,
e10cc1df
PF
156 unsigned long arg);
157#else
158#define tty_compat_ioctl NULL
159#endif
37bdfb07 160static int tty_fasync(int fd, struct file *filp, int on);
d5698c28 161static void release_tty(struct tty_struct *tty, int idx);
2a65f1d9 162static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty);
98a27ba4 163static void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty);
1da177e4 164
af9b897e
AC
165/**
166 * alloc_tty_struct - allocate a tty object
167 *
168 * Return a new empty tty structure. The data fields have not
169 * been initialized in any way but has been zeroed
170 *
171 * Locking: none
af9b897e 172 */
1da177e4
LT
173
174static struct tty_struct *alloc_tty_struct(void)
175{
1266b1e1 176 return kzalloc(sizeof(struct tty_struct), GFP_KERNEL);
1da177e4
LT
177}
178
33f0f88f
AC
179static void tty_buffer_free_all(struct tty_struct *);
180
af9b897e
AC
181/**
182 * free_tty_struct - free a disused tty
183 * @tty: tty struct to free
184 *
185 * Free the write buffers, tty queue and tty memory itself.
186 *
187 * Locking: none. Must be called after tty is definitely unused
188 */
189
1da177e4
LT
190static inline void free_tty_struct(struct tty_struct *tty)
191{
192 kfree(tty->write_buf);
33f0f88f 193 tty_buffer_free_all(tty);
1da177e4
LT
194 kfree(tty);
195}
196
197#define TTY_NUMBER(tty) ((tty)->index + (tty)->driver->name_base)
198
af9b897e
AC
199/**
200 * tty_name - return tty naming
201 * @tty: tty structure
202 * @buf: buffer for output
203 *
204 * Convert a tty structure into a name. The name reflects the kernel
205 * naming policy and if udev is in use may not reflect user space
206 *
207 * Locking: none
208 */
209
1da177e4
LT
210char *tty_name(struct tty_struct *tty, char *buf)
211{
212 if (!tty) /* Hmm. NULL pointer. That's fun. */
213 strcpy(buf, "NULL tty");
214 else
215 strcpy(buf, tty->name);
216 return buf;
217}
218
219EXPORT_SYMBOL(tty_name);
220
d769a669 221int tty_paranoia_check(struct tty_struct *tty, struct inode *inode,
1da177e4
LT
222 const char *routine)
223{
224#ifdef TTY_PARANOIA_CHECK
225 if (!tty) {
226 printk(KERN_WARNING
227 "null TTY for (%d:%d) in %s\n",
228 imajor(inode), iminor(inode), routine);
229 return 1;
230 }
231 if (tty->magic != TTY_MAGIC) {
232 printk(KERN_WARNING
233 "bad magic number for tty struct (%d:%d) in %s\n",
234 imajor(inode), iminor(inode), routine);
235 return 1;
236 }
237#endif
238 return 0;
239}
240
241static int check_tty_count(struct tty_struct *tty, const char *routine)
242{
243#ifdef CHECK_TTY_COUNT
244 struct list_head *p;
245 int count = 0;
37bdfb07 246
1da177e4
LT
247 file_list_lock();
248 list_for_each(p, &tty->tty_files) {
249 count++;
250 }
251 file_list_unlock();
252 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
253 tty->driver->subtype == PTY_TYPE_SLAVE &&
254 tty->link && tty->link->count)
255 count++;
256 if (tty->count != count) {
257 printk(KERN_WARNING "Warning: dev (%s) tty->count(%d) "
258 "!= #fd's(%d) in %s\n",
259 tty->name, tty->count, count, routine);
260 return count;
24ec839c 261 }
1da177e4
LT
262#endif
263 return 0;
264}
265
33f0f88f
AC
266/*
267 * Tty buffer allocation management
268 */
269
af9b897e
AC
270/**
271 * tty_buffer_free_all - free buffers used by a tty
272 * @tty: tty to free from
273 *
274 * Remove all the buffers pending on a tty whether queued with data
275 * or in the free ring. Must be called when the tty is no longer in use
276 *
277 * Locking: none
278 */
279
33f0f88f
AC
280static void tty_buffer_free_all(struct tty_struct *tty)
281{
282 struct tty_buffer *thead;
37bdfb07 283 while ((thead = tty->buf.head) != NULL) {
33f0f88f
AC
284 tty->buf.head = thead->next;
285 kfree(thead);
286 }
37bdfb07 287 while ((thead = tty->buf.free) != NULL) {
33f0f88f
AC
288 tty->buf.free = thead->next;
289 kfree(thead);
290 }
291 tty->buf.tail = NULL;
01da5fd8 292 tty->buf.memory_used = 0;
33f0f88f
AC
293}
294
01da5fd8
AC
295/**
296 * tty_buffer_init - prepare a tty buffer structure
297 * @tty: tty to initialise
298 *
299 * Set up the initial state of the buffer management for a tty device.
300 * Must be called before the other tty buffer functions are used.
301 *
302 * Locking: none
303 */
304
33f0f88f
AC
305static void tty_buffer_init(struct tty_struct *tty)
306{
808249ce 307 spin_lock_init(&tty->buf.lock);
33f0f88f
AC
308 tty->buf.head = NULL;
309 tty->buf.tail = NULL;
310 tty->buf.free = NULL;
01da5fd8 311 tty->buf.memory_used = 0;
33f0f88f
AC
312}
313
01da5fd8
AC
314/**
315 * tty_buffer_alloc - allocate a tty buffer
316 * @tty: tty device
317 * @size: desired size (characters)
318 *
319 * Allocate a new tty buffer to hold the desired number of characters.
320 * Return NULL if out of memory or the allocation would exceed the
321 * per device queue
322 *
323 * Locking: Caller must hold tty->buf.lock
324 */
325
326static struct tty_buffer *tty_buffer_alloc(struct tty_struct *tty, size_t size)
33f0f88f 327{
01da5fd8
AC
328 struct tty_buffer *p;
329
330 if (tty->buf.memory_used + size > 65536)
331 return NULL;
332 p = kmalloc(sizeof(struct tty_buffer) + 2 * size, GFP_ATOMIC);
37bdfb07 333 if (p == NULL)
33f0f88f
AC
334 return NULL;
335 p->used = 0;
336 p->size = size;
337 p->next = NULL;
8977d929
PF
338 p->commit = 0;
339 p->read = 0;
33f0f88f
AC
340 p->char_buf_ptr = (char *)(p->data);
341 p->flag_buf_ptr = (unsigned char *)p->char_buf_ptr + size;
01da5fd8 342 tty->buf.memory_used += size;
33f0f88f
AC
343 return p;
344}
345
01da5fd8
AC
346/**
347 * tty_buffer_free - free a tty buffer
348 * @tty: tty owning the buffer
349 * @b: the buffer to free
350 *
351 * Free a tty buffer, or add it to the free list according to our
352 * internal strategy
353 *
354 * Locking: Caller must hold tty->buf.lock
355 */
33f0f88f
AC
356
357static void tty_buffer_free(struct tty_struct *tty, struct tty_buffer *b)
358{
359 /* Dumb strategy for now - should keep some stats */
01da5fd8
AC
360 tty->buf.memory_used -= b->size;
361 WARN_ON(tty->buf.memory_used < 0);
362
37bdfb07 363 if (b->size >= 512)
33f0f88f
AC
364 kfree(b);
365 else {
366 b->next = tty->buf.free;
367 tty->buf.free = b;
368 }
369}
370
c5c34d48 371/**
42fd552e 372 * __tty_buffer_flush - flush full tty buffers
c5c34d48
PF
373 * @tty: tty to flush
374 *
42fd552e
AC
375 * flush all the buffers containing receive data. Caller must
376 * hold the buffer lock and must have ensured no parallel flush to
377 * ldisc is running.
c5c34d48 378 *
42fd552e 379 * Locking: Caller must hold tty->buf.lock
c5c34d48
PF
380 */
381
42fd552e 382static void __tty_buffer_flush(struct tty_struct *tty)
c5c34d48
PF
383{
384 struct tty_buffer *thead;
c5c34d48 385
37bdfb07 386 while ((thead = tty->buf.head) != NULL) {
c5c34d48
PF
387 tty->buf.head = thead->next;
388 tty_buffer_free(tty, thead);
389 }
390 tty->buf.tail = NULL;
42fd552e
AC
391}
392
393/**
394 * tty_buffer_flush - flush full tty buffers
395 * @tty: tty to flush
396 *
397 * flush all the buffers containing receive data. If the buffer is
398 * being processed by flush_to_ldisc then we defer the processing
399 * to that function
400 *
401 * Locking: none
402 */
403
404static void tty_buffer_flush(struct tty_struct *tty)
405{
406 unsigned long flags;
407 spin_lock_irqsave(&tty->buf.lock, flags);
408
409 /* If the data is being pushed to the tty layer then we can't
410 process it here. Instead set a flag and the flush_to_ldisc
411 path will process the flush request before it exits */
412 if (test_bit(TTY_FLUSHING, &tty->flags)) {
413 set_bit(TTY_FLUSHPENDING, &tty->flags);
414 spin_unlock_irqrestore(&tty->buf.lock, flags);
415 wait_event(tty->read_wait,
416 test_bit(TTY_FLUSHPENDING, &tty->flags) == 0);
417 return;
418 } else
419 __tty_buffer_flush(tty);
c5c34d48
PF
420 spin_unlock_irqrestore(&tty->buf.lock, flags);
421}
422
01da5fd8
AC
423/**
424 * tty_buffer_find - find a free tty buffer
425 * @tty: tty owning the buffer
426 * @size: characters wanted
427 *
428 * Locate an existing suitable tty buffer or if we are lacking one then
429 * allocate a new one. We round our buffers off in 256 character chunks
430 * to get better allocation behaviour.
431 *
432 * Locking: Caller must hold tty->buf.lock
433 */
434
33f0f88f
AC
435static struct tty_buffer *tty_buffer_find(struct tty_struct *tty, size_t size)
436{
437 struct tty_buffer **tbh = &tty->buf.free;
37bdfb07 438 while ((*tbh) != NULL) {
33f0f88f 439 struct tty_buffer *t = *tbh;
37bdfb07 440 if (t->size >= size) {
33f0f88f
AC
441 *tbh = t->next;
442 t->next = NULL;
443 t->used = 0;
8977d929
PF
444 t->commit = 0;
445 t->read = 0;
01da5fd8 446 tty->buf.memory_used += t->size;
33f0f88f
AC
447 return t;
448 }
449 tbh = &((*tbh)->next);
450 }
451 /* Round the buffer size out */
37bdfb07 452 size = (size + 0xFF) & ~0xFF;
01da5fd8 453 return tty_buffer_alloc(tty, size);
33f0f88f
AC
454 /* Should possibly check if this fails for the largest buffer we
455 have queued and recycle that ? */
456}
457
01da5fd8
AC
458/**
459 * tty_buffer_request_room - grow tty buffer if needed
460 * @tty: tty structure
461 * @size: size desired
462 *
463 * Make at least size bytes of linear space available for the tty
464 * buffer. If we fail return the size we managed to find.
465 *
466 * Locking: Takes tty->buf.lock
467 */
33f0f88f
AC
468int tty_buffer_request_room(struct tty_struct *tty, size_t size)
469{
808249ce
PF
470 struct tty_buffer *b, *n;
471 int left;
472 unsigned long flags;
473
474 spin_lock_irqsave(&tty->buf.lock, flags);
33f0f88f
AC
475
476 /* OPTIMISATION: We could keep a per tty "zero" sized buffer to
477 remove this conditional if its worth it. This would be invisible
478 to the callers */
33b37a33 479 if ((b = tty->buf.tail) != NULL)
33f0f88f 480 left = b->size - b->used;
33b37a33 481 else
808249ce
PF
482 left = 0;
483
484 if (left < size) {
485 /* This is the slow path - looking for new buffers to use */
486 if ((n = tty_buffer_find(tty, size)) != NULL) {
487 if (b != NULL) {
488 b->next = n;
8977d929 489 b->commit = b->used;
808249ce
PF
490 } else
491 tty->buf.head = n;
492 tty->buf.tail = n;
808249ce
PF
493 } else
494 size = left;
495 }
496
497 spin_unlock_irqrestore(&tty->buf.lock, flags);
33f0f88f
AC
498 return size;
499}
33f0f88f
AC
500EXPORT_SYMBOL_GPL(tty_buffer_request_room);
501
af9b897e
AC
502/**
503 * tty_insert_flip_string - Add characters to the tty buffer
504 * @tty: tty structure
505 * @chars: characters
506 * @size: size
507 *
508 * Queue a series of bytes to the tty buffering. All the characters
509 * passed are marked as without error. Returns the number added.
510 *
511 * Locking: Called functions may take tty->buf.lock
512 */
513
e1a25090
AM
514int tty_insert_flip_string(struct tty_struct *tty, const unsigned char *chars,
515 size_t size)
33f0f88f
AC
516{
517 int copied = 0;
518 do {
519 int space = tty_buffer_request_room(tty, size - copied);
520 struct tty_buffer *tb = tty->buf.tail;
521 /* If there is no space then tb may be NULL */
37bdfb07 522 if (unlikely(space == 0))
33f0f88f
AC
523 break;
524 memcpy(tb->char_buf_ptr + tb->used, chars, space);
525 memset(tb->flag_buf_ptr + tb->used, TTY_NORMAL, space);
526 tb->used += space;
527 copied += space;
528 chars += space;
527063ba
AD
529 /* There is a small chance that we need to split the data over
530 several buffers. If this is the case we must loop */
531 } while (unlikely(size > copied));
33f0f88f
AC
532 return copied;
533}
ee37df78 534EXPORT_SYMBOL(tty_insert_flip_string);
33f0f88f 535
af9b897e
AC
536/**
537 * tty_insert_flip_string_flags - Add characters to the tty buffer
538 * @tty: tty structure
539 * @chars: characters
540 * @flags: flag bytes
541 * @size: size
542 *
543 * Queue a series of bytes to the tty buffering. For each character
544 * the flags array indicates the status of the character. Returns the
545 * number added.
546 *
547 * Locking: Called functions may take tty->buf.lock
548 */
549
e1a25090
AM
550int tty_insert_flip_string_flags(struct tty_struct *tty,
551 const unsigned char *chars, const char *flags, size_t size)
33f0f88f
AC
552{
553 int copied = 0;
554 do {
555 int space = tty_buffer_request_room(tty, size - copied);
556 struct tty_buffer *tb = tty->buf.tail;
557 /* If there is no space then tb may be NULL */
37bdfb07 558 if (unlikely(space == 0))
33f0f88f
AC
559 break;
560 memcpy(tb->char_buf_ptr + tb->used, chars, space);
561 memcpy(tb->flag_buf_ptr + tb->used, flags, space);
562 tb->used += space;
563 copied += space;
564 chars += space;
565 flags += space;
527063ba
AD
566 /* There is a small chance that we need to split the data over
567 several buffers. If this is the case we must loop */
568 } while (unlikely(size > copied));
33f0f88f
AC
569 return copied;
570}
ff4547f4 571EXPORT_SYMBOL(tty_insert_flip_string_flags);
33f0f88f 572
af9b897e
AC
573/**
574 * tty_schedule_flip - push characters to ldisc
575 * @tty: tty to push from
576 *
577 * Takes any pending buffers and transfers their ownership to the
578 * ldisc side of the queue. It then schedules those characters for
579 * processing by the line discipline.
580 *
581 * Locking: Takes tty->buf.lock
582 */
583
e1a25090
AM
584void tty_schedule_flip(struct tty_struct *tty)
585{
586 unsigned long flags;
587 spin_lock_irqsave(&tty->buf.lock, flags);
33b37a33 588 if (tty->buf.tail != NULL)
e1a25090 589 tty->buf.tail->commit = tty->buf.tail->used;
e1a25090
AM
590 spin_unlock_irqrestore(&tty->buf.lock, flags);
591 schedule_delayed_work(&tty->buf.work, 1);
592}
593EXPORT_SYMBOL(tty_schedule_flip);
33f0f88f 594
af9b897e
AC
595/**
596 * tty_prepare_flip_string - make room for characters
597 * @tty: tty
598 * @chars: return pointer for character write area
599 * @size: desired size
600 *
33f0f88f
AC
601 * Prepare a block of space in the buffer for data. Returns the length
602 * available and buffer pointer to the space which is now allocated and
603 * accounted for as ready for normal characters. This is used for drivers
604 * that need their own block copy routines into the buffer. There is no
605 * guarantee the buffer is a DMA target!
af9b897e
AC
606 *
607 * Locking: May call functions taking tty->buf.lock
33f0f88f
AC
608 */
609
37bdfb07
AC
610int tty_prepare_flip_string(struct tty_struct *tty, unsigned char **chars,
611 size_t size)
33f0f88f
AC
612{
613 int space = tty_buffer_request_room(tty, size);
808249ce
PF
614 if (likely(space)) {
615 struct tty_buffer *tb = tty->buf.tail;
616 *chars = tb->char_buf_ptr + tb->used;
617 memset(tb->flag_buf_ptr + tb->used, TTY_NORMAL, space);
618 tb->used += space;
619 }
33f0f88f
AC
620 return space;
621}
622
623EXPORT_SYMBOL_GPL(tty_prepare_flip_string);
624
af9b897e
AC
625/**
626 * tty_prepare_flip_string_flags - make room for characters
627 * @tty: tty
628 * @chars: return pointer for character write area
629 * @flags: return pointer for status flag write area
630 * @size: desired size
631 *
33f0f88f
AC
632 * Prepare a block of space in the buffer for data. Returns the length
633 * available and buffer pointer to the space which is now allocated and
634 * accounted for as ready for characters. This is used for drivers
635 * that need their own block copy routines into the buffer. There is no
636 * guarantee the buffer is a DMA target!
af9b897e
AC
637 *
638 * Locking: May call functions taking tty->buf.lock
33f0f88f
AC
639 */
640
37bdfb07
AC
641int tty_prepare_flip_string_flags(struct tty_struct *tty,
642 unsigned char **chars, char **flags, size_t size)
33f0f88f
AC
643{
644 int space = tty_buffer_request_room(tty, size);
808249ce
PF
645 if (likely(space)) {
646 struct tty_buffer *tb = tty->buf.tail;
647 *chars = tb->char_buf_ptr + tb->used;
648 *flags = tb->flag_buf_ptr + tb->used;
649 tb->used += space;
650 }
33f0f88f
AC
651 return space;
652}
653
654EXPORT_SYMBOL_GPL(tty_prepare_flip_string_flags);
655
656
657
af9b897e
AC
658/**
659 * tty_set_termios_ldisc - set ldisc field
660 * @tty: tty structure
661 * @num: line discipline number
662 *
1da177e4 663 * This is probably overkill for real world processors but
37bdfb07 664 * they are not on hot paths so a little discipline won't do
1da177e4 665 * any harm.
af9b897e 666 *
24ec839c 667 * Locking: takes termios_mutex
1da177e4 668 */
37bdfb07 669
1da177e4
LT
670static void tty_set_termios_ldisc(struct tty_struct *tty, int num)
671{
5785c95b 672 mutex_lock(&tty->termios_mutex);
1da177e4 673 tty->termios->c_line = num;
5785c95b 674 mutex_unlock(&tty->termios_mutex);
1da177e4
LT
675}
676
677/*
678 * This guards the refcounted line discipline lists. The lock
679 * must be taken with irqs off because there are hangup path
680 * callers who will do ldisc lookups and cannot sleep.
681 */
37bdfb07 682
1da177e4
LT
683static DEFINE_SPINLOCK(tty_ldisc_lock);
684static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_wait);
37bdfb07 685/* Line disc dispatch table */
a352def2 686static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS];
1da177e4 687
af9b897e
AC
688/**
689 * tty_register_ldisc - install a line discipline
690 * @disc: ldisc number
691 * @new_ldisc: pointer to the ldisc object
692 *
693 * Installs a new line discipline into the kernel. The discipline
694 * is set up as unreferenced and then made available to the kernel
695 * from this point onwards.
696 *
697 * Locking:
698 * takes tty_ldisc_lock to guard against ldisc races
699 */
700
a352def2 701int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc)
1da177e4
LT
702{
703 unsigned long flags;
704 int ret = 0;
37bdfb07 705
1da177e4
LT
706 if (disc < N_TTY || disc >= NR_LDISCS)
707 return -EINVAL;
37bdfb07 708
1da177e4 709 spin_lock_irqsave(&tty_ldisc_lock, flags);
a352def2
AC
710 tty_ldiscs[disc] = new_ldisc;
711 new_ldisc->num = disc;
712 new_ldisc->refcount = 0;
1da177e4 713 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
37bdfb07 714
1da177e4
LT
715 return ret;
716}
1da177e4
LT
717EXPORT_SYMBOL(tty_register_ldisc);
718
af9b897e
AC
719/**
720 * tty_unregister_ldisc - unload a line discipline
721 * @disc: ldisc number
722 * @new_ldisc: pointer to the ldisc object
723 *
724 * Remove a line discipline from the kernel providing it is not
725 * currently in use.
726 *
727 * Locking:
728 * takes tty_ldisc_lock to guard against ldisc races
729 */
730
bfb07599
AD
731int tty_unregister_ldisc(int disc)
732{
733 unsigned long flags;
734 int ret = 0;
735
736 if (disc < N_TTY || disc >= NR_LDISCS)
737 return -EINVAL;
738
739 spin_lock_irqsave(&tty_ldisc_lock, flags);
a352def2 740 if (tty_ldiscs[disc]->refcount)
bfb07599
AD
741 ret = -EBUSY;
742 else
a352def2 743 tty_ldiscs[disc] = NULL;
bfb07599
AD
744 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
745
746 return ret;
747}
748EXPORT_SYMBOL(tty_unregister_ldisc);
749
a352def2
AC
750
751/**
752 * tty_ldisc_try_get - try and reference an ldisc
753 * @disc: ldisc number
754 * @ld: tty ldisc structure to complete
755 *
756 * Attempt to open and lock a line discipline into place. Return
757 * the line discipline refcounted and assigned in ld. On an error
758 * report the error code back
759 */
760
761static int tty_ldisc_try_get(int disc, struct tty_ldisc *ld)
762{
763 unsigned long flags;
764 struct tty_ldisc_ops *ldops;
765 int err = -EINVAL;
766
767 spin_lock_irqsave(&tty_ldisc_lock, flags);
768 ld->ops = NULL;
769 ldops = tty_ldiscs[disc];
770 /* Check the entry is defined */
771 if (ldops) {
772 /* If the module is being unloaded we can't use it */
773 if (!try_module_get(ldops->owner))
774 err = -EAGAIN;
775 else {
776 /* lock it */
777 ldops->refcount++;
778 ld->ops = ldops;
779 err = 0;
780 }
781 }
782 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
783 return err;
784}
785
af9b897e
AC
786/**
787 * tty_ldisc_get - take a reference to an ldisc
788 * @disc: ldisc number
a352def2 789 * @ld: tty line discipline structure to use
af9b897e
AC
790 *
791 * Takes a reference to a line discipline. Deals with refcounts and
792 * module locking counts. Returns NULL if the discipline is not available.
793 * Returns a pointer to the discipline and bumps the ref count if it is
794 * available
795 *
796 * Locking:
797 * takes tty_ldisc_lock to guard against ldisc races
798 */
799
a352def2 800static int tty_ldisc_get(int disc, struct tty_ldisc *ld)
1da177e4 801{
a352def2 802 int err;
1da177e4
LT
803
804 if (disc < N_TTY || disc >= NR_LDISCS)
a352def2
AC
805 return -EINVAL;
806 err = tty_ldisc_try_get(disc, ld);
807 if (err == -EAGAIN) {
808 request_module("tty-ldisc-%d", disc);
809 err = tty_ldisc_try_get(disc, ld);
810 }
811 return err;
1da177e4
LT
812}
813
af9b897e
AC
814/**
815 * tty_ldisc_put - drop ldisc reference
816 * @disc: ldisc number
817 *
818 * Drop a reference to a line discipline. Manage refcounts and
819 * module usage counts
820 *
821 * Locking:
822 * takes tty_ldisc_lock to guard against ldisc races
823 */
824
a352def2 825static void tty_ldisc_put(struct tty_ldisc_ops *ld)
1da177e4 826{
1da177e4 827 unsigned long flags;
a352def2 828 int disc = ld->num;
37bdfb07 829
56ee4827 830 BUG_ON(disc < N_TTY || disc >= NR_LDISCS);
37bdfb07 831
1da177e4 832 spin_lock_irqsave(&tty_ldisc_lock, flags);
a352def2 833 ld = tty_ldiscs[disc];
56ee4827
ES
834 BUG_ON(ld->refcount == 0);
835 ld->refcount--;
1da177e4
LT
836 module_put(ld->owner);
837 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
838}
37bdfb07 839
a352def2
AC
840static void * tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos)
841{
842 return (*pos < NR_LDISCS) ? pos : NULL;
843}
844
845static void * tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos)
846{
847 (*pos)++;
848 return (*pos < NR_LDISCS) ? pos : NULL;
849}
850
851static void tty_ldiscs_seq_stop(struct seq_file *m, void *v)
852{
853}
854
855static int tty_ldiscs_seq_show(struct seq_file *m, void *v)
856{
857 int i = *(loff_t *)v;
858 struct tty_ldisc ld;
859
860 if (tty_ldisc_get(i, &ld) < 0)
861 return 0;
862 seq_printf(m, "%-10s %2d\n", ld.ops->name ? ld.ops->name : "???", i);
863 tty_ldisc_put(ld.ops);
864 return 0;
865}
866
867static const struct seq_operations tty_ldiscs_seq_ops = {
868 .start = tty_ldiscs_seq_start,
869 .next = tty_ldiscs_seq_next,
870 .stop = tty_ldiscs_seq_stop,
871 .show = tty_ldiscs_seq_show,
872};
873
874static int proc_tty_ldiscs_open(struct inode *inode, struct file *file)
875{
876 return seq_open(file, &tty_ldiscs_seq_ops);
877}
878
879const struct file_operations tty_ldiscs_proc_fops = {
880 .owner = THIS_MODULE,
881 .open = proc_tty_ldiscs_open,
882 .read = seq_read,
883 .llseek = seq_lseek,
884 .release = seq_release,
885};
1da177e4 886
af9b897e
AC
887/**
888 * tty_ldisc_assign - set ldisc on a tty
889 * @tty: tty to assign
890 * @ld: line discipline
891 *
892 * Install an instance of a line discipline into a tty structure. The
893 * ldisc must have a reference count above zero to ensure it remains/
894 * The tty instance refcount starts at zero.
895 *
896 * Locking:
897 * Caller must hold references
898 */
899
1da177e4
LT
900static void tty_ldisc_assign(struct tty_struct *tty, struct tty_ldisc *ld)
901{
a352def2 902 ld->refcount = 0;
1da177e4 903 tty->ldisc = *ld;
1da177e4
LT
904}
905
906/**
907 * tty_ldisc_try - internal helper
908 * @tty: the tty
909 *
910 * Make a single attempt to grab and bump the refcount on
911 * the tty ldisc. Return 0 on failure or 1 on success. This is
912 * used to implement both the waiting and non waiting versions
913 * of tty_ldisc_ref
af9b897e
AC
914 *
915 * Locking: takes tty_ldisc_lock
1da177e4
LT
916 */
917
918static int tty_ldisc_try(struct tty_struct *tty)
919{
920 unsigned long flags;
921 struct tty_ldisc *ld;
922 int ret = 0;
37bdfb07 923
1da177e4
LT
924 spin_lock_irqsave(&tty_ldisc_lock, flags);
925 ld = &tty->ldisc;
37bdfb07 926 if (test_bit(TTY_LDISC, &tty->flags)) {
1da177e4
LT
927 ld->refcount++;
928 ret = 1;
929 }
930 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
931 return ret;
932}
933
934/**
935 * tty_ldisc_ref_wait - wait for the tty ldisc
936 * @tty: tty device
937 *
37bdfb07
AC
938 * Dereference the line discipline for the terminal and take a
939 * reference to it. If the line discipline is in flux then
1da177e4
LT
940 * wait patiently until it changes.
941 *
942 * Note: Must not be called from an IRQ/timer context. The caller
943 * must also be careful not to hold other locks that will deadlock
944 * against a discipline change, such as an existing ldisc reference
945 * (which we check for)
af9b897e
AC
946 *
947 * Locking: call functions take tty_ldisc_lock
1da177e4 948 */
37bdfb07 949
1da177e4
LT
950struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
951{
952 /* wait_event is a macro */
953 wait_event(tty_ldisc_wait, tty_ldisc_try(tty));
37bdfb07 954 if (tty->ldisc.refcount == 0)
1da177e4
LT
955 printk(KERN_ERR "tty_ldisc_ref_wait\n");
956 return &tty->ldisc;
957}
958
959EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
960
961/**
962 * tty_ldisc_ref - get the tty ldisc
963 * @tty: tty device
964 *
37bdfb07
AC
965 * Dereference the line discipline for the terminal and take a
966 * reference to it. If the line discipline is in flux then
1da177e4 967 * return NULL. Can be called from IRQ and timer functions.
af9b897e
AC
968 *
969 * Locking: called functions take tty_ldisc_lock
1da177e4 970 */
37bdfb07 971
1da177e4
LT
972struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
973{
37bdfb07 974 if (tty_ldisc_try(tty))
1da177e4
LT
975 return &tty->ldisc;
976 return NULL;
977}
978
979EXPORT_SYMBOL_GPL(tty_ldisc_ref);
980
981/**
982 * tty_ldisc_deref - free a tty ldisc reference
983 * @ld: reference to free up
984 *
985 * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May
986 * be called in IRQ context.
af9b897e
AC
987 *
988 * Locking: takes tty_ldisc_lock
1da177e4 989 */
37bdfb07 990
1da177e4
LT
991void tty_ldisc_deref(struct tty_ldisc *ld)
992{
993 unsigned long flags;
994
56ee4827 995 BUG_ON(ld == NULL);
37bdfb07 996
1da177e4 997 spin_lock_irqsave(&tty_ldisc_lock, flags);
37bdfb07 998 if (ld->refcount == 0)
1da177e4
LT
999 printk(KERN_ERR "tty_ldisc_deref: no references.\n");
1000 else
1001 ld->refcount--;
37bdfb07 1002 if (ld->refcount == 0)
1da177e4
LT
1003 wake_up(&tty_ldisc_wait);
1004 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
1005}
1006
1007EXPORT_SYMBOL_GPL(tty_ldisc_deref);
1008
1009/**
1010 * tty_ldisc_enable - allow ldisc use
1011 * @tty: terminal to activate ldisc on
1012 *
1013 * Set the TTY_LDISC flag when the line discipline can be called
3a4fa0a2 1014 * again. Do necessary wakeups for existing sleepers.
1da177e4
LT
1015 *
1016 * Note: nobody should set this bit except via this function. Clearing
1017 * directly is allowed.
1018 */
1019
1020static void tty_ldisc_enable(struct tty_struct *tty)
1021{
1022 set_bit(TTY_LDISC, &tty->flags);
1023 wake_up(&tty_ldisc_wait);
1024}
37bdfb07 1025
a352def2
AC
1026/**
1027 * tty_ldisc_restore - helper for tty ldisc change
1028 * @tty: tty to recover
1029 * @old: previous ldisc
1030 *
1031 * Restore the previous line discipline or N_TTY when a line discipline
1032 * change fails due to an open error
1033 */
1034
1035static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old)
1036{
1037 char buf[64];
1038 struct tty_ldisc new_ldisc;
1039
1040 /* There is an outstanding reference here so this is safe */
1041 tty_ldisc_get(old->ops->num, old);
1042 tty_ldisc_assign(tty, old);
1043 tty_set_termios_ldisc(tty, old->ops->num);
1044 if (old->ops->open && (old->ops->open(tty) < 0)) {
1045 tty_ldisc_put(old->ops);
1046 /* This driver is always present */
1047 if (tty_ldisc_get(N_TTY, &new_ldisc) < 0)
1048 panic("n_tty: get");
1049 tty_ldisc_assign(tty, &new_ldisc);
1050 tty_set_termios_ldisc(tty, N_TTY);
1051 if (new_ldisc.ops->open) {
1052 int r = new_ldisc.ops->open(tty);
1053 if (r < 0)
1054 panic("Couldn't open N_TTY ldisc for "
1055 "%s --- error %d.",
1056 tty_name(tty, buf), r);
1057 }
1058 }
1059}
1060
1da177e4
LT
1061/**
1062 * tty_set_ldisc - set line discipline
1063 * @tty: the terminal to set
1064 * @ldisc: the line discipline
1065 *
1066 * Set the discipline of a tty line. Must be called from a process
1067 * context.
af9b897e
AC
1068 *
1069 * Locking: takes tty_ldisc_lock.
24ec839c 1070 * called functions take termios_mutex
1da177e4 1071 */
37bdfb07 1072
1da177e4
LT
1073static int tty_set_ldisc(struct tty_struct *tty, int ldisc)
1074{
a352def2
AC
1075 int retval;
1076 struct tty_ldisc o_ldisc, new_ldisc;
1da177e4
LT
1077 int work;
1078 unsigned long flags;
ff55fe20 1079 struct tty_struct *o_tty;
1da177e4 1080
1da177e4 1081restart:
a352def2
AC
1082 /* This is a bit ugly for now but means we can break the 'ldisc
1083 is part of the tty struct' assumption later */
1084 retval = tty_ldisc_get(ldisc, &new_ldisc);
1085 if (retval)
1086 return retval;
1da177e4 1087
33f0f88f
AC
1088 /*
1089 * Problem: What do we do if this blocks ?
1090 */
1091
1da177e4
LT
1092 tty_wait_until_sent(tty, 0);
1093
a352def2
AC
1094 if (tty->ldisc.ops->num == ldisc) {
1095 tty_ldisc_put(new_ldisc.ops);
ff55fe20
JB
1096 return 0;
1097 }
1098
ae030e43
PF
1099 /*
1100 * No more input please, we are switching. The new ldisc
1101 * will update this value in the ldisc open function
1102 */
1103
1104 tty->receive_room = 0;
1105
ff55fe20
JB
1106 o_ldisc = tty->ldisc;
1107 o_tty = tty->link;
1108
1da177e4
LT
1109 /*
1110 * Make sure we don't change while someone holds a
1111 * reference to the line discipline. The TTY_LDISC bit
1112 * prevents anyone taking a reference once it is clear.
1113 * We need the lock to avoid racing reference takers.
1114 */
ff55fe20 1115
1da177e4 1116 spin_lock_irqsave(&tty_ldisc_lock, flags);
ff55fe20 1117 if (tty->ldisc.refcount || (o_tty && o_tty->ldisc.refcount)) {
37bdfb07 1118 if (tty->ldisc.refcount) {
ff55fe20
JB
1119 /* Free the new ldisc we grabbed. Must drop the lock
1120 first. */
1121 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
a352def2 1122 tty_ldisc_put(o_ldisc.ops);
ff55fe20
JB
1123 /*
1124 * There are several reasons we may be busy, including
1125 * random momentary I/O traffic. We must therefore
1126 * retry. We could distinguish between blocking ops
37bdfb07
AC
1127 * and retries if we made tty_ldisc_wait() smarter.
1128 * That is up for discussion.
ff55fe20
JB
1129 */
1130 if (wait_event_interruptible(tty_ldisc_wait, tty->ldisc.refcount == 0) < 0)
1131 return -ERESTARTSYS;
1132 goto restart;
1133 }
37bdfb07 1134 if (o_tty && o_tty->ldisc.refcount) {
ff55fe20 1135 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
a352def2 1136 tty_ldisc_put(o_tty->ldisc.ops);
ff55fe20
JB
1137 if (wait_event_interruptible(tty_ldisc_wait, o_tty->ldisc.refcount == 0) < 0)
1138 return -ERESTARTSYS;
1139 goto restart;
1140 }
1141 }
37bdfb07
AC
1142 /*
1143 * If the TTY_LDISC bit is set, then we are racing against
1144 * another ldisc change
1145 */
ff55fe20 1146 if (!test_bit(TTY_LDISC, &tty->flags)) {
a352def2 1147 struct tty_ldisc *ld;
1da177e4 1148 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
a352def2 1149 tty_ldisc_put(new_ldisc.ops);
ff55fe20
JB
1150 ld = tty_ldisc_ref_wait(tty);
1151 tty_ldisc_deref(ld);
1da177e4
LT
1152 goto restart;
1153 }
ff55fe20
JB
1154
1155 clear_bit(TTY_LDISC, &tty->flags);
817d6d3b 1156 if (o_tty)
ff55fe20 1157 clear_bit(TTY_LDISC, &o_tty->flags);
1da177e4 1158 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
a352def2 1159
1da177e4
LT
1160 /*
1161 * From this point on we know nobody has an ldisc
1162 * usage reference, nor can they obtain one until
1163 * we say so later on.
1164 */
ff55fe20 1165
33f0f88f 1166 work = cancel_delayed_work(&tty->buf.work);
1da177e4 1167 /*
33f0f88f 1168 * Wait for ->hangup_work and ->buf.work handlers to terminate
a352def2 1169 * MUST NOT hold locks here.
1da177e4 1170 */
1da177e4
LT
1171 flush_scheduled_work();
1172 /* Shutdown the current discipline. */
a352def2
AC
1173 if (o_ldisc.ops->close)
1174 (o_ldisc.ops->close)(tty);
1da177e4
LT
1175
1176 /* Now set up the new line discipline. */
a352def2 1177 tty_ldisc_assign(tty, &new_ldisc);
1da177e4 1178 tty_set_termios_ldisc(tty, ldisc);
a352def2
AC
1179 if (new_ldisc.ops->open)
1180 retval = (new_ldisc.ops->open)(tty);
1da177e4 1181 if (retval < 0) {
a352def2
AC
1182 tty_ldisc_put(new_ldisc.ops);
1183 tty_ldisc_restore(tty, &o_ldisc);
1da177e4
LT
1184 }
1185 /* At this point we hold a reference to the new ldisc and a
1186 a reference to the old ldisc. If we ended up flipping back
1187 to the existing ldisc we have two references to it */
37bdfb07 1188
a352def2 1189 if (tty->ldisc.ops->num != o_ldisc.ops->num && tty->ops->set_ldisc)
f34d7a5b 1190 tty->ops->set_ldisc(tty);
37bdfb07 1191
a352def2 1192 tty_ldisc_put(o_ldisc.ops);
37bdfb07 1193
1da177e4
LT
1194 /*
1195 * Allow ldisc referencing to occur as soon as the driver
1196 * ldisc callback completes.
1197 */
37bdfb07 1198
1da177e4 1199 tty_ldisc_enable(tty);
ff55fe20
JB
1200 if (o_tty)
1201 tty_ldisc_enable(o_tty);
37bdfb07 1202
1da177e4
LT
1203 /* Restart it in case no characters kick it off. Safe if
1204 already running */
ff55fe20 1205 if (work)
33f0f88f 1206 schedule_delayed_work(&tty->buf.work, 1);
1da177e4
LT
1207 return retval;
1208}
1209
af9b897e
AC
1210/**
1211 * get_tty_driver - find device of a tty
1212 * @dev_t: device identifier
1213 * @index: returns the index of the tty
1214 *
1215 * This routine returns a tty driver structure, given a device number
1216 * and also passes back the index number.
1217 *
1218 * Locking: caller must hold tty_mutex
1da177e4 1219 */
af9b897e 1220
1da177e4
LT
1221static struct tty_driver *get_tty_driver(dev_t device, int *index)
1222{
1223 struct tty_driver *p;
1224
1225 list_for_each_entry(p, &tty_drivers, tty_drivers) {
1226 dev_t base = MKDEV(p->major, p->minor_start);
1227 if (device < base || device >= base + p->num)
1228 continue;
1229 *index = device - base;
1230 return p;
1231 }
1232 return NULL;
1233}
1234
f2d937f3
JW
1235#ifdef CONFIG_CONSOLE_POLL
1236
1237/**
1238 * tty_find_polling_driver - find device of a polled tty
1239 * @name: name string to match
1240 * @line: pointer to resulting tty line nr
1241 *
1242 * This routine returns a tty driver structure, given a name
1243 * and the condition that the tty driver is capable of polled
1244 * operation.
1245 */
1246struct tty_driver *tty_find_polling_driver(char *name, int *line)
1247{
1248 struct tty_driver *p, *res = NULL;
1249 int tty_line = 0;
1250 char *str;
1251
1252 mutex_lock(&tty_mutex);
1253 /* Search through the tty devices to look for a match */
1254 list_for_each_entry(p, &tty_drivers, tty_drivers) {
1255 str = name + strlen(p->name);
1256 tty_line = simple_strtoul(str, &str, 10);
1257 if (*str == ',')
1258 str++;
1259 if (*str == '\0')
8da56309 1260 str = NULL;
f2d937f3 1261
f34d7a5b
AC
1262 if (tty_line >= 0 && tty_line <= p->num && p->ops &&
1263 p->ops->poll_init && !p->ops->poll_init(p, tty_line, str)) {
f2d937f3
JW
1264 res = p;
1265 *line = tty_line;
1266 break;
1267 }
1268 }
1269 mutex_unlock(&tty_mutex);
1270
1271 return res;
1272}
1273EXPORT_SYMBOL_GPL(tty_find_polling_driver);
1274#endif
1275
af9b897e
AC
1276/**
1277 * tty_check_change - check for POSIX terminal changes
1278 * @tty: tty to check
1279 *
1280 * If we try to write to, or set the state of, a terminal and we're
1281 * not in the foreground, send a SIGTTOU. If the signal is blocked or
1282 * ignored, go ahead and perform the operation. (POSIX 7.2)
1283 *
978e595f 1284 * Locking: ctrl_lock
1da177e4 1285 */
af9b897e 1286
37bdfb07 1287int tty_check_change(struct tty_struct *tty)
1da177e4 1288{
47f86834
AC
1289 unsigned long flags;
1290 int ret = 0;
1291
1da177e4
LT
1292 if (current->signal->tty != tty)
1293 return 0;
47f86834
AC
1294
1295 spin_lock_irqsave(&tty->ctrl_lock, flags);
1296
ab521dc0
EB
1297 if (!tty->pgrp) {
1298 printk(KERN_WARNING "tty_check_change: tty->pgrp == NULL!\n");
9ffee4cb 1299 goto out_unlock;
1da177e4 1300 }
ab521dc0 1301 if (task_pgrp(current) == tty->pgrp)
9ffee4cb
AM
1302 goto out_unlock;
1303 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
1da177e4 1304 if (is_ignored(SIGTTOU))
47f86834
AC
1305 goto out;
1306 if (is_current_pgrp_orphaned()) {
1307 ret = -EIO;
1308 goto out;
1309 }
040b6362
ON
1310 kill_pgrp(task_pgrp(current), SIGTTOU, 1);
1311 set_thread_flag(TIF_SIGPENDING);
47f86834
AC
1312 ret = -ERESTARTSYS;
1313out:
9ffee4cb
AM
1314 return ret;
1315out_unlock:
47f86834
AC
1316 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
1317 return ret;
1da177e4
LT
1318}
1319
1320EXPORT_SYMBOL(tty_check_change);
1321
37bdfb07 1322static ssize_t hung_up_tty_read(struct file *file, char __user *buf,
1da177e4
LT
1323 size_t count, loff_t *ppos)
1324{
1325 return 0;
1326}
1327
37bdfb07 1328static ssize_t hung_up_tty_write(struct file *file, const char __user *buf,
1da177e4
LT
1329 size_t count, loff_t *ppos)
1330{
1331 return -EIO;
1332}
1333
1334/* No kernel lock held - none needed ;) */
37bdfb07 1335static unsigned int hung_up_tty_poll(struct file *filp, poll_table *wait)
1da177e4
LT
1336{
1337 return POLLIN | POLLOUT | POLLERR | POLLHUP | POLLRDNORM | POLLWRNORM;
1338}
1339
04f378b1
AC
1340static long hung_up_tty_ioctl(struct file *file, unsigned int cmd,
1341 unsigned long arg)
38ad2ed0
PF
1342{
1343 return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
1344}
1345
37bdfb07 1346static long hung_up_tty_compat_ioctl(struct file *file,
38ad2ed0 1347 unsigned int cmd, unsigned long arg)
1da177e4
LT
1348{
1349 return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
1350}
1351
62322d25 1352static const struct file_operations tty_fops = {
1da177e4
LT
1353 .llseek = no_llseek,
1354 .read = tty_read,
1355 .write = tty_write,
1356 .poll = tty_poll,
04f378b1 1357 .unlocked_ioctl = tty_ioctl,
e10cc1df 1358 .compat_ioctl = tty_compat_ioctl,
1da177e4
LT
1359 .open = tty_open,
1360 .release = tty_release,
1361 .fasync = tty_fasync,
1362};
1363
1364#ifdef CONFIG_UNIX98_PTYS
62322d25 1365static const struct file_operations ptmx_fops = {
1da177e4
LT
1366 .llseek = no_llseek,
1367 .read = tty_read,
1368 .write = tty_write,
1369 .poll = tty_poll,
04f378b1 1370 .unlocked_ioctl = tty_ioctl,
e10cc1df 1371 .compat_ioctl = tty_compat_ioctl,
1da177e4
LT
1372 .open = ptmx_open,
1373 .release = tty_release,
1374 .fasync = tty_fasync,
1375};
1376#endif
1377
62322d25 1378static const struct file_operations console_fops = {
1da177e4
LT
1379 .llseek = no_llseek,
1380 .read = tty_read,
1381 .write = redirected_tty_write,
1382 .poll = tty_poll,
04f378b1 1383 .unlocked_ioctl = tty_ioctl,
e10cc1df 1384 .compat_ioctl = tty_compat_ioctl,
1da177e4
LT
1385 .open = tty_open,
1386 .release = tty_release,
1387 .fasync = tty_fasync,
1388};
1389
62322d25 1390static const struct file_operations hung_up_tty_fops = {
1da177e4
LT
1391 .llseek = no_llseek,
1392 .read = hung_up_tty_read,
1393 .write = hung_up_tty_write,
1394 .poll = hung_up_tty_poll,
04f378b1 1395 .unlocked_ioctl = hung_up_tty_ioctl,
38ad2ed0 1396 .compat_ioctl = hung_up_tty_compat_ioctl,
1da177e4
LT
1397 .release = tty_release,
1398};
1399
1400static DEFINE_SPINLOCK(redirect_lock);
1401static struct file *redirect;
1402
1403/**
1404 * tty_wakeup - request more data
1405 * @tty: terminal
1406 *
1407 * Internal and external helper for wakeups of tty. This function
1408 * informs the line discipline if present that the driver is ready
1409 * to receive more output data.
1410 */
37bdfb07 1411
1da177e4
LT
1412void tty_wakeup(struct tty_struct *tty)
1413{
1414 struct tty_ldisc *ld;
37bdfb07 1415
1da177e4
LT
1416 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) {
1417 ld = tty_ldisc_ref(tty);
37bdfb07 1418 if (ld) {
a352def2
AC
1419 if (ld->ops->write_wakeup)
1420 ld->ops->write_wakeup(tty);
1da177e4
LT
1421 tty_ldisc_deref(ld);
1422 }
1423 }
1424 wake_up_interruptible(&tty->write_wait);
1425}
1426
1427EXPORT_SYMBOL_GPL(tty_wakeup);
1428
1429/**
1430 * tty_ldisc_flush - flush line discipline queue
1431 * @tty: tty
1432 *
1433 * Flush the line discipline queue (if any) for this tty. If there
1434 * is no line discipline active this is a no-op.
1435 */
37bdfb07 1436
1da177e4
LT
1437void tty_ldisc_flush(struct tty_struct *tty)
1438{
1439 struct tty_ldisc *ld = tty_ldisc_ref(tty);
37bdfb07 1440 if (ld) {
a352def2
AC
1441 if (ld->ops->flush_buffer)
1442 ld->ops->flush_buffer(tty);
1da177e4
LT
1443 tty_ldisc_deref(ld);
1444 }
c5c34d48 1445 tty_buffer_flush(tty);
1da177e4
LT
1446}
1447
1448EXPORT_SYMBOL_GPL(tty_ldisc_flush);
edc6afc5
AC
1449
1450/**
1451 * tty_reset_termios - reset terminal state
1452 * @tty: tty to reset
1453 *
1454 * Restore a terminal to the driver default state
1455 */
1456
1457static void tty_reset_termios(struct tty_struct *tty)
1458{
1459 mutex_lock(&tty->termios_mutex);
1460 *tty->termios = tty->driver->init_termios;
1461 tty->termios->c_ispeed = tty_termios_input_baud_rate(tty->termios);
1462 tty->termios->c_ospeed = tty_termios_baud_rate(tty->termios);
1463 mutex_unlock(&tty->termios_mutex);
1464}
37bdfb07 1465
af9b897e
AC
1466/**
1467 * do_tty_hangup - actual handler for hangup events
65f27f38 1468 * @work: tty device
af9b897e 1469 *
a352def2 1470k * This can be called by the "eventd" kernel thread. That is process
af9b897e
AC
1471 * synchronous but doesn't hold any locks, so we need to make sure we
1472 * have the appropriate locks for what we're doing.
1473 *
1474 * The hangup event clears any pending redirections onto the hung up
1475 * device. It ensures future writes will error and it does the needed
1476 * line discipline hangup and signal delivery. The tty object itself
1477 * remains intact.
1478 *
1479 * Locking:
1480 * BKL
24ec839c
PZ
1481 * redirect lock for undoing redirection
1482 * file list lock for manipulating list of ttys
1483 * tty_ldisc_lock from called functions
1484 * termios_mutex resetting termios data
1485 * tasklist_lock to walk task list for hangup event
1486 * ->siglock to protect ->signal/->sighand
1da177e4 1487 */
65f27f38 1488static void do_tty_hangup(struct work_struct *work)
1da177e4 1489{
65f27f38
DH
1490 struct tty_struct *tty =
1491 container_of(work, struct tty_struct, hangup_work);
37bdfb07 1492 struct file *cons_filp = NULL;
1da177e4
LT
1493 struct file *filp, *f = NULL;
1494 struct task_struct *p;
1495 struct tty_ldisc *ld;
1496 int closecount = 0, n;
47f86834 1497 unsigned long flags;
1da177e4
LT
1498
1499 if (!tty)
1500 return;
1501
1502 /* inuse_filps is protected by the single kernel lock */
1503 lock_kernel();
1504
1505 spin_lock(&redirect_lock);
1506 if (redirect && redirect->private_data == tty) {
1507 f = redirect;
1508 redirect = NULL;
1509 }
1510 spin_unlock(&redirect_lock);
37bdfb07 1511
1da177e4
LT
1512 check_tty_count(tty, "do_tty_hangup");
1513 file_list_lock();
1514 /* This breaks for file handles being sent over AF_UNIX sockets ? */
2f512016 1515 list_for_each_entry(filp, &tty->tty_files, f_u.fu_list) {
1da177e4
LT
1516 if (filp->f_op->write == redirected_tty_write)
1517 cons_filp = filp;
1518 if (filp->f_op->write != tty_write)
1519 continue;
1520 closecount++;
1521 tty_fasync(-1, filp, 0); /* can't block */
1522 filp->f_op = &hung_up_tty_fops;
1523 }
1524 file_list_unlock();
37bdfb07
AC
1525 /*
1526 * FIXME! What are the locking issues here? This may me overdoing
1527 * things... This question is especially important now that we've
1528 * removed the irqlock.
1529 */
1da177e4 1530 ld = tty_ldisc_ref(tty);
37bdfb07
AC
1531 if (ld != NULL) {
1532 /* We may have no line discipline at this point */
a352def2
AC
1533 if (ld->ops->flush_buffer)
1534 ld->ops->flush_buffer(tty);
f34d7a5b 1535 tty_driver_flush_buffer(tty);
1da177e4 1536 if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
a352def2
AC
1537 ld->ops->write_wakeup)
1538 ld->ops->write_wakeup(tty);
1539 if (ld->ops->hangup)
1540 ld->ops->hangup(tty);
1da177e4 1541 }
37bdfb07
AC
1542 /*
1543 * FIXME: Once we trust the LDISC code better we can wait here for
1544 * ldisc completion and fix the driver call race
1545 */
1da177e4
LT
1546 wake_up_interruptible(&tty->write_wait);
1547 wake_up_interruptible(&tty->read_wait);
1da177e4
LT
1548 /*
1549 * Shutdown the current line discipline, and reset it to
1550 * N_TTY.
1551 */
1552 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
edc6afc5 1553 tty_reset_termios(tty);
1da177e4
LT
1554 /* Defer ldisc switch */
1555 /* tty_deferred_ldisc_switch(N_TTY);
37bdfb07 1556
1da177e4
LT
1557 This should get done automatically when the port closes and
1558 tty_release is called */
37bdfb07 1559
1da177e4 1560 read_lock(&tasklist_lock);
ab521dc0
EB
1561 if (tty->session) {
1562 do_each_pid_task(tty->session, PIDTYPE_SID, p) {
24ec839c 1563 spin_lock_irq(&p->sighand->siglock);
1da177e4
LT
1564 if (p->signal->tty == tty)
1565 p->signal->tty = NULL;
24ec839c
PZ
1566 if (!p->signal->leader) {
1567 spin_unlock_irq(&p->sighand->siglock);
1da177e4 1568 continue;
24ec839c
PZ
1569 }
1570 __group_send_sig_info(SIGHUP, SEND_SIG_PRIV, p);
1571 __group_send_sig_info(SIGCONT, SEND_SIG_PRIV, p);
ab521dc0 1572 put_pid(p->signal->tty_old_pgrp); /* A noop */
47f86834 1573 spin_lock_irqsave(&tty->ctrl_lock, flags);
ab521dc0
EB
1574 if (tty->pgrp)
1575 p->signal->tty_old_pgrp = get_pid(tty->pgrp);
47f86834 1576 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
24ec839c 1577 spin_unlock_irq(&p->sighand->siglock);
ab521dc0 1578 } while_each_pid_task(tty->session, PIDTYPE_SID, p);
1da177e4
LT
1579 }
1580 read_unlock(&tasklist_lock);
1581
47f86834 1582 spin_lock_irqsave(&tty->ctrl_lock, flags);
1da177e4 1583 tty->flags = 0;
d9c1e9a8
EB
1584 put_pid(tty->session);
1585 put_pid(tty->pgrp);
ab521dc0
EB
1586 tty->session = NULL;
1587 tty->pgrp = NULL;
1da177e4 1588 tty->ctrl_status = 0;
47f86834
AC
1589 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
1590
1da177e4 1591 /*
37bdfb07
AC
1592 * If one of the devices matches a console pointer, we
1593 * cannot just call hangup() because that will cause
1594 * tty->count and state->count to go out of sync.
1595 * So we just call close() the right number of times.
1da177e4
LT
1596 */
1597 if (cons_filp) {
f34d7a5b 1598 if (tty->ops->close)
1da177e4 1599 for (n = 0; n < closecount; n++)
f34d7a5b
AC
1600 tty->ops->close(tty, cons_filp);
1601 } else if (tty->ops->hangup)
1602 (tty->ops->hangup)(tty);
37bdfb07
AC
1603 /*
1604 * We don't want to have driver/ldisc interactions beyond
1605 * the ones we did here. The driver layer expects no
1606 * calls after ->hangup() from the ldisc side. However we
1607 * can't yet guarantee all that.
1608 */
1da177e4
LT
1609 set_bit(TTY_HUPPED, &tty->flags);
1610 if (ld) {
1611 tty_ldisc_enable(tty);
1612 tty_ldisc_deref(ld);
1613 }
1614 unlock_kernel();
1615 if (f)
1616 fput(f);
1617}
1618
af9b897e
AC
1619/**
1620 * tty_hangup - trigger a hangup event
1621 * @tty: tty to hangup
1622 *
1623 * A carrier loss (virtual or otherwise) has occurred on this like
1624 * schedule a hangup sequence to run after this event.
1625 */
1626
37bdfb07 1627void tty_hangup(struct tty_struct *tty)
1da177e4
LT
1628{
1629#ifdef TTY_DEBUG_HANGUP
1630 char buf[64];
1da177e4
LT
1631 printk(KERN_DEBUG "%s hangup...\n", tty_name(tty, buf));
1632#endif
1633 schedule_work(&tty->hangup_work);
1634}
1635
1636EXPORT_SYMBOL(tty_hangup);
1637
af9b897e
AC
1638/**
1639 * tty_vhangup - process vhangup
1640 * @tty: tty to hangup
1641 *
1642 * The user has asked via system call for the terminal to be hung up.
1643 * We do this synchronously so that when the syscall returns the process
3a4fa0a2 1644 * is complete. That guarantee is necessary for security reasons.
af9b897e
AC
1645 */
1646
37bdfb07 1647void tty_vhangup(struct tty_struct *tty)
1da177e4
LT
1648{
1649#ifdef TTY_DEBUG_HANGUP
1650 char buf[64];
1651
1652 printk(KERN_DEBUG "%s vhangup...\n", tty_name(tty, buf));
1653#endif
65f27f38 1654 do_tty_hangup(&tty->hangup_work);
1da177e4 1655}
37bdfb07 1656
1da177e4
LT
1657EXPORT_SYMBOL(tty_vhangup);
1658
af9b897e
AC
1659/**
1660 * tty_hung_up_p - was tty hung up
1661 * @filp: file pointer of tty
1662 *
1663 * Return true if the tty has been subject to a vhangup or a carrier
1664 * loss
1665 */
1666
37bdfb07 1667int tty_hung_up_p(struct file *filp)
1da177e4
LT
1668{
1669 return (filp->f_op == &hung_up_tty_fops);
1670}
1671
1672EXPORT_SYMBOL(tty_hung_up_p);
1673
522ed776 1674/**
37bdfb07
AC
1675 * is_tty - checker whether file is a TTY
1676 * @filp: file handle that may be a tty
1677 *
1678 * Check if the file handle is a tty handle.
522ed776 1679 */
37bdfb07 1680
522ed776
MT
1681int is_tty(struct file *filp)
1682{
1683 return filp->f_op->read == tty_read
1684 || filp->f_op->read == hung_up_tty_read;
1685}
1686
ab521dc0 1687static void session_clear_tty(struct pid *session)
24ec839c
PZ
1688{
1689 struct task_struct *p;
ab521dc0 1690 do_each_pid_task(session, PIDTYPE_SID, p) {
24ec839c 1691 proc_clear_tty(p);
ab521dc0 1692 } while_each_pid_task(session, PIDTYPE_SID, p);
24ec839c
PZ
1693}
1694
af9b897e
AC
1695/**
1696 * disassociate_ctty - disconnect controlling tty
1697 * @on_exit: true if exiting so need to "hang up" the session
1da177e4 1698 *
af9b897e
AC
1699 * This function is typically called only by the session leader, when
1700 * it wants to disassociate itself from its controlling tty.
1701 *
1702 * It performs the following functions:
1da177e4
LT
1703 * (1) Sends a SIGHUP and SIGCONT to the foreground process group
1704 * (2) Clears the tty from being controlling the session
1705 * (3) Clears the controlling tty for all processes in the
1706 * session group.
1707 *
af9b897e
AC
1708 * The argument on_exit is set to 1 if called when a process is
1709 * exiting; it is 0 if called by the ioctl TIOCNOTTY.
1710 *
24ec839c 1711 * Locking:
af9b897e 1712 * BKL is taken for hysterical raisins
24ec839c
PZ
1713 * tty_mutex is taken to protect tty
1714 * ->siglock is taken to protect ->signal/->sighand
1715 * tasklist_lock is taken to walk process list for sessions
1716 * ->siglock is taken to protect ->signal/->sighand
1da177e4 1717 */
af9b897e 1718
1da177e4
LT
1719void disassociate_ctty(int on_exit)
1720{
1721 struct tty_struct *tty;
ab521dc0 1722 struct pid *tty_pgrp = NULL;
1da177e4 1723
1da177e4 1724
70522e12 1725 mutex_lock(&tty_mutex);
24ec839c 1726 tty = get_current_tty();
1da177e4 1727 if (tty) {
ab521dc0 1728 tty_pgrp = get_pid(tty->pgrp);
70522e12 1729 mutex_unlock(&tty_mutex);
04f378b1 1730 lock_kernel();
24ec839c 1731 /* XXX: here we race, there is nothing protecting tty */
1da177e4
LT
1732 if (on_exit && tty->driver->type != TTY_DRIVER_TYPE_PTY)
1733 tty_vhangup(tty);
04f378b1 1734 unlock_kernel();
680a9671 1735 } else if (on_exit) {
ab521dc0 1736 struct pid *old_pgrp;
680a9671
EB
1737 spin_lock_irq(&current->sighand->siglock);
1738 old_pgrp = current->signal->tty_old_pgrp;
ab521dc0 1739 current->signal->tty_old_pgrp = NULL;
680a9671 1740 spin_unlock_irq(&current->sighand->siglock);
24ec839c 1741 if (old_pgrp) {
ab521dc0
EB
1742 kill_pgrp(old_pgrp, SIGHUP, on_exit);
1743 kill_pgrp(old_pgrp, SIGCONT, on_exit);
1744 put_pid(old_pgrp);
1da177e4 1745 }
70522e12 1746 mutex_unlock(&tty_mutex);
1da177e4
LT
1747 return;
1748 }
ab521dc0
EB
1749 if (tty_pgrp) {
1750 kill_pgrp(tty_pgrp, SIGHUP, on_exit);
1da177e4 1751 if (!on_exit)
ab521dc0
EB
1752 kill_pgrp(tty_pgrp, SIGCONT, on_exit);
1753 put_pid(tty_pgrp);
1da177e4
LT
1754 }
1755
24ec839c 1756 spin_lock_irq(&current->sighand->siglock);
2a65f1d9 1757 put_pid(current->signal->tty_old_pgrp);
23cac8de 1758 current->signal->tty_old_pgrp = NULL;
24ec839c
PZ
1759 spin_unlock_irq(&current->sighand->siglock);
1760
1761 mutex_lock(&tty_mutex);
1762 /* It is possible that do_tty_hangup has free'd this tty */
1763 tty = get_current_tty();
1764 if (tty) {
47f86834
AC
1765 unsigned long flags;
1766 spin_lock_irqsave(&tty->ctrl_lock, flags);
ab521dc0
EB
1767 put_pid(tty->session);
1768 put_pid(tty->pgrp);
1769 tty->session = NULL;
1770 tty->pgrp = NULL;
47f86834 1771 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
24ec839c
PZ
1772 } else {
1773#ifdef TTY_DEBUG_HANGUP
1774 printk(KERN_DEBUG "error attempted to write to tty [0x%p]"
1775 " = NULL", tty);
1776#endif
1777 }
1778 mutex_unlock(&tty_mutex);
1da177e4
LT
1779
1780 /* Now clear signal->tty under the lock */
1781 read_lock(&tasklist_lock);
ab521dc0 1782 session_clear_tty(task_session(current));
1da177e4 1783 read_unlock(&tasklist_lock);
1da177e4
LT
1784}
1785
98a27ba4
EB
1786/**
1787 *
1788 * no_tty - Ensure the current process does not have a controlling tty
1789 */
1790void no_tty(void)
1791{
1792 struct task_struct *tsk = current;
04f378b1 1793 lock_kernel();
98a27ba4
EB
1794 if (tsk->signal->leader)
1795 disassociate_ctty(0);
04f378b1 1796 unlock_kernel();
98a27ba4
EB
1797 proc_clear_tty(tsk);
1798}
1799
af9b897e
AC
1800
1801/**
beb7dd86 1802 * stop_tty - propagate flow control
af9b897e
AC
1803 * @tty: tty to stop
1804 *
1805 * Perform flow control to the driver. For PTY/TTY pairs we
beb7dd86 1806 * must also propagate the TIOCKPKT status. May be called
af9b897e
AC
1807 * on an already stopped device and will not re-call the driver
1808 * method.
1809 *
1810 * This functionality is used by both the line disciplines for
1811 * halting incoming flow and by the driver. It may therefore be
1812 * called from any context, may be under the tty atomic_write_lock
1813 * but not always.
1814 *
1815 * Locking:
04f378b1 1816 * Uses the tty control lock internally
af9b897e
AC
1817 */
1818
1da177e4
LT
1819void stop_tty(struct tty_struct *tty)
1820{
04f378b1
AC
1821 unsigned long flags;
1822 spin_lock_irqsave(&tty->ctrl_lock, flags);
1823 if (tty->stopped) {
1824 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
1da177e4 1825 return;
04f378b1 1826 }
1da177e4
LT
1827 tty->stopped = 1;
1828 if (tty->link && tty->link->packet) {
1829 tty->ctrl_status &= ~TIOCPKT_START;
1830 tty->ctrl_status |= TIOCPKT_STOP;
1831 wake_up_interruptible(&tty->link->read_wait);
1832 }
04f378b1 1833 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
f34d7a5b
AC
1834 if (tty->ops->stop)
1835 (tty->ops->stop)(tty);
1da177e4
LT
1836}
1837
1838EXPORT_SYMBOL(stop_tty);
1839
af9b897e 1840/**
beb7dd86 1841 * start_tty - propagate flow control
af9b897e
AC
1842 * @tty: tty to start
1843 *
1844 * Start a tty that has been stopped if at all possible. Perform
3a4fa0a2 1845 * any necessary wakeups and propagate the TIOCPKT status. If this
af9b897e
AC
1846 * is the tty was previous stopped and is being started then the
1847 * driver start method is invoked and the line discipline woken.
1848 *
1849 * Locking:
04f378b1 1850 * ctrl_lock
af9b897e
AC
1851 */
1852
1da177e4
LT
1853void start_tty(struct tty_struct *tty)
1854{
04f378b1
AC
1855 unsigned long flags;
1856 spin_lock_irqsave(&tty->ctrl_lock, flags);
1857 if (!tty->stopped || tty->flow_stopped) {
1858 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
1da177e4 1859 return;
04f378b1 1860 }
1da177e4
LT
1861 tty->stopped = 0;
1862 if (tty->link && tty->link->packet) {
1863 tty->ctrl_status &= ~TIOCPKT_STOP;
1864 tty->ctrl_status |= TIOCPKT_START;
1865 wake_up_interruptible(&tty->link->read_wait);
1866 }
04f378b1 1867 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
f34d7a5b
AC
1868 if (tty->ops->start)
1869 (tty->ops->start)(tty);
1da177e4
LT
1870 /* If we have a running line discipline it may need kicking */
1871 tty_wakeup(tty);
1da177e4
LT
1872}
1873
1874EXPORT_SYMBOL(start_tty);
1875
af9b897e
AC
1876/**
1877 * tty_read - read method for tty device files
1878 * @file: pointer to tty file
1879 * @buf: user buffer
1880 * @count: size of user buffer
1881 * @ppos: unused
1882 *
1883 * Perform the read system call function on this terminal device. Checks
1884 * for hung up devices before calling the line discipline method.
1885 *
1886 * Locking:
47f86834
AC
1887 * Locks the line discipline internally while needed. Multiple
1888 * read calls may be outstanding in parallel.
af9b897e
AC
1889 */
1890
37bdfb07 1891static ssize_t tty_read(struct file *file, char __user *buf, size_t count,
1da177e4
LT
1892 loff_t *ppos)
1893{
1894 int i;
37bdfb07 1895 struct tty_struct *tty;
1da177e4
LT
1896 struct inode *inode;
1897 struct tty_ldisc *ld;
1898
1899 tty = (struct tty_struct *)file->private_data;
a7113a96 1900 inode = file->f_path.dentry->d_inode;
1da177e4
LT
1901 if (tty_paranoia_check(tty, inode, "tty_read"))
1902 return -EIO;
1903 if (!tty || (test_bit(TTY_IO_ERROR, &tty->flags)))
1904 return -EIO;
1905
1906 /* We want to wait for the line discipline to sort out in this
1907 situation */
1908 ld = tty_ldisc_ref_wait(tty);
a352def2
AC
1909 if (ld->ops->read)
1910 i = (ld->ops->read)(tty, file, buf, count);
1da177e4
LT
1911 else
1912 i = -EIO;
1913 tty_ldisc_deref(ld);
1da177e4
LT
1914 if (i > 0)
1915 inode->i_atime = current_fs_time(inode->i_sb);
1916 return i;
1917}
1918
9c1729db
AC
1919void tty_write_unlock(struct tty_struct *tty)
1920{
1921 mutex_unlock(&tty->atomic_write_lock);
1922 wake_up_interruptible(&tty->write_wait);
1923}
1924
1925int tty_write_lock(struct tty_struct *tty, int ndelay)
1926{
1927 if (!mutex_trylock(&tty->atomic_write_lock)) {
1928 if (ndelay)
1929 return -EAGAIN;
1930 if (mutex_lock_interruptible(&tty->atomic_write_lock))
1931 return -ERESTARTSYS;
1932 }
1933 return 0;
1934}
1935
1da177e4
LT
1936/*
1937 * Split writes up in sane blocksizes to avoid
1938 * denial-of-service type attacks
1939 */
1940static inline ssize_t do_tty_write(
1941 ssize_t (*write)(struct tty_struct *, struct file *, const unsigned char *, size_t),
1942 struct tty_struct *tty,
1943 struct file *file,
1944 const char __user *buf,
1945 size_t count)
1946{
9c1729db 1947 ssize_t ret, written = 0;
1da177e4 1948 unsigned int chunk;
37bdfb07 1949
9c1729db
AC
1950 ret = tty_write_lock(tty, file->f_flags & O_NDELAY);
1951 if (ret < 0)
1952 return ret;
1da177e4
LT
1953
1954 /*
1955 * We chunk up writes into a temporary buffer. This
1956 * simplifies low-level drivers immensely, since they
1957 * don't have locking issues and user mode accesses.
1958 *
1959 * But if TTY_NO_WRITE_SPLIT is set, we should use a
1960 * big chunk-size..
1961 *
1962 * The default chunk-size is 2kB, because the NTTY
1963 * layer has problems with bigger chunks. It will
1964 * claim to be able to handle more characters than
1965 * it actually does.
af9b897e
AC
1966 *
1967 * FIXME: This can probably go away now except that 64K chunks
1968 * are too likely to fail unless switched to vmalloc...
1da177e4
LT
1969 */
1970 chunk = 2048;
1971 if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags))
1972 chunk = 65536;
1973 if (count < chunk)
1974 chunk = count;
1975
70522e12 1976 /* write_buf/write_cnt is protected by the atomic_write_lock mutex */
1da177e4
LT
1977 if (tty->write_cnt < chunk) {
1978 unsigned char *buf;
1979
1980 if (chunk < 1024)
1981 chunk = 1024;
1982
1983 buf = kmalloc(chunk, GFP_KERNEL);
1984 if (!buf) {
9c1729db
AC
1985 ret = -ENOMEM;
1986 goto out;
1da177e4
LT
1987 }
1988 kfree(tty->write_buf);
1989 tty->write_cnt = chunk;
1990 tty->write_buf = buf;
1991 }
1992
1993 /* Do the write .. */
1994 for (;;) {
1995 size_t size = count;
1996 if (size > chunk)
1997 size = chunk;
1998 ret = -EFAULT;
1999 if (copy_from_user(tty->write_buf, buf, size))
2000 break;
1da177e4 2001 ret = write(tty, file, tty->write_buf, size);
1da177e4
LT
2002 if (ret <= 0)
2003 break;
2004 written += ret;
2005 buf += ret;
2006 count -= ret;
2007 if (!count)
2008 break;
2009 ret = -ERESTARTSYS;
2010 if (signal_pending(current))
2011 break;
2012 cond_resched();
2013 }
2014 if (written) {
a7113a96 2015 struct inode *inode = file->f_path.dentry->d_inode;
1da177e4
LT
2016 inode->i_mtime = current_fs_time(inode->i_sb);
2017 ret = written;
2018 }
9c1729db
AC
2019out:
2020 tty_write_unlock(tty);
1da177e4
LT
2021 return ret;
2022}
2023
2024
af9b897e
AC
2025/**
2026 * tty_write - write method for tty device file
2027 * @file: tty file pointer
2028 * @buf: user data to write
2029 * @count: bytes to write
2030 * @ppos: unused
2031 *
2032 * Write data to a tty device via the line discipline.
2033 *
2034 * Locking:
2035 * Locks the line discipline as required
2036 * Writes to the tty driver are serialized by the atomic_write_lock
2037 * and are then processed in chunks to the device. The line discipline
2038 * write method will not be involked in parallel for each device
2039 * The line discipline write method is called under the big
2040 * kernel lock for historical reasons. New code should not rely on this.
2041 */
2042
37bdfb07
AC
2043static ssize_t tty_write(struct file *file, const char __user *buf,
2044 size_t count, loff_t *ppos)
1da177e4 2045{
37bdfb07 2046 struct tty_struct *tty;
a7113a96 2047 struct inode *inode = file->f_path.dentry->d_inode;
1da177e4
LT
2048 ssize_t ret;
2049 struct tty_ldisc *ld;
37bdfb07 2050
1da177e4
LT
2051 tty = (struct tty_struct *)file->private_data;
2052 if (tty_paranoia_check(tty, inode, "tty_write"))
2053 return -EIO;
f34d7a5b 2054 if (!tty || !tty->ops->write ||
37bdfb07
AC
2055 (test_bit(TTY_IO_ERROR, &tty->flags)))
2056 return -EIO;
f34d7a5b
AC
2057 /* Short term debug to catch buggy drivers */
2058 if (tty->ops->write_room == NULL)
2059 printk(KERN_ERR "tty driver %s lacks a write_room method.\n",
2060 tty->driver->name);
37bdfb07 2061 ld = tty_ldisc_ref_wait(tty);
a352def2 2062 if (!ld->ops->write)
1da177e4
LT
2063 ret = -EIO;
2064 else
a352def2 2065 ret = do_tty_write(ld->ops->write, tty, file, buf, count);
1da177e4
LT
2066 tty_ldisc_deref(ld);
2067 return ret;
2068}
2069
37bdfb07
AC
2070ssize_t redirected_tty_write(struct file *file, const char __user *buf,
2071 size_t count, loff_t *ppos)
1da177e4
LT
2072{
2073 struct file *p = NULL;
2074
2075 spin_lock(&redirect_lock);
2076 if (redirect) {
2077 get_file(redirect);
2078 p = redirect;
2079 }
2080 spin_unlock(&redirect_lock);
2081
2082 if (p) {
2083 ssize_t res;
2084 res = vfs_write(p, buf, count, &p->f_pos);
2085 fput(p);
2086 return res;
2087 }
1da177e4
LT
2088 return tty_write(file, buf, count, ppos);
2089}
2090
6f67048c
AC
2091void tty_port_init(struct tty_port *port)
2092{
2093 memset(port, 0, sizeof(*port));
2094 init_waitqueue_head(&port->open_wait);
2095 init_waitqueue_head(&port->close_wait);
2096 mutex_init(&port->mutex);
44b7d1b3
AC
2097 port->close_delay = (50 * HZ) / 100;
2098 port->closing_wait = (3000 * HZ) / 100;
6f67048c
AC
2099}
2100EXPORT_SYMBOL(tty_port_init);
2101
2102int tty_port_alloc_xmit_buf(struct tty_port *port)
2103{
2104 /* We may sleep in get_zeroed_page() */
2105 mutex_lock(&port->mutex);
2106 if (port->xmit_buf == NULL)
2107 port->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
2108 mutex_unlock(&port->mutex);
2109 if (port->xmit_buf == NULL)
2110 return -ENOMEM;
2111 return 0;
2112}
2113EXPORT_SYMBOL(tty_port_alloc_xmit_buf);
2114
2115void tty_port_free_xmit_buf(struct tty_port *port)
2116{
2117 mutex_lock(&port->mutex);
2118 if (port->xmit_buf != NULL) {
2119 free_page((unsigned long)port->xmit_buf);
2120 port->xmit_buf = NULL;
2121 }
2122 mutex_unlock(&port->mutex);
2123}
2124EXPORT_SYMBOL(tty_port_free_xmit_buf);
2125
2126
1da177e4
LT
2127static char ptychar[] = "pqrstuvwxyzabcde";
2128
af9b897e
AC
2129/**
2130 * pty_line_name - generate name for a pty
2131 * @driver: the tty driver in use
2132 * @index: the minor number
2133 * @p: output buffer of at least 6 bytes
2134 *
2135 * Generate a name from a driver reference and write it to the output
2136 * buffer.
2137 *
2138 * Locking: None
2139 */
2140static void pty_line_name(struct tty_driver *driver, int index, char *p)
1da177e4
LT
2141{
2142 int i = index + driver->name_base;
2143 /* ->name is initialized to "ttyp", but "tty" is expected */
2144 sprintf(p, "%s%c%x",
37bdfb07
AC
2145 driver->subtype == PTY_TYPE_SLAVE ? "tty" : driver->name,
2146 ptychar[i >> 4 & 0xf], i & 0xf);
1da177e4
LT
2147}
2148
af9b897e
AC
2149/**
2150 * pty_line_name - generate name for a tty
2151 * @driver: the tty driver in use
2152 * @index: the minor number
2153 * @p: output buffer of at least 7 bytes
2154 *
2155 * Generate a name from a driver reference and write it to the output
2156 * buffer.
2157 *
2158 * Locking: None
2159 */
2160static void tty_line_name(struct tty_driver *driver, int index, char *p)
1da177e4
LT
2161{
2162 sprintf(p, "%s%d", driver->name, index + driver->name_base);
2163}
2164
af9b897e
AC
2165/**
2166 * init_dev - initialise a tty device
2167 * @driver: tty driver we are opening a device on
2168 * @idx: device index
2169 * @tty: returned tty structure
2170 *
2171 * Prepare a tty device. This may not be a "new" clean device but
2172 * could also be an active device. The pty drivers require special
2173 * handling because of this.
2174 *
2175 * Locking:
2176 * The function is called under the tty_mutex, which
2177 * protects us from the tty struct or driver itself going away.
2178 *
2179 * On exit the tty device has the line discipline attached and
2180 * a reference count of 1. If a pair was created for pty/tty use
2181 * and the other was a pty master then it too has a reference count of 1.
2182 *
1da177e4 2183 * WSH 06/09/97: Rewritten to remove races and properly clean up after a
70522e12
IM
2184 * failed open. The new code protects the open with a mutex, so it's
2185 * really quite straightforward. The mutex locking can probably be
1da177e4
LT
2186 * relaxed for the (most common) case of reopening a tty.
2187 */
af9b897e 2188
1da177e4
LT
2189static int init_dev(struct tty_driver *driver, int idx,
2190 struct tty_struct **ret_tty)
2191{
2192 struct tty_struct *tty, *o_tty;
edc6afc5
AC
2193 struct ktermios *tp, **tp_loc, *o_tp, **o_tp_loc;
2194 struct ktermios *ltp, **ltp_loc, *o_ltp, **o_ltp_loc;
af9b897e 2195 int retval = 0;
a352def2 2196 struct tty_ldisc *ld;
1da177e4
LT
2197
2198 /* check whether we're reopening an existing tty */
2199 if (driver->flags & TTY_DRIVER_DEVPTS_MEM) {
2200 tty = devpts_get_tty(idx);
5a39e8c6
ASRF
2201 /*
2202 * If we don't have a tty here on a slave open, it's because
2203 * the master already started the close process and there's
2204 * no relation between devpts file and tty anymore.
2205 */
2206 if (!tty && driver->subtype == PTY_TYPE_SLAVE) {
2207 retval = -EIO;
2208 goto end_init;
2209 }
2210 /*
2211 * It's safe from now on because init_dev() is called with
2212 * tty_mutex held and release_dev() won't change tty->count
2213 * or tty->flags without having to grab tty_mutex
2214 */
1da177e4
LT
2215 if (tty && driver->subtype == PTY_TYPE_MASTER)
2216 tty = tty->link;
2217 } else {
2218 tty = driver->ttys[idx];
2219 }
2220 if (tty) goto fast_track;
2221
2222 /*
2223 * First time open is complex, especially for PTY devices.
2224 * This code guarantees that either everything succeeds and the
2225 * TTY is ready for operation, or else the table slots are vacated
37bdfb07 2226 * and the allocated memory released. (Except that the termios
1da177e4
LT
2227 * and locked termios may be retained.)
2228 */
2229
2230 if (!try_module_get(driver->owner)) {
2231 retval = -ENODEV;
2232 goto end_init;
2233 }
2234
2235 o_tty = NULL;
2236 tp = o_tp = NULL;
2237 ltp = o_ltp = NULL;
2238
2239 tty = alloc_tty_struct();
37bdfb07 2240 if (!tty)
1da177e4
LT
2241 goto fail_no_mem;
2242 initialize_tty_struct(tty);
2243 tty->driver = driver;
f34d7a5b 2244 tty->ops = driver->ops;
1da177e4
LT
2245 tty->index = idx;
2246 tty_line_name(driver, idx, tty->name);
2247
2248 if (driver->flags & TTY_DRIVER_DEVPTS_MEM) {
2249 tp_loc = &tty->termios;
2250 ltp_loc = &tty->termios_locked;
2251 } else {
2252 tp_loc = &driver->termios[idx];
2253 ltp_loc = &driver->termios_locked[idx];
2254 }
2255
2256 if (!*tp_loc) {
abcb1ff3 2257 tp = kmalloc(sizeof(struct ktermios), GFP_KERNEL);
1da177e4
LT
2258 if (!tp)
2259 goto free_mem_out;
2260 *tp = driver->init_termios;
2261 }
2262
2263 if (!*ltp_loc) {
506eb99a 2264 ltp = kzalloc(sizeof(struct ktermios), GFP_KERNEL);
1da177e4
LT
2265 if (!ltp)
2266 goto free_mem_out;
1da177e4
LT
2267 }
2268
2269 if (driver->type == TTY_DRIVER_TYPE_PTY) {
2270 o_tty = alloc_tty_struct();
2271 if (!o_tty)
2272 goto free_mem_out;
2273 initialize_tty_struct(o_tty);
2274 o_tty->driver = driver->other;
f34d7a5b 2275 o_tty->ops = driver->ops;
1da177e4
LT
2276 o_tty->index = idx;
2277 tty_line_name(driver->other, idx, o_tty->name);
2278
2279 if (driver->flags & TTY_DRIVER_DEVPTS_MEM) {
2280 o_tp_loc = &o_tty->termios;
2281 o_ltp_loc = &o_tty->termios_locked;
2282 } else {
2283 o_tp_loc = &driver->other->termios[idx];
2284 o_ltp_loc = &driver->other->termios_locked[idx];
2285 }
2286
2287 if (!*o_tp_loc) {
abcb1ff3 2288 o_tp = kmalloc(sizeof(struct ktermios), GFP_KERNEL);
1da177e4
LT
2289 if (!o_tp)
2290 goto free_mem_out;
2291 *o_tp = driver->other->init_termios;
2292 }
2293
2294 if (!*o_ltp_loc) {
506eb99a 2295 o_ltp = kzalloc(sizeof(struct ktermios), GFP_KERNEL);
1da177e4
LT
2296 if (!o_ltp)
2297 goto free_mem_out;
1da177e4
LT
2298 }
2299
2300 /*
2301 * Everything allocated ... set up the o_tty structure.
2302 */
37bdfb07 2303 if (!(driver->other->flags & TTY_DRIVER_DEVPTS_MEM))
1da177e4 2304 driver->other->ttys[idx] = o_tty;
1da177e4
LT
2305 if (!*o_tp_loc)
2306 *o_tp_loc = o_tp;
2307 if (!*o_ltp_loc)
2308 *o_ltp_loc = o_ltp;
2309 o_tty->termios = *o_tp_loc;
2310 o_tty->termios_locked = *o_ltp_loc;
2311 driver->other->refcount++;
2312 if (driver->subtype == PTY_TYPE_MASTER)
2313 o_tty->count++;
2314
2315 /* Establish the links in both directions */
2316 tty->link = o_tty;
2317 o_tty->link = tty;
2318 }
2319
37bdfb07 2320 /*
1da177e4 2321 * All structures have been allocated, so now we install them.
d5698c28 2322 * Failures after this point use release_tty to clean up, so
1da177e4
LT
2323 * there's no need to null out the local pointers.
2324 */
37bdfb07 2325 if (!(driver->flags & TTY_DRIVER_DEVPTS_MEM))
1da177e4 2326 driver->ttys[idx] = tty;
37bdfb07 2327
1da177e4
LT
2328 if (!*tp_loc)
2329 *tp_loc = tp;
2330 if (!*ltp_loc)
2331 *ltp_loc = ltp;
2332 tty->termios = *tp_loc;
2333 tty->termios_locked = *ltp_loc;
edc6afc5
AC
2334 /* Compatibility until drivers always set this */
2335 tty->termios->c_ispeed = tty_termios_input_baud_rate(tty->termios);
2336 tty->termios->c_ospeed = tty_termios_baud_rate(tty->termios);
1da177e4
LT
2337 driver->refcount++;
2338 tty->count++;
2339
37bdfb07 2340 /*
1da177e4 2341 * Structures all installed ... call the ldisc open routines.
d5698c28
CH
2342 * If we fail here just call release_tty to clean up. No need
2343 * to decrement the use counts, as release_tty doesn't care.
1da177e4 2344 */
a352def2
AC
2345
2346 ld = &tty->ldisc;
1da177e4 2347
a352def2
AC
2348 if (ld->ops->open) {
2349 retval = (ld->ops->open)(tty);
1da177e4
LT
2350 if (retval)
2351 goto release_mem_out;
2352 }
a352def2
AC
2353 if (o_tty && o_tty->ldisc.ops->open) {
2354 retval = (o_tty->ldisc.ops->open)(o_tty);
1da177e4 2355 if (retval) {
a352def2
AC
2356 if (ld->ops->close)
2357 (ld->ops->close)(tty);
1da177e4
LT
2358 goto release_mem_out;
2359 }
2360 tty_ldisc_enable(o_tty);
2361 }
2362 tty_ldisc_enable(tty);
2363 goto success;
2364
2365 /*
2366 * This fast open can be used if the tty is already open.
2367 * No memory is allocated, and the only failures are from
2368 * attempting to open a closing tty or attempting multiple
2369 * opens on a pty master.
2370 */
2371fast_track:
2372 if (test_bit(TTY_CLOSING, &tty->flags)) {
2373 retval = -EIO;
2374 goto end_init;
2375 }
2376 if (driver->type == TTY_DRIVER_TYPE_PTY &&
2377 driver->subtype == PTY_TYPE_MASTER) {
2378 /*
37bdfb07 2379 * special case for PTY masters: only one open permitted,
1da177e4
LT
2380 * and the slave side open count is incremented as well.
2381 */
2382 if (tty->count) {
2383 retval = -EIO;
2384 goto end_init;
2385 }
2386 tty->link->count++;
2387 }
2388 tty->count++;
2389 tty->driver = driver; /* N.B. why do this every time?? */
2390
2391 /* FIXME */
37bdfb07 2392 if (!test_bit(TTY_LDISC, &tty->flags))
1da177e4
LT
2393 printk(KERN_ERR "init_dev but no ldisc\n");
2394success:
2395 *ret_tty = tty;
37bdfb07 2396
70522e12 2397 /* All paths come through here to release the mutex */
1da177e4
LT
2398end_init:
2399 return retval;
2400
2401 /* Release locally allocated memory ... nothing placed in slots */
2402free_mem_out:
735d5661 2403 kfree(o_tp);
1da177e4
LT
2404 if (o_tty)
2405 free_tty_struct(o_tty);
735d5661
JJ
2406 kfree(ltp);
2407 kfree(tp);
1da177e4
LT
2408 free_tty_struct(tty);
2409
2410fail_no_mem:
2411 module_put(driver->owner);
2412 retval = -ENOMEM;
2413 goto end_init;
2414
d5698c28 2415 /* call the tty release_tty routine to clean out this slot */
1da177e4 2416release_mem_out:
4050914f
AM
2417 if (printk_ratelimit())
2418 printk(KERN_INFO "init_dev: ldisc open failed, "
2419 "clearing slot %d\n", idx);
d5698c28 2420 release_tty(tty, idx);
1da177e4
LT
2421 goto end_init;
2422}
2423
af9b897e 2424/**
d5698c28 2425 * release_one_tty - release tty structure memory
af9b897e
AC
2426 *
2427 * Releases memory associated with a tty structure, and clears out the
2428 * driver table slots. This function is called when a device is no longer
2429 * in use. It also gets called when setup of a device fails.
2430 *
2431 * Locking:
2432 * tty_mutex - sometimes only
2433 * takes the file list lock internally when working on the list
2434 * of ttys that the driver keeps.
2435 * FIXME: should we require tty_mutex is held here ??
1da177e4 2436 */
d5698c28 2437static void release_one_tty(struct tty_struct *tty, int idx)
1da177e4 2438{
1da177e4 2439 int devpts = tty->driver->flags & TTY_DRIVER_DEVPTS_MEM;
d5698c28 2440 struct ktermios *tp;
1da177e4
LT
2441
2442 if (!devpts)
2443 tty->driver->ttys[idx] = NULL;
d5698c28 2444
1da177e4
LT
2445 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) {
2446 tp = tty->termios;
2447 if (!devpts)
2448 tty->driver->termios[idx] = NULL;
2449 kfree(tp);
2450
2451 tp = tty->termios_locked;
2452 if (!devpts)
2453 tty->driver->termios_locked[idx] = NULL;
2454 kfree(tp);
2455 }
2456
d5698c28 2457
1da177e4
LT
2458 tty->magic = 0;
2459 tty->driver->refcount--;
d5698c28 2460
1da177e4
LT
2461 file_list_lock();
2462 list_del_init(&tty->tty_files);
2463 file_list_unlock();
d5698c28 2464
1da177e4
LT
2465 free_tty_struct(tty);
2466}
2467
d5698c28
CH
2468/**
2469 * release_tty - release tty structure memory
2470 *
2471 * Release both @tty and a possible linked partner (think pty pair),
2472 * and decrement the refcount of the backing module.
2473 *
2474 * Locking:
2475 * tty_mutex - sometimes only
2476 * takes the file list lock internally when working on the list
2477 * of ttys that the driver keeps.
2478 * FIXME: should we require tty_mutex is held here ??
2479 */
2480static void release_tty(struct tty_struct *tty, int idx)
2481{
2482 struct tty_driver *driver = tty->driver;
2483
2484 if (tty->link)
2485 release_one_tty(tty->link, idx);
2486 release_one_tty(tty, idx);
2487 module_put(driver->owner);
2488}
2489
1da177e4
LT
2490/*
2491 * Even releasing the tty structures is a tricky business.. We have
2492 * to be very careful that the structures are all released at the
2493 * same time, as interrupts might otherwise get the wrong pointers.
2494 *
2495 * WSH 09/09/97: rewritten to avoid some nasty race conditions that could
2496 * lead to double frees or releasing memory still in use.
2497 */
37bdfb07 2498static void release_dev(struct file *filp)
1da177e4
LT
2499{
2500 struct tty_struct *tty, *o_tty;
a352def2 2501 struct tty_ldisc ld;
1da177e4 2502 int pty_master, tty_closing, o_tty_closing, do_sleep;
14a6283e 2503 int devpts;
1da177e4
LT
2504 int idx;
2505 char buf[64];
2506 unsigned long flags;
37bdfb07 2507
1da177e4 2508 tty = (struct tty_struct *)filp->private_data;
37bdfb07
AC
2509 if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode,
2510 "release_dev"))
1da177e4
LT
2511 return;
2512
2513 check_tty_count(tty, "release_dev");
2514
2515 tty_fasync(-1, filp, 0);
2516
2517 idx = tty->index;
2518 pty_master = (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
2519 tty->driver->subtype == PTY_TYPE_MASTER);
2520 devpts = (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM) != 0;
1da177e4
LT
2521 o_tty = tty->link;
2522
2523#ifdef TTY_PARANOIA_CHECK
2524 if (idx < 0 || idx >= tty->driver->num) {
2525 printk(KERN_DEBUG "release_dev: bad idx when trying to "
2526 "free (%s)\n", tty->name);
2527 return;
2528 }
2529 if (!(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
2530 if (tty != tty->driver->ttys[idx]) {
2531 printk(KERN_DEBUG "release_dev: driver.table[%d] not tty "
2532 "for (%s)\n", idx, tty->name);
2533 return;
2534 }
2535 if (tty->termios != tty->driver->termios[idx]) {
2536 printk(KERN_DEBUG "release_dev: driver.termios[%d] not termios "
2537 "for (%s)\n",
2538 idx, tty->name);
2539 return;
2540 }
2541 if (tty->termios_locked != tty->driver->termios_locked[idx]) {
2542 printk(KERN_DEBUG "release_dev: driver.termios_locked[%d] not "
2543 "termios_locked for (%s)\n",
2544 idx, tty->name);
2545 return;
2546 }
2547 }
2548#endif
2549
2550#ifdef TTY_DEBUG_HANGUP
2551 printk(KERN_DEBUG "release_dev of %s (tty count=%d)...",
2552 tty_name(tty, buf), tty->count);
2553#endif
2554
2555#ifdef TTY_PARANOIA_CHECK
2556 if (tty->driver->other &&
2557 !(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
2558 if (o_tty != tty->driver->other->ttys[idx]) {
2559 printk(KERN_DEBUG "release_dev: other->table[%d] "
2560 "not o_tty for (%s)\n",
2561 idx, tty->name);
2562 return;
2563 }
2564 if (o_tty->termios != tty->driver->other->termios[idx]) {
2565 printk(KERN_DEBUG "release_dev: other->termios[%d] "
2566 "not o_termios for (%s)\n",
2567 idx, tty->name);
2568 return;
2569 }
37bdfb07 2570 if (o_tty->termios_locked !=
1da177e4
LT
2571 tty->driver->other->termios_locked[idx]) {
2572 printk(KERN_DEBUG "release_dev: other->termios_locked["
2573 "%d] not o_termios_locked for (%s)\n",
2574 idx, tty->name);
2575 return;
2576 }
2577 if (o_tty->link != tty) {
2578 printk(KERN_DEBUG "release_dev: bad pty pointers\n");
2579 return;
2580 }
2581 }
2582#endif
f34d7a5b
AC
2583 if (tty->ops->close)
2584 tty->ops->close(tty, filp);
1da177e4
LT
2585
2586 /*
2587 * Sanity check: if tty->count is going to zero, there shouldn't be
2588 * any waiters on tty->read_wait or tty->write_wait. We test the
2589 * wait queues and kick everyone out _before_ actually starting to
2590 * close. This ensures that we won't block while releasing the tty
2591 * structure.
2592 *
2593 * The test for the o_tty closing is necessary, since the master and
2594 * slave sides may close in any order. If the slave side closes out
2595 * first, its count will be one, since the master side holds an open.
2596 * Thus this test wouldn't be triggered at the time the slave closes,
2597 * so we do it now.
2598 *
2599 * Note that it's possible for the tty to be opened again while we're
2600 * flushing out waiters. By recalculating the closing flags before
2601 * each iteration we avoid any problems.
2602 */
2603 while (1) {
2604 /* Guard against races with tty->count changes elsewhere and
2605 opens on /dev/tty */
37bdfb07 2606
70522e12 2607 mutex_lock(&tty_mutex);
1da177e4
LT
2608 tty_closing = tty->count <= 1;
2609 o_tty_closing = o_tty &&
2610 (o_tty->count <= (pty_master ? 1 : 0));
1da177e4
LT
2611 do_sleep = 0;
2612
2613 if (tty_closing) {
2614 if (waitqueue_active(&tty->read_wait)) {
2615 wake_up(&tty->read_wait);
2616 do_sleep++;
2617 }
2618 if (waitqueue_active(&tty->write_wait)) {
2619 wake_up(&tty->write_wait);
2620 do_sleep++;
2621 }
2622 }
2623 if (o_tty_closing) {
2624 if (waitqueue_active(&o_tty->read_wait)) {
2625 wake_up(&o_tty->read_wait);
2626 do_sleep++;
2627 }
2628 if (waitqueue_active(&o_tty->write_wait)) {
2629 wake_up(&o_tty->write_wait);
2630 do_sleep++;
2631 }
2632 }
2633 if (!do_sleep)
2634 break;
2635
2636 printk(KERN_WARNING "release_dev: %s: read/write wait queue "
2637 "active!\n", tty_name(tty, buf));
70522e12 2638 mutex_unlock(&tty_mutex);
1da177e4 2639 schedule();
37bdfb07 2640 }
1da177e4
LT
2641
2642 /*
37bdfb07
AC
2643 * The closing flags are now consistent with the open counts on
2644 * both sides, and we've completed the last operation that could
1da177e4
LT
2645 * block, so it's safe to proceed with closing.
2646 */
1da177e4
LT
2647 if (pty_master) {
2648 if (--o_tty->count < 0) {
2649 printk(KERN_WARNING "release_dev: bad pty slave count "
2650 "(%d) for %s\n",
2651 o_tty->count, tty_name(o_tty, buf));
2652 o_tty->count = 0;
2653 }
2654 }
2655 if (--tty->count < 0) {
2656 printk(KERN_WARNING "release_dev: bad tty->count (%d) for %s\n",
2657 tty->count, tty_name(tty, buf));
2658 tty->count = 0;
2659 }
37bdfb07 2660
1da177e4
LT
2661 /*
2662 * We've decremented tty->count, so we need to remove this file
2663 * descriptor off the tty->tty_files list; this serves two
2664 * purposes:
2665 * - check_tty_count sees the correct number of file descriptors
2666 * associated with this tty.
2667 * - do_tty_hangup no longer sees this file descriptor as
2668 * something that needs to be handled for hangups.
2669 */
2670 file_kill(filp);
2671 filp->private_data = NULL;
2672
2673 /*
2674 * Perform some housekeeping before deciding whether to return.
2675 *
2676 * Set the TTY_CLOSING flag if this was the last open. In the
2677 * case of a pty we may have to wait around for the other side
2678 * to close, and TTY_CLOSING makes sure we can't be reopened.
2679 */
37bdfb07 2680 if (tty_closing)
1da177e4 2681 set_bit(TTY_CLOSING, &tty->flags);
37bdfb07 2682 if (o_tty_closing)
1da177e4
LT
2683 set_bit(TTY_CLOSING, &o_tty->flags);
2684
2685 /*
2686 * If _either_ side is closing, make sure there aren't any
2687 * processes that still think tty or o_tty is their controlling
2688 * tty.
2689 */
2690 if (tty_closing || o_tty_closing) {
1da177e4 2691 read_lock(&tasklist_lock);
24ec839c 2692 session_clear_tty(tty->session);
1da177e4 2693 if (o_tty)
24ec839c 2694 session_clear_tty(o_tty->session);
1da177e4
LT
2695 read_unlock(&tasklist_lock);
2696 }
2697
70522e12 2698 mutex_unlock(&tty_mutex);
da965822 2699
1da177e4
LT
2700 /* check whether both sides are closing ... */
2701 if (!tty_closing || (o_tty && !o_tty_closing))
2702 return;
37bdfb07 2703
1da177e4
LT
2704#ifdef TTY_DEBUG_HANGUP
2705 printk(KERN_DEBUG "freeing tty structure...");
2706#endif
2707 /*
2708 * Prevent flush_to_ldisc() from rescheduling the work for later. Then
2709 * kill any delayed work. As this is the final close it does not
2710 * race with the set_ldisc code path.
2711 */
2712 clear_bit(TTY_LDISC, &tty->flags);
33f0f88f 2713 cancel_delayed_work(&tty->buf.work);
1da177e4
LT
2714
2715 /*
33f0f88f 2716 * Wait for ->hangup_work and ->buf.work handlers to terminate
1da177e4 2717 */
37bdfb07 2718
1da177e4 2719 flush_scheduled_work();
37bdfb07 2720
1da177e4
LT
2721 /*
2722 * Wait for any short term users (we know they are just driver
2723 * side waiters as the file is closing so user count on the file
2724 * side is zero.
2725 */
2726 spin_lock_irqsave(&tty_ldisc_lock, flags);
37bdfb07 2727 while (tty->ldisc.refcount) {
1da177e4
LT
2728 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
2729 wait_event(tty_ldisc_wait, tty->ldisc.refcount == 0);
2730 spin_lock_irqsave(&tty_ldisc_lock, flags);
2731 }
2732 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
2733 /*
2734 * Shutdown the current line discipline, and reset it to N_TTY.
1da177e4
LT
2735 *
2736 * FIXME: this MUST get fixed for the new reflocking
2737 */
a352def2
AC
2738 if (tty->ldisc.ops->close)
2739 (tty->ldisc.ops->close)(tty);
2740 tty_ldisc_put(tty->ldisc.ops);
37bdfb07 2741
1da177e4
LT
2742 /*
2743 * Switch the line discipline back
2744 */
a352def2
AC
2745 WARN_ON(tty_ldisc_get(N_TTY, &ld));
2746 tty_ldisc_assign(tty, &ld);
37bdfb07 2747 tty_set_termios_ldisc(tty, N_TTY);
1da177e4
LT
2748 if (o_tty) {
2749 /* FIXME: could o_tty be in setldisc here ? */
2750 clear_bit(TTY_LDISC, &o_tty->flags);
a352def2
AC
2751 if (o_tty->ldisc.ops->close)
2752 (o_tty->ldisc.ops->close)(o_tty);
2753 tty_ldisc_put(o_tty->ldisc.ops);
2754 WARN_ON(tty_ldisc_get(N_TTY, &ld));
2755 tty_ldisc_assign(o_tty, &ld);
37bdfb07 2756 tty_set_termios_ldisc(o_tty, N_TTY);
1da177e4
LT
2757 }
2758 /*
d5698c28 2759 * The release_tty function takes care of the details of clearing
1da177e4
LT
2760 * the slots and preserving the termios structure.
2761 */
d5698c28 2762 release_tty(tty, idx);
1da177e4 2763
1da177e4 2764 /* Make this pty number available for reallocation */
718a9163
SB
2765 if (devpts)
2766 devpts_kill_index(idx);
1da177e4
LT
2767}
2768
af9b897e
AC
2769/**
2770 * tty_open - open a tty device
2771 * @inode: inode of device file
2772 * @filp: file pointer to tty
1da177e4 2773 *
af9b897e
AC
2774 * tty_open and tty_release keep up the tty count that contains the
2775 * number of opens done on a tty. We cannot use the inode-count, as
2776 * different inodes might point to the same tty.
1da177e4 2777 *
af9b897e
AC
2778 * Open-counting is needed for pty masters, as well as for keeping
2779 * track of serial lines: DTR is dropped when the last close happens.
2780 * (This is not done solely through tty->count, now. - Ted 1/27/92)
2781 *
2782 * The termios state of a pty is reset on first open so that
2783 * settings don't persist across reuse.
2784 *
24ec839c
PZ
2785 * Locking: tty_mutex protects tty, get_tty_driver and init_dev work.
2786 * tty->count should protect the rest.
2787 * ->siglock protects ->signal/->sighand
1da177e4 2788 */
af9b897e 2789
39d95b9d 2790static int __tty_open(struct inode *inode, struct file *filp)
1da177e4
LT
2791{
2792 struct tty_struct *tty;
2793 int noctty, retval;
2794 struct tty_driver *driver;
2795 int index;
2796 dev_t device = inode->i_rdev;
2797 unsigned short saved_flags = filp->f_flags;
2798
2799 nonseekable_open(inode, filp);
37bdfb07 2800
1da177e4
LT
2801retry_open:
2802 noctty = filp->f_flags & O_NOCTTY;
2803 index = -1;
2804 retval = 0;
37bdfb07 2805
70522e12 2806 mutex_lock(&tty_mutex);
1da177e4 2807
37bdfb07 2808 if (device == MKDEV(TTYAUX_MAJOR, 0)) {
24ec839c
PZ
2809 tty = get_current_tty();
2810 if (!tty) {
70522e12 2811 mutex_unlock(&tty_mutex);
1da177e4
LT
2812 return -ENXIO;
2813 }
24ec839c
PZ
2814 driver = tty->driver;
2815 index = tty->index;
1da177e4
LT
2816 filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */
2817 /* noctty = 1; */
2818 goto got_driver;
2819 }
2820#ifdef CONFIG_VT
37bdfb07 2821 if (device == MKDEV(TTY_MAJOR, 0)) {
1da177e4
LT
2822 extern struct tty_driver *console_driver;
2823 driver = console_driver;
2824 index = fg_console;
2825 noctty = 1;
2826 goto got_driver;
2827 }
2828#endif
37bdfb07 2829 if (device == MKDEV(TTYAUX_MAJOR, 1)) {
1da177e4
LT
2830 driver = console_device(&index);
2831 if (driver) {
2832 /* Don't let /dev/console block */
2833 filp->f_flags |= O_NONBLOCK;
2834 noctty = 1;
2835 goto got_driver;
2836 }
70522e12 2837 mutex_unlock(&tty_mutex);
1da177e4
LT
2838 return -ENODEV;
2839 }
2840
2841 driver = get_tty_driver(device, &index);
2842 if (!driver) {
70522e12 2843 mutex_unlock(&tty_mutex);
1da177e4
LT
2844 return -ENODEV;
2845 }
2846got_driver:
2847 retval = init_dev(driver, index, &tty);
70522e12 2848 mutex_unlock(&tty_mutex);
1da177e4
LT
2849 if (retval)
2850 return retval;
2851
2852 filp->private_data = tty;
2853 file_move(filp, &tty->tty_files);
2854 check_tty_count(tty, "tty_open");
2855 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
2856 tty->driver->subtype == PTY_TYPE_MASTER)
2857 noctty = 1;
2858#ifdef TTY_DEBUG_HANGUP
2859 printk(KERN_DEBUG "opening %s...", tty->name);
2860#endif
2861 if (!retval) {
f34d7a5b
AC
2862 if (tty->ops->open)
2863 retval = tty->ops->open(tty, filp);
1da177e4
LT
2864 else
2865 retval = -ENODEV;
2866 }
2867 filp->f_flags = saved_flags;
2868
37bdfb07
AC
2869 if (!retval && test_bit(TTY_EXCLUSIVE, &tty->flags) &&
2870 !capable(CAP_SYS_ADMIN))
1da177e4
LT
2871 retval = -EBUSY;
2872
2873 if (retval) {
2874#ifdef TTY_DEBUG_HANGUP
2875 printk(KERN_DEBUG "error %d in opening %s...", retval,
2876 tty->name);
2877#endif
2878 release_dev(filp);
2879 if (retval != -ERESTARTSYS)
2880 return retval;
2881 if (signal_pending(current))
2882 return retval;
2883 schedule();
2884 /*
2885 * Need to reset f_op in case a hangup happened.
2886 */
2887 if (filp->f_op == &hung_up_tty_fops)
2888 filp->f_op = &tty_fops;
2889 goto retry_open;
2890 }
24ec839c
PZ
2891
2892 mutex_lock(&tty_mutex);
2893 spin_lock_irq(&current->sighand->siglock);
1da177e4
LT
2894 if (!noctty &&
2895 current->signal->leader &&
2896 !current->signal->tty &&
ab521dc0 2897 tty->session == NULL)
2a65f1d9 2898 __proc_set_tty(current, tty);
24ec839c
PZ
2899 spin_unlock_irq(&current->sighand->siglock);
2900 mutex_unlock(&tty_mutex);
1da177e4
LT
2901 return 0;
2902}
2903
39d95b9d
JC
2904/* BKL pushdown: scary code avoidance wrapper */
2905static int tty_open(struct inode *inode, struct file *filp)
2906{
2907 int ret;
2908
2909 lock_kernel();
2910 ret = __tty_open(inode, filp);
2911 unlock_kernel();
2912 return ret;
2913}
2914
2915
2916
1da177e4 2917#ifdef CONFIG_UNIX98_PTYS
af9b897e
AC
2918/**
2919 * ptmx_open - open a unix 98 pty master
2920 * @inode: inode of device file
2921 * @filp: file pointer to tty
2922 *
2923 * Allocate a unix98 pty master device from the ptmx driver.
2924 *
2925 * Locking: tty_mutex protects theinit_dev work. tty->count should
37bdfb07 2926 * protect the rest.
af9b897e
AC
2927 * allocated_ptys_lock handles the list of free pty numbers
2928 */
2929
39d95b9d 2930static int __ptmx_open(struct inode *inode, struct file *filp)
1da177e4
LT
2931{
2932 struct tty_struct *tty;
2933 int retval;
2934 int index;
1da177e4
LT
2935
2936 nonseekable_open(inode, filp);
2937
2938 /* find a device that is not in use. */
718a9163
SB
2939 index = devpts_new_index();
2940 if (index < 0)
2941 return index;
1da177e4 2942
70522e12 2943 mutex_lock(&tty_mutex);
1da177e4 2944 retval = init_dev(ptm_driver, index, &tty);
70522e12 2945 mutex_unlock(&tty_mutex);
37bdfb07 2946
1da177e4
LT
2947 if (retval)
2948 goto out;
2949
2950 set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
2951 filp->private_data = tty;
2952 file_move(filp, &tty->tty_files);
2953
4f8f9d66
SB
2954 retval = devpts_pty_new(tty->link);
2955 if (retval)
1da177e4
LT
2956 goto out1;
2957
86a96538 2958 check_tty_count(tty, "ptmx_open");
f34d7a5b 2959 retval = ptm_driver->ops->open(tty, filp);
41126226 2960 if (!retval)
1da177e4
LT
2961 return 0;
2962out1:
2963 release_dev(filp);
9453a5ad 2964 return retval;
1da177e4 2965out:
718a9163 2966 devpts_kill_index(index);
1da177e4
LT
2967 return retval;
2968}
39d95b9d
JC
2969
2970static int ptmx_open(struct inode *inode, struct file *filp)
2971{
2972 int ret;
2973
2974 lock_kernel();
2975 ret = __ptmx_open(inode, filp);
2976 unlock_kernel();
2977 return ret;
2978}
1da177e4
LT
2979#endif
2980
af9b897e
AC
2981/**
2982 * tty_release - vfs callback for close
2983 * @inode: inode of tty
2984 * @filp: file pointer for handle to tty
2985 *
2986 * Called the last time each file handle is closed that references
2987 * this tty. There may however be several such references.
2988 *
2989 * Locking:
2990 * Takes bkl. See release_dev
2991 */
2992
37bdfb07 2993static int tty_release(struct inode *inode, struct file *filp)
1da177e4
LT
2994{
2995 lock_kernel();
2996 release_dev(filp);
2997 unlock_kernel();
2998 return 0;
2999}
3000
af9b897e
AC
3001/**
3002 * tty_poll - check tty status
3003 * @filp: file being polled
3004 * @wait: poll wait structures to update
3005 *
3006 * Call the line discipline polling method to obtain the poll
3007 * status of the device.
3008 *
3009 * Locking: locks called line discipline but ldisc poll method
3010 * may be re-entered freely by other callers.
3011 */
3012
37bdfb07 3013static unsigned int tty_poll(struct file *filp, poll_table *wait)
1da177e4 3014{
37bdfb07 3015 struct tty_struct *tty;
1da177e4
LT
3016 struct tty_ldisc *ld;
3017 int ret = 0;
3018
3019 tty = (struct tty_struct *)filp->private_data;
a7113a96 3020 if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, "tty_poll"))
1da177e4 3021 return 0;
37bdfb07 3022
1da177e4 3023 ld = tty_ldisc_ref_wait(tty);
a352def2
AC
3024 if (ld->ops->poll)
3025 ret = (ld->ops->poll)(tty, filp, wait);
1da177e4
LT
3026 tty_ldisc_deref(ld);
3027 return ret;
3028}
3029
37bdfb07 3030static int tty_fasync(int fd, struct file *filp, int on)
1da177e4 3031{
37bdfb07 3032 struct tty_struct *tty;
47f86834 3033 unsigned long flags;
5d1e3230 3034 int retval = 0;
1da177e4 3035
5d1e3230 3036 lock_kernel();
1da177e4 3037 tty = (struct tty_struct *)filp->private_data;
a7113a96 3038 if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, "tty_fasync"))
5d1e3230 3039 goto out;
37bdfb07 3040
1da177e4
LT
3041 retval = fasync_helper(fd, filp, on, &tty->fasync);
3042 if (retval <= 0)
5d1e3230 3043 goto out;
1da177e4
LT
3044
3045 if (on) {
ab521dc0
EB
3046 enum pid_type type;
3047 struct pid *pid;
1da177e4
LT
3048 if (!waitqueue_active(&tty->read_wait))
3049 tty->minimum_to_wake = 1;
47f86834 3050 spin_lock_irqsave(&tty->ctrl_lock, flags);
ab521dc0
EB
3051 if (tty->pgrp) {
3052 pid = tty->pgrp;
3053 type = PIDTYPE_PGID;
3054 } else {
3055 pid = task_pid(current);
3056 type = PIDTYPE_PID;
3057 }
47f86834 3058 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
ab521dc0 3059 retval = __f_setown(filp, pid, type, 0);
1da177e4 3060 if (retval)
5d1e3230 3061 goto out;
1da177e4
LT
3062 } else {
3063 if (!tty->fasync && !waitqueue_active(&tty->read_wait))
3064 tty->minimum_to_wake = N_TTY_BUF_SIZE;
3065 }
5d1e3230
JC
3066 retval = 0;
3067out:
3068 unlock_kernel();
3069 return retval;
1da177e4
LT
3070}
3071
af9b897e
AC
3072/**
3073 * tiocsti - fake input character
3074 * @tty: tty to fake input into
3075 * @p: pointer to character
3076 *
3a4fa0a2 3077 * Fake input to a tty device. Does the necessary locking and
af9b897e
AC
3078 * input management.
3079 *
3080 * FIXME: does not honour flow control ??
3081 *
3082 * Locking:
3083 * Called functions take tty_ldisc_lock
3084 * current->signal->tty check is safe without locks
28298232
AC
3085 *
3086 * FIXME: may race normal receive processing
af9b897e
AC
3087 */
3088
1da177e4
LT
3089static int tiocsti(struct tty_struct *tty, char __user *p)
3090{
3091 char ch, mbz = 0;
3092 struct tty_ldisc *ld;
37bdfb07 3093
1da177e4
LT
3094 if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN))
3095 return -EPERM;
3096 if (get_user(ch, p))
3097 return -EFAULT;
3098 ld = tty_ldisc_ref_wait(tty);
a352def2 3099 ld->ops->receive_buf(tty, &ch, &mbz, 1);
1da177e4
LT
3100 tty_ldisc_deref(ld);
3101 return 0;
3102}
3103
af9b897e
AC
3104/**
3105 * tiocgwinsz - implement window query ioctl
3106 * @tty; tty
3107 * @arg: user buffer for result
3108 *
808a0d38 3109 * Copies the kernel idea of the window size into the user buffer.
af9b897e 3110 *
24ec839c 3111 * Locking: tty->termios_mutex is taken to ensure the winsize data
808a0d38 3112 * is consistent.
af9b897e
AC
3113 */
3114
37bdfb07 3115static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg)
1da177e4 3116{
808a0d38
AC
3117 int err;
3118
5785c95b 3119 mutex_lock(&tty->termios_mutex);
808a0d38 3120 err = copy_to_user(arg, &tty->winsize, sizeof(*arg));
5785c95b 3121 mutex_unlock(&tty->termios_mutex);
808a0d38
AC
3122
3123 return err ? -EFAULT: 0;
1da177e4
LT
3124}
3125
af9b897e
AC
3126/**
3127 * tiocswinsz - implement window size set ioctl
3128 * @tty; tty
3129 * @arg: user buffer for result
3130 *
3131 * Copies the user idea of the window size to the kernel. Traditionally
3132 * this is just advisory information but for the Linux console it
3133 * actually has driver level meaning and triggers a VC resize.
3134 *
3135 * Locking:
ca9bda00
AC
3136 * Called function use the console_sem is used to ensure we do
3137 * not try and resize the console twice at once.
24ec839c
PZ
3138 * The tty->termios_mutex is used to ensure we don't double
3139 * resize and get confused. Lock order - tty->termios_mutex before
ca9bda00 3140 * console sem
af9b897e
AC
3141 */
3142
1da177e4 3143static int tiocswinsz(struct tty_struct *tty, struct tty_struct *real_tty,
37bdfb07 3144 struct winsize __user *arg)
1da177e4
LT
3145{
3146 struct winsize tmp_ws;
47f86834
AC
3147 struct pid *pgrp, *rpgrp;
3148 unsigned long flags;
1da177e4
LT
3149
3150 if (copy_from_user(&tmp_ws, arg, sizeof(*arg)))
3151 return -EFAULT;
ca9bda00 3152
5785c95b 3153 mutex_lock(&tty->termios_mutex);
1da177e4 3154 if (!memcmp(&tmp_ws, &tty->winsize, sizeof(*arg)))
ca9bda00
AC
3155 goto done;
3156
1da177e4
LT
3157#ifdef CONFIG_VT
3158 if (tty->driver->type == TTY_DRIVER_TYPE_CONSOLE) {
5785c95b
AV
3159 if (vc_lock_resize(tty->driver_data, tmp_ws.ws_col,
3160 tmp_ws.ws_row)) {
3161 mutex_unlock(&tty->termios_mutex);
37bdfb07 3162 return -ENXIO;
ca9bda00 3163 }
1da177e4
LT
3164 }
3165#endif
47f86834
AC
3166 /* Get the PID values and reference them so we can
3167 avoid holding the tty ctrl lock while sending signals */
3168 spin_lock_irqsave(&tty->ctrl_lock, flags);
3169 pgrp = get_pid(tty->pgrp);
3170 rpgrp = get_pid(real_tty->pgrp);
3171 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
3172
3173 if (pgrp)
3174 kill_pgrp(pgrp, SIGWINCH, 1);
3175 if (rpgrp != pgrp && rpgrp)
3176 kill_pgrp(rpgrp, SIGWINCH, 1);
3177
3178 put_pid(pgrp);
3179 put_pid(rpgrp);
3180
1da177e4
LT
3181 tty->winsize = tmp_ws;
3182 real_tty->winsize = tmp_ws;
ca9bda00 3183done:
5785c95b 3184 mutex_unlock(&tty->termios_mutex);
1da177e4
LT
3185 return 0;
3186}
3187
af9b897e
AC
3188/**
3189 * tioccons - allow admin to move logical console
3190 * @file: the file to become console
3191 *
3192 * Allow the adminstrator to move the redirected console device
3193 *
3194 * Locking: uses redirect_lock to guard the redirect information
3195 */
3196
1da177e4
LT
3197static int tioccons(struct file *file)
3198{
3199 if (!capable(CAP_SYS_ADMIN))
3200 return -EPERM;
3201 if (file->f_op->write == redirected_tty_write) {
3202 struct file *f;
3203 spin_lock(&redirect_lock);
3204 f = redirect;
3205 redirect = NULL;
3206 spin_unlock(&redirect_lock);
3207 if (f)
3208 fput(f);
3209 return 0;
3210 }
3211 spin_lock(&redirect_lock);
3212 if (redirect) {
3213 spin_unlock(&redirect_lock);
3214 return -EBUSY;
3215 }
3216 get_file(file);
3217 redirect = file;
3218 spin_unlock(&redirect_lock);
3219 return 0;
3220}
3221
af9b897e
AC
3222/**
3223 * fionbio - non blocking ioctl
3224 * @file: file to set blocking value
3225 * @p: user parameter
3226 *
3227 * Historical tty interfaces had a blocking control ioctl before
3228 * the generic functionality existed. This piece of history is preserved
3229 * in the expected tty API of posix OS's.
3230 *
3231 * Locking: none, the open fle handle ensures it won't go away.
3232 */
1da177e4
LT
3233
3234static int fionbio(struct file *file, int __user *p)
3235{
3236 int nonblock;
3237
3238 if (get_user(nonblock, p))
3239 return -EFAULT;
3240
04f378b1
AC
3241 /* file->f_flags is still BKL protected in the fs layer - vomit */
3242 lock_kernel();
1da177e4
LT
3243 if (nonblock)
3244 file->f_flags |= O_NONBLOCK;
3245 else
3246 file->f_flags &= ~O_NONBLOCK;
04f378b1 3247 unlock_kernel();
1da177e4
LT
3248 return 0;
3249}
3250
af9b897e
AC
3251/**
3252 * tiocsctty - set controlling tty
3253 * @tty: tty structure
3254 * @arg: user argument
3255 *
3256 * This ioctl is used to manage job control. It permits a session
3257 * leader to set this tty as the controlling tty for the session.
3258 *
3259 * Locking:
28298232 3260 * Takes tty_mutex() to protect tty instance
24ec839c
PZ
3261 * Takes tasklist_lock internally to walk sessions
3262 * Takes ->siglock() when updating signal->tty
af9b897e
AC
3263 */
3264
1da177e4
LT
3265static int tiocsctty(struct tty_struct *tty, int arg)
3266{
24ec839c 3267 int ret = 0;
ab521dc0 3268 if (current->signal->leader && (task_session(current) == tty->session))
24ec839c
PZ
3269 return ret;
3270
3271 mutex_lock(&tty_mutex);
1da177e4
LT
3272 /*
3273 * The process must be a session leader and
3274 * not have a controlling tty already.
3275 */
24ec839c
PZ
3276 if (!current->signal->leader || current->signal->tty) {
3277 ret = -EPERM;
3278 goto unlock;
3279 }
3280
ab521dc0 3281 if (tty->session) {
1da177e4
LT
3282 /*
3283 * This tty is already the controlling
3284 * tty for another session group!
3285 */
37bdfb07 3286 if (arg == 1 && capable(CAP_SYS_ADMIN)) {
1da177e4
LT
3287 /*
3288 * Steal it away
3289 */
1da177e4 3290 read_lock(&tasklist_lock);
24ec839c 3291 session_clear_tty(tty->session);
1da177e4 3292 read_unlock(&tasklist_lock);
24ec839c
PZ
3293 } else {
3294 ret = -EPERM;
3295 goto unlock;
3296 }
1da177e4 3297 }
24ec839c
PZ
3298 proc_set_tty(current, tty);
3299unlock:
28298232 3300 mutex_unlock(&tty_mutex);
24ec839c 3301 return ret;
1da177e4
LT
3302}
3303
5d0fdf1e
AC
3304/**
3305 * tty_get_pgrp - return a ref counted pgrp pid
3306 * @tty: tty to read
3307 *
3308 * Returns a refcounted instance of the pid struct for the process
3309 * group controlling the tty.
3310 */
3311
3312struct pid *tty_get_pgrp(struct tty_struct *tty)
3313{
3314 unsigned long flags;
3315 struct pid *pgrp;
3316
3317 spin_lock_irqsave(&tty->ctrl_lock, flags);
3318 pgrp = get_pid(tty->pgrp);
3319 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
3320
3321 return pgrp;
3322}
3323EXPORT_SYMBOL_GPL(tty_get_pgrp);
3324
af9b897e
AC
3325/**
3326 * tiocgpgrp - get process group
3327 * @tty: tty passed by user
3328 * @real_tty: tty side of the tty pased by the user if a pty else the tty
3329 * @p: returned pid
3330 *
3331 * Obtain the process group of the tty. If there is no process group
3332 * return an error.
3333 *
24ec839c 3334 * Locking: none. Reference to current->signal->tty is safe.
af9b897e
AC
3335 */
3336
1da177e4
LT
3337static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
3338{
5d0fdf1e
AC
3339 struct pid *pid;
3340 int ret;
1da177e4
LT
3341 /*
3342 * (tty == real_tty) is a cheap way of
3343 * testing if the tty is NOT a master pty.
3344 */
3345 if (tty == real_tty && current->signal->tty != real_tty)
3346 return -ENOTTY;
5d0fdf1e
AC
3347 pid = tty_get_pgrp(real_tty);
3348 ret = put_user(pid_vnr(pid), p);
3349 put_pid(pid);
3350 return ret;
1da177e4
LT
3351}
3352
af9b897e
AC
3353/**
3354 * tiocspgrp - attempt to set process group
3355 * @tty: tty passed by user
3356 * @real_tty: tty side device matching tty passed by user
3357 * @p: pid pointer
3358 *
3359 * Set the process group of the tty to the session passed. Only
3360 * permitted where the tty session is our session.
3361 *
47f86834 3362 * Locking: RCU, ctrl lock
af9b897e
AC
3363 */
3364
1da177e4
LT
3365static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
3366{
04a2e6a5
EB
3367 struct pid *pgrp;
3368 pid_t pgrp_nr;
1da177e4 3369 int retval = tty_check_change(real_tty);
47f86834 3370 unsigned long flags;
1da177e4
LT
3371
3372 if (retval == -EIO)
3373 return -ENOTTY;
3374 if (retval)
3375 return retval;
3376 if (!current->signal->tty ||
3377 (current->signal->tty != real_tty) ||
ab521dc0 3378 (real_tty->session != task_session(current)))
1da177e4 3379 return -ENOTTY;
04a2e6a5 3380 if (get_user(pgrp_nr, p))
1da177e4 3381 return -EFAULT;
04a2e6a5 3382 if (pgrp_nr < 0)
1da177e4 3383 return -EINVAL;
04a2e6a5 3384 rcu_read_lock();
b488893a 3385 pgrp = find_vpid(pgrp_nr);
04a2e6a5
EB
3386 retval = -ESRCH;
3387 if (!pgrp)
3388 goto out_unlock;
3389 retval = -EPERM;
3390 if (session_of_pgrp(pgrp) != task_session(current))
3391 goto out_unlock;
3392 retval = 0;
47f86834 3393 spin_lock_irqsave(&tty->ctrl_lock, flags);
ab521dc0
EB
3394 put_pid(real_tty->pgrp);
3395 real_tty->pgrp = get_pid(pgrp);
47f86834 3396 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
04a2e6a5
EB
3397out_unlock:
3398 rcu_read_unlock();
3399 return retval;
1da177e4
LT
3400}
3401
af9b897e
AC
3402/**
3403 * tiocgsid - get session id
3404 * @tty: tty passed by user
3405 * @real_tty: tty side of the tty pased by the user if a pty else the tty
3406 * @p: pointer to returned session id
3407 *
3408 * Obtain the session id of the tty. If there is no session
3409 * return an error.
3410 *
24ec839c 3411 * Locking: none. Reference to current->signal->tty is safe.
af9b897e
AC
3412 */
3413
1da177e4
LT
3414static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
3415{
3416 /*
3417 * (tty == real_tty) is a cheap way of
3418 * testing if the tty is NOT a master pty.
3419 */
3420 if (tty == real_tty && current->signal->tty != real_tty)
3421 return -ENOTTY;
ab521dc0 3422 if (!real_tty->session)
1da177e4 3423 return -ENOTTY;
b488893a 3424 return put_user(pid_vnr(real_tty->session), p);
1da177e4
LT
3425}
3426
af9b897e
AC
3427/**
3428 * tiocsetd - set line discipline
3429 * @tty: tty device
3430 * @p: pointer to user data
3431 *
3432 * Set the line discipline according to user request.
3433 *
3434 * Locking: see tty_set_ldisc, this function is just a helper
3435 */
3436
1da177e4
LT
3437static int tiocsetd(struct tty_struct *tty, int __user *p)
3438{
3439 int ldisc;
04f378b1 3440 int ret;
1da177e4
LT
3441
3442 if (get_user(ldisc, p))
3443 return -EFAULT;
04f378b1
AC
3444
3445 lock_kernel();
3446 ret = tty_set_ldisc(tty, ldisc);
3447 unlock_kernel();
3448
3449 return ret;
1da177e4
LT
3450}
3451
af9b897e
AC
3452/**
3453 * send_break - performed time break
3454 * @tty: device to break on
3455 * @duration: timeout in mS
3456 *
3457 * Perform a timed break on hardware that lacks its own driver level
3458 * timed break functionality.
3459 *
3460 * Locking:
28298232 3461 * atomic_write_lock serializes
af9b897e 3462 *
af9b897e
AC
3463 */
3464
b20f3ae5 3465static int send_break(struct tty_struct *tty, unsigned int duration)
1da177e4 3466{
9c1729db 3467 if (tty_write_lock(tty, 0) < 0)
f34d7a5b
AC
3468 return -EINTR;
3469 tty->ops->break_ctl(tty, -1);
9c1729db 3470 if (!signal_pending(current))
b20f3ae5 3471 msleep_interruptible(duration);
f34d7a5b 3472 tty->ops->break_ctl(tty, 0);
9c1729db 3473 tty_write_unlock(tty);
3e2a078c 3474 if (signal_pending(current))
f34d7a5b
AC
3475 return -EINTR;
3476 return 0;
1da177e4
LT
3477}
3478
af9b897e 3479/**
f34d7a5b 3480 * tty_tiocmget - get modem status
af9b897e
AC
3481 * @tty: tty device
3482 * @file: user file pointer
3483 * @p: pointer to result
3484 *
3485 * Obtain the modem status bits from the tty driver if the feature
3486 * is supported. Return -EINVAL if it is not available.
3487 *
3488 * Locking: none (up to the driver)
3489 */
3490
3491static int tty_tiocmget(struct tty_struct *tty, struct file *file, int __user *p)
1da177e4
LT
3492{
3493 int retval = -EINVAL;
3494
f34d7a5b
AC
3495 if (tty->ops->tiocmget) {
3496 retval = tty->ops->tiocmget(tty, file);
1da177e4
LT
3497
3498 if (retval >= 0)
3499 retval = put_user(retval, p);
3500 }
3501 return retval;
3502}
3503
af9b897e 3504/**
f34d7a5b 3505 * tty_tiocmset - set modem status
af9b897e
AC
3506 * @tty: tty device
3507 * @file: user file pointer
3508 * @cmd: command - clear bits, set bits or set all
3509 * @p: pointer to desired bits
3510 *
3511 * Set the modem status bits from the tty driver if the feature
3512 * is supported. Return -EINVAL if it is not available.
3513 *
3514 * Locking: none (up to the driver)
3515 */
3516
3517static int tty_tiocmset(struct tty_struct *tty, struct file *file, unsigned int cmd,
1da177e4
LT
3518 unsigned __user *p)
3519{
ae677517
AC
3520 int retval;
3521 unsigned int set, clear, val;
1da177e4 3522
ae677517
AC
3523 if (tty->ops->tiocmset == NULL)
3524 return -EINVAL;
1da177e4 3525
ae677517
AC
3526 retval = get_user(val, p);
3527 if (retval)
3528 return retval;
3529 set = clear = 0;
3530 switch (cmd) {
3531 case TIOCMBIS:
3532 set = val;
3533 break;
3534 case TIOCMBIC:
3535 clear = val;
3536 break;
3537 case TIOCMSET:
3538 set = val;
3539 clear = ~val;
3540 break;
3541 }
3542 set &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
3543 clear &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
3544 return tty->ops->tiocmset(tty, file, set, clear);
1da177e4
LT
3545}
3546
3547/*
3548 * Split this up, as gcc can choke on it otherwise..
3549 */
04f378b1 3550long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1da177e4
LT
3551{
3552 struct tty_struct *tty, *real_tty;
3553 void __user *p = (void __user *)arg;
3554 int retval;
3555 struct tty_ldisc *ld;
04f378b1 3556 struct inode *inode = file->f_dentry->d_inode;
37bdfb07 3557
1da177e4
LT
3558 tty = (struct tty_struct *)file->private_data;
3559 if (tty_paranoia_check(tty, inode, "tty_ioctl"))
3560 return -EINVAL;
3561
3562 real_tty = tty;
3563 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
3564 tty->driver->subtype == PTY_TYPE_MASTER)
3565 real_tty = tty->link;
3566
3567 /*
3568 * Break handling by driver
3569 */
04f378b1
AC
3570
3571 retval = -EINVAL;
3572
f34d7a5b 3573 if (!tty->ops->break_ctl) {
37bdfb07 3574 switch (cmd) {
1da177e4
LT
3575 case TIOCSBRK:
3576 case TIOCCBRK:
f34d7a5b
AC
3577 if (tty->ops->ioctl)
3578 retval = tty->ops->ioctl(tty, file, cmd, arg);
3579 if (retval != -EINVAL && retval != -ENOIOCTLCMD)
3580 printk(KERN_WARNING "tty: driver %s needs updating to use break_ctl\n", tty->driver->name);
04f378b1 3581 return retval;
37bdfb07 3582
1da177e4
LT
3583 /* These two ioctl's always return success; even if */
3584 /* the driver doesn't support them. */
3585 case TCSBRK:
3586 case TCSBRKP:
f34d7a5b 3587 if (!tty->ops->ioctl)
1da177e4 3588 return 0;
f34d7a5b
AC
3589 retval = tty->ops->ioctl(tty, file, cmd, arg);
3590 if (retval != -EINVAL && retval != -ENOIOCTLCMD)
3591 printk(KERN_WARNING "tty: driver %s needs updating to use break_ctl\n", tty->driver->name);
1da177e4
LT
3592 if (retval == -ENOIOCTLCMD)
3593 retval = 0;
3594 return retval;
3595 }
3596 }
3597
3598 /*
3599 * Factor out some common prep work
3600 */
3601 switch (cmd) {
3602 case TIOCSETD:
3603 case TIOCSBRK:
3604 case TIOCCBRK:
3605 case TCSBRK:
37bdfb07 3606 case TCSBRKP:
1da177e4
LT
3607 retval = tty_check_change(tty);
3608 if (retval)
3609 return retval;
3610 if (cmd != TIOCCBRK) {
3611 tty_wait_until_sent(tty, 0);
3612 if (signal_pending(current))
3613 return -EINTR;
3614 }
3615 break;
3616 }
3617
3618 switch (cmd) {
37bdfb07
AC
3619 case TIOCSTI:
3620 return tiocsti(tty, p);
3621 case TIOCGWINSZ:
3622 return tiocgwinsz(tty, p);
3623 case TIOCSWINSZ:
3624 return tiocswinsz(tty, real_tty, p);
3625 case TIOCCONS:
3626 return real_tty != tty ? -EINVAL : tioccons(file);
3627 case FIONBIO:
3628 return fionbio(file, p);
3629 case TIOCEXCL:
3630 set_bit(TTY_EXCLUSIVE, &tty->flags);
3631 return 0;
3632 case TIOCNXCL:
3633 clear_bit(TTY_EXCLUSIVE, &tty->flags);
3634 return 0;
3635 case TIOCNOTTY:
3636 if (current->signal->tty != tty)
3637 return -ENOTTY;
3638 no_tty();
3639 return 0;
3640 case TIOCSCTTY:
3641 return tiocsctty(tty, arg);
3642 case TIOCGPGRP:
3643 return tiocgpgrp(tty, real_tty, p);
3644 case TIOCSPGRP:
3645 return tiocspgrp(tty, real_tty, p);
3646 case TIOCGSID:
3647 return tiocgsid(tty, real_tty, p);
3648 case TIOCGETD:
a352def2 3649 return put_user(tty->ldisc.ops->num, (int __user *)p);
37bdfb07
AC
3650 case TIOCSETD:
3651 return tiocsetd(tty, p);
1da177e4 3652#ifdef CONFIG_VT
37bdfb07
AC
3653 case TIOCLINUX:
3654 return tioclinux(tty, arg);
1da177e4 3655#endif
37bdfb07
AC
3656 /*
3657 * Break handling
3658 */
3659 case TIOCSBRK: /* Turn break on, unconditionally */
f34d7a5b
AC
3660 if (tty->ops->break_ctl)
3661 tty->ops->break_ctl(tty, -1);
37bdfb07 3662 return 0;
1da177e4 3663
37bdfb07 3664 case TIOCCBRK: /* Turn break off, unconditionally */
f34d7a5b
AC
3665 if (tty->ops->break_ctl)
3666 tty->ops->break_ctl(tty, 0);
37bdfb07
AC
3667 return 0;
3668 case TCSBRK: /* SVID version: non-zero arg --> no break */
3669 /* non-zero arg means wait for all output data
3670 * to be sent (performed above) but don't send break.
3671 * This is used by the tcdrain() termios function.
3672 */
3673 if (!arg)
3674 return send_break(tty, 250);
3675 return 0;
3676 case TCSBRKP: /* support for POSIX tcsendbreak() */
3677 return send_break(tty, arg ? arg*100 : 250);
3678
3679 case TIOCMGET:
3680 return tty_tiocmget(tty, file, p);
3681 case TIOCMSET:
3682 case TIOCMBIC:
3683 case TIOCMBIS:
3684 return tty_tiocmset(tty, file, cmd, p);
3685 case TCFLSH:
3686 switch (arg) {
3687 case TCIFLUSH:
3688 case TCIOFLUSH:
3689 /* flush tty buffer and allow ldisc to process ioctl */
3690 tty_buffer_flush(tty);
c5c34d48 3691 break;
37bdfb07
AC
3692 }
3693 break;
1da177e4 3694 }
f34d7a5b
AC
3695 if (tty->ops->ioctl) {
3696 retval = (tty->ops->ioctl)(tty, file, cmd, arg);
1da177e4
LT
3697 if (retval != -ENOIOCTLCMD)
3698 return retval;
3699 }
3700 ld = tty_ldisc_ref_wait(tty);
3701 retval = -EINVAL;
a352def2
AC
3702 if (ld->ops->ioctl) {
3703 retval = ld->ops->ioctl(tty, file, cmd, arg);
1da177e4
LT
3704 if (retval == -ENOIOCTLCMD)
3705 retval = -EINVAL;
3706 }
3707 tty_ldisc_deref(ld);
3708 return retval;
3709}
3710
e10cc1df 3711#ifdef CONFIG_COMPAT
37bdfb07 3712static long tty_compat_ioctl(struct file *file, unsigned int cmd,
e10cc1df
PF
3713 unsigned long arg)
3714{
3715 struct inode *inode = file->f_dentry->d_inode;
3716 struct tty_struct *tty = file->private_data;
3717 struct tty_ldisc *ld;
3718 int retval = -ENOIOCTLCMD;
3719
3720 if (tty_paranoia_check(tty, inode, "tty_ioctl"))
3721 return -EINVAL;
3722
f34d7a5b
AC
3723 if (tty->ops->compat_ioctl) {
3724 retval = (tty->ops->compat_ioctl)(tty, file, cmd, arg);
e10cc1df
PF
3725 if (retval != -ENOIOCTLCMD)
3726 return retval;
3727 }
3728
3729 ld = tty_ldisc_ref_wait(tty);
a352def2
AC
3730 if (ld->ops->compat_ioctl)
3731 retval = ld->ops->compat_ioctl(tty, file, cmd, arg);
e10cc1df
PF
3732 tty_ldisc_deref(ld);
3733
3734 return retval;
3735}
3736#endif
1da177e4
LT
3737
3738/*
3739 * This implements the "Secure Attention Key" --- the idea is to
3740 * prevent trojan horses by killing all processes associated with this
3741 * tty when the user hits the "Secure Attention Key". Required for
3742 * super-paranoid applications --- see the Orange Book for more details.
37bdfb07 3743 *
1da177e4
LT
3744 * This code could be nicer; ideally it should send a HUP, wait a few
3745 * seconds, then send a INT, and then a KILL signal. But you then
3746 * have to coordinate with the init process, since all processes associated
3747 * with the current tty must be dead before the new getty is allowed
3748 * to spawn.
3749 *
3750 * Now, if it would be correct ;-/ The current code has a nasty hole -
3751 * it doesn't catch files in flight. We may send the descriptor to ourselves
3752 * via AF_UNIX socket, close it and later fetch from socket. FIXME.
3753 *
3754 * Nasty bug: do_SAK is being called in interrupt context. This can
3755 * deadlock. We punt it up to process context. AKPM - 16Mar2001
3756 */
8b6312f4 3757void __do_SAK(struct tty_struct *tty)
1da177e4
LT
3758{
3759#ifdef TTY_SOFT_SAK
3760 tty_hangup(tty);
3761#else
652486fb 3762 struct task_struct *g, *p;
ab521dc0 3763 struct pid *session;
1da177e4
LT
3764 int i;
3765 struct file *filp;
badf1662 3766 struct fdtable *fdt;
37bdfb07 3767
1da177e4
LT
3768 if (!tty)
3769 return;
24ec839c 3770 session = tty->session;
37bdfb07 3771
b3f13deb 3772 tty_ldisc_flush(tty);
1da177e4 3773
f34d7a5b 3774 tty_driver_flush_buffer(tty);
37bdfb07 3775
1da177e4 3776 read_lock(&tasklist_lock);
652486fb 3777 /* Kill the entire session */
ab521dc0 3778 do_each_pid_task(session, PIDTYPE_SID, p) {
652486fb 3779 printk(KERN_NOTICE "SAK: killed process %d"
a47afb0f 3780 " (%s): task_session_nr(p)==tty->session\n",
ba25f9dc 3781 task_pid_nr(p), p->comm);
652486fb 3782 send_sig(SIGKILL, p, 1);
ab521dc0 3783 } while_each_pid_task(session, PIDTYPE_SID, p);
652486fb
EB
3784 /* Now kill any processes that happen to have the
3785 * tty open.
3786 */
3787 do_each_thread(g, p) {
3788 if (p->signal->tty == tty) {
1da177e4 3789 printk(KERN_NOTICE "SAK: killed process %d"
a47afb0f 3790 " (%s): task_session_nr(p)==tty->session\n",
ba25f9dc 3791 task_pid_nr(p), p->comm);
1da177e4
LT
3792 send_sig(SIGKILL, p, 1);
3793 continue;
3794 }
3795 task_lock(p);
3796 if (p->files) {
ca99c1da
DS
3797 /*
3798 * We don't take a ref to the file, so we must
3799 * hold ->file_lock instead.
3800 */
3801 spin_lock(&p->files->file_lock);
badf1662 3802 fdt = files_fdtable(p->files);
37bdfb07 3803 for (i = 0; i < fdt->max_fds; i++) {
1da177e4
LT
3804 filp = fcheck_files(p->files, i);
3805 if (!filp)
3806 continue;
3807 if (filp->f_op->read == tty_read &&
3808 filp->private_data == tty) {
3809 printk(KERN_NOTICE "SAK: killed process %d"
3810 " (%s): fd#%d opened to the tty\n",
ba25f9dc 3811 task_pid_nr(p), p->comm, i);
20ac9437 3812 force_sig(SIGKILL, p);
1da177e4
LT
3813 break;
3814 }
3815 }
ca99c1da 3816 spin_unlock(&p->files->file_lock);
1da177e4
LT
3817 }
3818 task_unlock(p);
652486fb 3819 } while_each_thread(g, p);
1da177e4
LT
3820 read_unlock(&tasklist_lock);
3821#endif
3822}
3823
8b6312f4
EB
3824static void do_SAK_work(struct work_struct *work)
3825{
3826 struct tty_struct *tty =
3827 container_of(work, struct tty_struct, SAK_work);
3828 __do_SAK(tty);
3829}
3830
1da177e4
LT
3831/*
3832 * The tq handling here is a little racy - tty->SAK_work may already be queued.
3833 * Fortunately we don't need to worry, because if ->SAK_work is already queued,
3834 * the values which we write to it will be identical to the values which it
3835 * already has. --akpm
3836 */
3837void do_SAK(struct tty_struct *tty)
3838{
3839 if (!tty)
3840 return;
1da177e4
LT
3841 schedule_work(&tty->SAK_work);
3842}
3843
3844EXPORT_SYMBOL(do_SAK);
3845
af9b897e
AC
3846/**
3847 * flush_to_ldisc
65f27f38 3848 * @work: tty structure passed from work queue.
af9b897e
AC
3849 *
3850 * This routine is called out of the software interrupt to flush data
3851 * from the buffer chain to the line discipline.
3852 *
3853 * Locking: holds tty->buf.lock to guard buffer list. Drops the lock
3854 * while invoking the line discipline receive_buf method. The
3855 * receive_buf method is single threaded for each tty instance.
1da177e4 3856 */
37bdfb07 3857
65f27f38 3858static void flush_to_ldisc(struct work_struct *work)
1da177e4 3859{
65f27f38
DH
3860 struct tty_struct *tty =
3861 container_of(work, struct tty_struct, buf.work.work);
1da177e4
LT
3862 unsigned long flags;
3863 struct tty_ldisc *disc;
2c3bb20f 3864 struct tty_buffer *tbuf, *head;
8977d929
PF
3865 char *char_buf;
3866 unsigned char *flag_buf;
1da177e4
LT
3867
3868 disc = tty_ldisc_ref(tty);
3869 if (disc == NULL) /* !TTY_LDISC */
3870 return;
3871
808249ce 3872 spin_lock_irqsave(&tty->buf.lock, flags);
37bdfb07
AC
3873 /* So we know a flush is running */
3874 set_bit(TTY_FLUSHING, &tty->flags);
2c3bb20f
PF
3875 head = tty->buf.head;
3876 if (head != NULL) {
3877 tty->buf.head = NULL;
3878 for (;;) {
3879 int count = head->commit - head->read;
3880 if (!count) {
3881 if (head->next == NULL)
3882 break;
3883 tbuf = head;
3884 head = head->next;
3885 tty_buffer_free(tty, tbuf);
3886 continue;
3887 }
42fd552e
AC
3888 /* Ldisc or user is trying to flush the buffers
3889 we are feeding to the ldisc, stop feeding the
3890 line discipline as we want to empty the queue */
3891 if (test_bit(TTY_FLUSHPENDING, &tty->flags))
3892 break;
2c3bb20f
PF
3893 if (!tty->receive_room) {
3894 schedule_delayed_work(&tty->buf.work, 1);
3895 break;
3896 }
3897 if (count > tty->receive_room)
3898 count = tty->receive_room;
3899 char_buf = head->char_buf_ptr + head->read;
3900 flag_buf = head->flag_buf_ptr + head->read;
3901 head->read += count;
8977d929 3902 spin_unlock_irqrestore(&tty->buf.lock, flags);
a352def2
AC
3903 disc->ops->receive_buf(tty, char_buf,
3904 flag_buf, count);
8977d929
PF
3905 spin_lock_irqsave(&tty->buf.lock, flags);
3906 }
42fd552e 3907 /* Restore the queue head */
2c3bb20f 3908 tty->buf.head = head;
33f0f88f 3909 }
42fd552e
AC
3910 /* We may have a deferred request to flush the input buffer,
3911 if so pull the chain under the lock and empty the queue */
3912 if (test_bit(TTY_FLUSHPENDING, &tty->flags)) {
3913 __tty_buffer_flush(tty);
3914 clear_bit(TTY_FLUSHPENDING, &tty->flags);
3915 wake_up(&tty->read_wait);
3916 }
3917 clear_bit(TTY_FLUSHING, &tty->flags);
808249ce 3918 spin_unlock_irqrestore(&tty->buf.lock, flags);
817d6d3b 3919
1da177e4
LT
3920 tty_ldisc_deref(disc);
3921}
3922
1da177e4
LT
3923/**
3924 * tty_flip_buffer_push - terminal
3925 * @tty: tty to push
3926 *
3927 * Queue a push of the terminal flip buffers to the line discipline. This
3928 * function must not be called from IRQ context if tty->low_latency is set.
3929 *
3930 * In the event of the queue being busy for flipping the work will be
3931 * held off and retried later.
af9b897e
AC
3932 *
3933 * Locking: tty buffer lock. Driver locks in low latency mode.
1da177e4
LT
3934 */
3935
3936void tty_flip_buffer_push(struct tty_struct *tty)
3937{
808249ce
PF
3938 unsigned long flags;
3939 spin_lock_irqsave(&tty->buf.lock, flags);
33b37a33 3940 if (tty->buf.tail != NULL)
8977d929 3941 tty->buf.tail->commit = tty->buf.tail->used;
808249ce
PF
3942 spin_unlock_irqrestore(&tty->buf.lock, flags);
3943
1da177e4 3944 if (tty->low_latency)
65f27f38 3945 flush_to_ldisc(&tty->buf.work.work);
1da177e4 3946 else
33f0f88f 3947 schedule_delayed_work(&tty->buf.work, 1);
1da177e4
LT
3948}
3949
3950EXPORT_SYMBOL(tty_flip_buffer_push);
3951
33f0f88f 3952
af9b897e
AC
3953/**
3954 * initialize_tty_struct
3955 * @tty: tty to initialize
3956 *
3957 * This subroutine initializes a tty structure that has been newly
3958 * allocated.
3959 *
3960 * Locking: none - tty in question must not be exposed at this point
1da177e4 3961 */
af9b897e 3962
1da177e4
LT
3963static void initialize_tty_struct(struct tty_struct *tty)
3964{
a352def2 3965 struct tty_ldisc ld;
1da177e4
LT
3966 memset(tty, 0, sizeof(struct tty_struct));
3967 tty->magic = TTY_MAGIC;
a352def2
AC
3968 if (tty_ldisc_get(N_TTY, &ld) < 0)
3969 panic("n_tty: init_tty");
3970 tty_ldisc_assign(tty, &ld);
ab521dc0
EB
3971 tty->session = NULL;
3972 tty->pgrp = NULL;
1da177e4 3973 tty->overrun_time = jiffies;
33f0f88f
AC
3974 tty->buf.head = tty->buf.tail = NULL;
3975 tty_buffer_init(tty);
65f27f38 3976 INIT_DELAYED_WORK(&tty->buf.work, flush_to_ldisc);
5785c95b 3977 mutex_init(&tty->termios_mutex);
1da177e4
LT
3978 init_waitqueue_head(&tty->write_wait);
3979 init_waitqueue_head(&tty->read_wait);
65f27f38 3980 INIT_WORK(&tty->hangup_work, do_tty_hangup);
70522e12
IM
3981 mutex_init(&tty->atomic_read_lock);
3982 mutex_init(&tty->atomic_write_lock);
1da177e4 3983 spin_lock_init(&tty->read_lock);
04f378b1 3984 spin_lock_init(&tty->ctrl_lock);
1da177e4 3985 INIT_LIST_HEAD(&tty->tty_files);
7f1f86a0 3986 INIT_WORK(&tty->SAK_work, do_SAK_work);
1da177e4
LT
3987}
3988
f34d7a5b
AC
3989/**
3990 * tty_put_char - write one character to a tty
3991 * @tty: tty
3992 * @ch: character
3993 *
3994 * Write one byte to the tty using the provided put_char method
3995 * if present. Returns the number of characters successfully output.
3996 *
3997 * Note: the specific put_char operation in the driver layer may go
3998 * away soon. Don't call it directly, use this method
1da177e4 3999 */
af9b897e 4000
f34d7a5b 4001int tty_put_char(struct tty_struct *tty, unsigned char ch)
1da177e4 4002{
f34d7a5b
AC
4003 if (tty->ops->put_char)
4004 return tty->ops->put_char(tty, ch);
4005 return tty->ops->write(tty, &ch, 1);
1da177e4
LT
4006}
4007
f34d7a5b
AC
4008EXPORT_SYMBOL_GPL(tty_put_char);
4009
7fe845d1 4010static struct class *tty_class;
1da177e4
LT
4011
4012/**
af9b897e
AC
4013 * tty_register_device - register a tty device
4014 * @driver: the tty driver that describes the tty device
4015 * @index: the index in the tty driver for this tty device
4016 * @device: a struct device that is associated with this tty device.
4017 * This field is optional, if there is no known struct device
4018 * for this tty device it can be set to NULL safely.
1da177e4 4019 *
01107d34
GKH
4020 * Returns a pointer to the struct device for this tty device
4021 * (or ERR_PTR(-EFOO) on error).
1cdcb6b4 4022 *
af9b897e
AC
4023 * This call is required to be made to register an individual tty device
4024 * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If
4025 * that bit is not set, this function should not be called by a tty
4026 * driver.
4027 *
4028 * Locking: ??
1da177e4 4029 */
af9b897e 4030
01107d34
GKH
4031struct device *tty_register_device(struct tty_driver *driver, unsigned index,
4032 struct device *device)
1da177e4
LT
4033{
4034 char name[64];
4035 dev_t dev = MKDEV(driver->major, driver->minor_start) + index;
4036
4037 if (index >= driver->num) {
4038 printk(KERN_ERR "Attempt to register invalid tty line number "
4039 " (%d).\n", index);
1cdcb6b4 4040 return ERR_PTR(-EINVAL);
1da177e4
LT
4041 }
4042
1da177e4
LT
4043 if (driver->type == TTY_DRIVER_TYPE_PTY)
4044 pty_line_name(driver, index, name);
4045 else
4046 tty_line_name(driver, index, name);
1cdcb6b4 4047
01107d34 4048 return device_create(tty_class, device, dev, name);
1da177e4
LT
4049}
4050
4051/**
af9b897e
AC
4052 * tty_unregister_device - unregister a tty device
4053 * @driver: the tty driver that describes the tty device
4054 * @index: the index in the tty driver for this tty device
1da177e4 4055 *
af9b897e
AC
4056 * If a tty device is registered with a call to tty_register_device() then
4057 * this function must be called when the tty device is gone.
4058 *
4059 * Locking: ??
1da177e4 4060 */
af9b897e 4061
1da177e4
LT
4062void tty_unregister_device(struct tty_driver *driver, unsigned index)
4063{
37bdfb07
AC
4064 device_destroy(tty_class,
4065 MKDEV(driver->major, driver->minor_start) + index);
1da177e4
LT
4066}
4067
4068EXPORT_SYMBOL(tty_register_device);
4069EXPORT_SYMBOL(tty_unregister_device);
4070
4071struct tty_driver *alloc_tty_driver(int lines)
4072{
4073 struct tty_driver *driver;
4074
506eb99a 4075 driver = kzalloc(sizeof(struct tty_driver), GFP_KERNEL);
1da177e4 4076 if (driver) {
1da177e4
LT
4077 driver->magic = TTY_DRIVER_MAGIC;
4078 driver->num = lines;
4079 /* later we'll move allocation of tables here */
4080 }
4081 return driver;
4082}
4083
4084void put_tty_driver(struct tty_driver *driver)
4085{
4086 kfree(driver);
4087}
4088
b68e31d0
JD
4089void tty_set_operations(struct tty_driver *driver,
4090 const struct tty_operations *op)
1da177e4 4091{
f34d7a5b
AC
4092 driver->ops = op;
4093};
1da177e4
LT
4094
4095EXPORT_SYMBOL(alloc_tty_driver);
4096EXPORT_SYMBOL(put_tty_driver);
4097EXPORT_SYMBOL(tty_set_operations);
4098
4099/*
4100 * Called by a tty driver to register itself.
4101 */
4102int tty_register_driver(struct tty_driver *driver)
4103{
4104 int error;
37bdfb07 4105 int i;
1da177e4
LT
4106 dev_t dev;
4107 void **p = NULL;
4108
4109 if (driver->flags & TTY_DRIVER_INSTALLED)
4110 return 0;
4111
543691a6
AW
4112 if (!(driver->flags & TTY_DRIVER_DEVPTS_MEM) && driver->num) {
4113 p = kzalloc(driver->num * 3 * sizeof(void *), GFP_KERNEL);
1da177e4
LT
4114 if (!p)
4115 return -ENOMEM;
1da177e4
LT
4116 }
4117
4118 if (!driver->major) {
37bdfb07
AC
4119 error = alloc_chrdev_region(&dev, driver->minor_start,
4120 driver->num, driver->name);
1da177e4
LT
4121 if (!error) {
4122 driver->major = MAJOR(dev);
4123 driver->minor_start = MINOR(dev);
4124 }
4125 } else {
4126 dev = MKDEV(driver->major, driver->minor_start);
e5717c48 4127 error = register_chrdev_region(dev, driver->num, driver->name);
1da177e4
LT
4128 }
4129 if (error < 0) {
4130 kfree(p);
4131 return error;
4132 }
4133
4134 if (p) {
4135 driver->ttys = (struct tty_struct **)p;
edc6afc5 4136 driver->termios = (struct ktermios **)(p + driver->num);
37bdfb07
AC
4137 driver->termios_locked = (struct ktermios **)
4138 (p + driver->num * 2);
1da177e4
LT
4139 } else {
4140 driver->ttys = NULL;
4141 driver->termios = NULL;
4142 driver->termios_locked = NULL;
4143 }
4144
4145 cdev_init(&driver->cdev, &tty_fops);
4146 driver->cdev.owner = driver->owner;
4147 error = cdev_add(&driver->cdev, dev, driver->num);
4148 if (error) {
1da177e4
LT
4149 unregister_chrdev_region(dev, driver->num);
4150 driver->ttys = NULL;
4151 driver->termios = driver->termios_locked = NULL;
4152 kfree(p);
4153 return error;
4154 }
4155
ca509f69 4156 mutex_lock(&tty_mutex);
1da177e4 4157 list_add(&driver->tty_drivers, &tty_drivers);
ca509f69 4158 mutex_unlock(&tty_mutex);
37bdfb07
AC
4159
4160 if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) {
4161 for (i = 0; i < driver->num; i++)
1da177e4
LT
4162 tty_register_device(driver, i, NULL);
4163 }
4164 proc_tty_register_driver(driver);
4165 return 0;
4166}
4167
4168EXPORT_SYMBOL(tty_register_driver);
4169
4170/*
4171 * Called by a tty driver to unregister itself.
4172 */
4173int tty_unregister_driver(struct tty_driver *driver)
4174{
4175 int i;
edc6afc5 4176 struct ktermios *tp;
1da177e4
LT
4177 void *p;
4178
4179 if (driver->refcount)
4180 return -EBUSY;
4181
4182 unregister_chrdev_region(MKDEV(driver->major, driver->minor_start),
4183 driver->num);
ca509f69 4184 mutex_lock(&tty_mutex);
1da177e4 4185 list_del(&driver->tty_drivers);
ca509f69 4186 mutex_unlock(&tty_mutex);
1da177e4
LT
4187
4188 /*
4189 * Free the termios and termios_locked structures because
4190 * we don't want to get memory leaks when modular tty
4191 * drivers are removed from the kernel.
4192 */
4193 for (i = 0; i < driver->num; i++) {
4194 tp = driver->termios[i];
4195 if (tp) {
4196 driver->termios[i] = NULL;
4197 kfree(tp);
4198 }
4199 tp = driver->termios_locked[i];
4200 if (tp) {
4201 driver->termios_locked[i] = NULL;
4202 kfree(tp);
4203 }
331b8319 4204 if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV))
1da177e4
LT
4205 tty_unregister_device(driver, i);
4206 }
4207 p = driver->ttys;
4208 proc_tty_unregister_driver(driver);
4209 driver->ttys = NULL;
4210 driver->termios = driver->termios_locked = NULL;
4211 kfree(p);
4212 cdev_del(&driver->cdev);
4213 return 0;
4214}
1da177e4
LT
4215EXPORT_SYMBOL(tty_unregister_driver);
4216
24ec839c
PZ
4217dev_t tty_devnum(struct tty_struct *tty)
4218{
4219 return MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index;
4220}
4221EXPORT_SYMBOL(tty_devnum);
4222
4223void proc_clear_tty(struct task_struct *p)
4224{
4225 spin_lock_irq(&p->sighand->siglock);
4226 p->signal->tty = NULL;
4227 spin_unlock_irq(&p->sighand->siglock);
4228}
7cac4ce5 4229EXPORT_SYMBOL(proc_clear_tty);
24ec839c 4230
47f86834
AC
4231/* Called under the sighand lock */
4232
2a65f1d9 4233static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty)
24ec839c
PZ
4234{
4235 if (tty) {
47f86834
AC
4236 unsigned long flags;
4237 /* We should not have a session or pgrp to put here but.... */
4238 spin_lock_irqsave(&tty->ctrl_lock, flags);
d9c1e9a8
EB
4239 put_pid(tty->session);
4240 put_pid(tty->pgrp);
ab521dc0 4241 tty->pgrp = get_pid(task_pgrp(tsk));
47f86834
AC
4242 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
4243 tty->session = get_pid(task_session(tsk));
24ec839c 4244 }
2a65f1d9 4245 put_pid(tsk->signal->tty_old_pgrp);
24ec839c 4246 tsk->signal->tty = tty;
ab521dc0 4247 tsk->signal->tty_old_pgrp = NULL;
24ec839c
PZ
4248}
4249
98a27ba4 4250static void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty)
24ec839c
PZ
4251{
4252 spin_lock_irq(&tsk->sighand->siglock);
2a65f1d9 4253 __proc_set_tty(tsk, tty);
24ec839c
PZ
4254 spin_unlock_irq(&tsk->sighand->siglock);
4255}
4256
4257struct tty_struct *get_current_tty(void)
4258{
4259 struct tty_struct *tty;
4260 WARN_ON_ONCE(!mutex_is_locked(&tty_mutex));
4261 tty = current->signal->tty;
4262 /*
4263 * session->tty can be changed/cleared from under us, make sure we
4264 * issue the load. The obtained pointer, when not NULL, is valid as
4265 * long as we hold tty_mutex.
4266 */
4267 barrier();
4268 return tty;
4269}
a311f743 4270EXPORT_SYMBOL_GPL(get_current_tty);
1da177e4
LT
4271
4272/*
4273 * Initialize the console device. This is called *early*, so
4274 * we can't necessarily depend on lots of kernel help here.
4275 * Just do some early initializations, and do the complex setup
4276 * later.
4277 */
4278void __init console_init(void)
4279{
4280 initcall_t *call;
4281
4282 /* Setup the default TTY line discipline. */
4283 (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
4284
4285 /*
37bdfb07 4286 * set up the console device so that later boot sequences can
1da177e4
LT
4287 * inform about problems etc..
4288 */
1da177e4
LT
4289 call = __con_initcall_start;
4290 while (call < __con_initcall_end) {
4291 (*call)();
4292 call++;
4293 }
4294}
4295
1da177e4
LT
4296static int __init tty_class_init(void)
4297{
7fe845d1 4298 tty_class = class_create(THIS_MODULE, "tty");
1da177e4
LT
4299 if (IS_ERR(tty_class))
4300 return PTR_ERR(tty_class);
4301 return 0;
4302}
4303
4304postcore_initcall(tty_class_init);
4305
4306/* 3/2004 jmc: why do these devices exist? */
4307
4308static struct cdev tty_cdev, console_cdev;
4309#ifdef CONFIG_UNIX98_PTYS
4310static struct cdev ptmx_cdev;
4311#endif
4312#ifdef CONFIG_VT
4313static struct cdev vc0_cdev;
4314#endif
4315
4316/*
4317 * Ok, now we can initialize the rest of the tty devices and can count
4318 * on memory allocations, interrupts etc..
4319 */
4320static int __init tty_init(void)
4321{
4322 cdev_init(&tty_cdev, &tty_fops);
4323 if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) ||
4324 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0)
4325 panic("Couldn't register /dev/tty driver\n");
01107d34 4326 device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), "tty");
1da177e4
LT
4327
4328 cdev_init(&console_cdev, &console_fops);
4329 if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) ||
4330 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0)
4331 panic("Couldn't register /dev/console driver\n");
01107d34 4332 device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 1), "console");
1da177e4
LT
4333
4334#ifdef CONFIG_UNIX98_PTYS
4335 cdev_init(&ptmx_cdev, &ptmx_fops);
4336 if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) ||
4337 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0)
4338 panic("Couldn't register /dev/ptmx driver\n");
01107d34 4339 device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), "ptmx");
1da177e4
LT
4340#endif
4341
4342#ifdef CONFIG_VT
4343 cdev_init(&vc0_cdev, &console_fops);
4344 if (cdev_add(&vc0_cdev, MKDEV(TTY_MAJOR, 0), 1) ||
4345 register_chrdev_region(MKDEV(TTY_MAJOR, 0), 1, "/dev/vc/0") < 0)
4346 panic("Couldn't register /dev/tty0 driver\n");
01107d34 4347 device_create(tty_class, NULL, MKDEV(TTY_MAJOR, 0), "tty0");
1da177e4
LT
4348
4349 vty_init();
4350#endif
4351 return 0;
4352}
4353module_init(tty_init);