tty_baudrate.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 cbaud >= n_baud_table ? 0 : 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. #ifdef BOTHER
  91. /* Magic token for arbitrary speed via c_ispeed*/
  92. if (cbaud == BOTHER)
  93. return termios->c_ispeed;
  94. #endif
  95. if (cbaud & CBAUDEX) {
  96. cbaud &= ~CBAUDEX;
  97. if (cbaud < 1 || cbaud + 15 > n_baud_table)
  98. termios->c_cflag &= ~(CBAUDEX << IBSHIFT);
  99. else
  100. cbaud += 15;
  101. }
  102. return cbaud >= n_baud_table ? 0 : baud_table[cbaud];
  103. #else /* IBSHIFT */
  104. return tty_termios_baud_rate(termios);
  105. #endif /* IBSHIFT */
  106. }
  107. EXPORT_SYMBOL(tty_termios_input_baud_rate);
  108. /**
  109. * tty_termios_encode_baud_rate
  110. * @termios: ktermios structure holding user requested state
  111. * @ispeed: input speed
  112. * @ospeed: output speed
  113. *
  114. * Encode the speeds set into the passed termios structure. This is
  115. * used as a library helper for drivers so that they can report back
  116. * the actual speed selected when it differs from the speed requested
  117. *
  118. * For maximal back compatibility with legacy SYS5/POSIX *nix behaviour
  119. * we need to carefully set the bits when the user does not get the
  120. * desired speed. We allow small margins and preserve as much of possible
  121. * of the input intent to keep compatibility.
  122. *
  123. * Locking: Caller should hold termios lock. This is already held
  124. * when calling this function from the driver termios handler.
  125. *
  126. * The ifdefs deal with platforms whose owners have yet to update them
  127. * and will all go away once this is done.
  128. */
  129. void tty_termios_encode_baud_rate(struct ktermios *termios,
  130. speed_t ibaud, speed_t obaud)
  131. {
  132. int i = 0;
  133. int ifound = -1, ofound = -1;
  134. int iclose = ibaud/50, oclose = obaud/50;
  135. int ibinput = 0;
  136. if (obaud == 0) /* CD dropped */
  137. ibaud = 0; /* Clear ibaud to be sure */
  138. termios->c_ispeed = ibaud;
  139. termios->c_ospeed = obaud;
  140. #ifdef IBSHIFT
  141. if ((termios->c_cflag >> IBSHIFT) & CBAUD)
  142. ibinput = 1; /* An input speed was specified */
  143. #endif
  144. #ifdef BOTHER
  145. /* If the user asked for a precise weird speed give a precise weird
  146. answer. If they asked for a Bfoo speed they may have problems
  147. digesting non-exact replies so fuzz a bit */
  148. if ((termios->c_cflag & CBAUD) == BOTHER) {
  149. oclose = 0;
  150. if (!ibinput)
  151. iclose = 0;
  152. }
  153. if (((termios->c_cflag >> IBSHIFT) & CBAUD) == BOTHER)
  154. iclose = 0;
  155. #endif
  156. termios->c_cflag &= ~CBAUD;
  157. #ifdef IBSHIFT
  158. termios->c_cflag &= ~(CBAUD << IBSHIFT);
  159. #endif
  160. /*
  161. * Our goal is to find a close match to the standard baud rate
  162. * returned. Walk the baud rate table and if we get a very close
  163. * match then report back the speed as a POSIX Bxxxx value by
  164. * preference
  165. */
  166. do {
  167. if (obaud - oclose <= baud_table[i] &&
  168. obaud + oclose >= baud_table[i]) {
  169. termios->c_cflag |= baud_bits[i];
  170. ofound = i;
  171. }
  172. if (ibaud - iclose <= baud_table[i] &&
  173. ibaud + iclose >= baud_table[i]) {
  174. /* For the case input == output don't set IBAUD bits
  175. if the user didn't do so */
  176. if (ofound == i && !ibinput)
  177. ifound = i;
  178. #ifdef IBSHIFT
  179. else {
  180. ifound = i;
  181. termios->c_cflag |= (baud_bits[i] << IBSHIFT);
  182. }
  183. #endif
  184. }
  185. } while (++i < n_baud_table);
  186. /*
  187. * If we found no match then use BOTHER if provided or warn
  188. * the user their platform maintainer needs to wake up if not.
  189. */
  190. #ifdef BOTHER
  191. if (ofound == -1)
  192. termios->c_cflag |= BOTHER;
  193. /* Set exact input bits only if the input and output differ or the
  194. user already did */
  195. if (ifound == -1 && (ibaud != obaud || ibinput))
  196. termios->c_cflag |= (BOTHER << IBSHIFT);
  197. #else
  198. if (ifound == -1 || ofound == -1)
  199. pr_warn_once("tty: Unable to return correct speed data as your architecture needs updating.\n");
  200. #endif
  201. }
  202. EXPORT_SYMBOL_GPL(tty_termios_encode_baud_rate);
  203. /**
  204. * tty_encode_baud_rate - set baud rate of the tty
  205. * @ibaud: input baud rate
  206. * @obad: output baud rate
  207. *
  208. * Update the current termios data for the tty with the new speed
  209. * settings. The caller must hold the termios_rwsem for the tty in
  210. * question.
  211. */
  212. void tty_encode_baud_rate(struct tty_struct *tty, speed_t ibaud, speed_t obaud)
  213. {
  214. tty_termios_encode_baud_rate(&tty->termios, ibaud, obaud);
  215. }
  216. EXPORT_SYMBOL_GPL(tty_encode_baud_rate);