Lines Matching refs:tty

27 #define tty_ldisc_debug(tty, f, args...)	tty_debug(tty, f, ##args)  argument
29 #define tty_ldisc_debug(tty, f, args...) argument
146 static struct tty_ldisc *tty_ldisc_get(struct tty_struct *tty, int disc) in tty_ldisc_get() argument
174 ld->tty = tty; in tty_ldisc_get()
248 struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty) in tty_ldisc_ref_wait() argument
252 ldsem_down_read(&tty->ldisc_sem, MAX_SCHEDULE_TIMEOUT); in tty_ldisc_ref_wait()
253 ld = tty->ldisc; in tty_ldisc_ref_wait()
255 ldsem_up_read(&tty->ldisc_sem); in tty_ldisc_ref_wait()
269 struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty) in tty_ldisc_ref() argument
273 if (ldsem_down_read_trylock(&tty->ldisc_sem)) { in tty_ldisc_ref()
274 ld = tty->ldisc; in tty_ldisc_ref()
276 ldsem_up_read(&tty->ldisc_sem); in tty_ldisc_ref()
292 ldsem_up_read(&ld->tty->ldisc_sem); in tty_ldisc_deref()
298 __tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout) in __tty_ldisc_lock() argument
300 return ldsem_down_write(&tty->ldisc_sem, timeout); in __tty_ldisc_lock()
304 __tty_ldisc_lock_nested(struct tty_struct *tty, unsigned long timeout) in __tty_ldisc_lock_nested() argument
306 return ldsem_down_write_nested(&tty->ldisc_sem, in __tty_ldisc_lock_nested()
310 static inline void __tty_ldisc_unlock(struct tty_struct *tty) in __tty_ldisc_unlock() argument
312 ldsem_up_write(&tty->ldisc_sem); in __tty_ldisc_unlock()
315 int tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout) in tty_ldisc_lock() argument
320 set_bit(TTY_LDISC_CHANGING, &tty->flags); in tty_ldisc_lock()
321 wake_up_interruptible_all(&tty->read_wait); in tty_ldisc_lock()
322 wake_up_interruptible_all(&tty->write_wait); in tty_ldisc_lock()
324 ret = __tty_ldisc_lock(tty, timeout); in tty_ldisc_lock()
327 set_bit(TTY_LDISC_HALTED, &tty->flags); in tty_ldisc_lock()
331 void tty_ldisc_unlock(struct tty_struct *tty) in tty_ldisc_unlock() argument
333 clear_bit(TTY_LDISC_HALTED, &tty->flags); in tty_ldisc_unlock()
335 clear_bit(TTY_LDISC_CHANGING, &tty->flags); in tty_ldisc_unlock()
336 __tty_ldisc_unlock(tty); in tty_ldisc_unlock()
340 tty_ldisc_lock_pair_timeout(struct tty_struct *tty, struct tty_struct *tty2, in tty_ldisc_lock_pair_timeout() argument
345 if (tty < tty2) { in tty_ldisc_lock_pair_timeout()
346 ret = __tty_ldisc_lock(tty, timeout); in tty_ldisc_lock_pair_timeout()
350 __tty_ldisc_unlock(tty); in tty_ldisc_lock_pair_timeout()
354 WARN_ON_ONCE(tty == tty2); in tty_ldisc_lock_pair_timeout()
355 if (tty2 && tty != tty2) { in tty_ldisc_lock_pair_timeout()
358 ret = __tty_ldisc_lock_nested(tty, timeout); in tty_ldisc_lock_pair_timeout()
363 ret = __tty_ldisc_lock(tty, timeout); in tty_ldisc_lock_pair_timeout()
369 set_bit(TTY_LDISC_HALTED, &tty->flags); in tty_ldisc_lock_pair_timeout()
375 static void tty_ldisc_lock_pair(struct tty_struct *tty, struct tty_struct *tty2) in tty_ldisc_lock_pair() argument
377 tty_ldisc_lock_pair_timeout(tty, tty2, MAX_SCHEDULE_TIMEOUT); in tty_ldisc_lock_pair()
380 static void tty_ldisc_unlock_pair(struct tty_struct *tty, in tty_ldisc_unlock_pair() argument
383 __tty_ldisc_unlock(tty); in tty_ldisc_unlock_pair()
396 void tty_ldisc_flush(struct tty_struct *tty) in tty_ldisc_flush() argument
398 struct tty_ldisc *ld = tty_ldisc_ref(tty); in tty_ldisc_flush()
400 tty_buffer_flush(tty, ld); in tty_ldisc_flush()
422 static void tty_set_termios_ldisc(struct tty_struct *tty, int disc) in tty_set_termios_ldisc() argument
424 down_write(&tty->termios_rwsem); in tty_set_termios_ldisc()
425 tty->termios.c_line = disc; in tty_set_termios_ldisc()
426 up_write(&tty->termios_rwsem); in tty_set_termios_ldisc()
428 tty->disc_data = NULL; in tty_set_termios_ldisc()
429 tty->receive_room = 0; in tty_set_termios_ldisc()
443 static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld) in tty_ldisc_open() argument
445 WARN_ON(test_and_set_bit(TTY_LDISC_OPEN, &tty->flags)); in tty_ldisc_open()
449 ret = ld->ops->open(tty); in tty_ldisc_open()
451 clear_bit(TTY_LDISC_OPEN, &tty->flags); in tty_ldisc_open()
453 tty_ldisc_debug(tty, "%p: opened\n", ld); in tty_ldisc_open()
468 static void tty_ldisc_close(struct tty_struct *tty, struct tty_ldisc *ld) in tty_ldisc_close() argument
470 lockdep_assert_held_write(&tty->ldisc_sem); in tty_ldisc_close()
471 WARN_ON(!test_bit(TTY_LDISC_OPEN, &tty->flags)); in tty_ldisc_close()
472 clear_bit(TTY_LDISC_OPEN, &tty->flags); in tty_ldisc_close()
474 ld->ops->close(tty); in tty_ldisc_close()
475 tty_ldisc_debug(tty, "%p: closed\n", ld); in tty_ldisc_close()
487 static int tty_ldisc_failto(struct tty_struct *tty, int ld) in tty_ldisc_failto() argument
489 struct tty_ldisc *disc = tty_ldisc_get(tty, ld); in tty_ldisc_failto()
492 lockdep_assert_held_write(&tty->ldisc_sem); in tty_ldisc_failto()
495 tty->ldisc = disc; in tty_ldisc_failto()
496 tty_set_termios_ldisc(tty, ld); in tty_ldisc_failto()
497 r = tty_ldisc_open(tty, disc); in tty_ldisc_failto()
512 static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old) in tty_ldisc_restore() argument
515 if (tty_ldisc_failto(tty, old->ops->num) < 0) { in tty_ldisc_restore()
516 const char *name = tty_name(tty); in tty_ldisc_restore()
524 if (tty_ldisc_failto(tty, N_TTY) < 0 && in tty_ldisc_restore()
525 tty_ldisc_failto(tty, N_NULL) < 0) in tty_ldisc_restore()
541 int tty_set_ldisc(struct tty_struct *tty, int disc) in tty_set_ldisc() argument
546 new_ldisc = tty_ldisc_get(tty, disc); in tty_set_ldisc()
550 tty_lock(tty); in tty_set_ldisc()
551 retval = tty_ldisc_lock(tty, 5 * HZ); in tty_set_ldisc()
555 if (!tty->ldisc) { in tty_set_ldisc()
561 if (tty->ldisc->ops->num == disc) in tty_set_ldisc()
564 if (test_bit(TTY_HUPPED, &tty->flags)) { in tty_set_ldisc()
570 old_ldisc = tty->ldisc; in tty_set_ldisc()
573 tty_ldisc_close(tty, old_ldisc); in tty_set_ldisc()
576 tty->ldisc = new_ldisc; in tty_set_ldisc()
577 tty_set_termios_ldisc(tty, disc); in tty_set_ldisc()
579 retval = tty_ldisc_open(tty, new_ldisc); in tty_set_ldisc()
583 tty_ldisc_restore(tty, old_ldisc); in tty_set_ldisc()
586 if (tty->ldisc->ops->num != old_ldisc->ops->num && tty->ops->set_ldisc) { in tty_set_ldisc()
587 down_read(&tty->termios_rwsem); in tty_set_ldisc()
588 tty->ops->set_ldisc(tty); in tty_set_ldisc()
589 up_read(&tty->termios_rwsem); in tty_set_ldisc()
601 tty_ldisc_unlock(tty); in tty_set_ldisc()
607 tty_buffer_restart_work(tty->port); in tty_set_ldisc()
610 tty_unlock(tty); in tty_set_ldisc()
621 static void tty_ldisc_kill(struct tty_struct *tty) in tty_ldisc_kill() argument
623 lockdep_assert_held_write(&tty->ldisc_sem); in tty_ldisc_kill()
624 if (!tty->ldisc) in tty_ldisc_kill()
629 tty_ldisc_close(tty, tty->ldisc); in tty_ldisc_kill()
630 tty_ldisc_put(tty->ldisc); in tty_ldisc_kill()
632 tty->ldisc = NULL; in tty_ldisc_kill()
642 static void tty_reset_termios(struct tty_struct *tty) in tty_reset_termios() argument
644 down_write(&tty->termios_rwsem); in tty_reset_termios()
645 tty->termios = tty->driver->init_termios; in tty_reset_termios()
646 tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios); in tty_reset_termios()
647 tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios); in tty_reset_termios()
648 up_write(&tty->termios_rwsem); in tty_reset_termios()
666 int tty_ldisc_reinit(struct tty_struct *tty, int disc) in tty_ldisc_reinit() argument
671 lockdep_assert_held_write(&tty->ldisc_sem); in tty_ldisc_reinit()
672 ld = tty_ldisc_get(tty, disc); in tty_ldisc_reinit()
678 if (tty->ldisc) { in tty_ldisc_reinit()
679 tty_ldisc_close(tty, tty->ldisc); in tty_ldisc_reinit()
680 tty_ldisc_put(tty->ldisc); in tty_ldisc_reinit()
684 tty->ldisc = ld; in tty_ldisc_reinit()
685 tty_set_termios_ldisc(tty, disc); in tty_ldisc_reinit()
686 retval = tty_ldisc_open(tty, tty->ldisc); in tty_ldisc_reinit()
688 tty_ldisc_put(tty->ldisc); in tty_ldisc_reinit()
689 tty->ldisc = NULL; in tty_ldisc_reinit()
710 void tty_ldisc_hangup(struct tty_struct *tty, bool reinit) in tty_ldisc_hangup() argument
714 tty_ldisc_debug(tty, "%p: hangup\n", tty->ldisc); in tty_ldisc_hangup()
716 ld = tty_ldisc_ref(tty); in tty_ldisc_hangup()
719 ld->ops->flush_buffer(tty); in tty_ldisc_hangup()
720 tty_driver_flush_buffer(tty); in tty_ldisc_hangup()
721 if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) && in tty_ldisc_hangup()
723 ld->ops->write_wakeup(tty); in tty_ldisc_hangup()
725 ld->ops->hangup(tty); in tty_ldisc_hangup()
729 wake_up_interruptible_poll(&tty->write_wait, EPOLLOUT); in tty_ldisc_hangup()
730 wake_up_interruptible_poll(&tty->read_wait, EPOLLIN); in tty_ldisc_hangup()
738 tty_ldisc_lock(tty, MAX_SCHEDULE_TIMEOUT); in tty_ldisc_hangup()
740 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) in tty_ldisc_hangup()
741 tty_reset_termios(tty); in tty_ldisc_hangup()
743 if (tty->ldisc) { in tty_ldisc_hangup()
745 if (tty_ldisc_reinit(tty, tty->termios.c_line) < 0 && in tty_ldisc_hangup()
746 tty_ldisc_reinit(tty, N_TTY) < 0) in tty_ldisc_hangup()
747 WARN_ON(tty_ldisc_reinit(tty, N_NULL) < 0); in tty_ldisc_hangup()
749 tty_ldisc_kill(tty); in tty_ldisc_hangup()
751 tty_ldisc_unlock(tty); in tty_ldisc_hangup()
764 int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty) in tty_ldisc_setup() argument
766 int retval = tty_ldisc_open(tty, tty->ldisc); in tty_ldisc_setup()
778 tty_ldisc_close(tty, tty->ldisc); in tty_ldisc_setup()
793 void tty_ldisc_release(struct tty_struct *tty) in tty_ldisc_release() argument
795 struct tty_struct *o_tty = tty->link; in tty_ldisc_release()
802 tty_ldisc_lock_pair(tty, o_tty); in tty_ldisc_release()
803 tty_ldisc_kill(tty); in tty_ldisc_release()
806 tty_ldisc_unlock_pair(tty, o_tty); in tty_ldisc_release()
813 tty_ldisc_debug(tty, "released\n"); in tty_ldisc_release()
824 int tty_ldisc_init(struct tty_struct *tty) in tty_ldisc_init() argument
826 struct tty_ldisc *ld = tty_ldisc_get(tty, N_TTY); in tty_ldisc_init()
830 tty->ldisc = ld; in tty_ldisc_init()
841 void tty_ldisc_deinit(struct tty_struct *tty) in tty_ldisc_deinit() argument
844 if (tty->ldisc) in tty_ldisc_deinit()
845 tty_ldisc_put(tty->ldisc); in tty_ldisc_deinit()
846 tty->ldisc = NULL; in tty_ldisc_deinit()