clk: socfpga: switch to GENMASK()
[deliverable/linux.git] / drivers / tty / pty.c
1 /*
2 * Copyright (C) 1991, 1992 Linus Torvalds
3 *
4 * Added support for a Unix98-style ptmx device.
5 * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998
6 *
7 */
8
9 #include <linux/module.h>
10
11 #include <linux/errno.h>
12 #include <linux/interrupt.h>
13 #include <linux/tty.h>
14 #include <linux/tty_flip.h>
15 #include <linux/fcntl.h>
16 #include <linux/sched.h>
17 #include <linux/string.h>
18 #include <linux/major.h>
19 #include <linux/mm.h>
20 #include <linux/init.h>
21 #include <linux/device.h>
22 #include <linux/uaccess.h>
23 #include <linux/bitops.h>
24 #include <linux/devpts_fs.h>
25 #include <linux/slab.h>
26 #include <linux/mutex.h>
27 #include <linux/poll.h>
28
29
30 #ifdef CONFIG_UNIX98_PTYS
31 static struct tty_driver *ptm_driver;
32 static struct tty_driver *pts_driver;
33 static DEFINE_MUTEX(devpts_mutex);
34 #endif
35
36 static void pty_close(struct tty_struct *tty, struct file *filp)
37 {
38 BUG_ON(!tty);
39 if (tty->driver->subtype == PTY_TYPE_MASTER)
40 WARN_ON(tty->count > 1);
41 else {
42 if (test_bit(TTY_IO_ERROR, &tty->flags))
43 return;
44 if (tty->count > 2)
45 return;
46 }
47 set_bit(TTY_IO_ERROR, &tty->flags);
48 wake_up_interruptible(&tty->read_wait);
49 wake_up_interruptible(&tty->write_wait);
50 spin_lock_irq(&tty->ctrl_lock);
51 tty->packet = 0;
52 spin_unlock_irq(&tty->ctrl_lock);
53 /* Review - krefs on tty_link ?? */
54 if (!tty->link)
55 return;
56 set_bit(TTY_OTHER_CLOSED, &tty->link->flags);
57 tty_flip_buffer_push(tty->link->port);
58 wake_up_interruptible(&tty->link->write_wait);
59 if (tty->driver->subtype == PTY_TYPE_MASTER) {
60 set_bit(TTY_OTHER_CLOSED, &tty->flags);
61 #ifdef CONFIG_UNIX98_PTYS
62 if (tty->driver == ptm_driver) {
63 mutex_lock(&devpts_mutex);
64 if (tty->link->driver_data)
65 devpts_pty_kill(tty->link->driver_data);
66 mutex_unlock(&devpts_mutex);
67 }
68 #endif
69 tty_vhangup(tty->link);
70 }
71 }
72
73 /*
74 * The unthrottle routine is called by the line discipline to signal
75 * that it can receive more characters. For PTY's, the TTY_THROTTLED
76 * flag is always set, to force the line discipline to always call the
77 * unthrottle routine when there are fewer than TTY_THRESHOLD_UNTHROTTLE
78 * characters in the queue. This is necessary since each time this
79 * happens, we need to wake up any sleeping processes that could be
80 * (1) trying to send data to the pty, or (2) waiting in wait_until_sent()
81 * for the pty buffer to be drained.
82 */
83 static void pty_unthrottle(struct tty_struct *tty)
84 {
85 tty_wakeup(tty->link);
86 set_bit(TTY_THROTTLED, &tty->flags);
87 }
88
89 /**
90 * pty_write - write to a pty
91 * @tty: the tty we write from
92 * @buf: kernel buffer of data
93 * @count: bytes to write
94 *
95 * Our "hardware" write method. Data is coming from the ldisc which
96 * may be in a non sleeping state. We simply throw this at the other
97 * end of the link as if we were an IRQ handler receiving stuff for
98 * the other side of the pty/tty pair.
99 */
100
101 static int pty_write(struct tty_struct *tty, const unsigned char *buf, int c)
102 {
103 struct tty_struct *to = tty->link;
104
105 if (tty->stopped)
106 return 0;
107
108 if (c > 0) {
109 /* Stuff the data into the input queue of the other end */
110 c = tty_insert_flip_string(to->port, buf, c);
111 /* And shovel */
112 if (c)
113 tty_flip_buffer_push(to->port);
114 }
115 return c;
116 }
117
118 /**
119 * pty_write_room - write space
120 * @tty: tty we are writing from
121 *
122 * Report how many bytes the ldisc can send into the queue for
123 * the other device.
124 */
125
126 static int pty_write_room(struct tty_struct *tty)
127 {
128 if (tty->stopped)
129 return 0;
130 return tty_buffer_space_avail(tty->link->port);
131 }
132
133 /**
134 * pty_chars_in_buffer - characters currently in our tx queue
135 * @tty: our tty
136 *
137 * Report how much we have in the transmit queue. As everything is
138 * instantly at the other end this is easy to implement.
139 */
140
141 static int pty_chars_in_buffer(struct tty_struct *tty)
142 {
143 return 0;
144 }
145
146 /* Set the lock flag on a pty */
147 static int pty_set_lock(struct tty_struct *tty, int __user *arg)
148 {
149 int val;
150 if (get_user(val, arg))
151 return -EFAULT;
152 if (val)
153 set_bit(TTY_PTY_LOCK, &tty->flags);
154 else
155 clear_bit(TTY_PTY_LOCK, &tty->flags);
156 return 0;
157 }
158
159 static int pty_get_lock(struct tty_struct *tty, int __user *arg)
160 {
161 int locked = test_bit(TTY_PTY_LOCK, &tty->flags);
162 return put_user(locked, arg);
163 }
164
165 /* Set the packet mode on a pty */
166 static int pty_set_pktmode(struct tty_struct *tty, int __user *arg)
167 {
168 int pktmode;
169
170 if (get_user(pktmode, arg))
171 return -EFAULT;
172
173 spin_lock_irq(&tty->ctrl_lock);
174 if (pktmode) {
175 if (!tty->packet) {
176 tty->link->ctrl_status = 0;
177 smp_mb();
178 tty->packet = 1;
179 }
180 } else
181 tty->packet = 0;
182 spin_unlock_irq(&tty->ctrl_lock);
183
184 return 0;
185 }
186
187 /* Get the packet mode of a pty */
188 static int pty_get_pktmode(struct tty_struct *tty, int __user *arg)
189 {
190 int pktmode = tty->packet;
191 return put_user(pktmode, arg);
192 }
193
194 /* Send a signal to the slave */
195 static int pty_signal(struct tty_struct *tty, int sig)
196 {
197 struct pid *pgrp;
198
199 if (sig != SIGINT && sig != SIGQUIT && sig != SIGTSTP)
200 return -EINVAL;
201
202 if (tty->link) {
203 pgrp = tty_get_pgrp(tty->link);
204 if (pgrp)
205 kill_pgrp(pgrp, sig, 1);
206 put_pid(pgrp);
207 }
208 return 0;
209 }
210
211 static void pty_flush_buffer(struct tty_struct *tty)
212 {
213 struct tty_struct *to = tty->link;
214 struct tty_ldisc *ld;
215
216 if (!to)
217 return;
218
219 ld = tty_ldisc_ref(to);
220 tty_buffer_flush(to, ld);
221 if (ld)
222 tty_ldisc_deref(ld);
223
224 if (to->packet) {
225 spin_lock_irq(&tty->ctrl_lock);
226 tty->ctrl_status |= TIOCPKT_FLUSHWRITE;
227 wake_up_interruptible(&to->read_wait);
228 spin_unlock_irq(&tty->ctrl_lock);
229 }
230 }
231
232 static int pty_open(struct tty_struct *tty, struct file *filp)
233 {
234 if (!tty || !tty->link)
235 return -ENODEV;
236
237 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
238 goto out;
239 if (test_bit(TTY_PTY_LOCK, &tty->link->flags))
240 goto out;
241 if (tty->driver->subtype == PTY_TYPE_SLAVE && tty->link->count != 1)
242 goto out;
243
244 clear_bit(TTY_IO_ERROR, &tty->flags);
245 /* TTY_OTHER_CLOSED must be cleared before TTY_OTHER_DONE */
246 clear_bit(TTY_OTHER_CLOSED, &tty->link->flags);
247 clear_bit(TTY_OTHER_DONE, &tty->link->flags);
248 set_bit(TTY_THROTTLED, &tty->flags);
249 return 0;
250
251 out:
252 set_bit(TTY_IO_ERROR, &tty->flags);
253 return -EIO;
254 }
255
256 static void pty_set_termios(struct tty_struct *tty,
257 struct ktermios *old_termios)
258 {
259 /* See if packet mode change of state. */
260 if (tty->link && tty->link->packet) {
261 int extproc = (old_termios->c_lflag & EXTPROC) |
262 (tty->termios.c_lflag & EXTPROC);
263 int old_flow = ((old_termios->c_iflag & IXON) &&
264 (old_termios->c_cc[VSTOP] == '\023') &&
265 (old_termios->c_cc[VSTART] == '\021'));
266 int new_flow = (I_IXON(tty) &&
267 STOP_CHAR(tty) == '\023' &&
268 START_CHAR(tty) == '\021');
269 if ((old_flow != new_flow) || extproc) {
270 spin_lock_irq(&tty->ctrl_lock);
271 if (old_flow != new_flow) {
272 tty->ctrl_status &= ~(TIOCPKT_DOSTOP | TIOCPKT_NOSTOP);
273 if (new_flow)
274 tty->ctrl_status |= TIOCPKT_DOSTOP;
275 else
276 tty->ctrl_status |= TIOCPKT_NOSTOP;
277 }
278 if (extproc)
279 tty->ctrl_status |= TIOCPKT_IOCTL;
280 spin_unlock_irq(&tty->ctrl_lock);
281 wake_up_interruptible(&tty->link->read_wait);
282 }
283 }
284
285 tty->termios.c_cflag &= ~(CSIZE | PARENB);
286 tty->termios.c_cflag |= (CS8 | CREAD);
287 }
288
289 /**
290 * pty_do_resize - resize event
291 * @tty: tty being resized
292 * @ws: window size being set.
293 *
294 * Update the termios variables and send the necessary signals to
295 * peform a terminal resize correctly
296 */
297
298 static int pty_resize(struct tty_struct *tty, struct winsize *ws)
299 {
300 struct pid *pgrp, *rpgrp;
301 struct tty_struct *pty = tty->link;
302
303 /* For a PTY we need to lock the tty side */
304 mutex_lock(&tty->winsize_mutex);
305 if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
306 goto done;
307
308 /* Signal the foreground process group of both ptys */
309 pgrp = tty_get_pgrp(tty);
310 rpgrp = tty_get_pgrp(pty);
311
312 if (pgrp)
313 kill_pgrp(pgrp, SIGWINCH, 1);
314 if (rpgrp != pgrp && rpgrp)
315 kill_pgrp(rpgrp, SIGWINCH, 1);
316
317 put_pid(pgrp);
318 put_pid(rpgrp);
319
320 tty->winsize = *ws;
321 pty->winsize = *ws; /* Never used so will go away soon */
322 done:
323 mutex_unlock(&tty->winsize_mutex);
324 return 0;
325 }
326
327 /**
328 * pty_start - start() handler
329 * pty_stop - stop() handler
330 * @tty: tty being flow-controlled
331 *
332 * Propagates the TIOCPKT status to the master pty.
333 *
334 * NB: only the master pty can be in packet mode so only the slave
335 * needs start()/stop() handlers
336 */
337 static void pty_start(struct tty_struct *tty)
338 {
339 unsigned long flags;
340
341 if (tty->link && tty->link->packet) {
342 spin_lock_irqsave(&tty->ctrl_lock, flags);
343 tty->ctrl_status &= ~TIOCPKT_STOP;
344 tty->ctrl_status |= TIOCPKT_START;
345 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
346 wake_up_interruptible_poll(&tty->link->read_wait, POLLIN);
347 }
348 }
349
350 static void pty_stop(struct tty_struct *tty)
351 {
352 unsigned long flags;
353
354 if (tty->link && tty->link->packet) {
355 spin_lock_irqsave(&tty->ctrl_lock, flags);
356 tty->ctrl_status &= ~TIOCPKT_START;
357 tty->ctrl_status |= TIOCPKT_STOP;
358 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
359 wake_up_interruptible_poll(&tty->link->read_wait, POLLIN);
360 }
361 }
362
363 /**
364 * pty_common_install - set up the pty pair
365 * @driver: the pty driver
366 * @tty: the tty being instantiated
367 * @legacy: true if this is BSD style
368 *
369 * Perform the initial set up for the tty/pty pair. Called from the
370 * tty layer when the port is first opened.
371 *
372 * Locking: the caller must hold the tty_mutex
373 */
374 static int pty_common_install(struct tty_driver *driver, struct tty_struct *tty,
375 bool legacy)
376 {
377 struct tty_struct *o_tty;
378 struct tty_port *ports[2];
379 int idx = tty->index;
380 int retval = -ENOMEM;
381
382 /* Opening the slave first has always returned -EIO */
383 if (driver->subtype != PTY_TYPE_MASTER)
384 return -EIO;
385
386 ports[0] = kmalloc(sizeof **ports, GFP_KERNEL);
387 ports[1] = kmalloc(sizeof **ports, GFP_KERNEL);
388 if (!ports[0] || !ports[1])
389 goto err;
390 if (!try_module_get(driver->other->owner)) {
391 /* This cannot in fact currently happen */
392 goto err;
393 }
394 o_tty = alloc_tty_struct(driver->other, idx);
395 if (!o_tty)
396 goto err_put_module;
397
398 tty_set_lock_subclass(o_tty);
399 lockdep_set_subclass(&o_tty->termios_rwsem, TTY_LOCK_SLAVE);
400
401 if (legacy) {
402 /* We always use new tty termios data so we can do this
403 the easy way .. */
404 retval = tty_init_termios(tty);
405 if (retval)
406 goto err_deinit_tty;
407
408 retval = tty_init_termios(o_tty);
409 if (retval)
410 goto err_free_termios;
411
412 driver->other->ttys[idx] = o_tty;
413 driver->ttys[idx] = tty;
414 } else {
415 memset(&tty->termios_locked, 0, sizeof(tty->termios_locked));
416 tty->termios = driver->init_termios;
417 memset(&o_tty->termios_locked, 0, sizeof(tty->termios_locked));
418 o_tty->termios = driver->other->init_termios;
419 }
420
421 /*
422 * Everything allocated ... set up the o_tty structure.
423 */
424 tty_driver_kref_get(driver->other);
425 /* Establish the links in both directions */
426 tty->link = o_tty;
427 o_tty->link = tty;
428 tty_port_init(ports[0]);
429 tty_port_init(ports[1]);
430 tty_buffer_set_limit(ports[0], 8192);
431 tty_buffer_set_limit(ports[1], 8192);
432 o_tty->port = ports[0];
433 tty->port = ports[1];
434 o_tty->port->itty = o_tty;
435
436 tty_buffer_set_lock_subclass(o_tty->port);
437
438 tty_driver_kref_get(driver);
439 tty->count++;
440 o_tty->count++;
441 return 0;
442 err_free_termios:
443 if (legacy)
444 tty_free_termios(tty);
445 err_deinit_tty:
446 deinitialize_tty_struct(o_tty);
447 free_tty_struct(o_tty);
448 err_put_module:
449 module_put(driver->other->owner);
450 err:
451 kfree(ports[0]);
452 kfree(ports[1]);
453 return retval;
454 }
455
456 static void pty_cleanup(struct tty_struct *tty)
457 {
458 tty_port_put(tty->port);
459 }
460
461 /* Traditional BSD devices */
462 #ifdef CONFIG_LEGACY_PTYS
463
464 static int pty_install(struct tty_driver *driver, struct tty_struct *tty)
465 {
466 return pty_common_install(driver, tty, true);
467 }
468
469 static void pty_remove(struct tty_driver *driver, struct tty_struct *tty)
470 {
471 struct tty_struct *pair = tty->link;
472 driver->ttys[tty->index] = NULL;
473 if (pair)
474 pair->driver->ttys[pair->index] = NULL;
475 }
476
477 static int pty_bsd_ioctl(struct tty_struct *tty,
478 unsigned int cmd, unsigned long arg)
479 {
480 switch (cmd) {
481 case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
482 return pty_set_lock(tty, (int __user *) arg);
483 case TIOCGPTLCK: /* Get PT Lock status */
484 return pty_get_lock(tty, (int __user *)arg);
485 case TIOCPKT: /* Set PT packet mode */
486 return pty_set_pktmode(tty, (int __user *)arg);
487 case TIOCGPKT: /* Get PT packet mode */
488 return pty_get_pktmode(tty, (int __user *)arg);
489 case TIOCSIG: /* Send signal to other side of pty */
490 return pty_signal(tty, (int) arg);
491 case TIOCGPTN: /* TTY returns ENOTTY, but glibc expects EINVAL here */
492 return -EINVAL;
493 }
494 return -ENOIOCTLCMD;
495 }
496
497 static int legacy_count = CONFIG_LEGACY_PTY_COUNT;
498 module_param(legacy_count, int, 0);
499
500 /*
501 * The master side of a pty can do TIOCSPTLCK and thus
502 * has pty_bsd_ioctl.
503 */
504 static const struct tty_operations master_pty_ops_bsd = {
505 .install = pty_install,
506 .open = pty_open,
507 .close = pty_close,
508 .write = pty_write,
509 .write_room = pty_write_room,
510 .flush_buffer = pty_flush_buffer,
511 .chars_in_buffer = pty_chars_in_buffer,
512 .unthrottle = pty_unthrottle,
513 .ioctl = pty_bsd_ioctl,
514 .cleanup = pty_cleanup,
515 .resize = pty_resize,
516 .remove = pty_remove
517 };
518
519 static const struct tty_operations slave_pty_ops_bsd = {
520 .install = pty_install,
521 .open = pty_open,
522 .close = pty_close,
523 .write = pty_write,
524 .write_room = pty_write_room,
525 .flush_buffer = pty_flush_buffer,
526 .chars_in_buffer = pty_chars_in_buffer,
527 .unthrottle = pty_unthrottle,
528 .set_termios = pty_set_termios,
529 .cleanup = pty_cleanup,
530 .resize = pty_resize,
531 .start = pty_start,
532 .stop = pty_stop,
533 .remove = pty_remove
534 };
535
536 static void __init legacy_pty_init(void)
537 {
538 struct tty_driver *pty_driver, *pty_slave_driver;
539
540 if (legacy_count <= 0)
541 return;
542
543 pty_driver = tty_alloc_driver(legacy_count,
544 TTY_DRIVER_RESET_TERMIOS |
545 TTY_DRIVER_REAL_RAW |
546 TTY_DRIVER_DYNAMIC_ALLOC);
547 if (IS_ERR(pty_driver))
548 panic("Couldn't allocate pty driver");
549
550 pty_slave_driver = tty_alloc_driver(legacy_count,
551 TTY_DRIVER_RESET_TERMIOS |
552 TTY_DRIVER_REAL_RAW |
553 TTY_DRIVER_DYNAMIC_ALLOC);
554 if (IS_ERR(pty_slave_driver))
555 panic("Couldn't allocate pty slave driver");
556
557 pty_driver->driver_name = "pty_master";
558 pty_driver->name = "pty";
559 pty_driver->major = PTY_MASTER_MAJOR;
560 pty_driver->minor_start = 0;
561 pty_driver->type = TTY_DRIVER_TYPE_PTY;
562 pty_driver->subtype = PTY_TYPE_MASTER;
563 pty_driver->init_termios = tty_std_termios;
564 pty_driver->init_termios.c_iflag = 0;
565 pty_driver->init_termios.c_oflag = 0;
566 pty_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
567 pty_driver->init_termios.c_lflag = 0;
568 pty_driver->init_termios.c_ispeed = 38400;
569 pty_driver->init_termios.c_ospeed = 38400;
570 pty_driver->other = pty_slave_driver;
571 tty_set_operations(pty_driver, &master_pty_ops_bsd);
572
573 pty_slave_driver->driver_name = "pty_slave";
574 pty_slave_driver->name = "ttyp";
575 pty_slave_driver->major = PTY_SLAVE_MAJOR;
576 pty_slave_driver->minor_start = 0;
577 pty_slave_driver->type = TTY_DRIVER_TYPE_PTY;
578 pty_slave_driver->subtype = PTY_TYPE_SLAVE;
579 pty_slave_driver->init_termios = tty_std_termios;
580 pty_slave_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
581 pty_slave_driver->init_termios.c_ispeed = 38400;
582 pty_slave_driver->init_termios.c_ospeed = 38400;
583 pty_slave_driver->other = pty_driver;
584 tty_set_operations(pty_slave_driver, &slave_pty_ops_bsd);
585
586 if (tty_register_driver(pty_driver))
587 panic("Couldn't register pty driver");
588 if (tty_register_driver(pty_slave_driver))
589 panic("Couldn't register pty slave driver");
590 }
591 #else
592 static inline void legacy_pty_init(void) { }
593 #endif
594
595 /* Unix98 devices */
596 #ifdef CONFIG_UNIX98_PTYS
597
598 static struct cdev ptmx_cdev;
599
600 static int pty_unix98_ioctl(struct tty_struct *tty,
601 unsigned int cmd, unsigned long arg)
602 {
603 switch (cmd) {
604 case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
605 return pty_set_lock(tty, (int __user *)arg);
606 case TIOCGPTLCK: /* Get PT Lock status */
607 return pty_get_lock(tty, (int __user *)arg);
608 case TIOCPKT: /* Set PT packet mode */
609 return pty_set_pktmode(tty, (int __user *)arg);
610 case TIOCGPKT: /* Get PT packet mode */
611 return pty_get_pktmode(tty, (int __user *)arg);
612 case TIOCGPTN: /* Get PT Number */
613 return put_user(tty->index, (unsigned int __user *)arg);
614 case TIOCSIG: /* Send signal to other side of pty */
615 return pty_signal(tty, (int) arg);
616 }
617
618 return -ENOIOCTLCMD;
619 }
620
621 /**
622 * ptm_unix98_lookup - find a pty master
623 * @driver: ptm driver
624 * @idx: tty index
625 *
626 * Look up a pty master device. Called under the tty_mutex for now.
627 * This provides our locking.
628 */
629
630 static struct tty_struct *ptm_unix98_lookup(struct tty_driver *driver,
631 struct inode *ptm_inode, int idx)
632 {
633 /* Master must be open via /dev/ptmx */
634 return ERR_PTR(-EIO);
635 }
636
637 /**
638 * pts_unix98_lookup - find a pty slave
639 * @driver: pts driver
640 * @idx: tty index
641 *
642 * Look up a pty master device. Called under the tty_mutex for now.
643 * This provides our locking for the tty pointer.
644 */
645
646 static struct tty_struct *pts_unix98_lookup(struct tty_driver *driver,
647 struct inode *pts_inode, int idx)
648 {
649 struct tty_struct *tty;
650
651 mutex_lock(&devpts_mutex);
652 tty = devpts_get_priv(pts_inode);
653 mutex_unlock(&devpts_mutex);
654 /* Master must be open before slave */
655 if (!tty)
656 return ERR_PTR(-EIO);
657 return tty;
658 }
659
660 /* We have no need to install and remove our tty objects as devpts does all
661 the work for us */
662
663 static int pty_unix98_install(struct tty_driver *driver, struct tty_struct *tty)
664 {
665 return pty_common_install(driver, tty, false);
666 }
667
668 static void pty_unix98_remove(struct tty_driver *driver, struct tty_struct *tty)
669 {
670 }
671
672 /* this is called once with whichever end is closed last */
673 static void pty_unix98_shutdown(struct tty_struct *tty)
674 {
675 devpts_kill_index(tty->driver_data, tty->index);
676 }
677
678 static const struct tty_operations ptm_unix98_ops = {
679 .lookup = ptm_unix98_lookup,
680 .install = pty_unix98_install,
681 .remove = pty_unix98_remove,
682 .open = pty_open,
683 .close = pty_close,
684 .write = pty_write,
685 .write_room = pty_write_room,
686 .flush_buffer = pty_flush_buffer,
687 .chars_in_buffer = pty_chars_in_buffer,
688 .unthrottle = pty_unthrottle,
689 .ioctl = pty_unix98_ioctl,
690 .resize = pty_resize,
691 .shutdown = pty_unix98_shutdown,
692 .cleanup = pty_cleanup
693 };
694
695 static const struct tty_operations pty_unix98_ops = {
696 .lookup = pts_unix98_lookup,
697 .install = pty_unix98_install,
698 .remove = pty_unix98_remove,
699 .open = pty_open,
700 .close = pty_close,
701 .write = pty_write,
702 .write_room = pty_write_room,
703 .flush_buffer = pty_flush_buffer,
704 .chars_in_buffer = pty_chars_in_buffer,
705 .unthrottle = pty_unthrottle,
706 .set_termios = pty_set_termios,
707 .start = pty_start,
708 .stop = pty_stop,
709 .shutdown = pty_unix98_shutdown,
710 .cleanup = pty_cleanup,
711 };
712
713 /**
714 * ptmx_open - open a unix 98 pty master
715 * @inode: inode of device file
716 * @filp: file pointer to tty
717 *
718 * Allocate a unix98 pty master device from the ptmx driver.
719 *
720 * Locking: tty_mutex protects the init_dev work. tty->count should
721 * protect the rest.
722 * allocated_ptys_lock handles the list of free pty numbers
723 */
724
725 static int ptmx_open(struct inode *inode, struct file *filp)
726 {
727 struct tty_struct *tty;
728 struct inode *slave_inode;
729 int retval;
730 int index;
731
732 nonseekable_open(inode, filp);
733
734 /* We refuse fsnotify events on ptmx, since it's a shared resource */
735 filp->f_mode |= FMODE_NONOTIFY;
736
737 retval = tty_alloc_file(filp);
738 if (retval)
739 return retval;
740
741 /* find a device that is not in use. */
742 mutex_lock(&devpts_mutex);
743 index = devpts_new_index(inode);
744 if (index < 0) {
745 retval = index;
746 mutex_unlock(&devpts_mutex);
747 goto err_file;
748 }
749
750 mutex_unlock(&devpts_mutex);
751
752 mutex_lock(&tty_mutex);
753 tty = tty_init_dev(ptm_driver, index);
754
755 if (IS_ERR(tty)) {
756 retval = PTR_ERR(tty);
757 goto out;
758 }
759
760 /* The tty returned here is locked so we can safely
761 drop the mutex */
762 mutex_unlock(&tty_mutex);
763
764 set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
765 tty->driver_data = inode;
766
767 tty_add_file(tty, filp);
768
769 slave_inode = devpts_pty_new(inode,
770 MKDEV(UNIX98_PTY_SLAVE_MAJOR, index), index,
771 tty->link);
772 if (IS_ERR(slave_inode)) {
773 retval = PTR_ERR(slave_inode);
774 goto err_release;
775 }
776 tty->link->driver_data = slave_inode;
777
778 retval = ptm_driver->ops->open(tty, filp);
779 if (retval)
780 goto err_release;
781
782 tty_unlock(tty);
783 return 0;
784 err_release:
785 tty_unlock(tty);
786 tty_release(inode, filp);
787 return retval;
788 out:
789 mutex_unlock(&tty_mutex);
790 devpts_kill_index(inode, index);
791 err_file:
792 tty_free_file(filp);
793 return retval;
794 }
795
796 static struct file_operations ptmx_fops;
797
798 static void __init unix98_pty_init(void)
799 {
800 ptm_driver = tty_alloc_driver(NR_UNIX98_PTY_MAX,
801 TTY_DRIVER_RESET_TERMIOS |
802 TTY_DRIVER_REAL_RAW |
803 TTY_DRIVER_DYNAMIC_DEV |
804 TTY_DRIVER_DEVPTS_MEM |
805 TTY_DRIVER_DYNAMIC_ALLOC);
806 if (IS_ERR(ptm_driver))
807 panic("Couldn't allocate Unix98 ptm driver");
808 pts_driver = tty_alloc_driver(NR_UNIX98_PTY_MAX,
809 TTY_DRIVER_RESET_TERMIOS |
810 TTY_DRIVER_REAL_RAW |
811 TTY_DRIVER_DYNAMIC_DEV |
812 TTY_DRIVER_DEVPTS_MEM |
813 TTY_DRIVER_DYNAMIC_ALLOC);
814 if (IS_ERR(pts_driver))
815 panic("Couldn't allocate Unix98 pts driver");
816
817 ptm_driver->driver_name = "pty_master";
818 ptm_driver->name = "ptm";
819 ptm_driver->major = UNIX98_PTY_MASTER_MAJOR;
820 ptm_driver->minor_start = 0;
821 ptm_driver->type = TTY_DRIVER_TYPE_PTY;
822 ptm_driver->subtype = PTY_TYPE_MASTER;
823 ptm_driver->init_termios = tty_std_termios;
824 ptm_driver->init_termios.c_iflag = 0;
825 ptm_driver->init_termios.c_oflag = 0;
826 ptm_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
827 ptm_driver->init_termios.c_lflag = 0;
828 ptm_driver->init_termios.c_ispeed = 38400;
829 ptm_driver->init_termios.c_ospeed = 38400;
830 ptm_driver->other = pts_driver;
831 tty_set_operations(ptm_driver, &ptm_unix98_ops);
832
833 pts_driver->driver_name = "pty_slave";
834 pts_driver->name = "pts";
835 pts_driver->major = UNIX98_PTY_SLAVE_MAJOR;
836 pts_driver->minor_start = 0;
837 pts_driver->type = TTY_DRIVER_TYPE_PTY;
838 pts_driver->subtype = PTY_TYPE_SLAVE;
839 pts_driver->init_termios = tty_std_termios;
840 pts_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
841 pts_driver->init_termios.c_ispeed = 38400;
842 pts_driver->init_termios.c_ospeed = 38400;
843 pts_driver->other = ptm_driver;
844 tty_set_operations(pts_driver, &pty_unix98_ops);
845
846 if (tty_register_driver(ptm_driver))
847 panic("Couldn't register Unix98 ptm driver");
848 if (tty_register_driver(pts_driver))
849 panic("Couldn't register Unix98 pts driver");
850
851 /* Now create the /dev/ptmx special device */
852 tty_default_fops(&ptmx_fops);
853 ptmx_fops.open = ptmx_open;
854
855 cdev_init(&ptmx_cdev, &ptmx_fops);
856 if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) ||
857 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0)
858 panic("Couldn't register /dev/ptmx driver");
859 device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), NULL, "ptmx");
860 }
861
862 #else
863 static inline void unix98_pty_init(void) { }
864 #endif
865
866 static int __init pty_init(void)
867 {
868 legacy_pty_init();
869 unix98_pty_init();
870 return 0;
871 }
872 module_init(pty_init);
This page took 0.094073 seconds and 5 git commands to generate.