xref: /openbmc/linux/drivers/tty/tty_mutex.c (revision 3abf87cd3e70009ed70a7f28c2914888a7e27332)
196fd7ce5SGreg Kroah-Hartman #include <linux/tty.h>
296fd7ce5SGreg Kroah-Hartman #include <linux/module.h>
396fd7ce5SGreg Kroah-Hartman #include <linux/kallsyms.h>
496fd7ce5SGreg Kroah-Hartman #include <linux/semaphore.h>
596fd7ce5SGreg Kroah-Hartman #include <linux/sched.h>
696fd7ce5SGreg Kroah-Hartman 
789c8d91eSAlan Cox /* Legacy tty mutex glue */
889c8d91eSAlan Cox 
996fd7ce5SGreg Kroah-Hartman /*
1096fd7ce5SGreg Kroah-Hartman  * Getting the big tty mutex.
1196fd7ce5SGreg Kroah-Hartman  */
1289c8d91eSAlan Cox 
132febdb63SPeter Hurley void __lockfunc tty_lock(struct tty_struct *tty)
1496fd7ce5SGreg Kroah-Hartman {
1589c8d91eSAlan Cox 	if (tty->magic != TTY_MAGIC) {
167a0c4edaSSangho Yi 		pr_err("L Bad %p\n", tty);
1789c8d91eSAlan Cox 		WARN_ON(1);
1889c8d91eSAlan Cox 		return;
1989c8d91eSAlan Cox 	}
2089c8d91eSAlan Cox 	tty_kref_get(tty);
212febdb63SPeter Hurley 	mutex_lock(&tty->legacy_mutex);
2296fd7ce5SGreg Kroah-Hartman }
2396fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_lock);
2496fd7ce5SGreg Kroah-Hartman 
2589c8d91eSAlan Cox void __lockfunc tty_unlock(struct tty_struct *tty)
2696fd7ce5SGreg Kroah-Hartman {
2789c8d91eSAlan Cox 	if (tty->magic != TTY_MAGIC) {
287a0c4edaSSangho Yi 		pr_err("U Bad %p\n", tty);
2989c8d91eSAlan Cox 		WARN_ON(1);
3089c8d91eSAlan Cox 		return;
3189c8d91eSAlan Cox 	}
3289c8d91eSAlan Cox 	mutex_unlock(&tty->legacy_mutex);
3389c8d91eSAlan Cox 	tty_kref_put(tty);
3496fd7ce5SGreg Kroah-Hartman }
3596fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_unlock);
3689c8d91eSAlan Cox 
372aff5e2bSPeter Hurley void __lockfunc tty_lock_slave(struct tty_struct *tty)
3889c8d91eSAlan Cox {
39eef15e2aSPeter Hurley 	if (tty && tty != tty->link)
402febdb63SPeter Hurley 		tty_lock(tty);
4189c8d91eSAlan Cox }
4289c8d91eSAlan Cox 
432aff5e2bSPeter Hurley void __lockfunc tty_unlock_slave(struct tty_struct *tty)
4489c8d91eSAlan Cox {
452aff5e2bSPeter Hurley 	if (tty && tty != tty->link)
4689c8d91eSAlan Cox 		tty_unlock(tty);
4789c8d91eSAlan Cox }
482febdb63SPeter Hurley 
492febdb63SPeter Hurley void tty_set_lock_subclass(struct tty_struct *tty)
502febdb63SPeter Hurley {
51*3abf87cdSPeter Hurley 	lockdep_set_subclass(&tty->legacy_mutex, TTY_LOCK_SLAVE);
522febdb63SPeter Hurley }
53