From patchwork Fri Jul 30 13:02:50 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: 1511657 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=E2P+B2dc; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4Gbndg3BvYz9sT6 for ; Fri, 30 Jul 2021 23:03:35 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D31383945C0B for ; Fri, 30 Jul 2021 13:03:32 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D31383945C0B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1627650212; bh=fk6HqE9c7LiWw+P4Dss49+0+WBmB7kjK+aqRPrEAZSo=; 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=E2P+B2dcJKcgzKCX2BR2VlCnALjSr24yzJVId8iIP6tVfX6JOXJvB9V4Xcp91UtWw M4OE2GjC8F8/WA0aNarD//scj/I4xvfIInp6PWKsyK0LZelR2fbMcFJx/7z8EikPuJ ZxFGTAWjTV72jV0DXbv9rnj/sYwkZ2xwWv1NDRZg= 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 66D0C3858426 for ; Fri, 30 Jul 2021 13:03:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 66D0C3858426 Received: by mail.kernel.org (Postfix) with ESMTPSA id 05E9560FE7; Fri, 30 Jul 2021 13:03:17 +0000 (UTC) Received: by pali.im (Postfix) id B0217772; Fri, 30 Jul 2021 15:03:14 +0200 (CEST) To: linux-man@vger.kernel.org, Alejandro Colomar , Michael Kerrisk Subject: [PATCH v2] ioctl_tty.2: Add example how to get or set baudrate on the serial port Date: Fri, 30 Jul 2021 15:02:50 +0200 Message-Id: <20210730130251.18781-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.7 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+incoming=patchwork.ozlabs.org@sourceware.org Sender: "Libc-alpha" Signed-off-by: Pali Rohár --- 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 | 61 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/man2/ioctl_tty.2 b/man2/ioctl_tty.2 index 967b5c4c78c9..771a9a470bf0 100644 --- a/man2/ioctl_tty.2 +++ b/man2/ioctl_tty.2 @@ -748,6 +748,67 @@ 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[]) +{ +#if !defined(TCGETS2) || !defined(TCSETS2) || !defined(BOTHER) + fprintf(stderr, "TCGETS2, TCSETS2 or BOTHER is unsupported\en"); + /* Program may fallback to TCGETS/TCSETS with Bnnn constants */ + exit(EXIT_FAILURE); +#else + struct termios2 tio2; + 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); + } + + rc = ioctl(fd, TCGETS2, &tio2); + if (rc) { + perror("TCGETS2"); + close(fd); + exit(EXIT_FAILURE); + } + + printf("%u\en", tio2.c_ospeed); + + if (argc == 3) { + tio2.c_cflag &= ~CBAUD; + tio2.c_cflag |= BOTHER; + tio2.c_ospeed = tio2.c_ispeed = atoi(argv[2]); + + rc = ioctl(fd, TCSETS2, &tio2); + if (rc) { + perror("TCSETS2"); + close(fd); + exit(EXIT_FAILURE); + } + } + + close(fd); + exit(EXIT_SUCCESS); +#endif +} +.EE .SH SEE ALSO .BR ldattach (1), .BR ioctl (2),