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