speed_table.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * compact speed_t <-> speed functions for busybox
  4. *
  5. * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. */
  9. #include "libbb.h"
  10. struct speed_map {
  11. #if defined __FreeBSD__ \
  12. || (defined B115200 && B115200 > 0xffff) \
  13. || (defined B230400 && B230400 > 0xffff) \
  14. || (defined B460800 && B460800 > 0xffff) \
  15. || (defined B921600 && B921600 > 0xffff) \
  16. || (defined B1152000 && B1152000 > 0xffff) \
  17. || (defined B1000000 && B1000000 > 0xffff) \
  18. || (defined B2000000 && B2000000 > 0xffff) \
  19. || (defined B3000000 && B3000000 > 0xffff) \
  20. || (defined B4000000 && B4000000 > 0xffff)
  21. /* On FreeBSD, B<num> constants don't fit into a short */
  22. unsigned speed;
  23. #else
  24. unsigned short speed;
  25. #endif
  26. unsigned short value;
  27. };
  28. /* On Linux, Bxx constants are 0..15 (up to B38400) and 0x1001..0x100f */
  29. static const struct speed_map speeds[] = {
  30. {B0, 0},
  31. {B50, 50},
  32. {B75, 75},
  33. {B110, 110},
  34. {B134, 134},
  35. {B150, 150},
  36. {B200, 200},
  37. {B300, 300},
  38. {B600, 600},
  39. {B1200, 1200},
  40. {B1800, 1800},
  41. {B2400, 2400},
  42. {B4800, 4800},
  43. {B9600, 9600},
  44. #ifdef B19200
  45. {B19200, 19200},
  46. #elif defined(EXTA)
  47. {EXTA, 19200},
  48. #endif
  49. /* 19200 = 0x4b00 */
  50. /* 38400 = 0x9600, this value would use bit#15 if not "/200" encoded: */
  51. #ifdef B38400
  52. {B38400, 38400/200 + 0x8000u},
  53. #elif defined(EXTB)
  54. {EXTB, 38400/200 + 0x8000u},
  55. #endif
  56. #ifdef B57600
  57. {B57600, 57600/200 + 0x8000u},
  58. #endif
  59. #ifdef B115200
  60. {B115200, 115200/200 + 0x8000u},
  61. #endif
  62. #ifdef B230400
  63. {B230400, 230400/200 + 0x8000u},
  64. #endif
  65. #ifdef B460800
  66. {B460800, 460800/200 + 0x8000u},
  67. #endif
  68. #ifdef B576000
  69. {B576000, 576000/200 + 0x8000u},
  70. #endif
  71. #ifdef B921600
  72. {B921600, 921600/200 + 0x8000u},
  73. #endif
  74. #ifdef B1152000
  75. {B1152000, 1152000/200 + 0x8000u},
  76. #endif
  77. #ifdef B500000
  78. {B500000, 500000/200 + 0x8000u},
  79. #endif
  80. #ifdef B1000000
  81. {B1000000, 1000000/200 + 0x8000u},
  82. #endif
  83. #ifdef B1500000
  84. {B1500000, 1500000/200 + 0x8000u},
  85. #endif
  86. #ifdef B2000000
  87. {B2000000, 2000000/200 + 0x8000u},
  88. #endif
  89. #ifdef B2500000
  90. {B2500000, 2500000/200 + 0x8000u},
  91. #endif
  92. #ifdef B3000000
  93. {B3000000, 3000000/200 + 0x8000u},
  94. #endif
  95. #ifdef B3500000
  96. {B3500000, 3500000/200 + 0x8000u},
  97. #endif
  98. #ifdef B4000000
  99. {B4000000, 4000000/200 + 0x8000u},
  100. #endif
  101. /* 4000000/200 = 0x4e20, bit#15 still does not interfere with the value */
  102. /* (can use /800 if higher speeds would appear, /1600 won't work for B500000) */
  103. };
  104. /*
  105. * TODO: maybe we can just bite the bullet, ditch the table and use termios2
  106. * Linux API (supports arbitrary baud rates, no Bxxxx mess needed)? Example:
  107. *
  108. * #include <asm/termios.h>
  109. * #include <asm/ioctls.h>
  110. * struct termios2 t;
  111. * ioctl(fd, TCGETS2, &t);
  112. * t.c_ospeed = t.c_ispeed = 543210;
  113. * t.c_cflag &= ~CBAUD;
  114. * t.c_cflag |= BOTHER;
  115. * ioctl(fd, TCSETS2, &t);
  116. */
  117. enum { NUM_SPEEDS = ARRAY_SIZE(speeds) };
  118. unsigned FAST_FUNC tty_baud_to_value(speed_t speed)
  119. {
  120. int i = 0;
  121. do {
  122. if (speed == speeds[i].speed) {
  123. if (speeds[i].value & 0x8000u) {
  124. return ((unsigned)(speeds[i].value) & 0x7fffU) * 200;
  125. }
  126. return speeds[i].value;
  127. }
  128. } while (++i < NUM_SPEEDS);
  129. return 0;
  130. }
  131. speed_t FAST_FUNC tty_value_to_baud(unsigned int value)
  132. {
  133. int i = 0;
  134. do {
  135. if (value == tty_baud_to_value(speeds[i].speed)) {
  136. return speeds[i].speed;
  137. }
  138. } while (++i < NUM_SPEEDS);
  139. return (speed_t) - 1;
  140. }
  141. #if 0
  142. /* testing code */
  143. #include <stdio.h>
  144. int main(void)
  145. {
  146. unsigned long v;
  147. speed_t s;
  148. for (v = 0 ; v < 1000000; v++) {
  149. s = tty_value_to_baud(v);
  150. if (s == (speed_t) -1) {
  151. continue;
  152. }
  153. printf("v = %lu -- s = %0lo\n", v, (unsigned long) s);
  154. }
  155. printf("-------------------------------\n");
  156. for (s = 0 ; s < 010017+1; s++) {
  157. v = tty_baud_to_value(s);
  158. if (!v) {
  159. continue;
  160. }
  161. printf("v = %lu -- s = %0lo\n", v, (unsigned long) s);
  162. }
  163. return 0;
  164. }
  165. #endif