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