tty_baudrate.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
  4. */
  5. #include <linux/types.h>
  6. #include <linux/kernel.h>
  7. #include <linux/termios.h>
  8. #include <linux/tty.h>
  9. #include <linux/export.h>
  10. #include "tty.h"
  11. /*
  12. * Routine which returns the baud rate of the tty
  13. *
  14. * Note that the baud_table needs to be kept in sync with the
  15. * include/asm/termbits.h file.
  16. */
  17. static const speed_t baud_table[] = {
  18. 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400,
  19. 4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800,
  20. #ifdef __sparc__
  21. 76800, 153600, 307200, 614400, 921600, 500000, 576000,
  22. 1000000, 1152000, 1500000, 2000000
  23. #else
  24. 500000, 576000, 921600, 1000000, 1152000, 1500000, 2000000,
  25. 2500000, 3000000, 3500000, 4000000
  26. #endif
  27. };
  28. static const tcflag_t baud_bits[] = {
  29. B0, B50, B75, B110, B134, B150, B200, B300, B600, B1200, B1800, B2400,
  30. B4800, B9600, B19200, B38400, B57600, B115200, B230400, B460800,
  31. #ifdef __sparc__
  32. B76800, B153600, B307200, B614400, B921600, B500000, B576000,
  33. B1000000, B1152000, B1500000, B2000000
  34. #else
  35. B500000, B576000, B921600, B1000000, B1152000, B1500000, B2000000,
  36. B2500000, B3000000, B3500000, B4000000
  37. #endif
  38. };
  39. static int n_baud_table = ARRAY_SIZE(baud_table);
  40. /**
  41. * tty_termios_baud_rate
  42. * @termios: termios structure
  43. *
  44. * Convert termios baud rate data into a speed. This should be called
  45. * with the termios lock held if this termios is a terminal termios
  46. * structure. Device drivers can call this function but should use
  47. * ->c_[io]speed directly as they are updated.
  48. *
  49. * Locking: none
  50. */
  51. speed_t tty_termios_baud_rate(const struct ktermios *termios)
  52. {
  53. unsigned int cbaud;
  54. cbaud = termios->c_cflag & CBAUD;
  55. /* Magic token for arbitrary speed via c_ispeed/c_ospeed */
  56. if (cbaud == BOTHER)
  57. return termios->c_ospeed;
  58. if (cbaud & CBAUDEX) {
  59. cbaud &= ~CBAUDEX;
  60. cbaud += 15;
  61. }
  62. return cbaud >= n_baud_table ? 0 : baud_table[cbaud];
  63. }
  64. EXPORT_SYMBOL(tty_termios_baud_rate);
  65. /**
  66. * tty_termios_input_baud_rate
  67. * @termios: termios structure
  68. *
  69. * Convert termios baud rate data into a speed. This should be called
  70. * with the termios lock held if this termios is a terminal termios
  71. * structure. Device drivers can call this function but should use
  72. * ->c_[io]speed directly as they are updated.
  73. *
  74. * Locking: none
  75. */
  76. speed_t tty_termios_input_baud_rate(const struct ktermios *termios)
  77. {
  78. unsigned int cbaud = (termios->c_cflag >> IBSHIFT) & CBAUD;
  79. if (cbaud == B0)
  80. return tty_termios_baud_rate(termios);
  81. /* Magic token for arbitrary speed via c_ispeed */
  82. if (cbaud == BOTHER)
  83. return termios->c_ispeed;
  84. if (cbaud & CBAUDEX) {
  85. cbaud &= ~CBAUDEX;
  86. cbaud += 15;
  87. }
  88. return cbaud >= n_baud_table ? 0 : baud_table[cbaud];
  89. }
  90. EXPORT_SYMBOL(tty_termios_input_baud_rate);
  91. /**
  92. * tty_termios_encode_baud_rate
  93. * @termios: ktermios structure holding user requested state
  94. * @ibaud: input speed
  95. * @obaud: output speed
  96. *
  97. * Encode the speeds set into the passed termios structure. This is
  98. * used as a library helper for drivers so that they can report back
  99. * the actual speed selected when it differs from the speed requested
  100. *
  101. * For maximal back compatibility with legacy SYS5/POSIX *nix behaviour
  102. * we need to carefully set the bits when the user does not get the
  103. * desired speed. We allow small margins and preserve as much of possible
  104. * of the input intent to keep compatibility.
  105. *
  106. * Locking: Caller should hold termios lock. This is already held
  107. * when calling this function from the driver termios handler.
  108. *
  109. * The ifdefs deal with platforms whose owners have yet to update them
  110. * and will all go away once this is done.
  111. */
  112. void tty_termios_encode_baud_rate(struct ktermios *termios,
  113. speed_t ibaud, speed_t obaud)
  114. {
  115. int i = 0;
  116. int ifound = -1, ofound = -1;
  117. int iclose = ibaud/50, oclose = obaud/50;
  118. int ibinput = 0;
  119. if (obaud == 0) /* CD dropped */
  120. ibaud = 0; /* Clear ibaud to be sure */
  121. termios->c_ispeed = ibaud;
  122. termios->c_ospeed = obaud;
  123. if (((termios->c_cflag >> IBSHIFT) & CBAUD) != B0)
  124. ibinput = 1; /* An input speed was specified */
  125. /* If the user asked for a precise weird speed give a precise weird
  126. * answer. If they asked for a Bfoo speed they may have problems
  127. * digesting non-exact replies so fuzz a bit.
  128. */
  129. if ((termios->c_cflag & CBAUD) == BOTHER) {
  130. oclose = 0;
  131. if (!ibinput)
  132. iclose = 0;
  133. }
  134. if (((termios->c_cflag >> IBSHIFT) & CBAUD) == BOTHER)
  135. iclose = 0;
  136. termios->c_cflag &= ~CBAUD;
  137. termios->c_cflag &= ~(CBAUD << IBSHIFT);
  138. /*
  139. * Our goal is to find a close match to the standard baud rate
  140. * returned. Walk the baud rate table and if we get a very close
  141. * match then report back the speed as a POSIX Bxxxx value by
  142. * preference
  143. */
  144. do {
  145. if (obaud - oclose <= baud_table[i] &&
  146. obaud + oclose >= baud_table[i]) {
  147. termios->c_cflag |= baud_bits[i];
  148. ofound = i;
  149. }
  150. if (ibaud - iclose <= baud_table[i] &&
  151. ibaud + iclose >= baud_table[i]) {
  152. /* For the case input == output don't set IBAUD bits
  153. * if the user didn't do so.
  154. */
  155. if (ofound == i && !ibinput) {
  156. ifound = i;
  157. } else {
  158. ifound = i;
  159. termios->c_cflag |= (baud_bits[i] << IBSHIFT);
  160. }
  161. }
  162. } while (++i < n_baud_table);
  163. /* If we found no match then use BOTHER. */
  164. if (ofound == -1)
  165. termios->c_cflag |= BOTHER;
  166. /* Set exact input bits only if the input and output differ or the
  167. * user already did.
  168. */
  169. if (ifound == -1 && (ibaud != obaud || ibinput))
  170. termios->c_cflag |= (BOTHER << IBSHIFT);
  171. }
  172. EXPORT_SYMBOL_GPL(tty_termios_encode_baud_rate);
  173. /**
  174. * tty_encode_baud_rate - set baud rate of the tty
  175. * @tty: terminal device
  176. * @ibaud: input baud rate
  177. * @obaud: output baud rate
  178. *
  179. * Update the current termios data for the tty with the new speed
  180. * settings. The caller must hold the termios_rwsem for the tty in
  181. * question.
  182. */
  183. void tty_encode_baud_rate(struct tty_struct *tty, speed_t ibaud, speed_t obaud)
  184. {
  185. tty_termios_encode_baud_rate(&tty->termios, ibaud, obaud);
  186. }
  187. EXPORT_SYMBOL_GPL(tty_encode_baud_rate);