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