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 | 96f92a1 | 2014-01-08 15:25:20 +0100 | [diff] [blame] | 13 | #if defined __FreeBSD__ |
| 14 | /* On FreeBSD, B<num> constants don't fit into a short */ |
| 15 | unsigned speed; |
| 16 | #else |
Rob Landley | 57c1f73 | 2006-06-20 16:17:24 +0000 | [diff] [blame] | 17 | unsigned short speed; |
Denys Vlasenko | 96f92a1 | 2014-01-08 15:25:20 +0100 | [diff] [blame] | 18 | #endif |
Rob Landley | 57c1f73 | 2006-06-20 16:17:24 +0000 | [diff] [blame] | 19 | unsigned short value; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 20 | }; |
| 21 | |
Rob Landley | 57c1f73 | 2006-06-20 16:17:24 +0000 | [diff] [blame] | 22 | static const struct speed_map speeds[] = { |
| 23 | {B0, 0}, |
| 24 | {B50, 50}, |
| 25 | {B75, 75}, |
| 26 | {B110, 110}, |
| 27 | {B134, 134}, |
| 28 | {B150, 150}, |
| 29 | {B200, 200}, |
| 30 | {B300, 300}, |
| 31 | {B600, 600}, |
| 32 | {B1200, 1200}, |
| 33 | {B1800, 1800}, |
| 34 | {B2400, 2400}, |
| 35 | {B4800, 4800}, |
| 36 | {B9600, 9600}, |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 37 | #ifdef B19200 |
Rob Landley | 57c1f73 | 2006-06-20 16:17:24 +0000 | [diff] [blame] | 38 | {B19200, 19200}, |
| 39 | #elif defined(EXTA) |
| 40 | {EXTA, 19200}, |
| 41 | #endif |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 42 | #ifdef B38400 |
Rob Landley | 57c1f73 | 2006-06-20 16:17:24 +0000 | [diff] [blame] | 43 | {B38400, 38400/256 + 0x8000U}, |
| 44 | #elif defined(EXTB) |
| 45 | {EXTB, 38400/256 + 0x8000U}, |
| 46 | #endif |
| 47 | #ifdef B57600 |
| 48 | {B57600, 57600/256 + 0x8000U}, |
| 49 | #endif |
| 50 | #ifdef B115200 |
| 51 | {B115200, 115200/256 + 0x8000U}, |
| 52 | #endif |
| 53 | #ifdef B230400 |
| 54 | {B230400, 230400/256 + 0x8000U}, |
| 55 | #endif |
| 56 | #ifdef B460800 |
| 57 | {B460800, 460800/256 + 0x8000U}, |
| 58 | #endif |
Bernhard Reutner-Fischer | e707a30 | 2009-10-20 19:40:20 +0200 | [diff] [blame] | 59 | #ifdef B921600 |
| 60 | {B921600, 921600/256 + 0x8000U}, |
| 61 | #endif |
Rob Landley | 57c1f73 | 2006-06-20 16:17:24 +0000 | [diff] [blame] | 62 | }; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 63 | |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 64 | enum { NUM_SPEEDS = ARRAY_SIZE(speeds) }; |
Rob Landley | 57c1f73 | 2006-06-20 16:17:24 +0000 | [diff] [blame] | 65 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 66 | unsigned FAST_FUNC tty_baud_to_value(speed_t speed) |
Rob Landley | 57c1f73 | 2006-06-20 16:17:24 +0000 | [diff] [blame] | 67 | { |
| 68 | int i = 0; |
| 69 | |
| 70 | do { |
| 71 | if (speed == speeds[i].speed) { |
| 72 | if (speeds[i].value & 0x8000U) { |
| 73 | return ((unsigned long) (speeds[i].value) & 0x7fffU) * 256; |
| 74 | } |
| 75 | return speeds[i].value; |
| 76 | } |
| 77 | } while (++i < NUM_SPEEDS); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 82 | speed_t FAST_FUNC tty_value_to_baud(unsigned int value) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 83 | { |
Rob Landley | 57c1f73 | 2006-06-20 16:17:24 +0000 | [diff] [blame] | 84 | int i = 0; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 85 | |
Rob Landley | 57c1f73 | 2006-06-20 16:17:24 +0000 | [diff] [blame] | 86 | do { |
Peter Kjellerstedt | 5ab8f7d | 2006-06-20 16:35:37 +0000 | [diff] [blame] | 87 | if (value == tty_baud_to_value(speeds[i].speed)) { |
Rob Landley | 57c1f73 | 2006-06-20 16:17:24 +0000 | [diff] [blame] | 88 | return speeds[i].speed; |
| 89 | } |
| 90 | } while (++i < NUM_SPEEDS); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 91 | |
Rob Landley | 57c1f73 | 2006-06-20 16:17:24 +0000 | [diff] [blame] | 92 | return (speed_t) - 1; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 93 | } |
Rob Landley | 57c1f73 | 2006-06-20 16:17:24 +0000 | [diff] [blame] | 94 | |
| 95 | #if 0 |
| 96 | /* testing code */ |
| 97 | #include <stdio.h> |
| 98 | |
| 99 | int main(void) |
| 100 | { |
| 101 | unsigned long v; |
| 102 | speed_t s; |
| 103 | |
Bernhard Reutner-Fischer | e707a30 | 2009-10-20 19:40:20 +0200 | [diff] [blame] | 104 | for (v = 0 ; v < 1000000; v++) { |
Peter Kjellerstedt | 5ab8f7d | 2006-06-20 16:35:37 +0000 | [diff] [blame] | 105 | s = tty_value_to_baud(v); |
Rob Landley | 57c1f73 | 2006-06-20 16:17:24 +0000 | [diff] [blame] | 106 | if (s == (speed_t) -1) { |
| 107 | continue; |
| 108 | } |
| 109 | printf("v = %lu -- s = %0lo\n", v, (unsigned long) s); |
| 110 | } |
| 111 | |
| 112 | printf("-------------------------------\n"); |
| 113 | |
Denis Vlasenko | b71c668 | 2007-07-21 15:08:09 +0000 | [diff] [blame] | 114 | for (s = 0 ; s < 010017+1; s++) { |
Peter Kjellerstedt | 5ab8f7d | 2006-06-20 16:35:37 +0000 | [diff] [blame] | 115 | v = tty_baud_to_value(s); |
Rob Landley | 57c1f73 | 2006-06-20 16:17:24 +0000 | [diff] [blame] | 116 | if (!v) { |
| 117 | continue; |
| 118 | } |
| 119 | printf("v = %lu -- s = %0lo\n", v, (unsigned long) s); |
| 120 | } |
| 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | #endif |