linux-serial.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Andrey Smirnov <andrew.smirnov@gmail.com>
To: Rob Herring <robh@kernel.org>
Cc: Andrey Smirnov <andrew.smirnov@gmail.com>,
	cphealy@gmail.com, Guenter Roeck <linux@roeck-us.net>,
	linux-serial@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH 1/2] serdev: Add serdev_device_write subroutine
Date: Tue, 14 Mar 2017 06:48:18 -0700	[thread overview]
Message-ID: <20170314134819.19476-2-andrew.smirnov@gmail.com> (raw)
In-Reply-To: <20170314134819.19476-1-andrew.smirnov@gmail.com>

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)

Cc: cphealy@gmail.com
Cc: Guenter Roeck <linux@roeck-us.net>
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 | 37 +++++++++++++++++++++++++++++++++++++
 include/linux/serdev.h    | 23 +++++++++++++++++------
 2 files changed, 54 insertions(+), 6 deletions(-)

diff --git a/drivers/tty/serdev/core.c b/drivers/tty/serdev/core.c
index f4c6c90..759e834 100644
--- a/drivers/tty/serdev/core.c
+++ b/drivers/tty/serdev/core.c
@@ -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);
diff --git a/include/linux/serdev.h b/include/linux/serdev.h
index 5176cdc..8f7aa35 100644
--- a/include/linux/serdev.h
+++ b/include/linux/serdev.h
@@ -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)
 {
-- 
2.9.3

  reply	other threads:[~2017-03-14 13:48 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-03-14 13:48 [PATCH 0/2] serdev API extension Andrey Smirnov
2017-03-14 13:48 ` Andrey Smirnov [this message]
2017-03-14 23:17   ` [PATCH 1/2] serdev: Add serdev_device_write subroutine Andy Shevchenko
2017-03-16 14:23     ` Andrey Smirnov
2017-03-16 15:09       ` Andy Shevchenko
2017-03-17 13:33         ` Andrey Smirnov
2017-03-15 14:18   ` Rob Herring
2017-03-16 13:33     ` Andrey Smirnov
2017-03-17 16:31       ` Rob Herring
2017-03-14 13:48 ` [PATCH 2/2] serdev: Add minimal bus locking API Andrey Smirnov
2017-03-14 23:20   ` Andy Shevchenko
2017-03-16 14:37     ` Andrey Smirnov
2017-03-15 14:25   ` Rob Herring

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170314134819.19476-2-andrew.smirnov@gmail.com \
    --to=andrew.smirnov@gmail.com \
    --cc=cphealy@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-serial@vger.kernel.org \
    --cc=linux@roeck-us.net \
    --cc=robh@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).