[v4] ioctl_tty.2: Add example how to get or set baudrate on the serial port

Message ID 20210810194928.16408-1-pali@kernel.org
State Rejected
Headers
Series [v4] ioctl_tty.2: Add example how to get or set baudrate on the serial port |

Checks

Context Check Description
dj/TryBot-apply_patch fail Patch failed to apply to master at the time it was sent
dj/TryBot-32bit fail Patch series failed to apply

Commit Message

Pali Rohár Aug. 10, 2021, 7:49 p.m. UTC
  Setting custom baudrate for which is not defined Bnnn constant is possible
via BOTHER flag and then filling speed in c_ospeed and c_ispeed fields.

These two fields are either in struct termios or struct termios2. Former
belongs to TCGETS/TCSETS ioctls, latter to TCGETS2/TCSETS2 ioctls.

BOTHER flag with these two fields and new struct termios2 is not supported
by older versions of include header files.

Some architectures (e.g. amd64) provide both struct termios and struct
termios2, but c_ospeed and c_ispeed are only in struct termios2.

Some other architectures (e.g. alpha) provide both struct termios and struct
termios2 and both have c_ospeed and c_ispeed fields.

And some other architectures (e.g. powerpc) provide only struct termios
(no struct termios2) and it has c_ospeed and c_ispeed fields.

So basically to support all architectures it is needed to use
struct termios2 when TCGETS2/TCSETS2 is supported. Otherwise it is needed
to use struct termios with TCGETS/TCSETS (case for e.g. powerpc).

Setting input baudrate is done via IBSHIFT macro.

Signed-off-by: Pali Rohár <pali@kernel.org>

---
Changes in v4:
* Add SPDX-License-Identifier
* Correctly process split baudrates (separate output and input) via IBSHIFT
* Update commit message

Changes in v3:
* Check support for custom baudrate only based on BOTHER macro
* Use TCGETS/TCSETS/termios when TCGETS2/TCSETS2/termios2 is not available

Changes in v2:
* Use \e for backslash
* Use exit(EXIT_*) instead of return num
* Sort includes
* Add comment about possible fallback
---
 man2/ioctl_tty.2 | 100 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 100 insertions(+)
  

Comments

Pali Rohár Aug. 10, 2021, 8:11 p.m. UTC | #1
On Tuesday 10 August 2021 21:49:28 Pali Rohár wrote:
> Setting custom baudrate for which is not defined Bnnn constant is possible
> via BOTHER flag and then filling speed in c_ospeed and c_ispeed fields.
> 
> These two fields are either in struct termios or struct termios2. Former
> belongs to TCGETS/TCSETS ioctls, latter to TCGETS2/TCSETS2 ioctls.
> 
> BOTHER flag with these two fields and new struct termios2 is not supported
> by older versions of include header files.
> 
> Some architectures (e.g. amd64) provide both struct termios and struct
> termios2, but c_ospeed and c_ispeed are only in struct termios2.
> 
> Some other architectures (e.g. alpha) provide both struct termios and struct
> termios2 and both have c_ospeed and c_ispeed fields.
> 
> And some other architectures (e.g. powerpc) provide only struct termios
> (no struct termios2) and it has c_ospeed and c_ispeed fields.
> 
> So basically to support all architectures it is needed to use
> struct termios2 when TCGETS2/TCSETS2 is supported. Otherwise it is needed
> to use struct termios with TCGETS/TCSETS (case for e.g. powerpc).
> 
> Setting input baudrate is done via IBSHIFT macro.
> 
> Signed-off-by: Pali Rohár <pali@kernel.org>
> 
> ---
> Changes in v4:
> * Add SPDX-License-Identifier
> * Correctly process split baudrates (separate output and input) via IBSHIFT
> * Update commit message
> 
> Changes in v3:
> * Check support for custom baudrate only based on BOTHER macro
> * Use TCGETS/TCSETS/termios when TCGETS2/TCSETS2/termios2 is not available
> 
> Changes in v2:
> * Use \e for backslash
> * Use exit(EXIT_*) instead of return num
> * Sort includes
> * Add comment about possible fallback
> ---
>  man2/ioctl_tty.2 | 100 +++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 100 insertions(+)
> 
> diff --git a/man2/ioctl_tty.2 b/man2/ioctl_tty.2
> index abfdc1e21fe9..7b2b03ff6757 100644
> --- a/man2/ioctl_tty.2
> +++ b/man2/ioctl_tty.2
> @@ -796,6 +796,106 @@ main(void)
>      close(fd);
>  }
>  .EE
> +.PP
> +Get or set arbitrary baudrate on the serial port.
> +.PP
> +.EX
> +/* SPDX-License-Identifier: GPL-2.0-or-later */
> +
> +#include <asm/termbits.h>
> +#include <fcntl.h>
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <sys/ioctl.h>
> +#include <sys/types.h>
> +#include <unistd.h>
> +
> +int
> +main(int argc, char *argv[])
> +{
> +#ifndef BOTHER
> +    fprintf(stderr, "BOTHER is unsupported\en");
> +    /* Program may fallback to TCGETS/TCSETS with Bnnn constants */
> +    exit(EXIT_FAILURE);
> +#else
> +    /* Declare tio structure, its type depends on supported ioctl */
> +#ifdef TCGETS2
> +    struct termios2 tio;
> +#else
> +    struct termios tio;
> +#endif
> +    int fd, rc;
> +
> +    if (argc != 2 && argc != 3 && argc != 4) {
> +        fprintf(stderr, "Usage: %s device [output [input] ]\en", argv[0]);
> +        exit(EXIT_FAILURE);
> +    }
> +
> +    fd = open(argv[1], O_RDWR | O_NONBLOCK | O_NOCTTY);
> +    if (fd < 0) {
> +        perror("open");
> +        exit(EXIT_FAILURE);
> +    }
> +
> +    /* Get the current serial port settings via supported ioctl */
> +#ifdef TCGETS2
> +    rc = ioctl(fd, TCGETS2, &tio);
> +#else
> +    rc = ioctl(fd, TCGETS, &tio);
> +#endif
> +    if (rc) {
> +        perror("TCGETS");
> +        close(fd);
> +        exit(EXIT_FAILURE);
> +    }
> +
> +    /* Change baud rate when more arguments were provided */
> +    if (argc == 3 || argc == 4) {
> +        /* Clear the current output baud rate and fill a new value */
> +        tio.c_cflag &= ~CBAUD;
> +        tio.c_cflag |= BOTHER;
> +        tio.c_ospeed = atoi(argv[2]);
> +
> +        /* Clear the current input baud rate and fill a new value */
> +        tio.c_cflag &= ~(CBAUD << IBSHIFT);
> +        tio.c_cflag |= BOTHER << IBSHIFT;
> +        /* When 4th argument is not provided reuse output baud rate */
> +        tio.c_ispeed = (argc == 4) ? atoi(argv[3]) : atoi(argv[2]);
> +
> +        /* Set new serial port settings via supported ioctl */
> +#ifdef TCSETS2
> +        rc = ioctl(fd, TCSETS2, &tio);
> +#else
> +        rc = ioctl(fd, TCSETS, &tio);
> +#endif
> +        if (rc) {
> +            perror("TCSETS");
> +            close(fd);
> +            exit(EXIT_FAILURE);
> +        }
> +
> +        /* And get new values which were really configured */
> +#ifdef TCGETS2
> +        rc = ioctl(fd, TCGETS2, &tio);
> +#else
> +        rc = ioctl(fd, TCGETS, &tio);
> +#endif
> +        if (rc) {
> +            perror("TCGETS");
> +            close(fd);
> +            exit(EXIT_FAILURE);
> +        }
> +    }
> +
> +    close(fd);
> +
> +    printf("output baud rate: %u\en", tio.c_ospeed);
> +    printf("input baud rate: %u\en", tio.c_ispeed);
> +
> +    exit(EXIT_SUCCESS);
> +#endif
> +}
> +.EE
>  .SH SEE ALSO
>  .BR ldattach (1),
>  .BR ioctl (2),
> -- 
> 2.20.1
> 

Alejandro, in case you are interested I wrote also longer version which
can be compiled also with header files without BOTHER support.

I found out that because glibc does not support setting BOTHER speeds,
it also is not able to read current speed (via cfgetospeed()) if serial
port is configured to BOTHER state. And because lot of applications,
including GNU stty, use glibc's cfgetospeed() they are not able to parse
tty serial ports when BOTHER is set.

So due current glibc version, it is preferred to use Bnnn constants and
use BOTHER with c_ospeed only in case when there is no corresponding
Bnnn constant.

In this longer version, this preference is implemented.

But because this longer version is _longer_ I do not know if it is a
good idea to have it in manpage, even it is more preferred for
interoperability with existing applications.

Greg, if you have time, I would like to ask for reviewing new version
of manpage patch (or longer version). Or if you have an idea what is
better example in manpage (short or that longer version).

Longer version is below as MIME inline part of email.
  
Pali Rohár Aug. 31, 2021, 8:34 p.m. UTC | #2
On Tuesday 10 August 2021 21:49:28 Pali Rohár wrote:
> Setting custom baudrate for which is not defined Bnnn constant is possible
> via BOTHER flag and then filling speed in c_ospeed and c_ispeed fields.
> 
> These two fields are either in struct termios or struct termios2. Former
> belongs to TCGETS/TCSETS ioctls, latter to TCGETS2/TCSETS2 ioctls.
> 
> BOTHER flag with these two fields and new struct termios2 is not supported
> by older versions of include header files.
> 
> Some architectures (e.g. amd64) provide both struct termios and struct
> termios2, but c_ospeed and c_ispeed are only in struct termios2.
> 
> Some other architectures (e.g. alpha) provide both struct termios and struct
> termios2 and both have c_ospeed and c_ispeed fields.
> 
> And some other architectures (e.g. powerpc) provide only struct termios
> (no struct termios2) and it has c_ospeed and c_ispeed fields.
> 
> So basically to support all architectures it is needed to use
> struct termios2 when TCGETS2/TCSETS2 is supported. Otherwise it is needed
> to use struct termios with TCGETS/TCSETS (case for e.g. powerpc).
> 
> Setting input baudrate is done via IBSHIFT macro.
> 
> Signed-off-by: Pali Rohár <pali@kernel.org>

Hello! Do you have any comments on this patch?

> ---
> Changes in v4:
> * Add SPDX-License-Identifier
> * Correctly process split baudrates (separate output and input) via IBSHIFT
> * Update commit message
> 
> Changes in v3:
> * Check support for custom baudrate only based on BOTHER macro
> * Use TCGETS/TCSETS/termios when TCGETS2/TCSETS2/termios2 is not available
> 
> Changes in v2:
> * Use \e for backslash
> * Use exit(EXIT_*) instead of return num
> * Sort includes
> * Add comment about possible fallback
> ---
>  man2/ioctl_tty.2 | 100 +++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 100 insertions(+)
> 
> diff --git a/man2/ioctl_tty.2 b/man2/ioctl_tty.2
> index abfdc1e21fe9..7b2b03ff6757 100644
> --- a/man2/ioctl_tty.2
> +++ b/man2/ioctl_tty.2
> @@ -796,6 +796,106 @@ main(void)
>      close(fd);
>  }
>  .EE
> +.PP
> +Get or set arbitrary baudrate on the serial port.
> +.PP
> +.EX
> +/* SPDX-License-Identifier: GPL-2.0-or-later */
> +
> +#include <asm/termbits.h>
> +#include <fcntl.h>
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <sys/ioctl.h>
> +#include <sys/types.h>
> +#include <unistd.h>
> +
> +int
> +main(int argc, char *argv[])
> +{
> +#ifndef BOTHER
> +    fprintf(stderr, "BOTHER is unsupported\en");
> +    /* Program may fallback to TCGETS/TCSETS with Bnnn constants */
> +    exit(EXIT_FAILURE);
> +#else
> +    /* Declare tio structure, its type depends on supported ioctl */
> +#ifdef TCGETS2
> +    struct termios2 tio;
> +#else
> +    struct termios tio;
> +#endif
> +    int fd, rc;
> +
> +    if (argc != 2 && argc != 3 && argc != 4) {
> +        fprintf(stderr, "Usage: %s device [output [input] ]\en", argv[0]);
> +        exit(EXIT_FAILURE);
> +    }
> +
> +    fd = open(argv[1], O_RDWR | O_NONBLOCK | O_NOCTTY);
> +    if (fd < 0) {
> +        perror("open");
> +        exit(EXIT_FAILURE);
> +    }
> +
> +    /* Get the current serial port settings via supported ioctl */
> +#ifdef TCGETS2
> +    rc = ioctl(fd, TCGETS2, &tio);
> +#else
> +    rc = ioctl(fd, TCGETS, &tio);
> +#endif
> +    if (rc) {
> +        perror("TCGETS");
> +        close(fd);
> +        exit(EXIT_FAILURE);
> +    }
> +
> +    /* Change baud rate when more arguments were provided */
> +    if (argc == 3 || argc == 4) {
> +        /* Clear the current output baud rate and fill a new value */
> +        tio.c_cflag &= ~CBAUD;
> +        tio.c_cflag |= BOTHER;
> +        tio.c_ospeed = atoi(argv[2]);
> +
> +        /* Clear the current input baud rate and fill a new value */
> +        tio.c_cflag &= ~(CBAUD << IBSHIFT);
> +        tio.c_cflag |= BOTHER << IBSHIFT;
> +        /* When 4th argument is not provided reuse output baud rate */
> +        tio.c_ispeed = (argc == 4) ? atoi(argv[3]) : atoi(argv[2]);
> +
> +        /* Set new serial port settings via supported ioctl */
> +#ifdef TCSETS2
> +        rc = ioctl(fd, TCSETS2, &tio);
> +#else
> +        rc = ioctl(fd, TCSETS, &tio);
> +#endif
> +        if (rc) {
> +            perror("TCSETS");
> +            close(fd);
> +            exit(EXIT_FAILURE);
> +        }
> +
> +        /* And get new values which were really configured */
> +#ifdef TCGETS2
> +        rc = ioctl(fd, TCGETS2, &tio);
> +#else
> +        rc = ioctl(fd, TCGETS, &tio);
> +#endif
> +        if (rc) {
> +            perror("TCGETS");
> +            close(fd);
> +            exit(EXIT_FAILURE);
> +        }
> +    }
> +
> +    close(fd);
> +
> +    printf("output baud rate: %u\en", tio.c_ospeed);
> +    printf("input baud rate: %u\en", tio.c_ispeed);
> +
> +    exit(EXIT_SUCCESS);
> +#endif
> +}
> +.EE
>  .SH SEE ALSO
>  .BR ldattach (1),
>  .BR ioctl (2),
> -- 
> 2.20.1
>
  
Alejandro Colomar Sept. 10, 2021, 1:37 p.m. UTC | #3
Hi, Pali!

On 8/31/21 10:34 PM, Pali Rohár wrote:
> On Tuesday 10 August 2021 21:49:28 Pali Rohár wrote:
>> Setting custom baudrate for which is not defined Bnnn constant is possible
>> via BOTHER flag and then filling speed in c_ospeed and c_ispeed fields.
>>
>> These two fields are either in struct termios or struct termios2. Former
>> belongs to TCGETS/TCSETS ioctls, latter to TCGETS2/TCSETS2 ioctls.
>>
>> BOTHER flag with these two fields and new struct termios2 is not supported
>> by older versions of include header files.
>>
>> Some architectures (e.g. amd64) provide both struct termios and struct
>> termios2, but c_ospeed and c_ispeed are only in struct termios2.
>>
>> Some other architectures (e.g. alpha) provide both struct termios and struct
>> termios2 and both have c_ospeed and c_ispeed fields.
>>
>> And some other architectures (e.g. powerpc) provide only struct termios
>> (no struct termios2) and it has c_ospeed and c_ispeed fields.
>>
>> So basically to support all architectures it is needed to use
>> struct termios2 when TCGETS2/TCSETS2 is supported. Otherwise it is needed
>> to use struct termios with TCGETS/TCSETS (case for e.g. powerpc).
>>
>> Setting input baudrate is done via IBSHIFT macro.
>>
>> Signed-off-by: Pali Rohár <pali@kernel.org>
> 
> Hello! Do you have any comments on this patch?

Sorry, I started to fix the licensing issues Greg pointed out, and 
forgot about this.

I assume it's good since you have been doing a lot of related work in 
recent patches.  I'll only point out a cosmetic issue with the 
preprocessor stuff:  I'd indent with a single space after '#' to clarify 
preprocessor #if #else #endif relationships and improve readability.  I 
also prefer '#if [!]defined()' rather than '#if[n]def' (except for 
include guards); and there seems to be a mix in the existing pages, so 
I'll standardize that way from now on.

For example:

#if !defined a
#else
# if defined b
# endif
#endif

However, I applied those changes myself in a following patch, so you 
don't worry about them.

Patch applied!

Thanks,

Alex

> 
>> ---
>> Changes in v4:
>> * Add SPDX-License-Identifier
>> * Correctly process split baudrates (separate output and input) via IBSHIFT
>> * Update commit message
>>
>> Changes in v3:
>> * Check support for custom baudrate only based on BOTHER macro
>> * Use TCGETS/TCSETS/termios when TCGETS2/TCSETS2/termios2 is not available
>>
>> Changes in v2:
>> * Use \e for backslash
>> * Use exit(EXIT_*) instead of return num
>> * Sort includes
>> * Add comment about possible fallback
>> ---
>>   man2/ioctl_tty.2 | 100 +++++++++++++++++++++++++++++++++++++++++++++++
>>   1 file changed, 100 insertions(+)
>>
>> diff --git a/man2/ioctl_tty.2 b/man2/ioctl_tty.2
>> index abfdc1e21fe9..7b2b03ff6757 100644
>> --- a/man2/ioctl_tty.2
>> +++ b/man2/ioctl_tty.2
>> @@ -796,6 +796,106 @@ main(void)
>>       close(fd);
>>   }
>>   .EE
>> +.PP
>> +Get or set arbitrary baudrate on the serial port.
>> +.PP
>> +.EX
>> +/* SPDX-License-Identifier: GPL-2.0-or-later */
>> +
>> +#include <asm/termbits.h>
>> +#include <fcntl.h>
>> +#include <stdio.h>
>> +#include <stdlib.h>
>> +#include <sys/ioctl.h>
>> +#include <sys/types.h>
>> +#include <unistd.h>
>> +
>> +int
>> +main(int argc, char *argv[])
>> +{
>> +#ifndef BOTHER
>> +    fprintf(stderr, "BOTHER is unsupported\en");
>> +    /* Program may fallback to TCGETS/TCSETS with Bnnn constants */
>> +    exit(EXIT_FAILURE);
>> +#else
>> +    /* Declare tio structure, its type depends on supported ioctl */
>> +#ifdef TCGETS2
>> +    struct termios2 tio;
>> +#else
>> +    struct termios tio;
>> +#endif
>> +    int fd, rc;
>> +
>> +    if (argc != 2 && argc != 3 && argc != 4) {
>> +        fprintf(stderr, "Usage: %s device [output [input] ]\en", argv[0]);
>> +        exit(EXIT_FAILURE);
>> +    }
>> +
>> +    fd = open(argv[1], O_RDWR | O_NONBLOCK | O_NOCTTY);
>> +    if (fd < 0) {
>> +        perror("open");
>> +        exit(EXIT_FAILURE);
>> +    }
>> +
>> +    /* Get the current serial port settings via supported ioctl */
>> +#ifdef TCGETS2
>> +    rc = ioctl(fd, TCGETS2, &tio);
>> +#else
>> +    rc = ioctl(fd, TCGETS, &tio);
>> +#endif
>> +    if (rc) {
>> +        perror("TCGETS");
>> +        close(fd);
>> +        exit(EXIT_FAILURE);
>> +    }
>> +
>> +    /* Change baud rate when more arguments were provided */
>> +    if (argc == 3 || argc == 4) {
>> +        /* Clear the current output baud rate and fill a new value */
>> +        tio.c_cflag &= ~CBAUD;
>> +        tio.c_cflag |= BOTHER;
>> +        tio.c_ospeed = atoi(argv[2]);
>> +
>> +        /* Clear the current input baud rate and fill a new value */
>> +        tio.c_cflag &= ~(CBAUD << IBSHIFT);
>> +        tio.c_cflag |= BOTHER << IBSHIFT;
>> +        /* When 4th argument is not provided reuse output baud rate */
>> +        tio.c_ispeed = (argc == 4) ? atoi(argv[3]) : atoi(argv[2]);
>> +
>> +        /* Set new serial port settings via supported ioctl */
>> +#ifdef TCSETS2
>> +        rc = ioctl(fd, TCSETS2, &tio);
>> +#else
>> +        rc = ioctl(fd, TCSETS, &tio);
>> +#endif
>> +        if (rc) {
>> +            perror("TCSETS");
>> +            close(fd);
>> +            exit(EXIT_FAILURE);
>> +        }
>> +
>> +        /* And get new values which were really configured */
>> +#ifdef TCGETS2
>> +        rc = ioctl(fd, TCGETS2, &tio);
>> +#else
>> +        rc = ioctl(fd, TCGETS, &tio);
>> +#endif
>> +        if (rc) {
>> +            perror("TCGETS");
>> +            close(fd);
>> +            exit(EXIT_FAILURE);
>> +        }
>> +    }
>> +
>> +    close(fd);
>> +
>> +    printf("output baud rate: %u\en", tio.c_ospeed);
>> +    printf("input baud rate: %u\en", tio.c_ispeed);
>> +
>> +    exit(EXIT_SUCCESS);
>> +#endif
>> +}
>> +.EE
>>   .SH SEE ALSO
>>   .BR ldattach (1),
>>   .BR ioctl (2),
>> -- 
>> 2.20.1
>>
  
Pali Rohár Sept. 10, 2021, 1:39 p.m. UTC | #4
On Friday 10 September 2021 15:37:54 Alejandro Colomar (man-pages) wrote:
> Hi, Pali!
> 
> On 8/31/21 10:34 PM, Pali Rohár wrote:
> > On Tuesday 10 August 2021 21:49:28 Pali Rohár wrote:
> > > Setting custom baudrate for which is not defined Bnnn constant is possible
> > > via BOTHER flag and then filling speed in c_ospeed and c_ispeed fields.
> > > 
> > > These two fields are either in struct termios or struct termios2. Former
> > > belongs to TCGETS/TCSETS ioctls, latter to TCGETS2/TCSETS2 ioctls.
> > > 
> > > BOTHER flag with these two fields and new struct termios2 is not supported
> > > by older versions of include header files.
> > > 
> > > Some architectures (e.g. amd64) provide both struct termios and struct
> > > termios2, but c_ospeed and c_ispeed are only in struct termios2.
> > > 
> > > Some other architectures (e.g. alpha) provide both struct termios and struct
> > > termios2 and both have c_ospeed and c_ispeed fields.
> > > 
> > > And some other architectures (e.g. powerpc) provide only struct termios
> > > (no struct termios2) and it has c_ospeed and c_ispeed fields.
> > > 
> > > So basically to support all architectures it is needed to use
> > > struct termios2 when TCGETS2/TCSETS2 is supported. Otherwise it is needed
> > > to use struct termios with TCGETS/TCSETS (case for e.g. powerpc).
> > > 
> > > Setting input baudrate is done via IBSHIFT macro.
> > > 
> > > Signed-off-by: Pali Rohár <pali@kernel.org>
> > 
> > Hello! Do you have any comments on this patch?
> 
> Sorry, I started to fix the licensing issues Greg pointed out, and forgot
> about this.
> 
> I assume it's good since you have been doing a lot of related work in recent
> patches.  I'll only point out a cosmetic issue with the preprocessor stuff:
> I'd indent with a single space after '#' to clarify preprocessor #if #else
> #endif relationships and improve readability.  I also prefer '#if
> [!]defined()' rather than '#if[n]def' (except for include guards); and there
> seems to be a mix in the existing pages, so I'll standardize that way from
> now on.
> 
> For example:
> 
> #if !defined a
> #else
> # if defined b
> # endif
> #endif
> 
> However, I applied those changes myself in a following patch, so you don't
> worry about them.

Ok, perfect, thanks!

> Patch applied!
> 
> Thanks,
> 
> Alex
  

Patch

diff --git a/man2/ioctl_tty.2 b/man2/ioctl_tty.2
index abfdc1e21fe9..7b2b03ff6757 100644
--- a/man2/ioctl_tty.2
+++ b/man2/ioctl_tty.2
@@ -796,6 +796,106 @@  main(void)
     close(fd);
 }
 .EE
+.PP
+Get or set arbitrary baudrate on the serial port.
+.PP
+.EX
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#include <asm/termbits.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+int
+main(int argc, char *argv[])
+{
+#ifndef BOTHER
+    fprintf(stderr, "BOTHER is unsupported\en");
+    /* Program may fallback to TCGETS/TCSETS with Bnnn constants */
+    exit(EXIT_FAILURE);
+#else
+    /* Declare tio structure, its type depends on supported ioctl */
+#ifdef TCGETS2
+    struct termios2 tio;
+#else
+    struct termios tio;
+#endif
+    int fd, rc;
+
+    if (argc != 2 && argc != 3 && argc != 4) {
+        fprintf(stderr, "Usage: %s device [output [input] ]\en", argv[0]);
+        exit(EXIT_FAILURE);
+    }
+
+    fd = open(argv[1], O_RDWR | O_NONBLOCK | O_NOCTTY);
+    if (fd < 0) {
+        perror("open");
+        exit(EXIT_FAILURE);
+    }
+
+    /* Get the current serial port settings via supported ioctl */
+#ifdef TCGETS2
+    rc = ioctl(fd, TCGETS2, &tio);
+#else
+    rc = ioctl(fd, TCGETS, &tio);
+#endif
+    if (rc) {
+        perror("TCGETS");
+        close(fd);
+        exit(EXIT_FAILURE);
+    }
+
+    /* Change baud rate when more arguments were provided */
+    if (argc == 3 || argc == 4) {
+        /* Clear the current output baud rate and fill a new value */
+        tio.c_cflag &= ~CBAUD;
+        tio.c_cflag |= BOTHER;
+        tio.c_ospeed = atoi(argv[2]);
+
+        /* Clear the current input baud rate and fill a new value */
+        tio.c_cflag &= ~(CBAUD << IBSHIFT);
+        tio.c_cflag |= BOTHER << IBSHIFT;
+        /* When 4th argument is not provided reuse output baud rate */
+        tio.c_ispeed = (argc == 4) ? atoi(argv[3]) : atoi(argv[2]);
+
+        /* Set new serial port settings via supported ioctl */
+#ifdef TCSETS2
+        rc = ioctl(fd, TCSETS2, &tio);
+#else
+        rc = ioctl(fd, TCSETS, &tio);
+#endif
+        if (rc) {
+            perror("TCSETS");
+            close(fd);
+            exit(EXIT_FAILURE);
+        }
+
+        /* And get new values which were really configured */
+#ifdef TCGETS2
+        rc = ioctl(fd, TCGETS2, &tio);
+#else
+        rc = ioctl(fd, TCGETS, &tio);
+#endif
+        if (rc) {
+            perror("TCGETS");
+            close(fd);
+            exit(EXIT_FAILURE);
+        }
+    }
+
+    close(fd);
+
+    printf("output baud rate: %u\en", tio.c_ospeed);
+    printf("input baud rate: %u\en", tio.c_ispeed);
+
+    exit(EXIT_SUCCESS);
+#endif
+}
+.EE
 .SH SEE ALSO
 .BR ldattach (1),
 .BR ioctl (2),