OSDN Git Service

LT.old: Make __errno_location/__h_errno_location thread safe
[uclinux-h8/uClibc.git] / libc / termios / speed.c
1 /* `struct termios' speed frobnication functions.  Linux version.
2    Copyright (C) 1991,1992,1993,1995,1996,1997,1998,2000,2002,2003
3         Free Software Foundation, Inc.
4    This file is part of the GNU C Library.
5
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, see
18    <http://www.gnu.org/licenses/>.  */
19
20 #include <stddef.h>
21 #include <errno.h>
22 #include <termios.h>
23
24
25 /* This is a gross hack around a kernel bug.  If the cfsetispeed functions
26    is called with the SPEED argument set to zero this means use the same
27    speed as for output.  But we don't have independent input and output
28    speeds and therefore cannot record this.
29
30    We use an unused bit in the `c_iflag' field to keep track of this
31    use of `cfsetispeed'.  The value here must correspond to the one used
32    in `tcsetattr.c'.  */
33 #define IBAUD0  020000000000
34
35
36 /* Return the output baud rate stored in *TERMIOS_P.  */
37 speed_t cfgetospeed (const struct termios *termios_p)
38 {
39   return termios_p->c_cflag & (CBAUD | CBAUDEX);
40 }
41
42 /* Return the input baud rate stored in *TERMIOS_P.
43    Although for Linux there is no difference between input and output
44    speed, the numerical 0 is a special case for the input baud rate. It
45    should set the input baud rate to the output baud rate. */
46 speed_t cfgetispeed (const struct termios *termios_p)
47 {
48   return ((termios_p->c_iflag & IBAUD0)
49           ? 0 : termios_p->c_cflag & (CBAUD | CBAUDEX));
50 }
51
52 /* Set the output baud rate stored in *TERMIOS_P to SPEED.  */
53 int cfsetospeed (struct termios *termios_p, speed_t speed)
54 {
55   if ((speed & ~CBAUD) != 0
56       && (speed < B57600 || speed > __MAX_BAUD))
57     {
58       __set_errno (EINVAL);
59       return -1;
60     }
61
62   termios_p->c_cflag &= ~(CBAUD | CBAUDEX);
63   termios_p->c_cflag |= speed;
64
65   return 0;
66 }
67 libc_hidden_def (cfsetospeed)
68
69
70 /* Set the input baud rate stored in *TERMIOS_P to SPEED.
71    Although for Linux there is no difference between input and output
72    speed, the numerical 0 is a special case for the input baud rate.  It
73    should set the input baud rate to the output baud rate.  */
74 int cfsetispeed (struct termios *termios_p, speed_t speed)
75 {
76   if ((speed & ~CBAUD) != 0
77       && (speed < B57600 || speed > __MAX_BAUD))
78     {
79       __set_errno (EINVAL);
80       return -1;
81     }
82
83   if (speed == 0)
84     termios_p->c_iflag |= IBAUD0;
85   else
86     {
87       termios_p->c_iflag &= ~IBAUD0;
88       termios_p->c_cflag &= ~(CBAUD | CBAUDEX);
89       termios_p->c_cflag |= speed;
90     }
91
92   return 0;
93 }
94 libc_hidden_def (cfsetispeed)