Lines Matching +full:num +full:- +full:ss +full:- +full:bits
1 // SPDX-License-Identifier: GPL-2.0
8 * or rs-channels. It also implements echoing, cooked mode etc.
10 * Kill-line thanks to John T Kohl, who also corrected VMIN = VTIME = 0.
19 * between the high-level tty routines (tty_io.c and tty_ioctl.c) and
20 * the low-level tty routines (serial.c, pty.c, console.c). This
21 * makes for cleaner and more compact code. -TYT, 9/17/92
24 * which can be dynamically activated and de-activated by the line
29 * -- TYT, 1/31/92
32 * other bits should be there.
33 * -- Nick Holloway <alfie@dcs.warwick.ac.uk>, 27th May 1993.
36 * -- julian@uhunix.uhcc.hawaii.edu (J. Cowley), 13Jan94
39 * -- ctm@ardi.com, 9Sep95
42 * -- mj@k332.feld.cvut.cz, 19-Nov-95
45 * -- grif@cs.ucr.edu, 5-Dec-95
49 * -- Marko Kohtala <Marko.Kohtala@hut.fi>, March 97
52 * -- Bill Hawes <whawes@star.net>, June 97
55 * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 13-Jan-1998
57 * Added support for a Unix98-style ptmx device.
58 * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998
61 * -- Russell King <rmk@arm.linux.org.uk>
65 * -- Andrew Morton <andrewm@uow.edu.eu> 17Mar01
90 #include <linux/ppp-ioctl.h>
137 /* This list gets poked at by procfs and various bits of boot up code. This
161 * free_tty_struct - free a disused tty
172 put_device(tty->dev); in free_tty_struct()
173 kfree(tty->write_buf); in free_tty_struct()
174 tty->magic = 0xDEADDEAD; in free_tty_struct()
180 return ((struct tty_file_private *)file->private_data)->tty; in file_tty()
189 return -ENOMEM; in tty_alloc_file()
191 file->private_data = priv; in tty_alloc_file()
199 struct tty_file_private *priv = file->private_data; in tty_add_file()
201 priv->tty = tty; in tty_add_file()
202 priv->file = file; in tty_add_file()
204 spin_lock(&tty->files_lock); in tty_add_file()
205 list_add(&priv->list, &tty->tty_files); in tty_add_file()
206 spin_unlock(&tty->files_lock); in tty_add_file()
210 * tty_free_file - free file->private_data
217 struct tty_file_private *priv = file->private_data; in tty_free_file()
219 file->private_data = NULL; in tty_free_file()
226 struct tty_file_private *priv = file->private_data; in tty_del_file()
227 struct tty_struct *tty = priv->tty; in tty_del_file()
229 spin_lock(&tty->files_lock); in tty_del_file()
230 list_del(&priv->list); in tty_del_file()
231 spin_unlock(&tty->files_lock); in tty_del_file()
236 * tty_name - return tty naming
249 return tty->name; in tty_name()
256 if (!tty || !tty->driver) in tty_driver_name()
258 return tty->driver->name; in tty_driver_name()
270 if (tty->magic != TTY_MAGIC) { in tty_paranoia_check()
286 spin_lock(&tty->files_lock); in check_tty_count()
287 list_for_each(p, &tty->tty_files) { in check_tty_count()
290 spin_unlock(&tty->files_lock); in check_tty_count()
291 if (tty->driver->type == TTY_DRIVER_TYPE_PTY && in check_tty_count()
292 tty->driver->subtype == PTY_TYPE_SLAVE && in check_tty_count()
293 tty->link && tty->link->count) in check_tty_count()
295 if (tty_port_kopened(tty->port)) in check_tty_count()
297 if (tty->count != (count + kopen_count)) { in check_tty_count()
298 tty_warn(tty, "%s: tty->count(%d) != (#fd's(%d) + #kopen's(%d))\n", in check_tty_count()
299 routine, tty->count, count, kopen_count); in check_tty_count()
307 * get_tty_driver - find device of a tty
322 dev_t base = MKDEV(p->major, p->minor_start); in get_tty_driver()
323 if (device < base || device >= base + p->num) in get_tty_driver()
325 *index = device - base; in get_tty_driver()
332 * tty_dev_name_to_number - return dev_t for device name
338 * the function returns -ENODEV.
355 return -EINVAL; in tty_dev_name_to_number()
361 prefix_length = str - name; in tty_dev_name_to_number()
365 if (prefix_length == strlen(p->name) && strncmp(name, in tty_dev_name_to_number()
366 p->name, prefix_length) == 0) { in tty_dev_name_to_number()
367 if (index < p->num) { in tty_dev_name_to_number()
368 *number = MKDEV(p->major, p->minor_start + index); in tty_dev_name_to_number()
374 ret = -ENODEV; in tty_dev_name_to_number()
384 * tty_find_polling_driver - find device of a polled tty
405 len = str - name; in tty_find_polling_driver()
411 if (!len || strncmp(name, p->name, len) != 0) in tty_find_polling_driver()
419 if (tty_line >= 0 && tty_line < p->num && p->ops && in tty_find_polling_driver()
420 p->ops->poll_init && !p->ops->poll_init(p, tty_line, stp)) { in tty_find_polling_driver()
440 return -EIO; in hung_up_tty_write()
443 /* No kernel lock held - none needed ;) */
452 return cmd == TIOCSPGRP ? -ENOTTY : -EIO; in hung_up_tty_ioctl()
458 return cmd == TIOCSPGRP ? -ENOTTY : -EIO; in hung_up_tty_compat_ioctl()
463 return -ENOTTY; in hung_up_tty_fasync()
470 if (tty && tty->ops && tty->ops->show_fdinfo) in tty_show_fdinfo()
471 tty->ops->show_fdinfo(tty, m); in tty_show_fdinfo()
520 * tty_wakeup - request more data
532 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) { in tty_wakeup()
535 if (ld->ops->write_wakeup) in tty_wakeup()
536 ld->ops->write_wakeup(tty); in tty_wakeup()
540 wake_up_interruptible_poll(&tty->write_wait, EPOLLOUT); in tty_wakeup()
546 * __tty_hangup - actual handler for hangup events
565 * ->siglock to protect ->signal/->sighand
588 if (test_bit(TTY_HUPPED, &tty->flags)) { in __tty_hangup()
599 set_bit(TTY_HUPPING, &tty->flags); in __tty_hangup()
606 spin_lock(&tty->files_lock); in __tty_hangup()
608 list_for_each_entry(priv, &tty->tty_files, list) { in __tty_hangup()
609 filp = priv->file; in __tty_hangup()
610 if (filp->f_op->write_iter == redirected_tty_write) in __tty_hangup()
612 if (filp->f_op->write_iter != tty_write) in __tty_hangup()
615 __tty_fasync(-1, filp, 0); /* can't block */ in __tty_hangup()
616 filp->f_op = &hung_up_tty_fops; in __tty_hangup()
618 spin_unlock(&tty->files_lock); in __tty_hangup()
621 /* Account for the p->signal references we killed */ in __tty_hangup()
622 while (refs--) in __tty_hangup()
627 spin_lock_irq(&tty->ctrl_lock); in __tty_hangup()
628 clear_bit(TTY_THROTTLED, &tty->flags); in __tty_hangup()
629 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); in __tty_hangup()
630 put_pid(tty->session); in __tty_hangup()
631 put_pid(tty->pgrp); in __tty_hangup()
632 tty->session = NULL; in __tty_hangup()
633 tty->pgrp = NULL; in __tty_hangup()
634 tty->ctrl_status = 0; in __tty_hangup()
635 spin_unlock_irq(&tty->ctrl_lock); in __tty_hangup()
640 * tty->count and state->count to go out of sync. in __tty_hangup()
644 if (tty->ops->close) in __tty_hangup()
646 tty->ops->close(tty, cons_filp); in __tty_hangup()
647 } else if (tty->ops->hangup) in __tty_hangup()
648 tty->ops->hangup(tty); in __tty_hangup()
651 * we did here. The driver layer expects no calls after ->hangup() in __tty_hangup()
654 set_bit(TTY_HUPPED, &tty->flags); in __tty_hangup()
655 clear_bit(TTY_HUPPING, &tty->flags); in __tty_hangup()
671 * tty_hangup - trigger a hangup event
681 schedule_work(&tty->hangup_work); in tty_hangup()
687 * tty_vhangup - process vhangup
705 * tty_vhangup_self - process vhangup for own ctty
722 * tty_vhangup_session - hangup session leader exit
739 * tty_hung_up_p - was tty hung up
748 return (filp && filp->f_op == &hung_up_tty_fops); in tty_hung_up_p()
754 * stop_tty - propagate flow control
758 * on an already stopped device and will not re-call the driver
772 if (tty->stopped) in __stop_tty()
774 tty->stopped = 1; in __stop_tty()
775 if (tty->ops->stop) in __stop_tty()
776 tty->ops->stop(tty); in __stop_tty()
783 spin_lock_irqsave(&tty->flow_lock, flags); in stop_tty()
785 spin_unlock_irqrestore(&tty->flow_lock, flags); in stop_tty()
790 * start_tty - propagate flow control
803 if (!tty->stopped || tty->flow_stopped) in __start_tty()
805 tty->stopped = 0; in __start_tty()
806 if (tty->ops->start) in __start_tty()
807 tty->ops->start(tty); in __start_tty()
815 spin_lock_irqsave(&tty->flow_lock, flags); in start_tty()
817 spin_unlock_irqrestore(&tty->flow_lock, flags); in start_tty()
827 * lower three bits (i.e every 8 seconds). If so, then we can update in tty_update_time()
831 if ((sec ^ time->tv_sec) & ~7) in tty_update_time()
832 time->tv_sec = sec; in tty_update_time()
836 * Iterate on the ldisc ->read() function until we've gotten all
859 size = ld->ops->read(tty, file, kernel_buf, size, &cookie, offset); in iterate_tty_read()
864 /* Did we have an earlier error (ie -EFAULT)? */ in iterate_tty_read()
870 * -EOVERFLOW means we didn't have enough space in iterate_tty_read()
874 if (retval == -EOVERFLOW) in iterate_tty_read()
881 count -= copied; in iterate_tty_read()
884 * If the user copy failed, we still need to do another ->read() in iterate_tty_read()
891 retval = -EFAULT; in iterate_tty_read()
902 * tty_read - read method for tty device files
919 struct file *file = iocb->ki_filp; in tty_read()
925 return -EIO; in tty_read()
927 return -EIO; in tty_read()
934 i = -EIO; in tty_read()
935 if (ld->ops->read) in tty_read()
940 tty_update_time(&inode->i_atime); in tty_read()
947 mutex_unlock(&tty->atomic_write_lock); in tty_write_unlock()
948 wake_up_interruptible_poll(&tty->write_wait, EPOLLOUT); in tty_write_unlock()
953 if (!mutex_trylock(&tty->atomic_write_lock)) { in tty_write_lock()
955 return -EAGAIN; in tty_write_lock()
956 if (mutex_lock_interruptible(&tty->atomic_write_lock)) in tty_write_lock()
957 return -ERESTARTSYS; in tty_write_lock()
964 * denial-of-service type attacks
976 ret = tty_write_lock(tty, file->f_flags & O_NDELAY); in do_tty_write()
982 * simplifies low-level drivers immensely, since they in do_tty_write()
986 * big chunk-size.. in do_tty_write()
988 * The default chunk-size is 2kB, because the NTTY in do_tty_write()
997 if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags)) in do_tty_write()
1003 if (tty->write_cnt < chunk) { in do_tty_write()
1011 ret = -ENOMEM; in do_tty_write()
1014 kfree(tty->write_buf); in do_tty_write()
1015 tty->write_cnt = chunk; in do_tty_write()
1016 tty->write_buf = buf_chunk; in do_tty_write()
1025 ret = -EFAULT; in do_tty_write()
1026 if (copy_from_iter(tty->write_buf, size, from) != size) in do_tty_write()
1029 ret = write(tty, file, tty->write_buf, size); in do_tty_write()
1039 iov_iter_revert(from, size-ret); in do_tty_write()
1041 count -= ret; in do_tty_write()
1044 ret = -ERESTARTSYS; in do_tty_write()
1050 tty_update_time(&file_inode(file)->i_mtime); in do_tty_write()
1059 * tty_write_message - write a message to a certain tty, not just the console.
1073 mutex_lock(&tty->atomic_write_lock); in tty_write_message()
1075 if (tty->ops->write && tty->count > 0) in tty_write_message()
1076 tty->ops->write(tty, msg, strlen(msg)); in tty_write_message()
1085 * tty_write - write method for tty device file
1107 return -EIO; in file_tty_write()
1108 if (!tty || !tty->ops->write || tty_io_error(tty)) in file_tty_write()
1109 return -EIO; in file_tty_write()
1111 if (tty->ops->write_room == NULL) in file_tty_write()
1116 if (!ld->ops->write) in file_tty_write()
1117 ret = -EIO; in file_tty_write()
1119 ret = do_tty_write(ld->ops->write, tty, file, from); in file_tty_write()
1126 return file_tty_write(iocb->ki_filp, iocb, from); in tty_write()
1152 * tty_send_xchar - send priority character
1161 int was_stopped = tty->stopped; in tty_send_xchar()
1163 if (tty->ops->send_xchar) { in tty_send_xchar()
1164 down_read(&tty->termios_rwsem); in tty_send_xchar()
1165 tty->ops->send_xchar(tty, ch); in tty_send_xchar()
1166 up_read(&tty->termios_rwsem); in tty_send_xchar()
1171 return -ERESTARTSYS; in tty_send_xchar()
1173 down_read(&tty->termios_rwsem); in tty_send_xchar()
1176 tty->ops->write(tty, &ch, 1); in tty_send_xchar()
1179 up_read(&tty->termios_rwsem); in tty_send_xchar()
1187 * pty_line_name - generate name for a pty
1199 int i = index + driver->name_base; in pty_line_name()
1200 /* ->name is initialized to "ttyp", but "tty" is expected */ in pty_line_name()
1202 driver->subtype == PTY_TYPE_SLAVE ? "tty" : driver->name, in pty_line_name()
1207 * tty_line_name - generate name for a tty
1219 if (driver->flags & TTY_DRIVER_UNNUMBERED_NODE) in tty_line_name()
1220 return sprintf(p, "%s", driver->name); in tty_line_name()
1222 return sprintf(p, "%s%d", driver->name, in tty_line_name()
1223 index + driver->name_base); in tty_line_name()
1227 * tty_driver_lookup_tty() - find an existing tty, if any
1241 if (driver->ops->lookup) { in tty_driver_lookup_tty()
1243 tty = ERR_PTR(-EIO); in tty_driver_lookup_tty()
1245 tty = driver->ops->lookup(driver, file, idx); in tty_driver_lookup_tty()
1247 if (idx >= driver->num) in tty_driver_lookup_tty()
1248 return ERR_PTR(-EINVAL); in tty_driver_lookup_tty()
1249 tty = driver->ttys[idx]; in tty_driver_lookup_tty()
1257 * tty_init_termios - helper for termios setup
1267 int idx = tty->index; in tty_init_termios()
1269 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) in tty_init_termios()
1270 tty->termios = tty->driver->init_termios; in tty_init_termios()
1273 tp = tty->driver->termios[idx]; in tty_init_termios()
1275 tty->termios = *tp; in tty_init_termios()
1276 tty->termios.c_line = tty->driver->init_termios.c_line; in tty_init_termios()
1278 tty->termios = tty->driver->init_termios; in tty_init_termios()
1281 tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios); in tty_init_termios()
1282 tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios); in tty_init_termios()
1290 tty->count++; in tty_standard_install()
1291 driver->ttys[tty->index] = tty; in tty_standard_install()
1297 * tty_driver_install_tty() - install a tty entry in the driver
1301 * Install a tty object into the driver tables. The tty->index field
1311 return driver->ops->install ? driver->ops->install(driver, tty) : in tty_driver_install_tty()
1316 * tty_driver_remove_tty() - remove a tty from the driver tables
1320 * Remvoe a tty object from the driver tables. The tty->index field
1327 if (driver->ops->remove) in tty_driver_remove_tty()
1328 driver->ops->remove(driver, tty); in tty_driver_remove_tty()
1330 driver->ttys[tty->index] = NULL; in tty_driver_remove_tty()
1334 * tty_reopen() - fast re-open of an open tty
1337 * Return 0 on success, -errno on error.
1338 * Re-opens on master ptys are not allowed and return -EIO.
1344 struct tty_driver *driver = tty->driver; in tty_reopen()
1348 if (driver->type == TTY_DRIVER_TYPE_PTY && in tty_reopen()
1349 driver->subtype == PTY_TYPE_MASTER) in tty_reopen()
1350 return -EIO; in tty_reopen()
1352 if (!tty->count) in tty_reopen()
1353 return -EAGAIN; in tty_reopen()
1355 if (test_bit(TTY_EXCLUSIVE, &tty->flags) && !capable(CAP_SYS_ADMIN)) in tty_reopen()
1356 return -EBUSY; in tty_reopen()
1366 if (!tty->ldisc) in tty_reopen()
1367 retval = tty_ldisc_reinit(tty, tty->termios.c_line); in tty_reopen()
1372 tty->count++; in tty_reopen()
1378 * tty_init_dev - initialise a tty device
1415 if (!try_module_get(driver->owner)) in tty_init_dev()
1416 return ERR_PTR(-ENODEV); in tty_init_dev()
1420 retval = -ENOMEM; in tty_init_dev()
1429 if (!tty->port) in tty_init_dev()
1430 tty->port = driver->ports[idx]; in tty_init_dev()
1432 if (WARN_RATELIMIT(!tty->port, in tty_init_dev()
1433 "%s: %s driver does not set tty->port. This would crash the kernel. Fix the driver!\n", in tty_init_dev()
1434 __func__, tty->driver->name)) { in tty_init_dev()
1435 retval = -EINVAL; in tty_init_dev()
1442 tty->port->itty = tty; in tty_init_dev()
1449 retval = tty_ldisc_setup(tty, tty->link); in tty_init_dev()
1460 module_put(driver->owner); in tty_init_dev()
1475 * tty_save_termios() - save tty termios data in driver table
1483 int idx = tty->index; in tty_save_termios()
1486 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) in tty_save_termios()
1490 tp = tty->driver->termios[idx]; in tty_save_termios()
1495 tty->driver->termios[idx] = tp; in tty_save_termios()
1497 *tp = tty->termios; in tty_save_termios()
1502 * tty_flush_works - flush all works of a tty/pty pair
1509 flush_work(&tty->SAK_work); in tty_flush_works()
1510 flush_work(&tty->hangup_work); in tty_flush_works()
1511 if (tty->link) { in tty_flush_works()
1512 flush_work(&tty->link->SAK_work); in tty_flush_works()
1513 flush_work(&tty->link->hangup_work); in tty_flush_works()
1518 * release_one_tty - release tty structure memory
1536 struct tty_driver *driver = tty->driver; in release_one_tty()
1537 struct module *owner = driver->owner; in release_one_tty()
1539 if (tty->ops->cleanup) in release_one_tty()
1540 tty->ops->cleanup(tty); in release_one_tty()
1542 tty->magic = 0; in release_one_tty()
1546 spin_lock(&tty->files_lock); in release_one_tty()
1547 list_del_init(&tty->tty_files); in release_one_tty()
1548 spin_unlock(&tty->files_lock); in release_one_tty()
1550 put_pid(tty->pgrp); in release_one_tty()
1551 put_pid(tty->session); in release_one_tty()
1561 INIT_WORK(&tty->hangup_work, release_one_tty); in queue_release_one_tty()
1562 schedule_work(&tty->hangup_work); in queue_release_one_tty()
1566 * tty_kref_put - release a tty kref
1576 kref_put(&tty->kref, queue_release_one_tty); in tty_kref_put()
1581 * release_tty - release tty structure memory
1595 WARN_ON(tty->index != idx); in release_tty()
1597 if (tty->ops->shutdown) in release_tty()
1598 tty->ops->shutdown(tty); in release_tty()
1600 tty_driver_remove_tty(tty->driver, tty); in release_tty()
1601 if (tty->port) in release_tty()
1602 tty->port->itty = NULL; in release_tty()
1603 if (tty->link) in release_tty()
1604 tty->link->port->itty = NULL; in release_tty()
1605 if (tty->port) in release_tty()
1606 tty_buffer_cancel_work(tty->port); in release_tty()
1607 if (tty->link) in release_tty()
1608 tty_buffer_cancel_work(tty->link->port); in release_tty()
1610 tty_kref_put(tty->link); in release_tty()
1615 * tty_release_checks - check a tty before real release
1620 * This is a no-op unless TTY_PARANOIA_CHECK is defined.
1625 if (idx < 0 || idx >= tty->driver->num) { in tty_release_checks()
1627 return -1; in tty_release_checks()
1631 if (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM) in tty_release_checks()
1634 if (tty != tty->driver->ttys[idx]) { in tty_release_checks()
1636 idx, tty->driver->ttys[idx]); in tty_release_checks()
1637 return -1; in tty_release_checks()
1639 if (tty->driver->other) { in tty_release_checks()
1640 struct tty_struct *o_tty = tty->link; in tty_release_checks()
1642 if (o_tty != tty->driver->other->ttys[idx]) { in tty_release_checks()
1644 idx, tty->driver->other->ttys[idx]); in tty_release_checks()
1645 return -1; in tty_release_checks()
1647 if (o_tty->link != tty) { in tty_release_checks()
1648 tty_debug(tty, "bad link = %p\n", o_tty->link); in tty_release_checks()
1649 return -1; in tty_release_checks()
1657 * tty_kclose - closes tty opened by tty_kopen
1662 * flag on tty->port.
1680 tty_port_set_kopened(tty->port, 0); in tty_kclose()
1681 release_tty(tty, tty->index); in tty_kclose()
1687 * tty_release_struct - release a tty struct
1716 * tty_release - vfs callback for close
1749 __tty_fasync(-1, filp, 0); in tty_release()
1751 idx = tty->index; in tty_release()
1752 if (tty->driver->type == TTY_DRIVER_TYPE_PTY && in tty_release()
1753 tty->driver->subtype == PTY_TYPE_MASTER) in tty_release()
1754 o_tty = tty->link; in tty_release()
1761 tty_debug_hangup(tty, "releasing (count=%d)\n", tty->count); in tty_release()
1763 if (tty->ops->close) in tty_release()
1764 tty->ops->close(tty, filp); in tty_release()
1770 * Sanity check: if tty->count is going to zero, there shouldn't be in tty_release()
1771 * any waiters on tty->read_wait or tty->write_wait. We test the in tty_release()
1785 if (tty->count <= 1) { in tty_release()
1786 if (waitqueue_active(&tty->read_wait)) { in tty_release()
1787 wake_up_poll(&tty->read_wait, EPOLLIN); in tty_release()
1790 if (waitqueue_active(&tty->write_wait)) { in tty_release()
1791 wake_up_poll(&tty->write_wait, EPOLLOUT); in tty_release()
1795 if (o_tty && o_tty->count <= 1) { in tty_release()
1796 if (waitqueue_active(&o_tty->read_wait)) { in tty_release()
1797 wake_up_poll(&o_tty->read_wait, EPOLLIN); in tty_release()
1800 if (waitqueue_active(&o_tty->write_wait)) { in tty_release()
1801 wake_up_poll(&o_tty->write_wait, EPOLLOUT); in tty_release()
1820 if (--o_tty->count < 0) { in tty_release()
1821 tty_warn(tty, "bad slave count (%d)\n", o_tty->count); in tty_release()
1822 o_tty->count = 0; in tty_release()
1825 if (--tty->count < 0) { in tty_release()
1826 tty_warn(tty, "bad tty->count (%d)\n", tty->count); in tty_release()
1827 tty->count = 0; in tty_release()
1831 * We've decremented tty->count, so we need to remove this file in tty_release()
1832 * descriptor off the tty->tty_files list; this serves two in tty_release()
1834 * - check_tty_count sees the correct number of file descriptors in tty_release()
1836 * - do_tty_hangup no longer sees this file descriptor as in tty_release()
1848 if (!tty->count) { in tty_release()
1850 session_clear_tty(tty->session); in tty_release()
1852 session_clear_tty(o_tty->session); in tty_release()
1857 final = !tty->count && !(o_tty && o_tty->count); in tty_release()
1862 /* At this point, the tty->count == 0 should ensure a dead tty in tty_release()
1863 cannot be re-opened by a racing opener */ in tty_release()
1875 * tty_open_current_tty - get locked tty of current task
1880 * Performs a re-open of the current task's controlling tty.
1895 return ERR_PTR(-ENXIO); in tty_open_current_tty()
1897 filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */ in tty_open_current_tty()
1911 * tty_lookup_driver - lookup a tty driver for a given device file
1942 filp->f_flags |= O_NONBLOCK; in tty_lookup_driver()
1948 return ERR_PTR(-ENODEV); in tty_lookup_driver()
1953 return ERR_PTR(-ENODEV); in tty_lookup_driver()
1960 * tty_kopen - open a tty device for kernel
1964 * makes sure it's not already opened and performs the first-time
1970 * - concurrent first-time tty initialization
1971 * - concurrent tty driver removal w/ lookup
1972 * - concurrent tty removal from driver table
1978 int index = -1; in tty_kopen()
1995 tty = ERR_PTR(-EBUSY); in tty_kopen()
2000 tty_port_set_kopened(tty->port, 1); in tty_kopen()
2010 * tty_open_by_driver - open a tty device
2015 * performs the first-time tty initialization.
2017 * Returns the locked initialized or re-opened &tty_struct
2020 * - concurrent first-time tty initialization
2021 * - concurrent tty driver removal w/ lookup
2022 * - concurrent tty removal from driver table
2029 int index = -1; in tty_open_by_driver()
2047 if (tty_port_kopened(tty->port)) { in tty_open_by_driver()
2050 tty = ERR_PTR(-EBUSY); in tty_open_by_driver()
2057 if (retval == -EINTR) in tty_open_by_driver()
2058 retval = -ERESTARTSYS; in tty_open_by_driver()
2077 * tty_open - open a tty device
2082 * number of opens done on a tty. We cannot use the inode-count, as
2085 * Open-counting is needed for pty masters, as well as for keeping
2087 * (This is not done solely through tty->count, now. - Ted 1/27/92)
2093 * tty->count should protect the rest.
2094 * ->siglock protects ->signal/->sighand
2104 dev_t device = inode->i_rdev; in tty_open()
2105 unsigned saved_flags = filp->f_flags; in tty_open()
2112 return -ENOMEM; in tty_open()
2121 if (retval != -EAGAIN || signal_pending(current)) in tty_open()
2130 tty_debug_hangup(tty, "opening (count=%d)\n", tty->count); in tty_open()
2132 if (tty->ops->open) in tty_open()
2133 retval = tty->ops->open(tty, filp); in tty_open()
2135 retval = -ENODEV; in tty_open()
2136 filp->f_flags = saved_flags; in tty_open()
2143 if (retval != -ERESTARTSYS) in tty_open()
2154 filp->f_op = &tty_fops; in tty_open()
2157 clear_bit(TTY_HUPPED, &tty->flags); in tty_open()
2159 noctty = (filp->f_flags & O_NOCTTY) || in tty_open()
2162 (tty->driver->type == TTY_DRIVER_TYPE_PTY && in tty_open()
2163 tty->driver->subtype == PTY_TYPE_MASTER); in tty_open()
2173 * tty_poll - check tty status
2181 * may be re-entered freely by other callers.
2196 if (ld->ops->poll) in tty_poll()
2197 ret = ld->ops->poll(tty, filp, wait); in tty_poll()
2211 retval = fasync_helper(fd, filp, on, &tty->fasync); in __tty_fasync()
2219 spin_lock_irqsave(&tty->ctrl_lock, flags); in __tty_fasync()
2220 if (tty->pgrp) { in __tty_fasync()
2221 pid = tty->pgrp; in __tty_fasync()
2228 spin_unlock_irqrestore(&tty->ctrl_lock, flags); in __tty_fasync()
2240 int retval = -ENOTTY; in tty_fasync()
2251 * tiocsti - fake input character
2262 * current->signal->tty check is safe without locks
2270 if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN)) in tiocsti()
2271 return -EPERM; in tiocsti()
2273 return -EFAULT; in tiocsti()
2277 return -EIO; in tiocsti()
2278 tty_buffer_lock_exclusive(tty->port); in tiocsti()
2279 if (ld->ops->receive_buf) in tiocsti()
2280 ld->ops->receive_buf(tty, &ch, &mbz, 1); in tiocsti()
2281 tty_buffer_unlock_exclusive(tty->port); in tiocsti()
2287 * tiocgwinsz - implement window query ioctl
2293 * Locking: tty->winsize_mutex is taken to ensure the winsize data
2301 mutex_lock(&tty->winsize_mutex); in tiocgwinsz()
2302 err = copy_to_user(arg, &tty->winsize, sizeof(*arg)); in tiocgwinsz()
2303 mutex_unlock(&tty->winsize_mutex); in tiocgwinsz()
2305 return err ? -EFAULT: 0; in tiocgwinsz()
2309 * tty_do_resize - resize event
2322 mutex_lock(&tty->winsize_mutex); in tty_do_resize()
2323 if (!memcmp(ws, &tty->winsize, sizeof(*ws))) in tty_do_resize()
2332 tty->winsize = *ws; in tty_do_resize()
2334 mutex_unlock(&tty->winsize_mutex); in tty_do_resize()
2340 * tiocswinsz - implement window size set ioctl
2358 return -EFAULT; in tiocswinsz()
2360 if (tty->ops->resize) in tiocswinsz()
2361 return tty->ops->resize(tty, &tmp_ws); in tiocswinsz()
2367 * tioccons - allow admin to move logical console
2378 return -EPERM; in tioccons()
2379 if (file->f_op->write_iter == redirected_tty_write) { in tioccons()
2389 if (file->f_op->write_iter != tty_write) in tioccons()
2390 return -ENOTTY; in tioccons()
2391 if (!(file->f_mode & FMODE_WRITE)) in tioccons()
2392 return -EBADF; in tioccons()
2393 if (!(file->f_mode & FMODE_CAN_WRITE)) in tioccons()
2394 return -EINVAL; in tioccons()
2398 return -EBUSY; in tioccons()
2406 * tiocsetd - set line discipline
2421 return -EFAULT; in tiocsetd()
2429 * tiocgetd - get line discipline
2446 return -EIO; in tiocgetd()
2447 ret = put_user(ld->ops->num, p); in tiocgetd()
2453 * send_break - performed time break
2469 if (tty->ops->break_ctl == NULL) in send_break()
2472 if (tty->driver->flags & TTY_DRIVER_HARDWARE_BREAK) in send_break()
2473 return tty->ops->break_ctl(tty, duration); in send_break()
2477 return -EINTR; in send_break()
2479 retval = tty->ops->break_ctl(tty, -1); in send_break()
2482 retval = tty->ops->break_ctl(tty, 0); in send_break()
2483 } else if (retval == -EOPNOTSUPP) { in send_break()
2490 retval = -EINTR; in send_break()
2496 * tty_tiocmget - get modem status
2500 * Obtain the modem status bits from the tty driver if the feature
2501 * is supported. Return -ENOTTY if it is not available.
2508 int retval = -ENOTTY; in tty_tiocmget()
2510 if (tty->ops->tiocmget) { in tty_tiocmget()
2511 retval = tty->ops->tiocmget(tty); in tty_tiocmget()
2520 * tty_tiocmset - set modem status
2522 * @cmd: command - clear bits, set bits or set all
2523 * @p: pointer to desired bits
2525 * Set the modem status bits from the tty driver if the feature
2526 * is supported. Return -ENOTTY if it is not available.
2537 if (tty->ops->tiocmset == NULL) in tty_tiocmset()
2538 return -ENOTTY; in tty_tiocmset()
2558 return tty->ops->tiocmset(tty, set, clear); in tty_tiocmset()
2563 int retval = -EINVAL; in tty_tiocgicount()
2566 if (tty->ops->get_icount) in tty_tiocgicount()
2567 retval = tty->ops->get_icount(tty, &icount); in tty_tiocgicount()
2571 return -EFAULT; in tty_tiocgicount()
2575 static int tty_tiocsserial(struct tty_struct *tty, struct serial_struct __user *ss) in tty_tiocsserial() argument
2584 if (copy_from_user(&v, ss, sizeof(*ss))) in tty_tiocsserial()
2585 return -EFAULT; in tty_tiocsserial()
2592 if (!tty->ops->set_serial) in tty_tiocsserial()
2593 return -ENOTTY; in tty_tiocsserial()
2594 return tty->ops->set_serial(tty, &v); in tty_tiocsserial()
2597 static int tty_tiocgserial(struct tty_struct *tty, struct serial_struct __user *ss) in tty_tiocgserial() argument
2603 if (!tty->ops->get_serial) in tty_tiocgserial()
2604 return -ENOTTY; in tty_tiocgserial()
2605 err = tty->ops->get_serial(tty, &v); in tty_tiocgserial()
2606 if (!err && copy_to_user(ss, &v, sizeof(v))) in tty_tiocgserial()
2607 err = -EFAULT; in tty_tiocgserial()
2617 if (tty->driver->type == TTY_DRIVER_TYPE_PTY && in tty_pair_get_tty()
2618 tty->driver->subtype == PTY_TYPE_MASTER) in tty_pair_get_tty()
2619 tty = tty->link; in tty_pair_get_tty()
2635 return -EINVAL; in tty_ioctl()
2654 return -EINTR; in tty_ioctl()
2670 return real_tty != tty ? -EINVAL : tioccons(file); in tty_ioctl()
2672 set_bit(TTY_EXCLUSIVE, &tty->flags); in tty_ioctl()
2675 clear_bit(TTY_EXCLUSIVE, &tty->flags); in tty_ioctl()
2679 int excl = test_bit(TTY_EXCLUSIVE, &tty->flags); in tty_ioctl()
2688 return -EPERM; in tty_ioctl()
2700 if (tty->ops->break_ctl) in tty_ioctl()
2701 return tty->ops->break_ctl(tty, -1); in tty_ioctl()
2704 if (tty->ops->break_ctl) in tty_ioctl()
2705 return tty->ops->break_ctl(tty, 0); in tty_ioctl()
2707 case TCSBRK: /* SVID version: non-zero arg --> no break */ in tty_ioctl()
2708 /* non-zero arg means wait for all output data in tty_ioctl()
2744 if (retval != -ENOIOCTLCMD) in tty_ioctl()
2747 if (tty->ops->ioctl) { in tty_ioctl()
2748 retval = tty->ops->ioctl(tty, cmd, arg); in tty_ioctl()
2749 if (retval != -ENOIOCTLCMD) in tty_ioctl()
2755 retval = -EINVAL; in tty_ioctl()
2756 if (ld->ops->ioctl) { in tty_ioctl()
2757 retval = ld->ops->ioctl(tty, file, cmd, arg); in tty_ioctl()
2758 if (retval == -ENOIOCTLCMD) in tty_ioctl()
2759 retval = -ENOTTY; in tty_ioctl()
2790 struct serial_struct32 __user *ss) in compat_tty_tiocsserial() argument
2800 if (copy_from_user(&v32, ss, sizeof(*ss))) in compat_tty_tiocsserial()
2801 return -EFAULT; in compat_tty_tiocsserial()
2814 if (!tty->ops->set_serial) in compat_tty_tiocsserial()
2815 return -ENOTTY; in compat_tty_tiocsserial()
2816 return tty->ops->set_serial(tty, &v); in compat_tty_tiocsserial()
2820 struct serial_struct32 __user *ss) in compat_tty_tiocgserial() argument
2829 if (!tty->ops->get_serial) in compat_tty_tiocgserial()
2830 return -ENOTTY; in compat_tty_tiocgserial()
2831 err = tty->ops->get_serial(tty, &v); in compat_tty_tiocgserial()
2838 if (copy_to_user(ss, &v32, sizeof(v32))) in compat_tty_tiocgserial()
2839 err = -EFAULT; in compat_tty_tiocgserial()
2848 int retval = -ENOIOCTLCMD; in tty_compat_ioctl()
2930 return -EINVAL; in tty_compat_ioctl()
2938 if (tty->ops->compat_ioctl) { in tty_compat_ioctl()
2939 retval = tty->ops->compat_ioctl(tty, cmd, arg); in tty_compat_ioctl()
2940 if (retval != -ENOIOCTLCMD) in tty_compat_ioctl()
2947 if (ld->ops->compat_ioctl) in tty_compat_ioctl()
2948 retval = ld->ops->compat_ioctl(tty, file, cmd, arg); in tty_compat_ioctl()
2949 if (retval == -ENOIOCTLCMD && ld->ops->ioctl) in tty_compat_ioctl()
2950 retval = ld->ops->ioctl(tty, file, in tty_compat_ioctl()
2960 if (likely(file->f_op->read_iter != tty_read)) in this_tty()
2966 * This implements the "Secure Attention Key" --- the idea is to
2969 * super-paranoid applications --- see the Orange Book for more details.
2977 * Now, if it would be correct ;-/ The current code has a nasty hole -
2982 * deadlock. We punt it up to process context. AKPM - 16Mar2001
2997 spin_lock_irqsave(&tty->ctrl_lock, flags); in __do_SAK()
2998 session = get_pid(tty->session); in __do_SAK()
2999 spin_unlock_irqrestore(&tty->ctrl_lock, flags); in __do_SAK()
3009 task_pid_nr(p), p->comm); in __do_SAK()
3015 if (p->signal->tty == tty) { in __do_SAK()
3017 task_pid_nr(p), p->comm); in __do_SAK()
3022 i = iterate_fd(p->files, 0, this_tty, tty); in __do_SAK()
3025 task_pid_nr(p), p->comm, i - 1); in __do_SAK()
3043 * The tq handling here is a little racy - tty->SAK_work may already be queued.
3044 * Fortunately we don't need to worry, because if ->SAK_work is already queued,
3046 * already has. --akpm
3052 schedule_work(&tty->SAK_work); in do_SAK()
3070 * Locking: none - tty in question is not exposed at this point
3081 kref_init(&tty->kref); in alloc_tty_struct()
3082 tty->magic = TTY_MAGIC; in alloc_tty_struct()
3087 tty->session = NULL; in alloc_tty_struct()
3088 tty->pgrp = NULL; in alloc_tty_struct()
3089 mutex_init(&tty->legacy_mutex); in alloc_tty_struct()
3090 mutex_init(&tty->throttle_mutex); in alloc_tty_struct()
3091 init_rwsem(&tty->termios_rwsem); in alloc_tty_struct()
3092 mutex_init(&tty->winsize_mutex); in alloc_tty_struct()
3093 init_ldsem(&tty->ldisc_sem); in alloc_tty_struct()
3094 init_waitqueue_head(&tty->write_wait); in alloc_tty_struct()
3095 init_waitqueue_head(&tty->read_wait); in alloc_tty_struct()
3096 INIT_WORK(&tty->hangup_work, do_tty_hangup); in alloc_tty_struct()
3097 mutex_init(&tty->atomic_write_lock); in alloc_tty_struct()
3098 spin_lock_init(&tty->ctrl_lock); in alloc_tty_struct()
3099 spin_lock_init(&tty->flow_lock); in alloc_tty_struct()
3100 spin_lock_init(&tty->files_lock); in alloc_tty_struct()
3101 INIT_LIST_HEAD(&tty->tty_files); in alloc_tty_struct()
3102 INIT_WORK(&tty->SAK_work, do_SAK_work); in alloc_tty_struct()
3104 tty->driver = driver; in alloc_tty_struct()
3105 tty->ops = driver->ops; in alloc_tty_struct()
3106 tty->index = idx; in alloc_tty_struct()
3107 tty_line_name(driver, idx, tty->name); in alloc_tty_struct()
3108 tty->dev = tty_get_device(tty); in alloc_tty_struct()
3114 * tty_put_char - write one character to a tty
3127 if (tty->ops->put_char) in tty_put_char()
3128 return tty->ops->put_char(tty, ch); in tty_put_char()
3129 return tty->ops->write(tty, &ch, 1); in tty_put_char()
3141 driver->cdevs[index] = cdev_alloc(); in tty_cdev_add()
3142 if (!driver->cdevs[index]) in tty_cdev_add()
3143 return -ENOMEM; in tty_cdev_add()
3144 driver->cdevs[index]->ops = &tty_fops; in tty_cdev_add()
3145 driver->cdevs[index]->owner = driver->owner; in tty_cdev_add()
3146 err = cdev_add(driver->cdevs[index], dev, count); in tty_cdev_add()
3148 kobject_put(&driver->cdevs[index]->kobj); in tty_cdev_add()
3153 * tty_register_device - register a tty device
3161 * (or ERR_PTR(-EFOO) on error).
3185 * tty_register_device_attr - register a tty device
3195 * (or ERR_PTR(-EFOO) on error).
3210 dev_t devt = MKDEV(driver->major, driver->minor_start) + index; in tty_register_device_attr()
3215 if (index >= driver->num) { in tty_register_device_attr()
3217 driver->name, index); in tty_register_device_attr()
3218 return ERR_PTR(-EINVAL); in tty_register_device_attr()
3221 if (driver->type == TTY_DRIVER_TYPE_PTY) in tty_register_device_attr()
3228 return ERR_PTR(-ENOMEM); in tty_register_device_attr()
3230 dev->devt = devt; in tty_register_device_attr()
3231 dev->class = tty_class; in tty_register_device_attr()
3232 dev->parent = device; in tty_register_device_attr()
3233 dev->release = tty_device_create_release; in tty_register_device_attr()
3235 dev->groups = attr_grp; in tty_register_device_attr()
3244 if (!(driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)) { in tty_register_device_attr()
3249 tp = driver->termios[index]; in tty_register_device_attr()
3251 driver->termios[index] = NULL; in tty_register_device_attr()
3261 kobject_uevent(&dev->kobj, KOBJ_ADD); in tty_register_device_attr()
3275 * tty_unregister_device - unregister a tty device
3288 MKDEV(driver->major, driver->minor_start) + index); in tty_unregister_device()
3289 if (!(driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)) { in tty_unregister_device()
3290 cdev_del(driver->cdevs[index]); in tty_unregister_device()
3291 driver->cdevs[index] = NULL; in tty_unregister_device()
3297 * __tty_alloc_driver -- allocate tty driver
3300 * @flags: some of TTY_DRIVER_* flags, will be set in driver->flags
3313 return ERR_PTR(-EINVAL); in __tty_alloc_driver()
3317 return ERR_PTR(-ENOMEM); in __tty_alloc_driver()
3319 kref_init(&driver->kref); in __tty_alloc_driver()
3320 driver->magic = TTY_DRIVER_MAGIC; in __tty_alloc_driver()
3321 driver->num = lines; in __tty_alloc_driver()
3322 driver->owner = owner; in __tty_alloc_driver()
3323 driver->flags = flags; in __tty_alloc_driver()
3326 driver->ttys = kcalloc(lines, sizeof(*driver->ttys), in __tty_alloc_driver()
3328 driver->termios = kcalloc(lines, sizeof(*driver->termios), in __tty_alloc_driver()
3330 if (!driver->ttys || !driver->termios) { in __tty_alloc_driver()
3331 err = -ENOMEM; in __tty_alloc_driver()
3337 driver->ports = kcalloc(lines, sizeof(*driver->ports), in __tty_alloc_driver()
3339 if (!driver->ports) { in __tty_alloc_driver()
3340 err = -ENOMEM; in __tty_alloc_driver()
3346 driver->cdevs = kcalloc(cdevs, sizeof(*driver->cdevs), GFP_KERNEL); in __tty_alloc_driver()
3347 if (!driver->cdevs) { in __tty_alloc_driver()
3348 err = -ENOMEM; in __tty_alloc_driver()
3354 kfree(driver->ports); in __tty_alloc_driver()
3355 kfree(driver->ttys); in __tty_alloc_driver()
3356 kfree(driver->termios); in __tty_alloc_driver()
3357 kfree(driver->cdevs); in __tty_alloc_driver()
3369 if (driver->flags & TTY_DRIVER_INSTALLED) { in destruct_tty_driver()
3370 for (i = 0; i < driver->num; i++) { in destruct_tty_driver()
3371 tp = driver->termios[i]; in destruct_tty_driver()
3373 driver->termios[i] = NULL; in destruct_tty_driver()
3376 if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) in destruct_tty_driver()
3380 if (driver->flags & TTY_DRIVER_DYNAMIC_ALLOC) in destruct_tty_driver()
3381 cdev_del(driver->cdevs[0]); in destruct_tty_driver()
3383 kfree(driver->cdevs); in destruct_tty_driver()
3384 kfree(driver->ports); in destruct_tty_driver()
3385 kfree(driver->termios); in destruct_tty_driver()
3386 kfree(driver->ttys); in destruct_tty_driver()
3392 kref_put(&driver->kref, destruct_tty_driver); in tty_driver_kref_put()
3399 driver->ops = op; in tty_set_operations()
3419 if (!driver->major) { in tty_register_driver()
3420 error = alloc_chrdev_region(&dev, driver->minor_start, in tty_register_driver()
3421 driver->num, driver->name); in tty_register_driver()
3423 driver->major = MAJOR(dev); in tty_register_driver()
3424 driver->minor_start = MINOR(dev); in tty_register_driver()
3427 dev = MKDEV(driver->major, driver->minor_start); in tty_register_driver()
3428 error = register_chrdev_region(dev, driver->num, driver->name); in tty_register_driver()
3433 if (driver->flags & TTY_DRIVER_DYNAMIC_ALLOC) { in tty_register_driver()
3434 error = tty_cdev_add(driver, dev, 0, driver->num); in tty_register_driver()
3440 list_add(&driver->tty_drivers, &tty_drivers); in tty_register_driver()
3443 if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) { in tty_register_driver()
3444 for (i = 0; i < driver->num; i++) { in tty_register_driver()
3453 driver->flags |= TTY_DRIVER_INSTALLED; in tty_register_driver()
3457 for (i--; i >= 0; i--) in tty_register_driver()
3461 list_del(&driver->tty_drivers); in tty_register_driver()
3465 unregister_chrdev_region(dev, driver->num); in tty_register_driver()
3478 if (driver->refcount) in tty_unregister_driver()
3479 return -EBUSY; in tty_unregister_driver()
3481 unregister_chrdev_region(MKDEV(driver->major, driver->minor_start), in tty_unregister_driver()
3482 driver->num); in tty_unregister_driver()
3484 list_del(&driver->tty_drivers); in tty_unregister_driver()
3493 return MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index; in tty_devnum()
3506 if (dev->devt == MKDEV(TTYAUX_MAJOR, 0) || in tty_devnode()
3507 dev->devt == MKDEV(TTYAUX_MAJOR, 2)) in tty_devnode()
3517 tty_class->devnode = tty_devnode; in tty_class_init()
3536 if (!c->device) in show_cons_active()
3538 if (!c->write) in show_cons_active()
3540 if ((c->flags & CON_ENABLED) == 0) in show_cons_active()
3546 while (i--) { in show_cons_active()
3547 int index = cs[i]->index; in show_cons_active()
3548 struct tty_driver *drv = cs[i]->device(cs[i], &index); in show_cons_active()
3551 if (drv && (cs[i]->index > 0 || drv->major != TTY_MAJOR)) in show_cons_active()
3555 cs[i]->name, cs[i]->index); in show_cons_active()
3577 sysfs_notify(&consdev->kobj, NULL, "active"); in console_sysfs_notify()