speed_table.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. #include <termios.h>
  23. #include "libbb.h"
  24. struct speed_map {
  25. unsigned short speed;
  26. unsigned short value;
  27. };
  28. static const struct speed_map speeds[] = {
  29. {B0, 0},
  30. {B50, 50},
  31. {B75, 75},
  32. {B110, 110},
  33. {B134, 134},
  34. {B150, 150},
  35. {B200, 200},
  36. {B300, 300},
  37. {B600, 600},
  38. {B1200, 1200},
  39. {B1800, 1800},
  40. {B2400, 2400},
  41. {B4800, 4800},
  42. {B9600, 9600},
  43. #ifdef B19200
  44. {B19200, 19200},
  45. #elif defined(EXTA)
  46. {EXTA, 19200},
  47. #endif
  48. #ifdef B38400
  49. {B38400, 38400/256 + 0x8000U},
  50. #elif defined(EXTB)
  51. {EXTB, 38400/256 + 0x8000U},
  52. #endif
  53. #ifdef B57600
  54. {B57600, 57600/256 + 0x8000U},
  55. #endif
  56. #ifdef B115200
  57. {B115200, 115200/256 + 0x8000U},
  58. #endif
  59. #ifdef B230400
  60. {B230400, 230400/256 + 0x8000U},
  61. #endif
  62. #ifdef B460800
  63. {B460800, 460800/256 + 0x8000U},
  64. #endif
  65. };
  66. static const int NUM_SPEEDS = (sizeof(speeds) / sizeof(struct speed_map));
  67. unsigned long bb_baud_to_value(speed_t speed)
  68. {
  69. int i = 0;
  70. do {
  71. if (speed == speeds[i].speed) {
  72. if (speeds[i].value & 0x8000U) {
  73. return ((unsigned long) (speeds[i].value) & 0x7fffU) * 256;
  74. }
  75. return speeds[i].value;
  76. }
  77. } while (++i < NUM_SPEEDS);
  78. return 0;
  79. }
  80. speed_t bb_value_to_baud(unsigned long value)
  81. {
  82. int i = 0;
  83. do {
  84. if (value == bb_baud_to_value(speeds[i].speed)) {
  85. return speeds[i].speed;
  86. }
  87. } while (++i < NUM_SPEEDS);
  88. return (speed_t) - 1;
  89. }
  90. #if 0
  91. /* testing code */
  92. #include <stdio.h>
  93. int main(void)
  94. {
  95. unsigned long v;
  96. speed_t s;
  97. for (v = 0 ; v < 500000 ; v++) {
  98. s = bb_value_to_baud(v);
  99. if (s == (speed_t) -1) {
  100. continue;
  101. }
  102. printf("v = %lu -- s = %0lo\n", v, (unsigned long) s);
  103. }
  104. printf("-------------------------------\n");
  105. for (s = 0 ; s < 010017+1 ; s++) {
  106. v = bb_baud_to_value(s);
  107. if (!v) {
  108. continue;
  109. }
  110. printf("v = %lu -- s = %0lo\n", v, (unsigned long) s);
  111. }
  112. return 0;
  113. }
  114. #endif