Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 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 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 10 | #include "libbb.h" |
| 11 | |
Rob Landley | 57c1f73 | 2006-06-20 16:17:24 +0000 | [diff] [blame] | 12 | struct speed_map { |
Denys Vlasenko | 76787a7 | 2016-09-01 11:44:13 +0200 | [diff] [blame] | 13 | #if defined __FreeBSD__ \ |
| 14 | || (defined B115200 && B115200 > 0xffff) \ |
| 15 | || (defined B230400 && B230400 > 0xffff) \ |
| 16 | || (defined B460800 && B460800 > 0xffff) \ |
| 17 | || (defined B921600 && B921600 > 0xffff) \ |
| 18 | || (defined B1152000 && B1152000 > 0xffff) \ |
| 19 | || (defined B1000000 && B1000000 > 0xffff) \ |
| 20 | || (defined B2000000 && B2000000 > 0xffff) \ |
| 21 | || (defined B3000000 && B3000000 > 0xffff) \ |
| 22 | || (defined B4000000 && B4000000 > 0xffff) |
Denys Vlasenko | 96f92a1 | 2014-01-08 15:25:20 +0100 | [diff] [blame] | 23 | /* On FreeBSD, B<num> constants don't fit into a short */ |
| 24 | unsigned speed; |
| 25 | #else |
Rob Landley | 57c1f73 | 2006-06-20 16:17:24 +0000 | [diff] [blame] | 26 | unsigned short speed; |
Denys Vlasenko | 96f92a1 | 2014-01-08 15:25:20 +0100 | [diff] [blame] | 27 | #endif |
Rob Landley | 57c1f73 | 2006-06-20 16:17:24 +0000 | [diff] [blame] | 28 | unsigned short value; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 29 | }; |
| 30 | |
Denys Vlasenko | 76787a7 | 2016-09-01 11:44:13 +0200 | [diff] [blame] | 31 | /* On Linux, Bxx constants are 0..15 (up to B38400) and 0x1001..0x100f */ |
Rob Landley | 57c1f73 | 2006-06-20 16:17:24 +0000 | [diff] [blame] | 32 | static const struct speed_map speeds[] = { |
| 33 | {B0, 0}, |
| 34 | {B50, 50}, |
| 35 | {B75, 75}, |
| 36 | {B110, 110}, |
| 37 | {B134, 134}, |
| 38 | {B150, 150}, |
| 39 | {B200, 200}, |
| 40 | {B300, 300}, |
| 41 | {B600, 600}, |
| 42 | {B1200, 1200}, |
| 43 | {B1800, 1800}, |
| 44 | {B2400, 2400}, |
| 45 | {B4800, 4800}, |
| 46 | {B9600, 9600}, |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 47 | #ifdef B19200 |
Rob Landley | 57c1f73 | 2006-06-20 16:17:24 +0000 | [diff] [blame] | 48 | {B19200, 19200}, |
| 49 | #elif defined(EXTA) |
Denys Vlasenko | 76787a7 | 2016-09-01 11:44:13 +0200 | [diff] [blame] | 50 | {EXTA, 19200}, |
Rob Landley | 57c1f73 | 2006-06-20 16:17:24 +0000 | [diff] [blame] | 51 | #endif |
Denys Vlasenko | 76787a7 | 2016-09-01 11:44:13 +0200 | [diff] [blame] | 52 | /* 19200 = 0x4b00 */ |
Denys Vlasenko | 4537f83 | 2016-09-26 08:52:52 +0200 | [diff] [blame] | 53 | /* 38400 = 0x9600, this value would use bit#15 if not "/200" encoded: */ |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 54 | #ifdef B38400 |
Denys Vlasenko | 76787a7 | 2016-09-01 11:44:13 +0200 | [diff] [blame] | 55 | {B38400, 38400/200 + 0x8000u}, |
Rob Landley | 57c1f73 | 2006-06-20 16:17:24 +0000 | [diff] [blame] | 56 | #elif defined(EXTB) |
Denys Vlasenko | 76787a7 | 2016-09-01 11:44:13 +0200 | [diff] [blame] | 57 | {EXTB, 38400/200 + 0x8000u}, |
Rob Landley | 57c1f73 | 2006-06-20 16:17:24 +0000 | [diff] [blame] | 58 | #endif |
| 59 | #ifdef B57600 |
Denys Vlasenko | 76787a7 | 2016-09-01 11:44:13 +0200 | [diff] [blame] | 60 | {B57600, 57600/200 + 0x8000u}, |
Rob Landley | 57c1f73 | 2006-06-20 16:17:24 +0000 | [diff] [blame] | 61 | #endif |
| 62 | #ifdef B115200 |
Denys Vlasenko | 76787a7 | 2016-09-01 11:44:13 +0200 | [diff] [blame] | 63 | {B115200, 115200/200 + 0x8000u}, |
Rob Landley | 57c1f73 | 2006-06-20 16:17:24 +0000 | [diff] [blame] | 64 | #endif |
| 65 | #ifdef B230400 |
Denys Vlasenko | 76787a7 | 2016-09-01 11:44:13 +0200 | [diff] [blame] | 66 | {B230400, 230400/200 + 0x8000u}, |
Rob Landley | 57c1f73 | 2006-06-20 16:17:24 +0000 | [diff] [blame] | 67 | #endif |
| 68 | #ifdef B460800 |
Denys Vlasenko | 76787a7 | 2016-09-01 11:44:13 +0200 | [diff] [blame] | 69 | {B460800, 460800/200 + 0x8000u}, |
| 70 | #endif |
| 71 | #ifdef B576000 |
| 72 | {B576000, 576000/200 + 0x8000u}, |
Rob Landley | 57c1f73 | 2006-06-20 16:17:24 +0000 | [diff] [blame] | 73 | #endif |
Bernhard Reutner-Fischer | e707a30 | 2009-10-20 19:40:20 +0200 | [diff] [blame] | 74 | #ifdef B921600 |
Denys Vlasenko | 76787a7 | 2016-09-01 11:44:13 +0200 | [diff] [blame] | 75 | {B921600, 921600/200 + 0x8000u}, |
Bernhard Reutner-Fischer | e707a30 | 2009-10-20 19:40:20 +0200 | [diff] [blame] | 76 | #endif |
Denys Vlasenko | 76787a7 | 2016-09-01 11:44:13 +0200 | [diff] [blame] | 77 | #ifdef B1152000 |
| 78 | {B1152000, 1152000/200 + 0x8000u}, |
| 79 | #endif |
| 80 | |
| 81 | #ifdef B500000 |
| 82 | {B500000, 500000/200 + 0x8000u}, |
| 83 | #endif |
| 84 | #ifdef B1000000 |
| 85 | {B1000000, 1000000/200 + 0x8000u}, |
| 86 | #endif |
| 87 | #ifdef B1500000 |
| 88 | {B1500000, 1500000/200 + 0x8000u}, |
| 89 | #endif |
| 90 | #ifdef B2000000 |
| 91 | {B2000000, 2000000/200 + 0x8000u}, |
| 92 | #endif |
| 93 | #ifdef B2500000 |
| 94 | {B2500000, 2500000/200 + 0x8000u}, |
| 95 | #endif |
| 96 | #ifdef B3000000 |
| 97 | {B3000000, 3000000/200 + 0x8000u}, |
| 98 | #endif |
| 99 | #ifdef B3500000 |
| 100 | {B3500000, 3500000/200 + 0x8000u}, |
| 101 | #endif |
| 102 | #ifdef B4000000 |
| 103 | {B4000000, 4000000/200 + 0x8000u}, |
| 104 | #endif |
| 105 | /* 4000000/200 = 0x4e20, bit#15 still does not interfere with the value */ |
Denys Vlasenko | 525209a | 2016-09-26 14:37:12 +0200 | [diff] [blame] | 106 | /* (can use /800 if higher speeds would appear, /1600 won't work for B500000) */ |
Rob Landley | 57c1f73 | 2006-06-20 16:17:24 +0000 | [diff] [blame] | 107 | }; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 108 | |
Denys Vlasenko | 525209a | 2016-09-26 14:37:12 +0200 | [diff] [blame] | 109 | /* |
| 110 | * TODO: maybe we can just bite the bullet, ditch the table and use termios2 |
| 111 | * Linux API (supports arbitrary baud rates, no Bxxxx mess needed)? Example: |
| 112 | * |
| 113 | * #include <asm/termios.h> |
| 114 | * #include <asm/ioctls.h> |
| 115 | * struct termios2 t; |
| 116 | * ioctl(fd, TCGETS2, &t); |
| 117 | * t.c_ospeed = t.c_ispeed = 543210; |
| 118 | * t.c_cflag &= ~CBAUD; |
| 119 | * t.c_cflag |= BOTHER; |
| 120 | * ioctl(fd, TCSETS2, &t); |
| 121 | */ |
| 122 | |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 123 | enum { NUM_SPEEDS = ARRAY_SIZE(speeds) }; |
Rob Landley | 57c1f73 | 2006-06-20 16:17:24 +0000 | [diff] [blame] | 124 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 125 | unsigned FAST_FUNC tty_baud_to_value(speed_t speed) |
Rob Landley | 57c1f73 | 2006-06-20 16:17:24 +0000 | [diff] [blame] | 126 | { |
| 127 | int i = 0; |
| 128 | |
| 129 | do { |
| 130 | if (speed == speeds[i].speed) { |
Denys Vlasenko | 76787a7 | 2016-09-01 11:44:13 +0200 | [diff] [blame] | 131 | if (speeds[i].value & 0x8000u) { |
Denys Vlasenko | 525209a | 2016-09-26 14:37:12 +0200 | [diff] [blame] | 132 | return ((unsigned)(speeds[i].value) & 0x7fffU) * 200; |
Rob Landley | 57c1f73 | 2006-06-20 16:17:24 +0000 | [diff] [blame] | 133 | } |
| 134 | return speeds[i].value; |
| 135 | } |
| 136 | } while (++i < NUM_SPEEDS); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 141 | speed_t FAST_FUNC tty_value_to_baud(unsigned int value) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 142 | { |
Rob Landley | 57c1f73 | 2006-06-20 16:17:24 +0000 | [diff] [blame] | 143 | int i = 0; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 144 | |
Rob Landley | 57c1f73 | 2006-06-20 16:17:24 +0000 | [diff] [blame] | 145 | do { |
Peter Kjellerstedt | 5ab8f7d | 2006-06-20 16:35:37 +0000 | [diff] [blame] | 146 | if (value == tty_baud_to_value(speeds[i].speed)) { |
Rob Landley | 57c1f73 | 2006-06-20 16:17:24 +0000 | [diff] [blame] | 147 | return speeds[i].speed; |
| 148 | } |
| 149 | } while (++i < NUM_SPEEDS); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 150 | |
Rob Landley | 57c1f73 | 2006-06-20 16:17:24 +0000 | [diff] [blame] | 151 | return (speed_t) - 1; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 152 | } |
Rob Landley | 57c1f73 | 2006-06-20 16:17:24 +0000 | [diff] [blame] | 153 | |
| 154 | #if 0 |
| 155 | /* testing code */ |
| 156 | #include <stdio.h> |
| 157 | |
| 158 | int main(void) |
| 159 | { |
| 160 | unsigned long v; |
| 161 | speed_t s; |
| 162 | |
Bernhard Reutner-Fischer | e707a30 | 2009-10-20 19:40:20 +0200 | [diff] [blame] | 163 | for (v = 0 ; v < 1000000; v++) { |
Peter Kjellerstedt | 5ab8f7d | 2006-06-20 16:35:37 +0000 | [diff] [blame] | 164 | s = tty_value_to_baud(v); |
Rob Landley | 57c1f73 | 2006-06-20 16:17:24 +0000 | [diff] [blame] | 165 | if (s == (speed_t) -1) { |
| 166 | continue; |
| 167 | } |
| 168 | printf("v = %lu -- s = %0lo\n", v, (unsigned long) s); |
| 169 | } |
| 170 | |
| 171 | printf("-------------------------------\n"); |
| 172 | |
Denis Vlasenko | b71c668 | 2007-07-21 15:08:09 +0000 | [diff] [blame] | 173 | for (s = 0 ; s < 010017+1; s++) { |
Peter Kjellerstedt | 5ab8f7d | 2006-06-20 16:35:37 +0000 | [diff] [blame] | 174 | v = tty_baud_to_value(s); |
Rob Landley | 57c1f73 | 2006-06-20 16:17:24 +0000 | [diff] [blame] | 175 | if (!v) { |
| 176 | continue; |
| 177 | } |
| 178 | printf("v = %lu -- s = %0lo\n", v, (unsigned long) s); |
| 179 | } |
| 180 | |
| 181 | return 0; |
| 182 | } |
| 183 | #endif |