speed_table.c 2.0 KB

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