From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: <> Received: from fx308.security-mail.net (smtpout30.security-mail.net [85.31.212.39]) by sourceware.org (Postfix) with ESMTPS id 514CC396E42B for ; Tue, 10 Aug 2021 19:50:37 +0000 (GMT) Authentication-Results: sourceware.org; dkim=permerror (bad message/signature format) Received: by fx308.security-mail.net (Postfix) id 5CBD427FA6C; Tue, 10 Aug 2021 21:50:36 +0200 (CEST) Date: Tue, 10 Aug 2021 21:50:36 +0200 (CEST) From: MAILER-DAEMON (Mail Delivery System) Subject: Undelivered Mail Returned to Sender To: libc-alpha@sourceware.org Auto-Submitted: auto-replied MIME-Version: 1.0 Content-Type: multipart/report; report-type=delivery-status; boundary="14EE827FA6A.1628625036/fx308.security-mail.net" Content-Transfer-Encoding: 8bit Message-Id: <20210810195036.5CBD427FA6C@fx308.security-mail.net> X-Spam-Status: No, score=-11.1 required=5.0 tests=BAYES_00, GIT_PATCH_0, HTML_MESSAGE, KAM_DMARC_STATUS, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, 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-Content-Filtered-By: Mailman/MimeDel 2.1.29 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-List-Received-Date: Tue, 10 Aug 2021 19:50:48 -0000 This is a MIME-encapsulated message. --14EE827FA6A.1628625036/fx308.security-mail.net Content-Description: Notification Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit This is the mail system at host fx308.security-mail.net. I'm sorry to have to inform you that your message could not be delivered to one or more recipients. It's attached below. For further assistance, please send mail to postmaster. If you do so, please include this problem report. You can delete your own text from the attached returned message. The mail system : host zimbra2.kalray.eu[195.135.97.26] said: 550 5.1.1 : Recipient address rejected: User unknown in virtual mailbox table (in reply to RCPT TO command) --14EE827FA6A.1628625036/fx308.security-mail.net Content-Description: Delivery report Content-Type: message/delivery-status Reporting-MTA: dns; fx308.security-mail.net X-Postfix-Queue-ID: 14EE827FA6A X-Postfix-Sender: rfc822; libc-alpha@sourceware.org Arrival-Date: Tue, 10 Aug 2021 21:50:36 +0200 (CEST) Final-Recipient: rfc822; mpoulhies@kalray.eu Original-Recipient: rfc822;mpoulhies@kalray.eu Action: failed Status: 5.1.1 Remote-MTA: dns; zimbra2.kalray.eu Diagnostic-Code: smtp; 550 5.1.1 : Recipient address rejected: User unknown in virtual mailbox table --14EE827FA6A.1628625036/fx308.security-mail.net Content-Description: Undelivered Message Content-Type: message/rfc822 Content-Transfer-Encoding: 8bit Return-Path: Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) by fx308.security-mail.net (Postfix) with ESMTPS id 1F0C127FA5C for ; Tue, 10 Aug 2021 21:50:29 +0200 (CEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 880A8396E44B for ; Tue, 10 Aug 2021 19:50:27 +0000 (GMT) Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by sourceware.org (Postfix) with ESMTPS id 23D78396E474 for ; Tue, 10 Aug 2021 19:49:45 +0000 (GMT) Received: by mail.kernel.org (Postfix) with ESMTPSA id D0B3E60ED8; Tue, 10 Aug 2021 19:49:43 +0000 (UTC) Received: by pali.im (Postfix) id 66AC182D; Tue, 10 Aug 2021 21:49:41 +0200 (CEST) X-Quarantine-ID: X-Virus-Scanned: E-securemail, by Secumail X-Spam-Status: No, score=-3.449 tagged_above=-1000 required=7.5 tests=[AB_ENVFROM_LONG_40=0.5, AB_FAKE_WORD_1=0.2, AB_IN_REPLY_TO_EXISTS=-1, AB_LONG_SUBJ_30=0.001, DKIM_SIGNED=0.1, DKIM_VALID=-1, DKIM_VALID_AU=-0.1, FSL_RCVD_EX_3=0.01, FSL_RCVD_UT_3=0.01, HEAD_NEWS=-0.5, MISSING_MID=0.14, MM_ENVFROM_BOUNCE=1, RCVD_IN_DNSWL_MED=-1.3, S_FROM_GREY_MINUS_2=-2, S_KW_BODY__UPDATE_=0.5, T_RP_MATCHES_RCVD=-0.01] autolearn=disabled Authentication-Results: fx308.security-mail.net (amavisd-new); dkim=pass (1024-bit key) header.d=sourceware.org Secumail-id: DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 880A8396E44B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1628625027; bh=NgVZZlyYY3K1SrhCH11Yn4Sc7Fsqx94+M0hFiUiGJIU=; 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=nPdoZYWKth2X6KczYp+HGEwjRZlhuIb37EvX68DjpXpZDwA+4ENIkS+h/5zl8fxH4 mRKEYX6xaK7mwIV5Mf7mkkt1eswr/PVXQpMSD+cuMBQgZYwWOUD2RyFeLn2lW8PxB1 VflzaN+n1/62tQbjrgHaRHXleujzACO9lVxV4rlI= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 23D78396E474 To: linux-man@vger.kernel.org, Alejandro Colomar , Michael Kerrisk Subject: [PATCH v4] ioctl_tty.2: Add example how to get or set baudrate on the serial port Date: Tue, 10 Aug 2021 21:49:28 +0200 Message-ID: <20210810194928.16408-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-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: , From: Pali =?utf-8?b?Um9ow6Fy?= via Libc-alpha Reply-To: Pali =?utf-8?b?Um9ow6Fy?= Cc: Marek =?utf-8?b?QmVow7pu?= , "G. Branden Robinson" , libc-alpha@sourceware.org Errors-To: libc-alpha-bounces+mpoulhies=kalray.eu@sourceware.org Sender: Libc-alpha X-ALTERMIMEV2_in: done Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit 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 --- 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 +#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 + /* 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 To declare a filtering error, please use the following link : https://www.security-mail.net/reporter.php?mid=df5a.6112d885.c270.0&r=mpoulhies%40kalray.eu&s=libc-alpha-bounces%2Bmpoulhies%3Dkalray.eu%40sourceware.org&o=%5BPATCH+v4%5D+ioctl_tty.2%3A+Add+example+how+to+get+or+set+baudrate+on+the+serial+port&verdict=C&c=bba6a45faa79c35a46e941bf6cdc12942e2af641 --14EE827FA6A.1628625036/fx308.security-mail.net--