blob: cf7101e645c98a8fbcd39193c404e8b487ba46fa [file] [log] [blame]
Manuel Novoa III cad53642003-03-19 09:13:01 +00001/* 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 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Manuel Novoa III cad53642003-03-19 09:13:01 +00008 */
Manuel Novoa III cad53642003-03-19 09:13:01 +00009#include "libbb.h"
10
Rob Landley57c1f732006-06-20 16:17:24 +000011struct speed_map {
Denys Vlasenko76787a72016-09-01 11:44:13 +020012#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)
Denys Vlasenko96f92a12014-01-08 15:25:20 +010022 /* On FreeBSD, B<num> constants don't fit into a short */
23 unsigned speed;
24#else
Rob Landley57c1f732006-06-20 16:17:24 +000025 unsigned short speed;
Denys Vlasenko96f92a12014-01-08 15:25:20 +010026#endif
Rob Landley57c1f732006-06-20 16:17:24 +000027 unsigned short value;
Manuel Novoa III cad53642003-03-19 09:13:01 +000028};
29
Denys Vlasenko76787a72016-09-01 11:44:13 +020030/* On Linux, Bxx constants are 0..15 (up to B38400) and 0x1001..0x100f */
Denys Vlasenko965b7952020-11-30 13:03:03 +010031static const struct speed_map speeds[] ALIGN4 = {
Rob Landley57c1f732006-06-20 16:17:24 +000032 {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},
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020046#ifdef B19200
Rob Landley57c1f732006-06-20 16:17:24 +000047 {B19200, 19200},
48#elif defined(EXTA)
Denys Vlasenko76787a72016-09-01 11:44:13 +020049 {EXTA, 19200},
Rob Landley57c1f732006-06-20 16:17:24 +000050#endif
Denys Vlasenko76787a72016-09-01 11:44:13 +020051/* 19200 = 0x4b00 */
Denys Vlasenko4537f832016-09-26 08:52:52 +020052/* 38400 = 0x9600, this value would use bit#15 if not "/200" encoded: */
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020053#ifdef B38400
Denys Vlasenko76787a72016-09-01 11:44:13 +020054 {B38400, 38400/200 + 0x8000u},
Rob Landley57c1f732006-06-20 16:17:24 +000055#elif defined(EXTB)
Denys Vlasenko76787a72016-09-01 11:44:13 +020056 {EXTB, 38400/200 + 0x8000u},
Rob Landley57c1f732006-06-20 16:17:24 +000057#endif
58#ifdef B57600
Denys Vlasenko76787a72016-09-01 11:44:13 +020059 {B57600, 57600/200 + 0x8000u},
Rob Landley57c1f732006-06-20 16:17:24 +000060#endif
61#ifdef B115200
Denys Vlasenko76787a72016-09-01 11:44:13 +020062 {B115200, 115200/200 + 0x8000u},
Rob Landley57c1f732006-06-20 16:17:24 +000063#endif
64#ifdef B230400
Denys Vlasenko76787a72016-09-01 11:44:13 +020065 {B230400, 230400/200 + 0x8000u},
Rob Landley57c1f732006-06-20 16:17:24 +000066#endif
67#ifdef B460800
Denys Vlasenko76787a72016-09-01 11:44:13 +020068 {B460800, 460800/200 + 0x8000u},
69#endif
70#ifdef B576000
71 {B576000, 576000/200 + 0x8000u},
Rob Landley57c1f732006-06-20 16:17:24 +000072#endif
Bernhard Reutner-Fischere707a302009-10-20 19:40:20 +020073#ifdef B921600
Denys Vlasenko76787a72016-09-01 11:44:13 +020074 {B921600, 921600/200 + 0x8000u},
Bernhard Reutner-Fischere707a302009-10-20 19:40:20 +020075#endif
Denys Vlasenko76787a72016-09-01 11:44:13 +020076#ifdef B1152000
77 {B1152000, 1152000/200 + 0x8000u},
78#endif
79
80#ifdef B500000
81 {B500000, 500000/200 + 0x8000u},
82#endif
83#ifdef B1000000
84 {B1000000, 1000000/200 + 0x8000u},
85#endif
86#ifdef B1500000
87 {B1500000, 1500000/200 + 0x8000u},
88#endif
89#ifdef B2000000
90 {B2000000, 2000000/200 + 0x8000u},
91#endif
92#ifdef B2500000
93 {B2500000, 2500000/200 + 0x8000u},
94#endif
95#ifdef B3000000
96 {B3000000, 3000000/200 + 0x8000u},
97#endif
98#ifdef B3500000
99 {B3500000, 3500000/200 + 0x8000u},
100#endif
101#ifdef B4000000
102 {B4000000, 4000000/200 + 0x8000u},
103#endif
104/* 4000000/200 = 0x4e20, bit#15 still does not interfere with the value */
Denys Vlasenko525209a2016-09-26 14:37:12 +0200105/* (can use /800 if higher speeds would appear, /1600 won't work for B500000) */
Rob Landley57c1f732006-06-20 16:17:24 +0000106};
Manuel Novoa III cad53642003-03-19 09:13:01 +0000107
Denys Vlasenko525209a2016-09-26 14:37:12 +0200108/*
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:
111 *
112 * #include <asm/termios.h>
113 * #include <asm/ioctls.h>
114 * struct termios2 t;
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);
120 */
121
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000122enum { NUM_SPEEDS = ARRAY_SIZE(speeds) };
Rob Landley57c1f732006-06-20 16:17:24 +0000123
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000124unsigned FAST_FUNC tty_baud_to_value(speed_t speed)
Rob Landley57c1f732006-06-20 16:17:24 +0000125{
126 int i = 0;
127
128 do {
129 if (speed == speeds[i].speed) {
Denys Vlasenko76787a72016-09-01 11:44:13 +0200130 if (speeds[i].value & 0x8000u) {
Denys Vlasenko525209a2016-09-26 14:37:12 +0200131 return ((unsigned)(speeds[i].value) & 0x7fffU) * 200;
Rob Landley57c1f732006-06-20 16:17:24 +0000132 }
133 return speeds[i].value;
134 }
135 } while (++i < NUM_SPEEDS);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000136
137 return 0;
138}
139
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000140speed_t FAST_FUNC tty_value_to_baud(unsigned int value)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000141{
Rob Landley57c1f732006-06-20 16:17:24 +0000142 int i = 0;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000143
Rob Landley57c1f732006-06-20 16:17:24 +0000144 do {
Peter Kjellerstedt5ab8f7d2006-06-20 16:35:37 +0000145 if (value == tty_baud_to_value(speeds[i].speed)) {
Rob Landley57c1f732006-06-20 16:17:24 +0000146 return speeds[i].speed;
147 }
148 } while (++i < NUM_SPEEDS);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000149
Rob Landley57c1f732006-06-20 16:17:24 +0000150 return (speed_t) - 1;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000151}
Rob Landley57c1f732006-06-20 16:17:24 +0000152
153#if 0
154/* testing code */
155#include <stdio.h>
156
157int main(void)
158{
159 unsigned long v;
160 speed_t s;
161
Bernhard Reutner-Fischere707a302009-10-20 19:40:20 +0200162 for (v = 0 ; v < 1000000; v++) {
Peter Kjellerstedt5ab8f7d2006-06-20 16:35:37 +0000163 s = tty_value_to_baud(v);
Rob Landley57c1f732006-06-20 16:17:24 +0000164 if (s == (speed_t) -1) {
165 continue;
166 }
167 printf("v = %lu -- s = %0lo\n", v, (unsigned long) s);
168 }
169
170 printf("-------------------------------\n");
171
Denis Vlasenkob71c6682007-07-21 15:08:09 +0000172 for (s = 0 ; s < 010017+1; s++) {
Peter Kjellerstedt5ab8f7d2006-06-20 16:35:37 +0000173 v = tty_baud_to_value(s);
Rob Landley57c1f732006-06-20 16:17:24 +0000174 if (!v) {
175 continue;
176 }
177 printf("v = %lu -- s = %0lo\n", v, (unsigned long) s);
178 }
179
180 return 0;
181}
182#endif