blob: 174d531b2733411ef80ad8a05ba4ddaff4789b50 [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 */
9
Manuel Novoa III cad53642003-03-19 09:13:01 +000010#include "libbb.h"
11
Rob Landley57c1f732006-06-20 16:17:24 +000012struct speed_map {
Denys Vlasenko96f92a12014-01-08 15:25:20 +010013#if defined __FreeBSD__
14 /* On FreeBSD, B<num> constants don't fit into a short */
15 unsigned speed;
16#else
Rob Landley57c1f732006-06-20 16:17:24 +000017 unsigned short speed;
Denys Vlasenko96f92a12014-01-08 15:25:20 +010018#endif
Rob Landley57c1f732006-06-20 16:17:24 +000019 unsigned short value;
Manuel Novoa III cad53642003-03-19 09:13:01 +000020};
21
Rob Landley57c1f732006-06-20 16:17:24 +000022static 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 Vlasenkoe4dcba12010-10-28 18:57:19 +020037#ifdef B19200
Rob Landley57c1f732006-06-20 16:17:24 +000038 {B19200, 19200},
39#elif defined(EXTA)
40 {EXTA, 19200},
41#endif
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020042#ifdef B38400
Rob Landley57c1f732006-06-20 16:17:24 +000043 {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-Fischere707a302009-10-20 19:40:20 +020059#ifdef B921600
60 {B921600, 921600/256 + 0x8000U},
61#endif
Rob Landley57c1f732006-06-20 16:17:24 +000062};
Manuel Novoa III cad53642003-03-19 09:13:01 +000063
Denis Vlasenko80b8b392007-06-25 10:55:35 +000064enum { NUM_SPEEDS = ARRAY_SIZE(speeds) };
Rob Landley57c1f732006-06-20 16:17:24 +000065
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000066unsigned FAST_FUNC tty_baud_to_value(speed_t speed)
Rob Landley57c1f732006-06-20 16:17:24 +000067{
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 cad53642003-03-19 09:13:01 +000078
79 return 0;
80}
81
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000082speed_t FAST_FUNC tty_value_to_baud(unsigned int value)
Manuel Novoa III cad53642003-03-19 09:13:01 +000083{
Rob Landley57c1f732006-06-20 16:17:24 +000084 int i = 0;
Manuel Novoa III cad53642003-03-19 09:13:01 +000085
Rob Landley57c1f732006-06-20 16:17:24 +000086 do {
Peter Kjellerstedt5ab8f7d2006-06-20 16:35:37 +000087 if (value == tty_baud_to_value(speeds[i].speed)) {
Rob Landley57c1f732006-06-20 16:17:24 +000088 return speeds[i].speed;
89 }
90 } while (++i < NUM_SPEEDS);
Manuel Novoa III cad53642003-03-19 09:13:01 +000091
Rob Landley57c1f732006-06-20 16:17:24 +000092 return (speed_t) - 1;
Manuel Novoa III cad53642003-03-19 09:13:01 +000093}
Rob Landley57c1f732006-06-20 16:17:24 +000094
95#if 0
96/* testing code */
97#include <stdio.h>
98
99int main(void)
100{
101 unsigned long v;
102 speed_t s;
103
Bernhard Reutner-Fischere707a302009-10-20 19:40:20 +0200104 for (v = 0 ; v < 1000000; v++) {
Peter Kjellerstedt5ab8f7d2006-06-20 16:35:37 +0000105 s = tty_value_to_baud(v);
Rob Landley57c1f732006-06-20 16:17:24 +0000106 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 Vlasenkob71c6682007-07-21 15:08:09 +0000114 for (s = 0 ; s < 010017+1; s++) {
Peter Kjellerstedt5ab8f7d2006-06-20 16:35:37 +0000115 v = tty_baud_to_value(s);
Rob Landley57c1f732006-06-20 16:17:24 +0000116 if (!v) {
117 continue;
118 }
119 printf("v = %lu -- s = %0lo\n", v, (unsigned long) s);
120 }
121
122 return 0;
123}
124#endif