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
162 * free_tty_struct - free a disused tty
172 put_device(tty->dev); in free_tty_struct()
173 kvfree(tty->write_buf); in free_tty_struct()
179 return ((struct tty_file_private *)file->private_data)->tty; in file_tty()
188 return -ENOMEM; in tty_alloc_file()
190 file->private_data = priv; in tty_alloc_file()
198 struct tty_file_private *priv = file->private_data; in tty_add_file()
200 priv->tty = tty; in tty_add_file()
201 priv->file = file; in tty_add_file()
203 spin_lock(&tty->files_lock); in tty_add_file()
204 list_add(&priv->list, &tty->tty_files); in tty_add_file()
205 spin_unlock(&tty->files_lock); in tty_add_file()
209 * 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
248 return tty->name; in tty_name()
254 if (!tty || !tty->driver) in tty_driver_name()
256 return tty->driver->name; in tty_driver_name()
279 spin_lock(&tty->files_lock); in check_tty_count()
280 list_for_each(p, &tty->tty_files) { in check_tty_count()
283 spin_unlock(&tty->files_lock); in check_tty_count()
284 if (tty->driver->type == TTY_DRIVER_TYPE_PTY && in check_tty_count()
285 tty->driver->subtype == PTY_TYPE_SLAVE && in check_tty_count()
286 tty->link && tty->link->count) in check_tty_count()
288 if (tty_port_kopened(tty->port)) in check_tty_count()
290 if (tty->count != (count + kopen_count)) { in check_tty_count()
291 tty_warn(tty, "%s: tty->count(%d) != (#fd's(%d) + #kopen's(%d))\n", in check_tty_count()
292 routine, tty->count, count, kopen_count); in check_tty_count()
298 * get_tty_driver - find device of a tty
312 dev_t base = MKDEV(p->major, p->minor_start); in get_tty_driver()
314 if (device < base || device >= base + p->num) in get_tty_driver()
316 *index = device - base; in get_tty_driver()
323 * tty_dev_name_to_number - return dev_t for device name
329 * function returns -%ENODEV.
346 return -EINVAL; in tty_dev_name_to_number()
352 prefix_length = str - name; in tty_dev_name_to_number()
356 if (prefix_length == strlen(p->name) && strncmp(name, in tty_dev_name_to_number()
357 p->name, prefix_length) == 0) { in tty_dev_name_to_number()
358 if (index < p->num) { in tty_dev_name_to_number()
359 *number = MKDEV(p->major, p->minor_start + index); in tty_dev_name_to_number()
365 ret = -ENODEV; in tty_dev_name_to_number()
375 * tty_find_polling_driver - find device of a polled tty
395 len = str - name; in tty_find_polling_driver()
401 if (!len || strncmp(name, p->name, len) != 0) in tty_find_polling_driver()
409 if (tty_line >= 0 && tty_line < p->num && p->ops && in tty_find_polling_driver()
410 p->ops->poll_init && !p->ops->poll_init(p, tty_line, stp)) { in tty_find_polling_driver()
430 return -EIO; in hung_up_tty_write()
433 /* No kernel lock held - none needed ;) */
442 return cmd == TIOCSPGRP ? -ENOTTY : -EIO; in hung_up_tty_ioctl()
448 return cmd == TIOCSPGRP ? -ENOTTY : -EIO; in hung_up_tty_compat_ioctl()
453 return -ENOTTY; in hung_up_tty_fasync()
460 if (tty && tty->ops && tty->ops->show_fdinfo) in tty_show_fdinfo()
461 tty->ops->show_fdinfo(tty, m); in tty_show_fdinfo()
508 * tty_wakeup - request more data
519 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) { in tty_wakeup()
522 if (ld->ops->write_wakeup) in tty_wakeup()
523 ld->ops->write_wakeup(tty); in tty_wakeup()
527 wake_up_interruptible_poll(&tty->write_wait, EPOLLOUT); in tty_wakeup()
532 * tty_release_redirect - Release a redirect on a pty if present
553 * __tty_hangup - actual handler for hangup events
555 * @exit_session: if non-zero, signal all foreground group processes
574 * * ->siglock to protect ->signal/->sighand
592 if (test_bit(TTY_HUPPED, &tty->flags)) { in __tty_hangup()
603 set_bit(TTY_HUPPING, &tty->flags); in __tty_hangup()
611 spin_lock(&tty->files_lock); in __tty_hangup()
613 list_for_each_entry(priv, &tty->tty_files, list) { in __tty_hangup()
614 filp = priv->file; in __tty_hangup()
615 if (filp->f_op->write_iter == redirected_tty_write) in __tty_hangup()
617 if (filp->f_op->write_iter != tty_write) in __tty_hangup()
620 __tty_fasync(-1, filp, 0); /* can't block */ in __tty_hangup()
621 filp->f_op = &hung_up_tty_fops; in __tty_hangup()
623 spin_unlock(&tty->files_lock); in __tty_hangup()
626 /* Account for the p->signal references we killed */ in __tty_hangup()
627 while (refs--) in __tty_hangup()
632 spin_lock_irq(&tty->ctrl.lock); in __tty_hangup()
633 clear_bit(TTY_THROTTLED, &tty->flags); in __tty_hangup()
634 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); in __tty_hangup()
635 put_pid(tty->ctrl.session); in __tty_hangup()
636 put_pid(tty->ctrl.pgrp); in __tty_hangup()
637 tty->ctrl.session = NULL; in __tty_hangup()
638 tty->ctrl.pgrp = NULL; in __tty_hangup()
639 tty->ctrl.pktstatus = 0; in __tty_hangup()
640 spin_unlock_irq(&tty->ctrl.lock); in __tty_hangup()
645 * tty->count and state->count to go out of sync. in __tty_hangup()
649 if (tty->ops->close) in __tty_hangup()
651 tty->ops->close(tty, cons_filp); in __tty_hangup()
652 } else if (tty->ops->hangup) in __tty_hangup()
653 tty->ops->hangup(tty); in __tty_hangup()
656 * we did here. The driver layer expects no calls after ->hangup() in __tty_hangup()
659 set_bit(TTY_HUPPED, &tty->flags); in __tty_hangup()
660 clear_bit(TTY_HUPPING, &tty->flags); in __tty_hangup()
676 * tty_hangup - trigger a hangup event
685 schedule_work(&tty->hangup_work); in tty_hangup()
690 * tty_vhangup - process vhangup
706 * tty_vhangup_self - process vhangup for own ctty
722 * tty_vhangup_session - hangup session leader exit
738 * tty_hung_up_p - was tty hung up
745 return (filp && filp->f_op == &hung_up_tty_fops); in tty_hung_up_p()
751 if (tty->flow.stopped) in __stop_tty()
753 tty->flow.stopped = true; in __stop_tty()
754 if (tty->ops->stop) in __stop_tty()
755 tty->ops->stop(tty); in __stop_tty()
759 * stop_tty - propagate flow control
763 * device and will not re-call the &tty_driver->stop() method.
776 spin_lock_irqsave(&tty->flow.lock, flags); in stop_tty()
778 spin_unlock_irqrestore(&tty->flow.lock, flags); in stop_tty()
784 if (!tty->flow.stopped || tty->flow.tco_stopped) in __start_tty()
786 tty->flow.stopped = false; in __start_tty()
787 if (tty->ops->start) in __start_tty()
788 tty->ops->start(tty); in __start_tty()
793 * start_tty - propagate flow control
797 * stopped and is now being started, the &tty_driver->start() method is invoked
807 spin_lock_irqsave(&tty->flow.lock, flags); in start_tty()
809 spin_unlock_irqrestore(&tty->flow.lock, flags); in start_tty()
818 spin_lock(&tty->files_lock); in tty_update_time()
819 list_for_each_entry(priv, &tty->tty_files, list) { in tty_update_time()
820 struct inode *inode = file_inode(priv->file); in tty_update_time()
821 struct timespec64 *time = mtime ? &inode->i_mtime : &inode->i_atime; in tty_update_time()
825 * lower three bits (i.e every 8 seconds). If so, then we can update in tty_update_time()
829 if ((sec ^ time->tv_sec) & ~7) in tty_update_time()
830 time->tv_sec = sec; in tty_update_time()
832 spin_unlock(&tty->files_lock); in tty_update_time()
836 * Iterate on the ldisc ->read() function until we've gotten all
858 size = ld->ops->read(tty, file, kernel_buf, size, &cookie, offset); in iterate_tty_read()
863 /* Did we have an earlier error (ie -EFAULT)? */ in iterate_tty_read()
869 * -EOVERFLOW means we didn't have enough space in iterate_tty_read()
873 if (retval == -EOVERFLOW) in iterate_tty_read()
880 count -= copied; in iterate_tty_read()
883 * If the user copy failed, we still need to do another ->read() in iterate_tty_read()
890 retval = -EFAULT; in iterate_tty_read()
901 * tty_read - read method for tty device files
914 struct file *file = iocb->ki_filp; in tty_read()
921 return -EIO; in tty_read()
923 return -EIO; in tty_read()
931 ret = -EIO; in tty_read()
932 if (ld->ops->read) in tty_read()
944 mutex_unlock(&tty->atomic_write_lock); in tty_write_unlock()
945 wake_up_interruptible_poll(&tty->write_wait, EPOLLOUT); in tty_write_unlock()
950 if (!mutex_trylock(&tty->atomic_write_lock)) { in tty_write_lock()
952 return -EAGAIN; in tty_write_lock()
953 if (mutex_lock_interruptible(&tty->atomic_write_lock)) in tty_write_lock()
954 return -ERESTARTSYS; in tty_write_lock()
961 * denial-of-service type attacks
969 ret = tty_write_lock(tty, file->f_flags & O_NDELAY); in iterate_tty_write()
975 * simplifies low-level drivers immensely, since they in iterate_tty_write()
979 * big chunk-size.. in iterate_tty_write()
981 * The default chunk-size is 2kB, because the NTTY in iterate_tty_write()
987 if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags)) in iterate_tty_write()
993 if (tty->write_cnt < chunk) { in iterate_tty_write()
1001 ret = -ENOMEM; in iterate_tty_write()
1004 kvfree(tty->write_buf); in iterate_tty_write()
1005 tty->write_cnt = chunk; in iterate_tty_write()
1006 tty->write_buf = buf_chunk; in iterate_tty_write()
1013 ret = -EFAULT; in iterate_tty_write()
1014 if (copy_from_iter(tty->write_buf, size, from) != size) in iterate_tty_write()
1017 ret = ld->ops->write(tty, file, tty->write_buf, size); in iterate_tty_write()
1027 iov_iter_revert(from, size-ret); in iterate_tty_write()
1029 count -= ret; in iterate_tty_write()
1032 ret = -ERESTARTSYS; in iterate_tty_write()
1047 * tty_write_message - write a message to a certain tty, not just the console.
1060 mutex_lock(&tty->atomic_write_lock); in tty_write_message()
1062 if (tty->ops->write && tty->count > 0) in tty_write_message()
1063 tty->ops->write(tty, msg, strlen(msg)); in tty_write_message()
1076 return -EIO; in file_tty_write()
1077 if (!tty || !tty->ops->write || tty_io_error(tty)) in file_tty_write()
1078 return -EIO; in file_tty_write()
1080 if (tty->ops->write_room == NULL) in file_tty_write()
1085 if (!ld->ops->write) in file_tty_write()
1086 ret = -EIO; in file_tty_write()
1094 * tty_write - write method for tty device file
1109 return file_tty_write(iocb->ki_filp, iocb, from); in tty_write()
1136 * tty_send_xchar - send priority character
1146 bool was_stopped = tty->flow.stopped; in tty_send_xchar()
1148 if (tty->ops->send_xchar) { in tty_send_xchar()
1149 down_read(&tty->termios_rwsem); in tty_send_xchar()
1150 tty->ops->send_xchar(tty, ch); in tty_send_xchar()
1151 up_read(&tty->termios_rwsem); in tty_send_xchar()
1156 return -ERESTARTSYS; in tty_send_xchar()
1158 down_read(&tty->termios_rwsem); in tty_send_xchar()
1161 tty->ops->write(tty, &ch, 1); in tty_send_xchar()
1164 up_read(&tty->termios_rwsem); in tty_send_xchar()
1170 * pty_line_name - generate name for a pty
1183 int i = index + driver->name_base; in pty_line_name()
1184 /* ->name is initialized to "ttyp", but "tty" is expected */ in pty_line_name()
1186 driver->subtype == PTY_TYPE_SLAVE ? "tty" : driver->name, in pty_line_name()
1191 * tty_line_name - generate name for a tty
1203 if (driver->flags & TTY_DRIVER_UNNUMBERED_NODE) in tty_line_name()
1204 return sprintf(p, "%s", driver->name); in tty_line_name()
1206 return sprintf(p, "%s%d", driver->name, in tty_line_name()
1207 index + driver->name_base); in tty_line_name()
1211 * tty_driver_lookup_tty() - find an existing tty, if any
1226 if (driver->ops->lookup) { in tty_driver_lookup_tty()
1228 tty = ERR_PTR(-EIO); in tty_driver_lookup_tty()
1230 tty = driver->ops->lookup(driver, file, idx); in tty_driver_lookup_tty()
1232 if (idx >= driver->num) in tty_driver_lookup_tty()
1233 return ERR_PTR(-EINVAL); in tty_driver_lookup_tty()
1234 tty = driver->ttys[idx]; in tty_driver_lookup_tty()
1242 * tty_init_termios - helper for termios setup
1251 int idx = tty->index; in tty_init_termios()
1253 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) in tty_init_termios()
1254 tty->termios = tty->driver->init_termios; in tty_init_termios()
1257 tp = tty->driver->termios[idx]; in tty_init_termios()
1259 tty->termios = *tp; in tty_init_termios()
1260 tty->termios.c_line = tty->driver->init_termios.c_line; in tty_init_termios()
1262 tty->termios = tty->driver->init_termios; in tty_init_termios()
1265 tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios); in tty_init_termios()
1266 tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios); in tty_init_termios()
1271 * tty_standard_install - usual tty->ops->install
1275 * If the @driver overrides @tty->ops->install, it still can call this function
1282 tty->count++; in tty_standard_install()
1283 driver->ttys[tty->index] = tty; in tty_standard_install()
1289 * tty_driver_install_tty() - install a tty entry in the driver
1293 * Install a tty object into the driver tables. The @tty->index field will be
1302 return driver->ops->install ? driver->ops->install(driver, tty) : in tty_driver_install_tty()
1307 * tty_driver_remove_tty() - remove a tty from the driver tables
1311 * Remove a tty object from the driver tables. The tty->index field will be set
1318 if (driver->ops->remove) in tty_driver_remove_tty()
1319 driver->ops->remove(driver, tty); in tty_driver_remove_tty()
1321 driver->ttys[tty->index] = NULL; in tty_driver_remove_tty()
1325 * tty_reopen() - fast re-open of an open tty
1328 * Re-opens on master ptys are not allowed and return -%EIO.
1331 * Return: 0 on success, -errno on error.
1335 struct tty_driver *driver = tty->driver; in tty_reopen()
1339 if (driver->type == TTY_DRIVER_TYPE_PTY && in tty_reopen()
1340 driver->subtype == PTY_TYPE_MASTER) in tty_reopen()
1341 return -EIO; in tty_reopen()
1343 if (!tty->count) in tty_reopen()
1344 return -EAGAIN; in tty_reopen()
1346 if (test_bit(TTY_EXCLUSIVE, &tty->flags) && !capable(CAP_SYS_ADMIN)) in tty_reopen()
1347 return -EBUSY; in tty_reopen()
1357 if (!tty->ldisc) in tty_reopen()
1358 retval = tty_ldisc_reinit(tty, tty->termios.c_line); in tty_reopen()
1363 tty->count++; in tty_reopen()
1369 * tty_init_dev - initialise a tty device
1404 if (!try_module_get(driver->owner)) in tty_init_dev()
1405 return ERR_PTR(-ENODEV); in tty_init_dev()
1409 retval = -ENOMEM; in tty_init_dev()
1418 if (!tty->port) in tty_init_dev()
1419 tty->port = driver->ports[idx]; in tty_init_dev()
1421 if (WARN_RATELIMIT(!tty->port, in tty_init_dev()
1422 "%s: %s driver does not set tty->port. This would crash the kernel. Fix the driver!\n", in tty_init_dev()
1423 __func__, tty->driver->name)) { in tty_init_dev()
1424 retval = -EINVAL; in tty_init_dev()
1431 tty->port->itty = tty; in tty_init_dev()
1438 retval = tty_ldisc_setup(tty, tty->link); in tty_init_dev()
1449 module_put(driver->owner); in tty_init_dev()
1464 * tty_save_termios() - save tty termios data in driver table
1472 int idx = tty->index; in tty_save_termios()
1475 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) in tty_save_termios()
1479 tp = tty->driver->termios[idx]; in tty_save_termios()
1484 tty->driver->termios[idx] = tp; in tty_save_termios()
1486 *tp = tty->termios; in tty_save_termios()
1491 * tty_flush_works - flush all works of a tty/pty pair
1498 flush_work(&tty->SAK_work); in tty_flush_works()
1499 flush_work(&tty->hangup_work); in tty_flush_works()
1500 if (tty->link) { in tty_flush_works()
1501 flush_work(&tty->link->SAK_work); in tty_flush_works()
1502 flush_work(&tty->link->hangup_work); in tty_flush_works()
1507 * release_one_tty - release tty structure memory
1525 struct tty_driver *driver = tty->driver; in release_one_tty()
1526 struct module *owner = driver->owner; in release_one_tty()
1528 if (tty->ops->cleanup) in release_one_tty()
1529 tty->ops->cleanup(tty); in release_one_tty()
1534 spin_lock(&tty->files_lock); in release_one_tty()
1535 list_del_init(&tty->tty_files); in release_one_tty()
1536 spin_unlock(&tty->files_lock); in release_one_tty()
1538 put_pid(tty->ctrl.pgrp); in release_one_tty()
1539 put_pid(tty->ctrl.session); in release_one_tty()
1550 INIT_WORK(&tty->hangup_work, release_one_tty); in queue_release_one_tty()
1551 schedule_work(&tty->hangup_work); in queue_release_one_tty()
1555 * tty_kref_put - release a tty kref
1564 kref_put(&tty->kref, queue_release_one_tty); in tty_kref_put()
1569 * release_tty - release tty structure memory
1584 WARN_ON(tty->index != idx); in release_tty()
1586 if (tty->ops->shutdown) in release_tty()
1587 tty->ops->shutdown(tty); in release_tty()
1589 tty_driver_remove_tty(tty->driver, tty); in release_tty()
1590 if (tty->port) in release_tty()
1591 tty->port->itty = NULL; in release_tty()
1592 if (tty->link) in release_tty()
1593 tty->link->port->itty = NULL; in release_tty()
1594 if (tty->port) in release_tty()
1595 tty_buffer_cancel_work(tty->port); in release_tty()
1596 if (tty->link) in release_tty()
1597 tty_buffer_cancel_work(tty->link->port); in release_tty()
1599 tty_kref_put(tty->link); in release_tty()
1604 * tty_release_checks - check a tty before real release
1609 * no-op unless %TTY_PARANOIA_CHECK is defined.
1614 if (idx < 0 || idx >= tty->driver->num) { in tty_release_checks()
1616 return -1; in tty_release_checks()
1620 if (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM) in tty_release_checks()
1623 if (tty != tty->driver->ttys[idx]) { in tty_release_checks()
1625 idx, tty->driver->ttys[idx]); in tty_release_checks()
1626 return -1; in tty_release_checks()
1628 if (tty->driver->other) { in tty_release_checks()
1629 struct tty_struct *o_tty = tty->link; in tty_release_checks()
1631 if (o_tty != tty->driver->other->ttys[idx]) { in tty_release_checks()
1633 idx, tty->driver->other->ttys[idx]); in tty_release_checks()
1634 return -1; in tty_release_checks()
1636 if (o_tty->link != tty) { in tty_release_checks()
1637 tty_debug(tty, "bad link = %p\n", o_tty->link); in tty_release_checks()
1638 return -1; in tty_release_checks()
1646 * tty_kclose - closes tty opened by tty_kopen
1651 * @tty->port.
1669 tty_port_set_kopened(tty->port, 0); in tty_kclose()
1670 release_tty(tty, tty->index); in tty_kclose()
1676 * tty_release_struct - release a tty struct
1705 * tty_release - vfs callback for close
1737 __tty_fasync(-1, filp, 0); in tty_release()
1739 idx = tty->index; in tty_release()
1740 if (tty->driver->type == TTY_DRIVER_TYPE_PTY && in tty_release()
1741 tty->driver->subtype == PTY_TYPE_MASTER) in tty_release()
1742 o_tty = tty->link; in tty_release()
1749 tty_debug_hangup(tty, "releasing (count=%d)\n", tty->count); in tty_release()
1751 if (tty->ops->close) in tty_release()
1752 tty->ops->close(tty, filp); in tty_release()
1758 * Sanity check: if tty->count is going to zero, there shouldn't be in tty_release()
1759 * any waiters on tty->read_wait or tty->write_wait. We test the in tty_release()
1773 if (tty->count <= 1) { in tty_release()
1774 if (waitqueue_active(&tty->read_wait)) { in tty_release()
1775 wake_up_poll(&tty->read_wait, EPOLLIN); in tty_release()
1778 if (waitqueue_active(&tty->write_wait)) { in tty_release()
1779 wake_up_poll(&tty->write_wait, EPOLLOUT); in tty_release()
1783 if (o_tty && o_tty->count <= 1) { in tty_release()
1784 if (waitqueue_active(&o_tty->read_wait)) { in tty_release()
1785 wake_up_poll(&o_tty->read_wait, EPOLLIN); in tty_release()
1788 if (waitqueue_active(&o_tty->write_wait)) { in tty_release()
1789 wake_up_poll(&o_tty->write_wait, EPOLLOUT); in tty_release()
1808 if (--o_tty->count < 0) { in tty_release()
1809 tty_warn(tty, "bad slave count (%d)\n", o_tty->count); in tty_release()
1810 o_tty->count = 0; in tty_release()
1813 if (--tty->count < 0) { in tty_release()
1814 tty_warn(tty, "bad tty->count (%d)\n", tty->count); in tty_release()
1815 tty->count = 0; in tty_release()
1819 * We've decremented tty->count, so we need to remove this file in tty_release()
1820 * descriptor off the tty->tty_files list; this serves two in tty_release()
1822 * - check_tty_count sees the correct number of file descriptors in tty_release()
1824 * - do_tty_hangup no longer sees this file descriptor as in tty_release()
1836 if (!tty->count) { in tty_release()
1838 session_clear_tty(tty->ctrl.session); in tty_release()
1840 session_clear_tty(o_tty->ctrl.session); in tty_release()
1845 final = !tty->count && !(o_tty && o_tty->count); in tty_release()
1850 /* At this point, the tty->count == 0 should ensure a dead tty in tty_release()
1851 * cannot be re-opened by a racing opener. in tty_release()
1864 * tty_open_current_tty - get locked tty of current task
1869 * Performs a re-open of the current task's controlling tty.
1884 return ERR_PTR(-ENXIO); in tty_open_current_tty()
1886 filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */ in tty_open_current_tty()
1900 * tty_lookup_driver - lookup a tty driver for a given device file
1934 filp->f_flags |= O_NONBLOCK; in tty_lookup_driver()
1940 return ERR_PTR(-ENODEV); in tty_lookup_driver()
1945 return ERR_PTR(-ENODEV); in tty_lookup_driver()
1955 int index = -1; in tty_kopen()
1972 tty = ERR_PTR(-EBUSY); in tty_kopen()
1977 tty_port_set_kopened(tty->port, 1); in tty_kopen()
1986 * tty_kopen_exclusive - open a tty device for kernel
1990 * it's not already opened and performs the first-time tty initialization.
1993 * * concurrent first-time tty initialization
2006 * tty_kopen_shared - open a tty device for shared in-kernel use
2009 * Opens an already existing tty for in-kernel use. Compared to
2021 * tty_open_by_driver - open a tty device
2026 * first-time tty initialization.
2030 * * concurrent first-time tty initialization
2034 * Return: the locked initialized or re-opened &tty_struct
2041 int index = -1; in tty_open_by_driver()
2059 if (tty_port_kopened(tty->port)) { in tty_open_by_driver()
2062 tty = ERR_PTR(-EBUSY); in tty_open_by_driver()
2069 if (retval == -EINTR) in tty_open_by_driver()
2070 retval = -ERESTARTSYS; in tty_open_by_driver()
2089 * tty_open - open a tty device
2094 * of opens done on a tty. We cannot use the inode-count, as different inodes
2097 * Open-counting is needed for pty masters, as well as for keeping track of
2099 * (This is not done solely through tty->count, now. - Ted 1/27/92)
2106 * * @tty->count should protect the rest.
2107 * * ->siglock protects ->signal/->sighand
2115 dev_t device = inode->i_rdev; in tty_open()
2116 unsigned saved_flags = filp->f_flags; in tty_open()
2123 return -ENOMEM; in tty_open()
2132 if (retval != -EAGAIN || signal_pending(current)) in tty_open()
2141 tty_debug_hangup(tty, "opening (count=%d)\n", tty->count); in tty_open()
2143 if (tty->ops->open) in tty_open()
2144 retval = tty->ops->open(tty, filp); in tty_open()
2146 retval = -ENODEV; in tty_open()
2147 filp->f_flags = saved_flags; in tty_open()
2154 if (retval != -ERESTARTSYS) in tty_open()
2165 filp->f_op = &tty_fops; in tty_open()
2168 clear_bit(TTY_HUPPED, &tty->flags); in tty_open()
2170 noctty = (filp->f_flags & O_NOCTTY) || in tty_open()
2173 (tty->driver->type == TTY_DRIVER_TYPE_PTY && in tty_open()
2174 tty->driver->subtype == PTY_TYPE_MASTER); in tty_open()
2183 * tty_poll - check tty status
2191 * re-entered freely by other callers.
2205 if (ld->ops->poll) in tty_poll()
2206 ret = ld->ops->poll(tty, filp, wait); in tty_poll()
2220 retval = fasync_helper(fd, filp, on, &tty->fasync); in __tty_fasync()
2228 spin_lock_irqsave(&tty->ctrl.lock, flags); in __tty_fasync()
2229 if (tty->ctrl.pgrp) { in __tty_fasync()
2230 pid = tty->ctrl.pgrp; in __tty_fasync()
2237 spin_unlock_irqrestore(&tty->ctrl.lock, flags); in __tty_fasync()
2249 int retval = -ENOTTY; in tty_fasync()
2261 * tiocsti - fake input character
2271 * * current->signal->tty check is safe without locks
2279 return -EIO; in tiocsti()
2281 if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN)) in tiocsti()
2282 return -EPERM; in tiocsti()
2284 return -EFAULT; in tiocsti()
2288 return -EIO; in tiocsti()
2289 tty_buffer_lock_exclusive(tty->port); in tiocsti()
2290 if (ld->ops->receive_buf) in tiocsti()
2291 ld->ops->receive_buf(tty, &ch, &mbz, 1); in tiocsti()
2292 tty_buffer_unlock_exclusive(tty->port); in tiocsti()
2298 * tiocgwinsz - implement window query ioctl
2304 * Locking: @tty->winsize_mutex is taken to ensure the winsize data is
2311 mutex_lock(&tty->winsize_mutex); in tiocgwinsz()
2312 err = copy_to_user(arg, &tty->winsize, sizeof(*arg)); in tiocgwinsz()
2313 mutex_unlock(&tty->winsize_mutex); in tiocgwinsz()
2315 return err ? -EFAULT : 0; in tiocgwinsz()
2319 * tty_do_resize - resize event
2331 mutex_lock(&tty->winsize_mutex); in tty_do_resize()
2332 if (!memcmp(ws, &tty->winsize, sizeof(*ws))) in tty_do_resize()
2341 tty->winsize = *ws; in tty_do_resize()
2343 mutex_unlock(&tty->winsize_mutex); in tty_do_resize()
2349 * tiocswinsz - implement window size set ioctl
2367 return -EFAULT; in tiocswinsz()
2369 if (tty->ops->resize) in tiocswinsz()
2370 return tty->ops->resize(tty, &tmp_ws); in tiocswinsz()
2376 * tioccons - allow admin to move logical console
2386 return -EPERM; in tioccons()
2387 if (file->f_op->write_iter == redirected_tty_write) { in tioccons()
2398 if (file->f_op->write_iter != tty_write) in tioccons()
2399 return -ENOTTY; in tioccons()
2400 if (!(file->f_mode & FMODE_WRITE)) in tioccons()
2401 return -EBADF; in tioccons()
2402 if (!(file->f_mode & FMODE_CAN_WRITE)) in tioccons()
2403 return -EINVAL; in tioccons()
2407 return -EBUSY; in tioccons()
2415 * tiocsetd - set line discipline
2429 return -EFAULT; in tiocsetd()
2437 * tiocgetd - get line discipline
2453 return -EIO; in tiocgetd()
2454 ret = put_user(ld->ops->num, p); in tiocgetd()
2460 * send_break - performed time break
2468 * @tty->atomic_write_lock serializes
2474 if (tty->ops->break_ctl == NULL) in send_break()
2477 if (tty->driver->flags & TTY_DRIVER_HARDWARE_BREAK) in send_break()
2478 return tty->ops->break_ctl(tty, duration); in send_break()
2482 return -EINTR; in send_break()
2484 retval = tty->ops->break_ctl(tty, -1); in send_break()
2487 retval = tty->ops->break_ctl(tty, 0); in send_break()
2488 } else if (retval == -EOPNOTSUPP) { in send_break()
2495 retval = -EINTR; in send_break()
2501 * tty_tiocmget - get modem status
2505 * Obtain the modem status bits from the tty driver if the feature is
2506 * supported. Return -%ENOTTY if it is not available.
2512 int retval = -ENOTTY; in tty_tiocmget()
2514 if (tty->ops->tiocmget) { in tty_tiocmget()
2515 retval = tty->ops->tiocmget(tty); in tty_tiocmget()
2524 * tty_tiocmset - set modem status
2526 * @cmd: command - clear bits, set bits or set all
2527 * @p: pointer to desired bits
2529 * Set the modem status bits from the tty driver if the feature
2530 * is supported. Return -%ENOTTY if it is not available.
2540 if (tty->ops->tiocmset == NULL) in tty_tiocmset()
2541 return -ENOTTY; in tty_tiocmset()
2561 return tty->ops->tiocmset(tty, set, clear); in tty_tiocmset()
2565 * tty_get_icount - get tty statistics
2578 if (tty->ops->get_icount) in tty_get_icount()
2579 return tty->ops->get_icount(tty, icount); in tty_get_icount()
2581 return -ENOTTY; in tty_get_icount()
2595 return -EFAULT; in tty_tiocgicount()
2599 static int tty_set_serial(struct tty_struct *tty, struct serial_struct *ss) in tty_set_serial() argument
2604 flags = ss->flags & ASYNC_DEPRECATED; in tty_set_serial()
2610 if (!tty->ops->set_serial) in tty_set_serial()
2611 return -ENOTTY; in tty_set_serial()
2613 return tty->ops->set_serial(tty, ss); in tty_set_serial()
2616 static int tty_tiocsserial(struct tty_struct *tty, struct serial_struct __user *ss) in tty_tiocsserial() argument
2620 if (copy_from_user(&v, ss, sizeof(*ss))) in tty_tiocsserial()
2621 return -EFAULT; in tty_tiocsserial()
2626 static int tty_tiocgserial(struct tty_struct *tty, struct serial_struct __user *ss) in tty_tiocgserial() argument
2632 if (!tty->ops->get_serial) in tty_tiocgserial()
2633 return -ENOTTY; in tty_tiocgserial()
2634 err = tty->ops->get_serial(tty, &v); in tty_tiocgserial()
2635 if (!err && copy_to_user(ss, &v, sizeof(v))) in tty_tiocgserial()
2636 err = -EFAULT; in tty_tiocgserial()
2646 if (tty->driver->type == TTY_DRIVER_TYPE_PTY && in tty_pair_get_tty()
2647 tty->driver->subtype == PTY_TYPE_MASTER) in tty_pair_get_tty()
2648 tty = tty->link; in tty_pair_get_tty()
2664 return -EINVAL; in tty_ioctl()
2683 return -EINTR; in tty_ioctl()
2699 return real_tty != tty ? -EINVAL : tioccons(file); in tty_ioctl()
2701 set_bit(TTY_EXCLUSIVE, &tty->flags); in tty_ioctl()
2704 clear_bit(TTY_EXCLUSIVE, &tty->flags); in tty_ioctl()
2708 int excl = test_bit(TTY_EXCLUSIVE, &tty->flags); in tty_ioctl()
2718 return -EPERM; in tty_ioctl()
2731 if (tty->ops->break_ctl) in tty_ioctl()
2732 return tty->ops->break_ctl(tty, -1); in tty_ioctl()
2735 if (tty->ops->break_ctl) in tty_ioctl()
2736 return tty->ops->break_ctl(tty, 0); in tty_ioctl()
2738 case TCSBRK: /* SVID version: non-zero arg --> no break */ in tty_ioctl()
2739 /* non-zero arg means wait for all output data in tty_ioctl()
2775 if (retval != -ENOIOCTLCMD) in tty_ioctl()
2778 if (tty->ops->ioctl) { in tty_ioctl()
2779 retval = tty->ops->ioctl(tty, cmd, arg); in tty_ioctl()
2780 if (retval != -ENOIOCTLCMD) in tty_ioctl()
2786 retval = -EINVAL; in tty_ioctl()
2787 if (ld->ops->ioctl) { in tty_ioctl()
2788 retval = ld->ops->ioctl(tty, cmd, arg); in tty_ioctl()
2789 if (retval == -ENOIOCTLCMD) in tty_ioctl()
2790 retval = -ENOTTY; in tty_ioctl()
2821 struct serial_struct32 __user *ss) in compat_tty_tiocsserial() argument
2826 if (copy_from_user(&v32, ss, sizeof(*ss))) in compat_tty_tiocsserial()
2827 return -EFAULT; in compat_tty_tiocsserial()
2839 struct serial_struct32 __user *ss) in compat_tty_tiocgserial() argument
2848 if (!tty->ops->get_serial) in compat_tty_tiocgserial()
2849 return -ENOTTY; in compat_tty_tiocgserial()
2850 err = tty->ops->get_serial(tty, &v); in compat_tty_tiocgserial()
2857 if (copy_to_user(ss, &v32, sizeof(v32))) in compat_tty_tiocgserial()
2858 err = -EFAULT; in compat_tty_tiocgserial()
2867 int retval = -ENOIOCTLCMD; in tty_compat_ioctl()
2949 return -EINVAL; in tty_compat_ioctl()
2957 if (tty->ops->compat_ioctl) { in tty_compat_ioctl()
2958 retval = tty->ops->compat_ioctl(tty, cmd, arg); in tty_compat_ioctl()
2959 if (retval != -ENOIOCTLCMD) in tty_compat_ioctl()
2966 if (ld->ops->compat_ioctl) in tty_compat_ioctl()
2967 retval = ld->ops->compat_ioctl(tty, cmd, arg); in tty_compat_ioctl()
2968 if (retval == -ENOIOCTLCMD && ld->ops->ioctl) in tty_compat_ioctl()
2969 retval = ld->ops->ioctl(tty, (unsigned long)compat_ptr(cmd), in tty_compat_ioctl()
2979 if (likely(file->f_op->read_iter != tty_read)) in this_tty()
2985 * This implements the "Secure Attention Key" --- the idea is to
2988 * super-paranoid applications --- see the Orange Book for more details.
2996 * Now, if it would be correct ;-/ The current code has a nasty hole -
3001 * deadlock. We punt it up to process context. AKPM - 16Mar2001
3010 spin_lock_irqsave(&tty->ctrl.lock, flags); in __do_SAK()
3011 session = get_pid(tty->ctrl.session); in __do_SAK()
3012 spin_unlock_irqrestore(&tty->ctrl.lock, flags); in __do_SAK()
3022 task_pid_nr(p), p->comm); in __do_SAK()
3028 if (p->signal->tty == tty) { in __do_SAK()
3030 task_pid_nr(p), p->comm); in __do_SAK()
3036 i = iterate_fd(p->files, 0, this_tty, tty); in __do_SAK()
3039 task_pid_nr(p), p->comm, i - 1); in __do_SAK()
3057 * The tq handling here is a little racy - tty->SAK_work may already be queued.
3058 * Fortunately we don't need to worry, because if ->SAK_work is already queued,
3060 * already has. --akpm
3066 schedule_work(&tty->SAK_work); in do_SAK()
3080 * alloc_tty_struct - allocate a new tty
3086 * Locking: none - @tty in question is not exposed at this point
3096 kref_init(&tty->kref); in alloc_tty_struct()
3101 tty->ctrl.session = NULL; in alloc_tty_struct()
3102 tty->ctrl.pgrp = NULL; in alloc_tty_struct()
3103 mutex_init(&tty->legacy_mutex); in alloc_tty_struct()
3104 mutex_init(&tty->throttle_mutex); in alloc_tty_struct()
3105 init_rwsem(&tty->termios_rwsem); in alloc_tty_struct()
3106 mutex_init(&tty->winsize_mutex); in alloc_tty_struct()
3107 init_ldsem(&tty->ldisc_sem); in alloc_tty_struct()
3108 init_waitqueue_head(&tty->write_wait); in alloc_tty_struct()
3109 init_waitqueue_head(&tty->read_wait); in alloc_tty_struct()
3110 INIT_WORK(&tty->hangup_work, do_tty_hangup); in alloc_tty_struct()
3111 mutex_init(&tty->atomic_write_lock); in alloc_tty_struct()
3112 spin_lock_init(&tty->ctrl.lock); in alloc_tty_struct()
3113 spin_lock_init(&tty->flow.lock); in alloc_tty_struct()
3114 spin_lock_init(&tty->files_lock); in alloc_tty_struct()
3115 INIT_LIST_HEAD(&tty->tty_files); in alloc_tty_struct()
3116 INIT_WORK(&tty->SAK_work, do_SAK_work); in alloc_tty_struct()
3118 tty->driver = driver; in alloc_tty_struct()
3119 tty->ops = driver->ops; in alloc_tty_struct()
3120 tty->index = idx; in alloc_tty_struct()
3121 tty_line_name(driver, idx, tty->name); in alloc_tty_struct()
3122 tty->dev = tty_get_device(tty); in alloc_tty_struct()
3128 * tty_put_char - write one character to a tty
3132 * Write one byte to the @tty using the provided @tty->ops->put_char() method
3142 if (tty->ops->put_char) in tty_put_char()
3143 return tty->ops->put_char(tty, ch); in tty_put_char()
3144 return tty->ops->write(tty, &ch, 1); in tty_put_char()
3154 driver->cdevs[index] = cdev_alloc(); in tty_cdev_add()
3155 if (!driver->cdevs[index]) in tty_cdev_add()
3156 return -ENOMEM; in tty_cdev_add()
3157 driver->cdevs[index]->ops = &tty_fops; in tty_cdev_add()
3158 driver->cdevs[index]->owner = driver->owner; in tty_cdev_add()
3159 err = cdev_add(driver->cdevs[index], dev, count); in tty_cdev_add()
3161 kobject_put(&driver->cdevs[index]->kobj); in tty_cdev_add()
3166 * tty_register_device - register a tty device
3181 * ERR_PTR(-EFOO) on error).
3197 * tty_register_device_attr - register a tty device
3213 * ERR_PTR(-EFOO) on error).
3221 dev_t devt = MKDEV(driver->major, driver->minor_start) + index; in tty_register_device_attr()
3226 if (index >= driver->num) { in tty_register_device_attr()
3228 driver->name, index); in tty_register_device_attr()
3229 return ERR_PTR(-EINVAL); in tty_register_device_attr()
3232 if (driver->type == TTY_DRIVER_TYPE_PTY) in tty_register_device_attr()
3239 return ERR_PTR(-ENOMEM); in tty_register_device_attr()
3241 dev->devt = devt; in tty_register_device_attr()
3242 dev->class = &tty_class; in tty_register_device_attr()
3243 dev->parent = device; in tty_register_device_attr()
3244 dev->release = tty_device_create_release; in tty_register_device_attr()
3246 dev->groups = attr_grp; in tty_register_device_attr()
3255 if (!(driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)) { in tty_register_device_attr()
3260 tp = driver->termios[index]; in tty_register_device_attr()
3262 driver->termios[index] = NULL; in tty_register_device_attr()
3272 kobject_uevent(&dev->kobj, KOBJ_ADD); in tty_register_device_attr()
3286 * tty_unregister_device - unregister a tty device
3297 device_destroy(&tty_class, MKDEV(driver->major, driver->minor_start) + index); in tty_unregister_device()
3298 if (!(driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)) { in tty_unregister_device()
3299 cdev_del(driver->cdevs[index]); in tty_unregister_device()
3300 driver->cdevs[index] = NULL; in tty_unregister_device()
3306 * __tty_alloc_driver -- allocate tty driver
3309 * @flags: some of %TTY_DRIVER_ flags, will be set in driver->flags
3322 return ERR_PTR(-EINVAL); in __tty_alloc_driver()
3326 return ERR_PTR(-ENOMEM); in __tty_alloc_driver()
3328 kref_init(&driver->kref); in __tty_alloc_driver()
3329 driver->num = lines; in __tty_alloc_driver()
3330 driver->owner = owner; in __tty_alloc_driver()
3331 driver->flags = flags; in __tty_alloc_driver()
3334 driver->ttys = kcalloc(lines, sizeof(*driver->ttys), in __tty_alloc_driver()
3336 driver->termios = kcalloc(lines, sizeof(*driver->termios), in __tty_alloc_driver()
3338 if (!driver->ttys || !driver->termios) { in __tty_alloc_driver()
3339 err = -ENOMEM; in __tty_alloc_driver()
3345 driver->ports = kcalloc(lines, sizeof(*driver->ports), in __tty_alloc_driver()
3347 if (!driver->ports) { in __tty_alloc_driver()
3348 err = -ENOMEM; in __tty_alloc_driver()
3354 driver->cdevs = kcalloc(cdevs, sizeof(*driver->cdevs), GFP_KERNEL); in __tty_alloc_driver()
3355 if (!driver->cdevs) { in __tty_alloc_driver()
3356 err = -ENOMEM; in __tty_alloc_driver()
3362 kfree(driver->ports); in __tty_alloc_driver()
3363 kfree(driver->ttys); in __tty_alloc_driver()
3364 kfree(driver->termios); in __tty_alloc_driver()
3365 kfree(driver->cdevs); in __tty_alloc_driver()
3377 if (driver->flags & TTY_DRIVER_INSTALLED) { in destruct_tty_driver()
3378 for (i = 0; i < driver->num; i++) { in destruct_tty_driver()
3379 tp = driver->termios[i]; in destruct_tty_driver()
3381 driver->termios[i] = NULL; in destruct_tty_driver()
3384 if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) in destruct_tty_driver()
3388 if (driver->flags & TTY_DRIVER_DYNAMIC_ALLOC) in destruct_tty_driver()
3389 cdev_del(driver->cdevs[0]); in destruct_tty_driver()
3391 kfree(driver->cdevs); in destruct_tty_driver()
3392 kfree(driver->ports); in destruct_tty_driver()
3393 kfree(driver->termios); in destruct_tty_driver()
3394 kfree(driver->ttys); in destruct_tty_driver()
3399 * tty_driver_kref_put -- drop a reference to a tty driver
3406 kref_put(&driver->kref, destruct_tty_driver); in tty_driver_kref_put()
3411 * tty_register_driver -- register a tty driver
3423 if (!driver->major) { in tty_register_driver()
3424 error = alloc_chrdev_region(&dev, driver->minor_start, in tty_register_driver()
3425 driver->num, driver->name); in tty_register_driver()
3427 driver->major = MAJOR(dev); in tty_register_driver()
3428 driver->minor_start = MINOR(dev); in tty_register_driver()
3431 dev = MKDEV(driver->major, driver->minor_start); in tty_register_driver()
3432 error = register_chrdev_region(dev, driver->num, driver->name); in tty_register_driver()
3437 if (driver->flags & TTY_DRIVER_DYNAMIC_ALLOC) { in tty_register_driver()
3438 error = tty_cdev_add(driver, dev, 0, driver->num); in tty_register_driver()
3444 list_add(&driver->tty_drivers, &tty_drivers); in tty_register_driver()
3447 if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) { in tty_register_driver()
3448 for (i = 0; i < driver->num; i++) { in tty_register_driver()
3457 driver->flags |= TTY_DRIVER_INSTALLED; in tty_register_driver()
3461 for (i--; i >= 0; i--) in tty_register_driver()
3465 list_del(&driver->tty_drivers); in tty_register_driver()
3469 unregister_chrdev_region(dev, driver->num); in tty_register_driver()
3476 * tty_unregister_driver -- unregister a tty driver
3483 unregister_chrdev_region(MKDEV(driver->major, driver->minor_start), in tty_unregister_driver()
3484 driver->num); in tty_unregister_driver()
3486 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()
3539 * race-free reading of @flags. in show_cons_active()
3544 if (!c->device) in show_cons_active()
3546 if (!c->write) in show_cons_active()
3548 if ((c->flags & CON_ENABLED) == 0) in show_cons_active()
3561 while (i--) { in show_cons_active()
3562 int index = cs[i]->index; in show_cons_active()
3563 struct tty_driver *drv = cs[i]->device(cs[i], &index); in show_cons_active()
3566 if (drv && (cs[i]->index > 0 || drv->major != TTY_MAJOR)) in show_cons_active()
3570 cs[i]->name, cs[i]->index); in show_cons_active()
3594 sysfs_notify(&consdev->kobj, NULL, "active"); in console_sysfs_notify()