diff mbox series

[v2,1/4] i2c: smbus: add core function handling SMBus host-notify

Message ID 1593070769-9106-2-git-send-email-alain.volmat@st.com (mailing list archive)
State New, archived
Headers show
Series stm32-f7: Addition of SMBus Alert / Host-notify features | expand

Commit Message

Alain Volmat June 25, 2020, 7:39 a.m. UTC
SMBus Host-Notify protocol, from the adapter point of view
consist of receiving a message from a client, including the
client address and some other data.

It can be simply handled by creating a new slave device
and registering a callback performing the parsing of the
message received from the client.

This commit introduces two new core functions
  * i2c_new_smbus_host_notify_device
  * i2c_free_smbus_host_notify_device
that take care of registration of the new slave device and
callback and will call i2c_handle_smbus_host_notify once a
Host-Notify event is received.

Signed-off-by: Alain Volmat <alain.volmat@st.com>
---
v2: remove useless dev_err message in case of hnotify handling error
    prevent handling hnotify in case of a incomplete write

 drivers/i2c/i2c-core-smbus.c | 110 +++++++++++++++++++++++++++++++++++++++++++
 include/linux/i2c-smbus.h    |   2 +
 2 files changed, 112 insertions(+)

Comments

Wolfram Sang July 1, 2020, 10:49 a.m. UTC | #1
On Thu, Jun 25, 2020 at 09:39:26AM +0200, Alain Volmat wrote:
> SMBus Host-Notify protocol, from the adapter point of view
> consist of receiving a message from a client, including the
> client address and some other data.
> 
> It can be simply handled by creating a new slave device
> and registering a callback performing the parsing of the
> message received from the client.
> 
> This commit introduces two new core functions
>   * i2c_new_smbus_host_notify_device
>   * i2c_free_smbus_host_notify_device
> that take care of registration of the new slave device and
> callback and will call i2c_handle_smbus_host_notify once a
> Host-Notify event is received.
> 
> Signed-off-by: Alain Volmat <alain.volmat@st.com>
> ---
> v2: remove useless dev_err message in case of hnotify handling error
>     prevent handling hnotify in case of a incomplete write

Okay, now I got it to work, I also noted a few more issues.

First, I'd suggest s/i2c_smbus_host_notify/i2c_slave_host_notify/g for
all occurences in this patch. This makes a stronger distinction between
the generic HostNotify support and the slave specific one.

Also, I wonder if this shouldn't go to i2c-smbus.c instead but I haven't
checked if we end up in dependency hell then. Second best thought: at
least move to i2c-core-slave.c, then we could save the #ifdeffery in the
c-file?

> 
>  drivers/i2c/i2c-core-smbus.c | 110 +++++++++++++++++++++++++++++++++++++++++++
>  include/linux/i2c-smbus.h    |   2 +
>  2 files changed, 112 insertions(+)
> 
> diff --git a/drivers/i2c/i2c-core-smbus.c b/drivers/i2c/i2c-core-smbus.c
> index 56bb840142e3..3a37664fb5f6 100644
> --- a/drivers/i2c/i2c-core-smbus.c
> +++ b/drivers/i2c/i2c-core-smbus.c
> @@ -708,3 +708,113 @@ int of_i2c_setup_smbus_alert(struct i2c_adapter *adapter)
>  }
>  EXPORT_SYMBOL_GPL(of_i2c_setup_smbus_alert);
>  #endif
> +
> +#if IS_ENABLED(CONFIG_I2C_SLAVE)
> +struct i2c_smbus_host_notify_status {
> +	bool notify_start;
> +	u8 addr;
> +};
> +
> +static int i2c_smbus_host_notify_cb(struct i2c_client *client,
> +				    enum i2c_slave_event event, u8 *val)
> +{
> +	struct i2c_smbus_host_notify_status *status = client->dev.platform_data;
> +	int ret;
> +
> +	switch (event) {
> +	case I2C_SLAVE_WRITE_REQUESTED:
> +		status->notify_start = true;
> +		break;
> +	case I2C_SLAVE_WRITE_RECEIVED:
> +		/* We only retrieve the first byte received (addr)
> +		 * since there is currently no support to retrieve the data
> +		 * parameter from the client.
> +		 */
> +		if (!status->notify_start)
> +			break;
> +		status->addr = *val;
> +		status->notify_start = false;

So, we are safe if the message is too short. Otherwise, we capture the
first byte (== address) only, right. Further bytes until STOP are
discarded. So, we don't check if the message is too long and contains
more than the status word. Maybe we should add that?

> +		break;
> +	case I2C_SLAVE_STOP:
> +		/* In case of incomplete write, don't handle host-notify */
> +		if (status->notify_start) {
> +			status->notify_start = false;
> +			break;
> +		}
> +
> +		ret = i2c_handle_smbus_host_notify(client->adapter,
> +						   status->addr);
> +		if (ret < 0)
> +			return ret;
> +		break;
> +	default:

The missing cases are mandatory. From my testunit driver:

        case I2C_SLAVE_READ_REQUESTED:
        case I2C_SLAVE_READ_PROCESSED:
                *val = 0xff;
                break;

> +		/* Only handle necessary events */
> +		break;
> +	}
> +
> +	return 0;
> +}
> +

...

> --- a/include/linux/i2c-smbus.h
> +++ b/include/linux/i2c-smbus.h
> @@ -38,6 +38,8 @@ static inline int of_i2c_setup_smbus_alert(struct i2c_adapter *adap)
>  	return 0;
>  }
>  #endif
> +struct i2c_client *i2c_new_smbus_host_notify_device(struct i2c_adapter *adapter);
> +void i2c_free_smbus_host_notify_device(struct i2c_client *client);

Those need to be guarded with I2C_SLAVE as well. And an #else branch
with empty/successful placeholders.

>  
>  #if IS_ENABLED(CONFIG_I2C_SMBUS) && IS_ENABLED(CONFIG_DMI)
>  void i2c_register_spd(struct i2c_adapter *adap);
> -- 
> 2.7.4
>
Alain Volmat July 2, 2020, 11:23 a.m. UTC | #2
Hi Wolfram,

> Okay, now I got it to work, I also noted a few more issues.
> 
> First, I'd suggest s/i2c_smbus_host_notify/i2c_slave_host_notify/g for
> all occurences in this patch. This makes a stronger distinction between
> the generic HostNotify support and the slave specific one.

Ok with that.

> Also, I wonder if this shouldn't go to i2c-smbus.c instead but I haven't
> checked if we end up in dependency hell then. Second best thought: at
> least move to i2c-core-slave.c, then we could save the #ifdeffery in the
> c-file?

I have actually difficulties to understand clearly what should go within
i2c-smbus.c vs i2c-core-smbus or i2c-core-slave. My feeling is that
i2c-core-slave is more about the registering of a slave rather than one usage
of the slave mechanism. Hence I am not sure those functions/cb belong there.
But at the same time, I don't know about i2c-smbus vs i2c-core-smbus. I putted
it within i2c-core-smbus considering that the creation of the alert device
is also done there.

....

> > +{
> > +	struct i2c_smbus_host_notify_status *status = client->dev.platform_data;
> > +	int ret;
> > +
> > +	switch (event) {
> > +	case I2C_SLAVE_WRITE_REQUESTED:
> > +		status->notify_start = true;
> > +		break;
> > +	case I2C_SLAVE_WRITE_RECEIVED:
> > +		/* We only retrieve the first byte received (addr)
> > +		 * since there is currently no support to retrieve the data
> > +		 * parameter from the client.
> > +		 */
> > +		if (!status->notify_start)
> > +			break;
> > +		status->addr = *val;
> > +		status->notify_start = false;
> 
> So, we are safe if the message is too short. Otherwise, we capture the
> first byte (== address) only, right. Further bytes until STOP are
> discarded. So, we don't check if the message is too long and contains
> more than the status word. Maybe we should add that?

Yes I modified that.

> > +		break;
> > +	case I2C_SLAVE_STOP:
> > +		/* In case of incomplete write, don't handle host-notify */
> > +		if (status->notify_start) {
> > +			status->notify_start = false;
> > +			break;
> > +		}
> > +
> > +		ret = i2c_handle_smbus_host_notify(client->adapter,
> > +						   status->addr);
> > +		if (ret < 0)
> > +			return ret;
> > +		break;
> > +	default:
> 
> The missing cases are mandatory. From my testunit driver:
> 
>         case I2C_SLAVE_READ_REQUESTED:
>         case I2C_SLAVE_READ_PROCESSED:
>                 *val = 0xff;
>                 break;

Ok, done as well.

> > --- a/include/linux/i2c-smbus.h
> > +++ b/include/linux/i2c-smbus.h
> > @@ -38,6 +38,8 @@ static inline int of_i2c_setup_smbus_alert(struct i2c_adapter *adap)
> >  	return 0;
> >  }
> >  #endif
> > +struct i2c_client *i2c_new_smbus_host_notify_device(struct i2c_adapter *adapter);
> > +void i2c_free_smbus_host_notify_device(struct i2c_client *client);
> 
> Those need to be guarded with I2C_SLAVE as well. And an #else branch
> with empty/successful placeholders.

Ok understood.
diff mbox series

Patch

diff --git a/drivers/i2c/i2c-core-smbus.c b/drivers/i2c/i2c-core-smbus.c
index 56bb840142e3..3a37664fb5f6 100644
--- a/drivers/i2c/i2c-core-smbus.c
+++ b/drivers/i2c/i2c-core-smbus.c
@@ -708,3 +708,113 @@  int of_i2c_setup_smbus_alert(struct i2c_adapter *adapter)
 }
 EXPORT_SYMBOL_GPL(of_i2c_setup_smbus_alert);
 #endif
+
+#if IS_ENABLED(CONFIG_I2C_SLAVE)
+struct i2c_smbus_host_notify_status {
+	bool notify_start;
+	u8 addr;
+};
+
+static int i2c_smbus_host_notify_cb(struct i2c_client *client,
+				    enum i2c_slave_event event, u8 *val)
+{
+	struct i2c_smbus_host_notify_status *status = client->dev.platform_data;
+	int ret;
+
+	switch (event) {
+	case I2C_SLAVE_WRITE_REQUESTED:
+		status->notify_start = true;
+		break;
+	case I2C_SLAVE_WRITE_RECEIVED:
+		/* We only retrieve the first byte received (addr)
+		 * since there is currently no support to retrieve the data
+		 * parameter from the client.
+		 */
+		if (!status->notify_start)
+			break;
+		status->addr = *val;
+		status->notify_start = false;
+		break;
+	case I2C_SLAVE_STOP:
+		/* In case of incomplete write, don't handle host-notify */
+		if (status->notify_start) {
+			status->notify_start = false;
+			break;
+		}
+
+		ret = i2c_handle_smbus_host_notify(client->adapter,
+						   status->addr);
+		if (ret < 0)
+			return ret;
+		break;
+	default:
+		/* Only handle necessary events */
+		break;
+	}
+
+	return 0;
+}
+
+/**
+ * i2c_new_smbus_host_notify_device - get a client for SMBus host-notify support
+ * @adapter: the target adapter
+ * Context: can sleep
+ *
+ * Setup handling of the SMBus host-notify protocol on a given I2C bus segment.
+ *
+ * Handling is done by creating a device and its callback and handling data
+ * received via the SMBus host-notify address (0x8)
+ *
+ * This returns the client, which should be ultimately freed using
+ * i2c_free_smbus_host_notify_device(); or an ERRPTR to indicate an error.
+ */
+struct i2c_client *i2c_new_smbus_host_notify_device(struct i2c_adapter *adapter)
+{
+	struct i2c_board_info host_notify_board_info = {
+		I2C_BOARD_INFO("smbus_host_notify", 0x08),
+		.flags  = I2C_CLIENT_SLAVE,
+	};
+	struct i2c_smbus_host_notify_status *status;
+	struct i2c_client *client;
+	int ret;
+
+	status = kzalloc(sizeof(struct i2c_smbus_host_notify_status),
+			 GFP_KERNEL);
+	if (!status)
+		return ERR_PTR(-ENOMEM);
+
+	host_notify_board_info.platform_data = status;
+
+	client = i2c_new_client_device(adapter, &host_notify_board_info);
+	if (IS_ERR(client)) {
+		kfree(status);
+		return client;
+	}
+
+	ret = i2c_slave_register(client, i2c_smbus_host_notify_cb);
+	if (ret) {
+		i2c_unregister_device(client);
+		kfree(status);
+		return ERR_PTR(ret);
+	}
+
+	return client;
+}
+EXPORT_SYMBOL_GPL(i2c_new_smbus_host_notify_device);
+
+/**
+ * i2c_free_smbus_host_notify_device - free the client for SMBus host-notify
+ * support
+ * @client: the client to free
+ * Context: can sleep
+ *
+ * Free the i2c_client allocated via i2c_new_smbus_host_notify_device
+ */
+void i2c_free_smbus_host_notify_device(struct i2c_client *client)
+{
+	i2c_slave_unregister(client);
+	kfree(client->dev.platform_data);
+	i2c_unregister_device(client);
+}
+EXPORT_SYMBOL_GPL(i2c_free_smbus_host_notify_device);
+#endif
diff --git a/include/linux/i2c-smbus.h b/include/linux/i2c-smbus.h
index 1e4e0de4ef8b..974038a684ed 100644
--- a/include/linux/i2c-smbus.h
+++ b/include/linux/i2c-smbus.h
@@ -38,6 +38,8 @@  static inline int of_i2c_setup_smbus_alert(struct i2c_adapter *adap)
 	return 0;
 }
 #endif
+struct i2c_client *i2c_new_smbus_host_notify_device(struct i2c_adapter *adapter);
+void i2c_free_smbus_host_notify_device(struct i2c_client *client);
 
 #if IS_ENABLED(CONFIG_I2C_SMBUS) && IS_ENABLED(CONFIG_DMI)
 void i2c_register_spd(struct i2c_adapter *adap);