All of lore.kernel.org
 help / color / mirror / Atom feed
From: Simon Glass <sjg@chromium.org>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH 24/30] dm: sandbox: Add driver-model block-device support for sandbox
Date: Sun, 14 Feb 2016 19:16:53 -0700	[thread overview]
Message-ID: <1455502619-16093-25-git-send-email-sjg@chromium.org> (raw)
In-Reply-To: <1455502619-16093-1-git-send-email-sjg@chromium.org>

Update the host driver to support driver model for block devices. A future
commit will remove the old code, but for now it is useful to be able to use
it both with and without CONFIG_BLK.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 cmd/host.c                |   9 +++-
 drivers/block/sandbox.c   | 118 +++++++++++++++++++++++++++++++++++++++++++++-
 include/sandboxblockdev.h |   2 +
 3 files changed, 127 insertions(+), 2 deletions(-)

diff --git a/cmd/host.c b/cmd/host.c
index ee219ce..8d84415 100644
--- a/cmd/host.c
+++ b/cmd/host.c
@@ -5,6 +5,7 @@
  */
 
 #include <common.h>
+#include <dm.h>
 #include <fs.h>
 #include <part.h>
 #include <sandboxblockdev.h>
@@ -80,7 +81,13 @@ static int do_host_info(cmd_tbl_t *cmdtp, int flag, int argc,
 
 			continue;
 		}
-		struct host_block_dev *host_dev = blk_dev->priv;
+		struct host_block_dev *host_dev;
+
+#ifdef CONFIG_BLK
+		host_dev = dev_get_priv(blk_dev->bdev);
+#else
+		host_dev = blk_dev->priv;
+#endif
 		printf("%12lu %s\n", (unsigned long)blk_dev->lba,
 		       host_dev->filename);
 	}
diff --git a/drivers/block/sandbox.c b/drivers/block/sandbox.c
index dde9d68..f5a8688 100644
--- a/drivers/block/sandbox.c
+++ b/drivers/block/sandbox.c
@@ -4,14 +4,20 @@
  * SPDX-License-Identifier:	GPL-2.0+
  */
 
-#include <config.h>
 #include <common.h>
+#include <blk.h>
+#include <dm.h>
+#include <fdtdec.h>
 #include <part.h>
 #include <os.h>
 #include <malloc.h>
 #include <sandboxblockdev.h>
 #include <asm/errno.h>
+#include <dm/device-internal.h>
 
+DECLARE_GLOBAL_DATA_PTR;
+
+#ifndef CONFIG_BLK
 static struct host_block_dev host_devices[CONFIG_HOST_MAX_DEVICES];
 
 static struct host_block_dev *find_host_device(int dev)
@@ -21,7 +27,17 @@ static struct host_block_dev *find_host_device(int dev)
 
 	return NULL;
 }
+#endif
+
+#ifdef CONFIG_BLK
+static unsigned long host_block_read(struct udevice *dev,
+				     unsigned long start, lbaint_t blkcnt,
+				     void *buffer)
+{
+	struct host_block_dev *host_dev = dev_get_priv(dev);
+	struct blk_desc *block_dev = dev_get_uclass_platdata(dev);
 
+#else
 static unsigned long host_block_read(struct blk_desc *block_dev,
 				     unsigned long start, lbaint_t blkcnt,
 				     void *buffer)
@@ -31,6 +47,7 @@ static unsigned long host_block_read(struct blk_desc *block_dev,
 
 	if (!host_dev)
 		return -1;
+#endif
 
 	if (os_lseek(host_dev->fd, start * block_dev->blksz, OS_SEEK_SET) ==
 			-1) {
@@ -43,12 +60,21 @@ static unsigned long host_block_read(struct blk_desc *block_dev,
 	return -1;
 }
 
+#ifdef CONFIG_BLK
+static unsigned long host_block_write(struct udevice *dev,
+				      unsigned long start, lbaint_t blkcnt,
+				      const void *buffer)
+{
+	struct host_block_dev *host_dev = dev_get_priv(dev);
+	struct blk_desc *block_dev = dev_get_uclass_platdata(dev);
+#else
 static unsigned long host_block_write(struct blk_desc *block_dev,
 				      unsigned long start, lbaint_t blkcnt,
 				      const void *buffer)
 {
 	int dev = block_dev->devnum;
 	struct host_block_dev *host_dev = find_host_device(dev);
+#endif
 
 	if (os_lseek(host_dev->fd, start * block_dev->blksz, OS_SEEK_SET) ==
 			-1) {
@@ -61,6 +87,70 @@ static unsigned long host_block_write(struct blk_desc *block_dev,
 	return -1;
 }
 
+#ifdef CONFIG_BLK
+int host_dev_bind(int devnum, char *filename)
+{
+	struct host_block_dev *host_dev;
+	struct udevice *dev;
+	char dev_name[20], *str, *fname;
+	int ret, fd;
+
+	/* Remove and unbind the old device, if any */
+	ret = blk_get_device(IF_TYPE_HOST, devnum, &dev);
+	if (ret == 0) {
+		ret = device_remove(dev);
+		if (ret)
+			return ret;
+		ret = device_unbind(dev);
+		if (ret)
+			return ret;
+	} else if (ret != -ENODEV) {
+		return ret;
+	}
+
+	if (!filename)
+		return 0;
+
+	sprintf(dev_name, "host%d", devnum);
+	str = strdup(dev_name);
+	if (!str)
+		return -ENOMEM;
+	fname = strdup(filename);
+	if (!fname) {
+		free(str);
+		return -ENOMEM;
+	}
+
+	fd = os_open(filename, OS_O_RDWR);
+	if (fd == -1) {
+		printf("Failed to access host backing file '%s'\n", filename);
+		ret = -ENOENT;
+		goto err;
+	}
+	ret = blk_create_device(gd->dm_root, "sandbox_host_blk", str,
+				IF_TYPE_HOST, devnum, 512,
+				os_lseek(fd, 0, OS_SEEK_END), &dev);
+	if (ret)
+		goto err_file;
+	ret = device_probe(dev);
+	if (ret) {
+		device_unbind(dev);
+		goto err_file;
+	}
+
+	host_dev = dev_get_priv(dev);
+	host_dev->fd = fd;
+	host_dev->filename = fname;
+
+	return blk_prepare_device(dev);
+err_file:
+	os_close(fd);
+err:
+	free(fname);
+	free(str);
+	return ret;
+}
+#else
 int host_dev_bind(int dev, char *filename)
 {
 	struct host_block_dev *host_dev = find_host_device(dev);
@@ -100,9 +190,19 @@ int host_dev_bind(int dev, char *filename)
 
 	return 0;
 }
+#endif
 
 int host_get_dev_err(int devnum, struct blk_desc **blk_devp)
 {
+#ifdef CONFIG_BLK
+	struct udevice *dev;
+	int ret;
+
+	ret = blk_get_device(IF_TYPE_HOST, devnum, &dev);
+	if (ret)
+		return ret;
+	*blk_devp = dev_get_uclass_platdata(dev);
+#else
 	struct host_block_dev *host_dev = find_host_device(devnum);
 
 	if (!host_dev)
@@ -112,6 +212,8 @@ int host_get_dev_err(int devnum, struct blk_desc **blk_devp)
 		return -ENOENT;
 
 	*blk_devp = &host_dev->blk_dev;
+#endif
+
 	return 0;
 }
 
@@ -124,3 +226,17 @@ struct blk_desc *host_get_dev(int dev)
 
 	return blk_dev;
 }
+
+#ifdef CONFIG_BLK
+static const struct blk_ops sandbox_host_blk_ops = {
+	.read	= host_block_read,
+	.write	= host_block_write,
+};
+
+U_BOOT_DRIVER(sandbox_host_blk) = {
+	.name		= "sandbox_host_blk",
+	.id		= UCLASS_BLK,
+	.ops		= &sandbox_host_blk_ops,
+	.priv_auto_alloc_size	= sizeof(struct host_block_dev),
+};
+#endif
diff --git a/include/sandboxblockdev.h b/include/sandboxblockdev.h
index 59f9519..5174f45 100644
--- a/include/sandboxblockdev.h
+++ b/include/sandboxblockdev.h
@@ -8,7 +8,9 @@
 #define __SANDBOX_BLOCK_DEV__
 
 struct host_block_dev {
+#ifndef CONFIG_BLK
 	struct blk_desc blk_dev;
+#endif
 	char *filename;
 	int fd;
 };
-- 
2.7.0.rc3.207.g0ac5344

  parent reply	other threads:[~2016-02-15  2:16 UTC|newest]

Thread overview: 69+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-02-15  2:16 [U-Boot] [PATCH 00/30] dm: Add driver-model support for block drivers Simon Glass
2016-02-15  2:16 ` [U-Boot] [PATCH 01/30] dm: Drop the block_dev_desc_t typedef Simon Glass
2016-02-16 10:09   ` Bin Meng
2016-02-15  2:16 ` [U-Boot] [PATCH 02/30] dm: pci: Break out the common region display code Simon Glass
2016-02-16 10:09   ` Bin Meng
2016-02-29  4:18     ` Simon Glass
2016-02-17  0:19   ` Stephen Warren
2016-02-15  2:16 ` [U-Boot] [PATCH 03/30] dm: part: Correct a sandbox build warning Simon Glass
2016-02-16 10:09   ` Bin Meng
2016-02-29  4:18     ` Simon Glass
2016-02-15  2:16 ` [U-Boot] [PATCH 04/30] dm: fdtdec: " Simon Glass
2016-02-15  2:16 ` [U-Boot] [PATCH 05/30] dm: part: Drop the common.h header Simon Glass
2016-02-16 10:09   ` Bin Meng
2016-02-15  2:16 ` [U-Boot] [PATCH 06/30] dm: Add a new header for block devices Simon Glass
2016-02-16 10:10   ` Bin Meng
2016-02-15  2:16 ` [U-Boot] [PATCH 07/30] dm: blk: Convert interface type to an enum Simon Glass
2016-02-16 10:10   ` Bin Meng
2016-02-15  2:16 ` [U-Boot] [PATCH 08/30] dm: blk: Add comments to a few functions Simon Glass
2016-02-16 10:10   ` Bin Meng
2016-02-15  2:16 ` [U-Boot] [PATCH 09/30] dm: blk: Rename get_dev() to blk_get_dev() Simon Glass
2016-02-16 10:10   ` Bin Meng
2016-02-15  2:16 ` [U-Boot] [PATCH 10/30] dm: blk: Rename get_device() to blk_get_device_str() Simon Glass
2016-02-16 14:02   ` Bin Meng
2016-02-16 23:14   ` Stephen Warren
2016-02-15  2:16 ` [U-Boot] [PATCH 11/30] dm: blk: Rename get_device_and_partition() Simon Glass
2016-02-16 14:02   ` Bin Meng
2016-02-15  2:16 ` [U-Boot] [PATCH 12/30] dm: part: Add a cast to avoid a compiler warning Simon Glass
2016-02-16 14:02   ` Bin Meng
2016-02-15  2:16 ` [U-Boot] [PATCH 13/30] dm: sandbox: Enable all partition types Simon Glass
2016-02-15 22:37   ` Tom Rini
2016-02-16 14:02   ` Bin Meng
2016-02-15  2:16 ` [U-Boot] [PATCH 14/30] dm: part: Convert partition API use to linker lists Simon Glass
2016-02-15 22:37   ` Tom Rini
2016-02-16 14:25   ` Bin Meng
2016-02-29  4:18     ` Simon Glass
2016-02-17  6:41   ` Stephen Warren
2016-02-29  4:48     ` Simon Glass
2016-02-15  2:16 ` [U-Boot] [PATCH 15/30] dm: part: Rename some partition functions Simon Glass
2016-02-16 14:25   ` Bin Meng
2016-02-29  4:19     ` Simon Glass
2016-02-15  2:16 ` [U-Boot] [PATCH 16/30] dm: cbfs: Fix handling of invalid type Simon Glass
2016-02-16 14:51   ` Bin Meng
2016-02-29  4:19     ` Simon Glass
2016-02-15  2:16 ` [U-Boot] [PATCH 17/30] dm: sandbox: Enable cbfs and cramfs Simon Glass
2016-02-16 14:51   ` Bin Meng
2016-02-15  2:16 ` [U-Boot] [PATCH 18/30] dm: block: Rename device number member dev to devnum Simon Glass
2016-02-16 14:51   ` Bin Meng
2016-02-16 23:23   ` Stephen Warren
2016-02-29  4:19     ` Simon Glass
2016-02-15  2:16 ` [U-Boot] [PATCH 19/30] dm: block: Adjust device calls to go through helpers function Simon Glass
2016-02-16 14:51   ` Bin Meng
2016-02-15  2:16 ` [U-Boot] [PATCH 20/30] dm: usb: Avoid exceeding available array size for storage devices Simon Glass
2016-02-15  2:16 ` [U-Boot] [PATCH 21/30] dm: usb: Tidy up storage code ready for driver model conversion Simon Glass
2016-02-15  2:16 ` [U-Boot] [PATCH 22/30] dm: blk: Add a block-device uclass Simon Glass
2016-02-17  4:49   ` Bin Meng
2016-02-15  2:16 ` [U-Boot] [PATCH 23/30] dm: sandbox: Prepare block driver for driver-model conversion Simon Glass
2016-02-15  2:16 ` Simon Glass [this message]
2016-02-16 23:34   ` [U-Boot] [PATCH 24/30] dm: sandbox: Add driver-model block-device support for sandbox Stephen Warren
2016-02-29  4:19     ` Simon Glass
2016-02-15  2:16 ` [U-Boot] [PATCH 25/30] dm: usb: Convert USB storage to use driver-model for block devs Simon Glass
2016-02-15  2:16 ` [U-Boot] [PATCH 26/30] dm: usb: Unbind old block devices when shutting down USB Simon Glass
2016-02-15  2:16 ` [U-Boot] [PATCH 27/30] dm: sandbox: Switch over to use DM for block devices Simon Glass
2016-02-15  2:16 ` [U-Boot] [PATCH 28/30] dm: sandbox: Drop the pre-DM host implementation Simon Glass
2016-02-15  2:16 ` [U-Boot] [PATCH 29/30] dm: usb: Clean up USB after each test Simon Glass
2016-02-15  2:16 ` [U-Boot] [PATCH 30/30] dm: blk: Add tests for block devices Simon Glass
2016-02-15 22:37 ` [U-Boot] [PATCH 00/30] dm: Add driver-model support for block drivers Tom Rini
2016-02-29  4:19   ` Simon Glass
2016-02-16 23:43 ` Stephen Warren
2016-02-29  4:19   ` Simon Glass

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=1455502619-16093-25-git-send-email-sjg@chromium.org \
    --to=sjg@chromium.org \
    --cc=u-boot@lists.denx.de \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.