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: 44516 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 04C683850416 for ; Fri, 30 Jul 2021 13:03:40 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 04C683850416 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1627650220; 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=dgEcLbrXKNFphRsKFDYjfyohS0JM5qygXcib24ArbzM6Us6dqFISWFwrrbe4SSlGP emy6vthciQA2h+dFc5+zl3jt1Il8nldN5qDhszcrhAoQijCupwoF1WtoOqVIgwIHp0 MbsnGC974TFpqmvC2TUq71/I7aMb8K14YLRA42x8= 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+patchwork=sourceware.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),