From nobody Mon Feb 9 10:27:40 2026 Received: from cae.in-ulm.de (cae.in-ulm.de [217.10.14.231]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A53E8745F7; Sun, 18 Feb 2024 22:21:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.10.14.231 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708294874; cv=none; b=S+HiwLZmMW3YM+yuSGka40XjkAGUzYWdu7b7Xb1hYksNINaUKBO4NiMxjodZGeDpmE49HyNPPieRxnuqgQqhyAcJTRRVSjkggGht45ru/H1Jnrv3p96h+TC2x8IwotvGuPe+nfFU/9mklMcEMrC2JeH9L99fmvj2epwxtFX1Ysg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708294874; c=relaxed/simple; bh=oBIAbCrIJVBjDvCpY+ciTU3Tk2iXKsY0b6d7anvoIAY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=UFeMDpJ2M7E3SMiMZsd7QZoYgd7KSIXnPjY6xtDkYNLPYFjrXwBiI5TTc5mDlzRDPzf2mtOVv6/Sh2lBef2/sCxRmmuDeASVXjY2H/+d6l+o2wli4En42hbO5UQzFqQpi75KEM+R3OS1ZCaLp/wPYoEdD887/aUSloXTmioi8+0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=c--e.de; spf=pass smtp.mailfrom=c--e.de; arc=none smtp.client-ip=217.10.14.231 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=c--e.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=c--e.de Received: by cae.in-ulm.de (Postfix, from userid 1000) id 0456414092E; Sun, 18 Feb 2024 23:21:07 +0100 (CET) From: "Christian A. Ehrhardt" To: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org Cc: "Christian A. Ehrhardt" , Heikki Krogerus , Greg Kroah-Hartman , Maxime Coquelin , Alexandre Torgue , =?UTF-8?q?Uwe=20Kleine-K=C3=B6nig?= , =?UTF-8?q?Samuel=20=C4=8Cavoj?= , Hans de Goede , Neil Armstrong , Prashanth K , Dmitry Baryshkov , Saranya Gopal , Haotien Hsu , Andy Shevchenko , Utkarsh Patel , Bjorn Andersson , Luca Weiss , Min-Hua Chen , Rob Herring , Rajaram Regupathy , linux-stm32@st-md-mailman.stormreply.com, linux-arm-kernel@lists.infradead.org, Abhishek Pandit-Subedi Subject: [RFC PATCH 5/6] usb: typec: ucsi: Introdcue ->read_data and ->write_data Date: Sun, 18 Feb 2024 23:20:38 +0100 Message-Id: <20240218222039.822040-6-lk@c--e.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20240218222039.822040-1-lk@c--e.de> References: <20240218222039.822040-1-lk@c--e.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" With the previous refactoring ->read() is only used to read from UCSI_MESSAGE_IN. So rename ->read() to ->read_data(). Before this change there is (at least for some backends) no way to write to UCSI_MESSAGE_OUT without starting a command. As a result it is not possible to send a command with additional data on these backends. Currently, the UCSI core does not send such commands but they are defined in the spec and presumably will be required. Introduce ->write_data() to write to UCSI_MESSAGE_OUT without starting a command. This is symmetrical to ->read_data(). Signed-off-by: Christian A. Ehrhardt --- drivers/usb/typec/ucsi/ucsi.c | 6 ++--- drivers/usb/typec/ucsi/ucsi.h | 7 ++--- drivers/usb/typec/ucsi/ucsi_acpi.c | 21 +++++++++++---- drivers/usb/typec/ucsi/ucsi_ccg.c | 18 ++++++++----- drivers/usb/typec/ucsi/ucsi_glink.c | 37 +++++++++++++++++---------- drivers/usb/typec/ucsi/ucsi_stm32g0.c | 30 +++++++++++++++++----- 6 files changed, 82 insertions(+), 37 deletions(-) diff --git a/drivers/usb/typec/ucsi/ucsi.c b/drivers/usb/typec/ucsi/ucsi.c index a4ae3d5966a0..df3fe04cb9cd 100644 --- a/drivers/usb/typec/ucsi/ucsi.c +++ b/drivers/usb/typec/ucsi/ucsi.c @@ -46,7 +46,7 @@ static int ucsi_read_message_in(struct ucsi *ucsi, void *= buf, if (ucsi->version <=3D UCSI_VERSION_1_2) buf_size =3D clamp(buf_size, 0, 16); =20 - return ucsi->ops->read(ucsi, UCSI_MESSAGE_IN, buf, buf_size); + return ucsi->ops->read_data(ucsi, buf, buf_size); } =20 static int ucsi_acknowledge_command(struct ucsi *ucsi) @@ -1548,8 +1548,8 @@ struct ucsi *ucsi_create(struct device *dev, const st= ruct ucsi_operations *ops) { struct ucsi *ucsi; =20 - if (!ops || !ops->poll_cci || !ops->read || !ops->sync_write || - !ops->async_write) + if (!ops || !ops->poll_cci || !ops->read_data || !ops->write_data || + !ops->sync_write || !ops->async_write) return ERR_PTR(-EINVAL); =20 ucsi =3D kzalloc(sizeof(*ucsi), GFP_KERNEL); diff --git a/drivers/usb/typec/ucsi/ucsi.h b/drivers/usb/typec/ucsi/ucsi.h index 55e5c5a09b32..2ad68124511b 100644 --- a/drivers/usb/typec/ucsi/ucsi.h +++ b/drivers/usb/typec/ucsi/ucsi.h @@ -56,7 +56,8 @@ struct dentry; /** * struct ucsi_operations - UCSI I/O operations * @poll_cci: Update the cached CCI value from hardware. Required for rese= t. - * @read: Read operation + * @read_data: Read MESSAGE_IN data + * @write_data: Write MESSAGE_OUT data * @sync_write: Blocking write operation * @async_write: Non-blocking write operation * @update_altmodes: Squashes duplicate DP altmodes @@ -67,8 +68,8 @@ struct dentry; */ struct ucsi_operations { int (*poll_cci)(struct ucsi *ucsi); - int (*read)(struct ucsi *ucsi, unsigned int offset, - void *val, size_t val_len); + int (*read_data)(struct ucsi *ucsi, void *val, size_t val_len); + int (*write_data)(struct ucsi *ucsi, const void *val, size_t val_len); int (*sync_write)(struct ucsi *ucsi, unsigned int offset, const void *val, size_t val_len); int (*async_write)(struct ucsi *ucsi, unsigned int offset, diff --git a/drivers/usb/typec/ucsi/ucsi_acpi.c b/drivers/usb/typec/ucsi/uc= si_acpi.c index 3aedfe78fb65..79b47b433e35 100644 --- a/drivers/usb/typec/ucsi/ucsi_acpi.c +++ b/drivers/usb/typec/ucsi/ucsi_acpi.c @@ -58,12 +58,21 @@ static int ucsi_acpi_poll_cci(struct ucsi *ucsi) return 0; } =20 -static int ucsi_acpi_read(struct ucsi *ucsi, unsigned int offset, - void *val, size_t val_len) +static int ucsi_acpi_read_data(struct ucsi *ucsi, void *val, size_t val_le= n) { struct ucsi_acpi *ua =3D ucsi_get_drvdata(ucsi); =20 - memcpy(val, ua->base + offset, val_len); + memcpy(val, ua->base + UCSI_MESSAGE_IN, val_len); + + return 0; +} + +static int ucsi_acpi_write_data(struct ucsi *ucsi, const void *val, + size_t val_len) +{ + struct ucsi_acpi *ua =3D ucsi_get_drvdata(ucsi); + + memcpy(ua->base + UCSI_MESSAGE_OUT, val, val_len); =20 return 0; } @@ -108,7 +117,8 @@ static int ucsi_acpi_sync_write(struct ucsi *ucsi, unsi= gned int offset, =20 static const struct ucsi_operations ucsi_acpi_ops =3D { .poll_cci =3D ucsi_acpi_poll_cci, - .read =3D ucsi_acpi_read, + .read_data =3D ucsi_acpi_read_data, + .write_data =3D ucsi_acpi_write_data, .sync_write =3D ucsi_acpi_sync_write, .async_write =3D ucsi_acpi_async_write }; @@ -163,7 +173,8 @@ ucsi_dell_sync_write(struct ucsi *ucsi, unsigned int of= fset, =20 static const struct ucsi_operations ucsi_dell_ops =3D { .poll_cci =3D ucsi_acpi_poll_cci, - .read =3D ucsi_acpi_read, + .read_data =3D ucsi_acpi_read_data, + .write_data =3D ucsi_acpi_write_data, .sync_write =3D ucsi_dell_sync_write, .async_write =3D ucsi_acpi_async_write }; diff --git a/drivers/usb/typec/ucsi/ucsi_ccg.c b/drivers/usb/typec/ucsi/ucs= i_ccg.c index 55d0fe5324f4..d6026f61a0ed 100644 --- a/drivers/usb/typec/ucsi/ucsi_ccg.c +++ b/drivers/usb/typec/ucsi/ucsi_ccg.c @@ -559,16 +559,12 @@ static int ucsi_ccg_poll_cci(struct ucsi *ucsi) return 0; } =20 -static int ucsi_ccg_read(struct ucsi *ucsi, unsigned int offset, - void *val, size_t val_len) +static int ucsi_ccg_read_data(struct ucsi *ucsi, void *val, size_t val_len) { struct ucsi_ccg *uc =3D ucsi_get_drvdata(ucsi); struct ucsi_capability *cap; struct ucsi_altmode *alt; =20 - if (offset !=3D UCSI_MESSAGE_IN) - return -EINVAL; - spin_lock(&uc->op_lock); memcpy(val, &uc->op_data.message_in, val_len); spin_unlock(&uc->op_lock); @@ -600,6 +596,15 @@ static int ucsi_ccg_read(struct ucsi *ucsi, unsigned i= nt offset, return 0; } =20 +static int ucsi_ccg_write_data(struct ucsi *ucsi, const void *val, + size_t val_len) +{ + struct ucsi_ccg *uc =3D ucsi_get_drvdata(ucsi); + u16 reg =3D CCGX_RAB_UCSI_DATA_BLOCK(UCSI_MESSAGE_OUT); + + return ccg_write(uc, reg, val, val_len); +} + static int ucsi_ccg_async_write(struct ucsi *ucsi, unsigned int offset, const void *val, size_t val_len) { @@ -656,7 +661,8 @@ static int ucsi_ccg_sync_write(struct ucsi *ucsi, unsig= ned int offset, =20 static const struct ucsi_operations ucsi_ccg_ops =3D { .poll_cci =3D ucsi_ccg_poll_cci, - .read =3D ucsi_ccg_read, + .read_data =3D ucsi_ccg_read_data, + .write_data =3D ucsi_ccg_write_data, .sync_write =3D ucsi_ccg_sync_write, .async_write =3D ucsi_ccg_async_write, .update_altmodes =3D ucsi_ccg_update_altmodes diff --git a/drivers/usb/typec/ucsi/ucsi_glink.c b/drivers/usb/typec/ucsi/u= csi_glink.c index d65ab81b4ed6..9dab1b428ad9 100644 --- a/drivers/usb/typec/ucsi/ucsi_glink.c +++ b/drivers/usb/typec/ucsi/ucsi_glink.c @@ -75,6 +75,7 @@ struct pmic_glink_ucsi { struct work_struct register_work; =20 u8 read_buf[UCSI_BUF_SIZE]; + u8 write_buf[UCSI_BUF_SIZE]; }; =20 static int pmic_glink_sync_read_buf(struct pmic_glink_ucsi *ucsi) @@ -129,18 +130,28 @@ static int pmic_glink_ucsi_poll_cci(struct ucsi *__uc= si) return 0; } =20 -static int pmic_glink_ucsi_read(struct ucsi *__ucsi, unsigned int offset, - void *val, size_t val_len) +static int pmic_glink_ucsi_read_data(struct ucsi *__ucsi, + void *val, size_t val_len) { struct pmic_glink_ucsi *ucsi =3D ucsi_get_drvdata(__ucsi); =20 mutex_lock(&ucsi->lock); - memcpy(val, &ucsi->read_buf[offset], val_len); + memcpy(val, &ucsi->read_buf[UCSI_MESSAGE_IN], val_len); mutex_unlock(&ucsi->lock); =20 return 0; } =20 +static int pmic_glink_ucsi_write_data(struct ucsi *__ucsi, + const void *val, size_t val_len) +{ + struct pmic_glink_ucsi *ucsi =3D ucsi_get_drvdata(__ucsi); + + memcpy(&ucsi->write_buf[UCSI_MESSAGE_OUT], val, val_len); + + return 0; +} + static int pmic_glink_ucsi_locked_write(struct pmic_glink_ucsi *ucsi, unsi= gned int offset, const void *val, size_t val_len) { @@ -148,10 +159,15 @@ static int pmic_glink_ucsi_locked_write(struct pmic_g= link_ucsi *ucsi, unsigned i unsigned long left; int ret; =20 + if (offset !=3D UCSI_CONTROL || val_len !=3D sizeof(u64)) + return -EINVAL; + memcpy(ucsi->write_buf + UCSI_CONTROL, val, val_len); + req.hdr.owner =3D cpu_to_le32(PMIC_GLINK_OWNER_USBC); req.hdr.type =3D cpu_to_le32(MSG_TYPE_REQ_RESP); req.hdr.opcode =3D cpu_to_le32(UC_UCSI_WRITE_BUF_REQ); - memcpy(&req.buf[offset], val, val_len); + memcpy(req.buf, ucsi->write_buf, UCSI_BUF_SIZE); + memset(ucsi->write_buf, 0, sizeof(ucsi->write_buf)); =20 reinit_completion(&ucsi->write_ack); =20 @@ -214,7 +230,8 @@ static int pmic_glink_ucsi_sync_write(struct ucsi *__uc= si, unsigned int offset, =20 static const struct ucsi_operations pmic_glink_ucsi_ops =3D { .poll_cci =3D pmic_glink_ucsi_poll_cci, - .read =3D pmic_glink_ucsi_read, + .read_data =3D pmic_glink_ucsi_read_data, + .write_data =3D pmic_glink_ucsi_write_data, .sync_write =3D pmic_glink_ucsi_sync_write, .async_write =3D pmic_glink_ucsi_async_write }; @@ -286,7 +303,6 @@ static void pmic_glink_ucsi_notify(struct work_struct *= work) static void pmic_glink_ucsi_register(struct work_struct *work) { struct pmic_glink_ucsi *ucsi =3D container_of(work, struct pmic_glink_ucs= i, register_work); - __le16 version; int ret; =20 ret =3D pmic_glink_sync_read_buf(ucsi); @@ -294,14 +310,9 @@ static void pmic_glink_ucsi_register(struct work_struc= t *work) dev_err(ucsi->dev, "cannot sync read buf: %d\n", ret); return; } - ret =3D pmic_glink_ucsi_read(ucsi->ucsi, UCSI_VERSION, &version, - sizeof(version)); - if (ret < 0) { - dev_err(ucsi->dev, "cannot read version: %d\n", ret); - return; - } =20 - ucsi_register(ucsi->ucsi, le16_to_cpu(version)); + ucsi_register(ucsi->ucsi, + le16_to_cpu(*(__le16 *)(ucsi->read_buf + UCSI_VERSION))); } =20 static void pmic_glink_ucsi_callback(const void *data, size_t len, void *p= riv) diff --git a/drivers/usb/typec/ucsi/ucsi_stm32g0.c b/drivers/usb/typec/ucsi= /ucsi_stm32g0.c index bab2363b73f4..d68aca118e41 100644 --- a/drivers/usb/typec/ucsi/ucsi_stm32g0.c +++ b/drivers/usb/typec/ucsi/ucsi_stm32g0.c @@ -372,18 +372,17 @@ static int ucsi_stm32g0_poll_cci(struct ucsi *ucsi) return 0; } =20 -static int ucsi_stm32g0_read(struct ucsi *ucsi, unsigned int offset, - void *val, size_t len) +static int ucsi_stm32g0_read_data(struct ucsi *ucsi, void *val, size_t len) { struct ucsi_stm32g0 *g0 =3D ucsi_get_drvdata(ucsi); =20 - return ucsi_stm32g0_read_from_hw(g0, offset, val, len); + return ucsi_stm32g0_read_from_hw(g0, UCSI_MESSAGE_IN, val, len); } =20 -static int ucsi_stm32g0_async_write(struct ucsi *ucsi, unsigned int offset= , const void *val, - size_t len) +static int ucsi_stm32g0_write_to_hw(struct ucsi_stm32g0 *g0, + unsigned int offset, + const void *val, size_t len) { - struct ucsi_stm32g0 *g0 =3D ucsi_get_drvdata(ucsi); struct i2c_client *client =3D g0->client; struct i2c_msg msg[] =3D { { @@ -414,6 +413,22 @@ static int ucsi_stm32g0_async_write(struct ucsi *ucsi,= unsigned int offset, cons return 0; } =20 +static int ucsi_stm32g0_write_data(struct ucsi *ucsi, + const void *val, size_t len) +{ + struct ucsi_stm32g0 *g0 =3D ucsi_get_drvdata(ucsi); + + return ucsi_stm32g0_write_to_hw(g0, UCSI_MESSAGE_OUT, val, len); +} + +static int ucsi_stm32g0_async_write(struct ucsi *ucsi, unsigned int offset, + const void *val, size_t len) +{ + struct ucsi_stm32g0 *g0 =3D ucsi_get_drvdata(ucsi); + + return ucsi_stm32g0_write_to_hw(g0, offset, val, len); +} + static int ucsi_stm32g0_sync_write(struct ucsi *ucsi, unsigned int offset,= const void *val, size_t len) { @@ -463,7 +478,8 @@ static irqreturn_t ucsi_stm32g0_irq_handler(int irq, vo= id *data) =20 static const struct ucsi_operations ucsi_stm32g0_ops =3D { .poll_cci =3D ucsi_stm32g0_poll_cci, - .read =3D ucsi_stm32g0_read, + .read_data =3D ucsi_stm32g0_read_data, + .write_data =3D ucsi_stm32g0_write_data, .sync_write =3D ucsi_stm32g0_sync_write, .async_write =3D ucsi_stm32g0_async_write, }; --=20 2.40.1