1 /* vi: set sw=4 ts=4: */
3 * compact speed_t <-> speed functions for busybox
5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
12 #if defined __FreeBSD__ \
13 || (defined B115200 && B115200 > 0xffff) \
14 || (defined B230400 && B230400 > 0xffff) \
15 || (defined B460800 && B460800 > 0xffff) \
16 || (defined B921600 && B921600 > 0xffff) \
17 || (defined B1152000 && B1152000 > 0xffff) \
18 || (defined B1000000 && B1000000 > 0xffff) \
19 || (defined B2000000 && B2000000 > 0xffff) \
20 || (defined B3000000 && B3000000 > 0xffff) \
21 || (defined B4000000 && B4000000 > 0xffff)
22 /* On FreeBSD, B<num> constants don't fit into a short */
30 /* On Linux, Bxx constants are 0..15 (up to B38400) and 0x1001..0x100f */
31 static const struct speed_map speeds[] = {
52 /* 38400 = 0x9600, this value would use bit#15 if not "/200" encoded: */
54 {B38400, 38400/200 + 0x8000u},
56 {EXTB, 38400/200 + 0x8000u},
59 {B57600, 57600/200 + 0x8000u},
62 {B115200, 115200/200 + 0x8000u},
65 {B230400, 230400/200 + 0x8000u},
68 {B460800, 460800/200 + 0x8000u},
71 {B576000, 576000/200 + 0x8000u},
74 {B921600, 921600/200 + 0x8000u},
77 {B1152000, 1152000/200 + 0x8000u},
81 {B500000, 500000/200 + 0x8000u},
84 {B1000000, 1000000/200 + 0x8000u},
87 {B1500000, 1500000/200 + 0x8000u},
90 {B2000000, 2000000/200 + 0x8000u},
93 {B2500000, 2500000/200 + 0x8000u},
96 {B3000000, 3000000/200 + 0x8000u},
99 {B3500000, 3500000/200 + 0x8000u},
102 {B4000000, 4000000/200 + 0x8000u},
104 /* 4000000/200 = 0x4e20, bit#15 still does not interfere with the value */
105 /* (can use /800 if higher speeds would appear, /1600 won't work for B500000) */
109 * TODO: maybe we can just bite the bullet, ditch the table and use termios2
110 * Linux API (supports arbitrary baud rates, no Bxxxx mess needed)? Example:
112 * #include <asm/termios.h>
113 * #include <asm/ioctls.h>
115 * ioctl(fd, TCGETS2, &t);
116 * t.c_ospeed = t.c_ispeed = 543210;
117 * t.c_cflag &= ~CBAUD;
118 * t.c_cflag |= BOTHER;
119 * ioctl(fd, TCSETS2, &t);
122 enum { NUM_SPEEDS = ARRAY_SIZE(speeds) };
124 unsigned FAST_FUNC tty_baud_to_value(speed_t speed)
129 if (speed == speeds[i].speed) {
130 if (speeds[i].value & 0x8000u) {
131 return ((unsigned)(speeds[i].value) & 0x7fffU) * 200;
133 return speeds[i].value;
135 } while (++i < NUM_SPEEDS);
140 speed_t FAST_FUNC tty_value_to_baud(unsigned int value)
145 if (value == tty_baud_to_value(speeds[i].speed)) {
146 return speeds[i].speed;
148 } while (++i < NUM_SPEEDS);
150 return (speed_t) - 1;
162 for (v = 0 ; v < 1000000; v++) {
163 s = tty_value_to_baud(v);
164 if (s == (speed_t) -1) {
167 printf("v = %lu -- s = %0lo\n", v, (unsigned long) s);
170 printf("-------------------------------\n");
172 for (s = 0 ; s < 010017+1; s++) {
173 v = tty_baud_to_value(s);
177 printf("v = %lu -- s = %0lo\n", v, (unsigned long) s);