From nobody Mon Feb 13 21:09:48 2023 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4PFxmr3qJ8z3q5WS; Mon, 13 Feb 2023 21:09:48 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4PFxmr336zz4Rrh; Mon, 13 Feb 2023 21:09:48 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1676322588; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=UdMG7b7PhKK2Q0PzHZ13GmPU4b5Kjvaua6aeFwkjMg8=; b=h1onaX1XbBSBuVgDWPP7BWWNJe9hZaem0d/1lnUDDEoSpGcAnzA+gU6RH/Zl/2WzIv2yk3 YAyz+k7LLCx6+sFnZUTqLNyrvWYI/kcl1Bl3VlgulnkTt9iH7z5fagLx1XmUqXmH/m2NvC me8zWEpT+KorPXCEHcz311bdiAdecRxAMkBFplcYS60KnzGOjnchQwEP8SOo86zZcaAJ07 FP6C8CjkaWYnFrXDrfyBc7LoQm7sFLnyMrwCUHVnYxjS6OQlXWkdelSKoXiHvGJ3HjVrN6 BdmwVr4BBlHuVzbVrfaBjeMypgKwmQ7VI+Gm8v6Rqj8Iklaw/MTvz0gtL6bFKw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1676322588; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=UdMG7b7PhKK2Q0PzHZ13GmPU4b5Kjvaua6aeFwkjMg8=; b=uZcfJKzR8oRlA/nC4KK+LuSimcM3TQJjLTDBkuXritpuq4bAHDw4uC20Ex13r2bPKVcrnU Ax/8eNzLZxvcYJDKjo2F5fjh2l6REYRcr6omhf7PBvGvshbH11vNq+VEg9IZNMyW6L1dQd cdkwUn0LbYm3Ath1E7aYaCO+/hDC3/+yAme2pcp2DWpNY1rxFz8f031V7U+WHc0FWUlKHI 7T0Kh/b5OgEJdPGtU2SVWRFBVM/wyU2Xd9g2K7w3kkpatpygMVlaltE8UWZ6VFdQRMnW0M 4z/bWgJBClZUANiSvPk3+dvOP6trnHZ9zjENYOc8KEf2IkjgnFu6Ouoe4eeLSw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1676322588; a=rsa-sha256; cv=none; b=tV+Oc7ewy6K5hr+gj29uvrKTzgu6c2I40x9shWQayfP3ngYCfviMYGizNs6Fh/KncIL7bN DkyQWOqy8O6O4r+zfgpjS0Biwk4AsPp1Hf5F/wwBX67bOxEFfzw4qSz1dycQOsiri2id5Q sFD6D0BybPbNgVGjifsSlwMkZgW0c6hAX8VJUSc+KrwXoxnKhpigiBV1BVZcI6UnJyA1U0 XipXZs7aWwo7CXLMxCpED3bnIXThCdbxqeyNyOaI4j9GQOZWj9qoRC873UovFPN9rClEPK dMsP6Oub4xk84jQdFxLMsDxwJYm8Xcxvq++lFLHyn7x5w4lAtVihe8wK5iDosg== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4PFxmr27xHzY4x; Mon, 13 Feb 2023 21:09:48 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 31DL9mL2056386; Mon, 13 Feb 2023 21:09:48 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 31DL9mYe056385; Mon, 13 Feb 2023 21:09:48 GMT (envelope-from git) Date: Mon, 13 Feb 2023 21:09:48 GMT Message-Id: <202302132109.31DL9mYe056385@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: =?utf-8?Q?Jean-S=C3=A9bastien=20P=C3=A9dron?= Subject: git: 83276e1f952d - main - linuxkpi: Add `i2c_adapter_quirks` support List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: dumbbell X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 83276e1f952d720c23092ea17e95c03fef8476b7 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by dumbbell (ports committer): URL: https://cgit.FreeBSD.org/src/commit/?id=83276e1f952d720c23092ea17e95c03fef8476b7 commit 83276e1f952d720c23092ea17e95c03fef8476b7 Author: Jean-Sébastien Pédron AuthorDate: 2023-02-08 17:57:32 +0000 Commit: Jean-Sébastien Pédron CommitDate: 2023-02-13 21:09:31 +0000 linuxkpi: Add `i2c_adapter_quirks` support While here, also declare `I2C_CLASS_HWMON`. Reviewed by: manu Approved by: manu Differential Revision: https://reviews.freebsd.org/D38535 --- sys/compat/linuxkpi/common/include/linux/i2c.h | 25 ++++++ sys/compat/linuxkpi/common/src/linux_i2c.c | 113 +++++++++++++++++++++++++ 2 files changed, 138 insertions(+) diff --git a/sys/compat/linuxkpi/common/include/linux/i2c.h b/sys/compat/linuxkpi/common/include/linux/i2c.h index 365ab893fdfd..f24d282586f6 100644 --- a/sys/compat/linuxkpi/common/include/linux/i2c.h +++ b/sys/compat/linuxkpi/common/include/linux/i2c.h @@ -46,6 +46,7 @@ #define I2C_FUNC_SMBUS_BLOCK_PROC_CALL 0 #define I2C_FUNC_10BIT_ADDR 0 +#define I2C_CLASS_HWMON 0x1 #define I2C_CLASS_DDC 0x8 #define I2C_CLASS_SPD 0x80 @@ -58,6 +59,7 @@ struct i2c_adapter { const struct i2c_lock_operations *lock_ops; const struct i2c_algorithm *algo; + const struct i2c_adapter_quirks *quirks; void *algo_data; int retries; @@ -82,6 +84,29 @@ struct i2c_lock_operations { void (*unlock_bus)(struct i2c_adapter *, unsigned int); }; +struct i2c_adapter_quirks { + uint64_t flags; + int max_num_msgs; + uint16_t max_write_len; + uint16_t max_read_len; + uint16_t max_comb_1st_msg_len; + uint16_t max_comb_2nd_msg_len; +}; + +#define I2C_AQ_COMB BIT(0) +#define I2C_AQ_COMB_WRITE_FIRST BIT(1) +#define I2C_AQ_COMB_READ_SECOND BIT(2) +#define I2C_AQ_COMB_SAME_ADDR BIT(3) +#define I2C_AQ_COMB_WRITE_THEN_READ \ + (I2C_AQ_COMB | I2C_AQ_COMB_WRITE_FIRST | \ + I2C_AQ_COMB_READ_SECOND | I2C_AQ_COMB_SAME_ADDR) +#define I2C_AQ_NO_CLK_STRETCH BIT(4) +#define I2C_AQ_NO_ZERO_LEN_READ BIT(5) +#define I2C_AQ_NO_ZERO_LEN_WRITE BIT(6) +#define I2C_AQ_NO_ZERO_LEN \ + (I2C_AQ_NO_ZERO_LEN_READ | I2C_AQ_NO_ZERO_LEN_WRITE) +#define I2C_AQ_NO_REP_START BIT(7) + int lkpi_i2c_add_adapter(struct i2c_adapter *adapter); int lkpi_i2c_del_adapter(struct i2c_adapter *adapter); diff --git a/sys/compat/linuxkpi/common/src/linux_i2c.c b/sys/compat/linuxkpi/common/src/linux_i2c.c index 72ad37919d7f..253482b52550 100644 --- a/sys/compat/linuxkpi/common/src/linux_i2c.c +++ b/sys/compat/linuxkpi/common/src/linux_i2c.c @@ -167,6 +167,116 @@ lkpi_i2c_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr) return (0); } +static int i2c_check_for_quirks(struct i2c_adapter *adapter, + struct iic_msg *msgs, uint32_t nmsgs) +{ + const struct i2c_adapter_quirks *quirks; + device_t dev; + int i, max_nmsgs; + bool check_len; + + dev = adapter->dev.parent->bsddev; + quirks = adapter->quirks; + if (quirks == NULL) + return (0); + + check_len = true; + max_nmsgs = quirks->max_num_msgs; + + if (quirks->flags & I2C_AQ_COMB) { + max_nmsgs = 2; + + if (nmsgs == 2) { + if (quirks->flags & I2C_AQ_COMB_WRITE_FIRST && + msgs[0].flags & IIC_M_RD) { + device_printf(dev, + "Error: " + "first combined message must be write\n"); + return (EOPNOTSUPP); + } + if (quirks->flags & I2C_AQ_COMB_READ_SECOND && + !(msgs[1].flags & IIC_M_RD)) { + device_printf(dev, + "Error: " + "second combined message must be read\n"); + return (EOPNOTSUPP); + } + + if (quirks->flags & I2C_AQ_COMB_SAME_ADDR && + msgs[0].slave != msgs[1].slave) { + device_printf(dev, + "Error: " + "combined message must be use the same " + "address\n"); + return (EOPNOTSUPP); + } + + if (quirks->max_comb_1st_msg_len && + msgs[0].len > quirks->max_comb_1st_msg_len) { + device_printf(dev, + "Error: " + "message too long: %hu > %hu max\n", + msgs[0].len, + quirks->max_comb_1st_msg_len); + return (EOPNOTSUPP); + } + if (quirks->max_comb_2nd_msg_len && + msgs[1].len > quirks->max_comb_2nd_msg_len) { + device_printf(dev, + "Error: " + "message too long: %hu > %hu max\n", + msgs[1].len, + quirks->max_comb_2nd_msg_len); + return (EOPNOTSUPP); + } + + check_len = false; + } + } + + if (max_nmsgs && nmsgs > max_nmsgs) { + device_printf(dev, + "Error: too many messages: %d > %d max\n", + nmsgs, max_nmsgs); + return (EOPNOTSUPP); + } + + for (i = 0; i < nmsgs; i++) { + if (msgs[i].flags & IIC_M_RD) { + if (check_len && quirks->max_read_len && + msgs[i].len > quirks->max_read_len) { + device_printf(dev, + "Error: " + "message %d too long: %hu > %hu max\n", + i, msgs[i].len, quirks->max_read_len); + return (EOPNOTSUPP); + } + if (quirks->flags & I2C_AQ_NO_ZERO_LEN_READ && + msgs[i].len == 0) { + device_printf(dev, + "Error: message %d of length 0\n", i); + return (EOPNOTSUPP); + } + } else { + if (check_len && quirks->max_write_len && + msgs[i].len > quirks->max_write_len) { + device_printf(dev, + "Message %d too long: %hu > %hu max\n", + i, msgs[i].len, quirks->max_write_len); + return (EOPNOTSUPP); + } + if (quirks->flags & I2C_AQ_NO_ZERO_LEN_WRITE && + msgs[i].len == 0) { + device_printf(dev, + "Error: message %d of length 0\n", i); + return (EOPNOTSUPP); + } + } + } + + return (0); +} + static int lkpi_i2c_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs) { @@ -177,6 +287,9 @@ lkpi_i2c_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs) sc = device_get_softc(dev); if (sc->adapter == NULL) return (ENXIO); + ret = i2c_check_for_quirks(sc->adapter, msgs, nmsgs); + if (ret != 0) + return (ret); linux_set_current(curthread); linux_msgs = malloc(sizeof(struct i2c_msg) * nmsgs,