X86 platform drivers
 help / color / mirror / Atom feed
From: "Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>
To: Hans de Goede <hdegoede@redhat.com>
Cc: Andy Shevchenko <andy@kernel.org>,
	AceLan Kao <acelan.kao@canonical.com>,
	 Kai-Heng Feng <kai.heng.feng@canonical.com>,
	 platform-driver-x86@vger.kernel.org
Subject: Re: [PATCH 1/2] platform/x86: Add new Dell UART backlight driver
Date: Mon, 13 May 2024 16:34:35 +0300 (EEST)	[thread overview]
Message-ID: <7163ebe9-6cca-c21f-78b7-e6aeda2961b3@linux.intel.com> (raw)
In-Reply-To: <c9ec9611-615c-4a7d-9a72-89d6c7b4c680@redhat.com>

[-- Attachment #1: Type: text/plain, Size: 5734 bytes --]

On Mon, 13 May 2024, Hans de Goede wrote:

> Hi,
> 
> On 5/13/24 3:14 PM, Ilpo Järvinen wrote:
> > On Mon, 13 May 2024, Hans de Goede wrote:
> >> On 5/13/24 2:12 PM, Ilpo Järvinen wrote:
> >>> On Mon, 13 May 2024, Hans de Goede wrote:
> >>>> On 5/13/24 10:34 AM, Ilpo Järvinen wrote:
> >>>>> On Sun, 12 May 2024, Hans de Goede wrote:
> > 
> >>>>>> +
> >>>>>> +		dell_bl->resp_idx++;
> >>>>>> +		if (dell_bl->resp_idx < dell_bl->resp_len)
> >>>>>> +			continue;
> >>>>>> +
> >>>>>> +		csum = dell_uart_checksum(dell_bl->resp, dell_bl->resp_len - 1);
> >>>>>> +		if (dell_bl->resp[dell_bl->resp_len - 1] != csum) {
> >>>>>> +			dev_err(dell_bl->dev, "Checksum mismatch got 0x%02x expected 0x%02x\n",
> >>>>>> +				dell_bl->resp[dell_bl->resp_len - 1], csum);
> >>>>>> +			dell_bl->status = -EIO;
> >>>>>> +			goto wakeup;
> >>>>>> +		}
> >>>>>
> >>>>> Why is the checksum calculation and check inside the loop??
> >>>>
> >>>> The loop iterates over received bytes, which may contain extra data 
> >>>> after the response, the: 
> >>>>
> >>>> 		dell_bl->resp_idx++;
> >>>> 		if (dell_bl->resp_idx < dell_bl->resp_len)
> >>>> 			continue;
> >>>>
> >>>> continues looping until we have received all the expected bytes. So here, past this
> >>>> check, we are are at the point where we have a complete response and then we verify it.
> >>>>
> >>>> And on successful verification wake-up any waiters.
> >>>
> >>> So effectively you want to terminate the loop on two conditions here:
> >>>
> >>> a) dell_bl->resp_idx == dell_bl->resp_len (complete frame)
> >>> a) if i == len (not yet received a full frame)
> >>>
> >>> Why not code those rather than the current goto & continue madness?
> >>>
> >>> Then, after the loop, you can test:
> >>>
> >>> 	if (dell_bl->resp_idx == dell_bl->resp_len) {
> >>> 		// calc checksum, etc.
> >>> 	}
> >>>
> >>> ?
> >>
> >> Ok, I've added the following change for v3:
> >>
> >> diff --git a/drivers/platform/x86/dell/dell-uart-backlight.c b/drivers/platform/x86/dell/dell-uart-backlight.c
> >> index bf5b12efcb19..66d8c6ddcb83 100644
> >> --- a/drivers/platform/x86/dell/dell-uart-backlight.c
> >> +++ b/drivers/platform/x86/dell/dell-uart-backlight.c
> >> @@ -87,6 +87,7 @@ static int dell_uart_bl_command(struct dell_uart_backlight *dell_bl,
> >>  	dell_bl->status = -EBUSY;
> >>  	dell_bl->resp = resp;
> >>  	dell_bl->resp_idx = 0;
> >> +	dell_bl->resp_len = -1; /* Invalid / unset */
> >>  	dell_bl->resp_max_len = resp_max_len;
> >>  	dell_bl->pending_cmd = cmd[1];
> >>  
> >> @@ -219,7 +219,7 @@ static size_t dell_uart_bl_receive(struct serdev_device *serdev, const u8 *data,
> >>  		return len;
> >>  	}
> >>  
> >> -	for (i = 0; i < len; i++) {
> >> +	for (i = 0; i < len && dell_bl->resp_idx != dell_bl->resp_len; i++, dell_bl->resp_idx++) {
> >>  		dell_bl->resp[dell_bl->resp_idx] = data[i];
> >>  
> >>  		switch (dell_bl->resp_idx) {
> >> @@ -228,46 +228,41 @@ static size_t dell_uart_bl_receive(struct serdev_device *serdev, const u8 *data,
> >>  			if (dell_bl->resp_len < MIN_RESP_LEN) {
> >>  				dev_err(dell_bl->dev, "Response length too small %d < %d\n",
> >>  					dell_bl->resp_len, MIN_RESP_LEN);
> >> -				dell_bl->status = -EIO;
> >> -				goto wakeup;
> >> +				goto error;
> >>  			}
> >>  
> >>  			if (dell_bl->resp_len > dell_bl->resp_max_len) {
> >>  				dev_err(dell_bl->dev, "Response length too big %d > %d\n",
> >>  					dell_bl->resp_len, dell_bl->resp_max_len);
> >> -				dell_bl->status = -EIO;
> >> -				goto wakeup;
> >> +				goto error;
> >>  			}
> >>  			break;
> >>  		case RESP_CMD: /* CMD byte */
> >>  			if (dell_bl->resp[RESP_CMD] != dell_bl->pending_cmd) {
> >>  				dev_err(dell_bl->dev, "Response cmd 0x%02x != pending 0x%02x\n",
> >>  					dell_bl->resp[RESP_CMD], dell_bl->pending_cmd);
> >> -				dell_bl->status = -EIO;
> >> -				goto wakeup;
> >> +				goto error;
> >>  			}
> >>  			break;
> >>  		}
> >> +	}
> >>  
> >> -		dell_bl->resp_idx++;
> >> -		if (dell_bl->resp_idx < dell_bl->resp_len)
> >> -			continue;
> >> -
> >> +	if (dell_bl->resp_idx == dell_bl->resp_len) {
> >>  		csum = dell_uart_checksum(dell_bl->resp, dell_bl->resp_len - 1);
> >>  		if (dell_bl->resp[dell_bl->resp_len - 1] != csum) {
> >>  			dev_err(dell_bl->dev, "Checksum mismatch got 0x%02x expected 0x%02x\n",
> >>  				dell_bl->resp[dell_bl->resp_len - 1], csum);
> >> -			dell_bl->status = -EIO;
> >> -			goto wakeup;
> >> +			goto error;
> >>  		}
> >> -
> >>  		dell_bl->status = 0; /* Success */
> >> -		goto wakeup;
> >> +		wake_up(&dell_bl->wait_queue);
> >> +		return i;
> >>  	}
> >>  
> >>  	return len;
> >>  
> >> -wakeup:
> >> +error:
> >> +	dell_bl->status = -EIO;
> >>  	wake_up(&dell_bl->wait_queue);
> >>  	return i + 1;
> >>  }
> > 
> > Thanks, this is way easier to follow.
> 
> I'm glad you like it.
> 
> There is a little bug in this version though, the goto error on the checksum fail
> case returns i + i, which should be i in that case, I'll just drop the goto there and
> instead always use the return i already present at the end of the
> "if (dell_bl->resp_idx == dell_bl->resp_len) { }" block.

It could have been solved more logically incrementing i and resp_idx here:

		dell_bl->resp[dell_bl->resp_idx] = data[i];
		dell_bl->resp_idx++;
		i++;

so that the inconsistent state is eliminated.

I also realized (I know I was the one who suggested it) that reverse logic 
would be better for the incomplete frame check:

	if (dell_bl->resp_idx < dell_bl->resp_len)
		return len;

	// checksum logic...

Perhaps the success and error return paths could then be merged too.

-- 
 i.

  reply	other threads:[~2024-05-13 13:35 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-12 16:23 [PATCH 0/2] platform/x86: Add new Dell UART backlight driver Hans de Goede
2024-05-12 16:23 ` [PATCH 1/2] " Hans de Goede
2024-05-12 19:35   ` Andy Shevchenko
2024-05-13 10:01     ` Hans de Goede
2024-05-13 12:28       ` Andy Shevchenko
2024-05-13 12:36         ` Ilpo Järvinen
2024-05-13  8:34   ` Ilpo Järvinen
2024-05-13  9:55     ` Hans de Goede
2024-05-13 12:12       ` Ilpo Järvinen
2024-05-13 12:14         ` Ilpo Järvinen
2024-05-13 13:07         ` Hans de Goede
2024-05-13 13:14           ` Ilpo Järvinen
2024-05-13 13:21             ` Hans de Goede
2024-05-13 13:34               ` Ilpo Järvinen [this message]
2024-05-13 14:23                 ` Hans de Goede
2024-05-13 14:36                   ` Ilpo Järvinen
2024-05-13 14:39                     ` Hans de Goede
2024-05-12 16:23 ` [PATCH 2/2] tools arch x86: Add dell-uart-backlight-emulator Hans de Goede
2024-05-12 19:32   ` Andy Shevchenko
2024-05-12 19:47     ` Hans de Goede
2024-05-13 11:03     ` Hans de Goede

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=7163ebe9-6cca-c21f-78b7-e6aeda2961b3@linux.intel.com \
    --to=ilpo.jarvinen@linux.intel.com \
    --cc=acelan.kao@canonical.com \
    --cc=andy@kernel.org \
    --cc=hdegoede@redhat.com \
    --cc=kai.heng.feng@canonical.com \
    --cc=platform-driver-x86@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