linux-api.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Theodore Ts'o" <tytso@mit.edu>
To: "H. Peter Anvin" <hpa@zytor.com>
Cc: Maarten Brock <Maarten.Brock@sttls.nl>,
	"linux-serial@vger.kernel.org" <linux-serial@vger.kernel.org>,
	"linux-api@vger.kernel.org" <linux-api@vger.kernel.org>,
	LKML <linux-kernel@vger.kernel.org>
Subject: Re: RFC: Serial port DTR/RTS - O_NRESETDEV
Date: Mon, 10 Nov 2025 22:51:43 -0500	[thread overview]
Message-ID: <20251111035143.GJ2988753@mit.edu> (raw)
In-Reply-To: <0F8021E8-F288-4669-8195-9948844E36FD@zytor.com>

On Mon, Nov 10, 2025 at 01:05:55PM -0800, H. Peter Anvin wrote:
> 
> The parport driver layer is kind of similar to this, in some ways,
> but in the tty layer that is mostly handled by line disciplines
> instead. (The parport hardware was generally abused on a much lower
> level, as a substitute for GPIOs, so even the notion of a byte
> stream wasn't there.)

I'm not an RS-485 expert, but over the years, I've heard all sorts of
"interesting" timing requirements.  For example RTS can only be
dropped when the UART's shift register has completely drained.  That's
not when the OS has sent the characters to the FIFO (which is why
tcdrain isn't quite what you want); it's not when the UART has sent
the transmit interrupt, but when the UART's shift register is *empty*.
Doing this via the termios interface is decidedly non-optimal[1].

[1] https://www.moxa.com.cn/getmedia/a07808dd-f3d7-473b-9a2f-93b5ce673bb1/moxa-the-secrets-of-rs-485-half-duplex-communication-tech-note-v1.0.pdf

It *can* be done, sure, with some strategic usleep()'s, but not
necessarily optimally, and it is decidedly hacky.  From what I can
tell, it's actually not *that* different from treating RTS as a GPIO
pin, and really, this ought to be done in the kernel if you want
optimal control oer the timing of when RTS gets toggeled.

> *If* I'm reading the code correctly – which is a little complicated due to the sheer number of abstraction layers – hardware initialization is already deferred until first open, which would mean that disabling autoconfiguration (one of the features in TIOCSSERIAL) would again be a valid reason for wanting to be able to communicate with a device driver before requiring that it puts the underlying hardware in the state expected for operation *in the mode configured* (catch-22).

Well, part of the problem is that the PC's serial port predates PCI,
so there's no way we can tell whether or not there is a serial port at
a particular I/O port except by poking certain I/O ports, and seeing
if there is something that looks like a UART which responds.
Hopefully this won't accidentaly cause the NSA's thermite-charge
self-destruct charge from getting set off, and in practice, mainboard
designers who try to put things at the COM1/2/3/4 generally get weeded
out thanks to natural selection, hopefully without harming users along
the way.  :-)

Worse, we also wanted to support serial cards that were at
non-standard ports, or using non-standard interrupts, and so that's
one of the reason hardware initialization is deferred until after we
have a chance to configure the serial device's I/O port and interrupt.

Now, we are in the 21st century, and just as we are trying to declare
32-bit i386 dead (to the disappointment of legacy hardware owners
everywhere who are whining about Debian dropping installer support for
i386), we could try to declare the ISA bus as dead, and no longer
being supported, and we could probably drop a bunch of this
complexity.  We probably would need to support COM 1/2/3/4 ports since
these still tend to be hardwared without a way of detecting it their
existing via USB or PCI bus discovery mechanisms.  And for those, we
would still need to do UART autoconfiguration.  I suppose we could
just assume that all UART's are 16550A's, and that noone would
actually use older UART's type --- except (a) I bet there are some
cheap-skate embedded computer designers who decided to use a 8250
instead of 16550A for $REASONS$, and because of the extreme timing
requirements of some RS-485 use cases, I believe I have seen
recommendations to use setserial to actually force the UART into 8250
mode, to disable the TX FIFO --- all to satisfying the weird-shit RTS
timing requirements.  (This is where I get my beief that RS-485 !=
RS-232, and if you want to do all of this weird-shit timing
requirements because you are trying to implemet a multi-node bus which
is half-duplex, maybe this should be done in the kernel, damn it!)


How about this?  If you really don't want to open the device to
configure it not to assert DTR/RTS, perhaps we could specify on the
boot command line that one of TTYS[0123] has a different default line
discipline, and when tty's are opened when the RS-485 line displine,
the RS-485 rules apply, which would include not messing with DTR/RTS,
and if you want to play games with RTS getting dropped as soon as the
last bit has let the transmit shift register, we can get the kernel do
this in the line discpline, which arguably would be *way* more
reliable than playing GPIO-style timing games in userspace.

	      	      		 	      - Ted

  reply	other threads:[~2025-11-11  3:51 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-07  7:53 RFC: Serial port DTR/RTS - O_NRESETDEV H. Peter Anvin
2025-11-07 17:37 ` Theodore Ts'o
2025-11-09  2:25   ` H. Peter Anvin
2025-11-10  3:35     ` Theodore Ts'o
2025-11-10  5:00       ` H. Peter Anvin
2025-11-10 10:06         ` Maarten Brock
2025-11-10 20:19           ` Theodore Ts'o
2025-11-10 21:05             ` H. Peter Anvin
2025-11-11  3:51               ` Theodore Ts'o [this message]
2025-11-11  3:57                 ` H. Peter Anvin
2025-11-11  4:38                   ` Theodore Ts'o
2025-11-11 10:21                     ` Maarten Brock
2025-11-11 21:28                     ` H. Peter Anvin
2025-11-12 11:22                   ` Greg KH
2025-11-12 16:09                     ` H. Peter Anvin
2025-11-12 16:46                       ` Greg KH
2025-11-12 19:12                         ` H. Peter Anvin
2025-11-12 19:39                           ` Greg KH
2025-11-12 19:53                             ` H. Peter Anvin
2025-11-12 19:55                             ` H. Peter Anvin
2025-11-13 22:24                             ` RFC: Serial port DTR/RTS - O_<something> H. Peter Anvin
2025-11-14 10:26                               ` Maarten Brock
2025-11-14 18:49                               ` Maciej W. Rozycki
2025-11-14 18:53                                 ` H. Peter Anvin
2025-11-15 21:29                                   ` Ned Ulbricht
2025-11-15 22:29                                     ` H. Peter Anvin
2025-11-16  0:47                                     ` H. Peter Anvin
2025-11-18 16:33                                       ` Ned Ulbricht
2025-11-18 17:31                                         ` H. Peter Anvin
2025-11-18 18:05                                         ` H. Peter Anvin
2025-11-20 13:31                                           ` Ned Ulbricht
2025-11-10  5:20   ` RFC: Serial port DTR/RTS - O_NRESETDEV H. Peter Anvin
2025-11-09 20:43 ` Maciej W. Rozycki

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20251111035143.GJ2988753@mit.edu \
    --to=tytso@mit.edu \
    --cc=Maarten.Brock@sttls.nl \
    --cc=hpa@zytor.com \
    --cc=linux-api@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-serial@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).