termios.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_GENERIC_TERMIOS_H
  3. #define _ASM_GENERIC_TERMIOS_H
  4. #include <linux/uaccess.h>
  5. #include <uapi/asm-generic/termios.h>
  6. /* intr=^C quit=^\ erase=del kill=^U
  7. eof=^D vtime=\0 vmin=\1 sxtc=\0
  8. start=^Q stop=^S susp=^Z eol=\0
  9. reprint=^R discard=^U werase=^W lnext=^V
  10. eol2=\0
  11. */
  12. #define INIT_C_CC "\003\034\177\025\004\0\1\0\021\023\032\0\022\017\027\026\0"
  13. /*
  14. * Translate a "termio" structure into a "termios". Ugh.
  15. */
  16. static inline int user_termio_to_kernel_termios(struct ktermios *termios,
  17. const struct termio __user *termio)
  18. {
  19. unsigned short tmp;
  20. if (get_user(tmp, &termio->c_iflag) < 0)
  21. goto fault;
  22. termios->c_iflag = (0xffff0000 & termios->c_iflag) | tmp;
  23. if (get_user(tmp, &termio->c_oflag) < 0)
  24. goto fault;
  25. termios->c_oflag = (0xffff0000 & termios->c_oflag) | tmp;
  26. if (get_user(tmp, &termio->c_cflag) < 0)
  27. goto fault;
  28. termios->c_cflag = (0xffff0000 & termios->c_cflag) | tmp;
  29. if (get_user(tmp, &termio->c_lflag) < 0)
  30. goto fault;
  31. termios->c_lflag = (0xffff0000 & termios->c_lflag) | tmp;
  32. if (get_user(termios->c_line, &termio->c_line) < 0)
  33. goto fault;
  34. if (copy_from_user(termios->c_cc, termio->c_cc, NCC) != 0)
  35. goto fault;
  36. return 0;
  37. fault:
  38. return -EFAULT;
  39. }
  40. /*
  41. * Translate a "termios" structure into a "termio". Ugh.
  42. */
  43. static inline int kernel_termios_to_user_termio(struct termio __user *termio,
  44. struct ktermios *termios)
  45. {
  46. if (put_user(termios->c_iflag, &termio->c_iflag) < 0 ||
  47. put_user(termios->c_oflag, &termio->c_oflag) < 0 ||
  48. put_user(termios->c_cflag, &termio->c_cflag) < 0 ||
  49. put_user(termios->c_lflag, &termio->c_lflag) < 0 ||
  50. put_user(termios->c_line, &termio->c_line) < 0 ||
  51. copy_to_user(termio->c_cc, termios->c_cc, NCC) != 0)
  52. return -EFAULT;
  53. return 0;
  54. }
  55. #ifdef TCGETS2
  56. static inline int user_termios_to_kernel_termios(struct ktermios *k,
  57. struct termios2 __user *u)
  58. {
  59. return copy_from_user(k, u, sizeof(struct termios2));
  60. }
  61. static inline int kernel_termios_to_user_termios(struct termios2 __user *u,
  62. struct ktermios *k)
  63. {
  64. return copy_to_user(u, k, sizeof(struct termios2));
  65. }
  66. static inline int user_termios_to_kernel_termios_1(struct ktermios *k,
  67. struct termios __user *u)
  68. {
  69. return copy_from_user(k, u, sizeof(struct termios));
  70. }
  71. static inline int kernel_termios_to_user_termios_1(struct termios __user *u,
  72. struct ktermios *k)
  73. {
  74. return copy_to_user(u, k, sizeof(struct termios));
  75. }
  76. #else /* TCGETS2 */
  77. static inline int user_termios_to_kernel_termios(struct ktermios *k,
  78. struct termios __user *u)
  79. {
  80. return copy_from_user(k, u, sizeof(struct termios));
  81. }
  82. static inline int kernel_termios_to_user_termios(struct termios __user *u,
  83. struct ktermios *k)
  84. {
  85. return copy_to_user(u, k, sizeof(struct termios));
  86. }
  87. #endif /* TCGETS2 */
  88. #endif /* _ASM_GENERIC_TERMIOS_H */