tty_baudrate.c 6.5 KB

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