blob: 75351e4b77dfbad2843cb96331b1bcdfa4af4672 [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 Hurleyc2bb5242016-01-09 21:13:51 -080013void 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{
24 if (WARN(tty->magic != TTY_MAGIC, "L Bad %p\n", tty))
25 return -EIO;
26 tty_kref_get(tty);
27 return mutex_lock_interruptible(&tty->legacy_mutex);
28}
29
Peter Hurleyc2bb5242016-01-09 21:13:51 -080030void tty_unlock(struct tty_struct *tty)
Arnd Bergmannb07471f2010-08-06 21:40:30 +020031{
Peter Hurley6d029c62015-11-08 13:01:20 -050032 if (WARN(tty->magic != TTY_MAGIC, "U Bad %p\n", tty))
Alan Cox89c8d912012-08-08 16:30:13 +010033 return;
Alan Cox89c8d912012-08-08 16:30:13 +010034 mutex_unlock(&tty->legacy_mutex);
35 tty_kref_put(tty);
Arnd Bergmannb07471f2010-08-06 21:40:30 +020036}
37EXPORT_SYMBOL(tty_unlock);
Alan Cox89c8d912012-08-08 16:30:13 +010038
Peter Hurleyc2bb5242016-01-09 21:13:51 -080039void tty_lock_slave(struct tty_struct *tty)
Alan Cox89c8d912012-08-08 16:30:13 +010040{
Peter Hurleyeef15e22014-12-30 07:11:11 -050041 if (tty && tty != tty->link)
Peter Hurley2febdb62014-11-05 12:13:02 -050042 tty_lock(tty);
Alan Cox89c8d912012-08-08 16:30:13 +010043}
Alan Cox89c8d912012-08-08 16:30:13 +010044
Peter Hurleyc2bb5242016-01-09 21:13:51 -080045void tty_unlock_slave(struct tty_struct *tty)
Alan Cox89c8d912012-08-08 16:30:13 +010046{
Peter Hurley2aff5e22014-11-05 12:13:01 -050047 if (tty && tty != tty->link)
48 tty_unlock(tty);
Alan Cox89c8d912012-08-08 16:30:13 +010049}
Peter Hurley2febdb62014-11-05 12:13:02 -050050
51void tty_set_lock_subclass(struct tty_struct *tty)
52{
Peter Hurley3abf87c2015-01-17 15:42:04 -050053 lockdep_set_subclass(&tty->legacy_mutex, TTY_LOCK_SLAVE);
Peter Hurley2febdb62014-11-05 12:13:02 -050054}