termios-base.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /* termios.h: generic termios/termio user copying/translation
  3. */
  4. #ifndef _ASM_GENERIC_TERMIOS_BASE_H
  5. #define _ASM_GENERIC_TERMIOS_BASE_H
  6. #include <linux/uaccess.h>
  7. #ifndef __ARCH_TERMIO_GETPUT
  8. /*
  9. * Translate a "termio" structure into a "termios". Ugh.
  10. */
  11. static inline int user_termio_to_kernel_termios(struct ktermios *termios,
  12. struct termio __user *termio)
  13. {
  14. unsigned short tmp;
  15. if (get_user(tmp, &termio->c_iflag) < 0)
  16. goto fault;
  17. termios->c_iflag = (0xffff0000 & termios->c_iflag) | tmp;
  18. if (get_user(tmp, &termio->c_oflag) < 0)
  19. goto fault;
  20. termios->c_oflag = (0xffff0000 & termios->c_oflag) | tmp;
  21. if (get_user(tmp, &termio->c_cflag) < 0)
  22. goto fault;
  23. termios->c_cflag = (0xffff0000 & termios->c_cflag) | tmp;
  24. if (get_user(tmp, &termio->c_lflag) < 0)
  25. goto fault;
  26. termios->c_lflag = (0xffff0000 & termios->c_lflag) | tmp;
  27. if (get_user(termios->c_line, &termio->c_line) < 0)
  28. goto fault;
  29. if (copy_from_user(termios->c_cc, termio->c_cc, NCC) != 0)
  30. goto fault;
  31. return 0;
  32. fault:
  33. return -EFAULT;
  34. }
  35. /*
  36. * Translate a "termios" structure into a "termio". Ugh.
  37. */
  38. static inline int kernel_termios_to_user_termio(struct termio __user *termio,
  39. struct ktermios *termios)
  40. {
  41. if (put_user(termios->c_iflag, &termio->c_iflag) < 0 ||
  42. put_user(termios->c_oflag, &termio->c_oflag) < 0 ||
  43. put_user(termios->c_cflag, &termio->c_cflag) < 0 ||
  44. put_user(termios->c_lflag, &termio->c_lflag) < 0 ||
  45. put_user(termios->c_line, &termio->c_line) < 0 ||
  46. copy_to_user(termio->c_cc, termios->c_cc, NCC) != 0)
  47. return -EFAULT;
  48. return 0;
  49. }
  50. #ifndef user_termios_to_kernel_termios
  51. #define user_termios_to_kernel_termios(k, u) copy_from_user(k, u, sizeof(struct termios))
  52. #endif
  53. #ifndef kernel_termios_to_user_termios
  54. #define kernel_termios_to_user_termios(u, k) copy_to_user(u, k, sizeof(struct termios))
  55. #endif
  56. #define user_termios_to_kernel_termios_1(k, u) copy_from_user(k, u, sizeof(struct termios))
  57. #define kernel_termios_to_user_termios_1(u, k) copy_to_user(u, k, sizeof(struct termios))
  58. #endif /* __ARCH_TERMIO_GETPUT */
  59. #endif /* _ASM_GENERIC_TERMIOS_BASE_H */