tty_ioctl.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include <stddef.h>
  2. #include <rtthread.h>
  3. #include <tty.h>
  4. #if defined(RT_USING_POSIX_DEVIO)
  5. #include <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;
  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. rt_memcpy(&old_termios, &(tty->init_termios), sizeof(struct termios));
  45. level = rt_hw_interrupt_disable();
  46. tty->init_termios = *new_termios;
  47. rt_hw_interrupt_enable(level);
  48. ld = tty->ldisc;
  49. if (ld != NULL)
  50. {
  51. if (ld->ops->set_termios)
  52. {
  53. ld->ops->set_termios(tty, &old_termios);
  54. }
  55. }
  56. return 0;
  57. }
  58. int n_tty_ioctl_extend(struct tty_struct *tty, int cmd, void *args)
  59. {
  60. int ret = 0;
  61. void *p = (void *)args;
  62. struct tty_struct *real_tty = RT_NULL;
  63. if (tty->type == TTY_DRIVER_TYPE_PTY && tty->subtype == PTY_TYPE_MASTER)
  64. {
  65. real_tty = tty->other_struct;
  66. }
  67. else
  68. {
  69. real_tty = tty;
  70. }
  71. switch(cmd)
  72. {
  73. case TCGETS:
  74. case TCGETA:
  75. {
  76. struct termios *tio = (struct termios *)p;
  77. if (tio == RT_NULL)
  78. {
  79. return -RT_EINVAL;
  80. }
  81. rt_memcpy(tio, &real_tty->init_termios, sizeof(real_tty->init_termios));
  82. return ret;
  83. }
  84. case TCSETSF:
  85. {
  86. return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT | TERMIOS_OLD);
  87. }
  88. case TCSETSW:
  89. {
  90. return set_termios(real_tty, p, TERMIOS_WAIT | TERMIOS_OLD);
  91. }
  92. case TCSETS:
  93. {
  94. return set_termios(real_tty, p, TERMIOS_OLD);
  95. }
  96. case TCSETAF:
  97. {
  98. return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT | TERMIOS_TERMIO);
  99. }
  100. case TCSETAW:
  101. {
  102. return set_termios(real_tty, p, TERMIOS_WAIT | TERMIOS_TERMIO);
  103. }
  104. case TCSETA:
  105. {
  106. return set_termios(real_tty, p, TERMIOS_TERMIO);
  107. }
  108. default:
  109. break;
  110. }
  111. return -ENOIOCTLCMD;
  112. }