Skip to contentSkip to conversation

serdev API extension

PATCH Superseded Andrey Smirnov 3 participants 13 messages Mar 14, 2017 – Mar 17, 2017

Subsystems (2)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover serdev API extension

Rob,

These patches are a continuation of discussion about adding functionality to serdev API we had off-list.

Being added are a function to do a blocking write capable of dispatching multiple calls to write_buf() and some very minimalistic bus locking API.

Any feedback is appreciated.

Thanks,
Andrey Smirnov

Andrey Smirnov (2):
serdev: Add serdev_device_write subroutine
serdev: Add minimal bus locking API

 drivers/tty/serdev/core.c | 38 ++++++++++++++++++++++++++++++++++++++
 include/linux/serdev.h    | 34 ++++++++++++++++++++++++++++------
 2 files changed, 66 insertions(+), 6 deletions(-)
Signature · 1 line
2.9.3
#1 lore raw

PATCH 1/2 serdev: Add serdev_device_write subroutine

Add serdev_device_write() which is a blocking call allowing to transfer arbitraty amount of data (potentially exceeding amount that serdev_device_write_buf can process in a single call)

---
 drivers/tty/serdev/core.c | 37 +++++++++++++++++++++++++++++++++++++
 include/linux/serdev.h    | 23 +++++++++++++++++------
 2 files changed, 54 insertions(+), 6 deletions(-)
drivers/tty/serdev/core.c +37 -0
index f4c6c90..759e834 100644
@@ -128,6 +128,41 @@ int serdev_device_write_buf(struct serdev_device *serdev,
 }
 EXPORT_SYMBOL_GPL(serdev_device_write_buf);
 
+int serdev_device_write(struct serdev_device *serdev,
+			const unsigned char *buf, size_t count)
+{
+	int ret = count;
+
+	if (serdev->ops->write_wakeup)
+		return -EINVAL;
+
+	mutex_lock(&serdev->write_lock);
+
+	for (;;) {
+		size_t chunk;
+
+		reinit_completion(&serdev->write_wakeup);
+
+		chunk = serdev_device_write_buf(serdev, buf, count);
+		if (chunk < 0) {
+			ret = chunk;
+			goto done;
+		}
+
+		buf   += chunk;
+		count -= chunk;
+
+		if (!count)
+			break;
+
+		wait_for_completion(&serdev->write_wakeup);
+	}
+done:
+	mutex_unlock(&serdev->write_lock);
+	return ret;
+}
+EXPORT_SYMBOL_GPL(serdev_device_write);
+
 void serdev_device_write_flush(struct serdev_device *serdev)
 {
 	struct serdev_controller *ctrl = serdev->ctrl;
@@ -232,6 +267,8 @@ struct serdev_device *serdev_device_alloc(struct serdev_controller *ctrl)
 	serdev->dev.parent = &ctrl->dev;
 	serdev->dev.bus = &serdev_bus_type;
 	serdev->dev.type = &serdev_device_type;
+	init_completion(&serdev->write_wakeup);
+	mutex_init(&serdev->write_lock);
 	return serdev;
 }
 EXPORT_SYMBOL_GPL(serdev_device_alloc);
include/linux/serdev.h +17 -6
index 5176cdc..8f7aa35 100644
@@ -35,16 +35,19 @@ struct serdev_device_ops {
 
 /**
  * struct serdev_device - Basic representation of an serdev device
- * @dev:	Driver model representation of the device.
- * @nr:		Device number on serdev bus.
- * @ctrl:	serdev controller managing this device.
- * @ops:	Device operations.
+ * @dev:	 Driver model representation of the device.
+ * @nr:		 Device number on serdev bus.
+ * @ctrl:	 serdev controller managing this device.
+ * @ops:	 Device operations.
+ * @write_wakeup Completion used by serdev_device_write internally
  */
 struct serdev_device {
 	struct device dev;
 	int nr;
 	struct serdev_controller *ctrl;
 	const struct serdev_device_ops *ops;
+	struct completion write_wakeup;
+	struct mutex write_lock;
 };
 
 static inline struct serdev_device *to_serdev_device(struct device *d)
@@ -162,10 +165,13 @@ static inline void serdev_controller_write_wakeup(struct serdev_controller *ctrl
 {
 	struct serdev_device *serdev = ctrl->serdev;
 
-	if (!serdev || !serdev->ops->write_wakeup)
+	if (!serdev)
 		return;
 
-	serdev->ops->write_wakeup(serdev);
+	if (serdev->ops->write_wakeup)
+		serdev->ops->write_wakeup(serdev);
+	else
+		complete(&serdev->write_wakeup);
 }
 
 static inline int serdev_controller_receive_buf(struct serdev_controller *ctrl,
@@ -187,6 +193,7 @@ void serdev_device_close(struct serdev_device *);
 unsigned int serdev_device_set_baudrate(struct serdev_device *, unsigned int);
 void serdev_device_set_flow_control(struct serdev_device *, bool);
 int serdev_device_write_buf(struct serdev_device *, const unsigned char *, size_t);
+int serdev_device_write(struct serdev_device *serdev, const unsigned char *buf, size_t count);
 void serdev_device_write_flush(struct serdev_device *);
 int serdev_device_write_room(struct serdev_device *);
 
@@ -227,6 +234,10 @@ static inline int serdev_device_write_buf(struct serdev_device *sdev, const unsi
 {
 	return -ENODEV;
 }
+static inline int serdev_device_write(struct serdev_device *sdev, const unsigned char *buf, size_t count)
+{
+	return -ENODEV;
+}
 static inline void serdev_device_write_flush(struct serdev_device *sdev) {}
 static inline int serdev_device_write_room(struct serdev_device *sdev)
 {
Signature · 1 line
2.9.3
#9 lore raw

PATCH 2/2 serdev: Add minimal bus locking API

In reply to Andrey Smirnov

Add minimal bus locking API which is useful for serial devices that implement request-reply protocol

---
 drivers/tty/serdev/core.c |  1 +
 include/linux/serdev.h    | 11 +++++++++++
 2 files changed, 12 insertions(+)
drivers/tty/serdev/core.c +1 -0
index 759e834..20ca231 100644
@@ -269,6 +269,7 @@ struct serdev_device *serdev_device_alloc(struct serdev_controller *ctrl)
 	serdev->dev.type = &serdev_device_type;
 	init_completion(&serdev->write_wakeup);
 	mutex_init(&serdev->write_lock);
+	mutex_init(&serdev->bus_lock);
 	return serdev;
 }
 EXPORT_SYMBOL_GPL(serdev_device_alloc);
include/linux/serdev.h +11 -0
index 8f7aa35..6b73a79 100644
@@ -48,6 +48,7 @@ struct serdev_device {
 	const struct serdev_device_ops *ops;
 	struct completion write_wakeup;
 	struct mutex write_lock;
+	struct mutex bus_lock;
 };
 
 static inline struct serdev_device *to_serdev_device(struct device *d)
@@ -55,6 +56,16 @@ static inline struct serdev_device *to_serdev_device(struct device *d)
 	return container_of(d, struct serdev_device, dev);
 }
 
+static inline void serdev_device_bus_lock(struct serdev_device *serdev)
+{
+	mutex_lock(&serdev->bus_lock);
+}
+
+static inline void serdev_device_bus_unlock(struct serdev_device *serdev)
+{
+	mutex_unlock(&serdev->bus_lock);
+}
+
 /**
  * struct serdev_device_driver - serdev slave device driver
  * @driver:	serdev device drivers should initialize name field of this
Signature · 1 line
2.9.3
#2 lore raw

Re: [PATCH 1/2] serdev: Add serdev_device_write subroutine

In reply to Andrey Smirnov

drivers/tty/serdev/core.c int serdev_device_write_buf(struct serdev_device *serdev, · line 134

Quoted 9 lines · Andrey Smirnov

Add serdev_device_write() which is a blocking call allowing to transfer arbitraty amount of data (potentially exceeding amount that serdev_device_write_buf can process in a single call)

drivers/tty/serdev/core.c +4 -0
+int serdev_device_write(struct serdev_device *serdev,
+                       const unsigned char *buf, size_t count)
+{
 
+       int ret = count;

If count by some reason bigger than INT_MAX...

Quoted 5 lines
drivers/tty/serdev/core.c +5 -0
+
+       if (serdev->ops->write_wakeup)
+               return -EINVAL;
+
+       mutex_lock(&serdev->write_lock);

drivers/tty/serdev/core.c int serdev_device_write_buf(struct serdev_device *serdev, · line 147

Quoted 10 lines
drivers/tty/serdev/core.c +8 -0
+
+       for (;;) {
+               size_t chunk;
+
+               reinit_completion(&serdev->write_wakeup);
+
+               chunk = serdev_device_write_buf(serdev, buf, count);
 
 
+               if (chunk < 0) {

This will never happen. What kind of test did you try?

drivers/tty/serdev/core.c int serdev_device_write_buf(struct serdev_device *serdev, · line 155

Quoted 11 lines
drivers/tty/serdev/core.c +8 -0
+                       ret = chunk;
+                       goto done;
+               }
 
 
+
+               buf   += chunk;
+               count -= chunk;
+
 
+               if (!count)

What is supposed to be returned? Initial count? Does it make any sense?

Quoted 1 line

+ break;

Perhaps you need to refactor this function.

drivers/tty/serdev/core.c int serdev_device_write_buf(struct serdev_device *serdev, · line 160

Quoted 5 lines
drivers/tty/serdev/core.c +4 -0
+
+               wait_for_completion(&serdev->write_wakeup);
+       }
 
+done:

It would be nice to have a suffix, like

done_unlock:

But I'm pretty sure if you refactor the code in a smart way you will not need it.

Quoted 5 lines
include/linux/serdev.h +4 -0
+       mutex_unlock(&serdev->write_lock);
+       return ret;
+}
+EXPORT_SYMBOL_GPL(serdev_device_write);
 

include/linux/serdev.h struct serdev_device_ops { · line 41

Quoted 10 lines
include/linux/serdev.h +4 -4
 /**
  * struct serdev_device - Basic representation of an serdev device
- * @dev:       Driver model representation of the device.
- * @nr:                Device number on serdev bus.
- * @ctrl:      serdev controller managing this device.
- * @ops:       Device operations.
+ * @dev:        Driver model representation of the device.
+ * @nr:                 Device number on serdev bus.
+ * @ctrl:       serdev controller managing this device.
+ * @ops:        Device operations.

Does it make sense to shift? I would think of shorter field names instead.

Quoted 1 line

+ * @write_wakeup Completion used by serdev_device_write internally

Colon is missed.

Another filed is missed.

Quoted 14 lines

*/

 struct serdev_device {
        struct device dev;
        int nr;
        struct serdev_controller *ctrl;
        const struct serdev_device_ops *ops;
include/linux/serdev.h +2 -0
+       struct completion write_wakeup;
+       struct mutex write_lock;
 };
 
 static inline struct serdev_device *to_serdev_device(struct device *d)
@@ -162,10 +165,13 @@ static inline void serdev_controller_write_wakeup(struct serdev_controller *ctrl
 {
        struct serdev_device *serdev = ctrl->serdev;

include/linux/serdev.h static inline void serdev_controller_write_wakeup(struct serdev_controller *ctrl · line 174

Quoted 10 lines
include/linux/serdev.h +5 -2
 
-       if (!serdev || !serdev->ops->write_wakeup)
+       if (!serdev)
                return;
 
-       serdev->ops->write_wakeup(serdev);
+       if (serdev->ops->write_wakeup)
+               serdev->ops->write_wakeup(serdev);
+       else
+               complete(&serdev->write_wakeup);

By the way does this changes the possible context of application (atomic / non-atomic)?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#10 lore raw

Re: [PATCH 2/2] serdev: Add minimal bus locking API

In reply to Andrey Smirnov
Quoted 2 lines · Andrey Smirnov

Add minimal bus locking API which is useful for serial devices that implement request-reply protocol

Can you put an example here?

I'm not sure mutex is needed at all.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#6 lore raw

Re: [PATCH 1/2] serdev: Add serdev_device_write subroutine

In reply to Andrey Smirnov
Quoted 19 lines · Andrey Smirnov

Add serdev_device_write() which is a blocking call allowing to transfer arbitraty amount of data (potentially exceeding amount that serdev_device_write_buf can process in a single call)

---
 drivers/tty/serdev/core.c | 37 +++++++++++++++++++++++++++++++++++++
 include/linux/serdev.h    | 23 +++++++++++++++++------
 2 files changed, 54 insertions(+), 6 deletions(-)
drivers/tty/serdev/core.c +0 -0
index f4c6c90..759e834 100644

drivers/tty/serdev/core.c int serdev_device_write_buf(struct serdev_device *serdev, · line 132

Quoted 5 lines
drivers/tty/serdev/core.c +2 -0
@@ -128,6 +128,41 @@ int serdev_device_write_buf(struct serdev_device *serdev,
 }
 EXPORT_SYMBOL_GPL(serdev_device_write_buf);
 
+int serdev_device_write(struct serdev_device *serdev,
+                       const unsigned char *buf, size_t count)

_write vs. _write_buf are not all that clear what the difference is. but I don't have a better name.

Perhaps a timeout param is needed? This could never complete if CTS remains deasserted.

drivers/tty/serdev/core.c int serdev_device_write_buf(struct serdev_device *serdev, · line 144

Quoted 12 lines
drivers/tty/serdev/core.c +12 -0
+{
+       int ret = count;
+
+       if (serdev->ops->write_wakeup)
+               return -EINVAL;
+
+       mutex_lock(&serdev->write_lock);
+
+       for (;;) {
+               size_t chunk;
+
+               reinit_completion(&serdev->write_wakeup);

Perhaps write_comp instead as write_wakeup is already a function name.

Quoted 78 lines
drivers/tty/serdev/core.c +23 -0
+
+               chunk = serdev_device_write_buf(serdev, buf, count);
+               if (chunk < 0) {
+                       ret = chunk;
+                       goto done;
+               }
+
+               buf   += chunk;
+               count -= chunk;
+
+               if (!count)
+                       break;
+
+               wait_for_completion(&serdev->write_wakeup);
+       }
+done:
+       mutex_unlock(&serdev->write_lock);
+       return ret;
+}
+EXPORT_SYMBOL_GPL(serdev_device_write);
+
 void serdev_device_write_flush(struct serdev_device *serdev)
 {
        struct serdev_controller *ctrl = serdev->ctrl;
@@ -232,6 +267,8 @@ struct serdev_device *serdev_device_alloc(struct serdev_controller *ctrl)
        serdev->dev.parent = &ctrl->dev;
        serdev->dev.bus = &serdev_bus_type;
        serdev->dev.type = &serdev_device_type;
+       init_completion(&serdev->write_wakeup);
+       mutex_init(&serdev->write_lock);
        return serdev;
 }
 EXPORT_SYMBOL_GPL(serdev_device_alloc);
include/linux/serdev.h +12 -6
index 5176cdc..8f7aa35 100644
@@ -35,16 +35,19 @@ struct serdev_device_ops {
 
 /**
  * struct serdev_device - Basic representation of an serdev device
- * @dev:       Driver model representation of the device.
- * @nr:                Device number on serdev bus.
- * @ctrl:      serdev controller managing this device.
- * @ops:       Device operations.
+ * @dev:        Driver model representation of the device.
+ * @nr:                 Device number on serdev bus.
+ * @ctrl:       serdev controller managing this device.
+ * @ops:        Device operations.
+ * @write_wakeup Completion used by serdev_device_write internally
  */
 struct serdev_device {
        struct device dev;
        int nr;
        struct serdev_controller *ctrl;
        const struct serdev_device_ops *ops;
+       struct completion write_wakeup;
+       struct mutex write_lock;
 };
 
 static inline struct serdev_device *to_serdev_device(struct device *d)
@@ -162,10 +165,13 @@ static inline void serdev_controller_write_wakeup(struct serdev_controller *ctrl
 {
        struct serdev_device *serdev = ctrl->serdev;
 
-       if (!serdev || !serdev->ops->write_wakeup)
+       if (!serdev)
                return;
 
-       serdev->ops->write_wakeup(serdev);
+       if (serdev->ops->write_wakeup)
+               serdev->ops->write_wakeup(serdev);
+       else
+               complete(&serdev->write_wakeup);
 }
 
 static inline int serdev_controller_receive_buf(struct serdev_controller *ctrl,

include/linux/serdev.h void serdev_device_close(struct serdev_device *); · line 196

Quoted 4 lines
include/linux/serdev.h +1 -0
@@ -187,6 +193,7 @@ void serdev_device_close(struct serdev_device *);
 unsigned int serdev_device_set_baudrate(struct serdev_device *, unsigned int);
 void serdev_device_set_flow_control(struct serdev_device *, bool);
 int serdev_device_write_buf(struct serdev_device *, const unsigned char *, size_t);
+int serdev_device_write(struct serdev_device *serdev, const unsigned char *buf, size_t count);

Drop the param names to be consistent with the rest of the declarations.

Quoted 17 lines
include/linux/serdev.h +4 -1
 void serdev_device_write_flush(struct serdev_device *);
 int serdev_device_write_room(struct serdev_device *);
 
@@ -227,6 +234,10 @@ static inline int serdev_device_write_buf(struct serdev_device *sdev, const unsi
 {
        return -ENODEV;
 }
+static inline int serdev_device_write(struct serdev_device *sdev, const unsigned char *buf, size_t count)
+{
+       return -ENODEV;
+}
 static inline void serdev_device_write_flush(struct serdev_device *sdev) {}
 static inline int serdev_device_write_room(struct serdev_device *sdev)
 {
--

2.9.3

#12 lore raw

Re: [PATCH 2/2] serdev: Add minimal bus locking API

In reply to Andrey Smirnov
Quoted 2 lines · Andrey Smirnov

Add minimal bus locking API which is useful for serial devices that implement request-reply protocol

It's assumed that there's a single client, so I think the client drivers should manage any locking they need. Maybe that changes if we have multiple slave devices using RS-485.

Rob

#7 lore raw

Re: [PATCH 1/2] serdev: Add serdev_device_write subroutine

In reply to Rob Herring
Quoted 24 lines · Rob Herring
Quoted 22 lines · Andrey Smirnov

Add serdev_device_write() which is a blocking call allowing to transfer arbitraty amount of data (potentially exceeding amount that serdev_device_write_buf can process in a single call)

---
 drivers/tty/serdev/core.c | 37 +++++++++++++++++++++++++++++++++++++
 include/linux/serdev.h    | 23 +++++++++++++++++------
 2 files changed, 54 insertions(+), 6 deletions(-)
drivers/tty/serdev/core.c +0 -0
index f4c6c90..759e834 100644
@@ -128,6 +128,41 @@ int serdev_device_write_buf(struct serdev_device *serdev,
 }
 EXPORT_SYMBOL_GPL(serdev_device_write_buf);
 
Quoted 6 lines

drivers/tty/serdev/core.c int serdev_device_write_buf(struct serdev_device *serdev, · line 132

Quoted 2 lines
drivers/tty/serdev/core.c +2 -0
+int serdev_device_write(struct serdev_device *serdev,
+                       const unsigned char *buf, size_t count)

_write vs. _write_buf are not all that clear what the difference is. but I don't have a better name.

serdev_device_send?

Quoted 2 lines

Perhaps a timeout param is needed? This could never complete if CTS remains deasserted.

Yeah, I think it is a good idea, I'll add that in v2.

Quoted 16 lines

drivers/tty/serdev/core.c int serdev_device_write_buf(struct serdev_device *serdev, · line 144

Quoted 13 lines
drivers/tty/serdev/core.c +12 -0
+{
+       int ret = count;
+
+       if (serdev->ops->write_wakeup)
+               return -EINVAL;
+
+       mutex_lock(&serdev->write_lock);
+
+       for (;;) {
+               size_t chunk;
+
+               reinit_completion(&serdev->write_wakeup);

Perhaps write_comp instead as write_wakeup is already a function name.

OK, will do.

Quoted 76 lines
Quoted 76 lines
drivers/tty/serdev/core.c +23 -0
+
+               chunk = serdev_device_write_buf(serdev, buf, count);
+               if (chunk < 0) {
+                       ret = chunk;
+                       goto done;
+               }
+
+               buf   += chunk;
+               count -= chunk;
+
+               if (!count)
+                       break;
+
+               wait_for_completion(&serdev->write_wakeup);
+       }
+done:
+       mutex_unlock(&serdev->write_lock);
+       return ret;
+}
+EXPORT_SYMBOL_GPL(serdev_device_write);
+
 void serdev_device_write_flush(struct serdev_device *serdev)
 {
        struct serdev_controller *ctrl = serdev->ctrl;
@@ -232,6 +267,8 @@ struct serdev_device *serdev_device_alloc(struct serdev_controller *ctrl)
        serdev->dev.parent = &ctrl->dev;
        serdev->dev.bus = &serdev_bus_type;
        serdev->dev.type = &serdev_device_type;
+       init_completion(&serdev->write_wakeup);
+       mutex_init(&serdev->write_lock);
        return serdev;
 }
 EXPORT_SYMBOL_GPL(serdev_device_alloc);
include/linux/serdev.h +12 -6
index 5176cdc..8f7aa35 100644
@@ -35,16 +35,19 @@ struct serdev_device_ops {
 
 /**
  * struct serdev_device - Basic representation of an serdev device
- * @dev:       Driver model representation of the device.
- * @nr:                Device number on serdev bus.
- * @ctrl:      serdev controller managing this device.
- * @ops:       Device operations.
+ * @dev:        Driver model representation of the device.
+ * @nr:                 Device number on serdev bus.
+ * @ctrl:       serdev controller managing this device.
+ * @ops:        Device operations.
+ * @write_wakeup Completion used by serdev_device_write internally
  */
 struct serdev_device {
        struct device dev;
        int nr;
        struct serdev_controller *ctrl;
        const struct serdev_device_ops *ops;
+       struct completion write_wakeup;
+       struct mutex write_lock;
 };
 
 static inline struct serdev_device *to_serdev_device(struct device *d)
@@ -162,10 +165,13 @@ static inline void serdev_controller_write_wakeup(struct serdev_controller *ctrl
 {
        struct serdev_device *serdev = ctrl->serdev;
 
-       if (!serdev || !serdev->ops->write_wakeup)
+       if (!serdev)
                return;
 
-       serdev->ops->write_wakeup(serdev);
+       if (serdev->ops->write_wakeup)
+               serdev->ops->write_wakeup(serdev);
+       else
+               complete(&serdev->write_wakeup);
 }
 
Quoted 8 lines

include/linux/serdev.h void serdev_device_close(struct serdev_device *); · line 196

Quoted 6 lines
include/linux/serdev.h +1 -0
 static inline int serdev_controller_receive_buf(struct serdev_controller *ctrl,
@@ -187,6 +193,7 @@ void serdev_device_close(struct serdev_device *);
 unsigned int serdev_device_set_baudrate(struct serdev_device *, unsigned int);
 void serdev_device_set_flow_control(struct serdev_device *, bool);
 int serdev_device_write_buf(struct serdev_device *, const unsigned char *, size_t);
+int serdev_device_write(struct serdev_device *serdev, const unsigned char *buf, size_t count);

Drop the param names to be consistent with the rest of the declarations.

OK, will do.

Quoted 18 lines
Quoted 18 lines
include/linux/serdev.h +4 -1
 void serdev_device_write_flush(struct serdev_device *);
 int serdev_device_write_room(struct serdev_device *);
 
@@ -227,6 +234,10 @@ static inline int serdev_device_write_buf(struct serdev_device *sdev, const unsi
 {
        return -ENODEV;
 }
+static inline int serdev_device_write(struct serdev_device *sdev, const unsigned char *buf, size_t count)
+{
+       return -ENODEV;
+}
 static inline void serdev_device_write_flush(struct serdev_device *sdev) {}
 static inline int serdev_device_write_room(struct serdev_device *sdev)
 {
--

2.9.3

#3 lore raw

Re: [PATCH 1/2] serdev: Add serdev_device_write subroutine

In reply to Andy Shevchenko
Quoted 6 lines · Andy Shevchenko
Quoted 3 lines · Andrey Smirnov

Add serdev_device_write() which is a blocking call allowing to transfer arbitraty amount of data (potentially exceeding amount that serdev_device_write_buf can process in a single call)

Quoted 8 lines

drivers/tty/serdev/core.c int serdev_device_write_buf(struct serdev_device *serdev, · line 134

Quoted 5 lines
drivers/tty/serdev/core.c +4 -0
+int serdev_device_write(struct serdev_device *serdev,
+                       const unsigned char *buf, size_t count)
+{
 
+       int ret = count;

If count by some reason bigger than INT_MAX...

True, I was merely copying type signature of serdev_device_write_buf, which would have the same problem. I can add an appropriate check to the code, but at the same time, I'd love to know why it wasn't a concern in the latter function.

Quoted 14 lines
Quoted 12 lines
drivers/tty/serdev/core.c +12 -0
+
+       if (serdev->ops->write_wakeup)
+               return -EINVAL;
+
+       mutex_lock(&serdev->write_lock);
+
+       for (;;) {
+               size_t chunk;
+
+               reinit_completion(&serdev->write_wakeup);
+
+               chunk = serdev_device_write_buf(serdev, buf, count);
Quoted 4 lines
Quoted 1 line

+ if (chunk < 0) {

This will never happen. What kind of test did you try?

None of my code using that function ever hit that condition, OTOH, I am not sure why I should ignore the API's signed return type, which I assume is so in order to facilitate negative error returns. My thinking is that even if in the current codebase is incapable of ever returning negative result today, that doesn't mean it won't ever be, and IMHO the chances of that are no different from the chances of someone passing 'count' that is bigger than INT_MAX. Given that I'd rather keep the check until the type signature of serdev_device_write_buf changes.

Quoted 5 lines
Quoted 3 lines
drivers/tty/serdev/core.c +3 -0
+                       ret = chunk;
+                       goto done;
+               }
Quoted 9 lines

drivers/tty/serdev/core.c int serdev_device_write_buf(struct serdev_device *serdev, · line 155

Quoted 6 lines
drivers/tty/serdev/core.c +5 -0
+
+               buf   += chunk;
+               count -= chunk;
+
 
+               if (!count)

What is supposed to be returned? Initial count? Does it make any sense?

That part of this code is not very well thought out. I think returning number of bytes successfully written would make more sense. I'll change it to do that in v2, but I am open to suggestions.

Quoted 10 lines
Quoted 1 line

+ break;

Perhaps you need to refactor this function.

drivers/tty/serdev/core.c int serdev_device_write_buf(struct serdev_device *serdev, · line 160

Quoted 5 lines
drivers/tty/serdev/core.c +4 -0
+
+               wait_for_completion(&serdev->write_wakeup);
+       }
 
+done:
Quoted 8 lines

It would be nice to have a suffix, like

done_unlock:

But I'm pretty sure if you refactor the code in a smart way you will not need it.

Yeah, I agree, it should be possible to make that loop more concise. I'll give it a try in v2.

Quoted 16 lines
Quoted 5 lines
include/linux/serdev.h +4 -0
+       mutex_unlock(&serdev->write_lock);
+       return ret;
+}
+EXPORT_SYMBOL_GPL(serdev_device_write);
 

include/linux/serdev.h struct serdev_device_ops { · line 41

Quoted 10 lines
include/linux/serdev.h +4 -4
 /**
  * struct serdev_device - Basic representation of an serdev device
- * @dev:       Driver model representation of the device.
- * @nr:                Device number on serdev bus.
- * @ctrl:      serdev controller managing this device.
- * @ops:       Device operations.
+ * @dev:        Driver model representation of the device.
+ * @nr:                 Device number on serdev bus.
+ * @ctrl:       serdev controller managing this device.
+ * @ops:        Device operations.
Quoted 1 line

Does it make sense to shift? I would think of shorter field names instead.

I can see how that might make the diff look nice, but OTOH, I'd rather not limit myself to only ~4 letters.

Quoted 6 lines
Quoted 2 lines

+ * @write_wakeup Completion used by serdev_device_write internally

Colon is missed.

Another filed is missed.

Yep, thank you, will fix in v2.

Quoted 20 lines
Quoted 20 lines

*/

 struct serdev_device {
        struct device dev;
        int nr;
        struct serdev_controller *ctrl;
        const struct serdev_device_ops *ops;
include/linux/serdev.h +3 -1
+       struct completion write_wakeup;
+       struct mutex write_lock;
 };
 
 static inline struct serdev_device *to_serdev_device(struct device *d)
@@ -162,10 +165,13 @@ static inline void serdev_controller_write_wakeup(struct serdev_controller *ctrl
 {
        struct serdev_device *serdev = ctrl->serdev;
 
-       if (!serdev || !serdev->ops->write_wakeup)
+       if (!serdev)
                return;
 
Quoted 8 lines

include/linux/serdev.h static inline void serdev_controller_write_wakeup(struct serdev_controller *ctrl · line 174

Quoted 5 lines
include/linux/serdev.h +4 -1
-       serdev->ops->write_wakeup(serdev);
+       if (serdev->ops->write_wakeup)
+               serdev->ops->write_wakeup(serdev);
+       else
+               complete(&serdev->write_wakeup);

By the way does this changes the possible context of application (atomic / non-atomic)?

It should be possible to call complete() in atomic context, so I don't think it does.

Quoted 4 lines

--
With Best Regards,
Andy Shevchenko

#11 lore raw

Re: [PATCH 2/2] serdev: Add minimal bus locking API

In reply to Andy Shevchenko
Quoted 8 lines · Andy Shevchenko
Quoted 2 lines · Andrey Smirnov

Add minimal bus locking API which is useful for serial devices that implement request-reply protocol

Can you put an example here?

I'm not sure mutex is needed at all.

My use case is a "supervisory" microcontroller connected to SoC via UART various aspects of which are exposed via MFD. I saw this kind of "device design pattern" a number of times in my career, so I thought and abstraction to help dealing with cases like that might be useful.

However, since Rob mentioned that API's expectation is that any such locking is driver's responsibility, I'll drop this patch.

Thanks,
Andrey Smirnov

#4 lore raw

Re: [PATCH 1/2] serdev: Add serdev_device_write subroutine

In reply to Andrey Smirnov
Quoted 9 lines · Andrey Smirnov
Quoted 7 lines · Andy Shevchenko
Quoted 3 lines · Andrey Smirnov
drivers/tty/serdev/core.c +3 -0
+int serdev_device_write(struct serdev_device *serdev,
+                       const unsigned char *buf, size_t count)
+{
Quoted 8 lines
Quoted 3 lines
Quoted 1 line

+ int ret = count;

If count by some reason bigger than INT_MAX...

True, I was merely copying type signature of serdev_device_write_buf, which would have the same problem. I can add an appropriate check to the code, but at the same time, I'd love to know why it wasn't a concern in the latter function.

Perhaps we may survive without changing it, just document that count should not be bigger than INT_MAX.
OTOH it's counter intuitive to have size_t type which is used as int.

Quoted 12 lines
Quoted 11 lines

drivers/tty/serdev/core.c int serdev_device_write_buf(struct serdev_device *serdev, · line 147

Quoted 9 lines
drivers/tty/serdev/core.c +7 -0
+       for (;;) {
+               size_t chunk;
+
+               reinit_completion(&serdev->write_wakeup);
+
+               chunk = serdev_device_write_buf(serdev, buf, count);
 
 
+               if (chunk < 0) {

This will never happen. What kind of test did you try?

Quoted 9 lines

None of my code using that function ever hit that condition, OTOH, I am not sure why I should ignore the API's signed return type, which I assume is so in order to facilitate negative error returns. My thinking is that even if in the current codebase is incapable of ever returning negative result today, that doesn't mean it won't ever be, and IMHO the chances of that are no different from the chances of someone passing 'count' that is bigger than INT_MAX. Given that I'd rather keep the check until the type signature of serdev_device_write_buf changes.

You missed the point

size_t is *unsigned* type!

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

+ if (!count)

What is supposed to be returned? Initial count? Does it make any sense?

I'll change it to do that in v2, but I am open to suggestions.

Since function lacks of description (or I missed it?) I am out of knowledge what you are trying to achieve here.

Quoted 2 lines

I can see how that might make the diff look nice, but OTOH, I'd rather not limit myself to only ~4 letters.

Up to you and Rob.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw

Re: [PATCH 1/2] serdev: Add serdev_device_write subroutine

Quoted 46 lines · Andy Shevchenko
Quoted 9 lines · Andrey Smirnov
Quoted 7 lines · Andy Shevchenko
Quoted 3 lines · Andrey Smirnov
drivers/tty/serdev/core.c +3 -0
+int serdev_device_write(struct serdev_device *serdev,
+                       const unsigned char *buf, size_t count)
+{
Quoted 8 lines
Quoted 3 lines
Quoted 1 line

+ int ret = count;

If count by some reason bigger than INT_MAX...

True, I was merely copying type signature of serdev_device_write_buf, which would have the same problem. I can add an appropriate check to the code, but at the same time, I'd love to know why it wasn't a concern in the latter function.

Perhaps we may survive without changing it, just document that count should not be bigger than INT_MAX.
OTOH it's counter intuitive to have size_t type which is used as int.

Quoted 12 lines
Quoted 11 lines

drivers/tty/serdev/core.c int serdev_device_write_buf(struct serdev_device *serdev, · line 147

Quoted 9 lines
drivers/tty/serdev/core.c +7 -0
+       for (;;) {
+               size_t chunk;
+
+               reinit_completion(&serdev->write_wakeup);
+
+               chunk = serdev_device_write_buf(serdev, buf, count);
 
 
+               if (chunk < 0) {

This will never happen. What kind of test did you try?

Quoted 9 lines

None of my code using that function ever hit that condition, OTOH, I am not sure why I should ignore the API's signed return type, which I assume is so in order to facilitate negative error returns. My thinking is that even if in the current codebase is incapable of ever returning negative result today, that doesn't mean it won't ever be, and IMHO the chances of that are no different from the chances of someone passing 'count' that is bigger than INT_MAX. Given that I'd rather keep the check until the type signature of serdev_device_write_buf changes.

Quoted 4 lines

You missed the point

size_t is *unsigned* type!

Ah! I did miss the point! Yes, that's a bug for sure, will fix in v2. Good catch, thank you!

Quoted 9 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

+ if (!count)

What is supposed to be returned? Initial count? Does it make any sense?

I'll change it to do that in v2, but I am open to suggestions.

Since function lacks of description (or I missed it?) I am out of knowledge what you are trying to achieve here.

There's a description of what I had in mind for this function in commit message. It does need to be documented in the code, I agree.

Thanks,
Andrey Smirnov

#8 lore raw

Re: [PATCH 1/2] serdev: Add serdev_device_write subroutine

In reply to Andrey Smirnov
Quoted 25 lines · Andrey Smirnov
Quoted 24 lines · Rob Herring
Quoted 22 lines · Andrey Smirnov

Add serdev_device_write() which is a blocking call allowing to transfer arbitraty amount of data (potentially exceeding amount that serdev_device_write_buf can process in a single call)

---
 drivers/tty/serdev/core.c | 37 +++++++++++++++++++++++++++++++++++++
 include/linux/serdev.h    | 23 +++++++++++++++++------
 2 files changed, 54 insertions(+), 6 deletions(-)
drivers/tty/serdev/core.c +0 -0
index f4c6c90..759e834 100644
@@ -128,6 +128,41 @@ int serdev_device_write_buf(struct serdev_device *serdev,
 }
 EXPORT_SYMBOL_GPL(serdev_device_write_buf);
 
Quoted 8 lines
Quoted 6 lines

drivers/tty/serdev/core.c int serdev_device_write_buf(struct serdev_device *serdev, · line 132

Quoted 2 lines
drivers/tty/serdev/core.c +2 -0
+int serdev_device_write(struct serdev_device *serdev,
+                       const unsigned char *buf, size_t count)

_write vs. _write_buf are not all that clear what the difference is. but I don't have a better name.

serdev_device_send?

send vs. write_buf? Still not that clear.

Quoted 4 lines
Quoted 2 lines

Perhaps a timeout param is needed? This could never complete if CTS remains deasserted.

Yeah, I think it is a good idea, I'll add that in v2.

Maybe just combine the 2 functions and a timeout of 0 follows the original behavior of serdev_device_write_buf. I'm not sure if we could handle write_wakeup in that case though.

Rob