tty_baudrate.c 6.4 KB

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