baudrate.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or
  8. * without modification, are permitted (subject to the limitations
  9. * in the disclaimer below) provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer in the documentation and/or other materials
  18. * provided with the distribution.
  19. *
  20. * * Neither the name of Qualcomm Atheros nor the names of
  21. * its contributors may be used to endorse or promote products
  22. * derived from this software without specific prior written
  23. * permission.
  24. *
  25. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
  26. * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE
  27. * COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  28. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  29. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  30. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  31. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  33. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  34. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  36. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  37. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  38. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. *--------------------------------------------------------------------*/
  41. /*====================================================================*
  42. *
  43. * baud_t baudrate (unsigned rate)
  44. *
  45. * convert integer baud rate to system constant or bitmap;
  46. *
  47. *
  48. * Contributor(s):
  49. * Nathaniel Houghton
  50. * Charles Maier
  51. *
  52. *--------------------------------------------------------------------*/
  53. #ifndef BAUDRATE_SOURCE
  54. #define BAUDRATE_SOURCE
  55. #include <termios.h>
  56. #include <errno.h>
  57. #include "../tools/error.h"
  58. signed baudrate (unsigned baud, speed_t * speed)
  59. {
  60. static struct baud
  61. {
  62. unsigned baud;
  63. speed_t code;
  64. }
  65. bauds [] =
  66. {
  67. {
  68. 0,
  69. B0
  70. },
  71. {
  72. 50,
  73. B50
  74. },
  75. {
  76. 75,
  77. B75
  78. },
  79. {
  80. 110,
  81. B110
  82. },
  83. {
  84. 134,
  85. B134
  86. },
  87. {
  88. 150,
  89. B150
  90. },
  91. {
  92. 200,
  93. B200
  94. },
  95. {
  96. 300,
  97. B300
  98. },
  99. {
  100. 600,
  101. B600
  102. },
  103. {
  104. 1200,
  105. B1200
  106. },
  107. {
  108. 1800,
  109. B1800
  110. },
  111. {
  112. 2400,
  113. B2400
  114. },
  115. {
  116. 4800,
  117. B4800
  118. },
  119. {
  120. 9600,
  121. B9600
  122. },
  123. {
  124. 19200,
  125. B19200
  126. },
  127. {
  128. 38400,
  129. B38400
  130. },
  131. {
  132. 57600,
  133. B57600
  134. },
  135. {
  136. 115200,
  137. B115200
  138. },
  139. #ifdef B230400
  140. {
  141. 230400,
  142. B230400
  143. },
  144. #endif
  145. #ifdef B460800
  146. {
  147. 460800,
  148. B460800
  149. },
  150. #endif
  151. #ifdef B500000
  152. {
  153. 500000,
  154. B500000
  155. },
  156. #endif
  157. #ifdef B921600
  158. {
  159. 921600,
  160. B921600
  161. },
  162. #endif
  163. };
  164. signed lower = 0;
  165. signed upper = sizeof (bauds) / sizeof (struct baud);
  166. while (lower < upper)
  167. {
  168. signed index = (lower + upper) >> 1;
  169. signed order = baud - bauds [index].baud;
  170. if (order < 0)
  171. {
  172. upper = index - 0;
  173. continue;
  174. }
  175. if (order > 0)
  176. {
  177. lower = index + 1;
  178. continue;
  179. }
  180. *speed = bauds [index].code;
  181. return (0);
  182. }
  183. return (-1);
  184. }
  185. #endif