blob: 2640635ee177d1ad027ee7bc3443bfb2880ae17c [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Arnd Bergmannb07471f2010-08-06 21:40:30 +02002#include <linux/tty.h>
3#include <linux/module.h>
4#include <linux/kallsyms.h>
5#include <linux/semaphore.h>
6#include <linux/sched.h>
7
Alan Cox89c8d912012-08-08 16:30:13 +01008/* Legacy tty mutex glue */
9
Arnd Bergmannb07471f2010-08-06 21:40:30 +020010/*
11 * Getting the big tty mutex.
12 */
Alan Cox89c8d912012-08-08 16:30:13 +010013
Peter Hurleyc2bb5242016-01-09 21:13:51 -080014void tty_lock(struct tty_struct *tty)
Arnd Bergmannb07471f2010-08-06 21:40:30 +020015{
Peter Hurley6d029c62015-11-08 13:01:20 -050016 if (WARN(tty->magic != TTY_MAGIC, "L Bad %p\n", tty))
Alan Cox89c8d912012-08-08 16:30:13 +010017 return;
Alan Cox89c8d912012-08-08 16:30:13 +010018 tty_kref_get(tty);
Peter Hurley2febdb62014-11-05 12:13:02 -050019 mutex_lock(&tty->legacy_mutex);
Arnd Bergmannb07471f2010-08-06 21:40:30 +020020}
21EXPORT_SYMBOL(tty_lock);
22
Peter Hurley0bfd4642016-01-09 21:13:44 -080023int tty_lock_interruptible(struct tty_struct *tty)
24{
Peter Hurleye9036d02016-02-05 10:49:36 -080025 int ret;
26
Peter Hurley0bfd4642016-01-09 21:13:44 -080027 if (WARN(tty->magic != TTY_MAGIC, "L Bad %p\n", tty))
28 return -EIO;
29 tty_kref_get(tty);
Peter Hurleye9036d02016-02-05 10:49:36 -080030 ret = mutex_lock_interruptible(&tty->legacy_mutex);
31 if (ret)
32 tty_kref_put(tty);
33 return ret;
Peter Hurley0bfd4642016-01-09 21:13:44 -080034}
35
Peter Hurleyc2bb5242016-01-09 21:13:51 -080036void tty_unlock(struct tty_struct *tty)
Arnd Bergmannb07471f2010-08-06 21:40:30 +020037{
Peter Hurley6d029c62015-11-08 13:01:20 -050038 if (WARN(tty->magic != TTY_MAGIC, "U Bad %p\n", tty))
Alan Cox89c8d912012-08-08 16:30:13 +010039 return;
Alan Cox89c8d912012-08-08 16:30:13 +010040 mutex_unlock(&tty->legacy_mutex);
41 tty_kref_put(tty);
Arnd Bergmannb07471f2010-08-06 21:40:30 +020042}
43EXPORT_SYMBOL(tty_unlock);
Alan Cox89c8d912012-08-08 16:30:13 +010044
Peter Hurleyc2bb5242016-01-09 21:13:51 -080045void tty_lock_slave(struct tty_struct *tty)
Alan Cox89c8d912012-08-08 16:30:13 +010046{
Peter Hurleyeef15e22014-12-30 07:11:11 -050047 if (tty && tty != tty->link)
Peter Hurley2febdb62014-11-05 12:13:02 -050048 tty_lock(tty);
Alan Cox89c8d912012-08-08 16:30:13 +010049}
Alan Cox89c8d912012-08-08 16:30:13 +010050
Peter Hurleyc2bb5242016-01-09 21:13:51 -080051void tty_unlock_slave(struct tty_struct *tty)
Alan Cox89c8d912012-08-08 16:30:13 +010052{
Peter Hurley2aff5e22014-11-05 12:13:01 -050053 if (tty && tty != tty->link)
54 tty_unlock(tty);
Alan Cox89c8d912012-08-08 16:30:13 +010055}
Peter Hurley2febdb62014-11-05 12:13:02 -050056
57void tty_set_lock_subclass(struct tty_struct *tty)
58{
Peter Hurley3abf87c2015-01-17 15:42:04 -050059 lockdep_set_subclass(&tty->legacy_mutex, TTY_LOCK_SLAVE);
Peter Hurley2febdb62014-11-05 12:13:02 -050060}