blob: dfa9ec03fa8e06dfe03a6e0ae04c62a2ac774103 [file] [log] [blame]
Arnd Bergmannb07471f2010-08-06 21:40:30 +02001#include <linux/tty.h>
2#include <linux/module.h>
3#include <linux/kallsyms.h>
4#include <linux/semaphore.h>
5#include <linux/sched.h>
6
Alan Cox89c8d912012-08-08 16:30:13 +01007/* Legacy tty mutex glue */
8
Arnd Bergmannb07471f2010-08-06 21:40:30 +02009/*
10 * Getting the big tty mutex.
11 */
Alan Cox89c8d912012-08-08 16:30:13 +010012
Peter Hurley2febdb62014-11-05 12:13:02 -050013void __lockfunc tty_lock(struct tty_struct *tty)
Arnd Bergmannb07471f2010-08-06 21:40:30 +020014{
Peter Hurley6d029c62015-11-08 13:01:20 -050015 if (WARN(tty->magic != TTY_MAGIC, "L Bad %p\n", tty))
Alan Cox89c8d912012-08-08 16:30:13 +010016 return;
Alan Cox89c8d912012-08-08 16:30:13 +010017 tty_kref_get(tty);
Peter Hurley2febdb62014-11-05 12:13:02 -050018 mutex_lock(&tty->legacy_mutex);
Arnd Bergmannb07471f2010-08-06 21:40:30 +020019}
20EXPORT_SYMBOL(tty_lock);
21
Peter Hurley0bfd4642016-01-09 21:13:44 -080022int tty_lock_interruptible(struct tty_struct *tty)
23{
Peter Hurleye9036d02016-02-05 10:49:36 -080024 int ret;
25
Peter Hurley0bfd4642016-01-09 21:13:44 -080026 if (WARN(tty->magic != TTY_MAGIC, "L Bad %p\n", tty))
27 return -EIO;
28 tty_kref_get(tty);
Peter Hurleye9036d02016-02-05 10:49:36 -080029 ret = mutex_lock_interruptible(&tty->legacy_mutex);
30 if (ret)
31 tty_kref_put(tty);
32 return ret;
Peter Hurley0bfd4642016-01-09 21:13:44 -080033}
34
Alan Cox89c8d912012-08-08 16:30:13 +010035void __lockfunc tty_unlock(struct tty_struct *tty)
Arnd Bergmannb07471f2010-08-06 21:40:30 +020036{
Peter Hurley6d029c62015-11-08 13:01:20 -050037 if (WARN(tty->magic != TTY_MAGIC, "U Bad %p\n", tty))
Alan Cox89c8d912012-08-08 16:30:13 +010038 return;
Alan Cox89c8d912012-08-08 16:30:13 +010039 mutex_unlock(&tty->legacy_mutex);
40 tty_kref_put(tty);
Arnd Bergmannb07471f2010-08-06 21:40:30 +020041}
42EXPORT_SYMBOL(tty_unlock);
Alan Cox89c8d912012-08-08 16:30:13 +010043
Peter Hurley2aff5e22014-11-05 12:13:01 -050044void __lockfunc tty_lock_slave(struct tty_struct *tty)
Alan Cox89c8d912012-08-08 16:30:13 +010045{
Peter Hurleyeef15e22014-12-30 07:11:11 -050046 if (tty && tty != tty->link)
Peter Hurley2febdb62014-11-05 12:13:02 -050047 tty_lock(tty);
Alan Cox89c8d912012-08-08 16:30:13 +010048}
Alan Cox89c8d912012-08-08 16:30:13 +010049
Peter Hurley2aff5e22014-11-05 12:13:01 -050050void __lockfunc tty_unlock_slave(struct tty_struct *tty)
Alan Cox89c8d912012-08-08 16:30:13 +010051{
Peter Hurley2aff5e22014-11-05 12:13:01 -050052 if (tty && tty != tty->link)
53 tty_unlock(tty);
Alan Cox89c8d912012-08-08 16:30:13 +010054}
Peter Hurley2febdb62014-11-05 12:13:02 -050055
56void tty_set_lock_subclass(struct tty_struct *tty)
57{
Peter Hurley3abf87c2015-01-17 15:42:04 -050058 lockdep_set_subclass(&tty->legacy_mutex, TTY_LOCK_SLAVE);
Peter Hurley2febdb62014-11-05 12:13:02 -050059}