tty_ioctl.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include <stddef.h>
  2. #include <rtthread.h>
  3. #include <tty.h>
  4. #if defined(RT_USING_POSIX)
  5. #include <posix_termios.h>
  6. #endif
  7. #define DBG_TAG "TTY_IOCTL"
  8. #ifdef RT_TTY_DEBUG
  9. #define DBG_LVL DBG_LOG
  10. #else
  11. #define DBG_LVL DBG_INFO
  12. #endif /* RT_TTY_DEBUG */
  13. #include <rtdbg.h>
  14. /*
  15. * Internal flag options for termios setting behavior
  16. */
  17. #define TERMIOS_FLUSH 1
  18. #define TERMIOS_WAIT 2
  19. #define TERMIOS_TERMIO 4
  20. #define TERMIOS_OLD 8
  21. /**
  22. * set_termios - set termios values for a tty
  23. * @tty: terminal device
  24. * @arg: user data
  25. * @opt: option information
  26. *
  27. * Helper function to prepare termios data and run necessary other
  28. * functions before using tty_set_termios to do the actual changes.
  29. *
  30. * Locking:
  31. * Called functions take ldisc and termios_rwsem locks
  32. */
  33. static int set_termios(struct tty_struct *tty, void *arg, int opt)
  34. {
  35. struct termios old_termios = tty->init_termios;
  36. struct tty_ldisc *ld = RT_NULL;
  37. struct termios *new_termios = (struct termios *)arg;
  38. int level = 0;
  39. int retval = tty_check_change(tty);
  40. if (retval)
  41. {
  42. return retval;
  43. }
  44. level = rt_hw_interrupt_disable();
  45. tty->init_termios = *new_termios;
  46. rt_hw_interrupt_enable(level);
  47. ld = tty->ldisc;
  48. if (ld != NULL)
  49. {
  50. if (ld->ops->set_termios)
  51. {
  52. ld->ops->set_termios(tty, &old_termios);
  53. }
  54. }
  55. return 0;
  56. }
  57. int n_tty_ioctl_extend(struct tty_struct *tty, int cmd, void *args)
  58. {
  59. int ret = 0;
  60. void *p = (void *)args;
  61. struct tty_struct *real_tty = RT_NULL;
  62. if (tty->type == TTY_DRIVER_TYPE_PTY && tty->subtype == PTY_TYPE_MASTER)
  63. {
  64. real_tty = tty->other_struct;
  65. }
  66. else
  67. {
  68. real_tty = tty;
  69. }
  70. switch(cmd)
  71. {
  72. case TCGETS:
  73. {
  74. struct termios *tio = (struct termios *)p;
  75. if (tio == RT_NULL)
  76. {
  77. return -RT_EINVAL;
  78. }
  79. rt_memcpy(tio, &real_tty->init_termios, sizeof(real_tty->init_termios));
  80. return ret;
  81. }
  82. case TCSETSF:
  83. {
  84. return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT | TERMIOS_OLD);
  85. }
  86. case TCSETSW:
  87. {
  88. return set_termios(real_tty, p, TERMIOS_WAIT | TERMIOS_OLD);
  89. }
  90. case TCSETS:
  91. {
  92. return set_termios(real_tty, p, TERMIOS_OLD);
  93. }
  94. default:
  95. break;
  96. }
  97. return -ENOIOCTLCMD;
  98. }