18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci#include <linux/tty.h>
38c2ecf20Sopenharmony_ci#include <linux/module.h>
48c2ecf20Sopenharmony_ci#include <linux/kallsyms.h>
58c2ecf20Sopenharmony_ci#include <linux/semaphore.h>
68c2ecf20Sopenharmony_ci#include <linux/sched.h>
78c2ecf20Sopenharmony_ci#include "tty.h"
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci/* Legacy tty mutex glue */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci/*
128c2ecf20Sopenharmony_ci * Getting the big tty mutex.
138c2ecf20Sopenharmony_ci */
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_civoid tty_lock(struct tty_struct *tty)
168c2ecf20Sopenharmony_ci{
178c2ecf20Sopenharmony_ci	if (WARN(tty->magic != TTY_MAGIC, "L Bad %p\n", tty))
188c2ecf20Sopenharmony_ci		return;
198c2ecf20Sopenharmony_ci	tty_kref_get(tty);
208c2ecf20Sopenharmony_ci	mutex_lock(&tty->legacy_mutex);
218c2ecf20Sopenharmony_ci}
228c2ecf20Sopenharmony_ciEXPORT_SYMBOL(tty_lock);
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ciint tty_lock_interruptible(struct tty_struct *tty)
258c2ecf20Sopenharmony_ci{
268c2ecf20Sopenharmony_ci	int ret;
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci	if (WARN(tty->magic != TTY_MAGIC, "L Bad %p\n", tty))
298c2ecf20Sopenharmony_ci		return -EIO;
308c2ecf20Sopenharmony_ci	tty_kref_get(tty);
318c2ecf20Sopenharmony_ci	ret = mutex_lock_interruptible(&tty->legacy_mutex);
328c2ecf20Sopenharmony_ci	if (ret)
338c2ecf20Sopenharmony_ci		tty_kref_put(tty);
348c2ecf20Sopenharmony_ci	return ret;
358c2ecf20Sopenharmony_ci}
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_civoid tty_unlock(struct tty_struct *tty)
388c2ecf20Sopenharmony_ci{
398c2ecf20Sopenharmony_ci	if (WARN(tty->magic != TTY_MAGIC, "U Bad %p\n", tty))
408c2ecf20Sopenharmony_ci		return;
418c2ecf20Sopenharmony_ci	mutex_unlock(&tty->legacy_mutex);
428c2ecf20Sopenharmony_ci	tty_kref_put(tty);
438c2ecf20Sopenharmony_ci}
448c2ecf20Sopenharmony_ciEXPORT_SYMBOL(tty_unlock);
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_civoid tty_lock_slave(struct tty_struct *tty)
478c2ecf20Sopenharmony_ci{
488c2ecf20Sopenharmony_ci	if (tty && tty != tty->link)
498c2ecf20Sopenharmony_ci		tty_lock(tty);
508c2ecf20Sopenharmony_ci}
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_civoid tty_unlock_slave(struct tty_struct *tty)
538c2ecf20Sopenharmony_ci{
548c2ecf20Sopenharmony_ci	if (tty && tty != tty->link)
558c2ecf20Sopenharmony_ci		tty_unlock(tty);
568c2ecf20Sopenharmony_ci}
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_civoid tty_set_lock_subclass(struct tty_struct *tty)
598c2ecf20Sopenharmony_ci{
608c2ecf20Sopenharmony_ci	lockdep_set_subclass(&tty->legacy_mutex, TTY_LOCK_SLAVE);
618c2ecf20Sopenharmony_ci}
62