lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 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, write to the Free |
| 18 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 19 | 02111-1307 USA. */ |
| 20 | |
| 21 | #include <stddef.h> |
| 22 | #include <errno.h> |
| 23 | #include <termios.h> |
| 24 | |
| 25 | |
| 26 | /* This is a gross hack around a kernel bug. If the cfsetispeed functions |
| 27 | is called with the SPEED argument set to zero this means use the same |
| 28 | speed as for output. But we don't have independent input and output |
| 29 | speeds and therefore cannot record this. |
| 30 | |
| 31 | We use an unused bit in the `c_iflag' field to keep track of this |
| 32 | use of `cfsetispeed'. The value here must correspond to the one used |
| 33 | in `tcsetattr.c'. */ |
| 34 | #define IBAUD0 020000000000 |
| 35 | |
| 36 | |
| 37 | /* Return the output baud rate stored in *TERMIOS_P. */ |
| 38 | speed_t cfgetospeed (const struct termios *termios_p) |
| 39 | { |
| 40 | return termios_p->c_cflag & (CBAUD | CBAUDEX); |
| 41 | } |
| 42 | |
| 43 | /* Return the input baud rate stored in *TERMIOS_P. |
| 44 | Although for Linux there is no difference between input and output |
| 45 | speed, the numerical 0 is a special case for the input baud rate. It |
| 46 | should set the input baud rate to the output baud rate. */ |
| 47 | speed_t cfgetispeed (const struct termios *termios_p) |
| 48 | { |
| 49 | return ((termios_p->c_iflag & IBAUD0) |
| 50 | ? 0 : termios_p->c_cflag & (CBAUD | CBAUDEX)); |
| 51 | } |
| 52 | |
| 53 | /* Set the output baud rate stored in *TERMIOS_P to SPEED. */ |
| 54 | int cfsetospeed (struct termios *termios_p, speed_t speed) |
| 55 | { |
| 56 | if ((speed & ~CBAUD) != 0 |
| 57 | && (speed < B57600 || speed > __MAX_BAUD)) |
| 58 | { |
| 59 | __set_errno (EINVAL); |
| 60 | return -1; |
| 61 | } |
| 62 | |
| 63 | termios_p->c_cflag &= ~(CBAUD | CBAUDEX); |
| 64 | termios_p->c_cflag |= speed; |
| 65 | |
| 66 | return 0; |
| 67 | } |
| 68 | libc_hidden_def (cfsetospeed) |
| 69 | |
| 70 | |
| 71 | /* Set the input baud rate stored in *TERMIOS_P to SPEED. |
| 72 | Although for Linux there is no difference between input and output |
| 73 | speed, the numerical 0 is a special case for the input baud rate. It |
| 74 | should set the input baud rate to the output baud rate. */ |
| 75 | int cfsetispeed (struct termios *termios_p, speed_t speed) |
| 76 | { |
| 77 | if ((speed & ~CBAUD) != 0 |
| 78 | && (speed < B57600 || speed > __MAX_BAUD)) |
| 79 | { |
| 80 | __set_errno (EINVAL); |
| 81 | return -1; |
| 82 | } |
| 83 | |
| 84 | if (speed == 0) |
| 85 | termios_p->c_iflag |= IBAUD0; |
| 86 | else |
| 87 | { |
| 88 | termios_p->c_iflag &= ~IBAUD0; |
| 89 | termios_p->c_cflag &= ~(CBAUD | CBAUDEX); |
| 90 | termios_p->c_cflag |= speed; |
| 91 | } |
| 92 | |
| 93 | return 0; |
| 94 | } |
| 95 | libc_hidden_def (cfsetispeed) |