mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Christian A. Ehrhardt" <lk@c--e.de>
To: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: "Christian A. Ehrhardt" <lk@c--e.de>,
	"Heikki Krogerus" <heikki.krogerus@linux.intel.com>,
	"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	"Maxime Coquelin" <mcoquelin.stm32@gmail.com>,
	"Alexandre Torgue" <alexandre.torgue@foss.st.com>,
	"Uwe Kleine-König" <u.kleine-koenig@pengutronix.de>,
	"Samuel Čavoj" <samuel@cavoj.net>,
	"Hans de Goede" <hdegoede@redhat.com>,
	"Neil Armstrong" <neil.armstrong@linaro.org>,
	"Prashanth K" <quic_prashk@quicinc.com>,
	"Dmitry Baryshkov" <dmitry.baryshkov@linaro.org>,
	"Saranya Gopal" <saranya.gopal@intel.com>,
	"Haotien Hsu" <haotienh@nvidia.com>,
	"Andy Shevchenko" <andriy.shevchenko@linux.intel.com>,
	"Utkarsh Patel" <utkarsh.h.patel@intel.com>,
	"Bjorn Andersson" <andersson@kernel.org>,
	"Luca Weiss" <luca.weiss@fairphone.com>,
	"Min-Hua Chen" <minhuadotchen@gmail.com>,
	"Rob Herring" <robh@kernel.org>,
	"Rajaram Regupathy" <rajaram.regupathy@intel.com>,
	linux-stm32@st-md-mailman.stormreply.com,
	linux-arm-kernel@lists.infradead.org,
	"Abhishek Pandit-Subedi" <abhishekpandit@chromium.org>
Subject: [RFC PATCH 2/6] ucsi_ccg: Cleanup endianness confusion
Date: Sun, 18 Feb 2024 23:20:35 +0100	[thread overview]
Message-ID: <20240218222039.822040-3-lk@c--e.de> (raw)
In-Reply-To: <20240218222039.822040-1-lk@c--e.de>

When storing the cci value in the op_region struct it is
converted from host to little endian. However, the value is
read from hardware that is little endian according to the spec
and it is never converted to host byte order. However, the
value is used as if it where in host byte order.

Additionally, the message_in buffer is a byte array.
Any endian interpretation depends on the current command and
must be done in the ocntext of that command.

While all the UCSI world seems to be little endian and there
are many other endian issues if this is not true, this
particular value is treated with endian awareness, so it should
at least be done correctly.

Add the missing conversion from little endian to host byte order
when reading the CCI value from hardware. Additionally, make the
message_in buffer an u8 array and adjust the size macro accordingly.

Signed-off-by: Christian A. Ehrhardt <lk@c--e.de>
---
 drivers/usb/typec/ucsi/ucsi_ccg.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/drivers/usb/typec/ucsi/ucsi_ccg.c b/drivers/usb/typec/ucsi/ucsi_ccg.c
index dda7c7c94e08..709295948c65 100644
--- a/drivers/usb/typec/ucsi/ucsi_ccg.c
+++ b/drivers/usb/typec/ucsi/ucsi_ccg.c
@@ -192,10 +192,10 @@ struct ucsi_ccg_altmode {
 	bool checked;
 } __packed;
 
-#define CCGX_MESSAGE_IN_MAX 4
+#define CCGX_MESSAGE_IN_MAX 16
 struct op_region {
 	__le32 cci;
-	__le32 message_in[CCGX_MESSAGE_IN_MAX];
+	u8 message_in[CCGX_MESSAGE_IN_MAX];
 };
 
 struct ucsi_ccg {
@@ -678,6 +678,7 @@ static irqreturn_t ccg_irq_handler(int irq, void *data)
 	u16 reg = CCGX_RAB_UCSI_DATA_BLOCK(UCSI_CCI);
 	struct ucsi_ccg *uc = data;
 	u8 intr_reg;
+	__le32 __cci;
 	u32 cci = 0;
 	int ret = 0;
 
@@ -690,9 +691,10 @@ static irqreturn_t ccg_irq_handler(int irq, void *data)
 	else if (!(intr_reg & UCSI_READ_INT))
 		goto err_clear_irq;
 
-	ret = ccg_read(uc, reg, (void *)&cci, sizeof(cci));
+	ret = ccg_read(uc, reg, (void *)&__cci, sizeof(__cci));
 	if (ret)
 		goto err_clear_irq;
+	cci = le32_to_cpu(__cci);
 
 	if (UCSI_CCI_CONNECTOR(cci))
 		ucsi_connector_change(uc->ucsi, UCSI_CCI_CONNECTOR(cci));
-- 
2.40.1


  parent reply	other threads:[~2024-02-18 22:21 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-18 22:20 [RFC PATCH 0/6] UCSI backend API refactoring Christian A. Ehrhardt
2024-02-18 22:20 ` [RFC PATCH 1/6] usb: ucsi_glink: Fix endianness issues Christian A. Ehrhardt
2024-02-18 22:20 ` Christian A. Ehrhardt [this message]
2024-02-18 22:20 ` [RFC PATCH 3/6] usb: typec: ucsi: Make Version a parameter to ucsi_register Christian A. Ehrhardt
2024-02-18 22:20 ` [RFC PATCH 4/6] usb: typec: ucsi: Cache current CCI value in struct ucsi Christian A. Ehrhardt
2024-02-18 22:20 ` [RFC PATCH 5/6] usb: typec: ucsi: Introdcue ->read_data and ->write_data Christian A. Ehrhardt
2024-02-18 22:20 ` [RFC PATCH 6/6] usb: typec: ucsi: Convert a?sync_write to a?sync_cmd Christian A. Ehrhardt

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=20240218222039.822040-3-lk@c--e.de \
    --to=lk@c--e.de \
    --cc=abhishekpandit@chromium.org \
    --cc=alexandre.torgue@foss.st.com \
    --cc=andersson@kernel.org \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=dmitry.baryshkov@linaro.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=haotienh@nvidia.com \
    --cc=hdegoede@redhat.com \
    --cc=heikki.krogerus@linux.intel.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-stm32@st-md-mailman.stormreply.com \
    --cc=linux-usb@vger.kernel.org \
    --cc=luca.weiss@fairphone.com \
    --cc=mcoquelin.stm32@gmail.com \
    --cc=minhuadotchen@gmail.com \
    --cc=neil.armstrong@linaro.org \
    --cc=quic_prashk@quicinc.com \
    --cc=rajaram.regupathy@intel.com \
    --cc=robh@kernel.org \
    --cc=samuel@cavoj.net \
    --cc=saranya.gopal@intel.com \
    --cc=u.kleine-koenig@pengutronix.de \
    --cc=utkarsh.h.patel@intel.com \
    /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

all inboxes | Powered by JetHome®