speed_table.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. /* On FreeBSD, B<num> constants don't fit into a short */
  13. unsigned speed;
  14. #else
  15. unsigned short speed;
  16. #endif
  17. unsigned short value;
  18. };
  19. static const struct speed_map speeds[] = {
  20. {B0, 0},
  21. {B50, 50},
  22. {B75, 75},
  23. {B110, 110},
  24. {B134, 134},
  25. {B150, 150},
  26. {B200, 200},
  27. {B300, 300},
  28. {B600, 600},
  29. {B1200, 1200},
  30. {B1800, 1800},
  31. {B2400, 2400},
  32. {B4800, 4800},
  33. {B9600, 9600},
  34. #ifdef B19200
  35. {B19200, 19200},
  36. #elif defined(EXTA)
  37. {EXTA, 19200},
  38. #endif
  39. #ifdef B38400
  40. {B38400, 38400/256 + 0x8000U},
  41. #elif defined(EXTB)
  42. {EXTB, 38400/256 + 0x8000U},
  43. #endif
  44. #ifdef B57600
  45. {B57600, 57600/256 + 0x8000U},
  46. #endif
  47. #ifdef B115200
  48. {B115200, 115200/256 + 0x8000U},
  49. #endif
  50. #ifdef B230400
  51. {B230400, 230400/256 + 0x8000U},
  52. #endif
  53. #ifdef B460800
  54. {B460800, 460800/256 + 0x8000U},
  55. #endif
  56. #ifdef B921600
  57. {B921600, 921600/256 + 0x8000U},
  58. #endif
  59. };
  60. enum { NUM_SPEEDS = ARRAY_SIZE(speeds) };
  61. unsigned FAST_FUNC tty_baud_to_value(speed_t speed)
  62. {
  63. int i = 0;
  64. do {
  65. if (speed == speeds[i].speed) {
  66. if (speeds[i].value & 0x8000U) {
  67. return ((unsigned long) (speeds[i].value) & 0x7fffU) * 256;
  68. }
  69. return speeds[i].value;
  70. }
  71. } while (++i < NUM_SPEEDS);
  72. return 0;
  73. }
  74. speed_t FAST_FUNC tty_value_to_baud(unsigned int value)
  75. {
  76. int i = 0;
  77. do {
  78. if (value == tty_baud_to_value(speeds[i].speed)) {
  79. return speeds[i].speed;
  80. }
  81. } while (++i < NUM_SPEEDS);
  82. return (speed_t) - 1;
  83. }
  84. #if 0
  85. /* testing code */
  86. #include <stdio.h>
  87. int main(void)
  88. {
  89. unsigned long v;
  90. speed_t s;
  91. for (v = 0 ; v < 1000000; v++) {
  92. s = tty_value_to_baud(v);
  93. if (s == (speed_t) -1) {
  94. continue;
  95. }
  96. printf("v = %lu -- s = %0lo\n", v, (unsigned long) s);
  97. }
  98. printf("-------------------------------\n");
  99. for (s = 0 ; s < 010017+1; s++) {
  100. v = tty_baud_to_value(s);
  101. if (!v) {
  102. continue;
  103. }
  104. printf("v = %lu -- s = %0lo\n", v, (unsigned long) s);
  105. }
  106. return 0;
  107. }
  108. #endif