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 | 290fcb4 | 2006-06-18 23:59:03 +0000 | [diff] [blame^] | 12 | static const unsigned short speeds[] = { |
| 13 | 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, 9600, |
| 14 | 19200, 38400, 57600>>8, 115200>>8, 230400>>8, 460800>>8, 500000>>8, |
| 15 | 576000>>8, 921600>>8, 1000000>>8, 1152000>>8, 1500000>>8, 2000000>>8, |
| 16 | 3000000>>8, 3500000>>8, 4000000>>8 |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 17 | }; |
| 18 | |
Rob Landley | 290fcb4 | 2006-06-18 23:59:03 +0000 | [diff] [blame^] | 19 | unsigned int tty_baud_to_value(speed_t speed) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 20 | { |
Rob Landley | 290fcb4 | 2006-06-18 23:59:03 +0000 | [diff] [blame^] | 21 | int i; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 22 | |
Rob Landley | 290fcb4 | 2006-06-18 23:59:03 +0000 | [diff] [blame^] | 23 | for (i=0; i<sizeof(speeds) / sizeof(*speeds); i++) |
| 24 | if (speed == speeds[i] * (i>15 ? 256 : 1)) |
| 25 | return i>15 ? (i+4096-14) : i; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 26 | |
| 27 | return 0; |
| 28 | } |
| 29 | |
Rob Landley | 290fcb4 | 2006-06-18 23:59:03 +0000 | [diff] [blame^] | 30 | speed_t tty_value_to_baud(unsigned int value) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 31 | { |
Rob Landley | 290fcb4 | 2006-06-18 23:59:03 +0000 | [diff] [blame^] | 32 | int i; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 33 | |
Rob Landley | 290fcb4 | 2006-06-18 23:59:03 +0000 | [diff] [blame^] | 34 | for (i=0; i<sizeof(speeds) / sizeof(*speeds); i++) |
| 35 | if (value == (i>15 ? (i+4096-14) : i)) |
| 36 | return speeds[i] * (i>15 ? 256 : 1); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 37 | |
Rob Landley | 290fcb4 | 2006-06-18 23:59:03 +0000 | [diff] [blame^] | 38 | return -1; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 39 | } |