speed_table.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. };
  52. enum { NUM_SPEEDS = ARRAY_SIZE(speeds) };
  53. unsigned FAST_FUNC tty_baud_to_value(speed_t speed)
  54. {
  55. int i = 0;
  56. do {
  57. if (speed == speeds[i].speed) {
  58. if (speeds[i].value & 0x8000U) {
  59. return ((unsigned long) (speeds[i].value) & 0x7fffU) * 256;
  60. }
  61. return speeds[i].value;
  62. }
  63. } while (++i < NUM_SPEEDS);
  64. return 0;
  65. }
  66. speed_t FAST_FUNC tty_value_to_baud(unsigned int value)
  67. {
  68. int i = 0;
  69. do {
  70. if (value == tty_baud_to_value(speeds[i].speed)) {
  71. return speeds[i].speed;
  72. }
  73. } while (++i < NUM_SPEEDS);
  74. return (speed_t) - 1;
  75. }
  76. #if 0
  77. /* testing code */
  78. #include <stdio.h>
  79. int main(void)
  80. {
  81. unsigned long v;
  82. speed_t s;
  83. for (v = 0 ; v < 500000; v++) {
  84. s = tty_value_to_baud(v);
  85. if (s == (speed_t) -1) {
  86. continue;
  87. }
  88. printf("v = %lu -- s = %0lo\n", v, (unsigned long) s);
  89. }
  90. printf("-------------------------------\n");
  91. for (s = 0 ; s < 010017+1; s++) {
  92. v = tty_baud_to_value(s);
  93. if (!v) {
  94. continue;
  95. }
  96. printf("v = %lu -- s = %0lo\n", v, (unsigned long) s);
  97. }
  98. return 0;
  99. }
  100. #endif