From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2155) id BCEA2395B068; Fri, 11 Nov 2022 12:06:17 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BCEA2395B068 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1668168378; bh=C8lEt40Cnabpv+u59jdBeL0tPpPDU5JMuDLtCgs8D9A=; h=From:To:Subject:Date:From; b=Axe5uBYIeHQtDagABIOyYJgVjDBxDOaW5kmszbZdMdhQ6cJH0ihtqW1WX5LYRpDhz tepXMhijLxqg4htYBPSXonFC6JljkclPBh1cpbbphA58NOwIWNImx+YKo67iscVx8l akQcjUgoY64QQ+g5iOxCha6PO7d2QZvutHE/Y8B8= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Corinna Vinschen To: cygwin-cvs@sourceware.org Subject: [newlib-cygwin] Cygwin: fix TIOCMBIS/TIOCMBIC not working when using usbser.sys X-Act-Checkin: newlib-cygwin X-Git-Author: Carlo Bramini <30959007+carlo-bramini@users.noreply.github.com> X-Git-Refname: refs/heads/master X-Git-Oldrev: 5c79aa4b22df09b6fb3e97538fee7f108f9fd768 X-Git-Newrev: 2dab880c963ce0204c3513d664f610b587a3e6a6 Message-Id: <20221111120618.BCEA2395B068@sourceware.org> Date: Fri, 11 Nov 2022 12:06:17 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dnewlib-cygwin.git;h=3D2dab880c963= ce0204c3513d664f610b587a3e6a6 commit 2dab880c963ce0204c3513d664f610b587a3e6a6 Author: Carlo Bramini <30959007+carlo-bramini@users.noreply.github.com> Date: Fri Nov 11 11:15:27 2022 +0100 Cygwin: fix TIOCMBIS/TIOCMBIC not working when using usbser.sys =20 In winsup/cygwin/fhandler/serial.cc, the function fhandler_serial::switch_modem_lines() is called when TIOCMBIS/TIOCMBIC are used in an ioctl() call. =20 This function uses EscapeCommFunction() for setting and resetting RTS and DTR signals of a serial port. Unfortunately, this function does not work on USB CDC devices. =20 This is not a true bug of CYGWIN but an issue of the usbser.sys driver, from Windows 2000 to the latest Windows 11. Both 32bit and 64bit versions of the operating system are affected. Actually, I tested EscapeCommFunction() also when using a real UART, based on the traditional 16550 driver and it works fine. Using thirdy party CDC drivers, like the one provided by FTDI for their USB bridge chips, probably also works. =20 However, it is also possible to drive the RTS/DTR signals by writing their state with SetCommState(), which proved to be working fine all types of connection. This is also a better solution for handling these signals since RTS/DTR can be set at the same time rather than having two separate calls with a visible delay between them. Diff: --- winsup/cygwin/fhandler/serial.cc | 67 +++++++++++++++++-------------------= ---- 1 file changed, 28 insertions(+), 39 deletions(-) diff --git a/winsup/cygwin/fhandler/serial.cc b/winsup/cygwin/fhandler/seri= al.cc index 174a57a43..58a8df677 100644 --- a/winsup/cygwin/fhandler/serial.cc +++ b/winsup/cygwin/fhandler/serial.cc @@ -391,50 +391,39 @@ fhandler_serial::tcflow (int action) int fhandler_serial::switch_modem_lines (int set, int clr) { - int res =3D 0; - - if (set & TIOCM_RTS) - { - if (EscapeCommFunction (get_handle (), SETRTS)) - rts =3D TIOCM_RTS; - else - { - __seterrno (); - res =3D -1; - } - } - else if (clr & TIOCM_RTS) - { - if (EscapeCommFunction (get_handle (), CLRRTS)) - rts =3D 0; - else - { - __seterrno (); - res =3D -1; - } - } - if (set & TIOCM_DTR) + if ((set & (TIOCM_RTS | TIOCM_DTR)) || (clr & (TIOCM_RTS | TIOCM_DTR))) { - if (EscapeCommFunction (get_handle (), SETDTR)) - rts =3D TIOCM_DTR; + DCB dcb; + + memset(&dcb,0,sizeof(dcb)); + dcb.DCBlength =3D sizeof(dcb); + + if (!GetCommState(get_handle(), &dcb)) + { + __seterrno (); + return -1; + } + + if (set & TIOCM_RTS) + dcb.fRtsControl =3D RTS_CONTROL_ENABLE; else - { - __seterrno (); - res =3D -1; - } - } - else if (clr & TIOCM_DTR) - { - if (EscapeCommFunction (get_handle (), CLRDTR)) - rts =3D 0; + if (clr & TIOCM_RTS) + dcb.fRtsControl =3D RTS_CONTROL_DISABLE; + + if (set & TIOCM_DTR) + dcb.fRtsControl =3D DTR_CONTROL_ENABLE; else - { - __seterrno (); - res =3D -1; - } + if (clr & TIOCM_DTR) + dcb.fRtsControl =3D DTR_CONTROL_DISABLE; + + if (!SetCommState(get_handle(), &dcb)) + { + __seterrno (); + return -1; + } } =20 - return res; + return 0; } =20 /* ioctl: */