tty_baudrate.c 6.3 KB

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