From patchwork Sun Aug 1 13:51:45 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Pali_Roh=C3=A1r?= X-Patchwork-Id: 44549 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 600CD383B42F for ; Sun, 1 Aug 2021 13:55:33 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 600CD383B42F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1627826133; bh=qBuYyabT5s28Fu9tAnD69beYvf4u37dRA3rBiMnuyhc=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=uLX6X2xrAnq40fefgR/FdeuKOeuw8XC+K8kbwq4c49do26f61uyD7BzrZhUO1qE0+ 78HIG8+fizs7aBTlxhI+/vKcrDIzJjpdohVDhSWgzUFGmeN2J/G0uNmDAsBax5HvUP vGJBAkHXM3eIg8YcVSk6Aj7YaTr2hESkrjqXAFrs= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by sourceware.org (Postfix) with ESMTPS id 777413858439 for ; Sun, 1 Aug 2021 13:55:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 777413858439 Received: by mail.kernel.org (Postfix) with ESMTPSA id 5CAB8610A5; Sun, 1 Aug 2021 13:55:09 +0000 (UTC) Received: by pali.im (Postfix) id E7AAAEF9; Sun, 1 Aug 2021 15:55:06 +0200 (CEST) To: linux-man@vger.kernel.org, Alejandro Colomar , Michael Kerrisk Subject: [PATCH v3] ioctl_tty.2: Add example how to get or set baudrate on the serial port Date: Sun, 1 Aug 2021 15:51:45 +0200 Message-Id: <20210801135146.14849-1-pali@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210730095333.6118-1-pali@kernel.org> References: <20210730095333.6118-1-pali@kernel.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: =?utf-8?q?Pali_Roh=C3=A1r_via_Libc-alpha?= From: =?utf-8?q?Pali_Roh=C3=A1r?= Reply-To: =?utf-8?q?Pali_Roh=C3=A1r?= Cc: =?utf-8?q?Marek_Beh=C3=BAn?= , "G. Branden Robinson" , libc-alpha@sourceware.org Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" Signed-off-by: Pali Rohár --- 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 --- Hello Alejandro! I found out that this stuff is more complicated as I originally thought. And seems that additional documentation on this topic is needed... For setting custom baudrate it is needed to set BOTHER flag in c_cflag field and baudrate value itself in c_ospeed and c_ispeed fields. So when BOTHER flag is not provided by then setting custom baudrate is not possible, fields c_ospeed and c_ispeed do not exist (and only some predefined Bnnn baudrate values are supported). This applies when compiling application with older version of header files (prior support for custom baudrate was introduced into header files). First caveat: BOTHER constant is different for different architectures. So it is not possible to provide fallback #ifndef..#define BOTHER. And now the biggest issue: Some architectures have these c_ospeed and c_ispeed fields in struct termios and some in struct termios2. TCGETS/TCSETS ioctls use struct termios and TCGETS/TCSETS2 use struct termios2. 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). I updated v3 patch to handle this logic. --- man2/ioctl_tty.2 | 73 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/man2/ioctl_tty.2 b/man2/ioctl_tty.2 index 3020f9984872..d83cbd17225b 100644 --- a/man2/ioctl_tty.2 +++ b/man2/ioctl_tty.2 @@ -764,6 +764,79 @@ main(void) close(fd); } .EE +.PP +Get or set arbitrary baudrate on the serial port. +.PP +.EX +#include +#include +#include +#include +#include +#include +#include + +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 +#ifdef TCGETS2 + struct termios2 tio; +#else + struct termios tio; +#endif + int fd, rc; + + if (argc != 2 && argc != 3) { + fprintf(stderr, "Usage: %s device [new_baudrate]\en", argv[0]); + exit(EXIT_FAILURE); + } + + fd = open(argv[1], O_RDWR | O_NONBLOCK | O_NOCTTY); + if (fd < 0) { + perror("open"); + exit(EXIT_FAILURE); + } + +#ifdef TCGETS2 + rc = ioctl(fd, TCGETS2, &tio); +#else + rc = ioctl(fd, TCGETS, &tio); +#endif + if (rc) { + perror("TCGETS"); + close(fd); + exit(EXIT_FAILURE); + } + + printf("%u\en", tio.c_ospeed); + + if (argc == 3) { + tio.c_cflag &= ~CBAUD; + tio.c_cflag |= BOTHER; + tio.c_ospeed = tio.c_ispeed = atoi(argv[2]); + +#ifdef TCSETS2 + rc = ioctl(fd, TCSETS2, &tio); +#else + rc = ioctl(fd, TCSETS, &tio); +#endif + if (rc) { + perror("TCSETS"); + close(fd); + exit(EXIT_FAILURE); + } + } + + close(fd); + exit(EXIT_SUCCESS); +#endif +} +.EE .SH SEE ALSO .BR ldattach (1), .BR ioctl (2),