b4d576b4cfbc171b69c0749fc58b49dc1246d506
[oweals/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  * 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
23 #include <termios.h>
24 #include "libbb.h"
25
26 struct speed_map {
27         unsigned short speed;
28         unsigned short value;
29 };
30
31 static const struct speed_map speeds[] = {
32         {B0, 0},
33         {B50, 50},
34         {B75, 75},
35         {B110, 110},
36         {B134, 134},
37         {B150, 150},
38         {B200, 200},
39         {B300, 300},
40         {B600, 600},
41         {B1200, 1200},
42         {B1800, 1800},
43         {B2400, 2400},
44         {B4800, 4800},
45         {B9600, 9600},
46 #ifdef B19200
47         {B19200, 19200},
48 #elif defined(EXTA)
49         {EXTA, 19200},
50 #endif
51 #ifdef B38400
52         {B38400, 38400/256 + 0x8000U},
53 #elif defined(EXTB)
54         {EXTB, 38400/256 + 0x8000U},
55 #endif
56 #ifdef B57600
57         {B57600, 57600/256 + 0x8000U},
58 #endif
59 #ifdef B115200
60         {B115200, 115200/256 + 0x8000U},
61 #endif
62 #ifdef B230400
63         {B230400, 230400/256 + 0x8000U},
64 #endif
65 #ifdef B460800
66         {B460800, 460800/256 + 0x8000U},
67 #endif
68 #ifdef B500000
69         {B500000, 500000/256 + 0x8000U},
70 #endif
71 #ifdef B576000
72         {B576000, 576000/256 + 0x8000U},
73 #endif
74 #ifdef B921600
75         {B921600, 921600/256 + 0x8000U},
76 #endif
77 #ifdef B1000000
78         {B1000000, 1000000/256 + 0x8000U},
79 #endif
80 #ifdef B1152000
81         {B1152000, 1152000/256 + 0x8000U},
82 #endif
83 #ifdef B1500000
84         {B1500000, 1500000/256 + 0x8000U},
85 #endif
86 #ifdef B2000000
87         {B2000000, 2000000/256 + 0x8000U},
88 #endif
89 #ifdef B2500000
90         {B2500000, 2500000/256 + 0x8000U},
91 #endif
92 #ifdef B3000000
93         {B3000000, 3000000/256 + 0x8000U},
94 #endif
95 #ifdef B3500000
96         {B3500000, 3500000/256 + 0x8000U},
97 #endif
98 #ifdef B4000000
99         {B4000000, 4000000/256 + 0x8000U},
100 #endif
101 };
102
103 enum { NUM_SPEEDS = (sizeof(speeds) / sizeof(struct speed_map)) };
104
105 unsigned long bb_baud_to_value(speed_t speed)
106 {
107         int i = 0;
108
109         do {
110                 if (speed == speeds[i].speed) {
111                         if (speeds[i].value & 0x8000U) {
112                                 return ((unsigned long) (speeds[i].value) & 0x7fffU) * 256;
113                         }
114                         return speeds[i].value;
115                 }
116         } while (++i < NUM_SPEEDS);
117
118         return 0;
119 }
120
121 speed_t bb_value_to_baud(unsigned long value)
122 {
123         int i = 0;
124
125         do {
126                 if (value == bb_baud_to_value(speeds[i].speed)) {
127                         return speeds[i].speed;
128                 }
129         } while (++i < NUM_SPEEDS);
130
131         return (speed_t) - 1;
132 }
133
134 #if 0
135 /* testing code */
136 #include <stdio.h>
137
138 int main(void)
139 {
140         unsigned long v;
141         speed_t s;
142
143         for (v = 0 ; v < 500000 ; v++) {
144                 s = bb_value_to_baud(v);
145                 if (s == (speed_t) -1) {
146                         continue;
147                 }
148                 printf("v = %lu -- s = %0lo\n", v, (unsigned long) s);
149         }
150
151         printf("-------------------------------\n");
152
153         for (s = 0 ; s < 010017+1 ; s++) {
154                 v = bb_baud_to_value(s);
155                 if (!v) {
156                         continue;
157                 }
158                 printf("v = %lu -- s = %0lo\n", v, (unsigned long) s);
159         }
160
161         return 0;
162 }
163 #endif