* [PATCH] serdev: Replace serdev_device_write_buf with serdev_device_write
@ 2017-03-20 16:56 Andrey Smirnov
2017-03-24 18:31 ` Rob Herring
0 siblings, 1 reply; 2+ messages in thread
From: Andrey Smirnov @ 2017-03-20 16:56 UTC (permalink / raw)
To: Rob Herring
Cc: Andrey Smirnov, cphealy, Guenter Roeck, Andy Shevchenko,
linux-serial, linux-kernel
Convert serdev_device_write_buf's code to be able to transfer amount of
data potentially exceeding "write room" at the moment of invocation.
To support that, also add serdev_device_write_wakeup.
Drivers wanting to use full extent of serdev_device_write functionality
asre expected to provide serdev_device_write_wakeup as a sole handler of
.write_wakeup event or call it as a part of driver's custom
.write_wakeup code.
Drivers wanting to retain old serdev_device_write_buf behaviour can
replace those call to calls to serdev_device_write with timeout of
0. Providing .write_wakeup handler in such case is optional.
Cc: cphealy@gmail.com
Cc: Guenter Roeck <linux@roeck-us.net>
Cc: Andy Shevchenko <andy.shevchenko@gmail.com>
Cc: linux-serial@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com>
---
drivers/tty/serdev/core.c | 36 +++++++++++++++++++++++++++++++-----
include/linux/serdev.h | 11 +++++++++--
2 files changed, 40 insertions(+), 7 deletions(-)
diff --git a/drivers/tty/serdev/core.c b/drivers/tty/serdev/core.c
index f4c6c90..9962d84 100644
--- a/drivers/tty/serdev/core.c
+++ b/drivers/tty/serdev/core.c
@@ -116,17 +116,41 @@ void serdev_device_close(struct serdev_device *serdev)
}
EXPORT_SYMBOL_GPL(serdev_device_close);
-int serdev_device_write_buf(struct serdev_device *serdev,
- const unsigned char *buf, size_t count)
+void serdev_device_write_wakeup(struct serdev_device *serdev)
+{
+ complete(&serdev->write_comp);
+}
+EXPORT_SYMBOL_GPL(serdev_device_write_wakeup);
+
+int serdev_device_write(struct serdev_device *serdev,
+ const unsigned char *buf, size_t count,
+ unsigned long timeout)
{
struct serdev_controller *ctrl = serdev->ctrl;
+ int ret;
- if (!ctrl || !ctrl->ops->write_buf)
+ if (!ctrl || !ctrl->ops->write_buf ||
+ (timeout && !serdev->ops->write_wakeup))
return -EINVAL;
- return ctrl->ops->write_buf(ctrl, buf, count);
+ mutex_lock(&serdev->write_lock);
+ do {
+ reinit_completion(&serdev->write_comp);
+
+ ret = ctrl->ops->write_buf(ctrl, buf, count);
+ if (ret < 0)
+ break;
+
+ buf += ret;
+ count -= ret;
+
+ } while (count &&
+ wait_for_completion_timeout(&serdev->write_comp,
+ timeout));
+ mutex_unlock(&serdev->write_lock);
+ return ret < 0 ? ret : (count ? -ETIMEDOUT : 0);
}
-EXPORT_SYMBOL_GPL(serdev_device_write_buf);
+EXPORT_SYMBOL_GPL(serdev_device_write);
void serdev_device_write_flush(struct serdev_device *serdev)
{
@@ -232,6 +256,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_comp);
+ mutex_init(&serdev->write_lock);
return serdev;
}
EXPORT_SYMBOL_GPL(serdev_device_alloc);
diff --git a/include/linux/serdev.h b/include/linux/serdev.h
index 5176cdc..18cbbed 100644
--- a/include/linux/serdev.h
+++ b/include/linux/serdev.h
@@ -39,12 +39,17 @@ struct serdev_device_ops {
* @nr: Device number on serdev bus.
* @ctrl: serdev controller managing this device.
* @ops: Device operations.
+ * @write_comp Completion used by serdev_device_write internally
+ * @write_lock Mutext used to esure exclusive access to the bus when
+ * writing data with serdev_device_write()
*/
struct serdev_device {
struct device dev;
int nr;
struct serdev_controller *ctrl;
const struct serdev_device_ops *ops;
+ struct completion write_comp;
+ struct mutex write_lock;
};
static inline struct serdev_device *to_serdev_device(struct device *d)
@@ -186,10 +191,12 @@ int serdev_device_open(struct serdev_device *);
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);
+void serdev_device_write_wakeup(struct serdev_device *);
+int serdev_device_write(struct serdev_device *, const unsigned char *, size_t, unsigned long);
void serdev_device_write_flush(struct serdev_device *);
int serdev_device_write_room(struct serdev_device *);
+
/*
* serdev device driver functions
*/
@@ -223,7 +230,7 @@ static inline unsigned int serdev_device_set_baudrate(struct serdev_device *sdev
return 0;
}
static inline void serdev_device_set_flow_control(struct serdev_device *sdev, bool enable) {}
-static inline int serdev_device_write_buf(struct serdev_device *sdev, const unsigned char *buf, size_t count)
+static inline int serdev_device_write(struct serdev_device *sdev, const unsigned char *buf, size_t count)
{
return -ENODEV;
}
--
2.9.3
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] serdev: Replace serdev_device_write_buf with serdev_device_write
2017-03-20 16:56 [PATCH] serdev: Replace serdev_device_write_buf with serdev_device_write Andrey Smirnov
@ 2017-03-24 18:31 ` Rob Herring
0 siblings, 0 replies; 2+ messages in thread
From: Rob Herring @ 2017-03-24 18:31 UTC (permalink / raw)
To: Andrey Smirnov
Cc: Chris Healy, Guenter Roeck, Andy Shevchenko, linux-serial, linux-kernel
On Mon, Mar 20, 2017 at 11:56 AM, Andrey Smirnov
<andrew.smirnov@gmail.com> wrote:
> Convert serdev_device_write_buf's code to be able to transfer amount of
> data potentially exceeding "write room" at the moment of invocation.
>
> To support that, also add serdev_device_write_wakeup.
>
> Drivers wanting to use full extent of serdev_device_write functionality
> asre expected to provide serdev_device_write_wakeup as a sole handler of
> .write_wakeup event or call it as a part of driver's custom
> .write_wakeup code.
>
> Drivers wanting to retain old serdev_device_write_buf behaviour can
> replace those call to calls to serdev_device_write with timeout of
> 0. Providing .write_wakeup handler in such case is optional.
Looks good, just a couple of things.
>
> Cc: cphealy@gmail.com
> Cc: Guenter Roeck <linux@roeck-us.net>
> Cc: Andy Shevchenko <andy.shevchenko@gmail.com>
> Cc: linux-serial@vger.kernel.org
> Cc: linux-kernel@vger.kernel.org
> Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com>
> ---
> drivers/tty/serdev/core.c | 36 +++++++++++++++++++++++++++++++-----
> include/linux/serdev.h | 11 +++++++++--
> 2 files changed, 40 insertions(+), 7 deletions(-)
>
> diff --git a/drivers/tty/serdev/core.c b/drivers/tty/serdev/core.c
> index f4c6c90..9962d84 100644
> --- a/drivers/tty/serdev/core.c
> +++ b/drivers/tty/serdev/core.c
> @@ -116,17 +116,41 @@ void serdev_device_close(struct serdev_device *serdev)
> }
> EXPORT_SYMBOL_GPL(serdev_device_close);
>
> -int serdev_device_write_buf(struct serdev_device *serdev,
> - const unsigned char *buf, size_t count)
> +void serdev_device_write_wakeup(struct serdev_device *serdev)
> +{
> + complete(&serdev->write_comp);
> +}
> +EXPORT_SYMBOL_GPL(serdev_device_write_wakeup);
> +
> +int serdev_device_write(struct serdev_device *serdev,
> + const unsigned char *buf, size_t count,
> + unsigned long timeout)
> {
> struct serdev_controller *ctrl = serdev->ctrl;
> + int ret;
>
> - if (!ctrl || !ctrl->ops->write_buf)
> + if (!ctrl || !ctrl->ops->write_buf ||
> + (timeout && !serdev->ops->write_wakeup))
> return -EINVAL;
>
> - return ctrl->ops->write_buf(ctrl, buf, count);
> + mutex_lock(&serdev->write_lock);
> + do {
> + reinit_completion(&serdev->write_comp);
> +
> + ret = ctrl->ops->write_buf(ctrl, buf, count);
> + if (ret < 0)
> + break;
> +
> + buf += ret;
> + count -= ret;
> +
> + } while (count &&
> + wait_for_completion_timeout(&serdev->write_comp,
> + timeout));
This is going to wait for timeout time on each loop. We want the
timeout passed in to be the total time.
> + mutex_unlock(&serdev->write_lock);
> + return ret < 0 ? ret : (count ? -ETIMEDOUT : 0);
> }
> -EXPORT_SYMBOL_GPL(serdev_device_write_buf);
> +EXPORT_SYMBOL_GPL(serdev_device_write);
>
> void serdev_device_write_flush(struct serdev_device *serdev)
> {
> @@ -232,6 +256,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_comp);
> + mutex_init(&serdev->write_lock);
> return serdev;
> }
> EXPORT_SYMBOL_GPL(serdev_device_alloc);
> diff --git a/include/linux/serdev.h b/include/linux/serdev.h
> index 5176cdc..18cbbed 100644
> --- a/include/linux/serdev.h
> +++ b/include/linux/serdev.h
> @@ -39,12 +39,17 @@ struct serdev_device_ops {
> * @nr: Device number on serdev bus.
> * @ctrl: serdev controller managing this device.
> * @ops: Device operations.
> + * @write_comp Completion used by serdev_device_write internally
> + * @write_lock Mutext used to esure exclusive access to the bus when
> + * writing data with serdev_device_write()
> */
> struct serdev_device {
> struct device dev;
> int nr;
> struct serdev_controller *ctrl;
> const struct serdev_device_ops *ops;
> + struct completion write_comp;
> + struct mutex write_lock;
> };
>
> static inline struct serdev_device *to_serdev_device(struct device *d)
> @@ -186,10 +191,12 @@ int serdev_device_open(struct serdev_device *);
> 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);
> +void serdev_device_write_wakeup(struct serdev_device *);
> +int serdev_device_write(struct serdev_device *, const unsigned char *, size_t, unsigned long);
> void serdev_device_write_flush(struct serdev_device *);
> int serdev_device_write_room(struct serdev_device *);
>
> +
> /*
> * serdev device driver functions
> */
> @@ -223,7 +230,7 @@ static inline unsigned int serdev_device_set_baudrate(struct serdev_device *sdev
> return 0;
> }
> static inline void serdev_device_set_flow_control(struct serdev_device *sdev, bool enable) {}
> -static inline int serdev_device_write_buf(struct serdev_device *sdev, const unsigned char *buf, size_t count)
We probably should keep a wrapper for serdev_device_write_buf to avoid
cross tree dependencies. However, the Nokia BT support is going to
have them anyway.
> +static inline int serdev_device_write(struct serdev_device *sdev, const unsigned char *buf, size_t count)
> {
> return -ENODEV;
> }
> --
> 2.9.3
>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2017-03-24 18:31 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-03-20 16:56 [PATCH] serdev: Replace serdev_device_write_buf with serdev_device_write Andrey Smirnov
2017-03-24 18:31 ` Rob Herring
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).