Linux Audio

Check our new training course

In-person Linux kernel drivers training

Jun 16-20, 2025
Register
Loading...
v5.4
 1// SPDX-License-Identifier: GPL-2.0
 2#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
 8/* Legacy tty mutex glue */
 9
 
 
 
 
 
10/*
11 * Getting the big tty mutex.
12 */
13
14void tty_lock(struct tty_struct *tty)
 
15{
16	if (WARN(tty->magic != TTY_MAGIC, "L Bad %p\n", tty))
 
 
17		return;
 
18	tty_kref_get(tty);
19	mutex_lock(&tty->legacy_mutex);
20}
21EXPORT_SYMBOL(tty_lock);
22
23int tty_lock_interruptible(struct tty_struct *tty)
24{
25	int ret;
26
27	if (WARN(tty->magic != TTY_MAGIC, "L Bad %p\n", tty))
28		return -EIO;
29	tty_kref_get(tty);
30	ret = mutex_lock_interruptible(&tty->legacy_mutex);
31	if (ret)
32		tty_kref_put(tty);
33	return ret;
34}
 
35
36void tty_unlock(struct tty_struct *tty)
37{
38	if (WARN(tty->magic != TTY_MAGIC, "U Bad %p\n", tty))
 
 
39		return;
 
40	mutex_unlock(&tty->legacy_mutex);
41	tty_kref_put(tty);
42}
43EXPORT_SYMBOL(tty_unlock);
44
45void tty_lock_slave(struct tty_struct *tty)
 
 
 
 
 
46{
47	if (tty && tty != tty->link)
48		tty_lock(tty);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49}
50
51void tty_unlock_slave(struct tty_struct *tty)
52{
53	if (tty && tty != tty->link)
54		tty_unlock(tty);
55}
56
57void tty_set_lock_subclass(struct tty_struct *tty)
58{
59	lockdep_set_subclass(&tty->legacy_mutex, TTY_LOCK_SLAVE);
60}
v3.15
 
 1#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
 7/* Legacy tty mutex glue */
 8
 9enum {
10	TTY_MUTEX_NORMAL,
11	TTY_MUTEX_NESTED,
12};
13
14/*
15 * Getting the big tty mutex.
16 */
17
18static void __lockfunc tty_lock_nested(struct tty_struct *tty,
19				       unsigned int subclass)
20{
21	if (tty->magic != TTY_MAGIC) {
22		pr_err("L Bad %p\n", tty);
23		WARN_ON(1);
24		return;
25	}
26	tty_kref_get(tty);
27	mutex_lock_nested(&tty->legacy_mutex, subclass);
28}
 
29
30void __lockfunc tty_lock(struct tty_struct *tty)
31{
32	return tty_lock_nested(tty, TTY_MUTEX_NORMAL);
 
 
 
 
 
 
 
 
33}
34EXPORT_SYMBOL(tty_lock);
35
36void __lockfunc tty_unlock(struct tty_struct *tty)
37{
38	if (tty->magic != TTY_MAGIC) {
39		pr_err("U Bad %p\n", tty);
40		WARN_ON(1);
41		return;
42	}
43	mutex_unlock(&tty->legacy_mutex);
44	tty_kref_put(tty);
45}
46EXPORT_SYMBOL(tty_unlock);
47
48/*
49 * Getting the big tty mutex for a pair of ttys with lock ordering
50 * On a non pty/tty pair tty2 can be NULL which is just fine.
51 */
52void __lockfunc tty_lock_pair(struct tty_struct *tty,
53					struct tty_struct *tty2)
54{
55	if (tty < tty2) {
56		tty_lock(tty);
57		tty_lock_nested(tty2, TTY_MUTEX_NESTED);
58	} else {
59		if (tty2 && tty2 != tty)
60			tty_lock(tty2);
61		tty_lock_nested(tty, TTY_MUTEX_NESTED);
62	}
63}
64EXPORT_SYMBOL(tty_lock_pair);
65
66void __lockfunc tty_unlock_pair(struct tty_struct *tty,
67						struct tty_struct *tty2)
68{
69	tty_unlock(tty);
70	if (tty2 && tty2 != tty)
71		tty_unlock(tty2);
72}
73EXPORT_SYMBOL(tty_unlock_pair);