Re: [PATCH 2/2] media: pwm-ir-tx: trigger edges from hrtimer interrupt context

From: Sean Young
Date: Wed Oct 04 2023 - 10:42:13 EST


On Wed, Oct 04, 2023 at 03:54:32PM +0300, Ivaylo Dimitrov wrote:
> On 4.10.23 г. 11:00 ч., Sean Young wrote:
> > On Mon, Oct 02, 2023 at 09:16:53AM +0300, Ivaylo Dimitrov wrote:
> > > On 1.10.23 г. 13:40 ч., Sean Young wrote:
> > > > The pwm-ir-tx driver has to turn the pwm signal on and off, and suffers
> > > > from delays as this is done in process context. Make this work in atomic
> > > > context.
> > > >
> > > > This makes the driver much more precise.
> > > >
> > > > Signed-off-by: Sean Young <sean@xxxxxxxx>
> > > > Cc: Ivaylo Dimitrov <ivo.g.dimitrov.75@xxxxxxxxx>
> > > > ---
> > > > drivers/media/rc/pwm-ir-tx.c | 79 ++++++++++++++++++++++++++++--------
> > > > 1 file changed, 63 insertions(+), 16 deletions(-)
> > > >
> > >
> > > what about the following patch(not a proper one, just RFC)? It achieves the
> > > same (if not better) precision (on n900 at least) without using atomic pwm.
> > > What it does is: create a fifo thread in which we swicth pwm on/off, start
> > > hrtimer that is used to signal thread when to switch pwm.
> > > As signal comes earlier than needed(because hrtimer runs async to the
> > > thread), we do a busy loop wait for the precise time to switch the pwm. At
> > > least on n900, this busy loop is less than 200 us per switch(worst case,
> > > usually it is less than 100 us). That way, even if we have some latency
> > > spike, it is covered by not doing busy loop for that particular pwm switch
> > > and we keep the precise timing.
> >
> > I think this is a good idea.
> >
> > > Maybe we shall merge both patches so fifo thread to be used for sleeping
> > > pwms and hrtimer for atomic. I can do that and test it here if you think
> > > that approach makes sense.
> >
> > Let's try and merge this patch for the next merge window, and worry about
> > the atomic version after that. We've already queued the ir-rx51 removal
> > patches to media_stage so it would be nice to have to revert these patches,
> > and improve pwm-ir-tx for the next kernel release.
> >
>
> ir-rx51 is broken without
> https://www.spinics.net/lists/kernel/msg4953300.html, it is also missing a
> call to init_waitqueue_head() in the probe() function. So I have no strong
> opinion on what shall be done with it.

Sure, ok. I guess the pwm-ir-tx driver is less broken in that regard.

In that case I propose we merge the ir-rx51 for the next merge window,
and further fixes to pwm-ir-tx go in when they're ready.

> > This means the thread is always around. How about creating the thread
> > per-tx?
> >
>
> Yes, that can be done, just not sure what the overhead would be.
>
> Also, I think we shall reconsider the way the driver works:
>
> Imagine we have to pretend we are TV remote that supports NEC protocol (for
> example), especially the "REPEAT CODES" part. Currently, no matter what we
> do, there is no way to get the timings even remotely right, as we have no
> idea what the "warmup" and "complete" delays are. Like, starting thread (if
> needed), hrtimer setup time, completions waiting, contexts switching, etc.

It's not perfect, but the assumption is that those times are going to be
the same or very similar for each tx. So, if the setup/warmup time is the same
and if there is no complete delay, then using usleep() between two txs
works fine. I think in reality the setup/complete times are extremely
short (time to send usb packet or so), and compared to IR timings this is
insignificant.

Having said that, maybe a different scheme would be nice, which could offer
better precision.

> So, I think the correct thing to do is to copy txbuf (as a list of txbufs)
> into pwm_ir in tx function, start pulses generation and return from
> pwm_ir_tx() *immediately*, without waiting for tx to finish. If userspace
> requests submission of another set of pulses while we are still sending the
> current one, well, we accept it, add it to the list and delay the sending
> until the current one is finished. When there is nothing more to send (the
> list is empty), stop the hrtimer (and perhaps the thread)
>
> I think that way userspace will be able to append as many "repeat" pulses
> with proper timings as it wants (with some sane limits ofc).
>
> Unless we somehow have API restriction that we shall not return until tx is
> finished.
>
> Does that make any sense to you?

Two problems:

It's a breaking uapi change: for example lircd and ir-ctl use this for
calculating the gap between transmits. If we start returning early then
things break.

Secondly, not all drivers can support this, or they would need to support
it using a thread or so, which makes the driver code much more complicated
and we'd have to change nearly every driver.


Sean